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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.