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
1473a424c9b2fa657081fe1bde0f77a56868ca35
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/ring_theory/tensor_product.lean
4bc5cf2d9cf8bc8435e70a0cef30125531a9d056
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,342
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johan Commelin -/ import linear_algebra.tensor_product import algebra.algebra.tower /-! # The tensor product of R-algebras Let `R` be a (semi)ring and `A` an `R`-algebra. In this file we: - Define the `A`-module structure on `A βŠ— M`, for an `R`-module `M`. - Define the `R`-algebra structure on `A βŠ— B`, for another `R`-algebra `B`. and provide the structure isomorphisms * `R βŠ—[R] A ≃ₐ[R] A` * `A βŠ—[R] R ≃ₐ[R] A` * `A βŠ—[R] B ≃ₐ[R] B βŠ—[R] A` * `((A βŠ—[R] B) βŠ—[R] C) ≃ₐ[R] (A βŠ—[R] (B βŠ—[R] C))` ## Main declaration - `linear_map.base_change A f` is the `A`-linear map `A βŠ— f`, for an `R`-linear map `f`. ## Implementation notes The heterobasic definitions below such as: * `tensor_product.algebra_tensor_module.curry` * `tensor_product.algebra_tensor_module.uncurry` * `tensor_product.algebra_tensor_module.lcurry` * `tensor_product.algebra_tensor_module.lift` * `tensor_product.algebra_tensor_module.lift.equiv` * `tensor_product.algebra_tensor_module.mk` * `tensor_product.algebra_tensor_module.assoc` are just more general versions of the definitions already in `linear_algebra/tensor_product`. We could thus consider replacing the less general definitions with these ones. If we do this, we probably should still implement the less general ones as abbreviations to the more general ones with fewer type arguments. -/ universes u v₁ vβ‚‚ v₃ vβ‚„ open_locale tensor_product open tensor_product namespace tensor_product variables {R A M N P : Type*} /-! ### The `A`-module structure on `A βŠ—[R] M` -/ open linear_map open algebra (lsmul) namespace algebra_tensor_module section semiring 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 [add_comm_monoid N] [module R N] variables [add_comm_monoid P] [module R P] [module A P] [is_scalar_tower R A P] lemma smul_eq_lsmul_rtensor (a : A) (x : M βŠ—[R] N) : a β€’ x = (lsmul R M a).rtensor N x := rfl /-- Heterobasic version of `tensor_product.curry`: Given a linear map `M βŠ—[R] N β†’[A] P`, compose it with the canonical bilinear map `M β†’[A] N β†’[R] M βŠ—[R] N` to form a bilinear map `M β†’[A] N β†’[R] P`. -/ @[simps] def curry (f : (M βŠ—[R] N) β†’β‚—[A] P) : M β†’β‚—[A] (N β†’β‚—[R] P) := { map_smul' := Ξ» c x, linear_map.ext $ Ξ» y, f.map_smul c (x βŠ—β‚œ y), .. curry (f.restrict_scalars R) } lemma restrict_scalars_curry (f : (M βŠ—[R] N) β†’β‚—[A] P) : restrict_scalars R (curry f) = curry (f.restrict_scalars R) := rfl /-- Just as `tensor_product.ext` is marked `ext` instead of `tensor_product.ext'`, this is a better `ext` lemma than `tensor_product.algebra_tensor_module.ext` below. See note [partially-applied ext lemmas]. -/ @[ext] lemma curry_injective : function.injective (curry : (M βŠ— N β†’β‚—[A] P) β†’ (M β†’β‚—[A] N β†’β‚—[R] P)) := Ξ» x y h, linear_map.restrict_scalars_injective R $ curry_injective $ (congr_arg (linear_map.restrict_scalars R) h : _) theorem ext {g h : (M βŠ—[R] N) β†’β‚—[A] P} (H : βˆ€ x y, g (x βŠ—β‚œ y) = h (x βŠ—β‚œ y)) : g = h := curry_injective $ linear_map.extβ‚‚ H end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables [add_comm_monoid N] [module R N] variables [add_comm_monoid P] [module R P] [module A P] [is_scalar_tower R A P] /-- Heterobasic version of `tensor_product.lift`: Constructing a linear map `M βŠ—[R] N β†’[A] P` given a bilinear map `M β†’[A] N β†’[R] P` with the property that its composition with the canonical bilinear map `M β†’[A] N β†’[R] M βŠ—[R] N` is the given bilinear map `M β†’[A] N β†’[R] P`. -/ @[simps] def lift (f : M β†’β‚—[A] (N β†’β‚—[R] P)) : (M βŠ—[R] N) β†’β‚—[A] P := { map_smul' := Ξ» c, show βˆ€ x : M βŠ—[R] N, (lift (f.restrict_scalars R)).comp (lsmul R _ c) x = (lsmul R _ c).comp (lift (f.restrict_scalars R)) x, from ext_iff.1 $ tensor_product.ext' $ Ξ» x y, by simp only [comp_apply, algebra.lsmul_coe, smul_tmul', lift.tmul, coe_restrict_scalars_eq_coe, f.map_smul, smul_apply], .. lift (f.restrict_scalars R) } @[simp] lemma lift_tmul (f : M β†’β‚—[A] (N β†’β‚—[R] P)) (x : M) (y : N) : lift f (x βŠ—β‚œ y) = f x y := lift.tmul' x y variables (R A M N P) /-- Heterobasic version of `tensor_product.uncurry`: Linearly constructing a linear map `M βŠ—[R] N β†’[A] P` given a bilinear map `M β†’[A] N β†’[R] P` with the property that its composition with the canonical bilinear map `M β†’[A] N β†’[R] M βŠ—[R] N` is the given bilinear map `M β†’[A] N β†’[R] P`. -/ @[simps] def uncurry : (M β†’β‚—[A] (N β†’β‚—[R] P)) β†’β‚—[A] ((M βŠ—[R] N) β†’β‚—[A] P) := { to_fun := lift, map_add' := Ξ» f g, ext $ Ξ» x y, by simp only [lift_tmul, add_apply], map_smul' := Ξ» c f, ext $ Ξ» x y, by simp only [lift_tmul, smul_apply] } /-- Heterobasic version of `tensor_product.lcurry`: Given a linear map `M βŠ—[R] N β†’[A] P`, compose it with the canonical bilinear map `M β†’[A] N β†’[R] M βŠ—[R] N` to form a bilinear map `M β†’[A] N β†’[R] P`. -/ @[simps] def lcurry : ((M βŠ—[R] N) β†’β‚—[A] P) β†’β‚—[A] (M β†’β‚—[A] (N β†’β‚—[R] P)) := { to_fun := curry, map_add' := Ξ» f g, rfl, map_smul' := Ξ» c f, rfl } /-- Heterobasic version of `tensor_product.lift.equiv`: A linear equivalence constructing a linear map `M βŠ—[R] N β†’[A] P` given a bilinear map `M β†’[A] N β†’[R] P` with the property that its composition with the canonical bilinear map `M β†’[A] N β†’[R] M βŠ—[R] N` is the given bilinear map `M β†’[A] N β†’[R] P`. -/ def lift.equiv : (M β†’β‚—[A] (N β†’β‚—[R] P)) ≃ₗ[A] ((M βŠ—[R] N) β†’β‚—[A] P) := linear_equiv.of_linear (uncurry R A M N P) (lcurry R A M N P) (linear_map.ext $ Ξ» f, ext $ Ξ» x y, lift_tmul _ x y) (linear_map.ext $ Ξ» f, linear_map.ext $ Ξ» x, linear_map.ext $ Ξ» y, lift_tmul f x y) variables (R A M N P) /-- Heterobasic version of `tensor_product.mk`: The canonical bilinear map `M β†’[A] N β†’[R] M βŠ—[R] N`. -/ @[simps] def mk : M β†’β‚—[A] N β†’β‚—[R] M βŠ—[R] N := { map_smul' := Ξ» c x, rfl, .. mk R M N } local attribute [ext] tensor_product.ext /-- Heterobasic version of `tensor_product.assoc`: Linear equivalence between `(M βŠ—[A] N) βŠ—[R] P` and `M βŠ—[A] (N βŠ—[R] P)`. -/ def assoc : ((M βŠ—[A] P) βŠ—[R] N) ≃ₗ[A] (M βŠ—[A] (P βŠ—[R] N)) := linear_equiv.of_linear (lift $ tensor_product.uncurry A _ _ _ $ comp (lcurry R A _ _ _) $ tensor_product.mk A M (P βŠ—[R] N)) (tensor_product.uncurry A _ _ _ $ comp (uncurry R A _ _ _) $ by apply tensor_product.curry; exact (mk R A _ _)) (by { ext, refl, }) (by { ext, refl, }) end comm_semiring end algebra_tensor_module end tensor_product namespace linear_map open tensor_product /-! ### The base-change of a linear map of `R`-modules to a linear map of `A`-modules -/ section semiring variables {R A B M N : Type*} [comm_semiring R] variables [semiring A] [algebra R A] [semiring B] [algebra R B] variables [add_comm_monoid M] [module R M] [add_comm_monoid N] [module R N] variables (r : R) (f g : M β†’β‚—[R] N) variables (A) /-- `base_change A f` for `f : M β†’β‚—[R] N` is the `A`-linear map `A βŠ—[R] M β†’β‚—[A] A βŠ—[R] N`. -/ def base_change (f : M β†’β‚—[R] N) : A βŠ—[R] M β†’β‚—[A] A βŠ—[R] N := { to_fun := f.ltensor A, map_add' := (f.ltensor A).map_add, map_smul' := Ξ» a x, show (f.ltensor A) (rtensor M (algebra.lmul R A a) x) = (rtensor N ((algebra.lmul R A) a)) ((ltensor A f) x), by simp only [← comp_apply, ltensor_comp_rtensor, rtensor_comp_ltensor] } variables {A} @[simp] lemma base_change_tmul (a : A) (x : M) : f.base_change A (a βŠ—β‚œ x) = a βŠ—β‚œ (f x) := rfl lemma base_change_eq_ltensor : (f.base_change A : A βŠ— M β†’ A βŠ— N) = f.ltensor A := rfl @[simp] lemma base_change_add : (f + g).base_change A = f.base_change A + g.base_change A := by { ext, simp [base_change_eq_ltensor], } @[simp] lemma base_change_zero : base_change A (0 : M β†’β‚—[R] N) = 0 := by { ext, simp [base_change_eq_ltensor], } @[simp] lemma base_change_smul : (r β€’ f).base_change A = r β€’ (f.base_change A) := by { ext, simp [base_change_tmul], } variables (R A M N) /-- `base_change` as a linear map. -/ @[simps] def base_change_hom : (M β†’β‚—[R] N) β†’β‚—[R] A βŠ—[R] M β†’β‚—[A] A βŠ—[R] N := { to_fun := base_change A, map_add' := base_change_add, map_smul' := base_change_smul } end semiring section ring variables {R A B M N : Type*} [comm_ring R] variables [ring A] [algebra R A] [ring B] [algebra R B] variables [add_comm_group M] [module R M] [add_comm_group N] [module R N] variables (f g : M β†’β‚—[R] N) @[simp] lemma base_change_sub : (f - g).base_change A = f.base_change A - g.base_change A := by { ext, simp [base_change_eq_ltensor], } @[simp] lemma base_change_neg : (-f).base_change A = -(f.base_change A) := by { ext, simp [base_change_eq_ltensor], } end ring end linear_map namespace algebra namespace tensor_product section semiring variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type vβ‚‚} [semiring B] [algebra R B] /-! ### The `R`-algebra structure on `A βŠ—[R] B` -/ /-- (Implementation detail) The multiplication map on `A βŠ—[R] B`, for a fixed pure tensor in the first argument, as an `R`-linear map. -/ def mul_aux (a₁ : A) (b₁ : B) : (A βŠ—[R] B) β†’β‚—[R] (A βŠ—[R] B) := tensor_product.map (lmul_left R a₁) (lmul_left R b₁) @[simp] lemma mul_aux_apply (a₁ aβ‚‚ : A) (b₁ bβ‚‚ : B) : (mul_aux a₁ b₁) (aβ‚‚ βŠ—β‚œ[R] bβ‚‚) = (a₁ * aβ‚‚) βŠ—β‚œ[R] (b₁ * bβ‚‚) := rfl /-- (Implementation detail) The multiplication map on `A βŠ—[R] B`, as an `R`-bilinear map. -/ def mul : (A βŠ—[R] B) β†’β‚—[R] (A βŠ—[R] B) β†’β‚—[R] (A βŠ—[R] B) := tensor_product.lift $ linear_map.mkβ‚‚ R mul_aux (Ξ» x₁ xβ‚‚ y, tensor_product.ext' $ Ξ» x' y', by simp only [mul_aux_apply, linear_map.add_apply, add_mul, add_tmul]) (Ξ» c x y, tensor_product.ext' $ Ξ» x' y', by simp only [mul_aux_apply, linear_map.smul_apply, smul_tmul', smul_mul_assoc]) (Ξ» x y₁ yβ‚‚, tensor_product.ext' $ Ξ» x' y', by simp only [mul_aux_apply, linear_map.add_apply, add_mul, tmul_add]) (Ξ» c x y, tensor_product.ext' $ Ξ» x' y', by simp only [mul_aux_apply, linear_map.smul_apply, smul_tmul, smul_tmul', smul_mul_assoc]) @[simp] lemma mul_apply (a₁ aβ‚‚ : A) (b₁ bβ‚‚ : B) : mul (a₁ βŠ—β‚œ[R] b₁) (aβ‚‚ βŠ—β‚œ[R] bβ‚‚) = (a₁ * aβ‚‚) βŠ—β‚œ[R] (b₁ * bβ‚‚) := rfl lemma mul_assoc' (mul : (A βŠ—[R] B) β†’β‚—[R] (A βŠ—[R] B) β†’β‚—[R] (A βŠ—[R] B)) (h : βˆ€ (a₁ aβ‚‚ a₃ : A) (b₁ bβ‚‚ b₃ : B), mul (mul (a₁ βŠ—β‚œ[R] b₁) (aβ‚‚ βŠ—β‚œ[R] bβ‚‚)) (a₃ βŠ—β‚œ[R] b₃) = mul (a₁ βŠ—β‚œ[R] b₁) (mul (aβ‚‚ βŠ—β‚œ[R] bβ‚‚) (a₃ βŠ—β‚œ[R] b₃))) : βˆ€ (x y z : A βŠ—[R] B), mul (mul x y) z = mul x (mul y z) := begin intros, apply tensor_product.induction_on x, { simp, }, apply tensor_product.induction_on y, { simp, }, apply tensor_product.induction_on z, { simp, }, { intros, simp [h], }, { intros, simp [linear_map.map_add, *], }, { intros, simp [linear_map.map_add, *], }, { intros, simp [linear_map.map_add, *], }, end lemma mul_assoc (x y z : A βŠ—[R] B) : mul (mul x y) z = mul x (mul y z) := mul_assoc' mul (by { intros, simp only [mul_apply, mul_assoc], }) x y z lemma one_mul (x : A βŠ—[R] B) : mul (1 βŠ—β‚œ 1) x = x := begin apply tensor_product.induction_on x; simp {contextual := tt}, end lemma mul_one (x : A βŠ—[R] B) : mul x (1 βŠ—β‚œ 1) = x := begin apply tensor_product.induction_on x; simp {contextual := tt}, end instance : semiring (A βŠ—[R] B) := { zero := 0, add := (+), one := 1 βŠ—β‚œ 1, mul := Ξ» a b, mul a b, one_mul := one_mul, mul_one := mul_one, mul_assoc := mul_assoc, zero_mul := by simp, mul_zero := by simp, left_distrib := by simp, right_distrib := by simp, .. (by apply_instance : add_comm_monoid (A βŠ—[R] B)) }. lemma one_def : (1 : A βŠ—[R] B) = (1 : A) βŠ—β‚œ (1 : B) := rfl @[simp] lemma tmul_mul_tmul (a₁ aβ‚‚ : A) (b₁ bβ‚‚ : B) : (a₁ βŠ—β‚œ[R] b₁) * (aβ‚‚ βŠ—β‚œ[R] bβ‚‚) = (a₁ * aβ‚‚) βŠ—β‚œ[R] (b₁ * bβ‚‚) := rfl @[simp] lemma tmul_pow (a : A) (b : B) (k : β„•) : (a βŠ—β‚œ[R] b)^k = (a^k) βŠ—β‚œ[R] (b^k) := begin induction k with k ih, { simp [one_def], }, { simp [pow_succ, ih], } end /-- The algebra map `R β†’+* (A βŠ—[R] B)` giving `A βŠ—[R] B` the structure of an `R`-algebra. -/ def tensor_algebra_map : R β†’+* (A βŠ—[R] B) := { to_fun := Ξ» r, algebra_map R A r βŠ—β‚œ[R] 1, map_one' := by { simp, refl }, map_mul' := by simp, map_zero' := by simp [zero_tmul], map_add' := by simp [add_tmul], } instance : algebra R (A βŠ—[R] B) := { commutes' := Ξ» r x, begin apply tensor_product.induction_on x, { simp, }, { intros a b, simp [tensor_algebra_map, algebra.commutes], }, { intros y y' h h', simp at h h', simp [mul_add, add_mul, h, h'], }, end, smul_def' := Ξ» r x, begin apply tensor_product.induction_on x, { simp [smul_zero], }, { intros a b, rw [tensor_algebra_map, ←tmul_smul, ←smul_tmul, algebra.smul_def r a], simp, }, { intros, dsimp, simp [smul_add, mul_add, *], }, end, .. tensor_algebra_map, .. (by apply_instance : module R (A βŠ—[R] B)) }. @[simp] lemma algebra_map_apply (r : R) : (algebra_map R (A βŠ—[R] B)) r = ((algebra_map R A) r) βŠ—β‚œ[R] 1 := rfl variables {C : Type v₃} [semiring C] [algebra R C] @[ext] theorem ext {g h : (A βŠ—[R] B) →ₐ[R] C} (H : βˆ€ a b, g (a βŠ—β‚œ b) = h (a βŠ—β‚œ b)) : g = h := begin apply @alg_hom.to_linear_map_injective R (A βŠ—[R] B) C _ _ _ _ _ _ _ _, ext, simp [H], end /-- The algebra morphism `A →ₐ[R] A βŠ—[R] B` sending `a` to `a βŠ—β‚œ 1`. -/ def include_left : A →ₐ[R] A βŠ—[R] B := { to_fun := Ξ» a, a βŠ—β‚œ 1, map_zero' := by simp, map_add' := by simp [add_tmul], map_one' := rfl, map_mul' := by simp, commutes' := by simp, } @[simp] lemma include_left_apply (a : A) : (include_left : A →ₐ[R] A βŠ—[R] B) a = a βŠ—β‚œ 1 := rfl /-- The algebra morphism `B →ₐ[R] A βŠ—[R] B` sending `b` to `1 βŠ—β‚œ b`. -/ def include_right : B →ₐ[R] A βŠ—[R] B := { to_fun := Ξ» b, 1 βŠ—β‚œ b, map_zero' := by simp, map_add' := by simp [tmul_add], map_one' := rfl, map_mul' := by simp, commutes' := Ξ» r, begin simp only [algebra_map_apply], transitivity r β€’ ((1 : A) βŠ—β‚œ[R] (1 : B)), { rw [←tmul_smul, algebra.smul_def], simp, }, { simp [algebra.smul_def], }, end, } @[simp] lemma include_right_apply (b : B) : (include_right : B →ₐ[R] A βŠ—[R] B) b = 1 βŠ—β‚œ b := rfl end semiring section ring variables {R : Type u} [comm_ring R] variables {A : Type v₁} [ring A] [algebra R A] variables {B : Type vβ‚‚} [ring B] [algebra R B] instance : ring (A βŠ—[R] B) := { .. (by apply_instance : add_comm_group (A βŠ—[R] B)), .. (by apply_instance : semiring (A βŠ—[R] B)) }. end ring section comm_ring variables {R : Type u} [comm_ring R] variables {A : Type v₁} [comm_ring A] [algebra R A] variables {B : Type vβ‚‚} [comm_ring B] [algebra R B] instance : comm_ring (A βŠ—[R] B) := { mul_comm := Ξ» x y, begin apply tensor_product.induction_on x, { simp, }, { intros a₁ b₁, apply tensor_product.induction_on y, { simp, }, { intros aβ‚‚ bβ‚‚, simp [mul_comm], }, { intros aβ‚‚ bβ‚‚ ha hb, simp [mul_add, add_mul, ha, hb], }, }, { intros x₁ xβ‚‚ h₁ hβ‚‚, simp [mul_add, add_mul, h₁, hβ‚‚], }, end .. (by apply_instance : ring (A βŠ—[R] B)) }. end comm_ring /-- Verify that typeclass search finds the ring structure on `A βŠ—[β„€] B` when `A` and `B` are merely rings, by treating both as `β„€`-algebras. -/ example {A : Type v₁} [ring A] {B : Type vβ‚‚} [ring B] : ring (A βŠ—[β„€] B) := by apply_instance /-- Verify that typeclass search finds the comm_ring structure on `A βŠ—[β„€] B` when `A` and `B` are merely comm_rings, by treating both as `β„€`-algebras. -/ example {A : Type v₁} [comm_ring A] {B : Type vβ‚‚} [comm_ring B] : comm_ring (A βŠ—[β„€] B) := by apply_instance /-! We now build the structure maps for the symmetric monoidal category of `R`-algebras. -/ section monoidal section variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type vβ‚‚} [semiring B] [algebra R B] variables {C : Type v₃} [semiring C] [algebra R C] variables {D : Type vβ‚„} [semiring D] [algebra R D] /-- Build an algebra morphism from a linear map out of a tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_hom_of_linear_map_tensor_product (f : A βŠ—[R] B β†’β‚—[R] C) (w₁ : βˆ€ (a₁ aβ‚‚ : A) (b₁ bβ‚‚ : B), f ((a₁ * aβ‚‚) βŠ—β‚œ (b₁ * bβ‚‚)) = f (a₁ βŠ—β‚œ b₁) * f (aβ‚‚ βŠ—β‚œ bβ‚‚)) (wβ‚‚ : βˆ€ r, f ((algebra_map R A) r βŠ—β‚œ[R] 1) = (algebra_map R C) r): A βŠ—[R] B →ₐ[R] C := { map_one' := by simpa using wβ‚‚ 1, map_zero' := by simp, map_mul' := Ξ» x y, begin apply tensor_product.induction_on x, { simp, }, { intros a₁ b₁, apply tensor_product.induction_on y, { simp, }, { intros aβ‚‚ bβ‚‚, simp [w₁], }, { intros x₁ xβ‚‚ h₁ hβ‚‚, simp at h₁, simp at hβ‚‚, simp [mul_add, add_mul, h₁, hβ‚‚], }, }, { intros x₁ xβ‚‚ h₁ hβ‚‚, simp at h₁, simp at hβ‚‚, simp [mul_add, add_mul, h₁, hβ‚‚], } end, commutes' := Ξ» r, by simp [wβ‚‚], .. f } @[simp] lemma alg_hom_of_linear_map_tensor_product_apply (f w₁ wβ‚‚ x) : (alg_hom_of_linear_map_tensor_product f w₁ wβ‚‚ : A βŠ—[R] B →ₐ[R] C) x = f x := rfl /-- Build an algebra equivalence from a linear equivalence out of a tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_equiv_of_linear_equiv_tensor_product (f : A βŠ—[R] B ≃ₗ[R] C) (w₁ : βˆ€ (a₁ aβ‚‚ : A) (b₁ bβ‚‚ : B), f ((a₁ * aβ‚‚) βŠ—β‚œ (b₁ * bβ‚‚)) = f (a₁ βŠ—β‚œ b₁) * f (aβ‚‚ βŠ—β‚œ bβ‚‚)) (wβ‚‚ : βˆ€ r, f ((algebra_map R A) r βŠ—β‚œ[R] 1) = (algebra_map R C) r): A βŠ—[R] B ≃ₐ[R] C := { .. alg_hom_of_linear_map_tensor_product (f : A βŠ—[R] B β†’β‚—[R] C) w₁ wβ‚‚, .. f } @[simp] lemma alg_equiv_of_linear_equiv_tensor_product_apply (f w₁ wβ‚‚ x) : (alg_equiv_of_linear_equiv_tensor_product f w₁ wβ‚‚ : A βŠ—[R] B ≃ₐ[R] C) x = f x := rfl /-- Build an algebra equivalence from a linear equivalence out of a triple tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_equiv_of_linear_equiv_triple_tensor_product (f : ((A βŠ—[R] B) βŠ—[R] C) ≃ₗ[R] D) (w₁ : βˆ€ (a₁ aβ‚‚ : A) (b₁ bβ‚‚ : B) (c₁ cβ‚‚ : C), f ((a₁ * aβ‚‚) βŠ—β‚œ (b₁ * bβ‚‚) βŠ—β‚œ (c₁ * cβ‚‚)) = f (a₁ βŠ—β‚œ b₁ βŠ—β‚œ c₁) * f (aβ‚‚ βŠ—β‚œ bβ‚‚ βŠ—β‚œ cβ‚‚)) (wβ‚‚ : βˆ€ r, f (((algebra_map R A) r βŠ—β‚œ[R] (1 : B)) βŠ—β‚œ[R] (1 : C)) = (algebra_map R D) r) : (A βŠ—[R] B) βŠ—[R] C ≃ₐ[R] D := { to_fun := f, map_mul' := Ξ» x y, begin apply tensor_product.induction_on x, { simp, }, { intros ab₁ c₁, apply tensor_product.induction_on y, { simp, }, { intros abβ‚‚ cβ‚‚, apply tensor_product.induction_on ab₁, { simp, }, { intros a₁ b₁, apply tensor_product.induction_on abβ‚‚, { simp, }, { simp [w₁], }, { intros x₁ xβ‚‚ h₁ hβ‚‚, simp at h₁ hβ‚‚, simp [mul_add, add_tmul, h₁, hβ‚‚], }, }, { intros x₁ xβ‚‚ h₁ hβ‚‚, simp at h₁ hβ‚‚, simp [add_mul, add_tmul, h₁, hβ‚‚], }, }, { intros x₁ xβ‚‚ h₁ hβ‚‚, simp [mul_add, add_mul, h₁, hβ‚‚], }, }, { intros x₁ xβ‚‚ h₁ hβ‚‚, simp [mul_add, add_mul, h₁, hβ‚‚], } end, commutes' := Ξ» r, by simp [wβ‚‚], .. f } @[simp] lemma alg_equiv_of_linear_equiv_triple_tensor_product_apply (f w₁ wβ‚‚ x) : (alg_equiv_of_linear_equiv_triple_tensor_product f w₁ wβ‚‚ : (A βŠ—[R] B) βŠ—[R] C ≃ₐ[R] D) x = f x := rfl end variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type vβ‚‚} [semiring B] [algebra R B] variables {C : Type v₃} [semiring C] [algebra R C] variables {D : Type vβ‚„} [semiring D] [algebra R D] section variables (R A) /-- The base ring is a left identity for the tensor product of algebra, up to algebra isomorphism. -/ protected def lid : R βŠ—[R] A ≃ₐ[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.lid R A) (by simp [mul_smul]) (by simp [algebra.smul_def]) @[simp] theorem lid_tmul (r : R) (a : A) : (tensor_product.lid R A : (R βŠ— A β†’ A)) (r βŠ—β‚œ a) = r β€’ a := by simp [tensor_product.lid] /-- The base ring is a right identity for the tensor product of algebra, up to algebra isomorphism. -/ protected def rid : A βŠ—[R] R ≃ₐ[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.rid R A) (by simp [mul_smul]) (by simp [algebra.smul_def]) @[simp] theorem rid_tmul (r : R) (a : A) : (tensor_product.rid R A : (A βŠ— R β†’ A)) (a βŠ—β‚œ r) = r β€’ a := by simp [tensor_product.rid] section variables (R A B) /-- The tensor product of R-algebras is commutative, up to algebra isomorphism. -/ protected def comm : A βŠ—[R] B ≃ₐ[R] B βŠ—[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.comm R A B) (by simp) (Ξ» r, begin transitivity r β€’ ((1 : B) βŠ—β‚œ[R] (1 : A)), { rw [←tmul_smul, algebra.smul_def], simp, }, { simp [algebra.smul_def], }, end) @[simp] theorem comm_tmul (a : A) (b : B) : (tensor_product.comm R A B : (A βŠ—[R] B β†’ B βŠ—[R] A)) (a βŠ—β‚œ b) = (b βŠ—β‚œ a) := by simp [tensor_product.comm] end section variables {R A B C} lemma assoc_aux_1 (a₁ aβ‚‚ : A) (b₁ bβ‚‚ : B) (c₁ cβ‚‚ : C) : (tensor_product.assoc R A B C) (((a₁ * aβ‚‚) βŠ—β‚œ[R] (b₁ * bβ‚‚)) βŠ—β‚œ[R] (c₁ * cβ‚‚)) = (tensor_product.assoc R A B C) ((a₁ βŠ—β‚œ[R] b₁) βŠ—β‚œ[R] c₁) * (tensor_product.assoc R A B C) ((aβ‚‚ βŠ—β‚œ[R] bβ‚‚) βŠ—β‚œ[R] cβ‚‚) := rfl lemma assoc_aux_2 (r : R) : (tensor_product.assoc R A B C) (((algebra_map R A) r βŠ—β‚œ[R] 1) βŠ—β‚œ[R] 1) = (algebra_map R (A βŠ— (B βŠ— C))) r := rfl -- variables (R A B C) -- -- local attribute [elab_simple] alg_equiv_of_linear_equiv_triple_tensor_product -- /-- The associator for tensor product of R-algebras, as an algebra isomorphism. -/ -- -- FIXME This is _really_ slow to compile. :-( -- protected def assoc : ((A βŠ—[R] B) βŠ—[R] C) ≃ₐ[R] (A βŠ—[R] (B βŠ—[R] C)) := -- alg_equiv_of_linear_equiv_triple_tensor_product -- (tensor_product.assoc R A B C) -- assoc_aux_1 assoc_aux_2 -- variables {R A B C} -- @[simp] theorem assoc_tmul (a : A) (b : B) (c : C) : -- ((tensor_product.assoc R A B C) : -- (A βŠ—[R] B) βŠ—[R] C β†’ A βŠ—[R] (B βŠ—[R] C)) ((a βŠ—β‚œ b) βŠ—β‚œ c) = a βŠ—β‚œ (b βŠ—β‚œ c) := -- rfl end variables {R A B C D} /-- The tensor product of a pair of algebra morphisms. -/ def map (f : A →ₐ[R] B) (g : C →ₐ[R] D) : A βŠ—[R] C →ₐ[R] B βŠ—[R] D := alg_hom_of_linear_map_tensor_product (tensor_product.map f.to_linear_map g.to_linear_map) (by simp) (by simp [alg_hom.commutes]) @[simp] theorem map_tmul (f : A →ₐ[R] B) (g : C →ₐ[R] D) (a : A) (c : C) : map f g (a βŠ—β‚œ c) = f a βŠ—β‚œ g c := rfl /-- Construct an isomorphism between tensor products of R-algebras from isomorphisms between the tensor factors. -/ def congr (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) : A βŠ—[R] C ≃ₐ[R] B βŠ—[R] D := alg_equiv.of_alg_hom (map f g) (map f.symm g.symm) (ext $ Ξ» b d, by simp) (ext $ Ξ» a c, by simp) @[simp] lemma congr_apply (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) (x) : congr f g x = (map (f : A →ₐ[R] B) (g : C →ₐ[R] D)) x := rfl @[simp] lemma congr_symm_apply (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) (x) : (congr f g).symm x = (map (f.symm : B →ₐ[R] A) (g.symm : D →ₐ[R] C)) x := rfl end end monoidal end tensor_product end algebra
6a901b8a560dba3db5f828f93de9ce00b8716c49
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/algebra/group.lean
ef95bcf08edb293e6949aa7a72bc8d25a80cd088
[ "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
61,802
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 group_theory.group_action.conj_act import group_theory.group_action.quotient import order.filter.pointwise import topology.algebra.monoid import topology.compact_open import topology.sets.compacts import topology.algebra.constructions /-! # Topological groups This file defines the following typeclasses: * `topological_group`, `topological_add_group`: multiplicative and additive topological groups, i.e., groups with continuous `(*)` and `(⁻¹)` / `(+)` and `(-)`; * `has_continuous_sub G` means that `G` has a continuous subtraction operation. There is an instance deducing `has_continuous_sub` from `topological_group` but we use a separate typeclass because, e.g., `β„•` and `ℝβ‰₯0` have continuous subtraction but are not additive groups. We also define `homeomorph` versions of several `equiv`s: `homeomorph.mul_left`, `homeomorph.mul_right`, `homeomorph.inv`, and prove a few facts about neighbourhood filters in groups. ## Tags topological space, group, topological group -/ open classical set filter topological_space function open_locale classical topological_space filter pointwise universes u v w x variables {Ξ± : Type u} {Ξ² : Type v} {G : Type w} {H : Type x} section continuous_mul_group /-! ### Groups with continuous multiplication In this section we prove a few statements about groups with continuous `(*)`. -/ variables [topological_space G] [group G] [has_continuous_mul G] /-- Multiplication from the left in a topological group as a homeomorphism. -/ @[to_additive "Addition from the left in a topological additive group as a homeomorphism."] protected def homeomorph.mul_left (a : G) : G β‰ƒβ‚œ G := { continuous_to_fun := continuous_const.mul continuous_id, continuous_inv_fun := continuous_const.mul continuous_id, .. equiv.mul_left a } @[simp, to_additive] lemma homeomorph.coe_mul_left (a : G) : ⇑(homeomorph.mul_left a) = (*) a := rfl @[to_additive] lemma homeomorph.mul_left_symm (a : G) : (homeomorph.mul_left a).symm = homeomorph.mul_left a⁻¹ := by { ext, refl } @[to_additive] lemma is_open_map_mul_left (a : G) : is_open_map (Ξ» x, a * x) := (homeomorph.mul_left a).is_open_map @[to_additive is_open.left_add_coset] lemma is_open.left_coset {U : set G} (h : is_open U) (x : G) : is_open (left_coset x U) := is_open_map_mul_left x _ h @[to_additive] lemma is_closed_map_mul_left (a : G) : is_closed_map (Ξ» x, a * x) := (homeomorph.mul_left a).is_closed_map @[to_additive is_closed.left_add_coset] lemma is_closed.left_coset {U : set G} (h : is_closed U) (x : G) : is_closed (left_coset x U) := is_closed_map_mul_left x _ h /-- Multiplication from the right in a topological group as a homeomorphism. -/ @[to_additive "Addition from the right in a topological additive group as a homeomorphism."] protected def homeomorph.mul_right (a : G) : G β‰ƒβ‚œ G := { continuous_to_fun := continuous_id.mul continuous_const, continuous_inv_fun := continuous_id.mul continuous_const, .. equiv.mul_right a } @[simp, to_additive] lemma homeomorph.coe_mul_right (a : G) : ⇑(homeomorph.mul_right a) = Ξ» g, g * a := rfl @[to_additive] lemma homeomorph.mul_right_symm (a : G) : (homeomorph.mul_right a).symm = homeomorph.mul_right a⁻¹ := by { ext, refl } @[to_additive] lemma is_open_map_mul_right (a : G) : is_open_map (Ξ» x, x * a) := (homeomorph.mul_right a).is_open_map @[to_additive is_open.right_add_coset] lemma is_open.right_coset {U : set G} (h : is_open U) (x : G) : is_open (right_coset U x) := is_open_map_mul_right x _ h @[to_additive] lemma is_closed_map_mul_right (a : G) : is_closed_map (Ξ» x, x * a) := (homeomorph.mul_right a).is_closed_map @[to_additive is_closed.right_add_coset] lemma is_closed.right_coset {U : set G} (h : is_closed U) (x : G) : is_closed (right_coset U x) := is_closed_map_mul_right x _ h @[to_additive] lemma discrete_topology_of_open_singleton_one (h : is_open ({1} : set G)) : discrete_topology G := begin rw ← singletons_open_iff_discrete, intro g, suffices : {g} = (Ξ» (x : G), g⁻¹ * x) ⁻¹' {1}, { rw this, exact (continuous_mul_left (g⁻¹)).is_open_preimage _ h, }, simp only [mul_one, set.preimage_mul_left_singleton, eq_self_iff_true, inv_inv, set.singleton_eq_singleton_iff], end @[to_additive] lemma discrete_topology_iff_open_singleton_one : discrete_topology G ↔ is_open ({1} : set G) := ⟨λ h, forall_open_iff_discrete.mpr h {1}, discrete_topology_of_open_singleton_one⟩ end continuous_mul_group /-! ### `has_continuous_inv` and `has_continuous_neg` -/ /-- Basic hypothesis to talk about a topological additive group. A topological additive group over `M`, for example, is obtained by requiring the instances `add_group M` and `has_continuous_add M` and `has_continuous_neg M`. -/ class has_continuous_neg (G : Type u) [topological_space G] [has_neg G] : Prop := (continuous_neg : continuous (Ξ» a : G, -a)) /-- Basic hypothesis to talk about a topological group. A topological group over `M`, for example, is obtained by requiring the instances `group M` and `has_continuous_mul M` and `has_continuous_inv M`. -/ @[to_additive] class has_continuous_inv (G : Type u) [topological_space G] [has_inv G] : Prop := (continuous_inv : continuous (Ξ» a : G, a⁻¹)) export has_continuous_inv (continuous_inv) export has_continuous_neg (continuous_neg) section continuous_inv variables [topological_space G] [has_inv G] [has_continuous_inv G] @[to_additive] lemma continuous_on_inv {s : set G} : continuous_on has_inv.inv s := continuous_inv.continuous_on @[to_additive] lemma continuous_within_at_inv {s : set G} {x : G} : continuous_within_at has_inv.inv s x := continuous_inv.continuous_within_at @[to_additive] lemma continuous_at_inv {x : G} : continuous_at has_inv.inv x := continuous_inv.continuous_at @[to_additive] lemma tendsto_inv (a : G) : tendsto has_inv.inv (𝓝 a) (𝓝 (a⁻¹)) := continuous_at_inv /-- If a function converges to a value in a multiplicative topological group, then its inverse converges to the inverse of this value. For the version in normed fields assuming additionally that the limit is nonzero, use `tendsto.inv'`. -/ @[to_additive "If a function converges to a value in an additive topological group, then its negation converges to the negation of this value."] lemma filter.tendsto.inv {f : Ξ± β†’ G} {l : filter Ξ±} {y : G} (h : tendsto f l (𝓝 y)) : tendsto (Ξ» x, (f x)⁻¹) l (𝓝 y⁻¹) := (continuous_inv.tendsto y).comp h variables [topological_space Ξ±] {f : Ξ± β†’ G} {s : set Ξ±} {x : Ξ±} @[continuity, to_additive] lemma continuous.inv (hf : continuous f) : continuous (Ξ»x, (f x)⁻¹) := continuous_inv.comp hf @[to_additive] lemma continuous_at.inv (hf : continuous_at f x) : continuous_at (Ξ» x, (f x)⁻¹) x := continuous_at_inv.comp hf @[to_additive] lemma continuous_on.inv (hf : continuous_on f s) : continuous_on (Ξ»x, (f x)⁻¹) s := continuous_inv.comp_continuous_on hf @[to_additive] lemma continuous_within_at.inv (hf : continuous_within_at f s x) : continuous_within_at (Ξ» x, (f x)⁻¹) s x := hf.inv @[to_additive] instance [topological_space H] [has_inv H] [has_continuous_inv H] : has_continuous_inv (G Γ— H) := ⟨continuous_inv.fst'.prod_mk continuous_inv.snd'⟩ variable {ΞΉ : Type*} @[to_additive] instance pi.has_continuous_inv {C : ΞΉ β†’ Type*} [βˆ€ i, topological_space (C i)] [βˆ€ i, has_inv (C i)] [βˆ€ i, has_continuous_inv (C i)] : has_continuous_inv (Ξ  i, C i) := { continuous_inv := continuous_pi (Ξ» i, (continuous_apply i).inv) } /-- A version of `pi.has_continuous_inv` for non-dependent functions. It is needed because sometimes Lean fails to use `pi.has_continuous_inv` for non-dependent functions. -/ @[to_additive "A version of `pi.has_continuous_neg` for non-dependent functions. It is needed because sometimes Lean fails to use `pi.has_continuous_neg` for non-dependent functions."] instance pi.has_continuous_inv' : has_continuous_inv (ΞΉ β†’ G) := pi.has_continuous_inv @[priority 100, to_additive] instance has_continuous_inv_of_discrete_topology [topological_space H] [has_inv H] [discrete_topology H] : has_continuous_inv H := ⟨continuous_of_discrete_topology⟩ section pointwise_limits variables (G₁ Gβ‚‚ : Type*) [topological_space Gβ‚‚] [t2_space Gβ‚‚] @[to_additive] lemma is_closed_set_of_map_inv [has_inv G₁] [has_inv Gβ‚‚] [has_continuous_inv Gβ‚‚] : is_closed {f : G₁ β†’ Gβ‚‚ | βˆ€ x, f x⁻¹ = (f x)⁻¹ } := begin simp only [set_of_forall], refine is_closed_Inter (Ξ» i, is_closed_eq (continuous_apply _) (continuous_apply _).inv), end end pointwise_limits instance [topological_space H] [has_inv H] [has_continuous_inv H] : has_continuous_neg (additive H) := { continuous_neg := @continuous_inv H _ _ _ } instance [topological_space H] [has_neg H] [has_continuous_neg H] : has_continuous_inv (multiplicative H) := { continuous_inv := @continuous_neg H _ _ _ } end continuous_inv section continuous_involutive_inv variables [topological_space G] [has_involutive_inv G] [has_continuous_inv G] {s : set G} @[to_additive] lemma is_compact.inv (hs : is_compact s) : is_compact s⁻¹ := by { rw [← image_inv], exact hs.image continuous_inv } variables (G) /-- Inversion in a topological group as a homeomorphism. -/ @[to_additive "Negation in a topological group as a homeomorphism."] protected def homeomorph.inv (G : Type*) [topological_space G] [has_involutive_inv G] [has_continuous_inv G] : G β‰ƒβ‚œ G := { continuous_to_fun := continuous_inv, continuous_inv_fun := continuous_inv, .. equiv.inv G } @[to_additive] lemma is_open_map_inv : is_open_map (has_inv.inv : G β†’ G) := (homeomorph.inv _).is_open_map @[to_additive] lemma is_closed_map_inv : is_closed_map (has_inv.inv : G β†’ G) := (homeomorph.inv _).is_closed_map variables {G} @[to_additive] lemma is_open.inv (hs : is_open s) : is_open s⁻¹ := hs.preimage continuous_inv @[to_additive] lemma is_closed.inv (hs : is_closed s) : is_closed s⁻¹ := hs.preimage continuous_inv @[to_additive] lemma inv_closure : βˆ€ s : set G, (closure s)⁻¹ = closure s⁻¹ := (homeomorph.inv G).preimage_closure end continuous_involutive_inv section lattice_ops variables {ΞΉ' : Sort*} [has_inv G] @[to_additive] lemma has_continuous_inv_Inf {ts : set (topological_space G)} (h : Ξ  t ∈ ts, @has_continuous_inv G t _) : @has_continuous_inv G (Inf ts) _ := { continuous_inv := continuous_Inf_rng.2 (Ξ» t ht, continuous_Inf_dom ht (@has_continuous_inv.continuous_inv G t _ (h t ht))) } @[to_additive] lemma has_continuous_inv_infi {ts' : ΞΉ' β†’ topological_space G} (h' : Ξ  i, @has_continuous_inv G (ts' i) _) : @has_continuous_inv G (β¨… i, ts' i) _ := by {rw ← Inf_range, exact has_continuous_inv_Inf (set.forall_range_iff.mpr h')} @[to_additive] lemma has_continuous_inv_inf {t₁ tβ‚‚ : topological_space G} (h₁ : @has_continuous_inv G t₁ _) (hβ‚‚ : @has_continuous_inv G tβ‚‚ _) : @has_continuous_inv G (t₁ βŠ“ tβ‚‚) _ := by { rw inf_eq_infi, refine has_continuous_inv_infi (Ξ» b, _), cases b; assumption } end lattice_ops @[to_additive] lemma inducing.has_continuous_inv {G H : Type*} [has_inv G] [has_inv H] [topological_space G] [topological_space H] [has_continuous_inv H] {f : G β†’ H} (hf : inducing f) (hf_inv : βˆ€ x, f x⁻¹ = (f x)⁻¹) : has_continuous_inv G := ⟨hf.continuous_iff.2 $ by simpa only [(∘), hf_inv] using hf.continuous.inv⟩ section topological_group /-! ### Topological groups A topological group is a group in which the multiplication and inversion operations are continuous. Topological additive groups are defined in the same way. Equivalently, we can require that the division operation `Ξ» x y, x * y⁻¹` (resp., subtraction) is continuous. -/ /-- A topological (additive) group is a group in which the addition and negation operations are continuous. -/ class topological_add_group (G : Type u) [topological_space G] [add_group G] extends has_continuous_add G, has_continuous_neg G : Prop /-- A topological group is a group in which the multiplication and inversion operations are continuous. When you declare an instance that does not already have a `uniform_space` instance, you should also provide an instance of `uniform_space` and `uniform_group` using `topological_group.to_uniform_space` and `topological_comm_group_is_uniform`. -/ @[to_additive] class topological_group (G : Type*) [topological_space G] [group G] extends has_continuous_mul G, has_continuous_inv G : Prop section conj instance conj_act.units_has_continuous_const_smul {M} [monoid M] [topological_space M] [has_continuous_mul M] : has_continuous_const_smul (conj_act MΛ£) M := ⟨λ m, (continuous_const.mul continuous_id).mul continuous_const⟩ /-- we slightly weaken the type class assumptions here so that it will also apply to `ennreal`, but we nevertheless leave it in the `topological_group` namespace. -/ variables [topological_space G] [has_inv G] [has_mul G] [has_continuous_mul G] /-- Conjugation is jointly continuous on `G Γ— G` when both `mul` and `inv` are continuous. -/ @[to_additive "Conjugation is jointly continuous on `G Γ— G` when both `mul` and `inv` are continuous."] lemma topological_group.continuous_conj_prod [has_continuous_inv G] : continuous (Ξ» g : G Γ— G, g.fst * g.snd * g.fst⁻¹) := continuous_mul.mul (continuous_inv.comp continuous_fst) /-- Conjugation by a fixed element is continuous when `mul` is continuous. -/ @[to_additive "Conjugation by a fixed element is continuous when `add` is continuous."] lemma topological_group.continuous_conj (g : G) : continuous (Ξ» (h : G), g * h * g⁻¹) := (continuous_mul_right g⁻¹).comp (continuous_mul_left g) /-- Conjugation acting on fixed element of the group is continuous when both `mul` and `inv` are continuous. -/ @[to_additive "Conjugation acting on fixed element of the additive group is continuous when both `add` and `neg` are continuous."] lemma topological_group.continuous_conj' [has_continuous_inv G] (h : G) : continuous (Ξ» (g : G), g * h * g⁻¹) := (continuous_mul_right h).mul continuous_inv end conj variables [topological_space G] [group G] [topological_group G] [topological_space Ξ±] {f : Ξ± β†’ G} {s : set Ξ±} {x : Ξ±} section zpow @[continuity, to_additive] lemma continuous_zpow : βˆ€ z : β„€, continuous (Ξ» a : G, a ^ z) | (int.of_nat n) := by simpa using continuous_pow n | -[1+n] := by simpa using (continuous_pow (n + 1)).inv instance add_group.has_continuous_const_smul_int {A} [add_group A] [topological_space A] [topological_add_group A] : has_continuous_const_smul β„€ A := ⟨continuous_zsmul⟩ instance add_group.has_continuous_smul_int {A} [add_group A] [topological_space A] [topological_add_group A] : has_continuous_smul β„€ A := ⟨continuous_uncurry_of_discrete_topology continuous_zsmul⟩ @[continuity, to_additive] lemma continuous.zpow {f : Ξ± β†’ G} (h : continuous f) (z : β„€) : continuous (Ξ» b, (f b) ^ z) := (continuous_zpow z).comp h @[to_additive] lemma continuous_on_zpow {s : set G} (z : β„€) : continuous_on (Ξ» x, x ^ z) s := (continuous_zpow z).continuous_on @[to_additive] lemma continuous_at_zpow (x : G) (z : β„€) : continuous_at (Ξ» x, x ^ z) x := (continuous_zpow z).continuous_at @[to_additive] lemma filter.tendsto.zpow {Ξ±} {l : filter Ξ±} {f : Ξ± β†’ G} {x : G} (hf : tendsto f l (𝓝 x)) (z : β„€) : tendsto (Ξ» x, f x ^ z) l (𝓝 (x ^ z)) := (continuous_at_zpow _ _).tendsto.comp hf @[to_additive] lemma continuous_within_at.zpow {f : Ξ± β†’ G} {x : Ξ±} {s : set Ξ±} (hf : continuous_within_at f s x) (z : β„€) : continuous_within_at (Ξ» x, f x ^ z) s x := hf.zpow z @[to_additive] lemma continuous_at.zpow {f : Ξ± β†’ G} {x : Ξ±} (hf : continuous_at f x) (z : β„€) : continuous_at (Ξ» x, f x ^ z) x := hf.zpow z @[to_additive continuous_on.zsmul] lemma continuous_on.zpow {f : Ξ± β†’ G} {s : set Ξ±} (hf : continuous_on f s) (z : β„€) : continuous_on (Ξ» x, f x ^ z) s := Ξ» x hx, (hf x hx).zpow z end zpow section ordered_comm_group variables [topological_space H] [ordered_comm_group H] [topological_group H] @[to_additive] lemma tendsto_inv_nhds_within_Ioi {a : H} : tendsto has_inv.inv (𝓝[>] a) (𝓝[<] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Iio {a : H} : tendsto has_inv.inv (𝓝[<] a) (𝓝[>] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Ioi_inv {a : H} : tendsto has_inv.inv (𝓝[>] (a⁻¹)) (𝓝[<] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ioi _ _ _ _ (a⁻¹) @[to_additive] lemma tendsto_inv_nhds_within_Iio_inv {a : H} : tendsto has_inv.inv (𝓝[<] (a⁻¹)) (𝓝[>] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iio _ _ _ _ (a⁻¹) @[to_additive] lemma tendsto_inv_nhds_within_Ici {a : H} : tendsto has_inv.inv (𝓝[β‰₯] a) (𝓝[≀] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Iic {a : H} : tendsto has_inv.inv (𝓝[≀] a) (𝓝[β‰₯] (a⁻¹)) := (continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal] @[to_additive] lemma tendsto_inv_nhds_within_Ici_inv {a : H} : tendsto has_inv.inv (𝓝[β‰₯] (a⁻¹)) (𝓝[≀] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ici _ _ _ _ (a⁻¹) @[to_additive] lemma tendsto_inv_nhds_within_Iic_inv {a : H} : tendsto has_inv.inv (𝓝[≀] (a⁻¹)) (𝓝[β‰₯] a) := by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iic _ _ _ _ (a⁻¹) end ordered_comm_group @[instance, to_additive] instance [topological_space H] [group H] [topological_group H] : topological_group (G Γ— H) := { continuous_inv := continuous_inv.prod_map continuous_inv } @[to_additive] instance pi.topological_group {C : Ξ² β†’ Type*} [βˆ€ b, topological_space (C b)] [βˆ€ b, group (C b)] [βˆ€ b, topological_group (C b)] : topological_group (Ξ  b, C b) := { continuous_inv := continuous_pi (Ξ» i, (continuous_apply i).inv) } open mul_opposite @[to_additive] instance [group Ξ±] [has_continuous_inv Ξ±] : has_continuous_inv αᡐᡒᡖ := op_homeomorph.symm.inducing.has_continuous_inv unop_inv /-- If multiplication is continuous in `Ξ±`, then it also is in `αᡐᡒᡖ`. -/ @[to_additive "If addition is continuous in `Ξ±`, then it also is in `αᡃᡒᡖ`."] instance [group Ξ±] [topological_group Ξ±] : topological_group αᡐᡒᡖ := { } variable (G) @[to_additive] lemma nhds_one_symm : comap has_inv.inv (𝓝 (1 : G)) = 𝓝 (1 : G) := ((homeomorph.inv G).comap_nhds_eq _).trans (congr_arg nhds inv_one) /-- The map `(x, y) ↦ (x, xy)` as a homeomorphism. This is a shear mapping. -/ @[to_additive "The map `(x, y) ↦ (x, x + y)` as a homeomorphism. This is a shear mapping."] protected def homeomorph.shear_mul_right : G Γ— G β‰ƒβ‚œ G Γ— G := { continuous_to_fun := continuous_fst.prod_mk continuous_mul, continuous_inv_fun := continuous_fst.prod_mk $ continuous_fst.inv.mul continuous_snd, .. equiv.prod_shear (equiv.refl _) equiv.mul_left } @[simp, to_additive] lemma homeomorph.shear_mul_right_coe : ⇑(homeomorph.shear_mul_right G) = Ξ» z : G Γ— G, (z.1, z.1 * z.2) := rfl @[simp, to_additive] lemma homeomorph.shear_mul_right_symm_coe : ⇑(homeomorph.shear_mul_right G).symm = Ξ» z : G Γ— G, (z.1, z.1⁻¹ * z.2) := rfl variables {G} @[to_additive] protected lemma inducing.topological_group {F : Type*} [group H] [topological_space H] [monoid_hom_class F H G] (f : F) (hf : inducing f) : topological_group H := { to_has_continuous_mul := hf.has_continuous_mul _, to_has_continuous_inv := hf.has_continuous_inv (map_inv f) } @[to_additive] protected lemma topological_group_induced {F : Type*} [group H] [monoid_hom_class F H G] (f : F) : @topological_group H (induced f β€Ή_β€Ί) _ := by { letI := induced f β€Ή_β€Ί, exact inducing.topological_group f ⟨rfl⟩ } namespace subgroup @[to_additive] instance (S : subgroup G) : topological_group S := inducing.topological_group S.subtype inducing_coe end subgroup /-- The (topological-space) closure of a subgroup of a space `M` with `has_continuous_mul` is itself a subgroup. -/ @[to_additive "The (topological-space) closure of an additive subgroup of a space `M` with `has_continuous_add` is itself an additive subgroup."] def subgroup.topological_closure (s : subgroup G) : subgroup G := { carrier := closure (s : set G), inv_mem' := Ξ» g m, by simpa [←set.mem_inv, inv_closure] using m, ..s.to_submonoid.topological_closure } @[simp, to_additive] lemma subgroup.topological_closure_coe {s : subgroup G} : (s.topological_closure : set G) = closure s := rfl @[to_additive] lemma subgroup.subgroup_topological_closure (s : subgroup G) : s ≀ s.topological_closure := subset_closure @[to_additive] lemma subgroup.is_closed_topological_closure (s : subgroup G) : is_closed (s.topological_closure : set G) := by convert is_closed_closure @[to_additive] lemma subgroup.topological_closure_minimal (s : subgroup G) {t : subgroup G} (h : s ≀ t) (ht : is_closed (t : set G)) : s.topological_closure ≀ t := closure_minimal h ht @[to_additive] lemma dense_range.topological_closure_map_subgroup [group H] [topological_space H] [topological_group H] {f : G β†’* H} (hf : continuous f) (hf' : dense_range f) {s : subgroup G} (hs : s.topological_closure = ⊀) : (s.map f).topological_closure = ⊀ := begin rw set_like.ext'_iff at hs ⊒, simp only [subgroup.topological_closure_coe, subgroup.coe_top, ← dense_iff_closure_eq] at hs ⊒, exact hf'.dense_image hf hs end /-- The topological closure of a normal subgroup is normal.-/ @[to_additive "The topological closure of a normal additive subgroup is normal."] lemma subgroup.is_normal_topological_closure {G : Type*} [topological_space G] [group G] [topological_group G] (N : subgroup G) [N.normal] : (subgroup.topological_closure N).normal := { conj_mem := Ξ» n hn g, begin apply map_mem_closure (topological_group.continuous_conj g) hn, exact Ξ» m hm, subgroup.normal.conj_mem infer_instance m hm g end } @[to_additive] lemma mul_mem_connected_component_one {G : Type*} [topological_space G] [mul_one_class G] [has_continuous_mul G] {g h : G} (hg : g ∈ connected_component (1 : G)) (hh : h ∈ connected_component (1 : G)) : g * h ∈ connected_component (1 : G) := begin rw connected_component_eq hg, have hmul: g ∈ connected_component (g*h), { apply continuous.image_connected_component_subset (continuous_mul_left g), rw ← connected_component_eq hh, exact ⟨(1 : G), mem_connected_component, by simp only [mul_one]⟩ }, simpa [← connected_component_eq hmul] using (mem_connected_component) end @[to_additive] lemma inv_mem_connected_component_one {G : Type*} [topological_space G] [group G] [topological_group G] {g : G} (hg : g ∈ connected_component (1 : G)) : g⁻¹ ∈ connected_component (1 : G) := begin rw ← inv_one, exact continuous.image_connected_component_subset continuous_inv _ ((set.mem_image _ _ _).mp ⟨g, hg, rfl⟩) end /-- The connected component of 1 is a subgroup of `G`. -/ @[to_additive "The connected component of 0 is a subgroup of `G`."] def subgroup.connected_component_of_one (G : Type*) [topological_space G] [group G] [topological_group G] : subgroup G := { carrier := connected_component (1 : G), one_mem' := mem_connected_component, mul_mem' := Ξ» g h hg hh, mul_mem_connected_component_one hg hh, inv_mem' := Ξ» g hg, inv_mem_connected_component_one hg } /-- If a subgroup of a topological group is commutative, then so is its topological closure. -/ @[to_additive "If a subgroup of an additive topological group is commutative, then so is its topological closure."] def subgroup.comm_group_topological_closure [t2_space G] (s : subgroup G) (hs : βˆ€ (x y : s), x * y = y * x) : comm_group s.topological_closure := { ..s.topological_closure.to_group, ..s.to_submonoid.comm_monoid_topological_closure hs } @[to_additive exists_nhds_half_neg] lemma exists_nhds_split_inv {s : set G} (hs : s ∈ 𝓝 (1 : G)) : βˆƒ V ∈ 𝓝 (1 : G), βˆ€ (v ∈ V) (w ∈ V), v / w ∈ s := have ((Ξ»p : G Γ— G, p.1 * p.2⁻¹) ⁻¹' s) ∈ 𝓝 ((1, 1) : G Γ— G), from continuous_at_fst.mul continuous_at_snd.inv (by simpa), by simpa only [div_eq_mul_inv, nhds_prod_eq, mem_prod_self_iff, prod_subset_iff, mem_preimage] using this @[to_additive] lemma nhds_translation_mul_inv (x : G) : comap (Ξ» y : G, y * x⁻¹) (𝓝 1) = 𝓝 x := ((homeomorph.mul_right x⁻¹).comap_nhds_eq 1).trans $ show 𝓝 (1 * x⁻¹⁻¹) = 𝓝 x, by simp @[simp, to_additive] lemma map_mul_left_nhds (x y : G) : map ((*) x) (𝓝 y) = 𝓝 (x * y) := (homeomorph.mul_left x).map_nhds_eq y @[to_additive] lemma map_mul_left_nhds_one (x : G) : map ((*) x) (𝓝 1) = 𝓝 x := by simp /-- A monoid homomorphism (a bundled morphism of a type that implements `monoid_hom_class`) from a topological group to a topological monoid is continuous provided that it is continuous at one. See also `uniform_continuous_of_continuous_at_one`. -/ @[to_additive "An additive monoid homomorphism (a bundled morphism of a type that implements `add_monoid_hom_class`) from an additive topological group to an additive topological monoid is continuous provided that it is continuous at zero. See also `uniform_continuous_of_continuous_at_zero`."] lemma continuous_of_continuous_at_one {M hom : Type*} [mul_one_class M] [topological_space M] [has_continuous_mul M] [monoid_hom_class hom G M] (f : hom) (hf : continuous_at f 1) : continuous f := continuous_iff_continuous_at.2 $ Ξ» x, by simpa only [continuous_at, ← map_mul_left_nhds_one x, tendsto_map'_iff, (∘), map_mul, map_one, mul_one] using hf.tendsto.const_mul (f x) @[to_additive] lemma topological_group.ext {G : Type*} [group G] {t t' : topological_space G} (tg : @topological_group G t _) (tg' : @topological_group G t' _) (h : @nhds G t 1 = @nhds G t' 1) : t = t' := eq_of_nhds_eq_nhds $ Ξ» x, by rw [← @nhds_translation_mul_inv G t _ _ x , ← @nhds_translation_mul_inv G t' _ _ x , ← h] @[to_additive] lemma topological_group.of_nhds_aux {G : Type*} [group G] [topological_space G] (hinv : tendsto (Ξ» (x : G), x⁻¹) (𝓝 1) (𝓝 1)) (hleft : βˆ€ (xβ‚€ : G), 𝓝 xβ‚€ = map (Ξ» (x : G), xβ‚€ * x) (𝓝 1)) (hconj : βˆ€ (xβ‚€ : G), map (Ξ» (x : G), xβ‚€ * x * x₀⁻¹) (𝓝 1) ≀ 𝓝 1) : continuous (Ξ» x : G, x⁻¹) := begin rw continuous_iff_continuous_at, rintros xβ‚€, have key : (Ξ» x, (xβ‚€*x)⁻¹) = (Ξ» x, x₀⁻¹*x) ∘ (Ξ» x, xβ‚€*x*x₀⁻¹) ∘ (Ξ» x, x⁻¹), by {ext ; simp[mul_assoc] }, calc map (Ξ» x, x⁻¹) (𝓝 xβ‚€) = map (Ξ» x, x⁻¹) (map (Ξ» x, xβ‚€*x) $ 𝓝 1) : by rw hleft ... = map (Ξ» x, (xβ‚€*x)⁻¹) (𝓝 1) : by rw filter.map_map ... = map (((Ξ» x, x₀⁻¹*x) ∘ (Ξ» x, xβ‚€*x*x₀⁻¹)) ∘ (Ξ» x, x⁻¹)) (𝓝 1) : by rw key ... = map ((Ξ» x, x₀⁻¹*x) ∘ (Ξ» x, xβ‚€*x*x₀⁻¹)) _ : by rw ← filter.map_map ... ≀ map ((Ξ» x, x₀⁻¹ * x) ∘ Ξ» x, xβ‚€ * x * x₀⁻¹) (𝓝 1) : map_mono hinv ... = map (Ξ» x, x₀⁻¹ * x) (map (Ξ» x, xβ‚€ * x * x₀⁻¹) (𝓝 1)) : filter.map_map ... ≀ map (Ξ» x, x₀⁻¹ * x) (𝓝 1) : map_mono (hconj xβ‚€) ... = 𝓝 x₀⁻¹ : (hleft _).symm end @[to_additive] lemma topological_group.of_nhds_one' {G : Type u} [group G] [topological_space G] (hmul : tendsto (uncurry ((*) : G β†’ G β†’ G)) ((𝓝 1) Γ—αΆ  𝓝 1) (𝓝 1)) (hinv : tendsto (Ξ» x : G, x⁻¹) (𝓝 1) (𝓝 1)) (hleft : βˆ€ xβ‚€ : G, 𝓝 xβ‚€ = map (Ξ» x, xβ‚€*x) (𝓝 1)) (hright : βˆ€ xβ‚€ : G, 𝓝 xβ‚€ = map (Ξ» x, x*xβ‚€) (𝓝 1)) : topological_group G := begin refine { continuous_mul := (has_continuous_mul.of_nhds_one hmul hleft hright).continuous_mul, continuous_inv := topological_group.of_nhds_aux hinv hleft _ }, intros xβ‚€, suffices : map (Ξ» (x : G), xβ‚€ * x * x₀⁻¹) (𝓝 1) = 𝓝 1, by simp [this, le_refl], rw [show (Ξ» x, xβ‚€ * x * x₀⁻¹) = (Ξ» x, xβ‚€ * x) ∘ Ξ» x, x*x₀⁻¹, by {ext, simp [mul_assoc] }, ← filter.map_map, ← hright, hleft x₀⁻¹, filter.map_map], convert map_id, ext, simp end @[to_additive] lemma topological_group.of_nhds_one {G : Type u} [group G] [topological_space G] (hmul : tendsto (uncurry ((*) : G β†’ G β†’ G)) ((𝓝 1) Γ—αΆ  𝓝 1) (𝓝 1)) (hinv : tendsto (Ξ» x : G, x⁻¹) (𝓝 1) (𝓝 1)) (hleft : βˆ€ xβ‚€ : G, 𝓝 xβ‚€ = map (Ξ» x, xβ‚€*x) (𝓝 1)) (hconj : βˆ€ xβ‚€ : G, tendsto (Ξ» x, xβ‚€*x*x₀⁻¹) (𝓝 1) (𝓝 1)) : topological_group G := { continuous_mul := begin rw continuous_iff_continuous_at, rintros ⟨xβ‚€, yβ‚€βŸ©, have key : (Ξ» (p : G Γ— G), xβ‚€ * p.1 * (yβ‚€ * p.2)) = ((Ξ» x, xβ‚€*yβ‚€*x) ∘ (uncurry (*)) ∘ (prod.map (Ξ» x, y₀⁻¹*x*yβ‚€) id)), by { ext, simp [uncurry, prod.map, mul_assoc] }, specialize hconj y₀⁻¹, rw inv_inv at hconj, calc map (Ξ» (p : G Γ— G), p.1 * p.2) (𝓝 (xβ‚€, yβ‚€)) = map (Ξ» (p : G Γ— G), p.1 * p.2) ((𝓝 xβ‚€) Γ—αΆ  𝓝 yβ‚€) : by rw nhds_prod_eq ... = map (Ξ» (p : G Γ— G), xβ‚€ * p.1 * (yβ‚€ * p.2)) ((𝓝 1) Γ—αΆ  (𝓝 1)) : by rw [hleft xβ‚€, hleft yβ‚€, prod_map_map_eq, filter.map_map] ... = map (((Ξ» x, xβ‚€*yβ‚€*x) ∘ (uncurry (*))) ∘ (prod.map (Ξ» x, y₀⁻¹*x*yβ‚€) id))((𝓝 1) Γ—αΆ  (𝓝 1)) : by rw key ... = map ((Ξ» x, xβ‚€*yβ‚€*x) ∘ (uncurry (*))) ((map (Ξ» x, y₀⁻¹*x*yβ‚€) $ 𝓝 1) Γ—αΆ  (𝓝 1)) : by rw [← filter.map_map, ← prod_map_map_eq', map_id] ... ≀ map ((Ξ» x, xβ‚€*yβ‚€*x) ∘ (uncurry (*))) ((𝓝 1) Γ—αΆ  (𝓝 1)) : map_mono (filter.prod_mono hconj $ le_rfl) ... = map (Ξ» x, xβ‚€*yβ‚€*x) (map (uncurry (*)) ((𝓝 1) Γ—αΆ  (𝓝 1))) : by rw filter.map_map ... ≀ map (Ξ» x, xβ‚€*yβ‚€*x) (𝓝 1) : map_mono hmul ... = 𝓝 (xβ‚€*yβ‚€) : (hleft _).symm end, continuous_inv := topological_group.of_nhds_aux hinv hleft hconj} @[to_additive] lemma topological_group.of_comm_of_nhds_one {G : Type u} [comm_group G] [topological_space G] (hmul : tendsto (uncurry ((*) : G β†’ G β†’ G)) ((𝓝 1) Γ—αΆ  𝓝 1) (𝓝 1)) (hinv : tendsto (Ξ» x : G, x⁻¹) (𝓝 1) (𝓝 1)) (hleft : βˆ€ xβ‚€ : G, 𝓝 xβ‚€ = map (Ξ» x, xβ‚€*x) (𝓝 1)) : topological_group G := topological_group.of_nhds_one hmul hinv hleft (by simpa using tendsto_id) end topological_group section quotient_topological_group variables [topological_space G] [group G] [topological_group G] (N : subgroup G) (n : N.normal) @[to_additive] instance quotient_group.quotient.topological_space {G : Type*} [group G] [topological_space G] (N : subgroup G) : topological_space (G β§Έ N) := quotient.topological_space open quotient_group @[to_additive] lemma quotient_group.is_open_map_coe : is_open_map (coe : G β†’ G β§Έ N) := begin intros s s_op, change is_open ((coe : G β†’ G β§Έ N) ⁻¹' (coe '' s)), rw quotient_group.preimage_image_coe N s, exact is_open_Union (Ξ» n, (continuous_mul_right _).is_open_preimage s s_op) end @[to_additive] instance topological_group_quotient [N.normal] : topological_group (G β§Έ N) := { continuous_mul := begin have cont : continuous ((coe : G β†’ G β§Έ N) ∘ (Ξ» (p : G Γ— G), p.fst * p.snd)) := continuous_quot_mk.comp continuous_mul, have quot : quotient_map (Ξ» p : G Γ— G, ((p.1 : G β§Έ N), (p.2 : G β§Έ N))), { apply is_open_map.to_quotient_map, { exact (quotient_group.is_open_map_coe N).prod (quotient_group.is_open_map_coe N) }, { exact continuous_quot_mk.prod_map continuous_quot_mk }, { exact (surjective_quot_mk _).prod_map (surjective_quot_mk _) } }, exact (quotient_map.continuous_iff quot).2 cont, end, continuous_inv := by convert (@continuous_inv G _ _ _).quotient_map' _ } /-- Neighborhoods in the quotient are precisely the map of neighborhoods in the prequotient. -/ @[to_additive "Neighborhoods in the quotient are precisely the map of neighborhoods in the prequotient."] lemma quotient_group.nhds_eq (x : G) : 𝓝 (x : G β§Έ N) = map coe (𝓝 x) := le_antisymm ((quotient_group.is_open_map_coe N).nhds_le x) continuous_quot_mk.continuous_at variables (G) [first_countable_topology G] /-- Any first countable topological group has an antitone neighborhood basis `u : β„• β†’ set G` for which `(u (n + 1)) ^ 2 βŠ† u n`. The existence of such a neighborhood basis is a key tool for `quotient_group.complete_space` -/ @[to_additive "Any first countable topological additive group has an antitone neighborhood basis `u : β„• β†’ set G` for which `u (n + 1) + u (n + 1) βŠ† u n`. The existence of such a neighborhood basis is a key tool for `quotient_add_group.complete_space`"] lemma topological_group.exists_antitone_basis_nhds_one : βˆƒ (u : β„• β†’ set G), (𝓝 1).has_antitone_basis u ∧ (βˆ€ n, u (n + 1) * u (n + 1) βŠ† u n) := begin rcases (𝓝 (1 : G)).exists_antitone_basis with ⟨u, hu, u_anti⟩, have := ((hu.prod_nhds hu).tendsto_iff hu).mp (by simpa only [mul_one] using continuous_mul.tendsto ((1, 1) : G Γ— G)), simp only [and_self, mem_prod, and_imp, prod.forall, exists_true_left, prod.exists, forall_true_left] at this, have event_mul : βˆ€ n : β„•, βˆ€αΆ  m in at_top, u m * u m βŠ† u n, { intros n, rcases this n with ⟨j, k, h⟩, refine at_top_basis.eventually_iff.mpr ⟨max j k, true.intro, Ξ» m hm, _⟩, rintro - ⟨a, b, ha, hb, rfl⟩, exact h a b (u_anti ((le_max_left _ _).trans hm) ha) (u_anti ((le_max_right _ _).trans hm) hb)}, obtain βŸ¨Ο†, -, hΟ†, Ο†_anti_basis⟩ := has_antitone_basis.subbasis_with_rel ⟨hu, u_anti⟩ event_mul, exact ⟨u ∘ Ο†, Ο†_anti_basis, Ξ» n, hΟ† n.lt_succ_self⟩, end include n /-- In a first countable topological group `G` with normal subgroup `N`, `1 : G β§Έ N` has a countable neighborhood basis. -/ @[to_additive "In a first countable topological additive group `G` with normal additive subgroup `N`, `0 : G β§Έ N` has a countable neighborhood basis."] instance quotient_group.nhds_one_is_countably_generated : (𝓝 (1 : G β§Έ N)).is_countably_generated := (quotient_group.nhds_eq N 1).symm β–Έ map.is_countably_generated _ _ end quotient_topological_group /-- A typeclass saying that `Ξ» p : G Γ— G, p.1 - p.2` is a continuous function. This property automatically holds for topological additive groups but it also holds, e.g., for `ℝβ‰₯0`. -/ class has_continuous_sub (G : Type*) [topological_space G] [has_sub G] : Prop := (continuous_sub : continuous (Ξ» p : G Γ— G, p.1 - p.2)) /-- A typeclass saying that `Ξ» p : G Γ— G, p.1 / p.2` is a continuous function. This property automatically holds for topological groups. Lemmas using this class have primes. The unprimed version is for `group_with_zero`. -/ @[to_additive] class has_continuous_div (G : Type*) [topological_space G] [has_div G] : Prop := (continuous_div' : continuous (Ξ» p : G Γ— G, p.1 / p.2)) @[priority 100, to_additive] -- see Note [lower instance priority] instance topological_group.to_has_continuous_div [topological_space G] [group G] [topological_group G] : has_continuous_div G := ⟨by { simp only [div_eq_mul_inv], exact continuous_fst.mul continuous_snd.inv }⟩ export has_continuous_sub (continuous_sub) export has_continuous_div (continuous_div') section has_continuous_div variables [topological_space G] [has_div G] [has_continuous_div G] @[to_additive sub] lemma filter.tendsto.div' {f g : Ξ± β†’ G} {l : filter Ξ±} {a b : G} (hf : tendsto f l (𝓝 a)) (hg : tendsto g l (𝓝 b)) : tendsto (Ξ» x, f x / g x) l (𝓝 (a / b)) := (continuous_div'.tendsto (a, b)).comp (hf.prod_mk_nhds hg) @[to_additive const_sub] lemma filter.tendsto.const_div' (b : G) {c : G} {f : Ξ± β†’ G} {l : filter Ξ±} (h : tendsto f l (𝓝 c)) : tendsto (Ξ» k : Ξ±, b / f k) l (𝓝 (b / c)) := tendsto_const_nhds.div' h @[to_additive sub_const] lemma filter.tendsto.div_const' (b : G) {c : G} {f : Ξ± β†’ G} {l : filter Ξ±} (h : tendsto f l (𝓝 c)) : tendsto (Ξ» k : Ξ±, f k / b) l (𝓝 (c / b)) := h.div' tendsto_const_nhds variables [topological_space Ξ±] {f g : Ξ± β†’ G} {s : set Ξ±} {x : Ξ±} @[continuity, to_additive sub] lemma continuous.div' (hf : continuous f) (hg : continuous g) : continuous (Ξ» x, f x / g x) := continuous_div'.comp (hf.prod_mk hg : _) @[to_additive continuous_sub_left] lemma continuous_div_left' (a : G) : continuous (Ξ» b : G, a / b) := continuous_const.div' continuous_id @[to_additive continuous_sub_right] lemma continuous_div_right' (a : G) : continuous (Ξ» b : G, b / a) := continuous_id.div' continuous_const @[to_additive sub] lemma continuous_at.div' {f g : Ξ± β†’ G} {x : Ξ±} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (Ξ»x, f x / g x) x := hf.div' hg @[to_additive sub] lemma continuous_within_at.div' (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (Ξ» x, f x / g x) s x := hf.div' hg @[to_additive sub] lemma continuous_on.div' (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (Ξ»x, f x / g x) s := Ξ» x hx, (hf x hx).div' (hg x hx) end has_continuous_div section div_in_topological_group variables [group G] [topological_space G] [topological_group G] /-- A version of `homeomorph.mul_left a b⁻¹` that is defeq to `a / b`. -/ @[to_additive /-" A version of `homeomorph.add_left a (-b)` that is defeq to `a - b`. "-/, simps {simp_rhs := tt}] def homeomorph.div_left (x : G) : G β‰ƒβ‚œ G := { continuous_to_fun := continuous_const.div' continuous_id, continuous_inv_fun := continuous_inv.mul continuous_const, .. equiv.div_left x } @[to_additive] lemma is_open_map_div_left (a : G) : is_open_map ((/) a) := (homeomorph.div_left _).is_open_map @[to_additive] lemma is_closed_map_div_left (a : G) : is_closed_map ((/) a) := (homeomorph.div_left _).is_closed_map /-- A version of `homeomorph.mul_right a⁻¹ b` that is defeq to `b / a`. -/ @[to_additive /-" A version of `homeomorph.add_right (-a) b` that is defeq to `b - a`. "-/, simps {simp_rhs := tt}] def homeomorph.div_right (x : G) : G β‰ƒβ‚œ G := { continuous_to_fun := continuous_id.div' continuous_const, continuous_inv_fun := continuous_id.mul continuous_const, .. equiv.div_right x } @[to_additive] lemma is_open_map_div_right (a : G) : is_open_map (Ξ» x, x / a) := (homeomorph.div_right a).is_open_map @[to_additive] lemma is_closed_map_div_right (a : G) : is_closed_map (Ξ» x, x / a) := (homeomorph.div_right a).is_closed_map @[to_additive] lemma tendsto_div_nhds_one_iff {Ξ± : Type*} {l : filter Ξ±} {x : G} {u : Ξ± β†’ G} : tendsto (Ξ» n, u n / x) l (𝓝 1) ↔ tendsto u l (𝓝 x) := begin have A : tendsto (Ξ» (n : Ξ±), x) l (𝓝 x) := tendsto_const_nhds, exact ⟨λ h, by simpa using h.mul A, Ξ» h, by simpa using h.div' A⟩ end @[to_additive] lemma nhds_translation_div (x : G) : comap (/ x) (𝓝 1) = 𝓝 x := by simpa only [div_eq_mul_inv] using nhds_translation_mul_inv x end div_in_topological_group /-! ### Topological operations on pointwise sums and products A few results about interior and closure of the pointwise addition/multiplication of sets in groups with continuous addition/multiplication. See also `submonoid.top_closure_mul_self_eq` in `topology.algebra.monoid`. -/ section has_continuous_mul variables [topological_space Ξ±] [group Ξ±] [has_continuous_mul Ξ±] {s t : set Ξ±} @[to_additive] lemma is_open.mul_left (ht : is_open t) : is_open (s * t) := by { rw ←Union_mul_left_image, exact is_open_bUnion (Ξ» a ha, is_open_map_mul_left a t ht) } @[to_additive] lemma is_open.mul_right (hs : is_open s) : is_open (s * t) := by { rw ←Union_mul_right_image, exact is_open_bUnion (Ξ» a ha, is_open_map_mul_right a s hs) } @[to_additive] lemma subset_interior_mul_left : interior s * t βŠ† interior (s * t) := interior_maximal (set.mul_subset_mul_right interior_subset) is_open_interior.mul_right @[to_additive] lemma subset_interior_mul_right : s * interior t βŠ† interior (s * t) := interior_maximal (set.mul_subset_mul_left interior_subset) is_open_interior.mul_left @[to_additive] lemma subset_interior_mul : interior s * interior t βŠ† interior (s * t) := (set.mul_subset_mul_left interior_subset).trans subset_interior_mul_left end has_continuous_mul section topological_group variables [topological_space Ξ±] [group Ξ±] [topological_group Ξ±] {s t : set Ξ±} @[to_additive] lemma is_open.div_left (ht : is_open t) : is_open (s / t) := by { rw ←Union_div_left_image, exact is_open_bUnion (Ξ» a ha, is_open_map_div_left a t ht) } @[to_additive] lemma is_open.div_right (hs : is_open s) : is_open (s / t) := by { rw ←Union_div_right_image, exact is_open_bUnion (Ξ» a ha, is_open_map_div_right a s hs) } @[to_additive] lemma subset_interior_div_left : interior s / t βŠ† interior (s / t) := interior_maximal (div_subset_div_right interior_subset) is_open_interior.div_right @[to_additive] lemma subset_interior_div_right : s / interior t βŠ† interior (s / t) := interior_maximal (div_subset_div_left interior_subset) is_open_interior.div_left @[to_additive] lemma subset_interior_div : interior s / interior t βŠ† interior (s / t) := (div_subset_div_left interior_subset).trans subset_interior_div_left @[to_additive] lemma is_open.mul_closure (hs : is_open s) (t : set Ξ±) : s * closure t = s * t := begin refine (mul_subset_iff.2 $ Ξ» a ha b hb, _).antisymm (mul_subset_mul_left subset_closure), rw mem_closure_iff at hb, have hbU : b ∈ s⁻¹ * {a * b} := ⟨a⁻¹, a * b, set.inv_mem_inv.2 ha, rfl, inv_mul_cancel_left _ _⟩, obtain ⟨_, ⟨c, d, hc, (rfl : d = _), rfl⟩, hcs⟩ := hb _ hs.inv.mul_right hbU, exact ⟨c⁻¹, _, hc, hcs, inv_mul_cancel_left _ _⟩, end @[to_additive] lemma is_open.closure_mul (ht : is_open t) (s : set Ξ±) : closure s * t = s * t := by rw [←inv_inv (closure s * t), mul_inv_rev, inv_closure, ht.inv.mul_closure, mul_inv_rev, inv_inv, inv_inv] @[to_additive] lemma is_open.div_closure (hs : is_open s) (t : set Ξ±) : s / closure t = s / t := by simp_rw [div_eq_mul_inv, inv_closure, hs.mul_closure] @[to_additive] lemma is_open.closure_div (ht : is_open t) (s : set Ξ±) : closure s / t = s / t := by simp_rw [div_eq_mul_inv, ht.inv.closure_mul] end topological_group /-- additive group with a neighbourhood around 0. Only used to construct a topology and uniform space. This is currently only available for commutative groups, but it can be extended to non-commutative groups too. -/ class add_group_with_zero_nhd (G : Type u) extends add_comm_group G := (Z [] : filter G) (zero_Z : pure 0 ≀ Z) (sub_Z : tendsto (Ξ»p:GΓ—G, p.1 - p.2) (Z Γ—αΆ  Z) Z) section filter_mul section variables (G) [topological_space G] [group G] [topological_group G] @[to_additive] lemma topological_group.t1_space (h : @is_closed G _ {1}) : t1_space G := ⟨assume x, by { convert is_closed_map_mul_right x _ h, simp }⟩ @[to_additive] lemma topological_group.t3_space [t1_space G] : t3_space G := ⟨assume s a hs ha, let f := Ξ» p : G Γ— G, p.1 * (p.2)⁻¹ in have hf : continuous f := continuous_fst.mul continuous_snd.inv, -- a ∈ -s implies f (a, 1) ∈ -s, and so (a, 1) ∈ f⁻¹' (-s); -- and so can find t₁ tβ‚‚ open such that a ∈ t₁ Γ— tβ‚‚ βŠ† f⁻¹' (-s) let ⟨t₁, tβ‚‚, ht₁, htβ‚‚, a_mem_t₁, one_mem_tβ‚‚, t_subset⟩ := is_open_prod_iff.1 ((is_open_compl_iff.2 hs).preimage hf) a (1:G) (by simpa [f]) in begin use [s * tβ‚‚, htβ‚‚.mul_left, Ξ» x hx, ⟨x, 1, hx, one_mem_tβ‚‚, mul_one _⟩], rw [nhds_within, inf_principal_eq_bot, mem_nhds_iff], refine ⟨t₁, _, ht₁, a_mem_tβ‚βŸ©, rintros x hx ⟨y, z, hy, hz, yz⟩, have : x * z⁻¹ ∈ sᢜ := (prod_subset_iff.1 t_subset) x hx z hz, have : x * z⁻¹ ∈ s, rw ← yz, simpa, contradiction end⟩ @[to_additive] lemma topological_group.t2_space [t1_space G] : t2_space G := @t3_space.t2_space G _ (topological_group.t3_space G) variables {G} (S : subgroup G) [subgroup.normal S] [is_closed (S : set G)] @[to_additive] instance subgroup.t3_quotient_of_is_closed (S : subgroup G) [subgroup.normal S] [is_closed (S : set G)] : t3_space (G β§Έ S) := begin suffices : t1_space (G β§Έ S), { exact @topological_group.t3_space _ _ _ _ this, }, have hS : is_closed (S : set G) := infer_instance, rw ← quotient_group.ker_mk S at hS, exact topological_group.t1_space (G β§Έ S) ((quotient_map_quotient_mk.is_closed_preimage).mp hS), end end section /-! Some results about an open set containing the product of two sets in a topological group. -/ variables [topological_space G] [group G] [topological_group G] /-- Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `1` such that `K * V βŠ† U`. -/ @[to_additive "Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `0` such that `K + V βŠ† U`."] lemma compact_open_separated_mul_right {K U : set G} (hK : is_compact K) (hU : is_open U) (hKU : K βŠ† U) : βˆƒ V ∈ 𝓝 (1 : G), K * V βŠ† U := begin apply hK.induction_on, { exact ⟨univ, by simp⟩ }, { rintros s t hst ⟨V, hV, hV'⟩, exact ⟨V, hV, (mul_subset_mul_right hst).trans hV'⟩ }, { rintros s t ⟨V, V_in, hV'⟩ ⟨W, W_in, hW'⟩, use [V ∩ W, inter_mem V_in W_in], rw union_mul, exact union_subset ((mul_subset_mul_left (V.inter_subset_left W)).trans hV') ((mul_subset_mul_left (V.inter_subset_right W)).trans hW') }, { intros x hx, have := tendsto_mul (show U ∈ 𝓝 (x * 1), by simpa using hU.mem_nhds (hKU hx)), rw [nhds_prod_eq, mem_map, mem_prod_iff] at this, rcases this with ⟨t, ht, s, hs, h⟩, rw [← image_subset_iff, image_mul_prod] at h, exact ⟨t, mem_nhds_within_of_mem_nhds ht, s, hs, h⟩ } end open mul_opposite /-- Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `1` such that `V * K βŠ† U`. -/ @[to_additive "Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `0` such that `V + K βŠ† U`."] lemma compact_open_separated_mul_left {K U : set G} (hK : is_compact K) (hU : is_open U) (hKU : K βŠ† U) : βˆƒ V ∈ 𝓝 (1 : G), V * K βŠ† U := begin rcases compact_open_separated_mul_right (hK.image continuous_op) (op_homeomorph.is_open_map U hU) (image_subset op hKU) with ⟨V, (hV : V ∈ 𝓝 (op (1 : G))), hV' : op '' K * V βŠ† op '' U⟩, refine ⟨op ⁻¹' V, continuous_op.continuous_at hV, _⟩, rwa [← image_preimage_eq V op_surjective, ← image_op_mul, image_subset_iff, preimage_image_eq _ op_injective] at hV' end /-- A compact set is covered by finitely many left multiplicative translates of a set with non-empty interior. -/ @[to_additive "A compact set is covered by finitely many left additive translates of a set with non-empty interior."] lemma compact_covered_by_mul_left_translates {K V : set G} (hK : is_compact K) (hV : (interior V).nonempty) : βˆƒ t : finset G, K βŠ† ⋃ g ∈ t, (Ξ» h, g * h) ⁻¹' V := begin obtain ⟨t, ht⟩ : βˆƒ t : finset G, K βŠ† ⋃ x ∈ t, interior (((*) x) ⁻¹' V), { refine hK.elim_finite_subcover (Ξ» x, interior $ ((*) x) ⁻¹' V) (Ξ» x, is_open_interior) _, cases hV with gβ‚€ hgβ‚€, refine Ξ» g hg, mem_Union.2 ⟨gβ‚€ * g⁻¹, _⟩, refine preimage_interior_subset_interior_preimage (continuous_const.mul continuous_id) _, rwa [mem_preimage, inv_mul_cancel_right] }, exact ⟨t, subset.trans ht $ Unionβ‚‚_mono $ Ξ» g hg, interior_subset⟩ end /-- Every locally compact separable topological group is Οƒ-compact. Note: this is not true if we drop the topological group hypothesis. -/ @[priority 100, to_additive separable_locally_compact_add_group.sigma_compact_space "Every locally compact separable topological group is Οƒ-compact. Note: this is not true if we drop the topological group hypothesis."] instance separable_locally_compact_group.sigma_compact_space [separable_space G] [locally_compact_space G] : sigma_compact_space G := begin obtain ⟨L, hLc, hL1⟩ := exists_compact_mem_nhds (1 : G), refine ⟨⟨λ n, (Ξ» x, x * dense_seq G n) ⁻¹' L, _, _⟩⟩, { intro n, exact (homeomorph.mul_right _).compact_preimage.mpr hLc }, { refine Union_eq_univ_iff.2 (Ξ» x, _), obtain ⟨_, ⟨n, rfl⟩, hn⟩ : (range (dense_seq G) ∩ (Ξ» y, x * y) ⁻¹' L).nonempty, { rw [← (homeomorph.mul_left x).apply_symm_apply 1] at hL1, exact (dense_range_dense_seq G).inter_nhds_nonempty ((homeomorph.mul_left x).continuous.continuous_at $ hL1) }, exact ⟨n, hn⟩ } end /-- Every separated topological group in which there exists a compact set with nonempty interior is locally compact. -/ @[to_additive] lemma topological_space.positive_compacts.locally_compact_space_of_group [t2_space G] (K : positive_compacts G) : locally_compact_space G := begin refine locally_compact_of_compact_nhds (Ξ» x, _), obtain ⟨y, hy⟩ := K.interior_nonempty, let F := homeomorph.mul_left (x * y⁻¹), refine ⟨F '' K, _, K.compact.image F.continuous⟩, suffices : F.symm ⁻¹' K ∈ 𝓝 x, by { convert this, apply equiv.image_eq_preimage }, apply continuous_at.preimage_mem_nhds F.symm.continuous.continuous_at, have : F.symm x = y, by simp [F, homeomorph.mul_left_symm], rw this, exact mem_interior_iff_mem_nhds.1 hy end end section variables [topological_space G] [comm_group G] [topological_group G] @[to_additive] lemma nhds_mul (x y : G) : 𝓝 (x * y) = 𝓝 x * 𝓝 y := filter_eq $ set.ext $ assume s, begin rw [← nhds_translation_mul_inv x, ← nhds_translation_mul_inv y, ← nhds_translation_mul_inv (x*y)], split, { rintros ⟨t, ht, ts⟩, rcases exists_nhds_one_split ht with ⟨V, V1, h⟩, refine ⟨(Ξ»a, a * x⁻¹) ⁻¹' V, (Ξ»a, a * y⁻¹) ⁻¹' V, ⟨V, V1, subset.refl _⟩, ⟨V, V1, subset.refl _⟩, _⟩, rintros a ⟨v, w, v_mem, w_mem, rfl⟩, apply ts, simpa [mul_comm, mul_assoc, mul_left_comm] using h (v * x⁻¹) v_mem (w * y⁻¹) w_mem }, { rintros ⟨a, c, ⟨b, hb, ba⟩, ⟨d, hd, dc⟩, ac⟩, refine ⟨b ∩ d, inter_mem hb hd, assume v, _⟩, simp only [preimage_subset_iff, mul_inv_rev, mem_preimage] at *, rintros ⟨vb, vd⟩, refine ac ⟨v * y⁻¹, y, _, _, _⟩, { rw ← mul_assoc _ _ _ at vb, exact ba _ vb }, { apply dc y, rw mul_right_inv, exact mem_of_mem_nhds hd }, { simp only [inv_mul_cancel_right] } } end /-- On a topological group, `𝓝 : G β†’ filter G` can be promoted to a `mul_hom`. -/ @[to_additive "On an additive topological group, `𝓝 : G β†’ filter G` can be promoted to an `add_hom`.", simps] def nhds_mul_hom : G β†’β‚™* (filter G) := { to_fun := 𝓝, map_mul' := Ξ»_ _, nhds_mul _ _ } end end filter_mul instance {G} [topological_space G] [group G] [topological_group G] : topological_add_group (additive G) := { continuous_neg := @continuous_inv G _ _ _ } instance {G} [topological_space G] [add_group G] [topological_add_group G] : topological_group (multiplicative G) := { continuous_inv := @continuous_neg G _ _ _ } section quotient variables [group G] [topological_space G] [topological_group G] {Ξ“ : subgroup G} @[to_additive] instance quotient_group.has_continuous_const_smul : has_continuous_const_smul G (G β§Έ Ξ“) := { continuous_const_smul := Ξ» g, by convert ((@continuous_const _ _ _ _ g).mul continuous_id).quotient_map' _ } @[to_additive] lemma quotient_group.continuous_smul₁ (x : G β§Έ Ξ“) : continuous (Ξ» g : G, g β€’ x) := begin induction x using quotient_group.induction_on, exact continuous_quotient_mk.comp (continuous_mul_right x) end @[to_additive] instance quotient_group.has_continuous_smul [locally_compact_space G] : has_continuous_smul G (G β§Έ Ξ“) := { continuous_smul := begin let F : G Γ— G β§Έ Ξ“ β†’ G β§Έ Ξ“ := Ξ» p, p.1 β€’ p.2, change continuous F, have H : continuous (F ∘ (Ξ» p : G Γ— G, (p.1, quotient_group.mk p.2))), { change continuous (Ξ» p : G Γ— G, quotient_group.mk (p.1 * p.2)), refine continuous_coinduced_rng.comp continuous_mul }, exact quotient_map.continuous_lift_prod_right quotient_map_quotient_mk H, end } end quotient namespace units open mul_opposite (continuous_op continuous_unop) variables [monoid Ξ±] [topological_space Ξ±] [monoid Ξ²] [topological_space Ξ²] @[to_additive] instance [has_continuous_mul Ξ±] : topological_group Ξ±Λ£ := { continuous_inv := units.continuous_iff.2 $ ⟨continuous_coe_inv, continuous_coe⟩ } /-- The topological group isomorphism between the units of a product of two monoids, and the product of the units of each monoid. -/ @[to_additive "The topological group isomorphism between the additive units of a product of two additive monoids, and the product of the additive units of each additive monoid."] def homeomorph.prod_units : (Ξ± Γ— Ξ²)Λ£ β‰ƒβ‚œ (Ξ±Λ£ Γ— Ξ²Λ£) := { continuous_to_fun := (continuous_fst.units_map (monoid_hom.fst Ξ± Ξ²)).prod_mk (continuous_snd.units_map (monoid_hom.snd Ξ± Ξ²)), continuous_inv_fun := units.continuous_iff.2 ⟨continuous_coe.fst'.prod_mk continuous_coe.snd', continuous_coe_inv.fst'.prod_mk continuous_coe_inv.snd'⟩, to_equiv := mul_equiv.prod_units.to_equiv } end units section lattice_ops variables {ΞΉ : Sort*} [group G] @[to_additive] lemma topological_group_Inf {ts : set (topological_space G)} (h : βˆ€ t ∈ ts, @topological_group G t _) : @topological_group G (Inf ts) _ := { to_has_continuous_inv := @has_continuous_inv_Inf _ _ _ $ Ξ» t ht, @topological_group.to_has_continuous_inv G t _ $ h t ht, to_has_continuous_mul := @has_continuous_mul_Inf _ _ _ $ Ξ» t ht, @topological_group.to_has_continuous_mul G t _ $ h t ht } @[to_additive] lemma topological_group_infi {ts' : ΞΉ β†’ topological_space G} (h' : βˆ€ i, @topological_group G (ts' i) _) : @topological_group G (β¨… i, ts' i) _ := by { rw ← Inf_range, exact topological_group_Inf (set.forall_range_iff.mpr h') } @[to_additive] lemma topological_group_inf {t₁ tβ‚‚ : topological_space G} (h₁ : @topological_group G t₁ _) (hβ‚‚ : @topological_group G tβ‚‚ _) : @topological_group G (t₁ βŠ“ tβ‚‚) _ := by { rw inf_eq_infi, refine topological_group_infi (Ξ» b, _), cases b; assumption } end lattice_ops /-! ### Lattice of group topologies We define a type class `group_topology Ξ±` which endows a group `Ξ±` with a topology such that all group operations are continuous. Group topologies on a fixed group `Ξ±` are ordered, by reverse inclusion. They form a complete lattice, with `βŠ₯` the discrete topology and `⊀` the indiscrete topology. Any function `f : Ξ± β†’ Ξ²` induces `coinduced f : topological_space Ξ± β†’ group_topology Ξ²`. The additive version `add_group_topology Ξ±` and corresponding results are provided as well. -/ /-- A group topology on a group `Ξ±` is a topology for which multiplication and inversion are continuous. -/ structure group_topology (Ξ± : Type u) [group Ξ±] extends topological_space Ξ±, topological_group Ξ± : Type u /-- An additive group topology on an additive group `Ξ±` is a topology for which addition and negation are continuous. -/ structure add_group_topology (Ξ± : Type u) [add_group Ξ±] extends topological_space Ξ±, topological_add_group Ξ± : Type u attribute [to_additive] group_topology namespace group_topology variables [group Ξ±] /-- A version of the global `continuous_mul` suitable for dot notation. -/ @[to_additive "A version of the global `continuous_add` suitable for dot notation."] lemma continuous_mul' (g : group_topology Ξ±) : by haveI := g.to_topological_space; exact continuous (Ξ» p : Ξ± Γ— Ξ±, p.1 * p.2) := begin letI := g.to_topological_space, haveI := g.to_topological_group, exact continuous_mul, end /-- A version of the global `continuous_inv` suitable for dot notation. -/ @[to_additive "A version of the global `continuous_neg` suitable for dot notation."] lemma continuous_inv' (g : group_topology Ξ±) : by haveI := g.to_topological_space; exact continuous (has_inv.inv : Ξ± β†’ Ξ±) := begin letI := g.to_topological_space, haveI := g.to_topological_group, exact continuous_inv, end @[to_additive] lemma to_topological_space_injective : function.injective (to_topological_space : group_topology Ξ± β†’ topological_space Ξ±):= Ξ» f g h, by { cases f, cases g, congr' } @[ext, to_additive] lemma ext' {f g : group_topology Ξ±} (h : f.is_open = g.is_open) : f = g := to_topological_space_injective $ topological_space_eq h /-- The ordering on group topologies on the group `Ξ³`. `t ≀ s` if every set open in `s` is also open in `t` (`t` is finer than `s`). -/ @[to_additive "The ordering on group topologies on the group `Ξ³`. `t ≀ s` if every set open in `s` is also open in `t` (`t` is finer than `s`)."] instance : partial_order (group_topology Ξ±) := partial_order.lift to_topological_space to_topological_space_injective @[simp, to_additive] lemma to_topological_space_le {x y : group_topology Ξ±} : x.to_topological_space ≀ y.to_topological_space ↔ x ≀ y := iff.rfl @[to_additive] instance : has_top (group_topology Ξ±) := ⟨{to_topological_space := ⊀, continuous_mul := continuous_top, continuous_inv := continuous_top}⟩ @[simp, to_additive] lemma to_topological_space_top : (⊀ : group_topology Ξ±).to_topological_space = ⊀ := rfl @[to_additive] instance : has_bot (group_topology Ξ±) := ⟨{to_topological_space := βŠ₯, continuous_mul := by continuity, continuous_inv := continuous_bot}⟩ @[simp, to_additive] lemma to_topological_space_bot : (βŠ₯ : group_topology Ξ±).to_topological_space = βŠ₯ := rfl @[to_additive] instance : bounded_order (group_topology Ξ±) := { top := ⊀, le_top := Ξ» x, show x.to_topological_space ≀ ⊀, from le_top, bot := βŠ₯, bot_le := Ξ» x, show βŠ₯ ≀ x.to_topological_space, from bot_le } @[to_additive] instance : has_inf (group_topology Ξ±) := { inf := Ξ» x y, ⟨x.1 βŠ“ y.1, topological_group_inf x.2 y.2⟩ } @[simp, to_additive] lemma to_topological_space_inf (x y : group_topology Ξ±) : (x βŠ“ y).to_topological_space = x.to_topological_space βŠ“ y.to_topological_space := rfl @[to_additive] instance : semilattice_inf (group_topology Ξ±) := to_topological_space_injective.semilattice_inf _ to_topological_space_inf @[to_additive] instance : inhabited (group_topology Ξ±) := ⟨⊀⟩ local notation `cont` := @continuous _ _ /-- Infimum of a collection of group topologies. -/ @[to_additive "Infimum of a collection of additive group topologies"] instance : has_Inf (group_topology Ξ±) := { Inf := Ξ» S, ⟨Inf (to_topological_space '' S), topological_group_Inf $ ball_image_iff.2 $ Ξ» t ht, t.2⟩ } @[simp, to_additive] lemma to_topological_space_Inf (s : set (group_topology Ξ±)) : (Inf s).to_topological_space = Inf (to_topological_space '' s) := rfl @[simp, to_additive] lemma to_topological_space_infi {ΞΉ} (s : ΞΉ β†’ group_topology Ξ±) : (β¨… i, s i).to_topological_space = β¨… i, (s i).to_topological_space := congr_arg Inf (range_comp _ _).symm /-- Group topologies on `Ξ³` form a complete lattice, with `βŠ₯` the discrete topology and `⊀` the indiscrete topology. The infimum of a collection of group topologies is the topology generated by all their open sets (which is a group topology). The supremum of two group topologies `s` and `t` is the infimum of the family of all group topologies contained in the intersection of `s` and `t`. -/ @[to_additive "Group topologies on `Ξ³` form a complete lattice, with `βŠ₯` the discrete topology and `⊀` the indiscrete topology. The infimum of a collection of group topologies is the topology generated by all their open sets (which is a group topology). The supremum of two group topologies `s` and `t` is the infimum of the family of all group topologies contained in the intersection of `s` and `t`."] instance : complete_semilattice_Inf (group_topology Ξ±) := { Inf_le := Ξ» S a haS, to_topological_space_le.1 $ Inf_le ⟨a, haS, rfl⟩, le_Inf := begin intros S a hab, apply topological_space.complete_lattice.le_Inf, rintros _ ⟨b, hbS, rfl⟩, exact hab b hbS, end, ..group_topology.has_Inf, ..group_topology.partial_order } @[to_additive] instance : complete_lattice (group_topology Ξ±) := { inf := (βŠ“), top := ⊀, bot := βŠ₯, ..group_topology.bounded_order, ..group_topology.semilattice_inf, ..complete_lattice_of_complete_semilattice_Inf _ } /-- Given `f : Ξ± β†’ Ξ²` and a topology on `Ξ±`, the coinduced group topology on `Ξ²` is the finest topology such that `f` is continuous and `Ξ²` is a topological group. -/ @[to_additive "Given `f : Ξ± β†’ Ξ²` and a topology on `Ξ±`, the coinduced additive group topology on `Ξ²` is the finest topology such that `f` is continuous and `Ξ²` is a topological additive group."] def coinduced {Ξ± Ξ² : Type*} [t : topological_space Ξ±] [group Ξ²] (f : Ξ± β†’ Ξ²) : group_topology Ξ² := Inf {b : group_topology Ξ² | (topological_space.coinduced f t) ≀ b.to_topological_space} @[to_additive] lemma coinduced_continuous {Ξ± Ξ² : Type*} [t : topological_space Ξ±] [group Ξ²] (f : Ξ± β†’ Ξ²) : cont t (coinduced f).to_topological_space f := begin rw continuous_iff_coinduced_le, refine le_Inf _, rintros _ ⟨t', ht', rfl⟩, exact ht', end end group_topology
b2f665588de114f2c038d0dfd4596c82d5b3a904
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/order/filter/ennreal.lean
6b86310dfcb3a1ee9d3b432a6651b1ac0f1d6c15
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
4,506
lean
/- Copyright (c) 2021 RΓ©my Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: RΓ©my Degenne -/ import data.real.ennreal import order.filter.countable_Inter import order.liminf_limsup /-! # Order properties of extended non-negative reals This file compiles filter-related results about `ℝβ‰₯0∞` (see data/real/ennreal.lean). -/ open filter open_locale filter ennreal namespace ennreal variables {Ξ± : Type*} {f : filter Ξ±} lemma eventually_le_limsup [countable_Inter_filter f] (u : Ξ± β†’ ℝβ‰₯0∞) : βˆ€αΆ  y in f, u y ≀ f.limsup u := begin by_cases hx_top : f.limsup u = ⊀, { simp_rw hx_top, exact eventually_of_forall (Ξ» a, le_top), }, have h_forall_le : βˆ€αΆ  y in f, βˆ€ n : β„•, u y < f.limsup u + (1:ℝβ‰₯0∞)/n, { rw eventually_countable_forall, refine Ξ» n, eventually_lt_of_limsup_lt _, nth_rewrite 0 ←add_zero (f.limsup u), exact (add_lt_add_iff_left (lt_top_iff_ne_top.mpr hx_top)).mpr (by simp), }, refine h_forall_le.mono (Ξ» y hy, le_of_forall_pos_le_add (Ξ» r hr_pos hx_top, _)), have hr_ne_zero : (r : ℝβ‰₯0∞) β‰  0, { rw [ne.def, coe_eq_zero], exact (ne_of_lt hr_pos).symm, }, cases (exists_inv_nat_lt hr_ne_zero) with i hi, rw inv_eq_one_div at hi, exact le_trans (le_of_lt (hy i)) (add_le_add_left hi.le (f.limsup u)), end lemma limsup_eq_zero_iff [countable_Inter_filter f] {u : Ξ± β†’ ℝβ‰₯0∞} : f.limsup u = 0 ↔ u =αΆ [f] 0 := begin split; intro h, { have hu_zero := eventually_le.trans (eventually_le_limsup u) (eventually_of_forall (Ξ» _, le_of_eq h)), exact hu_zero.mono (Ξ» x hx, le_antisymm hx (zero_le _)), }, { rw limsup_congr h, simp_rw [pi.zero_apply, ←ennreal.bot_eq_zero, limsup_const_bot] }, end lemma limsup_const_mul_of_ne_top {u : Ξ± β†’ ℝβ‰₯0∞} {a : ℝβ‰₯0∞} (ha_top : a β‰  ⊀) : f.limsup (Ξ» (x : Ξ±), a * (u x)) = a * f.limsup u := begin by_cases ha_zero : a = 0, { simp_rw [ha_zero, zero_mul, ←ennreal.bot_eq_zero], exact limsup_const_bot, }, let g := Ξ» x : ℝβ‰₯0∞, a * x, have hg_bij : function.bijective g, from function.bijective_iff_has_inverse.mpr ⟨(Ξ» x, a⁻¹ * x), ⟨λ x, by simp [←mul_assoc, inv_mul_cancel ha_zero ha_top], Ξ» x, by simp [g, ←mul_assoc, mul_inv_cancel ha_zero ha_top]⟩⟩, have hg_mono : strict_mono g, from strict_mono_of_monotone_of_injective (Ξ» _ _ _, by rwa mul_le_mul_left ha_zero ha_top) hg_bij.1, let g_iso := strict_mono.order_iso_of_surjective g hg_mono hg_bij.2, refine (order_iso.limsup_apply g_iso _ _ _ _).symm, all_goals { by is_bounded_default }, end lemma limsup_const_mul [countable_Inter_filter f] {u : Ξ± β†’ ℝβ‰₯0∞} {a : ℝβ‰₯0∞} : f.limsup (Ξ» (x : Ξ±), a * (u x)) = a * f.limsup u := begin by_cases ha_top : a β‰  ⊀, { exact limsup_const_mul_of_ne_top ha_top, }, push_neg at ha_top, by_cases hu : u =αΆ [f] 0, { have hau : (Ξ» x, a * (u x)) =αΆ [f] 0, { refine hu.mono (Ξ» x hx, _), rw pi.zero_apply at hx, simp [hx], }, simp only [limsup_congr hu, limsup_congr hau, pi.zero_apply, ← bot_eq_zero, limsup_const_bot], simp, }, { simp_rw [ha_top, top_mul], have hu_mul : βˆƒαΆ  (x : Ξ±) in f, ⊀ ≀ ite (u x = 0) (0 : ℝβ‰₯0∞) ⊀, { rw [eventually_eq, not_eventually] at hu, refine hu.mono (Ξ» x hx, _), rw pi.zero_apply at hx, simp [hx], }, have h_top_le : f.limsup (Ξ» (x : Ξ±), ite (u x = 0) (0 : ℝβ‰₯0∞) ⊀) = ⊀, from eq_top_iff.mpr (le_limsup_of_frequently_le hu_mul), have hfu : f.limsup u β‰  0, from mt limsup_eq_zero_iff.1 hu, simp only [h_top_le, hfu, if_false], }, end lemma limsup_add_le [countable_Inter_filter f] (u v : Ξ± β†’ ℝβ‰₯0∞) : f.limsup (u + v) ≀ f.limsup u + f.limsup v := Inf_le ((eventually_le_limsup u).mp ((eventually_le_limsup v).mono (Ξ» _ hxg hxf, add_le_add hxf hxg))) lemma limsup_liminf_le_liminf_limsup {Ξ²} [encodable Ξ²] {f : filter Ξ±} [countable_Inter_filter f] {g : filter Ξ²} (u : Ξ± β†’ Ξ² β†’ ℝβ‰₯0∞) : f.limsup (Ξ» (a : Ξ±), g.liminf (Ξ» (b : Ξ²), u a b)) ≀ g.liminf (Ξ» b, f.limsup (Ξ» a, u a b)) := begin have h1 : βˆ€αΆ  a in f, βˆ€ b, u a b ≀ f.limsup (Ξ» a', u a' b), by { rw eventually_countable_forall, exact Ξ» b, ennreal.eventually_le_limsup (Ξ» a, u a b), }, refine Inf_le (h1.mono (Ξ» x hx, filter.liminf_le_liminf (filter.eventually_of_forall hx) _)), filter.is_bounded_default, end end ennreal
b1597087ebff5a45eb10f2f126eebc4fcec72260
d406927ab5617694ec9ea7001f101b7c9e3d9702
/archive/100-theorems-list/93_birthday_problem.lean
6d9c2d8bf3b1d05248cb2ab51af1ca9c07a1fee3
[ "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
3,311
lean
/- Copyright (c) 2021 Eric Rodriguez. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Rodriguez -/ import data.fintype.card_embedding import probability.cond_count import probability.notation /-! # Birthday Problem This file proves Theorem 93 from the [100 Theorems List](https://www.cs.ru.nl/~freek/100/). As opposed to the standard probabilistic statement, we instead state the birthday problem in terms of injective functions. The general result about `fintype.card (Ξ± β†ͺ Ξ²)` which this proof uses is `fintype.card_embedding_eq`. -/ local notation (name := finset.card) `|` x `|` := finset.card x local notation (name := fintype.card) `β€–` x `β€–` := fintype.card x /-- **Birthday Problem**: set cardinality interpretation. -/ theorem birthday : 2 * β€–fin 23 β†ͺ fin 365β€– < β€–fin 23 β†’ fin 365β€– ∧ 2 * β€–fin 22 β†ͺ fin 365β€– > β€–fin 22 β†’ fin 365β€– := begin simp only [nat.desc_factorial, fintype.card_fin, fintype.card_embedding_eq, fintype.card_fun], norm_num end section measure_theory open measure_theory probability_theory open_locale probability_theory ennreal variables {n m : β„•} /- In order for Lean to understand that we can take probabilities in `fin 23 β†’ fin 365`, we must tell Lean that there is a `measurable_space` structure on the space. Note that this instance is only for `fin m` - Lean automatically figures out that the function space `fin n β†’ fin m` is _also_ measurable, by using `measurable_space.pi`, and furthermore that all sets are measurable, from `measurable_singleton_class.pi`. -/ instance : measurable_space (fin m) := ⊀ instance : measurable_singleton_class (fin m) := ⟨λ _, trivial⟩ /- We then endow the space with a canonical measure, which is called β„™. We define this to be the conditional counting measure. -/ noncomputable instance : measure_space (fin n β†’ fin m) := ⟨cond_count set.univ⟩ /- The canonical measure on `fin n β†’ fin m` is a probability measure (except on an empty space). -/ instance : is_probability_measure (β„™ : measure (fin n β†’ fin (m + 1))) := cond_count_is_probability_measure set.finite_univ set.univ_nonempty lemma fin_fin.measure_apply {s : set $ fin n β†’ fin m} : β„™ s = (|s.to_finite.to_finset|) / β€–fin n β†’ fin mβ€– := by erw [cond_count_univ, measure.count_apply_finite] /-- **Birthday Problem**: first probabilistic interpretation. -/ theorem birthday_measure : β„™ {f : fin 23 β†’ fin 365 | function.injective f} < 1 / 2 := begin -- most of this proof is essentially converting it to the same form as `birthday`. rw [fin_fin.measure_apply], generalize_proofs hfin, have : |hfin.to_finset| = 42200819302092359872395663074908957253749760700776448000000, { transitivity β€–fin 23 β†ͺ fin 365β€–, { simp_rw [←fintype.card_coe, set.finite.coe_sort_to_finset, set.coe_set_of], exact fintype.card_congr (equiv.subtype_injective_equiv_embedding _ _) }, { simp only [fintype.card_embedding_eq, fintype.card_fin, nat.desc_factorial], norm_num } }, rw [this, ennreal.lt_div_iff_mul_lt, mul_comm, mul_div, ennreal.div_lt_iff], rotate, iterate 2 { right, norm_num }, iterate 2 { left, norm_num }, norm_cast, simp only [fintype.card_pi, fintype.card_fin], norm_num end end measure_theory
35934edc504af61365318212f4cf76a8c29e1431
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/all_goals.lean
28408aaf723cc850a41740d1459ea867c5aa7d3f
[ "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
241
lean
import data.nat open nat attribute nat.add [unfold 2] attribute nat.rec_on [unfold 2] example (a b c : nat) : a + 0 = 0 + a ∧ b + 0 = 0 + b := begin apply and.intro, all_goals esimp[of_num], all_goals (state; rewrite zero_add) end
491b556bd1d10f9f06cd2760535a9195ea226cdd
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/abelian/opposite.lean
bf0c940586623e24e14f2e3d7083b56e9c204fe0
[ "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
3,934
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.abelian.basic import category_theory.preadditive.opposite import category_theory.limits.opposites /-! # The opposite of an abelian category is abelian. -/ noncomputable theory namespace category_theory open category_theory.limits variables (C : Type*) [category C] [abelian C] local attribute [instance] has_finite_limits_of_has_equalizers_and_finite_products has_finite_colimits_of_has_coequalizers_and_finite_coproducts instance : abelian Cα΅’α΅– := { normal_mono_of_mono := Ξ» X Y f m, by exactI normal_mono_of_normal_epi_unop _ (normal_epi_of_epi f.unop), normal_epi_of_epi := Ξ» X Y f m, by exactI normal_epi_of_normal_mono_unop _ (normal_mono_of_mono f.unop), } section variables {C} {X Y : C} (f : X ⟢ Y) {A B : Cα΅’α΅–} (g : A ⟢ B) -- TODO: Generalize (this will work whenever f has a cokernel) -- (The abelian case is probably sufficient for most applications.) /-- The kernel of `f.op` is the opposite of `cokernel f`. -/ @[simps] def kernel_op_unop : (kernel f.op).unop β‰… cokernel f := { hom := (kernel.lift f.op (cokernel.Ο€ f).op $ by simp [← op_comp]).unop, inv := cokernel.desc f (kernel.ΞΉ f.op).unop $ by { rw [← f.unop_op, ← unop_comp, f.unop_op], simp }, hom_inv_id' := begin rw [← unop_id, ← (cokernel.desc f _ _).unop_op, ← unop_comp], congr' 1, dsimp, ext, simp [← op_comp], end, inv_hom_id' := begin dsimp, ext, simp [← unop_comp], end } -- TODO: Generalize (this will work whenever f has a kernel) -- (The abelian case is probably sufficient for most applications.) /-- The cokernel of `f.op` is the opposite of `kernel f`. -/ @[simps] def cokernel_op_unop : (cokernel f.op).unop β‰… kernel f := { hom := kernel.lift f (cokernel.Ο€ f.op).unop $ by { rw [← f.unop_op, ← unop_comp, f.unop_op], simp }, inv := (cokernel.desc f.op (kernel.ΞΉ f).op $ by simp [← op_comp]).unop, hom_inv_id' := begin rw [← unop_id, ← (kernel.lift f _ _).unop_op, ← unop_comp], congr' 1, dsimp, ext, simp [← op_comp], end, inv_hom_id' := begin dsimp, ext, simp [← unop_comp], end } /-- The kernel of `g.unop` is the opposite of `cokernel g`. -/ @[simps] def kernel_unop_op : opposite.op (kernel g.unop) β‰… cokernel g := (cokernel_op_unop g.unop).op /-- The cokernel of `g.unop` is the opposite of `kernel g`. -/ @[simps] def cokernel_unop_op : opposite.op (cokernel g.unop) β‰… kernel g := (kernel_op_unop g.unop).op lemma cokernel.Ο€_op : (cokernel.Ο€ f.op).unop = (cokernel_op_unop f).hom ≫ kernel.ΞΉ f ≫ eq_to_hom (opposite.unop_op _).symm := by simp [cokernel_op_unop] lemma kernel.ΞΉ_op : (kernel.ΞΉ f.op).unop = eq_to_hom (opposite.unop_op _) ≫ cokernel.Ο€ f ≫ (kernel_op_unop f).inv := by simp [kernel_op_unop] /-- The kernel of `f.op` is the opposite of `cokernel f`. -/ @[simps] def kernel_op_op : kernel f.op β‰… opposite.op (cokernel f) := (kernel_op_unop f).op.symm /-- The cokernel of `f.op` is the opposite of `kernel f`. -/ @[simps] def cokernel_op_op : cokernel f.op β‰… opposite.op (kernel f) := (cokernel_op_unop f).op.symm /-- The kernel of `g.unop` is the opposite of `cokernel g`. -/ @[simps] def kernel_unop_unop : kernel g.unop β‰… (cokernel g).unop := (kernel_unop_op g).unop.symm lemma kernel.ΞΉ_unop : (kernel.ΞΉ g.unop).op = eq_to_hom (opposite.op_unop _) ≫ cokernel.Ο€ g ≫ (kernel_unop_op g).inv := by simp lemma cokernel.Ο€_unop : (cokernel.Ο€ g.unop).op = (cokernel_unop_op g).hom ≫ kernel.ΞΉ g ≫ eq_to_hom (opposite.op_unop _).symm := by simp /-- The cokernel of `g.unop` is the opposite of `kernel g`. -/ @[simps] def cokernel_unop_unop : cokernel g.unop β‰… (kernel g).unop := (cokernel_unop_op g).unop.symm end end category_theory
8d000222e04241fda2d21ca43203d796050d78d8
508d8fd300d597fd3f7f0f8f3d0f70275d21965f
/src/mathematica.lean
9dd18bb7547556523a4dd9e7d680e57b770083f1
[]
no_license
holtzermann17/mathematica
e3cdf06b4e1a118fcb22dbf8a8b722b33cde93d1
ce589a885ccdea477e98e4ce38d6d3a4ae8940a9
refs/heads/master
1,588,476,421,760
1,553,861,282,000
1,553,861,282,000
178,398,173
0
0
null
1,553,861,103,000
1,553,861,103,000
null
UTF-8
Lean
false
false
30,103
lean
/- Copyright (c) 2017 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis -/ import .mathematica_parser system.io open expr string level name binder_info native namespace list variables {Ξ± Ξ² : Type} universes u v w def for : list Ξ± β†’ (Ξ± β†’ Ξ²) β†’ list Ξ² := flip map end list meta def rb_lmap.of_list {key : Type} {data : Type} [has_lt key] [decidable_rel ((<) : key β†’ key β†’ Prop)] : list (key Γ— data) β†’ rb_lmap key data | [] := rb_lmap.mk key data | ((k, v)::ls) := rb_lmap.insert (rb_lmap.of_list ls) k v meta def rb_map.insert_list {key : Type} {data : Type} : rb_map key data β†’ list (key Γ— data) β†’ rb_map key data | m [] := m | m ((k, d) :: t) := rb_map.insert_list (rb_map.insert m k d) t local attribute [instance] htfi -- this has the expected behavior only if i is under the max size of unsigned def unsigned_of_int (i : int) : unsigned := int.rec_on i (Ξ» k, unsigned.of_nat k) (Ξ» k, unsigned.of_nat k) meta def expand_let : expr β†’ expr | (elet nm tp val bod) := expr.replace bod (Ξ» e n, match e with |var n := some val | _ := none end) | e := e /- The following definitions are used to reflect Lean syntax into Mathematica. We reflect the types - name - level - list level - binder_info - expr -/ namespace mathematica meta def form_of_name : name β†’ string | anonymous := "LeanNameAnonymous" | (mk_string s nm) := "LeanNameMkString[\"" ++ s ++ "\", " ++ form_of_name nm ++ "]" | (mk_numeral i nm) := "LeanNameMkNum[" ++ to_string i ++ ", " ++ form_of_name nm ++ "]" meta def form_of_lvl : level β†’ string | zero := "LeanZeroLevel" | (succ l) := "LeanLevelSucc[" ++ form_of_lvl l ++ "]" | (level.max l1 l2) := "LeanLevelMax[" ++ form_of_lvl l1 ++ ", " ++ form_of_lvl l2 ++ "]" | (imax l1 l2) := "LeanLevelIMax[" ++ form_of_lvl l1 ++ ", " ++ form_of_lvl l2 ++ "]" | (param nm) := "LeanLevelParam[" ++ form_of_name nm ++ "]" | (mvar nm) := "LeanLevelMeta[" ++ form_of_name nm ++ "]" meta def form_of_lvl_list : list level β†’ string | [] := "LeanLevelListNil" | (h :: t) := "LeanLevelListCons[" ++ form_of_lvl h ++ ", " ++ form_of_lvl_list t ++ "]" meta def form_of_binder_info : binder_info β†’ string | binder_info.default := "BinderInfoDefault" | implicit := "BinderInfoImplicit" | strict_implicit := "BinderInfoStrictImplicit" | inst_implicit := "BinderInfoInstImplicit" | other := "BinderInfoOther" /- let expressions get unfolded before translation. translating macro expressions is not supported. -/ meta def form_of_expr : expr β†’ string | (var i) := "LeanVar[" ++ (format.to_string (to_fmt i) options.mk) ++ "]" | (sort lvl) := "LeanSort[" ++ form_of_lvl lvl ++ "]" | (const nm lvls) := "LeanConst[" ++ form_of_name nm ++ ", " ++ form_of_lvl_list lvls ++ "]" | (mvar nm nm' tp) := "LeanMetaVar[" ++ form_of_name nm ++ ", " ++ form_of_expr tp ++ "]" | (local_const nm ppnm bi tp) := "LeanLocal[" ++ form_of_name nm ++ ", " ++ form_of_name ppnm ++ ", " ++ form_of_binder_info bi ++ ", " ++ form_of_expr tp ++ "]" | (app f e) := "LeanApp[" ++ form_of_expr f ++ ", " ++ form_of_expr e ++ "]" | (lam nm bi tp bod) := "LeanLambda[" ++ form_of_name nm ++ ", " ++ form_of_binder_info bi ++ ", " ++ form_of_expr tp ++ ", " ++ form_of_expr bod ++ "]" | (pi nm bi tp bod) := "LeanPi[" ++ form_of_name nm ++ ", " ++ form_of_binder_info bi ++ ", " ++ form_of_expr tp ++ ", " ++ form_of_expr bod ++ "]" | (elet nm tp val bod) := form_of_expr $ expand_let $ elet nm tp val bod | (macro mdf mlst) := "LeanMacro" /- The following definitions are used to make pexprs out of various types. -/ end mathematica meta def pexpr_of_nat : β„• β†’ pexpr | 0 := ```(0) | 1 := ```(1) | k := if k%2=0 then ```(bit0 %%(pexpr_of_nat (k/2))) else ```(bit1 %%(pexpr_of_nat (k/2))) meta def pexpr_of_int : int β†’ pexpr | (int.of_nat n) := pexpr_of_nat n | (int.neg_succ_of_nat n) := ```(-%%(pexpr_of_nat (n+1))) namespace tactic namespace mathematica section def write_file (fn : string) (cnts : string) (mode := io.mode.write) : io unit := do h ← io.mk_file_handle fn io.mode.write, io.fs.write h cnts.to_char_buffer, io.fs.close h end /-- execute str evaluates str in Mathematica. The evaluation happens in a unique context; declarations that are made during evaluation will not be available in future evaluations. -/ meta def execute (cmd : string) (add_args : list string := []) : tactic char_buffer := let cmd' := escape_term cmd ++ "&!", args := ["_target/deps/mathematica/src/client2.py"].append add_args in if cmd'.length < 2040 then tactic.unsafe_run_io $ io.buffer_cmd { cmd := "python3", args := args.append [/-escape_quotes -/cmd'] } else do path ← mathematica.temp_file_name "exch", unsafe_run_io $ write_file path cmd' io.mode.write, unsafe_run_io $ io.buffer_cmd { cmd := "python3", args := args.append ["-f", path] } def get_cwd : io string := io.cmd {cmd := "pwd"} >>= Ξ» s, pure $ strip_newline s meta def execute_and_eval (cmd : string) : tactic mmexpr := execute cmd >>= parse_mmexpr_tac /-- execute_global str evaluates str in Mathematica. The evaluation happens in the global context; declarations that are made during evaluation will persist. -/ meta def execute_global (cmd : string) : tactic char_buffer := execute cmd ["-g"] /-- Returns the path to {run_directory}/extras/ -/ meta def extras_path : tactic string := unsafe_run_io get_cwd >>= Ξ» s, pure $ strip_trailing_whitespace s ++ "/src/extras/" end mathematica end tactic namespace mathematica open mmexpr tactic private meta def pexpr_mk_app : pexpr β†’ list pexpr β†’ pexpr | fn [] := fn | fn (h::t) := pexpr_mk_app (app fn h) t section translation open rb_lmap @[reducible] meta def trans_env := rb_map string expr meta def trans_env.empty := rb_map.mk string expr meta def sym_trans_pexpr_rule := string Γ— pexpr meta def sym_trans_expr_rule := string Γ— expr meta def app_trans_pexpr_keyed_rule := string Γ— (trans_env β†’ list mmexpr β†’ tactic pexpr) meta def app_trans_expr_keyed_rule := string Γ— (trans_env β†’ list mmexpr β†’ tactic expr) meta def app_trans_pexpr_unkeyed_rule := trans_env β†’ mmexpr β†’ list mmexpr β†’ tactic pexpr meta def app_trans_expr_unkeyed_rule := trans_env β†’ mmexpr β†’ list mmexpr β†’ tactic expr -- databases private meta def mk_sym_trans_pexpr_db (l : list name) : tactic (rb_lmap string pexpr) := do dcls ← monad.mapm (Ξ» n, mk_const n >>= eval_expr sym_trans_pexpr_rule) l, return $ of_list dcls private meta def mk_sym_trans_expr_db (l : list name) : tactic (rb_lmap string expr) := do dcls ← monad.mapm (Ξ» n, mk_const n >>= eval_expr sym_trans_expr_rule) l, return $ of_list dcls private meta def mk_app_trans_pexpr_keyed_db (l : list name) : tactic (rb_lmap string (trans_env β†’ list mmexpr β†’ tactic pexpr)) := do dcls ← monad.mapm (Ξ» n, mk_const n >>= eval_expr app_trans_pexpr_keyed_rule) l, return $ of_list dcls private meta def mk_app_trans_expr_keyed_db (l : list name) : tactic (rb_lmap string (trans_env β†’ list mmexpr β†’ tactic expr)) := do dcls ← monad.mapm (Ξ» n, mk_const n >>= eval_expr app_trans_expr_keyed_rule) l, return $ of_list dcls private meta def mk_app_trans_pexpr_unkeyed_db (l : list name) : tactic (list (trans_env β†’ mmexpr β†’ list mmexpr β†’ tactic pexpr)) := monad.mapm (Ξ» n, mk_const n >>= eval_expr app_trans_pexpr_unkeyed_rule) l private meta def mk_app_trans_expr_unkeyed_db (l : list name) : tactic (list (trans_env β†’ mmexpr β†’ list mmexpr β†’ tactic expr)) := monad.mapm (Ξ» n, mk_const n >>= eval_expr app_trans_expr_unkeyed_rule) l meta def ensure_has_type (e : expr) : name β†’ nat β†’ bool β†’ command := Ξ» decl_name _ _, do dcl ← get_decl decl_name, is_def_eq e (dcl.type) @[user_attribute] private meta def sym_to_pexpr_rule : user_attribute (rb_lmap string pexpr) unit := { name := `sym_to_pexpr, descr := "rule for translating a mmexpr.sym to a pexpr", cache_cfg := ⟨mk_sym_trans_pexpr_db, []⟩, after_set := ensure_has_type `(sym_trans_pexpr_rule) } @[user_attribute] private meta def sym_to_expr_rule : user_attribute (rb_lmap string expr) unit := { name := `sym_to_expr, descr := "rule for translating a mmexpr.sym to a expr", cache_cfg := ⟨mk_sym_trans_expr_db, []⟩, after_set := ensure_has_type `(sym_trans_expr_rule) } @[user_attribute] private meta def app_to_pexpr_keyed_rule : user_attribute (rb_lmap string (trans_env β†’ list mmexpr β†’ tactic pexpr)) := { name := `app_to_pexpr_keyed, descr := "rule for translating a mmexpr.app to a pexpr", cache_cfg := ⟨mk_app_trans_pexpr_keyed_db, []⟩, after_set := ensure_has_type `(app_trans_pexpr_keyed_rule) } @[user_attribute] private meta def app_to_expr_keyed_rule : user_attribute (rb_lmap string (trans_env β†’ list mmexpr β†’ tactic expr)) := { name := `app_to_expr_keyed, descr := "rule for translating a mmexpr.app to a expr", cache_cfg := ⟨mk_app_trans_expr_keyed_db, []⟩, after_set := ensure_has_type `(app_trans_expr_keyed_rule) } @[user_attribute] private meta def app_to_pexpr_unkeyed_rule : user_attribute (list (trans_env β†’ mmexpr β†’ list mmexpr β†’ tactic pexpr)) := { name := `app_to_pexpr_unkeyed, descr := "rule for translating a mmexpr.app to a pexpr", cache_cfg := ⟨mk_app_trans_pexpr_unkeyed_db, []⟩, after_set := ensure_has_type `(app_trans_pexpr_unkeyed_rule) } @[user_attribute] private meta def app_to_expr_unkeyed_rule : user_attribute (list (trans_env β†’ mmexpr β†’ list mmexpr β†’ tactic expr)) := { name := `app_to_expr_unkeyed, descr := "rule for translating a mmexpr.app to a expr", cache_cfg := ⟨mk_app_trans_expr_unkeyed_db, []⟩, after_set := ensure_has_type `(app_trans_expr_unkeyed_rule) } private meta def expr_of_mmexpr_app_keyed (env : trans_env) : mmexpr β†’ list mmexpr β†’ tactic expr | (sym hd) args := do expr_db ← app_to_expr_keyed_rule.get_cache, tactic.first $ (rb_lmap.find expr_db hd).for $ Ξ» f, f env args | _ _ := failed private meta def expr_of_mmexpr_app_unkeyed (env : trans_env) (hd : mmexpr) (args : list mmexpr) : tactic expr := do expr_db ← app_to_expr_unkeyed_rule.get_cache, tactic.first (list.map (Ξ» f : trans_env β†’ mmexpr β†’ list mmexpr β†’ tactic expr, f env hd args) expr_db) private meta def expr_of_mmexpr_app (env : trans_env) (expr_of_mmexpr : trans_env β†’ mmexpr β†’ tactic expr) (m : mmexpr) (l : list mmexpr) : tactic expr := expr_of_mmexpr_app_keyed env m l <|> expr_of_mmexpr_app_unkeyed env m l private meta def pexpr_of_mmexpr_app_keyed (env : trans_env) : mmexpr β†’ list mmexpr β†’ tactic pexpr | (sym hd) args := do expr_db ← app_to_pexpr_keyed_rule.get_cache, tactic.first $ (rb_lmap.find expr_db hd).for $ Ξ» f, f env args | _ _ := failed private meta def pexpr_of_mmexpr_app_unkeyed (env : trans_env) (hd : mmexpr) (args : list mmexpr) : tactic pexpr := do expr_db ← app_to_pexpr_unkeyed_rule.get_cache, tactic.first (list.map (Ξ» f : trans_env β†’ mmexpr β†’ list mmexpr β†’ tactic pexpr, f env hd args) expr_db) private meta def pexpr_of_mmexpr_app_decomp (env : trans_env) (pexpr_of_mmexpr : trans_env β†’ mmexpr β†’ tactic pexpr) (hd : mmexpr) (args : list mmexpr) : tactic pexpr := do hs ← pexpr_of_mmexpr env hd, args' ← monad.mapm (pexpr_of_mmexpr env) args, return $ pexpr_mk_app hs args' private meta def pexpr_of_mmexpr_app (env : trans_env) (pexpr_of_mmexpr : trans_env β†’ mmexpr β†’ tactic pexpr) (m : mmexpr) (l : list mmexpr) : tactic pexpr := pexpr_of_mmexpr_app_keyed env m l <|> pexpr_of_mmexpr_app_unkeyed env m l <|> pexpr_of_mmexpr_app_decomp env pexpr_of_mmexpr m l private meta def find_in_env (env : trans_env) (sym : string) : tactic expr := match rb_map.find env sym with | some e := return e | none := failed end /-- expr_of_mmexpr env m will attempt to translate m to an expr, using translation rules found by the attribute manager. env maps symbols (representing bound variables) to placeholder exprs. -/ meta def expr_of_mmexpr : trans_env β†’ mmexpr β†’ tactic expr | env (sym s) := find_in_env env s <|> do expr_db ← sym_to_expr_rule.get_cache, match rb_lmap.find expr_db s with | (h :: t) := return h | [] := fail ("Couldn't find translation for sym \"" ++ s ++ "\"") end | env (mstr s) := return (string.reflect s)--to_expr (_root_.quote s) | env (mreal r) := return (reflect r) | env (app hd args) := expr_of_mmexpr_app env expr_of_mmexpr hd args | env (mint i) := failed private meta def pexpr_of_mmexpr_aux (env : trans_env) (pexpr_of_mmexpr : trans_env β†’ mmexpr β†’ tactic pexpr) : mmexpr β†’ tactic pexpr | (sym s) := do expr_db ← sym_to_pexpr_rule.get_cache, match rb_lmap.find expr_db s with | (h :: t) := return h | [] := parse_name_tac s >>= resolve_name <|> fail ("Couldn't find translation for sym \"" ++ s ++ "\"") end | (mint i ) := return $ pexpr_of_int i | (app hd args) := pexpr_of_mmexpr_app env pexpr_of_mmexpr hd args | (mstr s) := fail "unreachable, str case shouldn't reach pexpr_of_mmexpr_aux" | (mreal r) := fail "unreachable, real case shouldn't reach pexpr_of_mmexpr_aux" /-- pexpr_of_mmexpr env m will attempt to translate m to a pexpr, using translation rules found by the attribute manager. env maps symbols (representing bound variables) to placeholder exprs. -/ meta def pexpr_of_mmexpr : trans_env β†’ mmexpr β†’ tactic pexpr := Ξ» env m, (do e ← expr_of_mmexpr env m, return ```(%%e)) <|> (pexpr_of_mmexpr_aux env pexpr_of_mmexpr m) end translation section unreflect meta def level_of_mmexpr : mmexpr β†’ tactic level | (sym "LeanZeroLevel") := return $ level.zero | (app (sym "LeanLevelSucc") [m]) := do m' ← level_of_mmexpr m, return $ level.succ m' | (app (sym "LeanLevelMax") [m1, m2]) := do m1' ← level_of_mmexpr m1, m2' ← level_of_mmexpr m2, return $ level.max m1' m2' | (app (sym "LeanLevelIMax") [m1, m2]) := do m1' ← level_of_mmexpr m1, m2' ← level_of_mmexpr m2, return $ level.imax m1' m2' | (app (sym "LeanLevelParam") [mstr s]) := return $ level.param s | (app (sym "LeanLevelMeta") [mstr s]) := return $ level.mvar s | _ := failed meta def level_list_of_mmexpr : mmexpr β†’ tactic (list level) | (sym "LeanLevelListNil") := return [] | (app (sym "LeanLevelListCons") [h, t]) := do h' ← level_of_mmexpr h, t' ← level_list_of_mmexpr t, return $ h' :: t' | _ := failed meta def name_of_mmexpr : mmexpr β†’ tactic name | (sym "LeanNameAnonymous") := return $ name.anonymous | (app (sym "LeanNameMkString") [mstr s, m]) := do n ← name_of_mmexpr m, return $ name.mk_string s n | (app (sym "LeanNameMkNum") [mint i, m]) := do n ← name_of_mmexpr m, return $ name.mk_numeral (unsigned_of_int i) n | _ := failed meta def binder_info_of_mmexpr : mmexpr β†’ tactic binder_info | (sym "BinderInfoDefault") := return $ binder_info.default | (sym "BinderInfoImplicit") := return $ binder_info.implicit | (sym "BinderInfoStrictImplicit") := return $ binder_info.strict_implicit | (sym "BinderInfoInstImplicit") := return $ binder_info.inst_implicit | (sym "BinderInfoOther") := return $ binder_info.aux_decl | _ := failed end unreflect section transl_expr_instances @[app_to_expr_keyed] meta def mmexpr_var_to_expr : app_trans_expr_keyed_rule := ⟨"LeanVar", Ξ» _ args, match args with | [mint i] := return $ var (i.nat_abs) | _ := failed end⟩ @[app_to_expr_keyed] meta def mmexpr_sort_to_expr : app_trans_expr_keyed_rule := ⟨"LeanSort", Ξ» _ args, match args with | [m] := do lvl ← level_of_mmexpr m, return $ sort lvl | _ := failed end⟩ @[app_to_expr_keyed] meta def mmexpr_const_to_expr : app_trans_expr_keyed_rule := ⟨"LeanConst", Ξ» _ args, match args with | [nm, lvls] := do nm' ← name_of_mmexpr nm, lvls' ← level_list_of_mmexpr lvls, return $ const nm' lvls' | _ := failed end⟩ @[app_to_expr_keyed] meta def mmexpr_mvar_to_expr : app_trans_expr_keyed_rule := ⟨"LeanMetaVar", Ξ» env args, match args with | [nm, tp] := do nm' ← name_of_mmexpr nm, tp' ← expr_of_mmexpr env tp, return $ mvar nm' `workaround tp' | _ := failed end⟩ @[app_to_expr_keyed] meta def mmexpr_local_to_expr : app_trans_expr_keyed_rule := ⟨"LeanLocal", Ξ» env args, match args with | [nm, ppnm, bi, tp] := do nm' ← name_of_mmexpr nm, ppnm' ← name_of_mmexpr ppnm, bi' ← binder_info_of_mmexpr bi, tp' ← expr_of_mmexpr env tp, return $ expr.local_const nm' ppnm' bi' tp' | _ := failed end⟩ @[app_to_expr_keyed] meta def mmexpr_app_to_expr : app_trans_expr_keyed_rule := ⟨"LeanApp", Ξ» env args, match args with | [hd, bd] := do hd' ← expr_of_mmexpr env hd, bd' ← expr_of_mmexpr env bd, return $ expr.app hd' bd' | _ := failed end⟩ @[app_to_expr_keyed] meta def mmexpr_lam_to_expr : app_trans_expr_keyed_rule := ⟨"LeanLambda", Ξ» env args, match args with | [nm, bi, tp, bd] := do nm' ← name_of_mmexpr nm, bi' ← binder_info_of_mmexpr bi, tp' ← expr_of_mmexpr env tp, bd' ← expr_of_mmexpr env bd, return $ lam nm' bi' tp' bd' | _ := failed end⟩ @[app_to_expr_keyed] meta def mmexpr_pi_to_expr : app_trans_expr_keyed_rule := ⟨"LeanPi", Ξ» env args, match args with | [nm, bi, tp, bd] := do nm' ← name_of_mmexpr nm, bi' ← binder_info_of_mmexpr bi, tp' ← expr_of_mmexpr env tp, bd' ← expr_of_mmexpr env bd, return $ lam nm' bi' tp' bd' | _ := failed end⟩ meta def pexpr_fold_op_aux (op : pexpr) : pexpr β†’ list pexpr β†’ pexpr | e [] := e | e (h::t) := pexpr_fold_op_aux ```(%%op %%e %%h) t meta def pexpr_fold_op (dflt op : pexpr) : list pexpr β†’ pexpr | [] := dflt | [h] := h | (h::t) := pexpr_fold_op_aux op h t -- pexpr instances @[app_to_pexpr_keyed] meta def add_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"Plus", Ξ» env args, do args' ← monad.mapm (pexpr_of_mmexpr env) args, return $ pexpr_fold_op ```(0) ```(has_add.add) args'⟩ @[app_to_pexpr_keyed] meta def mul_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"Times", Ξ» env args, do args' ← monad.mapm (pexpr_of_mmexpr env) args, return $ pexpr_fold_op ```(1) ```(has_mul.mul) args'⟩ @[app_to_pexpr_keyed] meta def pow_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"Power", Ξ» env args, do args' ← monad.mapm (pexpr_of_mmexpr env) args, return $ pexpr_fold_op ```(1) ```(has_pow.pow) args'⟩ @[app_to_pexpr_keyed] meta def list_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"List", Ξ» env args, do args' ← monad.mapm (pexpr_of_mmexpr env) args, return $ list.foldr (Ξ» h t, ```(%%h :: %%t)) ```([]) args'⟩ @[app_to_pexpr_keyed] meta def and_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"And", Ξ» env args, do args' ← monad.mapm (pexpr_of_mmexpr env) args, return $ pexpr_fold_op ```(true) ```(and) args'⟩ @[app_to_pexpr_keyed] meta def or_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"Or", Ξ» env args, do args' ← monad.mapm (pexpr_of_mmexpr env) args, return $ pexpr_fold_op ```(false) ```(or) args'⟩ @[app_to_pexpr_keyed] meta def not_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"Not", Ξ» env args, match args with | [t] := do t' ← pexpr_of_mmexpr env t, return ```(Β¬ %%t') | _ := failed end⟩ @[app_to_pexpr_keyed] meta def implies_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"Implies", Ξ» env args, match args with | [h,c] := do h' ← pexpr_of_mmexpr env h, c' ← pexpr_of_mmexpr env c, return $ ```(%%h' β†’ %%c') | _ := failed end⟩ @[app_to_pexpr_keyed] meta def hold_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"Hold", Ξ» env args, match args with | [h] := pexpr_of_mmexpr env h | _ := failed end⟩ private meta def replace_holds : mmexpr β†’ list mmexpr | (app (sym "Hold") l) := l | m := [m] meta def is_hold : mmexpr β†’ bool | (app (sym "Hold") l) := tt | _ := ff /-- F[Hold[a1, ..., an]] is equivalent to F[a1, ..., an]. F[t1, ..., Hold[p, q], ..., tn] is equivalent to F[t1, ..., p, q, ..., tn] -/ @[app_to_pexpr_unkeyed] meta def app_mvar_hold_to_pexpr : app_trans_pexpr_unkeyed_rule | env head [app (sym "Hold") l] := pexpr_of_mmexpr env (app head l) | env head l := if l.any is_hold then let ls := (l.map replace_holds).join in pexpr_of_mmexpr env (app head ls) else failed @[app_to_pexpr_unkeyed] meta def app_inactive_to_pexpr : app_trans_pexpr_unkeyed_rule | env (app (sym "Inactive") [t]) l := pexpr_of_mmexpr env (app t l) | _ _ _ := failed meta def pexpr.to_raw_expr : pexpr β†’ expr | (var n) := var n | (sort l) := sort l | (const nm ls) := const nm ls | (mvar n n' e) := mvar n n' (pexpr.to_raw_expr e) | (local_const nm ppnm bi tp) := local_const nm ppnm bi (pexpr.to_raw_expr tp) | (app f a) := app (pexpr.to_raw_expr f) (pexpr.to_raw_expr a) | (lam nm bi tp bd) := lam nm bi (pexpr.to_raw_expr tp) (pexpr.to_raw_expr bd) | (pi nm bi tp bd) := pi nm bi (pexpr.to_raw_expr tp) (pexpr.to_raw_expr bd) | (elet nm tp df bd) := elet nm (pexpr.to_raw_expr tp) (pexpr.to_raw_expr df) (pexpr.to_raw_expr bd) | (macro md l) := macro md (l.map pexpr.to_raw_expr) meta def pexpr.of_raw_expr : expr β†’ pexpr | (var n) := var n | (sort l) := sort l | (const nm ls) := const nm ls | (mvar n n' e) := mvar n n' (pexpr.of_raw_expr e) | (local_const nm ppnm bi tp) := local_const nm ppnm bi (pexpr.of_raw_expr tp) | (app f a) := app (pexpr.of_raw_expr f) (pexpr.of_raw_expr a) | (lam nm bi tp bd) := lam nm bi (pexpr.of_raw_expr tp) (pexpr.of_raw_expr bd) | (pi nm bi tp bd) := pi nm bi (pexpr.of_raw_expr tp) (pexpr.of_raw_expr bd) | (elet nm tp df bd) := elet nm (pexpr.of_raw_expr tp) (pexpr.of_raw_expr df) (pexpr.of_raw_expr bd) | (macro md l) := macro md (l.map pexpr.of_raw_expr) meta def mk_local_const_placeholder (n : name) : expr := let t := pexpr.mk_placeholder in local_const n n binder_info.default (pexpr.to_raw_expr t) meta def mk_local_const (n : name) (tp : pexpr): expr := local_const n n binder_info.default (pexpr.to_raw_expr tp) private meta def sym_to_lcs_using (env : trans_env) (e : mmexpr) : mmexpr β†’ tactic (string Γ— expr) | (sym s) := do p ← pexpr_of_mmexpr env e, return $ (s, mk_local_const s p) | _ := failed meta def sym_to_lcp : mmexpr β†’ tactic (string Γ— expr) | (sym s) := return $ (s, mk_local_const_placeholder s) | _ := failed meta def mk_lambdas (l : list expr) (b : pexpr) : pexpr := pexpr.of_raw_expr (lambdas l (pexpr.to_raw_expr b)) meta def mk_lambda' (x : expr) (b : pexpr) : pexpr := pexpr.of_raw_expr (lambdas [x] (pexpr.to_raw_expr b)) meta def mk_pis (l : list expr) (b : pexpr) : pexpr := pexpr.of_raw_expr (pis l (pexpr.to_raw_expr b)) meta def mk_pi' (x : expr) (b : pexpr) : pexpr := pexpr.of_raw_expr (pis [x] (pexpr.to_raw_expr b)) @[app_to_pexpr_keyed] meta def function_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"Function", Ξ» env args, match args with | [sym x, bd] := do v ← return $ mk_local_const_placeholder x, bd' ← pexpr_of_mmexpr (env.insert x v) bd, return $ mk_lambda' v bd' | [app (sym "List") l, bd] := do vs ← monad.mapm sym_to_lcp l, bd' ← pexpr_of_mmexpr (rb_map.insert_list env vs) bd, return $ mk_lambdas (list.map prod.snd vs) bd' | _ := failed end⟩ @[app_to_pexpr_keyed] meta def forall_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"ForAll", Ξ» env args, match args with | [sym x, bd] := do v ← return $ mk_local_const_placeholder x, bd' ← pexpr_of_mmexpr (env.insert x v) bd, return $ mk_pi' v bd' | [app (sym "List") l, bd] := do vs ← monad.mapm sym_to_lcp l, bd' ← pexpr_of_mmexpr (rb_map.insert_list env vs) bd, return $ mk_pis (list.map prod.snd vs) bd' | [sym x, t, bd] := do v ← return $ mk_local_const_placeholder x, bd' ← pexpr_of_mmexpr (env.insert x v) (app (sym "Implies") [t,bd]), return $ mk_pi' v bd' | _ := failed end⟩ @[app_to_pexpr_keyed] meta def forall_typed_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"ForAllTyped", Ξ» env args, match args with | [sym x, t, bd] := do (n, pe) ← sym_to_lcs_using env t (sym x), bd' ← pexpr_of_mmexpr (env.insert n pe) bd, return $ mk_pi' pe bd' | [app (sym "List") l, t, bd] := do vs ← monad.mapm (sym_to_lcs_using env t) l, bd' ← pexpr_of_mmexpr (rb_map.insert_list env vs) bd, return $ mk_pis (vs.map prod.snd) bd' | _ := failed end⟩ @[app_to_pexpr_keyed] meta def exists_to_pexpr : app_trans_pexpr_keyed_rule := ⟨"Exists", Ξ» env args, match args with | [sym x, bd] := do v ← return $ mk_local_const_placeholder x, bd' ← pexpr_of_mmexpr (env.insert x v) bd, lm ← return $ mk_lambda' v bd', return ``(Exists %%lm) | [app (sym "List") [], bd] := pexpr_of_mmexpr env bd | [app (sym "List") (h::t), bd] := pexpr_of_mmexpr env (app (sym "Exists") [h, app (sym "Exists") [app (sym "List") t, bd]]) | _ := failed end⟩ @[sym_to_pexpr] meta def type_to_pexpr : sym_trans_pexpr_rule := ⟨"Type", ```(Type)⟩ @[sym_to_pexpr] meta def prop_to_pexpr : sym_trans_pexpr_rule := ⟨"Prop", ```(Prop)⟩ @[sym_to_pexpr] meta def inter_to_pexpr : sym_trans_pexpr_rule := ⟨"SetInter", ```(has_inter.inter)⟩ @[sym_to_pexpr] meta def union_to_pexpr : sym_trans_pexpr_rule := ⟨"SetUnion", ```(has_union.union)⟩ @[sym_to_pexpr] meta def compl_to_pexpr : sym_trans_pexpr_rule := ⟨"SetCompl", ```(has_neg.neg)⟩ @[sym_to_pexpr] meta def empty_to_pexpr : sym_trans_pexpr_rule := ⟨"EmptySet", ```(βˆ…)⟩ @[sym_to_pexpr] meta def rat_to_pexpr : sym_trans_pexpr_rule := ⟨"Rational", ```(has_div.div)⟩ @[sym_to_pexpr] meta def eq_to_pexpr : sym_trans_pexpr_rule := ⟨"Equal", ```(eq)⟩ @[sym_to_expr] meta def true_to_expr : sym_trans_expr_rule := ⟨"True", `(true)⟩ @[sym_to_expr] meta def false_to_expr : sym_trans_expr_rule := ⟨"False", `(false)⟩ @[sym_to_pexpr] meta def less_to_pexpr : mathematica.sym_trans_pexpr_rule := ⟨"Less", ``(has_lt.lt)⟩ @[sym_to_pexpr] meta def greater_to_pexpr : mathematica.sym_trans_pexpr_rule := ⟨"Greater", ``(gt)⟩ @[sym_to_pexpr] meta def lesseq_to_pexpr : mathematica.sym_trans_pexpr_rule := ⟨"LessEqual", ``(has_le.le)⟩ @[sym_to_pexpr] meta def greatereq_to_pexpr : mathematica.sym_trans_pexpr_rule := ⟨"GreaterEqual", ``(ge)⟩ end transl_expr_instances end mathematica -- user-facing tactics namespace tactic namespace mathematica open mathematica meta def mk_get_cmd (path : string) : tactic string := do s ← extras_path, -- return $ "Get[\"" ++ path ++ "\",Path->{DirectoryFormat[\""++ s ++"\"]}];" return $ "Get[\"" ++ path ++ "\",Path->{DirectoryFormat[\""++ s ++"\"]}];" /-- load_file path will load the file found at path into Mathematica. The declarations will persist until the kernel is restarted. -/ meta def load_file (path : string) : tactic unit := do s ← mk_get_cmd path, execute_global s >> return () /-- run_command_on cmd e reflects e into Mathematica syntax, applies cmd to this reflection, evaluates this in Mathematica, and attempts to translate the result to a pexpr. -/ meta def run_command_on (cmd : string β†’ string) (e : expr) : tactic pexpr := do rval ← execute_and_eval $ cmd $ form_of_expr e, --rval' ← eval_expr mmexpr rval, pexpr_of_mmexpr trans_env.empty rval /-- run_command_on_using cmd e path reflects e into Mathematica syntax, applies cmd to this reflection, evaluates this in Mathematica after importing the file at path, and attempts to translate the result to a pexpr. -/ meta def run_command_on_using (cmd : string β†’ string) (e : expr) (path : string) : tactic pexpr := do p ← escape_slash <$> mk_get_cmd path, run_command_on (Ξ» s, p ++ cmd s) e meta def run_command_on_2 (cmd : string β†’ string β†’ string) (e1 e2 : expr) : tactic pexpr := do rval ← execute_and_eval $ cmd (form_of_expr e1) (form_of_expr e2), --rval' ← eval_expr mmexpr rval, pexpr_of_mmexpr trans_env.empty rval /-- run_command_on_2_using cmd e1 e2 reflects e1 and e2 into Mathematica syntax, applies cmd to these reflections, evaluates this in Mathematica after importing the file at path, and attempts to translate the result to a pexpr. -/ meta def run_command_on_2_using (cmd : string β†’ string β†’ string) (e1 e2 : expr) (path : string) : tactic pexpr := do p ← escape_slash <$> mk_get_cmd path, run_command_on_2 (Ξ» s1 s2, p ++ cmd s1 s2) e1 e2 private def sep_string : list string β†’ string | [] := "" | [s] := s | (h::t) := h ++ ", " ++ sep_string t /-- run_command_on_list cmd l reflects each element of l into Mathematica syntax, applies cmd to a Mathematica list of these reflections, evaluates this in Mathematica, and attempts to translate the result to a pexpr. -/ meta def run_command_on_list (cmd : string β†’ string) (l : list expr) : tactic pexpr := let lvs := "{" ++ (sep_string $ l.map form_of_expr) ++ "}" in do rval ← execute_and_eval $ cmd lvs, --rval' ← eval_expr mmexpr rval, pexpr_of_mmexpr trans_env.empty rval meta def run_command_on_list_using (cmd : string β†’ string) (l : list expr) (path : string) : tactic pexpr := let lvs := "{" ++ (sep_string $ l.map form_of_expr) ++ "}" in do p ← mk_get_cmd path, rval ← execute_and_eval $ p ++ cmd lvs, --rval' ← eval_expr mmexpr rval, pexpr_of_mmexpr trans_env.empty rval end mathematica end tactic
d1499299973e0c87841ceee804fe18f9f0225ef5
737dc4b96c97368cb66b925eeea3ab633ec3d702
/src/Lean/Meta/Tactic/SplitIf.lean
1f8d13dee2339d4f6f93592721a346d9eaca5861
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,483
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.LazyInitExtension import Lean.Meta.Tactic.Cases import Lean.Meta.Tactic.Simp.Main namespace Lean.Meta namespace SplitIf builtin_initialize ext : LazyInitExtension MetaM Simp.Context ← registerLazyInitExtension do let mut s : SimpLemmas := {} s ← s.addConst ``if_pos s ← s.addConst ``if_neg s ← s.addConst ``dif_pos s ← s.addConst ``dif_neg return { simpLemmas := s congrLemmas := (← getCongrLemmas) config.zeta := false config.beta := false config.eta := false config.iota := false config.proj := false config.decide := false } /-- Default `Simp.Context` for `simpIf` methods. It contains all congruence lemmas, but just the rewriting rules for reducing `if` expressions. -/ def getSimpContext : MetaM Simp.Context := ext.get /-- Default `discharge?` function for `simpIf` methods. It only uses hypotheses from the local context. It is effective after a case-split. -/ def discharge? (useDecide := false) : Simp.Discharge := fun prop => do let prop ← instantiateMVars prop trace[Meta.Tactic.splitIf] "discharge? {prop}, {prop.notNot?}" if useDecide then let prop ← instantiateMVars prop if !prop.hasFVar && !prop.hasMVar then let d ← mkDecide prop let r ← withDefault <| whnf d if r.isConstOf ``true then return some <| mkApp3 (mkConst ``of_decide_eq_true) prop d.appArg! (← mkEqRefl (mkConst ``true)) (← getLCtx).findDeclRevM? fun localDecl => do if localDecl.isAuxDecl then return none else if (← isDefEq prop localDecl.type) then return some localDecl.toExpr else match prop.notNot? with | none => return none | some arg => if (← isDefEq arg localDecl.type) then return some (mkApp2 (mkConst ``not_not_intro) arg localDecl.toExpr) else return none /-- Return the condition of an `if` expression to case split. -/ partial def findIfToSplit? (e : Expr) : Option Expr := if let some iteApp := e.find? fun e => !e.hasLooseBVars && (e.isIte || e.isDIte) then let cond := iteApp.getArg! 1 5 -- Try to find a nested `if` in `cond` findIfToSplit? cond |>.getD cond else none def splitIfAt? (mvarId : MVarId) (e : Expr) (hName? : Option Name) : MetaM (Option (ByCasesSubgoal Γ— ByCasesSubgoal)) := do if let some cond := findIfToSplit? e then let hName ← match hName? with | none => mkFreshUserName `h | some hName => pure hName trace[Meta.Tactic.splitIf] "splitting on {cond}" return some (← byCases mvarId cond hName) else return none end SplitIf open SplitIf def simpIfTarget (mvarId : MVarId) (useDecide := false) : MetaM MVarId := do let mut ctx ← getSimpContext if let some mvarId' ← simpTarget mvarId ctx (discharge? useDecide) then return mvarId' else unreachable! def simpIfLocalDecl (mvarId : MVarId) (fvarId : FVarId) : MetaM MVarId := do let mut ctx ← getSimpContext if let some (_, mvarId') ← simpLocalDecl mvarId fvarId ctx discharge? then return mvarId' else unreachable! def splitIfTarget? (mvarId : MVarId) (hName? : Option Name := none) : MetaM (Option (ByCasesSubgoal Γ— ByCasesSubgoal)) := commitWhenSome? do if let some (s₁, sβ‚‚) ← splitIfAt? mvarId (← getMVarType mvarId) hName? then let mvarId₁ ← simpIfTarget s₁.mvarId let mvarIdβ‚‚ ← simpIfTarget sβ‚‚.mvarId if s₁.mvarId == mvarId₁ && sβ‚‚.mvarId == mvarIdβ‚‚ then return none else return some ({ s₁ with mvarId := mvarId₁ }, { sβ‚‚ with mvarId := mvarIdβ‚‚ }) else return none def splitIfLocalDecl? (mvarId : MVarId) (fvarId : FVarId) (hName? : Option Name := none) : MetaM (Option (MVarId Γ— MVarId)) := commitWhenSome? do withMVarContext mvarId do if let some (s₁, sβ‚‚) ← splitIfAt? mvarId (← inferType (mkFVar fvarId)) hName? then let mvarId₁ ← simpIfLocalDecl s₁.mvarId fvarId let mvarIdβ‚‚ ← simpIfLocalDecl sβ‚‚.mvarId fvarId if s₁.mvarId == mvarId₁ && sβ‚‚.mvarId == mvarIdβ‚‚ then return none else return some (mvarId₁, mvarIdβ‚‚) else return none builtin_initialize registerTraceClass `Meta.Tactic.splitIf end Lean.Meta
eb601c1a8559dbbb83e11494bfc581274cc1b1b5
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/linear_algebra/matrix/is_diag.lean
9cdc5645eede16aecca3f0a4a67be730c807799b
[ "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,232
lean
/- Copyright (c) 2021 Lu-Ming Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Lu-Ming Zhang -/ import linear_algebra.matrix.symmetric import linear_algebra.matrix.orthogonal import data.matrix.kronecker /-! # Diagonal matrices This file contains the definition and basic results about diagonal matrices. ## Main results - `matrix.is_diag`: a proposition that states a given square matrix `A` is diagonal. ## Tags diag, diagonal, matrix -/ namespace matrix variables {Ξ± Ξ² R n m : Type*} open function open_locale matrix kronecker /-- `A.is_diag` means square matrix `A` is a diagonal matrix. -/ def is_diag [has_zero Ξ±] (A : matrix n n Ξ±) : Prop := βˆ€ ⦃i j⦄, i β‰  j β†’ A i j = 0 @[simp] lemma is_diag_diagonal [has_zero Ξ±] [decidable_eq n] (d : n β†’ Ξ±) : (diagonal d).is_diag := Ξ» i j, matrix.diagonal_apply_ne /-- Diagonal matrices are generated by `matrix.diagonal`. -/ lemma is_diag.exists_diagonal [has_zero Ξ±] [decidable_eq n] {A : matrix n n Ξ±} (h : A.is_diag) : βˆƒ d, diagonal d = A := begin refine ⟨λ i, A i i, ext $ Ξ» i j, _⟩, obtain rfl | hij := decidable.eq_or_ne i j, { rw diagonal_apply_eq }, { rw [diagonal_apply_ne hij, h hij] }, end /-- `matrix.is_diag.exists_diagonal` as an iff. -/ lemma is_diag_iff_exists_diagonal [has_zero Ξ±] [decidable_eq n] (A : matrix n n Ξ±) : A.is_diag ↔ (βˆƒ d, diagonal d = A) := ⟨is_diag.exists_diagonal, Ξ» ⟨d, hd⟩, hd β–Έ is_diag_diagonal d⟩ /-- Every matrix indexed by a subsingleton is diagonal. -/ lemma is_diag_of_subsingleton [has_zero Ξ±] [subsingleton n] (A : matrix n n Ξ±) : A.is_diag := Ξ» i j h, (h $ subsingleton.elim i j).elim /-- Every zero matrix is diagonal. -/ @[simp] lemma is_diag_zero [has_zero Ξ±] : (0 : matrix n n Ξ±).is_diag := Ξ» i j h, rfl /-- Every identity matrix is diagonal. -/ @[simp] lemma is_diag_one [decidable_eq n] [has_zero Ξ±] [has_one Ξ±] : (1 : matrix n n Ξ±).is_diag := Ξ» i j, one_apply_ne lemma is_diag.map [has_zero Ξ±] [has_zero Ξ²] {A : matrix n n Ξ±} (ha : A.is_diag) {f : Ξ± β†’ Ξ²} (hf : f 0 = 0) : (A.map f).is_diag := by { intros i j h, simp [ha h, hf] } lemma is_diag.neg [add_group Ξ±] {A : matrix n n Ξ±} (ha : A.is_diag) : (-A).is_diag := by { intros i j h, simp [ha h] } @[simp] lemma is_diag_neg_iff [add_group Ξ±] {A : matrix n n Ξ±} : (-A).is_diag ↔ A.is_diag := ⟨ Ξ» ha i j h, neg_eq_zero.1 (ha h), is_diag.neg ⟩ lemma is_diag.add [add_zero_class Ξ±] {A B : matrix n n Ξ±} (ha : A.is_diag) (hb : B.is_diag) : (A + B).is_diag := by { intros i j h, simp [ha h, hb h] } lemma is_diag.sub [add_group Ξ±] {A B : matrix n n Ξ±} (ha : A.is_diag) (hb : B.is_diag) : (A - B).is_diag := by { intros i j h, simp [ha h, hb h] } lemma is_diag.smul [monoid R] [add_monoid Ξ±] [distrib_mul_action R Ξ±] (k : R) {A : matrix n n Ξ±} (ha : A.is_diag) : (k β€’ A).is_diag := by { intros i j h, simp [ha h] } @[simp] lemma is_diag_smul_one (n) [semiring Ξ±] [decidable_eq n] (k : Ξ±) : (k β€’ (1 : matrix n n Ξ±)).is_diag := is_diag_one.smul k lemma is_diag.transpose [has_zero Ξ±] {A : matrix n n Ξ±} (ha : A.is_diag) : Aα΅€.is_diag := Ξ» i j h, ha h.symm @[simp] lemma is_diag_transpose_iff [has_zero Ξ±] {A : matrix n n Ξ±} : Aα΅€.is_diag ↔ A.is_diag := ⟨ is_diag.transpose, is_diag.transpose ⟩ lemma is_diag.conj_transpose [semiring Ξ±] [star_ring Ξ±] {A : matrix n n Ξ±} (ha : A.is_diag) : Aα΄΄.is_diag := ha.transpose.map (star_zero _) @[simp] lemma is_diag_conj_transpose_iff [semiring Ξ±] [star_ring Ξ±] {A : matrix n n Ξ±} : Aα΄΄.is_diag ↔ A.is_diag := ⟨ Ξ» ha, by {convert ha.conj_transpose, simp}, is_diag.conj_transpose ⟩ lemma is_diag.minor [has_zero Ξ±] {A : matrix n n Ξ±} (ha : A.is_diag) {f : m β†’ n} (hf : injective f) : (A.minor f f).is_diag := Ξ» i j h, ha (hf.ne h) /-- `(A βŠ— B).is_diag` if both `A` and `B` are diagonal. -/ lemma is_diag.kronecker [mul_zero_class Ξ±] {A : matrix m m Ξ±} {B : matrix n n Ξ±} (hA : A.is_diag) (hB : B.is_diag) : (A βŠ—β‚– B).is_diag := begin rintros ⟨a, b⟩ ⟨c, d⟩ h, simp only [prod.mk.inj_iff, ne.def, not_and_distrib] at h, cases h with hac hbd, { simp [hA hac] }, { simp [hB hbd] }, end lemma is_diag.is_symm [has_zero Ξ±] {A : matrix n n Ξ±} (h : A.is_diag) : A.is_symm := begin ext i j, by_cases g : i = j, { rw g }, simp [h g, h (ne.symm g)], end /-- The block matrix `A.from_blocks 0 0 D` is diagonal if `A` and `D` are diagonal. -/ lemma is_diag.from_blocks [has_zero Ξ±] {A : matrix m m Ξ±} {D : matrix n n Ξ±} (ha : A.is_diag) (hd : D.is_diag) : (A.from_blocks 0 0 D).is_diag := begin rintros (i | i) (j | j) hij, { exact ha (ne_of_apply_ne _ hij) }, { refl }, { refl }, { exact hd (ne_of_apply_ne _ hij) }, end /-- This is the `iff` version of `matrix.is_diag.from_blocks`. -/ lemma is_diag_from_blocks_iff [has_zero Ξ±] {A : matrix m m Ξ±} {B : matrix m n Ξ±} {C : matrix n m Ξ±} {D : matrix n n Ξ±} : (A.from_blocks B C D).is_diag ↔ A.is_diag ∧ B = 0 ∧ C = 0 ∧ D.is_diag := begin split, { intros h, refine ⟨λ i j hij, _, ext $ Ξ» i j, _, ext $ Ξ» i j, _, Ξ» i j hij, _⟩, { exact h (sum.inl_injective.ne hij), }, { exact h sum.inl_ne_inr, }, { exact h sum.inr_ne_inl, }, { exact h (sum.inr_injective.ne hij), }, }, { rintros ⟨ha, hb, hc, hd⟩, convert is_diag.from_blocks ha hd } end /-- A symmetric block matrix `A.from_blocks B C D` is diagonal if `A` and `D` are diagonal and `B` is `0`. -/ lemma is_diag.from_blocks_of_is_symm [has_zero Ξ±] {A : matrix m m Ξ±} {C : matrix n m Ξ±} {D : matrix n n Ξ±} (h : (A.from_blocks 0 C D).is_symm) (ha : A.is_diag) (hd : D.is_diag) : (A.from_blocks 0 C D).is_diag := begin rw ←(is_symm_from_blocks_iff.1 h).2.1, exact ha.from_blocks hd, end lemma mul_transpose_self_is_diag_iff_has_orthogonal_rows [fintype n] [has_mul Ξ±] [add_comm_monoid Ξ±] {A : matrix m n Ξ±} : (A ⬝ Aα΅€).is_diag ↔ A.has_orthogonal_rows := iff.rfl lemma transpose_mul_self_is_diag_iff_has_orthogonal_cols [fintype m] [has_mul Ξ±] [add_comm_monoid Ξ±] {A : matrix m n Ξ±} : (Aα΅€ ⬝ A).is_diag ↔ A.has_orthogonal_cols := iff.rfl end matrix
e5ea9964eb00f5ad3c48818c8de139dcea242e36
43390109ab88557e6090f3245c47479c123ee500
/src/chris_hughes_various/zmod.lean
e39bfa2e0edc658a7ad030ac44ae58e8c8ab6ad6
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,522
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Chris Hughes -/ import data.int.modeq data.fintype data.nat.prime data.nat.gcd lemma int.coe_nat_ne_zero_iff_pos {n : β„•} : (n : β„€) β‰  0 ↔ 0 < n := ⟨λ h, nat.pos_of_ne_zero $ int.coe_nat_ne_zero.1 h, Ξ» h, ne.symm $ ne_of_lt $ int.coe_nat_lt.2 h⟩ lemma int.coe_nat_nonneg (n : β„•) : (0 : β„€) ≀ n := int.coe_nat_le.2 $ nat.zero_le _ class pos_nat (n : β„•) := (pos : 0 < n) attribute [class] nat.prime instance pos_nat_of_prime (p : β„•) [hp : nat.prime p] : pos_nat p := ⟨hp.pos⟩ open nat nat.modeq int def zmod := fin namespace zmod instance {n : β„•} : has_neg (zmod n) := ⟨λ a, ⟨nat_mod (-(a.1 : β„€)) n, begin cases n with n, { exact (nat.not_lt_zero _ a.2).elim }, { have h : (nat.succ n : β„€) β‰  0 := dec_trivial, rw [← int.coe_nat_lt, nat_mod, to_nat_of_nonneg (int.mod_nonneg _ h)], exact int.mod_lt _ h } end⟩⟩ instance (n : β„•) : add_comm_semigroup (zmod n) := { add := @has_add.add (fin n) fin.has_add, add_assoc := Ξ» ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (show ((a + b) % n + c) ≑ (a + (b + c) % n) [MOD n], from calc ((a + b) % n + c) ≑ a + b + c [MOD n] : modeq_add (nat.mod_mod _ _) rfl ... ≑ a + (b + c) [MOD n] : by rw add_assoc ... ≑ (a + (b + c) % n) [MOD n] : modeq_add rfl (nat.mod_mod _ _).symm), add_comm := Ξ» ⟨a, _⟩ ⟨b, _⟩, fin.eq_of_veq (show (a + b) % n = (b + a) % n, by rw add_comm) } instance (n : β„•) : comm_semigroup (zmod n) := { mul := @has_mul.mul (fin n) fin.has_mul, mul_assoc := Ξ» ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (calc ((a * b) % n * c) ≑ a * b * c [MOD n] : modeq_mul (nat.mod_mod _ _) rfl ... ≑ a * (b * c) [MOD n] : by rw mul_assoc ... ≑ a * (b * c % n) [MOD n] : modeq_mul rfl (nat.mod_mod _ _).symm), mul_comm := Ξ» ⟨a, _⟩ ⟨b, _⟩, fin.eq_of_veq (show (a * b) % n = (b * a) % n, by rw mul_comm) } instance (n : β„•) [h0 : pos_nat n] : has_one (zmod n) := ⟨⟨1 % n, nat.mod_lt _ h0.pos⟩⟩ instance (n : β„•) [h0 : pos_nat n] : has_zero (zmod n) := ⟨⟨0, h0.pos⟩⟩ private lemma one_mul_aux (n : β„•) [h0 : pos_nat n] : βˆ€ a : zmod n, (1 : zmod n) * a = a := Ξ» ⟨a, ha⟩, fin.eq_of_veq (show (1 % n * a) % n = a, begin resetI, clear _fun_match, cases n with n, { simp }, { cases n with n, { rw [nat.mod_self, zero_mul]; exact (nat.eq_zero_of_le_zero (le_of_lt_succ ha)).symm }, { have h : 1 < n + 2 := dec_trivial, have ha : a < succ (succ n) := ha, rw [nat.mod_eq_of_lt h, one_mul, nat.mod_eq_of_lt ha] } } end) private lemma left_distrib_aux (n : β„•) : βˆ€ a b c : zmod n, a * (b + c) = a * b + a * c := Ξ» ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (calc a * ((b + c) % n) ≑ a * (b + c) [MOD n] : modeq_mul rfl (nat.mod_mod _ _) ... ≑ a * b + a * c [MOD n] : by rw mul_add ... ≑ (a * b) % n + (a * c) % n [MOD n] : modeq_add (nat.mod_mod _ _).symm (nat.mod_mod _ _).symm) instance (n : β„•) : distrib (zmod n) := { left_distrib := left_distrib_aux n, right_distrib := Ξ» a b c, by rw [mul_comm, left_distrib_aux, mul_comm _ b, mul_comm]; refl, ..zmod.add_comm_semigroup n, ..zmod.comm_semigroup n } instance (n : β„•) [h0 : pos_nat n] : comm_ring (zmod n) := { zero_add := Ξ» ⟨a, ha⟩, fin.eq_of_veq (show (0 + a) % n = a, by rw zero_add; exact nat.mod_eq_of_lt ha), add_zero := Ξ» ⟨a, ha⟩, fin.eq_of_veq (nat.mod_eq_of_lt ha), add_left_neg := Ξ» ⟨a, ha⟩, fin.eq_of_veq (show (((-a : β„€) % n).to_nat + a) % n = 0, from int.coe_nat_inj begin have hn : (n : β„€) β‰  0 := (ne_of_lt (int.coe_nat_lt.2 h0.pos)).symm, rw [int.coe_nat_mod, int.coe_nat_add, to_nat_of_nonneg (int.mod_nonneg _ hn), add_comm], simp, end), one_mul := one_mul_aux n, mul_one := Ξ» a, by rw mul_comm; exact one_mul_aux n a, ..zmod.has_zero n, ..zmod.has_one n, ..zmod.has_neg, ..zmod.distrib n, ..zmod.add_comm_semigroup n, ..zmod.comm_semigroup n } @[simp] lemma val_zero (n : β„•) [pos_nat n] : (0 : zmod n).val = 0 := rfl lemma add_val {n : β„•} : βˆ€ a b : zmod n, (a + b).val = (a.val + b.val) % n | ⟨_, _⟩ ⟨_, _⟩ := rfl lemma mul_val {n : β„•} : βˆ€ a b : zmod n, (a * b).val = (a.val * b.val) % n | ⟨_, _⟩ ⟨_, _⟩ := rfl lemma cast_val_nat {n : β„•} [pos_nat n] (a : β„•) : (a : zmod n).val = a % n := begin induction a with a ih, { rw [nat.zero_mod]; refl }, { rw [succ_eq_add_one, nat.cast_add, add_val, ih], show (a % n + ((0 + (1 % n)) % n)) % n = (a + 1) % n, rw [zero_add, nat.mod_mod], exact nat.modeq.modeq_add (nat.mod_mod a n) (nat.mod_mod 1 n) } end lemma mk_eq_cast {a n : β„•} (h : a < n) [pos_nat n] : (⟨a, h⟩ : zmod n) = (a : zmod n) := fin.eq_of_veq (by rw [cast_val_nat, nat.mod_eq_of_lt h]) @[simp] lemma cast_self_eq_zero (n : β„•) [pos_nat n] : (n : zmod n) = 0 := fin.eq_of_veq (show (n : zmod n).val = 0, by simp [cast_val_nat]) lemma cast_val_of_lt {a n : β„•} (h : a < n) [pos_nat n] : (a : zmod n).val = a := by rw [cast_val_nat, nat.mod_eq_of_lt h] @[simp] lemma cast_nat_mod (n : β„•) [pos_nat n] (a : β„•) : ((a % n : β„•) : zmod n) = a := by conv {to_rhs, rw ← nat.mod_add_div a n}; simp @[simp] lemma cast_int_mod (n : β„•) [pos_nat n] (a : β„€) : ((a % n : β„€) : zmod n) = a := by conv {to_rhs, rw ← int.mod_add_div a n}; simp lemma cast_val_int (n : β„•) [h0 : pos_nat n] (a : β„€) : (a : zmod n).val = (a % n).nat_abs := have h : nat_abs (a % n) < n := int.coe_nat_lt.1 begin rw [nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 h0.pos))], conv {to_rhs, rw ← abs_of_nonneg (int.coe_nat_nonneg n)}, exact int.mod_lt _ (int.coe_nat_ne_zero_iff_pos.2 h0.pos) end, int.coe_nat_inj $ by conv {to_lhs, rw [← cast_int_mod n a, ← nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 h0.pos)), int.cast_coe_nat, cast_val_of_lt h] } instance zmod_one.subsingleton : subsingleton (zmod 1) := ⟨λ a b, fin.eq_of_veq (by rw [eq_zero_of_le_zero (le_of_lt_succ a.2), eq_zero_of_le_zero (le_of_lt_succ b.2)])⟩ instance zmod_zero.subsingleton : subsingleton (zmod 0) := ⟨λ a, (nat.not_lt_zero _ a.2).elim⟩ lemma eq_iff_modeq_nat {n : β„•} [pos_nat n] {a b : β„•} : (a : zmod n) = b ↔ a ≑ b [MOD n] := ⟨λ h, by have := fin.veq_of_eq h; rwa [cast_val_nat, cast_val_nat] at this, Ξ» h, fin.eq_of_veq $ by rwa [cast_val_nat, cast_val_nat]⟩ lemma eq_iff_modeq_int {n : β„•} [h0 : pos_nat n] {a b : β„€} : (a : zmod n) = b ↔ a ≑ b [ZMOD n] := ⟨λ h, by have := fin.veq_of_eq h; rwa [cast_val_int, cast_val_int, ← int.coe_nat_eq_coe_nat_iff, nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 h0.pos)), nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 h0.pos))] at this, Ξ» h : a % n = b % n, by rw [← cast_int_mod n a, ← cast_int_mod n b, h]⟩ instance (n : β„•) : fintype (zmod n) := fin.fintype _ lemma card_zmod : βˆ€ n, fintype.card (zmod n) = n := fintype.card_fin instance (n : β„•) [pos_nat n] : has_inv (zmod n) := ⟨λ a, gcd_a a.1 n⟩ lemma gcd_a_modeq (a b : β„•) : (a : β„€) * gcd_a a b ≑ nat.gcd a b [ZMOD b] := by rw [← add_zero ((a : β„€) * _), gcd_eq_gcd_ab]; exact int.modeq.modeq_add rfl (int.modeq.modeq_zero_iff.2 (dvd_mul_right _ _)).symm lemma mul_inv_eq_gcd (n a : β„•) [pos_nat n]: (a : zmod n) * a⁻¹ = nat.gcd a n := by rw [← int.cast_coe_nat (nat.gcd _ _), nat.gcd_comm, nat.gcd_rec, ← eq_iff_modeq_int.2 (gcd_a_modeq _ _)]; simp [has_inv.inv, cast_val_nat] private lemma mul_inv_cancel_aux {p : β„•} [hp : prime p] : βˆ€ a : zmod p, a β‰  0 β†’ a * a⁻¹ = 1 := Ξ» ⟨a, hap⟩ ha0, begin rw [mk_eq_cast, ne.def, ← nat.cast_zero, eq_iff_modeq_nat, modeq_zero_iff] at ha0, have : nat.gcd p a = 1 := (prime.coprime_iff_not_dvd hp).2 ha0, rw [mk_eq_cast hap, mul_inv_eq_gcd, gcd_comm, this, nat.cast_one], end instance {p : β„•} [hp : prime p] : discrete_field (zmod p) := { zero_ne_one := fin.ne_of_vne $ show 0 β‰  1 % p, by rw nat.mod_eq_of_lt hp.gt_one; exact zero_ne_one, mul_inv_cancel := mul_inv_cancel_aux, inv_mul_cancel := Ξ» a, by rw mul_comm; exact mul_inv_cancel_aux _, has_decidable_eq := by apply_instance, inv_zero := show (gcd_a 0 p : zmod p) = 0, by unfold gcd_a xgcd xgcd_aux; refl, ..zmod.comm_ring p, ..zmod.has_inv p } end zmod
4002a64c5e2e137923c521cf6e28d726fa90c821
94e33a31faa76775069b071adea97e86e218a8ee
/src/measure_theory/covering/vitali.lean
a618a36cee83bce2a02e2345a80887751834e6f9
[ "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
29,568
lean
/- Copyright (c) 2021 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import topology.metric_space.basic import measure_theory.constructions.borel_space import measure_theory.covering.vitali_family /-! # Vitali covering theorems The topological Vitali covering theorem, in its most classical version, states the following. Consider a family of balls `(B (x_i, r_i))_{i ∈ I}` in a metric space, with uniformly bounded radii. Then one can extract a disjoint subfamily indexed by `J βŠ† I`, such that any `B (x_i, r_i)` is included in a ball `B (x_j, 5 r_j)`. We prove this theorem in `vitali.exists_disjoint_subfamily_covering_enlargment_closed_ball`. It is deduced from a more general version, called `vitali.exists_disjoint_subfamily_covering_enlargment`, which applies to any family of sets together with a size function `Ξ΄` (think "radius" or "diameter"). We deduce the measurable Vitali covering theorem. Assume one is given a family `t` of closed sets with nonempty interior, such that each `a ∈ t` is included in a ball `B (x, r)` and covers a definite proportion of the ball `B (x, 6 r)` for a given measure `ΞΌ` (think of the situation where `ΞΌ` is a doubling measure and `t` is a family of balls). Consider a set `s` at which the family is fine, i.e., every point of `s` belongs to arbitrarily small elements of `t`. Then one can extract from `t` a disjoint subfamily that covers almost all `s`. It is proved in `vitali.exists_disjoint_covering_ae`. A way to restate this theorem is to say that the set of closed sets `a` with nonempty interior covering a fixed proportion `1/C` of the ball `closed_ball x (3 * diam a)` forms a Vitali family. This version is given in `vitali.vitali_family`. -/ variables {Ξ± : Type*} open set metric measure_theory topological_space filter open_locale nnreal classical ennreal topological_space namespace vitali /-- Vitali covering theorem: given a set `t` of subsets of a type, one may extract a disjoint subfamily `u` such that the `Ο„`-enlargment of this family covers all elements of `t`, where `Ο„ > 1` is any fixed number. When `t` is a family of balls, the `Ο„`-enlargment of `ball x r` is `ball x ((1+2Ο„) r)`. In general, it is expressed in terms of a function `Ξ΄` (think "radius" or "diameter"), positive and bounded on all elements of `t`. The condition is that every element `a` of `t` should intersect an element `b` of `u` of size larger than that of `a` up to `Ο„`, i.e., `Ξ΄ b β‰₯ Ξ΄ a / Ο„`. -/ theorem exists_disjoint_subfamily_covering_enlargment (t : set (set Ξ±)) (Ξ΄ : set Ξ± β†’ ℝ) (Ο„ : ℝ) (hΟ„ : 1 < Ο„) (Ξ΄nonneg : βˆ€ a ∈ t, 0 ≀ Ξ΄ a) (R : ℝ) (Ξ΄le : βˆ€ a ∈ t, Ξ΄ a ≀ R) (hne : βˆ€ a ∈ t, set.nonempty a) : βˆƒ u βŠ† t, u.pairwise_disjoint id ∧ βˆ€ a ∈ t, βˆƒ b ∈ u, set.nonempty (a ∩ b) ∧ Ξ΄ a ≀ Ο„ * Ξ΄ b := begin /- The proof could be formulated as a transfinite induction. First pick an element of `t` with `Ξ΄` as large as possible (up to a factor of `Ο„`). Then among the remaining elements not intersecting the already chosen one, pick another element with large `Ξ΄`. Go on forever (transfinitely) until there is nothing left. Instead, we give a direct Zorn-based argument. Consider a maximal family `u` of disjoint sets with the following property: if an element `a` of `t` intersects some element `b` of `u`, then it intersects some `b' ∈ u` with `Ξ΄ b' β‰₯ Ξ΄ a / Ο„`. Such a maximal family exists by Zorn. If this family did not intersect some element `a ∈ t`, then take an element `a' ∈ t` which does not intersect any element of `u`, with `Ξ΄ a'` almost as large as possible. One checks easily that `u βˆͺ {a'}` still has this property, contradicting the maximality. Therefore, `u` intersects all elements of `t`, and by definition it satisfies all the desired properties. -/ let T : set (set (set Ξ±)) := {u | u βŠ† t ∧ u.pairwise_disjoint id ∧ βˆ€ a ∈ t, βˆ€ b ∈ u, set.nonempty (a ∩ b) β†’ βˆƒ c ∈ u, (a ∩ c).nonempty ∧ Ξ΄ a ≀ Ο„ * Ξ΄ c}, -- By Zorn, choose a maximal family in the good set `T` of disjoint families. obtain ⟨u, uT, hu⟩ : βˆƒ u ∈ T, βˆ€ v ∈ T, u βŠ† v β†’ v = u, { refine zorn_subset _ (Ξ» U UT hU, _), refine βŸ¨β‹ƒβ‚€ U, _, Ξ» s hs, subset_sUnion_of_mem hs⟩, simp only [set.sUnion_subset_iff, and_imp, exists_prop, forall_exists_index, mem_sUnion, set.mem_set_of_eq], refine ⟨λ u hu, (UT hu).1, (pairwise_disjoint_sUnion hU.directed_on).2 (Ξ» u hu, (UT hu).2.1), Ξ» a hat b u uU hbu hab, _⟩, obtain ⟨c, cu, ac, hc⟩ : βˆƒ (c : set Ξ±) (H : c ∈ u), (a ∩ c).nonempty ∧ Ξ΄ a ≀ Ο„ * Ξ΄ c := (UT uU).2.2 a hat b hbu hab, exact ⟨c, ⟨u, uU, cu⟩, ac, hc⟩ }, -- the only nontrivial bit is to check that every `a ∈ t` intersects an element `b ∈ u` with -- comparatively large `Ξ΄ b`. Assume this is not the case, then we will contradict the maximality. refine ⟨u, uT.1, uT.2.1, Ξ» a hat, _⟩, contrapose! hu, have a_disj : βˆ€ c ∈ u, disjoint a c, { assume c hc, by_contra, rw not_disjoint_iff_nonempty_inter at h, obtain ⟨d, du, ad, hd⟩ : βˆƒ (d : set Ξ±) (H : d ∈ u), (a ∩ d).nonempty ∧ Ξ΄ a ≀ Ο„ * Ξ΄ d := uT.2.2 a hat c hc h, exact lt_irrefl _ ((hu d du ad).trans_le hd) }, -- Let `A` be all the elements of `t` which do not intersect the family `u`. It is nonempty as it -- contains `a`. We will pick an element `a'` of `A` with `Ξ΄ a'` almost as large as possible. let A := {a' | a' ∈ t ∧ βˆ€ c ∈ u, disjoint a' c}, have Anonempty : A.nonempty := ⟨a, hat, a_disj⟩, let m := Sup (Ξ΄ '' A), have bddA : bdd_above (Ξ΄ '' A), { refine ⟨R, Ξ» x xA, _⟩, rcases (mem_image _ _ _).1 xA with ⟨a', ha', rfl⟩, exact Ξ΄le a' ha'.1 }, obtain ⟨a', a'A, ha'⟩ : βˆƒ a' ∈ A, m / Ο„ ≀ Ξ΄ a', { have : 0 ≀ m := (Ξ΄nonneg a hat).trans (le_cSup bddA (mem_image_of_mem _ ⟨hat, a_disj⟩)), rcases eq_or_lt_of_le this with mzero|mpos, { refine ⟨a, ⟨hat, a_disj⟩, _⟩, simpa only [← mzero, zero_div] using Ξ΄nonneg a hat }, { have I : m / Ο„ < m, { rw div_lt_iff (zero_lt_one.trans hΟ„), conv_lhs { rw ← mul_one m }, exact (mul_lt_mul_left mpos).2 hΟ„ }, rcases exists_lt_of_lt_cSup (nonempty_image_iff.2 Anonempty) I with ⟨x, xA, hx⟩, rcases (mem_image _ _ _).1 xA with ⟨a', ha', rfl⟩, exact ⟨a', ha', hx.le⟩, } }, clear hat hu a_disj a, have a'_ne_u : a' βˆ‰ u := Ξ» H, (hne _ a'A.1).ne_empty (disjoint_self.1 (a'A.2 _ H)), -- we claim that `u βˆͺ {a'}` still belongs to `T`, contradicting the maximality of `u`. refine ⟨insert a' u, ⟨_, _, _⟩, subset_insert _ _, (ne_insert_of_not_mem _ a'_ne_u).symm⟩, -- check that `u βˆͺ {a'}` is made of elements of `t`. { rw insert_subset, exact ⟨a'A.1, uT.1⟩ }, -- check that `u βˆͺ {a'}` is a disjoint family. This follows from the fact that `a'` does not -- intersect `u`. { exact uT.2.1.insert (Ξ» b bu ba', a'A.2 b bu) }, -- check that every element `c` of `t` intersecting `u βˆͺ {a'}` intersects an element of this -- family with large `Ξ΄`. { assume c ct b ba'u hcb, -- if `c` already intersects an element of `u`, then it intersects an element of `u` with -- large `Ξ΄` by the assumption on `u`, and there is nothing left to do. by_cases H : βˆƒ d ∈ u, set.nonempty (c ∩ d), { rcases H with ⟨d, du, hd⟩, rcases uT.2.2 c ct d du hd with ⟨d', d'u, hd'⟩, exact ⟨d', mem_insert_of_mem _ d'u, hd'⟩ }, -- otherwise, `c` belongs to `A`. The element of `u βˆͺ {a'}` that it intersects has to be `a'`. -- moreover, `Ξ΄ c` is smaller than the maximum `m` of `Ξ΄` over `A`, which is `≀ Ξ΄ a' / Ο„` -- thanks to the good choice of `a'`. This is the desired inequality. { push_neg at H, simp only [← not_disjoint_iff_nonempty_inter, not_not] at H, rcases mem_insert_iff.1 ba'u with rfl|H', { refine ⟨b, mem_insert _ _, hcb, _⟩, calc Ξ΄ c ≀ m : le_cSup bddA (mem_image_of_mem _ ⟨ct, H⟩) ... = Ο„ * (m / Ο„) : by { field_simp [(zero_lt_one.trans hΟ„).ne'], ring } ... ≀ Ο„ * Ξ΄ b : mul_le_mul_of_nonneg_left ha' (zero_le_one.trans hΟ„.le) }, { rw ← not_disjoint_iff_nonempty_inter at hcb, exact (hcb (H _ H')).elim } } } end /-- Vitali covering theorem, closed balls version: given a family `t` of closed balls, one can extract a disjoint subfamily `u βŠ† t` so that all balls in `t` are covered by the 5-times dilations of balls in `u`. -/ theorem exists_disjoint_subfamily_covering_enlargment_closed_ball [metric_space Ξ±] (t : set (set Ξ±)) (R : ℝ) (ht : βˆ€ s ∈ t, βˆƒ x r, s = closed_ball x r ∧ r ≀ R) : βˆƒ u βŠ† t, u.pairwise_disjoint id ∧ βˆ€ a ∈ t, βˆƒ x r, closed_ball x r ∈ u ∧ a βŠ† closed_ball x (5 * r) := begin rcases eq_empty_or_nonempty t with rfl|tnonempty, { exact βŸ¨βˆ…, subset.refl _, pairwise_disjoint_empty, by simp⟩ }, haveI : inhabited Ξ±, { choose s hst using tnonempty, choose x r hxr using ht s hst, exact ⟨x⟩ }, -- Exclude the trivial case where `t` is reduced to the empty set. rcases eq_or_ne t {βˆ…} with rfl|t_ne_empty, { refine ⟨{βˆ…}, subset.refl _, _⟩, simp only [true_and, closed_ball_eq_empty, mem_singleton_iff, and_true, empty_subset, forall_eq, pairwise_disjoint_singleton, exists_const], exact ⟨-1, by simp only [right.neg_neg_iff, zero_lt_one]⟩ }, -- The real proof starts now. Since the center or the radius of a ball is not uniquely defined -- in a general metric space, we just choose one for definiteness. choose! x r hxr using ht, have r_nonneg : βˆ€ (a : set Ξ±), a ∈ t β†’ a.nonempty β†’ 0 ≀ r a, { assume a hat a_nonempty, rw (hxr a hat).1 at a_nonempty, simpa only [nonempty_closed_ball] using a_nonempty }, -- The difference with the generic version is that we are not excluding empty sets in our family -- (which would correspond to `r a < 0`). To compensate for this, we apply the generic version -- to the subfamily `t'` made of nonempty sets, and we use `Ξ΄ = r` there. This gives a disjointed -- subfamily `u'`. let t' := {a ∈ t | 0 ≀ r a}, obtain ⟨u', u't', u'_disj, hu'⟩ : βˆƒ u' βŠ† t', u'.pairwise_disjoint id ∧ βˆ€ a ∈ t', βˆƒ b ∈ u', set.nonempty (a ∩ b) ∧ r a ≀ 2 * r b, { refine exists_disjoint_subfamily_covering_enlargment t' r 2 one_lt_two (Ξ» a ha, ha.2) R (Ξ» a ha, (hxr a ha.1).2) (Ξ» a ha, _), rw [(hxr a ha.1).1], simp only [ha.2, nonempty_closed_ball] }, -- this subfamily is nonempty, as we have excluded the situation `t = {βˆ…}`. have u'_nonempty : u'.nonempty, { have : βˆƒ a ∈ t, a β‰  βˆ…, { contrapose! t_ne_empty, apply subset.antisymm, { simpa only using t_ne_empty }, { rcases tnonempty with ⟨a, hat⟩, have := t_ne_empty a hat, simpa only [this, singleton_subset_iff] using hat } }, rcases this with ⟨a, hat, a_nonempty⟩, have ranonneg : 0 ≀ r a := r_nonneg a hat (ne_empty_iff_nonempty.1 a_nonempty), rcases hu' a ⟨hat, ranonneg⟩ with ⟨b, bu', hb⟩, exact ⟨b, bu'⟩ }, -- check that the family `u'` gives the desired disjoint covering. refine ⟨u', Ξ» a ha, (u't' ha).1, u'_disj, Ξ» a hat, _⟩, -- it remains to check that any ball in `t` is contained in the 5-dilation of a ball -- in `u'`. This depends on whether the ball is empty of not. rcases eq_empty_or_nonempty a with rfl|a_nonempty, -- if the ball is empty, use any element of `u'` (since we know that `u'` is nonempty). { rcases u'_nonempty with ⟨b, hb⟩, refine ⟨x b, r b, _, empty_subset _⟩, rwa ← (hxr b (u't' hb).1).1 }, -- if the ball is not empty, it belongs to `t'`. Then it intersects a ball `a'` in `u'` with -- controlled radius, by definition of `u'`. It is straightforward to check that this ball -- satisfies all the desired properties. { have hat' : a ∈ t' := ⟨hat, r_nonneg a hat a_nonempty⟩, obtain ⟨a', a'u', aa', raa'⟩ : (βˆƒ (a' : set Ξ±) (H : a' ∈ u'), (a ∩ a').nonempty ∧ r a ≀ 2 * r a') := hu' a hat', refine ⟨x a', r a', _, _⟩, { convert a'u', exact (hxr a' (u't' a'u').1).1.symm }, { rw (hxr a hat'.1).1 at aa' ⊒, rw (hxr a' (u't' a'u').1).1 at aa', have : dist (x a) (x a') ≀ r a + r a' := dist_le_add_of_nonempty_closed_ball_inter_closed_ball aa', apply closed_ball_subset_closed_ball', linarith } } end /-- The measurable Vitali covering theorem. Assume one is given a family `t` of closed sets with nonempty interior, such that each `a ∈ t` is included in a ball `B (x, r)` and covers a definite proportion of the ball `B (x, 6 r)` for a given measure `ΞΌ` (think of the situation where `ΞΌ` is a doubling measure and `t` is a family of balls). Consider a (possible non-measurable) set `s` at which the family is fine, i.e., every point of `s` belongs to arbitrarily small elements of `t`. Then one can extract from `t` a disjoint subfamily that covers almost all `s`. -/ theorem exists_disjoint_covering_ae [metric_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±] [second_countable_topology Ξ±] (ΞΌ : measure Ξ±) [is_locally_finite_measure ΞΌ] (s : set Ξ±) (t : set (set Ξ±)) (hf : βˆ€ x ∈ s, βˆ€ (Ξ΅ > (0 : ℝ)), βˆƒ a ∈ t, x ∈ a ∧ a βŠ† closed_ball x Ξ΅) (ht : βˆ€ a ∈ t, (interior a).nonempty) (h't : βˆ€ a ∈ t, is_closed a) (C : ℝβ‰₯0) (h : βˆ€ a ∈ t, βˆƒ x ∈ a, ΞΌ (closed_ball x (3 * diam a)) ≀ C * ΞΌ a) : βˆƒ u βŠ† t, u.countable ∧ u.pairwise_disjoint id ∧ ΞΌ (s \ ⋃ (a ∈ u), a) = 0 := begin /- The idea of the proof is the following. Assume for simplicity that `ΞΌ` is finite. Applying the abstract Vitali covering theorem with `Ξ΄ = diam`, one obtains a disjoint subfamily `u`, such that any element of `t` intersects an element of `u` with comparable diameter. Fix `Ξ΅ > 0`. Since the elements of `u` have summable measure, one can remove finitely elements `w_1, ..., w_n`. so that the measure of the remaining elements is `< Ξ΅`. Consider now a point `z` not in the `w_i`. There is a small ball around `z` not intersecting the `w_i` (as they are closed), an element `a ∈ t` contained in this small ball (as the family `t` is fine at `z`) and an element `b ∈ u` intersecting `a`, with comparable diameter (by definition of `u`). Then `z` belongs to the enlargement of `b`. This shows that `s \ (w_1 βˆͺ ... βˆͺ w_n)` is contained in `⋃ (b ∈ u \ {w_1, ... w_n}) (enlargement of b)`. The measure of the latter set is bounded by `βˆ‘ (b ∈ u \ {w_1, ... w_n}) C * ΞΌ b` (by the doubling property of the measure), which is at most `C Ξ΅`. Letting `Ξ΅` tend to `0` shows that `s` is almost everywhere covered by the family `u`. For the real argument, the measure is only locally finite. Therefore, we implement the same strategy, but locally restricted to balls on which the measure is finite. For this, we do not use the whole family `t`, but a subfamily `t'` supported on small balls (which is possible since the family is assumed to be fine at every point of `s`). -/ rcases eq_empty_or_nonempty s with rfl|nonempty, { refine βŸ¨βˆ…, empty_subset _, countable_empty, pairwise_disjoint_empty, by simp only [measure_empty, Union_false, Union_empty, diff_self]⟩ }, haveI : inhabited Ξ±, { choose x hx using nonempty, exact ⟨x⟩ }, -- choose around each `x` a small ball on which the measure is finite have : βˆ€ x, βˆƒ r, 0 < r ∧ r ≀ 1 ∧ ΞΌ (closed_ball x (20 * r)) < ∞, { assume x, obtain ⟨R, Rpos, ΞΌR⟩ : βˆƒ (R : ℝ) (hR : 0 < R), ΞΌ (closed_ball x R) < ∞ := (ΞΌ.finite_at_nhds x).exists_mem_basis nhds_basis_closed_ball, refine ⟨min 1 (R/20), _, min_le_left _ _, _⟩, { simp only [true_and, lt_min_iff, zero_lt_one], linarith }, { apply lt_of_le_of_lt (measure_mono _) ΞΌR, apply closed_ball_subset_closed_ball, calc 20 * min 1 (R / 20) ≀ 20 * (R/20) : mul_le_mul_of_nonneg_left (min_le_right _ _) (by norm_num) ... = R : by ring } }, choose r hr0 hr1 hrΞΌ, -- we restrict to a subfamily `t'` of `t`, made of elements small enough to ensure that -- they only see a finite part of the measure. let t' := {a ∈ t | βˆƒ x, a βŠ† closed_ball x (r x)}, -- extract a disjoint subfamily `u` of `t'` thanks to the abstract Vitali covering theorem. obtain ⟨u, ut', u_disj, hu⟩ : βˆƒ u βŠ† t', u.pairwise_disjoint id ∧ βˆ€ a ∈ t', βˆƒ b ∈ u, set.nonempty (a ∩ b) ∧ diam a ≀ 2 * diam b, { have A : βˆ€ (a : set Ξ±), a ∈ t' β†’ diam a ≀ 2, { rintros a ⟨hat, ⟨x, hax⟩⟩, calc diam a ≀ 2 * 1 : diam_le_of_subset_closed_ball zero_le_one (hax.trans $ closed_ball_subset_closed_ball $ hr1 x) ... = 2 : mul_one _ }, have B : βˆ€ (a : set Ξ±), a ∈ t' β†’ a.nonempty := Ξ» a hat', set.nonempty.mono interior_subset (ht a hat'.1), exact exists_disjoint_subfamily_covering_enlargment t' diam 2 one_lt_two (Ξ» a ha, diam_nonneg) 2 A B }, have ut : u βŠ† t := Ξ» a hau, (ut' hau).1, -- As the space is second countable, the family is countable since all its sets have nonempty -- interior. have u_count : u.countable := u_disj.countable_of_nonempty_interior (Ξ» a ha, ht a (ut ha)), -- the family `u` will be the desired family refine ⟨u, Ξ» a hat', (ut' hat').1, u_count, u_disj, _⟩, -- it suffices to show that it covers almost all `s` locally around each point `x`. refine null_of_locally_null _ (Ξ» x hx, _), -- let `v` be the subfamily of `u` made of those sets intersecting the small ball `ball x (r x)` let v := {a ∈ u | (a ∩ ball x (r x)).nonempty }, have vu : v βŠ† u := Ξ» a ha, ha.1, -- they are all contained in a fixed ball of finite measure, thanks to our choice of `t'` obtain ⟨R, ΞΌR, hR⟩ : βˆƒ R, ΞΌ (closed_ball x R) < ∞ ∧ βˆ€ a ∈ u, (a ∩ ball x (r x)).nonempty β†’ a βŠ† closed_ball x R, { have : βˆ€ a ∈ u, βˆƒ y, a βŠ† closed_ball y (r y) := Ξ» a hau, (ut' hau).2, choose! y hy using this, have Idist_v : βˆ€ a ∈ v, dist (y a) x ≀ r (y a) + r x, { assume a hav, apply dist_le_add_of_nonempty_closed_ball_inter_closed_ball, exact hav.2.mono (inter_subset_inter (hy a hav.1) ball_subset_closed_ball) }, set R0 := Sup ((Ξ» a, r (y a)) '' v) with hR0, have R0_bdd : bdd_above ((Ξ» a, r (y a)) '' v), { refine ⟨1, Ξ» r' hr', _⟩, rcases (mem_image _ _ _).1 hr' with ⟨b, hb, rfl⟩, exact hr1 _ }, rcases le_total R0 (r x) with H|H, { refine ⟨20 * r x, hrΞΌ x, Ξ» a au hax, _⟩, refine (hy a au).trans _, apply closed_ball_subset_closed_ball', have : r (y a) ≀ R0 := le_cSup R0_bdd (mem_image_of_mem _ ⟨au, hax⟩), linarith [(hr0 (y a)).le, (hr0 x).le, Idist_v a ⟨au, hax⟩] }, { have R0pos : 0 < R0 := (hr0 x).trans_le H, have vnonempty : v.nonempty, { by_contra, rw [← ne_empty_iff_nonempty, not_not] at h, simp only [h, real.Sup_empty, image_empty] at hR0, exact lt_irrefl _ (R0pos.trans_le (le_of_eq hR0)) }, obtain ⟨a, hav, R0a⟩ : βˆƒ a ∈ v, R0/2 < r (y a), { obtain ⟨r', r'mem, hr'⟩ : βˆƒ r' ∈ ((Ξ» a, r (y a)) '' v), R0 / 2 < r' := exists_lt_of_lt_cSup (nonempty_image_iff.2 vnonempty) (half_lt_self R0pos), rcases (mem_image _ _ _).1 r'mem with ⟨a, hav, rfl⟩, exact ⟨a, hav, hr'⟩ }, refine ⟨8 * R0, _, _⟩, { apply lt_of_le_of_lt (measure_mono _) (hrΞΌ (y a)), apply closed_ball_subset_closed_ball', rw dist_comm, linarith [Idist_v a hav] }, { assume b bu hbx, refine (hy b bu).trans _, apply closed_ball_subset_closed_ball', have : r (y b) ≀ R0 := le_cSup R0_bdd (mem_image_of_mem _ ⟨bu, hbx⟩), linarith [Idist_v b ⟨bu, hbx⟩] } } }, -- we will show that, in `ball x (r x)`, almost all `s` is covered by the family `u`. refine ⟨_ ∩ ball x (r x), inter_mem_nhds_within _ (ball_mem_nhds _ (hr0 _)), nonpos_iff_eq_zero.mp (le_of_forall_le_of_dense (Ξ» Ξ΅ Ξ΅pos, _))⟩, -- the elements of `v` are disjoint and all contained in a finite volume ball, hence the sum -- of their measures is finite. have I : βˆ‘' (a : v), ΞΌ a < ∞, { calc βˆ‘' (a : v), ΞΌ a = ΞΌ (⋃ (a ∈ v), a) : begin rw measure_bUnion (u_count.mono vu) _ (Ξ» a ha, (h't _ (vu.trans ut ha)).measurable_set), exact u_disj.subset vu end ... ≀ ΞΌ (closed_ball x R) : measure_mono (Unionβ‚‚_subset (Ξ» a ha, hR a (vu ha) ha.2)) ... < ∞ : ΞΌR }, -- we can obtain a finite subfamily of `v`, such that the measures of the remaining elements -- add up to an arbitrarily small number, say `Ξ΅ / C`. obtain ⟨w, hw⟩ : βˆƒ (w : finset β†₯v), βˆ‘' (a : {a // a βˆ‰ w}), ΞΌ a < Ξ΅ / C, { haveI : ne_bot (at_top : filter (finset v)) := at_top_ne_bot, have : 0 < Ξ΅ / C, by simp only [ennreal.div_pos_iff, Ξ΅pos.ne', ennreal.coe_ne_top, ne.def, not_false_iff, and_self], exact ((tendsto_order.1 (ennreal.tendsto_tsum_compl_at_top_zero I.ne)).2 _ this).exists }, choose! y hy using h, -- main property: the points `z` of `s` which are not covered by `u` are contained in the -- enlargements of the elements not in `w`. have M : (s \ ⋃ (a : set Ξ±) (H : a ∈ u), a) ∩ ball x (r x) βŠ† ⋃ (a : {a // a βˆ‰ w}), closed_ball (y a) (3 * diam (a : set Ξ±)), { assume z hz, set k := ⋃ (a : v) (ha : a ∈ w), (a : set Ξ±) with hk, have k_closed : is_closed k := is_closed_bUnion w.finite_to_set (Ξ» i hi, h't _ (ut (vu i.2))), have z_notmem_k : z βˆ‰ k, { simp only [not_exists, exists_prop, mem_Union, mem_sep_eq, forall_exists_index, set_coe.exists, not_and, exists_and_distrib_right, subtype.coe_mk], assume b hbv h'b h'z, have : z ∈ (s \ ⋃ (a : set Ξ±) (H : a ∈ u), a) ∩ (⋃ (a : set Ξ±) (H : a ∈ u), a) := mem_inter (mem_of_mem_inter_left hz) (mem_bUnion (vu hbv) h'z), simpa only [diff_inter_self] }, -- since the elements of `w` are closed and finitely many, one can find a small ball around `z` -- not intersecting them have : ball x (r x) \ k ∈ 𝓝 z, { apply is_open.mem_nhds (is_open_ball.sdiff k_closed) _, exact (mem_diff _).2 ⟨mem_of_mem_inter_right hz, z_notmem_k⟩ }, obtain ⟨d, dpos, hd⟩ : βˆƒ (d : ℝ) (dpos : 0 < d), closed_ball z d βŠ† ball x (r x) \ k := nhds_basis_closed_ball.mem_iff.1 this, -- choose an element `a` of the family `t` contained in this small ball obtain ⟨a, hat, za, ad⟩ : βˆƒ a ∈ t, z ∈ a ∧ a βŠ† closed_ball z d := hf z ((mem_diff _).1 (mem_of_mem_inter_left hz)).1 d dpos, have ax : a βŠ† ball x (r x) := ad.trans (hd.trans (diff_subset (ball x (r x)) k)), -- it intersects an element `b` of `u` with comparable diameter, by definition of `u` obtain ⟨b, bu, ab, bdiam⟩ : βˆƒ (b : set Ξ±) (H : b ∈ u), (a ∩ b).nonempty ∧ diam a ≀ 2 * diam b := hu a ⟨hat, ⟨x, ax.trans ball_subset_closed_ball⟩⟩, have bv : b ∈ v, { refine ⟨bu, ab.mono _⟩, rw inter_comm, exact inter_subset_inter_right _ ax }, let b' : v := ⟨b, bv⟩, -- `b` can not belong to `w`, as the elements of `w` do not intersect `closed_ball z d`, -- contrary to `b` have b'_notmem_w : b' βˆ‰ w, { assume b'w, have b'k : (b' : set Ξ±) βŠ† k := finset.subset_set_bUnion_of_mem b'w, have : ((ball x (r x) \ k) ∩ k).nonempty := ab.mono (inter_subset_inter (ad.trans hd) b'k), simpa only [diff_inter_self, not_nonempty_empty] }, let b'' : {a // a βˆ‰ w} := ⟨b', b'_notmem_w⟩, -- since `a` and `b` have comparable diameters, it follows that `z` belongs to the -- enlargement of `b` have zb : z ∈ closed_ball (y b) (3 * diam b), { rcases ab with ⟨e, ⟨ea, eb⟩⟩, have A : dist z e ≀ diam a := dist_le_diam_of_mem (bounded_closed_ball.mono ad) za ea, have B : dist e (y b) ≀ diam b, { rcases (ut' bu).2 with ⟨c, hc⟩, apply dist_le_diam_of_mem (bounded_closed_ball.mono hc) eb (hy b (ut bu)).1 }, simp only [mem_closed_ball], linarith [dist_triangle z e (y b)] }, suffices H : closed_ball (y (b'' : set Ξ±)) (3 * diam (b'' : set Ξ±)) βŠ† ⋃ (a : {a // a βˆ‰ w}), closed_ball (y (a : set Ξ±)) (3 * diam (a : set Ξ±)), from H zb, exact subset_Union (Ξ» (a : {a // a βˆ‰ w}), closed_ball (y a) (3 * diam (a : set Ξ±))) b'' }, -- now that we have proved our main inclusion, we can use it to estimate the measure of the points -- in `ball x (r x)` not covered by `u`. haveI : encodable v := (u_count.mono vu).to_encodable, calc ΞΌ ((s \ ⋃ (a : set Ξ±) (H : a ∈ u), a) ∩ ball x (r x)) ≀ ΞΌ (⋃ (a : {a // a βˆ‰ w}), closed_ball (y a) (3 * diam (a : set Ξ±))) : measure_mono M ... ≀ βˆ‘' (a : {a // a βˆ‰ w}), ΞΌ (closed_ball (y a) (3 * diam (a : set Ξ±))) : measure_Union_le _ ... ≀ βˆ‘' (a : {a // a βˆ‰ w}), C * ΞΌ a : ennreal.tsum_le_tsum (Ξ» a, (hy a (ut (vu a.1.2))).2) ... = C * βˆ‘' (a : {a // a βˆ‰ w}), ΞΌ a : ennreal.tsum_mul_left ... ≀ C * (Ξ΅ / C) : ennreal.mul_le_mul le_rfl hw.le ... ≀ Ξ΅ : ennreal.mul_div_le, end /-- Assume that around every point there are arbitrarily small scales at which the measure is doubling. Then the set of closed sets `a` with nonempty interior covering a fixed proportion `1/C` of the ball `closed_ball x (3 * diam a)` forms a Vitali family. This is essentially a restatement of the measurable Vitali theorem. -/ protected def vitali_family [metric_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±] [second_countable_topology Ξ±] (ΞΌ : measure Ξ±) [is_locally_finite_measure ΞΌ] (C : ℝβ‰₯0) (h : βˆ€ x (Ξ΅ > 0), βˆƒ r ∈ Ioc (0 : ℝ) Ξ΅, ΞΌ (closed_ball x (6 * r)) ≀ C * ΞΌ (closed_ball x r)) : vitali_family ΞΌ := { sets_at := Ξ» x, {a | x ∈ a ∧ is_closed a ∧ (interior a).nonempty ∧ ΞΌ (closed_ball x (3 * diam a)) ≀ C * ΞΌ a}, measurable_set' := Ξ» x a ha, ha.2.1.measurable_set, nonempty_interior := Ξ» x a ha, ha.2.2.1, nontrivial := Ξ» x Ξ΅ Ξ΅pos, begin obtain ⟨r, ⟨rpos, rΡ⟩, ΞΌr⟩ : βˆƒ r ∈ Ioc (0 : ℝ) Ξ΅, ΞΌ (closed_ball x (6 * r)) ≀ C * ΞΌ (closed_ball x r) := h x Ξ΅ Ξ΅pos, refine ⟨closed_ball x r, ⟨_, is_closed_ball, _, _⟩, closed_ball_subset_closed_ball rΡ⟩, { simp only [rpos.le, mem_closed_ball, dist_self] }, { exact (nonempty_ball.2 rpos).mono (ball_subset_interior_closed_ball) }, { apply le_trans (measure_mono (closed_ball_subset_closed_ball _)) ΞΌr, have : diam (closed_ball x r) ≀ 2 * r := diam_closed_ball rpos.le, linarith } end, covering := begin assume s f fsubset ffine, rcases eq_empty_or_nonempty s with rfl|H, { exact βŸ¨βˆ…, Ξ» _, βˆ…, by simp, by simp⟩ }, haveI : inhabited Ξ±, { choose x hx using H, exact ⟨x⟩ }, let t := ⋃ (x ∈ s), f x, have A₁ : βˆ€ x ∈ s, βˆ€ (Ξ΅ : ℝ), 0 < Ξ΅ β†’ (βˆƒ a ∈ t, x ∈ a ∧ a βŠ† closed_ball x Ξ΅), { assume x xs Ξ΅ Ξ΅pos, rcases ffine x xs Ξ΅ Ξ΅pos with ⟨a, xa, hax⟩, exact ⟨a, mem_bUnion xs xa, (fsubset x xs xa).1, hax⟩ }, have Aβ‚‚ : βˆ€ a ∈ t, (interior a).nonempty, { rintros a ha, rcases mem_Unionβ‚‚.1 ha with ⟨x, xs, xa⟩, exact (fsubset x xs xa).2.2.1 }, have A₃ : βˆ€ a ∈ t, is_closed a, { rintros a ha, rcases mem_Unionβ‚‚.1 ha with ⟨x, xs, xa⟩, exact (fsubset x xs xa).2.1 }, have Aβ‚„ : βˆ€ a ∈ t, βˆƒ x ∈ a, ΞΌ (closed_ball x (3 * diam a)) ≀ C * ΞΌ a, { rintros a ha, rcases mem_Unionβ‚‚.1 ha with ⟨x, xs, xa⟩, exact ⟨x, (fsubset x xs xa).1, (fsubset x xs xa).2.2.2⟩ }, obtain ⟨u, ut, u_count, u_disj, ΞΌu⟩ : βˆƒ u βŠ† t, u.countable ∧ u.pairwise disjoint ∧ ΞΌ (s \ ⋃ a ∈ u, a) = 0 := exists_disjoint_covering_ae ΞΌ s t A₁ Aβ‚‚ A₃ C Aβ‚„, have : βˆ€ a ∈ u, βˆƒ x ∈ s, a ∈ f x := Ξ» a ha, mem_Unionβ‚‚.1 (ut ha), choose! x hx using this, have inj_on_x : inj_on x u, { assume a ha b hb hab, have A : (a ∩ b).nonempty, { refine ⟨x a, mem_inter ((fsubset _ (hx a ha).1 (hx a ha).2).1) _⟩, rw hab, exact (fsubset _ (hx b hb).1 (hx b hb).2).1 }, contrapose A, have : disjoint a b := u_disj ha hb A, simpa only [← not_disjoint_iff_nonempty_inter] }, refine ⟨x '' u, function.inv_fun_on x u, _, _, _, _⟩, { assume y hy, rcases (mem_image _ _ _).1 hy with ⟨a, au, rfl⟩, exact (hx a au).1 }, { rw [inj_on_x.pairwise_disjoint_image], assume a ha b hb hab, simp only [function.on_fun, inj_on_x.left_inv_on_inv_fun_on ha, inj_on_x.left_inv_on_inv_fun_on hb, (∘)], exact u_disj ha hb hab }, { assume y hy, rcases (mem_image _ _ _).1 hy with ⟨a, ha, rfl⟩, rw inj_on_x.left_inv_on_inv_fun_on ha, exact (hx a ha).2 }, { rw [bUnion_image], convert ΞΌu using 3, exact Unionβ‚‚_congr (Ξ» a ha, inj_on_x.left_inv_on_inv_fun_on ha) } end } end vitali
261cf12017ec5f574114589510ec607625e0b162
5df84495ec6c281df6d26411cc20aac5c941e745
/src/formal_ml/fin_group.lean
17647fa12782b5c4884f291e0f26d497bc005897
[ "Apache-2.0" ]
permissive
eric-wieser/formal-ml
e278df5a8df78aa3947bc8376650419e1b2b0a14
630011d19fdd9539c8d6493a69fe70af5d193590
refs/heads/master
1,681,491,589,256
1,612,642,743,000
1,612,642,743,000
360,114,136
0
0
Apache-2.0
1,618,998,189,000
1,618,998,188,000
null
UTF-8
Lean
false
false
3,419
lean
/- Copyright 2020 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -/ import data.fin import tactic.linarith import tactic.zify /- Note : Looking at the documentation of zmod suggests this is unnecessary. -/ namespace nat --Used below in simps in many places. @[simp] lemma mul_mod_mod {a b c : β„•} : (a * (b % c)) % c = (a * b) % c := begin rw ← mod_add_div b c, simp [right_distrib, left_distrib], rw mul_comm a (c * (b / c)), rw mul_assoc, rw add_mul_mod_self_left, end end nat namespace fin lemma pos_of_fin {n:β„•} (a:fin n) : 0 < n := begin apply lt_of_le_of_lt (nat.zero_le _) a.property, end def neg {n:β„•} (a:fin n):fin n := @subtype.mk β„• _ ((n - a) % n) begin apply nat.mod_lt, apply fin.pos_of_fin a, end instance has_neg (n:β„•):has_neg (fin n) := ⟨@fin.neg n⟩ lemma neg_def {n:β„•} (a:fin n.succ):(-a).val = (n.succ - a.val) % n.succ := begin have A1:(-a) = fin.of_nat (n.succ - a.val) := rfl, rw A1, unfold fin.of_nat, end @[simp] lemma val_mod {n:β„•} {a:fin n}:(↑a:nat) % n = (↑a:nat) := begin have A1:(↑a:β„•) = (a.val) := rfl, rw A1, rw nat.mod_eq_of_lt a.property, end instance add_comm_group (n:β„•):add_comm_group (fin n.succ) := { add_assoc := begin intros a b c, apply subtype.eq, simp [fin.add_def], rw add_assoc, end, zero_add := begin intros a, apply subtype.eq, simp [fin.add_def], end, add_zero := begin intros a, apply subtype.eq, simp [fin.add_def], end, add_left_neg := begin intro a, apply subtype.eq, simp [fin.add_def, fin.neg_def], have A3: (↑a) < n.succ, { apply a.property, }, have A4: n.succ - (↑a) + (↑a) = n.succ, { zify [le_of_lt A3], linarith }, rw A4, simp, end, add_comm := begin intros a b, apply subtype.eq, simp [fin.add_def], rw add_comm, end, ..fin.has_add, ..fin.has_zero, ..fin.has_neg (n.succ), } instance comm_ring (n:β„•):comm_ring (fin n.succ.succ) := { mul_assoc := begin intros a b c, apply subtype.eq, simp [fin.mul_def], rw mul_comm, --rw mul_mod_mod, rw ← mul_assoc, simp, rw mul_comm, end, one_mul := begin intros a, apply subtype.eq, simp [fin.mul_def], end, mul_one := begin intros a, apply subtype.eq, simp [fin.mul_def], end, mul_comm := begin intros a b, apply subtype.eq, simp [fin.mul_def], rw mul_comm, end, left_distrib := begin intros a b c, apply subtype.eq, simp [fin.mul_def, fin.add_def], rw left_distrib, end, right_distrib := begin intros a b c, apply subtype.eq, simp [fin.mul_def, fin.add_def], rw mul_comm, simp, rw mul_comm, rw right_distrib, end, ..fin.has_one, ..fin.has_mul, ..fin.has_add, ..fin.has_zero, ..fin.has_neg (n.succ.succ), ..fin.add_comm_group (n.succ), } end fin
8b0240fedbf894f1d3e612da2abef093b36f3e5c
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Init/Data/String/Basic.lean
a9f850bce56a3efaec80aa71a22d76e42760396e
[ "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
19,227
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Data.List.Basic import Init.Data.Char.Basic import Init.Data.Option.Basic universe u def List.asString (s : List Char) : String := ⟨s⟩ namespace String instance : OfNat String.Pos (nat_lit 0) where ofNat := {} instance : LT String := ⟨fun s₁ sβ‚‚ => s₁.data < sβ‚‚.data⟩ @[extern "lean_string_dec_lt"] instance decLt (s₁ sβ‚‚ : @& String) : Decidable (s₁ < sβ‚‚) := List.hasDecidableLt s₁.data sβ‚‚.data @[extern "lean_string_length"] def length : (@& String) β†’ Nat | ⟨s⟩ => s.length /-- The internal implementation uses dynamic arrays and will perform destructive updates if the String is not shared. -/ @[extern "lean_string_push"] def push : String β†’ Char β†’ String | ⟨s⟩, c => ⟨s ++ [c]⟩ /-- The internal implementation uses dynamic arrays and will perform destructive updates if the String is not shared. -/ @[extern "lean_string_append"] def append : String β†’ (@& String) β†’ String | ⟨a⟩, ⟨b⟩ => ⟨a ++ b⟩ /-- O(n) in the runtime, where n is the length of the String -/ def toList (s : String) : List Char := s.data private def utf8GetAux : List Char β†’ Pos β†’ Pos β†’ Char | [], _, _ => default | c::cs, i, p => if i = p then c else utf8GetAux cs (i + c) p /-- Return character at position `p`. If `p` is not a valid position returns `(default : Char)`. See `utf8GetAux` for the reference implementation. -/ @[extern "lean_string_utf8_get"] def get (s : @& String) (p : @& Pos) : Char := match s with | ⟨s⟩ => utf8GetAux s 0 p private def utf8GetAux? : List Char β†’ Pos β†’ Pos β†’ Option Char | [], _, _ => none | c::cs, i, p => if i = p then c else utf8GetAux cs (i + c) p @[extern "lean_string_utf8_get_opt"] def get? : (@& String) β†’ (@& Pos) β†’ Option Char | ⟨s⟩, p => utf8GetAux? s 0 p /-- Similar to `get`, but produces a panic error message if `p` is not a valid `String.Pos`. -/ @[extern "lean_string_utf8_get_bang"] def get! (s : @& String) (p : @& Pos) : Char := match s with | ⟨s⟩ => utf8GetAux s 0 p private def utf8SetAux (c' : Char) : List Char β†’ Pos β†’ Pos β†’ List Char | [], _, _ => [] | c::cs, i, p => if i = p then (c'::cs) else c::(utf8SetAux c' cs (i + c) p) @[extern "lean_string_utf8_set"] def set : String β†’ (@& Pos) β†’ Char β†’ String | ⟨s⟩, i, c => ⟨utf8SetAux c s 0 i⟩ def modify (s : String) (i : Pos) (f : Char β†’ Char) : String := s.set i <| f <| s.get i @[extern "lean_string_utf8_next"] def next (s : @& String) (p : @& Pos) : Pos := let c := get s p p + c private def utf8PrevAux : List Char β†’ Pos β†’ Pos β†’ Pos | [], _, _ => 0 | c::cs, i, p => let i' := i + c if i' = p then i else utf8PrevAux cs i' p @[extern "lean_string_utf8_prev"] def prev : (@& String) β†’ (@& Pos) β†’ Pos | ⟨s⟩, p => if p = 0 then 0 else utf8PrevAux s 0 p def front (s : String) : Char := get s 0 def back (s : String) : Char := get s (prev s s.endPos) @[extern "lean_string_utf8_at_end"] def atEnd : (@& String) β†’ (@& Pos) β†’ Bool | s, p => p.byteIdx β‰₯ utf8ByteSize s /- TODO: remove `partial` keywords after we restore the tactic framework and wellfounded recursion support -/ partial def posOfAux (s : String) (c : Char) (stopPos : Pos) (pos : Pos) : Pos := if pos == stopPos then pos else if s.get pos == c then pos else posOfAux s c stopPos (s.next pos) @[inline] def posOf (s : String) (c : Char) : Pos := posOfAux s c s.endPos 0 partial def revPosOfAux (s : String) (c : Char) (pos : Pos) : Option Pos := if s.get pos == c then some pos else if pos == 0 then none else revPosOfAux s c (s.prev pos) def revPosOf (s : String) (c : Char) : Option Pos := if s.endPos == 0 then none else revPosOfAux s c (s.prev s.endPos) partial def findAux (s : String) (p : Char β†’ Bool) (stopPos : Pos) (pos : Pos) : Pos := if pos == stopPos then pos else if p (s.get pos) then pos else findAux s p stopPos (s.next pos) @[inline] def find (s : String) (p : Char β†’ Bool) : Pos := findAux s p s.endPos 0 partial def revFindAux (s : String) (p : Char β†’ Bool) (pos : Pos) : Option Pos := if p (s.get pos) then some pos else if pos == 0 then none else revFindAux s p (s.prev pos) def revFind (s : String) (p : Char β†’ Bool) : Option Pos := if s.endPos == 0 then none else revFindAux s p (s.prev s.endPos) abbrev Pos.min (p₁ pβ‚‚ : Pos) : Pos := { byteIdx := p₁.byteIdx.min pβ‚‚.byteIdx } /-- Returns the first position where the two strings differ. -/ partial def firstDiffPos (a b : String) : Pos := let stopPos := a.endPos.min b.endPos let rec loop (i : Pos) : Pos := if i == stopPos || a.get i != b.get i then i else loop (a.next i) loop 0 @[extern "lean_string_utf8_extract"] def extract : (@& String) β†’ (@& Pos) β†’ (@& Pos) β†’ String | ⟨s⟩, b, e => if b.byteIdx β‰₯ e.byteIdx then ⟨[]⟩ else ⟨go₁ s 0 b e⟩ where go₁ : List Char β†’ Pos β†’ Pos β†’ Pos β†’ List Char | [], _, _, _ => [] | s@(c::cs), i, b, e => if i = b then goβ‚‚ s i e else go₁ cs (i + c) b e goβ‚‚ : List Char β†’ Pos β†’ Pos β†’ List Char | [], _, _ => [] | c::cs, i, e => if i = e then [] else c :: goβ‚‚ cs (i + c) e @[specialize] partial def splitAux (s : String) (p : Char β†’ Bool) (b : Pos) (i : Pos) (r : List String) : List String := if s.atEnd i then let r := (s.extract b i)::r r.reverse else if p (s.get i) then let i := s.next i splitAux s p i i (s.extract b { byteIdx := i.byteIdx - 1 } :: r) else splitAux s p b (s.next i) r @[specialize] def split (s : String) (p : Char β†’ Bool) : List String := splitAux s p 0 0 [] partial def splitOnAux (s sep : String) (b : Pos) (i : Pos) (j : Pos) (r : List String) : List String := if s.atEnd i then let r := if sep.atEnd j then ""::(s.extract b (i - j))::r else (s.extract b i)::r r.reverse else if s.get i == sep.get j then let i := s.next i let j := sep.next j if sep.atEnd j then splitOnAux s sep i i 0 (s.extract b (i - j)::r) else splitOnAux s sep b i j r else splitOnAux s sep b (s.next i) 0 r def splitOn (s : String) (sep : String := " ") : List String := if sep == "" then [s] else splitOnAux s sep 0 0 0 [] instance : Inhabited String := ⟨""⟩ instance : Append String := ⟨String.append⟩ def str : String β†’ Char β†’ String := push def pushn (s : String) (c : Char) (n : Nat) : String := n.repeat (fun s => s.push c) s def isEmpty (s : String) : Bool := s.endPos == 0 def join (l : List String) : String := l.foldl (fun r s => r ++ s) "" def singleton (c : Char) : String := "".push c def intercalate (s : String) : List String β†’ String | [] => "" | a :: as => go a s as where go (acc : String) (s : String) : List String β†’ String | a :: as => go (acc ++ s ++ a) s as | [] => acc structure Iterator where s : String i : Pos deriving DecidableEq def mkIterator (s : String) : Iterator := ⟨s, 0⟩ abbrev iter := mkIterator instance : SizeOf String.Iterator where sizeOf i := i.1.utf8ByteSize - i.2.byteIdx theorem Iterator.sizeOf_eq (i : String.Iterator) : sizeOf i = i.1.utf8ByteSize - i.2.byteIdx := rfl namespace Iterator def toString : Iterator β†’ String | ⟨s, _⟩ => s def remainingBytes : Iterator β†’ Nat | ⟨s, i⟩ => s.endPos.byteIdx - i.byteIdx def pos : Iterator β†’ Pos | ⟨_, i⟩ => i def curr : Iterator β†’ Char | ⟨s, i⟩ => get s i def next : Iterator β†’ Iterator | ⟨s, i⟩ => ⟨s, s.next i⟩ def prev : Iterator β†’ Iterator | ⟨s, i⟩ => ⟨s, s.prev i⟩ def atEnd : Iterator β†’ Bool | ⟨s, i⟩ => i.byteIdx β‰₯ s.endPos.byteIdx def hasNext : Iterator β†’ Bool | ⟨s, i⟩ => i.byteIdx < s.endPos.byteIdx def hasPrev : Iterator β†’ Bool | ⟨_, i⟩ => i.byteIdx > 0 def setCurr : Iterator β†’ Char β†’ Iterator | ⟨s, i⟩, c => ⟨s.set i c, i⟩ def toEnd : Iterator β†’ Iterator | ⟨s, _⟩ => ⟨s, s.endPos⟩ def extract : Iterator β†’ Iterator β†’ String | ⟨s₁, b⟩, ⟨sβ‚‚, e⟩ => if s₁ β‰  sβ‚‚ || b > e then "" else s₁.extract b e def forward : Iterator β†’ Nat β†’ Iterator | it, 0 => it | it, n+1 => forward it.next n def remainingToString : Iterator β†’ String | ⟨s, i⟩ => s.extract i s.endPos def nextn : Iterator β†’ Nat β†’ Iterator | it, 0 => it | it, i+1 => nextn it.next i def prevn : Iterator β†’ Nat β†’ Iterator | it, 0 => it | it, i+1 => prevn it.prev i end Iterator partial def offsetOfPosAux (s : String) (pos : Pos) (i : Pos) (offset : Nat) : Nat := if i == pos || s.atEnd i then offset else offsetOfPosAux s pos (s.next i) (offset+1) def offsetOfPos (s : String) (pos : Pos) : Nat := offsetOfPosAux s pos 0 0 @[specialize] partial def foldlAux {Ξ± : Type u} (f : Ξ± β†’ Char β†’ Ξ±) (s : String) (stopPos : Pos) (i : Pos) (a : Ξ±) : Ξ± := let rec loop (i : Pos) (a : Ξ±) := if i == stopPos then a else loop (s.next i) (f a (s.get i)) loop i a @[inline] def foldl {Ξ± : Type u} (f : Ξ± β†’ Char β†’ Ξ±) (init : Ξ±) (s : String) : Ξ± := foldlAux f s s.endPos 0 init @[specialize] partial def foldrAux {Ξ± : Type u} (f : Char β†’ Ξ± β†’ Ξ±) (a : Ξ±) (s : String) (stopPos : Pos) (i : Pos) : Ξ± := let rec loop (i : Pos) := if i == stopPos then a else f (s.get i) (loop (s.next i)) loop i @[inline] def foldr {Ξ± : Type u} (f : Char β†’ Ξ± β†’ Ξ±) (init : Ξ±) (s : String) : Ξ± := foldrAux f init s s.endPos 0 @[specialize] partial def anyAux (s : String) (stopPos : Pos) (p : Char β†’ Bool) (i : Pos) : Bool := let rec loop (i : Pos) := if i == stopPos then false else if p (s.get i) then true else loop (s.next i) loop i @[inline] def any (s : String) (p : Char β†’ Bool) : Bool := anyAux s s.endPos p 0 @[inline] def all (s : String) (p : Char β†’ Bool) : Bool := !s.any (fun c => !p c) def contains (s : String) (c : Char) : Bool := s.any (fun a => a == c) @[specialize] partial def mapAux (f : Char β†’ Char) (i : Pos) (s : String) : String := if s.atEnd i then s else let c := f (s.get i) let s := s.set i c mapAux f (s.next i) s @[inline] def map (f : Char β†’ Char) (s : String) : String := mapAux f 0 s def isNat (s : String) : Bool := !s.isEmpty && s.all (Β·.isDigit) def toNat? (s : String) : Option Nat := if s.isNat then some <| s.foldl (fun n c => n*10 + (c.toNat - '0'.toNat)) 0 else none /-- Return `true` iff the substring of byte size `sz` starting at position `off1` in `s1` is equal to that starting at `off2` in `s2.`. False if either substring of that byte size does not exist. -/ partial def substrEq (s1 : String) (off1 : String.Pos) (s2 : String) (off2 : String.Pos) (sz : Nat) : Bool := off1.byteIdx + sz ≀ s1.endPos.byteIdx && off2.byteIdx + sz ≀ s2.endPos.byteIdx && loop off1 off2 { byteIdx := off1.byteIdx + sz } where loop (off1 off2 stop1 : Pos) := if off1.byteIdx >= stop1.byteIdx then true else let c₁ := s1.get off1 let cβ‚‚ := s2.get off2 c₁ == cβ‚‚ && loop (off1 + c₁) (off2 + cβ‚‚) stop1 /-- Return true iff `p` is a prefix of `s` -/ def isPrefixOf (p : String) (s : String) : Bool := substrEq p 0 s 0 p.endPos.byteIdx /-- Replace all occurrences of `pattern` in `s` with `replacment`. -/ partial def replace (s pattern replacement : String) : String := loop "" 0 0 where loop (acc : String) (accStop pos : String.Pos) := if pos.byteIdx + pattern.endPos.byteIdx > s.endPos.byteIdx then acc ++ s.extract accStop s.endPos else if s.substrEq pos pattern 0 pattern.endPos.byteIdx then loop (acc ++ s.extract accStop pos ++ replacement) (pos + pattern) (pos + pattern) else loop acc accStop (s.next pos) end String namespace Substring @[inline] def isEmpty (ss : Substring) : Bool := ss.bsize == 0 @[inline] def toString : Substring β†’ String | ⟨s, b, e⟩ => s.extract b e @[inline] def toIterator : Substring β†’ String.Iterator | ⟨s, b, _⟩ => ⟨s, b⟩ /-- Return the codepoint at the given offset into the substring. -/ @[inline] def get : Substring β†’ String.Pos β†’ Char | ⟨s, b, _⟩, p => s.get (b+p) /-- Given an offset of a codepoint into the substring, return the offset there of the next codepoint. -/ @[inline] def next : Substring β†’ String.Pos β†’ String.Pos | ⟨s, b, e⟩, p => let absP := b+p if absP = e then p else { byteIdx := (s.next absP).byteIdx - b.byteIdx } /-- Given an offset of a codepoint into the substring, return the offset there of the previous codepoint. -/ @[inline] def prev : Substring β†’ String.Pos β†’ String.Pos | ⟨s, b, _⟩, p => let absP := b+p if absP = b then p else { byteIdx := (s.prev absP).byteIdx - b.byteIdx } def nextn : Substring β†’ Nat β†’ String.Pos β†’ String.Pos | _, 0, p => p | ss, i+1, p => ss.nextn i (ss.next p) def prevn : Substring β†’ Nat β†’ String.Pos β†’ String.Pos | _, 0, p => p | ss, i+1, p => ss.prevn i (ss.prev p) @[inline] def front (s : Substring) : Char := s.get 0 /-- Return the offset into `s` of the first occurence of `c` in `s`, or `s.bsize` if `c` doesn't occur. -/ @[inline] def posOf (s : Substring) (c : Char) : String.Pos := match s with | ⟨s, b, e⟩ => { byteIdx := (String.posOfAux s c e b).byteIdx - b.byteIdx } @[inline] def drop : Substring β†’ Nat β†’ Substring | ss@⟨s, b, e⟩, n => ⟨s, b + ss.nextn n 0, e⟩ @[inline] def dropRight : Substring β†’ Nat β†’ Substring | ss@⟨s, b, _⟩, n => ⟨s, b, b + ss.prevn n ⟨ss.bsize⟩⟩ @[inline] def take : Substring β†’ Nat β†’ Substring | ss@⟨s, b, _⟩, n => ⟨s, b, b + ss.nextn n 0⟩ @[inline] def takeRight : Substring β†’ Nat β†’ Substring | ss@⟨s, b, e⟩, n => ⟨s, b + ss.prevn n ⟨ss.bsize⟩, e⟩ @[inline] def atEnd : Substring β†’ String.Pos β†’ Bool | ⟨_, b, e⟩, p => b + p == e @[inline] def extract : Substring β†’ String.Pos β†’ String.Pos β†’ Substring | ⟨s, b, e⟩, b', e' => if b' β‰₯ e' then ⟨"", 0, 0⟩ else ⟨s, e.min (b+b'), e.min (b+e')⟩ partial def splitOn (s : Substring) (sep : String := " ") : List Substring := if sep == "" then [s] else let rec loop (b i j : String.Pos) (r : List Substring) : List Substring := if i.byteIdx == s.bsize then let r := if sep.atEnd j then "".toSubstring :: s.extract b (i-j) :: r else s.extract b i :: r r.reverse else if s.get i == sep.get j then let i := s.next i let j := sep.next j if sep.atEnd j then loop i i 0 (s.extract b (i-j) :: r) else loop b i j r else loop b (s.next i) 0 r loop 0 0 0 [] @[inline] def foldl {Ξ± : Type u} (f : Ξ± β†’ Char β†’ Ξ±) (init : Ξ±) (s : Substring) : Ξ± := match s with | ⟨s, b, e⟩ => String.foldlAux f s e b init @[inline] def foldr {Ξ± : Type u} (f : Char β†’ Ξ± β†’ Ξ±) (init : Ξ±) (s : Substring) : Ξ± := match s with | ⟨s, b, e⟩ => String.foldrAux f init s e b @[inline] def any (s : Substring) (p : Char β†’ Bool) : Bool := match s with | ⟨s, b, e⟩ => String.anyAux s e p b @[inline] def all (s : Substring) (p : Char β†’ Bool) : Bool := !s.any (fun c => !p c) def contains (s : Substring) (c : Char) : Bool := s.any (fun a => a == c) @[specialize] private partial def takeWhileAux (s : String) (stopPos : String.Pos) (p : Char β†’ Bool) (i : String.Pos) : String.Pos := if i == stopPos then i else if p (s.get i) then takeWhileAux s stopPos p (s.next i) else i @[inline] def takeWhile : Substring β†’ (Char β†’ Bool) β†’ Substring | ⟨s, b, e⟩, p => let e := takeWhileAux s e p b; ⟨s, b, e⟩ @[inline] def dropWhile : Substring β†’ (Char β†’ Bool) β†’ Substring | ⟨s, b, e⟩, p => let b := takeWhileAux s e p b; ⟨s, b, e⟩ @[specialize] private partial def takeRightWhileAux (s : String) (begPos : String.Pos) (p : Char β†’ Bool) (i : String.Pos) : String.Pos := if i == begPos then i else let i' := s.prev i let c := s.get i' if !p c then i else takeRightWhileAux s begPos p i' @[inline] def takeRightWhile : Substring β†’ (Char β†’ Bool) β†’ Substring | ⟨s, b, e⟩, p => let b := takeRightWhileAux s b p e ⟨s, b, e⟩ @[inline] def dropRightWhile : Substring β†’ (Char β†’ Bool) β†’ Substring | ⟨s, b, e⟩, p => let e := takeRightWhileAux s b p e ⟨s, b, e⟩ @[inline] def trimLeft (s : Substring) : Substring := s.dropWhile Char.isWhitespace @[inline] def trimRight (s : Substring) : Substring := s.dropRightWhile Char.isWhitespace @[inline] def trim : Substring β†’ Substring | ⟨s, b, e⟩ => let b := takeWhileAux s e Char.isWhitespace b let e := takeRightWhileAux s b Char.isWhitespace e ⟨s, b, e⟩ def isNat (s : Substring) : Bool := s.all fun c => c.isDigit def toNat? (s : Substring) : Option Nat := if s.isNat then some <| s.foldl (fun n c => n*10 + (c.toNat - '0'.toNat)) 0 else none def beq (ss1 ss2 : Substring) : Bool := ss1.bsize == ss2.bsize && ss1.str.substrEq ss1.startPos ss2.str ss2.startPos ss1.bsize instance hasBeq : BEq Substring := ⟨beq⟩ end Substring namespace String def drop (s : String) (n : Nat) : String := (s.toSubstring.drop n).toString def dropRight (s : String) (n : Nat) : String := (s.toSubstring.dropRight n).toString def take (s : String) (n : Nat) : String := (s.toSubstring.take n).toString def takeRight (s : String) (n : Nat) : String := (s.toSubstring.takeRight n).toString def takeWhile (s : String) (p : Char β†’ Bool) : String := (s.toSubstring.takeWhile p).toString def dropWhile (s : String) (p : Char β†’ Bool) : String := (s.toSubstring.dropWhile p).toString def takeRightWhile (s : String) (p : Char β†’ Bool) : String := (s.toSubstring.takeRightWhile p).toString def dropRightWhile (s : String) (p : Char β†’ Bool) : String := (s.toSubstring.dropRightWhile p).toString def startsWith (s pre : String) : Bool := s.toSubstring.take pre.length == pre.toSubstring def endsWith (s post : String) : Bool := s.toSubstring.takeRight post.length == post.toSubstring def trimRight (s : String) : String := s.toSubstring.trimRight.toString def trimLeft (s : String) : String := s.toSubstring.trimLeft.toString def trim (s : String) : String := s.toSubstring.trim.toString @[inline] def nextWhile (s : String) (p : Char β†’ Bool) (i : String.Pos) : String.Pos := Substring.takeWhileAux s s.endPos p i @[inline] def nextUntil (s : String) (p : Char β†’ Bool) (i : String.Pos) : String.Pos := nextWhile s (fun c => !p c) i def toUpper (s : String) : String := s.map Char.toUpper def toLower (s : String) : String := s.map Char.toLower def capitalize (s : String) := s.set 0 <| s.get 0 |>.toUpper def decapitalize (s : String) := s.set 0 <| s.get 0 |>.toLower end String protected def Char.toString (c : Char) : String := String.singleton c
322ad952307e51b342c94461931f5b8f675deccf
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/holeErrors.lean
9f56ae5d6e5ab1a7045e4e0a246be598acf7231b
[ "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
162
lean
new_frontend def f1 := id def f2 : _ := id def f3 := let x := id; x def f4 (x) := x def f5 (x : _) := x def f6 := fun x => x def f7 := let rec x := id; 10
06b446014d9286590a206b85476937f6fb820904
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/nat/log.lean
c5259f0cdb17edf6587516aded1038a6119dc2cb
[ "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
3,459
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import data.nat.pow /-! # Natural number logarithm This file defines `log b n`, the logarithm of `n` with base `b`, to be the largest `k` such that `b ^ k ≀ n`. -/ namespace nat /-- `log b n`, is the logarithm of natural number `n` in base `b`. It returns the largest `k : β„•` such that `b^k ≀ n`, so if `b^k = n`, it returns exactly `k`. -/ @[pp_nodot] def log (b : β„•) : β„• β†’ β„• | n := if h : b ≀ n ∧ 1 < b then have n / b < n, from div_lt_self (nat.lt_of_lt_of_le (lt_trans zero_lt_one h.2) h.1) h.2, log (n / b) + 1 else 0 lemma log_eq_zero {b n : β„•} (hnb : n < b ∨ b ≀ 1) : log b n = 0 := begin rw [or_iff_not_and_not, not_lt, not_le] at hnb, rw [log, ←ite_not, if_pos hnb], end lemma log_eq_zero_of_lt {b n : β„•} (hn : n < b) : log b n = 0 := log_eq_zero $ or.inl hn lemma log_eq_zero_of_le {b n : β„•} (hb : b ≀ 1) : log b n = 0 := log_eq_zero $ or.inr hb lemma log_zero_eq_zero {b : β„•} : log b 0 = 0 := by { rw log, cases b; refl } lemma log_one_eq_zero {b : β„•} : log b 1 = 0 := if h : b ≀ 1 then log_eq_zero_of_le h else log_eq_zero_of_lt (not_le.mp h) lemma log_b_zero_eq_zero {n : β„•} : log 0 n = 0 := log_eq_zero_of_le zero_le_one lemma log_b_one_eq_zero {n : β„•} : log 1 n = 0 := log_eq_zero_of_le rfl.ge lemma pow_le_iff_le_log (x y : β„•) {b} (hb : 1 < b) (hy : 1 ≀ y) : b^x ≀ y ↔ x ≀ log b y := begin induction y using nat.strong_induction_on with y ih generalizing x, rw [log], split_ifs, { have h'' : 0 < b := lt_of_le_of_lt (zero_le _) hb, cases h with hβ‚€ h₁, rw [← nat.sub_le_right_iff_le_add,← ih (y / b), le_div_iff_mul_le _ _ h'',← pow_succ'], { cases x; simp [hβ‚€,hy] }, { apply div_lt_self; assumption }, { rwa [le_div_iff_mul_le _ _ h'',one_mul], } }, { replace h := lt_of_not_ge (not_and'.1 h hb), split; intros h', { have := lt_of_le_of_lt h' h, apply le_of_succ_le_succ, change x < 1, rw [← pow_lt_iff_lt_right hb,pow_one], exact this }, { replace h' := le_antisymm h' (zero_le _), rw [h',pow_zero], exact hy} }, end lemma log_pow (b x : β„•) (hb : 1 < b) : log b (b ^ x) = x := eq_of_forall_le_iff $ Ξ» z, by { rwa [← pow_le_iff_le_log _ _ hb,pow_le_iff_le_right], rw ← pow_zero b, apply pow_le_pow_of_le_right, apply lt_of_le_of_lt (zero_le _) hb, apply zero_le } lemma pow_succ_log_gt_self (b x : β„•) (hb : 1 < b) (hy : 1 ≀ x) : x < b ^ succ (log b x) := begin apply lt_of_not_ge, rw [(β‰₯),pow_le_iff_le_log _ _ hb hy], apply not_le_of_lt, apply lt_succ_self, end lemma pow_log_le_self (b x : β„•) (hb : 1 < b) (hx : 1 ≀ x) : b ^ log b x ≀ x := by rw [pow_le_iff_le_log _ _ hb hx] lemma log_le_log_of_le {b n m : β„•} (h : n ≀ m) : log b n ≀ log b m := begin cases le_or_lt b 1 with hb hb, { rw log_eq_zero_of_le hb, exact zero_le _ }, { cases eq_zero_or_pos n with hn hn, { rw [hn, log_zero_eq_zero], exact zero_le _ }, { rw ←pow_le_iff_le_log _ _ hb (lt_of_lt_of_le hn h), exact (pow_log_le_self b n hb hn).trans h } } end lemma log_le_log_succ {b n : β„•} : log b n ≀ log b n.succ := log_le_log_of_le $ le_succ n lemma log_mono {b : β„•} : monotone (Ξ» n : β„•, log b n) := monotone_of_monotone_nat $ Ξ» n, log_le_log_succ end nat
28ebd80d59c39d0884de672926c1cf18b4f4ceaf
b210f3162055e6c91af0e5938b8e29ad2b66957d
/notes02.20.20.lean
7e433def7e1a5e3872946696c012347b607f7a2b
[]
no_license
colemanjenkins/SharedFiles
db8a5cea23e51b7781ce779af546045c4230c17d
6ac3cc21a2912f3c8e62a23a882384b0b2a5d41b
refs/heads/master
1,608,650,217,053
1,598,992,893,000
1,598,992,893,000
236,838,099
0
0
null
null
null
null
UTF-8
Lean
false
false
667
lean
namespace hidden --Ξ± is an argument to the function of what type the list is inductive list_poly (Ξ±: Type) | nil {}: list_poly | cons (h : Ξ±)(t : list_poly) : list_poly def l1 := list_poly.cons 5 list_poly.nil def fib : β„• β†’ β„• | 0 := 0 | 1 := 1 | (n'' + 2) := (fib n'') + fib (n'' + 1) #eval fib 33 def equals : β„• β†’ β„• β†’ bool | 0 0 := true | _ 0 := false | 0 _ := false | (nat.succ n') (nat.succ m') := equals n' m' #eval equals 7 7 def sub : β„• β†’ β„• β†’ β„• | 0 0 := 0 | n 0 := n | 0 n := 0 | (nat.succ n') (nat.succ m') := sub n' m' end hidden /- 1. Write type of function without thinking of implementation 2. Write test cases -/
42910bebfa8f827192ef19f18521ed8c82acd4a4
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/metric_space/cantor_scheme.lean
eea5317cc3187bcdf177486b8e798207d455451e
[ "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
6,751
lean
/- Copyright (c) 2023 Felix Weilacher. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Felix Weilacher -/ import topology.metric_space.pi_nat /-! # (Topological) Schemes and their induced maps > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In topology, and especially descriptive set theory, one often constructs functions `(β„• β†’ Ξ²) β†’ Ξ±`, where Ξ± is some topological space and Ξ² is a discrete space, as an appropriate limit of some map `list Ξ² β†’ set Ξ±`. We call the latter type of map a "`Ξ²`-scheme on `Ξ±`". This file develops the basic, abstract theory of these schemes and the functions they induce. ## Main Definitions * `cantor_scheme.induced_map A` : The aforementioned "limit" of a scheme `A : list Ξ² β†’ set Ξ±`. This is a partial function from `β„• β†’ Ξ²` to `a`, implemented here as an object of type `Ξ£ s : set (β„• β†’ Ξ²), s β†’ Ξ±`. That is, `(induced_map A).1` is the domain and `(induced_map A).2` is the function. ## Implementation Notes We consider end-appending to be the fundamental way to build lists (say on `Ξ²`) inductively, as this interacts better with the topology on `β„• β†’ Ξ²`. As a result, functions like `list.nth` or `stream.take` do not have their intended meaning in this file. See instead `pi_nat.res`. ## References * [kechris1995] (Chapters 6-7) ## Tags scheme, cantor scheme, lusin scheme, approximation. -/ namespace cantor_scheme open list function filter set pi_nat open_locale classical topology variables {Ξ² Ξ± : Type*} (A : list Ξ² β†’ set Ξ±) /-- From a `Ξ²`-scheme on `Ξ±` `A`, we define a partial function from `(β„• β†’ Ξ²)` to `Ξ±` which sends each infinite sequence `x` to an element of the intersection along the branch corresponding to `x`, if it exists. We call this the map induced by the scheme. -/ noncomputable def induced_map : Ξ£ s : set (β„• β†’ Ξ²), s β†’ Ξ± := ⟨λ x, set.nonempty β‹‚ n : β„•, A (res x n), Ξ» x, x.property.some⟩ section topology /-- A scheme is antitone if each set contains its children. -/ protected def antitone : Prop := βˆ€ l : list Ξ², βˆ€ a : Ξ², A (a :: l) βŠ† A l /-- A useful strengthening of being antitone is to require that each set contains the closure of each of its children. -/ def closure_antitone [topological_space Ξ±] : Prop := βˆ€ l : list Ξ², βˆ€ a : Ξ², closure (A (a :: l)) βŠ† A l /-- A scheme is disjoint if the children of each set of pairwise disjoint. -/ protected def disjoint : Prop := βˆ€ l : list Ξ², _root_.pairwise $ Ξ» a b, disjoint (A (a :: l)) (A (b :: l)) variable {A} /-- If `x` is in the domain of the induced map of a scheme `A`, its image under this map is in each set along the corresponding branch. -/ lemma map_mem (x : (induced_map A).1) (n : β„•) : (induced_map A).2 x ∈ A (res x n) := begin have := x.property.some_mem, rw mem_Inter at this, exact this n, end protected lemma closure_antitone.antitone [topological_space Ξ±] (hA : closure_antitone A) : cantor_scheme.antitone A := Ξ» l a, subset_closure.trans (hA l a) protected lemma antitone.closure_antitone [topological_space Ξ±] (hanti : cantor_scheme.antitone A) (hclosed : βˆ€ l, is_closed (A l)) : closure_antitone A := Ξ» l a, (hclosed _).closure_eq.subset.trans (hanti _ _) /-- A scheme where the children of each set are pairwise disjoint induces an injective map. -/ theorem disjoint.map_injective (hA : cantor_scheme.disjoint A) : injective (induced_map A).2 := begin rintros ⟨x, hx⟩ ⟨y, hy⟩ hxy, refine subtype.coe_injective (res_injective _), dsimp, ext n : 1, induction n with n ih, { simp }, simp only [res_succ], refine ⟨_, ih⟩, contrapose hA, simp only [cantor_scheme.disjoint, _root_.pairwise, ne.def, not_forall, exists_prop], refine ⟨res x n, _, _, hA, _⟩, rw not_disjoint_iff, refine ⟨(induced_map A).2 ⟨x, hx⟩, _, _⟩, { rw ← res_succ, apply map_mem, }, rw [hxy, ih, ← res_succ], apply map_mem, end end topology section metric variable [pseudo_metric_space Ξ±] variable (A) /-- A scheme on a metric space has vanishing diameter if diameter approaches 0 along each branch. -/ def vanishing_diam : Prop := βˆ€ x : β„• β†’ Ξ², tendsto (Ξ» n : β„•, emetric.diam (A (res x n))) at_top (𝓝 0) variable {A} lemma vanishing_diam.dist_lt (hA : vanishing_diam A) (Ξ΅ : ℝ) (Ξ΅_pos : 0 < Ξ΅) (x : β„• β†’ Ξ²) : βˆƒ n : β„•, βˆ€ y z ∈ A (res x n), dist y z < Ξ΅ := begin specialize hA x, rw ennreal.tendsto_at_top_zero at hA, cases hA (ennreal.of_real (Ξ΅ / 2)) (by { simp only [gt_iff_lt, ennreal.of_real_pos], linarith }) with n hn, use n, intros y hy z hz, rw [← ennreal.of_real_lt_of_real_iff Ξ΅_pos, ← edist_dist], apply lt_of_le_of_lt (emetric.edist_le_diam_of_mem hy hz), apply lt_of_le_of_lt (hn _ (le_refl _)), rw ennreal.of_real_lt_of_real_iff Ξ΅_pos, linarith, end /-- A scheme with vanishing diameter along each branch induces a continuous map. -/ theorem vanishing_diam.map_continuous [topological_space Ξ²] [discrete_topology Ξ²] (hA : vanishing_diam A) : continuous (induced_map A).2 := begin rw metric.continuous_iff', rintros ⟨x, hx⟩ Ξ΅ Ξ΅_pos, cases hA.dist_lt _ Ξ΅_pos x with n hn, rw _root_.eventually_nhds_iff, refine ⟨coe ⁻¹' cylinder x n, _, _, by simp⟩, { rintros ⟨y, hy⟩ hyx, rw [mem_preimage, subtype.coe_mk, cylinder_eq_res, mem_set_of] at hyx, apply hn, { rw ← hyx, apply map_mem, }, apply map_mem, }, apply continuous_subtype_coe.is_open_preimage, apply is_open_cylinder, end /-- A scheme on a complete space with vanishing diameter such that each set contains the closure of its children induces a total map. -/ theorem closure_antitone.map_of_vanishing_diam [complete_space Ξ±] (hdiam : vanishing_diam A) (hanti : closure_antitone A) (hnonempty : βˆ€ l, (A l).nonempty) : (induced_map A).1 = univ := begin rw eq_univ_iff_forall, intro x, choose u hu using Ξ» n, hnonempty (res x n), have umem : βˆ€ n m : β„•, n ≀ m β†’ u m ∈ A (res x n), { have : antitone (Ξ» n : β„•, A (res x n)), { refine antitone_nat_of_succ_le _, intro n, apply hanti.antitone, }, intros n m hnm, exact this hnm (hu _), }, have : cauchy_seq u, { rw metric.cauchy_seq_iff, intros Ξ΅ Ξ΅_pos, cases hdiam.dist_lt _ Ξ΅_pos x with n hn, use n, intros mβ‚€ hmβ‚€ m₁ hm₁, apply hn; apply umem; assumption, }, cases cauchy_seq_tendsto_of_complete this with y hy, use y, rw mem_Inter, intro n, apply hanti _ (x n), apply mem_closure_of_tendsto hy, rw eventually_at_top, exact ⟨n.succ, umem _⟩, end end metric end cantor_scheme
d5614c7181169e4227ab2f89c6630079a842f4d6
9dc8cecdf3c4634764a18254e94d43da07142918
/src/group_theory/order_of_element.lean
38672cc20d2044bd8e59f77a86252b691e4ffa37
[ "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
34,087
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, Julian Kuelshammer -/ import algebra.hom.iterate import data.nat.modeq import data.set.pointwise import dynamics.periodic_pts import group_theory.index /-! # Order of an element This file defines the order of an element of a finite group. For a finite group `G` the order of `x ∈ G` is the minimal `n β‰₯ 1` such that `x ^ n = 1`. ## Main definitions * `is_of_fin_order` is a predicate on an element `x` of a monoid `G` saying that `x` is of finite order. * `is_of_fin_add_order` is the additive analogue of `is_of_fin_order`. * `order_of x` defines the order of an element `x` of a monoid `G`, by convention its value is `0` if `x` has infinite order. * `add_order_of` is the additive analogue of `order_of`. ## Tags order of an element -/ open function nat open_locale pointwise universes u v variables {G : Type u} {A : Type v} variables {x y : G} {a b : A} {n m : β„•} section monoid_add_monoid variables [monoid G] [add_monoid A] section is_of_fin_order @[to_additive] lemma is_periodic_pt_mul_iff_pow_eq_one (x : G) : is_periodic_pt ((*) x) n 1 ↔ x ^ n = 1 := by rw [is_periodic_pt, is_fixed_pt, mul_left_iterate, mul_one] /-- `is_of_fin_add_order` is a predicate on an element `a` of an additive monoid to be of finite order, i.e. there exists `n β‰₯ 1` such that `n β€’ a = 0`.-/ def is_of_fin_add_order (a : A) : Prop := (0 : A) ∈ periodic_pts ((+) a) /-- `is_of_fin_order` is a predicate on an element `x` of a monoid to be of finite order, i.e. there exists `n β‰₯ 1` such that `x ^ n = 1`.-/ @[to_additive is_of_fin_add_order] def is_of_fin_order (x : G) : Prop := (1 : G) ∈ periodic_pts ((*) x) lemma is_of_fin_add_order_of_mul_iff : is_of_fin_add_order (additive.of_mul x) ↔ is_of_fin_order x := iff.rfl lemma is_of_fin_order_of_add_iff : is_of_fin_order (multiplicative.of_add a) ↔ is_of_fin_add_order a := iff.rfl @[to_additive is_of_fin_add_order_iff_nsmul_eq_zero] lemma is_of_fin_order_iff_pow_eq_one (x : G) : is_of_fin_order x ↔ βˆƒ n, 0 < n ∧ x ^ n = 1 := by { convert iff.rfl, simp [is_periodic_pt_mul_iff_pow_eq_one] } /-- Elements of finite order are of finite order in submonoids.-/ @[to_additive is_of_fin_add_order_iff_coe "Elements of finite order are of finite order in submonoids."] lemma is_of_fin_order_iff_coe (H : submonoid G) (x : H) : is_of_fin_order x ↔ is_of_fin_order (x : G) := by { rw [is_of_fin_order_iff_pow_eq_one, is_of_fin_order_iff_pow_eq_one], norm_cast } /-- The image of an element of finite order has finite order. -/ @[to_additive add_monoid_hom.is_of_fin_order "The image of an element of finite additive order has finite additive order."] lemma monoid_hom.is_of_fin_order {H : Type v} [monoid H] (f : G β†’* H) {x : G} (h : is_of_fin_order x) : is_of_fin_order $ f x := (is_of_fin_order_iff_pow_eq_one _).mpr $ begin rcases (is_of_fin_order_iff_pow_eq_one _).mp h with ⟨n, npos, hn⟩, exact ⟨n, npos, by rw [←f.map_pow, hn, f.map_one]⟩, end /-- If a direct product has finite order then so does each component. -/ @[to_additive "If a direct product has finite additive order then so does each component."] lemma is_of_fin_order.apply {Ξ· : Type*} {Gs : Ξ· β†’ Type*} [βˆ€ i, monoid (Gs i)] {x : Ξ  i, Gs i} (h : is_of_fin_order x) : βˆ€ i, is_of_fin_order (x i) := begin rcases (is_of_fin_order_iff_pow_eq_one _).mp h with ⟨n, npos, hn⟩, exact Ξ» _, (is_of_fin_order_iff_pow_eq_one _).mpr ⟨n, npos, (congr_fun hn.symm _).symm⟩, end /-- 1 is of finite order in any monoid. -/ @[to_additive "0 is of finite order in any additive monoid."] lemma is_of_fin_order_one : is_of_fin_order (1 : G) := (is_of_fin_order_iff_pow_eq_one 1).mpr ⟨1, _root_.one_pos, one_pow 1⟩ end is_of_fin_order /-- `order_of x` is the order of the element `x`, i.e. the `n β‰₯ 1`, s.t. `x ^ n = 1` if it exists. Otherwise, i.e. if `x` is of infinite order, then `order_of x` is `0` by convention.-/ @[to_additive add_order_of "`add_order_of a` is the order of the element `a`, i.e. the `n β‰₯ 1`, s.t. `n β€’ a = 0` if it exists. Otherwise, i.e. if `a` is of infinite order, then `add_order_of a` is `0` by convention."] noncomputable def order_of (x : G) : β„• := minimal_period ((*) x) 1 @[simp] lemma add_order_of_of_mul_eq_order_of (x : G) : add_order_of (additive.of_mul x) = order_of x := rfl @[simp] lemma order_of_of_add_eq_add_order_of (a : A) : order_of (multiplicative.of_add a) = add_order_of a := rfl @[to_additive add_order_of_pos'] lemma order_of_pos' (h : is_of_fin_order x) : 0 < order_of x := minimal_period_pos_of_mem_periodic_pts h @[to_additive add_order_of_nsmul_eq_zero] lemma pow_order_of_eq_one (x : G) : x ^ order_of x = 1 := begin convert is_periodic_pt_minimal_period ((*) x) _, rw [order_of, mul_left_iterate, mul_one], end @[to_additive add_order_of_eq_zero] lemma order_of_eq_zero (h : Β¬ is_of_fin_order x) : order_of x = 0 := by rwa [order_of, minimal_period, dif_neg] @[to_additive add_order_of_eq_zero_iff] lemma order_of_eq_zero_iff : order_of x = 0 ↔ Β¬ is_of_fin_order x := ⟨λ h H, (order_of_pos' H).ne' h, order_of_eq_zero⟩ @[to_additive add_order_of_eq_zero_iff'] lemma order_of_eq_zero_iff' : order_of x = 0 ↔ βˆ€ n : β„•, 0 < n β†’ x ^ n β‰  1 := by simp_rw [order_of_eq_zero_iff, is_of_fin_order_iff_pow_eq_one, not_exists, not_and] /-- A group element has finite order iff its order is positive. -/ @[to_additive add_order_of_pos_iff "A group element has finite additive order iff its order is positive."] lemma order_of_pos_iff : 0 < order_of x ↔ is_of_fin_order x := by rwa [iff_not_comm.mp order_of_eq_zero_iff, pos_iff_ne_zero] @[to_additive nsmul_ne_zero_of_lt_add_order_of'] lemma pow_ne_one_of_lt_order_of' (n0 : n β‰  0) (h : n < order_of x) : x ^ n β‰  1 := Ξ» j, not_is_periodic_pt_of_pos_of_lt_minimal_period n0 h ((is_periodic_pt_mul_iff_pow_eq_one x).mpr j) @[to_additive add_order_of_le_of_nsmul_eq_zero] lemma order_of_le_of_pow_eq_one (hn : 0 < n) (h : x ^ n = 1) : order_of x ≀ n := is_periodic_pt.minimal_period_le hn (by rwa is_periodic_pt_mul_iff_pow_eq_one) @[simp, to_additive] lemma order_of_one : order_of (1 : G) = 1 := by rw [order_of, one_mul_eq_id, minimal_period_id] @[simp, to_additive add_monoid.order_of_eq_one_iff] lemma order_of_eq_one_iff : order_of x = 1 ↔ x = 1 := by rw [order_of, is_fixed_point_iff_minimal_period_eq_one, is_fixed_pt, mul_one] @[to_additive nsmul_eq_mod_add_order_of] lemma pow_eq_mod_order_of {n : β„•} : x ^ n = x ^ (n % order_of x) := calc x ^ n = x ^ (n % order_of x + order_of x * (n / order_of x)) : by rw [nat.mod_add_div] ... = x ^ (n % order_of x) : by simp [pow_add, pow_mul, pow_order_of_eq_one] @[to_additive add_order_of_dvd_of_nsmul_eq_zero] lemma order_of_dvd_of_pow_eq_one (h : x ^ n = 1) : order_of x ∣ n := is_periodic_pt.minimal_period_dvd ((is_periodic_pt_mul_iff_pow_eq_one _).mpr h) @[to_additive add_order_of_dvd_iff_nsmul_eq_zero] lemma order_of_dvd_iff_pow_eq_one {n : β„•} : order_of x ∣ n ↔ x ^ n = 1 := ⟨λ h, by rw [pow_eq_mod_order_of, nat.mod_eq_zero_of_dvd h, pow_zero], order_of_dvd_of_pow_eq_one⟩ @[to_additive add_order_of_map_dvd] lemma order_of_map_dvd {H : Type*} [monoid H] (ψ : G β†’* H) (x : G) : order_of (ψ x) ∣ order_of x := by { apply order_of_dvd_of_pow_eq_one, rw [←map_pow, pow_order_of_eq_one], apply map_one } @[to_additive] lemma exists_pow_eq_self_of_coprime (h : n.coprime (order_of x)) : βˆƒ m : β„•, (x ^ n) ^ m = x := begin by_cases h0 : order_of x = 0, { rw [h0, coprime_zero_right] at h, exact ⟨1, by rw [h, pow_one, pow_one]⟩ }, by_cases h1 : order_of x = 1, { exact ⟨0, by rw [order_of_eq_one_iff.mp h1, one_pow, one_pow]⟩ }, obtain ⟨m, hm⟩ := exists_mul_mod_eq_one_of_coprime h (one_lt_iff_ne_zero_and_ne_one.mpr ⟨h0, h1⟩), exact ⟨m, by rw [←pow_mul, pow_eq_mod_order_of, hm, pow_one]⟩, end /-- If `x^n = 1`, but `x^(n/p) β‰  1` for all prime factors `p` of `n`, then `x` has order `n` in `G`. -/ @[to_additive add_order_of_eq_of_nsmul_and_div_prime_nsmul "If `n * x = 0`, but `n/p * x β‰  0` for all prime factors `p` of `n`, then `x` has order `n` in `G`."] theorem order_of_eq_of_pow_and_pow_div_prime (hn : 0 < n) (hx : x^n = 1) (hd : βˆ€ p : β„•, p.prime β†’ p ∣ n β†’ x^(n/p) β‰  1) : order_of x = n := begin -- Let `a` be `n/(order_of x)`, and show `a = 1` cases exists_eq_mul_right_of_dvd (order_of_dvd_of_pow_eq_one hx) with a ha, suffices : a = 1, by simp [this, ha], -- Assume `a` is not one... by_contra, have a_min_fac_dvd_p_sub_one : a.min_fac ∣ n, { obtain ⟨b, hb⟩ : βˆƒ (b : β„•), a = b * a.min_fac := exists_eq_mul_left_of_dvd a.min_fac_dvd, rw [hb, ←mul_assoc] at ha, exact dvd.intro_left (order_of x * b) ha.symm, }, -- Use the minimum prime factor of `a` as `p`. refine hd a.min_fac (nat.min_fac_prime h) a_min_fac_dvd_p_sub_one _, rw [←order_of_dvd_iff_pow_eq_one, nat.dvd_div_iff (a_min_fac_dvd_p_sub_one), ha, mul_comm, nat.mul_dvd_mul_iff_left (order_of_pos' _)], { exact nat.min_fac_dvd a, }, { rw is_of_fin_order_iff_pow_eq_one, exact Exists.intro n (id ⟨hn, hx⟩) }, end @[to_additive add_order_of_eq_add_order_of_iff] lemma order_of_eq_order_of_iff {H : Type*} [monoid H] {y : H} : order_of x = order_of y ↔ βˆ€ n : β„•, x ^ n = 1 ↔ y ^ n = 1 := by simp_rw [← is_periodic_pt_mul_iff_pow_eq_one, ← minimal_period_eq_minimal_period_iff, order_of] @[to_additive add_order_of_injective] lemma order_of_injective {H : Type*} [monoid H] (f : G β†’* H) (hf : function.injective f) (x : G) : order_of (f x) = order_of x := by simp_rw [order_of_eq_order_of_iff, ←f.map_pow, ←f.map_one, hf.eq_iff, iff_self, forall_const] @[simp, norm_cast, to_additive] lemma order_of_submonoid {H : submonoid G} (y : H) : order_of (y : G) = order_of y := order_of_injective H.subtype subtype.coe_injective y @[to_additive] lemma order_of_units {y : GΛ£} : order_of (y : G) = order_of y := order_of_injective (units.coe_hom G) units.ext y variables (x) @[to_additive add_order_of_nsmul'] lemma order_of_pow' (h : n β‰  0) : order_of (x ^ n) = order_of x / gcd (order_of x) n := begin convert minimal_period_iterate_eq_div_gcd h, simp only [order_of, mul_left_iterate], end variables (a) (n) @[to_additive add_order_of_nsmul''] lemma order_of_pow'' (h : is_of_fin_order x) : order_of (x ^ n) = order_of x / gcd (order_of x) n := begin convert minimal_period_iterate_eq_div_gcd' h, simp only [order_of, mul_left_iterate], end @[to_additive] lemma commute.order_of_mul_dvd_lcm {x y : G} (h : commute x y) : order_of (x * y) ∣ nat.lcm (order_of x) (order_of y) := begin convert function.commute.minimal_period_of_comp_dvd_lcm h.function_commute_mul_left, rw [order_of, comp_mul_left], end @[to_additive add_order_of_add_dvd_mul_add_order_of] lemma commute.order_of_mul_dvd_mul_order_of {x y : G} (h : commute x y) : order_of (x * y) ∣ (order_of x) * (order_of y) := dvd_trans h.order_of_mul_dvd_lcm (lcm_dvd_mul _ _) @[to_additive add_order_of_add_eq_mul_add_order_of_of_coprime] lemma commute.order_of_mul_eq_mul_order_of_of_coprime {x y : G} (h : commute x y) (hco : nat.coprime (order_of x) (order_of y)) : order_of (x * y) = (order_of x) * (order_of y) := begin convert h.function_commute_mul_left.minimal_period_of_comp_eq_mul_of_coprime hco, simp only [order_of, comp_mul_left], end /-- Commuting elements of finite order are closed under multiplication. -/ @[to_additive "Commuting elements of finite additive order are closed under addition."] lemma commute.is_of_fin_order_mul {x} (h : commute x y) (hx : is_of_fin_order x) (hy : is_of_fin_order y) : is_of_fin_order (x * y) := order_of_pos_iff.mp $ pos_of_dvd_of_pos h.order_of_mul_dvd_mul_order_of $ mul_pos (order_of_pos' hx) (order_of_pos' hy) section p_prime variables {a x n} {p : β„•} [hp : fact p.prime] include hp @[to_additive add_order_of_eq_prime] lemma order_of_eq_prime (hg : x ^ p = 1) (hg1 : x β‰  1) : order_of x = p := minimal_period_eq_prime ((is_periodic_pt_mul_iff_pow_eq_one _).mpr hg) (by rwa [is_fixed_pt, mul_one]) @[to_additive add_order_of_eq_prime_pow] lemma order_of_eq_prime_pow (hnot : Β¬ x ^ p ^ n = 1) (hfin : x ^ p ^ (n + 1) = 1) : order_of x = p ^ (n + 1) := begin apply minimal_period_eq_prime_pow; rwa is_periodic_pt_mul_iff_pow_eq_one, end @[to_additive exists_add_order_of_eq_prime_pow_iff] lemma exists_order_of_eq_prime_pow_iff : (βˆƒ k : β„•, order_of x = p ^ k) ↔ (βˆƒ m : β„•, x ^ (p : β„•) ^ m = 1) := ⟨λ ⟨k, hk⟩, ⟨k, by rw [←hk, pow_order_of_eq_one]⟩, Ξ» ⟨_, hm⟩, begin obtain ⟨k, _, hk⟩ := (nat.dvd_prime_pow hp.elim).mp (order_of_dvd_of_pow_eq_one hm), exact ⟨k, hk⟩, end⟩ omit hp -- An example on how to determine the order of an element of a finite group. example : order_of (-1 : β„€Λ£) = 2 := order_of_eq_prime (int.units_sq _) dec_trivial end p_prime end monoid_add_monoid section cancel_monoid variables [left_cancel_monoid G] (x y) @[to_additive nsmul_injective_of_lt_add_order_of] lemma pow_injective_of_lt_order_of (hn : n < order_of x) (hm : m < order_of x) (eq : x ^ n = x ^ m) : n = m := eq_of_lt_minimal_period_of_iterate_eq hn hm (by simpa only [mul_left_iterate, mul_one]) @[to_additive mem_multiples_iff_mem_range_add_order_of'] lemma mem_powers_iff_mem_range_order_of' [decidable_eq G] (hx : 0 < order_of x) : y ∈ submonoid.powers x ↔ y ∈ (finset.range (order_of x)).image ((^) x : β„• β†’ G) := finset.mem_range_iff_mem_finset_range_of_mod_eq' hx (Ξ» i, pow_eq_mod_order_of.symm) lemma pow_eq_one_iff_modeq : x ^ n = 1 ↔ n ≑ 0 [MOD (order_of x)] := by rw [modeq_zero_iff_dvd, order_of_dvd_iff_pow_eq_one] lemma pow_eq_pow_iff_modeq : x ^ n = x ^ m ↔ n ≑ m [MOD (order_of x)] := begin wlog hmn : m ≀ n, obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le hmn, rw [← mul_one (x ^ m), pow_add, mul_left_cancel_iff, pow_eq_one_iff_modeq], exact ⟨λ h, nat.modeq.add_left _ h, Ξ» h, nat.modeq.add_left_cancel' _ h⟩, end end cancel_monoid section group variables [group G] [add_group A] {x a} {i : β„€} /-- Inverses of elements of finite order have finite order. -/ @[to_additive "Inverses of elements of finite additive order have finite additive order."] lemma is_of_fin_order.inv {x : G} (hx : is_of_fin_order x) : is_of_fin_order x⁻¹ := (is_of_fin_order_iff_pow_eq_one _).mpr $ begin rcases (is_of_fin_order_iff_pow_eq_one x).mp hx with ⟨n, npos, hn⟩, refine ⟨n, npos, by simp_rw [inv_pow, hn, inv_one]⟩, end /-- Inverses of elements of finite order have finite order. -/ @[simp, to_additive "Inverses of elements of finite additive order have finite additive order."] lemma is_of_fin_order_inv_iff {x : G} : is_of_fin_order x⁻¹ ↔ is_of_fin_order x := ⟨λ h, inv_inv x β–Έ h.inv, is_of_fin_order.inv⟩ @[to_additive add_order_of_dvd_iff_zsmul_eq_zero] lemma order_of_dvd_iff_zpow_eq_one : (order_of x : β„€) ∣ i ↔ x ^ i = 1 := begin rcases int.eq_coe_or_neg i with ⟨i, rfl|rfl⟩, { rw [int.coe_nat_dvd, order_of_dvd_iff_pow_eq_one, zpow_coe_nat] }, { rw [dvd_neg, int.coe_nat_dvd, zpow_neg, inv_eq_one, zpow_coe_nat, order_of_dvd_iff_pow_eq_one] } end @[simp, to_additive] lemma order_of_inv (x : G) : order_of x⁻¹ = order_of x := by simp [order_of_eq_order_of_iff] @[simp, norm_cast, to_additive] lemma order_of_subgroup {H : subgroup G} (y: H) : order_of (y : G) = order_of y := order_of_injective H.subtype subtype.coe_injective y @[to_additive zsmul_eq_mod_add_order_of] lemma zpow_eq_mod_order_of : x ^ i = x ^ (i % order_of x) := calc x ^ i = x ^ (i % order_of x + order_of x * (i / order_of x)) : by rw [int.mod_add_div] ... = x ^ (i % order_of x) : by simp [zpow_add, zpow_mul, pow_order_of_eq_one] @[to_additive nsmul_inj_iff_of_add_order_of_eq_zero] lemma pow_inj_iff_of_order_of_eq_zero (h : order_of x = 0) {n m : β„•} : x ^ n = x ^ m ↔ n = m := begin rw [order_of_eq_zero_iff, is_of_fin_order_iff_pow_eq_one] at h, push_neg at h, induction n with n IH generalizing m, { cases m, { simp }, { simpa [eq_comm] using h m.succ m.zero_lt_succ } }, { cases m, { simpa using h n.succ n.zero_lt_succ }, { simp [pow_succ, IH] } } end @[to_additive] lemma pow_inj_mod {n m : β„•} : x ^ n = x ^ m ↔ n % order_of x = m % order_of x := begin cases (order_of x).zero_le.eq_or_lt with hx hx, { simp [pow_inj_iff_of_order_of_eq_zero, hx.symm] }, rw [pow_eq_mod_order_of, @pow_eq_mod_order_of _ _ _ m], exact ⟨pow_injective_of_lt_order_of _ (nat.mod_lt _ hx) (nat.mod_lt _ hx), Ξ» h, congr_arg _ h⟩ end end group section comm_monoid variables [comm_monoid G] /-- Elements of finite order are closed under multiplication. -/ @[to_additive "Elements of finite additive order are closed under addition."] lemma is_of_fin_order.mul (hx : is_of_fin_order x) (hy : is_of_fin_order y) : is_of_fin_order (x * y) := (commute.all x y).is_of_fin_order_mul hx hy end comm_monoid section finite_monoid variables [monoid G] open_locale big_operators @[to_additive sum_card_add_order_of_eq_card_nsmul_eq_zero] lemma sum_card_order_of_eq_card_pow_eq_one [fintype G] [decidable_eq G] (hn : 0 < n) : βˆ‘ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (Ξ» x : G, order_of x = m)).card = (finset.univ.filter (Ξ» x : G, x ^ n = 1)).card := calc βˆ‘ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (Ξ» x : G, order_of x = m)).card = _ : (finset.card_bUnion (by { intros, apply finset.disjoint_filter.2, cc })).symm ... = _ : congr_arg finset.card (finset.ext (begin assume x, suffices : order_of x ≀ n ∧ order_of x ∣ n ↔ x ^ n = 1, { simpa [nat.lt_succ_iff], }, exact ⟨λ h, let ⟨m, hm⟩ := h.2 in by rw [hm, pow_mul, pow_order_of_eq_one, one_pow], Ξ» h, ⟨order_of_le_of_pow_eq_one hn h, order_of_dvd_of_pow_eq_one h⟩⟩ end)) end finite_monoid section finite_cancel_monoid -- TODO: Of course everything also works for right_cancel_monoids. variables [left_cancel_monoid G] [add_left_cancel_monoid A] -- TODO: Use this to show that a finite left cancellative monoid is a group. @[to_additive] lemma exists_pow_eq_one [finite G] (x : G) : is_of_fin_order x := begin refine (is_of_fin_order_iff_pow_eq_one _).mpr _, obtain ⟨i, j, a_eq, ne⟩ : βˆƒ(i j : β„•), x ^ i = x ^ j ∧ i β‰  j := by simpa only [not_forall, exists_prop, injective] using (not_injective_infinite_finite (Ξ»i:β„•, x^i)), wlog h'' : j ≀ i, refine ⟨i - j, tsub_pos_of_lt (lt_of_le_of_ne h'' ne.symm), mul_right_injective (x^j) _⟩, rw [mul_one, ← pow_add, ← a_eq, add_tsub_cancel_of_le h''], end @[to_additive add_order_of_le_card_univ] lemma order_of_le_card_univ [fintype G] : order_of x ≀ fintype.card G := finset.le_card_of_inj_on_range ((^) x) (assume n _, finset.mem_univ _) (assume i hi j hj, pow_injective_of_lt_order_of x hi hj) /-- This is the same as `order_of_pos' but with one fewer explicit assumption since this is automatic in case of a finite cancellative monoid.-/ @[to_additive add_order_of_pos "This is the same as `add_order_of_pos' but with one fewer explicit assumption since this is automatic in case of a finite cancellative additive monoid."] lemma order_of_pos [finite G] (x : G) : 0 < order_of x := order_of_pos' (exists_pow_eq_one x) open nat /-- This is the same as `order_of_pow'` and `order_of_pow''` but with one assumption less which is automatic in the case of a finite cancellative monoid.-/ @[to_additive add_order_of_nsmul "This is the same as `add_order_of_nsmul'` and `add_order_of_nsmul` but with one assumption less which is automatic in the case of a finite cancellative additive monoid."] lemma order_of_pow [finite G] (x : G) : order_of (x ^ n) = order_of x / gcd (order_of x) n := order_of_pow'' _ _ (exists_pow_eq_one _) @[to_additive mem_multiples_iff_mem_range_add_order_of] lemma mem_powers_iff_mem_range_order_of [finite G] [decidable_eq G] : y ∈ submonoid.powers x ↔ y ∈ (finset.range (order_of x)).image ((^) x : β„• β†’ G) := finset.mem_range_iff_mem_finset_range_of_mod_eq' (order_of_pos x) (assume i, pow_eq_mod_order_of.symm) @[to_additive decidable_multiples] noncomputable instance decidable_powers : decidable_pred (∈ submonoid.powers x) := classical.dec_pred _ /--The equivalence between `fin (order_of x)` and `submonoid.powers x`, sending `i` to `x ^ i`."-/ @[to_additive fin_equiv_multiples "The equivalence between `fin (add_order_of a)` and `add_submonoid.multiples a`, sending `i` to `i β€’ a`."] noncomputable def fin_equiv_powers [finite G] (x : G) : fin (order_of x) ≃ (submonoid.powers x : set G) := equiv.of_bijective (Ξ» n, ⟨x ^ ↑n, ⟨n, rfl⟩⟩) ⟨λ ⟨i, hi⟩ ⟨j, hj⟩ ij, fin.ext (pow_injective_of_lt_order_of x hi hj (subtype.mk_eq_mk.1 ij)), Ξ» ⟨_, i, rfl⟩, ⟨⟨i % order_of x, mod_lt i (order_of_pos x)⟩, subtype.eq pow_eq_mod_order_of.symm⟩⟩ @[simp, to_additive fin_equiv_multiples_apply] lemma fin_equiv_powers_apply [finite G] {x : G} {n : fin (order_of x)} : fin_equiv_powers x n = ⟨x ^ ↑n, n, rfl⟩ := rfl @[simp, to_additive fin_equiv_multiples_symm_apply] lemma fin_equiv_powers_symm_apply [finite G] (x : G) (n : β„•) {hn : βˆƒ (m : β„•), x ^ m = x ^ n} : ((fin_equiv_powers x).symm ⟨x ^ n, hn⟩) = ⟨n % order_of x, nat.mod_lt _ (order_of_pos x)⟩ := by rw [equiv.symm_apply_eq, fin_equiv_powers_apply, subtype.mk_eq_mk, pow_eq_mod_order_of, fin.coe_mk] /-- The equivalence between `submonoid.powers` of two elements `x, y` of the same order, mapping `x ^ i` to `y ^ i`. -/ @[to_additive multiples_equiv_multiples "The equivalence between `submonoid.multiples` of two elements `a, b` of the same additive order, mapping `i β€’ a` to `i β€’ b`."] noncomputable def powers_equiv_powers [finite G] (h : order_of x = order_of y) : (submonoid.powers x : set G) ≃ (submonoid.powers y : set G) := (fin_equiv_powers x).symm.trans ((fin.cast h).to_equiv.trans (fin_equiv_powers y)) @[simp, to_additive multiples_equiv_multiples_apply] lemma powers_equiv_powers_apply [finite G] (h : order_of x = order_of y) (n : β„•) : powers_equiv_powers h ⟨x ^ n, n, rfl⟩ = ⟨y ^ n, n, rfl⟩ := begin rw [powers_equiv_powers, equiv.trans_apply, equiv.trans_apply, fin_equiv_powers_symm_apply, ← equiv.eq_symm_apply, fin_equiv_powers_symm_apply], simp [h] end @[to_additive add_order_of_eq_card_multiples] lemma order_eq_card_powers [fintype G] : order_of x = fintype.card (submonoid.powers x : set G) := (fintype.card_fin (order_of x)).symm.trans (fintype.card_eq.2 ⟨fin_equiv_powers x⟩) end finite_cancel_monoid section finite_group variables [group G] [add_group A] @[to_additive] lemma exists_zpow_eq_one [finite G] (x : G) : βˆƒ (i : β„€) (H : i β‰  0), x ^ (i : β„€) = 1 := begin rcases exists_pow_eq_one x with ⟨w, hw1, hw2⟩, refine ⟨w, int.coe_nat_ne_zero.mpr (ne_of_gt hw1), _⟩, rw zpow_coe_nat, exact (is_periodic_pt_mul_iff_pow_eq_one _).mp hw2, end open subgroup @[to_additive mem_multiples_iff_mem_zmultiples] lemma mem_powers_iff_mem_zpowers [finite G] : y ∈ submonoid.powers x ↔ y ∈ zpowers x := ⟨λ ⟨n, hn⟩, ⟨n, by simp * at *⟩, Ξ» ⟨i, hi⟩, ⟨(i % order_of x).nat_abs, by rwa [← zpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (order_of_pos x))), ← zpow_eq_mod_order_of]⟩⟩ @[to_additive multiples_eq_zmultiples] lemma powers_eq_zpowers [finite G] (x : G) : (submonoid.powers x : set G) = zpowers x := set.ext $ Ξ» x, mem_powers_iff_mem_zpowers @[to_additive mem_zmultiples_iff_mem_range_add_order_of] lemma mem_zpowers_iff_mem_range_order_of [finite G] [decidable_eq G] : y ∈ subgroup.zpowers x ↔ y ∈ (finset.range (order_of x)).image ((^) x : β„• β†’ G) := by rw [← mem_powers_iff_mem_zpowers, mem_powers_iff_mem_range_order_of] @[to_additive decidable_zmultiples] noncomputable instance decidable_zpowers : decidable_pred (∈ subgroup.zpowers x) := classical.dec_pred _ /-- The equivalence between `fin (order_of x)` and `subgroup.zpowers x`, sending `i` to `x ^ i`. -/ @[to_additive fin_equiv_zmultiples "The equivalence between `fin (add_order_of a)` and `subgroup.zmultiples a`, sending `i` to `i β€’ a`."] noncomputable def fin_equiv_zpowers [finite G] (x : G) : fin (order_of x) ≃ (subgroup.zpowers x : set G) := (fin_equiv_powers x).trans (equiv.set.of_eq (powers_eq_zpowers x)) @[simp, to_additive fin_equiv_zmultiples_apply] lemma fin_equiv_zpowers_apply [finite G] {n : fin (order_of x)} : fin_equiv_zpowers x n = ⟨x ^ (n : β„•), n, zpow_coe_nat x n⟩ := rfl @[simp, to_additive fin_equiv_zmultiples_symm_apply] lemma fin_equiv_zpowers_symm_apply [finite G] (x : G) (n : β„•) {hn : βˆƒ (m : β„€), x ^ m = x ^ n} : ((fin_equiv_zpowers x).symm ⟨x ^ n, hn⟩) = ⟨n % order_of x, nat.mod_lt _ (order_of_pos x)⟩ := by { rw [fin_equiv_zpowers, equiv.symm_trans_apply, equiv.set.of_eq_symm_apply], exact fin_equiv_powers_symm_apply x n } /-- The equivalence between `subgroup.zpowers` of two elements `x, y` of the same order, mapping `x ^ i` to `y ^ i`. -/ @[to_additive zmultiples_equiv_zmultiples "The equivalence between `subgroup.zmultiples` of two elements `a, b` of the same additive order, mapping `i β€’ a` to `i β€’ b`."] noncomputable def zpowers_equiv_zpowers [finite G] (h : order_of x = order_of y) : (subgroup.zpowers x : set G) ≃ (subgroup.zpowers y : set G) := (fin_equiv_zpowers x).symm.trans ((fin.cast h).to_equiv.trans (fin_equiv_zpowers y)) @[simp, to_additive zmultiples_equiv_zmultiples_apply] lemma zpowers_equiv_zpowers_apply [finite G] (h : order_of x = order_of y) (n : β„•) : zpowers_equiv_zpowers h ⟨x ^ n, n, zpow_coe_nat x n⟩ = ⟨y ^ n, n, zpow_coe_nat y n⟩ := begin rw [zpowers_equiv_zpowers, equiv.trans_apply, equiv.trans_apply, fin_equiv_zpowers_symm_apply, ← equiv.eq_symm_apply, fin_equiv_zpowers_symm_apply], simp [h] end variables [fintype G] /-- See also `order_eq_card_zpowers'`. -/ @[to_additive add_order_eq_card_zmultiples "See also `add_order_eq_card_zmultiples'`."] lemma order_eq_card_zpowers : order_of x = fintype.card (zpowers x) := (fintype.card_fin (order_of x)).symm.trans (fintype.card_eq.2 ⟨fin_equiv_zpowers x⟩) open quotient_group /- TODO: use cardinal theory, introduce `card : set G β†’ β„•`, or setup decidability for cosets -/ @[to_additive add_order_of_dvd_card_univ] lemma order_of_dvd_card_univ : order_of x ∣ fintype.card G := begin classical, have ft_prod : fintype ((G β§Έ zpowers x) Γ— zpowers x), from fintype.of_equiv G group_equiv_quotient_times_subgroup, have ft_s : fintype (zpowers x), from @fintype.prod_right _ _ _ ft_prod _, have ft_cosets : fintype (G β§Έ zpowers x), from @fintype.prod_left _ _ _ ft_prod ⟨⟨1, (zpowers x).one_mem⟩⟩, have eq₁ : fintype.card G = @fintype.card _ ft_cosets * @fintype.card _ ft_s, from calc fintype.card G = @fintype.card _ ft_prod : @fintype.card_congr _ _ _ ft_prod group_equiv_quotient_times_subgroup ... = @fintype.card _ (@prod.fintype _ _ ft_cosets ft_s) : congr_arg (@fintype.card _) $ subsingleton.elim _ _ ... = @fintype.card _ ft_cosets * @fintype.card _ ft_s : @fintype.card_prod _ _ ft_cosets ft_s, have eqβ‚‚ : order_of x = @fintype.card _ ft_s, from calc order_of x = _ : order_eq_card_zpowers ... = _ : congr_arg (@fintype.card _) $ subsingleton.elim _ _, exact dvd.intro (@fintype.card (G β§Έ subgroup.zpowers x) ft_cosets) (by rw [eq₁, eqβ‚‚, mul_comm]) end @[simp, to_additive card_nsmul_eq_zero] lemma pow_card_eq_one : x ^ fintype.card G = 1 := let ⟨m, hm⟩ := @order_of_dvd_card_univ _ x _ _ in by simp [hm, pow_mul, pow_order_of_eq_one] @[to_additive] lemma subgroup.pow_index_mem {G : Type*} [group G] (H : subgroup G) [finite (G β§Έ H)] [normal H] (g : G) : g ^ index H ∈ H := by { casesI nonempty_fintype (G β§Έ H), rw [←eq_one_iff, quotient_group.coe_pow H, index_eq_card, pow_card_eq_one] } @[to_additive] lemma pow_eq_mod_card (n : β„•) : x ^ n = x ^ (n % fintype.card G) := by rw [pow_eq_mod_order_of, ←nat.mod_mod_of_dvd n order_of_dvd_card_univ, ← pow_eq_mod_order_of] @[to_additive] lemma zpow_eq_mod_card (n : β„€) : x ^ n = x ^ (n % fintype.card G) := by rw [zpow_eq_mod_order_of, ← int.mod_mod_of_dvd n (int.coe_nat_dvd.2 order_of_dvd_card_univ), ← zpow_eq_mod_order_of] /-- If `gcd(|G|,n)=1` then the `n`th power map is a bijection -/ @[to_additive "If `gcd(|G|,n)=1` then the smul by `n` is a bijection", simps] def pow_coprime (h : nat.coprime (fintype.card G) n) : G ≃ G := { to_fun := Ξ» g, g ^ n, inv_fun := Ξ» g, g ^ (nat.gcd_b (fintype.card G) n), left_inv := Ξ» g, by { have key : g ^ _ = g ^ _ := congr_arg (Ξ» n : β„€, g ^ n) (nat.gcd_eq_gcd_ab (fintype.card G) n), rwa [zpow_add, zpow_mul, zpow_mul, zpow_coe_nat, zpow_coe_nat, zpow_coe_nat, h.gcd_eq_one, pow_one, pow_card_eq_one, one_zpow, one_mul, eq_comm] at key }, right_inv := Ξ» g, by { have key : g ^ _ = g ^ _ := congr_arg (Ξ» n : β„€, g ^ n) (nat.gcd_eq_gcd_ab (fintype.card G) n), rwa [zpow_add, zpow_mul, zpow_mul', zpow_coe_nat, zpow_coe_nat, zpow_coe_nat, h.gcd_eq_one, pow_one, pow_card_eq_one, one_zpow, one_mul, eq_comm] at key } } @[simp, to_additive] lemma pow_coprime_one (h : nat.coprime (fintype.card G) n) : pow_coprime h 1 = 1 := one_pow n @[simp, to_additive] lemma pow_coprime_inv (h : nat.coprime (fintype.card G) n) {g : G} : pow_coprime h g⁻¹ = (pow_coprime h g)⁻¹ := inv_pow g n @[to_additive add_inf_eq_bot_of_coprime] lemma inf_eq_bot_of_coprime {G : Type*} [group G] {H K : subgroup G} [fintype H] [fintype K] (h : nat.coprime (fintype.card H) (fintype.card K)) : H βŠ“ K = βŠ₯ := begin refine (H βŠ“ K).eq_bot_iff_forall.mpr (Ξ» x hx, _), rw [←order_of_eq_one_iff, ←nat.dvd_one, ←h.gcd_eq_one, nat.dvd_gcd_iff], exact ⟨(congr_arg (∣ fintype.card H) (order_of_subgroup ⟨x, hx.1⟩)).mpr order_of_dvd_card_univ, (congr_arg (∣ fintype.card K) (order_of_subgroup ⟨x, hx.2⟩)).mpr order_of_dvd_card_univ⟩, end variable (a) /-- TODO: Generalise to `submonoid.powers`.-/ @[to_additive image_range_add_order_of, nolint to_additive_doc] lemma image_range_order_of [decidable_eq G] : finset.image (Ξ» i, x ^ i) (finset.range (order_of x)) = (zpowers x : set G).to_finset := by { ext x, rw [set.mem_to_finset, set_like.mem_coe, mem_zpowers_iff_mem_range_order_of] } /-- TODO: Generalise to `finite` + `cancel_monoid`. -/ @[to_additive gcd_nsmul_card_eq_zero_iff "TODO: Generalise to `finite` + `cancel_add_monoid`"] lemma pow_gcd_card_eq_one_iff : x ^ n = 1 ↔ x ^ (gcd n (fintype.card G)) = 1 := ⟨λ h, pow_gcd_eq_one _ h $ pow_card_eq_one, Ξ» h, let ⟨m, hm⟩ := gcd_dvd_left n (fintype.card G) in by rw [hm, pow_mul, h, one_pow]⟩ end finite_group section pow_is_subgroup /-- A nonempty idempotent subset of a finite cancellative monoid is a submonoid -/ @[to_additive "A nonempty idempotent subset of a finite cancellative add monoid is a submonoid"] def submonoid_of_idempotent {M : Type*} [left_cancel_monoid M] [fintype M] (S : set M) (hS1 : S.nonempty) (hS2 : S * S = S) : submonoid M := have pow_mem : βˆ€ a : M, a ∈ S β†’ βˆ€ n : β„•, a ^ (n + 1) ∈ S := Ξ» a ha, nat.rec (by rwa [zero_add, pow_one]) (Ξ» n ih, (congr_arg2 (∈) (pow_succ a (n + 1)).symm hS2).mp (set.mul_mem_mul ha ih)), { carrier := S, one_mem' := by { obtain ⟨a, ha⟩ := hS1, rw [←pow_order_of_eq_one a, ← tsub_add_cancel_of_le (succ_le_of_lt (order_of_pos a))], exact pow_mem a ha (order_of a - 1) }, mul_mem' := Ξ» a b ha hb, (congr_arg2 (∈) rfl hS2).mp (set.mul_mem_mul ha hb) } /-- A nonempty idempotent subset of a finite group is a subgroup -/ @[to_additive "A nonempty idempotent subset of a finite add group is a subgroup"] def subgroup_of_idempotent {G : Type*} [group G] [fintype G] (S : set G) (hS1 : S.nonempty) (hS2 : S * S = S) : subgroup G := { carrier := S, inv_mem' := Ξ» a ha, show a⁻¹ ∈ submonoid_of_idempotent S hS1 hS2, by { rw [←one_mul a⁻¹, ←pow_one a, ←pow_order_of_eq_one a, ←pow_sub a (order_of_pos a)], exact pow_mem ha (order_of a - 1) }, .. submonoid_of_idempotent S hS1 hS2 } /-- If `S` is a nonempty subset of a finite group `G`, then `S ^ |G|` is a subgroup -/ @[to_additive smul_card_add_subgroup "If `S` is a nonempty subset of a finite add group `G`, then `|G| β€’ S` is a subgroup", simps] def pow_card_subgroup {G : Type*} [group G] [fintype G] (S : set G) (hS : S.nonempty) : subgroup G := have one_mem : (1 : G) ∈ (S ^ fintype.card G) := by { obtain ⟨a, ha⟩ := hS, rw ← pow_card_eq_one, exact set.pow_mem_pow ha (fintype.card G) }, subgroup_of_idempotent (S ^ (fintype.card G)) ⟨1, one_mem⟩ begin classical!, refine (set.eq_of_subset_of_card_le (set.subset_mul_left _ one_mem) (ge_of_eq _)).symm, simp_rw [← pow_add, group.card_pow_eq_card_pow_card_univ S (fintype.card G) le_rfl, group.card_pow_eq_card_pow_card_univ S (fintype.card G + fintype.card G) le_add_self], end end pow_is_subgroup section linear_ordered_ring variable [linear_ordered_ring G] lemma order_of_abs_ne_one (h : |x| β‰  1) : order_of x = 0 := begin rw order_of_eq_zero_iff', intros n hn hx, replace hx : |x| ^ n = 1 := by simpa only [abs_one, abs_pow] using congr_arg abs hx, cases h.lt_or_lt with h h, { exact ((pow_lt_one (abs_nonneg x) h hn.ne').ne hx).elim }, { exact ((one_lt_pow h hn.ne').ne' hx).elim } end lemma linear_ordered_ring.order_of_le_two : order_of x ≀ 2 := begin cases ne_or_eq (|x|) 1 with h h, { simp [order_of_abs_ne_one h] }, rcases eq_or_eq_neg_of_abs_eq h with rfl | rfl, { simp }, apply order_of_le_of_pow_eq_one; norm_num end end linear_ordered_ring
cdd53cf4342c2333e6cd923997a16c27e83be590
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/inner_product_space/orientation.lean
a9c97374111ffccddcde8be1ead3c5ded684c802
[ "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
2,318
lean
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import analysis.inner_product_space.projection import linear_algebra.orientation /-! # Orientations of real inner product spaces. This file provides definitions and proves lemmas about orientations of real inner product spaces. ## Main definitions * `orientation.fin_orthonormal_basis` is an orthonormal basis, indexed by `fin n`, with the given orientation. -/ noncomputable theory variables {E : Type*} [inner_product_space ℝ E] variables {ΞΉ : Type*} [fintype ΞΉ] [decidable_eq ΞΉ] open finite_dimensional /-- `basis.adjust_to_orientation`, applied to an orthonormal basis, produces an orthonormal basis. -/ lemma orthonormal.orthonormal_adjust_to_orientation [nonempty ΞΉ] {e : basis ΞΉ ℝ E} (h : orthonormal ℝ e) (x : orientation ℝ E ΞΉ) : orthonormal ℝ (e.adjust_to_orientation x) := h.orthonormal_of_forall_eq_or_eq_neg (e.adjust_to_orientation_apply_eq_or_eq_neg x) /-- An orthonormal basis, indexed by `fin n`, with the given orientation. -/ protected def orientation.fin_orthonormal_basis {n : β„•} (hn : 0 < n) (h : finrank ℝ E = n) (x : orientation ℝ E (fin n)) : basis (fin n) ℝ E := begin haveI := fin.pos_iff_nonempty.1 hn, haveI := finite_dimensional_of_finrank (h.symm β–Έ hn : 0 < finrank ℝ E), exact (fin_std_orthonormal_basis h).adjust_to_orientation x end /-- `orientation.fin_orthonormal_basis` is orthonormal. -/ protected lemma orientation.fin_orthonormal_basis_orthonormal {n : β„•} (hn : 0 < n) (h : finrank ℝ E = n) (x : orientation ℝ E (fin n)) : orthonormal ℝ (x.fin_orthonormal_basis hn h) := begin haveI := fin.pos_iff_nonempty.1 hn, haveI := finite_dimensional_of_finrank (h.symm β–Έ hn : 0 < finrank ℝ E), exact (fin_std_orthonormal_basis_orthonormal h).orthonormal_adjust_to_orientation _ end /-- `orientation.fin_orthonormal_basis` gives a basis with the required orientation. -/ @[simp] lemma orientation.fin_orthonormal_basis_orientation {n : β„•} (hn : 0 < n) (h : finrank ℝ E = n) (x : orientation ℝ E (fin n)) : (x.fin_orthonormal_basis hn h).orientation = x := begin haveI := fin.pos_iff_nonempty.1 hn, exact basis.orientation_adjust_to_orientation _ _ end
d955518f95dfd6be07f8d2d1fdc5be15ace29ec1
a4673261e60b025e2c8c825dfa4ab9108246c32e
/src/Lean/Elab/Exception.lean
c7def7780337f2ce46d8518e099963e1fdd58d21
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,126
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.InternalExceptionId import Lean.Meta.Basic namespace Lean.Elab builtin_initialize postponeExceptionId : InternalExceptionId ← registerInternalExceptionId `postpone builtin_initialize unsupportedSyntaxExceptionId : InternalExceptionId ← registerInternalExceptionId `unsupportedSyntax builtin_initialize abortExceptionId : InternalExceptionId ← registerInternalExceptionId `abortElab builtin_initialize unboundImplicitExceptionId : InternalExceptionId ← registerInternalExceptionId `unboundImplicit def throwPostpone [MonadExceptOf Exception m] : m Ξ± := throw $ Exception.internal postponeExceptionId def throwUnsupportedSyntax [MonadExceptOf Exception m] : m Ξ± := throw $ Exception.internal unsupportedSyntaxExceptionId def throwIllFormedSyntax [Monad m] [MonadExceptOf Exception m] [MonadRef m] [AddErrorMessageContext m] : m Ξ± := throwError "ill-formed syntax" def throwUnboundImplicitLocal [MonadExceptOf Exception m] (n : Name) : m Ξ± := throw $ Exception.internal unboundImplicitExceptionId <| KVMap.empty.insert `localId n def isUnboundImplicitLocalException? (ex : Exception) : Option Name := match ex with | Exception.internal id k => if id == unboundImplicitExceptionId then some <| k.getName `localId `x else none | _ => none def throwAlreadyDeclaredUniverseLevel [Monad m] [MonadExceptOf Exception m] [MonadRef m] [AddErrorMessageContext m] (u : Name) : m Ξ± := throwError! "a universe level named '{u}' has already been declared" -- Throw exception to abort elaboration without producing any error message def throwAbort {Ξ± m} [MonadExcept Exception m] : m Ξ± := throw $ Exception.internal abortExceptionId def mkMessageCore (fileName : String) (fileMap : FileMap) (msgData : MessageData) (severity : MessageSeverity) (pos : String.Pos) : Message := let pos := fileMap.toPosition pos { fileName := fileName, pos := pos, data := msgData, severity := severity } end Lean.Elab
82a98ec6f48405b4e52d1252965cd8867150d78c
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/run/sigma_no_confusion.lean
f41b5ca1d87cb632b2ff5bba2d9b205dd0a99197
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,222
lean
import data.sigma namespace sigma namespace manual definition no_confusion_type {A : Type} {B : A β†’ Type} (P : Type) (v₁ vβ‚‚ : sigma B) : Type := rec_on v₁ (Ξ» (a₁ : A) (b₁ : B a₁), rec_on vβ‚‚ (Ξ» (aβ‚‚ : A) (bβ‚‚ : B aβ‚‚), (Ξ  (eq₁ : a₁ = aβ‚‚), eq.rec_on eq₁ b₁ = bβ‚‚ β†’ P) β†’ P)) definition no_confusion {A : Type} {B : A β†’ Type} {P : Type} {v₁ vβ‚‚ : sigma B} : v₁ = vβ‚‚ β†’ no_confusion_type P v₁ vβ‚‚ := assume H₁₂ : v₁ = vβ‚‚, have aux : v₁ = v₁ β†’ no_confusion_type P v₁ v₁, from assume H₁₁, rec_on v₁ (Ξ» (a₁ : A) (b₁ : B a₁) (h : Ξ  (eq₁ : a₁ = a₁), eq.rec_on eq₁ b₁ = b₁ β†’ P), h rfl rfl), eq.rec_on H₁₂ aux H₁₂ end manual theorem sigma.mk.inj_1 {A : Type} {B : A β†’ Type} {a₁ aβ‚‚ : A} {b₁ : B a₁} {bβ‚‚ : B aβ‚‚} (Heq : mk a₁ b₁ = mk aβ‚‚ bβ‚‚) : a₁ = aβ‚‚ := begin apply (no_confusion Heq), intros, assumption end theorem sigma.mk.inj_2 {A : Type} {B : A β†’ Type} (a₁ aβ‚‚ : A) (b₁ : B a₁) (bβ‚‚ : B aβ‚‚) (Heq : mk a₁ b₁ = mk aβ‚‚ bβ‚‚) : b₁ == bβ‚‚ := begin apply (no_confusion Heq), intros, eassumption end end sigma
e984944dc9ddca2a749284c3e4d94dfb90b23930
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/algebra/operations.lean
492885cb7f30917b1299ceaa5d7a9afb604c3dc2
[ "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
25,175
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.algebra.bilinear import algebra.algebra.equiv import algebra.module.submodule.pointwise import algebra.module.submodule.bilinear import algebra.module.opposites import algebra.order.kleene import data.finset.pointwise import data.set.semiring import data.set.pointwise.big_operators import group_theory.group_action.sub_mul_action.pointwise /-! # Multiplication and division of submodules of an algebra. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. An interface for multiplication and division of sub-R-modules of an R-algebra A is developed. ## Main definitions Let `R` be a commutative ring (or semiring) and let `A` be an `R`-algebra. * `1 : submodule R A` : the R-submodule R of the R-algebra A * `has_mul (submodule R A)` : multiplication of two sub-R-modules M and N of A is defined to be the smallest submodule containing all the products `m * n`. * `has_div (submodule R A)` : `I / J` is defined to be the submodule consisting of all `a : A` such that `a β€’ J βŠ† I` It is proved that `submodule R A` is a semiring, and also an algebra over `set A`. Additionally, in the `pointwise` locale we promote `submodule.pointwise_distrib_mul_action` to a `mul_semiring_action` as `submodule.pointwise_mul_semiring_action`. ## Tags multiplication of submodules, division of submodules, submodule semiring -/ universes uΞΉ u v open algebra set mul_opposite open_locale big_operators open_locale pointwise namespace sub_mul_action variables {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] lemma algebra_map_mem (r : R) : algebra_map R A r ∈ (1 : sub_mul_action R A) := ⟨r, (algebra_map_eq_smul_one r).symm⟩ lemma mem_one' {x : A} : x ∈ (1 : sub_mul_action R A) ↔ βˆƒ y, algebra_map R A y = x := exists_congr $ Ξ» r, by rw algebra_map_eq_smul_one end sub_mul_action namespace submodule variables {ΞΉ : Sort uΞΉ} variables {R : Type u} [comm_semiring R] section ring variables {A : Type v} [semiring A] [algebra R A] variables (S T : set A) {M N P Q : submodule R A} {m n : A} /-- `1 : submodule R A` is the submodule R of A. -/ instance : has_one (submodule R A) := ⟨(algebra.linear_map R A).range⟩ theorem one_eq_range : (1 : submodule R A) = (algebra.linear_map R A).range := rfl lemma le_one_to_add_submonoid : 1 ≀ (1 : submodule R A).to_add_submonoid := begin rintros x ⟨n, rfl⟩, exact ⟨n, map_nat_cast (algebra_map R A) n⟩, end lemma algebra_map_mem (r : R) : algebra_map R A r ∈ (1 : submodule R A) := linear_map.mem_range_self _ _ @[simp] lemma mem_one {x : A} : x ∈ (1 : submodule R A) ↔ βˆƒ y, algebra_map R A y = x := iff.rfl @[simp] lemma to_sub_mul_action_one : (1 : submodule R A).to_sub_mul_action = 1 := set_like.ext $ Ξ» x, mem_one.trans sub_mul_action.mem_one'.symm theorem one_eq_span : (1 : submodule R A) = R βˆ™ 1 := begin apply submodule.ext, intro a, simp only [mem_one, mem_span_singleton, algebra.smul_def, mul_one] end theorem one_eq_span_one_set : (1 : submodule R A) = span R 1 := one_eq_span theorem one_le : (1 : submodule R A) ≀ P ↔ (1 : A) ∈ P := by simpa only [one_eq_span, span_le, set.singleton_subset_iff] protected lemma map_one {A'} [semiring A'] [algebra R A'] (f : A →ₐ[R] A') : map f.to_linear_map (1 : submodule R A) = 1 := by { ext, simp } @[simp] lemma map_op_one : map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) (1 : submodule R A) = 1 := by { ext, induction x using mul_opposite.rec, simp } @[simp] lemma comap_op_one : comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) (1 : submodule R Aᡐᡒᡖ) = 1 := by { ext, simp } @[simp] lemma map_unop_one : map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) (1 : submodule R Aᡐᡒᡖ) = 1 := by rw [←comap_equiv_eq_map_symm, comap_op_one] @[simp] lemma comap_unop_one : comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) (1 : submodule R A) = 1 := by rw [←map_equiv_eq_comap_symm, map_op_one] /-- Multiplication of sub-R-modules of an R-algebra A. The submodule `M * N` is the smallest R-submodule of `A` containing the elements `m * n` for `m ∈ M` and `n ∈ N`. -/ instance : has_mul (submodule R A) := ⟨submodule.mapβ‚‚ $ linear_map.mul R A⟩ theorem mul_mem_mul (hm : m ∈ M) (hn : n ∈ N) : m * n ∈ M * N := apply_mem_mapβ‚‚ _ hm hn theorem mul_le : M * N ≀ P ↔ βˆ€ (m ∈ M) (n ∈ N), m * n ∈ P := mapβ‚‚_le lemma mul_to_add_submonoid (M N : submodule R A) : (M * N).to_add_submonoid = M.to_add_submonoid * N.to_add_submonoid := begin dsimp [has_mul.mul], simp_rw [←linear_map.mul_left_to_add_monoid_hom R, linear_map.mul_left, ←map_to_add_submonoid _ N, mapβ‚‚], rw supr_to_add_submonoid, refl, end @[elab_as_eliminator] protected theorem mul_induction_on {C : A β†’ Prop} {r : A} (hr : r ∈ M * N) (hm : βˆ€ (m ∈ M) (n ∈ N), C (m * n)) (ha : βˆ€ x y, C x β†’ C y β†’ C (x + y)) : C r := begin rw [←mem_to_add_submonoid, mul_to_add_submonoid] at hr, exact add_submonoid.mul_induction_on hr hm ha, end /-- A dependent version of `mul_induction_on`. -/ @[elab_as_eliminator] protected theorem mul_induction_on' {C : Ξ  r, r ∈ M * N β†’ Prop} (hm : βˆ€ (m ∈ M) (n ∈ N), C (m * n) (mul_mem_mul β€Ή_β€Ί β€Ή_β€Ί)) (ha : βˆ€ x hx y hy, C x hx β†’ C y hy β†’ C (x + y) (add_mem β€Ή_β€Ί β€Ή_β€Ί)) {r : A} (hr : r ∈ M * N) : C r hr := begin refine exists.elim _ (Ξ» (hr : r ∈ M * N) (hc : C r hr), hc), exact submodule.mul_induction_on hr (Ξ» x hx y hy, ⟨_, hm _ hx _ hy⟩) (Ξ» x y ⟨_, hx⟩ ⟨_, hy⟩, ⟨_, ha _ _ _ _ hx hy⟩), end variables R theorem span_mul_span : span R S * span R T = span R (S * T) := mapβ‚‚_span_span _ _ _ _ variables {R} variables (M N P Q) @[simp] theorem mul_bot : M * βŠ₯ = βŠ₯ := mapβ‚‚_bot_right _ _ @[simp] theorem bot_mul : βŠ₯ * M = βŠ₯ := mapβ‚‚_bot_left _ _ @[simp] protected theorem one_mul : (1 : submodule R A) * M = M := by { conv_lhs { rw [one_eq_span, ← span_eq M] }, erw [span_mul_span, one_mul, span_eq] } @[simp] protected theorem mul_one : M * 1 = M := by { conv_lhs { rw [one_eq_span, ← span_eq M] }, erw [span_mul_span, mul_one, span_eq] } variables {M N P Q} @[mono] theorem mul_le_mul (hmp : M ≀ P) (hnq : N ≀ Q) : M * N ≀ P * Q := mapβ‚‚_le_mapβ‚‚ hmp hnq theorem mul_le_mul_left (h : M ≀ N) : M * P ≀ N * P := mapβ‚‚_le_mapβ‚‚_left h theorem mul_le_mul_right (h : N ≀ P) : M * N ≀ M * P := mapβ‚‚_le_mapβ‚‚_right h variables (M N P) theorem mul_sup : M * (N βŠ” P) = M * N βŠ” M * P := mapβ‚‚_sup_right _ _ _ _ theorem sup_mul : (M βŠ” N) * P = M * P βŠ” N * P := mapβ‚‚_sup_left _ _ _ _ lemma mul_subset_mul : (↑M : set A) * (↑N : set A) βŠ† (↑(M * N) : set A) := image2_subset_mapβ‚‚ (algebra.lmul R A).to_linear_map M N protected lemma map_mul {A'} [semiring A'] [algebra R A'] (f : A →ₐ[R] A') : map f.to_linear_map (M * N) = map f.to_linear_map M * map f.to_linear_map N := calc map f.to_linear_map (M * N) = ⨆ (i : M), (N.map (linear_map.mul R A i)).map f.to_linear_map : map_supr _ _ ... = map f.to_linear_map M * map f.to_linear_map N : begin apply congr_arg Sup, ext S, split; rintros ⟨y, hy⟩, { use [f y, mem_map.mpr ⟨y.1, y.2, rfl⟩], refine trans _ hy, ext, simp }, { obtain ⟨y', hy', fy_eq⟩ := mem_map.mp y.2, use [y', hy'], refine trans _ hy, rw f.to_linear_map_apply at fy_eq, ext, simp [fy_eq] } end lemma map_op_mul : map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) (M * N) = map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) N * map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) M := begin apply le_antisymm, { simp_rw map_le_iff_le_comap, refine mul_le.2 (Ξ» m hm n hn, _), rw [mem_comap, map_equiv_eq_comap_symm, map_equiv_eq_comap_symm], show op n * op m ∈ _, exact mul_mem_mul hn hm }, { refine mul_le.2 (mul_opposite.rec $ Ξ» m hm, mul_opposite.rec $ Ξ» n hn, _), rw submodule.mem_map_equiv at ⊒ hm hn, exact mul_mem_mul hn hm, } end lemma comap_unop_mul : comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) (M * N) = comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) N * comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) M := by simp_rw [←map_equiv_eq_comap_symm, map_op_mul] lemma map_unop_mul (M N : submodule R Aᡐᡒᡖ) : map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) (M * N) = map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) N * map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) M := have function.injective (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) := linear_equiv.injective _, map_injective_of_injective this $ by rw [← map_comp, map_op_mul, ←map_comp, ←map_comp, linear_equiv.comp_coe, linear_equiv.symm_trans_self, linear_equiv.refl_to_linear_map, map_id, map_id, map_id] lemma comap_op_mul (M N : submodule R Aᡐᡒᡖ) : comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) (M * N) = comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) N * comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) M := by simp_rw [comap_equiv_eq_map_symm, map_unop_mul] section open_locale pointwise /-- `submodule.has_pointwise_neg` distributes over multiplication. This is available as an instance in the `pointwise` locale. -/ protected def has_distrib_pointwise_neg {A} [ring A] [algebra R A] : has_distrib_neg (submodule R A) := to_add_submonoid_injective.has_distrib_neg _ neg_to_add_submonoid mul_to_add_submonoid localized "attribute [instance] submodule.has_distrib_pointwise_neg" in pointwise end section decidable_eq open_locale classical lemma mem_span_mul_finite_of_mem_span_mul {R A} [semiring R] [add_comm_monoid A] [has_mul A] [module R A] {S : set A} {S' : set A} {x : A} (hx : x ∈ span R (S * S')) : βˆƒ (T T' : finset A), ↑T βŠ† S ∧ ↑T' βŠ† S' ∧ x ∈ span R (T * T' : set A) := begin obtain ⟨U, h, hU⟩ := mem_span_finite_of_mem_span hx, obtain ⟨T, T', hS, hS', h⟩ := finset.subset_mul h, use [T, T', hS, hS'], have h' : (U : set A) βŠ† T * T', { assumption_mod_cast, }, have h'' := span_mono h' hU, assumption, end end decidable_eq lemma mul_eq_span_mul_set (s t : submodule R A) : s * t = span R ((s : set A) * (t : set A)) := mapβ‚‚_eq_span_image2 _ s t lemma supr_mul (s : ΞΉ β†’ submodule R A) (t : submodule R A) : (⨆ i, s i) * t = ⨆ i, s i * t := mapβ‚‚_supr_left _ s t lemma mul_supr (t : submodule R A) (s : ΞΉ β†’ submodule R A) : t * (⨆ i, s i) = ⨆ i, t * s i := mapβ‚‚_supr_right _ t s lemma mem_span_mul_finite_of_mem_mul {P Q : submodule R A} {x : A} (hx : x ∈ P * Q) : βˆƒ (T T' : finset A), (T : set A) βŠ† P ∧ (T' : set A) βŠ† Q ∧ x ∈ span R (T * T' : set A) := submodule.mem_span_mul_finite_of_mem_span_mul (by rwa [← submodule.span_eq P, ← submodule.span_eq Q, submodule.span_mul_span] at hx) variables {M N P} lemma mem_span_singleton_mul {x y : A} : x ∈ span R {y} * P ↔ βˆƒ z ∈ P, y * z = x := by { simp_rw [(*), mapβ‚‚_span_singleton_eq_map, exists_prop], refl } lemma mem_mul_span_singleton {x y : A} : x ∈ P * span R {y} ↔ βˆƒ z ∈ P, z * y = x := by { simp_rw [(*), mapβ‚‚_span_singleton_eq_map_flip, exists_prop], refl } /-- Sub-R-modules of an R-algebra form an idempotent semiring. -/ instance : idem_semiring (submodule R A) := { one_mul := submodule.one_mul, mul_one := submodule.mul_one, zero_mul := bot_mul, mul_zero := mul_bot, left_distrib := mul_sup, right_distrib := sup_mul, ..to_add_submonoid_injective.semigroup _ (Ξ» m n : submodule R A, mul_to_add_submonoid m n), ..add_monoid_with_one.unary, ..submodule.pointwise_add_comm_monoid, ..submodule.has_one, ..submodule.has_mul, ..(by apply_instance : order_bot (submodule R A)), ..(by apply_instance : lattice (submodule R A)) } variables (M) lemma span_pow (s : set A) : βˆ€ n : β„•, span R s ^ n = span R (s ^ n) | 0 := by rw [pow_zero, pow_zero, one_eq_span_one_set] | (n + 1) := by rw [pow_succ, pow_succ, span_pow, span_mul_span] lemma pow_eq_span_pow_set (n : β„•) : M ^ n = span R ((M : set A) ^ n) := by rw [←span_pow, span_eq] lemma pow_subset_pow {n : β„•} : (↑M : set A)^n βŠ† ↑(M^n : submodule R A) := (pow_eq_span_pow_set M n).symm β–Έ subset_span lemma pow_mem_pow {x : A} (hx : x ∈ M) (n : β„•) : x ^ n ∈ M ^ n := pow_subset_pow _ $ set.pow_mem_pow hx _ lemma pow_to_add_submonoid {n : β„•} (h : n β‰  0) : (M ^ n).to_add_submonoid = M.to_add_submonoid ^ n := begin induction n with n ih, { exact (h rfl).elim }, { rw [pow_succ, pow_succ, mul_to_add_submonoid], cases n, { rw [pow_zero, pow_zero, mul_one, ←mul_to_add_submonoid, mul_one] }, { rw ih n.succ_ne_zero } }, end lemma le_pow_to_add_submonoid {n : β„•} : M.to_add_submonoid ^ n ≀ (M ^ n).to_add_submonoid := begin obtain rfl | hn := decidable.eq_or_ne n 0, { rw [pow_zero, pow_zero], exact le_one_to_add_submonoid }, { exact (pow_to_add_submonoid M hn).ge } end /-- Dependent version of `submodule.pow_induction_on_left`. -/ @[elab_as_eliminator] protected theorem pow_induction_on_left' {C : Ξ  (n : β„•) x, x ∈ M ^ n β†’ Prop} (hr : βˆ€ r : R, C 0 (algebra_map _ _ r) (algebra_map_mem r)) (hadd : βˆ€ x y i hx hy, C i x hx β†’ C i y hy β†’ C i (x + y) (add_mem β€Ή_β€Ί β€Ή_β€Ί)) (hmul : βˆ€ (m ∈ M) i x hx, C i x hx β†’ C (i.succ) (m * x) (mul_mem_mul H hx)) {x : A} {n : β„•} (hx : x ∈ M ^ n) : C n x hx := begin induction n with n n_ih generalizing x, { rw pow_zero at hx, obtain ⟨r, rfl⟩ := hx, exact hr r, }, exact submodule.mul_induction_on' (Ξ» m hm x ih, hmul _ hm _ _ _ (n_ih ih)) (Ξ» x hx y hy Cx Cy, hadd _ _ _ _ _ Cx Cy) hx, end /-- Dependent version of `submodule.pow_induction_on_right`. -/ @[elab_as_eliminator] protected theorem pow_induction_on_right' {C : Ξ  (n : β„•) x, x ∈ M ^ n β†’ Prop} (hr : βˆ€ r : R, C 0 (algebra_map _ _ r) (algebra_map_mem r)) (hadd : βˆ€ x y i hx hy, C i x hx β†’ C i y hy β†’ C i (x + y) (add_mem β€Ή_β€Ί β€Ή_β€Ί)) (hmul : βˆ€ i x hx, C i x hx β†’ βˆ€ m ∈ M, C (i.succ) (x * m) ((pow_succ' M i).symm β–Έ mul_mem_mul hx H)) {x : A} {n : β„•} (hx : x ∈ M ^ n) : C n x hx := begin induction n with n n_ih generalizing x, { rw pow_zero at hx, obtain ⟨r, rfl⟩ := hx, exact hr r, }, revert hx, simp_rw pow_succ', intro hx, exact submodule.mul_induction_on' (Ξ» m hm x ih, hmul _ _ hm (n_ih _) _ ih) (Ξ» x hx y hy Cx Cy, hadd _ _ _ _ _ Cx Cy) hx, end /-- To show a property on elements of `M ^ n` holds, it suffices to show that it holds for scalars, is closed under addition, and holds for `m * x` where `m ∈ M` and it holds for `x` -/ @[elab_as_eliminator] protected theorem pow_induction_on_left {C : A β†’ Prop} (hr : βˆ€ r : R, C (algebra_map _ _ r)) (hadd : βˆ€ x y, C x β†’ C y β†’ C (x + y)) (hmul : βˆ€ (m ∈ M) x, C x β†’ C (m * x)) {x : A} {n : β„•} (hx : x ∈ M ^ n) : C x := submodule.pow_induction_on_left' M (by exact hr) (Ξ» x y i hx hy, hadd x y) (Ξ» m hm i x hx, hmul _ hm _) hx /-- To show a property on elements of `M ^ n` holds, it suffices to show that it holds for scalars, is closed under addition, and holds for `x * m` where `m ∈ M` and it holds for `x` -/ @[elab_as_eliminator] protected theorem pow_induction_on_right {C : A β†’ Prop} (hr : βˆ€ r : R, C (algebra_map _ _ r)) (hadd : βˆ€ x y, C x β†’ C y β†’ C (x + y)) (hmul : βˆ€ x, C x β†’ βˆ€ (m ∈ M), C (x * m)) {x : A} {n : β„•} (hx : x ∈ M ^ n) : C x := submodule.pow_induction_on_right' M (by exact hr) (Ξ» x y i hx hy, hadd x y) (Ξ» i x hx, hmul _) hx /-- `submonoid.map` as a `monoid_with_zero_hom`, when applied to `alg_hom`s. -/ @[simps] def map_hom {A'} [semiring A'] [algebra R A'] (f : A →ₐ[R] A') : submodule R A β†’*β‚€ submodule R A' := { to_fun := map f.to_linear_map, map_zero' := submodule.map_bot _, map_one' := submodule.map_one _, map_mul' := Ξ» _ _, submodule.map_mul _ _ _} /-- The ring of submodules of the opposite algebra is isomorphic to the opposite ring of submodules. -/ @[simps apply symm_apply] def equiv_opposite : submodule R Aᡐᡒᡖ ≃+* (submodule R A)ᡐᡒᡖ := { to_fun := Ξ» p, op $ p.comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ), inv_fun := Ξ» p, p.unop.comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A), left_inv := Ξ» p, set_like.coe_injective $ rfl, right_inv := Ξ» p, unop_injective $ set_like.coe_injective rfl, map_add' := Ξ» p q, by simp [comap_equiv_eq_map_symm, ←op_add], map_mul' := Ξ» p q, congr_arg op $ comap_op_mul _ _ } protected lemma map_pow {A'} [semiring A'] [algebra R A'] (f : A →ₐ[R] A') (n : β„•) : map f.to_linear_map (M ^ n) = map f.to_linear_map M ^ n := map_pow (map_hom f) M n lemma comap_unop_pow (n : β„•) : comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) (M ^ n) = comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) M ^ n := (equiv_opposite : submodule R Aᡐᡒᡖ ≃+* _).symm.map_pow (op M) n lemma comap_op_pow (n : β„•) (M : submodule R Aᡐᡒᡖ) : comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) (M ^ n) = comap (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) M ^ n := op_injective $ (equiv_opposite : submodule R Aᡐᡒᡖ ≃+* _).map_pow M n lemma map_op_pow (n : β„•) : map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) (M ^ n) = map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ) : A β†’β‚—[R] Aᡐᡒᡖ) M ^ n := by rw [map_equiv_eq_comap_symm, map_equiv_eq_comap_symm, comap_unop_pow] lemma map_unop_pow (n : β„•) (M : submodule R Aᡐᡒᡖ) : map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) (M ^ n) = map (↑(op_linear_equiv R : A ≃ₗ[R] Aᡐᡒᡖ).symm : Aᡐᡒᡖ β†’β‚—[R] A) M ^ n := by rw [←comap_equiv_eq_map_symm, ←comap_equiv_eq_map_symm, comap_op_pow] /-- `span` is a semiring homomorphism (recall multiplication is pointwise multiplication of subsets on either side). -/ @[simps] def span.ring_hom : set_semiring A β†’+* submodule R A := { to_fun := Ξ» s, submodule.span R s.down, map_zero' := span_empty, map_one' := one_eq_span.symm, map_add' := span_union, map_mul' := Ξ» s t, by rw [set_semiring.down_mul, span_mul_span, ← image_mul_prod] } section variables {Ξ± : Type*} [monoid Ξ±] [mul_semiring_action Ξ± A] [smul_comm_class Ξ± R A] /-- The action on a submodule corresponding to applying the action to every element. This is available as an instance in the `pointwise` locale. This is a stronger version of `submodule.pointwise_distrib_mul_action`. -/ protected def pointwise_mul_semiring_action : mul_semiring_action Ξ± (submodule R A) := { smul_mul := Ξ» r x y, submodule.map_mul x y $ mul_semiring_action.to_alg_hom R A r, smul_one := Ξ» r, submodule.map_one $ mul_semiring_action.to_alg_hom R A r, ..submodule.pointwise_distrib_mul_action } localized "attribute [instance] submodule.pointwise_mul_semiring_action" in pointwise end end ring section comm_ring variables {A : Type v} [comm_semiring A] [algebra R A] variables {M N : submodule R A} {m n : A} theorem mul_mem_mul_rev (hm : m ∈ M) (hn : n ∈ N) : n * m ∈ M * N := mul_comm m n β–Έ mul_mem_mul hm hn variables (M N) protected theorem mul_comm : M * N = N * M := le_antisymm (mul_le.2 $ Ξ» r hrm s hsn, mul_mem_mul_rev hsn hrm) (mul_le.2 $ Ξ» r hrn s hsm, mul_mem_mul_rev hsm hrn) /-- Sub-R-modules of an R-algebra A form a semiring. -/ instance : idem_comm_semiring (submodule R A) := { mul_comm := submodule.mul_comm, .. submodule.idem_semiring } lemma prod_span {ΞΉ : Type*} (s : finset ΞΉ) (M : ΞΉ β†’ set A) : (∏ i in s, submodule.span R (M i)) = submodule.span R (∏ i in s, M i) := begin letI := classical.dec_eq ΞΉ, refine finset.induction_on s _ _, { simp [one_eq_span, set.singleton_one] }, { intros _ _ H ih, rw [finset.prod_insert H, finset.prod_insert H, ih, span_mul_span] } end lemma prod_span_singleton {ΞΉ : Type*} (s : finset ΞΉ) (x : ΞΉ β†’ A) : (∏ i in s, span R ({x i} : set A)) = span R {∏ i in s, x i} := by rw [prod_span, set.finset_prod_singleton] variables (R A) /-- R-submodules of the R-algebra A are a module over `set A`. -/ instance module_set : module (set_semiring A) (submodule R A) := { smul := Ξ» s P, span R s.down * P, smul_add := Ξ» _ _ _, mul_add _ _ _, add_smul := Ξ» s t P, by simp_rw [has_smul.smul, set_semiring.down_add, span_union, sup_mul, add_eq_sup], mul_smul := Ξ» s t P, by simp_rw [has_smul.smul, set_semiring.down_mul, ← mul_assoc, span_mul_span], one_smul := Ξ» P, by simp_rw [has_smul.smul, set_semiring.down_one, ←one_eq_span_one_set, one_mul], zero_smul := Ξ» P, by simp_rw [has_smul.smul, set_semiring.down_zero, span_empty, bot_mul, bot_eq_zero], smul_zero := Ξ» _, mul_bot _ } variables {R A} lemma smul_def (s : set_semiring A) (P : submodule R A) : s β€’ P = span R s.down * P := rfl lemma smul_le_smul {s t : set_semiring A} {M N : submodule R A} (h₁ : s.down βŠ† t.down) (hβ‚‚ : M ≀ N) : s β€’ M ≀ t β€’ N := mul_le_mul (span_mono h₁) hβ‚‚ lemma smul_singleton (a : A) (M : submodule R A) : ({a} : set A).up β€’ M = M.map (linear_map.mul_left R a) := begin conv_lhs {rw ← span_eq M}, change span _ _ * span _ _ = _, rw [span_mul_span], apply le_antisymm, { rw span_le, rintros _ ⟨b, m, hb, hm, rfl⟩, rw [set_like.mem_coe, mem_map, set.mem_singleton_iff.mp hb], exact ⟨m, hm, rfl⟩ }, { rintros _ ⟨m, hm, rfl⟩, exact subset_span ⟨a, m, set.mem_singleton a, hm, rfl⟩ } end section quotient /-- The elements of `I / J` are the `x` such that `x β€’ J βŠ† I`. In fact, we define `x ∈ I / J` to be `βˆ€ y ∈ J, x * y ∈ I` (see `mem_div_iff_forall_mul_mem`), which is equivalent to `x β€’ J βŠ† I` (see `mem_div_iff_smul_subset`), but nicer to use in proofs. This is the general form of the ideal quotient, traditionally written $I : J$. -/ instance : has_div (submodule R A) := ⟨ Ξ» I J, { carrier := { x | βˆ€ y ∈ J, x * y ∈ I }, zero_mem' := Ξ» y hy, by { rw zero_mul, apply submodule.zero_mem }, add_mem' := Ξ» a b ha hb y hy, by { rw add_mul, exact submodule.add_mem _ (ha _ hy) (hb _ hy) }, smul_mem' := Ξ» r x hx y hy, by { rw algebra.smul_mul_assoc, exact submodule.smul_mem _ _ (hx _ hy) } } ⟩ lemma mem_div_iff_forall_mul_mem {x : A} {I J : submodule R A} : x ∈ I / J ↔ βˆ€ y ∈ J, x * y ∈ I := iff.refl _ lemma mem_div_iff_smul_subset {x : A} {I J : submodule R A} : x ∈ I / J ↔ x β€’ (J : set A) βŠ† I := ⟨ Ξ» h y ⟨y', hy', xy'_eq_y⟩, by { rw ← xy'_eq_y, apply h, assumption }, Ξ» h y hy, h (set.smul_mem_smul_set hy) ⟩ lemma le_div_iff {I J K : submodule R A} : I ≀ J / K ↔ βˆ€ (x ∈ I) (z ∈ K), x * z ∈ J := iff.refl _ lemma le_div_iff_mul_le {I J K : submodule R A} : I ≀ J / K ↔ I * K ≀ J := by rw [le_div_iff, mul_le] @[simp] lemma one_le_one_div {I : submodule R A} : 1 ≀ 1 / I ↔ I ≀ 1 := begin split, all_goals {intro hI}, {rwa [le_div_iff_mul_le, one_mul] at hI}, {rwa [le_div_iff_mul_le, one_mul]}, end lemma le_self_mul_one_div {I : submodule R A} (hI : I ≀ 1) : I ≀ I * (1 / I) := begin rw [← mul_one I] {occs := occurrences.pos [1]}, apply mul_le_mul_right (one_le_one_div.mpr hI), end lemma mul_one_div_le_one {I : submodule R A} : I * (1 / I) ≀ 1 := begin rw submodule.mul_le, intros m hm n hn, rw [submodule.mem_div_iff_forall_mul_mem] at hn, rw mul_comm, exact hn m hm, end @[simp] protected lemma map_div {B : Type*} [comm_semiring B] [algebra R B] (I J : submodule R A) (h : A ≃ₐ[R] B) : (I / J).map h.to_linear_map = I.map h.to_linear_map / J.map h.to_linear_map := begin ext x, simp only [mem_map, mem_div_iff_forall_mul_mem], split, { rintro ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩, exact ⟨x * y, hx _ hy, h.map_mul x y⟩ }, { rintro hx, refine ⟨h.symm x, Ξ» z hz, _, h.apply_symm_apply x⟩, obtain ⟨xz, xz_mem, hxz⟩ := hx (h z) ⟨z, hz, rfl⟩, convert xz_mem, apply h.injective, erw [h.map_mul, h.apply_symm_apply, hxz] } end end quotient end comm_ring end submodule
131f84ad80f5b3da4e400c4866067fd99fe77a50
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/big_operators/order_auto.lean
7a2415bd2307760c39d806bc48d19a1ea5471653
[]
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
13,952
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.big_operators.basic import Mathlib.PostPort universes u v w namespace Mathlib /-! # Results about big operators with values in an ordered algebraic structure. Mostly monotonicity results for the `βˆ‘` operation. -/ namespace finset theorem le_sum_of_subadditive {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [add_comm_monoid Ξ±] [ordered_add_comm_monoid Ξ²] (f : Ξ± β†’ Ξ²) (h_zero : f 0 = 0) (h_add : βˆ€ (x y : Ξ±), f (x + y) ≀ f x + f y) (s : finset Ξ³) (g : Ξ³ β†’ Ξ±) : f (finset.sum s fun (x : Ξ³) => g x) ≀ finset.sum s fun (x : Ξ³) => f (g x) := sorry theorem abs_sum_le_sum_abs {Ξ± : Type u} {Ξ² : Type v} [linear_ordered_field Ξ±] {f : Ξ² β†’ Ξ±} {s : finset Ξ²} : abs (finset.sum s fun (x : Ξ²) => f x) ≀ finset.sum s fun (x : Ξ²) => abs (f x) := le_sum_of_subadditive abs abs_zero abs_add s f theorem abs_prod {Ξ± : Type u} {Ξ² : Type v} [linear_ordered_comm_ring Ξ±] {f : Ξ² β†’ Ξ±} {s : finset Ξ²} : abs (finset.prod s fun (x : Ξ²) => f x) = finset.prod s fun (x : Ξ²) => abs (f x) := monoid_hom.map_prod (monoid_with_zero_hom.to_monoid_hom abs_hom) (fun (x : Ξ²) => f x) s theorem sum_le_sum {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} [ordered_add_comm_monoid Ξ²] : (βˆ€ (x : Ξ±), x ∈ s β†’ f x ≀ g x) β†’ (finset.sum s fun (x : Ξ±) => f x) ≀ finset.sum s fun (x : Ξ±) => g x := sorry theorem card_le_mul_card_image_of_maps_to {Ξ± : Type u} {Ξ³ : Type w} [DecidableEq Ξ³] {f : Ξ± β†’ Ξ³} {s : finset Ξ±} {t : finset Ξ³} (Hf : βˆ€ (a : Ξ±), a ∈ s β†’ f a ∈ t) (n : β„•) (hn : βˆ€ (a : Ξ³), a ∈ t β†’ card (filter (fun (x : Ξ±) => f x = a) s) ≀ n) : card s ≀ n * card t := sorry theorem card_le_mul_card_image {Ξ± : Type u} {Ξ³ : Type w} [DecidableEq Ξ³] {f : Ξ± β†’ Ξ³} (s : finset Ξ±) (n : β„•) (hn : βˆ€ (a : Ξ³), a ∈ image f s β†’ card (filter (fun (x : Ξ±) => f x = a) s) ≀ n) : card s ≀ n * card (image f s) := card_le_mul_card_image_of_maps_to (fun (x : Ξ±) => mem_image_of_mem f) n hn theorem mul_card_image_le_card_of_maps_to {Ξ± : Type u} {Ξ³ : Type w} [DecidableEq Ξ³] {f : Ξ± β†’ Ξ³} {s : finset Ξ±} {t : finset Ξ³} (Hf : βˆ€ (a : Ξ±), a ∈ s β†’ f a ∈ t) (n : β„•) (hn : βˆ€ (a : Ξ³), a ∈ t β†’ n ≀ card (filter (fun (x : Ξ±) => f x = a) s)) : n * card t ≀ card s := sorry theorem mul_card_image_le_card {Ξ± : Type u} {Ξ³ : Type w} [DecidableEq Ξ³] {f : Ξ± β†’ Ξ³} (s : finset Ξ±) (n : β„•) (hn : βˆ€ (a : Ξ³), a ∈ image f s β†’ n ≀ card (filter (fun (x : Ξ±) => f x = a) s)) : n * card (image f s) ≀ card s := mul_card_image_le_card_of_maps_to (fun (x : Ξ±) => mem_image_of_mem f) n hn theorem sum_nonneg {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} [ordered_add_comm_monoid Ξ²] (h : βˆ€ (x : Ξ±), x ∈ s β†’ 0 ≀ f x) : 0 ≀ finset.sum s fun (x : Ξ±) => f x := le_trans (eq.mpr (id (Eq._oldrec (Eq.refl (0 ≀ finset.sum s fun (x : Ξ±) => 0)) sum_const_zero)) (le_refl 0)) (sum_le_sum h) theorem sum_nonpos {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} [ordered_add_comm_monoid Ξ²] (h : βˆ€ (x : Ξ±), x ∈ s β†’ f x ≀ 0) : (finset.sum s fun (x : Ξ±) => f x) ≀ 0 := le_trans (sum_le_sum h) (eq.mpr (id (Eq._oldrec (Eq.refl ((finset.sum s fun (x : Ξ±) => 0) ≀ 0)) sum_const_zero)) (le_refl 0)) theorem sum_le_sum_of_subset_of_nonneg {Ξ± : Type u} {Ξ² : Type v} {s₁ : finset Ξ±} {sβ‚‚ : finset Ξ±} {f : Ξ± β†’ Ξ²} [ordered_add_comm_monoid Ξ²] (h : s₁ βŠ† sβ‚‚) (hf : βˆ€ (x : Ξ±), x ∈ sβ‚‚ β†’ Β¬x ∈ s₁ β†’ 0 ≀ f x) : (finset.sum s₁ fun (x : Ξ±) => f x) ≀ finset.sum sβ‚‚ fun (x : Ξ±) => f x := sorry theorem sum_mono_set_of_nonneg {Ξ± : Type u} {Ξ² : Type v} {f : Ξ± β†’ Ξ²} [ordered_add_comm_monoid Ξ²] (hf : βˆ€ (x : Ξ±), 0 ≀ f x) : monotone fun (s : finset Ξ±) => finset.sum s fun (x : Ξ±) => f x := fun (s₁ sβ‚‚ : finset Ξ±) (hs : s₁ ≀ sβ‚‚) => sum_le_sum_of_subset_of_nonneg hs fun (x : Ξ±) (_x : x ∈ sβ‚‚) (_x : Β¬x ∈ s₁) => hf x theorem sum_fiberwise_le_sum_of_sum_fiber_nonneg {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [ordered_add_comm_monoid Ξ²] [DecidableEq Ξ³] {s : finset Ξ±} {t : finset Ξ³} {g : Ξ± β†’ Ξ³} {f : Ξ± β†’ Ξ²} (h : βˆ€ (y : Ξ³), Β¬y ∈ t β†’ 0 ≀ finset.sum (filter (fun (x : Ξ±) => g x = y) s) fun (x : Ξ±) => f x) : (finset.sum t fun (y : Ξ³) => finset.sum (filter (fun (x : Ξ±) => g x = y) s) fun (x : Ξ±) => f x) ≀ finset.sum s fun (x : Ξ±) => f x := sorry theorem sum_le_sum_fiberwise_of_sum_fiber_nonpos {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} [ordered_add_comm_monoid Ξ²] [DecidableEq Ξ³] {s : finset Ξ±} {t : finset Ξ³} {g : Ξ± β†’ Ξ³} {f : Ξ± β†’ Ξ²} (h : βˆ€ (y : Ξ³), Β¬y ∈ t β†’ (finset.sum (filter (fun (x : Ξ±) => g x = y) s) fun (x : Ξ±) => f x) ≀ 0) : (finset.sum s fun (x : Ξ±) => f x) ≀ finset.sum t fun (y : Ξ³) => finset.sum (filter (fun (x : Ξ±) => g x = y) s) fun (x : Ξ±) => f x := sum_fiberwise_le_sum_of_sum_fiber_nonneg h theorem sum_eq_zero_iff_of_nonneg {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} [ordered_add_comm_monoid Ξ²] : (βˆ€ (x : Ξ±), x ∈ s β†’ 0 ≀ f x) β†’ ((finset.sum s fun (x : Ξ±) => f x) = 0 ↔ βˆ€ (x : Ξ±), x ∈ s β†’ f x = 0) := sorry theorem sum_eq_zero_iff_of_nonpos {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} [ordered_add_comm_monoid Ξ²] : (βˆ€ (x : Ξ±), x ∈ s β†’ f x ≀ 0) β†’ ((finset.sum s fun (x : Ξ±) => f x) = 0 ↔ βˆ€ (x : Ξ±), x ∈ s β†’ f x = 0) := sum_eq_zero_iff_of_nonneg theorem single_le_sum {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} [ordered_add_comm_monoid Ξ²] (hf : βˆ€ (x : Ξ±), x ∈ s β†’ 0 ≀ f x) {a : Ξ±} (h : a ∈ s) : f a ≀ finset.sum s fun (x : Ξ±) => f x := sorry @[simp] theorem sum_eq_zero_iff {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} [canonically_ordered_add_monoid Ξ²] : (finset.sum s fun (x : Ξ±) => f x) = 0 ↔ βˆ€ (x : Ξ±), x ∈ s β†’ f x = 0 := sum_eq_zero_iff_of_nonneg fun (x : Ξ±) (hx : x ∈ s) => zero_le (f x) theorem sum_le_sum_of_subset {Ξ± : Type u} {Ξ² : Type v} {s₁ : finset Ξ±} {sβ‚‚ : finset Ξ±} {f : Ξ± β†’ Ξ²} [canonically_ordered_add_monoid Ξ²] (h : s₁ βŠ† sβ‚‚) : (finset.sum s₁ fun (x : Ξ±) => f x) ≀ finset.sum sβ‚‚ fun (x : Ξ±) => f x := sum_le_sum_of_subset_of_nonneg h fun (x : Ξ±) (h₁ : x ∈ sβ‚‚) (hβ‚‚ : Β¬x ∈ s₁) => zero_le (f x) theorem sum_mono_set {Ξ± : Type u} {Ξ² : Type v} [canonically_ordered_add_monoid Ξ²] (f : Ξ± β†’ Ξ²) : monotone fun (s : finset Ξ±) => finset.sum s fun (x : Ξ±) => f x := fun (s₁ sβ‚‚ : finset Ξ±) (hs : s₁ ≀ sβ‚‚) => sum_le_sum_of_subset hs theorem sum_le_sum_of_ne_zero {Ξ± : Type u} {Ξ² : Type v} {s₁ : finset Ξ±} {sβ‚‚ : finset Ξ±} {f : Ξ± β†’ Ξ²} [canonically_ordered_add_monoid Ξ²] (h : βˆ€ (x : Ξ±), x ∈ s₁ β†’ f x β‰  0 β†’ x ∈ sβ‚‚) : (finset.sum s₁ fun (x : Ξ±) => f x) ≀ finset.sum sβ‚‚ fun (x : Ξ±) => f x := sorry theorem sum_lt_sum {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} [ordered_cancel_add_comm_monoid Ξ²] (Hle : βˆ€ (i : Ξ±), i ∈ s β†’ f i ≀ g i) (Hlt : βˆƒ (i : Ξ±), βˆƒ (H : i ∈ s), f i < g i) : (finset.sum s fun (x : Ξ±) => f x) < finset.sum s fun (x : Ξ±) => g x := sorry theorem sum_lt_sum_of_nonempty {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} [ordered_cancel_add_comm_monoid Ξ²] (hs : finset.nonempty s) (Hlt : βˆ€ (x : Ξ±), x ∈ s β†’ f x < g x) : (finset.sum s fun (x : Ξ±) => f x) < finset.sum s fun (x : Ξ±) => g x := sum_lt_sum (fun (i : Ξ±) (hi : i ∈ s) => le_of_lt (Hlt i hi)) (Exists.dcases_on hs fun (i : Ξ±) (hi : i ∈ s) => Exists.intro i (Exists.intro hi (Hlt i hi))) theorem sum_lt_sum_of_subset {Ξ± : Type u} {Ξ² : Type v} {s₁ : finset Ξ±} {sβ‚‚ : finset Ξ±} {f : Ξ± β†’ Ξ²} [ordered_cancel_add_comm_monoid Ξ²] [DecidableEq Ξ±] (h : s₁ βŠ† sβ‚‚) {i : Ξ±} (hi : i ∈ sβ‚‚ \ s₁) (hpos : 0 < f i) (hnonneg : βˆ€ (j : Ξ±), j ∈ sβ‚‚ \ s₁ β†’ 0 ≀ f j) : (finset.sum s₁ fun (x : Ξ±) => f x) < finset.sum sβ‚‚ fun (x : Ξ±) => f x := sorry theorem exists_lt_of_sum_lt {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} [linear_ordered_cancel_add_comm_monoid Ξ²] (Hlt : (finset.sum s fun (x : Ξ±) => f x) < finset.sum s fun (x : Ξ±) => g x) : βˆƒ (i : Ξ±), βˆƒ (H : i ∈ s), f i < g i := sorry theorem exists_le_of_sum_le {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} [linear_ordered_cancel_add_comm_monoid Ξ²] (hs : finset.nonempty s) (Hle : (finset.sum s fun (x : Ξ±) => f x) ≀ finset.sum s fun (x : Ξ±) => g x) : βˆƒ (i : Ξ±), βˆƒ (H : i ∈ s), f i ≀ g i := sorry theorem exists_pos_of_sum_zero_of_exists_nonzero {Ξ± : Type u} {Ξ² : Type v} {s : finset Ξ±} [linear_ordered_cancel_add_comm_monoid Ξ²] (f : Ξ± β†’ Ξ²) (h₁ : (finset.sum s fun (e : Ξ±) => f e) = 0) (hβ‚‚ : βˆƒ (x : Ξ±), βˆƒ (H : x ∈ s), f x β‰  0) : βˆƒ (x : Ξ±), βˆƒ (H : x ∈ s), 0 < f x := sorry /- this is also true for a ordered commutative multiplicative monoid -/ theorem prod_nonneg {Ξ± : Type u} {Ξ² : Type v} [linear_ordered_comm_ring Ξ²] {s : finset Ξ±} {f : Ξ± β†’ Ξ²} (h0 : βˆ€ (x : Ξ±), x ∈ s β†’ 0 ≀ f x) : 0 ≀ finset.prod s fun (x : Ξ±) => f x := prod_induction f (fun (x : Ξ²) => 0 ≀ x) (fun (_x _x_1 : Ξ²) (ha : 0 ≀ _x) (hb : 0 ≀ _x_1) => mul_nonneg ha hb) zero_le_one h0 /- this is also true for a ordered commutative multiplicative monoid -/ theorem prod_pos {Ξ± : Type u} {Ξ² : Type v} [linear_ordered_comm_ring Ξ²] {s : finset Ξ±} {f : Ξ± β†’ Ξ²} (h0 : βˆ€ (x : Ξ±), x ∈ s β†’ 0 < f x) : 0 < finset.prod s fun (x : Ξ±) => f x := prod_induction f (fun (x : Ξ²) => 0 < x) (fun (_x _x_1 : Ξ²) (ha : 0 < _x) (hb : 0 < _x_1) => mul_pos ha hb) zero_lt_one h0 /- this is also true for a ordered commutative multiplicative monoid -/ theorem prod_le_prod {Ξ± : Type u} {Ξ² : Type v} [linear_ordered_comm_ring Ξ²] {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (h0 : βˆ€ (x : Ξ±), x ∈ s β†’ 0 ≀ f x) (h1 : βˆ€ (x : Ξ±), x ∈ s β†’ f x ≀ g x) : (finset.prod s fun (x : Ξ±) => f x) ≀ finset.prod s fun (x : Ξ±) => g x := sorry theorem prod_le_one {Ξ± : Type u} {Ξ² : Type v} [linear_ordered_comm_ring Ξ²] {s : finset Ξ±} {f : Ξ± β†’ Ξ²} (h0 : βˆ€ (x : Ξ±), x ∈ s β†’ 0 ≀ f x) (h1 : βˆ€ (x : Ξ±), x ∈ s β†’ f x ≀ 1) : (finset.prod s fun (x : Ξ±) => f x) ≀ 1 := sorry /-- If `g, h ≀ f` and `g i + h i ≀ f i`, then the product of `f` over `s` is at least the sum of the products of `g` and `h`. This is the version for `linear_ordered_comm_ring`. -/ theorem prod_add_prod_le {Ξ± : Type u} {Ξ² : Type v} [linear_ordered_comm_ring Ξ²] {s : finset Ξ±} {i : Ξ±} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {h : Ξ± β†’ Ξ²} (hi : i ∈ s) (h2i : g i + h i ≀ f i) (hgf : βˆ€ (j : Ξ±), j ∈ s β†’ j β‰  i β†’ g j ≀ f j) (hhf : βˆ€ (j : Ξ±), j ∈ s β†’ j β‰  i β†’ h j ≀ f j) (hg : βˆ€ (i : Ξ±), i ∈ s β†’ 0 ≀ g i) (hh : βˆ€ (i : Ξ±), i ∈ s β†’ 0 ≀ h i) : ((finset.prod s fun (i : Ξ±) => g i) + finset.prod s fun (i : Ξ±) => h i) ≀ finset.prod s fun (i : Ξ±) => f i := sorry theorem prod_le_prod' {Ξ± : Type u} {Ξ² : Type v} [canonically_ordered_comm_semiring Ξ²] {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} (h : βˆ€ (i : Ξ±), i ∈ s β†’ f i ≀ g i) : (finset.prod s fun (x : Ξ±) => f x) ≀ finset.prod s fun (x : Ξ±) => g x := sorry /-- If `g, h ≀ f` and `g i + h i ≀ f i`, then the product of `f` over `s` is at least the sum of the products of `g` and `h`. This is the version for `canonically_ordered_comm_semiring`. -/ theorem prod_add_prod_le' {Ξ± : Type u} {Ξ² : Type v} [canonically_ordered_comm_semiring Ξ²] {s : finset Ξ±} {i : Ξ±} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ²} {h : Ξ± β†’ Ξ²} (hi : i ∈ s) (h2i : g i + h i ≀ f i) (hgf : βˆ€ (j : Ξ±), j ∈ s β†’ j β‰  i β†’ g j ≀ f j) (hhf : βˆ€ (j : Ξ±), j ∈ s β†’ j β‰  i β†’ h j ≀ f j) : ((finset.prod s fun (i : Ξ±) => g i) + finset.prod s fun (i : Ξ±) => h i) ≀ finset.prod s fun (i : Ξ±) => f i := sorry end finset namespace with_top /-- A product of finite numbers is still finite -/ theorem prod_lt_top {Ξ± : Type u} {Ξ² : Type v} [canonically_ordered_comm_semiring Ξ²] [nontrivial Ξ²] [DecidableEq Ξ²] {s : finset Ξ±} {f : Ξ± β†’ with_top Ξ²} (h : βˆ€ (a : Ξ±), a ∈ s β†’ f a < ⊀) : (finset.prod s fun (x : Ξ±) => f x) < ⊀ := finset.prod_induction f (fun (a : with_top Ξ²) => a < ⊀) (fun (a b : with_top Ξ²) => mul_lt_top) (coe_lt_top 1) h /-- A sum of finite numbers is still finite -/ theorem sum_lt_top {Ξ± : Type u} {Ξ² : Type v} [ordered_add_comm_monoid Ξ²] {s : finset Ξ±} {f : Ξ± β†’ with_top Ξ²} : (βˆ€ (a : Ξ±), a ∈ s β†’ f a < ⊀) β†’ (finset.sum s fun (x : Ξ±) => f x) < ⊀ := sorry /-- A sum of finite numbers is still finite -/ theorem sum_lt_top_iff {Ξ± : Type u} {Ξ² : Type v} [canonically_ordered_add_monoid Ξ²] {s : finset Ξ±} {f : Ξ± β†’ with_top Ξ²} : (finset.sum s fun (x : Ξ±) => f x) < ⊀ ↔ βˆ€ (a : Ξ±), a ∈ s β†’ f a < ⊀ := sorry /-- A sum of numbers is infinite iff one of them is infinite -/ theorem sum_eq_top_iff {Ξ± : Type u} {Ξ² : Type v} [canonically_ordered_add_monoid Ξ²] {s : finset Ξ±} {f : Ξ± β†’ with_top Ξ²} : (finset.sum s fun (x : Ξ±) => f x) = ⊀ ↔ βˆƒ (a : Ξ±), βˆƒ (H : a ∈ s), f a = ⊀ := sorry end Mathlib
0acee656a7f82e83898fa5d17173410101bad8d1
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/witt_vector/basic.lean
acc7721bce7e017f2f25378903c681bde44d1c6f
[ "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
9,526
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import data.mv_polynomial.counit import data.mv_polynomial.invertible import ring_theory.witt_vector.defs /-! # Witt vectors This file verifies that the ring operations on `witt_vector p R` satisfy the axioms of a commutative ring. ## Main definitions * `witt_vector.map`: lifts a ring homomorphism `R β†’+* S` to a ring homomorphism `π•Ž R β†’+* π•Ž S`. * `witt_vector.ghost_component n x`: evaluates the `n`th Witt polynomial on the first `n` coefficients of `x`, producing a value in `R`. This is a ring homomorphism. * `witt_vector.ghost_map`: a ring homomorphism `π•Ž R β†’+* (β„• β†’ R)`, obtained by packaging all the ghost components together. If `p` is invertible in `R`, then the ghost map is an equivalence, which we use to define the ring operations on `π•Ž R`. * `witt_vector.comm_ring`: the ring structure induced by the ghost components. ## Notation We use notation `π•Ž R`, entered `\bbW`, for the Witt vectors over `R`. ## Implementation details As we prove that the ghost components respect the ring operations, we face a number of repetitive proofs. To avoid duplicating code we factor these proofs into a custom tactic, only slightly more powerful than a tactic macro. This tactic is not particularly useful outside of its applications in this file. ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ noncomputable theory open mv_polynomial function open_locale big_operators variables {p : β„•} {R S T : Type*} [hp : fact p.prime] [comm_ring R] [comm_ring S] [comm_ring T] variables {Ξ± : Type*} {Ξ² : Type*} local notation `π•Ž` := witt_vector p -- type as `\bbW` open_locale witt namespace witt_vector /-- `f : Ξ± β†’ Ξ²` induces a map from `π•Ž Ξ±` to `π•Ž Ξ²` by applying `f` componentwise. If `f` is a ring homomorphism, then so is `f`, see `witt_vector.map f`. -/ def map_fun (f : Ξ± β†’ Ξ²) : π•Ž Ξ± β†’ π•Ž Ξ² := Ξ» x, mk _ (f ∘ x.coeff) namespace map_fun lemma injective (f : Ξ± β†’ Ξ²) (hf : injective f) : injective (map_fun f : π•Ž Ξ± β†’ π•Ž Ξ²) := Ξ» x y h, ext $ Ξ» n, hf (congr_arg (Ξ» x, coeff x n) h : _) lemma surjective (f : Ξ± β†’ Ξ²) (hf : surjective f) : surjective (map_fun f : π•Ž Ξ± β†’ π•Ž Ξ²) := Ξ» x, ⟨mk _ (Ξ» n, classical.some $ hf $ x.coeff n), by { ext n, dsimp [map_fun], rw classical.some_spec (hf (x.coeff n)) }⟩ variables (f : R β†’+* S) (x y : π•Ž R) /-- Auxiliary tactic for showing that `map_fun` respects the ring operations. -/ meta def map_fun_tac : tactic unit := `[ext n, show f (aeval _ _) = aeval _ _, rw map_aeval, apply evalβ‚‚_hom_congr (ring_hom.ext_int _ _) _ rfl, ext ⟨i, k⟩, fin_cases i; refl] include hp /- We do not tag these lemmas as `@[simp]` because they will be bundled in `map` later on. -/ lemma zero : map_fun f (0 : π•Ž R) = 0 := by map_fun_tac lemma one : map_fun f (1 : π•Ž R) = 1 := by map_fun_tac lemma add : map_fun f (x + y) = map_fun f x + map_fun f y := by map_fun_tac lemma sub : map_fun f (x - y) = map_fun f x - map_fun f y := by map_fun_tac lemma mul : map_fun f (x * y) = map_fun f x * map_fun f y := by map_fun_tac lemma neg : map_fun f (-x) = -map_fun f x := by map_fun_tac end map_fun end witt_vector section tactic setup_tactic_parser open tactic /-- An auxiliary tactic for proving that `ghost_fun` respects the ring operations. -/ meta def tactic.interactive.ghost_fun_tac (Ο† fn : parse parser.pexpr) : tactic unit := do fn ← to_expr ```(%%fn : fin _ β†’ β„• β†’ R), `(fin %%k β†’ _ β†’ _) ← infer_type fn, `[ext n], `[dunfold witt_vector.has_zero witt_zero witt_vector.has_one witt_one witt_vector.has_neg witt_neg witt_vector.has_mul witt_mul witt_vector.has_sub witt_sub witt_vector.has_add witt_add ], to_expr ```(congr_fun (congr_arg (@peval R _ %%k) (witt_structure_int_prop p %%Ο† n)) %%fn) >>= note `this none, `[simpa [ghost_fun, aeval_rename, aeval_bind₁, (∘), uncurry, peval, eval] using this] end tactic namespace witt_vector /-- Evaluates the `n`th Witt polynomial on the first `n` coefficients of `x`, producing a value in `R`. This function will be bundled as the ring homomorphism `witt_vector.ghost_map` once the ring structure is available, but we rely on it to set up the ring structure in the first place. -/ private def ghost_fun : π•Ž R β†’ (β„• β†’ R) := Ξ» x n, aeval x.coeff (W_ β„€ n) section ghost_fun include hp /- The following lemmas are not `@[simp]` because they will be bundled in `ghost_map` later on. -/ variables (x y : π•Ž R) omit hp local attribute [simp] lemma matrix_vec_empty_coeff {R} (i j) : @coeff p R (matrix.vec_empty i) j = (matrix.vec_empty i : β„• β†’ R) j := by rcases i with ⟨_ | _ | _ | _ | i_val, ⟨⟩⟩ include hp private lemma ghost_fun_zero : ghost_fun (0 : π•Ž R) = 0 := by ghost_fun_tac 0 ![] private lemma ghost_fun_one : ghost_fun (1 : π•Ž R) = 1 := by ghost_fun_tac 1 ![] private lemma ghost_fun_add : ghost_fun (x + y) = ghost_fun x + ghost_fun y := by ghost_fun_tac (X 0 + X 1) ![x.coeff, y.coeff] private lemma ghost_fun_sub : ghost_fun (x - y) = ghost_fun x - ghost_fun y := by ghost_fun_tac (X 0 - X 1) ![x.coeff, y.coeff] private lemma ghost_fun_mul : ghost_fun (x * y) = ghost_fun x * ghost_fun y := by ghost_fun_tac (X 0 * X 1) ![x.coeff, y.coeff] private lemma ghost_fun_neg : ghost_fun (-x) = - ghost_fun x := by ghost_fun_tac (-X 0) ![x.coeff] end ghost_fun variables (p) (R) /-- The bijection between `π•Ž R` and `β„• β†’ R`, under the assumption that `p` is invertible in `R`. In `witt_vector.ghost_equiv` we upgrade this to an isomorphism of rings. -/ private def ghost_equiv' [invertible (p : R)] : π•Ž R ≃ (β„• β†’ R) := { to_fun := ghost_fun, inv_fun := Ξ» x, mk p $ Ξ» n, aeval x (X_in_terms_of_W p R n), left_inv := begin intro x, ext n, have := bind₁_witt_polynomial_X_in_terms_of_W p R n, apply_fun (aeval x.coeff) at this, simpa only [aeval_bind₁, aeval_X, ghost_fun, aeval_witt_polynomial] end, right_inv := begin intro x, ext n, have := bind₁_X_in_terms_of_W_witt_polynomial p R n, apply_fun (aeval x) at this, simpa only [aeval_bind₁, aeval_X, ghost_fun, aeval_witt_polynomial] end } include hp local attribute [instance] private def comm_ring_aux₁ : comm_ring (π•Ž (mv_polynomial R β„š)) := (ghost_equiv' p (mv_polynomial R β„š)).injective.comm_ring (ghost_fun) ghost_fun_zero ghost_fun_one ghost_fun_add ghost_fun_mul ghost_fun_neg ghost_fun_sub local attribute [instance] private def comm_ring_auxβ‚‚ : comm_ring (π•Ž (mv_polynomial R β„€)) := (map_fun.injective _ $ map_injective (int.cast_ring_hom β„š) int.cast_injective).comm_ring _ (map_fun.zero _) (map_fun.one _) (map_fun.add _) (map_fun.mul _) (map_fun.neg _) (map_fun.sub _) /-- The commutative ring structure on `π•Ž R`. -/ instance : comm_ring (π•Ž R) := (map_fun.surjective _ $ counit_surjective _).comm_ring (map_fun $ mv_polynomial.counit _) (map_fun.zero _) (map_fun.one _) (map_fun.add _) (map_fun.mul _) (map_fun.neg _) (map_fun.sub _) variables {p R} /-- `witt_vector.map f` is the ring homomorphism `π•Ž R β†’+* π•Ž S` naturally induced by a ring homomorphism `f : R β†’+* S`. It acts coefficientwise. -/ def map (f : R β†’+* S) : π•Ž R β†’+* π•Ž S := { to_fun := map_fun f, map_zero' := map_fun.zero f, map_one' := map_fun.one f, map_add' := map_fun.add f, map_mul' := map_fun.mul f } lemma map_injective (f : R β†’+* S) (hf : injective f) : injective (map f : π•Ž R β†’ π•Ž S) := map_fun.injective f hf lemma map_surjective (f : R β†’+* S) (hf : surjective f) : surjective (map f : π•Ž R β†’ π•Ž S) := map_fun.surjective f hf @[simp] lemma map_coeff (f : R β†’+* S) (x : π•Ž R) (n : β„•) : (map f x).coeff n = f (x.coeff n) := rfl /-- `witt_vector.ghost_map` is a ring homomorphism that maps each Witt vector to the sequence of its ghost components. -/ def ghost_map : π•Ž R β†’+* β„• β†’ R := { to_fun := ghost_fun, map_zero' := ghost_fun_zero, map_one' := ghost_fun_one, map_add' := ghost_fun_add, map_mul' := ghost_fun_mul } /-- Evaluates the `n`th Witt polynomial on the first `n` coefficients of `x`, producing a value in `R`. -/ def ghost_component (n : β„•) : π•Ž R β†’+* R := (pi.eval_ring_hom _ n).comp ghost_map lemma ghost_component_apply (n : β„•) (x : π•Ž R) : ghost_component n x = aeval x.coeff (W_ β„€ n) := rfl @[simp] lemma ghost_map_apply (x : π•Ž R) (n : β„•) : ghost_map x n = ghost_component n x := rfl section invertible variables (p R) [invertible (p : R)] /-- `witt_vector.ghost_map` is a ring isomorphism when `p` is invertible in `R`. -/ def ghost_equiv : π•Ž R ≃+* (β„• β†’ R) := { .. (ghost_map : π•Ž R β†’+* (β„• β†’ R)), .. (ghost_equiv' p R) } @[simp] lemma ghost_equiv_coe : (ghost_equiv p R : π•Ž R β†’+* (β„• β†’ R)) = ghost_map := rfl lemma ghost_map.bijective_of_invertible : function.bijective (ghost_map : π•Ž R β†’ β„• β†’ R) := (ghost_equiv p R).bijective end invertible /-- `witt_vector.coeff x 0` as a `ring_hom` -/ @[simps] def constant_coeff : π•Ž R β†’+* R := { to_fun := Ξ» x, x.coeff 0, map_zero' := by simp, map_one' := by simp, map_add' := add_coeff_zero, map_mul' := mul_coeff_zero } instance [nontrivial R] : nontrivial (π•Ž R) := constant_coeff.domain_nontrivial end witt_vector
a7d8383044de56a0401ada027663bd35f308277b
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Elab/Tactic/Delta.lean
7171b785a4217395fb6f2dfb8118e05e24b6167b
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,437
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Tactic.Delta import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.Location namespace Lean.Elab.Tactic open Meta def deltaLocalDecl (declNames : Array Name) (fvarId : FVarId) : TacticM Unit := do let mvarId ← getMainGoal let localDecl ← fvarId.getDecl let typeNew ← deltaExpand localDecl.type declNames.contains if typeNew == localDecl.type then throwTacticEx `delta mvarId m!"did not delta reduce {declNames} at {localDecl.userName}" replaceMainGoal [← mvarId.replaceLocalDeclDefEq fvarId typeNew] def deltaTarget (declNames : Array Name) : TacticM Unit := do let mvarId ← getMainGoal let target ← getMainTarget let targetNew ← deltaExpand target declNames.contains if targetNew == target then throwTacticEx `delta mvarId m!"did not delta reduce {declNames}" replaceMainGoal [← mvarId.replaceTargetDefEq targetNew] /-- "delta " ident+ (location)? -/ @[builtin_tactic Lean.Parser.Tactic.delta] def evalDelta : Tactic := fun stx => do let declNames ← stx[1].getArgs.mapM resolveGlobalConstNoOverloadWithInfo let loc := expandOptLocation stx[2] withLocation loc (deltaLocalDecl declNames) (deltaTarget declNames) (throwTacticEx `delta Β· m!"did not delta reduce {declNames}") end Lean.Elab.Tactic
46e02437e7526859054d78123aa51853422cf02a
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/PrettyPrinter/Delaborator/Builtins.lean
08096fa0fef9a1259f68af55b36a4c1d99963c30
[ "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
33,393
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.PrettyPrinter.Delaborator.Basic import Lean.PrettyPrinter.Delaborator.SubExpr import Lean.PrettyPrinter.Delaborator.TopDownAnalyze import Lean.Parser namespace Lean.PrettyPrinter.Delaborator open Lean.Meta open Lean.Parser.Term open SubExpr open TSyntax.Compat def maybeAddBlockImplicit (ident : Syntax) : DelabM Syntax := do if ← getPPOption getPPAnalysisBlockImplicit then `(@$ident:ident) else pure ident def unfoldMDatas : Expr β†’ Expr | Expr.mdata _ e => unfoldMDatas e | e => e @[builtinDelab fvar] def delabFVar : Delab := do let Expr.fvar fvarId ← getExpr | unreachable! try let l ← fvarId.getDecl maybeAddBlockImplicit (mkIdent l.userName) catch _ => -- loose free variable, use internal name maybeAddBlockImplicit <| mkIdent fvarId.name -- loose bound variable, use pseudo syntax @[builtinDelab bvar] def delabBVar : Delab := do let Expr.bvar idx ← getExpr | unreachable! pure $ mkIdent $ Name.mkSimple $ "#" ++ toString idx @[builtinDelab mvar] def delabMVar : Delab := do let Expr.mvar n ← getExpr | unreachable! let mvarDecl ← n.getDecl let n := match mvarDecl.userName with | Name.anonymous => n.name.replacePrefix `_uniq `m | n => n `(?$(mkIdent n)) @[builtinDelab sort] def delabSort : Delab := do let Expr.sort l ← getExpr | unreachable! match l with | Level.zero => `(Prop) | Level.succ .zero => `(Type) | _ => match l.dec with | some l' => `(Type $(Level.quote l' max_prec)) | none => `(Sort $(Level.quote l max_prec)) def unresolveNameGlobal (nβ‚€ : Name) : DelabM Name := do if nβ‚€.hasMacroScopes then return nβ‚€ if (← getPPOption getPPFullNames) then match (← resolveGlobalName nβ‚€) with | [(potentialMatch, _)] => if potentialMatch == nβ‚€ then return nβ‚€ else return rootNamespace ++ nβ‚€ | _ => return nβ‚€ -- if can't resolve, return the original let mut initialNames := (getRevAliases (← getEnv) nβ‚€).toArray initialNames := initialNames.push (rootNamespace ++ nβ‚€) for initialName in initialNames do match (← unresolveNameCore initialName) with | none => continue | some n => return n return nβ‚€ -- if can't resolve, return the original where unresolveNameCore (n : Name) : DelabM (Option Name) := do let mut revComponents := n.components' let mut candidate := Name.anonymous for _ in [:revComponents.length] do match revComponents with | [] => return none | cmpt::rest => candidate := cmpt ++ candidate; revComponents := rest match (← resolveGlobalName candidate) with | [(potentialMatch, _)] => if potentialMatch == nβ‚€ then return some candidate else continue | _ => continue return none -- NOTE: not a registered delaborator, as `const` is never called (see [delab] description) def delabConst : Delab := do let Expr.const cβ‚€ ls ← getExpr | unreachable! let cβ‚€ := if (← getPPOption getPPPrivateNames) then cβ‚€ else (privateToUserName? cβ‚€).getD cβ‚€ let mut c ← unresolveNameGlobal cβ‚€ let stx ← if ls.isEmpty || !(← getPPOption getPPUniverses) then if (← getLCtx).usesUserName c then -- `c` is also a local declaration if c == cβ‚€ && !(← read).inPattern then -- `c` is the fully qualified named. So, we append the `_root_` prefix c := `_root_ ++ c else c := cβ‚€ pure <| mkIdent c else `($(mkIdent c).{$[$(ls.toArray.map quote)],*}) let mut stx ← maybeAddBlockImplicit stx if (← getPPOption getPPTagAppFns) then stx ← annotateCurPos stx addTermInfo (← getPos) stx (← getExpr) return stx def withMDataOptions [Inhabited Ξ±] (x : DelabM Ξ±) : DelabM Ξ± := do match ← getExpr with | Expr.mdata m .. => let mut posOpts := (← read).optionsPerPos let pos ← getPos for (k, v) in m do if (`pp).isPrefixOf k then let opts := posOpts.find? pos |>.getD {} posOpts := posOpts.insert pos (opts.insert k v) withReader ({ Β· with optionsPerPos := posOpts }) $ withMDataExpr x | _ => x partial def withMDatasOptions [Inhabited Ξ±] (x : DelabM Ξ±) : DelabM Ξ± := do if (← getExpr).isMData then withMDataOptions (withMDatasOptions x) else x def delabAppFn : Delab := do if (← getExpr).consumeMData.isConst then withMDatasOptions delabConst else delab structure ParamKind where name : Name bInfo : BinderInfo defVal : Option Expr := none isAutoParam : Bool := false def ParamKind.isRegularExplicit (param : ParamKind) : Bool := param.bInfo.isExplicit && !param.isAutoParam && param.defVal.isNone /-- Return array with n-th element set to kind of n-th parameter of `e`. -/ partial def getParamKinds : DelabM (Array ParamKind) := do let e ← getExpr try withTransparency TransparencyMode.all do forallTelescopeArgs e.getAppFn e.getAppArgs fun params _ => do params.mapM fun param => do let l ← param.fvarId!.getDecl pure { name := l.userName, bInfo := l.binderInfo, defVal := l.type.getOptParamDefault?, isAutoParam := l.type.isAutoParam } catch _ => pure #[] -- recall that expr may be nonsensical where forallTelescopeArgs f args k := do forallBoundedTelescope (← inferType f) args.size fun xs b => if xs.isEmpty || xs.size == args.size then -- we still want to consider optParams forallTelescopeReducing b fun ys b => k (xs ++ ys) b else forallTelescopeArgs (mkAppN f $ args.shrink xs.size) (args.extract xs.size args.size) fun ys b => k (xs ++ ys) b @[builtinDelab app] def delabAppExplicit : Delab := do let paramKinds ← getParamKinds let tagAppFn ← getPPOption getPPTagAppFns let (fnStx, _, argStxs) ← withAppFnArgs (do let stx ← withOptionAtCurrPos `pp.tagAppFns tagAppFn delabAppFn let needsExplicit := stx.raw.getKind != ``Lean.Parser.Term.explicit let stx ← if needsExplicit then `(@$stx) else pure stx pure (stx, paramKinds.toList, #[])) (fun ⟨fnStx, paramKinds, argStxs⟩ => do let isInstImplicit := match paramKinds with | [] => false | param :: _ => param.bInfo == BinderInfo.instImplicit let argStx ← if ← getPPOption getPPAnalysisHole then `(_) else if isInstImplicit == true then let stx ← if ← getPPOption getPPInstances then delab else `(_) if ← getPPOption getPPInstanceTypes then let typeStx ← withType delab `(($stx : $typeStx)) else pure stx else delab pure (fnStx, paramKinds.tailD [], argStxs.push argStx)) return Syntax.mkApp fnStx argStxs def shouldShowMotive (motive : Expr) (opts : Options) : MetaM Bool := do pure (getPPMotivesAll opts) <||> (pure (getPPMotivesPi opts) <&&> returnsPi motive) <||> (pure (getPPMotivesNonConst opts) <&&> isNonConstFun motive) def isRegularApp : DelabM Bool := do let e ← getExpr if not (unfoldMDatas e.getAppFn).isConst then return false if ← withNaryFn (withMDatasOptions (getPPOption getPPUniverses <||> getPPOption getPPAnalysisBlockImplicit)) then return false for i in [:e.getAppNumArgs] do if ← withNaryArg i (getPPOption getPPAnalysisNamedArg) then return false return true def unexpandRegularApp (stx : Syntax) : Delab := do let Expr.const c .. := (unfoldMDatas (← getExpr).getAppFn) | unreachable! let fs := appUnexpanderAttribute.getValues (← getEnv) c let ref ← getRef fs.firstM fun f => match f stx |>.run ref |>.run () with | EStateM.Result.ok stx _ => pure stx | _ => failure -- abbrev coe {Ξ± : Sort u} {Ξ² : Sort v} (a : Ξ±) [CoeT Ξ± a Ξ²] : Ξ² -- abbrev coeFun {Ξ± : Sort u} {Ξ³ : Ξ± β†’ Sort v} (a : Ξ±) [CoeFun Ξ± Ξ³] : Ξ³ a def unexpandCoe (stx : Syntax) : Delab := whenPPOption getPPCoercions do if not (isCoe (← getExpr)) then failure match stx with | `($_ $arg) => return arg | `($_ $args*) => `($(args.get! 0) $(args.eraseIdx 0)*) | _ => failure def unexpandStructureInstance (stx : Syntax) : Delab := whenPPOption getPPStructureInstances do let env ← getEnv let e ← getExpr let some s ← pure $ e.isConstructorApp? env | failure guard $ isStructure env s.induct; /- If implicit arguments should be shown, and the structure has parameters, we should not pretty print using { ... }, because we will not be able to see the parameters. -/ let fieldNames := getStructureFields env s.induct let mut fields := #[] guard $ fieldNames.size == stx[1].getNumArgs let args := e.getAppArgs let fieldVals := args.extract s.numParams args.size for idx in [:fieldNames.size] do let fieldName := fieldNames[idx]! let fieldId := mkIdent fieldName let fieldPos ← nextExtraPos let fieldId := annotatePos fieldPos fieldId addFieldInfo fieldPos (s.induct ++ fieldName) fieldName fieldId fieldVals[idx]! let field ← `(structInstField|$fieldId:ident := $(stx[1][idx])) fields := fields.push field let tyStx ← withType do if (← getPPOption getPPStructureInstanceType) then delab >>= pure ∘ some else pure none `({ $fields,* $[: $tyStx]? }) @[builtinDelab app] def delabAppImplicit : Delab := do -- TODO: always call the unexpanders, make them guard on the right # args? let paramKinds ← getParamKinds if ← getPPOption getPPExplicit then if paramKinds.any (fun param => !param.isRegularExplicit) then failure -- If the application has an implicit function type, fall back to delabAppExplicit. -- This is e.g. necessary for `@Eq`. let isImplicitApp ← try let ty ← whnf (← inferType (← getExpr)) pure <| ty.isForall && (ty.binderInfo == BinderInfo.implicit || ty.binderInfo == BinderInfo.instImplicit) catch _ => pure false if isImplicitApp then failure let tagAppFn ← getPPOption getPPTagAppFns let (fnStx, _, argStxs) ← withAppFnArgs (withOptionAtCurrPos `pp.tagAppFns tagAppFn <| return (← delabAppFn, paramKinds.toList, #[])) (fun (fnStx, paramKinds, argStxs) => do let arg ← getExpr let opts ← getOptions let mkNamedArg (name : Name) (argStx : Syntax) : DelabM Syntax := do `(Parser.Term.namedArgument| ($(mkIdent name) := $argStx)) let argStx? : Option Syntax ← if ← getPPOption getPPAnalysisSkip then pure none else if ← getPPOption getPPAnalysisHole then `(_) else match paramKinds with | [] => delab | param :: rest => if param.defVal.isSome && rest.isEmpty then let v := param.defVal.get! if !v.hasLooseBVars && v == arg then pure none else delab else if !param.isRegularExplicit && param.defVal.isNone then if ← getPPOption getPPAnalysisNamedArg <||> (pure (param.name == `motive) <&&> shouldShowMotive arg opts) then some <$> mkNamedArg param.name (← delab) else pure none else delab let argStxs := match argStx? with | none => argStxs | some stx => argStxs.push stx pure (fnStx, paramKinds.tailD [], argStxs)) let stx := Syntax.mkApp fnStx argStxs if ← isRegularApp then (guard (← getPPOption getPPNotation) *> unexpandRegularApp stx) <|> (guard (← getPPOption getPPStructureInstances) *> unexpandStructureInstance stx) <|> (guard (← getPPOption getPPNotation) *> unexpandCoe stx) <|> pure stx else pure stx /-- State for `delabAppMatch` and helpers. -/ structure AppMatchState where info : MatcherInfo matcherTy : Expr params : Array Expr := #[] motive : Option (Term Γ— Expr) := none motiveNamed : Bool := false discrs : Array Term := #[] varNames : Array (Array Name) := #[] rhss : Array Term := #[] -- additional arguments applied to the result of the `match` expression moreArgs : Array Term := #[] /-- Extract arguments of motive applications from the matcher type. For the example below: `#[#[`([])], #[`(a::as)]]` -/ private partial def delabPatterns (st : AppMatchState) : DelabM (Array (Array Term)) := withReader (fun ctx => { ctx with inPattern := true, optionsPerPos := {} }) do let ty ← instantiateForall st.matcherTy st.params -- need to reduce `let`s that are lifted into the matcher type forallTelescopeReducing ty fun params _ => do -- skip motive and discriminators let alts := Array.ofSubarray params[1 + st.discrs.size:] alts.mapIdxM fun idx alt => do let ty ← inferType alt -- TODO: this is a hack; we are accessing the expression out-of-sync with the position -- Currently, we reset `optionsPerPos` at the beginning of `delabPatterns` to avoid -- incorrectly considering annotations. withTheReader SubExpr ({ Β· with expr := ty }) $ usingNames st.varNames[idx]! do withAppFnArgs (pure #[]) (fun pats => do pure $ pats.push (← delab)) where usingNames {Ξ±} (varNames : Array Name) (x : DelabM Ξ±) : DelabM Ξ± := usingNamesAux 0 varNames x usingNamesAux {Ξ±} (i : Nat) (varNames : Array Name) (x : DelabM Ξ±) : DelabM Ξ± := if i < varNames.size then withBindingBody varNames[i]! <| usingNamesAux (i+1) varNames x else x /-- Skip `numParams` binders, and execute `x varNames` where `varNames` contains the new binder names. -/ private partial def skippingBinders {Ξ±} (numParams : Nat) (x : Array Name β†’ DelabM Ξ±) : DelabM Ξ± := loop numParams #[] where loop : Nat β†’ Array Name β†’ DelabM Ξ± | 0, varNames => x varNames | n+1, varNames => do let rec visitLambda : DelabM Ξ± := do let varName := (← getExpr).bindingName!.eraseMacroScopes -- Pattern variables cannot shadow each other if varNames.contains varName then let varName := (← getLCtx).getUnusedName varName withBindingBody varName do loop n (varNames.push varName) else withBindingBodyUnusedName fun id => do loop n (varNames.push id.getId) let e ← getExpr if e.isLambda then visitLambda else -- eta expand `e` let e ← forallTelescopeReducing (← inferType e) fun xs _ => do if xs.size == 1 && (← inferType xs[0]!).isConstOf ``Unit then -- `e` might be a thunk create by the dependent pattern matching compiler, and `xs[0]` may not even be a pattern variable. -- If it is a pattern variable, it doesn't look too bad to use `()` instead of the pattern variable. -- If it becomes a problem in the future, we should modify the dependent pattern matching compiler, and make sure -- it adds an annotation to distinguish these two cases. mkLambdaFVars xs (mkApp e (mkConst ``Unit.unit)) else mkLambdaFVars xs (mkAppN e xs) withTheReader SubExpr (fun ctx => { ctx with expr := e }) visitLambda /-- Delaborate applications of "matchers" such as ``` List.map.match_1 : {Ξ± : Type _} β†’ (motive : List Ξ± β†’ Sort _) β†’ (x : List Ξ±) β†’ (Unit β†’ motive List.nil) β†’ ((a : Ξ±) β†’ (as : List Ξ±) β†’ motive (a :: as)) β†’ motive x ``` -/ @[builtinDelab app] def delabAppMatch : Delab := whenPPOption getPPNotation <| whenPPOption getPPMatch do -- incrementally fill `AppMatchState` from arguments let st ← withAppFnArgs (do let (Expr.const c us) ← getExpr | failure let (some info) ← getMatcherInfo? c | failure return { matcherTy := (← getConstInfo c).instantiateTypeLevelParams us, info := info : AppMatchState }) (fun st => do if st.params.size < st.info.numParams then return { st with params := st.params.push (← getExpr) } else if st.motive.isNone then -- store motive argument separately let lamMotive ← getExpr let piMotive ← lambdaTelescope lamMotive fun xs body => mkForallFVars xs body -- TODO: pp.analyze has not analyzed `piMotive`, only `lamMotive` -- Thus the binder types won't have any annotations let piStx ← withTheReader SubExpr (fun cfg => { cfg with expr := piMotive }) delab let named ← getPPOption getPPAnalysisNamedArg return { st with motive := (piStx, lamMotive), motiveNamed := named } else if st.discrs.size < st.info.numDiscrs then let idx := st.discrs.size let discr ← delab if let some hName := st.info.discrInfos[idx]!.hName? then -- TODO: we should check whether the corresponding binder name, matches `hName`. -- If it does not we should pretty print this `match` as a regular application. return { st with discrs := st.discrs.push (← `(matchDiscr| $(mkIdent hName) : $discr)) } else return { st with discrs := st.discrs.push (← `(matchDiscr| $discr:term)) } else if st.rhss.size < st.info.altNumParams.size then /- We save the variables names here to be able to implement safe_shadowing. The pattern delaboration must use the names saved here. -/ let (varNames, rhs) ← skippingBinders st.info.altNumParams[st.rhss.size]! fun varNames => do let rhs ← delab return (varNames, rhs) return { st with rhss := st.rhss.push rhs, varNames := st.varNames.push varNames } else return { st with moreArgs := st.moreArgs.push (← delab) }) if st.discrs.size < st.info.numDiscrs || st.rhss.size < st.info.altNumParams.size then -- underapplied failure match st.discrs, st.rhss with | #[discr], #[] => let stx ← `(nomatch $discr) return Syntax.mkApp stx st.moreArgs | _, #[] => failure | _, _ => let pats ← delabPatterns st let stx ← do let (piStx, lamMotive) := st.motive.get! let opts ← getOptions -- TODO: disable the match if other implicits are needed? if ← pure st.motiveNamed <||> shouldShowMotive lamMotive opts then `(match (motive := $piStx) $[$st.discrs:matchDiscr],* with $[| $pats,* => $st.rhss]*) else `(match $[$st.discrs:matchDiscr],* with $[| $pats,* => $st.rhss]*) return Syntax.mkApp stx st.moreArgs /-- Delaborate applications of the form `(fun x => b) v` as `let_fun x := v; b` -/ def delabLetFun : Delab := do let stxV ← withAppArg delab withAppFn do let Expr.lam n _ b _ ← getExpr | unreachable! let n ← getUnusedName n b let stxB ← withBindingBody n delab if ← getPPOption getPPLetVarTypes <||> getPPOption getPPAnalysisLetVarType then let stxT ← withBindingDomain delab `(let_fun $(mkIdent n) : $stxT := $stxV; $stxB) else `(let_fun $(mkIdent n) := $stxV; $stxB) @[builtinDelab mdata] def delabMData : Delab := do if let some _ := inaccessible? (← getExpr) then let s ← withMDataExpr delab if (← read).inPattern then `(.($s)) -- We only include the inaccessible annotation when we are delaborating patterns else return s else if isLetFun (← getExpr) && getPPNotation (← getOptions) then withMDataExpr <| delabLetFun else if let some _ := isLHSGoal? (← getExpr) then withMDataExpr <| withAppFn <| withAppArg <| delab else withMDataOptions delab /-- Check for a `Syntax.ident` of the given name anywhere in the tree. This is usually a bad idea since it does not check for shadowing bindings, but in the delaborator we assume that bindings are never shadowed. -/ partial def hasIdent (id : Name) : Syntax β†’ Bool | Syntax.ident _ _ id' _ => id == id' | Syntax.node _ _ args => args.any (hasIdent id) | _ => false /-- Return `true` iff current binder should be merged with the nested binder, if any, into a single binder group: * both binders must have same binder info and domain * they cannot be inst-implicit (`[a b : A]` is not valid syntax) * `pp.binderTypes` must be the same value for both terms * prefer `fun a b` over `fun (a b)` -/ private def shouldGroupWithNext : DelabM Bool := do let e ← getExpr let ppEType ← getPPOption (getPPBinderTypes e) let go (e' : Expr) := do let ppE'Type ← withBindingBody `_ $ getPPOption (getPPBinderTypes e) pure $ e.binderInfo == e'.binderInfo && e.bindingDomain! == e'.bindingDomain! && e'.binderInfo != BinderInfo.instImplicit && ppEType == ppE'Type && (e'.binderInfo != BinderInfo.default || ppE'Type) match e with | Expr.lam _ _ e'@(Expr.lam _ _ _ _) _ => go e' | Expr.forallE _ _ e'@(Expr.forallE _ _ _ _) _ => go e' | _ => pure false where getPPBinderTypes (e : Expr) := if e.isForall then getPPPiBinderTypes else getPPFunBinderTypes private partial def delabBinders (delabGroup : Array Syntax β†’ Syntax β†’ Delab) : optParam (Array Syntax) #[] β†’ Delab -- Accumulate names (`Syntax.ident`s with position information) of the current, unfinished -- binder group `(d e ...)` as determined by `shouldGroupWithNext`. We cannot do grouping -- inside-out, on the Syntax level, because it depends on comparing the Expr binder types. | curNames => do if ← shouldGroupWithNext then -- group with nested binder => recurse immediately withBindingBodyUnusedName fun stxN => delabBinders delabGroup (curNames.push stxN) else -- don't group => delab body and prepend current binder group let (stx, stxN) ← withBindingBodyUnusedName fun stxN => return (← delab, stxN) delabGroup (curNames.push stxN) stx @[builtinDelab lam] def delabLam : Delab := delabBinders fun curNames stxBody => do let e ← getExpr let stxT ← withBindingDomain delab let ppTypes ← getPPOption getPPFunBinderTypes let usedDownstream := curNames.any (fun n => hasIdent n.getId stxBody) -- leave lambda implicit if possible -- TODO: for now we just always block implicit lambdas when delaborating. We can revisit. -- Note: the current issue is that it requires state, i.e. if *any* previous binder was implicit, -- it doesn't seem like we can leave a subsequent binder implicit. let blockImplicitLambda := true /- let blockImplicitLambda := expl || e.binderInfo == BinderInfo.default || -- Note: the following restriction fixes many issues with roundtripping, -- but this condition may still not be perfectly in sync with the elaborator. e.binderInfo == BinderInfo.instImplicit || Elab.Term.blockImplicitLambda stxBody || usedDownstream -/ if !blockImplicitLambda then pure stxBody else let defaultCase (_ : Unit) : Delab := do if ppTypes then -- "default" binder group is the only one that expects binder names -- as a term, i.e. a single `Syntax.ident` or an application thereof let stxCurNames ← if curNames.size > 1 then `($(curNames.get! 0) $(curNames.eraseIdx 0)*) else pure $ curNames.get! 0; `(funBinder| ($stxCurNames : $stxT)) else pure curNames.back -- here `curNames.size == 1` let group ← match e.binderInfo, ppTypes with | BinderInfo.default, _ => defaultCase () | BinderInfo.auxDecl, _ => defaultCase () | BinderInfo.implicit, true => `(funBinder| {$curNames* : $stxT}) | BinderInfo.implicit, false => `(funBinder| {$curNames*}) | BinderInfo.strictImplicit, true => `(funBinder| ⦃$curNames* : $stxT⦄) | BinderInfo.strictImplicit, false => `(funBinder| ⦃$curNames*⦄) | BinderInfo.instImplicit, _ => if usedDownstream then `(funBinder| [$curNames.back : $stxT]) -- here `curNames.size == 1` else `(funBinder| [$stxT]) match stxBody with | `(fun $binderGroups* => $stxBody) => `(fun $group $binderGroups* => $stxBody) | _ => `(fun $group => $stxBody) @[builtinDelab forallE] def delabForall : Delab := delabBinders fun curNames stxBody => do let e ← getExpr let prop ← try isProp e catch _ => pure false let stxT ← withBindingDomain delab let group ← match e.binderInfo with | BinderInfo.implicit => `(bracketedBinderF|{$curNames* : $stxT}) | BinderInfo.strictImplicit => `(bracketedBinderF|⦃$curNames* : $stxT⦄) -- here `curNames.size == 1` | BinderInfo.instImplicit => `(bracketedBinderF|[$curNames.back : $stxT]) | _ => -- heuristic: use non-dependent arrows only if possible for whole group to avoid -- noisy mix like `(Ξ± : Type) β†’ Type β†’ (Ξ³ : Type) β†’ ...`. let dependent := curNames.any fun n => hasIdent n.getId stxBody -- NOTE: non-dependent arrows are available only for the default binder info if dependent then if prop && !(← getPPOption getPPPiBinderTypes) then return ← `(βˆ€ $curNames:ident*, $stxBody) else `(bracketedBinderF|($curNames* : $stxT)) else return ← curNames.foldrM (fun _ stxBody => `($stxT β†’ $stxBody)) stxBody if prop then match stxBody with | `(βˆ€ $groups*, $stxBody) => `(βˆ€ $group $groups*, $stxBody) | _ => `(βˆ€ $group, $stxBody) else `($group:bracketedBinder β†’ $stxBody) @[builtinDelab letE] def delabLetE : Delab := do let Expr.letE n t v b _ ← getExpr | unreachable! let n ← getUnusedName n b let stxV ← descend v 1 delab let stxB ← withLetDecl n t v fun fvar => let b := b.instantiate1 fvar descend b 2 delab if ← getPPOption getPPLetVarTypes <||> getPPOption getPPAnalysisLetVarType then let stxT ← descend t 0 delab `(let $(mkIdent n) : $stxT := $stxV; $stxB) else `(let $(mkIdent n) := $stxV; $stxB) @[builtinDelab lit] def delabLit : Delab := do let Expr.lit l ← getExpr | unreachable! match l with | Literal.natVal n => pure $ quote n | Literal.strVal s => pure $ quote s -- `@OfNat.ofNat _ n _` ~> `n` @[builtinDelab app.OfNat.ofNat] def delabOfNat : Delab := whenPPOption getPPCoercions do let .app (.app _ (.lit (.natVal n))) _ ← getExpr | failure return quote n -- `@OfDecimal.ofDecimal _ _ m s e` ~> `m*10^(sign * e)` where `sign == 1` if `s = false` and `sign = -1` if `s = true` @[builtinDelab app.OfScientific.ofScientific] def delabOfScientific : Delab := whenPPOption getPPCoercions do let expr ← getExpr guard <| expr.getAppNumArgs == 5 let .lit (.natVal m) ← pure (expr.getArg! 2) | failure let .lit (.natVal e) ← pure (expr.getArg! 4) | failure let s ← match expr.getArg! 3 with | Expr.const ``Bool.true _ => pure true | Expr.const ``Bool.false _ => pure false | _ => failure let str := toString m if s && e == str.length then return Syntax.mkScientificLit ("0." ++ str) else if s && e < str.length then let mStr := str.extract 0 ⟨str.length - e⟩ let eStr := str.extract ⟨str.length - e⟩ ⟨str.length⟩ return Syntax.mkScientificLit (mStr ++ "." ++ eStr) else return Syntax.mkScientificLit (str ++ "e" ++ (if s then "-" else "") ++ toString e) /-- Delaborate a projection primitive. These do not usually occur in user code, but are pretty-printed when e.g. `#print`ing a projection function. -/ @[builtinDelab proj] def delabProj : Delab := do let Expr.proj _ idx _ ← getExpr | unreachable! let e ← withProj delab -- not perfectly authentic: elaborates to the `idx`-th named projection -- function (e.g. `e.1` is `Prod.fst e`), which unfolds to the actual -- `proj`. let idx := Syntax.mkLit fieldIdxKind (toString (idx + 1)); `($(e).$idx:fieldIdx) /-- Delaborate a call to a projection function such as `Prod.fst`. -/ @[builtinDelab app] def delabProjectionApp : Delab := whenPPOption getPPStructureProjections $ do let e@(Expr.app fn _) ← getExpr | failure let .const c@(.str _ f) _ ← pure fn.getAppFn | failure let env ← getEnv let some info ← pure $ env.getProjectionFnInfo? c | failure -- can't use with classes since the instance parameter is implicit guard $ !info.fromClass -- projection function should be fully applied (#struct params + 1 instance parameter) -- TODO: support over-application guard $ e.getAppNumArgs == info.numParams + 1 -- If pp.explicit is true, and the structure has parameters, we should not -- use field notation because we will not be able to see the parameters. let expl ← getPPOption getPPExplicit guard $ !expl || info.numParams == 0 let appStx ← withAppArg delab `($(appStx).$(mkIdent f):ident) @[builtinDelab app.dite] def delabDIte : Delab := whenPPOption getPPNotation do -- Note: we keep this as a delaborator for now because it actually accesses the expression. guard $ (← getExpr).getAppNumArgs == 5 let c ← withAppFn $ withAppFn $ withAppFn $ withAppArg delab let (t, h) ← withAppFn $ withAppArg $ delabBranch none let (e, _) ← withAppArg $ delabBranch h `(if $(mkIdent h):ident : $c then $t else $e) where delabBranch (h? : Option Name) : DelabM (Syntax Γ— Name) := do let e ← getExpr guard e.isLambda let h ← match h? with | some h => return (← withBindingBody h delab, h) | none => withBindingBodyUnusedName fun h => do return (← delab, h.getId) @[builtinDelab app.cond] def delabCond : Delab := whenPPOption getPPNotation do guard $ (← getExpr).getAppNumArgs == 4 let c ← withAppFn $ withAppFn $ withAppArg delab let t ← withAppFn $ withAppArg delab let e ← withAppArg delab `(bif $c then $t else $e) @[builtinDelab app.namedPattern] def delabNamedPattern : Delab := do -- Note: we keep this as a delaborator because it accesses the DelabM context guard (← read).inPattern guard $ (← getExpr).getAppNumArgs == 4 let x ← withAppFn $ withAppFn $ withAppArg delab let p ← withAppFn $ withAppArg delab -- TODO: we should hide `h` if it has an inaccessible name and is not used in the rhs let h ← withAppArg delab guard x.raw.isIdent `($x:ident@$h:ident:$p:term) -- Sigma and PSigma delaborators def delabSigmaCore (sigma : Bool) : Delab := whenPPOption getPPNotation do guard $ (← getExpr).getAppNumArgs == 2 guard $ (← getExpr).appArg!.isLambda withAppArg do let Ξ± ← withBindingDomain delab let bodyExpr := (← getExpr).bindingBody! withBindingBodyUnusedName fun n => do let b ← delab if bodyExpr.hasLooseBVars then if sigma then `(($n:ident : $Ξ±) Γ— $b) else `(($n:ident : $Ξ±) Γ—' $b) else if sigma then `((_ : $Ξ±) Γ— $b) else `((_ : $Ξ±) Γ—' $b) @[builtinDelab app.Sigma] def delabSigma : Delab := delabSigmaCore (sigma := true) @[builtinDelab app.PSigma] def delabPSigma : Delab := delabSigmaCore (sigma := false) partial def delabDoElems : DelabM (List Syntax) := do let e ← getExpr if e.isAppOfArity ``Bind.bind 6 then -- Bind.bind.{u, v} : {m : Type u β†’ Type v} β†’ [self : Bind m] β†’ {Ξ± Ξ² : Type u} β†’ m Ξ± β†’ (Ξ± β†’ m Ξ²) β†’ m Ξ² let Ξ± := e.getAppArgs[2]! let ma ← withAppFn $ withAppArg delab withAppArg do match (← getExpr) with | Expr.lam _ _ body _ => withBindingBodyUnusedName fun n => do if body.hasLooseBVars then prependAndRec `(doElem|let $n:term ← $ma:term) else if Ξ±.isConstOf ``Unit || Ξ±.isConstOf ``PUnit then prependAndRec `(doElem|$ma:term) else prependAndRec `(doElem|let _ ← $ma:term) | _ => failure else if e.isLet then let Expr.letE n t v b _ ← getExpr | unreachable! let n ← getUnusedName n b let stxT ← descend t 0 delab let stxV ← descend v 1 delab withLetDecl n t v fun fvar => let b := b.instantiate1 fvar descend b 2 $ prependAndRec `(doElem|let $(mkIdent n) : $stxT := $stxV) else let stx ← delab return [← `(doElem|$stx:term)] where prependAndRec x : DelabM _ := List.cons <$> x <*> delabDoElems @[builtinDelab app.Bind.bind] def delabDo : Delab := whenPPOption getPPNotation do guard <| (← getExpr).isAppOfArity ``Bind.bind 6 let elems ← delabDoElems let items ← elems.toArray.mapM (`(doSeqItem|$(Β·):doElem)) `(do $items:doSeqItem*) def reifyName : Expr β†’ DelabM Name | .const ``Lean.Name.anonymous .. => return Name.anonymous | .app (.app (.const ``Lean.Name.str ..) n) (.lit (.strVal s)) => return (← reifyName n).mkStr s | .app (.app (.const ``Lean.Name.num ..) n) (.lit (.natVal i)) => return (← reifyName n).mkNum i | _ => failure @[builtinDelab app.Lean.Name.str] def delabNameMkStr : Delab := whenPPOption getPPNotation do let n ← reifyName (← getExpr) -- not guaranteed to be a syntactically valid name, but usually more helpful than the explicit version return mkNode ``Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{n}"] @[builtinDelab app.Lean.Name.num] def delabNameMkNum : Delab := delabNameMkStr end Lean.PrettyPrinter.Delaborator
0f16f1da6d39cdc20f1dab4cfd8db05757d762c1
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/linear_algebra/matrix/basis.lean
04691d56bd65667a7c648d6550f884ef26cf50e7
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,643
lean
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Patrick Massot, Casper Putz, Anne Baanen -/ import linear_algebra.matrix.reindex import linear_algebra.matrix.to_lin /-! # Bases and matrices This file defines the map `basis.to_matrix` that sends a family of vectors to the matrix of their coordinates with respect to some basis. ## Main definitions * `basis.to_matrix e v` is the matrix whose `i, j`th entry is `e.repr (v j) i` * `basis.to_matrix_equiv` is `basis.to_matrix` bundled as a linear equiv ## Main results * `linear_map.to_matrix_id_eq_basis_to_matrix`: `linear_map.to_matrix b c id` is equal to `basis.to_matrix b c` * `basis.to_matrix_mul_to_matrix`: multiplying `basis.to_matrix` with another `basis.to_matrix` gives a `basis.to_matrix` ## Tags matrix, basis -/ noncomputable theory open linear_map matrix set submodule open_locale big_operators open_locale matrix section basis_to_matrix variables {ΞΉ ΞΉ' ΞΊ ΞΊ' : Type*} variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M] open function matrix /-- From a basis `e : ΞΉ β†’ M` and a family of vectors `v : ΞΉ' β†’ M`, make the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def basis.to_matrix (e : basis ΞΉ R M) (v : ΞΉ' β†’ M) : matrix ΞΉ ΞΉ' R := Ξ» i j, e.repr (v j) i variables (e : basis ΞΉ R M) (v : ΞΉ' β†’ M) (i : ΞΉ) (j : ΞΉ') namespace basis lemma to_matrix_apply : e.to_matrix v i j = e.repr (v j) i := rfl lemma to_matrix_transpose_apply : (e.to_matrix v)α΅€ j = e.repr (v j) := funext $ (Ξ» _, rfl) lemma to_matrix_eq_to_matrix_constr [fintype ΞΉ] [decidable_eq ΞΉ] (v : ΞΉ β†’ M) : e.to_matrix v = linear_map.to_matrix e e (e.constr β„• v) := by { ext, rw [basis.to_matrix_apply, linear_map.to_matrix_apply, basis.constr_basis] } @[simp] lemma to_matrix_self [decidable_eq ΞΉ] : e.to_matrix e = 1 := begin rw basis.to_matrix, ext i j, simp [basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm] end lemma to_matrix_update [decidable_eq ΞΉ'] (x : M) : e.to_matrix (function.update v j x) = matrix.update_column (e.to_matrix v) j (e.repr x) := begin ext i' k, rw [basis.to_matrix, matrix.update_column_apply, e.to_matrix_apply], split_ifs, { rw [h, update_same j x v] }, { rw update_noteq h }, end @[simp] lemma sum_to_matrix_smul_self [fintype ΞΉ] : βˆ‘ (i : ΞΉ), e.to_matrix v i j β€’ e i = v j := by simp_rw [e.to_matrix_apply, e.sum_repr] @[simp] lemma to_lin_to_matrix [fintype ΞΉ] [fintype ΞΉ'] [decidable_eq ΞΉ'] (v : basis ΞΉ' R M) : matrix.to_lin v e (e.to_matrix v) = id := v.ext (Ξ» i, by rw [to_lin_self, id_apply, e.sum_to_matrix_smul_self]) /-- From a basis `e : ΞΉ β†’ M`, build a linear equivalence between families of vectors `v : ΞΉ β†’ M`, and matrices, making the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def to_matrix_equiv [fintype ΞΉ] (e : basis ΞΉ R M) : (ΞΉ β†’ M) ≃ₗ[R] matrix ΞΉ ΞΉ R := { to_fun := e.to_matrix, map_add' := Ξ» v w, begin ext i j, change _ = _ + _, rw [e.to_matrix_apply, pi.add_apply, linear_equiv.map_add], refl end, map_smul' := begin intros c v, ext i j, rw [e.to_matrix_apply, pi.smul_apply, linear_equiv.map_smul], refl end, inv_fun := Ξ» m j, βˆ‘ i, (m i j) β€’ e i, left_inv := begin intro v, ext j, exact e.sum_to_matrix_smul_self v j end, right_inv := begin intros m, ext k l, simp only [e.to_matrix_apply, ← e.equiv_fun_apply, ← e.equiv_fun_symm_apply, linear_equiv.apply_symm_apply], end } end basis section mul_linear_map_to_matrix variables {N : Type*} [add_comm_group N] [module R N] variables (b : basis ΞΉ R M) (b' : basis ΞΉ' R M) (c : basis ΞΊ R N) (c' : basis ΞΊ' R N) variables (f : M β†’β‚—[R] N) open linear_map section fintype variables [fintype ΞΉ'] [fintype ΞΊ] [fintype ΞΊ'] @[simp] lemma basis_to_matrix_mul_linear_map_to_matrix [decidable_eq ΞΉ'] : c.to_matrix c' ⬝ linear_map.to_matrix b' c' f = linear_map.to_matrix b' c f := (matrix.to_lin b' c).injective (by haveI := classical.dec_eq ΞΊ'; rw [to_lin_to_matrix, to_lin_mul b' c' c, to_lin_to_matrix, c.to_lin_to_matrix, id_comp]) variable [fintype ΞΉ] @[simp] lemma linear_map_to_matrix_mul_basis_to_matrix [decidable_eq ΞΉ] [decidable_eq ΞΉ'] : linear_map.to_matrix b' c' f ⬝ b'.to_matrix b = linear_map.to_matrix b c' f := (matrix.to_lin b c').injective (by rw [to_lin_to_matrix, to_lin_mul b b' c', to_lin_to_matrix, b'.to_lin_to_matrix, comp_id]) /-- A generalization of `linear_map.to_matrix_id`. -/ @[simp] lemma linear_map.to_matrix_id_eq_basis_to_matrix [decidable_eq ΞΉ] : linear_map.to_matrix b b' id = b'.to_matrix b := by { haveI := classical.dec_eq ΞΉ', rw [←@basis_to_matrix_mul_linear_map_to_matrix _ _ ΞΉ, to_matrix_id, matrix.mul_one] } /-- See also `basis.to_matrix_reindex` which gives the `simp` normal form of this result. -/ lemma basis.to_matrix_reindex' [decidable_eq ΞΉ] [decidable_eq ΞΉ'] (b : basis ΞΉ R M) (v : ΞΉ' β†’ M) (e : ΞΉ ≃ ΞΉ') : (b.reindex e).to_matrix v = matrix.reindex_alg_equiv _ e (b.to_matrix (v ∘ e)) := by { ext, simp only [basis.to_matrix_apply, basis.reindex_repr, matrix.reindex_alg_equiv_apply, matrix.reindex_apply, matrix.minor_apply, function.comp_app, e.apply_symm_apply] } end fintype /-- A generalization of `basis.to_matrix_self`, in the opposite direction. -/ @[simp] lemma basis.to_matrix_mul_to_matrix {ΞΉ'' : Type*} [fintype ΞΉ'] (b'' : ΞΉ'' β†’ M) : b.to_matrix b' ⬝ b'.to_matrix b'' = b.to_matrix b'' := begin have := classical.dec_eq ΞΉ, have := classical.dec_eq ΞΉ', haveI := classical.dec_eq ΞΉ'', ext i j, simp only [matrix.mul_apply, basis.to_matrix_apply, basis.sum_repr_mul_repr], end /-- `b.to_matrix b'` and `b'.to_matrix b` are inverses. -/ lemma basis.to_matrix_mul_to_matrix_flip [decidable_eq ΞΉ] [fintype ΞΉ'] : b.to_matrix b' ⬝ b'.to_matrix b = 1 := by rw [basis.to_matrix_mul_to_matrix, basis.to_matrix_self] @[simp] lemma basis.to_matrix_reindex (b : basis ΞΉ R M) (v : ΞΉ' β†’ M) (e : ΞΉ ≃ ΞΉ') : (b.reindex e).to_matrix v = (b.to_matrix v).minor e.symm id := by { ext, simp only [basis.to_matrix_apply, basis.reindex_repr, matrix.minor_apply, id.def] } @[simp] lemma basis.to_matrix_map (b : basis ΞΉ R M) (f : M ≃ₗ[R] N) (v : ΞΉ β†’ N) : (b.map f).to_matrix v = b.to_matrix (f.symm ∘ v) := by { ext, simp only [basis.to_matrix_apply, basis.map, linear_equiv.trans_apply] } end mul_linear_map_to_matrix end basis_to_matrix
574087569611a8d412c2bc82966150e4ce1ad330
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/t5.lean
f53b330265a36cb0a3a52054952288334d7888fc
[ "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
245
lean
constant N : Type.{1} constant f : N β†’ N constant a : N definition g (a : N) : N := f a check g namespace foo definition h : N := f a check h private definition q : N := f a check q end foo check foo.h check q -- Error q is now hidden
dba267ee76691bb8d1eec249eb2f0d8a84eaa1ff
94e33a31faa76775069b071adea97e86e218a8ee
/src/group_theory/group_action/quotient.lean
b0e68712cc1c3f2df44473f82f50fb56e10c4c29
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
13,943
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Thomas Browning -/ import group_theory.group_action.basic import group_theory.quotient_group /-! # Properties of group actions involving quotient groups This file proves properties of group actions which use the quotient group construction, notably * the orbit-stabilizer theorem `card_orbit_mul_card_stabilizer_eq_card_group` * the class formula `card_eq_sum_card_group_div_card_stabilizer'` * Burnside's lemma `sum_card_fixed_by_eq_card_orbits_mul_card_group` -/ universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} open function open_locale big_operators namespace mul_action variables [group Ξ±] section quotient_action open subgroup mul_opposite quotient_group variables (Ξ²) [monoid Ξ²] [mul_action Ξ² Ξ±] (H : subgroup Ξ±) /-- A typeclass for when a `mul_action Ξ² Ξ±` descends to the quotient `Ξ± β§Έ H`. -/ class quotient_action : Prop := (inv_mul_mem : βˆ€ (b : Ξ²) {a a' : Ξ±}, a⁻¹ * a' ∈ H β†’ (b β€’ a)⁻¹ * (b β€’ a') ∈ H) /-- A typeclass for when an `add_action Ξ² Ξ±` descends to the quotient `Ξ± β§Έ H`. -/ class _root_.add_action.quotient_action {Ξ± : Type*} (Ξ² : Type*) [add_group Ξ±] [add_monoid Ξ²] [add_action Ξ² Ξ±] (H : add_subgroup Ξ±) : Prop := (inv_mul_mem : βˆ€ (b : Ξ²) {a a' : Ξ±}, -a + a' ∈ H β†’ -(b +α΅₯ a) + (b +α΅₯ a') ∈ H) attribute [to_additive add_action.quotient_action] mul_action.quotient_action @[to_additive] instance left_quotient_action : quotient_action Ξ± H := ⟨λ _ _ _ _, by rwa [smul_eq_mul, smul_eq_mul, mul_inv_rev, mul_assoc, inv_mul_cancel_left]⟩ @[to_additive] instance right_quotient_action : quotient_action H.normalizer.opposite H := ⟨λ b c _ _, by rwa [smul_def, smul_def, smul_eq_mul_unop, smul_eq_mul_unop, mul_inv_rev, ←mul_assoc, mem_normalizer_iff'.mp b.prop, mul_assoc, mul_inv_cancel_left]⟩ @[to_additive] instance right_quotient_action' [hH : H.normal] : quotient_action αᡐᡒᡖ H := ⟨λ _ _ _ _, by rwa [smul_eq_mul_unop, smul_eq_mul_unop, mul_inv_rev, mul_assoc, hH.mem_comm_iff, mul_assoc, mul_inv_cancel_right]⟩ @[to_additive] instance quotient [quotient_action Ξ² H] : mul_action Ξ² (Ξ± β§Έ H) := { smul := Ξ» b, quotient.map' ((β€’) b) (Ξ» a a' h, left_rel_apply.mpr $ quotient_action.inv_mul_mem b $ left_rel_apply.mp h), one_smul := Ξ» q, quotient.induction_on' q (Ξ» a, congr_arg quotient.mk' (one_smul Ξ² a)), mul_smul := Ξ» b b' q, quotient.induction_on' q (Ξ» a, congr_arg quotient.mk' (mul_smul b b' a)) } variables {Ξ²} @[simp, to_additive] lemma quotient.smul_mk [quotient_action Ξ² H] (b : Ξ²) (a : Ξ±) : (b β€’ quotient_group.mk a : Ξ± β§Έ H) = quotient_group.mk (b β€’ a) := rfl @[simp, to_additive] lemma quotient.smul_coe [quotient_action Ξ² H] (b : Ξ²) (a : Ξ±) : (b β€’ a : Ξ± β§Έ H) = ↑(b β€’ a) := rfl @[simp, to_additive] lemma quotient.mk_smul_out' [quotient_action Ξ² H] (b : Ξ²) (q : Ξ± β§Έ H) : quotient_group.mk (b β€’ q.out') = b β€’ q := by rw [←quotient.smul_mk, quotient_group.out_eq'] @[simp, to_additive] lemma quotient.coe_smul_out' [quotient_action Ξ² H] (b : Ξ²) (q : Ξ± β§Έ H) : ↑(b β€’ q.out') = b β€’ q := quotient.mk_smul_out' H b q end quotient_action open quotient_group /-- The canonical map to the left cosets. -/ def _root_.mul_action_hom.to_quotient (H : subgroup Ξ±) : Ξ± β†’[Ξ±] Ξ± β§Έ H := ⟨coe, quotient.smul_coe H⟩ @[simp] lemma _root_.mul_action_hom.to_quotient_apply (H : subgroup Ξ±) (g : Ξ±) : mul_action_hom.to_quotient H g = g := rfl @[to_additive] instance mul_left_cosets_comp_subtype_val (H I : subgroup Ξ±) : mul_action I (Ξ± β§Έ H) := mul_action.comp_hom (Ξ± β§Έ H) (subgroup.subtype I) variables (Ξ±) {Ξ²} [mul_action Ξ± Ξ²] (x : Ξ²) /-- The canonical map from the quotient of the stabilizer to the set. -/ @[to_additive "The canonical map from the quotient of the stabilizer to the set. "] def of_quotient_stabilizer (g : Ξ± β§Έ (mul_action.stabilizer Ξ± x)) : Ξ² := quotient.lift_on' g (β€’x) $ Ξ» g1 g2 H, calc g1 β€’ x = g1 β€’ (g1⁻¹ * g2) β€’ x : congr_arg _ ((left_rel_apply.mp H).symm) ... = g2 β€’ x : by rw [smul_smul, mul_inv_cancel_left] @[simp, to_additive] theorem of_quotient_stabilizer_mk (g : Ξ±) : of_quotient_stabilizer Ξ± x (quotient_group.mk g) = g β€’ x := rfl @[to_additive] theorem of_quotient_stabilizer_mem_orbit (g) : of_quotient_stabilizer Ξ± x g ∈ orbit Ξ± x := quotient.induction_on' g $ Ξ» g, ⟨g, rfl⟩ @[to_additive] theorem of_quotient_stabilizer_smul (g : Ξ±) (g' : Ξ± β§Έ (mul_action.stabilizer Ξ± x)) : of_quotient_stabilizer Ξ± x (g β€’ g') = g β€’ of_quotient_stabilizer Ξ± x g' := quotient.induction_on' g' $ Ξ» _, mul_smul _ _ _ @[to_additive] theorem injective_of_quotient_stabilizer : function.injective (of_quotient_stabilizer Ξ± x) := Ξ» y₁ yβ‚‚, quotient.induction_onβ‚‚' y₁ yβ‚‚ $ Ξ» g₁ gβ‚‚ (H : g₁ β€’ x = gβ‚‚ β€’ x), quotient.sound' $ by { rw [left_rel_apply], show (g₁⁻¹ * gβ‚‚) β€’ x = x, rw [mul_smul, ← H, inv_smul_smul] } /-- Orbit-stabilizer theorem. -/ @[to_additive "Orbit-stabilizer theorem."] noncomputable def orbit_equiv_quotient_stabilizer (b : Ξ²) : orbit Ξ± b ≃ Ξ± β§Έ (stabilizer Ξ± b) := equiv.symm $ equiv.of_bijective (Ξ» g, ⟨of_quotient_stabilizer Ξ± b g, of_quotient_stabilizer_mem_orbit Ξ± b g⟩) ⟨λ x y hxy, injective_of_quotient_stabilizer Ξ± b (by convert congr_arg subtype.val hxy), Ξ» ⟨b, ⟨g, hgb⟩⟩, ⟨g, subtype.eq hgb⟩⟩ /-- Orbit-stabilizer theorem. -/ @[to_additive "Orbit-stabilizer theorem."] noncomputable def orbit_prod_stabilizer_equiv_group (b : Ξ²) : orbit Ξ± b Γ— stabilizer Ξ± b ≃ Ξ± := (equiv.prod_congr (orbit_equiv_quotient_stabilizer Ξ± _) (equiv.refl _)).trans subgroup.group_equiv_quotient_times_subgroup.symm /-- Orbit-stabilizer theorem. -/ @[to_additive "Orbit-stabilizer theorem."] lemma card_orbit_mul_card_stabilizer_eq_card_group (b : Ξ²) [fintype Ξ±] [fintype $ orbit Ξ± b] [fintype $ stabilizer Ξ± b] : fintype.card (orbit Ξ± b) * fintype.card (stabilizer Ξ± b) = fintype.card Ξ± := by rw [← fintype.card_prod, fintype.card_congr (orbit_prod_stabilizer_equiv_group Ξ± b)] @[simp, to_additive] theorem orbit_equiv_quotient_stabilizer_symm_apply (b : Ξ²) (a : Ξ±) : ((orbit_equiv_quotient_stabilizer Ξ± b).symm a : Ξ²) = a β€’ b := rfl @[simp, to_additive] lemma stabilizer_quotient {G} [group G] (H : subgroup G) : mul_action.stabilizer G ((1 : G) : G β§Έ H) = H := by { ext, simp [quotient_group.eq] } variable (Ξ²) local notation `Ξ©` := (quotient $ orbit_rel Ξ± Ξ²) /-- **Class formula** : given `G` a group acting on `X` and `Ο†` a function mapping each orbit of `X` under this action (that is, each element of the quotient of `X` by the relation `orbit_rel G X`) to an element in this orbit, this gives a (noncomputable) bijection between `X` and the disjoint union of `G/Stab(Ο†(Ο‰))` over all orbits `Ο‰`. In most cases you'll want `Ο†` to be `quotient.out'`, so we provide `mul_action.self_equiv_sigma_orbits_quotient_stabilizer` as a special case. -/ @[to_additive "**Class formula** : given `G` an additive group acting on `X` and `Ο†` a function mapping each orbit of `X` under this action (that is, each element of the quotient of `X` by the relation `orbit_rel G X`) to an element in this orbit, this gives a (noncomputable) bijection between `X` and the disjoint union of `G/Stab(Ο†(Ο‰))` over all orbits `Ο‰`. In most cases you'll want `Ο†` to be `quotient.out'`, so we provide `add_action.self_equiv_sigma_orbits_quotient_stabilizer` as a special case. "] noncomputable def self_equiv_sigma_orbits_quotient_stabilizer' {Ο† : Ξ© β†’ Ξ²} (hΟ† : left_inverse quotient.mk' Ο†) : Ξ² ≃ Ξ£ (Ο‰ : Ξ©), Ξ± β§Έ (stabilizer Ξ± (Ο† Ο‰)) := calc Ξ² ≃ Ξ£ (Ο‰ : Ξ©), orbit Ξ± (Ο† Ο‰) : self_equiv_sigma_orbits' Ξ± Ξ² hΟ† ... ≃ Ξ£ (Ο‰ : Ξ©), Ξ± β§Έ (stabilizer Ξ± (Ο† Ο‰)) : equiv.sigma_congr_right (Ξ» Ο‰, orbit_equiv_quotient_stabilizer Ξ± (Ο† Ο‰)) /-- **Class formula** for a finite group acting on a finite type. See `mul_action.card_eq_sum_card_group_div_card_stabilizer` for a specialized version using `quotient.out'`. -/ @[to_additive "**Class formula** for a finite group acting on a finite type. See `add_action.card_eq_sum_card_add_group_div_card_stabilizer` for a specialized version using `quotient.out'`."] lemma card_eq_sum_card_group_div_card_stabilizer' [fintype Ξ±] [fintype Ξ²] [fintype Ξ©] [Ξ  (b : Ξ²), fintype $ stabilizer Ξ± b] {Ο† : Ξ© β†’ Ξ²} (hΟ† : left_inverse quotient.mk' Ο†) : fintype.card Ξ² = βˆ‘ (Ο‰ : Ξ©), fintype.card Ξ± / fintype.card (stabilizer Ξ± (Ο† Ο‰)) := begin classical, have : βˆ€ Ο‰ : Ξ©, fintype.card Ξ± / fintype.card β†₯(stabilizer Ξ± (Ο† Ο‰)) = fintype.card (Ξ± β§Έ stabilizer Ξ± (Ο† Ο‰)), { intro Ο‰, rw [fintype.card_congr (@subgroup.group_equiv_quotient_times_subgroup Ξ± _ (stabilizer Ξ± $ Ο† Ο‰)), fintype.card_prod, nat.mul_div_cancel], exact fintype.card_pos_iff.mpr (by apply_instance) }, simp_rw [this, ← fintype.card_sigma, fintype.card_congr (self_equiv_sigma_orbits_quotient_stabilizer' Ξ± Ξ² hΟ†)], end /-- **Class formula**. This is a special case of `mul_action.self_equiv_sigma_orbits_quotient_stabilizer'` with `Ο† = quotient.out'`. -/ @[to_additive "**Class formula**. This is a special case of `add_action.self_equiv_sigma_orbits_quotient_stabilizer'` with `Ο† = quotient.out'`. "] noncomputable def self_equiv_sigma_orbits_quotient_stabilizer : Ξ² ≃ Ξ£ (Ο‰ : Ξ©), Ξ± β§Έ (stabilizer Ξ± Ο‰.out') := self_equiv_sigma_orbits_quotient_stabilizer' Ξ± Ξ² quotient.out_eq' /-- **Class formula** for a finite group acting on a finite type. -/ @[to_additive "**Class formula** for a finite group acting on a finite type."] lemma card_eq_sum_card_group_div_card_stabilizer [fintype Ξ±] [fintype Ξ²] [fintype Ξ©] [Ξ  (b : Ξ²), fintype $ stabilizer Ξ± b] : fintype.card Ξ² = βˆ‘ (Ο‰ : Ξ©), fintype.card Ξ± / fintype.card (stabilizer Ξ± Ο‰.out') := card_eq_sum_card_group_div_card_stabilizer' Ξ± Ξ² quotient.out_eq' /-- **Burnside's lemma** : a (noncomputable) bijection between the disjoint union of all `{x ∈ X | g β€’ x = x}` for `g ∈ G` and the product `G Γ— X/G`, where `G` is a group acting on `X` and `X/G`denotes the quotient of `X` by the relation `orbit_rel G X`. -/ @[to_additive "**Burnside's lemma** : a (noncomputable) bijection between the disjoint union of all `{x ∈ X | g β€’ x = x}` for `g ∈ G` and the product `G Γ— X/G`, where `G` is an additive group acting on `X` and `X/G`denotes the quotient of `X` by the relation `orbit_rel G X`. "] noncomputable def sigma_fixed_by_equiv_orbits_prod_group : (Ξ£ (a : Ξ±), (fixed_by Ξ± Ξ² a)) ≃ Ξ© Γ— Ξ± := calc (Ξ£ (a : Ξ±), fixed_by Ξ± Ξ² a) ≃ {ab : Ξ± Γ— Ξ² // ab.1 β€’ ab.2 = ab.2} : (equiv.subtype_prod_equiv_sigma_subtype _).symm ... ≃ {ba : Ξ² Γ— Ξ± // ba.2 β€’ ba.1 = ba.1} : (equiv.prod_comm Ξ± Ξ²).subtype_equiv (Ξ» ab, iff.rfl) ... ≃ Ξ£ (b : Ξ²), stabilizer Ξ± b : equiv.subtype_prod_equiv_sigma_subtype (Ξ» (b : Ξ²) a, a ∈ stabilizer Ξ± b) ... ≃ Ξ£ (Ο‰b : (Ξ£ (Ο‰ : Ξ©), orbit Ξ± Ο‰.out')), stabilizer Ξ± (Ο‰b.2 : Ξ²) : (self_equiv_sigma_orbits Ξ± Ξ²).sigma_congr_left' ... ≃ Ξ£ (Ο‰ : Ξ©), (Ξ£ (b : orbit Ξ± Ο‰.out'), stabilizer Ξ± (b : Ξ²)) : equiv.sigma_assoc (Ξ» (Ο‰ : Ξ©) (b : orbit Ξ± Ο‰.out'), stabilizer Ξ± (b : Ξ²)) ... ≃ Ξ£ (Ο‰ : Ξ©), (Ξ£ (b : orbit Ξ± Ο‰.out'), stabilizer Ξ± Ο‰.out') : equiv.sigma_congr_right (Ξ» Ο‰, equiv.sigma_congr_right $ Ξ» ⟨b, hb⟩, (stabilizer_equiv_stabilizer_of_orbit_rel hb).to_equiv) ... ≃ Ξ£ (Ο‰ : Ξ©), orbit Ξ± Ο‰.out' Γ— stabilizer Ξ± Ο‰.out' : equiv.sigma_congr_right (Ξ» Ο‰, equiv.sigma_equiv_prod _ _) ... ≃ Ξ£ (Ο‰ : Ξ©), Ξ± : equiv.sigma_congr_right (Ξ» Ο‰, orbit_prod_stabilizer_equiv_group Ξ± Ο‰.out') ... ≃ Ξ© Γ— Ξ± : equiv.sigma_equiv_prod Ξ© Ξ± /-- **Burnside's lemma** : given a finite group `G` acting on a set `X`, the average number of elements fixed by each `g ∈ G` is the number of orbits. -/ @[to_additive "**Burnside's lemma** : given a finite additive group `G` acting on a set `X`, the average number of elements fixed by each `g ∈ G` is the number of orbits. "] lemma sum_card_fixed_by_eq_card_orbits_mul_card_group [fintype Ξ±] [Ξ  a, fintype $ fixed_by Ξ± Ξ² a] [fintype Ξ©] : βˆ‘ (a : Ξ±), fintype.card (fixed_by Ξ± Ξ² a) = fintype.card Ξ© * fintype.card Ξ± := by rw [← fintype.card_prod, ← fintype.card_sigma, fintype.card_congr (sigma_fixed_by_equiv_orbits_prod_group Ξ± Ξ²)] @[to_additive] instance is_pretransitive_quotient (G) [group G] (H : subgroup G) : is_pretransitive G (G β§Έ H) := { exists_smul_eq := begin rintros ⟨x⟩ ⟨y⟩, refine ⟨y * x⁻¹, quotient_group.eq.mpr _⟩, simp only [smul_eq_mul, H.one_mem, mul_left_inv, inv_mul_cancel_right], end } end mul_action namespace subgroup variables {G : Type*} [group G] (H : subgroup G) lemma normal_core_eq_ker : H.normal_core = (mul_action.to_perm_hom G (G β§Έ H)).ker := begin refine le_antisymm (Ξ» g hg, equiv.perm.ext (Ξ» q, quotient_group.induction_on q (Ξ» g', (mul_action.quotient.smul_mk H g g').trans (quotient_group.eq.mpr _)))) (subgroup.normal_le_normal_core.mpr (Ξ» g hg, _)), { rw [smul_eq_mul, mul_inv_rev, ←inv_inv g', inv_inv], exact H.normal_core.inv_mem hg g'⁻¹ }, { rw [←H.inv_mem_iff, ←mul_one g⁻¹, ←quotient_group.eq, ←mul_one g], exact (mul_action.quotient.smul_mk H g 1).symm.trans (equiv.perm.ext_iff.mp hg (1 : G)) }, end noncomputable instance fintype_quotient_normal_core [fintype (G β§Έ H)] : fintype (G β§Έ H.normal_core) := begin rw H.normal_core_eq_ker, classical, exact fintype.of_equiv _ (quotient_group.quotient_ker_equiv_range _).symm.to_equiv, end end subgroup
e80dcaed7ca2a8bd363a2f0f6a129c40c65e3554
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta.lean
0b70d2aca72ed07f68c5ee092673bd526a406e0c
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,260
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Basic import Lean.Meta.LevelDefEq import Lean.Meta.WHNF import Lean.Meta.InferType import Lean.Meta.FunInfo import Lean.Meta.ExprDefEq import Lean.Meta.DecLevel import Lean.Meta.DiscrTree import Lean.Meta.Reduce import Lean.Meta.Instances import Lean.Meta.AbstractMVars import Lean.Meta.SynthInstance import Lean.Meta.AppBuilder import Lean.Meta.Tactic import Lean.Meta.KAbstract import Lean.Meta.RecursorInfo import Lean.Meta.GeneralizeTelescope import Lean.Meta.Match import Lean.Meta.ReduceEval import Lean.Meta.Closure import Lean.Meta.AbstractNestedProofs import Lean.Meta.ForEachExpr import Lean.Meta.Transform import Lean.Meta.PPGoal import Lean.Meta.UnificationHint import Lean.Meta.Inductive import Lean.Meta.SizeOf import Lean.Meta.IndPredBelow import Lean.Meta.Coe import Lean.Meta.CollectFVars import Lean.Meta.GeneralizeVars import Lean.Meta.Injective import Lean.Meta.Structure import Lean.Meta.Constructions import Lean.Meta.CongrTheorems import Lean.Meta.Eqns import Lean.Meta.CasesOn import Lean.Meta.ExprLens import Lean.Meta.ExprTraverse import Lean.Meta.Eval
6a1f16cf78f8322c52ad7c074259890d949456b7
0851884047bb567d19e188e8f1ad959c5ae9c5ce
/src/M3P14/sheet1.lean
b89409b9109f877e8e6fc008bfb0573c6b649687
[ "Apache-2.0" ]
permissive
yz5216/xena-UROP-2018
00d3f71a831324966d40d302544ed2cbbec3fd0f
5e9da9dc64dc51897677f8b73ab9f94061a8d977
refs/heads/master
1,584,922,436,989
1,531,487,250,000
1,531,487,250,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,318
lean
import data.nat.gcd import data.nat.modeq import data.nat.prime import tactic.norm_num import data.nat.gcd open classical namespace nat -- TODO : change β„• to β„€, but before that need to extend gcd to integers. -- Show that for a, b, d integers, we have (da, db) = d(a,b). theorem q1a (a b d : β„•) : gcd (d*a) (d*b) = d * (gcd a b) := gcd_mul_left d a b -- Let a, b, n integers, and suppose that n|ab. Show that n/(a,b) divides b. theorem q1b (a b n : β„•) (ha : a > 0) (hn : n > 0) : n ∣ (a*b) β†’ (n / gcd a n) ∣ b := Ξ» h, have n / gcd n a ∣ b * (a / gcd n a) := dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left a hn) begin rw ← nat.mul_div_assoc _ (gcd_dvd_right n a), rw nat.div_mul_cancel (gcd_dvd_left n a), rw mul_comm b, rwa nat.div_mul_cancel (dvd_trans (gcd_dvd_left n a) h), end, begin cases exists_eq_mul_left_of_dvd h with c hc, have := coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd (gcd_pos_of_pos_left a hn)) this, rwa gcd_comm, end -- Express 18 as an integer linear combination of 327 and 120. theorem q2a : βˆƒ x y : β„€, 18 = 327*x + 120*y := ⟨-66, 180, by norm_num⟩ -- Find, with proof, all solutions to the linear diophantine equation 100x + 68y = 14. theorem q2b : βˆ€ x y : β„€, 100*x + 68*y = 14 := sorry -- Find a multiplicative inverse of 31 modulo 132. --theorem q2c : theorem q2c : βˆƒ x : β„€, 31*x % 132 = 1 := ⟨115, by norm_num⟩ -- Find an integer congruent to 3 mod 9 and congruent to 1 mod 49. theorem q2d : βˆƒ x : β„€, x % 9 = 3 β†’ x % 49 = 1 := ⟨-195, by norm_num⟩ theorem extended_chinese_remainder {p q r : β„•} (co : coprime p q ∧ coprime q r ∧ coprime r p) (a b c: β„•) : {k // k ≑ a [MOD p] ∧ k ≑ b [MOD q] ∧ k ≑ c[MOD r]} :=sorry -- Find, with proof, the smallest nonnegative integer n such that n = 1 (mod 3), n = 4 (mod 5), and n = 3 (mod 7). theorem q2e : βˆƒ n : β„•, βˆ€ nβ‚‚ : β„•, n % 3 = 1 ∧ n % 5 = 4 ∧ n % 7 = 3 β†’ nβ‚‚ % 3 = 1 ∧ nβ‚‚ % 5 = 4 ∧ nβ‚‚ % 7 = 3 β†’ n ≀ nβ‚‚ := begin --@extended_chinese_remainder 3 5 7 dec_trivial 1 4 3 let n := 94, let p := Ξ» n, n % 3 = 1 ∧ n % 5 = 4 ∧ n % 7 = 3, have h : βˆƒ n : β„•, p n, from ⟨94, dec_trivial⟩, apply exists.intro (nat.find h), assume nβ‚‚ hn hnβ‚‚, exact nat.find_min' h hnβ‚‚, end -- Let m and n be integers. Show that the greatest common divisor of m and n is the unique positive integer d such that: -- - d divides both m and n, and -- - if x divides both m and n, then x divides d. theorem q3 : βˆ€ m n : β„•, βˆƒ! d : β„•, βˆ€ x : β„€, gcd m n = d β†’ d ∣ m β†’ d ∣ n β†’ x ∣ m β†’ x ∣ n β†’ x ∣ d := sorry -- Let a and b be nonzero integers. Show that there is a unique positive integer m with the following two properties: -- - a and b divide m, and -- - if n is any number divisible by both a and b, then m|n. -- The number m is called the least common multiple of a and b. theorem gcd_dvd_trans {a b c : β„•} : a ∣ b β†’ b ∣ c β†’ a ∣ c := begin intro Hab, intro Hbc, cases Hab with e He, cases Hbc with f Hf, existsi e * f, rw Hf, rw He, exact mul_assoc _ _ _, end -- TODO: to find in mathlib theorem coprime_dvd_of_dvd_mul {a b c : β„•} (h1 : a ∣ c) (h2 : b ∣ c) (h3 : coprime a b) : a*b ∣ c := sorry theorem q4a : βˆ€ a b : β„•, βˆƒ! m : β„•, βˆ€ n : β„•, a β‰  0 ∧ b β‰  0 ∧ a ∣ m ∧ b ∣ m ∧ a ∣ n ∧ b ∣ n β†’ m ∣ n := begin --existence --define m = ab/(a,b) , a=da', b=db' β†’ d ∣ n, a'∣ n, b'∣ n β†’ m= da'b' ∣n as d, a', b' is coprime intro a, intro b, let m := a*b/(gcd a b), have m_def : m = a*b/(gcd a b), by simp, apply exists_unique.intro m, { assume n, assume h, have a_dvd_n : a ∣ n, from h.right.right.right.right.left, have b_dvd_n : b ∣ n, from h.right.right.right.right.right, let a' := a/(gcd a b), have ap_eq : a/(gcd a b) = a', by simp, have gcd_dvd_a : (gcd a b) ∣ a, from gcd_dvd_left _ _, have gcd_dvd_b : (gcd a b) ∣ b, from gcd_dvd_right _ _, let b':= b/(gcd a b), have bp_eq : b' = b/(gcd a b), by simp, let d := gcd a b, have d_eq : d = gcd a b, by simp, cases em (coprime d a' ∨ coprime d b') with h1 h2, { have eq_a_bis : a = gcd a b * a', from nat.eq_mul_of_div_eq_right gcd_dvd_a ap_eq, have a'_dvd_a : a' ∣ a, { rw eq_a_bis, exact dvd_mul_left _ _, }, have eq_b_bis : b = gcd a b * b', from nat.eq_mul_of_div_eq_right gcd_dvd_b bp_eq, have b'_dvd_b : b' ∣ b, { rw eq_b_bis, exact dvd_mul_left _ _, }, have a_dvd_n : a ∣ n, from h.right.right.right.right.left, have a'_dvd_n : a' ∣ n, from gcd_dvd_trans a'_dvd_a a_dvd_n, have b'_dvd_n : b' ∣ n, from gcd_dvd_trans b'_dvd_b b_dvd_n, have eq2 : (b / (gcd a b)) * gcd a b = b, from nat.div_mul_cancel gcd_dvd_b, --have eq2_bis : gcd a b * a / (gcd a b) = a, from nat.mul_comm (nat.div_mul_cancel gcd_dvd_a), have eq_b : d*b' = b, { calc d*b' = b : by rw [nat.mul_comm,d_eq, bp_eq, eq2] }, have eq_a : d*a' = a, { calc d*a' = gcd a b * a' : by rw d_eq ... = gcd a b * (a / (gcd a b)) : by rw ap_eq ... = a / gcd a b * gcd a b : by rw nat.mul_comm ... = a : by rw nat.div_mul_cancel gcd_dvd_a }, cases h1 with cda' cdb', { have cdba : coprime (d*b') a', { have cbpap : coprime b' a', { have b_gr_0 : b > 0, { rw pos_iff_ne_zero, exact h.right.left, }, have gcd_gr_0 : gcd b a > 0, from nat.gcd_pos_of_pos_left a b_gr_0, have cop_fractions : coprime (b /gcd b a) (a / gcd b a), from nat.coprime_div_gcd_div_gcd gcd_gr_0, have cop_fractions_2 : coprime (b /gcd a b) (a / gcd a b), from eq.subst (gcd_comm b a) cop_fractions, have cop_fractions_3 : coprime b' (a / gcd a b), from eq.subst bp_eq cop_fractions_2, exact eq.subst ap_eq cop_fractions_3, }, exact coprime.mul cda' cbpap, }, have cba : coprime b a', from eq.subst eq_b cdba, have ba'_dvd_n : b*a' ∣ n, from coprime_dvd_of_dvd_mul b_dvd_n a'_dvd_n cba, have eq3 : b*a' = m, { calc b*a' = b * (a / gcd a b) : by rw ap_eq ... = (b * a) / gcd a b : by rw nat.mul_div_assoc b gcd_dvd_a ... = a * b / gcd a b : by rw nat.mul_comm ... = m : by rw m_def }, exact eq.subst eq3 ba'_dvd_n, }, { have cdba : coprime (d*a') b', { have capbp : coprime a' b', { have b_gr_0 : b > 0, { rw pos_iff_ne_zero, exact h.right.left, }, have gcd_gr_0 : gcd a b > 0, from nat.gcd_pos_of_pos_right a b_gr_0, have cop_fractions : coprime (a / gcd a b) (b /gcd a b), from nat.coprime_div_gcd_div_gcd gcd_gr_0, have cop_fractions_2 : coprime a' (b / gcd a b), from eq.subst ap_eq cop_fractions, exact eq.subst bp_eq cop_fractions_2, }, exact coprime.mul cdb' capbp, }, have cba : coprime a b', from eq.subst eq_a cdba, have ba'_dvd_n : a*b' ∣ n, from coprime_dvd_of_dvd_mul a_dvd_n b'_dvd_n cba, have eq3 : a*b' = m, { calc a*b' = a * (b / gcd a b) : by rw bp_eq ... = (a * b) / gcd a b : by rw nat.mul_div_assoc a gcd_dvd_b ... = m : by rw m_def }, exact eq.subst eq3 ba'_dvd_n, } }, { sorry, } }, { sorry, } end #print notation ∣ #print has_dvd.dvd variables a b c : β„• variable gcd_dvd_b : (gcd a b) ∣ b variable b_diff_0 : (bβ‰ 0) #check nat.div_mul_cancel gcd_dvd_b #check nat.div_mul_cancel gcd_dvd_b variable a' : β„• variable (adiff0: (a' > 0)) variable (advdb: (a' ∣ a)) variable (gcddivides : (gcd a b ∣ a)) variable (eq: a/(gcd a b) = a') #check nat.div_eq_iff_eq_mul_right adiff0 advdb #check nat.eq_mul_of_div_eq_right advdb eq #check nat.eq_mul_of_div_eq_right gcddivides eq #print coprime #print notation ∣ #check has_dvd.dvd -- Show that the least common multiple of a and b is given by |ab|/(a,b) -- TODO: need to change β„• to β„€ and use abs(a*b) theorem q4b : βˆ€ a b : β„•, lcm a b = a*b/(gcd a b) := begin intros a b, exact gcd_mul_lcm a b, sorry end -- Let m and n be positive integers, and let K be the kernel of the map: -- β„€/mnβ„€ β†’ β„€/mβ„€ x β„€/nβ„€ -- that takes a class mod mn to the corresponding classes modulo m and n. -- Show that K has (m, n) elements. What are they? -- theorem q5 : -- -- Show that the equation ax = b (mod n) has no solutions if b is not divisible by (a, n), and exactly (a, n) solutions in β„€/n otherwise. -- theorem q6 : -- Show that the equation ax = b (mod n) has no solutions if b is not divisible by (a, n), and exactly (a, n) solutions in β„€/n otherwise. -- TODO: how to specify "there are exactly n solutions to an equation"? --theorem q6 : Β¬(gcd a n ∣ b) β†’ Β¬(βˆƒ x, a*x ≑ b [MOD n]) -- -- For n a positive integer, let Οƒ(n) denote the sum Ξ£ d for d∣n and d>0, of the positive divisors of n. -- -- Show that the function n ↦ Οƒ(n) is multiplicative. -- theorem q7 : --finset.range -- Let p be a prime, and a be any integer. Show that a^(pΒ²+p+1) is congruent to a^3 modulo p. lemma nat.pow_mul (a b c : β„•) : a ^ (b * c) = (a ^ b) ^ c := begin induction c with c ih, simp, rw [nat.mul_succ, nat.pow_add, nat.pow_succ, ih], end lemma nat.pow.mul (a b p q: β„•) : a ≑ b [MOD q] β†’ a ^ p ≑ b ^ p [MOD q] := begin intro h, induction p with p ih, simp, rw [nat.pow_succ, nat.pow_succ], apply nat.modeq.modeq_mul ih h, end theorem fermat_little_theorem : βˆ€ a p : β„•, prime p β†’ a ^ p ≑ a [MOD p] := sorry theorem q8 : βˆ€ a p : β„•, prime p β†’ a^(p^2+p+1) ≑ a^3 [MOD p] := begin assume a p hp, rw [nat.pow_add, nat.pow_one, nat.pow_add, nat.pow_succ, nat.pow_one, nat.pow_succ, nat.pow_succ, nat.pow_one], apply nat.modeq.modeq_mul, apply nat.modeq.modeq_mul, rw nat.pow_mul, have middle_step : (a ^ p) ^ p ≑ a ^ p [MOD p], let b := a ^ p, exact fermat_little_theorem b p hp, exact modeq.trans middle_step (fermat_little_theorem a p hp), exact fermat_little_theorem a p hp, exact modeq.refl a, end -- Let n be a squarefree positive integer, and suppose that for all primes p dividing n, we have (p-1)∣(n - 1). -- Show that for all integers a with (a, n) = 1, we have a^n = a (mod n). def square_free_int (a : β„•) := βˆ€ n : β„•, (n*n) ∣ a β†’ n = 1 theorem q9 : βˆ€ n p a, square_free_int n ∧ prime p ∧ p ∣ n β†’ (p-1)∣(n - 1) β†’ gcd a n = 1 β†’ a^n ≑ a [MOD n] := sorry -- Let n be a positive integer. Show that Ξ£ Ξ¦(d) for d∣n and d>0 = n. -- [Hint: First show that the number of integers a with a ≀ 0 < n and (a, n) = n/d is equal to Ξ¦(d).] --theorem q10 : end nat
c5e4aa35eaf391773561fa25f78298d51e07a76e
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/category/Rel_auto.lean
74424618f0a58d41fa3ed00747f7d0ddb7dbfdc1
[]
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
770
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.category.default import Mathlib.PostPort universes u u_1 namespace Mathlib /-! The category of types with binary relations as morphisms. -/ namespace category_theory /-- A type synonym for `Type`, which carries the category instance for which morphisms are binary relations. -/ def Rel := Type u protected instance Rel.inhabited : Inhabited Rel := eq.mpr sorry sort.inhabited /-- The category of types with binary relations as morphisms. -/ protected instance rel : large_category Rel := category.mk end Mathlib
947f00705bb464c053c44ee3e0d326ca098b5e59
63abd62053d479eae5abf4951554e1064a4c45b4
/src/analysis/convex/specific_functions.lean
4b313f5542c3ecf7bf1ae77cef69153595d92741
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
5,097
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, SΓ©bastien GouΓ«zel -/ import analysis.calculus.mean_value import data.nat.parity import analysis.special_functions.pow /-! # Collection of convex functions In this file we prove that the following functions are convex: * `convex_on_exp` : the exponential function is convex on $(-∞, +∞)$; * `convex_on_pow_of_even` : given an even natural number $n$, the function $f(x)=x^n$ is convex on $(-∞, +∞)$; * `convex_on_pow` : for a natural $n$, the function $f(x)=x^n$ is convex on $[0, +∞)$; * `convex_on_fpow` : for an integer $m$, the function $f(x)=x^m$ is convex on $(0, +∞)$. * `convex_on_rpow : βˆ€ p : ℝ, 1 ≀ p β†’ convex_on (Ici 0) (Ξ» x, x ^ p)` -/ open real set open_locale big_operators /-- `exp` is convex on the whole real line -/ lemma convex_on_exp : convex_on univ exp := convex_on_univ_of_deriv2_nonneg differentiable_exp (by simp) (assume x, (iter_deriv_exp 2).symm β–Έ le_of_lt (exp_pos x)) /-- `x^n`, `n : β„•` is convex on the whole real line whenever `n` is even -/ lemma convex_on_pow_of_even {n : β„•} (hn : even n) : convex_on set.univ (Ξ» x : ℝ, x^n) := begin apply convex_on_univ_of_deriv2_nonneg differentiable_pow, { simp only [deriv_pow', differentiable.mul, differentiable_const, differentiable_pow] }, { intro x, rcases nat.even.sub hn (nat.even_bit0 1) with ⟨k, hk⟩, simp only [iter_deriv_pow, finset.prod_range_succ, finset.prod_range_zero, nat.sub_zero, mul_one, hk, pow_mul', pow_two], exact mul_nonneg (nat.cast_nonneg _) (mul_self_nonneg _) } end /-- `x^n`, `n : β„•` is convex on `[0, +∞)` for all `n` -/ lemma convex_on_pow (n : β„•) : convex_on (Ici 0) (Ξ» x : ℝ, x^n) := begin apply convex_on_of_deriv2_nonneg (convex_Ici _) (continuous_pow n).continuous_on; simp only [interior_Ici, differentiable_on_pow, deriv_pow', differentiable_on_const, differentiable_on.mul, iter_deriv_pow], intros x hx, exact mul_nonneg (nat.cast_nonneg _) (pow_nonneg (le_of_lt hx) _) end lemma finset.prod_nonneg_of_card_nonpos_even {Ξ± Ξ² : Type*} [linear_ordered_comm_ring Ξ²] {f : Ξ± β†’ Ξ²} [decidable_pred (Ξ» x, f x ≀ 0)] {s : finset Ξ±} (h0 : even (s.filter (Ξ» x, f x ≀ 0)).card) : 0 ≀ ∏ x in s, f x := calc 0 ≀ (∏ x in s, ((if f x ≀ 0 then (-1:Ξ²) else 1) * f x)) : finset.prod_nonneg (Ξ» x _, by { split_ifs with hx hx, by simp [hx], simp at hx ⊒, exact le_of_lt hx }) ... = _ : by rw [finset.prod_mul_distrib, finset.prod_ite, finset.prod_const_one, mul_one, finset.prod_const, neg_one_pow_eq_pow_mod_two, nat.even_iff.1 h0, pow_zero, one_mul] lemma int_prod_range_nonneg (m : β„€) (n : β„•) (hn : even n) : 0 ≀ ∏ k in finset.range n, (m - k) := begin cases (le_or_lt ↑n m) with hnm hmn, { exact finset.prod_nonneg (Ξ» k hk, sub_nonneg.2 (le_trans (int.coe_nat_le.2 $ le_of_lt $ finset.mem_range.1 hk) hnm)) }, cases le_or_lt 0 m with hm hm, { lift m to β„• using hm, exact le_of_eq (eq.symm $ finset.prod_eq_zero (finset.mem_range.2 $ int.coe_nat_lt.1 hmn) (sub_self _)) }, clear hmn, apply finset.prod_nonneg_of_card_nonpos_even, convert hn, convert finset.card_range n, ext k, simp only [finset.mem_filter, finset.mem_range], refine ⟨and.left, Ξ» hk, ⟨hk, sub_nonpos.2 $ le_trans (le_of_lt hm) _⟩⟩, exact int.coe_nat_nonneg k end /-- `x^m`, `m : β„€` is convex on `(0, +∞)` for all `m` -/ lemma convex_on_fpow (m : β„€) : convex_on (Ioi 0) (Ξ» x : ℝ, x^m) := begin apply convex_on_of_deriv2_nonneg (convex_Ioi 0); try { rw [interior_Ioi] }, { exact (differentiable_on_fpow $ lt_irrefl _).continuous_on }, { exact differentiable_on_fpow (lt_irrefl _) }, { have : eq_on (deriv (Ξ»x:ℝ, x^m)) (Ξ»x, ↑m * x^(m-1)) (Ioi 0), from Ξ» x hx, deriv_fpow (ne_of_gt hx), refine (differentiable_on_congr this).2 _, exact (differentiable_on_fpow (lt_irrefl _)).const_mul _ }, { intros x hx, simp only [iter_deriv_fpow (ne_of_gt hx)], refine mul_nonneg (int.cast_nonneg.2 _) (fpow_nonneg_of_nonneg (le_of_lt hx) _), exact int_prod_range_nonneg _ _ (nat.even_bit0 1) } end lemma convex_on_rpow {p : ℝ} (hp : 1 ≀ p) : convex_on (Ici 0) (Ξ» x : ℝ, x^p) := begin have A : deriv (Ξ» (x : ℝ), x ^ p) = Ξ» x, p * x^(p-1), by { ext x, simp [hp] }, apply convex_on_of_deriv2_nonneg (convex_Ici 0), { apply (continuous_rpow_of_pos (Ξ» _, lt_of_lt_of_le zero_lt_one hp) continuous_id continuous_const).continuous_on }, { apply differentiable.differentiable_on, simp [hp] }, { rw A, assume x hx, replace hx : x β‰  0, by { simp at hx, exact ne_of_gt hx }, simp [differentiable_at.differentiable_within_at, hx] }, { assume x hx, replace hx : 0 < x, by simpa using hx, suffices : 0 ≀ p * ((p - 1) * x ^ (p - 1 - 1)), by simpa [ne_of_gt hx, A], apply mul_nonneg (le_trans zero_le_one hp), exact mul_nonneg (sub_nonneg_of_le hp) (rpow_nonneg_of_nonneg (le_of_lt hx) _) } end
b32a739da3829a33aed809584944543af35f9a2d
df561f413cfe0a88b1056655515399c546ff32a5
/6-advanced-addition-world/l13.lean
d3435f1bf350e41bd6e16282cdffa7eb496c8f80
[]
no_license
nicholaspun/natural-number-game-solutions
31d5158415c6f582694680044c5c6469032c2a06
1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0
refs/heads/main
1,675,123,625,012
1,607,633,548,000
1,607,633,548,000
318,933,860
3
1
null
null
null
null
UTF-8
Lean
false
false
317
lean
lemma ne_succ_self (n : mynat) : n β‰  succ n := begin cases n with k, exact zero_ne_succ 0, intro x, have h := succ_eq_succ_iff k (succ k), rw h at x, symmetry at x, rw succ_eq_add_one at x, have h2 := eq_zero_of_add_right_eq_self x, rw one_eq_succ_zero at h2, symmetry at h2, apply zero_ne_succ 0, exact h2, end
b891a0412577b1a7748cf01cd661b1d5eac2d56d
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/data/stream.lean
59bfae1e49f4b4ca98a0edd21673a2f7333f5cb4
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,348
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ open nat function option universes u v w def stream (Ξ± : Type u) := nat β†’ Ξ± namespace stream variables {Ξ± : Type u} {Ξ² : Type v} {Ξ΄ : Type w} def cons (a : Ξ±) (s : stream Ξ±) : stream Ξ± := Ξ» i, match i with | 0 := a | succ n := s n end notation h :: t := cons h t @[reducible] def head (s : stream Ξ±) : Ξ± := s 0 def tail (s : stream Ξ±) : stream Ξ± := Ξ» i, s (i+1) def drop (n : nat) (s : stream Ξ±) : stream Ξ± := Ξ» i, s (i+n) @[reducible] def nth (n : nat) (s : stream Ξ±) : Ξ± := s n protected lemma eta (s : stream Ξ±) : head s :: tail s = s := funext (Ξ» i, begin cases i, repeat {reflexivity} end) lemma nth_zero_cons (a : Ξ±) (s : stream Ξ±) : nth 0 (a :: s) = a := rfl lemma head_cons (a : Ξ±) (s : stream Ξ±) : head (a :: s) = a := rfl lemma tail_cons (a : Ξ±) (s : stream Ξ±) : tail (a :: s) = s := rfl lemma tail_drop (n : nat) (s : stream Ξ±) : tail (drop n s) = drop n (tail s) := funext (Ξ» i, begin unfold tail drop, simp end) lemma nth_drop (n m : nat) (s : stream Ξ±) : nth n (drop m s) = nth (n+m) s := rfl lemma tail_eq_drop (s : stream Ξ±) : tail s = drop 1 s := rfl lemma drop_drop (n m : nat) (s : stream Ξ±) : drop n (drop m s) = drop (n+m) s := funext (Ξ» i, begin unfold drop, rw add_assoc end) lemma nth_succ (n : nat) (s : stream Ξ±) : nth (succ n) s = nth n (tail s) := rfl lemma drop_succ (n : nat) (s : stream Ξ±) : drop (succ n) s = drop n (tail s) := rfl protected lemma ext {s₁ sβ‚‚ : stream Ξ±} : (βˆ€ n, nth n s₁ = nth n sβ‚‚) β†’ s₁ = sβ‚‚ := assume h, funext h def all (p : Ξ± β†’ Prop) (s : stream Ξ±) := βˆ€ n, p (nth n s) def any (p : Ξ± β†’ Prop) (s : stream Ξ±) := βˆƒ n, p (nth n s) lemma all_def (p : Ξ± β†’ Prop) (s : stream Ξ±) : all p s = βˆ€ n, p (nth n s) := rfl lemma any_def (p : Ξ± β†’ Prop) (s : stream Ξ±) : any p s = βˆƒ n, p (nth n s) := rfl protected def mem (a : Ξ±) (s : stream Ξ±) := any (Ξ» b, a = b) s instance : has_mem Ξ± (stream Ξ±) := ⟨stream.mem⟩ lemma mem_cons (a : Ξ±) (s : stream Ξ±) : a ∈ (a::s) := exists.intro 0 rfl lemma mem_cons_of_mem {a : Ξ±} {s : stream Ξ±} (b : Ξ±) : a ∈ s β†’ a ∈ b :: s := assume ⟨n, h⟩, exists.intro (succ n) (by rw [nth_succ, tail_cons, h]) lemma eq_or_mem_of_mem_cons {a b : Ξ±} {s : stream Ξ±} : a ∈ b::s β†’ a = b ∨ a ∈ s := assume ⟨n, h⟩, begin cases n with n', {left, exact h}, {right, rw [nth_succ, tail_cons] at h, exact ⟨n', h⟩} end lemma mem_of_nth_eq {n : nat} {s : stream Ξ±} {a : Ξ±} : a = nth n s β†’ a ∈ s := assume h, exists.intro n h section map variable (f : Ξ± β†’ Ξ²) def map (s : stream Ξ±) : stream Ξ² := Ξ» n, f (nth n s) lemma drop_map (n : nat) (s : stream Ξ±) : drop n (map f s) = map f (drop n s) := stream.ext (Ξ» i, rfl) lemma nth_map (n : nat) (s : stream Ξ±) : nth n (map f s) = f (nth n s) := rfl lemma tail_map (s : stream Ξ±) : tail (map f s) = map f (tail s) := begin rw tail_eq_drop, reflexivity end lemma head_map (s : stream Ξ±) : head (map f s) = f (head s) := rfl lemma map_eq (s : stream Ξ±) : map f s = f (head s) :: map f (tail s) := by rw [-stream.eta (map f s), tail_map, head_map] lemma map_cons (a : Ξ±) (s : stream Ξ±) : map f (a :: s) = f a :: map f s := begin rw [-stream.eta (map f (a :: s)), map_eq], reflexivity end lemma map_id (s : stream Ξ±) : map id s = s := rfl lemma map_map (g : Ξ² β†’ Ξ΄) (f : Ξ± β†’ Ξ²) (s : stream Ξ±) : map g (map f s) = map (g ∘ f) s := rfl lemma mem_map {a : Ξ±} {s : stream Ξ±} : a ∈ s β†’ f a ∈ map f s := assume ⟨n, h⟩, exists.intro n (by rw [nth_map, h]) end map section zip variable (f : Ξ± β†’ Ξ² β†’ Ξ΄) def zip (s₁ : stream Ξ±) (sβ‚‚ : stream Ξ²) : stream Ξ΄ := Ξ» n, f (nth n s₁) (nth n sβ‚‚) lemma drop_zip (n : nat) (s₁ : stream Ξ±) (sβ‚‚ : stream Ξ²) : drop n (zip f s₁ sβ‚‚) = zip f (drop n s₁) (drop n sβ‚‚) := stream.ext (Ξ» i, rfl) lemma nth_zip (n : nat) (s₁ : stream Ξ±) (sβ‚‚ : stream Ξ²) : nth n (zip f s₁ sβ‚‚) = f (nth n s₁) (nth n sβ‚‚) := rfl lemma head_zip (s₁ : stream Ξ±) (sβ‚‚ : stream Ξ²) : head (zip f s₁ sβ‚‚) = f (head s₁) (head sβ‚‚) := rfl lemma tail_zip (s₁ : stream Ξ±) (sβ‚‚ : stream Ξ²) : tail (zip f s₁ sβ‚‚) = zip f (tail s₁) (tail sβ‚‚) := rfl lemma zip_eq (s₁ : stream Ξ±) (sβ‚‚ : stream Ξ²) : zip f s₁ sβ‚‚ = f (head s₁) (head sβ‚‚) :: zip f (tail s₁) (tail sβ‚‚) := begin rw [-stream.eta (zip f s₁ sβ‚‚)], reflexivity end end zip def const (a : Ξ±) : stream Ξ± := Ξ» n, a lemma mem_const (a : Ξ±) : a ∈ const a := exists.intro 0 rfl lemma const_eq (a : Ξ±) : const a = a :: const a := begin apply stream.ext, intro n, cases n, repeat {reflexivity} end lemma tail_const (a : Ξ±) : tail (const a) = const a := suffices tail (a :: const a) = const a, by rwa -const_eq at this, rfl lemma map_const (f : Ξ± β†’ Ξ²) (a : Ξ±) : map f (const a) = const (f a) := rfl lemma nth_const (n : nat) (a : Ξ±) : nth n (const a) = a := rfl lemma drop_const (n : nat) (a : Ξ±) : drop n (const a) = const a := stream.ext (Ξ» i, rfl) def iterate (f : Ξ± β†’ Ξ±) (a : Ξ±) : stream Ξ± := Ξ» n, nat.rec_on n a (Ξ» n r, f r) lemma head_iterate (f : Ξ± β†’ Ξ±) (a : Ξ±) : head (iterate f a) = a := rfl lemma tail_iterate (f : Ξ± β†’ Ξ±) (a : Ξ±) : tail (iterate f a) = iterate f (f a) := begin apply funext, intro n, induction n with n' ih, {reflexivity}, {unfold tail iterate, unfold tail iterate at ih, rw add_one_eq_succ at ih, dsimp at ih, rw add_one_eq_succ, dsimp, rw ih} end lemma iterate_eq (f : Ξ± β†’ Ξ±) (a : Ξ±) : iterate f a = a :: iterate f (f a) := begin rw [-stream.eta (iterate f a)], rw tail_iterate, reflexivity end lemma nth_zero_iterate (f : Ξ± β†’ Ξ±) (a : Ξ±) : nth 0 (iterate f a) = a := rfl lemma nth_succ_iterate (n : nat) (f : Ξ± β†’ Ξ±) (a : Ξ±) : nth (succ n) (iterate f a) = nth n (iterate f (f a)) := by rw [nth_succ, tail_iterate] section bisim variable (R : stream Ξ± β†’ stream Ξ± β†’ Prop) local infix ~ := R def is_bisimulation := βˆ€ ⦃s₁ s₂⦄, s₁ ~ sβ‚‚ β†’ head s₁ = head sβ‚‚ ∧ tail s₁ ~ tail sβ‚‚ lemma nth_of_bisim (bisim : is_bisimulation R) : βˆ€ {s₁ sβ‚‚} n, s₁ ~ sβ‚‚ β†’ nth n s₁ = nth n sβ‚‚ ∧ drop (n+1) s₁ ~ drop (n+1) sβ‚‚ | s₁ sβ‚‚ 0 h := bisim h | s₁ sβ‚‚ (n+1) h := match bisim h with | ⟨h₁, trel⟩ := nth_of_bisim n trel end -- If two streams are bisimilar, then they are equal lemma eq_of_bisim (bisim : is_bisimulation R) : βˆ€ {s₁ sβ‚‚}, s₁ ~ sβ‚‚ β†’ s₁ = sβ‚‚ := Ξ» s₁ sβ‚‚ r, stream.ext (Ξ» n, and.elim_left (nth_of_bisim R bisim n r)) end bisim lemma bisim_simple (s₁ sβ‚‚ : stream Ξ±) : head s₁ = head sβ‚‚ β†’ s₁ = tail s₁ β†’ sβ‚‚ = tail sβ‚‚ β†’ s₁ = sβ‚‚ := assume hh ht₁ htβ‚‚, eq_of_bisim (Ξ» s₁ sβ‚‚, head s₁ = head sβ‚‚ ∧ s₁ = tail s₁ ∧ sβ‚‚ = tail sβ‚‚) (Ξ» s₁ sβ‚‚ ⟨h₁, hβ‚‚, hβ‚ƒβŸ©, begin constructor, exact h₁, rw [-hβ‚‚, -h₃], repeat {constructor, repeat {assumption}} end) (and.intro hh (and.intro ht₁ htβ‚‚)) lemma coinduction {s₁ sβ‚‚ : stream Ξ±} : head s₁ = head sβ‚‚ β†’ (βˆ€ (Ξ² : Type u) (fr : stream Ξ± β†’ Ξ²), fr s₁ = fr sβ‚‚ β†’ fr (tail s₁) = fr (tail sβ‚‚)) β†’ s₁ = sβ‚‚ := assume hh ht, eq_of_bisim (Ξ» s₁ sβ‚‚, head s₁ = head sβ‚‚ ∧ βˆ€ (Ξ² : Type u) (fr : stream Ξ± β†’ Ξ²), fr s₁ = fr sβ‚‚ β†’ fr (tail s₁) = fr (tail sβ‚‚)) (Ξ» s₁ sβ‚‚ h, have h₁ : head s₁ = head sβ‚‚, from and.elim_left h, have hβ‚‚ : head (tail s₁) = head (tail sβ‚‚), from and.elim_right h Ξ± (@head Ξ±) h₁, have h₃ : βˆ€ (Ξ² : Type u) (fr : stream Ξ± β†’ Ξ²), fr (tail s₁) = fr (tail sβ‚‚) β†’ fr (tail (tail s₁)) = fr (tail (tail sβ‚‚)), from Ξ» Ξ² fr, and.elim_right h Ξ² (Ξ» s, fr (tail s)), and.intro h₁ (and.intro hβ‚‚ h₃)) (and.intro hh ht) lemma iterate_id (a : Ξ±) : iterate id a = const a := coinduction rfl (Ξ» Ξ² fr ch, begin rw [tail_iterate, tail_const], exact ch end) local attribute [reducible] stream lemma map_iterate (f : Ξ± β†’ Ξ±) (a : Ξ±) : iterate f (f a) = map f (iterate f a) := begin apply funext, intro n, induction n with n' ih, {reflexivity}, { unfold map iterate nth, dsimp, unfold map iterate nth at ih, dsimp at ih, rw ih } end section corec def corec (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ Ξ±) : Ξ± β†’ stream Ξ² := Ξ» a, map f (iterate g a) def corec_on (a : Ξ±) (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ Ξ±) : stream Ξ² := corec f g a lemma corec_def (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ Ξ±) (a : Ξ±) : corec f g a = map f (iterate g a) := rfl lemma corec_eq (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ Ξ±) (a : Ξ±) : corec f g a = f a :: corec f g (g a) := begin rw [corec_def, map_eq, head_iterate, tail_iterate], reflexivity end lemma corec_id_id_eq_const (a : Ξ±) : corec id id a = const a := by rw [corec_def, map_id, iterate_id] lemma corec_id_f_eq_iterate (f : Ξ± β†’ Ξ±) (a : Ξ±) : corec id f a = iterate f a := rfl end corec -- corec is also known as unfold def unfolds (g : Ξ± β†’ Ξ²) (f : Ξ± β†’ Ξ±) (a : Ξ±) : stream Ξ² := corec g f a lemma unfolds_eq (g : Ξ± β†’ Ξ²) (f : Ξ± β†’ Ξ±) (a : Ξ±) : unfolds g f a = g a :: unfolds g f (f a) := begin unfold unfolds, rw [corec_eq] end lemma nth_unfolds_head_tail : βˆ€ (n : nat) (s : stream Ξ±), nth n (unfolds head tail s) = nth n s := begin intro n, induction n with n' ih, {intro s, reflexivity}, {intro s, rw [nth_succ, nth_succ, unfolds_eq, tail_cons, ih]} end lemma unfolds_head_eq : βˆ€ (s : stream Ξ±), unfolds head tail s = s := Ξ» s, stream.ext (Ξ» n, nth_unfolds_head_tail n s) def interleave (s₁ sβ‚‚ : stream Ξ±) : stream Ξ± := corec_on (s₁, sβ‚‚) (Ξ» ⟨s₁, sβ‚‚βŸ©, head s₁) (Ξ» ⟨s₁, sβ‚‚βŸ©, (sβ‚‚, tail s₁)) infix `β‹ˆ`:65 := interleave lemma interleave_eq (s₁ sβ‚‚ : stream Ξ±) : s₁ β‹ˆ sβ‚‚ = head s₁ :: head sβ‚‚ :: (tail s₁ β‹ˆ tail sβ‚‚) := begin unfold interleave corec_on, rw corec_eq, dsimp, rw corec_eq, reflexivity end lemma tail_interleave (s₁ sβ‚‚ : stream Ξ±) : tail (s₁ β‹ˆ sβ‚‚) = sβ‚‚ β‹ˆ (tail s₁) := begin unfold interleave corec_on, rw corec_eq, reflexivity end lemma interleave_tail_tail (s₁ sβ‚‚ : stream Ξ±) : tail s₁ β‹ˆ tail sβ‚‚ = tail (tail (s₁ β‹ˆ sβ‚‚)) := begin rw [interleave_eq s₁ sβ‚‚], reflexivity end lemma nth_interleave_left : βˆ€ (n : nat) (s₁ sβ‚‚ : stream Ξ±), nth (2*n) (s₁ β‹ˆ sβ‚‚) = nth n s₁ | 0 s₁ sβ‚‚ := rfl | (succ n) s₁ sβ‚‚ := begin change nth (succ (succ (2*n))) (s₁ β‹ˆ sβ‚‚) = nth (succ n) s₁, rw [nth_succ, nth_succ, interleave_eq, tail_cons, tail_cons, nth_interleave_left], reflexivity end lemma nth_interleave_right : βˆ€ (n : nat) (s₁ sβ‚‚ : stream Ξ±), nth (2*n+1) (s₁ β‹ˆ sβ‚‚) = nth n sβ‚‚ | 0 s₁ sβ‚‚ := rfl | (succ n) s₁ sβ‚‚ := begin change nth (succ (succ (2*n+1))) (s₁ β‹ˆ sβ‚‚) = nth (succ n) sβ‚‚, rw [nth_succ, nth_succ, interleave_eq, tail_cons, tail_cons, nth_interleave_right], reflexivity end lemma mem_interleave_left {a : Ξ±} {s₁ : stream Ξ±} (sβ‚‚ : stream Ξ±) : a ∈ s₁ β†’ a ∈ s₁ β‹ˆ sβ‚‚ := assume ⟨n, h⟩, exists.intro (2*n) (by rw [h, nth_interleave_left]) lemma mem_interleave_right {a : Ξ±} {s₁ : stream Ξ±} (sβ‚‚ : stream Ξ±) : a ∈ sβ‚‚ β†’ a ∈ s₁ β‹ˆ sβ‚‚ := assume ⟨n, h⟩, exists.intro (2*n+1) (by rw [h, nth_interleave_right]) def even (s : stream Ξ±) : stream Ξ± := corec (Ξ» s, head s) (Ξ» s, tail (tail s)) s def odd (s : stream Ξ±) : stream Ξ± := even (tail s) lemma odd_eq (s : stream Ξ±) : odd s = even (tail s) := rfl lemma head_even (s : stream Ξ±) : head (even s) = head s := rfl lemma tail_even (s : stream Ξ±) : tail (even s) = even (tail (tail s)) := begin unfold even, rw corec_eq, reflexivity end lemma even_cons_cons (a₁ aβ‚‚ : Ξ±) (s : stream Ξ±) : even (a₁ :: aβ‚‚ :: s) = a₁ :: even s := begin unfold even, rw corec_eq, reflexivity end lemma even_tail (s : stream Ξ±) : even (tail s) = odd s := rfl lemma even_interleave (s₁ sβ‚‚ : stream Ξ±) : even (s₁ β‹ˆ sβ‚‚) = s₁ := eq_of_bisim (Ξ» s₁' s₁, βˆƒ sβ‚‚, s₁' = even (s₁ β‹ˆ sβ‚‚)) (Ξ» s₁' s₁ ⟨sβ‚‚, hβ‚βŸ©, begin rw h₁, constructor, {reflexivity}, {exact ⟨tail sβ‚‚, by rw [interleave_eq, even_cons_cons, tail_cons]⟩} end) (exists.intro sβ‚‚ rfl) lemma interleave_even_odd (s₁ : stream Ξ±) : even s₁ β‹ˆ odd s₁ = s₁ := eq_of_bisim (Ξ» s' s, s' = even s β‹ˆ odd s) (Ξ» s' s (h : s' = even s β‹ˆ odd s), begin rw h, constructor, {reflexivity}, {dsimp, rw [odd_eq, odd_eq, tail_interleave, tail_even]} end) rfl lemma nth_even : βˆ€ (n : nat) (s : stream Ξ±), nth n (even s) = nth (2*n) s | 0 s := rfl | (succ n) s := begin change nth (succ n) (even s) = nth (succ (succ (2 * n))) s, rw [nth_succ, nth_succ, tail_even, nth_even], reflexivity end lemma nth_odd : βˆ€ (n : nat) (s : stream Ξ±), nth n (odd s) = nth (2*n + 1) s := Ξ» n s, begin rw [odd_eq, nth_even], reflexivity end lemma mem_of_mem_even (a : Ξ±) (s : stream Ξ±) : a ∈ even s β†’ a ∈ s := assume ⟨n, h⟩, exists.intro (2*n) (by rw [h, nth_even]) lemma mem_of_mem_odd (a : Ξ±) (s : stream Ξ±) : a ∈ odd s β†’ a ∈ s := assume ⟨n, h⟩, exists.intro (2*n+1) (by rw [h, nth_odd]) def append_stream : list Ξ± β†’ stream Ξ± β†’ stream Ξ± | [] s := s | (list.cons a l) s := a :: append_stream l s lemma nil_append_stream (s : stream Ξ±) : append_stream [] s = s := rfl lemma cons_append_stream (a : Ξ±) (l : list Ξ±) (s : stream Ξ±) : append_stream (a::l) s = a :: append_stream l s := rfl infix `++β‚›`:65 := append_stream lemma append_append_stream : βˆ€ (l₁ lβ‚‚ : list Ξ±) (s : stream Ξ±), (l₁ ++ lβ‚‚) ++β‚› s = l₁ ++β‚› (lβ‚‚ ++β‚› s) | [] lβ‚‚ s := rfl | (list.cons a l₁) lβ‚‚ s := by rw [list.cons_append, cons_append_stream, cons_append_stream, append_append_stream] lemma map_append_stream (f : Ξ± β†’ Ξ²) : βˆ€ (l : list Ξ±) (s : stream Ξ±), map f (l ++β‚› s) = list.map f l ++β‚› map f s | [] s := rfl | (list.cons a l) s := by rw [cons_append_stream, list.map_cons, map_cons, cons_append_stream, map_append_stream] lemma drop_append_stream : βˆ€ (l : list Ξ±) (s : stream Ξ±), drop l^.length (l ++β‚› s) = s | [] s := by reflexivity | (list.cons a l) s := by rw [list.length_cons, add_one_eq_succ, drop_succ, cons_append_stream, tail_cons, drop_append_stream] lemma append_stream_head_tail (s : stream Ξ±) : [head s] ++β‚› tail s = s := by rw [cons_append_stream, nil_append_stream, stream.eta] lemma mem_append_stream_right : βˆ€ {a : Ξ±} (l : list Ξ±) {s : stream Ξ±}, a ∈ s β†’ a ∈ l ++β‚› s | a [] s h := h | a (list.cons b l) s h := have ih : a ∈ l ++β‚› s, from mem_append_stream_right l h, mem_cons_of_mem _ ih lemma mem_append_stream_left : βˆ€ {a : Ξ±} {l : list Ξ±} (s : stream Ξ±), a ∈ l β†’ a ∈ l ++β‚› s | a [] s h := absurd h (list.not_mem_nil _) | a (list.cons b l) s h := or.elim (list.eq_or_mem_of_mem_cons h) (Ξ» (aeqb : a = b), exists.intro 0 aeqb) (Ξ» (ainl : a ∈ l), mem_cons_of_mem b (mem_append_stream_left s ainl)) def approx : nat β†’ stream Ξ± β†’ list Ξ± | 0 s := [] | (n+1) s := list.cons (head s) (approx n (tail s)) lemma approx_zero (s : stream Ξ±) : approx 0 s = [] := rfl lemma approx_succ (n : nat) (s : stream Ξ±) : approx (succ n) s = head s :: approx n (tail s) := rfl lemma nth_approx : βˆ€ (n : nat) (s : stream Ξ±), list.nth (approx (succ n) s) n = some (nth n s) | 0 s := rfl | (n+1) s := begin rw [approx_succ, add_one_eq_succ, list.nth_succ, nth_approx], reflexivity end lemma append_approx_drop : βˆ€ (n : nat) (s : stream Ξ±), append_stream (approx n s) (drop n s) = s := begin intro n, induction n with n' ih, {intro s, reflexivity}, {intro s, rw [approx_succ, drop_succ, cons_append_stream, ih (tail s), stream.eta]} end -- Take lemma reduces a proof of equality of infinite streams to an -- induction over all their finite approximations. lemma take_lemma (s₁ sβ‚‚ : stream Ξ±) : (βˆ€ (n : nat), approx n s₁ = approx n sβ‚‚) β†’ s₁ = sβ‚‚ := begin intro h, apply stream.ext, intro n, induction n with n ih, {note aux := h 1, unfold approx at aux, injection aux with aux, exact aux}, {assert h₁ : some (nth (succ n) s₁) = some (nth (succ n) sβ‚‚), {rw [-nth_approx, -nth_approx, h (succ (succ n))]}, injection h₁, assumption} end -- auxiliary def for cycle corecursive def private def cycle_f : Ξ± Γ— list Ξ± Γ— Ξ± Γ— list Ξ± β†’ Ξ± | (v, _, _, _) := v -- auxiliary def for cycle corecursive def private def cycle_g : Ξ± Γ— list Ξ± Γ— Ξ± Γ— list Ξ± β†’ Ξ± Γ— list Ξ± Γ— Ξ± Γ— list Ξ± | (v₁, [], vβ‚€, lβ‚€) := (vβ‚€, lβ‚€, vβ‚€, lβ‚€) | (v₁, list.cons vβ‚‚ lβ‚‚, vβ‚€, lβ‚€) := (vβ‚‚, lβ‚‚, vβ‚€, lβ‚€) private lemma cycle_g_cons (a : Ξ±) (a₁ : Ξ±) (l₁ : list Ξ±) (aβ‚€ : Ξ±) (lβ‚€ : list Ξ±) : cycle_g (a, a₁::l₁, aβ‚€, lβ‚€) = (a₁, l₁, aβ‚€, lβ‚€) := rfl def cycle : Ξ  (l : list Ξ±), l β‰  [] β†’ stream Ξ± | [] h := absurd rfl h | (list.cons a l) h := corec cycle_f cycle_g (a, l, a, l) lemma cycle_eq : βˆ€ (l : list Ξ±) (h : l β‰  []), cycle l h = l ++β‚› cycle l h | [] h := absurd rfl h | (list.cons a l) h := have gen : βˆ€ l' a', corec cycle_f cycle_g (a', l', a, l) = (a' :: l') ++β‚› corec cycle_f cycle_g (a, l, a, l), begin intro l', induction l' with a₁ l₁ ih, {intros, rw [corec_eq], reflexivity}, {intros, rw [corec_eq, cycle_g_cons, ih a₁], reflexivity} end, gen l a lemma mem_cycle {a : Ξ±} {l : list Ξ±} : βˆ€ (h : l β‰  []), a ∈ l β†’ a ∈ cycle l h := assume h ainl, begin rw [cycle_eq], exact mem_append_stream_left _ ainl end lemma cycle_singleton (a : Ξ±) (h : [a] β‰  []) : cycle [a] h = const a := coinduction rfl (Ξ» Ξ² fr ch, by rwa [cycle_eq, const_eq]) def tails (s : stream Ξ±) : stream (stream Ξ±) := corec id tail (tail s) lemma tails_eq (s : stream Ξ±) : tails s = tail s :: tails (tail s) := begin unfold tails, rw [corec_eq] end lemma nth_tails : βˆ€ (n : nat) (s : stream Ξ±), nth n (tails s) = drop n (tail s) := begin intro n, induction n with n' ih, {intros, reflexivity}, {intro s, rw [nth_succ, drop_succ, tails_eq, tail_cons, ih]} end lemma tails_eq_iterate (s : stream Ξ±) : tails s = iterate tail (tail s) := rfl def inits_core (l : list Ξ±) (s : stream Ξ±) : stream (list Ξ±) := corec_on (l, s) (Ξ» ⟨a, b⟩, a) (Ξ» p, match p with (l', s') := (l' ++ [head s'], tail s') end) def inits (s : stream Ξ±) : stream (list Ξ±) := inits_core [head s] (tail s) lemma inits_core_eq (l : list Ξ±) (s : stream Ξ±) : inits_core l s = l :: inits_core (l ++ [head s]) (tail s) := begin unfold inits_core corec_on, rw [corec_eq], reflexivity end lemma tail_inits (s : stream Ξ±) : tail (inits s) = inits_core [head s, head (tail s)] (tail (tail s)) := begin unfold inits, rw inits_core_eq, reflexivity end lemma inits_tail (s : stream Ξ±) : inits (tail s) = inits_core [head (tail s)] (tail (tail s)) := rfl lemma cons_nth_inits_core : βˆ€ (a : Ξ±) (n : nat) (l : list Ξ±) (s : stream Ξ±), a :: nth n (inits_core l s) = nth n (inits_core (a::l) s) := begin intros a n, induction n with n' ih, {intros, reflexivity}, {intros l s, rw [nth_succ, inits_core_eq, tail_cons, ih, inits_core_eq (a::l) s], reflexivity } end lemma nth_inits : βˆ€ (n : nat) (s : stream Ξ±), nth n (inits s) = approx (succ n) s := begin intro n, induction n with n' ih, {intros, reflexivity}, {intros, rw [nth_succ, approx_succ, -ih, tail_inits, inits_tail, cons_nth_inits_core]} end lemma inits_eq (s : stream Ξ±) : inits s = [head s] :: map (list.cons (head s)) (inits (tail s)) := begin apply stream.ext, intro n, cases n, {reflexivity}, {rw [nth_inits, nth_succ, tail_cons, nth_map, nth_inits], reflexivity} end lemma zip_inits_tails (s : stream Ξ±) : zip append_stream (inits s) (tails s) = const s := begin apply stream.ext, intro n, rw [nth_zip, nth_inits, nth_tails, nth_const, approx_succ, cons_append_stream, append_approx_drop, stream.eta] end def pure (a : Ξ±) : stream Ξ± := const a def apply (f : stream (Ξ± β†’ Ξ²)) (s : stream Ξ±) : stream Ξ² := Ξ» n, (nth n f) (nth n s) infix `βŠ›`:75 := apply -- input as \o* lemma identity (s : stream Ξ±) : pure id βŠ› s = s := rfl lemma composition (g : stream (Ξ² β†’ Ξ΄)) (f : stream (Ξ± β†’ Ξ²)) (s : stream Ξ±) : pure comp βŠ› g βŠ› f βŠ› s = g βŠ› (f βŠ› s) := rfl lemma homomorphism (f : Ξ± β†’ Ξ²) (a : Ξ±) : pure f βŠ› pure a = pure (f a) := rfl lemma interchange (fs : stream (Ξ± β†’ Ξ²)) (a : Ξ±) : fs βŠ› pure a = pure (Ξ» f : Ξ± β†’ Ξ², f a) βŠ› fs := rfl lemma map_eq_apply (f : Ξ± β†’ Ξ²) (s : stream Ξ±) : map f s = pure f βŠ› s := rfl def nats : stream nat := Ξ» n, n lemma nth_nats (n : nat) : nth n nats = n := rfl lemma nats_eq : nats = 0 :: map succ nats := begin apply stream.ext, intro n, cases n, reflexivity, rw [nth_succ], reflexivity end end stream
d395fa7bac334c39ff1412dc84a8972cf02ca684
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/02_Dependent_Type_Theory.org.3.lean
5a0589f6d4abcffcb6a550a5f6b47deec7f4f90f
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
393
lean
/- page 13 -/ import standard import standard open bool nat prod -- BEGIN check nat -- Type₁ check bool -- Type₁ check nat β†’ bool -- Type₁ check nat Γ— bool -- Type₁ check nat β†’ nat -- ... check nat Γ— nat β†’ nat check nat β†’ nat β†’ nat check nat β†’ (nat β†’ nat) check nat β†’ nat β†’ bool check (nat β†’ nat) β†’ nat -- END
0a9b78f84e56e05de9dc506c24a78e8123bba4cf
367134ba5a65885e863bdc4507601606690974c1
/src/group_theory/solvable.lean
adbb45dd85c660a7ffc4c00f430176ea85b3cfac
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
10,191
lean
/- Copyright (c) 2021 Jordan Brown, Thomas Browning and Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jordan Brown, Thomas Browning and Patrick Lutz -/ import group_theory.abelianization import data.bracket /-! # Solvable Groups In this file we introduce the notion of a solvable group. We define a solvable group as one whose derived series is eventually trivial. This requires defining the commutator of two subgroups and the derived series of a group. ## Main definitions * `general_commutator H₁ Hβ‚‚` : the commutator of the subgroups `H₁` and `Hβ‚‚` * `derived_series G n` : the `n`th term in the derived series of `G`, defined by iterating `general_commutator` starting with the top subgroup * `is_solvable G` : the group `G` is solvable -/ open subgroup variables {G : Type*} [group G] section general_commutator /-- The commutator of two subgroups `H₁` and `Hβ‚‚`. -/ instance general_commutator : has_bracket (subgroup G) (subgroup G) := ⟨λ H₁ Hβ‚‚, closure {x | βˆƒ (p ∈ H₁) (q ∈ Hβ‚‚), p * q * p⁻¹ * q⁻¹ = x}⟩ lemma general_commutator_def (H₁ Hβ‚‚ : subgroup G) : ⁅H₁, H₂⁆ = closure {x | βˆƒ (p ∈ H₁) (q ∈ Hβ‚‚), p * q * p⁻¹ * q⁻¹ = x} := rfl instance general_commutator_normal (H₁ Hβ‚‚ : subgroup G) [h₁ : H₁.normal] [hβ‚‚ : Hβ‚‚.normal] : normal ⁅H₁, H₂⁆ := begin let base : set G := {x | βˆƒ (p ∈ H₁) (q ∈ Hβ‚‚), p * q * p⁻¹ * q⁻¹ = x}, suffices h_base : base = group.conjugates_of_set base, { dsimp only [general_commutator_def, ←base], rw h_base, exact subgroup.normal_closure_normal }, apply set.subset.antisymm group.subset_conjugates_of_set, intros a h, rw group.mem_conjugates_of_set_iff at h, rcases h with ⟨b, ⟨c, hc, e, he, rfl⟩, d, rfl⟩, exact ⟨d * c * d⁻¹, h₁.conj_mem c hc d, d * e * d⁻¹, hβ‚‚.conj_mem e he d, by group⟩, end lemma general_commutator_mono {H₁ Hβ‚‚ K₁ Kβ‚‚ : subgroup G} (h₁ : H₁ ≀ K₁) (hβ‚‚ : Hβ‚‚ ≀ Kβ‚‚) : ⁅H₁, H₂⁆ ≀ ⁅K₁, K₂⁆ := begin apply closure_mono, rintros x ⟨p, hp, q, hq, rfl⟩, exact ⟨p, h₁ hp, q, hβ‚‚ hq, rfl⟩, end lemma general_commutator_def' (H₁ Hβ‚‚ : subgroup G) [H₁.normal] [Hβ‚‚.normal] : ⁅H₁, H₂⁆ = normal_closure {x | βˆƒ (p ∈ H₁) (q ∈ Hβ‚‚), p * q * p⁻¹ * q⁻¹ = x} := by rw [← normal_closure_eq_self ⁅H₁, H₂⁆, general_commutator_def, normal_closure_closure_eq_normal_closure] lemma general_commutator_le (H₁ Hβ‚‚ : subgroup G) (K : subgroup G) : ⁅H₁, H₂⁆ ≀ K ↔ βˆ€ (p ∈ H₁) (q ∈ Hβ‚‚), p * q * p⁻¹ * q⁻¹ ∈ K := begin rw [general_commutator, closure_le], split, { intros h p hp q hq, exact h ⟨p, hp, q, hq, rfl⟩, }, { rintros h x ⟨p, hp, q, hq, rfl⟩, exact h p hp q hq, } end lemma general_commutator_comm (H₁ Hβ‚‚ : subgroup G) : ⁅H₁, H₂⁆ = ⁅Hβ‚‚, H₁⁆ := begin suffices : βˆ€ H₁ Hβ‚‚ : subgroup G, ⁅H₁, H₂⁆ ≀ ⁅Hβ‚‚, H₁⁆, { exact le_antisymm (this _ _) (this _ _) }, intros H₁ Hβ‚‚, rw general_commutator_le, intros p hp q hq, have h : (p * q * p⁻¹ * q⁻¹)⁻¹ ∈ ⁅Hβ‚‚, H₁⁆ := subset_closure ⟨q, hq, p, hp, by group⟩, convert inv_mem ⁅Hβ‚‚, H₁⁆ h, group, end lemma general_commutator_le_right (H₁ Hβ‚‚ : subgroup G) [h : normal Hβ‚‚] : ⁅H₁, H₂⁆ ≀ Hβ‚‚ := begin rw general_commutator_le, intros p hp q hq, exact mul_mem Hβ‚‚ (h.conj_mem q hq p) (inv_mem Hβ‚‚ hq), end lemma general_commutator_le_left (H₁ Hβ‚‚ : subgroup G) [h : normal H₁] : ⁅H₁, H₂⁆ ≀ H₁ := begin rw general_commutator_comm, exact general_commutator_le_right Hβ‚‚ H₁, end @[simp] lemma general_commutator_bot (H : subgroup G) : ⁅H, βŠ₯⁆ = (βŠ₯ : subgroup G) := by { rw eq_bot_iff, exact general_commutator_le_right H βŠ₯ } @[simp] lemma bot_general_commutator (H : subgroup G) : ⁅(βŠ₯ : subgroup G), H⁆ = (βŠ₯ : subgroup G) := by { rw eq_bot_iff, exact general_commutator_le_left βŠ₯ H } lemma general_commutator_le_inf (H₁ Hβ‚‚ : subgroup G) [normal H₁] [normal Hβ‚‚] : ⁅H₁, H₂⁆ ≀ H₁ βŠ“ Hβ‚‚ := by simp only [general_commutator_le_left, general_commutator_le_right, le_inf_iff, and_self] end general_commutator section derived_series variables (G) /-- The derived series of the group `G`, obtained by starting from the subgroup `⊀` and repeatedly taking the commutator of the previous subgroup with itself for `n` times. -/ def derived_series : β„• β†’ subgroup G | 0 := ⊀ | (n + 1) := ⁅(derived_series n), (derived_series n)⁆ @[simp] lemma derived_series_zero : derived_series G 0 = ⊀ := rfl @[simp] lemma derived_series_succ (n : β„•) : derived_series G (n + 1) = ⁅(derived_series G n), (derived_series G n)⁆ := rfl lemma derived_series_normal (n : β„•) : (derived_series G n).normal := begin induction n with n ih, { exact subgroup.top_normal, }, { rw derived_series_succ, exactI general_commutator_normal (derived_series G n) (derived_series G n), } end @[simp] lemma general_commutator_eq_commutator : ⁅(⊀ : subgroup G), (⊀ : subgroup G)⁆ = commutator G := begin rw [commutator, general_commutator_def'], apply le_antisymm; apply normal_closure_mono, { exact Ξ» x ⟨p, _, q, _, h⟩, ⟨p, q, h⟩, }, { exact Ξ» x ⟨p, q, h⟩, ⟨p, mem_top p, q, mem_top q, h⟩, } end lemma commutator_def' : commutator G = subgroup.closure {x : G | βˆƒ p q, p * q * p⁻¹ * q⁻¹ = x} := begin rw [← general_commutator_eq_commutator, general_commutator], apply le_antisymm; apply closure_mono, { exact Ξ» x ⟨p, _, q, _, h⟩, ⟨p, q, h⟩ }, { exact Ξ» x ⟨p, q, h⟩, ⟨p, mem_top p, q, mem_top q, h⟩ } end @[simp] lemma derived_series_one : derived_series G 1 = commutator G := general_commutator_eq_commutator G end derived_series section commutator_map variables {G} {G' : Type*} [group G'] {f : G β†’* G'} lemma map_commutator_eq_commutator_map (H₁ Hβ‚‚ : subgroup G) : ⁅H₁, H₂⁆.map f = ⁅H₁.map f, Hβ‚‚.map f⁆ := begin rw [general_commutator, general_commutator, monoid_hom.map_closure], apply le_antisymm; apply closure_mono, { rintros _ ⟨x, ⟨p, hp, q, hq, rfl⟩, rfl⟩, refine ⟨f p, mem_map.mpr ⟨p, hp, rfl⟩, f q, mem_map.mpr ⟨q, hq, rfl⟩, by simp *⟩, }, { rintros x ⟨_, ⟨p, hp, rfl⟩, _, ⟨q, hq, rfl⟩, rfl⟩, refine ⟨p * q * p⁻¹ * q⁻¹, ⟨p, hp, q, hq, rfl⟩, by simp *⟩, }, end lemma commutator_le_map_commutator {H₁ Hβ‚‚ : subgroup G} {K₁ Kβ‚‚ : subgroup G'} (h₁ : K₁ ≀ H₁.map f) (hβ‚‚ : Kβ‚‚ ≀ Hβ‚‚.map f) : ⁅K₁, K₂⁆ ≀ ⁅H₁, H₂⁆.map f := by { rw map_commutator_eq_commutator_map, exact general_commutator_mono h₁ hβ‚‚ } section derived_series_map variables (f) lemma map_derived_series_le_derived_series (n : β„•) : (derived_series G n).map f ≀ derived_series G' n := begin induction n with n ih, { simp only [derived_series_zero, le_top], }, { simp only [derived_series_succ, map_commutator_eq_commutator_map, general_commutator_mono, *], } end variables {f} lemma derived_series_le_map_derived_series (hf : function.surjective f) (n : β„•) : derived_series G' n ≀ (derived_series G n).map f := begin induction n with n ih, { rwa [derived_series_zero, derived_series_zero, top_le_iff, ← monoid_hom.range_eq_map, ← monoid_hom.range_top_iff_surjective.mpr], }, { simp only [*, derived_series_succ, commutator_le_map_commutator], } end lemma map_derived_series_eq (hf : function.surjective f) (n : β„•) : (derived_series G n).map f = derived_series G' n := le_antisymm (map_derived_series_le_derived_series f n) (derived_series_le_map_derived_series hf n) end derived_series_map end commutator_map section solvable variables (G) /-- A group `G` is solvable if its derived series is eventually trivial. We use this definition because it's the most convenient one to work with. -/ class is_solvable : Prop := (solvable : βˆƒ n : β„•, derived_series G n = βŠ₯) lemma is_solvable_def : is_solvable G ↔ βˆƒ n : β„•, derived_series G n = βŠ₯ := ⟨λ h, h.solvable, Ξ» h, ⟨h⟩⟩ @[priority 100] instance comm_group.is_solvable {G : Type*} [comm_group G] : is_solvable G := begin use 1, rw [eq_bot_iff, derived_series_one], calc commutator G ≀ (monoid_hom.id G).ker : abelianization.commutator_subset_ker (monoid_hom.id G) ... = βŠ₯ : rfl, end lemma is_solvable_of_comm {G : Type*} [hG : group G] (h : βˆ€ a b : G, a * b = b * a) : is_solvable G := begin letI hG' : comm_group G := { mul_comm := h .. hG }, tactic.unfreeze_local_instances, cases hG, exact comm_group.is_solvable, end lemma is_solvable_of_top_eq_bot (h : (⊀ : subgroup G) = βŠ₯) : is_solvable G := ⟨⟨0, h⟩⟩ @[priority 100] instance is_solvable_of_subsingleton [subsingleton G] : is_solvable G := is_solvable_of_top_eq_bot G (by ext; simp at *) variables {G} {G' : Type*} [group G'] {f : G β†’* G'} lemma solvable_of_solvable_injective (hf : function.injective f) [h : is_solvable G'] : is_solvable G := begin rw is_solvable_def at *, cases h with n hn, use n, rw ← map_eq_bot_iff_of_injective _ hf, rw eq_bot_iff at *, calc map f (derived_series G n) ≀ derived_series G' n : map_derived_series_le_derived_series f n ... ≀ βŠ₯ : hn, end instance subgroup_solvable_of_solvable (H : subgroup G) [h : is_solvable G] : is_solvable H := solvable_of_solvable_injective (show function.injective (subtype H), from subtype.val_injective) lemma solvable_of_surjective (hf : function.surjective f) [h : is_solvable G] : is_solvable G' := begin rw is_solvable_def at *, cases h with n hn, use n, calc derived_series G' n = (derived_series G n).map f : eq.symm (map_derived_series_eq hf n) ... = (βŠ₯ : subgroup G).map f : by rw hn ... = βŠ₯ : map_bot f, end instance solvable_quotient_of_solvable (H : subgroup G) [H.normal] [h : is_solvable G] : is_solvable (quotient_group.quotient H) := solvable_of_surjective (show function.surjective (quotient_group.mk' H), by tidy) end solvable
51e3ddf380b22966656db7bddbec301e6e486cc4
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/order/filter/ultrafilter.lean
bcf154a39b370aa62cf102d40ebf02cd0e17072a
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
12,348
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, Jeremy Avigad, Yury Kudryashov -/ import order.filter.cofinite /-! # Ultrafilters An ultrafilter is a minimal (maximal in the set order) proper filter. In this file we define * `ultrafilter.of`: an ultrafilter that is less than or equal to a given filter; * `ultrafilter`: subtype of ultrafilters; * `ultrafilter.pure`: `pure x` as an `ultrafiler`; * `ultrafilter.map`, `ultrafilter.bind`, `ultrafilter.comap` : operations on ultrafilters; * `hyperfilter`: the ultrafilter extending the cofinite filter. -/ universes u v variables {Ξ± : Type u} {Ξ² : Type v} open set zorn filter function open_locale classical filter /-- An ultrafilter is a minimal (maximal in the set order) proper filter. -/ @[protect_proj] structure ultrafilter (Ξ± : Type*) extends filter Ξ± := (ne_bot' : ne_bot to_filter) (le_of_le : βˆ€ g, filter.ne_bot g β†’ g ≀ to_filter β†’ to_filter ≀ g) namespace ultrafilter variables {f g : ultrafilter Ξ±} {s t : set Ξ±} {p q : Ξ± β†’ Prop} instance : has_coe_t (ultrafilter Ξ±) (filter Ξ±) := ⟨ultrafilter.to_filter⟩ instance : has_mem (set Ξ±) (ultrafilter Ξ±) := ⟨λ s f, s ∈ (f : filter Ξ±)⟩ lemma unique (f : ultrafilter Ξ±) {g : filter Ξ±} (h : g ≀ f) (hne : ne_bot g . tactic.apply_instance) : g = f := le_antisymm h $ f.le_of_le g hne h instance ne_bot (f : ultrafilter Ξ±) : ne_bot (f : filter Ξ±) := f.ne_bot' @[simp, norm_cast] lemma mem_coe : s ∈ (f : filter Ξ±) ↔ s ∈ f := iff.rfl lemma coe_injective : injective (coe : ultrafilter Ξ± β†’ filter Ξ±) | ⟨f, h₁, hβ‚‚βŸ© ⟨g, h₃, hβ‚„βŸ© rfl := by congr @[simp, norm_cast] lemma coe_le_coe {f g : ultrafilter Ξ±} : (f : filter Ξ±) ≀ g ↔ f = g := ⟨λ h, coe_injective $ g.unique h, Ξ» h, h β–Έ le_rfl⟩ @[simp, norm_cast] lemma coe_inj : (f : filter Ξ±) = g ↔ f = g := coe_injective.eq_iff @[ext] lemma ext ⦃f g : ultrafilter α⦄ (h : βˆ€ s, s ∈ f ↔ s ∈ g) : f = g := coe_injective $ filter.ext h lemma le_of_inf_ne_bot (f : ultrafilter Ξ±) {g : filter Ξ±} (hg : ne_bot (↑f βŠ“ g)) : ↑f ≀ g := le_of_inf_eq (f.unique inf_le_left hg) lemma le_of_inf_ne_bot' (f : ultrafilter Ξ±) {g : filter Ξ±} (hg : ne_bot (g βŠ“ f)) : ↑f ≀ g := f.le_of_inf_ne_bot $ by rwa inf_comm @[simp] lemma compl_not_mem_iff : sᢜ βˆ‰ f ↔ s ∈ f := ⟨λ hsc, le_principal_iff.1 $ f.le_of_inf_ne_bot ⟨λ h, hsc $ mem_sets_of_eq_bot $ by rwa compl_compl⟩, compl_not_mem_sets⟩ @[simp] lemma frequently_iff_eventually : (βˆƒαΆ  x in f, p x) ↔ βˆ€αΆ  x in f, p x := compl_not_mem_iff alias frequently_iff_eventually ↔ filter.frequently.eventually _ lemma compl_mem_iff_not_mem : sᢜ ∈ f ↔ s βˆ‰ f := by rw [← compl_not_mem_iff, compl_compl] /-- If `sᢜ βˆ‰ f ↔ s ∈ f`, then `f` is an ultrafilter. The other implication is given by `ultrafilter.compl_not_mem_iff`. -/ def of_compl_not_mem_iff (f : filter Ξ±) (h : βˆ€ s, sᢜ βˆ‰ f ↔ s ∈ f) : ultrafilter Ξ± := { to_filter := f, ne_bot' := ⟨λ hf, by simpa [hf] using h⟩, le_of_le := Ξ» g hg hgf s hs, (h s).1 $ Ξ» hsc, by exactI compl_not_mem_sets hs (hgf hsc) } lemma nonempty_of_mem (hs : s ∈ f) : s.nonempty := nonempty_of_mem_sets hs lemma ne_empty_of_mem (hs : s ∈ f) : s β‰  βˆ… := (nonempty_of_mem hs).ne_empty @[simp] lemma empty_not_mem : βˆ… βˆ‰ f := empty_nmem_sets f lemma mem_or_compl_mem (f : ultrafilter Ξ±) (s : set Ξ±) : s ∈ f ∨ sᢜ ∈ f := or_iff_not_imp_left.2 compl_mem_iff_not_mem.2 protected lemma em (f : ultrafilter Ξ±) (p : Ξ± β†’ Prop) : (βˆ€αΆ  x in f, p x) ∨ βˆ€αΆ  x in f, Β¬p x := f.mem_or_compl_mem {x | p x} lemma eventually_or : (βˆ€αΆ  x in f, p x ∨ q x) ↔ (βˆ€αΆ  x in f, p x) ∨ βˆ€αΆ  x in f, q x := ⟨λ H, (f.em p).imp_right $ Ξ» hp, (H.and hp).mono $ Ξ» x ⟨hx, hnx⟩, hx.resolve_left hnx, Ξ» H, H.elim (Ξ» hp, hp.mono $ Ξ» x, or.inl) (Ξ» hp, hp.mono $ Ξ» x, or.inr)⟩ lemma union_mem_iff : s βˆͺ t ∈ f ↔ s ∈ f ∨ t ∈ f := eventually_or lemma eventually_not : (βˆ€αΆ  x in f, Β¬p x) ↔ Β¬βˆ€αΆ  x in f, p x := compl_mem_iff_not_mem lemma eventually_imp : (βˆ€αΆ  x in f, p x β†’ q x) ↔ (βˆ€αΆ  x in f, p x) β†’ βˆ€αΆ  x in f, q x := by simp only [imp_iff_not_or, eventually_or, eventually_not] lemma finite_sUnion_mem_iff {s : set (set Ξ±)} (hs : finite s) : ⋃₀ s ∈ f ↔ βˆƒt∈s, t ∈ f := finite.induction_on hs (by simp) $ Ξ» a s ha hs his, by simp [union_mem_iff, his, or_and_distrib_right, exists_or_distrib] lemma finite_bUnion_mem_iff {is : set Ξ²} {s : Ξ² β†’ set Ξ±} (his : finite is) : (⋃i∈is, s i) ∈ f ↔ βˆƒi∈is, s i ∈ f := by simp only [← sUnion_image, finite_sUnion_mem_iff (his.image s), bex_image_iff] /-- Pushforward for ultrafilters. -/ def map (m : Ξ± β†’ Ξ²) (f : ultrafilter Ξ±) : ultrafilter Ξ² := of_compl_not_mem_iff (map m f) $ Ξ» s, @compl_not_mem_iff _ f (m ⁻¹' s) @[simp, norm_cast] lemma coe_map (m : Ξ± β†’ Ξ²) (f : ultrafilter Ξ±) : (map m f : filter Ξ²) = filter.map m ↑f := rfl @[simp] lemma mem_map {m : Ξ± β†’ Ξ²} {f : ultrafilter Ξ±} {s : set Ξ²} : s ∈ map m f ↔ m ⁻¹' s ∈ f := iff.rfl /-- The pullback of an ultrafilter along an injection whose range is large with respect to the given ultrafilter. -/ def comap {m : Ξ± β†’ Ξ²} (u : ultrafilter Ξ²) (inj : injective m) (large : set.range m ∈ u) : ultrafilter Ξ± := { to_filter := comap m u, ne_bot' := u.ne_bot'.comap_of_range_mem large, le_of_le := Ξ» g hg hgu, by { resetI, simp only [← u.unique (map_le_iff_le_comap.2 hgu), comap_map inj, le_rfl] } } /-- The principal ultrafilter associated to a point `x`. -/ instance : has_pure ultrafilter := ⟨λ Ξ± a, of_compl_not_mem_iff (pure a) $ Ξ» s, by simp⟩ @[simp] lemma mem_pure_sets {a : Ξ±} {s : set Ξ±} : s ∈ (pure a : ultrafilter Ξ±) ↔ a ∈ s := iff.rfl instance [inhabited Ξ±] : inhabited (ultrafilter Ξ±) := ⟨pure (default _)⟩ /-- Monadic bind for ultrafilters, coming from the one on filters defined in terms of map and join.-/ def bind (f : ultrafilter Ξ±) (m : Ξ± β†’ ultrafilter Ξ²) : ultrafilter Ξ² := of_compl_not_mem_iff (bind ↑f (Ξ» x, ↑(m x))) $ Ξ» s, by simp only [mem_bind_sets', mem_coe, ← compl_mem_iff_not_mem, compl_set_of, compl_compl] instance ultrafilter.has_bind : has_bind ultrafilter := ⟨@ultrafilter.bind⟩ instance ultrafilter.functor : functor ultrafilter := { map := @ultrafilter.map } instance ultrafilter.monad : monad ultrafilter := { map := @ultrafilter.map } section local attribute [instance] filter.monad filter.is_lawful_monad instance ultrafilter.is_lawful_monad : is_lawful_monad ultrafilter := { id_map := assume Ξ± f, coe_injective (id_map f.1), pure_bind := assume Ξ± Ξ² a f, coe_injective (pure_bind a (coe ∘ f)), bind_assoc := assume Ξ± Ξ² Ξ³ f m₁ mβ‚‚, coe_injective (filter_eq rfl), bind_pure_comp_eq_map := assume Ξ± Ξ² f x, coe_injective (bind_pure_comp_eq_map f x.1) } end /-- The ultrafilter lemma: Any proper filter is contained in an ultrafilter. -/ lemma exists_le (f : filter Ξ±) [h : ne_bot f] : βˆƒu : ultrafilter Ξ±, ↑u ≀ f := begin let Ο„ := {f' // ne_bot f' ∧ f' ≀ f}, let r : Ο„ β†’ Ο„ β†’ Prop := Ξ»t₁ tβ‚‚, tβ‚‚.val ≀ t₁.val, haveI := nonempty_of_ne_bot f, let top : Ο„ := ⟨f, h, le_refl f⟩, let sup : Ξ (c:set Ο„), chain r c β†’ Ο„ := Ξ»c hc, βŸ¨β¨…a:{a:Ο„ // a ∈ insert top c}, a.1, infi_ne_bot_of_directed (directed_of_chain $ chain_insert hc $ Ξ» ⟨b, _, hb⟩ _ _, or.inl hb) (assume ⟨⟨a, ha, _⟩, _⟩, ha), infi_le_of_le ⟨top, mem_insert _ _⟩ (le_refl _)⟩, have : βˆ€c (hc: chain r c) a (ha : a ∈ c), r a (sup c hc), from assume c hc a ha, infi_le_of_le ⟨a, mem_insert_of_mem _ ha⟩ (le_refl _), have : (βˆƒ (u : Ο„), βˆ€ (a : Ο„), r u a β†’ r a u), from exists_maximal_of_chains_bounded (assume c hc, ⟨sup c hc, this c hc⟩) (assume f₁ fβ‚‚ f₃ h₁ hβ‚‚, le_trans hβ‚‚ h₁), cases this with uΟ„ hmin, exact ⟨⟨uΟ„.val, uΟ„.property.left, assume g hg₁ hgβ‚‚, hmin ⟨g, hg₁, le_trans hgβ‚‚ uΟ„.property.right⟩ hgβ‚‚βŸ©, uΟ„.property.right⟩ end alias exists_le ← filter.exists_ultrafilter_le /-- Construct an ultrafilter extending a given filter. The ultrafilter lemma is the assertion that such a filter exists; we use the axiom of choice to pick one. -/ noncomputable def of (f : filter Ξ±) [ne_bot f] : ultrafilter Ξ± := classical.some (exists_le f) lemma of_le (f : filter Ξ±) [ne_bot f] : ↑(of f) ≀ f := classical.some_spec (exists_le f) lemma of_coe (f : ultrafilter Ξ±) : of ↑f = f := coe_inj.1 $ f.unique (of_le f) lemma exists_ultrafilter_of_finite_inter_nonempty (S : set (set Ξ±)) (cond : βˆ€ T : finset (set Ξ±), (↑T : set (set Ξ±)) βŠ† S β†’ (β‹‚β‚€ (↑T : set (set Ξ±))).nonempty) : βˆƒ F : ultrafilter Ξ±, S βŠ† F.sets := begin suffices : βˆƒ F : filter Ξ±, ne_bot F ∧ S βŠ† F.sets, { rcases this with ⟨F, cond, hF⟩, resetI, obtain ⟨G : ultrafilter Ξ±, h1 : ↑G ≀ F⟩ := exists_le F, exact ⟨G, Ξ» T hT, h1 (hF hT)⟩ }, use filter.generate S, refine ⟨_, Ξ» T hT, filter.generate_sets.basic hT⟩, rw ← forall_sets_nonempty_iff_ne_bot, intros T hT, rcases mem_generate_iff.mp hT with ⟨A, h1, h2, h3⟩, let B := set.finite.to_finset h2, rw (show A = ↑B, by simp) at *, rcases cond B h1 with ⟨x, hx⟩, exact ⟨x, h3 hx⟩, end end ultrafilter namespace filter open ultrafilter lemma mem_iff_ultrafilter {s : set Ξ±} {f : filter Ξ±} : s ∈ f ↔ βˆ€ g : ultrafilter Ξ±, ↑g ≀ f β†’ s ∈ g := begin refine ⟨λ hf g hg, hg hf, Ξ» H, by_contra $ Ξ» hf, _⟩, set g : filter β†₯sᢜ := comap coe f, haveI : ne_bot g := comap_ne_bot_iff_compl_range.2 (by simpa [compl_set_of]), simpa using H ((of g).map coe) (map_le_iff_le_comap.mpr (of_le g)) end lemma le_iff_ultrafilter {f₁ fβ‚‚ : filter Ξ±} : f₁ ≀ fβ‚‚ ↔ βˆ€ g : ultrafilter Ξ±, ↑g ≀ f₁ β†’ ↑g ≀ fβ‚‚ := ⟨λ h g h₁, h₁.trans h, Ξ» h s hs, mem_iff_ultrafilter.2 $ Ξ» g hg, h g hg hs⟩ /-- A filter equals the intersection of all the ultrafilters which contain it. -/ lemma supr_ultrafilter_le_eq (f : filter Ξ±) : (⨆ (g : ultrafilter Ξ±) (hg : ↑g ≀ f), (g : filter Ξ±)) = f := eq_of_forall_ge_iff $ Ξ» f', by simp only [supr_le_iff, ← le_iff_ultrafilter] /-- The `tendsto` relation can be checked on ultrafilters. -/ lemma tendsto_iff_ultrafilter (f : Ξ± β†’ Ξ²) (l₁ : filter Ξ±) (lβ‚‚ : filter Ξ²) : tendsto f l₁ lβ‚‚ ↔ βˆ€ g : ultrafilter Ξ±, ↑g ≀ l₁ β†’ tendsto f g lβ‚‚ := by simpa only [tendsto_iff_comap] using le_iff_ultrafilter lemma exists_ultrafilter_iff {f : filter Ξ±} : (βˆƒ (u : ultrafilter Ξ±), ↑u ≀ f) ↔ ne_bot f := ⟨λ ⟨u, uf⟩, ne_bot_of_le uf, Ξ» h, @exists_ultrafilter_le _ _ h⟩ lemma forall_ne_bot_le_iff {g : filter Ξ±} {p : filter Ξ± β†’ Prop} (hp : monotone p) : (βˆ€ f : filter Ξ±, ne_bot f β†’ f ≀ g β†’ p f) ↔ βˆ€ f : ultrafilter Ξ±, ↑f ≀ g β†’ p f := begin refine ⟨λ H f hf, H f f.ne_bot hf, _⟩, introsI H f hf hfg, exact hp (of_le f) (H _ ((of_le f).trans hfg)) end section hyperfilter variables (Ξ±) [infinite Ξ±] /-- The ultrafilter extending the cofinite filter. -/ noncomputable def hyperfilter : ultrafilter Ξ± := ultrafilter.of cofinite variable {Ξ±} lemma hyperfilter_le_cofinite : ↑(hyperfilter Ξ±) ≀ @cofinite Ξ± := ultrafilter.of_le cofinite @[simp] lemma bot_ne_hyperfilter : (βŠ₯ : filter Ξ±) β‰  hyperfilter Ξ± := (by apply_instance : ne_bot ↑(hyperfilter Ξ±)).1.symm theorem nmem_hyperfilter_of_finite {s : set Ξ±} (hf : s.finite) : s βˆ‰ hyperfilter Ξ± := Ξ» hy, compl_not_mem_sets hy $ hyperfilter_le_cofinite hf.compl_mem_cofinite alias nmem_hyperfilter_of_finite ← set.finite.nmem_hyperfilter theorem compl_mem_hyperfilter_of_finite {s : set Ξ±} (hf : set.finite s) : sᢜ ∈ hyperfilter Ξ± := compl_mem_iff_not_mem.2 hf.nmem_hyperfilter alias compl_mem_hyperfilter_of_finite ← set.finite.compl_mem_hyperfilter theorem mem_hyperfilter_of_finite_compl {s : set Ξ±} (hf : set.finite sᢜ) : s ∈ hyperfilter Ξ± := compl_compl s β–Έ hf.compl_mem_hyperfilter end hyperfilter end filter
98bc42f17b79001c7e7c52649230ab2c76f65e68
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/measure_theory/probability_mass_function_auto.lean
b8b99e7515e83986fc1a291c62a05c0dac146920
[]
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
5,564
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes HΓΆlzl -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.instances.ennreal import Mathlib.PostPort universes u u_1 u_2 u_3 namespace Mathlib /-! # Probability mass functions This file is about probability mass functions or discrete probability measures: a function `Ξ± β†’ ℝβ‰₯0` such that the values have (infinite) sum `1`. This file features the monadic structure of `pmf` and the Bernoulli distribution ## Implementation Notes This file is not yet connected to the `measure_theory` library in any way. At some point we need to define a `measure` from a `pmf` and prove the appropriate lemmas about that. ## Tags probability mass function, discrete probability measure, bernoulli distribution -/ /-- A probability mass function, or discrete probability measures is a function `Ξ± β†’ ℝβ‰₯0` such that the values have (infinite) sum `1`. -/ def pmf (Ξ± : Type u) := Subtype fun (f : Ξ± β†’ nnreal) => has_sum f 1 namespace pmf protected instance has_coe_to_fun {Ξ± : Type u_1} : has_coe_to_fun (pmf Ξ±) := has_coe_to_fun.mk (fun (p : pmf Ξ±) => Ξ± β†’ nnreal) fun (p : pmf Ξ±) (a : Ξ±) => subtype.val p a protected theorem ext {Ξ± : Type u_1} {p : pmf Ξ±} {q : pmf Ξ±} : (βˆ€ (a : Ξ±), coe_fn p a = coe_fn q a) β†’ p = q := sorry theorem has_sum_coe_one {Ξ± : Type u_1} (p : pmf Ξ±) : has_sum (⇑p) 1 := subtype.property p theorem summable_coe {Ξ± : Type u_1} (p : pmf Ξ±) : summable ⇑p := has_sum.summable (has_sum_coe_one p) @[simp] theorem tsum_coe {Ξ± : Type u_1} (p : pmf Ξ±) : (tsum fun (a : Ξ±) => coe_fn p a) = 1 := has_sum.tsum_eq (has_sum_coe_one p) /-- The support of a `pmf` is the set where it is nonzero. -/ def support {Ξ± : Type u_1} (p : pmf Ξ±) : set Ξ± := set_of fun (a : Ξ±) => subtype.val p a β‰  0 /-- The pure `pmf` is the `pmf` where all the mass lies in one point. The value of `pure a` is `1` at `a` and `0` elsewhere. -/ def pure {Ξ± : Type u_1} (a : Ξ±) : pmf Ξ± := { val := fun (a' : Ξ±) => ite (a' = a) 1 0, property := sorry } @[simp] theorem pure_apply {Ξ± : Type u_1} (a : Ξ±) (a' : Ξ±) : coe_fn (pure a) a' = ite (a' = a) 1 0 := rfl protected instance inhabited {Ξ± : Type u_1} [Inhabited Ξ±] : Inhabited (pmf Ξ±) := { default := pure Inhabited.default } theorem coe_le_one {Ξ± : Type u_1} (p : pmf Ξ±) (a : Ξ±) : coe_fn p a ≀ 1 := sorry protected theorem bind.summable {Ξ± : Type u_1} {Ξ² : Type u_2} (p : pmf Ξ±) (f : Ξ± β†’ pmf Ξ²) (b : Ξ²) : summable fun (a : Ξ±) => coe_fn p a * coe_fn (f a) b := sorry /-- The monadic bind operation for `pmf`. -/ def bind {Ξ± : Type u_1} {Ξ² : Type u_2} (p : pmf Ξ±) (f : Ξ± β†’ pmf Ξ²) : pmf Ξ² := { val := fun (b : Ξ²) => tsum fun (a : Ξ±) => coe_fn p a * coe_fn (f a) b, property := sorry } @[simp] theorem bind_apply {Ξ± : Type u_1} {Ξ² : Type u_2} (p : pmf Ξ±) (f : Ξ± β†’ pmf Ξ²) (b : Ξ²) : coe_fn (bind p f) b = tsum fun (a : Ξ±) => coe_fn p a * coe_fn (f a) b := rfl theorem coe_bind_apply {Ξ± : Type u_1} {Ξ² : Type u_2} (p : pmf Ξ±) (f : Ξ± β†’ pmf Ξ²) (b : Ξ²) : ↑(coe_fn (bind p f) b) = tsum fun (a : Ξ±) => ↑(coe_fn p a) * ↑(coe_fn (f a) b) := sorry @[simp] theorem pure_bind {Ξ± : Type u_1} {Ξ² : Type u_2} (a : Ξ±) (f : Ξ± β†’ pmf Ξ²) : bind (pure a) f = f a := sorry @[simp] theorem bind_pure {Ξ± : Type u_1} (p : pmf Ξ±) : bind p pure = p := sorry @[simp] theorem bind_bind {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} (p : pmf Ξ±) (f : Ξ± β†’ pmf Ξ²) (g : Ξ² β†’ pmf Ξ³) : bind (bind p f) g = bind p fun (a : Ξ±) => bind (f a) g := sorry theorem bind_comm {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} (p : pmf Ξ±) (q : pmf Ξ²) (f : Ξ± β†’ Ξ² β†’ pmf Ξ³) : (bind p fun (a : Ξ±) => bind q (f a)) = bind q fun (b : Ξ²) => bind p fun (a : Ξ±) => f a b := sorry /-- The functorial action of a function on a `pmf`. -/ def map {Ξ± : Type u_1} {Ξ² : Type u_2} (f : Ξ± β†’ Ξ²) (p : pmf Ξ±) : pmf Ξ² := bind p (pure ∘ f) theorem bind_pure_comp {Ξ± : Type u_1} {Ξ² : Type u_2} (f : Ξ± β†’ Ξ²) (p : pmf Ξ±) : bind p (pure ∘ f) = map f p := rfl theorem map_id {Ξ± : Type u_1} (p : pmf Ξ±) : map id p = p := sorry theorem map_comp {Ξ± : Type u_1} {Ξ² : Type u_2} {Ξ³ : Type u_3} (p : pmf Ξ±) (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ Ξ³) : map g (map f p) = map (g ∘ f) p := sorry theorem pure_map {Ξ± : Type u_1} {Ξ² : Type u_2} (a : Ξ±) (f : Ξ± β†’ Ξ²) : map f (pure a) = pure (f a) := sorry /-- The monadic sequencing operation for `pmf`. -/ def seq {Ξ± : Type u_1} {Ξ² : Type u_2} (f : pmf (Ξ± β†’ Ξ²)) (p : pmf Ξ±) : pmf Ξ² := bind f fun (m : Ξ± β†’ Ξ²) => bind p fun (a : Ξ±) => pure (m a) /-- Given a non-empty multiset `s` we construct the `pmf` which sends `a` to the fraction of elements in `s` that are `a`. -/ def of_multiset {Ξ± : Type u_1} (s : multiset Ξ±) (hs : s β‰  0) : pmf Ξ± := { val := fun (a : Ξ±) => ↑(multiset.count a s) / ↑(coe_fn multiset.card s), property := sorry } /-- Given a finite type `Ξ±` and a function `f : Ξ± β†’ ℝβ‰₯0` with sum 1, we get a `pmf`. -/ def of_fintype {Ξ± : Type u_1} [fintype Ξ±] (f : Ξ± β†’ nnreal) (h : (finset.sum finset.univ fun (x : Ξ±) => f x) = 1) : pmf Ξ± := { val := f, property := sorry } /-- A `pmf` which assigns probability `p` to `tt` and `1 - p` to `ff`. -/ def bernoulli (p : nnreal) (h : p ≀ 1) : pmf Bool := of_fintype (fun (b : Bool) => cond b p (1 - p)) sorry end Mathlib
75b1004f622fbf8e9ec916db8ad3160402cd8096
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/smt/prove.lean
04501e2d1989a585649a5f39bb0437a61cef6c78
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
359
lean
namespace smt open tactic private meta def collect_props : list expr β†’ tactic (list expr) | [] := return [] | (H :: Hs) := do Eqs ← collect_props Hs, Htype ← infer_type H >>= whnf, return $ if Htype = expr.prop then (H :: Eqs) else Eqs meta def prove : tactic unit := do local_context >>= collect_props >>= revert_lst, simp end smt
31eb51336298f886bcdc03b9e4b875b5ecf13ace
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/subst3.lean
a415b51636aa62a3fa541921661a560848d0b0d4
[ "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
243
lean
import macros theorem T (A : Type) (p : A -> Bool) (f : A -> A -> A) : forall x y z, p (f x x) β†’ x = y β†’ x = z β†’ p (f y z) := Ξ» x y z, Ξ» (H1 : p (f x x)) (H2 : x = y) (H3 : x = z), subst (subst H1 H2) H3 print environment 1.
4a9475eb6e8fcb17e0a2d7b635954badfe037a6c
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/data/complex/is_R_or_C.lean
462281fa820347c0ff7e60e00495cc66cede5c8d
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
31,453
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 data.real.sqrt import field_theory.tower import analysis.normed_space.finite_dimension /-! # `is_R_or_C`: a typeclass for ℝ or β„‚ This file defines the typeclass `is_R_or_C` intended to have only two instances: ℝ and β„‚. It is meant for definitions and theorems which hold for both the real and the complex case, and in particular when the real case follows directly from the complex case by setting `re` to `id`, `im` to zero and so on. Its API follows closely that of β„‚. Possible applications include defining inner products and Hilbert spaces for both the real and complex case. One would produce the definitions and proof for an arbitrary field of this typeclass, which basically amounts to doing the complex case, and the two cases then fall out immediately from the two instances of the class. The instance for `ℝ` is registered in this file. The instance for `β„‚` is declared in `analysis.complex.basic`. ## Implementation notes The coercion from reals into an `is_R_or_C` field is done by registering `algebra_map ℝ K` as a `has_coe_t`. For this to work, we must proceed carefully to avoid problems involving circular coercions in the case `K=ℝ`; in particular, we cannot use the plain `has_coe` and must set priorities carefully. This problem was already solved for `β„•`, and we copy the solution detailed in `data/nat/cast`. See also Note [coercion into rings] for more details. In addition, several lemmas need to be set at priority 900 to make sure that they do not override their counterparts in `complex.lean` (which causes linter errors). -/ open_locale big_operators section local notation `π“š` := algebra_map ℝ _ open_locale complex_conjugate /-- This typeclass captures properties shared by ℝ and β„‚, with an API that closely matches that of β„‚. -/ class is_R_or_C (K : Type*) extends nondiscrete_normed_field K, star_ring K, normed_algebra ℝ K, complete_space K := (re : K β†’+ ℝ) (im : K β†’+ ℝ) (I : K) -- Meant to be set to 0 for K=ℝ (I_re_ax : re I = 0) (I_mul_I_ax : I = 0 ∨ I * I = -1) (re_add_im_ax : βˆ€ (z : K), π“š (re z) + π“š (im z) * I = z) (of_real_re_ax : βˆ€ r : ℝ, re (π“š r) = r) (of_real_im_ax : βˆ€ r : ℝ, im (π“š r) = 0) (mul_re_ax : βˆ€ z w : K, re (z * w) = re z * re w - im z * im w) (mul_im_ax : βˆ€ z w : K, im (z * w) = re z * im w + im z * re w) (conj_re_ax : βˆ€ z : K, re (conj z) = re z) (conj_im_ax : βˆ€ z : K, im (conj z) = -(im z)) (conj_I_ax : conj I = -I) (norm_sq_eq_def_ax : βˆ€ (z : K), βˆ₯zβˆ₯^2 = (re z) * (re z) + (im z) * (im z)) (mul_im_I_ax : βˆ€ (z : K), (im z) * im I = im z) (inv_def_ax : βˆ€ (z : K), z⁻¹ = conj z * π“š ((βˆ₯zβˆ₯^2)⁻¹)) (div_I_ax : βˆ€ (z : K), z / I = -(z * I)) end namespace is_R_or_C variables {K : Type*} [is_R_or_C K] open_locale complex_conjugate /- The priority must be set at 900 to ensure that coercions are tried in the right order. See Note [coercion into rings], or `data/nat/cast.lean` for more details. -/ @[priority 900] noncomputable instance algebra_map_coe : has_coe_t ℝ K := ⟨algebra_map ℝ K⟩ lemma of_real_alg (x : ℝ) : (x : K) = x β€’ (1 : K) := algebra.algebra_map_eq_smul_one x lemma algebra_map_eq_of_real : ⇑(algebra_map ℝ K) = coe := rfl @[simp] lemma re_add_im (z : K) : ((re z) : K) + (im z) * I = z := is_R_or_C.re_add_im_ax z @[simp, norm_cast] lemma of_real_re : βˆ€ r : ℝ, re (r : K) = r := is_R_or_C.of_real_re_ax @[simp, norm_cast] lemma of_real_im : βˆ€ r : ℝ, im (r : K) = 0 := is_R_or_C.of_real_im_ax @[simp] lemma mul_re : βˆ€ z w : K, re (z * w) = re z * re w - im z * im w := is_R_or_C.mul_re_ax @[simp] lemma mul_im : βˆ€ z w : K, im (z * w) = re z * im w + im z * re w := is_R_or_C.mul_im_ax theorem inv_def (z : K) : z⁻¹ = conj z * ((βˆ₯zβˆ₯^2)⁻¹:ℝ) := is_R_or_C.inv_def_ax z theorem ext_iff : βˆ€ {z w : K}, z = w ↔ re z = re w ∧ im z = im w := Ξ» z w, { mp := by { rintro rfl, cc }, mpr := by { rintro ⟨h₁,hβ‚‚βŸ©, rw [←re_add_im z, ←re_add_im w, h₁, hβ‚‚] } } theorem ext : βˆ€ {z w : K}, re z = re w β†’ im z = im w β†’ z = w := by { simp_rw ext_iff, cc } @[simp, norm_cast, priority 900] lemma of_real_zero : ((0 : ℝ) : K) = 0 := by rw [of_real_alg, zero_smul] @[simp] lemma zero_re' : re (0 : K) = (0 : ℝ) := re.map_zero @[simp, norm_cast, priority 900] lemma of_real_one : ((1 : ℝ) : K) = 1 := by rw [of_real_alg, one_smul] @[simp] lemma one_re : re (1 : K) = 1 := by rw [←of_real_one, of_real_re] @[simp] lemma one_im : im (1 : K) = 0 := by rw [←of_real_one, of_real_im] @[simp, norm_cast, priority 900] theorem of_real_inj {z w : ℝ} : (z : K) = (w : K) ↔ z = w := { mp := Ξ» h, by { convert congr_arg re h; simp only [of_real_re] }, mpr := Ξ» h, by rw h } @[simp] lemma bit0_re (z : K) : re (bit0 z) = bit0 (re z) := by simp [bit0] @[simp] lemma bit1_re (z : K) : re (bit1 z) = bit1 (re z) := by simp only [bit1, add_monoid_hom.map_add, bit0_re, add_right_inj, one_re] @[simp] lemma bit0_im (z : K) : im (bit0 z) = bit0 (im z) := by simp [bit0] @[simp] lemma bit1_im (z : K) : im (bit1 z) = bit0 (im z) := by simp only [bit1, add_right_eq_self, add_monoid_hom.map_add, bit0_im, one_im] @[simp, priority 900] theorem of_real_eq_zero {z : ℝ} : (z : K) = 0 ↔ z = 0 := by rw [←of_real_zero]; exact of_real_inj @[simp, norm_cast, priority 900] lemma of_real_add ⦃r s : ℝ⦄ : ((r + s : ℝ) : K) = r + s := by { apply (@is_R_or_C.ext_iff K _ ((r + s : ℝ) : K) (r + s)).mpr, simp } @[simp, norm_cast, priority 900] lemma of_real_bit0 (r : ℝ) : ((bit0 r : ℝ) : K) = bit0 (r : K) := ext_iff.2 $ by simp [bit0] @[simp, norm_cast, priority 900] lemma of_real_bit1 (r : ℝ) : ((bit1 r : ℝ) : K) = bit1 (r : K) := ext_iff.2 $ by simp [bit1] /- Note: This can be proven by `norm_num` once K is proven to be of characteristic zero below. -/ lemma two_ne_zero : (2 : K) β‰  0 := begin intro h, rw [(show (2 : K) = ((2 : ℝ) : K), by norm_num), ←of_real_zero, of_real_inj] at h, linarith, end @[simp, norm_cast, priority 900] lemma of_real_neg (r : ℝ) : ((-r : ℝ) : K) = -r := ext_iff.2 $ by simp @[simp, norm_cast, priority 900] lemma of_real_mul (r s : ℝ) : ((r * s : ℝ) : K) = r * s := ext_iff.2 $ by simp @[simp, norm_cast] lemma of_real_smul (r x : ℝ) : r β€’ (x : K) = (r : K) * (x : K) := by { simp_rw [← smul_eq_mul, of_real_alg r], simp, } lemma of_real_mul_re (r : ℝ) (z : K) : re (↑r * z) = r * re z := by simp only [mul_re, of_real_im, zero_mul, of_real_re, sub_zero] lemma of_real_mul_im (r : ℝ) (z : K) : im (↑r * z) = r * (im z) := by simp only [add_zero, of_real_im, zero_mul, of_real_re, mul_im] lemma smul_re : βˆ€ (r : ℝ) (z : K), re (r β€’ z) = r * (re z) := Ξ» r z, by { rw algebra.smul_def, apply of_real_mul_re } lemma smul_im : βˆ€ (r : ℝ) (z : K), im (r β€’ z) = r * (im z) := Ξ» r z, by { rw algebra.smul_def, apply of_real_mul_im } /-! ### The imaginary unit, `I` -/ /-- The imaginary unit. -/ @[simp] lemma I_re : re (I : K) = 0 := I_re_ax @[simp] lemma I_im (z : K) : im z * im (I : K) = im z := mul_im_I_ax z @[simp] lemma I_im' (z : K) : im (I : K) * im z = im z := by rw [mul_comm, I_im _] lemma I_mul_re (z : K) : re (I * z) = - im z := by simp only [I_re, zero_sub, I_im', zero_mul, mul_re] lemma I_mul_I : (I : K) = 0 ∨ (I : K) * I = -1 := I_mul_I_ax @[simp] lemma conj_re (z : K) : re (conj z) = re z := is_R_or_C.conj_re_ax z @[simp] lemma conj_im (z : K) : im (conj z) = -(im z) := is_R_or_C.conj_im_ax z @[simp] lemma conj_I : conj (I : K) = -I := is_R_or_C.conj_I_ax @[simp] lemma conj_of_real (r : ℝ) : conj (r : K) = (r : K) := by { rw ext_iff, simp only [of_real_im, conj_im, eq_self_iff_true, conj_re, and_self, neg_zero] } @[simp] lemma conj_bit0 (z : K) : conj (bit0 z) = bit0 (conj z) := by simp [bit0, ext_iff] @[simp] lemma conj_bit1 (z : K) : conj (bit1 z) = bit1 (conj z) := by simp [bit0, ext_iff] @[simp] lemma conj_neg_I : conj (-I) = (I : K) := by simp [ext_iff] lemma conj_eq_re_sub_im (z : K) : conj z = re z - (im z) * I := by { rw ext_iff, simp, } lemma conj_smul (r : ℝ) (z : K) : conj (r β€’ z) = r β€’ conj z := begin simp_rw conj_eq_re_sub_im, simp only [smul_re, smul_im, of_real_mul], rw smul_sub, simp_rw of_real_alg, simp, end lemma eq_conj_iff_real {z : K} : conj z = z ↔ βˆƒ r : ℝ, z = (r : K) := begin split, { intro h, suffices : im z = 0, { use (re z), rw ← add_zero (coe _), convert (re_add_im z).symm, simp [this] }, contrapose! h, rw ← re_add_im z, simp only [conj_of_real, ring_equiv.map_add, ring_equiv.map_mul, conj_I_ax], rw [add_left_cancel_iff, ext_iff], simpa [neg_eq_iff_add_eq_zero, add_self_eq_zero] }, { rintros ⟨r, rfl⟩, apply conj_of_real } end variables (K) /-- Conjugation as a ring equivalence. This is used to convert the inner product into a sesquilinear product. -/ abbreviation conj_to_ring_equiv : K ≃+* Kα΅’α΅– := star_ring_equiv variables {K} lemma eq_conj_iff_re {z : K} : conj z = z ↔ ((re z) : K) = z := eq_conj_iff_real.trans ⟨by rintro ⟨r, rfl⟩; simp, Ξ» h, ⟨_, h.symm⟩⟩ /-- The norm squared function. -/ def norm_sq : monoid_with_zero_hom K ℝ := { to_fun := Ξ» z, re z * re z + im z * im z, map_zero' := by simp, map_one' := by simp, map_mul' := Ξ» z w, by { simp, ring } } lemma norm_sq_eq_def {z : K} : βˆ₯zβˆ₯^2 = (re z) * (re z) + (im z) * (im z) := norm_sq_eq_def_ax z lemma norm_sq_eq_def' (z : K) : norm_sq z = βˆ₯zβˆ₯^2 := by { rw norm_sq_eq_def, refl } @[simp] lemma norm_sq_of_real (r : ℝ) : βˆ₯(r : K)βˆ₯^2 = r * r := by simp [norm_sq_eq_def] lemma norm_sq_zero : norm_sq (0 : K) = 0 := norm_sq.map_zero lemma norm_sq_one : norm_sq (1 : K) = 1 := norm_sq.map_one lemma norm_sq_nonneg (z : K) : 0 ≀ norm_sq z := add_nonneg (mul_self_nonneg _) (mul_self_nonneg _) @[simp] lemma norm_sq_eq_zero {z : K} : norm_sq z = 0 ↔ z = 0 := by { rw [norm_sq_eq_def'], simp [sq] } @[simp] lemma norm_sq_pos {z : K} : 0 < norm_sq z ↔ z β‰  0 := by rw [lt_iff_le_and_ne, ne, eq_comm]; simp [norm_sq_nonneg] @[simp] lemma norm_sq_neg (z : K) : norm_sq (-z) = norm_sq z := by simp [norm_sq_eq_def'] @[simp] lemma norm_sq_conj (z : K) : norm_sq (conj z) = norm_sq z := by simp [norm_sq] @[simp] lemma norm_sq_mul (z w : K) : norm_sq (z * w) = norm_sq z * norm_sq w := norm_sq.map_mul z w lemma norm_sq_add (z w : K) : norm_sq (z + w) = norm_sq z + norm_sq w + 2 * (re (z * conj w)) := by simp [norm_sq, sq]; ring lemma re_sq_le_norm_sq (z : K) : re z * re z ≀ norm_sq z := le_add_of_nonneg_right (mul_self_nonneg _) lemma im_sq_le_norm_sq (z : K) : im z * im z ≀ norm_sq z := le_add_of_nonneg_left (mul_self_nonneg _) theorem mul_conj (z : K) : z * conj z = ((norm_sq z) : K) := by simp [ext_iff, norm_sq, mul_comm, sub_eq_neg_add, add_comm] theorem add_conj (z : K) : z + conj z = 2 * (re z) := by simp [ext_iff, two_mul] /-- The pseudo-coercion `of_real` as a `ring_hom`. -/ noncomputable def of_real_hom : ℝ β†’+* K := algebra_map ℝ K /-- The coercion from reals as a `ring_hom`. -/ noncomputable def coe_hom : ℝ β†’+* K := ⟨coe, of_real_one, of_real_mul, of_real_zero, of_real_add⟩ @[simp, norm_cast, priority 900] lemma of_real_sub (r s : ℝ) : ((r - s : ℝ) : K) = r - s := ext_iff.2 $ by simp @[simp, norm_cast, priority 900] lemma of_real_pow (r : ℝ) (n : β„•) : ((r ^ n : ℝ) : K) = r ^ n := by induction n; simp [*, of_real_mul, pow_succ] theorem sub_conj (z : K) : z - conj z = (2 * im z) * I := by simp [ext_iff, two_mul, sub_eq_add_neg, add_mul, mul_im_I_ax] lemma norm_sq_sub (z w : K) : norm_sq (z - w) = norm_sq z + norm_sq w - 2 * re (z * conj w) := by simp [-mul_re, norm_sq_add, add_comm, add_left_comm, sub_eq_add_neg] lemma sqrt_norm_sq_eq_norm {z : K} : real.sqrt (norm_sq z) = βˆ₯zβˆ₯ := begin have hβ‚‚ : βˆ₯zβˆ₯ = real.sqrt (βˆ₯zβˆ₯^2) := (real.sqrt_sq (norm_nonneg z)).symm, rw [hβ‚‚], exact congr_arg real.sqrt (norm_sq_eq_def' z) end /-! ### Inversion -/ @[simp] lemma inv_re (z : K) : re (z⁻¹) = re z / norm_sq z := by simp [inv_def, norm_sq_eq_def, norm_sq, division_def] @[simp] lemma inv_im (z : K) : im (z⁻¹) = im (-z) / norm_sq z := by simp [inv_def, norm_sq_eq_def, norm_sq, division_def] @[simp, norm_cast, priority 900] lemma of_real_inv (r : ℝ) : ((r⁻¹ : ℝ) : K) = r⁻¹ := begin rw ext_iff, by_cases r = 0, { simp [h] }, { simp; field_simp [h, norm_sq] }, end protected lemma inv_zero : (0⁻¹ : K) = 0 := by rw [← of_real_zero, ← of_real_inv, inv_zero] protected theorem mul_inv_cancel {z : K} (h : z β‰  0) : z * z⁻¹ = 1 := by rw [inv_def, ←mul_assoc, mul_conj, ←of_real_mul, ←norm_sq_eq_def', mul_inv_cancel (mt norm_sq_eq_zero.1 h), of_real_one] lemma div_re (z w : K) : re (z / w) = re z * re w / norm_sq w + im z * im w / norm_sq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg] lemma div_im (z w : K) : im (z / w) = im z * re w / norm_sq w - re z * im w / norm_sq w := by simp [div_eq_mul_inv, mul_assoc, sub_eq_add_neg, add_comm] @[simp, norm_cast, priority 900] lemma of_real_div (r s : ℝ) : ((r / s : ℝ) : K) = r / s := (@is_R_or_C.coe_hom K _).map_div r s lemma div_re_of_real {z : K} {r : ℝ} : re (z / r) = re z / r := begin by_cases h : r = 0, { simp [h, of_real_zero] }, { change r β‰  0 at h, rw [div_eq_mul_inv, ←of_real_inv, div_eq_mul_inv], simp [norm_sq, div_mul_eq_div_mul_one_div, div_self h] } end @[simp, norm_cast, priority 900] lemma of_real_fpow (r : ℝ) (n : β„€) : ((r ^ n : ℝ) : K) = r ^ n := (@is_R_or_C.coe_hom K _).map_fpow r n lemma I_mul_I_of_nonzero : (I : K) β‰  0 β†’ (I : K) * I = -1 := by { have := I_mul_I_ax, tauto } @[simp] lemma div_I (z : K) : z / I = -(z * I) := begin by_cases h : (I : K) = 0, { simp [h] }, { field_simp [mul_assoc, I_mul_I_of_nonzero h] } end @[simp] lemma inv_I : (I : K)⁻¹ = -I := by { by_cases h : (I : K) = 0; field_simp [h] } @[simp] lemma norm_sq_inv (z : K) : norm_sq z⁻¹ = (norm_sq z)⁻¹ := (@norm_sq K _).map_inv z @[simp] lemma norm_sq_div (z w : K) : norm_sq (z / w) = norm_sq z / norm_sq w := (@norm_sq K _).map_div z w lemma norm_conj {z : K} : βˆ₯conj zβˆ₯ = βˆ₯zβˆ₯ := by simp only [←sqrt_norm_sq_eq_norm, norm_sq_conj] /-! ### Cast lemmas -/ @[simp, norm_cast, priority 900] theorem of_real_nat_cast (n : β„•) : ((n : ℝ) : K) = n := of_real_hom.map_nat_cast n @[simp, norm_cast] lemma nat_cast_re (n : β„•) : re (n : K) = n := by rw [← of_real_nat_cast, of_real_re] @[simp, norm_cast] lemma nat_cast_im (n : β„•) : im (n : K) = 0 := by rw [← of_real_nat_cast, of_real_im] @[simp, norm_cast, priority 900] theorem of_real_int_cast (n : β„€) : ((n : ℝ) : K) = n := of_real_hom.map_int_cast n @[simp, norm_cast] lemma int_cast_re (n : β„€) : re (n : K) = n := by rw [← of_real_int_cast, of_real_re] @[simp, norm_cast] lemma int_cast_im (n : β„€) : im (n : K) = 0 := by rw [← of_real_int_cast, of_real_im] @[simp, norm_cast, priority 900] theorem of_real_rat_cast (n : β„š) : ((n : ℝ) : K) = n := (@is_R_or_C.of_real_hom K _).map_rat_cast n @[simp, norm_cast] lemma rat_cast_re (q : β„š) : re (q : K) = q := by rw [← of_real_rat_cast, of_real_re] @[simp, norm_cast] lemma rat_cast_im (q : β„š) : im (q : K) = 0 := by rw [← of_real_rat_cast, of_real_im] /-! ### Characteristic zero -/ -- TODO: I think this can be instance, because it is a `Prop` /-- ℝ and β„‚ are both of characteristic zero. Note: This is not registered as an instance to avoid having multiple instances on ℝ and β„‚. -/ lemma char_zero_R_or_C : char_zero K := char_zero_of_inj_zero $ Ξ» n h, by rwa [← of_real_nat_cast, of_real_eq_zero, nat.cast_eq_zero] at h theorem re_eq_add_conj (z : K) : ↑(re z) = (z + conj z) / 2 := begin haveI : char_zero K := char_zero_R_or_C, rw [add_conj, mul_div_cancel_left ((re z):K) two_ne_zero'], end theorem im_eq_conj_sub (z : K) : ↑(im z) = I * (conj z - z) / 2 := begin rw [← neg_inj, ← of_real_neg, ← I_mul_re, re_eq_add_conj], simp [mul_add, sub_eq_add_neg, neg_div'] end /-! ### Absolute value -/ /-- The complex absolute value function, defined as the square root of the norm squared. -/ @[pp_nodot] noncomputable def abs (z : K) : ℝ := (norm_sq z).sqrt local notation `abs'` := has_abs.abs local notation `absK` := @abs K _ @[simp, norm_cast] lemma abs_of_real (r : ℝ) : absK r = abs' r := by simp [abs, norm_sq, norm_sq_of_real, real.sqrt_mul_self_eq_abs] lemma norm_eq_abs (z : K) : βˆ₯zβˆ₯ = absK z := by simp [abs, norm_sq_eq_def'] @[norm_cast] lemma norm_of_real (z : ℝ) : βˆ₯(z : K)βˆ₯ = βˆ₯zβˆ₯ := by { rw [is_R_or_C.norm_eq_abs, is_R_or_C.abs_of_real, real.norm_eq_abs] } lemma abs_of_nonneg {r : ℝ} (h : 0 ≀ r) : absK r = r := (abs_of_real _).trans (abs_of_nonneg h) lemma abs_of_nat (n : β„•) : absK n = n := by { rw [← of_real_nat_cast], exact abs_of_nonneg (nat.cast_nonneg n) } lemma mul_self_abs (z : K) : abs z * abs z = norm_sq z := real.mul_self_sqrt (norm_sq_nonneg _) @[simp] lemma abs_zero : absK 0 = 0 := by simp [abs] @[simp] lemma abs_one : absK 1 = 1 := by simp [abs] @[simp] lemma abs_two : absK 2 = 2 := calc absK 2 = absK (2 : ℝ) : by rw [of_real_bit0, of_real_one] ... = (2 : ℝ) : abs_of_nonneg (by norm_num) lemma abs_nonneg (z : K) : 0 ≀ absK z := real.sqrt_nonneg _ @[simp] lemma abs_eq_zero {z : K} : absK z = 0 ↔ z = 0 := (real.sqrt_eq_zero $ norm_sq_nonneg _).trans norm_sq_eq_zero lemma abs_ne_zero {z : K} : abs z β‰  0 ↔ z β‰  0 := not_congr abs_eq_zero @[simp] lemma abs_conj (z : K) : abs (conj z) = abs z := by simp [abs] @[simp] lemma abs_mul (z w : K) : abs (z * w) = abs z * abs w := by rw [abs, norm_sq_mul, real.sqrt_mul (norm_sq_nonneg _)]; refl lemma abs_re_le_abs (z : K) : abs' (re z) ≀ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg (re z)) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply re_sq_le_norm_sq lemma abs_im_le_abs (z : K) : abs' (im z) ≀ abs z := by rw [mul_self_le_mul_self_iff (_root_.abs_nonneg (im z)) (abs_nonneg _), abs_mul_abs_self, mul_self_abs]; apply im_sq_le_norm_sq lemma norm_re_le_norm (z : K) : βˆ₯re zβˆ₯ ≀ βˆ₯zβˆ₯ := by { rw [is_R_or_C.norm_eq_abs, real.norm_eq_abs], exact is_R_or_C.abs_re_le_abs _, } lemma norm_im_le_norm (z : K) : βˆ₯im zβˆ₯ ≀ βˆ₯zβˆ₯ := by { rw [is_R_or_C.norm_eq_abs, real.norm_eq_abs], exact is_R_or_C.abs_im_le_abs _, } lemma re_le_abs (z : K) : re z ≀ abs z := (abs_le.1 (abs_re_le_abs _)).2 lemma im_le_abs (z : K) : im z ≀ abs z := (abs_le.1 (abs_im_le_abs _)).2 lemma im_eq_zero_of_le {a : K} (h : abs a ≀ re a) : im a = 0 := begin rw ← zero_eq_mul_self, have : re a * re a = re a * re a + im a * im a, { convert is_R_or_C.mul_self_abs a; linarith [re_le_abs a] }, linarith end lemma re_eq_self_of_le {a : K} (h : abs a ≀ re a) : (re a : K) = a := by { rw ← re_add_im a, simp [im_eq_zero_of_le h] } lemma abs_add (z w : K) : abs (z + w) ≀ abs z + abs w := (mul_self_le_mul_self_iff (abs_nonneg _) (add_nonneg (abs_nonneg _) (abs_nonneg _))).2 $ begin rw [mul_self_abs, add_mul_self_eq, mul_self_abs, mul_self_abs, add_right_comm, norm_sq_add, add_le_add_iff_left, mul_assoc, mul_le_mul_left (@zero_lt_two ℝ _ _)], simpa [-mul_re] using re_le_abs (z * conj w) end instance : is_absolute_value absK := { abv_nonneg := abs_nonneg, abv_eq_zero := Ξ» _, abs_eq_zero, abv_add := abs_add, abv_mul := abs_mul } open is_absolute_value @[simp] lemma abs_abs (z : K) : abs' (abs z) = abs z := _root_.abs_of_nonneg (abs_nonneg _) @[simp] lemma abs_pos {z : K} : 0 < abs z ↔ z β‰  0 := abv_pos abs @[simp] lemma abs_neg : βˆ€ z : K, abs (-z) = abs z := abv_neg abs lemma abs_sub : βˆ€ z w : K, abs (z - w) = abs (w - z) := abv_sub abs lemma abs_sub_le : βˆ€ a b c : K, abs (a - c) ≀ abs (a - b) + abs (b - c) := abv_sub_le abs @[simp] theorem abs_inv : βˆ€ z : K, abs z⁻¹ = (abs z)⁻¹ := abv_inv abs @[simp] theorem abs_div : βˆ€ z w : K, abs (z / w) = abs z / abs w := abv_div abs lemma abs_abs_sub_le_abs_sub : βˆ€ z w : K, abs' (abs z - abs w) ≀ abs (z - w) := abs_abv_sub_le_abv_sub abs lemma abs_re_div_abs_le_one (z : K) : abs' (re z / abs z) ≀ 1 := begin by_cases hz : z = 0, { simp [hz, zero_le_one] }, { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_re_le_abs] } end lemma abs_im_div_abs_le_one (z : K) : abs' (im z / abs z) ≀ 1 := begin by_cases hz : z = 0, { simp [hz, zero_le_one] }, { simp_rw [_root_.abs_div, abs_abs, div_le_iff (abs_pos.2 hz), one_mul, abs_im_le_abs] } end @[simp, norm_cast] lemma abs_cast_nat (n : β„•) : abs (n : K) = n := by rw [← of_real_nat_cast, abs_of_nonneg (nat.cast_nonneg n)] lemma norm_sq_eq_abs (x : K) : norm_sq x = abs x ^ 2 := by rw [abs, sq, real.mul_self_sqrt (norm_sq_nonneg _)] lemma re_eq_abs_of_mul_conj (x : K) : re (x * (conj x)) = abs (x * (conj x)) := by rw [mul_conj, of_real_re, abs_of_real, norm_sq_eq_abs, sq, _root_.abs_mul, abs_abs] lemma abs_sq_re_add_conj (x : K) : (abs (x + conj x))^2 = (re (x + conj x))^2 := by simp [sq, ←norm_sq_eq_abs, norm_sq] lemma abs_sq_re_add_conj' (x : K) : (abs (conj x + x))^2 = (re (conj x + x))^2 := by simp [sq, ←norm_sq_eq_abs, norm_sq] lemma conj_mul_eq_norm_sq_left (x : K) : conj x * x = ((norm_sq x) : K) := begin rw ext_iff, refine ⟨by simp [of_real_re, mul_re, conj_re, conj_im, norm_sq],_⟩, simp [of_real_im, mul_im, conj_im, conj_re, mul_comm], end /-! ### Cauchy sequences -/ theorem is_cau_seq_re (f : cau_seq K abs) : is_cau_seq abs' (Ξ» n, re (f n)) := Ξ» Ξ΅ Ξ΅0, (f.cauchy Ξ΅0).imp $ Ξ» i H j ij, lt_of_le_of_lt (by simpa using abs_re_le_abs (f j - f i)) (H _ ij) theorem is_cau_seq_im (f : cau_seq K abs) : is_cau_seq abs' (Ξ» n, im (f n)) := Ξ» Ξ΅ Ξ΅0, (f.cauchy Ξ΅0).imp $ Ξ» i H j ij, lt_of_le_of_lt (by simpa using abs_im_le_abs (f j - f i)) (H _ ij) /-- The real part of a K Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_re (f : cau_seq K abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_re f⟩ /-- The imaginary part of a K Cauchy sequence, as a real Cauchy sequence. -/ noncomputable def cau_seq_im (f : cau_seq K abs) : cau_seq ℝ abs' := ⟨_, is_cau_seq_im f⟩ lemma is_cau_seq_abs {f : β„• β†’ K} (hf : is_cau_seq abs f) : is_cau_seq abs' (abs ∘ f) := Ξ» Ξ΅ Ξ΅0, let ⟨i, hi⟩ := hf Ξ΅ Ξ΅0 in ⟨i, Ξ» j hj, lt_of_le_of_lt (abs_abs_sub_le_abs_sub _ _) (hi j hj)⟩ @[simp, norm_cast, priority 900] lemma of_real_prod {Ξ± : Type*} (s : finset Ξ±) (f : Ξ± β†’ ℝ) : ((∏ i in s, f i : ℝ) : K) = ∏ i in s, (f i : K) := ring_hom.map_prod _ _ _ @[simp, norm_cast, priority 900] lemma of_real_sum {Ξ± : Type*} (s : finset Ξ±) (f : Ξ± β†’ ℝ) : ((βˆ‘ i in s, f i : ℝ) : K) = βˆ‘ i in s, (f i : K) := ring_hom.map_sum _ _ _ @[simp, norm_cast] lemma of_real_finsupp_sum {Ξ± M : Type*} [has_zero M] (f : Ξ± β†’β‚€ M) (g : Ξ± β†’ M β†’ ℝ) : ((f.sum (Ξ» a b, g a b) : ℝ) : K) = f.sum (Ξ» a b, ((g a b) : K)) := ring_hom.map_finsupp_sum _ f g @[simp, norm_cast] lemma of_real_finsupp_prod {Ξ± M : Type*} [has_zero M] (f : Ξ± β†’β‚€ M) (g : Ξ± β†’ M β†’ ℝ) : ((f.prod (Ξ» a b, g a b) : ℝ) : K) = f.prod (Ξ» a b, ((g a b) : K)) := ring_hom.map_finsupp_prod _ f g end is_R_or_C namespace finite_dimensional variables {K : Type*} [is_R_or_C K] open_locale classical open is_R_or_C /-- This instance generates a type-class problem with a metavariable `?m` that should satisfy `is_R_or_C ?m`. Since this can only be satisfied by `ℝ` or `β„‚`, this does not cause problems. -/ library_note "is_R_or_C instance" /-- An `is_R_or_C` field is finite-dimensional over `ℝ`, since it is spanned by `{1, I}`. -/ @[nolint dangerous_instance] instance is_R_or_C_to_real : finite_dimensional ℝ K := ⟨⟨{1, I}, begin rw eq_top_iff, intros a _, rw [finset.coe_insert, finset.coe_singleton, submodule.mem_span_insert], refine ⟨re a, (im a) β€’ I, _, _⟩, { rw submodule.mem_span_singleton, use im a }, simp [re_add_im a, algebra.smul_def, algebra_map_eq_of_real] end⟩⟩ /-- Over an `is_R_or_C` field, we can register the properness of finite-dimensional normed spaces as an instance. -/ @[priority 900, nolint dangerous_instance] instance proper_is_R_or_C -- note [is_R_or_C instance] {E : Type*} [normed_group E] [normed_space K E] [finite_dimensional K E] : proper_space E := begin letI : normed_space ℝ E := restrict_scalars.normed_space ℝ K E, letI : is_scalar_tower ℝ K E := restrict_scalars.is_scalar_tower _ _ _, letI : finite_dimensional ℝ E := finite_dimensional.trans ℝ K E, apply_instance end end finite_dimensional section instances noncomputable instance real.is_R_or_C : is_R_or_C ℝ := { re := add_monoid_hom.id ℝ, im := 0, I := 0, I_re_ax := by simp only [add_monoid_hom.map_zero], I_mul_I_ax := or.intro_left _ rfl, re_add_im_ax := Ξ» z, by unfold_coes; simp [add_zero, id.def, mul_zero], of_real_re_ax := Ξ» r, by simp only [add_monoid_hom.id_apply, algebra.id.map_eq_self], of_real_im_ax := Ξ» r, by simp only [add_monoid_hom.zero_apply], mul_re_ax := Ξ» z w, by simp only [sub_zero, mul_zero, add_monoid_hom.zero_apply, add_monoid_hom.id_apply], mul_im_ax := Ξ» z w, by simp only [add_zero, zero_mul, mul_zero, add_monoid_hom.zero_apply], conj_re_ax := Ξ» z, by simp only [star_ring_aut_apply, star_id_of_comm], conj_im_ax := Ξ» z, by simp only [neg_zero, add_monoid_hom.zero_apply], conj_I_ax := by simp only [ring_equiv.map_zero, neg_zero], norm_sq_eq_def_ax := Ξ» z, by simp only [sq, norm, ←abs_mul, abs_mul_self z, add_zero, mul_zero, add_monoid_hom.zero_apply, add_monoid_hom.id_apply], mul_im_I_ax := Ξ» z, by simp only [mul_zero, add_monoid_hom.zero_apply], inv_def_ax := Ξ» z, by simp only [star_ring_aut_apply, star, sq, real.norm_eq_abs, abs_mul_abs_self, ←div_eq_mul_inv, algebra.id.map_eq_id, id.def, ring_hom.id_apply, div_self_mul_self'], div_I_ax := Ξ» z, by simp only [div_zero, mul_zero, neg_zero]} end instances namespace is_R_or_C open_locale complex_conjugate section cleanup_lemmas local notation `reR` := @is_R_or_C.re ℝ _ local notation `imR` := @is_R_or_C.im ℝ _ local notation `IR` := @is_R_or_C.I ℝ _ local notation `absR` := @is_R_or_C.abs ℝ _ local notation `norm_sqR` := @is_R_or_C.norm_sq ℝ _ @[simp] lemma re_to_real {x : ℝ} : reR x = x := rfl @[simp] lemma im_to_real {x : ℝ} : imR x = 0 := rfl @[simp] lemma conj_to_real {x : ℝ} : conj x = x := rfl @[simp] lemma I_to_real : IR = 0 := rfl @[simp] lemma norm_sq_to_real {x : ℝ} : norm_sq x = x*x := by simp [is_R_or_C.norm_sq] @[simp] lemma abs_to_real {x : ℝ} : absR x = has_abs.abs x := by simp [is_R_or_C.abs, abs, real.sqrt_mul_self_eq_abs] @[simp] lemma coe_real_eq_id : @coe ℝ ℝ _ = id := rfl end cleanup_lemmas section linear_maps variables {K : Type*} [is_R_or_C K] /-- The real part in a `is_R_or_C` field, as a linear map. -/ noncomputable def re_lm : K β†’β‚—[ℝ] ℝ := { map_smul' := smul_re, .. re } @[simp] lemma re_lm_coe : (re_lm : K β†’ ℝ) = re := rfl /-- The real part in a `is_R_or_C` field, as a continuous linear map. -/ noncomputable def re_clm : K β†’L[ℝ] ℝ := linear_map.mk_continuous re_lm 1 $ by { simp only [norm_eq_abs, re_lm_coe, one_mul, abs_to_real], exact abs_re_le_abs, } @[simp] lemma re_clm_norm : βˆ₯(re_clm : K β†’L[ℝ] ℝ)βˆ₯ = 1 := begin apply le_antisymm (linear_map.mk_continuous_norm_le _ zero_le_one _), convert continuous_linear_map.ratio_le_op_norm _ (1 : K), simp, end @[simp, norm_cast] lemma re_clm_coe : ((re_clm : K β†’L[ℝ] ℝ) : K β†’β‚—[ℝ] ℝ) = re_lm := rfl @[simp] lemma re_clm_apply : ((re_clm : K β†’L[ℝ] ℝ) : K β†’ ℝ) = re := rfl @[continuity] lemma continuous_re : continuous (re : K β†’ ℝ) := re_clm.continuous /-- The imaginary part in a `is_R_or_C` field, as a linear map. -/ noncomputable def im_lm : K β†’β‚—[ℝ] ℝ := { map_smul' := smul_im, .. im } @[simp] lemma im_lm_coe : (im_lm : K β†’ ℝ) = im := rfl /-- The imaginary part in a `is_R_or_C` field, as a continuous linear map. -/ noncomputable def im_clm : K β†’L[ℝ] ℝ := linear_map.mk_continuous im_lm 1 $ by { simp only [norm_eq_abs, re_lm_coe, one_mul, abs_to_real], exact abs_im_le_abs, } @[simp, norm_cast] lemma im_clm_coe : ((im_clm : K β†’L[ℝ] ℝ) : K β†’β‚—[ℝ] ℝ) = im_lm := rfl @[simp] lemma im_clm_apply : ((im_clm : K β†’L[ℝ] ℝ) : K β†’ ℝ) = im := rfl @[continuity] lemma continuous_im : continuous (im : K β†’ ℝ) := im_clm.continuous /-- Conjugate as an `ℝ`-algebra equivalence -/ noncomputable def conj_ae : K ≃ₐ[ℝ] K := { commutes' := conj_of_real, .. star_ring_aut } @[simp] lemma conj_ae_coe : (conj_ae : K β†’ K) = conj := rfl /-- Conjugate as a linear isometry -/ noncomputable def conj_lie : K ≃ₗᡒ[ℝ] K := ⟨conj_ae.to_linear_equiv, Ξ» z, by simp [norm_eq_abs]⟩ @[simp] lemma conj_lie_apply : (conj_lie : K β†’ K) = conj := rfl /-- Conjugate as a continuous linear equivalence -/ noncomputable def conj_cle : K ≃L[ℝ] K := @conj_lie K _ @[simp] lemma conj_cle_coe : (@conj_cle K _).to_linear_equiv = conj_ae.to_linear_equiv := rfl @[simp] lemma conj_cle_apply : (conj_cle : K β†’ K) = conj := rfl @[simp] lemma conj_cle_norm : βˆ₯(@conj_cle K _ : K β†’L[ℝ] K)βˆ₯ = 1 := (@conj_lie K _).to_linear_isometry.norm_to_continuous_linear_map @[continuity] lemma continuous_conj : continuous (conj : K β†’ K) := conj_lie.continuous /-- The `ℝ β†’ K` coercion, as a linear map -/ noncomputable def of_real_am : ℝ →ₐ[ℝ] K := algebra.of_id ℝ K @[simp] lemma of_real_am_coe : (of_real_am : ℝ β†’ K) = coe := rfl /-- The ℝ β†’ K coercion, as a linear isometry -/ noncomputable def of_real_li : ℝ β†’β‚—α΅’[ℝ] K := { to_linear_map := of_real_am.to_linear_map, norm_map' := by simp [norm_eq_abs] } @[simp] lemma of_real_li_apply : (of_real_li : ℝ β†’ K) = coe := rfl /-- The `ℝ β†’ K` coercion, as a continuous linear map -/ noncomputable def of_real_clm : ℝ β†’L[ℝ] K := of_real_li.to_continuous_linear_map @[simp] lemma of_real_clm_coe : ((@of_real_clm K _) : ℝ β†’β‚—[ℝ] K) = of_real_am.to_linear_map := rfl @[simp] lemma of_real_clm_apply : (of_real_clm : ℝ β†’ K) = coe := rfl @[simp] lemma of_real_clm_norm : βˆ₯(of_real_clm : ℝ β†’L[ℝ] K)βˆ₯ = 1 := linear_isometry.norm_to_continuous_linear_map of_real_li @[continuity] lemma continuous_of_real : continuous (coe : ℝ β†’ K) := of_real_li.continuous end linear_maps end is_R_or_C section normalization variables {K : Type*} [is_R_or_C K] variables {E : Type*} [normed_group E] [normed_space K E] open is_R_or_C /- Note: one might think the following lemma belongs in `analysis.normed_space.basic`. But it can't be placed there, because that file is an import of `data.complex.is_R_or_C`! -/ /-- Lemma to normalize a vector in a normed space `E` over either `β„‚` or `ℝ` to unit length. -/ @[simp] lemma norm_smul_inv_norm {x : E} (hx : x β‰  0) : βˆ₯(βˆ₯xβˆ₯⁻¹ : K) β€’ xβˆ₯ = 1 := begin have h : βˆ₯(βˆ₯xβˆ₯ : K)βˆ₯ = βˆ₯xβˆ₯, { rw norm_eq_abs, exact abs_of_nonneg (norm_nonneg _) }, have : βˆ₯xβˆ₯ β‰  0 := by simp [hx], field_simp [norm_smul, h] end end normalization
99503895cb94515c924f59330a9cb21e02290e85
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/archive/imo/imo2001_q6.lean
32619246443a22a72e2c20c464731ebad558f2d1
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,076
lean
/- Copyright (c) 2021 Sara DΓ­az Real. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sara DΓ­az Real -/ import data.int.basic import algebra.associated import tactic.linarith import tactic.ring /-! # IMO 2001 Q6 Let $a$, $b$, $c$, $d$ be integers with $a > b > c > d > 0$. Suppose that $$ a*c + b*d = (a + b - c + d) * (-a + b + c + d). $$ Prove that $a*b + c*d$ is not prime. -/ variables {a b c d : β„€} theorem imo2001_q6 (hd : 0 < d) (hdc : d < c) (hcb : c < b) (hba : b < a) (h : a*c + b*d = (a + b - c + d) * (-a + b + c + d)) : Β¬ prime (a*b + c*d) := begin assume h0 : prime (a*b + c*d), have ha : 0 < a, { linarith }, have hb : 0 < b, { linarith }, have hc : 0 < c, { linarith }, -- the key step is to show that `a*c + b*d` divides the product `(a*b + c*d) * (a*d + b*c)` have dvd_mul : a*c + b*d ∣ (a*b + c*d) * (a*d + b*c), { use b^2 + b*d + d^2, have equivalent_sums : a^2 - a*c + c^2 = b^2 + b*d + d^2, { ring_nf at h, nlinarith only [h], }, calc (a * b + c * d) * (a * d + b * c) = a*c * (b^2 + b*d + d^2) + b*d * (a^2 - a*c + c^2) : by ring ... = a*c * (b^2 + b*d + d^2) + b*d * (b^2 + b*d + d^2) : by rw equivalent_sums ... = (a * c + b * d) * (b ^ 2 + b * d + d ^ 2) : by ring, }, -- since `a*b + c*d` is prime (by assumption), it must divide `a*c + b*d` or `a*d + b*c` obtain (h1 : a*b + c*d ∣ a*c + b*d) | (h2 : a*c + b*d ∣ a*d + b*c) := left_dvd_or_dvd_right_of_dvd_prime_mul h0 dvd_mul, -- in both cases, we derive a contradiction { have aux : 0 < a*c + b*d, { nlinarith only [ha, hb, hc, hd] }, have : a*b + c*d ≀ a*c + b*d, { from int.le_of_dvd aux h1 }, have : Β¬ (a*b + c*d ≀ a*c + b*d), { nlinarith only [hba, hcb, hdc, h] }, contradiction, }, { have aux : 0 < a*d + b*c, { nlinarith only [ha, hb, hc, hd] }, have : a*c + b*d ≀ a*d + b*c, { from int.le_of_dvd aux h2 }, have : Β¬ (a*c + b*d ≀ a*d + b*c), { nlinarith only [hba, hdc, h] }, contradiction, }, end
1b5e4c64c26a2cd80e95d14cd3ecb2f1c4f591b0
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/data/vector2.lean
94f548027ebae62bbb07da01efd10e24e92f3c4d
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
10,261
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Additional theorems about the `vector` type. -/ import tactic.tauto import data.vector import data.list.nodup import data.list.of_fn universes u variables {n : β„•} namespace vector variables {Ξ± : Type*} attribute [simp] head_cons tail_cons instance [inhabited Ξ±] : inhabited (vector Ξ± n) := ⟨of_fn (Ξ» _, default Ξ±)⟩ theorem to_list_injective : function.injective (@to_list Ξ± n) := subtype.val_injective @[simp] theorem to_list_of_fn : βˆ€ {n} (f : fin n β†’ Ξ±), to_list (of_fn f) = list.of_fn f | 0 f := rfl | (n+1) f := by rw [of_fn, list.of_fn_succ, to_list_cons, to_list_of_fn] @[simp] theorem mk_to_list : βˆ€ (v : vector Ξ± n) h, (⟨to_list v, h⟩ : vector Ξ± n) = v | ⟨l, hβ‚βŸ© hβ‚‚ := rfl @[simp] lemma to_list_map {Ξ² : Type*} (v : vector Ξ± n) (f : Ξ± β†’ Ξ²) : (v.map f).to_list = v.to_list.map f := by cases v; refl theorem nth_eq_nth_le : βˆ€ (v : vector Ξ± n) (i), nth v i = v.to_list.nth_le i.1 (by rw to_list_length; exact i.2) | ⟨l, h⟩ i := rfl @[simp] lemma nth_map {Ξ² : Type*} (v : vector Ξ± n) (f : Ξ± β†’ Ξ²) (i : fin n) : (v.map f).nth i = f (v.nth i) := by simp [nth_eq_nth_le] @[simp] theorem nth_of_fn {n} (f : fin n β†’ Ξ±) (i) : nth (of_fn f) i = f i := by rw [nth_eq_nth_le, ← list.nth_le_of_fn f]; congr; apply to_list_of_fn @[simp] theorem of_fn_nth (v : vector Ξ± n) : of_fn (nth v) = v := begin rcases v with ⟨l, rfl⟩, apply to_list_injective, change nth ⟨l, eq.refl _⟩ with Ξ» i, nth ⟨l, rfl⟩ i, simp [nth, list.of_fn_nth_le] end @[simp] theorem nth_tail : βˆ€ (v : vector Ξ± n.succ) (i : fin n), nth (tail v) i = nth v i.succ | ⟨a::l, e⟩ ⟨i, h⟩ := by simp [nth_eq_nth_le]; refl @[simp] theorem tail_of_fn {n : β„•} (f : fin n.succ β†’ Ξ±) : tail (of_fn f) = of_fn (Ξ» i, f i.succ) := (of_fn_nth _).symm.trans $ by congr; funext i; simp lemma mem_iff_nth {a : Ξ±} {v : vector Ξ± n} : a ∈ v.to_list ↔ βˆƒ i, v.nth i = a := by simp only [list.mem_iff_nth_le, fin.exists_iff, vector.nth_eq_nth_le]; exact ⟨λ ⟨i, hi, h⟩, ⟨i, by rwa to_list_length at hi, h⟩, Ξ» ⟨i, hi, h⟩, ⟨i, by rwa to_list_length, h⟩⟩ lemma nodup_iff_nth_inj {v : vector Ξ± n} : v.to_list.nodup ↔ function.injective v.nth := begin cases v with l hl, subst hl, simp only [list.nodup_iff_nth_le_inj], split, { intros h i j hij, cases i, cases j, simp [nth_eq_nth_le] at *, tauto }, { intros h i j hi hj hij, have := @h ⟨i, hi⟩ ⟨j, hj⟩, simp [nth_eq_nth_le] at *, tauto } end @[simp] lemma nth_mem (i : fin n) (v : vector Ξ± n) : v.nth i ∈ v.to_list := by rw [nth_eq_nth_le]; exact list.nth_le_mem _ _ _ theorem head'_to_list : βˆ€ (v : vector Ξ± n.succ), (to_list v).head' = some (head v) | ⟨a::l, e⟩ := rfl def reverse (v : vector Ξ± n) : vector Ξ± n := ⟨v.to_list.reverse, by simp⟩ @[simp] theorem nth_zero : βˆ€ (v : vector Ξ± n.succ), nth v 0 = head v | ⟨a::l, e⟩ := rfl @[simp] theorem head_of_fn {n : β„•} (f : fin n.succ β†’ Ξ±) : head (of_fn f) = f 0 := by rw [← nth_zero, nth_of_fn] @[simp] theorem nth_cons_zero (a : Ξ±) (v : vector Ξ± n) : nth (a :: v) 0 = a := by simp [nth_zero] @[simp] theorem nth_cons_succ (a : Ξ±) (v : vector Ξ± n) (i : fin n) : nth (a :: v) i.succ = nth v i := by rw [← nth_tail, tail_cons] def m_of_fn {m} [monad m] {Ξ± : Type u} : βˆ€ {n}, (fin n β†’ m Ξ±) β†’ m (vector Ξ± n) | 0 f := pure nil | (n+1) f := do a ← f 0, v ← m_of_fn (Ξ»i, f i.succ), pure (a :: v) theorem m_of_fn_pure {m} [monad m] [is_lawful_monad m] {Ξ±} : βˆ€ {n} (f : fin n β†’ Ξ±), @m_of_fn m _ _ _ (Ξ» i, pure (f i)) = pure (of_fn f) | 0 f := rfl | (n+1) f := by simp [m_of_fn, @m_of_fn_pure n, of_fn] def mmap {m} [monad m] {Ξ±} {Ξ² : Type u} (f : Ξ± β†’ m Ξ²) : βˆ€ {n}, vector Ξ± n β†’ m (vector Ξ² n) | _ ⟨[], rfl⟩ := pure nil | _ ⟨a::l, rfl⟩ := do h' ← f a, t' ← mmap ⟨l, rfl⟩, pure (h' :: t') @[simp] theorem mmap_nil {m} [monad m] {Ξ± Ξ²} (f : Ξ± β†’ m Ξ²) : mmap f nil = pure nil := rfl @[simp] theorem mmap_cons {m} [monad m] {Ξ± Ξ²} (f : Ξ± β†’ m Ξ²) (a) : βˆ€ {n} (v : vector Ξ± n), mmap f (a::v) = do h' ← f a, t' ← mmap f v, pure (h' :: t') | _ ⟨l, rfl⟩ := rfl @[ext] theorem ext : βˆ€ {v w : vector Ξ± n} (h : βˆ€ m : fin n, vector.nth v m = vector.nth w m), v = w | ⟨v, hv⟩ ⟨w, hw⟩ h := subtype.eq (list.ext_le (by rw [hv, hw]) (Ξ» m hm hn, h ⟨m, hv β–Έ hm⟩)) instance zero_subsingleton : subsingleton (vector Ξ± 0) := ⟨λ _ _, vector.ext (Ξ» m, fin.elim0 m)⟩ def to_array : vector Ξ± n β†’ array n Ξ± | ⟨xs, h⟩ := cast (by rw h) xs.to_array section insert_nth variable {a : Ξ±} def insert_nth (a : Ξ±) (i : fin (n+1)) (v : vector Ξ± n) : vector Ξ± (n+1) := ⟨v.1.insert_nth i.1 a, begin rw [list.length_insert_nth, v.2], rw [v.2, ← nat.succ_le_succ_iff], exact i.2 end⟩ lemma insert_nth_val {i : fin (n+1)} {v : vector Ξ± n} : (v.insert_nth a i).val = v.val.insert_nth i.1 a := rfl @[simp] lemma remove_nth_val {i : fin n} : βˆ€{v : vector Ξ± n}, (remove_nth i v).val = v.val.remove_nth i.1 | ⟨l, hl⟩ := rfl lemma remove_nth_insert_nth {v : vector Ξ± n} {i : fin (n+1)} : remove_nth i (insert_nth a i v) = v := subtype.eq $ list.remove_nth_insert_nth i.1 v.1 lemma remove_nth_insert_nth_ne {v : vector Ξ± (n+1)} : βˆ€{i j : fin (n+2)} (h : i β‰  j), remove_nth i (insert_nth a j v) = insert_nth a (i.pred_above j h.symm) (remove_nth (j.pred_above i h) v) | ⟨i, hi⟩ ⟨j, hj⟩ ne := begin have : i β‰  j := fin.vne_of_ne ne, refine subtype.eq _, dsimp [insert_nth, remove_nth, fin.pred_above, fin.cast_lt], rcases lt_trichotomy i j with h | h | h, { have h_nji : Β¬ j < i := lt_asymm h, have j_pos : 0 < j := lt_of_le_of_lt (zero_le i) h, simp [h, h_nji, fin.lt_iff_val_lt_val], rw [show j.pred = j - 1, from rfl, list.insert_nth_remove_nth_of_ge, nat.sub_add_cancel j_pos], { rw [v.2], exact lt_of_lt_of_le h (nat.le_of_succ_le_succ hj) }, { exact nat.le_sub_right_of_add_le h } }, { exact (this h).elim }, { have h_nij : Β¬ i < j := lt_asymm h, have i_pos : 0 < i := lt_of_le_of_lt (zero_le j) h, simp [h, h_nij, fin.lt_iff_val_lt_val], rw [show i.pred = i - 1, from rfl, list.insert_nth_remove_nth_of_le, nat.sub_add_cancel i_pos], { show i - 1 + 1 ≀ v.val.length, rw [v.2, nat.sub_add_cancel i_pos], exact nat.le_of_lt_succ hi }, { exact nat.le_sub_right_of_add_le h } } end lemma insert_nth_comm (a b : Ξ±) (i j : fin (n+1)) (h : i ≀ j) : βˆ€(v : vector Ξ± n), (v.insert_nth a i).insert_nth b j.succ = (v.insert_nth b j).insert_nth a i.cast_succ | ⟨l, hl⟩ := begin refine subtype.eq _, simp [insert_nth_val, fin.succ_val, fin.cast_succ], apply list.insert_nth_comm, { assumption }, { rw hl, exact nat.le_of_succ_le_succ j.2 } end end insert_nth section update_nth /-- `update_nth v n a` replaces the `n`th element of `v` with `a` -/ def update_nth (v : vector Ξ± n) (i : fin n) (a : Ξ±) : vector Ξ± n := ⟨v.1.update_nth i.1 a, by rw [list.update_nth_length, v.2]⟩ @[simp] lemma nth_update_nth_same (v : vector Ξ± n) (i : fin n) (a : Ξ±) : (v.update_nth i a).nth i = a := by cases v; cases i; simp [vector.update_nth, vector.nth_eq_nth_le] lemma nth_update_nth_of_ne {v : vector Ξ± n} {i j : fin n} (h : i β‰  j) (a : Ξ±) : (v.update_nth i a).nth j = v.nth j := by cases v; cases i; cases j; simp [vector.update_nth, vector.nth_eq_nth_le, list.nth_le_update_nth_of_ne (fin.vne_of_ne h)] lemma nth_update_nth_eq_if {v : vector Ξ± n} {i j : fin n} (a : Ξ±) : (v.update_nth i a).nth j = if i = j then a else v.nth j := by split_ifs; try {simp *}; try {rw nth_update_nth_of_ne}; assumption end update_nth end vector namespace vector section traverse variables {F G : Type u β†’ Type u} variables [applicative F] [applicative G] open applicative functor open list (cons) nat private def traverse_aux {Ξ± Ξ² : Type u} (f : Ξ± β†’ F Ξ²) : Ξ  (x : list Ξ±), F (vector Ξ² x.length) | [] := pure vector.nil | (x::xs) := vector.cons <$> f x <*> traverse_aux xs protected def traverse {Ξ± Ξ² : Type u} (f : Ξ± β†’ F Ξ²) : vector Ξ± n β†’ F (vector Ξ² n) | ⟨v, Hv⟩ := cast (by rw Hv) $ traverse_aux f v variables [is_lawful_applicative F] [is_lawful_applicative G] variables {Ξ± Ξ² Ξ³ : Type u} @[simp] protected lemma traverse_def (f : Ξ± β†’ F Ξ²) (x : Ξ±) : βˆ€ (xs : vector Ξ± n), (x :: xs).traverse f = cons <$> f x <*> xs.traverse f := by rintro ⟨xs, rfl⟩; refl protected lemma id_traverse : βˆ€ (x : vector Ξ± n), x.traverse id.mk = x := begin rintro ⟨x, rfl⟩, dsimp [vector.traverse, cast], induction x with x xs IH, {refl}, simp! [IH], refl end open function protected lemma comp_traverse (f : Ξ² β†’ F Ξ³) (g : Ξ± β†’ G Ξ²) : βˆ€ (x : vector Ξ± n), vector.traverse (comp.mk ∘ functor.map f ∘ g) x = comp.mk (vector.traverse f <$> vector.traverse g x) := by rintro ⟨x, rfl⟩; dsimp [vector.traverse, cast]; induction x with x xs; simp! [cast, *] with functor_norm; [refl, simp [(∘)]] protected lemma traverse_eq_map_id {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) : βˆ€ (x : vector Ξ± n), x.traverse (id.mk ∘ f) = id.mk (map f x) := by rintro ⟨x, rfl⟩; simp!; induction x; simp! * with functor_norm; refl variable (Ξ· : applicative_transformation F G) protected lemma naturality {Ξ± Ξ² : Type*} (f : Ξ± β†’ F Ξ²) : βˆ€ (x : vector Ξ± n), Ξ· (x.traverse f) = x.traverse (@Ξ· _ ∘ f) := by rintro ⟨x, rfl⟩; simp! [cast]; induction x with x xs IH; simp! * with functor_norm end traverse instance : traversable.{u} (flip vector n) := { traverse := @vector.traverse n, map := Ξ» Ξ± Ξ², @vector.map.{u u} Ξ± Ξ² n } instance : is_lawful_traversable.{u} (flip vector n) := { id_traverse := @vector.id_traverse n, comp_traverse := @vector.comp_traverse n, traverse_eq_map_id := @vector.traverse_eq_map_id n, naturality := @vector.naturality n, id_map := by intros; cases x; simp! [(<$>)], comp_map := by intros; cases x; simp! [(<$>)] } end vector
22af2227b1bb289ce8197f553a0d4f2ecc9ccc29
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world10/level2.lean
cb29b0895717b8a1b3e6fbe990f53dc5ff30d589
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
2,003
lean
import game.world10.level1 -- hide namespace mynat -- hide /- # Inequality world. Here's a nice easy one. ## Level 2: le_refl -/ /- Lemma : The $\le$ relation is reflexive. In other words, if $x$ is a natural number, then $x\le x$. -/ lemma le_refl (x : mynat) : x ≀ x := begin [nat_num_game] end /- ## Upgrading the `refl` tactic Now with the following incantation (NB thanks to master wizard Reid Barton for correcting my spell)... -/ attribute [refl] mynat.le_refl /- ...we find that the `refl` tactic will close all goals of the form `a ≀ a` as well as all goals of the form `a = a`. -/ example : (0 : mynat) ≀ 0 := begin refl end /- ## Pro tip Did you skip `rw le_iff_exists_add` in your proof of `le_refl` above? Instead of `rw add_zero` or `ring` or `exact add_zero x` at the end there, what happens if you just try `refl`? The *definition* of `x + 0` is `x`, so you don't need to `rw add_zero` either! The proof ``` use 0, refl, ``` works. The same remarks are true of `add_succ`, `mul_zero`, `mul_succ`, `pow_zero` and `pow_succ`. All of those theorems are true *by definition*. The same is *not* true however of `zero_add`; the theorem `0 + x = x` was proved by induction on `x`, and in particular it is not true by *definition*. Definitional equality is of great importance to computer scientists, but mathematicians are much more fluid with their idea of a definition -- a concept can simultaneously have three equivalent definitions in a maths talk, as long as they're all logically equivalent. In Lean, a definition is *one thing*, and definitional equality is a subtle concept which depends on exactly which definition you chose. `add_comm` is certainly not true by definition, which means that if we had decided to define `a ≀ b` by `βˆƒ c, b = c + a` (rather than `a + c`) all the same theorems would be true, but `refl` would work in different places. `refl` closes a goal of the form `X = Y` if `X` and `Y` are definitionally equal. -/ end mynat -- hide
80ee9f5fa84c886727560e76a80a9280ca7d99fd
1a61aba1b67cddccce19532a9596efe44be4285f
/hott/types/equiv.hlean
bf06b063bdcbb24530224e64eed4e5f5b812a382
[ "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
7,164
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Ported from Coq HoTT Theorems about the types equiv and is_equiv -/ import .fiber .arrow arity ..hprop_trunc open eq is_trunc sigma sigma.ops pi fiber function equiv equiv.ops namespace is_equiv variables {A B : Type} (f : A β†’ B) [H : is_equiv f] include H /- is_equiv f is a mere proposition -/ definition is_contr_fiber_of_is_equiv [instance] (b : B) : is_contr (fiber f b) := is_contr.mk (fiber.mk (f⁻¹ b) (right_inv f b)) (Ξ»z, fiber.rec_on z (Ξ»a p, fiber_eq ((ap f⁻¹ p)⁻¹ ⬝ left_inv f a) (calc right_inv f b = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ ((ap (f ∘ f⁻¹) p) ⬝ right_inv f b) : by rewrite inv_con_cancel_left ... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ (right_inv f (f a) ⬝ p) : by rewrite ap_con_eq_con ... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ (ap f (left_inv f a) ⬝ p) : by rewrite adj ... = (ap (f ∘ f⁻¹) p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite con.assoc ... = (ap f (ap f⁻¹ p))⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite ap_compose ... = ap f (ap f⁻¹ p)⁻¹ ⬝ ap f (left_inv f a) ⬝ p : by rewrite ap_inv ... = ap f ((ap f⁻¹ p)⁻¹ ⬝ left_inv f a) ⬝ p : by rewrite ap_con))) definition is_contr_right_inverse : is_contr (Ξ£(g : B β†’ A), f ∘ g ~ id) := begin fapply is_trunc_equiv_closed, {apply sigma_equiv_sigma_id, intro g, apply eq_equiv_homotopy}, fapply is_trunc_equiv_closed, {apply fiber.sigma_char}, fapply is_contr_fiber_of_is_equiv, apply (to_is_equiv (arrow_equiv_arrow_right (equiv.mk f H))), end definition is_contr_right_coherence (u : Ξ£(g : B β†’ A), f ∘ g ~ id) : is_contr (Ξ£(Ξ· : u.1 ∘ f ~ id), Ξ (a : A), u.2 (f a) = ap f (Ξ· a)) := begin fapply is_trunc_equiv_closed, {apply equiv.symm, apply sigma_pi_equiv_pi_sigma}, fapply is_trunc_equiv_closed, {apply pi_equiv_pi_id, intro a, apply (fiber_eq_equiv (fiber.mk (u.1 (f a)) (u.2 (f a))) (fiber.mk a idp))}, end omit H protected definition sigma_char : (is_equiv f) ≃ (Ξ£(g : B β†’ A) (Ξ΅ : f ∘ g ~ id) (Ξ· : g ∘ f ~ id), Ξ (a : A), Ξ΅ (f a) = ap f (Ξ· a)) := equiv.MK (Ξ»H, ⟨inv f, right_inv f, left_inv f, adj f⟩) (Ξ»p, is_equiv.mk f p.1 p.2.1 p.2.2.1 p.2.2.2) (Ξ»p, begin induction p with p1 p2, induction p2 with p21 p22, induction p22 with p221 p222, reflexivity end) (Ξ»H, by induction H; reflexivity) protected definition sigma_char' : (is_equiv f) ≃ (Ξ£(u : Ξ£(g : B β†’ A), f ∘ g ~ id), Ξ£(Ξ· : u.1 ∘ f ~ id), Ξ (a : A), u.2 (f a) = ap f (Ξ· a)) := calc (is_equiv f) ≃ (Ξ£(g : B β†’ A) (Ξ΅ : f ∘ g ~ id) (Ξ· : g ∘ f ~ id), Ξ (a : A), Ξ΅ (f a) = ap f (Ξ· a)) : is_equiv.sigma_char ... ≃ (Ξ£(u : Ξ£(g : B β†’ A), f ∘ g ~ id), Ξ£(Ξ· : u.1 ∘ f ~ id), Ξ (a : A), u.2 (f a) = ap f (Ξ· a)) : {sigma_assoc_equiv (Ξ»u, Ξ£(Ξ· : u.1 ∘ f ~ id), Ξ (a : A), u.2 (f a) = ap f (Ξ· a))} local attribute is_contr_right_inverse [instance] [priority 1600] local attribute is_contr_right_coherence [instance] [priority 1600] theorem is_hprop_is_equiv [instance] : is_hprop (is_equiv f) := is_hprop_of_imp_is_contr (Ξ»(H : is_equiv f), is_trunc_equiv_closed -2 (equiv.symm !is_equiv.sigma_char')) definition inv_eq_inv {A B : Type} {f f' : A β†’ B} {Hf : is_equiv f} {Hf' : is_equiv f'} (p : f = f') : f⁻¹ = f'⁻¹ := apd011 inv p !is_hprop.elim /- contractible fibers -/ definition is_contr_fun [reducible] (f : A β†’ B) := Ξ (b : B), is_contr (fiber f b) definition is_contr_fun_of_is_equiv [H : is_equiv f] : is_contr_fun f := is_contr_fiber_of_is_equiv f definition is_hprop_is_contr_fun (f : A β†’ B) : is_hprop (is_contr_fun f) := _ /- we cannot make the next theorem an instance, because it loops together with is_contr_fiber_of_is_equiv -/ definition is_equiv_of_is_contr_fun [H : is_contr_fun f] : is_equiv f := adjointify _ (Ξ»b, point (center (fiber f b))) (Ξ»b, point_eq (center (fiber f b))) (Ξ»a, ap point (center_eq (fiber.mk a idp))) definition is_equiv_of_imp_is_equiv (H : B β†’ is_equiv f) : is_equiv f := @is_equiv_of_is_contr_fun _ _ f (Ξ»b, @is_contr_fiber_of_is_equiv _ _ _ (H b) _) definition is_equiv_equiv_is_contr_fun : is_equiv f ≃ is_contr_fun f := equiv_of_is_hprop _ (Ξ»H, !is_equiv_of_is_contr_fun) end is_equiv namespace equiv open is_equiv variables {A B C : Type} definition equiv_mk_eq {f f' : A β†’ B} [H : is_equiv f] [H' : is_equiv f'] (p : f = f') : equiv.mk f H = equiv.mk f' H' := apd011 equiv.mk p !is_hprop.elim definition equiv_eq {f f' : A ≃ B} (p : to_fun f = to_fun f') : f = f' := by (cases f; cases f'; apply (equiv_mk_eq p)) definition equiv_eq' {f f' : A ≃ B} (p : to_fun f ~ to_fun f') : f = f' := by apply equiv_eq;apply eq_of_homotopy p definition trans_symm (f : A ≃ B) (g : B ≃ C) : (f ⬝e g)⁻¹ᡉ = g⁻¹ᡉ ⬝e f⁻¹ᡉ :> (C ≃ A) := equiv_eq idp definition symm_symm (f : A ≃ B) : f⁻¹ᡉ⁻¹ᡉ = f :> (A ≃ B) := equiv_eq idp protected definition equiv.sigma_char [constructor] (A B : Type) : (A ≃ B) ≃ Ξ£(f : A β†’ B), is_equiv f := begin fapply equiv.MK, {intro F, exact ⟨to_fun F, to_is_equiv F⟩}, {intro p, cases p with f H, exact (equiv.mk f H)}, {intro p, cases p, exact idp}, {intro F, cases F, exact idp}, end definition equiv_eq_char (f f' : A ≃ B) : (f = f') ≃ (to_fun f = to_fun f') := calc (f = f') ≃ (to_fun !equiv.sigma_char f = to_fun !equiv.sigma_char f') : eq_equiv_fn_eq (to_fun !equiv.sigma_char) ... ≃ ((to_fun !equiv.sigma_char f).1 = (to_fun !equiv.sigma_char f').1 ) : equiv_subtype ... ≃ (to_fun f = to_fun f') : equiv.refl definition is_equiv_ap_to_fun (f f' : A ≃ B) : is_equiv (ap to_fun : f = f' β†’ to_fun f = to_fun f') := begin fapply adjointify, {intro p, cases f with f H, cases f' with f' H', cases p, apply ap (mk f'), apply is_hprop.elim}, {intro p, cases f with f H, cases f' with f' H', cases p, apply @concat _ _ (ap to_fun (ap (equiv.mk f') (is_hprop.elim H H'))), {apply idp}, generalize is_hprop.elim H H', intro q, cases q, apply idp}, {intro p, cases p, cases f with f H, apply ap (ap (equiv.mk f)), apply is_hset.elim} end definition equiv_pathover {A : Type} {a a' : A} (p : a = a') {B : A β†’ Type} {C : A β†’ Type} (f : B a ≃ C a) (g : B a' ≃ C a') (r : Ξ (b : B a) (b' : B a') (q : b =[p] b'), f b =[p] g b') : f =[p] g := begin fapply change_path_equiv', { intro a, apply equiv.sigma_char}, { fapply sigma_pathover, esimp, apply arrow_pathover, exact r, apply is_hprop.elimo} end end equiv
df56b247cce892e1b3f5b89c5b5988ac125fab8d
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/12_Axioms.org.8.lean
dc0a2f7a8cfd6e1a2422ddcce0f3edf315ff47bd
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
582
lean
import standard namespace hide -- BEGIN open setoid constant quot.{l} : Ξ  {A : Type.{l}}, setoid A β†’ Type.{l} namespace quot constant mk : Ξ  {A : Type} [s : setoid A], A β†’ quot s notation `⟦`:max a `⟧`:0 := mk a constant sound : Ξ  {A : Type} [s : setoid A] {a b : A}, a β‰ˆ b β†’ ⟦a⟧ = ⟦b⟧ constant lift : Ξ  {A B : Type} [s : setoid A] (f : A β†’ B), (βˆ€ a b, a β‰ˆ b β†’ f a = f b) β†’ quot s β†’ B constant ind : βˆ€ {A : Type} [s : setoid A] {B : quot s β†’ Prop}, (βˆ€ a, B ⟦a⟧) β†’ βˆ€ q, B q end quot -- END end hide
92ca11c420f8dd96c16ba12c78fbf5d7b1dd5bdb
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/topology/continuous_function/bounded.lean
04dda9060f6963b1c8bd3f42b4dc99bbefb170c7
[ "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
34,626
lean
/- Copyright (c) 2018 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel, Mario Carneiro, Yury Kudryashov, Heather Macbeth -/ import analysis.normed_space.basic import topology.continuous_function.algebra /-! # Bounded continuous functions The type of bounded continuous functions taking values in a metric space, with the uniform distance. -/ noncomputable theory open_locale topological_space classical nnreal open set filter metric universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} /-- The type of bounded continuous functions from a topological space to a metric space -/ structure bounded_continuous_function (Ξ± : Type u) (Ξ² : Type v) [topological_space Ξ±] [metric_space Ξ²] extends continuous_map Ξ± Ξ² : Type (max u v) := (bounded' : βˆƒC, βˆ€x y:Ξ±, dist (to_fun x) (to_fun y) ≀ C) localized "infixr ` →ᡇ `:25 := bounded_continuous_function" in bounded_continuous_function namespace bounded_continuous_function section basics variables [topological_space Ξ±] [metric_space Ξ²] [metric_space Ξ³] variables {f g : Ξ± →ᡇ Ξ²} {x : Ξ±} {C : ℝ} instance : has_coe_to_fun (Ξ± →ᡇ Ξ²) := ⟨_, Ξ» f, f.to_fun⟩ protected lemma bounded (f : Ξ± →ᡇ Ξ²) : βˆƒC, βˆ€ x y : Ξ±, dist (f x) (f y) ≀ C := f.bounded' @[continuity] protected lemma continuous (f : Ξ± →ᡇ Ξ²) : continuous f := f.to_continuous_map.continuous @[ext] lemma ext (H : βˆ€x, f x = g x) : f = g := by { cases f, cases g, congr, ext, exact H x, } lemma ext_iff : f = g ↔ βˆ€ x, f x = g x := ⟨λ h, Ξ» x, h β–Έ rfl, ext⟩ lemma bounded_range : bounded (range f) := bounded_range_iff.2 f.bounded /-- A continuous function with an explicit bound is a bounded continuous function. -/ def mk_of_bound (f : C(Ξ±, Ξ²)) (C : ℝ) (h : βˆ€ x y : Ξ±, dist (f x) (f y) ≀ C) : Ξ± →ᡇ Ξ² := ⟨f, ⟨C, h⟩⟩ @[simp] lemma mk_of_bound_coe {f} {C} {h} : (mk_of_bound f C h : Ξ± β†’ Ξ²) = (f : Ξ± β†’ Ξ²) := rfl /-- A continuous function on a compact space is automatically a bounded continuous function. -/ def mk_of_compact [compact_space Ξ±] (f : C(Ξ±, Ξ²)) : Ξ± →ᡇ Ξ² := ⟨f, bounded_range_iff.1 $ bounded_of_compact $ compact_range f.continuous⟩ @[simp] lemma mk_of_compact_apply [compact_space Ξ±] (f : C(Ξ±, Ξ²)) (a : Ξ±) : mk_of_compact f a = f a := rfl /-- If a function is bounded on a discrete space, it is automatically continuous, and therefore gives rise to an element of the type of bounded continuous functions -/ def mk_of_discrete [discrete_topology Ξ±] (f : Ξ± β†’ Ξ²) (C : ℝ) (h : βˆ€ x y : Ξ±, dist (f x) (f y) ≀ C) : Ξ± →ᡇ Ξ² := ⟨⟨f, continuous_of_discrete_topology⟩, ⟨C, h⟩⟩ @[simp] lemma mk_of_discrete_apply [discrete_topology Ξ±] (f : Ξ± β†’ Ξ²) (C) (h) (a : Ξ±) : mk_of_discrete f C h a = f a := rfl section variables (Ξ± Ξ²) /-- The map forgetting that a bounded continuous function is bounded. -/ def forget_boundedness : (Ξ± →ᡇ Ξ²) β†’ C(Ξ±, Ξ²) := Ξ» f, f.1 @[simp] lemma forget_boundedness_coe (f : Ξ± →ᡇ Ξ²) : (forget_boundedness Ξ± Ξ² f : Ξ± β†’ Ξ²) = f := rfl end /-- The uniform distance between two bounded continuous functions -/ instance : has_dist (Ξ± →ᡇ Ξ²) := ⟨λf g, Inf {C | 0 ≀ C ∧ βˆ€ x : Ξ±, dist (f x) (g x) ≀ C}⟩ lemma dist_eq : dist f g = Inf {C | 0 ≀ C ∧ βˆ€ x : Ξ±, dist (f x) (g x) ≀ C} := rfl lemma dist_set_exists : βˆƒ C, 0 ≀ C ∧ βˆ€ x : Ξ±, dist (f x) (g x) ≀ C := begin refine if h : nonempty Ξ± then _ else ⟨0, le_refl _, Ξ» x, h.elim ⟨x⟩⟩, cases h with x, rcases f.bounded with ⟨Cf, hCf : βˆ€ x y, dist (f x) (f y) ≀ Cf⟩, rcases g.bounded with ⟨Cg, hCg : βˆ€ x y, dist (g x) (g y) ≀ Cg⟩, let C := max 0 (dist (f x) (g x) + (Cf + Cg)), refine ⟨C, le_max_left _ _, Ξ» y, _⟩, calc dist (f y) (g y) ≀ dist (f x) (g x) + (dist (f x) (f y) + dist (g x) (g y)) : dist_triangle4_left _ _ _ _ ... ≀ dist (f x) (g x) + (Cf + Cg) : by mono* ... ≀ C : le_max_right _ _ end /-- The pointwise distance is controlled by the distance between functions, by definition. -/ lemma dist_coe_le_dist (x : Ξ±) : dist (f x) (g x) ≀ dist f g := le_cInf dist_set_exists $ Ξ»b hb, hb.2 x /- This lemma will be needed in the proof of the metric space instance, but it will become useless afterwards as it will be superseded by the general result that the distance is nonnegative in metric spaces. -/ private lemma dist_nonneg' : 0 ≀ dist f g := le_cInf dist_set_exists (Ξ» C, and.left) /-- The distance between two functions is controlled by the supremum of the pointwise distances -/ lemma dist_le (C0 : (0 : ℝ) ≀ C) : dist f g ≀ C ↔ βˆ€x:Ξ±, dist (f x) (g x) ≀ C := ⟨λ h x, le_trans (dist_coe_le_dist x) h, Ξ» H, cInf_le ⟨0, Ξ» C, and.left⟩ ⟨C0, H⟩⟩ lemma dist_le_iff_of_nonempty [nonempty Ξ±] : dist f g ≀ C ↔ βˆ€ x, dist (f x) (g x) ≀ C := ⟨λ h x, le_trans (dist_coe_le_dist x) h, Ξ» w, (dist_le (le_trans dist_nonneg (w (nonempty.some β€Ή_β€Ί)))).mpr w⟩ lemma dist_lt_of_nonempty_compact [nonempty Ξ±] [compact_space Ξ±] (w : βˆ€x:Ξ±, dist (f x) (g x) < C) : dist f g < C := begin have c : continuous (Ξ» x, dist (f x) (g x)), { continuity, }, obtain ⟨x, -, le⟩ := is_compact.exists_forall_ge compact_univ set.univ_nonempty (continuous.continuous_on c), exact lt_of_le_of_lt (dist_le_iff_of_nonempty.mpr (Ξ» y, le y trivial)) (w x), end lemma dist_lt_iff_of_compact [compact_space Ξ±] (C0 : (0 : ℝ) < C) : dist f g < C ↔ βˆ€x:Ξ±, dist (f x) (g x) < C := begin fsplit, { intros w x, exact lt_of_le_of_lt (dist_coe_le_dist x) w, }, { by_cases h : nonempty Ξ±, { resetI, exact dist_lt_of_nonempty_compact, }, { rintro -, convert C0, apply le_antisymm _ dist_nonneg', rw [dist_eq], exact cInf_le ⟨0, Ξ» C, and.left⟩ ⟨le_refl _, Ξ» x, false.elim (h (nonempty.intro x))⟩, }, }, end lemma dist_lt_iff_of_nonempty_compact [nonempty Ξ±] [compact_space Ξ±] : dist f g < C ↔ βˆ€x:Ξ±, dist (f x) (g x) < C := ⟨λ w x, lt_of_le_of_lt (dist_coe_le_dist x) w, dist_lt_of_nonempty_compact⟩ /-- On an empty space, bounded continuous functions are at distance 0 -/ lemma dist_zero_of_empty (e : Β¬ nonempty Ξ±) : dist f g = 0 := le_antisymm ((dist_le (le_refl _)).2 $ Ξ» x, e.elim ⟨x⟩) dist_nonneg' /-- The type of bounded continuous functions, with the uniform distance, is a metric space. -/ instance : metric_space (Ξ± →ᡇ Ξ²) := { dist_self := Ξ» f, le_antisymm ((dist_le (le_refl _)).2 $ Ξ» x, by simp) dist_nonneg', eq_of_dist_eq_zero := Ξ» f g hfg, by ext x; exact eq_of_dist_eq_zero (le_antisymm (hfg β–Έ dist_coe_le_dist _) dist_nonneg), dist_comm := Ξ» f g, by simp [dist_eq, dist_comm], dist_triangle := Ξ» f g h, (dist_le (add_nonneg dist_nonneg' dist_nonneg')).2 $ Ξ» x, le_trans (dist_triangle _ _ _) (add_le_add (dist_coe_le_dist _) (dist_coe_le_dist _)) } variables (Ξ±) {Ξ²} /-- Constant as a continuous bounded function. -/ def const (b : Ξ²) : Ξ± →ᡇ Ξ² := ⟨continuous_map.const b, 0, by simp [le_refl]⟩ variable {Ξ±} @[simp] lemma coe_const (b : Ξ²) : ⇑(const Ξ± b) = function.const Ξ± b := rfl lemma const_apply (a : Ξ±) (b : Ξ²) : (const Ξ± b : Ξ± β†’ Ξ²) a = b := rfl /-- If the target space is inhabited, so is the space of bounded continuous functions -/ instance [inhabited Ξ²] : inhabited (Ξ± →ᡇ Ξ²) := ⟨const Ξ± (default Ξ²)⟩ /-- The evaluation map is continuous, as a joint function of `u` and `x` -/ theorem continuous_eval : continuous (Ξ» p : (Ξ± →ᡇ Ξ²) Γ— Ξ±, p.1 p.2) := continuous_iff'.2 $ Ξ» ⟨f, x⟩ Ξ΅ Ξ΅0, /- use the continuity of `f` to find a neighborhood of `x` where it varies at most by Ξ΅/2 -/ have Hs : _ := continuous_iff'.1 f.continuous x (Ξ΅/2) (half_pos Ξ΅0), mem_sets_of_superset (prod_mem_nhds_sets (ball_mem_nhds _ (half_pos Ξ΅0)) Hs) $ Ξ» ⟨g, y⟩ ⟨hg, hy⟩, calc dist (g y) (f x) ≀ dist (g y) (f y) + dist (f y) (f x) : dist_triangle _ _ _ ... < Ξ΅/2 + Ξ΅/2 : add_lt_add (lt_of_le_of_lt (dist_coe_le_dist _) hg) hy ... = Ξ΅ : add_halves _ /-- In particular, when `x` is fixed, `f β†’ f x` is continuous -/ theorem continuous_evalx {x : Ξ±} : continuous (Ξ» f : Ξ± →ᡇ Ξ², f x) := continuous_eval.comp (continuous_id.prod_mk continuous_const) /-- Bounded continuous functions taking values in a complete space form a complete space. -/ instance [complete_space Ξ²] : complete_space (Ξ± →ᡇ Ξ²) := complete_of_cauchy_seq_tendsto $ Ξ» (f : β„• β†’ Ξ± →ᡇ Ξ²) (hf : cauchy_seq f), begin /- We have to show that `f n` converges to a bounded continuous function. For this, we prove pointwise convergence to define the limit, then check it is a continuous bounded function, and then check the norm convergence. -/ rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, b0, b_bound, b_lim⟩, have f_bdd := Ξ»x n m N hn hm, le_trans (dist_coe_le_dist x) (b_bound n m N hn hm), have fx_cau : βˆ€x, cauchy_seq (Ξ»n, f n x) := Ξ»x, cauchy_seq_iff_le_tendsto_0.2 ⟨b, b0, f_bdd x, b_lim⟩, choose F hF using Ξ»x, cauchy_seq_tendsto_of_complete (fx_cau x), /- F : Ξ± β†’ Ξ², hF : βˆ€ (x : Ξ±), tendsto (Ξ» (n : β„•), f n x) at_top (𝓝 (F x)) `F` is the desired limit function. Check that it is uniformly approximated by `f N` -/ have fF_bdd : βˆ€x N, dist (f N x) (F x) ≀ b N := Ξ» x N, le_of_tendsto (tendsto_const_nhds.dist (hF x)) (filter.eventually_at_top.2 ⟨N, Ξ»n hn, f_bdd x N n N (le_refl N) hn⟩), refine ⟨⟨⟨F, _⟩, _⟩, _⟩, { /- Check that `F` is continuous, as a uniform limit of continuous functions -/ have : tendsto_uniformly (Ξ»n x, f n x) F at_top, { refine metric.tendsto_uniformly_iff.2 (Ξ» Ξ΅ Ξ΅0, _), refine ((tendsto_order.1 b_lim).2 Ξ΅ Ξ΅0).mono (Ξ» n hn x, _), rw dist_comm, exact lt_of_le_of_lt (fF_bdd x n) hn }, exact this.continuous (Ξ»N, (f N).continuous) }, { /- Check that `F` is bounded -/ rcases (f 0).bounded with ⟨C, hC⟩, refine ⟨C + (b 0 + b 0), Ξ» x y, _⟩, calc dist (F x) (F y) ≀ dist (f 0 x) (f 0 y) + (dist (f 0 x) (F x) + dist (f 0 y) (F y)) : dist_triangle4_left _ _ _ _ ... ≀ C + (b 0 + b 0) : by mono* }, { /- Check that `F` is close to `f N` in distance terms -/ refine tendsto_iff_dist_tendsto_zero.2 (squeeze_zero (Ξ» _, dist_nonneg) _ b_lim), exact Ξ» N, (dist_le (b0 _)).2 (Ξ»x, fF_bdd x N) } end /-- Composition (in the target) of a bounded continuous function with a Lipschitz map again gives a bounded continuous function -/ def comp (G : Ξ² β†’ Ξ³) {C : ℝβ‰₯0} (H : lipschitz_with C G) (f : Ξ± →ᡇ Ξ²) : Ξ± →ᡇ Ξ³ := ⟨⟨λx, G (f x), H.continuous.comp f.continuous⟩, let ⟨D, hD⟩ := f.bounded in ⟨max C 0 * D, Ξ» x y, calc dist (G (f x)) (G (f y)) ≀ C * dist (f x) (f y) : H.dist_le_mul _ _ ... ≀ max C 0 * dist (f x) (f y) : mul_le_mul_of_nonneg_right (le_max_left C 0) dist_nonneg ... ≀ max C 0 * D : mul_le_mul_of_nonneg_left (hD _ _) (le_max_right C 0)⟩⟩ /-- The composition operator (in the target) with a Lipschitz map is Lipschitz -/ lemma lipschitz_comp {G : Ξ² β†’ Ξ³} {C : ℝβ‰₯0} (H : lipschitz_with C G) : lipschitz_with C (comp G H : (Ξ± →ᡇ Ξ²) β†’ Ξ± →ᡇ Ξ³) := lipschitz_with.of_dist_le_mul $ Ξ» f g, (dist_le (mul_nonneg C.2 dist_nonneg)).2 $ Ξ» x, calc dist (G (f x)) (G (g x)) ≀ C * dist (f x) (g x) : H.dist_le_mul _ _ ... ≀ C * dist f g : mul_le_mul_of_nonneg_left (dist_coe_le_dist _) C.2 /-- The composition operator (in the target) with a Lipschitz map is uniformly continuous -/ lemma uniform_continuous_comp {G : Ξ² β†’ Ξ³} {C : ℝβ‰₯0} (H : lipschitz_with C G) : uniform_continuous (comp G H : (Ξ± →ᡇ Ξ²) β†’ Ξ± →ᡇ Ξ³) := (lipschitz_comp H).uniform_continuous /-- The composition operator (in the target) with a Lipschitz map is continuous -/ lemma continuous_comp {G : Ξ² β†’ Ξ³} {C : ℝβ‰₯0} (H : lipschitz_with C G) : continuous (comp G H : (Ξ± →ᡇ Ξ²) β†’ Ξ± →ᡇ Ξ³) := (lipschitz_comp H).continuous /-- Restriction (in the target) of a bounded continuous function taking values in a subset -/ def cod_restrict (s : set Ξ²) (f : Ξ± →ᡇ Ξ²) (H : βˆ€x, f x ∈ s) : Ξ± →ᡇ s := ⟨⟨s.cod_restrict f H, continuous_subtype_mk _ f.continuous⟩, f.bounded⟩ end basics section arzela_ascoli variables [topological_space Ξ±] [compact_space Ξ±] [metric_space Ξ²] variables {f g : Ξ± →ᡇ Ξ²} {x : Ξ±} {C : ℝ} /- Arzela-Ascoli theorem asserts that, on a compact space, a set of functions sharing a common modulus of continuity and taking values in a compact set forms a compact subset for the topology of uniform convergence. In this section, we prove this theorem and several useful variations around it. -/ /-- First version, with pointwise equicontinuity and range in a compact space -/ theorem arzela_ascoli₁ [compact_space Ξ²] (A : set (Ξ± →ᡇ Ξ²)) (closed : is_closed A) (H : βˆ€ (x:Ξ±) (Ξ΅ > 0), βˆƒU ∈ 𝓝 x, βˆ€ (y z ∈ U) (f : Ξ± →ᡇ Ξ²), f ∈ A β†’ dist (f y) (f z) < Ξ΅) : is_compact A := begin refine compact_of_totally_bounded_is_closed _ closed, refine totally_bounded_of_finite_discretization (Ξ» Ξ΅ Ξ΅0, _), rcases exists_between Ξ΅0 with βŸ¨Ξ΅β‚, Ρ₁0, Ξ΅Ξ΅β‚βŸ©, let Ξ΅β‚‚ := Ρ₁/2/2, /- We have to find a finite discretization of `u`, i.e., finite information that is sufficient to reconstruct `u` up to Ξ΅. This information will be provided by the values of `u` on a sufficiently dense set tΞ±, slightly translated to fit in a finite Ξ΅β‚‚-dense set tΞ² in the image. Such sets exist by compactness of the source and range. Then, to check that these data determine the function up to Ξ΅, one uses the control on the modulus of continuity to extend the closeness on tΞ± to closeness everywhere. -/ have Ξ΅β‚‚0 : Ξ΅β‚‚ > 0 := half_pos (half_pos Ρ₁0), have : βˆ€x:Ξ±, βˆƒU, x ∈ U ∧ is_open U ∧ βˆ€ (y z ∈ U) {f : Ξ± →ᡇ Ξ²}, f ∈ A β†’ dist (f y) (f z) < Ξ΅β‚‚ := Ξ» x, let ⟨U, nhdsU, hU⟩ := H x _ Ξ΅β‚‚0, ⟨V, VU, openV, xV⟩ := mem_nhds_sets_iff.1 nhdsU in ⟨V, xV, openV, Ξ»y z hy hz f hf, hU y z (VU hy) (VU hz) f hf⟩, choose U hU using this, /- For all x, the set hU x is an open set containing x on which the elements of A fluctuate by at most Ξ΅β‚‚. We extract finitely many of these sets that cover the whole space, by compactness -/ rcases compact_univ.elim_finite_subcover_image (Ξ»x _, (hU x).2.1) (Ξ»x hx, mem_bUnion (mem_univ _) (hU x).1) with ⟨tΞ±, _, ⟨_⟩, htα⟩, /- tΞ± : set Ξ±, htΞ± : univ βŠ† ⋃x ∈ tΞ±, U x -/ rcases @finite_cover_balls_of_compact Ξ² _ _ compact_univ _ Ξ΅β‚‚0 with ⟨tΞ², _, ⟨_⟩, htβ⟩, resetI, /- tΞ² : set Ξ², htΞ² : univ βŠ† ⋃y ∈ tΞ², ball y Ξ΅β‚‚ -/ /- Associate to every point `y` in the space a nearby point `F y` in tΞ² -/ choose F hF using Ξ»y, show βˆƒz∈tΞ², dist y z < Ξ΅β‚‚, by simpa using htΞ² (mem_univ y), /- F : Ξ² β†’ Ξ², hF : βˆ€ (y : Ξ²), F y ∈ tΞ² ∧ dist y (F y) < Ξ΅β‚‚ -/ /- Associate to every function a discrete approximation, mapping each point in `tΞ±` to a point in `tΞ²` close to its true image by the function. -/ refine ⟨tΞ± β†’ tΞ², by apply_instance, Ξ» f a, ⟨F (f a), (hF (f a)).1⟩, _⟩, rintro ⟨f, hf⟩ ⟨g, hg⟩ f_eq_g, /- If two functions have the same approximation, then they are within distance Ξ΅ -/ refine lt_of_le_of_lt ((dist_le $ le_of_lt Ρ₁0).2 (Ξ» x, _)) ΡΡ₁, obtain ⟨x', x'tΞ±, hx'⟩ : βˆƒx' ∈ tΞ±, x ∈ U x' := mem_bUnion_iff.1 (htΞ± (mem_univ x)), refine calc dist (f x) (g x) ≀ dist (f x) (f x') + dist (g x) (g x') + dist (f x') (g x') : dist_triangle4_right _ _ _ _ ... ≀ Ξ΅β‚‚ + Ξ΅β‚‚ + Ρ₁/2 : le_of_lt (add_lt_add (add_lt_add _ _) _) ... = Ρ₁ : by rw [add_halves, add_halves], { exact (hU x').2.2 _ _ hx' ((hU x').1) hf }, { exact (hU x').2.2 _ _ hx' ((hU x').1) hg }, { have F_f_g : F (f x') = F (g x') := (congr_arg (Ξ» f:tΞ± β†’ tΞ², (f ⟨x', x'tα⟩ : Ξ²)) f_eq_g : _), calc dist (f x') (g x') ≀ dist (f x') (F (f x')) + dist (g x') (F (f x')) : dist_triangle_right _ _ _ ... = dist (f x') (F (f x')) + dist (g x') (F (g x')) : by rw F_f_g ... < Ξ΅β‚‚ + Ξ΅β‚‚ : add_lt_add (hF (f x')).2 (hF (g x')).2 ... = Ρ₁/2 : add_halves _ } end /-- Second version, with pointwise equicontinuity and range in a compact subset -/ theorem arzela_ascoliβ‚‚ (s : set Ξ²) (hs : is_compact s) (A : set (Ξ± →ᡇ Ξ²)) (closed : is_closed A) (in_s : βˆ€(f : Ξ± →ᡇ Ξ²) (x : Ξ±), f ∈ A β†’ f x ∈ s) (H : βˆ€(x:Ξ±) (Ξ΅ > 0), βˆƒU ∈ 𝓝 x, βˆ€ (y z ∈ U) (f : Ξ± →ᡇ Ξ²), f ∈ A β†’ dist (f y) (f z) < Ξ΅) : is_compact A := /- This version is deduced from the previous one by restricting to the compact type in the target, using compactness there and then lifting everything to the original space. -/ begin have M : lipschitz_with 1 coe := lipschitz_with.subtype_coe s, let F : (Ξ± →ᡇ s) β†’ Ξ± →ᡇ Ξ² := comp coe M, refine compact_of_is_closed_subset ((_ : is_compact (F ⁻¹' A)).image (continuous_comp M)) closed (Ξ» f hf, _), { haveI : compact_space s := compact_iff_compact_space.1 hs, refine arzela_ascoli₁ _ (continuous_iff_is_closed.1 (continuous_comp M) _ closed) (Ξ» x Ξ΅ Ξ΅0, bex.imp_right (Ξ» U U_nhds hU y z hy hz f hf, _) (H x Ξ΅ Ξ΅0)), calc dist (f y) (f z) = dist (F f y) (F f z) : rfl ... < Ξ΅ : hU y z hy hz (F f) hf }, { let g := cod_restrict s f (Ξ»x, in_s f x hf), rw [show f = F g, by ext; refl] at hf ⊒, exact ⟨g, hf, rfl⟩ } end /-- Third (main) version, with pointwise equicontinuity and range in a compact subset, but without closedness. The closure is then compact -/ theorem arzela_ascoli (s : set Ξ²) (hs : is_compact s) (A : set (Ξ± →ᡇ Ξ²)) (in_s : βˆ€(f : Ξ± →ᡇ Ξ²) (x : Ξ±), f ∈ A β†’ f x ∈ s) (H : βˆ€(x:Ξ±) (Ξ΅ > 0), βˆƒU ∈ 𝓝 x, βˆ€ (y z ∈ U) (f : Ξ± →ᡇ Ξ²), f ∈ A β†’ dist (f y) (f z) < Ξ΅) : is_compact (closure A) := /- This version is deduced from the previous one by checking that the closure of A, in addition to being closed, still satisfies the properties of compact range and equicontinuity -/ arzela_ascoliβ‚‚ s hs (closure A) is_closed_closure (Ξ» f x hf, (mem_of_closed' hs.is_closed).2 $ Ξ» Ξ΅ Ξ΅0, let ⟨g, gA, dist_fg⟩ := metric.mem_closure_iff.1 hf Ξ΅ Ξ΅0 in ⟨g x, in_s g x gA, lt_of_le_of_lt (dist_coe_le_dist _) dist_fg⟩) (Ξ» x Ξ΅ Ξ΅0, show βˆƒ U ∈ 𝓝 x, βˆ€ y z ∈ U, βˆ€ (f : Ξ± →ᡇ Ξ²), f ∈ closure A β†’ dist (f y) (f z) < Ξ΅, begin refine bex.imp_right (Ξ» U U_set hU y z hy hz f hf, _) (H x (Ξ΅/2) (half_pos Ξ΅0)), rcases metric.mem_closure_iff.1 hf (Ξ΅/2/2) (half_pos (half_pos Ξ΅0)) with ⟨g, gA, dist_fg⟩, replace dist_fg := Ξ» x, lt_of_le_of_lt (dist_coe_le_dist x) dist_fg, calc dist (f y) (f z) ≀ dist (f y) (g y) + dist (f z) (g z) + dist (g y) (g z) : dist_triangle4_right _ _ _ _ ... < Ξ΅/2/2 + Ξ΅/2/2 + Ξ΅/2 : add_lt_add (add_lt_add (dist_fg y) (dist_fg z)) (hU y z hy hz g gA) ... = Ξ΅ : by rw [add_halves, add_halves] end) /- To apply the previous theorems, one needs to check the equicontinuity. An important instance is when the source space is a metric space, and there is a fixed modulus of continuity for all the functions in the set A -/ lemma equicontinuous_of_continuity_modulus {Ξ± : Type u} [metric_space Ξ±] (b : ℝ β†’ ℝ) (b_lim : tendsto b (𝓝 0) (𝓝 0)) (A : set (Ξ± →ᡇ Ξ²)) (H : βˆ€(x y:Ξ±) (f : Ξ± →ᡇ Ξ²), f ∈ A β†’ dist (f x) (f y) ≀ b (dist x y)) (x:Ξ±) (Ξ΅ : ℝ) (Ξ΅0 : 0 < Ξ΅) : βˆƒU ∈ 𝓝 x, βˆ€ (y z ∈ U) (f : Ξ± →ᡇ Ξ²), f ∈ A β†’ dist (f y) (f z) < Ξ΅ := begin rcases tendsto_nhds_nhds.1 b_lim Ξ΅ Ξ΅0 with ⟨δ, Ξ΄0, hδ⟩, refine ⟨ball x (Ξ΄/2), ball_mem_nhds x (half_pos Ξ΄0), Ξ» y z hy hz f hf, _⟩, have : dist y z < Ξ΄ := calc dist y z ≀ dist y x + dist z x : dist_triangle_right _ _ _ ... < Ξ΄/2 + Ξ΄/2 : add_lt_add hy hz ... = Ξ΄ : add_halves _, calc dist (f y) (f z) ≀ b (dist y z) : H y z f hf ... ≀ abs (b (dist y z)) : le_abs_self _ ... = dist (b (dist y z)) 0 : by simp [real.dist_eq] ... < Ξ΅ : hΞ΄ (by simpa [real.dist_eq] using this), end end arzela_ascoli section normed_group /- In this section, if Ξ² is a normed group, then we show that the space of bounded continuous functions from Ξ± to Ξ² inherits a normed group structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space Ξ±] [normed_group Ξ²] variables (f g : Ξ± →ᡇ Ξ²) {x : Ξ±} {C : ℝ} instance : has_zero (Ξ± →ᡇ Ξ²) := ⟨const Ξ± 0⟩ @[simp] lemma coe_zero : ((0 : Ξ± →ᡇ Ξ²) : Ξ± β†’ Ξ²) = 0 := rfl instance : has_norm (Ξ± →ᡇ Ξ²) := ⟨λu, dist u 0⟩ lemma norm_def : βˆ₯fβˆ₯ = dist f 0 := rfl /-- The norm of a bounded continuous function is the supremum of `βˆ₯f xβˆ₯`. We use `Inf` to ensure that the definition works if `Ξ±` has no elements. -/ lemma norm_eq (f : Ξ± →ᡇ Ξ²) : βˆ₯fβˆ₯ = Inf {C : ℝ | 0 ≀ C ∧ βˆ€ (x : Ξ±), βˆ₯f xβˆ₯ ≀ C} := by simp [norm_def, bounded_continuous_function.dist_eq] lemma norm_coe_le_norm (x : Ξ±) : βˆ₯f xβˆ₯ ≀ βˆ₯fβˆ₯ := calc βˆ₯f xβˆ₯ = dist (f x) ((0 : Ξ± →ᡇ Ξ²) x) : by simp [dist_zero_right] ... ≀ βˆ₯fβˆ₯ : dist_coe_le_dist _ lemma dist_le_two_norm' {f : Ξ³ β†’ Ξ²} {C : ℝ} (hC : βˆ€ x, βˆ₯f xβˆ₯ ≀ C) (x y : Ξ³) : dist (f x) (f y) ≀ 2 * C := calc dist (f x) (f y) ≀ βˆ₯f xβˆ₯ + βˆ₯f yβˆ₯ : dist_le_norm_add_norm _ _ ... ≀ C + C : add_le_add (hC x) (hC y) ... = 2 * C : (two_mul _).symm /-- Distance between the images of any two points is at most twice the norm of the function. -/ lemma dist_le_two_norm (x y : Ξ±) : dist (f x) (f y) ≀ 2 * βˆ₯fβˆ₯ := dist_le_two_norm' f.norm_coe_le_norm x y variable {f} /-- The norm of a function is controlled by the supremum of the pointwise norms -/ lemma norm_le (C0 : (0 : ℝ) ≀ C) : βˆ₯fβˆ₯ ≀ C ↔ βˆ€x:Ξ±, βˆ₯f xβˆ₯ ≀ C := by simpa using @dist_le _ _ _ _ f 0 _ C0 lemma norm_le_of_nonempty [nonempty Ξ±] {f : Ξ± →ᡇ Ξ²} {M : ℝ} : βˆ₯fβˆ₯ ≀ M ↔ βˆ€ x, βˆ₯f xβˆ₯ ≀ M := begin simp_rw [norm_def, ←dist_zero_right], exact dist_le_iff_of_nonempty, end lemma norm_lt_iff_of_compact [compact_space Ξ±] {f : Ξ± →ᡇ Ξ²} {M : ℝ} (M0 : 0 < M) : βˆ₯fβˆ₯ < M ↔ βˆ€ x, βˆ₯f xβˆ₯ < M := begin simp_rw [norm_def, ←dist_zero_right], exact dist_lt_iff_of_compact M0, end lemma norm_lt_iff_of_nonempty_compact [nonempty Ξ±] [compact_space Ξ±] {f : Ξ± →ᡇ Ξ²} {M : ℝ} : βˆ₯fβˆ₯ < M ↔ βˆ€ x, βˆ₯f xβˆ₯ < M := begin simp_rw [norm_def, ←dist_zero_right], exact dist_lt_iff_of_nonempty_compact, end variable (f) /-- Norm of `const Ξ± b` is less than or equal to `βˆ₯bβˆ₯`. If `Ξ±` is nonempty, then it is equal to `βˆ₯bβˆ₯`. -/ lemma norm_const_le (b : Ξ²) : βˆ₯const Ξ± bβˆ₯ ≀ βˆ₯bβˆ₯ := (norm_le (norm_nonneg b)).2 $ Ξ» x, le_refl _ @[simp] lemma norm_const_eq [h : nonempty Ξ±] (b : Ξ²) : βˆ₯const Ξ± bβˆ₯ = βˆ₯bβˆ₯ := le_antisymm (norm_const_le b) $ h.elim $ Ξ» x, (const Ξ± b).norm_coe_le_norm x /-- Constructing a bounded continuous function from a uniformly bounded continuous function taking values in a normed group. -/ def of_normed_group {Ξ± : Type u} {Ξ² : Type v} [topological_space Ξ±] [normed_group Ξ²] (f : Ξ± β†’ Ξ²) (Hf : continuous f) (C : ℝ) (H : βˆ€x, βˆ₯f xβˆ₯ ≀ C) : Ξ± →ᡇ Ξ² := ⟨⟨λn, f n, Hf⟩, ⟨_, dist_le_two_norm' H⟩⟩ lemma norm_of_normed_group_le {f : Ξ± β†’ Ξ²} (hfc : continuous f) {C : ℝ} (hC : 0 ≀ C) (hfC : βˆ€ x, βˆ₯f xβˆ₯ ≀ C) : βˆ₯of_normed_group f hfc C hfCβˆ₯ ≀ C := (norm_le hC).2 hfC /-- Constructing a bounded continuous function from a uniformly bounded function on a discrete space, taking values in a normed group -/ def of_normed_group_discrete {Ξ± : Type u} {Ξ² : Type v} [topological_space Ξ±] [discrete_topology Ξ±] [normed_group Ξ²] (f : Ξ± β†’ Ξ²) (C : ℝ) (H : βˆ€x, norm (f x) ≀ C) : Ξ± →ᡇ Ξ² := of_normed_group f continuous_of_discrete_topology C H /-- The pointwise sum of two bounded continuous functions is again bounded continuous. -/ instance : has_add (Ξ± →ᡇ Ξ²) := ⟨λf g, of_normed_group (f + g) (f.continuous.add g.continuous) (βˆ₯fβˆ₯ + βˆ₯gβˆ₯) $ Ξ» x, le_trans (norm_add_le _ _) (add_le_add (f.norm_coe_le_norm x) (g.norm_coe_le_norm x))⟩ /-- The pointwise opposite of a bounded continuous function is again bounded continuous. -/ instance : has_neg (Ξ± →ᡇ Ξ²) := ⟨λf, of_normed_group (-f) f.continuous.neg βˆ₯fβˆ₯ $ Ξ» x, trans_rel_right _ (norm_neg _) (f.norm_coe_le_norm x)⟩ /-- The pointwise difference of two bounded continuous functions is again bounded continuous. -/ instance : has_sub (Ξ± →ᡇ Ξ²) := ⟨λf g, of_normed_group (f - g) (f.continuous.sub g.continuous) (βˆ₯fβˆ₯ + βˆ₯gβˆ₯) $ Ξ» x, by { simp only [sub_eq_add_neg], exact le_trans (norm_add_le _ _) (add_le_add (f.norm_coe_le_norm x) $ trans_rel_right _ (norm_neg _) (g.norm_coe_le_norm x)) }⟩ @[simp] lemma coe_add : ⇑(f + g) = f + g := rfl lemma add_apply : (f + g) x = f x + g x := rfl @[simp] lemma coe_neg : ⇑(-f) = -f := rfl lemma neg_apply : (-f) x = -f x := rfl lemma forall_coe_zero_iff_zero : (βˆ€x, f x = 0) ↔ f = 0 := (@ext_iff _ _ _ _ f 0).symm instance : add_comm_group (Ξ± →ᡇ Ξ²) := { add_assoc := assume f g h, by ext; simp [add_assoc], zero_add := assume f, by ext; simp, add_zero := assume f, by ext; simp, add_left_neg := assume f, by ext; simp, add_comm := assume f g, by ext; simp [add_comm], sub_eq_add_neg := assume f g, by { ext, apply sub_eq_add_neg }, ..bounded_continuous_function.has_add, ..bounded_continuous_function.has_neg, ..bounded_continuous_function.has_sub, ..bounded_continuous_function.has_zero } @[simp] lemma coe_sub : ⇑(f - g) = f - g := rfl lemma sub_apply : (f - g) x = f x - g x := rfl /-- Coercion of a `normed_group_hom` is an `add_monoid_hom`. Similar to `add_monoid_hom.coe_fn` -/ @[simps] def coe_fn_add_hom : (Ξ± →ᡇ Ξ²) β†’+ (Ξ± β†’ Ξ²) := { to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add} open_locale big_operators @[simp] lemma coe_sum {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ (Ξ± →ᡇ Ξ²)) : ⇑(βˆ‘ i in s, f i) = (βˆ‘ i in s, (f i : Ξ± β†’ Ξ²)) := (@coe_fn_add_hom Ξ± Ξ² _ _).map_sum f s lemma sum_apply {ΞΉ : Type*} (s : finset ΞΉ) (f : ΞΉ β†’ (Ξ± →ᡇ Ξ²)) (a : Ξ±) : (βˆ‘ i in s, f i) a = (βˆ‘ i in s, f i a) := by simp instance : normed_group (Ξ± →ᡇ Ξ²) := { dist_eq := Ξ» f g, by simp only [norm_eq, dist_eq, dist_eq_norm, sub_apply] } lemma abs_diff_coe_le_dist : βˆ₯f x - g xβˆ₯ ≀ dist f g := by { rw dist_eq_norm, exact (f - g).norm_coe_le_norm x } lemma coe_le_coe_add_dist {f g : Ξ± →ᡇ ℝ} : f x ≀ g x + dist f g := sub_le_iff_le_add'.1 $ (abs_le.1 $ @dist_coe_le_dist _ _ _ _ f g x).2 variables (Ξ± Ξ²) /-- The additive map forgetting that a bounded continuous function is bounded. -/ @[simps] def forget_boundedness_add_hom : (Ξ± →ᡇ Ξ²) β†’+ C(Ξ±, Ξ²) := { to_fun := forget_boundedness Ξ± Ξ², map_zero' := by { ext, simp, }, map_add' := by { intros, ext, simp, }, } end normed_group section normed_space /-! ### Normed space structure In this section, if `Ξ²` is a normed space, then we show that the space of bounded continuous functions from `Ξ±` to `Ξ²` inherits a normed space structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {π•œ : Type*} [normed_field π•œ] variables [topological_space Ξ±] [normed_group Ξ²] [normed_space π•œ Ξ²] variables {f g : Ξ± →ᡇ Ξ²} {x : Ξ±} {C : ℝ} instance : has_scalar π•œ (Ξ± →ᡇ Ξ²) := ⟨λ c f, of_normed_group (c β€’ f) (f.continuous.const_smul c) (βˆ₯cβˆ₯ * βˆ₯fβˆ₯) $ Ξ» x, trans_rel_right _ (norm_smul _ _) (mul_le_mul_of_nonneg_left (f.norm_coe_le_norm _) (norm_nonneg _))⟩ @[simp] lemma coe_smul (c : π•œ) (f : Ξ± →ᡇ Ξ²) : ⇑(c β€’ f) = Ξ» x, c β€’ (f x) := rfl lemma smul_apply (c : π•œ) (f : Ξ± →ᡇ Ξ²) (x : Ξ±) : (c β€’ f) x = c β€’ f x := rfl instance : module π•œ (Ξ± →ᡇ Ξ²) := module.of_core $ { smul := (β€’), smul_add := Ξ» c f g, ext $ Ξ» x, smul_add c (f x) (g x), add_smul := Ξ» c₁ cβ‚‚ f, ext $ Ξ» x, add_smul c₁ cβ‚‚ (f x), mul_smul := Ξ» c₁ cβ‚‚ f, ext $ Ξ» x, mul_smul c₁ cβ‚‚ (f x), one_smul := Ξ» f, ext $ Ξ» x, one_smul π•œ (f x) } instance : normed_space π•œ (Ξ± →ᡇ Ξ²) := ⟨λ c f, norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _⟩ variables (Ξ± Ξ²) /-- The linear map forgetting that a bounded continuous function is bounded. -/ @[simps] def forget_boundedness_linear_map : (Ξ± →ᡇ Ξ²) β†’β‚—[π•œ] C(Ξ±, Ξ²) := { to_fun := forget_boundedness Ξ± Ξ², map_smul' := by { intros, ext, simp, }, map_add' := by { intros, ext, simp, }, } end normed_space section normed_ring /-! ### Normed ring structure In this section, if `R` is a normed ring, then we show that the space of bounded continuous functions from `Ξ±` to `R` inherits a normed ring structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space Ξ±] {R : Type*} [normed_ring R] instance : ring (Ξ± →ᡇ R) := { one := const Ξ± 1, mul := Ξ» f g, of_normed_group (f * g) (f.continuous.mul g.continuous) (βˆ₯fβˆ₯ * βˆ₯gβˆ₯) $ Ξ» x, le_trans (normed_ring.norm_mul (f x) (g x)) $ mul_le_mul (f.norm_coe_le_norm x) (g.norm_coe_le_norm x) (norm_nonneg _) (norm_nonneg _), one_mul := Ξ» f, ext $ Ξ» x, one_mul (f x), mul_one := Ξ» f, ext $ Ξ» x, mul_one (f x), mul_assoc := Ξ» f₁ fβ‚‚ f₃, ext $ Ξ» x, mul_assoc _ _ _, left_distrib := Ξ» f₁ fβ‚‚ f₃, ext $ Ξ» x, left_distrib _ _ _, right_distrib := Ξ» f₁ fβ‚‚ f₃, ext $ Ξ» x, right_distrib _ _ _, .. bounded_continuous_function.add_comm_group } @[simp] lemma coe_mul (f g : Ξ± →ᡇ R) : ⇑(f * g) = f * g := rfl lemma mul_apply (f g : Ξ± →ᡇ R) (x : Ξ±) : (f * g) x = f x * g x := rfl instance : normed_ring (Ξ± →ᡇ R) := { norm_mul := Ξ» f g, norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _, .. bounded_continuous_function.normed_group } end normed_ring section normed_comm_ring /-! ### Normed commutative ring structure In this section, if `R` is a normed commutative ring, then we show that the space of bounded continuous functions from `Ξ±` to `R` inherits a normed commutative ring structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables [topological_space Ξ±] {R : Type*} [normed_comm_ring R] instance : comm_ring (Ξ± →ᡇ R) := { mul_comm := Ξ» f₁ fβ‚‚, ext $ Ξ» x, mul_comm _ _, .. bounded_continuous_function.ring } instance : normed_comm_ring (Ξ± →ᡇ R) := { .. bounded_continuous_function.comm_ring, .. bounded_continuous_function.normed_group } end normed_comm_ring section normed_algebra /-! ### Normed algebra structure In this section, if `Ξ³` is a normed algebra, then we show that the space of bounded continuous functions from `Ξ±` to `Ξ³` inherits a normed algebra structure, by using pointwise operations and checking that they are compatible with the uniform distance. -/ variables {π•œ : Type*} [normed_field π•œ] variables [topological_space Ξ±] [normed_group Ξ²] [normed_space π•œ Ξ²] variables [normed_ring Ξ³] [normed_algebra π•œ Ξ³] variables {f g : Ξ± →ᡇ Ξ³} {x : Ξ±} {c : π•œ} /-- `bounded_continuous_function.const` as a `ring_hom`. -/ def C : π•œ β†’+* (Ξ± →ᡇ Ξ³) := { to_fun := Ξ» (c : π•œ), const Ξ± ((algebra_map π•œ Ξ³) c), map_one' := ext $ Ξ» x, (algebra_map π•œ Ξ³).map_one, map_mul' := Ξ» c₁ cβ‚‚, ext $ Ξ» x, (algebra_map π•œ Ξ³).map_mul _ _, map_zero' := ext $ Ξ» x, (algebra_map π•œ Ξ³).map_zero, map_add' := Ξ» c₁ cβ‚‚, ext $ Ξ» x, (algebra_map π•œ Ξ³).map_add _ _ } instance : algebra π•œ (Ξ± →ᡇ Ξ³) := { to_ring_hom := C, commutes' := Ξ» c f, ext $ Ξ» x, algebra.commutes' _ _, smul_def' := Ξ» c f, ext $ Ξ» x, algebra.smul_def' _ _, ..bounded_continuous_function.module, ..bounded_continuous_function.ring } @[simp] lemma algebra_map_apply (k : π•œ) (a : Ξ±) : algebra_map π•œ (Ξ± →ᡇ Ξ³) k a = k β€’ 1 := by { rw algebra.algebra_map_eq_smul_one, refl, } instance [nonempty Ξ±] : normed_algebra π•œ (Ξ± →ᡇ Ξ³) := { norm_algebra_map_eq := Ξ» c, begin calc βˆ₯ (algebra_map π•œ (Ξ± →ᡇ Ξ³)).to_fun cβˆ₯ = βˆ₯(algebra_map π•œ Ξ³) cβˆ₯ : _ ... = βˆ₯cβˆ₯ : norm_algebra_map_eq _ _, apply norm_const_eq ((algebra_map π•œ Ξ³) c), assumption, end, ..bounded_continuous_function.algebra } /-! ### Structure as normed module over scalar functions If `Ξ²` is a normed `π•œ`-space, then we show that the space of bounded continuous functions from `Ξ±` to `Ξ²` is naturally a module over the algebra of bounded continuous functions from `Ξ±` to `π•œ`. -/ instance has_scalar' : has_scalar (Ξ± →ᡇ π•œ) (Ξ± →ᡇ Ξ²) := ⟨λ (f : Ξ± →ᡇ π•œ) (g : Ξ± →ᡇ Ξ²), of_normed_group (Ξ» x, (f x) β€’ (g x)) (f.continuous.smul g.continuous) (βˆ₯fβˆ₯ * βˆ₯gβˆ₯) (Ξ» x, calc βˆ₯f x β€’ g xβˆ₯ ≀ βˆ₯f xβˆ₯ * βˆ₯g xβˆ₯ : normed_space.norm_smul_le _ _ ... ≀ βˆ₯fβˆ₯ * βˆ₯gβˆ₯ : mul_le_mul (f.norm_coe_le_norm _) (g.norm_coe_le_norm _) (norm_nonneg _) (norm_nonneg _)) ⟩ instance module' : module (Ξ± →ᡇ π•œ) (Ξ± →ᡇ Ξ²) := module.of_core $ { smul := (β€’), smul_add := Ξ» c f₁ fβ‚‚, ext $ Ξ» x, smul_add _ _ _, add_smul := Ξ» c₁ cβ‚‚ f, ext $ Ξ» x, add_smul _ _ _, mul_smul := Ξ» c₁ cβ‚‚ f, ext $ Ξ» x, mul_smul _ _ _, one_smul := Ξ» f, ext $ Ξ» x, one_smul π•œ (f x) } lemma norm_smul_le (f : Ξ± →ᡇ π•œ) (g : Ξ± →ᡇ Ξ²) : βˆ₯f β€’ gβˆ₯ ≀ βˆ₯fβˆ₯ * βˆ₯gβˆ₯ := norm_of_normed_group_le _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) _ /- TODO: When `normed_module` has been added to `normed_space.basic`, the above facts show that the space of bounded continuous functions from `Ξ±` to `Ξ²` is naturally a normed module over the algebra of bounded continuous functions from `Ξ±` to `π•œ`. -/ end normed_algebra end bounded_continuous_function
a77da0fe7940320cc712f04f5135f4710442f02d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/calculus/lhopital_auto.lean
cee30f14cf7f418672a81791d27eda3cc1121d9c
[]
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
17,353
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.mean_value import Mathlib.PostPort namespace Mathlib /-! # L'HΓ΄pital's rule for 0/0 indeterminate forms In this file, we prove several forms of "L'Hopital's rule" for computing 0/0 indeterminate forms. The proof of `has_deriv_at.lhopital_zero_right_on_Ioo` is based on the one given in the corresponding [Wikibooks](https://en.wikibooks.org/wiki/Calculus/L%27H%C3%B4pital%27s_Rule) chapter, and all other statements are derived from this one by composing by carefully chosen functions. Note that the filter `f'/g'` tends to isn't required to be one of `𝓝 a`, `at_top` or `at_bot`. In fact, we give a slightly stronger statement by allowing it to be any filter on `ℝ`. Each statement is available in a `has_deriv_at` form and a `deriv` form, which is denoted by each statement being in either the `has_deriv_at` or the `deriv` namespace. -/ /-! ## Interval-based versions We start by proving statements where all conditions (derivability, `g' β‰  0`) have to be satisfied on an explicitely-provided interval. -/ namespace has_deriv_at theorem lhopital_zero_right_on_Ioo {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at g (g' x) x) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ g' x β‰  0) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry theorem lhopital_zero_right_on_Ico {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at g (g' x) x) (hcf : continuous_on f (set.Ico a b)) (hcg : continuous_on g (set.Ico a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ g' x β‰  0) (hfa : f a = 0) (hga : g a = 0) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry theorem lhopital_zero_left_on_Ioo {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at g (g' x) x) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ g' x β‰  0) (hfb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds 0)) (hgb : filter.tendsto g (nhds_within b (set.Iio b)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within b (set.Iio b)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within b (set.Iio b)) l := sorry theorem lhopital_zero_left_on_Ioc {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ has_deriv_at g (g' x) x) (hcf : continuous_on f (set.Ioc a b)) (hcg : continuous_on g (set.Ioc a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ g' x β‰  0) (hfb : f b = 0) (hgb : g b = 0) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within b (set.Iio b)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within b (set.Iio b)) l := sorry theorem lhopital_zero_at_top_on_Ioi {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Ioi a β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Ioi a β†’ has_deriv_at g (g' x) x) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioi a β†’ g' x β‰  0) (hftop : filter.tendsto f filter.at_top (nhds 0)) (hgtop : filter.tendsto g filter.at_top (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) filter.at_top l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_top l := sorry theorem lhopital_zero_at_bot_on_Iio {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : βˆ€ (x : ℝ), x ∈ set.Iio a β†’ has_deriv_at f (f' x) x) (hgg' : βˆ€ (x : ℝ), x ∈ set.Iio a β†’ has_deriv_at g (g' x) x) (hg' : βˆ€ (x : ℝ), x ∈ set.Iio a β†’ g' x β‰  0) (hfbot : filter.tendsto f filter.at_bot (nhds 0)) (hgbot : filter.tendsto g filter.at_bot (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) filter.at_bot l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_bot l := sorry end has_deriv_at namespace deriv theorem lhopital_zero_right_on_Ioo {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Ioo a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ deriv g x β‰  0) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry theorem lhopital_zero_right_on_Ico {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Ioo a b)) (hcf : continuous_on f (set.Ico a b)) (hcg : continuous_on g (set.Ico a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ deriv g x β‰  0) (hfa : f a = 0) (hga : g a = 0) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry theorem lhopital_zero_left_on_Ioo {a : ℝ} {b : ℝ} (hab : a < b) {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Ioo a b)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioo a b β†’ deriv g x β‰  0) (hfb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds 0)) (hgb : filter.tendsto g (nhds_within b (set.Iio b)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within b (set.Iio b)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within b (set.Iio b)) l := sorry theorem lhopital_zero_at_top_on_Ioi {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Ioi a)) (hg' : βˆ€ (x : ℝ), x ∈ set.Ioi a β†’ deriv g x β‰  0) (hftop : filter.tendsto f filter.at_top (nhds 0)) (hgtop : filter.tendsto g filter.at_top (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) filter.at_top l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_top l := sorry theorem lhopital_zero_at_bot_on_Iio {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : differentiable_on ℝ f (set.Iio a)) (hg' : βˆ€ (x : ℝ), x ∈ set.Iio a β†’ deriv g x β‰  0) (hfbot : filter.tendsto f filter.at_bot (nhds 0)) (hgbot : filter.tendsto g filter.at_bot (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) filter.at_bot l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_bot l := sorry end deriv /-! ## Generic versions The following statements no longer any explicit interval, as they only require conditions holding eventually. -/ namespace has_deriv_at /-- L'HΓ΄pital's rule for approaching a real from the right, `has_deriv_at` version -/ theorem lhopital_zero_nhds_right {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) (nhds_within a (set.Ioi a))) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) (nhds_within a (set.Ioi a))) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) (nhds_within a (set.Ioi a))) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real from the left, `has_deriv_at` version -/ theorem lhopital_zero_nhds_left {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) (nhds_within a (set.Iio a))) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) (nhds_within a (set.Iio a))) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) (nhds_within a (set.Iio a))) (hfa : filter.tendsto f (nhds_within a (set.Iio a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Iio a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.Iio a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Iio a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real, `has_deriv_at` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhds' {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) (nhds_within a (set.univ \ singleton a))) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) (nhds_within a (set.univ \ singleton a))) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) (nhds_within a (set.univ \ singleton a))) (hfa : filter.tendsto f (nhds_within a (set.univ \ singleton a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.univ \ singleton a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds_within a (set.univ \ singleton a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.univ \ singleton a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real, `has_deriv_at` version -/ theorem lhopital_zero_nhds {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) (nhds a)) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) (nhds a)) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) (nhds a)) (hfa : filter.tendsto f (nhds a) (nhds 0)) (hga : filter.tendsto g (nhds a) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) (nhds a) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.univ \ singleton a)) l := lhopital_zero_nhds' (eventually_nhds_within_of_eventually_nhds hff') (eventually_nhds_within_of_eventually_nhds hgg') (eventually_nhds_within_of_eventually_nhds hg') (tendsto_nhds_within_of_tendsto_nhds hfa) (tendsto_nhds_within_of_tendsto_nhds hga) (tendsto_nhds_within_of_tendsto_nhds hdiv) /-- L'HΓ΄pital's rule for approaching +∞, `has_deriv_at` version -/ theorem lhopital_zero_at_top {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) filter.at_top) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) filter.at_top) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) filter.at_top) (hftop : filter.tendsto f filter.at_top (nhds 0)) (hgtop : filter.tendsto g filter.at_top (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) filter.at_top l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_top l := sorry /-- L'HΓ΄pital's rule for approaching -∞, `has_deriv_at` version -/ theorem lhopital_zero_at_bot {l : filter ℝ} {f : ℝ β†’ ℝ} {f' : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} {g' : ℝ β†’ ℝ} (hff' : filter.eventually (fun (x : ℝ) => has_deriv_at f (f' x) x) filter.at_bot) (hgg' : filter.eventually (fun (x : ℝ) => has_deriv_at g (g' x) x) filter.at_bot) (hg' : filter.eventually (fun (x : ℝ) => g' x β‰  0) filter.at_bot) (hfbot : filter.tendsto f filter.at_bot (nhds 0)) (hgbot : filter.tendsto g filter.at_bot (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => f' x / g' x) filter.at_bot l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_bot l := sorry end has_deriv_at namespace deriv /-- L'HΓ΄pital's rule for approaching a real from the right, `deriv` version -/ theorem lhopital_zero_nhds_right {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) (nhds_within a (set.Ioi a))) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) (nhds_within a (set.Ioi a))) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.Ioi a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Ioi a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real from the left, `deriv` version -/ theorem lhopital_zero_nhds_left {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) (nhds_within a (set.Iio a))) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) (nhds_within a (set.Iio a))) (hfa : filter.tendsto f (nhds_within a (set.Iio a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.Iio a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.Iio a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.Iio a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real, `deriv` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhds' {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) (nhds_within a (set.univ \ singleton a))) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) (nhds_within a (set.univ \ singleton a))) (hfa : filter.tendsto f (nhds_within a (set.univ \ singleton a)) (nhds 0)) (hga : filter.tendsto g (nhds_within a (set.univ \ singleton a)) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds_within a (set.univ \ singleton a)) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.univ \ singleton a)) l := sorry /-- L'HΓ΄pital's rule for approaching a real, `deriv` version -/ theorem lhopital_zero_nhds {a : ℝ} {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) (nhds a)) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) (nhds a)) (hfa : filter.tendsto f (nhds a) (nhds 0)) (hga : filter.tendsto g (nhds a) (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) (nhds a) l) : filter.tendsto (fun (x : ℝ) => f x / g x) (nhds_within a (set.univ \ singleton a)) l := lhopital_zero_nhds' (eventually_nhds_within_of_eventually_nhds hdf) (eventually_nhds_within_of_eventually_nhds hg') (tendsto_nhds_within_of_tendsto_nhds hfa) (tendsto_nhds_within_of_tendsto_nhds hga) (tendsto_nhds_within_of_tendsto_nhds hdiv) /-- L'HΓ΄pital's rule for approaching +∞, `deriv` version -/ theorem lhopital_zero_at_top {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) filter.at_top) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) filter.at_top) (hftop : filter.tendsto f filter.at_top (nhds 0)) (hgtop : filter.tendsto g filter.at_top (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) filter.at_top l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_top l := sorry /-- L'HΓ΄pital's rule for approaching -∞, `deriv` version -/ theorem lhopital_zero_at_bot {l : filter ℝ} {f : ℝ β†’ ℝ} {g : ℝ β†’ ℝ} (hdf : filter.eventually (fun (x : ℝ) => differentiable_at ℝ f x) filter.at_bot) (hg' : filter.eventually (fun (x : ℝ) => deriv g x β‰  0) filter.at_bot) (hfbot : filter.tendsto f filter.at_bot (nhds 0)) (hgbot : filter.tendsto g filter.at_bot (nhds 0)) (hdiv : filter.tendsto (fun (x : ℝ) => deriv f x / deriv g x) filter.at_bot l) : filter.tendsto (fun (x : ℝ) => f x / g x) filter.at_bot l := sorry end Mathlib
8c18a93253c527369164f2929903595ab71032d4
d5b53bc87e7f4dda87570c8ef6ee4b4de685f315
/src/G_module/hom.lean
c8c3be09cc2e5ad6a019276e0942e4a54c06e1e1
[]
no_license
Shenyang1995/M4R
3bec366fba7262ed29d7f64b4ba7cc978494c022
a6a3399c4d1935b39a22f64c30f293ef2a32fdeb
refs/heads/master
1,597,008,096,640
1,591,722,931,000
1,591,722,931,000
214,177,424
5
0
null
null
null
null
UTF-8
Lean
false
false
1,472
lean
import G_module.basic variables (G : Type*) [group G] variables (M : Type*) [add_comm_group M] [G_module G M] variables (N : Type*) [add_comm_group N] [G_module G N] structure G_module_hom := (f : M β†’+ N) (smul : βˆ€ g : G, βˆ€ m : M, f (g β€’ m) = g β€’ (f m)) instance : has_coe (G_module_hom G M N) (add_group_hom M N) := ⟨G_module_hom.f⟩ notation M ` β†’[`:25 G:25 `] `:0 N:0 := G_module_hom G M N namespace G_module_hom variables {G} {M} {N} def zero : M β†’[G] N := sorry def id : M β†’[G] M := sorry variables {P : Type*} [add_comm_group P] [G_module G P] def comp (Ξ± : M β†’[G] N) (Ξ² : N β†’[G] P) : M β†’[G] P := sorry lemma id_comp {Ξ± : M β†’[G] N} : comp id Ξ± = Ξ± := sorry lemma comp_id {Ξ± : M β†’[G] N} : comp Ξ± id = Ξ± := sorry variables {Q : Type*} [add_comm_group Q] [G_module G Q] lemma comp_assoc {Ξ± : M β†’[G] N} {Ξ² : N β†’[G] P} {Ξ³ : P β†’[G] Q} : comp (comp Ξ± Ξ²) Ξ³ = comp Ξ± (comp Ξ² Ξ³) := sorry -- could do zero_comp, comp_zero variable (Ξ± : M β†’[G] N) example : ⇑α = Ξ±.f.to_fun := rfl lemma map_add {m n : M} : (Ξ± : M β†’ N) (m + n) = (Ξ± : M β†’ N) m + (Ξ± : M β†’ N) n := add_monoid_hom.map_add (Ξ±.f) m n lemma map_smul {g : G} {m : M} : (Ξ± : M β†’ N) (g β€’ m) = g β€’ ((Ξ± : M β†’ N) m) := G_module_hom.smul Ξ± g m lemma map_sum (I : Type*) (Ο† : I β†’ M) (s : finset I) : Ξ± (finset.sum s Ο†) = finset.sum s (Ξ» (x : I), Ξ± (Ο† x)) := add_monoid_hom.map_sum Ξ±.f Ο† s end G_module_hom
310f3c026460e263101a7e17daec5ab4df40fd3e
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/algebra/continuous_monoid_hom.lean
4150f256e0c8b1928e376a47af03a456af47b048
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
11,468
lean
/- Copyright (c) 2022 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import analysis.complex.circle import topology.continuous_function.algebra /-! # Continuous Monoid Homs This file defines the space of continuous homomorphisms between two topological groups. ## Main definitions * `continuous_monoid_hom A B`: The continuous homomorphisms `A β†’* B`. * `continuous_add_monoid_hom A B`: The continuous additive homomorphisms `A β†’+ B`. -/ open_locale pointwise open function variables (F A B C D E : Type*) [monoid A] [monoid B] [monoid C] [monoid D] [comm_group E] [topological_space A] [topological_space B] [topological_space C] [topological_space D] [topological_space E] [topological_group E] /-- The type of continuous additive monoid homomorphisms from `A` to `B`. When possible, instead of parametrizing results over `(f : continuous_add_monoid_hom A B)`, you should parametrize over `(F : Type*) [continuous_add_monoid_hom_class F A B] (f : F)`. When you extend this structure, make sure to extend `continuous_add_monoid_hom_class`. -/ structure continuous_add_monoid_hom (A B : Type*) [add_monoid A] [add_monoid B] [topological_space A] [topological_space B] extends A β†’+ B := (continuous_to_fun : continuous to_fun) /-- The type of continuous monoid homomorphisms from `A` to `B`. When possible, instead of parametrizing results over `(f : continuous_monoid_hom A B)`, you should parametrize over `(F : Type*) [continuous_monoid_hom_class F A B] (f : F)`. When you extend this structure, make sure to extend `continuous_add_monoid_hom_class`. -/ @[to_additive] structure continuous_monoid_hom extends A β†’* B := (continuous_to_fun : continuous to_fun) /-- `continuous_add_monoid_hom_class F A B` states that `F` is a type of continuous additive monoid homomorphisms. You should also extend this typeclass when you extend `continuous_add_monoid_hom`. -/ class continuous_add_monoid_hom_class (A B : Type*) [add_monoid A] [add_monoid B] [topological_space A] [topological_space B] extends add_monoid_hom_class F A B := (map_continuous (f : F) : continuous f) /-- `continuous_monoid_hom_class F A B` states that `F` is a type of continuous additive monoid homomorphisms. You should also extend this typeclass when you extend `continuous_monoid_hom`. -/ @[to_additive] class continuous_monoid_hom_class extends monoid_hom_class F A B := (map_continuous (f : F) : continuous f) /-- Reinterpret a `continuous_monoid_hom` as a `monoid_hom`. -/ add_decl_doc continuous_monoid_hom.to_monoid_hom /-- Reinterpret a `continuous_add_monoid_hom` as an `add_monoid_hom`. -/ add_decl_doc continuous_add_monoid_hom.to_add_monoid_hom @[priority 100, to_additive] -- See note [lower instance priority] instance continuous_monoid_hom_class.to_continuous_map_class [continuous_monoid_hom_class F A B] : continuous_map_class F A B := { .. β€Ήcontinuous_monoid_hom_class F A Bβ€Ί } namespace continuous_monoid_hom variables {A B C D E} @[to_additive] instance : continuous_monoid_hom_class (continuous_monoid_hom A B) A B := { coe := Ξ» f, f.to_fun, coe_injective' := Ξ» f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_mul := Ξ» f, f.map_mul', map_one := Ξ» f, f.map_one', map_continuous := Ξ» f, f.continuous_to_fun } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ @[to_additive "Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly."] instance : has_coe_to_fun (continuous_monoid_hom A B) (Ξ» _, A β†’ B) := fun_like.has_coe_to_fun @[to_additive] lemma ext {f g : continuous_monoid_hom A B} (h : βˆ€ x, f x = g x) : f = g := fun_like.ext _ _ h /-- Reinterpret a `continuous_monoid_hom` as a `continuous_map`. -/ @[to_additive "Reinterpret a `continuous_add_monoid_hom` as a `continuous_map`."] def to_continuous_map (f : continuous_monoid_hom A B) : C(A, B) := { .. f} @[to_additive] lemma to_continuous_map_injective : injective (to_continuous_map : _ β†’ C(A, B)) := Ξ» f g h, ext $ by convert fun_like.ext_iff.1 h /-- Construct a `continuous_monoid_hom` from a `continuous` `monoid_hom`. -/ @[to_additive "Construct a `continuous_add_monoid_hom` from a `continuous` `add_monoid_hom`.", simps] def mk' (f : A β†’* B) (hf : continuous f) : continuous_monoid_hom A B := { continuous_to_fun := hf, .. f } /-- Composition of two continuous homomorphisms. -/ @[to_additive "Composition of two continuous homomorphisms.", simps] def comp (g : continuous_monoid_hom B C) (f : continuous_monoid_hom A B) : continuous_monoid_hom A C := mk' (g.to_monoid_hom.comp f.to_monoid_hom) (g.continuous_to_fun.comp f.continuous_to_fun) /-- Product of two continuous homomorphisms on the same space. -/ @[to_additive "Product of two continuous homomorphisms on the same space.", simps] def prod (f : continuous_monoid_hom A B) (g : continuous_monoid_hom A C) : continuous_monoid_hom A (B Γ— C) := mk' (f.to_monoid_hom.prod g.to_monoid_hom) (f.continuous_to_fun.prod_mk g.continuous_to_fun) /-- Product of two continuous homomorphisms on different spaces. -/ @[to_additive "Product of two continuous homomorphisms on different spaces.", simps] def prod_map (f : continuous_monoid_hom A C) (g : continuous_monoid_hom B D) : continuous_monoid_hom (A Γ— B) (C Γ— D) := mk' (f.to_monoid_hom.prod_map g.to_monoid_hom) (f.continuous_to_fun.prod_map g.continuous_to_fun) variables (A B C D E) /-- The trivial continuous homomorphism. -/ @[to_additive "The trivial continuous homomorphism.", simps] def one : continuous_monoid_hom A B := mk' 1 continuous_const @[to_additive] instance : inhabited (continuous_monoid_hom A B) := ⟨one A B⟩ /-- The identity continuous homomorphism. -/ @[to_additive "The identity continuous homomorphism.", simps] def id : continuous_monoid_hom A A := mk' (monoid_hom.id A) continuous_id /-- The continuous homomorphism given by projection onto the first factor. -/ @[to_additive "The continuous homomorphism given by projection onto the first factor.", simps] def fst : continuous_monoid_hom (A Γ— B) A := mk' (monoid_hom.fst A B) continuous_fst /-- The continuous homomorphism given by projection onto the second factor. -/ @[to_additive "The continuous homomorphism given by projection onto the second factor.", simps] def snd : continuous_monoid_hom (A Γ— B) B := mk' (monoid_hom.snd A B) continuous_snd /-- The continuous homomorphism given by inclusion of the first factor. -/ @[to_additive "The continuous homomorphism given by inclusion of the first factor.", simps] def inl : continuous_monoid_hom A (A Γ— B) := prod (id A) (one A B) /-- The continuous homomorphism given by inclusion of the second factor. -/ @[to_additive "The continuous homomorphism given by inclusion of the second factor.", simps] def inr : continuous_monoid_hom B (A Γ— B) := prod (one B A) (id B) /-- The continuous homomorphism given by the diagonal embedding. -/ @[to_additive "The continuous homomorphism given by the diagonal embedding.", simps] def diag : continuous_monoid_hom A (A Γ— A) := prod (id A) (id A) /-- The continuous homomorphism given by swapping components. -/ @[to_additive "The continuous homomorphism given by swapping components.", simps] def swap : continuous_monoid_hom (A Γ— B) (B Γ— A) := prod (snd A B) (fst A B) /-- The continuous homomorphism given by multiplication. -/ @[to_additive "The continuous homomorphism given by addition.", simps] def mul : continuous_monoid_hom (E Γ— E) E := mk' mul_monoid_hom continuous_mul /-- The continuous homomorphism given by inversion. -/ @[to_additive "The continuous homomorphism given by negation.", simps] def inv : continuous_monoid_hom E E := mk' inv_monoid_hom continuous_inv variables {A B C D E} /-- Coproduct of two continuous homomorphisms to the same space. -/ @[to_additive "Coproduct of two continuous homomorphisms to the same space.", simps] def coprod (f : continuous_monoid_hom A E) (g : continuous_monoid_hom B E) : continuous_monoid_hom (A Γ— B) E := (mul E).comp (f.prod_map g) @[to_additive] instance : comm_group (continuous_monoid_hom A E) := { mul := Ξ» f g, (mul E).comp (f.prod g), mul_comm := Ξ» f g, ext (Ξ» x, mul_comm (f x) (g x)), mul_assoc := Ξ» f g h, ext (Ξ» x, mul_assoc (f x) (g x) (h x)), one := one A E, one_mul := Ξ» f, ext (Ξ» x, one_mul (f x)), mul_one := Ξ» f, ext (Ξ» x, mul_one (f x)), inv := Ξ» f, (inv E).comp f, mul_left_inv := Ξ» f, ext (Ξ» x, mul_left_inv (f x)) } instance : topological_space (continuous_monoid_hom A B) := topological_space.induced to_continuous_map continuous_map.compact_open variables (A B C D E) lemma is_inducing : inducing (to_continuous_map : continuous_monoid_hom A B β†’ C(A, B)) := ⟨rfl⟩ lemma is_embedding : embedding (to_continuous_map : continuous_monoid_hom A B β†’ C(A, B)) := ⟨is_inducing A B, to_continuous_map_injective⟩ lemma is_closed_embedding [has_continuous_mul B] [t2_space B] : closed_embedding (to_continuous_map : continuous_monoid_hom A B β†’ C(A, B)) := ⟨is_embedding A B, ⟨begin suffices : (set.range (to_continuous_map : continuous_monoid_hom A B β†’ C(A, B))) = ({f | f '' {1} βŠ† {1}ᢜ} βˆͺ ⋃ (x y) (U V W) (hU : is_open U) (hV : is_open V) (hW : is_open W) (h : disjoint (U * V) W), {f | f '' {x} βŠ† U} ∩ {f | f '' {y} βŠ† V} ∩ {f | f '' {x * y} βŠ† W})ᢜ, { rw [this, compl_compl], refine (continuous_map.is_open_gen is_compact_singleton is_open_compl_singleton).union _, repeat { apply is_open_Union, intro, }, repeat { apply is_open.inter }, all_goals { apply continuous_map.is_open_gen is_compact_singleton, assumption } }, simp_rw [set.compl_union, set.compl_Union, set.image_singleton, set.singleton_subset_iff, set.ext_iff, set.mem_inter_iff, set.mem_Inter, set.mem_compl_iff], refine Ξ» f, ⟨_, _⟩, { rintros ⟨f, rfl⟩, exact ⟨λ h, h (map_one f), Ξ» x y U V W hU hV hW h ⟨⟨hfU, hfV⟩, hfW⟩, h ⟨set.mul_mem_mul hfU hfV, (congr_arg (∈ W) (map_mul f x y)).mp hfW⟩⟩ }, { rintros ⟨hf1, hf2⟩, suffices : βˆ€ x y, f (x * y) = f x * f y, { refine ⟨({ map_one' := of_not_not hf1, map_mul' := this, .. f } : continuous_monoid_hom A B), continuous_map.ext (Ξ» _, rfl)⟩, }, intros x y, contrapose! hf2, obtain ⟨UV, W, hUV, hW, hfUV, hfW, h⟩ := t2_separation hf2.symm, have hB := @continuous_mul B _ _ _, obtain ⟨U, V, hU, hV, hfU, hfV, h'⟩ := is_open_prod_iff.mp (hUV.preimage hB) (f x) (f y) hfUV, refine ⟨x, y, U, V, W, hU, hV, hW, h.mono_left _, ⟨hfU, hfV⟩, hfW⟩, rintros _ ⟨x, y, hx : (x, y).1 ∈ U, hy : (x, y).2 ∈ V, rfl⟩, exact h' ⟨hx, hy⟩ }, end⟩⟩ variables {A B C D E} instance [t2_space B] : t2_space (continuous_monoid_hom A B) := (is_embedding A B).t2_space instance : topological_group (continuous_monoid_hom A E) := let hi := is_inducing A E, hc := hi.continuous in { continuous_mul := hi.continuous_iff.mpr (continuous_mul.comp (continuous.prod_map hc hc)), continuous_inv := hi.continuous_iff.mpr (continuous_inv.comp hc) } end continuous_monoid_hom /-- The Pontryagin dual of `A` is the group of continuous homomorphism `A β†’ circle`. -/ @[derive [topological_space, t2_space, comm_group, topological_group, inhabited]] def pontryagin_dual := continuous_monoid_hom A circle
fbd4bf27e09585fb327e9e84e393e71cb0a45021
41ebf3cb010344adfa84907b3304db00e02db0a6
/uexp/src/uexp/rules/removeSemiJoinWithFilter.lean
457d601fc01f64907d4d39df2a7c39370db7a4f5
[ "BSD-2-Clause" ]
permissive
ReinierKoops/Cosette
e061b2ba58b26f4eddf4cd052dcf7abd16dfe8fb
eb8dadd06ee05fe7b6b99de431dd7c4faef5cb29
refs/heads/master
1,686,483,953,198
1,624,293,498,000
1,624,293,498,000
378,997,885
0
0
BSD-2-Clause
1,624,293,485,000
1,624,293,484,000
null
UTF-8
Lean
false
false
2,139
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..meta.ucongr import ..meta.TDP set_option profiler true open Expr open Proj open Pred open SQL open tree notation `int` := datatypes.int constant str_foo_: const int. theorem rule: forall ( Ξ“ scm_t scm_account scm_bonus scm_dept scm_emp: Schema) (rel_t: relation scm_t) (rel_account: relation scm_account) (rel_bonus: relation scm_bonus) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (t_k0 : Column int scm_t) (t_c1 : Column int scm_t) (t_f1_a0 : Column int scm_t) (t_f2_a0 : Column int scm_t) (t_f0_c0 : Column int scm_t) (t_f1_c0 : Column int scm_t) (t_f0_c1 : Column int scm_t) (t_f1_c2 : Column int scm_t) (t_f2_c3 : Column int scm_t) (account_acctno : Column int scm_account) (account_type : Column int scm_account) (account_balance : Column int scm_account) (bonus_ename : Column int scm_bonus) (bonus_job : Column int scm_bonus) (bonus_sal : Column int scm_bonus) (bonus_comm : Column int scm_bonus) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp), denoteSQL ((SELECT1 (rightβ‹…leftβ‹…emp_ename) (FROM1 (product (table rel_emp) (table rel_dept)) WHERE (and (equal (uvariable (rightβ‹…leftβ‹…emp_deptno)) (uvariable (rightβ‹…rightβ‹…dept_deptno))) (equal (uvariable (rightβ‹…leftβ‹…emp_ename)) (constantExpr str_foo_))))) : SQL Ξ“ _ ) = denoteSQL ((SELECT1 (rightβ‹…leftβ‹…emp_ename) (FROM1 (product ((SELECT * FROM1 (table rel_emp) WHERE (equal (uvariable (rightβ‹…emp_ename)) (constantExpr str_foo_)))) (table rel_dept)) WHERE (equal (uvariable (rightβ‹…leftβ‹…emp_deptno)) (uvariable (rightβ‹…rightβ‹…dept_deptno))))) : SQL Ξ“ _ ) := begin intros, unfold_all_denotations, funext, print_size, simp, print_size, TDP' ucongr, end
9ef57f175bcb825fedef7e710124c451c5747817
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/category_theory/punit.lean
89a152badbae30c0bb289987e0831c9e6539cc1f
[ "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
1,729
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.const import category_theory.discrete_category universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory namespace functor variables (C : Type u) [category.{v} C] /-- The constant functor sending everything to `punit.star`. -/ def star : C β₯€ discrete punit := (functor.const _).obj punit.star variable {C} /-- Any two functors to `discrete punit` are isomorphic. -/ def punit_ext (F G : C β₯€ discrete punit) : F β‰… G := nat_iso.of_components (Ξ» _, eq_to_iso dec_trivial) (Ξ» _ _ _, dec_trivial) /-- Any two functors to `discrete punit` are *equal*. You probably want to use `punit_ext` instead of this. -/ lemma punit_ext' (F G : C β₯€ discrete punit) : F = G := functor.ext (Ξ» _, dec_trivial) (Ξ» _ _ _, dec_trivial) /-- The functor from `discrete punit` sending everything to the given object. -/ abbreviation from_punit (X : C) : discrete punit β₯€ C := (functor.const _).obj X /-- Functors from `discrete punit` are equivalent to the category itself. -/ @[simps] def equiv : (discrete punit β₯€ C) β‰Œ C := { functor := { obj := Ξ» F, F.obj punit.star, map := Ξ» F G ΞΈ, ΞΈ.app punit.star }, inverse := functor.const _, unit_iso := begin apply nat_iso.of_components _ _, intro X, apply discrete.nat_iso, rintro ⟨⟩, apply iso.refl _, intros, ext ⟨⟩, simp, end, counit_iso := begin refine nat_iso.of_components iso.refl _, intros X Y f, dsimp, simp, end } end functor end category_theory
2d5755ca3b973b6a130d011358543651b2e03d9c
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/infix_paren.lean
7af261f9c5e124b3b5104e539f7d31560ee0c396
[ "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
526
lean
open list #eval filter (< 10) [20, 5, 10, 3, 2, 14, 1] #eval qsort (Ξ» x y, x < y) [20, 5, 10, 3, 2, 14, 1] #eval foldl (+) 0 [1, 2, 3] example : foldl (+) 0 [3, 4, 1] = 8 := rfl example : foldl (*) 2 [3, 4, 1] = 24 := rfl #check (+) 1 2 example : (+) 1 2 = 3 := rfl example : (*) 3 4 = 12 := rfl example : (++) [1,2] [3,4] = [1,2,3,4] := rfl example : (++) [1,2] [3,4] = [1,2] ++ [3,4] := rfl /- (-) is rejected since we have prefix notation for - -/ example : list.foldr (::) [] [1, 2, 3, 4] = [1, 2, 3, 4] := rfl
92b7cc27f741bc9721e7c54b274f744f0b4d1473
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/nat/cast.lean
128730a2fd86e7063f9579098bd9a8fc280a90b0
[ "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
12,594
lean
/- Copyright (c) 2014 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.order.field import data.nat.basic /-! # Cast of naturals This file defines the *canonical* homomorphism from the natural numbers into a type `Ξ±` with `0`, `1` and `+` (typically an `add_monoid` with one). ## Main declarations * `cast`: Canonical homomorphism `β„• β†’ Ξ±` where `Ξ±` has a `0`, `1` and `+`. * `bin_cast`: Binary representation version of `cast`. * `cast_add_monoid_hom`: `cast` bundled as an `add_monoid_hom`. * `cast_ring_hom`: `cast` bundled as a `ring_hom`. ## Implementation note Setting up the coercions priorities is tricky. See Note [coercion into rings]. -/ namespace nat variables {Ξ± : Type*} section variables [has_zero Ξ±] [has_one Ξ±] [has_add Ξ±] /-- Canonical homomorphism from `β„•` to a type `Ξ±` with `0`, `1` and `+`. -/ protected def cast : β„• β†’ Ξ± | 0 := 0 | (n+1) := cast n + 1 /-- Computationally friendlier cast than `nat.cast`, using binary representation. -/ protected def bin_cast (n : β„•) : Ξ± := @nat.binary_rec (Ξ» _, Ξ±) 0 (Ξ» odd k a, cond odd (a + a + 1) (a + a)) n /-- Coercions such as `nat.cast_coe` that go from a concrete structure such as `β„•` to an arbitrary ring `Ξ±` should be set up as follows: ```lean @[priority 900] instance : has_coe_t β„• Ξ± := ⟨...⟩ ``` It needs to be `has_coe_t` instead of `has_coe` because otherwise type-class inference would loop when constructing the transitive coercion `β„• β†’ β„• β†’ β„• β†’ ...`. The reduced priority is necessary so that it doesn't conflict with instances such as `has_coe_t Ξ± (option Ξ±)`. For this to work, we reduce the priority of the `coe_base` and `coe_trans` instances because we want the instances for `has_coe_t` to be tried in the following order: 1. `has_coe_t` instances declared in mathlib (such as `has_coe_t Ξ± (with_top Ξ±)`, etc.) 2. `coe_base`, which contains instances such as `has_coe (fin n) n` 3. `nat.cast_coe : has_coe_t β„• Ξ±` etc. 4. `coe_trans` If `coe_trans` is tried first, then `nat.cast_coe` doesn't get a chance to apply. -/ library_note "coercion into rings" attribute [instance, priority 950] coe_base attribute [instance, priority 500] coe_trans -- see note [coercion into rings] @[priority 900] instance cast_coe : has_coe_t β„• Ξ± := ⟨nat.cast⟩ @[simp, norm_cast] theorem cast_zero : ((0 : β„•) : Ξ±) = 0 := rfl theorem cast_add_one (n : β„•) : ((n + 1 : β„•) : Ξ±) = n + 1 := rfl @[simp, norm_cast, priority 500] theorem cast_succ (n : β„•) : ((succ n : β„•) : Ξ±) = n + 1 := rfl @[simp, norm_cast] theorem cast_ite (P : Prop) [decidable P] (m n : β„•) : (((ite P m n) : β„•) : Ξ±) = ite P (m : Ξ±) (n : Ξ±) := by { split_ifs; refl, } end @[simp, norm_cast] theorem cast_one [add_monoid Ξ±] [has_one Ξ±] : ((1 : β„•) : Ξ±) = 1 := zero_add _ @[simp, norm_cast] theorem cast_add [add_monoid Ξ±] [has_one Ξ±] (m) : βˆ€ n, ((m + n : β„•) : Ξ±) = m + n | 0 := (add_zero _).symm | (n+1) := show ((m + n : β„•) : Ξ±) + 1 = m + (n + 1), by rw [cast_add n, add_assoc] @[simp] lemma bin_cast_eq [add_monoid Ξ±] [has_one Ξ±] (n : β„•) : (nat.bin_cast n : Ξ±) = ((n : β„•) : Ξ±) := begin rw nat.bin_cast, apply binary_rec _ _ n, { rw [binary_rec_zero, cast_zero] }, { intros b k h, rw [binary_rec_eq, h], { cases b; simp [bit, bit0, bit1] }, { simp } }, end /-- `coe : β„• β†’ Ξ±` as an `add_monoid_hom`. -/ def cast_add_monoid_hom (Ξ± : Type*) [add_monoid Ξ±] [has_one Ξ±] : β„• β†’+ Ξ± := { to_fun := coe, map_add' := cast_add, map_zero' := cast_zero } @[simp] lemma coe_cast_add_monoid_hom [add_monoid Ξ±] [has_one Ξ±] : (cast_add_monoid_hom Ξ± : β„• β†’ Ξ±) = coe := rfl @[simp, norm_cast] theorem cast_bit0 [add_monoid Ξ±] [has_one Ξ±] (n : β„•) : ((bit0 n : β„•) : Ξ±) = bit0 n := cast_add _ _ @[simp, norm_cast] theorem cast_bit1 [add_monoid Ξ±] [has_one Ξ±] (n : β„•) : ((bit1 n : β„•) : Ξ±) = bit1 n := by rw [bit1, cast_add_one, cast_bit0]; refl lemma cast_two {Ξ± : Type*} [add_monoid Ξ±] [has_one Ξ±] : ((2 : β„•) : Ξ±) = 2 := by simp @[simp, norm_cast] theorem cast_pred [add_group Ξ±] [has_one Ξ±] : βˆ€ {n}, 0 < n β†’ ((n - 1 : β„•) : Ξ±) = n - 1 | (n+1) h := (add_sub_cancel (n:Ξ±) 1).symm @[simp, norm_cast] theorem cast_sub [add_group Ξ±] [has_one Ξ±] {m n} (h : m ≀ n) : ((n - m : β„•) : Ξ±) = n - m := eq_sub_of_add_eq $ by rw [← cast_add, tsub_add_cancel_of_le h] @[simp, norm_cast] theorem cast_mul [non_assoc_semiring Ξ±] (m) : βˆ€ n, ((m * n : β„•) : Ξ±) = m * n | 0 := (mul_zero _).symm | (n+1) := (cast_add _ _).trans $ show ((m * n : β„•) : Ξ±) + m = m * (n + 1), by rw [cast_mul n, left_distrib, mul_one] @[simp] theorem cast_dvd {Ξ± : Type*} [field Ξ±] {m n : β„•} (n_dvd : n ∣ m) (n_nonzero : (n:Ξ±) β‰  0) : ((m / n : β„•) : Ξ±) = m / n := begin rcases n_dvd with ⟨k, rfl⟩, have : n β‰  0, {rintro rfl, simpa using n_nonzero}, rw nat.mul_div_cancel_left _ (pos_iff_ne_zero.2 this), rw [nat.cast_mul, mul_div_cancel_left _ n_nonzero], end /-- `coe : β„• β†’ Ξ±` as a `ring_hom` -/ def cast_ring_hom (Ξ± : Type*) [non_assoc_semiring Ξ±] : β„• β†’+* Ξ± := { to_fun := coe, map_one' := cast_one, map_mul' := cast_mul, .. cast_add_monoid_hom Ξ± } @[simp] lemma coe_cast_ring_hom [non_assoc_semiring Ξ±] : (cast_ring_hom Ξ± : β„• β†’ Ξ±) = coe := rfl lemma cast_commute [non_assoc_semiring Ξ±] (n : β„•) (x : Ξ±) : commute ↑n x := nat.rec_on n (commute.zero_left x) $ Ξ» n ihn, ihn.add_left $ commute.one_left x lemma cast_comm [non_assoc_semiring Ξ±] (n : β„•) (x : Ξ±) : (n : Ξ±) * x = x * n := (cast_commute n x).eq lemma commute_cast [semiring Ξ±] (x : Ξ±) (n : β„•) : commute x n := (n.cast_commute x).symm section variables [ordered_semiring Ξ±] @[simp] theorem cast_nonneg : βˆ€ n : β„•, 0 ≀ (n : Ξ±) | 0 := le_refl _ | (n+1) := add_nonneg (cast_nonneg n) zero_le_one @[mono] theorem mono_cast : monotone (coe : β„• β†’ Ξ±) := Ξ» m n h, let ⟨k, hk⟩ := le_iff_exists_add.1 h in by simp [hk] variable [nontrivial Ξ±] theorem strict_mono_cast : strict_mono (coe : β„• β†’ Ξ±) := Ξ» m n h, nat.le_induction (lt_add_of_pos_right _ zero_lt_one) (Ξ» n _ h, lt_add_of_lt_of_pos h zero_lt_one) _ h @[simp, norm_cast] theorem cast_le {m n : β„•} : (m : Ξ±) ≀ n ↔ m ≀ n := strict_mono_cast.le_iff_le @[simp, norm_cast, mono] theorem cast_lt {m n : β„•} : (m : Ξ±) < n ↔ m < n := strict_mono_cast.lt_iff_lt @[simp] theorem cast_pos {n : β„•} : (0 : Ξ±) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] lemma cast_add_one_pos (n : β„•) : 0 < (n : Ξ±) + 1 := add_pos_of_nonneg_of_pos n.cast_nonneg zero_lt_one @[simp, norm_cast] theorem one_lt_cast {n : β„•} : 1 < (n : Ξ±) ↔ 1 < n := by rw [← cast_one, cast_lt] @[simp, norm_cast] theorem one_le_cast {n : β„•} : 1 ≀ (n : Ξ±) ↔ 1 ≀ n := by rw [← cast_one, cast_le] @[simp, norm_cast] theorem cast_lt_one {n : β„•} : (n : Ξ±) < 1 ↔ n = 0 := by rw [← cast_one, cast_lt, lt_succ_iff, le_zero_iff] @[simp, norm_cast] theorem cast_le_one {n : β„•} : (n : Ξ±) ≀ 1 ↔ n ≀ 1 := by rw [← cast_one, cast_le] end @[simp, norm_cast] theorem cast_min [linear_ordered_semiring Ξ±] {a b : β„•} : (↑(min a b) : Ξ±) = min a b := (@mono_cast Ξ± _).map_min @[simp, norm_cast] theorem cast_max [linear_ordered_semiring Ξ±] {a b : β„•} : (↑(max a b) : Ξ±) = max a b := (@mono_cast Ξ± _).map_max @[simp, norm_cast] theorem abs_cast [linear_ordered_ring Ξ±] (a : β„•) : |(a : Ξ±)| = a := abs_of_nonneg (cast_nonneg a) lemma coe_nat_dvd [comm_semiring Ξ±] {m n : β„•} (h : m ∣ n) : (m : Ξ±) ∣ (n : Ξ±) := ring_hom.map_dvd (nat.cast_ring_hom Ξ±) h alias coe_nat_dvd ← has_dvd.dvd.nat_cast section linear_ordered_field variables [linear_ordered_field Ξ±] /-- Natural division is always less than division in the field. -/ lemma cast_div_le {m n : β„•} : ((m / n : β„•) : Ξ±) ≀ m / n := begin cases n, { rw [cast_zero, div_zero, nat.div_zero, cast_zero] }, rwa [le_div_iff, ←nat.cast_mul], exact nat.cast_le.2 (nat.div_mul_le_self m n.succ), { exact nat.cast_pos.2 n.succ_pos } end lemma inv_pos_of_nat {n : β„•} : 0 < ((n : Ξ±) + 1)⁻¹ := inv_pos.2 $ add_pos_of_nonneg_of_pos n.cast_nonneg zero_lt_one lemma one_div_pos_of_nat {n : β„•} : 0 < 1 / ((n : Ξ±) + 1) := by { rw one_div, exact inv_pos_of_nat } lemma one_div_le_one_div {n m : β„•} (h : n ≀ m) : 1 / ((m : Ξ±) + 1) ≀ 1 / ((n : Ξ±) + 1) := by { refine one_div_le_one_div_of_le _ _, exact nat.cast_add_one_pos _, simpa } lemma one_div_lt_one_div {n m : β„•} (h : n < m) : 1 / ((m : Ξ±) + 1) < 1 / ((n : Ξ±) + 1) := by { refine one_div_lt_one_div_of_lt _ _, exact nat.cast_add_one_pos _, simpa } end linear_ordered_field end nat namespace prod variables {Ξ± : Type*} {Ξ² : Type*} [has_zero Ξ±] [has_one Ξ±] [has_add Ξ±] [has_zero Ξ²] [has_one Ξ²] [has_add Ξ²] @[simp] lemma fst_nat_cast (n : β„•) : (n : Ξ± Γ— Ξ²).fst = n := by induction n; simp * @[simp] lemma snd_nat_cast (n : β„•) : (n : Ξ± Γ— Ξ²).snd = n := by induction n; simp * end prod namespace add_monoid_hom variables {A B : Type*} [add_monoid A] @[ext] lemma ext_nat {f g : β„• β†’+ A} (h : f 1 = g 1) : f = g := ext $ Ξ» n, nat.rec_on n (f.map_zero.trans g.map_zero.symm) $ Ξ» n ihn, by simp only [nat.succ_eq_add_one, *, map_add] variables [has_one A] [add_monoid B] [has_one B] lemma eq_nat_cast (f : β„• β†’+ A) (h1 : f 1 = 1) : βˆ€ n : β„•, f n = n := congr_fun $ show f = nat.cast_add_monoid_hom A, from ext_nat (h1.trans nat.cast_one.symm) lemma map_nat_cast (f : A β†’+ B) (h1 : f 1 = 1) (n : β„•) : f n = n := (f.comp (nat.cast_add_monoid_hom A)).eq_nat_cast (by simp [h1]) _ end add_monoid_hom namespace monoid_with_zero_hom variables {A : Type*} [monoid_with_zero A] /-- If two `monoid_with_zero_hom`s agree on the positive naturals they are equal. -/ @[ext] theorem ext_nat {f g : monoid_with_zero_hom β„• A} (h_pos : βˆ€ {n : β„•}, 0 < n β†’ f n = g n) : f = g := begin ext (_ | n), { rw [f.map_zero, g.map_zero] }, { exact h_pos n.zero_lt_succ, }, end end monoid_with_zero_hom namespace ring_hom variables {R : Type*} {S : Type*} [non_assoc_semiring R] [non_assoc_semiring S] @[simp] lemma eq_nat_cast (f : β„• β†’+* R) (n : β„•) : f n = n := f.to_add_monoid_hom.eq_nat_cast f.map_one n @[simp] lemma map_nat_cast (f : R β†’+* S) (n : β„•) : f n = n := (f.comp (nat.cast_ring_hom R)).eq_nat_cast n lemma ext_nat (f g : β„• β†’+* R) : f = g := coe_add_monoid_hom_injective $ add_monoid_hom.ext_nat $ f.map_one.trans g.map_one.symm end ring_hom @[simp, norm_cast] theorem nat.cast_id (n : β„•) : ↑n = n := ((ring_hom.id β„•).eq_nat_cast n).symm @[simp] theorem nat.cast_with_bot : βˆ€ (n : β„•), @coe β„• (with_bot β„•) (@coe_to_lift _ _ nat.cast_coe) n = n | 0 := rfl | (n+1) := by rw [with_bot.coe_add, nat.cast_add, nat.cast_with_bot n]; refl instance nat.subsingleton_ring_hom {R : Type*} [non_assoc_semiring R] : subsingleton (β„• β†’+* R) := ⟨ring_hom.ext_nat⟩ namespace with_top variables {Ξ± : Type*} variables [has_zero Ξ±] [has_one Ξ±] [has_add Ξ±] @[simp, norm_cast] lemma coe_nat : βˆ€(n : nat), ((n : Ξ±) : with_top Ξ±) = n | 0 := rfl | (n+1) := by { push_cast, rw [coe_nat n] } @[simp] lemma nat_ne_top (n : nat) : (n : with_top Ξ±) β‰  ⊀ := by { rw [←coe_nat n], apply coe_ne_top } @[simp] lemma top_ne_nat (n : nat) : (⊀ : with_top Ξ±) β‰  n := by { rw [←coe_nat n], apply top_ne_coe } lemma add_one_le_of_lt {i n : with_top β„•} (h : i < n) : i + 1 ≀ n := begin cases n, { exact le_top }, cases i, { exact (not_le_of_lt h le_top).elim }, exact with_top.coe_le_coe.2 (with_top.coe_lt_coe.1 h) end lemma one_le_iff_pos {n : with_top β„•} : 1 ≀ n ↔ 0 < n := ⟨lt_of_lt_of_le (coe_lt_coe.mpr zero_lt_one), Ξ» h, by simpa only [zero_add] using add_one_le_of_lt h⟩ @[elab_as_eliminator] lemma nat_induction {P : with_top β„• β†’ Prop} (a : with_top β„•) (h0 : P 0) (hsuc : βˆ€n:β„•, P n β†’ P n.succ) (htop : (βˆ€n : β„•, P n) β†’ P ⊀) : P a := begin have A : βˆ€n:β„•, P n := Ξ» n, nat.rec_on n h0 hsuc, cases a, { exact htop A }, { exact A a } end end with_top namespace pi variables {Ξ± Ξ² : Type*} lemma nat_apply [has_zero Ξ²] [has_one Ξ²] [has_add Ξ²] : βˆ€ (n : β„•) (a : Ξ±), (n : Ξ± β†’ Ξ²) a = n | 0 a := rfl | (n+1) a := by rw [nat.cast_succ, nat.cast_succ, add_apply, nat_apply, one_apply] @[simp] lemma coe_nat [has_zero Ξ²] [has_one Ξ²] [has_add Ξ²] (n : β„•) : (n : Ξ± β†’ Ξ²) = Ξ» _, n := by { ext, rw pi.nat_apply } end pi
5beead1b9f5a9ab5c9302eeac51ea047e504c1c2
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/special_functions/pow/continuity.lean
b59cb77b44b816234d2f8843cec7bd9a4e13670a
[ "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
20,715
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, SΓ©bastien GouΓ«zel, RΓ©my Degenne, David Loeffler -/ import analysis.special_functions.pow.asymptotics /-! # Continuity of power functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains lemmas about continuity of the power functions on `β„‚`, `ℝ`, `ℝβ‰₯0`, and `ℝβ‰₯0∞`. -/ noncomputable theory open_locale classical real topology nnreal ennreal filter big_operators complex_conjugate open filter finset set section cpow_limits /-! ## Continuity for complex powers -/ open complex variables {Ξ± : Type*} lemma zero_cpow_eq_nhds {b : β„‚} (hb : b β‰  0) : (Ξ» (x : β„‚), (0 : β„‚) ^ x) =αΆ [𝓝 b] 0 := begin suffices : βˆ€αΆ  (x : β„‚) in (𝓝 b), x β‰  0, from this.mono (Ξ» x hx, by { dsimp only, rw [zero_cpow hx, pi.zero_apply]} ), exact is_open.eventually_mem is_open_ne hb, end lemma cpow_eq_nhds {a b : β„‚} (ha : a β‰  0) : (Ξ» x, x ^ b) =αΆ [𝓝 a] Ξ» x, exp (log x * b) := begin suffices : βˆ€αΆ  (x : β„‚) in (𝓝 a), x β‰  0, from this.mono (Ξ» x hx, by { dsimp only, rw [cpow_def_of_ne_zero hx], }), exact is_open.eventually_mem is_open_ne ha, end lemma cpow_eq_nhds' {p : β„‚ Γ— β„‚} (hp_fst : p.fst β‰  0) : (Ξ» x, x.1 ^ x.2) =αΆ [𝓝 p] Ξ» x, exp (log x.1 * x.2) := begin suffices : βˆ€αΆ  (x : β„‚ Γ— β„‚) in (𝓝 p), x.1 β‰  0, from this.mono (Ξ» x hx, by { dsimp only, rw cpow_def_of_ne_zero hx, }), refine is_open.eventually_mem _ hp_fst, change is_open {x : β„‚ Γ— β„‚ | x.1 = 0}ᢜ, rw is_open_compl_iff, exact is_closed_eq continuous_fst continuous_const, end /- Continuity of `Ξ» x, a ^ x`: union of these two lemmas is optimal. -/ lemma continuous_at_const_cpow {a b : β„‚} (ha : a β‰  0) : continuous_at (Ξ» x, a ^ x) b := begin have cpow_eq : (Ξ» x:β„‚, a ^ x) = Ξ» x, exp (log a * x), by { ext1 b, rw [cpow_def_of_ne_zero ha], }, rw cpow_eq, exact continuous_exp.continuous_at.comp (continuous_at.mul continuous_at_const continuous_at_id), end lemma continuous_at_const_cpow' {a b : β„‚} (h : b β‰  0) : continuous_at (Ξ» x, a ^ x) b := begin by_cases ha : a = 0, { rw [ha, continuous_at_congr (zero_cpow_eq_nhds h)], exact continuous_at_const, }, { exact continuous_at_const_cpow ha, }, end /-- The function `z ^ w` is continuous in `(z, w)` provided that `z` does not belong to the interval `(-∞, 0]` on the real line. See also `complex.continuous_at_cpow_zero_of_re_pos` for a version that works for `z = 0` but assumes `0 < re w`. -/ lemma continuous_at_cpow {p : β„‚ Γ— β„‚} (hp_fst : 0 < p.fst.re ∨ p.fst.im β‰  0) : continuous_at (Ξ» x : β„‚ Γ— β„‚, x.1 ^ x.2) p := begin have hp_fst_ne_zero : p.fst β‰  0, by { intro h, cases hp_fst; { rw h at hp_fst, simpa using hp_fst, }, }, rw continuous_at_congr (cpow_eq_nhds' hp_fst_ne_zero), refine continuous_exp.continuous_at.comp _, refine continuous_at.mul (continuous_at.comp _ continuous_fst.continuous_at) continuous_snd.continuous_at, exact continuous_at_clog hp_fst, end lemma continuous_at_cpow_const {a b : β„‚} (ha : 0 < a.re ∨ a.im β‰  0) : continuous_at (Ξ» x, cpow x b) a := tendsto.comp (@continuous_at_cpow (a, b) ha) (continuous_at_id.prod continuous_at_const) lemma filter.tendsto.cpow {l : filter Ξ±} {f g : Ξ± β†’ β„‚} {a b : β„‚} (hf : tendsto f l (𝓝 a)) (hg : tendsto g l (𝓝 b)) (ha : 0 < a.re ∨ a.im β‰  0) : tendsto (Ξ» x, f x ^ g x) l (𝓝 (a ^ b)) := (@continuous_at_cpow (a,b) ha).tendsto.comp (hf.prod_mk_nhds hg) lemma filter.tendsto.const_cpow {l : filter Ξ±} {f : Ξ± β†’ β„‚} {a b : β„‚} (hf : tendsto f l (𝓝 b)) (h : a β‰  0 ∨ b β‰  0) : tendsto (Ξ» x, a ^ f x) l (𝓝 (a ^ b)) := begin cases h, { exact (continuous_at_const_cpow h).tendsto.comp hf, }, { exact (continuous_at_const_cpow' h).tendsto.comp hf, }, end variables [topological_space Ξ±] {f g : Ξ± β†’ β„‚} {s : set Ξ±} {a : Ξ±} lemma continuous_within_at.cpow (hf : continuous_within_at f s a) (hg : continuous_within_at g s a) (h0 : 0 < (f a).re ∨ (f a).im β‰  0) : continuous_within_at (Ξ» x, f x ^ g x) s a := hf.cpow hg h0 lemma continuous_within_at.const_cpow {b : β„‚} (hf : continuous_within_at f s a) (h : b β‰  0 ∨ f a β‰  0) : continuous_within_at (Ξ» x, b ^ f x) s a := hf.const_cpow h lemma continuous_at.cpow (hf : continuous_at f a) (hg : continuous_at g a) (h0 : 0 < (f a).re ∨ (f a).im β‰  0) : continuous_at (Ξ» x, f x ^ g x) a := hf.cpow hg h0 lemma continuous_at.const_cpow {b : β„‚} (hf : continuous_at f a) (h : b β‰  0 ∨ f a β‰  0) : continuous_at (Ξ» x, b ^ f x) a := hf.const_cpow h lemma continuous_on.cpow (hf : continuous_on f s) (hg : continuous_on g s) (h0 : βˆ€ a ∈ s, 0 < (f a).re ∨ (f a).im β‰  0) : continuous_on (Ξ» x, f x ^ g x) s := Ξ» a ha, (hf a ha).cpow (hg a ha) (h0 a ha) lemma continuous_on.const_cpow {b : β„‚} (hf : continuous_on f s) (h : b β‰  0 ∨ βˆ€ a ∈ s, f a β‰  0) : continuous_on (Ξ» x, b ^ f x) s := Ξ» a ha, (hf a ha).const_cpow (h.imp id $ Ξ» h, h a ha) lemma continuous.cpow (hf : continuous f) (hg : continuous g) (h0 : βˆ€ a, 0 < (f a).re ∨ (f a).im β‰  0) : continuous (Ξ» x, f x ^ g x) := continuous_iff_continuous_at.2 $ Ξ» a, (hf.continuous_at.cpow hg.continuous_at (h0 a)) lemma continuous.const_cpow {b : β„‚} (hf : continuous f) (h : b β‰  0 ∨ βˆ€ a, f a β‰  0) : continuous (Ξ» x, b ^ f x) := continuous_iff_continuous_at.2 $ Ξ» a, (hf.continuous_at.const_cpow $ h.imp id $ Ξ» h, h a) lemma continuous_on.cpow_const {b : β„‚} (hf : continuous_on f s) (h : βˆ€ (a : Ξ±), a ∈ s β†’ 0 < (f a).re ∨ (f a).im β‰  0) : continuous_on (Ξ» x, (f x) ^ b) s := hf.cpow continuous_on_const h end cpow_limits section rpow_limits /-! ## Continuity for real powers -/ namespace real lemma continuous_at_const_rpow {a b : ℝ} (h : a β‰  0) : continuous_at (rpow a) b := begin have : rpow a = Ξ» x : ℝ, ((a : β„‚) ^ (x : β„‚)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], }, rw this, refine complex.continuous_re.continuous_at.comp _, refine (continuous_at_const_cpow _).comp complex.continuous_of_real.continuous_at, norm_cast, exact h, end lemma continuous_at_const_rpow' {a b : ℝ} (h : b β‰  0) : continuous_at (rpow a) b := begin have : rpow a = Ξ» x : ℝ, ((a : β„‚) ^ (x : β„‚)).re, by { ext1 x, rw [rpow_eq_pow, rpow_def], }, rw this, refine complex.continuous_re.continuous_at.comp _, refine (continuous_at_const_cpow' _).comp complex.continuous_of_real.continuous_at, norm_cast, exact h, end lemma rpow_eq_nhds_of_neg {p : ℝ Γ— ℝ} (hp_fst : p.fst < 0) : (Ξ» x : ℝ Γ— ℝ, x.1 ^ x.2) =αΆ [𝓝 p] Ξ» x, exp (log x.1 * x.2) * cos (x.2 * Ο€) := begin suffices : βˆ€αΆ  (x : ℝ Γ— ℝ) in (𝓝 p), x.1 < 0, from this.mono (Ξ» x hx, by { dsimp only, rw rpow_def_of_neg hx, }), exact is_open.eventually_mem (is_open_lt continuous_fst continuous_const) hp_fst, end lemma rpow_eq_nhds_of_pos {p : ℝ Γ— ℝ} (hp_fst : 0 < p.fst) : (Ξ» x : ℝ Γ— ℝ, x.1 ^ x.2) =αΆ [𝓝 p] Ξ» x, exp (log x.1 * x.2) := begin suffices : βˆ€αΆ  (x : ℝ Γ— ℝ) in (𝓝 p), 0 < x.1, from this.mono (Ξ» x hx, by { dsimp only, rw rpow_def_of_pos hx, }), exact is_open.eventually_mem (is_open_lt continuous_const continuous_fst) hp_fst, end lemma continuous_at_rpow_of_ne (p : ℝ Γ— ℝ) (hp : p.1 β‰  0) : continuous_at (Ξ» p : ℝ Γ— ℝ, p.1 ^ p.2) p := begin rw ne_iff_lt_or_gt at hp, cases hp, { rw continuous_at_congr (rpow_eq_nhds_of_neg hp), refine continuous_at.mul _ (continuous_cos.continuous_at.comp _), { refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at), refine (continuous_at_log _).comp continuous_fst.continuous_at, exact hp.ne, }, { exact continuous_snd.continuous_at.mul continuous_at_const, }, }, { rw continuous_at_congr (rpow_eq_nhds_of_pos hp), refine continuous_exp.continuous_at.comp (continuous_at.mul _ continuous_snd.continuous_at), refine (continuous_at_log _).comp continuous_fst.continuous_at, exact hp.lt.ne.symm, }, end lemma continuous_at_rpow_of_pos (p : ℝ Γ— ℝ) (hp : 0 < p.2) : continuous_at (Ξ» p : ℝ Γ— ℝ, p.1 ^ p.2) p := begin cases p with x y, obtain hx|rfl := ne_or_eq x 0, { exact continuous_at_rpow_of_ne (x, y) hx }, have A : tendsto (Ξ» p : ℝ Γ— ℝ, exp (log p.1 * p.2)) (𝓝[β‰ ] 0 Γ—αΆ  𝓝 y) (𝓝 0) := tendsto_exp_at_bot.comp ((tendsto_log_nhds_within_zero.comp tendsto_fst).at_bot_mul hp tendsto_snd), have B : tendsto (Ξ» p : ℝ Γ— ℝ, p.1 ^ p.2) (𝓝[β‰ ] 0 Γ—αΆ  𝓝 y) (𝓝 0) := squeeze_zero_norm (Ξ» p, abs_rpow_le_exp_log_mul p.1 p.2) A, have C : tendsto (Ξ» p : ℝ Γ— ℝ, p.1 ^ p.2) (𝓝[{0}] 0 Γ—αΆ  𝓝 y) (pure 0), { rw [nhds_within_singleton, tendsto_pure, pure_prod, eventually_map], exact (lt_mem_nhds hp).mono (Ξ» y hy, zero_rpow hy.ne') }, simpa only [← sup_prod, ← nhds_within_union, compl_union_self, nhds_within_univ, nhds_prod_eq, continuous_at, zero_rpow hp.ne'] using B.sup (C.mono_right (pure_le_nhds _)) end lemma continuous_at_rpow (p : ℝ Γ— ℝ) (h : p.1 β‰  0 ∨ 0 < p.2) : continuous_at (Ξ» p : ℝ Γ— ℝ, p.1 ^ p.2) p := h.elim (Ξ» h, continuous_at_rpow_of_ne p h) (Ξ» h, continuous_at_rpow_of_pos p h) lemma continuous_at_rpow_const (x : ℝ) (q : ℝ) (h : x β‰  0 ∨ 0 < q) : continuous_at (Ξ» (x : ℝ), x ^ q) x := begin change continuous_at ((Ξ» p : ℝ Γ— ℝ, p.1 ^ p.2) ∘ (Ξ» y : ℝ, (y, q))) x, apply continuous_at.comp, { exact continuous_at_rpow (x, q) h }, { exact (continuous_id'.prod_mk continuous_const).continuous_at } end end real section variable {Ξ± : Type*} lemma filter.tendsto.rpow {l : filter Ξ±} {f g : Ξ± β†’ ℝ} {x y : ℝ} (hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) (h : x β‰  0 ∨ 0 < y) : tendsto (Ξ» t, f t ^ g t) l (𝓝 (x ^ y)) := (real.continuous_at_rpow (x, y) h).tendsto.comp (hf.prod_mk_nhds hg) lemma filter.tendsto.rpow_const {l : filter Ξ±} {f : Ξ± β†’ ℝ} {x p : ℝ} (hf : tendsto f l (𝓝 x)) (h : x β‰  0 ∨ 0 ≀ p) : tendsto (Ξ» a, f a ^ p) l (𝓝 (x ^ p)) := if h0 : 0 = p then h0 β–Έ by simp [tendsto_const_nhds] else hf.rpow tendsto_const_nhds (h.imp id $ Ξ» h', h'.lt_of_ne h0) variables [topological_space Ξ±] {f g : Ξ± β†’ ℝ} {s : set Ξ±} {x : Ξ±} {p : ℝ} lemma continuous_at.rpow (hf : continuous_at f x) (hg : continuous_at g x) (h : f x β‰  0 ∨ 0 < g x) : continuous_at (Ξ» t, f t ^ g t) x := hf.rpow hg h lemma continuous_within_at.rpow (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) (h : f x β‰  0 ∨ 0 < g x) : continuous_within_at (Ξ» t, f t ^ g t) s x := hf.rpow hg h lemma continuous_on.rpow (hf : continuous_on f s) (hg : continuous_on g s) (h : βˆ€ x ∈ s, f x β‰  0 ∨ 0 < g x) : continuous_on (Ξ» t, f t ^ g t) s := Ξ» t ht, (hf t ht).rpow (hg t ht) (h t ht) lemma continuous.rpow (hf : continuous f) (hg : continuous g) (h : βˆ€ x, f x β‰  0 ∨ 0 < g x) : continuous (Ξ» x, f x ^ g x) := continuous_iff_continuous_at.2 $ Ξ» x, (hf.continuous_at.rpow hg.continuous_at (h x)) lemma continuous_within_at.rpow_const (hf : continuous_within_at f s x) (h : f x β‰  0 ∨ 0 ≀ p) : continuous_within_at (Ξ» x, f x ^ p) s x := hf.rpow_const h lemma continuous_at.rpow_const (hf : continuous_at f x) (h : f x β‰  0 ∨ 0 ≀ p) : continuous_at (Ξ» x, f x ^ p) x := hf.rpow_const h lemma continuous_on.rpow_const (hf : continuous_on f s) (h : βˆ€ x ∈ s, f x β‰  0 ∨ 0 ≀ p) : continuous_on (Ξ» x, f x ^ p) s := Ξ» x hx, (hf x hx).rpow_const (h x hx) lemma continuous.rpow_const (hf : continuous f) (h : βˆ€ x, f x β‰  0 ∨ 0 ≀ p) : continuous (Ξ» x, f x ^ p) := continuous_iff_continuous_at.2 $ Ξ» x, hf.continuous_at.rpow_const (h x) end end rpow_limits /-! ## Continuity results for `cpow`, part II These results involve relating real and complex powers, so cannot be done higher up. -/ section cpow_limits2 namespace complex /-- See also `continuous_at_cpow` and `complex.continuous_at_cpow_of_re_pos`. -/ lemma continuous_at_cpow_zero_of_re_pos {z : β„‚} (hz : 0 < z.re) : continuous_at (Ξ» x : β„‚ Γ— β„‚, x.1 ^ x.2) (0, z) := begin have hzβ‚€ : z β‰  0, from ne_of_apply_ne re hz.ne', rw [continuous_at, zero_cpow hzβ‚€, tendsto_zero_iff_norm_tendsto_zero], refine squeeze_zero (Ξ» _, norm_nonneg _) (Ξ» _, abs_cpow_le _ _) _, simp only [div_eq_mul_inv, ← real.exp_neg], refine tendsto.zero_mul_is_bounded_under_le _ _, { convert (continuous_fst.norm.tendsto _).rpow ((continuous_re.comp continuous_snd).tendsto _) _; simp [hz, real.zero_rpow hz.ne'] }, { simp only [(∘), real.norm_eq_abs, abs_of_pos (real.exp_pos _)], rcases exists_gt (|im z|) with ⟨C, hC⟩, refine ⟨real.exp (Ο€ * C), eventually_map.2 _⟩, refine (((continuous_im.comp continuous_snd).abs.tendsto (_, z)).eventually (gt_mem_nhds hC)).mono (Ξ» z hz, real.exp_le_exp.2 $ (neg_le_abs_self _).trans _), rw _root_.abs_mul, exact mul_le_mul (abs_le.2 ⟨(neg_pi_lt_arg _).le, arg_le_pi _⟩) hz.le (_root_.abs_nonneg _) real.pi_pos.le } end /-- See also `continuous_at_cpow` for a version that assumes `p.1 β‰  0` but makes no assumptions about `p.2`. -/ lemma continuous_at_cpow_of_re_pos {p : β„‚ Γ— β„‚} (h₁ : 0 ≀ p.1.re ∨ p.1.im β‰  0) (hβ‚‚ : 0 < p.2.re) : continuous_at (Ξ» x : β„‚ Γ— β„‚, x.1 ^ x.2) p := begin cases p with z w, rw [← not_lt_zero_iff, lt_iff_le_and_ne, not_and_distrib, ne.def, not_not, not_le_zero_iff] at h₁, rcases h₁ with h₁|(rfl : z = 0), exacts [continuous_at_cpow h₁, continuous_at_cpow_zero_of_re_pos hβ‚‚] end /-- See also `continuous_at_cpow_const` for a version that assumes `z β‰  0` but makes no assumptions about `w`. -/ lemma continuous_at_cpow_const_of_re_pos {z w : β„‚} (hz : 0 ≀ re z ∨ im z β‰  0) (hw : 0 < re w) : continuous_at (Ξ» x, x ^ w) z := tendsto.comp (@continuous_at_cpow_of_re_pos (z, w) hz hw) (continuous_at_id.prod continuous_at_const) /-- Continuity of `(x, y) ↦ x ^ y` as a function on `ℝ Γ— β„‚`. -/ lemma continuous_at_of_real_cpow (x : ℝ) (y : β„‚) (h : 0 < y.re ∨ x β‰  0) : continuous_at (Ξ» p, ↑p.1 ^ p.2 : ℝ Γ— β„‚ β†’ β„‚) (x, y) := begin rcases lt_trichotomy 0 x with hx | rfl | hx, { -- x > 0 : easy case have : continuous_at (Ξ» p, βŸ¨β†‘p.1, p.2⟩ : ℝ Γ— β„‚ β†’ β„‚ Γ— β„‚) (x, y), from continuous_of_real.continuous_at.prod_map continuous_at_id, refine (continuous_at_cpow (or.inl _)).comp this, rwa of_real_re }, { -- x = 0 : reduce to continuous_at_cpow_zero_of_re_pos have A : continuous_at (Ξ» p, p.1 ^ p.2 : β„‚ Γ— β„‚ β†’ β„‚) βŸ¨β†‘(0:ℝ), y⟩, { rw of_real_zero, apply continuous_at_cpow_zero_of_re_pos, tauto }, have B : continuous_at (Ξ» p, βŸ¨β†‘p.1, p.2⟩ : ℝ Γ— β„‚ β†’ β„‚ Γ— β„‚) ⟨0, y⟩, from continuous_of_real.continuous_at.prod_map continuous_at_id, exact @continuous_at.comp (ℝ Γ— β„‚) (β„‚ Γ— β„‚) β„‚ _ _ _ _ (Ξ» p, βŸ¨β†‘p.1, p.2⟩) ⟨0, y⟩ A B }, { -- x < 0 : difficult case suffices : continuous_at (Ξ» p, (-↑p.1) ^ p.2 * exp (Ο€ * I * p.2) : ℝ Γ— β„‚ β†’ β„‚) (x, y), { refine this.congr (eventually_of_mem (prod_mem_nhds (Iio_mem_nhds hx) univ_mem) _), exact Ξ» p hp, (of_real_cpow_of_nonpos (le_of_lt hp.1) p.2).symm }, have A : continuous_at (Ξ» p, ⟨-↑p.1, p.2⟩ : ℝ Γ— β„‚ β†’ β„‚ Γ— β„‚) (x, y), from continuous_at.prod_map (continuous_of_real.continuous_at.neg) continuous_at_id, apply continuous_at.mul, { refine (continuous_at_cpow (or.inl _)).comp A, rwa [neg_re, of_real_re, neg_pos] }, { exact (continuous_exp.comp (continuous_const.mul continuous_snd)).continuous_at } }, end lemma continuous_at_of_real_cpow_const (x : ℝ) (y : β„‚) (h : 0 < y.re ∨ x β‰  0) : continuous_at (Ξ» a, a ^ y : ℝ β†’ β„‚) x := @continuous_at.comp _ _ _ _ _ _ _ _ x (continuous_at_of_real_cpow x y h) (continuous_id.prod_mk continuous_const).continuous_at lemma continuous_of_real_cpow_const {y : β„‚} (hs : 0 < y.re) : continuous (Ξ» x, x ^ y : ℝ β†’ β„‚) := continuous_iff_continuous_at.mpr (Ξ» x, continuous_at_of_real_cpow_const x y (or.inl hs)) end complex end cpow_limits2 /-! ## Limits and continuity for `ℝβ‰₯0` powers -/ namespace nnreal lemma continuous_at_rpow {x : ℝβ‰₯0} {y : ℝ} (h : x β‰  0 ∨ 0 < y) : continuous_at (Ξ»p:ℝβ‰₯0×ℝ, p.1^p.2) (x, y) := begin have : (Ξ»p:ℝβ‰₯0×ℝ, p.1^p.2) = real.to_nnreal ∘ (Ξ»p:ℝ×ℝ, p.1^p.2) ∘ (Ξ»p:ℝβ‰₯0 Γ— ℝ, (p.1.1, p.2)), { ext p, rw [coe_rpow, real.coe_to_nnreal _ (real.rpow_nonneg_of_nonneg p.1.2 _)], refl }, rw this, refine continuous_real_to_nnreal.continuous_at.comp (continuous_at.comp _ _), { apply real.continuous_at_rpow, simp only [ne.def] at h, rw ← (nnreal.coe_eq_zero x) at h, exact h }, { exact ((continuous_subtype_val.comp continuous_fst).prod_mk continuous_snd).continuous_at } end lemma eventually_pow_one_div_le (x : ℝβ‰₯0) {y : ℝβ‰₯0} (hy : 1 < y) : βˆ€αΆ  (n : β„•) in at_top, x ^ (1 / n : ℝ) ≀ y := begin obtain ⟨m, hm⟩ := add_one_pow_unbounded_of_pos x (tsub_pos_of_lt hy), rw [tsub_add_cancel_of_le hy.le] at hm, refine eventually_at_top.2 ⟨m + 1, Ξ» n hn, _⟩, simpa only [nnreal.rpow_one_div_le_iff (nat.cast_pos.2 $ m.succ_pos.trans_le hn), nnreal.rpow_nat_cast] using hm.le.trans (pow_le_pow hy.le (m.le_succ.trans hn)), end end nnreal open filter lemma filter.tendsto.nnrpow {Ξ± : Type*} {f : filter Ξ±} {u : Ξ± β†’ ℝβ‰₯0} {v : Ξ± β†’ ℝ} {x : ℝβ‰₯0} {y : ℝ} (hx : tendsto u f (𝓝 x)) (hy : tendsto v f (𝓝 y)) (h : x β‰  0 ∨ 0 < y) : tendsto (Ξ» a, (u a) ^ (v a)) f (𝓝 (x ^ y)) := tendsto.comp (nnreal.continuous_at_rpow h) (hx.prod_mk_nhds hy) namespace nnreal lemma continuous_at_rpow_const {x : ℝβ‰₯0} {y : ℝ} (h : x β‰  0 ∨ 0 ≀ y) : continuous_at (Ξ» z, z^y) x := h.elim (Ξ» h, tendsto_id.nnrpow tendsto_const_nhds (or.inl h)) $ Ξ» h, h.eq_or_lt.elim (Ξ» h, h β–Έ by simp only [rpow_zero, continuous_at_const]) (Ξ» h, tendsto_id.nnrpow tendsto_const_nhds (or.inr h)) lemma continuous_rpow_const {y : ℝ} (h : 0 ≀ y) : continuous (Ξ» x : ℝβ‰₯0, x^y) := continuous_iff_continuous_at.2 $ Ξ» x, continuous_at_rpow_const (or.inr h) end nnreal /-! ## Continuity for `ℝβ‰₯0∞` powers -/ namespace ennreal lemma eventually_pow_one_div_le {x : ℝβ‰₯0∞} (hx : x β‰  ∞) {y : ℝβ‰₯0∞} (hy : 1 < y) : βˆ€αΆ  (n : β„•) in at_top, x ^ (1 / n : ℝ) ≀ y := begin lift x to ℝβ‰₯0 using hx, by_cases y = ∞, { exact eventually_of_forall (Ξ» n, h.symm β–Έ le_top) }, { lift y to ℝβ‰₯0 using h, have := nnreal.eventually_pow_one_div_le x (by exact_mod_cast hy : 1 < y), refine this.congr (eventually_of_forall $ Ξ» n, _), rw [coe_rpow_of_nonneg x (by positivity : 0 ≀ (1 / n : ℝ)), coe_le_coe] }, end private lemma continuous_at_rpow_const_of_pos {x : ℝβ‰₯0∞} {y : ℝ} (h : 0 < y) : continuous_at (Ξ» a : ℝβ‰₯0∞, a ^ y) x := begin by_cases hx : x = ⊀, { rw [hx, continuous_at], convert tendsto_rpow_at_top h, simp [h] }, lift x to ℝβ‰₯0 using hx, rw continuous_at_coe_iff, convert continuous_coe.continuous_at.comp (nnreal.continuous_at_rpow_const (or.inr h.le)) using 1, ext1 x, simp [coe_rpow_of_nonneg _ h.le] end @[continuity] lemma continuous_rpow_const {y : ℝ} : continuous (Ξ» a : ℝβ‰₯0∞, a ^ y) := begin apply continuous_iff_continuous_at.2 (Ξ» x, _), rcases lt_trichotomy 0 y with hy|rfl|hy, { exact continuous_at_rpow_const_of_pos hy }, { simp only [rpow_zero], exact continuous_at_const }, { obtain ⟨z, hz⟩ : βˆƒ z, y = -z := ⟨-y, (neg_neg _).symm⟩, have z_pos : 0 < z, by simpa [hz] using hy, simp_rw [hz, rpow_neg], exact continuous_inv.continuous_at.comp (continuous_at_rpow_const_of_pos z_pos) } end lemma tendsto_const_mul_rpow_nhds_zero_of_pos {c : ℝβ‰₯0∞} (hc : c β‰  ∞) {y : ℝ} (hy : 0 < y) : tendsto (Ξ» x : ℝβ‰₯0∞, c * x ^ y) (𝓝 0) (𝓝 0) := begin convert ennreal.tendsto.const_mul (ennreal.continuous_rpow_const.tendsto 0) _, { simp [hy] }, { exact or.inr hc } end end ennreal lemma filter.tendsto.ennrpow_const {Ξ± : Type*} {f : filter Ξ±} {m : Ξ± β†’ ℝβ‰₯0∞} {a : ℝβ‰₯0∞} (r : ℝ) (hm : tendsto m f (𝓝 a)) : tendsto (Ξ» x, (m x) ^ r) f (𝓝 (a ^ r)) := (ennreal.continuous_rpow_const.tendsto a).comp hm
c720767658d7fb7d0990d45bd9d11e7133679185
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/limits/colimit_limit.lean
960039ce8197066ed40d4ec019100be035e0c723
[ "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
4,436
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.types import category_theory.functor.currying import category_theory.limits.functor_category /-! # The morphism comparing a colimit of limits with the corresponding limit of colimits. For `F : J Γ— K β₯€ C` there is always a morphism $\colim_k \lim_j F(j,k) β†’ \lim_j \colim_k F(j, k)$. While it is not usually an isomorphism, with additional hypotheses on `J` and `K` it may be, in which case we say that "colimits commute with limits". The prototypical example, proved in `category_theory.limits.filtered_colimit_commutes_finite_limit`, is that when `C = Type`, filtered colimits commute with finite limits. ## References * Borceux, Handbook of categorical algebra 1, Section 2.13 * [Stacks: Filtered colimits](https://stacks.math.columbia.edu/tag/002W) -/ universes v u open category_theory namespace category_theory.limits variables {J K : Type v} [small_category J] [small_category K] variables {C : Type u} [category.{v} C] variables (F : J Γ— K β₯€ C) open category_theory.prod lemma map_id_left_eq_curry_map {j : J} {k k' : K} {f : k ⟢ k'} : F.map ((πŸ™ j, f) : (j, k) ⟢ (j, k')) = ((curry.obj F).obj j).map f := rfl lemma map_id_right_eq_curry_swap_map {j j' : J} {f : j ⟢ j'} {k : K} : F.map ((f, πŸ™ k) : (j, k) ⟢ (j', k)) = ((curry.obj (swap K J β‹™ F)).obj k).map f := rfl variables [has_limits_of_shape J C] variables [has_colimits_of_shape K C] /-- The universal morphism $\colim_k \lim_j F(j,k) β†’ \lim_j \colim_k F(j, k)$. -/ noncomputable def colimit_limit_to_limit_colimit : colimit ((curry.obj (swap K J β‹™ F)) β‹™ lim) ⟢ limit ((curry.obj F) β‹™ colim) := limit.lift ((curry.obj F) β‹™ colim) { X := _, Ο€ := { app := Ξ» j, colimit.desc ((curry.obj (swap K J β‹™ F)) β‹™ lim) { X := _, ΞΉ := { app := Ξ» k, limit.Ο€ ((curry.obj (swap K J β‹™ F)).obj k) j ≫ colimit.ΞΉ ((curry.obj F).obj j) k, naturality' := begin dsimp, intros k k' f, simp only [functor.comp_map, curry.obj_map_app, limits.lim_map_Ο€_assoc, swap_map, category.comp_id, map_id_left_eq_curry_map, colimit.w], end }, }, naturality' := begin dsimp, intros j j' f, ext k, simp only [limits.colimit.ΞΉ_map, curry.obj_map_app, limits.colimit.ΞΉ_desc_assoc, limits.colimit.ΞΉ_desc, category.id_comp, category.assoc, map_id_right_eq_curry_swap_map, limit.w_assoc], end } } /-- Since `colimit_limit_to_limit_colimit` is a morphism from a colimit to a limit, this lemma characterises it. -/ @[simp, reassoc] lemma ΞΉ_colimit_limit_to_limit_colimit_Ο€ (j) (k) : colimit.ΞΉ _ k ≫ colimit_limit_to_limit_colimit F ≫ limit.Ο€ _ j = limit.Ο€ ((curry.obj (swap K J β‹™ F)).obj k) j ≫ colimit.ΞΉ ((curry.obj F).obj j) k := by { dsimp [colimit_limit_to_limit_colimit], simp, } @[simp] lemma ΞΉ_colimit_limit_to_limit_colimit_Ο€_apply (F : J Γ— K β₯€ Type v) (j) (k) (f) : limit.Ο€ ((curry.obj F) β‹™ colim) j (colimit_limit_to_limit_colimit F (colimit.ΞΉ ((curry.obj (swap K J β‹™ F)) β‹™ lim) k f)) = colimit.ΞΉ ((curry.obj F).obj j) k (limit.Ο€ ((curry.obj (swap K J β‹™ F)).obj k) j f) := by { dsimp [colimit_limit_to_limit_colimit], simp, } /-- The map `colimit_limit_to_limit_colimit` realized as a map of cones. -/ @[simps] noncomputable def colimit_limit_to_limit_colimit_cone (G : J β₯€ K β₯€ C) [has_limit G] : colim.map_cone (limit.cone G) ⟢ limit.cone (G β‹™ colim) := { hom := colim.map (limit_iso_swap_comp_lim G).hom ≫ colimit_limit_to_limit_colimit (uncurry.obj G : _) ≫ lim.map (whisker_right (currying.unit_iso.app G).inv colim), w' := Ξ» j, begin ext1 k, simp only [limit_obj_iso_limit_comp_evaluation_hom_Ο€_assoc, iso.app_inv, ΞΉ_colimit_limit_to_limit_colimit_Ο€_assoc, whisker_right_app, colimit.ΞΉ_map, functor.map_cone_Ο€_app, category.id_comp, eq_to_hom_refl, eq_to_hom_app, colimit.ΞΉ_map_assoc, limit.cone_Ο€, lim_map_Ο€_assoc, lim_map_Ο€, category.assoc, currying_unit_iso_inv_app_app_app, limit_iso_swap_comp_lim_hom_app, lim_map_eq_lim_map], dsimp, simp only [category.id_comp], erw limit_obj_iso_limit_comp_evaluation_hom_Ο€_assoc, end } end category_theory.limits
bcbe3f33e8a2174a2e82c65345ec36bbf0b91278
d406927ab5617694ec9ea7001f101b7c9e3d9702
/counterexamples/seminorm_lattice_not_distrib.lean
7963e3e7a07ef21b1829266b849e4896331bcde1
[ "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
3,001
lean
/- Copyright (c) 2022 Pierre-Alexandre Bazin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Pierre-Alexandre Bazin -/ import analysis.seminorm /-! # The lattice of seminorms is not distributive We provide an example of three seminorms over the ℝ-vector space ℝ×ℝ which don't satisfy the lattice distributivity property `(p βŠ” q1) βŠ“ (p βŠ” q2) ≀ p βŠ” (q1 βŠ“ q2)`. This proves the lattice `seminorm ℝ (ℝ Γ— ℝ)` is not distributive. ## References * https://en.wikipedia.org/wiki/Seminorm#Examples -/ open seminorm open_locale nnreal namespace seminorm_not_distrib @[simps] noncomputable def p : seminorm ℝ (ℝ×ℝ) := (norm_seminorm ℝ ℝ).comp (linear_map.fst _ _ _) βŠ” (norm_seminorm ℝ ℝ).comp (linear_map.snd _ _ _) @[simps] noncomputable def q1 : seminorm ℝ (ℝ×ℝ) := (4 : ℝβ‰₯0) β€’ (norm_seminorm ℝ ℝ).comp (linear_map.fst _ _ _) @[simps] noncomputable def q2 : seminorm ℝ (ℝ×ℝ) := (4 : ℝβ‰₯0) β€’ (norm_seminorm ℝ ℝ).comp (linear_map.snd _ _ _) lemma eq_one : (p βŠ” (q1 βŠ“ q2)) (1, 1) = 1 := begin suffices : (β¨… x : ℝ Γ— ℝ, q1 x + q2 (1 - x)) ≀ 1, by simpa, apply cinfi_le_of_le bdd_below_range_add ((0, 1) : ℝ×ℝ), dsimp [q1, q2], simp only [abs_zero, smul_zero, sub_self, add_zero, zero_le_one], end /-- This is a counterexample to the distributivity of the lattice `seminorm ℝ (ℝ Γ— ℝ)`. -/ lemma not_distrib : Β¬((p βŠ” q1) βŠ“ (p βŠ” q2) ≀ p βŠ” (q1 βŠ“ q2)) := begin intro le_sup_inf, have c : Β¬(4/3 ≀ (1:ℝ)) := by norm_num, apply c, nth_rewrite 2 ← eq_one, apply le_trans _ (le_sup_inf _), apply le_cinfi, intro x, cases le_or_lt x.fst (1/3) with h1 h1, { cases le_or_lt x.snd (2/3) with h2 h2, { calc 4/3 = 4 * (1 - 2/3) : by norm_num ... ≀ 4 * (1 - x.snd) : (mul_le_mul_left zero_lt_four).mpr (sub_le_sub_left h2 _) ... ≀ 4 * |1 - x.snd| : (mul_le_mul_left zero_lt_four).mpr (le_abs_self _) ... = q2 ((1, 1) - x) : rfl ... ≀ (p βŠ” q2) ((1, 1) - x) : le_sup_right ... ≀ (p βŠ” q1) x + (p βŠ” q2) ((1, 1) - x) : le_add_of_nonneg_left (map_nonneg _ _) }, { calc 4/3 = 2/3 + (1 - 1/3) : by norm_num ... ≀ x.snd + (1 - x.fst) : add_le_add (le_of_lt h2) (sub_le_sub_left h1 _) ... ≀ |x.snd| + |1 - x.fst| : add_le_add (le_abs_self _) (le_abs_self _) ... ≀ p x + p ((1, 1) - x) : add_le_add le_sup_right le_sup_left ... ≀ (p βŠ” q1) x + (p βŠ” q2) ((1, 1) - x) : add_le_add le_sup_left le_sup_left } }, { calc 4/3 = 4 * (1/3) : by norm_num ... ≀ 4 * x.fst : (mul_le_mul_left zero_lt_four).mpr (le_of_lt h1) ... ≀ 4 * |x.fst| : (mul_le_mul_left zero_lt_four).mpr (le_abs_self _) ... = q1 x : rfl ... ≀ (p βŠ” q1) x : le_sup_right ... ≀ (p βŠ” q1) x + (p βŠ” q2) ((1, 1) - x) : le_add_of_nonneg_right (map_nonneg _ _) } end end seminorm_not_distrib
bd2a02995accd695d4e448e595e8e87bf94ad448
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/group_theory/order_of_element.lean
7a5dabf7ae8e1e9c2f1049571e136c222bea2a80
[ "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
24,330
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 -/ import algebra.big_operators.order import group_theory.coset import data.nat.totient import data.set.finite open function open_locale big_operators variables {Ξ± : Type*} {s : set Ξ±} {a a₁ aβ‚‚ b c: Ξ±} -- TODO this lemma isn't used anywhere in this file, and should be moved elsewhere. namespace finset open finset lemma mem_range_iff_mem_finset_range_of_mod_eq [decidable_eq Ξ±] {f : β„€ β†’ Ξ±} {a : Ξ±} {n : β„•} (hn : 0 < n) (h : βˆ€i, f (i % n) = f i) : a ∈ set.range f ↔ a ∈ (finset.range n).image (Ξ»i, f i) := suffices (βˆƒi, f (i % n) = a) ↔ βˆƒi, i < n ∧ f ↑i = a, by simpa [h], have hn' : 0 < (n : β„€), from int.coe_nat_lt.mpr hn, iff.intro (assume ⟨i, hi⟩, have 0 ≀ i % ↑n, from int.mod_nonneg _ (ne_of_gt hn'), ⟨int.to_nat (i % n), by rw [←int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos i hn', hi⟩⟩) (assume ⟨i, hi, ha⟩, ⟨i, by rw [int.mod_eq_of_lt (int.coe_zero_le _) (int.coe_nat_lt_coe_nat_of_lt hi), ha]⟩) end finset lemma conj_injective [group Ξ±] {x : Ξ±} : function.injective (Ξ» (g : Ξ±), x * g * x⁻¹) := Ξ» a b h, by simpa [mul_left_inj, mul_right_inj] using h lemma mem_normalizer_fintype [group Ξ±] {s : set Ξ±} [fintype s] {x : Ξ±} (h : βˆ€ n, n ∈ s β†’ x * n * x⁻¹ ∈ s) : x ∈ is_subgroup.normalizer s := by haveI := classical.prop_decidable; haveI := set.fintype_image s (Ξ» n, x * n * x⁻¹); exact Ξ» n, ⟨h n, Ξ» h₁, have heq : (Ξ» n, x * n * x⁻¹) '' s = s := set.eq_of_subset_of_card_le (Ξ» n ⟨y, hy⟩, hy.2 β–Έ h y hy.1) (by rw set.card_image_of_injective s conj_injective), have x * n * x⁻¹ ∈ (Ξ» n, x * n * x⁻¹) '' s := heq.symm β–Έ h₁, let ⟨y, hy⟩ := this in conj_injective hy.2 β–Έ hy.1⟩ section order_of variable [group Ξ±] open quotient_group set @[simp] lemma card_trivial [fintype (is_subgroup.trivial Ξ±)] : fintype.card (is_subgroup.trivial Ξ±) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(1 : Ξ±), by simp⟩, Ξ» ⟨y, hy⟩, subtype.eq $ is_subgroup.mem_trivial.1 hy⟩ variables [fintype Ξ±] [dec : decidable_eq Ξ±] instance quotient_group.fintype (s : set Ξ±) [is_subgroup s] [d : decidable_pred s] : fintype (quotient s) := @quotient.fintype _ _ (left_rel s) (Ξ» _ _, d _) lemma card_eq_card_quotient_mul_card_subgroup (s : set Ξ±) [hs : is_subgroup s] [fintype s] [decidable_pred s] : fintype.card Ξ± = fintype.card (quotient s) * fintype.card s := by rw ← fintype.card_prod; exact fintype.card_congr (is_subgroup.group_equiv_quotient_times_subgroup hs) lemma card_subgroup_dvd_card (s : set Ξ±) [is_subgroup s] [fintype s] : fintype.card s ∣ fintype.card Ξ± := by haveI := classical.prop_decidable; simp [card_eq_card_quotient_mul_card_subgroup s] lemma card_quotient_dvd_card (s : set Ξ±) [is_subgroup s] [decidable_pred s] [fintype s] : fintype.card (quotient s) ∣ fintype.card Ξ± := by simp [card_eq_card_quotient_mul_card_subgroup s] lemma exists_gpow_eq_one (a : Ξ±) : βˆƒiβ‰ 0, a ^ (i:β„€) = 1 := have Β¬ injective (Ξ»i:β„€, a ^ i), from not_injective_infinite_fintype _, let ⟨i, j, a_eq, ne⟩ := show βˆƒ(i j : β„€), a ^ i = a ^ j ∧ i β‰  j, by rw [injective] at this; simpa [classical.not_forall] in have a ^ (i - j) = 1, by simp [sub_eq_add_neg, gpow_add, gpow_neg, a_eq], ⟨i - j, sub_ne_zero.mpr ne, this⟩ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_pow_eq_one (a : Ξ±) : βˆƒi > 0, a ^ i = 1 := let ⟨i, hi, eq⟩ := exists_gpow_eq_one a in begin cases i, { exact ⟨i, nat.pos_of_ne_zero (by simp [int.of_nat_eq_coe, *] at *), eq⟩ }, { exact ⟨i + 1, dec_trivial, inv_eq_one.1 eq⟩ } end include dec /-- `order_of a` is the order of the element `a`, i.e. the `n β‰₯ 1`, s.t. `a ^ n = 1` -/ def order_of (a : Ξ±) : β„• := nat.find (exists_pow_eq_one a) lemma pow_order_of_eq_one (a : Ξ±) : a ^ order_of a = 1 := let ⟨h₁, hβ‚‚βŸ© := nat.find_spec (exists_pow_eq_one a) in hβ‚‚ lemma order_of_pos (a : Ξ±) : 0 < order_of a := let ⟨h₁, hβ‚‚βŸ© := nat.find_spec (exists_pow_eq_one a) in h₁ private lemma pow_injective_aux {n m : β„•} (a : Ξ±) (h : n ≀ m) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := decidable.by_contradiction $ assume ne : n β‰  m, have h₁ : m - n > 0, from nat.pos_of_ne_zero (by simp [nat.sub_eq_iff_eq_add h, ne.symm]), have hβ‚‚ : a ^ (m - n) = 1, by simp [pow_sub _ h, eq], have le : order_of a ≀ m - n, from nat.find_min' (exists_pow_eq_one a) ⟨h₁, hβ‚‚βŸ©, have lt : m - n < order_of a, from (nat.sub_lt_left_iff_lt_add h).mpr $ nat.lt_add_left _ _ _ hm, lt_irrefl _ (lt_of_le_of_lt le lt) lemma pow_injective_of_lt_order_of {n m : β„•} (a : Ξ±) (hn : n < order_of a) (hm : m < order_of a) (eq : a ^ n = a ^ m) : n = m := (le_total n m).elim (assume h, pow_injective_aux a h hn hm eq) (assume h, (pow_injective_aux a h hm hn eq.symm).symm) lemma order_of_le_card_univ : order_of a ≀ fintype.card Ξ± := finset.card_le_of_inj_on ((^) a) (assume n _, fintype.complete _) (assume i j, pow_injective_of_lt_order_of a) lemma pow_eq_mod_order_of {n : β„•} : a ^ n = a ^ (n % order_of a) := calc a ^ n = a ^ (n % order_of a + order_of a * (n / order_of a)) : by rw [nat.mod_add_div] ... = a ^ (n % order_of a) : by simp [pow_add, pow_mul, pow_order_of_eq_one] lemma gpow_eq_mod_order_of {i : β„€} : a ^ i = a ^ (i % order_of a) := calc a ^ i = a ^ (i % order_of a + order_of a * (i / order_of a)) : by rw [int.mod_add_div] ... = a ^ (i % order_of a) : by simp [gpow_add, gpow_mul, pow_order_of_eq_one] lemma mem_gpowers_iff_mem_range_order_of {a a' : Ξ±} : a' ∈ gpowers a ↔ a' ∈ (finset.range (order_of a)).image ((^) a : β„• β†’ Ξ±) := finset.mem_range_iff_mem_finset_range_of_mod_eq (order_of_pos a) (assume i, gpow_eq_mod_order_of.symm) instance decidable_gpowers : decidable_pred (gpowers a) := assume a', decidable_of_iff' (a' ∈ (finset.range (order_of a)).image ((^) a)) mem_gpowers_iff_mem_range_order_of lemma order_of_dvd_of_pow_eq_one {n : β„•} (h : a ^ n = 1) : order_of a ∣ n := by_contradiction (Ξ» h₁, nat.find_min _ (show n % order_of a < order_of a, from nat.mod_lt _ (order_of_pos _)) ⟨nat.pos_of_ne_zero (mt nat.dvd_of_mod_eq_zero h₁), by rwa ← pow_eq_mod_order_of⟩) lemma order_of_dvd_iff_pow_eq_one {n : β„•} : order_of a ∣ n ↔ a ^ n = 1 := ⟨λ h, by rw [pow_eq_mod_order_of, nat.mod_eq_zero_of_dvd h, pow_zero], order_of_dvd_of_pow_eq_one⟩ lemma order_of_le_of_pow_eq_one {n : β„•} (hn : 0 < n) (h : a ^ n = 1) : order_of a ≀ n := nat.find_min' (exists_pow_eq_one a) ⟨hn, h⟩ lemma sum_card_order_of_eq_card_pow_eq_one {n : β„•} (hn : 0 < n) : βˆ‘ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (Ξ» a : Ξ±, order_of a = m)).card = (finset.univ.filter (Ξ» a : Ξ±, a ^ n = 1)).card := calc βˆ‘ m in (finset.range n.succ).filter (∣ n), (finset.univ.filter (Ξ» a : Ξ±, order_of a = m)).card = _ : (finset.card_bind (by { intros, apply finset.disjoint_filter.2, cc })).symm ... = _ : congr_arg finset.card (finset.ext (begin assume a, suffices : order_of a ≀ n ∧ order_of a ∣ n ↔ a ^ n = 1, { simpa [nat.lt_succ_iff], }, exact ⟨λ h, let ⟨m, hm⟩ := h.2 in by rw [hm, pow_mul, pow_order_of_eq_one, _root_.one_pow], Ξ» h, ⟨order_of_le_of_pow_eq_one hn h, order_of_dvd_of_pow_eq_one h⟩⟩ end)) section local attribute [instance] set_fintype lemma order_eq_card_gpowers : order_of a = fintype.card (gpowers a) := begin refine (finset.card_eq_of_bijective _ _ _ _).symm, { exact Ξ»n hn, ⟨gpow a n, ⟨n, rfl⟩⟩ }, { exact assume ⟨_, i, rfl⟩ _, have pos: (0:int) < order_of a, from int.coe_nat_lt.mpr $ order_of_pos a, have 0 ≀ i % (order_of a), from int.mod_nonneg _ $ ne_of_gt pos, ⟨int.to_nat (i % order_of a), by rw [← int.coe_nat_lt, int.to_nat_of_nonneg this]; exact ⟨int.mod_lt_of_pos _ pos, subtype.eq gpow_eq_mod_order_of.symm⟩⟩ }, { intros, exact finset.mem_univ _ }, { exact assume i j hi hj eq, pow_injective_of_lt_order_of a hi hj $ by simpa using eq } end @[simp] lemma order_of_one : order_of (1 : Ξ±) = 1 := by rw [order_eq_card_gpowers, fintype.card_eq_one_iff]; exact ⟨⟨1, 0, rfl⟩, Ξ» ⟨a, i, ha⟩, by simp [ha.symm]⟩ @[simp] lemma order_of_eq_one_iff : order_of a = 1 ↔ a = 1 := ⟨λ h, by conv { to_lhs, rw [← pow_one a, ← h, pow_order_of_eq_one] }, Ξ» h, by simp [h]⟩ lemma order_of_eq_prime {p : β„•} [hp : fact p.prime] (hg : a^p = 1) (hg1 : a β‰  1) : order_of a = p := (hp.2 _ (order_of_dvd_of_pow_eq_one hg)).resolve_left (mt order_of_eq_one_iff.1 hg1) section classical open_locale classical open quotient_group /- TODO: use cardinal theory, introduce `card : set Ξ± β†’ β„•`, or setup decidability for cosets -/ lemma order_of_dvd_card_univ : order_of a ∣ fintype.card Ξ± := have ft_prod : fintype (quotient (gpowers a) Γ— (gpowers a)), from fintype.of_equiv Ξ± (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup, have ft_s : fintype (gpowers a), from @fintype.fintype_prod_right _ _ _ ft_prod _, have ft_cosets : fintype (quotient (gpowers a)), from @fintype.fintype_prod_left _ _ _ ft_prod ⟨⟨1, is_submonoid.one_mem⟩⟩, have ft : fintype (quotient (gpowers a) Γ— (gpowers a)), from @prod.fintype _ _ ft_cosets ft_s, have eq₁ : fintype.card Ξ± = @fintype.card _ ft_cosets * @fintype.card _ ft_s, from calc fintype.card Ξ± = @fintype.card _ ft_prod : @fintype.card_congr _ _ _ ft_prod (gpowers.is_subgroup a).group_equiv_quotient_times_subgroup ... = @fintype.card _ (@prod.fintype _ _ ft_cosets ft_s) : congr_arg (@fintype.card _) $ subsingleton.elim _ _ ... = @fintype.card _ ft_cosets * @fintype.card _ ft_s : @fintype.card_prod _ _ ft_cosets ft_s, have eqβ‚‚ : order_of a = @fintype.card _ ft_s, from calc order_of a = _ : order_eq_card_gpowers ... = _ : congr_arg (@fintype.card _) $ subsingleton.elim _ _, dvd.intro (@fintype.card (quotient (gpowers a)) ft_cosets) $ by rw [eq₁, eqβ‚‚, mul_comm] omit dec @[simp] lemma pow_card_eq_one (a : Ξ±) : a ^ fintype.card Ξ± = 1 := let ⟨m, hm⟩ := @order_of_dvd_card_univ _ a _ _ _ in by simp [hm, pow_mul, pow_order_of_eq_one] lemma powers_eq_gpowers (a : Ξ±) : powers a = gpowers a := set.ext (Ξ» x, ⟨λ ⟨n, hn⟩, ⟨n, by simp * at *⟩, Ξ» ⟨i, hi⟩, ⟨(i % order_of a).nat_abs, by rwa [← 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]⟩⟩) end classical open nat lemma order_of_pow (a : Ξ±) (n : β„•) : order_of (a ^ n) = order_of a / gcd (order_of a) n := dvd_antisymm (order_of_dvd_of_pow_eq_one (by rw [← pow_mul, ← nat.mul_div_assoc _ (gcd_dvd_left _ _), mul_comm, nat.mul_div_assoc _ (gcd_dvd_right _ _), pow_mul, pow_order_of_eq_one, _root_.one_pow])) (have gcd_pos : 0 < gcd (order_of a) n, from gcd_pos_of_pos_left n (order_of_pos a), have hdvd : order_of a ∣ n * order_of (a ^ n), from order_of_dvd_of_pow_eq_one (by rw [pow_mul, pow_order_of_eq_one]), coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd gcd_pos) (dvd_of_mul_dvd_mul_right gcd_pos (by rwa [nat.div_mul_cancel (gcd_dvd_left _ _), mul_assoc, nat.div_mul_cancel (gcd_dvd_right _ _), mul_comm]))) lemma image_range_order_of (a : Ξ±) : finset.image (Ξ» i, a ^ i) (finset.range (order_of a)) = (gpowers a).to_finset := by { ext x, rw [set.mem_to_finset, mem_gpowers_iff_mem_range_order_of] } omit dec open_locale classical lemma pow_gcd_card_eq_one_iff {n : β„•} {a : Ξ±} : a ^ n = 1 ↔ a ^ (gcd n (fintype.card Ξ±)) = 1 := ⟨λ h, have hn : order_of a ∣ n, from dvd_of_mod_eq_zero $ by_contradiction (Ξ» ha, by rw pow_eq_mod_order_of at h; exact (not_le_of_gt (nat.mod_lt n (order_of_pos a))) (order_of_le_of_pow_eq_one (nat.pos_of_ne_zero ha) h)), let ⟨m, hm⟩ := dvd_gcd hn order_of_dvd_card_univ in by rw [hm, pow_mul, pow_order_of_eq_one, _root_.one_pow], Ξ» h, let ⟨m, hm⟩ := gcd_dvd_left n (fintype.card Ξ±) in by rw [hm, pow_mul, h, _root_.one_pow]⟩ end end order_of section cyclic local attribute [instance] set_fintype /-- A group is called *cyclic* if it is generated by a single element. -/ class is_cyclic (Ξ± : Type*) [group Ξ±] : Prop := (exists_generator [] : βˆƒ g : Ξ±, βˆ€ x, x ∈ gpowers g) /-- A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `comm_group`. -/ def is_cyclic.comm_group [hg : group Ξ±] [is_cyclic Ξ±] : comm_group Ξ± := { mul_comm := Ξ» x y, show x * y = y * x, from let ⟨g, hg⟩ := is_cyclic.exists_generator Ξ± in let ⟨n, hn⟩ := hg x in let ⟨m, hm⟩ := hg y in hm β–Έ hn β–Έ gpow_mul_comm _ _ _, ..hg } lemma is_cyclic_of_order_of_eq_card [group Ξ±] [fintype Ξ±] [decidable_eq Ξ±] (x : Ξ±) (hx : order_of x = fintype.card Ξ±) : is_cyclic Ξ± := ⟨⟨x, set.eq_univ_iff_forall.1 $ set.eq_of_subset_of_card_le (set.subset_univ _) (by rw [fintype.card_congr (equiv.set.univ Ξ±), ← hx, order_eq_card_gpowers])⟩⟩ lemma order_of_eq_card_of_forall_mem_gpowers [group Ξ±] [fintype Ξ±] [decidable_eq Ξ±] {g : Ξ±} (hx : βˆ€ x, x ∈ gpowers g) : order_of g = fintype.card Ξ± := by rw [← fintype.card_congr (equiv.set.univ Ξ±), order_eq_card_gpowers]; simp [hx]; congr instance [group Ξ±] : is_cyclic (is_subgroup.trivial Ξ±) := ⟨⟨(1 : is_subgroup.trivial Ξ±), Ξ» x, ⟨0, subtype.eq $ eq.symm (is_subgroup.mem_trivial.1 x.2)⟩⟩⟩ instance is_subgroup.is_cyclic [group Ξ±] [is_cyclic Ξ±] (H : set Ξ±) [is_subgroup H] : is_cyclic H := by haveI := classical.prop_decidable; exact let ⟨g, hg⟩ := is_cyclic.exists_generator Ξ± in if hx : βˆƒ (x : Ξ±), x ∈ H ∧ x β‰  (1 : Ξ±) then let ⟨x, hx₁, hxβ‚‚βŸ© := hx in let ⟨k, hk⟩ := hg x in have hex : βˆƒ n : β„•, 0 < n ∧ g ^ n ∈ H, from ⟨k.nat_abs, nat.pos_of_ne_zero (Ξ» h, hxβ‚‚ $ by rw [← hk, int.eq_zero_of_nat_abs_eq_zero h, gpow_zero]), match k, hk with | (k : β„•), hk := by rw [int.nat_abs_of_nat, ← gpow_coe_nat, hk]; exact hx₁ | -[1+ k], hk := by rw [int.nat_abs_of_neg_succ_of_nat, ← is_subgroup.inv_mem_iff H]; simp * at * end⟩, ⟨⟨⟨g ^ nat.find hex, (nat.find_spec hex).2⟩, Ξ» ⟨x, hx⟩, let ⟨k, hk⟩ := hg x in have hk₁ : g ^ ((nat.find hex : β„€) * (k / nat.find hex)) ∈ gpowers (g ^ nat.find hex), from ⟨k / nat.find hex, eq.symm $ gpow_mul _ _ _⟩, have hkβ‚‚ : g ^ ((nat.find hex : β„€) * (k / nat.find hex)) ∈ H, by rw gpow_mul; exact is_subgroup.gpow_mem (nat.find_spec hex).2, have hk₃ : g ^ (k % nat.find hex) ∈ H, from (is_subgroup.mul_mem_cancel_right H hkβ‚‚).1 $ by rw [← gpow_add, int.mod_add_div, hk]; exact hx, have hkβ‚„ : k % nat.find hex = (k % nat.find hex).nat_abs, by rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (nat.find_spec hex).1)), have hkβ‚… : g ^ (k % nat.find hex ).nat_abs ∈ H, by rwa [← gpow_coe_nat, ← hkβ‚„], have hk₆ : (k % (nat.find hex : β„€)).nat_abs = 0, from by_contradiction (Ξ» h, nat.find_min hex (int.coe_nat_lt.1 $ by rw [← hkβ‚„]; exact int.mod_lt_of_pos _ (int.coe_nat_pos.2 (nat.find_spec hex).1)) ⟨nat.pos_of_ne_zero h, hkβ‚…βŸ©), ⟨k / (nat.find hex : β„€), subtype.ext_iff_val.2 begin suffices : g ^ ((nat.find hex : β„€) * (k / nat.find hex)) = x, { simpa [gpow_mul] }, rw [int.mul_div_cancel' (int.dvd_of_mod_eq_zero (int.eq_zero_of_nat_abs_eq_zero hk₆)), hk] end⟩⟩⟩ else have H = is_subgroup.trivial Ξ±, from set.ext $ Ξ» x, ⟨λ h, by simp at *; tauto, Ξ» h, by rw [is_subgroup.mem_trivial.1 h]; exact is_submonoid.one_mem⟩, by clear _let_match; substI this; apply_instance open finset nat lemma is_cyclic.card_pow_eq_one_le [group Ξ±] [fintype Ξ±] [decidable_eq Ξ±] [is_cyclic Ξ±] {n : β„•} (hn0 : 0 < n) : (univ.filter (Ξ» a : Ξ±, a ^ n = 1)).card ≀ n := let ⟨g, hg⟩ := is_cyclic.exists_generator Ξ± in calc (univ.filter (Ξ» a : Ξ±, a ^ n = 1)).card ≀ (gpowers (g ^ (fintype.card Ξ± / (gcd n (fintype.card Ξ±))))).to_finset.card : card_le_of_subset (Ξ» x hx, let ⟨m, hm⟩ := show x ∈ powers g, from (powers_eq_gpowers g).symm β–Έ hg x in set.mem_to_finset.2 ⟨(m / (fintype.card Ξ± / (gcd n (fintype.card Ξ±))) : β„•), have hgmn : g ^ (m * gcd n (fintype.card Ξ±)) = 1, by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2, begin rw [gpow_coe_nat, ← pow_mul, nat.mul_div_cancel_left', hm], refine dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (fintype.card Ξ±) hn0) _, conv {to_lhs, rw [nat.div_mul_cancel (gcd_dvd_right _ _), ← order_of_eq_card_of_forall_mem_gpowers hg]}, exact order_of_dvd_of_pow_eq_one hgmn end⟩) ... ≀ n : let ⟨m, hm⟩ := gcd_dvd_right n (fintype.card Ξ±) in have hm0 : 0 < m, from nat.pos_of_ne_zero (Ξ» hm0, (by rw [hm0, mul_zero, fintype.card_eq_zero_iff] at hm; exact hm 1)), begin rw [← fintype.card_of_finset' _ (Ξ» _, set.mem_to_finset), ← order_eq_card_gpowers, order_of_pow, order_of_eq_card_of_forall_mem_gpowers hg], rw [hm] {occs := occurrences.pos [2,3]}, rw [nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left, hm, nat.mul_div_cancel _ hm0], exact le_of_dvd hn0 (gcd_dvd_left _ _) end lemma is_cyclic.exists_monoid_generator (Ξ± : Type*) [group Ξ±] [fintype Ξ±] [is_cyclic Ξ±] : βˆƒ x : Ξ±, βˆ€ y : Ξ±, y ∈ powers x := by simp only [powers_eq_gpowers]; exact is_cyclic.exists_generator Ξ± section variables [group Ξ±] [fintype Ξ±] [decidable_eq Ξ±] lemma is_cyclic.image_range_order_of (ha : βˆ€ x : Ξ±, x ∈ gpowers a) : finset.image (Ξ» i, a ^ i) (range (order_of a)) = univ := begin simp only [image_range_order_of, set.eq_univ_iff_forall.mpr ha], convert set.to_finset_univ end lemma is_cyclic.image_range_card (ha : βˆ€ x : Ξ±, x ∈ gpowers a) : finset.image (Ξ» i, a ^ i) (range (fintype.card Ξ±)) = univ := by rw [← order_of_eq_card_of_forall_mem_gpowers ha, is_cyclic.image_range_order_of ha] end section totient variables [group Ξ±] [fintype Ξ±] [decidable_eq Ξ±] (hn : βˆ€ n : β„•, 0 < n β†’ (univ.filter (Ξ» a : Ξ±, a ^ n = 1)).card ≀ n) include hn lemma card_pow_eq_one_eq_order_of_aux (a : Ξ±) : (finset.univ.filter (Ξ» b : Ξ±, b ^ order_of a = 1)).card = order_of a := le_antisymm (hn _ (order_of_pos _)) (calc order_of a = @fintype.card (gpowers a) (id _) : order_eq_card_gpowers ... ≀ @fintype.card (↑(univ.filter (Ξ» b : Ξ±, b ^ order_of a = 1)) : set Ξ±) (fintype.of_finset _ (Ξ» _, iff.rfl)) : @fintype.card_le_of_injective (gpowers a) (↑(univ.filter (Ξ» b : Ξ±, b ^ order_of a = 1)) : set Ξ±) (id _) (id _) (Ξ» b, ⟨b.1, mem_filter.2 ⟨mem_univ _, let ⟨i, hi⟩ := b.2 in by rw [← hi, ← gpow_coe_nat, ← gpow_mul, mul_comm, gpow_mul, gpow_coe_nat, pow_order_of_eq_one, one_gpow]⟩⟩) (Ξ» _ _ h, subtype.eq (subtype.mk.inj h)) ... = (univ.filter (Ξ» b : Ξ±, b ^ order_of a = 1)).card : fintype.card_of_finset _ _) open_locale nat -- use Ο† for nat.totient private lemma card_order_of_eq_totient_aux₁ : βˆ€ {d : β„•}, d ∣ fintype.card Ξ± β†’ 0 < (univ.filter (Ξ» a : Ξ±, order_of a = d)).card β†’ (univ.filter (Ξ» a : Ξ±, order_of a = d)).card = Ο† d | 0 := Ξ» hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in absurd (mem_filter.1 ha).2 $ ne_of_gt $ order_of_pos a | (d+1) := Ξ» hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in have ha : order_of a = d.succ, from (mem_filter.1 ha).2, have h : βˆ‘ m in (range d.succ).filter (∣ d.succ), (univ.filter (Ξ» a : Ξ±, order_of a = m)).card = βˆ‘ m in (range d.succ).filter (∣ d.succ), Ο† m, from finset.sum_congr rfl (Ξ» m hm, have hmd : m < d.succ, from mem_range.1 (mem_filter.1 hm).1, have hm : m ∣ d.succ, from (mem_filter.1 hm).2, card_order_of_eq_totient_aux₁ (dvd.trans hm hd) (finset.card_pos.2 ⟨a ^ (d.succ / m), mem_filter.2 ⟨mem_univ _, by rw [order_of_pow, ha, gcd_eq_right (div_dvd_of_dvd hm), nat.div_div_self hm (succ_pos _)]⟩⟩)), have hinsert : insert d.succ ((range d.succ).filter (∣ d.succ)) = (range d.succ.succ).filter (∣ d.succ), from (finset.ext $ Ξ» x, ⟨λ h, (mem_insert.1 h).elim (Ξ» h, by simp [h, range_succ]) (by clear _let_match; simp [range_succ]; tauto), by clear _let_match; simp [range_succ] {contextual := tt}; tauto⟩), have hinsert₁ : d.succ βˆ‰ (range d.succ).filter (∣ d.succ), by simp [mem_range, zero_le_one, le_succ], (add_left_inj (βˆ‘ m in (range d.succ).filter (∣ d.succ), (univ.filter (Ξ» a : Ξ±, order_of a = m)).card)).1 (calc _ = βˆ‘ m in insert d.succ (filter (∣ d.succ) (range d.succ)), (univ.filter (Ξ» a : Ξ±, order_of a = m)).card : eq.symm (finset.sum_insert (by simp [mem_range, zero_le_one, le_succ])) ... = βˆ‘ m in (range d.succ.succ).filter (∣ d.succ), (univ.filter (Ξ» a : Ξ±, order_of a = m)).card : sum_congr hinsert (Ξ» _ _, rfl) ... = (univ.filter (Ξ» a : Ξ±, a ^ d.succ = 1)).card : sum_card_order_of_eq_card_pow_eq_one (succ_pos d) ... = βˆ‘ m in (range d.succ.succ).filter (∣ d.succ), Ο† m : ha β–Έ (card_pow_eq_one_eq_order_of_aux hn a).symm β–Έ (sum_totient _).symm ... = _ : by rw [h, ← sum_insert hinsert₁]; exact finset.sum_congr hinsert.symm (Ξ» _ _, rfl)) lemma card_order_of_eq_totient_auxβ‚‚ {d : β„•} (hd : d ∣ fintype.card Ξ±) : (univ.filter (Ξ» a : Ξ±, order_of a = d)).card = Ο† d := by_contradiction $ Ξ» h, have h0 : (univ.filter (Ξ» a : Ξ± , order_of a = d)).card = 0 := not_not.1 (mt nat.pos_iff_ne_zero.2 (mt (card_order_of_eq_totient_aux₁ hn hd) h)), let c := fintype.card Ξ± in have hc0 : 0 < c, from fintype.card_pos_iff.2 ⟨1⟩, lt_irrefl c $ calc c = (univ.filter (Ξ» a : Ξ±, a ^ c = 1)).card : congr_arg card $ by simp [finset.ext_iff, c] ... = βˆ‘ m in (range c.succ).filter (∣ c), (univ.filter (Ξ» a : Ξ±, order_of a = m)).card : (sum_card_order_of_eq_card_pow_eq_one hc0).symm ... = βˆ‘ m in ((range c.succ).filter (∣ c)).erase d, (univ.filter (Ξ» a : Ξ±, order_of a = m)).card : eq.symm (sum_subset (erase_subset _ _) (Ξ» m hm₁ hmβ‚‚, have m = d, by simp at *; cc, by simp [*, finset.ext_iff] at *; exact h0)) ... ≀ βˆ‘ m in ((range c.succ).filter (∣ c)).erase d, Ο† m : sum_le_sum (Ξ» m hm, have hmc : m ∣ c, by simp at hm; tauto, (imp_iff_not_or.1 (card_order_of_eq_totient_aux₁ hn hmc)).elim (Ξ» h, by simp [nat.le_zero_iff.1 (le_of_not_gt h), nat.zero_le]) (Ξ» h, by rw h)) ... < Ο† d + βˆ‘ m in ((range c.succ).filter (∣ c)).erase d, Ο† m : lt_add_of_pos_left _ (totient_pos (nat.pos_of_ne_zero (Ξ» h, nat.pos_iff_ne_zero.1 hc0 (eq_zero_of_zero_dvd $ h β–Έ hd)))) ... = βˆ‘ m in insert d (((range c.succ).filter (∣ c)).erase d), Ο† m : eq.symm (sum_insert (by simp)) ... = βˆ‘ m in (range c.succ).filter (∣ c), Ο† m : finset.sum_congr (finset.insert_erase (mem_filter.2 ⟨mem_range.2 (lt_succ_of_le (le_of_dvd hc0 hd)), hd⟩)) (Ξ» _ _, rfl) ... = c : sum_totient _ lemma is_cyclic_of_card_pow_eq_one_le : is_cyclic Ξ± := have (univ.filter (Ξ» a : Ξ±, order_of a = fintype.card Ξ±)).nonempty, from (card_pos.1 $ by rw [card_order_of_eq_totient_auxβ‚‚ hn (dvd_refl _)]; exact totient_pos (fintype.card_pos_iff.2 ⟨1⟩)), let ⟨x, hx⟩ := this in is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2 end totient lemma is_cyclic.card_order_of_eq_totient [group Ξ±] [is_cyclic Ξ±] [fintype Ξ±] [decidable_eq Ξ±] {d : β„•} (hd : d ∣ fintype.card Ξ±) : (univ.filter (Ξ» a : Ξ±, order_of a = d)).card = totient d := card_order_of_eq_totient_auxβ‚‚ (Ξ» n, is_cyclic.card_pow_eq_one_le) hd end cyclic
0af4aafda1bfdefa6f5f014bbd404a17f487a6c5
abd85493667895c57a7507870867b28124b3998f
/src/data/equiv/mul_add.lean
9f0510a013c9266b33240c77a64d27888da57aea
[ "Apache-2.0" ]
permissive
pechersky/mathlib
d56eef16bddb0bfc8bc552b05b7270aff5944393
f1df14c2214ee114c9738e733efd5de174deb95d
refs/heads/master
1,666,714,392,571
1,591,747,567,000
1,591,747,567,000
270,557,274
0
0
Apache-2.0
1,591,597,975,000
1,591,597,974,000
null
UTF-8
Lean
false
false
12,082
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, Callum Sutton, Yury Kudryashov -/ import data.equiv.basic import deprecated.group /-! # Multiplicative and additive equivs In this file we define two extensions of `equiv` called `add_equiv` and `mul_equiv`, which are datatypes representing isomorphisms of `add_monoid`s/`add_group`s and `monoid`s/`group`s. We also introduce the corresponding groups of automorphisms `add_aut` and `mul_aut`. ## Notations The extended equivs all have coercions to functions, and the coercions are the canonical notation when treating the isomorphisms as maps. ## Implementation notes The fields for `mul_equiv`, `add_equiv` now avoid the unbundled `is_mul_hom` and `is_add_hom`, as these are deprecated. Definition of multiplication in the groups of automorphisms agrees with function composition, multiplication in `equiv.perm`, and multiplication in `category_theory.End`, not with `category_theory.comp`. ## Tags equiv, mul_equiv, add_equiv, mul_aut, add_aut -/ variables {A : Type*} {B : Type*} {M : Type*} {N : Type*} {P : Type*} {G : Type*} {H : Type*} set_option old_structure_cmd true /-- add_equiv Ξ± Ξ² is the type of an equiv Ξ± ≃ Ξ² which preserves addition. -/ structure add_equiv (A B : Type*) [has_add A] [has_add B] extends A ≃ B := (map_add' : βˆ€ x y : A, to_fun (x + y) = to_fun x + to_fun y) /-- `mul_equiv Ξ± Ξ²` is the type of an equiv `Ξ± ≃ Ξ²` which preserves multiplication. -/ @[to_additive] structure mul_equiv (M N : Type*) [has_mul M] [has_mul N] extends M ≃ N := (map_mul' : βˆ€ x y : M, to_fun (x * y) = to_fun x * to_fun y) infix ` ≃* `:25 := mul_equiv infix ` ≃+ `:25 := add_equiv namespace mul_equiv @[to_additive] instance [has_mul M] [has_mul N] : has_coe_to_fun (M ≃* N) := ⟨_, mul_equiv.to_fun⟩ variables [has_mul M] [has_mul N] [has_mul P] @[simp, to_additive] lemma to_fun_apply {f : M ≃* N} {m : M} : f.to_fun m = f m := rfl /-- A multiplicative isomorphism preserves multiplication (canonical form). -/ @[to_additive] lemma map_mul (f : M ≃* N) : βˆ€ x y, f (x * y) = f x * f y := f.map_mul' /-- A multiplicative isomorphism preserves multiplication (deprecated). -/ @[to_additive] instance (h : M ≃* N) : is_mul_hom h := ⟨h.map_mul⟩ /-- Makes a multiplicative isomorphism from a bijection which preserves multiplication. -/ @[to_additive] def mk' (f : M ≃ N) (h : βˆ€ x y, f (x * y) = f x * f y) : M ≃* N := ⟨f.1, f.2, f.3, f.4, h⟩ /-- The identity map is a multiplicative isomorphism. -/ @[refl, to_additive] def refl (M : Type*) [has_mul M] : M ≃* M := { map_mul' := Ξ» _ _, rfl, ..equiv.refl _} /-- The inverse of an isomorphism is an isomorphism. -/ @[symm, to_additive] def symm (h : M ≃* N) : N ≃* M := { map_mul' := Ξ» n₁ nβ‚‚, h.left_inv.injective begin show h.to_equiv (h.to_equiv.symm (n₁ * nβ‚‚)) = h ((h.to_equiv.symm n₁) * (h.to_equiv.symm nβ‚‚)), rw h.map_mul, show _ = h.to_equiv (_) * h.to_equiv (_), rw [h.to_equiv.apply_symm_apply, h.to_equiv.apply_symm_apply, h.to_equiv.apply_symm_apply], end, ..h.to_equiv.symm} @[simp, to_additive] theorem to_equiv_symm (f : M ≃* N) : f.symm.to_equiv = f.to_equiv.symm := rfl @[simp, to_additive] theorem coe_mk (f : M β†’ N) (g h₁ hβ‚‚ h₃) : ⇑(mul_equiv.mk f g h₁ hβ‚‚ h₃) = f := rfl @[simp, to_additive] theorem coe_symm_mk (f : M β†’ N) (g h₁ hβ‚‚ h₃) : ⇑(mul_equiv.mk f g h₁ hβ‚‚ h₃).symm = g := rfl /-- Transitivity of multiplication-preserving isomorphisms -/ @[trans, to_additive] def trans (h1 : M ≃* N) (h2 : N ≃* P) : (M ≃* P) := { map_mul' := Ξ» x y, show h2 (h1 (x * y)) = h2 (h1 x) * h2 (h1 y), by rw [h1.map_mul, h2.map_mul], ..h1.to_equiv.trans h2.to_equiv } /-- e.right_inv in canonical form -/ @[simp, to_additive] lemma apply_symm_apply (e : M ≃* N) : βˆ€ y, e (e.symm y) = y := e.to_equiv.apply_symm_apply /-- e.left_inv in canonical form -/ @[simp, to_additive] lemma symm_apply_apply (e : M ≃* N) : βˆ€ x, e.symm (e x) = x := e.to_equiv.symm_apply_apply /-- a multiplicative equiv of monoids sends 1 to 1 (and is hence a monoid isomorphism) -/ @[simp, to_additive] lemma map_one {M N} [monoid M] [monoid N] (h : M ≃* N) : h 1 = 1 := by rw [←mul_one (h 1), ←h.apply_symm_apply 1, ←h.map_mul, one_mul] @[simp, to_additive] lemma map_eq_one_iff {M N} [monoid M] [monoid N] (h : M ≃* N) {x : M} : h x = 1 ↔ x = 1 := h.map_one β–Έ h.to_equiv.apply_eq_iff_eq x 1 @[to_additive] lemma map_ne_one_iff {M N} [monoid M] [monoid N] (h : M ≃* N) {x : M} : h x β‰  1 ↔ x β‰  1 := ⟨mt h.map_eq_one_iff.2, mt h.map_eq_one_iff.1⟩ /-- Extract the forward direction of a multiplicative equivalence as a multiplication preserving function. -/ @[to_additive to_add_monoid_hom] def to_monoid_hom {M N} [monoid M] [monoid N] (h : M ≃* N) : (M β†’* N) := { map_one' := h.map_one, .. h } @[simp, to_additive] lemma to_monoid_hom_apply {M N} [monoid M] [monoid N] (e : M ≃* N) (x : M) : e.to_monoid_hom x = e x := rfl /-- A multiplicative equivalence of groups preserves inversion. -/ @[to_additive] lemma map_inv [group G] [group H] (h : G ≃* H) (x : G) : h x⁻¹ = (h x)⁻¹ := h.to_monoid_hom.map_inv x /-- A multiplicative bijection between two monoids is a monoid hom (deprecated -- use to_monoid_hom). -/ @[to_additive is_add_monoid_hom] instance is_monoid_hom {M N} [monoid M] [monoid N] (h : M ≃* N) : is_monoid_hom h := ⟨h.map_one⟩ /-- A multiplicative bijection between two groups is a group hom (deprecated -- use to_monoid_hom). -/ @[to_additive is_add_group_hom] instance is_group_hom {G H} [group G] [group H] (h : G ≃* H) : is_group_hom h := { map_mul := h.map_mul } /-- Two multiplicative isomorphisms agree if they are defined by the same underlying function. -/ @[ext, to_additive "Two additive isomorphisms agree if they are defined by the same underlying function."] lemma ext {f g : mul_equiv M N} (h : βˆ€ x, f x = g x) : f = g := begin have h₁ : f.to_equiv = g.to_equiv := equiv.ext h, cases f, cases g, congr, { exact (funext h) }, { exact congr_arg equiv.inv_fun h₁ } end attribute [ext] add_equiv.ext end mul_equiv /-- An additive equivalence of additive groups preserves subtraction. -/ lemma add_equiv.map_sub [add_group A] [add_group B] (h : A ≃+ B) (x y : A) : h (x - y) = h x - h y := h.to_add_monoid_hom.map_sub x y /-- The group of multiplicative automorphisms. -/ @[to_additive "The group of additive automorphisms."] def mul_aut (M : Type*) [has_mul M] := M ≃* M attribute [reducible] mul_aut add_aut namespace mul_aut variables (M) [has_mul M] /-- The group operation on multiplicative automorphisms is defined by `Ξ» g h, mul_equiv.trans h g`. This means that multiplication agrees with composition, `(g*h)(x) = g (h x)`. -/ instance : group (mul_aut M) := by refine_struct { mul := Ξ» g h, mul_equiv.trans h g, one := mul_equiv.refl M, inv := mul_equiv.symm }; intros; ext; try { refl }; apply equiv.left_inv instance : inhabited (mul_aut M) := ⟨1⟩ @[simp] lemma coe_mul (e₁ eβ‚‚ : mul_aut M) : ⇑(e₁ * eβ‚‚) = e₁ ∘ eβ‚‚ := rfl @[simp] lemma coe_one : ⇑(1 : mul_aut M) = id := rfl /-- Monoid hom from the group of multiplicative automorphisms to the group of permutations. -/ def to_perm : mul_aut M β†’* equiv.perm M := by refine_struct { to_fun := mul_equiv.to_equiv }; intros; refl end mul_aut namespace add_aut variables (A) [has_add A] /-- The group operation on additive automorphisms is defined by `Ξ» g h, mul_equiv.trans h g`. This means that multiplication agrees with composition, `(g*h)(x) = g (h x)`. -/ instance group : group (add_aut A) := by refine_struct { mul := Ξ» g h, add_equiv.trans h g, one := add_equiv.refl A, inv := add_equiv.symm }; intros; ext; try { refl }; apply equiv.left_inv instance : inhabited (add_aut A) := ⟨1⟩ @[simp] lemma coe_mul (e₁ eβ‚‚ : add_aut A) : ⇑(e₁ * eβ‚‚) = e₁ ∘ eβ‚‚ := rfl @[simp] lemma coe_one : ⇑(1 : add_aut A) = id := rfl /-- Monoid hom from the group of multiplicative automorphisms to the group of permutations. -/ def to_perm : add_aut A β†’* equiv.perm A := by refine_struct { to_fun := add_equiv.to_equiv }; intros; refl end add_aut /-- A group is isomorphic to its group of units. -/ def to_units (G) [group G] : G ≃* units G := { to_fun := Ξ» x, ⟨x, x⁻¹, mul_inv_self _, inv_mul_self _⟩, inv_fun := coe, left_inv := Ξ» x, rfl, right_inv := Ξ» u, units.ext rfl, map_mul' := Ξ» x y, units.ext rfl } namespace units variables [monoid M] [monoid N] [monoid P] /-- A multiplicative equivalence of monoids defines a multiplicative equivalence of their groups of units. -/ def map_equiv (h : M ≃* N) : units M ≃* units N := { inv_fun := map h.symm.to_monoid_hom, left_inv := Ξ» u, ext $ h.left_inv u, right_inv := Ξ» u, ext $ h.right_inv u, .. map h.to_monoid_hom } end units namespace equiv section group variables [group G] @[to_additive] protected def mul_left (a : G) : perm G := { to_fun := Ξ»x, a * x, inv_fun := Ξ»x, a⁻¹ * x, left_inv := assume x, show a⁻¹ * (a * x) = x, from inv_mul_cancel_left a x, right_inv := assume x, show a * (a⁻¹ * x) = x, from mul_inv_cancel_left a x } @[simp, to_additive] lemma coe_mul_left (a : G) : ⇑(equiv.mul_left a) = (*) a := rfl @[simp, to_additive] lemma mul_left_symm (a : G) : (equiv.mul_left a).symm = equiv.mul_left a⁻¹ := ext $ Ξ» x, rfl @[to_additive] protected def mul_right (a : G) : perm G := { to_fun := Ξ»x, x * a, inv_fun := Ξ»x, x * a⁻¹, left_inv := assume x, show (x * a) * a⁻¹ = x, from mul_inv_cancel_right x a, right_inv := assume x, show (x * a⁻¹) * a = x, from inv_mul_cancel_right x a } @[simp, to_additive] lemma coe_mul_right (a : G) : ⇑(equiv.mul_right a) = Ξ» x, x * a := rfl @[simp, to_additive] lemma mul_right_symm (a : G) : (equiv.mul_right a).symm = equiv.mul_right a⁻¹ := ext $ Ξ» x, rfl variable (G) @[to_additive] protected def inv : perm G := { to_fun := Ξ»a, a⁻¹, inv_fun := Ξ»a, a⁻¹, left_inv := assume a, inv_inv a, right_inv := assume a, inv_inv a } variable {G} @[simp, to_additive] lemma coe_inv : ⇑(equiv.inv G) = has_inv.inv := rfl @[simp, to_additive] lemma inv_symm : (equiv.inv G).symm = equiv.inv G := rfl end group section point_reflection variables [add_comm_group A] (x y : A) /-- Point reflection in `x` as a permutation. -/ def point_reflection (x : A) : perm A := (equiv.neg A).trans (equiv.add_left (x + x)) lemma point_reflection_apply : point_reflection x y = x + x - y := rfl @[simp] lemma point_reflection_self : point_reflection x x = x := add_sub_cancel _ _ lemma point_reflection_involutive : function.involutive (point_reflection x : A β†’ A) := Ξ» y, by simp only [point_reflection_apply, sub_sub_cancel] @[simp] lemma point_reflection_symm : (point_reflection x).symm = point_reflection x := by { ext y, rw [symm_apply_eq, point_reflection_involutive x y] } /-- `x` is the only fixed point of `point_reflection x`. This lemma requires `x + x = y + y ↔ x = y`. There is no typeclass to use here, so we add it as an explicit argument. -/ lemma point_reflection_fixed_iff_of_bit0_inj {x y : A} (h : function.injective (bit0 : A β†’ A)) : point_reflection x y = y ↔ y = x := sub_eq_iff_eq_add.trans $ h.eq_iff.trans eq_comm end point_reflection end equiv section type_tags /-- Reinterpret `f : G ≃+ H` as `multiplicative G ≃* multiplicative H`. -/ def add_equiv.to_multiplicative [add_monoid G] [add_monoid H] (f : G ≃+ H) : multiplicative G ≃* multiplicative H := ⟨f.to_add_monoid_hom.to_multiplicative, f.symm.to_add_monoid_hom.to_multiplicative, f.3, f.4, f.5⟩ /-- Reinterpret `f : G ≃* H` as `additive G ≃+ additive H`. -/ def mul_equiv.to_additive [monoid G] [monoid H] (f : G ≃* H) : additive G ≃+ additive H := ⟨f.to_monoid_hom.to_additive, f.symm.to_monoid_hom.to_additive, f.3, f.4, f.5⟩ end type_tags
a2b8290c2d47dbc360fe132c4686d171dc71fe48
534c92d7322a8676cfd1583e26f5946134561b54
/src/Exercises/01_Propositions/Q0107/Q0007.lean
0b7284bc4aef2f1c35690667a69af66eb05e1c21
[ "Apache-2.0" ]
permissive
kbuzzard/mathematics-in-lean
53f387174f04d6077f434e27c407aee9425837f7
3fad7bb7e888dabef94921101af8671b78a4304a
refs/heads/master
1,586,812,457,439
1,546,893,744,000
1,546,893,744,000
163,450,734
8
0
null
null
null
null
UTF-8
Lean
false
false
67
lean
theorem false_implies_false : false β†’ false := begin sorry end
48a508fd503226f25fdea84a78be1fa5067f468c
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/Elab/Match.lean
b563c849afa20a0bbf456714a2c36d659a096e5b
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
38,740
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.Match.MatchPatternAttr import Lean.Meta.Match.Match import Lean.Elab.SyntheticMVars import Lean.Elab.App namespace Lean.Elab.Term open Meta /- This modules assumes "match"-expressions use the following syntax. ```lean def matchAlt : Parser := nodeWithAntiquot "matchAlt" `Lean.Parser.Term.matchAlt $ sepBy1 termParser ", " >> darrow >> termParser def matchAlts (optionalFirstBar := true) : Parser := group $ withPosition $ fun pos => (if optionalFirstBar then optional "| " else "| ") >> sepBy1 matchAlt (checkColGe pos.column "alternatives must be indented" >> "|") def matchDiscr := parser! optional (try (ident >> checkNoWsBefore "no space before ':'" >> ":")) >> termParser def Β«matchΒ» := parser!:leadPrec "match " >> sepBy1 matchDiscr ", " >> optType >> " with " >> matchAlts ``` -/ structure MatchAltView := (ref : Syntax) (patterns : Array Syntax) (rhs : Syntax) def mkMatchAltView (ref : Syntax) (matchAlt : Syntax) : MatchAltView := { ref := ref patterns := matchAlt[0].getSepArgs rhs := matchAlt[2] } private def expandSimpleMatch (stx discr lhsVar rhs : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do let newStx ← `(let $lhsVar := $discr; $rhs) withMacroExpansion stx newStx $ elabTerm newStx expectedType? private def expandSimpleMatchWithType (stx discr lhsVar type rhs : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do let newStx ← `(let $lhsVar : $type := $discr; $rhs) withMacroExpansion stx newStx $ elabTerm newStx expectedType? private def elabDiscrsWitMatchType (discrStxs : Array Syntax) (matchType : Expr) (expectedType : Expr) : TermElabM (Array Expr) := do let mut discrs := #[] let mut i := 0 let mut matchType := matchType for discrStx in discrStxs do i := i + 1 matchType ← whnf matchType match matchType with | Expr.forallE _ d b _ => let discr ← fullApproxDefEq $ elabTermEnsuringType discrStx[1] d trace[Elab.match]! "discr #{i} {discr} : {d}" matchType ← b.instantiate1 discr discrs := discrs.push discr | _ => throwError! "invalid type provided to match-expression, function type with arity #{discrStxs.size} expected" pure discrs private def mkUserNameFor (e : Expr) : TermElabM Name := match e with | Expr.fvar fvarId _ => do pure (← getLocalDecl fvarId).userName | _ => mkFreshBinderName -- `expandNonAtomicDiscrs?` create auxiliary variables with base name `_discr` private def isAuxDiscrName (n : Name) : Bool := n.eraseMacroScopes == `_discr -- See expandNonAtomicDiscrs? private def elabAtomicDiscr (discr : Syntax) : TermElabM Expr := do let term := discr[1] match (← isLocalIdent? term) with | some e@(Expr.fvar fvarId _) => let localDecl ← getLocalDecl fvarId if !isAuxDiscrName localDecl.userName then pure e -- it is not an auxiliary local created by `expandNonAtomicDiscrs?` else pure localDecl.value | _ => throwErrorAt discr "unexpected discriminant" private def elabMatchTypeAndDiscrs (discrStxs : Array Syntax) (matchOptType : Syntax) (matchAltViews : Array MatchAltView) (expectedType : Expr) : TermElabM (Array Expr Γ— Expr Γ— Array MatchAltView) := do let numDiscrs := discrStxs.size if matchOptType.isNone then let rec loop (i : Nat) (discrs : Array Expr) (matchType : Expr) (matchAltViews : Array MatchAltView) := do match i with | 0 => pure (discrs.reverse, matchType, matchAltViews) | i+1 => let discrStx := discrStxs[i] let discr ← elabAtomicDiscr discrStx let discr ← instantiateMVars discr let discrType ← inferType discr let discrType ← instantiateMVars discrType let matchTypeBody ← kabstract matchType discr let userName ← mkUserNameFor discr if discrStx[0].isNone then loop i (discrs.push discr) (Lean.mkForall userName BinderInfo.default discrType matchTypeBody) matchAltViews else let identStx := discrStx[0][0] withLocalDeclD userName discrType fun x => do let eqType ← mkEq discr x withLocalDeclD identStx.getId eqType fun h => do let matchTypeBody := matchTypeBody.instantiate1 x let matchType ← mkForallFVars #[x, h] matchTypeBody let refl ← mkEqRefl discr let discrs := (discrs.push refl).push discr let matchAltViews := matchAltViews.map fun altView => { altView with patterns := altView.patterns.insertAt (i+1) identStx } loop i discrs matchType matchAltViews loop discrStxs.size #[] expectedType matchAltViews else let matchTypeStx := matchOptType[0][1] let matchType ← elabType matchTypeStx let discrs ← elabDiscrsWitMatchType discrStxs matchType expectedType pure (discrs, matchType, matchAltViews) /- nodeWithAntiquot "matchAlt" `Lean.Parser.Term.matchAlt $ sepBy1 termParser ", " >> darrow >> termParser -/ def expandMacrosInPatterns (matchAlts : Array MatchAltView) : MacroM (Array MatchAltView) := do matchAlts.mapM fun matchAlt => do let patterns ← matchAlt.patterns.mapM expandMacros pure { matchAlt with patterns := patterns } /- Given `stx` a match-expression, return its alternatives. -/ private def getMatchAlts (stx : Syntax) : Array MatchAltView := do let matchAlts := stx[4] let firstVBar := matchAlts[0] let mut ref := firstVBar let mut result := #[] for arg in matchAlts[1].getArgs do if ref.isNone then ref := arg -- The first vertical bar is optional if arg.getKind == `Lean.Parser.Term.matchAlt then result := result.push (mkMatchAltView ref arg) else ref := arg -- current `arg` is a vertical bar pure result /-- Auxiliary annotation used to mark terms marked with the "inaccessible" annotation `.(t)` and `_` in patterns. -/ def mkInaccessible (e : Expr) : Expr := mkAnnotation `_inaccessible e def inaccessible? (e : Expr) : Option Expr := annotation? `_inaccessible e inductive PatternVar := | localVar (userName : Name) -- anonymous variables (`_`) are encoded using metavariables | anonymousVar (mvarId : MVarId) instance : ToString PatternVar := ⟨fun | PatternVar.localVar x => toString x | PatternVar.anonymousVar mvarId => s!"?m{mvarId}"⟩ builtin_initialize Parser.registerBuiltinNodeKind `MVarWithIdKind /-- Create an auxiliary Syntax node wrapping a fresh metavariable id. We use this kind of Syntax for representing `_` occurring in patterns. The metavariables are created before we elaborate the patterns into `Expr`s. -/ private def mkMVarSyntax : TermElabM Syntax := do let mvarId ← mkFreshId pure $ Syntax.node `MVarWithIdKind #[Syntax.node mvarId #[]] /-- Given a syntax node constructed using `mkMVarSyntax`, return its MVarId -/ private def getMVarSyntaxMVarId (stx : Syntax) : MVarId := stx[0].getKind /-- The elaboration function for `Syntax` created using `mkMVarSyntax`. It just converts the metavariable id wrapped by the Syntax into an `Expr`. -/ @[builtinTermElab MVarWithIdKind] def elabMVarWithIdKind : TermElab := fun stx expectedType? => pure $ mkInaccessible $ mkMVar (getMVarSyntaxMVarId stx) @[builtinTermElab inaccessible] def elabInaccessible : TermElab := fun stx expectedType? => do let e ← elabTerm stx[1] expectedType? pure $ mkInaccessible e /- Patterns define new local variables. This module collect them and preprocess `_` occurring in patterns. Recall that an `_` may represent anonymous variables or inaccessible terms that are implied by typing constraints. Thus, we represent them with fresh named holes `?x`. After we elaborate the pattern, if the metavariable remains unassigned, we transform it into a regular pattern variable. Otherwise, it becomes an inaccessible term. Macros occurring in patterns are expanded before the `collectPatternVars` method is executed. The following kinds of Syntax are handled by this module - Constructor applications - Applications of functions tagged with the `[matchPattern]` attribute - Identifiers - Anonymous constructors - Structure instances - Inaccessible terms - Named patterns - Tuple literals - Type ascriptions - Literals: num, string and char -/ namespace CollectPatternVars structure State := (found : NameSet := {}) (vars : Array PatternVar := #[]) abbrev M := StateRefT State TermElabM private def throwCtorExpected {Ξ±} : M Ξ± := throwError "invalid pattern, constructor or constant marked with '[matchPattern]' expected" private def getNumExplicitCtorParams (ctorVal : ConstructorVal) : TermElabM Nat := forallBoundedTelescope ctorVal.type ctorVal.nparams fun ps _ => do let mut result := 0 for p in ps do let localDecl ← getLocalDecl p.fvarId! if localDecl.binderInfo.isExplicit then result := result+1 pure result private def throwAmbiguous {Ξ±} (fs : List Expr) : M Ξ± := throwError! "ambiguous pattern, use fully qualified name, possible interpretations {fs}" def resolveId? (stx : Syntax) : M (Option Expr) := match stx with | Syntax.ident _ _ val preresolved => do let rs ← try resolveName val preresolved [] catch _ => pure [] let rs := rs.filter fun ⟨f, projs⟩ => projs.isEmpty let fs := rs.map fun (f, _) => f match fs with | [] => pure none | [f] => pure (some f) | _ => throwAmbiguous fs | _ => throwError "identifier expected" private def throwInvalidPattern {Ξ±} : M Ξ± := throwError "invalid pattern" namespace CtorApp /- An application in a pattern can be 1- A constructor application The elaborator assumes fields are accessible and inductive parameters are not accessible. 2- A regular application `(f ...)` where `f` is tagged with `[matchPattern]`. The elaborator assumes implicit arguments are not accessible and explicit ones are accessible. -/ structure Context := (funId : Syntax) (ctorVal? : Option ConstructorVal) -- It is `some`, if constructor application (explicit : Bool) (ellipsis : Bool) (paramDecls : Array LocalDecl) (paramDeclIdx : Nat := 0) (namedArgs : Array NamedArg) (args : List Arg) (newArgs : Array Syntax := #[]) instance : Inhabited Context := ⟨⟨arbitrary, none, false, false, #[], 0, #[], [], #[]⟩⟩ private def isDone (ctx : Context) : Bool := ctx.paramDeclIdx β‰₯ ctx.paramDecls.size private def finalize (ctx : Context) : M Syntax := do if ctx.namedArgs.isEmpty && ctx.args.isEmpty then let fStx ← `(@$(ctx.funId):ident) pure $ Syntax.mkApp fStx ctx.newArgs else throwError "too many arguments" private def isNextArgAccessible (ctx : Context) : Bool := let i := ctx.paramDeclIdx match ctx.ctorVal? with | some ctorVal => i β‰₯ ctorVal.nparams -- For constructor applications only fields are accessible | none => if h : i < ctx.paramDecls.size then -- For `[matchPattern]` applications, only explicit parameters are accessible. let d := ctx.paramDecls.get ⟨i, h⟩ d.binderInfo.isExplicit else false private def getNextParam (ctx : Context) : LocalDecl Γ— Context := let i := ctx.paramDeclIdx let d := ctx.paramDecls[i] (d, { ctx with paramDeclIdx := ctx.paramDeclIdx + 1 }) private def pushNewArg (collect : Syntax β†’ M Syntax) (accessible : Bool) (ctx : Context) (arg : Arg) : M Context := match arg with | Arg.stx stx => do let stx ← if accessible then collect stx else pure stx pure { ctx with newArgs := ctx.newArgs.push stx } | _ => unreachable! private def processExplicitArg (collect : Syntax β†’ M Syntax) (accessible : Bool) (ctx : Context) : M Context := match ctx.args with | [] => if ctx.ellipsis then do let hole ← `(_) pushNewArg collect accessible ctx (Arg.stx hole) else throwError! "explicit parameter is missing, unused named arguments {ctx.namedArgs.map fun narg => narg.name}" | arg::args => do let ctx := { ctx with args := args } pushNewArg collect accessible ctx arg private def processImplicitArg (collect : Syntax β†’ M Syntax) (accessible : Bool) (ctx : Context) : M Context := if ctx.explicit then processExplicitArg collect accessible ctx else do let hole ← `(_) pushNewArg collect accessible ctx (Arg.stx hole) private partial def processCtorAppAux (collect : Syntax β†’ M Syntax) (ctx : Context) : M Syntax := do if isDone ctx then finalize ctx else let accessible := isNextArgAccessible ctx let (d, ctx) := getNextParam ctx match ctx.namedArgs.findIdx? fun namedArg => namedArg.name == d.userName with | some idx => let arg := ctx.namedArgs[idx] let ctx := { ctx with namedArgs := ctx.namedArgs.eraseIdx idx } let ctx ← pushNewArg collect accessible ctx arg.val processCtorAppAux collect ctx | none => let ctx ← match d.binderInfo with | BinderInfo.implicit => processImplicitArg collect accessible ctx | BinderInfo.instImplicit => processImplicitArg collect accessible ctx | _ => processExplicitArg collect accessible ctx processCtorAppAux collect ctx def processCtorApp (collect : Syntax β†’ M Syntax) (f : Syntax) (namedArgs : Array NamedArg) (args : Array Arg) (ellipsis : Bool) : M Syntax := do let args := args.toList let (fId, explicit) ← match_syntax f with | `($fId:ident) => pure (fId, false) | `(@$fId:ident) => pure (fId, true) | _ => throwError "identifier expected" let some (Expr.const fName _ _) ← resolveId? fId | throwCtorExpected let fInfo ← getConstInfo fName forallTelescopeReducing fInfo.type fun xs _ => do let paramDecls ← xs.mapM getFVarLocalDecl match fInfo with | ConstantInfo.ctorInfo val => processCtorAppAux collect { funId := fId, explicit := explicit, ctorVal? := val, paramDecls := paramDecls, namedArgs := namedArgs, args := args, ellipsis := ellipsis } | _ => let env ← getEnv if hasMatchPatternAttribute env fName then processCtorAppAux collect { funId := fId, explicit := explicit, ctorVal? := none, paramDecls := paramDecls, namedArgs := namedArgs, args := args, ellipsis := ellipsis } else throwCtorExpected end CtorApp def processCtorApp (collect : Syntax β†’ M Syntax) (stx : Syntax) : M Syntax := do let (f, namedArgs, args, ellipsis) ← liftM $ expandApp stx true CtorApp.processCtorApp collect f namedArgs args ellipsis def processCtor (collect : Syntax β†’ M Syntax) (stx : Syntax) : M Syntax := do CtorApp.processCtorApp collect stx #[] #[] false private def processVar (idStx : Syntax) : M Syntax := do unless idStx.isIdent do throwErrorAt idStx "identifier expected" let id := idStx.getId unless id.eraseMacroScopes.isAtomic do throwError "invalid pattern variable, must be atomic" let s ← get if s.found.contains id then throwError! "invalid pattern, variable '{id}' occurred more than once" modify fun s => { s with vars := s.vars.push (PatternVar.localVar id), found := s.found.insert id } pure idStx /- Check whether `stx` is a pattern variable or constructor-like (i.e., constructor or constant tagged with `[matchPattern]` attribute) -/ private def processId (collect : Syntax β†’ M Syntax) (stx : Syntax) : M Syntax := do let env ← getEnv match (← resolveId? stx) with | none => processVar stx | some f => match f with | Expr.const fName _ _ => match env.find? fName with | some (ConstantInfo.ctorInfo _) => processCtor collect stx | some _ => if hasMatchPatternAttribute env fName then processCtor collect stx else processVar stx | none => throwCtorExpected | _ => processVar stx private def nameToPattern : Name β†’ TermElabM Syntax | Name.anonymous => `(Name.anonymous) | Name.str p s _ => do let p ← nameToPattern p; `(Name.str $p $(quote s) _) | Name.num p n _ => do let p ← nameToPattern p; `(Name.num $p $(quote n) _) private def quotedNameToPattern (stx : Syntax) : TermElabM Syntax := match stx[0].isNameLit? with | some val => nameToPattern val | none => throwIllFormedSyntax partial def collect : Syntax β†’ M Syntax | stx@(Syntax.node k args) => withRef stx $ withFreshMacroScope do if k == `Lean.Parser.Term.app then processCtorApp collect stx else if k == `Lean.Parser.Term.anonymousCtor then let elems ← args[1].getArgs.mapSepElemsM collect pure $ Syntax.node k (args.set! 1 $ mkNullNode elems) else if k == `Lean.Parser.Term.structInst then /- ``` parser! "{" >> optional (atomic (termParser >> " with ")) >> manyIndent (group (structInstField >> optional ", ")) >> optional ".." >> optional (" : " >> termParser) >> " }" ``` -/ let withMod := args[1] unless withMod.isNone do throwErrorAt withMod "invalid struct instance pattern, 'with' is not allowed in patterns" let fields ← args[2].getArgs.mapM fun p => do -- p is of the form (group (structInstField >> optional ", ")) let field := p[0] -- parser! structInstLVal >> " := " >> termParser let newVal ← collect field[3] -- `structInstLVal` has arity 2 let field := field.setArg 3 newVal pure <| field.setArg 0 field pure <| Syntax.node k (args.set! 2 <| mkNullNode fields) else if k == `Lean.Parser.Term.hole then let r ← mkMVarSyntax modify fun s => { s with vars := s.vars.push $ PatternVar.anonymousVar $ getMVarSyntaxMVarId r } pure r else if k == `Lean.Parser.Term.paren then let arg := args[1] if arg.isNone then pure stx -- `()` else let t := arg[0] let s := arg[1] if s.isNone || s[0].getKind == `Lean.Parser.Term.typeAscription then -- Ignore `s`, since it empty or it is a type ascription let t ← collect t let arg := arg.setArg 0 t pure $ Syntax.node k (args.set! 1 arg) else -- Tuple literal is a constructor let t ← collect t let arg := arg.setArg 0 t let tupleTail := s[0] let tupleTailElems := tupleTail[1].getArgs let tupleTailElems ← tupleTailElems.mapSepElemsM collect let tupleTail := tupleTail.setArg 1 $ mkNullNode tupleTailElems let s := s.setArg 0 tupleTail let arg := arg.setArg 1 s pure $ Syntax.node k (args.set! 1 arg) else if k == `Lean.Parser.Term.explicitUniv then processCtor collect stx[0] else if k == `Lean.Parser.Term.namedPattern then /- Recall that def namedPattern := check... >> tparser! "@" >> termParser -/ let id := stx[0] processVar id let pat := stx[2] let pat ← collect pat `(namedPattern $id $pat) else if k == `Lean.Parser.Term.inaccessible then pure stx else if k == strLitKind then pure stx else if k == numLitKind then pure stx else if k == charLitKind then pure stx else if k == `Lean.Parser.Term.quotedName then /- Quoted names have an elaboration function associated with them, and they will not be macro expanded. Note that macro expansion is not a good option since it produces a term using the smart constructors `Name.mkStr`, `Name.mkNum` instead of the constructors `Name.str` and `Name.num` -/ quotedNameToPattern stx else if k == choiceKind then throwError "invalid pattern, notation is ambiguous" else throwInvalidPattern | stx@(Syntax.ident _ _ _ _) => processId collect stx | stx => throwInvalidPattern def main (alt : MatchAltView) : M MatchAltView := do let patterns ← alt.patterns.mapM fun p => do trace[Elab.match]! "collecting variables at pattern: {p}" collect p pure { alt with patterns := patterns } end CollectPatternVars private def collectPatternVars (alt : MatchAltView) : TermElabM (Array PatternVar Γ— MatchAltView) := do let (alt, s) ← (CollectPatternVars.main alt).run {} pure (s.vars, alt) /- Return the pattern variables in the given pattern. Remark: this method is not used here, but in other macros (e.g., at `Do.lean`). -/ def getPatternVars (patternStx : Syntax) : TermElabM (Array PatternVar) := do let patternStx ← liftMacroM $ expandMacros patternStx let (_, s) ← (CollectPatternVars.collect patternStx).run {} pure s.vars def getPatternsVars (patterns : Array Syntax) : TermElabM (Array PatternVar) := do let collect : CollectPatternVars.M Unit := do for pattern in patterns do CollectPatternVars.collect (← liftMacroM $ expandMacros pattern) pure () let (_, s) ← collect.run {} pure s.vars /- We convert the collected `PatternVar`s intro `PatternVarDecl` -/ inductive PatternVarDecl := /- For `anonymousVar`, we create both a metavariable and a free variable. The free variable is used as an assignment for the metavariable when it is not assigned during pattern elaboration. -/ | anonymousVar (mvarId : MVarId) (fvarId : FVarId) | localVar (fvarId : FVarId) private partial def withPatternVars {Ξ±} (pVars : Array PatternVar) (k : Array PatternVarDecl β†’ TermElabM Ξ±) : TermElabM Ξ± := let rec loop (i : Nat) (decls : Array PatternVarDecl) := do if h : i < pVars.size then match pVars.get ⟨i, h⟩ with | PatternVar.anonymousVar mvarId => let type ← mkFreshTypeMVar let userName ← mkFreshBinderName withLocalDecl userName BinderInfo.default type fun x => loop (i+1) (decls.push (PatternVarDecl.anonymousVar mvarId x.fvarId!)) | PatternVar.localVar userName => let type ← mkFreshTypeMVar withLocalDecl userName BinderInfo.default type fun x => loop (i+1) (decls.push (PatternVarDecl.localVar x.fvarId!)) else /- We must create the metavariables for `PatternVar.anonymousVar` AFTER we create the new local decls using `withLocalDecl`. Reason: their scope must include the new local decls since some of them are assigned by typing constraints. -/ decls.forM fun decl => match decl with | PatternVarDecl.anonymousVar mvarId fvarId => do let type ← inferType (mkFVar fvarId) mkFreshExprMVarWithId mvarId type pure () | _ => pure () k decls loop 0 #[] private def elabPatterns (patternStxs : Array Syntax) (matchType : Expr) : TermElabM (Array Expr Γ— Expr) := do let mut patterns := #[] let mut matchType := matchType for patternStx in patternStxs do matchType ← whnf matchType match matchType with | Expr.forallE _ d b _ => let pattern ← elabTermEnsuringType patternStx d matchType := b.instantiate1 pattern patterns := patterns.push pattern | _ => throwError "unexpected match type" pure (patterns, matchType) def finalizePatternDecls (patternVarDecls : Array PatternVarDecl) : TermElabM (Array LocalDecl) := do let mut decls := #[] for pdecl in patternVarDecls do match pdecl with | PatternVarDecl.localVar fvarId => let decl ← getLocalDecl fvarId let decl ← instantiateLocalDeclMVars decl decls := decls.push decl | PatternVarDecl.anonymousVar mvarId fvarId => let e ← instantiateMVars (mkMVar mvarId); trace[Elab.match]! "finalizePatternDecls: mvarId: {mvarId} := {e}, fvar: {mkFVar fvarId}" match e with | Expr.mvar newMVarId _ => /- Metavariable was not assigned, or assigned to another metavariable. So, we assign to the auxiliary free variable we created at `withPatternVars` to `newMVarId`. -/ assignExprMVar newMVarId (mkFVar fvarId) trace[Elab.match]! "finalizePatternDecls: {mkMVar newMVarId} := {mkFVar fvarId}" let decl ← getLocalDecl fvarId let decl ← instantiateLocalDeclMVars decl decls := decls.push decl | _ => pure () pure decls open Meta.Match (Pattern Pattern.var Pattern.inaccessible Pattern.ctor Pattern.as Pattern.val Pattern.arrayLit AltLHS MatcherResult) namespace ToDepElimPattern structure State := (found : NameSet := {}) (localDecls : Array LocalDecl) (newLocals : NameSet := {}) abbrev M := StateRefT State TermElabM private def alreadyVisited (fvarId : FVarId) : M Bool := do let s ← get pure $ s.found.contains fvarId private def markAsVisited (fvarId : FVarId) : M Unit := modify fun s => { s with found := s.found.insert fvarId } private def throwInvalidPattern {Ξ±} (e : Expr) : M Ξ± := throwError! "invalid pattern {indentExpr e}" /- Create a new LocalDecl `x` for the metavariable `mvar`, and return `Pattern.var x` -/ private def mkLocalDeclFor (mvar : Expr) : M Pattern := do let mvarId := mvar.mvarId! let s ← get match (← getExprMVarAssignment? mvarId) with | some val => pure $ Pattern.inaccessible val | none => let fvarId ← mkFreshId let type ← inferType mvar /- HACK: `fvarId` is not in the scope of `mvarId` If this generates problems in the future, we should update the metavariable declarations. -/ assignExprMVar mvarId (mkFVar fvarId) let userName ← liftM $ mkFreshBinderName let newDecl := LocalDecl.cdecl arbitrary fvarId userName type BinderInfo.default; modify fun s => { s with newLocals := s.newLocals.insert fvarId, localDecls := match s.localDecls.findIdx? fun decl => mvar.occurs decl.type with | none => s.localDecls.push newDecl -- None of the existing declarations depend on `mvar` | some i => s.localDecls.insertAt i newDecl } pure $ Pattern.var fvarId partial def main (e : Expr) : M Pattern := do let isLocalDecl (fvarId : FVarId) : M Bool := do let s ← get pure $ s.localDecls.any fun d => d.fvarId == fvarId let mkPatternVar (fvarId : FVarId) (e : Expr) : M Pattern := do if (← alreadyVisited fvarId) then pure $ Pattern.inaccessible e else markAsVisited fvarId pure $ Pattern.var e.fvarId! let mkInaccessible (e : Expr) : M Pattern := do match e with | Expr.fvar fvarId _ => if (← isLocalDecl fvarId) then mkPatternVar fvarId e else pure $ Pattern.inaccessible e | _ => pure $ Pattern.inaccessible e match inaccessible? e with | some t => mkInaccessible t | none => match e.arrayLit? with | some (Ξ±, lits) => let ps ← lits.mapM main; pure $ Pattern.arrayLit Ξ± ps | none => if e.isAppOfArity `namedPattern 3 then let p ← main $ e.getArg! 2; match e.getArg! 1 with | Expr.fvar fvarId _ => pure $ Pattern.as fvarId p | _ => throwError "unexpected occurrence of auxiliary declaration 'namedPattern'" else if e.isNatLit || e.isStringLit || e.isCharLit then pure $ Pattern.val e else if e.isFVar then let fvarId := e.fvarId! unless(← isLocalDecl fvarId) do throwInvalidPattern e mkPatternVar fvarId e else if e.isMVar then mkLocalDeclFor e else let newE ← whnf e if newE != e then main newE else matchConstCtor e.getAppFn (fun _ => throwInvalidPattern e) fun v us => do let args := e.getAppArgs unless args.size == v.nparams + v.nfields do throwInvalidPattern e let params := args.extract 0 v.nparams let fields := args.extract v.nparams args.size let fields ← fields.mapM main pure $ Pattern.ctor v.name us params.toList fields.toList end ToDepElimPattern def withDepElimPatterns {Ξ±} (localDecls : Array LocalDecl) (ps : Array Expr) (k : Array LocalDecl β†’ Array Pattern β†’ TermElabM Ξ±) : TermElabM Ξ± := do let (patterns, s) ← (ps.mapM ToDepElimPattern.main).run { localDecls := localDecls } let localDecls ← s.localDecls.mapM fun d => instantiateLocalDeclMVars d /- toDepElimPatterns may have added new localDecls. Thus, we must update the local context before we execute `k` -/ let lctx ← getLCtx let lctx := localDecls.foldl (fun (lctx : LocalContext) d => lctx.erase d.fvarId) lctx let lctx := localDecls.foldl (fun (lctx : LocalContext) d => lctx.addDecl d) lctx withTheReader Meta.Context (fun ctx => { ctx with lctx := lctx }) $ k localDecls patterns private def withElaboratedLHS {Ξ±} (ref : Syntax) (patternVarDecls : Array PatternVarDecl) (patternStxs : Array Syntax) (matchType : Expr) (k : AltLHS β†’ Expr β†’ TermElabM Ξ±) : TermElabM Ξ± := do let (patterns, matchType) ← withSynthesize $ elabPatterns patternStxs matchType let localDecls ← finalizePatternDecls patternVarDecls let patterns ← patterns.mapM instantiateMVars withDepElimPatterns localDecls patterns fun localDecls patterns => k { ref := ref, fvarDecls := localDecls.toList, patterns := patterns.toList } matchType def elabMatchAltView (alt : MatchAltView) (matchType : Expr) : TermElabM (AltLHS Γ— Expr) := withRef alt.ref do let (patternVars, alt) ← collectPatternVars alt trace[Elab.match]! "patternVars: {patternVars}" withPatternVars patternVars fun patternVarDecls => do withElaboratedLHS alt.ref patternVarDecls alt.patterns matchType fun altLHS matchType => do let rhs ← elabTermEnsuringType alt.rhs matchType let xs := altLHS.fvarDecls.toArray.map LocalDecl.toExpr let rhs ← if xs.isEmpty then pure $ mkSimpleThunk rhs else mkLambdaFVars xs rhs trace[Elab.match]! "rhs: {rhs}" pure (altLHS, rhs) def mkMatcher (elimName : Name) (matchType : Expr) (numDiscrs : Nat) (lhss : List AltLHS) : TermElabM MatcherResult := liftMetaM $ Meta.Match.mkMatcher elimName matchType numDiscrs lhss def reportMatcherResultErrors (result : MatcherResult) : TermElabM Unit := do -- TODO: improve error messages unless result.counterExamples.isEmpty do throwError! "missing cases:\n{Meta.Match.counterExamplesToMessageData result.counterExamples}" unless result.unusedAltIdxs.isEmpty do throwError! "unused alternatives: {result.unusedAltIdxs.map fun idx => s!"#{idx+1}"}" private def elabMatchAux (discrStxs : Array Syntax) (altViews : Array MatchAltView) (matchOptType : Syntax) (expectedType : Expr) : TermElabM Expr := do let (discrs, matchType, altViews) ← elabMatchTypeAndDiscrs discrStxs matchOptType altViews expectedType let matchAlts ← liftMacroM $ expandMacrosInPatterns altViews trace[Elab.match]! "matchType: {matchType}" let alts ← matchAlts.mapM $ fun alt => elabMatchAltView alt matchType /- We should not use `synthesizeSyntheticMVarsNoPostponing` here. Otherwise, we will not be able to elaborate examples such as: ``` def f (x : Nat) : Option Nat := none def g (xs : List (Nat Γ— Nat)) : IO Unit := xs.forM fun x => match f x.fst with | _ => pure () ``` If `synthesizeSyntheticMVarsNoPostponing`, the example above fails at `x.fst` because the type of `x` is only available adfer we proces the last argument of `List.forM`. -/ synthesizeSyntheticMVars /- We apply pending default types to make sure we can process examples such as ``` let (a, b) := (0, 0) ``` -/ synthesizeUsingDefault let rhss := alts.map Prod.snd let matchType ← instantiateMVars matchType let altLHSS ← alts.toList.mapM fun alt => do let altLHS ← Match.instantiateAltLHSMVars alt.1 withRef altLHS.ref do for d in altLHS.fvarDecls do if d.hasExprMVar then withExistingLocalDecls altLHS.fvarDecls do throwError! "invalid match-expression, type of pattern variable '{d.toExpr}' contains metavariables{indentExpr d.type}" for p in altLHS.patterns do if p.hasExprMVar then withExistingLocalDecls altLHS.fvarDecls do throwError! "invalid match-expression, pattern contains metavariables{indentExpr (← p.toExpr)}" pure altLHS let numDiscrs := discrs.size let matcherName ← mkAuxName `match let matcherResult ← mkMatcher matcherName matchType numDiscrs altLHSS let motive ← forallBoundedTelescope matchType numDiscrs fun xs matchType => mkLambdaFVars xs matchType reportMatcherResultErrors matcherResult let r := mkApp matcherResult.matcher motive let r := mkAppN r discrs let r := mkAppN r rhss trace[Elab.match]! "result: {r}" pure r private def getDiscrs (matchStx : Syntax) : Array Syntax := matchStx[1].getSepArgs private def getMatchOptType (matchStx : Syntax) : Syntax := matchStx[2] private def expandNonAtomicDiscrs? (matchStx : Syntax) : TermElabM (Option Syntax) := let matchOptType := getMatchOptType matchStx; if matchOptType.isNone then do let discrs := getDiscrs matchStx; let allLocal ← discrs.allM fun discr => Option.isSome <$> isLocalIdent? discr[1] if allLocal then pure none else let rec loop (discrs : List Syntax) (discrsNew : Array Syntax) := do match discrs with | [] => let discrs := Syntax.mkSep discrsNew (mkAtomFrom matchStx ", "); pure (matchStx.setArg 1 discrs) | discr :: discrs => -- Recall that -- matchDiscr := parser! optional (ident >> ":") >> termParser let term := discr[1] match (← isLocalIdent? term) with | some _ => loop discrs (discrsNew.push discr) | none => withFreshMacroScope do let d ← `(_discr); unless isAuxDiscrName d.getId do -- Use assertion? throwError "unexpected internal auxiliary discriminant name" let discrNew := discr.setArg 1 d; let r ← loop discrs (discrsNew.push discrNew) `(let _discr := $term; $r) pure (some (← loop discrs.toList #[])) else -- We do not pull non atomic discriminants when match type is provided explicitly by the user pure none private def waitExpectedType (expectedType? : Option Expr) : TermElabM Expr := do tryPostponeIfNoneOrMVar expectedType? match expectedType? with | some expectedType => pure expectedType | none => mkFreshTypeMVar private def tryPostponeIfDiscrTypeIsMVar (matchStx : Syntax) : TermElabM Unit := do -- We don't wait for the discriminants types when match type is provided by user if getMatchOptType matchStx |>.isNone then let discrs := getDiscrs matchStx for discr in discrs do let term := discr[1] match (← isLocalIdent? term) with | none => throwErrorAt discr "unexpected discriminant" -- see `expandNonAtomicDiscrs? | some d => let dType ← inferType d trace[Elab.match]! "discr {d} : {dType}" tryPostponeIfMVar dType /- We (try to) elaborate a `match` only when the expected type is available. If the `matchType` has not been provided by the user, we also try to postpone elaboration if the type of a discriminant is not available. That is, it is of the form `(?m ...)`. We use `expandNonAtomicDiscrs?` to make sure all discriminants are local variables. This is a standard trick we use in the elaborator, and it is also used to elaborate structure instances. Suppose, we are trying to elaborate ``` match g x with | ... => ... ``` `expandNonAtomicDiscrs?` converts it intro ``` let _discr := g x match _discr with | ... => ... ``` Thus, at `tryPostponeIfDiscrTypeIsMVar` we only need to check whether the type of `_discr` is not of the form `(?m ...)`. Note that, the auxiliary variable `_discr` is expanded at `elabAtomicDiscr`. This elaboration technique is needed to elaborate terms such as: ```lean xs.filter fun (a, b) => a > b ``` which are syntax sugar for ```lean List.filter (fun p => match p with | (a, b) => a > b) xs ``` When we visit `match p with | (a, b) => a > b`, we don't know the type of `p` yet. -/ private def waitExpectedTypeAndDiscrs (matchStx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do tryPostponeIfNoneOrMVar expectedType? tryPostponeIfDiscrTypeIsMVar matchStx match expectedType? with | some expectedType => pure expectedType | none => mkFreshTypeMVar /- ``` parser!:leadPrec "match " >> sepBy1 matchDiscr ", " >> optType >> " with " >> matchAlts ``` Remark the `optIdent` must be `none` at `matchDiscr`. They are expanded by `expandMatchDiscr?`. -/ private def elabMatchCore (stx : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do let expectedType ← waitExpectedTypeAndDiscrs stx expectedType? let discrStxs := (getDiscrs stx).map fun d => d let altViews := getMatchAlts stx let matchOptType := getMatchOptType stx elabMatchAux discrStxs altViews matchOptType expectedType -- parser! "match " >> sepBy1 termParser ", " >> optType >> " with " >> matchAlts @[builtinTermElab Β«matchΒ»] def elabMatch : TermElab := fun stx expectedType? => match_syntax stx with | `(match $discr:term with $y:ident => $rhs:term) => expandSimpleMatch stx discr y rhs expectedType? | `(match $discr:term with | $y:ident => $rhs:term) => expandSimpleMatch stx discr y rhs expectedType? | `(match $discr:term : $type with $y:ident => $rhs:term) => expandSimpleMatchWithType stx discr y type rhs expectedType? | `(match $discr:term : $type with | $y:ident => $rhs:term) => expandSimpleMatchWithType stx discr y type rhs expectedType? | _ => do match (← expandNonAtomicDiscrs? stx) with | some stxNew => withMacroExpansion stx stxNew $ elabTerm stxNew expectedType? | none => let discrs := getDiscrs stx; let matchOptType := getMatchOptType stx; if !matchOptType.isNone && discrs.any fun d => !d[0].isNone then throwErrorAt matchOptType "match expected type should not be provided when discriminants with equality proofs are used" elabMatchCore stx expectedType? @[builtinInit] private def regTraceClasses : IO Unit := do registerTraceClass `Elab.match; pure () -- parser!:leadPrec "nomatch " >> termParser @[builtinTermElab Β«nomatchΒ»] def elabNoMatch : TermElab := fun stx expectedType? => match_syntax stx with | `(nomatch $discrExpr) => do let expectedType ← waitExpectedType expectedType? let discr := Syntax.node `Lean.Parser.Term.matchDiscr #[mkNullNode, discrExpr] elabMatchAux #[discr] #[] mkNullNode expectedType | _ => throwUnsupportedSyntax end Term end Elab end Lean
42ceba4640b0ad926ab1643e90b632fed3bff438
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/group_theory/submonoid/operations.lean
85397f2a1e6f9e4a52e3fa9b7a287a005e1f4862
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
35,503
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, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import group_theory.submonoid.basic import data.equiv.mul_add import algebra.group.prod import algebra.group.inj_surj /-! # Operations on `submonoid`s In this file we define various operations on `submonoid`s and `monoid_hom`s. ## Main definitions ### Conversion between multiplicative and additive definitions * `submonoid.to_add_submonoid`, `submonoid.to_add_submonoid'`, `add_submonoid.to_submonoid`, `add_submonoid.to_submonoid'`: convert between multiplicative and additive submonoids of `M`, `multiplicative M`, and `additive M`. These are stated as `order_iso`s. ### (Commutative) monoid structure on a submonoid * `submonoid.to_monoid`, `submonoid.to_comm_monoid`: a submonoid inherits a (commutative) monoid structure. ### Group actions by submonoids * `submonoid.mul_action`, `submonoid.distrib_mul_action`: a submonoid inherits (distributive) multiplicative actions. ### Operations on submonoids * `submonoid.comap`: preimage of a submonoid under a monoid homomorphism as a submonoid of the domain; * `submonoid.map`: image of a submonoid under a monoid homomorphism as a submonoid of the codomain; * `submonoid.prod`: product of two submonoids `s : submonoid M` and `t : submonoid N` as a submonoid of `M Γ— N`; ### Monoid homomorphisms between submonoid * `submonoid.subtype`: embedding of a submonoid into the ambient monoid. * `submonoid.inclusion`: given two submonoids `S`, `T` such that `S ≀ T`, `S.inclusion T` is the inclusion of `S` into `T` as a monoid homomorphism; * `mul_equiv.submonoid_congr`: converts a proof of `S = T` into a monoid isomorphism between `S` and `T`. * `submonoid.prod_equiv`: monoid isomorphism between `s.prod t` and `s Γ— t`; ### Operations on `monoid_hom`s * `monoid_hom.mrange`: range of a monoid homomorphism as a submonoid of the codomain; * `monoid_hom.mker`: kernel of a monoid homomorphism as a submonoid of the domain; * `monoid_hom.mrestrict`: restrict a monoid homomorphism to a submonoid; * `monoid_hom.cod_mrestrict`: restrict the codomain of a monoid homomorphism to a submonoid; * `monoid_hom.mrange_restrict`: restrict a monoid homomorphism to its range; ## Tags submonoid, range, product, map, comap -/ variables {M N P : Type*} [mul_one_class M] [mul_one_class N] [mul_one_class P] (S : submonoid M) /-! ### Conversion to/from `additive`/`multiplicative` -/ section /-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/ @[simps] def submonoid.to_add_submonoid : submonoid M ≃o add_submonoid (additive M) := { to_fun := Ξ» S, { carrier := additive.to_mul ⁻¹' S, zero_mem' := S.one_mem', add_mem' := S.mul_mem' }, inv_fun := Ξ» S, { carrier := additive.of_mul ⁻¹' S, one_mem' := S.zero_mem', mul_mem' := S.add_mem' }, left_inv := Ξ» x, by cases x; refl, right_inv := Ξ» x, by cases x; refl, map_rel_iff' := Ξ» a b, iff.rfl, } /-- Additive submonoids of an additive monoid `additive M` are isomorphic to submonoids of `M`. -/ abbreviation add_submonoid.to_submonoid' : add_submonoid (additive M) ≃o submonoid M := submonoid.to_add_submonoid.symm lemma submonoid.to_add_submonoid_closure (S : set M) : (submonoid.closure S).to_add_submonoid = add_submonoid.closure (additive.to_mul ⁻¹' S) := le_antisymm (submonoid.to_add_submonoid.to_galois_connection.l_le $ submonoid.closure_le.2 add_submonoid.subset_closure) (add_submonoid.closure_le.2 submonoid.subset_closure) lemma add_submonoid.to_submonoid'_closure (S : set (additive M)) : (add_submonoid.closure S).to_submonoid' = submonoid.closure (multiplicative.of_add ⁻¹' S) := le_antisymm (add_submonoid.to_submonoid'.to_galois_connection.l_le $ add_submonoid.closure_le.2 submonoid.subset_closure) (submonoid.closure_le.2 add_submonoid.subset_closure) end section variables {A : Type*} [add_zero_class A] /-- Additive submonoids of an additive monoid `A` are isomorphic to multiplicative submonoids of `multiplicative A`. -/ @[simps] def add_submonoid.to_submonoid : add_submonoid A ≃o submonoid (multiplicative A) := { to_fun := Ξ» S, { carrier := multiplicative.to_add ⁻¹' S, one_mem' := S.zero_mem', mul_mem' := S.add_mem' }, inv_fun := Ξ» S, { carrier := multiplicative.of_add ⁻¹' S, zero_mem' := S.one_mem', add_mem' := S.mul_mem' }, left_inv := Ξ» x, by cases x; refl, right_inv := Ξ» x, by cases x; refl, map_rel_iff' := Ξ» a b, iff.rfl, } /-- Submonoids of a monoid `multiplicative A` are isomorphic to additive submonoids of `A`. -/ abbreviation submonoid.to_add_submonoid' : submonoid (multiplicative A) ≃o add_submonoid A := add_submonoid.to_submonoid.symm lemma add_submonoid.to_submonoid_closure (S : set A) : (add_submonoid.closure S).to_submonoid = submonoid.closure (multiplicative.to_add ⁻¹' S) := le_antisymm (add_submonoid.to_submonoid.to_galois_connection.l_le $ add_submonoid.closure_le.2 submonoid.subset_closure) (submonoid.closure_le.2 add_submonoid.subset_closure) lemma submonoid.to_add_submonoid'_closure (S : set (multiplicative A)) : (submonoid.closure S).to_add_submonoid' = add_submonoid.closure (additive.of_mul ⁻¹' S) := le_antisymm (submonoid.to_add_submonoid'.to_galois_connection.l_le $ submonoid.closure_le.2 add_submonoid.subset_closure) (add_submonoid.closure_le.2 submonoid.subset_closure) end namespace submonoid open set /-! ### `comap` and `map` -/ /-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def comap (f : M β†’* N) (S : submonoid N) : submonoid M := { carrier := (f ⁻¹' S), one_mem' := show f 1 ∈ S, by rw f.map_one; exact S.one_mem, mul_mem' := Ξ» a b ha hb, show f (a * b) ∈ S, by rw f.map_mul; exact S.mul_mem ha hb } @[simp, to_additive] lemma coe_comap (S : submonoid N) (f : M β†’* N) : (S.comap f : set M) = f ⁻¹' S := rfl @[simp, to_additive] lemma mem_comap {S : submonoid N} {f : M β†’* N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl @[to_additive] lemma comap_comap (S : submonoid P) (g : N β†’* P) (f : M β†’* N) : (S.comap g).comap f = S.comap (g.comp f) := rfl @[simp, to_additive] lemma comap_id (S : submonoid P) : S.comap (monoid_hom.id _) = S := ext (by simp) /-- The image of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def map (f : M β†’* N) (S : submonoid M) : submonoid N := { carrier := (f '' S), one_mem' := ⟨1, S.one_mem, f.map_one⟩, mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy, by rw f.map_mul; refl⟩ end } @[simp, to_additive] lemma coe_map (f : M β†’* N) (S : submonoid M) : (S.map f : set N) = f '' S := rfl @[simp, to_additive] lemma mem_map {f : M β†’* N} {S : submonoid M} {y : N} : y ∈ S.map f ↔ βˆƒ x ∈ S, f x = y := mem_image_iff_bex @[to_additive] lemma mem_map_of_mem (f : M β†’* N) {S : submonoid M} {x : M} (hx : x ∈ S) : f x ∈ S.map f := mem_image_of_mem f hx @[to_additive] lemma apply_coe_mem_map (f : M β†’* N) (S : submonoid M) (x : S) : f x ∈ S.map f := mem_map_of_mem f x.prop @[to_additive] lemma map_map (g : N β†’* P) (f : M β†’* N) : (S.map f).map g = S.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ @[to_additive] lemma map_le_iff_le_comap {f : M β†’* N} {S : submonoid M} {T : submonoid N} : S.map f ≀ T ↔ S ≀ T.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : M β†’* N) : galois_connection (map f) (comap f) := Ξ» S T, map_le_iff_le_comap @[to_additive] lemma map_le_of_le_comap {T : submonoid N} {f : M β†’* N} : S ≀ T.comap f β†’ S.map f ≀ T := (gc_map_comap f).l_le @[to_additive] lemma le_comap_of_map_le {T : submonoid N} {f : M β†’* N} : S.map f ≀ T β†’ S ≀ T.comap f := (gc_map_comap f).le_u @[to_additive] lemma le_comap_map {f : M β†’* N} : S ≀ (S.map f).comap f := (gc_map_comap f).le_u_l _ @[to_additive] lemma map_comap_le {S : submonoid N} {f : M β†’* N} : (S.comap f).map f ≀ S := (gc_map_comap f).l_u_le _ @[to_additive] lemma monotone_map {f : M β†’* N} : monotone (map f) := (gc_map_comap f).monotone_l @[to_additive] lemma monotone_comap {f : M β†’* N} : monotone (comap f) := (gc_map_comap f).monotone_u @[simp, to_additive] lemma map_comap_map {f : M β†’* N} : ((S.map f).comap f).map f = S.map f := congr_fun ((gc_map_comap f).l_u_l_eq_l) _ @[simp, to_additive] lemma comap_map_comap {S : submonoid N} {f : M β†’* N} : ((S.comap f).map f).comap f = S.comap f := congr_fun ((gc_map_comap f).u_l_u_eq_u) _ @[to_additive] lemma map_sup (S T : submonoid M) (f : M β†’* N) : (S βŠ” T).map f = S.map f βŠ” T.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ΞΉ : Sort*} (f : M β†’* N) (s : ΞΉ β†’ submonoid M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_inf (S T : submonoid N) (f : M β†’* N) : (S βŠ“ T).comap f = S.comap f βŠ“ T.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ΞΉ : Sort*} (f : M β†’* N) (s : ΞΉ β†’ submonoid N) : (infi s).comap f = β¨… i, (s i).comap f := (gc_map_comap f).u_infi @[simp, to_additive] lemma map_bot (f : M β†’* N) : (βŠ₯ : submonoid M).map f = βŠ₯ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : M β†’* N) : (⊀ : submonoid N).comap f = ⊀ := (gc_map_comap f).u_top @[simp, to_additive] lemma map_id (S : submonoid M) : S.map (monoid_hom.id M) = S := ext (Ξ» x, ⟨λ ⟨_, h, rfl⟩, h, Ξ» h, ⟨_, h, rfl⟩⟩) section galois_coinsertion variables {ΞΉ : Type*} {f : M β†’* N} (hf : function.injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ @[to_additive /-" `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. "-/] def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (Ξ» S x, by simp [mem_comap, mem_map, hf.eq_iff]) @[to_additive] lemma comap_map_eq_of_injective (S : submonoid M) : (S.map f).comap f = S := (gci_map_comap hf).u_l_eq _ @[to_additive] lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective @[to_additive] lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective @[to_additive] lemma comap_inf_map_of_injective (S T : submonoid M) : (S.map f βŠ“ T.map f).comap f = S βŠ“ T := (gci_map_comap hf).u_inf_l _ _ @[to_additive] lemma comap_infi_map_of_injective (S : ΞΉ β†’ submonoid M) : (β¨… i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ @[to_additive] lemma comap_sup_map_of_injective (S T : submonoid M) : (S.map f βŠ” T.map f).comap f = S βŠ” T := (gci_map_comap hf).u_sup_l _ _ @[to_additive] lemma comap_supr_map_of_injective (S : ΞΉ β†’ submonoid M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ @[to_additive] lemma map_le_map_iff_of_injective {S T : submonoid M} : S.map f ≀ T.map f ↔ S ≀ T := (gci_map_comap hf).l_le_l_iff @[to_additive] lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section galois_insertion variables {ΞΉ : Type*} {f : M β†’* N} (hf : function.surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ @[to_additive /-" `map f` and `comap f` form a `galois_insertion` when `f` is surjective. "-/] def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (Ξ» S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩) @[to_additive] lemma map_comap_eq_of_surjective (S : submonoid N) : (S.comap f).map f = S := (gi_map_comap hf).l_u_eq _ @[to_additive] lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective @[to_additive] lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective @[to_additive] lemma map_inf_comap_of_surjective (S T : submonoid N) : (S.comap f βŠ“ T.comap f).map f = S βŠ“ T := (gi_map_comap hf).l_inf_u _ _ @[to_additive] lemma map_infi_comap_of_surjective (S : ΞΉ β†’ submonoid N) : (β¨… i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ @[to_additive] lemma map_sup_comap_of_surjective (S T : submonoid N) : (S.comap f βŠ” T.comap f).map f = S βŠ” T := (gi_map_comap hf).l_sup_u _ _ @[to_additive] lemma map_supr_comap_of_surjective (S : ΞΉ β†’ submonoid N) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ @[to_additive] lemma comap_le_comap_iff_of_surjective {S T : submonoid N} : S.comap f ≀ T.comap f ↔ S ≀ T := (gi_map_comap hf).u_le_u_iff @[to_additive] lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion /-- A submonoid of a monoid inherits a multiplication. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."] instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩ /-- A submonoid of a monoid inherits a 1. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."] instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩ @[simp, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl @[simp, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl attribute [norm_cast] coe_mul coe_one attribute [norm_cast] add_submonoid.coe_add add_submonoid.coe_zero /-- A submonoid of a unital magma inherits a unital magma structure. -/ @[to_additive "An `add_submonoid` of an unital additive magma inherits an unital additive magma structure."] instance to_mul_one_class {M : Type*} [mul_one_class M] (S : submonoid M) : mul_one_class S := subtype.coe_injective.mul_one_class coe rfl (Ξ» _ _, rfl) /-- A submonoid of a monoid inherits a monoid structure. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure."] instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S := subtype.coe_injective.monoid coe rfl (Ξ» _ _, rfl) /-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `add_comm_monoid` is an `add_comm_monoid`."] instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S := subtype.coe_injective.comm_monoid coe rfl (Ξ» _ _, rfl) /-- A submonoid of an `ordered_comm_monoid` is an `ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_add_comm_monoid` is an `ordered_add_comm_monoid`."] instance to_ordered_comm_monoid {M} [ordered_comm_monoid M] (S : submonoid M) : ordered_comm_monoid S := subtype.coe_injective.ordered_comm_monoid coe rfl (Ξ» _ _, rfl) /-- A submonoid of a `linear_ordered_comm_monoid` is a `linear_ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_add_comm_monoid` is a `linear_ordered_add_comm_monoid`."] instance to_linear_ordered_comm_monoid {M} [linear_ordered_comm_monoid M] (S : submonoid M) : linear_ordered_comm_monoid S := subtype.coe_injective.linear_ordered_comm_monoid coe rfl (Ξ» _ _, rfl) /-- A submonoid of an `ordered_cancel_comm_monoid` is an `ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_cancel_add_comm_monoid` is an `ordered_cancel_add_comm_monoid`."] instance to_ordered_cancel_comm_monoid {M} [ordered_cancel_comm_monoid M] (S : submonoid M) : ordered_cancel_comm_monoid S := subtype.coe_injective.ordered_cancel_comm_monoid coe rfl (Ξ» _ _, rfl) /-- A submonoid of a `linear_ordered_cancel_comm_monoid` is a `linear_ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_cancel_add_comm_monoid` is a `linear_ordered_cancel_add_comm_monoid`."] instance to_linear_ordered_cancel_comm_monoid {M} [linear_ordered_cancel_comm_monoid M] (S : submonoid M) : linear_ordered_cancel_comm_monoid S := subtype.coe_injective.linear_ordered_cancel_comm_monoid coe rfl (Ξ» _ _, rfl) /-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/ @[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."] def subtype : S β†’* M := ⟨coe, rfl, Ξ» _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑S.subtype = coe := rfl /-- A submonoid is isomorphic to its image under an injective function -/ @[to_additive "An additive submonoid is isomorphic to its image under an injective function"] noncomputable def equiv_map_of_injective (f : M β†’* N) (hf : function.injective f) : S ≃* S.map f := { map_mul' := Ξ» _ _, subtype.ext (f.map_mul _ _), ..equiv.set.image f S hf } @[simp, to_additive] lemma coe_equiv_map_of_injective_apply (f : M β†’* N) (hf : function.injective f) (x : S) : (equiv_map_of_injective S f hf x : N) = f x := rfl /-- An induction principle on elements of the type `submonoid.closure s`. If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p` holds for all elements of the closure of `s`. The difference with `submonoid.closure_induction` is that this acts on the subtype. -/ @[to_additive "An induction principle on elements of the type `add_submonoid.closure s`. If `p` holds for `0` and all elements of `s`, and is preserved under addition, then `p` holds for all elements of the closure of `s`. The difference with `add_submonoid.closure_induction` is that this acts on the subtype."] lemma closure_induction' (s : set M) {p : closure s β†’ Prop} (Hs : βˆ€ x (h : x ∈ s), p ⟨x, subset_closure h⟩) (H1 : p 1) (Hmul : βˆ€ x y, p x β†’ p y β†’ p (x * y)) (x : closure s) : p x := subtype.rec_on x $ Ξ» x hx, begin refine exists.elim _ (Ξ» (hx : x ∈ closure s) (hc : p ⟨x, hx⟩), hc), exact closure_induction hx (Ξ» x hx, ⟨subset_closure hx, Hs x hx⟩) ⟨one_mem _, H1⟩ (Ξ» x y hx hy, exists.elim hx $ Ξ» hx' hx, exists.elim hy $ Ξ» hy' hy, ⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩), end attribute [elab_as_eliminator] submonoid.closure_induction' add_submonoid.closure_induction' /-- Given `submonoid`s `s`, `t` of monoids `M`, `N` respectively, `s Γ— t` as a submonoid of `M Γ— N`. -/ @[to_additive prod "Given `add_submonoid`s `s`, `t` of `add_monoid`s `A`, `B` respectively, `s Γ— t` as an `add_submonoid` of `A Γ— B`."] def prod (s : submonoid M) (t : submonoid N) : submonoid (M Γ— N) := { carrier := (s : set M).prod t, one_mem' := ⟨s.one_mem, t.one_mem⟩, mul_mem' := Ξ» p q hp hq, ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ } @[to_additive coe_prod] lemma coe_prod (s : submonoid M) (t : submonoid N) : (s.prod t : set (M Γ— N)) = (s : set M).prod (t : set N) := rfl @[to_additive mem_prod] lemma mem_prod {s : submonoid M} {t : submonoid N} {p : M Γ— N} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[to_additive prod_mono] lemma prod_mono {s₁ sβ‚‚ : submonoid M} {t₁ tβ‚‚ : submonoid N} (hs : s₁ ≀ sβ‚‚) (ht : t₁ ≀ tβ‚‚) : s₁.prod t₁ ≀ sβ‚‚.prod tβ‚‚ := set.prod_mono hs ht @[to_additive prod_top] lemma prod_top (s : submonoid M) : s.prod (⊀ : submonoid N) = s.comap (monoid_hom.fst M N) := ext $ Ξ» x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (s : submonoid N) : (⊀ : submonoid M).prod s = s.comap (monoid_hom.snd M N) := ext $ Ξ» x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊀ : submonoid M).prod (⊀ : submonoid N) = ⊀ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (βŠ₯ : submonoid M).prod (βŠ₯ : submonoid N) = βŠ₯ := set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk] /-- The product of submonoids is isomorphic to their product as monoids. -/ @[to_additive prod_equiv "The product of additive submonoids is isomorphic to their product as additive monoids"] def prod_equiv (s : submonoid M) (t : submonoid N) : s.prod t ≃* s Γ— t := { map_mul' := Ξ» x y, rfl, .. equiv.set.prod ↑s ↑t } open monoid_hom @[to_additive] lemma map_inl (s : submonoid M) : s.map (inl M N) = s.prod βŠ₯ := ext $ Ξ» p, ⟨λ ⟨x, hx, hp⟩, hp β–Έ ⟨hx, set.mem_singleton 1⟩, Ξ» ⟨hps, hp1⟩, ⟨p.1, hps, prod.ext rfl $ (set.eq_of_mem_singleton hp1).symm⟩⟩ @[to_additive] lemma map_inr (s : submonoid N) : s.map (inr M N) = prod βŠ₯ s := ext $ Ξ» p, ⟨λ ⟨x, hx, hp⟩, hp β–Έ ⟨set.mem_singleton 1, hx⟩, Ξ» ⟨hp1, hps⟩, ⟨p.2, hps, prod.ext (set.eq_of_mem_singleton hp1).symm rfl⟩⟩ @[simp, to_additive prod_bot_sup_bot_prod] lemma prod_bot_sup_bot_prod (s : submonoid M) (t : submonoid N) : (s.prod βŠ₯) βŠ” (prod βŠ₯ t) = s.prod t := le_antisymm (sup_le (prod_mono (le_refl s) bot_le) (prod_mono bot_le (le_refl t))) $ assume p hp, prod.fst_mul_snd p β–Έ mul_mem _ ((le_sup_left : s.prod βŠ₯ ≀ s.prod βŠ₯ βŠ” prod βŠ₯ t) ⟨hp.1, set.mem_singleton 1⟩) ((le_sup_right : prod βŠ₯ t ≀ s.prod βŠ₯ βŠ” prod βŠ₯ t) ⟨set.mem_singleton 1, hp.2⟩) @[to_additive] lemma mem_map_equiv {f : M ≃* N} {K : submonoid M} {x : N} : x ∈ K.map f.to_monoid_hom ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x @[to_additive] lemma map_equiv_eq_comap_symm (f : M ≃* N) (K : submonoid M) : K.map f.to_monoid_hom = K.comap f.symm.to_monoid_hom := set_like.coe_injective (f.to_equiv.image_eq_preimage K) @[to_additive] lemma comap_equiv_eq_map_symm (f : N ≃* M) (K : submonoid M) : K.comap f.to_monoid_hom = K.map f.symm.to_monoid_hom := (map_equiv_eq_comap_symm f.symm K).symm end submonoid namespace monoid_hom open submonoid /-- For many categories (monoids, modules, rings, ...) the set-theoretic image of a morphism `f` is a subobject of the codomain. When this is the case, it is useful to define the range of a morphism in such a way that the underlying carrier set of the range subobject is definitionally `set.range f`. In particular this means that the types `β†₯(set.range f)` and `β†₯f.range` are interchangeable without proof obligations. A convenient candidate definition for range which is mathematically correct is `map ⊀ f`, just as `set.range` could have been defined as `f '' set.univ`. However, this lacks the desired definitional convenience, in that it both does not match `set.range`, and that it introduces a redudant `x ∈ ⊀` term which clutters proofs. In such a case one may resort to the `copy` pattern. A `copy` function converts the definitional problem for the carrier set of a subobject into a one-off propositional proof obligation which one discharges while writing the definition of the definitionally convenient range (the parameter `hs` in the example below). A good example is the case of a morphism of monoids. A convenient definition for `monoid_hom.mrange` would be `(⊀ : submonoid M).map f`. However since this lacks the required definitional convenience, we first define `submonoid.copy` as follows: ```lean protected def copy (S : submonoid M) (s : set M) (hs : s = S) : submonoid M := { carrier := s, one_mem' := hs.symm β–Έ S.one_mem', mul_mem' := hs.symm β–Έ S.mul_mem' } ``` and then finally define: ```lean def mrange (f : M β†’* N) : submonoid N := ((⊀ : submonoid M).map f).copy (set.range f) set.image_univ.symm ``` -/ library_note "range copy pattern" /-- The range of a monoid homomorphism is a submonoid. See Note [range copy pattern]. -/ @[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."] def mrange (f : M β†’* N) : submonoid N := ((⊀ : submonoid M).map f).copy (set.range f) set.image_univ.symm @[simp, to_additive] lemma coe_mrange (f : M β†’* N) : (f.mrange : set N) = set.range f := rfl @[simp, to_additive] lemma mem_mrange {f : M β†’* N} {y : N} : y ∈ f.mrange ↔ βˆƒ x, f x = y := iff.rfl @[to_additive] lemma mrange_eq_map (f : M β†’* N) : f.mrange = (⊀ : submonoid M).map f := by ext; simp @[to_additive] lemma map_mrange (g : N β†’* P) (f : M β†’* N) : f.mrange.map g = (g.comp f).mrange := by simpa only [mrange_eq_map] using (⊀ : submonoid M).map_map g f @[to_additive] lemma mrange_top_iff_surjective {N} [mul_one_class N] {f : M β†’* N} : f.mrange = (⊀ : submonoid N) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_mrange, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid hom is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."] lemma mrange_top_of_surjective {N} [mul_one_class N] (f : M β†’* N) (hf : function.surjective f) : f.mrange = (⊀ : submonoid N) := mrange_top_iff_surjective.2 hf @[to_additive] lemma mclosure_preimage_le (f : M β†’* N) (s : set N) : closure (f ⁻¹' s) ≀ (closure s).comap f := closure_le.2 $ Ξ» x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set."] lemma map_mclosure (f : M β†’* N) (s : set M) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (mclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) /-- Restriction of a monoid hom to a submonoid of the domain. -/ @[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."] def mrestrict {N : Type*} [mul_one_class N] (f : M β†’* N) (S : submonoid M) : S β†’* N := f.comp S.subtype @[simp, to_additive] lemma mrestrict_apply {N : Type*} [mul_one_class N] (f : M β†’* N) (x : S) : f.mrestrict S x = f x := rfl /-- Restriction of a monoid hom to a submonoid of the codomain. -/ @[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain.", simps] def cod_mrestrict (f : M β†’* N) (S : submonoid N) (h : βˆ€ x, f x ∈ S) : M β†’* S := { to_fun := Ξ» n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := Ξ» x y, subtype.eq (f.map_mul x y) } /-- Restriction of a monoid hom to its range interpreted as a submonoid. -/ @[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."] def mrange_restrict {N} [mul_one_class N] (f : M β†’* N) : M β†’* f.mrange := f.cod_mrestrict f.mrange $ Ξ» x, ⟨x, rfl⟩ @[simp, to_additive] lemma coe_mrange_restrict {N} [mul_one_class N] (f : M β†’* N) (x : M) : (f.mrange_restrict x : N) = f x := rfl /-- The multiplicative kernel of a monoid homomorphism is the submonoid of elements `x : G` such that `f x = 1` -/ @[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_submonoid` of elements such that `f x = 0`"] def mker (f : M β†’* N) : submonoid M := (βŠ₯ : submonoid N).comap f @[to_additive] lemma mem_mker (f : M β†’* N) {x : M} : x ∈ f.mker ↔ f x = 1 := iff.rfl @[to_additive] lemma coe_mker (f : M β†’* N) : (f.mker : set M) = (f : M β†’ N) ⁻¹' {1} := rfl @[to_additive] instance decidable_mem_mker [decidable_eq N] (f : M β†’* N) : decidable_pred (∈ f.mker) := Ξ» x, decidable_of_iff (f x = 1) f.mem_mker @[to_additive] lemma comap_mker (g : N β†’* P) (f : M β†’* N) : g.mker.comap f = (g.comp f).mker := rfl @[simp, to_additive] lemma comap_bot' (f : M β†’* N) : (βŠ₯ : submonoid N).comap f = f.mker := rfl @[to_additive] lemma range_restrict_mker (f : M β†’* N) : mker (mrange_restrict f) = mker f := begin ext, change (⟨f x, _⟩ : mrange f) = ⟨1, _⟩ ↔ f x = 1, simp only [], end @[simp, to_additive] lemma mker_one : (1 : M β†’* N).mker = ⊀ := by { ext, simp [mem_mker] } @[to_additive] lemma prod_map_comap_prod' {M' : Type*} {N' : Type*} [mul_one_class M'] [mul_one_class N'] (f : M β†’* N) (g : M' β†’* N') (S : submonoid N) (S' : submonoid N') : (S.prod S').comap (prod_map f g) = (S.comap f).prod (S'.comap g) := set_like.coe_injective $ set.preimage_prod_map_prod f g _ _ @[to_additive] lemma mker_prod_map {M' : Type*} {N' : Type*} [mul_one_class M'] [mul_one_class N'] (f : M β†’* N) (g : M' β†’* N') : (prod_map f g).mker = f.mker.prod g.mker := by rw [←comap_bot', ←comap_bot', ←comap_bot', ←prod_map_comap_prod', bot_prod_bot] end monoid_hom namespace submonoid open monoid_hom @[to_additive] lemma mrange_inl : (inl M N).mrange = prod ⊀ βŠ₯ := by simpa only [mrange_eq_map] using map_inl ⊀ @[to_additive] lemma mrange_inr : (inr M N).mrange = prod βŠ₯ ⊀ := by simpa only [mrange_eq_map] using map_inr ⊀ @[to_additive] lemma mrange_inl' : (inl M N).mrange = comap (snd M N) βŠ₯ := mrange_inl.trans (top_prod _) @[to_additive] lemma mrange_inr' : (inr M N).mrange = comap (fst M N) βŠ₯ := mrange_inr.trans (prod_top _) @[simp, to_additive] lemma mrange_fst : (fst M N).mrange = ⊀ := (fst M N).mrange_top_of_surjective $ @prod.fst_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_snd : (snd M N).mrange = ⊀ := (snd M N).mrange_top_of_surjective $ @prod.snd_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_inl_sup_mrange_inr : (inl M N).mrange βŠ” (inr M N).mrange = ⊀ := by simp only [mrange_inl, mrange_inr, prod_bot_sup_bot_prod, top_prod_top] /-- The monoid hom associated to an inclusion of submonoids. -/ @[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."] def inclusion {S T : submonoid M} (h : S ≀ T) : S β†’* T := S.subtype.cod_mrestrict _ (Ξ» x, h x.2) @[simp, to_additive] lemma range_subtype (s : submonoid M) : s.subtype.mrange = s := set_like.coe_injective $ (coe_mrange _).trans $ subtype.range_coe @[to_additive] lemma eq_top_iff' : S = ⊀ ↔ βˆ€ x : M, x ∈ S := eq_top_iff.trans ⟨λ h m, h $ mem_top m, Ξ» h m _, h m⟩ @[to_additive] lemma eq_bot_iff_forall : S = βŠ₯ ↔ βˆ€ x ∈ S, x = (1 : M) := begin split, { intros h x x_in, rwa [h, mem_bot] at x_in }, { intros h, ext x, rw mem_bot, exact ⟨h x, by { rintros rfl, exact S.one_mem }⟩ }, end @[to_additive] lemma nontrivial_iff_exists_ne_one (S : submonoid M) : nontrivial S ↔ βˆƒ x ∈ S, x β‰  (1:M) := begin split, { introI h, rcases exists_ne (1 : S) with ⟨⟨h, h_in⟩, h_ne⟩, use [h, h_in], intro hyp, apply h_ne, simpa [hyp] }, { rintros ⟨x, x_in, hx⟩, apply nontrivial_of_ne (⟨x, x_in⟩ : S) 1, intro hyp, apply hx, simpa [has_one.one] using hyp }, end /-- A submonoid is either the trivial submonoid or nontrivial. -/ @[to_additive] lemma bot_or_nontrivial (S : submonoid M) : S = βŠ₯ ∨ nontrivial S := begin classical, by_cases h : βˆ€ x ∈ S, x = (1 : M), { left, exact S.eq_bot_iff_forall.mpr h }, { right, push_neg at h, simpa [nontrivial_iff_exists_ne_one] using h }, end /-- A submonoid is either the trivial submonoid or contains a nonzero element. -/ @[to_additive] lemma bot_or_exists_ne_one (S : submonoid M) : S = βŠ₯ ∨ βˆƒ x ∈ S, x β‰  (1:M) := begin convert S.bot_or_nontrivial, rw nontrivial_iff_exists_ne_one end end submonoid namespace mul_equiv variables {S} {T : submonoid M} /-- Makes the identity isomorphism from a proof that two submonoids of a multiplicative monoid are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two submonoids of an additive monoid are equal."] def submonoid_congr (h : S = T) : S ≃* T := { map_mul' := Ξ» _ _, rfl, ..equiv.set_congr $ congr_arg _ h } -- this name is primed so that the version to `f.range` instead of `f.mrange` can be unprimed. /-- A monoid homomorphism `f : M β†’* N` with a left-inverse `g : N β†’ M` defines a multiplicative equivalence between `M` and `f.mrange`. This is a bidirectional version of `monoid_hom.mrange_restrict`. -/ @[to_additive /-" An additive monoid homomorphism `f : M β†’+ N` with a left-inverse `g : N β†’ M` defines an additive equivalence between `M` and `f.mrange`. This is a bidirectional version of `add_monoid_hom.mrange_restrict`. "-/, simps {simp_rhs := tt}] def of_left_inverse' (f : M β†’* N) {g : N β†’ M} (h : function.left_inverse g f) : M ≃* f.mrange := { to_fun := f.mrange_restrict, inv_fun := g ∘ f.mrange.subtype, left_inv := h, right_inv := Ξ» x, subtype.ext $ let ⟨x', hx'⟩ := monoid_hom.mem_mrange.mp x.prop in show f (g x) = x, by rw [←hx', h x'], .. f.mrange_restrict } /-- A `mul_equiv` `Ο†` between two monoids `M` and `N` induces a `mul_equiv` between a submonoid `S ≀ M` and the submonoid `Ο†(S) ≀ N`. -/ @[to_additive "An `add_equiv` `Ο†` between two additive monoids `M` and `N` induces an `add_equiv` between a submonoid `S ≀ M` and the submonoid `Ο†(S) ≀ N`. "] def submonoid_equiv_map (e : M ≃* N) (S : submonoid M) : S ≃* S.map e.to_monoid_hom := { map_mul' := Ξ» _ _, subtype.ext (e.map_mul _ _), ..equiv.image e.to_equiv S } end mul_equiv section actions /-! ### Actions by `submonoid`s These instances tranfer the action by an element `m : M` of a monoid `M` written as `m β€’ a` onto the action by an element `s : S` of a submonoid `S : submonoid M` such that `s β€’ a = (s : M) β€’ a`. These instances work particularly well in conjunction with `monoid.to_mul_action`, enabling `s β€’ m` as an alias for `↑s * m`. -/ namespace submonoid variables {M' : Type*} {Ξ± Ξ² : Type*} [monoid M'] /-- The action by a submonoid is the action by the underlying monoid. -/ @[to_additive /-"The additive action by an add_submonoid is the action by the underlying add_monoid. "-/] instance [mul_action M' Ξ±] (S : submonoid M') : mul_action S Ξ± := mul_action.comp_hom _ S.subtype @[to_additive] lemma smul_def [mul_action M' Ξ±] {S : submonoid M'} (g : S) (m : Ξ±) : g β€’ m = (g : M') β€’ m := rfl /-- The action by a submonoid is the action by the underlying monoid. -/ instance [add_monoid Ξ±] [distrib_mul_action M' Ξ±] (S : submonoid M') : distrib_mul_action S Ξ± := distrib_mul_action.comp_hom _ S.subtype /-- The action by a submonoid is the action by the underlying monoid. -/ instance [monoid Ξ±] [mul_distrib_mul_action M' Ξ±] (S : submonoid M') : mul_distrib_mul_action S Ξ± := mul_distrib_mul_action.comp_hom _ S.subtype @[to_additive] instance smul_comm_class_left [mul_action M' Ξ²] [has_scalar Ξ± Ξ²] [smul_comm_class M' Ξ± Ξ²] (S : submonoid M') : smul_comm_class S Ξ± Ξ² := ⟨λ a, (smul_comm (a : M') : _)⟩ @[to_additive] instance smul_comm_class_right [has_scalar Ξ± Ξ²] [mul_action M' Ξ²] [smul_comm_class Ξ± M' Ξ²] (S : submonoid M') : smul_comm_class Ξ± S Ξ² := ⟨λ a s, (smul_comm a (s : M') : _)⟩ /-- Note that this provides `is_scalar_tower S M' M'` which is needed by `smul_mul_assoc`. -/ instance [has_scalar Ξ± Ξ²] [mul_action M' Ξ±] [mul_action M' Ξ²] [is_scalar_tower M' Ξ± Ξ²] (S : submonoid M') : is_scalar_tower S Ξ± Ξ² := ⟨λ a, (smul_assoc (a : M') : _)⟩ example {S : submonoid M'} : is_scalar_tower S M' M' := by apply_instance instance [mul_action M' Ξ±] [has_faithful_scalar M' Ξ±] (S : submonoid M') : has_faithful_scalar S Ξ± := { eq_of_smul_eq_smul := Ξ» x y h, subtype.ext (eq_of_smul_eq_smul h) } end submonoid end actions
187970e9eea2e4d4067dcf70337b6f58c8275177
ec5e5a9dbe7f60fa5784d15211d8bf24ada0825c
/src/SimMonad.lean
3d28b75c12b66514bebe2ed3a3b6d2425902d2bc
[]
no_license
pnwamk/lean-llvm
fcd9a828e52e80eb197f7d9032b3846f2e09ef74
ebc3bca9a57a6aef29529d46394f560398fb5c9c
refs/heads/master
1,668,418,078,706
1,593,548,643,000
1,593,548,643,000
258,617,753
0
0
null
1,587,760,298,000
1,587,760,298,000
null
UTF-8
Lean
false
false
6,765
lean
import Init.Data.Array import Init.Data.Int import Init.Data.RBMap import Init.Data.String import Galois.Data.Bitvec import LeanLLVM.AST import LeanLLVM.PP import LeanLLVM.TypeContext import LeanLLVM.Value namespace LLVM inductive trace_event : Type | load (ptr : bitvec 64) (mt:mem_type) (val:Sim.Value) | store (ptr : bitvec 64) (mt:mem_type) (val:Sim.Value) | alloca (ptr : bitvec 64) (sz : Nat) namespace trace_event def asString : trace_event -> String | load ptr mt val => "LOAD " ++ ptr.pp_hex ++ " " ++ val.pretty.render | store ptr mt val => "STORE " ++ ptr.pp_hex ++ " " ++ val.pretty.render | alloca ptr sz => "ALLOCA " ++ ptr.pp_hex ++ " " ++ toString sz end trace_event structure frame := (locals : Sim.regMap) (func : Define) (curr : BlockLabel) (prev : Option BlockLabel) (framePtr : Nat) instance frameInh : Inhabited frame := ⟨{ locals := RBMap.empty, func := { linkage := none, retType := PrimType.void, name := ⟨""⟩, args := Array.empty, varArgs := false, attrs := Array.empty, sec := none, gc := none, body := Array.empty, metadata := Strmap.empty, comdat := none }, curr := ⟨Ident.named ""⟩, prev := none, framePtr := 0 }⟩ structure State := (mem : Sim.memMap) (mod : Module) (dl : DataLayout) (heapAllocPtr : Nat) (stackPtr : Nat) (symmap : @RBMap Symbol (bitvec 64) (Ξ»x y => decide (x < y))) (revsymmap : @RBMap (bitvec 64) Symbol (Ξ»x y => decide (bitvec.ult x y))) structure SimConts (z:Type) := (kerr : IO.Error β†’ z) /- error continuation -/ (kret : Option Sim.Value β†’ State β†’ z) /- return continuation -/ (kcall : (Option Sim.Value β†’ State β†’ z) β†’ Symbol β†’ List Sim.Value β†’ State β†’ z) /- call continuation -/ (kjump : BlockLabel β†’ frame β†’ State β†’ z) /- jump continuation -/ (ktrace : trace_event -> z -> z ) /- trace operation -/ structure Sim (a:Type) := (runSim : βˆ€{z:Type}, SimConts z β†’ (a β†’ frame β†’ State β†’ z) β†’ (frame β†’ State β†’ z)) namespace Sim instance {a:Type} : Inhabited (Sim a) := ⟨ ⟨λz conts k f st => conts.kerr (IO.userError "black hole")⟩ ⟩ instance monad : Monad Sim := { bind := Ξ»a b mx mf => Sim.mk (Ξ»z conts k => mx.runSim conts (Ξ»x => (mf x).runSim conts k)) , pure := Ξ»a x => Sim.mk (Ξ»z _ k => k x) } instance monadExcept : MonadExcept IO.Error Sim := { throw := Ξ»a err => Sim.mk (Ξ»z conts _k _frm _st => conts.kerr err) , catch := Ξ»a m handle => Sim.mk (Ξ»z conts k frm st => let conts' := { conts with kerr := Ξ»err => (handle err).runSim conts k frm st }; m.runSim conts' k frm st) }. def setFrame (frm:frame) : Sim Unit := Sim.mk (Ξ»z _ k _ st => k () frm st). def getFrame : Sim frame := Sim.mk (Ξ»z _ k frm st => k frm frm st). def modifyFrame (f: frame β†’ frame) : Sim Unit := Sim.mk (Ξ»z _ k frm st => k () (f frm) st). def getState : Sim State := Sim.mk (Ξ»z _ k frm st => k st frm st). def setState (st:State) : Sim Unit := Sim.mk (Ξ»z _ k frm _ => k () frm st). def assignReg (reg:Ident) (v:Value) : Sim Unit := modifyFrame (Ξ»frm => { frm with locals := RBMap.insert frm.locals reg v }). def trace (ev:trace_event) : Sim Unit := Sim.mk (Ξ»z conts k frm st => conts.ktrace ev (k () frm st)) def lookupReg (reg:Ident) : Sim Value := do frm <- getFrame; match RBMap.find? frm.locals reg with | none => throw (IO.userError ("unassigned register: " ++ reg.asString)) | some v => pure v def returnVoid {a} : Sim a := Sim.mk (Ξ»z conts _k frm st => conts.kret none { st with stackPtr := frm.framePtr }). def returnValue {a} (v:Sim.Value) : Sim a := Sim.mk (Ξ»z conts _k frm st => conts.kret (some v) { st with stackPtr := frm.framePtr }). def jump {a} (l:BlockLabel) : Sim a := Sim.mk (Ξ»z conts _k frm st => conts.kjump l frm st). def call (s:Symbol) (args:List Value) : Sim (Option Value) := Sim.mk (Ξ»z conts k frm st => conts.kcall (Ξ»v => k v frm) s args st). def eval_mem_type (t:LLVMType) : Sim mem_type := do st <- Sim.getState; match lift_mem_type st.dl st.mod.types t with | none => throw (IO.userError ("could not lift type: " ++ (pp t).render)) | some mt => pure mt partial def eval : mem_type β†’ LLVM.Value β†’ Sim Sim.Value | _, Value.ident i => Sim.lookupReg i | mem_type.int w, Value.integer n => pure (Value.bv (bitvec.of_int w n)) | mem_type.int w, Value.bool true => pure (Value.bv (bitvec.of_int w 1)) | mem_type.int w, Value.bool false => pure (Value.bv (bitvec.of_int w 0)) | mem_type.int w, Value.null => pure (Value.bv (bitvec.of_int w 0)) | mem_type.int w, Value.zeroInit => pure (Value.bv (bitvec.of_int w 0)) | mem_type.int w, Value.undef => pure (Value.bv (bitvec.of_int w 0)) --??? | mem_type.ptr _, Value.symbol s => do st <- Sim.getState; match st.symmap.find? s with | some ptr => pure (Value.bv ptr) | none => throw (IO.userError ("could not resolve symbol: " ++ s.symbol)) | mem_type.array _n eltp, LLVM.Value.array _tp vs => do vs' <- vs.mapM (eval eltp); pure (Value.array eltp vs') | _, v => throw (IO.userError ("bad Value/type in evaluation: " ++ (pp v).render)) def eval_typed (tv:Typed LLVM.Value) : Sim Sim.Value := do mt <- eval_mem_type tv.type; eval mt tv.value end Sim -- Heap allocation counts up. Find the next aligned Value and return it, -- advancing the heap allocation pointer x bytes beyond. def allocOnHeap (sz:Nat) (a:Alignment) (st:State) : bitvec 64 Γ— State := let ptr := padToAlignment st.heapAllocPtr a; (bitvec.of_nat 64 ptr, { st with heapAllocPtr := ptr + sz }) -- Stack allocation counts down. Find the next aligned Value that provides -- enough space and return it, advancing the stack pointer to this point. def allocOnStack (sz:Nat) (a:Alignment) (st:State) : bitvec 64 Γ— State := let ptr := padDownToAlignment (st.stackPtr - sz) a; ( bitvec.of_nat 64 ptr, { st with stackPtr := ptr }) def allocFunctionSymbol (s:Symbol) (st:State) : State := let (ptr, st') := allocOnHeap 16 align16 st; -- 16 bytes with 16 byte alignment, rather arbitrarily { st' with symmap := st.symmap.insert s ptr, revsymmap := st.revsymmap.insert ptr s, } def linkSymbol (st:State) (x:Symbol Γ— bitvec 64) : State := let (s,ptr) := x; { st with symmap := st.symmap.insert s ptr, revsymmap := st.revsymmap.insert ptr s } def initializeState (mod : Module) (dl:DataLayout) (ls:List (Symbol Γ— bitvec 64)) : State := ls.foldl linkSymbol { mem := RBMap.empty , mod := mod , dl := dl , heapAllocPtr := 0 , stackPtr := 2^64 , symmap := RBMap.empty , revsymmap := RBMap.empty } end LLVM
b921c267992bdc6e7712e8fd0cbaea9c012a184e
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/spectral/hom.lean
60fc940ec9401746e2723f663d3f22e200a8f0d0
[ "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,758
lean
/- Copyright (c) 2022 YaΓ«l Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: YaΓ«l Dillies -/ import topology.continuous_function.basic /-! # Spectral maps This file defines spectral maps. A map is spectral when it's continuous and the preimage of a compact open set is compact open. ## Main declarations * `is_spectral_map`: Predicate for a map to be spectral. * `spectral_map`: Bundled spectral maps. * `spectral_map_class`: Typeclass for a type to be a type of spectral maps. ## TODO Once we have `spectral_space`, `is_spectral_map` should move to `topology.spectral.basic`. -/ open function order_dual variables {F Ξ± Ξ² Ξ³ Ξ΄ : Type*} section unbundled variables [topological_space Ξ±] [topological_space Ξ²] [topological_space Ξ³] {f : Ξ± β†’ Ξ²} {s : set Ξ²} /-- A function between topological spaces is spectral if it is continuous and the preimage of every compact open set is compact open. -/ structure is_spectral_map (f : Ξ± β†’ Ξ²) extends continuous f : Prop := (compact_preimage_of_open ⦃s : set β⦄ : is_open s β†’ is_compact s β†’ is_compact (f ⁻¹' s)) lemma is_compact.preimage_of_open (hf : is_spectral_map f) (hβ‚€ : is_compact s) (h₁ : is_open s) : is_compact (f ⁻¹' s) := hf.compact_preimage_of_open h₁ hβ‚€ lemma is_spectral_map.continuous {f : Ξ± β†’ Ξ²} (hf : is_spectral_map f) : continuous f := hf.to_continuous lemma is_spectral_map_id : is_spectral_map (@id Ξ±) := ⟨continuous_id, Ξ» s _, id⟩ lemma is_spectral_map.comp {f : Ξ² β†’ Ξ³} {g : Ξ± β†’ Ξ²} (hf : is_spectral_map f) (hg : is_spectral_map g) : is_spectral_map (f ∘ g) := ⟨hf.continuous.comp hg.continuous, Ξ» s hsβ‚€ hs₁, (hs₁.preimage_of_open hf hsβ‚€).preimage_of_open hg (hsβ‚€.preimage hf.continuous)⟩ end unbundled /-- The type of spectral maps from `Ξ±` to `Ξ²`. -/ structure spectral_map (Ξ± Ξ² : Type*) [topological_space Ξ±] [topological_space Ξ²] := (to_fun : Ξ± β†’ Ξ²) (spectral' : is_spectral_map to_fun) /-- `spectral_map_class F Ξ± Ξ²` states that `F` is a type of spectral maps. You should extend this class when you extend `spectral_map`. -/ class spectral_map_class (F : Type*) (Ξ± Ξ² : out_param $ Type*) [topological_space Ξ±] [topological_space Ξ²] extends fun_like F Ξ± (Ξ» _, Ξ²) := (map_spectral (f : F) : is_spectral_map f) export spectral_map_class (map_spectral) attribute [simp] map_spectral @[priority 100] -- See note [lower instance priority] instance spectral_map_class.to_continuous_map_class [topological_space Ξ±] [topological_space Ξ²] [spectral_map_class F Ξ± Ξ²] : continuous_map_class F Ξ± Ξ² := ⟨λ f, (map_spectral f).continuous⟩ instance [topological_space Ξ±] [topological_space Ξ²] [spectral_map_class F Ξ± Ξ²] : has_coe_t F (spectral_map Ξ± Ξ²) := ⟨λ f, ⟨_, map_spectral f⟩⟩ /-! ### Spectral maps -/ namespace spectral_map variables [topological_space Ξ±] [topological_space Ξ²] [topological_space Ξ³] [topological_space Ξ΄] /-- Reinterpret a `spectral_map` as a `continuous_map`. -/ def to_continuous_map (f : spectral_map Ξ± Ξ²) : continuous_map Ξ± Ξ² := ⟨_, f.spectral'.continuous⟩ instance : spectral_map_class (spectral_map Ξ± Ξ²) Ξ± Ξ² := { coe := spectral_map.to_fun, coe_injective' := Ξ» f g h, by { cases f, cases g, congr' }, map_spectral := Ξ» f, f.spectral' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (spectral_map Ξ± Ξ²) (Ξ» _, Ξ± β†’ Ξ²) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {f : spectral_map Ξ± Ξ²} : f.to_fun = (f : Ξ± β†’ Ξ²) := rfl @[ext] lemma ext {f g : spectral_map Ξ± Ξ²} (h : βˆ€ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `spectral_map` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : spectral_map Ξ± Ξ²) (f' : Ξ± β†’ Ξ²) (h : f' = f) : spectral_map Ξ± Ξ² := ⟨f', h.symm.subst f.spectral'⟩ variables (Ξ±) /-- `id` as a `spectral_map`. -/ protected def id : spectral_map Ξ± Ξ± := ⟨id, is_spectral_map_id⟩ instance : inhabited (spectral_map Ξ± Ξ±) := ⟨spectral_map.id α⟩ @[simp] lemma coe_id : ⇑(spectral_map.id Ξ±) = id := rfl variables {Ξ±} @[simp] lemma id_apply (a : Ξ±) : spectral_map.id Ξ± a = a := rfl /-- Composition of `spectral_map`s as a `spectral_map`. -/ def comp (f : spectral_map Ξ² Ξ³) (g : spectral_map Ξ± Ξ²) : spectral_map Ξ± Ξ³ := ⟨f.to_continuous_map.comp g.to_continuous_map, f.spectral'.comp g.spectral'⟩ @[simp] lemma coe_comp (f : spectral_map Ξ² Ξ³) (g : spectral_map Ξ± Ξ²) : (f.comp g : Ξ± β†’ Ξ³) = f ∘ g := rfl @[simp] lemma comp_apply (f : spectral_map Ξ² Ξ³) (g : spectral_map Ξ± Ξ²) (a : Ξ±) : (f.comp g) a = f (g a) := rfl @[simp] lemma coe_comp_continuous_map (f : spectral_map Ξ² Ξ³) (g : spectral_map Ξ± Ξ²) : (f.comp g : continuous_map Ξ± Ξ³) = (f : continuous_map Ξ² Ξ³).comp g := rfl @[simp] lemma comp_assoc (f : spectral_map Ξ³ Ξ΄) (g : spectral_map Ξ² Ξ³) (h : spectral_map Ξ± Ξ²) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : spectral_map Ξ± Ξ²) : f.comp (spectral_map.id Ξ±) = f := ext $ Ξ» a, rfl @[simp] lemma id_comp (f : spectral_map Ξ± Ξ²) : (spectral_map.id Ξ²).comp f = f := ext $ Ξ» a, rfl lemma cancel_right {g₁ gβ‚‚ : spectral_map Ξ² Ξ³} {f : spectral_map Ξ± Ξ²} (hf : surjective f) : g₁.comp f = gβ‚‚.comp f ↔ g₁ = gβ‚‚ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : spectral_map Ξ² Ξ³} {f₁ fβ‚‚ : spectral_map Ξ± Ξ²} (hg : injective g) : g.comp f₁ = g.comp fβ‚‚ ↔ f₁ = fβ‚‚ := ⟨λ h, ext $ Ξ» a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end spectral_map
07202db816190dda1e0e7e0a983ef07bf522b92c
618003631150032a5676f229d13a079ac875ff77
/src/combinatorics/composition.lean
d2d0bfde7b123a185881e5c74a691781d9337c01
[ "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
34,333
lean
/- Copyright (c) 2020 SΓ©bastien GouΓ«zel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import data.fintype.card import tactic.omega /-! # Compositions A composition of an integer `n` is a decomposition `n = iβ‚€ + ... + i_{k-1}` of `n` into a sum of positive integers. Combinatorially, it corresponds to a decomposition of `{0, ..., n-1}` into non-empty blocks of consecutive integers, where the `iβ±Ό` are the lengths of the blocks. This notion is closely related to that of a partition of `n`, but in a composition of `n` the order of the `iβ±Ό`s matters. We implement two different structures covering these two viewpoints on compositions. The first one, made of a list of positive integers summing to `n`, is the main one and is called `composition n`. The second one is useful for combinatorial arguments (for instance to show that the number of compositions of `n` is `2^(n-1)`). It is given by a subset of `{0, ..., n}` containing `0` and `n`, where the elements of the subset (other than `n`) correspond to the leftmost points of each block. The main API is built on `composition n`, and we provide an equivalence between the two types. ## Main functions * `c : composition n` is a structure, made of a list of integers which are all positive and add up to `n`. * `composition_card` states that the cardinality of `composition n` is exactly `2^(n-1)`, which is proved by constructing an equiv with `composition_as_set n` (see below), which is itself in bijection with the subsets of `fin (n-1)` (this holds even for `n = 0`, where `-` is nat subtraction). Let `c : composition n` be a composition of `n`. Then * `c.blocks` is the list of blocks in `c`. * `c.length` is the number of blocks in the composition. * `c.blocks_fun : fin c.length β†’ β„•` is the realization of `c.blocks` as a function on `fin c.length`. This is the main object when using compositions to understand the composition of analytic functions. * `c.size_up_to : β„• β†’ β„•` is the sum of the size of the blocks up to `i`.; * `c.embedding i : fin (c.blocks_fun i) β†’ fin n` is the increasing embedding of the `i`-th block in `fin n`; * `c.index j`, for `j : fin n`, is the index of the block containing `j`. Compositions can also be used to split lists. Let `l` be a list of length `n` and `c` a composition of `n`. * `l.split_wrt_composition c` is a list of lists, made of the slices of `l` corresponding to the blocks of `c`. * `join_split_wrt_composition` states that splitting a list and then joining it gives back the original list. * `split_wrt_composition_join` states that joining a list of lists, and then splitting it back according to the right composition, gives back the original list of lists. We turn to the second viewpoint on compositions, that we realize as a finset of `fin (n+1)`. `c : composition_as_set n` is a structure made of a finset of `fin (n+1)` called `c.boundaries` and proofs that it contains `0` and `n`. (Taking a finset of `fin n` containing `0` would not make sense in the edge case `n = 0`, while the previous description works in all cases). The elements of this set (other than `n`) correspond to leftmost points of blocks. Thus, there is an equiv between `composition n` and `composition_as_set n`. We only construct basic API on `composition_as_set` (notably `c.length` and `c.blocks`) to be able to construct this equiv, called `composition_equiv n`. Since there is a straightforward equiv between `composition_as_set n` and finsets of `{1, ..., n-1}` (obtained by removing `0` and `n` from a `composition_as_set` and called `composition_as_set_equiv n`), we deduce that `composition_as_set n` and `composition n` are both fintypes of cardinality `2^(n - 1)` (see `composition_as_set_card` and `composition_card`). ## Implementation details The main motivation for this structure and its API is in the construction of the composition of formal multilinear series, and the proof that the composition of analytic functions is analytic. The representation of a composition as a list is very handy as lists are very flexible and already have a well-developed API. ## Tags Composition, partition ## References <https://en.wikipedia.org/wiki/Composition_(combinatorics)> -/ open list open_locale classical variable {n : β„•} /-- A composition of `n` is a list of positive integers summing to `n`. -/ @[ext] structure composition (n : β„•) := (blocks : list β„•) (blocks_pos : βˆ€ {i}, i ∈ blocks β†’ 0 < i) (blocks_sum : blocks.sum = n) /-- Combinatorial viewpoint on a composition of `n`, by seeing it as non-empty blocks of consecutive integers in `{0, ..., n-1}`. We register every block by its left end-point, yielding a finset containing `0`. As this does not make sense for `n = 0`, we add `n` to this finset, and get a finset of `{0, ..., n}` containing `0` and `n`. This is the data in the structure `composition_as_set n`. -/ @[ext] structure composition_as_set (n : β„•) := (boundaries : finset (fin n.succ)) (zero_mem : (0 : fin n.succ) ∈ boundaries) (last_mem : (fin.last n ∈ boundaries)) instance {n : β„•} : inhabited (composition_as_set n) := ⟨⟨finset.univ, finset.mem_univ _, finset.mem_univ _⟩⟩ /-! ### Compositions A composition of an integer `n` is a decomposition `n = iβ‚€ + ... + i_{k-1}` of `n` into a sum of positive integers. -/ namespace composition variables (c : composition n) instance (n : β„•) : has_to_string (composition n) := ⟨λ c, to_string c.blocks⟩ /-- The length of a composition, i.e., the number of blocks in the composition. -/ @[reducible] def length : β„• := c.blocks.length lemma blocks_length : c.blocks.length = c.length := rfl /-- The blocks of a composition, seen as a function on `fin c.length`. When composing analytic functions using compositions, this is the main player. -/ def blocks_fun : fin c.length β†’ β„• := Ξ» i, nth_le c.blocks i.1 i.2 lemma of_fn_blocks_fun : of_fn c.blocks_fun = c.blocks := of_fn_nth_le _ lemma sum_blocks_fun : finset.univ.sum c.blocks_fun = n := by conv_rhs { rw [← c.blocks_sum, ← of_fn_blocks_fun, sum_of_fn] } @[simp] lemma one_le_blocks {i : β„•} (h : i ∈ c.blocks) : 1 ≀ i := c.blocks_pos h @[simp] lemma one_le_blocks' {i : β„•} (h : i < c.length) : 1 ≀ nth_le c.blocks i h:= c.one_le_blocks (nth_le_mem (blocks c) i h) @[simp] lemma blocks_pos' (i : β„•) (h : i < c.length) : 0 < nth_le c.blocks i h:= c.one_le_blocks' h lemma length_le : c.length ≀ n := begin conv_rhs { rw ← c.blocks_sum }, exact length_le_sum_of_one_le _ (Ξ» i hi, c.one_le_blocks hi) end lemma length_pos_of_pos (h : 0 < n) : 0 < c.length := begin apply length_pos_of_sum_pos, convert h, exact c.blocks_sum end /-- The sum of the sizes of the blocks in a composition up to `i`. -/ def size_up_to (i : β„•) : β„• := (c.blocks.take i).sum @[simp] lemma size_up_to_zero : c.size_up_to 0 = 0 := by simp [size_up_to] lemma size_up_to_of_length_le (i : β„•) (h : c.length ≀ i) : c.size_up_to i = n := begin dsimp [size_up_to], convert c.blocks_sum, exact take_all_of_le h end @[simp] lemma size_up_to_length : c.size_up_to c.length = n := c.size_up_to_of_length_le c.length (le_refl _) lemma size_up_to_le (i : β„•) : c.size_up_to i ≀ n := begin conv_rhs { rw [← c.blocks_sum, ← sum_take_add_sum_drop _ i] }, exact nat.le_add_right _ _ end lemma size_up_to_succ {i : β„•} (h : i < c.length) : c.size_up_to (i+1) = c.size_up_to i + c.blocks.nth_le i h := by { simp only [size_up_to], rw sum_take_succ _ _ h } lemma size_up_to_succ' (i : fin c.length) : c.size_up_to ((i : β„•) + 1) = c.size_up_to i + c.blocks_fun i := c.size_up_to_succ i.2 lemma size_up_to_strict_mono {i : β„•} (h : i < c.length) : c.size_up_to i < c.size_up_to (i+1) := by { rw c.size_up_to_succ h, simp } lemma monotone_size_up_to : monotone c.size_up_to := monotone_sum_take _ /-- The `i`-th boundary of a composition, i.e., the leftmost point of the `i`-th block. We include a virtual point at the right of the last block, to make for a nice equiv with `composition_as_set n`. -/ def boundary : fin (c.length + 1) β†’ fin (n+1) := Ξ» i, ⟨c.size_up_to i, nat.lt_succ_of_le (c.size_up_to_le i)⟩ @[simp] lemma boundary_zero : c.boundary 0 = 0 := by simp [boundary, fin.ext_iff] @[simp] lemma boundary_last : c.boundary (fin.last c.length) = fin.last n := by simp [boundary, fin.ext_iff] lemma strict_mono_boundary : strict_mono c.boundary := begin apply fin.strict_mono_iff_lt_succ.2 (Ξ» i hi, _), exact c.size_up_to_strict_mono ((add_lt_add_iff_right 1).mp hi) end /-- The boundaries of a composition, i.e., the leftmost point of all the blocks. We include a virtual point at the right of the last block, to make for a nice equiv with `composition_as_set n`. -/ def boundaries : finset (fin (n+1)) := finset.univ.image c.boundary lemma card_boundaries_eq_succ_length : c.boundaries.card = c.length + 1 := begin dsimp [boundaries], rw finset.card_image_of_injective finset.univ c.strict_mono_boundary.injective, simp end /-- To `c : composition n`, one can associate a `composition_as_set n` by registering the leftmost point of each block, and adding a virtual point at the right of the last block. -/ def to_composition_as_set : composition_as_set n := { boundaries := c.boundaries, zero_mem := begin simp only [boundaries, finset.mem_univ, exists_prop_of_true, finset.mem_image], exact ⟨0, rfl⟩, end, last_mem := begin simp only [boundaries, finset.mem_univ, exists_prop_of_true, finset.mem_image], exact ⟨fin.last c.length, c.boundary_last⟩, end } /-- The canonical increasing bijection between `fin (c.length + 1)` and `c.boundaries` is exactly `c.boundary`. -/ lemma mono_of_fin_boundaries : c.boundary = finset.mono_of_fin c.boundaries c.card_boundaries_eq_succ_length := begin apply finset.mono_of_fin_unique' _ _ c.strict_mono_boundary, assume i hi, simp [boundaries, - set.mem_range, set.mem_range_self] end /-- Embedding the `i`-th block of a composition (identified with `fin (c.blocks_fun i)`) into `fin n` at the relevant position. -/ def embedding (i : fin c.length) : fin (c.blocks_fun i) β†’ fin n := Ξ» j, ⟨c.size_up_to i.1 + j.val, calc c.size_up_to i.1 + j.val < c.size_up_to i.1 + c.blocks.nth_le i.1 i.2 : add_lt_add_left j.2 _ ... = c.size_up_to (i.1 + 1) : (c.size_up_to_succ _).symm ... ≀ n : by { conv_rhs { rw ← c.size_up_to_length }, exact monotone_sum_take _ i.2 } ⟩ lemma embedding_inj (i : fin c.length) : function.injective (c.embedding i) := Ξ» a b hab, by simpa [embedding, fin.ext_iff] using hab /-- `index_exists` asserts there is some `i` so `j < c.size_up_to (i+1)`. In the next definition we use `nat.find` to produce the minimal such index. -/ lemma index_exists {j : β„•} (h : j < n) : βˆƒ i : β„•, j < c.size_up_to i.succ ∧ i < c.length := begin have n_pos : 0 < n := lt_of_le_of_lt (zero_le j) h, have : 0 < c.blocks.sum, by rwa [← c.blocks_sum] at n_pos, have length_pos : 0 < c.blocks.length := length_pos_of_sum_pos (blocks c) this, refine ⟨c.length.pred, _, nat.pred_lt (ne_of_gt length_pos)⟩, have : c.length.pred.succ = c.length := nat.succ_pred_eq_of_pos length_pos, simp [this, h] end /-- `c.index j` is the index of the block in the composition `c` containing `j`. -/ def index (j : fin n) : fin c.length := ⟨nat.find (c.index_exists j.2), (nat.find_spec (c.index_exists j.2)).2⟩ lemma lt_size_up_to_index_succ (j : fin n) : j.val < c.size_up_to (c.index j).succ := (nat.find_spec (c.index_exists j.2)).1 lemma size_up_to_index_le (j : fin n) : c.size_up_to (c.index j) ≀ j := begin by_contradiction H, set i := c.index j with hi, push_neg at H, have i_pos : (0 : β„•) < i, { by_contradiction i_pos, push_neg at i_pos, simp [le_zero_iff_eq.mp i_pos, c.size_up_to_zero] at H, exact nat.not_succ_le_zero j H }, let i₁ := (i : β„•).pred, have i₁_lt_i : i₁ < i := nat.pred_lt (ne_of_gt i_pos), have i₁_succ : i₁.succ = i := nat.succ_pred_eq_of_pos i_pos, have := nat.find_min (c.index_exists j.2) i₁_lt_i, simp [lt_trans i₁_lt_i (c.index j).2, i₁_succ] at this, exact nat.lt_le_antisymm H this end /-- Mapping an element `j` of `fin n` to the element in the block containing it, identified with `fin (c.blocks_fun (c.index j))` through the canonical increasing bijection. -/ def inv_embedding (j : fin n) : fin (c.blocks_fun (c.index j)) := ⟨j - c.size_up_to (c.index j), begin rw [nat.sub_lt_right_iff_lt_add, add_comm, ← size_up_to_succ'], { exact lt_size_up_to_index_succ _ _ }, { exact size_up_to_index_le _ _ } end⟩ lemma embedding_comp_inv (j : fin n) : c.embedding (c.index j) (c.inv_embedding j) = j := begin rw fin.ext_iff, apply nat.add_sub_cancel' (c.size_up_to_index_le j), end lemma mem_range_embedding_iff {j : fin n} {i : fin c.length} : j ∈ set.range (c.embedding i) ↔ c.size_up_to i ≀ j ∧ (j : β„•) < c.size_up_to (i : β„•).succ := begin split, { assume h, rcases set.mem_range.2 h with ⟨k, hk⟩, rw fin.ext_iff at hk, change c.size_up_to i + k.val = (j : β„•) at hk, rw ← hk, simp [size_up_to_succ', k.2] }, { assume h, apply set.mem_range.2, refine ⟨⟨j.val - c.size_up_to i, _⟩, _⟩, { rw [nat.sub_lt_left_iff_lt_add, ← size_up_to_succ'], { exact h.2 }, { exact h.1 } }, { rw fin.ext_iff, exact nat.add_sub_cancel' h.1 } } end /-- The embeddings of different blocks of a composition are disjoint. -/ lemma disjoint_range {i₁ iβ‚‚ : fin c.length} (h : i₁ β‰  iβ‚‚) : disjoint (set.range (c.embedding i₁)) (set.range (c.embedding iβ‚‚)) := begin classical, wlog h' : i₁ ≀ iβ‚‚ using i₁ iβ‚‚, by_contradiction d, obtain ⟨x, hx₁, hxβ‚‚βŸ© : βˆƒ x : fin n, (x ∈ set.range (c.embedding i₁) ∧ x ∈ set.range (c.embedding iβ‚‚)) := set.not_disjoint_iff.1 d, have : i₁ < iβ‚‚ := lt_of_le_of_ne h' h, have A : (i₁ : β„•).succ ≀ iβ‚‚ := nat.succ_le_of_lt this, apply lt_irrefl (x : β„•), calc (x : β„•) < c.size_up_to (i₁ : β„•).succ : (c.mem_range_embedding_iff.1 hx₁).2 ... ≀ c.size_up_to (iβ‚‚ : β„•) : monotone_sum_take _ A ... ≀ x : (c.mem_range_embedding_iff.1 hxβ‚‚).1 end lemma mem_range_embedding (j : fin n) : j ∈ set.range (c.embedding (c.index j)) := begin have : c.embedding (c.index j) (c.inv_embedding j) ∈ set.range (c.embedding (c.index j)) := set.mem_range_self _, rwa c.embedding_comp_inv j at this end lemma mem_range_embedding_iff' {j : fin n} {i : fin c.length} : j ∈ set.range (c.embedding i) ↔ i = c.index j := begin split, { rw ← not_imp_not, assume h, exact set.disjoint_right.1 (c.disjoint_range h) (c.mem_range_embedding j) }, { assume h, rw h, exact c.mem_range_embedding j } end lemma index_embedding (i : fin c.length) (j : fin (c.blocks_fun i)) : c.index (c.embedding i j) = i := begin symmetry, rw ← mem_range_embedding_iff', apply set.mem_range_self end lemma inv_embedding_comp (i : fin c.length) (j : fin (c.blocks_fun i)) : (c.inv_embedding (c.embedding i j)).val = j.val := begin simp only [inv_embedding, index_embedding], simp [embedding, fin.coe_eq_val, index_embedding] end /-- Equivalence between the disjoint union of the blocks (each of them seen as `fin (c.blocks_fun i)`) with `fin n`. -/ def blocks_fin_equiv : (Ξ£ i : fin c.length, fin (c.blocks_fun i)) ≃ fin n := { to_fun := Ξ» x, c.embedding x.1 x.2, inv_fun := Ξ» j, ⟨c.index j, c.inv_embedding j⟩, left_inv := Ξ» x, begin rcases x with ⟨i, y⟩, dsimp, congr, { exact c.index_embedding _ _ }, rw fin.heq_ext_iff, { exact c.inv_embedding_comp _ _ }, { rw c.index_embedding } end, right_inv := Ξ» j, c.embedding_comp_inv j } lemma blocks_fun_congr {n₁ nβ‚‚ : β„•} (c₁ : composition n₁) (cβ‚‚ : composition nβ‚‚) (i₁ : fin c₁.length) (iβ‚‚ : fin cβ‚‚.length) (hn : n₁ = nβ‚‚) (hc : c₁.blocks = cβ‚‚.blocks) (hi : (i₁ : β„•) = iβ‚‚) : c₁.blocks_fun i₁ = cβ‚‚.blocks_fun iβ‚‚ := by { cases hn, rw ← composition.ext_iff at hc, cases hc, congr, rwa fin.ext_iff } /-- Two compositions (possibly of different integers) coincide if and only if they have the same sequence of blocks. -/ lemma sigma_eq_iff_blocks_eq {c : Ξ£ n, composition n} {c' : Ξ£ n, composition n} : c = c' ↔ c.2.blocks = c'.2.blocks := begin refine ⟨λ H, by rw H, Ξ» H, _⟩, rcases c with ⟨n, c⟩, rcases c' with ⟨n', c'⟩, have : n = n', by { rw [← c.blocks_sum, ← c'.blocks_sum, H] }, induction this, simp only [true_and, eq_self_iff_true, heq_iff_eq], ext1, exact H end /-- The composition made of blocks all of size `1`. -/ def ones (n : β„•) : composition n := ⟨repeat (1 : β„•) n, Ξ» i hi, by simp [list.eq_of_mem_repeat hi], by simp⟩ instance {n : β„•} : inhabited (composition n) := ⟨composition.ones n⟩ @[simp] lemma ones_length (n : β„•) : (ones n).length = n := list.length_repeat 1 n @[simp] lemma ones_blocks (n : β„•) : (ones n).blocks = repeat (1 : β„•) n := by simp only [blocks, ones, map_repeat] @[simp] lemma ones_blocks_fun (n : β„•) (i : fin (ones n).length) : (ones n).blocks_fun i = 1 := by simp [blocks_fun, ones, blocks, i.2] @[simp] lemma ones_size_up_to (n : β„•) (i : β„•) : (ones n).size_up_to i = min i n := by simp [size_up_to, ones_blocks, take_repeat] @[simp] lemma ones_embedding (i : fin (ones n).length) (h : 0 < (ones n).blocks_fun i) : (ones n).embedding i ⟨0, h⟩ = ⟨i.1, lt_of_lt_of_le i.2 (ones n).length_le⟩ := begin have : i.val < n, by { convert i.2, exact (ones_length n).symm }, simp [embedding, le_of_lt this] end lemma eq_ones_iff {c : composition n} : c = ones n ↔ βˆ€ i ∈ c.blocks, i = 1 := begin split, { assume H, rw [H, ones_blocks], exact Ξ» i, eq_of_mem_repeat }, { assume H, ext1, have A : c.blocks = repeat 1 c.blocks.length := eq_repeat_of_mem H, have : c.blocks.length = n, by { conv_rhs { rw [← c.blocks_sum, A] }, simp }, rw [A, this, ones_blocks] }, end lemma ne_ones_iff {c : composition n} : c β‰  ones n ↔ βˆƒ i ∈ c.blocks, 1 < i := begin rw ← not_iff_not, push_neg, rw eq_ones_iff, have : βˆ€ j ∈ c.blocks, j = 1 ↔ j ≀ 1 := Ξ» j hj, by simp [le_antisymm_iff, c.one_le_blocks hj], simp [this] {contextual := tt} end /-- The composition made of a single block of size `n`. -/ def single (n : β„•) (h : 0 < n) : composition n := ⟨[n], by simp [h], by simp⟩ @[simp] lemma single_length {n : β„•} (h : 0 < n) : (single n h).length = 1 := by simp [single, length] @[simp] lemma single_blocks {n : β„•} (h : 0 < n) : (single n h).blocks = [n] := by simp [blocks, single] @[simp] lemma single_blocks_fun {n : β„•} (h : 0 < n) (i : fin (single n h).length) : (single n h).blocks_fun i = n := by simp [blocks_fun, single, blocks, i.2] @[simp] lemma single_embedding {n : β„•} (h : 0 < n) (i : fin n) : (single n h).embedding ⟨0, single_length h β–Έ zero_lt_one⟩ i = i := by simp [embedding] lemma eq_single_iff {n : β„•} {h : 0 < n} {c : composition n } : c = single n h ↔ c.length = 1 := begin split, { assume H, rw H, exact single_length h }, { assume H, ext1, have A : c.blocks.length = 1 := H β–Έ c.blocks_length, have B : c.blocks.sum = n := c.blocks_sum, rw eq_cons_of_length_one A at B ⊒, simpa [single_blocks] using B } end end composition /-! ### Splitting a list Given a list of length `n` and a composition `c` of `n`, one can split `l` into `c.length` sublists of respective lengths `c.blocks_fun 0`, ..., `c.blocks_fun (c.length-1)`. This is inverse to the join operation. -/ namespace list variable {Ξ± : Type*} /-- Auxiliary for `list.split_wrt_composition`. -/ def split_wrt_composition_aux : list Ξ± β†’ list β„• β†’ list (list Ξ±) | l [] := [] | l (n :: ns) := let (l₁, lβ‚‚) := l.split_at n in l₁ :: split_wrt_composition_aux lβ‚‚ ns /-- Given a list of length `n` and a composition `[i₁, ..., iβ‚–]` of `n`, split `l` into a list of `k` lists corresponding to the blocks of the composition, of respective lengths `i₁`, ..., `iβ‚–`. This makes sense mostly when `n = l.length`, but this is not necessary for the definition. -/ def split_wrt_composition (l : list Ξ±) (c : composition n) : list (list Ξ±) := split_wrt_composition_aux l c.blocks local attribute [simp] split_wrt_composition_aux.equations._eqn_1 local attribute [simp] lemma split_wrt_composition_aux_cons (l : list Ξ±) (n ns) : l.split_wrt_composition_aux (n :: ns) = take n l :: (drop n l).split_wrt_composition_aux ns := by simp [split_wrt_composition_aux] lemma length_split_wrt_composition_aux (l : list Ξ±) (ns) : length (l.split_wrt_composition_aux ns) = ns.length := by induction ns generalizing l; simp * /-- When one splits a list along a composition `c`, the number of sublists thus created is `c.length`. -/ @[simp] lemma length_split_wrt_composition (l : list Ξ±) (c : composition n) : length (l.split_wrt_composition c) = c.length := length_split_wrt_composition_aux _ _ lemma map_length_split_wrt_composition_aux {ns : list β„•} : βˆ€ {l : list Ξ±}, ns.sum ≀ l.length β†’ map length (l.split_wrt_composition_aux ns) = ns := begin induction ns with n ns IH; intros l h; simp at h ⊒, have := le_trans (nat.le_add_right _ _) h, rw IH, {simp [this]}, rwa [length_drop, nat.le_sub_left_iff_add_le this] end /-- When one splits a list along a composition `c`, the lengths of the sublists thus created are given by the block sizes in `c`. -/ lemma map_length_split_wrt_composition (l : list Ξ±) (c : composition l.length) : map length (l.split_wrt_composition c) = c.blocks := map_length_split_wrt_composition_aux (le_of_eq c.blocks_sum) lemma length_pos_of_mem_split_wrt_composition {l l' : list Ξ±} {c : composition l.length} (h : l' ∈ l.split_wrt_composition c) : 0 < length l' := begin have : l'.length ∈ (l.split_wrt_composition c).map list.length := list.mem_map_of_mem list.length h, rw map_length_split_wrt_composition at this, exact c.blocks_pos this end lemma sum_take_map_length_split_wrt_composition (l : list Ξ±) (c : composition l.length) (i : β„•) : (((l.split_wrt_composition c).map length).take i).sum = c.size_up_to i := by { congr, exact map_length_split_wrt_composition l c } lemma nth_le_split_wrt_composition_aux (l : list Ξ±) (ns : list β„•) {i : β„•} (hi) : nth_le (l.split_wrt_composition_aux ns) i hi = (l.take (ns.take (i+1)).sum).drop (ns.take i).sum := begin induction ns with n ns IH generalizing l i, {cases hi}, cases i; simp [IH], rw [add_comm n, drop_add, drop_take], end /-- The `i`-th sublist in the splitting of a list `l` along a composition `c`, is the slice of `l` between the indices `c.size_up_to i` and `c.size_up_to (i+1)`, i.e., the indices in the `i`-th block of the composition. -/ lemma nth_le_split_wrt_composition (l : list Ξ±) (c : composition n) {i : β„•} (hi : i < (l.split_wrt_composition c).length) : nth_le (l.split_wrt_composition c) i hi = (l.take (c.size_up_to (i+1))).drop (c.size_up_to i) := nth_le_split_wrt_composition_aux _ _ _ theorem join_split_wrt_composition_aux {ns : list β„•} : βˆ€ {l : list Ξ±}, ns.sum = l.length β†’ (l.split_wrt_composition_aux ns).join = l := begin induction ns with n ns IH; intros l h; simp at h ⊒, { exact (length_eq_zero.1 h.symm).symm }, rw IH, {simp}, rwa [length_drop, ← h, nat.add_sub_cancel_left] end /-- If one splits a list along a composition, and then joins the sublists, one gets back the original list. -/ @[simp] theorem join_split_wrt_composition (l : list Ξ±) (c : composition l.length) : (l.split_wrt_composition c).join = l := join_split_wrt_composition_aux c.blocks_sum /-- If one joins a list of lists and then splits the join along the right composition, one gets back the original list of lists. -/ @[simp] theorem split_wrt_composition_join (L : list (list Ξ±)) (c : composition L.join.length) (h : map length L = c.blocks) : split_wrt_composition (join L) c = L := by simp only [eq_self_iff_true, and_self, eq_iff_join_eq, join_split_wrt_composition, map_length_split_wrt_composition, h] end list /-! ### Compositions as sets Combinatorial viewpoints on compositions, seen as finite subsets of `fin (n+1)` containing `0` and `n`, where the points of the set (other than `n`) correspond to the leftmost points of each block. -/ /-- Bijection between compositions of `n` and subsets of `{0, ..., n-2}`, defined by considering the restriction of the subset to `{1, ..., n-1}` and shifting to the left by one. -/ def composition_as_set_equiv (n : β„•) : composition_as_set n ≃ finset (fin (n - 1)) := { to_fun := Ξ» c, {i : fin (n-1) | (⟨1 + i.val, by { have := i.2, omega }⟩ : fin n.succ) ∈ c.boundaries}.to_finset, inv_fun := Ξ» s, { boundaries := {i : fin n.succ | (i = 0) ∨ (i = fin.last n) ∨ (βˆƒ (j : fin (n-1)) (hj : j ∈ s), i.val = j.val + 1)}.to_finset, zero_mem := by simp, last_mem := by simp }, left_inv := begin assume c, ext i, simp only [exists_prop, add_comm, set.mem_to_finset, true_or, or_true, set.mem_set_of_eq, fin.last_val], split, { rintro (rfl | rfl | ⟨j, hj1, hj2⟩), { exact c.zero_mem }, { exact c.last_mem }, { convert hj1, rwa fin.ext_iff } }, { simp only [classical.or_iff_not_imp_left], assume i_mem i_ne_zero i_ne_last, simp [fin.ext_iff] at i_ne_zero i_ne_last, refine ⟨⟨i.val - 1, _⟩, _, _⟩, { have : i.val < n + 1 := i.2, omega }, { convert i_mem, rw fin.ext_iff, simp, omega }, { simp, omega } }, end, right_inv := begin assume s, ext i, have : 1 + i.val β‰  n, by { apply ne_of_lt, have := i.2, omega }, simp only [fin.ext_iff, this, exists_prop, fin.val_zero, false_or, add_right_inj, add_comm, set.mem_to_finset, true_or, add_eq_zero_iff, or_true, one_ne_zero, set.mem_set_of_eq, fin.last_val, false_and], split, { rintros ⟨j, js, hj⟩, convert js, exact (fin.ext_iff _ _).2 hj }, { assume h, exact ⟨i, h, rfl⟩ } end } instance composition_as_set_fintype (n : β„•) : fintype (composition_as_set n) := fintype.of_equiv _ (composition_as_set_equiv n).symm lemma composition_as_set_card (n : β„•) : fintype.card (composition_as_set n) = 2 ^ (n - 1) := begin have : fintype.card (finset (fin (n-1))) = 2 ^ (n - 1), by simp, rw ← this, exact fintype.card_congr (composition_as_set_equiv n) end namespace composition_as_set variables (c : composition_as_set n) lemma boundaries_nonempty : c.boundaries.nonempty := ⟨0, c.zero_mem⟩ lemma card_boundaries_pos : 0 < finset.card c.boundaries := finset.card_pos.mpr c.boundaries_nonempty /-- Number of blocks in a `composition_as_set`. -/ def length : β„• := finset.card c.boundaries - 1 lemma card_boundaries_eq_succ_length : c.boundaries.card = c.length + 1 := (nat.sub_eq_iff_eq_add c.card_boundaries_pos).mp rfl lemma length_lt_card_boundaries : c.length < c.boundaries.card := by { rw c.card_boundaries_eq_succ_length, exact lt_add_one _ } lemma lt_length (i : fin c.length) : i.val + 1 < c.boundaries.card := nat.add_lt_of_lt_sub_right i.2 lemma lt_length' (i : fin c.length) : i.val < c.boundaries.card := lt_of_le_of_lt (nat.le_succ i.val) (c.lt_length i) /-- Canonical increasing bijection from `fin c.boundaries.card` to `c.boundaries`. -/ def boundary : fin c.boundaries.card β†’ fin (n+1) := finset.mono_of_fin c.boundaries rfl @[simp] lemma boundary_zero : c.boundary ⟨0, c.card_boundaries_pos⟩ = 0 := begin rw [boundary, finset.mono_of_fin_zero rfl c.boundaries_nonempty c.card_boundaries_pos], exact le_antisymm (finset.min'_le _ _ _ c.zero_mem) (fin.zero_le _), end @[simp] lemma boundary_length : c.boundary ⟨c.length, c.length_lt_card_boundaries⟩ = fin.last n := begin convert finset.mono_of_fin_last rfl c.boundaries_nonempty c.card_boundaries_pos, exact le_antisymm (finset.le_max' _ _ _ c.last_mem) (fin.le_last _) end /-- Size of the `i`-th block in a `composition_as_set`, seen as a function on `fin c.length`. -/ def blocks_fun (i : fin c.length) : β„• := (c.boundary ⟨i.val + 1, c.lt_length i⟩).val - (c.boundary ⟨i.val, c.lt_length' i⟩).val lemma blocks_fun_pos (i : fin c.length) : 0 < c.blocks_fun i := begin have : (⟨i.val, c.lt_length' i⟩ : fin c.boundaries.card) < ⟨i.val + 1, c.lt_length i⟩ := nat.lt_succ_self _, exact nat.lt_sub_left_of_add_lt (finset.mono_of_fin_strict_mono c.boundaries rfl this) end /-- List of the sizes of the blocks in a `composition_as_set`. -/ def blocks (c : composition_as_set n) : list β„• := of_fn c.blocks_fun @[simp] lemma blocks_length : c.blocks.length = c.length := length_of_fn _ lemma blocks_partial_sum {i : β„•} (h : i < c.boundaries.card) : (c.blocks.take i).sum = c.boundary ⟨i, h⟩ := begin induction i with i IH, { simp }, have A : i < c.blocks.length, { rw c.card_boundaries_eq_succ_length at h, simp [blocks, nat.lt_of_succ_lt_succ h] }, have B : i < c.boundaries.card := lt_of_lt_of_le A (by simp [blocks, length, nat.sub_le]), rw [sum_take_succ _ _ A, IH B], simp only [blocks, blocks_fun, fin.coe_eq_val, nth_le_of_fn'], rw nat.add_sub_cancel', refine le_of_lt (@finset.mono_of_fin_strict_mono _ _ c.boundaries _ rfl (⟨i, B⟩ : fin c.boundaries.card) _ _), exact nat.lt_succ_self _ end lemma mem_boundaries_iff_exists_blocks_sum_take_eq {j : fin (n+1)} : j ∈ c.boundaries ↔ βˆƒ i < c.boundaries.card, (c.blocks.take i).sum = j.val := begin split, { assume hj, rcases (c.boundaries.mono_of_fin_bij_on rfl).surj_on hj with ⟨i, _, hi⟩, refine ⟨i.1, i.2, _⟩, rw [← hi, c.blocks_partial_sum i.2], refl }, { rintros ⟨i, hi, H⟩, convert (c.boundaries.mono_of_fin_bij_on rfl).maps_to (set.mem_univ ⟨i, hi⟩), have : c.boundary ⟨i, hi⟩ = j, by rwa [fin.ext_iff, ← fin.coe_eq_val, ← c.blocks_partial_sum hi], exact this.symm } end lemma blocks_sum : c.blocks.sum = n := begin have : c.blocks.take c.length = c.blocks := take_all_of_le (by simp [blocks]), rw [← this, c.blocks_partial_sum c.length_lt_card_boundaries, c.boundary_length], refl end /-- Associating a `composition n` to a `composition_as_set n`, by registering the sizes of the blocks as a list of positive integers. -/ def to_composition : composition n := { blocks := c.blocks, blocks_pos := by simp only [blocks, forall_mem_of_fn_iff, blocks_fun_pos c, forall_true_iff], blocks_sum := c.blocks_sum } end composition_as_set /-! ### Equivalence between compositions and compositions as sets In this section, we explain how to go back and forth between a `composition` and a `composition_as_set`, by showing that their `blocks` and `length` and `boundaries` correspond to each other, and construct an equivalence between them called `composition_equiv`. -/ @[simp] lemma composition.to_composition_as_set_length (c : composition n) : c.to_composition_as_set.length = c.length := by simp [composition.to_composition_as_set, composition_as_set.length, c.card_boundaries_eq_succ_length] @[simp] lemma composition_as_set.to_composition_length (c : composition_as_set n) : c.to_composition.length = c.length := by simp [composition_as_set.to_composition, composition.length, composition.blocks] @[simp] lemma composition.to_composition_as_set_blocks (c : composition n) : c.to_composition_as_set.blocks = c.blocks := begin let d := c.to_composition_as_set, change d.blocks = c.blocks, have length_eq : d.blocks.length = c.blocks.length, { convert c.to_composition_as_set_length, simp [composition_as_set.blocks] }, suffices H : βˆ€ (i ≀ d.blocks.length), (d.blocks.take i).sum = (c.blocks.take i).sum, from eq_of_sum_take_eq length_eq H, assume i hi, have i_lt : i < d.boundaries.card, { convert nat.lt_succ_iff.2 hi, convert d.card_boundaries_eq_succ_length, exact length_of_fn _ }, have i_lt' : i < c.boundaries.card := i_lt, have i_lt'' : i < c.length + 1, by rwa c.card_boundaries_eq_succ_length at i_lt', have A : finset.mono_of_fin d.boundaries rfl ⟨i, i_lt⟩ = finset.mono_of_fin c.boundaries rfl ⟨i, i_lt'⟩ := rfl, have B : c.size_up_to i = c.boundary ⟨i, i_lt''⟩ := rfl, rw [d.blocks_partial_sum i_lt, composition_as_set.boundary, A, ← composition.size_up_to, B, fin.coe_eq_val, fin.coe_eq_val, ← fin.ext_iff, c.mono_of_fin_boundaries, finset.mono_of_fin_eq_mono_of_fin_iff] end @[simp] lemma composition_as_set.to_composition_blocks (c : composition_as_set n) : c.to_composition.blocks = c.blocks := rfl @[simp] lemma composition_as_set.to_composition_boundaries (c : composition_as_set n) : c.to_composition.boundaries = c.boundaries := begin ext j, simp [c.mem_boundaries_iff_exists_blocks_sum_take_eq, c.card_boundaries_eq_succ_length, composition.boundary, fin.ext_iff, composition.size_up_to, exists_prop, finset.mem_univ, take, exists_prop_of_true, finset.mem_image, composition_as_set.to_composition_blocks, composition.boundaries], split, { rintros ⟨i, hi⟩, refine ⟨i.1, _, hi⟩, convert i.2, simp }, { rintros ⟨i, i_lt, hi⟩, have : i < c.to_composition.length + 1, by simpa using i_lt, exact ⟨⟨i, this⟩, hi⟩ } end @[simp] lemma composition.to_composition_as_set_boundaries (c : composition n) : c.to_composition_as_set.boundaries = c.boundaries := rfl /-- Equivalence between `composition n` and `composition_as_set n`. -/ def composition_equiv (n : β„•) : composition n ≃ composition_as_set n := { to_fun := Ξ» c, c.to_composition_as_set, inv_fun := Ξ» c, c.to_composition, left_inv := Ξ» c, by { ext1, exact c.to_composition_as_set_blocks }, right_inv := Ξ» c, by { ext1, exact c.to_composition_boundaries } } instance composition_fintype (n : β„•) : fintype (composition n) := fintype.of_equiv _ (composition_equiv n).symm lemma composition_card (n : β„•) : fintype.card (composition n) = 2 ^ (n - 1) := begin rw ← composition_as_set_card n, exact fintype.card_congr (composition_equiv n) end
0a75202633e2b4cf0cf64ad4a788ab8ecb7415f1
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/ore_localization/basic.lean
d307a9e61ee955cb7183445c4fbe9ac11763ab99
[ "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
33,920
lean
/- Copyright (c) 2022 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Kevin Klinge -/ import group_theory.monoid_localization import ring_theory.non_zero_divisors import ring_theory.ore_localization.ore_set import tactic.noncomm_ring /-! # Localization over right Ore sets. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the localization of a monoid over a right Ore set and proves its universal mapping property. It then extends the definition and its properties first to semirings and then to rings. We show that in the case of a commutative monoid this definition coincides with the common monoid localization. Finally we show that in a ring without zero divisors, taking the Ore localization at `R - {0}` results in a division ring. ## Notations Introduces the notation `R[S⁻¹]` for the Ore localization of a monoid `R` at a right Ore subset `S`. Also defines a new heterogeneos division notation `r /β‚’ s` for a numerator `r : R` and a denominator `s : S`. ## References * <https://ncatlab.org/nlab/show/Ore+localization> * [Zoran Ε koda, *Noncommutative localization in noncommutative geometry*][skoda2006] ## Tags localization, Ore, non-commutative -/ universe u open ore_localization namespace ore_localization variables (R : Type*) [monoid R] (S : submonoid R) [ore_set S] /-- The setoid on `R Γ— S` used for the Ore localization. -/ def ore_eqv : setoid (R Γ— S) := { r := Ξ» rs rs', βˆƒ (u : S) (v : R), rs'.1 * u = rs.1 * v ∧ (rs'.2 : R) * u = rs.2 * v, iseqv := begin refine ⟨_, _, _⟩, { rintro ⟨r,s⟩, use 1, use 1, simp [submonoid.one_mem] }, { rintros ⟨r, s⟩ ⟨r', s'⟩ ⟨u, v, hru, hsu⟩, rcases ore_condition (s : R) s' with ⟨rβ‚‚, sβ‚‚, hβ‚βŸ©, rcases ore_condition rβ‚‚ u with ⟨r₃, s₃, hβ‚‚βŸ©, have : (s : R) * ((v : R) * r₃) = (s : R) * (sβ‚‚ * s₃), { assoc_rw [h₁, hβ‚‚, hsu], symmetry, apply mul_assoc }, rcases ore_left_cancel (v * r₃) (sβ‚‚ * s₃) s this with ⟨w, hw⟩, use sβ‚‚ * s₃ * w, use u * r₃ * w, split; simp only [submonoid.coe_mul], { assoc_rw [hru, ←hw], simp [mul_assoc] }, { assoc_rw [hsu, ←hw], simp [mul_assoc] } }, { rintros ⟨r₁, sβ‚βŸ© ⟨rβ‚‚, sβ‚‚βŸ© ⟨r₃, sβ‚ƒβŸ© ⟨u, v, hur₁, hs₁u⟩ ⟨u', v', hurβ‚‚, hsβ‚‚u⟩, rcases ore_condition v' u with ⟨r', s', h⟩, use u' * s', use v * r', split; simp only [submonoid.coe_mul], { assoc_rw [hurβ‚‚, h, hur₁, mul_assoc] }, { assoc_rw [hsβ‚‚u, h, hs₁u, mul_assoc] } } end } end ore_localization /-- The ore localization of a monoid and a submonoid fulfilling the ore condition. -/ def ore_localization (R : Type*) [monoid R] (S : submonoid R) [ore_set S] := quotient (ore_localization.ore_eqv R S) namespace ore_localization section monoid variables {R : Type*} [monoid R] {S : submonoid R} variables (R S) [ore_set S] notation R `[`:1075 S `⁻¹]`:1075 := ore_localization R S local attribute [instance] ore_eqv variables {R S} /-- The division in the ore localization `R[S⁻¹]`, as a fraction of an element of `R` and `S`. -/ def ore_div (r : R) (s : S) : R[S⁻¹] := quotient.mk (r, s) infixl ` /β‚’ `:70 := ore_div @[elab_as_eliminator] protected lemma ind {Ξ² : R [S ⁻¹] β†’ Prop} (c : βˆ€ (r : R) (s : S), Ξ² (r /β‚’ s)) : βˆ€ q, Ξ² q := by { apply quotient.ind, rintro ⟨r, s⟩, exact c r s } lemma ore_div_eq_iff {r₁ rβ‚‚ : R} {s₁ sβ‚‚ : S} : r₁ /β‚’ s₁ = rβ‚‚ /β‚’ sβ‚‚ ↔ (βˆƒ (u : S) (v : R), rβ‚‚ * u = r₁ * v ∧ (sβ‚‚ : R) * u = s₁ * v) := quotient.eq' /-- A fraction `r /β‚’ s` is equal to its expansion by an arbitrary factor `t` if `s * t ∈ S`. -/ protected lemma expand (r : R) (s : S) (t : R) (hst : (s : R) * t ∈ S) : r /β‚’ s = (r * t) /β‚’ (⟨s * t, hst⟩) := by { apply quotient.sound, refine ⟨s, t * s, _, _⟩; dsimp; rw [mul_assoc]; refl } /-- A fraction is equal to its expansion by an factor from s. -/ protected lemma expand' (r : R) (s s' : S) : r /β‚’ s = (r * s') /β‚’ (s * s') := ore_localization.expand r s s' (by norm_cast; apply set_like.coe_mem) /-- Fractions which differ by a factor of the numerator can be proven equal if those factors expand to equal elements of `R`. -/ protected lemma eq_of_num_factor_eq {r r' r₁ rβ‚‚ : R} {s t : S} (h : r * t = r' * t) : (r₁ * r * rβ‚‚) /β‚’ s = (r₁ * r' * rβ‚‚) /β‚’ s := begin rcases ore_condition rβ‚‚ t with ⟨rβ‚‚',t', hrβ‚‚βŸ©, calc (r₁ * r * rβ‚‚) /β‚’ s = (r₁ * r * rβ‚‚ * t') /β‚’ (s * t') : ore_localization.expand _ _ t' _ ... = ((r₁ * r) * (rβ‚‚ * t')) /β‚’ (s * t') : by simp [←mul_assoc] ... = ((r₁ * r) * (t * rβ‚‚')) /β‚’ (s * t') : by rw hrβ‚‚ ... = (r₁ * (r * t) * rβ‚‚') /β‚’ (s * t') : by simp [←mul_assoc] ... = (r₁ * (r' * t) * rβ‚‚') /β‚’ (s * t') : by rw h ... = (r₁ * r' * (t * rβ‚‚')) /β‚’ (s * t') : by simp [←mul_assoc] ... = (r₁ * r' * (rβ‚‚ * t')) /β‚’ (s * t') : by rw hrβ‚‚ ... = (r₁ * r' * rβ‚‚ * t') /β‚’ (s * t') : by simp [←mul_assoc] ... = (r₁ * r' * rβ‚‚) /β‚’ s : by symmetry; apply ore_localization.expand end /-- A function or predicate over `R` and `S` can be lifted to `R[S⁻¹]` if it is invariant under expansion on the right. -/ def lift_expand {C : Sort*} (P : R β†’ S β†’ C) (hP : βˆ€ (r t : R) (s : S) (ht : ((s : R) * t) ∈ S), P r s = P (r * t) ⟨s * t, ht⟩) : R[S⁻¹] β†’ C := quotient.lift (Ξ» (p : R Γ— S), P p.1 p.2) $ Ξ» p q pq, begin cases p with r₁ s₁, cases q with rβ‚‚ sβ‚‚, rcases pq with ⟨u, v, hrβ‚‚, hsβ‚‚βŸ©, dsimp at *, have s₁vS : (s₁ : R) * v ∈ S, { rw [←hsβ‚‚, ←S.coe_mul], exact set_like.coe_mem (sβ‚‚ * u) }, replace hsβ‚‚ : sβ‚‚ * u = ⟨(s₁ : R) * v, s₁vS⟩, { ext, simp [hsβ‚‚] }, rw [hP r₁ v s₁ s₁vS, hP rβ‚‚ u sβ‚‚ (by { norm_cast, rw hsβ‚‚, assumption }), hrβ‚‚], simpa [← hsβ‚‚] end @[simp] lemma lift_expand_of {C : Sort*} {P : R β†’ S β†’ C} {hP : βˆ€ (r t : R) (s : S) (ht : ((s : R) * t) ∈ S), P r s = P (r * t) ⟨s * t, ht⟩} (r : R) (s : S) : lift_expand P hP (r /β‚’ s) = P r s := rfl /-- A version of `lift_expand` used to simultaneously lift functions with two arguments in ``R[S⁻¹]`.-/ def liftβ‚‚_expand {C : Sort*} (P : R β†’ S β†’ R β†’ S β†’ C) (hP : βˆ€ (r₁ t₁ : R) (s₁ : S) (ht₁ : (s₁ : R) * t₁ ∈ S) (rβ‚‚ tβ‚‚ : R) (sβ‚‚ : S) (htβ‚‚ : (sβ‚‚ : R) * tβ‚‚ ∈ S), P r₁ s₁ rβ‚‚ sβ‚‚ = P (r₁ * t₁) ⟨s₁ * t₁, htβ‚βŸ© (rβ‚‚ * tβ‚‚) ⟨sβ‚‚ * tβ‚‚, htβ‚‚βŸ©) : R[S⁻¹] β†’ R[S⁻¹] β†’ C := lift_expand (Ξ» r₁ s₁, lift_expand (P r₁ s₁) $ Ξ» rβ‚‚ tβ‚‚ sβ‚‚ htβ‚‚, by simp [hP r₁ 1 s₁ (by simp) rβ‚‚ tβ‚‚ sβ‚‚ htβ‚‚]) $ Ξ» r₁ t₁ s₁ ht₁, begin ext x, induction x using ore_localization.ind with rβ‚‚ sβ‚‚, rw [lift_expand_of, lift_expand_of, hP r₁ t₁ s₁ ht₁ rβ‚‚ 1 sβ‚‚ (by simp)], simp, end @[simp] lemma liftβ‚‚_expand_of {C : Sort*} {P : R β†’ S β†’ R β†’ S β†’ C} {hP : βˆ€ (r₁ t₁ : R) (s₁ : S) (ht₁ : (s₁ : R) * t₁ ∈ S) (rβ‚‚ tβ‚‚ : R) (sβ‚‚ : S) (htβ‚‚ : (sβ‚‚ : R) * tβ‚‚ ∈ S), P r₁ s₁ rβ‚‚ sβ‚‚ = P (r₁ * t₁) ⟨s₁ * t₁, htβ‚βŸ© (rβ‚‚ * tβ‚‚) ⟨sβ‚‚ * tβ‚‚, htβ‚‚βŸ©} (r₁ : R) (s₁ : S) (rβ‚‚ : R) (sβ‚‚ : S) : liftβ‚‚_expand P hP (r₁ /β‚’ s₁) (rβ‚‚ /β‚’ sβ‚‚) = P r₁ s₁ rβ‚‚ sβ‚‚ := rfl private def mul' (r₁ : R) (s₁ : S) (rβ‚‚ : R) (sβ‚‚ : S) : R[S⁻¹] := (r₁ * ore_num rβ‚‚ s₁) /β‚’ (sβ‚‚ * ore_denom rβ‚‚ s₁) private lemma mul'_char (r₁ rβ‚‚ : R) (s₁ sβ‚‚ : S) (u : S) (v : R) (huv : rβ‚‚ * (u : R) = s₁ * v) : mul' r₁ s₁ rβ‚‚ sβ‚‚ = (r₁ * v) /β‚’ (sβ‚‚ * u) := begin simp only [mul'], have hβ‚€ := ore_eq rβ‚‚ s₁, set vβ‚€ := ore_num rβ‚‚ s₁, set uβ‚€ := ore_denom rβ‚‚ s₁, rcases ore_condition (uβ‚€ : R) u with ⟨r₃, s₃, hβ‚ƒβŸ©, have := calc (s₁ : R) * (v * r₃) = rβ‚‚ * u * r₃ : by assoc_rw ←huv; symmetry; apply mul_assoc ... = rβ‚‚ * uβ‚€ * s₃ : by assoc_rw ←h₃; refl ... = s₁ * (vβ‚€ * s₃) : by assoc_rw hβ‚€; apply mul_assoc, rcases ore_left_cancel _ _ _ this with ⟨sβ‚„, hsβ‚„βŸ©, symmetry, rw ore_div_eq_iff, use s₃ * sβ‚„, use r₃ * sβ‚„, simp only [submonoid.coe_mul], split, { assoc_rw ←hsβ‚„, simp only [mul_assoc] }, { assoc_rw h₃, simp only [mul_assoc] } end /-- The multiplication on the Ore localization of monoids. -/ protected def mul : R[S⁻¹] β†’ R[S⁻¹] β†’ R[S⁻¹] := liftβ‚‚_expand mul' $ Ξ» rβ‚‚ p sβ‚‚ hp r₁ r s₁ hr, begin have h₁ := ore_eq r₁ sβ‚‚, set r₁' := ore_num r₁ sβ‚‚, set sβ‚‚' := ore_denom r₁ sβ‚‚, rcases ore_condition (↑sβ‚‚ * r₁') ⟨sβ‚‚ * p, hp⟩ with ⟨p', s_star, hβ‚‚βŸ©, dsimp at hβ‚‚, rcases ore_condition r (sβ‚‚' * s_star) with ⟨p_flat, s_flat, hβ‚ƒβŸ©, simp only [S.coe_mul] at h₃, have : r₁ * r * s_flat = sβ‚‚ * p * (p' * p_flat), { rw [←mul_assoc, ←hβ‚‚, ←h₁, mul_assoc, h₃], simp only [mul_assoc] }, rw mul'_char (rβ‚‚ * p) (r₁ * r) βŸ¨β†‘sβ‚‚ * p, hp⟩ βŸ¨β†‘s₁ * r, hr⟩ _ _ this, clear this, have hsssp : ↑s₁ * ↑sβ‚‚' * ↑s_star * p_flat ∈ S, { rw [mul_assoc, mul_assoc, ←mul_assoc ↑sβ‚‚', ←h₃, ←mul_assoc], exact S.mul_mem hr (set_like.coe_mem s_flat) }, have : (βŸ¨β†‘s₁ * r, hr⟩ : S) * s_flat = ⟨s₁ * sβ‚‚' * s_star * p_flat, hsssp⟩, { ext, simp only [set_like.coe_mk, submonoid.coe_mul], rw [mul_assoc, h₃, ←mul_assoc, ←mul_assoc] }, rw this, clear this, rcases ore_left_cancel (p * p') (r₁' * ↑s_star) sβ‚‚ (by simp [←mul_assoc, hβ‚‚]) with ⟨sβ‚‚'', hβ‚‚''⟩, rw [←mul_assoc, mul_assoc rβ‚‚, ore_localization.eq_of_num_factor_eq hβ‚‚''], norm_cast at ⊒ hsssp, rw [←ore_localization.expand _ _ _ hsssp, ←mul_assoc], apply ore_localization.expand end instance : has_mul R[S⁻¹] := ⟨ore_localization.mul⟩ lemma ore_div_mul_ore_div {r₁ rβ‚‚ : R} {s₁ sβ‚‚ : S} : (r₁ /β‚’ s₁) * (rβ‚‚ /β‚’ sβ‚‚) = (r₁ * ore_num rβ‚‚ s₁) /β‚’ (sβ‚‚ * ore_denom rβ‚‚ s₁) := rfl /-- A characterization lemma for the multiplication on the Ore localization, allowing for a choice of Ore numerator and Ore denominator. -/ lemma ore_div_mul_char (r₁ rβ‚‚ : R) (s₁ sβ‚‚ : S) (r' : R) (s' : S) (huv : rβ‚‚ * (s' : R) = s₁ * r') : (r₁ /β‚’ s₁) * (rβ‚‚ /β‚’ sβ‚‚) = (r₁ * r') /β‚’ (sβ‚‚ * s') := mul'_char r₁ rβ‚‚ s₁ sβ‚‚ s' r' huv /-- Another characterization lemma for the multiplication on the Ore localizaion delivering Ore witnesses and conditions bundled in a sigma type. -/ def ore_div_mul_char' (r₁ rβ‚‚ : R) (s₁ sβ‚‚ : S) : Ξ£' r' : R, Ξ£' s' : S, rβ‚‚ * (s' : R) = s₁ * r' ∧ (r₁ /β‚’ s₁) * (rβ‚‚ /β‚’ sβ‚‚) = (r₁ * r') /β‚’ (sβ‚‚ * s') := ⟨ore_num rβ‚‚ s₁, ore_denom rβ‚‚ s₁, ore_eq rβ‚‚ s₁, ore_div_mul_ore_div⟩ instance : has_one R[S⁻¹] := ⟨1 /β‚’ 1⟩ protected lemma one_def : (1 : R[S⁻¹]) = 1 /β‚’ 1 := rfl instance : inhabited R[S⁻¹] := ⟨1⟩ @[simp] protected lemma div_eq_one' {r : R} (hr : r ∈ S) : r /β‚’ ⟨r, hr⟩ = 1 := by { rw [ore_localization.one_def, ore_div_eq_iff], exact ⟨⟨r, hr⟩, 1, by simp, by simp⟩ } @[simp] protected lemma div_eq_one {s : S} : (s : R) /β‚’ s = 1 := by { cases s; apply ore_localization.div_eq_one' } protected lemma one_mul (x : R[S⁻¹]) : 1 * x = x := begin induction x using ore_localization.ind with r s, simp [ore_localization.one_def, ore_div_mul_char (1 : R) r (1 : S) s r 1 (by simp)] end protected lemma mul_one (x : R[S⁻¹]) : x * 1 = x := begin induction x using ore_localization.ind with r s, simp [ore_localization.one_def, ore_div_mul_char r 1 s 1 1 s (by simp)] end protected lemma mul_assoc (x y z : R[S⁻¹]) : x * y * z = x * (y * z) := begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with rβ‚‚ sβ‚‚, induction z using ore_localization.ind with r₃ s₃, rcases ore_div_mul_char' r₁ rβ‚‚ s₁ sβ‚‚ with ⟨ra, sa, ha, ha'⟩, rw ha', clear ha', rcases ore_div_mul_char' rβ‚‚ r₃ sβ‚‚ s₃ with ⟨rb, sb, hb, hb'⟩, rw hb', clear hb', rcases ore_condition rb sa with ⟨rc, sc, hc⟩, rw ore_div_mul_char (r₁ * ra) r₃ (sβ‚‚ * sa) s₃ rc (sb * sc) (by { simp only [submonoid.coe_mul], assoc_rw [hb, hc] }), rw [mul_assoc, ←mul_assoc s₃], symmetry, apply ore_div_mul_char, assoc_rw [hc, ←ha], apply mul_assoc end instance : monoid R[S⁻¹] := { one_mul := ore_localization.one_mul, mul_one := ore_localization.mul_one, mul_assoc := ore_localization.mul_assoc, .. ore_localization.has_mul, .. ore_localization.has_one } protected lemma mul_inv (s s' : S) : ((s : R) /β‚’ s') * (s' /β‚’ s) = 1 := by simp [ore_div_mul_char (s :R) s' s' s 1 1 (by simp)] @[simp] protected lemma mul_one_div {r : R} {s t : S} : (r /β‚’ s) * (1 /β‚’ t) = r /β‚’ (t * s) := by simp [ore_div_mul_char r 1 s t 1 s (by simp)] @[simp] protected lemma mul_cancel {r : R} {s t : S} : (r /β‚’ s) * (s /β‚’ t) = r /β‚’ t := by simp [ore_div_mul_char r s s t 1 1 (by simp)] @[simp] protected lemma mul_cancel' {r₁ rβ‚‚ : R} {s t : S} : (r₁ /β‚’ s) * ((s * rβ‚‚) /β‚’ t) = (r₁ * rβ‚‚) /β‚’ t := by simp [ore_div_mul_char r₁ (s * rβ‚‚) s t rβ‚‚ 1 (by simp)] @[simp] lemma div_one_mul {p r : R} {s : S} : (r /β‚’ 1) * (p /β‚’ s) = (r * p) /β‚’ s := --TODO use coercion r ↦ r /β‚’ 1 by simp [ore_div_mul_char r p 1 s p 1 (by simp)] /-- The fraction `s /β‚’ 1` as a unit in `R[S⁻¹]`, where `s : S`. -/ def numerator_unit (s : S) : units R[S⁻¹] := { val := (s : R) /β‚’ 1, inv := (1 : R) /β‚’ s, val_inv := ore_localization.mul_inv s 1, inv_val := ore_localization.mul_inv 1 s } /-- The multiplicative homomorphism from `R` to `R[S⁻¹]`, mapping `r : R` to the fraction `r /β‚’ 1`. -/ def numerator_hom : R β†’* R[S⁻¹] := { to_fun := Ξ» r, r /β‚’ 1, map_one' := rfl, map_mul' := Ξ» r₁ rβ‚‚, div_one_mul.symm } lemma numerator_hom_apply {r : R} : numerator_hom r = r /β‚’ (1 : S) := rfl lemma numerator_is_unit (s : S) : is_unit ((numerator_hom (s : R)) : R[S⁻¹]) := ⟨numerator_unit s, rfl⟩ section UMP variables {T : Type*} [monoid T] variables (f : R β†’* T) (fS : S β†’* units T) variables (hf : βˆ€ (s : S), f s = fS s) include f fS hf /-- The universal lift from a morphism `R β†’* T`, which maps elements of `S` to units of `T`, to a morphism `R[S⁻¹] β†’* T`. -/ def universal_mul_hom : R[S⁻¹] β†’* T := { to_fun := Ξ» x, x.lift_expand (Ξ» r s, (f r) * ((fS s)⁻¹ : units T)) $ Ξ» r t s ht, begin have : ((fS ⟨s * t, ht⟩) : T) = fS s * f t, { simp only [←hf, set_like.coe_mk, monoid_hom.map_mul] }, conv_rhs { rw [monoid_hom.map_mul, ←mul_one (f r), ←units.coe_one, ←mul_left_inv (fS s)], rw [units.coe_mul, ←mul_assoc, mul_assoc _ ↑(fS s), ←this, mul_assoc] }, simp only [mul_one, units.mul_inv] end, map_one' := by rw [ore_localization.one_def, lift_expand_of]; simp, map_mul' := Ξ» x y, begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with rβ‚‚ sβ‚‚, rcases ore_div_mul_char' r₁ rβ‚‚ s₁ sβ‚‚ with ⟨ra, sa, ha, ha'⟩, rw ha', clear ha', rw [lift_expand_of, lift_expand_of, lift_expand_of], conv_rhs { congr, skip, congr, rw [←mul_one (f rβ‚‚), ←(fS sa).mul_inv, ←mul_assoc, ←hf, ←f.map_mul, ha, f.map_mul] }, rw [mul_assoc, mul_assoc, mul_assoc, ←mul_assoc _ (f s₁), hf s₁, (fS s₁).inv_mul, one_mul, f.map_mul, mul_assoc, fS.map_mul, ←units.coe_mul], refl end } lemma universal_mul_hom_apply {r : R} {s : S} : universal_mul_hom f fS hf (r /β‚’ s) = (f r) * ((fS s)⁻¹ : units T) := rfl lemma universal_mul_hom_commutes {r : R} : universal_mul_hom f fS hf (numerator_hom r) = f r := by simp [numerator_hom_apply, universal_mul_hom_apply] /-- The universal morphism `universal_mul_hom` is unique. -/ lemma universal_mul_hom_unique (Ο† : R[S⁻¹] β†’* T) (huniv : βˆ€ (r : R), Ο† (numerator_hom r) = f r) : Ο† = universal_mul_hom f fS hf := begin ext, induction x using ore_localization.ind with r s, rw [universal_mul_hom_apply, ←huniv r, numerator_hom_apply, ←mul_one (Ο† (r /β‚’ s)), ←units.coe_one, ←mul_right_inv (fS s), units.coe_mul, ←mul_assoc, ←hf, ←huniv, ←φ.map_mul, numerator_hom_apply, ore_localization.mul_cancel], end end UMP end monoid section comm_monoid variables {R : Type*} [comm_monoid R] {S : submonoid R} [ore_set S] lemma ore_div_mul_ore_div_comm {r₁ rβ‚‚ : R} {s₁ sβ‚‚ : S} : (r₁ /β‚’ s₁) * (rβ‚‚ /β‚’ sβ‚‚) = (r₁ * rβ‚‚) /β‚’ (s₁ * sβ‚‚) := by rw [ore_div_mul_char r₁ rβ‚‚ s₁ sβ‚‚ rβ‚‚ s₁ (by simp [mul_comm]), mul_comm sβ‚‚] instance : comm_monoid R[S⁻¹] := { mul_comm := Ξ» x y, begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with rβ‚‚ sβ‚‚, rw [ore_div_mul_ore_div_comm, ore_div_mul_ore_div_comm, mul_comm r₁, mul_comm s₁], end, ..ore_localization.monoid } variables (R S) /-- The morphism `numerator_hom` is a monoid localization map in the case of commutative `R`. -/ protected def localization_map : S.localization_map R[S⁻¹] := { to_fun := numerator_hom, map_one' := rfl, map_mul' := Ξ» r₁ rβ‚‚, by simp, map_units' := numerator_is_unit, surj' := Ξ» z, begin induction z using ore_localization.ind with r s, use (r, s), dsimp, rw [numerator_hom_apply, numerator_hom_apply], simp end, eq_iff_exists' := Ξ» r₁ rβ‚‚, begin dsimp, split, { intro h, rw [numerator_hom_apply, numerator_hom_apply, ore_div_eq_iff] at h, rcases h with ⟨u, v, h₁, hβ‚‚βŸ©, dsimp at hβ‚‚, rw [one_mul, one_mul] at hβ‚‚, subst hβ‚‚, use u, simpa only [mul_comm] using h₁.symm }, { rintro ⟨s, h⟩, rw [numerator_hom_apply, numerator_hom_apply, ore_div_eq_iff], refine ⟨s, s, _, _⟩, { simpa [mul_comm] using h.symm }, { simp [one_mul]} } end } /-- If `R` is commutative, Ore localization and monoid localization are isomorphic. -/ protected noncomputable def equiv_monoid_localization : localization S ≃* R[S⁻¹] := localization.mul_equiv_of_quotient (ore_localization.localization_map R S) end comm_monoid section semiring variables {R : Type*} [semiring R] {S : submonoid R} [ore_set S] private def add'' (r₁ : R) (s₁ : S) (rβ‚‚ : R) (sβ‚‚ : S) : R[S⁻¹] := (r₁ * ore_denom (s₁ : R) sβ‚‚ + rβ‚‚ * ore_num s₁ sβ‚‚) /β‚’ (s₁ * ore_denom s₁ sβ‚‚) private lemma add''_char (r₁ : R) (s₁ : S) (rβ‚‚ : R) (sβ‚‚ : S) (rb : R) (sb : S) (hb : (s₁ : R) * sb = (sβ‚‚ : R) * rb) : add'' r₁ s₁ rβ‚‚ sβ‚‚ = (r₁ * sb + rβ‚‚ * rb) /β‚’ (s₁ * sb) := begin simp only [add''], have ha := ore_eq (s₁ : R) sβ‚‚, set! ra := ore_num (s₁ : R) sβ‚‚ with h, rw ←h at *, clear h, -- r tilde set! sa := ore_denom (s₁ : R) sβ‚‚ with h, rw ←h at *, clear h, -- s tilde rcases ore_condition (sa : R) sb with ⟨rc, sc, hc⟩, -- s*, r* have : (sβ‚‚ : R) * (rb * rc) = sβ‚‚ * (ra * sc), { rw [←mul_assoc, ←hb, mul_assoc, ←hc, ←mul_assoc, ←mul_assoc, ha] }, rcases ore_left_cancel _ _ sβ‚‚ this with ⟨sd, hd⟩, -- s# symmetry, rw ore_div_eq_iff, use sc * sd, use rc * sd, split; simp only [submonoid.coe_mul], { noncomm_ring, assoc_rw [hd, hc], noncomm_ring }, { assoc_rewrite [hc], noncomm_ring } end local attribute [instance] ore_localization.ore_eqv private def add' (rβ‚‚ : R) (sβ‚‚ : S) : R[S⁻¹] β†’ R[S⁻¹] := --plus tilde quotient.lift (Ξ» (r₁s₁ : R Γ— S), add'' r₁s₁.1 r₁s₁.2 rβ‚‚ sβ‚‚) $ begin rintros ⟨r₁', s₁'⟩ ⟨r₁, sβ‚βŸ© ⟨sb, rb, hb, hb'⟩, -- s*, r* rcases ore_condition (s₁' : R) sβ‚‚ with ⟨rc, sc, hc⟩, --s~~, r~~ rcases ore_condition rb sc with ⟨rd, sd, hd⟩, -- s#, r# dsimp at *, rw add''_char _ _ _ _ rc sc hc, have : ↑s₁ * ↑(sb * sd) = ↑sβ‚‚ * (rc * rd), { simp only [submonoid.coe_mul], assoc_rewrite [hb', hd, hc], noncomm_ring }, rw add''_char _ _ _ _ (rc * rd : R) (sb * sd : S) this, simp only [submonoid.coe_mul], assoc_rw [hb, hd], rw [←mul_assoc, ←add_mul, ore_div_eq_iff], use 1, use rd, split, { simp }, { simp only [mul_one, submonoid.coe_one, submonoid.coe_mul] at ⊒ this, assoc_rw [hc, this] }, end private lemma add'_comm (r₁ rβ‚‚ : R) (s₁ sβ‚‚ : S) : add' r₁ s₁ (rβ‚‚ /β‚’ sβ‚‚) = add' rβ‚‚ sβ‚‚ (r₁ /β‚’ s₁) := begin simp only [add', ore_div, add'', quotient.lift_mk, quotient.eq], have hb := ore_eq ↑sβ‚‚ s₁, set rb := ore_num ↑sβ‚‚ s₁ with h, -- r~~ rw ←h, clear h, set sb := ore_denom ↑sβ‚‚ s₁ with h, rw ←h, clear h, -- s~~ have ha := ore_eq ↑s₁ sβ‚‚, set ra := ore_num ↑s₁ sβ‚‚ with h, -- r~ rw ←h, clear h, set sa := ore_denom ↑s₁ sβ‚‚ with h, rw ←h, clear h, -- s~ rcases ore_condition ra sb with ⟨rc, sc, hc⟩, -- r#, s# have : (s₁ : R) * (rb * rc) = s₁ * (sa * sc), { rw [←mul_assoc, ←hb, mul_assoc, ←hc, ←mul_assoc, ←ha, mul_assoc] }, rcases ore_left_cancel _ _ s₁ this with ⟨sd, hd⟩, -- s+ use sc * sd, use rc * sd, dsimp, split, { rw [add_mul, add_mul, add_comm], assoc_rw [←hd, hc], noncomm_ring }, { rw [mul_assoc, ←mul_assoc ↑sa, ←hd, hb], noncomm_ring } end /-- The addition on the Ore localization. -/ private def add : R[S⁻¹] β†’ R[S⁻¹] β†’ R[S⁻¹] := Ξ» x, quotient.lift (Ξ» rs : R Γ— S, add' rs.1 rs.2 x) begin rintros ⟨r₁, sβ‚βŸ© ⟨rβ‚‚, sβ‚‚βŸ© hyz, induction x using ore_localization.ind with r₃ s₃, dsimp, rw [add'_comm, add'_comm rβ‚‚], simp [(/β‚’), quotient.sound hyz], end instance : has_add R[S⁻¹] := ⟨add⟩ lemma ore_div_add_ore_div {r r' : R} {s s' : S} : r /β‚’ s + r' /β‚’ s' = (r * ore_denom (s : R) s' + r' * ore_num s s') /β‚’ (s * ore_denom s s') := rfl /-- A characterization of the addition on the Ore localizaion, allowing for arbitrary Ore numerator and Ore denominator. -/ lemma ore_div_add_char {r r' : R} (s s' : S) (rb : R) (sb : S) (h : (s : R) * sb = s' * rb) : r /β‚’ s + r' /β‚’ s' = (r * sb + r' * rb) /β‚’ (s * sb) := add''_char r s r' s' rb sb h /-- Another characterization of the addition on the Ore localization, bundling up all witnesses and conditions into a sigma type. -/ def ore_div_add_char' (r r' : R) (s s' : S) : Ξ£' r'' : R, Ξ£' s'' : S, (s : R) * s'' = s' * r'' ∧ r /β‚’ s + r' /β‚’s' = (r * s'' + r' * r'') /β‚’ (s * s'') := ⟨ore_num s s', ore_denom s s', ore_eq s s', ore_div_add_ore_div⟩ @[simp] lemma add_ore_div {r r' : R} {s : S} : (r /β‚’ s) + (r' /β‚’ s) = (r + r') /β‚’ s := by simp [ore_div_add_char s s 1 1 (by simp)] protected lemma add_assoc (x y z : R[S⁻¹]) : (x + y) + z = x + (y + z) := begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with rβ‚‚ sβ‚‚, induction z using ore_localization.ind with r₃ s₃, rcases ore_div_add_char' r₁ rβ‚‚ s₁ sβ‚‚ with ⟨ra, sa, ha, ha'⟩, rw ha', clear ha', rcases ore_div_add_char' rβ‚‚ r₃ sβ‚‚ s₃ with ⟨rb, sb, hb, hb'⟩, rw hb', clear hb', rcases ore_div_add_char' (r₁ * sa + rβ‚‚ * ra) r₃ (s₁ * sa) s₃ with ⟨rc, sc, hc, q⟩, rw q, clear q, rcases ore_div_add_char' r₁ (rβ‚‚ * sb + r₃ * rb) s₁ (sβ‚‚ * sb) with ⟨rd, sd, hd, q⟩, rw q, clear q, noncomm_ring, rw add_comm (rβ‚‚ * _), repeat { rw ←add_ore_div }, congr' 1, { rcases ore_condition (sd : R) (sa * sc) with ⟨re, se, he⟩, { simp_rw ←submonoid.coe_mul at hb hc hd, assoc_rw [subtype.coe_eq_of_eq_mk hc], rw [←ore_localization.expand, subtype.coe_eq_of_eq_mk hd, ←mul_assoc, ←ore_localization.expand, subtype.coe_eq_of_eq_mk hb], apply ore_localization.expand } }, congr' 1, { rw [←ore_localization.expand', ←mul_assoc, ←mul_assoc, ←ore_localization.expand', ←ore_localization.expand'] }, { simp_rw [←submonoid.coe_mul] at ha hd, rw [subtype.coe_eq_of_eq_mk hd, ←mul_assoc, ←mul_assoc, ←mul_assoc, ←ore_localization.expand, ←ore_localization.expand', subtype.coe_eq_of_eq_mk ha, ←ore_localization.expand], apply ore_localization.expand' } end private def zero : R[S⁻¹] := 0 /β‚’ 1 instance : has_zero R[S⁻¹] := ⟨zero⟩ protected lemma zero_def : (0 : R[S⁻¹]) = 0 /β‚’ 1 := rfl @[simp] lemma zero_div_eq_zero (s : S) : 0 /β‚’ s = 0 := by { rw [ore_localization.zero_def, ore_div_eq_iff], exact ⟨s, 1, by simp⟩ } protected lemma zero_add (x : R[S⁻¹]) : 0 + x = x := begin induction x using ore_localization.ind, rw [←zero_div_eq_zero, add_ore_div], simp end protected lemma add_comm (x y : R[S⁻¹]) : x + y = y + x := begin induction x using ore_localization.ind, induction y using ore_localization.ind, change add' _ _ (_ /β‚’ _) = _, apply add'_comm end instance : add_comm_monoid R[S⁻¹] := { add_comm := ore_localization.add_comm, add_assoc := ore_localization.add_assoc, zero := zero, zero_add := ore_localization.zero_add, add_zero := Ξ» x, by rw [ore_localization.add_comm, ore_localization.zero_add], .. ore_localization.has_add } protected lemma zero_mul (x : R[S⁻¹]) : 0 * x = 0 := begin induction x using ore_localization.ind with r s, rw [ore_localization.zero_def, ore_div_mul_char 0 r 1 s r 1 (by simp)], simp end protected lemma mul_zero (x : R[S⁻¹]) : x * 0 = 0 := begin induction x using ore_localization.ind with r s, rw [ore_localization.zero_def, ore_div_mul_char r 0 s 1 0 1 (by simp)], simp end protected lemma left_distrib (x y z : R[S⁻¹]) : x * (y + z) = x * y + x * z := begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with rβ‚‚ sβ‚‚, induction z using ore_localization.ind with r₃ s₃, rcases ore_div_add_char' rβ‚‚ r₃ sβ‚‚ s₃ with ⟨ra, sa, ha, q⟩, rw q, clear q, rw ore_localization.expand' rβ‚‚ sβ‚‚ sa, rcases ore_div_mul_char' r₁ (rβ‚‚ * sa) s₁ (sβ‚‚ * sa) with ⟨rb, sb, hb, q⟩, rw q, clear q, have hs₃rasb : ↑s₃ * (ra * sb) ∈ S, { rw [←mul_assoc, ←ha], norm_cast, apply set_like.coe_mem }, rw ore_localization.expand _ _ _ hs₃rasb, have ha' : (↑(sβ‚‚ * sa * sb)) = ↑s₃ * (ra * sb), { simp [ha, ←mul_assoc] }, rw ←subtype.coe_eq_of_eq_mk ha', rcases ore_div_mul_char' r₁ (r₃ * (ra * sb)) s₁ (sβ‚‚ * sa * sb) with ⟨rc, sc, hc, hc'⟩, rw hc', rw ore_div_add_char (sβ‚‚ * sa * sb) (sβ‚‚ * sa * sb * sc) 1 sc (by simp), rw ore_localization.expand' (rβ‚‚ * ↑sa + r₃ * ra) (sβ‚‚ * sa) (sb * sc), conv_lhs { congr, skip, congr, rw [add_mul, S.coe_mul, ←mul_assoc, hb, ←mul_assoc, mul_assoc r₃, hc, mul_assoc, ←mul_add] }, rw ore_localization.mul_cancel', simp only [mul_one, submonoid.coe_mul, mul_add, ←mul_assoc], end lemma right_distrib (x y z : R[S⁻¹]) : (x + y) * z = x * z + y * z := begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with rβ‚‚ sβ‚‚, induction z using ore_localization.ind with r₃ s₃, rcases ore_div_add_char' r₁ rβ‚‚ s₁ sβ‚‚ with ⟨ra, sa, ha, ha'⟩, rw ha', clear ha', norm_cast at ha, rw ore_localization.expand' r₁ s₁ sa, rw ore_localization.expand rβ‚‚ sβ‚‚ ra (by rw ←ha; apply set_like.coe_mem), rw ←subtype.coe_eq_of_eq_mk ha, repeat { rw ore_div_mul_ore_div }, simp only [add_mul, add_ore_div] end instance : semiring R[S⁻¹] := { zero_mul := ore_localization.zero_mul, mul_zero := ore_localization.mul_zero, left_distrib := ore_localization.left_distrib, right_distrib := right_distrib, .. ore_localization.add_comm_monoid, .. ore_localization.monoid } section UMP variables {T : Type*} [semiring T] variables (f : R β†’+* T) (fS : S β†’* units T) variables (hf : βˆ€ (s : S), f s = fS s) include f fS hf /-- The universal lift from a ring homomorphism `f : R β†’+* T`, which maps elements in `S` to units of `T`, to a ring homomorphism `R[S⁻¹] β†’+* T`. This extends the construction on monoids. -/ def universal_hom : R[S⁻¹] β†’+* T := { map_zero' := begin rw [monoid_hom.to_fun_eq_coe, ore_localization.zero_def, universal_mul_hom_apply], simp end, map_add' := Ξ» x y, begin induction x using ore_localization.ind with r₁ s₁, induction y using ore_localization.ind with rβ‚‚ sβ‚‚, rcases ore_div_add_char' r₁ rβ‚‚ s₁ sβ‚‚ with ⟨r₃, s₃, h₃, h₃'⟩, rw h₃', clear h₃', simp only [universal_mul_hom_apply, ring_hom.coe_monoid_hom, ring_hom.to_monoid_hom_eq_coe, monoid_hom.to_fun_eq_coe], simp only [mul_inv_rev, monoid_hom.map_mul, ring_hom.map_add, ring_hom.map_mul, units.coe_mul], rw [add_mul, ←mul_assoc, mul_assoc (f r₁), hf, ←units.coe_mul], simp only [mul_one, mul_right_inv, units.coe_one], congr' 1, rw [mul_assoc], congr' 1, norm_cast at h₃, have h₃' := subtype.coe_eq_of_eq_mk h₃, rw [←units.coe_mul, ←mul_inv_rev, ←fS.map_mul, h₃'], have hsβ‚‚r₃ : ↑sβ‚‚ * r₃ ∈ S, { rw ←h₃, exact set_like.coe_mem (s₁ * s₃)}, apply (units.inv_mul_cancel_left (fS sβ‚‚) _).symm.trans, conv_lhs { congr, skip, rw [←units.mul_inv_cancel_left (fS ⟨sβ‚‚ * r₃, hsβ‚‚rβ‚ƒβŸ©) (fS sβ‚‚), mul_assoc, mul_assoc], congr, skip, rw [←hf, ←mul_assoc (f sβ‚‚), ←f.map_mul], conv { congr, skip, congr, rw [←h₃] }, rw [hf, ←mul_assoc, ←h₃', units.inv_mul] }, rw [one_mul, ←h₃', units.mul_inv, mul_one], end, .. universal_mul_hom f.to_monoid_hom fS hf } lemma universal_hom_apply {r : R} {s : S} : universal_hom f fS hf (r /β‚’ s) = (f r) * ((fS s)⁻¹ : units T) := rfl lemma universal_hom_commutes {r : R} : universal_hom f fS hf (numerator_hom r) = f r := by simp [numerator_hom_apply, universal_hom_apply] lemma universal_hom_unique (Ο† : R[S⁻¹] β†’+* T) (huniv : βˆ€ (r : R), Ο† (numerator_hom r) = f r) : Ο† = universal_hom f fS hf := ring_hom.coe_monoid_hom_injective $ universal_mul_hom_unique (ring_hom.to_monoid_hom f) fS hf ↑φ huniv end UMP end semiring section ring variables {R : Type*} [ring R] {S : submonoid R} [ore_set S] /-- Negation on the Ore localization is defined via negation on the numerator. -/ protected def neg : R[S⁻¹] β†’ R[S⁻¹] := lift_expand (Ξ» (r : R) (s : S), (- r) /β‚’ s) $ Ξ» r t s ht, by rw [neg_mul_eq_neg_mul, ←ore_localization.expand] instance : has_neg R[S⁻¹] := ⟨ore_localization.neg⟩ @[simp] protected lemma neg_def (r : R) (s : S) : - (r /β‚’ s) = (- r) /β‚’ s := rfl protected lemma add_left_neg (x : R[S⁻¹]) : (- x) + x = 0 := by induction x using ore_localization.ind with r s; simp instance : ring R[S⁻¹] := { add_left_neg := ore_localization.add_left_neg, .. ore_localization.semiring, .. ore_localization.has_neg } open_locale non_zero_divisors lemma numerator_hom_inj (hS : S ≀ R⁰) : function.injective (numerator_hom : R β†’ R[S⁻¹]) := Ξ» r₁ rβ‚‚ h, begin rw [numerator_hom_apply, numerator_hom_apply, ore_div_eq_iff] at h, rcases h with ⟨u, v, h₁, hβ‚‚βŸ©, simp only [S.coe_one, one_mul] at hβ‚‚, rwa [←hβ‚‚, mul_cancel_right_mem_non_zero_divisor (hS (set_like.coe_mem u)), eq_comm] at h₁, end lemma nontrivial_of_non_zero_divisors [nontrivial R] (hS : S ≀ R⁰) : nontrivial R[S⁻¹] := ⟨⟨0, 1, Ξ» h, begin rw [ore_localization.one_def, ore_localization.zero_def] at h, apply non_zero_divisors.coe_ne_zero 1 (numerator_hom_inj hS h).symm end⟩⟩ end ring section division_ring open_locale non_zero_divisors open_locale classical variables {R : Type*} [ring R] [nontrivial R] [ore_set R⁰] instance : nontrivial R[R⁰⁻¹] := nontrivial_of_non_zero_divisors (refl R⁰) variables [no_zero_divisors R] noncomputable theory /-- The inversion of Ore fractions for a ring without zero divisors, satisying `0⁻¹ = 0` and `(r /β‚’ r')⁻¹ = r' /β‚’ r` for `r β‰  0`. -/ protected def inv : R[R⁰⁻¹] β†’ R[R⁰⁻¹] := lift_expand (Ξ» r s, if hr: r = (0 : R) then (0 : R[R⁰⁻¹]) else (s /β‚’ ⟨r, Ξ» _, eq_zero_of_ne_zero_of_mul_right_eq_zero hr⟩)) begin intros r t s hst, by_cases hr : r = 0, { simp [hr] }, { by_cases ht : t = 0, { exfalso, apply non_zero_divisors.coe_ne_zero ⟨_, hst⟩, simp [ht, mul_zero] }, { simp only [hr, ht, set_like.coe_mk, dif_neg, not_false_iff, or_self, mul_eq_zero], apply ore_localization.expand } } end instance : has_inv R[R⁰⁻¹] := ⟨ore_localization.inv⟩ protected lemma inv_def {r : R} {s : R⁰} : (r /β‚’ s)⁻¹ = if hr: r = (0 : R) then (0 : R[R⁰⁻¹]) else (s /β‚’ ⟨r, Ξ» _, eq_zero_of_ne_zero_of_mul_right_eq_zero hr⟩) := rfl protected lemma mul_inv_cancel (x : R[R⁰⁻¹]) (h : x β‰  0) : x * x⁻¹ = 1 := begin induction x using ore_localization.ind with r s, rw [ore_localization.inv_def, ore_localization.one_def], by_cases hr : r = 0, { exfalso, apply h, simp [hr] }, { simp [hr], apply ore_localization.div_eq_one' } end protected lemma inv_zero : (0 : R[R⁰⁻¹])⁻¹ = 0 := by { rw [ore_localization.zero_def, ore_localization.inv_def], simp } instance : division_ring R[(R⁰)⁻¹] := { mul_inv_cancel := ore_localization.mul_inv_cancel, inv_zero := ore_localization.inv_zero, .. ore_localization.nontrivial, .. ore_localization.has_inv, .. ore_localization.ring } end division_ring end ore_localization
0894addc580789b3a4a57f42ae0ac84abf491fb3
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Meta/MatchUtil.lean
1f1ff7dea396a348bf5fdb42add387c4be43c16c
[ "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
1,440
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.Recognizers import Lean.Meta.Basic namespace Lean.Meta @[inline] def testHelper (e : Expr) (p : Expr β†’ MetaM Bool) : MetaM Bool := do if (← p e) then return true else p (← whnf e) @[inline] def matchHelper? (e : Expr) (p? : Expr β†’ MetaM (Option Ξ±)) : MetaM (Option Ξ±) := do match (← p? e) with | none => p? (← whnf e) | s => return s def matchEq? (e : Expr) : MetaM (Option (Expr Γ— Expr Γ— Expr)) := matchHelper? e fun e => return Expr.eq? e def matchFalse (e : Expr) : MetaM Bool := do testHelper e fun e => return e.isConstOf ``False def matchNot? (e : Expr) : MetaM (Option Expr) := matchHelper? e fun e => do if let some e := e.not? then return e else if let some (a, b) := e.arrow? then if (← matchFalse b) then return some a else return none else return none def matchNe? (e : Expr) : MetaM (Option (Expr Γ— Expr Γ— Expr)) := matchHelper? e fun e => do if let some r := e.ne? then return r else if let some e ← matchNot? e then matchEq? e else return none def matchConstructorApp? (e : Expr) : MetaM (Option ConstructorVal) := do let env ← getEnv matchHelper? e fun e => return e.isConstructorApp? env end Lean.Meta
22a075bae73b1cef312f2118512ba347c0313baa
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/simplifier15.lean
e8c8fbd47eefa6b14f08a149e5525ea538b3348b
[ "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
542
lean
-- normalizing reducible non-subsingleton instances import algebra.ring open algebra universe l constants (T : Type.{l}) (s : comm_ring T) constants (x1 x2 x3 x4 : T) (f g : T β†’ T) attribute s [instance] set_option pp.all true #simplify eq null 0 x1 #simplify eq null 0 x1 + x1 #simplify eq null 0 x1 + x1 + x1 #simplify eq null 0 x1 + x1 + x1 + x1 #simplify eq null 0 x1 + x1 + (x1 + x1) + x1 #simplify eq simplifier.ac 0 x1 + x1 + x1 #simplify eq simplifier.ac 0 x1 + x1 + x1 + x1 #simplify eq simplifier.ac 0 x1 + x1 + (x1 + x1) + x1
32e43c22d6045311c04b850e88a72b1d11cc5706
46125763b4dbf50619e8846a1371029346f4c3db
/src/topology/continuous_on.lean
6511266b5014851950da28f23f4af02f31a4f721
[ "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
23,920
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: SΓ©bastien GouΓ«zel -/ import topology.constructions /-! # Neighborhoods and continuity relative to a subset This file defines relative versions `nhds_within` of `nhds` `continuous_on` of `continuous` `continuous_within_at` of `continuous_at` and proves their basic properties, including the relationships between these restricted notions and the corresponding notions for the subtype equipped with the subspace topology. -/ open set filter open_locale topological_space variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} variables [topological_space Ξ±] /-- The "neighborhood within" filter. Elements of `nhds_within a s` are sets containing the intersection of `s` and a neighborhood of `a`. -/ def nhds_within (a : Ξ±) (s : set Ξ±) : filter Ξ± := 𝓝 a βŠ“ principal s theorem nhds_within_eq (a : Ξ±) (s : set Ξ±) : nhds_within a s = β¨… t ∈ {t : set Ξ± | a ∈ t ∧ is_open t}, principal (t ∩ s) := have set.univ ∈ {s : set Ξ± | a ∈ s ∧ is_open s}, from ⟨set.mem_univ _, is_open_univ⟩, begin rw [nhds_within, nhds, lattice.binfi_inf]; try { exact this }, simp only [inf_principal] end theorem nhds_within_univ (a : Ξ±) : nhds_within a set.univ = 𝓝 a := by rw [nhds_within, principal_univ, lattice.inf_top_eq] lemma nhds_within_has_basis {p : Ξ² β†’ Prop} {s : Ξ² β†’ set Ξ±} {a : Ξ±} (h : (𝓝 a).has_basis p s) (t : set Ξ±) : (nhds_within a t).has_basis p (Ξ» i, s i ∩ t) := h.inf_principal t lemma nhds_within_basis_open (a : Ξ±) (t : set Ξ±) : (nhds_within a t).has_basis (Ξ» u, a ∈ u ∧ is_open u) (Ξ» u, u ∩ t) := nhds_within_has_basis (nhds_basis_opens a) t theorem mem_nhds_within {t : set Ξ±} {a : Ξ±} {s : set Ξ±} : t ∈ nhds_within a s ↔ βˆƒ u, is_open u ∧ a ∈ u ∧ u ∩ s βŠ† t := by simpa only [exists_prop, and_assoc, and_comm] using nhds_within_basis_open a s t lemma mem_nhds_within_iff_exists_mem_nhds_inter {t : set Ξ±} {a : Ξ±} {s : set Ξ±} : t ∈ nhds_within a s ↔ βˆƒ u ∈ 𝓝 a, u ∩ s βŠ† t := nhds_within_has_basis (𝓝 a).basis_sets s t lemma mem_nhds_within_of_mem_nhds {s t : set Ξ±} {a : Ξ±} (h : s ∈ 𝓝 a) : s ∈ nhds_within a t := mem_inf_sets_of_left h theorem self_mem_nhds_within {a : Ξ±} {s : set Ξ±} : s ∈ nhds_within a s := mem_inf_sets_of_right (mem_principal_self s) theorem inter_mem_nhds_within (s : set Ξ±) {t : set Ξ±} {a : Ξ±} (h : t ∈ 𝓝 a) : s ∩ t ∈ nhds_within a s := inter_mem_sets (mem_inf_sets_of_right (mem_principal_self s)) (mem_inf_sets_of_left h) theorem nhds_within_mono (a : Ξ±) {s t : set Ξ±} (h : s βŠ† t) : nhds_within a s ≀ nhds_within a t := lattice.inf_le_inf (le_refl _) (principal_mono.mpr h) lemma mem_of_mem_nhds_within {a : Ξ±} {s t : set Ξ±} (ha : a ∈ s) (ht : t ∈ nhds_within a s) : a ∈ t := let ⟨u, hu, H⟩ := mem_nhds_within.1 ht in H.2 ⟨H.1, ha⟩ theorem nhds_within_restrict'' {a : Ξ±} (s : set Ξ±) {t : set Ξ±} (h : t ∈ nhds_within a s) : nhds_within a s = nhds_within a (s ∩ t) := le_antisymm (lattice.le_inf lattice.inf_le_left (le_principal_iff.mpr (inter_mem_sets self_mem_nhds_within h))) (lattice.inf_le_inf (le_refl _) (principal_mono.mpr (set.inter_subset_left _ _))) theorem nhds_within_restrict' {a : Ξ±} (s : set Ξ±) {t : set Ξ±} (h : t ∈ 𝓝 a) : nhds_within a s = nhds_within a (s ∩ t) := nhds_within_restrict'' s $ mem_inf_sets_of_left h theorem nhds_within_restrict {a : Ξ±} (s : set Ξ±) {t : set Ξ±} (hβ‚€ : a ∈ t) (h₁ : is_open t) : nhds_within a s = nhds_within a (s ∩ t) := nhds_within_restrict' s (mem_nhds_sets h₁ hβ‚€) theorem nhds_within_le_of_mem {a : Ξ±} {s t : set Ξ±} (h : s ∈ nhds_within a t) : nhds_within a t ≀ nhds_within a s := begin rcases mem_nhds_within.1 h with ⟨u, u_open, au, uts⟩, have : nhds_within a t = nhds_within a (t ∩ u) := nhds_within_restrict _ au u_open, rw [this, inter_comm], exact nhds_within_mono _ uts end theorem nhds_within_eq_nhds_within {a : Ξ±} {s t u : set Ξ±} (hβ‚€ : a ∈ s) (h₁ : is_open s) (hβ‚‚ : t ∩ s = u ∩ s) : nhds_within a t = nhds_within a u := by rw [nhds_within_restrict t hβ‚€ h₁, nhds_within_restrict u hβ‚€ h₁, hβ‚‚] theorem nhds_within_eq_of_open {a : Ξ±} {s : set Ξ±} (hβ‚€ : a ∈ s) (h₁ : is_open s) : nhds_within a s = 𝓝 a := by rw [←nhds_within_univ]; apply nhds_within_eq_nhds_within hβ‚€ h₁; rw [set.univ_inter, set.inter_self] @[simp] theorem nhds_within_empty (a : Ξ±) : nhds_within a {} = βŠ₯ := by rw [nhds_within, principal_empty, lattice.inf_bot_eq] theorem nhds_within_union (a : Ξ±) (s t : set Ξ±) : nhds_within a (s βˆͺ t) = nhds_within a s βŠ” nhds_within a t := by unfold nhds_within; rw [←lattice.inf_sup_left, sup_principal] theorem nhds_within_inter (a : Ξ±) (s t : set Ξ±) : nhds_within a (s ∩ t) = nhds_within a s βŠ“ nhds_within a t := by unfold nhds_within; rw [lattice.inf_left_comm, lattice.inf_assoc, inf_principal, ←lattice.inf_assoc, lattice.inf_idem] theorem nhds_within_inter' (a : Ξ±) (s t : set Ξ±) : nhds_within a (s ∩ t) = (nhds_within a s) βŠ“ principal t := by { unfold nhds_within, rw [←inf_principal, lattice.inf_assoc] } lemma nhds_within_prod_eq {Ξ± : Type*} [topological_space Ξ±] {Ξ² : Type*} [topological_space Ξ²] (a : Ξ±) (b : Ξ²) (s : set Ξ±) (t : set Ξ²) : nhds_within (a, b) (s.prod t) = (nhds_within a s).prod (nhds_within b t) := by { unfold nhds_within, rw [nhds_prod_eq, ←filter.prod_inf_prod, filter.prod_principal_principal] } theorem tendsto_if_nhds_within {f g : Ξ± β†’ Ξ²} {p : Ξ± β†’ Prop} [decidable_pred p] {a : Ξ±} {s : set Ξ±} {l : filter Ξ²} (hβ‚€ : tendsto f (nhds_within a (s ∩ p)) l) (h₁ : tendsto g (nhds_within a (s ∩ {x | Β¬ p x})) l) : tendsto (Ξ» x, if p x then f x else g x) (nhds_within a s) l := by apply tendsto_if; rw [←nhds_within_inter']; assumption lemma map_nhds_within (f : Ξ± β†’ Ξ²) (a : Ξ±) (s : set Ξ±) : map f (nhds_within a s) = β¨… t ∈ {t : set Ξ± | a ∈ t ∧ is_open t}, principal (set.image f (t ∩ s)) := ((nhds_within_basis_open a s).map f).eq_binfi theorem tendsto_nhds_within_mono_left {f : Ξ± β†’ Ξ²} {a : Ξ±} {s t : set Ξ±} {l : filter Ξ²} (hst : s βŠ† t) (h : tendsto f (nhds_within a t) l) : tendsto f (nhds_within a s) l := tendsto_le_left (nhds_within_mono a hst) h theorem tendsto_nhds_within_mono_right {f : Ξ² β†’ Ξ±} {l : filter Ξ²} {a : Ξ±} {s t : set Ξ±} (hst : s βŠ† t) (h : tendsto f l (nhds_within a s)) : tendsto f l (nhds_within a t) := tendsto_le_right (nhds_within_mono a hst) h theorem tendsto_nhds_within_of_tendsto_nhds {f : Ξ± β†’ Ξ²} {a : Ξ±} {s : set Ξ±} {l : filter Ξ²} (h : tendsto f (𝓝 a) l) : tendsto f (nhds_within a s) l := by rw [←nhds_within_univ] at h; exact tendsto_nhds_within_mono_left (set.subset_univ _) h theorem principal_subtype {Ξ± : Type*} (s : set Ξ±) (t : set {x // x ∈ s}) : principal t = comap subtype.val (principal (subtype.val '' t)) := by rw comap_principal; rw set.preimage_image_eq; apply subtype.val_injective lemma mem_closure_iff_nhds_within_ne_bot {s : set Ξ±} {x : Ξ±} : x ∈ closure s ↔ nhds_within x s β‰  βŠ₯ := mem_closure_iff_nhds.trans (nhds_within_has_basis (𝓝 x).basis_sets s).forall_nonempty_iff_ne_bot lemma nhds_within_ne_bot_of_mem {s : set Ξ±} {x : Ξ±} (hx : x ∈ s) : nhds_within x s β‰  βŠ₯ := mem_closure_iff_nhds_within_ne_bot.1 $ subset_closure hx lemma is_closed.mem_of_nhds_within_ne_bot {s : set Ξ±} (hs : is_closed s) {x : Ξ±} (hx : nhds_within x s β‰  βŠ₯) : x ∈ s := by simpa only [closure_eq_of_is_closed hs] using mem_closure_iff_nhds_within_ne_bot.2 hx /- nhds_within and subtypes -/ theorem mem_nhds_within_subtype (s : set Ξ±) (a : {x // x ∈ s}) (t u : set {x // x ∈ s}) : t ∈ nhds_within a u ↔ t ∈ comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' u)) := by rw [nhds_within, nhds_subtype, principal_subtype, ←comap_inf, ←nhds_within] theorem nhds_within_subtype (s : set Ξ±) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : nhds_within a t = comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' t)) := filter_eq $ by ext u; rw mem_nhds_within_subtype theorem nhds_within_eq_map_subtype_val {s : set Ξ±} {a : Ξ±} (h : a ∈ s) : nhds_within a s = map subtype.val (𝓝 ⟨a, h⟩) := have hβ‚€ : s ∈ nhds_within a s, by { rw [mem_nhds_within], existsi set.univ, simp [set.diff_eq] }, have h₁ : βˆ€ y ∈ s, βˆƒ x, @subtype.val _ s x = y, from Ξ» y h, ⟨⟨y, h⟩, rfl⟩, begin rw [←nhds_within_univ, nhds_within_subtype, subtype.val_image_univ], exact (map_comap_of_surjective' hβ‚€ h₁).symm, end theorem tendsto_nhds_within_iff_subtype {s : set Ξ±} {a : Ξ±} (h : a ∈ s) (f : Ξ± β†’ Ξ²) (l : filter Ξ²) : tendsto f (nhds_within a s) l ↔ tendsto (function.restrict f s) (𝓝 ⟨a, h⟩) l := by rw [tendsto, tendsto, function.restrict, nhds_within_eq_map_subtype_val h, ←(@filter.map_map _ _ _ _ subtype.val)] variables [topological_space Ξ²] [topological_space Ξ³] /-- A function between topological spaces is continuous at a point `xβ‚€` within a subset `s` if `f x` tends to `f xβ‚€` when `x` tends to `xβ‚€` while staying within `s`. -/ def continuous_within_at (f : Ξ± β†’ Ξ²) (s : set Ξ±) (x : Ξ±) : Prop := tendsto f (nhds_within x s) (𝓝 (f x)) /-- If a function is continuous within `s` at `x`, then it tends to `f x` within `s` by definition. We register this fact for use with the dot notation, especially to use `tendsto.comp` as `continuous_within_at.comp` will have a different meaning. -/ lemma continuous_within_at.tendsto {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) : tendsto f (nhds_within x s) (𝓝 (f x)) := h /-- A function between topological spaces is continuous on a subset `s` when it's continuous at every point of `s` within `s`. -/ def continuous_on (f : Ξ± β†’ Ξ²) (s : set Ξ±) : Prop := βˆ€ x ∈ s, continuous_within_at f s x theorem continuous_within_at_univ (f : Ξ± β†’ Ξ²) (x : Ξ±) : continuous_within_at f set.univ x ↔ continuous_at f x := by rw [continuous_at, continuous_within_at, nhds_within_univ] theorem continuous_within_at_iff_continuous_at_restrict (f : Ξ± β†’ Ξ²) {x : Ξ±} {s : set Ξ±} (h : x ∈ s) : continuous_within_at f s x ↔ continuous_at (function.restrict f s) ⟨x, h⟩ := tendsto_nhds_within_iff_subtype h f _ theorem continuous_within_at.tendsto_nhds_within_image {f : Ξ± β†’ Ξ²} {x : Ξ±} {s : set Ξ±} (h : continuous_within_at f s x) : tendsto f (nhds_within x s) (nhds_within (f x) (f '' s)) := tendsto_inf.2 ⟨h, tendsto_principal.2 $ mem_inf_sets_of_right $ mem_principal_sets.2 $ Ξ» x, mem_image_of_mem _⟩ theorem continuous_on_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} : continuous_on f s ↔ βˆ€ x ∈ s, βˆ€ t : set Ξ², is_open t β†’ f x ∈ t β†’ βˆƒ u, is_open u ∧ x ∈ u ∧ u ∩ s βŠ† f ⁻¹' t := by simp only [continuous_on, continuous_within_at, tendsto_nhds, mem_nhds_within] theorem continuous_on_iff_continuous_restrict {f : Ξ± β†’ Ξ²} {s : set Ξ±} : continuous_on f s ↔ continuous (function.restrict f s) := begin rw [continuous_on, continuous_iff_continuous_at], split, { rintros h ⟨x, xs⟩, exact (continuous_within_at_iff_continuous_at_restrict f xs).mp (h x xs) }, intros h x xs, exact (continuous_within_at_iff_continuous_at_restrict f xs).mpr (h ⟨x, xs⟩) end theorem continuous_on_iff' {f : Ξ± β†’ Ξ²} {s : set Ξ±} : continuous_on f s ↔ βˆ€ t : set Ξ², is_open t β†’ βˆƒ u, is_open u ∧ f ⁻¹' t ∩ s = u ∩ s := have βˆ€ t, is_open (function.restrict f s ⁻¹' t) ↔ βˆƒ (u : set Ξ±), is_open u ∧ f ⁻¹' t ∩ s = u ∩ s, begin intro t, rw [is_open_induced_iff, function.restrict_eq, set.preimage_comp], simp only [subtype.preimage_val_eq_preimage_val_iff], split; { rintros ⟨u, ou, useq⟩, exact ⟨u, ou, useq.symm⟩ } end, by rw [continuous_on_iff_continuous_restrict, continuous]; simp only [this] theorem continuous_on_iff_is_closed {f : Ξ± β†’ Ξ²} {s : set Ξ±} : continuous_on f s ↔ βˆ€ t : set Ξ², is_closed t β†’ βˆƒ u, is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s := have βˆ€ t, is_closed (function.restrict f s ⁻¹' t) ↔ βˆƒ (u : set Ξ±), is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s, begin intro t, rw [is_closed_induced_iff, function.restrict_eq, set.preimage_comp], simp only [subtype.preimage_val_eq_preimage_val_iff] end, by rw [continuous_on_iff_continuous_restrict, continuous_iff_is_closed]; simp only [this] theorem nhds_within_le_comap {x : Ξ±} {s : set Ξ±} {f : Ξ± β†’ Ξ²} (ctsf : continuous_within_at f s x) : nhds_within x s ≀ comap f (nhds_within (f x) (f '' s)) := map_le_iff_le_comap.1 ctsf.tendsto_nhds_within_image theorem continuous_within_at_iff_ptendsto_res (f : Ξ± β†’ Ξ²) {x : Ξ±} {s : set Ξ±} : continuous_within_at f s x ↔ ptendsto (pfun.res f s) (𝓝 x) (𝓝 (f x)) := tendsto_iff_ptendsto _ _ _ _ lemma continuous_iff_continuous_on_univ {f : Ξ± β†’ Ξ²} : continuous f ↔ continuous_on f univ := by simp [continuous_iff_continuous_at, continuous_on, continuous_at, continuous_within_at, nhds_within_univ] lemma continuous_within_at.mono {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (h : continuous_within_at f t x) (hs : s βŠ† t) : continuous_within_at f s x := tendsto_le_left (nhds_within_mono x hs) h lemma continuous_within_at_inter' {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (h : t ∈ nhds_within x s) : continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x := by simp [continuous_within_at, nhds_within_restrict'' s h] lemma continuous_within_at_inter {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (h : t ∈ 𝓝 x) : continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x := by simp [continuous_within_at, nhds_within_restrict' s h] lemma continuous_within_at.union {f : Ξ± β†’ Ξ²} {s t : set Ξ±} {x : Ξ±} (hs : continuous_within_at f s x) (ht : continuous_within_at f t x) : continuous_within_at f (s βˆͺ t) x := by simp only [continuous_within_at, nhds_within_union, tendsto, map_sup, lattice.sup_le_iff.2 ⟨hs, ht⟩] lemma continuous_within_at.mem_closure_image {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) (hx : x ∈ closure s) : f x ∈ closure (f '' s) := mem_closure_of_tendsto (mem_closure_iff_nhds_within_ne_bot.1 hx) h $ mem_sets_of_superset self_mem_nhds_within (subset_preimage_image f s) lemma continuous_within_at.mem_closure {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} {A : set Ξ²} (h : continuous_within_at f s x) (hx : x ∈ closure s) (hA : s βŠ† f⁻¹' A) : f x ∈ closure A := closure_mono (image_subset_iff.2 hA) (h.mem_closure_image hx) lemma continuous_within_at.image_closure {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hf : βˆ€ x ∈ closure s, continuous_within_at f s x) : f '' (closure s) βŠ† closure (f '' s) := begin rintros _ ⟨x, hx, rfl⟩, exact (hf x hx).mem_closure_image hx end theorem is_open_map.continuous_on_image_of_left_inv_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} (h : is_open_map (function.restrict f s)) {finv : Ξ² β†’ Ξ±} (hleft : left_inv_on finv f s) : continuous_on finv (f '' s) := begin rintros _ ⟨x, xs, rfl⟩ t ht, rw [hleft xs] at ht, replace h := h.nhds_le ⟨x, xs⟩, apply mem_nhds_within_of_mem_nhds, apply h, erw [map_compose.symm, function.comp, mem_map, ← nhds_within_eq_map_subtype_val], apply mem_sets_of_superset (inter_mem_nhds_within _ ht), assume y hy, rw [mem_set_of_eq, mem_preimage, hleft hy.1], exact hy.2 end theorem is_open_map.continuous_on_range_of_left_inverse {f : Ξ± β†’ Ξ²} (hf : is_open_map f) {finv : Ξ² β†’ Ξ±} (hleft : function.left_inverse finv f) : continuous_on finv (range f) := begin rw [← image_univ], exact (hf.restrict is_open_univ).continuous_on_image_of_left_inv_on (Ξ» x _, hleft x) end lemma continuous_on.congr_mono {f g : Ξ± β†’ Ξ²} {s s₁ : set Ξ±} (h : continuous_on f s) (h' : βˆ€x ∈ s₁, g x = f x) (h₁ : s₁ βŠ† s) : continuous_on g s₁ := begin assume x hx, unfold continuous_within_at, have A := (h x (h₁ hx)).mono h₁, unfold continuous_within_at at A, rw ← h' x hx at A, have : {x : Ξ± | g x = f x} ∈ nhds_within x s₁ := mem_inf_sets_of_right h', apply tendsto.congr' _ A, convert this, ext, finish end lemma continuous_on.congr {f g : Ξ± β†’ Ξ²} {s : set Ξ±} (h : continuous_on f s) (h' : βˆ€x ∈ s, g x = f x) : continuous_on g s := h.congr_mono h' (subset.refl _) lemma continuous_on_congr {f g : Ξ± β†’ Ξ²} {s : set Ξ±} (h' : βˆ€x ∈ s, g x = f x) : continuous_on g s ↔ continuous_on f s := ⟨λ h, continuous_on.congr h (Ξ»x hx, (h' x hx).symm), Ξ» h, continuous_on.congr h h'⟩ lemma continuous_at.continuous_within_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_at f x) : continuous_within_at f s x := continuous_within_at.mono ((continuous_within_at_univ f x).2 h) (subset_univ _) lemma continuous_within_at.continuous_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) (hs : s ∈ 𝓝 x) : continuous_at f x := begin have : s = univ ∩ s, by rw univ_inter, rwa [this, continuous_within_at_inter hs, continuous_within_at_univ] at h end lemma continuous_within_at.comp {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} {x : Ξ±} (hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) (h : s βŠ† f ⁻¹' t) : continuous_within_at (g ∘ f) s x := begin have : tendsto f (principal s) (principal t), by { rw tendsto_principal_principal, exact Ξ»x hx, h hx }, have : tendsto f (nhds_within x s) (principal t) := tendsto_le_left lattice.inf_le_right this, have : tendsto f (nhds_within x s) (nhds_within (f x) t) := tendsto_inf.2 ⟨hf, this⟩, exact tendsto.comp hg this end lemma continuous_on.comp {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hg : continuous_on g t) (hf : continuous_on f s) (h : s βŠ† f ⁻¹' t) : continuous_on (g ∘ f) s := Ξ»x hx, continuous_within_at.comp (hg _ (h hx)) (hf x hx) h lemma continuous_on.mono {f : Ξ± β†’ Ξ²} {s t : set Ξ±} (hf : continuous_on f s) (h : t βŠ† s) : continuous_on f t := Ξ»x hx, tendsto_le_left (nhds_within_mono _ h) (hf x (h hx)) lemma continuous.continuous_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} (h : continuous f) : continuous_on f s := begin rw continuous_iff_continuous_on_univ at h, exact h.mono (subset_univ _) end lemma continuous.continuous_within_at {f : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous f) : continuous_within_at f s x := tendsto_le_left lattice.inf_le_left (h.tendsto x) lemma continuous.comp_continuous_on {g : Ξ² β†’ Ξ³} {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hg : continuous g) (hf : continuous_on f s) : continuous_on (g ∘ f) s := hg.continuous_on.comp hf subset_preimage_univ lemma continuous_within_at.preimage_mem_nhds_within {f : Ξ± β†’ Ξ²} {x : Ξ±} {s : set Ξ±} {t : set Ξ²} (h : continuous_within_at f s x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ nhds_within x s := h ht lemma continuous_within_at.preimage_mem_nhds_within' {f : Ξ± β†’ Ξ²} {x : Ξ±} {s : set Ξ±} {t : set Ξ²} (h : continuous_within_at f s x) (ht : t ∈ nhds_within (f x) (f '' s)) : f ⁻¹' t ∈ nhds_within x s := begin rw mem_nhds_within at ht, rcases ht with ⟨u, u_open, fxu, hu⟩, have : f ⁻¹' u ∩ s ∈ nhds_within x s := filter.inter_mem_sets (h (mem_nhds_sets u_open fxu)) self_mem_nhds_within, apply mem_sets_of_superset this, calc f ⁻¹' u ∩ s βŠ† f ⁻¹' u ∩ f ⁻¹' (f '' s) : inter_subset_inter_right _ (subset_preimage_image f s) ... = f ⁻¹' (u ∩ f '' s) : rfl ... βŠ† f ⁻¹' t : preimage_mono hu end lemma continuous_within_at.congr_of_mem_nhds_within {f f₁ : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) (h₁ : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) : continuous_within_at f₁ s x := by rwa [continuous_within_at, filter.tendsto, hx, filter.map_cong h₁] lemma continuous_within_at.congr {f f₁ : Ξ± β†’ Ξ²} {s : set Ξ±} {x : Ξ±} (h : continuous_within_at f s x) (h₁ : βˆ€y∈s, f₁ y = f y) (hx : f₁ x = f x) : continuous_within_at f₁ s x := h.congr_of_mem_nhds_within (mem_sets_of_superset self_mem_nhds_within h₁) hx lemma continuous_on_const {s : set Ξ±} {c : Ξ²} : continuous_on (Ξ»x, c) s := continuous_const.continuous_on lemma continuous_within_at_const {b : Ξ²} {s : set Ξ±} {x : Ξ±} : continuous_within_at (Ξ» _:Ξ±, b) s x := continuous_const.continuous_within_at lemma continuous_on_id {s : set Ξ±} : continuous_on id s := continuous_id.continuous_on lemma continuous_within_at_id {s : set Ξ±} {x : Ξ±} : continuous_within_at id s x := continuous_id.continuous_within_at lemma continuous_on_open_iff {f : Ξ± β†’ Ξ²} {s : set Ξ±} (hs : is_open s) : continuous_on f s ↔ (βˆ€t, is_open t β†’ is_open (s ∩ f⁻¹' t)) := begin rw continuous_on_iff', split, { assume h t ht, rcases h t ht with ⟨u, u_open, hu⟩, rw [inter_comm, hu], apply is_open_inter u_open hs }, { assume h t ht, refine ⟨s ∩ f ⁻¹' t, h t ht, _⟩, rw [@inter_comm _ s (f ⁻¹' t), inter_assoc, inter_self] } end lemma continuous_on.preimage_open_of_open {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hf : continuous_on f s) (hs : is_open s) (ht : is_open t) : is_open (s ∩ f⁻¹' t) := (continuous_on_open_iff hs).1 hf t ht lemma continuous_on.preimage_closed_of_closed {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hf : continuous_on f s) (hs : is_closed s) (ht : is_closed t) : is_closed (s ∩ f⁻¹' t) := begin rcases continuous_on_iff_is_closed.1 hf t ht with ⟨u, hu⟩, rw [inter_comm, hu.2], apply is_closed_inter hu.1 hs end lemma continuous_on.preimage_interior_subset_interior_preimage {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (hf : continuous_on f s) (hs : is_open s) : s ∩ f⁻¹' (interior t) βŠ† s ∩ interior (f⁻¹' t) := calc s ∩ f ⁻¹' (interior t) = interior (s ∩ f ⁻¹' (interior t)) : (interior_eq_of_open (hf.preimage_open_of_open hs is_open_interior)).symm ... βŠ† interior (s ∩ f ⁻¹' t) : interior_mono (inter_subset_inter (subset.refl _) (preimage_mono interior_subset)) ... = s ∩ interior (f ⁻¹' t) : by rw [interior_inter, interior_eq_of_open hs] lemma continuous_on_of_locally_continuous_on {f : Ξ± β†’ Ξ²} {s : set Ξ±} (h : βˆ€x∈s, βˆƒt, is_open t ∧ x ∈ t ∧ continuous_on f (s ∩ t)) : continuous_on f s := begin assume x xs, rcases h x xs with ⟨t, open_t, xt, ct⟩, have := ct x ⟨xs, xt⟩, rwa [continuous_within_at, ← nhds_within_restrict _ xt open_t] at this end lemma continuous_on_open_of_generate_from {Ξ² : Type*} {s : set Ξ±} {T : set (set Ξ²)} {f : Ξ± β†’ Ξ²} (hs : is_open s) (h : βˆ€t ∈ T, is_open (s ∩ f⁻¹' t)) : @continuous_on Ξ± Ξ² _ (topological_space.generate_from T) f s := begin rw continuous_on_open_iff, assume t ht, induction ht with u hu u v Tu Tv hu hv U hU hU', { exact h u hu }, { simp only [preimage_univ, inter_univ], exact hs }, { have : s ∩ f ⁻¹' (u ∩ v) = (s ∩ f ⁻¹' u) ∩ (s ∩ f ⁻¹' v), by { ext x, simp, split, finish, finish }, rw this, exact is_open_inter hu hv }, { rw [preimage_sUnion, inter_bUnion], exact is_open_bUnion hU' }, { exact hs } end lemma continuous_within_at.prod {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {s : set Ξ±} {x : Ξ±} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (Ξ»x, (f x, g x)) s x := hf.prod_mk_nhds hg lemma continuous_on.prod {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {s : set Ξ±} (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (Ξ»x, (f x, g x)) s := Ξ»x hx, continuous_within_at.prod (hf x hx) (hg x hx)
c44a14cac0901306c73517856b404cb5efdc39a6
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/convex/stone_separation.lean
b60920f402d7f4b07c4b77c4f4c84946bcaba08d
[ "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,116
lean
/- Copyright (c) 2022 YaΓ«l Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: YaΓ«l Dillies -/ import analysis.convex.join /-! # Stone's separation theorem This file prove Stone's separation theorem. This tells us that any two disjoint convex sets can be separated by a convex set whose complement is also convex. In locally convex real topological vector spaces, the Hahn-Banach separation theorems provide stronger statements: one may find a separating hyperplane, instead of merely a convex set whose complement is convex. -/ open set open_locale big_operators variables {π•œ E ΞΉ : Type*} [linear_ordered_field π•œ] [add_comm_group E] [module π•œ E] {s t : set E} /-- In a tetrahedron with vertices `x`, `y`, `p`, `q`, any segment `[u, v]` joining the opposite edges `[x, p]` and `[y, q]` passes through any triangle of vertices `p`, `q`, `z` where `z ∈ [x, y]`. -/ lemma not_disjoint_segment_convex_hull_triple {p q u v x y z : E} (hz : z ∈ segment π•œ x y) (hu : u ∈ segment π•œ x p) (hv : v ∈ segment π•œ y q) : Β¬ disjoint (segment π•œ u v) (convex_hull π•œ {p, q, z}) := begin rw not_disjoint_iff, obtain ⟨az, bz, haz, hbz, habz, rfl⟩ := hz, obtain rfl | haz' := haz.eq_or_lt, { rw zero_add at habz, rw [zero_smul, zero_add, habz, one_smul], refine ⟨v, right_mem_segment _ _ _, segment_subset_convex_hull _ _ hv⟩; simp }, obtain ⟨av, bv, hav, hbv, habv, rfl⟩ := hv, obtain rfl | hav' := hav.eq_or_lt, { rw zero_add at habv, rw [zero_smul, zero_add, habv, one_smul], exact ⟨q, right_mem_segment _ _ _, subset_convex_hull _ _ $ by simp⟩ }, obtain ⟨au, bu, hau, hbu, habu, rfl⟩ := hu, have hab : 0 < az * av + bz * au := add_pos_of_pos_of_nonneg (mul_pos haz' hav') (mul_nonneg hbz hau), refine ⟨(az * av / (az * av + bz * au)) β€’ (au β€’ x + bu β€’ p) + (bz * au / (az * av + bz * au)) β€’ (av β€’ y + bv β€’ q), ⟨_, _, _, _, _, rfl⟩, _⟩, { exact div_nonneg (mul_nonneg haz hav) hab.le }, { exact div_nonneg (mul_nonneg hbz hau) hab.le }, { rw [←add_div, div_self hab.ne'] }, rw [smul_add, smul_add, add_add_add_comm, add_comm, ←mul_smul, ←mul_smul], classical, let w : fin 3 β†’ π•œ := ![az * av * bu, bz * au * bv, au * av], let z : fin 3 β†’ E := ![p, q, az β€’ x + bz β€’ y], have hwβ‚€ : βˆ€ i, 0 ≀ w i, { rintro i, fin_cases i, { exact mul_nonneg (mul_nonneg haz hav) hbu }, { exact mul_nonneg (mul_nonneg hbz hau) hbv }, { exact mul_nonneg hau hav } }, have hw : βˆ‘ i, w i = az * av + bz * au, { transitivity az * av * bu + (bz * au * bv + au * av), { simp [w, fin.sum_univ_succ, fin.sum_univ_zero] }, rw [←one_mul (au * av), ←habz, add_mul, ←add_assoc, add_add_add_comm, mul_assoc, ←mul_add, mul_assoc, ←mul_add, mul_comm av, ←add_mul, ←mul_add, add_comm bu, add_comm bv, habu, habv, one_mul, mul_one] }, have hz : βˆ€ i, z i ∈ ({p, q, az β€’ x + bz β€’ y} : set E), { rintro i, fin_cases i; simp [z] }, convert finset.center_mass_mem_convex_hull (finset.univ : finset (fin 3)) (Ξ» i _, hwβ‚€ i) (by rwa hw) (Ξ» i _, hz i), rw finset.center_mass, simp_rw [div_eq_inv_mul, hw, mul_assoc, mul_smul (az * av + bz * au)⁻¹, ←smul_add, add_assoc, ←mul_assoc], congr' 3, rw [←mul_smul, ←mul_rotate, mul_right_comm, mul_smul, ←mul_smul _ av, mul_rotate, mul_smul _ bz, ←smul_add], simp only [list.map, list.pmap, nat.add_def, add_zero, fin.mk_bit0, fin.mk_one, list.foldr_cons, list.foldr_nil], refl, end /-- **Stone's Separation Theorem** -/ lemma exists_convex_convex_compl_subset (hs : convex π•œ s) (ht : convex π•œ t) (hst : disjoint s t) : βˆƒ C : set E, convex π•œ C ∧ convex π•œ Cᢜ ∧ s βŠ† C ∧ t βŠ† Cᢜ := begin let S : set (set E) := {C | convex π•œ C ∧ disjoint C t}, obtain ⟨C, hC, hsC, hCmax⟩ := zorn_subset_nonempty S (Ξ» c hcS hc ⟨t, ht⟩, βŸ¨β‹ƒβ‚€ c, ⟨hc.directed_on.convex_sUnion (Ξ» s hs, (hcS hs).1), disjoint_sUnion_left.2 $ Ξ» c hc, (hcS hc).2⟩, Ξ» s, subset_sUnion_of_mem⟩) s ⟨hs, hst⟩, refine ⟨C, hC.1, convex_iff_segment_subset.2 $ Ξ» x hx y hy z hz hzC, _, hsC, hC.2.subset_compl_left⟩, suffices h : βˆ€ c ∈ Cᢜ, βˆƒ a ∈ C, (segment π•œ c a ∩ t).nonempty, { obtain ⟨p, hp, u, hu, hut⟩ := h x hx, obtain ⟨q, hq, v, hv, hvt⟩ := h y hy, refine not_disjoint_segment_convex_hull_triple hz hu hv (hC.2.symm.mono (ht.segment_subset hut hvt) $ convex_hull_min _ hC.1), simp [insert_subset, hp, hq, singleton_subset_iff.2 hzC] }, rintro c hc, by_contra' h, suffices h : disjoint (convex_hull π•œ (insert c C)) t, { rw ←hCmax _ ⟨convex_convex_hull _ _, h⟩ ((subset_insert _ _).trans $ subset_convex_hull _ _) at hc, exact hc (subset_convex_hull _ _ $ mem_insert _ _) }, rw [convex_hull_insert ⟨z, hzC⟩, convex_join_singleton_left], refine disjoint_Unionβ‚‚_left.2 (Ξ» a ha, disjoint_iff_inf_le.mpr $ Ξ» b hb, h a _ ⟨b, hb⟩), rwa ←hC.1.convex_hull_eq, end
2ad2620bf42fb639513900f19ad17eeaef29a79a
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/typeclass_metas_internal_goals3.lean
1ab0b26cafbeb846e38764512290c78835387e87
[ "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
592
lean
class Base (Ξ± : Type) := (u:Unit) class Depends (Ξ± : Type) [Base Ξ±] := (u:Unit) class Top := (u:Unit) instance AllBase {Ξ± : Type} : Base Ξ± := {u:=()} instance DependsNotConstrainingImplicit {Ξ± : Type} /- [Base Ξ±] -/ {_:Base Ξ±} : Depends Ξ± := {u:=()} instance BaseAsImplicit₁ {Ξ± : Type} {_:Base Ξ±} [Depends Ξ±] : Top := {u:=()} instance BaseAsInstImplicit {Ξ± : Type} [Base Ξ±] [Depends Ξ±] : Top := {u:=()} instance BaseAsImplicitβ‚‚ {Ξ± : Type} {_:Base Ξ±} [Depends Ξ±] : Top := {u:=()} axiom K : Type instance BaseK : Base K := {u:=()} set_option pp.all true #synth Top
393d07e16046aaa476439203291cbd61e5edfe60
bb31430994044506fa42fd667e2d556327e18dfe
/src/topology/metric_space/lipschitz.lean
4f33443074d1ecd78311296747c2e7516d8a6f92
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
29,198
lean
/- Copyright (c) 2018 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 logic.function.iterate import data.set.intervals.proj_Icc import topology.algebra.order.field import topology.metric_space.basic import topology.bornology.hom /-! # Lipschitz continuous functions A map `f : Ξ± β†’ Ξ²` between two (extended) metric spaces is called *Lipschitz continuous* with constant `K β‰₯ 0` if for all `x, y` we have `edist (f x) (f y) ≀ K * edist x y`. For a metric space, the latter inequality is equivalent to `dist (f x) (f y) ≀ K * dist x y`. There is also a version asserting this inequality only for `x` and `y` in some set `s`. In this file we provide various ways to prove that various combinations of Lipschitz continuous functions are Lipschitz continuous. We also prove that Lipschitz continuous functions are uniformly continuous. ## Main definitions and lemmas * `lipschitz_with K f`: states that `f` is Lipschitz with constant `K : ℝβ‰₯0` * `lipschitz_on_with K f`: states that `f` is Lipschitz with constant `K : ℝβ‰₯0` on a set `s` * `lipschitz_with.uniform_continuous`: a Lipschitz function is uniformly continuous * `lipschitz_on_with.uniform_continuous_on`: a function which is Lipschitz on a set is uniformly continuous on that set. ## Implementation notes The parameter `K` has type `ℝβ‰₯0`. This way we avoid conjuction in the definition and have coercions both to `ℝ` and `ℝβ‰₯0∞`. Constructors whose names end with `'` take `K : ℝ` as an argument, and return `lipschitz_with (real.to_nnreal K) f`. -/ universes u v w x open filter function set open_locale topological_space nnreal ennreal variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {ΞΉ : Type x} /-- A function `f` is Lipschitz continuous with constant `K β‰₯ 0` if for all `x, y` we have `dist (f x) (f y) ≀ K * dist x y` -/ def lipschitz_with [pseudo_emetric_space Ξ±] [pseudo_emetric_space Ξ²] (K : ℝβ‰₯0) (f : Ξ± β†’ Ξ²) := βˆ€x y, edist (f x) (f y) ≀ K * edist x y lemma lipschitz_with_iff_dist_le_mul [pseudo_metric_space Ξ±] [pseudo_metric_space Ξ²] {K : ℝβ‰₯0} {f : Ξ± β†’ Ξ²} : lipschitz_with K f ↔ βˆ€ x y, dist (f x) (f y) ≀ K * dist x y := by { simp only [lipschitz_with, edist_nndist, dist_nndist], norm_cast } alias lipschitz_with_iff_dist_le_mul ↔ lipschitz_with.dist_le_mul lipschitz_with.of_dist_le_mul /-- A function `f` is Lipschitz continuous with constant `K β‰₯ 0` on `s` if for all `x, y` in `s` we have `dist (f x) (f y) ≀ K * dist x y` -/ def lipschitz_on_with [pseudo_emetric_space Ξ±] [pseudo_emetric_space Ξ²] (K : ℝβ‰₯0) (f : Ξ± β†’ Ξ²) (s : set Ξ±) := βˆ€ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), edist (f x) (f y) ≀ K * edist x y @[simp] lemma lipschitz_on_with_empty [pseudo_emetric_space Ξ±] [pseudo_emetric_space Ξ²] (K : ℝβ‰₯0) (f : Ξ± β†’ Ξ²) : lipschitz_on_with K f βˆ… := Ξ» x x_in y y_in, false.elim x_in lemma lipschitz_on_with.mono [pseudo_emetric_space Ξ±] [pseudo_emetric_space Ξ²] {K : ℝβ‰₯0} {s t : set Ξ±} {f : Ξ± β†’ Ξ²} (hf : lipschitz_on_with K f t) (h : s βŠ† t) : lipschitz_on_with K f s := Ξ» x x_in y y_in, hf (h x_in) (h y_in) lemma lipschitz_on_with_iff_dist_le_mul [pseudo_metric_space Ξ±] [pseudo_metric_space Ξ²] {K : ℝβ‰₯0} {s : set Ξ±} {f : Ξ± β†’ Ξ²} : lipschitz_on_with K f s ↔ βˆ€ (x ∈ s) (y ∈ s), dist (f x) (f y) ≀ K * dist x y := by { simp only [lipschitz_on_with, edist_nndist, dist_nndist], norm_cast } alias lipschitz_on_with_iff_dist_le_mul ↔ lipschitz_on_with.dist_le_mul lipschitz_on_with.of_dist_le_mul @[simp] lemma lipschitz_on_univ [pseudo_emetric_space Ξ±] [pseudo_emetric_space Ξ²] {K : ℝβ‰₯0} {f : Ξ± β†’ Ξ²} : lipschitz_on_with K f univ ↔ lipschitz_with K f := by simp [lipschitz_on_with, lipschitz_with] lemma lipschitz_on_with_iff_restrict [pseudo_emetric_space Ξ±] [pseudo_emetric_space Ξ²] {K : ℝβ‰₯0} {f : Ξ± β†’ Ξ²} {s : set Ξ±} : lipschitz_on_with K f s ↔ lipschitz_with K (s.restrict f) := by simp only [lipschitz_on_with, lipschitz_with, set_coe.forall', restrict, subtype.edist_eq] alias lipschitz_on_with_iff_restrict ↔ lipschitz_on_with.to_restrict _ lemma maps_to.lipschitz_on_with_iff_restrict [pseudo_emetric_space Ξ±] [pseudo_emetric_space Ξ²] {K : ℝβ‰₯0} {f : Ξ± β†’ Ξ²} {s : set Ξ±} {t : set Ξ²} (h : maps_to f s t) : lipschitz_on_with K f s ↔ lipschitz_with K (h.restrict f s t) := lipschitz_on_with_iff_restrict alias maps_to.lipschitz_on_with_iff_restrict ↔ lipschitz_on_with.to_restrict_maps_to _ namespace lipschitz_with section emetric open emetric variables [pseudo_emetric_space Ξ±] [pseudo_emetric_space Ξ²] [pseudo_emetric_space Ξ³] variables {K : ℝβ‰₯0} {f : Ξ± β†’ Ξ²} {x y : Ξ±} {r : ℝβ‰₯0∞} protected lemma lipschitz_on_with (h : lipschitz_with K f) (s : set Ξ±) : lipschitz_on_with K f s := Ξ» x _ y _, h x y lemma edist_le_mul (h : lipschitz_with K f) (x y : Ξ±) : edist (f x) (f y) ≀ K * edist x y := h x y lemma edist_le_mul_of_le (h : lipschitz_with K f) (hr : edist x y ≀ r) : edist (f x) (f y) ≀ K * r := (h x y).trans $ ennreal.mul_left_mono hr lemma edist_lt_mul_of_lt (h : lipschitz_with K f) (hK : K β‰  0) (hr : edist x y < r) : edist (f x) (f y) < K * r := (h x y).trans_lt $ (ennreal.mul_lt_mul_left (ennreal.coe_ne_zero.2 hK) ennreal.coe_ne_top).2 hr lemma maps_to_emetric_closed_ball (h : lipschitz_with K f) (x : Ξ±) (r : ℝβ‰₯0∞) : maps_to f (closed_ball x r) (closed_ball (f x) (K * r)) := Ξ» y hy, h.edist_le_mul_of_le hy lemma maps_to_emetric_ball (h : lipschitz_with K f) (hK : K β‰  0) (x : Ξ±) (r : ℝβ‰₯0∞) : maps_to f (ball x r) (ball (f x) (K * r)) := Ξ» y hy, h.edist_lt_mul_of_lt hK hy lemma edist_lt_top (hf : lipschitz_with K f) {x y : Ξ±} (h : edist x y β‰  ⊀) : edist (f x) (f y) < ⊀ := (hf x y).trans_lt $ ennreal.mul_lt_top ennreal.coe_ne_top h lemma mul_edist_le (h : lipschitz_with K f) (x y : Ξ±) : (K⁻¹ : ℝβ‰₯0∞) * edist (f x) (f y) ≀ edist x y := begin rw [mul_comm, ← div_eq_mul_inv], exact ennreal.div_le_of_le_mul' (h x y) end protected lemma of_edist_le (h : βˆ€ x y, edist (f x) (f y) ≀ edist x y) : lipschitz_with 1 f := Ξ» x y, by simp only [ennreal.coe_one, one_mul, h] protected lemma weaken (hf : lipschitz_with K f) {K' : ℝβ‰₯0} (h : K ≀ K') : lipschitz_with K' f := assume x y, le_trans (hf x y) $ ennreal.mul_right_mono (ennreal.coe_le_coe.2 h) lemma ediam_image_le (hf : lipschitz_with K f) (s : set Ξ±) : emetric.diam (f '' s) ≀ K * emetric.diam s := begin apply emetric.diam_le, rintros _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩, exact hf.edist_le_mul_of_le (emetric.edist_le_diam_of_mem hx hy) end lemma edist_lt_of_edist_lt_div (hf : lipschitz_with K f) {x y : Ξ±} {d : ℝβ‰₯0∞} (h : edist x y < d / K) : edist (f x) (f y) < d := calc edist (f x) (f y) ≀ K * edist x y : hf x y ... < d : ennreal.mul_lt_of_lt_div' h /-- A Lipschitz function is uniformly continuous -/ protected lemma uniform_continuous (hf : lipschitz_with K f) : uniform_continuous f := begin refine emetric.uniform_continuous_iff.2 (λΡ Ξ΅pos, _), use [Ξ΅ / K, ennreal.div_pos_iff.2 ⟨ne_of_gt Ξ΅pos, ennreal.coe_ne_top⟩], exact Ξ» x y, hf.edist_lt_of_edist_lt_div end /-- A Lipschitz function is continuous -/ protected lemma continuous (hf : lipschitz_with K f) : continuous f := hf.uniform_continuous.continuous protected lemma const (b : Ξ²) : lipschitz_with 0 (Ξ»a:Ξ±, b) := assume x y, by simp only [edist_self, zero_le] protected lemma id : lipschitz_with 1 (@id Ξ±) := lipschitz_with.of_edist_le $ assume x y, le_rfl protected lemma subtype_val (s : set Ξ±) : lipschitz_with 1 (subtype.val : s β†’ Ξ±) := lipschitz_with.of_edist_le $ assume x y, le_rfl protected lemma subtype_coe (s : set Ξ±) : lipschitz_with 1 (coe : s β†’ Ξ±) := lipschitz_with.subtype_val s lemma subtype_mk (hf : lipschitz_with K f) {p : Ξ² β†’ Prop} (hp : βˆ€ x, p (f x)) : lipschitz_with K (Ξ» x, ⟨f x, hp x⟩ : Ξ± β†’ {y // p y}) := hf protected lemma eval {Ξ± : ΞΉ β†’ Type u} [Ξ  i, pseudo_emetric_space (Ξ± i)] [fintype ΞΉ] (i : ΞΉ) : lipschitz_with 1 (function.eval i : (Ξ  i, Ξ± i) β†’ Ξ± i) := lipschitz_with.of_edist_le $ Ξ» f g, by convert edist_le_pi_edist f g i protected lemma restrict (hf : lipschitz_with K f) (s : set Ξ±) : lipschitz_with K (s.restrict f) := Ξ» x y, hf x y protected lemma comp {Kf Kg : ℝβ‰₯0} {f : Ξ² β†’ Ξ³} {g : Ξ± β†’ Ξ²} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf * Kg) (f ∘ g) := assume x y, calc edist (f (g x)) (f (g y)) ≀ Kf * edist (g x) (g y) : hf _ _ ... ≀ Kf * (Kg * edist x y) : ennreal.mul_left_mono (hg _ _) ... = (Kf * Kg : ℝβ‰₯0) * edist x y : by rw [← mul_assoc, ennreal.coe_mul] lemma comp_lipschitz_on_with {Kf Kg : ℝβ‰₯0} {f : Ξ² β†’ Ξ³} {g : Ξ± β†’ Ξ²} {s : set Ξ±} (hf : lipschitz_with Kf f) (hg : lipschitz_on_with Kg g s) : lipschitz_on_with (Kf * Kg) (f ∘ g) s := lipschitz_on_with_iff_restrict.mpr $ hf.comp hg.to_restrict protected lemma prod_fst : lipschitz_with 1 (@prod.fst Ξ± Ξ²) := lipschitz_with.of_edist_le $ assume x y, le_max_left _ _ protected lemma prod_snd : lipschitz_with 1 (@prod.snd Ξ± Ξ²) := lipschitz_with.of_edist_le $ assume x y, le_max_right _ _ protected lemma prod {f : Ξ± β†’ Ξ²} {Kf : ℝβ‰₯0} (hf : lipschitz_with Kf f) {g : Ξ± β†’ Ξ³} {Kg : ℝβ‰₯0} (hg : lipschitz_with Kg g) : lipschitz_with (max Kf Kg) (Ξ» x, (f x, g x)) := begin assume x y, rw [ennreal.coe_mono.map_max, prod.edist_eq, ennreal.max_mul], exact max_le_max (hf x y) (hg x y) end protected lemma prod_mk_left (a : Ξ±) : lipschitz_with 1 (prod.mk a : Ξ² β†’ Ξ± Γ— Ξ²) := by simpa only [max_eq_right zero_le_one] using (lipschitz_with.const a).prod lipschitz_with.id protected lemma prod_mk_right (b : Ξ²) : lipschitz_with 1 (Ξ» a : Ξ±, (a, b)) := by simpa only [max_eq_left zero_le_one] using lipschitz_with.id.prod (lipschitz_with.const b) protected lemma uncurry {f : Ξ± β†’ Ξ² β†’ Ξ³} {KΞ± KΞ² : ℝβ‰₯0} (hΞ± : βˆ€ b, lipschitz_with KΞ± (Ξ» a, f a b)) (hΞ² : βˆ€ a, lipschitz_with KΞ² (f a)) : lipschitz_with (KΞ± + KΞ²) (function.uncurry f) := begin rintros ⟨a₁, bβ‚βŸ© ⟨aβ‚‚, bβ‚‚βŸ©, simp only [function.uncurry, ennreal.coe_add, add_mul], apply le_trans (edist_triangle _ (f aβ‚‚ b₁) _), exact add_le_add (le_trans (hΞ± _ _ _) $ ennreal.mul_left_mono $ le_max_left _ _) (le_trans (hΞ² _ _ _) $ ennreal.mul_left_mono $ le_max_right _ _) end protected lemma iterate {f : Ξ± β†’ Ξ±} (hf : lipschitz_with K f) : βˆ€n, lipschitz_with (K ^ n) (f^[n]) | 0 := by simpa only [pow_zero] using lipschitz_with.id | (n + 1) := by rw [pow_succ']; exact (iterate n).comp hf lemma edist_iterate_succ_le_geometric {f : Ξ± β†’ Ξ±} (hf : lipschitz_with K f) (x n) : edist (f^[n] x) (f^[n + 1] x) ≀ edist x (f x) * K ^ n := begin rw [iterate_succ, mul_comm], simpa only [ennreal.coe_pow] using (hf.iterate n) x (f x) end protected lemma mul {f g : function.End Ξ±} {Kf Kg} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf * Kg) (f * g : function.End Ξ±) := hf.comp hg /-- The product of a list of Lipschitz continuous endomorphisms is a Lipschitz continuous endomorphism. -/ protected lemma list_prod (f : ΞΉ β†’ function.End Ξ±) (K : ΞΉ β†’ ℝβ‰₯0) (h : βˆ€ i, lipschitz_with (K i) (f i)) : βˆ€ l : list ΞΉ, lipschitz_with (l.map K).prod (l.map f).prod | [] := by simpa using lipschitz_with.id | (i :: l) := by { simp only [list.map_cons, list.prod_cons], exact (h i).mul (list_prod l) } protected lemma pow {f : function.End Ξ±} {K} (h : lipschitz_with K f) : βˆ€ n : β„•, lipschitz_with (K^n) (f^n : function.End Ξ±) | 0 := by simpa only [pow_zero] using lipschitz_with.id | (n + 1) := by { rw [pow_succ, pow_succ], exact h.mul (pow n) } end emetric section metric variables [pseudo_metric_space Ξ±] [pseudo_metric_space Ξ²] [pseudo_metric_space Ξ³] {K : ℝβ‰₯0} {f : Ξ± β†’ Ξ²} {x y : Ξ±} {r : ℝ} protected lemma of_dist_le' {K : ℝ} (h : βˆ€ x y, dist (f x) (f y) ≀ K * dist x y) : lipschitz_with (real.to_nnreal K) f := of_dist_le_mul $ Ξ» x y, le_trans (h x y) $ mul_le_mul_of_nonneg_right (real.le_coe_to_nnreal K) dist_nonneg protected lemma mk_one (h : βˆ€ x y, dist (f x) (f y) ≀ dist x y) : lipschitz_with 1 f := of_dist_le_mul $ by simpa only [nnreal.coe_one, one_mul] using h /-- For functions to `ℝ`, it suffices to prove `f x ≀ f y + K * dist x y`; this version doesn't assume `0≀K`. -/ protected lemma of_le_add_mul' {f : Ξ± β†’ ℝ} (K : ℝ) (h : βˆ€x y, f x ≀ f y + K * dist x y) : lipschitz_with (real.to_nnreal K) f := have I : βˆ€ x y, f x - f y ≀ K * dist x y, from assume x y, sub_le_iff_le_add'.2 (h x y), lipschitz_with.of_dist_le' $ assume x y, abs_sub_le_iff.2 ⟨I x y, dist_comm y x β–Έ I y x⟩ /-- For functions to `ℝ`, it suffices to prove `f x ≀ f y + K * dist x y`; this version assumes `0≀K`. -/ protected lemma of_le_add_mul {f : Ξ± β†’ ℝ} (K : ℝβ‰₯0) (h : βˆ€x y, f x ≀ f y + K * dist x y) : lipschitz_with K f := by simpa only [real.to_nnreal_coe] using lipschitz_with.of_le_add_mul' K h protected lemma of_le_add {f : Ξ± β†’ ℝ} (h : βˆ€ x y, f x ≀ f y + dist x y) : lipschitz_with 1 f := lipschitz_with.of_le_add_mul 1 $ by simpa only [nnreal.coe_one, one_mul] protected lemma le_add_mul {f : Ξ± β†’ ℝ} {K : ℝβ‰₯0} (h : lipschitz_with K f) (x y) : f x ≀ f y + K * dist x y := sub_le_iff_le_add'.1 $ le_trans (le_abs_self _) $ h.dist_le_mul x y protected lemma iff_le_add_mul {f : Ξ± β†’ ℝ} {K : ℝβ‰₯0} : lipschitz_with K f ↔ βˆ€ x y, f x ≀ f y + K * dist x y := ⟨lipschitz_with.le_add_mul, lipschitz_with.of_le_add_mul K⟩ lemma nndist_le (hf : lipschitz_with K f) (x y : Ξ±) : nndist (f x) (f y) ≀ K * nndist x y := hf.dist_le_mul x y lemma dist_le_mul_of_le (hf : lipschitz_with K f) (hr : dist x y ≀ r) : dist (f x) (f y) ≀ K * r := (hf.dist_le_mul x y).trans $ mul_le_mul_of_nonneg_left hr K.coe_nonneg lemma maps_to_closed_ball (hf : lipschitz_with K f) (x : Ξ±) (r : ℝ) : maps_to f (metric.closed_ball x r) (metric.closed_ball (f x) (K * r)) := Ξ» y hy, hf.dist_le_mul_of_le hy lemma dist_lt_mul_of_lt (hf : lipschitz_with K f) (hK : K β‰  0) (hr : dist x y < r) : dist (f x) (f y) < K * r := (hf.dist_le_mul x y).trans_lt $ (mul_lt_mul_left $ nnreal.coe_pos.2 hK.bot_lt).2 hr lemma maps_to_ball (hf : lipschitz_with K f) (hK : K β‰  0) (x : Ξ±) (r : ℝ) : maps_to f (metric.ball x r) (metric.ball (f x) (K * r)) := Ξ» y hy, hf.dist_lt_mul_of_lt hK hy /-- A Lipschitz continuous map is a locally bounded map. -/ def to_locally_bounded_map (f : Ξ± β†’ Ξ²) (hf : lipschitz_with K f) : locally_bounded_map Ξ± Ξ² := locally_bounded_map.of_map_bounded f $ Ξ» s hs, let ⟨C, hC⟩ := metric.is_bounded_iff.1 hs in metric.is_bounded_iff.2 ⟨K * C, ball_image_iff.2 $ Ξ» x hx, ball_image_iff.2 $ Ξ» y hy, hf.dist_le_mul_of_le (hC hx hy)⟩ @[simp] lemma coe_to_locally_bounded_map (hf : lipschitz_with K f) : ⇑(hf.to_locally_bounded_map f) = f := rfl lemma comap_cobounded_le (hf : lipschitz_with K f) : comap f (bornology.cobounded Ξ²) ≀ bornology.cobounded Ξ± := (hf.to_locally_bounded_map f).2 lemma bounded_image (hf : lipschitz_with K f) {s : set Ξ±} (hs : metric.bounded s) : metric.bounded (f '' s) := metric.bounded_iff_ediam_ne_top.2 $ ne_top_of_le_ne_top (ennreal.mul_ne_top ennreal.coe_ne_top hs.ediam_ne_top) (hf.ediam_image_le s) lemma diam_image_le (hf : lipschitz_with K f) (s : set Ξ±) (hs : metric.bounded s) : metric.diam (f '' s) ≀ K * metric.diam s := metric.diam_le_of_forall_dist_le (mul_nonneg K.coe_nonneg metric.diam_nonneg) $ ball_image_iff.2 $ Ξ» x hx, ball_image_iff.2 $ Ξ» y hy, hf.dist_le_mul_of_le $ metric.dist_le_diam_of_mem hs hx hy protected lemma dist_left (y : Ξ±) : lipschitz_with 1 (Ξ» x, dist x y) := lipschitz_with.of_le_add $ assume x z, by { rw [add_comm], apply dist_triangle } protected lemma dist_right (x : Ξ±) : lipschitz_with 1 (dist x) := lipschitz_with.of_le_add $ assume y z, dist_triangle_right _ _ _ protected lemma dist : lipschitz_with 2 (function.uncurry $ @dist Ξ± _) := lipschitz_with.uncurry lipschitz_with.dist_left lipschitz_with.dist_right lemma dist_iterate_succ_le_geometric {f : Ξ± β†’ Ξ±} (hf : lipschitz_with K f) (x n) : dist (f^[n] x) (f^[n + 1] x) ≀ dist x (f x) * K ^ n := begin rw [iterate_succ, mul_comm], simpa only [nnreal.coe_pow] using (hf.iterate n).dist_le_mul x (f x) end lemma _root_.lipschitz_with_max : lipschitz_with 1 (Ξ» p : ℝ Γ— ℝ, max p.1 p.2) := lipschitz_with.of_le_add $ Ξ» p₁ pβ‚‚, sub_le_iff_le_add'.1 $ (le_abs_self _).trans (abs_max_sub_max_le_max _ _ _ _) lemma _root_.lipschitz_with_min : lipschitz_with 1 (Ξ» p : ℝ Γ— ℝ, min p.1 p.2) := lipschitz_with.of_le_add $ Ξ» p₁ pβ‚‚, sub_le_iff_le_add'.1 $ (le_abs_self _).trans (abs_min_sub_min_le_max _ _ _ _) end metric section emetric variables {Ξ±} [pseudo_emetric_space Ξ±] {f g : Ξ± β†’ ℝ} {Kf Kg : ℝβ‰₯0} protected lemma max (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (max Kf Kg) (Ξ» x, max (f x) (g x)) := by simpa only [(∘), one_mul] using lipschitz_with_max.comp (hf.prod hg) protected lemma min (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (max Kf Kg) (Ξ» x, min (f x) (g x)) := by simpa only [(∘), one_mul] using lipschitz_with_min.comp (hf.prod hg) lemma max_const (hf : lipschitz_with Kf f) (a : ℝ) : lipschitz_with Kf (Ξ» x, max (f x) a) := by simpa only [max_eq_left (zero_le Kf)] using hf.max (lipschitz_with.const a) lemma const_max (hf : lipschitz_with Kf f) (a : ℝ) : lipschitz_with Kf (Ξ» x, max a (f x)) := by simpa only [max_comm] using hf.max_const a lemma min_const (hf : lipschitz_with Kf f) (a : ℝ) : lipschitz_with Kf (Ξ» x, min (f x) a) := by simpa only [max_eq_left (zero_le Kf)] using hf.min (lipschitz_with.const a) lemma const_min (hf : lipschitz_with Kf f) (a : ℝ) : lipschitz_with Kf (Ξ» x, min a (f x)) := by simpa only [min_comm] using hf.min_const a end emetric protected lemma proj_Icc {a b : ℝ} (h : a ≀ b) : lipschitz_with 1 (proj_Icc a b h) := ((lipschitz_with.id.const_min _).const_max _).subtype_mk _ end lipschitz_with namespace metric variables [pseudo_metric_space Ξ±] [pseudo_metric_space Ξ²] {s : set Ξ±} {t : set Ξ²} lemma bounded.left_of_prod (h : bounded (s Γ—Λ’ t)) (ht : t.nonempty) : bounded s := by simpa only [fst_image_prod s ht] using (@lipschitz_with.prod_fst Ξ± Ξ² _ _).bounded_image h lemma bounded.right_of_prod (h : bounded (s Γ—Λ’ t)) (hs : s.nonempty) : bounded t := by simpa only [snd_image_prod hs t] using (@lipschitz_with.prod_snd Ξ± Ξ² _ _).bounded_image h lemma bounded_prod_of_nonempty (hs : s.nonempty) (ht : t.nonempty) : bounded (s Γ—Λ’ t) ↔ bounded s ∧ bounded t := ⟨λ h, ⟨h.left_of_prod ht, h.right_of_prod hs⟩, Ξ» h, h.1.prod h.2⟩ lemma bounded_prod : bounded (s Γ—Λ’ t) ↔ s = βˆ… ∨ t = βˆ… ∨ bounded s ∧ bounded t := begin rcases s.eq_empty_or_nonempty with rfl|hs, { simp }, rcases t.eq_empty_or_nonempty with rfl|ht, { simp }, simp only [bounded_prod_of_nonempty hs ht, hs.ne_empty, ht.ne_empty, false_or] end end metric namespace lipschitz_on_with section emetric variables [pseudo_emetric_space Ξ±] [pseudo_emetric_space Ξ²] [pseudo_emetric_space Ξ³] variables {K : ℝβ‰₯0} {s : set Ξ±} {f : Ξ± β†’ Ξ²} protected lemma uniform_continuous_on (hf : lipschitz_on_with K f s) : uniform_continuous_on f s := uniform_continuous_on_iff_restrict.mpr (lipschitz_on_with_iff_restrict.mp hf).uniform_continuous protected lemma continuous_on (hf : lipschitz_on_with K f s) : continuous_on f s := hf.uniform_continuous_on.continuous_on lemma edist_lt_of_edist_lt_div (hf : lipschitz_on_with K f s) {x y : Ξ±} (hx : x ∈ s) (hy : y ∈ s) {d : ℝβ‰₯0∞} (hd : edist x y < d / K) : edist (f x) (f y) < d := (lipschitz_on_with_iff_restrict.mp hf).edist_lt_of_edist_lt_div $ show edist (⟨x, hx⟩ : s) ⟨y, hy⟩ < d / K, from hd protected lemma comp {g : Ξ² β†’ Ξ³} {t : set Ξ²} {Kg : ℝβ‰₯0} (hg : lipschitz_on_with Kg g t) (hf : lipschitz_on_with K f s) (hmaps : maps_to f s t) : lipschitz_on_with (Kg * K) (g ∘ f) s := lipschitz_on_with_iff_restrict.mpr $ hg.to_restrict.comp (hf.to_restrict_maps_to hmaps) end emetric section metric variables [pseudo_metric_space Ξ±] [pseudo_metric_space Ξ²] [pseudo_metric_space Ξ³] variables {K : ℝβ‰₯0} {s : set Ξ±} {f : Ξ± β†’ Ξ²} protected lemma of_dist_le' {K : ℝ} (h : βˆ€ (x ∈ s) (y ∈ s), dist (f x) (f y) ≀ K * dist x y) : lipschitz_on_with (real.to_nnreal K) f s := of_dist_le_mul $ Ξ» x hx y hy, le_trans (h x hx y hy) $ mul_le_mul_of_nonneg_right (real.le_coe_to_nnreal K) dist_nonneg protected lemma mk_one (h : βˆ€ (x ∈ s) (y ∈ s), dist (f x) (f y) ≀ dist x y) : lipschitz_on_with 1 f s := of_dist_le_mul $ by simpa only [nnreal.coe_one, one_mul] using h /-- For functions to `ℝ`, it suffices to prove `f x ≀ f y + K * dist x y`; this version doesn't assume `0≀K`. -/ protected lemma of_le_add_mul' {f : Ξ± β†’ ℝ} (K : ℝ) (h : βˆ€ (x ∈ s) (y ∈ s), f x ≀ f y + K * dist x y) : lipschitz_on_with (real.to_nnreal K) f s := have I : βˆ€ (x ∈ s) (y ∈ s), f x - f y ≀ K * dist x y, from assume x hx y hy, sub_le_iff_le_add'.2 (h x hx y hy), lipschitz_on_with.of_dist_le' $ assume x hx y hy, abs_sub_le_iff.2 ⟨I x hx y hy, dist_comm y x β–Έ I y hy x hx⟩ /-- For functions to `ℝ`, it suffices to prove `f x ≀ f y + K * dist x y`; this version assumes `0≀K`. -/ protected lemma of_le_add_mul {f : Ξ± β†’ ℝ} (K : ℝβ‰₯0) (h : βˆ€ (x ∈ s) (y ∈ s), f x ≀ f y + K * dist x y) : lipschitz_on_with K f s := by simpa only [real.to_nnreal_coe] using lipschitz_on_with.of_le_add_mul' K h protected lemma of_le_add {f : Ξ± β†’ ℝ} (h : βˆ€ (x ∈ s) (y ∈ s), f x ≀ f y + dist x y) : lipschitz_on_with 1 f s := lipschitz_on_with.of_le_add_mul 1 $ by simpa only [nnreal.coe_one, one_mul] protected lemma le_add_mul {f : Ξ± β†’ ℝ} {K : ℝβ‰₯0} (h : lipschitz_on_with K f s) {x : Ξ±} (hx : x ∈ s) {y : Ξ±} (hy : y ∈ s) : f x ≀ f y + K * dist x y := sub_le_iff_le_add'.1 $ le_trans (le_abs_self _) $ h.dist_le_mul x hx y hy protected lemma iff_le_add_mul {f : Ξ± β†’ ℝ} {K : ℝβ‰₯0} : lipschitz_on_with K f s ↔ βˆ€ (x ∈ s) (y ∈ s), f x ≀ f y + K * dist x y := ⟨lipschitz_on_with.le_add_mul, lipschitz_on_with.of_le_add_mul K⟩ end metric end lipschitz_on_with /-- Consider a function `f : Ξ± Γ— Ξ² β†’ Ξ³`. Suppose that it is continuous on each β€œvertical fiber” `{a} Γ— t`, `a ∈ s`, and is Lipschitz continuous on each β€œhorizontal fiber” `s Γ— {b}`, `b ∈ t` with the same Lipschitz constant `K`. Then it is continuous on `s Γ— t`. The actual statement uses (Lipschitz) continuity of `Ξ» y, f (a, y)` and `Ξ» x, f (x, b)` instead of continuity of `f` on subsets of the product space. -/ lemma continuous_on_prod_of_continuous_on_lipschitz_on [pseudo_emetric_space Ξ±] [topological_space Ξ²] [pseudo_emetric_space Ξ³] (f : Ξ± Γ— Ξ² β†’ Ξ³) {s : set Ξ±} {t : set Ξ²} (K : ℝβ‰₯0) (ha : βˆ€ a ∈ s, continuous_on (Ξ» y, f (a, y)) t) (hb : βˆ€ b ∈ t, lipschitz_on_with K (Ξ» x, f (x, b)) s) : continuous_on f (s Γ—Λ’ t) := begin rintro ⟨x, y⟩ ⟨hx : x ∈ s, hy : y ∈ t⟩, refine emetric.tendsto_nhds.2 (Ξ» Ξ΅ (Ξ΅0 : 0 < Ξ΅), _), replace Ξ΅0 : 0 < Ξ΅ / 2 := ennreal.half_pos (ne_of_gt Ξ΅0), have Ξ΅K : 0 < Ξ΅ / 2 / K := ennreal.div_pos_iff.2 ⟨Ρ0.ne', ennreal.coe_ne_top⟩, have A : s ∩ emetric.ball x (Ξ΅ / 2 / K) ∈ 𝓝[s] x := inter_mem_nhds_within _ (emetric.ball_mem_nhds _ Ξ΅K), have B : {b : Ξ² | b ∈ t ∧ edist (f (x, b)) (f (x, y)) < Ξ΅ / 2} ∈ 𝓝[t] y := inter_mem self_mem_nhds_within (ha x hx y hy (emetric.ball_mem_nhds _ Ξ΅0)), filter_upwards [nhds_within_prod A B], rintro ⟨a, b⟩ ⟨⟨has : a ∈ s, hax : edist a x < Ξ΅ / 2 / K⟩, hbt : b ∈ t, hby : edist (f (x, b)) (f (x, y)) < Ξ΅ / 2⟩, calc edist (f (a, b)) (f (x, y)) ≀ edist (f (a, b)) (f (x, b)) + edist (f (x, b)) (f (x, y)) : edist_triangle _ _ _ ... < Ξ΅ / 2 + Ξ΅ / 2 : ennreal.add_lt_add ((hb _ hbt).edist_lt_of_edist_lt_div has hx hax) hby ... = Ξ΅ : ennreal.add_halves Ξ΅ end /-- Consider a function `f : Ξ± Γ— Ξ² β†’ Ξ³`. Suppose that it is continuous on each β€œvertical section” `{a} Γ— univ`, `a : Ξ±`, and is Lipschitz continuous on each β€œhorizontal section” `univ Γ— {b}`, `b : Ξ²` with the same Lipschitz constant `K`. Then it is continuous. The actual statement uses (Lipschitz) continuity of `Ξ» y, f (a, y)` and `Ξ» x, f (x, b)` instead of continuity of `f` on subsets of the product space. -/ lemma continuous_prod_of_continuous_lipschitz [pseudo_emetric_space Ξ±] [topological_space Ξ²] [pseudo_emetric_space Ξ³] (f : Ξ± Γ— Ξ² β†’ Ξ³) (K : ℝβ‰₯0) (ha : βˆ€ a, continuous (Ξ» y, f (a, y))) (hb : βˆ€ b, lipschitz_with K (Ξ» x, f (x, b))) : continuous f := begin simp only [continuous_iff_continuous_on_univ, ← univ_prod_univ, ← lipschitz_on_univ] at *, exact continuous_on_prod_of_continuous_on_lipschitz_on f K (Ξ» a _, ha a) (Ξ» b _, hb b) end open metric /-- If a function is locally Lipschitz around a point, then it is continuous at this point. -/ lemma continuous_at_of_locally_lipschitz [pseudo_metric_space Ξ±] [pseudo_metric_space Ξ²] {f : Ξ± β†’ Ξ²} {x : Ξ±} {r : ℝ} (hr : 0 < r) (K : ℝ) (h : βˆ€ y, dist y x < r β†’ dist (f y) (f x) ≀ K * dist y x) : continuous_at f x := begin -- We use `h` to squeeze `dist (f y) (f x)` between `0` and `K * dist y x` refine tendsto_iff_dist_tendsto_zero.2 (squeeze_zero' (eventually_of_forall $ Ξ» _, dist_nonneg) (mem_of_superset (ball_mem_nhds _ hr) h) _), -- Then show that `K * dist y x` tends to zero as `y β†’ x` refine (continuous_const.mul (continuous_id.dist continuous_const)).tendsto' _ _ _, simp end /-- A function `f : Ξ± β†’ ℝ` which is `K`-Lipschitz on a subset `s` admits a `K`-Lipschitz extension to the whole space. -/ lemma lipschitz_on_with.extend_real [pseudo_metric_space Ξ±] {f : Ξ± β†’ ℝ} {s : set Ξ±} {K : ℝβ‰₯0} (hf : lipschitz_on_with K f s) : βˆƒ g : Ξ± β†’ ℝ, lipschitz_with K g ∧ eq_on f g s := begin /- An extension is given by `g y = Inf {f x + K * dist y x | x ∈ s}`. Taking `x = y`, one has `g y ≀ f y` for `y ∈ s`, and the other inequality holds because `f` is `K`-Lipschitz, so that it can not counterbalance the growth of `K * dist y x`. One readily checks from the formula that the extended function is also `K`-Lipschitz. -/ rcases eq_empty_or_nonempty s with rfl|hs, { exact ⟨λ x, 0, (lipschitz_with.const _).weaken (zero_le _), eq_on_empty _ _⟩ }, haveI : nonempty s, by simp only [hs, nonempty_coe_sort], let g := Ξ» (y : Ξ±), infi (Ξ» (x : s), f x + K * dist y x), have B : βˆ€ (y : Ξ±), bdd_below (range (Ξ» (x : s), f x + K * dist y x)), { assume y, rcases hs with ⟨z, hz⟩, refine ⟨f z - K * dist y z, _⟩, rintros w ⟨t, rfl⟩, dsimp, rw [sub_le_iff_le_add, add_assoc, ← mul_add, add_comm (dist y t)], calc f z ≀ f t + K * dist z t : hf.le_add_mul hz t.2 ... ≀ f t + K * (dist y z + dist y t) : add_le_add_left (mul_le_mul_of_nonneg_left (dist_triangle_left _ _ _) K.2) _ }, have E : eq_on f g s, { assume x hx, refine le_antisymm (le_cinfi (Ξ» y, hf.le_add_mul hx y.2)) _, simpa only [add_zero, subtype.coe_mk, mul_zero, dist_self] using cinfi_le (B x) ⟨x, hx⟩ }, refine ⟨g, lipschitz_with.of_le_add_mul K (Ξ» x y, _), E⟩, rw ← sub_le_iff_le_add, refine le_cinfi (Ξ» z, _), rw sub_le_iff_le_add, calc g x ≀ f z + K * dist x z : cinfi_le (B x) _ ... ≀ f z + K * dist y z + K * dist x y : begin rw [add_assoc, ← mul_add, add_comm (dist y z)], exact add_le_add_left (mul_le_mul_of_nonneg_left (dist_triangle _ _ _) K.2) _, end end /-- A function `f : Ξ± β†’ (ΞΉ β†’ ℝ)` which is `K`-Lipschitz on a subset `s` admits a `K`-Lipschitz extension to the whole space. TODO: state the same result (with the same proof) for the space `β„“^∞ (ΞΉ, ℝ)` over a possibly infinite type `ΞΉ`. -/ lemma lipschitz_on_with.extend_pi [pseudo_metric_space Ξ±] [fintype ΞΉ] {f : Ξ± β†’ (ΞΉ β†’ ℝ)} {s : set Ξ±} {K : ℝβ‰₯0} (hf : lipschitz_on_with K f s) : βˆƒ g : Ξ± β†’ (ΞΉ β†’ ℝ), lipschitz_with K g ∧ eq_on f g s := begin have : βˆ€ i, βˆƒ g : Ξ± β†’ ℝ, lipschitz_with K g ∧ eq_on (Ξ» x, f x i) g s, { assume i, have : lipschitz_on_with K (Ξ» (x : Ξ±), f x i) s, { apply lipschitz_on_with.of_dist_le_mul (Ξ» x hx y hy, _), exact (dist_le_pi_dist _ _ i).trans (hf.dist_le_mul x hx y hy) }, exact this.extend_real }, choose g hg using this, refine ⟨λ x i, g i x, lipschitz_with.of_dist_le_mul (Ξ» x y, _), _⟩, { exact (dist_pi_le_iff (mul_nonneg K.2 dist_nonneg)).2 (Ξ» i, (hg i).1.dist_le_mul x y) }, { assume x hx, ext1 i, exact (hg i).2 hx } end
616050f877a68da4298fee1bd14b0d4a273ebf5c
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/topology/algebra/infinite_sum.lean
5b34e3e3454fcd63e252f479abbee02ed9b64cf1
[ "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
68,328
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import algebra.big_operators.intervals import algebra.big_operators.nat_antidiagonal import logic.encodable.lattice import topology.algebra.mul_action import topology.algebra.order.monotone_convergence import topology.instances.real /-! # Infinite sum over a topological monoid This sum is known as unconditionally convergent, as it sums to the same value under all possible permutations. For Euclidean spaces (finite dimensional Banach spaces) this is equivalent to absolute convergence. Note: There are summable sequences which are not unconditionally convergent! The other way holds generally, see `has_sum.tendsto_sum_nat`. ## References * Bourbaki: General Topology (1995), Chapter 3 Β§5 (Infinite sums in commutative groups) -/ noncomputable theory open finset filter function classical open_locale topological_space classical big_operators nnreal variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} {Ξ΄ : Type*} section has_sum variables [add_comm_monoid Ξ±] [topological_space Ξ±] /-- Infinite sum on a topological monoid The `at_top` filter on `finset Ξ²` is the limit of all finite sets towards the entire type. So we sum up bigger and bigger sets. This sum operation is invariant under reordering. In particular, the function `β„• β†’ ℝ` sending `n` to `(-1)^n / (n+1)` does not have a sum for this definition, but a series which is absolutely convergent will have the correct sum. This is based on Mario Carneiro's [infinite sum `df-tsms` in Metamath](http://us.metamath.org/mpeuni/df-tsms.html). For the definition or many statements, `Ξ±` does not need to be a topological monoid. We only add this assumption later, for the lemmas where it is relevant. -/ def has_sum (f : Ξ² β†’ Ξ±) (a : Ξ±) : Prop := tendsto (Ξ»s:finset Ξ², βˆ‘ b in s, f b) at_top (𝓝 a) /-- `summable f` means that `f` has some (infinite) sum. Use `tsum` to get the value. -/ def summable (f : Ξ² β†’ Ξ±) : Prop := βˆƒa, has_sum f a /-- `βˆ‘' i, f i` is the sum of `f` it exists, or 0 otherwise -/ @[irreducible] def tsum {Ξ²} (f : Ξ² β†’ Ξ±) := if h : summable f then classical.some h else 0 -- see Note [operator precedence of big operators] notation `βˆ‘'` binders `, ` r:(scoped:67 f, tsum f) := r variables {f g : Ξ² β†’ Ξ±} {a b : Ξ±} {s : finset Ξ²} lemma summable.has_sum (ha : summable f) : has_sum f (βˆ‘'b, f b) := by simp [ha, tsum]; exact some_spec ha lemma has_sum.summable (h : has_sum f a) : summable f := ⟨a, h⟩ /-- Constant zero function has sum `0` -/ lemma has_sum_zero : has_sum (Ξ»b, 0 : Ξ² β†’ Ξ±) 0 := by simp [has_sum, tendsto_const_nhds] lemma has_sum_empty [is_empty Ξ²] : has_sum f 0 := by convert has_sum_zero lemma summable_zero : summable (Ξ»b, 0 : Ξ² β†’ Ξ±) := has_sum_zero.summable lemma summable_empty [is_empty Ξ²] : summable f := has_sum_empty.summable lemma tsum_eq_zero_of_not_summable (h : Β¬ summable f) : βˆ‘'b, f b = 0 := by simp [tsum, h] lemma summable_congr (hfg : βˆ€b, f b = g b) : summable f ↔ summable g := iff_of_eq (congr_arg summable $ funext hfg) lemma summable.congr (hf : summable f) (hfg : βˆ€b, f b = g b) : summable g := (summable_congr hfg).mp hf lemma has_sum.has_sum_of_sum_eq {g : Ξ³ β†’ Ξ±} (h_eq : βˆ€u:finset Ξ³, βˆƒv:finset Ξ², βˆ€v', v βŠ† v' β†’ βˆƒu', u βŠ† u' ∧ βˆ‘ x in u', g x = βˆ‘ b in v', f b) (hf : has_sum g a) : has_sum f a := le_trans (map_at_top_finset_sum_le_of_sum_eq h_eq) hf lemma has_sum_iff_has_sum {g : Ξ³ β†’ Ξ±} (h₁ : βˆ€u:finset Ξ³, βˆƒv:finset Ξ², βˆ€v', v βŠ† v' β†’ βˆƒu', u βŠ† u' ∧ βˆ‘ x in u', g x = βˆ‘ b in v', f b) (hβ‚‚ : βˆ€v:finset Ξ², βˆƒu:finset Ξ³, βˆ€u', u βŠ† u' β†’ βˆƒv', v βŠ† v' ∧ βˆ‘ b in v', f b = βˆ‘ x in u', g x) : has_sum f a ↔ has_sum g a := ⟨has_sum.has_sum_of_sum_eq hβ‚‚, has_sum.has_sum_of_sum_eq hβ‚βŸ© lemma function.injective.has_sum_iff {g : Ξ³ β†’ Ξ²} (hg : injective g) (hf : βˆ€ x βˆ‰ set.range g, f x = 0) : has_sum (f ∘ g) a ↔ has_sum f a := by simp only [has_sum, tendsto, hg.map_at_top_finset_sum_eq hf] lemma function.injective.summable_iff {g : Ξ³ β†’ Ξ²} (hg : injective g) (hf : βˆ€ x βˆ‰ set.range g, f x = 0) : summable (f ∘ g) ↔ summable f := exists_congr $ Ξ» _, hg.has_sum_iff hf lemma has_sum_subtype_iff_of_support_subset {s : set Ξ²} (hf : support f βŠ† s) : has_sum (f ∘ coe : s β†’ Ξ±) a ↔ has_sum f a := subtype.coe_injective.has_sum_iff $ by simpa using support_subset_iff'.1 hf lemma has_sum_subtype_iff_indicator {s : set Ξ²} : has_sum (f ∘ coe : s β†’ Ξ±) a ↔ has_sum (s.indicator f) a := by rw [← set.indicator_range_comp, subtype.range_coe, has_sum_subtype_iff_of_support_subset set.support_indicator_subset] lemma summable_subtype_iff_indicator {s : set Ξ²} : summable (f ∘ coe : s β†’ Ξ±) ↔ summable (s.indicator f) := exists_congr (Ξ» _, has_sum_subtype_iff_indicator) @[simp] lemma has_sum_subtype_support : has_sum (f ∘ coe : support f β†’ Ξ±) a ↔ has_sum f a := has_sum_subtype_iff_of_support_subset $ set.subset.refl _ lemma has_sum_fintype [fintype Ξ²] (f : Ξ² β†’ Ξ±) : has_sum f (βˆ‘ b, f b) := order_top.tendsto_at_top_nhds _ protected lemma finset.has_sum (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : has_sum (f ∘ coe : (↑s : set Ξ²) β†’ Ξ±) (βˆ‘ b in s, f b) := by { rw ← sum_attach, exact has_sum_fintype _ } protected lemma finset.summable (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : summable (f ∘ coe : (↑s : set Ξ²) β†’ Ξ±) := (s.has_sum f).summable protected lemma set.finite.summable {s : set Ξ²} (hs : s.finite) (f : Ξ² β†’ Ξ±) : summable (f ∘ coe : s β†’ Ξ±) := by convert hs.to_finset.summable f; simp only [hs.coe_to_finset] /-- If a function `f` vanishes outside of a finite set `s`, then it `has_sum` `βˆ‘ b in s, f b`. -/ lemma has_sum_sum_of_ne_finset_zero (hf : βˆ€bβˆ‰s, f b = 0) : has_sum f (βˆ‘ b in s, f b) := (has_sum_subtype_iff_of_support_subset $ support_subset_iff'.2 hf).1 $ s.has_sum f lemma summable_of_ne_finset_zero (hf : βˆ€bβˆ‰s, f b = 0) : summable f := (has_sum_sum_of_ne_finset_zero hf).summable lemma has_sum_single {f : Ξ² β†’ Ξ±} (b : Ξ²) (hf : βˆ€b' β‰  b, f b' = 0) : has_sum f (f b) := suffices has_sum f (βˆ‘ b' in {b}, f b'), by simpa using this, has_sum_sum_of_ne_finset_zero $ by simpa [hf] lemma has_sum_ite_eq (b : Ξ²) [decidable_pred (= b)] (a : Ξ±) : has_sum (Ξ»b', if b' = b then a else 0) a := begin convert has_sum_single b _, { exact (if_pos rfl).symm }, assume b' hb', exact if_neg hb' end lemma equiv.has_sum_iff (e : Ξ³ ≃ Ξ²) : has_sum (f ∘ e) a ↔ has_sum f a := e.injective.has_sum_iff $ by simp lemma function.injective.has_sum_range_iff {g : Ξ³ β†’ Ξ²} (hg : injective g) : has_sum (Ξ» x : set.range g, f x) a ↔ has_sum (f ∘ g) a := (equiv.of_injective g hg).has_sum_iff.symm lemma equiv.summable_iff (e : Ξ³ ≃ Ξ²) : summable (f ∘ e) ↔ summable f := exists_congr $ Ξ» a, e.has_sum_iff lemma summable.prod_symm {f : Ξ² Γ— Ξ³ β†’ Ξ±} (hf : summable f) : summable (Ξ» p : Ξ³ Γ— Ξ², f p.swap) := (equiv.prod_comm Ξ³ Ξ²).summable_iff.2 hf lemma equiv.has_sum_iff_of_support {g : Ξ³ β†’ Ξ±} (e : support f ≃ support g) (he : βˆ€ x : support f, g (e x) = f x) : has_sum f a ↔ has_sum g a := have (g ∘ coe) ∘ e = f ∘ coe, from funext he, by rw [← has_sum_subtype_support, ← this, e.has_sum_iff, has_sum_subtype_support] lemma has_sum_iff_has_sum_of_ne_zero_bij {g : Ξ³ β†’ Ξ±} (i : support g β†’ Ξ²) (hi : βˆ€ ⦃x y⦄, i x = i y β†’ (x : Ξ³) = y) (hf : support f βŠ† set.range i) (hfg : βˆ€ x, f (i x) = g x) : has_sum f a ↔ has_sum g a := iff.symm $ equiv.has_sum_iff_of_support (equiv.of_bijective (Ξ» x, ⟨i x, Ξ» hx, x.coe_prop $ hfg x β–Έ hx⟩) ⟨λ x y h, subtype.ext $ hi $ subtype.ext_iff.1 h, Ξ» y, (hf y.coe_prop).imp $ Ξ» x hx, subtype.ext hx⟩) hfg lemma equiv.summable_iff_of_support {g : Ξ³ β†’ Ξ±} (e : support f ≃ support g) (he : βˆ€ x : support f, g (e x) = f x) : summable f ↔ summable g := exists_congr $ Ξ» _, e.has_sum_iff_of_support he protected lemma has_sum.map [add_comm_monoid Ξ³] [topological_space Ξ³] (hf : has_sum f a) {G} [add_monoid_hom_class G Ξ± Ξ³] (g : G) (hg : continuous g) : has_sum (g ∘ f) (g a) := have g ∘ (Ξ»s:finset Ξ², βˆ‘ b in s, f b) = (Ξ»s:finset Ξ², βˆ‘ b in s, g (f b)), from funext $ map_sum g _, show tendsto (Ξ»s:finset Ξ², βˆ‘ b in s, g (f b)) at_top (𝓝 (g a)), from this β–Έ (hg.tendsto a).comp hf protected lemma summable.map [add_comm_monoid Ξ³] [topological_space Ξ³] (hf : summable f) {G} [add_monoid_hom_class G Ξ± Ξ³] (g : G) (hg : continuous g) : summable (g ∘ f) := (hf.has_sum.map g hg).summable protected lemma summable.map_iff_of_left_inverse [add_comm_monoid Ξ³] [topological_space Ξ³] {G G'} [add_monoid_hom_class G Ξ± Ξ³] [add_monoid_hom_class G' Ξ³ Ξ±] (g : G) (g' : G') (hg : continuous g) (hg' : continuous g') (hinv : function.left_inverse g' g) : summable (g ∘ f) ↔ summable f := ⟨λ h, begin have := h.map _ hg', rwa [←function.comp.assoc, hinv.id] at this, end, Ξ» h, h.map _ hg⟩ /-- A special case of `summable.map_iff_of_left_inverse` for convenience -/ protected lemma summable.map_iff_of_equiv [add_comm_monoid Ξ³] [topological_space Ξ³] {G} [add_equiv_class G Ξ± Ξ³] (g : G) (hg : continuous g) (hg' : continuous (add_equiv_class.inv g : Ξ³ β†’ Ξ±)) : summable (g ∘ f) ↔ summable f := summable.map_iff_of_left_inverse g (g : Ξ± ≃+ Ξ³).symm hg hg' (add_equiv_class.left_inv g) /-- If `f : β„• β†’ Ξ±` has sum `a`, then the partial sums `βˆ‘_{i=0}^{n-1} f i` converge to `a`. -/ lemma has_sum.tendsto_sum_nat {f : β„• β†’ Ξ±} (h : has_sum f a) : tendsto (Ξ»n:β„•, βˆ‘ i in range n, f i) at_top (𝓝 a) := h.comp tendsto_finset_range lemma has_sum.unique {a₁ aβ‚‚ : Ξ±} [t2_space Ξ±] : has_sum f a₁ β†’ has_sum f aβ‚‚ β†’ a₁ = aβ‚‚ := tendsto_nhds_unique lemma summable.has_sum_iff_tendsto_nat [t2_space Ξ±] {f : β„• β†’ Ξ±} {a : Ξ±} (hf : summable f) : has_sum f a ↔ tendsto (Ξ»n:β„•, βˆ‘ i in range n, f i) at_top (𝓝 a) := begin refine ⟨λ h, h.tendsto_sum_nat, Ξ» h, _⟩, rw tendsto_nhds_unique h hf.has_sum.tendsto_sum_nat, exact hf.has_sum end lemma function.surjective.summable_iff_of_has_sum_iff {Ξ±' : Type*} [add_comm_monoid Ξ±'] [topological_space Ξ±'] {e : Ξ±' β†’ Ξ±} (hes : function.surjective e) {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ±'} (he : βˆ€ {a}, has_sum f (e a) ↔ has_sum g a) : summable f ↔ summable g := hes.exists.trans $ exists_congr $ @he section mul_opposite open mul_opposite lemma has_sum.op (hf : has_sum f a) : has_sum (Ξ» a, op (f a)) (op a) := (hf.map (@op_add_equiv Ξ± _) continuous_op : _) lemma summable.op (hf : summable f) : summable (op ∘ f) := hf.has_sum.op.summable lemma has_sum.unop {f : Ξ² β†’ αᡐᡒᡖ} {a : αᡐᡒᡖ} (hf : has_sum f a) : has_sum (Ξ» a, unop (f a)) (unop a) := (hf.map (@op_add_equiv Ξ± _).symm continuous_unop : _) lemma summable.unop {f : Ξ² β†’ αᡐᡒᡖ} (hf : summable f) : summable (unop ∘ f) := hf.has_sum.unop.summable @[simp] lemma has_sum_op : has_sum (Ξ» a, op (f a)) (op a) ↔ has_sum f a := ⟨has_sum.unop, has_sum.op⟩ @[simp] lemma has_sum_unop {f : Ξ² β†’ αᡐᡒᡖ} {a : αᡐᡒᡖ} : has_sum (Ξ» a, unop (f a)) (unop a) ↔ has_sum f a := ⟨has_sum.op, has_sum.unop⟩ @[simp] lemma summable_op : summable (Ξ» a, op (f a)) ↔ summable f := ⟨summable.unop, summable.op⟩ @[simp] lemma summable_unop {f : Ξ² β†’ αᡐᡒᡖ} : summable (Ξ» a, unop (f a)) ↔ summable f := ⟨summable.op, summable.unop⟩ end mul_opposite section has_continuous_star variables [star_add_monoid Ξ±] [has_continuous_star Ξ±] lemma has_sum.star (h : has_sum f a) : has_sum (Ξ» b, star (f b)) (star a) := by simpa only using h.map (star_add_equiv : Ξ± ≃+ Ξ±) continuous_star lemma summable.star (hf : summable f) : summable (Ξ» b, star (f b)) := hf.has_sum.star.summable lemma summable.of_star (hf : summable (Ξ» b, star (f b))) : summable f := by simpa only [star_star] using hf.star @[simp] lemma summable_star_iff : summable (Ξ» b, star (f b)) ↔ summable f := ⟨summable.of_star, summable.star⟩ @[simp] lemma summable_star_iff' : summable (star f) ↔ summable f := summable_star_iff end has_continuous_star variable [has_continuous_add Ξ±] lemma has_sum.add (hf : has_sum f a) (hg : has_sum g b) : has_sum (Ξ»b, f b + g b) (a + b) := by simp only [has_sum, sum_add_distrib]; exact hf.add hg lemma summable.add (hf : summable f) (hg : summable g) : summable (Ξ»b, f b + g b) := (hf.has_sum.add hg.has_sum).summable lemma has_sum_sum {f : Ξ³ β†’ Ξ² β†’ Ξ±} {a : Ξ³ β†’ Ξ±} {s : finset Ξ³} : (βˆ€i∈s, has_sum (f i) (a i)) β†’ has_sum (Ξ»b, βˆ‘ i in s, f i b) (βˆ‘ i in s, a i) := finset.induction_on s (by simp only [has_sum_zero, sum_empty, forall_true_iff]) (by simp only [has_sum.add, sum_insert, mem_insert, forall_eq_or_imp, forall_2_true_iff, not_false_iff, forall_true_iff] {contextual := tt}) lemma summable_sum {f : Ξ³ β†’ Ξ² β†’ Ξ±} {s : finset Ξ³} (hf : βˆ€i∈s, summable (f i)) : summable (Ξ»b, βˆ‘ i in s, f i b) := (has_sum_sum $ assume i hi, (hf i hi).has_sum).summable lemma has_sum.add_disjoint {s t : set Ξ²} (hs : disjoint s t) (ha : has_sum (f ∘ coe : s β†’ Ξ±) a) (hb : has_sum (f ∘ coe : t β†’ Ξ±) b) : has_sum (f ∘ coe : s βˆͺ t β†’ Ξ±) (a + b) := begin rw has_sum_subtype_iff_indicator at *, rw set.indicator_union_of_disjoint hs, exact ha.add hb end lemma has_sum.add_is_compl {s t : set Ξ²} (hs : is_compl s t) (ha : has_sum (f ∘ coe : s β†’ Ξ±) a) (hb : has_sum (f ∘ coe : t β†’ Ξ±) b) : has_sum f (a + b) := by simpa [← hs.compl_eq] using (has_sum_subtype_iff_indicator.1 ha).add (has_sum_subtype_iff_indicator.1 hb) lemma has_sum.add_compl {s : set Ξ²} (ha : has_sum (f ∘ coe : s β†’ Ξ±) a) (hb : has_sum (f ∘ coe : sᢜ β†’ Ξ±) b) : has_sum f (a + b) := ha.add_is_compl is_compl_compl hb lemma summable.add_compl {s : set Ξ²} (hs : summable (f ∘ coe : s β†’ Ξ±)) (hsc : summable (f ∘ coe : sᢜ β†’ Ξ±)) : summable f := (hs.has_sum.add_compl hsc.has_sum).summable lemma has_sum.compl_add {s : set Ξ²} (ha : has_sum (f ∘ coe : sᢜ β†’ Ξ±) a) (hb : has_sum (f ∘ coe : s β†’ Ξ±) b) : has_sum f (a + b) := ha.add_is_compl is_compl_compl.symm hb lemma has_sum.even_add_odd {f : β„• β†’ Ξ±} (he : has_sum (Ξ» k, f (2 * k)) a) (ho : has_sum (Ξ» k, f (2 * k + 1)) b) : has_sum f (a + b) := begin have := mul_right_injectiveβ‚€ (@two_ne_zero β„• _ _), replace he := this.has_sum_range_iff.2 he, replace ho := ((add_left_injective 1).comp this).has_sum_range_iff.2 ho, refine he.add_is_compl _ ho, simpa [(∘)] using nat.is_compl_even_odd end lemma summable.compl_add {s : set Ξ²} (hs : summable (f ∘ coe : sᢜ β†’ Ξ±)) (hsc : summable (f ∘ coe : s β†’ Ξ±)) : summable f := (hs.has_sum.compl_add hsc.has_sum).summable lemma summable.even_add_odd {f : β„• β†’ Ξ±} (he : summable (Ξ» k, f (2 * k))) (ho : summable (Ξ» k, f (2 * k + 1))) : summable f := (he.has_sum.even_add_odd ho.has_sum).summable lemma has_sum.sigma [t3_space Ξ±] {Ξ³ : Ξ² β†’ Type*} {f : (Ξ£ b:Ξ², Ξ³ b) β†’ Ξ±} {g : Ξ² β†’ Ξ±} {a : Ξ±} (ha : has_sum f a) (hf : βˆ€b, has_sum (Ξ»c, f ⟨b, c⟩) (g b)) : has_sum g a := begin refine (at_top_basis.tendsto_iff (closed_nhds_basis a)).mpr _, rintros s ⟨hs, hsc⟩, rcases mem_at_top_sets.mp (ha hs) with ⟨u, hu⟩, use [u.image sigma.fst, trivial], intros bs hbs, simp only [set.mem_preimage, ge_iff_le, finset.le_iff_subset] at hu, have : tendsto (Ξ» t : finset (Ξ£ b, Ξ³ b), βˆ‘ p in t.filter (Ξ» p, p.1 ∈ bs), f p) at_top (𝓝 $ βˆ‘ b in bs, g b), { simp only [← sigma_preimage_mk, sum_sigma], refine tendsto_finset_sum _ (Ξ» b hb, _), change tendsto (Ξ» t, (Ξ» t, βˆ‘ s in t, f ⟨b, s⟩) (preimage t (sigma.mk b) _)) at_top (𝓝 (g b)), exact tendsto.comp (hf b) (tendsto_finset_preimage_at_top_at_top _) }, refine hsc.mem_of_tendsto this (eventually_at_top.2 ⟨u, Ξ» t ht, hu _ (Ξ» x hx, _)⟩), exact mem_filter.2 ⟨ht hx, hbs $ mem_image_of_mem _ hx⟩ end /-- If a series `f` on `Ξ² Γ— Ξ³` has sum `a` and for each `b` the restriction of `f` to `{b} Γ— Ξ³` has sum `g b`, then the series `g` has sum `a`. -/ lemma has_sum.prod_fiberwise [t3_space Ξ±] {f : Ξ² Γ— Ξ³ β†’ Ξ±} {g : Ξ² β†’ Ξ±} {a : Ξ±} (ha : has_sum f a) (hf : βˆ€b, has_sum (Ξ»c, f (b, c)) (g b)) : has_sum g a := has_sum.sigma ((equiv.sigma_equiv_prod Ξ² Ξ³).has_sum_iff.2 ha) hf lemma summable.sigma' [t3_space Ξ±] {Ξ³ : Ξ² β†’ Type*} {f : (Ξ£b:Ξ², Ξ³ b) β†’ Ξ±} (ha : summable f) (hf : βˆ€b, summable (Ξ»c, f ⟨b, c⟩)) : summable (Ξ»b, βˆ‘'c, f ⟨b, c⟩) := (ha.has_sum.sigma (assume b, (hf b).has_sum)).summable lemma has_sum.sigma_of_has_sum [t3_space Ξ±] {Ξ³ : Ξ² β†’ Type*} {f : (Ξ£ b:Ξ², Ξ³ b) β†’ Ξ±} {g : Ξ² β†’ Ξ±} {a : Ξ±} (ha : has_sum g a) (hf : βˆ€b, has_sum (Ξ»c, f ⟨b, c⟩) (g b)) (hf' : summable f) : has_sum f a := by simpa [(hf'.has_sum.sigma hf).unique ha] using hf'.has_sum end has_sum section tsum variables [add_comm_monoid Ξ±] [topological_space Ξ±] lemma tsum_congr_subtype (f : Ξ² β†’ Ξ±) {s t : set Ξ²} (h : s = t) : βˆ‘' (x : s), f x = βˆ‘' (x : t), f x := by rw h lemma tsum_zero' (hz : is_closed ({0} : set Ξ±)) : βˆ‘' b : Ξ², (0 : Ξ±) = 0 := begin classical, rw [tsum, dif_pos summable_zero], suffices : βˆ€ (x : Ξ±), has_sum (Ξ» (b : Ξ²), (0 : Ξ±)) x β†’ x = 0, { exact this _ (classical.some_spec _) }, intros x hx, contrapose! hx, simp only [has_sum, tendsto_nhds, finset.sum_const_zero, filter.mem_at_top_sets, ge_iff_le, finset.le_eq_subset, set.mem_preimage, not_forall, not_exists, exists_prop, exists_and_distrib_right], refine ⟨{0}ᢜ, ⟨is_open_compl_iff.mpr hz, _⟩, Ξ» y, ⟨⟨y, subset_refl _⟩, _⟩⟩, { simpa using hx }, { simp } end @[simp] lemma tsum_zero [t1_space Ξ±] : βˆ‘' b : Ξ², (0 : Ξ±) = 0 := tsum_zero' is_closed_singleton variables [t2_space Ξ±] {f g : Ξ² β†’ Ξ±} {a a₁ aβ‚‚ : Ξ±} lemma has_sum.tsum_eq (ha : has_sum f a) : βˆ‘'b, f b = a := (summable.has_sum ⟨a, ha⟩).unique ha lemma summable.has_sum_iff (h : summable f) : has_sum f a ↔ βˆ‘'b, f b = a := iff.intro has_sum.tsum_eq (assume eq, eq β–Έ h.has_sum) @[simp] lemma tsum_empty [is_empty Ξ²] : βˆ‘'b, f b = 0 := has_sum_empty.tsum_eq lemma tsum_eq_sum {f : Ξ² β†’ Ξ±} {s : finset Ξ²} (hf : βˆ€bβˆ‰s, f b = 0) : βˆ‘' b, f b = βˆ‘ b in s, f b := (has_sum_sum_of_ne_finset_zero hf).tsum_eq lemma tsum_congr {Ξ± Ξ² : Type*} [add_comm_monoid Ξ±] [topological_space Ξ±] {f g : Ξ² β†’ Ξ±} (hfg : βˆ€ b, f b = g b) : βˆ‘' b, f b = βˆ‘' b, g b := congr_arg tsum (funext hfg) lemma tsum_fintype [fintype Ξ²] (f : Ξ² β†’ Ξ±) : βˆ‘'b, f b = βˆ‘ b, f b := (has_sum_fintype f).tsum_eq lemma tsum_bool (f : bool β†’ Ξ±) : βˆ‘' i : bool, f i = f false + f true := by { rw [tsum_fintype, finset.sum_eq_add]; simp } @[simp] lemma finset.tsum_subtype (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : βˆ‘' x : {x // x ∈ s}, f x = βˆ‘ x in s, f x := (s.has_sum f).tsum_eq @[simp] lemma finset.tsum_subtype' (s : finset Ξ²) (f : Ξ² β†’ Ξ±) : βˆ‘' x : (s : set Ξ²), f x = βˆ‘ x in s, f x := s.tsum_subtype f lemma tsum_eq_single {f : Ξ² β†’ Ξ±} (b : Ξ²) (hf : βˆ€b' β‰  b, f b' = 0) : βˆ‘'b, f b = f b := (has_sum_single b hf).tsum_eq lemma tsum_tsum_eq_single (f : Ξ² β†’ Ξ³ β†’ Ξ±) (b : Ξ²) (c : Ξ³) (hfb : βˆ€ b' β‰  b, f b' c = 0) (hfc : βˆ€ (b' : Ξ²) (c' : Ξ³), c' β‰  c β†’ f b' c' = 0) : βˆ‘' b' c', f b' c' = f b c := calc βˆ‘' b' c', f b' c' = βˆ‘' b', f b' c : tsum_congr $ Ξ» b', tsum_eq_single _ (hfc b') ... = f b c : tsum_eq_single _ hfb @[simp] lemma tsum_ite_eq (b : Ξ²) [decidable_pred (= b)] (a : Ξ±) : βˆ‘' b', (if b' = b then a else 0) = a := (has_sum_ite_eq b a).tsum_eq lemma tsum_dite_right (P : Prop) [decidable P] (x : Ξ² β†’ Β¬ P β†’ Ξ±) : βˆ‘' (b : Ξ²), (if h : P then (0 : Ξ±) else x b h) = if h : P then (0 : Ξ±) else βˆ‘' (b : Ξ²), x b h := by by_cases hP : P; simp [hP] lemma tsum_dite_left (P : Prop) [decidable P] (x : Ξ² β†’ P β†’ Ξ±) : βˆ‘' (b : Ξ²), (if h : P then x b h else 0) = if h : P then (βˆ‘' (b : Ξ²), x b h) else 0 := by by_cases hP : P; simp [hP] lemma function.surjective.tsum_eq_tsum_of_has_sum_iff_has_sum {Ξ±' : Type*} [add_comm_monoid Ξ±'] [topological_space Ξ±'] {e : Ξ±' β†’ Ξ±} (hes : function.surjective e) (h0 : e 0 = 0) {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ±'} (h : βˆ€ {a}, has_sum f (e a) ↔ has_sum g a) : βˆ‘' b, f b = e (βˆ‘' c, g c) := by_cases (assume : summable g, (h.mpr this.has_sum).tsum_eq) (assume hg : Β¬ summable g, have hf : Β¬ summable f, from mt (hes.summable_iff_of_has_sum_iff @h).1 hg, by simp [tsum, hf, hg, h0]) lemma tsum_eq_tsum_of_has_sum_iff_has_sum {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ±} (h : βˆ€{a}, has_sum f a ↔ has_sum g a) : βˆ‘'b, f b = βˆ‘'c, g c := surjective_id.tsum_eq_tsum_of_has_sum_iff_has_sum rfl @h lemma equiv.tsum_eq (j : Ξ³ ≃ Ξ²) (f : Ξ² β†’ Ξ±) : βˆ‘'c, f (j c) = βˆ‘'b, f b := tsum_eq_tsum_of_has_sum_iff_has_sum $ Ξ» a, j.has_sum_iff lemma equiv.tsum_eq_tsum_of_support {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ±} (e : support f ≃ support g) (he : βˆ€ x, g (e x) = f x) : (βˆ‘' x, f x) = βˆ‘' y, g y := tsum_eq_tsum_of_has_sum_iff_has_sum $ Ξ» _, e.has_sum_iff_of_support he lemma tsum_eq_tsum_of_ne_zero_bij {g : Ξ³ β†’ Ξ±} (i : support g β†’ Ξ²) (hi : βˆ€ ⦃x y⦄, i x = i y β†’ (x : Ξ³) = y) (hf : support f βŠ† set.range i) (hfg : βˆ€ x, f (i x) = g x) : βˆ‘' x, f x = βˆ‘' y, g y := tsum_eq_tsum_of_has_sum_iff_has_sum $ Ξ» _, has_sum_iff_has_sum_of_ne_zero_bij i hi hf hfg lemma tsum_subtype (s : set Ξ²) (f : Ξ² β†’ Ξ±) : βˆ‘' x:s, f x = βˆ‘' x, s.indicator f x := tsum_eq_tsum_of_has_sum_iff_has_sum $ Ξ» _, has_sum_subtype_iff_indicator lemma tsum_op : βˆ‘' x, mul_opposite.op (f x) = mul_opposite.op (βˆ‘' x, f x) := begin by_cases h : summable f, { exact h.has_sum.op.tsum_eq, }, { have ho := summable_op.not.mpr h, rw [tsum_eq_zero_of_not_summable h, tsum_eq_zero_of_not_summable ho, mul_opposite.op_zero] }, end lemma tsum_unop {f : Ξ² β†’ αᡐᡒᡖ} : βˆ‘' x, mul_opposite.unop (f x) = mul_opposite.unop (βˆ‘' x, f x) := mul_opposite.op_injective tsum_op.symm section has_continuous_add variable [has_continuous_add Ξ±] lemma tsum_add (hf : summable f) (hg : summable g) : βˆ‘'b, (f b + g b) = (βˆ‘'b, f b) + (βˆ‘'b, g b) := (hf.has_sum.add hg.has_sum).tsum_eq lemma tsum_sum {f : Ξ³ β†’ Ξ² β†’ Ξ±} {s : finset Ξ³} (hf : βˆ€i∈s, summable (f i)) : βˆ‘'b, βˆ‘ i in s, f i b = βˆ‘ i in s, βˆ‘'b, f i b := (has_sum_sum $ assume i hi, (hf i hi).has_sum).tsum_eq lemma tsum_sigma' [t3_space Ξ±] {Ξ³ : Ξ² β†’ Type*} {f : (Ξ£b:Ξ², Ξ³ b) β†’ Ξ±} (h₁ : βˆ€b, summable (Ξ»c, f ⟨b, c⟩)) (hβ‚‚ : summable f) : βˆ‘'p, f p = βˆ‘'b c, f ⟨b, c⟩ := (hβ‚‚.has_sum.sigma (assume b, (h₁ b).has_sum)).tsum_eq.symm lemma tsum_prod' [t3_space Ξ±] {f : Ξ² Γ— Ξ³ β†’ Ξ±} (h : summable f) (h₁ : βˆ€b, summable (Ξ»c, f (b, c))) : βˆ‘'p, f p = βˆ‘'b c, f (b, c) := (h.has_sum.prod_fiberwise (assume b, (h₁ b).has_sum)).tsum_eq.symm lemma tsum_comm' [t3_space Ξ±] {f : Ξ² β†’ Ξ³ β†’ Ξ±} (h : summable (function.uncurry f)) (h₁ : βˆ€b, summable (f b)) (hβ‚‚ : βˆ€ c, summable (Ξ» b, f b c)) : βˆ‘' c b, f b c = βˆ‘' b c, f b c := begin erw [← tsum_prod' h h₁, ← tsum_prod' h.prod_symm hβ‚‚, ← (equiv.prod_comm Ξ² Ξ³).tsum_eq], refl, assumption end end has_continuous_add section has_continuous_star variables [star_add_monoid Ξ±] [has_continuous_star Ξ±] lemma tsum_star : star (βˆ‘' b, f b) = βˆ‘' b, star (f b) := begin by_cases hf : summable f, { exact hf.has_sum.star.tsum_eq.symm, }, { rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt summable.of_star hf), star_zero] }, end end has_continuous_star section encodable open encodable variable [encodable Ξ³] /-- You can compute a sum over an encodably type by summing over the natural numbers and taking a supremum. This is useful for outer measures. -/ theorem tsum_supr_decodeβ‚‚ [complete_lattice Ξ²] (m : Ξ² β†’ Ξ±) (m0 : m βŠ₯ = 0) (s : Ξ³ β†’ Ξ²) : βˆ‘' i : β„•, m (⨆ b ∈ decodeβ‚‚ Ξ³ i, s b) = βˆ‘' b : Ξ³, m (s b) := begin have H : βˆ€ n, m (⨆ b ∈ decodeβ‚‚ Ξ³ n, s b) β‰  0 β†’ (decodeβ‚‚ Ξ³ n).is_some, { intros n h, cases decodeβ‚‚ Ξ³ n with b, { refine (h $ by simp [m0]).elim }, { exact rfl } }, symmetry, refine tsum_eq_tsum_of_ne_zero_bij (Ξ» a, option.get (H a.1 a.2)) _ _ _, { rintros ⟨m, hm⟩ ⟨n, hn⟩ e, have := mem_decodeβ‚‚.1 (option.get_mem (H n hn)), rwa [← e, mem_decodeβ‚‚.1 (option.get_mem (H m hm))] at this }, { intros b h, refine ⟨⟨encode b, _⟩, _⟩, { simp only [mem_support, encodekβ‚‚] at h ⊒, convert h, simp [set.ext_iff, encodekβ‚‚] }, { exact option.get_of_mem _ (encodekβ‚‚ _) } }, { rintros ⟨n, h⟩, dsimp only [subtype.coe_mk], transitivity, swap, rw [show decodeβ‚‚ Ξ³ n = _, from option.get_mem (H n h)], congr, simp [ext_iff, -option.some_get] } end /-- `tsum_supr_decodeβ‚‚` specialized to the complete lattice of sets. -/ theorem tsum_Union_decodeβ‚‚ (m : set Ξ² β†’ Ξ±) (m0 : m βˆ… = 0) (s : Ξ³ β†’ set Ξ²) : βˆ‘' i, m (⋃ b ∈ decodeβ‚‚ Ξ³ i, s b) = βˆ‘' b, m (s b) := tsum_supr_decodeβ‚‚ m m0 s /-! Some properties about measure-like functions. These could also be functions defined on complete sublattices of sets, with the property that they are countably sub-additive. `R` will probably be instantiated with `(≀)` in all applications. -/ /-- If a function is countably sub-additive then it is sub-additive on encodable types -/ theorem rel_supr_tsum [complete_lattice Ξ²] (m : Ξ² β†’ Ξ±) (m0 : m βŠ₯ = 0) (R : Ξ± β†’ Ξ± β†’ Prop) (m_supr : βˆ€(s : β„• β†’ Ξ²), R (m (⨆ i, s i)) βˆ‘' i, m (s i)) (s : Ξ³ β†’ Ξ²) : R (m (⨆ b : Ξ³, s b)) βˆ‘' b : Ξ³, m (s b) := by { rw [← supr_decodeβ‚‚, ← tsum_supr_decodeβ‚‚ _ m0 s], exact m_supr _ } /-- If a function is countably sub-additive then it is sub-additive on finite sets -/ theorem rel_supr_sum [complete_lattice Ξ²] (m : Ξ² β†’ Ξ±) (m0 : m βŠ₯ = 0) (R : Ξ± β†’ Ξ± β†’ Prop) (m_supr : βˆ€(s : β„• β†’ Ξ²), R (m (⨆ i, s i)) (βˆ‘' i, m (s i))) (s : Ξ΄ β†’ Ξ²) (t : finset Ξ΄) : R (m (⨆ d ∈ t, s d)) (βˆ‘ d in t, m (s d)) := by { cases t.nonempty_encodable, rw [supr_subtype'], convert rel_supr_tsum m m0 R m_supr _, rw [← finset.tsum_subtype], assumption } /-- If a function is countably sub-additive then it is binary sub-additive -/ theorem rel_sup_add [complete_lattice Ξ²] (m : Ξ² β†’ Ξ±) (m0 : m βŠ₯ = 0) (R : Ξ± β†’ Ξ± β†’ Prop) (m_supr : βˆ€(s : β„• β†’ Ξ²), R (m (⨆ i, s i)) (βˆ‘' i, m (s i))) (s₁ sβ‚‚ : Ξ²) : R (m (s₁ βŠ” sβ‚‚)) (m s₁ + m sβ‚‚) := begin convert rel_supr_tsum m m0 R m_supr (Ξ» b, cond b s₁ sβ‚‚), { simp only [supr_bool_eq, cond] }, { rw [tsum_fintype, fintype.sum_bool, cond, cond] } end end encodable variables [has_continuous_add Ξ±] lemma tsum_add_tsum_compl {s : set Ξ²} (hs : summable (f ∘ coe : s β†’ Ξ±)) (hsc : summable (f ∘ coe : sᢜ β†’ Ξ±)) : (βˆ‘' x : s, f x) + (βˆ‘' x : sᢜ, f x) = βˆ‘' x, f x := (hs.has_sum.add_compl hsc.has_sum).tsum_eq.symm lemma tsum_union_disjoint {s t : set Ξ²} (hd : disjoint s t) (hs : summable (f ∘ coe : s β†’ Ξ±)) (ht : summable (f ∘ coe : t β†’ Ξ±)) : (βˆ‘' x : s βˆͺ t, f x) = (βˆ‘' x : s, f x) + (βˆ‘' x : t, f x) := (hs.has_sum.add_disjoint hd ht.has_sum).tsum_eq lemma tsum_even_add_odd {f : β„• β†’ Ξ±} (he : summable (Ξ» k, f (2 * k))) (ho : summable (Ξ» k, f (2 * k + 1))) : (βˆ‘' k, f (2 * k)) + (βˆ‘' k, f (2 * k + 1)) = βˆ‘' k, f k := (he.has_sum.even_add_odd ho.has_sum).tsum_eq.symm end tsum section prod variables [add_comm_monoid Ξ±] [topological_space Ξ±] [add_comm_monoid Ξ³] [topological_space Ξ³] lemma has_sum.prod_mk {f : Ξ² β†’ Ξ±} {g : Ξ² β†’ Ξ³} {a : Ξ±} {b : Ξ³} (hf : has_sum f a) (hg : has_sum g b) : has_sum (Ξ» x, (⟨f x, g x⟩ : Ξ± Γ— Ξ³)) ⟨a, b⟩ := by simp [has_sum, ← prod_mk_sum, filter.tendsto.prod_mk_nhds hf hg] end prod section pi variables {ΞΉ : Type*} {Ο€ : Ξ± β†’ Type*} [βˆ€ x, add_comm_monoid (Ο€ x)] [βˆ€ x, topological_space (Ο€ x)] lemma pi.has_sum {f : ΞΉ β†’ βˆ€ x, Ο€ x} {g : βˆ€ x, Ο€ x} : has_sum f g ↔ βˆ€ x, has_sum (Ξ» i, f i x) (g x) := by simp only [has_sum, tendsto_pi_nhds, sum_apply] lemma pi.summable {f : ΞΉ β†’ βˆ€ x, Ο€ x} : summable f ↔ βˆ€ x, summable (Ξ» i, f i x) := by simp only [summable, pi.has_sum, skolem] lemma tsum_apply [βˆ€ x, t2_space (Ο€ x)] {f : ΞΉ β†’ βˆ€ x, Ο€ x}{x : Ξ±} (hf : summable f) : (βˆ‘' i, f i) x = βˆ‘' i, f i x := (pi.has_sum.mp hf.has_sum x).tsum_eq.symm end pi section topological_group variables [add_comm_group Ξ±] [topological_space Ξ±] [topological_add_group Ξ±] variables {f g : Ξ² β†’ Ξ±} {a a₁ aβ‚‚ : Ξ±} -- `by simpa using` speeds up elaboration. Why? lemma has_sum.neg (h : has_sum f a) : has_sum (Ξ»b, - f b) (- a) := by simpa only using h.map (-add_monoid_hom.id Ξ±) continuous_neg lemma summable.neg (hf : summable f) : summable (Ξ»b, - f b) := hf.has_sum.neg.summable lemma summable.of_neg (hf : summable (Ξ»b, - f b)) : summable f := by simpa only [neg_neg] using hf.neg lemma summable_neg_iff : summable (Ξ» b, - f b) ↔ summable f := ⟨summable.of_neg, summable.neg⟩ lemma has_sum.sub (hf : has_sum f a₁) (hg : has_sum g aβ‚‚) : has_sum (Ξ»b, f b - g b) (a₁ - aβ‚‚) := by { simp only [sub_eq_add_neg], exact hf.add hg.neg } lemma summable.sub (hf : summable f) (hg : summable g) : summable (Ξ»b, f b - g b) := (hf.has_sum.sub hg.has_sum).summable lemma summable.trans_sub (hg : summable g) (hfg : summable (Ξ»b, f b - g b)) : summable f := by simpa only [sub_add_cancel] using hfg.add hg lemma summable_iff_of_summable_sub (hfg : summable (Ξ»b, f b - g b)) : summable f ↔ summable g := ⟨λ hf, hf.trans_sub $ by simpa only [neg_sub] using hfg.neg, Ξ» hg, hg.trans_sub hfg⟩ lemma has_sum.update (hf : has_sum f a₁) (b : Ξ²) [decidable_eq Ξ²] (a : Ξ±) : has_sum (update f b a) (a - f b + a₁) := begin convert ((has_sum_ite_eq b _).add hf), ext b', by_cases h : b' = b, { rw [h, update_same], simp only [eq_self_iff_true, if_true, sub_add_cancel] }, simp only [h, update_noteq, if_false, ne.def, zero_add, not_false_iff], end lemma summable.update (hf : summable f) (b : Ξ²) [decidable_eq Ξ²] (a : Ξ±) : summable (update f b a) := (hf.has_sum.update b a).summable lemma has_sum.has_sum_compl_iff {s : set Ξ²} (hf : has_sum (f ∘ coe : s β†’ Ξ±) a₁) : has_sum (f ∘ coe : sᢜ β†’ Ξ±) aβ‚‚ ↔ has_sum f (a₁ + aβ‚‚) := begin refine ⟨λ h, hf.add_compl h, Ξ» h, _⟩, rw [has_sum_subtype_iff_indicator] at hf ⊒, rw [set.indicator_compl], simpa only [add_sub_cancel'] using h.sub hf end lemma has_sum.has_sum_iff_compl {s : set Ξ²} (hf : has_sum (f ∘ coe : s β†’ Ξ±) a₁) : has_sum f aβ‚‚ ↔ has_sum (f ∘ coe : sᢜ β†’ Ξ±) (aβ‚‚ - a₁) := iff.symm $ hf.has_sum_compl_iff.trans $ by rw [add_sub_cancel'_right] lemma summable.summable_compl_iff {s : set Ξ²} (hf : summable (f ∘ coe : s β†’ Ξ±)) : summable (f ∘ coe : sᢜ β†’ Ξ±) ↔ summable f := ⟨λ ⟨a, ha⟩, (hf.has_sum.has_sum_compl_iff.1 ha).summable, Ξ» ⟨a, ha⟩, (hf.has_sum.has_sum_iff_compl.1 ha).summable⟩ protected lemma finset.has_sum_compl_iff (s : finset Ξ²) : has_sum (Ξ» x : {x // x βˆ‰ s}, f x) a ↔ has_sum f (a + βˆ‘ i in s, f i) := (s.has_sum f).has_sum_compl_iff.trans $ by rw [add_comm] protected lemma finset.has_sum_iff_compl (s : finset Ξ²) : has_sum f a ↔ has_sum (Ξ» x : {x // x βˆ‰ s}, f x) (a - βˆ‘ i in s, f i) := (s.has_sum f).has_sum_iff_compl protected lemma finset.summable_compl_iff (s : finset Ξ²) : summable (Ξ» x : {x // x βˆ‰ s}, f x) ↔ summable f := (s.summable f).summable_compl_iff lemma set.finite.summable_compl_iff {s : set Ξ²} (hs : s.finite) : summable (f ∘ coe : sᢜ β†’ Ξ±) ↔ summable f := (hs.summable f).summable_compl_iff lemma has_sum_ite_eq_extract [decidable_eq Ξ²] (hf : has_sum f a) (b : Ξ²) : has_sum (Ξ» n, ite (n = b) 0 (f n)) (a - f b) := begin convert hf.update b 0 using 1, { ext n, rw function.update_apply, }, { rw [sub_add_eq_add_sub, zero_add], }, end section tsum variables [t2_space Ξ±] lemma tsum_neg : βˆ‘'b, - f b = - βˆ‘'b, f b := begin by_cases hf : summable f, { exact hf.has_sum.neg.tsum_eq, }, { simp [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt summable.of_neg hf)] }, end lemma tsum_sub (hf : summable f) (hg : summable g) : βˆ‘'b, (f b - g b) = βˆ‘'b, f b - βˆ‘'b, g b := (hf.has_sum.sub hg.has_sum).tsum_eq lemma sum_add_tsum_compl {s : finset Ξ²} (hf : summable f) : (βˆ‘ x in s, f x) + (βˆ‘' x : (↑s : set Ξ²)ᢜ, f x) = βˆ‘' x, f x := ((s.has_sum f).add_compl (s.summable_compl_iff.2 hf).has_sum).tsum_eq.symm /-- Let `f : Ξ² β†’ Ξ±` be a sequence with summable series and let `b ∈ Ξ²` be an index. Lemma `tsum_ite_eq_extract` writes `Ξ£ f n` as the sum of `f b` plus the series of the remaining terms. -/ lemma tsum_ite_eq_extract [decidable_eq Ξ²] (hf : summable f) (b : Ξ²) : βˆ‘' n, f n = f b + βˆ‘' n, ite (n = b) 0 (f n) := begin rw (has_sum_ite_eq_extract hf.has_sum b).tsum_eq, exact (add_sub_cancel'_right _ _).symm, end end tsum /-! ### Sums on subtypes If `s` is a finset of `Ξ±`, we show that the summability of `f` in the whole space and on the subtype `univ - s` are equivalent, and relate their sums. For a function defined on `β„•`, we deduce the formula `(βˆ‘ i in range k, f i) + (βˆ‘' i, f (i + k)) = (βˆ‘' i, f i)`, in `sum_add_tsum_nat_add`. -/ section subtype lemma has_sum_nat_add_iff {f : β„• β†’ Ξ±} (k : β„•) {a : Ξ±} : has_sum (Ξ» n, f (n + k)) a ↔ has_sum f (a + βˆ‘ i in range k, f i) := begin refine iff.trans _ ((range k).has_sum_compl_iff), rw [← (not_mem_range_equiv k).symm.has_sum_iff], refl end lemma summable_nat_add_iff {f : β„• β†’ Ξ±} (k : β„•) : summable (Ξ» n, f (n + k)) ↔ summable f := iff.symm $ (equiv.add_right (βˆ‘ i in range k, f i)).surjective.summable_iff_of_has_sum_iff $ Ξ» a, (has_sum_nat_add_iff k).symm lemma has_sum_nat_add_iff' {f : β„• β†’ Ξ±} (k : β„•) {a : Ξ±} : has_sum (Ξ» n, f (n + k)) (a - βˆ‘ i in range k, f i) ↔ has_sum f a := by simp [has_sum_nat_add_iff] lemma sum_add_tsum_nat_add [t2_space Ξ±] {f : β„• β†’ Ξ±} (k : β„•) (h : summable f) : (βˆ‘ i in range k, f i) + (βˆ‘' i, f (i + k)) = βˆ‘' i, f i := by simpa only [add_comm] using ((has_sum_nat_add_iff k).1 ((summable_nat_add_iff k).2 h).has_sum).unique h.has_sum lemma tsum_eq_zero_add [t2_space Ξ±] {f : β„• β†’ Ξ±} (hf : summable f) : βˆ‘'b, f b = f 0 + βˆ‘'b, f (b + 1) := by simpa only [sum_range_one] using (sum_add_tsum_nat_add 1 hf).symm /-- For `f : β„• β†’ Ξ±`, then `βˆ‘' k, f (k + i)` tends to zero. This does not require a summability assumption on `f`, as otherwise all sums are zero. -/ lemma tendsto_sum_nat_add [t2_space Ξ±] (f : β„• β†’ Ξ±) : tendsto (Ξ» i, βˆ‘' k, f (k + i)) at_top (𝓝 0) := begin by_cases hf : summable f, { have hβ‚€ : (Ξ» i, (βˆ‘' i, f i) - βˆ‘ j in range i, f j) = Ξ» i, βˆ‘' (k : β„•), f (k + i), { ext1 i, rw [sub_eq_iff_eq_add, add_comm, sum_add_tsum_nat_add i hf] }, have h₁ : tendsto (Ξ» i : β„•, βˆ‘' i, f i) at_top (𝓝 (βˆ‘' i, f i)) := tendsto_const_nhds, simpa only [hβ‚€, sub_self] using tendsto.sub h₁ hf.has_sum.tendsto_sum_nat }, { convert tendsto_const_nhds, ext1 i, rw ← summable_nat_add_iff i at hf, { exact tsum_eq_zero_of_not_summable hf }, { apply_instance } } end /-- If `fβ‚€, f₁, fβ‚‚, ...` and `gβ‚€, g₁, gβ‚‚, ...` are both convergent then so is the `β„€`-indexed sequence: `..., gβ‚‚, g₁, gβ‚€, fβ‚€, f₁, fβ‚‚, ...`. -/ lemma has_sum.int_rec {b : Ξ±} {f g : β„• β†’ Ξ±} (hf : has_sum f a) (hg : has_sum g b) : @has_sum Ξ± _ _ _ (@int.rec (Ξ» _, Ξ±) f g : β„€ β†’ Ξ±) (a + b) := begin -- note this proof works for any two-case inductive have h₁ : injective (coe : β„• β†’ β„€) := @int.of_nat.inj, have hβ‚‚ : injective int.neg_succ_of_nat := @int.neg_succ_of_nat.inj, have : is_compl (set.range (coe : β„• β†’ β„€)) (set.range int.neg_succ_of_nat), { split, { rintros _ ⟨⟨i, rfl⟩, ⟨j, ⟨⟩⟩⟩ }, { rintros (i | j) h, exacts [or.inl ⟨_, rfl⟩, or.inr ⟨_, rfl⟩] } }, exact has_sum.add_is_compl this (h₁.has_sum_range_iff.mpr hf) (hβ‚‚.has_sum_range_iff.mpr hg), end lemma has_sum.nonneg_add_neg {b : Ξ±} {f : β„€ β†’ Ξ±} (hnonneg : has_sum (Ξ» n : β„•, f n) a) (hneg : has_sum (Ξ» (n : β„•), f (-n.succ)) b) : has_sum f (a + b) := begin simp_rw ← int.neg_succ_of_nat_coe at hneg, convert hnonneg.int_rec hneg using 1, ext (i | j); refl, end lemma has_sum.pos_add_zero_add_neg {b : Ξ±} {f : β„€ β†’ Ξ±} (hpos : has_sum (Ξ» n:β„•, f(n + 1)) a) (hneg : has_sum (Ξ» (n : β„•), f (-n.succ)) b) : has_sum f (a + f 0 + b) := begin have : βˆ€ g : β„• β†’ Ξ±, has_sum (Ξ» k, g (k + 1)) a β†’ has_sum g (a + g 0), { intros g hg, simpa using (has_sum_nat_add_iff _).mp hg }, exact (this (Ξ» n, f n) hpos).nonneg_add_neg hneg, end end subtype end topological_group section topological_semiring variables [non_unital_non_assoc_semiring Ξ±] [topological_space Ξ±] [topological_semiring Ξ±] variables {f g : Ξ² β†’ Ξ±} {a a₁ aβ‚‚ : Ξ±} lemma has_sum.mul_left (aβ‚‚) (h : has_sum f a₁) : has_sum (Ξ»b, aβ‚‚ * f b) (aβ‚‚ * a₁) := by simpa only using h.map (add_monoid_hom.mul_left aβ‚‚) (continuous_const.mul continuous_id) lemma has_sum.mul_right (aβ‚‚) (hf : has_sum f a₁) : has_sum (Ξ»b, f b * aβ‚‚) (a₁ * aβ‚‚) := by simpa only using hf.map (add_monoid_hom.mul_right aβ‚‚) (continuous_id.mul continuous_const) lemma summable.mul_left (a) (hf : summable f) : summable (Ξ»b, a * f b) := (hf.has_sum.mul_left _).summable lemma summable.mul_right (a) (hf : summable f) : summable (Ξ»b, f b * a) := (hf.has_sum.mul_right _).summable section tsum variables [t2_space Ξ±] lemma summable.tsum_mul_left (a) (hf : summable f) : βˆ‘'b, a * f b = a * βˆ‘'b, f b := (hf.has_sum.mul_left _).tsum_eq lemma summable.tsum_mul_right (a) (hf : summable f) : (βˆ‘'b, f b * a) = (βˆ‘'b, f b) * a := (hf.has_sum.mul_right _).tsum_eq lemma commute.tsum_right (a) (h : βˆ€ b, commute a (f b)) : commute a (βˆ‘' b, f b) := if hf : summable f then (hf.tsum_mul_left a).symm.trans ((congr_arg _ $ funext h).trans (hf.tsum_mul_right a)) else (tsum_eq_zero_of_not_summable hf).symm β–Έ commute.zero_right _ lemma commute.tsum_left (a) (h : βˆ€ b, commute (f b) a) : commute (βˆ‘' b, f b) a := (commute.tsum_right _ $ Ξ» b, (h b).symm).symm end tsum end topological_semiring section const_smul variables {R : Type*} [monoid R] [topological_space Ξ±] [add_comm_monoid Ξ±] [distrib_mul_action R Ξ±] [has_continuous_const_smul R Ξ±] {f : Ξ² β†’ Ξ±} lemma has_sum.const_smul {a : Ξ±} {r : R} (hf : has_sum f a) : has_sum (Ξ» z, r β€’ f z) (r β€’ a) := hf.map (distrib_mul_action.to_add_monoid_hom Ξ± r) (continuous_const_smul r) lemma summable.const_smul {r : R} (hf : summable f) : summable (Ξ» z, r β€’ f z) := hf.has_sum.const_smul.summable lemma tsum_const_smul [t2_space Ξ±] {r : R} (hf : summable f) : βˆ‘' z, r β€’ f z = r β€’ βˆ‘' z, f z := hf.has_sum.const_smul.tsum_eq end const_smul section smul_const variables {R : Type*} [semiring R] [topological_space R] [topological_space Ξ±] [add_comm_monoid Ξ±] [module R Ξ±] [has_continuous_smul R Ξ±] {f : Ξ² β†’ R} lemma has_sum.smul_const {a : Ξ±} {r : R} (hf : has_sum f r) : has_sum (Ξ» z, f z β€’ a) (r β€’ a) := hf.map ((smul_add_hom R Ξ±).flip a) (continuous_id.smul continuous_const) lemma summable.smul_const {a : Ξ±} (hf : summable f) : summable (Ξ» z, f z β€’ a) := hf.has_sum.smul_const.summable lemma tsum_smul_const [t2_space Ξ±] {a : Ξ±} (hf : summable f) : βˆ‘' z, f z β€’ a = (βˆ‘' z, f z) β€’ a := hf.has_sum.smul_const.tsum_eq end smul_const section division_ring variables [division_ring Ξ±] [topological_space Ξ±] [topological_ring Ξ±] {f g : Ξ² β†’ Ξ±} {a a₁ aβ‚‚ : Ξ±} lemma has_sum.div_const (h : has_sum f a) (b : Ξ±) : has_sum (Ξ» x, f x / b) (a / b) := by simp only [div_eq_mul_inv, h.mul_right b⁻¹] lemma summable.div_const (h : summable f) (b : Ξ±) : summable (Ξ» x, f x / b) := (h.has_sum.div_const b).summable lemma has_sum_mul_left_iff (h : aβ‚‚ β‰  0) : has_sum f a₁ ↔ has_sum (Ξ»b, aβ‚‚ * f b) (aβ‚‚ * a₁) := ⟨has_sum.mul_left _, Ξ» H, by simpa only [inv_mul_cancel_leftβ‚€ h] using H.mul_left aβ‚‚β»ΒΉβŸ© lemma has_sum_mul_right_iff (h : aβ‚‚ β‰  0) : has_sum f a₁ ↔ has_sum (Ξ»b, f b * aβ‚‚) (a₁ * aβ‚‚) := ⟨has_sum.mul_right _, Ξ» H, by simpa only [mul_inv_cancel_rightβ‚€ h] using H.mul_right aβ‚‚β»ΒΉβŸ© lemma summable_mul_left_iff (h : a β‰  0) : summable f ↔ summable (Ξ»b, a * f b) := ⟨λ H, H.mul_left _, Ξ» H, by simpa only [inv_mul_cancel_leftβ‚€ h] using H.mul_left a⁻¹⟩ lemma summable_mul_right_iff (h : a β‰  0) : summable f ↔ summable (Ξ»b, f b * a) := ⟨λ H, H.mul_right _, Ξ» H, by simpa only [mul_inv_cancel_rightβ‚€ h] using H.mul_right a⁻¹⟩ lemma tsum_mul_left [t2_space Ξ±] : (βˆ‘' x, a * f x) = a * βˆ‘' x, f x := if hf : summable f then hf.tsum_mul_left a else if ha : a = 0 then by simp [ha] else by rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt (summable_mul_left_iff ha).2 hf), mul_zero] lemma tsum_mul_right [t2_space Ξ±] : (βˆ‘' x, f x * a) = (βˆ‘' x, f x) * a := if hf : summable f then hf.tsum_mul_right a else if ha : a = 0 then by simp [ha] else by rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt (summable_mul_right_iff ha).2 hf), zero_mul] end division_ring section order_topology variables [ordered_add_comm_monoid Ξ±] [topological_space Ξ±] [order_closed_topology Ξ±] variables {f g : Ξ² β†’ Ξ±} {a a₁ aβ‚‚ : Ξ±} lemma has_sum_le (h : βˆ€b, f b ≀ g b) (hf : has_sum f a₁) (hg : has_sum g aβ‚‚) : a₁ ≀ aβ‚‚ := le_of_tendsto_of_tendsto' hf hg $ assume s, sum_le_sum $ assume b _, h b @[mono] lemma has_sum_mono (hf : has_sum f a₁) (hg : has_sum g aβ‚‚) (h : f ≀ g) : a₁ ≀ aβ‚‚ := has_sum_le h hf hg lemma has_sum_le_of_sum_le (hf : has_sum f a) (h : βˆ€ s : finset Ξ², βˆ‘ b in s, f b ≀ aβ‚‚) : a ≀ aβ‚‚ := le_of_tendsto' hf h lemma le_has_sum_of_le_sum (hf : has_sum f a) (h : βˆ€ s : finset Ξ², aβ‚‚ ≀ βˆ‘ b in s, f b) : aβ‚‚ ≀ a := ge_of_tendsto' hf h lemma has_sum_le_inj {g : Ξ³ β†’ Ξ±} (i : Ξ² β†’ Ξ³) (hi : injective i) (hs : βˆ€cβˆ‰set.range i, 0 ≀ g c) (h : βˆ€b, f b ≀ g (i b)) (hf : has_sum f a₁) (hg : has_sum g aβ‚‚) : a₁ ≀ aβ‚‚ := have has_sum (Ξ»c, (partial_inv i c).cases_on' 0 f) a₁, begin refine (has_sum_iff_has_sum_of_ne_zero_bij (i ∘ coe) _ _ _).2 hf, { exact assume c₁ cβ‚‚ eq, hi eq }, { intros c hc, rw [mem_support] at hc, cases eq : partial_inv i c with b; rw eq at hc, { contradiction }, { rw [partial_inv_of_injective hi] at eq, exact ⟨⟨b, hc⟩, eq⟩ } }, { assume c, simp [partial_inv_left hi, option.cases_on'] } end, begin refine has_sum_le (assume c, _) this hg, by_cases c ∈ set.range i, { rcases h with ⟨b, rfl⟩, rw [partial_inv_left hi, option.cases_on'], exact h _ }, { have : partial_inv i c = none := dif_neg h, rw [this, option.cases_on'], exact hs _ h } end lemma tsum_le_tsum_of_inj {g : Ξ³ β†’ Ξ±} (i : Ξ² β†’ Ξ³) (hi : injective i) (hs : βˆ€cβˆ‰set.range i, 0 ≀ g c) (h : βˆ€b, f b ≀ g (i b)) (hf : summable f) (hg : summable g) : tsum f ≀ tsum g := has_sum_le_inj i hi hs h hf.has_sum hg.has_sum lemma sum_le_has_sum (s : finset Ξ²) (hs : βˆ€ bβˆ‰s, 0 ≀ f b) (hf : has_sum f a) : βˆ‘ b in s, f b ≀ a := ge_of_tendsto hf (eventually_at_top.2 ⟨s, Ξ» t hst, sum_le_sum_of_subset_of_nonneg hst $ Ξ» b hbt hbs, hs b hbs⟩) lemma is_lub_has_sum (h : βˆ€ b, 0 ≀ f b) (hf : has_sum f a) : is_lub (set.range (Ξ» s : finset Ξ², βˆ‘ b in s, f b)) a := is_lub_of_tendsto_at_top (finset.sum_mono_set_of_nonneg h) hf lemma le_has_sum (hf : has_sum f a) (b : Ξ²) (hb : βˆ€ b' β‰  b, 0 ≀ f b') : f b ≀ a := calc f b = βˆ‘ b in {b}, f b : finset.sum_singleton.symm ... ≀ a : sum_le_has_sum _ (by { convert hb, simp }) hf lemma sum_le_tsum {f : Ξ² β†’ Ξ±} (s : finset Ξ²) (hs : βˆ€ bβˆ‰s, 0 ≀ f b) (hf : summable f) : βˆ‘ b in s, f b ≀ βˆ‘' b, f b := sum_le_has_sum s hs hf.has_sum lemma le_tsum (hf : summable f) (b : Ξ²) (hb : βˆ€ b' β‰  b, 0 ≀ f b') : f b ≀ βˆ‘' b, f b := le_has_sum (summable.has_sum hf) b hb lemma tsum_le_tsum (h : βˆ€b, f b ≀ g b) (hf : summable f) (hg : summable g) : βˆ‘'b, f b ≀ βˆ‘'b, g b := has_sum_le h hf.has_sum hg.has_sum @[mono] lemma tsum_mono (hf : summable f) (hg : summable g) (h : f ≀ g) : βˆ‘' n, f n ≀ βˆ‘' n, g n := tsum_le_tsum h hf hg lemma tsum_le_of_sum_le (hf : summable f) (h : βˆ€ s : finset Ξ², βˆ‘ b in s, f b ≀ aβ‚‚) : βˆ‘' b, f b ≀ aβ‚‚ := has_sum_le_of_sum_le hf.has_sum h lemma tsum_le_of_sum_le' (haβ‚‚ : 0 ≀ aβ‚‚) (h : βˆ€ s : finset Ξ², βˆ‘ b in s, f b ≀ aβ‚‚) : βˆ‘' b, f b ≀ aβ‚‚ := begin by_cases hf : summable f, { exact tsum_le_of_sum_le hf h }, { rw tsum_eq_zero_of_not_summable hf, exact haβ‚‚ } end lemma has_sum.nonneg (h : βˆ€ b, 0 ≀ g b) (ha : has_sum g a) : 0 ≀ a := has_sum_le h has_sum_zero ha lemma has_sum.nonpos (h : βˆ€ b, g b ≀ 0) (ha : has_sum g a) : a ≀ 0 := has_sum_le h ha has_sum_zero lemma tsum_nonneg (h : βˆ€ b, 0 ≀ g b) : 0 ≀ βˆ‘'b, g b := begin by_cases hg : summable g, { exact hg.has_sum.nonneg h }, { simp [tsum_eq_zero_of_not_summable hg] } end lemma tsum_nonpos (h : βˆ€ b, f b ≀ 0) : βˆ‘'b, f b ≀ 0 := begin by_cases hf : summable f, { exact hf.has_sum.nonpos h }, { simp [tsum_eq_zero_of_not_summable hf] } end end order_topology section ordered_topological_group variables [ordered_add_comm_group Ξ±] [topological_space Ξ±] [topological_add_group Ξ±] [order_closed_topology Ξ±] {f g : Ξ² β†’ Ξ±} {a₁ aβ‚‚ : Ξ±} lemma has_sum_lt {i : Ξ²} (h : βˆ€ (b : Ξ²), f b ≀ g b) (hi : f i < g i) (hf : has_sum f a₁) (hg : has_sum g aβ‚‚) : a₁ < aβ‚‚ := have update f i 0 ≀ update g i 0 := update_le_update_iff.mpr ⟨rfl.le, Ξ» i _, h i⟩, have 0 - f i + a₁ ≀ 0 - g i + aβ‚‚ := has_sum_le this (hf.update i 0) (hg.update i 0), by simpa only [zero_sub, add_neg_cancel_left] using add_lt_add_of_lt_of_le hi this @[mono] lemma has_sum_strict_mono (hf : has_sum f a₁) (hg : has_sum g aβ‚‚) (h : f < g) : a₁ < aβ‚‚ := let ⟨hle, i, hi⟩ := pi.lt_def.mp h in has_sum_lt hle hi hf hg lemma tsum_lt_tsum {i : Ξ²} (h : βˆ€ (b : Ξ²), f b ≀ g b) (hi : f i < g i) (hf : summable f) (hg : summable g) : βˆ‘' n, f n < βˆ‘' n, g n := has_sum_lt h hi hf.has_sum hg.has_sum @[mono] lemma tsum_strict_mono (hf : summable f) (hg : summable g) (h : f < g) : βˆ‘' n, f n < βˆ‘' n, g n := let ⟨hle, i, hi⟩ := pi.lt_def.mp h in tsum_lt_tsum hle hi hf hg lemma tsum_pos (hsum : summable g) (hg : βˆ€ b, 0 ≀ g b) (i : Ξ²) (hi : 0 < g i) : 0 < βˆ‘' b, g b := by { rw ← tsum_zero, exact tsum_lt_tsum hg hi summable_zero hsum } lemma has_sum_zero_iff_of_nonneg (hf : βˆ€ i, 0 ≀ f i) : has_sum f 0 ↔ f = 0 := begin split, { intros hf', ext i, by_contra hi', have hi : 0 < f i := lt_of_le_of_ne (hf i) (ne.symm hi'), simpa using has_sum_lt hf hi has_sum_zero hf' }, { rintros rfl, exact has_sum_zero }, end end ordered_topological_group section canonically_ordered variables [canonically_ordered_add_monoid Ξ±] [topological_space Ξ±] [order_closed_topology Ξ±] variables {f : Ξ² β†’ Ξ±} {a : Ξ±} lemma le_has_sum' (hf : has_sum f a) (b : Ξ²) : f b ≀ a := le_has_sum hf b $ Ξ» _ _, zero_le _ lemma le_tsum' (hf : summable f) (b : Ξ²) : f b ≀ βˆ‘' b, f b := le_tsum hf b $ Ξ» _ _, zero_le _ lemma has_sum_zero_iff : has_sum f 0 ↔ βˆ€ x, f x = 0 := begin refine ⟨_, Ξ» h, _⟩, { contrapose!, exact Ξ» ⟨x, hx⟩ h, irrefl _ (lt_of_lt_of_le (pos_iff_ne_zero.2 hx) (le_has_sum' h x)) }, { convert has_sum_zero, exact funext h } end lemma tsum_eq_zero_iff (hf : summable f) : βˆ‘' i, f i = 0 ↔ βˆ€ x, f x = 0 := by rw [←has_sum_zero_iff, hf.has_sum_iff] lemma tsum_ne_zero_iff (hf : summable f) : βˆ‘' i, f i β‰  0 ↔ βˆƒ x, f x β‰  0 := by rw [ne.def, tsum_eq_zero_iff hf, not_forall] lemma is_lub_has_sum' (hf : has_sum f a) : is_lub (set.range (Ξ» s : finset Ξ², βˆ‘ b in s, f b)) a := is_lub_of_tendsto_at_top (finset.sum_mono_set f) hf end canonically_ordered section uniform_group variables [add_comm_group Ξ±] [uniform_space Ξ±] /-- The **Cauchy criterion** for infinite sums, also known as the **Cauchy convergence test** -/ lemma summable_iff_cauchy_seq_finset [complete_space Ξ±] {f : Ξ² β†’ Ξ±} : summable f ↔ cauchy_seq (Ξ» (s : finset Ξ²), βˆ‘ b in s, f b) := cauchy_map_iff_exists_tendsto.symm variables [uniform_add_group Ξ±] {f g : Ξ² β†’ Ξ±} {a a₁ aβ‚‚ : Ξ±} lemma cauchy_seq_finset_iff_vanishing : cauchy_seq (Ξ» (s : finset Ξ²), βˆ‘ b in s, f b) ↔ βˆ€ e ∈ 𝓝 (0:Ξ±), (βˆƒs:finset Ξ², βˆ€t, disjoint t s β†’ βˆ‘ b in t, f b ∈ e) := begin simp only [cauchy_seq, cauchy_map_iff, and_iff_right at_top_ne_bot, prod_at_top_at_top_eq, uniformity_eq_comap_nhds_zero Ξ±, tendsto_comap_iff, (∘)], rw [tendsto_at_top'], split, { assume h e he, rcases h e he with ⟨⟨s₁, sβ‚‚βŸ©, h⟩, use [s₁ βˆͺ sβ‚‚], assume t ht, specialize h (s₁ βˆͺ sβ‚‚, (s₁ βˆͺ sβ‚‚) βˆͺ t) ⟨le_sup_left, le_sup_of_le_left le_sup_right⟩, simpa only [finset.sum_union ht.symm, add_sub_cancel'] using h }, { assume h e he, rcases exists_nhds_half_neg he with ⟨d, hd, hde⟩, rcases h d hd with ⟨s, h⟩, use [(s, s)], rintros ⟨t₁, tβ‚‚βŸ© ⟨ht₁, htβ‚‚βŸ©, have : βˆ‘ b in tβ‚‚, f b - βˆ‘ b in t₁, f b = βˆ‘ b in tβ‚‚ \ s, f b - βˆ‘ b in t₁ \ s, f b, { simp only [(finset.sum_sdiff ht₁).symm, (finset.sum_sdiff htβ‚‚).symm, add_sub_add_right_eq_sub] }, simp only [this], exact hde _ (h _ finset.sdiff_disjoint) _ (h _ finset.sdiff_disjoint) } end local attribute [instance] topological_add_group.t3_space /-- The sum over the complement of a finset tends to `0` when the finset grows to cover the whole space. This does not need a summability assumption, as otherwise all sums are zero. -/ lemma tendsto_tsum_compl_at_top_zero [t1_space Ξ±] (f : Ξ² β†’ Ξ±) : tendsto (Ξ» (s : finset Ξ²), βˆ‘' b : {x // x βˆ‰ s}, f b) at_top (𝓝 0) := begin by_cases H : summable f, { assume e he, rcases nhds_is_closed he with ⟨o, ho, oe, o_closed⟩, simp only [le_eq_subset, set.mem_preimage, mem_at_top_sets, filter.mem_map, ge_iff_le], obtain ⟨s, hs⟩ : βˆƒ (s : finset Ξ²), βˆ€ (t : finset Ξ²), disjoint t s β†’ βˆ‘ (b : Ξ²) in t, f b ∈ o := cauchy_seq_finset_iff_vanishing.1 (tendsto.cauchy_seq H.has_sum) o ho, refine ⟨s, Ξ» a sa, oe _⟩, have A : summable (Ξ» b : {x // x βˆ‰ a}, f b) := a.summable_compl_iff.2 H, apply is_closed.mem_of_tendsto o_closed A.has_sum (eventually_of_forall (Ξ» b, _)), have : disjoint (finset.image (Ξ» (i : {x // x βˆ‰ a}), (i : Ξ²)) b) s, { apply disjoint_left.2 (Ξ» i hi his, _), rcases mem_image.1 hi with ⟨i', hi', rfl⟩, exact i'.2 (sa his), }, convert hs _ this using 1, rw sum_image, assume i hi j hj hij, exact subtype.ext hij }, { convert tendsto_const_nhds, ext s, apply tsum_eq_zero_of_not_summable, rwa finset.summable_compl_iff } end variable [complete_space Ξ±] lemma summable_iff_vanishing : summable f ↔ βˆ€ e ∈ 𝓝 (0:Ξ±), (βˆƒs:finset Ξ², βˆ€t, disjoint t s β†’ βˆ‘ b in t, f b ∈ e) := by rw [summable_iff_cauchy_seq_finset, cauchy_seq_finset_iff_vanishing] /- TODO: generalize to monoid with a uniform continuous subtraction operator: `(a + b) - b = a` -/ lemma summable.summable_of_eq_zero_or_self (hf : summable f) (h : βˆ€b, g b = 0 ∨ g b = f b) : summable g := summable_iff_vanishing.2 $ assume e he, let ⟨s, hs⟩ := summable_iff_vanishing.1 hf e he in ⟨s, assume t ht, have eq : βˆ‘ b in t.filter (Ξ»b, g b = f b), f b = βˆ‘ b in t, g b := calc βˆ‘ b in t.filter (Ξ»b, g b = f b), f b = βˆ‘ b in t.filter (Ξ»b, g b = f b), g b : finset.sum_congr rfl (assume b hb, (finset.mem_filter.1 hb).2.symm) ... = βˆ‘ b in t, g b : begin refine finset.sum_subset (finset.filter_subset _ _) _, assume b hbt hb, simp only [(βˆ‰), finset.mem_filter, and_iff_right hbt] at hb, exact (h b).resolve_right hb end, eq β–Έ hs _ $ finset.disjoint_of_subset_left (finset.filter_subset _ _) ht⟩ protected lemma summable.indicator (hf : summable f) (s : set Ξ²) : summable (s.indicator f) := hf.summable_of_eq_zero_or_self $ set.indicator_eq_zero_or_self _ _ lemma summable.comp_injective {i : Ξ³ β†’ Ξ²} (hf : summable f) (hi : injective i) : summable (f ∘ i) := begin simpa only [set.indicator_range_comp] using (hi.summable_iff _).2 (hf.indicator (set.range i)), exact Ξ» x hx, set.indicator_of_not_mem hx _ end lemma summable.subtype (hf : summable f) (s : set Ξ²) : summable (f ∘ coe : s β†’ Ξ±) := hf.comp_injective subtype.coe_injective lemma summable_subtype_and_compl {s : set Ξ²} : summable (Ξ» x : s, f x) ∧ summable (Ξ» x : sᢜ, f x) ↔ summable f := ⟨and_imp.2 summable.add_compl, Ξ» h, ⟨h.subtype s, h.subtype sᢜ⟩⟩ lemma summable.sigma_factor {Ξ³ : Ξ² β†’ Type*} {f : (Ξ£b:Ξ², Ξ³ b) β†’ Ξ±} (ha : summable f) (b : Ξ²) : summable (Ξ»c, f ⟨b, c⟩) := ha.comp_injective sigma_mk_injective lemma summable.sigma [t1_space Ξ±] {Ξ³ : Ξ² β†’ Type*} {f : (Ξ£b:Ξ², Ξ³ b) β†’ Ξ±} (ha : summable f) : summable (Ξ»b, βˆ‘'c, f ⟨b, c⟩) := ha.sigma' (Ξ» b, ha.sigma_factor b) lemma summable.prod_factor {f : Ξ² Γ— Ξ³ β†’ Ξ±} (h : summable f) (b : Ξ²) : summable (Ξ» c, f (b, c)) := h.comp_injective $ Ξ» c₁ cβ‚‚ h, (prod.ext_iff.1 h).2 lemma tsum_sigma [t1_space Ξ±] {Ξ³ : Ξ² β†’ Type*} {f : (Ξ£b:Ξ², Ξ³ b) β†’ Ξ±} (ha : summable f) : βˆ‘'p, f p = βˆ‘'b c, f ⟨b, c⟩ := tsum_sigma' (Ξ» b, ha.sigma_factor b) ha lemma tsum_prod [t1_space Ξ±] {f : Ξ² Γ— Ξ³ β†’ Ξ±} (h : summable f) : βˆ‘'p, f p = βˆ‘'b c, f ⟨b, c⟩ := tsum_prod' h h.prod_factor lemma tsum_comm [t1_space Ξ±] {f : Ξ² β†’ Ξ³ β†’ Ξ±} (h : summable (function.uncurry f)) : βˆ‘' c b, f b c = βˆ‘' b c, f b c := tsum_comm' h h.prod_factor h.prod_symm.prod_factor lemma has_sum.sum_nat_of_sum_int [t2_space Ξ±] {f : β„€ β†’ Ξ±} (hf : has_sum f a) : has_sum (Ξ» n:β„•, f(n + 1) + f(-n.succ)) (a - f 0) := begin obtain ⟨b₁, hβ‚βŸ© : summable (Ξ» n : β„•, f(n + 1)) := hf.summable.comp_injective (Ξ» x₁ xβ‚‚, by simp), obtain ⟨bβ‚‚, hβ‚‚βŸ© : summable (Ξ» n : β„•, f(-n.succ)) := hf.summable.comp_injective (Ξ» x₁ xβ‚‚, by simp), convert h₁.add hβ‚‚, rw hf.unique (h₁.pos_add_zero_add_neg hβ‚‚), abel, end end uniform_group section topological_group variables {G : Type*} [topological_space G] [add_comm_group G] [topological_add_group G] {f : Ξ± β†’ G} lemma summable.vanishing (hf : summable f) ⦃e : set G⦄ (he : e ∈ 𝓝 (0 : G)) : βˆƒ s : finset Ξ±, βˆ€ t, disjoint t s β†’ βˆ‘ k in t, f k ∈ e := begin letI : uniform_space G := topological_add_group.to_uniform_space G, letI : uniform_add_group G := topological_add_comm_group_is_uniform, rcases hf with ⟨y, hy⟩, exact cauchy_seq_finset_iff_vanishing.1 hy.cauchy_seq e he end /-- Series divergence test: if `f` is a convergent series, then `f x` tends to zero along `cofinite`. -/ lemma summable.tendsto_cofinite_zero (hf : summable f) : tendsto f cofinite (𝓝 0) := begin intros e he, rw [filter.mem_map], rcases hf.vanishing he with ⟨s, hs⟩, refine s.eventually_cofinite_nmem.mono (Ξ» x hx, _), by simpa using hs {x} (disjoint_singleton_left.2 hx) end lemma summable.tendsto_at_top_zero {f : β„• β†’ G} (hf : summable f) : tendsto f at_top (𝓝 0) := by { rw ←nat.cofinite_eq_at_top, exact hf.tendsto_cofinite_zero } lemma summable.tendsto_top_of_pos {Ξ± : Type*} [linear_ordered_field Ξ±] [topological_space Ξ±] [order_topology Ξ±] {f : β„• β†’ Ξ±} (hf : summable f⁻¹) (hf' : βˆ€ n, 0 < f n) : tendsto f at_top at_top := begin rw [show f = f⁻¹⁻¹, by { ext, simp }], apply filter.tendsto.inv_tendsto_zero, apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (summable.tendsto_at_top_zero hf), rw eventually_iff_exists_mem, refine ⟨set.Ioi 0, Ioi_mem_at_top _, Ξ» _ _, _⟩, rw [set.mem_Ioi, inv_eq_one_div, one_div, pi.inv_apply, _root_.inv_pos], exact hf' _, end end topological_group section linear_order /-! For infinite sums taking values in a linearly ordered monoid, the existence of a least upper bound for the finite sums is a criterion for summability. This criterion is useful when applied in a linearly ordered monoid which is also a complete or conditionally complete linear order, such as `ℝ`, `ℝβ‰₯0`, `ℝβ‰₯0∞`, because it is then easy to check the existence of a least upper bound. -/ lemma has_sum_of_is_lub_of_nonneg [linear_ordered_add_comm_monoid Ξ²] [topological_space Ξ²] [order_topology Ξ²] {f : Ξ± β†’ Ξ²} (b : Ξ²) (h : βˆ€ b, 0 ≀ f b) (hf : is_lub (set.range (Ξ» s, βˆ‘ a in s, f a)) b) : has_sum f b := tendsto_at_top_is_lub (finset.sum_mono_set_of_nonneg h) hf lemma has_sum_of_is_lub [canonically_linear_ordered_add_monoid Ξ²] [topological_space Ξ²] [order_topology Ξ²] {f : Ξ± β†’ Ξ²} (b : Ξ²) (hf : is_lub (set.range (Ξ» s, βˆ‘ a in s, f a)) b) : has_sum f b := tendsto_at_top_is_lub (finset.sum_mono_set f) hf lemma summable_abs_iff [linear_ordered_add_comm_group Ξ²] [uniform_space Ξ²] [uniform_add_group Ξ²] [complete_space Ξ²] {f : Ξ± β†’ Ξ²} : summable (Ξ» x, |f x|) ↔ summable f := have h1 : βˆ€ x : {x | 0 ≀ f x}, |f x| = f x := Ξ» x, abs_of_nonneg x.2, have h2 : βˆ€ x : {x | 0 ≀ f x}ᢜ, |f x| = -f x := Ξ» x, abs_of_neg (not_le.1 x.2), calc summable (Ξ» x, |f x|) ↔ summable (Ξ» x : {x | 0 ≀ f x}, |f x|) ∧ summable (Ξ» x : {x | 0 ≀ f x}ᢜ, |f x|) : summable_subtype_and_compl.symm ... ↔ summable (Ξ» x : {x | 0 ≀ f x}, f x) ∧ summable (Ξ» x : {x | 0 ≀ f x}ᢜ, -f x) : by simp only [h1, h2] ... ↔ _ : by simp only [summable_neg_iff, summable_subtype_and_compl] alias summable_abs_iff ↔ summable.of_abs summable.abs lemma finite_of_summable_const [linear_ordered_add_comm_group Ξ²] [archimedean Ξ²] [topological_space Ξ²] [order_closed_topology Ξ²] {b : Ξ²} (hb : 0 < b) (hf : summable (Ξ» a : Ξ±, b)) : set.finite (set.univ : set Ξ±) := begin have H : βˆ€ s : finset Ξ±, s.card β€’ b ≀ βˆ‘' a : Ξ±, b, { intros s, simpa using sum_le_has_sum s (Ξ» a ha, hb.le) hf.has_sum }, obtain ⟨n, hn⟩ := archimedean.arch (βˆ‘' a : Ξ±, b) hb, have : βˆ€ s : finset Ξ±, s.card ≀ n, { intros s, simpa [nsmul_le_nsmul_iff hb] using (H s).trans hn }, haveI : fintype Ξ± := fintype_of_finset_card_le n this, exact set.finite_univ end end linear_order section cauchy_seq open filter /-- If the extended distance between consecutive points of a sequence is estimated by a summable series of `nnreal`s, then the original sequence is a Cauchy sequence. -/ lemma cauchy_seq_of_edist_le_of_summable [pseudo_emetric_space Ξ±] {f : β„• β†’ Ξ±} (d : β„• β†’ ℝβ‰₯0) (hf : βˆ€ n, edist (f n) (f n.succ) ≀ d n) (hd : summable d) : cauchy_seq f := begin refine emetric.cauchy_seq_iff_nnreal.2 (Ξ» Ξ΅ Ξ΅pos, _), -- Actually we need partial sums of `d` to be a Cauchy sequence replace hd : cauchy_seq (Ξ» (n : β„•), βˆ‘ x in range n, d x) := let ⟨_, H⟩ := hd in H.tendsto_sum_nat.cauchy_seq, -- Now we take the same `N` as in one of the definitions of a Cauchy sequence refine (metric.cauchy_seq_iff'.1 hd Ξ΅ (nnreal.coe_pos.2 Ξ΅pos)).imp (Ξ» N hN n hn, _), have hsum := hN n hn, -- We simplify the known inequality rw [dist_nndist, nnreal.nndist_eq, ← sum_range_add_sum_Ico _ hn, add_tsub_cancel_left] at hsum, norm_cast at hsum, replace hsum := lt_of_le_of_lt (le_max_left _ _) hsum, rw edist_comm, -- Then use `hf` to simplify the goal to the same form apply lt_of_le_of_lt (edist_le_Ico_sum_of_edist_le hn (Ξ» k _ _, hf k)), assumption_mod_cast end /-- If the distance between consecutive points of a sequence is estimated by a summable series, then the original sequence is a Cauchy sequence. -/ lemma cauchy_seq_of_dist_le_of_summable [pseudo_metric_space Ξ±] {f : β„• β†’ Ξ±} (d : β„• β†’ ℝ) (hf : βˆ€ n, dist (f n) (f n.succ) ≀ d n) (hd : summable d) : cauchy_seq f := begin refine metric.cauchy_seq_iff'.2 (λΡ Ξ΅pos, _), replace hd : cauchy_seq (Ξ» (n : β„•), βˆ‘ x in range n, d x) := let ⟨_, H⟩ := hd in H.tendsto_sum_nat.cauchy_seq, refine (metric.cauchy_seq_iff'.1 hd Ξ΅ Ξ΅pos).imp (Ξ» N hN n hn, _), have hsum := hN n hn, rw [real.dist_eq, ← sum_Ico_eq_sub _ hn] at hsum, calc dist (f n) (f N) = dist (f N) (f n) : dist_comm _ _ ... ≀ βˆ‘ x in Ico N n, d x : dist_le_Ico_sum_of_dist_le hn (Ξ» k _ _, hf k) ... ≀ |βˆ‘ x in Ico N n, d x| : le_abs_self _ ... < Ξ΅ : hsum end lemma cauchy_seq_of_summable_dist [pseudo_metric_space Ξ±] {f : β„• β†’ Ξ±} (h : summable (Ξ»n, dist (f n) (f n.succ))) : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ (Ξ» _, le_rfl) h lemma dist_le_tsum_of_dist_le_of_tendsto [pseudo_metric_space Ξ±] {f : β„• β†’ Ξ±} (d : β„• β†’ ℝ) (hf : βˆ€ n, dist (f n) (f n.succ) ≀ d n) (hd : summable d) {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) (n : β„•) : dist (f n) a ≀ βˆ‘' m, d (n + m) := begin refine le_of_tendsto (tendsto_const_nhds.dist ha) (eventually_at_top.2 ⟨n, Ξ» m hnm, _⟩), refine le_trans (dist_le_Ico_sum_of_dist_le hnm (Ξ» k _ _, hf k)) _, rw [sum_Ico_eq_sum_range], refine sum_le_tsum (range _) (Ξ» _ _, le_trans dist_nonneg (hf _)) _, exact hd.comp_injective (add_right_injective n) end lemma dist_le_tsum_of_dist_le_of_tendstoβ‚€ [pseudo_metric_space Ξ±] {f : β„• β†’ Ξ±} (d : β„• β†’ ℝ) (hf : βˆ€ n, dist (f n) (f n.succ) ≀ d n) (hd : summable d) {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≀ tsum d := by simpa only [zero_add] using dist_le_tsum_of_dist_le_of_tendsto d hf hd ha 0 lemma dist_le_tsum_dist_of_tendsto [pseudo_metric_space Ξ±] {f : β„• β†’ Ξ±} (h : summable (Ξ»n, dist (f n) (f n.succ))) {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) (n) : dist (f n) a ≀ βˆ‘' m, dist (f (n+m)) (f (n+m).succ) := show dist (f n) a ≀ βˆ‘' m, (Ξ»x, dist (f x) (f x.succ)) (n + m), from dist_le_tsum_of_dist_le_of_tendsto (Ξ» n, dist (f n) (f n.succ)) (Ξ» _, le_rfl) h ha n lemma dist_le_tsum_dist_of_tendstoβ‚€ [pseudo_metric_space Ξ±] {f : β„• β†’ Ξ±} (h : summable (Ξ»n, dist (f n) (f n.succ))) {a : Ξ±} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≀ βˆ‘' n, dist (f n) (f n.succ) := by simpa only [zero_add] using dist_le_tsum_dist_of_tendsto h ha 0 end cauchy_seq /-! ## Multipliying two infinite sums In this section, we prove various results about `(βˆ‘' x : Ξ², f x) * (βˆ‘' y : Ξ³, g y)`. Note that we always assume that the family `Ξ» x : Ξ² Γ— Ξ³, f x.1 * g x.2` is summable, since there is no way to deduce this from the summmabilities of `f` and `g` in general, but if you are working in a normed space, you may want to use the analogous lemmas in `analysis/normed_space/basic` (e.g `tsum_mul_tsum_of_summable_norm`). We first establish results about arbitrary index types, `Ξ²` and `Ξ³`, and then we specialize to `Ξ² = Ξ³ = β„•` to prove the Cauchy product formula (see `tsum_mul_tsum_eq_tsum_sum_antidiagonal`). ### Arbitrary index types -/ section tsum_mul_tsum variables [topological_space Ξ±] [t3_space Ξ±] [non_unital_non_assoc_semiring Ξ±] [topological_semiring Ξ±] {f : Ξ² β†’ Ξ±} {g : Ξ³ β†’ Ξ±} {s t u : Ξ±} lemma has_sum.mul_eq (hf : has_sum f s) (hg : has_sum g t) (hfg : has_sum (Ξ» (x : Ξ² Γ— Ξ³), f x.1 * g x.2) u) : s * t = u := have key₁ : has_sum (Ξ» b, f b * t) (s * t), from hf.mul_right t, have this : βˆ€ b : Ξ², has_sum (Ξ» c : Ξ³, f b * g c) (f b * t), from Ξ» b, hg.mul_left (f b), have keyβ‚‚ : has_sum (Ξ» b, f b * t) u, from has_sum.prod_fiberwise hfg this, key₁.unique keyβ‚‚ lemma has_sum.mul (hf : has_sum f s) (hg : has_sum g t) (hfg : summable (Ξ» (x : Ξ² Γ— Ξ³), f x.1 * g x.2)) : has_sum (Ξ» (x : Ξ² Γ— Ξ³), f x.1 * g x.2) (s * t) := let ⟨u, hu⟩ := hfg in (hf.mul_eq hg hu).symm β–Έ hu /-- Product of two infinites sums indexed by arbitrary types. See also `tsum_mul_tsum_of_summable_norm` if `f` and `g` are abolutely summable. -/ lemma tsum_mul_tsum (hf : summable f) (hg : summable g) (hfg : summable (Ξ» (x : Ξ² Γ— Ξ³), f x.1 * g x.2)) : (βˆ‘' x, f x) * (βˆ‘' y, g y) = (βˆ‘' z : Ξ² Γ— Ξ³, f z.1 * g z.2) := hf.has_sum.mul_eq hg.has_sum hfg.has_sum end tsum_mul_tsum section cauchy_product /-! ### `β„•`-indexed families (Cauchy product) We prove two versions of the Cauchy product formula. The first one is `tsum_mul_tsum_eq_tsum_sum_range`, where the `n`-th term is a sum over `finset.range (n+1)` involving `nat` substraction. In order to avoid `nat` substraction, we also provide `tsum_mul_tsum_eq_tsum_sum_antidiagonal`, where the `n`-th term is a sum over all pairs `(k, l)` such that `k+l=n`, which corresponds to the `finset` `finset.nat.antidiagonal n` -/ variables {f : β„• β†’ Ξ±} {g : β„• β†’ Ξ±} open finset variables [topological_space Ξ±] [non_unital_non_assoc_semiring Ξ±] /- The family `(k, l) : β„• Γ— β„• ↦ f k * g l` is summable if and only if the family `(n, k, l) : Ξ£ (n : β„•), nat.antidiagonal n ↦ f k * g l` is summable. -/ lemma summable_mul_prod_iff_summable_mul_sigma_antidiagonal {f g : β„• β†’ Ξ±} : summable (Ξ» x : β„• Γ— β„•, f x.1 * g x.2) ↔ summable (Ξ» x : (Ξ£ (n : β„•), nat.antidiagonal n), f (x.2 : β„• Γ— β„•).1 * g (x.2 : β„• Γ— β„•).2) := nat.sigma_antidiagonal_equiv_prod.summable_iff.symm variables [t3_space Ξ±] [topological_semiring Ξ±] lemma summable_sum_mul_antidiagonal_of_summable_mul {f g : β„• β†’ Ξ±} (h : summable (Ξ» x : β„• Γ— β„•, f x.1 * g x.2)) : summable (Ξ» n, βˆ‘ kl in nat.antidiagonal n, f kl.1 * g kl.2) := begin rw summable_mul_prod_iff_summable_mul_sigma_antidiagonal at h, conv {congr, funext, rw [← finset.sum_finset_coe, ← tsum_fintype]}, exact h.sigma' (Ξ» n, (has_sum_fintype _).summable), end /-- The Cauchy product formula for the product of two infinites sums indexed by `β„•`, expressed by summing on `finset.nat.antidiagonal`. See also `tsum_mul_tsum_eq_tsum_sum_antidiagonal_of_summable_norm` if `f` and `g` are absolutely summable. -/ lemma tsum_mul_tsum_eq_tsum_sum_antidiagonal (hf : summable f) (hg : summable g) (hfg : summable (Ξ» (x : β„• Γ— β„•), f x.1 * g x.2)) : (βˆ‘' n, f n) * (βˆ‘' n, g n) = (βˆ‘' n, βˆ‘ kl in nat.antidiagonal n, f kl.1 * g kl.2) := begin conv_rhs {congr, funext, rw [← finset.sum_finset_coe, ← tsum_fintype]}, rw [tsum_mul_tsum hf hg hfg, ← nat.sigma_antidiagonal_equiv_prod.tsum_eq (_ : β„• Γ— β„• β†’ Ξ±)], exact tsum_sigma' (Ξ» n, (has_sum_fintype _).summable) (summable_mul_prod_iff_summable_mul_sigma_antidiagonal.mp hfg) end lemma summable_sum_mul_range_of_summable_mul {f g : β„• β†’ Ξ±} (h : summable (Ξ» x : β„• Γ— β„•, f x.1 * g x.2)) : summable (Ξ» n, βˆ‘ k in range (n+1), f k * g (n - k)) := begin simp_rw ← nat.sum_antidiagonal_eq_sum_range_succ (Ξ» k l, f k * g l), exact summable_sum_mul_antidiagonal_of_summable_mul h end /-- The Cauchy product formula for the product of two infinites sums indexed by `β„•`, expressed by summing on `finset.range`. See also `tsum_mul_tsum_eq_tsum_sum_range_of_summable_norm` if `f` and `g` are absolutely summable. -/ lemma tsum_mul_tsum_eq_tsum_sum_range (hf : summable f) (hg : summable g) (hfg : summable (Ξ» (x : β„• Γ— β„•), f x.1 * g x.2)) : (βˆ‘' n, f n) * (βˆ‘' n, g n) = (βˆ‘' n, βˆ‘ k in range (n+1), f k * g (n - k)) := begin simp_rw ← nat.sum_antidiagonal_eq_sum_range_succ (Ξ» k l, f k * g l), exact tsum_mul_tsum_eq_tsum_sum_antidiagonal hf hg hfg end end cauchy_product
5d989edb13b0c13f044e4e8a06fc21c17f797d9a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/exterior_algebra/basic.lean
ddde814780643f99443b1cc5588462492e57e39f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
11,499
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhangir Azerbayev, Adam Topaz, Eric Wieser -/ import linear_algebra.clifford_algebra.basic import linear_algebra.alternating /-! # Exterior Algebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We construct the exterior algebra of a module `M` over a commutative semiring `R`. ## Notation The exterior algebra of the `R`-module `M` is denoted as `exterior_algebra R M`. It is endowed with the structure of an `R`-algebra. Given a linear morphism `f : M β†’ A` from a module `M` to another `R`-algebra `A`, such that `cond : βˆ€ m : M, f m * f m = 0`, there is a (unique) lift of `f` to an `R`-algebra morphism, which is denoted `exterior_algebra.lift R f cond`. The canonical linear map `M β†’ exterior_algebra R M` is denoted `exterior_algebra.ΞΉ R`. ## Theorems The main theorems proved ensure that `exterior_algebra R M` satisfies the universal property of the exterior algebra. 1. `ΞΉ_comp_lift` is fact that the composition of `ΞΉ R` with `lift R f cond` agrees with `f`. 2. `lift_unique` ensures the uniqueness of `lift R f cond` with respect to 1. ## Definitions * `ΞΉ_multi` is the `alternating_map` corresponding to the wedge product of `ΞΉ R m` terms. ## Implementation details The exterior algebra of `M` is constructed as simply `clifford_algebra (0 : quadratic_form R M)`, as this avoids us having to duplicate API. -/ universes u1 u2 u3 variables (R : Type u1) [comm_ring R] variables (M : Type u2) [add_comm_group M] [module R M] /-- The exterior algebra of an `R`-module `M`. -/ @[reducible] def exterior_algebra := clifford_algebra (0 : quadratic_form R M) namespace exterior_algebra variables {M} /-- The canonical linear map `M β†’β‚—[R] exterior_algebra R M`. -/ @[reducible] def ΞΉ : M β†’β‚—[R] exterior_algebra R M := by exact clifford_algebra.ΞΉ _ variables {R} /-- As well as being linear, `ΞΉ m` squares to zero -/ @[simp] theorem ΞΉ_sq_zero (m : M) : (ΞΉ R m) * (ΞΉ R m) = 0 := (clifford_algebra.ΞΉ_sq_scalar _ m).trans $ map_zero _ variables {A : Type*} [semiring A] [algebra R A] @[simp] theorem comp_ΞΉ_sq_zero (g : exterior_algebra R M →ₐ[R] A) (m : M) : g (ΞΉ R m) * g (ΞΉ R m) = 0 := by rw [←alg_hom.map_mul, ΞΉ_sq_zero, alg_hom.map_zero] variables (R) /-- Given a linear map `f : M β†’β‚—[R] A` into an `R`-algebra `A`, which satisfies the condition: `cond : βˆ€ m : M, f m * f m = 0`, this is the canonical lift of `f` to a morphism of `R`-algebras from `exterior_algebra R M` to `A`. -/ @[simps symm_apply] def lift : {f : M β†’β‚—[R] A // βˆ€ m, f m * f m = 0} ≃ (exterior_algebra R M →ₐ[R] A) := equiv.trans (equiv.subtype_equiv (equiv.refl _) $ by simp) $ clifford_algebra.lift _ @[simp] theorem ΞΉ_comp_lift (f : M β†’β‚—[R] A) (cond : βˆ€ m, f m * f m = 0) : (lift R ⟨f, cond⟩).to_linear_map.comp (ΞΉ R) = f := clifford_algebra.ΞΉ_comp_lift f _ @[simp] theorem lift_ΞΉ_apply (f : M β†’β‚—[R] A) (cond : βˆ€ m, f m * f m = 0) (x) : lift R ⟨f, cond⟩ (ΞΉ R x) = f x := clifford_algebra.lift_ΞΉ_apply f _ x @[simp] theorem lift_unique (f : M β†’β‚—[R] A) (cond : βˆ€ m, f m * f m = 0) (g : exterior_algebra R M →ₐ[R] A) : g.to_linear_map.comp (ΞΉ R) = f ↔ g = lift R ⟨f, cond⟩ := clifford_algebra.lift_unique f _ _ variables {R M} @[simp] theorem lift_comp_ΞΉ (g : exterior_algebra R M →ₐ[R] A) : lift R ⟨g.to_linear_map.comp (ΞΉ R), comp_ΞΉ_sq_zero _⟩ = g := clifford_algebra.lift_comp_ΞΉ g /-- See note [partially-applied ext lemmas]. -/ @[ext] theorem hom_ext {f g : exterior_algebra R M →ₐ[R] A} (h : f.to_linear_map.comp (ΞΉ R) = g.to_linear_map.comp (ΞΉ R)) : f = g := clifford_algebra.hom_ext h /-- If `C` holds for the `algebra_map` of `r : R` into `exterior_algebra R M`, the `ΞΉ` of `x : M`, and is preserved under addition and muliplication, then it holds for all of `exterior_algebra R M`. -/ @[elab_as_eliminator] lemma induction {C : exterior_algebra R M β†’ Prop} (h_grade0 : βˆ€ r, C (algebra_map R (exterior_algebra R M) r)) (h_grade1 : βˆ€ x, C (ΞΉ R x)) (h_mul : βˆ€ a b, C a β†’ C b β†’ C (a * b)) (h_add : βˆ€ a b, C a β†’ C b β†’ C (a + b)) (a : exterior_algebra R M) : C a := clifford_algebra.induction h_grade0 h_grade1 h_mul h_add a /-- The left-inverse of `algebra_map`. -/ def algebra_map_inv : exterior_algebra R M →ₐ[R] R := exterior_algebra.lift R ⟨(0 : M β†’β‚—[R] R), Ξ» m, by simp⟩ variables (M) lemma algebra_map_left_inverse : function.left_inverse algebra_map_inv (algebra_map R $ exterior_algebra R M) := Ξ» x, by simp [algebra_map_inv] @[simp] lemma algebra_map_inj (x y : R) : algebra_map R (exterior_algebra R M) x = algebra_map R (exterior_algebra R M) y ↔ x = y := (algebra_map_left_inverse M).injective.eq_iff @[simp] lemma algebra_map_eq_zero_iff (x : R) : algebra_map R (exterior_algebra R M) x = 0 ↔ x = 0 := map_eq_zero_iff (algebra_map _ _) (algebra_map_left_inverse _).injective @[simp] lemma algebra_map_eq_one_iff (x : R) : algebra_map R (exterior_algebra R M) x = 1 ↔ x = 1 := map_eq_one_iff (algebra_map _ _) (algebra_map_left_inverse _).injective lemma is_unit_algebra_map (r : R) : is_unit (algebra_map R (exterior_algebra R M) r) ↔ is_unit r := is_unit_map_of_left_inverse _ (algebra_map_left_inverse M) /-- Invertibility in the exterior algebra is the same as invertibility of the base ring. -/ @[simps] def invertible_algebra_map_equiv (r : R) : invertible (algebra_map R (exterior_algebra R M) r) ≃ invertible r := invertible_equiv_of_left_inverse _ _ _ (algebra_map_left_inverse M) variables {M} /-- The canonical map from `exterior_algebra R M` into `triv_sq_zero_ext R M` that sends `exterior_algebra.ΞΉ` to `triv_sq_zero_ext.inr`. -/ def to_triv_sq_zero_ext [module Rᡐᡒᡖ M] [is_central_scalar R M] : exterior_algebra R M →ₐ[R] triv_sq_zero_ext R M := lift R ⟨triv_sq_zero_ext.inr_hom R M, Ξ» m, triv_sq_zero_ext.inr_mul_inr R m m⟩ @[simp] lemma to_triv_sq_zero_ext_ΞΉ [module Rᡐᡒᡖ M] [is_central_scalar R M] (x : M) : to_triv_sq_zero_ext (ΞΉ R x) = triv_sq_zero_ext.inr x := lift_ΞΉ_apply _ _ _ _ /-- The left-inverse of `ΞΉ`. As an implementation detail, we implement this using `triv_sq_zero_ext` which has a suitable algebra structure. -/ def ΞΉ_inv : exterior_algebra R M β†’β‚—[R] M := begin letI : module Rᡐᡒᡖ M := module.comp_hom _ ((ring_hom.id R).from_opposite mul_comm), haveI : is_central_scalar R M := ⟨λ r m, rfl⟩, exact (triv_sq_zero_ext.snd_hom R M).comp to_triv_sq_zero_ext.to_linear_map end lemma ΞΉ_left_inverse : function.left_inverse ΞΉ_inv (ΞΉ R : M β†’ exterior_algebra R M) := Ξ» x, by simp [ΞΉ_inv] variables (R) @[simp] lemma ΞΉ_inj (x y : M) : ΞΉ R x = ΞΉ R y ↔ x = y := ΞΉ_left_inverse.injective.eq_iff variables {R} @[simp] lemma ΞΉ_eq_zero_iff (x : M) : ΞΉ R x = 0 ↔ x = 0 := by rw [←ι_inj R x 0, linear_map.map_zero] @[simp] lemma ΞΉ_eq_algebra_map_iff (x : M) (r : R) : ΞΉ R x = algebra_map R _ r ↔ x = 0 ∧ r = 0 := begin refine ⟨λ h, _, _⟩, { letI : module Rᡐᡒᡖ M := module.comp_hom _ ((ring_hom.id R).from_opposite mul_comm), haveI : is_central_scalar R M := ⟨λ r m, rfl⟩, have hf0 : to_triv_sq_zero_ext (ΞΉ R x) = (0, x), from to_triv_sq_zero_ext_ΞΉ _, rw [h, alg_hom.commutes] at hf0, have : r = 0 ∧ 0 = x := prod.ext_iff.1 hf0, exact this.symm.imp_left eq.symm, }, { rintro ⟨rfl, rfl⟩, rw [linear_map.map_zero, ring_hom.map_zero] } end @[simp] lemma ΞΉ_ne_one [nontrivial R] (x : M) : ΞΉ R x β‰  1 := begin rw [←(algebra_map R (exterior_algebra R M)).map_one, ne.def, ΞΉ_eq_algebra_map_iff], exact one_ne_zero ∘ and.right, end /-- The generators of the exterior algebra are disjoint from its scalars. -/ lemma ΞΉ_range_disjoint_one : disjoint (linear_map.range (ΞΉ R : M β†’β‚—[R] exterior_algebra R M)) (1 : submodule R (exterior_algebra R M)) := begin rw submodule.disjoint_def, rintros _ ⟨x, hx⟩ ⟨r, (rfl : algebra_map _ _ _ = _)⟩, rw ΞΉ_eq_algebra_map_iff x at hx, rw [hx.2, ring_hom.map_zero] end @[simp] lemma ΞΉ_add_mul_swap (x y : M) : ΞΉ R x * ΞΉ R y + ΞΉ R y * ΞΉ R x = 0 := calc _ = ΞΉ R (x + y) * ΞΉ R (x + y) : by simp [mul_add, add_mul] ... = _ : ΞΉ_sq_zero _ lemma ΞΉ_mul_prod_list {n : β„•} (f : fin n β†’ M) (i : fin n) : (ΞΉ R $ f i) * (list.of_fn $ Ξ» i, ΞΉ R $ f i).prod = 0 := begin induction n with n hn, { exact i.elim0, }, { rw [list.of_fn_succ, list.prod_cons, ←mul_assoc], by_cases h : i = 0, { rw [h, ΞΉ_sq_zero, zero_mul], }, { replace hn := congr_arg ((*) $ ΞΉ R $ f 0) (hn (Ξ» i, f $ fin.succ i) (i.pred h)), simp only at hn, rw [fin.succ_pred, ←mul_assoc, mul_zero] at hn, refine (eq_zero_iff_eq_zero_of_add_eq_zero _).mp hn, rw [← add_mul, ΞΉ_add_mul_swap, zero_mul], } } end variables (R) /-- The product of `n` terms of the form `ΞΉ R m` is an alternating map. This is a special case of `multilinear_map.mk_pi_algebra_fin`, and the exterior algebra version of `tensor_algebra.tprod`. -/ def ΞΉ_multi (n : β„•) : alternating_map R M (exterior_algebra R M) (fin n) := let F := (multilinear_map.mk_pi_algebra_fin R n (exterior_algebra R M)).comp_linear_map (Ξ» i, ΞΉ R) in { map_eq_zero_of_eq' := Ξ» f x y hfxy hxy, begin rw [multilinear_map.comp_linear_map_apply, multilinear_map.mk_pi_algebra_fin_apply], clear F, wlog h : x < y, { exact this n f y x hfxy.symm hxy.symm (hxy.lt_or_lt.resolve_left h), }, clear hxy, induction n with n hn, { exact x.elim0, }, { rw [list.of_fn_succ, list.prod_cons], by_cases hx : x = 0, -- one of the repeated terms is on the left { rw hx at hfxy h, rw [hfxy, ←fin.succ_pred y (ne_of_lt h).symm], exact ΞΉ_mul_prod_list (f ∘ fin.succ) _, }, -- ignore the left-most term and induct on the remaining ones, decrementing indices { convert mul_zero _, refine hn (Ξ» i, f $ fin.succ i) (x.pred hx) (y.pred (ne_of_lt $ lt_of_le_of_lt x.zero_le h).symm) _ (fin.pred_lt_pred_iff.mpr h), simp only [fin.succ_pred], exact hfxy, } } end, to_fun := F, ..F} variables {R} lemma ΞΉ_multi_apply {n : β„•} (v : fin n β†’ M) : ΞΉ_multi R n v = (list.of_fn $ Ξ» i, ΞΉ R (v i)).prod := rfl @[simp] lemma ΞΉ_multi_zero_apply (v : fin 0 β†’ M) : ΞΉ_multi R 0 v = 1 := rfl @[simp] lemma ΞΉ_multi_succ_apply {n : β„•} (v : fin n.succ β†’ M) : ΞΉ_multi R _ v = ΞΉ R (v 0) * ΞΉ_multi R _ (matrix.vec_tail v):= (congr_arg list.prod (list.of_fn_succ _)).trans list.prod_cons lemma ΞΉ_multi_succ_curry_left {n : β„•} (m : M) : (ΞΉ_multi R n.succ).curry_left m = (linear_map.mul_left R (ΞΉ R m)).comp_alternating_map (ΞΉ_multi R n) := alternating_map.ext $ Ξ» v, (ΞΉ_multi_succ_apply _).trans $ begin simp_rw matrix.tail_cons, refl, end end exterior_algebra namespace tensor_algebra variables {R M} /-- The canonical image of the `tensor_algebra` in the `exterior_algebra`, which maps `tensor_algebra.ΞΉ R x` to `exterior_algebra.ΞΉ R x`. -/ def to_exterior : tensor_algebra R M →ₐ[R] exterior_algebra R M := tensor_algebra.lift R (exterior_algebra.ΞΉ R : M β†’β‚—[R] exterior_algebra R M) @[simp] lemma to_exterior_ΞΉ (m : M) : (tensor_algebra.ΞΉ R m).to_exterior = exterior_algebra.ΞΉ R m := by simp [to_exterior] end tensor_algebra
85fcf65b5a043936410f04d2ff3391c3bbcfaf84
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/set/bool_indicator.lean
3a7718dc53a3385d7c5979f33e4ec9ef769283c3
[ "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
1,696
lean
/- Copyright (c) 2022 Dagur TΓ³mas Ásgeirsson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Dagur TΓ³mas Ásgeirsson, Leonardo de Moura -/ import data.set.image /-! # Indicator function valued in bool > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. See also `set.indicator` and `set.piecewise`. -/ open bool namespace set variables {Ξ± : Type*} (s : set Ξ±) /-- `bool_indicator` maps `x` to `tt` if `x ∈ s`, else to `ff` -/ noncomputable def bool_indicator (x : Ξ±) := @ite _ (x ∈ s) (classical.prop_decidable _) tt ff lemma mem_iff_bool_indicator (x : Ξ±) : x ∈ s ↔ s.bool_indicator x = tt := by { unfold bool_indicator, split_ifs ; tauto } lemma not_mem_iff_bool_indicator (x : Ξ±) : x βˆ‰ s ↔ s.bool_indicator x = ff := by { unfold bool_indicator, split_ifs ; tauto } lemma preimage_bool_indicator_tt : s.bool_indicator ⁻¹' {tt} = s := ext (Ξ» x, (s.mem_iff_bool_indicator x).symm) lemma preimage_bool_indicator_ff : s.bool_indicator ⁻¹' {ff} = sᢜ := ext (Ξ» x, (s.not_mem_iff_bool_indicator x).symm) open_locale classical lemma preimage_bool_indicator_eq_union (t : set bool) : s.bool_indicator ⁻¹' t = (if tt ∈ t then s else βˆ…) βˆͺ (if ff ∈ t then sᢜ else βˆ…) := begin ext x, dsimp [bool_indicator], split_ifs ; tauto end lemma preimage_bool_indicator (t : set bool) : s.bool_indicator ⁻¹' t = univ ∨ s.bool_indicator ⁻¹' t = s ∨ s.bool_indicator ⁻¹' t = sᢜ ∨ s.bool_indicator ⁻¹' t = βˆ… := begin simp only [preimage_bool_indicator_eq_union], split_ifs ; simp [s.union_compl_self] end end set
a1c7244321818cb8e752b463a752e5fb982d83f9
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/real/ennreal.lean
36deb7d0fbd7a1176991061bd43465f8351430dd
[ "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
79,601
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 /-! # Extended non-negative reals We define `ennreal = ℝβ‰₯0∞ := with_top ℝβ‰₯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 ∘ real.to_nnreal` 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∞`. -/ 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 [ has_zero, add_comm_monoid_with_one, canonically_ordered_comm_semiring, complete_linear_order, densely_ordered, nontrivial, canonically_linear_ordered_add_monoid, has_sub, has_ordered_sub, linear_ordered_add_comm_monoid_with_top]] def ennreal := with_top ℝβ‰₯0 localized "notation `ℝβ‰₯0∞` := ennreal" in ennreal localized "notation `∞` := (⊀ : ennreal)" in ennreal namespace ennreal variables {a b c d : ℝβ‰₯0∞} {r p q : ℝβ‰₯0} -- TODO: why are the two covariant instances necessary? why aren't they inferred? instance covariant_class_mul_le : covariant_class ℝβ‰₯0∞ ℝβ‰₯0∞ (*) (≀) := canonically_ordered_comm_semiring.to_covariant_mul_le instance covariant_class_add_le : covariant_class ℝβ‰₯0∞ ℝβ‰₯0∞ (+) (≀) := ordered_add_comm_monoid.to_covariant_class_left ℝβ‰₯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 noncomputable def of_real (r : real) : ℝβ‰₯0∞ := coe (real.to_nnreal 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, real.coe_to_nnreal _ 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_rfl | none := le_top lemma coe_nnreal_eq (r : ℝβ‰₯0) : (r : ℝβ‰₯0∞) = ennreal.of_real r := by { rw [ennreal.of_real, real.to_nnreal], 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] }, { rintro (rfl : x = 0), exact or.inl rfl }, end, by rintro (h | 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 lemma coe_ne_zero : (r : ℝβ‰₯0∞) β‰  0 ↔ r β‰  0 := not_congr coe_eq_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_comm_semiring.zero_lt_one @[simp] lemma one_lt_two : (1 : ℝβ‰₯0∞) < 2 := coe_one β–Έ coe_two β–Έ by exact_mod_cast (@one_lt_two β„• _ _) lemma one_le_two : (1 : ℝβ‰₯0∞) ≀ 2 := one_lt_two.le @[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 /-- `(1 : ℝβ‰₯0∞) ≀ 1`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_one_ennreal : fact ((1 : ℝβ‰₯0∞) ≀ 1) := ⟨le_rfl⟩ /-- `(1 : ℝβ‰₯0∞) ≀ 2`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_two_ennreal : fact ((1 : ℝβ‰₯0∞) ≀ 2) := ⟨ennreal.coe_le_coe.2 (show (1 : ℝβ‰₯0) ≀ 2, by norm_num)⟩ /-- `(1 : ℝβ‰₯0∞) ≀ ∞`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_top_ennreal : fact ((1 : ℝβ‰₯0∞) ≀ ∞) := ⟨le_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 $ ne_top_equiv_nnreal.symm.surjective.infi_congr _$ Ξ» 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 Ξ±α΅’α΅ˆ _ _ lemma supr_ne_top [complete_lattice Ξ±] (f : ℝβ‰₯0∞ β†’ Ξ±) : (⨆ x β‰  ∞, f x) = ⨆ x : ℝβ‰₯0, f x := @infi_ne_top Ξ±α΅’α΅ˆ _ _ 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 ⟨λ r, inf_le_of_left_le $ infi_le _ _, inf_le_right⟩) lemma supr_ennreal {Ξ± : Type*} [complete_lattice Ξ±] {f : ℝβ‰₯0∞ β†’ Ξ±} : (⨆ n, f n) = (⨆ n : ℝβ‰₯0, f n) βŠ” f ∞ := @infi_ennreal Ξ±α΅’α΅ˆ _ _ @[simp] lemma add_top : a + ∞ = ∞ := add_top _ @[simp] lemma top_add : ∞ + a = ∞ := 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 section actions /-- A `mul_action` over `ℝβ‰₯0∞` restricts to a `mul_action` over `ℝβ‰₯0`. -/ noncomputable instance {M : Type*} [mul_action ℝβ‰₯0∞ M] : mul_action ℝβ‰₯0 M := mul_action.comp_hom M of_nnreal_hom.to_monoid_hom lemma smul_def {M : Type*} [mul_action ℝβ‰₯0∞ M] (c : ℝβ‰₯0) (x : M) : c β€’ x = (c : ℝβ‰₯0∞) β€’ x := rfl instance {M N : Type*} [mul_action ℝβ‰₯0∞ M] [mul_action ℝβ‰₯0∞ N] [has_smul M N] [is_scalar_tower ℝβ‰₯0∞ M N] : is_scalar_tower ℝβ‰₯0 M N := { smul_assoc := Ξ» r, (smul_assoc (r : ℝβ‰₯0∞) : _)} instance smul_comm_class_left {M N : Type*} [mul_action ℝβ‰₯0∞ N] [has_smul M N] [smul_comm_class ℝβ‰₯0∞ M N] : smul_comm_class ℝβ‰₯0 M N := { smul_comm := Ξ» r, (smul_comm (r : ℝβ‰₯0∞) : _)} instance smul_comm_class_right {M N : Type*} [mul_action ℝβ‰₯0∞ N] [has_smul M N] [smul_comm_class M ℝβ‰₯0∞ N] : smul_comm_class M ℝβ‰₯0 N := { smul_comm := Ξ» m r, (smul_comm m (r : ℝβ‰₯0∞) : _)} /-- A `distrib_mul_action` over `ℝβ‰₯0∞` restricts to a `distrib_mul_action` over `ℝβ‰₯0`. -/ noncomputable instance {M : Type*} [add_monoid M] [distrib_mul_action ℝβ‰₯0∞ M] : distrib_mul_action ℝβ‰₯0 M := distrib_mul_action.comp_hom M of_nnreal_hom.to_monoid_hom /-- A `module` over `ℝβ‰₯0∞` restricts to a `module` over `ℝβ‰₯0`. -/ noncomputable instance {M : Type*} [add_comm_monoid M] [module ℝβ‰₯0∞ M] : module ℝβ‰₯0 M := module.comp_hom M of_nnreal_hom /-- An `algebra` over `ℝβ‰₯0∞` restricts to an `algebra` over `ℝβ‰₯0`. -/ noncomputable instance {A : Type*} [semiring A] [algebra ℝβ‰₯0∞ A] : algebra ℝβ‰₯0 A := { smul := (β€’), commutes' := Ξ» r x, by simp [algebra.commutes], smul_def' := Ξ» r x, by simp [←algebra.smul_def (r : ℝβ‰₯0∞) x, smul_def], to_ring_hom := ((algebra_map ℝβ‰₯0∞ A).comp (of_nnreal_hom : ℝβ‰₯0 β†’+* ℝβ‰₯0∞)) } -- verify that the above produces instances we might care about noncomputable example : algebra ℝβ‰₯0 ℝβ‰₯0∞ := infer_instance noncomputable example : distrib_mul_action ℝβ‰₯0Λ£ ℝβ‰₯0∞ := infer_instance lemma coe_smul {R} (r : R) (s : ℝβ‰₯0) [has_smul R ℝβ‰₯0] [has_smul R ℝβ‰₯0∞] [is_scalar_tower R ℝβ‰₯0 ℝβ‰₯0] [is_scalar_tower R ℝβ‰₯0 ℝβ‰₯0∞] : (↑(r β€’ s) : ℝβ‰₯0∞) = r β€’ ↑s := by rw [←smul_one_smul ℝβ‰₯0 r (s: ℝβ‰₯0∞), smul_def, smul_eq_mul, ←ennreal.coe_mul, smul_mul_assoc, one_mul] end actions @[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 := by { lift r₁ to ℝβ‰₯0 using h₁, lift rβ‚‚ to ℝβ‰₯0 using hβ‚‚, refl } 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 lt_top_of_mul_ne_top_left (h : a * b β‰  ∞) (hb : b β‰  0) : a < ∞ := lt_top_iff_ne_top.2 $ Ξ» ha, h $ mul_eq_top.2 (or.inr ⟨ha, hb⟩) lemma lt_top_of_mul_ne_top_right (h : a * b β‰  ∞) (ha : a β‰  0) : b < ∞ := lt_top_of_mul_ne_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_ne_top_left h.ne hb, lt_top_of_mul_ne_top_right h.ne ha⟩ }, { rintro (⟨ha, hb⟩|rfl|rfl); [exact mul_lt_top ha.ne hb.ne, 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 } lemma mul_pos_iff : 0 < a * b ↔ 0 < a ∧ 0 < b := canonically_ordered_comm_semiring.mul_pos lemma mul_pos (ha : a β‰  0) (hb : b β‰  0) : 0 < a * b := mul_pos_iff.2 ⟨pos_iff_ne_zero.2 ha, pos_iff_ne_zero.2 hb⟩ @[simp] lemma pow_eq_top_iff {n : β„•} : a ^ n = ∞ ↔ a = ∞ ∧ n β‰  0 := begin induction n with n ihn, { simp }, rw [pow_succ, mul_eq_top, ihn], fsplit, { rintro (⟨-,rfl,h0⟩|⟨rfl,h0⟩); exact ⟨rfl, n.succ_ne_zero⟩ }, { rintro ⟨rfl, -⟩, exact or.inr ⟨rfl, pow_ne_zero n top_ne_zero⟩ } end lemma pow_eq_top (n : β„•) (h : a ^ n = ∞) : a = ∞ := (pow_eq_top_iff.1 h).1 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 @[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 @[simp, norm_cast] lemma to_nnreal_nat (n : β„•) : (n : ℝβ‰₯0∞).to_nnreal = n := by conv_lhs { rw [← ennreal.coe_nat n, ennreal.to_nnreal_coe] } @[simp, norm_cast] lemma to_real_nat (n : β„•) : (n : ℝβ‰₯0∞).to_real = n := by conv_lhs { rw [← ennreal.of_real_coe_nat n, ennreal.to_real_of_real (nat.cast_nonneg _)] } 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_rfl }, { 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 lemma one_le_pow_of_one_le (ha : 1 ≀ a) (n : β„•) : 1 ≀ a ^ n := by simpa using pow_le_pow ha (zero_le n) @[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) @[simp] lemma sup_eq_max : a βŠ” b = max a b := rfl protected lemma pow_pos : 0 < a β†’ βˆ€ n : β„•, 0 < a^n := canonically_ordered_comm_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 protected lemma le_of_add_le_add_left : a β‰  ∞ β†’ a + b ≀ a + c β†’ b ≀ c := with_top.le_of_add_le_add_left protected lemma le_of_add_le_add_right : a β‰  ∞ β†’ b + a ≀ c + a β†’ b ≀ c := with_top.le_of_add_le_add_right protected lemma add_lt_add_left : a β‰  ∞ β†’ b < c β†’ a + b < a + c := with_top.add_lt_add_left protected lemma add_lt_add_right : a β‰  ∞ β†’ b < c β†’ b + a < c + a := with_top.add_lt_add_right protected lemma add_le_add_iff_left : a β‰  ∞ β†’ (a + b ≀ a + c ↔ b ≀ c) := with_top.add_le_add_iff_left protected lemma add_le_add_iff_right : a β‰  ∞ β†’ (b + a ≀ c + a ↔ b ≀ c) := with_top.add_le_add_iff_right protected lemma add_lt_add_iff_left : a β‰  ∞ β†’ (a + b < a + c ↔ b < c) := with_top.add_lt_add_iff_left protected lemma add_lt_add_iff_right : a β‰  ∞ β†’ (b + a < c + a ↔ b < c) := with_top.add_lt_add_iff_right protected lemma add_lt_add_of_le_of_lt : a β‰  ∞ β†’ a ≀ b β†’ c < d β†’ a + c < b + d := with_top.add_lt_add_of_le_of_lt protected lemma add_lt_add_of_lt_of_le : c β‰  ∞ β†’ a < b β†’ c ≀ d β†’ a + c < b + d := with_top.add_lt_add_of_lt_of_le instance contravariant_class_add_lt : contravariant_class ℝβ‰₯0∞ ℝβ‰₯0∞ (+) (<) := with_top.contravariant_class_add_lt lemma lt_add_right (ha : a β‰  ∞) (hb : b β‰  0) : a < a + b := by rwa [← pos_iff_ne_zero, ←ennreal.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 < real.to_nnreal q ∧ (real.to_nnreal 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, tsub_pos_iff_lt.2 ad, _⟩, rw [some_eq_coe, ← coe_add], convert cb, have : real.to_nnreal c = d, by { rw [← nnreal.coe_eq, real.coe_to_nnreal _ c_nonneg], refl }, rw [add_comm, this], exact tsub_add_cancel_of_le ad.le 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 @[simp] lemma Union_Iio_coe_nat : (⋃ n : β„•, Iio (n : ℝβ‰₯0∞)) = {∞}ᢜ := begin ext x, rw [mem_Union], exact ⟨λ ⟨n, hn⟩, ne_top_of_lt hn, ennreal.exists_nat_gt⟩ end @[simp] lemma Union_Iic_coe_nat : (⋃ n : β„•, Iic (n : ℝβ‰₯0∞)) = {∞}ᢜ := subset.antisymm (Union_subset $ Ξ» n x hx, ne_top_of_le_ne_top coe_nat_ne_top hx) $ Union_Iio_coe_nat β–Έ Union_mono (Ξ» n, Iio_subset_Iic_self) @[simp] lemma Union_Ioc_coe_nat : (⋃ n : β„•, Ioc a n) = Ioi a \ {∞} := by simp only [← Ioi_inter_Iic, ← inter_Union, Union_Iic_coe_nat, diff_eq] @[simp] lemma Union_Ioo_coe_nat : (⋃ n : β„•, Ioo a n) = Ioi a \ {∞} := by simp only [← Ioi_inter_Iio, ← inter_Union, Union_Iio_coe_nat, diff_eq] @[simp] lemma Union_Icc_coe_nat : (⋃ n : β„•, Icc a n) = Ici a \ {∞} := by simp only [← Ici_inter_Iic, ← inter_Union, Union_Iic_coe_nat, diff_eq] @[simp] lemma Union_Ico_coe_nat : (⋃ n : β„•, Ico a n) = Ici a \ {∞} := by simp only [← Ici_inter_Iio, ← inter_Union, Union_Iio_coe_nat, diff_eq] @[simp] lemma Inter_Ici_coe_nat : (β‹‚ n : β„•, Ici (n : ℝβ‰₯0∞)) = {∞} := by simp only [← compl_Iio, ← compl_Union, Union_Iio_coe_nat, compl_compl] @[simp] lemma Inter_Ioi_coe_nat : (β‹‚ n : β„•, Ioi (n : ℝβ‰₯0∞)) = {∞} := by simp only [← compl_Iic, ← compl_Union, Union_Iic_coe_nat, compl_compl] 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 := 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_rfl lemma mul_right_mono : monotone (Ξ» x, x * a) := Ξ» b c h, mul_le_mul h le_rfl lemma pow_strict_mono {n : β„•} (hn : n β‰  0) : strict_mono (Ξ» (x : ℝβ‰₯0∞), x^n) := begin assume x y hxy, obtain ⟨n, rfl⟩ := nat.exists_eq_succ_of_ne_zero hn, induction n with n IH, { simp only [hxy, pow_one] }, { simp only [pow_succ _ n.succ, mul_lt_mul hxy (IH (nat.succ_pos _).ne')] } end 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 cancel /-- An element `a` is `add_le_cancellable` if `a + b ≀ a + c` implies `b ≀ c` for all `b` and `c`. This is true in `ℝβ‰₯0∞` for all elements except `∞`. -/ lemma add_le_cancellable_iff_ne {a : ℝβ‰₯0∞} : add_le_cancellable a ↔ a β‰  ∞ := begin split, { rintro h rfl, refine ennreal.zero_lt_one.not_le (h _), simp, }, { rintro h b c hbc, apply ennreal.le_of_add_le_add_left h hbc } end /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_ne {a : ℝβ‰₯0∞} (h : a β‰  ∞) : add_le_cancellable a := add_le_cancellable_iff_ne.mpr h /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_lt {a : ℝβ‰₯0∞} (h : a < ∞) : add_le_cancellable a := cancel_of_ne h.ne /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_lt' {a b : ℝβ‰₯0∞} (h : a < b) : add_le_cancellable a := cancel_of_ne h.ne_top /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_coe {a : ℝβ‰₯0} : add_le_cancellable (a : ℝβ‰₯0∞) := cancel_of_ne coe_ne_top lemma add_right_inj (h : a β‰  ∞) : a + b = a + c ↔ b = c := (cancel_of_ne h).inj lemma add_left_inj (h : a β‰  ∞) : b + a = c + a ↔ b = c := (cancel_of_ne h).inj_left end cancel section sub lemma sub_eq_Inf {a b : ℝβ‰₯0∞} : a - b = Inf {d | a ≀ d + b} := le_antisymm (le_Inf $ Ξ» c, tsub_le_iff_right.mpr) $ Inf_le le_tsub_add /-- This is a special case of `with_top.coe_sub` in the `ennreal` namespace -/ lemma coe_sub : (↑(r - p) : ℝβ‰₯0∞) = ↑r - ↑p := with_top.coe_sub /-- This is a special case of `with_top.top_sub_coe` in the `ennreal` namespace -/ lemma top_sub_coe : ∞ - ↑r = ∞ := with_top.top_sub_coe /-- This is a special case of `with_top.sub_top` in the `ennreal` namespace -/ lemma sub_top : a - ∞ = 0 := with_top.sub_top lemma sub_eq_top_iff : a - b = ∞ ↔ a = ∞ ∧ b β‰  ∞ := by { cases a; cases b; simp [← with_top.coe_sub] } lemma sub_ne_top (ha : a β‰  ∞) : a - b β‰  ∞ := mt sub_eq_top_iff.mp $ mt and.left ha protected lemma sub_eq_of_eq_add (hb : b β‰  ∞) : a = c + b β†’ a - b = c := (cancel_of_ne hb).tsub_eq_of_eq_add protected lemma eq_sub_of_add_eq (hc : c β‰  ∞) : a + c = b β†’ a = b - c := (cancel_of_ne hc).eq_tsub_of_add_eq protected lemma sub_eq_of_eq_add_rev (hb : b β‰  ∞) : a = b + c β†’ a - b = c := (cancel_of_ne hb).tsub_eq_of_eq_add_rev lemma sub_eq_of_add_eq (hb : b β‰  ∞) (hc : a + b = c) : c - b = a := ennreal.sub_eq_of_eq_add hb hc.symm @[simp] protected lemma add_sub_cancel_left (ha : a β‰  ∞) : a + b - a = b := (cancel_of_ne ha).add_tsub_cancel_left @[simp] protected lemma add_sub_cancel_right (hb : b β‰  ∞) : a + b - b = a := (cancel_of_ne hb).add_tsub_cancel_right protected lemma lt_add_of_sub_lt_left (h : a β‰  ∞ ∨ b β‰  ∞) : a - b < c β†’ a < b + c := begin obtain rfl | hb := eq_or_ne b ∞, { rw [top_add, lt_top_iff_ne_top], exact Ξ» _, h.resolve_right (not_not.2 rfl) }, { exact (cancel_of_ne hb).lt_add_of_tsub_lt_left } end protected lemma lt_add_of_sub_lt_right (h : a β‰  ∞ ∨ c β‰  ∞) : a - c < b β†’ a < b + c := add_comm c b β–Έ ennreal.lt_add_of_sub_lt_left h lemma le_sub_of_add_le_left (ha : a β‰  ∞) : a + b ≀ c β†’ b ≀ c - a := (cancel_of_ne ha).le_tsub_of_add_le_left lemma le_sub_of_add_le_right (hb : b β‰  ∞) : a + b ≀ c β†’ a ≀ c - b := (cancel_of_ne hb).le_tsub_of_add_le_right protected lemma sub_lt_of_lt_add (hac : c ≀ a) (h : a < b + c) : a - c < b := ((cancel_of_lt' $ hac.trans_lt h).tsub_lt_iff_right hac).mpr h protected lemma sub_lt_iff_lt_right (hb : b β‰  ∞) (hab : b ≀ a) : a - b < c ↔ a < c + b := (cancel_of_ne hb).tsub_lt_iff_right hab protected lemma sub_lt_self (ha : a β‰  ∞) (haβ‚€ : a β‰  0) (hb : b β‰  0) : a - b < a := (cancel_of_ne ha).tsub_lt_self (pos_iff_ne_zero.2 haβ‚€) (pos_iff_ne_zero.2 hb) protected lemma sub_lt_self_iff (ha : a β‰  ∞) : a - b < a ↔ 0 < a ∧ 0 < b := (cancel_of_ne ha).tsub_lt_self_iff lemma sub_lt_of_sub_lt (hβ‚‚ : c ≀ a) (h₃ : a β‰  ∞ ∨ b β‰  ∞) (h₁ : a - b < c) : a - c < b := ennreal.sub_lt_of_lt_add hβ‚‚ (add_comm c b β–Έ ennreal.lt_add_of_sub_lt_right h₃ h₁) lemma sub_sub_cancel (h : a β‰  ∞) (h2 : b ≀ a) : a - (a - b) = b := (cancel_of_ne $ sub_ne_top h).tsub_tsub_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 := (cancel_of_ne ha).tsub_right_inj (cancel_of_ne $ ne_top_of_le_ne_top ha hb) (cancel_of_ne $ ne_top_of_le_ne_top ha hc) hb hc 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] }, rcases eq_or_lt_of_le (zero_le b) with rfl|hb, { simp }, exact (cancel_of_ne $ mul_ne_top hab.ne_top (h hb hab)).tsub_mul 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 } 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∞} (h : βˆ€ a ∈ s, f a β‰  ∞) : βˆ‘ a in s, f a < ∞ := with_top.sum_lt_top h /-- 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 lemma lt_top_of_sum_ne_top {s : finset Ξ±} {f : Ξ± β†’ ℝβ‰₯0∞} (h : (βˆ‘ x in s, f x) β‰  ∞) {a : Ξ±} (ha : a ∈ s) : f a < ∞ := sum_lt_top_iff.1 h.lt_top a ha /-- 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 rfl], { intros x hx, exact (coe_to_nnreal (hf x hx)).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 real.to_nnreal_sum_of_nonneg hf, end theorem sum_lt_sum_of_nonempty {s : finset Ξ±} (hs : s.nonempty) {f g : Ξ± β†’ ℝβ‰₯0∞} (Hlt : βˆ€ i ∈ s, f i < g i) : βˆ‘ i in s, f i < βˆ‘ i in s, g i := begin induction hs using finset.nonempty.cons_induction with a a s as hs IH, { simp [Hlt _ (finset.mem_singleton_self _)] }, { simp only [as, finset.sum_cons, not_false_iff], exact ennreal.add_lt_add (Hlt _ (finset.mem_cons_self _ _)) (IH (Ξ» i hi, Hlt _ (finset.mem_cons.2 $ or.inr hi))) } end theorem exists_le_of_sum_le {s : finset Ξ±} (hs : s.nonempty) {f g : Ξ± β†’ ℝβ‰₯0∞} (Hle : βˆ‘ i in s, f i ≀ βˆ‘ i in s, g i) : βˆƒ i ∈ s, f i ≀ g i := begin contrapose! Hle, apply ennreal.sum_lt_sum_of_nonempty hs Hle, end end sum section interval variables {x y z : ℝβ‰₯0∞} {Ξ΅ Ρ₁ Ξ΅β‚‚ : ℝβ‰₯0∞} {s : set ℝβ‰₯0∞} protected lemma Ico_eq_Iio : (Ico 0 y) = (Iio y) := Ico_bot lemma mem_Iio_self_add : x β‰  ∞ β†’ Ξ΅ β‰  0 β†’ x ∈ Iio (x + Ξ΅) := assume xt Ξ΅0, lt_add_right xt Ξ΅0 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 xt Ξ΅0'⟩ end interval section bit @[mono] lemma bit0_strict_mono : strict_mono (bit0 : ℝβ‰₯0∞ β†’ ℝβ‰₯0∞) := Ξ» a b h, add_lt_add h h lemma bit0_injective : function.injective (bit0 : ℝβ‰₯0∞ β†’ ℝβ‰₯0∞) := bit0_strict_mono.injective @[simp] lemma bit0_lt_bit0 : bit0 a < bit0 b ↔ a < b := bit0_strict_mono.lt_iff_lt @[simp, mono] lemma bit0_le_bit0 : bit0 a ≀ bit0 b ↔ a ≀ b := bit0_strict_mono.le_iff_le @[simp] lemma bit0_inj : bit0 a = bit0 b ↔ a = b := bit0_injective.eq_iff @[simp] lemma bit0_eq_zero_iff : bit0 a = 0 ↔ a = 0 := bit0_injective.eq_iff' bit0_zero @[simp] lemma bit0_top : bit0 ∞ = ∞ := add_top @[simp] lemma bit0_eq_top_iff : bit0 a = ∞ ↔ a = ∞ := bit0_injective.eq_iff' bit0_top @[mono] lemma bit1_strict_mono : strict_mono (bit1 : ℝβ‰₯0∞ β†’ ℝβ‰₯0∞) := Ξ» a b h, ennreal.add_lt_add_right one_ne_top (bit0_strict_mono h) lemma bit1_injective : function.injective (bit1 : ℝβ‰₯0∞ β†’ ℝβ‰₯0∞) := bit1_strict_mono.injective @[simp] lemma bit1_lt_bit1 : bit1 a < bit1 b ↔ a < b := bit1_strict_mono.lt_iff_lt @[simp, mono] lemma bit1_le_bit1 : bit1 a ≀ bit1 b ↔ a ≀ b := bit1_strict_mono.le_iff_le @[simp] lemma bit1_inj : bit1 a = bit1 b ↔ a = b := bit1_injective.eq_iff @[simp] lemma bit1_ne_zero : bit1 a β‰  0 := by simp [bit1] @[simp] lemma bit1_top : bit1 ∞ = ∞ := by rw [bit1, bit0_top, top_add] @[simp] lemma bit1_eq_top_iff : bit1 a = ∞ ↔ a = ∞ := bit1_injective.eq_iff' bit1_top @[simp] lemma bit1_eq_one_iff : bit1 a = 1 ↔ a = 0 := bit1_injective.eq_iff' bit1_zero end bit section inv noncomputable theory instance : has_inv ℝβ‰₯0∞ := ⟨λa, Inf {b | 1 ≀ a * b}⟩ instance : div_inv_monoid ℝβ‰₯0∞ := { inv := has_inv.inv, .. (infer_instance : monoid ℝβ‰₯0∞) } lemma div_eq_inv_mul : a / b = b⁻¹ * a := by rw [div_eq_mul_inv, mul_comm] @[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] lemma coe_inv_le : (↑r⁻¹ : ℝβ‰₯0∞) ≀ (↑r)⁻¹ := le_Inf $ assume b (hb : 1 ≀ ↑r * b), coe_le_iff.2 $ by { rintro b rfl, apply nnreal.inv_le_of_le_mul, rwa [← coe_mul, ← coe_one, coe_le_coe] at hb } @[simp, norm_cast] lemma coe_inv (hr : r β‰  0) : (↑r⁻¹ : ℝβ‰₯0∞) = (↑r)⁻¹ := coe_inv_le.antisymm $ Inf_le $ le_of_eq $ by rw [← coe_mul, mul_inv_cancel hr, coe_one] @[norm_cast] lemma coe_inv_two : ((2⁻¹ : ℝβ‰₯0) : ℝβ‰₯0∞) = 2⁻¹ := by rw [coe_inv _root_.two_ne_zero, 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] lemma div_zero (h : a β‰  0) : a / 0 = ∞ := by simp [div_eq_mul_inv, h] @[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 cases n, { simp only [pow_zero, inv_one] }, induction a using with_top.rec_top_coe, { simp [top_pow n.succ_pos] }, rcases eq_or_ne a 0 with rfl|ha, { simp [top_pow, zero_pow, n.succ_pos] }, rw [← coe_inv ha, ← coe_pow, ← coe_inv (pow_ne_zero _ ha), ← inv_pow, coe_pow] 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 div_mul_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, div_mul_cancel h0 hI] instance : has_involutive_inv ℝβ‰₯0∞ := { inv := has_inv.inv, inv_inv := Ξ» a, by by_cases a = 0; cases a; simp [*, none_eq_top, some_eq_coe, -coe_inv, (coe_inv _).symm] at * } @[simp] lemma inv_eq_top : a⁻¹ = ∞ ↔ a = 0 := inv_zero β–Έ inv_inj 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 : y β‰  0) : x / y < ∞ := mul_lt_top h1 (inv_ne_top.mpr h2) @[simp] lemma inv_eq_zero : a⁻¹ = 0 ↔ a = ∞ := inv_top β–Έ inv_inj lemma inv_ne_zero : a⁻¹ β‰  0 ↔ a β‰  ∞ := by simp lemma mul_inv {a b : ℝβ‰₯0∞} (ha : a β‰  0 ∨ b β‰  ∞) (hb : a β‰  ∞ ∨ b β‰  0) : (a * b)⁻¹ = a⁻¹ * b⁻¹ := begin induction b using with_top.rec_top_coe, { replace ha : a β‰  0 := ha.neg_resolve_right rfl, simp [ha], }, induction a using with_top.rec_top_coe, { replace hb : b β‰  0 := coe_ne_zero.1 (hb.neg_resolve_left rfl), simp [hb] }, by_cases h'a : a = 0, { simp only [h'a, with_top.top_mul, ennreal.inv_zero, ennreal.coe_ne_top, zero_mul, ne.def, not_false_iff, ennreal.coe_zero, ennreal.inv_eq_zero] }, by_cases h'b : b = 0, { simp only [h'b, ennreal.inv_zero, ennreal.coe_ne_top, with_top.mul_top, ne.def, not_false_iff, mul_zero, ennreal.coe_zero, ennreal.inv_eq_zero] }, rw [← ennreal.coe_mul, ← ennreal.coe_inv, ← ennreal.coe_inv h'a, ← ennreal.coe_inv h'b, ← ennreal.coe_mul, mul_inv_rev, mul_comm], simp [h'a, h'b], end @[simp] lemma inv_pos : 0 < a⁻¹ ↔ a β‰  ∞ := pos_iff_ne_zero.trans inv_ne_zero lemma inv_strict_anti : strict_anti (has_inv.inv : ℝβ‰₯0∞ β†’ ℝβ‰₯0∞) := begin intros a b h, lift a to ℝβ‰₯0 using h.ne_top, induction b using with_top.rec_top_coe, { simp }, rw [coe_lt_coe] at h, rcases eq_or_ne a 0 with rfl|ha, { simp [h] }, rw [← coe_inv h.ne_bot, ← coe_inv ha, coe_lt_coe], exact nnreal.inv_lt_inv ha h end @[simp] lemma inv_lt_inv : a⁻¹ < b⁻¹ ↔ b < a := inv_strict_anti.lt_iff_lt 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 := inv_strict_anti.le_iff_le 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] /-- The inverse map `Ξ» x, x⁻¹` is an order isomorphism between `ℝβ‰₯0∞` and its `order_dual` -/ @[simps apply] def _root_.order_iso.inv_ennreal : ℝβ‰₯0∞ ≃o ℝβ‰₯0βˆžα΅’α΅ˆ := { map_rel_iff' := Ξ» a b, ennreal.inv_le_inv, to_equiv := (equiv.inv ℝβ‰₯0∞).trans order_dual.to_dual } @[simp] lemma _root_.order_iso.inv_ennreal_symm_apply : order_iso.inv_ennreal.symm a = (order_dual.of_dual a)⁻¹ := rfl 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 induction b using with_top.rec_top_coe, { lift c to ℝβ‰₯0 using ht.neg_resolve_left rfl, rw [div_top, nonpos_iff_eq_zero, mul_top], rcases eq_or_ne a 0 with rfl|ha; simp * }, rcases eq_or_ne b 0 with (rfl | hb), { have hc : c β‰  0, from h0.neg_resolve_left rfl, simp [div_zero hc] }, { rw [← coe_ne_zero] at hb, rw [← ennreal.mul_le_mul_right hb coe_ne_top, div_mul_cancel hb coe_ne_top] }, 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 lt_div_iff_mul_lt (hb0 : b β‰  0 ∨ c β‰  ∞) (hbt : b β‰  ∞ ∨ c β‰  0) : c < a / b ↔ c * b < a := lt_iff_lt_of_le_iff_le (div_le_iff_le_mul hb0 hbt) 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 lemma div_le_of_le_mul' (h : a ≀ b * c) : a / b ≀ c := div_le_of_le_mul $ mul_comm b c β–Έ h lemma mul_le_of_le_div (h : a ≀ b / c) : a * c ≀ b := begin rw [← inv_inv c], exact div_le_of_le_mul h, end lemma mul_le_of_le_div' (h : a ≀ b / c) : c * a ≀ b := mul_comm a c β–Έ mul_le_of_le_div h 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 mul_lt_of_lt_div' (h : a < b / c) : c * a < b := mul_comm a c β–Έ mul_lt_of_lt_div h lemma inv_le_iff_le_mul (h₁ : b = ∞ β†’ a β‰  0) (hβ‚‚ : a = ∞ β†’ b β‰  0) : a⁻¹ ≀ b ↔ 1 ≀ a * b := begin rw [← one_div, div_le_iff_le_mul, mul_comm], exacts [or_not_of_imp h₁, not_or_of_imp hβ‚‚] end @[simp] lemma le_inv_iff_mul_le : a ≀ b⁻¹ ↔ a * b ≀ 1 := by rw [← one_div, le_div_iff_mul_le]; { right, simp } lemma div_le_div {a b c d : ℝβ‰₯0∞} (hab : a ≀ b) (hdc : d ≀ c) : a / c ≀ b / d := div_eq_mul_inv b d β–Έ div_eq_mul_inv a c β–Έ ennreal.mul_le_mul hab (ennreal.inv_le_inv.mpr hdc) lemma eq_inv_of_mul_eq_one_left (h : a * b = 1) : a = b⁻¹ := begin have hb : b β‰  ∞, { rintro rfl, simpa [left_ne_zero_of_mul_eq_one h] using h }, rw [← mul_one a, ← mul_inv_cancel (right_ne_zero_of_mul_eq_one h) hb, ← mul_assoc, h, one_mul] end 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 le_of_forall_pos_nnreal_lt {x y : ℝβ‰₯0∞} (h : βˆ€ r : ℝβ‰₯0, 0 < r β†’ ↑r < x β†’ ↑r ≀ y) : x ≀ y := le_of_forall_nnreal_lt $ Ξ» r hr, (zero_le r).eq_or_lt.elim (Ξ» h, h β–Έ zero_le _) (Ξ» h0, h r h0 hr) 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 add_div : (a + b) / c = a / c + b / c := right_distrib a b (c⁻¹) lemma div_add_div_same {a b c : ℝβ‰₯0∞} : a / c + b / c = (a + b) / c := add_div.symm lemma div_self (h0 : a β‰  0) (hI : a β‰  ∞) : a / a = 1 := mul_inv_cancel h0 hI lemma mul_div_le : a * (b / a) ≀ b := mul_le_of_le_div' le_rfl -- TODO: add this lemma for an `is_unit` in any `division_monoid` lemma eq_div_iff (ha : a β‰  0) (ha' : a β‰  ∞) : b = c / a ↔ a * b = c := ⟨λ h, by rw [h, mul_div_cancel' ha ha'], Ξ» h, by rw [← h, mul_div_assoc, mul_div_cancel' ha ha']⟩ lemma div_eq_div_iff (ha : a β‰  0) (ha' : a β‰  ∞) (hb : b β‰  0) (hb' : b β‰  ∞) : c / b = d / a ↔ a * c = b * d := begin rw eq_div_iff ha ha', conv_rhs { rw eq_comm }, rw [← eq_div_iff hb hb', mul_div_assoc, eq_comm], 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 inv_three_add_inv_three : (3 : ℝβ‰₯0∞)⁻¹ + 3⁻¹ + 3⁻¹ = 1 := begin rw [show (3 : ℝβ‰₯0∞)⁻¹ + 3⁻¹ + 3⁻¹ = 3 * 3⁻¹, by ring, ← div_eq_mul_inv, ennreal.div_self]; simp, end @[simp] 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 add_thirds (a : ℝβ‰₯0∞) : a / 3 + a / 3 + a / 3 = a := by rw [div_eq_mul_inv, ← mul_add, ← mul_add, inv_three_add_inv_three, 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 : a β‰  0) : 0 < a / 2 := by simp [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, rw coe_ne_zero at hz, rw [← coe_two, ← coe_div, coe_lt_coe], exacts [nnreal.half_lt_self hz, two_ne_zero'] end lemma half_le_self : a / 2 ≀ a := le_add_self.trans_eq (add_halves _) 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 /-- The birational order isomorphism between `ℝβ‰₯0∞` and the unit interval `set.Iic (1 : ℝβ‰₯0∞)`. -/ @[simps apply_coe] def order_iso_Iic_one_birational : ℝβ‰₯0∞ ≃o Iic (1 : ℝβ‰₯0∞) := begin refine strict_mono.order_iso_of_right_inverse (Ξ» x, ⟨(x⁻¹ + 1)⁻¹, inv_le_one.2 $ le_add_self⟩) (Ξ» x y hxy, _) (Ξ» x, (x⁻¹ - 1)⁻¹) (Ξ» x, subtype.ext _), { simpa only [subtype.mk_lt_mk, inv_lt_inv, ennreal.add_lt_add_iff_right one_ne_top] }, { have : (1 : ℝβ‰₯0∞) ≀ x⁻¹, from one_le_inv.2 x.2, simp only [inv_inv, subtype.coe_mk, tsub_add_cancel_of_le this] } end @[simp] lemma order_iso_Iic_one_birational_symm_apply (x : Iic (1 : ℝβ‰₯0∞)) : order_iso_Iic_one_birational.symm x = (x⁻¹ - 1)⁻¹ := rfl /-- Order isomorphism between an initial interval in `ℝβ‰₯0∞` and an initial interval in `ℝβ‰₯0`. -/ @[simps apply_coe] def order_iso_Iic_coe (a : ℝβ‰₯0) : Iic (a : ℝβ‰₯0∞) ≃o Iic a := order_iso.symm { to_fun := Ξ» x, ⟨x, coe_le_coe.2 x.2⟩, inv_fun := Ξ» x, ⟨ennreal.to_nnreal x, coe_le_coe.1 $ coe_to_nnreal_le_self.trans x.2⟩, left_inv := Ξ» x, subtype.ext $ to_nnreal_coe, right_inv := Ξ» x, subtype.ext $ coe_to_nnreal (ne_top_of_le_ne_top coe_ne_top x.2), map_rel_iff' := Ξ» x y, by simp only [equiv.coe_fn_mk, subtype.mk_le_mk, coe_coe, coe_le_coe, subtype.coe_le_coe] } @[simp] lemma order_iso_Iic_coe_symm_apply_coe (a : ℝβ‰₯0) (b : Iic a) : ((order_iso_Iic_coe a).symm b : ℝβ‰₯0∞) = b := rfl /-- An order isomorphism between the extended nonnegative real numbers and the unit interval. -/ def order_iso_unit_interval_birational : ℝβ‰₯0∞ ≃o Icc (0 : ℝ) 1 := order_iso_Iic_one_birational.trans $ (order_iso_Iic_coe 1).trans $ (nnreal.order_iso_Icc_zero_coe 1).symm @[simp] lemma order_iso_unit_interval_birational_apply_coe (x : ℝβ‰₯0∞) : (order_iso_unit_interval_birational x : ℝ) = (x⁻¹ + 1)⁻¹.to_real := rfl 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 lt_of_le_of_lt (by simp) 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⟩, refine ⟨n, lt_trans _ hn⟩, rw [← ennreal.inv_pow, inv_lt_inv], norm_cast, exact n.lt_two_pow end @[simp, norm_cast] lemma coe_zpow (hr : r β‰  0) (n : β„€) : (↑(r^n) : ℝβ‰₯0∞) = r^n := begin cases n, { simp only [int.of_nat_eq_coe, coe_pow, zpow_coe_nat] }, { have : r ^ n.succ β‰  0 := pow_ne_zero (n+1) hr, simp only [zpow_neg_succ_of_nat, coe_inv this, coe_pow] } end lemma zpow_pos (ha : a β‰  0) (h'a : a β‰  ∞) (n : β„€) : 0 < a ^ n := begin cases n, { exact ennreal.pow_pos ha.bot_lt n }, { simp only [h'a, pow_eq_top_iff, zpow_neg_succ_of_nat, ne.def, not_false_iff, inv_pos, false_and] } end lemma zpow_lt_top (ha : a β‰  0) (h'a : a β‰  ∞) (n : β„€) : a ^ n < ∞ := begin cases n, { exact ennreal.pow_lt_top h'a.lt_top _ }, { simp only [ennreal.pow_pos ha.bot_lt (n + 1), zpow_neg_succ_of_nat, inv_lt_top] } end lemma exists_mem_Ico_zpow {x y : ℝβ‰₯0∞} (hx : x β‰  0) (h'x : x β‰  ∞) (hy : 1 < y) (h'y : y β‰  ⊀) : βˆƒ n : β„€, x ∈ Ico (y ^ n) (y ^ (n + 1)) := begin lift x to ℝβ‰₯0 using h'x, lift y to ℝβ‰₯0 using h'y, have A : y β‰  0, { simpa only [ne.def, coe_eq_zero] using (ennreal.zero_lt_one.trans hy).ne' }, obtain ⟨n, hn, h'n⟩ : βˆƒ n : β„€, y ^ n ≀ x ∧ x < y ^ (n + 1), { refine nnreal.exists_mem_Ico_zpow _ (one_lt_coe_iff.1 hy), simpa only [ne.def, coe_eq_zero] using hx }, refine ⟨n, _, _⟩, { rwa [← ennreal.coe_zpow A, ennreal.coe_le_coe] }, { rwa [← ennreal.coe_zpow A, ennreal.coe_lt_coe] } end lemma exists_mem_Ioc_zpow {x y : ℝβ‰₯0∞} (hx : x β‰  0) (h'x : x β‰  ∞) (hy : 1 < y) (h'y : y β‰  ⊀) : βˆƒ n : β„€, x ∈ Ioc (y ^ n) (y ^ (n + 1)) := begin lift x to ℝβ‰₯0 using h'x, lift y to ℝβ‰₯0 using h'y, have A : y β‰  0, { simpa only [ne.def, coe_eq_zero] using (ennreal.zero_lt_one.trans hy).ne' }, obtain ⟨n, hn, h'n⟩ : βˆƒ n : β„€, y ^ n < x ∧ x ≀ y ^ (n + 1), { refine nnreal.exists_mem_Ioc_zpow _ (one_lt_coe_iff.1 hy), simpa only [ne.def, coe_eq_zero] using hx }, refine ⟨n, _, _⟩, { rwa [← ennreal.coe_zpow A, ennreal.coe_lt_coe] }, { rwa [← ennreal.coe_zpow A, ennreal.coe_le_coe] } end lemma Ioo_zero_top_eq_Union_Ico_zpow {y : ℝβ‰₯0∞} (hy : 1 < y) (h'y : y β‰  ⊀) : Ioo (0 : ℝβ‰₯0∞) (∞ : ℝβ‰₯0∞) = ⋃ (n : β„€), Ico (y^n) (y^(n+1)) := begin ext x, simp only [mem_Union, mem_Ioo, mem_Ico], split, { rintros ⟨hx, h'x⟩, exact exists_mem_Ico_zpow hx.ne' h'x.ne hy h'y }, { rintros ⟨n, hn, h'n⟩, split, { apply lt_of_lt_of_le _ hn, exact ennreal.zpow_pos (ennreal.zero_lt_one.trans hy).ne' h'y _ }, { apply lt_trans h'n _, exact ennreal.zpow_lt_top (ennreal.zero_lt_one.trans hy).ne' h'y _ } } end lemma zpow_le_of_le {x : ℝβ‰₯0∞} (hx : 1 ≀ x) {a b : β„€} (h : a ≀ b) : x ^ a ≀ x ^ b := begin induction a with a a; induction b with b b, { simp only [int.of_nat_eq_coe, zpow_coe_nat], exact pow_le_pow hx (int.le_of_coe_nat_le_coe_nat h), }, { apply absurd h (not_le_of_gt _), exact lt_of_lt_of_le (int.neg_succ_lt_zero _) (int.of_nat_nonneg _) }, { simp only [zpow_neg_succ_of_nat, int.of_nat_eq_coe, zpow_coe_nat], refine le_trans (inv_le_one.2 _) _; exact ennreal.one_le_pow_of_one_le hx _, }, { simp only [zpow_neg_succ_of_nat, inv_le_inv], apply pow_le_pow hx, simpa only [←int.coe_nat_le_coe_nat_iff, neg_le_neg_iff, int.coe_nat_add, int.coe_nat_one, int.neg_succ_of_nat_eq] using h } end lemma monotone_zpow {x : ℝβ‰₯0∞} (hx : 1 ≀ x) : monotone ((^) x : β„€ β†’ ℝβ‰₯0∞) := Ξ» a b h, zpow_le_of_le hx h lemma zpow_add {x : ℝβ‰₯0∞} (hx : x β‰  0) (h'x : x β‰  ∞) (m n : β„€) : x ^ (m + n) = x ^ m * x ^ n := begin lift x to ℝβ‰₯0 using h'x, replace hx : x β‰  0, by simpa only [ne.def, coe_eq_zero] using hx, simp only [← coe_zpow hx, zpow_addβ‚€ hx, coe_mul] 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_sub_of_le {a b : ℝβ‰₯0∞} (h : b ≀ a) (ha : a β‰  ∞): (a - b).to_real = a.to_real - b.to_real := begin lift b to ℝβ‰₯0 using ne_top_of_le_ne_top ha h, lift a to ℝβ‰₯0 using ha, simp only [← ennreal.coe_sub, ennreal.coe_to_real, nnreal.coe_sub (ennreal.coe_le_coe.mp h)], end lemma le_to_real_sub {a b : ℝβ‰₯0∞} (hb : b β‰  ∞) : a.to_real - b.to_real ≀ (a - b).to_real := begin lift b to ℝβ‰₯0 using hb, induction a using with_top.rec_top_coe, { simp }, { simp only [←coe_sub, nnreal.sub_def, real.coe_to_nnreal', coe_to_real], exact le_max_left _ _ } 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, real.to_nnreal_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 real.to_nnreal_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 lemma to_real_mono (hb : b β‰  ∞) (h : a ≀ b) : a.to_real ≀ b.to_real := (to_real_le_to_real (h.trans_lt (lt_top_iff_ne_top.2 hb)).ne hb).2 h @[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_strict_mono (hb : b β‰  ∞) (h : a < b) : a.to_real < b.to_real := (to_real_lt_to_real (h.trans (lt_top_iff_ne_top.2 hb)).ne hb).2 h lemma to_nnreal_mono (hb : b β‰  ∞) (h : a ≀ b) : a.to_nnreal ≀ b.to_nnreal := by simpa [←ennreal.coe_le_coe, hb, (h.trans_lt hb.lt_top).ne] @[simp] lemma to_nnreal_le_to_nnreal (ha : a β‰  ∞) (hb : b β‰  ∞) : a.to_nnreal ≀ b.to_nnreal ↔ a ≀ b := ⟨λ h, by rwa [←coe_to_nnreal ha, ←coe_to_nnreal hb, coe_le_coe], to_nnreal_mono hb⟩ lemma to_nnreal_strict_mono (hb : b β‰  ∞) (h : a < b) : a.to_nnreal < b.to_nnreal := by simpa [←ennreal.coe_lt_coe, hb, (h.trans hb.lt_top).ne] @[simp] lemma to_nnreal_lt_to_nnreal (ha : a β‰  ∞) (hb : b β‰  ∞) : a.to_nnreal < b.to_nnreal ↔ a < b := ⟨λ h, by rwa [←coe_to_nnreal ha, ←coe_to_nnreal hb, coe_lt_coe], to_nnreal_strict_mono hb⟩ 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 < ∞) := by { induction a using with_top.rec_top_coe; simp } lemma to_nnreal_pos {a : ℝβ‰₯0∞} (haβ‚€ : a β‰  0) (ha_top : a β‰  ∞) : 0 < a.to_nnreal := to_nnreal_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr haβ‚€, lt_top_iff_ne_top.mpr ha_top⟩ lemma to_real_pos_iff : 0 < a.to_real ↔ (0 < a ∧ a < ∞):= (nnreal.coe_pos).trans to_nnreal_pos_iff lemma to_real_pos {a : ℝβ‰₯0∞} (haβ‚€ : a β‰  0) (ha_top : a β‰  ∞) : 0 < a.to_real := to_real_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr haβ‚€, lt_top_iff_ne_top.mpr ha_top⟩ lemma of_real_le_of_real {p q : ℝ} (h : p ≀ q) : ennreal.of_real p ≀ ennreal.of_real q := by simp [ennreal.of_real, real.to_nnreal_le_to_nnreal 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, real.to_nnreal_le_to_nnreal_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, real.to_nnreal_lt_to_nnreal_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, real.to_nnreal_lt_to_nnreal_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] @[simp] lemma zero_eq_of_real {p : ℝ} : 0 = ennreal.of_real p ↔ p ≀ 0 := eq_comm.trans of_real_eq_zero alias of_real_eq_zero ↔ _ of_real_of_nonpos lemma of_real_sub (p : ℝ) (hq : 0 ≀ q) : ennreal.of_real (p - q) = ennreal.of_real p - ennreal.of_real q := begin obtain h | h := le_total p q, { rw [of_real_of_nonpos (sub_nonpos_of_le h), tsub_eq_zero_of_le (of_real_le_of_real h)] }, refine ennreal.eq_sub_of_add_eq of_real_ne_top _, rw [←of_real_add (sub_nonneg_of_le h) hq, sub_add_cancel], end 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 real.to_nnreal_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 real.to_nnreal_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 real.le_to_nnreal_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 real.lt_to_nnreal_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, real.to_nnreal_mul hp] lemma of_real_mul' {p q : ℝ} (hq : 0 ≀ q) : ennreal.of_real (p * q) = ennreal.of_real p * ennreal.of_real q := by rw [mul_comm, of_real_mul hq, mul_comm] lemma of_real_pow {p : ℝ} (hp : 0 ≀ p) (n : β„•) : ennreal.of_real (p ^ n) = ennreal.of_real p ^ n := by rw [of_real_eq_coe_nnreal hp, ← coe_pow, ← of_real_coe_nnreal, nnreal.coe_pow, nnreal.coe_mk] 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 (real.to_nnreal x) (by simp [hx]), coe_eq_coe, real.to_nnreal_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_mul_inv, div_eq_mul_inv, of_real_mul' (inv_nonneg.2 hy.le), of_real_inv_of_pos hy] 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 lemma to_nnreal_top_mul (a : ℝβ‰₯0∞) : ennreal.to_nnreal (∞ * a) = 0 := by rw [mul_comm, to_nnreal_mul_top] @[simp] lemma to_nnreal_mul {a b : ℝβ‰₯0∞} : (a * b).to_nnreal = a.to_nnreal * b.to_nnreal := begin induction a using with_top.rec_top_coe, { rw [to_nnreal_top_mul, top_to_nnreal, zero_mul] }, induction b using with_top.rec_top_coe, { rw [to_nnreal_mul_top, top_to_nnreal, mul_zero] }, simp only [to_nnreal_coe, ← coe_mul] end @[simp] lemma smul_to_nnreal (a : ℝβ‰₯0) (b : ℝβ‰₯0∞) : (a β€’ b).to_nnreal = a * b.to_nnreal := begin change ((a : ℝβ‰₯0∞) * b).to_nnreal = a * b.to_nnreal, simp only [ennreal.to_nnreal_mul, ennreal.to_nnreal_coe], 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' := Ξ» _ _, to_nnreal_mul } @[simp] lemma to_nnreal_pow (a : ℝβ‰₯0∞) (n : β„•) : (a ^ n).to_nnreal = a.to_nnreal ^ n := to_nnreal_hom.map_pow a n @[simp] 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 @[simp] lemma to_real_mul : (a * b).to_real = a.to_real * b.to_real := to_real_hom.map_mul a b @[simp] lemma to_real_pow (a : ℝβ‰₯0∞) (n : β„•) : (a ^ n).to_real = a.to_real ^ n := to_real_hom.map_pow a n @[simp] 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 to_real_of_real_mul (c : ℝ) (a : ℝβ‰₯0∞) (h : 0 ≀ c) : ennreal.to_real ((ennreal.of_real c) * a) = c * ennreal.to_real a := by rw [ennreal.to_real_mul, ennreal.to_real_of_real h] lemma to_real_mul_top (a : ℝβ‰₯0∞) : ennreal.to_real (a * ∞) = 0 := by rw [to_real_mul, top_to_real, mul_zero] 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, lift b to ℝβ‰₯0 using hb, simp only [coe_eq_coe, nnreal.coe_eq, coe_to_real], end lemma to_real_smul (r : ℝβ‰₯0) (s : ℝβ‰₯0∞) : (r β€’ s).to_real = r β€’ s.to_real := by { rw [ennreal.smul_def, smul_eq_mul, to_real_mul, coe_to_real], refl } protected lemma trichotomy (p : ℝβ‰₯0∞) : p = 0 ∨ p = ∞ ∨ 0 < p.to_real := by simpa only [or_iff_not_imp_left] using to_real_pos protected lemma trichotomyβ‚‚ {p q : ℝβ‰₯0∞} (hpq : p ≀ q) : (p = 0 ∧ q = 0) ∨ (p = 0 ∧ q = ∞) ∨ (p = 0 ∧ 0 < q.to_real) ∨ (p = ∞ ∧ q = ∞) ∨ (0 < p.to_real ∧ q = ∞) ∨ (0 < p.to_real ∧ 0 < q.to_real ∧ p.to_real ≀ q.to_real) := begin rcases eq_or_lt_of_le (bot_le : 0 ≀ p) with (rfl : 0 = p) | (hp : 0 < p), { simpa using q.trichotomy }, rcases eq_or_lt_of_le (le_top : q ≀ ∞) with rfl | hq, { simpa using p.trichotomy }, repeat { right }, have hq' : 0 < q := lt_of_lt_of_le hp hpq, have hp' : p < ∞ := lt_of_le_of_lt hpq hq, simp [ennreal.to_real_le_to_real hp'.ne hq.ne, ennreal.to_real_pos_iff, hpq, hp, hp', hq', hq], end protected lemma dichotomy (p : ℝβ‰₯0∞) [fact (1 ≀ p)] : p = ∞ ∨ 1 ≀ p.to_real := begin have : p = ⊀ ∨ 0 < p.to_real ∧ 1 ≀ p.to_real, { simpa using ennreal.trichotomyβ‚‚ (fact.out _ : 1 ≀ p) }, exact this.imp_right (Ξ» h, h.2) end lemma to_nnreal_inv (a : ℝβ‰₯0∞) : (a⁻¹).to_nnreal = (a.to_nnreal)⁻¹ := begin induction a using with_top.rec_top_coe, { simp }, rcases eq_or_ne a 0 with rfl|ha, { simp }, rw [← coe_inv ha, to_nnreal_coe, to_nnreal_coe] end lemma to_nnreal_div (a b : ℝβ‰₯0∞) : (a / b).to_nnreal = a.to_nnreal / b.to_nnreal := by rw [div_eq_mul_inv, to_nnreal_mul, to_nnreal_inv, div_eq_mul_inv] lemma to_real_inv (a : ℝβ‰₯0∞) : (a⁻¹).to_real = (a.to_real)⁻¹ := by { simp_rw ennreal.to_real, norm_cast, exact to_nnreal_inv a, } lemma to_real_div (a b : ℝβ‰₯0∞) : (a / b).to_real = a.to_real / b.to_real := by rw [div_eq_mul_inv, to_real_mul, to_real_inv, div_eq_mul_inv] 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 real.to_nnreal_prod_of_nonneg hf, end @[simp] lemma to_nnreal_bit0 {x : ℝβ‰₯0∞} : (bit0 x).to_nnreal = bit0 (x.to_nnreal) := begin induction x using with_top.rec_top_coe, { simp }, { exact to_nnreal_add coe_ne_top coe_ne_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 (by rwa [ne.def, bit0_eq_top_iff]) ennreal.one_ne_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 : ℝ) : ennreal.of_real (bit0 r) = bit0 (ennreal.of_real r) := by simp [ennreal.of_real] @[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 [real.to_nnreal_one, bit1]) 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_rfl) (tsub_le_iff_right.1 $ le_infi $ assume i, tsub_le_iff_right.2 $ infi_le _ _) lemma supr_sub : (⨆i, f i) - a = (⨆i, f i - a) := le_antisymm (tsub_le_iff_right.2 $ supr_le $ assume i, tsub_le_iff_right.1 $ le_supr _ i) (supr_le $ assume i, tsub_le_tsub (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 [tsub_le_iff_right, add_comm, infi_add], simp [tsub_le_iff_right, 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] 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 := begin induction s using finset.induction_on with a s ha ih, { simp }, have : βˆ€ (i j : ΞΉ), βˆƒ (k : ΞΉ), f k a + βˆ‘ b in s, f k b ≀ f i a + βˆ‘ b in s, f j b, { intros i j, obtain ⟨k, hk⟩ := h (insert a s) i j, exact ⟨k, add_le_add (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum $ Ξ» a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩ }, simp [ha, ih.symm, infi_add_infi this] end /-- If `x β‰  0` and `x β‰  ∞`, then right multiplication by `x` maps infimum to infimum. See also `ennreal.infi_mul` that assumes `[nonempty ΞΉ]` but does not require `x β‰  0`. -/ lemma infi_mul_of_ne {ΞΉ} {f : ΞΉ β†’ ℝβ‰₯0∞} {x : ℝβ‰₯0∞} (h0 : x β‰  0) (h : x β‰  ∞) : infi f * x = β¨… i, f i * x := le_antisymm mul_right_mono.map_infi_le ((div_le_iff_le_mul (or.inl h0) $ or.inl h).mp $ le_infi $ Ξ» i, (div_le_iff_le_mul (or.inl h0) $ or.inl h).mpr $ infi_le _ _) /-- If `x β‰  ∞`, then right multiplication by `x` maps infimum over a nonempty type to infimum. See also `ennreal.infi_mul_of_ne` that assumes `x β‰  0` but does not require `[nonempty ΞΉ]`. -/ lemma infi_mul {ΞΉ} [nonempty ΞΉ] {f : ΞΉ β†’ ℝβ‰₯0∞} {x : ℝβ‰₯0∞} (h : x β‰  ∞) : infi f * x = β¨… i, f i * x := begin by_cases h0 : x = 0, { simp only [h0, mul_zero, infi_const] }, { exact infi_mul_of_ne h0 h } end /-- If `x β‰  ∞`, then left multiplication by `x` maps infimum over a nonempty type to infimum. See also `ennreal.mul_infi_of_ne` that assumes `x β‰  0` but does not require `[nonempty ΞΉ]`. -/ lemma mul_infi {ΞΉ} [nonempty ΞΉ] {f : ΞΉ β†’ ℝβ‰₯0∞} {x : ℝβ‰₯0∞} (h : x β‰  ∞) : x * infi f = β¨… i, x * f i := by simpa only [mul_comm] using infi_mul h /-- If `x β‰  0` and `x β‰  ∞`, then left multiplication by `x` maps infimum to infimum. See also `ennreal.mul_infi` that assumes `[nonempty ΞΉ]` but does not require `x β‰  0`. -/ lemma mul_infi_of_ne {ΞΉ} {f : ΞΉ β†’ ℝβ‰₯0∞} {x : ℝβ‰₯0∞} (h0 : x β‰  0) (h : x β‰  ∞) : x * infi f = β¨… i, x * f i := by simpa only [mul_comm] using infi_mul_of_ne h0 h /-! `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 end ennreal
a5f785e8e1c98a09bf4ec2d45f580e303b914de6
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/634.lean
1bdfce80c17c54d993151dd377be651834789829
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
293
lean
open nat namespace foo section parameter (X : Type) definition A {n : β„•} : Type := X variable {n : β„•} set_option pp.implicit true check @A n set_option pp.full_names true check @foo.A n check @A n set_option pp.full_names false check @foo.A n check @A n end end foo
8fdb7cf9e4d86a6df32698a67515f7c0fb999689
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/int/gcd.lean
cb56e3892c3600333949d6541338b5d14da01a02
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
12,039
lean
/- Copyright (c) 2018 Guy Leroy. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sangwoo Jo (aka Jason), Guy Leroy, Johannes HΓΆlzl, Mario Carneiro -/ import data.nat.prime /-! # Extended GCD and divisibility over β„€ ## Main definitions * Given `x y : β„•`, `xgcd x y` computes the pair of integers `(a, b)` such that `gcd x y = x * a + y * b`. `gcd_a x y` and `gcd_b x y` are defined to be `a` and `b`, respectively. ## Main statements * `gcd_eq_gcd_ab`: BΓ©zout's lemma, given `x y : β„•`, `gcd x y = x * gcd_a x y + y * gcd_b x y`. -/ /-! ### Extended Euclidean algorithm -/ namespace nat /-- Helper function for the extended GCD algorithm (`nat.xgcd`). -/ def xgcd_aux : β„• β†’ β„€ β†’ β„€ β†’ β„• β†’ β„€ β†’ β„€ β†’ β„• Γ— β„€ Γ— β„€ | 0 s t r' s' t' := (r', s', t') | r@(succ _) s t r' s' t' := have r' % r < r, from mod_lt _ $ succ_pos _, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t @[simp] theorem xgcd_zero_left {s t r' s' t'} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by simp [xgcd_aux] theorem xgcd_aux_rec {r s t r' s' t'} (h : 0 < r) : xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t := by cases r; [exact absurd h (lt_irrefl _), {simp only [xgcd_aux], refl}] /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : β„•) : β„€ Γ— β„€ := (xgcd_aux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a (x y : β„•) : β„€ := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : β„•) : β„€ := (xgcd x y).2 @[simp] theorem xgcd_aux_fst (x y) : βˆ€ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by simp) (Ξ» x y h IH s t s' t', by simp [xgcd_aux_rec, h, IH]; rw ← gcd_rec) theorem xgcd_aux_val (x y) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1]; cases xgcd_aux x 1 0 y 0 1; refl theorem xgcd_val (x y) : xgcd x y = (gcd_a x y, gcd_b x y) := by unfold gcd_a gcd_b; cases xgcd x y; refl section parameters (x y : β„•) private def P : β„• Γ— β„€ Γ— β„€ β†’ Prop | (r, s, t) := (r : β„€) = x * s + y * t theorem xgcd_aux_P {r r'} : βˆ€ {s t s' t'}, P (r, s, t) β†’ P (r', s', t') β†’ P (xgcd_aux r s t r' s' t') := gcd.induction r r' (by simp) $ Ξ» a b h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, dsimp [P] at *, rw [int.mod_def], generalize : (b / a : β„€) = k, rw [p, p'], simp [mul_add, mul_comm, mul_left_comm, add_comm, add_left_comm, sub_eq_neg_add, mul_assoc] end /-- BΓ©zout's lemma: given `x y : β„•`, `gcd x y = x * a + y * b`, where `a = gcd_a x y` and `b = gcd_b x y` are computed by the extended Euclidean algorithm. -/ theorem gcd_eq_gcd_ab : (gcd x y : β„€) = x * gcd_a x y + y * gcd_b x y := by have := @xgcd_aux_P x y x y 1 0 0 1 (by simp [P]) (by simp [P]); rwa [xgcd_aux_val, xgcd_val] at this end end nat /-! ### Divisibility over β„€ -/ namespace int theorem nat_abs_div (a b : β„€) (H : b ∣ a) : nat_abs (a / b) = (nat_abs a) / (nat_abs b) := begin cases (nat.eq_zero_or_pos (nat_abs b)), {rw eq_zero_of_nat_abs_eq_zero h, simp [int.div_zero]}, calc nat_abs (a / b) = nat_abs (a / b) * 1 : by rw mul_one ... = nat_abs (a / b) * (nat_abs b / nat_abs b) : by rw nat.div_self h ... = nat_abs (a / b) * nat_abs b / nat_abs b : by rw (nat.mul_div_assoc _ (dvd_refl _)) ... = nat_abs (a / b * b) / nat_abs b : by rw (nat_abs_mul (a / b) b) ... = nat_abs a / nat_abs b : by rw int.div_mul_cancel H, end theorem nat_abs_dvd_abs_iff {i j : β„€} : i.nat_abs ∣ j.nat_abs ↔ i ∣ j := ⟨assume (H : i.nat_abs ∣ j.nat_abs), dvd_nat_abs.mp (nat_abs_dvd.mp (coe_nat_dvd.mpr H)), assume H : (i ∣ j), coe_nat_dvd.mp (dvd_nat_abs.mpr (nat_abs_dvd.mpr H))⟩ lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : β„•} (p_prime : nat.prime p) {m n : β„€} {k l : β„•} (hpm : ↑(p ^ k) ∣ m) (hpn : ↑(p ^ l) ∣ n) (hpmn : ↑(p ^ (k+l+1)) ∣ m*n) : ↑(p ^ (k+1)) ∣ m ∨ ↑(p ^ (l+1)) ∣ n := have hpm' : p ^ k ∣ m.nat_abs, from int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpm, have hpn' : p ^ l ∣ n.nat_abs, from int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpn, have hpmn' : (p ^ (k+l+1)) ∣ m.nat_abs*n.nat_abs, by rw ←int.nat_abs_mul; apply (int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpmn), let hsd := nat.succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul p_prime hpm' hpn' hpmn' in hsd.elim (Ξ» hsd1, or.inl begin apply int.dvd_nat_abs.1, apply int.coe_nat_dvd.2 hsd1 end) (Ξ» hsd2, or.inr begin apply int.dvd_nat_abs.1, apply int.coe_nat_dvd.2 hsd2 end) theorem dvd_of_mul_dvd_mul_left {i j k : β„€} (k_non_zero : k β‰  0) (H : k * i ∣ k * j) : i ∣ j := dvd.elim H (Ξ»l H1, by rw mul_assoc at H1; exact ⟨_, mul_left_cancel' k_non_zero H1⟩) theorem dvd_of_mul_dvd_mul_right {i j k : β„€} (k_non_zero : k β‰  0) (H : i * k ∣ j * k) : i ∣ j := by rw [mul_comm i k, mul_comm j k] at H; exact dvd_of_mul_dvd_mul_left k_non_zero H lemma prime.dvd_nat_abs_of_coe_dvd_pow_two {p : β„•} (hp : p.prime) (k : β„€) (h : ↑p ∣ k ^ 2) : p ∣ k.nat_abs := begin apply @nat.prime.dvd_of_dvd_pow _ _ 2 hp, rwa [pow_two, ← nat_abs_mul, ← coe_nat_dvd_left, ← pow_two] end /-- β„€ specific version of least common multiple. -/ def lcm (i j : β„€) : β„• := nat.lcm (nat_abs i) (nat_abs j) theorem lcm_def (i j : β„€) : lcm i j = nat.lcm (nat_abs i) (nat_abs j) := rfl theorem gcd_dvd_left (i j : β„€) : (gcd i j : β„€) ∣ i := dvd_nat_abs.mp $ coe_nat_dvd.mpr $ nat.gcd_dvd_left _ _ theorem gcd_dvd_right (i j : β„€) : (gcd i j : β„€) ∣ j := dvd_nat_abs.mp $ coe_nat_dvd.mpr $ nat.gcd_dvd_right _ _ theorem dvd_gcd {i j k : β„€} (h1 : k ∣ i) (h2 : k ∣ j) : k ∣ gcd i j := nat_abs_dvd.1 $ coe_nat_dvd.2 $ nat.dvd_gcd (nat_abs_dvd_abs_iff.2 h1) (nat_abs_dvd_abs_iff.2 h2) theorem gcd_mul_lcm (i j : β„€) : gcd i j * lcm i j = nat_abs (i * j) := by rw [int.gcd, int.lcm, nat.gcd_mul_lcm, nat_abs_mul] theorem gcd_comm (i j : β„€) : gcd i j = gcd j i := nat.gcd_comm _ _ theorem gcd_assoc (i j k : β„€) : gcd (gcd i j) k = gcd i (gcd j k) := nat.gcd_assoc _ _ _ @[simp] theorem gcd_self (i : β„€) : gcd i i = nat_abs i := by simp [gcd] @[simp] theorem gcd_zero_left (i : β„€) : gcd 0 i = nat_abs i := by simp [gcd] @[simp] theorem gcd_zero_right (i : β„€) : gcd i 0 = nat_abs i := by simp [gcd] @[simp] theorem gcd_one_left (i : β„€) : gcd 1 i = 1 := nat.gcd_one_left _ @[simp] theorem gcd_one_right (i : β„€) : gcd i 1 = 1 := nat.gcd_one_right _ theorem gcd_mul_left (i j k : β„€) : gcd (i * j) (i * k) = nat_abs i * gcd j k := by { rw [int.gcd, int.gcd, nat_abs_mul, nat_abs_mul], apply nat.gcd_mul_left } theorem gcd_mul_right (i j k : β„€) : gcd (i * j) (k * j) = gcd i k * nat_abs j := by { rw [int.gcd, int.gcd, nat_abs_mul, nat_abs_mul], apply nat.gcd_mul_right } theorem gcd_pos_of_non_zero_left {i : β„€} (j : β„€) (i_non_zero : i β‰  0) : 0 < gcd i j := nat.gcd_pos_of_pos_left (nat_abs j) (nat_abs_pos_of_ne_zero i_non_zero) theorem gcd_pos_of_non_zero_right (i : β„€) {j : β„€} (j_non_zero : j β‰  0) : 0 < gcd i j := nat.gcd_pos_of_pos_right (nat_abs i) (nat_abs_pos_of_ne_zero j_non_zero) theorem gcd_eq_zero_iff {i j : β„€} : gcd i j = 0 ↔ i = 0 ∧ j = 0 := begin rw int.gcd, split, { intro h, exact ⟨nat_abs_eq_zero.mp (nat.eq_zero_of_gcd_eq_zero_left h), nat_abs_eq_zero.mp (nat.eq_zero_of_gcd_eq_zero_right h)⟩ }, { intro h, rw [nat_abs_eq_zero.mpr h.left, nat_abs_eq_zero.mpr h.right], apply nat.gcd_zero_left } end theorem gcd_div {i j k : β„€} (H1 : k ∣ i) (H2 : k ∣ j) : gcd (i / k) (j / k) = gcd i j / nat_abs k := by rw [gcd, nat_abs_div i k H1, nat_abs_div j k H2]; exact nat.gcd_div (nat_abs_dvd_abs_iff.mpr H1) (nat_abs_dvd_abs_iff.mpr H2) theorem gcd_div_gcd_div_gcd {i j : β„€} (H : 0 < gcd i j) : gcd (i / gcd i j) (j / gcd i j) = 1 := begin rw [gcd_div (gcd_dvd_left i j) (gcd_dvd_right i j)], rw [nat_abs_of_nat, nat.div_self H] end theorem gcd_dvd_gcd_of_dvd_left {i k : β„€} (j : β„€) (H : i ∣ k) : gcd i j ∣ gcd k j := int.coe_nat_dvd.1 $ dvd_gcd (dvd.trans (gcd_dvd_left i j) H) (gcd_dvd_right i j) theorem gcd_dvd_gcd_of_dvd_right {i k : β„€} (j : β„€) (H : i ∣ k) : gcd j i ∣ gcd j k := int.coe_nat_dvd.1 $ dvd_gcd (gcd_dvd_left j i) (dvd.trans (gcd_dvd_right j i) H) theorem gcd_dvd_gcd_mul_left (i j k : β„€) : gcd i j ∣ gcd (k * i) j := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right (i j k : β„€) : gcd i j ∣ gcd (i * k) j := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _) theorem gcd_dvd_gcd_mul_left_right (i j k : β„€) : gcd i j ∣ gcd i (k * j) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (i j k : β„€) : gcd i j ∣ gcd i (j * k) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _) theorem gcd_eq_left {i j : β„€} (H : i ∣ j) : gcd i j = nat_abs i := nat.dvd_antisymm (by unfold gcd; exact nat.gcd_dvd_left _ _) (by unfold gcd; exact nat.dvd_gcd (dvd_refl _) (nat_abs_dvd_abs_iff.mpr H)) theorem gcd_eq_right {i j : β„€} (H : j ∣ i) : gcd i j = nat_abs j := by rw [gcd_comm, gcd_eq_left H] theorem ne_zero_of_gcd {x y : β„€} (hc : gcd x y β‰  0) : x β‰  0 ∨ y β‰  0 := begin contrapose! hc, rw [hc.left, hc.right, gcd_zero_right, nat_abs_zero] end theorem exists_gcd_one {m n : β„€} (H : 0 < gcd m n) : βˆƒ (m' n' : β„€), gcd m' n' = 1 ∧ m = m' * gcd m n ∧ n = n' * gcd m n := ⟨_, _, gcd_div_gcd_div_gcd H, (int.div_mul_cancel (gcd_dvd_left m n)).symm, (int.div_mul_cancel (gcd_dvd_right m n)).symm⟩ theorem exists_gcd_one' {m n : β„€} (H : 0 < gcd m n) : βˆƒ (g : β„•) (m' n' : β„€), 0 < g ∧ gcd m' n' = 1 ∧ m = m' * g ∧ n = n' * g := let ⟨m', n', h⟩ := exists_gcd_one H in ⟨_, m', n', H, h⟩ theorem pow_dvd_pow_iff {m n : β„€} {k : β„•} (k0 : 0 < k) : m ^ k ∣ n ^ k ↔ m ∣ n := begin refine ⟨λ h, _, Ξ» h, pow_dvd_pow_of_dvd h _⟩, apply int.nat_abs_dvd_abs_iff.mp, apply (nat.pow_dvd_pow_iff k0).mp, rw [← int.nat_abs_pow, ← int.nat_abs_pow], exact int.nat_abs_dvd_abs_iff.mpr h end /-! ### lcm -/ theorem lcm_comm (i j : β„€) : lcm i j = lcm j i := by { rw [int.lcm, int.lcm], exact nat.lcm_comm _ _ } theorem lcm_assoc (i j k : β„€) : lcm (lcm i j) k = lcm i (lcm j k) := by { rw [int.lcm, int.lcm, int.lcm, int.lcm, nat_abs_of_nat, nat_abs_of_nat], apply nat.lcm_assoc } @[simp] theorem lcm_zero_left (i : β„€) : lcm 0 i = 0 := by { rw [int.lcm], apply nat.lcm_zero_left } @[simp] theorem lcm_zero_right (i : β„€) : lcm i 0 = 0 := by { rw [int.lcm], apply nat.lcm_zero_right } @[simp] theorem lcm_one_left (i : β„€) : lcm 1 i = nat_abs i := by { rw int.lcm, apply nat.lcm_one_left } @[simp] theorem lcm_one_right (i : β„€) : lcm i 1 = nat_abs i := by { rw int.lcm, apply nat.lcm_one_right } @[simp] theorem lcm_self (i : β„€) : lcm i i = nat_abs i := by { rw int.lcm, apply nat.lcm_self } theorem dvd_lcm_left (i j : β„€) : i ∣ lcm i j := by { rw int.lcm, apply coe_nat_dvd_right.mpr, apply nat.dvd_lcm_left } theorem dvd_lcm_right (i j : β„€) : j ∣ lcm i j := by { rw int.lcm, apply coe_nat_dvd_right.mpr, apply nat.dvd_lcm_right } theorem lcm_dvd {i j k : β„€} : i ∣ k β†’ j ∣ k β†’ (lcm i j : β„€) ∣ k := begin rw int.lcm, intros hi hj, exact coe_nat_dvd_left.mpr (nat.lcm_dvd (nat_abs_dvd_abs_iff.mpr hi) (nat_abs_dvd_abs_iff.mpr hj)) end end int lemma pow_gcd_eq_one {M : Type*} [monoid M] (x : M) {m n : β„•} (hm : x ^ m = 1) (hn : x ^ n = 1) : x ^ m.gcd n = 1 := begin cases m, { simp only [hn, nat.gcd_zero_left] }, obtain ⟨x, rfl⟩ : is_unit x, { apply is_unit_of_pow_eq_one _ _ hm m.succ_pos }, simp only [← units.coe_pow] at *, rw [← units.coe_one, ← gpow_coe_nat, ← units.ext_iff] at *, simp only [nat.gcd_eq_gcd_ab, gpow_add, gpow_mul, hm, hn, one_gpow, one_mul] end
d7448e36e9b23b64b01fdd8459cef2b82fd9cd46
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/geometry/manifold/algebra/monoid.lean
fa09c5e93041802a6d73154986e0601e78507440
[ "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
9,614
lean
/- Copyright Β© 2020 NicolΓ² Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: NicolΓ² Cavalleri -/ import geometry.manifold.times_cont_mdiff /-! # Smooth monoid A smooth monoid is a monoid that is also a smooth manifold, in which multiplication is a smooth map of the product manifold `G` Γ— `G` into `G`. In this file we define the basic structures to talk about smooth monoids: `has_smooth_mul` and its additive counterpart `has_smooth_add`. These structures are general enough to also talk about smooth semigroups. -/ section set_option old_structure_cmd true /-- 1. All smooth algebraic structures on `G` are `Prop`-valued classes that extend `smooth_manifold_with_corners I G`. This way we save users from adding both `[smooth_manifold_with_corners I G]` and `[has_smooth_mul I G]` to the assumptions. While many API lemmas hold true without the `smooth_manifold_with_corners I G` assumption, we're not aware of a mathematically interesting monoid on a topological manifold such that (a) the space is not a `smooth_manifold_with_corners`; (b) the multiplication is smooth at `(a, b)` in the charts `ext_chart_at I a`, `ext_chart_at I b`, `ext_chart_at I (a * b)`. 2. Because of `model_prod` we can't assume, e.g., that a `lie_group` is modelled on `π“˜(π•œ, E)`. So, we formulate the definitions and lemmas for any model. 3. While smoothness of an operation implies its continuity, lemmas like `has_continuous_mul_of_smooth` can't be instances becausen otherwise Lean would have to search for `has_smooth_mul I G` with unknown `π•œ`, `E`, `H`, and `I : model_with_corners π•œ E H`. If users needs `[has_continuous_mul G]` in a proof about a smooth monoid, then they need to either add `[has_continuous_mul G]` as an assumption (worse) or use `haveI` in the proof (better). -/ library_note "Design choices about smooth algebraic structures" /-- Basic hypothesis to talk about a smooth (Lie) additive monoid or a smooth additive semigroup. A smooth additive monoid over `Ξ±`, for example, is obtained by requiring both the instances `add_monoid Ξ±` and `has_smooth_add Ξ±`. -/ -- See note [Design choices about smooth algebraic structures] @[ancestor smooth_manifold_with_corners] class has_smooth_add {π•œ : Type*} [nondiscrete_normed_field π•œ] {H : Type*} [topological_space H] {E : Type*} [normed_group E] [normed_space π•œ E] (I : model_with_corners π•œ E H) (G : Type*) [has_add G] [topological_space G] [charted_space H G] extends smooth_manifold_with_corners I G : Prop := (smooth_add : smooth (I.prod I) I (Ξ» p : GΓ—G, p.1 + p.2)) /-- Basic hypothesis to talk about a smooth (Lie) monoid or a smooth semigroup. A smooth monoid over `G`, for example, is obtained by requiring both the instances `monoid G` and `has_smooth_mul I G`. -/ -- See note [Design choices about smooth algebraic structures] @[ancestor smooth_manifold_with_corners, to_additive] class has_smooth_mul {π•œ : Type*} [nondiscrete_normed_field π•œ] {H : Type*} [topological_space H] {E : Type*} [normed_group E] [normed_space π•œ E] (I : model_with_corners π•œ E H) (G : Type*) [has_mul G] [topological_space G] [charted_space H G] extends smooth_manifold_with_corners I G : Prop := (smooth_mul : smooth (I.prod I) I (Ξ» p : GΓ—G, p.1 * p.2)) end section has_smooth_mul variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {H : Type*} [topological_space H] {E : Type*} [normed_group E] [normed_space π•œ E] {I : model_with_corners π•œ E H} {G : Type*} [has_mul G] [topological_space G] [charted_space H G] [has_smooth_mul I G] {E' : Type*} [normed_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] {I' : model_with_corners π•œ E' H'} {M : Type*} [topological_space M] [charted_space H' M] section variables (I) @[to_additive] lemma smooth_mul : smooth (I.prod I) I (Ξ» p : GΓ—G, p.1 * p.2) := has_smooth_mul.smooth_mul /-- If the multiplication is smooth, then it is continuous. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]. -/ @[to_additive "If the addition is smooth, then it is continuous. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]."] lemma has_continuous_mul_of_smooth : has_continuous_mul G := ⟨(smooth_mul I).continuous⟩ end @[to_additive] lemma smooth.mul {f : M β†’ G} {g : M β†’ G} (hf : smooth I' I f) (hg : smooth I' I g) : smooth I' I (f * g) := (smooth_mul I).comp (hf.prod_mk hg) @[to_additive] lemma smooth_mul_left {a : G} : smooth I I (Ξ» b : G, a * b) := smooth_const.mul smooth_id @[to_additive] lemma smooth_mul_right {a : G} : smooth I I (Ξ» b : G, b * a) := smooth_id.mul smooth_const @[to_additive] lemma smooth_on.mul {f : M β†’ G} {g : M β†’ G} {s : set M} (hf : smooth_on I' I f s) (hg : smooth_on I' I g s) : smooth_on I' I (f * g) s := ((smooth_mul I).comp_smooth_on (hf.prod_mk hg) : _) /- Instance of product -/ @[to_additive] instance has_smooth_mul.prod {π•œ : Type*} [nondiscrete_normed_field π•œ] {E : Type*} [normed_group E] [normed_space π•œ E] {H : Type*} [topological_space H] (I : model_with_corners π•œ E H) (G : Type*) [topological_space G] [charted_space H G] [has_mul G] [has_smooth_mul I G] {E' : Type*} [normed_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] (I' : model_with_corners π•œ E' H') (G' : Type*) [topological_space G'] [charted_space H' G'] [has_mul G'] [has_smooth_mul I' G'] : has_smooth_mul (I.prod I') (GΓ—G') := { smooth_mul := ((smooth_fst.comp smooth_fst).smooth.mul (smooth_fst.comp smooth_snd)).prod_mk ((smooth_snd.comp smooth_fst).smooth.mul (smooth_snd.comp smooth_snd)), .. smooth_manifold_with_corners.prod G G' } variable (I) end has_smooth_mul section monoid variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {H : Type*} [topological_space H] {E : Type*} [normed_group E] [normed_space π•œ E] {I : model_with_corners π•œ E H} {G : Type*} [monoid G] [topological_space G] [charted_space H G] [has_smooth_mul I G] {H' : Type*} [topological_space H'] {E' : Type*} [normed_group E'] [normed_space π•œ E'] {I' : model_with_corners π•œ E' H'} {G' : Type*} [monoid G'] [topological_space G'] [charted_space H' G'] [has_smooth_mul I' G'] lemma smooth_pow : βˆ€ n : β„•, smooth I I (Ξ» a : G, a ^ n) | 0 := by { simp only [pow_zero], exact smooth_const } | (k+1) := by simpa [pow_succ] using smooth_id.mul (smooth_pow _) /-- Morphism of additive smooth monoids. -/ structure smooth_add_monoid_morphism (I : model_with_corners π•œ E H) (I' : model_with_corners π•œ E' H') (G : Type*) [topological_space G] [charted_space H G] [add_monoid G] [has_smooth_add I G] (G' : Type*) [topological_space G'] [charted_space H' G'] [add_monoid G'] [has_smooth_add I' G'] extends G β†’+ G' := (smooth_to_fun : smooth I I' to_fun) /-- Morphism of smooth monoids. -/ @[to_additive] structure smooth_monoid_morphism (I : model_with_corners π•œ E H) (I' : model_with_corners π•œ E' H') (G : Type*) [topological_space G] [charted_space H G] [monoid G] [has_smooth_mul I G] (G' : Type*) [topological_space G'] [charted_space H' G'] [monoid G'] [has_smooth_mul I' G'] extends G β†’* G' := (smooth_to_fun : smooth I I' to_fun) @[to_additive] instance : has_one (smooth_monoid_morphism I I' G G') := ⟨{ smooth_to_fun := smooth_const, to_monoid_hom := 1 }⟩ @[to_additive] instance : inhabited (smooth_monoid_morphism I I' G G') := ⟨1⟩ @[to_additive] instance : has_coe_to_fun (smooth_monoid_morphism I I' G G') := ⟨_, Ξ» a, a.to_fun⟩ end monoid section comm_monoid open_locale big_operators variables {π•œ : Type*} [nondiscrete_normed_field π•œ] {H : Type*} [topological_space H] {E : Type*} [normed_group E] [normed_space π•œ E] {I : model_with_corners π•œ E H} {G : Type*} [comm_monoid G] [topological_space G] [charted_space H G] [has_smooth_mul I G] {E' : Type*} [normed_group E'] [normed_space π•œ E'] {H' : Type*} [topological_space H'] {I' : model_with_corners π•œ E' H'} {M : Type*} [topological_space M] [charted_space H' M] @[to_additive] lemma smooth_finset_prod' {ΞΉ} {s : finset ΞΉ} {f : ΞΉ β†’ M β†’ G} (h : βˆ€ i ∈ s, smooth I' I (f i)) : smooth I' I (∏ i in s, f i) := finset.prod_induction _ _ (Ξ» f g hf hg, hf.mul hg) (@smooth_const _ _ _ _ _ _ _ I' _ _ _ _ _ _ _ _ I _ _ _ 1) h @[to_additive] lemma smooth_finset_prod {ΞΉ} {s : finset ΞΉ} {f : ΞΉ β†’ M β†’ G} (h : βˆ€ i ∈ s, smooth I' I (f i)) : smooth I' I (Ξ» x, ∏ i in s, f i x) := by { simp only [← finset.prod_apply], exact smooth_finset_prod' h } open function filter @[to_additive] lemma smooth_finprod {ΞΉ} {f : ΞΉ β†’ M β†’ G} (h : βˆ€ i, smooth I' I (f i)) (hfin : locally_finite (Ξ» i, mul_support (f i))) : smooth I' I (Ξ» x, ∏ᢠ i, f i x) := begin intro x, rcases hfin x with ⟨U, hxU, hUf⟩, have : smooth_at I' I (Ξ» x, ∏ i in hUf.to_finset, f i x) x, from smooth_finset_prod (Ξ» i hi, h i) x, refine this.congr_of_eventually_eq (mem_sets_of_superset hxU $ Ξ» y hy, _), refine finprod_eq_prod_of_mul_support_subset _ (Ξ» i hi, _), rw [hUf.coe_to_finset], exact ⟨y, hi, hy⟩ end @[to_additive] lemma smooth_finprod_cond {ΞΉ} {f : ΞΉ β†’ M β†’ G} {p : ΞΉ β†’ Prop} (hc : βˆ€ i, p i β†’ smooth I' I (f i)) (hf : locally_finite (Ξ» i, mul_support (f i))) : smooth I' I (Ξ» x, ∏ᢠ i (hi : p i), f i x) := begin simp only [← finprod_subtype_eq_finprod_cond], exact smooth_finprod (Ξ» i, hc i i.2) (hf.comp_injective subtype.coe_injective) end end comm_monoid
10444bde5bd0a04b3dc122bc4260e9687445c655
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/deprecated/submonoid.lean
8e22bee60522dcd90bba4fb64a3b92e7cfc6a8c6
[ "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
20,462
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, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard -/ import group_theory.submonoid /-! # Submonoids This file defines unbundled multiplicative and additive submonoids (deprecated). For bundled form see `group_theory/submonoid`. We some results about images and preimages of submonoids under monoid homomorphisms. These theorems use unbundled monoid homomorphisms (also deprecated). There are also theorems about the submonoids generated by an element or a subset of a monoid, defined inductively. ## Implementation notes Unbundled submonoids will slowly be removed from mathlib. ## Tags submonoid, submonoids, is_submonoid -/ open_locale big_operators variables {M : Type*} [monoid M] {s : set M} variables {A : Type*} [add_monoid A] {t : set A} /-- `s` is an additive submonoid: a set containing 0 and closed under addition. -/ class is_add_submonoid (s : set A) : Prop := (zero_mem : (0:A) ∈ s) (add_mem {a b} : a ∈ s β†’ b ∈ s β†’ a + b ∈ s) /-- `s` is a submonoid: a set containing 1 and closed under multiplication. -/ @[to_additive is_add_submonoid] class is_submonoid (s : set M) : Prop := (one_mem : (1:M) ∈ s) (mul_mem {a b} : a ∈ s β†’ b ∈ s β†’ a * b ∈ s) lemma additive.is_add_submonoid (s : set M) : βˆ€ [is_submonoid s], @is_add_submonoid (additive M) _ s | ⟨h₁, hβ‚‚βŸ© := ⟨h₁, @hβ‚‚βŸ© theorem additive.is_add_submonoid_iff {s : set M} : @is_add_submonoid (additive M) _ s ↔ is_submonoid s := ⟨λ ⟨h₁, hβ‚‚βŸ©, ⟨h₁, @hβ‚‚βŸ©, Ξ» h, by exactI additive.is_add_submonoid _⟩ lemma multiplicative.is_submonoid (s : set A) : βˆ€ [is_add_submonoid s], @is_submonoid (multiplicative A) _ s | ⟨h₁, hβ‚‚βŸ© := ⟨h₁, @hβ‚‚βŸ© theorem multiplicative.is_submonoid_iff {s : set A} : @is_submonoid (multiplicative A) _ s ↔ is_add_submonoid s := ⟨λ ⟨h₁, hβ‚‚βŸ©, ⟨h₁, @hβ‚‚βŸ©, Ξ» h, by exactI multiplicative.is_submonoid _⟩ /-- The intersection of two submonoids of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The intersection of two `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of M."] instance is_submonoid.inter (s₁ sβ‚‚ : set M) [is_submonoid s₁] [is_submonoid sβ‚‚] : is_submonoid (s₁ ∩ sβ‚‚) := { one_mem := ⟨is_submonoid.one_mem, is_submonoid.one_mem⟩, mul_mem := Ξ» x y hx hy, ⟨is_submonoid.mul_mem hx.1 hy.1, is_submonoid.mul_mem hx.2 hy.2⟩ } /-- The intersection of an indexed set of submonoids of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The intersection of an indexed set of `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of `M`."] instance is_submonoid.Inter {ΞΉ : Sort*} (s : ΞΉ β†’ set M) [h : βˆ€ y : ΞΉ, is_submonoid (s y)] : is_submonoid (set.Inter s) := { one_mem := set.mem_Inter.2 $ Ξ» y, is_submonoid.one_mem, mul_mem := Ξ» x₁ xβ‚‚ h₁ hβ‚‚, set.mem_Inter.2 $ Ξ» y, is_submonoid.mul_mem (set.mem_Inter.1 h₁ y) (set.mem_Inter.1 hβ‚‚ y) } /-- The union of an indexed, directed, nonempty set of submonoids of a monoid `M` is a submonoid of `M`. -/ @[to_additive is_add_submonoid_Union_of_directed "The union of an indexed, directed, nonempty set of `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of `M`. "] lemma is_submonoid_Union_of_directed {ΞΉ : Type*} [hΞΉ : nonempty ΞΉ] (s : ΞΉ β†’ set M) [βˆ€ i, is_submonoid (s i)] (directed : βˆ€ i j, βˆƒ k, s i βŠ† s k ∧ s j βŠ† s k) : is_submonoid (⋃i, s i) := { one_mem := let ⟨i⟩ := hΞΉ in set.mem_Union.2 ⟨i, is_submonoid.one_mem⟩, mul_mem := Ξ» a b ha hb, let ⟨i, hi⟩ := set.mem_Union.1 ha in let ⟨j, hj⟩ := set.mem_Union.1 hb in let ⟨k, hk⟩ := directed i j in set.mem_Union.2 ⟨k, is_submonoid.mul_mem (hk.1 hi) (hk.2 hj)⟩ } section powers /-- The set of natural number powers `1, x, xΒ², ...` of an element `x` of a monoid. -/ def powers (x : M) : set M := {y | βˆƒ n:β„•, x^n = y} /-- The set of natural number multiples `0, x, 2x, ...` of an element `x` of an `add_monoid`. -/ def multiples (x : A) : set A := {y | βˆƒ n:β„•, n β€’β„• x = y} attribute [to_additive multiples] powers /-- 1 is in the set of natural number powers of an element of a monoid. -/ lemma powers.one_mem {x : M} : (1 : M) ∈ powers x := ⟨0, pow_zero _⟩ /-- 0 is in the set of natural number multiples of an element of an `add_monoid`. -/ lemma multiples.zero_mem {x : A} : (0 : A) ∈ multiples x := ⟨0, zero_nsmul _⟩ attribute [to_additive] powers.one_mem /-- An element of a monoid is in the set of that element's natural number powers. -/ lemma powers.self_mem {x : M} : x ∈ powers x := ⟨1, pow_one _⟩ /-- An element of an `add_monoid` is in the set of that element's natural number multiples. -/ lemma multiples.self_mem {x : A} : x ∈ multiples x := ⟨1, one_nsmul _⟩ attribute [to_additive] powers.self_mem /-- The set of natural number powers of an element of a monoid is closed under multiplication. -/ lemma powers.mul_mem {x y z : M} : (y ∈ powers x) β†’ (z ∈ powers x) β†’ (y * z ∈ powers x) := Ξ» ⟨n₁, hβ‚βŸ© ⟨nβ‚‚, hβ‚‚βŸ©, ⟨n₁ + nβ‚‚, by simp only [pow_add, *]⟩ /-- The set of natural number multiples of an element of an `add_monoid` is closed under addition. -/ lemma multiples.add_mem {x y z : A} : (y ∈ multiples x) β†’ (z ∈ multiples x) β†’ (y + z ∈ multiples x) := @powers.mul_mem (multiplicative A) _ _ _ _ attribute [to_additive] powers.mul_mem /-- The set of natural number powers of an element of a monoid `M` is a submonoid of `M`. -/ @[to_additive is_add_submonoid "The set of natural number multiples of an element of an `add_monoid` `M` is an `add_submonoid` of `M`."] instance powers.is_submonoid (x : M) : is_submonoid (powers x) := { one_mem := powers.one_mem, mul_mem := Ξ» y z, powers.mul_mem } /-- A monoid is a submonoid of itself. -/ @[to_additive is_add_submonoid "An `add_monoid` is an `add_submonoid` of itself."] instance univ.is_submonoid : is_submonoid (@set.univ M) := by split; simp /-- The preimage of a submonoid under a monoid hom is a submonoid of the domain. -/ @[to_additive is_add_submonoid "The preimage of an `add_submonoid` under an `add_monoid` hom is an `add_submonoid` of the domain."] instance preimage.is_submonoid {N : Type*} [monoid N] (f : M β†’ N) [is_monoid_hom f] (s : set N) [is_submonoid s] : is_submonoid (f ⁻¹' s) := { one_mem := show f 1 ∈ s, by rw is_monoid_hom.map_one f; exact is_submonoid.one_mem, mul_mem := Ξ» a b (ha : f a ∈ s) (hb : f b ∈ s), show f (a * b) ∈ s, by rw is_monoid_hom.map_mul f; exact is_submonoid.mul_mem ha hb } /-- The image of a submonoid under a monoid hom is a submonoid of the codomain. -/ @[instance, to_additive is_add_submonoid "The image of an `add_submonoid` under an `add_monoid` hom is an `add_submonoid` of the codomain."] lemma image.is_submonoid {Ξ³ : Type*} [monoid Ξ³] (f : M β†’ Ξ³) [is_monoid_hom f] (s : set M) [is_submonoid s] : is_submonoid (f '' s) := { one_mem := ⟨1, is_submonoid.one_mem, is_monoid_hom.map_one f⟩, mul_mem := Ξ» a b ⟨x, hx⟩ ⟨y, hy⟩, ⟨x * y, is_submonoid.mul_mem hx.1 hy.1, by rw [is_monoid_hom.map_mul f, hx.2, hy.2]⟩ } /-- The image of a monoid hom is a submonoid of the codomain. -/ @[to_additive is_add_submonoid "The image of an `add_monoid` hom is an `add_submonoid` of the codomain."] instance range.is_submonoid {Ξ³ : Type*} [monoid Ξ³] (f : M β†’ Ξ³) [is_monoid_hom f] : is_submonoid (set.range f) := by rw ← set.image_univ; apply_instance /-- Submonoids are closed under natural powers. -/ lemma is_submonoid.pow_mem {a : M} [is_submonoid s] (h : a ∈ s) : βˆ€ {n : β„•}, a ^ n ∈ s | 0 := is_submonoid.one_mem | (n + 1) := is_submonoid.mul_mem h is_submonoid.pow_mem /-- An `add_submonoid` is closed under multiplication by naturals. -/ lemma is_add_submonoid.smul_mem {a : A} [is_add_submonoid t] : βˆ€ (h : a ∈ t) {n : β„•}, n β€’β„• a ∈ t := @is_submonoid.pow_mem (multiplicative A) _ _ _ (multiplicative.is_submonoid _) attribute [to_additive smul_mem] is_submonoid.pow_mem /-- The set of natural number powers of an element of a submonoid is a subset of the submonoid. -/ lemma is_submonoid.power_subset {a : M} [is_submonoid s] (h : a ∈ s) : powers a βŠ† s := assume x ⟨n, hx⟩, hx β–Έ is_submonoid.pow_mem h /-- The set of natural number multiples of an element of an `add_submonoid` is a subset of the `add_submonoid`. -/ lemma is_add_submonoid.multiple_subset {a : A} [is_add_submonoid t] : a ∈ t β†’ multiples a βŠ† t := @is_submonoid.power_subset (multiplicative A) _ _ _ (multiplicative.is_submonoid _) attribute [to_additive multiple_subset] is_submonoid.power_subset end powers namespace is_submonoid /-- The product of a list of elements of a submonoid is an element of the submonoid. -/ @[to_additive "The sum of a list of elements of an `add_submonoid` is an element of the `add_submonoid`."] lemma list_prod_mem [is_submonoid s] : βˆ€{l : list M}, (βˆ€x∈l, x ∈ s) β†’ l.prod ∈ s | [] h := one_mem | (a::l) h := suffices a * l.prod ∈ s, by simpa, have a ∈ s ∧ (βˆ€x∈l, x ∈ s), by simpa using h, is_submonoid.mul_mem this.1 (list_prod_mem this.2) /-- The product of a multiset of elements of a submonoid of a `comm_monoid` is an element of the submonoid. -/ @[to_additive "The sum of a multiset of elements of an `add_submonoid` of an `add_comm_monoid` is an element of the `add_submonoid`. "] lemma multiset_prod_mem {M} [comm_monoid M] (s : set M) [is_submonoid s] (m : multiset M) : (βˆ€a∈m, a ∈ s) β†’ m.prod ∈ s := begin refine quotient.induction_on m (assume l hl, _), rw [multiset.quot_mk_to_coe, multiset.coe_prod], exact list_prod_mem hl end /-- The product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is an element of the submonoid. -/ @[to_additive "The sum of elements of an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is an element of the `add_submonoid`."] lemma finset_prod_mem {M A} [comm_monoid M] (s : set M) [is_submonoid s] (f : A β†’ M) : βˆ€(t : finset A), (βˆ€b∈t, f b ∈ s) β†’ ∏ b in t, f b ∈ s | ⟨m, hm⟩ hs := begin refine multiset_prod_mem s _ _, simp, rintros a b hb rfl, exact hs _ hb end end is_submonoid -- TODO: modify `subtype_instance` to produce this definition, then use it here -- and for `subtype.group` /-- Submonoids are themselves monoids. -/ @[to_additive add_monoid "An `add_submonoid` is itself an `add_monoid`."] instance subtype.monoid {s : set M} [is_submonoid s] : monoid s := { one := ⟨1, is_submonoid.one_mem⟩, mul := Ξ» x y, ⟨x * y, is_submonoid.mul_mem x.2 y.2⟩, mul_one := Ξ» x, subtype.eq $ mul_one x.1, one_mul := Ξ» x, subtype.eq $ one_mul x.1, mul_assoc := Ξ» x y z, subtype.eq $ mul_assoc x.1 y.1 z.1 } /-- Submonoids of commutative monoids are themselves commutative monoids. -/ @[to_additive add_comm_monoid "An `add_submonoid` of a commutative `add_monoid` is itself a commutative `add_monoid`. "] instance subtype.comm_monoid {M} [comm_monoid M] {s : set M} [is_submonoid s] : comm_monoid s := { mul_comm := Ξ» x y, subtype.eq $ mul_comm x.1 y.1, .. subtype.monoid } /-- Submonoids inherit the 1 of the monoid. -/ @[simp, norm_cast, to_additive "An `add_submonoid` inherits the 0 of the `add_monoid`. "] lemma is_submonoid.coe_one [is_submonoid s] : ((1 : s) : M) = 1 := rfl attribute [norm_cast] is_add_submonoid.coe_zero /-- Submonoids inherit the multiplication of the monoid. -/ @[simp, norm_cast, to_additive "An `add_submonoid` inherits the addition of the `add_monoid`. "] lemma is_submonoid.coe_mul [is_submonoid s] (a b : s) : ((a * b : s) : M) = a * b := rfl attribute [norm_cast] is_add_submonoid.coe_add /-- Submonoids inherit the exponentiation by naturals of the monoid. -/ @[simp, norm_cast] lemma is_submonoid.coe_pow [is_submonoid s] (a : s) (n : β„•) : ((a ^ n : s) : M) = a ^ n := by induction n; simp [*, pow_succ] /-- An `add_submonoid` inherits the multiplication by naturals of the `add_monoid`. -/ @[simp, norm_cast] lemma is_add_submonoid.smul_coe {A : Type*} [add_monoid A] {s : set A} [is_add_submonoid s] (a : s) (n : β„•) : ((n β€’β„• a : s) : A) = n β€’β„• a := by {induction n, refl, simp [*, succ_nsmul]} attribute [to_additive smul_coe] is_submonoid.coe_pow /-- The natural injection from a submonoid into the monoid is a monoid hom. -/ @[to_additive is_add_monoid_hom "The natural injection from an `add_submonoid` into the `add_monoid` is an `add_monoid` hom. "] instance subtype_val.is_monoid_hom [is_submonoid s] : is_monoid_hom (subtype.val : s β†’ M) := { map_one := rfl, map_mul := Ξ» _ _, rfl } /-- The natural injection from a submonoid into the monoid is a monoid hom. -/ @[to_additive is_add_monoid_hom "The natural injection from an `add_submonoid` into the `add_monoid` is an `add_monoid` hom. "] instance coe.is_monoid_hom [is_submonoid s] : is_monoid_hom (coe : s β†’ M) := subtype_val.is_monoid_hom /-- Given a monoid hom `f : Ξ³ β†’ M` whose image is contained in a submonoid `s`, the induced map from `Ξ³` to `s` is a monoid hom. -/ @[to_additive is_add_monoid_hom "Given an `add_monoid` hom `f : Ξ³ β†’ M` whose image is contained in an `add_submonoid` s, the induced map from `Ξ³` to `s` is an `add_monoid` hom."] instance subtype_mk.is_monoid_hom {Ξ³ : Type*} [monoid Ξ³] [is_submonoid s] (f : Ξ³ β†’ M) [is_monoid_hom f] (h : βˆ€ x, f x ∈ s) : is_monoid_hom (Ξ» x, (⟨f x, h x⟩ : s)) := { map_one := subtype.eq (is_monoid_hom.map_one f), map_mul := Ξ» x y, subtype.eq (is_monoid_hom.map_mul f x y) } /-- Given two submonoids `s` and `t` such that `s βŠ† t`, the natural injection from `s` into `t` is a monoid hom. -/ @[to_additive is_add_monoid_hom "Given two `add_submonoid`s `s` and `t` such that `s βŠ† t`, the natural injection from `s` into `t` is an `add_monoid` hom."] instance set_inclusion.is_monoid_hom (t : set M) [is_submonoid s] [is_submonoid t] (h : s βŠ† t) : is_monoid_hom (set.inclusion h) := subtype_mk.is_monoid_hom _ _ namespace add_monoid /-- The inductively defined membership predicate for the submonoid generated by a subset of a monoid. -/ inductive in_closure (s : set A) : A β†’ Prop | basic {a : A} : a ∈ s β†’ in_closure a | zero : in_closure 0 | add {a b : A} : in_closure a β†’ in_closure b β†’ in_closure (a + b) end add_monoid namespace monoid /-- The inductively defined membership predicate for the `add_submonoid` generated by a subset of an add_monoid. -/ inductive in_closure (s : set M) : M β†’ Prop | basic {a : M} : a ∈ s β†’ in_closure a | one : in_closure 1 | mul {a b : M} : in_closure a β†’ in_closure b β†’ in_closure (a * b) attribute [to_additive] monoid.in_closure attribute [to_additive] monoid.in_closure.one attribute [to_additive] monoid.in_closure.mul /-- The inductively defined submonoid generated by a subset of a monoid. -/ @[to_additive "The inductively defined `add_submonoid` genrated by a subset of an `add_monoid`."] def closure (s : set M) : set M := {a | in_closure s a } @[to_additive is_add_submonoid] instance closure.is_submonoid (s : set M) : is_submonoid (closure s) := { one_mem := in_closure.one, mul_mem := assume a b, in_closure.mul } /-- A subset of a monoid is contained in the submonoid it generates. -/ @[to_additive "A subset of an `add_monoid` is contained in the `add_submonoid` it generates."] theorem subset_closure {s : set M} : s βŠ† closure s := assume a, in_closure.basic /-- The submonoid generated by a set is contained in any submonoid that contains the set. -/ @[to_additive "The `add_submonoid` generated by a set is contained in any `add_submonoid` that contains the set."] theorem closure_subset {s t : set M} [is_submonoid t] (h : s βŠ† t) : closure s βŠ† t := assume a ha, by induction ha; simp [h _, *, is_submonoid.one_mem, is_submonoid.mul_mem] /-- Given subsets `t` and `s` of a monoid `M`, if `s βŠ† t`, the submonoid of `M` generated by `s` is contained in the submonoid generated by `t`. -/ @[to_additive "Given subsets `t` and `s` of an `add_monoid M`, if `s βŠ† t`, the `add_submonoid` of `M` generated by `s` is contained in the `add_submonoid` generated by `t`."] theorem closure_mono {s t : set M} (h : s βŠ† t) : closure s βŠ† closure t := closure_subset $ set.subset.trans h subset_closure /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ @[to_additive "The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number multiples of the element."] theorem closure_singleton {x : M} : closure ({x} : set M) = powers x := set.eq_of_subset_of_subset (closure_subset $ set.singleton_subset_iff.2 $ powers.self_mem) $ is_submonoid.power_subset $ set.singleton_subset_iff.1 $ subset_closure /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set under the monoid hom. -/ @[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set under the `add_monoid` hom."] lemma image_closure {A : Type*} [monoid A] (f : M β†’ A) [is_monoid_hom f] (s : set M) : f '' closure s = closure (f '' s) := le_antisymm begin rintros _ ⟨x, hx, rfl⟩, apply in_closure.rec_on hx; intros, { solve_by_elim [subset_closure, set.mem_image_of_mem] }, { rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem }, { rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] } end (closure_subset $ set.image_subset _ subset_closure) /-- Given an element `a` of the submonoid of a monoid `M` generated by a set `s`, there exists a list of elements of `s` whose product is `a`. -/ @[to_additive "Given an element `a` of the `add_submonoid` of an `add_monoid M` generated by a set `s`, there exists a list of elements of `s` whose sum is `a`."] theorem exists_list_of_mem_closure {s : set M} {a : M} (h : a ∈ closure s) : (βˆƒl:list M, (βˆ€x∈l, x ∈ s) ∧ l.prod = a) := begin induction h, case in_closure.basic : a ha { existsi ([a]), simp [ha] }, case in_closure.one { existsi ([]), simp }, case in_closure.mul : a b _ _ ha hb { rcases ha with ⟨la, ha, eqa⟩, rcases hb with ⟨lb, hb, eqb⟩, existsi (la ++ lb), simp [eqa.symm, eqb.symm, or_imp_distrib], exact assume a, ⟨ha a, hb a⟩ } end /-- Given sets `s, t` of a commutative monoid `M`, `x ∈ M` is in the submonoid of `M` generated by `s βˆͺ t` iff there exists an element of the submonoid generated by `s` and an element of the submonoid generated by `t` whose product is `x`. -/ @[to_additive "Given sets `s, t` of a commutative `add_monoid M`, `x ∈ M` is in the `add_submonoid` of `M` generated by `s βˆͺ t` iff there exists an element of the `add_submonoid` generated by `s` and an element of the `add_submonoid` generated by `t` whose sum is `x`."] theorem mem_closure_union_iff {M : Type*} [comm_monoid M] {s t : set M} {x : M} : x ∈ closure (s βˆͺ t) ↔ βˆƒ y ∈ closure s, βˆƒ z ∈ closure t, y * z = x := ⟨λ hx, let ⟨L, HL1, HL2⟩ := exists_list_of_mem_closure hx in HL2 β–Έ list.rec_on L (Ξ» _, ⟨1, is_submonoid.one_mem, 1, is_submonoid.one_mem, mul_one _⟩) (Ξ» hd tl ih HL1, let ⟨y, hy, z, hz, hyzx⟩ := ih (list.forall_mem_of_forall_mem_cons HL1) in or.cases_on (HL1 hd $ list.mem_cons_self _ _) (Ξ» hs, ⟨hd * y, is_submonoid.mul_mem (subset_closure hs) hy, z, hz, by rw [mul_assoc, list.prod_cons, ← hyzx]; refl⟩) (Ξ» ht, ⟨y, hy, z * hd, is_submonoid.mul_mem hz (subset_closure ht), by rw [← mul_assoc, list.prod_cons, ← hyzx, mul_comm hd]; refl⟩)) HL1, Ξ» ⟨y, hy, z, hz, hyzx⟩, hyzx β–Έ is_submonoid.mul_mem (closure_mono (set.subset_union_left _ _) hy) (closure_mono (set.subset_union_right _ _) hz)⟩ end monoid /-- Create a bundled submonoid from a set `s` and `[is_submonoid s]`. -/ @[to_additive "Create a bundled additive submonoid from a set `s` and `[is_add_submonoid s]`."] def submonoid.of (s : set M) [h : is_submonoid s] : submonoid M := ⟨s, h.1, h.2⟩ @[to_additive] instance submonoid.is_submonoid (S : submonoid M) : is_submonoid (S : set M) := ⟨S.2, S.3⟩
c3a971eb9b3037cc4a29429ff138bd5b01d5fea0
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/playground/view_expander.lean
bff152690e72dc233e05d057feb563732ee0165f
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,319
lean
import init.lean.expander open Lean open Lean.Parser open Lean.Expander def prof {Ξ± : Type} (msg : String) (p : IO Ξ±) : IO Ξ± := let msg₁ := "Time for '" ++ msg ++ "':" in let msgβ‚‚ := "Memory usage for '" ++ msg ++ "':" in allocprof msgβ‚‚ (timeit msg₁ p) @[derive HasView] def leaf.Parser : termParser := node! leaf ["foo"] @[derive HasView] def node.Parser : termParser := node! node [children: Combinators.many Term.Parser] def node.arity := 4 def mkStx : β„• β†’ Syntax | 0 := review leaf {} | (n+1) := review node $ ⟨(List.replicate node.arity Syntax.missing).map (Ξ» _, mkStx n)⟩ def cfg : FrontendConfig := {filename := "foo", fileMap := FileMap.fromString "", input := ""} def test (transformers : List (Name Γ— transformer)) (stx : Syntax) : IO Unit := match expand stx {cfg with transformers := RBMap.fromList transformers _} with | Except.ok _ := pure () | Except.error e := throw e.toString def testNoOp := test [] def testNoExp := test [(`node, Ξ» stx, noExpansion)] def testSimple := test [(`node, Ξ» stx, pure $ Syntax.mkNode ⟨`node2⟩ $ let v := view node stx in v.children)] def main (xs : List String) : IO Unit := do let stx := mkStx 11, --xs.head.toNat, prof "testNoOp" $ testNoOp stx, prof "testNoExp" $ testNoExp stx, prof "testSimple" $ testSimple stx, pure ()
1d4917eb52be07f2ee51a36f823c892e9fc7535f
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/list/nodup.lean
266f073427c39bd57730147659cd5a502ebce4d8
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
14,401
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau -/ import data.list.lattice import data.list.pairwise import data.list.forall2 /-! # Lists with no duplicates `list.nodup` is defined in `data/list/defs`. In this file we prove various properties of this predicate. -/ universes u v open nat function variables {Ξ± : Type u} {Ξ² : Type v} {l l₁ lβ‚‚ : list Ξ±} {a b : Ξ±} namespace list @[simp] theorem forall_mem_ne {a : Ξ±} {l : list Ξ±} : (βˆ€ (a' : Ξ±), a' ∈ l β†’ Β¬a = a') ↔ a βˆ‰ l := ⟨λ h m, h _ m rfl, Ξ» h a' m e, h (e.symm β–Έ m)⟩ @[simp] theorem nodup_nil : @nodup Ξ± [] := pairwise.nil @[simp] theorem nodup_cons {a : Ξ±} {l : list Ξ±} : nodup (a::l) ↔ a βˆ‰ l ∧ nodup l := by simp only [nodup, pairwise_cons, forall_mem_ne] protected lemma pairwise.nodup {l : list Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} [is_irrefl Ξ± r] (h : pairwise r l) : nodup l := h.imp $ Ξ» a b, ne_of_irrefl lemma rel_nodup {r : Ξ± β†’ Ξ² β†’ Prop} (hr : relator.bi_unique r) : (forallβ‚‚ r β‡’ (↔)) nodup nodup | _ _ forallβ‚‚.nil := by simp only [nodup_nil] | _ _ (forallβ‚‚.cons hab h) := by simpa only [nodup_cons] using relator.rel_and (relator.rel_not (rel_mem hr hab h)) (rel_nodup h) protected lemma nodup.cons (ha : a βˆ‰ l) (hl : nodup l) : nodup (a :: l) := nodup_cons.2 ⟨ha, hl⟩ lemma nodup_singleton (a : Ξ±) : nodup [a] := pairwise_singleton _ _ lemma nodup.of_cons (h : nodup (a :: l)) : nodup l := (nodup_cons.1 h).2 lemma nodup.not_mem (h : (a :: l).nodup) : a βˆ‰ l := (nodup_cons.1 h).1 lemma not_nodup_cons_of_mem : a ∈ l β†’ Β¬ nodup (a :: l) := imp_not_comm.1 nodup.not_mem protected lemma nodup.sublist : l₁ <+ lβ‚‚ β†’ nodup lβ‚‚ β†’ nodup l₁ := pairwise.sublist theorem not_nodup_pair (a : Ξ±) : Β¬ nodup [a, a] := not_nodup_cons_of_mem $ mem_singleton_self _ theorem nodup_iff_sublist {l : list Ξ±} : nodup l ↔ βˆ€ a, Β¬ [a, a] <+ l := ⟨λ d a h, not_nodup_pair a (d.sublist h), begin induction l with a l IH; intro h, {exact nodup_nil}, exact (IH $ Ξ» a s, h a $ sublist_cons_of_sublist _ s).cons (Ξ» al, h a $ (singleton_sublist.2 al).cons_cons _) end⟩ theorem nodup_iff_nth_le_inj {l : list Ξ±} : nodup l ↔ βˆ€ i j h₁ hβ‚‚, nth_le l i h₁ = nth_le l j hβ‚‚ β†’ i = j := pairwise_iff_nth_le.trans ⟨λ H i j h₁ hβ‚‚ h, ((lt_trichotomy _ _) .resolve_left (Ξ» h', H _ _ hβ‚‚ h' h)) .resolve_right (Ξ» h', H _ _ h₁ h' h.symm), Ξ» H i j h₁ hβ‚‚ h, ne_of_lt hβ‚‚ (H _ _ _ _ h)⟩ theorem nodup.nth_le_inj_iff {l : list Ξ±} (h : nodup l) {i j : β„•} (hi : i < l.length) (hj : j < l.length) : l.nth_le i hi = l.nth_le j hj ↔ i = j := ⟨nodup_iff_nth_le_inj.mp h _ _ _ _, by simp {contextual := tt}⟩ lemma nodup_iff_nth_ne_nth {l : list Ξ±} : l.nodup ↔ βˆ€ (i j : β„•), i < j β†’ j < l.length β†’ l.nth i β‰  l.nth j := begin rw nodup_iff_nth_le_inj, simp only [nth_le_eq_iff, some_nth_le_eq], split; rintro h i j h₁ hβ‚‚, { exact mt (h i j (h₁.trans hβ‚‚) hβ‚‚) (ne_of_lt h₁) }, { intro h₃, by_contra hβ‚„, cases lt_or_gt_of_ne hβ‚„ with hβ‚… hβ‚…, { exact h i j hβ‚… hβ‚‚ h₃ }, { exact h j i hβ‚… h₁ h₃.symm }}, end lemma nodup.ne_singleton_iff {l : list Ξ±} (h : nodup l) (x : Ξ±) : l β‰  [x] ↔ l = [] ∨ βˆƒ y ∈ l, y β‰  x := begin induction l with hd tl hl, { simp }, { specialize hl h.of_cons, by_cases hx : tl = [x], { simpa [hx, and.comm, and_or_distrib_left] using h }, { rw [←ne.def, hl] at hx, rcases hx with rfl | ⟨y, hy, hx⟩, { simp }, { have : tl β‰  [] := ne_nil_of_mem hy, suffices : βˆƒ (y : Ξ±) (H : y ∈ hd :: tl), y β‰  x, { simpa [ne_nil_of_mem hy] }, exact ⟨y, mem_cons_of_mem _ hy, hx⟩ } } } end lemma nth_le_eq_of_ne_imp_not_nodup (xs : list Ξ±) (n m : β„•) (hn : n < xs.length) (hm : m < xs.length) (h : xs.nth_le n hn = xs.nth_le m hm) (hne : n β‰  m) : Β¬ nodup xs := begin rw nodup_iff_nth_le_inj, simp only [exists_prop, exists_and_distrib_right, not_forall], exact ⟨n, m, ⟨hn, hm, h⟩, hne⟩ end @[simp] theorem nth_le_index_of [decidable_eq Ξ±] {l : list Ξ±} (H : nodup l) (n h) : index_of (nth_le l n h) l = n := nodup_iff_nth_le_inj.1 H _ _ _ h $ index_of_nth_le $ index_of_lt_length.2 $ nth_le_mem _ _ _ theorem nodup_iff_count_le_one [decidable_eq Ξ±] {l : list Ξ±} : nodup l ↔ βˆ€ a, count a l ≀ 1 := nodup_iff_sublist.trans $ forall_congr $ Ξ» a, have [a, a] <+ l ↔ 1 < count a l, from (@le_count_iff_repeat_sublist _ _ a l 2).symm, (not_congr this).trans not_lt theorem nodup_repeat (a : Ξ±) : βˆ€ {n : β„•}, nodup (repeat a n) ↔ n ≀ 1 | 0 := by simp [nat.zero_le] | 1 := by simp | (n+2) := iff_of_false (Ξ» H, nodup_iff_sublist.1 H a ((repeat_sublist_repeat _).2 (nat.le_add_left 2 n))) (not_le_of_lt $ nat.le_add_left 2 n) @[simp] theorem count_eq_one_of_mem [decidable_eq Ξ±] {a : Ξ±} {l : list Ξ±} (d : nodup l) (h : a ∈ l) : count a l = 1 := le_antisymm (nodup_iff_count_le_one.1 d a) (count_pos.2 h) lemma nodup.of_append_left : nodup (l₁ ++ lβ‚‚) β†’ nodup l₁ := nodup.sublist (sublist_append_left l₁ lβ‚‚) lemma nodup.of_append_right : nodup (l₁ ++ lβ‚‚) β†’ nodup lβ‚‚ := nodup.sublist (sublist_append_right l₁ lβ‚‚) theorem nodup_append {l₁ lβ‚‚ : list Ξ±} : nodup (l₁++lβ‚‚) ↔ nodup l₁ ∧ nodup lβ‚‚ ∧ disjoint l₁ lβ‚‚ := by simp only [nodup, pairwise_append, disjoint_iff_ne] theorem disjoint_of_nodup_append {l₁ lβ‚‚ : list Ξ±} (d : nodup (l₁++lβ‚‚)) : disjoint l₁ lβ‚‚ := (nodup_append.1 d).2.2 lemma nodup.append (d₁ : nodup l₁) (dβ‚‚ : nodup lβ‚‚) (dj : disjoint l₁ lβ‚‚) : nodup (l₁ ++ lβ‚‚) := nodup_append.2 ⟨d₁, dβ‚‚, dj⟩ theorem nodup_append_comm {l₁ lβ‚‚ : list Ξ±} : nodup (l₁++lβ‚‚) ↔ nodup (lβ‚‚++l₁) := by simp only [nodup_append, and.left_comm, disjoint_comm] theorem nodup_middle {a : Ξ±} {l₁ lβ‚‚ : list Ξ±} : nodup (l₁ ++ a::lβ‚‚) ↔ nodup (a::(l₁++lβ‚‚)) := by simp only [nodup_append, not_or_distrib, and.left_comm, and_assoc, nodup_cons, mem_append, disjoint_cons_right] lemma nodup.of_map (f : Ξ± β†’ Ξ²) {l : list Ξ±} : nodup (map f l) β†’ nodup l := pairwise.of_map f $ Ξ» a b, mt $ congr_arg f lemma nodup.map_on {f : Ξ± β†’ Ξ²} (H : βˆ€ x ∈ l, βˆ€ y ∈ l, f x = f y β†’ x = y) (d : nodup l) : (map f l).nodup := pairwise.map _ (by exact Ξ» a b ⟨ma, mb, n⟩ e, n (H a ma b mb e)) (pairwise.and_mem.1 d) theorem inj_on_of_nodup_map {f : Ξ± β†’ Ξ²} {l : list Ξ±} (d : nodup (map f l)) : βˆ€ ⦃x⦄, x ∈ l β†’ βˆ€ ⦃y⦄, y ∈ l β†’ f x = f y β†’ x = y := begin induction l with hd tl ih, { simp }, { simp only [map, nodup_cons, mem_map, not_exists, not_and, ←ne.def] at d, rintro _ (rfl | h₁) _ (rfl | hβ‚‚) h₃, { refl }, { apply (d.1 _ hβ‚‚ h₃.symm).elim }, { apply (d.1 _ h₁ h₃).elim }, { apply ih d.2 h₁ hβ‚‚ h₃ } } end theorem nodup_map_iff_inj_on {f : Ξ± β†’ Ξ²} {l : list Ξ±} (d : nodup l) : nodup (map f l) ↔ (βˆ€ (x ∈ l) (y ∈ l), f x = f y β†’ x = y) := ⟨inj_on_of_nodup_map, Ξ» h, d.map_on h⟩ protected lemma nodup.map {f : Ξ± β†’ Ξ²} (hf : injective f) : nodup l β†’ nodup (map f l) := nodup.map_on (assume x _ y _ h, hf h) theorem nodup_map_iff {f : Ξ± β†’ Ξ²} {l : list Ξ±} (hf : injective f) : nodup (map f l) ↔ nodup l := ⟨nodup.of_map _, nodup.map hf⟩ @[simp] theorem nodup_attach {l : list Ξ±} : nodup (attach l) ↔ nodup l := ⟨λ h, attach_map_val l β–Έ h.map (Ξ» a b, subtype.eq), Ξ» h, nodup.of_map subtype.val ((attach_map_val l).symm β–Έ h)⟩ alias nodup_attach ↔ nodup.of_attach nodup.attach attribute [protected] nodup.attach lemma nodup.pmap {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {l : list Ξ±} {H} (hf : βˆ€ a ha b hb, f a ha = f b hb β†’ a = b) (h : nodup l) : nodup (pmap f l H) := by rw [pmap_eq_map_attach]; exact h.attach.map (Ξ» ⟨a, ha⟩ ⟨b, hb⟩ h, by congr; exact hf a (H _ ha) b (H _ hb) h) lemma nodup.filter (p : Ξ± β†’ Prop) [decidable_pred p] {l} : nodup l β†’ nodup (filter p l) := pairwise.filter p @[simp] theorem nodup_reverse {l : list Ξ±} : nodup (reverse l) ↔ nodup l := pairwise_reverse.trans $ by simp only [nodup, ne.def, eq_comm] lemma nodup.erase_eq_filter [decidable_eq Ξ±] {l} (d : nodup l) (a : Ξ±) : l.erase a = filter (β‰  a) l := begin induction d with b l m d IH, {refl}, by_cases b = a, { subst h, rw [erase_cons_head, filter_cons_of_neg], symmetry, rw filter_eq_self, simpa only [ne.def, eq_comm] using m, exact not_not_intro rfl }, { rw [erase_cons_tail _ h, filter_cons_of_pos, IH], exact h } end lemma nodup.erase [decidable_eq Ξ±] (a : Ξ±) : nodup l β†’ nodup (l.erase a) := nodup.sublist $ erase_sublist _ _ lemma nodup.diff [decidable_eq Ξ±] : l₁.nodup β†’ (l₁.diff lβ‚‚).nodup := nodup.sublist $ diff_sublist _ _ lemma nodup.mem_erase_iff [decidable_eq Ξ±] (d : nodup l) : a ∈ l.erase b ↔ a β‰  b ∧ a ∈ l := by rw [d.erase_eq_filter, mem_filter, and_comm] lemma nodup.not_mem_erase [decidable_eq Ξ±] (h : nodup l) : a βˆ‰ l.erase a := Ξ» H, (h.mem_erase_iff.1 H).1 rfl theorem nodup_join {L : list (list Ξ±)} : nodup (join L) ↔ (βˆ€ l ∈ L, nodup l) ∧ pairwise disjoint L := by simp only [nodup, pairwise_join, disjoint_left.symm, forall_mem_ne] theorem nodup_bind {l₁ : list Ξ±} {f : Ξ± β†’ list Ξ²} : nodup (l₁.bind f) ↔ (βˆ€ x ∈ l₁, nodup (f x)) ∧ pairwise (Ξ» (a b : Ξ±), disjoint (f a) (f b)) l₁ := by simp only [list.bind, nodup_join, pairwise_map, and_comm, and.left_comm, mem_map, exists_imp_distrib, and_imp]; rw [show (βˆ€ (l : list Ξ²) (x : Ξ±), f x = l β†’ x ∈ l₁ β†’ nodup l) ↔ (βˆ€ (x : Ξ±), x ∈ l₁ β†’ nodup (f x)), from forall_swap.trans $ forall_congr $ Ξ»_, forall_eq'] protected lemma nodup.product {lβ‚‚ : list Ξ²} (d₁ : l₁.nodup) (dβ‚‚ : lβ‚‚.nodup) : (l₁.product lβ‚‚).nodup := nodup_bind.2 ⟨λ a ma, dβ‚‚.map $ left_inverse.injective $ Ξ» b, (rfl : (a,b).2 = b), d₁.imp $ Ξ» a₁ aβ‚‚ n x h₁ hβ‚‚, begin rcases mem_map.1 h₁ with ⟨b₁, mb₁, rfl⟩, rcases mem_map.1 hβ‚‚ with ⟨bβ‚‚, mbβ‚‚, ⟨⟩⟩, exact n rfl end⟩ lemma nodup.sigma {Οƒ : Ξ± β†’ Type*} {lβ‚‚ : Ξ  a, list (Οƒ a)} (d₁ : nodup l₁) (dβ‚‚ : βˆ€ a, nodup (lβ‚‚ a)) : (l₁.sigma lβ‚‚).nodup := nodup_bind.2 ⟨λ a ma, (dβ‚‚ a).map (Ξ» b b' h, by injection h with _ h; exact eq_of_heq h), d₁.imp $ Ξ» a₁ aβ‚‚ n x h₁ hβ‚‚, begin rcases mem_map.1 h₁ with ⟨b₁, mb₁, rfl⟩, rcases mem_map.1 hβ‚‚ with ⟨bβ‚‚, mbβ‚‚, ⟨⟩⟩, exact n rfl end⟩ protected lemma nodup.filter_map {f : Ξ± β†’ option Ξ²} (h : βˆ€ a a' b, b ∈ f a β†’ b ∈ f a' β†’ a = a') : nodup l β†’ nodup (filter_map f l) := pairwise.filter_map f $ Ξ» a a' n b bm b' bm' e, n $ h a a' b' (e β–Έ bm) bm' protected lemma nodup.concat (h : a βˆ‰ l) (h' : l.nodup) : (l.concat a).nodup := by rw concat_eq_append; exact h'.append (nodup_singleton _) (disjoint_singleton.2 h) lemma nodup.insert [decidable_eq Ξ±] (h : l.nodup) : (insert a l).nodup := if h' : a ∈ l then by rw [insert_of_mem h']; exact h else by rw [insert_of_not_mem h', nodup_cons]; split; assumption lemma nodup.union [decidable_eq Ξ±] (l₁ : list Ξ±) (h : nodup lβ‚‚) : (l₁ βˆͺ lβ‚‚).nodup := begin induction l₁ with a l₁ ih generalizing lβ‚‚, { exact h }, { exact (ih h).insert } end lemma nodup.inter [decidable_eq Ξ±] (lβ‚‚ : list Ξ±) : nodup l₁ β†’ nodup (l₁ ∩ lβ‚‚) := nodup.filter _ @[simp] theorem nodup_sublists {l : list Ξ±} : nodup (sublists l) ↔ nodup l := ⟨λ h, (h.sublist (map_ret_sublist_sublists _)).of_map _, Ξ» h, (pairwise_sublists h).imp (Ξ» _ _ h, mt reverse_inj.2 h.to_ne)⟩ @[simp] theorem nodup_sublists' {l : list Ξ±} : nodup (sublists' l) ↔ nodup l := by rw [sublists'_eq_sublists, nodup_map_iff reverse_injective, nodup_sublists, nodup_reverse] alias nodup_sublists ↔ nodup.of_sublists nodup.sublists alias nodup_sublists' ↔ nodup.of_sublists' nodup.sublists' attribute [protected] nodup.sublists nodup.sublists' lemma nodup_sublists_len (n : β„•) (h : nodup l) : (sublists_len n l).nodup := h.sublists'.sublist $ sublists_len_sublist_sublists' _ _ lemma nodup.diff_eq_filter [decidable_eq Ξ±] : βˆ€ {l₁ lβ‚‚ : list Ξ±} (hl₁ : l₁.nodup), l₁.diff lβ‚‚ = l₁.filter (βˆ‰ lβ‚‚) | l₁ [] hl₁ := by simp | l₁ (a::lβ‚‚) hl₁ := begin rw [diff_cons, (hl₁.erase _).diff_eq_filter, hl₁.erase_eq_filter, filter_filter], simp only [mem_cons_iff, not_or_distrib, and.comm] end lemma nodup.mem_diff_iff [decidable_eq Ξ±] (hl₁ : l₁.nodup) : a ∈ l₁.diff lβ‚‚ ↔ a ∈ l₁ ∧ a βˆ‰ lβ‚‚ := by rw [hl₁.diff_eq_filter, mem_filter] protected lemma nodup.update_nth : βˆ€ {l : list Ξ±} {n : β„•} {a : Ξ±} (hl : l.nodup) (ha : a βˆ‰ l), (l.update_nth n a).nodup | [] n a hl ha := nodup_nil | (b::l) 0 a hl ha := nodup_cons.2 ⟨mt (mem_cons_of_mem _) ha, (nodup_cons.1 hl).2⟩ | (b::l) (n+1) a hl ha := nodup_cons.2 ⟨λ h, (mem_or_eq_of_mem_update_nth h).elim (nodup_cons.1 hl).1 (Ξ» hba, ha (hba β–Έ mem_cons_self _ _)), hl.of_cons.update_nth (mt (mem_cons_of_mem _) ha)⟩ lemma nodup.map_update [decidable_eq Ξ±] {l : list Ξ±} (hl : l.nodup) (f : Ξ± β†’ Ξ²) (x : Ξ±) (y : Ξ²) : l.map (function.update f x y) = if x ∈ l then (l.map f).update_nth (l.index_of x) y else l.map f := begin induction l with hd tl ihl, { simp }, rw [nodup_cons] at hl, simp only [mem_cons_iff, map, ihl hl.2], by_cases H : hd = x, { subst hd, simp [update_nth, hl.1] }, { simp [ne.symm H, H, update_nth, ← apply_ite (cons (f hd))] } end lemma nodup.pairwise_of_forall_ne {l : list Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (hl : l.nodup) (h : βˆ€ (a ∈ l) (b ∈ l), a β‰  b β†’ r a b) : l.pairwise r := begin classical, refine pairwise_of_reflexive_on_dupl_of_forall_ne _ h, intros x hx, rw nodup_iff_count_le_one at hl, exact absurd (hl x) hx.not_le end lemma nodup.pairwise_of_set_pairwise {l : list Ξ±} {r : Ξ± β†’ Ξ± β†’ Prop} (hl : l.nodup) (h : {x | x ∈ l}.pairwise r) : l.pairwise r := hl.pairwise_of_forall_ne h end list theorem option.to_list_nodup {Ξ±} : βˆ€ o : option Ξ±, o.to_list.nodup | none := list.nodup_nil | (some x) := list.nodup_singleton x
f06e8f1b4f7bd08466b207f46de2613b28c6b9b0
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/linear_algebra/cross_product.lean
91eac76bb2ce359fc85ab1829eca9e782230231f
[ "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,325
lean
/- Copyright (c) 2021 Martin Dvorak. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Martin Dvorak, Kyle Miller, Eric Wieser -/ import data.matrix.notation import linear_algebra.bilinear_map import linear_algebra.matrix.determinant import algebra.lie.basic /-! # Cross products This module defines the cross product of vectors in $R^3$ for $R$ a commutative ring, as a bilinear map. ## Main definitions * `cross_product` is the cross product of pairs of vectors in $R^3$. ## Main results * `triple_product_eq_det` * `cross_dot_cross` * `jacobi_cross` ## Notation The locale `matrix` gives the following notation: * `×₃` for the cross product ## Tags crossproduct -/ open matrix open_locale matrix variables {R : Type*} [comm_ring R] /-- The cross product of two vectors in $R^3$ for $R$ a commutative ring. -/ def cross_product : (fin 3 β†’ R) β†’β‚—[R] (fin 3 β†’ R) β†’β‚—[R] (fin 3 β†’ R) := begin apply linear_map.mkβ‚‚ R (Ξ» (a b : fin 3 β†’ R), ![a 1 * b 2 - a 2 * b 1, a 2 * b 0 - a 0 * b 2, a 0 * b 1 - a 1 * b 0]), { intros, simp [vec3_add (_ : R), add_comm, add_assoc, add_left_comm, add_mul, sub_eq_add_neg] }, { intros, simp [smul_vec3 (_ : R) (_ : R), mul_comm, mul_assoc, mul_left_comm, mul_add, sub_eq_add_neg] }, { intros, simp [vec3_add (_ : R), add_comm, add_assoc, add_left_comm, mul_add, sub_eq_add_neg] }, { intros, simp [smul_vec3 (_ : R) (_ : R), mul_comm, mul_assoc, mul_left_comm, mul_add, sub_eq_add_neg] }, end localized "infixl (name := cross_product) ` ×₃ `: 74 := cross_product" in matrix lemma cross_apply (a b : fin 3 β†’ R) : a ×₃ b = ![a 1 * b 2 - a 2 * b 1, a 2 * b 0 - a 0 * b 2, a 0 * b 1 - a 1 * b 0] := rfl section products_properties @[simp] lemma cross_anticomm (v w : fin 3 β†’ R) : - (v ×₃ w) = w ×₃ v := by simp [cross_apply, mul_comm] alias cross_anticomm ← neg_cross @[simp] lemma cross_anticomm' (v w : fin 3 β†’ R) : v ×₃ w + w ×₃ v = 0 := by rw [add_eq_zero_iff_eq_neg, cross_anticomm] @[simp] lemma cross_self (v : fin 3 β†’ R) : v ×₃ v = 0 := by simp [cross_apply, mul_comm] /-- The cross product of two vectors is perpendicular to the first vector. -/ @[simp] lemma dot_self_cross (v w : fin 3 β†’ R) : v ⬝α΅₯ (v ×₃ w) = 0 := by simp [cross_apply, vec3_dot_product, mul_sub, mul_assoc, mul_left_comm] /-- The cross product of two vectors is perpendicular to the second vector. -/ @[simp] lemma dot_cross_self (v w : fin 3 β†’ R) : w ⬝α΅₯ (v ×₃ w) = 0 := by rw [← cross_anticomm, matrix.dot_product_neg, dot_self_cross, neg_zero] /-- Cyclic permutations preserve the triple product. See also `triple_product_eq_det`. -/ lemma triple_product_permutation (u v w : fin 3 β†’ R) : u ⬝α΅₯ (v ×₃ w) = v ⬝α΅₯ (w ×₃ u) := begin simp only [cross_apply, vec3_dot_product, matrix.head_cons, matrix.cons_vec_bit0_eq_alt0, matrix.empty_append, matrix.cons_val_one, matrix.cons_vec_alt0, matrix.cons_append, matrix.cons_val_zero], ring, end /-- The triple product of `u`, `v`, and `w` is equal to the determinant of the matrix with those vectors as its rows. -/ theorem triple_product_eq_det (u v w : fin 3 β†’ R) : u ⬝α΅₯ (v ×₃ w) = matrix.det ![u, v, w] := begin simp only [vec3_dot_product, cross_apply, matrix.det_fin_three, matrix.head_cons, matrix.cons_vec_bit0_eq_alt0, matrix.empty_vec_alt0, matrix.cons_vec_alt0, matrix.vec_head_vec_alt0, fin.fin_append_apply_zero, matrix.empty_append, matrix.cons_append, matrix.cons_val', matrix.cons_val_one, matrix.cons_val_zero], ring, end /-- The scalar quadruple product identity, related to the Binet-Cauchy identity. -/ theorem cross_dot_cross (u v w x : fin 3 β†’ R) : (u ×₃ v) ⬝α΅₯ (w ×₃ x) = (u ⬝α΅₯ w) * (v ⬝α΅₯ x) - (u ⬝α΅₯ x) * (v ⬝α΅₯ w) := begin simp only [vec3_dot_product, cross_apply, cons_append, cons_vec_bit0_eq_alt0, cons_val_one, cons_vec_alt0, linear_map.mkβ‚‚_apply, cons_val_zero, head_cons, empty_append], ring_nf, end end products_properties section leibniz_properties /-- The cross product satisfies the Leibniz lie property. -/ lemma leibniz_cross (u v w : fin 3 β†’ R) : u ×₃ (v ×₃ w) = (u ×₃ v) ×₃ w + v ×₃ (u ×₃ w) := begin dsimp only [cross_apply], ext i, fin_cases i; norm_num; ring, end /-- The three-dimensional vectors together with the operations + and ×₃ form a Lie ring. Note we do not make this an instance as a conflicting one already exists via `lie_ring.of_associative_ring`. -/ def cross.lie_ring : lie_ring (fin 3 β†’ R) := { bracket := Ξ» u v, u ×₃ v, add_lie := linear_map.map_addβ‚‚ _, lie_add := Ξ» u, linear_map.map_add _, lie_self := cross_self, leibniz_lie := leibniz_cross, ..pi.add_comm_group } local attribute [instance] cross.lie_ring lemma cross_cross (u v w : fin 3 β†’ R) : (u ×₃ v) ×₃ w = u ×₃ (v ×₃ w) - v ×₃ (u ×₃ w) := lie_lie u v w /-- Jacobi identity: For a cross product of three vectors, their sum over the three even permutations is equal to the zero vector. -/ theorem jacobi_cross (u v w : fin 3 β†’ R) : u ×₃ (v ×₃ w) + v ×₃ (w ×₃ u) + w ×₃ (u ×₃ v) = 0 := lie_jacobi u v w end leibniz_properties
f36b3e6a30ee3b0ae063bc63fef13fdc71dbe593
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/push_neg_auto.lean
1a59f860991dd1aac4e14a566b0a84dcb5585ebf
[]
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
3,191
lean
/- Copyright (c) 2019 Patrick Massot All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Patrick Massot, Simon Hudon A tactic pushing negations into an expression -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.logic.basic import Mathlib.algebra.order import Mathlib.PostPort universes u namespace Mathlib namespace push_neg theorem not_not_eq (p : Prop) : (¬¬p) = p := propext not_not theorem not_and_eq (p : Prop) (q : Prop) : (Β¬(p ∧ q)) = (p β†’ Β¬q) := propext not_and theorem not_or_eq (p : Prop) (q : Prop) : (Β¬(p ∨ q)) = (Β¬p ∧ Β¬q) := propext not_or_distrib theorem not_forall_eq {Ξ± : Sort u} (s : Ξ± β†’ Prop) : (Β¬βˆ€ (x : Ξ±), s x) = βˆƒ (x : Ξ±), Β¬s x := propext not_forall theorem not_exists_eq {Ξ± : Sort u} (s : Ξ± β†’ Prop) : (Β¬βˆƒ (x : Ξ±), s x) = βˆ€ (x : Ξ±), Β¬s x := propext not_exists theorem not_implies_eq (p : Prop) (q : Prop) : (Β¬(p β†’ q)) = (p ∧ Β¬q) := propext not_imp theorem classical.implies_iff_not_or (p : Prop) (q : Prop) : p β†’ q ↔ Β¬p ∨ q := imp_iff_not_or theorem not_eq {Ξ± : Sort u} (a : Ξ±) (b : Ξ±) : Β¬a = b ↔ a β‰  b := iff.rfl theorem not_le_eq {Ξ² : Type u} [linear_order Ξ²] (a : Ξ²) (b : Ξ²) : (Β¬a ≀ b) = (b < a) := propext not_le theorem not_lt_eq {Ξ² : Type u} [linear_order Ξ²] (a : Ξ²) (b : Ξ²) : (Β¬a < b) = (b ≀ a) := propext not_lt end push_neg /-- Push negations in the goal of some assumption. For instance, a hypothesis `h : Β¬ βˆ€ x, βˆƒ y, x ≀ y` will be transformed by `push_neg at h` into `h : βˆƒ x, βˆ€ y, y < x`. Variables names are conserved. This tactic pushes negations inside expressions. For instance, given an assumption ```lean h : Β¬ βˆ€ Ξ΅ > 0, βˆƒ Ξ΄ > 0, βˆ€ x, |x - xβ‚€| ≀ Ξ΄ β†’ |f x - yβ‚€| ≀ Ξ΅) ``` writing `push_neg at h` will turn `h` into ```lean h : βˆƒ Ξ΅, Ξ΅ > 0 ∧ βˆ€ Ξ΄, Ξ΄ > 0 β†’ (βˆƒ x, |x - xβ‚€| ≀ Ξ΄ ∧ Ξ΅ < |f x - yβ‚€|), ``` (the pretty printer does *not* use the abreviations `βˆ€ Ξ΄ > 0` and `βˆƒ Ξ΅ > 0` but this issue has nothing to do with `push_neg`). Note that names are conserved by this tactic, contrary to what would happen with `simp` using the relevant lemmas. One can also use this tactic at the goal using `push_neg`, at every assumption and the goal using `push_neg at *` or at selected assumptions and the goal using say `push_neg at h h' ⊒` as usual. -/ theorem imp_of_not_imp_not (P : Prop) (Q : Prop) : (Β¬Q β†’ Β¬P) β†’ P β†’ Q := fun (h : Β¬Q β†’ Β¬P) (hP : P) => classical.by_contradiction fun (h' : Β¬Q) => h h' hP /-- Matches either an identifier "h" or a pair of identifiers "h with k" -/ /-- Transforms the goal into its contrapositive. * `contrapose` turns a goal `P β†’ Q` into `Β¬ Q β†’ Β¬ P` * `contrapose!` turns a goal `P β†’ Q` into `Β¬ Q β†’ Β¬ P` and pushes negations inside `P` and `Q` using `push_neg` * `contrapose h` first reverts the local assumption `h`, and then uses `contrapose` and `intro h` * `contrapose! h` first reverts the local assumption `h`, and then uses `contrapose!` and `intro h` * `contrapose h with new_h` uses the name `new_h` for the introduced hypothesis -/ end Mathlib
bbd46047f74754a07c476350bbb68044d3157682
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/topology/algebra/open_subgroup.lean
ce946e9a952f3fd2da543c477b59dfb71ab9db0a
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
7,376
lean
/- Copyright (c) 2019 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import order.filter.lift import topology.opens import topology.algebra.ring open topological_space open_locale topological_space set_option old_structure_cmd true /-- The type of open subgroups of a topological additive group. -/ @[ancestor add_subgroup] structure open_add_subgroup (G : Type*) [add_group G] [topological_space G] extends add_subgroup G := (is_open' : is_open carrier) /-- The type of open subgroups of a topological group. -/ @[ancestor subgroup, to_additive] structure open_subgroup (G : Type*) [group G] [topological_space G] extends subgroup G := (is_open' : is_open carrier) /-- Reinterpret an `open_subgroup` as a `subgroup`. -/ add_decl_doc open_subgroup.to_subgroup /-- Reinterpret an `open_add_subgroup` as an `add_subgroup`. -/ add_decl_doc open_add_subgroup.to_add_subgroup -- Tell Lean that `open_add_subgroup` is a namespace namespace open_add_subgroup end open_add_subgroup namespace open_subgroup open function topological_space variables {G : Type*} [group G] [topological_space G] variables {U V : open_subgroup G} {g : G} @[to_additive] instance has_coe_set : has_coe_t (open_subgroup G) (set G) := ⟨λ U, U.1⟩ @[to_additive] instance : has_mem G (open_subgroup G) := ⟨λ g U, g ∈ (U : set G)⟩ @[to_additive] instance has_coe_subgroup : has_coe_t (open_subgroup G) (subgroup G) := ⟨to_subgroup⟩ @[to_additive] instance has_coe_opens : has_coe_t (open_subgroup G) (opens G) := ⟨λ U, ⟨U, U.is_open'⟩⟩ @[simp, to_additive] lemma mem_coe : g ∈ (U : set G) ↔ g ∈ U := iff.rfl @[simp, to_additive] lemma mem_coe_opens : g ∈ (U : opens G) ↔ g ∈ U := iff.rfl @[simp, to_additive] lemma mem_coe_subgroup : g ∈ (U : subgroup G) ↔ g ∈ U := iff.rfl attribute [norm_cast] mem_coe mem_coe_opens mem_coe_subgroup open_add_subgroup.mem_coe open_add_subgroup.mem_coe_opens open_add_subgroup.mem_coe_add_subgroup @[to_additive] lemma coe_injective : injective (coe : open_subgroup G β†’ set G) := Ξ» U V h, by cases U; cases V; congr; assumption @[ext, to_additive] lemma ext (h : βˆ€ x, x ∈ U ↔ x ∈ V) : (U = V) := coe_injective $ set.ext h @[to_additive] lemma ext_iff : (U = V) ↔ (βˆ€ x, x ∈ U ↔ x ∈ V) := ⟨λ h x, h β–Έ iff.rfl, ext⟩ variable (U) @[to_additive] protected lemma is_open : is_open (U : set G) := U.is_open' @[to_additive] protected lemma one_mem : (1 : G) ∈ U := U.one_mem' @[to_additive] protected lemma inv_mem {g : G} (h : g ∈ U) : g⁻¹ ∈ U := U.inv_mem' h @[to_additive] protected lemma mul_mem {g₁ gβ‚‚ : G} (h₁ : g₁ ∈ U) (hβ‚‚ : gβ‚‚ ∈ U) : g₁ * gβ‚‚ ∈ U := U.mul_mem' h₁ hβ‚‚ @[to_additive] lemma mem_nhds_one : (U : set G) ∈ 𝓝 (1 : G) := mem_nhds_sets U.is_open U.one_mem variable {U} @[to_additive] instance : has_top (open_subgroup G) := ⟨{ is_open' := is_open_univ, .. (⊀ : subgroup G) }⟩ @[to_additive] instance : inhabited (open_subgroup G) := ⟨⊀⟩ @[to_additive] lemma is_closed [has_continuous_mul G] (U : open_subgroup G) : is_closed (U : set G) := begin refine is_open_iff_forall_mem_open.2 (Ξ» x hx, ⟨(Ξ» y, y * x⁻¹) ⁻¹' U, _, _, _⟩), { intros u hux, simp only [set.mem_preimage, set.mem_compl_iff, mem_coe] at hux hx ⊒, refine mt (Ξ» hu, _) hx, convert U.mul_mem (U.inv_mem hux) hu, simp }, { exact U.is_open.preimage (continuous_mul_right _) }, { simp [U.one_mem] } end section variables {H : Type*} [group H] [topological_space H] /-- The product of two open subgroups as an open subgroup of the product group. -/ @[to_additive "The product of two open subgroups as an open subgroup of the product group."] def prod (U : open_subgroup G) (V : open_subgroup H) : open_subgroup (G Γ— H) := { carrier := (U : set G).prod (V : set H), is_open' := U.is_open.prod V.is_open, .. (U : subgroup G).prod (V : subgroup H) } end @[to_additive] instance : partial_order (open_subgroup G) := { le := Ξ» U V, βˆ€ ⦃x⦄, x ∈ U β†’ x ∈ V, .. partial_order.lift (coe : open_subgroup G β†’ set G) coe_injective } @[to_additive] instance : semilattice_inf_top (open_subgroup G) := { inf := Ξ» U V, { is_open' := is_open_inter U.is_open V.is_open, .. (U : subgroup G) βŠ“ V }, inf_le_left := Ξ» U V, set.inter_subset_left _ _, inf_le_right := Ξ» U V, set.inter_subset_right _ _, le_inf := Ξ» U V W hV hW, set.subset_inter hV hW, top := ⊀, le_top := Ξ» U, set.subset_univ _, ..open_subgroup.partial_order } @[simp, to_additive] lemma coe_inf : (↑(U βŠ“ V) : set G) = (U : set G) ∩ V := rfl @[simp, to_additive] lemma coe_subset : (U : set G) βŠ† V ↔ U ≀ V := iff.rfl @[simp, to_additive] lemma coe_subgroup_le : (U : subgroup G) ≀ (V : subgroup G) ↔ U ≀ V := iff.rfl attribute [norm_cast] coe_inf coe_subset coe_subgroup_le open_add_subgroup.coe_inf open_add_subgroup.coe_subset open_add_subgroup.coe_add_subgroup_le end open_subgroup namespace subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] (H : subgroup G) @[to_additive] lemma is_open_of_mem_nhds {g : G} (hg : (H : set G) ∈ 𝓝 g) : is_open (H : set G) := begin simp only [is_open_iff_mem_nhds, subgroup.mem_coe] at hg ⊒, intros x hx, have : filter.tendsto (Ξ» y, y * (x⁻¹ * g)) (𝓝 x) (𝓝 $ x * (x⁻¹ * g)) := (continuous_id.mul continuous_const).tendsto _, rw [mul_inv_cancel_left] at this, have := filter.mem_map.1 (this hg), replace hg : g ∈ H := subgroup.mem_coe.1 (mem_of_nhds hg), simp only [subgroup.mem_coe, H.mul_mem_cancel_right (H.mul_mem (H.inv_mem hx) hg)] at this, exact this end @[to_additive] lemma is_open_of_open_subgroup {U : open_subgroup G} (h : U.1 ≀ H) : is_open (H : set G) := H.is_open_of_mem_nhds (filter.mem_sets_of_superset U.mem_nhds_one h) @[to_additive] lemma is_open_mono {H₁ Hβ‚‚ : subgroup G} (h : H₁ ≀ Hβ‚‚) (h₁ : is_open (H₁ :set G)) : is_open (Hβ‚‚ : set G) := @is_open_of_open_subgroup _ _ _ _ Hβ‚‚ { is_open' := h₁, .. H₁ } h end subgroup namespace open_subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] @[to_additive] instance : semilattice_sup_top (open_subgroup G) := { sup := Ξ» U V, { is_open' := show is_open (((U : subgroup G) βŠ” V : subgroup G) : set G), from subgroup.is_open_mono le_sup_left U.is_open, .. ((U : subgroup G) βŠ” V) }, le_sup_left := Ξ» U V, coe_subgroup_le.1 le_sup_left, le_sup_right := Ξ» U V, coe_subgroup_le.1 le_sup_right, sup_le := Ξ» U V W hU hV, coe_subgroup_le.1 (sup_le hU hV), ..open_subgroup.semilattice_inf_top } end open_subgroup namespace submodule open open_add_subgroup variables {R : Type*} {M : Type*} [comm_ring R] variables [add_comm_group M] [topological_space M] [topological_add_group M] [module R M] lemma is_open_mono {U P : submodule R M} (h : U ≀ P) (hU : is_open (U : set M)) : is_open (P : set M) := @add_subgroup.is_open_mono M _ _ _ U.to_add_subgroup P.to_add_subgroup h hU end submodule namespace ideal variables {R : Type*} [comm_ring R] variables [topological_space R] [topological_ring R] lemma is_open_of_open_subideal {U I : ideal R} (h : U ≀ I) (hU : is_open (U : set R)) : is_open (I : set R) := submodule.is_open_mono h hU end ideal
921119c9a152ed173b2b4969bd32404814370240
75bd9c50a345718d735a7533c007cf45f9da9a83
/src/data/multiset/basic.lean
b7015955dc884d4207b6b0005ed8c60c81feca28
[ "Apache-2.0" ]
permissive
jtbarker/mathlib
a1a3b1ddc16179826260578410746756ef18032c
392d3e376b44265ef2dedbd92231d3177acc1fd0
refs/heads/master
1,671,246,411,096
1,600,801,712,000
1,600,801,712,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
84,791
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import data.list.perm import algebra.group_power /-! # Multisets These are implemented as the quotient of a list by permutations. -/ open list subtype nat variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*} /-- `multiset Ξ±` is the quotient of `list Ξ±` by list permutation. The result is a type of finite sets with duplicates allowed. -/ def {u} multiset (Ξ± : Type u) : Type u := quotient (list.is_setoid Ξ±) namespace multiset instance : has_coe (list Ξ±) (multiset Ξ±) := ⟨quot.mk _⟩ @[simp] theorem quot_mk_to_coe (l : list Ξ±) : @eq (multiset Ξ±) ⟦l⟧ l := rfl @[simp] theorem quot_mk_to_coe' (l : list Ξ±) : @eq (multiset Ξ±) (quot.mk (β‰ˆ) l) l := rfl @[simp] theorem quot_mk_to_coe'' (l : list Ξ±) : @eq (multiset Ξ±) (quot.mk setoid.r l) l := rfl @[simp] theorem coe_eq_coe {l₁ lβ‚‚ : list Ξ±} : (l₁ : multiset Ξ±) = lβ‚‚ ↔ l₁ ~ lβ‚‚ := quotient.eq instance has_decidable_eq [decidable_eq Ξ±] : decidable_eq (multiset Ξ±) | s₁ sβ‚‚ := quotient.rec_on_subsingletonβ‚‚ s₁ sβ‚‚ $ Ξ» l₁ lβ‚‚, decidable_of_iff' _ quotient.eq /-- defines a size for a multiset by referring to the size of the underlying list -/ protected def sizeof [has_sizeof Ξ±] (s : multiset Ξ±) : β„• := quot.lift_on s sizeof $ Ξ» l₁ lβ‚‚, perm.sizeof_eq_sizeof instance has_sizeof [has_sizeof Ξ±] : has_sizeof (multiset Ξ±) := ⟨multiset.sizeof⟩ /- empty multiset -/ /-- `0 : multiset Ξ±` is the empty set -/ protected def zero : multiset Ξ± := @nil Ξ± instance : has_zero (multiset Ξ±) := ⟨multiset.zero⟩ instance : has_emptyc (multiset Ξ±) := ⟨0⟩ instance : inhabited (multiset Ξ±) := ⟨0⟩ @[simp] theorem coe_nil_eq_zero : (@nil Ξ± : multiset Ξ±) = 0 := rfl @[simp] theorem empty_eq_zero : (βˆ… : multiset Ξ±) = 0 := rfl theorem coe_eq_zero (l : list Ξ±) : (l : multiset Ξ±) = 0 ↔ l = [] := iff.trans coe_eq_coe perm_nil /- cons -/ /-- `cons a s` is the multiset which contains `s` plus one more instance of `a`. -/ def cons (a : Ξ±) (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (a :: l : multiset Ξ±)) (Ξ» l₁ lβ‚‚ p, quot.sound (p.cons a)) notation a :: b := cons a b instance : has_insert Ξ± (multiset Ξ±) := ⟨cons⟩ @[simp] theorem insert_eq_cons (a : Ξ±) (s : multiset Ξ±) : insert a s = a::s := rfl @[simp] theorem cons_coe (a : Ξ±) (l : list Ξ±) : (a::l : multiset Ξ±) = (a::l : list Ξ±) := rfl theorem singleton_coe (a : Ξ±) : (a::0 : multiset Ξ±) = ([a] : list Ξ±) := rfl @[simp] theorem cons_inj_left {a b : Ξ±} (s : multiset Ξ±) : a::s = b::s ↔ a = b := ⟨quot.induction_on s $ Ξ» l e, have [a] ++ l ~ [b] ++ l, from quotient.exact e, singleton_perm_singleton.1 $ (perm_append_right_iff _).1 this, congr_arg _⟩ @[simp] theorem cons_inj_right (a : Ξ±) : βˆ€{s t : multiset Ξ±}, a::s = a::t ↔ s = t := by rintros ⟨lβ‚βŸ© ⟨lβ‚‚βŸ©; simp @[recursor 5] protected theorem induction {p : multiset Ξ± β†’ Prop} (h₁ : p 0) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : multiset Ξ±}, p s β†’ p (a :: s)) : βˆ€s, p s := by rintros ⟨l⟩; induction l with _ _ ih; [exact h₁, exact hβ‚‚ ih] @[elab_as_eliminator] protected theorem induction_on {p : multiset Ξ± β†’ Prop} (s : multiset Ξ±) (h₁ : p 0) (hβ‚‚ : βˆ€ ⦃a : α⦄ {s : multiset Ξ±}, p s β†’ p (a :: s)) : p s := multiset.induction h₁ hβ‚‚ s theorem cons_swap (a b : Ξ±) (s : multiset Ξ±) : a :: b :: s = b :: a :: s := quot.induction_on s $ Ξ» l, quotient.sound $ perm.swap _ _ _ section rec variables {C : multiset Ξ± β†’ Sort*} /-- Dependent recursor on multisets. TODO: should be @[recursor 6], but then the definition of `multiset.pi` fails with a stack overflow in `whnf`. -/ protected def rec (C_0 : C 0) (C_cons : Ξ a m, C m β†’ C (a::m)) (C_cons_heq : βˆ€a a' m b, C_cons a (a'::m) (C_cons a' m b) == C_cons a' (a::m) (C_cons a m b)) (m : multiset Ξ±) : C m := quotient.hrec_on m (@list.rec Ξ± (Ξ»l, C ⟦l⟧) C_0 (Ξ»a l b, C_cons a ⟦l⟧ b)) $ assume l l' h, h.rec_heq (assume a l l' b b' hl, have ⟦l⟧ = ⟦l'⟧, from quot.sound hl, by cc) (assume a a' l, C_cons_heq a a' ⟦l⟧) @[elab_as_eliminator] protected def rec_on (m : multiset Ξ±) (C_0 : C 0) (C_cons : Ξ a m, C m β†’ C (a::m)) (C_cons_heq : βˆ€a a' m b, C_cons a (a'::m) (C_cons a' m b) == C_cons a' (a::m) (C_cons a m b)) : C m := multiset.rec C_0 C_cons C_cons_heq m variables {C_0 : C 0} {C_cons : Ξ a m, C m β†’ C (a::m)} {C_cons_heq : βˆ€a a' m b, C_cons a (a'::m) (C_cons a' m b) == C_cons a' (a::m) (C_cons a m b)} @[simp] lemma rec_on_0 : @multiset.rec_on Ξ± C (0:multiset Ξ±) C_0 C_cons C_cons_heq = C_0 := rfl @[simp] lemma rec_on_cons (a : Ξ±) (m : multiset Ξ±) : (a :: m).rec_on C_0 C_cons C_cons_heq = C_cons a m (m.rec_on C_0 C_cons C_cons_heq) := quotient.induction_on m $ assume l, rfl end rec section mem /-- `a ∈ s` means that `a` has nonzero multiplicity in `s`. -/ def mem (a : Ξ±) (s : multiset Ξ±) : Prop := quot.lift_on s (Ξ» l, a ∈ l) (Ξ» l₁ lβ‚‚ (e : l₁ ~ lβ‚‚), propext $ e.mem_iff) instance : has_mem Ξ± (multiset Ξ±) := ⟨mem⟩ @[simp] lemma mem_coe {a : Ξ±} {l : list Ξ±} : a ∈ (l : multiset Ξ±) ↔ a ∈ l := iff.rfl instance decidable_mem [decidable_eq Ξ±] (a : Ξ±) (s : multiset Ξ±) : decidable (a ∈ s) := quot.rec_on_subsingleton s $ list.decidable_mem a @[simp] theorem mem_cons {a b : Ξ±} {s : multiset Ξ±} : a ∈ b :: s ↔ a = b ∨ a ∈ s := quot.induction_on s $ Ξ» l, iff.rfl lemma mem_cons_of_mem {a b : Ξ±} {s : multiset Ξ±} (h : a ∈ s) : a ∈ b :: s := mem_cons.2 $ or.inr h @[simp] theorem mem_cons_self (a : Ξ±) (s : multiset Ξ±) : a ∈ a :: s := mem_cons.2 (or.inl rfl) theorem forall_mem_cons {p : Ξ± β†’ Prop} {a : Ξ±} {s : multiset Ξ±} : (βˆ€ x ∈ (a :: s), p x) ↔ p a ∧ βˆ€ x ∈ s, p x := quotient.induction_on' s $ Ξ» L, list.forall_mem_cons theorem exists_cons_of_mem {s : multiset Ξ±} {a : Ξ±} : a ∈ s β†’ βˆƒ t, s = a :: t := quot.induction_on s $ Ξ» l (h : a ∈ l), let ⟨l₁, lβ‚‚, e⟩ := mem_split h in e.symm β–Έ ⟨(l₁++lβ‚‚ : list Ξ±), quot.sound perm_middle⟩ @[simp] theorem not_mem_zero (a : Ξ±) : a βˆ‰ (0 : multiset Ξ±) := id theorem eq_zero_of_forall_not_mem {s : multiset Ξ±} : (βˆ€x, x βˆ‰ s) β†’ s = 0 := quot.induction_on s $ Ξ» l H, by rw eq_nil_iff_forall_not_mem.mpr H; refl theorem eq_zero_iff_forall_not_mem {s : multiset Ξ±} : s = 0 ↔ βˆ€ a, a βˆ‰ s := ⟨λ h, h.symm β–Έ Ξ» _, not_false, eq_zero_of_forall_not_mem⟩ theorem exists_mem_of_ne_zero {s : multiset Ξ±} : s β‰  0 β†’ βˆƒ a : Ξ±, a ∈ s := quot.induction_on s $ assume l hl, match l, hl with | [] := assume h, false.elim $ h rfl | (a :: l) := assume _, ⟨a, by simp⟩ end @[simp] lemma zero_ne_cons {a : Ξ±} {m : multiset Ξ±} : 0 β‰  a :: m := assume h, have a ∈ (0:multiset Ξ±), from h.symm β–Έ mem_cons_self _ _, not_mem_zero _ this @[simp] lemma cons_ne_zero {a : Ξ±} {m : multiset Ξ±} : a :: m β‰  0 := zero_ne_cons.symm lemma cons_eq_cons {a b : Ξ±} {as bs : multiset Ξ±} : a :: as = b :: bs ↔ ((a = b ∧ as = bs) ∨ (a β‰  b ∧ βˆƒcs, as = b :: cs ∧ bs = a :: cs)) := begin haveI : decidable_eq Ξ± := classical.dec_eq Ξ±, split, { assume eq, by_cases a = b, { subst h, simp * at * }, { have : a ∈ b :: bs, from eq β–Έ mem_cons_self _ _, have : a ∈ bs, by simpa [h], rcases exists_cons_of_mem this with ⟨cs, hcs⟩, simp [h, hcs], have : a :: as = b :: a :: cs, by simp [eq, hcs], have : a :: as = a :: b :: cs, by rwa [cons_swap], simpa using this } }, { assume h, rcases h with ⟨eq₁, eqβ‚‚βŸ© | ⟨h, cs, eq₁, eqβ‚‚βŸ©, { simp * }, { simp [*, cons_swap a b] } } end end mem /- subset -/ section subset /-- `s βŠ† t` is the lift of the list subset relation. It means that any element with nonzero multiplicity in `s` has nonzero multiplicity in `t`, but it does not imply that the multiplicity of `a` in `s` is less or equal than in `t`; see `s ≀ t` for this relation. -/ protected def subset (s t : multiset Ξ±) : Prop := βˆ€ ⦃a : α⦄, a ∈ s β†’ a ∈ t instance : has_subset (multiset Ξ±) := ⟨multiset.subset⟩ @[simp] theorem coe_subset {l₁ lβ‚‚ : list Ξ±} : (l₁ : multiset Ξ±) βŠ† lβ‚‚ ↔ l₁ βŠ† lβ‚‚ := iff.rfl @[simp] theorem subset.refl (s : multiset Ξ±) : s βŠ† s := Ξ» a h, h theorem subset.trans {s t u : multiset Ξ±} : s βŠ† t β†’ t βŠ† u β†’ s βŠ† u := Ξ» h₁ hβ‚‚ a m, hβ‚‚ (h₁ m) theorem subset_iff {s t : multiset Ξ±} : s βŠ† t ↔ (βˆ€β¦ƒx⦄, x ∈ s β†’ x ∈ t) := iff.rfl theorem mem_of_subset {s t : multiset Ξ±} {a : Ξ±} (h : s βŠ† t) : a ∈ s β†’ a ∈ t := @h _ @[simp] theorem zero_subset (s : multiset Ξ±) : 0 βŠ† s := Ξ» a, (not_mem_nil a).elim @[simp] theorem cons_subset {a : Ξ±} {s t : multiset Ξ±} : (a :: s) βŠ† t ↔ a ∈ t ∧ s βŠ† t := by simp [subset_iff, or_imp_distrib, forall_and_distrib] theorem eq_zero_of_subset_zero {s : multiset Ξ±} (h : s βŠ† 0) : s = 0 := eq_zero_of_forall_not_mem h theorem subset_zero {s : multiset Ξ±} : s βŠ† 0 ↔ s = 0 := ⟨eq_zero_of_subset_zero, Ξ» xeq, xeq.symm β–Έ subset.refl 0⟩ end subset section to_list /-- Produces a list of the elements in the multiset using choice. -/ @[reducible] noncomputable def to_list {Ξ± : Type*} (s : multiset Ξ±) := classical.some (quotient.exists_rep s) @[simp] lemma to_list_zero {Ξ± : Type*} : (multiset.to_list 0 : list Ξ±) = [] := (multiset.coe_eq_zero _).1 (classical.some_spec (quotient.exists_rep multiset.zero)) lemma coe_to_list {Ξ± : Type*} (s : multiset Ξ±) : (s.to_list : multiset Ξ±) = s := classical.some_spec (quotient.exists_rep _) lemma mem_to_list {Ξ± : Type*} (a : Ξ±) (s : multiset Ξ±) : a ∈ s.to_list ↔ a ∈ s := by rw [←multiset.mem_coe, multiset.coe_to_list] end to_list /- multiset order -/ /-- `s ≀ t` means that `s` is a sublist of `t` (up to permutation). Equivalently, `s ≀ t` means that `count a s ≀ count a t` for all `a`. -/ protected def le (s t : multiset Ξ±) : Prop := quotient.lift_onβ‚‚ s t (<+~) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, propext (pβ‚‚.subperm_left.trans p₁.subperm_right) instance : partial_order (multiset Ξ±) := { le := multiset.le, le_refl := by rintros ⟨l⟩; exact subperm.refl _, le_trans := by rintros ⟨lβ‚βŸ© ⟨lβ‚‚βŸ© ⟨lβ‚ƒβŸ©; exact @subperm.trans _ _ _ _, le_antisymm := by rintros ⟨lβ‚βŸ© ⟨lβ‚‚βŸ© h₁ hβ‚‚; exact quot.sound (subperm.antisymm h₁ hβ‚‚) } theorem subset_of_le {s t : multiset Ξ±} : s ≀ t β†’ s βŠ† t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, subperm.subset theorem mem_of_le {s t : multiset Ξ±} {a : Ξ±} (h : s ≀ t) : a ∈ s β†’ a ∈ t := mem_of_subset (subset_of_le h) @[simp] theorem coe_le {l₁ lβ‚‚ : list Ξ±} : (l₁ : multiset Ξ±) ≀ lβ‚‚ ↔ l₁ <+~ lβ‚‚ := iff.rfl @[elab_as_eliminator] theorem le_induction_on {C : multiset Ξ± β†’ multiset Ξ± β†’ Prop} {s t : multiset Ξ±} (h : s ≀ t) (H : βˆ€ {l₁ lβ‚‚ : list Ξ±}, l₁ <+ lβ‚‚ β†’ C l₁ lβ‚‚) : C s t := quotient.induction_onβ‚‚ s t (Ξ» l₁ lβ‚‚ ⟨l, p, s⟩, (show ⟦l⟧ = ⟦lβ‚βŸ§, from quot.sound p) β–Έ H s) h theorem zero_le (s : multiset Ξ±) : 0 ≀ s := quot.induction_on s $ Ξ» l, (nil_sublist l).subperm theorem le_zero {s : multiset Ξ±} : s ≀ 0 ↔ s = 0 := ⟨λ h, le_antisymm h (zero_le _), le_of_eq⟩ theorem lt_cons_self (s : multiset Ξ±) (a : Ξ±) : s < a :: s := quot.induction_on s $ Ξ» l, suffices l <+~ a :: l ∧ (Β¬l ~ a :: l), by simpa [lt_iff_le_and_ne], ⟨(sublist_cons _ _).subperm, Ξ» p, ne_of_lt (lt_succ_self (length l)) p.length_eq⟩ theorem le_cons_self (s : multiset Ξ±) (a : Ξ±) : s ≀ a :: s := le_of_lt $ lt_cons_self _ _ theorem cons_le_cons_iff (a : Ξ±) {s t : multiset Ξ±} : a :: s ≀ a :: t ↔ s ≀ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, subperm_cons a theorem cons_le_cons (a : Ξ±) {s t : multiset Ξ±} : s ≀ t β†’ a :: s ≀ a :: t := (cons_le_cons_iff a).2 theorem le_cons_of_not_mem {a : Ξ±} {s t : multiset Ξ±} (m : a βˆ‰ s) : s ≀ a :: t ↔ s ≀ t := begin refine ⟨_, Ξ» h, le_trans h $ le_cons_self _ _⟩, suffices : βˆ€ {t'} (_ : s ≀ t') (_ : a ∈ t'), a :: s ≀ t', { exact Ξ» h, (cons_le_cons_iff a).1 (this h (mem_cons_self _ _)) }, introv h, revert m, refine le_induction_on h _, introv s m₁ mβ‚‚, rcases mem_split mβ‚‚ with ⟨r₁, rβ‚‚, rfl⟩, exact perm_middle.subperm_left.2 ((subperm_cons _).2 $ ((sublist_or_mem_of_sublist s).resolve_right m₁).subperm) end /- cardinality -/ /-- The cardinality of a multiset is the sum of the multiplicities of all its elements, or simply the length of the underlying list. -/ def card (s : multiset Ξ±) : β„• := quot.lift_on s length $ Ξ» l₁ lβ‚‚, perm.length_eq @[simp] theorem coe_card (l : list Ξ±) : card (l : multiset Ξ±) = length l := rfl @[simp] theorem card_zero : @card Ξ± 0 = 0 := rfl @[simp] theorem card_cons (a : Ξ±) (s : multiset Ξ±) : card (a :: s) = card s + 1 := quot.induction_on s $ Ξ» l, rfl @[simp] theorem card_singleton (a : Ξ±) : card (a::0) = 1 := by simp theorem card_le_of_le {s t : multiset Ξ±} (h : s ≀ t) : card s ≀ card t := le_induction_on h $ Ξ» l₁ lβ‚‚, length_le_of_sublist theorem eq_of_le_of_card_le {s t : multiset Ξ±} (h : s ≀ t) : card t ≀ card s β†’ s = t := le_induction_on h $ Ξ» l₁ lβ‚‚ s hβ‚‚, congr_arg coe $ eq_of_sublist_of_length_le s hβ‚‚ theorem card_lt_of_lt {s t : multiset Ξ±} (h : s < t) : card s < card t := lt_of_not_ge $ Ξ» hβ‚‚, ne_of_lt h $ eq_of_le_of_card_le (le_of_lt h) hβ‚‚ theorem lt_iff_cons_le {s t : multiset Ξ±} : s < t ↔ βˆƒ a, a :: s ≀ t := ⟨quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, subperm.exists_of_length_lt (le_of_lt h) (card_lt_of_lt h), Ξ» ⟨a, h⟩, lt_of_lt_of_le (lt_cons_self _ _) h⟩ @[simp] theorem card_eq_zero {s : multiset Ξ±} : card s = 0 ↔ s = 0 := ⟨λ h, (eq_of_le_of_card_le (zero_le _) (le_of_eq h)).symm, Ξ» e, by simp [e]⟩ theorem card_pos {s : multiset Ξ±} : 0 < card s ↔ s β‰  0 := pos_iff_ne_zero.trans $ not_congr card_eq_zero theorem card_pos_iff_exists_mem {s : multiset Ξ±} : 0 < card s ↔ βˆƒ a, a ∈ s := quot.induction_on s $ Ξ» l, length_pos_iff_exists_mem @[elab_as_eliminator] def strong_induction_on {p : multiset Ξ± β†’ Sort*} : βˆ€ (s : multiset Ξ±), (βˆ€ s, (βˆ€t < s, p t) β†’ p s) β†’ p s | s := Ξ» ih, ih s $ Ξ» t h, have card t < card s, from card_lt_of_lt h, strong_induction_on t ih using_well_founded {rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf card⟩]} theorem strong_induction_eq {p : multiset Ξ± β†’ Sort*} (s : multiset Ξ±) (H) : @strong_induction_on _ p s H = H s (Ξ» t h, @strong_induction_on _ p t H) := by rw [strong_induction_on] @[elab_as_eliminator] lemma case_strong_induction_on {p : multiset Ξ± β†’ Prop} (s : multiset Ξ±) (hβ‚€ : p 0) (h₁ : βˆ€ a s, (βˆ€t ≀ s, p t) β†’ p (a :: s)) : p s := multiset.strong_induction_on s $ assume s, multiset.induction_on s (Ξ» _, hβ‚€) $ Ξ» a s _ ih, h₁ _ _ $ Ξ» t h, ih _ $ lt_of_le_of_lt h $ lt_cons_self _ _ /- singleton -/ instance : has_singleton Ξ± (multiset Ξ±) := ⟨λ a, a::0⟩ instance : is_lawful_singleton Ξ± (multiset Ξ±) := ⟨λ a, rfl⟩ @[simp] theorem singleton_eq_singleton (a : Ξ±) : singleton a = a::0 := rfl @[simp] theorem mem_singleton {a b : Ξ±} : b ∈ a::0 ↔ b = a := by simp theorem mem_singleton_self (a : Ξ±) : a ∈ (a::0 : multiset Ξ±) := mem_cons_self _ _ theorem singleton_inj {a b : Ξ±} : a::0 = b::0 ↔ a = b := cons_inj_left _ @[simp] theorem singleton_ne_zero (a : Ξ±) : a::0 β‰  0 := ne_of_gt (lt_cons_self _ _) @[simp] theorem singleton_le {a : Ξ±} {s : multiset Ξ±} : a::0 ≀ s ↔ a ∈ s := ⟨λ h, mem_of_le h (mem_singleton_self _), Ξ» h, let ⟨t, e⟩ := exists_cons_of_mem h in e.symm β–Έ cons_le_cons _ (zero_le _)⟩ theorem card_eq_one {s : multiset Ξ±} : card s = 1 ↔ βˆƒ a, s = a::0 := ⟨quot.induction_on s $ Ξ» l h, (list.length_eq_one.1 h).imp $ Ξ» a, congr_arg coe, Ξ» ⟨a, e⟩, e.symm β–Έ rfl⟩ /- add -/ /-- The sum of two multisets is the lift of the list append operation. This adds the multiplicities of each element, i.e. `count a (s + t) = count a s + count a t`. -/ protected def add (s₁ sβ‚‚ : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s₁ sβ‚‚ (Ξ» l₁ lβ‚‚, ((l₁ ++ lβ‚‚ : list Ξ±) : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ p₁.append pβ‚‚ instance : has_add (multiset Ξ±) := ⟨multiset.add⟩ @[simp] theorem coe_add (s t : list Ξ±) : (s + t : multiset Ξ±) = (s ++ t : list Ξ±) := rfl protected theorem add_comm (s t : multiset Ξ±) : s + t = t + s := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, quot.sound perm_append_comm protected theorem zero_add (s : multiset Ξ±) : 0 + s = s := quot.induction_on s $ Ξ» l, rfl theorem singleton_add (a : Ξ±) (s : multiset Ξ±) : ↑[a] + s = a::s := rfl protected theorem add_le_add_left (s) {t u : multiset Ξ±} : s + t ≀ s + u ↔ t ≀ u := quotient.induction_on₃ s t u $ Ξ» l₁ lβ‚‚ l₃, subperm_append_left _ protected theorem add_left_cancel (s) {t u : multiset Ξ±} (h : s + t = s + u) : t = u := le_antisymm ((multiset.add_le_add_left _).1 (le_of_eq h)) ((multiset.add_le_add_left _).1 (le_of_eq h.symm)) instance : ordered_cancel_add_comm_monoid (multiset Ξ±) := { zero := 0, add := (+), add_comm := multiset.add_comm, add_assoc := Ξ» s₁ sβ‚‚ s₃, quotient.induction_on₃ s₁ sβ‚‚ s₃ $ Ξ» l₁ lβ‚‚ l₃, congr_arg coe $ append_assoc l₁ lβ‚‚ l₃, zero_add := multiset.zero_add, add_zero := Ξ» s, by rw [multiset.add_comm, multiset.zero_add], add_left_cancel := multiset.add_left_cancel, add_right_cancel := Ξ» s₁ sβ‚‚ s₃ h, multiset.add_left_cancel sβ‚‚ $ by simpa [multiset.add_comm] using h, add_le_add_left := Ξ» s₁ sβ‚‚ h s₃, (multiset.add_le_add_left _).2 h, le_of_add_le_add_left := Ξ» s₁ sβ‚‚ s₃, (multiset.add_le_add_left _).1, ..@multiset.partial_order Ξ± } @[simp] theorem cons_add (a : Ξ±) (s t : multiset Ξ±) : a :: s + t = a :: (s + t) := by rw [← singleton_add, ← singleton_add, add_assoc] @[simp] theorem add_cons (a : Ξ±) (s t : multiset Ξ±) : s + a :: t = a :: (s + t) := by rw [add_comm, cons_add, add_comm] theorem le_add_right (s t : multiset Ξ±) : s ≀ s + t := by simpa using add_le_add_left (zero_le t) s theorem le_add_left (s t : multiset Ξ±) : s ≀ t + s := by simpa using add_le_add_right (zero_le t) s @[simp] theorem card_add (s t : multiset Ξ±) : card (s + t) = card s + card t := quotient.induction_onβ‚‚ s t length_append lemma card_smul (s : multiset Ξ±) (n : β„•) : (n β€’β„• s).card = n * s.card := by induction n; simp [succ_nsmul, *, nat.succ_mul]; cc @[simp] theorem mem_add {a : Ξ±} {s t : multiset Ξ±} : a ∈ s + t ↔ a ∈ s ∨ a ∈ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, mem_append theorem le_iff_exists_add {s t : multiset Ξ±} : s ≀ t ↔ βˆƒ u, t = s + u := ⟨λ h, le_induction_on h $ Ξ» l₁ lβ‚‚ s, let ⟨l, p⟩ := s.exists_perm_append in ⟨l, quot.sound p⟩, λ⟨u, e⟩, e.symm β–Έ le_add_right s u⟩ instance : canonically_ordered_add_monoid (multiset Ξ±) := { lt_of_add_lt_add_left := @lt_of_add_lt_add_left _ _, le_iff_exists_add := @le_iff_exists_add _, bot := 0, bot_le := multiset.zero_le, ..multiset.ordered_cancel_add_comm_monoid } /- repeat -/ /-- `repeat a n` is the multiset containing only `a` with multiplicity `n`. -/ def repeat (a : Ξ±) (n : β„•) : multiset Ξ± := repeat a n @[simp] lemma repeat_zero (a : Ξ±) : repeat a 0 = 0 := rfl @[simp] lemma repeat_succ (a : Ξ±) (n) : repeat a (n+1) = a :: repeat a n := by simp [repeat] @[simp] lemma repeat_one (a : Ξ±) : repeat a 1 = a :: 0 := by simp @[simp] lemma card_repeat : βˆ€ (a : Ξ±) n, card (repeat a n) = n := length_repeat theorem eq_of_mem_repeat {a b : Ξ±} {n} : b ∈ repeat a n β†’ b = a := eq_of_mem_repeat theorem eq_repeat' {a : Ξ±} {s : multiset Ξ±} : s = repeat a s.card ↔ βˆ€ b ∈ s, b = a := quot.induction_on s $ Ξ» l, iff.trans ⟨λ h, (perm_repeat.1 $ (quotient.exact h)), congr_arg coe⟩ eq_repeat' theorem eq_repeat_of_mem {a : Ξ±} {s : multiset Ξ±} : (βˆ€ b ∈ s, b = a) β†’ s = repeat a s.card := eq_repeat'.2 theorem eq_repeat {a : Ξ±} {n} {s : multiset Ξ±} : s = repeat a n ↔ card s = n ∧ βˆ€ b ∈ s, b = a := ⟨λ h, h.symm β–Έ ⟨card_repeat _ _, Ξ» b, eq_of_mem_repeat⟩, Ξ» ⟨e, al⟩, e β–Έ eq_repeat_of_mem al⟩ theorem repeat_subset_singleton : βˆ€ (a : Ξ±) n, repeat a n βŠ† a::0 := repeat_subset_singleton theorem repeat_le_coe {a : Ξ±} {n} {l : list Ξ±} : repeat a n ≀ l ↔ list.repeat a n <+ l := ⟨λ ⟨l', p, s⟩, (perm_repeat.1 p) β–Έ s, sublist.subperm⟩ /- erase -/ section erase variables [decidable_eq Ξ±] {s t : multiset Ξ±} {a b : Ξ±} /-- `erase s a` is the multiset that subtracts 1 from the multiplicity of `a`. -/ def erase (s : multiset Ξ±) (a : Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (l.erase a : multiset Ξ±)) (Ξ» l₁ lβ‚‚ p, quot.sound (p.erase a)) @[simp] theorem coe_erase (l : list Ξ±) (a : Ξ±) : erase (l : multiset Ξ±) a = l.erase a := rfl @[simp] theorem erase_zero (a : Ξ±) : (0 : multiset Ξ±).erase a = 0 := rfl @[simp] theorem erase_cons_head (a : Ξ±) (s : multiset Ξ±) : (a :: s).erase a = s := quot.induction_on s $ Ξ» l, congr_arg coe $ erase_cons_head a l @[simp, priority 990] theorem erase_cons_tail {a b : Ξ±} (s : multiset Ξ±) (h : b β‰  a) : (b::s).erase a = b :: s.erase a := quot.induction_on s $ Ξ» l, congr_arg coe $ erase_cons_tail l h @[simp, priority 980] theorem erase_of_not_mem {a : Ξ±} {s : multiset Ξ±} : a βˆ‰ s β†’ s.erase a = s := quot.induction_on s $ Ξ» l h, congr_arg coe $ erase_of_not_mem h @[simp, priority 980] theorem cons_erase {s : multiset Ξ±} {a : Ξ±} : a ∈ s β†’ a :: s.erase a = s := quot.induction_on s $ Ξ» l h, quot.sound (perm_cons_erase h).symm theorem le_cons_erase (s : multiset Ξ±) (a : Ξ±) : s ≀ a :: s.erase a := if h : a ∈ s then le_of_eq (cons_erase h).symm else by rw erase_of_not_mem h; apply le_cons_self theorem erase_add_left_pos {a : Ξ±} {s : multiset Ξ±} (t) : a ∈ s β†’ (s + t).erase a = s.erase a + t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ erase_append_left lβ‚‚ h theorem erase_add_right_pos {a : Ξ±} (s) {t : multiset Ξ±} (h : a ∈ t) : (s + t).erase a = s + t.erase a := by rw [add_comm, erase_add_left_pos s h, add_comm] theorem erase_add_right_neg {a : Ξ±} {s : multiset Ξ±} (t) : a βˆ‰ s β†’ (s + t).erase a = s + t.erase a := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ erase_append_right lβ‚‚ h theorem erase_add_left_neg {a : Ξ±} (s) {t : multiset Ξ±} (h : a βˆ‰ t) : (s + t).erase a = s.erase a + t := by rw [add_comm, erase_add_right_neg s h, add_comm] theorem erase_le (a : Ξ±) (s : multiset Ξ±) : s.erase a ≀ s := quot.induction_on s $ Ξ» l, (erase_sublist a l).subperm @[simp] theorem erase_lt {a : Ξ±} {s : multiset Ξ±} : s.erase a < s ↔ a ∈ s := ⟨λ h, not_imp_comm.1 erase_of_not_mem (ne_of_lt h), Ξ» h, by simpa [h] using lt_cons_self (s.erase a) a⟩ theorem erase_subset (a : Ξ±) (s : multiset Ξ±) : s.erase a βŠ† s := subset_of_le (erase_le a s) theorem mem_erase_of_ne {a b : Ξ±} {s : multiset Ξ±} (ab : a β‰  b) : a ∈ s.erase b ↔ a ∈ s := quot.induction_on s $ Ξ» l, list.mem_erase_of_ne ab theorem mem_of_mem_erase {a b : Ξ±} {s : multiset Ξ±} : a ∈ s.erase b β†’ a ∈ s := mem_of_subset (erase_subset _ _) theorem erase_comm (s : multiset Ξ±) (a b : Ξ±) : (s.erase a).erase b = (s.erase b).erase a := quot.induction_on s $ Ξ» l, congr_arg coe $ l.erase_comm a b theorem erase_le_erase {s t : multiset Ξ±} (a : Ξ±) (h : s ≀ t) : s.erase a ≀ t.erase a := le_induction_on h $ Ξ» l₁ lβ‚‚ h, (h.erase _).subperm theorem erase_le_iff_le_cons {s t : multiset Ξ±} {a : Ξ±} : s.erase a ≀ t ↔ s ≀ a :: t := ⟨λ h, le_trans (le_cons_erase _ _) (cons_le_cons _ h), Ξ» h, if m : a ∈ s then by rw ← cons_erase m at h; exact (cons_le_cons_iff _).1 h else le_trans (erase_le _ _) ((le_cons_of_not_mem m).1 h)⟩ @[simp] theorem card_erase_of_mem {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ card (s.erase a) = pred (card s) := quot.induction_on s $ Ξ» l, length_erase_of_mem theorem card_erase_lt_of_mem {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ card (s.erase a) < card s := Ξ» h, card_lt_of_lt (erase_lt.mpr h) theorem card_erase_le {a : Ξ±} {s : multiset Ξ±} : card (s.erase a) ≀ card s := card_le_of_le (erase_le a s) end erase @[simp] theorem coe_reverse (l : list Ξ±) : (reverse l : multiset Ξ±) = l := quot.sound $ reverse_perm _ /- map -/ /-- `map f s` is the lift of the list `map` operation. The multiplicity of `b` in `map f s` is the number of `a ∈ s` (counting multiplicity) such that `f a = b`. -/ def map (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : multiset Ξ² := quot.lift_on s (Ξ» l : list Ξ±, (l.map f : multiset Ξ²)) (Ξ» l₁ lβ‚‚ p, quot.sound (p.map f)) theorem forall_mem_map_iff {f : Ξ± β†’ Ξ²} {p : Ξ² β†’ Prop} {s : multiset Ξ±} : (βˆ€ y ∈ s.map f, p y) ↔ (βˆ€ x ∈ s, p (f x)) := quotient.induction_on' s $ Ξ» L, list.forall_mem_map_iff @[simp] theorem coe_map (f : Ξ± β†’ Ξ²) (l : list Ξ±) : map f ↑l = l.map f := rfl @[simp] theorem map_zero (f : Ξ± β†’ Ξ²) : map f 0 = 0 := rfl @[simp] theorem map_cons (f : Ξ± β†’ Ξ²) (a s) : map f (a::s) = f a :: map f s := quot.induction_on s $ Ξ» l, rfl lemma map_singleton (f : Ξ± β†’ Ξ²) (a : Ξ±) : ({a} : multiset Ξ±).map f = {f a} := rfl theorem map_repeat (f : Ξ± β†’ Ξ²) (a : Ξ±) (k : β„•) : (repeat a k).map f = repeat (f a) k := by { induction k, simp, simpa } @[simp] theorem map_add (f : Ξ± β†’ Ξ²) (s t) : map f (s + t) = map f s + map f t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ map_append _ _ _ instance (f : Ξ± β†’ Ξ²) : is_add_monoid_hom (map f) := { map_add := map_add _, map_zero := map_zero _ } @[simp] theorem mem_map {f : Ξ± β†’ Ξ²} {b : Ξ²} {s : multiset Ξ±} : b ∈ map f s ↔ βˆƒ a, a ∈ s ∧ f a = b := quot.induction_on s $ Ξ» l, mem_map @[simp] theorem card_map (f : Ξ± β†’ Ξ²) (s) : card (map f s) = card s := quot.induction_on s $ Ξ» l, length_map _ _ @[simp] theorem map_eq_zero {s : multiset Ξ±} {f : Ξ± β†’ Ξ²} : s.map f = 0 ↔ s = 0 := by rw [← multiset.card_eq_zero, multiset.card_map, multiset.card_eq_zero] theorem mem_map_of_mem (f : Ξ± β†’ Ξ²) {a : Ξ±} {s : multiset Ξ±} (h : a ∈ s) : f a ∈ map f s := mem_map.2 ⟨_, h, rfl⟩ theorem mem_map_of_injective {f : Ξ± β†’ Ξ²} (H : function.injective f) {a : Ξ±} {s : multiset Ξ±} : f a ∈ map f s ↔ a ∈ s := quot.induction_on s $ Ξ» l, mem_map_of_injective H @[simp] theorem map_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : map g (map f s) = map (g ∘ f) s := quot.induction_on s $ Ξ» l, congr_arg coe $ list.map_map _ _ _ theorem map_id (s : multiset Ξ±) : map id s = s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_id _ @[simp] lemma map_id' (s : multiset Ξ±) : map (Ξ»x, x) s = s := map_id s @[simp] theorem map_const (s : multiset Ξ±) (b : Ξ²) : map (function.const Ξ± b) s = repeat b s.card := quot.induction_on s $ Ξ» l, congr_arg coe $ map_const _ _ @[congr] theorem map_congr {f g : Ξ± β†’ Ξ²} {s : multiset Ξ±} : (βˆ€ x ∈ s, f x = g x) β†’ map f s = map g s := quot.induction_on s $ Ξ» l H, congr_arg coe $ map_congr H lemma map_hcongr {Ξ²' : Type*} {m : multiset Ξ±} {f : Ξ± β†’ Ξ²} {f' : Ξ± β†’ Ξ²'} (h : Ξ² = Ξ²') (hf : βˆ€a∈m, f a == f' a) : map f m == map f' m := begin subst h, simp at hf, simp [map_congr hf] end theorem eq_of_mem_map_const {b₁ bβ‚‚ : Ξ²} {l : list Ξ±} (h : b₁ ∈ map (function.const Ξ± bβ‚‚) l) : b₁ = bβ‚‚ := eq_of_mem_repeat $ by rwa map_const at h @[simp] theorem map_le_map {f : Ξ± β†’ Ξ²} {s t : multiset Ξ±} (h : s ≀ t) : map f s ≀ map f t := le_induction_on h $ Ξ» l₁ lβ‚‚ h, (h.map f).subperm @[simp] theorem map_subset_map {f : Ξ± β†’ Ξ²} {s t : multiset Ξ±} (H : s βŠ† t) : map f s βŠ† map f t := Ξ» b m, let ⟨a, h, e⟩ := mem_map.1 m in mem_map.2 ⟨a, H h, e⟩ /- fold -/ /-- `foldl f H b s` is the lift of the list operation `foldl f b l`, which folds `f` over the multiset. It is well defined when `f` is right-commutative, that is, `f (f b a₁) aβ‚‚ = f (f b aβ‚‚) a₁`. -/ def foldl (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (b : Ξ²) (s : multiset Ξ±) : Ξ² := quot.lift_on s (Ξ» l, foldl f b l) (Ξ» l₁ lβ‚‚ p, p.foldl_eq H b) @[simp] theorem foldl_zero (f : Ξ² β†’ Ξ± β†’ Ξ²) (H b) : foldl f H b 0 = b := rfl @[simp] theorem foldl_cons (f : Ξ² β†’ Ξ± β†’ Ξ²) (H b a s) : foldl f H b (a :: s) = foldl f H (f b a) s := quot.induction_on s $ Ξ» l, rfl @[simp] theorem foldl_add (f : Ξ² β†’ Ξ± β†’ Ξ²) (H b s t) : foldl f H b (s + t) = foldl f H (foldl f H b s) t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, foldl_append _ _ _ _ /-- `foldr f H b s` is the lift of the list operation `foldr f b l`, which folds `f` over the multiset. It is well defined when `f` is left-commutative, that is, `f a₁ (f aβ‚‚ b) = f aβ‚‚ (f a₁ b)`. -/ def foldr (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (s : multiset Ξ±) : Ξ² := quot.lift_on s (Ξ» l, foldr f b l) (Ξ» l₁ lβ‚‚ p, p.foldr_eq H b) @[simp] theorem foldr_zero (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b) : foldr f H b 0 = b := rfl @[simp] theorem foldr_cons (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b a s) : foldr f H b (a :: s) = f a (foldr f H b s) := quot.induction_on s $ Ξ» l, rfl @[simp] theorem foldr_add (f : Ξ± β†’ Ξ² β†’ Ξ²) (H b s t) : foldr f H b (s + t) = foldr f H (foldr f H b t) s := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, foldr_append _ _ _ _ @[simp] theorem coe_foldr (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (l : list Ξ±) : foldr f H b l = l.foldr f b := rfl @[simp] theorem coe_foldl (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (b : Ξ²) (l : list Ξ±) : foldl f H b l = l.foldl f b := rfl theorem coe_foldr_swap (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (l : list Ξ±) : foldr f H b l = l.foldl (Ξ» x y, f y x) b := (congr_arg (foldr f H b) (coe_reverse l)).symm.trans $ foldr_reverse _ _ _ theorem foldr_swap (f : Ξ± β†’ Ξ² β†’ Ξ²) (H : left_commutative f) (b : Ξ²) (s : multiset Ξ±) : foldr f H b s = foldl (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) b s := quot.induction_on s $ Ξ» l, coe_foldr_swap _ _ _ _ theorem foldl_swap (f : Ξ² β†’ Ξ± β†’ Ξ²) (H : right_commutative f) (b : Ξ²) (s : multiset Ξ±) : foldl f H b s = foldr (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) b s := (foldr_swap _ _ _ _).symm /-- Product of a multiset given a commutative monoid structure on `Ξ±`. `prod {a, b, c} = a * b * c` -/ @[to_additive] def prod [comm_monoid Ξ±] : multiset Ξ± β†’ Ξ± := foldr (*) (Ξ» x y z, by simp [mul_left_comm]) 1 @[to_additive] theorem prod_eq_foldr [comm_monoid Ξ±] (s : multiset Ξ±) : prod s = foldr (*) (Ξ» x y z, by simp [mul_left_comm]) 1 s := rfl @[to_additive] theorem prod_eq_foldl [comm_monoid Ξ±] (s : multiset Ξ±) : prod s = foldl (*) (Ξ» x y z, by simp [mul_right_comm]) 1 s := (foldr_swap _ _ _ _).trans (by simp [mul_comm]) @[simp, to_additive] theorem coe_prod [comm_monoid Ξ±] (l : list Ξ±) : prod ↑l = l.prod := prod_eq_foldl _ @[simp, to_additive] theorem prod_zero [comm_monoid Ξ±] : @prod Ξ± _ 0 = 1 := rfl @[simp, to_additive] theorem prod_cons [comm_monoid Ξ±] (a : Ξ±) (s) : prod (a :: s) = a * prod s := foldr_cons _ _ _ _ _ @[to_additive] theorem prod_singleton [comm_monoid Ξ±] (a : Ξ±) : prod (a :: 0) = a := by simp @[simp, to_additive] theorem prod_add [comm_monoid Ξ±] (s t : multiset Ξ±) : prod (s + t) = prod s * prod t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, by simp instance sum.is_add_monoid_hom [add_comm_monoid Ξ±] : is_add_monoid_hom (sum : multiset Ξ± β†’ Ξ±) := { map_add := sum_add, map_zero := sum_zero } lemma prod_smul {Ξ± : Type*} [comm_monoid Ξ±] (m : multiset Ξ±) : βˆ€n, (n β€’β„• m).prod = m.prod ^ n | 0 := rfl | (n + 1) := by rw [add_nsmul, one_nsmul, _root_.pow_add, _root_.pow_one, prod_add, prod_smul n] @[simp] theorem prod_repeat [comm_monoid Ξ±] (a : Ξ±) (n : β„•) : prod (multiset.repeat a n) = a ^ n := by simp [repeat, list.prod_repeat] @[simp] theorem sum_repeat [add_comm_monoid Ξ±] : βˆ€ (a : Ξ±) (n : β„•), sum (multiset.repeat a n) = n β€’β„• a := @prod_repeat (multiplicative Ξ±) _ attribute [to_additive] prod_repeat lemma prod_map_one [comm_monoid Ξ³] {m : multiset Ξ±} : prod (m.map (Ξ»a, (1 : Ξ³))) = (1 : Ξ³) := by simp lemma sum_map_zero [add_comm_monoid Ξ³] {m : multiset Ξ±} : sum (m.map (Ξ»a, (0 : Ξ³))) = (0 : Ξ³) := by simp attribute [to_additive] prod_map_one @[simp, to_additive] lemma prod_map_mul [comm_monoid Ξ³] {m : multiset Ξ±} {f g : Ξ± β†’ Ξ³} : prod (m.map $ Ξ»a, f a * g a) = prod (m.map f) * prod (m.map g) := multiset.induction_on m (by simp) (assume a m ih, by simp [ih]; cc) lemma prod_map_prod_map [comm_monoid Ξ³] (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ Ξ³} : prod (m.map $ Ξ»a, prod $ n.map $ Ξ»b, f a b) = prod (n.map $ Ξ»b, prod $ m.map $ Ξ»a, f a b) := multiset.induction_on m (by simp) (assume a m ih, by simp [ih]) lemma sum_map_sum_map [add_comm_monoid Ξ³] : βˆ€ (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ Ξ³}, sum (m.map $ Ξ»a, sum $ n.map $ Ξ»b, f a b) = sum (n.map $ Ξ»b, sum $ m.map $ Ξ»a, f a b) := @prod_map_prod_map _ _ (multiplicative Ξ³) _ attribute [to_additive] prod_map_prod_map lemma sum_map_mul_left [semiring Ξ²] {b : Ξ²} {s : multiset Ξ±} {f : Ξ± β†’ Ξ²} : sum (s.map (Ξ»a, b * f a)) = b * sum (s.map f) := multiset.induction_on s (by simp) (assume a s ih, by simp [ih, mul_add]) lemma sum_map_mul_right [semiring Ξ²] {b : Ξ²} {s : multiset Ξ±} {f : Ξ± β†’ Ξ²} : sum (s.map (Ξ»a, f a * b)) = sum (s.map f) * b := multiset.induction_on s (by simp) (assume a s ih, by simp [ih, add_mul]) theorem prod_ne_zero {R : Type*} [integral_domain R] {m : multiset R} : (βˆ€ x ∈ m, (x : _) β‰  0) β†’ m.prod β‰  0 := multiset.induction_on m (Ξ» _, one_ne_zero) $ Ξ» hd tl ih H, by { rw forall_mem_cons at H, rw prod_cons, exact mul_ne_zero H.1 (ih H.2) } lemma prod_eq_zero {Ξ± : Type*} [comm_semiring Ξ±] {s : multiset Ξ±} (h : (0 : Ξ±) ∈ s) : multiset.prod s = 0 := begin rcases multiset.exists_cons_of_mem h with ⟨s', hs'⟩, simp [hs', multiset.prod_cons] end @[to_additive] lemma prod_hom [comm_monoid Ξ±] [comm_monoid Ξ²] (s : multiset Ξ±) (f : Ξ± β†’* Ξ²) : (s.map f).prod = f s.prod := quotient.induction_on s $ Ξ» l, by simp only [l.prod_hom f, quot_mk_to_coe, coe_map, coe_prod] @[to_additive] theorem prod_hom_rel [comm_monoid Ξ²] [comm_monoid Ξ³] (s : multiset Ξ±) {r : Ξ² β†’ Ξ³ β†’ Prop} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} (h₁ : r 1 1) (hβ‚‚ : βˆ€β¦ƒa b c⦄, r b c β†’ r (f a * b) (g a * c)) : r (s.map f).prod (s.map g).prod := quotient.induction_on s $ Ξ» l, by simp only [l.prod_hom_rel h₁ hβ‚‚, quot_mk_to_coe, coe_map, coe_prod] lemma dvd_prod [comm_monoid Ξ±] {a : Ξ±} {s : multiset Ξ±} : a ∈ s β†’ a ∣ s.prod := quotient.induction_on s (Ξ» l a h, by simpa using list.dvd_prod h) a theorem prod_eq_zero_iff [comm_cancel_monoid_with_zero Ξ±] [nontrivial Ξ±] {s : multiset Ξ±} : s.prod = 0 ↔ (0 : Ξ±) ∈ s := multiset.induction_on s (by simp) $ assume a s, by simp [mul_eq_zero, @eq_comm _ 0 a] {contextual := tt} lemma le_sum_of_subadditive [add_comm_monoid Ξ±] [ordered_add_comm_monoid Ξ²] (f : Ξ± β†’ Ξ²) (h_zero : f 0 = 0) (h_add : βˆ€x y, f (x + y) ≀ f x + f y) (s : multiset Ξ±) : f s.sum ≀ (s.map f).sum := multiset.induction_on s (le_of_eq h_zero) $ assume a s ih, by rw [sum_cons, map_cons, sum_cons]; from le_trans (h_add a s.sum) (add_le_add_left ih _) lemma abs_sum_le_sum_abs [discrete_linear_ordered_field Ξ±] {s : multiset Ξ±} : abs s.sum ≀ (s.map abs).sum := le_sum_of_subadditive _ abs_zero abs_add s theorem dvd_sum [comm_semiring Ξ±] {a : Ξ±} {s : multiset Ξ±} : (βˆ€ x ∈ s, a ∣ x) β†’ a ∣ s.sum := multiset.induction_on s (Ξ» _, dvd_zero _) (Ξ» x s ih h, by rw sum_cons; exact dvd_add (h _ (mem_cons_self _ _)) (ih (Ξ» y hy, h _ (mem_cons.2 (or.inr hy))))) /- join -/ /-- `join S`, where `S` is a multiset of multisets, is the lift of the list join operation, that is, the union of all the sets. join {{1, 2}, {1, 2}, {0, 1}} = {0, 1, 1, 1, 2, 2} -/ def join : multiset (multiset Ξ±) β†’ multiset Ξ± := sum theorem coe_join : βˆ€ L : list (list Ξ±), join (L.map (@coe _ (multiset Ξ±) _) : multiset (multiset Ξ±)) = L.join | [] := rfl | (l :: L) := congr_arg (Ξ» s : multiset Ξ±, ↑l + s) (coe_join L) @[simp] theorem join_zero : @join Ξ± 0 = 0 := rfl @[simp] theorem join_cons (s S) : @join Ξ± (s :: S) = s + join S := sum_cons _ _ @[simp] theorem join_add (S T) : @join Ξ± (S + T) = join S + join T := sum_add _ _ @[simp] theorem mem_join {a S} : a ∈ @join Ξ± S ↔ βˆƒ s ∈ S, a ∈ s := multiset.induction_on S (by simp) $ by simp [or_and_distrib_right, exists_or_distrib] {contextual := tt} @[simp] theorem card_join (S) : card (@join Ξ± S) = sum (map card S) := multiset.induction_on S (by simp) (by simp) /- bind -/ /-- `bind s f` is the monad bind operation, defined as `join (map f s)`. It is the union of `f a` as `a` ranges over `s`. -/ def bind (s : multiset Ξ±) (f : Ξ± β†’ multiset Ξ²) : multiset Ξ² := join (map f s) @[simp] theorem coe_bind (l : list Ξ±) (f : Ξ± β†’ list Ξ²) : @bind Ξ± Ξ² l (Ξ» a, f a) = l.bind f := by rw [list.bind, ← coe_join, list.map_map]; refl @[simp] theorem zero_bind (f : Ξ± β†’ multiset Ξ²) : bind 0 f = 0 := rfl @[simp] theorem cons_bind (a s) (f : Ξ± β†’ multiset Ξ²) : bind (a::s) f = f a + bind s f := by simp [bind] @[simp] theorem add_bind (s t) (f : Ξ± β†’ multiset Ξ²) : bind (s + t) f = bind s f + bind t f := by simp [bind] @[simp] theorem bind_zero (s : multiset Ξ±) : bind s (Ξ»a, 0 : Ξ± β†’ multiset Ξ²) = 0 := by simp [bind, join] @[simp] theorem bind_add (s : multiset Ξ±) (f g : Ξ± β†’ multiset Ξ²) : bind s (Ξ»a, f a + g a) = bind s f + bind s g := by simp [bind, join] @[simp] theorem bind_cons (s : multiset Ξ±) (f : Ξ± β†’ Ξ²) (g : Ξ± β†’ multiset Ξ²) : bind s (Ξ»a, f a :: g a) = map f s + bind s g := multiset.induction_on s (by simp) (by simp [add_comm, add_left_comm] {contextual := tt}) @[simp] theorem mem_bind {b s} {f : Ξ± β†’ multiset Ξ²} : b ∈ bind s f ↔ βˆƒ a ∈ s, b ∈ f a := by simp [bind]; simp [-exists_and_distrib_right, exists_and_distrib_right.symm]; rw exists_swap; simp [and_assoc] @[simp] theorem card_bind (s) (f : Ξ± β†’ multiset Ξ²) : card (bind s f) = sum (map (card ∘ f) s) := by simp [bind] lemma bind_congr {f g : Ξ± β†’ multiset Ξ²} {m : multiset Ξ±} : (βˆ€a∈m, f a = g a) β†’ bind m f = bind m g := by simp [bind] {contextual := tt} lemma bind_hcongr {Ξ²' : Type*} {m : multiset Ξ±} {f : Ξ± β†’ multiset Ξ²} {f' : Ξ± β†’ multiset Ξ²'} (h : Ξ² = Ξ²') (hf : βˆ€a∈m, f a == f' a) : bind m f == bind m f' := begin subst h, simp at hf, simp [bind_congr hf] end lemma map_bind (m : multiset Ξ±) (n : Ξ± β†’ multiset Ξ²) (f : Ξ² β†’ Ξ³) : map f (bind m n) = bind m (Ξ»a, map f (n a)) := multiset.induction_on m (by simp) (by simp {contextual := tt}) lemma bind_map (m : multiset Ξ±) (n : Ξ² β†’ multiset Ξ³) (f : Ξ± β†’ Ξ²) : bind (map f m) n = bind m (Ξ»a, n (f a)) := multiset.induction_on m (by simp) (by simp {contextual := tt}) lemma bind_assoc {s : multiset Ξ±} {f : Ξ± β†’ multiset Ξ²} {g : Ξ² β†’ multiset Ξ³} : (s.bind f).bind g = s.bind (Ξ»a, (f a).bind g) := multiset.induction_on s (by simp) (by simp {contextual := tt}) lemma bind_bind (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ multiset Ξ³} : (bind m $ Ξ»a, bind n $ Ξ»b, f a b) = (bind n $ Ξ»b, bind m $ Ξ»a, f a b) := multiset.induction_on m (by simp) (by simp {contextual := tt}) lemma bind_map_comm (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β†’ Ξ² β†’ Ξ³} : (bind m $ Ξ»a, n.map $ Ξ»b, f a b) = (bind n $ Ξ»b, m.map $ Ξ»a, f a b) := multiset.induction_on m (by simp) (by simp {contextual := tt}) @[simp, to_additive] lemma prod_bind [comm_monoid Ξ²] (s : multiset Ξ±) (t : Ξ± β†’ multiset Ξ²) : prod (bind s t) = prod (s.map $ Ξ»a, prod (t a)) := multiset.induction_on s (by simp) (assume a s ih, by simp [ih, cons_bind]) /- product -/ /-- The multiplicity of `(a, b)` in `product s t` is the product of the multiplicity of `a` in `s` and `b` in `t`. -/ def product (s : multiset Ξ±) (t : multiset Ξ²) : multiset (Ξ± Γ— Ξ²) := s.bind $ Ξ» a, t.map $ prod.mk a @[simp] theorem coe_product (l₁ : list Ξ±) (lβ‚‚ : list Ξ²) : @product Ξ± Ξ² l₁ lβ‚‚ = l₁.product lβ‚‚ := by rw [product, list.product, ← coe_bind]; simp @[simp] theorem zero_product (t) : @product Ξ± Ξ² 0 t = 0 := rfl @[simp] theorem cons_product (a : Ξ±) (s : multiset Ξ±) (t : multiset Ξ²) : product (a :: s) t = map (prod.mk a) t + product s t := by simp [product] @[simp] theorem product_singleton (a : Ξ±) (b : Ξ²) : product (a::0) (b::0) = (a,b)::0 := rfl @[simp] theorem add_product (s t : multiset Ξ±) (u : multiset Ξ²) : product (s + t) u = product s u + product t u := by simp [product] @[simp] theorem product_add (s : multiset Ξ±) : βˆ€ t u : multiset Ξ², product s (t + u) = product s t + product s u := multiset.induction_on s (Ξ» t u, rfl) $ Ξ» a s IH t u, by rw [cons_product, IH]; simp; cc @[simp] theorem mem_product {s t} : βˆ€ {p : Ξ± Γ— Ξ²}, p ∈ @product Ξ± Ξ² s t ↔ p.1 ∈ s ∧ p.2 ∈ t | (a, b) := by simp [product, and.left_comm] @[simp] theorem card_product (s : multiset Ξ±) (t : multiset Ξ²) : card (product s t) = card s * card t := by simp [product, repeat, (∘), mul_comm] /- sigma -/ section variable {Οƒ : Ξ± β†’ Type*} /-- `sigma s t` is the dependent version of `product`. It is the sum of `(a, b)` as `a` ranges over `s` and `b` ranges over `t a`. -/ protected def sigma (s : multiset Ξ±) (t : Ξ  a, multiset (Οƒ a)) : multiset (Ξ£ a, Οƒ a) := s.bind $ Ξ» a, (t a).map $ sigma.mk a @[simp] theorem coe_sigma (l₁ : list Ξ±) (lβ‚‚ : Ξ  a, list (Οƒ a)) : @multiset.sigma Ξ± Οƒ l₁ (Ξ» a, lβ‚‚ a) = l₁.sigma lβ‚‚ := by rw [multiset.sigma, list.sigma, ← coe_bind]; simp @[simp] theorem zero_sigma (t) : @multiset.sigma Ξ± Οƒ 0 t = 0 := rfl @[simp] theorem cons_sigma (a : Ξ±) (s : multiset Ξ±) (t : Ξ  a, multiset (Οƒ a)) : (a :: s).sigma t = map (sigma.mk a) (t a) + s.sigma t := by simp [multiset.sigma] @[simp] theorem sigma_singleton (a : Ξ±) (b : Ξ± β†’ Ξ²) : (a::0).sigma (Ξ» a, b a::0) = ⟨a, b a⟩::0 := rfl @[simp] theorem add_sigma (s t : multiset Ξ±) (u : Ξ  a, multiset (Οƒ a)) : (s + t).sigma u = s.sigma u + t.sigma u := by simp [multiset.sigma] @[simp] theorem sigma_add (s : multiset Ξ±) : βˆ€ t u : Ξ  a, multiset (Οƒ a), s.sigma (Ξ» a, t a + u a) = s.sigma t + s.sigma u := multiset.induction_on s (Ξ» t u, rfl) $ Ξ» a s IH t u, by rw [cons_sigma, IH]; simp; cc @[simp] theorem mem_sigma {s t} : βˆ€ {p : Ξ£ a, Οƒ a}, p ∈ @multiset.sigma Ξ± Οƒ s t ↔ p.1 ∈ s ∧ p.2 ∈ t p.1 | ⟨a, b⟩ := by simp [multiset.sigma, and_assoc, and.left_comm] @[simp] theorem card_sigma (s : multiset Ξ±) (t : Ξ  a, multiset (Οƒ a)) : card (s.sigma t) = sum (map (Ξ» a, card (t a)) s) := by simp [multiset.sigma, (∘)] end /- map for partial functions -/ /-- Lift of the list `pmap` operation. Map a partial function `f` over a multiset `s` whose elements are all in the domain of `f`. -/ def pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (s : multiset Ξ±) : (βˆ€ a ∈ s, p a) β†’ multiset Ξ² := quot.rec_on s (Ξ» l H, ↑(pmap f l H)) $ Ξ» l₁ lβ‚‚ (pp : l₁ ~ lβ‚‚), funext $ Ξ» (Hβ‚‚ : βˆ€ a ∈ lβ‚‚, p a), have H₁ : βˆ€ a ∈ l₁, p a, from Ξ» a h, Hβ‚‚ a (pp.subset h), have βˆ€ {sβ‚‚ e H}, @eq.rec (multiset Ξ±) l₁ (Ξ» s, (βˆ€ a ∈ s, p a) β†’ multiset Ξ²) (Ξ» _, ↑(pmap f l₁ H₁)) sβ‚‚ e H = ↑(pmap f l₁ H₁), by intros sβ‚‚ e _; subst e, this.trans $ quot.sound $ pp.pmap f @[simp] theorem coe_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (l : list Ξ±) (H : βˆ€ a ∈ l, p a) : pmap f l H = l.pmap f H := rfl @[simp] lemma pmap_zero {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (h : βˆ€a∈(0:multiset Ξ±), p a) : pmap f 0 h = 0 := rfl @[simp] lemma pmap_cons {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (a : Ξ±) (m : multiset Ξ±) : βˆ€(h : βˆ€b∈a::m, p b), pmap f (a :: m) h = f a (h a (mem_cons_self a m)) :: pmap f m (Ξ»a ha, h a $ mem_cons_of_mem ha) := quotient.induction_on m $ assume l h, rfl /-- "Attach" a proof that `a ∈ s` to each element `a` in `s` to produce a multiset on `{x // x ∈ s}`. -/ def attach (s : multiset Ξ±) : multiset {x // x ∈ s} := pmap subtype.mk s (Ξ» a, id) @[simp] theorem coe_attach (l : list Ξ±) : @eq (multiset {x // x ∈ l}) (@attach Ξ± l) l.attach := rfl theorem sizeof_lt_sizeof_of_mem [has_sizeof Ξ±] {x : Ξ±} {s : multiset Ξ±} (hx : x ∈ s) : sizeof x < sizeof s := by { induction s with l a b, exact list.sizeof_lt_sizeof_of_mem hx, refl } theorem pmap_eq_map (p : Ξ± β†’ Prop) (f : Ξ± β†’ Ξ²) (s : multiset Ξ±) : βˆ€ H, @pmap _ _ p (Ξ» a _, f a) s H = map f s := quot.induction_on s $ Ξ» l H, congr_arg coe $ pmap_eq_map p f l H theorem pmap_congr {p q : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {g : Ξ  a, q a β†’ Ξ²} (s : multiset Ξ±) {H₁ Hβ‚‚} (h : βˆ€ a h₁ hβ‚‚, f a h₁ = g a hβ‚‚) : pmap f s H₁ = pmap g s Hβ‚‚ := quot.induction_on s (Ξ» l H₁ Hβ‚‚, congr_arg coe $ pmap_congr l h) H₁ Hβ‚‚ theorem map_pmap {p : Ξ± β†’ Prop} (g : Ξ² β†’ Ξ³) (f : Ξ  a, p a β†’ Ξ²) (s) : βˆ€ H, map g (pmap f s H) = pmap (Ξ» a h, g (f a h)) s H := quot.induction_on s $ Ξ» l H, congr_arg coe $ map_pmap g f l H theorem pmap_eq_map_attach {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (s) : βˆ€ H, pmap f s H = s.attach.map (Ξ» x, f x.1 (H _ x.2)) := quot.induction_on s $ Ξ» l H, congr_arg coe $ pmap_eq_map_attach f l H theorem attach_map_val (s : multiset Ξ±) : s.attach.map subtype.val = s := quot.induction_on s $ Ξ» l, congr_arg coe $ attach_map_val l @[simp] theorem mem_attach (s : multiset Ξ±) : βˆ€ x, x ∈ s.attach := quot.induction_on s $ Ξ» l, mem_attach _ @[simp] theorem mem_pmap {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {s H b} : b ∈ pmap f s H ↔ βˆƒ a (h : a ∈ s), f a (H a h) = b := quot.induction_on s (Ξ» l H, mem_pmap) H @[simp] theorem card_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (s H) : card (pmap f s H) = card s := quot.induction_on s (Ξ» l H, length_pmap) H @[simp] theorem card_attach {m : multiset Ξ±} : card (attach m) = card m := card_pmap _ _ _ @[simp] lemma attach_zero : (0 : multiset Ξ±).attach = 0 := rfl lemma attach_cons (a : Ξ±) (m : multiset Ξ±) : (a :: m).attach = ⟨a, mem_cons_self a m⟩ :: (m.attach.map $ Ξ»p, ⟨p.1, mem_cons_of_mem p.2⟩) := quotient.induction_on m $ assume l, congr_arg coe $ congr_arg (list.cons _) $ by rw [list.map_pmap]; exact list.pmap_congr _ (assume a' h₁ hβ‚‚, subtype.eq rfl) section decidable_pi_exists variables {m : multiset Ξ±} protected def decidable_forall_multiset {p : Ξ± β†’ Prop} [hp : βˆ€a, decidable (p a)] : decidable (βˆ€a∈m, p a) := quotient.rec_on_subsingleton m (Ξ»l, decidable_of_iff (βˆ€a∈l, p a) $ by simp) instance decidable_dforall_multiset {p : Ξ a∈m, Prop} [hp : βˆ€a (h : a ∈ m), decidable (p a h)] : decidable (βˆ€a (h : a ∈ m), p a h) := decidable_of_decidable_of_iff (@multiset.decidable_forall_multiset {a // a ∈ m} m.attach (Ξ»a, p a.1 a.2) _) (iff.intro (assume h a ha, h ⟨a, ha⟩ (mem_attach _ _)) (assume h ⟨a, ha⟩ _, h _ _)) /-- decidable equality for functions whose domain is bounded by multisets -/ instance decidable_eq_pi_multiset {Ξ² : Ξ± β†’ Type*} [h : βˆ€a, decidable_eq (Ξ² a)] : decidable_eq (Ξ a∈m, Ξ² a) := assume f g, decidable_of_iff (βˆ€a (h : a ∈ m), f a h = g a h) (by simp [function.funext_iff]) def decidable_exists_multiset {p : Ξ± β†’ Prop} [decidable_pred p] : decidable (βˆƒ x ∈ m, p x) := quotient.rec_on_subsingleton m list.decidable_exists_mem instance decidable_dexists_multiset {p : Ξ a∈m, Prop} [hp : βˆ€a (h : a ∈ m), decidable (p a h)] : decidable (βˆƒa (h : a ∈ m), p a h) := decidable_of_decidable_of_iff (@multiset.decidable_exists_multiset {a // a ∈ m} m.attach (Ξ»a, p a.1 a.2) _) (iff.intro (Ξ» ⟨⟨a, haβ‚βŸ©, _, haβ‚‚βŸ©, ⟨a, ha₁, haβ‚‚βŸ©) (Ξ» ⟨a, ha₁, haβ‚‚βŸ©, ⟨⟨a, haβ‚βŸ©, mem_attach _ _, haβ‚‚βŸ©)) end decidable_pi_exists /- subtraction -/ section variables [decidable_eq Ξ±] {s t u : multiset Ξ±} {a b : Ξ±} /-- `s - t` is the multiset such that `count a (s - t) = count a s - count a t` for all `a`. -/ protected def sub (s t : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s t (Ξ» l₁ lβ‚‚, (l₁.diff lβ‚‚ : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ p₁.diff pβ‚‚ instance : has_sub (multiset Ξ±) := ⟨multiset.sub⟩ @[simp] theorem coe_sub (s t : list Ξ±) : (s - t : multiset Ξ±) = (s.diff t : list Ξ±) := rfl theorem sub_eq_fold_erase (s t : multiset Ξ±) : s - t = foldl erase erase_comm s t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, show ↑(l₁.diff lβ‚‚) = foldl erase erase_comm ↑l₁ ↑lβ‚‚, by { rw diff_eq_foldl l₁ lβ‚‚, symmetry, exact foldl_hom _ _ _ _ _ (Ξ» x y, rfl) } @[simp] theorem sub_zero (s : multiset Ξ±) : s - 0 = s := quot.induction_on s $ Ξ» l, rfl @[simp] theorem sub_cons (a : Ξ±) (s t : multiset Ξ±) : s - a::t = s.erase a - t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ diff_cons _ _ _ theorem add_sub_of_le (h : s ≀ t) : s + (t - s) = t := begin revert t, refine multiset.induction_on s (by simp) (Ξ» a s IH t h, _), have := cons_erase (mem_of_le h (mem_cons_self _ _)), rw [cons_add, sub_cons, IH, this], exact (cons_le_cons_iff a).1 (this.symm β–Έ h) end theorem sub_add' : s - (t + u) = s - t - u := quotient.induction_on₃ s t u $ Ξ» l₁ lβ‚‚ l₃, congr_arg coe $ diff_append _ _ _ theorem sub_add_cancel (h : t ≀ s) : s - t + t = s := by rw [add_comm, add_sub_of_le h] @[simp] theorem add_sub_cancel_left (s : multiset Ξ±) : βˆ€ t, s + t - s = t := multiset.induction_on s (by simp) (Ξ» a s IH t, by rw [cons_add, sub_cons, erase_cons_head, IH]) @[simp] theorem add_sub_cancel (s t : multiset Ξ±) : s + t - t = s := by rw [add_comm, add_sub_cancel_left] theorem sub_le_sub_right (h : s ≀ t) (u) : s - u ≀ t - u := by revert s t h; exact multiset.induction_on u (by simp {contextual := tt}) (Ξ» a u IH s t h, by simp [IH, erase_le_erase a h]) theorem sub_le_sub_left (h : s ≀ t) : βˆ€ u, u - t ≀ u - s := le_induction_on h $ Ξ» l₁ lβ‚‚ h, begin induction h with l₁ lβ‚‚ a s IH l₁ lβ‚‚ a s IH; intro u, { refl }, { rw [← cons_coe, sub_cons], exact le_trans (sub_le_sub_right (erase_le _ _) _) (IH u) }, { rw [← cons_coe, sub_cons, ← cons_coe, sub_cons], exact IH _ } end theorem sub_le_iff_le_add : s - t ≀ u ↔ s ≀ u + t := by revert s; exact multiset.induction_on t (by simp) (Ξ» a t IH s, by simp [IH, erase_le_iff_le_cons]) theorem le_sub_add (s t : multiset Ξ±) : s ≀ s - t + t := sub_le_iff_le_add.1 (le_refl _) theorem sub_le_self (s t : multiset Ξ±) : s - t ≀ s := sub_le_iff_le_add.2 (le_add_right _ _) @[simp] theorem card_sub {s t : multiset Ξ±} (h : t ≀ s) : card (s - t) = card s - card t := (nat.sub_eq_of_eq_add $ by rw [add_comm, ← card_add, sub_add_cancel h]).symm /- union -/ /-- `s βˆͺ t` is the lattice join operation with respect to the multiset `≀`. The multiplicity of `a` in `s βˆͺ t` is the maximum of the multiplicities in `s` and `t`. -/ def union (s t : multiset Ξ±) : multiset Ξ± := s - t + t instance : has_union (multiset Ξ±) := ⟨union⟩ theorem union_def (s t : multiset Ξ±) : s βˆͺ t = s - t + t := rfl theorem le_union_left (s t : multiset Ξ±) : s ≀ s βˆͺ t := le_sub_add _ _ theorem le_union_right (s t : multiset Ξ±) : t ≀ s βˆͺ t := le_add_left _ _ theorem eq_union_left : t ≀ s β†’ s βˆͺ t = s := sub_add_cancel theorem union_le_union_right (h : s ≀ t) (u) : s βˆͺ u ≀ t βˆͺ u := add_le_add_right (sub_le_sub_right h _) u theorem union_le (h₁ : s ≀ u) (hβ‚‚ : t ≀ u) : s βˆͺ t ≀ u := by rw ← eq_union_left hβ‚‚; exact union_le_union_right h₁ t @[simp] theorem mem_union : a ∈ s βˆͺ t ↔ a ∈ s ∨ a ∈ t := ⟨λ h, (mem_add.1 h).imp_left (mem_of_le $ sub_le_self _ _), or.rec (mem_of_le $ le_union_left _ _) (mem_of_le $ le_union_right _ _)⟩ @[simp] theorem map_union [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (finj : function.injective f) {s t : multiset Ξ±} : map f (s βˆͺ t) = map f s βˆͺ map f t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe (by rw [list.map_append f, list.map_diff finj]) /- inter -/ /-- `s ∩ t` is the lattice meet operation with respect to the multiset `≀`. The multiplicity of `a` in `s ∩ t` is the minimum of the multiplicities in `s` and `t`. -/ def inter (s t : multiset Ξ±) : multiset Ξ± := quotient.lift_onβ‚‚ s t (Ξ» l₁ lβ‚‚, (l₁.bag_inter lβ‚‚ : multiset Ξ±)) $ Ξ» v₁ vβ‚‚ w₁ wβ‚‚ p₁ pβ‚‚, quot.sound $ p₁.bag_inter pβ‚‚ instance : has_inter (multiset Ξ±) := ⟨inter⟩ @[simp] theorem inter_zero (s : multiset Ξ±) : s ∩ 0 = 0 := quot.induction_on s $ Ξ» l, congr_arg coe l.bag_inter_nil @[simp] theorem zero_inter (s : multiset Ξ±) : 0 ∩ s = 0 := quot.induction_on s $ Ξ» l, congr_arg coe l.nil_bag_inter @[simp] theorem cons_inter_of_pos {a} (s : multiset Ξ±) {t} : a ∈ t β†’ (a :: s) ∩ t = a :: s ∩ t.erase a := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ cons_bag_inter_of_pos _ h @[simp] theorem cons_inter_of_neg {a} (s : multiset Ξ±) {t} : a βˆ‰ t β†’ (a :: s) ∩ t = s ∩ t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚ h, congr_arg coe $ cons_bag_inter_of_neg _ h theorem inter_le_left (s t : multiset Ξ±) : s ∩ t ≀ s := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, (bag_inter_sublist_left _ _).subperm theorem inter_le_right (s : multiset Ξ±) : βˆ€ t, s ∩ t ≀ t := multiset.induction_on s (Ξ» t, (zero_inter t).symm β–Έ zero_le _) $ Ξ» a s IH t, if h : a ∈ t then by simpa [h] using cons_le_cons a (IH (t.erase a)) else by simp [h, IH] theorem le_inter (h₁ : s ≀ t) (hβ‚‚ : s ≀ u) : s ≀ t ∩ u := begin revert s u, refine multiset.induction_on t _ (Ξ» a t IH, _); intros, { simp [h₁] }, by_cases a ∈ u, { rw [cons_inter_of_pos _ h, ← erase_le_iff_le_cons], exact IH (erase_le_iff_le_cons.2 h₁) (erase_le_erase _ hβ‚‚) }, { rw cons_inter_of_neg _ h, exact IH ((le_cons_of_not_mem $ mt (mem_of_le hβ‚‚) h).1 h₁) hβ‚‚ } end @[simp] theorem mem_inter : a ∈ s ∩ t ↔ a ∈ s ∧ a ∈ t := ⟨λ h, ⟨mem_of_le (inter_le_left _ _) h, mem_of_le (inter_le_right _ _) h⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, by rw [← cons_erase h₁, cons_inter_of_pos _ hβ‚‚]; apply mem_cons_self⟩ instance : lattice (multiset Ξ±) := { sup := (βˆͺ), sup_le := @union_le _ _, le_sup_left := le_union_left, le_sup_right := le_union_right, inf := (∩), le_inf := @le_inter _ _, inf_le_left := inter_le_left, inf_le_right := inter_le_right, ..@multiset.partial_order Ξ± } @[simp] theorem sup_eq_union (s t : multiset Ξ±) : s βŠ” t = s βˆͺ t := rfl @[simp] theorem inf_eq_inter (s t : multiset Ξ±) : s βŠ“ t = s ∩ t := rfl @[simp] theorem le_inter_iff : s ≀ t ∩ u ↔ s ≀ t ∧ s ≀ u := le_inf_iff @[simp] theorem union_le_iff : s βˆͺ t ≀ u ↔ s ≀ u ∧ t ≀ u := sup_le_iff instance : semilattice_inf_bot (multiset Ξ±) := { bot := 0, bot_le := zero_le, ..multiset.lattice } theorem union_comm (s t : multiset Ξ±) : s βˆͺ t = t βˆͺ s := sup_comm theorem inter_comm (s t : multiset Ξ±) : s ∩ t = t ∩ s := inf_comm theorem eq_union_right (h : s ≀ t) : s βˆͺ t = t := by rw [union_comm, eq_union_left h] theorem union_le_union_left (h : s ≀ t) (u) : u βˆͺ s ≀ u βˆͺ t := sup_le_sup_left h _ theorem union_le_add (s t : multiset Ξ±) : s βˆͺ t ≀ s + t := union_le (le_add_right _ _) (le_add_left _ _) theorem union_add_distrib (s t u : multiset Ξ±) : (s βˆͺ t) + u = (s + u) βˆͺ (t + u) := by simpa [(βˆͺ), union, eq_comm, add_assoc] using show s + u - (t + u) = s - t, by rw [add_comm t, sub_add', add_sub_cancel] theorem add_union_distrib (s t u : multiset Ξ±) : s + (t βˆͺ u) = (s + t) βˆͺ (s + u) := by rw [add_comm, union_add_distrib, add_comm s, add_comm s] theorem cons_union_distrib (a : Ξ±) (s t : multiset Ξ±) : a :: (s βˆͺ t) = (a :: s) βˆͺ (a :: t) := by simpa using add_union_distrib (a::0) s t theorem inter_add_distrib (s t u : multiset Ξ±) : (s ∩ t) + u = (s + u) ∩ (t + u) := begin by_contra h, cases lt_iff_cons_le.1 (lt_of_le_of_ne (le_inter (add_le_add_right (inter_le_left s t) u) (add_le_add_right (inter_le_right s t) u)) h) with a hl, rw ← cons_add at hl, exact not_le_of_lt (lt_cons_self (s ∩ t) a) (le_inter (le_of_add_le_add_right (le_trans hl (inter_le_left _ _))) (le_of_add_le_add_right (le_trans hl (inter_le_right _ _)))) end theorem add_inter_distrib (s t u : multiset Ξ±) : s + (t ∩ u) = (s + t) ∩ (s + u) := by rw [add_comm, inter_add_distrib, add_comm s, add_comm s] theorem cons_inter_distrib (a : Ξ±) (s t : multiset Ξ±) : a :: (s ∩ t) = (a :: s) ∩ (a :: t) := by simp theorem union_add_inter (s t : multiset Ξ±) : s βˆͺ t + s ∩ t = s + t := begin apply le_antisymm, { rw union_add_distrib, refine union_le (add_le_add_left (inter_le_right _ _) _) _, rw add_comm, exact add_le_add_right (inter_le_left _ _) _ }, { rw [add_comm, add_inter_distrib], refine le_inter (add_le_add_right (le_union_right _ _) _) _, rw add_comm, exact add_le_add_right (le_union_left _ _) _ } end theorem sub_add_inter (s t : multiset Ξ±) : s - t + s ∩ t = s := begin rw [inter_comm], revert s, refine multiset.induction_on t (by simp) (Ξ» a t IH s, _), by_cases a ∈ s, { rw [cons_inter_of_pos _ h, sub_cons, add_cons, IH, cons_erase h] }, { rw [cons_inter_of_neg _ h, sub_cons, erase_of_not_mem h, IH] } end theorem sub_inter (s t : multiset Ξ±) : s - (s ∩ t) = s - t := add_right_cancel $ by rw [sub_add_inter s t, sub_add_cancel (inter_le_left _ _)] end /- filter -/ section variables {p : Ξ± β†’ Prop} [decidable_pred p] /-- `filter p s` returns the elements in `s` (with the same multiplicities) which satisfy `p`, and removes the rest. -/ def filter (p : Ξ± β†’ Prop) [h : decidable_pred p] (s : multiset Ξ±) : multiset Ξ± := quot.lift_on s (Ξ» l, (filter p l : multiset Ξ±)) (Ξ» l₁ lβ‚‚ h, quot.sound $ h.filter p) @[simp] theorem coe_filter (p : Ξ± β†’ Prop) [h : decidable_pred p] (l : list Ξ±) : filter p (↑l) = l.filter p := rfl @[simp] theorem filter_zero (p : Ξ± β†’ Prop) [h : decidable_pred p] : filter p 0 = 0 := rfl @[simp] theorem filter_cons_of_pos {a : Ξ±} (s) : p a β†’ filter p (a::s) = a :: filter p s := quot.induction_on s $ Ξ» l h, congr_arg coe $ filter_cons_of_pos l h @[simp] theorem filter_cons_of_neg {a : Ξ±} (s) : Β¬ p a β†’ filter p (a::s) = filter p s := quot.induction_on s $ Ξ» l h, @congr_arg _ _ _ _ coe $ filter_cons_of_neg l h lemma filter_congr {p q : Ξ± β†’ Prop} [decidable_pred p] [decidable_pred q] {s : multiset Ξ±} : (βˆ€ x ∈ s, p x ↔ q x) β†’ filter p s = filter q s := quot.induction_on s $ Ξ» l h, congr_arg coe $ filter_congr h @[simp] theorem filter_add (s t : multiset Ξ±) : filter p (s + t) = filter p s + filter p t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, congr_arg coe $ filter_append _ _ @[simp] theorem filter_le (s : multiset Ξ±) : filter p s ≀ s := quot.induction_on s $ Ξ» l, (filter_sublist _).subperm @[simp] theorem filter_subset (s : multiset Ξ±) : filter p s βŠ† s := subset_of_le $ filter_le _ @[simp] theorem mem_filter {a : Ξ±} {s} : a ∈ filter p s ↔ a ∈ s ∧ p a := quot.induction_on s $ Ξ» l, mem_filter theorem of_mem_filter {a : Ξ±} {s} (h : a ∈ filter p s) : p a := (mem_filter.1 h).2 theorem mem_of_mem_filter {a : Ξ±} {s} (h : a ∈ filter p s) : a ∈ s := (mem_filter.1 h).1 theorem mem_filter_of_mem {a : Ξ±} {l} (m : a ∈ l) (h : p a) : a ∈ filter p l := mem_filter.2 ⟨m, h⟩ theorem filter_eq_self {s} : filter p s = s ↔ βˆ€ a ∈ s, p a := quot.induction_on s $ Ξ» l, iff.trans ⟨λ h, eq_of_sublist_of_length_eq (filter_sublist _) (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_self theorem filter_eq_nil {s} : filter p s = 0 ↔ βˆ€ a ∈ s, Β¬p a := quot.induction_on s $ Ξ» l, iff.trans ⟨λ h, eq_nil_of_length_eq_zero (@congr_arg _ _ _ _ card h), congr_arg coe⟩ filter_eq_nil theorem filter_le_filter {s t} (h : s ≀ t) : filter p s ≀ filter p t := le_induction_on h $ Ξ» l₁ lβ‚‚ h, (filter_sublist_filter h).subperm theorem le_filter {s t} : s ≀ filter p t ↔ s ≀ t ∧ βˆ€ a ∈ s, p a := ⟨λ h, ⟨le_trans h (filter_le _), Ξ» a m, of_mem_filter (mem_of_le h m)⟩, Ξ» ⟨h, al⟩, filter_eq_self.2 al β–Έ filter_le_filter h⟩ @[simp] theorem filter_sub [decidable_eq Ξ±] (s t : multiset Ξ±) : filter p (s - t) = filter p s - filter p t := begin revert s, refine multiset.induction_on t (by simp) (Ξ» a t IH s, _), rw [sub_cons, IH], by_cases p a, { rw [filter_cons_of_pos _ h, sub_cons], congr, by_cases m : a ∈ s, { rw [← cons_inj_right a, ← filter_cons_of_pos _ h, cons_erase (mem_filter_of_mem m h), cons_erase m] }, { rw [erase_of_not_mem m, erase_of_not_mem (mt mem_of_mem_filter m)] } }, { rw [filter_cons_of_neg _ h], by_cases m : a ∈ s, { rw [(by rw filter_cons_of_neg _ h : filter p (erase s a) = filter p (a :: erase s a)), cons_erase m] }, { rw [erase_of_not_mem m] } } end @[simp] theorem filter_union [decidable_eq Ξ±] (s t : multiset Ξ±) : filter p (s βˆͺ t) = filter p s βˆͺ filter p t := by simp [(βˆͺ), union] @[simp] theorem filter_inter [decidable_eq Ξ±] (s t : multiset Ξ±) : filter p (s ∩ t) = filter p s ∩ filter p t := le_antisymm (le_inter (filter_le_filter $ inter_le_left _ _) (filter_le_filter $ inter_le_right _ _)) $ le_filter.2 ⟨inf_le_inf (filter_le _) (filter_le _), Ξ» a h, of_mem_filter (mem_of_le (inter_le_left _ _) h)⟩ @[simp] theorem filter_filter {q} [decidable_pred q] (s : multiset Ξ±) : filter p (filter q s) = filter (Ξ» a, p a ∧ q a) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_filter l theorem filter_add_filter {q} [decidable_pred q] (s : multiset Ξ±) : filter p s + filter q s = filter (Ξ» a, p a ∨ q a) s + filter (Ξ» a, p a ∧ q a) s := multiset.induction_on s rfl $ Ξ» a s IH, by by_cases p a; by_cases q a; simp * theorem filter_add_not (s : multiset Ξ±) : filter p s + filter (Ξ» a, Β¬ p a) s = s := by rw [filter_add_filter, filter_eq_self.2, filter_eq_nil.2]; simp [decidable.em] /- filter_map -/ /-- `filter_map f s` is a combination filter/map operation on `s`. The function `f : Ξ± β†’ option Ξ²` is applied to each element of `s`; if `f a` is `some b` then `b` is added to the result, otherwise `a` is removed from the resulting multiset. -/ def filter_map (f : Ξ± β†’ option Ξ²) (s : multiset Ξ±) : multiset Ξ² := quot.lift_on s (Ξ» l, (filter_map f l : multiset Ξ²)) (Ξ» l₁ lβ‚‚ h, quot.sound $ h.filter_map f) @[simp] theorem coe_filter_map (f : Ξ± β†’ option Ξ²) (l : list Ξ±) : filter_map f l = l.filter_map f := rfl @[simp] theorem filter_map_zero (f : Ξ± β†’ option Ξ²) : filter_map f 0 = 0 := rfl @[simp] theorem filter_map_cons_none {f : Ξ± β†’ option Ξ²} (a : Ξ±) (s : multiset Ξ±) (h : f a = none) : filter_map f (a :: s) = filter_map f s := quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ filter_map_cons_none a l h @[simp] theorem filter_map_cons_some (f : Ξ± β†’ option Ξ²) (a : Ξ±) (s : multiset Ξ±) {b : Ξ²} (h : f a = some b) : filter_map f (a :: s) = b :: filter_map f s := quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ filter_map_cons_some f a l h theorem filter_map_eq_map (f : Ξ± β†’ Ξ²) : filter_map (some ∘ f) = map f := funext $ Ξ» s, quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_map f) l theorem filter_map_eq_filter (p : Ξ± β†’ Prop) [decidable_pred p] : filter_map (option.guard p) = filter p := funext $ Ξ» s, quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_filter p) l theorem filter_map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ option Ξ³) (s : multiset Ξ±) : filter_map g (filter_map f s) = filter_map (Ξ» x, (f x).bind g) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_filter_map f g l theorem map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ³) (s : multiset Ξ±) : map g (filter_map f s) = filter_map (Ξ» x, (f x).map g) s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_filter_map f g l theorem filter_map_map (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ option Ξ³) (s : multiset Ξ±) : filter_map g (map f s) = filter_map (g ∘ f) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_map f g l theorem filter_filter_map (f : Ξ± β†’ option Ξ²) (p : Ξ² β†’ Prop) [decidable_pred p] (s : multiset Ξ±) : filter p (filter_map f s) = filter_map (Ξ» x, (f x).filter p) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_filter_map f p l theorem filter_map_filter (p : Ξ± β†’ Prop) [decidable_pred p] (f : Ξ± β†’ option Ξ²) (s : multiset Ξ±) : filter_map f (filter p s) = filter_map (Ξ» x, if p x then f x else none) s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_filter p f l @[simp] theorem filter_map_some (s : multiset Ξ±) : filter_map some s = s := quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_some l @[simp] theorem mem_filter_map (f : Ξ± β†’ option Ξ²) (s : multiset Ξ±) {b : Ξ²} : b ∈ filter_map f s ↔ βˆƒ a, a ∈ s ∧ f a = some b := quot.induction_on s $ Ξ» l, mem_filter_map f l theorem map_filter_map_of_inv (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ±) (H : βˆ€ x : Ξ±, (f x).map g = some x) (s : multiset Ξ±) : map g (filter_map f s) = s := quot.induction_on s $ Ξ» l, congr_arg coe $ map_filter_map_of_inv f g H l theorem filter_map_le_filter_map (f : Ξ± β†’ option Ξ²) {s t : multiset Ξ±} (h : s ≀ t) : filter_map f s ≀ filter_map f t := le_induction_on h $ Ξ» l₁ lβ‚‚ h, (h.filter_map _).subperm /-! ### countp -/ /-- `countp p s` counts the number of elements of `s` (with multiplicity) that satisfy `p`. -/ def countp (p : Ξ± β†’ Prop) [decidable_pred p] (s : multiset Ξ±) : β„• := quot.lift_on s (countp p) (Ξ» l₁ lβ‚‚, perm.countp_eq p) @[simp] theorem coe_countp (l : list Ξ±) : countp p l = l.countp p := rfl @[simp] theorem countp_zero (p : Ξ± β†’ Prop) [decidable_pred p] : countp p 0 = 0 := rfl @[simp] theorem countp_cons_of_pos {a : Ξ±} (s) : p a β†’ countp p (a::s) = countp p s + 1 := quot.induction_on s countp_cons_of_pos @[simp] theorem countp_cons_of_neg {a : Ξ±} (s) : Β¬ p a β†’ countp p (a::s) = countp p s := quot.induction_on s countp_cons_of_neg theorem countp_eq_card_filter (s) : countp p s = card (filter p s) := quot.induction_on s $ Ξ» l, countp_eq_length_filter _ @[simp] theorem countp_add (s t) : countp p (s + t) = countp p s + countp p t := by simp [countp_eq_card_filter] instance countp.is_add_monoid_hom : is_add_monoid_hom (countp p : multiset Ξ± β†’ β„•) := { map_add := countp_add, map_zero := countp_zero _ } theorem countp_pos {s} : 0 < countp p s ↔ βˆƒ a ∈ s, p a := by simp [countp_eq_card_filter, card_pos_iff_exists_mem] @[simp] theorem countp_sub [decidable_eq Ξ±] {s t : multiset Ξ±} (h : t ≀ s) : countp p (s - t) = countp p s - countp p t := by simp [countp_eq_card_filter, h, filter_le_filter] theorem countp_pos_of_mem {s a} (h : a ∈ s) (pa : p a) : 0 < countp p s := countp_pos.2 ⟨_, h, pa⟩ theorem countp_le_of_le {s t} (h : s ≀ t) : countp p s ≀ countp p t := by simpa [countp_eq_card_filter] using card_le_of_le (filter_le_filter h) @[simp] theorem countp_filter {q} [decidable_pred q] (s : multiset Ξ±) : countp p (filter q s) = countp (Ξ» a, p a ∧ q a) s := by simp [countp_eq_card_filter] end /- count -/ section variable [decidable_eq Ξ±] /-- `count a s` is the multiplicity of `a` in `s`. -/ def count (a : Ξ±) : multiset Ξ± β†’ β„• := countp (eq a) @[simp] theorem coe_count (a : Ξ±) (l : list Ξ±) : count a (↑l) = l.count a := coe_countp _ @[simp] theorem count_zero (a : Ξ±) : count a 0 = 0 := rfl @[simp] theorem count_cons_self (a : Ξ±) (s : multiset Ξ±) : count a (a::s) = succ (count a s) := countp_cons_of_pos _ rfl @[simp, priority 990] theorem count_cons_of_ne {a b : Ξ±} (h : a β‰  b) (s : multiset Ξ±) : count a (b::s) = count a s := countp_cons_of_neg _ h theorem count_le_of_le (a : Ξ±) {s t} : s ≀ t β†’ count a s ≀ count a t := countp_le_of_le theorem count_le_count_cons (a b : Ξ±) (s : multiset Ξ±) : count a s ≀ count a (b :: s) := count_le_of_le _ (le_cons_self _ _) theorem count_singleton (a : Ξ±) : count a (a::0) = 1 := by simp @[simp] theorem count_add (a : Ξ±) : βˆ€ s t, count a (s + t) = count a s + count a t := countp_add instance count.is_add_monoid_hom (a : Ξ±) : is_add_monoid_hom (count a : multiset Ξ± β†’ β„•) := countp.is_add_monoid_hom @[simp] theorem count_smul (a : Ξ±) (n s) : count a (n β€’β„• s) = n * count a s := by induction n; simp [*, succ_nsmul', succ_mul] theorem count_pos {a : Ξ±} {s : multiset Ξ±} : 0 < count a s ↔ a ∈ s := by simp [count, countp_pos] @[simp, priority 980] theorem count_eq_zero_of_not_mem {a : Ξ±} {s : multiset Ξ±} (h : a βˆ‰ s) : count a s = 0 := by_contradiction $ Ξ» h', h $ count_pos.1 (nat.pos_of_ne_zero h') theorem count_eq_zero {a : Ξ±} {s : multiset Ξ±} : count a s = 0 ↔ a βˆ‰ s := iff_not_comm.1 $ count_pos.symm.trans pos_iff_ne_zero theorem count_ne_zero {a : Ξ±} {s : multiset Ξ±} : count a s β‰  0 ↔ a ∈ s := by simp [ne.def, count_eq_zero] @[simp] theorem count_repeat (a : Ξ±) (n : β„•) : count a (repeat a n) = n := by simp [repeat] @[simp] theorem count_erase_self (a : Ξ±) (s : multiset Ξ±) : count a (erase s a) = pred (count a s) := begin by_cases a ∈ s, { rw [(by rw cons_erase h : count a s = count a (a::erase s a)), count_cons_self]; refl }, { rw [erase_of_not_mem h, count_eq_zero.2 h]; refl } end @[simp, priority 980] theorem count_erase_of_ne {a b : Ξ±} (ab : a β‰  b) (s : multiset Ξ±) : count a (erase s b) = count a s := begin by_cases b ∈ s, { rw [← count_cons_of_ne ab, cons_erase h] }, { rw [erase_of_not_mem h] } end @[simp] theorem count_sub (a : Ξ±) (s t : multiset Ξ±) : count a (s - t) = count a s - count a t := begin revert s, refine multiset.induction_on t (by simp) (Ξ» b t IH s, _), rw [sub_cons, IH], by_cases ab : a = b, { subst b, rw [count_erase_self, count_cons_self, sub_succ, pred_sub] }, { rw [count_erase_of_ne ab, count_cons_of_ne ab] } end @[simp] theorem count_union (a : Ξ±) (s t : multiset Ξ±) : count a (s βˆͺ t) = max (count a s) (count a t) := by simp [(βˆͺ), union, sub_add_eq_max, -add_comm] @[simp] theorem count_inter (a : Ξ±) (s t : multiset Ξ±) : count a (s ∩ t) = min (count a s) (count a t) := begin apply @nat.add_left_cancel (count a (s - t)), rw [← count_add, sub_add_inter, count_sub, sub_add_min], end lemma count_sum {m : multiset Ξ²} {f : Ξ² β†’ multiset Ξ±} {a : Ξ±} : count a (map f m).sum = sum (m.map $ Ξ»b, count a $ f b) := multiset.induction_on m (by simp) ( by simp) lemma count_bind {m : multiset Ξ²} {f : Ξ² β†’ multiset Ξ±} {a : Ξ±} : count a (bind m f) = sum (m.map $ Ξ»b, count a $ f b) := count_sum theorem le_count_iff_repeat_le {a : Ξ±} {s : multiset Ξ±} {n : β„•} : n ≀ count a s ↔ repeat a n ≀ s := quot.induction_on s $ Ξ» l, le_count_iff_repeat_sublist.trans repeat_le_coe.symm @[simp] theorem count_filter {p} [decidable_pred p] {a} {s : multiset Ξ±} (h : p a) : count a (filter p s) = count a s := quot.induction_on s $ Ξ» l, count_filter h theorem ext {s t : multiset Ξ±} : s = t ↔ βˆ€ a, count a s = count a t := quotient.induction_onβ‚‚ s t $ Ξ» l₁ lβ‚‚, quotient.eq.trans perm_iff_count @[ext] theorem ext' {s t : multiset Ξ±} : (βˆ€ a, count a s = count a t) β†’ s = t := ext.2 @[simp] theorem coe_inter (s t : list Ξ±) : (s ∩ t : multiset Ξ±) = (s.bag_inter t : list Ξ±) := by ext; simp theorem le_iff_count {s t : multiset Ξ±} : s ≀ t ↔ βˆ€ a, count a s ≀ count a t := ⟨λ h a, count_le_of_le a h, Ξ» al, by rw ← (ext.2 (Ξ» a, by simp [max_eq_right (al a)]) : s βˆͺ t = t); apply le_union_left⟩ instance : distrib_lattice (multiset Ξ±) := { le_sup_inf := Ξ» s t u, le_of_eq $ eq.symm $ ext.2 $ Ξ» a, by simp only [max_min_distrib_left, multiset.count_inter, multiset.sup_eq_union, multiset.count_union, multiset.inf_eq_inter], ..multiset.lattice } instance : semilattice_sup_bot (multiset Ξ±) := { bot := 0, bot_le := zero_le, ..multiset.lattice } end /- relator -/ section rel /-- `rel r s t` -- lift the relation `r` between two elements to a relation between `s` and `t`, s.t. there is a one-to-one mapping betweem elements in `s` and `t` following `r`. -/ inductive rel (r : Ξ± β†’ Ξ² β†’ Prop) : multiset Ξ± β†’ multiset Ξ² β†’ Prop | zero : rel 0 0 | cons {a b as bs} : r a b β†’ rel as bs β†’ rel (a :: as) (b :: bs) mk_iff_of_inductive_prop multiset.rel multiset.rel_iff variables {Ξ΄ : Type*} {r : Ξ± β†’ Ξ² β†’ Prop} {p : Ξ³ β†’ Ξ΄ β†’ Prop} private lemma rel_flip_aux {s t} (h : rel r s t) : rel (flip r) t s := rel.rec_on h rel.zero (assume _ _ _ _ hβ‚€ h₁ ih, rel.cons hβ‚€ ih) lemma rel_flip {s t} : rel (flip r) s t ↔ rel r t s := ⟨rel_flip_aux, rel_flip_aux⟩ lemma rel_eq_refl {s : multiset Ξ±} : rel (=) s s := multiset.induction_on s rel.zero (assume a s, rel.cons rfl) lemma rel_eq {s t : multiset Ξ±} : rel (=) s t ↔ s = t := begin split, { assume h, induction h; simp * }, { assume h, subst h, exact rel_eq_refl } end lemma rel.mono {p : Ξ± β†’ Ξ² β†’ Prop} {s t} (h : βˆ€a b, r a b β†’ p a b) (hst : rel r s t) : rel p s t := begin induction hst, case rel.zero { exact rel.zero }, case rel.cons : a b s t hab hst ih { exact ih.cons (h a b hab) } end lemma rel.add {s t u v} (hst : rel r s t) (huv : rel r u v) : rel r (s + u) (t + v) := begin induction hst, case rel.zero { simpa using huv }, case rel.cons : a b s t hab hst ih { simpa using ih.cons hab } end lemma rel_flip_eq {s t : multiset Ξ±} : rel (Ξ»a b, b = a) s t ↔ s = t := show rel (flip (=)) s t ↔ s = t, by rw [rel_flip, rel_eq, eq_comm] @[simp] lemma rel_zero_left {b : multiset Ξ²} : rel r 0 b ↔ b = 0 := by rw [rel_iff]; simp @[simp] lemma rel_zero_right {a : multiset Ξ±} : rel r a 0 ↔ a = 0 := by rw [rel_iff]; simp lemma rel_cons_left {a as bs} : rel r (a :: as) bs ↔ (βˆƒb bs', r a b ∧ rel r as bs' ∧ bs = b :: bs') := begin split, { generalize hm : a :: as = m, assume h, induction h generalizing as, case rel.zero { simp at hm, contradiction }, case rel.cons : a' b as' bs ha'b h ih { rcases cons_eq_cons.1 hm with ⟨eq₁, eqβ‚‚βŸ© | ⟨h, cs, eq₁, eqβ‚‚βŸ©, { subst eq₁, subst eqβ‚‚, exact ⟨b, bs, ha'b, h, rfl⟩ }, { rcases ih eqβ‚‚.symm with ⟨b', bs', h₁, hβ‚‚, eq⟩, exact ⟨b', b::bs', h₁, eq₁.symm β–Έ rel.cons ha'b hβ‚‚, eq.symm β–Έ cons_swap _ _ _⟩ } } }, { exact assume ⟨b, bs', hab, h, eq⟩, eq.symm β–Έ rel.cons hab h } end lemma rel_cons_right {as b bs} : rel r as (b :: bs) ↔ (βˆƒa as', r a b ∧ rel r as' bs ∧ as = a :: as') := begin rw [← rel_flip, rel_cons_left], apply exists_congr, assume a, apply exists_congr, assume as', rw [rel_flip, flip] end lemma rel_add_left {asβ‚€ as₁} : βˆ€{bs}, rel r (asβ‚€ + as₁) bs ↔ (βˆƒbsβ‚€ bs₁, rel r asβ‚€ bsβ‚€ ∧ rel r as₁ bs₁ ∧ bs = bsβ‚€ + bs₁) := multiset.induction_on asβ‚€ (by simp) begin assume a s ih bs, simp only [ih, cons_add, rel_cons_left], split, { assume h, rcases h with ⟨b, bs', hab, h, rfl⟩, rcases h with ⟨bsβ‚€, bs₁, hβ‚€, h₁, rfl⟩, exact ⟨b :: bsβ‚€, bs₁, ⟨b, bsβ‚€, hab, hβ‚€, rfl⟩, h₁, by simp⟩ }, { assume h, rcases h with ⟨bsβ‚€, bs₁, h, h₁, rfl⟩, rcases h with ⟨b, bs, hab, hβ‚€, rfl⟩, exact ⟨b, bs + bs₁, hab, ⟨bs, bs₁, hβ‚€, h₁, rfl⟩, by simp⟩ } end lemma rel_add_right {as bsβ‚€ bs₁} : rel r as (bsβ‚€ + bs₁) ↔ (βˆƒasβ‚€ as₁, rel r asβ‚€ bsβ‚€ ∧ rel r as₁ bs₁ ∧ as = asβ‚€ + as₁) := by rw [← rel_flip, rel_add_left]; simp [rel_flip] lemma rel_map_left {s : multiset Ξ³} {f : Ξ³ β†’ Ξ±} : βˆ€{t}, rel r (s.map f) t ↔ rel (Ξ»a b, r (f a) b) s t := multiset.induction_on s (by simp) (by simp [rel_cons_left] {contextual := tt}) lemma rel_map_right {s : multiset Ξ±} {t : multiset Ξ³} {f : Ξ³ β†’ Ξ²} : rel r s (t.map f) ↔ rel (Ξ»a b, r a (f b)) s t := by rw [← rel_flip, rel_map_left, ← rel_flip]; refl lemma rel_join {s t} (h : rel (rel r) s t) : rel r s.join t.join := begin induction h, case rel.zero { simp }, case rel.cons : a b s t hab hst ih { simpa using hab.add ih } end lemma rel_map {p : Ξ³ β†’ Ξ΄ β†’ Prop} {s t} {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ΄} (h : (r β‡’ p) f g) (hst : rel r s t) : rel p (s.map f) (t.map g) := by rw [rel_map_left, rel_map_right]; exact hst.mono h lemma rel_bind {p : Ξ³ β†’ Ξ΄ β†’ Prop} {s t} {f : Ξ± β†’ multiset Ξ³} {g : Ξ² β†’ multiset Ξ΄} (h : (r β‡’ rel p) f g) (hst : rel r s t) : rel p (s.bind f) (t.bind g) := by apply rel_join; apply rel_map; assumption lemma card_eq_card_of_rel {r : Ξ± β†’ Ξ² β†’ Prop} {s : multiset Ξ±} {t : multiset Ξ²} (h : rel r s t) : card s = card t := by induction h; simp [*] lemma exists_mem_of_rel_of_mem {r : Ξ± β†’ Ξ² β†’ Prop} {s : multiset Ξ±} {t : multiset Ξ²} (h : rel r s t) : βˆ€ {a : Ξ±} (ha : a ∈ s), βˆƒ b ∈ t, r a b := begin induction h with x y s t hxy hst ih, { simp }, { assume a ha, cases mem_cons.1 ha with ha ha, { exact ⟨y, mem_cons_self _ _, ha.symm β–Έ hxy⟩ }, { rcases ih ha with ⟨b, hbt, hab⟩, exact ⟨b, mem_cons.2 (or.inr hbt), hab⟩ } } end end rel section map theorem map_eq_map {f : Ξ± β†’ Ξ²} (hf : function.injective f) {s t : multiset Ξ±} : s.map f = t.map f ↔ s = t := by rw [← rel_eq, ← rel_eq, rel_map_left, rel_map_right]; simp [hf.eq_iff] theorem map_injective {f : Ξ± β†’ Ξ²} (hf : function.injective f) : function.injective (multiset.map f) := assume x y, (map_eq_map hf).1 end map section quot theorem map_mk_eq_map_mk_of_rel {r : Ξ± β†’ Ξ± β†’ Prop} {s t : multiset Ξ±} (hst : s.rel r t) : s.map (quot.mk r) = t.map (quot.mk r) := rel.rec_on hst rfl $ assume a b s t hab hst ih, by simp [ih, quot.sound hab] theorem exists_multiset_eq_map_quot_mk {r : Ξ± β†’ Ξ± β†’ Prop} (s : multiset (quot r)) : βˆƒt:multiset Ξ±, s = t.map (quot.mk r) := multiset.induction_on s ⟨0, rfl⟩ $ assume a s ⟨t, ht⟩, quot.induction_on a $ assume a, ht.symm β–Έ ⟨a::t, (map_cons _ _ _).symm⟩ theorem induction_on_multiset_quot {r : Ξ± β†’ Ξ± β†’ Prop} {p : multiset (quot r) β†’ Prop} (s : multiset (quot r)) : (βˆ€s:multiset Ξ±, p (s.map (quot.mk r))) β†’ p s := match s, exists_multiset_eq_map_quot_mk s with _, ⟨t, rfl⟩ := assume h, h _ end end quot /- disjoint -/ /-- `disjoint s t` means that `s` and `t` have no elements in common. -/ def disjoint (s t : multiset Ξ±) : Prop := βˆ€ ⦃a⦄, a ∈ s β†’ a ∈ t β†’ false @[simp] theorem coe_disjoint (l₁ lβ‚‚ : list Ξ±) : @disjoint Ξ± l₁ lβ‚‚ ↔ l₁.disjoint lβ‚‚ := iff.rfl theorem disjoint.symm {s t : multiset Ξ±} (d : disjoint s t) : disjoint t s | a iβ‚‚ i₁ := d i₁ iβ‚‚ theorem disjoint_comm {s t : multiset Ξ±} : disjoint s t ↔ disjoint t s := ⟨disjoint.symm, disjoint.symm⟩ theorem disjoint_left {s t : multiset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ s β†’ a βˆ‰ t := iff.rfl theorem disjoint_right {s t : multiset Ξ±} : disjoint s t ↔ βˆ€ {a}, a ∈ t β†’ a βˆ‰ s := disjoint_comm theorem disjoint_iff_ne {s t : multiset Ξ±} : disjoint s t ↔ βˆ€ a ∈ s, βˆ€ b ∈ t, a β‰  b := by simp [disjoint_left, imp_not_comm] theorem disjoint_of_subset_left {s t u : multiset Ξ±} (h : s βŠ† u) (d : disjoint u t) : disjoint s t | x m₁ := d (h m₁) theorem disjoint_of_subset_right {s t u : multiset Ξ±} (h : t βŠ† u) (d : disjoint s u) : disjoint s t | x m m₁ := d m (h m₁) theorem disjoint_of_le_left {s t u : multiset Ξ±} (h : s ≀ u) : disjoint u t β†’ disjoint s t := disjoint_of_subset_left (subset_of_le h) theorem disjoint_of_le_right {s t u : multiset Ξ±} (h : t ≀ u) : disjoint s u β†’ disjoint s t := disjoint_of_subset_right (subset_of_le h) @[simp] theorem zero_disjoint (l : multiset Ξ±) : disjoint 0 l | a := (not_mem_nil a).elim @[simp, priority 1100] theorem singleton_disjoint {l : multiset Ξ±} {a : Ξ±} : disjoint (a::0) l ↔ a βˆ‰ l := by simp [disjoint]; refl @[simp, priority 1100] theorem disjoint_singleton {l : multiset Ξ±} {a : Ξ±} : disjoint l (a::0) ↔ a βˆ‰ l := by rw disjoint_comm; simp @[simp] theorem disjoint_add_left {s t u : multiset Ξ±} : disjoint (s + t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_add_right {s t u : multiset Ξ±} : disjoint s (t + u) ↔ disjoint s t ∧ disjoint s u := by rw [disjoint_comm, disjoint_add_left]; tauto @[simp] theorem disjoint_cons_left {a : Ξ±} {s t : multiset Ξ±} : disjoint (a::s) t ↔ a βˆ‰ t ∧ disjoint s t := (@disjoint_add_left _ (a::0) s t).trans $ by simp @[simp] theorem disjoint_cons_right {a : Ξ±} {s t : multiset Ξ±} : disjoint s (a::t) ↔ a βˆ‰ s ∧ disjoint s t := by rw [disjoint_comm, disjoint_cons_left]; tauto theorem inter_eq_zero_iff_disjoint [decidable_eq Ξ±] {s t : multiset Ξ±} : s ∩ t = 0 ↔ disjoint s t := by rw ← subset_zero; simp [subset_iff, disjoint] @[simp] theorem disjoint_union_left [decidable_eq Ξ±] {s t u : multiset Ξ±} : disjoint (s βˆͺ t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right [decidable_eq Ξ±] {s t u : multiset Ξ±} : disjoint s (t βˆͺ u) ↔ disjoint s t ∧ disjoint s u := by simp [disjoint, or_imp_distrib, forall_and_distrib] lemma disjoint_map_map {f : Ξ± β†’ Ξ³} {g : Ξ² β†’ Ξ³} {s : multiset Ξ±} {t : multiset Ξ²} : disjoint (s.map f) (t.map g) ↔ (βˆ€a∈s, βˆ€b∈t, f a β‰  g b) := begin simp [disjoint], split, from assume h a ha b hb eq, h _ ha rfl _ hb eq.symm, from assume h c a ha eq₁ b hb eqβ‚‚, h _ ha _ hb (eqβ‚‚.symm β–Έ eq₁) end /-- `pairwise r m` states that there exists a list of the elements s.t. `r` holds pairwise on this list. -/ def pairwise (r : Ξ± β†’ Ξ± β†’ Prop) (m : multiset Ξ±) : Prop := βˆƒl:list Ξ±, m = l ∧ l.pairwise r lemma pairwise_coe_iff_pairwise {r : Ξ± β†’ Ξ± β†’ Prop} (hr : symmetric r) {l : list Ξ±} : multiset.pairwise r l ↔ l.pairwise r := iff.intro (assume ⟨l', eq, h⟩, ((quotient.exact eq).pairwise_iff hr).2 h) (assume h, ⟨l, rfl, h⟩) end multiset namespace multiset section choose variables (p : Ξ± β†’ Prop) [decidable_pred p] (l : multiset Ξ±) /-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose p l hp` returns that `a`. -/ def choose_x : Ξ  hp : (βˆƒ! a, a ∈ l ∧ p a), { a // a ∈ l ∧ p a } := quotient.rec_on l (Ξ» l' ex_unique, list.choose_x p l' (exists_of_exists_unique ex_unique)) begin intros, funext hp, suffices all_equal : βˆ€ x y : { t // t ∈ b ∧ p t }, x = y, { apply all_equal }, { rintros ⟨x, px⟩ ⟨y, py⟩, rcases hp with ⟨z, ⟨z_mem_l, pz⟩, z_unique⟩, congr, calc x = z : z_unique x px ... = y : (z_unique y py).symm } end def choose (hp : βˆƒ! a, a ∈ l ∧ p a) : Ξ± := choose_x p l hp lemma choose_spec (hp : βˆƒ! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : βˆƒ! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : βˆƒ! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose variable (Ξ±) /-- The equivalence between lists and multisets of a subsingleton type. -/ def subsingleton_equiv [subsingleton Ξ±] : list Ξ± ≃ multiset Ξ± := { to_fun := coe, inv_fun := quot.lift id $ Ξ» (a b : list Ξ±) (h : a ~ b), list.ext_le h.length_eq $ Ξ» n h₁ hβ‚‚, subsingleton.elim _ _, left_inv := Ξ» l, rfl, right_inv := Ξ» m, quot.induction_on m $ Ξ» l, rfl } end multiset @[to_additive] theorem monoid_hom.map_multiset_prod [comm_monoid Ξ±] [comm_monoid Ξ²] (f : Ξ± β†’* Ξ²) (s : multiset Ξ±) : f s.prod = (s.map f).prod := (s.prod_hom f).symm
4eabbdc6a91011f12a0b80405e4a3b17aa61d464
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/data/list/basic.lean
74254ac0d367b7ece8b727172824129f9c5d7908
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
186,311
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import control.monad.basic import data.nat.basic /-! # Basic properties of lists -/ open function nat namespace list universes u v w x variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} attribute [inline] list.head instance : is_left_id (list Ξ±) has_append.append [] := ⟨ nil_append ⟩ instance : is_right_id (list Ξ±) has_append.append [] := ⟨ append_nil ⟩ instance : is_associative (list Ξ±) has_append.append := ⟨ append_assoc ⟩ theorem cons_ne_nil (a : Ξ±) (l : list Ξ±) : a::l β‰  []. theorem cons_ne_self (a : Ξ±) (l : list Ξ±) : a::l β‰  l := mt (congr_arg length) (nat.succ_ne_self _) theorem head_eq_of_cons_eq {h₁ hβ‚‚ : Ξ±} {t₁ tβ‚‚ : list Ξ±} : (h₁::t₁) = (hβ‚‚::tβ‚‚) β†’ h₁ = hβ‚‚ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq) theorem tail_eq_of_cons_eq {h₁ hβ‚‚ : Ξ±} {t₁ tβ‚‚ : list Ξ±} : (h₁::t₁) = (hβ‚‚::tβ‚‚) β†’ t₁ = tβ‚‚ := assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq) @[simp] theorem cons_injective {a : Ξ±} : injective (cons a) := assume l₁ lβ‚‚, assume Pe, tail_eq_of_cons_eq Pe theorem cons_inj (a : Ξ±) {l l' : list Ξ±} : a::l = a::l' ↔ l = l' := cons_injective.eq_iff theorem exists_cons_of_ne_nil {l : list Ξ±} (h : l β‰  nil) : βˆƒ b L, l = b :: L := by { induction l with c l', contradiction, use [c,l'], } /-! ### mem -/ theorem mem_singleton_self (a : Ξ±) : a ∈ [a] := mem_cons_self _ _ theorem eq_of_mem_singleton {a b : Ξ±} : a ∈ [b] β†’ a = b := assume : a ∈ [b], or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, this) (assume : a ∈ [], absurd this (not_mem_nil a)) @[simp] theorem mem_singleton {a b : Ξ±} : a ∈ [b] ↔ a = b := ⟨eq_of_mem_singleton, or.inl⟩ theorem mem_of_mem_cons_of_mem {a b : Ξ±} {l : list Ξ±} : a ∈ b::l β†’ b ∈ l β†’ a ∈ l := assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl) (assume : a = b, begin subst a, exact binl end) (assume : a ∈ l, this) theorem _root_.decidable.list.eq_or_ne_mem_of_mem [decidable_eq Ξ±] {a b : Ξ±} {l : list Ξ±} (h : a ∈ b :: l) : a = b ∨ (a β‰  b ∧ a ∈ l) := decidable.by_cases or.inl $ assume : a β‰  b, h.elim or.inl $ assume h, or.inr ⟨this, h⟩ theorem eq_or_ne_mem_of_mem {a b : Ξ±} {l : list Ξ±} : a ∈ b :: l β†’ a = b ∨ (a β‰  b ∧ a ∈ l) := by classical; exact decidable.list.eq_or_ne_mem_of_mem theorem not_mem_append {a : Ξ±} {s t : list Ξ±} (h₁ : a βˆ‰ s) (hβ‚‚ : a βˆ‰ t) : a βˆ‰ s ++ t := mt mem_append.1 $ not_or_distrib.2 ⟨h₁, hβ‚‚βŸ© theorem ne_nil_of_mem {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : l β‰  [] := by intro e; rw e at h; cases h theorem mem_split {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : βˆƒ s t : list Ξ±, l = s ++ a :: t := begin induction l with b l ih, {cases h}, rcases h with rfl | h, { exact ⟨[], l, rfl⟩ }, { rcases ih h with ⟨s, t, rfl⟩, exact ⟨b::s, t, rfl⟩ } end theorem mem_of_ne_of_mem {a y : Ξ±} {l : list Ξ±} (h₁ : a β‰  y) (hβ‚‚ : a ∈ y :: l) : a ∈ l := or.elim (eq_or_mem_of_mem_cons hβ‚‚) (Ξ»e, absurd e h₁) (Ξ»r, r) theorem ne_of_not_mem_cons {a b : Ξ±} {l : list Ξ±} : a βˆ‰ b::l β†’ a β‰  b := assume nin aeqb, absurd (or.inl aeqb) nin theorem not_mem_of_not_mem_cons {a b : Ξ±} {l : list Ξ±} : a βˆ‰ b::l β†’ a βˆ‰ l := assume nin nainl, absurd (or.inr nainl) nin theorem not_mem_cons_of_ne_of_not_mem {a y : Ξ±} {l : list Ξ±} : a β‰  y β†’ a βˆ‰ l β†’ a βˆ‰ y::l := assume p1 p2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2)) theorem ne_and_not_mem_of_not_mem_cons {a y : Ξ±} {l : list Ξ±} : a βˆ‰ y::l β†’ a β‰  y ∧ a βˆ‰ l := assume p, and.intro (ne_of_not_mem_cons p) (not_mem_of_not_mem_cons p) @[simp] theorem mem_map {f : Ξ± β†’ Ξ²} {b : Ξ²} {l : list Ξ±} : b ∈ map f l ↔ βˆƒ a, a ∈ l ∧ f a = b := begin -- This proof uses no axioms, that's why it's longer that `induction`; simp [...] induction l with a l ihl, { split, { rintro ⟨_⟩ }, { rintro ⟨a, ⟨_⟩, _⟩ } }, { refine (or_congr eq_comm ihl).trans _, split, { rintro (h|⟨c, hcl, h⟩), exacts [⟨a, or.inl rfl, h⟩, ⟨c, or.inr hcl, h⟩] }, { rintro ⟨c, (hc|hc), h⟩, exacts [or.inl $ (congr_arg f hc.symm).trans h, or.inr ⟨c, hc, h⟩] } } end alias mem_map ↔ list.exists_of_mem_map _ theorem mem_map_of_mem (f : Ξ± β†’ Ξ²) {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : f a ∈ map f l := mem_map.2 ⟨a, h, rfl⟩ theorem mem_map_of_injective {f : Ξ± β†’ Ξ²} (H : injective f) {a : Ξ±} {l : list Ξ±} : f a ∈ map f l ↔ a ∈ l := ⟨λ m, let ⟨a', m', e⟩ := exists_of_mem_map m in H e β–Έ m', mem_map_of_mem _⟩ lemma forall_mem_map_iff {f : Ξ± β†’ Ξ²} {l : list Ξ±} {P : Ξ² β†’ Prop} : (βˆ€ i ∈ l.map f, P i) ↔ βˆ€ j ∈ l, P (f j) := begin split, { assume H j hj, exact H (f j) (mem_map_of_mem f hj) }, { assume H i hi, rcases mem_map.1 hi with ⟨j, hj, ji⟩, rw ← ji, exact H j hj } end @[simp] lemma map_eq_nil {f : Ξ± β†’ Ξ²} {l : list Ξ±} : list.map f l = [] ↔ l = [] := ⟨by cases l; simp only [forall_prop_of_true, map, forall_prop_of_false, not_false_iff], Ξ» h, h.symm β–Έ rfl⟩ @[simp] theorem mem_join {a : Ξ±} : βˆ€ {L : list (list Ξ±)}, a ∈ join L ↔ βˆƒ l, l ∈ L ∧ a ∈ l | [] := ⟨false.elim, λ⟨_, h, _⟩, false.elim h⟩ | (c :: L) := by simp only [join, mem_append, @mem_join L, mem_cons_iff, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem exists_of_mem_join {a : Ξ±} {L : list (list Ξ±)} : a ∈ join L β†’ βˆƒ l, l ∈ L ∧ a ∈ l := mem_join.1 theorem mem_join_of_mem {a : Ξ±} {L : list (list Ξ±)} {l} (lL : l ∈ L) (al : a ∈ l) : a ∈ join L := mem_join.2 ⟨l, lL, al⟩ @[simp] theorem mem_bind {b : Ξ²} {l : list Ξ±} {f : Ξ± β†’ list Ξ²} : b ∈ list.bind l f ↔ βˆƒ a ∈ l, b ∈ f a := iff.trans mem_join ⟨λ ⟨l', h1, h2⟩, let ⟨a, al, fa⟩ := exists_of_mem_map h1 in ⟨a, al, fa.symm β–Έ h2⟩, Ξ» ⟨a, al, bfa⟩, ⟨f a, mem_map_of_mem _ al, bfa⟩⟩ theorem exists_of_mem_bind {b : Ξ²} {l : list Ξ±} {f : Ξ± β†’ list Ξ²} : b ∈ list.bind l f β†’ βˆƒ a ∈ l, b ∈ f a := mem_bind.1 theorem mem_bind_of_mem {b : Ξ²} {l : list Ξ±} {f : Ξ± β†’ list Ξ²} {a} (al : a ∈ l) (h : b ∈ f a) : b ∈ list.bind l f := mem_bind.2 ⟨a, al, h⟩ lemma bind_map {g : Ξ± β†’ list Ξ²} {f : Ξ² β†’ Ξ³} : βˆ€(l : list Ξ±), list.map f (l.bind g) = l.bind (Ξ»a, (g a).map f) | [] := rfl | (a::l) := by simp only [cons_bind, map_append, bind_map l] lemma map_bind (g : Ξ² β†’ list Ξ³) (f : Ξ± β†’ Ξ²) : βˆ€ l : list Ξ±, (list.map f l).bind g = l.bind (Ξ» a, g (f a)) | [] := rfl | (a::l) := by simp only [cons_bind, map_cons, map_bind l] /-- If each element of a list can be lifted to some type, then the whole list can be lifted to this type. -/ instance [h : can_lift Ξ± Ξ²] : can_lift (list Ξ±) (list Ξ²) := { coe := list.map h.coe, cond := Ξ» l, βˆ€ x ∈ l, can_lift.cond Ξ² x, prf := Ξ» l H, begin induction l with a l ihl, { exact ⟨[], rfl⟩ }, rcases ihl (Ξ» x hx, H x (or.inr hx)) with ⟨l, rfl⟩, rcases can_lift.prf a (H a (or.inl rfl)) with ⟨a, rfl⟩, exact ⟨a :: l, rfl⟩ end} /-! ### length -/ theorem length_eq_zero {l : list Ξ±} : length l = 0 ↔ l = [] := ⟨eq_nil_of_length_eq_zero, Ξ» h, h.symm β–Έ rfl⟩ @[simp] lemma length_singleton (a : Ξ±) : length [a] = 1 := rfl theorem length_pos_of_mem {a : Ξ±} : βˆ€ {l : list Ξ±}, a ∈ l β†’ 0 < length l | (b::l) _ := zero_lt_succ _ theorem exists_mem_of_length_pos : βˆ€ {l : list Ξ±}, 0 < length l β†’ βˆƒ a, a ∈ l | (b::l) _ := ⟨b, mem_cons_self _ _⟩ theorem length_pos_iff_exists_mem {l : list Ξ±} : 0 < length l ↔ βˆƒ a, a ∈ l := ⟨exists_mem_of_length_pos, Ξ» ⟨a, h⟩, length_pos_of_mem h⟩ theorem ne_nil_of_length_pos {l : list Ξ±} : 0 < length l β†’ l β‰  [] := Ξ» h1 h2, lt_irrefl 0 ((length_eq_zero.2 h2).subst h1) theorem length_pos_of_ne_nil {l : list Ξ±} : l β‰  [] β†’ 0 < length l := Ξ» h, pos_iff_ne_zero.2 $ Ξ» h0, h $ length_eq_zero.1 h0 theorem length_pos_iff_ne_nil {l : list Ξ±} : 0 < length l ↔ l β‰  [] := ⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩ lemma exists_mem_of_ne_nil (l : list Ξ±) (h : l β‰  []) : βˆƒ x, x ∈ l := exists_mem_of_length_pos (length_pos_of_ne_nil h) theorem length_eq_one {l : list Ξ±} : length l = 1 ↔ βˆƒ a, l = [a] := ⟨match l with [a], _ := ⟨a, rfl⟩ end, Ξ» ⟨a, e⟩, e.symm β–Έ rfl⟩ lemma exists_of_length_succ {n} : βˆ€ l : list Ξ±, l.length = n + 1 β†’ βˆƒ h t, l = h :: t | [] H := absurd H.symm $ succ_ne_zero n | (h :: t) H := ⟨h, t, rfl⟩ @[simp] lemma length_injective_iff : injective (list.length : list Ξ± β†’ β„•) ↔ subsingleton Ξ± := begin split, { intro h, refine ⟨λ x y, _⟩, suffices : [x] = [y], { simpa using this }, apply h, refl }, { intros hΞ± l1 l2 hl, induction l1 generalizing l2; cases l2, { refl }, { cases hl }, { cases hl }, congr, exactI subsingleton.elim _ _, apply l1_ih, simpa using hl } end @[simp] lemma length_injective [subsingleton Ξ±] : injective (length : list Ξ± β†’ β„•) := length_injective_iff.mpr $ by apply_instance /-! ### set-theoretic notation of lists -/ lemma empty_eq : (βˆ… : list Ξ±) = [] := by refl lemma singleton_eq (x : Ξ±) : ({x} : list Ξ±) = [x] := rfl lemma insert_neg [decidable_eq Ξ±] {x : Ξ±} {l : list Ξ±} (h : x βˆ‰ l) : has_insert.insert x l = x :: l := if_neg h lemma insert_pos [decidable_eq Ξ±] {x : Ξ±} {l : list Ξ±} (h : x ∈ l) : has_insert.insert x l = l := if_pos h lemma doubleton_eq [decidable_eq Ξ±] {x y : Ξ±} (h : x β‰  y) : ({x, y} : list Ξ±) = [x, y] := by { rw [insert_neg, singleton_eq], rwa [singleton_eq, mem_singleton] } /-! ### bounded quantifiers over lists -/ theorem forall_mem_nil (p : Ξ± β†’ Prop) : βˆ€ x ∈ @nil Ξ±, p x. theorem forall_mem_cons : βˆ€ {p : Ξ± β†’ Prop} {a : Ξ±} {l : list Ξ±}, (βˆ€ x ∈ a :: l, p x) ↔ p a ∧ βˆ€ x ∈ l, p x := ball_cons theorem forall_mem_of_forall_mem_cons {p : Ξ± β†’ Prop} {a : Ξ±} {l : list Ξ±} (h : βˆ€ x ∈ a :: l, p x) : βˆ€ x ∈ l, p x := (forall_mem_cons.1 h).2 theorem forall_mem_singleton {p : Ξ± β†’ Prop} {a : Ξ±} : (βˆ€ x ∈ [a], p x) ↔ p a := by simp only [mem_singleton, forall_eq] theorem forall_mem_append {p : Ξ± β†’ Prop} {l₁ lβ‚‚ : list Ξ±} : (βˆ€ x ∈ l₁ ++ lβ‚‚, p x) ↔ (βˆ€ x ∈ l₁, p x) ∧ (βˆ€ x ∈ lβ‚‚, p x) := by simp only [mem_append, or_imp_distrib, forall_and_distrib] theorem not_exists_mem_nil (p : Ξ± β†’ Prop) : Β¬ βˆƒ x ∈ @nil Ξ±, p x. theorem exists_mem_cons_of {p : Ξ± β†’ Prop} {a : Ξ±} (l : list Ξ±) (h : p a) : βˆƒ x ∈ a :: l, p x := bex.intro a (mem_cons_self _ _) h theorem exists_mem_cons_of_exists {p : Ξ± β†’ Prop} {a : Ξ±} {l : list Ξ±} (h : βˆƒ x ∈ l, p x) : βˆƒ x ∈ a :: l, p x := bex.elim h (Ξ» x xl px, bex.intro x (mem_cons_of_mem _ xl) px) theorem or_exists_of_exists_mem_cons {p : Ξ± β†’ Prop} {a : Ξ±} {l : list Ξ±} (h : βˆƒ x ∈ a :: l, p x) : p a ∨ βˆƒ x ∈ l, p x := bex.elim h (Ξ» x xal px, or.elim (eq_or_mem_of_mem_cons xal) (assume : x = a, begin rw ←this, left, exact px end) (assume : x ∈ l, or.inr (bex.intro x this px))) theorem exists_mem_cons_iff (p : Ξ± β†’ Prop) (a : Ξ±) (l : list Ξ±) : (βˆƒ x ∈ a :: l, p x) ↔ p a ∨ βˆƒ x ∈ l, p x := iff.intro or_exists_of_exists_mem_cons (assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists) /-! ### list subset -/ theorem subset_def {l₁ lβ‚‚ : list Ξ±} : l₁ βŠ† lβ‚‚ ↔ βˆ€ ⦃a : α⦄, a ∈ l₁ β†’ a ∈ lβ‚‚ := iff.rfl theorem subset_append_of_subset_left (l l₁ lβ‚‚ : list Ξ±) : l βŠ† l₁ β†’ l βŠ† l₁++lβ‚‚ := Ξ» s, subset.trans s $ subset_append_left _ _ theorem subset_append_of_subset_right (l l₁ lβ‚‚ : list Ξ±) : l βŠ† lβ‚‚ β†’ l βŠ† l₁++lβ‚‚ := Ξ» s, subset.trans s $ subset_append_right _ _ @[simp] theorem cons_subset {a : Ξ±} {l m : list Ξ±} : a::l βŠ† m ↔ a ∈ m ∧ l βŠ† m := by simp only [subset_def, mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] theorem cons_subset_of_subset_of_mem {a : Ξ±} {l m : list Ξ±} (ainm : a ∈ m) (lsubm : l βŠ† m) : a::l βŠ† m := cons_subset.2 ⟨ainm, lsubm⟩ theorem append_subset_of_subset_of_subset {l₁ lβ‚‚ l : list Ξ±} (l₁subl : l₁ βŠ† l) (lβ‚‚subl : lβ‚‚ βŠ† l) : l₁ ++ lβ‚‚ βŠ† l := Ξ» a h, (mem_append.1 h).elim (@l₁subl _) (@lβ‚‚subl _) @[simp] theorem append_subset_iff {l₁ lβ‚‚ l : list Ξ±} : l₁ ++ lβ‚‚ βŠ† l ↔ l₁ βŠ† l ∧ lβ‚‚ βŠ† l := begin split, { intro h, simp only [subset_def] at *, split; intros; simp* }, { rintro ⟨h1, h2⟩, apply append_subset_of_subset_of_subset h1 h2 } end theorem eq_nil_of_subset_nil : βˆ€ {l : list Ξ±}, l βŠ† [] β†’ l = [] | [] s := rfl | (a::l) s := false.elim $ s $ mem_cons_self a l theorem eq_nil_iff_forall_not_mem {l : list Ξ±} : l = [] ↔ βˆ€ a, a βˆ‰ l := show l = [] ↔ l βŠ† [], from ⟨λ e, e β–Έ subset.refl _, eq_nil_of_subset_nil⟩ theorem map_subset {l₁ lβ‚‚ : list Ξ±} (f : Ξ± β†’ Ξ²) (H : l₁ βŠ† lβ‚‚) : map f l₁ βŠ† map f lβ‚‚ := Ξ» x, by simp only [mem_map, not_and, exists_imp_distrib, and_imp]; exact Ξ» a h e, ⟨a, H h, e⟩ theorem map_subset_iff {l₁ lβ‚‚ : list Ξ±} (f : Ξ± β†’ Ξ²) (h : injective f) : map f l₁ βŠ† map f lβ‚‚ ↔ l₁ βŠ† lβ‚‚ := begin refine ⟨_, map_subset f⟩, intros h2 x hx, rcases mem_map.1 (h2 (mem_map_of_mem f hx)) with ⟨x', hx', hxx'⟩, cases h hxx', exact hx' end /-! ### append -/ lemma append_eq_has_append {L₁ Lβ‚‚ : list Ξ±} : list.append L₁ Lβ‚‚ = L₁ ++ Lβ‚‚ := rfl @[simp] lemma singleton_append {x : Ξ±} {l : list Ξ±} : [x] ++ l = x :: l := rfl theorem append_ne_nil_of_ne_nil_left (s t : list Ξ±) : s β‰  [] β†’ s ++ t β‰  [] := by induction s; intros; contradiction theorem append_ne_nil_of_ne_nil_right (s t : list Ξ±) : t β‰  [] β†’ s ++ t β‰  [] := by induction s; intros; contradiction @[simp] lemma append_eq_nil {p q : list Ξ±} : (p ++ q) = [] ↔ p = [] ∧ q = [] := by cases p; simp only [nil_append, cons_append, eq_self_iff_true, true_and, false_and] @[simp] lemma nil_eq_append_iff {a b : list Ξ±} : [] = a ++ b ↔ a = [] ∧ b = [] := by rw [eq_comm, append_eq_nil] lemma append_eq_cons_iff {a b c : list Ξ±} {x : Ξ±} : a ++ b = x :: c ↔ (a = [] ∧ b = x :: c) ∨ (βˆƒa', a = x :: a' ∧ c = a' ++ b) := by cases a; simp only [and_assoc, @eq_comm _ c, nil_append, cons_append, eq_self_iff_true, true_and, false_and, exists_false, false_or, or_false, exists_and_distrib_left, exists_eq_left'] lemma cons_eq_append_iff {a b c : list Ξ±} {x : Ξ±} : (x :: c : list Ξ±) = a ++ b ↔ (a = [] ∧ b = x :: c) ∨ (βˆƒa', a = x :: a' ∧ c = a' ++ b) := by rw [eq_comm, append_eq_cons_iff] lemma append_eq_append_iff {a b c d : list Ξ±} : a ++ b = c ++ d ↔ (βˆƒa', c = a ++ a' ∧ b = a' ++ d) ∨ (βˆƒc', a = c ++ c' ∧ d = c' ++ b) := begin induction a generalizing c, case nil { rw nil_append, split, { rintro rfl, left, exact ⟨_, rfl, rfl⟩ }, { rintro (⟨a', rfl, rfl⟩ | ⟨a', H, rfl⟩), {refl}, {rw [← append_assoc, ← H], refl} } }, case cons : a as ih { cases c, { simp only [cons_append, nil_append, false_and, exists_false, false_or, exists_eq_left'], exact eq_comm }, { simp only [cons_append, @eq_comm _ a, ih, and_assoc, and_or_distrib_left, exists_and_distrib_left] } } end @[simp] theorem take_append_drop : βˆ€ (n : β„•) (l : list Ξ±), take n l ++ drop n l = l | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := congr_arg (cons x) $ take_append_drop n xs -- TODO(Leo): cleanup proof after arith dec proc theorem append_inj : βˆ€ {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±}, s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚ β†’ length s₁ = length sβ‚‚ β†’ s₁ = sβ‚‚ ∧ t₁ = tβ‚‚ | [] [] t₁ tβ‚‚ h hl := ⟨rfl, h⟩ | (a::s₁) [] t₁ tβ‚‚ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl | [] (b::sβ‚‚) t₁ tβ‚‚ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl.symm | (a::s₁) (b::sβ‚‚) t₁ tβ‚‚ h hl := list.no_confusion h $ Ξ»ab hap, let ⟨e1, e2⟩ := @append_inj s₁ sβ‚‚ t₁ tβ‚‚ hap (succ.inj hl) in by rw [ab, e1, e2]; exact ⟨rfl, rfl⟩ theorem append_inj_right {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length s₁ = length sβ‚‚) : t₁ = tβ‚‚ := (append_inj h hl).right theorem append_inj_left {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length s₁ = length sβ‚‚) : s₁ = sβ‚‚ := (append_inj h hl).left theorem append_inj' {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length t₁ = length tβ‚‚) : s₁ = sβ‚‚ ∧ t₁ = tβ‚‚ := append_inj h $ @nat.add_right_cancel _ (length t₁) _ $ let hap := congr_arg length h in by simp only [length_append] at hap; rwa [← hl] at hap theorem append_inj_right' {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length t₁ = length tβ‚‚) : t₁ = tβ‚‚ := (append_inj' h hl).right theorem append_inj_left' {s₁ sβ‚‚ t₁ tβ‚‚ : list Ξ±} (h : s₁ ++ t₁ = sβ‚‚ ++ tβ‚‚) (hl : length t₁ = length tβ‚‚) : s₁ = sβ‚‚ := (append_inj' h hl).left theorem append_left_cancel {s t₁ tβ‚‚ : list Ξ±} (h : s ++ t₁ = s ++ tβ‚‚) : t₁ = tβ‚‚ := append_inj_right h rfl theorem append_right_cancel {s₁ sβ‚‚ t : list Ξ±} (h : s₁ ++ t = sβ‚‚ ++ t) : s₁ = sβ‚‚ := append_inj_left' h rfl theorem append_right_injective (s : list Ξ±) : function.injective (Ξ» t, s ++ t) := Ξ» t₁ tβ‚‚, append_left_cancel theorem append_right_inj {t₁ tβ‚‚ : list Ξ±} (s) : s ++ t₁ = s ++ tβ‚‚ ↔ t₁ = tβ‚‚ := (append_right_injective s).eq_iff theorem append_left_injective (t : list Ξ±) : function.injective (Ξ» s, s ++ t) := Ξ» s₁ sβ‚‚, append_right_cancel theorem append_left_inj {s₁ sβ‚‚ : list Ξ±} (t) : s₁ ++ t = sβ‚‚ ++ t ↔ s₁ = sβ‚‚ := (append_left_injective t).eq_iff theorem map_eq_append_split {f : Ξ± β†’ Ξ²} {l : list Ξ±} {s₁ sβ‚‚ : list Ξ²} (h : map f l = s₁ ++ sβ‚‚) : βˆƒ l₁ lβ‚‚, l = l₁ ++ lβ‚‚ ∧ map f l₁ = s₁ ∧ map f lβ‚‚ = sβ‚‚ := begin have := h, rw [← take_append_drop (length s₁) l] at this ⊒, rw map_append at this, refine ⟨_, _, rfl, append_inj this _⟩, rw [length_map, length_take, min_eq_left], rw [← length_map f l, h, length_append], apply nat.le_add_right end /-! ### repeat -/ @[simp] theorem repeat_succ (a : Ξ±) (n) : repeat a (n + 1) = a :: repeat a n := rfl theorem mem_repeat {a b : Ξ±} : βˆ€ {n}, b ∈ repeat a n ↔ n β‰  0 ∧ b = a | 0 := by simp | (n + 1) := by simp [mem_repeat] theorem eq_of_mem_repeat {a b : Ξ±} {n} (h : b ∈ repeat a n) : b = a := (mem_repeat.1 h).2 theorem eq_repeat_of_mem {a : Ξ±} : βˆ€ {l : list Ξ±}, (βˆ€ b ∈ l, b = a) β†’ l = repeat a l.length | [] H := rfl | (b::l) H := by cases forall_mem_cons.1 H with H₁ Hβ‚‚; unfold length repeat; congr; [exact H₁, exact eq_repeat_of_mem Hβ‚‚] theorem eq_repeat' {a : Ξ±} {l : list Ξ±} : l = repeat a l.length ↔ βˆ€ b ∈ l, b = a := ⟨λ h, h.symm β–Έ Ξ» b, eq_of_mem_repeat, eq_repeat_of_mem⟩ theorem eq_repeat {a : Ξ±} {n} {l : list Ξ±} : l = repeat a n ↔ length l = n ∧ βˆ€ b ∈ l, b = a := ⟨λ h, h.symm β–Έ ⟨length_repeat _ _, Ξ» b, eq_of_mem_repeat⟩, Ξ» ⟨e, al⟩, e β–Έ eq_repeat_of_mem al⟩ theorem repeat_add (a : Ξ±) (m n) : repeat a (m + n) = repeat a m ++ repeat a n := by induction m; simp only [*, zero_add, succ_add, repeat]; split; refl theorem repeat_subset_singleton (a : Ξ±) (n) : repeat a n βŠ† [a] := Ξ» b h, mem_singleton.2 (eq_of_mem_repeat h) @[simp] theorem map_const (l : list Ξ±) (b : Ξ²) : map (function.const Ξ± b) l = repeat b l.length := by induction l; [refl, simp only [*, map]]; split; refl theorem eq_of_mem_map_const {b₁ bβ‚‚ : Ξ²} {l : list Ξ±} (h : b₁ ∈ map (function.const Ξ± bβ‚‚) l) : b₁ = bβ‚‚ := by rw map_const at h; exact eq_of_mem_repeat h @[simp] theorem map_repeat (f : Ξ± β†’ Ξ²) (a : Ξ±) (n) : map f (repeat a n) = repeat (f a) n := by induction n; [refl, simp only [*, repeat, map]]; split; refl @[simp] theorem tail_repeat (a : Ξ±) (n) : tail (repeat a n) = repeat a n.pred := by cases n; refl @[simp] theorem join_repeat_nil (n : β„•) : join (repeat [] n) = @nil Ξ± := by induction n; [refl, simp only [*, repeat, join, append_nil]] lemma repeat_left_injective {n : β„•} (hn : n β‰  0) : function.injective (Ξ» a : Ξ±, repeat a n) := Ξ» a b h, (eq_repeat.1 h).2 _ $ mem_repeat.2 ⟨hn, rfl⟩ lemma repeat_left_inj {a b : Ξ±} {n : β„•} (hn : n β‰  0) : repeat a n = repeat b n ↔ a = b := (repeat_left_injective hn).eq_iff @[simp] lemma repeat_left_inj' {a b : Ξ±} : βˆ€ {n}, repeat a n = repeat b n ↔ n = 0 ∨ a = b | 0 := by simp | (n + 1) := (repeat_left_inj n.succ_ne_zero).trans $ by simp only [n.succ_ne_zero, false_or] lemma repeat_right_injective (a : Ξ±) : function.injective (repeat a) := function.left_inverse.injective (length_repeat a) @[simp] lemma repeat_right_inj {a : Ξ±} {n m : β„•} : repeat a n = repeat a m ↔ n = m := (repeat_right_injective a).eq_iff /-! ### pure -/ @[simp] theorem mem_pure {Ξ±} (x y : Ξ±) : x ∈ (pure y : list Ξ±) ↔ x = y := by simp! [pure,list.ret] /-! ### bind -/ @[simp] theorem bind_eq_bind {Ξ± Ξ²} (f : Ξ± β†’ list Ξ²) (l : list Ξ±) : l >>= f = l.bind f := rfl -- TODO: duplicate of a lemma in core theorem bind_append (f : Ξ± β†’ list Ξ²) (l₁ lβ‚‚ : list Ξ±) : (l₁ ++ lβ‚‚).bind f = l₁.bind f ++ lβ‚‚.bind f := append_bind _ _ _ @[simp] theorem bind_singleton (f : Ξ± β†’ list Ξ²) (x : Ξ±) : [x].bind f = f x := append_nil (f x) @[simp] theorem bind_singleton' (l : list Ξ±) : l.bind (Ξ» x, [x]) = l := bind_pure l theorem map_eq_bind {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (l : list Ξ±) : map f l = l.bind (Ξ» x, [f x]) := by { transitivity, rw [← bind_singleton' l, bind_map], refl } theorem bind_assoc {Ξ± Ξ²} (l : list Ξ±) (f : Ξ± β†’ list Ξ²) (g : Ξ² β†’ list Ξ³) : (l.bind f).bind g = l.bind (Ξ» x, (f x).bind g) := by induction l; simp * /-! ### concat -/ theorem concat_nil (a : Ξ±) : concat [] a = [a] := rfl theorem concat_cons (a b : Ξ±) (l : list Ξ±) : concat (a :: l) b = a :: concat l b := rfl @[simp] theorem concat_eq_append (a : Ξ±) (l : list Ξ±) : concat l a = l ++ [a] := by induction l; simp only [*, concat]; split; refl theorem init_eq_of_concat_eq {a : Ξ±} {l₁ lβ‚‚ : list Ξ±} : concat l₁ a = concat lβ‚‚ a β†’ l₁ = lβ‚‚ := begin intro h, rw [concat_eq_append, concat_eq_append] at h, exact append_right_cancel h end theorem last_eq_of_concat_eq {a b : Ξ±} {l : list Ξ±} : concat l a = concat l b β†’ a = b := begin intro h, rw [concat_eq_append, concat_eq_append] at h, exact head_eq_of_cons_eq (append_left_cancel h) end theorem concat_ne_nil (a : Ξ±) (l : list Ξ±) : concat l a β‰  [] := by simp theorem concat_append (a : Ξ±) (l₁ lβ‚‚ : list Ξ±) : concat l₁ a ++ lβ‚‚ = l₁ ++ a :: lβ‚‚ := by simp theorem length_concat (a : Ξ±) (l : list Ξ±) : length (concat l a) = succ (length l) := by simp only [concat_eq_append, length_append, length] theorem append_concat (a : Ξ±) (l₁ lβ‚‚ : list Ξ±) : l₁ ++ concat lβ‚‚ a = concat (l₁ ++ lβ‚‚) a := by simp /-! ### reverse -/ @[simp] theorem reverse_nil : reverse (@nil Ξ±) = [] := rfl local attribute [simp] reverse_core @[simp] theorem reverse_cons (a : Ξ±) (l : list Ξ±) : reverse (a::l) = reverse l ++ [a] := have aux : βˆ€ l₁ lβ‚‚, reverse_core l₁ lβ‚‚ ++ [a] = reverse_core l₁ (lβ‚‚ ++ [a]), by intro l₁; induction l₁; intros; [refl, simp only [*, reverse_core, cons_append]], (aux l nil).symm theorem reverse_core_eq (l₁ lβ‚‚ : list Ξ±) : reverse_core l₁ lβ‚‚ = reverse l₁ ++ lβ‚‚ := by induction l₁ generalizing lβ‚‚; [refl, simp only [*, reverse_core, reverse_cons, append_assoc]]; refl theorem reverse_cons' (a : Ξ±) (l : list Ξ±) : reverse (a::l) = concat (reverse l) a := by simp only [reverse_cons, concat_eq_append] @[simp] theorem reverse_singleton (a : Ξ±) : reverse [a] = [a] := rfl @[simp] theorem reverse_append (s t : list Ξ±) : reverse (s ++ t) = (reverse t) ++ (reverse s) := by induction s; [rw [nil_append, reverse_nil, append_nil], simp only [*, cons_append, reverse_cons, append_assoc]] theorem reverse_concat (l : list Ξ±) (a : Ξ±) : reverse (concat l a) = a :: reverse l := by rw [concat_eq_append, reverse_append, reverse_singleton, singleton_append] @[simp] theorem reverse_reverse (l : list Ξ±) : reverse (reverse l) = l := by induction l; [refl, simp only [*, reverse_cons, reverse_append]]; refl @[simp] theorem reverse_involutive : involutive (@reverse Ξ±) := Ξ» l, reverse_reverse l @[simp] theorem reverse_injective : injective (@reverse Ξ±) := reverse_involutive.injective @[simp] theorem reverse_inj {l₁ lβ‚‚ : list Ξ±} : reverse l₁ = reverse lβ‚‚ ↔ l₁ = lβ‚‚ := reverse_injective.eq_iff lemma reverse_eq_iff {l l' : list Ξ±} : l.reverse = l' ↔ l = l'.reverse := reverse_involutive.eq_iff @[simp] theorem reverse_eq_nil {l : list Ξ±} : reverse l = [] ↔ l = [] := @reverse_inj _ l [] theorem concat_eq_reverse_cons (a : Ξ±) (l : list Ξ±) : concat l a = reverse (a :: reverse l) := by simp only [concat_eq_append, reverse_cons, reverse_reverse] @[simp] theorem length_reverse (l : list Ξ±) : length (reverse l) = length l := by induction l; [refl, simp only [*, reverse_cons, length_append, length]] @[simp] theorem map_reverse (f : Ξ± β†’ Ξ²) (l : list Ξ±) : map f (reverse l) = reverse (map f l) := by induction l; [refl, simp only [*, map, reverse_cons, map_append]] theorem map_reverse_core (f : Ξ± β†’ Ξ²) (l₁ lβ‚‚ : list Ξ±) : map f (reverse_core l₁ lβ‚‚) = reverse_core (map f l₁) (map f lβ‚‚) := by simp only [reverse_core_eq, map_append, map_reverse] @[simp] theorem mem_reverse {a : Ξ±} {l : list Ξ±} : a ∈ reverse l ↔ a ∈ l := by induction l; [refl, simp only [*, reverse_cons, mem_append, mem_singleton, mem_cons_iff, not_mem_nil, false_or, or_false, or_comm]] @[simp] theorem reverse_repeat (a : Ξ±) (n) : reverse (repeat a n) = repeat a n := eq_repeat.2 ⟨by simp only [length_reverse, length_repeat], Ξ» b h, eq_of_mem_repeat (mem_reverse.1 h)⟩ /-! ### empty -/ attribute [simp] list.empty lemma empty_iff_eq_nil {l : list Ξ±} : l.empty ↔ l = [] := list.cases_on l (by simp) (by simp) /-! ### init -/ @[simp] theorem length_init : βˆ€ (l : list Ξ±), length (init l) = length l - 1 | [] := rfl | [a] := rfl | (a :: b :: l) := begin rw init, simp only [add_left_inj, length, succ_add_sub_one], exact length_init (b :: l) end /-! ### last -/ @[simp] theorem last_cons {a : Ξ±} {l : list Ξ±} : βˆ€ (h₁ : a :: l β‰  nil) (hβ‚‚ : l β‰  nil), last (a :: l) h₁ = last l hβ‚‚ := by {induction l; intros, contradiction, reflexivity} @[simp] theorem last_append {a : Ξ±} (l : list Ξ±) (h : l ++ [a] β‰  []) : last (l ++ [a]) h = a := by induction l; [refl, simp only [cons_append, last_cons _ (Ξ» H, cons_ne_nil _ _ (append_eq_nil.1 H).2), *]] theorem last_concat {a : Ξ±} (l : list Ξ±) (h : concat l a β‰  []) : last (concat l a) h = a := by simp only [concat_eq_append, last_append] @[simp] theorem last_singleton (a : Ξ±) (h : [a] β‰  []) : last [a] h = a := rfl @[simp] theorem last_cons_cons (a₁ aβ‚‚ : Ξ±) (l : list Ξ±) (h : a₁::aβ‚‚::l β‰  []) : last (a₁::aβ‚‚::l) h = last (aβ‚‚::l) (cons_ne_nil aβ‚‚ l) := rfl theorem init_append_last : βˆ€ {l : list Ξ±} (h : l β‰  []), init l ++ [last l h] = l | [] h := absurd rfl h | [a] h := rfl | (a::b::l) h := begin rw [init, cons_append, last_cons (cons_ne_nil _ _) (cons_ne_nil _ _)], congr, exact init_append_last (cons_ne_nil b l) end theorem last_congr {l₁ lβ‚‚ : list Ξ±} (h₁ : l₁ β‰  []) (hβ‚‚ : lβ‚‚ β‰  []) (h₃ : l₁ = lβ‚‚) : last l₁ h₁ = last lβ‚‚ hβ‚‚ := by subst l₁ theorem last_mem : βˆ€ {l : list Ξ±} (h : l β‰  []), last l h ∈ l | [] h := absurd rfl h | [a] h := or.inl rfl | (a::b::l) h := or.inr $ by { rw [last_cons_cons], exact last_mem (cons_ne_nil b l) } lemma last_repeat_succ (a m : β„•) : (repeat a m.succ).last (ne_nil_of_length_eq_succ (show (repeat a m.succ).length = m.succ, by rw length_repeat)) = a := begin induction m with k IH, { simp }, { simpa only [repeat_succ, last] } end /-! ### last' -/ @[simp] theorem last'_is_none : βˆ€ {l : list Ξ±}, (last' l).is_none ↔ l = [] | [] := by simp | [a] := by simp | (a::b::l) := by simp [@last'_is_none (b::l)] @[simp] theorem last'_is_some : βˆ€ {l : list Ξ±}, l.last'.is_some ↔ l β‰  [] | [] := by simp | [a] := by simp | (a::b::l) := by simp [@last'_is_some (b::l)] theorem mem_last'_eq_last : βˆ€ {l : list Ξ±} {x : Ξ±}, x ∈ l.last' β†’ βˆƒ h, x = last l h | [] x hx := false.elim $ by simpa using hx | [a] x hx := have a = x, by simpa using hx, this β–Έ ⟨cons_ne_nil a [], rfl⟩ | (a::b::l) x hx := begin rw last' at hx, rcases mem_last'_eq_last hx with ⟨h₁, hβ‚‚βŸ©, use cons_ne_nil _ _, rwa [last_cons] end theorem mem_of_mem_last' {l : list Ξ±} {a : Ξ±} (ha : a ∈ l.last') : a ∈ l := let ⟨h₁, hβ‚‚βŸ© := mem_last'_eq_last ha in hβ‚‚.symm β–Έ last_mem _ theorem init_append_last' : βˆ€ {l : list Ξ±} (a ∈ l.last'), init l ++ [a] = l | [] a ha := (option.not_mem_none a ha).elim | [a] _ rfl := rfl | (a :: b :: l) c hc := by { rw [last'] at hc, rw [init, cons_append, init_append_last' _ hc] } theorem ilast_eq_last' [inhabited Ξ±] : βˆ€ l : list Ξ±, l.ilast = l.last'.iget | [] := by simp [ilast, arbitrary] | [a] := rfl | [a, b] := rfl | [a, b, c] := rfl | (a :: b :: c :: l) := by simp [ilast, ilast_eq_last' (c :: l)] @[simp] theorem last'_append_cons : βˆ€ (l₁ : list Ξ±) (a : Ξ±) (lβ‚‚ : list Ξ±), last' (l₁ ++ a :: lβ‚‚) = last' (a :: lβ‚‚) | [] a lβ‚‚ := rfl | [b] a lβ‚‚ := rfl | (b::c::l₁) a lβ‚‚ := by rw [cons_append, cons_append, last', ← cons_append, last'_append_cons] theorem last'_append_of_ne_nil (l₁ : list Ξ±) : βˆ€ {lβ‚‚ : list Ξ±} (hlβ‚‚ : lβ‚‚ β‰  []), last' (l₁ ++ lβ‚‚) = last' lβ‚‚ | [] hlβ‚‚ := by contradiction | (b::lβ‚‚) _ := last'_append_cons l₁ b lβ‚‚ /-! ### head(') and tail -/ theorem head_eq_head' [inhabited Ξ±] (l : list Ξ±) : head l = (head' l).iget := by cases l; refl theorem mem_of_mem_head' {x : Ξ±} : βˆ€ {l : list Ξ±}, x ∈ l.head' β†’ x ∈ l | [] h := (option.not_mem_none _ h).elim | (a::l) h := by { simp only [head', option.mem_def] at h, exact h β–Έ or.inl rfl } @[simp] theorem head_cons [inhabited Ξ±] (a : Ξ±) (l : list Ξ±) : head (a::l) = a := rfl @[simp] theorem tail_nil : tail (@nil Ξ±) = [] := rfl @[simp] theorem tail_cons (a : Ξ±) (l : list Ξ±) : tail (a::l) = l := rfl @[simp] theorem head_append [inhabited Ξ±] (t : list Ξ±) {s : list Ξ±} (h : s β‰  []) : head (s ++ t) = head s := by {induction s, contradiction, refl} theorem tail_append_singleton_of_ne_nil {a : Ξ±} {l : list Ξ±} (h : l β‰  nil) : tail (l ++ [a]) = tail l ++ [a] := by { induction l, contradiction, rw [tail,cons_append,tail], } theorem cons_head'_tail : βˆ€ {l : list Ξ±} {a : Ξ±} (h : a ∈ head' l), a :: tail l = l | [] a h := by contradiction | (b::l) a h := by { simp at h, simp [h] } theorem head_mem_head' [inhabited Ξ±] : βˆ€ {l : list Ξ±} (h : l β‰  []), head l ∈ head' l | [] h := by contradiction | (a::l) h := rfl theorem cons_head_tail [inhabited Ξ±] {l : list Ξ±} (h : l β‰  []) : (head l)::(tail l) = l := cons_head'_tail (head_mem_head' h) lemma head_mem_self [inhabited Ξ±] {l : list Ξ±} (h : l β‰  nil) : l.head ∈ l := begin have h' := mem_cons_self l.head l.tail, rwa cons_head_tail h at h', end @[simp] theorem head'_map (f : Ξ± β†’ Ξ²) (l) : head' (map f l) = (head' l).map f := by cases l; refl lemma tail_append_of_ne_nil (l l' : list Ξ±) (h : l β‰  []) : (l ++ l').tail = l.tail ++ l' := begin cases l, { contradiction }, { simp } end @[simp] lemma nth_le_tail (l : list Ξ±) (i) (h : i < l.tail.length) (h' : i + 1 < l.length := by simpa [←lt_tsub_iff_right] using h) : l.tail.nth_le i h = l.nth_le (i + 1) h' := begin cases l, { cases h, }, { simpa } end /-! ### Induction from the right -/ /-- Induction principle from the right for lists: if a property holds for the empty list, and for `l ++ [a]` if it holds for `l`, then it holds for all lists. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ @[elab_as_eliminator] def reverse_rec_on {C : list Ξ± β†’ Sort*} (l : list Ξ±) (H0 : C []) (H1 : βˆ€ (l : list Ξ±) (a : Ξ±), C l β†’ C (l ++ [a])) : C l := begin rw ← reverse_reverse l, induction reverse l, { exact H0 }, { rw reverse_cons, exact H1 _ _ ih } end /-- Bidirectional induction principle for lists: if a property holds for the empty list, the singleton list, and `a :: (l ++ [b])` from `l`, then it holds for all lists. This can be used to prove statements about palindromes. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def bidirectional_rec {C : list Ξ± β†’ Sort*} (H0 : C []) (H1 : βˆ€ (a : Ξ±), C [a]) (Hn : βˆ€ (a : Ξ±) (l : list Ξ±) (b : Ξ±), C l β†’ C (a :: (l ++ [b]))) : βˆ€ l, C l | [] := H0 | [a] := H1 a | (a :: b :: l) := let l' := init (b :: l), b' := last (b :: l) (cons_ne_nil _ _) in have length l' < length (a :: b :: l), by { change _ < length l + 2, simp }, begin rw ←init_append_last (cons_ne_nil b l), have : C l', from bidirectional_rec l', exact Hn a l' b' β€ΉC l'β€Ί end using_well_founded { rel_tac := Ξ» _ _, `[exact ⟨_, measure_wf list.length⟩] } /-- Like `bidirectional_rec`, but with the list parameter placed first. -/ @[elab_as_eliminator] def bidirectional_rec_on {C : list Ξ± β†’ Sort*} (l : list Ξ±) (H0 : C []) (H1 : βˆ€ (a : Ξ±), C [a]) (Hn : βˆ€ (a : Ξ±) (l : list Ξ±) (b : Ξ±), C l β†’ C (a :: (l ++ [b]))) : C l := bidirectional_rec H0 H1 Hn l /-! ### sublists -/ @[simp] theorem nil_sublist : Ξ  (l : list Ξ±), [] <+ l | [] := sublist.slnil | (a :: l) := sublist.cons _ _ a (nil_sublist l) @[refl, simp] theorem sublist.refl : Ξ  (l : list Ξ±), l <+ l | [] := sublist.slnil | (a :: l) := sublist.cons2 _ _ a (sublist.refl l) @[trans] theorem sublist.trans {l₁ lβ‚‚ l₃ : list Ξ±} (h₁ : l₁ <+ lβ‚‚) (hβ‚‚ : lβ‚‚ <+ l₃) : l₁ <+ l₃ := sublist.rec_on hβ‚‚ (Ξ»_ s, s) (Ξ»lβ‚‚ l₃ a hβ‚‚ IH l₁ h₁, sublist.cons _ _ _ (IH l₁ h₁)) (Ξ»lβ‚‚ l₃ a hβ‚‚ IH l₁ h₁, @sublist.cases_on _ (Ξ»l₁ lβ‚‚', lβ‚‚' = a :: lβ‚‚ β†’ l₁ <+ a :: l₃) _ _ h₁ (Ξ»_, nil_sublist _) (Ξ»l₁ lβ‚‚' a' h₁' e, match a', lβ‚‚', e, h₁' with ._, ._, rfl, h₁ := sublist.cons _ _ _ (IH _ h₁) end) (Ξ»l₁ lβ‚‚' a' h₁' e, match a', lβ‚‚', e, h₁' with ._, ._, rfl, h₁ := sublist.cons2 _ _ _ (IH _ h₁) end) rfl) l₁ h₁ @[simp] theorem sublist_cons (a : Ξ±) (l : list Ξ±) : l <+ a::l := sublist.cons _ _ _ (sublist.refl l) theorem sublist_of_cons_sublist {a : Ξ±} {l₁ lβ‚‚ : list Ξ±} : a::l₁ <+ lβ‚‚ β†’ l₁ <+ lβ‚‚ := sublist.trans (sublist_cons a l₁) theorem cons_sublist_cons {l₁ lβ‚‚ : list Ξ±} (a : Ξ±) (s : l₁ <+ lβ‚‚) : a::l₁ <+ a::lβ‚‚ := sublist.cons2 _ _ _ s @[simp] theorem sublist_append_left : Ξ  (l₁ lβ‚‚ : list Ξ±), l₁ <+ l₁++lβ‚‚ | [] lβ‚‚ := nil_sublist _ | (a::l₁) lβ‚‚ := cons_sublist_cons _ (sublist_append_left l₁ lβ‚‚) @[simp] theorem sublist_append_right : Ξ  (l₁ lβ‚‚ : list Ξ±), lβ‚‚ <+ l₁++lβ‚‚ | [] lβ‚‚ := sublist.refl _ | (a::l₁) lβ‚‚ := sublist.cons _ _ _ (sublist_append_right l₁ lβ‚‚) theorem sublist_cons_of_sublist (a : Ξ±) {l₁ lβ‚‚ : list Ξ±} : l₁ <+ lβ‚‚ β†’ l₁ <+ a::lβ‚‚ := sublist.cons _ _ _ theorem sublist_append_of_sublist_left {l l₁ lβ‚‚ : list Ξ±} (s : l <+ l₁) : l <+ l₁++lβ‚‚ := s.trans $ sublist_append_left _ _ theorem sublist_append_of_sublist_right {l l₁ lβ‚‚ : list Ξ±} (s : l <+ lβ‚‚) : l <+ l₁++lβ‚‚ := s.trans $ sublist_append_right _ _ theorem sublist_of_cons_sublist_cons {l₁ lβ‚‚ : list Ξ±} : βˆ€ {a : Ξ±}, a::l₁ <+ a::lβ‚‚ β†’ l₁ <+ lβ‚‚ | ._ (sublist.cons ._ ._ a s) := sublist_of_cons_sublist s | ._ (sublist.cons2 ._ ._ a s) := s theorem cons_sublist_cons_iff {l₁ lβ‚‚ : list Ξ±} {a : Ξ±} : a::l₁ <+ a::lβ‚‚ ↔ l₁ <+ lβ‚‚ := ⟨sublist_of_cons_sublist_cons, cons_sublist_cons _⟩ @[simp] theorem append_sublist_append_left {l₁ lβ‚‚ : list Ξ±} : βˆ€ l, l++l₁ <+ l++lβ‚‚ ↔ l₁ <+ lβ‚‚ | [] := iff.rfl | (a::l) := cons_sublist_cons_iff.trans (append_sublist_append_left l) theorem sublist.append_right {l₁ lβ‚‚ : list Ξ±} (h : l₁ <+ lβ‚‚) (l) : l₁++l <+ lβ‚‚++l := begin induction h with _ _ a _ ih _ _ a _ ih, { refl }, { apply sublist_cons_of_sublist a ih }, { apply cons_sublist_cons a ih } end theorem sublist_or_mem_of_sublist {l l₁ lβ‚‚ : list Ξ±} {a : Ξ±} (h : l <+ l₁ ++ a::lβ‚‚) : l <+ l₁ ++ lβ‚‚ ∨ a ∈ l := begin induction l₁ with b l₁ IH generalizing l, { cases h, { left, exact β€Ήl <+ lβ‚‚β€Ί }, { right, apply mem_cons_self } }, { cases h with _ _ _ h _ _ _ h, { exact or.imp_left (sublist_cons_of_sublist _) (IH h) }, { exact (IH h).imp (cons_sublist_cons _) (mem_cons_of_mem _) } } end theorem sublist.reverse {l₁ lβ‚‚ : list Ξ±} (h : l₁ <+ lβ‚‚) : l₁.reverse <+ lβ‚‚.reverse := begin induction h with _ _ _ _ ih _ _ a _ ih, {refl}, { rw reverse_cons, exact sublist_append_of_sublist_left ih }, { rw [reverse_cons, reverse_cons], exact ih.append_right [a] } end @[simp] theorem reverse_sublist_iff {l₁ lβ‚‚ : list Ξ±} : l₁.reverse <+ lβ‚‚.reverse ↔ l₁ <+ lβ‚‚ := ⟨λ h, l₁.reverse_reverse β–Έ lβ‚‚.reverse_reverse β–Έ h.reverse, sublist.reverse⟩ @[simp] theorem append_sublist_append_right {l₁ lβ‚‚ : list Ξ±} (l) : l₁++l <+ lβ‚‚++l ↔ l₁ <+ lβ‚‚ := ⟨λ h, by simpa only [reverse_append, append_sublist_append_left, reverse_sublist_iff] using h.reverse, Ξ» h, h.append_right l⟩ theorem sublist.append {l₁ lβ‚‚ r₁ rβ‚‚ : list Ξ±} (hl : l₁ <+ lβ‚‚) (hr : r₁ <+ rβ‚‚) : l₁ ++ r₁ <+ lβ‚‚ ++ rβ‚‚ := (hl.append_right _).trans ((append_sublist_append_left _).2 hr) theorem sublist.subset : Ξ  {l₁ lβ‚‚ : list Ξ±}, l₁ <+ lβ‚‚ β†’ l₁ βŠ† lβ‚‚ | ._ ._ sublist.slnil b h := h | ._ ._ (sublist.cons l₁ lβ‚‚ a s) b h := mem_cons_of_mem _ (sublist.subset s h) | ._ ._ (sublist.cons2 l₁ lβ‚‚ a s) b h := match eq_or_mem_of_mem_cons h with | or.inl h := h β–Έ mem_cons_self _ _ | or.inr h := mem_cons_of_mem _ (sublist.subset s h) end theorem singleton_sublist {a : Ξ±} {l} : [a] <+ l ↔ a ∈ l := ⟨λ h, h.subset (mem_singleton_self _), Ξ» h, let ⟨s, t, e⟩ := mem_split h in e.symm β–Έ (cons_sublist_cons _ (nil_sublist _)).trans (sublist_append_right _ _)⟩ theorem eq_nil_of_sublist_nil {l : list Ξ±} (s : l <+ []) : l = [] := eq_nil_of_subset_nil $ s.subset @[simp] theorem sublist_nil_iff_eq_nil {l : list Ξ±} : l <+ [] ↔ l = [] := ⟨eq_nil_of_sublist_nil, Ξ» H, H β–Έ sublist.refl _⟩ theorem repeat_sublist_repeat (a : Ξ±) {m n} : repeat a m <+ repeat a n ↔ m ≀ n := ⟨λ h, by simpa only [length_repeat] using length_le_of_sublist h, Ξ» h, by induction h; [refl, simp only [*, repeat_succ, sublist.cons]] ⟩ theorem eq_of_sublist_of_length_eq : βˆ€ {l₁ lβ‚‚ : list Ξ±}, l₁ <+ lβ‚‚ β†’ length l₁ = length lβ‚‚ β†’ l₁ = lβ‚‚ | ._ ._ sublist.slnil h := rfl | ._ ._ (sublist.cons l₁ lβ‚‚ a s) h := absurd (length_le_of_sublist s) $ not_le_of_gt $ by rw h; apply lt_succ_self | ._ ._ (sublist.cons2 l₁ lβ‚‚ a s) h := by rw [length, length] at h; injection h with h; rw eq_of_sublist_of_length_eq s h theorem eq_of_sublist_of_length_le {l₁ lβ‚‚ : list Ξ±} (s : l₁ <+ lβ‚‚) (h : length lβ‚‚ ≀ length l₁) : l₁ = lβ‚‚ := eq_of_sublist_of_length_eq s (le_antisymm (length_le_of_sublist s) h) theorem sublist.antisymm {l₁ lβ‚‚ : list Ξ±} (s₁ : l₁ <+ lβ‚‚) (sβ‚‚ : lβ‚‚ <+ l₁) : l₁ = lβ‚‚ := eq_of_sublist_of_length_le s₁ (length_le_of_sublist sβ‚‚) instance decidable_sublist [decidable_eq Ξ±] : βˆ€ (l₁ lβ‚‚ : list Ξ±), decidable (l₁ <+ lβ‚‚) | [] lβ‚‚ := is_true $ nil_sublist _ | (a::l₁) [] := is_false $ Ξ»h, list.no_confusion $ eq_nil_of_sublist_nil h | (a::l₁) (b::lβ‚‚) := if h : a = b then decidable_of_decidable_of_iff (decidable_sublist l₁ lβ‚‚) $ by rw [← h]; exact ⟨cons_sublist_cons _, sublist_of_cons_sublist_cons⟩ else decidable_of_decidable_of_iff (decidable_sublist (a::l₁) lβ‚‚) ⟨sublist_cons_of_sublist _, Ξ»s, match a, l₁, s, h with | a, l₁, sublist.cons ._ ._ ._ s', h := s' | ._, ._, sublist.cons2 t ._ ._ s', h := absurd rfl h end⟩ /-! ### index_of -/ section index_of variable [decidable_eq Ξ±] @[simp] theorem index_of_nil (a : Ξ±) : index_of a [] = 0 := rfl theorem index_of_cons (a b : Ξ±) (l : list Ξ±) : index_of a (b::l) = if a = b then 0 else succ (index_of a l) := rfl theorem index_of_cons_eq {a b : Ξ±} (l : list Ξ±) : a = b β†’ index_of a (b::l) = 0 := assume e, if_pos e @[simp] theorem index_of_cons_self (a : Ξ±) (l : list Ξ±) : index_of a (a::l) = 0 := index_of_cons_eq _ rfl @[simp, priority 990] theorem index_of_cons_ne {a b : Ξ±} (l : list Ξ±) : a β‰  b β†’ index_of a (b::l) = succ (index_of a l) := assume n, if_neg n theorem index_of_eq_length {a : Ξ±} {l : list Ξ±} : index_of a l = length l ↔ a βˆ‰ l := begin induction l with b l ih, { exact iff_of_true rfl (not_mem_nil _) }, simp only [length, mem_cons_iff, index_of_cons], split_ifs, { exact iff_of_false (by rintro ⟨⟩) (Ξ» H, H $ or.inl h) }, { simp only [h, false_or], rw ← ih, exact succ_inj' } end @[simp, priority 980] theorem index_of_of_not_mem {l : list Ξ±} {a : Ξ±} : a βˆ‰ l β†’ index_of a l = length l := index_of_eq_length.2 theorem index_of_le_length {a : Ξ±} {l : list Ξ±} : index_of a l ≀ length l := begin induction l with b l ih, {refl}, simp only [length, index_of_cons], by_cases h : a = b, {rw if_pos h, exact nat.zero_le _}, rw if_neg h, exact succ_le_succ ih end theorem index_of_lt_length {a} {l : list Ξ±} : index_of a l < length l ↔ a ∈ l := ⟨λh, decidable.by_contradiction $ Ξ» al, ne_of_lt h $ index_of_eq_length.2 al, Ξ»al, lt_of_le_of_ne index_of_le_length $ Ξ» h, index_of_eq_length.1 h al⟩ end index_of /-! ### nth element -/ theorem nth_le_of_mem : βˆ€ {a} {l : list Ξ±}, a ∈ l β†’ βˆƒ n h, nth_le l n h = a | a (_ :: l) (or.inl rfl) := ⟨0, succ_pos _, rfl⟩ | a (b :: l) (or.inr m) := let ⟨n, h, e⟩ := nth_le_of_mem m in ⟨n+1, succ_lt_succ h, e⟩ theorem nth_le_nth : βˆ€ {l : list Ξ±} {n} h, nth l n = some (nth_le l n h) | (a :: l) 0 h := rfl | (a :: l) (n+1) h := @nth_le_nth l n _ theorem nth_len_le : βˆ€ {l : list Ξ±} {n}, length l ≀ n β†’ nth l n = none | [] n h := rfl | (a :: l) (n+1) h := nth_len_le (le_of_succ_le_succ h) theorem nth_eq_some {l : list Ξ±} {n a} : nth l n = some a ↔ βˆƒ h, nth_le l n h = a := ⟨λ e, have h : n < length l, from lt_of_not_ge $ Ξ» hn, by rw nth_len_le hn at e; contradiction, ⟨h, by rw nth_le_nth h at e; injection e with e; apply nth_le_mem⟩, Ξ» ⟨h, e⟩, e β–Έ nth_le_nth _⟩ @[simp] theorem nth_eq_none_iff : βˆ€ {l : list Ξ±} {n}, nth l n = none ↔ length l ≀ n := begin intros, split, { intro h, by_contradiction h', have hβ‚‚ : βˆƒ h, l.nth_le n h = l.nth_le n (lt_of_not_ge h') := ⟨lt_of_not_ge h', rfl⟩, rw [← nth_eq_some, h] at hβ‚‚, cases hβ‚‚ }, { solve_by_elim [nth_len_le] }, end theorem nth_of_mem {a} {l : list Ξ±} (h : a ∈ l) : βˆƒ n, nth l n = some a := let ⟨n, h, e⟩ := nth_le_of_mem h in ⟨n, by rw [nth_le_nth, e]⟩ theorem nth_le_mem : βˆ€ (l : list Ξ±) n h, nth_le l n h ∈ l | (a :: l) 0 h := mem_cons_self _ _ | (a :: l) (n+1) h := mem_cons_of_mem _ (nth_le_mem l _ _) theorem nth_mem {l : list Ξ±} {n a} (e : nth l n = some a) : a ∈ l := let ⟨h, e⟩ := nth_eq_some.1 e in e β–Έ nth_le_mem _ _ _ theorem mem_iff_nth_le {a} {l : list Ξ±} : a ∈ l ↔ βˆƒ n h, nth_le l n h = a := ⟨nth_le_of_mem, Ξ» ⟨n, h, e⟩, e β–Έ nth_le_mem _ _ _⟩ theorem mem_iff_nth {a} {l : list Ξ±} : a ∈ l ↔ βˆƒ n, nth l n = some a := mem_iff_nth_le.trans $ exists_congr $ Ξ» n, nth_eq_some.symm lemma nth_zero (l : list Ξ±) : l.nth 0 = l.head' := by cases l; refl lemma nth_injective {Ξ± : Type u} {xs : list Ξ±} {i j : β„•} (hβ‚€ : i < xs.length) (h₁ : nodup xs) (hβ‚‚ : xs.nth i = xs.nth j) : i = j := begin induction xs with x xs generalizing i j, { cases hβ‚€ }, { cases i; cases j, case nat.zero nat.zero { refl }, case nat.succ nat.succ { congr, cases h₁, apply xs_ih; solve_by_elim [lt_of_succ_lt_succ] }, iterate 2 { dsimp at hβ‚‚, cases h₁ with _ _ h h', cases h x _ rfl, rw mem_iff_nth, exact ⟨_, hβ‚‚.symm⟩ <|> exact ⟨_, hβ‚‚βŸ© } }, end @[simp] theorem nth_map (f : Ξ± β†’ Ξ²) : βˆ€ l n, nth (map f l) n = (nth l n).map f | [] n := rfl | (a :: l) 0 := rfl | (a :: l) (n+1) := nth_map l n theorem nth_le_map (f : Ξ± β†’ Ξ²) {l n} (H1 H2) : nth_le (map f l) n H1 = f (nth_le l n H2) := option.some.inj $ by rw [← nth_le_nth, nth_map, nth_le_nth]; refl /-- A version of `nth_le_map` that can be used for rewriting. -/ theorem nth_le_map_rev (f : Ξ± β†’ Ξ²) {l n} (H) : f (nth_le l n H) = nth_le (map f l) n ((length_map f l).symm β–Έ H) := (nth_le_map f _ _).symm @[simp] theorem nth_le_map' (f : Ξ± β†’ Ξ²) {l n} (H) : nth_le (map f l) n H = f (nth_le l n (length_map f l β–Έ H)) := nth_le_map f _ _ /-- If one has `nth_le L i hi` in a formula and `h : L = L'`, one can not `rw h` in the formula as `hi` gives `i < L.length` and not `i < L'.length`. The lemma `nth_le_of_eq` can be used to make such a rewrite, with `rw (nth_le_of_eq h)`. -/ lemma nth_le_of_eq {L L' : list Ξ±} (h : L = L') {i : β„•} (hi : i < L.length) : nth_le L i hi = nth_le L' i (h β–Έ hi) := by { congr, exact h} @[simp] lemma nth_le_singleton (a : Ξ±) {n : β„•} (hn : n < 1) : nth_le [a] n hn = a := have hn0 : n = 0 := le_zero_iff.1 (le_of_lt_succ hn), by subst hn0; refl lemma nth_le_zero [inhabited Ξ±] {L : list Ξ±} (h : 0 < L.length) : L.nth_le 0 h = L.head := by { cases L, cases h, simp, } lemma nth_le_append : βˆ€ {l₁ lβ‚‚ : list Ξ±} {n : β„•} (hn₁) (hnβ‚‚), (l₁ ++ lβ‚‚).nth_le n hn₁ = l₁.nth_le n hnβ‚‚ | [] _ n hn₁ hnβ‚‚ := (nat.not_lt_zero _ hnβ‚‚).elim | (a::l) _ 0 hn₁ hnβ‚‚ := rfl | (a::l) _ (n+1) hn₁ hnβ‚‚ := by simp only [nth_le, cons_append]; exact nth_le_append _ _ lemma nth_le_append_right_aux {l₁ lβ‚‚ : list Ξ±} {n : β„•} (h₁ : l₁.length ≀ n) (hβ‚‚ : n < (l₁ ++ lβ‚‚).length) : n - l₁.length < lβ‚‚.length := begin rw list.length_append at hβ‚‚, convert (tsub_lt_tsub_iff_right h₁).mpr hβ‚‚, simp, end lemma nth_le_append_right : βˆ€ {l₁ lβ‚‚ : list Ξ±} {n : β„•} (h₁ : l₁.length ≀ n) (hβ‚‚), (l₁ ++ lβ‚‚).nth_le n hβ‚‚ = lβ‚‚.nth_le (n - l₁.length) (nth_le_append_right_aux h₁ hβ‚‚) | [] _ n h₁ hβ‚‚ := rfl | (a :: l) _ (n+1) h₁ hβ‚‚ := begin dsimp, conv { to_rhs, congr, skip, rw [tsub_add_eq_tsub_tsub, tsub_right_comm, add_tsub_cancel_right], }, rw nth_le_append_right (nat.lt_succ_iff.mp h₁), end @[simp] lemma nth_le_repeat (a : Ξ±) {n m : β„•} (h : m < (list.repeat a n).length) : (list.repeat a n).nth_le m h = a := eq_of_mem_repeat (nth_le_mem _ _ _) lemma nth_append {l₁ lβ‚‚ : list Ξ±} {n : β„•} (hn : n < l₁.length) : (l₁ ++ lβ‚‚).nth n = l₁.nth n := have hn' : n < (l₁ ++ lβ‚‚).length := lt_of_lt_of_le hn (by rw length_append; exact nat.le_add_right _ _), by rw [nth_le_nth hn, nth_le_nth hn', nth_le_append] lemma nth_append_right {l₁ lβ‚‚ : list Ξ±} {n : β„•} (hn : l₁.length ≀ n) : (l₁ ++ lβ‚‚).nth n = lβ‚‚.nth (n - l₁.length) := begin by_cases hl : n < (l₁ ++ lβ‚‚).length, { rw [nth_le_nth hl, nth_le_nth, nth_le_append_right hn] }, { rw [nth_len_le (le_of_not_lt hl), nth_len_le], rw [not_lt, length_append] at hl, exact le_tsub_of_add_le_left hl } end lemma last_eq_nth_le : βˆ€ (l : list Ξ±) (h : l β‰  []), last l h = l.nth_le (l.length - 1) (nat.sub_lt (length_pos_of_ne_nil h) one_pos) | [] h := rfl | [a] h := by rw [last_singleton, nth_le_singleton] | (a :: b :: l) h := by { rw [last_cons, last_eq_nth_le (b :: l)], refl, exact cons_ne_nil b l } @[simp] lemma nth_concat_length : βˆ€ (l : list Ξ±) (a : Ξ±), (l ++ [a]).nth l.length = some a | [] a := rfl | (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length] lemma nth_le_cons_length (x : Ξ±) (xs : list Ξ±) (n : β„•) (h : n = xs.length) : (x :: xs).nth_le n (by simp [h]) = (x :: xs).last (cons_ne_nil x xs) := begin rw last_eq_nth_le, congr, simp [h] end @[ext] theorem ext : βˆ€ {l₁ lβ‚‚ : list Ξ±}, (βˆ€n, nth l₁ n = nth lβ‚‚ n) β†’ l₁ = lβ‚‚ | [] [] h := rfl | (a::l₁) [] h := by have h0 := h 0; contradiction | [] (a'::lβ‚‚) h := by have h0 := h 0; contradiction | (a::l₁) (a'::lβ‚‚) h := by have h0 : some a = some a' := h 0; injection h0 with aa; simp only [aa, ext (Ξ»n, h (n+1))]; split; refl theorem ext_le {l₁ lβ‚‚ : list Ξ±} (hl : length l₁ = length lβ‚‚) (h : βˆ€n h₁ hβ‚‚, nth_le l₁ n h₁ = nth_le lβ‚‚ n hβ‚‚) : l₁ = lβ‚‚ := ext $ Ξ»n, if h₁ : n < length l₁ then by rw [nth_le_nth, nth_le_nth, h n h₁ (by rwa [← hl])] else let h₁ := le_of_not_gt h₁ in by { rw [nth_len_le h₁, nth_len_le], rwa [←hl], } @[simp] theorem index_of_nth_le [decidable_eq Ξ±] {a : Ξ±} : βˆ€ {l : list Ξ±} h, nth_le l (index_of a l) h = a | (b::l) h := by by_cases h' : a = b; simp only [h', if_pos, if_false, index_of_cons, nth_le, @index_of_nth_le l] @[simp] theorem index_of_nth [decidable_eq Ξ±] {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : nth l (index_of a l) = some a := by rw [nth_le_nth, index_of_nth_le (index_of_lt_length.2 h)] theorem nth_le_reverse_aux1 : βˆ€ (l r : list Ξ±) (i h1 h2), nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2 | [] r i := Ξ»h1 h2, rfl | (a :: l) r i := by rw (show i + length (a :: l) = i + 1 + length l, from add_right_comm i (length l) 1); exact Ξ»h1 h2, nth_le_reverse_aux1 l (a :: r) (i+1) h1 (succ_lt_succ h2) lemma index_of_inj [decidable_eq Ξ±] {l : list Ξ±} {x y : Ξ±} (hx : x ∈ l) (hy : y ∈ l) : index_of x l = index_of y l ↔ x = y := ⟨λ h, have nth_le l (index_of x l) (index_of_lt_length.2 hx) = nth_le l (index_of y l) (index_of_lt_length.2 hy), by simp only [h], by simpa only [index_of_nth_le], Ξ» h, by subst h⟩ theorem nth_le_reverse_aux2 : βˆ€ (l r : list Ξ±) (i : nat) (h1) (h2), nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2 | [] r i h1 h2 := absurd h2 (nat.not_lt_zero _) | (a :: l) r 0 h1 h2 := begin have aux := nth_le_reverse_aux1 l (a :: r) 0, rw zero_add at aux, exact aux _ (zero_lt_succ _) end | (a :: l) r (i+1) h1 h2 := begin have aux := nth_le_reverse_aux2 l (a :: r) i, have heq := calc length (a :: l) - 1 - (i + 1) = length l - (1 + i) : by rw add_comm; refl ... = length l - 1 - i : by rw ← tsub_add_eq_tsub_tsub, rw [← heq] at aux, apply aux end @[simp] theorem nth_le_reverse (l : list Ξ±) (i : nat) (h1 h2) : nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 := nth_le_reverse_aux2 _ _ _ _ _ lemma nth_le_reverse' (l : list Ξ±) (n : β„•) (hn : n < l.reverse.length) (hn') : l.reverse.nth_le n hn = l.nth_le (l.length - 1 - n) hn' := begin rw eq_comm, convert nth_le_reverse l.reverse _ _ _ using 1, { simp }, { simpa } end lemma eq_cons_of_length_one {l : list Ξ±} (h : l.length = 1) : l = [l.nth_le 0 (h.symm β–Έ zero_lt_one)] := begin refine ext_le (by convert h) (Ξ» n h₁ hβ‚‚, _), simp only [nth_le_singleton], congr, exact eq_bot_iff.mpr (nat.lt_succ_iff.mp hβ‚‚) end lemma modify_nth_tail_modify_nth_tail {f g : list Ξ± β†’ list Ξ±} (m : β„•) : βˆ€n (l:list Ξ±), (l.modify_nth_tail f n).modify_nth_tail g (m + n) = l.modify_nth_tail (Ξ»l, (f l).modify_nth_tail g m) n | 0 l := rfl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_modify_nth_tail n l) lemma modify_nth_tail_modify_nth_tail_le {f g : list Ξ± β†’ list Ξ±} (m n : β„•) (l : list Ξ±) (h : n ≀ m) : (l.modify_nth_tail f n).modify_nth_tail g m = l.modify_nth_tail (Ξ»l, (f l).modify_nth_tail g (m - n)) n := begin rcases le_iff_exists_add.1 h with ⟨m, rfl⟩, rw [add_tsub_cancel_left, add_comm, modify_nth_tail_modify_nth_tail] end lemma modify_nth_tail_modify_nth_tail_same {f g : list Ξ± β†’ list Ξ±} (n : β„•) (l:list Ξ±) : (l.modify_nth_tail f n).modify_nth_tail g n = l.modify_nth_tail (g ∘ f) n := by rw [modify_nth_tail_modify_nth_tail_le n n l (le_refl n), tsub_self]; refl lemma modify_nth_tail_id : βˆ€n (l:list Ξ±), l.modify_nth_tail id n = l | 0 l := rfl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_id n l) theorem remove_nth_eq_nth_tail : βˆ€ n (l : list Ξ±), remove_nth l n = modify_nth_tail tail n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (a::l) := congr_arg (cons _) (remove_nth_eq_nth_tail _ _) theorem update_nth_eq_modify_nth (a : Ξ±) : βˆ€ n (l : list Ξ±), update_nth l n a = modify_nth (Ξ» _, a) n l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := congr_arg (cons _) (update_nth_eq_modify_nth _ _) theorem modify_nth_eq_update_nth (f : Ξ± β†’ Ξ±) : βˆ€ n (l : list Ξ±), modify_nth f n l = ((Ξ» a, update_nth l n (f a)) <$> nth l n).get_or_else l | 0 l := by cases l; refl | (n+1) [] := rfl | (n+1) (b::l) := (congr_arg (cons b) (modify_nth_eq_update_nth n l)).trans $ by cases nth l n; refl theorem nth_modify_nth (f : Ξ± β†’ Ξ±) : βˆ€ n (l : list Ξ±) m, nth (modify_nth f n l) m = (Ξ» a, if n = m then f a else a) <$> nth l m | n l 0 := by cases l; cases n; refl | n [] (m+1) := by cases n; refl | 0 (a::l) (m+1) := by cases nth l m; refl | (n+1) (a::l) (m+1) := (nth_modify_nth n l m).trans $ by cases nth l m with b; by_cases n = m; simp only [h, if_pos, if_true, if_false, option.map_none, option.map_some, mt succ.inj, not_false_iff] theorem modify_nth_tail_length (f : list Ξ± β†’ list Ξ±) (H : βˆ€ l, length (f l) = length l) : βˆ€ n l, length (modify_nth_tail f n l) = length l | 0 l := H _ | (n+1) [] := rfl | (n+1) (a::l) := @congr_arg _ _ _ _ (+1) (modify_nth_tail_length _ _) @[simp] theorem modify_nth_length (f : Ξ± β†’ Ξ±) : βˆ€ n l, length (modify_nth f n l) = length l := modify_nth_tail_length _ (Ξ» l, by cases l; refl) @[simp] theorem update_nth_length (l : list Ξ±) (n) (a : Ξ±) : length (update_nth l n a) = length l := by simp only [update_nth_eq_modify_nth, modify_nth_length] @[simp] theorem nth_modify_nth_eq (f : Ξ± β†’ Ξ±) (n) (l : list Ξ±) : nth (modify_nth f n l) n = f <$> nth l n := by simp only [nth_modify_nth, if_pos] @[simp] theorem nth_modify_nth_ne (f : Ξ± β†’ Ξ±) {m n} (l : list Ξ±) (h : m β‰  n) : nth (modify_nth f m l) n = nth l n := by simp only [nth_modify_nth, if_neg h, id_map'] theorem nth_update_nth_eq (a : Ξ±) (n) (l : list Ξ±) : nth (update_nth l n a) n = (Ξ» _, a) <$> nth l n := by simp only [update_nth_eq_modify_nth, nth_modify_nth_eq] theorem nth_update_nth_of_lt (a : Ξ±) {n} {l : list Ξ±} (h : n < length l) : nth (update_nth l n a) n = some a := by rw [nth_update_nth_eq, nth_le_nth h]; refl theorem nth_update_nth_ne (a : Ξ±) {m n} (l : list Ξ±) (h : m β‰  n) : nth (update_nth l m a) n = nth l n := by simp only [update_nth_eq_modify_nth, nth_modify_nth_ne _ _ h] @[simp] lemma update_nth_nil (n : β„•) (a : Ξ±) : [].update_nth n a = [] := rfl @[simp] lemma update_nth_succ (x : Ξ±) (xs : list Ξ±) (n : β„•) (a : Ξ±) : (x :: xs).update_nth n.succ a = x :: xs.update_nth n a := rfl lemma update_nth_comm (a b : Ξ±) : Ξ  {n m : β„•} (l : list Ξ±) (h : n β‰  m), (l.update_nth n a).update_nth m b = (l.update_nth m b).update_nth n a | _ _ [] _ := by simp | 0 0 (x :: t) h := absurd rfl h | (n + 1) 0 (x :: t) h := by simp [list.update_nth] | 0 (m + 1) (x :: t) h := by simp [list.update_nth] | (n + 1) (m + 1) (x :: t) h := by { simp only [update_nth, true_and, eq_self_iff_true], exact update_nth_comm t (Ξ» h', h $ nat.succ_inj'.mpr h'), } @[simp] lemma nth_le_update_nth_eq (l : list Ξ±) (i : β„•) (a : Ξ±) (h : i < (l.update_nth i a).length) : (l.update_nth i a).nth_le i h = a := by rw [← option.some_inj, ← nth_le_nth, nth_update_nth_eq, nth_le_nth]; simp * at * @[simp] lemma nth_le_update_nth_of_ne {l : list Ξ±} {i j : β„•} (h : i β‰  j) (a : Ξ±) (hj : j < (l.update_nth i a).length) : (l.update_nth i a).nth_le j hj = l.nth_le j (by simpa using hj) := by rw [← option.some_inj, ← list.nth_le_nth, list.nth_update_nth_ne _ _ h, list.nth_le_nth] lemma mem_or_eq_of_mem_update_nth : βˆ€ {l : list Ξ±} {n : β„•} {a b : Ξ±} (h : a ∈ l.update_nth n b), a ∈ l ∨ a = b | [] n a b h := false.elim h | (c::l) 0 a b h := ((mem_cons_iff _ _ _).1 h).elim or.inr (or.inl ∘ mem_cons_of_mem _) | (c::l) (n+1) a b h := ((mem_cons_iff _ _ _).1 h).elim (Ξ» h, h β–Έ or.inl (mem_cons_self _ _)) (Ξ» h, (mem_or_eq_of_mem_update_nth h).elim (or.inl ∘ mem_cons_of_mem _) or.inr) section insert_nth variable {a : Ξ±} @[simp] lemma insert_nth_zero (s : list Ξ±) (x : Ξ±) : insert_nth 0 x s = x :: s := rfl @[simp] lemma insert_nth_succ_nil (n : β„•) (a : Ξ±) : insert_nth (n + 1) a [] = [] := rfl @[simp] lemma insert_nth_succ_cons (s : list Ξ±) (hd x : Ξ±) (n : β„•) : insert_nth (n + 1) x (hd :: s) = hd :: (insert_nth n x s) := rfl lemma length_insert_nth : βˆ€n as, n ≀ length as β†’ length (insert_nth n a as) = length as + 1 | 0 as h := rfl | (n+1) [] h := (nat.not_succ_le_zero _ h).elim | (n+1) (a'::as) h := congr_arg nat.succ $ length_insert_nth n as (nat.le_of_succ_le_succ h) lemma remove_nth_insert_nth (n:β„•) (l : list Ξ±) : (l.insert_nth n a).remove_nth n = l := by rw [remove_nth_eq_nth_tail, insert_nth, modify_nth_tail_modify_nth_tail_same]; from modify_nth_tail_id _ _ lemma insert_nth_remove_nth_of_ge : βˆ€n m as, n < length as β†’ n ≀ m β†’ insert_nth m a (as.remove_nth n) = (as.insert_nth (m + 1) a).remove_nth n | 0 0 [] has _ := (lt_irrefl _ has).elim | 0 0 (a::as) has hmn := by simp [remove_nth, insert_nth] | 0 (m+1) (a::as) has hmn := rfl | (n+1) (m+1) (a::as) has hmn := congr_arg (cons a) $ insert_nth_remove_nth_of_ge n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn) lemma insert_nth_remove_nth_of_le : βˆ€n m as, n < length as β†’ m ≀ n β†’ insert_nth m a (as.remove_nth n) = (as.insert_nth m a).remove_nth (n + 1) | n 0 (a :: as) has hmn := rfl | (n + 1) (m + 1) (a :: as) has hmn := congr_arg (cons a) $ insert_nth_remove_nth_of_le n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn) lemma insert_nth_comm (a b : Ξ±) : βˆ€(i j : β„•) (l : list Ξ±) (h : i ≀ j) (hj : j ≀ length l), (l.insert_nth i a).insert_nth (j + 1) b = (l.insert_nth j b).insert_nth i a | 0 j l := by simp [insert_nth] | (i + 1) 0 l := assume h, (nat.not_lt_zero _ h).elim | (i + 1) (j+1) [] := by simp | (i + 1) (j+1) (c::l) := assume hβ‚€ h₁, by simp [insert_nth]; exact insert_nth_comm i j l (nat.le_of_succ_le_succ hβ‚€) (nat.le_of_succ_le_succ h₁) lemma mem_insert_nth {a b : Ξ±} : βˆ€ {n : β„•} {l : list Ξ±} (hi : n ≀ l.length), a ∈ l.insert_nth n b ↔ a = b ∨ a ∈ l | 0 as h := iff.rfl | (n+1) [] h := (nat.not_succ_le_zero _ h).elim | (n+1) (a'::as) h := begin dsimp [list.insert_nth], erw [list.mem_cons_iff, mem_insert_nth (nat.le_of_succ_le_succ h), list.mem_cons_iff, ← or.assoc, or_comm (a = a'), or.assoc] end lemma inj_on_insert_nth_index_of_not_mem (l : list Ξ±) (x : Ξ±) (hx : x βˆ‰ l) : set.inj_on (Ξ» k, insert_nth k x l) {n | n ≀ l.length} := begin induction l with hd tl IH, { intros n hn m hm h, simp only [set.mem_singleton_iff, set.set_of_eq_eq_singleton, length, nonpos_iff_eq_zero] at hn hm, simp [hn, hm] }, { intros n hn m hm h, simp only [length, set.mem_set_of_eq] at hn hm, simp only [mem_cons_iff, not_or_distrib] at hx, cases n; cases m, { refl }, { simpa [hx.left] using h }, { simpa [ne.symm hx.left] using h }, { simp only [true_and, eq_self_iff_true, insert_nth_succ_cons] at h, rw nat.succ_inj', refine IH hx.right _ _ h, { simpa [nat.succ_le_succ_iff] using hn }, { simpa [nat.succ_le_succ_iff] using hm } } } end lemma insert_nth_of_length_lt (l : list Ξ±) (x : Ξ±) (n : β„•) (h : l.length < n) : insert_nth n x l = l := begin induction l with hd tl IH generalizing n, { cases n, { simpa using h }, { simp } }, { cases n, { simpa using h }, { simp only [nat.succ_lt_succ_iff, length] at h, simpa using IH _ h } } end @[simp] lemma insert_nth_length_self (l : list Ξ±) (x : Ξ±) : insert_nth l.length x l = l ++ [x] := begin induction l with hd tl IH, { simp }, { simpa using IH } end lemma length_le_length_insert_nth (l : list Ξ±) (x : Ξ±) (n : β„•) : l.length ≀ (insert_nth n x l).length := begin cases le_or_lt n l.length with hn hn, { rw length_insert_nth _ _ hn, exact (nat.lt_succ_self _).le }, { rw insert_nth_of_length_lt _ _ _ hn } end lemma length_insert_nth_le_succ (l : list Ξ±) (x : Ξ±) (n : β„•) : (insert_nth n x l).length ≀ l.length + 1 := begin cases le_or_lt n l.length with hn hn, { rw length_insert_nth _ _ hn }, { rw insert_nth_of_length_lt _ _ _ hn, exact (nat.lt_succ_self _).le } end lemma nth_le_insert_nth_of_lt (l : list Ξ±) (x : Ξ±) (n k : β„•) (hn : k < n) (hk : k < l.length) (hk' : k < (insert_nth n x l).length := hk.trans_le (length_le_length_insert_nth _ _ _)): (insert_nth n x l).nth_le k hk' = l.nth_le k hk := begin induction n with n IH generalizing k l, { simpa using hn }, { cases l with hd tl, { simp }, { cases k, { simp }, { rw nat.succ_lt_succ_iff at hn, simpa using IH _ _ hn _ } } } end @[simp] lemma nth_le_insert_nth_self (l : list Ξ±) (x : Ξ±) (n : β„•) (hn : n ≀ l.length) (hn' : n < (insert_nth n x l).length := by rwa [length_insert_nth _ _ hn, nat.lt_succ_iff]) : (insert_nth n x l).nth_le n hn' = x := begin induction l with hd tl IH generalizing n, { simp only [length, nonpos_iff_eq_zero] at hn, simp [hn] }, { cases n, { simp }, { simp only [nat.succ_le_succ_iff, length] at hn, simpa using IH _ hn } } end lemma nth_le_insert_nth_add_succ (l : list Ξ±) (x : Ξ±) (n k : β„•) (hk' : n + k < l.length) (hk : n + k + 1 < (insert_nth n x l).length := by rwa [length_insert_nth _ _ (le_self_add.trans hk'.le), nat.succ_lt_succ_iff]) : (insert_nth n x l).nth_le (n + k + 1) hk = nth_le l (n + k) hk' := begin induction l with hd tl IH generalizing n k, { simpa using hk' }, { cases n, { simpa }, { simpa [succ_add] using IH _ _ _ } } end lemma insert_nth_injective (n : β„•) (x : Ξ±) : function.injective (insert_nth n x) := begin induction n with n IH, { have : insert_nth 0 x = cons x := funext (Ξ» _, rfl), simp [this] }, { rintros (_|⟨a, as⟩) (_|⟨b, bs⟩) h; simpa [IH.eq_iff] using h <|> refl } end end insert_nth /-! ### map -/ @[simp] lemma map_nil (f : Ξ± β†’ Ξ²) : map f [] = [] := rfl theorem map_eq_foldr (f : Ξ± β†’ Ξ²) (l : list Ξ±) : map f l = foldr (Ξ» a bs, f a :: bs) [] l := by induction l; simp * lemma map_congr {f g : Ξ± β†’ Ξ²} : βˆ€ {l : list Ξ±}, (βˆ€ x ∈ l, f x = g x) β†’ map f l = map g l | [] _ := rfl | (a::l) h := let ⟨h₁, hβ‚‚βŸ© := forall_mem_cons.1 h in by rw [map, map, h₁, map_congr hβ‚‚] lemma map_eq_map_iff {f g : Ξ± β†’ Ξ²} {l : list Ξ±} : map f l = map g l ↔ (βˆ€ x ∈ l, f x = g x) := begin refine ⟨_, map_congr⟩, intros h x hx, rw [mem_iff_nth_le] at hx, rcases hx with ⟨n, hn, rfl⟩, rw [nth_le_map_rev f, nth_le_map_rev g], congr, exact h end theorem map_concat (f : Ξ± β†’ Ξ²) (a : Ξ±) (l : list Ξ±) : map f (concat l a) = concat (map f l) (f a) := by induction l; [refl, simp only [*, concat_eq_append, cons_append, map, map_append]]; split; refl theorem map_id' {f : Ξ± β†’ Ξ±} (h : βˆ€ x, f x = x) (l : list Ξ±) : map f l = l := by induction l; [refl, simp only [*, map]]; split; refl theorem eq_nil_of_map_eq_nil {f : Ξ± β†’ Ξ²} {l : list Ξ±} (h : map f l = nil) : l = nil := eq_nil_of_length_eq_zero $ by rw [← length_map f l, h]; refl @[simp] theorem map_join (f : Ξ± β†’ Ξ²) (L : list (list Ξ±)) : map f (join L) = join (map (map f) L) := by induction L; [refl, simp only [*, join, map, map_append]] theorem bind_ret_eq_map (f : Ξ± β†’ Ξ²) (l : list Ξ±) : l.bind (list.ret ∘ f) = map f l := by unfold list.bind; induction l; simp only [map, join, list.ret, cons_append, nil_append, *]; split; refl @[simp] theorem map_eq_map {Ξ± Ξ²} (f : Ξ± β†’ Ξ²) (l : list Ξ±) : f <$> l = map f l := rfl @[simp] theorem map_tail (f : Ξ± β†’ Ξ²) (l) : map f (tail l) = tail (map f l) := by cases l; refl @[simp] theorem map_injective_iff {f : Ξ± β†’ Ξ²} : injective (map f) ↔ injective f := begin split; intros h x y hxy, { suffices : [x] = [y], { simpa using this }, apply h, simp [hxy] }, { induction y generalizing x, simpa using hxy, cases x, simpa using hxy, simp at hxy, simp [y_ih hxy.2, h hxy.1] } end /-- A single `list.map` of a composition of functions is equal to composing a `list.map` with another `list.map`, fully applied. This is the reverse direction of `list.map_map`. -/ lemma comp_map (h : Ξ² β†’ Ξ³) (g : Ξ± β†’ Ξ²) (l : list Ξ±) : map (h ∘ g) l = map h (map g l) := (map_map _ _ _).symm /-- Composing a `list.map` with another `list.map` is equal to a single `list.map` of composed functions. -/ @[simp] lemma map_comp_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ²) : map g ∘ map f = map (g ∘ f) := by { ext l, rw comp_map } theorem map_filter_eq_foldr (f : Ξ± β†’ Ξ²) (p : Ξ± β†’ Prop) [decidable_pred p] (as : list Ξ±) : map f (filter p as) = foldr (Ξ» a bs, if p a then f a :: bs else bs) [] as := by { induction as, { refl }, { simp! [*, apply_ite (map f)] } } lemma last_map (f : Ξ± β†’ Ξ²) {l : list Ξ±} (hl : l β‰  []) : (l.map f).last (mt eq_nil_of_map_eq_nil hl) = f (l.last hl) := begin induction l with l_ih l_tl l_ih, { apply (hl rfl).elim }, { cases l_tl, { simp }, { simpa using l_ih } } end /-! ### mapβ‚‚ -/ theorem nil_mapβ‚‚ (f : Ξ± β†’ Ξ² β†’ Ξ³) (l : list Ξ²) : mapβ‚‚ f [] l = [] := by cases l; refl theorem mapβ‚‚_nil (f : Ξ± β†’ Ξ² β†’ Ξ³) (l : list Ξ±) : mapβ‚‚ f l [] = [] := by cases l; refl @[simp] theorem mapβ‚‚_flip (f : Ξ± β†’ Ξ² β†’ Ξ³) : βˆ€ as bs, mapβ‚‚ (flip f) bs as = mapβ‚‚ f as bs | [] [] := rfl | [] (b :: bs) := rfl | (a :: as) [] := rfl | (a :: as) (b :: bs) := by { simp! [mapβ‚‚_flip], refl } /-! ### take, drop -/ @[simp] theorem take_zero (l : list Ξ±) : take 0 l = [] := rfl @[simp] theorem take_nil : βˆ€ n, take n [] = ([] : list Ξ±) | 0 := rfl | (n+1) := rfl theorem take_cons (n) (a : Ξ±) (l : list Ξ±) : take (succ n) (a::l) = a :: take n l := rfl @[simp] theorem take_length : βˆ€ (l : list Ξ±), take (length l) l = l | [] := rfl | (a::l) := begin change a :: (take (length l) l) = a :: l, rw take_length end theorem take_all_of_le : βˆ€ {n} {l : list Ξ±}, length l ≀ n β†’ take n l = l | 0 [] h := rfl | 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _)) | (n+1) [] h := rfl | (n+1) (a::l) h := begin change a :: take n l = a :: l, rw [take_all_of_le (le_of_succ_le_succ h)] end @[simp] theorem take_left : βˆ€ l₁ lβ‚‚ : list Ξ±, take (length l₁) (l₁ ++ lβ‚‚) = l₁ | [] lβ‚‚ := rfl | (a::l₁) lβ‚‚ := congr_arg (cons a) (take_left l₁ lβ‚‚) theorem take_left' {l₁ lβ‚‚ : list Ξ±} {n} (h : length l₁ = n) : take n (l₁ ++ lβ‚‚) = l₁ := by rw ← h; apply take_left theorem take_take : βˆ€ (n m) (l : list Ξ±), take n (take m l) = take (min n m) l | n 0 l := by rw [min_zero, take_zero, take_nil] | 0 m l := by rw [zero_min, take_zero, take_zero] | (succ n) (succ m) nil := by simp only [take_nil] | (succ n) (succ m) (a::l) := by simp only [take, min_succ_succ, take_take n m l]; split; refl theorem take_repeat (a : Ξ±) : βˆ€ (n m : β„•), take n (repeat a m) = repeat a (min n m) | n 0 := by simp | 0 m := by simp | (succ n) (succ m) := by simp [min_succ_succ, take_repeat] lemma map_take {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) : βˆ€ (L : list Ξ±) (i : β„•), (L.take i).map f = (L.map f).take i | [] i := by simp | L 0 := by simp | (h :: t) (n+1) := by { dsimp, rw [map_take], } /-- Taking the first `n` elements in `l₁ ++ lβ‚‚` is the same as appending the first `n` elements of `l₁` to the first `n - l₁.length` elements of `lβ‚‚`. -/ lemma take_append_eq_append_take {l₁ lβ‚‚ : list Ξ±} {n : β„•} : take n (l₁ ++ lβ‚‚) = take n l₁ ++ take (n - l₁.length) lβ‚‚ := begin induction l₁ generalizing n, { simp }, cases n, { simp }, simp * end lemma take_append_of_le_length {l₁ lβ‚‚ : list Ξ±} {n : β„•} (h : n ≀ l₁.length) : (l₁ ++ lβ‚‚).take n = l₁.take n := by simp [take_append_eq_append_take, tsub_eq_zero_iff_le.mpr h] /-- Taking the first `l₁.length + i` elements in `l₁ ++ lβ‚‚` is the same as appending the first `i` elements of `lβ‚‚` to `l₁`. -/ lemma take_append {l₁ lβ‚‚ : list Ξ±} (i : β„•) : take (l₁.length + i) (l₁ ++ lβ‚‚) = l₁ ++ (take i lβ‚‚) := by simp [take_append_eq_append_take, take_all_of_le le_self_add] /-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of length `> i`. Version designed to rewrite from the big list to the small list. -/ lemma nth_le_take (L : list Ξ±) {i j : β„•} (hi : i < L.length) (hj : i < j) : nth_le L i hi = nth_le (L.take j) i (by { rw length_take, exact lt_min hj hi }) := by { rw nth_le_of_eq (take_append_drop j L).symm hi, exact nth_le_append _ _ } /-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of length `> i`. Version designed to rewrite from the small list to the big list. -/ lemma nth_le_take' (L : list Ξ±) {i j : β„•} (hi : i < (L.take j).length) : nth_le (L.take j) i hi = nth_le L i (lt_of_lt_of_le hi (by simp [le_refl])) := by { simp at hi, rw nth_le_take L _ hi.1 } lemma nth_take {l : list Ξ±} {n m : β„•} (h : m < n) : (l.take n).nth m = l.nth m := begin induction n with n hn generalizing l m, { simp only [nat.nat_zero_eq_zero] at h, exact absurd h (not_lt_of_le m.zero_le) }, { cases l with hd tl, { simp only [take_nil] }, { cases m, { simp only [nth, take] }, { simpa only using hn (nat.lt_of_succ_lt_succ h) } } }, end @[simp] lemma nth_take_of_succ {l : list Ξ±} {n : β„•} : (l.take (n + 1)).nth n = l.nth n := nth_take (nat.lt_succ_self n) lemma take_succ {l : list Ξ±} {n : β„•} : l.take (n + 1) = l.take n ++ (l.nth n).to_list := begin induction l with hd tl hl generalizing n, { simp only [option.to_list, nth, take_nil, append_nil]}, { cases n, { simp only [option.to_list, nth, eq_self_iff_true, and_self, take, nil_append] }, { simp only [hl, cons_append, nth, eq_self_iff_true, and_self, take] } } end @[simp] lemma take_eq_nil_iff {l : list Ξ±} {k : β„•} : l.take k = [] ↔ l = [] ∨ k = 0 := by { cases l; cases k; simp [nat.succ_ne_zero] } lemma init_eq_take (l : list Ξ±) : l.init = l.take l.length.pred := begin cases l with x l, { simp [init] }, { induction l with hd tl hl generalizing x, { simp [init], }, { simp [init, hl] } } end lemma init_take {n : β„•} {l : list Ξ±} (h : n < l.length) : (l.take n).init = l.take n.pred := by simp [init_eq_take, min_eq_left_of_lt h, take_take, pred_le] @[simp] lemma drop_eq_nil_of_le {l : list Ξ±} {k : β„•} (h : l.length ≀ k) : l.drop k = [] := by simpa [←length_eq_zero] using tsub_eq_zero_iff_le.mpr h lemma drop_eq_nil_iff_le {l : list Ξ±} {k : β„•} : l.drop k = [] ↔ l.length ≀ k := begin refine ⟨λ h, _, drop_eq_nil_of_le⟩, induction k with k hk generalizing l, { simp only [drop] at h, simp [h] }, { cases l, { simp }, { simp only [drop] at h, simpa [nat.succ_le_succ_iff] using hk h } } end lemma tail_drop (l : list Ξ±) (n : β„•) : (l.drop n).tail = l.drop (n + 1) := begin induction l with hd tl hl generalizing n, { simp }, { cases n, { simp }, { simp [hl] } } end lemma cons_nth_le_drop_succ {l : list Ξ±} {n : β„•} (hn : n < l.length) : l.nth_le n hn :: l.drop (n + 1) = l.drop n := begin induction l with hd tl hl generalizing n, { exact absurd n.zero_le (not_le_of_lt (by simpa using hn)) }, { cases n, { simp }, { simp only [nat.succ_lt_succ_iff, list.length] at hn, simpa [list.nth_le, list.drop] using hl hn } } end theorem drop_nil : βˆ€ n, drop n [] = ([] : list Ξ±) := Ξ» _, drop_eq_nil_of_le (nat.zero_le _) lemma mem_of_mem_drop {Ξ±} {n : β„•} {l : list Ξ±} {x : Ξ±} (h : x ∈ l.drop n) : x ∈ l := begin induction l generalizing n, case list.nil : n h { simpa using h }, case list.cons : l_hd l_tl l_ih n h { cases n; simp only [mem_cons_iff, drop] at h ⊒, { exact h }, right, apply l_ih h }, end @[simp] theorem drop_one : βˆ€ l : list Ξ±, drop 1 l = tail l | [] := rfl | (a :: l) := rfl theorem drop_add : βˆ€ m n (l : list Ξ±), drop (m + n) l = drop m (drop n l) | m 0 l := rfl | m (n+1) [] := (drop_nil _).symm | m (n+1) (a::l) := drop_add m n _ @[simp] theorem drop_left : βˆ€ l₁ lβ‚‚ : list Ξ±, drop (length l₁) (l₁ ++ lβ‚‚) = lβ‚‚ | [] lβ‚‚ := rfl | (a::l₁) lβ‚‚ := drop_left l₁ lβ‚‚ theorem drop_left' {l₁ lβ‚‚ : list Ξ±} {n} (h : length l₁ = n) : drop n (l₁ ++ lβ‚‚) = lβ‚‚ := by rw ← h; apply drop_left theorem drop_eq_nth_le_cons : βˆ€ {n} {l : list Ξ±} h, drop n l = nth_le l n h :: drop (n+1) l | 0 (a::l) h := rfl | (n+1) (a::l) h := @drop_eq_nth_le_cons n _ _ @[simp] lemma drop_length (l : list Ξ±) : l.drop l.length = [] := calc l.drop l.length = (l ++ []).drop l.length : by simp ... = [] : drop_left _ _ /-- Dropping the elements up to `n` in `l₁ ++ lβ‚‚` is the same as dropping the elements up to `n` in `l₁`, dropping the elements up to `n - l₁.length` in `lβ‚‚`, and appending them. -/ lemma drop_append_eq_append_drop {l₁ lβ‚‚ : list Ξ±} {n : β„•} : drop n (l₁ ++ lβ‚‚) = drop n l₁ ++ drop (n - l₁.length) lβ‚‚ := begin induction l₁ generalizing n, { simp }, cases n, { simp }, simp * end lemma drop_append_of_le_length {l₁ lβ‚‚ : list Ξ±} {n : β„•} (h : n ≀ l₁.length) : (l₁ ++ lβ‚‚).drop n = l₁.drop n ++ lβ‚‚ := by simp [drop_append_eq_append_drop, tsub_eq_zero_iff_le.mpr h] /-- Dropping the elements up to `l₁.length + i` in `l₁ + lβ‚‚` is the same as dropping the elements up to `i` in `lβ‚‚`. -/ lemma drop_append {l₁ lβ‚‚ : list Ξ±} (i : β„•) : drop (l₁.length + i) (l₁ ++ lβ‚‚) = drop i lβ‚‚ := by simp [drop_append_eq_append_drop, take_all_of_le le_self_add] /-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by dropping the first `i` elements. Version designed to rewrite from the big list to the small list. -/ lemma nth_le_drop (L : list Ξ±) {i j : β„•} (h : i + j < L.length) : nth_le L (i + j) h = nth_le (L.drop i) j begin have A : i < L.length := lt_of_le_of_lt (nat.le.intro rfl) h, rw (take_append_drop i L).symm at h, simpa only [le_of_lt A, min_eq_left, add_lt_add_iff_left, length_take, length_append] using h end := begin have A : length (take i L) = i, by simp [le_of_lt (lt_of_le_of_lt (nat.le.intro rfl) h)], rw [nth_le_of_eq (take_append_drop i L).symm h, nth_le_append_right]; simp [A] end /-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by dropping the first `i` elements. Version designed to rewrite from the small list to the big list. -/ lemma nth_le_drop' (L : list Ξ±) {i j : β„•} (h : j < (L.drop i).length) : nth_le (L.drop i) j h = nth_le L (i + j) (lt_tsub_iff_left.mp ((length_drop i L) β–Έ h)) := by rw nth_le_drop lemma nth_drop (L : list Ξ±) (i j : β„•) : nth (L.drop i) j = nth L (i + j) := begin ext, simp only [nth_eq_some, nth_le_drop', option.mem_def], split; exact Ξ» ⟨h, ha⟩, ⟨by simpa [lt_tsub_iff_left] using h, ha⟩ end @[simp] theorem drop_drop (n : β„•) : βˆ€ (m) (l : list Ξ±), drop n (drop m l) = drop (n + m) l | m [] := by simp | 0 l := by simp | (m+1) (a::l) := calc drop n (drop (m + 1) (a :: l)) = drop n (drop m l) : rfl ... = drop (n + m) l : drop_drop m l ... = drop (n + (m + 1)) (a :: l) : rfl theorem drop_take : βˆ€ (m : β„•) (n : β„•) (l : list Ξ±), drop m (take (m + n) l) = take n (drop m l) | 0 n _ := by simp | (m+1) n nil := by simp | (m+1) n (_::l) := have h: m + 1 + n = (m+n) + 1, by ac_refl, by simpa [take_cons, h] using drop_take m n l lemma map_drop {Ξ± Ξ² : Type*} (f : Ξ± β†’ Ξ²) : βˆ€ (L : list Ξ±) (i : β„•), (L.drop i).map f = (L.map f).drop i | [] i := by simp | L 0 := by simp | (h :: t) (n+1) := by { dsimp, rw [map_drop], } theorem modify_nth_tail_eq_take_drop (f : list Ξ± β†’ list Ξ±) (H : f [] = []) : βˆ€ n l, modify_nth_tail f n l = take n l ++ f (drop n l) | 0 l := rfl | (n+1) [] := H.symm | (n+1) (b::l) := congr_arg (cons b) (modify_nth_tail_eq_take_drop n l) theorem modify_nth_eq_take_drop (f : Ξ± β†’ Ξ±) : βˆ€ n l, modify_nth f n l = take n l ++ modify_head f (drop n l) := modify_nth_tail_eq_take_drop _ rfl theorem modify_nth_eq_take_cons_drop (f : Ξ± β†’ Ξ±) {n l} (h) : modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n+1) l := by rw [modify_nth_eq_take_drop, drop_eq_nth_le_cons h]; refl theorem update_nth_eq_take_cons_drop (a : Ξ±) {n l} (h : n < length l) : update_nth l n a = take n l ++ a :: drop (n+1) l := by rw [update_nth_eq_modify_nth, modify_nth_eq_take_cons_drop _ h] lemma reverse_take {Ξ±} {xs : list Ξ±} (n : β„•) (h : n ≀ xs.length) : xs.reverse.take n = (xs.drop (xs.length - n)).reverse := begin induction xs generalizing n; simp only [reverse_cons, drop, reverse_nil, zero_tsub, length, take_nil], cases h.lt_or_eq_dec with h' h', { replace h' := le_of_succ_le_succ h', rwa [take_append_of_le_length, xs_ih _ h'], rw [show xs_tl.length + 1 - n = succ (xs_tl.length - n), from _, drop], { rwa [succ_eq_add_one, ← tsub_add_eq_add_tsub] }, { rwa length_reverse } }, { subst h', rw [length, tsub_self, drop], suffices : xs_tl.length + 1 = (xs_tl.reverse ++ [xs_hd]).length, by rw [this, take_length, reverse_cons], rw [length_append, length_reverse], refl } end @[simp] lemma update_nth_eq_nil (l : list Ξ±) (n : β„•) (a : Ξ±) : l.update_nth n a = [] ↔ l = [] := by cases l; cases n; simp only [update_nth] section take' variable [inhabited Ξ±] @[simp] theorem take'_length : βˆ€ n l, length (@take' Ξ± _ n l) = n | 0 l := rfl | (n+1) l := congr_arg succ (take'_length _ _) @[simp] theorem take'_nil : βˆ€ n, take' n (@nil Ξ±) = repeat (default _) n | 0 := rfl | (n+1) := congr_arg (cons _) (take'_nil _) theorem take'_eq_take : βˆ€ {n} {l : list Ξ±}, n ≀ length l β†’ take' n l = take n l | 0 l h := rfl | (n+1) (a::l) h := congr_arg (cons _) $ take'_eq_take $ le_of_succ_le_succ h @[simp] theorem take'_left (l₁ lβ‚‚ : list Ξ±) : take' (length l₁) (l₁ ++ lβ‚‚) = l₁ := (take'_eq_take (by simp only [length_append, nat.le_add_right])).trans (take_left _ _) theorem take'_left' {l₁ lβ‚‚ : list Ξ±} {n} (h : length l₁ = n) : take' n (l₁ ++ lβ‚‚) = l₁ := by rw ← h; apply take'_left end take' /-! ### foldl, foldr -/ lemma foldl_ext (f g : Ξ± β†’ Ξ² β†’ Ξ±) (a : Ξ±) {l : list Ξ²} (H : βˆ€ a : Ξ±, βˆ€ b ∈ l, f a b = g a b) : foldl f a l = foldl g a l := begin induction l with hd tl ih generalizing a, {refl}, unfold foldl, rw [ih (Ξ» a b bin, H a b $ mem_cons_of_mem _ bin), H a hd (mem_cons_self _ _)] end lemma foldr_ext (f g : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) {l : list Ξ±} (H : βˆ€ a ∈ l, βˆ€ b : Ξ², f a b = g a b) : foldr f b l = foldr g b l := begin induction l with hd tl ih, {refl}, simp only [mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] at H, simp only [foldr, ih H.2, H.1] end @[simp] theorem foldl_nil (f : Ξ± β†’ Ξ² β†’ Ξ±) (a : Ξ±) : foldl f a [] = a := rfl @[simp] theorem foldl_cons (f : Ξ± β†’ Ξ² β†’ Ξ±) (a : Ξ±) (b : Ξ²) (l : list Ξ²) : foldl f a (b::l) = foldl f (f a b) l := rfl @[simp] theorem foldr_nil (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) : foldr f b [] = b := rfl @[simp] theorem foldr_cons (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) (a : Ξ±) (l : list Ξ±) : foldr f b (a::l) = f a (foldr f b l) := rfl @[simp] theorem foldl_append (f : Ξ± β†’ Ξ² β†’ Ξ±) : βˆ€ (a : Ξ±) (l₁ lβ‚‚ : list Ξ²), foldl f a (l₁++lβ‚‚) = foldl f (foldl f a l₁) lβ‚‚ | a [] lβ‚‚ := rfl | a (b::l₁) lβ‚‚ := by simp only [cons_append, foldl_cons, foldl_append (f a b) l₁ lβ‚‚] @[simp] theorem foldr_append (f : Ξ± β†’ Ξ² β†’ Ξ²) : βˆ€ (b : Ξ²) (l₁ lβ‚‚ : list Ξ±), foldr f b (l₁++lβ‚‚) = foldr f (foldr f b lβ‚‚) l₁ | b [] lβ‚‚ := rfl | b (a::l₁) lβ‚‚ := by simp only [cons_append, foldr_cons, foldr_append b l₁ lβ‚‚] @[simp] theorem foldl_join (f : Ξ± β†’ Ξ² β†’ Ξ±) : βˆ€ (a : Ξ±) (L : list (list Ξ²)), foldl f a (join L) = foldl (foldl f) a L | a [] := rfl | a (l::L) := by simp only [join, foldl_append, foldl_cons, foldl_join (foldl f a l) L] @[simp] theorem foldr_join (f : Ξ± β†’ Ξ² β†’ Ξ²) : βˆ€ (b : Ξ²) (L : list (list Ξ±)), foldr f b (join L) = foldr (Ξ» l b, foldr f b l) b L | a [] := rfl | a (l::L) := by simp only [join, foldr_append, foldr_join a L, foldr_cons] theorem foldl_reverse (f : Ξ± β†’ Ξ² β†’ Ξ±) (a : Ξ±) (l : list Ξ²) : foldl f a (reverse l) = foldr (Ξ»x y, f y x) a l := by induction l; [refl, simp only [*, reverse_cons, foldl_append, foldl_cons, foldl_nil, foldr]] theorem foldr_reverse (f : Ξ± β†’ Ξ² β†’ Ξ²) (a : Ξ²) (l : list Ξ±) : foldr f a (reverse l) = foldl (Ξ»x y, f y x) a l := let t := foldl_reverse (Ξ»x y, f y x) a (reverse l) in by rw reverse_reverse l at t; rwa t @[simp] theorem foldr_eta : βˆ€ (l : list Ξ±), foldr cons [] l = l | [] := rfl | (x::l) := by simp only [foldr_cons, foldr_eta l]; split; refl @[simp] theorem reverse_foldl {l : list Ξ±} : reverse (foldl (Ξ» t h, h :: t) [] l) = l := by rw ←foldr_reverse; simp @[simp] theorem foldl_map (g : Ξ² β†’ Ξ³) (f : Ξ± β†’ Ξ³ β†’ Ξ±) (a : Ξ±) (l : list Ξ²) : foldl f a (map g l) = foldl (Ξ»x y, f x (g y)) a l := by revert a; induction l; intros; [refl, simp only [*, map, foldl]] @[simp] theorem foldr_map (g : Ξ² β†’ Ξ³) (f : Ξ³ β†’ Ξ± β†’ Ξ±) (a : Ξ±) (l : list Ξ²) : foldr f a (map g l) = foldr (f ∘ g) a l := by revert a; induction l; intros; [refl, simp only [*, map, foldr]] theorem foldl_map' {Ξ± Ξ²: Type u} (g : Ξ± β†’ Ξ²) (f : Ξ± β†’ Ξ± β†’ Ξ±) (f' : Ξ² β†’ Ξ² β†’ Ξ²) (a : Ξ±) (l : list Ξ±) (h : βˆ€ x y, f' (g x) (g y) = g (f x y)) : list.foldl f' (g a) (l.map g) = g (list.foldl f a l) := begin induction l generalizing a, { simp }, { simp [l_ih, h] } end theorem foldr_map' {Ξ± Ξ²: Type u} (g : Ξ± β†’ Ξ²) (f : Ξ± β†’ Ξ± β†’ Ξ±) (f' : Ξ² β†’ Ξ² β†’ Ξ²) (a : Ξ±) (l : list Ξ±) (h : βˆ€ x y, f' (g x) (g y) = g (f x y)) : list.foldr f' (g a) (l.map g) = g (list.foldr f a l) := begin induction l generalizing a, { simp }, { simp [l_ih, h] } end theorem foldl_hom (l : list Ξ³) (f : Ξ± β†’ Ξ²) (op : Ξ± β†’ Ξ³ β†’ Ξ±) (op' : Ξ² β†’ Ξ³ β†’ Ξ²) (a : Ξ±) (h : βˆ€a x, f (op a x) = op' (f a) x) : foldl op' (f a) l = f (foldl op a l) := eq.symm $ by { revert a, induction l; intros; [refl, simp only [*, foldl]] } theorem foldr_hom (l : list Ξ³) (f : Ξ± β†’ Ξ²) (op : Ξ³ β†’ Ξ± β†’ Ξ±) (op' : Ξ³ β†’ Ξ² β†’ Ξ²) (a : Ξ±) (h : βˆ€x a, f (op x a) = op' x (f a)) : foldr op' (f a) l = f (foldr op a l) := by { revert a, induction l; intros; [refl, simp only [*, foldr]] } lemma injective_foldl_comp {Ξ± : Type*} {l : list (Ξ± β†’ Ξ±)} {f : Ξ± β†’ Ξ±} (hl : βˆ€ f ∈ l, function.injective f) (hf : function.injective f): function.injective (@list.foldl (Ξ± β†’ Ξ±) (Ξ± β†’ Ξ±) function.comp f l) := begin induction l generalizing f, { exact hf }, { apply l_ih (Ξ» _ h, hl _ (list.mem_cons_of_mem _ h)), apply function.injective.comp hf, apply hl _ (list.mem_cons_self _ _) } end /-- Induction principle for values produced by a `foldr`: if a property holds for the seed element `b : Ξ²` and for all incremental `op : Ξ± β†’ Ξ² β†’ Ξ²` performed on the elements `(a : Ξ±) ∈ l`. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def foldr_rec_on {C : Ξ² β†’ Sort*} (l : list Ξ±) (op : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) (hb : C b) (hl : βˆ€ (b : Ξ²) (hb : C b) (a : Ξ±) (ha : a ∈ l), C (op a b)) : C (foldr op b l) := begin induction l with hd tl IH, { exact hb }, { refine hl _ _ hd (mem_cons_self hd tl), refine IH _, intros y hy x hx, exact hl y hy x (mem_cons_of_mem hd hx) } end /-- Induction principle for values produced by a `foldl`: if a property holds for the seed element `b : Ξ²` and for all incremental `op : Ξ² β†’ Ξ± β†’ Ξ²` performed on the elements `(a : Ξ±) ∈ l`. The principle is given for a `Sort`-valued predicate, i.e., it can also be used to construct data. -/ def foldl_rec_on {C : Ξ² β†’ Sort*} (l : list Ξ±) (op : Ξ² β†’ Ξ± β†’ Ξ²) (b : Ξ²) (hb : C b) (hl : βˆ€ (b : Ξ²) (hb : C b) (a : Ξ±) (ha : a ∈ l), C (op b a)) : C (foldl op b l) := begin induction l with hd tl IH generalizing b, { exact hb }, { refine IH _ _ _, { intros y hy x hx, exact hl y hy x (mem_cons_of_mem hd hx) }, { exact hl b hb hd (mem_cons_self hd tl) } } end @[simp] lemma foldr_rec_on_nil {C : Ξ² β†’ Sort*} (op : Ξ± β†’ Ξ² β†’ Ξ²) (b) (hb : C b) (hl) : foldr_rec_on [] op b hb hl = hb := rfl @[simp] lemma foldr_rec_on_cons {C : Ξ² β†’ Sort*} (x : Ξ±) (l : list Ξ±) (op : Ξ± β†’ Ξ² β†’ Ξ²) (b) (hb : C b) (hl : βˆ€ (b : Ξ²) (hb : C b) (a : Ξ±) (ha : a ∈ (x :: l)), C (op a b)) : foldr_rec_on (x :: l) op b hb hl = hl _ (foldr_rec_on l op b hb (Ξ» b hb a ha, hl b hb a (mem_cons_of_mem _ ha))) x (mem_cons_self _ _) := rfl @[simp] lemma foldl_rec_on_nil {C : Ξ² β†’ Sort*} (op : Ξ² β†’ Ξ± β†’ Ξ²) (b) (hb : C b) (hl) : foldl_rec_on [] op b hb hl = hb := rfl /- scanl -/ section scanl variables {f : Ξ² β†’ Ξ± β†’ Ξ²} {b : Ξ²} {a : Ξ±} {l : list Ξ±} lemma length_scanl : βˆ€ a l, length (scanl f a l) = l.length + 1 | a [] := rfl | a (x :: l) := by erw [length_cons, length_cons, length_scanl] @[simp] lemma scanl_nil (b : Ξ²) : scanl f b nil = [b] := rfl @[simp] lemma scanl_cons : scanl f b (a :: l) = [b] ++ scanl f (f b a) l := by simp only [scanl, eq_self_iff_true, singleton_append, and_self] @[simp] lemma nth_zero_scanl : (scanl f b l).nth 0 = some b := begin cases l, { simp only [nth, scanl_nil] }, { simp only [nth, scanl_cons, singleton_append] } end @[simp] lemma nth_le_zero_scanl {h : 0 < (scanl f b l).length} : (scanl f b l).nth_le 0 h = b := begin cases l, { simp only [nth_le, scanl_nil] }, { simp only [nth_le, scanl_cons, singleton_append] } end lemma nth_succ_scanl {i : β„•} : (scanl f b l).nth (i + 1) = ((scanl f b l).nth i).bind (Ξ» x, (l.nth i).map (Ξ» y, f x y)) := begin induction l with hd tl hl generalizing b i, { symmetry, simp only [option.bind_eq_none', nth, forall_2_true_iff, not_false_iff, option.map_none', scanl_nil, option.not_mem_none, forall_true_iff] }, { simp only [nth, scanl_cons, singleton_append], cases i, { simp only [option.map_some', nth_zero_scanl, nth, option.some_bind'] }, { simp only [hl, nth] } } end lemma nth_le_succ_scanl {i : β„•} {h : i + 1 < (scanl f b l).length} : (scanl f b l).nth_le (i + 1) h = f ((scanl f b l).nth_le i (nat.lt_of_succ_lt h)) (l.nth_le i (nat.lt_of_succ_lt_succ (lt_of_lt_of_le h (le_of_eq (length_scanl b l))))) := begin induction i with i hi generalizing b l, { cases l, { simp only [length, zero_add, scanl_nil] at h, exact absurd h (lt_irrefl 1) }, { simp only [scanl_cons, singleton_append, nth_le_zero_scanl, nth_le] } }, { cases l, { simp only [length, add_lt_iff_neg_right, scanl_nil] at h, exact absurd h (not_lt_of_lt nat.succ_pos') }, { simp_rw scanl_cons, rw nth_le_append_right _, { simpa only [hi, length, succ_add_sub_one] }, { simp only [length, nat.zero_le, le_add_iff_nonneg_left] } } } end end scanl /- scanr -/ @[simp] theorem scanr_nil (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) : scanr f b [] = [b] := rfl @[simp] theorem scanr_aux_cons (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) : βˆ€ (a : Ξ±) (l : list Ξ±), scanr_aux f b (a::l) = (foldr f b (a::l), scanr f b l) | a [] := rfl | a (x::l) := let t := scanr_aux_cons x l in by simp only [scanr, scanr_aux, t, foldr_cons] @[simp] theorem scanr_cons (f : Ξ± β†’ Ξ² β†’ Ξ²) (b : Ξ²) (a : Ξ±) (l : list Ξ±) : scanr f b (a::l) = foldr f b (a::l) :: scanr f b l := by simp only [scanr, scanr_aux_cons, foldr_cons]; split; refl section foldl_eq_foldr -- foldl and foldr coincide when f is commutative and associative variables {f : Ξ± β†’ Ξ± β†’ Ξ±} (hcomm : commutative f) (hassoc : associative f) include hassoc theorem foldl1_eq_foldr1 : βˆ€ a b l, foldl f a (l++[b]) = foldr f b (a::l) | a b nil := rfl | a b (c :: l) := by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l]; rw hassoc include hcomm theorem foldl_eq_of_comm_of_assoc : βˆ€ a b l, foldl f a (b::l) = f b (foldl f a l) | a b nil := hcomm a b | a b (c::l) := by simp only [foldl_cons]; rw [← foldl_eq_of_comm_of_assoc, right_comm _ hcomm hassoc]; refl theorem foldl_eq_foldr : βˆ€ a l, foldl f a l = foldr f a l | a nil := rfl | a (b :: l) := by simp only [foldr_cons, foldl_eq_of_comm_of_assoc hcomm hassoc]; rw (foldl_eq_foldr a l) end foldl_eq_foldr section foldl_eq_foldlr' variables {f : Ξ± β†’ Ξ² β†’ Ξ±} variables hf : βˆ€ a b c, f (f a b) c = f (f a c) b include hf theorem foldl_eq_of_comm' : βˆ€ a b l, foldl f a (b::l) = f (foldl f a l) b | a b [] := rfl | a b (c :: l) := by rw [foldl,foldl,foldl,← foldl_eq_of_comm',foldl,hf] theorem foldl_eq_foldr' : βˆ€ a l, foldl f a l = foldr (flip f) a l | a [] := rfl | a (b :: l) := by rw [foldl_eq_of_comm' hf,foldr,foldl_eq_foldr']; refl end foldl_eq_foldlr' section foldl_eq_foldlr' variables {f : Ξ± β†’ Ξ² β†’ Ξ²} variables hf : βˆ€ a b c, f a (f b c) = f b (f a c) include hf theorem foldr_eq_of_comm' : βˆ€ a b l, foldr f a (b::l) = foldr f (f b a) l | a b [] := rfl | a b (c :: l) := by rw [foldr,foldr,foldr,hf,← foldr_eq_of_comm']; refl end foldl_eq_foldlr' section variables {op : Ξ± β†’ Ξ± β†’ Ξ±} [ha : is_associative Ξ± op] [hc : is_commutative Ξ± op] local notation a * b := op a b local notation l <*> a := foldl op a l include ha lemma foldl_assoc : βˆ€ {l : list Ξ±} {a₁ aβ‚‚}, l <*> (a₁ * aβ‚‚) = a₁ * (l <*> aβ‚‚) | [] a₁ aβ‚‚ := rfl | (a :: l) a₁ aβ‚‚ := calc a::l <*> (a₁ * aβ‚‚) = l <*> (a₁ * (aβ‚‚ * a)) : by simp only [foldl_cons, ha.assoc] ... = a₁ * (a::l <*> aβ‚‚) : by rw [foldl_assoc, foldl_cons] lemma foldl_op_eq_op_foldr_assoc : βˆ€{l : list Ξ±} {a₁ aβ‚‚}, (l <*> a₁) * aβ‚‚ = a₁ * l.foldr (*) aβ‚‚ | [] a₁ aβ‚‚ := rfl | (a :: l) a₁ aβ‚‚ := by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc]; rw [foldl_op_eq_op_foldr_assoc] include hc lemma foldl_assoc_comm_cons {l : list Ξ±} {a₁ aβ‚‚} : (a₁ :: l) <*> aβ‚‚ = a₁ * (l <*> aβ‚‚) := by rw [foldl_cons, hc.comm, foldl_assoc] end /-! ### mfoldl, mfoldr, mmap -/ section mfoldl_mfoldr variables {m : Type v β†’ Type w} [monad m] @[simp] theorem mfoldl_nil (f : Ξ² β†’ Ξ± β†’ m Ξ²) {b} : mfoldl f b [] = pure b := rfl @[simp] theorem mfoldr_nil (f : Ξ± β†’ Ξ² β†’ m Ξ²) {b} : mfoldr f b [] = pure b := rfl @[simp] theorem mfoldl_cons {f : Ξ² β†’ Ξ± β†’ m Ξ²} {b a l} : mfoldl f b (a :: l) = f b a >>= Ξ» b', mfoldl f b' l := rfl @[simp] theorem mfoldr_cons {f : Ξ± β†’ Ξ² β†’ m Ξ²} {b a l} : mfoldr f b (a :: l) = mfoldr f b l >>= f a := rfl theorem mfoldr_eq_foldr (f : Ξ± β†’ Ξ² β†’ m Ξ²) (b l) : mfoldr f b l = foldr (Ξ» a mb, mb >>= f a) (pure b) l := by induction l; simp * attribute [simp] mmap mmap' variables [is_lawful_monad m] theorem mfoldl_eq_foldl (f : Ξ² β†’ Ξ± β†’ m Ξ²) (b l) : mfoldl f b l = foldl (Ξ» mb a, mb >>= Ξ» b, f b a) (pure b) l := begin suffices h : βˆ€ (mb : m Ξ²), (mb >>= Ξ» b, mfoldl f b l) = foldl (Ξ» mb a, mb >>= Ξ» b, f b a) mb l, by simp [←h (pure b)], induction l; intro, { simp }, { simp only [mfoldl, foldl, ←l_ih] with monad_norm } end @[simp] theorem mfoldl_append {f : Ξ² β†’ Ξ± β†’ m Ξ²} : βˆ€ {b l₁ lβ‚‚}, mfoldl f b (l₁ ++ lβ‚‚) = mfoldl f b l₁ >>= Ξ» x, mfoldl f x lβ‚‚ | _ [] _ := by simp only [nil_append, mfoldl_nil, pure_bind] | _ (_::_) _ := by simp only [cons_append, mfoldl_cons, mfoldl_append, is_lawful_monad.bind_assoc] @[simp] theorem mfoldr_append {f : Ξ± β†’ Ξ² β†’ m Ξ²} : βˆ€ {b l₁ lβ‚‚}, mfoldr f b (l₁ ++ lβ‚‚) = mfoldr f b lβ‚‚ >>= Ξ» x, mfoldr f x l₁ | _ [] _ := by simp only [nil_append, mfoldr_nil, bind_pure] | _ (_::_) _ := by simp only [mfoldr_cons, cons_append, mfoldr_append, is_lawful_monad.bind_assoc] end mfoldl_mfoldr /-! ### prod and sum -/ -- list.sum was already defined in defs.lean, but we couldn't tag it with `to_additive` yet. attribute [to_additive] list.prod section monoid variables [monoid Ξ±] {l l₁ lβ‚‚ : list Ξ±} {a : Ξ±} @[simp, to_additive] theorem prod_nil : ([] : list Ξ±).prod = 1 := rfl @[to_additive] theorem prod_singleton : [a].prod = a := one_mul a @[simp, to_additive] theorem prod_cons : (a::l).prod = a * l.prod := calc (a::l).prod = foldl (*) (a * 1) l : by simp only [list.prod, foldl_cons, one_mul, mul_one] ... = _ : foldl_assoc @[simp, to_additive] theorem prod_append : (l₁ ++ lβ‚‚).prod = l₁.prod * lβ‚‚.prod := calc (l₁ ++ lβ‚‚).prod = foldl (*) (foldl (*) 1 l₁ * 1) lβ‚‚ : by simp [list.prod] ... = l₁.prod * lβ‚‚.prod : foldl_assoc @[to_additive] theorem prod_concat : (l.concat a).prod = l.prod * a := by rw [concat_eq_append, prod_append, prod_cons, prod_nil, mul_one] @[simp, to_additive] theorem prod_join {l : list (list Ξ±)} : l.join.prod = (l.map list.prod).prod := by induction l; [refl, simp only [*, list.join, map, prod_append, prod_cons]] /-- If zero is an element of a list `L`, then `list.prod L = 0`. If the domain is a nontrivial monoid with zero with no divisors, then this implication becomes an `iff`, see `list.prod_eq_zero_iff`. -/ theorem prod_eq_zero {Mβ‚€ : Type*} [monoid_with_zero Mβ‚€] {L : list Mβ‚€} (h : (0 : Mβ‚€) ∈ L) : L.prod = 0 := begin induction L with a L ihL, { exact absurd h (not_mem_nil _) }, { rw prod_cons, cases (mem_cons_iff _ _ _).1 h with ha hL, exacts [mul_eq_zero_of_left ha.symm _, mul_eq_zero_of_right _ (ihL hL)] } end /-- Product of elements of a list `L` equals zero if and only if `0 ∈ L`. See also `list.prod_eq_zero` for an implication that needs weaker typeclass assumptions. -/ @[simp] theorem prod_eq_zero_iff {Mβ‚€ : Type*} [monoid_with_zero Mβ‚€] [nontrivial Mβ‚€] [no_zero_divisors Mβ‚€] {L : list Mβ‚€} : L.prod = 0 ↔ (0 : Mβ‚€) ∈ L := begin induction L with a L ihL, { simp }, { rw [prod_cons, mul_eq_zero, ihL, mem_cons_iff, eq_comm] } end theorem prod_ne_zero {Mβ‚€ : Type*} [monoid_with_zero Mβ‚€] [nontrivial Mβ‚€] [no_zero_divisors Mβ‚€] {L : list Mβ‚€} (hL : (0 : Mβ‚€) βˆ‰ L) : L.prod β‰  0 := mt prod_eq_zero_iff.1 hL @[to_additive] theorem prod_eq_foldr : l.prod = foldr (*) 1 l := list.rec_on l rfl $ Ξ» a l ihl, by rw [prod_cons, foldr_cons, ihl] @[to_additive] theorem prod_hom_rel {Ξ± Ξ² Ξ³ : Type*} [monoid Ξ²] [monoid Ξ³] (l : list Ξ±) {r : Ξ² β†’ Ξ³ β†’ Prop} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} (h₁ : r 1 1) (hβ‚‚ : βˆ€β¦ƒa b c⦄, r b c β†’ r (f a * b) (g a * c)) : r (l.map f).prod (l.map g).prod := list.rec_on l h₁ (Ξ» a l hl, by simp only [map_cons, prod_cons, hβ‚‚ hl]) @[to_additive] theorem prod_hom [monoid Ξ²] (l : list Ξ±) (f : Ξ± β†’* Ξ²) : (l.map f).prod = f l.prod := by { simp only [prod, foldl_map, f.map_one.symm], exact l.foldl_hom _ _ _ 1 f.map_mul } @[to_additive] lemma prod_is_unit [monoid Ξ²] : Ξ  {L : list Ξ²} (u : βˆ€ m ∈ L, is_unit m), is_unit L.prod | [] _ := by simp | (h :: t) u := begin simp only [list.prod_cons], exact is_unit.mul (u h (mem_cons_self h t)) (prod_is_unit (Ξ» m mt, u m (mem_cons_of_mem h mt))) end @[simp, to_additive] lemma prod_take_mul_prod_drop : βˆ€ (L : list Ξ±) (i : β„•), (L.take i).prod * (L.drop i).prod = L.prod | [] i := by simp | L 0 := by simp | (h :: t) (n+1) := by { dsimp, rw [prod_cons, prod_cons, mul_assoc, prod_take_mul_prod_drop], } @[simp, to_additive] lemma prod_take_succ : βˆ€ (L : list Ξ±) (i : β„•) (p), (L.take (i + 1)).prod = (L.take i).prod * L.nth_le i p | [] i p := by cases p | (h :: t) 0 _ := by simp | (h :: t) (n+1) _ := by { dsimp, rw [prod_cons, prod_cons, prod_take_succ, mul_assoc], } /-- A list with product not one must have positive length. -/ lemma length_pos_of_prod_ne_one (L : list Ξ±) (h : L.prod β‰  1) : 0 < L.length := by { cases L, { simp at h, cases h, }, { simp, }, } @[to_additive] lemma prod_update_nth : βˆ€ (L : list Ξ±) (n : β„•) (a : Ξ±), (L.update_nth n a).prod = (L.take n).prod * (if n < L.length then a else 1) * (L.drop (n + 1)).prod | (x::xs) 0 a := by simp [update_nth] | (x::xs) (i+1) a := by simp [update_nth, prod_update_nth xs i a, mul_assoc] | [] _ _ := by simp [update_nth, (nat.zero_le _).not_lt] open opposite lemma _root_.opposite.op_list_prod : βˆ€ (l : list Ξ±), op (l.prod) = (l.map op).reverse.prod | [] := rfl | (x :: xs) := by rw [list.prod_cons, list.map_cons, list.reverse_cons', list.prod_concat, op_mul, _root_.opposite.op_list_prod] lemma _root_.opposite.unop_list_prod : βˆ€ (l : list Ξ±α΅’α΅–), (l.prod).unop = (l.map unop).reverse.prod | [] := rfl | (x :: xs) := by rw [list.prod_cons, list.map_cons, list.reverse_cons', list.prod_concat, unop_mul, _root_.opposite.unop_list_prod] end monoid section group variables [group Ξ±] /-- This is the `list.prod` version of `mul_inv_rev` -/ @[to_additive "This is the `list.sum` version of `add_neg_rev`"] lemma prod_inv_reverse : βˆ€ (L : list Ξ±), L.prod⁻¹ = (L.map (Ξ» x, x⁻¹)).reverse.prod | [] := by simp | (x :: xs) := by simp [prod_inv_reverse xs] /-- A non-commutative variant of `list.prod_reverse` -/ @[to_additive "A non-commutative variant of `list.sum_reverse`"] lemma prod_reverse_noncomm : βˆ€ (L : list Ξ±), L.reverse.prod = (L.map (Ξ» x, x⁻¹)).prod⁻¹ := by simp [prod_inv_reverse] /-- Counterpart to `list.prod_take_succ` when we have an inverse operation -/ @[simp, to_additive /-"Counterpart to `list.sum_take_succ` when we have an negation operation"-/] lemma prod_drop_succ : βˆ€ (L : list Ξ±) (i : β„•) (p), (L.drop (i + 1)).prod = (L.nth_le i p)⁻¹ * (L.drop i).prod | [] i p := false.elim (nat.not_lt_zero _ p) | (x :: xs) 0 p := by simp | (x :: xs) (i + 1) p := prod_drop_succ xs i _ end group section comm_group variables [comm_group Ξ±] /-- This is the `list.prod` version of `mul_inv` -/ @[to_additive "This is the `list.sum` version of `add_neg`"] lemma prod_inv : βˆ€ (L : list Ξ±), L.prod⁻¹ = (L.map (Ξ» x, x⁻¹)).prod | [] := by simp | (x :: xs) := by simp [mul_comm, prod_inv xs] /-- Alternative version of `list.prod_update_nth` when the list is over a group -/ @[to_additive /-"Alternative version of `list.sum_update_nth` when the list is over a group"-/] lemma prod_update_nth' (L : list Ξ±) (n : β„•) (a : Ξ±) : (L.update_nth n a).prod = L.prod * (if hn : n < L.length then (L.nth_le n hn)⁻¹ * a else 1) := begin refine (prod_update_nth L n a).trans _, split_ifs with hn hn, { rw [mul_comm _ a, mul_assoc a, prod_drop_succ L n hn, mul_comm _ (drop n L).prod, ← mul_assoc (take n L).prod, prod_take_mul_prod_drop, mul_comm a, mul_assoc] }, { simp only [take_all_of_le (le_of_not_lt hn), prod_nil, mul_one, drop_eq_nil_of_le ((le_of_not_lt hn).trans n.le_succ)] } end end comm_group lemma eq_of_sum_take_eq [add_left_cancel_monoid Ξ±] {L L' : list Ξ±} (h : L.length = L'.length) (h' : βˆ€ i ≀ L.length, (L.take i).sum = (L'.take i).sum) : L = L' := begin apply ext_le h (Ξ» i h₁ hβ‚‚, _), have : (L.take (i + 1)).sum = (L'.take (i + 1)).sum := h' _ (nat.succ_le_of_lt h₁), rw [sum_take_succ L i h₁, sum_take_succ L' i hβ‚‚, h' i (le_of_lt h₁)] at this, exact add_left_cancel this end lemma monotone_sum_take [canonically_ordered_add_monoid Ξ±] (L : list Ξ±) : monotone (Ξ» i, (L.take i).sum) := begin apply monotone_nat_of_le_succ (Ξ» n, _), by_cases h : n < L.length, { rw sum_take_succ _ _ h, exact le_self_add }, { push_neg at h, simp [take_all_of_le h, take_all_of_le (le_trans h (nat.le_succ _))] } end @[to_additive sum_nonneg] lemma one_le_prod_of_one_le [ordered_comm_monoid Ξ±] {l : list Ξ±} (hl₁ : βˆ€ x ∈ l, (1 : Ξ±) ≀ x) : 1 ≀ l.prod := begin induction l with hd tl ih, { simp }, rw prod_cons, exact one_le_mul (hl₁ hd (mem_cons_self hd tl)) (ih (Ξ» x h, hl₁ x (mem_cons_of_mem hd h))), end @[to_additive] lemma single_le_prod [ordered_comm_monoid Ξ±] {l : list Ξ±} (hl₁ : βˆ€ x ∈ l, (1 : Ξ±) ≀ x) : βˆ€ x ∈ l, x ≀ l.prod := begin induction l, { simp }, simp_rw [prod_cons, forall_mem_cons] at ⊒ hl₁, split, { exact le_mul_of_one_le_right' (one_le_prod_of_one_le hl₁.2) }, { exact Ξ» x H, le_mul_of_one_le_of_le hl₁.1 (l_ih hl₁.right x H) }, end @[to_additive all_zero_of_le_zero_le_of_sum_eq_zero] lemma all_one_of_le_one_le_of_prod_eq_one [ordered_comm_monoid Ξ±] {l : list Ξ±} (hl₁ : βˆ€ x ∈ l, (1 : Ξ±) ≀ x) (hlβ‚‚ : l.prod = 1) : βˆ€ x ∈ l, x = (1 : Ξ±) := Ξ» x hx, le_antisymm (hlβ‚‚ β–Έ single_le_prod hl₁ _ hx) (hl₁ x hx) lemma sum_eq_zero_iff [canonically_ordered_add_monoid Ξ±] (l : list Ξ±) : l.sum = 0 ↔ βˆ€ x ∈ l, x = (0 : Ξ±) := ⟨all_zero_of_le_zero_le_of_sum_eq_zero (Ξ» _ _, zero_le _), begin induction l, { simp }, { intro h, rw [sum_cons, add_eq_zero_iff], rw forall_mem_cons at h, exact ⟨h.1, l_ih h.2⟩ }, end⟩ /-- A list with sum not zero must have positive length. -/ lemma length_pos_of_sum_ne_zero [add_monoid Ξ±] (L : list Ξ±) (h : L.sum β‰  0) : 0 < L.length := by { cases L, { simp at h, cases h, }, { simp, }, } /-- If all elements in a list are bounded below by `1`, then the length of the list is bounded by the sum of the elements. -/ lemma length_le_sum_of_one_le (L : list β„•) (h : βˆ€ i ∈ L, 1 ≀ i) : L.length ≀ L.sum := begin induction L with j L IH h, { simp }, rw [sum_cons, length, add_comm], exact add_le_add (h _ (set.mem_insert _ _)) (IH (Ξ» i hi, h i (set.mem_union_right _ hi))) end -- Now we tie those lemmas back to their multiplicative versions. attribute [to_additive] prod_take_mul_prod_drop prod_take_succ length_pos_of_prod_ne_one /-- A list with positive sum must have positive length. -/ -- This is an easy consequence of `length_pos_of_sum_ne_zero`, but often useful in applications. lemma length_pos_of_sum_pos [ordered_cancel_add_comm_monoid Ξ±] (L : list Ξ±) (h : 0 < L.sum) : 0 < L.length := length_pos_of_sum_ne_zero L (ne_of_gt h) -- TODO: develop theory of tropical rings lemma sum_le_foldr_max [add_monoid Ξ±] [add_monoid Ξ²] [linear_order Ξ²] (f : Ξ± β†’ Ξ²) (h0 : f 0 ≀ 0) (hadd : βˆ€ x y, f (x + y) ≀ max (f x) (f y)) (l : list Ξ±) : f l.sum ≀ (l.map f).foldr max 0 := begin induction l with hd tl IH, { simpa using h0 }, { simp only [list.sum_cons, list.foldr_map, le_max_iff, list.foldr] at IH ⊒, cases le_or_lt (f tl.sum) (f hd), { left, refine (hadd _ _).trans _, simpa using h }, { right, refine (hadd _ _).trans _, simp only [IH, max_le_iff, and_true, h.le.trans IH] } } end @[simp, to_additive] theorem prod_erase [decidable_eq Ξ±] [comm_monoid Ξ±] {a} : Ξ  {l : list Ξ±}, a ∈ l β†’ a * (l.erase a).prod = l.prod | (b::l) h := begin rcases decidable.list.eq_or_ne_mem_of_mem h with rfl | ⟨ne, h⟩, { simp only [list.erase, if_pos, prod_cons] }, { simp only [list.erase, if_neg (mt eq.symm ne), prod_cons, prod_erase h, mul_left_comm a b] } end lemma dvd_prod [comm_monoid Ξ±] {a} {l : list Ξ±} (ha : a ∈ l) : a ∣ l.prod := let ⟨s, t, h⟩ := mem_split ha in by rw [h, prod_append, prod_cons, mul_left_comm]; exact dvd_mul_right _ _ @[simp] theorem sum_const_nat (m n : β„•) : sum (list.repeat m n) = m * n := by induction n; [refl, simp only [*, repeat_succ, sum_cons, nat.mul_succ, add_comm]] theorem dvd_sum [comm_semiring Ξ±] {a} {l : list Ξ±} (h : βˆ€ x ∈ l, a ∣ x) : a ∣ l.sum := begin induction l with x l ih, { exact dvd_zero _ }, { rw [list.sum_cons], exact dvd_add (h _ (mem_cons_self _ _)) (ih (Ξ» x hx, h x (mem_cons_of_mem _ hx))) } end @[simp] theorem length_join (L : list (list Ξ±)) : length (join L) = sum (map length L) := by induction L; [refl, simp only [*, join, map, sum_cons, length_append]] @[simp] theorem length_bind (l : list Ξ±) (f : Ξ± β†’ list Ξ²) : length (list.bind l f) = sum (map (length ∘ f) l) := by rw [list.bind, length_join, map_map] lemma exists_lt_of_sum_lt [linear_ordered_cancel_add_comm_monoid Ξ²] {l : list Ξ±} (f g : Ξ± β†’ Ξ²) (h : (l.map f).sum < (l.map g).sum) : βˆƒ x ∈ l, f x < g x := begin induction l with x l, { exfalso, exact lt_irrefl _ h }, { by_cases h' : f x < g x, exact ⟨x, mem_cons_self _ _, h'⟩, rcases l_ih _ with ⟨y, h1y, h2y⟩, refine ⟨y, mem_cons_of_mem x h1y, h2y⟩, simp at h, exact lt_of_add_lt_add_left (lt_of_lt_of_le h $ add_le_add_right (le_of_not_gt h') _) } end lemma exists_le_of_sum_le [linear_ordered_cancel_add_comm_monoid Ξ²] {l : list Ξ±} (hl : l β‰  []) (f g : Ξ± β†’ Ξ²) (h : (l.map f).sum ≀ (l.map g).sum) : βˆƒ x ∈ l, f x ≀ g x := begin cases l with x l, { contradiction }, { by_cases h' : f x ≀ g x, exact ⟨x, mem_cons_self _ _, h'⟩, rcases exists_lt_of_sum_lt f g _ with ⟨y, h1y, h2y⟩, exact ⟨y, mem_cons_of_mem x h1y, le_of_lt h2y⟩, simp at h, exact lt_of_add_lt_add_left (lt_of_le_of_lt h $ add_lt_add_right (lt_of_not_ge h') _) } end -- Several lemmas about sum/head/tail for `list β„•`. -- These are hard to generalize well, as they rely on the fact that `default β„• = 0`. -- We'd like to state this as `L.head * L.tail.prod = L.prod`, -- but because `L.head` relies on an inhabited instances and -- returns a garbage value for the empty list, this is not possible. -- Instead we write the statement in terms of `(L.nth 0).get_or_else 1`, -- and below, restate the lemma just for `β„•`. @[to_additive] lemma head_mul_tail_prod' [monoid Ξ±] (L : list Ξ±) : (L.nth 0).get_or_else 1 * L.tail.prod = L.prod := by cases L; simp lemma head_add_tail_sum (L : list β„•) : L.head + L.tail.sum = L.sum := by { cases L, { simp, refl, }, { simp, }, } lemma head_le_sum (L : list β„•) : L.head ≀ L.sum := nat.le.intro (head_add_tail_sum L) lemma tail_sum (L : list β„•) : L.tail.sum = L.sum - L.head := by rw [← head_add_tail_sum L, add_comm, add_tsub_cancel_right] section variables {G : Type*} [comm_group G] attribute [to_additive] alternating_prod @[simp, to_additive] lemma alternating_prod_nil : alternating_prod ([] : list G) = 1 := rfl @[simp, to_additive] lemma alternating_prod_singleton (g : G) : alternating_prod [g] = g := rfl @[simp, to_additive alternating_sum_cons_cons'] lemma alternating_prod_cons_cons (g h : G) (l : list G) : alternating_prod (g :: h :: l) = g * h⁻¹ * alternating_prod l := rfl lemma alternating_sum_cons_cons {G : Type*} [add_comm_group G] (g h : G) (l : list G) : alternating_sum (g :: h :: l) = g - h + alternating_sum l := by rw [sub_eq_add_neg, alternating_sum] end /-! ### join -/ attribute [simp] join @[simp] lemma join_nil {Ξ± : Type u} : [([] : list Ξ±)].join = [] := rfl @[simp] theorem join_eq_nil : βˆ€ {L : list (list Ξ±)}, join L = [] ↔ βˆ€ l ∈ L, l = [] | [] := iff_of_true rfl (forall_mem_nil _) | (l::L) := by simp only [join, append_eq_nil, join_eq_nil, forall_mem_cons] @[simp] theorem join_append (L₁ Lβ‚‚ : list (list Ξ±)) : join (L₁ ++ Lβ‚‚) = join L₁ ++ join Lβ‚‚ := by induction L₁; [refl, simp only [*, join, cons_append, append_assoc]] @[simp] theorem join_filter_empty_eq_ff [decidable_pred (Ξ» l : list Ξ±, l.empty = ff)] : βˆ€ {L : list (list Ξ±)}, join (L.filter (Ξ» l, l.empty = ff)) = L.join | [] := rfl | ([]::L) := by simp [@join_filter_empty_eq_ff L] | ((a::l)::L) := by simp [@join_filter_empty_eq_ff L] @[simp] theorem join_filter_ne_nil [decidable_pred (Ξ» l : list Ξ±, l β‰  [])] {L : list (list Ξ±)} : join (L.filter (Ξ» l, l β‰  [])) = L.join := by simp [join_filter_empty_eq_ff, ← empty_iff_eq_nil] lemma join_join (l : list (list (list Ξ±))) : l.join.join = (l.map join).join := by { induction l, simp, simp [l_ih] } /-- In a join, taking the first elements up to an index which is the sum of the lengths of the first `i` sublists, is the same as taking the join of the first `i` sublists. -/ lemma take_sum_join (L : list (list Ξ±)) (i : β„•) : L.join.take ((L.map length).take i).sum = (L.take i).join := begin induction L generalizing i, { simp }, cases i, { simp }, simp [take_append, L_ih] end /-- In a join, dropping all the elements up to an index which is the sum of the lengths of the first `i` sublists, is the same as taking the join after dropping the first `i` sublists. -/ lemma drop_sum_join (L : list (list Ξ±)) (i : β„•) : L.join.drop ((L.map length).take i).sum = (L.drop i).join := begin induction L generalizing i, { simp }, cases i, { simp }, simp [drop_append, L_ih], end /-- Taking only the first `i+1` elements in a list, and then dropping the first `i` ones, one is left with a list of length `1` made of the `i`-th element of the original list. -/ lemma drop_take_succ_eq_cons_nth_le (L : list Ξ±) {i : β„•} (hi : i < L.length) : (L.take (i+1)).drop i = [nth_le L i hi] := begin induction L generalizing i, { simp only [length] at hi, exact (nat.not_succ_le_zero i hi).elim }, cases i, { simp }, have : i < L_tl.length, { simp at hi, exact nat.lt_of_succ_lt_succ hi }, simp [L_ih this], refl end /-- In a join of sublists, taking the slice between the indices `A` and `B - 1` gives back the original sublist of index `i` if `A` is the sum of the lenghts of sublists of index `< i`, and `B` is the sum of the lengths of sublists of index `≀ i`. -/ lemma drop_take_succ_join_eq_nth_le (L : list (list Ξ±)) {i : β„•} (hi : i < L.length) : (L.join.take ((L.map length).take (i+1)).sum).drop ((L.map length).take i).sum = nth_le L i hi := begin have : (L.map length).take i = ((L.take (i+1)).map length).take i, by simp [map_take, take_take], simp [take_sum_join, this, drop_sum_join, drop_take_succ_eq_cons_nth_le _ hi] end /-- Auxiliary lemma to control elements in a join. -/ lemma sum_take_map_length_lt1 (L : list (list Ξ±)) {i j : β„•} (hi : i < L.length) (hj : j < (nth_le L i hi).length) : ((L.map length).take i).sum + j < ((L.map length).take (i+1)).sum := by simp [hi, sum_take_succ, hj] /-- Auxiliary lemma to control elements in a join. -/ lemma sum_take_map_length_lt2 (L : list (list Ξ±)) {i j : β„•} (hi : i < L.length) (hj : j < (nth_le L i hi).length) : ((L.map length).take i).sum + j < L.join.length := begin convert lt_of_lt_of_le (sum_take_map_length_lt1 L hi hj) (monotone_sum_take _ hi), have : L.length = (L.map length).length, by simp, simp [this, -length_map] end /-- The `n`-th element in a join of sublists is the `j`-th element of the `i`th sublist, where `n` can be obtained in terms of `i` and `j` by adding the lengths of all the sublists of index `< i`, and adding `j`. -/ lemma nth_le_join (L : list (list Ξ±)) {i j : β„•} (hi : i < L.length) (hj : j < (nth_le L i hi).length) : nth_le L.join (((L.map length).take i).sum + j) (sum_take_map_length_lt2 L hi hj) = nth_le (nth_le L i hi) j hj := by rw [nth_le_take L.join (sum_take_map_length_lt2 L hi hj) (sum_take_map_length_lt1 L hi hj), nth_le_drop, nth_le_of_eq (drop_take_succ_join_eq_nth_le L hi)] /-- Two lists of sublists are equal iff their joins coincide, as well as the lengths of the sublists. -/ theorem eq_iff_join_eq (L L' : list (list Ξ±)) : L = L' ↔ L.join = L'.join ∧ map length L = map length L' := begin refine ⟨λ H, by simp [H], _⟩, rintros ⟨join_eq, length_eq⟩, apply ext_le, { have : length (map length L) = length (map length L'), by rw length_eq, simpa using this }, { assume n h₁ hβ‚‚, rw [← drop_take_succ_join_eq_nth_le, ← drop_take_succ_join_eq_nth_le, join_eq, length_eq] } end /-! ### intersperse -/ @[simp] lemma intersperse_nil {Ξ± : Type u} (a : Ξ±) : intersperse a [] = [] := rfl @[simp] lemma intersperse_singleton {Ξ± : Type u} (a b : Ξ±) : intersperse a [b] = [b] := rfl @[simp] lemma intersperse_cons_cons {Ξ± : Type u} (a b c : Ξ±) (tl : list Ξ±) : intersperse a (b :: c :: tl) = b :: a :: intersperse a (c :: tl) := rfl /-! ### split_at and split_on -/ @[simp] theorem split_at_eq_take_drop : βˆ€ (n : β„•) (l : list Ξ±), split_at n l = (take n l, drop n l) | 0 a := rfl | (succ n) [] := rfl | (succ n) (x :: xs) := by simp only [split_at, split_at_eq_take_drop n xs, take, drop] @[simp] lemma split_on_nil {Ξ± : Type u} [decidable_eq Ξ±] (a : Ξ±) : [].split_on a = [[]] := rfl /-- An auxiliary definition for proving a specification lemma for `split_on_p`. `split_on_p_aux' P xs ys` splits the list `ys ++ xs` at every element satisfying `P`, where `ys` is an accumulating parameter for the initial segment of elements not satisfying `P`. -/ def split_on_p_aux' {Ξ± : Type u} (P : Ξ± β†’ Prop) [decidable_pred P] : list Ξ± β†’ list Ξ± β†’ list (list Ξ±) | [] xs := [xs] | (h :: t) xs := if P h then xs :: split_on_p_aux' t [] else split_on_p_aux' t (xs ++ [h]) lemma split_on_p_aux_eq {Ξ± : Type u} (P : Ξ± β†’ Prop) [decidable_pred P] (xs ys : list Ξ±) : split_on_p_aux' P xs ys = split_on_p_aux P xs ((++) ys) := begin induction xs with a t ih generalizing ys; simp! only [append_nil, eq_self_iff_true, and_self], split_ifs; rw ih, { refine ⟨rfl, rfl⟩ }, { congr, ext, simp } end lemma split_on_p_aux_nil {Ξ± : Type u} (P : Ξ± β†’ Prop) [decidable_pred P] (xs : list Ξ±) : split_on_p_aux P xs id = split_on_p_aux' P xs [] := by { rw split_on_p_aux_eq, refl } /-- The original list `L` can be recovered by joining the lists produced by `split_on_p p L`, interspersed with the elements `L.filter p`. -/ lemma split_on_p_spec {Ξ± : Type u} (p : Ξ± β†’ Prop) [decidable_pred p] (as : list Ξ±) : join (zip_with (++) (split_on_p p as) ((as.filter p).map (Ξ» x, [x]) ++ [[]])) = as := begin rw [split_on_p, split_on_p_aux_nil], suffices : βˆ€ xs, join (zip_with (++) (split_on_p_aux' p as xs) ((as.filter p).map(Ξ» x, [x]) ++ [[]])) = xs ++ as, { rw this, refl }, induction as; intro; simp! only [split_on_p_aux', append_nil], split_ifs; simp [zip_with, join, *], end /-! ### all & any -/ @[simp] theorem all_nil (p : Ξ± β†’ bool) : all [] p = tt := rfl @[simp] theorem all_cons (p : Ξ± β†’ bool) (a : Ξ±) (l : list Ξ±) : all (a::l) p = (p a && all l p) := rfl theorem all_iff_forall {p : Ξ± β†’ bool} {l : list Ξ±} : all l p ↔ βˆ€ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_true rfl (forall_mem_nil _) }, simp only [all_cons, band_coe_iff, ih, forall_mem_cons] end theorem all_iff_forall_prop {p : Ξ± β†’ Prop} [decidable_pred p] {l : list Ξ±} : all l (Ξ» a, p a) ↔ βˆ€ a ∈ l, p a := by simp only [all_iff_forall, bool.of_to_bool_iff] @[simp] theorem any_nil (p : Ξ± β†’ bool) : any [] p = ff := rfl @[simp] theorem any_cons (p : Ξ± β†’ bool) (a : Ξ±) (l : list Ξ±) : any (a::l) p = (p a || any l p) := rfl theorem any_iff_exists {p : Ξ± β†’ bool} {l : list Ξ±} : any l p ↔ βˆƒ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_false bool.not_ff (not_exists_mem_nil _) }, simp only [any_cons, bor_coe_iff, ih, exists_mem_cons_iff] end theorem any_iff_exists_prop {p : Ξ± β†’ Prop} [decidable_pred p] {l : list Ξ±} : any l (Ξ» a, p a) ↔ βˆƒ a ∈ l, p a := by simp [any_iff_exists] theorem any_of_mem {p : Ξ± β†’ bool} {a : Ξ±} {l : list Ξ±} (h₁ : a ∈ l) (hβ‚‚ : p a) : any l p := any_iff_exists.2 ⟨_, h₁, hβ‚‚βŸ© @[priority 500] instance decidable_forall_mem {p : Ξ± β†’ Prop} [decidable_pred p] (l : list Ξ±) : decidable (βˆ€ x ∈ l, p x) := decidable_of_iff _ all_iff_forall_prop instance decidable_exists_mem {p : Ξ± β†’ Prop} [decidable_pred p] (l : list Ξ±) : decidable (βˆƒ x ∈ l, p x) := decidable_of_iff _ any_iff_exists_prop /-! ### map for partial functions -/ /-- Partial map. If `f : Ξ  a, p a β†’ Ξ²` is a partial function defined on `a : Ξ±` satisfying `p`, then `pmap f l h` is essentially the same as `map f l` but is defined only when all members of `l` satisfy `p`, using the proof to apply `f`. -/ @[simp] def pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) : Ξ  l : list Ξ±, (βˆ€ a ∈ l, p a) β†’ list Ξ² | [] H := [] | (a::l) H := f a (forall_mem_cons.1 H).1 :: pmap l (forall_mem_cons.1 H).2 /-- "Attach" the proof that the elements of `l` are in `l` to produce a new list with the same elements but in the type `{x // x ∈ l}`. -/ def attach (l : list Ξ±) : list {x // x ∈ l} := pmap subtype.mk l (Ξ» a, id) theorem sizeof_lt_sizeof_of_mem [has_sizeof Ξ±] {x : Ξ±} {l : list Ξ±} (hx : x ∈ l) : sizeof x < sizeof l := begin induction l with h t ih; cases hx, { rw hx, exact lt_add_of_lt_of_nonneg (lt_one_add _) (nat.zero_le _) }, { exact lt_add_of_pos_of_le (zero_lt_one_add _) (le_of_lt (ih hx)) } end @[simp] theorem pmap_eq_map (p : Ξ± β†’ Prop) (f : Ξ± β†’ Ξ²) (l : list Ξ±) (H) : @pmap _ _ p (Ξ» a _, f a) l H = map f l := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_congr {p q : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {g : Ξ  a, q a β†’ Ξ²} (l : list Ξ±) {H₁ Hβ‚‚} (h : βˆ€ a h₁ hβ‚‚, f a h₁ = g a hβ‚‚) : pmap f l H₁ = pmap g l Hβ‚‚ := by induction l with _ _ ih; [refl, rw [pmap, pmap, h, ih]] theorem map_pmap {p : Ξ± β†’ Prop} (g : Ξ² β†’ Ξ³) (f : Ξ  a, p a β†’ Ξ²) (l H) : map g (pmap f l H) = pmap (Ξ» a h, g (f a h)) l H := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_map {p : Ξ² β†’ Prop} (g : βˆ€ b, p b β†’ Ξ³) (f : Ξ± β†’ Ξ²) (l H) : pmap g (map f l) H = pmap (Ξ» a h, g (f a) h) l (Ξ» a h, H _ (mem_map_of_mem _ h)) := by induction l; [refl, simp only [*, pmap, map]]; split; refl theorem pmap_eq_map_attach {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) (l H) : pmap f l H = l.attach.map (Ξ» x, f x.1 (H _ x.2)) := by rw [attach, map_pmap]; exact pmap_congr l (Ξ» a h₁ hβ‚‚, rfl) theorem attach_map_val (l : list Ξ±) : l.attach.map subtype.val = l := by rw [attach, map_pmap]; exact (pmap_eq_map _ _ _ _).trans (map_id l) @[simp] theorem mem_attach (l : list Ξ±) : βˆ€ x, x ∈ l.attach | ⟨a, h⟩ := by have := mem_map.1 (by rw [attach_map_val]; exact h); { rcases this with ⟨⟨_, _⟩, m, rfl⟩, exact m } @[simp] theorem mem_pmap {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {l H b} : b ∈ pmap f l H ↔ βˆƒ a (h : a ∈ l), f a (H a h) = b := by simp only [pmap_eq_map_attach, mem_map, mem_attach, true_and, subtype.exists] @[simp] theorem length_pmap {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {l H} : length (pmap f l H) = length l := by induction l; [refl, simp only [*, pmap, length]] @[simp] lemma length_attach (L : list Ξ±) : L.attach.length = L.length := length_pmap @[simp] lemma pmap_eq_nil {p : Ξ± β†’ Prop} {f : Ξ  a, p a β†’ Ξ²} {l H} : pmap f l H = [] ↔ l = [] := by rw [← length_eq_zero, length_pmap, length_eq_zero] @[simp] lemma attach_eq_nil (l : list Ξ±) : l.attach = [] ↔ l = [] := pmap_eq_nil lemma last_pmap {Ξ± Ξ² : Type*} (p : Ξ± β†’ Prop) (f : Ξ  a, p a β†’ Ξ²) (l : list Ξ±) (hl₁ : βˆ€ a ∈ l, p a) (hlβ‚‚ : l β‰  []) : (l.pmap f hl₁).last (mt list.pmap_eq_nil.1 hlβ‚‚) = f (l.last hlβ‚‚) (hl₁ _ (list.last_mem hlβ‚‚)) := begin induction l with l_hd l_tl l_ih, { apply (hlβ‚‚ rfl).elim }, { cases l_tl, { simp }, { apply l_ih } } end lemma nth_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) {l : list Ξ±} (h : βˆ€ a ∈ l, p a) (n : β„•) : nth (pmap f l h) n = option.pmap f (nth l n) (Ξ» x H, h x (nth_mem H)) := begin induction l with hd tl hl generalizing n, { simp }, { cases n; simp [hl] } end lemma nth_le_pmap {p : Ξ± β†’ Prop} (f : Ξ  a, p a β†’ Ξ²) {l : list Ξ±} (h : βˆ€ a ∈ l, p a) {n : β„•} (hn : n < (pmap f l h).length) : nth_le (pmap f l h) n hn = f (nth_le l n (@length_pmap _ _ p f l h β–Έ hn)) (h _ (nth_le_mem l n (@length_pmap _ _ p f l h β–Έ hn))) := begin induction l with hd tl hl generalizing n, { simp only [length, pmap] at hn, exact absurd hn (not_lt_of_le n.zero_le) }, { cases n, { simp }, { simpa [hl] } } end /-! ### find -/ section find variables {p : Ξ± β†’ Prop} [decidable_pred p] {l : list Ξ±} {a : Ξ±} @[simp] theorem find_nil (p : Ξ± β†’ Prop) [decidable_pred p] : find p [] = none := rfl @[simp] theorem find_cons_of_pos (l) (h : p a) : find p (a::l) = some a := if_pos h @[simp] theorem find_cons_of_neg (l) (h : Β¬ p a) : find p (a::l) = find p l := if_neg h @[simp] theorem find_eq_none : find p l = none ↔ βˆ€ x ∈ l, Β¬ p x := begin induction l with a l IH, { exact iff_of_true rfl (forall_mem_nil _) }, rw forall_mem_cons, by_cases h : p a, { simp only [find_cons_of_pos _ h, h, not_true, false_and] }, { rwa [find_cons_of_neg _ h, iff_true_intro h, true_and] } end theorem find_some (H : find p l = some a) : p a := begin induction l with b l IH, {contradiction}, by_cases h : p b, { rw find_cons_of_pos _ h at H, cases H, exact h }, { rw find_cons_of_neg _ h at H, exact IH H } end @[simp] theorem find_mem (H : find p l = some a) : a ∈ l := begin induction l with b l IH, {contradiction}, by_cases h : p b, { rw find_cons_of_pos _ h at H, cases H, apply mem_cons_self }, { rw find_cons_of_neg _ h at H, exact mem_cons_of_mem _ (IH H) } end end find /-! ### lookmap -/ section lookmap variables (f : Ξ± β†’ option Ξ±) @[simp] theorem lookmap_nil : [].lookmap f = [] := rfl @[simp] theorem lookmap_cons_none {a : Ξ±} (l : list Ξ±) (h : f a = none) : (a :: l).lookmap f = a :: l.lookmap f := by simp [lookmap, h] @[simp] theorem lookmap_cons_some {a b : Ξ±} (l : list Ξ±) (h : f a = some b) : (a :: l).lookmap f = b :: l := by simp [lookmap, h] theorem lookmap_some : βˆ€ l : list Ξ±, l.lookmap some = l | [] := rfl | (a::l) := rfl theorem lookmap_none : βˆ€ l : list Ξ±, l.lookmap (Ξ» _, none) = l | [] := rfl | (a::l) := congr_arg (cons a) (lookmap_none l) theorem lookmap_congr {f g : Ξ± β†’ option Ξ±} : βˆ€ {l : list Ξ±}, (βˆ€ a ∈ l, f a = g a) β†’ l.lookmap f = l.lookmap g | [] H := rfl | (a::l) H := begin cases forall_mem_cons.1 H with H₁ Hβ‚‚, cases h : g a with b, { simp [h, H₁.trans h, lookmap_congr Hβ‚‚] }, { simp [lookmap_cons_some _ _ h, lookmap_cons_some _ _ (H₁.trans h)] } end theorem lookmap_of_forall_not {l : list Ξ±} (H : βˆ€ a ∈ l, f a = none) : l.lookmap f = l := (lookmap_congr H).trans (lookmap_none l) theorem lookmap_map_eq (g : Ξ± β†’ Ξ²) (h : βˆ€ a (b ∈ f a), g a = g b) : βˆ€ l : list Ξ±, map g (l.lookmap f) = map g l | [] := rfl | (a::l) := begin cases h' : f a with b, { simp [h', lookmap_map_eq] }, { simp [lookmap_cons_some _ _ h', h _ _ h'] } end theorem lookmap_id' (h : βˆ€ a (b ∈ f a), a = b) (l : list Ξ±) : l.lookmap f = l := by rw [← map_id (l.lookmap f), lookmap_map_eq, map_id]; exact h theorem length_lookmap (l : list Ξ±) : length (l.lookmap f) = length l := by rw [← length_map, lookmap_map_eq _ (Ξ» _, ()), length_map]; simp end lookmap /-! ### filter_map -/ @[simp] theorem filter_map_nil (f : Ξ± β†’ option Ξ²) : filter_map f [] = [] := rfl @[simp] theorem filter_map_cons_none {f : Ξ± β†’ option Ξ²} (a : Ξ±) (l : list Ξ±) (h : f a = none) : filter_map f (a :: l) = filter_map f l := by simp only [filter_map, h] @[simp] theorem filter_map_cons_some (f : Ξ± β†’ option Ξ²) (a : Ξ±) (l : list Ξ±) {b : Ξ²} (h : f a = some b) : filter_map f (a :: l) = b :: filter_map f l := by simp only [filter_map, h]; split; refl lemma filter_map_append {Ξ± Ξ² : Type*} (l l' : list Ξ±) (f : Ξ± β†’ option Ξ²) : filter_map f (l ++ l') = filter_map f l ++ filter_map f l' := begin induction l with hd tl hl generalizing l', { simp }, { rw [cons_append, filter_map, filter_map], cases f hd; simp only [filter_map, hl, cons_append, eq_self_iff_true, and_self] } end theorem filter_map_eq_map (f : Ξ± β†’ Ξ²) : filter_map (some ∘ f) = map f := begin funext l, induction l with a l IH, {refl}, simp only [filter_map_cons_some (some ∘ f) _ _ rfl, IH, map_cons], split; refl end theorem filter_map_eq_filter (p : Ξ± β†’ Prop) [decidable_pred p] : filter_map (option.guard p) = filter p := begin funext l, induction l with a l IH, {refl}, by_cases pa : p a, { simp only [filter_map, option.guard, IH, if_pos pa, filter_cons_of_pos _ pa], split; refl }, { simp only [filter_map, option.guard, IH, if_neg pa, filter_cons_of_neg _ pa] } end theorem filter_map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ option Ξ³) (l : list Ξ±) : filter_map g (filter_map f l) = filter_map (Ξ» x, (f x).bind g) l := begin induction l with a l IH, {refl}, cases h : f a with b, { rw [filter_map_cons_none _ _ h, filter_map_cons_none, IH], simp only [h, option.none_bind'] }, rw filter_map_cons_some _ _ _ h, cases h' : g b with c; [ rw [filter_map_cons_none _ _ h', filter_map_cons_none, IH], rw [filter_map_cons_some _ _ _ h', filter_map_cons_some, IH] ]; simp only [h, h', option.some_bind'] end theorem map_filter_map (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ³) (l : list Ξ±) : map g (filter_map f l) = filter_map (Ξ» x, (f x).map g) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_map_map (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ option Ξ³) (l : list Ξ±) : filter_map g (map f l) = filter_map (g ∘ f) l := by rw [← filter_map_eq_map, filter_map_filter_map]; refl theorem filter_filter_map (f : Ξ± β†’ option Ξ²) (p : Ξ² β†’ Prop) [decidable_pred p] (l : list Ξ±) : filter p (filter_map f l) = filter_map (Ξ» x, (f x).filter p) l := by rw [← filter_map_eq_filter, filter_map_filter_map]; refl theorem filter_map_filter (p : Ξ± β†’ Prop) [decidable_pred p] (f : Ξ± β†’ option Ξ²) (l : list Ξ±) : filter_map f (filter p l) = filter_map (Ξ» x, if p x then f x else none) l := begin rw [← filter_map_eq_filter, filter_map_filter_map], congr, funext x, show (option.guard p x).bind f = ite (p x) (f x) none, by_cases h : p x, { simp only [option.guard, if_pos h, option.some_bind'] }, { simp only [option.guard, if_neg h, option.none_bind'] } end @[simp] theorem filter_map_some (l : list Ξ±) : filter_map some l = l := by rw filter_map_eq_map; apply map_id @[simp] theorem mem_filter_map (f : Ξ± β†’ option Ξ²) (l : list Ξ±) {b : Ξ²} : b ∈ filter_map f l ↔ βˆƒ a, a ∈ l ∧ f a = some b := begin induction l with a l IH, { split, { intro H, cases H }, { rintro ⟨_, H, _⟩, cases H } }, cases h : f a with b', { have : f a β‰  some b, {rw h, intro, contradiction}, simp only [filter_map_cons_none _ _ h, IH, mem_cons_iff, or_and_distrib_right, exists_or_distrib, exists_eq_left, this, false_or] }, { have : f a = some b ↔ b = b', { split; intro t, {rw t at h; injection h}, {exact t.symm β–Έ h} }, simp only [filter_map_cons_some _ _ _ h, IH, mem_cons_iff, or_and_distrib_right, exists_or_distrib, this, exists_eq_left] } end theorem map_filter_map_of_inv (f : Ξ± β†’ option Ξ²) (g : Ξ² β†’ Ξ±) (H : βˆ€ x : Ξ±, (f x).map g = some x) (l : list Ξ±) : map g (filter_map f l) = l := by simp only [map_filter_map, H, filter_map_some] theorem sublist.filter_map (f : Ξ± β†’ option Ξ²) {l₁ lβ‚‚ : list Ξ±} (s : l₁ <+ lβ‚‚) : filter_map f l₁ <+ filter_map f lβ‚‚ := by induction s with l₁ lβ‚‚ a s IH l₁ lβ‚‚ a s IH; simp only [filter_map]; cases f a with b; simp only [filter_map, IH, sublist.cons, sublist.cons2] theorem sublist.map (f : Ξ± β†’ Ξ²) {l₁ lβ‚‚ : list Ξ±} (s : l₁ <+ lβ‚‚) : map f l₁ <+ map f lβ‚‚ := filter_map_eq_map f β–Έ s.filter_map _ /-! ### reduce_option -/ @[simp] lemma reduce_option_cons_of_some (x : Ξ±) (l : list (option Ξ±)) : reduce_option (some x :: l) = x :: l.reduce_option := by simp only [reduce_option, filter_map, id.def, eq_self_iff_true, and_self] @[simp] lemma reduce_option_cons_of_none (l : list (option Ξ±)) : reduce_option (none :: l) = l.reduce_option := by simp only [reduce_option, filter_map, id.def] @[simp] lemma reduce_option_nil : @reduce_option Ξ± [] = [] := rfl @[simp] lemma reduce_option_map {l : list (option Ξ±)} {f : Ξ± β†’ Ξ²} : reduce_option (map (option.map f) l) = map f (reduce_option l) := begin induction l with hd tl hl, { simp only [reduce_option_nil, map_nil] }, { cases hd; simpa only [true_and, option.map_some', map, eq_self_iff_true, reduce_option_cons_of_some] using hl }, end lemma reduce_option_append (l l' : list (option Ξ±)) : (l ++ l').reduce_option = l.reduce_option ++ l'.reduce_option := filter_map_append l l' id lemma reduce_option_length_le (l : list (option Ξ±)) : l.reduce_option.length ≀ l.length := begin induction l with hd tl hl, { simp only [reduce_option_nil, length] }, { cases hd, { exact nat.le_succ_of_le hl }, { simpa only [length, add_le_add_iff_right, reduce_option_cons_of_some] using hl} } end lemma reduce_option_length_eq_iff {l : list (option Ξ±)} : l.reduce_option.length = l.length ↔ βˆ€ x ∈ l, option.is_some x := begin induction l with hd tl hl, { simp only [forall_const, reduce_option_nil, not_mem_nil, forall_prop_of_false, eq_self_iff_true, length, not_false_iff] }, { cases hd, { simp only [mem_cons_iff, forall_eq_or_imp, bool.coe_sort_ff, false_and, reduce_option_cons_of_none, length, option.is_some_none, iff_false], intro H, have := reduce_option_length_le tl, rw H at this, exact absurd (nat.lt_succ_self _) (not_lt_of_le this) }, { simp only [hl, true_and, mem_cons_iff, forall_eq_or_imp, add_left_inj, bool.coe_sort_tt, length, option.is_some_some, reduce_option_cons_of_some] } } end lemma reduce_option_length_lt_iff {l : list (option Ξ±)} : l.reduce_option.length < l.length ↔ none ∈ l := begin rw [(reduce_option_length_le l).lt_iff_ne, ne, reduce_option_length_eq_iff], induction l; simp *, rw [eq_comm, ← option.not_is_some_iff_eq_none, decidable.imp_iff_not_or] end lemma reduce_option_singleton (x : option Ξ±) : [x].reduce_option = x.to_list := by cases x; refl lemma reduce_option_concat (l : list (option Ξ±)) (x : option Ξ±) : (l.concat x).reduce_option = l.reduce_option ++ x.to_list := begin induction l with hd tl hl generalizing x, { cases x; simp [option.to_list] }, { simp only [concat_eq_append, reduce_option_append] at hl, cases hd; simp [hl, reduce_option_append] } end lemma reduce_option_concat_of_some (l : list (option Ξ±)) (x : Ξ±) : (l.concat (some x)).reduce_option = l.reduce_option.concat x := by simp only [reduce_option_nil, concat_eq_append, reduce_option_append, reduce_option_cons_of_some] lemma reduce_option_mem_iff {l : list (option Ξ±)} {x : Ξ±} : x ∈ l.reduce_option ↔ (some x) ∈ l := by simp only [reduce_option, id.def, mem_filter_map, exists_eq_right] lemma reduce_option_nth_iff {l : list (option Ξ±)} {x : Ξ±} : (βˆƒ i, l.nth i = some (some x)) ↔ βˆƒ i, l.reduce_option.nth i = some x := by rw [←mem_iff_nth, ←mem_iff_nth, reduce_option_mem_iff] /-! ### filter -/ section filter variables {p : Ξ± β†’ Prop} [decidable_pred p] theorem filter_eq_foldr (p : Ξ± β†’ Prop) [decidable_pred p] (l : list Ξ±) : filter p l = foldr (Ξ» a out, if p a then a :: out else out) [] l := by induction l; simp [*, filter] lemma filter_congr {p q : Ξ± β†’ Prop} [decidable_pred p] [decidable_pred q] : βˆ€ {l : list Ξ±}, (βˆ€ x ∈ l, p x ↔ q x) β†’ filter p l = filter q l | [] _ := rfl | (a::l) h := by rw forall_mem_cons at h; by_cases pa : p a; [simp only [filter_cons_of_pos _ pa, filter_cons_of_pos _ (h.1.1 pa), filter_congr h.2], simp only [filter_cons_of_neg _ pa, filter_cons_of_neg _ (mt h.1.2 pa), filter_congr h.2]]; split; refl @[simp] theorem filter_subset (l : list Ξ±) : filter p l βŠ† l := (filter_sublist l).subset theorem of_mem_filter {a : Ξ±} : βˆ€ {l}, a ∈ filter p l β†’ p a | (b::l) ain := if pb : p b then have a ∈ b :: filter p l, by simpa only [filter_cons_of_pos _ pb] using ain, or.elim (eq_or_mem_of_mem_cons this) (assume : a = b, begin rw [← this] at pb, exact pb end) (assume : a ∈ filter p l, of_mem_filter this) else begin simp only [filter_cons_of_neg _ pb] at ain, exact (of_mem_filter ain) end theorem mem_of_mem_filter {a : Ξ±} {l} (h : a ∈ filter p l) : a ∈ l := filter_subset l h theorem mem_filter_of_mem {a : Ξ±} : βˆ€ {l}, a ∈ l β†’ p a β†’ a ∈ filter p l | (_::l) (or.inl rfl) pa := by rw filter_cons_of_pos _ pa; apply mem_cons_self | (b::l) (or.inr ain) pa := if pb : p b then by rw [filter_cons_of_pos _ pb]; apply mem_cons_of_mem; apply mem_filter_of_mem ain pa else by rw [filter_cons_of_neg _ pb]; apply mem_filter_of_mem ain pa @[simp] theorem mem_filter {a : Ξ±} {l} : a ∈ filter p l ↔ a ∈ l ∧ p a := ⟨λ h, ⟨mem_of_mem_filter h, of_mem_filter h⟩, Ξ» ⟨h₁, hβ‚‚βŸ©, mem_filter_of_mem h₁ hβ‚‚βŸ© lemma monotone_filter_left (p : Ξ± β†’ Prop) [decidable_pred p] ⦃l l' : list α⦄ (h : l βŠ† l') : filter p l βŠ† filter p l' := begin intros x hx, rw [mem_filter] at hx ⊒, exact ⟨h hx.left, hx.right⟩ end theorem filter_eq_self {l} : filter p l = l ↔ βˆ€ a ∈ l, p a := begin induction l with a l ih, { exact iff_of_true rfl (forall_mem_nil _) }, rw forall_mem_cons, by_cases p a, { rw [filter_cons_of_pos _ h, cons_inj, ih, and_iff_right h] }, { rw [filter_cons_of_neg _ h], refine iff_of_false _ (mt and.left h), intro e, have := filter_sublist l, rw e at this, exact not_lt_of_ge (length_le_of_sublist this) (lt_succ_self _) } end theorem filter_eq_nil {l} : filter p l = [] ↔ βˆ€ a ∈ l, Β¬p a := by simp only [eq_nil_iff_forall_not_mem, mem_filter, not_and] variable (p) theorem sublist.filter {l₁ lβ‚‚} (s : l₁ <+ lβ‚‚) : filter p l₁ <+ filter p lβ‚‚ := filter_map_eq_filter p β–Έ s.filter_map _ lemma monotone_filter_right (l : list Ξ±) ⦃p q : Ξ± β†’ Prop⦄ [decidable_pred p] [decidable_pred q] (h : p ≀ q) : l.filter p <+ l.filter q := begin induction l with hd tl IH, { refl }, { by_cases hp : p hd, { rw [filter_cons_of_pos _ hp, filter_cons_of_pos _ (h _ hp)], exact cons_sublist_cons hd IH }, { rw filter_cons_of_neg _ hp, by_cases hq : q hd, { rw filter_cons_of_pos _ hq, exact sublist_cons_of_sublist hd IH }, { rw filter_cons_of_neg _ hq, exact IH } } } end theorem map_filter (f : Ξ² β†’ Ξ±) (l : list Ξ²) : filter p (map f l) = map f (filter (p ∘ f) l) := by rw [← filter_map_eq_map, filter_filter_map, filter_map_filter]; refl @[simp] theorem filter_filter (q) [decidable_pred q] : βˆ€ l, filter p (filter q l) = filter (Ξ» a, p a ∧ q a) l | [] := rfl | (a :: l) := by by_cases hp : p a; by_cases hq : q a; simp only [hp, hq, filter, if_true, if_false, true_and, false_and, filter_filter l, eq_self_iff_true] @[simp] lemma filter_true {h : decidable_pred (Ξ» a : Ξ±, true)} (l : list Ξ±) : @filter Ξ± (Ξ» _, true) h l = l := by convert filter_eq_self.2 (Ξ» _ _, trivial) @[simp] lemma filter_false {h : decidable_pred (Ξ» a : Ξ±, false)} (l : list Ξ±) : @filter Ξ± (Ξ» _, false) h l = [] := by convert filter_eq_nil.2 (Ξ» _ _, id) @[simp] theorem span_eq_take_drop : βˆ€ (l : list Ξ±), span p l = (take_while p l, drop_while p l) | [] := rfl | (a::l) := if pa : p a then by simp only [span, if_pos pa, span_eq_take_drop l, take_while, drop_while] else by simp only [span, take_while, drop_while, if_neg pa] @[simp] theorem take_while_append_drop : βˆ€ (l : list Ξ±), take_while p l ++ drop_while p l = l | [] := rfl | (a::l) := if pa : p a then by rw [take_while, drop_while, if_pos pa, if_pos pa, cons_append, take_while_append_drop l] else by rw [take_while, drop_while, if_neg pa, if_neg pa, nil_append] @[simp] theorem countp_nil : countp p [] = 0 := rfl @[simp] theorem countp_cons_of_pos {a : Ξ±} (l) (pa : p a) : countp p (a::l) = countp p l + 1 := if_pos pa @[simp] theorem countp_cons_of_neg {a : Ξ±} (l) (pa : Β¬ p a) : countp p (a::l) = countp p l := if_neg pa theorem countp_eq_length_filter (l) : countp p l = length (filter p l) := by induction l with x l ih; [refl, by_cases (p x)]; [simp only [filter_cons_of_pos _ h, countp, ih, if_pos h], simp only [countp_cons_of_neg _ _ h, ih, filter_cons_of_neg _ h]]; refl local attribute [simp] countp_eq_length_filter @[simp] theorem countp_append (l₁ lβ‚‚) : countp p (l₁ ++ lβ‚‚) = countp p l₁ + countp p lβ‚‚ := by simp only [countp_eq_length_filter, filter_append, length_append] theorem countp_pos {l} : 0 < countp p l ↔ βˆƒ a ∈ l, p a := by simp only [countp_eq_length_filter, length_pos_iff_exists_mem, mem_filter, exists_prop] theorem countp_le_of_sublist {l₁ lβ‚‚} (s : l₁ <+ lβ‚‚) : countp p l₁ ≀ countp p lβ‚‚ := by simpa only [countp_eq_length_filter] using length_le_of_sublist (s.filter p) @[simp] theorem countp_filter {q} [decidable_pred q] (l : list Ξ±) : countp p (filter q l) = countp (Ξ» a, p a ∧ q a) l := by simp only [countp_eq_length_filter, filter_filter] end filter /-! ### count -/ section count variable [decidable_eq Ξ±] @[simp] theorem count_nil (a : Ξ±) : count a [] = 0 := rfl theorem count_cons (a b : Ξ±) (l : list Ξ±) : count a (b :: l) = if a = b then succ (count a l) else count a l := rfl theorem count_cons' (a b : Ξ±) (l : list Ξ±) : count a (b :: l) = count a l + (if a = b then 1 else 0) := begin rw count_cons, split_ifs; refl end @[simp] theorem count_cons_self (a : Ξ±) (l : list Ξ±) : count a (a::l) = succ (count a l) := if_pos rfl @[simp, priority 990] theorem count_cons_of_ne {a b : Ξ±} (h : a β‰  b) (l : list Ξ±) : count a (b::l) = count a l := if_neg h theorem count_tail : Ξ  (l : list Ξ±) (a : Ξ±) (h : 0 < l.length), l.tail.count a = l.count a - ite (a = list.nth_le l 0 h) 1 0 | (_ :: _) a h := by { rw [count_cons], split_ifs; simp } theorem count_le_of_sublist (a : Ξ±) {l₁ lβ‚‚} : l₁ <+ lβ‚‚ β†’ count a l₁ ≀ count a lβ‚‚ := countp_le_of_sublist _ theorem count_le_count_cons (a b : Ξ±) (l : list Ξ±) : count a l ≀ count a (b :: l) := count_le_of_sublist _ (sublist_cons _ _) theorem count_singleton (a : Ξ±) : count a [a] = 1 := if_pos rfl @[simp] theorem count_append (a : Ξ±) : βˆ€ l₁ lβ‚‚, count a (l₁ ++ lβ‚‚) = count a l₁ + count a lβ‚‚ := countp_append _ theorem count_concat (a : Ξ±) (l : list Ξ±) : count a (concat l a) = succ (count a l) := by simp [-add_comm] theorem count_pos {a : Ξ±} {l : list Ξ±} : 0 < count a l ↔ a ∈ l := by simp only [count, countp_pos, exists_prop, exists_eq_right'] @[simp, priority 980] theorem count_eq_zero_of_not_mem {a : Ξ±} {l : list Ξ±} (h : a βˆ‰ l) : count a l = 0 := decidable.by_contradiction $ Ξ» h', h $ count_pos.1 (nat.pos_of_ne_zero h') theorem not_mem_of_count_eq_zero {a : Ξ±} {l : list Ξ±} (h : count a l = 0) : a βˆ‰ l := Ξ» h', ne_of_gt (count_pos.2 h') h @[simp] theorem count_repeat (a : Ξ±) (n : β„•) : count a (repeat a n) = n := by rw [count, countp_eq_length_filter, filter_eq_self.2, length_repeat]; exact Ξ» b m, (eq_of_mem_repeat m).symm theorem le_count_iff_repeat_sublist {a : Ξ±} {l : list Ξ±} {n : β„•} : n ≀ count a l ↔ repeat a n <+ l := ⟨λ h, ((repeat_sublist_repeat a).2 h).trans $ have filter (eq a) l = repeat a (count a l), from eq_repeat.2 ⟨by simp only [count, countp_eq_length_filter], Ξ» b m, (of_mem_filter m).symm⟩, by rw ← this; apply filter_sublist, Ξ» h, by simpa only [count_repeat] using count_le_of_sublist a h⟩ theorem repeat_count_eq_of_count_eq_length {a : Ξ±} {l : list Ξ±} (h : count a l = length l) : repeat a (count a l) = l := eq_of_sublist_of_length_eq (le_count_iff_repeat_sublist.mp (le_refl (count a l))) (eq.trans (length_repeat a (count a l)) h) @[simp] theorem count_filter {p} [decidable_pred p] {a} {l : list Ξ±} (h : p a) : count a (filter p l) = count a l := by simp only [count, countp_filter]; congr; exact set.ext (Ξ» b, and_iff_left_of_imp (Ξ» e, e β–Έ h)) lemma count_bind {Ξ± Ξ²} [decidable_eq Ξ²] (l : list Ξ±) (f : Ξ± β†’ list Ξ²) (x : Ξ²) : count x (l.bind f) = sum (map (count x ∘ f) l) := begin induction l with hd tl IH, { simp }, { simpa } end @[simp] lemma count_map_map {Ξ± Ξ²} [decidable_eq Ξ±] [decidable_eq Ξ²] (l : list Ξ±) (f : Ξ± β†’ Ξ²) (hf : function.injective f) (x : Ξ±) : count (f x) (map f l) = count x l := begin induction l with y l IH generalizing x, { simp }, { rw map_cons, by_cases h : x = y, { simpa [h] using IH _ }, { simpa [h, hf.ne h] using IH _ } } end end count /-! ### prefix, suffix, infix -/ @[simp] theorem prefix_append (l₁ lβ‚‚ : list Ξ±) : l₁ <+: l₁ ++ lβ‚‚ := ⟨lβ‚‚, rfl⟩ @[simp] theorem suffix_append (l₁ lβ‚‚ : list Ξ±) : lβ‚‚ <:+ l₁ ++ lβ‚‚ := ⟨l₁, rfl⟩ theorem infix_append (l₁ lβ‚‚ l₃ : list Ξ±) : lβ‚‚ <:+: l₁ ++ lβ‚‚ ++ l₃ := ⟨l₁, l₃, rfl⟩ @[simp] theorem infix_append' (l₁ lβ‚‚ l₃ : list Ξ±) : lβ‚‚ <:+: l₁ ++ (lβ‚‚ ++ l₃) := by rw ← list.append_assoc; apply infix_append theorem nil_prefix (l : list Ξ±) : [] <+: l := ⟨l, rfl⟩ theorem nil_suffix (l : list Ξ±) : [] <:+ l := ⟨l, append_nil _⟩ @[refl] theorem prefix_refl (l : list Ξ±) : l <+: l := ⟨[], append_nil _⟩ @[refl] theorem suffix_refl (l : list Ξ±) : l <:+ l := ⟨[], rfl⟩ @[simp] theorem suffix_cons (a : Ξ±) : βˆ€ l, l <:+ a :: l := suffix_append [a] theorem prefix_concat (a : Ξ±) (l) : l <+: concat l a := by simp theorem infix_of_prefix {l₁ lβ‚‚ : list Ξ±} : l₁ <+: lβ‚‚ β†’ l₁ <:+: lβ‚‚ := λ⟨t, h⟩, ⟨[], t, h⟩ theorem infix_of_suffix {l₁ lβ‚‚ : list Ξ±} : l₁ <:+ lβ‚‚ β†’ l₁ <:+: lβ‚‚ := λ⟨t, h⟩, ⟨t, [], by simp only [h, append_nil]⟩ @[refl] theorem infix_refl (l : list Ξ±) : l <:+: l := infix_of_prefix $ prefix_refl l theorem nil_infix (l : list Ξ±) : [] <:+: l := infix_of_prefix $ nil_prefix l theorem infix_cons {L₁ Lβ‚‚ : list Ξ±} {x : Ξ±} : L₁ <:+: Lβ‚‚ β†’ L₁ <:+: x :: Lβ‚‚ := λ⟨LP, LS, H⟩, ⟨x :: LP, LS, H β–Έ rfl⟩ @[trans] theorem is_prefix.trans : βˆ€ {l₁ lβ‚‚ l₃ : list Ξ±}, l₁ <+: lβ‚‚ β†’ lβ‚‚ <+: l₃ β†’ l₁ <+: l₃ | l ._ ._ ⟨r₁, rfl⟩ ⟨rβ‚‚, rfl⟩ := ⟨r₁ ++ rβ‚‚, (append_assoc _ _ _).symm⟩ @[trans] theorem is_suffix.trans : βˆ€ {l₁ lβ‚‚ l₃ : list Ξ±}, l₁ <:+ lβ‚‚ β†’ lβ‚‚ <:+ l₃ β†’ l₁ <:+ l₃ | l ._ ._ ⟨l₁, rfl⟩ ⟨lβ‚‚, rfl⟩ := ⟨lβ‚‚ ++ l₁, append_assoc _ _ _⟩ @[trans] theorem is_infix.trans : βˆ€ {l₁ lβ‚‚ l₃ : list Ξ±}, l₁ <:+: lβ‚‚ β†’ lβ‚‚ <:+: l₃ β†’ l₁ <:+: l₃ | l ._ ._ ⟨l₁, r₁, rfl⟩ ⟨lβ‚‚, rβ‚‚, rfl⟩ := ⟨lβ‚‚ ++ l₁, r₁ ++ rβ‚‚, by simp only [append_assoc]⟩ theorem sublist_of_infix {l₁ lβ‚‚ : list Ξ±} : l₁ <:+: lβ‚‚ β†’ l₁ <+ lβ‚‚ := λ⟨s, t, h⟩, by rw [← h]; exact (sublist_append_right _ _).trans (sublist_append_left _ _) theorem sublist_of_prefix {l₁ lβ‚‚ : list Ξ±} : l₁ <+: lβ‚‚ β†’ l₁ <+ lβ‚‚ := sublist_of_infix ∘ infix_of_prefix theorem sublist_of_suffix {l₁ lβ‚‚ : list Ξ±} : l₁ <:+ lβ‚‚ β†’ l₁ <+ lβ‚‚ := sublist_of_infix ∘ infix_of_suffix theorem reverse_suffix {l₁ lβ‚‚ : list Ξ±} : reverse l₁ <:+ reverse lβ‚‚ ↔ l₁ <+: lβ‚‚ := ⟨λ ⟨r, e⟩, ⟨reverse r, by rw [← reverse_reverse l₁, ← reverse_append, e, reverse_reverse]⟩, Ξ» ⟨r, e⟩, ⟨reverse r, by rw [← reverse_append, e]⟩⟩ theorem reverse_prefix {l₁ lβ‚‚ : list Ξ±} : reverse l₁ <+: reverse lβ‚‚ ↔ l₁ <:+ lβ‚‚ := by rw ← reverse_suffix; simp only [reverse_reverse] theorem length_le_of_infix {l₁ lβ‚‚ : list Ξ±} (s : l₁ <:+: lβ‚‚) : length l₁ ≀ length lβ‚‚ := length_le_of_sublist $ sublist_of_infix s theorem eq_nil_of_infix_nil {l : list Ξ±} (s : l <:+: []) : l = [] := eq_nil_of_sublist_nil $ sublist_of_infix s @[simp] theorem eq_nil_iff_infix_nil {l : list Ξ±} : l <:+: [] ↔ l = [] := ⟨eq_nil_of_infix_nil, Ξ» h, h β–Έ infix_refl _⟩ theorem eq_nil_of_prefix_nil {l : list Ξ±} (s : l <+: []) : l = [] := eq_nil_of_infix_nil $ infix_of_prefix s @[simp] theorem eq_nil_iff_prefix_nil {l : list Ξ±} : l <+: [] ↔ l = [] := ⟨eq_nil_of_prefix_nil, Ξ» h, h β–Έ prefix_refl _⟩ theorem eq_nil_of_suffix_nil {l : list Ξ±} (s : l <:+ []) : l = [] := eq_nil_of_infix_nil $ infix_of_suffix s @[simp] theorem eq_nil_iff_suffix_nil {l : list Ξ±} : l <:+ [] ↔ l = [] := ⟨eq_nil_of_suffix_nil, Ξ» h, h β–Έ suffix_refl _⟩ theorem infix_iff_prefix_suffix (l₁ lβ‚‚ : list Ξ±) : l₁ <:+: lβ‚‚ ↔ βˆƒ t, l₁ <+: t ∧ t <:+ lβ‚‚ := ⟨λ⟨s, t, e⟩, ⟨l₁ ++ t, ⟨_, rfl⟩, by rw [← e, append_assoc]; exact ⟨_, rfl⟩⟩, λ⟨._, ⟨t, rfl⟩, ⟨s, e⟩⟩, ⟨s, t, by rw append_assoc; exact e⟩⟩ theorem eq_of_infix_of_length_eq {l₁ lβ‚‚ : list Ξ±} (s : l₁ <:+: lβ‚‚) : length l₁ = length lβ‚‚ β†’ l₁ = lβ‚‚ := eq_of_sublist_of_length_eq $ sublist_of_infix s theorem eq_of_prefix_of_length_eq {l₁ lβ‚‚ : list Ξ±} (s : l₁ <+: lβ‚‚) : length l₁ = length lβ‚‚ β†’ l₁ = lβ‚‚ := eq_of_sublist_of_length_eq $ sublist_of_prefix s theorem eq_of_suffix_of_length_eq {l₁ lβ‚‚ : list Ξ±} (s : l₁ <:+ lβ‚‚) : length l₁ = length lβ‚‚ β†’ l₁ = lβ‚‚ := eq_of_sublist_of_length_eq $ sublist_of_suffix s theorem prefix_of_prefix_length_le : βˆ€ {l₁ lβ‚‚ l₃ : list Ξ±}, l₁ <+: l₃ β†’ lβ‚‚ <+: l₃ β†’ length l₁ ≀ length lβ‚‚ β†’ l₁ <+: lβ‚‚ | [] lβ‚‚ l₃ h₁ hβ‚‚ _ := nil_prefix _ | (a::l₁) (b::lβ‚‚) _ ⟨r₁, rfl⟩ ⟨rβ‚‚, e⟩ ll := begin injection e with _ e', subst b, rcases prefix_of_prefix_length_le ⟨_, rfl⟩ ⟨_, e'⟩ (le_of_succ_le_succ ll) with ⟨r₃, rfl⟩, exact ⟨r₃, rfl⟩ end theorem prefix_or_prefix_of_prefix {l₁ lβ‚‚ l₃ : list Ξ±} (h₁ : l₁ <+: l₃) (hβ‚‚ : lβ‚‚ <+: l₃) : l₁ <+: lβ‚‚ ∨ lβ‚‚ <+: l₁ := (le_total (length l₁) (length lβ‚‚)).imp (prefix_of_prefix_length_le h₁ hβ‚‚) (prefix_of_prefix_length_le hβ‚‚ h₁) theorem suffix_of_suffix_length_le {l₁ lβ‚‚ l₃ : list Ξ±} (h₁ : l₁ <:+ l₃) (hβ‚‚ : lβ‚‚ <:+ l₃) (ll : length l₁ ≀ length lβ‚‚) : l₁ <:+ lβ‚‚ := reverse_prefix.1 $ prefix_of_prefix_length_le (reverse_prefix.2 h₁) (reverse_prefix.2 hβ‚‚) (by simp [ll]) theorem suffix_or_suffix_of_suffix {l₁ lβ‚‚ l₃ : list Ξ±} (h₁ : l₁ <:+ l₃) (hβ‚‚ : lβ‚‚ <:+ l₃) : l₁ <:+ lβ‚‚ ∨ lβ‚‚ <:+ l₁ := (prefix_or_prefix_of_prefix (reverse_prefix.2 h₁) (reverse_prefix.2 hβ‚‚)).imp reverse_prefix.1 reverse_prefix.1 theorem suffix_cons_iff {x : Ξ±} {l₁ lβ‚‚ : list Ξ±} : l₁ <:+ x :: lβ‚‚ ↔ l₁ = x :: lβ‚‚ ∨ l₁ <:+ lβ‚‚ := begin split, { rintro ⟨⟨hd, tl⟩, hlβ‚ƒβŸ©, { exact or.inl hl₃ }, { simp only [cons_append] at hl₃, exact or.inr ⟨_, hl₃.2⟩ } }, { rintro (rfl | hl₁), { exact (x :: lβ‚‚).suffix_refl }, { exact hl₁.trans (lβ‚‚.suffix_cons _) } } end theorem infix_of_mem_join : βˆ€ {L : list (list Ξ±)} {l}, l ∈ L β†’ l <:+: join L | (_ :: L) l (or.inl rfl) := infix_append [] _ _ | (l' :: L) l (or.inr h) := is_infix.trans (infix_of_mem_join h) $ infix_of_suffix $ suffix_append _ _ theorem prefix_append_right_inj {l₁ lβ‚‚ : list Ξ±} (l) : l ++ l₁ <+: l ++ lβ‚‚ ↔ l₁ <+: lβ‚‚ := exists_congr $ Ξ» r, by rw [append_assoc, append_right_inj] theorem prefix_cons_inj {l₁ lβ‚‚ : list Ξ±} (a) : a :: l₁ <+: a :: lβ‚‚ ↔ l₁ <+: lβ‚‚ := prefix_append_right_inj [a] theorem take_prefix (n) (l : list Ξ±) : take n l <+: l := ⟨_, take_append_drop _ _⟩ theorem drop_suffix (n) (l : list Ξ±) : drop n l <:+ l := ⟨_, take_append_drop _ _⟩ theorem tail_suffix (l : list Ξ±) : tail l <:+ l := by rw ← drop_one; apply drop_suffix lemma tail_sublist (l : list Ξ±) : l.tail <+ l := sublist_of_suffix (tail_suffix l) theorem tail_subset (l : list Ξ±) : tail l βŠ† l := (tail_sublist l).subset lemma mem_of_mem_tail {l : list Ξ±} {a : Ξ±} (h : a ∈ l.tail) : a ∈ l := tail_subset l h theorem prefix_iff_eq_append {l₁ lβ‚‚ : list Ξ±} : l₁ <+: lβ‚‚ ↔ l₁ ++ drop (length l₁) lβ‚‚ = lβ‚‚ := ⟨by rintros ⟨r, rfl⟩; rw drop_left, Ξ» e, ⟨_, e⟩⟩ theorem suffix_iff_eq_append {l₁ lβ‚‚ : list Ξ±} : l₁ <:+ lβ‚‚ ↔ take (length lβ‚‚ - length l₁) lβ‚‚ ++ l₁ = lβ‚‚ := ⟨by rintros ⟨r, rfl⟩; simp only [length_append, add_tsub_cancel_right, take_left], Ξ» e, ⟨_, e⟩⟩ theorem prefix_iff_eq_take {l₁ lβ‚‚ : list Ξ±} : l₁ <+: lβ‚‚ ↔ l₁ = take (length l₁) lβ‚‚ := ⟨λ h, append_right_cancel $ (prefix_iff_eq_append.1 h).trans (take_append_drop _ _).symm, Ξ» e, e.symm β–Έ take_prefix _ _⟩ theorem suffix_iff_eq_drop {l₁ lβ‚‚ : list Ξ±} : l₁ <:+ lβ‚‚ ↔ l₁ = drop (length lβ‚‚ - length l₁) lβ‚‚ := ⟨λ h, append_left_cancel $ (suffix_iff_eq_append.1 h).trans (take_append_drop _ _).symm, Ξ» e, e.symm β–Έ drop_suffix _ _⟩ instance decidable_prefix [decidable_eq Ξ±] : βˆ€ (l₁ lβ‚‚ : list Ξ±), decidable (l₁ <+: lβ‚‚) | [] lβ‚‚ := is_true ⟨lβ‚‚, rfl⟩ | (a::l₁) [] := is_false $ Ξ» ⟨t, te⟩, list.no_confusion te | (a::l₁) (b::lβ‚‚) := if h : a = b then @decidable_of_iff _ _ (by rw [← h, prefix_cons_inj]) (decidable_prefix l₁ lβ‚‚) else is_false $ Ξ» ⟨t, te⟩, h $ by injection te -- Alternatively, use mem_tails instance decidable_suffix [decidable_eq Ξ±] : βˆ€ (l₁ lβ‚‚ : list Ξ±), decidable (l₁ <:+ lβ‚‚) | [] lβ‚‚ := is_true ⟨lβ‚‚, append_nil _⟩ | (a::l₁) [] := is_false $ mt (length_le_of_sublist ∘ sublist_of_suffix) dec_trivial | l₁ lβ‚‚ := let len1 := length l₁, len2 := length lβ‚‚ in if hl : len1 ≀ len2 then decidable_of_iff' (l₁ = drop (len2-len1) lβ‚‚) suffix_iff_eq_drop else is_false $ Ξ» h, hl $ length_le_of_sublist $ sublist_of_suffix h lemma prefix_take_le_iff {L : list (list (option Ξ±))} {m n : β„•} (hm : m < L.length) : (take m L) <+: (take n L) ↔ m ≀ n := begin simp only [prefix_iff_eq_take, length_take], induction m with m IH generalizing L n, { simp only [min_eq_left, eq_self_iff_true, nat.zero_le, take] }, { cases n, { simp only [nat.nat_zero_eq_zero, nonpos_iff_eq_zero, take, take_nil], split, { cases L, { exact absurd hm (not_lt_of_le m.succ.zero_le) }, { simp only [forall_prop_of_false, not_false_iff, take] } }, { intro h, contradiction } }, { cases L with l ls, { exact absurd hm (not_lt_of_le m.succ.zero_le) }, { simp only [length] at hm, specialize @IH ls n (nat.lt_of_succ_lt_succ hm), simp only [le_of_lt (nat.lt_of_succ_lt_succ hm), min_eq_left] at IH, simp only [le_of_lt hm, IH, true_and, min_eq_left, eq_self_iff_true, length, take], exact ⟨nat.succ_le_succ, nat.le_of_succ_le_succ⟩ } } }, end lemma cons_prefix_iff {l l' : list Ξ±} {x y : Ξ±} : x :: l <+: y :: l' ↔ x = y ∧ l <+: l' := begin split, { rintro ⟨L, hL⟩, simp only [cons_append] at hL, exact ⟨hL.left, ⟨L, hL.right⟩⟩ }, { rintro ⟨rfl, h⟩, rwa [prefix_cons_inj] }, end lemma map_prefix {l l' : list Ξ±} (f : Ξ± β†’ Ξ²) (h : l <+: l') : l.map f <+: l'.map f := begin induction l with hd tl hl generalizing l', { simp only [nil_prefix, map_nil] }, { cases l' with hd' tl', { simpa only using eq_nil_of_prefix_nil h }, { rw cons_prefix_iff at h, simp only [h, prefix_cons_inj, hl, map] } }, end lemma is_prefix.filter_map {l l' : list Ξ±} (h : l <+: l') (f : Ξ± β†’ option Ξ²) : l.filter_map f <+: l'.filter_map f := begin induction l with hd tl hl generalizing l', { simp only [nil_prefix, filter_map_nil] }, { cases l' with hd' tl', { simpa only using eq_nil_of_prefix_nil h }, { rw cons_prefix_iff at h, rw [←@singleton_append _ hd _, ←@singleton_append _ hd' _, filter_map_append, filter_map_append, h.left, prefix_append_right_inj], exact hl h.right } }, end lemma is_prefix.reduce_option {l l' : list (option Ξ±)} (h : l <+: l') : l.reduce_option <+: l'.reduce_option := h.filter_map id lemma is_prefix.filter (p : Ξ± β†’ Prop) [decidable_pred p] ⦃l l' : list α⦄ (h : l <+: l') : filter p l <+: filter p l' := begin obtain ⟨xs, rfl⟩ := h, rw filter_append, exact prefix_append _ _ end lemma is_suffix.filter (p : Ξ± β†’ Prop) [decidable_pred p] ⦃l l' : list α⦄ (h : l <:+ l') : filter p l <:+ filter p l' := begin obtain ⟨xs, rfl⟩ := h, rw filter_append, exact suffix_append _ _ end lemma is_infix.filter (p : Ξ± β†’ Prop) [decidable_pred p] ⦃l l' : list α⦄ (h : l <:+: l') : filter p l <:+: filter p l' := begin obtain ⟨xs, ys, rfl⟩ := h, rw [filter_append, filter_append], exact infix_append _ _ _ end @[simp] theorem mem_inits : βˆ€ (s t : list Ξ±), s ∈ inits t ↔ s <+: t | s [] := suffices s = nil ↔ s <+: nil, by simpa only [inits, mem_singleton], ⟨λh, h.symm β–Έ prefix_refl [], eq_nil_of_prefix_nil⟩ | s (a::t) := suffices (s = nil ∨ βˆƒ l ∈ inits t, a :: l = s) ↔ s <+: a :: t, by simpa, ⟨λo, match s, o with | ._, or.inl rfl := ⟨_, rfl⟩ | s, or.inr ⟨r, hr, hs⟩ := let ⟨s, ht⟩ := (mem_inits _ _).1 hr in by rw [← hs, ← ht]; exact ⟨s, rfl⟩ end, Ξ»mi, match s, mi with | [], ⟨._, rfl⟩ := or.inl rfl | (b::s), ⟨r, hr⟩ := list.no_confusion hr $ Ξ»ba (st : s++r = t), or.inr $ by rw ba; exact ⟨_, (mem_inits _ _).2 ⟨_, st⟩, rfl⟩ end⟩ @[simp] theorem mem_tails : βˆ€ (s t : list Ξ±), s ∈ tails t ↔ s <:+ t | s [] := by simp only [tails, mem_singleton]; exact ⟨λh, by rw h; exact suffix_refl [], eq_nil_of_suffix_nil⟩ | s (a::t) := by simp only [tails, mem_cons_iff, mem_tails s t]; exact show s = a :: t ∨ s <:+ t ↔ s <:+ a :: t, from ⟨λo, match s, t, o with | ._, t, or.inl rfl := suffix_refl _ | s, ._, or.inr ⟨l, rfl⟩ := ⟨a::l, rfl⟩ end, Ξ»e, match s, t, e with | ._, t, ⟨[], rfl⟩ := or.inl rfl | s, t, ⟨b::l, he⟩ := list.no_confusion he (Ξ»ab lt, or.inr ⟨l, lt⟩) end⟩ lemma inits_cons (a : Ξ±) (l : list Ξ±) : inits (a :: l) = [] :: l.inits.map (Ξ» t, a :: t) := by simp lemma tails_cons (a : Ξ±) (l : list Ξ±) : tails (a :: l) = (a :: l) :: l.tails := by simp @[simp] lemma inits_append : βˆ€ (s t : list Ξ±), inits (s ++ t) = s.inits ++ t.inits.tail.map (Ξ» l, s ++ l) | [] [] := by simp | [] (a::t) := by simp | (a::s) t := by simp [inits_append s t] @[simp] lemma tails_append : βˆ€ (s t : list Ξ±), tails (s ++ t) = s.tails.map (Ξ» l, l ++ t) ++ t.tails.tail | [] [] := by simp | [] (a::t) := by simp | (a::s) t := by simp [tails_append s t] -- the lemma names `inits_eq_tails` and `tails_eq_inits` are like `sublists_eq_sublists'` lemma inits_eq_tails : βˆ€ (l : list Ξ±), l.inits = (reverse $ map reverse $ tails $ reverse l) | [] := by simp | (a :: l) := by simp [inits_eq_tails l, map_eq_map_iff] lemma tails_eq_inits : βˆ€ (l : list Ξ±), l.tails = (reverse $ map reverse $ inits $ reverse l) | [] := by simp | (a :: l) := by simp [tails_eq_inits l, append_left_inj] lemma inits_reverse (l : list Ξ±) : inits (reverse l) = reverse (map reverse l.tails) := by { rw tails_eq_inits l, simp [reverse_involutive.comp_self], } lemma tails_reverse (l : list Ξ±) : tails (reverse l) = reverse (map reverse l.inits) := by { rw inits_eq_tails l, simp [reverse_involutive.comp_self], } lemma map_reverse_inits (l : list Ξ±) : map reverse l.inits = (reverse $ tails $ reverse l) := by { rw inits_eq_tails l, simp [reverse_involutive.comp_self], } lemma map_reverse_tails (l : list Ξ±) : map reverse l.tails = (reverse $ inits $ reverse l) := by { rw tails_eq_inits l, simp [reverse_involutive.comp_self], } @[simp] lemma length_tails (l : list Ξ±) : length (tails l) = length l + 1 := begin induction l with x l IH, { simp }, { simpa using IH } end @[simp] lemma length_inits (l : list Ξ±) : length (inits l) = length l + 1 := by simp [inits_eq_tails] @[simp] lemma nth_le_tails (l : list Ξ±) (n : β„•) (hn : n < length (tails l)) : nth_le (tails l) n hn = l.drop n := begin induction l with x l IH generalizing n, { simp }, { cases n, { simp }, { simpa using IH n _ } }, end @[simp] lemma nth_le_inits (l : list Ξ±) (n : β„•) (hn : n < length (inits l)) : nth_le (inits l) n hn = l.take n := begin induction l with x l IH generalizing n, { simp }, { cases n, { simp }, { simpa using IH n _ } } end instance decidable_infix [decidable_eq Ξ±] : βˆ€ (l₁ lβ‚‚ : list Ξ±), decidable (l₁ <:+: lβ‚‚) | [] lβ‚‚ := is_true ⟨[], lβ‚‚, rfl⟩ | (a::l₁) [] := is_false $ λ⟨s, t, te⟩, absurd te $ append_ne_nil_of_ne_nil_left _ _ $ append_ne_nil_of_ne_nil_right _ _ $ Ξ»h, list.no_confusion h | l₁ lβ‚‚ := decidable_of_decidable_of_iff (list.decidable_bex (Ξ»t, l₁ <+: t) (tails lβ‚‚)) $ by refine (exists_congr (Ξ»t, _)).trans (infix_iff_prefix_suffix _ _).symm; exact ⟨λ⟨h1, h2⟩, ⟨h2, (mem_tails _ _).1 h1⟩, λ⟨h2, h1⟩, ⟨(mem_tails _ _).2 h1, h2⟩⟩ /-! ### insert -/ section insert variable [decidable_eq Ξ±] @[simp] theorem insert_nil (a : Ξ±) : insert a nil = [a] := rfl theorem insert.def (a : Ξ±) (l : list Ξ±) : insert a l = if a ∈ l then l else a :: l := rfl @[simp, priority 980] theorem insert_of_mem {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : insert a l = l := by simp only [insert.def, if_pos h] @[simp, priority 970] theorem insert_of_not_mem {a : Ξ±} {l : list Ξ±} (h : a βˆ‰ l) : insert a l = a :: l := by simp only [insert.def, if_neg h]; split; refl @[simp] theorem mem_insert_iff {a b : Ξ±} {l : list Ξ±} : a ∈ insert b l ↔ a = b ∨ a ∈ l := begin by_cases h' : b ∈ l, { simp only [insert_of_mem h'], apply (or_iff_right_of_imp _).symm, exact Ξ» e, e.symm β–Έ h' }, simp only [insert_of_not_mem h', mem_cons_iff] end @[simp] theorem suffix_insert (a : Ξ±) (l : list Ξ±) : l <:+ insert a l := by by_cases a ∈ l; [simp only [insert_of_mem h], simp only [insert_of_not_mem h, suffix_cons]] @[simp] theorem mem_insert_self (a : Ξ±) (l : list Ξ±) : a ∈ insert a l := mem_insert_iff.2 (or.inl rfl) theorem mem_insert_of_mem {a b : Ξ±} {l : list Ξ±} (h : a ∈ l) : a ∈ insert b l := mem_insert_iff.2 (or.inr h) theorem eq_or_mem_of_mem_insert {a b : Ξ±} {l : list Ξ±} (h : a ∈ insert b l) : a = b ∨ a ∈ l := mem_insert_iff.1 h @[simp] theorem length_insert_of_mem {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : length (insert a l) = length l := by rw insert_of_mem h @[simp] theorem length_insert_of_not_mem {a : Ξ±} {l : list Ξ±} (h : a βˆ‰ l) : length (insert a l) = length l + 1 := by rw insert_of_not_mem h; refl end insert /-! ### erasep -/ section erasep variables {p : Ξ± β†’ Prop} [decidable_pred p] @[simp] theorem erasep_nil : [].erasep p = [] := rfl theorem erasep_cons (a : Ξ±) (l : list Ξ±) : (a :: l).erasep p = if p a then l else a :: l.erasep p := rfl @[simp] theorem erasep_cons_of_pos {a : Ξ±} {l : list Ξ±} (h : p a) : (a :: l).erasep p = l := by simp [erasep_cons, h] @[simp] theorem erasep_cons_of_neg {a : Ξ±} {l : list Ξ±} (h : Β¬ p a) : (a::l).erasep p = a :: l.erasep p := by simp [erasep_cons, h] theorem erasep_of_forall_not {l : list Ξ±} (h : βˆ€ a ∈ l, Β¬ p a) : l.erasep p = l := by induction l with _ _ ih; [refl, simp [h _ (or.inl rfl), ih (forall_mem_of_forall_mem_cons h)]] theorem exists_of_erasep {l : list Ξ±} {a} (al : a ∈ l) (pa : p a) : βˆƒ a l₁ lβ‚‚, (βˆ€ b ∈ l₁, Β¬ p b) ∧ p a ∧ l = l₁ ++ a :: lβ‚‚ ∧ l.erasep p = l₁ ++ lβ‚‚ := begin induction l with b l IH, {cases al}, by_cases pb : p b, { exact ⟨b, [], l, forall_mem_nil _, pb, by simp [pb]⟩ }, { rcases al with rfl | al, {exact pb.elim pa}, rcases IH al with ⟨c, l₁, lβ‚‚, h₁, hβ‚‚, h₃, hβ‚„βŸ©, exact ⟨c, b::l₁, lβ‚‚, forall_mem_cons.2 ⟨pb, hβ‚βŸ©, hβ‚‚, by rw h₃; refl, by simp [pb, hβ‚„]⟩ } end theorem exists_or_eq_self_of_erasep (p : Ξ± β†’ Prop) [decidable_pred p] (l : list Ξ±) : l.erasep p = l ∨ βˆƒ a l₁ lβ‚‚, (βˆ€ b ∈ l₁, Β¬ p b) ∧ p a ∧ l = l₁ ++ a :: lβ‚‚ ∧ l.erasep p = l₁ ++ lβ‚‚ := begin by_cases h : βˆƒ a ∈ l, p a, { rcases h with ⟨a, ha, pa⟩, exact or.inr (exists_of_erasep ha pa) }, { simp at h, exact or.inl (erasep_of_forall_not h) } end @[simp] theorem length_erasep_of_mem {l : list Ξ±} {a} (al : a ∈ l) (pa : p a) : length (l.erasep p) = pred (length l) := by rcases exists_of_erasep al pa with ⟨_, l₁, lβ‚‚, _, _, e₁, eβ‚‚βŸ©; rw eβ‚‚; simp [-add_comm, e₁]; refl theorem erasep_append_left {a : Ξ±} (pa : p a) : βˆ€ {l₁ : list Ξ±} (lβ‚‚), a ∈ l₁ β†’ (l₁++lβ‚‚).erasep p = l₁.erasep p ++ lβ‚‚ | (x::xs) lβ‚‚ h := begin by_cases h' : p x; simp [h'], rw erasep_append_left lβ‚‚ (mem_of_ne_of_mem (mt _ h') h), rintro rfl, exact pa end theorem erasep_append_right : βˆ€ {l₁ : list Ξ±} (lβ‚‚), (βˆ€ b ∈ l₁, Β¬ p b) β†’ (l₁++lβ‚‚).erasep p = l₁ ++ lβ‚‚.erasep p | [] lβ‚‚ h := rfl | (x::xs) lβ‚‚ h := by simp [(forall_mem_cons.1 h).1, erasep_append_right _ (forall_mem_cons.1 h).2] theorem erasep_sublist (l : list Ξ±) : l.erasep p <+ l := by rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, lβ‚‚, h₁, hβ‚‚, h₃, hβ‚„βŸ©; [rw h, {rw [hβ‚„, h₃], simp}] theorem erasep_subset (l : list Ξ±) : l.erasep p βŠ† l := (erasep_sublist l).subset theorem sublist.erasep {l₁ lβ‚‚ : list Ξ±} (s : l₁ <+ lβ‚‚) : l₁.erasep p <+ lβ‚‚.erasep p := begin induction s, case list.sublist.slnil { refl }, case list.sublist.cons : l₁ lβ‚‚ a s IH { by_cases h : p a; simp [h], exacts [IH.trans (erasep_sublist _), IH.cons _ _ _] }, case list.sublist.cons2 : l₁ lβ‚‚ a s IH { by_cases h : p a; simp [h], exacts [s, IH.cons2 _ _ _] } end theorem mem_of_mem_erasep {a : Ξ±} {l : list Ξ±} : a ∈ l.erasep p β†’ a ∈ l := @erasep_subset _ _ _ _ _ @[simp] theorem mem_erasep_of_neg {a : Ξ±} {l : list Ξ±} (pa : Β¬ p a) : a ∈ l.erasep p ↔ a ∈ l := ⟨mem_of_mem_erasep, Ξ» al, begin rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, lβ‚‚, h₁, hβ‚‚, h₃, hβ‚„βŸ©, { rwa h }, { rw hβ‚„, rw h₃ at al, have : a β‰  c, {rintro rfl, exact pa.elim hβ‚‚}, simpa [this] using al } end⟩ theorem erasep_map (f : Ξ² β†’ Ξ±) : βˆ€ (l : list Ξ²), (map f l).erasep p = map f (l.erasep (p ∘ f)) | [] := rfl | (b::l) := by by_cases p (f b); simp [h, erasep_map l] @[simp] theorem extractp_eq_find_erasep : βˆ€ l : list Ξ±, extractp p l = (find p l, erasep p l) | [] := rfl | (a::l) := by by_cases pa : p a; simp [extractp, pa, extractp_eq_find_erasep l] end erasep /-! ### erase -/ section erase variable [decidable_eq Ξ±] @[simp] theorem erase_nil (a : Ξ±) : [].erase a = [] := rfl theorem erase_cons (a b : Ξ±) (l : list Ξ±) : (b :: l).erase a = if b = a then l else b :: l.erase a := rfl @[simp] theorem erase_cons_head (a : Ξ±) (l : list Ξ±) : (a :: l).erase a = l := by simp only [erase_cons, if_pos rfl] @[simp] theorem erase_cons_tail {a b : Ξ±} (l : list Ξ±) (h : b β‰  a) : (b::l).erase a = b :: l.erase a := by simp only [erase_cons, if_neg h]; split; refl theorem erase_eq_erasep (a : Ξ±) (l : list Ξ±) : l.erase a = l.erasep (eq a) := by { induction l with b l, {refl}, by_cases a = b; [simp [h], simp [h, ne.symm h, *]] } @[simp, priority 980] theorem erase_of_not_mem {a : Ξ±} {l : list Ξ±} (h : a βˆ‰ l) : l.erase a = l := by rw [erase_eq_erasep, erasep_of_forall_not]; rintro b h' rfl; exact h h' theorem exists_erase_eq {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : βˆƒ l₁ lβ‚‚, a βˆ‰ l₁ ∧ l = l₁ ++ a :: lβ‚‚ ∧ l.erase a = l₁ ++ lβ‚‚ := by rcases exists_of_erasep h rfl with ⟨_, l₁, lβ‚‚, h₁, rfl, hβ‚‚, hβ‚ƒβŸ©; rw erase_eq_erasep; exact ⟨l₁, lβ‚‚, Ξ» h, h₁ _ h rfl, hβ‚‚, hβ‚ƒβŸ© @[simp] theorem length_erase_of_mem {a : Ξ±} {l : list Ξ±} (h : a ∈ l) : length (l.erase a) = pred (length l) := by rw erase_eq_erasep; exact length_erasep_of_mem h rfl theorem erase_append_left {a : Ξ±} {l₁ : list Ξ±} (lβ‚‚) (h : a ∈ l₁) : (l₁++lβ‚‚).erase a = l₁.erase a ++ lβ‚‚ := by simp [erase_eq_erasep]; exact erasep_append_left (by refl) lβ‚‚ h theorem erase_append_right {a : Ξ±} {l₁ : list Ξ±} (lβ‚‚) (h : a βˆ‰ l₁) : (l₁++lβ‚‚).erase a = l₁ ++ lβ‚‚.erase a := by rw [erase_eq_erasep, erase_eq_erasep, erasep_append_right]; rintro b h' rfl; exact h h' theorem erase_sublist (a : Ξ±) (l : list Ξ±) : l.erase a <+ l := by rw erase_eq_erasep; apply erasep_sublist theorem erase_subset (a : Ξ±) (l : list Ξ±) : l.erase a βŠ† l := (erase_sublist a l).subset theorem sublist.erase (a : Ξ±) {l₁ lβ‚‚ : list Ξ±} (h : l₁ <+ lβ‚‚) : l₁.erase a <+ lβ‚‚.erase a := by simp [erase_eq_erasep]; exact sublist.erasep h theorem mem_of_mem_erase {a b : Ξ±} {l : list Ξ±} : a ∈ l.erase b β†’ a ∈ l := @erase_subset _ _ _ _ _ @[simp] theorem mem_erase_of_ne {a b : Ξ±} {l : list Ξ±} (ab : a β‰  b) : a ∈ l.erase b ↔ a ∈ l := by rw erase_eq_erasep; exact mem_erasep_of_neg ab.symm theorem erase_comm (a b : Ξ±) (l : list Ξ±) : (l.erase a).erase b = (l.erase b).erase a := if ab : a = b then by rw ab else if ha : a ∈ l then if hb : b ∈ l then match l, l.erase a, exists_erase_eq ha, hb with | ._, ._, ⟨l₁, lβ‚‚, ha', rfl, rfl⟩, hb := if h₁ : b ∈ l₁ then by rw [erase_append_left _ h₁, erase_append_left _ h₁, erase_append_right _ (mt mem_of_mem_erase ha'), erase_cons_head] else by rw [erase_append_right _ h₁, erase_append_right _ h₁, erase_append_right _ ha', erase_cons_tail _ ab, erase_cons_head] end else by simp only [erase_of_not_mem hb, erase_of_not_mem (mt mem_of_mem_erase hb)] else by simp only [erase_of_not_mem ha, erase_of_not_mem (mt mem_of_mem_erase ha)] theorem map_erase [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (finj : injective f) {a : Ξ±} (l : list Ξ±) : map f (l.erase a) = (map f l).erase (f a) := by rw [erase_eq_erasep, erase_eq_erasep, erasep_map]; congr; ext b; simp [finj.eq_iff] theorem map_foldl_erase [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (finj : injective f) {l₁ lβ‚‚ : list Ξ±} : map f (foldl list.erase l₁ lβ‚‚) = foldl (Ξ» l a, l.erase (f a)) (map f l₁) lβ‚‚ := by induction lβ‚‚ generalizing l₁; [refl, simp only [foldl_cons, map_erase finj, *]] @[simp] theorem count_erase_self (a : Ξ±) : βˆ€ (s : list Ξ±), count a (list.erase s a) = pred (count a s) | [] := by simp | (h :: t) := begin rw erase_cons, by_cases p : h = a, { rw [if_pos p, count_cons', if_pos p.symm], simp }, { rw [if_neg p, count_cons', count_cons', if_neg (Ξ» x : a = h, p x.symm), count_erase_self], simp, } end @[simp] theorem count_erase_of_ne {a b : Ξ±} (ab : a β‰  b) : βˆ€ (s : list Ξ±), count a (list.erase s b) = count a s | [] := by simp | (x :: xs) := begin rw erase_cons, split_ifs with h, { rw [count_cons', h, if_neg ab], simp }, { rw [count_cons', count_cons', count_erase_of_ne] } end end erase /-! ### diff -/ section diff variable [decidable_eq Ξ±] @[simp] theorem diff_nil (l : list Ξ±) : l.diff [] = l := rfl @[simp] theorem diff_cons (l₁ lβ‚‚ : list Ξ±) (a : Ξ±) : l₁.diff (a::lβ‚‚) = (l₁.erase a).diff lβ‚‚ := if h : a ∈ l₁ then by simp only [list.diff, if_pos h] else by simp only [list.diff, if_neg h, erase_of_not_mem h] lemma diff_cons_right (l₁ lβ‚‚ : list Ξ±) (a : Ξ±) : l₁.diff (a::lβ‚‚) = (l₁.diff lβ‚‚).erase a := begin induction lβ‚‚ with b lβ‚‚ ih generalizing l₁ a, { simp_rw [diff_cons, diff_nil] }, { rw [diff_cons, diff_cons, erase_comm, ← diff_cons, ih, ← diff_cons] } end lemma diff_erase (l₁ lβ‚‚ : list Ξ±) (a : Ξ±) : (l₁.diff lβ‚‚).erase a = (l₁.erase a).diff lβ‚‚ := by rw [← diff_cons_right, diff_cons] @[simp] theorem nil_diff (l : list Ξ±) : [].diff l = [] := by induction l; [refl, simp only [*, diff_cons, erase_of_not_mem (not_mem_nil _)]] theorem diff_eq_foldl : βˆ€ (l₁ lβ‚‚ : list Ξ±), l₁.diff lβ‚‚ = foldl list.erase l₁ lβ‚‚ | l₁ [] := rfl | l₁ (a::lβ‚‚) := (diff_cons l₁ lβ‚‚ a).trans (diff_eq_foldl _ _) @[simp] theorem diff_append (l₁ lβ‚‚ l₃ : list Ξ±) : l₁.diff (lβ‚‚ ++ l₃) = (l₁.diff lβ‚‚).diff l₃ := by simp only [diff_eq_foldl, foldl_append] @[simp] theorem map_diff [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} (finj : injective f) {l₁ lβ‚‚ : list Ξ±} : map f (l₁.diff lβ‚‚) = (map f l₁).diff (map f lβ‚‚) := by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj] theorem diff_sublist : βˆ€ l₁ lβ‚‚ : list Ξ±, l₁.diff lβ‚‚ <+ l₁ | l₁ [] := sublist.refl _ | l₁ (a::lβ‚‚) := calc l₁.diff (a :: lβ‚‚) = (l₁.erase a).diff lβ‚‚ : diff_cons _ _ _ ... <+ l₁.erase a : diff_sublist _ _ ... <+ l₁ : list.erase_sublist _ _ theorem diff_subset (l₁ lβ‚‚ : list Ξ±) : l₁.diff lβ‚‚ βŠ† l₁ := (diff_sublist _ _).subset theorem mem_diff_of_mem {a : Ξ±} : βˆ€ {l₁ lβ‚‚ : list Ξ±}, a ∈ l₁ β†’ a βˆ‰ lβ‚‚ β†’ a ∈ l₁.diff lβ‚‚ | l₁ [] h₁ hβ‚‚ := h₁ | l₁ (b::lβ‚‚) h₁ hβ‚‚ := by rw diff_cons; exact mem_diff_of_mem ((mem_erase_of_ne (ne_of_not_mem_cons hβ‚‚)).2 h₁) (not_mem_of_not_mem_cons hβ‚‚) theorem sublist.diff_right : βˆ€ {l₁ lβ‚‚ l₃: list Ξ±}, l₁ <+ lβ‚‚ β†’ l₁.diff l₃ <+ lβ‚‚.diff l₃ | l₁ lβ‚‚ [] h := h | l₁ lβ‚‚ (a::l₃) h := by simp only [diff_cons, (h.erase _).diff_right] theorem erase_diff_erase_sublist_of_sublist {a : Ξ±} : βˆ€ {l₁ lβ‚‚ : list Ξ±}, l₁ <+ lβ‚‚ β†’ (lβ‚‚.erase a).diff (l₁.erase a) <+ lβ‚‚.diff l₁ | [] lβ‚‚ h := erase_sublist _ _ | (b::l₁) lβ‚‚ h := if heq : b = a then by simp only [heq, erase_cons_head, diff_cons] else by simpa only [erase_cons_head, erase_cons_tail _ heq, diff_cons, erase_comm a b lβ‚‚] using erase_diff_erase_sublist_of_sublist (h.erase b) end diff /-! ### enum -/ theorem length_enum_from : βˆ€ n (l : list Ξ±), length (enum_from n l) = length l | n [] := rfl | n (a::l) := congr_arg nat.succ (length_enum_from _ _) theorem length_enum : βˆ€ (l : list Ξ±), length (enum l) = length l := length_enum_from _ @[simp] theorem enum_from_nth : βˆ€ n (l : list Ξ±) m, nth (enum_from n l) m = (Ξ» a, (n + m, a)) <$> nth l m | n [] m := rfl | n (a :: l) 0 := rfl | n (a :: l) (m+1) := (enum_from_nth (n+1) l m).trans $ by rw [add_right_comm]; refl @[simp] theorem enum_nth : βˆ€ (l : list Ξ±) n, nth (enum l) n = (Ξ» a, (n, a)) <$> nth l n := by simp only [enum, enum_from_nth, zero_add]; intros; refl @[simp] theorem enum_from_map_snd : βˆ€ n (l : list Ξ±), map prod.snd (enum_from n l) = l | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_snd _ _) @[simp] theorem enum_map_snd : βˆ€ (l : list Ξ±), map prod.snd (enum l) = l := enum_from_map_snd _ theorem mem_enum_from {x : Ξ±} {i : β„•} : βˆ€ {j : β„•} (xs : list Ξ±), (i, x) ∈ xs.enum_from j β†’ j ≀ i ∧ i < j + xs.length ∧ x ∈ xs | j [] := by simp [enum_from] | j (y :: ys) := suffices i = j ∧ x = y ∨ (i, x) ∈ enum_from (j + 1) ys β†’ j ≀ i ∧ i < j + (length ys + 1) ∧ (x = y ∨ x ∈ ys), by simpa [enum_from, mem_enum_from ys], begin rintro (h|h), { refine ⟨le_of_eq h.1.symm,h.1 β–Έ _,or.inl h.2⟩, apply nat.lt_add_of_pos_right; simp }, { obtain ⟨hji, hijlen, hmem⟩ := mem_enum_from _ h, refine ⟨_, _, _⟩, { exact le_trans (nat.le_succ _) hji }, { convert hijlen using 1, ac_refl }, { simp [hmem] } } end /-! ### product -/ @[simp] theorem nil_product (l : list Ξ²) : product (@nil Ξ±) l = [] := rfl @[simp] theorem product_cons (a : Ξ±) (l₁ : list Ξ±) (lβ‚‚ : list Ξ²) : product (a::l₁) lβ‚‚ = map (Ξ» b, (a, b)) lβ‚‚ ++ product l₁ lβ‚‚ := rfl @[simp] theorem product_nil : βˆ€ (l : list Ξ±), product l (@nil Ξ²) = [] | [] := rfl | (a::l) := by rw [product_cons, product_nil]; refl @[simp] theorem mem_product {l₁ : list Ξ±} {lβ‚‚ : list Ξ²} {a : Ξ±} {b : Ξ²} : (a, b) ∈ product l₁ lβ‚‚ ↔ a ∈ l₁ ∧ b ∈ lβ‚‚ := by simp only [product, mem_bind, mem_map, prod.ext_iff, exists_prop, and.left_comm, exists_and_distrib_left, exists_eq_left, exists_eq_right] theorem length_product (l₁ : list Ξ±) (lβ‚‚ : list Ξ²) : length (product l₁ lβ‚‚) = length l₁ * length lβ‚‚ := by induction l₁ with x l₁ IH; [exact (zero_mul _).symm, simp only [length, product_cons, length_append, IH, right_distrib, one_mul, length_map, add_comm]] /-! ### sigma -/ section variable {Οƒ : Ξ± β†’ Type*} @[simp] theorem nil_sigma (l : Ξ  a, list (Οƒ a)) : (@nil Ξ±).sigma l = [] := rfl @[simp] theorem sigma_cons (a : Ξ±) (l₁ : list Ξ±) (lβ‚‚ : Ξ  a, list (Οƒ a)) : (a::l₁).sigma lβ‚‚ = map (sigma.mk a) (lβ‚‚ a) ++ l₁.sigma lβ‚‚ := rfl @[simp] theorem sigma_nil : βˆ€ (l : list Ξ±), l.sigma (Ξ» a, @nil (Οƒ a)) = [] | [] := rfl | (a::l) := by rw [sigma_cons, sigma_nil]; refl @[simp] theorem mem_sigma {l₁ : list Ξ±} {lβ‚‚ : Ξ  a, list (Οƒ a)} {a : Ξ±} {b : Οƒ a} : sigma.mk a b ∈ l₁.sigma lβ‚‚ ↔ a ∈ l₁ ∧ b ∈ lβ‚‚ a := by simp only [list.sigma, mem_bind, mem_map, exists_prop, exists_and_distrib_left, and.left_comm, exists_eq_left, heq_iff_eq, exists_eq_right] theorem length_sigma (l₁ : list Ξ±) (lβ‚‚ : Ξ  a, list (Οƒ a)) : length (l₁.sigma lβ‚‚) = (l₁.map (Ξ» a, length (lβ‚‚ a))).sum := by induction l₁ with x l₁ IH; [refl, simp only [map, sigma_cons, length_append, length_map, IH, sum_cons]] end section choose variables (p : Ξ± β†’ Prop) [decidable_pred p] (l : list Ξ±) lemma choose_spec (hp : βˆƒ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : βˆƒ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : βˆƒ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose /-! ### mapβ‚‚_left' -/ section mapβ‚‚_left' -- The definitional equalities for `mapβ‚‚_left'` can already be used by the -- simplifie because `mapβ‚‚_left'` is marked `@[simp]`. @[simp] theorem mapβ‚‚_left'_nil_right (f : Ξ± β†’ option Ξ² β†’ Ξ³) (as) : mapβ‚‚_left' f as [] = (as.map (Ξ» a, f a none), []) := by cases as; refl end mapβ‚‚_left' /-! ### mapβ‚‚_right' -/ section mapβ‚‚_right' variables (f : option Ξ± β†’ Ξ² β†’ Ξ³) (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem mapβ‚‚_right'_nil_left : mapβ‚‚_right' f [] bs = (bs.map (f none), []) := by cases bs; refl @[simp] theorem mapβ‚‚_right'_nil_right : mapβ‚‚_right' f as [] = ([], as) := rfl @[simp] theorem mapβ‚‚_right'_nil_cons : mapβ‚‚_right' f [] (b :: bs) = (f none b :: bs.map (f none), []) := rfl @[simp] theorem mapβ‚‚_right'_cons_cons : mapβ‚‚_right' f (a :: as) (b :: bs) = let rec := mapβ‚‚_right' f as bs in (f (some a) b :: rec.fst, rec.snd) := rfl end mapβ‚‚_right' /-! ### zip_left' -/ section zip_left' variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem zip_left'_nil_right : zip_left' as ([] : list Ξ²) = (as.map (Ξ» a, (a, none)), []) := by cases as; refl @[simp] theorem zip_left'_nil_left : zip_left' ([] : list Ξ±) bs = ([], bs) := rfl @[simp] theorem zip_left'_cons_nil : zip_left' (a :: as) ([] : list Ξ²) = ((a, none) :: as.map (Ξ» a, (a, none)), []) := rfl @[simp] theorem zip_left'_cons_cons : zip_left' (a :: as) (b :: bs) = let rec := zip_left' as bs in ((a, some b) :: rec.fst, rec.snd) := rfl end zip_left' /-! ### zip_right' -/ section zip_right' variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem zip_right'_nil_left : zip_right' ([] : list Ξ±) bs = (bs.map (Ξ» b, (none, b)), []) := by cases bs; refl @[simp] theorem zip_right'_nil_right : zip_right' as ([] : list Ξ²) = ([], as) := rfl @[simp] theorem zip_right'_nil_cons : zip_right' ([] : list Ξ±) (b :: bs) = ((none, b) :: bs.map (Ξ» b, (none, b)), []) := rfl @[simp] theorem zip_right'_cons_cons : zip_right' (a :: as) (b :: bs) = let rec := zip_right' as bs in ((some a, b) :: rec.fst, rec.snd) := rfl end zip_right' /-! ### mapβ‚‚_left -/ section mapβ‚‚_left variables (f : Ξ± β†’ option Ξ² β†’ Ξ³) (as : list Ξ±) -- The definitional equalities for `mapβ‚‚_left` can already be used by the -- simplifier because `mapβ‚‚_left` is marked `@[simp]`. @[simp] theorem mapβ‚‚_left_nil_right : mapβ‚‚_left f as [] = as.map (Ξ» a, f a none) := by cases as; refl theorem mapβ‚‚_left_eq_mapβ‚‚_left' : βˆ€ as bs, mapβ‚‚_left f as bs = (mapβ‚‚_left' f as bs).fst | [] bs := by simp! | (a :: as) [] := by simp! | (a :: as) (b :: bs) := by simp! [*] theorem mapβ‚‚_left_eq_mapβ‚‚ : βˆ€ as bs, length as ≀ length bs β†’ mapβ‚‚_left f as bs = mapβ‚‚ (Ξ» a b, f a (some b)) as bs | [] [] h := by simp! | [] (b :: bs) h := by simp! | (a :: as) [] h := by { simp at h, contradiction } | (a :: as) (b :: bs) h := by { simp at h, simp! [*] } end mapβ‚‚_left /-! ### mapβ‚‚_right -/ section mapβ‚‚_right variables (f : option Ξ± β†’ Ξ² β†’ Ξ³) (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem mapβ‚‚_right_nil_left : mapβ‚‚_right f [] bs = bs.map (f none) := by cases bs; refl @[simp] theorem mapβ‚‚_right_nil_right : mapβ‚‚_right f as [] = [] := rfl @[simp] theorem mapβ‚‚_right_nil_cons : mapβ‚‚_right f [] (b :: bs) = f none b :: bs.map (f none) := rfl @[simp] theorem mapβ‚‚_right_cons_cons : mapβ‚‚_right f (a :: as) (b :: bs) = f (some a) b :: mapβ‚‚_right f as bs := rfl theorem mapβ‚‚_right_eq_mapβ‚‚_right' : mapβ‚‚_right f as bs = (mapβ‚‚_right' f as bs).fst := by simp only [mapβ‚‚_right, mapβ‚‚_right', mapβ‚‚_left_eq_mapβ‚‚_left'] theorem mapβ‚‚_right_eq_mapβ‚‚ (h : length bs ≀ length as) : mapβ‚‚_right f as bs = mapβ‚‚ (Ξ» a b, f (some a) b) as bs := begin have : (Ξ» a b, flip f a (some b)) = (flip (Ξ» a b, f (some a) b)) := rfl, simp only [mapβ‚‚_right, mapβ‚‚_left_eq_mapβ‚‚, mapβ‚‚_flip, *] end end mapβ‚‚_right /-! ### zip_left -/ section zip_left variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem zip_left_nil_right : zip_left as ([] : list Ξ²) = as.map (Ξ» a, (a, none)) := by cases as; refl @[simp] theorem zip_left_nil_left : zip_left ([] : list Ξ±) bs = [] := rfl @[simp] theorem zip_left_cons_nil : zip_left (a :: as) ([] : list Ξ²) = (a, none) :: as.map (Ξ» a, (a, none)) := rfl @[simp] theorem zip_left_cons_cons : zip_left (a :: as) (b :: bs) = (a, some b) :: zip_left as bs := rfl theorem zip_left_eq_zip_left' : zip_left as bs = (zip_left' as bs).fst := by simp only [zip_left, zip_left', mapβ‚‚_left_eq_mapβ‚‚_left'] end zip_left /-! ### zip_right -/ section zip_right variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²) @[simp] theorem zip_right_nil_left : zip_right ([] : list Ξ±) bs = bs.map (Ξ» b, (none, b)) := by cases bs; refl @[simp] theorem zip_right_nil_right : zip_right as ([] : list Ξ²) = [] := rfl @[simp] theorem zip_right_nil_cons : zip_right ([] : list Ξ±) (b :: bs) = (none, b) :: bs.map (Ξ» b, (none, b)) := rfl @[simp] theorem zip_right_cons_cons : zip_right (a :: as) (b :: bs) = (some a, b) :: zip_right as bs := rfl theorem zip_right_eq_zip_right' : zip_right as bs = (zip_right' as bs).fst := by simp only [zip_right, zip_right', mapβ‚‚_right_eq_mapβ‚‚_right'] end zip_right /-! ### to_chunks -/ section to_chunks @[simp] theorem to_chunks_nil (n) : @to_chunks Ξ± n [] = [] := by cases n; refl theorem to_chunks_aux_eq (n) : βˆ€ xs i, @to_chunks_aux Ξ± n xs i = (xs.take i, (xs.drop i).to_chunks (n+1)) | [] i := by cases i; refl | (x::xs) 0 := by rw [to_chunks_aux, drop, to_chunks]; cases to_chunks_aux n xs n; refl | (x::xs) (i+1) := by rw [to_chunks_aux, to_chunks_aux_eq]; refl theorem to_chunks_eq_cons' (n) : βˆ€ {xs : list Ξ±} (h : xs β‰  []), xs.to_chunks (n+1) = xs.take (n+1) :: (xs.drop (n+1)).to_chunks (n+1) | [] e := (e rfl).elim | (x::xs) _ := by rw [to_chunks, to_chunks_aux_eq]; refl theorem to_chunks_eq_cons : βˆ€ {n} {xs : list Ξ±} (n0 : n β‰  0) (x0 : xs β‰  []), xs.to_chunks n = xs.take n :: (xs.drop n).to_chunks n | 0 _ e := (e rfl).elim | (n+1) xs _ := to_chunks_eq_cons' _ theorem to_chunks_aux_join {n} : βˆ€ {xs i l L}, @to_chunks_aux Ξ± n xs i = (l, L) β†’ l ++ L.join = xs | [] _ _ _ rfl := rfl | (x::xs) i l L e := begin cases i; [ cases e' : to_chunks_aux n xs n with l L, cases e' : to_chunks_aux n xs i with l L]; { rw [to_chunks_aux, e', to_chunks_aux] at e, cases e, exact (congr_arg (cons x) (to_chunks_aux_join e') : _) } end @[simp] theorem to_chunks_join : βˆ€ n xs, (@to_chunks Ξ± n xs).join = xs | n [] := by cases n; refl | 0 (x::xs) := by simp only [to_chunks, join]; rw append_nil | (n+1) (x::xs) := begin rw to_chunks, cases e : to_chunks_aux n xs n with l L, exact (congr_arg (cons x) (to_chunks_aux_join e) : _), end theorem to_chunks_length_le : βˆ€ n xs, n β‰  0 β†’ βˆ€ l : list Ξ±, l ∈ @to_chunks Ξ± n xs β†’ l.length ≀ n | 0 _ e _ := (e rfl).elim | (n+1) xs _ l := begin refine (measure_wf length).induction xs _, intros xs IH h, by_cases x0 : xs = [], {subst xs, cases h}, rw to_chunks_eq_cons' _ x0 at h, rcases h with rfl|h, { apply length_take_le }, { refine IH _ _ h, simp only [measure, inv_image, length_drop], exact tsub_lt_self (length_pos_iff_ne_nil.2 x0) (succ_pos _) }, end end to_chunks /-! ### Miscellaneous lemmas -/ theorem ilast'_mem : βˆ€ a l, @ilast' Ξ± a l ∈ a :: l | a [] := or.inl rfl | a (b::l) := or.inr (ilast'_mem b l) @[simp] lemma nth_le_attach (L : list Ξ±) (i) (H : i < L.attach.length) : (L.attach.nth_le i H).1 = L.nth_le i (length_attach L β–Έ H) := calc (L.attach.nth_le i H).1 = (L.attach.map subtype.val).nth_le i (by simpa using H) : by rw nth_le_map' ... = L.nth_le i _ : by congr; apply attach_map_val end list @[to_additive] theorem monoid_hom.map_list_prod {Ξ± Ξ² : Type*} [monoid Ξ±] [monoid Ξ²] (f : Ξ± β†’* Ξ²) (l : list Ξ±) : f l.prod = (l.map f).prod := (l.prod_hom f).symm open opposite /-- A morphism into the opposite monoid acts on the product by acting on the reversed elements -/ lemma monoid_hom.unop_map_list_prod {Ξ± Ξ² : Type*} [monoid Ξ±] [monoid Ξ²] (f : Ξ± β†’* Ξ²α΅’α΅–) (l : list Ξ±): unop (f l.prod) = (l.map (unop ∘ f)).reverse.prod := begin rw [f.map_list_prod l, opposite.unop_list_prod, list.map_map], end namespace list @[to_additive] theorem prod_map_hom {Ξ± Ξ² Ξ³ : Type*} [monoid Ξ²] [monoid Ξ³] (L : list Ξ±) (f : Ξ± β†’ Ξ²) (g : Ξ² β†’* Ξ³) : (L.map (g ∘ f)).prod = g ((L.map f).prod) := by {rw g.map_list_prod, exact congr_arg _ (map_map _ _ _).symm} theorem sum_map_mul_left {Ξ± : Type*} [semiring Ξ±] {Ξ² : Type*} (L : list Ξ²) (f : Ξ² β†’ Ξ±) (r : Ξ±) : (L.map (Ξ» b, r * f b)).sum = r * (L.map f).sum := sum_map_hom L f $ add_monoid_hom.mul_left r theorem sum_map_mul_right {Ξ± : Type*} [semiring Ξ±] {Ξ² : Type*} (L : list Ξ²) (f : Ξ² β†’ Ξ±) (r : Ξ±) : (L.map (Ξ» b, f b * r)).sum = (L.map f).sum * r := sum_map_hom L f $ add_monoid_hom.mul_right r universes u v @[simp] theorem mem_map_swap {Ξ± : Type u} {Ξ² : Type v} (x : Ξ±) (y : Ξ²) (xs : list (Ξ± Γ— Ξ²)) : (y, x) ∈ map prod.swap xs ↔ (x, y) ∈ xs := begin induction xs with x xs, { simp only [not_mem_nil, map_nil] }, { cases x with a b, simp only [mem_cons_iff, prod.mk.inj_iff, map, prod.swap_prod_mk, prod.exists, xs_ih, and_comm] }, end lemma slice_eq {Ξ±} (xs : list Ξ±) (n m : β„•) : slice n m xs = xs.take n ++ xs.drop (n+m) := begin induction n generalizing xs, { simp [slice] }, { cases xs; simp [slice, *, nat.succ_add], } end lemma sizeof_slice_lt {Ξ±} [has_sizeof Ξ±] (i j : β„•) (hj : 0 < j) (xs : list Ξ±) (hi : i < xs.length) : sizeof (list.slice i j xs) < sizeof xs := begin induction xs generalizing i j, case list.nil : i j h { cases hi }, case list.cons : x xs xs_ih i j h { cases i; simp only [-slice_eq, list.slice], { cases j, cases h, dsimp only [drop], unfold_wf, apply @lt_of_le_of_lt _ _ _ xs.sizeof, { clear_except, induction xs generalizing j; unfold_wf, case list.nil : j { refl }, case list.cons : xs_hd xs_tl xs_ih j { cases j; unfold_wf, refl, transitivity, apply xs_ih, simp }, }, unfold_wf, apply zero_lt_one_add, }, { unfold_wf, apply xs_ih _ _ h, apply lt_of_succ_lt_succ hi, } }, end end list
19594f4372f4d4a72aaf059769df4013527debfa
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/category_theory/category/default.lean
85a4e8640f9398fc2a9beb4587c1e889b8b459f4
[ "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
7,591
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Johannes HΓΆlzl, Reid Barton -/ import tactic.basic import tactic.tidy /-! # Categories Defines a category, as a type class parametrised by the type of objects. ## Notations Introduces notations * `X ⟢ Y` for the morphism spaces, * `f ≫ g` for composition in the 'arrows' convention. Users may like to add `f ⊚ g` for composition in the standard convention, using ```lean local notation f ` ⊚ `:80 g:80 := category.comp g f -- type as \oo ``` -/ universes v u -- The order in this declaration matters: v often needs to be explicitly specified while u often can be omitted namespace category_theory /- The propositional fields of `category` are annotated with the auto_param `obviously`, which is defined here as a [`replacer` tactic](https://leanprover-community.github.io/mathlib_docs/commands.html#def_replacer). We then immediately set up `obviously` to call `tidy`. Later, this can be replaced with more powerful tactics. -/ def_replacer obviously @[obviously] meta def obviously' := tactic.tidy class has_hom (obj : Type u) : Type (max u (v+1)) := (hom : obj β†’ obj β†’ Type v) infixr ` ⟢ `:10 := has_hom.hom -- type as \h section prio set_option default_priority 100 -- see Note [default priority] class category_struct (obj : Type u) extends has_hom.{v} obj : Type (max u (v+1)) := (id : Ξ  X : obj, hom X X) (comp : Ξ  {X Y Z : obj}, (X ⟢ Y) β†’ (Y ⟢ Z) β†’ (X ⟢ Z)) notation `πŸ™` := category_struct.id -- type as \b1 infixr ` ≫ `:80 := category_struct.comp -- type as \gg /-- The typeclass `category C` describes morphisms associated to objects of type `C`. The universe levels of the objects and morphisms are unconstrained, and will often need to be specified explicitly, as `category.{v} C`. (See also `large_category` and `small_category`.) -/ class category (obj : Type u) extends category_struct.{v} obj : Type (max u (v+1)) := (id_comp' : βˆ€ {X Y : obj} (f : hom X Y), πŸ™ X ≫ f = f . obviously) (comp_id' : βˆ€ {X Y : obj} (f : hom X Y), f ≫ πŸ™ Y = f . obviously) (assoc' : βˆ€ {W X Y Z : obj} (f : hom W X) (g : hom X Y) (h : hom Y Z), (f ≫ g) ≫ h = f ≫ (g ≫ h) . obviously) end prio -- `restate_axiom` is a command that creates a lemma from a structure field, -- discarding any auto_param wrappers from the type. -- (It removes a backtick from the name, if it finds one, and otherwise adds "_lemma".) restate_axiom category.id_comp' restate_axiom category.comp_id' restate_axiom category.assoc' attribute [simp] category.id_comp category.comp_id category.assoc attribute [trans] category_struct.comp /-- A `large_category` has objects in one universe level higher than the universe level of the morphisms. It is useful for examples such as the category of types, or the category of groups, etc. -/ abbreviation large_category (C : Type (u+1)) : Type (u+1) := category.{u} C /-- A `small_category` has objects and morphisms in the same universe level. -/ abbreviation small_category (C : Type u) : Type (u+1) := category.{u} C section variables {C : Type u} [π’ž : category.{v} C] {X Y Z : C} include π’ž /-- postcompose an equation between morphisms by another morphism -/ lemma eq_whisker {f g : X ⟢ Y} (w : f = g) (h : Y ⟢ Z) : f ≫ h = g ≫ h := by rw w /-- precompose an equation between morphisms by another morphism -/ lemma whisker_eq (f : X ⟢ Y) {g h : Y ⟢ Z} (w : g = h) : f ≫ g = f ≫ h := by rw w infixr ` =≫ `:80 := eq_whisker infixr ` ≫= `:80 := whisker_eq lemma eq_of_comp_left_eq {f g : X ⟢ Y} (w : βˆ€ {Z : C} (h : Y ⟢ Z), f ≫ h = g ≫ h) : f = g := by { convert w (πŸ™ Y), tidy } lemma eq_of_comp_right_eq {f g : Y ⟢ Z} (w : βˆ€ {X : C} (h : X ⟢ Y), h ≫ f = h ≫ g) : f = g := by { convert w (πŸ™ Y), tidy } lemma eq_of_comp_left_eq' (f g : X ⟢ Y) (w : (Ξ» {Z : C} (h : Y ⟢ Z), f ≫ h) = (Ξ» {Z : C} (h : Y ⟢ Z), g ≫ h)) : f = g := eq_of_comp_left_eq (Ξ» Z h, by convert congr_fun (congr_fun w Z) h) lemma eq_of_comp_right_eq' (f g : Y ⟢ Z) (w : (Ξ» {X : C} (h : X ⟢ Y), h ≫ f) = (Ξ» {X : C} (h : X ⟢ Y), h ≫ g)) : f = g := eq_of_comp_right_eq (Ξ» X h, by convert congr_fun (congr_fun w X) h) lemma id_of_comp_left_id (f : X ⟢ X) (w : βˆ€ {Y : C} (g : X ⟢ Y), f ≫ g = g) : f = πŸ™ X := by { convert w (πŸ™ X), tidy } lemma id_of_comp_right_id (f : X ⟢ X) (w : βˆ€ {Y : C} (g : Y ⟢ X), g ≫ f = g) : f = πŸ™ X := by { convert w (πŸ™ X), tidy } class epi (f : X ⟢ Y) : Prop := (left_cancellation : Ξ  {Z : C} (g h : Y ⟢ Z) (w : f ≫ g = f ≫ h), g = h) class mono (f : X ⟢ Y) : Prop := (right_cancellation : Ξ  {Z : C} (g h : Z ⟢ X) (w : g ≫ f = h ≫ f), g = h) instance (X : C) : epi.{v} (πŸ™ X) := ⟨λ Z g h w, by simpa using w⟩ instance (X : C) : mono.{v} (πŸ™ X) := ⟨λ Z g h w, by simpa using w⟩ lemma cancel_epi (f : X ⟢ Y) [epi f] {g h : Y ⟢ Z} : (f ≫ g = f ≫ h) ↔ g = h := ⟨ Ξ» p, epi.left_cancellation g h p, begin intro a, subst a end ⟩ lemma cancel_mono (f : X ⟢ Y) [mono f] {g h : Z ⟢ X} : (g ≫ f = h ≫ f) ↔ g = h := ⟨ Ξ» p, mono.right_cancellation g h p, begin intro a, subst a end ⟩ lemma cancel_epi_id (f : X ⟢ Y) [epi f] {h : Y ⟢ Y} : (f ≫ h = f) ↔ h = πŸ™ Y := by { convert cancel_epi f, simp, } lemma cancel_mono_id (f : X ⟢ Y) [mono f] {g : X ⟢ X} : (g ≫ f = f) ↔ g = πŸ™ X := by { convert cancel_mono f, simp, } lemma epi_comp {X Y Z : C} (f : X ⟢ Y) [epi f] (g : Y ⟢ Z) [epi g] : epi (f ≫ g) := begin split, intros Z a b w, apply (cancel_epi g).1, apply (cancel_epi f).1, simpa using w, end lemma mono_comp {X Y Z : C} (f : X ⟢ Y) [mono f] (g : Y ⟢ Z) [mono g] : mono (f ≫ g) := begin split, intros Z a b w, apply (cancel_mono f).1, apply (cancel_mono g).1, simpa using w, end lemma mono_of_mono {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [mono (f ≫ g)] : mono f := begin split, intros Z a b w, replace w := congr_arg (Ξ» k, k ≫ g) w, dsimp at w, rw [category.assoc, category.assoc] at w, exact (cancel_mono _).1 w, end lemma mono_of_mono_fac {X Y Z : C} {f : X ⟢ Y} {g : Y ⟢ Z} {h : X ⟢ Z} [mono h] (w : f ≫ g = h) : mono f := by { resetI, subst h, exact mono_of_mono f g, } lemma epi_of_epi {X Y Z : C} (f : X ⟢ Y) (g : Y ⟢ Z) [epi (f ≫ g)] : epi g := begin split, intros Z a b w, replace w := congr_arg (Ξ» k, f ≫ k) w, dsimp at w, rw [←category.assoc, ←category.assoc] at w, exact (cancel_epi _).1 w, end lemma epi_of_epi_fac {X Y Z : C} {f : X ⟢ Y} {g : Y ⟢ Z} {h : X ⟢ Z} [epi h] (w : f ≫ g = h) : epi g := by { resetI, subst h, exact epi_of_epi f g, } end section variable (C : Type u) variable [category.{v} C] universe u' instance ulift_category : category.{v} (ulift.{u'} C) := { hom := Ξ» X Y, (X.down ⟢ Y.down), id := Ξ» X, πŸ™ X.down, comp := Ξ» _ _ _ f g, f ≫ g } -- We verify that this previous instance can lift small categories to large categories. example (D : Type u) [small_category D] : large_category (ulift.{u+1} D) := by apply_instance end end category_theory open category_theory namespace preorder variables (Ξ± : Type u) @[priority 100] -- see Note [lower instance priority] instance small_category [preorder Ξ±] : small_category Ξ± := { hom := Ξ» U V, ulift (plift (U ≀ V)), id := Ξ» X, ⟨ ⟨ le_refl X ⟩ ⟩, comp := Ξ» X Y Z f g, ⟨ ⟨ le_trans _ _ _ f.down.down g.down.down ⟩ ⟩ } end preorder
c8d0a4379e224ae134f77151e169afcdaf5f2fe5
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/data/polynomial/ring_division.lean
052ae4103a8caa4fe359c4fb5dd0f487c595e48b
[ "Apache-2.0" ]
permissive
spolu/mathlib
bacf18c3d2a561d00ecdc9413187729dd1f705ed
480c92cdfe1cf3c2d083abded87e82162e8814f4
refs/heads/master
1,671,684,094,325
1,600,736,045,000
1,600,736,045,000
297,564,749
1
0
null
1,600,758,368,000
1,600,758,367,000
null
UTF-8
Lean
false
false
20,133
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes HΓΆlzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.basic import data.polynomial.div import data.polynomial.algebra_map /-! # Theory of univariate polynomials This file starts looking like the ring theory of $ R[X] $ -/ noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open finset namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : β„•} section comm_ring variables [comm_ring R] {p q : polynomial R} variables [comm_ring S] lemma nat_degree_pos_of_aeval_root [algebra R S] {p : polynomial R} (hp : p β‰  0) {z : S} (hz : aeval z p = 0) (inj : βˆ€ (x : R), algebra_map R S x = 0 β†’ x = 0) : 0 < p.nat_degree := nat_degree_pos_of_evalβ‚‚_root hp (algebra_map R S) hz inj lemma degree_pos_of_aeval_root [algebra R S] {p : polynomial R} (hp : p β‰  0) {z : S} (hz : aeval z p = 0) (inj : βˆ€ (x : R), algebra_map R S x = 0 β†’ x = 0) : 0 < p.degree := nat_degree_pos_iff_degree_pos.mp (nat_degree_pos_of_aeval_root hp hz inj) end comm_ring section integral_domain variables [integral_domain R] {p q : polynomial R} instance : integral_domain (polynomial R) := { eq_zero_or_eq_zero_of_mul_eq_zero := Ξ» a b h, begin have : leading_coeff 0 = leading_coeff a * leading_coeff b := h β–Έ leading_coeff_mul a b, rw [leading_coeff_zero, eq_comm] at this, erw [← leading_coeff_eq_zero, ← leading_coeff_eq_zero], exact eq_zero_or_eq_zero_of_mul_eq_zero this end, ..polynomial.nontrivial, ..polynomial.comm_ring } lemma nat_degree_mul (hp : p β‰  0) (hq : q β‰  0) : nat_degree (p * q) = nat_degree p + nat_degree q := by rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree (mul_ne_zero hp hq), with_bot.coe_add, ← degree_eq_nat_degree hp, ← degree_eq_nat_degree hq, degree_mul] @[simp] lemma nat_degree_pow (p : polynomial R) (n : β„•) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp [hp0, hn0] else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else nat_degree_pow' (by rw [← leading_coeff_pow, ne.def, leading_coeff_eq_zero]; exact pow_ne_zero _ hp0) lemma root_mul : is_root (p * q) a ↔ is_root p a ∨ is_root q a := by simp_rw [is_root, eval_mul, mul_eq_zero] lemma root_or_root_of_root_mul (h : is_root (p * q) a) : is_root p a ∨ is_root q a := root_mul.1 h lemma degree_le_mul_left (p : polynomial R) (hq : q β‰  0) : degree p ≀ degree (p * q) := if hp : p = 0 then by simp only [hp, zero_mul, le_refl] else by rw [degree_mul, degree_eq_nat_degree hp, degree_eq_nat_degree hq]; exact with_bot.coe_le_coe.2 (nat.le_add_right _ _) theorem nat_degree_le_of_dvd {p q : polynomial R} (h1 : p ∣ q) (h2 : q β‰  0) : p.nat_degree ≀ q.nat_degree := begin rcases h1 with ⟨q, rfl⟩, rw mul_ne_zero_iff at h2, rw [nat_degree_mul h2.1 h2.2], exact nat.le_add_right _ _ end section roots open multiset local attribute [reducible] with_zero lemma degree_eq_zero_of_is_unit (h : is_unit p) : degree p = 0 := let ⟨q, hq⟩ := is_unit_iff_dvd_one.1 h in have hp0 : p β‰  0, from Ξ» hp0, by simpa [hp0] using hq, have hq0 : q β‰  0, from Ξ» hp0, by simpa [hp0] using hq, have nat_degree (1 : polynomial R) = nat_degree (p * q), from congr_arg _ hq, by rw [nat_degree_one, nat_degree_mul hp0 hq0, eq_comm, _root_.add_eq_zero_iff, ← with_bot.coe_eq_coe, ← degree_eq_nat_degree hp0] at this; exact this.1 @[simp] lemma degree_coe_units (u : units (polynomial R)) : degree (u : polynomial R) = 0 := degree_eq_zero_of_is_unit ⟨u, rfl⟩ theorem prime_X_sub_C {r : R} : prime (X - C r) := ⟨X_sub_C_ne_zero r, not_is_unit_X_sub_C, Ξ» _ _, by { simp_rw [dvd_iff_is_root, is_root.def, eval_mul, mul_eq_zero], exact id }⟩ theorem prime_X : prime (X : polynomial R) := by { convert (prime_X_sub_C : prime (X - C 0 : polynomial R)), simp } lemma prime_of_degree_eq_one_of_monic (hp1 : degree p = 1) (hm : monic p) : prime p := have p = X - C (- p.coeff 0), by simpa [hm.leading_coeff] using eq_X_add_C_of_degree_eq_one hp1, this.symm β–Έ prime_X_sub_C theorem irreducible_X_sub_C (r : R) : irreducible (X - C r) := irreducible_of_prime prime_X_sub_C theorem irreducible_X : irreducible (X : polynomial R) := irreducible_of_prime prime_X lemma irreducible_of_degree_eq_one_of_monic (hp1 : degree p = 1) (hm : monic p) : irreducible p := irreducible_of_prime (prime_of_degree_eq_one_of_monic hp1 hm) theorem eq_of_monic_of_associated (hp : p.monic) (hq : q.monic) (hpq : associated p q) : p = q := begin obtain ⟨u, hu⟩ := hpq, unfold monic at hp hq, rw eq_C_of_degree_le_zero (le_of_eq $ degree_coe_units _) at hu, rw [← hu, leading_coeff_mul, hp, one_mul, leading_coeff_C] at hq, rwa [hq, C_1, mul_one] at hu end @[simp] lemma root_multiplicity_zero {x : R} : root_multiplicity x 0 = 0 := dif_pos rfl lemma root_multiplicity_eq_zero {p : polynomial R} {x : R} (h : Β¬ is_root p x) : root_multiplicity x p = 0 := begin rw root_multiplicity_eq_multiplicity, split_ifs, { refl }, rw [← enat.coe_inj, enat.coe_get, multiplicity.multiplicity_eq_zero_of_not_dvd, enat.coe_zero], intro hdvd, exact h (dvd_iff_is_root.mp hdvd) end lemma root_multiplicity_pos {p : polynomial R} (hp : p β‰  0) {x : R} : 0 < root_multiplicity x p ↔ is_root p x := begin rw [← dvd_iff_is_root, root_multiplicity_eq_multiplicity, dif_neg hp, ← enat.coe_lt_coe, enat.coe_get], exact multiplicity.dvd_iff_multiplicity_pos end lemma root_multiplicity_mul {p q : polynomial R} {x : R} (hpq : p * q β‰  0) : root_multiplicity x (p * q) = root_multiplicity x p + root_multiplicity x q := begin have hp : p β‰  0 := left_ne_zero_of_mul hpq, have hq : q β‰  0 := right_ne_zero_of_mul hpq, rw [root_multiplicity_eq_multiplicity (p * q), dif_neg hpq, root_multiplicity_eq_multiplicity p, dif_neg hp, root_multiplicity_eq_multiplicity q, dif_neg hq, @multiplicity.mul' _ _ _ (X - C x) _ _ prime_X_sub_C], end lemma root_multiplicity_X_sub_C_self {x : R} : root_multiplicity x (X - C x) = 1 := by rw [root_multiplicity_eq_multiplicity, dif_neg (X_sub_C_ne_zero x), multiplicity.get_multiplicity_self] lemma root_multiplicity_X_sub_C {x y : R} : root_multiplicity x (X - C y) = if x = y then 1 else 0 := begin split_ifs with hxy, { rw hxy, exact root_multiplicity_X_sub_C_self }, exact root_multiplicity_eq_zero (mt root_X_sub_C.mp (ne.symm hxy)) end lemma exists_multiset_roots : βˆ€ {p : polynomial R} (hp : p β‰  0), βˆƒ s : multiset R, (s.card : with_bot β„•) ≀ degree p ∧ βˆ€ a, s.count a = root_multiplicity a p | p := Ξ» hp, by haveI := classical.prop_decidable (βˆƒ x, is_root p x); exact if h : βˆƒ x, is_root p x then let ⟨x, hx⟩ := h in have hpd : 0 < degree p := degree_pos_of_root hp hx, have hd0 : p /β‚˜ (X - C x) β‰  0 := Ξ» h, by rw [← mul_div_by_monic_eq_iff_is_root.2 hx, h, mul_zero] at hp; exact hp rfl, have wf : degree (p /β‚˜ _) < degree p := degree_div_by_monic_lt _ (monic_X_sub_C x) hp ((degree_X_sub_C x).symm β–Έ dec_trivial), let ⟨t, htd, htr⟩ := @exists_multiset_roots (p /β‚˜ (X - C x)) hd0 in have hdeg : degree (X - C x) ≀ degree p := begin rw [degree_X_sub_C, degree_eq_nat_degree hp], rw degree_eq_nat_degree hp at hpd, exact with_bot.coe_le_coe.2 (with_bot.coe_lt_coe.1 hpd) end, have hdiv0 : p /β‚˜ (X - C x) β‰  0 := mt (div_by_monic_eq_zero_iff (monic_X_sub_C x) (ne_zero_of_monic (monic_X_sub_C x))).1 $ not_lt.2 hdeg, ⟨x :: t, calc (card (x :: t) : with_bot β„•) = t.card + 1 : by exact_mod_cast card_cons _ _ ... ≀ degree p : by rw [← degree_add_div_by_monic (monic_X_sub_C x) hdeg, degree_X_sub_C, add_comm]; exact add_le_add (le_refl (1 : with_bot β„•)) htd, begin assume a, conv_rhs { rw ← mul_div_by_monic_eq_iff_is_root.mpr hx }, rw [root_multiplicity_mul (mul_ne_zero (X_sub_C_ne_zero _) hdiv0), root_multiplicity_X_sub_C, ← htr a], split_ifs with ha, { rw [ha, count_cons_self, nat.succ_eq_add_one, add_comm] }, { rw [count_cons_of_ne ha, zero_add] }, end⟩ else ⟨0, (degree_eq_nat_degree hp).symm β–Έ with_bot.coe_le_coe.2 (nat.zero_le _), by { intro a, rw [count_zero, root_multiplicity_eq_zero (not_exists.mp h a)] }⟩ using_well_founded {dec_tac := tactic.assumption} /-- `roots p` noncomputably gives a multiset containing all the roots of `p`, including their multiplicities. -/ noncomputable def roots (p : polynomial R) : multiset R := if h : p = 0 then βˆ… else classical.some (exists_multiset_roots h) @[simp] lemma roots_zero : (0 : polynomial R).roots = 0 := dif_pos rfl lemma card_roots (hp0 : p β‰  0) : ((roots p).card : with_bot β„•) ≀ degree p := begin unfold roots, rw dif_neg hp0, exact (classical.some_spec (exists_multiset_roots hp0)).1 end lemma card_roots' {p : polynomial R} (hp0 : p β‰  0) : p.roots.card ≀ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots hp0) (le_of_eq $ degree_eq_nat_degree hp0)) lemma card_roots_sub_C {p : polynomial R} {a : R} (hp0 : 0 < degree p) : ((p - C a).roots.card : with_bot β„•) ≀ degree p := calc ((p - C a).roots.card : with_bot β„•) ≀ degree (p - C a) : card_roots $ mt sub_eq_zero.1 $ Ξ» h, not_le_of_gt hp0 $ h.symm β–Έ degree_C_le ... = degree p : by rw [sub_eq_add_neg, ← C_neg]; exact degree_add_C hp0 lemma card_roots_sub_C' {p : polynomial R} {a : R} (hp0 : 0 < degree p) : (p - C a).roots.card ≀ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots_sub_C hp0) (le_of_eq $ degree_eq_nat_degree (Ξ» h, by simp [*, lt_irrefl] at *))) @[simp] lemma count_roots (hp : p β‰  0) : p.roots.count a = root_multiplicity a p := by { rw [roots, dif_neg hp], exact (classical.some_spec (exists_multiset_roots hp)).2 a } @[simp] lemma mem_roots (hp : p β‰  0) : a ∈ p.roots ↔ is_root p a := by rw [← count_pos, count_roots hp, root_multiplicity_pos hp] lemma roots_mul {p q : polynomial R} (hpq : p * q β‰  0) : (p * q).roots = p.roots + q.roots := multiset.ext.mpr $ Ξ» r, by rw [count_add, count_roots hpq, count_roots (left_ne_zero_of_mul hpq), count_roots (right_ne_zero_of_mul hpq), root_multiplicity_mul hpq] @[simp] lemma mem_roots_sub_C {p : polynomial R} {a x : R} (hp0 : 0 < degree p) : x ∈ (p - C a).roots ↔ p.eval x = a := (mem_roots (show p - C a β‰  0, from mt sub_eq_zero.1 $ Ξ» h, not_le_of_gt hp0 $ h.symm β–Έ degree_C_le)).trans (by rw [is_root.def, eval_sub, eval_C, sub_eq_zero]) @[simp] lemma roots_X_sub_C (r : R) : roots (X - C r) = r :: 0 := begin ext s, rw [count_roots (X_sub_C_ne_zero r), root_multiplicity_X_sub_C], split_ifs with h, { rw [h, count_singleton] }, { rw [count_cons_of_ne h, count_zero] } end @[simp] lemma roots_C (x : R) : (C x).roots = 0 := if H : x = 0 then by rw [H, C_0, roots_zero] else multiset.ext.mpr $ Ξ» r, have h : C x β‰  0, from Ξ» h, H $ C_inj.1 $ h.symm β–Έ C_0.symm, have not_root : Β¬ is_root (C x) r := mt (Ξ» (h : eval r (C x) = 0), trans eval_C.symm h) H, by rw [count_roots h, count_zero, root_multiplicity_eq_zero not_root] @[simp] lemma roots_one : (1 : polynomial R).roots = βˆ… := roots_C 1 lemma roots_list_prod (L : list (polynomial R)) : (βˆ€ p ∈ L, (p : _) β‰  0) β†’ L.prod.roots = (L : multiset (polynomial R)).bind roots := list.rec_on L (Ξ» _, roots_one) $ Ξ» hd tl ih H, begin rw list.forall_mem_cons at H, rw [list.prod_cons, roots_mul (mul_ne_zero H.1 $ list.prod_ne_zero H.2), ← multiset.cons_coe, multiset.cons_bind, ih H.2] end lemma roots_multiset_prod (m : multiset (polynomial R)) : (βˆ€ p ∈ m, (p : _) β‰  0) β†’ m.prod.roots = m.bind roots := multiset.induction_on m (Ξ» _, roots_one) $ Ξ» hd tl ih H, begin rw multiset.forall_mem_cons at H, rw [multiset.prod_cons, roots_mul (mul_ne_zero H.1 $ multiset.prod_ne_zero H.2), multiset.cons_bind, ih H.2] end lemma roots_prod {ΞΉ : Type*} (f : ΞΉ β†’ polynomial R) (s : finset ΞΉ) : s.prod f β‰  0 β†’ (s.prod f).roots = s.val.bind (Ξ» i, roots (f i)) := begin refine s.induction_on _ _, { intros, exact roots_one }, intros i s hi ih ne_zero, rw prod_insert hi at ⊒ ne_zero, rw [roots_mul ne_zero, ih (right_ne_zero_of_mul ne_zero), insert_val, ndinsert_of_not_mem hi, cons_bind] end lemma roots_prod_X_sub_C (s : finset R) : (s.prod (Ξ» a, X - C a)).roots = s.val := (roots_prod (Ξ» a, X - C a) s (prod_ne_zero_iff.mpr (Ξ» a _, X_sub_C_ne_zero a))).trans (by simp_rw [roots_X_sub_C, bind_cons, bind_zero, add_zero, multiset.map_id']) lemma card_roots_X_pow_sub_C {n : β„•} (hn : 0 < n) (a : R) : (roots ((X : polynomial R) ^ n - C a)).card ≀ n := with_bot.coe_le_coe.1 $ calc ((roots ((X : polynomial R) ^ n - C a)).card : with_bot β„•) ≀ degree ((X : polynomial R) ^ n - C a) : card_roots (X_pow_sub_C_ne_zero hn a) ... = n : degree_X_pow_sub_C hn a /-- `nth_roots n a` noncomputably returns the solutions to `x ^ n = a`-/ def nth_roots {R : Type*} [integral_domain R] (n : β„•) (a : R) : multiset R := roots ((X : polynomial R) ^ n - C a) @[simp] lemma mem_nth_roots {R : Type*} [integral_domain R] {n : β„•} (hn : 0 < n) {a x : R} : x ∈ nth_roots n a ↔ x ^ n = a := by rw [nth_roots, mem_roots (X_pow_sub_C_ne_zero hn a), is_root.def, eval_sub, eval_C, eval_pow, eval_X, sub_eq_zero_iff_eq] lemma card_nth_roots {R : Type*} [integral_domain R] (n : β„•) (a : R) : (nth_roots n a).card ≀ n := if hn : n = 0 then if h : (X : polynomial R) ^ n - C a = 0 then by simp only [nat.zero_le, nth_roots, roots, h, dif_pos rfl, empty_eq_zero, card_zero] else with_bot.coe_le_coe.1 (le_trans (card_roots h) (by rw [hn, pow_zero, ← C_1, ← @is_ring_hom.map_sub _ _ _ _ (@C R _)]; exact degree_C_le)) else by rw [← with_bot.coe_le_coe, ← degree_X_pow_sub_C (nat.pos_of_ne_zero hn) a]; exact card_roots (X_pow_sub_C_ne_zero (nat.pos_of_ne_zero hn) a) lemma coeff_comp_degree_mul_degree (hqd0 : nat_degree q β‰  0) : coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p := if hp0 : p = 0 then by simp [hp0] else calc coeff (p.comp q) (nat_degree p * nat_degree q) = p.sum (Ξ» n a, coeff (C a * q ^ n) (nat_degree p * nat_degree q)) : by rw [comp, evalβ‚‚, coeff_sum] ... = coeff (C (leading_coeff p) * q ^ nat_degree p) (nat_degree p * nat_degree q) : finset.sum_eq_single _ begin assume b hbs hbp, have hq0 : q β‰  0, from Ξ» hq0, hqd0 (by rw [hq0, nat_degree_zero]), have : coeff p b β‰  0, rwa finsupp.mem_support_iff at hbs, refine coeff_eq_zero_of_degree_lt _, rw [degree_mul], erw degree_C this, rw [degree_pow, zero_add, degree_eq_nat_degree hq0, ← with_bot.coe_nsmul, nsmul_eq_mul, with_bot.coe_lt_coe, nat.cast_id], rw mul_lt_mul_right, apply lt_of_le_of_ne, assumption', swap, omega, exact le_nat_degree_of_ne_zero this, end begin intro h, contrapose! hp0, rw finsupp.mem_support_iff at h, push_neg at h, rwa ← leading_coeff_eq_zero, end ... = _ : have coeff (q ^ nat_degree p) (nat_degree p * nat_degree q) = leading_coeff (q ^ nat_degree p), by rw [leading_coeff, nat_degree_pow], by rw [coeff_C_mul, this, leading_coeff_pow] lemma nat_degree_comp : nat_degree (p.comp q) = nat_degree p * nat_degree q := le_antisymm nat_degree_comp_le (if hp0 : p = 0 then by rw [hp0, zero_comp, nat_degree_zero, zero_mul] else if hqd0 : nat_degree q = 0 then have degree q ≀ 0, by rw [← with_bot.coe_zero, ← hqd0]; exact degree_le_nat_degree, by rw [eq_C_of_degree_le_zero this]; simp else le_nat_degree_of_ne_zero $ have hq0 : q β‰  0, from Ξ» hq0, hqd0 $ by rw [hq0, nat_degree_zero], calc coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p : coeff_comp_degree_mul_degree hqd0 ... β‰  0 : mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (pow_ne_zero _ (mt leading_coeff_eq_zero.1 hq0))) lemma leading_coeff_comp (hq : nat_degree q β‰  0) : leading_coeff (p.comp q) = leading_coeff p * leading_coeff q ^ nat_degree p := by rw [← coeff_comp_degree_mul_degree hq, ← nat_degree_comp]; refl lemma units_coeff_zero_smul (c : units (polynomial R)) (p : polynomial R) : (c : polynomial R).coeff 0 β€’ p = c * p := by rw [←polynomial.C_mul', ←polynomial.eq_C_of_degree_eq_zero (degree_coe_units c)] @[simp] lemma nat_degree_coe_units (u : units (polynomial R)) : nat_degree (u : polynomial R) = 0 := nat_degree_eq_of_degree_eq_some (degree_coe_units u) end roots theorem is_unit_iff {f : polynomial R} : is_unit f ↔ βˆƒ r : R, is_unit r ∧ C r = f := ⟨λ hf, ⟨f.coeff 0, is_unit_C.1 $ eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf) β–Έ hf, (eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf)).symm⟩, Ξ» ⟨r, hr, hrf⟩, hrf β–Έ is_unit_C.2 hr⟩ lemma coeff_coe_units_zero_ne_zero (u : units (polynomial R)) : coeff (u : polynomial R) 0 β‰  0 := begin conv in (0) {rw [← nat_degree_coe_units u]}, rw [← leading_coeff, ne.def, leading_coeff_eq_zero], exact units.ne_zero _ end lemma degree_eq_degree_of_associated (h : associated p q) : degree p = degree q := let ⟨u, hu⟩ := h in by simp [hu.symm] lemma degree_eq_one_of_irreducible_of_root (hi : irreducible p) {x : R} (hx : is_root p x) : degree p = 1 := let ⟨g, hg⟩ := dvd_iff_is_root.2 hx in have is_unit (X - C x) ∨ is_unit g, from hi.2 _ _ hg, this.elim (Ξ» h, have h₁ : degree (X - C x) = 1, from degree_X_sub_C x, have hβ‚‚ : degree (X - C x) = 0, from degree_eq_zero_of_is_unit h, by rw h₁ at hβ‚‚; exact absurd hβ‚‚ dec_trivial) (Ξ» hgu, by rw [hg, degree_mul, degree_X_sub_C, degree_eq_zero_of_is_unit hgu, add_zero]) end integral_domain section variables [semiring R] [integral_domain S] (Ο† : R β†’+* S) lemma is_unit_of_is_unit_leading_coeff_of_is_unit_map (f : polynomial R) (hf : is_unit (leading_coeff f)) (H : is_unit (map Ο† f)) : is_unit f := begin have dz := degree_eq_zero_of_is_unit H, rw degree_map_eq_of_leading_coeff_ne_zero at dz, { rw eq_C_of_degree_eq_zero dz, apply is_unit.map', convert hf, rw (degree_eq_iff_nat_degree_eq _).1 dz, rintro rfl, simpa using H, }, { intro h, have u : is_unit (Ο† f.leading_coeff) := is_unit.map' _ hf, rw h at u, simpa using u, } end end section variables [integral_domain R] [integral_domain S] (Ο† : R β†’+* S) /-- A polynomial over an integral domain `R` is irreducible if it is monic and irreducible after mapping into an integral domain `S`. A special case of this lemma is that a polynomial over `β„€` is irreducible if it is monic and irreducible over `β„€/pβ„€` for some prime `p`. -/ lemma irreducible_of_irreducible_map (f : polynomial R) (h_mon : monic f) (h_irr : irreducible (map Ο† f)) : irreducible f := begin fsplit, { intro h, exact h_irr.1 (is_unit.map (monoid_hom.of (map Ο†)) h), }, { intros a b h, have q := (leading_coeff_mul a b).symm, rw ←h at q, dsimp [monic] at h_mon, rw h_mon at q, have au : is_unit a.leading_coeff := is_unit_of_mul_eq_one _ _ q, rw mul_comm at q, have bu : is_unit b.leading_coeff := is_unit_of_mul_eq_one _ _ q, clear q h_mon, have h' := congr_arg (map Ο†) h, simp only [map_mul] at h', cases h_irr.2 _ _ h' with w w, { left, exact is_unit_of_is_unit_leading_coeff_of_is_unit_map _ _ au w, }, { right, exact is_unit_of_is_unit_leading_coeff_of_is_unit_map _ _ bu w, }, } end end end polynomial namespace is_integral_domain variables {R : Type*} [comm_ring R] /-- Lift evidence that `is_integral_domain R` to `is_integral_domain (polynomial R)`. -/ lemma polynomial (h : is_integral_domain R) : is_integral_domain (polynomial R) := @integral_domain.to_is_integral_domain _ (@polynomial.integral_domain _ (h.to_integral_domain _)) end is_integral_domain
23b96a607cfb479b07d9b530fc92271f1da12f1e
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/limits/shapes/images.lean
32f0f5051e324c9efa016a337dfef72183a0f821
[ "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
30,024
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.pullbacks import category_theory.limits.shapes.strong_epi /-! # Categorical images We define the categorical image of `f` as a factorisation `f = e ≫ m` through a monomorphism `m`, so that `m` factors through the `m'` in any other such factorisation. ## Main definitions * A `mono_factorisation` is a factorisation `f = e ≫ m`, where `m` is a monomorphism * `is_image F` means that a given mono factorisation `F` has the universal property of the image. * `has_image f` means that we have chosen an image for the morphism `f : X ⟢ Y`. * In this case, `image f` is the image object, `image.ΞΉ f : image f ⟢ Y` is the monomorphism `m` of the factorisation and `factor_thru_image f : X ⟢ image f` is the morphism `e`. * `has_images C` means that every morphism in `C` has an image. * Let `f : X ⟢ Y` and `g : P ⟢ Q` be morphisms in `C`, which we will represent as objects of the arrow category `arrow C`. Then `sq : f ⟢ g` is a commutative square in `C`. If `f` and `g` have images, then `has_image_map sq` represents the fact that there is a morphism `i : image f ⟢ image g` making the diagram X ----β†’ image f ----β†’ Y | | | | | | ↓ ↓ ↓ P ----β†’ image g ----β†’ Q commute, where the top row is the image factorisation of `f`, the bottom row is the image factorisation of `g`, and the outer rectangle is the commutative square `sq`. * If a category `has_images`, then `has_image_maps` means that every commutative square admits an image map. * If a category `has_images`, then `has_strong_epi_images` means that the morphism to the image is always a strong epimorphism. ## Main statements * When `C` has equalizers, the morphism `e` appearing in an image factorisation is an epimorphism. * When `C` has strong epi images, then these images admit image maps. ## Future work * TODO: coimages, and abelian categories. * TODO: connect this with existing working in the group theory and ring theory libraries. -/ noncomputable theory universes v u open category_theory open category_theory.limits.walking_parallel_pair namespace category_theory.limits variables {C : Type u} [category.{v} C] variables {X Y : C} (f : X ⟢ Y) /-- A factorisation of a morphism `f = e ≫ m`, with `m` monic. -/ structure mono_factorisation (f : X ⟢ Y) := (I : C) (m : I ⟢ Y) [m_mono : mono m] (e : X ⟢ I) (fac' : e ≫ m = f . obviously) restate_axiom mono_factorisation.fac' attribute [simp, reassoc] mono_factorisation.fac attribute [instance] mono_factorisation.m_mono attribute [instance] mono_factorisation.m_mono namespace mono_factorisation /-- The obvious factorisation of a monomorphism through itself. -/ def self [mono f] : mono_factorisation f := { I := X, m := f, e := πŸ™ X } -- I'm not sure we really need this, but the linter says that an inhabited instance -- ought to exist... instance [mono f] : inhabited (mono_factorisation f) := ⟨self f⟩ variables {f} /-- The morphism `m` in a factorisation `f = e ≫ m` through a monomorphism is uniquely determined. -/ @[ext] lemma ext {F F' : mono_factorisation f} (hI : F.I = F'.I) (hm : F.m = (eq_to_hom hI) ≫ F'.m) : F = F' := begin cases F, cases F', cases hI, simp at hm, dsimp at F_fac' F'_fac', congr, { assumption }, { resetI, apply (cancel_mono F_m).1, rw [F_fac', hm, F'_fac'], } end /-- Any mono factorisation of `f` gives a mono factorisation of `f ≫ g` when `g` is a mono. -/ @[simps] def comp_mono (F : mono_factorisation f) {Y' : C} (g : Y ⟢ Y') [mono g] : mono_factorisation (f ≫ g) := { I := F.I, m := F.m ≫ g, m_mono := mono_comp _ _, e := F.e, } /-- A mono factorisation of `f ≫ g`, where `g` is an isomorphism, gives a mono factorisation of `f`. -/ @[simps] def of_comp_iso {Y' : C} {g : Y ⟢ Y'} [is_iso g] (F : mono_factorisation (f ≫ g)) : mono_factorisation f := { I := F.I, m := F.m ≫ (inv g), m_mono := mono_comp _ _, e := F.e, } /-- Any mono factorisation of `f` gives a mono factorisation of `g ≫ f`. -/ @[simps] def iso_comp (F : mono_factorisation f) {X' : C} (g : X' ⟢ X) : mono_factorisation (g ≫ f) := { I := F.I, m := F.m, e := g ≫ F.e, } /-- A mono factorisation of `g ≫ f`, where `g` is an isomorphism, gives a mono factorisation of `f`. -/ @[simps] def of_iso_comp {X' : C} (g : X' ⟢ X) [is_iso g] (F : mono_factorisation (g ≫ f)) : mono_factorisation f := { I := F.I, m := F.m, e := inv g ≫ F.e, } /-- If `f` and `g` are isomorphic arrows, then a mono factorisation of `f` gives a mono factorisation of `g` -/ @[simps] def of_arrow_iso {f g : arrow C} (F : mono_factorisation f.hom) (sq : f ⟢ g) [is_iso sq] : mono_factorisation g.hom := { I := F.I, m := F.m ≫ sq.right, e := inv sq.left ≫ F.e, m_mono := mono_comp _ _, fac' := by simp only [fac_assoc, arrow.w, is_iso.inv_comp_eq, category.assoc] } end mono_factorisation variable {f} /-- Data exhibiting that a given factorisation through a mono is initial. -/ structure is_image (F : mono_factorisation f) := (lift : Ξ  (F' : mono_factorisation f), F.I ⟢ F'.I) (lift_fac' : Ξ  (F' : mono_factorisation f), lift F' ≫ F'.m = F.m . obviously) restate_axiom is_image.lift_fac' attribute [simp, reassoc] is_image.lift_fac namespace is_image @[simp, reassoc] lemma fac_lift {F : mono_factorisation f} (hF : is_image F) (F' : mono_factorisation f) : F.e ≫ hF.lift F' = F'.e := (cancel_mono F'.m).1 $ by simp variable (f) /-- The trivial factorisation of a monomorphism satisfies the universal property. -/ @[simps] def self [mono f] : is_image (mono_factorisation.self f) := { lift := Ξ» F', F'.e } instance [mono f] : inhabited (is_image (mono_factorisation.self f)) := ⟨self f⟩ variable {f} /-- Two factorisations through monomorphisms satisfying the universal property must factor through isomorphic objects. -/ -- TODO this is another good candidate for a future `unique_up_to_canonical_iso`. @[simps] def iso_ext {F F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') : F.I β‰… F'.I := { hom := hF.lift F', inv := hF'.lift F, hom_inv_id' := (cancel_mono F.m).1 (by simp), inv_hom_id' := (cancel_mono F'.m).1 (by simp) } variables {F F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') lemma iso_ext_hom_m : (iso_ext hF hF').hom ≫ F'.m = F.m := by simp lemma iso_ext_inv_m : (iso_ext hF hF').inv ≫ F.m = F'.m := by simp lemma e_iso_ext_hom : F.e ≫ (iso_ext hF hF').hom = F'.e := by simp lemma e_iso_ext_inv : F'.e ≫ (iso_ext hF hF').inv = F.e := by simp /-- If `f` and `g` are isomorphic arrows, then a mono factorisation of `f` that is an image gives a mono factorisation of `g` that is an image -/ @[simps] def of_arrow_iso {f g : arrow C} {F : mono_factorisation f.hom} (hF : is_image F) (sq : f ⟢ g) [is_iso sq] : is_image (F.of_arrow_iso sq) := { lift := Ξ» F', hF.lift (F'.of_arrow_iso (inv sq)), lift_fac' := Ξ» F', by simpa only [mono_factorisation.of_arrow_iso_m, arrow.inv_right, ← category.assoc, is_iso.comp_inv_eq] using hF.lift_fac (F'.of_arrow_iso (inv sq)) } end is_image variable (f) /-- Data exhibiting that a morphism `f` has an image. -/ structure image_factorisation (f : X ⟢ Y) := (F : mono_factorisation f) (is_image : is_image F) namespace image_factorisation instance [mono f] : inhabited (image_factorisation f) := ⟨⟨_, is_image.self f⟩⟩ /-- If `f` and `g` are isomorphic arrows, then an image factorisation of `f` gives an image factorisation of `g` -/ @[simps] def of_arrow_iso {f g : arrow C} (F : image_factorisation f.hom) (sq : f ⟢ g) [is_iso sq] : image_factorisation g.hom := { F := F.F.of_arrow_iso sq, is_image := F.is_image.of_arrow_iso sq } end image_factorisation /-- `has_image f` means that there exists an image factorisation of `f`. -/ class has_image (f : X ⟢ Y) : Prop := mk' :: (exists_image : nonempty (image_factorisation f)) lemma has_image.mk {f : X ⟢ Y} (F : image_factorisation f) : has_image f := ⟨nonempty.intro F⟩ lemma has_image.of_arrow_iso {f g : arrow C} [h : has_image f.hom] (sq : f ⟢ g) [is_iso sq] : has_image g.hom := ⟨⟨h.exists_image.some.of_arrow_iso sq⟩⟩ section variable [has_image f] /-- The chosen factorisation of `f` through a monomorphism. -/ def image.mono_factorisation : mono_factorisation f := (classical.choice (has_image.exists_image)).F /-- The witness of the universal property for the chosen factorisation of `f` through a monomorphism. -/ def image.is_image : is_image (image.mono_factorisation f) := (classical.choice (has_image.exists_image)).is_image /-- The categorical image of a morphism. -/ def image : C := (image.mono_factorisation f).I /-- The inclusion of the image of a morphism into the target. -/ def image.ΞΉ : image f ⟢ Y := (image.mono_factorisation f).m @[simp] lemma image.as_ΞΉ : (image.mono_factorisation f).m = image.ΞΉ f := rfl instance : mono (image.ΞΉ f) := (image.mono_factorisation f).m_mono /-- The map from the source to the image of a morphism. -/ def factor_thru_image : X ⟢ image f := (image.mono_factorisation f).e /-- Rewrite in terms of the `factor_thru_image` interface. -/ @[simp] lemma as_factor_thru_image : (image.mono_factorisation f).e = factor_thru_image f := rfl @[simp, reassoc] lemma image.fac : factor_thru_image f ≫ image.ΞΉ f = f := (image.mono_factorisation f).fac' variable {f} /-- Any other factorisation of the morphism `f` through a monomorphism receives a map from the image. -/ def image.lift (F' : mono_factorisation f) : image f ⟢ F'.I := (image.is_image f).lift F' @[simp, reassoc] lemma image.lift_fac (F' : mono_factorisation f) : image.lift F' ≫ F'.m = image.ΞΉ f := (image.is_image f).lift_fac' F' @[simp, reassoc] lemma image.fac_lift (F' : mono_factorisation f) : factor_thru_image f ≫ image.lift F' = F'.e := (image.is_image f).fac_lift F' @[simp, reassoc] lemma is_image.lift_ΞΉ {F : mono_factorisation f} (hF : is_image F) : hF.lift (image.mono_factorisation f) ≫ image.ΞΉ f = F.m := hF.lift_fac _ -- TODO we could put a category structure on `mono_factorisation f`, -- with the morphisms being `g : I ⟢ I'` commuting with the `m`s -- (they then automatically commute with the `e`s) -- and show that an `image_of f` gives an initial object there -- (uniqueness of the lift comes for free). instance image.lift_mono (F' : mono_factorisation f) : mono (image.lift F') := by { apply mono_of_mono _ F'.m, simpa using mono_factorisation.m_mono _ } lemma has_image.uniq (F' : mono_factorisation f) (l : image f ⟢ F'.I) (w : l ≫ F'.m = image.ΞΉ f) : l = image.lift F' := (cancel_mono F'.m).1 (by simp [w]) /-- If `has_image g`, then `has_image (f ≫ g)` when `f` is an isomorphism. -/ instance {X Y Z : C} (f : X ⟢ Y) [is_iso f] (g : Y ⟢ Z) [has_image g] : has_image (f ≫ g) := { exists_image := ⟨ { F := { I := image g, m := image.ΞΉ g, e := f ≫ factor_thru_image g, }, is_image := { lift := Ξ» F', image.lift { I := F'.I, m := F'.m, e := inv f ≫ F'.e, }, }, }⟩ } end section variables (C) /-- `has_images` represents a choice of image for every morphism -/ class has_images := (has_image : Ξ  {X Y : C} (f : X ⟢ Y), has_image f) attribute [instance, priority 100] has_images.has_image end section variables (f) [has_image f] /-- The image of a monomorphism is isomorphic to the source. -/ def image_mono_iso_source [mono f] : image f β‰… X := is_image.iso_ext (image.is_image f) (is_image.self f) @[simp, reassoc] lemma image_mono_iso_source_inv_ΞΉ [mono f] : (image_mono_iso_source f).inv ≫ image.ΞΉ f = f := by simp [image_mono_iso_source] @[simp, reassoc] lemma image_mono_iso_source_hom_self [mono f] : (image_mono_iso_source f).hom ≫ f = image.ΞΉ f := begin conv { to_lhs, congr, skip, rw ←image_mono_iso_source_inv_ΞΉ f, }, rw [←category.assoc, iso.hom_inv_id, category.id_comp], end -- This is the proof that `factor_thru_image f` is an epimorphism -- from https://en.wikipedia.org/wiki/Image_%28category_theory%29, which is in turn taken from: -- Mitchell, Barry (1965), Theory of categories, MR 0202787, p.12, Proposition 10.1 @[ext] lemma image.ext {W : C} {g h : image f ⟢ W} [has_limit (parallel_pair g h)] (w : factor_thru_image f ≫ g = factor_thru_image f ≫ h) : g = h := begin let q := equalizer.ΞΉ g h, let e' := equalizer.lift _ w, let F' : mono_factorisation f := { I := equalizer g h, m := q ≫ image.ΞΉ f, m_mono := by apply mono_comp, e := e' }, let v := image.lift F', have tβ‚€ : v ≫ q ≫ image.ΞΉ f = image.ΞΉ f := image.lift_fac F', have t : v ≫ q = πŸ™ (image f) := (cancel_mono_id (image.ΞΉ f)).1 (by { convert tβ‚€ using 1, rw category.assoc }), -- The proof from wikipedia next proves `q ≫ v = πŸ™ _`, -- and concludes that `equalizer g h β‰… image f`, -- but this isn't necessary. calc g = πŸ™ (image f) ≫ g : by rw [category.id_comp] ... = v ≫ q ≫ g : by rw [←t, category.assoc] ... = v ≫ q ≫ h : by rw [equalizer.condition g h] ... = πŸ™ (image f) ≫ h : by rw [←category.assoc, t] ... = h : by rw [category.id_comp] end instance [Ξ  {Z : C} (g h : image f ⟢ Z), has_limit (parallel_pair g h)] : epi (factor_thru_image f) := ⟨λ Z g h w, image.ext f w⟩ lemma epi_image_of_epi {X Y : C} (f : X ⟢ Y) [has_image f] [E : epi f] : epi (image.ΞΉ f) := begin rw ←image.fac f at E, resetI, exact epi_of_epi (factor_thru_image f) (image.ΞΉ f), end lemma epi_of_epi_image {X Y : C} (f : X ⟢ Y) [has_image f] [epi (image.ΞΉ f)] [epi (factor_thru_image f)] : epi f := by { rw [←image.fac f], apply epi_comp, } end section variables {f} {f' : X ⟢ Y} [has_image f] [has_image f'] /-- An equation between morphisms gives a comparison map between the images (which momentarily we prove is an iso). -/ def image.eq_to_hom (h : f = f') : image f ⟢ image f' := image.lift { I := image f', m := image.ΞΉ f', e := factor_thru_image f', }. instance (h : f = f') : is_iso (image.eq_to_hom h) := ⟨⟨image.eq_to_hom h.symm, ⟨(cancel_mono (image.ΞΉ f)).1 (by simp [image.eq_to_hom]), (cancel_mono (image.ΞΉ f')).1 (by simp [image.eq_to_hom])⟩⟩⟩ /-- An equation between morphisms gives an isomorphism between the images. -/ def image.eq_to_iso (h : f = f') : image f β‰… image f' := as_iso (image.eq_to_hom h) /-- As long as the category has equalizers, the image inclusion maps commute with `image.eq_to_iso`. -/ lemma image.eq_fac [has_equalizers C] (h : f = f') : image.ΞΉ f = (image.eq_to_iso h).hom ≫ image.ΞΉ f' := by { ext, simp [image.eq_to_iso, image.eq_to_hom], } end section variables {Z : C} (g : Y ⟢ Z) /-- The comparison map `image (f ≫ g) ⟢ image g`. -/ def image.pre_comp [has_image g] [has_image (f ≫ g)] : image (f ≫ g) ⟢ image g := image.lift { I := image g, m := image.ΞΉ g, e := f ≫ factor_thru_image g } @[simp, reassoc] lemma image.pre_comp_ΞΉ [has_image g] [has_image (f ≫ g)] : image.pre_comp f g ≫ image.ΞΉ g = image.ΞΉ (f ≫ g) := by simp [image.pre_comp] @[simp, reassoc] lemma image.factor_thru_image_pre_comp [has_image g] [has_image (f ≫ g)] : factor_thru_image (f ≫ g) ≫ image.pre_comp f g = f ≫ factor_thru_image g := by simp [image.pre_comp] /-- `image.pre_comp f g` is a monomorphism. -/ instance image.pre_comp_mono [has_image g] [has_image (f ≫ g)] : mono (image.pre_comp f g) := begin apply mono_of_mono _ (image.ΞΉ g), simp only [image.pre_comp_ΞΉ], apply_instance, end /-- The two step comparison map `image (f ≫ (g ≫ h)) ⟢ image (g ≫ h) ⟢ image h` agrees with the one step comparison map `image (f ≫ (g ≫ h)) β‰… image ((f ≫ g) ≫ h) ⟢ image h`. -/ lemma image.pre_comp_comp {W : C} (h : Z ⟢ W) [has_image (g ≫ h)] [has_image (f ≫ g ≫ h)] [has_image h] [has_image ((f ≫ g) ≫ h)] : image.pre_comp f (g ≫ h) ≫ image.pre_comp g h = image.eq_to_hom (category.assoc f g h).symm ≫ (image.pre_comp (f ≫ g) h) := begin apply (cancel_mono (image.ΞΉ h)).1, simp [image.pre_comp, image.eq_to_hom], end variables [has_equalizers C] /-- `image.pre_comp f g` is an epimorphism when `f` is an epimorphism (we need `C` to have equalizers to prove this). -/ instance image.pre_comp_epi_of_epi [has_image g] [has_image (f ≫ g)] [epi f] : epi (image.pre_comp f g) := begin apply epi_of_epi_fac (image.factor_thru_image_pre_comp _ _), exact epi_comp _ _ end instance has_image_iso_comp [is_iso f] [has_image g] : has_image (f ≫ g) := has_image.mk { F := (image.mono_factorisation g).iso_comp f, is_image := { lift := Ξ» F', image.lift (F'.of_iso_comp f) }, } /-- `image.pre_comp f g` is an isomorphism when `f` is an isomorphism (we need `C` to have equalizers to prove this). -/ instance image.is_iso_precomp_iso (f : X ⟢ Y) [is_iso f] [has_image g] : is_iso (image.pre_comp f g) := ⟨⟨image.lift { I := image (f ≫ g), m := image.ΞΉ (f ≫ g), e := inv f ≫ factor_thru_image (f ≫ g) }, ⟨by { ext, simp [image.pre_comp], }, by { ext, simp [image.pre_comp], }⟩⟩⟩ -- Note that in general we don't have the other comparison map you might expect -- `image f ⟢ image (f ≫ g)`. instance has_image_comp_iso [has_image f] [is_iso g] : has_image (f ≫ g) := has_image.mk { F := (image.mono_factorisation f).comp_mono g, is_image := { lift := Ξ» F', image.lift F'.of_comp_iso }, } /-- Postcomposing by an isomorphism induces an isomorphism on the image. -/ def image.comp_iso [has_image f] [is_iso g] : image f β‰… image (f ≫ g) := { hom := image.lift (image.mono_factorisation (f ≫ g)).of_comp_iso, inv := image.lift ((image.mono_factorisation f).comp_mono g) } @[simp, reassoc] lemma image.comp_iso_hom_comp_image_ΞΉ [has_image f] [is_iso g] : (image.comp_iso f g).hom ≫ image.ΞΉ (f ≫ g) = image.ΞΉ f ≫ g := by { ext, simp [image.comp_iso] } @[simp, reassoc] lemma image.comp_iso_inv_comp_image_ΞΉ [has_image f] [is_iso g] : (image.comp_iso f g).inv ≫ image.ΞΉ f = image.ΞΉ (f ≫ g) ≫ inv g := by { ext, simp [image.comp_iso] } end end category_theory.limits namespace category_theory.limits variables {C : Type u} [category.{v} C] section instance {X Y : C} (f : X ⟢ Y) [has_image f] : has_image (arrow.mk f).hom := show has_image f, by apply_instance end section has_image_map /-- An image map is a morphism `image f β†’ image g` fitting into a commutative square and satisfying the obvious commutativity conditions. -/ structure image_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟢ g) := (map : image f.hom ⟢ image g.hom) (map_ΞΉ' : map ≫ image.ΞΉ g.hom = image.ΞΉ f.hom ≫ sq.right . obviously) instance inhabited_image_map {f : arrow C} [has_image f.hom] : inhabited (image_map (πŸ™ f)) := βŸ¨βŸ¨πŸ™ _, by tidy⟩⟩ restate_axiom image_map.map_ΞΉ' attribute [simp, reassoc] image_map.map_ΞΉ @[simp, reassoc] lemma image_map.factor_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟢ g) (m : image_map sq) : factor_thru_image f.hom ≫ m.map = sq.left ≫ factor_thru_image g.hom := (cancel_mono (image.ΞΉ g.hom)).1 $ by simp /-- To give an image map for a commutative square with `f` at the top and `g` at the bottom, it suffices to give a map between any mono factorisation of `f` and any image factorisation of `g`. -/ def image_map.transport {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟢ g) (F : mono_factorisation f.hom) {F' : mono_factorisation g.hom} (hF' : is_image F') {map : F.I ⟢ F'.I} (map_ΞΉ : map ≫ F'.m = F.m ≫ sq.right) : image_map sq := { map := image.lift F ≫ map ≫ hF'.lift (image.mono_factorisation g.hom), map_ΞΉ' := by simp [map_ΞΉ] } /-- `has_image_map sq` means that there is an `image_map` for the square `sq`. -/ class has_image_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟢ g) : Prop := mk' :: (has_image_map : nonempty (image_map sq)) lemma has_image_map.mk {f g : arrow C} [has_image f.hom] [has_image g.hom] {sq : f ⟢ g} (m : image_map sq) : has_image_map sq := ⟨nonempty.intro m⟩ lemma has_image_map.transport {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟢ g) (F : mono_factorisation f.hom) {F' : mono_factorisation g.hom} (hF' : is_image F') (map : F.I ⟢ F'.I) (map_ΞΉ : map ≫ F'.m = F.m ≫ sq.right) : has_image_map sq := has_image_map.mk $ image_map.transport sq F hF' map_ΞΉ /-- Obtain an `image_map` from a `has_image_map` instance. -/ def has_image_map.image_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟢ g) [has_image_map sq] : image_map sq := classical.choice $ @has_image_map.has_image_map _ _ _ _ _ _ sq _ @[priority 100] -- see Note [lower instance priority] instance has_image_map_of_is_iso {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟢ g) [is_iso sq] : has_image_map sq := has_image_map.mk { map := image.lift ((image.mono_factorisation g.hom).of_arrow_iso (inv sq)), map_ΞΉ' := begin erw [← cancel_mono (inv sq).right, category.assoc, ← mono_factorisation.of_arrow_iso_m, image.lift_fac, category.assoc, ← comma.comp_right, is_iso.hom_inv_id, comma.id_right, category.comp_id], end } instance has_image_map.comp {f g h : arrow C} [has_image f.hom] [has_image g.hom] [has_image h.hom] (sq1 : f ⟢ g) (sq2 : g ⟢ h) [has_image_map sq1] [has_image_map sq2] : has_image_map (sq1 ≫ sq2) := has_image_map.mk { map := (has_image_map.image_map sq1).map ≫ (has_image_map.image_map sq2).map, map_ΞΉ' := by simp only [image_map.map_ΞΉ, image_map.map_ΞΉ_assoc, comma.comp_right, category.assoc] } variables {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟢ g) section local attribute [ext] image_map instance : subsingleton (image_map sq) := subsingleton.intro $ Ξ» a b, image_map.ext a b $ (cancel_mono (image.ΞΉ g.hom)).1 $ by simp only [image_map.map_ΞΉ] end variable [has_image_map sq] /-- The map on images induced by a commutative square. -/ abbreviation image.map : image f.hom ⟢ image g.hom := (has_image_map.image_map sq).map lemma image.factor_map : factor_thru_image f.hom ≫ image.map sq = sq.left ≫ factor_thru_image g.hom := by simp lemma image.map_ΞΉ : image.map sq ≫ image.ΞΉ g.hom = image.ΞΉ f.hom ≫ sq.right := by simp lemma image.map_hom_mk'_ΞΉ {X Y P Q : C} {k : X ⟢ Y} [has_image k] {l : P ⟢ Q} [has_image l] {m : X ⟢ P} {n : Y ⟢ Q} (w : m ≫ l = k ≫ n) [has_image_map (arrow.hom_mk' w)] : image.map (arrow.hom_mk' w) ≫ image.ΞΉ l = image.ΞΉ k ≫ n := image.map_ΞΉ _ section variables {h : arrow C} [has_image h.hom] (sq' : g ⟢ h) variables [has_image_map sq'] /-- Image maps for composable commutative squares induce an image map in the composite square. -/ def image_map_comp : image_map (sq ≫ sq') := { map := image.map sq ≫ image.map sq' } @[simp] lemma image.map_comp [has_image_map (sq ≫ sq')] : image.map (sq ≫ sq') = image.map sq ≫ image.map sq' := show (has_image_map.image_map (sq ≫ sq')).map = (image_map_comp sq sq').map, by congr end section variables (f) /-- The identity `image f ⟢ image f` fits into the commutative square represented by the identity morphism `πŸ™ f` in the arrow category. -/ def image_map_id : image_map (πŸ™ f) := { map := πŸ™ (image f.hom) } @[simp] lemma image.map_id [has_image_map (πŸ™ f)] : image.map (πŸ™ f) = πŸ™ (image f.hom) := show (has_image_map.image_map (πŸ™ f)).map = (image_map_id f).map, by congr end end has_image_map section variables (C) [has_images C] /-- If a category `has_image_maps`, then all commutative squares induce morphisms on images. -/ class has_image_maps := (has_image_map : Ξ  {f g : arrow C} (st : f ⟢ g), has_image_map st) attribute [instance, priority 100] has_image_maps.has_image_map end section has_image_maps variables [has_images C] [has_image_maps C] /-- The functor from the arrow category of `C` to `C` itself that maps a morphism to its image and a commutative square to the induced morphism on images. -/ @[simps] def im : arrow C β₯€ C := { obj := Ξ» f, image f.hom, map := Ξ» _ _ st, image.map st } end has_image_maps section strong_epi_mono_factorisation /-- A strong epi-mono factorisation is a decomposition `f = e ≫ m` with `e` a strong epimorphism and `m` a monomorphism. -/ structure strong_epi_mono_factorisation {X Y : C} (f : X ⟢ Y) extends mono_factorisation f := [e_strong_epi : strong_epi e] attribute [instance] strong_epi_mono_factorisation.e_strong_epi /-- Satisfying the inhabited linter -/ instance strong_epi_mono_factorisation_inhabited {X Y : C} (f : X ⟢ Y) [strong_epi f] : inhabited (strong_epi_mono_factorisation f) := ⟨⟨⟨Y, πŸ™ Y, f, by simp⟩⟩⟩ /-- A mono factorisation coming from a strong epi-mono factorisation always has the universal property of the image. -/ def strong_epi_mono_factorisation.to_mono_is_image {X Y : C} {f : X ⟢ Y} (F : strong_epi_mono_factorisation f) : is_image F.to_mono_factorisation := { lift := Ξ» G, arrow.lift $ arrow.hom_mk' $ show G.e ≫ G.m = F.e ≫ F.m, by rw [F.to_mono_factorisation.fac, G.fac] } variable (C) /-- A category has strong epi-mono factorisations if every morphism admits a strong epi-mono factorisation. -/ class has_strong_epi_mono_factorisations : Prop := mk' :: (has_fac : Ξ  {X Y : C} (f : X ⟢ Y), nonempty (strong_epi_mono_factorisation f)) variable {C} lemma has_strong_epi_mono_factorisations.mk (d : Ξ  {X Y : C} (f : X ⟢ Y), strong_epi_mono_factorisation f) : has_strong_epi_mono_factorisations C := ⟨λ X Y f, nonempty.intro $ d f⟩ @[priority 100] instance has_images_of_has_strong_epi_mono_factorisations [has_strong_epi_mono_factorisations C] : has_images C := { has_image := Ξ» X Y f, let F' := classical.choice (has_strong_epi_mono_factorisations.has_fac f) in has_image.mk { F := F'.to_mono_factorisation, is_image := F'.to_mono_is_image } } end strong_epi_mono_factorisation section has_strong_epi_images variables (C) [has_images C] /-- A category has strong epi images if it has all images and `factor_thru_image f` is a strong epimorphism for all `f`. -/ class has_strong_epi_images : Prop := (strong_factor_thru_image : Ξ  {X Y : C} (f : X ⟢ Y), strong_epi (factor_thru_image f)) attribute [instance] has_strong_epi_images.strong_factor_thru_image end has_strong_epi_images section has_strong_epi_images /-- If there is a single strong epi-mono factorisation of `f`, then every image factorisation is a strong epi-mono factorisation. -/ lemma strong_epi_of_strong_epi_mono_factorisation {X Y : C} {f : X ⟢ Y} (F : strong_epi_mono_factorisation f) {F' : mono_factorisation f} (hF' : is_image F') : strong_epi F'.e := by { rw ←is_image.e_iso_ext_hom F.to_mono_is_image hF', apply strong_epi_comp } lemma strong_epi_factor_thru_image_of_strong_epi_mono_factorisation {X Y : C} {f : X ⟢ Y} [has_image f] (F : strong_epi_mono_factorisation f) : strong_epi (factor_thru_image f) := strong_epi_of_strong_epi_mono_factorisation F $ image.is_image f /-- If we constructed our images from strong epi-mono factorisations, then these images are strong epi images. -/ @[priority 100] instance has_strong_epi_images_of_has_strong_epi_mono_factorisations [has_strong_epi_mono_factorisations C] : has_strong_epi_images C := { strong_factor_thru_image := Ξ» X Y f, strong_epi_factor_thru_image_of_strong_epi_mono_factorisation $ classical.choice $ has_strong_epi_mono_factorisations.has_fac f } end has_strong_epi_images section has_strong_epi_images variables [has_images C] /-- A category with strong epi images has image maps. -/ @[priority 100] instance has_image_maps_of_has_strong_epi_images [has_strong_epi_images C] : has_image_maps C := { has_image_map := Ξ» f g st, has_image_map.mk { map := arrow.lift $ arrow.hom_mk' $ show (st.left ≫ factor_thru_image g.hom) ≫ image.ΞΉ g.hom = factor_thru_image f.hom ≫ (image.ΞΉ f.hom ≫ st.right), by simp } } /-- If a category has images, equalizers and pullbacks, then images are automatically strong epi images. -/ @[priority 100] instance has_strong_epi_images_of_has_pullbacks_of_has_equalizers [has_pullbacks C] [has_equalizers C] : has_strong_epi_images C := { strong_factor_thru_image := Ξ» X Y f, { epi := by apply_instance, has_lift := Ξ» A B x y h h_mono w, arrow.has_lift.mk { lift := image.lift { I := pullback h y, m := pullback.snd ≫ image.ΞΉ f, m_mono := by exactI mono_comp _ _, e := pullback.lift _ _ w } ≫ pullback.fst } } } end has_strong_epi_images variables [has_strong_epi_mono_factorisations.{v} C] variables {X Y : C} {f : X ⟢ Y} /-- If `C` has strong epi mono factorisations, then the image is unique up to isomorphism, in that if `f` factors as a strong epi followed by a mono, this factorisation is essentially the image factorisation. -/ def image.iso_strong_epi_mono {I' : C} (e : X ⟢ I') (m : I' ⟢ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : I' β‰… image f := is_image.iso_ext {strong_epi_mono_factorisation . I := I', m := m, e := e}.to_mono_is_image $ image.is_image f @[simp] lemma image.iso_strong_epi_mono_hom_comp_ΞΉ {I' : C} (e : X ⟢ I') (m : I' ⟢ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : (image.iso_strong_epi_mono e m comm).hom ≫ image.ΞΉ f = m := is_image.lift_fac _ _ @[simp] lemma image.iso_strong_epi_mono_inv_comp_mono {I' : C} (e : X ⟢ I') (m : I' ⟢ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : (image.iso_strong_epi_mono e m comm).inv ≫ m = image.ΞΉ f := image.lift_fac _ end category_theory.limits
ef0191bea7623a379c3e5d73f1890c01848c4645
37336e9dcf558f325cf43bdf45bab5d8e455a3b4
/M1F/2017-18/Example_Sheet_01/Question_01/solution.lean
566bc3cfb92b5b5fe0e11ff063cc247461427b3e
[]
no_license
rafaelbailo/xena
bfeab41b3b401c812689a4879d58f2ad046240b5
fad865c3d1bac6279f9a77e8aa6ba7e0cd572e06
refs/heads/master
1,626,002,809,060
1,507,793,086,000
1,507,793,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,671
lean
/- M1F 2017-18 Sheet 1 Question 1, solution Author : Kevin Buzzard Note: this version of the solution uses integers instead of real numbers. This is because the real numbers are not currently in Lean's standard library and I want to make things as easy as possible at this point. This file should work with any version of lean -- whether you installed it yourself or are running the version on https://leanprover.github.io/live/latest/ Lean does have real numbers -- it's just that you need to install another library to get them (the mathlib library). Notes for KMB : If I switch to real numbers, I should also switch to x^2 -/ -- Here are some solutions to M1F sheet 1 question 1. -- Let me stress that they are basically completely -- unreadable unless you use Lean to read them. -- If you use Lean, then by moving the cursor around -- the proofs, or clicking on them, you will be able -- to see, at each stage, what Xena -- knows and what she is trying to prove. -- This will give you a much better feeling for what -- is going on. -- Before we start, just let me define the power notation, -- which is not in the core lean library (it is in another -- library called mathlib which may not be installed on your machine). -- Don't worry about this bit. I'm defining x^n by recursion on n, which -- is sort of like induction but with definitions instead of proofs. @[reducible] definition pow : int -> nat -> int | _ 0 := (1:int) | x (nat.succ n) := x * (pow x n) notation x `^` n := pow x n -- Now onto the question. -- M1F Sheet 1 Q1 part (a): this is false. theorem m1f_sheet01_q01a_is_F : Β¬ (βˆ€ x : β„€, x^2 - 3*x + 2 = 0 β†’ x=1) := begin -- -- We're going to prove this by contradiction. -- Let's let H1 be our false hypothesis, -- so H1 is "for all x, x^2-3x+2=0 implies x=1". -- By the way, if you're using lean to read this file, you -- can hover over or click on various lines to see much more -- clearly what is going on. -- intro H1, -- -- Now the problems will occur if we let x be 2, -- because x^2-3*2+2=0 but 2 isn't 1. So let's -- see what we can deduce from H1 if we set x=2. -- have H2 : (2:β„€)^2-3*2+2=0 β†’ (2:β„€)=1, -- new hypothesis exact (H1 2), -- and there's its proof. -- -- Hypothesis H2 now says that smething which is true, -- implies something which is false. So that's going to -- be our contradiction. But Xena really -- needs to be talked through this. Let's first -- check that 2^2-3*2+2=0! Let's call this very -- easy hypothesis H3. -- have H3 : (2:β„€)^2-3*2+2=0, trivial, -- Xena can work this proof out herself. -- -- From hypotheses H2 and H3 we can deduce that 2=1! have H4 : (2:β„€) = 1, exact (H2 H3), -- H2 applied to H3 tells us that 2=1. But Xena is smart -- enough to know that 2 isn't 1. have H5 : (2:β„€) β‰  1, cc, -- "cc" is a proof tactic that Xena knows. -- Amongst other things, it solves basic inequalities like this. -- Xena currently knows a few of these tactics, -- and she learns more all the time, as new ones are -- implemented into her brain. It stands for -- "congruence closure", not that this is much help. -- -- Anyway, the point is that H4 and H5 now contradict -- each other. So we can finish the proof! contradiction end -- End M1F Sheet 1 Q1 part (a) -- M1F Sheet 1 Q1 part (b) is true. theorem m1f_sheet01_q01b_is_T : βˆ€ x : β„€, x=1 β†’ x^2-3*x+2=0 := -- This one is much easier. -- First we assume the hypothesis that x=1. begin intros x H1, -- now H1 says that x=1. -- Our goal is to prove that x^2-3*x+2=0. -- -- First let's simplify our goal using the -- hypothesis H1. simp [H1], -- our goal is now to prove 1^2-3+2=0, but Xena can do this. trivial end -- End M1F Sheet 1 Q1 part (b) -- M1F Sheet 1 Q1 part (c) is false and this follows from part (a) theorem m1f_sheet01_q01c_is_F : Β¬ (βˆ€ x : β„€, x^2 - 3*x + 2 = 0 ↔ x=1) := begin -- We prove it by contradiction. intro H, -- H is now the hypothesis that for all x, x^2-3x+2=0 iff x=1 -- We could just now copy out the proof of part a now -- but we could also just use part a. -- We first deduce from H that for all x, x^2-3x+2=0 implies x=1. have H2: βˆ€ x : β„€, (x^2-3*x+2=0 β†’ x=1), exact Ξ» x, iff.elim_left (H x), -- and now we note that this and part a give us our contradiction apply m1f_sheet01_q01a_is_F, assumption end -- End M1F Sheet 1 Q1 part (c) -- M1F Sheet 1 Q1 part (d) is true but the proof is really long. theorem m1f_sheet01_q01d_is_T : βˆ€ x : β„€, x^2 - 3*x + 2 = 0 ↔ (x=1 ∨ x=2) := begin -- Now this one is going to be some work, -- because we need to prove that the *only* roots of -- the quadratic are x=1 and x=2, but Xena doesn't know -- anything about solving quadratic equations. -- -- First let's get x. intro x, -- Now we have to prove an iff statement, so let's -- break it up into an if and an only if. apply iff.intro, -- now we have two things to prove so I'll indent. -- Let's do the easy one first. (that weird v sign means "or") show x=1 ∨ x=2 β†’ x^2-3*x+2=0, intro H12, -- hypothesis H12 says "x=1 or x=2" apply or.elim H12, -- eliminate the or, intro H1, -- H1 is the hypothesis that x=1. simp [H1], -- now sub in to x^2-3x+2=0, trivial, -- and it's trivial. -- that was x=1, and x=2 is just as easy. intro H2, simp [H2], trivial, -- now we have to do the trickier case. -- -- We have to prove x^2-3x+2=0 implies x=1 or x=2. intro H, -- this is the hypothesis x^2-3x+2=0. -- We will now show that this hypothesis H implies x=1 or x=2 -- by slowly introducing new hypotheses H2,H3,H4 of H, with the "have" command, -- and then proving each one, -- until finally we have proved x=1 or x=2. -- First Let's factor. have H2:x^2-3*x+2=(x-1)*(x-2), -- Aargh! Lean stuggles to do this! Excuse us for a second. unfold pow, change (3:β„€) with 2+1;simp [mul_add,mul_one,add_left_cancel_iff], -- Done. have H3: (x-1)*(x-2)=0, -- This follows from H1 and H2 via a calculation. exact calc (x-1)*(x-2) = x^2-3*x+2 : eq.symm H2 ... = 0 : H , -- From H3 we can deduce x-1=0 or x-2=0. This is an inbuilt -- fact in Lean -- if a*b=0 then either a=0 or b=0. So we apply this fact. have H4: (x-1)=0 ∨ (x-2)=0, apply iff.elim_left mul_eq_zero_iff_eq_zero_or_eq_zero, assumption, -- Now all we have to do is to prove that if x-1=0 then x=1, and if x-2=0 then x=2 cases H4 with HX1 HX2, -- x-1=0 left, -- prove the left part of "x=1 or x=2" apply iff.elim_left sub_eq_zero_iff_eq HX1, -- x-2=0 right, apply iff.elim_left sub_eq_zero_iff_eq HX2, -- QED! end -- End M1F Sheet 1 Q1 part (d) -- M1F Sheet 1 Q1 part (e) is true, and follows easily from (d) theorem m1f_sheet01_q01e_is_T : βˆ€ x : β„€, x^2 - 3*x + 2 = 0 β†’ (x=1 ∨ x=2 ∨ x=3) := begin intro x, intro Hx, have H12 : x=1 ∨ x=2, apply (iff.elim_left (m1f_sheet01_q01d_is_T x) Hx), -- H12 is "x=1 or x=2", and goal is now "x=1 or x=2 or x=3", so looking good :-) -- To understand the next lines better it might be good to think of -- it as x=1 or (x=2 or x=3) cases H12, -- next line is x=1, one after is x=2 left,assumption, right,left,assumption, end -- End M1F Sheet 1 Q1 part (e) -- M1F Sheet 1 Q1 part (f) is false theorem m1f_sheet01_q01f_is_F : Β¬ (βˆ€ x : β„€, (x=1 ∨ x=2 ∨ x=3) β†’ x^2 - 3*x + 2 = 0) := begin intro H, specialize H 3, have H2 : (3:int)=1 ∨ (3:int)=2 ∨ (3:int)=3, right,right,trivial, let s:=(3:int)^2-3*3+2, have H3: s=0, exact H H2, -- But it's easy to check s isn't 0: have H4: s β‰  0, exact dec_trivial, -- H3 and H4 are a contradiction. contradiction, end -- End M1F Sheet 1 Q1 part (f)
66338c133e17cc70a6b10bf37f81b15237b8c163
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/liouville/measure.lean
1211eae475035746bb8cba97355b477a4e9c09f1
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
6,205
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import measure_theory.measure.lebesgue import number_theory.liouville.residual import number_theory.liouville.liouville_with import analysis.p_series /-! # Volume of the set of Liouville numbers In this file we prove that the set of Liouville numbers with exponent (irrationality measure) strictly greater than two is a set of Lebesuge measure zero, see `volume_Union_set_of_liouville_with`. Since this set is a residual set, we show that the filters `residual` and `volume.ae` are disjoint. These filters correspond to two common notions of genericity on `ℝ`: residual sets and sets of full measure. The fact that the filters are disjoint means that two mutually exclusive properties can be β€œgeneric” at the same time (in the sense of different β€œgenericity” filters). ## Tags Liouville number, Lebesgue measure, residual, generic property -/ open_locale filter big_operators ennreal topological_space nnreal open filter set metric measure_theory real lemma set_of_liouville_with_subset_aux : {x : ℝ | βˆƒ p > 2, liouville_with p x} βŠ† ⋃ m : β„€, (Ξ» x : ℝ, x + m) ⁻¹' (⋃ n > (0 : β„•), {x : ℝ | βˆƒαΆ  b : β„• in at_top, βˆƒ a ∈ finset.Icc (0 : β„€) b, |x - (a : β„€) / b| < 1 / b ^ (2 + 1 / n : ℝ)}) := begin rintro x ⟨p, hp, hxp⟩, rcases exists_nat_one_div_lt (sub_pos.2 hp) with ⟨n, hn⟩, rw lt_sub_iff_add_lt' at hn, suffices : βˆ€ y : ℝ, liouville_with p y β†’ y ∈ Ico (0 : ℝ) 1 β†’ βˆƒαΆ  b : β„• in at_top, βˆƒ a ∈ finset.Icc (0 : β„€) b, |y - a / b| < 1 / b ^ (2 + 1 / (n + 1) : ℝ), { simp only [mem_Union, mem_preimage], have hx : x + ↑(-⌊xβŒ‹) ∈ Ico (0 : ℝ) 1, { simp only [int.floor_le, int.lt_floor_add_one, add_neg_lt_iff_le_add', zero_add, and_self, mem_Ico, int.cast_neg, le_add_neg_iff_add_le] }, refine ⟨-⌊xβŒ‹, n + 1, n.succ_pos, this _ (hxp.add_int _) hx⟩ }, clear hxp x, intros x hxp hx01, refine ((hxp.frequently_lt_rpow_neg hn).and_eventually (eventually_ge_at_top 1)).mono _, rintro b ⟨⟨a, hne, hlt⟩, hb⟩, rw [rpow_neg b.cast_nonneg, ← one_div] at hlt, refine ⟨a, _, hlt⟩, replace hb : (1 : ℝ) ≀ b, from nat.one_le_cast.2 hb, have hb0 : (0 : ℝ) < b := zero_lt_one.trans_le hb, replace hlt : |x - a / b| < 1 / b, { refine hlt.trans_le (one_div_le_one_div_of_le hb0 _), calc (b : ℝ) = b ^ (1 : ℝ) : (rpow_one _).symm ... ≀ b ^ (2 + 1 / (n + 1) : ℝ) : rpow_le_rpow_of_exponent_le hb (one_le_two.trans _), simpa using n.cast_add_one_pos.le }, rw [sub_div' _ _ _ hb0.ne', abs_div, abs_of_pos hb0, div_lt_div_right hb0, abs_sub_lt_iff, sub_lt_iff_lt_add, sub_lt_iff_lt_add, ← sub_lt_iff_lt_add'] at hlt, rw [finset.mem_Icc, ← int.lt_add_one_iff, ← int.lt_add_one_iff, ← neg_lt_iff_pos_add, add_comm, ← @int.cast_lt ℝ, ← @int.cast_lt ℝ], push_cast, refine ⟨lt_of_le_of_lt _ hlt.1, hlt.2.trans_le _⟩, { simp only [mul_nonneg hx01.left b.cast_nonneg, neg_le_sub_iff_le_add, le_add_iff_nonneg_left] }, { rw [add_le_add_iff_left], calc x * b ≀ 1 * b : mul_le_mul_of_nonneg_right hx01.2.le hb0.le ... = b : one_mul b } end /-- The set of numbers satisfying the Liouville condition with some exponent `p > 2` has Lebesgue measure zero. -/ @[simp] lemma volume_Union_set_of_liouville_with : volume (⋃ (p : ℝ) (hp : 2 < p), {x : ℝ | liouville_with p x}) = 0 := begin simp only [← set_of_exists], refine measure_mono_null set_of_liouville_with_subset_aux _, rw measure_Union_null_iff, intro m, rw measure_preimage_add_right, clear m, refine (measure_bUnion_null_iff $ countable_encodable _).2 (Ξ» n (hn : 1 ≀ n), _), generalize hr : (2 + 1 / n : ℝ) = r, replace hr : 2 < r, by simp [← hr, zero_lt_one.trans_le hn], clear hn n, refine measure_set_of_frequently_eq_zero _, simp only [set_of_exists, ← real.dist_eq, ← mem_ball, set_of_mem_eq], set B : β„€ β†’ β„• β†’ set ℝ := Ξ» a b, ball (a / b) (1 / b ^ r), have hB : βˆ€ a b, volume (B a b) = ↑(2 / b ^ r : ℝβ‰₯0), { intros a b, simp only [B, real.volume_ball], rw [ennreal.of_real, mul_one_div, to_nnreal_div zero_le_two, to_nnreal_bit0 zero_le_one, to_nnreal_one, to_nnreal_rpow_of_nonneg (nat.cast_nonneg _), nnreal.to_nnreal_coe_nat] }, have : βˆ€ b : β„•, volume (⋃ a ∈ finset.Icc (0 : β„€) b, B a b) ≀ (2 * (b ^ (1 - r) + b ^ (-r)) : ℝβ‰₯0), { intro b, calc volume (⋃ a ∈ finset.Icc (0 : β„€) b, B a b) ≀ βˆ‘ a in finset.Icc (0 : β„€) b, volume (B a b) : measure_bUnion_finset_le _ _ ... = ((b + 1) * (2 / b ^ r) : ℝβ‰₯0) : by simp only [hB, int.card_Icc, finset.sum_const, nsmul_eq_mul, sub_zero, ← int.coe_nat_succ, int.to_nat_coe_nat, ← nat.cast_succ, ennreal.coe_mul, ennreal.coe_nat] ... = _ : _, have : 1 - r β‰  0, by linarith, rw [ennreal.coe_eq_coe], simp [add_mul, div_eq_mul_inv, nnreal.rpow_neg, nnreal.rpow_sub' _ this, mul_add, mul_left_comm] }, refine ne_top_of_le_ne_top (ennreal.tsum_coe_ne_top_iff_summable.2 _) (ennreal.tsum_le_tsum this), refine (summable.add _ _).mul_left _; simp only [nnreal.summable_rpow]; linarith end lemma ae_not_liouville_with : βˆ€α΅ x, βˆ€ p > (2 : ℝ), Β¬liouville_with p x := by simpa only [ae_iff, not_forall, not_not, set_of_exists] using volume_Union_set_of_liouville_with lemma ae_not_liouville : βˆ€α΅ x, Β¬liouville x := ae_not_liouville_with.mono $ Ξ» x h₁ hβ‚‚, h₁ 3 (by norm_num) (hβ‚‚.liouville_with 3) /-- The set of Liouville numbers has Lebesgue measure zero. -/ @[simp] lemma volume_set_of_liouville : volume {x : ℝ | liouville x} = 0 := by simpa only [ae_iff, not_not] using ae_not_liouville /-- The filters `residual ℝ` and `volume.ae` are disjoint. This means that there exists a residual set of Lebesgue measure zero (e.g., the set of Liouville numbers). -/ lemma real.disjoint_residual_ae : disjoint (residual ℝ) volume.ae := disjoint_of_disjoint_of_mem disjoint_compl_right eventually_residual_liouville ae_not_liouville
d4c5b6593bab30798fb0c62eb7487876ab6df618
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/notation3.lean
6e82b417c9df80965f2b172cbeb549cc276ab92e
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
314
lean
import data.prod data.num inductive list (T : Type) : Type := nil {} : list T, cons : T β†’ list T β†’ list T open list notation h :: t := cons h t notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l open prod num constants a b : num check [a, b, b] check (a, true, a = b, b) check (a, b) check [1, 2+2, 3]
287f3502852f5e6f560fb20e3e01c18048908919
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/stage0/src/Leanpkg/Manifest.lean
a941e92b02bc688bb046f5a8fd1480126e3a7e29
[ "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
2,781
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Sebastian Ullrich -/ import Leanpkg.Toml import Leanpkg.LeanVersion namespace Leanpkg inductive Source where | path (dirName : String) : Source | git (url rev : String) (branch : Option String) : Source namespace Source def fromToml (v : Toml.Value) : Option Source := (do let Toml.Value.str dirName ← v.lookup "path" | none path dirName) <|> (do let Toml.Value.str url ← v.lookup "git" | none let Toml.Value.str rev ← v.lookup "rev" | none match v.lookup "branch" with | none => git url rev none | some (Toml.Value.str branch) => git url rev (some branch) | _ => none) def toToml : Source β†’ Toml.Value | path dirName => Toml.Value.table [("path", Toml.Value.str dirName)] | git url rev none => Toml.Value.table [("git", Toml.Value.str url), ("rev", Toml.Value.str rev)] | git url rev (some branch) => Toml.Value.table [("git", Toml.Value.str url), ("branch", Toml.Value.str branch), ("rev", Toml.Value.str rev)] end Source structure Dependency where name : String src : Source structure Manifest where name : String version : String leanVersion : String := leanVersionString timeout : Option Nat := none path : Option String := none dependencies : List Dependency := [] namespace Manifest def effectivePath (m : Manifest) : String := m.path.getD "." def fromToml (t : Toml.Value) : Option Manifest := OptionM.run do let pkg ← t.lookup "package" let Toml.Value.str n ← pkg.lookup "name" | none let Toml.Value.str ver ← pkg.lookup "version" | none let leanVer ← match pkg.lookup "lean_version" with | some (Toml.Value.str leanVer) => some leanVer | none => some leanVersionString | _ => none let tm ← match pkg.lookup "timeout" with | some (Toml.Value.nat timeout) => some (some timeout) | none => some none | _ => none let path ← match pkg.lookup "path" with | some (Toml.Value.str path) => some (some path) | none => some none | _ => none let Toml.Value.table deps ← t.lookup "dependencies" <|> some (Toml.Value.table []) | none let deps ← deps.mapM fun ⟨n, src⟩ => do Dependency.mk n (← Source.fromToml src) return { name := n, version := ver, leanVersion := leanVer, path := path, dependencies := deps, timeout := tm } def fromFile (fn : String) : IO Manifest := do let cnts ← IO.FS.readFile fn let toml ← Toml.parse cnts let some manifest ← pure (fromToml toml) | throw <| IO.userError s!"cannot read manifest from {fn}" manifest end Manifest def leanpkgTomlFn := "leanpkg.toml" end Leanpkg
1fc8531142ee3c9c26371feda6e6ce4e6d99c9ce
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/clear_tac.lean
e54e8f36ad663f002240080f9a5dea7855f50c62
[ "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
289
lean
import logic example {a b c : Prop} : a β†’ b β†’ c β†’ a ∧ b := begin intros [Ha, Hb, Hc], clear Hc, clear c, apply (and.intro Ha Hb), end example {a b c : Prop} : a β†’ b β†’ c β†’ c ∧ b := begin intros [Ha, Hb, Hc], clear Ha, clear a, apply (and.intro Hc Hb), end
630745a5eb12c9b0b486d2f48fd235e6d3ef71da
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/algebra/quadratic_discriminant.lean
b5fb40f62b91e8bdda4404e07b3f7f5f53e50ac3
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
6,793
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import tactic.linarith /-! # Quadratic discriminants and roots of a quadratic This file defines the discriminant of a quadratic and gives the solution to a quadratic equation. ## Main definition The discriminant of a quadratic `a*x*x + b*x + c` is `b*b - 4*a*c`. ## Main statements β€’ Roots of a quadratic can be written as `(-b + s) / (2 * a)` or `(-b - s) / (2 * a)`, where `s` is the square root of the discriminant. β€’ If the discriminant has no square root, then the corresponding quadratic has no root. β€’ If a quadratic is always non-negative, then its discriminant is non-positive. ## Tags polynomial, quadratic, discriminant, root -/ variables {Ξ± : Type*} section lemmas variables [linear_ordered_field Ξ±] {a b c : Ξ±} lemma exists_le_mul_self : βˆ€ a : Ξ±, βˆƒ x : Ξ±, a ≀ x * x := begin classical, -- TODO: otherwise linarith performance sucks assume a, cases le_total 1 a with ha ha, { use a, exact le_mul_of_one_le_left (by linarith) ha }, { use 1, linarith } end lemma exists_lt_mul_self : βˆ€ a : Ξ±, βˆƒ x : Ξ±, a < x * x := begin classical, -- todo: otherwise linarith performance sucks assume a, rcases (exists_le_mul_self a) with ⟨x, hx⟩, cases le_total 0 x with hx' hx', { use (x + 1), have : (x+1)*(x+1) = x*x + 2*x + 1, {ring}, exact lt_of_le_of_lt hx (by rw this; linarith) }, { use (x - 1), have : (x-1)*(x-1) = x*x - 2*x + 1, {ring}, exact lt_of_le_of_lt hx (by rw this; linarith) } end end lemmas variables [linear_ordered_field Ξ±] {a b c x : Ξ±} /-- Discriminant of a quadratic -/ def discrim [ring Ξ±] (a b c : Ξ±) : Ξ± := b^2 - 4 * a * c /-- A quadratic has roots if and only if its discriminant equals some square. -/ lemma quadratic_eq_zero_iff_discrim_eq_square (ha : a β‰  0) : βˆ€ x : Ξ±, a * x * x + b * x + c = 0 ↔ discrim a b c = (2 * a * x + b)^2 := by classical; exact -- TODO: otherwise linarith performance sucks assume x, iff.intro (assume h, calc discrim a b c = 4*a*(a*x*x + b*x + c) + b*b - 4*a*c : by rw [h, discrim]; ring ... = (2*a*x + b)^2 : by ring) (assume h, have ha : 2*2*a β‰  0 := mul_ne_zero (mul_ne_zero two_ne_zero two_ne_zero) ha, mul_left_cancel' ha $ calc 2 * 2 * a * (a * x * x + b * x + c) = (2*a*x + b)^2 - (b^2 - 4*a*c) : by ring ... = 0 : by { rw [← h, discrim], ring } ... = 2*2*a*0 : by ring) /-- Roots of a quadratic -/ lemma quadratic_eq_zero_iff (ha : a β‰  0) {s : Ξ±} (h : discrim a b c = s * s) : βˆ€ x : Ξ±, a * x * x + b * x + c = 0 ↔ x = (-b + s) / (2 * a) ∨ x = (-b - s) / (2 * a) := assume x, begin classical, -- TODO: otherwise linarith performance sucks rw [quadratic_eq_zero_iff_discrim_eq_square ha, h, pow_two, mul_self_eq_mul_self_iff], have ne : 2 * a β‰  0 := mul_ne_zero two_ne_zero ha, have : x = 2 * a * x / (2 * a) := (mul_div_cancel_left x ne).symm, have h₁ : 2 * a * ((-b + s) / (2 * a)) = -b + s := mul_div_cancel' _ ne, have hβ‚‚ : 2 * a * ((-b - s) / (2 * a)) = -b - s := mul_div_cancel' _ ne, split, { intro h', rcases h', { left, rw h', simpa [add_comm] }, { right, rw h', simpa [add_comm, sub_eq_add_neg] } }, { intro h', rcases h', { left, rw [h', h₁], ring }, { right, rw [h', hβ‚‚], ring } } end /-- A quadratic has roots if its discriminant has square roots -/ lemma exist_quadratic_eq_zero (ha : a β‰  0) (h : βˆƒ s, discrim a b c = s * s) : βˆƒ x, a * x * x + b * x + c = 0 := begin rcases h with ⟨s, hs⟩, use (-b + s) / (2 * a), rw quadratic_eq_zero_iff ha hs, simp end /-- Root of a quadratic when its discriminant equals zero -/ lemma quadratic_eq_zero_iff_of_discrim_eq_zero (ha : a β‰  0) (h : discrim a b c = 0) : βˆ€ x : Ξ±, a * x * x + b * x + c = 0 ↔ x = -b / (2 * a) := assume x, begin classical, -- TODO: otherwise linarith performance sucks have : discrim a b c = 0 * 0 := eq.trans h (by ring), rw quadratic_eq_zero_iff ha this, simp end /-- A quadratic has no root if its discriminant has no square root. -/ lemma quadratic_ne_zero_of_discrim_ne_square (ha : a β‰  0) (h : βˆ€ s : Ξ±, discrim a b c β‰  s * s) : βˆ€ (x : Ξ±), a * x * x + b * x + c β‰  0 := begin assume x h', rw [quadratic_eq_zero_iff_discrim_eq_square ha, pow_two] at h', have := h _, contradiction end /-- If a polynomial of degree 2 is always nonnegative, then its discriminant is nonpositive -/ lemma discriminant_le_zero {a b c : Ξ±} (h : βˆ€ x : Ξ±, 0 ≀ a*x*x + b*x + c) : discrim a b c ≀ 0 := by classical; exact -- TODO: otherwise linarith performance sucks have hc : 0 ≀ c, by { have := h 0, linarith }, begin rw [discrim, pow_two], cases lt_trichotomy a 0 with ha ha, -- if a < 0 by_cases hb : b = 0, { rw hb at *, rcases exists_lt_mul_self (-c/a) with ⟨x, hx⟩, have := mul_lt_mul_of_neg_left hx ha, rw [mul_div_cancel' _ (ne_of_lt ha), ← mul_assoc] at this, have hβ‚‚ := h x, linarith }, { by_cases hc' : c = 0, { rw hc' at *, have : -(a*-b*-b + b*-b + 0) = (1-a)*(b*b), {ring}, have h := h (-b), rw [← neg_nonpos, this] at h, have : b * b ≀ 0 := nonpos_of_mul_nonpos_left h (by linarith), linarith }, { have h := h (-c/b), have : a*(-c/b)*(-c/b) + b*(-c/b) + c = a*((c/b)*(c/b)), { rw mul_div_cancel' _ hb, ring }, rw this at h, have : 0 ≀ a := nonneg_of_mul_nonneg_right h (mul_self_pos $ div_ne_zero hc' hb), linarith [ha] } }, cases ha with ha ha, -- if a = 0 by_cases hb : b = 0, { rw [ha, hb], linarith }, { have := h ((-c-1)/b), rw [ha, mul_div_cancel' _ hb] at this, linarith }, -- if a > 0 have := calc 4*a* (a*(-(b/a)*(1/2))*(-(b/a)*(1/2)) + b*(-(b/a)*(1/2)) + c) = (a*(b/a)) * (a*(b/a)) - 2*(a*(b/a))*b + 4*a*c : by ring ... = -(b*b - 4*a*c) : by { simp only [mul_div_cancel' b (ne_of_gt ha)], ring }, have ha' : 0 ≀ 4*a, {linarith}, have h := (mul_nonneg ha' (h (-(b/a) * (1/2)))), rw this at h, rwa ← neg_nonneg end /-- If a polynomial of degree 2 is always positive, then its discriminant is negative, at least when the coefficient of the quadratic term is nonzero. -/ lemma discriminant_lt_zero {a b c : Ξ±} (ha : a β‰  0) (h : βˆ€ x : Ξ±, 0 < a*x*x + b*x + c) : discrim a b c < 0 := begin classical, -- TODO: otherwise linarith performance sucks have : βˆ€ x : Ξ±, 0 ≀ a*x*x + b*x + c := assume x, le_of_lt (h x), refine lt_of_le_of_ne (discriminant_le_zero this) _, assume h', have := h (-b / (2 * a)), have : a * (-b / (2 * a)) * (-b / (2 * a)) + b * (-b / (2 * a)) + c = 0, { rw [quadratic_eq_zero_iff_of_discrim_eq_zero ha h' (-b / (2 * a))] }, linarith end
cd294b2abd2e646e30f38873e8381df07e29b2e9
6f1049e897f569e5c47237de40321e62f0181948
/src/solutions/00_first_proofs.lean
e8d7816cba958a293e6601376512875c10b88daa
[ "Apache-2.0" ]
permissive
anrddh/tutorials
f654a0807b9523608544836d9a81939f8e1dceb8
3ba43804e7b632201c494cdaa8da5406f1a255f9
refs/heads/master
1,655,542,921,827
1,588,846,595,000
1,588,846,595,000
262,330,134
0
0
null
1,588,944,346,000
1,588,944,345,000
null
UTF-8
Lean
false
false
18,018
lean
/- This file is intended for Lean beginners. The goal is to demonstrate what it feels like to prove things using Lean and mathlib. Complicated definitions and theory building are not covered. Everything is covered again more slowly and with exercises in the next files. -/ -- We want real numbers and their basic properties import data.real.basic -- We want to be able to define functions using the law of excluded middle noncomputable theory open_locale classical /- Our first goal is to define the set of upper bounds of a set of real numbers. This is already defined in mathlib (in a more general context), but we repeat it for the sake of exposition. Right-click "upper_bounds" below to get offered to jump to mathlib's version -/ #check upper_bounds /-- The set of upper bounds of a set of real numbers ℝ -/ def up_bounds (A : set ℝ) := { x : ℝ | βˆ€ a ∈ A, a ≀ x} /-- Predicate `is_max a A` means `a` is a maximum of `A` -/ def is_max (a : ℝ) (A : set ℝ) := a ∈ A ∧ a ∈ up_bounds A /- In the above definition, the symbol `∧` means "and". We also see the most visible difference between set theoretic foundations and type theoretic ones (used by almost all proof assistants). In set theory, everything is a set, and the only relation you get from foundations are `=` and `∈`. In type theory, there is a meta-theoretic relation of "typing": `a : ℝ` reads "`a` is a real number" or, more precisely, "the type of `a` is `ℝ`". Here "meta-theoretic" means this is not a statement you can prove or disprove inside the theory, it's a fact that is true or not. Here we impose this fact, in other circumstances, it would be checked by the Lean kernel. By contrast, `a ∈ A` is a statement inside the theory. Here it's part of the definition, in other circumstances it could be something proven inside Lean. -/ /- For illustrative purposes, we now define an infix version of the above predicate. It will allow us to write `a is_a_max_of A`, which is closer to a sentence. -/ infix `is_a_max_of`:55 := is_max /- Let's prove something now! A set of real number has at most one maximum. Here everything left of the final `:` is introducing the objects and assumption. The equality `x = y` right of the colon is the conclusion. -/ lemma unique_max (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin -- We first break our assumptions in their two constituent pieces. -- We are free to choose the name following `with` cases hx with x_in x_up, cases hy with y_in y_up, -- Assumption `x_up` means x isn't less than elements of A, let's apply this to y specialize x_up y, -- Assumption `x_up` now needs the information that `y` is indeed in `A`. specialize x_up y_in, -- Let's do this quicker with roles swapped specialize y_up x x_in, -- We explained to Lean the idea of this proof. -- Now we know `x ≀ y` and `y ≀ x`, and Lean shouldn't need more help. -- `linarith` proves equalities and inequalities that follow linearly from -- assumption we have. linarith, end /- The above proof is too long, even if you remove comments. We don't really need the unpacking steps at the beginning, we can access both parts of the assumption `hx : x is_a_max_of A` using shortcuts `h.1` and `h.2`. We can also improve readability without assistance from the tactic state display, clearly announcing intermediate goals using `have`. This way we get to the following version of the same proof. -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin have : x ≀ y, from hy.2 x hx.1, have : y ≀ x, from hx.2 y hy.1, linarith, end /- Notice how mathematics based on type theory treats the assumption `βˆ€ a ∈ A, a ≀ y` as a function turning an element `a` of `A` into the statement `a ≀ y`. More precisely, this assumption is the abbreviation of `βˆ€ a : ℝ, a ∈ A β†’ a ≀ y`. The expression `hy.2 x` appearing in the above proof is then the statement `x ∈ A β†’ x ≀ y`, which itself is a function turning a statement `x ∈ A` into `x ≀ y` so that the full expression `hy.2 x hx.1` is indeed a proof of `x ≀ y`. One could argue a three line long proof of this lemma is still two lines too long. This is debatable, but mathlib's style is to write very short proofs for trivial lemmas. Those proofs are not easy to read but they are meant to indicate that the proof is probably not worth reading. In order to reach this stage, we need to know what linarith did for us. It invoked the lemma `le_antisymm` which says: `x ≀ y β†’ y ≀ x β†’ x = y`. This arrow, which is used both for function and implication, is right associative. So the statement is `x ≀ y β†’ (y ≀ x β†’ x = y)` which reads: I will send a proof `p` of `x ≀ y` to a function sending a proof `p'` of `y ≀ x` to a proof of `x = y`. Hence `le_antisymm p p'` is a proof of `x = y`. Using this we can get our one-line proof: -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := le_antisymm (hy.2 x hx.1) (hx.2 y hy.1) /- Such a proof is called a proof term (or a "term mode" proof). Notice it has no `begin` and `end`. It is directly the kind of low level proof that the Lean kernel is consuming. Commands like `cases`, `specialize` or `linarith` are called tactics, they help users constructing proof terms that could be very tedious to write directly. The most efficient proof style combines tactics with proof terms like our previous `have : x ≀ y, from hy.2 x hx.1` where `hy.2 x hx.1` is a proof term embeded inside a tactic mode proof. In the remaining of this file, we'll be characterizing infima of sets of real numbers in term of sequences. -/ /-- The set of lower bounds of a set of real numbers ℝ -/ def low_bounds (A : set ℝ) := { x : ℝ | βˆ€ a ∈ A, x ≀ a} /- We now define `a` is an infimum of `A`. Again there is already a more general version in mathlib. -/ def is_inf (x : ℝ) (A : set ℝ) := x is_a_max_of (low_bounds A) infix `is_an_inf_of`:55 := is_inf /- We need to prove that any number which is greater than the infimum of A is greater than some element of A. -/ lemma inf_lt {A : set ℝ} {x : ℝ} (hx : x is_an_inf_of A) : βˆ€ y, x < y β†’ βˆƒ a ∈ A, a < y := begin -- Let `y` be any real number. intro y, -- Let's prove the contrapositive contrapose, -- The symbol `Β¬` means negation. Let's ask Lean to rewrite the goal without negation, -- pushing negation through quantifiers and inequalities push_neg, -- Let's assume the premise, calling the assumption `h` intro h, -- `h` is exactly saying `y` is a lower bound of `A` so the second part of -- the infimum assumption `hx` applied to `y` and `h` is exactly what we want. exact hx.2 y h end /- In the above proof, the sequence `contrapose, push_neg` is so common it can be abbreviated to `contrapose!`. With these commands, we enter the gray zone between proof checking and proof finding. Practical computer proof checking crucially needs the computer to handle tedious proof steps. In the next proof, we'll start using `linarith` a bit more seriously, going one step further into automation. Our next real goal is to prove inequalities for limits of sequences. We extract the following lemma: if `y ≀ x + Ξ΅` for all positive `Ξ΅` then `y ≀ x`. -/ lemma le_of_le_add_eps {x y : ℝ} : (βˆ€ Ξ΅ > 0, y ≀ x + Ξ΅) β†’ y ≀ x := begin -- Let's prove the contrapositive, asking Lean to push negations right away. contrapose!, -- Assume `h : x < y`. intro h, -- We need to find `Ξ΅` such that `Ξ΅` is positive and `x + Ξ΅ < y`. -- Let's use `(y-x)/2` use ((y-x)/2), -- we now have two properties to prove. Let's do both in turn, using `linarith` split, linarith, linarith, end /- Note how `linarith` was used for both sub-goals at the end of the above proof. We could have shortened that using the semi-colon combinator instead of comma, writing `split ; linarith`. Next we will study a compressed version of that proof: -/ example {x y : ℝ} : (βˆ€ Ξ΅ > 0, y ≀ x + Ξ΅) β†’ y ≀ x := begin contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩, end /- The angle brackets `⟨` and `⟩` introduce compound data or proofs. A proof of a `βˆƒ z, P z` statemement is composed of a witness `zβ‚€` and a proof `z` of `P zβ‚€`. The compound is denoted by `⟨zβ‚€, h⟩`. In the example above, the predicate is itself compound, it is a conjunction `P z ∧ Q z`. So the proof term should read `⟨zβ‚€, ⟨h₁, hβ‚‚βŸ©βŸ©` where `h₁` (resp. `hβ‚‚`) is a proof of `P zβ‚€` (resp. `Q zβ‚€`). But these so-called "anonymous constructor" brackets are right-associative, so we can get rid of the nested brackets. The keyword `by` introduces tactic mode inside term mode, it is a shorter version of the `begin`/`end` pair, which is more convenient for single tactic blocks. In this example, `begin` enters tactic mode, `exact` leaves it, `by` re-enters it. Going all the way to a proof term would make the proof much longer, because we crucially use automation with `contrapose!` and `linarith`. We can still get a one-line proof using curly braces to gather several tactic invocation, and the `by` abbreviation instead of `begin`/`end`: -/ example {x y : ℝ} : (βˆ€ Ξ΅ > 0, y ≀ x + Ξ΅) β†’ y ≀ x := by { contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩ } /- One could argue that the above proof is a bit too terse, and we are relying too much on linarith. Let's have more `linarith` calls for smaller steps. For the sake of (tiny) variation, we will also assume the premise and argue by contradiction instead of contraposing. -/ example {x y : ℝ} : (βˆ€ Ξ΅ > 0, y ≀ x + Ξ΅) β†’ y ≀ x := begin intro h, -- Assume the conclusion is false, and call this assumption H. by_contradiction H, push_neg at H, -- Now let's compute. have key := calc -- Each line must end with a colon followed by a proof term -- We want to specialize our assumption `h` to `Ξ΅ = (y-x)/2` but this is long to -- type, so let's put a hole `_` that Lean will fill in by comparing the -- statement we want to prove and our proof term with a hole. As usual, -- positivity of `(y-x)/2` is proved by `linarith` y ≀ x + (y-x)/2 : h _ (by linarith) ... = x/2 + y/2 : by ring ... < y : by linarith, -- our key now says `y < y` (notice how the sequence `≀`, `=`, `<` was correctly -- merged into a `<`). Let `linarith` find the desired contradiction now. linarith, -- alternatively, we could have provided the proof term -- `exact lt_irrefl y key` end /- Now we are ready for some analysis. Let's setup notation for absolute value -/ local notation `|`x`|` := abs x /- And let's define convergence of sequences of real numbers (of course there is a much more general definition in mathlib). -/ /-- The sequence `u` tends to `l` -/ def limit (u : β„• β†’ ℝ) (l : ℝ) := βˆ€ Ξ΅ > 0, βˆƒ N, βˆ€ n β‰₯ N, |u n - l| ≀ Ξ΅ /- In the above definition, `u n` denotes the n-th term of the sequence. We can add parentheses to get `u(n)` but we try to avoid parentheses because they pile up very quickly -/ -- If y ≀ u n for all n and u n goes to x then y ≀ x lemma le_lim {x y : ℝ} {u : β„• β†’ ℝ} (hu : limit u x) (ineq : βˆ€ n, y ≀ u n) : y ≀ x := begin -- Let's apply our previous lemma apply le_of_le_add_eps, -- We need to prove y ≀ x + Ξ΅ for all positive Ξ΅. -- Let Ξ΅ be any positive real intros Ξ΅ Ξ΅_pos, -- we now specialize our limit assumption to this `Ξ΅`, and immediately -- fix a `N` as promised by the definition. cases hu Ξ΅ Ξ΅_pos with N HN, -- Now we only need to compute until reaching the conclusion calc y ≀ u N : ineq N ... = x + (u N - x) : by linarith -- We'll need `add_le_add` which says `a ≀ b` and `c ≀ d` implies `a + c ≀ b + d` -- We need a lemma saying `z ≀ |z|`. Because we don't know the name of this lemma, -- let's use `library_search`. Because searching thourgh the library is slow, -- Lean will write what it found in the Lean message window when cursor is on -- that line, so that we can replace it by the lemma. We see `le_max_left` which -- says `a ≀ max a b`. Actually there is a more specific lemma `le_abs_self` ... ≀ x + |u N - x| : add_le_add (by linarith) (by library_search) ... ≀ x + Ξ΅ : add_le_add (by linarith) (HN N (by linarith)), end /- The next lemma has been extracted from the main proof in order to discuss numbers. In ordinary maths, we know that β„• is *not* contained in `ℝ`, whatever the construction of real numbers that we use. For instance a natural number is not an equivalence class of Cauchy sequences. But it's very easy to pretend otherwise. Formal maths requires slightly more care. In the statement below, the "type ascription" `(n + 1 : ℝ)` forces Lean to convert the natural number `n+1` into a real number. The "inclusion" map will be displayed in tactic state as `↑`. There are various lemmas asserting this map is compatible with addition and monotone, but we don't want to bother writing their names. The `norm_cast` tactic is designed to wisely apply those lemmas for us. -/ lemma inv_succ_pos : βˆ€ n : β„•, 1/(n+1 : ℝ) > 0 := begin -- Let `n` be any integer intro n, -- Since we don't know the name of the relevant lemma, asserting that the inverse of -- a positive number is positive, let's state that is suffices -- to prove that `n+1`, seen as a real number, is positive, and ask `library_search` suffices : (n + 1 : ℝ) > 0, { library_search }, -- Now we want to reduce to a statement about natural numbers, not real numbers -- coming from natural numbers. norm_cast, -- and then get the usual help from `linarith` linarith, end /- That was a pretty long proof for an obvious fact. And stating it as a lemma feels stupid, so let's find a way to write it on one line in case we want to include it in some other proof without stating a lemma. First the `library_search` call above displays the name of the relevant lemma: `one_div_pos_of_pos`. We can also replace the `linarith` call on the last line by `library_search` to learn the name of the lemma `nat.succ_pos` asserting that the successor of a natural number is positive. There is also a variant on `norm_cast` that combines it with `exact`. The term mode analogue of `intro` is `Ξ»`. We get down to: -/ example : βˆ€ n : β„•, 1/(n+1 : ℝ) > 0 := Ξ» n, one_div_pos_of_pos (by exact_mod_cast nat.succ_pos n) /- The next proof uses mostly known things, so we will commment only new aspects. -/ lemma limit_inv_succ : βˆ€ Ξ΅ > 0, βˆƒ N : β„•, βˆ€ n β‰₯ N, 1/(n + 1 : ℝ) ≀ Ξ΅ := begin intros Ξ΅ Ξ΅_pos, suffices : βˆƒ N : β„•, 1/Ξ΅ ≀ N, { -- Because we didn't provide a name for the above statement, Lean called it `this`. -- Let's fix an `N` that works. cases this with N HN, use N, intros n Hn, -- Now we want to rewrite the goal using lemmas -- `div_le_iff' : 0 < b β†’ (a / b ≀ c ↔ a ≀ b * c)` -- `div_le_iff : 0 < b β†’ (a / b ≀ c ↔ a ≀ c * b)` -- the second one will be rewritten from right to left, as indicated by `←`. -- Lean will create a side goal for the required positivity assumption that -- we don't provide for `div_le_iff'`. rw [div_le_iff', ← div_le_iff Ξ΅_pos], -- We want to replace assumption `Hn` by its real counter-part so that -- linarith can find what it needs. replace Hn : (N : ℝ) ≀ n, exact_mod_cast Hn, linarith, -- we are still left with the positivity assumption, but already discussed -- how to prove it in the preceding lemma exact_mod_cast nat.succ_pos n }, -- Now we need to prove that sufficient statement. -- We want to use that `ℝ` is archimedean. So we start typing -- `exact archimedean_` and hit Ctrl-space to see what completion Lean proposes -- the lemma `archimedean_iff_nat_le` sounds promising. We select the left to -- right implication using `.1`. This a generic lemma for fields equiped with -- a linear (ie total) order. We need to provide a proof that `ℝ` is indeed -- archimedean. This is done using the `apply_instance` tactic that will be -- covered elsewhere. exact archimedean_iff_nat_le.1 (by apply_instance) (1/Ξ΅), end /- We can now put all pieces together, with almost no new things to explain. -/ lemma inf_seq (A : set ℝ) (x : ℝ) : (x is_an_inf_of A) ↔ (x ∈ low_bounds A ∧ βˆƒ u : β„• β†’ ℝ, limit u x ∧ βˆ€ n, u n ∈ A ) := begin split, { intro h, split, { exact h.1 }, -- On the next line, we don't need to tell Lean to treat `n+1` as a real number because -- we add `x` to it, so Lean knows there is only one way to make sense of this expression. have key : βˆ€ n : β„•, βˆƒ a ∈ A, a < x + 1/(n+1), { intro n, -- we can use the lemma we proved above apply inf_lt h, -- and another one we proved! have : 0 < 1/(n+1 : ℝ), from inv_succ_pos n, linarith }, -- Now we need to use axiom of (countable) choice choose u hu using key, use u, split, { intros Ξ΅ Ξ΅_pos, -- again we use a lemma we proved, specializing it to our fixed `Ξ΅`, and fixing a `N` cases limit_inv_succ Ξ΅ Ξ΅_pos with N H, use N, intros n hn, have : x ≀ u n, from h.1 _ (hu n).1, have := calc u n < x + 1/(n + 1) : (hu n).2 ... ≀ x + Ξ΅ : add_le_add (le_refl x) (H n hn), rw abs_of_nonneg ; linarith }, { intro n, exact (hu n).1 } }, { intro h, -- Assumption `h` is made of nested compound statements. We can use the -- recursive version of `cases` to unpack it in one go. rcases h with ⟨x_min, u, lim, huA⟩, split, exact x_min, intros y y_mino, apply le_lim lim, intro n, exact y_mino (u n) (huA n) }, end
700e53d0acf2d4da86e1a236f1d23a24d66c7346
9028d228ac200bbefe3a711342514dd4e4458bff
/src/algebra/big_operators/basic.lean
b5a7b2df3867f906e8fae9b2a3d6a76d22aff4e4
[ "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
48,162
lean
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import data.finset.fold import data.equiv.mul_add import tactic.abel /-! # Big operators In this file we define products and sums indexed by finite sets (specifically, `finset`). ## Notation We introduce the following notation, localized in `big_operators`. To enable the notation, use `open_locale big_operators`. Let `s` be a `finset Ξ±`, and `f : Ξ± β†’ Ξ²` a function. * `∏ x in s, f x` is notation for `finset.prod s f` (assuming `Ξ²` is a `comm_monoid`) * `βˆ‘ x in s, f x` is notation for `finset.sum s f` (assuming `Ξ²` is an `add_comm_monoid`) * `∏ x, f x` is notation for `finset.prod finset.univ f` (assuming `Ξ±` is a `fintype` and `Ξ²` is a `comm_monoid`) * `βˆ‘ x, f x` is notation for `finset.sum finset.univ f` (assuming `Ξ±` is a `fintype` and `Ξ²` is an `add_comm_monoid`) -/ universes u v w variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} namespace finset /-- `∏ x in s, f x` is the product of `f x` as `x` ranges over the elements of the finite set `s`. -/ @[to_additive "`βˆ‘ x in s, f` is the sum of `f x` as `x` ranges over the elements of the finite set `s`."] protected def prod [comm_monoid Ξ²] (s : finset Ξ±) (f : Ξ± β†’ Ξ²) : Ξ² := (s.1.map f).prod @[simp, to_additive] lemma prod_mk [comm_monoid Ξ²] (s : multiset Ξ±) (hs) (f : Ξ± β†’ Ξ²) : (⟨s, hs⟩ : finset Ξ±).prod f = (s.map f).prod := rfl end finset /- ## Operator precedence of `∏` and `βˆ‘` There is no established mathematical convention for the operator precedence of big operators like `∏` and `βˆ‘`. We will have to make a choice. Online discussions, such as https://math.stackexchange.com/q/185538/30839 seem to suggest that `∏` and `βˆ‘` should have the same precedence, and that this should be somewhere between `*` and `+`. The latter have precedence levels `70` and `65` respectively, and we therefore choose the level `67`. In practice, this means that parentheses should be placed as follows: ```lean βˆ‘ k in K, (a k + b k) = βˆ‘ k in K, a k + βˆ‘ k in K, b k β†’ ∏ k in K, a k * b k = (∏ k in K, a k) * (∏ k in K, b k) ``` (Example taken from page 490 of Knuth's *Concrete Mathematics*.) -/ localized "notation `βˆ‘` binders `, ` r:(scoped:67 f, finset.sum finset.univ f) := r" in big_operators localized "notation `∏` binders `, ` r:(scoped:67 f, finset.prod finset.univ f) := r" in big_operators localized "notation `βˆ‘` binders ` in ` s `, ` r:(scoped:67 f, finset.sum s f) := r" in big_operators localized "notation `∏` binders ` in ` s `, ` r:(scoped:67 f, finset.prod s f) := r" in big_operators open_locale big_operators namespace finset variables {s s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} {f g : Ξ± β†’ Ξ²} @[to_additive] lemma prod_eq_multiset_prod [comm_monoid Ξ²] (s : finset Ξ±) (f : Ξ± β†’ Ξ²) : ∏ x in s, f x = (s.1.map f).prod := rfl @[to_additive] theorem prod_eq_fold [comm_monoid Ξ²] (s : finset Ξ±) (f : Ξ± β†’ Ξ²) : (∏ x in s, f x) = s.fold (*) 1 f := rfl end finset @[to_additive] lemma monoid_hom.map_prod [comm_monoid Ξ²] [comm_monoid Ξ³] (g : Ξ² β†’* Ξ³) (f : Ξ± β†’ Ξ²) (s : finset Ξ±) : g (∏ x in s, f x) = ∏ x in s, g (f x) := by simp only [finset.prod_eq_multiset_prod, g.map_multiset_prod, multiset.map_map] @[to_additive] lemma mul_equiv.map_prod [comm_monoid Ξ²] [comm_monoid Ξ³] (g : Ξ² ≃* Ξ³) (f : Ξ± β†’ Ξ²) (s : finset Ξ±) : g (∏ x in s, f x) = ∏ x in s, g (f x) := g.to_monoid_hom.map_prod f s lemma ring_hom.map_list_prod [semiring Ξ²] [semiring Ξ³] (f : Ξ² β†’+* Ξ³) (l : list Ξ²) : f l.prod = (l.map f).prod := f.to_monoid_hom.map_list_prod l lemma ring_hom.map_list_sum [semiring Ξ²] [semiring Ξ³] (f : Ξ² β†’+* Ξ³) (l : list Ξ²) : f l.sum = (l.map f).sum := f.to_add_monoid_hom.map_list_sum l lemma ring_hom.map_multiset_prod [comm_semiring Ξ²] [comm_semiring Ξ³] (f : Ξ² β†’+* Ξ³) (s : multiset Ξ²) : f s.prod = (s.map f).prod := f.to_monoid_hom.map_multiset_prod s lemma ring_hom.map_multiset_sum [semiring Ξ²] [semiring Ξ³] (f : Ξ² β†’+* Ξ³) (s : multiset Ξ²) : f s.sum = (s.map f).sum := f.to_add_monoid_hom.map_multiset_sum s lemma ring_hom.map_prod [comm_semiring Ξ²] [comm_semiring Ξ³] (g : Ξ² β†’+* Ξ³) (f : Ξ± β†’ Ξ²) (s : finset Ξ±) : g (∏ x in s, f x) = ∏ x in s, g (f x) := g.to_monoid_hom.map_prod f s lemma ring_hom.map_sum [semiring Ξ²] [semiring Ξ³] (g : Ξ² β†’+* Ξ³) (f : Ξ± β†’ Ξ²) (s : finset Ξ±) : g (βˆ‘ x in s, f x) = βˆ‘ x in s, g (f x) := g.to_add_monoid_hom.map_sum f s namespace finset variables {s s₁ sβ‚‚ : finset Ξ±} {a : Ξ±} {f g : Ξ± β†’ Ξ²} section comm_monoid variables [comm_monoid Ξ²] @[simp, to_additive] lemma prod_empty {Ξ± : Type u} {f : Ξ± β†’ Ξ²} : (∏ x in (βˆ…:finset Ξ±), f x) = 1 := rfl @[simp, to_additive] lemma prod_insert [decidable_eq Ξ±] : a βˆ‰ s β†’ (∏ x in (insert a s), f x) = f a * ∏ x in s, f x := fold_insert /-- The product of `f` over `insert a s` is the same as the product over `s`, as long as `a` is in `s` or `f a = 1`. -/ @[simp, to_additive "The sum of `f` over `insert a s` is the same as the sum over `s`, as long as `a` is in `s` or `f a = 0`."] lemma prod_insert_of_eq_one_if_not_mem [decidable_eq Ξ±] (h : a βˆ‰ s β†’ f a = 1) : ∏ x in insert a s, f x = ∏ x in s, f x := begin by_cases hm : a ∈ s, { simp_rw insert_eq_of_mem hm }, { rw [prod_insert hm, h hm, one_mul] }, end /-- The product of `f` over `insert a s` is the same as the product over `s`, as long as `f a = 1`. -/ @[simp, to_additive "The sum of `f` over `insert a s` is the same as the sum over `s`, as long as `f a = 0`."] lemma prod_insert_one [decidable_eq Ξ±] (h : f a = 1) : ∏ x in insert a s, f x = ∏ x in s, f x := prod_insert_of_eq_one_if_not_mem (Ξ» _, h) @[simp, to_additive] lemma prod_singleton : (∏ x in (singleton a), f x) = f a := eq.trans fold_singleton $ mul_one _ @[to_additive] lemma prod_pair [decidable_eq Ξ±] {a b : Ξ±} (h : a β‰  b) : (∏ x in ({a, b} : finset Ξ±), f x) = f a * f b := by rw [prod_insert (not_mem_singleton.2 h), prod_singleton] @[simp, priority 1100] lemma prod_const_one : (∏ x in s, (1 : Ξ²)) = 1 := by simp only [finset.prod, multiset.map_const, multiset.prod_repeat, one_pow] @[simp, priority 1100] lemma sum_const_zero {Ξ²} {s : finset Ξ±} [add_comm_monoid Ξ²] : (βˆ‘ x in s, (0 : Ξ²)) = 0 := @prod_const_one _ (multiplicative Ξ²) _ _ attribute [to_additive] prod_const_one @[simp, to_additive] lemma prod_image [decidable_eq Ξ±] {s : finset Ξ³} {g : Ξ³ β†’ Ξ±} : (βˆ€x∈s, βˆ€y∈s, g x = g y β†’ x = y) β†’ (∏ x in (s.image g), f x) = ∏ x in s, f (g x) := fold_image @[simp, to_additive] lemma prod_map (s : finset Ξ±) (e : Ξ± β†ͺ Ξ³) (f : Ξ³ β†’ Ξ²) : (∏ x in (s.map e), f x) = ∏ x in s, f (e x) := by rw [finset.prod, finset.map_val, multiset.map_map]; refl @[congr, to_additive] lemma prod_congr (h : s₁ = sβ‚‚) : (βˆ€x∈sβ‚‚, f x = g x) β†’ s₁.prod f = sβ‚‚.prod g := by rw [h]; exact fold_congr attribute [congr] finset.sum_congr @[to_additive] lemma prod_union_inter [decidable_eq Ξ±] : (∏ x in (s₁ βˆͺ sβ‚‚), f x) * (∏ x in (s₁ ∩ sβ‚‚), f x) = (∏ x in s₁, f x) * (∏ x in sβ‚‚, f x) := fold_union_inter @[to_additive] lemma prod_union [decidable_eq Ξ±] (h : disjoint s₁ sβ‚‚) : (∏ x in (s₁ βˆͺ sβ‚‚), f x) = (∏ x in s₁, f x) * (∏ x in sβ‚‚, f x) := by rw [←prod_union_inter, (disjoint_iff_inter_eq_empty.mp h)]; exact (mul_one _).symm @[to_additive] lemma prod_sdiff [decidable_eq Ξ±] (h : s₁ βŠ† sβ‚‚) : (∏ x in (sβ‚‚ \ s₁), f x) * (∏ x in s₁, f x) = (∏ x in sβ‚‚, f x) := by rw [←prod_union sdiff_disjoint, sdiff_union_of_subset h] @[simp, to_additive] lemma prod_sum_elim [decidable_eq (Ξ± βŠ• Ξ³)] (s : finset Ξ±) (t : finset Ξ³) (f : Ξ± β†’ Ξ²) (g : Ξ³ β†’ Ξ²) : ∏ x in s.image sum.inl βˆͺ t.image sum.inr, sum.elim f g x = (∏ x in s, f x) * (∏ x in t, g x) := begin rw [prod_union, prod_image, prod_image], { simp only [sum.elim_inl, sum.elim_inr] }, { exact Ξ» _ _ _ _, sum.inr.inj }, { exact Ξ» _ _ _ _, sum.inl.inj }, { rintros i hi, erw [finset.mem_inter, finset.mem_image, finset.mem_image] at hi, rcases hi with ⟨⟨i, hi, rfl⟩, ⟨j, hj, H⟩⟩, cases H } end @[to_additive] lemma prod_bind [decidable_eq Ξ±] {s : finset Ξ³} {t : Ξ³ β†’ finset Ξ±} : (βˆ€ x ∈ s, βˆ€ y ∈ s, x β‰  y β†’ disjoint (t x) (t y)) β†’ (∏ x in (s.bind t), f x) = ∏ x in s, ∏ i in t x, f i := by haveI := classical.dec_eq Ξ³; exact finset.induction_on s (Ξ» _, by simp only [bind_empty, prod_empty]) (assume x s hxs ih hd, have hd' : βˆ€x∈s, βˆ€y∈s, x β‰  y β†’ disjoint (t x) (t y), from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy), have βˆ€y∈s, x β‰  y, from assume _ hy h, by rw [←h] at hy; contradiction, have βˆ€y∈s, disjoint (t x) (t y), from assume _ hy, hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hy) (this _ hy), have disjoint (t x) (finset.bind s t), from (disjoint_bind_right _ _ _).mpr this, by simp only [bind_insert, prod_insert hxs, prod_union this, ih hd']) @[to_additive] lemma prod_product {s : finset Ξ³} {t : finset Ξ±} {f : Ξ³Γ—Ξ± β†’ Ξ²} : (∏ x in s.product t, f x) = ∏ x in s, ∏ y in t, f (x, y) := begin haveI := classical.dec_eq Ξ±, haveI := classical.dec_eq Ξ³, rw [product_eq_bind, prod_bind], { congr, funext, exact prod_image (Ξ» _ _ _ _ H, (prod.mk.inj H).2) }, simp only [disjoint_iff_ne, mem_image], rintros _ _ _ _ h ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ _, apply h, cc end /-- An uncurried version of `prod_product`. -/ @[to_additive] lemma prod_product' {s : finset Ξ³} {t : finset Ξ±} {f : Ξ³ β†’ Ξ± β†’ Ξ²} : (∏ x in s.product t, f x.1 x.2) = ∏ x in s, ∏ y in t, f x y := prod_product @[to_additive] lemma prod_sigma {Οƒ : Ξ± β†’ Type*} {s : finset Ξ±} {t : Ξ a, finset (Οƒ a)} {f : sigma Οƒ β†’ Ξ²} : (∏ x in s.sigma t, f x) = ∏ a in s, ∏ s in (t a), f ⟨a, s⟩ := by classical; calc (∏ x in s.sigma t, f x) = ∏ x in s.bind (Ξ»a, (t a).map (function.embedding.sigma_mk a)), f x : by rw sigma_eq_bind ... = ∏ a in s, ∏ x in (t a).map (function.embedding.sigma_mk a), f x : prod_bind $ assume a₁ ha aβ‚‚ haβ‚‚ h x hx, by { simp only [inf_eq_inter, mem_inter, mem_map, function.embedding.sigma_mk_to_fun] at hx, rcases hx with ⟨⟨y, hy, rfl⟩, ⟨z, hz, hz'⟩⟩, cc } ... = ∏ a in s, ∏ s in t a, f ⟨a, s⟩ : prod_congr rfl $ Ξ» _ _, prod_map _ _ _ @[to_additive] lemma prod_fiberwise_of_maps_to [decidable_eq Ξ³] {s : finset Ξ±} {t : finset Ξ³} {g : Ξ± β†’ Ξ³} (h : βˆ€ x ∈ s, g x ∈ t) (f : Ξ± β†’ Ξ²) : (∏ y in t, ∏ x in s.filter (Ξ» x, g x = y), f x) = ∏ x in s, f x := begin letI := classical.dec_eq Ξ±, rw [← bind_filter_eq_of_maps_to h] {occs := occurrences.pos [2]}, refine (prod_bind $ Ξ» x' hx y' hy hne, _).symm, rw [disjoint_filter], rintros x hx rfl, exact hne end @[to_additive] lemma prod_image' [decidable_eq Ξ±] {s : finset Ξ³} {g : Ξ³ β†’ Ξ±} (h : Ξ³ β†’ Ξ²) (eq : βˆ€c∈s, f (g c) = ∏ x in s.filter (Ξ»c', g c' = g c), h x) : (∏ x in s.image g, f x) = ∏ x in s, h x := calc (∏ x in s.image g, f x) = ∏ x in s.image g, ∏ x in s.filter (Ξ» c', g c' = x), h x : prod_congr rfl $ Ξ» x hx, let ⟨c, hcs, hc⟩ := mem_image.1 hx in hc β–Έ (eq c hcs) ... = ∏ x in s, h x : prod_fiberwise_of_maps_to (Ξ» x, mem_image_of_mem g) _ @[to_additive] lemma prod_mul_distrib : ∏ x in s, (f x * g x) = (∏ x in s, f x) * (∏ x in s, g x) := eq.trans (by rw one_mul; refl) fold_op_distrib @[to_additive] lemma prod_comm {s : finset Ξ³} {t : finset Ξ±} {f : Ξ³ β†’ Ξ± β†’ Ξ²} : (∏ x in s, ∏ y in t, f x y) = (∏ y in t, ∏ x in s, f x y) := begin classical, apply finset.induction_on s, { simp only [prod_empty, prod_const_one] }, { intros _ _ H ih, simp only [prod_insert H, prod_mul_distrib, ih] } end @[to_additive] lemma prod_hom [comm_monoid Ξ³] (s : finset Ξ±) {f : Ξ± β†’ Ξ²} (g : Ξ² β†’ Ξ³) [is_monoid_hom g] : (∏ x in s, g (f x)) = g (∏ x in s, f x) := ((monoid_hom.of g).map_prod f s).symm @[to_additive] lemma prod_hom_rel [comm_monoid Ξ³] {r : Ξ² β†’ Ξ³ β†’ Prop} {f : Ξ± β†’ Ξ²} {g : Ξ± β†’ Ξ³} {s : finset Ξ±} (h₁ : r 1 1) (hβ‚‚ : βˆ€a b c, r b c β†’ r (f a * b) (g a * c)) : r (∏ x in s, f x) (∏ x in s, g x) := by { delta finset.prod, apply multiset.prod_hom_rel; assumption } @[to_additive] lemma prod_subset (h : s₁ βŠ† sβ‚‚) (hf : βˆ€ x ∈ sβ‚‚, x βˆ‰ s₁ β†’ f x = 1) : (∏ x in s₁, f x) = ∏ x in sβ‚‚, f x := by haveI := classical.dec_eq Ξ±; exact have ∏ x in sβ‚‚ \ s₁, f x = ∏ x in sβ‚‚ \ s₁, 1, from prod_congr rfl $ by simpa only [mem_sdiff, and_imp], by rw [←prod_sdiff h]; simp only [this, prod_const_one, one_mul] @[to_additive] lemma prod_filter_of_ne {p : Ξ± β†’ Prop} [decidable_pred p] (hp : βˆ€ x ∈ s, f x β‰  1 β†’ p x) : (∏ x in (s.filter p), f x) = (∏ x in s, f x) := prod_subset (filter_subset _) $ Ξ» x, by { classical, rw [not_imp_comm, mem_filter], exact Ξ» h₁ hβ‚‚, ⟨h₁, hp _ h₁ hβ‚‚βŸ© } -- If we use `[decidable_eq Ξ²]` here, some rewrites fail because they find a wrong `decidable` -- instance first; `{βˆ€x, decidable (f x β‰  1)}` doesn't work with `rw ← prod_filter_ne_one` @[to_additive] lemma prod_filter_ne_one [βˆ€ x, decidable (f x β‰  1)] : (∏ x in (s.filter $ Ξ»x, f x β‰  1), f x) = (∏ x in s, f x) := prod_filter_of_ne $ Ξ» _ _, id @[to_additive] lemma prod_filter (p : Ξ± β†’ Prop) [decidable_pred p] (f : Ξ± β†’ Ξ²) : (∏ a in s.filter p, f a) = (∏ a in s, if p a then f a else 1) := calc (∏ a in s.filter p, f a) = ∏ a in s.filter p, if p a then f a else 1 : prod_congr rfl (assume a h, by rw [if_pos (mem_filter.1 h).2]) ... = ∏ a in s, if p a then f a else 1 : begin refine prod_subset (filter_subset s) (assume x hs h, _), rw [mem_filter, not_and] at h, exact if_neg (h hs) end @[to_additive] lemma prod_eq_single {s : finset Ξ±} {f : Ξ± β†’ Ξ²} (a : Ξ±) (hβ‚€ : βˆ€b∈s, b β‰  a β†’ f b = 1) (h₁ : a βˆ‰ s β†’ f a = 1) : (∏ x in s, f x) = f a := by haveI := classical.dec_eq Ξ±; from classical.by_cases (assume : a ∈ s, calc (∏ x in s, f x) = ∏ x in {a}, f x : begin refine (prod_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { simpa only [mem_singleton] } end ... = f a : prod_singleton) (assume : a βˆ‰ s, (prod_congr rfl $ Ξ» b hb, hβ‚€ b hb $ by rintro rfl; cc).trans $ prod_const_one.trans (h₁ this).symm) @[to_additive] lemma prod_attach {f : Ξ± β†’ Ξ²} : (∏ x in s.attach, f x) = (∏ x in s, f x) := by haveI := classical.dec_eq Ξ±; exact calc (∏ x in s.attach, f x.val) = (∏ x in (s.attach).image subtype.val, f x) : by rw [prod_image]; exact assume x _ y _, subtype.eq ... = _ : by rw [attach_image_val] /-- A product over `s.subtype p` equals one over `s.filter p`. -/ @[simp, to_additive "A sum over `s.subtype p` equals one over `s.filter p`."] lemma prod_subtype_eq_prod_filter (f : Ξ± β†’ Ξ²) {p : Ξ± β†’ Prop} [decidable_pred p] : ∏ x in s.subtype p, f x = ∏ x in s.filter p, f x := begin conv_lhs { erw ←prod_map (s.subtype p) (function.embedding.subtype _) f }, exact prod_congr (subtype_map _) (Ξ» x hx, rfl) end /-- If all elements of a `finset` satisfy the predicate `p`, a product over `s.subtype p` equals that product over `s`. -/ @[to_additive "If all elements of a `finset` satisfy the predicate `p`, a sum over `s.subtype p` equals that sum over `s`."] lemma prod_subtype_of_mem (f : Ξ± β†’ Ξ²) {p : Ξ± β†’ Prop} [decidable_pred p] (h : βˆ€ x ∈ s, p x) : ∏ x in s.subtype p, f x = ∏ x in s, f x := by simp_rw [prod_subtype_eq_prod_filter, filter_true_of_mem h] /-- A product of a function over a `finset` in a subtype equals a product in the main type of a function that agrees with the first function on that `finset`. -/ @[to_additive "A sum of a function over a `finset` in a subtype equals a sum in the main type of a function that agrees with the first function on that `finset`."] lemma prod_subtype_map_embedding {p : Ξ± β†’ Prop} {s : finset {x // p x}} {f : {x // p x} β†’ Ξ²} {g : Ξ± β†’ Ξ²} (h : βˆ€ x : {x // p x}, x ∈ s β†’ g x = f x) : ∏ x in s.map (function.embedding.subtype _), g x = ∏ x in s, f x := begin rw finset.prod_map, exact finset.prod_congr rfl h end @[to_additive] lemma prod_eq_one {f : Ξ± β†’ Ξ²} {s : finset Ξ±} (h : βˆ€x∈s, f x = 1) : (∏ x in s, f x) = 1 := calc (∏ x in s, f x) = ∏ x in s, 1 : finset.prod_congr rfl h ... = 1 : finset.prod_const_one @[to_additive] lemma prod_apply_dite {s : finset Ξ±} {p : Ξ± β†’ Prop} {hp : decidable_pred p} (f : Ξ  (x : Ξ±), p x β†’ Ξ³) (g : Ξ  (x : Ξ±), Β¬p x β†’ Ξ³) (h : Ξ³ β†’ Ξ²) : (∏ x in s, h (if hx : p x then f x hx else g x hx)) = (∏ x in (s.filter p).attach, h (f x.1 (mem_filter.mp x.2).2)) * (∏ x in (s.filter (Ξ» x, Β¬ p x)).attach, h (g x.1 (mem_filter.mp x.2).2)) := by letI := classical.dec_eq Ξ±; exact calc ∏ x in s, h (if hx : p x then f x hx else g x hx) = ∏ x in s.filter p βˆͺ s.filter (Ξ» x, Β¬ p x), h (if hx : p x then f x hx else g x hx) : by rw [filter_union_filter_neg_eq] ... = (∏ x in s.filter p, h (if hx : p x then f x hx else g x hx)) * (∏ x in s.filter (Ξ» x, Β¬ p x), h (if hx : p x then f x hx else g x hx)) : prod_union (by simp [disjoint_right] {contextual := tt}) ... = (∏ x in (s.filter p).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) * (∏ x in (s.filter (Ξ» x, Β¬ p x)).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) : congr_arg2 _ prod_attach.symm prod_attach.symm ... = (∏ x in (s.filter p).attach, h (f x.1 (mem_filter.mp x.2).2)) * (∏ x in (s.filter (Ξ» x, Β¬ p x)).attach, h (g x.1 (mem_filter.mp x.2).2)) : congr_arg2 _ (prod_congr rfl (Ξ» x hx, congr_arg h (dif_pos (mem_filter.mp x.2).2))) (prod_congr rfl (Ξ» x hx, congr_arg h (dif_neg (mem_filter.mp x.2).2))) @[to_additive] lemma prod_apply_ite {s : finset Ξ±} {p : Ξ± β†’ Prop} {hp : decidable_pred p} (f g : Ξ± β†’ Ξ³) (h : Ξ³ β†’ Ξ²) : (∏ x in s, h (if p x then f x else g x)) = (∏ x in s.filter p, h (f x)) * (∏ x in s.filter (Ξ» x, Β¬ p x), h (g x)) := trans (prod_apply_dite _ _ _) (congr_arg2 _ (@prod_attach _ _ _ _ (h ∘ f)) (@prod_attach _ _ _ _ (h ∘ g))) @[to_additive] lemma prod_dite {s : finset Ξ±} {p : Ξ± β†’ Prop} {hp : decidable_pred p} (f : Ξ  (x : Ξ±), p x β†’ Ξ²) (g : Ξ  (x : Ξ±), Β¬p x β†’ Ξ²) : (∏ x in s, if hx : p x then f x hx else g x hx) = (∏ x in (s.filter p).attach, f x.1 (mem_filter.mp x.2).2) * (∏ x in (s.filter (Ξ» x, Β¬ p x)).attach, g x.1 (mem_filter.mp x.2).2) := by simp [prod_apply_dite _ _ (Ξ» x, x)] @[to_additive] lemma prod_ite {s : finset Ξ±} {p : Ξ± β†’ Prop} {hp : decidable_pred p} (f g : Ξ± β†’ Ξ²) : (∏ x in s, if p x then f x else g x) = (∏ x in s.filter p, f x) * (∏ x in s.filter (Ξ» x, Β¬ p x), g x) := by simp [prod_apply_ite _ _ (Ξ» x, x)] @[to_additive] lemma prod_extend_by_one [decidable_eq Ξ±] (s : finset Ξ±) (f : Ξ± β†’ Ξ²) : ∏ i in s, (if i ∈ s then f i else 1) = ∏ i in s, f i := prod_congr rfl $ Ξ» i hi, if_pos hi @[simp, to_additive] lemma prod_dite_eq [decidable_eq Ξ±] (s : finset Ξ±) (a : Ξ±) (b : Ξ  x : Ξ±, a = x β†’ Ξ²) : (∏ x in s, (if h : a = x then b x h else 1)) = ite (a ∈ s) (b a rfl) 1 := begin split_ifs with h, { rw [finset.prod_eq_single a, dif_pos rfl], { intros, rw dif_neg, cc }, { cc } }, { rw finset.prod_eq_one, intros, rw dif_neg, intro, cc } end @[simp, to_additive] lemma prod_dite_eq' [decidable_eq Ξ±] (s : finset Ξ±) (a : Ξ±) (b : Ξ  x : Ξ±, x = a β†’ Ξ²) : (∏ x in s, (if h : x = a then b x h else 1)) = ite (a ∈ s) (b a rfl) 1 := begin split_ifs with h, { rw [finset.prod_eq_single a, dif_pos rfl], { intros, rw dif_neg, cc }, { cc } }, { rw finset.prod_eq_one, intros, rw dif_neg, intro, cc } end @[simp, to_additive] lemma prod_ite_eq [decidable_eq Ξ±] (s : finset Ξ±) (a : Ξ±) (b : Ξ± β†’ Ξ²) : (∏ x in s, (ite (a = x) (b x) 1)) = ite (a ∈ s) (b a) 1 := prod_dite_eq s a (Ξ» x _, b x) /-- When a product is taken over a conditional whose condition is an equality test on the index and whose alternative is 1, then the product's value is either the term at that index or `1`. The difference with `prod_ite_eq` is that the arguments to `eq` are swapped. -/ @[simp, to_additive] lemma prod_ite_eq' [decidable_eq Ξ±] (s : finset Ξ±) (a : Ξ±) (b : Ξ± β†’ Ξ²) : (∏ x in s, (ite (x = a) (b x) 1)) = ite (a ∈ s) (b a) 1 := prod_dite_eq' s a (Ξ» x _, b x) /-- Reorder a product. The difference with `prod_bij'` is that the bijection is specified as a surjective injection, rather than by an inverse function. -/ @[to_additive] lemma prod_bij {s : finset Ξ±} {t : finset Ξ³} {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ²} (i : Ξ a∈s, Ξ³) (hi : βˆ€a ha, i a ha ∈ t) (h : βˆ€a ha, f a = g (i a ha)) (i_inj : βˆ€a₁ aβ‚‚ ha₁ haβ‚‚, i a₁ ha₁ = i aβ‚‚ haβ‚‚ β†’ a₁ = aβ‚‚) (i_surj : βˆ€b∈t, βˆƒa ha, b = i a ha) : (∏ x in s, f x) = (∏ x in t, g x) := congr_arg multiset.prod (multiset.map_eq_map_of_bij_of_nodup f g s.2 t.2 i hi h i_inj i_surj) /-- Reorder a product. The difference with `prod_bij` is that the bijection is specified with an inverse, rather than as a surjective injection. -/ @[to_additive] lemma prod_bij' {s : finset Ξ±} {t : finset Ξ³} {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ²} (i : Ξ a∈s, Ξ³) (hi : βˆ€a ha, i a ha ∈ t) (h : βˆ€a ha, f a = g (i a ha)) (j : Ξ a∈t, Ξ±) (hj : βˆ€a ha, j a ha ∈ s) (left_inv : βˆ€ a ha, j (i a ha) (hi a ha) = a) (right_inv : βˆ€ a ha, i (j a ha) (hj a ha) = a) : (∏ x in s, f x) = (∏ x in t, g x) := begin refine prod_bij i hi h _ _, {intros a1 a2 h1 h2 eq, rw [←left_inv a1 h1, ←left_inv a2 h2], cc,}, {intros b hb, use j b hb, use hj b hb, exact (right_inv b hb).symm,}, end @[to_additive] lemma prod_bij_ne_one {s : finset Ξ±} {t : finset Ξ³} {f : Ξ± β†’ Ξ²} {g : Ξ³ β†’ Ξ²} (i : Ξ a∈s, f a β‰  1 β†’ Ξ³) (hi₁ : βˆ€a h₁ hβ‚‚, i a h₁ hβ‚‚ ∈ t) (hiβ‚‚ : βˆ€a₁ aβ‚‚ h₁₁ h₁₂ h₂₁ hβ‚‚β‚‚, i a₁ h₁₁ h₁₂ = i aβ‚‚ h₂₁ hβ‚‚β‚‚ β†’ a₁ = aβ‚‚) (hi₃ : βˆ€b∈t, g b β‰  1 β†’ βˆƒa h₁ hβ‚‚, b = i a h₁ hβ‚‚) (h : βˆ€a h₁ hβ‚‚, f a = g (i a h₁ hβ‚‚)) : (∏ x in s, f x) = (∏ x in t, g x) := by classical; exact calc (∏ x in s, f x) = ∏ x in (s.filter $ Ξ»x, f x β‰  1), f x : prod_filter_ne_one.symm ... = ∏ x in (t.filter $ Ξ»x, g x β‰  1), g x : prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2) (assume a ha, (mem_filter.mp ha).elim $ Ξ»h₁ hβ‚‚, mem_filter.mpr ⟨hi₁ a h₁ hβ‚‚, Ξ» hg, hβ‚‚ (hg β–Έ h a h₁ hβ‚‚)⟩) (assume a ha, (mem_filter.mp ha).elim $ h a) (assume a₁ aβ‚‚ ha₁ haβ‚‚, (mem_filter.mp ha₁).elim $ Ξ» ha₁₁ ha₁₂, (mem_filter.mp haβ‚‚).elim $ Ξ» ha₂₁ haβ‚‚β‚‚, hiβ‚‚ a₁ aβ‚‚ _ _ _ _) (assume b hb, (mem_filter.mp hb).elim $ Ξ»h₁ hβ‚‚, let ⟨a, ha₁, haβ‚‚, eq⟩ := hi₃ b h₁ hβ‚‚ in ⟨a, mem_filter.mpr ⟨ha₁, haβ‚‚βŸ©, eq⟩) ... = (∏ x in t, g x) : prod_filter_ne_one @[to_additive] lemma nonempty_of_prod_ne_one (h : (∏ x in s, f x) β‰  1) : s.nonempty := s.eq_empty_or_nonempty.elim (Ξ» H, false.elim $ h $ H.symm β–Έ prod_empty) id @[to_additive] lemma exists_ne_one_of_prod_ne_one (h : (∏ x in s, f x) β‰  1) : βˆƒa∈s, f a β‰  1 := begin classical, rw ← prod_filter_ne_one at h, rcases nonempty_of_prod_ne_one h with ⟨x, hx⟩, exact ⟨x, (mem_filter.1 hx).1, (mem_filter.1 hx).2⟩ end @[to_additive] lemma prod_subset_one_on_sdiff [decidable_eq Ξ±] (h : s₁ βŠ† sβ‚‚) (hg : βˆ€ x ∈ (sβ‚‚ \ s₁), g x = 1) (hfg : βˆ€ x ∈ s₁, f x = g x) : ∏ i in s₁, f i = ∏ i in sβ‚‚, g i := begin rw [← prod_sdiff h, prod_eq_one hg, one_mul], exact prod_congr rfl hfg end lemma sum_range_succ {Ξ²} [add_comm_monoid Ξ²] (f : β„• β†’ Ξ²) (n : β„•) : (βˆ‘ x in range (n + 1), f x) = f n + (βˆ‘ x in range n, f x) := by rw [range_succ, sum_insert not_mem_range_self] @[to_additive] lemma prod_range_succ (f : β„• β†’ Ξ²) (n : β„•) : (∏ x in range (n + 1), f x) = f n * (∏ x in range n, f x) := by rw [range_succ, prod_insert not_mem_range_self] lemma prod_range_succ' (f : β„• β†’ Ξ²) : βˆ€ n : β„•, (∏ k in range (n + 1), f k) = (∏ k in range n, f (k+1)) * f 0 | 0 := (prod_range_succ _ _).trans $ mul_comm _ _ | (n + 1) := by rw [prod_range_succ (Ξ» m, f (nat.succ m)), mul_assoc, ← prod_range_succ']; exact prod_range_succ _ _ @[to_additive] lemma prod_range_zero (f : β„• β†’ Ξ²) : (∏ k in range 0, f k) = 1 := by rw [range_zero, prod_empty] lemma prod_range_one (f : β„• β†’ Ξ²) : (∏ k in range 1, f k) = f 0 := by { rw [range_one], apply @prod_singleton β„• Ξ² 0 f } lemma sum_range_one {Ξ΄ : Type*} [add_comm_monoid Ξ΄] (f : β„• β†’ Ξ΄) : (βˆ‘ k in range 1, f k) = f 0 := @prod_range_one (multiplicative Ξ΄) _ f attribute [to_additive finset.sum_range_one] prod_range_one open multiset lemma prod_multiset_map_count [decidable_eq Ξ±] (s : multiset Ξ±) {M : Type*} [comm_monoid M] (f : Ξ± β†’ M) : (s.map f).prod = ∏ m in s.to_finset, (f m) ^ (s.count m) := begin apply s.induction_on, { simp only [prod_const_one, count_zero, prod_zero, pow_zero, map_zero] }, intros a s ih, simp only [prod_cons, map_cons, to_finset_cons, ih], by_cases has : a ∈ s.to_finset, { rw [insert_eq_of_mem has, ← insert_erase has, prod_insert (not_mem_erase _ _), prod_insert (not_mem_erase _ _), ← mul_assoc, count_cons_self, pow_succ], congr' 1, refine prod_congr rfl (Ξ» x hx, _), rw [count_cons_of_ne (ne_of_mem_erase hx)] }, rw [prod_insert has, count_cons_self, count_eq_zero_of_not_mem (mt mem_to_finset.2 has), pow_one], congr' 1, refine prod_congr rfl (Ξ» x hx, _), rw count_cons_of_ne, rintro rfl, exact has hx end lemma prod_multiset_count [decidable_eq Ξ±] [comm_monoid Ξ±] (s : multiset Ξ±) : s.prod = ∏ m in s.to_finset, m ^ (s.count m) := by { convert prod_multiset_map_count s id, rw map_id } /-- To prove a property of a product, it suffices to prove that the property is multiplicative and holds on factors. -/ @[to_additive "To prove a property of a sum, it suffices to prove that the property is additive and holds on summands."] lemma prod_induction {M : Type*} [comm_monoid M] (f : Ξ± β†’ M) (p : M β†’ Prop) (p_mul : βˆ€ a b, p a β†’ p b β†’ p (a * b)) (p_one : p 1) (p_s : βˆ€ x ∈ s, p $ f x) : p $ ∏ x in s, f x := begin classical, induction s using finset.induction with x hx s hs, simpa, rw finset.prod_insert, swap, assumption, apply p_mul, apply p_s, simp, apply hs, intros a ha, apply p_s, simp [ha], end /-- For any product along `{0, ..., n-1}` of a commutative-monoid-valued function, we can verify that it's equal to a different function just by checking ratios of adjacent terms. This is a multiplicative discrete analogue of the fundamental theorem of calculus. -/ lemma prod_range_induction {M : Type*} [comm_monoid M] (f s : β„• β†’ M) (h0 : s 0 = 1) (h : βˆ€ n, s (n + 1) = s n * f n) (n : β„•) : ∏ k in finset.range n, f k = s n := begin induction n with k hk, { simp only [h0, finset.prod_range_zero] }, { simp only [hk, finset.prod_range_succ, h, mul_comm] } end /-- For any sum along `{0, ..., n-1}` of a commutative-monoid-valued function, we can verify that it's equal to a different function just by checking differences of adjacent terms. This is a discrete analogue of the fundamental theorem of calculus. -/ lemma sum_range_induction {M : Type*} [add_comm_monoid M] (f s : β„• β†’ M) (h0 : s 0 = 0) (h : βˆ€ n, s (n + 1) = s n + f n) (n : β„•) : βˆ‘ k in finset.range n, f k = s n := @prod_range_induction (multiplicative M) _ f s h0 h n /-- A telescoping sum along `{0, ..., n-1}` of an additive commutative group valued function reduces to the difference of the last and first terms.-/ lemma sum_range_sub {G : Type*} [add_comm_group G] (f : β„• β†’ G) (n : β„•) : βˆ‘ i in range n, (f (i+1) - f i) = f n - f 0 := by { apply sum_range_induction; abel, simp } lemma sum_range_sub' {G : Type*} [add_comm_group G] (f : β„• β†’ G) (n : β„•) : βˆ‘ i in range n, (f i - f (i+1)) = f 0 - f n := by { apply sum_range_induction; abel, simp } /-- A telescoping product along `{0, ..., n-1}` of a commutative group valued function reduces to the ratio of the last and first factors.-/ @[to_additive] lemma prod_range_div {M : Type*} [comm_group M] (f : β„• β†’ M) (n : β„•) : ∏ i in range n, (f (i+1) * (f i)⁻¹) = f n * (f 0)⁻¹ := by apply @sum_range_sub (additive M) @[to_additive] lemma prod_range_div' {M : Type*} [comm_group M] (f : β„• β†’ M) (n : β„•) : ∏ i in range n, (f i * (f (i+1))⁻¹) = (f 0) * (f n)⁻¹ := by apply @sum_range_sub' (additive M) /-- A telescoping sum along `{0, ..., n-1}` of an `β„•`-valued function reduces to the difference of the last and first terms when the function we are summing is monotone. -/ lemma sum_range_sub_of_monotone {f : β„• β†’ β„•} (h : monotone f) (n : β„•) : βˆ‘ i in range n, (f (i+1) - f i) = f n - f 0 := begin refine sum_range_induction _ _ (nat.sub_self _) (Ξ» n, _) _, have h₁ : f n ≀ f (n+1) := h (nat.le_succ _), have hβ‚‚ : f 0 ≀ f n := h (nat.zero_le _), rw [←nat.sub_add_comm hβ‚‚, nat.add_sub_cancel' h₁], end @[simp] lemma prod_const (b : Ξ²) : (∏ x in s, b) = b ^ s.card := by haveI := classical.dec_eq Ξ±; exact finset.induction_on s rfl (Ξ» a s has ih, by rw [prod_insert has, card_insert_of_not_mem has, pow_succ, ih]) lemma pow_eq_prod_const (b : Ξ²) : βˆ€ n, b ^ n = ∏ k in range n, b | 0 := rfl | (n+1) := by simp lemma prod_pow (s : finset Ξ±) (n : β„•) (f : Ξ± β†’ Ξ²) : (∏ x in s, f x ^ n) = (∏ x in s, f x) ^ n := by haveI := classical.dec_eq Ξ±; exact finset.induction_on s (by simp) (by simp [mul_pow] {contextual := tt}) -- `to_additive` fails on this lemma, so we prove it manually below lemma prod_flip {n : β„•} (f : β„• β†’ Ξ²) : (∏ r in range (n + 1), f (n - r)) = (∏ k in range (n + 1), f k) := begin induction n with n ih, { rw [prod_range_one, prod_range_one] }, { rw [prod_range_succ', prod_range_succ _ (nat.succ n), mul_comm], simp [← ih] } end @[to_additive] lemma prod_involution {s : finset Ξ±} {f : Ξ± β†’ Ξ²} : βˆ€ (g : Ξ  a ∈ s, Ξ±) (h₁ : βˆ€ a ha, f a * f (g a ha) = 1) (hβ‚‚ : βˆ€ a ha, f a β‰  1 β†’ g a ha β‰  a) (h₃ : βˆ€ a ha, g a ha ∈ s) (hβ‚„ : βˆ€ a ha, g (g a ha) (h₃ a ha) = a), (∏ x in s, f x) = 1 := by haveI := classical.dec_eq Ξ±; haveI := classical.dec_eq Ξ²; exact finset.strong_induction_on s (Ξ» s ih g h₁ hβ‚‚ h₃ hβ‚„, s.eq_empty_or_nonempty.elim (Ξ» hs, hs.symm β–Έ rfl) (Ξ» ⟨x, hx⟩, have hmem : βˆ€ y ∈ (s.erase x).erase (g x hx), y ∈ s, from Ξ» y hy, (mem_of_mem_erase (mem_of_mem_erase hy)), have g_inj : βˆ€ {x hx y hy}, g x hx = g y hy β†’ x = y, from Ξ» x hx y hy h, by rw [← hβ‚„ x hx, ← hβ‚„ y hy]; simp [h], have ih': ∏ y in erase (erase s x) (g x hx), f y = (1 : Ξ²) := ih ((s.erase x).erase (g x hx)) ⟨subset.trans (erase_subset _ _) (erase_subset _ _), Ξ» h, not_mem_erase (g x hx) (s.erase x) (h (h₃ x hx))⟩ (Ξ» y hy, g y (hmem y hy)) (Ξ» y hy, h₁ y (hmem y hy)) (Ξ» y hy, hβ‚‚ y (hmem y hy)) (Ξ» y hy, mem_erase.2 ⟨λ (h : g y _ = g x hx), by simpa [g_inj h] using hy, mem_erase.2 ⟨λ (h : g y _ = x), have y = g x hx, from hβ‚„ y (hmem y hy) β–Έ by simp [h], by simpa [this] using hy, h₃ y (hmem y hy)⟩⟩) (Ξ» y hy, hβ‚„ y (hmem y hy)), if hx1 : f x = 1 then ih' β–Έ eq.symm (prod_subset hmem (Ξ» y hy hy₁, have y = x ∨ y = g x hx, by simp [hy] at hy₁; tauto, this.elim (Ξ» h, h.symm β–Έ hx1) (Ξ» h, h₁ x hx β–Έ h β–Έ hx1.symm β–Έ (one_mul _).symm))) else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase (mem_erase.2 ⟨hβ‚‚ x hx hx1, h₃ x hx⟩), prod_insert (not_mem_erase _ _), ih', mul_one, h₁ x hx])) /-- The product of the composition of functions `f` and `g`, is the product over `b ∈ s.image g` of `f b` to the power of the cardinality of the fibre of `b` -/ lemma prod_comp [decidable_eq Ξ³] {s : finset Ξ±} (f : Ξ³ β†’ Ξ²) (g : Ξ± β†’ Ξ³) : ∏ a in s, f (g a) = ∏ b in s.image g, f b ^ (s.filter (Ξ» a, g a = b)).card := calc ∏ a in s, f (g a) = ∏ x in (s.image g).sigma (Ξ» b : Ξ³, s.filter (Ξ» a, g a = b)), f (g x.2) : prod_bij (Ξ» a ha, ⟨g a, a⟩) (by simp; tauto) (Ξ» _ _, rfl) (by simp) (by finish) ... = ∏ b in s.image g, ∏ a in s.filter (Ξ» a, g a = b), f (g a) : prod_sigma ... = ∏ b in s.image g, ∏ a in s.filter (Ξ» a, g a = b), f b : prod_congr rfl (Ξ» b hb, prod_congr rfl (by simp {contextual := tt})) ... = ∏ b in s.image g, f b ^ (s.filter (Ξ» a, g a = b)).card : prod_congr rfl (Ξ» _ _, prod_const _) @[to_additive] lemma prod_piecewise [decidable_eq Ξ±] (s t : finset Ξ±) (f g : Ξ± β†’ Ξ²) : (∏ x in s, (t.piecewise f g) x) = (∏ x in s ∩ t, f x) * (∏ x in s \ t, g x) := by { rw [piecewise, prod_ite, filter_mem_eq_inter, ← sdiff_eq_filter], } @[to_additive] lemma prod_inter_mul_prod_diff [decidable_eq Ξ±] (s t : finset Ξ±) (f : Ξ± β†’ Ξ²) : (∏ x in s ∩ t, f x) * (∏ x in s \ t, f x) = (∏ x in s, f x) := by { convert (s.prod_piecewise t f f).symm, simp [finset.piecewise] } @[to_additive] lemma mul_prod_diff_singleton [decidable_eq Ξ±] {s : finset Ξ±} {i : Ξ±} (h : i ∈ s) (f : Ξ± β†’ Ξ²) : f i * (∏ x in s \ {i}, f x) = ∏ x in s, f x := by { convert s.prod_inter_mul_prod_diff {i} f, simp [h] } /-- If we can partition a product into subsets that cancel out, then the whole product cancels. -/ @[to_additive] lemma prod_cancels_of_partition_cancels (R : setoid Ξ±) [decidable_rel R.r] (h : βˆ€ x ∈ s, (∏ a in s.filter (Ξ»y, y β‰ˆ x), f a) = 1) : (∏ x in s, f x) = 1 := begin suffices : ∏ xbar in s.image quotient.mk, ∏ y in s.filter (Ξ» y, ⟦y⟧ = xbar), f y = (∏ x in s, f x), { rw [←this, ←finset.prod_eq_one], intros xbar xbar_in_s, rcases (mem_image).mp xbar_in_s with ⟨x, x_in_s, xbar_eq_x⟩, rw [←xbar_eq_x, filter_congr (Ξ» y _, @quotient.eq _ R y x)], apply h x x_in_s }, apply finset.prod_image' f, intros, refl end @[to_additive] lemma prod_update_of_not_mem [decidable_eq Ξ±] {s : finset Ξ±} {i : Ξ±} (h : i βˆ‰ s) (f : Ξ± β†’ Ξ²) (b : Ξ²) : (∏ x in s, function.update f i b x) = (∏ x in s, f x) := begin apply prod_congr rfl (Ξ»j hj, _), have : j β‰  i, by { assume eq, rw eq at hj, exact h hj }, simp [this] end lemma prod_update_of_mem [decidable_eq Ξ±] {s : finset Ξ±} {i : Ξ±} (h : i ∈ s) (f : Ξ± β†’ Ξ²) (b : Ξ²) : (∏ x in s, function.update f i b x) = b * (∏ x in s \ (singleton i), f x) := by { rw [update_eq_piecewise, prod_piecewise], simp [h] } /-- If a product of a `finset` of size at most 1 has a given value, so do the terms in that product. -/ lemma eq_of_card_le_one_of_prod_eq {s : finset Ξ±} (hc : s.card ≀ 1) {f : Ξ± β†’ Ξ²} {b : Ξ²} (h : ∏ x in s, f x = b) : βˆ€ x ∈ s, f x = b := begin intros x hx, by_cases hc0 : s.card = 0, { exact false.elim (card_ne_zero_of_mem hx hc0) }, { have h1 : s.card = 1 := le_antisymm hc (nat.one_le_of_lt (nat.pos_of_ne_zero hc0)), rw card_eq_one at h1, cases h1 with x2 hx2, rw [hx2, mem_singleton] at hx, simp_rw hx2 at h, rw hx, rw prod_singleton at h, exact h } end /-- If a sum of a `finset` of size at most 1 has a given value, so do the terms in that sum. -/ lemma eq_of_card_le_one_of_sum_eq [add_comm_monoid Ξ³] {s : finset Ξ±} (hc : s.card ≀ 1) {f : Ξ± β†’ Ξ³} {b : Ξ³} (h : βˆ‘ x in s, f x = b) : βˆ€ x ∈ s, f x = b := begin intros x hx, by_cases hc0 : s.card = 0, { exact false.elim (card_ne_zero_of_mem hx hc0) }, { have h1 : s.card = 1 := le_antisymm hc (nat.one_le_of_lt (nat.pos_of_ne_zero hc0)), rw card_eq_one at h1, cases h1 with x2 hx2, rw [hx2, mem_singleton] at hx, simp_rw hx2 at h, rw hx, rw sum_singleton at h, exact h } end attribute [to_additive eq_of_card_le_one_of_sum_eq] eq_of_card_le_one_of_prod_eq /-- If a function applied at a point is 1, a product is unchanged by removing that point, if present, from a `finset`. -/ @[to_additive "If a function applied at a point is 0, a sum is unchanged by removing that point, if present, from a `finset`."] lemma prod_erase [decidable_eq Ξ±] (s : finset Ξ±) {f : Ξ± β†’ Ξ²} {a : Ξ±} (h : f a = 1) : ∏ x in s.erase a, f x = ∏ x in s, f x := begin rw ←sdiff_singleton_eq_erase, apply prod_subset sdiff_subset_self, intros x hx hnx, rw sdiff_singleton_eq_erase at hnx, rwa eq_of_mem_of_not_mem_erase hx hnx end /-- If a product is 1 and the function is 1 except possibly at one point, it is 1 everywhere on the `finset`. -/ @[to_additive "If a sum is 0 and the function is 0 except possibly at one point, it is 0 everywhere on the `finset`."] lemma eq_one_of_prod_eq_one {s : finset Ξ±} {f : Ξ± β†’ Ξ²} {a : Ξ±} (hp : ∏ x in s, f x = 1) (h1 : βˆ€ x ∈ s, x β‰  a β†’ f x = 1) : βˆ€ x ∈ s, f x = 1 := begin intros x hx, classical, by_cases h : x = a, { rw h, rw h at hx, rw [←prod_subset (singleton_subset_iff.2 hx) (Ξ» t ht ha, h1 t ht (not_mem_singleton.1 ha)), prod_singleton] at hp, exact hp }, { exact h1 x hx h } end lemma prod_pow_boole [decidable_eq Ξ±] (s : finset Ξ±) (f : Ξ± β†’ Ξ²) (a : Ξ±) : (∏ x in s, (f x)^(ite (a = x) 1 0)) = ite (a ∈ s) (f a) 1 := by simp end comm_monoid /-- If `f = g = h` everywhere but at `i`, where `f i = g i + h i`, then the product of `f` over `s` is the sum of the products of `g` and `h`. -/ lemma prod_add_prod_eq [comm_semiring Ξ²] {s : finset Ξ±} {i : Ξ±} {f g h : Ξ± β†’ Ξ²} (hi : i ∈ s) (h1 : g i + h i = f i) (h2 : βˆ€ j ∈ s, j β‰  i β†’ g j = f j) (h3 : βˆ€ j ∈ s, j β‰  i β†’ h j = f j) : ∏ i in s, g i + ∏ i in s, h i = ∏ i in s, f i := by { classical, simp_rw [← mul_prod_diff_singleton hi, ← h1, right_distrib], congr' 2; apply prod_congr rfl; simpa } lemma sum_update_of_mem [add_comm_monoid Ξ²] [decidable_eq Ξ±] {s : finset Ξ±} {i : Ξ±} (h : i ∈ s) (f : Ξ± β†’ Ξ²) (b : Ξ²) : (βˆ‘ x in s, function.update f i b x) = b + (βˆ‘ x in s \ (singleton i), f x) := by { rw [update_eq_piecewise, sum_piecewise], simp [h] } attribute [to_additive] prod_update_of_mem lemma sum_nsmul [add_comm_monoid Ξ²] (s : finset Ξ±) (n : β„•) (f : Ξ± β†’ Ξ²) : (βˆ‘ x in s, n β€’β„• (f x)) = n β€’β„• ((βˆ‘ x in s, f x)) := @prod_pow _ (multiplicative Ξ²) _ _ _ _ attribute [to_additive sum_nsmul] prod_pow @[simp] lemma sum_const [add_comm_monoid Ξ²] (b : Ξ²) : (βˆ‘ x in s, b) = s.card β€’β„• b := @prod_const _ (multiplicative Ξ²) _ _ _ attribute [to_additive] prod_const lemma card_eq_sum_ones (s : finset Ξ±) : s.card = βˆ‘ _ in s, 1 := by simp lemma sum_const_nat {m : β„•} {f : Ξ± β†’ β„•} (h₁ : βˆ€x ∈ s, f x = m) : (βˆ‘ x in s, f x) = card s * m := begin rw [← nat.nsmul_eq_mul, ← sum_const], apply sum_congr rfl h₁ end @[simp] lemma sum_boole {s : finset Ξ±} {p : Ξ± β†’ Prop} [semiring Ξ²] {hp : decidable_pred p} : (βˆ‘ x in s, if p x then (1 : Ξ²) else (0 : Ξ²)) = (s.filter p).card := by simp [sum_ite] @[norm_cast] lemma sum_nat_cast [add_comm_monoid Ξ²] [has_one Ξ²] (s : finset Ξ±) (f : Ξ± β†’ β„•) : ↑(βˆ‘ x in s, f x : β„•) = (βˆ‘ x in s, (f x : Ξ²)) := (nat.cast_add_monoid_hom Ξ²).map_sum f s lemma sum_comp [add_comm_monoid Ξ²] [decidable_eq Ξ³] {s : finset Ξ±} (f : Ξ³ β†’ Ξ²) (g : Ξ± β†’ Ξ³) : βˆ‘ a in s, f (g a) = βˆ‘ b in s.image g, (s.filter (Ξ» a, g a = b)).card β€’β„• (f b) := @prod_comp _ (multiplicative Ξ²) _ _ _ _ _ _ attribute [to_additive "The sum of the composition of functions `f` and `g`, is the sum over `b ∈ s.image g` of `f b` times of the cardinality of the fibre of `b`"] prod_comp lemma sum_range_succ' [add_comm_monoid Ξ²] (f : β„• β†’ Ξ²) : βˆ€ n : β„•, (βˆ‘ i in range (n + 1), f i) = (βˆ‘ i in range n, f (i + 1)) + f 0 := @prod_range_succ' (multiplicative Ξ²) _ _ attribute [to_additive] prod_range_succ' lemma sum_flip [add_comm_monoid Ξ²] {n : β„•} (f : β„• β†’ Ξ²) : (βˆ‘ i in range (n + 1), f (n - i)) = (βˆ‘ i in range (n + 1), f i) := @prod_flip (multiplicative Ξ²) _ _ _ attribute [to_additive] prod_flip section comm_group variables [comm_group Ξ²] @[simp, to_additive] lemma prod_inv_distrib : (∏ x in s, (f x)⁻¹) = (∏ x in s, f x)⁻¹ := s.prod_hom has_inv.inv end comm_group @[simp] theorem card_sigma {Οƒ : Ξ± β†’ Type*} (s : finset Ξ±) (t : Ξ  a, finset (Οƒ a)) : card (s.sigma t) = βˆ‘ a in s, card (t a) := multiset.card_sigma _ _ lemma card_bind [decidable_eq Ξ²] {s : finset Ξ±} {t : Ξ± β†’ finset Ξ²} (h : βˆ€ x ∈ s, βˆ€ y ∈ s, x β‰  y β†’ disjoint (t x) (t y)) : (s.bind t).card = βˆ‘ u in s, card (t u) := calc (s.bind t).card = βˆ‘ i in s.bind t, 1 : by simp ... = βˆ‘ a in s, βˆ‘ i in t a, 1 : finset.sum_bind h ... = βˆ‘ u in s, card (t u) : by simp lemma card_bind_le [decidable_eq Ξ²] {s : finset Ξ±} {t : Ξ± β†’ finset Ξ²} : (s.bind t).card ≀ βˆ‘ a in s, (t a).card := by haveI := classical.dec_eq Ξ±; exact finset.induction_on s (by simp) (Ξ» a s has ih, calc ((insert a s).bind t).card ≀ (t a).card + (s.bind t).card : by rw bind_insert; exact finset.card_union_le _ _ ... ≀ βˆ‘ a in insert a s, card (t a) : by rw sum_insert has; exact add_le_add_left ih _) theorem card_eq_sum_card_fiberwise [decidable_eq Ξ²] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} {t : finset Ξ²} (H : βˆ€ x ∈ s, f x ∈ t) : s.card = βˆ‘ a in t, (s.filter (Ξ» x, f x = a)).card := by simp only [card_eq_sum_ones, sum_fiberwise_of_maps_to H] theorem card_eq_sum_card_image [decidable_eq Ξ²] (f : Ξ± β†’ Ξ²) (s : finset Ξ±) : s.card = βˆ‘ a in s.image f, (s.filter (Ξ» x, f x = a)).card := card_eq_sum_card_fiberwise (Ξ» _, mem_image_of_mem _) lemma gsmul_sum [add_comm_group Ξ²] {f : Ξ± β†’ Ξ²} {s : finset Ξ±} (z : β„€) : gsmul z (βˆ‘ a in s, f a) = βˆ‘ a in s, gsmul z (f a) := (s.sum_hom (gsmul z)).symm @[simp] lemma sum_sub_distrib [add_comm_group Ξ²] : βˆ‘ x in s, (f x - g x) = (βˆ‘ x in s, f x) - (βˆ‘ x in s, g x) := sum_add_distrib.trans $ congr_arg _ sum_neg_distrib section prod_eq_zero variables [comm_monoid_with_zero Ξ²] lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : (∏ x in s, f x) = 0 := by haveI := classical.dec_eq Ξ±; calc (∏ x in s, f x) = ∏ x in insert a (erase s a), f x : by rw insert_erase ha ... = 0 : by rw [prod_insert (not_mem_erase _ _), h, zero_mul] lemma prod_boole {s : finset Ξ±} {p : Ξ± β†’ Prop} [decidable_pred p] : ∏ i in s, ite (p i) (1 : Ξ²) (0 : Ξ²) = ite (βˆ€ i ∈ s, p i) 1 0 := begin split_ifs, { apply prod_eq_one, intros i hi, rw if_pos (h i hi) }, { push_neg at h, rcases h with ⟨i, hi, hq⟩, apply prod_eq_zero hi, rw [if_neg hq] }, end variables [nontrivial Ξ²] [no_zero_divisors Ξ²] lemma prod_eq_zero_iff : (∏ x in s, f x) = 0 ↔ (βˆƒa∈s, f a = 0) := begin classical, apply finset.induction_on s, exact ⟨not.elim one_ne_zero, Ξ» ⟨_, H, _⟩, H.elim⟩, assume a s ha ih, rw [prod_insert ha, mul_eq_zero, bex_def, exists_mem_insert, ih, ← bex_def] end theorem prod_ne_zero_iff : (∏ x in s, f x) β‰  0 ↔ (βˆ€ a ∈ s, f a β‰  0) := by { rw [ne, prod_eq_zero_iff], push_neg } end prod_eq_zero section comm_group_with_zero variables [comm_group_with_zero Ξ²] @[simp] lemma prod_inv_distrib' : (∏ x in s, (f x)⁻¹) = (∏ x in s, f x)⁻¹ := begin classical, by_cases h : βˆƒ x ∈ s, f x = 0, { simpa [prod_eq_zero_iff.mpr h, prod_eq_zero_iff] using h }, { push_neg at h, have h' := prod_ne_zero_iff.mpr h, have hf : βˆ€ x ∈ s, (f x)⁻¹ * f x = 1 := Ξ» x hx, inv_mul_cancel (h x hx), apply mul_right_cancel' h', simp [h, h', ← finset.prod_mul_distrib, prod_congr rfl hf] } end end comm_group_with_zero end finset namespace list @[to_additive] lemma prod_to_finset {M : Type*} [decidable_eq Ξ±] [comm_monoid M] (f : Ξ± β†’ M) : βˆ€ {l : list Ξ±} (hl : l.nodup), l.to_finset.prod f = (l.map f).prod | [] _ := by simp | (a :: l) hl := let ⟨not_mem, hl⟩ := list.nodup_cons.mp hl in by simp [finset.prod_insert (mt list.mem_to_finset.mp not_mem), prod_to_finset hl] end list namespace multiset variables [decidable_eq Ξ±] @[simp] lemma to_finset_sum_count_eq (s : multiset Ξ±) : (βˆ‘ a in s.to_finset, s.count a) = s.card := multiset.induction_on s rfl (assume a s ih, calc (βˆ‘ x in to_finset (a ::β‚˜ s), count x (a ::β‚˜ s)) = βˆ‘ x in to_finset (a ::β‚˜ s), ((if x = a then 1 else 0) + count x s) : finset.sum_congr rfl $ Ξ» _ _, by split_ifs; [simp only [h, count_cons_self, nat.one_add], simp only [count_cons_of_ne h, zero_add]] ... = card (a ::β‚˜ s) : begin by_cases a ∈ s.to_finset, { have : βˆ‘ x in s.to_finset, ite (x = a) 1 0 = βˆ‘ x in {a}, ite (x = a) 1 0, { rw [finset.sum_ite_eq', if_pos h, finset.sum_singleton, if_pos rfl], }, rw [to_finset_cons, finset.insert_eq_of_mem h, finset.sum_add_distrib, ih, this, finset.sum_singleton, if_pos rfl, add_comm, card_cons] }, { have ha : a βˆ‰ s, by rwa mem_to_finset at h, have : βˆ‘ x in to_finset s, ite (x = a) 1 0 = βˆ‘ x in to_finset s, 0, from finset.sum_congr rfl (Ξ» x hx, if_neg $ by rintro rfl; cc), rw [to_finset_cons, finset.sum_insert h, if_pos rfl, finset.sum_add_distrib, this, finset.sum_const_zero, ih, count_eq_zero_of_not_mem ha, zero_add, add_comm, card_cons] } end) lemma count_sum' {s : finset Ξ²} {a : Ξ±} {f : Ξ² β†’ multiset Ξ±} : count a (βˆ‘ x in s, f x) = βˆ‘ x in s, count a (f x) := by { dunfold finset.sum, rw count_sum } lemma to_finset_sum_count_smul_eq (s : multiset Ξ±) : (βˆ‘ a in s.to_finset, s.count a β€’β„• (a ::β‚˜ 0)) = s := begin apply ext', intro b, rw count_sum', have h : count b s = count b (count b s β€’β„• (b ::β‚˜ 0)), { rw [singleton_coe, count_smul, ← singleton_coe, count_singleton, mul_one] }, rw h, clear h, apply finset.sum_eq_single b, { intros c h hcb, rw count_smul, convert mul_zero (count c s), apply count_eq_zero.mpr, exact finset.not_mem_singleton.mpr (ne.symm hcb) }, { intro hb, rw [count_eq_zero_of_not_mem (mt mem_to_finset.2 hb), count_smul, zero_mul]} end theorem exists_smul_of_dvd_count (s : multiset Ξ±) {k : β„•} (h : βˆ€ (a : Ξ±), k ∣ multiset.count a s) : βˆƒ (u : multiset Ξ±), s = k β€’β„• u := begin use βˆ‘ a in s.to_finset, (s.count a / k) β€’β„• (a ::β‚˜ 0), have hβ‚‚ : βˆ‘ (x : Ξ±) in s.to_finset, k β€’β„• (count x s / k β€’β„• (x ::β‚˜ 0)) = βˆ‘ (x : Ξ±) in s.to_finset, count x s β€’β„• (x ::β‚˜ 0), { refine congr_arg s.to_finset.sum _, apply funext, intro x, rw [← mul_nsmul, nat.mul_div_cancel' (h x)] }, rw [← finset.sum_nsmul, hβ‚‚, to_finset_sum_count_smul_eq] end end multiset @[simp, norm_cast] lemma nat.coe_prod {R : Type*} [comm_semiring R] (f : Ξ± β†’ β„•) (s : finset Ξ±) : (β†‘βˆ i in s, f i : R) = ∏ i in s, f i := (nat.cast_ring_hom R).map_prod _ _ @[simp, norm_cast] lemma int.coe_prod {R : Type*} [comm_ring R] (f : Ξ± β†’ β„€) (s : finset Ξ±) : (β†‘βˆ i in s, f i : R) = ∏ i in s, f i := (int.cast_ring_hom R).map_prod _ _ @[simp, norm_cast] lemma units.coe_prod {M : Type*} [comm_monoid M] (f : Ξ± β†’ units M) (s : finset Ξ±) : (β†‘βˆ i in s, f i : M) = ∏ i in s, f i := (units.coe_hom M).map_prod _ _
dc5ead92aeabc84493a4107572a3a336c6ea9b61
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/run/def13.lean
933a7340583d1d79e5ac91fb40d11fb3feac6976
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
611
lean
variables {Ξ±} (p : Ξ± β†’ Prop) [DecidablePred p] def filter : List Ξ± β†’ List Ξ± | [] => [] | a::as => if p a then a :: filter as else filter as theorem filter_nil : filter p [] = [] := rfl theorem filter_cons (a : Ξ±) (as : List Ξ±) : filter p (a :: as) = if p a then a :: filter p as else filter p as := rfl theorem filter_cons_of_pos {a : Ξ±} (as : List Ξ±) (h : p a) : filter p (a :: as) = a :: filter p as := by rw filter_cons; rw ifPos h; exact rfl theorem filter_cons_of_neg {a : Ξ±} (as : List Ξ±) (h : Β¬ p a) : filter p (a :: as) = filter p as := by rw filter_cons; rw ifNeg h; exact rfl