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
83f1ada3f002a33b6a7124094a6ea09da3769b74
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/algebra/algebra/basic.lean
bea860c9921cd1c30919e52660a921632bbb9880
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
56,580
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import tactic.nth_rewrite import data.matrix.basic import data.equiv.ring_aut import linear_algebra.tensor_product import ring_theory.subring import deprecated.subring import algebra.opposites /-! # Algebra over Commutative Semiring In this file we define `algebra`s over commutative (semi)rings, algebra homomorphisms `alg_hom`, algebra equivalences `alg_equiv`. We also define usual operations on `alg_hom`s (`id`, `comp`). `subalgebra`s are defined in `algebra.algebra.subalgebra`. If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now deprecated and replaced with `is_scalar_tower`. For the category of `R`-algebras, denoted `Algebra R`, see the file `algebra/category/Algebra/basic.lean`. ## Notations * `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`. * `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`. -/ universes u v w u₁ v₁ open_locale tensor_product big_operators section prio -- We set this priority to 0 later in this file set_option extends_priority 200 /- control priority of `instance [algebra R A] : has_scalar R A` -/ /-- Given a commutative (semi)ring `R`, an `R`-algebra is a (possibly noncommutative) (semi)ring `A` endowed with a morphism of rings `R →+* A` which lands in the center of `A`. For convenience, this typeclass extends `has_scalar R A` where the scalar action must agree with left multiplication by the image of the structure morphism. Given an `algebra R A` instance, the structure morphism `R →+* A` is denoted `algebra_map R A`. -/ @[nolint has_inhabited_instance] class algebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A] extends has_scalar R A, R →+* A := (commutes' : ∀ r x, to_fun r * x = x * to_fun r) (smul_def' : ∀ r x, r • x = to_fun r * x) end prio /-- Embedding `R →+* A` given by `algebra` structure. -/ def algebra_map (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] : R →+* A := algebra.to_ring_hom /-- Creating an algebra from a morphism to the center of a semiring. -/ def ring_hom.to_algebra' {R S} [comm_semiring R] [semiring S] (i : R →+* S) (h : ∀ c x, i c * x = x * i c) : algebra R S := { smul := λ c x, i c * x, commutes' := h, smul_def' := λ c x, rfl, to_ring_hom := i} /-- Creating an algebra from a morphism to a commutative semiring. -/ def ring_hom.to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) : algebra R S := i.to_algebra' $ λ _, mul_comm _ lemma ring_hom.algebra_map_to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) : @algebra_map R S _ _ i.to_algebra = i := rfl namespace algebra variables {R : Type u} {S : Type v} {A : Type w} {B : Type*} /-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure. If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `algebra` over `R`. -/ def of_semimodule' [comm_semiring R] [semiring A] [semimodule R A] (h₁ : ∀ (r : R) (x : A), (r • 1) * x = r • x) (h₂ : ∀ (r : R) (x : A), x * (r • 1) = r • x) : algebra R A := { to_fun := λ r, r • 1, map_one' := one_smul _ _, map_mul' := λ r₁ r₂, by rw [h₁, mul_smul], map_zero' := zero_smul _ _, map_add' := λ r₁ r₂, add_smul r₁ r₂ 1, commutes' := λ r x, by simp only [h₁, h₂], smul_def' := λ r x, by simp only [h₁] } /-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure. If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A` is an `algebra` over `R`. -/ def of_semimodule [comm_semiring R] [semiring A] [semimodule R A] (h₁ : ∀ (r : R) (x y : A), (r • x) * y = r • (x * y)) (h₂ : ∀ (r : R) (x y : A), x * (r • y) = r • (x * y)) : algebra R A := of_semimodule' (λ r x, by rw [h₁, one_mul]) (λ r x, by rw [h₂, mul_one]) section semiring variables [comm_semiring R] [comm_semiring S] variables [semiring A] [algebra R A] [semiring B] [algebra R B] lemma smul_def'' (r : R) (x : A) : r • x = algebra_map R A r * x := algebra.smul_def' r x /-- To prove two algebra structures on a fixed `[comm_semiring R] [semiring A]` agree, it suffices to check the `algebra_map`s agree. -/ -- We'll later use this to show `algebra ℤ M` is a subsingleton. @[ext] lemma algebra_ext {R : Type*} [comm_semiring R] {A : Type*} [semiring A] (P Q : algebra R A) (w : ∀ (r : R), by { haveI := P, exact algebra_map R A r } = by { haveI := Q, exact algebra_map R A r }) : P = Q := begin unfreezingI { rcases P with ⟨⟨P⟩⟩, rcases Q with ⟨⟨Q⟩⟩ }, congr, { funext r a, replace w := congr_arg (λ s, s * a) (w r), simp only [←algebra.smul_def''] at w, apply w, }, { ext r, exact w r, }, { apply proof_irrel_heq, }, { apply proof_irrel_heq, }, end @[priority 200] -- see Note [lower instance priority] instance to_semimodule : semimodule R A := { one_smul := by simp [smul_def''], mul_smul := by simp [smul_def'', mul_assoc], smul_add := by simp [smul_def'', mul_add], smul_zero := by simp [smul_def''], add_smul := by simp [smul_def'', add_mul], zero_smul := by simp [smul_def''] } -- from now on, we don't want to use the following instance anymore attribute [instance, priority 0] algebra.to_has_scalar lemma smul_def (r : R) (x : A) : r • x = algebra_map R A r * x := algebra.smul_def' r x lemma algebra_map_eq_smul_one (r : R) : algebra_map R A r = r • 1 := calc algebra_map R A r = algebra_map R A r * 1 : (mul_one _).symm ... = r • 1 : (algebra.smul_def r 1).symm lemma algebra_map_eq_smul_one' : ⇑(algebra_map R A) = λ r, r • (1 : A) := funext algebra_map_eq_smul_one theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r := algebra.commutes' r x theorem left_comm (r : R) (x y : A) : x * (algebra_map R A r * y) = algebra_map R A r * (x * y) := by rw [← mul_assoc, ← commutes, mul_assoc] @[simp] lemma mul_smul_comm (s : R) (x y : A) : x * (s • y) = s • (x * y) := by rw [smul_def, smul_def, left_comm] @[simp] lemma smul_mul_assoc (r : R) (x y : A) : (r • x) * y = r • (x * y) := by rw [smul_def, smul_def, mul_assoc] lemma smul_mul_smul (r s : R) (x y : A) : (r • x) * (s • y) = (r * s) • (x * y) := by rw [algebra.smul_mul_assoc, algebra.mul_smul_comm, smul_smul] section variables {r : R} {a : A} @[simp] lemma bit0_smul_one : bit0 r • (1 : A) = r • 2 := by simp [bit0, add_smul, smul_add] @[simp] lemma bit0_smul_bit0 : bit0 r • bit0 a = r • (bit0 (bit0 a)) := by simp [bit0, add_smul, smul_add] @[simp] lemma bit0_smul_bit1 : bit0 r • bit1 a = r • (bit0 (bit1 a)) := by simp [bit0, add_smul, smul_add] @[simp] lemma bit1_smul_one : bit1 r • (1 : A) = r • 2 + 1 := by simp [bit1, add_smul, smul_add] @[simp] lemma bit1_smul_bit0 : bit1 r • bit0 a = r • (bit0 (bit0 a)) + bit0 a := by simp [bit1, add_smul, smul_add] @[simp] lemma bit1_smul_bit1 : bit1 r • bit1 a = r • (bit0 (bit1 a)) + bit1 a := by { simp only [bit0, bit1, add_smul, smul_add, one_smul], abel } end variables (R A) /-- The canonical ring homomorphism `algebra_map R A : R →* A` for any `R`-algebra `A`, packaged as an `R`-linear map. -/ protected def linear_map : R →ₗ[R] A := { map_smul' := λ x y, by simp [algebra.smul_def], ..algebra_map R A } @[simp] lemma linear_map_apply (r : R) : algebra.linear_map R A r = algebra_map R A r := rfl instance id : algebra R R := (ring_hom.id R).to_algebra variables {R A} namespace id @[simp] lemma map_eq_self (x : R) : algebra_map R R x = x := rfl @[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl end id section prod variables (R A B) instance : algebra R (A × B) := { commutes' := by { rintro r ⟨a, b⟩, dsimp, rw [commutes r a, commutes r b] }, smul_def' := by { rintro r ⟨a, b⟩, dsimp, rw [smul_def r a, smul_def r b] }, .. prod.semimodule, .. ring_hom.prod (algebra_map R A) (algebra_map R B) } variables {R A B} @[simp] lemma algebra_map_prod_apply (r : R) : algebra_map R (A × B) r = (algebra_map R A r, algebra_map R B r) := rfl end prod /-- Algebra over a subsemiring. -/ instance of_subsemiring (S : subsemiring R) : algebra S A := { smul := λ s x, (s : R) • x, commutes' := λ r x, algebra.commutes r x, smul_def' := λ r x, algebra.smul_def r x, .. (algebra_map R A).comp (subsemiring.subtype S) } /-- Algebra over a subring. -/ instance of_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] (S : subring R) : algebra S A := { smul := λ s x, (s : R) • x, commutes' := λ r x, algebra.commutes r x, smul_def' := λ r x, algebra.smul_def r x, .. (algebra_map R A).comp (subring.subtype S) } lemma algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) : (algebra_map S R : S →+* R) = subring.subtype S := rfl lemma coe_algebra_map_of_subring {R : Type*} [comm_ring R] (S : subring R) : (algebra_map S R : S → R) = subtype.val := rfl lemma algebra_map_of_subring_apply {R : Type*} [comm_ring R] (S : subring R) (x : S) : algebra_map S R x = x := rfl section local attribute [instance] subset.comm_ring /-- Algebra over a set that is closed under the ring operations. -/ local attribute [instance] def of_is_subring {R A : Type*} [comm_ring R] [ring A] [algebra R A] (S : set R) [is_subring S] : algebra S A := algebra.of_subring S.to_subring lemma is_subring_coe_algebra_map_hom {R : Type*} [comm_ring R] (S : set R) [is_subring S] : (algebra_map S R : S →+* R) = is_subring.subtype S := rfl lemma is_subring_coe_algebra_map {R : Type*} [comm_ring R] (S : set R) [is_subring S] : (algebra_map S R : S → R) = subtype.val := rfl lemma is_subring_algebra_map_apply {R : Type*} [comm_ring R] (S : set R) [is_subring S] (x : S) : algebra_map S R x = x := rfl lemma set_range_subset {R : Type*} [comm_ring R] {T₁ T₂ : set R} [is_subring T₁] (hyp : T₁ ⊆ T₂) : set.range (algebra_map T₁ R) ⊆ T₂ := begin rintros x ⟨⟨t, ht⟩, rfl⟩, exact hyp ht, end end /-- Explicit characterization of the submonoid map in the case of an algebra. `S` is made explicit to help with type inference -/ def algebra_map_submonoid (S : Type*) [semiring S] [algebra R S] (M : submonoid R) : (submonoid S) := submonoid.map (algebra_map R S : R →* S) M lemma mem_algebra_map_submonoid_of_mem [algebra R S] {M : submonoid R} (x : M) : (algebra_map R S x) ∈ algebra_map_submonoid S M := set.mem_image_of_mem (algebra_map R S) x.2 end semiring section ring variables [comm_ring R] variables (R) /-- A `semiring` that is an `algebra` over a commutative ring carries a natural `ring` structure. -/ def semiring_to_ring [semiring A] [algebra R A] : ring A := { ..semimodule.add_comm_monoid_to_add_comm_group R, ..(infer_instance : semiring A) } variables {R} lemma mul_sub_algebra_map_commutes [ring A] [algebra R A] (x : A) (r : R) : x * (x - algebra_map R A r) = (x - algebra_map R A r) * x := by rw [mul_sub, ←commutes, sub_mul] lemma mul_sub_algebra_map_pow_commutes [ring A] [algebra R A] (x : A) (r : R) (n : ℕ) : x * (x - algebra_map R A r) ^ n = (x - algebra_map R A r) ^ n * x := begin induction n with n ih, { simp }, { rw [pow_succ, ←mul_assoc, mul_sub_algebra_map_commutes, mul_assoc, ih, ←mul_assoc], } end /-- If `algebra_map R A` is injective and `A` has no zero divisors, `R`-multiples in `A` are zero only if one of the factors is zero. Cannot be an instance because there is no `injective (algebra_map R A)` typeclass. -/ lemma no_zero_smul_divisors.of_algebra_map_injective [semiring A] [algebra R A] [no_zero_divisors A] (h : function.injective (algebra_map R A)) : no_zero_smul_divisors R A := ⟨λ c x hcx, (mul_eq_zero.mp ((smul_def c x).symm.trans hcx)).imp_left ((algebra_map R A).injective_iff.mp h _)⟩ end ring section field variables [field R] [semiring A] [algebra R A] @[priority 100] -- see note [lower instance priority] instance [nontrivial A] [no_zero_divisors A] : no_zero_smul_divisors R A := no_zero_smul_divisors.of_algebra_map_injective (algebra_map R A).injective end field end algebra namespace opposite variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] instance : algebra R Aᵒᵖ := { to_ring_hom := (algebra_map R A).to_opposite $ λ x y, algebra.commutes _ _, smul_def' := λ c x, unop_injective $ by { dsimp, simp only [op_mul, algebra.smul_def, algebra.commutes, op_unop] }, commutes' := λ r, op_induction $ λ x, by dsimp; simp only [← op_mul, algebra.commutes], ..opposite.has_scalar A R } @[simp] lemma algebra_map_apply (c : R) : algebra_map R Aᵒᵖ c = op (algebra_map R A c) := rfl end opposite namespace module variables (R : Type u) (M : Type v) [comm_semiring R] [add_comm_monoid M] [semimodule R M] instance endomorphism_algebra : algebra R (M →ₗ[R] M) := { to_fun := λ r, r • linear_map.id, map_one' := one_smul _ _, map_zero' := zero_smul _ _, map_add' := λ r₁ r₂, add_smul _ _ _, map_mul' := λ r₁ r₂, by { ext x, simp [mul_smul] }, commutes' := by { intros, ext, simp }, smul_def' := by { intros, ext, simp } } lemma algebra_map_End_eq_smul_id (a : R) : (algebra_map R (End R M)) a = a • linear_map.id := rfl @[simp] lemma algebra_map_End_apply (a : R) (m : M) : (algebra_map R (End R M)) a m = a • m := rfl @[simp] lemma ker_algebra_map_End (K : Type u) (V : Type v) [field K] [add_comm_group V] [vector_space K V] (a : K) (ha : a ≠ 0) : ((algebra_map K (End K V)) a).ker = ⊥ := linear_map.ker_smul _ _ ha end module instance matrix_algebra (n : Type u) (R : Type v) [decidable_eq n] [fintype n] [comm_semiring R] : algebra R (matrix n n R) := { commutes' := by { intros, simp [matrix.scalar], }, smul_def' := by { intros, simp [matrix.scalar], }, ..(matrix.scalar n) } @[simp] lemma matrix.algebra_map_eq_smul (n : Type u) {R : Type v} [decidable_eq n] [fintype n] [comm_semiring R] (r : R) : (algebra_map R (matrix n n R)) r = r • 1 := rfl set_option old_structure_cmd true /-- Defining the homomorphism in the category R-Alg. -/ @[nolint has_inhabited_instance] structure alg_hom (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_hom A B := (commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r) run_cmd tactic.add_doc_string `alg_hom.to_ring_hom "Reinterpret an `alg_hom` as a `ring_hom`" infixr ` →ₐ `:25 := alg_hom _ notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁} section semiring variables [comm_semiring R] [semiring A] [semiring B] [semiring C] [semiring D] variables [algebra R A] [algebra R B] [algebra R C] [algebra R D] instance : has_coe_to_fun (A →ₐ[R] B) := ⟨_, λ f, f.to_fun⟩ initialize_simps_projections alg_hom (to_fun → apply) @[simp] lemma to_fun_eq_coe (f : A →ₐ[R] B) : f.to_fun = f := rfl instance coe_ring_hom : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩ instance coe_monoid_hom : has_coe (A →ₐ[R] B) (A →* B) := ⟨λ f, ↑(f : A →+* B)⟩ instance coe_add_monoid_hom : has_coe (A →ₐ[R] B) (A →+ B) := ⟨λ f, ↑(f : A →+* B)⟩ @[simp, norm_cast] lemma coe_mk {f : A → B} (h₁ h₂ h₃ h₄ h₅) : ⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := rfl @[simp, norm_cast] lemma coe_to_ring_hom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl -- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute. @[norm_cast] lemma coe_to_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl -- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute. @[norm_cast] lemma coe_to_add_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl variables (φ : A →ₐ[R] B) theorem coe_fn_inj ⦃φ₁ φ₂ : A →ₐ[R] B⦄ (H : ⇑φ₁ = φ₂) : φ₁ = φ₂ := by { cases φ₁, cases φ₂, congr, exact H } theorem coe_ring_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) := λ φ₁ φ₂ H, coe_fn_inj $ show ((φ₁ : (A →+* B)) : A → B) = ((φ₂ : (A →+* B)) : A → B), from congr_arg _ H theorem coe_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →* B)) := ring_hom.coe_monoid_hom_injective.comp coe_ring_hom_injective theorem coe_add_monoid_hom_injective : function.injective (coe : (A →ₐ[R] B) → (A →+ B)) := ring_hom.coe_add_monoid_hom_injective.comp coe_ring_hom_injective protected lemma congr_fun {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁ = φ₂) (x : A) : φ₁ x = φ₂ x := H ▸ rfl protected lemma congr_arg (φ : A →ₐ[R] B) {x y : A} (h : x = y) : φ x = φ y := h ▸ rfl @[ext] theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ := coe_fn_inj $ funext H theorem ext_iff {φ₁ φ₂ : A →ₐ[R] B} : φ₁ = φ₂ ↔ ∀ x, φ₁ x = φ₂ x := ⟨alg_hom.congr_fun, ext⟩ @[simp] theorem mk_coe {f : A →ₐ[R] B} (h₁ h₂ h₃ h₄ h₅) : (⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := ext $ λ _, rfl @[simp] theorem commutes (r : R) : φ (algebra_map R A r) = algebra_map R B r := φ.commutes' r theorem comp_algebra_map : (φ : A →+* B).comp (algebra_map R A) = algebra_map R B := ring_hom.ext $ φ.commutes @[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s := φ.to_ring_hom.map_add r s @[simp] lemma map_zero : φ 0 = 0 := φ.to_ring_hom.map_zero @[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y := φ.to_ring_hom.map_mul x y @[simp] lemma map_one : φ 1 = 1 := φ.to_ring_hom.map_one @[simp] lemma map_smul (r : R) (x : A) : φ (r • x) = r • φ x := by simp only [algebra.smul_def, map_mul, commutes] @[simp] lemma map_pow (x : A) (n : ℕ) : φ (x ^ n) = (φ x) ^ n := φ.to_ring_hom.map_pow x n lemma map_sum {ι : Type*} (f : ι → A) (s : finset ι) : φ (∑ x in s, f x) = ∑ x in s, φ (f x) := φ.to_ring_hom.map_sum f s lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.sum g) = f.sum (λ i a, φ (g i a)) := φ.map_sum _ _ @[simp] lemma map_nat_cast (n : ℕ) : φ n = n := φ.to_ring_hom.map_nat_cast n @[simp] lemma map_bit0 (x) : φ (bit0 x) = bit0 (φ x) := φ.to_ring_hom.map_bit0 x @[simp] lemma map_bit1 (x) : φ (bit1 x) = bit1 (φ x) := φ.to_ring_hom.map_bit1 x /-- If a `ring_hom` is `R`-linear, then it is an `alg_hom`. -/ def mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : A →ₐ[R] B := { to_fun := f, commutes' := λ c, by simp only [algebra.algebra_map_eq_smul_one, h, f.map_one], .. f } @[simp] lemma coe_mk' (f : A →+* B) (h : ∀ (c : R) x, f (c • x) = c • f x) : ⇑(mk' f h) = f := rfl section variables (R A) /-- Identity map as an `alg_hom`. -/ protected def id : A →ₐ[R] A := { commutes' := λ _, rfl, ..ring_hom.id A } end @[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl /-- Composition of algebra homeomorphisms. -/ def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C := { commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl, .. φ₁.to_ring_hom.comp ↑φ₂ } @[simp] lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl @[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ := ext $ λ x, rfl @[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ := ext $ λ x, rfl theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) : (φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) := ext $ λ x, rfl /-- R-Alg ⥤ R-Mod -/ def to_linear_map : A →ₗ B := { to_fun := φ, map_add' := φ.map_add, map_smul' := φ.map_smul } @[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ := ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H @[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [comm_semiring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) lemma map_prod {ι : Type*} (f : ι → A) (s : finset ι) : φ (∏ x in s, f x) = ∏ x in s, φ (f x) := φ.to_ring_hom.map_prod f s lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.prod g) = f.prod (λ i a, φ (g i a)) := φ.map_prod _ _ end comm_semiring section ring variables [comm_semiring R] [ring A] [ring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) @[simp] lemma map_neg (x) : φ (-x) = -φ x := φ.to_ring_hom.map_neg x @[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y := φ.to_ring_hom.map_sub x y @[simp] lemma map_int_cast (n : ℤ) : φ n = n := φ.to_ring_hom.map_int_cast n end ring section division_ring variables [comm_ring R] [division_ring A] [division_ring B] variables [algebra R A] [algebra R B] (φ : A →ₐ[R] B) @[simp] lemma map_inv (x) : φ (x⁻¹) = (φ x)⁻¹ := φ.to_ring_hom.map_inv x @[simp] lemma map_div (x y) : φ (x / y) = φ x / φ y := φ.to_ring_hom.map_div x y end division_ring theorem injective_iff {R A B : Type*} [comm_semiring R] [ring A] [semiring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) : function.injective f ↔ (∀ x, f x = 0 → x = 0) := ring_hom.injective_iff (f : A →+* B) end alg_hom set_option old_structure_cmd true /-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/ structure alg_equiv (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B := (commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r) attribute [nolint doc_blame] alg_equiv.to_ring_equiv attribute [nolint doc_blame] alg_equiv.to_equiv attribute [nolint doc_blame] alg_equiv.to_add_equiv attribute [nolint doc_blame] alg_equiv.to_mul_equiv notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A' namespace alg_equiv variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁} section semiring variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃] variables [algebra R A₁] [algebra R A₂] [algebra R A₃] variables (e : A₁ ≃ₐ[R] A₂) instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) := ⟨_, alg_equiv.to_fun⟩ @[ext] lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : 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 protected lemma congr_arg {f : A₁ ≃ₐ[R] A₂} : Π {x x' : A₁}, x = x' → f x = f x' | _ _ rfl := rfl protected lemma congr_fun {f g : A₁ ≃ₐ[R] A₂} (h : f = g) (x : A₁) : f x = g x := h ▸ rfl lemma ext_iff {f g : A₁ ≃ₐ[R] A₂} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, ext⟩ lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) := begin intros f g w, ext, exact congr_fun w a, end instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩ @[simp] lemma coe_mk {to_fun inv_fun left_inv right_inv map_mul map_add commutes} : ⇑(⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) = to_fun := rfl @[simp] theorem mk_coe (e : A₁ ≃ₐ[R] A₂) (e' h₁ h₂ h₃ h₄ h₅) : (⟨e, e', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂) = e := ext $ λ _, rfl @[simp] lemma to_fun_eq_coe (e : A₁ ≃ₐ[R] A₂) : e.to_fun = e := rfl -- TODO: decide on a simp-normal form so that only one of these two lemmas is needed @[simp, norm_cast] lemma coe_ring_equiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl @[simp] lemma coe_ring_equiv' : (e.to_ring_equiv : A₁ → A₂) = e := rfl lemma coe_ring_equiv_injective : function.injective (λ e : A₁ ≃ₐ[R] A₂, (e : A₁ ≃+* A₂)) := begin intros f g w, ext, replace w : ((f : A₁ ≃+* A₂) : A₁ → A₂) = ((g : A₁ ≃+* A₂) : A₁ → A₂) := congr_arg (λ e : A₁ ≃+* A₂, (e : A₁ → A₂)) w, exact congr_fun w a, end @[simp] lemma map_add : ∀ x y, e (x + y) = e x + e y := e.to_add_equiv.map_add @[simp] lemma map_zero : e 0 = 0 := e.to_add_equiv.map_zero @[simp] lemma map_mul : ∀ x y, e (x * y) = (e x) * (e y) := e.to_mul_equiv.map_mul @[simp] lemma map_one : e 1 = 1 := e.to_mul_equiv.map_one @[simp] lemma commutes : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r := e.commutes' lemma map_sum {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∑ x in s, f x) = ∑ x in s, e (f x) := e.to_add_equiv.map_sum f s lemma map_finsupp_sum {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.sum g) = f.sum (λ i b, e (g i b)) := e.map_sum _ _ /-- Interpret an algebra equivalence as an algebra homomorphism. This definition is included for symmetry with the other `to_*_hom` projections. The `simp` normal form is to use the coercion of the `has_coe_to_alg_hom` instance. -/ def to_alg_hom : A₁ →ₐ[R] A₂ := { map_one' := e.map_one, map_zero' := e.map_zero, ..e } instance has_coe_to_alg_hom : has_coe (A₁ ≃ₐ[R] A₂) (A₁ →ₐ[R] A₂) := ⟨to_alg_hom⟩ @[simp] lemma to_alg_hom_eq_coe : e.to_alg_hom = e := rfl @[simp, norm_cast] lemma coe_alg_hom : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e := rfl /-- The two paths coercion can take to a `ring_hom` are equivalent -/ lemma coe_ring_hom_commutes : ((e : A₁ →ₐ[R] A₂) : A₁ →+* A₂) = ((e : A₁ ≃+* A₂) : A₁ →+* A₂) := rfl @[simp] lemma map_pow : ∀ (x : A₁) (n : ℕ), e (x ^ n) = (e x) ^ n := e.to_alg_hom.map_pow lemma injective : function.injective e := e.to_equiv.injective lemma surjective : function.surjective e := e.to_equiv.surjective lemma bijective : function.bijective e := e.to_equiv.bijective instance : has_one (A₁ ≃ₐ[R] A₁) := ⟨{commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)}⟩ instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨1⟩ /-- Algebra equivalences are reflexive. -/ @[refl] def refl : A₁ ≃ₐ[R] A₁ := 1 @[simp] lemma coe_refl : (@refl R A₁ _ _ _ : A₁ →ₐ[R] A₁) = alg_hom.id R A₁ := alg_hom.ext (λ x, rfl) /-- Algebra equivalences are symmetric. -/ @[symm] def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ := { commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr, change _ = e _, rw e.commutes, }, ..e.to_ring_equiv.symm, } /-- See Note [custom simps projection] -/ def simps.inv_fun (e : A₁ ≃ₐ[R] A₂) : A₂ → A₁ := e.symm initialize_simps_projections alg_equiv (to_fun → apply, inv_fun → symm_apply) @[simp] lemma inv_fun_eq_symm {e : A₁ ≃ₐ[R] A₂} : e.inv_fun = e.symm := rfl @[simp] lemma symm_symm (e : A₁ ≃ₐ[R] A₂) : e.symm.symm = e := by { ext, refl, } lemma symm_bijective : function.bijective (symm : (A₁ ≃ₐ[R] A₂) → (A₂ ≃ₐ[R] A₁)) := equiv.bijective ⟨symm, symm, symm_symm, symm_symm⟩ @[simp] lemma mk_coe' (e : A₁ ≃ₐ[R] A₂) (f h₁ h₂ h₃ h₄ h₅) : (⟨f, e, h₁, h₂, h₃, h₄, h₅⟩ : A₂ ≃ₐ[R] A₁) = e.symm := symm_bijective.injective $ ext $ λ x, rfl @[simp] theorem symm_mk (f f') (h₁ h₂ h₃ h₄ h₅) : (⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm = { to_fun := f', inv_fun := f, ..(⟨f, f', h₁, h₂, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm } := rfl /-- Algebra equivalences are transitive. -/ @[trans] def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ := { commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'], ..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), } @[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x := e.to_equiv.apply_symm_apply @[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x := e.to_equiv.symm_apply_apply @[simp] lemma trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) : (e₁.trans e₂) x = e₂ (e₁ x) := rfl @[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ := by { ext, simp } @[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) : alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ := by { ext, simp } /-- If `A₁` is equivalent to `A₁'` and `A₂` is equivalent to `A₂'`, then the type of maps `A₁ →ₐ[R] A₂` is equivalent to the type of maps `A₁' →ₐ[R] A₂'`. -/ def arrow_congr {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (A₁ →ₐ[R] A₂) ≃ (A₁' →ₐ[R] A₂') := { to_fun := λ f, (e₂.to_alg_hom.comp f).comp e₁.symm.to_alg_hom, inv_fun := λ f, (e₂.symm.to_alg_hom.comp f).comp e₁.to_alg_hom, left_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, symm_comp], simp only [←alg_hom.comp_assoc, symm_comp, alg_hom.id_comp, alg_hom.comp_id] }, right_inv := λ f, by { simp only [alg_hom.comp_assoc, to_alg_hom_eq_coe, comp_symm], simp only [←alg_hom.comp_assoc, comp_symm, alg_hom.id_comp, alg_hom.comp_id] } } lemma arrow_congr_comp {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') (e₃ : A₃ ≃ₐ[R] A₃') (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [arrow_congr, equiv.coe_fn_mk, alg_hom.comp_apply], congr, exact (e₂.symm_apply_apply _).symm } @[simp] lemma arrow_congr_refl : arrow_congr alg_equiv.refl alg_equiv.refl = equiv.refl (A₁ →ₐ[R] A₂) := by { ext, refl } @[simp] lemma arrow_congr_trans {A₁' A₂' A₃' : Type*} [semiring A₁'] [semiring A₂'] [semiring A₃'] [algebra R A₁'] [algebra R A₂'] [algebra R A₃'] (e₁ : A₁ ≃ₐ[R] A₂) (e₁' : A₁' ≃ₐ[R] A₂') (e₂ : A₂ ≃ₐ[R] A₃) (e₂' : A₂' ≃ₐ[R] A₃') : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := by { ext, refl } @[simp] lemma arrow_congr_symm {A₁' A₂' : Type*} [semiring A₁'] [semiring A₂'] [algebra R A₁'] [algebra R A₂'] (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := by { ext, refl } /-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/ def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂) (h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ := { inv_fun := g, left_inv := alg_hom.ext_iff.1 h₂, right_inv := alg_hom.ext_iff.1 h₁, ..f } /-- Promotes a bijective algebra homomorphism to an algebra equivalence. -/ noncomputable def of_bijective (f : A₁ →ₐ[R] A₂) (hf : function.bijective f) : A₁ ≃ₐ[R] A₂ := { .. ring_equiv.of_bijective (f : A₁ →+* A₂) hf, .. f } /-- Forgetting the multiplicative structures, an equivalence of algebras is a linear equivalence. -/ def to_linear_equiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ := { to_fun := e.to_fun, map_add' := λ x y, by simp, map_smul' := λ r x, by simp [algebra.smul_def''], inv_fun := e.symm.to_fun, left_inv := e.left_inv, right_inv := e.right_inv, } @[simp] lemma to_linear_equiv_apply (e : A₁ ≃ₐ[R] A₂) (x : A₁) : e.to_linear_equiv x = e x := rfl theorem to_linear_equiv_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_equiv = e₂.to_linear_equiv) : e₁ = e₂ := ext $ λ x, show e₁.to_linear_equiv x = e₂.to_linear_equiv x, by rw H /-- Interpret an algebra equivalence as a linear map. -/ def to_linear_map : A₁ →ₗ[R] A₂ := e.to_alg_hom.to_linear_map @[simp] lemma to_alg_hom_to_linear_map : (e : A₁ →ₐ[R] A₂).to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_equiv_to_linear_map : e.to_linear_equiv.to_linear_map = e.to_linear_map := rfl @[simp] lemma to_linear_map_apply (x : A₁) : e.to_linear_map x = e x := rfl theorem to_linear_map_inj {e₁ e₂ : A₁ ≃ₐ[R] A₂} (H : e₁.to_linear_map = e₂.to_linear_map) : e₁ = e₂ := ext $ λ x, show e₁.to_linear_map x = e₂.to_linear_map x, by rw H @[simp] lemma trans_to_linear_map (f : A₁ ≃ₐ[R] A₂) (g : A₂ ≃ₐ[R] A₃) : (f.trans g).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl section of_linear_equiv variables (l : A₁ ≃ₗ[R] A₂) (map_mul : ∀ x y : A₁, l (x * y) = l x * l y) (commutes : ∀ r : R, l (algebra_map R A₁ r) = algebra_map R A₂ r) /-- Upgrade a linear equivalence to an algebra equivalence, given that it distributes over multiplication and action of scalars. -/ def of_linear_equiv : A₁ ≃ₐ[R] A₂ := { to_fun := l, inv_fun := l.symm, map_mul' := map_mul, commutes' := commutes, ..l } @[simp] lemma of_linear_equiv_to_linear_equiv (map_mul) (commutes) : of_linear_equiv e.to_linear_equiv map_mul commutes = e := by { ext, refl } @[simp] lemma to_linear_equiv_of_linear_equiv : to_linear_equiv (of_linear_equiv l map_mul commutes) = l := by { ext, refl } @[simp] lemma of_linear_equiv_apply (x : A₁) : of_linear_equiv l map_mul commutes x = l x := rfl end of_linear_equiv instance aut : group (A₁ ≃ₐ[R] A₁) := { mul := λ ϕ ψ, ψ.trans ϕ, mul_assoc := λ ϕ ψ χ, rfl, one := 1, one_mul := λ ϕ, by { ext, refl }, mul_one := λ ϕ, by { ext, refl }, inv := symm, mul_left_inv := λ ϕ, by { ext, exact symm_apply_apply ϕ a } } @[simp] lemma mul_apply (e₁ e₂ : A₁ ≃ₐ[R] A₁) (x : A₁) : (e₁ * e₂) x = e₁ (e₂ x) := rfl /-- An algebra isomorphism induces a group isomorphism between automorphism groups -/ @[simps apply] def aut_congr (ϕ : A₁ ≃ₐ[R] A₂) : (A₁ ≃ₐ[R] A₁) ≃* (A₂ ≃ₐ[R] A₂) := { to_fun := λ ψ, ϕ.symm.trans (ψ.trans ϕ), inv_fun := λ ψ, ϕ.trans (ψ.trans ϕ.symm), left_inv := λ ψ, by { ext, simp_rw [trans_apply, symm_apply_apply] }, right_inv := λ ψ, by { ext, simp_rw [trans_apply, apply_symm_apply] }, map_mul' := λ ψ χ, by { ext, simp only [mul_apply, trans_apply, symm_apply_apply] } } @[simp] lemma aut_congr_refl : aut_congr (alg_equiv.refl) = mul_equiv.refl (A₁ ≃ₐ[R] A₁) := by { ext, refl } @[simp] lemma aut_congr_symm (ϕ : A₁ ≃ₐ[R] A₂) : (aut_congr ϕ).symm = aut_congr ϕ.symm := rfl @[simp] lemma aut_congr_trans (ϕ : A₁ ≃ₐ[R] A₂) (ψ : A₂ ≃ₐ[R] A₃) : (aut_congr ϕ).trans (aut_congr ψ) = aut_congr (ϕ.trans ψ) := rfl end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A₁] [comm_semiring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) lemma map_prod {ι : Type*} (f : ι → A₁) (s : finset ι) : e (∏ x in s, f x) = ∏ x in s, e (f x) := e.to_alg_hom.map_prod f s lemma map_finsupp_prod {α : Type*} [has_zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.prod g) = f.prod (λ i a, e (g i a)) := e.to_alg_hom.map_finsupp_prod f g end comm_semiring section ring variables [comm_ring R] [ring A₁] [ring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[simp] lemma map_neg (x) : e (-x) = -e x := e.to_alg_hom.map_neg x @[simp] lemma map_sub (x y) : e (x - y) = e x - e y := e.to_alg_hom.map_sub x y end ring section division_ring variables [comm_ring R] [division_ring A₁] [division_ring A₂] variables [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[simp] lemma map_inv (x) : e (x⁻¹) = (e x)⁻¹ := e.to_alg_hom.map_inv x @[simp] lemma map_div (x y) : e (x / y) = e x / e y := e.to_alg_hom.map_div x y end division_ring end alg_equiv namespace matrix /-! ### `matrix` section Specialize `matrix.one_map` and `matrix.zero_map` to `alg_hom` and `alg_equiv`. TODO: there should be a way to avoid restating these for each `foo_hom`. -/ variables {R A₁ A₂ n : Type*} [fintype n] section semiring variables [comm_semiring R] [semiring A₁] [algebra R A₁] [semiring A₂] [algebra R A₂] /-- A version of `matrix.one_map` where `f` is an `alg_hom`. -/ @[simp] lemma alg_hom_map_one [decidable_eq n] (f : A₁ →ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 := one_map f.map_zero f.map_one /-- A version of `matrix.one_map` where `f` is an `alg_equiv`. -/ @[simp] lemma alg_equiv_map_one [decidable_eq n] (f : A₁ ≃ₐ[R] A₂) : (1 : matrix n n A₁).map f = 1 := one_map f.map_zero f.map_one /-- A version of `matrix.zero_map` where `f` is an `alg_hom`. -/ @[simp] lemma alg_hom_map_zero (f : A₁ →ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 := map_zero f.map_zero /-- A version of `matrix.zero_map` where `f` is an `alg_equiv`. -/ @[simp] lemma alg_equiv_map_zero (f : A₁ ≃ₐ[R] A₂) : (0 : matrix n n A₁).map f = 0 := map_zero f.map_zero end semiring end matrix namespace algebra variables (R : Type u) (S : Type v) (A : Type w) include R S A /-- `comap R S A` is a type alias for `A`, and has an R-algebra structure defined on it when `algebra R S` and `algebra S A`. If `S` is an `R`-algebra and `A` is an `S`-algebra then `algebra.comap.algebra R S A` can be used to provide `A` with a structure of an `R`-algebra. Other than that, `algebra.comap` is now deprecated and replaced with `is_scalar_tower`. -/ /- This is done to avoid a type class search with meta-variables `algebra R ?m_1` and `algebra ?m_1 A -/ /- The `nolint` attribute is added because it has unused arguments `R` and `S`, but these are necessary for synthesizing the appropriate type classes -/ @[nolint unused_arguments] def comap : Type w := A instance comap.inhabited [h : inhabited A] : inhabited (comap R S A) := h instance comap.semiring [h : semiring A] : semiring (comap R S A) := h instance comap.ring [h : ring A] : ring (comap R S A) := h instance comap.comm_semiring [h : comm_semiring A] : comm_semiring (comap R S A) := h instance comap.comm_ring [h : comm_ring A] : comm_ring (comap R S A) := h instance comap.algebra' [comm_semiring S] [semiring A] [h : algebra S A] : algebra S (comap R S A) := h /-- Identity homomorphism `A →ₐ[S] comap R S A`. -/ def comap.to_comap [comm_semiring S] [semiring A] [algebra S A] : A →ₐ[S] comap R S A := alg_hom.id S A /-- Identity homomorphism `comap R S A →ₐ[S] A`. -/ def comap.of_comap [comm_semiring S] [semiring A] [algebra S A] : comap R S A →ₐ[S] A := alg_hom.id S A variables [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] /-- `R ⟶ S` induces `S-Alg ⥤ R-Alg` -/ instance comap.algebra : algebra R (comap R S A) := { smul := λ r x, (algebra_map R S r • x : A), commutes' := λ r x, algebra.commutes _ _, smul_def' := λ _ _, algebra.smul_def _ _, .. (algebra_map S A).comp (algebra_map R S) } /-- Embedding of `S` into `comap R S A`. -/ def to_comap : S →ₐ[R] comap R S A := { commutes' := λ r, rfl, .. algebra_map S A } theorem to_comap_apply (x) : to_comap R S A x = algebra_map S A x := rfl end algebra section variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁} variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B] variables [algebra R S] [algebra S A] [algebra S B] include R /-- R ⟶ S induces S-Alg ⥤ R-Alg. See `alg_hom.restrict_scalars` for the version that uses `is_scalar_tower` instead of `comap`. -/ def alg_hom.comap (φ : A →ₐ[S] B) : algebra.comap R S A →ₐ[R] algebra.comap R S B := { commutes' := λ r, φ.commutes (algebra_map R S r) ..φ } /-- `alg_hom.comap` for `alg_equiv`. See `alg_equiv.restrict_scalars` for the version that uses `is_scalar_tower` instead of `comap`. -/ def alg_equiv.comap (φ : A ≃ₐ[S] B) : algebra.comap R S A ≃ₐ[R] algebra.comap R S B := { commutes' := λ r, φ.commutes (algebra_map R S r) ..φ } end namespace ring_hom variables {R S : Type*} /-- Reinterpret a `ring_hom` as an `ℕ`-algebra homomorphism. -/ def to_nat_alg_hom [semiring R] [semiring S] [algebra ℕ R] [algebra ℕ S] (f : R →+* S) : R →ₐ[ℕ] S := { to_fun := f, commutes' := λ n, by simp, .. f } /-- Reinterpret a `ring_hom` as a `ℤ`-algebra homomorphism. -/ def to_int_alg_hom [ring R] [ring S] [algebra ℤ R] [algebra ℤ S] (f : R →+* S) : R →ₐ[ℤ] S := { commutes' := λ n, by simp, .. f } @[simp] lemma map_rat_algebra_map [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) (r : ℚ) : f (algebra_map ℚ R r) = algebra_map ℚ S r := ring_hom.ext_iff.1 (subsingleton.elim (f.comp (algebra_map ℚ R)) (algebra_map ℚ S)) r /-- Reinterpret a `ring_hom` as a `ℚ`-algebra homomorphism. -/ def to_rat_alg_hom [ring R] [ring S] [algebra ℚ R] [algebra ℚ S] (f : R →+* S) : R →ₐ[ℚ] S := { commutes' := f.map_rat_algebra_map, .. f } end ring_hom namespace rat instance algebra_rat {α} [division_ring α] [char_zero α] : algebra ℚ α := (rat.cast_hom α).to_algebra' $ λ r x, r.cast_commute x @[simp] theorem algebra_map_rat_rat : algebra_map ℚ ℚ = ring_hom.id ℚ := subsingleton.elim _ _ -- TODO[gh-6025]: make this an instance once safe to do so lemma algebra_rat_subsingleton {α} [semiring α] : subsingleton (algebra ℚ α) := ⟨λ x y, algebra.algebra_ext x y $ ring_hom.congr_fun $ subsingleton.elim _ _⟩ end rat namespace algebra open module variables (R : Type u) (A : Type v) variables [comm_semiring R] [semiring A] [algebra R A] /-- `algebra_map` as an `alg_hom`. -/ def of_id : R →ₐ[R] A := { commutes' := λ _, rfl, .. algebra_map R A } variables {R} theorem of_id_apply (r) : of_id R A r = algebra_map R A r := rfl variables (R A) /-- The multiplication in an algebra is a bilinear map. -/ def lmul : A →ₐ[R] (End R A) := { map_one' := by { ext a, exact one_mul a }, map_mul' := by { intros a b, ext c, exact mul_assoc a b c }, map_zero' := by { ext a, exact zero_mul a }, commutes' := by { intro r, ext a, dsimp, rw [smul_def] }, .. (show A →ₗ[R] A →ₗ[R] A, from linear_map.mk₂ R (*) (λ x y z, add_mul x y z) (λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y]) (λ x y z, mul_add x y z) (λ c x y, by rw [smul_def, smul_def, left_comm])) } variables {A} /-- The multiplication on the left in an algebra is a linear map. -/ def lmul_left (r : A) : A →ₗ A := lmul R A r /-- The multiplication on the right in an algebra is a linear map. -/ def lmul_right (r : A) : A →ₗ A := (lmul R A).to_linear_map.flip r /-- Simultaneous multiplication on the left and right is a linear map. -/ def lmul_left_right (vw: A × A) : A →ₗ[R] A := (lmul_right R vw.2).comp (lmul_left R vw.1) /-- The multiplication map on an algebra, as an `R`-linear map from `A ⊗[R] A` to `A`. -/ def lmul' : A ⊗[R] A →ₗ[R] A := tensor_product.lift (lmul R A).to_linear_map variables {R A} @[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl @[simp] lemma lmul_left_apply (p q : A) : lmul_left R p q = p * q := rfl @[simp] lemma lmul_right_apply (p q : A) : lmul_right R p q = q * p := rfl @[simp] lemma lmul_left_right_apply (vw : A × A) (p : A) : lmul_left_right R vw p = vw.1 * p * vw.2 := rfl @[simp] lemma lmul_left_one : lmul_left R (1:A) = linear_map.id := by { ext, simp only [linear_map.id_coe, one_mul, id.def, lmul_left_apply] } @[simp] lemma lmul_left_mul (a b : A) : lmul_left R (a * b) = (lmul_left R a).comp (lmul_left R b) := by { ext, simp only [lmul_left_apply, linear_map.comp_apply, mul_assoc] } @[simp] lemma lmul_right_one : lmul_right R (1:A) = linear_map.id := by { ext, simp only [linear_map.id_coe, mul_one, id.def, lmul_right_apply] } @[simp] lemma lmul_right_mul (a b : A) : lmul_right R (a * b) = (lmul_right R b).comp (lmul_right R a) := by { ext, simp only [lmul_right_apply, linear_map.comp_apply, mul_assoc] } @[simp] lemma lmul'_apply {x y : A} : lmul' R (x ⊗ₜ y) = x * y := by simp only [algebra.lmul', tensor_product.lift.tmul, alg_hom.to_linear_map_apply, lmul_apply] instance linear_map.semimodule' (R : Type u) [comm_semiring R] (M : Type v) [add_comm_monoid M] [semimodule R M] (S : Type w) [comm_semiring S] [algebra R S] : semimodule S (M →ₗ[R] S) := { smul := λ s f, linear_map.llcomp _ _ _ _ (algebra.lmul R S s) f, one_smul := λ f, linear_map.ext $ λ x, one_mul _, mul_smul := λ s₁ s₂ f, linear_map.ext $ λ x, mul_assoc _ _ _, smul_add := λ s f g, linear_map.map_add _ _ _, smul_zero := λ s, linear_map.map_zero _, add_smul := λ s₁ s₂ f, linear_map.ext $ λ x, add_mul _ _ _, zero_smul := λ f, linear_map.ext $ λ x, zero_mul _ } end algebra section ring namespace algebra variables {R A : Type*} [comm_semiring R] [ring A] [algebra R A] lemma lmul_left_injective [no_zero_divisors A] {x : A} (hx : x ≠ 0) : function.injective (lmul_left R x) := by { letI : domain A := { exists_pair_ne := ⟨x, 0, hx⟩, ..‹ring A›, ..‹no_zero_divisors A› }, exact mul_right_injective' hx } lemma lmul_right_injective [no_zero_divisors A] {x : A} (hx : x ≠ 0) : function.injective (lmul_right R x) := by { letI : domain A := { exists_pair_ne := ⟨x, 0, hx⟩, ..‹ring A›, ..‹no_zero_divisors A› }, exact mul_left_injective' hx } lemma lmul_injective [no_zero_divisors A] {x : A} (hx : x ≠ 0) : function.injective (lmul R A x) := by { letI : domain A := { exists_pair_ne := ⟨x, 0, hx⟩, ..‹ring A›, ..‹no_zero_divisors A› }, exact mul_right_injective' hx } end algebra end ring section nat variables (R : Type*) [semiring R] /-- Semiring ⥤ ℕ-Alg -/ instance algebra_nat : algebra ℕ R := { commutes' := nat.cast_commute, smul_def' := λ _ _, nsmul_eq_mul _ _, to_ring_hom := nat.cast_ring_hom R } section span_nat open submodule lemma span_nat_eq_add_group_closure (s : set R) : (span ℕ s).to_add_submonoid = add_submonoid.closure s := eq.symm $ add_submonoid.closure_eq_of_le subset_span $ λ x hx, span_induction hx (λ x hx, add_submonoid.subset_closure hx) (add_submonoid.zero_mem _) (λ _ _, add_submonoid.add_mem _) (λ _ _ _, add_submonoid.nsmul_mem _ ‹_› _) @[simp] lemma span_nat_eq (s : add_submonoid R) : (span ℕ (s : set R)).to_add_submonoid = s := by rw [span_nat_eq_add_group_closure, s.closure_eq] end span_nat end nat section int variables (R : Type*) [ring R] /-- Ring ⥤ ℤ-Alg -/ instance algebra_int : algebra ℤ R := { commutes' := int.cast_commute, smul_def' := λ _ _, gsmul_eq_mul _ _, to_ring_hom := int.cast_ring_hom R } variables {R} section variables {S : Type*} [ring S] instance int_algebra_subsingleton : subsingleton (algebra ℤ S) := ⟨λ P Q, by { ext, simp, }⟩ end section variables {S : Type*} [semiring S] instance nat_algebra_subsingleton : subsingleton (algebra ℕ S) := ⟨λ P Q, by { ext, simp, }⟩ end section span_int open submodule lemma span_int_eq_add_group_closure (s : set R) : (span ℤ s).to_add_subgroup = add_subgroup.closure s := eq.symm $ add_subgroup.closure_eq_of_le _ subset_span $ λ x hx, span_induction hx (λ x hx, add_subgroup.subset_closure hx) (add_subgroup.zero_mem _) (λ _ _, add_subgroup.add_mem _) (λ _ _ _, add_subgroup.gsmul_mem _ ‹_› _) @[simp] lemma span_int_eq (s : add_subgroup R) : (span ℤ (s : set R)).to_add_subgroup = s := by rw [span_int_eq_add_group_closure, s.closure_eq] end span_int end int /-! The R-algebra structure on `Π i : I, A i` when each `A i` is an R-algebra. We couldn't set this up back in `algebra.pi_instances` because this file imports it. -/ namespace pi variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) variables (I f) instance algebra (α) {r : comm_semiring α} [s : ∀ i, semiring (f i)] [∀ i, algebra α (f i)] : algebra α (Π i : I, f i) := { commutes' := λ a f, begin ext, simp [algebra.commutes], end, smul_def' := λ a f, begin ext, simp [algebra.smul_def''], end, ..pi.ring_hom (λ i, algebra_map α (f i)) } @[simp] lemma algebra_map_apply (α) {r : comm_semiring α} [s : ∀ i, semiring (f i)] [∀ i, algebra α (f i)] (a : α) (i : I) : algebra_map α (Π i, f i) a i = algebra_map α (f i) a := rfl -- One could also build a `Π i, R i`-algebra structure on `Π i, A i`, -- when each `A i` is an `R i`-algebra, although I'm not sure that it's useful. end pi section is_scalar_tower variables {R : Type*} [comm_semiring R] variables (A : Type*) [semiring A] [algebra R A] variables {M : Type*} [add_comm_monoid M] [semimodule A M] [semimodule R M] [is_scalar_tower R A M] variables {N : Type*} [add_comm_monoid N] [semimodule A N] [semimodule R N] [is_scalar_tower R A N] lemma algebra_compatible_smul (r : R) (m : M) : r • m = ((algebra_map R A) r) • m := by rw [←(one_smul A m), ←smul_assoc, algebra.smul_def, mul_one, one_smul] @[simp] lemma algebra_map_smul (r : R) (m : M) : ((algebra_map R A) r) • m = r • m := (algebra_compatible_smul A r m).symm variable {A} @[priority 100] -- see Note [lower instance priority] instance is_scalar_tower.to_smul_comm_class : smul_comm_class R A M := ⟨λ r a m, by rw [algebra_compatible_smul A r (a • m), smul_smul, algebra.commutes, mul_smul, ←algebra_compatible_smul]⟩ @[priority 100] -- see Note [lower instance priority] instance is_scalar_tower.to_smul_comm_class' : smul_comm_class A R M := smul_comm_class.symm _ _ _ lemma smul_algebra_smul_comm (r : R) (a : A) (m : M) : a • r • m = r • a • m := smul_comm _ _ _ namespace linear_map instance coe_is_scalar_tower : has_coe (M →ₗ[A] N) (M →ₗ[R] N) := ⟨restrict_scalars R⟩ variables (R) {A M N} @[simp, norm_cast squash] lemma coe_restrict_scalars_eq_coe (f : M →ₗ[A] N) : (f.restrict_scalars R : M → N) = f := rfl @[simp, norm_cast squash] lemma coe_coe_is_scalar_tower (f : M →ₗ[A] N) : ((f : M →ₗ[R] N) : M → N) = f := rfl /-- `A`-linearly coerce a `R`-linear map from `M` to `A` to a function, given an algebra `A` over a commutative semiring `R` and `M` a semimodule over `R`. -/ def lto_fun (R : Type u) (M : Type v) (A : Type w) [comm_semiring R] [add_comm_monoid M] [semimodule R M] [comm_ring A] [algebra R A] : (M →ₗ[R] A) →ₗ[A] (M → A) := { to_fun := linear_map.to_fun, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } end linear_map end is_scalar_tower section restrict_scalars /- In this section, we describe restriction of scalars: if `S` is an algebra over `R`, then `S`-modules are also `R`-modules. -/ section type_synonym variables (R A M : Type*) /-- Warning: use this type synonym judiciously! The preferred way of working with an `A`-module `M` as `R`-module (where `A` is an `R`-algebra), is by `[module R M] [module A M] [is_scalar_tower R A M]`. When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a module structure over `R`, provided as a type synonym `module.restrict_scalars R A M := M`. -/ @[nolint unused_arguments] def restrict_scalars (R A M : Type*) : Type* := M instance [I : inhabited M] : inhabited (restrict_scalars R A M) := I instance [I : add_comm_monoid M] : add_comm_monoid (restrict_scalars R A M) := I instance [I : add_comm_group M] : add_comm_group (restrict_scalars R A M) := I instance restrict_scalars.module_orig [semiring A] [add_comm_monoid M] [I : semimodule A M] : semimodule A (restrict_scalars R A M) := I variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [semimodule A M] /-- When `M` is a module over a ring `A`, and `A` is an algebra over `R`, then `M` inherits a module structure over `R`. The preferred way of setting this up is `[module R M] [module A M] [is_scalar_tower R A M]`. -/ instance : semimodule R (restrict_scalars R A M) := semimodule.comp_hom M (algebra_map R A) lemma restrict_scalars_smul_def (c : R) (x : restrict_scalars R A M) : c • x = ((algebra_map R A c) • x : M) := rfl instance : is_scalar_tower R A (restrict_scalars R A M) := ⟨λ r A M, by { rw [algebra.smul_def, mul_smul], refl }⟩ instance submodule.restricted_module (V : submodule A M) : semimodule R V := restrict_scalars.semimodule R A V instance submodule.restricted_module_is_scalar_tower (V : submodule A M) : is_scalar_tower R A V := restrict_scalars.is_scalar_tower R A V end type_synonym /-! TODO: The following lemmas no longer involve `algebra` at all, and could be moved closer to `algebra/module/submodule.lean`. Currently this is tricky because `ker`, `range`, `⊤`, and `⊥` are all defined in `linear_algebra/basic.lean`. -/ section semimodule open semimodule variables (R S M N : Type*) [semiring R] [semiring S] [has_scalar R S] variables [add_comm_monoid M] [semimodule R M] [semimodule S M] [is_scalar_tower R S M] variables [add_comm_monoid N] [semimodule R N] [semimodule S N] [is_scalar_tower R S N] variables {S M N} namespace submodule /-- `V.restrict_scalars R` is the `R`-submodule of the `R`-module given by restriction of scalars, corresponding to `V`, an `S`-submodule of the original `S`-module. -/ @[simps] def restrict_scalars (V : submodule S M) : submodule R M := { carrier := V.carrier, zero_mem' := V.zero_mem, smul_mem' := λ c m h, V.smul_of_tower_mem c h, add_mem' := λ x y hx hy, V.add_mem hx hy } @[simp] lemma restrict_scalars_mem (V : submodule S M) (m : M) : m ∈ V.restrict_scalars R ↔ m ∈ V := iff.refl _ variables (R S M) lemma restrict_scalars_injective : function.injective (restrict_scalars R : submodule S M → submodule R M) := λ V₁ V₂ h, ext $ by convert set.ext_iff.1 (set_like.ext'_iff.1 h); refl @[simp] lemma restrict_scalars_inj {V₁ V₂ : submodule S M} : restrict_scalars R V₁ = restrict_scalars R V₂ ↔ V₁ = V₂ := (restrict_scalars_injective R _ _).eq_iff @[simp] lemma restrict_scalars_bot : restrict_scalars R (⊥ : submodule S M) = ⊥ := rfl @[simp] lemma restrict_scalars_top : restrict_scalars R (⊤ : submodule S M) = ⊤ := rfl /-- If `S` is an `R`-algebra, then the `R`-module generated by a set `X` is included in the `S`-module generated by `X`. -/ lemma span_le_restrict_scalars (X : set M) : span R (X : set M) ≤ restrict_scalars R (span S X) := submodule.span_le.mpr submodule.subset_span end submodule @[simp] lemma linear_map.ker_restrict_scalars (f : M →ₗ[S] N) : (f.restrict_scalars R).ker = f.ker.restrict_scalars R := rfl end semimodule end restrict_scalars namespace submodule variables (R A M : Type*) variables [comm_semiring R] [semiring A] [algebra R A] [add_comm_monoid M] variables [semimodule R M] [semimodule A M] [is_scalar_tower R A M] /-- If `A` is an `R`-algebra such that the induced morhpsim `R →+* A` is surjective, then the `R`-module generated by a set `X` equals the `A`-module generated by `X`. -/ lemma span_eq_restrict_scalars (X : set M) (hsur : function.surjective (algebra_map R A)) : span R X = restrict_scalars R (span A X) := begin apply (span_le_restrict_scalars R A M X).antisymm (λ m hm, _), refine span_induction hm subset_span (zero_mem _) (λ _ _, add_mem _) (λ a m hm, _), obtain ⟨r, rfl⟩ := hsur a, simpa [algebra_map_smul] using smul_mem _ r hm end end submodule
6ecfa7ef2d69fc3b62b194acc6c930914b9546ce
2d34dfb0a1cc250584282618dc10ea03d3fa858e
/src/Mbar/complex2.lean
e81bbf93c75596aa1c89f30e3dff92b082f7b55b
[]
no_license
zeta1999/lean-liquid
61e294ec5adae959d8ee1b65d015775484ff58c2
96bb0fa3afc3b451bcd1fb7d974348de2f290541
refs/heads/master
1,676,579,150,248
1,610,771,445,000
1,610,771,445,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,786
lean
import Mbar.complex import for_mathlib.equalizers open_locale classical nnreal noncomputable theory open opposite breen_deligne category_theory category_theory.limits variables (BD : package) (c' : ℕ → ℝ≥0) [BD.suitable c'] variables (V : NormedGroup) (S : Type*) (r r' c c₁ c₂ c₃ c₄ : ℝ≥0) (a : ℕ) [fintype S] /- TODO: Do we want to define the `T⁻¹`-invariants as a kernel, or would it be better to use equalizers? -/ /-- The space `V-hat(Mbar_{r'}(S)_{≤c}^a)^{T⁻¹}`. -/ def LCC_Mbar_pow_Tinv [fact (0 < r)] [fact (0 < r')] [fact (r' ≤ 1)] [normed_with_aut r V] : NormedGroup := equalizer (LCC_Mbar_pow.Tinv V S r' c a) (normed_with_aut.T.inv ≫ (LCC_Mbar_pow.res V S r' _ _ a)) variables [fact (0 < r)] [fact (0 < r')] [fact (r' ≤ 1)] [normed_with_aut r V] namespace LCC_Mbar_pow_Tinv def res [fact (c₁ ≤ c₂)] : LCC_Mbar_pow_Tinv V S r r' c₂ a ⟶ LCC_Mbar_pow_Tinv V S r r' c₁ a := equalizer.map (LCC_Mbar_pow.res _ _ _ _ _ _) (LCC_Mbar_pow.res _ _ _ _ _ _) begin rw LCC_Mbar_pow.Tinv_res end begin haveI : fact (c₁ ≤ r'⁻¹ * c₂) := le_trans ‹c₁ ≤ c₂› (show fact (c₂ ≤ r'⁻¹ * c₂), by apply_instance), rw [category.assoc, LCC_Mbar_pow.res_comp_res, ← LCC_Mbar_pow.T_inv_res_assoc, LCC_Mbar_pow.res_comp_res] end lemma res_comp_res [fact (c₁ ≤ c₂)] [fact (c₂ ≤ c₃)] [fact (c₁ ≤ c₃)] : res V S r r' c₂ c₃ a ≫ res V S r r' c₁ c₂ a = res V S r r' c₁ c₃ a := by simp only [res, equalizer.map_comp_map, LCC_Mbar_pow.res_comp_res] @[simp] lemma res_refl [normed_with_aut r V] : res V S r r' c c a = 𝟙 _ := by { simp only [res, equalizer.map_id, LCC_Mbar_pow.res_refl], refl } end LCC_Mbar_pow_Tinv namespace breen_deligne namespace universal_map variables {l m n : ℕ} def eval_Mbar_pow_Tinv (f : universal_map m n) [f.suitable c₁ c₂] : LCC_Mbar_pow_Tinv V S r r' c₂ n ⟶ LCC_Mbar_pow_Tinv V S r r' c₁ m := equalizer.map (f.eval_Mbar_pow V S r' ((r'⁻¹ * c₁)) ((r'⁻¹ * c₂))) (f.eval_Mbar_pow V S r' c₁ c₂) (by rw eval_Mbar_pow_comp_Tinv) (by rw [category.assoc, ← eval_Mbar_pow_comp_res V S r' c₁ c₂ (r'⁻¹ * c₁) (r'⁻¹ * c₂) f, eval_Mbar_pow_comp_T_inv_assoc]) @[simp] lemma eval_Mbar_pow_Tinv_zero : (0 : universal_map m n).eval_Mbar_pow_Tinv V S r r' c₁ c₂ = 0 := begin apply equalizer.hom_ext, simp only [eval_Mbar_pow_Tinv, eval_Mbar_pow_zero, zero_comp, equalizer.map_ι, comp_zero] end lemma eval_Mbar_pow_Tinv_comp (g : universal_map m n) (f : universal_map l m) [g.suitable c₂ c₃] [f.suitable c₁ c₂] [(comp g f).suitable c₁ c₃] : (comp g f).eval_Mbar_pow_Tinv V S r r' c₁ c₃ = g.eval_Mbar_pow_Tinv V S r r' c₂ c₃ ≫ f.eval_Mbar_pow_Tinv V S r r' c₁ c₂ := by simp only [eval_Mbar_pow_Tinv, equalizer.map_comp_map, ← eval_Mbar_pow_comp] -- move this instance fix_my_name [fact (c₁ ≤ c₂)] : fact (r' * c₁ ≤ r' * c₂) := mul_le_mul' le_rfl ‹_› lemma eval_Mbar_pow_Tinv_comp_res (f : universal_map m n) [f.suitable c₁ c₂] [f.suitable c₃ c₄] [fact (c₁ ≤ c₃)] [fact (c₂ ≤ c₄)] : f.eval_Mbar_pow_Tinv V S r r' c₃ c₄ ≫ LCC_Mbar_pow_Tinv.res V S r r' c₁ c₃ m = LCC_Mbar_pow_Tinv.res V S r r' c₂ c₄ n ≫ f.eval_Mbar_pow_Tinv V S r r' c₁ c₂ := begin delta eval_Mbar_pow_Tinv LCC_Mbar_pow_Tinv.res, rw [equalizer.map_comp_map, equalizer.map_comp_map], congr' 1; apply eval_Mbar_pow_comp_res end end universal_map end breen_deligne open breen_deligne def Mbar_complex (BD : breen_deligne.package) (c' : ℕ → ℝ≥0) [BD.suitable c'] : cochain_complex NormedGroup := { X := int.extend_from_nat 0 $ λ i, LCC_Mbar_pow_Tinv V S r r' (c * c' i) (BD.rank i), d := int.extend_from_nat 0 $ λ i, (BD.map i).eval_Mbar_pow_Tinv V S r r' (c * c' (i+1)) (c * c' i), d_squared' := begin ext1 ⟨i⟩, { dsimp, simp only [pi.comp_apply, pi.zero_apply], erw ← universal_map.eval_Mbar_pow_Tinv_comp V S r r' _ (c * c' (i+1)) _ (BD.map i) (BD.map (i+1)), simp only [BD.map_comp_map, universal_map.eval_Mbar_pow_Tinv_zero], apply_instance }, { show 0 ≫ _ = 0, rw [zero_comp] } end } @[simp] lemma Mbar_complex.d_neg_succ_of_nat (BD : breen_deligne.package) (c' : ℕ → ℝ≥0) [BD.suitable c'] (n : ℕ) : (Mbar_complex V S r r' c BD c').d -[1+n] = 0 := rfl def Mbar_system (BD : breen_deligne.package) (c' : ℕ → ℝ≥0) [BD.suitable c'] : system_of_complexes := { obj := λ c, Mbar_complex V S r r' (unop c : ℝ≥0) BD c', map := λ c₂ c₁ h, { f := int.extend_from_nat 0 $ λ i, by { haveI : fact (((unop c₁ : ℝ≥0) : ℝ) ≤ (unop c₂ : ℝ≥0)) := h.unop.down.down, exact LCC_Mbar_pow_Tinv.res V S r r' _ _ (BD.rank i) }, comm' := begin ext1 ⟨i⟩, { dsimp [int.extend_from_nat], apply universal_map.eval_Mbar_pow_Tinv_comp_res }, { dsimp [int.extend_from_nat], simp only [Mbar_complex.d_neg_succ_of_nat, zero_comp] } end }, map_id' := begin intro c, ext ⟨i⟩ : 2, { dsimp [int.extend_from_nat], rw LCC_Mbar_pow_Tinv.res_refl V S r r' _ _, refl }, { dsimp [int.extend_from_nat], ext } end, map_comp' := begin intros c₃ c₂ c₁ h h', haveI H' : fact (((unop c₁ : ℝ≥0) : ℝ) ≤ (unop c₂ : ℝ≥0)) := h'.unop.down.down, haveI H : fact (((unop c₂ : ℝ≥0) : ℝ) ≤ (unop c₃ : ℝ≥0)) := h.unop.down.down, have : fact (((unop c₁ : ℝ≥0) : ℝ) ≤ (unop c₃ : ℝ≥0)) := le_trans H' H, ext ⟨i⟩ : 2, { dsimp [int.extend_from_nat], rw LCC_Mbar_pow_Tinv.res_comp_res V S r r' _ _ _ _ }, { dsimp [int.extend_from_nat], rw zero_comp }, end }
db3856d6055cda2ada61ec8ebb587af9a8ccd7af
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/data/multiset_extra.lean
f74f5e074d35f37e747f0b14afd82c0d43cb4999
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,779
lean
import data.multiset import data.list_extra namespace multiset open multiset variables {α : Type*} def all_prop (p : α → Prop) (s : multiset α) : Prop := quot.lift_on s (list.all_prop p) (λ l₁ l₂ e, by {change list.perm l₁ l₂ at e, apply propext, apply @list.perm_induction_on α (λ l₁ l₂, list.all_prop p l₁ ↔ list.all_prop p l₂) l₁ l₂ e, {rw[iff_self],trivial}, {intros x l₁ l₂ h,simp only[],intro h',rw[list.all_prop,list.all_prop,h']}, {intros x y l₁ l₂ h,simp only [],intro h', repeat {rw[list.all_prop]},rw[← and_assoc,and_comm (p y),and_assoc,h'],}, {intros l₁ l₂ l₃ h₁₂ h₂₃,simp only[],intros e₁₂ e₂₃,exact e₁₂.trans e₂₃} }) lemma all_prop_coe (p : α → Prop) (l : list α) : all_prop p l = list.all_prop p l := rfl lemma all_prop_iff {p : α → Prop} {s : multiset α} : all_prop p s ↔ ∀ a, a ∈ s → p a := begin rcases s with l, have : quot.mk setoid.r l = (l : multiset α) := rfl, rw[this], rw[all_prop_coe,list.all_prop_iff], split; intros h a; replace h := h a; rw[mem_coe] at *; exact h, end lemma pairwise_zero {α : Type*} (p : α → α → Prop) : pairwise p 0 := ⟨list.nil,⟨rfl,list.pairwise.nil⟩⟩ variable [decidable_eq α] lemma eq_iff_count {s t : multiset α} : s = t ↔ ∀ a, count a s = count a t := begin split, {intros e a,rw[e]}, {intro h,apply le_antisymm;rw[le_iff_count]; intro a; rw[h a],} end lemma eq_repeat_count (s : multiset α) : s.erase_dup.bind (λ a, repeat a (count a s)) = s := begin rw[eq_iff_count],intro a, rw[count_bind], let s₁ := s.erase_dup, let m₁ := s₁.card, let c := (λ b, count a (repeat b (count b s))), change (s₁.map c).sum = count a s, by_cases h : a ∈ s, {have h₁ : a ∈ s₁ := mem_erase_dup.mpr h, let s₂ := s₁.erase a, have : s₁ = a :: s₂ := (cons_erase h₁).symm, rw[this,map_cons,sum_cons], have : a ∉ s₂ := λ h, ((mem_erase_iff_of_nodup s.nodup_erase_dup).mp h).left rfl, have : s₂.map c = repeat 0 (s₂.map c).card := by {apply eq_repeat.mpr ⟨rfl,_⟩,intros m hm, rcases (mem_map.mp hm) with ⟨b,⟨h₁,h₂⟩⟩, rw[← h₂,count_eq_zero],intro ha, rw[← eq_of_mem_repeat ha] at h₁, exact this h₁, }, rw[this,sum_repeat,nat.smul_eq_mul,mul_zero,add_zero], dsimp[c],rw[count_repeat], }, {rw[count_eq_zero.mpr h], have : s₁.map c = repeat 0 (s₁.map c).card := by {apply eq_repeat.mpr ⟨rfl,_⟩,intros m hm, rcases (mem_map.mp hm) with ⟨b,⟨h₁,h₂⟩⟩, rw[← h₂,count_eq_zero],intro ha, rw[eq_of_mem_repeat ha] at h, exact h (mem_erase_dup.mp h₁), }, rw[this,sum_repeat,nat.smul_eq_mul,mul_zero], } end end multiset
815da49e6f8b32ef06ef8caf8c460371cf905050
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/matrix/basic.lean
d681d732fc5882f2fd114a7bfcd1b1c3c8ddbe30
[ "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
86,157
lean
/- Copyright (c) 2018 Ellen Arlt. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin, Lu-Ming Zhang -/ import algebra.algebra.pi import algebra.big_operators.pi import algebra.big_operators.ring import algebra.big_operators.ring_equiv import algebra.module.linear_map import algebra.module.pi import algebra.star.big_operators import algebra.star.module import algebra.star.pi import data.fintype.big_operators /-! # Matrices > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines basic properties of matrices. Matrices with rows indexed by `m`, columns indexed by `n`, and entries of type `α` are represented with `matrix m n α`. For the typical approach of counting rows and columns, `matrix (fin m) (fin n) α` can be used. ## Notation The locale `matrix` gives the following notation: * `⬝ᵥ` for `matrix.dot_product` * `⬝` for `matrix.mul` * `ᵀ` for `matrix.transpose` * `ᴴ` for `matrix.conj_transpose` ## Implementation notes For convenience, `matrix m n α` is defined as `m → n → α`, as this allows elements of the matrix to be accessed with `A i j`. However, it is not advisable to _construct_ matrices using terms of the form `λ i j, _` or even `(λ i j, _ : matrix m n α)`, as these are not recognized by lean as having the right type. Instead, `matrix.of` should be used. ## TODO Under various conditions, multiplication of infinite matrices makes sense. These have not yet been implemented. -/ universes u u' v w open_locale big_operators /-- `matrix m n R` is the type of matrices with entries in `R`, whose rows are indexed by `m` and whose columns are indexed by `n`. -/ def matrix (m : Type u) (n : Type u') (α : Type v) : Type (max u u' v) := m → n → α variables {l m n o : Type*} {m' : o → Type*} {n' : o → Type*} variables {R : Type*} {S : Type*} {α : Type v} {β : Type w} {γ : Type*} namespace matrix section ext variables {M N : matrix m n α} theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N := ⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩ @[ext] theorem ext : (∀ i j, M i j = N i j) → M = N := ext_iff.mp end ext /-- Cast a function into a matrix. The two sides of the equivalence are definitionally equal types. We want to use an explicit cast to distinguish the types because `matrix` has different instances to pi types (such as `pi.has_mul`, which performs elementwise multiplication, vs `matrix.has_mul`). If you are defining a matrix, in terms of its entries, use `of (λ i j, _)`. The purpose of this approach is to ensure that terms of th e form `(λ i j, _) * (λ i j, _)` do not appear, as the type of `*` can be misleading. Porting note: In Lean 3, it is also safe to use pattern matching in a definition as `| i j := _`, which can only be unfolded when fully-applied. leanprover/lean4#2042 means this does not (currently) work in Lean 4. -/ def of : (m → n → α) ≃ matrix m n α := equiv.refl _ @[simp] lemma of_apply (f : m → n → α) (i j) : of f i j = f i j := rfl @[simp] lemma of_symm_apply (f : matrix m n α) (i j) : of.symm f i j = f i j := rfl /-- `M.map f` is the matrix obtained by applying `f` to each entry of the matrix `M`. This is available in bundled forms as: * `add_monoid_hom.map_matrix` * `linear_map.map_matrix` * `ring_hom.map_matrix` * `alg_hom.map_matrix` * `equiv.map_matrix` * `add_equiv.map_matrix` * `linear_equiv.map_matrix` * `ring_equiv.map_matrix` * `alg_equiv.map_matrix` -/ def map (M : matrix m n α) (f : α → β) : matrix m n β := of (λ i j, f (M i j)) @[simp] lemma map_apply {M : matrix m n α} {f : α → β} {i : m} {j : n} : M.map f i j = f (M i j) := rfl @[simp] lemma map_id (M : matrix m n α) : M.map id = M := by { ext, refl, } @[simp] lemma map_map {M : matrix m n α} {β γ : Type*} {f : α → β} {g : β → γ} : (M.map f).map g = M.map (g ∘ f) := by { ext, refl, } lemma map_injective {f : α → β} (hf : function.injective f) : function.injective (λ M : matrix m n α, M.map f) := λ M N h, ext $ λ i j, hf $ ext_iff.mpr h i j /-- The transpose of a matrix. -/ def transpose (M : matrix m n α) : matrix n m α := of $ λ x y, M y x -- TODO: set as an equation lemma for `transpose`, see mathlib4#3024 @[simp] lemma transpose_apply (M : matrix m n α) (i j) : transpose M i j = M j i := rfl localized "postfix (name := matrix.transpose) `ᵀ`:1500 := matrix.transpose" in matrix /-- The conjugate transpose of a matrix defined in term of `star`. -/ def conj_transpose [has_star α] (M : matrix m n α) : matrix n m α := M.transpose.map star localized "postfix (name := matrix.conj_transpose) `ᴴ`:1500 := matrix.conj_transpose" in matrix /-- `matrix.col u` is the column matrix whose entries are given by `u`. -/ def col (w : m → α) : matrix m unit α := of $ λ x y, w x -- TODO: set as an equation lemma for `col`, see mathlib4#3024 @[simp] lemma col_apply (w : m → α) (i j) : col w i j = w i := rfl /-- `matrix.row u` is the row matrix whose entries are given by `u`. -/ def row (v : n → α) : matrix unit n α := of $ λ x y, v y -- TODO: set as an equation lemma for `row`, see mathlib4#3024 @[simp] lemma row_apply (v : n → α) (i j) : row v i j = v j := rfl instance [inhabited α] : inhabited (matrix m n α) := pi.inhabited _ instance [has_add α] : has_add (matrix m n α) := pi.has_add instance [add_semigroup α] : add_semigroup (matrix m n α) := pi.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (matrix m n α) := pi.add_comm_semigroup instance [has_zero α] : has_zero (matrix m n α) := pi.has_zero instance [add_zero_class α] : add_zero_class (matrix m n α) := pi.add_zero_class instance [add_monoid α] : add_monoid (matrix m n α) := pi.add_monoid instance [add_comm_monoid α] : add_comm_monoid (matrix m n α) := pi.add_comm_monoid instance [has_neg α] : has_neg (matrix m n α) := pi.has_neg instance [has_sub α] : has_sub (matrix m n α) := pi.has_sub instance [add_group α] : add_group (matrix m n α) := pi.add_group instance [add_comm_group α] : add_comm_group (matrix m n α) := pi.add_comm_group instance [unique α] : unique (matrix m n α) := pi.unique instance [subsingleton α] : subsingleton (matrix m n α) := pi.subsingleton instance [nonempty m] [nonempty n] [nontrivial α] : nontrivial (matrix m n α) := function.nontrivial instance [has_smul R α] : has_smul R (matrix m n α) := pi.has_smul instance [has_smul R α] [has_smul S α] [smul_comm_class R S α] : smul_comm_class R S (matrix m n α) := pi.smul_comm_class instance [has_smul R S] [has_smul R α] [has_smul S α] [is_scalar_tower R S α] : is_scalar_tower R S (matrix m n α) := pi.is_scalar_tower instance [has_smul R α] [has_smul Rᵐᵒᵖ α] [is_central_scalar R α] : is_central_scalar R (matrix m n α) := pi.is_central_scalar instance [monoid R] [mul_action R α] : mul_action R (matrix m n α) := pi.mul_action _ instance [monoid R] [add_monoid α] [distrib_mul_action R α] : distrib_mul_action R (matrix m n α) := pi.distrib_mul_action _ instance [semiring R] [add_comm_monoid α] [module R α] : module R (matrix m n α) := pi.module _ _ _ /-! simp-normal form pulls `of` to the outside. -/ @[simp] lemma of_zero [has_zero α] : of (0 : m → n → α) = 0 := rfl @[simp] lemma of_add_of [has_add α] (f g : m → n → α) : of f + of g = of (f + g) := rfl @[simp] lemma of_sub_of [has_sub α] (f g : m → n → α) : of f - of g = of (f - g) := rfl @[simp] lemma neg_of [has_neg α] (f : m → n → α) : -of f = of (-f) := rfl @[simp] lemma smul_of [has_smul R α] (r : R) (f : m → n → α) : r • of f = of (r • f) := rfl @[simp] protected lemma map_zero [has_zero α] [has_zero β] (f : α → β) (h : f 0 = 0) : (0 : matrix m n α).map f = 0 := by { ext, simp [h], } protected lemma map_add [has_add α] [has_add β] (f : α → β) (hf : ∀ a₁ a₂, f (a₁ + a₂) = f a₁ + f a₂) (M N : matrix m n α) : (M + N).map f = M.map f + N.map f := ext $ λ _ _, hf _ _ protected lemma map_sub [has_sub α] [has_sub β] (f : α → β) (hf : ∀ a₁ a₂, f (a₁ - a₂) = f a₁ - f a₂) (M N : matrix m n α) : (M - N).map f = M.map f - N.map f := ext $ λ _ _, hf _ _ lemma map_smul [has_smul R α] [has_smul R β] (f : α → β) (r : R) (hf : ∀ a, f (r • a) = r • f a) (M : matrix m n α) : (r • M).map f = r • (M.map f) := ext $ λ _ _, hf _ /-- The scalar action via `has_mul.to_has_smul` is transformed by the same map as the elements of the matrix, when `f` preserves multiplication. -/ lemma map_smul' [has_mul α] [has_mul β] (f : α → β) (r : α) (A : matrix n n α) (hf : ∀ a₁ a₂, f (a₁ * a₂) = f a₁ * f a₂) : (r • A).map f = f r • A.map f := ext $ λ _ _, hf _ _ /-- The scalar action via `has_mul.to_has_opposite_smul` is transformed by the same map as the elements of the matrix, when `f` preserves multiplication. -/ lemma map_op_smul' [has_mul α] [has_mul β] (f : α → β) (r : α) (A : matrix n n α) (hf : ∀ a₁ a₂, f (a₁ * a₂) = f a₁ * f a₂) : (mul_opposite.op r • A).map f = mul_opposite.op (f r) • A.map f := ext $ λ _ _, hf _ _ lemma _root_.is_smul_regular.matrix [has_smul R S] {k : R} (hk : is_smul_regular S k) : is_smul_regular (matrix m n S) k := is_smul_regular.pi $ λ _, is_smul_regular.pi $ λ _, hk lemma _root_.is_left_regular.matrix [has_mul α] {k : α} (hk : is_left_regular k) : is_smul_regular (matrix m n α) k := hk.is_smul_regular.matrix instance subsingleton_of_empty_left [is_empty m] : subsingleton (matrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim i }⟩ instance subsingleton_of_empty_right [is_empty n] : subsingleton (matrix m n α) := ⟨λ M N, by { ext, exact is_empty_elim j }⟩ end matrix open_locale matrix namespace matrix section diagonal variables [decidable_eq n] /-- `diagonal d` is the square matrix such that `(diagonal d) i i = d i` and `(diagonal d) i j = 0` if `i ≠ j`. Note that bundled versions exist as: * `matrix.diagonal_add_monoid_hom` * `matrix.diagonal_linear_map` * `matrix.diagonal_ring_hom` * `matrix.diagonal_alg_hom` -/ def diagonal [has_zero α] (d : n → α) : matrix n n α := of $ λ i j, if i = j then d i else 0 -- TODO: set as an equation lemma for `diagonal`, see mathlib4#3024 lemma diagonal_apply [has_zero α] (d : n → α) (i j) : diagonal d i j = if i = j then d i else 0 := rfl @[simp] theorem diagonal_apply_eq [has_zero α] (d : n → α) (i : n) : (diagonal d) i i = d i := by simp [diagonal] @[simp] theorem diagonal_apply_ne [has_zero α] (d : n → α) {i j : n} (h : i ≠ j) : (diagonal d) i j = 0 := by simp [diagonal, h] theorem diagonal_apply_ne' [has_zero α] (d : n → α) {i j : n} (h : j ≠ i) : (diagonal d) i j = 0 := diagonal_apply_ne d h.symm @[simp] theorem diagonal_eq_diagonal_iff [has_zero α] {d₁ d₂ : n → α} : diagonal d₁ = diagonal d₂ ↔ ∀ i, d₁ i = d₂ i := ⟨λ h i, by simpa using congr_arg (λ m : matrix n n α, m i i) h, λ h, by rw show d₁ = d₂, from funext h⟩ lemma diagonal_injective [has_zero α] : function.injective (diagonal : (n → α) → matrix n n α) := λ d₁ d₂ h, funext $ λ i, by simpa using matrix.ext_iff.mpr h i i @[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 := by { ext, simp [diagonal] } @[simp] lemma diagonal_transpose [has_zero α] (v : n → α) : (diagonal v)ᵀ = diagonal v := begin ext i j, by_cases h : i = j, { simp [h, transpose] }, { simp [h, transpose, diagonal_apply_ne' _ h] } end @[simp] theorem diagonal_add [add_zero_class α] (d₁ d₂ : n → α) : diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) := by ext i j; by_cases h : i = j; simp [h] @[simp] theorem diagonal_smul [monoid R] [add_monoid α] [distrib_mul_action R α] (r : R) (d : n → α) : diagonal (r • d) = r • diagonal d := by ext i j; by_cases h : i = j; simp [h] variables (n α) /-- `matrix.diagonal` as an `add_monoid_hom`. -/ @[simps] def diagonal_add_monoid_hom [add_zero_class α] : (n → α) →+ matrix n n α := { to_fun := diagonal, map_zero' := diagonal_zero, map_add' := λ x y, (diagonal_add x y).symm,} variables (R) /-- `matrix.diagonal` as a `linear_map`. -/ @[simps] def diagonal_linear_map [semiring R] [add_comm_monoid α] [module R α] : (n → α) →ₗ[R] matrix n n α := { map_smul' := diagonal_smul, .. diagonal_add_monoid_hom n α,} variables {n α R} @[simp] lemma diagonal_map [has_zero α] [has_zero β] {f : α → β} (h : f 0 = 0) {d : n → α} : (diagonal d).map f = diagonal (λ m, f (d m)) := by { ext, simp only [diagonal_apply, map_apply], split_ifs; simp [h], } @[simp] lemma diagonal_conj_transpose [add_monoid α] [star_add_monoid α] (v : n → α) : (diagonal v)ᴴ = diagonal (star v) := begin rw [conj_transpose, diagonal_transpose, diagonal_map (star_zero _)], refl, end section one variables [has_zero α] [has_one α] instance : has_one (matrix n n α) := ⟨diagonal (λ _, 1)⟩ @[simp] theorem diagonal_one : (diagonal (λ _, 1) : matrix n n α) = 1 := rfl theorem one_apply {i j} : (1 : matrix n n α) i j = if i = j then 1 else 0 := rfl @[simp] theorem one_apply_eq (i) : (1 : matrix n n α) i i = 1 := diagonal_apply_eq _ i @[simp] theorem one_apply_ne {i j} : i ≠ j → (1 : matrix n n α) i j = 0 := diagonal_apply_ne _ theorem one_apply_ne' {i j} : j ≠ i → (1 : matrix n n α) i j = 0 := diagonal_apply_ne' _ @[simp] lemma map_one [has_zero β] [has_one β] (f : α → β) (h₀ : f 0 = 0) (h₁ : f 1 = 1) : (1 : matrix n n α).map f = (1 : matrix n n β) := by { ext, simp only [one_apply, map_apply], split_ifs; simp [h₀, h₁], } lemma one_eq_pi_single {i j} : (1 : matrix n n α) i j = pi.single i 1 j := by simp only [one_apply, pi.single_apply, eq_comm]; congr -- deal with decidable_eq end one section numeral @[simp] lemma bit0_apply [has_add α] (M : matrix m m α) (i : m) (j : m) : (bit0 M) i j = bit0 (M i j) := rfl variables [add_zero_class α] [has_one α] lemma bit1_apply (M : matrix n n α) (i : n) (j : n) : (bit1 M) i j = if i = j then bit1 (M i j) else bit0 (M i j) := by dsimp [bit1]; by_cases h : i = j; simp [h] @[simp] lemma bit1_apply_eq (M : matrix n n α) (i : n) : (bit1 M) i i = bit1 (M i i) := by simp [bit1_apply] @[simp] lemma bit1_apply_ne (M : matrix n n α) {i j : n} (h : i ≠ j) : (bit1 M) i j = bit0 (M i j) := by simp [bit1_apply, h] end numeral end diagonal section diag /-- The diagonal of a square matrix. -/ @[simp] def diag (A : matrix n n α) (i : n) : α := A i i @[simp] lemma diag_diagonal [decidable_eq n] [has_zero α] (a : n → α) : diag (diagonal a) = a := funext $ @diagonal_apply_eq _ _ _ _ a @[simp] lemma diag_transpose (A : matrix n n α) : diag Aᵀ = diag A := rfl @[simp] theorem diag_zero [has_zero α] : diag (0 : matrix n n α) = 0 := rfl @[simp] theorem diag_add [has_add α] (A B : matrix n n α) : diag (A + B) = diag A + diag B := rfl @[simp] theorem diag_sub [has_sub α] (A B : matrix n n α) : diag (A - B) = diag A - diag B := rfl @[simp] theorem diag_neg [has_neg α] (A : matrix n n α) : diag (-A) = -diag A := rfl @[simp] theorem diag_smul [has_smul R α] (r : R) (A : matrix n n α) : diag (r • A) = r • diag A := rfl @[simp] theorem diag_one [decidable_eq n] [has_zero α] [has_one α] : diag (1 : matrix n n α) = 1 := diag_diagonal _ variables (n α) /-- `matrix.diag` as an `add_monoid_hom`. -/ @[simps] def diag_add_monoid_hom [add_zero_class α] : matrix n n α →+ (n → α) := { to_fun := diag, map_zero' := diag_zero, map_add' := diag_add,} variables (R) /-- `matrix.diag` as a `linear_map`. -/ @[simps] def diag_linear_map [semiring R] [add_comm_monoid α] [module R α] : matrix n n α →ₗ[R] (n → α) := { map_smul' := diag_smul, .. diag_add_monoid_hom n α,} variables {n α R} lemma diag_map {f : α → β} {A : matrix n n α} : diag (A.map f) = f ∘ diag A := rfl @[simp] lemma diag_conj_transpose [add_monoid α] [star_add_monoid α] (A : matrix n n α) : diag Aᴴ = star (diag A) := rfl @[simp] lemma diag_list_sum [add_monoid α] (l : list (matrix n n α)) : diag l.sum = (l.map diag).sum := map_list_sum (diag_add_monoid_hom n α) l @[simp] lemma diag_multiset_sum [add_comm_monoid α] (s : multiset (matrix n n α)) : diag s.sum = (s.map diag).sum := map_multiset_sum (diag_add_monoid_hom n α) s @[simp] lemma diag_sum {ι} [add_comm_monoid α] (s : finset ι) (f : ι → matrix n n α) : diag (∑ i in s, f i) = ∑ i in s, diag (f i) := map_sum (diag_add_monoid_hom n α) f s end diag section dot_product variables [fintype m] [fintype n] /-- `dot_product v w` is the sum of the entrywise products `v i * w i` -/ def dot_product [has_mul α] [add_comm_monoid α] (v w : m → α) : α := ∑ i, v i * w i /- The precedence of 72 comes immediately after ` • ` for `has_smul.smul`, so that `r₁ • a ⬝ᵥ r₂ • b` is parsed as `(r₁ • a) ⬝ᵥ (r₂ • b)` here. -/ localized "infix (name := matrix.dot_product) ` ⬝ᵥ `:72 := matrix.dot_product" in matrix lemma dot_product_assoc [non_unital_semiring α] (u : m → α) (w : n → α) (v : matrix m n α) : (λ j, u ⬝ᵥ (λ i, v i j)) ⬝ᵥ w = u ⬝ᵥ (λ i, (v i) ⬝ᵥ w) := by simpa [dot_product, finset.mul_sum, finset.sum_mul, mul_assoc] using finset.sum_comm lemma dot_product_comm [add_comm_monoid α] [comm_semigroup α] (v w : m → α) : v ⬝ᵥ w = w ⬝ᵥ v := by simp_rw [dot_product, mul_comm] @[simp] lemma dot_product_punit [add_comm_monoid α] [has_mul α] (v w : punit → α) : v ⬝ᵥ w = v ⟨⟩ * w ⟨⟩ := by simp [dot_product] section mul_one_class variables [mul_one_class α] [add_comm_monoid α] lemma dot_product_one (v : n → α) : v ⬝ᵥ 1 = ∑ i, v i := by simp [(⬝ᵥ)] lemma one_dot_product (v : n → α) : 1 ⬝ᵥ v = ∑ i, v i := by simp [(⬝ᵥ)] end mul_one_class section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring α] (u v w : m → α) (x y : n → α) @[simp] lemma dot_product_zero : v ⬝ᵥ 0 = 0 := by simp [dot_product] @[simp] lemma dot_product_zero' : v ⬝ᵥ (λ _, 0) = 0 := dot_product_zero v @[simp] lemma zero_dot_product : 0 ⬝ᵥ v = 0 := by simp [dot_product] @[simp] lemma zero_dot_product' : (λ _, (0 : α)) ⬝ᵥ v = 0 := zero_dot_product v @[simp] lemma add_dot_product : (u + v) ⬝ᵥ w = u ⬝ᵥ w + v ⬝ᵥ w := by simp [dot_product, add_mul, finset.sum_add_distrib] @[simp] lemma dot_product_add : u ⬝ᵥ (v + w) = u ⬝ᵥ v + u ⬝ᵥ w := by simp [dot_product, mul_add, finset.sum_add_distrib] @[simp] lemma sum_elim_dot_product_sum_elim : (sum.elim u x) ⬝ᵥ (sum.elim v y) = u ⬝ᵥ v + x ⬝ᵥ y := by simp [dot_product] /-- Permuting a vector on the left of a dot product can be transferred to the right. -/ @[simp] lemma comp_equiv_symm_dot_product (e : m ≃ n) : (u ∘ e.symm) ⬝ᵥ x = u ⬝ᵥ (x ∘ e) := (e.sum_comp _).symm.trans $ finset.sum_congr rfl $ λ _ _, by simp only [function.comp, equiv.symm_apply_apply] /-- Permuting a vector on the right of a dot product can be transferred to the left. -/ @[simp] lemma dot_product_comp_equiv_symm (e : n ≃ m) : u ⬝ᵥ (x ∘ e.symm) = (u ∘ e) ⬝ᵥ x := by simpa only [equiv.symm_symm] using (comp_equiv_symm_dot_product u x e.symm).symm /-- Permuting vectors on both sides of a dot product is a no-op. -/ @[simp] lemma comp_equiv_dot_product_comp_equiv (e : m ≃ n) : (x ∘ e) ⬝ᵥ (y ∘ e) = x ⬝ᵥ y := by simp only [←dot_product_comp_equiv_symm, function.comp, equiv.apply_symm_apply] end non_unital_non_assoc_semiring section non_unital_non_assoc_semiring_decidable variables [decidable_eq m] [non_unital_non_assoc_semiring α] (u v w : m → α) @[simp] lemma diagonal_dot_product (i : m) : diagonal v i ⬝ᵥ w = v i * w i := have ∀ j ≠ i, diagonal v i j * w j = 0 := λ j hij, by simp [diagonal_apply_ne' _ hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma dot_product_diagonal (i : m) : v ⬝ᵥ diagonal w i = v i * w i := have ∀ j ≠ i, v j * diagonal w i j = 0 := λ j hij, by simp [diagonal_apply_ne' _ hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma dot_product_diagonal' (i : m) : v ⬝ᵥ (λ j, diagonal w j i) = v i * w i := have ∀ j ≠ i, v j * diagonal w j i = 0 := λ j hij, by simp [diagonal_apply_ne _ hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma single_dot_product (x : α) (i : m) : pi.single i x ⬝ᵥ v = x * v i := have ∀ j ≠ i, pi.single i x j * v j = 0 := λ j hij, by simp [pi.single_eq_of_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp @[simp] lemma dot_product_single (x : α) (i : m) : v ⬝ᵥ pi.single i x = v i * x := have ∀ j ≠ i, v j * pi.single i x j = 0 := λ j hij, by simp [pi.single_eq_of_ne hij], by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp end non_unital_non_assoc_semiring_decidable section non_assoc_semiring variables [non_assoc_semiring α] @[simp] lemma one_dot_product_one : (1 : n → α) ⬝ᵥ 1 = fintype.card n := by simp [dot_product, fintype.card] end non_assoc_semiring section non_unital_non_assoc_ring variables [non_unital_non_assoc_ring α] (u v w : m → α) @[simp] lemma neg_dot_product : -v ⬝ᵥ w = - (v ⬝ᵥ w) := by simp [dot_product] @[simp] lemma dot_product_neg : v ⬝ᵥ -w = - (v ⬝ᵥ w) := by simp [dot_product] @[simp] lemma sub_dot_product : (u - v) ⬝ᵥ w = u ⬝ᵥ w - v ⬝ᵥ w := by simp [sub_eq_add_neg] @[simp] lemma dot_product_sub : u ⬝ᵥ (v - w) = u ⬝ᵥ v - u ⬝ᵥ w := by simp [sub_eq_add_neg] end non_unital_non_assoc_ring section distrib_mul_action variables [monoid R] [has_mul α] [add_comm_monoid α] [distrib_mul_action R α] @[simp] lemma smul_dot_product [is_scalar_tower R α α] (x : R) (v w : m → α) : (x • v) ⬝ᵥ w = x • (v ⬝ᵥ w) := by simp [dot_product, finset.smul_sum, smul_mul_assoc] @[simp] lemma dot_product_smul [smul_comm_class R α α] (x : R) (v w : m → α) : v ⬝ᵥ (x • w) = x • (v ⬝ᵥ w) := by simp [dot_product, finset.smul_sum, mul_smul_comm] end distrib_mul_action section star_ring variables [non_unital_semiring α] [star_ring α] (v w : m → α) lemma star_dot_product_star : star v ⬝ᵥ star w = star (w ⬝ᵥ v) := by simp [dot_product] lemma star_dot_product : star v ⬝ᵥ w = star (star w ⬝ᵥ v) := by simp [dot_product] lemma dot_product_star : v ⬝ᵥ star w = star (w ⬝ᵥ star v) := by simp [dot_product] end star_ring end dot_product open_locale matrix /-- `M ⬝ N` is the usual product of matrices `M` and `N`, i.e. we have that `(M ⬝ N) i k` is the dot product of the `i`-th row of `M` by the `k`-th column of `N`. This is currently only defined when `m` is finite. -/ protected def mul [fintype m] [has_mul α] [add_comm_monoid α] (M : matrix l m α) (N : matrix m n α) : matrix l n α := λ i k, (λ j, M i j) ⬝ᵥ (λ j, N j k) localized "infixl (name := matrix.mul) ` ⬝ `:75 := matrix.mul" in matrix theorem mul_apply [fintype m] [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = ∑ j, M i j * N j k := rfl instance [fintype n] [has_mul α] [add_comm_monoid α] : has_mul (matrix n n α) := ⟨matrix.mul⟩ @[simp] theorem mul_eq_mul [fintype n] [has_mul α] [add_comm_monoid α] (M N : matrix n n α) : M * N = M ⬝ N := rfl theorem mul_apply' [fintype m] [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = (λ j, M i j) ⬝ᵥ (λ j, N j k) := rfl @[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) : -diagonal d = diagonal (λ i, -d i) := ((diagonal_add_monoid_hom n α).map_neg d).symm lemma sum_apply [add_comm_monoid α] (i : m) (j : n) (s : finset β) (g : β → matrix m n α) : (∑ c in s, g c) i j = ∑ c in s, g c i j := (congr_fun (s.sum_apply i g) j).trans (s.sum_apply j _) lemma two_mul_expl {R : Type*} [comm_ring R] (A B : matrix (fin 2) (fin 2) R) : (A * B) 0 0 = A 0 0 * B 0 0 + A 0 1 * B 1 0 ∧ (A * B) 0 1 = A 0 0 * B 0 1 + A 0 1 * B 1 1 ∧ (A * B) 1 0 = A 1 0 * B 0 0 + A 1 1 * B 1 0 ∧ (A * B) 1 1 = A 1 0 * B 0 1 + A 1 1 * B 1 1 := begin split, work_on_goal 2 {split}, work_on_goal 3 {split}, all_goals {simp only [matrix.mul_eq_mul], rw [matrix.mul_apply, finset.sum_fin_eq_sum_range, finset.sum_range_succ, finset.sum_range_succ], simp}, end section add_comm_monoid variables [add_comm_monoid α] [has_mul α] @[simp] lemma smul_mul [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α] (a : R) (M : matrix m n α) (N : matrix n l α) : (a • M) ⬝ N = a • M ⬝ N := by { ext, apply smul_dot_product } @[simp] lemma mul_smul [fintype n] [monoid R] [distrib_mul_action R α] [smul_comm_class R α α] (M : matrix m n α) (a : R) (N : matrix n l α) : M ⬝ (a • N) = a • M ⬝ N := by { ext, apply dot_product_smul } end add_comm_monoid section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring α] @[simp] protected theorem mul_zero [fintype n] (M : matrix m n α) : M ⬝ (0 : matrix n o α) = 0 := by { ext i j, apply dot_product_zero } @[simp] protected theorem zero_mul [fintype m] (M : matrix m n α) : (0 : matrix l m α) ⬝ M = 0 := by { ext i j, apply zero_dot_product } protected theorem mul_add [fintype n] (L : matrix m n α) (M N : matrix n o α) : L ⬝ (M + N) = L ⬝ M + L ⬝ N := by { ext i j, apply dot_product_add } protected theorem add_mul [fintype m] (L M : matrix l m α) (N : matrix m n α) : (L + M) ⬝ N = L ⬝ N + M ⬝ N := by { ext i j, apply add_dot_product } instance [fintype n] : non_unital_non_assoc_semiring (matrix n n α) := { mul := (*), add := (+), zero := 0, mul_zero := matrix.mul_zero, zero_mul := matrix.zero_mul, left_distrib := matrix.mul_add, right_distrib := matrix.add_mul, .. matrix.add_comm_monoid} @[simp] theorem diagonal_mul [fintype m] [decidable_eq m] (d : m → α) (M : matrix m n α) (i j) : (diagonal d).mul M i j = d i * M i j := diagonal_dot_product _ _ _ @[simp] theorem mul_diagonal [fintype n] [decidable_eq n] (d : n → α) (M : matrix m n α) (i j) : (M ⬝ diagonal d) i j = M i j * d j := by { rw ← diagonal_transpose, apply dot_product_diagonal } @[simp] theorem diagonal_mul_diagonal [fintype n] [decidable_eq n] (d₁ d₂ : n → α) : (diagonal d₁) ⬝ (diagonal d₂) = diagonal (λ i, d₁ i * d₂ i) := by ext i j; by_cases i = j; simp [h] theorem diagonal_mul_diagonal' [fintype n] [decidable_eq n] (d₁ d₂ : n → α) : diagonal d₁ * diagonal d₂ = diagonal (λ i, d₁ i * d₂ i) := diagonal_mul_diagonal _ _ lemma smul_eq_diagonal_mul [fintype m] [decidable_eq m] (M : matrix m n α) (a : α) : a • M = diagonal (λ _, a) ⬝ M := by { ext, simp } @[simp] lemma diag_col_mul_row (a b : n → α) : diag (col a ⬝ row b) = a * b := by { ext, simp [matrix.mul_apply, col, row] } /-- Left multiplication by a matrix, as an `add_monoid_hom` from matrices to matrices. -/ @[simps] def add_monoid_hom_mul_left [fintype m] (M : matrix l m α) : matrix m n α →+ matrix l n α := { to_fun := λ x, M ⬝ x, map_zero' := matrix.mul_zero _, map_add' := matrix.mul_add _ } /-- Right multiplication by a matrix, as an `add_monoid_hom` from matrices to matrices. -/ @[simps] def add_monoid_hom_mul_right [fintype m] (M : matrix m n α) : matrix l m α →+ matrix l n α := { to_fun := λ x, x ⬝ M, map_zero' := matrix.zero_mul _, map_add' := λ _ _, matrix.add_mul _ _ _ } protected lemma sum_mul [fintype m] (s : finset β) (f : β → matrix l m α) (M : matrix m n α) : (∑ a in s, f a) ⬝ M = ∑ a in s, f a ⬝ M := (add_monoid_hom_mul_right M : matrix l m α →+ _).map_sum f s protected lemma mul_sum [fintype m] (s : finset β) (f : β → matrix m n α) (M : matrix l m α) : M ⬝ ∑ a in s, f a = ∑ a in s, M ⬝ f a := (add_monoid_hom_mul_left M : matrix m n α →+ _).map_sum f s /-- This instance enables use with `smul_mul_assoc`. -/ instance semiring.is_scalar_tower [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α] : is_scalar_tower R (matrix n n α) (matrix n n α) := ⟨λ r m n, matrix.smul_mul r m n⟩ /-- This instance enables use with `mul_smul_comm`. -/ instance semiring.smul_comm_class [fintype n] [monoid R] [distrib_mul_action R α] [smul_comm_class R α α] : smul_comm_class R (matrix n n α) (matrix n n α) := ⟨λ r m n, (matrix.mul_smul m r n).symm⟩ end non_unital_non_assoc_semiring section non_assoc_semiring variables [non_assoc_semiring α] @[simp] protected theorem one_mul [fintype m] [decidable_eq m] (M : matrix m n α) : (1 : matrix m m α) ⬝ M = M := by ext i j; rw [← diagonal_one, diagonal_mul, one_mul] @[simp] protected theorem mul_one [fintype n] [decidable_eq n] (M : matrix m n α) : M ⬝ (1 : matrix n n α) = M := by ext i j; rw [← diagonal_one, mul_diagonal, mul_one] instance [fintype n] [decidable_eq n] : non_assoc_semiring (matrix n n α) := { one := 1, one_mul := matrix.one_mul, mul_one := matrix.mul_one, nat_cast := λ n, diagonal (λ _, n), nat_cast_zero := by ext; simp [nat.cast], nat_cast_succ := λ n, by ext; by_cases i = j; simp [nat.cast, *], .. matrix.non_unital_non_assoc_semiring } @[simp] lemma map_mul [fintype n] {L : matrix m n α} {M : matrix n o α} [non_assoc_semiring β] {f : α →+* β} : (L ⬝ M).map f = L.map f ⬝ M.map f := by { ext, simp [mul_apply, ring_hom.map_sum], } variables (α n) /-- `matrix.diagonal` as a `ring_hom`. -/ @[simps] def diagonal_ring_hom [fintype n] [decidable_eq n] : (n → α) →+* matrix n n α := { to_fun := diagonal, map_one' := diagonal_one, map_mul' := λ _ _, (diagonal_mul_diagonal' _ _).symm, .. diagonal_add_monoid_hom n α } end non_assoc_semiring section non_unital_semiring variables [non_unital_semiring α] [fintype m] [fintype n] protected theorem mul_assoc (L : matrix l m α) (M : matrix m n α) (N : matrix n o α) : (L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) := by { ext, apply dot_product_assoc } instance : non_unital_semiring (matrix n n α) := { mul_assoc := matrix.mul_assoc, ..matrix.non_unital_non_assoc_semiring } end non_unital_semiring section semiring variables [semiring α] instance [fintype n] [decidable_eq n] : semiring (matrix n n α) := { ..matrix.non_unital_semiring, ..matrix.non_assoc_semiring } end semiring section non_unital_non_assoc_ring variables [non_unital_non_assoc_ring α] [fintype n] @[simp] protected theorem neg_mul (M : matrix m n α) (N : matrix n o α) : (-M) ⬝ N = -(M ⬝ N) := by { ext, apply neg_dot_product } @[simp] protected theorem mul_neg (M : matrix m n α) (N : matrix n o α) : M ⬝ (-N) = -(M ⬝ N) := by { ext, apply dot_product_neg } protected theorem sub_mul (M M' : matrix m n α) (N : matrix n o α) : (M - M') ⬝ N = M ⬝ N - M' ⬝ N := by rw [sub_eq_add_neg, matrix.add_mul, matrix.neg_mul, sub_eq_add_neg] protected theorem mul_sub (M : matrix m n α) (N N' : matrix n o α) : M ⬝ (N - N') = M ⬝ N - M ⬝ N' := by rw [sub_eq_add_neg, matrix.mul_add, matrix.mul_neg, sub_eq_add_neg] instance : non_unital_non_assoc_ring (matrix n n α) := { ..matrix.non_unital_non_assoc_semiring, ..matrix.add_comm_group } end non_unital_non_assoc_ring instance [fintype n] [non_unital_ring α] : non_unital_ring (matrix n n α) := { ..matrix.non_unital_semiring, ..matrix.add_comm_group } instance [fintype n] [decidable_eq n] [non_assoc_ring α] : non_assoc_ring (matrix n n α) := { ..matrix.non_assoc_semiring, ..matrix.add_comm_group } instance [fintype n] [decidable_eq n] [ring α] : ring (matrix n n α) := { ..matrix.semiring, ..matrix.add_comm_group } section semiring variables [semiring α] lemma diagonal_pow [fintype n] [decidable_eq n] (v : n → α) (k : ℕ) : diagonal v ^ k = diagonal (v ^ k) := (map_pow (diagonal_ring_hom n α) v k).symm @[simp] lemma mul_mul_left [fintype n] (M : matrix m n α) (N : matrix n o α) (a : α) : of (λ i j, a * M i j) ⬝ N = a • (M ⬝ N) := smul_mul a M N /-- The ring homomorphism `α →+* matrix n n α` sending `a` to the diagonal matrix with `a` on the diagonal. -/ def scalar (n : Type u) [decidable_eq n] [fintype n] : α →+* matrix n n α := { to_fun := λ a, a • 1, map_one' := by simp, map_mul' := by { intros, ext, simp [mul_assoc], }, .. (smul_add_hom α _).flip (1 : matrix n n α) } section scalar variables [decidable_eq n] [fintype n] @[simp] lemma coe_scalar : (scalar n : α → matrix n n α) = λ a, a • 1 := rfl lemma scalar_apply_eq (a : α) (i : n) : scalar n a i i = a := by simp only [coe_scalar, smul_eq_mul, mul_one, one_apply_eq, pi.smul_apply] lemma scalar_apply_ne (a : α) (i j : n) (h : i ≠ j) : scalar n a i j = 0 := by simp only [h, coe_scalar, one_apply_ne, ne.def, not_false_iff, pi.smul_apply, smul_zero] lemma scalar_inj [nonempty n] {r s : α} : scalar n r = scalar n s ↔ r = s := begin split, { intro h, inhabit n, rw [← scalar_apply_eq r (arbitrary n), ← scalar_apply_eq s (arbitrary n), h] }, { rintro rfl, refl } end end scalar end semiring section comm_semiring variables [comm_semiring α] [fintype n] lemma smul_eq_mul_diagonal [decidable_eq n] (M : matrix m n α) (a : α) : a • M = M ⬝ diagonal (λ _, a) := by { ext, simp [mul_comm] } @[simp] lemma mul_mul_right (M : matrix m n α) (N : matrix n o α) (a : α) : M ⬝ of (λ i j, a * N i j) = a • (M ⬝ N) := mul_smul M a N lemma scalar.commute [decidable_eq n] (r : α) (M : matrix n n α) : commute (scalar n r) M := by simp [commute, semiconj_by] end comm_semiring section algebra variables [fintype n] [decidable_eq n] variables [comm_semiring R] [semiring α] [semiring β] [algebra R α] [algebra R β] instance : algebra R (matrix n n α) := { commutes' := λ r x, begin ext, simp [matrix.scalar, matrix.mul_apply, matrix.one_apply, algebra.commutes, smul_ite], end, smul_def' := λ r x, begin ext, simp [matrix.scalar, algebra.smul_def r], end, ..((matrix.scalar n).comp (algebra_map R α)) } lemma algebra_map_matrix_apply {r : R} {i j : n} : algebra_map R (matrix n n α) r i j = if i = j then algebra_map R α r else 0 := begin dsimp [algebra_map, algebra.to_ring_hom, matrix.scalar], split_ifs with h; simp [h, matrix.one_apply_ne], end lemma algebra_map_eq_diagonal (r : R) : algebra_map R (matrix n n α) r = diagonal (algebra_map R (n → α) r) := matrix.ext $ λ i j, algebra_map_matrix_apply @[simp] lemma algebra_map_eq_smul (r : R) : algebra_map R (matrix n n R) r = r • (1 : matrix n n R) := rfl lemma algebra_map_eq_diagonal_ring_hom : algebra_map R (matrix n n α) = (diagonal_ring_hom n α).comp (algebra_map R _) := ring_hom.ext algebra_map_eq_diagonal @[simp] lemma map_algebra_map (r : R) (f : α → β) (hf : f 0 = 0) (hf₂ : f (algebra_map R α r) = algebra_map R β r) : (algebra_map R (matrix n n α) r).map f = algebra_map R (matrix n n β) r := begin rw [algebra_map_eq_diagonal, algebra_map_eq_diagonal, diagonal_map hf], congr' 1 with x, simp only [hf₂, pi.algebra_map_apply] end variables (R) /-- `matrix.diagonal` as an `alg_hom`. -/ @[simps] def diagonal_alg_hom : (n → α) →ₐ[R] matrix n n α := { to_fun := diagonal, commutes' := λ r, (algebra_map_eq_diagonal r).symm, .. diagonal_ring_hom n α } end algebra end matrix /-! ### Bundled versions of `matrix.map` -/ namespace equiv /-- The `equiv` between spaces of matrices induced by an `equiv` between their coefficients. This is `matrix.map` as an `equiv`. -/ @[simps apply] def map_matrix (f : α ≃ β) : matrix m n α ≃ matrix m n β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, left_inv := λ M, matrix.ext $ λ _ _, f.symm_apply_apply _, right_inv := λ M, matrix.ext $ λ _ _, f.apply_symm_apply _, } @[simp] lemma map_matrix_refl : (equiv.refl α).map_matrix = equiv.refl (matrix m n α) := rfl @[simp] lemma map_matrix_symm (f : α ≃ β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃ _) := rfl @[simp] lemma map_matrix_trans (f : α ≃ β) (g : β ≃ γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃ _) := rfl end equiv namespace add_monoid_hom variables [add_zero_class α] [add_zero_class β] [add_zero_class γ] /-- The `add_monoid_hom` between spaces of matrices induced by an `add_monoid_hom` between their coefficients. This is `matrix.map` as an `add_monoid_hom`. -/ @[simps] def map_matrix (f : α →+ β) : matrix m n α →+ matrix m n β := { to_fun := λ M, M.map f, map_zero' := matrix.map_zero f f.map_zero, map_add' := matrix.map_add f f.map_add } @[simp] lemma map_matrix_id : (add_monoid_hom.id α).map_matrix = add_monoid_hom.id (matrix m n α) := rfl @[simp] lemma map_matrix_comp (f : β →+ γ) (g : α →+ β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m n α →+ _) := rfl end add_monoid_hom namespace add_equiv variables [has_add α] [has_add β] [has_add γ] /-- The `add_equiv` between spaces of matrices induced by an `add_equiv` between their coefficients. This is `matrix.map` as an `add_equiv`. -/ @[simps apply] def map_matrix (f : α ≃+ β) : matrix m n α ≃+ matrix m n β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, map_add' := matrix.map_add f f.map_add, .. f.to_equiv.map_matrix } @[simp] lemma map_matrix_refl : (add_equiv.refl α).map_matrix = add_equiv.refl (matrix m n α) := rfl @[simp] lemma map_matrix_symm (f : α ≃+ β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃+ _) := rfl @[simp] lemma map_matrix_trans (f : α ≃+ β) (g : β ≃+ γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃+ _) := rfl end add_equiv namespace linear_map variables [semiring R] [add_comm_monoid α] [add_comm_monoid β] [add_comm_monoid γ] variables [module R α] [module R β] [module R γ] /-- The `linear_map` between spaces of matrices induced by a `linear_map` between their coefficients. This is `matrix.map` as a `linear_map`. -/ @[simps] def map_matrix (f : α →ₗ[R] β) : matrix m n α →ₗ[R] matrix m n β := { to_fun := λ M, M.map f, map_add' := matrix.map_add f f.map_add, map_smul' := λ r, matrix.map_smul f r (f.map_smul r), } @[simp] lemma map_matrix_id : linear_map.id.map_matrix = (linear_map.id : matrix m n α →ₗ[R] _) := rfl @[simp] lemma map_matrix_comp (f : β →ₗ[R] γ) (g : α →ₗ[R] β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m n α →ₗ[R] _) := rfl end linear_map namespace linear_equiv variables [semiring R] [add_comm_monoid α] [add_comm_monoid β] [add_comm_monoid γ] variables [module R α] [module R β] [module R γ] /-- The `linear_equiv` between spaces of matrices induced by an `linear_equiv` between their coefficients. This is `matrix.map` as an `linear_equiv`. -/ @[simps apply] def map_matrix (f : α ≃ₗ[R] β) : matrix m n α ≃ₗ[R] matrix m n β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, .. f.to_equiv.map_matrix, .. f.to_linear_map.map_matrix } @[simp] lemma map_matrix_refl : (linear_equiv.refl R α).map_matrix = linear_equiv.refl R (matrix m n α) := rfl @[simp] lemma map_matrix_symm (f : α ≃ₗ[R] β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃ₗ[R] _) := rfl @[simp] lemma map_matrix_trans (f : α ≃ₗ[R] β) (g : β ≃ₗ[R] γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃ₗ[R] _) := rfl end linear_equiv namespace ring_hom variables [fintype m] [decidable_eq m] variables [non_assoc_semiring α] [non_assoc_semiring β] [non_assoc_semiring γ] /-- The `ring_hom` between spaces of square matrices induced by a `ring_hom` between their coefficients. This is `matrix.map` as a `ring_hom`. -/ @[simps] def map_matrix (f : α →+* β) : matrix m m α →+* matrix m m β := { to_fun := λ M, M.map f, map_one' := by simp, map_mul' := λ L M, matrix.map_mul, .. f.to_add_monoid_hom.map_matrix } @[simp] lemma map_matrix_id : (ring_hom.id α).map_matrix = ring_hom.id (matrix m m α) := rfl @[simp] lemma map_matrix_comp (f : β →+* γ) (g : α →+* β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m m α →+* _) := rfl end ring_hom namespace ring_equiv variables [fintype m] [decidable_eq m] variables [non_assoc_semiring α] [non_assoc_semiring β] [non_assoc_semiring γ] /-- The `ring_equiv` between spaces of square matrices induced by a `ring_equiv` between their coefficients. This is `matrix.map` as a `ring_equiv`. -/ @[simps apply] def map_matrix (f : α ≃+* β) : matrix m m α ≃+* matrix m m β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, .. f.to_ring_hom.map_matrix, .. f.to_add_equiv.map_matrix } @[simp] lemma map_matrix_refl : (ring_equiv.refl α).map_matrix = ring_equiv.refl (matrix m m α) := rfl @[simp] lemma map_matrix_symm (f : α ≃+* β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m m β ≃+* _) := rfl @[simp] lemma map_matrix_trans (f : α ≃+* β) (g : β ≃+* γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m m α ≃+* _) := rfl end ring_equiv namespace alg_hom variables [fintype m] [decidable_eq m] variables [comm_semiring R] [semiring α] [semiring β] [semiring γ] variables [algebra R α] [algebra R β] [algebra R γ] /-- The `alg_hom` between spaces of square matrices induced by a `alg_hom` between their coefficients. This is `matrix.map` as a `alg_hom`. -/ @[simps] def map_matrix (f : α →ₐ[R] β) : matrix m m α →ₐ[R] matrix m m β := { to_fun := λ M, M.map f, commutes' := λ r, matrix.map_algebra_map r f f.map_zero (f.commutes r), .. f.to_ring_hom.map_matrix } @[simp] lemma map_matrix_id : (alg_hom.id R α).map_matrix = alg_hom.id R (matrix m m α) := rfl @[simp] lemma map_matrix_comp (f : β →ₐ[R] γ) (g : α →ₐ[R] β) : f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m m α →ₐ[R] _) := rfl end alg_hom namespace alg_equiv variables [fintype m] [decidable_eq m] variables [comm_semiring R] [semiring α] [semiring β] [semiring γ] variables [algebra R α] [algebra R β] [algebra R γ] /-- The `alg_equiv` between spaces of square matrices induced by a `alg_equiv` between their coefficients. This is `matrix.map` as a `alg_equiv`. -/ @[simps apply] def map_matrix (f : α ≃ₐ[R] β) : matrix m m α ≃ₐ[R] matrix m m β := { to_fun := λ M, M.map f, inv_fun := λ M, M.map f.symm, .. f.to_alg_hom.map_matrix, .. f.to_ring_equiv.map_matrix } @[simp] lemma map_matrix_refl : alg_equiv.refl.map_matrix = (alg_equiv.refl : matrix m m α ≃ₐ[R] _) := rfl @[simp] lemma map_matrix_symm (f : α ≃ₐ[R] β) : f.map_matrix.symm = (f.symm.map_matrix : matrix m m β ≃ₐ[R] _) := rfl @[simp] lemma map_matrix_trans (f : α ≃ₐ[R] β) (g : β ≃ₐ[R] γ) : f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m m α ≃ₐ[R] _) := rfl end alg_equiv open_locale matrix namespace matrix /-- For two vectors `w` and `v`, `vec_mul_vec w v i j` is defined to be `w i * v j`. Put another way, `vec_mul_vec w v` is exactly `col w ⬝ row v`. -/ def vec_mul_vec [has_mul α] (w : m → α) (v : n → α) : matrix m n α := of $ λ x y, w x * v y -- TODO: set as an equation lemma for `vec_mul_vec`, see mathlib4#3024 lemma vec_mul_vec_apply [has_mul α] (w : m → α) (v : n → α) (i j) : vec_mul_vec w v i j = w i * v j := rfl lemma vec_mul_vec_eq [has_mul α] [add_comm_monoid α] (w : m → α) (v : n → α) : vec_mul_vec w v = (col w) ⬝ (row v) := by { ext i j, simp only [vec_mul_vec, mul_apply, fintype.univ_punit, finset.sum_singleton], refl } section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring α] /-- `mul_vec M v` is the matrix-vector product of `M` and `v`, where `v` is seen as a column matrix. Put another way, `mul_vec M v` is the vector whose entries are those of `M ⬝ col v` (see `col_mul_vec`). -/ def mul_vec [fintype n] (M : matrix m n α) (v : n → α) : m → α | i := (λ j, M i j) ⬝ᵥ v /-- `vec_mul v M` is the vector-matrix product of `v` and `M`, where `v` is seen as a row matrix. Put another way, `vec_mul v M` is the vector whose entries are those of `row v ⬝ M` (see `row_vec_mul`). -/ def vec_mul [fintype m] (v : m → α) (M : matrix m n α) : n → α | j := v ⬝ᵥ (λ i, M i j) /-- Left multiplication by a matrix, as an `add_monoid_hom` from vectors to vectors. -/ @[simps] def mul_vec.add_monoid_hom_left [fintype n] (v : n → α) : matrix m n α →+ m → α := { to_fun := λ M, mul_vec M v, map_zero' := by ext; simp [mul_vec]; refl, map_add' := λ x y, by { ext m, apply add_dot_product } } lemma mul_vec_diagonal [fintype m] [decidable_eq m] (v w : m → α) (x : m) : mul_vec (diagonal v) w x = v x * w x := diagonal_dot_product v w x lemma vec_mul_diagonal [fintype m] [decidable_eq m] (v w : m → α) (x : m) : vec_mul v (diagonal w) x = v x * w x := dot_product_diagonal' v w x /-- Associate the dot product of `mul_vec` to the left. -/ lemma dot_product_mul_vec [fintype n] [fintype m] [non_unital_semiring R] (v : m → R) (A : matrix m n R) (w : n → R) : v ⬝ᵥ mul_vec A w = vec_mul v A ⬝ᵥ w := by simp only [dot_product, vec_mul, mul_vec, finset.mul_sum, finset.sum_mul, mul_assoc]; exact finset.sum_comm @[simp] lemma mul_vec_zero [fintype n] (A : matrix m n α) : mul_vec A 0 = 0 := by { ext, simp [mul_vec] } @[simp] lemma zero_vec_mul [fintype m] (A : matrix m n α) : vec_mul 0 A = 0 := by { ext, simp [vec_mul] } @[simp] lemma zero_mul_vec [fintype n] (v : n → α) : mul_vec (0 : matrix m n α) v = 0 := by { ext, simp [mul_vec] } @[simp] lemma vec_mul_zero [fintype m] (v : m → α) : vec_mul v (0 : matrix m n α) = 0 := by { ext, simp [vec_mul] } lemma smul_mul_vec_assoc [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α] (a : R) (A : matrix m n α) (b : n → α) : (a • A).mul_vec b = a • (A.mul_vec b) := by { ext, apply smul_dot_product, } lemma mul_vec_add [fintype n] (A : matrix m n α) (x y : n → α) : A.mul_vec (x + y) = A.mul_vec x + A.mul_vec y := by { ext, apply dot_product_add } lemma add_mul_vec [fintype n] (A B : matrix m n α) (x : n → α) : (A + B).mul_vec x = A.mul_vec x + B.mul_vec x := by { ext, apply add_dot_product } lemma vec_mul_add [fintype m] (A B : matrix m n α) (x : m → α) : vec_mul x (A + B) = vec_mul x A + vec_mul x B := by { ext, apply dot_product_add } lemma add_vec_mul [fintype m] (A : matrix m n α) (x y : m → α) : vec_mul (x + y) A = vec_mul x A + vec_mul y A := by { ext, apply add_dot_product } lemma vec_mul_smul [fintype n] [monoid R] [non_unital_non_assoc_semiring S] [distrib_mul_action R S] [is_scalar_tower R S S] (M : matrix n m S) (b : R) (v : n → S) : M.vec_mul (b • v) = b • M.vec_mul v := by { ext i, simp only [vec_mul, dot_product, finset.smul_sum, pi.smul_apply, smul_mul_assoc] } lemma mul_vec_smul [fintype n] [monoid R] [non_unital_non_assoc_semiring S] [distrib_mul_action R S] [smul_comm_class R S S] (M : matrix m n S) (b : R) (v : n → S) : M.mul_vec (b • v) = b • M.mul_vec v := by { ext i, simp only [mul_vec, dot_product, finset.smul_sum, pi.smul_apply, mul_smul_comm] } @[simp] lemma mul_vec_single [fintype n] [decidable_eq n] [non_unital_non_assoc_semiring R] (M : matrix m n R) (j : n) (x : R) : M.mul_vec (pi.single j x) = (λ i, M i j * x) := funext $ λ i, dot_product_single _ _ _ @[simp] lemma single_vec_mul [fintype m] [decidable_eq m] [non_unital_non_assoc_semiring R] (M : matrix m n R) (i : m) (x : R) : vec_mul (pi.single i x) M = (λ j, x * M i j) := funext $ λ i, single_dot_product _ _ _ @[simp] lemma diagonal_mul_vec_single [fintype n] [decidable_eq n] [non_unital_non_assoc_semiring R] (v : n → R) (j : n) (x : R) : (diagonal v).mul_vec (pi.single j x) = pi.single j (v j * x) := begin ext i, rw mul_vec_diagonal, exact pi.apply_single (λ i x, v i * x) (λ i, mul_zero _) j x i, end @[simp] lemma single_vec_mul_diagonal [fintype n] [decidable_eq n] [non_unital_non_assoc_semiring R] (v : n → R) (j : n) (x : R) : vec_mul (pi.single j x) (diagonal v) = pi.single j (x * v j) := begin ext i, rw vec_mul_diagonal, exact pi.apply_single (λ i x, x * v i) (λ i, zero_mul _) j x i, end end non_unital_non_assoc_semiring section non_unital_semiring variables [non_unital_semiring α] @[simp] lemma vec_mul_vec_mul [fintype n] [fintype m] (v : m → α) (M : matrix m n α) (N : matrix n o α) : vec_mul (vec_mul v M) N = vec_mul v (M ⬝ N) := by { ext, apply dot_product_assoc } @[simp] lemma mul_vec_mul_vec [fintype n] [fintype o] (v : o → α) (M : matrix m n α) (N : matrix n o α) : mul_vec M (mul_vec N v) = mul_vec (M ⬝ N) v := by { ext, symmetry, apply dot_product_assoc } lemma star_mul_vec [fintype n] [star_ring α] (M : matrix m n α) (v : n → α) : star (M.mul_vec v) = vec_mul (star v) (Mᴴ) := funext $ λ i, (star_dot_product_star _ _).symm lemma star_vec_mul [fintype m] [star_ring α] (M : matrix m n α) (v : m → α) : star (M.vec_mul v) = (Mᴴ).mul_vec (star v) := funext $ λ i, (star_dot_product_star _ _).symm lemma mul_vec_conj_transpose [fintype m] [star_ring α] (A : matrix m n α) (x : m → α) : mul_vec Aᴴ x = star (vec_mul (star x) A) := funext $ λ i, star_dot_product _ _ lemma vec_mul_conj_transpose [fintype n] [star_ring α] (A : matrix m n α) (x : n → α) : vec_mul x Aᴴ = star (mul_vec A (star x)) := funext $ λ i, dot_product_star _ _ lemma mul_mul_apply [fintype n] (A B C : matrix n n α) (i j : n) : (A ⬝ B ⬝ C) i j = A i ⬝ᵥ (B.mul_vec (Cᵀ j)) := by { rw matrix.mul_assoc, simpa only [mul_apply, dot_product, mul_vec] } end non_unital_semiring section non_assoc_semiring variables [non_assoc_semiring α] lemma mul_vec_one [fintype n] (A : matrix m n α) : mul_vec A 1 = λ i, ∑ j, A i j := by ext; simp [mul_vec, dot_product] lemma vec_one_mul [fintype m] (A : matrix m n α) : vec_mul 1 A = λ j, ∑ i, A i j := by ext; simp [vec_mul, dot_product] variables [fintype m] [fintype n] [decidable_eq m] @[simp] lemma one_mul_vec (v : m → α) : mul_vec 1 v = v := by { ext, rw [←diagonal_one, mul_vec_diagonal, one_mul] } @[simp] lemma vec_mul_one (v : m → α) : vec_mul v 1 = v := by { ext, rw [←diagonal_one, vec_mul_diagonal, mul_one] } end non_assoc_semiring section non_unital_non_assoc_ring variables [non_unital_non_assoc_ring α] lemma neg_vec_mul [fintype m] (v : m → α) (A : matrix m n α) : vec_mul (-v) A = - vec_mul v A := by { ext, apply neg_dot_product } lemma vec_mul_neg [fintype m] (v : m → α) (A : matrix m n α) : vec_mul v (-A) = - vec_mul v A := by { ext, apply dot_product_neg } lemma neg_mul_vec [fintype n] (v : n → α) (A : matrix m n α) : mul_vec (-A) v = - mul_vec A v := by { ext, apply neg_dot_product } lemma mul_vec_neg [fintype n] (v : n → α) (A : matrix m n α) : mul_vec A (-v) = - mul_vec A v := by { ext, apply dot_product_neg } lemma sub_mul_vec [fintype n] (A B : matrix m n α) (x : n → α) : mul_vec (A - B) x = mul_vec A x - mul_vec B x := by simp [sub_eq_add_neg, add_mul_vec, neg_mul_vec] lemma vec_mul_sub [fintype m] (A B : matrix m n α) (x : m → α) : vec_mul x (A - B) = vec_mul x A - vec_mul x B := by simp [sub_eq_add_neg, vec_mul_add, vec_mul_neg] end non_unital_non_assoc_ring section non_unital_comm_semiring variables [non_unital_comm_semiring α] lemma mul_vec_transpose [fintype m] (A : matrix m n α) (x : m → α) : mul_vec Aᵀ x = vec_mul x A := by { ext, apply dot_product_comm } lemma vec_mul_transpose [fintype n] (A : matrix m n α) (x : n → α) : vec_mul x Aᵀ = mul_vec A x := by { ext, apply dot_product_comm } lemma mul_vec_vec_mul [fintype n] [fintype o] (A : matrix m n α) (B : matrix o n α) (x : o → α) : mul_vec A (vec_mul x B) = mul_vec (A ⬝ Bᵀ) x := by rw [← mul_vec_mul_vec, mul_vec_transpose] lemma vec_mul_mul_vec [fintype m] [fintype n] (A : matrix m n α) (B : matrix m o α) (x : n → α) : vec_mul (mul_vec A x) B = vec_mul x (Aᵀ ⬝ B) := by rw [← vec_mul_vec_mul, vec_mul_transpose] end non_unital_comm_semiring section comm_semiring variables [comm_semiring α] lemma mul_vec_smul_assoc [fintype n] (A : matrix m n α) (b : n → α) (a : α) : A.mul_vec (a • b) = a • (A.mul_vec b) := by { ext, apply dot_product_smul } end comm_semiring section transpose open_locale matrix @[simp] lemma transpose_transpose (M : matrix m n α) : Mᵀᵀ = M := by ext; refl @[simp] lemma transpose_zero [has_zero α] : (0 : matrix m n α)ᵀ = 0 := by ext i j; refl @[simp] lemma transpose_one [decidable_eq n] [has_zero α] [has_one α] : (1 : matrix n n α)ᵀ = 1 := begin ext i j, rw [transpose_apply, ←diagonal_one], by_cases i = j, { simp only [h, diagonal_apply_eq] }, { simp only [diagonal_apply_ne _ h, diagonal_apply_ne' _ h] } end @[simp] lemma transpose_add [has_add α] (M : matrix m n α) (N : matrix m n α) : (M + N)ᵀ = Mᵀ + Nᵀ := by { ext i j, simp } @[simp] lemma transpose_sub [has_sub α] (M : matrix m n α) (N : matrix m n α) : (M - N)ᵀ = Mᵀ - Nᵀ := by { ext i j, simp } @[simp] lemma transpose_mul [add_comm_monoid α] [comm_semigroup α] [fintype n] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᵀ = Nᵀ ⬝ Mᵀ := begin ext i j, apply dot_product_comm end @[simp] lemma transpose_smul {R : Type*} [has_smul R α] (c : R) (M : matrix m n α) : (c • M)ᵀ = c • Mᵀ := by { ext i j, refl } @[simp] lemma transpose_neg [has_neg α] (M : matrix m n α) : (- M)ᵀ = - Mᵀ := by ext i j; refl lemma transpose_map {f : α → β} {M : matrix m n α} : Mᵀ.map f = (M.map f)ᵀ := by { ext, refl } variables (m n α) /-- `matrix.transpose` as an `add_equiv` -/ @[simps apply] def transpose_add_equiv [has_add α] : matrix m n α ≃+ matrix n m α := { to_fun := transpose, inv_fun := transpose, left_inv := transpose_transpose, right_inv := transpose_transpose, map_add' := transpose_add } @[simp] lemma transpose_add_equiv_symm [has_add α] : (transpose_add_equiv m n α).symm = transpose_add_equiv n m α := rfl variables {m n α} lemma transpose_list_sum [add_monoid α] (l : list (matrix m n α)) : l.sumᵀ = (l.map transpose).sum := (transpose_add_equiv m n α).to_add_monoid_hom.map_list_sum l lemma transpose_multiset_sum [add_comm_monoid α] (s : multiset (matrix m n α)) : s.sumᵀ = (s.map transpose).sum := (transpose_add_equiv m n α).to_add_monoid_hom.map_multiset_sum s lemma transpose_sum [add_comm_monoid α] {ι : Type*} (s : finset ι) (M : ι → matrix m n α) : (∑ i in s, M i)ᵀ = ∑ i in s, (M i)ᵀ := (transpose_add_equiv m n α).to_add_monoid_hom.map_sum _ s variables (m n R α) /-- `matrix.transpose` as a `linear_map` -/ @[simps apply] def transpose_linear_equiv [semiring R] [add_comm_monoid α] [module R α] : matrix m n α ≃ₗ[R] matrix n m α := { map_smul' := transpose_smul, ..transpose_add_equiv m n α} @[simp] lemma transpose_linear_equiv_symm [semiring R] [add_comm_monoid α] [module R α] : (transpose_linear_equiv m n R α).symm = transpose_linear_equiv n m R α := rfl variables {m n R α} variables (m α) /-- `matrix.transpose` as a `ring_equiv` to the opposite ring -/ @[simps] def transpose_ring_equiv [add_comm_monoid α] [comm_semigroup α] [fintype m] : matrix m m α ≃+* (matrix m m α)ᵐᵒᵖ := { to_fun := λ M, mul_opposite.op (Mᵀ), inv_fun := λ M, M.unopᵀ, map_mul' := λ M N, (congr_arg mul_opposite.op (transpose_mul M N)).trans (mul_opposite.op_mul _ _), left_inv := λ M, transpose_transpose M, right_inv := λ M, mul_opposite.unop_injective $ transpose_transpose M.unop, ..(transpose_add_equiv m m α).trans mul_opposite.op_add_equiv } variables {m α} @[simp] lemma transpose_pow [comm_semiring α] [fintype m] [decidable_eq m] (M : matrix m m α) (k : ℕ) : (M ^ k)ᵀ = Mᵀ ^ k := mul_opposite.op_injective $ map_pow (transpose_ring_equiv m α) M k lemma transpose_list_prod [comm_semiring α] [fintype m] [decidable_eq m] (l : list (matrix m m α)) : l.prodᵀ = (l.map transpose).reverse.prod := (transpose_ring_equiv m α).unop_map_list_prod l variables (R m α) /-- `matrix.transpose` as an `alg_equiv` to the opposite ring -/ @[simps] def transpose_alg_equiv [comm_semiring R] [comm_semiring α] [fintype m] [decidable_eq m] [algebra R α] : matrix m m α ≃ₐ[R] (matrix m m α)ᵐᵒᵖ := { to_fun := λ M, mul_opposite.op (Mᵀ), commutes' := λ r, by simp only [algebra_map_eq_diagonal, diagonal_transpose, mul_opposite.algebra_map_apply], ..(transpose_add_equiv m m α).trans mul_opposite.op_add_equiv, ..transpose_ring_equiv m α } variables {R m α} end transpose section conj_transpose open_locale matrix /-- Tell `simp` what the entries are in a conjugate transposed matrix. Compare with `mul_apply`, `diagonal_apply_eq`, etc. -/ @[simp] lemma conj_transpose_apply [has_star α] (M : matrix m n α) (i j) : M.conj_transpose j i = star (M i j) := rfl @[simp] lemma conj_transpose_conj_transpose [has_involutive_star α] (M : matrix m n α) : Mᴴᴴ = M := matrix.ext $ by simp @[simp] lemma conj_transpose_zero [add_monoid α] [star_add_monoid α] : (0 : matrix m n α)ᴴ = 0 := matrix.ext $ by simp @[simp] lemma conj_transpose_one [decidable_eq n] [semiring α] [star_ring α]: (1 : matrix n n α)ᴴ = 1 := by simp [conj_transpose] @[simp] lemma conj_transpose_add [add_monoid α] [star_add_monoid α] (M N : matrix m n α) : (M + N)ᴴ = Mᴴ + Nᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_sub [add_group α] [star_add_monoid α] (M N : matrix m n α) : (M - N)ᴴ = Mᴴ - Nᴴ := matrix.ext $ by simp /-- Note that `star_module` is quite a strong requirement; as such we also provide the following variants which this lemma would not apply to: * `matrix.conj_transpose_smul_non_comm` * `matrix.conj_transpose_nsmul` * `matrix.conj_transpose_zsmul` * `matrix.conj_transpose_nat_cast_smul` * `matrix.conj_transpose_int_cast_smul` * `matrix.conj_transpose_inv_nat_cast_smul` * `matrix.conj_transpose_inv_int_cast_smul` * `matrix.conj_transpose_rat_smul` * `matrix.conj_transpose_rat_cast_smul` -/ @[simp] lemma conj_transpose_smul [has_star R] [has_star α] [has_smul R α] [star_module R α] (c : R) (M : matrix m n α) : (c • M)ᴴ = star c • Mᴴ := matrix.ext $ λ i j, star_smul _ _ @[simp] lemma conj_transpose_smul_non_comm [has_star R] [has_star α] [has_smul R α] [has_smul Rᵐᵒᵖ α] (c : R) (M : matrix m n α) (h : ∀ (r : R) (a : α), star (r • a) = mul_opposite.op (star r) • star a) : (c • M)ᴴ = mul_opposite.op (star c) • Mᴴ := matrix.ext $ by simp [h] @[simp] lemma conj_transpose_smul_self [semigroup α] [star_semigroup α] (c : α) (M : matrix m n α) : (c • M)ᴴ = mul_opposite.op (star c) • Mᴴ := conj_transpose_smul_non_comm c M star_mul @[simp] lemma conj_transpose_nsmul [add_monoid α] [star_add_monoid α] (c : ℕ) (M : matrix m n α) : (c • M)ᴴ = c • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_zsmul [add_group α] [star_add_monoid α] (c : ℤ) (M : matrix m n α) : (c • M)ᴴ = c • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_nat_cast_smul [semiring R] [add_comm_monoid α] [star_add_monoid α] [module R α] (c : ℕ) (M : matrix m n α) : ((c : R) • M)ᴴ = (c : R) • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_int_cast_smul [ring R] [add_comm_group α] [star_add_monoid α] [module R α] (c : ℤ) (M : matrix m n α) : ((c : R) • M)ᴴ = (c : R) • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_inv_nat_cast_smul [division_semiring R] [add_comm_monoid α] [star_add_monoid α] [module R α] (c : ℕ) (M : matrix m n α) : ((c : R)⁻¹ • M)ᴴ = (c : R)⁻¹ • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_inv_int_cast_smul [division_ring R] [add_comm_group α] [star_add_monoid α] [module R α] (c : ℤ) (M : matrix m n α) : ((c : R)⁻¹ • M)ᴴ = (c : R)⁻¹ • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_rat_cast_smul [division_ring R] [add_comm_group α] [star_add_monoid α] [module R α] (c : ℚ) (M : matrix m n α) : ((c : R) • M)ᴴ = (c : R) • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_rat_smul [add_comm_group α] [star_add_monoid α] [module ℚ α] (c : ℚ) (M : matrix m n α) : (c • M)ᴴ = c • Mᴴ := matrix.ext $ by simp @[simp] lemma conj_transpose_mul [fintype n] [non_unital_semiring α] [star_ring α] (M : matrix m n α) (N : matrix n l α) : (M ⬝ N)ᴴ = Nᴴ ⬝ Mᴴ := matrix.ext $ by simp [mul_apply] @[simp] lemma conj_transpose_neg [add_group α] [star_add_monoid α] (M : matrix m n α) : (- M)ᴴ = - Mᴴ := matrix.ext $ by simp lemma conj_transpose_map [has_star α] [has_star β] {A : matrix m n α} (f : α → β) (hf : function.semiconj f star star) : Aᴴ.map f = (A.map f)ᴴ := matrix.ext $ λ i j, hf _ variables (m n α) /-- `matrix.conj_transpose` as an `add_equiv` -/ @[simps apply] def conj_transpose_add_equiv [add_monoid α] [star_add_monoid α] : matrix m n α ≃+ matrix n m α := { to_fun := conj_transpose, inv_fun := conj_transpose, left_inv := conj_transpose_conj_transpose, right_inv := conj_transpose_conj_transpose, map_add' := conj_transpose_add } @[simp] lemma conj_transpose_add_equiv_symm [add_monoid α] [star_add_monoid α] : (conj_transpose_add_equiv m n α).symm = conj_transpose_add_equiv n m α := rfl variables {m n α} lemma conj_transpose_list_sum [add_monoid α] [star_add_monoid α] (l : list (matrix m n α)) : l.sumᴴ = (l.map conj_transpose).sum := (conj_transpose_add_equiv m n α).to_add_monoid_hom.map_list_sum l lemma conj_transpose_multiset_sum [add_comm_monoid α] [star_add_monoid α] (s : multiset (matrix m n α)) : s.sumᴴ = (s.map conj_transpose).sum := (conj_transpose_add_equiv m n α).to_add_monoid_hom.map_multiset_sum s lemma conj_transpose_sum [add_comm_monoid α] [star_add_monoid α] {ι : Type*} (s : finset ι) (M : ι → matrix m n α) : (∑ i in s, M i)ᴴ = ∑ i in s, (M i)ᴴ := (conj_transpose_add_equiv m n α).to_add_monoid_hom.map_sum _ s variables (m n R α) /-- `matrix.conj_transpose` as a `linear_map` -/ @[simps apply] def conj_transpose_linear_equiv [comm_semiring R] [star_ring R] [add_comm_monoid α] [star_add_monoid α] [module R α] [star_module R α] : matrix m n α ≃ₗ⋆[R] matrix n m α := { map_smul' := conj_transpose_smul, ..conj_transpose_add_equiv m n α} @[simp] lemma conj_transpose_linear_equiv_symm [comm_semiring R] [star_ring R] [add_comm_monoid α] [star_add_monoid α] [module R α] [star_module R α] : (conj_transpose_linear_equiv m n R α).symm = conj_transpose_linear_equiv n m R α := rfl variables {m n R α} variables (m α) /-- `matrix.conj_transpose` as a `ring_equiv` to the opposite ring -/ @[simps] def conj_transpose_ring_equiv [semiring α] [star_ring α] [fintype m] : matrix m m α ≃+* (matrix m m α)ᵐᵒᵖ := { to_fun := λ M, mul_opposite.op (Mᴴ), inv_fun := λ M, M.unopᴴ, map_mul' := λ M N, (congr_arg mul_opposite.op (conj_transpose_mul M N)).trans (mul_opposite.op_mul _ _), ..(conj_transpose_add_equiv m m α).trans mul_opposite.op_add_equiv } variables {m α} @[simp] lemma conj_transpose_pow [semiring α] [star_ring α] [fintype m] [decidable_eq m] (M : matrix m m α) (k : ℕ) : (M ^ k)ᴴ = Mᴴ ^ k := mul_opposite.op_injective $ map_pow (conj_transpose_ring_equiv m α) M k lemma conj_transpose_list_prod [semiring α] [star_ring α] [fintype m] [decidable_eq m] (l : list (matrix m m α)) : l.prodᴴ = (l.map conj_transpose).reverse.prod := (conj_transpose_ring_equiv m α).unop_map_list_prod l end conj_transpose section star /-- When `α` has a star operation, square matrices `matrix n n α` have a star operation equal to `matrix.conj_transpose`. -/ instance [has_star α] : has_star (matrix n n α) := {star := conj_transpose} lemma star_eq_conj_transpose [has_star α] (M : matrix m m α) : star M = Mᴴ := rfl @[simp] lemma star_apply [has_star α] (M : matrix n n α) (i j) : (star M) i j = star (M j i) := rfl instance [has_involutive_star α] : has_involutive_star (matrix n n α) := { star_involutive := conj_transpose_conj_transpose } /-- When `α` is a `*`-additive monoid, `matrix.has_star` is also a `*`-additive monoid. -/ instance [add_monoid α] [star_add_monoid α] : star_add_monoid (matrix n n α) := { star_add := conj_transpose_add } instance [has_star α] [has_star β] [has_smul α β] [star_module α β] : star_module α (matrix n n β) := { star_smul := conj_transpose_smul } /-- When `α` is a `*`-(semi)ring, `matrix.has_star` is also a `*`-(semi)ring. -/ instance [fintype n] [non_unital_semiring α] [star_ring α] : star_ring (matrix n n α) := { star_add := conj_transpose_add, star_mul := conj_transpose_mul, } /-- A version of `star_mul` for `⬝` instead of `*`. -/ lemma star_mul [fintype n] [non_unital_semiring α] [star_ring α] (M N : matrix n n α) : star (M ⬝ N) = star N ⬝ star M := conj_transpose_mul _ _ end star /-- Given maps `(r_reindex : l → m)` and `(c_reindex : o → n)` reindexing the rows and columns of a matrix `M : matrix m n α`, the matrix `M.submatrix r_reindex c_reindex : matrix l o α` is defined by `(M.submatrix r_reindex c_reindex) i j = M (r_reindex i) (c_reindex j)` for `(i,j) : l × o`. Note that the total number of row and columns does not have to be preserved. -/ def submatrix (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) : matrix l o α := of $ λ i j, A (r_reindex i) (c_reindex j) @[simp] lemma submatrix_apply (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) (i j) : A.submatrix r_reindex c_reindex i j = A (r_reindex i) (c_reindex j) := rfl @[simp] lemma submatrix_id_id (A : matrix m n α) : A.submatrix id id = A := ext $ λ _ _, rfl @[simp] lemma submatrix_submatrix {l₂ o₂ : Type*} (A : matrix m n α) (r₁ : l → m) (c₁ : o → n) (r₂ : l₂ → l) (c₂ : o₂ → o) : (A.submatrix r₁ c₁).submatrix r₂ c₂ = A.submatrix (r₁ ∘ r₂) (c₁ ∘ c₂) := ext $ λ _ _, rfl @[simp] lemma transpose_submatrix (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) : (A.submatrix r_reindex c_reindex)ᵀ = Aᵀ.submatrix c_reindex r_reindex := ext $ λ _ _, rfl @[simp] lemma conj_transpose_submatrix [has_star α] (A : matrix m n α) (r_reindex : l → m) (c_reindex : o → n) : (A.submatrix r_reindex c_reindex)ᴴ = Aᴴ.submatrix c_reindex r_reindex := ext $ λ _ _, rfl lemma submatrix_add [has_add α] (A B : matrix m n α) : ((A + B).submatrix : (l → m) → (o → n) → matrix l o α) = A.submatrix + B.submatrix := rfl lemma submatrix_neg [has_neg α] (A : matrix m n α) : ((-A).submatrix : (l → m) → (o → n) → matrix l o α) = -A.submatrix := rfl lemma submatrix_sub [has_sub α] (A B : matrix m n α) : ((A - B).submatrix : (l → m) → (o → n) → matrix l o α) = A.submatrix - B.submatrix := rfl @[simp] lemma submatrix_zero [has_zero α] : ((0 : matrix m n α).submatrix : (l → m) → (o → n) → matrix l o α) = 0 := rfl lemma submatrix_smul {R : Type*} [has_smul R α] (r : R) (A : matrix m n α) : ((r • A : matrix m n α).submatrix : (l → m) → (o → n) → matrix l o α) = r • A.submatrix := rfl lemma submatrix_map (f : α → β) (e₁ : l → m) (e₂ : o → n) (A : matrix m n α) : (A.map f).submatrix e₁ e₂ = (A.submatrix e₁ e₂).map f := rfl /-- Given a `(m × m)` diagonal matrix defined by a map `d : m → α`, if the reindexing map `e` is injective, then the resulting matrix is again diagonal. -/ lemma submatrix_diagonal [has_zero α] [decidable_eq m] [decidable_eq l] (d : m → α) (e : l → m) (he : function.injective e) : (diagonal d).submatrix e e = diagonal (d ∘ e) := ext $ λ i j, begin rw submatrix_apply, by_cases h : i = j, { rw [h, diagonal_apply_eq, diagonal_apply_eq], }, { rw [diagonal_apply_ne _ h, diagonal_apply_ne _ (he.ne h)], }, end lemma submatrix_one [has_zero α] [has_one α] [decidable_eq m] [decidable_eq l] (e : l → m) (he : function.injective e) : (1 : matrix m m α).submatrix e e = 1 := submatrix_diagonal _ e he lemma submatrix_mul [fintype n] [fintype o] [has_mul α] [add_comm_monoid α] {p q : Type*} (M : matrix m n α) (N : matrix n p α) (e₁ : l → m) (e₂ : o → n) (e₃ : q → p) (he₂ : function.bijective e₂) : (M ⬝ N).submatrix e₁ e₃ = (M.submatrix e₁ e₂) ⬝ (N.submatrix e₂ e₃) := ext $ λ _ _, (he₂.sum_comp _).symm lemma diag_submatrix (A : matrix m m α) (e : l → m) : diag (A.submatrix e e) = A.diag ∘ e := rfl /-! `simp` lemmas for `matrix.submatrix`s interaction with `matrix.diagonal`, `1`, and `matrix.mul` for when the mappings are bundled. -/ @[simp] lemma submatrix_diagonal_embedding [has_zero α] [decidable_eq m] [decidable_eq l] (d : m → α) (e : l ↪ m) : (diagonal d).submatrix e e = diagonal (d ∘ e) := submatrix_diagonal d e e.injective @[simp] lemma submatrix_diagonal_equiv [has_zero α] [decidable_eq m] [decidable_eq l] (d : m → α) (e : l ≃ m) : (diagonal d).submatrix e e = diagonal (d ∘ e) := submatrix_diagonal d e e.injective @[simp] lemma submatrix_one_embedding [has_zero α] [has_one α] [decidable_eq m] [decidable_eq l] (e : l ↪ m) : (1 : matrix m m α).submatrix e e = 1 := submatrix_one e e.injective @[simp] lemma submatrix_one_equiv [has_zero α] [has_one α] [decidable_eq m] [decidable_eq l] (e : l ≃ m) : (1 : matrix m m α).submatrix e e = 1 := submatrix_one e e.injective @[simp] lemma submatrix_mul_equiv [fintype n] [fintype o] [add_comm_monoid α] [has_mul α] {p q : Type*} (M : matrix m n α) (N : matrix n p α) (e₁ : l → m) (e₂ : o ≃ n) (e₃ : q → p) : (M.submatrix e₁ e₂) ⬝ (N.submatrix e₂ e₃) = (M ⬝ N).submatrix e₁ e₃ := (submatrix_mul M N e₁ e₂ e₃ e₂.bijective).symm lemma submatrix_mul_vec_equiv [fintype n] [fintype o] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : o → α) (e₁ : l → m) (e₂ : o ≃ n) : (M.submatrix e₁ e₂).mul_vec v = M.mul_vec (v ∘ e₂.symm) ∘ e₁ := funext $ λ i, eq.symm (dot_product_comp_equiv_symm _ _ _) lemma submatrix_vec_mul_equiv [fintype l] [fintype m] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : l → α) (e₁ : l ≃ m) (e₂ : o → n) : vec_mul v (M.submatrix e₁ e₂) = vec_mul (v ∘ e₁.symm) M ∘ e₂ := funext $ λ i, eq.symm (comp_equiv_symm_dot_product _ _ _) lemma mul_submatrix_one [fintype n] [finite o] [non_assoc_semiring α] [decidable_eq o] (e₁ : n ≃ o) (e₂ : l → o) (M : matrix m n α) : M ⬝ (1 : matrix o o α).submatrix e₁ e₂ = submatrix M id (e₁.symm ∘ e₂) := begin casesI nonempty_fintype o, let A := M.submatrix id e₁.symm, have : M = A.submatrix id e₁, { simp only [submatrix_submatrix, function.comp.right_id, submatrix_id_id, equiv.symm_comp_self], }, rw [this, submatrix_mul_equiv], simp only [matrix.mul_one, submatrix_submatrix, function.comp.right_id, submatrix_id_id, equiv.symm_comp_self], end lemma one_submatrix_mul [fintype m] [finite o] [non_assoc_semiring α] [decidable_eq o] (e₁ : l → o) (e₂ : m ≃ o) (M : matrix m n α) : ((1 : matrix o o α).submatrix e₁ e₂).mul M = submatrix M (e₂.symm ∘ e₁) id := begin casesI nonempty_fintype o, let A := M.submatrix e₂.symm id, have : M = A.submatrix e₂ id, { simp only [submatrix_submatrix, function.comp.right_id, submatrix_id_id, equiv.symm_comp_self], }, rw [this, submatrix_mul_equiv], simp only [matrix.one_mul, submatrix_submatrix, function.comp.right_id, submatrix_id_id, equiv.symm_comp_self], end /-- The natural map that reindexes a matrix's rows and columns with equivalent types is an equivalence. -/ def reindex (eₘ : m ≃ l) (eₙ : n ≃ o) : matrix m n α ≃ matrix l o α := { to_fun := λ M, M.submatrix eₘ.symm eₙ.symm, inv_fun := λ M, M.submatrix eₘ eₙ, left_inv := λ M, by simp, right_inv := λ M, by simp, } @[simp] lemma reindex_apply (eₘ : m ≃ l) (eₙ : n ≃ o) (M : matrix m n α) : reindex eₘ eₙ M = M.submatrix eₘ.symm eₙ.symm := rfl @[simp] lemma reindex_refl_refl (A : matrix m n α) : reindex (equiv.refl _) (equiv.refl _) A = A := A.submatrix_id_id @[simp] lemma reindex_symm (eₘ : m ≃ l) (eₙ : n ≃ o) : (reindex eₘ eₙ).symm = (reindex eₘ.symm eₙ.symm : matrix l o α ≃ _) := rfl @[simp] lemma reindex_trans {l₂ o₂ : Type*} (eₘ : m ≃ l) (eₙ : n ≃ o) (eₘ₂ : l ≃ l₂) (eₙ₂ : o ≃ o₂) : (reindex eₘ eₙ).trans (reindex eₘ₂ eₙ₂) = (reindex (eₘ.trans eₘ₂) (eₙ.trans eₙ₂) : matrix m n α ≃ _) := equiv.ext $ λ A, (A.submatrix_submatrix eₘ.symm eₙ.symm eₘ₂.symm eₙ₂.symm : _) lemma transpose_reindex (eₘ : m ≃ l) (eₙ : n ≃ o) (M : matrix m n α) : (reindex eₘ eₙ M)ᵀ = (reindex eₙ eₘ Mᵀ) := rfl lemma conj_transpose_reindex [has_star α] (eₘ : m ≃ l) (eₙ : n ≃ o) (M : matrix m n α) : (reindex eₘ eₙ M)ᴴ = (reindex eₙ eₘ Mᴴ) := rfl @[simp] lemma submatrix_mul_transpose_submatrix [fintype m] [fintype n] [add_comm_monoid α] [has_mul α] (e : m ≃ n) (M : matrix m n α) : (M.submatrix id e) ⬝ (Mᵀ).submatrix e id = M ⬝ Mᵀ := by rw [submatrix_mul_equiv, submatrix_id_id] /-- The left `n × l` part of a `n × (l+r)` matrix. -/ @[reducible] def sub_left {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin l) α := submatrix A id (fin.cast_add r) /-- The right `n × r` part of a `n × (l+r)` matrix. -/ @[reducible] def sub_right {m l r : nat} (A : matrix (fin m) (fin (l + r)) α) : matrix (fin m) (fin r) α := submatrix A id (fin.nat_add l) /-- The top `u × n` part of a `(u+d) × n` matrix. -/ @[reducible] def sub_up {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin u) (fin n) α := submatrix A (fin.cast_add d) id /-- The bottom `d × n` part of a `(u+d) × n` matrix. -/ @[reducible] def sub_down {d u n : nat} (A : matrix (fin (u + d)) (fin n) α) : matrix (fin d) (fin n) α := submatrix A (fin.nat_add u) id /-- The top-right `u × r` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_up_right {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin r) α := sub_up (sub_right A) /-- The bottom-right `d × r` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_down_right {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin r) α := sub_down (sub_right A) /-- The top-left `u × l` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_up_left {d u l r : nat} (A : matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin u) (fin (l)) α := sub_up (sub_left A) /-- The bottom-left `d × l` part of a `(u+d) × (l+r)` matrix. -/ @[reducible] def sub_down_left {d u l r : nat} (A: matrix (fin (u + d)) (fin (l + r)) α) : matrix (fin d) (fin (l)) α := sub_down (sub_left A) section row_col /-! ### `row_col` section Simplification lemmas for `matrix.row` and `matrix.col`. -/ open_locale matrix @[simp] lemma col_add [has_add α] (v w : m → α) : col (v + w) = col v + col w := by { ext, refl } @[simp] lemma col_smul [has_smul R α] (x : R) (v : m → α) : col (x • v) = x • col v := by { ext, refl } @[simp] lemma row_add [has_add α] (v w : m → α) : row (v + w) = row v + row w := by { ext, refl } @[simp] lemma row_smul [has_smul R α] (x : R) (v : m → α) : row (x • v) = x • row v := by { ext, refl } @[simp] lemma transpose_col (v : m → α) : (matrix.col v)ᵀ = matrix.row v := by { ext, refl } @[simp] lemma transpose_row (v : m → α) : (matrix.row v)ᵀ = matrix.col v := by { ext, refl } @[simp] lemma conj_transpose_col [has_star α] (v : m → α) : (col v)ᴴ = row (star v) := by { ext, refl } @[simp] lemma conj_transpose_row [has_star α] (v : m → α) : (row v)ᴴ = col (star v) := by { ext, refl } lemma row_vec_mul [fintype m] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : m → α) : matrix.row (matrix.vec_mul v M) = matrix.row v ⬝ M := by {ext, refl} lemma col_vec_mul [fintype m] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : m → α) : matrix.col (matrix.vec_mul v M) = (matrix.row v ⬝ M)ᵀ := by {ext, refl} lemma col_mul_vec [fintype n] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : n → α) : matrix.col (matrix.mul_vec M v) = M ⬝ matrix.col v := by {ext, refl} lemma row_mul_vec [fintype n] [non_unital_non_assoc_semiring α] (M : matrix m n α) (v : n → α) : matrix.row (matrix.mul_vec M v) = (M ⬝ matrix.col v)ᵀ := by {ext, refl} @[simp] lemma row_mul_col_apply [fintype m] [has_mul α] [add_comm_monoid α] (v w : m → α) (i j) : (row v ⬝ col w) i j = v ⬝ᵥ w := rfl end row_col section update /-- Update, i.e. replace the `i`th row of matrix `A` with the values in `b`. -/ def update_row [decidable_eq m] (M : matrix m n α) (i : m) (b : n → α) : matrix m n α := of $ function.update M i b /-- Update, i.e. replace the `j`th column of matrix `A` with the values in `b`. -/ def update_column [decidable_eq n] (M : matrix m n α) (j : n) (b : m → α) : matrix m n α := of $ λ i, function.update (M i) j (b i) variables {M : matrix m n α} {i : m} {j : n} {b : n → α} {c : m → α} @[simp] lemma update_row_self [decidable_eq m] : update_row M i b i = b := function.update_same i b M @[simp] lemma update_column_self [decidable_eq n] : update_column M j c i j = c i := function.update_same j (c i) (M i) @[simp] lemma update_row_ne [decidable_eq m] {i' : m} (i_ne : i' ≠ i) : update_row M i b i' = M i' := function.update_noteq i_ne b M @[simp] lemma update_column_ne [decidable_eq n] {j' : n} (j_ne : j' ≠ j) : update_column M j c i j' = M i j' := function.update_noteq j_ne (c i) (M i) lemma update_row_apply [decidable_eq m] {i' : m} : update_row M i b i' j = if i' = i then b j else M i' j := begin by_cases i' = i, { rw [h, update_row_self, if_pos rfl] }, { rwa [update_row_ne h, if_neg h] } end lemma update_column_apply [decidable_eq n] {j' : n} : update_column M j c i j' = if j' = j then c i else M i j' := begin by_cases j' = j, { rw [h, update_column_self, if_pos rfl] }, { rwa [update_column_ne h, if_neg h] } end @[simp] lemma update_column_subsingleton [subsingleton n] (A : matrix m n R) (i : n) (b : m → R) : A.update_column i b = (col b).submatrix id (function.const n ()) := begin ext x y, simp [update_column_apply, subsingleton.elim i y] end @[simp] lemma update_row_subsingleton [subsingleton m] (A : matrix m n R) (i : m) (b : n → R) : A.update_row i b = (row b).submatrix (function.const m ()) id := begin ext x y, simp [update_column_apply, subsingleton.elim i x] end lemma map_update_row [decidable_eq m] (f : α → β) : map (update_row M i b) f = update_row (M.map f) i (f ∘ b) := begin ext i' j', rw [update_row_apply, map_apply, map_apply, update_row_apply], exact apply_ite f _ _ _, end lemma map_update_column [decidable_eq n] (f : α → β) : map (update_column M j c) f = update_column (M.map f) j (f ∘ c) := begin ext i' j', rw [update_column_apply, map_apply, map_apply, update_column_apply], exact apply_ite f _ _ _, end lemma update_row_transpose [decidable_eq n] : update_row Mᵀ j c = (update_column M j c)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end lemma update_column_transpose [decidable_eq m] : update_column Mᵀ i b = (update_row M i b)ᵀ := begin ext i' j, rw [transpose_apply, update_row_apply, update_column_apply], refl end lemma update_row_conj_transpose [decidable_eq n] [has_star α] : update_row Mᴴ j (star c) = (update_column M j c)ᴴ := begin rw [conj_transpose, conj_transpose, transpose_map, transpose_map, update_row_transpose, map_update_column], refl, end lemma update_column_conj_transpose [decidable_eq m] [has_star α] : update_column Mᴴ i (star b) = (update_row M i b)ᴴ := begin rw [conj_transpose, conj_transpose, transpose_map, transpose_map, update_column_transpose, map_update_row], refl, end @[simp] lemma update_row_eq_self [decidable_eq m] (A : matrix m n α) (i : m) : A.update_row i (A i) = A := function.update_eq_self i A @[simp] lemma update_column_eq_self [decidable_eq n] (A : matrix m n α) (i : n) : A.update_column i (λ j, A j i) = A := funext $ λ j, function.update_eq_self i (A j) lemma diagonal_update_column_single [decidable_eq n] [has_zero α] (v : n → α) (i : n) (x : α): (diagonal v).update_column i (pi.single i x) = diagonal (function.update v i x) := begin ext j k, obtain rfl | hjk := eq_or_ne j k, { rw [diagonal_apply_eq], obtain rfl | hji := eq_or_ne j i, { rw [update_column_self, pi.single_eq_same, function.update_same], }, { rw [update_column_ne hji, diagonal_apply_eq, function.update_noteq hji], } }, { rw [diagonal_apply_ne _ hjk], obtain rfl | hki := eq_or_ne k i, { rw [update_column_self, pi.single_eq_of_ne hjk] }, { rw [update_column_ne hki, diagonal_apply_ne _ hjk] } } end lemma diagonal_update_row_single [decidable_eq n] [has_zero α] (v : n → α) (i : n) (x : α): (diagonal v).update_row i (pi.single i x) = diagonal (function.update v i x) := by rw [←diagonal_transpose, update_row_transpose, diagonal_update_column_single, diagonal_transpose] /-! Updating rows and columns commutes in the obvious way with reindexing the matrix. -/ lemma update_row_submatrix_equiv [decidable_eq l] [decidable_eq m] (A : matrix m n α) (i : l) (r : o → α) (e : l ≃ m) (f : o ≃ n) : update_row (A.submatrix e f) i r = (A.update_row (e i) (λ j, r (f.symm j))).submatrix e f := begin ext i' j, simp only [submatrix_apply, update_row_apply, equiv.apply_eq_iff_eq, equiv.symm_apply_apply], end lemma submatrix_update_row_equiv [decidable_eq l] [decidable_eq m] (A : matrix m n α) (i : m) (r : n → α) (e : l ≃ m) (f : o ≃ n) : (A.update_row i r).submatrix e f = update_row (A.submatrix e f) (e.symm i) (λ i, r (f i)) := eq.trans (by simp_rw equiv.apply_symm_apply) (update_row_submatrix_equiv A _ _ e f).symm lemma update_column_submatrix_equiv [decidable_eq o] [decidable_eq n] (A : matrix m n α) (j : o) (c : l → α) (e : l ≃ m) (f : o ≃ n) : update_column (A.submatrix e f) j c = (A.update_column (f j) (λ i, c (e.symm i))).submatrix e f := by simpa only [←transpose_submatrix, update_row_transpose] using congr_arg transpose (update_row_submatrix_equiv Aᵀ j c f e) lemma submatrix_update_column_equiv [decidable_eq o] [decidable_eq n] (A : matrix m n α) (j : n) (c : m → α) (e : l ≃ m) (f : o ≃ n) : (A.update_column j c).submatrix e f = update_column (A.submatrix e f) (f.symm j) (λ i, c (e i)) := eq.trans (by simp_rw equiv.apply_symm_apply) (update_column_submatrix_equiv A _ _ e f).symm /-! `reindex` versions of the above `submatrix` lemmas for convenience. -/ lemma update_row_reindex [decidable_eq l] [decidable_eq m] (A : matrix m n α) (i : l) (r : o → α) (e : m ≃ l) (f : n ≃ o) : update_row (reindex e f A) i r = reindex e f (A.update_row (e.symm i) (λ j, r (f j))) := update_row_submatrix_equiv _ _ _ _ _ lemma reindex_update_row [decidable_eq l] [decidable_eq m] (A : matrix m n α) (i : m) (r : n → α) (e : m ≃ l) (f : n ≃ o) : reindex e f (A.update_row i r) = update_row (reindex e f A) (e i) (λ i, r (f.symm i)) := submatrix_update_row_equiv _ _ _ _ _ lemma update_column_reindex [decidable_eq o] [decidable_eq n] (A : matrix m n α) (j : o) (c : l → α) (e : m ≃ l) (f : n ≃ o) : update_column (reindex e f A) j c = reindex e f (A.update_column (f.symm j) (λ i, c (e i))) := update_column_submatrix_equiv _ _ _ _ _ lemma reindex_update_column [decidable_eq o] [decidable_eq n] (A : matrix m n α) (j : n) (c : m → α) (e : m ≃ l) (f : n ≃ o) : reindex e f (A.update_column j c) = update_column (reindex e f A) (f j) (λ i, c (e.symm i)) := submatrix_update_column_equiv _ _ _ _ _ end update end matrix namespace ring_hom variables [fintype n] [non_assoc_semiring α] [non_assoc_semiring β] lemma map_matrix_mul (M : matrix m n α) (N : matrix n o α) (i : m) (j : o) (f : α →+* β) : f (matrix.mul M N i j) = matrix.mul (M.map f) (N.map f) i j := by simp [matrix.mul_apply, ring_hom.map_sum] lemma map_dot_product [non_assoc_semiring R] [non_assoc_semiring S] (f : R →+* S) (v w : n → R) : f (v ⬝ᵥ w) = (f ∘ v) ⬝ᵥ (f ∘ w) := by simp only [matrix.dot_product, f.map_sum, f.map_mul] lemma map_vec_mul [non_assoc_semiring R] [non_assoc_semiring S] (f : R →+* S) (M : matrix n m R) (v : n → R) (i : m) : f (M.vec_mul v i) = ((M.map f).vec_mul (f ∘ v) i) := by simp only [matrix.vec_mul, matrix.map_apply, ring_hom.map_dot_product] lemma map_mul_vec [non_assoc_semiring R] [non_assoc_semiring S] (f : R →+* S) (M : matrix m n R) (v : n → R) (i : m) : f (M.mul_vec v i) = ((M.map f).mul_vec (f ∘ v) i) := by simp only [matrix.mul_vec, matrix.map_apply, ring_hom.map_dot_product] end ring_hom
9b445a24ca129536078d91f71cb4362956fdb58f
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/data/finsupp/basic.lean
24ed24806adce0ceab2ba39c2e1bf3ab07b17c74
[ "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
105,540
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, Scott Morrison -/ import data.finset.preimage import algebra.indicator_function import algebra.group_action_hom /-! # Type of functions with finite support For any type `α` and a type `M` with zero, we define the type `finsupp α M` (notation: `α →₀ M`) of finitely supported functions from `α` to `M`, i.e. the functions which are zero everywhere on `α` except on a finite set. Functions with finite support are used (at least) in the following parts of the library: * `monoid_algebra R M` and `add_monoid_algebra R M` are defined as `M →₀ R`; * polynomials and multivariate polynomials are defined as `add_monoid_algebra`s, hence they use `finsupp` under the hood; * the linear combination of a family of vectors `v i` with coefficients `f i` (as used, e.g., to define linearly independent family `linear_independent`) is defined as a map `finsupp.total : (ι → M) → (ι →₀ R) →ₗ[R] M`. Some other constructions are naturally equivalent to `α →₀ M` with some `α` and `M` but are defined in a different way in the library: * `multiset α ≃+ α →₀ ℕ`; * `free_abelian_group α ≃+ α →₀ ℤ`. Most of the theory assumes that the range is a commutative additive monoid. This gives us the big sum operator as a powerful way to construct `finsupp` elements. Many constructions based on `α →₀ M` use `semireducible` type tags to avoid reusing unwanted type instances. E.g., `monoid_algebra`, `add_monoid_algebra`, and types based on these two have non-pointwise multiplication. ## Notations This file adds `α →₀ M` as a global notation for `finsupp α M`. We also use the following convention for `Type*` variables in this file * `α`, `β`, `γ`: types with no additional structure that appear as the first argument to `finsupp` somewhere in the statement; * `ι` : an auxiliary index type; * `M`, `M'`, `N`, `P`: types with `has_zero` or `(add_)(comm_)monoid` structure; `M` is also used for a (semi)module over a (semi)ring. * `G`, `H`: groups (commutative or not, multiplicative or additive); * `R`, `S`: (semi)rings. ## TODO * This file is currently ~2K lines long, so possibly it should be splitted into smaller chunks; * Add the list of definitions and important lemmas to the module docstring. ## Implementation notes This file is a `noncomputable theory` and uses classical logic throughout. ## Notation This file defines `α →₀ β` as notation for `finsupp α β`. -/ noncomputable theory open_locale classical big_operators open finset variables {α β γ ι M M' N P G H R S : Type*} /-- `finsupp α M`, denoted `α →₀ M`, is the type of functions `f : α → M` such that `f x = 0` for all but finitely many `x`. -/ structure finsupp (α : Type*) (M : Type*) [has_zero M] := (support : finset α) (to_fun : α → M) (mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0) infixr ` →₀ `:25 := finsupp namespace finsupp /-! ### Basic declarations about `finsupp` -/ section basic variable [has_zero M] instance : has_coe_to_fun (α →₀ M) := ⟨λ _, α → M, to_fun⟩ @[simp] lemma coe_mk (f : α → M) (s : finset α) (h : ∀ a, a ∈ s ↔ f a ≠ 0) : ⇑(⟨s, f, h⟩ : α →₀ M) = f := rfl instance : has_zero (α →₀ M) := ⟨⟨∅, 0, λ _, ⟨false.elim, λ H, H rfl⟩⟩⟩ @[simp] lemma coe_zero : ⇑(0 : α →₀ M) = 0 := rfl lemma zero_apply {a : α} : (0 : α →₀ M) a = 0 := rfl @[simp] lemma support_zero : (0 : α →₀ M).support = ∅ := rfl instance : inhabited (α →₀ M) := ⟨0⟩ @[simp] lemma mem_support_iff {f : α →₀ M} : ∀{a:α}, a ∈ f.support ↔ f a ≠ 0 := f.mem_support_to_fun @[simp, norm_cast] lemma fun_support_eq (f : α →₀ M) : function.support f = f.support := set.ext $ λ x, mem_support_iff.symm lemma not_mem_support_iff {f : α →₀ M} {a} : a ∉ f.support ↔ f a = 0 := not_iff_comm.1 mem_support_iff.symm lemma coe_fn_injective : @function.injective (α →₀ M) (α → M) coe_fn | ⟨s, f, hf⟩ ⟨t, g, hg⟩ h := begin change f = g at h, subst h, have : s = t, { ext a, exact (hf a).trans (hg a).symm }, subst this end @[simp, norm_cast] lemma coe_fn_inj {f g : α →₀ M} : (f : α → M) = g ↔ f = g := coe_fn_injective.eq_iff @[simp, norm_cast] lemma coe_eq_zero {f : α →₀ M} : (f : α → M) = 0 ↔ f = 0 := by rw [← coe_zero, coe_fn_inj] @[ext] lemma ext {f g : α →₀ M} (h : ∀a, f a = g a) : f = g := coe_fn_injective (funext h) lemma ext_iff {f g : α →₀ M} : f = g ↔ (∀a:α, f a = g a) := ⟨by rintros rfl a; refl, ext⟩ lemma ext_iff' {f g : α →₀ M} : f = g ↔ f.support = g.support ∧ ∀ x ∈ f.support, f x = g x := ⟨λ h, h ▸ ⟨rfl, λ _ _, rfl⟩, λ ⟨h₁, h₂⟩, ext $ λ a, if h : a ∈ f.support then h₂ a h else have hf : f a = 0, from not_mem_support_iff.1 h, have hg : g a = 0, by rwa [h₁, not_mem_support_iff] at h, by rw [hf, hg]⟩ lemma congr_fun {f g : α →₀ M} (h : f = g) (a : α) : f a = g a := congr_fun (congr_arg finsupp.to_fun h) a @[simp] lemma support_eq_empty {f : α →₀ M} : f.support = ∅ ↔ f = 0 := by exact_mod_cast @function.support_eq_empty_iff _ _ _ f lemma support_nonempty_iff {f : α →₀ M} : f.support.nonempty ↔ f ≠ 0 := by simp only [finsupp.support_eq_empty, finset.nonempty_iff_ne_empty, ne.def] lemma nonzero_iff_exists {f : α →₀ M} : f ≠ 0 ↔ ∃ a : α, f a ≠ 0 := by simp [← finsupp.support_eq_empty, finset.eq_empty_iff_forall_not_mem] lemma card_support_eq_zero {f : α →₀ M} : card f.support = 0 ↔ f = 0 := by simp instance [decidable_eq α] [decidable_eq M] : decidable_eq (α →₀ M) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀a∈f.support, f a = g a)) ext_iff'.symm lemma finite_support (f : α →₀ M) : set.finite (function.support f) := f.fun_support_eq.symm ▸ f.support.finite_to_set lemma support_subset_iff {s : set α} {f : α →₀ M} : ↑f.support ⊆ s ↔ (∀a∉s, f a = 0) := by simp only [set.subset_def, mem_coe, mem_support_iff]; exact forall_congr (assume a, not_imp_comm) /-- Given `fintype α`, `equiv_fun_on_fintype` is the `equiv` between `α →₀ β` and `α → β`. (All functions on a finite type are finitely supported.) -/ @[simps] def equiv_fun_on_fintype [fintype α] : (α →₀ M) ≃ (α → M) := ⟨λf a, f a, λf, mk (finset.univ.filter $ λa, f a ≠ 0) f (by simp only [true_and, finset.mem_univ, iff_self, finset.mem_filter, finset.filter_congr_decidable, forall_true_iff]), begin intro f, ext a, refl end, begin intro f, ext a, refl end⟩ @[simp] lemma equiv_fun_on_fintype_symm_coe {α} [fintype α] (f : α →₀ M) : equiv_fun_on_fintype.symm f = f := by { ext, simp [equiv_fun_on_fintype], } end basic /-! ### Declarations about `single` -/ section single variables [has_zero M] {a a' : α} {b : M} /-- `single a b` is the finitely supported function which has value `b` at `a` and zero otherwise. -/ def single (a : α) (b : M) : α →₀ M := ⟨if b = 0 then ∅ else {a}, λ a', if a = a' then b else 0, λ a', begin by_cases hb : b = 0; by_cases a = a'; simp only [hb, h, if_pos, if_false, mem_singleton], { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨λ _, hb, λ _, rfl⟩ }, { exact ⟨λ H _, h H.symm, λ H, (H rfl).elim⟩ } end⟩ lemma single_apply [decidable (a = a')] : single a b a' = if a = a' then b else 0 := by convert rfl lemma single_eq_indicator : ⇑(single a b) = set.indicator {a} (λ _, b) := by { ext, simp [single_apply, set.indicator, @eq_comm _ a] } @[simp] lemma single_eq_same : (single a b : α →₀ M) a = b := if_pos rfl @[simp] lemma single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ M) a' = 0 := if_neg h lemma single_eq_update : ⇑(single a b) = function.update 0 a b := by rw [single_eq_indicator, ← set.piecewise_eq_indicator, set.piecewise_singleton] lemma single_eq_pi_single : ⇑(single a b) = pi.single a b := single_eq_update @[simp] lemma single_zero : (single a 0 : α →₀ M) = 0 := coe_fn_injective $ by simpa only [single_eq_update, coe_zero] using function.update_eq_self a (0 : α → M) lemma single_of_single_apply (a a' : α) (b : M) : single a ((single a' b) a) = single a' (single a' b) a := begin rw [single_apply, single_apply], ext, split_ifs, { rw h, }, { rw [zero_apply, single_apply, if_t_t], }, end lemma support_single_ne_zero (hb : b ≠ 0) : (single a b).support = {a} := if_neg hb lemma support_single_subset : (single a b).support ⊆ {a} := show ite _ _ _ ⊆ _, by split_ifs; [exact empty_subset _, exact subset.refl _] lemma single_apply_mem (x) : single a b x ∈ ({0, b} : set M) := by rcases em (a = x) with (rfl|hx); [simp, simp [single_eq_of_ne hx]] lemma range_single_subset : set.range (single a b) ⊆ {0, b} := set.range_subset_iff.2 single_apply_mem /-- `finsupp.single a b` is injective in `b`. For the statement that it is injective in `a`, see `finsupp.single_left_injective` -/ lemma single_injective (a : α) : function.injective (single a : M → α →₀ M) := assume b₁ b₂ eq, have (single a b₁ : α →₀ M) a = (single a b₂ : α →₀ M) a, by rw eq, by rwa [single_eq_same, single_eq_same] at this lemma single_apply_eq_zero {a x : α} {b : M} : single a b x = 0 ↔ (x = a → b = 0) := by simp [single_eq_indicator] lemma mem_support_single (a a' : α) (b : M) : a ∈ (single a' b).support ↔ a = a' ∧ b ≠ 0 := by simp [single_apply_eq_zero, not_or_distrib] lemma eq_single_iff {f : α →₀ M} {a b} : f = single a b ↔ f.support ⊆ {a} ∧ f a = b := begin refine ⟨λ h, h.symm ▸ ⟨support_single_subset, single_eq_same⟩, _⟩, rintro ⟨h, rfl⟩, ext x, by_cases hx : a = x; simp only [hx, single_eq_same, single_eq_of_ne, ne.def, not_false_iff], exact not_mem_support_iff.1 (mt (λ hx, (mem_singleton.1 (h hx)).symm) hx) end lemma single_eq_single_iff (a₁ a₂ : α) (b₁ b₂ : M) : single a₁ b₁ = single a₂ b₂ ↔ ((a₁ = a₂ ∧ b₁ = b₂) ∨ (b₁ = 0 ∧ b₂ = 0)) := begin split, { assume eq, by_cases a₁ = a₂, { refine or.inl ⟨h, _⟩, rwa [h, (single_injective a₂).eq_iff] at eq }, { rw [ext_iff] at eq, have h₁ := eq a₁, have h₂ := eq a₂, simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (ne.symm h)] at h₁ h₂, exact or.inr ⟨h₁, h₂.symm⟩ } }, { rintros (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩), { refl }, { rw [single_zero, single_zero] } } end /-- `finsupp.single a b` is injective in `a`. For the statement that it is injective in `b`, see `finsupp.single_injective` -/ lemma single_left_injective (h : b ≠ 0) : function.injective (λ a : α, single a b) := λ a a' H, (((single_eq_single_iff _ _ _ _).mp H).resolve_right $ λ hb, h hb.1).left lemma single_left_inj (h : b ≠ 0) : single a b = single a' b ↔ a = a' := (single_left_injective h).eq_iff lemma support_single_ne_bot (i : α) (h : b ≠ 0) : (single i b).support ≠ ⊥ := begin have : i ∈ (single i b).support := by simpa using h, intro H, simpa [H] end lemma support_single_disjoint {b' : M} (hb : b ≠ 0) (hb' : b' ≠ 0) {i j : α} : disjoint (single i b).support (single j b').support ↔ i ≠ j := by simpa [support_single_ne_zero, hb, hb'] using ne_comm @[simp] lemma single_eq_zero : single a b = 0 ↔ b = 0 := by simp [ext_iff, single_eq_indicator] lemma single_swap (a₁ a₂ : α) (b : M) : single a₁ b a₂ = single a₂ b a₁ := by simp only [single_apply]; ac_refl instance [nonempty α] [nontrivial M] : nontrivial (α →₀ M) := begin inhabit α, rcases exists_ne (0 : M) with ⟨x, hx⟩, exact nontrivial_of_ne (single (default α) x) 0 (mt single_eq_zero.1 hx) end lemma unique_single [unique α] (x : α →₀ M) : x = single (default α) (x (default α)) := ext $ unique.forall_iff.2 single_eq_same.symm lemma unique_ext [unique α] {f g : α →₀ M} (h : f (default α) = g (default α)) : f = g := ext $ λ a, by rwa [unique.eq_default a] lemma unique_ext_iff [unique α] {f g : α →₀ M} : f = g ↔ f (default α) = g (default α) := ⟨λ h, h ▸ rfl, unique_ext⟩ @[simp] lemma unique_single_eq_iff [unique α] {b' : M} : single a b = single a' b' ↔ b = b' := by rw [unique_ext_iff, unique.eq_default a, unique.eq_default a', single_eq_same, single_eq_same] lemma support_eq_singleton {f : α →₀ M} {a : α} : f.support = {a} ↔ f a ≠ 0 ∧ f = single a (f a) := ⟨λ h, ⟨mem_support_iff.1 $ h.symm ▸ finset.mem_singleton_self a, eq_single_iff.2 ⟨subset_of_eq h, rfl⟩⟩, λ h, h.2.symm ▸ support_single_ne_zero h.1⟩ lemma support_eq_singleton' {f : α →₀ M} {a : α} : f.support = {a} ↔ ∃ b ≠ 0, f = single a b := ⟨λ h, let h := support_eq_singleton.1 h in ⟨_, h.1, h.2⟩, λ ⟨b, hb, hf⟩, hf.symm ▸ support_single_ne_zero hb⟩ lemma card_support_eq_one {f : α →₀ M} : card f.support = 1 ↔ ∃ a, f a ≠ 0 ∧ f = single a (f a) := by simp only [card_eq_one, support_eq_singleton] lemma card_support_eq_one' {f : α →₀ M} : card f.support = 1 ↔ ∃ a (b ≠ 0), f = single a b := by simp only [card_eq_one, support_eq_singleton'] @[simp] lemma equiv_fun_on_fintype_single [fintype α] (x : α) (m : M) : (@finsupp.equiv_fun_on_fintype α M _ _) (finsupp.single x m) = pi.single x m := by { ext, simp [finsupp.single_eq_pi_single, finsupp.equiv_fun_on_fintype], } @[simp] lemma equiv_fun_on_fintype_symm_single [fintype α] (x : α) (m : M) : (@finsupp.equiv_fun_on_fintype α M _ _).symm (pi.single x m) = finsupp.single x m := by { ext, simp [finsupp.single_eq_pi_single, finsupp.equiv_fun_on_fintype], } end single /-! ### Declarations about `on_finset` -/ section on_finset variables [has_zero M] /-- `on_finset s f hf` is the finsupp function representing `f` restricted to the finset `s`. The function needs to be `0` outside of `s`. Use this when the set needs to be filtered anyways, otherwise a better set representation is often available. -/ def on_finset (s : finset α) (f : α → M) (hf : ∀a, f a ≠ 0 → a ∈ s) : α →₀ M := ⟨s.filter (λa, f a ≠ 0), f, by simpa⟩ @[simp] lemma on_finset_apply {s : finset α} {f : α → M} {hf a} : (on_finset s f hf : α →₀ M) a = f a := rfl @[simp] lemma support_on_finset_subset {s : finset α} {f : α → M} {hf} : (on_finset s f hf).support ⊆ s := filter_subset _ _ @[simp] lemma mem_support_on_finset {s : finset α} {f : α → M} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) {a : α} : a ∈ (finsupp.on_finset s f hf).support ↔ f a ≠ 0 := by rw [finsupp.mem_support_iff, finsupp.on_finset_apply] lemma support_on_finset {s : finset α} {f : α → M} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) : (finsupp.on_finset s f hf).support = s.filter (λ a, f a ≠ 0) := rfl end on_finset section of_support_finite variables [has_zero M] /-- The natural `finsupp` induced by the function `f` given that it has finite support. -/ noncomputable def of_support_finite (f : α → M) (hf : (function.support f).finite) : α →₀ M := { support := hf.to_finset, to_fun := f, mem_support_to_fun := λ _, hf.mem_to_finset } lemma of_support_finite_coe {f : α → M} {hf : (function.support f).finite} : (of_support_finite f hf : α → M) = f := rfl instance : can_lift (α → M) (α →₀ M) := { coe := coe_fn, cond := λ f, (function.support f).finite, prf := λ f hf, ⟨of_support_finite f hf, rfl⟩ } end of_support_finite /-! ### Declarations about `map_range` -/ section map_range variables [has_zero M] [has_zero N] [has_zero P] /-- The composition of `f : M → N` and `g : α →₀ M` is `map_range f hf g : α →₀ N`, well-defined when `f 0 = 0`. This preserves the structure on `f`, and exists in various bundled forms for when `f` is itself bundled: * `finsupp.map_range.equiv` * `finsupp.map_range.zero_hom` * `finsupp.map_range.add_monoid_hom` * `finsupp.map_range.add_equiv` * `finsupp.map_range.linear_map` * `finsupp.map_range.linear_equiv` -/ def map_range (f : M → N) (hf : f 0 = 0) (g : α →₀ M) : α →₀ N := on_finset g.support (f ∘ g) $ assume a, by rw [mem_support_iff, not_imp_not]; exact λ H, (congr_arg f H).trans hf @[simp] lemma map_range_apply {f : M → N} {hf : f 0 = 0} {g : α →₀ M} {a : α} : map_range f hf g a = f (g a) := rfl @[simp] lemma map_range_zero {f : M → N} {hf : f 0 = 0} : map_range f hf (0 : α →₀ M) = 0 := ext $ λ a, by simp only [hf, zero_apply, map_range_apply] @[simp] lemma map_range_id (g : α →₀ M) : map_range id rfl g = g := ext $ λ _, rfl lemma map_range_comp (f : N → P) (hf : f 0 = 0) (f₂ : M → N) (hf₂ : f₂ 0 = 0) (h : (f ∘ f₂) 0 = 0) (g : α →₀ M) : map_range (f ∘ f₂) h g = map_range f hf (map_range f₂ hf₂ g) := ext $ λ _, rfl lemma support_map_range {f : M → N} {hf : f 0 = 0} {g : α →₀ M} : (map_range f hf g).support ⊆ g.support := support_on_finset_subset @[simp] lemma map_range_single {f : M → N} {hf : f 0 = 0} {a : α} {b : M} : map_range f hf (single a b) = single a (f b) := ext $ λ a', show f (ite _ _ _) = ite _ _ _, by split_ifs; [refl, exact hf] end map_range /-! ### Declarations about `emb_domain` -/ section emb_domain variables [has_zero M] [has_zero N] /-- Given `f : α ↪ β` and `v : α →₀ M`, `emb_domain f v : β →₀ M` is the finitely supported function whose value at `f a : β` is `v a`. For a `b : β` outside the range of `f`, it is zero. -/ def emb_domain (f : α ↪ β) (v : α →₀ M) : β →₀ M := begin refine ⟨v.support.map f, λa₂, if h : a₂ ∈ v.support.map f then v (v.support.choose (λa₁, f a₁ = a₂) _) else 0, _⟩, { rcases finset.mem_map.1 h with ⟨a, ha, rfl⟩, exact exists_unique.intro a ⟨ha, rfl⟩ (assume b ⟨_, hb⟩, f.injective hb) }, { assume a₂, split_ifs, { simp only [h, true_iff, ne.def], rw [← not_mem_support_iff, not_not], apply finset.choose_mem }, { simp only [h, ne.def, ne_self_iff_false] } } end @[simp] lemma support_emb_domain (f : α ↪ β) (v : α →₀ M) : (emb_domain f v).support = v.support.map f := rfl @[simp] lemma emb_domain_zero (f : α ↪ β) : (emb_domain f 0 : β →₀ M) = 0 := rfl @[simp] lemma emb_domain_apply (f : α ↪ β) (v : α →₀ M) (a : α) : emb_domain f v (f a) = v a := begin change dite _ _ _ = _, split_ifs; rw [finset.mem_map' f] at h, { refine congr_arg (v : α → M) (f.inj' _), exact finset.choose_property (λa₁, f a₁ = f a) _ _ }, { exact (not_mem_support_iff.1 h).symm } end lemma emb_domain_notin_range (f : α ↪ β) (v : α →₀ M) (a : β) (h : a ∉ set.range f) : emb_domain f v a = 0 := begin refine dif_neg (mt (assume h, _) h), rcases finset.mem_map.1 h with ⟨a, h, rfl⟩, exact set.mem_range_self a end lemma emb_domain_injective (f : α ↪ β) : function.injective (emb_domain f : (α →₀ M) → (β →₀ M)) := λ l₁ l₂ h, ext $ λ a, by simpa only [emb_domain_apply] using ext_iff.1 h (f a) @[simp] lemma emb_domain_inj {f : α ↪ β} {l₁ l₂ : α →₀ M} : emb_domain f l₁ = emb_domain f l₂ ↔ l₁ = l₂ := (emb_domain_injective f).eq_iff @[simp] lemma emb_domain_eq_zero {f : α ↪ β} {l : α →₀ M} : emb_domain f l = 0 ↔ l = 0 := (emb_domain_injective f).eq_iff' $ emb_domain_zero f lemma emb_domain_map_range (f : α ↪ β) (g : M → N) (p : α →₀ M) (hg : g 0 = 0) : emb_domain f (map_range g hg p) = map_range g hg (emb_domain f p) := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a', rfl⟩, rw [map_range_apply, emb_domain_apply, emb_domain_apply, map_range_apply] }, { rw [map_range_apply, emb_domain_notin_range, emb_domain_notin_range, ← hg]; assumption } end lemma single_of_emb_domain_single (l : α →₀ M) (f : α ↪ β) (a : β) (b : M) (hb : b ≠ 0) (h : l.emb_domain f = single a b) : ∃ x, l = single x b ∧ f x = a := begin have h_map_support : finset.map f (l.support) = {a}, by rw [←support_emb_domain, h, support_single_ne_zero hb]; refl, have ha : a ∈ finset.map f (l.support), by simp only [h_map_support, finset.mem_singleton], rcases finset.mem_map.1 ha with ⟨c, hc₁, hc₂⟩, use c, split, { ext d, rw [← emb_domain_apply f l, h], by_cases h_cases : c = d, { simp only [eq.symm h_cases, hc₂, single_eq_same] }, { rw [single_apply, single_apply, if_neg, if_neg h_cases], by_contra hfd, exact h_cases (f.injective (hc₂.trans hfd)) } }, { exact hc₂ } end @[simp] lemma emb_domain_single (f : α ↪ β) (a : α) (m : M) : emb_domain f (single a m) = single (f a) m := begin ext b, by_cases h : b ∈ set.range f, { rcases h with ⟨a', rfl⟩, simp [single_apply], }, { simp only [emb_domain_notin_range, h, single_apply, not_false_iff], rw if_neg, rintro rfl, simpa using h, }, end end emb_domain /-! ### Declarations about `zip_with` -/ section zip_with variables [has_zero M] [has_zero N] [has_zero P] /-- `zip_with f hf g₁ g₂` is the finitely supported function satisfying `zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, and it is well-defined when `f 0 0 = 0`. -/ def zip_with (f : M → N → P) (hf : f 0 0 = 0) (g₁ : α →₀ M) (g₂ : α →₀ N) : (α →₀ P) := on_finset (g₁.support ∪ g₂.support) (λa, f (g₁ a) (g₂ a)) $ λ a H, begin simp only [mem_union, mem_support_iff, ne], rw [← not_and_distrib], rintro ⟨h₁, h₂⟩, rw [h₁, h₂] at H, exact H hf end @[simp] lemma zip_with_apply {f : M → N → P} {hf : f 0 0 = 0} {g₁ : α →₀ M} {g₂ : α →₀ N} {a : α} : zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a) := rfl lemma support_zip_with [D : decidable_eq α] {f : M → N → P} {hf : f 0 0 = 0} {g₁ : α →₀ M} {g₂ : α →₀ N} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := by rw subsingleton.elim D; exact support_on_finset_subset end zip_with /-! ### Declarations about `erase` -/ section erase variables [has_zero M] /-- `erase a f` is the finitely supported function equal to `f` except at `a` where it is equal to `0`. -/ def erase (a : α) (f : α →₀ M) : α →₀ M := ⟨f.support.erase a, (λa', if a' = a then 0 else f a'), assume a', by rw [mem_erase, mem_support_iff]; split_ifs; [exact ⟨λ H _, H.1 h, λ H, (H rfl).elim⟩, exact and_iff_right h]⟩ @[simp] lemma support_erase {a : α} {f : α →₀ M} : (f.erase a).support = f.support.erase a := rfl @[simp] lemma erase_same {a : α} {f : α →₀ M} : (f.erase a) a = 0 := if_pos rfl @[simp] lemma erase_ne {a a' : α} {f : α →₀ M} (h : a' ≠ a) : (f.erase a) a' = f a' := if_neg h @[simp] lemma erase_single {a : α} {b : M} : (erase a (single a b)) = 0 := begin ext s, by_cases hs : s = a, { rw [hs, erase_same], refl }, { rw [erase_ne hs], exact single_eq_of_ne (ne.symm hs) } end lemma erase_single_ne {a a' : α} {b : M} (h : a ≠ a') : (erase a (single a' b)) = single a' b := begin ext s, by_cases hs : s = a, { rw [hs, erase_same, single_eq_of_ne (h.symm)] }, { rw [erase_ne hs] } end @[simp] lemma erase_zero (a : α) : erase a (0 : α →₀ M) = 0 := by rw [← support_eq_empty, support_erase, support_zero, erase_empty] end erase /-! ### Declarations about `sum` and `prod` In most of this section, the domain `β` is assumed to be an `add_monoid`. -/ section sum_prod -- [to_additive sum] for finsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/ def sum [has_zero M] [add_comm_monoid N] (f : α →₀ M) (g : α → M → N) : N := ∑ a in f.support, g a (f a) /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive] def prod [has_zero M] [comm_monoid N] (f : α →₀ M) (g : α → M → N) : N := ∏ a in f.support, g a (f a) variables [has_zero M] [has_zero M'] [comm_monoid N] @[to_additive] lemma prod_of_support_subset (f : α →₀ M) {s : finset α} (hs : f.support ⊆ s) (g : α → M → N) (h : ∀ i ∈ s, g i 0 = 1) : f.prod g = ∏ x in s, g x (f x) := finset.prod_subset hs $ λ x hxs hx, h x hxs ▸ congr_arg (g x) $ not_mem_support_iff.1 hx @[to_additive] lemma prod_fintype [fintype α] (f : α →₀ M) (g : α → M → N) (h : ∀ i, g i 0 = 1) : f.prod g = ∏ i, g i (f i) := f.prod_of_support_subset (subset_univ _) g (λ x _, h x) @[simp, to_additive] lemma prod_single_index {a : α} {b : M} {h : α → M → N} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := calc (single a b).prod h = ∏ x in {a}, h x (single a b x) : prod_of_support_subset _ support_single_subset h $ λ x hx, (mem_singleton.1 hx).symm ▸ h_zero ... = h a b : by simp @[to_additive] lemma prod_map_range_index {f : M → M'} {hf : f 0 = 0} {g : α →₀ M} {h : α → M' → N} (h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) := finset.prod_subset support_map_range $ λ _ _ H, by rw [not_mem_support_iff.1 H, h0] @[simp, to_additive] lemma prod_zero_index {h : α → M → N} : (0 : α →₀ M).prod h = 1 := rfl @[to_additive] lemma prod_comm (f : α →₀ M) (g : β →₀ M') (h : α → M → β → M' → N) : f.prod (λ x v, g.prod (λ x' v', h x v x' v')) = g.prod (λ x' v', f.prod (λ x v, h x v x' v')) := finset.prod_comm @[simp, to_additive] lemma prod_ite_eq [decidable_eq α] (f : α →₀ M) (a : α) (b : α → M → N) : f.prod (λ x v, ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq, } @[simp] lemma sum_ite_self_eq [decidable_eq α] {N : Type*} [add_comm_monoid N] (f : α →₀ N) (a : α) : f.sum (λ x v, ite (a = x) v 0) = f a := by { convert f.sum_ite_eq a (λ x, id), simp [ite_eq_right_iff.2 eq.symm] } /-- A restatement of `prod_ite_eq` with the equality test reversed. -/ @[simp, to_additive "A restatement of `sum_ite_eq` with the equality test reversed."] lemma prod_ite_eq' [decidable_eq α] (f : α →₀ M) (a : α) (b : α → M → N) : f.prod (λ x v, ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq', } @[simp] lemma sum_ite_self_eq' [decidable_eq α] {N : Type*} [add_comm_monoid N] (f : α →₀ N) (a : α) : f.sum (λ x v, ite (x = a) v 0) = f a := by { convert f.sum_ite_eq' a (λ x, id), simp [ite_eq_right_iff.2 eq.symm] } @[simp] lemma prod_pow [fintype α] (f : α →₀ ℕ) (g : α → N) : f.prod (λ a b, g a ^ b) = ∏ a, g a ^ (f a) := f.prod_fintype _ $ λ a, pow_zero _ /-- If `g` maps a second argument of 0 to 1, then multiplying it over the result of `on_finset` is the same as multiplying it over the original `finset`. -/ @[to_additive "If `g` maps a second argument of 0 to 0, summing it over the result of `on_finset` is the same as summing it over the original `finset`."] lemma on_finset_prod {s : finset α} {f : α → M} {g : α → M → N} (hf : ∀a, f a ≠ 0 → a ∈ s) (hg : ∀ a, g a 0 = 1) : (on_finset s f hf).prod g = ∏ a in s, g a (f a) := finset.prod_subset support_on_finset_subset $ by simp [*] { contextual := tt } @[to_additive] lemma _root_.submonoid.finsupp_prod_mem (S : submonoid N) (f : α →₀ M) (g : α → M → N) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ S) : f.prod g ∈ S := S.prod_mem $ λ i hi, h _ (finsupp.mem_support_iff.mp hi) end sum_prod /-! ### Additive monoid structure on `α →₀ M` -/ section add_zero_class variables [add_zero_class M] instance : has_add (α →₀ M) := ⟨zip_with (+) (add_zero 0)⟩ @[simp] lemma coe_add (f g : α →₀ M) : ⇑(f + g) = f + g := rfl lemma add_apply (g₁ g₂ : α →₀ M) (a : α) : (g₁ + g₂) a = g₁ a + g₂ a := rfl lemma support_add [decidable_eq α] {g₁ g₂ : α →₀ M} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with lemma support_add_eq [decidable_eq α] {g₁ g₂ : α →₀ M} (h : disjoint g₁.support g₂.support) : (g₁ + g₂).support = g₁.support ∪ g₂.support := le_antisymm support_zip_with $ assume a ha, (finset.mem_union.1 ha).elim (assume ha, have a ∉ g₂.support, from disjoint_left.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, add_zero]) (assume ha, have a ∉ g₁.support, from disjoint_right.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, zero_add]) @[simp] lemma single_add {a : α} {b₁ b₂ : M} : single a (b₁ + b₂) = single a b₁ + single a b₂ := ext $ assume a', begin by_cases h : a = a', { rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] } end instance : add_zero_class (α →₀ M) := { zero := 0, add := (+), zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _, add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ } /-- `finsupp.single` as an `add_monoid_hom`. See `finsupp.lsingle` for the stronger version as a linear map. -/ @[simps] def single_add_hom (a : α) : M →+ α →₀ M := ⟨single a, single_zero, λ _ _, single_add⟩ /-- Evaluation of a function `f : α →₀ M` at a point as an additive monoid homomorphism. See `finsupp.lapply` for the stronger version as a linear map. -/ @[simps apply] def apply_add_hom (a : α) : (α →₀ M) →+ M := ⟨λ g, g a, zero_apply, λ _ _, add_apply _ _ _⟩ lemma single_add_erase (a : α) (f : α →₀ M) : single a (f a) + f.erase a = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, add_zero] else by simp only [add_apply, single_eq_of_ne h, zero_add, erase_ne (ne.symm h)] lemma erase_add_single (a : α) (f : α →₀ M) : f.erase a + single a (f a) = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, zero_add] else by simp only [add_apply, single_eq_of_ne h, add_zero, erase_ne (ne.symm h)] @[simp] lemma erase_add (a : α) (f f' : α →₀ M) : erase a (f + f') = erase a f + erase a f' := begin ext s, by_cases hs : s = a, { rw [hs, add_apply, erase_same, erase_same, erase_same, add_zero] }, rw [add_apply, erase_ne hs, erase_ne hs, erase_ne hs, add_apply], end @[elab_as_eliminator] protected theorem induction {p : (α →₀ M) → Prop} (f : α →₀ M) (h0 : p 0) (ha : ∀a b (f : α →₀ M), a ∉ f.support → b ≠ 0 → p f → p (single a b + f)) : p f := suffices ∀s (f : α →₀ M), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma induction₂ {p : (α →₀ M) → Prop} (f : α →₀ M) (h0 : p 0) (ha : ∀a b (f : α →₀ M), a ∉ f.support → b ≠ 0 → p f → p (f + single a b)) : p f := suffices ∀s (f : α →₀ M), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma induction_linear {p : (α →₀ M) → Prop} (f : α →₀ M) (h0 : p 0) (hadd : ∀ f g : α →₀ M, p f → p g → p (f + g)) (hsingle : ∀ a b, p (single a b)) : p f := induction₂ f h0 (λ a b f _ _ w, hadd _ _ w (hsingle _ _)) @[simp] lemma add_closure_Union_range_single : add_submonoid.closure (⋃ a : α, set.range (single a : M → α →₀ M)) = ⊤ := top_unique $ λ x hx, finsupp.induction x (add_submonoid.zero_mem _) $ λ a b f ha hb hf, add_submonoid.add_mem _ (add_submonoid.subset_closure $ set.mem_Union.2 ⟨a, set.mem_range_self _⟩) hf /-- If two additive homomorphisms from `α →₀ M` are equal on each `single a b`, then they are equal. -/ lemma add_hom_ext [add_zero_class N] ⦃f g : (α →₀ M) →+ N⦄ (H : ∀ x y, f (single x y) = g (single x y)) : f = g := begin refine add_monoid_hom.eq_of_eq_on_mdense add_closure_Union_range_single (λ f hf, _), simp only [set.mem_Union, set.mem_range] at hf, rcases hf with ⟨x, y, rfl⟩, apply H end /-- If two additive homomorphisms from `α →₀ M` are equal on each `single a b`, then they are equal. We formulate this using equality of `add_monoid_hom`s so that `ext` tactic can apply a type-specific extensionality lemma after this one. E.g., if the fiber `M` is `ℕ` or `ℤ`, then it suffices to verify `f (single a 1) = g (single a 1)`. -/ @[ext] lemma add_hom_ext' [add_zero_class N] ⦃f g : (α →₀ M) →+ N⦄ (H : ∀ x, f.comp (single_add_hom x) = g.comp (single_add_hom x)) : f = g := add_hom_ext $ λ x, add_monoid_hom.congr_fun (H x) lemma mul_hom_ext [mul_one_class N] ⦃f g : multiplicative (α →₀ M) →* N⦄ (H : ∀ x y, f (multiplicative.of_add $ single x y) = g (multiplicative.of_add $ single x y)) : f = g := monoid_hom.ext $ add_monoid_hom.congr_fun $ @add_hom_ext α M (additive N) _ _ f.to_additive'' g.to_additive'' H @[ext] lemma mul_hom_ext' [mul_one_class N] {f g : multiplicative (α →₀ M) →* N} (H : ∀ x, f.comp (single_add_hom x).to_multiplicative = g.comp (single_add_hom x).to_multiplicative) : f = g := mul_hom_ext $ λ x, monoid_hom.congr_fun (H x) lemma map_range_add [add_zero_class N] {f : M → N} {hf : f 0 = 0} (hf' : ∀ x y, f (x + y) = f x + f y) (v₁ v₂ : α →₀ M) : map_range f hf (v₁ + v₂) = map_range f hf v₁ + map_range f hf v₂ := ext $ λ a, by simp only [hf', add_apply, map_range_apply] /-- Bundle `emb_domain f` as an additive map from `α →₀ M` to `β →₀ M`. -/ @[simps] def emb_domain.add_monoid_hom (f : α ↪ β) : (α →₀ M) →+ (β →₀ M) := { to_fun := λ v, emb_domain f v, map_zero' := by simp, map_add' := λ v w, begin ext b, by_cases h : b ∈ set.range f, { rcases h with ⟨a, rfl⟩, simp, }, { simp [emb_domain_notin_range, h], }, end, } @[simp] lemma emb_domain_add (f : α ↪ β) (v w : α →₀ M) : emb_domain f (v + w) = emb_domain f v + emb_domain f w := (emb_domain.add_monoid_hom f).map_add v w end add_zero_class section add_monoid variables [add_monoid M] instance : add_monoid (α →₀ M) := { add_monoid . zero := 0, add := (+), add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _, nsmul := λ n v, v.map_range ((•) n) (nsmul_zero _), nsmul_zero' := λ v, by { ext i, simp }, nsmul_succ' := λ n v, by { ext i, simp [nat.succ_eq_one_add, add_nsmul] }, .. finsupp.add_zero_class } end add_monoid end finsupp @[to_additive] lemma mul_equiv.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P] (h : N ≃* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ @[to_additive] lemma monoid_hom.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P] (h : N →* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ lemma ring_hom.map_finsupp_sum [has_zero M] [semiring R] [semiring S] (h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.sum g) = f.sum (λ a b, h (g a b)) := h.map_sum _ _ lemma ring_hom.map_finsupp_prod [has_zero M] [comm_semiring R] [comm_semiring S] (h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.prod g) = f.prod (λ a b, h (g a b)) := h.map_prod _ _ @[to_additive] lemma monoid_hom.coe_finsupp_prod [has_zero β] [monoid N] [comm_monoid P] (f : α →₀ β) (g : α → β → N →* P) : ⇑(f.prod g) = f.prod (λ i fi, g i fi) := monoid_hom.coe_prod _ _ @[simp, to_additive] lemma monoid_hom.finsupp_prod_apply [has_zero β] [monoid N] [comm_monoid P] (f : α →₀ β) (g : α → β → N →* P) (x : N) : f.prod g x = f.prod (λ i fi, g i fi x) := monoid_hom.finset_prod_apply _ _ _ namespace finsupp section nat_sub instance nat_sub : has_sub (α →₀ ℕ) := ⟨zip_with (λ m n, m - n) (nat.sub_zero 0)⟩ @[simp] lemma coe_nat_sub (g₁ g₂ : α →₀ ℕ) : ⇑(g₁ - g₂) = g₁ - g₂ := rfl lemma nat_sub_apply (g₁ g₂ : α →₀ ℕ) (a : α) : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma single_nat_sub {a : α} {n₁ n₂ : ℕ} : single a (n₁ - n₂) = single a n₁ - single a n₂ := begin ext f, by_cases h : (a = f), { rw [h, nat_sub_apply, single_eq_same, single_eq_same, single_eq_same] }, rw [nat_sub_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h] end -- These next two lemmas are used in developing -- the partial derivative on `mv_polynomial`. lemma sub_single_one_add {a : α} {u u' : α →₀ ℕ} (h : u a ≠ 0) : u - single a 1 + u' = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u a), { contradiction }, { simp }, }, { simp [h], } end lemma add_sub_single_one {a : α} {u u' : α →₀ ℕ} (h : u' a ≠ 0) : u + (u' - single a 1) = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u' a), { contradiction }, { simp }, }, { simp [h], } end @[simp] lemma nat_zero_sub (f : α →₀ ℕ) : 0 - f = 0 := ext $ λ x, nat.zero_sub _ end nat_sub instance [add_comm_monoid M] : add_comm_monoid (α →₀ M) := { add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _, .. finsupp.add_monoid } instance [add_group G] : has_sub (α →₀ G) := ⟨zip_with has_sub.sub (sub_zero _)⟩ instance [add_group G] : add_group (α →₀ G) := { neg := map_range (has_neg.neg) neg_zero, sub := has_sub.sub, sub_eq_add_neg := λ x y, ext (λ i, sub_eq_add_neg _ _), add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _, gsmul := λ n v, v.map_range ((•) n) (gsmul_zero _), gsmul_zero' := λ v, by { ext i, simp }, gsmul_succ' := λ n v, by { ext i, simp [nat.succ_eq_one_add, add_gsmul] }, gsmul_neg' := λ n v, by { ext i, simp only [nat.succ_eq_add_one, map_range_apply, gsmul_neg_succ_of_nat, int.coe_nat_succ, neg_inj, add_gsmul, add_nsmul, one_gsmul, gsmul_coe_nat, one_nsmul] }, .. finsupp.add_monoid } instance [add_comm_group G] : add_comm_group (α →₀ G) := { add_comm := add_comm, ..finsupp.add_group } lemma single_multiset_sum [add_comm_monoid M] (s : multiset M) (a : α) : single a s.sum = (s.map (single a)).sum := multiset.induction_on s single_zero $ λ a s ih, by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons] lemma single_finset_sum [add_comm_monoid M] (s : finset ι) (f : ι → M) (a : α) : single a (∑ b in s, f b) = ∑ b in s, single a (f b) := begin transitivity, apply single_multiset_sum, rw [multiset.map_map], refl end lemma single_sum [has_zero M] [add_comm_monoid N] (s : ι →₀ M) (f : ι → M → N) (a : α) : single a (s.sum f) = s.sum (λd c, single a (f d c)) := single_finset_sum _ _ _ @[to_additive] lemma prod_neg_index [add_group G] [comm_monoid M] {g : α →₀ G} {h : α → G → M} (h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) := prod_map_range_index h0 @[simp] lemma coe_neg [add_group G] (g : α →₀ G) : ⇑(-g) = -g := rfl lemma neg_apply [add_group G] (g : α →₀ G) (a : α) : (- g) a = - g a := rfl @[simp] lemma coe_sub [add_group G] (g₁ g₂ : α →₀ G) : ⇑(g₁ - g₂) = g₁ - g₂ := rfl lemma sub_apply [add_group G] (g₁ g₂ : α →₀ G) (a : α) : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma support_neg [add_group G] {f : α →₀ G} : support (-f) = support f := finset.subset.antisymm support_map_range (calc support f = support (- (- f)) : congr_arg support (neg_neg _).symm ... ⊆ support (- f) : support_map_range) @[simp] lemma sum_apply [has_zero M] [add_comm_monoid N] {f : α →₀ M} {g : α → M → β →₀ N} {a₂ : β} : (f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) := (apply_add_hom a₂ : (β →₀ N) →+ _).map_sum _ _ lemma support_sum [decidable_eq β] [has_zero M] [add_comm_monoid N] {f : α →₀ M} {g : α → M → (β →₀ N)} : (f.sum g).support ⊆ f.support.bUnion (λa, (g a (f a)).support) := have ∀ c, f.sum (λ a b, g a b c) ≠ 0 → (∃ a, f a ≠ 0 ∧ ¬ (g a (f a)) c = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, mem_support_iff.mp ha, ne⟩, by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bUnion, sum_apply, exists_prop] @[simp] lemma sum_zero [has_zero M] [add_comm_monoid N] {f : α →₀ M} : f.sum (λa b, (0 : N)) = 0 := finset.sum_const_zero @[simp, to_additive] lemma prod_mul [has_zero M] [comm_monoid N] {f : α →₀ M} {h₁ h₂ : α → M → N} : f.prod (λa b, h₁ a b * h₂ a b) = f.prod h₁ * f.prod h₂ := finset.prod_mul_distrib @[simp, to_additive] lemma prod_inv [has_zero M] [comm_group G] {f : α →₀ M} {h : α → M → G} : f.prod (λa b, (h a b)⁻¹) = (f.prod h)⁻¹ := (((monoid_hom.id G)⁻¹).map_prod _ _).symm @[simp] lemma sum_sub [has_zero M] [add_comm_group G] {f : α →₀ M} {h₁ h₂ : α → M → G} : f.sum (λa b, h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ := finset.sum_sub_distrib @[to_additive] lemma prod_add_index [add_comm_monoid M] [comm_monoid N] {f g : α →₀ M} {h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := have hf : f.prod h = ∏ a in f.support ∪ g.support, h a (f a), from f.prod_of_support_subset (subset_union_left _ _) _ $ λ a ha, h_zero a, have hg : g.prod h = ∏ a in f.support ∪ g.support, h a (g a), from g.prod_of_support_subset (subset_union_right _ _) _ $ λ a ha, h_zero a, have hfg : (f + g).prod h = ∏ a in f.support ∪ g.support, h a ((f + g) a), from (f + g).prod_of_support_subset support_add _ $ λ a ha, h_zero a, by simp only [*, add_apply, prod_mul_distrib] @[simp] lemma sum_add_index' [add_comm_monoid M] [add_comm_monoid N] {f g : α →₀ M} (h : α → M →+ N) : (f + g).sum (λ x, h x) = f.sum (λ x, h x) + g.sum (λ x, h x) := sum_add_index (λ a, (h a).map_zero) (λ a, (h a).map_add) @[simp] lemma prod_add_index' [add_comm_monoid M] [comm_monoid N] {f g : α →₀ M} (h : α → multiplicative M →* N) : (f + g).prod (λ a b, h a (multiplicative.of_add b)) = f.prod (λ a b, h a (multiplicative.of_add b)) * g.prod (λ a b, h a (multiplicative.of_add b)) := prod_add_index (λ a, (h a).map_one) (λ a, (h a).map_mul) /-- The canonical isomorphism between families of additive monoid homomorphisms `α → (M →+ N)` and monoid homomorphisms `(α →₀ M) →+ N`. -/ def lift_add_hom [add_comm_monoid M] [add_comm_monoid N] : (α → M →+ N) ≃+ ((α →₀ M) →+ N) := { to_fun := λ F, { to_fun := λ f, f.sum (λ x, F x), map_zero' := finset.sum_empty, map_add' := λ _ _, sum_add_index (λ x, (F x).map_zero) (λ x, (F x).map_add) }, inv_fun := λ F x, F.comp $ single_add_hom x, left_inv := λ F, by { ext, simp }, right_inv := λ F, by { ext, simp }, map_add' := λ F G, by { ext, simp } } @[simp] lemma lift_add_hom_apply [add_comm_monoid M] [add_comm_monoid N] (F : α → M →+ N) (f : α →₀ M) : lift_add_hom F f = f.sum (λ x, F x) := rfl @[simp] lemma lift_add_hom_symm_apply [add_comm_monoid M] [add_comm_monoid N] (F : (α →₀ M) →+ N) (x : α) : lift_add_hom.symm F x = F.comp (single_add_hom x) := rfl lemma lift_add_hom_symm_apply_apply [add_comm_monoid M] [add_comm_monoid N] (F : (α →₀ M) →+ N) (x : α) (y : M) : lift_add_hom.symm F x y = F (single x y) := rfl @[simp] lemma lift_add_hom_single_add_hom [add_comm_monoid M] : lift_add_hom (single_add_hom : α → M →+ α →₀ M) = add_monoid_hom.id _ := lift_add_hom.to_equiv.apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma sum_single [add_comm_monoid M] (f : α →₀ M) : f.sum single = f := add_monoid_hom.congr_fun lift_add_hom_single_add_hom f @[simp] lemma lift_add_hom_apply_single [add_comm_monoid M] [add_comm_monoid N] (f : α → M →+ N) (a : α) (b : M) : lift_add_hom f (single a b) = f a b := sum_single_index (f a).map_zero @[simp] lemma lift_add_hom_comp_single [add_comm_monoid M] [add_comm_monoid N] (f : α → M →+ N) (a : α) : (lift_add_hom f).comp (single_add_hom a) = f a := add_monoid_hom.ext $ λ b, lift_add_hom_apply_single f a b lemma comp_lift_add_hom [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] (g : N →+ P) (f : α → M →+ N) : g.comp (lift_add_hom f) = lift_add_hom (λ a, g.comp (f a)) := lift_add_hom.symm_apply_eq.1 $ funext $ λ a, by rw [lift_add_hom_symm_apply, add_monoid_hom.comp_assoc, lift_add_hom_comp_single] lemma sum_sub_index [add_comm_group β] [add_comm_group γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := (lift_add_hom (λ a, add_monoid_hom.of_map_sub (h a) (h_sub a))).map_sub f g @[to_additive] lemma prod_emb_domain [has_zero M] [comm_monoid N] {v : α →₀ M} {f : α ↪ β} {g : β → M → N} : (v.emb_domain f).prod g = v.prod (λ a b, g (f a) b) := begin rw [prod, prod, support_emb_domain, finset.prod_map], simp_rw emb_domain_apply, end @[to_additive] lemma prod_finset_sum_index [add_comm_monoid M] [comm_monoid N] {s : finset ι} {g : ι → α →₀ M} {h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : ∏ i in s, (g i).prod h = (∑ i in s, g i).prod h := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, sum_insert has, prod_add_index h_zero h_add] @[to_additive] lemma prod_sum_index [add_comm_monoid M] [add_comm_monoid N] [comm_monoid P] {f : α →₀ M} {g : α → M → β →₀ N} {h : β → N → P} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f.sum g).prod h = f.prod (λa b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm lemma multiset_sum_sum_index [add_comm_monoid M] [add_comm_monoid N] (f : multiset (α →₀ M)) (h : α → M → N) (h₀ : ∀a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : M), h a (b₁ + b₂) = h a b₁ + h a b₂) : (f.sum.sum h) = (f.map $ λg:α →₀ M, g.sum h).sum := multiset.induction_on f rfl $ assume a s ih, by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index h₀ h₁, ih] lemma support_sum_eq_bUnion {α : Type*} {ι : Type*} {M : Type*} [add_comm_monoid M] {g : ι → α →₀ M} (s : finset ι) (h : ∀ i₁ i₂, i₁ ≠ i₂ → disjoint (g i₁).support (g i₂).support) : (∑ i in s, g i).support = s.bUnion (λ i, (g i).support) := begin apply finset.induction_on s, { simp }, { intros i s hi, simp only [hi, sum_insert, not_false_iff, bUnion_insert], intro hs, rw [finsupp.support_add_eq, hs], rw [hs], intros x hx, simp only [mem_bUnion, exists_prop, inf_eq_inter, ne.def, mem_inter] at hx, obtain ⟨hxi, j, hj, hxj⟩ := hx, have hn : i ≠ j := λ H, hi (H.symm ▸ hj), apply h _ _ hn, simp [hxi, hxj] } end lemma multiset_map_sum [has_zero M] {f : α →₀ M} {m : β → γ} {h : α → M → multiset β} : multiset.map m (f.sum h) = f.sum (λa b, (h a b).map m) := (multiset.map_add_monoid_hom m).map_sum _ f.support lemma multiset_sum_sum [has_zero M] [add_comm_monoid N] {f : α →₀ M} {h : α → M → multiset N} : multiset.sum (f.sum h) = f.sum (λa b, multiset.sum (h a b)) := (multiset.sum_add_monoid_hom : multiset N →+ N).map_sum _ f.support section map_range section equiv variables [has_zero M] [has_zero N] [has_zero P] /-- `finsupp.map_range` as an equiv. -/ @[simps apply] def map_range.equiv (f : M ≃ N) (hf : f 0 = 0) (hf' : f.symm 0 = 0) : (α →₀ M) ≃ (α →₀ N) := { to_fun := (map_range f hf : (α →₀ M) → (α →₀ N)), inv_fun := (map_range f.symm hf' : (α →₀ N) → (α →₀ M)), left_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw equiv.symm_comp_self, { exact map_range_id _ }, { refl }, end, right_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw equiv.self_comp_symm, { exact map_range_id _ }, { refl }, end } @[simp] lemma map_range.equiv_refl : map_range.equiv (equiv.refl M) rfl rfl = equiv.refl (α →₀ M) := equiv.ext map_range_id lemma map_range.equiv_trans (f : M ≃ N) (hf : f 0 = 0) (hf') (f₂ : N ≃ P) (hf₂ : f₂ 0 = 0) (hf₂') : (map_range.equiv (f.trans f₂) (by rw [equiv.trans_apply, hf, hf₂]) (by rw [equiv.symm_trans_apply, hf₂', hf']) : (α →₀ _) ≃ _) = (map_range.equiv f hf hf').trans (map_range.equiv f₂ hf₂ hf₂') := equiv.ext $ map_range_comp _ _ _ _ _ @[simp] lemma map_range.equiv_symm (f : M ≃ N) (hf hf') : ((map_range.equiv f hf hf').symm : (α →₀ _) ≃ _) = map_range.equiv f.symm hf' hf := equiv.ext $ λ x, rfl end equiv section zero_hom variables [has_zero M] [has_zero N] [has_zero P] /-- Composition with a fixed zero-preserving homomorphism is itself an zero-preserving homomorphism on functions. -/ @[simps] def map_range.zero_hom (f : zero_hom M N) : zero_hom (α →₀ M) (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), map_zero' := map_range_zero } @[simp] lemma map_range.zero_hom_id : map_range.zero_hom (zero_hom.id M) = zero_hom.id (α →₀ M) := zero_hom.ext map_range_id lemma map_range.zero_hom_comp (f : zero_hom N P) (f₂ : zero_hom M N) : (map_range.zero_hom (f.comp f₂) : zero_hom (α →₀ _) _) = (map_range.zero_hom f).comp (map_range.zero_hom f₂) := zero_hom.ext $ map_range_comp _ _ _ _ _ end zero_hom section add_monoid_hom variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] /-- Composition with a fixed additive homomorphism is itself an additive homomorphism on functions. -/ @[simps] def map_range.add_monoid_hom (f : M →+ N) : (α →₀ M) →+ (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), map_zero' := map_range_zero, map_add' := λ a b, map_range_add f.map_add _ _ } @[simp] lemma map_range.add_monoid_hom_id : map_range.add_monoid_hom (add_monoid_hom.id M) = add_monoid_hom.id (α →₀ M) := add_monoid_hom.ext map_range_id lemma map_range.add_monoid_hom_comp (f : N →+ P) (f₂ : M →+ N) : (map_range.add_monoid_hom (f.comp f₂) : (α →₀ _) →+ _) = (map_range.add_monoid_hom f).comp (map_range.add_monoid_hom f₂) := add_monoid_hom.ext $ map_range_comp _ _ _ _ _ @[simp] lemma map_range.add_monoid_hom_to_zero_hom (f : M →+ N) : (map_range.add_monoid_hom f).to_zero_hom = (map_range.zero_hom f.to_zero_hom : zero_hom (α →₀ _) _) := zero_hom.ext $ λ _, rfl lemma map_range_multiset_sum (f : M →+ N) (m : multiset (α →₀ M)) : map_range f f.map_zero m.sum = (m.map $ λx, map_range f f.map_zero x).sum := (map_range.add_monoid_hom f : (α →₀ _) →+ _).map_multiset_sum _ lemma map_range_finset_sum (f : M →+ N) (s : finset ι) (g : ι → (α →₀ M)) : map_range f f.map_zero (∑ x in s, g x) = ∑ x in s, map_range f f.map_zero (g x) := (map_range.add_monoid_hom f : (α →₀ _) →+ _).map_sum _ _ /-- `finsupp.map_range.add_monoid_hom` as an equiv. -/ @[simps apply] def map_range.add_equiv (f : M ≃+ N) : (α →₀ M) ≃+ (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), inv_fun := (map_range f.symm f.symm.map_zero : (α →₀ N) → (α →₀ M)), left_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw add_equiv.symm_comp_self, { exact map_range_id _ }, { refl }, end, right_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw add_equiv.self_comp_symm, { exact map_range_id _ }, { refl }, end, ..(map_range.add_monoid_hom f.to_add_monoid_hom) } @[simp] lemma map_range.add_equiv_refl : map_range.add_equiv (add_equiv.refl M) = add_equiv.refl (α →₀ M) := add_equiv.ext map_range_id lemma map_range.add_equiv_trans (f : M ≃+ N) (f₂ : N ≃+ P) : (map_range.add_equiv (f.trans f₂) : (α →₀ _) ≃+ _) = (map_range.add_equiv f).trans (map_range.add_equiv f₂) := add_equiv.ext $ map_range_comp _ _ _ _ _ @[simp] lemma map_range.add_equiv_symm (f : M ≃+ N) : ((map_range.add_equiv f).symm : (α →₀ _) ≃+ _) = map_range.add_equiv f.symm := add_equiv.ext $ λ x, rfl @[simp] lemma map_range.add_equiv_to_add_monoid_hom (f : M ≃+ N) : (map_range.add_equiv f : (α →₀ _) ≃+ _).to_add_monoid_hom = (map_range.add_monoid_hom f.to_add_monoid_hom : (α →₀ _) →+ _) := add_monoid_hom.ext $ λ _, rfl @[simp] lemma map_range.add_equiv_to_equiv (f : M ≃+ N) : (map_range.add_equiv f).to_equiv = (map_range.equiv f.to_equiv f.map_zero f.symm.map_zero : (α →₀ _) ≃ _) := equiv.ext $ λ _, rfl end add_monoid_hom end map_range /-! ### Declarations about `map_domain` -/ section map_domain variables [add_comm_monoid M] {v v₁ v₂ : α →₀ M} /-- Given `f : α → β` and `v : α →₀ M`, `map_domain f v : β →₀ M` is the finitely supported function whose value at `a : β` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α → β) (v : α →₀ M) : β →₀ M := v.sum $ λa, single (f a) lemma map_domain_apply {f : α → β} (hf : function.injective f) (x : α →₀ M) (a : α) : map_domain f x (f a) = x a := begin rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same], { assume b _ hba, exact single_eq_of_ne (hf.ne hba) }, { assume h, rw [not_mem_support_iff.1 h, single_zero, zero_apply] } end lemma map_domain_notin_range {f : α → β} (x : α →₀ M) (a : β) (h : a ∉ set.range f) : map_domain f x a = 0 := begin rw [map_domain, sum_apply, sum], exact finset.sum_eq_zero (assume a' h', single_eq_of_ne $ assume eq, h $ eq ▸ set.mem_range_self _) end @[simp] lemma map_domain_id : map_domain id v = v := sum_single _ lemma map_domain_comp {f : α → β} {g : β → γ} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := begin refine ((sum_sum_index _ _).trans _).symm, { intros, exact single_zero }, { intros, exact single_add }, refine sum_congr rfl (λ _ _, sum_single_index _), { exact single_zero } end @[simp] lemma map_domain_single {f : α → β} {a : α} {b : M} : map_domain f (single a b) = single (f a) b := sum_single_index single_zero @[simp] lemma map_domain_zero {f : α → β} : map_domain f (0 : α →₀ M) = (0 : β →₀ M) := sum_zero_index lemma map_domain_congr {f g : α → β} (h : ∀x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ λ _ H, by simp only [h _ H] lemma map_domain_add {f : α → β} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ := sum_add_index (λ _, single_zero) (λ _ _ _, single_add) @[simp] lemma map_domain_equiv_apply {f : α ≃ β} (x : α →₀ M) (a : β) : map_domain f x a = x (f.symm a) := begin conv_lhs { rw ←f.apply_symm_apply a }, exact map_domain_apply f.injective _ _, end /-- `finsupp.map_domain` is an `add_monoid_hom`. -/ @[simps] def map_domain.add_monoid_hom (f : α → β) : (α →₀ M) →+ (β →₀ M) := { to_fun := map_domain f, map_zero' := map_domain_zero, map_add' := λ _ _, map_domain_add} @[simp] lemma map_domain.add_monoid_hom_id : map_domain.add_monoid_hom id = add_monoid_hom.id (α →₀ M) := add_monoid_hom.ext $ λ _, map_domain_id lemma map_domain.add_monoid_hom_comp (f : β → γ) (g : α → β) : (map_domain.add_monoid_hom (f ∘ g) : (α →₀ M) →+ (γ →₀ M)) = (map_domain.add_monoid_hom f).comp (map_domain.add_monoid_hom g) := add_monoid_hom.ext $ λ _, map_domain_comp lemma map_domain_finset_sum {f : α → β} {s : finset ι} {v : ι → α →₀ M} : map_domain f (∑ i in s, v i) = ∑ i in s, map_domain f (v i) := (map_domain.add_monoid_hom f : (α →₀ M) →+ β →₀ M).map_sum _ _ lemma map_domain_sum [has_zero N] {f : α → β} {s : α →₀ N} {v : α → N → α →₀ M} : map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) := (map_domain.add_monoid_hom f : (α →₀ M) →+ β →₀ M).map_finsupp_sum _ _ lemma map_domain_support [decidable_eq β] {f : α → β} {s : α →₀ M} : (s.map_domain f).support ⊆ s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bUnion_mono $ assume a ha, support_single_subset) $ by rw [finset.bUnion_singleton]; exact subset.refl _ @[to_additive] lemma prod_map_domain_index [comm_monoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (h_zero : ∀b, h b 0 = 1) (h_add : ∀b m₁ m₂, h b (m₁ + m₂) = h b m₁ * h b m₂) : (map_domain f s).prod h = s.prod (λa m, h (f a) m) := (prod_sum_index h_zero h_add).trans $ prod_congr rfl $ λ _ _, prod_single_index (h_zero _) /-- A version of `sum_map_domain_index` that takes a bundled `add_monoid_hom`, rather than separate linearity hypotheses. -/ -- Note that in `prod_map_domain_index`, `M` is still an additive monoid, -- so there is no analogous version in terms of `monoid_hom`. @[simp] lemma sum_map_domain_index_add_monoid_hom [add_comm_monoid N] {f : α → β} {s : α →₀ M} (h : β → M →+ N) : (map_domain f s).sum (λ b m, h b m) = s.sum (λ a m, h (f a) m) := @sum_map_domain_index _ _ _ _ _ _ _ _ (λ b m, h b m) (λ b, (h b).map_zero) (λ b m₁ m₂, (h b).map_add _ _) lemma emb_domain_eq_map_domain (f : α ↪ β) (v : α →₀ M) : emb_domain f v = map_domain f v := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a, rfl⟩, rw [map_domain_apply f.injective, emb_domain_apply] }, { rw [map_domain_notin_range, emb_domain_notin_range]; assumption } end @[to_additive] lemma prod_map_domain_index_inj [comm_monoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (hf : function.injective f) : (s.map_domain f).prod h = s.prod (λa b, h (f a) b) := by rw [←function.embedding.coe_fn_mk f hf, ←emb_domain_eq_map_domain, prod_emb_domain] lemma map_domain_injective {f : α → β} (hf : function.injective f) : function.injective (map_domain f : (α →₀ M) → (β →₀ M)) := begin assume v₁ v₂ eq, ext a, have : map_domain f v₁ (f a) = map_domain f v₂ (f a), { rw eq }, rwa [map_domain_apply hf, map_domain_apply hf] at this, end lemma map_domain.add_monoid_hom_comp_map_range [add_comm_monoid N] (f : α → β) (g : M →+ N) : (map_domain.add_monoid_hom f).comp (map_range.add_monoid_hom g) = (map_range.add_monoid_hom g).comp (map_domain.add_monoid_hom f) := by { ext, simp } /-- When `g` preserves addition, `map_range` and `map_domain` commute. -/ lemma map_domain_map_range [add_comm_monoid N] (f : α → β) (v : α →₀ M) (g : M → N) (h0 : g 0 = 0) (hadd : ∀ x y, g (x + y) = g x + g y) : map_domain f (map_range g h0 v) = map_range g h0 (map_domain f v) := let g' : M →+ N := { to_fun := g, map_zero' := h0, map_add' := hadd} in add_monoid_hom.congr_fun (map_domain.add_monoid_hom_comp_map_range f g') v end map_domain /-! ### Declarations about `comap_domain` -/ section comap_domain /-- Given `f : α → β`, `l : β →₀ M` and a proof `hf` that `f` is injective on the preimage of `l.support`, `comap_domain f l hf` is the finitely supported function from `α` to `M` given by composing `l` with `f`. -/ def comap_domain [has_zero M] (f : α → β) (l : β →₀ M) (hf : set.inj_on f (f ⁻¹' ↑l.support)) : α →₀ M := { support := l.support.preimage f hf, to_fun := (λ a, l (f a)), mem_support_to_fun := begin intros a, simp only [finset.mem_def.symm, finset.mem_preimage], exact l.mem_support_to_fun (f a), end } @[simp] lemma comap_domain_apply [has_zero M] (f : α → β) (l : β →₀ M) (hf : set.inj_on f (f ⁻¹' ↑l.support)) (a : α) : comap_domain f l hf a = l (f a) := rfl lemma sum_comap_domain [has_zero M] [add_comm_monoid N] (f : α → β) (l : β →₀ M) (g : β → M → N) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : (comap_domain f l hf.inj_on).sum (g ∘ f) = l.sum g := begin simp only [sum, comap_domain_apply, (∘)], simp [comap_domain, finset.sum_preimage_of_bij f _ _ (λ x, g x (l x))], end lemma eq_zero_of_comap_domain_eq_zero [add_comm_monoid M] (f : α → β) (l : β →₀ M) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : comap_domain f l hf.inj_on = 0 → l = 0 := begin rw [← support_eq_empty, ← support_eq_empty, comap_domain], simp only [finset.ext_iff, finset.not_mem_empty, iff_false, mem_preimage], assume h a ha, cases hf.2.2 ha with b hb, exact h b (hb.2.symm ▸ ha) end lemma map_domain_comap_domain [add_comm_monoid M] (f : α → β) (l : β →₀ M) (hf : function.injective f) (hl : ↑l.support ⊆ set.range f): map_domain f (comap_domain f l (hf.inj_on _)) = l := begin ext a, by_cases h_cases: a ∈ set.range f, { rcases set.mem_range.1 h_cases with ⟨b, hb⟩, rw [hb.symm, map_domain_apply hf, comap_domain_apply] }, { rw map_domain_notin_range _ _ h_cases, by_contra h_contr, apply h_cases (hl $ finset.mem_coe.2 $ mem_support_iff.2 $ λ h, h_contr h.symm) } end end comap_domain section option /-- Restrict a finitely supported function on `option α` to a finitely supported function on `α`. -/ def some [has_zero M] (f : option α →₀ M) : α →₀ M := f.comap_domain option.some (λ _, by simp) @[simp] lemma some_apply [has_zero M] (f : option α →₀ M) (a : α) : f.some a = f (option.some a) := rfl @[simp] lemma some_zero [has_zero M] : (0 : option α →₀ M).some = 0 := by { ext, simp, } @[simp] lemma some_add [add_comm_monoid M] (f g : option α →₀ M) : (f + g).some = f.some + g.some := by { ext, simp, } @[simp] lemma some_single_none [has_zero M] (m : M) : (single none m : option α →₀ M).some = 0 := by { ext, simp, } @[simp] lemma some_single_some [has_zero M] (a : α) (m : M) : (single (option.some a) m : option α →₀ M).some = single a m := by { ext b, simp [single_apply], } @[to_additive] lemma prod_option_index [add_comm_monoid M] [comm_monoid N] (f : option α →₀ M) (b : option α → M → N) (h_zero : ∀ o, b o 0 = 1) (h_add : ∀ o m₁ m₂, b o (m₁ + m₂) = b o m₁ * b o m₂) : f.prod b = b none (f none) * f.some.prod (λ a, b (option.some a)) := begin apply induction_linear f, { simp [h_zero], }, { intros f₁ f₂ h₁ h₂, rw [finsupp.prod_add_index, h₁, h₂, some_add, finsupp.prod_add_index], simp only [h_add, pi.add_apply, finsupp.coe_add], rw mul_mul_mul_comm, all_goals { simp [h_zero, h_add], }, }, { rintros (_|a) m; simp [h_zero, h_add], } end lemma sum_option_index_smul [semiring R] [add_comm_monoid M] [module R M] (f : option α →₀ R) (b : option α → M) : f.sum (λ o r, r • b o) = f none • b none + f.some.sum (λ a r, r • b (option.some a)) := f.sum_option_index _ (λ _, zero_smul _ _) (λ _ _ _, add_smul _ _ _) end option /-! ### Declarations about `equiv_congr_left` -/ section equiv_congr_left variable [has_zero M] /-- Given `f : α ≃ β`, we can map `l : α →₀ M` to `equiv_map_domain f l : β →₀ M` (computably) by mapping the support forwards and the function backwards. -/ def equiv_map_domain (f : α ≃ β) (l : α →₀ M) : β →₀ M := { support := l.support.map f.to_embedding, to_fun := λ a, l (f.symm a), mem_support_to_fun := λ a, by simp only [finset.mem_map_equiv, mem_support_to_fun]; refl } @[simp] lemma equiv_map_domain_apply (f : α ≃ β) (l : α →₀ M) (b : β) : equiv_map_domain f l b = l (f.symm b) := rfl lemma equiv_map_domain_symm_apply (f : α ≃ β) (l : β →₀ M) (a : α) : equiv_map_domain f.symm l a = l (f a) := rfl @[simp] lemma equiv_map_domain_refl (l : α →₀ M) : equiv_map_domain (equiv.refl _) l = l := by ext x; refl lemma equiv_map_domain_refl' : equiv_map_domain (equiv.refl _) = @id (α →₀ M) := by ext x; refl lemma equiv_map_domain_trans (f : α ≃ β) (g : β ≃ γ) (l : α →₀ M) : equiv_map_domain (f.trans g) l = equiv_map_domain g (equiv_map_domain f l) := by ext x; refl lemma equiv_map_domain_trans' (f : α ≃ β) (g : β ≃ γ) : @equiv_map_domain _ _ M _ (f.trans g) = equiv_map_domain g ∘ equiv_map_domain f := by ext x; refl @[simp] lemma equiv_map_domain_single (f : α ≃ β) (a : α) (b : M) : equiv_map_domain f (single a b) = single (f a) b := by ext x; simp only [single_apply, equiv.apply_eq_iff_eq_symm_apply, equiv_map_domain_apply]; congr @[simp] lemma equiv_map_domain_zero {f : α ≃ β} : equiv_map_domain f (0 : α →₀ M) = (0 : β →₀ M) := by ext x; simp only [equiv_map_domain_apply, coe_zero, pi.zero_apply] lemma equiv_map_domain_eq_map_domain {M} [add_comm_monoid M] (f : α ≃ β) (l : α →₀ M) : equiv_map_domain f l = map_domain f l := by ext x; simp [map_domain_equiv_apply] /-- Given `f : α ≃ β`, the finitely supported function spaces are also in bijection: `(α →₀ M) ≃ (β →₀ M)`. This is the finitely-supported version of `equiv.Pi_congr_left`. -/ def equiv_congr_left (f : α ≃ β) : (α →₀ M) ≃ (β →₀ M) := by refine ⟨equiv_map_domain f, equiv_map_domain f.symm, λ f, _, λ f, _⟩; ext x; simp only [equiv_map_domain_apply, equiv.symm_symm, equiv.symm_apply_apply, equiv.apply_symm_apply] @[simp] lemma equiv_congr_left_apply (f : α ≃ β) (l : α →₀ M) : equiv_congr_left f l = equiv_map_domain f l := rfl @[simp] lemma equiv_congr_left_symm (f : α ≃ β) : (@equiv_congr_left _ _ M _ f).symm = equiv_congr_left f.symm := rfl end equiv_congr_left /-! ### Declarations about `filter` -/ section filter section has_zero variables [has_zero M] (p : α → Prop) (f : α →₀ M) /-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : α → Prop) (f : α →₀ M) : α →₀ M := { to_fun := λ a, if p a then f a else 0, support := f.support.filter (λ a, p a), mem_support_to_fun := λ a, by split_ifs; { simp only [h, mem_filter, mem_support_iff], tauto } } lemma filter_apply (a : α) [D : decidable (p a)] : f.filter p a = if p a then f a else 0 := by rw subsingleton.elim D; refl lemma filter_eq_indicator : ⇑(f.filter p) = set.indicator {x | p x} f := rfl @[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := if_pos h @[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 := if_neg h @[simp] lemma support_filter [D : decidable_pred p] : (f.filter p).support = f.support.filter p := by rw subsingleton.elim D; refl lemma filter_zero : (0 : α →₀ M).filter p = 0 := by rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty] @[simp] lemma filter_single_of_pos {a : α} {b : M} (h : p a) : (single a b).filter p = single a b := coe_fn_injective $ by simp [filter_eq_indicator, set.subset_def, mem_support_single, h] @[simp] lemma filter_single_of_neg {a : α} {b : M} (h : ¬ p a) : (single a b).filter p = 0 := ext $ by simp [filter_eq_indicator, single_apply_eq_zero, @imp.swap (p _), h] end has_zero lemma filter_pos_add_filter_neg [add_zero_class M] (f : α →₀ M) (p : α → Prop) : f.filter p + f.filter (λa, ¬ p a) = f := coe_fn_injective $ set.indicator_self_add_compl {x | p x} f end filter /-! ### Declarations about `frange` -/ section frange variables [has_zero M] /-- `frange f` is the image of `f` on the support of `f`. -/ def frange (f : α →₀ M) : finset M := finset.image f f.support theorem mem_frange {f : α →₀ M} {y : M} : y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y := finset.mem_image.trans ⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩, λ ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩ theorem zero_not_mem_frange {f : α →₀ M} : (0:M) ∉ f.frange := λ H, (mem_frange.1 H).1 rfl theorem frange_single {x : α} {y : M} : frange (single x y) ⊆ {y} := λ r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 ▸ (by rw single_apply at ht2 ⊢; split_ifs at ht2 ⊢; [exact finset.mem_singleton_self _, cc]) end frange /-! ### Declarations about `subtype_domain` -/ section subtype_domain section zero variables [has_zero M] {p : α → Prop} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain (p : α → Prop) (f : α →₀ M) : (subtype p →₀ M) := ⟨f.support.subtype p, f ∘ coe, λ a, by simp only [mem_subtype, mem_support_iff]⟩ @[simp] lemma support_subtype_domain [D : decidable_pred p] {f : α →₀ M} : (subtype_domain p f).support = f.support.subtype p := by rw subsingleton.elim D; refl @[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ M} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ M) = 0 := rfl lemma subtype_domain_eq_zero_iff' {f : α →₀ M} : f.subtype_domain p = 0 ↔ ∀ x, p x → f x = 0 := by simp_rw [← support_eq_empty, support_subtype_domain, subtype_eq_empty, not_mem_support_iff] lemma subtype_domain_eq_zero_iff {f : α →₀ M} (hf : ∀ x ∈ f.support , p x) : f.subtype_domain p = 0 ↔ f = 0 := subtype_domain_eq_zero_iff'.trans ⟨λ H, ext $ λ x, if hx : p x then H x hx else not_mem_support_iff.1 $ mt (hf x) hx, λ H x _, by simp [H]⟩ @[to_additive] lemma prod_subtype_domain_index [comm_monoid N] {v : α →₀ M} {h : α → M → N} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λa b, h a b) = v.prod h := prod_bij (λp _, p.val) (λ _, mem_subtype.1) (λ _ _, rfl) (λ _ _ _ _, subtype.eq) (λ b hb, ⟨⟨b, hp b hb⟩, mem_subtype.2 hb, rfl⟩) end zero section add_zero_class variables [add_zero_class M] {p : α → Prop} {v v' : α →₀ M} @[simp] lemma subtype_domain_add {v v' : α →₀ M} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ _, rfl /-- `subtype_domain` but as an `add_monoid_hom`. -/ def subtype_domain_add_monoid_hom : (α →₀ M) →+ subtype p →₀ M := { to_fun := subtype_domain p, map_zero' := subtype_domain_zero, map_add' := λ _ _, subtype_domain_add } /-- `finsupp.filter` as an `add_monoid_hom`. -/ def filter_add_hom (p : α → Prop) : (α →₀ M) →+ (α →₀ M) := { to_fun := filter p, map_zero' := filter_zero p, map_add' := λ f g, coe_fn_injective $ set.indicator_add {x | p x} f g } @[simp] lemma filter_add {v v' : α →₀ M} : (v + v').filter p = v.filter p + v'.filter p := (filter_add_hom p).map_add v v' end add_zero_class section comm_monoid variables [add_comm_monoid M] {p : α → Prop} lemma subtype_domain_sum {s : finset ι} {h : ι → α →₀ M} : (∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p := (subtype_domain_add_monoid_hom : _ →+ subtype p →₀ M).map_sum _ s lemma subtype_domain_finsupp_sum [has_zero N] {s : β →₀ N} {h : β → N → α →₀ M} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum lemma filter_sum (s : finset ι) (f : ι → α →₀ M) : (∑ a in s, f a).filter p = ∑ a in s, filter p (f a) := (filter_add_hom p : (α →₀ M) →+ _).map_sum f s lemma filter_eq_sum (p : α → Prop) [D : decidable_pred p] (f : α →₀ M) : f.filter p = ∑ i in f.support.filter p, single i (f i) := (f.filter p).sum_single.symm.trans $ finset.sum_congr (by rw subsingleton.elim D; refl) $ λ x hx, by rw [filter_apply_pos _ _ (mem_filter.1 hx).2] end comm_monoid section group variables [add_group G] {p : α → Prop} {v v' : α →₀ G} @[simp] lemma subtype_domain_neg : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ _, rfl @[simp] lemma subtype_domain_sub : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ _, rfl @[simp] lemma single_neg {a : α} {b : G} : single a (-b) = -single a b := (single_add_hom a : G →+ _).map_neg b @[simp] lemma single_sub {a : α} {b₁ b₂ : G} : single a (b₁ - b₂) = single a b₁ - single a b₂ := (single_add_hom a : G →+ _).map_sub b₁ b₂ end group end subtype_domain /-! ### Declarations relating `finsupp` to `multiset` -/ section multiset /-- Given `f : α →₀ ℕ`, `f.to_multiset` is the multiset with multiplicities given by the values of `f` on the elements of `α`. We define this function as an `add_equiv`. -/ def to_multiset : (α →₀ ℕ) ≃+ multiset α := { to_fun := λ f, f.sum (λa n, n • {a}), inv_fun := λ s, ⟨s.to_finset, λ a, s.count a, λ a, by simp⟩, left_inv := λ f, ext $ λ a, suffices (if f a = 0 then 0 else f a) = f a, by simpa [finsupp.sum, multiset.count_sum', multiset.count_cons], by split_ifs with h; [rw h, refl], right_inv := λ s, by simp [finsupp.sum], map_add' := λ f g, sum_add_index (λ a, zero_nsmul _) (λ a, add_nsmul _) } lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 := rfl lemma to_multiset_add (m n : α →₀ ℕ) : (m + n).to_multiset = m.to_multiset + n.to_multiset := to_multiset.map_add m n lemma to_multiset_apply (f : α →₀ ℕ) : f.to_multiset = f.sum (λ a n, n • {a}) := rfl @[simp] lemma to_multiset_symm_apply (s : multiset α) (x : α) : finsupp.to_multiset.symm s x = s.count x := rfl @[simp] lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (single a n) = n • {a} := by rw [to_multiset_apply, sum_single_index]; apply zero_nsmul lemma to_multiset_sum {ι : Type*} {f : ι → α →₀ ℕ} (s : finset ι) : finsupp.to_multiset (∑ i in s, f i) = ∑ i in s, finsupp.to_multiset (f i) := add_equiv.map_sum _ _ _ lemma to_multiset_sum_single {ι : Type*} (s : finset ι) (n : ℕ) : finsupp.to_multiset (∑ i in s, single i n) = n • s.val := by simp_rw [to_multiset_sum, finsupp.to_multiset_single, multiset.singleton_eq_singleton, sum_nsmul, sum_multiset_singleton] lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λa, id) := by simp [to_multiset_apply, add_monoid_hom.map_finsupp_sum, function.id_def] lemma to_multiset_map (f : α →₀ ℕ) (g : α → β) : f.to_multiset.map g = (f.map_domain g).to_multiset := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single, to_multiset_single, to_multiset_add, to_multiset_single, ← multiset.coe_map_add_monoid_hom, (multiset.map_add_monoid_hom g).map_nsmul], refl } end @[simp] lemma prod_to_multiset [comm_monoid M] (f : M →₀ ℕ) : f.to_multiset.prod = f.prod (λa n, a ^ n) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index, finsupp.prod_single_index, multiset.prod_nsmul, multiset.singleton_eq_singleton, multiset.prod_singleton], { exact pow_zero a }, { exact pow_zero }, { exact pow_add } } end @[simp] lemma to_finset_to_multiset [decidable_eq α] (f : α →₀ ℕ) : f.to_multiset.to_finset = f.support := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.to_finset_zero, support_zero] }, { assume a n f ha hn ih, rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq, support_single_ne_zero hn, multiset.to_finset_nsmul _ _ hn, multiset.singleton_eq_singleton, multiset.to_finset_cons, multiset.to_finset_zero], refl, refine disjoint.mono_left support_single_subset _, rwa [finset.singleton_disjoint] } end @[simp] lemma count_to_multiset [decidable_eq α] (f : α →₀ ℕ) (a : α) : f.to_multiset.count a = f a := calc f.to_multiset.count a = f.sum (λx n, (n • {x} : multiset α).count a) : (multiset.count_add_monoid_hom a).map_sum _ f.support ... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_nsmul] ... = f.sum (λx n, n * (x ::ₘ 0 : multiset α).count a) : rfl ... = f a * (a ::ₘ 0 : multiset α).count a : sum_eq_single _ (λ a' _ H, by simp only [multiset.count_cons_of_ne (ne.symm H), multiset.count_zero, mul_zero]) (λ H, by simp only [not_mem_support_iff.1 H, zero_mul]) ... = f a : by simp only [multiset.count_singleton, mul_one] lemma mem_support_multiset_sum [add_comm_monoid M] {s : multiset (α →₀ M)} (a : α) : a ∈ s.sum.support → ∃f∈s, a ∈ (f : α →₀ M).support := multiset.induction_on s false.elim begin assume f s ih ha, by_cases a ∈ f.support, { exact ⟨f, multiset.mem_cons_self _ _, h⟩ }, { simp only [multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha, rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩, exact ⟨f', multiset.mem_cons_of_mem h₀, h₁⟩ } end lemma mem_support_finset_sum [add_comm_monoid M] {s : finset ι} {h : ι → α →₀ M} (a : α) (ha : a ∈ (∑ c in s, h c).support) : ∃ c ∈ s, a ∈ (h c).support := let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in ⟨c, hc, eq.symm ▸ hfa⟩ @[simp] lemma mem_to_multiset (f : α →₀ ℕ) (i : α) : i ∈ f.to_multiset ↔ i ∈ f.support := by rw [← multiset.count_ne_zero, finsupp.count_to_multiset, finsupp.mem_support_iff] end multiset /-! ### Declarations about `curry` and `uncurry` -/ section curry_uncurry variables [add_comm_monoid M] [add_comm_monoid N] /-- Given a finitely supported function `f` from a product type `α × β` to `γ`, `curry f` is the "curried" finitely supported function from `α` to the type of finitely supported functions from `β` to `γ`. -/ protected def curry (f : (α × β) →₀ M) : α →₀ (β →₀ M) := f.sum $ λp c, single p.1 (single p.2 c) @[simp] lemma curry_apply (f : (α × β) →₀ M) (x : α) (y : β) : f.curry x y = f (x, y) := begin have : ∀ (b : α × β), single b.fst (single b.snd (f b)) x y = if b = (x, y) then f b else 0, { rintros ⟨b₁, b₂⟩, simp [single_apply, ite_apply, prod.ext_iff, ite_and], split_ifs; simp [single_apply, *] }, rw [finsupp.curry, sum_apply, sum_apply, finsupp.sum, finset.sum_eq_single, this, if_pos rfl], { intros b hb b_ne, rw [this b, if_neg b_ne] }, { intros hxy, rw [this (x, y), if_pos rfl, not_mem_support_iff.mp hxy] } end lemma sum_curry_index (f : (α × β) →₀ M) (g : α → β → M → N) (hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) : f.curry.sum (λa f, f.sum (g a)) = f.sum (λp c, g p.1 p.2 c) := begin rw [finsupp.curry], transitivity, { exact sum_sum_index (assume a, sum_zero_index) (assume a b₀ b₁, sum_add_index (assume a, hg₀ _ _) (assume c d₀ d₁, hg₁ _ _ _ _)) }, congr, funext p c, transitivity, { exact sum_single_index sum_zero_index }, exact sum_single_index (hg₀ _ _) end /-- Given a finitely supported function `f` from `α` to the type of finitely supported functions from `β` to `M`, `uncurry f` is the "uncurried" finitely supported function from `α × β` to `M`. -/ protected def uncurry (f : α →₀ (β →₀ M)) : (α × β) →₀ M := f.sum $ λa g, g.sum $ λb c, single (a, b) c /-- `finsupp_prod_equiv` defines the `equiv` between `((α × β) →₀ M)` and `(α →₀ (β →₀ M))` given by currying and uncurrying. -/ def finsupp_prod_equiv : ((α × β) →₀ M) ≃ (α →₀ (β →₀ M)) := by refine ⟨finsupp.curry, finsupp.uncurry, λ f, _, λ f, _⟩; simp only [ finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] lemma filter_curry (f : α × β →₀ M) (p : α → Prop) : (f.filter (λa:α×β, p a.1)).curry = f.curry.filter p := begin rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, filter_sum, support_filter, sum_filter], refine finset.sum_congr rfl _, rintros ⟨a₁, a₂⟩ ha, dsimp only, split_ifs, { rw [filter_apply_pos, filter_single_of_pos]; exact h }, { rwa [filter_single_of_neg] } end lemma support_curry [decidable_eq α] (f : α × β →₀ M) : f.curry.support ⊆ f.support.image prod.fst := begin rw ← finset.bUnion_singleton, refine finset.subset.trans support_sum _, refine finset.bUnion_mono (assume a _, support_single_subset) end end curry_uncurry section sum /-- `finsupp.sum_elim f g` maps `inl x` to `f x` and `inr y` to `g y`. -/ def sum_elim {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) : α ⊕ β →₀ γ := on_finset ((f.support.map ⟨_, sum.inl_injective⟩) ∪ g.support.map ⟨_, sum.inr_injective⟩) (sum.elim f g) (λ ab h, by { cases ab with a b; simp only [sum.elim_inl, sum.elim_inr] at h; simpa }) @[simp] lemma coe_sum_elim {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) : ⇑(sum_elim f g) = sum.elim f g := rfl lemma sum_elim_apply {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : α ⊕ β) : sum_elim f g x = sum.elim f g x := rfl lemma sum_elim_inl {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : α) : sum_elim f g (sum.inl x) = f x := rfl lemma sum_elim_inr {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : β) : sum_elim f g (sum.inr x) = g x := rfl /-- The equivalence between `(α ⊕ β) →₀ γ` and `(α →₀ γ) × (β →₀ γ)`. This is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/ @[simps apply symm_apply] def sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] : ((α ⊕ β) →₀ γ) ≃ (α →₀ γ) × (β →₀ γ) := { to_fun := λ f, ⟨f.comap_domain sum.inl (sum.inl_injective.inj_on _), f.comap_domain sum.inr (sum.inr_injective.inj_on _)⟩, inv_fun := λ fg, sum_elim fg.1 fg.2, left_inv := λ f, by { ext ab, cases ab with a b; simp }, right_inv := λ fg, by { ext; simp } } lemma fst_sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] (f : (α ⊕ β) →₀ γ) (x : α) : (sum_finsupp_equiv_prod_finsupp f).1 x = f (sum.inl x) := rfl lemma snd_sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] (f : (α ⊕ β) →₀ γ) (y : β) : (sum_finsupp_equiv_prod_finsupp f).2 y = f (sum.inr y) := rfl lemma sum_finsupp_equiv_prod_finsupp_symm_inl {α β γ : Type*} [has_zero γ] (fg : (α →₀ γ) × (β →₀ γ)) (x : α) : (sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x := rfl lemma sum_finsupp_equiv_prod_finsupp_symm_inr {α β γ : Type*} [has_zero γ] (fg : (α →₀ γ) × (β →₀ γ)) (y : β) : (sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y := rfl variables [add_monoid M] /-- The additive equivalence between `(α ⊕ β) →₀ M` and `(α →₀ M) × (β →₀ M)`. This is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/ @[simps apply symm_apply] def sum_finsupp_add_equiv_prod_finsupp {α β : Type*} : ((α ⊕ β) →₀ M) ≃+ (α →₀ M) × (β →₀ M) := { map_add' := by { intros, ext; simp only [equiv.to_fun_as_coe, prod.fst_add, prod.snd_add, add_apply, snd_sum_finsupp_equiv_prod_finsupp, fst_sum_finsupp_equiv_prod_finsupp] }, .. sum_finsupp_equiv_prod_finsupp } lemma fst_sum_finsupp_add_equiv_prod_finsupp {α β : Type*} (f : (α ⊕ β) →₀ M) (x : α) : (sum_finsupp_add_equiv_prod_finsupp f).1 x = f (sum.inl x) := rfl lemma snd_sum_finsupp_add_equiv_prod_finsupp {α β : Type*} (f : (α ⊕ β) →₀ M) (y : β) : (sum_finsupp_add_equiv_prod_finsupp f).2 y = f (sum.inr y) := rfl lemma sum_finsupp_add_equiv_prod_finsupp_symm_inl {α β : Type*} (fg : (α →₀ M) × (β →₀ M)) (x : α) : (sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x := rfl lemma sum_finsupp_add_equiv_prod_finsupp_symm_inr {α β : Type*} (fg : (α →₀ M) × (β →₀ M)) (y : β) : (sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y := rfl end sum section variables [group G] [mul_action G α] [add_comm_monoid M] /-- Scalar multiplication by a group element g, given by precomposition with the action of g⁻¹ on the domain. -/ def comap_has_scalar : has_scalar G (α →₀ M) := { smul := λ g f, f.comap_domain (λ a, g⁻¹ • a) (λ a a' m m' h, by simpa [←mul_smul] using (congr_arg (λ a, g • a) h)) } local attribute [instance] comap_has_scalar /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is multiplicative in g. -/ def comap_mul_action : mul_action G (α →₀ M) := { one_smul := λ f, by { ext, dsimp [(•)], simp, }, mul_smul := λ g g' f, by { ext, dsimp [(•)], simp [mul_smul], }, } local attribute [instance] comap_mul_action /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is additive in the second argument. -/ def comap_distrib_mul_action : distrib_mul_action G (α →₀ M) := { smul_zero := λ g, by { ext, dsimp [(•)], simp, }, smul_add := λ g f f', by { ext, dsimp [(•)], simp, }, } /-- Scalar multiplication by a group element on finitely supported functions on a group, given by precomposition with the action of g⁻¹. -/ def comap_distrib_mul_action_self : distrib_mul_action G (G →₀ M) := @finsupp.comap_distrib_mul_action G M G _ (monoid.to_mul_action G) _ @[simp] lemma comap_smul_single (g : G) (a : α) (b : M) : g • single a b = single (g • a) b := begin ext a', dsimp [(•)], by_cases h : g • a = a', { subst h, simp [←mul_smul], }, { simp [single_eq_of_ne h], rw [single_eq_of_ne], rintro rfl, simpa [←mul_smul] using h, } end @[simp] lemma comap_smul_apply (g : G) (f : α →₀ M) (a : α) : (g • f) a = f (g⁻¹ • a) := rfl end section instance [monoid R] [add_monoid M] [distrib_mul_action R M] : has_scalar R (α →₀ M) := ⟨λa v, v.map_range ((•) a) (smul_zero _)⟩ /-! Throughout this section, some `monoid` and `semiring` arguments are specified with `{}` instead of `[]`. See note [implicit instance arguments]. -/ @[simp] lemma coe_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] (b : R) (v : α →₀ M) : ⇑(b • v) = b • v := rfl lemma smul_apply {_ : monoid R} [add_monoid M] [distrib_mul_action R M] (b : R) (v : α →₀ M) (a : α) : (b • v) a = b • (v a) := rfl lemma _root_.is_smul_regular.finsupp {_ : monoid R} [add_monoid M] [distrib_mul_action R M] {k : R} (hk : is_smul_regular M k) : is_smul_regular (α →₀ M) k := λ _ _ h, ext $ λ i, hk (congr_fun h i) instance [monoid R] [nonempty α] [add_monoid M] [distrib_mul_action R M] [has_faithful_scalar R M] : has_faithful_scalar R (α →₀ M) := { eq_of_smul_eq_smul := λ r₁ r₂ h, let ⟨a⟩ := ‹nonempty α› in eq_of_smul_eq_smul $ λ m : M, by simpa using congr_fun (h (single a m)) a } variables (α M) instance [monoid R] [add_monoid M] [distrib_mul_action R M] : distrib_mul_action R (α →₀ M) := { smul := (•), smul_add := λ a x y, ext $ λ _, smul_add _ _ _, one_smul := λ x, ext $ λ _, one_smul _ _, mul_smul := λ r s x, ext $ λ _, mul_smul _ _ _, smul_zero := λ x, ext $ λ _, smul_zero _ } instance [monoid R] [monoid S] [add_monoid M] [distrib_mul_action R M] [distrib_mul_action S M] [has_scalar R S] [is_scalar_tower R S M] : is_scalar_tower R S (α →₀ M) := { smul_assoc := λ r s a, ext $ λ _, smul_assoc _ _ _ } instance [monoid R] [monoid S] [add_monoid M] [distrib_mul_action R M] [distrib_mul_action S M] [smul_comm_class R S M] : smul_comm_class R S (α →₀ M) := { smul_comm := λ r s a, ext $ λ _, smul_comm _ _ _ } instance [semiring R] [add_comm_monoid M] [module R M] : module R (α →₀ M) := { smul := (•), zero_smul := λ x, ext $ λ _, zero_smul _ _, add_smul := λ a x y, ext $ λ _, add_smul _ _ _, .. finsupp.distrib_mul_action α M } variables {α M} {R} lemma support_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] {b : R} {g : α →₀ M} : (b • g).support ⊆ g.support := λ a, by { simp only [smul_apply, mem_support_iff, ne.def], exact mt (λ h, h.symm ▸ smul_zero _) } section variables {p : α → Prop} @[simp] lemma filter_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] {b : R} {v : α →₀ M} : (b • v).filter p = b • v.filter p := coe_fn_injective $ set.indicator_smul {x | p x} b v end lemma map_domain_smul {_ : monoid R} [add_comm_monoid M] [distrib_mul_action R M] {f : α → β} (b : R) (v : α →₀ M) : map_domain f (b • v) = b • map_domain f v := begin change map_domain f (map_range _ _ _) = map_range _ _ _, apply finsupp.induction v, { simp only [map_domain_zero, map_range_zero] }, intros a b v' hv₁ hv₂ IH, rw [map_range_add, map_domain_add, IH, map_domain_add, map_range_add, map_range_single, map_domain_single, map_domain_single, map_range_single]; apply smul_add end @[simp] lemma smul_single {_ : monoid R} [add_monoid M] [distrib_mul_action R M] (c : R) (a : α) (b : M) : c • finsupp.single a b = finsupp.single a (c • b) := map_range_single @[simp] lemma smul_single' {_ : semiring R} (c : R) (a : α) (b : R) : c • finsupp.single a b = finsupp.single a (c * b) := smul_single _ _ _ lemma map_range_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] [add_monoid N] [distrib_mul_action R N] {f : M → N} {hf : f 0 = 0} (c : R) (v : α →₀ M) (hsmul : ∀ x, f (c • x) = c • f x) : map_range f hf (c • v) = c • map_range f hf v := begin erw ←map_range_comp, have : (f ∘ (•) c) = ((•) c ∘ f) := funext hsmul, simp_rw this, apply map_range_comp, rw [function.comp_apply, smul_zero, hf], end lemma smul_single_one [semiring R] (a : α) (b : R) : b • single a 1 = single a b := by rw [smul_single, smul_eq_mul, mul_one] end lemma sum_smul_index [semiring R] [add_comm_monoid M] {g : α →₀ R} {b : R} {h : α → R → M} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) := finsupp.sum_map_range_index h0 lemma sum_smul_index' [monoid R] [add_monoid M] [distrib_mul_action R M] [add_comm_monoid N] {g : α →₀ M} {b : R} {h : α → M → N} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi c, h i (b • c)) := finsupp.sum_map_range_index h0 /-- A version of `finsupp.sum_smul_index'` for bundled additive maps. -/ lemma sum_smul_index_add_monoid_hom [monoid R] [add_monoid M] [add_comm_monoid N] [distrib_mul_action R M] {g : α →₀ M} {b : R} {h : α → M →+ N} : (b • g).sum (λ a, h a) = g.sum (λ i c, h i (b • c)) := sum_map_range_index (λ i, (h i).map_zero) instance [semiring R] [add_comm_monoid M] [module R M] {ι : Type*} [no_zero_smul_divisors R M] : no_zero_smul_divisors R (ι →₀ M) := ⟨λ c f h, or_iff_not_imp_left.mpr (λ hc, finsupp.ext (λ i, (smul_eq_zero.mp (finsupp.ext_iff.mp h i)).resolve_left hc))⟩ section distrib_mul_action_hom variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid N] [distrib_mul_action R M] [distrib_mul_action R N] /-- `finsupp.single` as a `distrib_mul_action_hom`. See also `finsupp.lsingle` for the version as a linear map. -/ def distrib_mul_action_hom.single (a : α) : M →+[R] (α →₀ M) := { map_smul' := λ k m, by simp only [add_monoid_hom.to_fun_eq_coe, single_add_hom_apply, smul_single], .. single_add_hom a } lemma distrib_mul_action_hom_ext {f g : (α →₀ M) →+[R] N} (h : ∀ (a : α) (m : M), f (single a m) = g (single a m)) : f = g := distrib_mul_action_hom.to_add_monoid_hom_injective $ add_hom_ext h /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma distrib_mul_action_hom_ext' {f g : (α →₀ M) →+[R] N} (h : ∀ (a : α), f.comp (distrib_mul_action_hom.single a) = g.comp (distrib_mul_action_hom.single a)) : f = g := distrib_mul_action_hom_ext $ λ a, distrib_mul_action_hom.congr_fun (h a) end distrib_mul_action_hom section variables [has_zero R] /-- The `finsupp` version of `pi.unique`. -/ instance unique_of_right [subsingleton R] : unique (α →₀ R) := { uniq := λ l, ext $ λ i, subsingleton.elim _ _, .. finsupp.inhabited } /-- The `finsupp` version of `pi.unique_of_is_empty`. -/ instance unique_of_left [is_empty α] : unique (α →₀ R) := { uniq := λ l, ext is_empty_elim, .. finsupp.inhabited } end /-- Given an `add_comm_monoid M` and `s : set α`, `restrict_support_equiv s M` is the `equiv` between the subtype of finitely supported functions with support contained in `s` and the type of finitely supported functions from `s`. -/ def restrict_support_equiv (s : set α) (M : Type*) [add_comm_monoid M] : {f : α →₀ M // ↑f.support ⊆ s } ≃ (s →₀ M) := begin refine ⟨λf, subtype_domain (λx, x ∈ s) f.1, λ f, ⟨f.map_domain subtype.val, _⟩, _, _⟩, { refine set.subset.trans (finset.coe_subset.2 map_domain_support) _, rw [finset.coe_image, set.image_subset_iff], exact assume x hx, x.2 }, { rintros ⟨f, hf⟩, apply subtype.eq, ext a, dsimp only, refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s → α), _) (assume h, _), { rcases h with ⟨x, rfl⟩, rw [map_domain_apply subtype.val_injective, subtype_domain_apply] }, { convert map_domain_notin_range _ _ h, rw [← not_mem_support_iff], refine mt _ h, exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } }, { assume f, ext ⟨a, ha⟩, dsimp only, rw [subtype_domain_apply, map_domain_apply subtype.val_injective] } end /-- Given `add_comm_monoid M` and `e : α ≃ β`, `dom_congr e` is the corresponding `equiv` between `α →₀ M` and `β →₀ M`. This is `finsupp.equiv_congr_left` as an `add_equiv`. -/ @[simps apply] protected def dom_congr [add_comm_monoid M] (e : α ≃ β) : (α →₀ M) ≃+ (β →₀ M) := { to_fun := equiv_map_domain e, inv_fun := equiv_map_domain e.symm, left_inv := λ v, begin simp only [← equiv_map_domain_trans, equiv.trans_symm], exact equiv_map_domain_refl _ end, right_inv := begin assume v, simp only [← equiv_map_domain_trans, equiv.symm_trans], exact equiv_map_domain_refl _ end, map_add' := λ a b, by simp only [equiv_map_domain_eq_map_domain]; exact map_domain_add } @[simp] lemma dom_congr_refl [add_comm_monoid M] : finsupp.dom_congr (equiv.refl α) = add_equiv.refl (α →₀ M) := add_equiv.ext $ λ _, equiv_map_domain_refl _ @[simp] lemma dom_congr_symm [add_comm_monoid M] (e : α ≃ β) : (finsupp.dom_congr e).symm = (finsupp.dom_congr e.symm : (β →₀ M) ≃+ (α →₀ M)):= add_equiv.ext $ λ _, rfl @[simp] lemma dom_congr_trans [add_comm_monoid M] (e : α ≃ β) (f : β ≃ γ) : (finsupp.dom_congr e).trans (finsupp.dom_congr f) = (finsupp.dom_congr (e.trans f) : (α →₀ M) ≃+ _) := add_equiv.ext $ λ _, (equiv_map_domain_trans _ _ _).symm end finsupp namespace finsupp /-! ### Declarations about sigma types -/ section sigma variables {αs : ι → Type*} [has_zero M] (l : (Σ i, αs i) →₀ M) /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `M` and an index element `i : ι`, `split l i` is the `i`th component of `l`, a finitely supported function from `as i` to `M`. This is the `finsupp` version of `sigma.curry`. -/ def split (i : ι) : αs i →₀ M := l.comap_domain (sigma.mk i) (λ x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2) lemma split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ := begin dunfold split, rw comap_domain_apply end /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β`, `split_support l` is the finset of indices in `ι` that appear in the support of `l`. -/ def split_support : finset ι := l.support.image sigma.fst lemma mem_split_support_iff_nonzero (i : ι) : i ∈ split_support l ↔ split l i ≠ 0 := begin rw [split_support, mem_image, ne.def, ← support_eq_empty, ← ne.def, ← finset.nonempty_iff_ne_empty, split, comap_domain, finset.nonempty], simp only [exists_prop, finset.mem_preimage, exists_and_distrib_right, exists_eq_right, mem_support_iff, sigma.exists, ne.def] end /-- Given `l`, a finitely supported function from the sigma type `Σ i, αs i` to `β` and an `ι`-indexed family `g` of functions from `(αs i →₀ β)` to `γ`, `split_comp` defines a finitely supported function from the index type `ι` to `γ` given by composing `g i` with `split l i`. -/ def split_comp [has_zero N] (g : Π i, (αs i →₀ M) → N) (hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ N := { support := split_support l, to_fun := λ i, g i (split l i), mem_support_to_fun := begin intros i, rw [mem_split_support_iff_nonzero, not_iff_not, hg], end } lemma sigma_support : l.support = l.split_support.sigma (λ i, (l.split i).support) := by simp only [finset.ext_iff, split_support, split, comap_domain, mem_image, mem_preimage, sigma.forall, mem_sigma]; tauto lemma sigma_sum [add_comm_monoid N] (f : (Σ (i : ι), αs i) → M → N) : l.sum f = ∑ i in split_support l, (split l i).sum (λ (a : αs i) b, f ⟨i, a⟩ b) := by simp only [sum, sigma_support, sum_sigma, split_apply] variables {η : Type*} [fintype η] {ιs : η → Type*} [has_zero α] /-- On a `fintype η`, `finsupp.split` is an equivalence between `(Σ (j : η), ιs j) →₀ α` and `Π j, (ιs j →₀ α)`. This is the `finsupp` version of `equiv.Pi_curry`. -/ noncomputable def sigma_finsupp_equiv_pi_finsupp : ((Σ j, ιs j) →₀ α) ≃ Π j, (ιs j →₀ α) := { to_fun := split, inv_fun := λ f, on_finset (finset.univ.sigma (λ j, (f j).support)) (λ ji, f ji.1 ji.2) (λ g hg, finset.mem_sigma.mpr ⟨finset.mem_univ _, mem_support_iff.mpr hg⟩), left_inv := λ f, by { ext, simp [split] }, right_inv := λ f, by { ext, simp [split] } } @[simp] lemma sigma_finsupp_equiv_pi_finsupp_apply (f : (Σ j, ιs j) →₀ α) (j i) : sigma_finsupp_equiv_pi_finsupp f j i = f ⟨j, i⟩ := rfl /-- On a `fintype η`, `finsupp.split` is an additive equivalence between `(Σ (j : η), ιs j) →₀ α` and `Π j, (ιs j →₀ α)`. This is the `add_equiv` version of `finsupp.sigma_finsupp_equiv_pi_finsupp`. -/ noncomputable def sigma_finsupp_add_equiv_pi_finsupp {α : Type*} {ιs : η → Type*} [add_monoid α] : ((Σ j, ιs j) →₀ α) ≃+ Π j, (ιs j →₀ α) := { map_add' := λ f g, by { ext, simp }, .. sigma_finsupp_equiv_pi_finsupp } @[simp] lemma sigma_finsupp_add_equiv_pi_finsupp_apply {α : Type*} {ιs : η → Type*} [add_monoid α] (f : (Σ j, ιs j) →₀ α) (j i) : sigma_finsupp_add_equiv_pi_finsupp f j i = f ⟨j, i⟩ := rfl end sigma end finsupp /-! ### Declarations relating `multiset` to `finsupp` -/ namespace multiset /-- Given a multiset `s`, `s.to_finsupp` returns the finitely supported function on `ℕ` given by the multiplicities of the elements of `s`. -/ def to_finsupp : multiset α ≃+ (α →₀ ℕ) := finsupp.to_multiset.symm @[simp] lemma to_finsupp_support [D : decidable_eq α] (s : multiset α) : s.to_finsupp.support = s.to_finset := by rw subsingleton.elim D; refl @[simp] lemma to_finsupp_apply [D : decidable_eq α] (s : multiset α) (a : α) : to_finsupp s a = s.count a := by rw subsingleton.elim D; refl lemma to_finsupp_zero : to_finsupp (0 : multiset α) = 0 := add_equiv.map_zero _ lemma to_finsupp_add (s t : multiset α) : to_finsupp (s + t) = to_finsupp s + to_finsupp t := to_finsupp.map_add s t @[simp] lemma to_finsupp_singleton (a : α) : to_finsupp (a ::ₘ 0) = finsupp.single a 1 := finsupp.to_multiset.symm_apply_eq.2 $ by simp @[simp] lemma to_finsupp_to_multiset (s : multiset α) : s.to_finsupp.to_multiset = s := finsupp.to_multiset.apply_symm_apply s lemma to_finsupp_eq_iff {s : multiset α} {f : α →₀ ℕ} : s.to_finsupp = f ↔ s = f.to_multiset := finsupp.to_multiset.symm_apply_eq end multiset @[simp] lemma finsupp.to_multiset_to_finsupp (f : α →₀ ℕ) : f.to_multiset.to_finsupp = f := finsupp.to_multiset.symm_apply_apply f /-! ### Declarations about order(ed) instances on `finsupp` -/ namespace finsupp instance [preorder M] [has_zero M] : preorder (α →₀ M) := { le := λ f g, ∀ s, f s ≤ g s, le_refl := λ f s, le_refl _, le_trans := λ f g h Hfg Hgh s, le_trans (Hfg s) (Hgh s) } instance [partial_order M] [has_zero M] : partial_order (α →₀ M) := { le_antisymm := λ f g hfg hgf, ext $ λ s, le_antisymm (hfg s) (hgf s), .. finsupp.preorder } instance [ordered_cancel_add_comm_monoid M] : ordered_cancel_add_comm_monoid (α →₀ M) := { add_le_add_left := λ a b h c s, add_le_add_left (h s) (c s), le_of_add_le_add_left := λ a b c h s, le_of_add_le_add_left (h s), add_left_cancel := λ a b c h, ext $ λ s, add_left_cancel (ext_iff.1 h s), .. finsupp.add_comm_monoid, .. finsupp.partial_order } lemma le_def [preorder M] [has_zero M] {f g : α →₀ M} : f ≤ g ↔ ∀ x, f x ≤ g x := iff.rfl lemma le_iff [canonically_ordered_add_monoid M] (f g : α →₀ M) : f ≤ g ↔ ∀ s ∈ f.support, f s ≤ g s := ⟨λ h s hs, h s, λ h s, if H : s ∈ f.support then h s H else (not_mem_support_iff.1 H).symm ▸ zero_le (g s)⟩ instance decidable_le [canonically_ordered_add_monoid M] [decidable_rel (@has_le.le M _)] : decidable_rel (@has_le.le (α →₀ M) _) := λ f g, decidable_of_iff _ (le_iff f g).symm @[simp] lemma add_eq_zero_iff [canonically_ordered_add_monoid M] (f g : α →₀ M) : f + g = 0 ↔ f = 0 ∧ g = 0 := by simp [ext_iff, forall_and_distrib] /-- `finsupp.to_multiset` as an order isomorphism. -/ def order_iso_multiset : (α →₀ ℕ) ≃o multiset α := { to_equiv := to_multiset.to_equiv, map_rel_iff' := λ f g, by simp [multiset.le_iff_count, le_def] } @[simp] lemma coe_order_iso_multiset : ⇑(@order_iso_multiset α) = to_multiset := rfl @[simp] lemma coe_order_iso_multiset_symm : ⇑(@order_iso_multiset α).symm = multiset.to_finsupp := rfl lemma to_multiset_strict_mono : strict_mono (@to_multiset α) := order_iso_multiset.strict_mono lemma sum_id_lt_of_lt (m n : α →₀ ℕ) (h : m < n) : m.sum (λ _, id) < n.sum (λ _, id) := begin rw [← card_to_multiset, ← card_to_multiset], apply multiset.card_lt_of_lt, exact to_multiset_strict_mono h end variable (α) /-- The order on `σ →₀ ℕ` is well-founded.-/ lemma lt_wf : well_founded (@has_lt.lt (α →₀ ℕ) _) := subrelation.wf (sum_id_lt_of_lt) $ inv_image.wf _ nat.lt_wf variable {α} @[simp] lemma nat_add_sub_cancel (f g : α →₀ ℕ) : f + g - g = f := ext $ λ a, nat.add_sub_cancel _ _ @[simp] lemma nat_add_sub_cancel_left (f g : α →₀ ℕ) : f + g - f = g := ext $ λ a, nat.add_sub_cancel_left _ _ lemma nat_add_sub_of_le {f g : α →₀ ℕ} (h : f ≤ g) : f + (g - f) = g := ext $ λ a, nat.add_sub_of_le (h a) lemma nat_sub_add_cancel {f g : α →₀ ℕ} (h : f ≤ g) : g - f + f = g := ext $ λ a, nat.sub_add_cancel (h a) instance : canonically_ordered_add_monoid (α →₀ ℕ) := { bot := 0, bot_le := λ f s, zero_le (f s), le_iff_exists_add := λ f g, ⟨λ H, ⟨g - f, (nat_add_sub_of_le H).symm⟩, λ ⟨c, hc⟩, hc.symm ▸ λ x, by simp⟩, .. (infer_instance : ordered_add_comm_monoid (α →₀ ℕ)) } end finsupp namespace multiset lemma to_finsuppstrict_mono : strict_mono (@to_finsupp α) := finsupp.order_iso_multiset.symm.strict_mono end multiset
382bf03388705b0db705a20ea59fb6c3ac4101eb
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/order/complete_lattice.lean
9883b130d4e45d9882cd2cfb59ba11b444e6fdd2
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
40,070
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 order.bounds /-! # Theory of complete lattices ## Main definitions * `Sup` and `Inf` are the supremum and the infimum of a set; * `supr (f : ι → α)` and `infi (f : ι → α)` are indexed supremum and infimum of a function, defined as `Sup` and `Inf` of the range of this function; * `class complete_lattice`: a bounded lattice such that `Sup s` is always the least upper boundary of `s` and `Inf s` is always the greatest lower boundary of `s`; * `class complete_linear_order`: a linear ordered complete lattice. ## Naming conventions We use `Sup`/`Inf`/`supr`/`infi` for the corresponding functions in the statement. Sometimes we also use `bsupr`/`binfi` for "bounded` supremum or infimum, i.e. one of `⨆ i ∈ s, f i`, `⨆ i (hi : p i), f i`, or more generally `⨆ i (hi : p i), f i hi`. ## Notation * `⨆ i, f i` : `supr f`, the supremum of the range of `f`; * `⨅ i, f i` : `infi f`, the infimum of the range of `f`. -/ set_option old_structure_cmd true open set variables {α β β₂ : Type*} {ι ι₂ : Sort*} /-- class for the `Sup` operator -/ class has_Sup (α : Type*) := (Sup : set α → α) /-- class for the `Inf` operator -/ class has_Inf (α : Type*) := (Inf : set α → α) export has_Sup (Sup) has_Inf (Inf) /-- Supremum of a set -/ add_decl_doc has_Sup.Sup /-- Infimum of a set -/ add_decl_doc has_Inf.Inf /-- Indexed supremum -/ def supr [has_Sup α] {ι} (s : ι → α) : α := Sup (range s) /-- Indexed infimum -/ def infi [has_Inf α] {ι} (s : ι → α) : α := Inf (range s) @[priority 50] instance has_Inf_to_nonempty (α) [has_Inf α] : nonempty α := ⟨Inf ∅⟩ @[priority 50] instance has_Sup_to_nonempty (α) [has_Sup α] : nonempty α := ⟨Sup ∅⟩ notation `⨆` binders `, ` r:(scoped f, supr f) := r notation `⨅` binders `, ` r:(scoped f, infi f) := r instance (α) [has_Inf α] : has_Sup (order_dual α) := ⟨(Inf : set α → α)⟩ instance (α) [has_Sup α] : has_Inf (order_dual α) := ⟨(Sup : set α → α)⟩ /-- A complete lattice is a bounded lattice which has suprema and infima for every subset. -/ class complete_lattice (α : Type*) extends bounded_lattice α, has_Sup α, has_Inf α := (le_Sup : ∀s, ∀a∈s, a ≤ Sup s) (Sup_le : ∀s a, (∀b∈s, b ≤ a) → Sup s ≤ a) (Inf_le : ∀s, ∀a∈s, Inf s ≤ a) (le_Inf : ∀s a, (∀b∈s, a ≤ b) → a ≤ Inf s) /-- Create a `complete_lattice` from a `partial_order` and `Inf` function that returns the greatest lower bound of a set. Usually this constructor provides poor definitional equalities. If other fields are known explicitly, they should be provided; for example, if `inf` is known explicitly, construct the `complete_lattice` instance as ``` instance : complete_lattice my_T := { inf := better_inf, le_inf := ..., inf_le_right := ..., inf_le_left := ... -- don't care to fix sup, Sup, bot, top ..complete_lattice_of_Inf my_T _ } ``` -/ def complete_lattice_of_Inf (α : Type*) [H1 : partial_order α] [H2 : has_Inf α] (is_glb_Inf : ∀ s : set α, is_glb s (Inf s)) : complete_lattice α := { bot := Inf univ, bot_le := λ x, (is_glb_Inf univ).1 trivial, top := Inf ∅, le_top := λ a, (is_glb_Inf ∅).2 $ by simp, sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, inf := λ a b, Inf {a, b}, le_inf := λ a b c hab hac, by { apply (is_glb_Inf _).2, simp [*] }, inf_le_right := λ a b, (is_glb_Inf _).1 $ mem_insert_of_mem _ $ mem_singleton _, inf_le_left := λ a b, (is_glb_Inf _).1 $ mem_insert _ _, sup_le := λ a b c hac hbc, (is_glb_Inf _).1 $ by simp [*], le_sup_left := λ a b, (is_glb_Inf _).2 $ λ x, and.left, le_sup_right := λ a b, (is_glb_Inf _).2 $ λ x, and.right, le_Inf := λ s a ha, (is_glb_Inf s).2 ha, Inf_le := λ s a ha, (is_glb_Inf s).1 ha, Sup := λ s, Inf (upper_bounds s), le_Sup := λ s a ha, (is_glb_Inf (upper_bounds s)).2 $ λ b hb, hb ha, Sup_le := λ s a ha, (is_glb_Inf (upper_bounds s)).1 ha, .. H1, .. H2 } /-- Create a `complete_lattice` from a `partial_order` and `Sup` function that returns the least upper bound of a set. Usually this constructor provides poor definitional equalities. If other fields are known explicitly, they should be provided; for example, if `inf` is known explicitly, construct the `complete_lattice` instance as ``` instance : complete_lattice my_T := { inf := better_inf, le_inf := ..., inf_le_right := ..., inf_le_left := ... -- don't care to fix sup, Inf, bot, top ..complete_lattice_of_Sup my_T _ } ``` -/ def complete_lattice_of_Sup (α : Type*) [H1 : partial_order α] [H2 : has_Sup α] (is_lub_Sup : ∀ s : set α, is_lub s (Sup s)) : complete_lattice α := { top := Sup univ, le_top := λ x, (is_lub_Sup univ).1 trivial, bot := Sup ∅, bot_le := λ x, (is_lub_Sup ∅).2 $ by simp, sup := λ a b, Sup {a, b}, sup_le := λ a b c hac hbc, (is_lub_Sup _).2 (by simp [*]), le_sup_left := λ a b, (is_lub_Sup _).1 $ mem_insert _ _, le_sup_right := λ a b, (is_lub_Sup _).1 $ mem_insert_of_mem _ $ mem_singleton _, inf := λ a b, Sup {x | x ≤ a ∧ x ≤ b}, le_inf := λ a b c hab hac, (is_lub_Sup _).1 $ by simp [*], inf_le_left := λ a b, (is_lub_Sup _).2 (λ x, and.left), inf_le_right := λ a b, (is_lub_Sup _).2 (λ x, and.right), Inf := λ s, Sup (lower_bounds s), Sup_le := λ s a ha, (is_lub_Sup s).2 ha, le_Sup := λ s a ha, (is_lub_Sup s).1 ha, Inf_le := λ s a ha, (is_lub_Sup (lower_bounds s)).2 (λ b hb, hb ha), le_Inf := λ s a ha, (is_lub_Sup (lower_bounds s)).1 ha, .. H1, .. H2 } /-- A complete linear order is a linear order whose lattice structure is complete. -/ class complete_linear_order (α : Type*) extends complete_lattice α, linear_order α namespace order_dual variable (α) instance [complete_lattice α] : complete_lattice (order_dual α) := { le_Sup := @complete_lattice.Inf_le α _, Sup_le := @complete_lattice.le_Inf α _, Inf_le := @complete_lattice.le_Sup α _, le_Inf := @complete_lattice.Sup_le α _, .. order_dual.bounded_lattice α, ..order_dual.has_Sup α, ..order_dual.has_Inf α } instance [complete_linear_order α] : complete_linear_order (order_dual α) := { .. order_dual.complete_lattice α, .. order_dual.linear_order α } end order_dual section variables [complete_lattice α] {s t : set α} {a b : α} @[ematch] theorem le_Sup : a ∈ s → a ≤ Sup s := complete_lattice.le_Sup s a theorem Sup_le : (∀b∈s, b ≤ a) → Sup s ≤ a := complete_lattice.Sup_le s a @[ematch] theorem Inf_le : a ∈ s → Inf s ≤ a := complete_lattice.Inf_le s a theorem le_Inf : (∀b∈s, a ≤ b) → a ≤ Inf s := complete_lattice.le_Inf s a lemma is_lub_Sup (s : set α) : is_lub s (Sup s) := ⟨assume x, le_Sup, assume x, Sup_le⟩ lemma is_lub.Sup_eq (h : is_lub s a) : Sup s = a := (is_lub_Sup s).unique h lemma is_glb_Inf (s : set α) : is_glb s (Inf s) := ⟨assume a, Inf_le, assume a, le_Inf⟩ lemma is_glb.Inf_eq (h : is_glb s a) : Inf s = a := (is_glb_Inf s).unique h theorem le_Sup_of_le (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s := le_trans h (le_Sup hb) theorem Inf_le_of_le (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a := le_trans (Inf_le hb) h theorem Sup_le_Sup (h : s ⊆ t) : Sup s ≤ Sup t := (is_lub_Sup s).mono (is_lub_Sup t) h theorem Inf_le_Inf (h : s ⊆ t) : Inf t ≤ Inf s := (is_glb_Inf s).mono (is_glb_Inf t) h @[simp] theorem Sup_le_iff : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) := is_lub_le_iff (is_lub_Sup s) @[simp] theorem le_Inf_iff : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) := le_is_glb_iff (is_glb_Inf s) theorem Sup_le_Sup_of_forall_exists_le (h : ∀ x ∈ s, ∃ y ∈ t, x ≤ y) : Sup s ≤ Sup t := le_of_forall_le' (by simp only [Sup_le_iff]; introv h₀ h₁; rcases h _ h₁ with ⟨y,hy,hy'⟩; solve_by_elim [le_trans hy']) theorem Inf_le_Inf_of_forall_exists_le (h : ∀ x ∈ s, ∃ y ∈ t, y ≤ x) : Inf t ≤ Inf s := le_of_forall_le (by simp only [le_Inf_iff]; introv h₀ h₁; rcases h _ h₁ with ⟨y,hy,hy'⟩; solve_by_elim [le_trans _ hy']) theorem Inf_le_Sup (hs : s.nonempty) : Inf s ≤ Sup s := is_glb_le_is_lub (is_glb_Inf s) (is_lub_Sup s) hs -- TODO: it is weird that we have to add union_def theorem Sup_union {s t : set α} : Sup (s ∪ t) = Sup s ⊔ Sup t := ((is_lub_Sup s).union (is_lub_Sup t)).Sup_eq theorem Sup_inter_le {s t : set α} : Sup (s ∩ t) ≤ Sup s ⊓ Sup t := by finish /- Sup_le (assume a ⟨a_s, a_t⟩, le_inf (le_Sup a_s) (le_Sup a_t)) -/ theorem Inf_union {s t : set α} : Inf (s ∪ t) = Inf s ⊓ Inf t := ((is_glb_Inf s).union (is_glb_Inf t)).Inf_eq theorem le_Inf_inter {s t : set α} : Inf s ⊔ Inf t ≤ Inf (s ∩ t) := @Sup_inter_le (order_dual α) _ _ _ @[simp] theorem Sup_empty : Sup ∅ = (⊥ : α) := is_lub_empty.Sup_eq @[simp] theorem Inf_empty : Inf ∅ = (⊤ : α) := (@is_glb_empty α _).Inf_eq @[simp] theorem Sup_univ : Sup univ = (⊤ : α) := (@is_lub_univ α _).Sup_eq @[simp] theorem Inf_univ : Inf univ = (⊥ : α) := is_glb_univ.Inf_eq -- TODO(Jeremy): get this automatically @[simp] theorem Sup_insert {a : α} {s : set α} : Sup (insert a s) = a ⊔ Sup s := ((is_lub_Sup s).insert a).Sup_eq @[simp] theorem Inf_insert {a : α} {s : set α} : Inf (insert a s) = a ⊓ Inf s := ((is_glb_Inf s).insert a).Inf_eq theorem Sup_le_Sup_of_subset_instert_bot (h : s ⊆ insert ⊥ t) : Sup s ≤ Sup t := le_trans (Sup_le_Sup h) (le_of_eq (trans Sup_insert bot_sup_eq)) theorem Inf_le_Inf_of_subset_insert_top (h : s ⊆ insert ⊤ t) : Inf t ≤ Inf s := le_trans (le_of_eq (trans top_inf_eq.symm Inf_insert.symm)) (Inf_le_Inf h) -- We will generalize this to conditionally complete lattices in `cSup_singleton`. theorem Sup_singleton {a : α} : Sup {a} = a := is_lub_singleton.Sup_eq -- We will generalize this to conditionally complete lattices in `cInf_singleton`. theorem Inf_singleton {a : α} : Inf {a} = a := is_glb_singleton.Inf_eq theorem Sup_pair {a b : α} : Sup {a, b} = a ⊔ b := (@is_lub_pair α _ a b).Sup_eq theorem Inf_pair {a b : α} : Inf {a, b} = a ⊓ b := (@is_glb_pair α _ a b).Inf_eq @[simp] theorem Inf_eq_top : Inf s = ⊤ ↔ (∀a∈s, a = ⊤) := iff.intro (assume h a ha, top_unique $ h ▸ Inf_le ha) (assume h, top_unique $ le_Inf $ assume a ha, top_le_iff.2 $ h a ha) lemma eq_singleton_top_of_Inf_eq_top_of_nonempty {s : set α} (h_inf : Inf s = ⊤) (hne : s.nonempty) : s = {⊤} := by { rw set.eq_singleton_iff_nonempty_unique_mem, rw Inf_eq_top at h_inf, exact ⟨hne, h_inf⟩, } @[simp] theorem Sup_eq_bot : Sup s = ⊥ ↔ (∀a∈s, a = ⊥) := @Inf_eq_top (order_dual α) _ _ lemma eq_singleton_bot_of_Sup_eq_bot_of_nonempty {s : set α} (h_sup : Sup s = ⊥) (hne : s.nonempty) : s = {⊥} := by { rw set.eq_singleton_iff_nonempty_unique_mem, rw Sup_eq_bot at h_sup, exact ⟨hne, h_sup⟩, } end section complete_linear_order variables [complete_linear_order α] {s t : set α} {a b : α} lemma Inf_lt_iff : Inf s < b ↔ (∃a∈s, a < b) := is_glb_lt_iff (is_glb_Inf s) lemma lt_Sup_iff : b < Sup s ↔ (∃a∈s, b < a) := lt_is_lub_iff (is_lub_Sup s) lemma Sup_eq_top : Sup s = ⊤ ↔ (∀b<⊤, ∃a∈s, b < a) := iff.intro (assume (h : Sup s = ⊤) b hb, by rwa [←h, lt_Sup_iff] at hb) (assume h, top_unique $ le_of_not_gt $ assume h', let ⟨a, ha, h⟩ := h _ h' in lt_irrefl a $ lt_of_le_of_lt (le_Sup ha) h) lemma Inf_eq_bot : Inf s = ⊥ ↔ (∀b>⊥, ∃a∈s, a < b) := @Sup_eq_top (order_dual α) _ _ lemma lt_supr_iff {f : ι → α} : a < supr f ↔ (∃i, a < f i) := lt_Sup_iff.trans exists_range_iff lemma infi_lt_iff {f : ι → α} : infi f < a ↔ (∃i, f i < a) := Inf_lt_iff.trans exists_range_iff end complete_linear_order /- ### supr & infi -/ section variables [complete_lattice α] {s t : ι → α} {a b : α} -- TODO: this declaration gives error when starting smt state --@[ematch] theorem le_supr (s : ι → α) (i : ι) : s i ≤ supr s := le_Sup ⟨i, rfl⟩ @[ematch] theorem le_supr' (s : ι → α) (i : ι) : (: s i ≤ supr s :) := le_Sup ⟨i, rfl⟩ /- TODO: this version would be more powerful, but, alas, the pattern matcher doesn't accept it. @[ematch] theorem le_supr' (s : ι → α) (i : ι) : (: s i :) ≤ (: supr s :) := le_Sup ⟨i, rfl⟩ -/ lemma is_lub_supr : is_lub (range s) (⨆j, s j) := is_lub_Sup _ lemma is_lub.supr_eq (h : is_lub (range s) a) : (⨆j, s j) = a := h.Sup_eq lemma is_glb_infi : is_glb (range s) (⨅j, s j) := is_glb_Inf _ lemma is_glb.infi_eq (h : is_glb (range s) a) : (⨅j, s j) = a := h.Inf_eq theorem le_supr_of_le (i : ι) (h : a ≤ s i) : a ≤ supr s := le_trans h (le_supr _ i) theorem le_bsupr {p : ι → Prop} {f : Π i (h : p i), α} (i : ι) (hi : p i) : f i hi ≤ ⨆ i hi, f i hi := le_supr_of_le i $ le_supr (f i) hi theorem le_bsupr_of_le {p : ι → Prop} {f : Π i (h : p i), α} (i : ι) (hi : p i) (h : a ≤ f i hi) : a ≤ ⨆ i hi, f i hi := le_trans h (le_bsupr i hi) theorem supr_le (h : ∀i, s i ≤ a) : supr s ≤ a := Sup_le $ assume b ⟨i, eq⟩, eq ▸ h i theorem bsupr_le {p : ι → Prop} {f : Π i (h : p i), α} (h : ∀ i hi, f i hi ≤ a) : (⨆ i (hi : p i), f i hi) ≤ a := supr_le $ λ i, supr_le $ h i theorem bsupr_le_supr (p : ι → Prop) (f : ι → α) : (⨆ i (H : p i), f i) ≤ ⨆ i, f i := bsupr_le (λ i hi, le_supr f i) theorem supr_le_supr (h : ∀i, s i ≤ t i) : supr s ≤ supr t := supr_le $ assume i, le_supr_of_le i (h i) theorem supr_le_supr2 {t : ι₂ → α} (h : ∀i, ∃j, s i ≤ t j) : supr s ≤ supr t := supr_le $ assume j, exists.elim (h j) le_supr_of_le theorem bsupr_le_bsupr {p : ι → Prop} {f g : Π i (hi : p i), α} (h : ∀ i hi, f i hi ≤ g i hi) : (⨆ i hi, f i hi) ≤ ⨆ i hi, g i hi := bsupr_le $ λ i hi, le_trans (h i hi) (le_bsupr i hi) theorem supr_le_supr_const (h : ι → ι₂) : (⨆ i:ι, a) ≤ (⨆ j:ι₂, a) := supr_le $ le_supr _ ∘ h @[simp] theorem supr_le_iff : supr s ≤ a ↔ (∀i, s i ≤ a) := (is_lub_le_iff is_lub_supr).trans forall_range_iff theorem supr_lt_iff : supr s < a ↔ ∃ b < a, ∀ i, s i ≤ b := ⟨λ h, ⟨supr s, h, λ i, le_supr s i⟩, λ ⟨b, hba, hsb⟩, (supr_le hsb).trans_lt hba⟩ theorem Sup_eq_supr {s : set α} : Sup s = (⨆a ∈ s, a) := le_antisymm (Sup_le $ assume b h, le_supr_of_le b $ le_supr _ h) (supr_le $ assume b, supr_le $ assume h, le_Sup h) lemma le_supr_iff : (a ≤ supr s) ↔ (∀ b, (∀ i, s i ≤ b) → a ≤ b) := ⟨λ h b hb, le_trans h (supr_le hb), λ h, h _ $ λ i, le_supr s i⟩ lemma monotone.le_map_supr [complete_lattice β] {f : α → β} (hf : monotone f) : (⨆ i, f (s i)) ≤ f (supr s) := supr_le $ λ i, hf $ le_supr _ _ lemma monotone.le_map_supr2 [complete_lattice β] {f : α → β} (hf : monotone f) {ι' : ι → Sort*} (s : Π i, ι' i → α) : (⨆ i (h : ι' i), f (s i h)) ≤ f (⨆ i (h : ι' i), s i h) := calc (⨆ i h, f (s i h)) ≤ (⨆ i, f (⨆ h, s i h)) : supr_le_supr $ λ i, hf.le_map_supr ... ≤ f (⨆ i (h : ι' i), s i h) : hf.le_map_supr lemma monotone.le_map_Sup [complete_lattice β] {s : set α} {f : α → β} (hf : monotone f) : (⨆a∈s, f a) ≤ f (Sup s) := by rw [Sup_eq_supr]; exact hf.le_map_supr2 _ lemma supr_comp_le {ι' : Sort*} (f : ι' → α) (g : ι → ι') : (⨆ x, f (g x)) ≤ ⨆ y, f y := supr_le_supr2 $ λ x, ⟨_, le_refl _⟩ lemma monotone.supr_comp_eq [preorder β] {f : β → α} (hf : monotone f) {s : ι → β} (hs : ∀ x, ∃ i, x ≤ s i) : (⨆ x, f (s x)) = ⨆ y, f y := le_antisymm (supr_comp_le _ _) (supr_le_supr2 $ λ x, (hs x).imp $ λ i hi, hf hi) lemma function.surjective.supr_comp {α : Type*} [has_Sup α] {f : ι → ι₂} (hf : function.surjective f) (g : ι₂ → α) : (⨆ x, g (f x)) = ⨆ y, g y := by simp only [supr, hf.range_comp] lemma supr_congr {α : Type*} [has_Sup α] {f : ι → α} {g : ι₂ → α} (h : ι → ι₂) (h1 : function.surjective h) (h2 : ∀ x, g (h x) = f x) : (⨆ x, f x) = ⨆ y, g y := by { convert h1.supr_comp g, exact (funext h2).symm } -- TODO: finish doesn't do well here. @[congr] theorem supr_congr_Prop {α : Type*} [has_Sup α] {p q : Prop} {f₁ : p → α} {f₂ : q → α} (pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : supr f₁ = supr f₂ := begin have : f₁ ∘ pq.mpr = f₂ := funext f, rw [← this], refine (function.surjective.supr_comp (λ h, ⟨pq.1 h, _⟩) f₁).symm, refl end theorem infi_le (s : ι → α) (i : ι) : infi s ≤ s i := Inf_le ⟨i, rfl⟩ @[ematch] theorem infi_le' (s : ι → α) (i : ι) : (: infi s ≤ s i :) := Inf_le ⟨i, rfl⟩ theorem infi_le_of_le (i : ι) (h : s i ≤ a) : infi s ≤ a := le_trans (infi_le _ i) h theorem binfi_le {p : ι → Prop} {f : Π i (hi : p i), α} (i : ι) (hi : p i) : (⨅ i hi, f i hi) ≤ f i hi := infi_le_of_le i $ infi_le (f i) hi theorem binfi_le_of_le {p : ι → Prop} {f : Π i (hi : p i), α} (i : ι) (hi : p i) (h : f i hi ≤ a) : (⨅ i hi, f i hi) ≤ a := le_trans (binfi_le i hi) h theorem le_infi (h : ∀i, a ≤ s i) : a ≤ infi s := le_Inf $ assume b ⟨i, eq⟩, eq ▸ h i theorem le_binfi {p : ι → Prop} {f : Π i (h : p i), α} (h : ∀ i hi, a ≤ f i hi) : a ≤ ⨅ i hi, f i hi := le_infi $ λ i, le_infi $ h i theorem infi_le_binfi (p : ι → Prop) (f : ι → α) : (⨅ i, f i) ≤ ⨅ i (H : p i), f i := le_binfi (λ i hi, infi_le f i) theorem infi_le_infi (h : ∀i, s i ≤ t i) : infi s ≤ infi t := le_infi $ assume i, infi_le_of_le i (h i) theorem infi_le_infi2 {t : ι₂ → α} (h : ∀j, ∃i, s i ≤ t j) : infi s ≤ infi t := le_infi $ assume j, exists.elim (h j) infi_le_of_le theorem binfi_le_binfi {p : ι → Prop} {f g : Π i (h : p i), α} (h : ∀ i hi, f i hi ≤ g i hi) : (⨅ i hi, f i hi) ≤ ⨅ i hi, g i hi := le_binfi $ λ i hi, le_trans (binfi_le i hi) (h i hi) theorem infi_le_infi_const (h : ι₂ → ι) : (⨅ i:ι, a) ≤ (⨅ j:ι₂, a) := le_infi $ infi_le _ ∘ h @[simp] theorem le_infi_iff : a ≤ infi s ↔ (∀i, a ≤ s i) := ⟨assume : a ≤ infi s, assume i, le_trans this (infi_le _ _), le_infi⟩ theorem Inf_eq_infi {s : set α} : Inf s = (⨅a ∈ s, a) := @Sup_eq_supr (order_dual α) _ _ lemma monotone.map_infi_le [complete_lattice β] {f : α → β} (hf : monotone f) : f (infi s) ≤ (⨅ i, f (s i)) := le_infi $ λ i, hf $ infi_le _ _ lemma monotone.map_infi2_le [complete_lattice β] {f : α → β} (hf : monotone f) {ι' : ι → Sort*} (s : Π i, ι' i → α) : f (⨅ i (h : ι' i), s i h) ≤ (⨅ i (h : ι' i), f (s i h)) := @monotone.le_map_supr2 (order_dual α) (order_dual β) _ _ _ f hf.order_dual _ _ lemma monotone.map_Inf_le [complete_lattice β] {s : set α} {f : α → β} (hf : monotone f) : f (Inf s) ≤ ⨅ a∈s, f a := by rw [Inf_eq_infi]; exact hf.map_infi2_le _ lemma le_infi_comp {ι' : Sort*} (f : ι' → α) (g : ι → ι') : (⨅ y, f y) ≤ ⨅ x, f (g x) := infi_le_infi2 $ λ x, ⟨_, le_refl _⟩ lemma monotone.infi_comp_eq [preorder β] {f : β → α} (hf : monotone f) {s : ι → β} (hs : ∀ x, ∃ i, s i ≤ x) : (⨅ x, f (s x)) = ⨅ y, f y := le_antisymm (infi_le_infi2 $ λ x, (hs x).imp $ λ i hi, hf hi) (le_infi_comp _ _) lemma function.surjective.infi_comp {α : Type*} [has_Inf α] {f : ι → ι₂} (hf : function.surjective f) (g : ι₂ → α) : (⨅ x, g (f x)) = ⨅ y, g y := @function.surjective.supr_comp _ _ (order_dual α) _ f hf g lemma infi_congr {α : Type*} [has_Inf α] {f : ι → α} {g : ι₂ → α} (h : ι → ι₂) (h1 : function.surjective h) (h2 : ∀ x, g (h x) = f x) : (⨅ x, f x) = ⨅ y, g y := @supr_congr _ _ (order_dual α) _ _ _ h h1 h2 @[congr] theorem infi_congr_Prop {α : Type*} [has_Inf α] {p q : Prop} {f₁ : p → α} {f₂ : q → α} (pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : infi f₁ = infi f₂ := @supr_congr_Prop (order_dual α) _ p q f₁ f₂ pq f lemma supr_const_le {x : α} : (⨆ (h : ι), x) ≤ x := supr_le (λ _, le_rfl) lemma le_infi_const {x : α} : x ≤ (⨅ (h : ι), x) := le_infi (λ _, le_rfl) -- We will generalize this to conditionally complete lattices in `cinfi_const`. theorem infi_const [nonempty ι] {a : α} : (⨅ b:ι, a) = a := by rw [infi, range_const, Inf_singleton] -- We will generalize this to conditionally complete lattices in `csupr_const`. theorem supr_const [nonempty ι] {a : α} : (⨆ b:ι, a) = a := @infi_const (order_dual α) _ _ _ _ @[simp] lemma infi_top : (⨅i:ι, ⊤ : α) = ⊤ := top_unique $ le_infi $ assume i, le_refl _ @[simp] lemma supr_bot : (⨆i:ι, ⊥ : α) = ⊥ := @infi_top (order_dual α) _ _ @[simp] lemma infi_eq_top : infi s = ⊤ ↔ (∀i, s i = ⊤) := Inf_eq_top.trans forall_range_iff @[simp] lemma supr_eq_bot : supr s = ⊥ ↔ (∀i, s i = ⊥) := Sup_eq_bot.trans forall_range_iff @[simp] lemma infi_pos {p : Prop} {f : p → α} (hp : p) : (⨅ h : p, f h) = f hp := le_antisymm (infi_le _ _) (le_infi $ assume h, le_refl _) @[simp] lemma infi_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨅ h : p, f h) = ⊤ := le_antisymm le_top $ le_infi $ assume h, (hp h).elim @[simp] lemma supr_pos {p : Prop} {f : p → α} (hp : p) : (⨆ h : p, f h) = f hp := le_antisymm (supr_le $ assume h, le_refl _) (le_supr _ _) @[simp] lemma supr_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨆ h : p, f h) = ⊥ := le_antisymm (supr_le $ assume h, (hp h).elim) bot_le lemma supr_eq_dif {p : Prop} [decidable p] (a : p → α) : (⨆h:p, a h) = (if h : p then a h else ⊥) := by by_cases p; simp [h] lemma supr_eq_if {p : Prop} [decidable p] (a : α) : (⨆h:p, a) = (if p then a else ⊥) := supr_eq_dif (λ _, a) lemma infi_eq_dif {p : Prop} [decidable p] (a : p → α) : (⨅h:p, a h) = (if h : p then a h else ⊤) := @supr_eq_dif (order_dual α) _ _ _ _ lemma infi_eq_if {p : Prop} [decidable p] (a : α) : (⨅h:p, a) = (if p then a else ⊤) := infi_eq_dif (λ _, a) -- TODO: should this be @[simp]? theorem infi_comm {f : ι → ι₂ → α} : (⨅i, ⨅j, f i j) = (⨅j, ⨅i, f i j) := le_antisymm (le_infi $ assume i, le_infi $ assume j, infi_le_of_le j $ infi_le _ i) (le_infi $ assume j, le_infi $ assume i, infi_le_of_le i $ infi_le _ j) /- TODO: this is strange. In the proof below, we get exactly the desired among the equalities, but close does not get it. begin apply @le_antisymm, simp, intros, begin [smt] ematch, ematch, ematch, trace_state, have := le_refl (f i_1 i), trace_state, close end end -/ -- TODO: should this be @[simp]? theorem supr_comm {f : ι → ι₂ → α} : (⨆i, ⨆j, f i j) = (⨆j, ⨆i, f i j) := @infi_comm (order_dual α) _ _ _ _ @[simp] theorem infi_infi_eq_left {b : β} {f : Πx:β, x = b → α} : (⨅x, ⨅h:x = b, f x h) = f b rfl := le_antisymm (infi_le_of_le b $ infi_le _ rfl) (le_infi $ assume b', le_infi $ assume eq, match b', eq with ._, rfl := le_refl _ end) @[simp] theorem infi_infi_eq_right {b : β} {f : Πx:β, b = x → α} : (⨅x, ⨅h:b = x, f x h) = f b rfl := le_antisymm (infi_le_of_le b $ infi_le _ rfl) (le_infi $ assume b', le_infi $ assume eq, match b', eq with ._, rfl := le_refl _ end) @[simp] theorem supr_supr_eq_left {b : β} {f : Πx:β, x = b → α} : (⨆x, ⨆h : x = b, f x h) = f b rfl := @infi_infi_eq_left (order_dual α) _ _ _ _ @[simp] theorem supr_supr_eq_right {b : β} {f : Πx:β, b = x → α} : (⨆x, ⨆h : b = x, f x h) = f b rfl := @infi_infi_eq_right (order_dual α) _ _ _ _ attribute [ematch] le_refl theorem infi_subtype {p : ι → Prop} {f : subtype p → α} : (⨅ x, f x) = (⨅ i (h:p i), f ⟨i, h⟩) := le_antisymm (le_infi $ assume i, le_infi $ assume : p i, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) lemma infi_subtype' {p : ι → Prop} {f : ∀ i, p i → α} : (⨅ i (h : p i), f i h) = (⨅ x : subtype p, f x x.property) := (@infi_subtype _ _ _ p (λ x, f x.val x.property)).symm lemma infi_subtype'' {ι} (s : set ι) (f : ι → α) : (⨅ i : s, f i) = ⨅ (t : ι) (H : t ∈ s), f t := infi_subtype theorem infi_inf_eq {f g : ι → α} : (⨅ x, f x ⊓ g x) = (⨅ x, f x) ⊓ (⨅ x, g x) := le_antisymm (le_inf (le_infi $ assume i, infi_le_of_le i inf_le_left) (le_infi $ assume i, infi_le_of_le i inf_le_right)) (le_infi $ assume i, le_inf (inf_le_left_of_le $ infi_le _ _) (inf_le_right_of_le $ infi_le _ _)) /- TODO: here is another example where more flexible pattern matching might help. begin apply @le_antisymm, safe, pose h := f a ⊓ g a, begin [smt] ematch, ematch end end -/ lemma infi_inf [h : nonempty ι] {f : ι → α} {a : α} : (⨅x, f x) ⊓ a = (⨅ x, f x ⊓ a) := by rw [infi_inf_eq, infi_const] lemma inf_infi [nonempty ι] {f : ι → α} {a : α} : a ⊓ (⨅x, f x) = (⨅ x, a ⊓ f x) := by rw [inf_comm, infi_inf]; simp [inf_comm] lemma binfi_inf {p : ι → Prop} {f : Π i (hi : p i), α} {a : α} (h : ∃ i, p i) : (⨅i (h : p i), f i h) ⊓ a = (⨅ i (h : p i), f i h ⊓ a) := by haveI : nonempty {i // p i} := (let ⟨i, hi⟩ := h in ⟨⟨i, hi⟩⟩); rw [infi_subtype', infi_subtype', infi_inf] lemma inf_binfi {p : ι → Prop} {f : Π i (hi : p i), α} {a : α} (h : ∃ i, p i) : a ⊓ (⨅i (h : p i), f i h) = (⨅ i (h : p i), a ⊓ f i h) := by simpa only [inf_comm] using binfi_inf h theorem supr_sup_eq {f g : β → α} : (⨆ x, f x ⊔ g x) = (⨆ x, f x) ⊔ (⨆ x, g x) := @infi_inf_eq (order_dual α) β _ _ _ lemma supr_sup [h : nonempty ι] {f : ι → α} {a : α} : (⨆ x, f x) ⊔ a = (⨆ x, f x ⊔ a) := @infi_inf (order_dual α) _ _ _ _ _ lemma sup_supr [nonempty ι] {f : ι → α} {a : α} : a ⊔ (⨆ x, f x) = (⨆ x, a ⊔ f x) := @inf_infi (order_dual α) _ _ _ _ _ /- supr and infi under Prop -/ @[simp] theorem infi_false {s : false → α} : infi s = ⊤ := le_antisymm le_top (le_infi $ assume i, false.elim i) @[simp] theorem supr_false {s : false → α} : supr s = ⊥ := le_antisymm (supr_le $ assume i, false.elim i) bot_le @[simp] theorem infi_true {s : true → α} : infi s = s trivial := le_antisymm (infi_le _ _) (le_infi $ assume ⟨⟩, le_refl _) @[simp] theorem supr_true {s : true → α} : supr s = s trivial := le_antisymm (supr_le $ assume ⟨⟩, le_refl _) (le_supr _ _) @[simp] theorem infi_exists {p : ι → Prop} {f : Exists p → α} : (⨅ x, f x) = (⨅ i, ⨅ h:p i, f ⟨i, h⟩) := le_antisymm (le_infi $ assume i, le_infi $ assume : p i, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) @[simp] theorem supr_exists {p : ι → Prop} {f : Exists p → α} : (⨆ x, f x) = (⨆ i, ⨆ h:p i, f ⟨i, h⟩) := @infi_exists (order_dual α) _ _ _ _ theorem infi_and {p q : Prop} {s : p ∧ q → α} : infi s = (⨅ h₁ h₂, s ⟨h₁, h₂⟩) := le_antisymm (le_infi $ assume i, le_infi $ assume j, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) /-- The symmetric case of `infi_and`, useful for rewriting into a infimum over a conjunction -/ lemma infi_and' {p q : Prop} {s : p → q → α} : (⨅ (h₁ : p) (h₂ : q), s h₁ h₂) = ⨅ (h : p ∧ q), s h.1 h.2 := by { symmetry, exact infi_and } theorem supr_and {p q : Prop} {s : p ∧ q → α} : supr s = (⨆ h₁ h₂, s ⟨h₁, h₂⟩) := @infi_and (order_dual α) _ _ _ _ /-- The symmetric case of `supr_and`, useful for rewriting into a supremum over a conjunction -/ lemma supr_and' {p q : Prop} {s : p → q → α} : (⨆ (h₁ : p) (h₂ : q), s h₁ h₂) = ⨆ (h : p ∧ q), s h.1 h.2 := by { symmetry, exact supr_and } theorem infi_or {p q : Prop} {s : p ∨ q → α} : infi s = (⨅ h : p, s (or.inl h)) ⊓ (⨅ h : q, s (or.inr h)) := le_antisymm (le_inf (infi_le_infi2 $ assume j, ⟨_, le_refl _⟩) (infi_le_infi2 $ assume j, ⟨_, le_refl _⟩)) (le_infi $ assume i, match i with | or.inl i := inf_le_left_of_le $ infi_le _ _ | or.inr j := inf_le_right_of_le $ infi_le _ _ end) theorem supr_or {p q : Prop} {s : p ∨ q → α} : (⨆ x, s x) = (⨆ i, s (or.inl i)) ⊔ (⨆ j, s (or.inr j)) := @infi_or (order_dual α) _ _ _ _ lemma Sup_range {α : Type*} [has_Sup α] {f : ι → α} : Sup (range f) = supr f := rfl lemma Inf_range {α : Type*} [has_Inf α] {f : ι → α} : Inf (range f) = infi f := rfl lemma supr_range {g : β → α} {f : ι → β} : (⨆b∈range f, g b) = (⨆i, g (f i)) := le_antisymm (supr_le $ assume b, supr_le $ assume ⟨i, (h : f i = b)⟩, h ▸ le_supr _ i) (supr_le $ assume i, le_supr_of_le (f i) $ le_supr (λp, g (f i)) (mem_range_self _)) lemma infi_range {g : β → α} {f : ι → β} : (⨅b∈range f, g b) = (⨅i, g (f i)) := @supr_range (order_dual α) _ _ _ _ _ theorem Inf_image {s : set β} {f : β → α} : Inf (f '' s) = (⨅ a ∈ s, f a) := by rw [← infi_subtype'', infi, range_comp, subtype.range_coe] theorem Sup_image {s : set β} {f : β → α} : Sup (f '' s) = (⨆ a ∈ s, f a) := @Inf_image (order_dual α) _ _ _ _ /- ### supr and infi under set constructions -/ theorem infi_emptyset {f : β → α} : (⨅ x ∈ (∅ : set β), f x) = ⊤ := by simp theorem supr_emptyset {f : β → α} : (⨆ x ∈ (∅ : set β), f x) = ⊥ := by simp theorem infi_univ {f : β → α} : (⨅ x ∈ (univ : set β), f x) = (⨅ x, f x) := by simp theorem supr_univ {f : β → α} : (⨆ x ∈ (univ : set β), f x) = (⨆ x, f x) := by simp theorem infi_union {f : β → α} {s t : set β} : (⨅ x ∈ s ∪ t, f x) = (⨅x∈s, f x) ⊓ (⨅x∈t, f x) := by simp only [← infi_inf_eq, infi_or] lemma infi_split (f : β → α) (p : β → Prop) : (⨅ i, f i) = (⨅ i (h : p i), f i) ⊓ (⨅ i (h : ¬ p i), f i) := by simpa [classical.em] using @infi_union _ _ _ f {i | p i} {i | ¬ p i} lemma infi_split_single (f : β → α) (i₀ : β) : (⨅ i, f i) = f i₀ ⊓ (⨅ i (h : i ≠ i₀), f i) := by convert infi_split _ _; simp theorem infi_le_infi_of_subset {f : β → α} {s t : set β} (h : s ⊆ t) : (⨅ x ∈ t, f x) ≤ (⨅ x ∈ s, f x) := by rw [(union_eq_self_of_subset_left h).symm, infi_union]; exact inf_le_left theorem supr_union {f : β → α} {s t : set β} : (⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) := @infi_union (order_dual α) _ _ _ _ _ lemma supr_split (f : β → α) (p : β → Prop) : (⨆ i, f i) = (⨆ i (h : p i), f i) ⊔ (⨆ i (h : ¬ p i), f i) := @infi_split (order_dual α) _ _ _ _ lemma supr_split_single (f : β → α) (i₀ : β) : (⨆ i, f i) = f i₀ ⊔ (⨆ i (h : i ≠ i₀), f i) := @infi_split_single (order_dual α) _ _ _ _ theorem supr_le_supr_of_subset {f : β → α} {s t : set β} (h : s ⊆ t) : (⨆ x ∈ s, f x) ≤ (⨆ x ∈ t, f x) := @infi_le_infi_of_subset (order_dual α) _ _ _ _ _ h theorem infi_insert {f : β → α} {s : set β} {b : β} : (⨅ x ∈ insert b s, f x) = f b ⊓ (⨅x∈s, f x) := eq.trans infi_union $ congr_arg (λx:α, x ⊓ (⨅x∈s, f x)) infi_infi_eq_left theorem supr_insert {f : β → α} {s : set β} {b : β} : (⨆ x ∈ insert b s, f x) = f b ⊔ (⨆x∈s, f x) := eq.trans supr_union $ congr_arg (λx:α, x ⊔ (⨆x∈s, f x)) supr_supr_eq_left theorem infi_singleton {f : β → α} {b : β} : (⨅ x ∈ (singleton b : set β), f x) = f b := by simp theorem infi_pair {f : β → α} {a b : β} : (⨅ x ∈ ({a, b} : set β), f x) = f a ⊓ f b := by rw [infi_insert, infi_singleton] theorem supr_singleton {f : β → α} {b : β} : (⨆ x ∈ (singleton b : set β), f x) = f b := @infi_singleton (order_dual α) _ _ _ _ theorem supr_pair {f : β → α} {a b : β} : (⨆ x ∈ ({a, b} : set β), f x) = f a ⊔ f b := by rw [supr_insert, supr_singleton] lemma infi_image {γ} {f : β → γ} {g : γ → α} {t : set β} : (⨅ c ∈ f '' t, g c) = (⨅ b ∈ t, g (f b)) := by rw [← Inf_image, ← Inf_image, ← image_comp] lemma supr_image {γ} {f : β → γ} {g : γ → α} {t : set β} : (⨆ c ∈ f '' t, g c) = (⨆ b ∈ t, g (f b)) := @infi_image (order_dual α) _ _ _ _ _ _ /-! ### `supr` and `infi` under `Type` -/ theorem infi_of_empty' (h : ι → false) {s : ι → α} : infi s = ⊤ := top_unique (le_infi $ assume i, (h i).elim) theorem supr_of_empty' (h : ι → false) {s : ι → α} : supr s = ⊥ := bot_unique (supr_le $ assume i, (h i).elim) theorem infi_of_empty (h : ¬nonempty ι) {s : ι → α} : infi s = ⊤ := infi_of_empty' (λ i, h ⟨i⟩) theorem supr_of_empty (h : ¬nonempty ι) {s : ι → α} : supr s = ⊥ := supr_of_empty' (λ i, h ⟨i⟩) @[simp] theorem infi_empty {s : empty → α} : infi s = ⊤ := infi_of_empty nonempty_empty @[simp] theorem supr_empty {s : empty → α} : supr s = ⊥ := supr_of_empty nonempty_empty lemma supr_bool_eq {f : bool → α} : (⨆b:bool, f b) = f tt ⊔ f ff := le_antisymm (supr_le $ assume b, match b with tt := le_sup_left | ff := le_sup_right end) (sup_le (le_supr _ _) (le_supr _ _)) lemma infi_bool_eq {f : bool → α} : (⨅b:bool, f b) = f tt ⊓ f ff := @supr_bool_eq (order_dual α) _ _ lemma is_glb_binfi {s : set β} {f : β → α} : is_glb (f '' s) (⨅ x ∈ s, f x) := by simpa only [range_comp, subtype.range_coe, infi_subtype'] using @is_glb_infi α s _ (f ∘ coe) theorem supr_subtype {p : ι → Prop} {f : subtype p → α} : (⨆ x, f x) = (⨆ i (h:p i), f ⟨i, h⟩) := @infi_subtype (order_dual α) _ _ _ _ lemma supr_subtype' {p : ι → Prop} {f : ∀ i, p i → α} : (⨆ i (h : p i), f i h) = (⨆ x : subtype p, f x x.property) := (@supr_subtype _ _ _ p (λ x, f x.val x.property)).symm lemma Sup_eq_supr' {s : set α} : Sup s = ⨆ x : s, (x : α) := by rw [Sup_eq_supr, supr_subtype']; refl lemma is_lub_bsupr {s : set β} {f : β → α} : is_lub (f '' s) (⨆ x ∈ s, f x) := by simpa only [range_comp, subtype.range_coe, supr_subtype'] using @is_lub_supr α s _ (f ∘ coe) theorem infi_sigma {p : β → Type*} {f : sigma p → α} : (⨅ x, f x) = (⨅ i (h:p i), f ⟨i, h⟩) := le_antisymm (le_infi $ assume i, le_infi $ assume : p i, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) theorem supr_sigma {p : β → Type*} {f : sigma p → α} : (⨆ x, f x) = (⨆ i (h:p i), f ⟨i, h⟩) := @infi_sigma (order_dual α) _ _ _ _ theorem infi_prod {γ : Type*} {f : β × γ → α} : (⨅ x, f x) = (⨅ i j, f (i, j)) := le_antisymm (le_infi $ assume i, le_infi $ assume j, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) theorem supr_prod {γ : Type*} {f : β × γ → α} : (⨆ x, f x) = (⨆ i j, f (i, j)) := @infi_prod (order_dual α) _ _ _ _ theorem infi_sum {γ : Type*} {f : β ⊕ γ → α} : (⨅ x, f x) = (⨅ i, f (sum.inl i)) ⊓ (⨅ j, f (sum.inr j)) := le_antisymm (le_inf (infi_le_infi2 $ assume i, ⟨_, le_refl _⟩) (infi_le_infi2 $ assume j, ⟨_, le_refl _⟩)) (le_infi $ assume s, match s with | sum.inl i := inf_le_left_of_le $ infi_le _ _ | sum.inr j := inf_le_right_of_le $ infi_le _ _ end) theorem supr_sum {γ : Type*} {f : β ⊕ γ → α} : (⨆ x, f x) = (⨆ i, f (sum.inl i)) ⊔ (⨆ j, f (sum.inr j)) := @infi_sum (order_dual α) _ _ _ _ /-! ### `supr` and `infi` under `ℕ` -/ lemma supr_ge_eq_supr_nat_add {u : ℕ → α} (n : ℕ) : (⨆ i ≥ n, u i) = ⨆ i, u (i + n) := begin apply le_antisymm; simp only [supr_le_iff], { exact λ i hi, le_Sup ⟨i - n, by { dsimp only, rw nat.sub_add_cancel hi }⟩ }, { exact λ i, le_Sup ⟨i + n, supr_pos (nat.le_add_left _ _)⟩ } end lemma infi_ge_eq_infi_nat_add {u : ℕ → α} (n : ℕ) : (⨅ i ≥ n, u i) = ⨅ i, u (i + n) := @supr_ge_eq_supr_nat_add (order_dual α) _ _ _ end section complete_linear_order variables [complete_linear_order α] lemma supr_eq_top (f : ι → α) : supr f = ⊤ ↔ (∀b<⊤, ∃i, b < f i) := by simp only [← Sup_range, Sup_eq_top, set.exists_range_iff] lemma infi_eq_bot (f : ι → α) : infi f = ⊥ ↔ (∀b>⊥, ∃i, f i < b) := by simp only [← Inf_range, Inf_eq_bot, set.exists_range_iff] end complete_linear_order /-! ### Instances -/ instance complete_lattice_Prop : complete_lattice Prop := { Sup := λs, ∃a∈s, a, le_Sup := assume s a h p, ⟨a, h, p⟩, Sup_le := assume s a h ⟨b, h', p⟩, h b h' p, Inf := λs, ∀a:Prop, a∈s → a, Inf_le := assume s a h p, p a h, le_Inf := assume s a h p b hb, h b hb p, .. bounded_distrib_lattice_Prop } lemma Inf_Prop_eq {s : set Prop} : Inf s = (∀p ∈ s, p) := rfl lemma Sup_Prop_eq {s : set Prop} : Sup s = (∃p ∈ s, p) := rfl lemma infi_Prop_eq {ι : Sort*} {p : ι → Prop} : (⨅i, p i) = (∀i, p i) := le_antisymm (assume h i, h _ ⟨i, rfl⟩ ) (assume h p ⟨i, eq⟩, eq ▸ h i) lemma supr_Prop_eq {ι : Sort*} {p : ι → Prop} : (⨆i, p i) = (∃i, p i) := le_antisymm (λ ⟨q, ⟨i, (eq : p i = q)⟩, hq⟩, ⟨i, eq.symm ▸ hq⟩) (λ ⟨i, hi⟩, ⟨p i, ⟨i, rfl⟩, hi⟩) instance pi.has_Sup {α : Type*} {β : α → Type*} [Π i, has_Sup (β i)] : has_Sup (Π i, β i) := ⟨λ s i, ⨆ f : s, (f : Π i, β i) i⟩ instance pi.has_Inf {α : Type*} {β : α → Type*} [Π i, has_Inf (β i)] : has_Inf (Π i, β i) := ⟨λ s i, ⨅ f : s, (f : Π i, β i) i⟩ instance pi.complete_lattice {α : Type*} {β : α → Type*} [∀ i, complete_lattice (β i)] : complete_lattice (Π i, β i) := { Sup := Sup, Inf := Inf, le_Sup := λ s f hf i, le_supr (λ f : s, (f : Π i, β i) i) ⟨f, hf⟩, Inf_le := λ s f hf i, infi_le (λ f : s, (f : Π i, β i) i) ⟨f, hf⟩, Sup_le := λ s f hf i, supr_le $ λ g, hf g g.2 i, le_Inf := λ s f hf i, le_infi $ λ g, hf g g.2 i, .. pi.bounded_lattice } lemma Inf_apply {α : Type*} {β : α → Type*} [Π i, has_Inf (β i)] {s : set (Πa, β a)} {a : α} : (Inf s) a = (⨅ f : s, (f : Πa, β a) a) := rfl lemma infi_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Inf (β i)] {f : ι → Πa, β a} {a : α} : (⨅i, f i) a = (⨅i, f i a) := by rw [infi, Inf_apply, infi, infi, ← image_eq_range (λ f : Π i, β i, f a) (range f), ← range_comp] lemma Sup_apply {α : Type*} {β : α → Type*} [Π i, has_Sup (β i)] {s : set (Πa, β a)} {a : α} : (Sup s) a = (⨆f:s, (f : Πa, β a) a) := rfl lemma supr_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Sup (β i)] {f : ι → Πa, β a} {a : α} : (⨆i, f i) a = (⨆i, f i a) := @infi_apply α (λ i, order_dual (β i)) _ _ f a section complete_lattice variables [preorder α] [complete_lattice β] theorem monotone_Sup_of_monotone {s : set (α → β)} (m_s : ∀f∈s, monotone f) : monotone (Sup s) := assume x y h, supr_le $ λ f, le_supr_of_le f $ m_s f f.2 h theorem monotone_Inf_of_monotone {s : set (α → β)} (m_s : ∀f∈s, monotone f) : monotone (Inf s) := assume x y h, le_infi $ λ f, infi_le_of_le f $ m_s f f.2 h end complete_lattice namespace prod variables (α β) instance [has_Inf α] [has_Inf β] : has_Inf (α × β) := ⟨λs, (Inf (prod.fst '' s), Inf (prod.snd '' s))⟩ instance [has_Sup α] [has_Sup β] : has_Sup (α × β) := ⟨λs, (Sup (prod.fst '' s), Sup (prod.snd '' s))⟩ instance [complete_lattice α] [complete_lattice β] : complete_lattice (α × β) := { le_Sup := assume s p hab, ⟨le_Sup $ mem_image_of_mem _ hab, le_Sup $ mem_image_of_mem _ hab⟩, Sup_le := assume s p h, ⟨ Sup_le $ ball_image_of_ball $ assume p hp, (h p hp).1, Sup_le $ ball_image_of_ball $ assume p hp, (h p hp).2⟩, Inf_le := assume s p hab, ⟨Inf_le $ mem_image_of_mem _ hab, Inf_le $ mem_image_of_mem _ hab⟩, le_Inf := assume s p h, ⟨ le_Inf $ ball_image_of_ball $ assume p hp, (h p hp).1, le_Inf $ ball_image_of_ball $ assume p hp, (h p hp).2⟩, .. prod.bounded_lattice α β, .. prod.has_Sup α β, .. prod.has_Inf α β } end prod
e279eea7e046733a70c675d49f5860eb48e2a0e0
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/linarith/verification.lean
ea5123b0dd715ff691737eb2d0b3d2eca5f21aff
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,288
lean
/- Copyright (c) 2020 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import tactic.linarith.elimination import tactic.linarith.parsing /-! # Deriving a proof of false `linarith` uses an untrusted oracle to produce a certificate of unsatisfiability. It needs to do some proof reconstruction work to turn this into a proof term. This file implements the reconstruction. ## Main declarations The public facing declaration in this file is `prove_false_by_linarith`. -/ namespace linarith open ineq tactic native /-! ### Auxiliary functions for assembling proofs -/ /-- `mul_expr n e` creates a `pexpr` representing `n*e`. When elaborated, the coefficient will be a native numeral of the same type as `e`. -/ meta def mul_expr (n : ℕ) (e : expr) : pexpr := if n = 1 then ``(%%e) else ``(%%(nat.to_pexpr n) * %%e) private meta def add_exprs_aux : pexpr → list pexpr → pexpr | p [] := p | p [a] := ``(%%p + %%a) | p (h::t) := add_exprs_aux ``(%%p + %%h) t /-- `add_exprs l` creates a `pexpr` representing the sum of the elements of `l`, associated left. If `l` is empty, it will be the `pexpr` 0. Otherwise, it does not include 0 in the sum. -/ meta def add_exprs : list pexpr → pexpr | [] := ``(0) | (h::t) := add_exprs_aux h t /-- If our goal is to add together two inequalities `t1 R1 0` and `t2 R2 0`, `ineq_const_nm R1 R2` produces the strength of the inequality in the sum `R`, along with the name of a lemma to apply in order to conclude `t1 + t2 R 0`. -/ meta def ineq_const_nm : ineq → ineq → (name × ineq) | eq eq := (``eq_of_eq_of_eq, eq) | eq le := (``le_of_eq_of_le, le) | eq lt := (``lt_of_eq_of_lt, lt) | le eq := (``le_of_le_of_eq, le) | le le := (`add_nonpos, le) | le lt := (`add_lt_of_le_of_neg, lt) | lt eq := (``lt_of_lt_of_eq, lt) | lt le := (`add_lt_of_neg_of_le, lt) | lt lt := (`left.add_neg, lt) /-- `mk_lt_zero_pf_aux c pf npf coeff` assumes that `pf` is a proof of `t1 R1 0` and `npf` is a proof of `t2 R2 0`. It uses `mk_single_comp_zero_pf` to prove `t1 + coeff*t2 R 0`, and returns `R` along with this proof. -/ meta def mk_lt_zero_pf_aux (c : ineq) (pf npf : expr) (coeff : ℕ) : tactic (ineq × expr) := do (iq, h') ← mk_single_comp_zero_pf coeff npf, let (nm, niq) := ineq_const_nm c iq, prod.mk niq <$> mk_app nm [pf, h'] /-- `mk_lt_zero_pf coeffs pfs` takes a list of proofs of the form `tᵢ Rᵢ 0`, paired with coefficients `cᵢ`. It produces a proof that `∑cᵢ * tᵢ R 0`, where `R` is as strong as possible. -/ meta def mk_lt_zero_pf : list (expr × ℕ) → tactic expr | [] := fail "no linear hypotheses found" | [(h, c)] := prod.snd <$> mk_single_comp_zero_pf c h | ((h, c)::t) := do (iq, h') ← mk_single_comp_zero_pf c h, prod.snd <$> t.mfoldl (λ pr ce, mk_lt_zero_pf_aux pr.1 pr.2 ce.1 ce.2) (iq, h') /-- If `prf` is a proof of `t R s`, `term_of_ineq_prf prf` returns `t`. -/ meta def term_of_ineq_prf (prf : expr) : tactic expr := prod.fst <$> (infer_type prf >>= instantiate_mvars >>= get_rel_sides) /-- If `prf` is a proof of `t R s`, `ineq_prf_tp prf` returns the type of `t`. -/ meta def ineq_prf_tp (prf : expr) : tactic expr := term_of_ineq_prf prf >>= infer_type >>= instantiate_mvars /-- `mk_neg_one_lt_zero_pf tp` returns a proof of `-1 < 0`, where the numerals are natively of type `tp`. -/ meta def mk_neg_one_lt_zero_pf (tp : expr) : tactic expr := do h ← mk_mapp `linarith.zero_lt_one [tp, none, none], mk_app `neg_neg_of_pos [h] /-- If `e` is a proof that `t = 0`, `mk_neg_eq_zero_pf e` returns a proof that `-t = 0`. -/ meta def mk_neg_eq_zero_pf (e : expr) : tactic expr := to_expr ``(neg_eq_zero.mpr %%e) /-- `prove_eq_zero_using tac e` tries to use `tac` to construct a proof of `e = 0`. -/ meta def prove_eq_zero_using (tac : tactic unit) (e : expr) : tactic expr := do tgt ← to_expr ``(%%e = 0), prod.snd <$> solve_aux tgt (tac >> done) /-- `add_neg_eq_pfs l` inspects the list of proofs `l` for proofs of the form `t = 0`. For each such proof, it adds a proof of `-t = 0` to the list. -/ meta def add_neg_eq_pfs : list expr → tactic (list expr) | [] := return [] | (h::t) := do some (iq, tp) ← parse_into_comp_and_expr <$> (infer_type h >>= instantiate_mvars), match iq with | ineq.eq := do nep ← mk_neg_eq_zero_pf h, tl ← add_neg_eq_pfs t, return $ h::nep::tl | _ := list.cons h <$> add_neg_eq_pfs t end /-! #### The main method -/ /-- `prove_false_by_linarith` is the main workhorse of `linarith`. Given a list `l` of proofs of `tᵢ Rᵢ 0`, it tries to derive a contradiction from `l` and use this to produce a proof of `false`. An oracle is used to search for a certificate of unsatisfiability. In the current implementation, this is the Fourier Motzkin elimination routine in `elimination.lean`, but other oracles could easily be swapped in. The returned certificate is a map `m` from hypothesis indices to natural number coefficients. If our set of hypotheses has the form `{tᵢ Rᵢ 0}`, then the elimination process should have guaranteed that 1.\ `∑ (m i)*tᵢ = 0`, with at least one `i` such that `m i > 0` and `Rᵢ` is `<`. We have also that 2.\ `∑ (m i)*tᵢ < 0`, since for each `i`, `(m i)*tᵢ ≤ 0` and at least one is strictly negative. So we conclude a contradiction `0 < 0`. It remains to produce proofs of (1) and (2). (1) is verified by calling the `discharger` tactic of the `linarith_config` object, which is typically `ring`. We prove (2) by folding over the set of hypotheses. -/ meta def prove_false_by_linarith (cfg : linarith_config) : list expr → tactic expr | [] := fail "no args to linarith" | l@(h::t) := do -- for the elimination to work properly, we must add a proof of `-1 < 0` to the list, -- along with negated equality proofs. l' ← add_neg_eq_pfs l, hz ← ineq_prf_tp h >>= mk_neg_one_lt_zero_pf, let inputs := hz::l', -- perform the elimination and fail if no contradiction is found. (comps, max_var) ← linear_forms_and_max_var cfg.transparency inputs, certificate ← cfg.oracle.get_or_else fourier_motzkin.produce_certificate comps max_var <|> fail "linarith failed to find a contradiction", linarith_trace "linarith has found a contradiction", let enum_inputs := inputs.enum, -- construct a list pairing nonzero coeffs with the proof of their corresponding comparison let zip := enum_inputs.filter_map $ λ ⟨n, e⟩, prod.mk e <$> certificate.find n, mls ← zip.mmap (λ ⟨e, n⟩, do e ← term_of_ineq_prf e, return (mul_expr n e)), -- `sm` is the sum of input terms, scaled to cancel out all variables. sm ← to_expr $ add_exprs mls, pformat! "The expression\n {sm}\nshould be both 0 and negative" >>= linarith_trace, -- we prove that `sm = 0`, typically with `ring`. sm_eq_zero ← prove_eq_zero_using cfg.discharger sm, linarith_trace "We have proved that it is zero", -- we also prove that `sm < 0`. sm_lt_zero ← mk_lt_zero_pf zip, linarith_trace "We have proved that it is negative", -- this is a contradiction. pftp ← infer_type sm_lt_zero, (_, nep, _) ← rewrite_core sm_eq_zero pftp, pf' ← mk_eq_mp nep sm_lt_zero, mk_app `lt_irrefl [pf'] end linarith
09419cebf9702981e870b8b2a98341d7d52e28dc
d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6
/Papyrus/Internal/Enum.lean
cb05f0b9edb730d0b4e29c18bfb3979f6b53bb7c
[ "Apache-2.0" ]
permissive
xubaiw/lean4-papyrus
c3fbbf8ba162eb5f210155ae4e20feb2d32c8182
02e82973a5badda26fc0f9fd15b3d37e2eb309e0
refs/heads/master
1,691,425,756,824
1,632,122,825,000
1,632,123,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,405
lean
import Lean.Parser.Command open Lean Parser Command namespace Papyrus.Internal def mkSealedEnumReprInst (ty : Syntax) (ctors : Array Syntax) : MacroM Syntax := do let currNamespace ← Macro.getCurrNamespace let ctorFmts ← ctors.mapM fun ctor => `(Std.format $(quote <| toString (currNamespace ++ ctor[2].getId))) `(def reprFormats : Array Std.Format := #[$[$ctorFmts],*] instance : Repr $ty := ⟨fun e _ => reprFormats[e.val.val]⟩) def unpackOptDeriving : (stx : Syntax) → (Array Syntax × Array (Option Syntax)) | `(optDeriving| deriving $[$clss $[with $argss?]?],*) => (clss, argss?) | _ => (#[], #[]) set_option hygiene false -------------------------------------------------------------------------------- -- # Open Enums -------------------------------------------------------------------------------- syntax enumCtor := "\n| " declModifiers ident " := " term scoped macro (name := enumDecl) mods:declModifiers "enum " id:ident " : " type:term optional(" := " <|> " where ") ctors:many(enumCtor) deriv?:optDeriving : command => do let mut defs : Array Syntax := #[] -- structure defs := defs.push <| ← `($mods:declModifiers structure $id where val : $type $deriv?:optDeriving) -- constructors for ctor in ctors do let ctorId := ctor[2] let ctorQualId := mkIdentFrom ctorId <| id.getId.modifyBase (· ++ ctorId.getId) let ctorVal := ctor[4] let ctorMods := ctor[1] defs := defs.push <| ← `($ctorMods:declModifiers def $ctorQualId:ident : $id := mk $ctorVal) mkNullNode defs -------------------------------------------------------------------------------- -- # Sealed Enums -------------------------------------------------------------------------------- syntax identCtor := "\n| " declModifiers ident scoped macro (name := sealedEnumDecl) mods:declModifiers "sealed-enum " id:ident " : " type:term optional(" := " <|> " where ") ctors:many(identCtor) deriv?:optDeriving : command => do let mut innerDefs := #[] let numCtors := ctors.size let maxValLit := quote (numCtors - 1) -- filter out special deriving instances let mut derivBEq := false let mut derivDecEq := false let mut derivInhabited := false let mut derivRepr := false let mut remClasses := #[] let mut remArgss? := #[] let (classes, argss?) := unpackOptDeriving deriv? for cls in classes, args? in argss? do if cls.matchesIdent ``BEq then derivBEq := true else if cls.matchesIdent ``DecidableEq then derivDecEq := true else if cls.matchesIdent ``Inhabited then derivInhabited := true else if cls.matchesIdent ``Repr then derivRepr := true else remClasses := remClasses.push cls remArgss? := remArgss?.push args? -- structure let structDecl ← `($mods:declModifiers structure $id where val : $type h : val ≤ $maxValLit deriving $[$remClasses $[with $remArgss?]?],*) -- maximum innerDefs := innerDefs.push <| ← `(def maxVal : $type := $maxValLit) -- theorems innerDefs := innerDefs.push <| ← `(theorem eq_of_val_eq : {a b : $id} → a.val = b.val → a = b | ⟨v, h⟩, ⟨_, _⟩, rfl => rfl theorem val_eq_of_eq {a b : $id} (h : a = b) : a.val = b.val := h ▸ rfl theorem ne_of_val_ne {a b : $id} (h : a.val ≠ b.val) : a ≠ b := fun h' => absurd (val_eq_of_eq h') h) -- constructors for ctor in ctors, i in [:numCtors] do let ctorVal := quote i let ctorMods := ctor[1] let ctorId := ctor[2] innerDefs := innerDefs.push <| ← `($ctorMods:declModifiers def $ctorId:ident : $id := mk $ctorVal (by decide)) -- derive special instance if derivBEq then innerDefs := innerDefs.push <| ← `(instance : BEq $id := ⟨fun a b => a.val == b.val⟩) if derivDecEq then innerDefs := innerDefs.push <| ← `(instance : DecidableEq $id := fun a b => if h : a.val = b.val then isTrue (eq_of_val_eq h) else isFalse (ne_of_val_ne h)) if derivInhabited then innerDefs := innerDefs.push <| ← `(instance : Inhabited $id := ⟨mk maxVal (Nat.le_refl _)⟩) if derivRepr then innerDefs := innerDefs.push <| ← mkSealedEnumReprInst id ctors -- syntax `($structDecl:command namespace $id:ident $(mkNullNode innerDefs) end $id:ident)
75d6039256afb3bfd4f7ec34514ca9f715df2353
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/algebra/char_p/invertible.lean
d22b4acf207ad91b467c1da14ec2ce2ef774a2ff
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,914
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Anne Baanen -/ import algebra.invertible import algebra.field import algebra.char_p.basic /-! # Invertibility of elements given a characteristic This file includes some instances of `invertible` for specific numbers in characteristic zero. Some more cases are given as a `def`, to be included only when needed. To construct instances for concrete numbers, `invertible_of_nonzero` is a useful definition. -/ variables {K : Type*} section field variables [field K] /-- A natural number `t` is invertible in a field `K` if the charactistic of `K` does not divide `t`. -/ def invertible_of_ring_char_not_dvd {t : ℕ} (not_dvd : ¬(ring_char K ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((ring_char.spec K t).mp h)) /-- A natural number `t` is invertible in a field `K` of charactistic `p` if `p` does not divide `t`. -/ def invertible_of_char_p_not_dvd {p : ℕ} [char_p K p] {t : ℕ} (not_dvd : ¬(p ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((char_p.cast_eq_zero_iff K p t).mp h)) instance invertible_of_pos [char_zero K] (n : ℕ) [h : fact (0 < n)] : invertible (n : K) := invertible_of_nonzero $ by simpa [pos_iff_ne_zero] using h end field section division_ring variables [division_ring K] [char_zero K] instance invertible_succ (n : ℕ) : invertible (n.succ : K) := invertible_of_nonzero (nat.cast_ne_zero.mpr (nat.succ_ne_zero _)) /-! A few `invertible n` instances for small numerals `n`. Feel free to add your own number when you need its inverse. -/ instance invertible_two : invertible (2 : K) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 2 ≠ 0)) instance invertible_three : invertible (3 : K) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 3 ≠ 0)) end division_ring
fe44df96ca3a432d129619b25aaea904230edf65
bdb33f8b7ea65f7705fc342a178508e2722eb851
/algebra/ordered_field.lean
27e617b3f9f7fc618318df5260fa63ef5c289464
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
8,093
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.ordered_ring algebra.field section linear_ordered_field variables {α : Type*} [linear_ordered_field α] {a b c d : α} def div_pos := @div_pos_of_pos_of_pos lemma inv_pos {a : α} : 0 < a → 0 < a⁻¹ := by rw [inv_eq_one_div]; exact div_pos zero_lt_one lemma inv_lt_zero {a : α} : a < 0 → a⁻¹ < 0 := by rw [inv_eq_one_div]; exact div_neg_of_pos_of_neg zero_lt_one lemma one_le_div_iff_le (hb : 0 < b) : 1 ≤ a / b ↔ b ≤ a := ⟨le_of_one_le_div a hb, one_le_div_of_le a hb⟩ lemma one_lt_div_iff_lt (hb : 0 < b) : 1 < a / b ↔ b < a := ⟨lt_of_one_lt_div a hb, one_lt_div_of_lt a hb⟩ lemma div_le_one_iff_le (hb : 0 < b) : a / b ≤ 1 ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 (one_lt_div_iff_lt hb) lemma div_lt_one_iff_lt (hb : 0 < b) : a / b < 1 ↔ a < b := le_iff_le_iff_lt_iff_lt.1 (one_le_div_iff_le hb) lemma le_div_iff (hc : 0 < c) : a ≤ b / c ↔ a * c ≤ b := ⟨mul_le_of_le_div hc, le_div_of_mul_le hc⟩ lemma le_div_iff' (hc : 0 < c) : a ≤ b / c ↔ c * a ≤ b := by rw [mul_comm, le_div_iff hc] lemma div_le_iff (hb : 0 < b) : a / b ≤ c ↔ a ≤ c * b := ⟨le_mul_of_div_le hb, by rw [mul_comm]; exact div_le_of_le_mul hb⟩ lemma div_le_iff' (hb : 0 < b) : a / b ≤ c ↔ a ≤ b * c := by rw [mul_comm, div_le_iff hb] lemma lt_div_iff (hc : 0 < c) : a < b / c ↔ a * c < b := ⟨mul_lt_of_lt_div hc, lt_div_of_mul_lt hc⟩ lemma lt_div_iff' (hc : 0 < c) : a < b / c ↔ c * a < b := by rw [mul_comm, lt_div_iff hc] lemma div_le_iff_of_neg (hc : c < 0) : b / c ≤ a ↔ a * c ≤ b := ⟨mul_le_of_div_le_of_neg hc, div_le_of_mul_le_of_neg hc⟩ lemma div_lt_iff (hc : 0 < c) : b / c < a ↔ b < a * c := le_iff_le_iff_lt_iff_lt.1 (le_div_iff hc) lemma div_lt_iff' (hc : 0 < c) : b / c < a ↔ b < c * a := by rw [mul_comm, div_lt_iff hc] lemma div_lt_iff_of_neg (hc : c < 0) : b / c < a ↔ a * c < b := ⟨mul_lt_of_gt_div_of_neg hc, div_lt_of_mul_gt_of_neg hc⟩ lemma inv_le_inv (ha : 0 < a) (hb : 0 < b) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := by rw [inv_eq_one_div, div_le_iff ha, ← div_eq_inv_mul, one_le_div_iff_le hb] lemma inv_le (ha : 0 < a) (hb : 0 < b) : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := by rw [← inv_le_inv hb (inv_pos ha), division_ring.inv_inv (ne_of_gt ha)] lemma le_inv (ha : 0 < a) (hb : 0 < b) : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := by rw [← inv_le_inv (inv_pos hb) ha, division_ring.inv_inv (ne_of_gt hb)] lemma one_div_le_one_div (ha : 0 < a) (hb : 0 < b) : 1 / a ≤ 1 / b ↔ b ≤ a := by simpa [one_div_eq_inv] using inv_le_inv ha hb lemma inv_lt_inv (ha : 0 < a) (hb : 0 < b) : a⁻¹ < b⁻¹ ↔ b < a := le_iff_le_iff_lt_iff_lt.1 (inv_le_inv hb ha) lemma inv_lt (ha : 0 < a) (hb : 0 < b) : a⁻¹ < b ↔ b⁻¹ < a := le_iff_le_iff_lt_iff_lt.1 (le_inv hb ha) lemma lt_inv (ha : 0 < a) (hb : 0 < b) : a < b⁻¹ ↔ b < a⁻¹ := le_iff_le_iff_lt_iff_lt.1 (inv_le hb ha) lemma one_div_lt_one_div (ha : 0 < a) (hb : 0 < b) : 1 / a < 1 / b ↔ b < a := le_iff_le_iff_lt_iff_lt.1 (one_div_le_one_div hb ha) def div_nonneg := @div_nonneg_of_nonneg_of_pos lemma div_lt_div_right (hc : 0 < c) : a / c < b / c ↔ a < b := ⟨le_imp_le_iff_lt_imp_lt.1 (λ h, div_le_div_of_le_of_pos h hc), λ h, div_lt_div_of_lt_of_pos h hc⟩ lemma div_le_div_right (hc : 0 < c) : a / c ≤ b / c ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 (div_lt_div_right hc) lemma div_lt_div_right_of_neg (hc : c < 0) : a / c < b / c ↔ b < a := ⟨le_imp_le_iff_lt_imp_lt.1 (λ h, div_le_div_of_le_of_neg h hc), λ h, div_lt_div_of_lt_of_neg h hc⟩ lemma div_le_div_right_of_neg (hc : c < 0) : a / c ≤ b / c ↔ b ≤ a := le_iff_le_iff_lt_iff_lt.2 (div_lt_div_right_of_neg hc) lemma div_lt_div_left (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b < a / c ↔ c < b := (mul_lt_mul_left ha).trans (inv_lt_inv hb hc) lemma div_le_div_left (ha : 0 < a) (hb : 0 < b) (hc : 0 < c) : a / b ≤ a / c ↔ c ≤ b := le_iff_le_iff_lt_iff_lt.2 (div_lt_div_left ha hc hb) lemma div_lt_div_iff (b0 : 0 < b) (d0 : 0 < d) : a / b < c / d ↔ a * d < c * b := by rw [lt_div_iff d0, div_mul_eq_mul_div, div_lt_iff b0] lemma div_lt_div (hac : a < c) (hbd : d ≤ b) (c0 : 0 ≤ c) (d0 : 0 < d) : a / b < c / d := (div_lt_div_iff (lt_of_lt_of_le d0 hbd) d0).2 (mul_lt_mul hac hbd d0 c0) lemma div_lt_div' (hac : a ≤ c) (hbd : d < b) (c0 : 0 < c) (d0 : 0 < d) : a / b < c / d := (div_lt_div_iff (lt_trans d0 hbd) d0).2 (mul_lt_mul' hac hbd (le_of_lt d0) c0) lemma half_pos {a : α} (h : 0 < a) : 0 < a / 2 := div_pos h two_pos def half_lt_self := @div_two_lt_of_pos lemma ivl_translate : (λx, x + c) '' {r:α | a ≤ r ∧ r ≤ b } = {r:α | a + c ≤ r ∧ r ≤ b + c} := calc (λx, x + c) '' {r | a ≤ r ∧ r ≤ b } = (λx, x - c) ⁻¹' {r | a ≤ r ∧ r ≤ b } : congr_fun (set.image_eq_preimage_of_inverse (assume a, add_sub_cancel a c) (assume b, sub_add_cancel b c)) _ ... = {r | a + c ≤ r ∧ r ≤ b + c} : set.ext $ by simp [-sub_eq_add_neg, le_sub_iff_add_le, sub_le_iff_le_add] lemma ivl_stretch (hc : 0 < c) : (λx, x * c) '' {r | a ≤ r ∧ r ≤ b } = {r | a * c ≤ r ∧ r ≤ b * c} := calc (λx, x * c) '' {r | a ≤ r ∧ r ≤ b } = (λx, x / c) ⁻¹' {r | a ≤ r ∧ r ≤ b } : congr_fun (set.image_eq_preimage_of_inverse (assume a, mul_div_cancel _ $ ne_of_gt hc) (assume b, div_mul_cancel _ $ ne_of_gt hc)) _ ... = {r | a * c ≤ r ∧ r ≤ b * c} : set.ext $ by simp [le_div_iff, div_le_iff, hc] instance linear_ordered_field.to_densely_ordered [linear_ordered_field α] : densely_ordered α := { dense := assume a₁ a₂ h, ⟨(a₁ + a₂) / 2, calc a₁ = (a₁ + a₁) / 2 : (add_self_div_two a₁).symm ... < (a₁ + a₂) / 2 : div_lt_div_of_lt_of_pos (add_lt_add_left h _) two_pos, calc (a₁ + a₂) / 2 < (a₂ + a₂) / 2 : div_lt_div_of_lt_of_pos (add_lt_add_right h _) two_pos ... = a₂ : add_self_div_two a₂⟩ } instance linear_ordered_field.to_no_top_order [linear_ordered_field α] : no_top_order α := { no_top := assume a, ⟨a + 1, lt_add_of_le_of_pos (le_refl a) zero_lt_one ⟩ } instance linear_ordered_field.to_no_bot_order [linear_ordered_field α] : no_bot_order α := { no_bot := assume a, ⟨a + -1, add_lt_of_le_of_neg (le_refl _) (neg_lt_of_neg_lt $ by simp [zero_lt_one]) ⟩ } lemma inv_lt_one {a : α} (ha : 1 < a) : a⁻¹ < 1 := by rw [inv_eq_one_div]; exact div_lt_of_mul_lt_of_pos (lt_trans zero_lt_one ha) (by simp *) lemma one_lt_inv (h₁ : 0 < a) (h₂ : a < 1) : 1 < a⁻¹ := by rw [inv_eq_one_div, lt_div_iff h₁]; simp [h₂] lemma mul_self_inj_of_nonneg {a b : α} (a0 : 0 ≤ a) (b0 : 0 ≤ b) : a * a = b * b ↔ a = b := (mul_self_eq_mul_self_iff a b).trans $ or_iff_left_of_imp $ λ h, by subst a; rw [le_antisymm (neg_nonneg.1 a0) b0, neg_zero] end linear_ordered_field section variables {α : Type*} [discrete_linear_ordered_field α] (a b c : α) lemma inv_pos' {a : α} : 0 < a⁻¹ ↔ 0 < a := ⟨by rw [inv_eq_one_div]; exact pos_of_one_div_pos, inv_pos⟩ lemma inv_neg' {a : α} : a⁻¹ < 0 ↔ a < 0 := ⟨by rw [inv_eq_one_div]; exact neg_of_one_div_neg, inv_lt_zero⟩ lemma inv_nonneg {a : α} : 0 ≤ a⁻¹ ↔ 0 ≤ a := le_iff_le_iff_lt_iff_lt.2 inv_neg' lemma inv_nonpos {a : α} : a⁻¹ ≤ 0 ↔ a ≤ 0 := le_iff_le_iff_lt_iff_lt.2 inv_pos' lemma abs_inv : abs a⁻¹ = (abs a)⁻¹ := have h : abs (1 / a) = 1 / abs a, begin rw [abs_div, abs_of_nonneg], exact zero_le_one end, by simp [*] at * lemma inv_neg : (-a)⁻¹ = -(a⁻¹) := if h : a = 0 then by simp [h, inv_zero] else by rwa [inv_eq_one_div, inv_eq_one_div, div_neg_eq_neg_div] lemma inv_le_inv_of_le {a b : α} (hb : 0 < b) (h : b ≤ a) : a⁻¹ ≤ b⁻¹ := begin rw [inv_eq_one_div, inv_eq_one_div], exact one_div_le_one_div_of_le hb h end lemma div_nonneg' {a b : α} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a / b := (lt_or_eq_of_le hb).elim (div_nonneg ha) (λ h, by simp [h.symm]) end
cd4968f07a32ee99446499e00fcd44215bca79c1
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_simp_st1.lean
290e3b168f760a16c6a49d1013694cba42dc131e
[ "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
467
lean
import data.nat open algebra nat section open nat set_option blast.strategy "preprocess" -- make sure simplifier is not used by default attribute add.comm [simp] attribute add.assoc [simp] attribute add.left_comm [simp] example (a b c : nat) : a + b + b + c = c + b + a + b := by simp example (a b c : nat) : a = b → a + c = c + b := by simp definition f : nat → nat := sorry example (a b c : nat) : f a = f b → f a + c = c + f b := by simp end
8e606a3553e8e0b41e5b490b9c471b87613be3a9
437dc96105f48409c3981d46fb48e57c9ac3a3e4
/src/tactic/lint/basic.lean
ce58c13ea8979be35eb051daee4e43027ab69add
[ "Apache-2.0" ]
permissive
dan-c-k/mathlib
08efec79bd7481ee6da9cc44c24a653bff4fbe0d
96efc220f6225bc7a5ed8349900391a33a38cc56
refs/heads/master
1,658,082,847,093
1,589,013,201,000
1,589,013,201,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,174
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner -/ import tactic.core /-! # Basic linter types and attributes This file defines the basic types and attributes used by the linting framework. A linter essentially consists of a function `declaration → tactic (option string)`, this function together with some metadata is stored in the `linter` structure. We define two attributes: * `@[linter]` applies to a declaration of type `linter` and adds it to the default linter set. * `@[nolint linter_name]` omits the tagged declaration from being checked by the linter with name `linter_name`. -/ open tactic setup_tactic_parser -- Manual constant subexpression elimination for performance. private meta def linter_ns := `linter private meta def nolint_infix := `_nolint /-- Computes the declaration name used to store the nolint attribute data. Retrieving the parameters for user attributes is *extremely* slow. Hence we store the parameters of the nolint attribute as declarations in the environment. E.g. for `@[nolint foo] def bar := _` we add the following declaration: ```lean meta def bar._nolint.foo : unit := () ``` -/ private meta def mk_nolint_decl_name (decl : name) (linter : name) : name := (decl ++ nolint_infix) ++ linter /-- Defines the user attribute `nolint` for skipping `#lint` -/ @[user_attribute] meta def nolint_attr : user_attribute _ (list name) := { name := "nolint", descr := "Do not report this declaration in any of the tests of `#lint`", after_set := some $ λ n _ _, (do ls@(_::_) ← nolint_attr.get_param n | fail "you need to specify at least one linter to disable", ls.mmap' $ λ l, do get_decl (linter_ns ++ l) <|> fail ("unknown linter: " ++ to_string l), try $ add_decl $ declaration.defn (mk_nolint_decl_name n l) [] `(unit) `(unit.star) (default _) ff), parser := ident* } add_tactic_doc { name := "nolint", category := doc_category.attr, decl_names := [`nolint_attr], tags := ["linting"] } /-- `should_be_linted linter decl` returns true if `decl` should be checked using `linter`, i.e., if there is no `nolint` attribute. -/ meta def should_be_linted (linter : name) (decl : name) : tactic bool := do e ← get_env, pure $ ¬ e.contains (mk_nolint_decl_name decl linter) /-- A linting test for the `#lint` command. `test` defines a test to perform on every declaration. It should never fail. Returning `none` signifies a passing test. Returning `some msg` reports a failing test with error `msg`. `no_errors_found` is the message printed when all tests are negative, and `errors_found` is printed when at least one test is positive. If `is_fast` is false, this test will be omitted from `#lint-`. If `auto_decls` is true, this test will also be executed on automatically generated declarations. -/ meta structure linter := (test : declaration → tactic (option string)) (no_errors_found : string) (errors_found : string) (is_fast : bool := tt) (auto_decls : bool) /-- Takes a list of names that resolve to declarations of type `linter`, and produces a list of linters. -/ meta def get_linters (l : list name) : tactic (list (name × linter)) := l.mmap (λ n, prod.mk n.last <$> (mk_const n >>= eval_expr linter) <|> fail format!"invalid linter: {n}") /-- Defines the user attribute `linter` for adding a linter to the default set. Linters should be defined in the `linter` namespace. A linter `linter.my_new_linter` is referred to as `my_new_linter` (without the `linter` namespace) when used in `#lint`. -/ @[user_attribute] meta def linter_attr : user_attribute unit unit := { name := "linter", descr := "Use this declaration as a linting test in #lint", after_set := some $ λ nm _ _, mk_const nm >>= infer_type >>= unify `(linter) } add_tactic_doc { name := "linter", category := doc_category.attr, decl_names := [`linter_attr], tags := ["linting"] }
eea52b4557b73e3ebd01e18799e1ad40dca43110
d450724ba99f5b50b57d244eb41fef9f6789db81
/src/mywork/lectures/lecture_24 copy.lean
5f610ad04e3e545d00bb11d01ab947afa07cfa94
[]
no_license
jakekauff/CS2120F21
4f009adeb4ce4a148442b562196d66cc6c04530c
e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad
refs/heads/main
1,693,841,880,030
1,637,604,848,000
1,637,604,848,000
399,946,698
0
0
null
null
null
null
UTF-8
Lean
false
false
7,506
lean
import .lecture_23 variables {α β : Type} (r : β → β → Prop) local infix `≺`:50 := r /- UNIVERSAL QUANTIFICATION OVER AN EMPTY SET IS TRUE Let's review the most puzzling of the examples from last time: a relation r = {(0,1), (2,3)} we said is transitive because it satisfies the constraint that defines transitivity: for every x, y, and x, if (x,y) is in r, and (y,z) is in r, then (x,z) is in r. This relation is transitive because in *every* case where we have (x, y) and (y, z) in r, (x, z) is in r. In this example, there are *no* such cases, and so the predicate is satisfied! Let's think about this principle using a different example. Question: is every ball in an empty bucket of balls red? -/ axioms (Ball : Type) (red : Ball → Prop) def empty_bucket := ({} : set Ball) lemma allBallsInEmptyBucketAreRed : ∀ (b : Ball), b ∈ empty_bucket → red b := begin assume b h, cases h, -- finish off this proof end /- Whoa, ok. That's a little bit counterintuitive, but it's correct. A universal quantification over an empty set is always trivially true. Here's another way to think about it. Suppose we had a set with two balls in it: { b1, b2 }. To show that every ball is red, we could use case analysis: break the task into two cases: show that b1 is red, AND show that all balls in the remaining set, { b2 }, are red. To prove the latter, we'd show that b2 is red, and that all balls in the remaining set, {}, are red. So we have that all balls are red if (red b1) ∧ (red b2) ∧ "all balls in {} are red". If b1 and b2 really are red, then the last proposition better be true if the whole chain of ∧ operations is to be true. When you think about ∀ as a version of ∧ that can take any number of arguments, not just 2, it becomes clear that when applied to zero arguments, the answer really has to be true, otherwise this operation would *always* produce propositions that are ultimately false. -/ /- So now let's revisit once again our funny example of transitivity: { (0,1), (2,3) }. There are no cases here where we have both (x,y) and (y,z) as pairs in this relation, so there are no cases to consider. When there are no cases to consider, the conclusion holds in all cases, of which there are 0, so it holds. Question: Is this symmetric? {(0,1), (1,0), (2,2)} How about this: {(0,1), (1,0), (2,3)}? Now suppose that we have a relation, r, over a set of values, {0, 1, 2, 3, 4, 5}. Is this relation reflexive? {} What about this: {(0, 1), (2, 3)} Question: If a relation is transitive and symmetric is it necessarily reflexive? If so, give an informal argument/proof. If not, give a counter-example. -/ /- CLOSURE OPERATIONS ON RELATIONS Given a relation, r, the reflexive, symmetric, or transitive closure of r is the smallest relation that (1) contains r, and (2) contains any additional pairs needed to make the resulting relation reflexive, or symmetric, or transitive, respectively. The reflexive, symmetric, transitive closure of r is the smallest relation that contains r and has all three properties. Note that the resulting relation will be an equivalence relation. -/ -- REFLEXIVE CLOSURE /- A pair (a, b) is in the reflexive closure of r if (a, b) is in r or if (a = b). -/ def reflexive_closure := λ (a b : β), (r a b) ∨ (a = b) /- Exercise: what pairs are in the reflexivee closure of the following relation, r, over the set {0, 1, 2}? r = {} -/ -- SYMMETRIC CLOSURE /- A pair (a, b) is in the symmetric closure of r if (a, b) is in r or if (b, a) is i r. -/ def symmetric_closure := λ (a b : β), (r a b) ∨ (r b a) /- Consider a set, s = {0, 1, 2, 3} and a binary relation on s, r = {(0,1),(3,2)}. What pairs are in the symmetric closure, (sc r), of r. Clearly (0,1) and (3,2) are in (sc r), because they are in r. But are (1,0) and (2,3) in sc r, as we expect? Let's apply symmetric_closure to a=1 b=0. This pair is defined to be in the *closure* if the resulting proposition is true. The proposition is (a,b) is in r or (b, a) is in r. Clearly (a=1,b=0) is not in r, but (b=0,a=1) is in r, so (a=1,b=0) is (defined to be) in (sc r). In a nutshell, the symmetric closure includes an edge/pair if either it is in r, or its reverse is r. -/ /- Let's look examples. What's in the reflexive closure of { (0,1), (1,2), (2, 3), (3, 4), (4, 5) }? It is often easier to think about these things with the aid of a picture, so let's draw this relation and see in graphical terms what it means to compute its reflexive closure. -/ -- TRANSITIVE CLOSURE /- The definitions of the reflexive and symmetric closures of a relation are pretty easy to state. It's a little harder to say what set of pairs is in the transitive closure of a given relation, r. Clearly it's a relation, r', where r' is the smallest relation that contains r and that is transitive. In other words, (tc r) is r with any additional pairs needed to make the result transitive. What set of pairs is this? Well, (1) it contains every pair in r, and (2) if (a,b) is in (tc r) and if (b, c) is in (tc r), then (a, c) must also be in r. The way we will say this formally uses what we call an inductive definition. Inductive definitions allow for "bigger" things to be built whenever there are smaller things of the right kind. Consider this relation, r = { (0,1), (1,2), (2, 3) }. It's transitive closure clearly contains all three of these pairs. What else must it include at a minimum to be transitive? Consider r = { (0,1), (1,2), (2, 3), (3, 4), (4, 5) }. What is its transitive closure? In plain English, if there's a "path" of pairs between two values, a and c, e.g., by way of b where (a,b) and (b,c) are in the relation, then (a,c) will also be in the relation, i.e., the direct connection, (a,c), will also be in the relation. The following formal definition is not one that I expect you to understand immediately, as we have not yet introduced iductive definitions, of which this is an example. But you can just read it as definining the introduction rules, base and trans, for proving one relation is the transitive closure of another, r. The first rule says that if any two objects, a and b, are related in r, then they are also related in tc r (the transitive closure of r). The second rule says that if objects a and b are related in (tc r) and b and c are related in (tc r) then a and c must also be related in r. For any length-2 "path" from a to c (via b), then there's a direct connection: (a,c) ∈ r. -/ namespace hidden inductive tc {α : Type} (r : α → α → Prop) : α → α → Prop | base : ∀ a b, r a b → tc a b | trans : ∀ a b c, tc a b → tc b c → tc a c end hidden /- Here's a possibly surprising fact: the transitive closure concept *cannot be expressed, defined, nor the concept used, in first-order predicate logic*. The reason is that you can't quantify over relations, and so cannot write "for any relation, r, it's transitive closure is ..." Quantifying over relations just isn't allowed: it's not even part of the syntax of first- order predicate logic. Yet what we've written, formally and understandably, is a concept essential in all kinds of logical reasoning in computer science. That suggests something about teaching first-order logic as a first logic for computer science: there's real reason to doubt that it's the best choice. The higher-order predicate logic of Lean and similar modern proof assistants is strictly more expressive. -/
c6036ea06ac974bbb7194c37479139794edcc5b7
efce24474b28579aba3272fdb77177dc2b11d7aa
/src/data/is_equiv.lean
fac8a6419aa59a2d512f15d4f2fbad85e00f5c45
[ "Apache-2.0" ]
permissive
rwbarton/lean-homotopy-theory
cff499f24268d60e1c546e7c86c33f58c62888ed
39e1b4ea1ed1b0eca2f68bc64162dde6a6396dee
refs/heads/lean-3.4.2
1,622,711,883,224
1,598,550,958,000
1,598,550,958,000
136,023,667
12
6
Apache-2.0
1,573,187,573,000
1,528,116,262,000
Lean
UTF-8
Lean
false
false
844
lean
import data.equiv.basic universes u v w variables {α : Sort u} {β : Sort v} {γ : Sort w} -- A constructive version of `bijective`. structure Is_equiv (f : α → β) := (e : α ≃ β) (h : f = e) instance (f : α → β) : subsingleton (Is_equiv f) := ⟨begin intros e e', cases e with ee eh, cases e' with e'e e'h, have : ee = e'e, from equiv.coe_fn_injective (by cc), cc end⟩ lemma Is_equiv.bijective {f : α → β} (e : Is_equiv f) : function.bijective f := by rw e.h; exact e.e.bijective lemma Is_equiv.cancel_left {f : α → β} (e : Is_equiv f) {a : α} : e.e.inv_fun (f a) = a := by rw [e.h] { occs := occurrences.pos [2] }; exact e.e.left_inv a lemma Is_equiv.cancel_right {f : α → β} (e : Is_equiv f) {b : β} : f (e.e.inv_fun b) = b := by rw [e.h] { occs := occurrences.pos [1] }; exact e.e.right_inv b
2b861c581537ffa0b598b02454363832a3c191f6
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/counterexamples/phillips.lean
faa9bc48063de70a3b195ef3fc111a935716d75e
[ "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
28,554
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 analysis.normed_space.hahn_banach import measure_theory.measure.lebesgue /-! # A counterexample on Pettis integrability There are several theories of integration for functions taking values in Banach spaces. Bochner integration, requiring approximation by simple functions, is the analogue of the one-dimensional theory. It is very well behaved, but only works for functions with second-countable range. For functions `f` taking values in a larger Banach space `B`, one can define the Dunford integral as follows. Assume that, for all continuous linear functional `φ`, the function `φ ∘ f` is measurable (we say that `f` is weakly measurable, or scalarly measurable) and integrable. Then `φ ↦ ∫ φ ∘ f` is continuous (by the closed graph theorem), and therefore defines an element of the bidual `B**`. This is the Dunford integral of `f`. This Dunford integral is not usable in practice as it does not belong to the right space. Let us say that a function is Pettis integrable if its Dunford integral belongs to the canonical image of `B` in `B**`. In this case, we define the Pettis integral as the Dunford integral inside `B`. This integral is very general, but not really usable to do analysis. This file illustrates this, by giving an example of a function with nice properties but which is *not* Pettis-integrable. This function: - is defined from `[0, 1]` to a complete Banach space; - is weakly measurable; - has norm everywhere bounded by `1` (in particular, its norm is integrable); - and yet it is not Pettis-integrable with respect to Lebesgue measure. This construction is due to [Ralph S. Phillips, *Integration in a convex linear topological space*][phillips1940], in Example 10.8. It requires the continuum hypothesis. The example is the following. Under the continuum hypothesis, one can find a subset of `ℝ²` which, along each vertical line, only misses a countable set of points, while it is countable along each horizontal line. This is due to Sierpinski, and formalized in `sierpinski_pathological_family`. (In fact, Sierpinski proves that the existence of such a set is equivalent to the continuum hypothesis). Let `B` be the set of all bounded functions on `ℝ` (we are really talking about everywhere defined functions here). Define `f : ℝ → B` by taking `f x` to be the characteristic function of the vertical slice at position `x` of Sierpinski's set. It is our counterexample. To show that it is weakly measurable, we should consider `φ ∘ f` where `φ` is an arbitrary continuous linear form on `B`. There is no reasonable classification of such linear forms (they can be very wild). But if one restricts such a linear form to characteristic functions, one gets a finitely additive signed "measure". Such a "measure" can be decomposed into a discrete part (supported on a countable set) and a continuous part (giving zero mass to countable sets). For all but countably many points, `f x` will not intersect the discrete support of `φ` thanks to the definition of the Sierpinski set. This implies that `φ ∘ f` is constant outside of a countable set, and equal to the total mass of the continuous part of `φ` there. In particular, it is measurable (and its integral is the total mass of the continuous part of `φ`). Assume that `f` has a Pettis integral `g`. For all continuous linear form `φ`, then `φ g` should be the total mass of the continuous part of `φ`. Taking for `φ` the evaluation at the point `x` (which has no continuous part), one gets `g x = 0`. Take then for `φ` the Lebesgue integral on `[0, 1]` (or rather an arbitrary extension of Lebesgue integration to all bounded functions, thanks to Hahn-Banach). Then `φ g` should be the total mass of the continuous part of `φ`, which is `1`. This contradicts the fact that `g = 0`, and concludes the proof that `f` has no Pettis integral. ## Implementation notes The space of all bounded functions is defined as the space of all bounded continuous functions on a discrete copy of the original type, as mathlib only contains the space of all bounded continuous functions (which is the useful one). -/ universe u variables {α : Type u} open set bounded_continuous_function measure_theory open cardinal (aleph) open_locale cardinal bounded_continuous_function noncomputable theory /-- A copy of a type, endowed with the discrete topology -/ def discrete_copy (α : Type u) : Type u := α instance : topological_space (discrete_copy α) := ⊥ instance : discrete_topology (discrete_copy α) := ⟨rfl⟩ instance [inhabited α] : inhabited (discrete_copy α) := ⟨id default α⟩ namespace phillips_1940 /-! ### Extending the integral Thanks to Hahn-Banach, one can define a (non-canonical) continuous linear functional on the space of all bounded functions, coinciding with the integral on the integrable ones. -/ /-- The subspace of integrable functions in the space of all bounded functions on a type. This is a technical device, used to apply Hahn-Banach theorem to construct an extension of the integral to all bounded functions. -/ def bounded_integrable_functions [measurable_space α] (μ : measure α) : subspace ℝ (discrete_copy α →ᵇ ℝ) := { carrier := {f | integrable f μ}, zero_mem' := integrable_zero _ _ _, add_mem' := λ f g hf hg, integrable.add hf hg, smul_mem' := λ c f hf, integrable.smul c hf } /-- The integral, as a continuous linear map on the subspace of integrable functions in the space of all bounded functions on a type. This is a technical device, that we will extend through Hahn-Banach. -/ def bounded_integrable_functions_integral_clm [measurable_space α] (μ : measure α) [is_finite_measure μ] : bounded_integrable_functions μ →L[ℝ] ℝ := linear_map.mk_continuous { to_fun := λ f, ∫ x, f x ∂μ, map_add' := λ f g, integral_add f.2 g.2, map_smul' := λ c f, integral_smul _ _ } (μ univ).to_real begin assume f, rw mul_comm, apply norm_integral_le_of_norm_le_const, apply filter.eventually_of_forall, assume x, exact bounded_continuous_function.norm_coe_le_norm f x, end /-- Given a measure, there exists a continuous linear form on the space of all bounded functions (not necessarily measurable) that coincides with the integral on bounded measurable functions. -/ lemma exists_linear_extension_to_bounded_functions [measurable_space α] (μ : measure α) [is_finite_measure μ] : ∃ φ : (discrete_copy α →ᵇ ℝ) →L[ℝ] ℝ, ∀ (f : discrete_copy α →ᵇ ℝ), integrable f μ → φ f = ∫ x, f x ∂μ := begin rcases exists_extension_norm_eq _ (bounded_integrable_functions_integral_clm μ) with ⟨φ, hφ⟩, exact ⟨φ, λ f hf, hφ.1 ⟨f, hf⟩⟩, end /-- An arbitrary extension of the integral to all bounded functions, as a continuous linear map. It is not at all canonical, and constructed using Hahn-Banach. -/ def _root_.measure_theory.measure.extension_to_bounded_functions [measurable_space α] (μ : measure α) [is_finite_measure μ] : (discrete_copy α →ᵇ ℝ) →L[ℝ] ℝ := (exists_linear_extension_to_bounded_functions μ).some lemma extension_to_bounded_functions_apply [measurable_space α] (μ : measure α) [is_finite_measure μ] (f : discrete_copy α →ᵇ ℝ) (hf : integrable f μ) : μ.extension_to_bounded_functions f = ∫ x, f x ∂μ := (exists_linear_extension_to_bounded_functions μ).some_spec f hf /-! ### Additive measures on the space of all sets We define bounded finitely additive signed measures on the space of all subsets of a type `α`, and show that such an object can be split into a discrete part and a continuous part. -/ /-- A bounded signed finitely additive measure defined on *all* subsets of a type. -/ structure bounded_additive_measure (α : Type u) := (to_fun : set α → ℝ) (additive' : ∀ s t, disjoint s t → to_fun (s ∪ t) = to_fun s + to_fun t) (exists_bound : ∃ (C : ℝ), ∀ s, |to_fun s| ≤ C) instance : inhabited (bounded_additive_measure α) := ⟨{ to_fun := λ s, 0, additive' := λ s t hst, by simp, exists_bound := ⟨0, λ s, by simp⟩ }⟩ instance : has_coe_to_fun (bounded_additive_measure α) (λ _, set α → ℝ) := ⟨λ f, f.to_fun⟩ namespace bounded_additive_measure /-- A constant bounding the mass of any set for `f`. -/ def C (f : bounded_additive_measure α) := f.exists_bound.some lemma additive (f : bounded_additive_measure α) (s t : set α) (h : disjoint s t) : f (s ∪ t) = f s + f t := f.additive' s t h lemma abs_le_bound (f : bounded_additive_measure α) (s : set α) : |f s| ≤ f.C := f.exists_bound.some_spec s lemma le_bound (f : bounded_additive_measure α) (s : set α) : f s ≤ f.C := le_trans (le_abs_self _) (f.abs_le_bound s) @[simp] lemma empty (f : bounded_additive_measure α) : f ∅ = 0 := begin have : (∅ : set α) = ∅ ∪ ∅, by simp only [empty_union], apply_fun f at this, rwa [f.additive _ _ (empty_disjoint _), self_eq_add_left] at this, end instance : has_neg (bounded_additive_measure α) := ⟨λ f, { to_fun := λ s, - f s, additive' := λ s t hst, by simp only [f.additive s t hst, add_comm, neg_add_rev], exists_bound := ⟨f.C, λ s, by simp [f.abs_le_bound]⟩ }⟩ @[simp] lemma neg_apply (f : bounded_additive_measure α) (s : set α) : (-f) s = - (f s) := rfl /-- Restricting a bounded additive measure to a subset still gives a bounded additive measure. -/ def restrict (f : bounded_additive_measure α) (t : set α) : bounded_additive_measure α := { to_fun := λ s, f (t ∩ s), additive' := λ s s' h, begin rw [← f.additive (t ∩ s) (t ∩ s'), inter_union_distrib_left], exact h.mono (inter_subset_right _ _) (inter_subset_right _ _), end, exists_bound := ⟨f.C, λ s, f.abs_le_bound _⟩ } @[simp] lemma restrict_apply (f : bounded_additive_measure α) (s t : set α) : f.restrict s t = f (s ∩ t) := rfl /-- There is a maximal countable set of positive measure, in the sense that any countable set not intersecting it has nonpositive measure. Auxiliary lemma to prove `exists_discrete_support`. -/ lemma exists_discrete_support_nonpos (f : bounded_additive_measure α) : ∃ (s : set α), countable s ∧ (∀ t, countable t → f (t \ s) ≤ 0) := begin /- The idea of the proof is to construct the desired set inductively, adding at each step a countable set with close to maximal measure among those points that have not already been chosen. Doing this countably many steps will be enough. Indeed, otherwise, a remaining set would have positive measure `ε`. This means that at each step the set we have added also had a large measure, say at least `ε / 2`. After `n` steps, the set we have constructed has therefore measure at least `n * ε / 2`. This is a contradiction since the measures have to remain uniformly bounded. We argue from the start by contradiction, as this means that our inductive construction will never be stuck, so we won't have to consider this case separately. -/ by_contra h, push_neg at h, -- We will formulate things in terms of the type of countable subsets of `α`, as this is more -- convenient to formalize the inductive construction. let A : set (set α) := {t | countable t}, let empty : A := ⟨∅, countable_empty⟩, haveI : nonempty A := ⟨empty⟩, -- given a countable set `s`, one can find a set `t` in its complement with measure close to -- maximal. have : ∀ (s : A), ∃ (t : A), (∀ (u : A), f (u \ s) ≤ 2 * f (t \ s)), { assume s, have B : bdd_above (range (λ (u : A), f (u \ s))), { refine ⟨f.C, λ x hx, _⟩, rcases hx with ⟨u, hu⟩, rw ← hu, exact f.le_bound _ }, let S := supr (λ (t : A), f (t \ s)), have S_pos : 0 < S, { rcases h s.1 s.2 with ⟨t, t_count, ht⟩, apply ht.trans_le, let t' : A := ⟨t, t_count⟩, change f (t' \ s) ≤ S, exact le_csupr B t' }, rcases exists_lt_of_lt_csupr (half_lt_self S_pos) with ⟨t, ht⟩, refine ⟨t, λ u, _⟩, calc f (u \ s) ≤ S : le_csupr B _ ... = 2 * (S / 2) : by ring ... ≤ 2 * f (t \ s) : mul_le_mul_of_nonneg_left ht.le (by norm_num) }, choose! F hF using this, -- iterate the above construction, by adding at each step a set with measure close to maximal in -- the complement of already chosen points. This is the set `s n` at step `n`. let G : A → A := λ u, ⟨u ∪ F u, u.2.union (F u).2⟩, let s : ℕ → A := λ n, G^[n] empty, -- We will get a contradiction from the fact that there is a countable set `u` with positive -- measure in the complement of `⋃ n, s n`. rcases h (⋃ n, s n) (countable_Union (λ n, (s n).2)) with ⟨t, t_count, ht⟩, let u : A := ⟨t \ ⋃ n, s n, t_count.mono (diff_subset _ _)⟩, set ε := f u with hε, have ε_pos : 0 < ε := ht, have I1 : ∀ n, ε / 2 ≤ f (s (n+1) \ s n), { assume n, rw [div_le_iff' (show (0 : ℝ) < 2, by norm_num), hε], convert hF (s n) u using 3, { dsimp [u], ext x, simp only [not_exists, mem_Union, mem_diff], tauto }, { simp only [s, function.iterate_succ', subtype.coe_mk, union_diff_left] } }, have I2 : ∀ (n : ℕ), (n : ℝ) * (ε / 2) ≤ f (s n), { assume n, induction n with n IH, { simp only [s, bounded_additive_measure.empty, id.def, nat.cast_zero, zero_mul, function.iterate_zero, subtype.coe_mk], }, { have : (s (n+1) : set α) = (s (n+1) \ s n) ∪ s n, by simp only [s, function.iterate_succ', union_comm, union_diff_self, subtype.coe_mk, union_diff_left], rw [nat.succ_eq_add_one, this, f.additive], swap, { rw disjoint.comm, apply disjoint_diff }, calc ((n + 1) : ℝ) * (ε / 2) = ε / 2 + n * (ε / 2) : by ring ... ≤ f ((s (n + 1)) \ (s n)) + f (s n) : add_le_add (I1 n) IH } }, rcases exists_nat_gt (f.C / (ε / 2)) with ⟨n, hn⟩, have : (n : ℝ) ≤ f.C / (ε / 2), by { rw le_div_iff (half_pos ε_pos), exact (I2 n).trans (f.le_bound _) }, exact lt_irrefl _ (this.trans_lt hn), end lemma exists_discrete_support (f : bounded_additive_measure α) : ∃ (s : set α), countable s ∧ (∀ t, countable t → f (t \ s) = 0) := begin rcases f.exists_discrete_support_nonpos with ⟨s₁, s₁_count, h₁⟩, rcases (-f).exists_discrete_support_nonpos with ⟨s₂, s₂_count, h₂⟩, refine ⟨s₁ ∪ s₂, s₁_count.union s₂_count, λ t ht, le_antisymm _ _⟩, { have : t \ (s₁ ∪ s₂) = (t \ (s₁ ∪ s₂)) \ s₁, by rw [diff_diff, union_comm, union_assoc, union_self], rw this, exact h₁ _ (ht.mono (diff_subset _ _)) }, { have : t \ (s₁ ∪ s₂) = (t \ (s₁ ∪ s₂)) \ s₂, by rw [diff_diff, union_assoc, union_self], rw this, simp only [neg_nonpos, neg_apply] at h₂, exact h₂ _ (ht.mono (diff_subset _ _)) }, end /-- A countable set outside of which the measure gives zero mass to countable sets. We are not claiming this set is unique, but we make an arbitrary choice of such a set. -/ def discrete_support (f : bounded_additive_measure α) : set α := (exists_discrete_support f).some lemma countable_discrete_support (f : bounded_additive_measure α) : countable f.discrete_support := (exists_discrete_support f).some_spec.1 lemma apply_countable (f : bounded_additive_measure α) (t : set α) (ht : countable t) : f (t \ f.discrete_support) = 0 := (exists_discrete_support f).some_spec.2 t ht /-- The discrete part of a bounded additive measure, obtained by restricting the measure to its countable support. -/ def discrete_part (f : bounded_additive_measure α) : bounded_additive_measure α := f.restrict f.discrete_support /-- The continuous part of a bounded additive measure, giving zero measure to every countable set. -/ def continuous_part (f : bounded_additive_measure α) : bounded_additive_measure α := f.restrict (univ \ f.discrete_support) lemma eq_add_parts (f : bounded_additive_measure α) (s : set α) : f s = f.discrete_part s + f.continuous_part s := begin simp only [discrete_part, continuous_part, restrict_apply], rw [← f.additive, ← inter_distrib_right], { simp only [union_univ, union_diff_self, univ_inter] }, { have : disjoint f.discrete_support (univ \ f.discrete_support) := disjoint_diff, exact this.mono (inter_subset_left _ _) (inter_subset_left _ _) } end lemma discrete_part_apply (f : bounded_additive_measure α) (s : set α) : f.discrete_part s = f (f.discrete_support ∩ s) := rfl lemma continuous_part_apply_eq_zero_of_countable (f : bounded_additive_measure α) (s : set α) (hs : countable s) : f.continuous_part s = 0 := begin simp [continuous_part], convert f.apply_countable s hs using 2, ext x, simp [and_comm] end lemma continuous_part_apply_diff (f : bounded_additive_measure α) (s t : set α) (hs : countable s) : f.continuous_part (t \ s) = f.continuous_part t := begin conv_rhs { rw ← diff_union_inter t s }, rw [additive, self_eq_add_right], { exact continuous_part_apply_eq_zero_of_countable _ _ (hs.mono (inter_subset_right _ _)) }, { exact disjoint.mono_right (inter_subset_right _ _) (disjoint.comm.1 disjoint_diff) }, end end bounded_additive_measure open bounded_additive_measure section /-! ### Relationship between continuous functionals and finitely additive measures. -/ lemma norm_indicator_le_one (s : set α) (x : α) : ∥(indicator s (1 : α → ℝ)) x∥ ≤ 1 := by { simp only [indicator, pi.one_apply], split_ifs; norm_num } /-- A functional in the dual space of bounded functions gives rise to a bounded additive measure, by applying the functional to the indicator functions. -/ def _root_.continuous_linear_map.to_bounded_additive_measure [topological_space α] [discrete_topology α] (f : (α →ᵇ ℝ) →L[ℝ] ℝ) : bounded_additive_measure α := { to_fun := λ s, f (of_normed_group_discrete (indicator s 1) 1 (norm_indicator_le_one s)), additive' := λ s t hst, begin have : of_normed_group_discrete (indicator (s ∪ t) 1) 1 (norm_indicator_le_one (s ∪ t)) = of_normed_group_discrete (indicator s 1) 1 (norm_indicator_le_one s) + of_normed_group_discrete (indicator t 1) 1 (norm_indicator_le_one t), by { ext x, simp [indicator_union_of_disjoint hst], }, rw [this, f.map_add], end, exists_bound := ⟨∥f∥, λ s, begin have I : ∥of_normed_group_discrete (indicator s 1) 1 (norm_indicator_le_one s)∥ ≤ 1, by apply norm_of_normed_group_le _ zero_le_one, apply le_trans (f.le_op_norm _), simpa using mul_le_mul_of_nonneg_left I (norm_nonneg f), end⟩ } @[simp] lemma continuous_part_eval_clm_eq_zero [topological_space α] [discrete_topology α] (s : set α) (x : α) : (eval_clm ℝ x).to_bounded_additive_measure.continuous_part s = 0 := let f := (eval_clm ℝ x).to_bounded_additive_measure in calc f.continuous_part s = f.continuous_part (s \ {x}) : (continuous_part_apply_diff _ _ _ (countable_singleton x)).symm ... = f ((univ \ f.discrete_support) ∩ (s \ {x})) : rfl ... = indicator ((univ \ f.discrete_support) ∩ (s \ {x})) 1 x : rfl ... = 0 : by simp lemma to_functions_to_measure [measurable_space α] (μ : measure α) [is_finite_measure μ] (s : set α) (hs : measurable_set s) : μ.extension_to_bounded_functions.to_bounded_additive_measure s = (μ s).to_real := begin change μ.extension_to_bounded_functions (of_normed_group_discrete (indicator s (λ x, 1)) 1 (norm_indicator_le_one s)) = (μ s).to_real, rw extension_to_bounded_functions_apply, { change ∫ x, s.indicator (λ y, (1 : ℝ)) x ∂μ = _, simp [integral_indicator hs] }, { change integrable (indicator s 1) μ, have : integrable (λ x, (1 : ℝ)) μ := integrable_const (1 : ℝ), apply this.mono' (measurable.indicator (@measurable_const _ _ _ _ (1 : ℝ)) hs).ae_measurable, apply filter.eventually_of_forall, exact norm_indicator_le_one _ } end lemma to_functions_to_measure_continuous_part [measurable_space α] [measurable_singleton_class α] (μ : measure α) [is_finite_measure μ] [has_no_atoms μ] (s : set α) (hs : measurable_set s) : μ.extension_to_bounded_functions.to_bounded_additive_measure.continuous_part s = (μ s).to_real := begin let f := μ.extension_to_bounded_functions.to_bounded_additive_measure, change f ((univ \ f.discrete_support) ∩ s) = (μ s).to_real, rw to_functions_to_measure, swap, { exact measurable_set.inter (measurable_set.univ.diff (countable.measurable_set f.countable_discrete_support)) hs }, congr' 1, rw [inter_comm, ← inter_diff_assoc, inter_univ], exact measure_diff_null (f.countable_discrete_support.measure_zero _) end end /-! ### A set in `ℝ²` large along verticals, small along horizontals We construct a subset of `ℝ²`, given as a family of sets, which is large along verticals (i.e., it only misses a countable set along each vertical) but small along horizontals (it is countable along horizontals). Such a set can not be measurable as it would contradict Fubini theorem. We need the continuum hypothesis to construct it. -/ theorem sierpinski_pathological_family (Hcont : #ℝ = aleph 1) : ∃ (f : ℝ → set ℝ), (∀ x, countable (univ \ f x)) ∧ (∀ y, countable {x | y ∈ f x}) := begin rcases cardinal.ord_eq ℝ with ⟨r, hr, H⟩, resetI, refine ⟨λ x, {y | r x y}, λ x, _, λ y, _⟩, { have : univ \ {y | r x y} = {y | r y x} ∪ {x}, { ext y, simp only [true_and, mem_univ, mem_set_of_eq, mem_insert_iff, union_singleton, mem_diff], rcases trichotomous_of r x y with h|rfl|h, { simp only [h, not_or_distrib, false_iff, not_true], split, { rintros rfl, exact irrefl_of r y h }, { exact asymm h } }, { simp only [true_or, eq_self_iff_true, iff_true], exact irrefl x }, { simp only [h, iff_true, or_true], exact asymm h } }, rw this, apply countable.union _ (countable_singleton _), rw [cardinal.countable_iff_lt_aleph_one, ← Hcont], exact cardinal.card_typein_lt r x H }, { rw [cardinal.countable_iff_lt_aleph_one, ← Hcont], exact cardinal.card_typein_lt r y H } end /-- A family of sets in `ℝ` which only miss countably many points, but such that any point is contained in only countably many of them. -/ def spf (Hcont : #ℝ = aleph 1) (x : ℝ) : set ℝ := (sierpinski_pathological_family Hcont).some x lemma countable_compl_spf (Hcont : #ℝ = aleph 1) (x : ℝ) : countable (univ \ spf Hcont x) := (sierpinski_pathological_family Hcont).some_spec.1 x lemma countable_spf_mem (Hcont : #ℝ = aleph 1) (y : ℝ) : countable {x | y ∈ spf Hcont x} := (sierpinski_pathological_family Hcont).some_spec.2 y /-! ### A counterexample for the Pettis integral We construct a function `f` from `[0,1]` to a complete Banach space `B`, which is weakly measurable (i.e., for any continuous linear form `φ` on `B` the function `φ ∘ f` is measurable), bounded in norm (i.e., for all `x`, one has `∥f x∥ ≤ 1`), and still `f` has no Pettis integral. This construction, due to Phillips, requires the continuum hypothesis. We will take for `B` the space of all bounded functions on `ℝ`, with the supremum norm (no measure here, we are really talking of everywhere defined functions). And `f x` will be the characteristic function of a set which is large (it has countable complement), as in the Sierpinski pathological family. -/ /-- A family of bounded functions `f_x` from `ℝ` (seen with the discrete topology) to `ℝ` (in fact taking values in `{0, 1}`), indexed by a real parameter `x`, corresponding to the characteristic functions of the different fibers of the Sierpinski pathological family -/ def f (Hcont : #ℝ = aleph 1) (x : ℝ) : (discrete_copy ℝ →ᵇ ℝ) := of_normed_group_discrete (indicator (spf Hcont x) 1) 1 (norm_indicator_le_one _) lemma apply_f_eq_continuous_part (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) (x : ℝ) (hx : φ.to_bounded_additive_measure.discrete_support ∩ spf Hcont x = ∅) : φ (f Hcont x) = φ.to_bounded_additive_measure.continuous_part univ := begin set ψ := φ.to_bounded_additive_measure with hψ, have : φ (f Hcont x) = ψ (spf Hcont x) := rfl, have U : univ = spf Hcont x ∪ (univ \ spf Hcont x), by simp only [union_univ, union_diff_self], rw [this, eq_add_parts, discrete_part_apply, hx, ψ.empty, zero_add, U, ψ.continuous_part.additive _ _ (disjoint_diff), ψ.continuous_part_apply_eq_zero_of_countable _ (countable_compl_spf Hcont x), add_zero], end lemma countable_ne (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : countable {x | φ.to_bounded_additive_measure.continuous_part univ ≠ φ (f Hcont x)} := begin have A : {x | φ.to_bounded_additive_measure.continuous_part univ ≠ φ (f Hcont x)} ⊆ {x | φ.to_bounded_additive_measure.discrete_support ∩ spf Hcont x ≠ ∅}, { assume x hx, contrapose! hx, simp only [not_not, mem_set_of_eq] at hx, simp [apply_f_eq_continuous_part Hcont φ x hx], }, have B : {x | φ.to_bounded_additive_measure.discrete_support ∩ spf Hcont x ≠ ∅} ⊆ ⋃ y ∈ φ.to_bounded_additive_measure.discrete_support, {x | y ∈ spf Hcont x}, { assume x hx, dsimp at hx, rw [← ne.def, ne_empty_iff_nonempty] at hx, simp only [exists_prop, mem_Union, mem_set_of_eq], exact hx }, apply countable.mono (subset.trans A B), exact countable.bUnion (countable_discrete_support _) (λ a ha, countable_spf_mem Hcont a), end lemma comp_ae_eq_const (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : ∀ᵐ x ∂(volume.restrict (Icc (0 : ℝ) 1)), φ.to_bounded_additive_measure.continuous_part univ = φ (f Hcont x) := begin apply ae_restrict_of_ae, refine measure_mono_null _ ((countable_ne Hcont φ).measure_zero _), assume x, simp only [imp_self, mem_set_of_eq, mem_compl_eq], end lemma integrable_comp (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : integrable_on (λ x, φ (f Hcont x)) (Icc 0 1) := begin have : integrable_on (λ x, φ.to_bounded_additive_measure.continuous_part univ) (Icc (0 : ℝ) 1) volume, by simp [integrable_on_const], apply integrable.congr this (comp_ae_eq_const Hcont φ), end lemma integral_comp (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : ∫ x in Icc 0 1, φ (f Hcont x) = φ.to_bounded_additive_measure.continuous_part univ := begin rw ← integral_congr_ae (comp_ae_eq_const Hcont φ), simp, end /-! The next few statements show that the function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` takes its values in a complete space, is scalarly measurable, is everywhere bounded by `1`, and still has no Pettis integral. -/ example : complete_space (discrete_copy ℝ →ᵇ ℝ) := by apply_instance /-- The function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` is scalarly measurable. -/ lemma measurable_comp (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : measurable (λ x, φ (f Hcont x)) := begin have : measurable (λ x, φ.to_bounded_additive_measure.continuous_part univ) := measurable_const, refine this.measurable_of_countable_ne _, exact countable_ne Hcont φ, end /-- The function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` is uniformly bounded by `1` in norm. -/ lemma norm_bound (Hcont : #ℝ = aleph 1) (x : ℝ) : ∥f Hcont x∥ ≤ 1 := norm_of_normed_group_le _ zero_le_one _ /-- The function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` has no Pettis integral. -/ theorem no_pettis_integral (Hcont : #ℝ = aleph 1) : ¬ ∃ (g : discrete_copy ℝ →ᵇ ℝ), ∀ (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ), ∫ x in Icc 0 1, φ (f Hcont x) = φ g := begin rintros ⟨g, h⟩, simp only [integral_comp] at h, have : g = 0, { ext x, have : g x = eval_clm ℝ x g := rfl, rw [this, ← h], simp }, simp only [this, continuous_linear_map.map_zero] at h, specialize h (volume.restrict (Icc (0 : ℝ) 1)).extension_to_bounded_functions, simp_rw [to_functions_to_measure_continuous_part _ _ measurable_set.univ] at h, simpa using h, end end phillips_1940
d331aa381045c4c7036bddd9e6a3ead711010781
b9d8165d695e844c92d9d4cdcac7b5ab9efe09f7
/src/analysis/calculus/deriv.lean
3459a583e03514d9403b61c2ed4a0acb6b55b9ca
[ "Apache-2.0" ]
permissive
spapinistarkware/mathlib
e917d9c44bf85ef51db18e7a11615959f714efc5
0a9a1ff463a1f26e27d7c391eb7f6334f0d90383
refs/heads/master
1,606,808,129,547
1,577,478,369,000
1,577,478,369,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
38,099
lean
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Sébastien Gouëzel -/ import analysis.calculus.fderiv /-! # One-dimensional derivatives This file defines the derivative of a function `f : 𝕜 → F` where `𝕜` is a normed field and `F` is a normed space over this field. The derivative of such a function `f` at a point `x` is given by an element `f' : F`. The theory is developed analogously to the [Fréchet derivatives](./fderiv.lean). We first introduce predicates defined in terms of the corresponding predicates for Fréchet derivatives: - `has_deriv_at_filter f f' x L` states that the function `f` has the derivative `f'` at the point `x` as `x` goes along the filter `L`. - `has_deriv_within_at f f' s x` states that the function `f` has the derivative `f'` at the point `x` within the subset `s`. - `has_deriv_at f f' x` states that the function `f` has the derivative `f'` at the point `x`. For the last two notions we also define a functional version: - `deriv_within f s x` is a derivative of `f` at `x` within `s`. If the derivative does not exist, then `deriv_within f s x` equals zero. - `deriv f x` is a derivative of `f` at `x`. If the derivative does not exist, then `deriv f x` equals zero. The theorems `fderiv_within_deriv_within` and `fderiv_deriv` show that the one-dimensional derivatives coincide with the general Fréchet derivatives. We also show the existence and compute the derivatives of: - constants - the identity function - linear maps - addition - negation - subtraction - multiplication - inverse `x → x⁻¹` - multiplication of two functions in `𝕜 → 𝕜` - multiplication of a function in `𝕜 → 𝕜` and of a function in `𝕜 → E` - composition of a function in `𝕜 → F` with a function in `𝕜 → 𝕜` - composition of a function in `F → E` with a function in `𝕜 → F` - division - polynomials ## Implementation notes Most of the theorems are direct restatements of the corresponding theorems for Fréchet derivatives. -/ universes u v w noncomputable theory open_locale classical topological_space open filter asymptotics set open continuous_linear_map (smul_right smul_right_one_eq_iff) set_option class.instance_max_depth 100 variables {𝕜 : Type u} [nondiscrete_normed_field 𝕜] variables {F : Type v} [normed_group F] [normed_space 𝕜 F] variables {E : Type w} [normed_group E] [normed_space 𝕜 E] /-- `f` has the derivative `f'` at the point `x` as `x` goes along the filter `L`. That is, `f x' = f x + (x' - x) • f' + o(x' - x)` where `x'` converges along the filter `L`. -/ def has_deriv_at_filter (f : 𝕜 → F) (f' : F) (x : 𝕜) (L : filter 𝕜) := has_fderiv_at_filter f (smul_right 1 f' : 𝕜 →L[𝕜] F) x L /-- `f` has the derivative `f'` at the point `x` within the subset `s`. That is, `f x' = f x + (x' - x) • f' + o(x' - x)` where `x'` converges to `x` inside `s`. -/ def has_deriv_within_at (f : 𝕜 → F) (f' : F) (s : set 𝕜) (x : 𝕜) := has_deriv_at_filter f f' x (nhds_within x s) /-- `f` has the derivative `f'` at the point `x`. That is, `f x' = f x + (x' - x) • f' + o(x' - x)` where `x'` converges to `x`. -/ def has_deriv_at (f : 𝕜 → F) (f' : F) (x : 𝕜) := has_deriv_at_filter f f' x (𝓝 x) /-- Derivative of `f` at the point `x` within the set `s`, if it exists. Zero otherwise. If the derivative exists (i.e., `∃ f', has_deriv_within_at f f' s x`), then `f x' = f x + (x' - x) • deriv_within f s x + o(x' - x)` where `x'` converges to `x` inside `s`. -/ def deriv_within (f : 𝕜 → F) (s : set 𝕜) (x : 𝕜) := (fderiv_within 𝕜 f s x : 𝕜 →L[𝕜] F) 1 /-- Derivative of `f` at the point `x`, if it exists. Zero otherwise. If the derivative exists (i.e., `∃ f', has_deriv_at f f' x`), then `f x' = f x + (x' - x) • deriv f x + o(x' - x)` where `x'` converges to `x`. -/ def deriv (f : 𝕜 → F) (x : 𝕜) := (fderiv 𝕜 f x : 𝕜 →L[𝕜] F) 1 variables {f f₀ f₁ g : 𝕜 → F} variables {f' f₀' f₁' g' : F} variables {x : 𝕜} variables {s t : set 𝕜} variables {L L₁ L₂ : filter 𝕜} /-- Expressing `has_fderiv_at_filter f f' x L` in terms of `has_deriv_at_filter` -/ lemma has_fderiv_at_filter_iff_has_deriv_at_filter {f' : 𝕜 →L[𝕜] F} : has_fderiv_at_filter f f' x L ↔ has_deriv_at_filter f (f' 1) x L := by simp [has_deriv_at_filter] /-- Expressing `has_fderiv_within_at f f' s x` in terms of `has_deriv_within_at` -/ lemma has_fderiv_within_at_iff_has_deriv_within_at {f' : 𝕜 →L[𝕜] F} : has_fderiv_within_at f f' s x ↔ has_deriv_within_at f (f' 1) s x := by simp [has_deriv_within_at, has_deriv_at_filter, has_fderiv_within_at] /-- Expressing `has_deriv_within_at f f' s x` in terms of `has_fderiv_within_at` -/ lemma has_deriv_within_at_iff_has_fderiv_within_at {f' : F} : has_deriv_within_at f f' s x ↔ has_fderiv_within_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) s x := iff.rfl /-- Expressing `has_fderiv_at f f' x` in terms of `has_deriv_at` -/ lemma has_fderiv_at_iff_has_deriv_at {f' : 𝕜 →L[𝕜] F} : has_fderiv_at f f' x ↔ has_deriv_at f (f' 1) x := by simp [has_deriv_at, has_deriv_at_filter, has_fderiv_at] /-- Expressing `has_deriv_at f f' x` in terms of `has_fderiv_at` -/ lemma has_deriv_at_iff_has_fderiv_at {f' : F} : has_deriv_at f f' x ↔ has_fderiv_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) x := iff.rfl lemma deriv_within_zero_of_not_differentiable_within_at (h : ¬ differentiable_within_at 𝕜 f s x) : deriv_within f s x = 0 := by { unfold deriv_within, rw fderiv_within_zero_of_not_differentiable_within_at, simp, assumption } lemma deriv_zero_of_not_differentiable_at (h : ¬ differentiable_at 𝕜 f x) : deriv f x = 0 := by { unfold deriv, rw fderiv_zero_of_not_differentiable_at, simp, assumption } theorem unique_diff_within_at.eq_deriv (s : set 𝕜) (H : unique_diff_within_at 𝕜 s x) (h : has_deriv_within_at f f' s x) (h₁ : has_deriv_within_at f f₁' s x) : f' = f₁' := smul_right_one_eq_iff.mp $ unique_diff_within_at.eq H h h₁ theorem has_deriv_at_filter_iff_tendsto : has_deriv_at_filter f f' x L ↔ tendsto (λ x' : 𝕜, ∥x' - x∥⁻¹ * ∥f x' - f x - (x' - x) • f'∥) L (𝓝 0) := has_fderiv_at_filter_iff_tendsto theorem has_deriv_within_at_iff_tendsto : has_deriv_within_at f f' s x ↔ tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - (x' - x) • f'∥) (nhds_within x s) (𝓝 0) := has_fderiv_at_filter_iff_tendsto theorem has_deriv_at_iff_tendsto : has_deriv_at f f' x ↔ tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - (x' - x) • f'∥) (𝓝 x) (𝓝 0) := has_fderiv_at_filter_iff_tendsto theorem has_deriv_at_iff_is_o_nhds_zero : has_deriv_at f f' x ↔ is_o (λh, f (x + h) - f x - h • f') (λh, h) (𝓝 0) := has_fderiv_at_iff_is_o_nhds_zero theorem has_deriv_at_filter.mono (h : has_deriv_at_filter f f' x L₂) (hst : L₁ ≤ L₂) : has_deriv_at_filter f f' x L₁ := has_fderiv_at_filter.mono h hst theorem has_deriv_within_at.mono (h : has_deriv_within_at f f' t x) (hst : s ⊆ t) : has_deriv_within_at f f' s x := has_fderiv_within_at.mono h hst theorem has_deriv_at.has_deriv_at_filter (h : has_deriv_at f f' x) (hL : L ≤ 𝓝 x) : has_deriv_at_filter f f' x L := has_fderiv_at.has_fderiv_at_filter h hL theorem has_deriv_at.has_deriv_within_at (h : has_deriv_at f f' x) : has_deriv_within_at f f' s x := has_fderiv_at.has_fderiv_within_at h lemma has_deriv_within_at.differentiable_within_at (h : has_deriv_within_at f f' s x) : differentiable_within_at 𝕜 f s x := has_fderiv_within_at.differentiable_within_at h lemma has_deriv_at.differentiable_at (h : has_deriv_at f f' x) : differentiable_at 𝕜 f x := has_fderiv_at.differentiable_at h @[simp] lemma has_deriv_within_at_univ : has_deriv_within_at f f' univ x ↔ has_deriv_at f f' x := has_fderiv_within_at_univ theorem has_deriv_at_unique (h₀ : has_deriv_at f f₀' x) (h₁ : has_deriv_at f f₁' x) : f₀' = f₁' := smul_right_one_eq_iff.mp $ has_fderiv_at_unique h₀ h₁ lemma has_deriv_within_at_inter' (h : t ∈ nhds_within x s) : has_deriv_within_at f f' (s ∩ t) x ↔ has_deriv_within_at f f' s x := has_fderiv_within_at_inter' h lemma has_deriv_within_at_inter (h : t ∈ 𝓝 x) : has_deriv_within_at f f' (s ∩ t) x ↔ has_deriv_within_at f f' s x := has_fderiv_within_at_inter h lemma has_deriv_within_at.union (hs : has_deriv_within_at f f' s x) (ht : has_deriv_within_at f f' t x) : has_deriv_within_at f f' (s ∪ t) x := begin simp only [has_deriv_within_at, nhds_within_union], exact is_o_join hs ht, end lemma has_deriv_within_at.nhds_within (h : has_deriv_within_at f f' s x) (ht : s ∈ nhds_within x t) : has_deriv_within_at f f' t x := (has_deriv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _)) lemma has_deriv_within_at.has_deriv_at (h : has_deriv_within_at f f' s x) (hs : s ∈ 𝓝 x) : has_deriv_at f f' x := has_fderiv_within_at.has_fderiv_at h hs lemma differentiable_within_at.has_deriv_within_at (h : differentiable_within_at 𝕜 f s x) : has_deriv_within_at f (deriv_within f s x) s x := show has_fderiv_within_at _ _ _ _, by { convert h.has_fderiv_within_at, simp [deriv_within] } lemma differentiable_at.has_deriv_at (h : differentiable_at 𝕜 f x) : has_deriv_at f (deriv f x) x := show has_fderiv_at _ _ _, by { convert h.has_fderiv_at, simp [deriv] } lemma has_deriv_at.deriv (h : has_deriv_at f f' x) : deriv f x = f' := has_deriv_at_unique h.differentiable_at.has_deriv_at h lemma has_deriv_within_at.deriv_within (h : has_deriv_within_at f f' s x) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within f s x = f' := hxs.eq_deriv _ h.differentiable_within_at.has_deriv_within_at h lemma fderiv_within_deriv_within : (fderiv_within 𝕜 f s x : 𝕜 → F) 1 = deriv_within f s x := rfl lemma deriv_within_fderiv_within : smul_right 1 (deriv_within f s x) = fderiv_within 𝕜 f s x := by simp [deriv_within] lemma fderiv_deriv : (fderiv 𝕜 f x : 𝕜 → F) 1 = deriv f x := rfl lemma deriv_fderiv : smul_right 1 (deriv f x) = fderiv 𝕜 f x := by simp [deriv] lemma differentiable_at.deriv_within (h : differentiable_at 𝕜 f x) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within f s x = deriv f x := by { unfold deriv_within deriv, rw h.fderiv_within hxs } lemma deriv_within_subset (st : s ⊆ t) (ht : unique_diff_within_at 𝕜 s x) (h : differentiable_within_at 𝕜 f t x) : deriv_within f s x = deriv_within f t x := ((differentiable_within_at.has_deriv_within_at h).mono st).deriv_within ht @[simp] lemma deriv_within_univ : deriv_within f univ = deriv f := by { ext, unfold deriv_within deriv, rw fderiv_within_univ } lemma deriv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_diff_within_at 𝕜 s x) : deriv_within f (s ∩ t) x = deriv_within f s x := by { unfold deriv_within, rw fderiv_within_inter ht hs } section congr /-! ### Congruence properties of derivatives -/ theorem has_deriv_at_filter_congr_of_mem_sets (hx : f₀ x = f₁ x) (h₀ : {x | f₀ x = f₁ x} ∈ L) (h₁ : f₀' = f₁') : has_deriv_at_filter f₀ f₀' x L ↔ has_deriv_at_filter f₁ f₁' x L := has_fderiv_at_filter_congr_of_mem_sets hx h₀ (by simp [h₁]) lemma has_deriv_at_filter.congr_of_mem_sets (h : has_deriv_at_filter f f' x L) (hL : {x | f₁ x = f x} ∈ L) (hx : f₁ x = f x) : has_deriv_at_filter f₁ f' x L := by rwa has_deriv_at_filter_congr_of_mem_sets hx hL rfl lemma has_deriv_within_at.congr_mono (h : has_deriv_within_at f f' s x) (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : has_deriv_within_at f₁ f' t x := has_fderiv_within_at.congr_mono h ht hx h₁ lemma has_deriv_within_at.congr (h : has_deriv_within_at f f' s x) (hs : ∀x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : has_deriv_within_at f₁ f' s x := h.congr_mono hs hx (subset.refl _) lemma has_deriv_within_at.congr_of_mem_nhds_within (h : has_deriv_within_at f f' s x) (h₁ : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) : has_deriv_within_at f₁ f' s x := has_deriv_at_filter.congr_of_mem_sets h h₁ hx lemma has_deriv_at.congr_of_mem_nhds (h : has_deriv_at f f' x) (h₁ : {y | f₁ y = f y} ∈ 𝓝 x) : has_deriv_at f₁ f' x := has_deriv_at_filter.congr_of_mem_sets h h₁ (mem_of_nhds h₁ : _) lemma deriv_within_congr_of_mem_nhds_within (hs : unique_diff_within_at 𝕜 s x) (hL : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) : deriv_within f₁ s x = deriv_within f s x := by { unfold deriv_within, rw fderiv_within_congr_of_mem_nhds_within hs hL hx } lemma deriv_within_congr (hs : unique_diff_within_at 𝕜 s x) (hL : ∀y∈s, f₁ y = f y) (hx : f₁ x = f x) : deriv_within f₁ s x = deriv_within f s x := by { unfold deriv_within, rw fderiv_within_congr hs hL hx } lemma deriv_congr_of_mem_nhds (hL : {y | f₁ y = f y} ∈ 𝓝 x) : deriv f₁ x = deriv f x := by { unfold deriv, rwa fderiv_congr_of_mem_nhds } end congr section id /-! ### Derivative of the identity -/ variables (s x L) theorem has_deriv_at_filter_id : has_deriv_at_filter id 1 x L := (is_o_zero _ _).congr_left $ by simp theorem has_deriv_within_at_id : has_deriv_within_at id 1 s x := has_deriv_at_filter_id _ _ theorem has_deriv_at_id : has_deriv_at id 1 x := has_deriv_at_filter_id _ _ lemma deriv_id : deriv id x = 1 := has_deriv_at.deriv (has_deriv_at_id x) @[simp] lemma deriv_id' : deriv (@id 𝕜) = λ _, 1 := funext deriv_id lemma deriv_within_id (hxs : unique_diff_within_at 𝕜 s x) : deriv_within id s x = 1 := by { unfold deriv_within, rw fderiv_within_id, simp, assumption } end id section const /-! ### Derivative of constant functions -/ variables (c : F) (s x L) theorem has_deriv_at_filter_const : has_deriv_at_filter (λ x, c) 0 x L := (is_o_zero _ _).congr_left $ λ _, by simp [continuous_linear_map.zero_apply, sub_self] theorem has_deriv_within_at_const : has_deriv_within_at (λ x, c) 0 s x := has_deriv_at_filter_const _ _ _ theorem has_deriv_at_const : has_deriv_at (λ x, c) 0 x := has_deriv_at_filter_const _ _ _ lemma deriv_const : deriv (λ x, c) x = 0 := has_deriv_at.deriv (has_deriv_at_const x c) @[simp] lemma deriv_const' : deriv (λ x:𝕜, c) = λ x, 0 := funext (λ x, deriv_const x c) lemma deriv_within_const (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λ x, c) s x = 0 := by { rw (differentiable_at_const _).deriv_within hxs, apply deriv_const } end const section is_linear_map /-! ### Derivative of linear maps -/ variables (s x L) [is_linear_map 𝕜 f] lemma is_linear_map.has_deriv_at_filter : has_deriv_at_filter f (f 1) x L := (is_o_zero _ _).congr_left begin intro y, simp [add_smul], rw ← is_linear_map.smul f x, rw ← is_linear_map.smul f y, simp end lemma is_linear_map.has_deriv_within_at : has_deriv_within_at f (f 1) s x := is_linear_map.has_deriv_at_filter _ _ lemma is_linear_map.has_deriv_at : has_deriv_at f (f 1) x := is_linear_map.has_deriv_at_filter _ _ lemma is_linear_map.differentiable_at : differentiable_at 𝕜 f x := (is_linear_map.has_deriv_at _).differentiable_at lemma is_linear_map.differentiable_within_at : differentiable_within_at 𝕜 f s x := (is_linear_map.differentiable_at _).differentiable_within_at @[simp] lemma is_linear_map.deriv : deriv f x = f 1 := has_deriv_at.deriv (is_linear_map.has_deriv_at _) lemma is_linear_map.deriv_within (hxs : unique_diff_within_at 𝕜 s x) : deriv_within f s x = f 1 := begin rw differentiable_at.deriv_within (is_linear_map.differentiable_at _) hxs, apply is_linear_map.deriv, assumption end lemma is_linear_map.differentiable : differentiable 𝕜 f := λ x, is_linear_map.differentiable_at _ lemma is_linear_map.differentiable_on : differentiable_on 𝕜 f s := is_linear_map.differentiable.differentiable_on end is_linear_map section add /-! ### Derivative of the sum of two functions -/ theorem has_deriv_at_filter.add (hf : has_deriv_at_filter f f' x L) (hg : has_deriv_at_filter g g' x L) : has_deriv_at_filter (λ y, f y + g y) (f' + g') x L := (hf.add hg).congr_left $ by simp [add_smul, smul_add] theorem has_deriv_within_at.add (hf : has_deriv_within_at f f' s x) (hg : has_deriv_within_at g g' s x) : has_deriv_within_at (λ y, f y + g y) (f' + g') s x := hf.add hg theorem has_deriv_at.add (hf : has_deriv_at f f' x) (hg : has_deriv_at g g' x) : has_deriv_at (λ x, f x + g x) (f' + g') x := hf.add hg lemma deriv_within_add (hxs : unique_diff_within_at 𝕜 s x) (hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) : deriv_within (λy, f y + g y) s x = deriv_within f s x + deriv_within g s x := (hf.has_deriv_within_at.add hg.has_deriv_within_at).deriv_within hxs lemma deriv_add (hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) : deriv (λy, f y + g y) x = deriv f x + deriv g x := (hf.has_deriv_at.add hg.has_deriv_at).deriv end add section neg /-! ### Derivative of the negative of a function -/ theorem has_deriv_at_filter.neg (h : has_deriv_at_filter f f' x L) : has_deriv_at_filter (λ x, -f x) (-f') x L := (h.smul (-1)).congr (by simp) (by simp) theorem has_deriv_within_at.neg (h : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, -f x) (-f') s x := h.neg theorem has_deriv_at.neg (h : has_deriv_at f f' x) : has_deriv_at (λ x, -f x) (-f') x := h.neg lemma deriv_within_neg (hxs : unique_diff_within_at 𝕜 s x) (h : differentiable_within_at 𝕜 f s x) : deriv_within (λy, -f y) s x = - deriv_within f s x := h.has_deriv_within_at.neg.deriv_within hxs lemma deriv_neg : deriv (λy, -f y) x = - deriv f x := if h : differentiable_at 𝕜 f x then h.has_deriv_at.neg.deriv else have ¬differentiable_at 𝕜 (λ y, -f y) x, from λ h', by simpa only [neg_neg] using h'.neg, by simp only [deriv_zero_of_not_differentiable_at h, deriv_zero_of_not_differentiable_at this, neg_zero] @[simp] lemma deriv_neg' : deriv (λy, -f y) = (λ x, - deriv f x) := funext $ λ x, deriv_neg end neg section sub /-! ### Derivative of the difference of two functions -/ theorem has_deriv_at_filter.sub (hf : has_deriv_at_filter f f' x L) (hg : has_deriv_at_filter g g' x L) : has_deriv_at_filter (λ x, f x - g x) (f' - g') x L := hf.add hg.neg theorem has_deriv_within_at.sub (hf : has_deriv_within_at f f' s x) (hg : has_deriv_within_at g g' s x) : has_deriv_within_at (λ x, f x - g x) (f' - g') s x := hf.sub hg theorem has_deriv_at.sub (hf : has_deriv_at f f' x) (hg : has_deriv_at g g' x) : has_deriv_at (λ x, f x - g x) (f' - g') x := hf.sub hg lemma deriv_within_sub (hxs : unique_diff_within_at 𝕜 s x) (hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) : deriv_within (λy, f y - g y) s x = deriv_within f s x - deriv_within g s x := (hf.has_deriv_within_at.sub hg.has_deriv_within_at).deriv_within hxs lemma deriv_sub (hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) : deriv (λ y, f y - g y) x = deriv f x - deriv g x := (hf.has_deriv_at.sub hg.has_deriv_at).deriv theorem has_deriv_at_filter.is_O_sub (h : has_deriv_at_filter f f' x L) : is_O (λ x', f x' - f x) (λ x', x' - x) L := has_fderiv_at_filter.is_O_sub h end sub section continuous /-! ### Continuity of a function admitting a derivative -/ theorem has_deriv_at_filter.tendsto_nhds (hL : L ≤ 𝓝 x) (h : has_deriv_at_filter f f' x L) : tendsto f L (𝓝 (f x)) := has_fderiv_at_filter.tendsto_nhds hL h theorem has_deriv_within_at.continuous_within_at (h : has_deriv_within_at f f' s x) : continuous_within_at f s x := has_deriv_at_filter.tendsto_nhds lattice.inf_le_left h theorem has_deriv_at.continuous_at (h : has_deriv_at f f' x) : continuous_at f x := has_deriv_at_filter.tendsto_nhds (le_refl _) h end continuous section cartesian_product /-! ### Derivative of the cartesian product of two functions -/ variables {G : Type w} [normed_group G] [normed_space 𝕜 G] variables {f₂ : 𝕜 → G} {f₂' : G} lemma has_deriv_at_filter.prod (hf₁ : has_deriv_at_filter f₁ f₁' x L) (hf₂ : has_deriv_at_filter f₂ f₂' x L) : has_deriv_at_filter (λ x, (f₁ x, f₂ x)) (f₁', f₂') x L := show has_fderiv_at_filter _ _ _ _, by convert has_fderiv_at_filter.prod hf₁ hf₂ lemma has_deriv_within_at.prod (hf₁ : has_deriv_within_at f₁ f₁' s x) (hf₂ : has_deriv_within_at f₂ f₂' s x) : has_deriv_within_at (λ x, (f₁ x, f₂ x)) (f₁', f₂') s x := hf₁.prod hf₂ lemma has_deriv_at.prod (hf₁ : has_deriv_at f₁ f₁' x) (hf₂ : has_deriv_at f₂ f₂' x) : has_deriv_at (λ x, (f₁ x, f₂ x)) (f₁', f₂') x := hf₁.prod hf₂ end cartesian_product section composition /-! ### Derivative of the composition of a vector valued function and a scalar function -/ variables {h : 𝕜 → 𝕜} {h' : 𝕜} /- For composition lemmas, we put x explicit to help the elaborator, as otherwise Lean tends to get confused since there are too many possibilities for composition -/ variable (x) theorem has_deriv_at_filter.comp (hg : has_deriv_at_filter g g' (h x) (L.map h)) (hh : has_deriv_at_filter h h' x L) : has_deriv_at_filter (g ∘ h) (h' • g') x L := have (smul_right 1 g' : 𝕜 →L[𝕜] _).comp (smul_right 1 h' : 𝕜 →L[𝕜] _) = smul_right 1 (h' • g'), by { ext, simp [mul_smul] }, begin unfold has_deriv_at_filter, rw ← this, exact has_fderiv_at_filter.comp x hg hh, end theorem has_deriv_within_at.comp {t : set 𝕜} (hg : has_deriv_within_at g g' t (h x)) (hh : has_deriv_within_at h h' s x) (hst : s ⊆ h ⁻¹' t) : has_deriv_within_at (g ∘ h) (h' • g') s x := begin apply has_deriv_at_filter.comp _ (has_deriv_at_filter.mono hg _) hh, calc map h (nhds_within x s) ≤ nhds_within (h x) (h '' s) : hh.continuous_within_at.tendsto_nhds_within_image ... ≤ nhds_within (h x) t : nhds_within_mono _ (image_subset_iff.mpr hst) end /-- The chain rule. -/ theorem has_deriv_at.comp (hg : has_deriv_at g g' (h x)) (hh : has_deriv_at h h' x) : has_deriv_at (g ∘ h) (h' • g') x := (hg.mono hh.continuous_at).comp x hh theorem has_deriv_at.comp_has_deriv_within_at (hg : has_deriv_at g g' (h x)) (hh : has_deriv_within_at h h' s x) : has_deriv_within_at (g ∘ h) (h' • g') s x := begin rw ← has_deriv_within_at_univ at hg, exact has_deriv_within_at.comp x hg hh subset_preimage_univ end lemma deriv_within.comp (hg : differentiable_within_at 𝕜 g t (h x)) (hh : differentiable_within_at 𝕜 h s x) (hs : s ⊆ h ⁻¹' t) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (g ∘ h) s x = deriv_within h s x • deriv_within g t (h x) := begin apply has_deriv_within_at.deriv_within _ hxs, exact has_deriv_within_at.comp x (hg.has_deriv_within_at) (hh.has_deriv_within_at) hs end lemma deriv.comp (hg : differentiable_at 𝕜 g (h x)) (hh : differentiable_at 𝕜 h x) : deriv (g ∘ h) x = deriv h x • deriv g (h x) := begin apply has_deriv_at.deriv, exact has_deriv_at.comp x hg.has_deriv_at hh.has_deriv_at end end composition section composition_vector /-! ### Derivative of the composition of a function between vector spaces and of a function defined on `𝕜` -/ variables {l : F → E} {l' : F →L[𝕜] E} variable (x) /-- The composition `l ∘ f` where `l : F → E` and `f : 𝕜 → F`, has a derivative within a set equal to the Fréchet derivative of `l` applied to the derivative of `f`. -/ theorem has_fderiv_within_at.comp_has_deriv_within_at {t : set F} (hl : has_fderiv_within_at l l' t (f x)) (hf : has_deriv_within_at f f' s x) (hst : s ⊆ f ⁻¹' t) : has_deriv_within_at (l ∘ f) (l' (f')) s x := begin rw has_deriv_within_at_iff_has_fderiv_within_at, convert has_fderiv_within_at.comp x hl hf hst, ext, simp end /-- The composition `l ∘ f` where `l : F → E` and `f : 𝕜 → F`, has a derivative equal to the Fréchet derivative of `l` applied to the derivative of `f`. -/ theorem has_fderiv_at.comp_has_deriv_at (hl : has_fderiv_at l l' (f x)) (hf : has_deriv_at f f' x) : has_deriv_at (l ∘ f) (l' (f')) x := begin rw has_deriv_at_iff_has_fderiv_at, convert has_fderiv_at.comp x hl hf, ext, simp end theorem has_fderiv_at.comp_has_deriv_within_at (hl : has_fderiv_at l l' (f x)) (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (l ∘ f) (l' (f')) s x := begin rw ← has_fderiv_within_at_univ at hl, exact has_fderiv_within_at.comp_has_deriv_within_at x hl hf subset_preimage_univ end lemma fderiv_within.comp_deriv_within {t : set F} (hl : differentiable_within_at 𝕜 l t (f x)) (hf : differentiable_within_at 𝕜 f s x) (hs : s ⊆ f ⁻¹' t) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (l ∘ f) s x = (fderiv_within 𝕜 l t (f x) : F → E) (deriv_within f s x) := begin apply has_deriv_within_at.deriv_within _ hxs, exact (hl.has_fderiv_within_at).comp_has_deriv_within_at x (hf.has_deriv_within_at) hs end lemma fderiv.comp_deriv (hl : differentiable_at 𝕜 l (f x)) (hf : differentiable_at 𝕜 f x) : deriv (l ∘ f) x = (fderiv 𝕜 l (f x) : F → E) (deriv f x) := begin apply has_deriv_at.deriv _, exact (hl.has_fderiv_at).comp_has_deriv_at x (hf.has_deriv_at) end end composition_vector section mul_vector /-! ### Derivative of the multiplication of a scalar function and a vector function -/ variables {c : 𝕜 → 𝕜} {c' : 𝕜} theorem has_deriv_within_at.smul' (hc : has_deriv_within_at c c' s x) (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ y, c y • f y) (c x • f' + c' • f x) s x := begin show has_fderiv_within_at _ _ _ _, convert has_fderiv_within_at.smul' hc hf, ext, simp [smul_add, (mul_smul _ _ _).symm, mul_comm] end theorem has_deriv_at.smul' (hc : has_deriv_at c c' x) (hf : has_deriv_at f f' x) : has_deriv_at (λ y, c y • f y) (c x • f' + c' • f x) x := begin show has_fderiv_at _ _ _, convert has_fderiv_at.smul' hc hf, ext, simp [smul_add, (mul_smul _ _ _).symm, mul_comm] end lemma deriv_within_smul' (hxs : unique_diff_within_at 𝕜 s x) (hc : differentiable_within_at 𝕜 c s x) (hf : differentiable_within_at 𝕜 f s x) : deriv_within (λ y, c y • f y) s x = c x • deriv_within f s x + (deriv_within c s x) • f x := (hc.has_deriv_within_at.smul' hf.has_deriv_within_at).deriv_within hxs lemma deriv_smul' (hc : differentiable_at 𝕜 c x) (hf : differentiable_at 𝕜 f x) : deriv (λ y, c y • f y) x = c x • deriv f x + (deriv c x) • f x := (hc.has_deriv_at.smul' hf.has_deriv_at).deriv end mul_vector section mul /-! ### Derivative of the multiplication of two scalar functions -/ variables {c d : 𝕜 → 𝕜} {c' d' : 𝕜} theorem has_deriv_within_at.mul (hc : has_deriv_within_at c c' s x) (hd : has_deriv_within_at d d' s x) : has_deriv_within_at (λ y, c y * d y) (c x * d' + d x * c') s x := begin show has_fderiv_within_at _ _ _ _, convert has_fderiv_within_at.mul hc hd, ext, simp, ring, end theorem has_deriv_at.mul (hc : has_deriv_at c c' x) (hd : has_deriv_at d d' x) : has_deriv_at (λ y, c y * d y) (c x * d' + d x * c') x := begin show has_fderiv_at _ _ _, convert has_fderiv_at.mul hc hd, ext, simp, ring, end lemma deriv_within_mul (hxs : unique_diff_within_at 𝕜 s x) (hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) : deriv_within (λ y, c y * d y) s x = c x * deriv_within d s x + d x * deriv_within c s x := (hc.has_deriv_within_at.mul hd.has_deriv_within_at).deriv_within hxs lemma deriv_mul (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) : deriv (λ y, c y * d y) x = c x * deriv d x + d x * deriv c x := (hc.has_deriv_at.mul hd.has_deriv_at).deriv end mul section inverse /-! ### Derivative of `x ↦ x⁻¹` -/ lemma has_deriv_at_inv_one : has_deriv_at (λx, x⁻¹) (-1) (1 : 𝕜) := begin rw has_deriv_at_iff_is_o_nhds_zero, have : is_o (λ (h : 𝕜), h^2 * (1 + h)⁻¹) (λ (h : 𝕜), h * 1) (𝓝 0), { have : tendsto (λ (h : 𝕜), (1 + h)⁻¹) (𝓝 0) (𝓝 (1 + 0)⁻¹) := ((tendsto_const_nhds).add tendsto_id).inv' (by norm_num), exact is_o_mul_right (is_o_pow_id (by norm_num)) (is_O_one_of_tendsto this) }, apply (is_o_congr _ _).2 this, { have : metric.ball (0 : 𝕜) (∥(1:𝕜)∥) ∈ 𝓝 (0 : 𝕜), { apply mem_nhds_sets metric.is_open_ball, simp [zero_lt_one] }, filter_upwards [this], assume h hx, have : 0 < ∥1 + h∥ := calc 0 < ∥(1:𝕜)∥ - ∥-h∥ : by rwa [norm_neg, sub_pos, ← dist_zero_right h] ... ≤ ∥1 - -h∥ : norm_sub_norm_le _ _ ... = ∥1 + h∥ : by simp, have : 1 + h ≠ 0 := (norm_pos_iff (1 + h)).mp this, calc (1 + h)⁻¹ - 1⁻¹ - h * -1 = (1+h)⁻¹ - ((1+h) * (1+h)⁻¹) + h * ((1+h) * (1+h)⁻¹) : by { simp only [mul_inv_cancel this], simp } ... = h^2 * (1+h)⁻¹ : by { generalize : (1+h)⁻¹ = y, ring } }, { convert univ_mem_sets, simp } end theorem has_deriv_at_inv (x_ne_zero : x ≠ 0) : has_deriv_at (λy, y⁻¹) (-(x^2)⁻¹) x := begin have A : has_deriv_at (λy, y⁻¹) (-1) (x⁻¹ * x : 𝕜), by { simp [inv_mul_cancel x_ne_zero, has_deriv_at_inv_one] }, have B : has_deriv_at (λy, x⁻¹ * y) (x⁻¹) x, by { convert ((has_deriv_at_const x (x⁻¹)).mul (has_deriv_at_id x)), simp }, convert (has_deriv_at_const _ (x⁻¹)).mul (A.comp x B : _), { ext y, have : x⁻¹ * (y⁻¹ * x) = y⁻¹, by rw [← mul_assoc, mul_comm, ← mul_assoc, mul_inv_cancel x_ne_zero, one_mul], simp [mul_inv', this] }, { simp [pow_two, mul_inv'] } end theorem has_deriv_within_at_inv (x_ne_zero : x ≠ 0) (s : set 𝕜) : has_deriv_within_at (λx, x⁻¹) (-(x^2)⁻¹) s x := (has_deriv_at_inv x_ne_zero).has_deriv_within_at lemma differentiable_at_inv (x_ne_zero : x ≠ 0) : differentiable_at 𝕜 (λx, x⁻¹) x := (has_deriv_at_inv x_ne_zero).differentiable_at lemma differentiable_within_at_inv (x_ne_zero : x ≠ 0) : differentiable_within_at 𝕜 (λx, x⁻¹) s x := (differentiable_at_inv x_ne_zero).differentiable_within_at lemma differentiable_on_inv : differentiable_on 𝕜 (λx:𝕜, x⁻¹) {x | x ≠ 0} := λx hx, differentiable_within_at_inv hx lemma deriv_inv (x_ne_zero : x ≠ 0) : deriv (λx, x⁻¹) x = -(x^2)⁻¹ := (has_deriv_at_inv x_ne_zero).deriv lemma deriv_within_inv (x_ne_zero : x ≠ 0) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, x⁻¹) s x = -(x^2)⁻¹ := begin rw differentiable_at.deriv_within (differentiable_at_inv x_ne_zero) hxs, exact deriv_inv x_ne_zero end lemma has_fderiv_at_inv (x_ne_zero : x ≠ 0) : has_fderiv_at (λx, x⁻¹) (smul_right 1 (-(x^2)⁻¹) : 𝕜 →L[𝕜] 𝕜) x := by simpa [has_deriv_at_iff_has_fderiv_at] using has_deriv_at_inv x_ne_zero lemma has_fderiv_within_at_inv (x_ne_zero : x ≠ 0) : has_fderiv_within_at (λx, x⁻¹) (smul_right 1 (-(x^2)⁻¹) : 𝕜 →L[𝕜] 𝕜) s x := (has_fderiv_at_inv x_ne_zero).has_fderiv_within_at lemma fderiv_inv (x_ne_zero : x ≠ 0) : fderiv 𝕜 (λx, x⁻¹) x = smul_right 1 (-(x^2)⁻¹) := (has_fderiv_at_inv x_ne_zero).fderiv lemma fderiv_within_inv (x_ne_zero : x ≠ 0) (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 (λx, x⁻¹) s x = smul_right 1 (-(x^2)⁻¹) := begin rw differentiable_at.fderiv_within (differentiable_at_inv x_ne_zero) hxs, exact fderiv_inv x_ne_zero end end inverse section division /-! ### Derivative of `x ↦ c x / d x` -/ variables {c d : 𝕜 → 𝕜} {c' d' : 𝕜} lemma has_deriv_within_at.div (hc : has_deriv_within_at c c' s x) (hd : has_deriv_within_at d d' s x) (hx : d x ≠ 0) : has_deriv_within_at (λ y, c y / d y) ((c' * d x - c x * d') / (d x)^2) s x := begin have A : (d x)⁻¹ * (d x)⁻¹ * (c' * d x) = (d x)⁻¹ * c', by rw [← mul_assoc, mul_comm, ← mul_assoc, ← mul_assoc, mul_inv_cancel hx, one_mul], convert hc.mul ((has_deriv_at_inv hx).comp_has_deriv_within_at x hd), simp [div_eq_inv_mul, pow_two, mul_inv', mul_add, A], ring end lemma has_deriv_at.div (hc : has_deriv_at c c' x) (hd : has_deriv_at d d' x) (hx : d x ≠ 0) : has_deriv_at (λ y, c y / d y) ((c' * d x - c x * d') / (d x)^2) x := begin rw ← has_deriv_within_at_univ at *, exact hc.div hd hx end lemma differentiable_within_at.div (hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) (hx : d x ≠ 0) : differentiable_within_at 𝕜 (λx, c x / d x) s x := ((hc.has_deriv_within_at).div (hd.has_deriv_within_at) hx).differentiable_within_at lemma differentiable_at.div (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) (hx : d x ≠ 0) : differentiable_at 𝕜 (λx, c x / d x) x := ((hc.has_deriv_at).div (hd.has_deriv_at) hx).differentiable_at lemma differentiable_on.div (hc : differentiable_on 𝕜 c s) (hd : differentiable_on 𝕜 d s) (hx : ∀ x ∈ s, d x ≠ 0) : differentiable_on 𝕜 (λx, c x / d x) s := λx h, (hc x h).div (hd x h) (hx x h) lemma differentiable.div (hc : differentiable 𝕜 c) (hd : differentiable 𝕜 d) (hx : ∀ x, d x ≠ 0) : differentiable 𝕜 (λx, c x / d x) := λx, (hc x).div (hd x) (hx x) lemma deriv_within_div (hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) (hx : d x ≠ 0) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, c x / d x) s x = ((deriv_within c s x) * d x - c x * (deriv_within d s x)) / (d x)^2 := ((hc.has_deriv_within_at).div (hd.has_deriv_within_at) hx).deriv_within hxs lemma deriv_div (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) (hx : d x ≠ 0) : deriv (λx, c x / d x) x = ((deriv c x) * d x - c x * (deriv d x)) / (d x)^2 := ((hc.has_deriv_at).div (hd.has_deriv_at) hx).deriv end division namespace polynomial /-! ### Derivative of a polynomial -/ variable (p : polynomial 𝕜) /-- The derivative (in the analysis sense) of a polynomial `p` is given by `p.derivative`. -/ protected lemma has_deriv_at (x : 𝕜) : has_deriv_at (λx, p.eval x) (p.derivative.eval x) x := begin apply p.induction_on, { simp [has_deriv_at_const] }, { assume p q hp hq, convert hp.add hq; simp }, { assume n a h, convert h.mul (has_deriv_at_id x), { ext y, simp [pow_add, mul_assoc] }, { simp [pow_add], ring } } end protected theorem has_deriv_within_at (x : 𝕜) (s : set 𝕜) : has_deriv_within_at (λx, p.eval x) (p.derivative.eval x) s x := (p.has_deriv_at x).has_deriv_within_at protected lemma differentiable_at : differentiable_at 𝕜 (λx, p.eval x) x := (p.has_deriv_at x).differentiable_at protected lemma differentiable_within_at : differentiable_within_at 𝕜 (λx, p.eval x) s x := p.differentiable_at.differentiable_within_at protected lemma differentiable : differentiable 𝕜 (λx, p.eval x) := λx, p.differentiable_at protected lemma differentiable_on : differentiable_on 𝕜 (λx, p.eval x) s := p.differentiable.differentiable_on @[simp] protected lemma deriv : deriv (λx, p.eval x) x = p.derivative.eval x := (p.has_deriv_at x).deriv protected lemma deriv_within (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, p.eval x) s x = p.derivative.eval x := begin rw differentiable_at.deriv_within p.differentiable_at hxs, exact p.deriv end protected lemma continuous : continuous (λx, p.eval x) := p.differentiable.continuous protected lemma continuous_on : continuous_on (λx, p.eval x) s := p.continuous.continuous_on protected lemma continuous_at : continuous_at (λx, p.eval x) x := p.continuous.continuous_at protected lemma continuous_within_at : continuous_within_at (λx, p.eval x) s x := p.continuous_at.continuous_within_at protected lemma has_fderiv_at (x : 𝕜) : has_fderiv_at (λx, p.eval x) (smul_right 1 (p.derivative.eval x) : 𝕜 →L[𝕜] 𝕜) x := by simpa [has_deriv_at_iff_has_fderiv_at] using p.has_deriv_at x protected lemma has_fderiv_within_at (x : 𝕜) : has_fderiv_within_at (λx, p.eval x) (smul_right 1 (p.derivative.eval x) : 𝕜 →L[𝕜] 𝕜) s x := (p.has_fderiv_at x).has_fderiv_within_at @[simp] protected lemma fderiv : fderiv 𝕜 (λx, p.eval x) x = smul_right 1 (p.derivative.eval x) := (p.has_fderiv_at x).fderiv protected lemma fderiv_within (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 (λx, p.eval x) s x = smul_right 1 (p.derivative.eval x) := begin rw differentiable_at.fderiv_within p.differentiable_at hxs, exact p.fderiv end end polynomial section pow /-! ### Derivative of `x ↦ x^n` for `n : ℕ` -/ variable {n : ℕ } lemma has_deriv_at_pow (n : ℕ) (x : 𝕜) : has_deriv_at (λx, x^n) ((n : 𝕜) * x^(n-1)) x := begin convert (polynomial.C 1 * (polynomial.X)^n).has_deriv_at x, { simp }, { rw [polynomial.derivative_monomial], simp } end theorem has_deriv_within_at_pow (n : ℕ) (x : 𝕜) (s : set 𝕜) : has_deriv_within_at (λx, x^n) ((n : 𝕜) * x^(n-1)) s x := (has_deriv_at_pow n x).has_deriv_within_at lemma differentiable_at_pow : differentiable_at 𝕜 (λx, x^n) x := (has_deriv_at_pow n x).differentiable_at lemma differentiable_within_at_pow : differentiable_within_at 𝕜 (λx, x^n) s x := differentiable_at_pow.differentiable_within_at lemma differentiable_pow : differentiable 𝕜 (λx:𝕜, x^n) := λx, differentiable_at_pow lemma differentiable_on_pow : differentiable_on 𝕜 (λx, x^n) s := differentiable_pow.differentiable_on lemma deriv_pow : deriv (λx, x^n) x = (n : 𝕜) * x^(n-1) := (has_deriv_at_pow n x).deriv @[simp] lemma deriv_pow' : deriv (λx, x^n) = λ x, (n : 𝕜) * x^(n-1) := funext $ λ x, deriv_pow lemma deriv_within_pow (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, x^n) s x = (n : 𝕜) * x^(n-1) := by rw [differentiable_at_pow.deriv_within hxs, deriv_pow] end pow
3ff896d85bca97eac61b55e08561bb657cf04282
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/analysis/calculus/mean_value.lean
b47d53a0bc5788be3e018112904cad35f4a596f4
[ "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
48,033
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yury Kudryashov -/ import analysis.calculus.local_extr import analysis.convex.topology /-! # The mean value inequality and equalities In this file we prove the following facts: * `convex.norm_image_sub_le_of_norm_deriv_le` : if `f` is differentiable on a convex set `s` and the norm of its derivative is bounded by `C`, then `f` is Lipschitz continuous on `s` with constant `C`; also a variant in which what is bounded by `C` is the norm of the difference of the derivative from a fixed linear map. * `image_le_of*`, `image_norm_le_of_*` : several similar lemmas deducing `f x ≤ B x` or `∥f x∥ ≤ B x` from upper estimates on `f'` or `∥f'∥`, respectively. These lemmas differ by their assumptions: * `of_liminf_*` lemmas assume that limit inferior of some ratio is less than `B' x`; * `of_deriv_right_*`, `of_norm_deriv_right_*` lemmas assume that the right derivative or its norm is less than `B' x`; * `of_*_lt_*` lemmas assume a strict inequality whenever `f x = B x` or `∥f x∥ = B x`; * `of_*_le_*` lemmas assume a non-strict inequality everywhere on `[a, b)`; * name of a lemma ends with `'` if (1) it assumes that `B` is continuous on `[a, b]` and has a right derivative at every point of `[a, b)`, and (2) the lemma has a counterpart assuming that `B` is differentiable everywhere on `ℝ` * `norm_image_sub_le_*_segment` : if derivative of `f` on `[a, b]` is bounded above by a constant `C`, then `∥f x - f a∥ ≤ C * ∥x - a∥`; several versions deal with right derivative and derivative within `[a, b]` (`has_deriv_within_at` or `deriv_within`). * `convex.is_const_of_fderiv_within_eq_zero` : if a function has derivative `0` on a convex set `s`, then it is a constant on `s`. * `exists_ratio_has_deriv_at_eq_ratio_slope` and `exists_ratio_deriv_eq_ratio_slope` : Cauchy's Mean Value Theorem. * `exists_has_deriv_at_eq_slope` and `exists_deriv_eq_slope` : Lagrange's Mean Value Theorem. * `domain_mvt` : Lagrange's Mean Value Theorem, applied to a segment in a convex domain. * `convex.image_sub_lt_mul_sub_of_deriv_lt`, `convex.mul_sub_lt_image_sub_of_lt_deriv`, `convex.image_sub_le_mul_sub_of_deriv_le`, `convex.mul_sub_le_image_sub_of_le_deriv`, if `∀ x, C (</≤/>/≥) (f' x)`, then `C * (y - x) (</≤/>/≥) (f y - f x)` whenever `x < y`. * `convex.mono_of_deriv_nonneg`, `convex.antimono_of_deriv_nonpos`, `convex.strict_mono_of_deriv_pos`, `convex.strict_antimono_of_deriv_neg` : if the derivative of a function is non-negative/non-positive/positive/negative, then the function is monotone/monotonically decreasing/strictly monotone/strictly monotonically decreasing. * `convex_on_of_deriv_mono`, `convex_on_of_deriv2_nonneg` : if the derivative of a function is increasing or its second derivative is nonnegative, then the original function is convex. * `strict_fderiv_of_cont_diff` : a C^1 function over the reals is strictly differentiable. (This is a corollary of the mean value inequality.) -/ variables {E : Type*} [normed_group E] [normed_space ℝ E] {F : Type*} [normed_group F] [normed_space ℝ F] open metric set asymptotics continuous_linear_map filter open_locale classical topological_space /-! ### One-dimensional fencing inequalities -/ /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in nhds_within x (Ioi x), (z - x)⁻¹ * (f z - f x) < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := begin change Icc a b ⊆ {x | f x ≤ B x}, set s := {x | f x ≤ B x} ∩ Icc a b, have A : continuous_on (λ x, (f x, B x)) (Icc a b), from hf.prod hB, have : is_closed s, { simp only [s, inter_comm], exact A.preimage_closed_of_closed is_closed_Icc order_closed_topology.is_closed_le' }, apply this.Icc_subset_of_forall_exists_gt ha, rintros x ⟨hxB, xab⟩ y hy, change f x ≤ B x at hxB, cases lt_or_eq_of_le hxB with hxB hxB, { -- If `f x < B x`, then all we need is continuity of both sides apply @nonempty_of_mem_sets _ (nhds_within x (Ioi x)), refine inter_mem_sets _ (Ioc_mem_nhds_within_Ioi ⟨le_refl x, hy⟩), have : ∀ᶠ x in nhds_within x (Icc a b), f x < B x, from A x (Ico_subset_Icc_self xab) (mem_nhds_sets (is_open_lt continuous_fst continuous_snd) hxB), have : ∀ᶠ x in nhds_within x (Ioi x), f x < B x, from nhds_within_le_of_mem (Icc_mem_nhds_within_Ioi xab) this, refine mem_sets_of_superset this (set_of_subset_set_of.2 $ λ y, le_of_lt) }, { rcases dense (bound x xab hxB) with ⟨r, hfr, hrB⟩, specialize hf' x xab r hfr, have HB : ∀ᶠ z in nhds_within x (Ioi x), r < (z - x)⁻¹ * (B z - B x), from (has_deriv_within_at_iff_tendsto_slope' $ lt_irrefl x).1 (hB' x xab) (mem_nhds_sets is_open_Ioi hrB), obtain ⟨z, ⟨hfz, hzB⟩, hz⟩ : ∃ z, ((z - x)⁻¹ * (f z - f x) < r ∧ r < (z - x)⁻¹ * (B z - B x)) ∧ z ∈ Ioc x y, from ((hf'.and_eventually HB).and_eventually (Ioc_mem_nhds_within_Ioi ⟨le_refl _, hy⟩)).exists, have := le_of_lt (lt_trans hfz hzB), refine ⟨z, _, hz⟩, rw [mul_le_mul_left (inv_pos.2 $ sub_pos.2 hz.1), hxB, sub_le_sub_iff_right] at this, exact this } end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in nhds_within x (Ioi x), (z - x)⁻¹ * (f z - f x) < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by `B'`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_le_deriv_boundary {f : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) -- `bound` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ B' x` (bound : ∀ x ∈ Ico a b, ∀ r, B' x < r → ∃ᶠ z in nhds_within x (Ioi x), (z - x)⁻¹ * (f z - f x) < r) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := begin have Hr : ∀ x ∈ Icc a b, ∀ r ∈ Ioi (0:ℝ), f x ≤ B x + r * (x - a), { intros x hx r hr, apply image_le_of_liminf_slope_right_lt_deriv_boundary' hf bound, { rwa [sub_self, mul_zero, add_zero] }, { exact hB.add (continuous_on_const.mul (continuous_id.continuous_on.sub continuous_on_const)) }, { assume x hx, exact (hB' x hx).add (((has_deriv_within_at_id x (Ioi x)).sub_const a).const_mul r) }, { assume x hx _, rw [mul_one], exact (lt_add_iff_pos_right _).2 hr }, exact hx }, assume x hx, have : continuous_within_at (λ r, B x + r * (x - a)) (Ioi 0) 0, from continuous_within_at_const.add (continuous_within_at_id.mul continuous_within_at_const), convert continuous_within_at_const.closure_le _ this (Hr x hx); simp [closure_Ioi] end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf (λ x hx r hr, (hf' x hx).liminf_right_slope_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_deriv_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x ≤ B' x` on `[a, b)`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_le_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, f' x ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary hf ha hB hB' $ assume x hx r hr, (hf' x hx).liminf_right_slope_le (lt_of_le_of_lt (bound x hx) hr) /-! ### Vector-valued functions `f : ℝ → E` -/ section variables {f : ℝ → E} {a b : ℝ} /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `B` has right derivative at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(∥f z∥ - ∥f x∥) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. -/ lemma image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary {E : Type*} [normed_group E] {f : ℝ → E} {f' : ℝ → ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf ∥z - x∥⁻¹ * (∥f z∥ - ∥f x∥) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in nhds_within x (Ioi x), (z - x)⁻¹ * (∥f z∥ - ∥f x∥) < r) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' (continuous_norm.comp_continuous_on hf) hf' ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary' {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → ∥f' x∥ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary hf (λ x hx r hr, (hf' x hx).liminf_right_slope_norm_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → ∥f' x∥ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_norm_deriv_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary' {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, ∥f' x∥ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary (continuous_norm.comp_continuous_on hf) ha hB hB' $ (λ x hx r hr, (hf' x hx).liminf_right_slope_norm_le (lt_of_le_of_lt (bound x hx) hr)) /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, ∥f' x∥ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_norm_deriv_right_le_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- A function on `[a, b]` with the norm of the right derivative bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`. -/ theorem norm_image_sub_le_of_norm_deriv_right_le_segment {f' : ℝ → E} {C : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) (bound : ∀x ∈ Ico a b, ∥f' x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin let g := λ x, f x - f a, have hg : continuous_on g (Icc a b), from hf.sub continuous_on_const, have hg' : ∀ x ∈ Ico a b, has_deriv_within_at g (f' x) (Ioi x) x, { assume x hx, simpa using (hf' x hx).sub (has_deriv_within_at_const _ _ _) }, let B := λ x, C * (x - a), have hB : ∀ x, has_deriv_at B C x, { assume x, simpa using (has_deriv_at_const x C).mul ((has_deriv_at_id x).sub (has_deriv_at_const x a)) }, convert image_norm_le_of_norm_deriv_right_le_deriv_boundary hg hg' _ hB bound, { simp only [g, B] }, { simp only [g, B], rw [sub_self, _root_.norm_zero, sub_self, mul_zero] } end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc a b, has_deriv_within_at f (f' x) (Icc a b) x) (bound : ∀x ∈ Ico a b, ∥f' x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_right_le_segment (λ x hx, (hf x hx).continuous_within_at) (λ x hx, _) bound, apply (hf x $ Ico_subset_Icc_self hx).nhds_within, exact Icc_mem_nhds_within_Ioi hx end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment {C : ℝ} (hf : differentiable_on ℝ f (Icc a b)) (bound : ∀x ∈ Ico a b, ∥deriv_within f (Icc a b) x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_le_segment' _ bound, exact λ x hx, (hf x hx).has_deriv_within_at end /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc (0:ℝ) 1, has_deriv_within_at f (f' x) (Icc (0:ℝ) 1) x) (bound : ∀x ∈ Ico (0:ℝ) 1, ∥f' x∥ ≤ C) : ∥f 1 - f 0∥ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment' hf bound 1 (right_mem_Icc.2 zero_le_one) /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01 {C : ℝ} (hf : differentiable_on ℝ f (Icc (0:ℝ) 1)) (bound : ∀x ∈ Ico (0:ℝ) 1, ∥deriv_within f (Icc (0:ℝ) 1) x∥ ≤ C) : ∥f 1 - f 0∥ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment hf bound 1 (right_mem_Icc.2 zero_le_one) end /-! ### Vector-valued functions `f : E → F` -/ /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] F)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := begin /- By composition with `t ↦ x + t • (y-x)`, we reduce to a statement for functions defined on `[0,1]`, for which it is proved in `norm_image_sub_le_of_norm_deriv_le_segment`. We just have to check the differentiability of the composition and bounds on its derivative, which is straightforward but tedious for lack of automation. -/ have C0 : 0 ≤ C := le_trans (norm_nonneg _) (bound x xs), set g : ℝ → E := λ t, x + t • (y - x), have Dg : ∀ t, has_deriv_at g (y-x) t, { assume t, simpa only [one_smul] using ((has_deriv_at_id t).smul_const (y - x)).const_add x }, have segm : Icc 0 1 ⊆ g ⁻¹' s, { rw [← image_subset_iff, ← segment_eq_image'], apply hs.segment_subset xs ys }, have : f x = f (g 0), by { simp only [g], rw [zero_smul, add_zero] }, rw this, have : f y = f (g 1), by { simp only [g], rw [one_smul, add_sub_cancel'_right] }, rw this, have D2: ∀ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) ((f' (g t) : E → F) (y-x)) (Icc (0:ℝ) 1) t, { intros t ht, exact (hf (g t) $ segm ht).comp_has_deriv_within_at _ (Dg t).has_deriv_within_at segm }, apply norm_image_sub_le_of_norm_deriv_le_segment_01' D2, assume t ht, refine le_trans (le_op_norm _ _) (mul_le_mul_of_nonneg_right _ (norm_nonneg _)), exact bound (g t) (segm $ Ico_subset_Icc_self ht) end /-- The mean value theorem on a convex set: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} {x y : E} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥fderiv_within ℝ f s x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le {f : E → F} {C : ℝ} {s : set E} {x y : E} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥fderiv ℝ f x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- Variant of the mean value inequality on a convex set, using a bound on the difference between the derivative and a fixed linear map, rather than a bound on the derivative itself. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le' {f : E → F} {C : ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] F)} {φ : E →L[ℝ] F} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := begin /- We subtract `φ` to define a new function `g` for which `g' = 0`, for which the previous theorem applies, `convex.norm_image_sub_le_of_norm_has_fderiv_within_le`. Then, we just need to glue together the pieces, expressing back `f` in terms of `g`. -/ let g := λy, f y - φ y, have hg : ∀ x ∈ s, has_fderiv_within_at g (f' x - φ) s x := λ x xs, (hf x xs).sub φ.has_fderiv_within_at, calc ∥f y - f x - φ (y - x)∥ = ∥f y - f x - (φ y - φ x)∥ : by simp ... = ∥(f y - φ y) - (f x - φ x)∥ : by abel ... = ∥g y - g x∥ : by simp ... ≤ C * ∥y - x∥ : convex.norm_image_sub_le_of_norm_has_fderiv_within_le hg bound hs xs ys, end /-- Variant of the mean value inequality on a convex set. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le' {f : E → F} {C : ℝ} {s : set E} {x y : E} {φ : E →L[ℝ] F} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥fderiv_within ℝ f s x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (λ x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- Variant of the mean value inequality on a convex set. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le' {f : E → F} {C : ℝ} {s : set E} {x y : E} {φ : E →L[ℝ] F} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥fderiv ℝ f x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- If a function has zero Fréchet derivative at every point of a convex set, then it is a constant on this set. -/ theorem convex.is_const_of_fderiv_within_eq_zero {s : set E} (hs : convex s) {f : E → F} (hf : differentiable_on ℝ f s) (hf' : ∀ x ∈ s, fderiv_within ℝ f s x = 0) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : f x = f y := have bound : ∀ x ∈ s, ∥fderiv_within ℝ f s x∥ ≤ 0, from λ x hx, by simp only [hf' x hx, _root_.norm_zero], by simpa only [(dist_eq_norm _ _).symm, zero_mul, dist_le_zero, eq_comm] using hs.norm_image_sub_le_of_norm_fderiv_within_le hf bound hx hy theorem is_const_of_fderiv_eq_zero {f : E → F} (hf : differentiable ℝ f) (hf' : ∀ x, fderiv ℝ f x = 0) (x y : E) : f x = f y := convex_univ.is_const_of_fderiv_within_eq_zero hf.differentiable_on (λ x _, by rw fderiv_within_univ; exact hf' x) trivial trivial /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_deriv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_deriv_within_le {f f' : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : ∀ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := convex.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) (λ x hx, le_trans (by simp) (bound x hx)) hs xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv_within` -/ theorem convex.norm_image_sub_le_of_norm_deriv_within_le {f : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥deriv_within f s x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_within_at) bound xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv`. -/ theorem convex.norm_image_sub_le_of_norm_deriv_le {f : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥deriv f x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound xs ys /-! ### Functions `[a, b] → ℝ`. -/ section interval -- Declare all variables here to make sure they come in a correct order variables (f f' : ℝ → ℝ) {a b : ℝ} (hab : a < b) (hfc : continuous_on f (Icc a b)) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hfd : differentiable_on ℝ f (Ioo a b)) (g g' : ℝ → ℝ) (hgc : continuous_on g (Icc a b)) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hgd : differentiable_on ℝ g (Ioo a b)) include hab hfc hff' hgc hgg' /-- Cauchy's Mean Value Theorem, `has_deriv_at` version. -/ lemma exists_ratio_has_deriv_at_eq_ratio_slope : ∃ c ∈ Ioo a b, (g b - g a) * f' c = (f b - f a) * g' c := begin let h := λ x, (g b - g a) * f x - (f b - f a) * g x, have hI : h a = h b, { simp only [h], ring }, let h' := λ x, (g b - g a) * f' x - (f b - f a) * g' x, have hhh' : ∀ x ∈ Ioo a b, has_deriv_at h (h' x) x, from λ x hx, ((hff' x hx).const_mul (g b - g a)).sub ((hgg' x hx).const_mul (f b - f a)), have hhc : continuous_on h (Icc a b), from (continuous_on_const.mul hfc).sub (continuous_on_const.mul hgc), rcases exists_has_deriv_at_eq_zero h h' hab hhc hI hhh' with ⟨c, cmem, hc⟩, exact ⟨c, cmem, sub_eq_zero.1 hc⟩ end omit hgc hgg' /-- Lagrange's Mean Value Theorem, `has_deriv_at` version -/ lemma exists_has_deriv_at_eq_slope : ∃ c ∈ Ioo a b, f' c = (f b - f a) / (b - a) := begin rcases exists_ratio_has_deriv_at_eq_ratio_slope f f' hab hfc hff' id 1 continuous_id.continuous_on (λ x hx, has_deriv_at_id x) with ⟨c, cmem, hc⟩, use [c, cmem], simp only [_root_.id, pi.one_apply, mul_one] at hc, rw [← hc, mul_div_cancel_left], exact ne_of_gt (sub_pos.2 hab) end omit hff' /-- Cauchy's Mean Value Theorem, `deriv` version. -/ lemma exists_ratio_deriv_eq_ratio_slope : ∃ c ∈ Ioo a b, (g b - g a) * (deriv f c) = (f b - f a) * (deriv g c) := exists_ratio_has_deriv_at_eq_ratio_slope f (deriv f) hab hfc (λ x hx, ((hfd x hx).differentiable_at $ mem_nhds_sets is_open_Ioo hx).has_deriv_at) g (deriv g) hgc (λ x hx, ((hgd x hx).differentiable_at $ mem_nhds_sets is_open_Ioo hx).has_deriv_at) /-- Lagrange's Mean Value Theorem, `deriv` version. -/ lemma exists_deriv_eq_slope : ∃ c ∈ Ioo a b, deriv f c = (f b - f a) / (b - a) := exists_has_deriv_at_eq_slope f (deriv f) hab hfc (λ x hx, ((hfd x hx).differentiable_at $ mem_nhds_sets is_open_Ioo hx).has_deriv_at) end interval /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C < f'`, then `f` grows faster than `C * x` on `D`, i.e., `C * (y - x) < f y - f x` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.mul_sub_lt_image_sub_of_lt_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_gt : ∀ x ∈ interior D, C < deriv f x) : ∀ x y ∈ D, x < y → C * (y - x) < f y - f x := begin assume x y hx hy hxy, have hxyD : Icc x y ⊆ D, from convex_real_iff.1 hD hx hy, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), have : C < (f y - f x) / (y - x), by { rw [← ha], exact hf'_gt _ (hxyD' a_mem) }, exact (lt_div_iff (sub_pos.2 hxy)).1 this end /-- Let `f : ℝ → ℝ` be a differentiable function. If `C < f'`, then `f` grows faster than `C * x`, i.e., `C * (y - x) < f y - f x` whenever `x < y`. -/ theorem mul_sub_lt_image_sub_of_lt_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (hf'_gt : ∀ x, C < deriv f x) ⦃x y⦄ (hxy : x < y) : C * (y - x) < f y - f x := convex_univ.mul_sub_lt_image_sub_of_lt_deriv hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_gt x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C ≤ f'`, then `f` grows at least as fast as `C * x` on `D`, i.e., `C * (y - x) ≤ f y - f x` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.mul_sub_le_image_sub_of_le_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_ge : ∀ x ∈ interior D, C ≤ deriv f x) : ∀ x y ∈ D, x ≤ y → C * (y - x) ≤ f y - f x := begin assume x y hx hy hxy, cases eq_or_lt_of_le hxy with hxy' hxy', by rw [hxy', sub_self, sub_self, mul_zero], have hxyD : Icc x y ⊆ D, from convex_real_iff.1 hD hx hy, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy' (hf.mono hxyD) (hf'.mono hxyD'), have : C ≤ (f y - f x) / (y - x), by { rw [← ha], exact hf'_ge _ (hxyD' a_mem) }, exact (le_div_iff (sub_pos.2 hxy')).1 this end /-- Let `f : ℝ → ℝ` be a differentiable function. If `C ≤ f'`, then `f` grows at least as fast as `C * x`, i.e., `C * (y - x) ≤ f y - f x` whenever `x ≤ y`. -/ theorem mul_sub_le_image_sub_of_le_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (hf'_ge : ∀ x, C ≤ deriv f x) ⦃x y⦄ (hxy : x ≤ y) : C * (y - x) ≤ f y - f x := convex_univ.mul_sub_le_image_sub_of_le_deriv hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_ge x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.image_sub_lt_mul_sub_of_deriv_lt {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (lt_hf' : ∀ x ∈ interior D, deriv f x < C) : ∀ x y ∈ D, x < y → f y - f x < C * (y - x) := begin assume x y hx hy hxy, have hf'_gt : ∀ x ∈ interior D, -C < deriv (λ y, -f y) x, { assume x hx, rw [deriv.neg, neg_lt_neg_iff], exact lt_hf' x hx }, simpa [-neg_lt_neg_iff] using neg_lt_neg (hD.mul_sub_lt_image_sub_of_lt_deriv hf.neg hf'.neg hf'_gt x y hx hy hxy) end /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x < y`. -/ theorem image_sub_lt_mul_sub_of_deriv_lt {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (lt_hf' : ∀ x, deriv f x < C) ⦃x y⦄ (hxy : x < y) : f y - f x < C * (y - x) := convex_univ.image_sub_lt_mul_sub_of_deriv_lt hf.continuous.continuous_on hf.differentiable_on (λ x _, lt_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' ≤ C`, then `f` grows at most as fast as `C * x` on `D`, i.e., `f y - f x ≤ C * (y - x)` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.image_sub_le_mul_sub_of_deriv_le {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (le_hf' : ∀ x ∈ interior D, deriv f x ≤ C) : ∀ x y ∈ D, x ≤ y → f y - f x ≤ C * (y - x) := begin assume x y hx hy hxy, have hf'_ge : ∀ x ∈ interior D, -C ≤ deriv (λ y, -f y) x, { assume x hx, rw [deriv.neg, neg_le_neg_iff], exact le_hf' x hx }, simpa [-neg_le_neg_iff] using neg_le_neg (hD.mul_sub_le_image_sub_of_le_deriv hf.neg hf'.neg hf'_ge x y hx hy hxy) end /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' ≤ C`, then `f` grows at most as fast as `C * x`, i.e., `f y - f x ≤ C * (y - x)` whenever `x ≤ y`. -/ theorem image_sub_le_mul_sub_of_deriv_le {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (le_hf' : ∀ x, deriv f x ≤ C) ⦃x y⦄ (hxy : x ≤ y) : f y - f x ≤ C * (y - x) := convex_univ.image_sub_le_mul_sub_of_deriv_le hf.continuous.continuous_on hf.differentiable_on (λ x _, le_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is positive, then `f` is a strictly monotonically increasing function on `D`. -/ theorem convex.strict_mono_of_deriv_pos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_pos : ∀ x ∈ interior D, 0 < deriv f x) : ∀ x y ∈ D, x < y → f x < f y := by simpa only [zero_mul, sub_pos] using hD.mul_sub_lt_image_sub_of_lt_deriv hf hf' hf'_pos /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is positive, then `f` is a strictly monotonically increasing function. -/ theorem strict_mono_of_deriv_pos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_pos : ∀ x, 0 < deriv f x) : strict_mono f := λ x y hxy, convex_univ.strict_mono_of_deriv_pos hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_pos x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonnegative, then `f` is a monotonically increasing function on `D`. -/ theorem convex.mono_of_deriv_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonneg : ∀ x ∈ interior D, 0 ≤ deriv f x) : ∀ x y ∈ D, x ≤ y → f x ≤ f y := by simpa only [zero_mul, sub_nonneg] using hD.mul_sub_le_image_sub_of_le_deriv hf hf' hf'_nonneg /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonnegative, then `f` is a monotonically increasing function. -/ theorem mono_of_deriv_nonneg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, 0 ≤ deriv f x) : monotone f := λ x y hxy, convex_univ.mono_of_deriv_nonneg hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is negative, then `f` is a strictly monotonically decreasing function on `D`. -/ theorem convex.strict_antimono_of_deriv_neg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_neg : ∀ x ∈ interior D, deriv f x < 0) : ∀ x y ∈ D, x < y → f y < f x := by simpa only [zero_mul, sub_lt_zero] using hD.image_sub_lt_mul_sub_of_deriv_lt hf hf' hf'_neg /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is negative, then `f` is a strictly monotonically decreasing function. -/ theorem strict_antimono_of_deriv_neg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, deriv f x < 0) : ∀ ⦃x y⦄, x < y → f y < f x := λ x y hxy, convex_univ.strict_antimono_of_deriv_neg hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonpositive, then `f` is a monotonically decreasing function on `D`. -/ theorem convex.antimono_of_deriv_nonpos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonpos : ∀ x ∈ interior D, deriv f x ≤ 0) : ∀ x y ∈ D, x ≤ y → f y ≤ f x := by simpa only [zero_mul, sub_nonpos] using hD.image_sub_le_mul_sub_of_deriv_le hf hf' hf'_nonpos /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonpositive, then `f` is a monotonically decreasing function. -/ theorem antimono_of_deriv_nonpos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, deriv f x ≤ 0) : ∀ ⦃x y⦄, x ≤ y → f y ≤ f x := λ x y hxy, convex_univ.antimono_of_deriv_nonpos hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is monotone on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv_mono {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_mono : ∀ x y ∈ interior D, x ≤ y → deriv f x ≤ deriv f y) : convex_on D f := convex_on_real_of_slope_mono_adjacent hD begin intros x y z hx hz hxy hyz, -- First we prove some trivial inclusions have hxzD : Icc x z ⊆ D, from convex_real_iff.1 hD hx hz, have hxyD : Icc x y ⊆ D, from subset.trans (Icc_subset_Icc_right $ le_of_lt hyz) hxzD, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, have hyzD : Icc y z ⊆ D, from subset.trans (Icc_subset_Icc_left $ le_of_lt hxy) hxzD, have hyzD' : Ioo y z ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hyzD⟩, -- Then we apply MVT to both `[x, y]` and `[y, z]` obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), obtain ⟨b, ⟨hyb, hbz⟩, hb⟩ : ∃ b ∈ Ioo y z, deriv f b = (f z - f y) / (z - y), from exists_deriv_eq_slope f hyz (hf.mono hyzD) (hf'.mono hyzD'), rw [← ha, ← hb], exact hf'_mono a b (hxyD' ⟨hxa, hay⟩) (hyzD' ⟨hyb, hbz⟩) (le_of_lt $ lt_trans hay hyb) end /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is monotone on the interior, then `f` is convex on `ℝ`. -/ theorem convex_on_univ_of_deriv_mono {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_mono : monotone (deriv f)) : convex_on univ f := convex_on_of_deriv_mono convex_univ hf.continuous.continuous_on hf.differentiable_on (λ x y _ _ h, hf'_mono h) /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is twice differentiable on its interior, and `f''` is nonnegative on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv2_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'' : differentiable_on ℝ (deriv f) (interior D)) (hf''_nonneg : ∀ x ∈ interior D, 0 ≤ (deriv^[2] f x)) : convex_on D f := convex_on_of_deriv_mono hD hf hf' $ assume x y hx hy hxy, hD.interior.mono_of_deriv_nonneg hf''.continuous_on (by rwa [interior_interior]) (by rwa [interior_interior]) _ _ hx hy hxy /-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonnegative on `ℝ`, then `f` is convex on `ℝ`. -/ theorem convex_on_univ_of_deriv2_nonneg {f : ℝ → ℝ} (hf' : differentiable ℝ f) (hf'' : differentiable ℝ (deriv f)) (hf''_nonneg : ∀ x, 0 ≤ (deriv^[2] f x)) : convex_on univ f := convex_on_of_deriv2_nonneg convex_univ hf'.continuous.continuous_on hf'.differentiable_on hf''.differentiable_on (λ x _, hf''_nonneg x) /-! ### Functions `f : E → ℝ` -/ /-- Lagrange's Mean Value Theorem, applied to convex domains. -/ theorem domain_mvt {f : E → ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] ℝ)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∃ z ∈ segment x y, f y - f x = f' z (y - x) := begin have hIccIoo := @Ioo_subset_Icc_self ℝ _ 0 1, -- parametrize segment set g : ℝ → E := λ t, x + t • (y - x), have hseg : ∀ t ∈ Icc (0:ℝ) 1, g t ∈ segment x y, { rw segment_eq_image', simp only [mem_image, and_imp, add_right_inj], intros t ht, exact ⟨t, ht, rfl⟩ }, have hseg' : Icc 0 1 ⊆ g ⁻¹' s, { rw ← image_subset_iff, unfold image, change ∀ _, _, intros z Hz, rw mem_set_of_eq at Hz, rcases Hz with ⟨t, Ht, hgt⟩, rw ← hgt, exact hs.segment_subset xs ys (hseg t Ht) }, -- derivative of pullback of f under parametrization have hfg: ∀ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) ((f' (g t) : E → ℝ) (y-x)) (Icc (0:ℝ) 1) t, { intros t Ht, have hg : has_deriv_at g (y-x) t, { have := ((has_deriv_at_id t).smul_const (y - x)).const_add x, rwa one_smul at this }, exact (hf (g t) $ hseg' Ht).comp_has_deriv_within_at _ hg.has_deriv_within_at hseg' }, -- apply 1-variable mean value theorem to pullback have hMVT : ∃ (t ∈ Ioo (0:ℝ) 1), ((f' (g t) : E → ℝ) (y-x)) = (f (g 1) - f (g 0)) / (1 - 0), { refine exists_has_deriv_at_eq_slope (f ∘ g) _ (by norm_num) _ _, { unfold continuous_on, exact λ t Ht, (hfg t Ht).continuous_within_at }, { refine λ t Ht, (hfg t $ hIccIoo Ht).has_deriv_at _, refine mem_nhds_sets_iff.mpr _, use (Ioo (0:ℝ) 1), refine ⟨hIccIoo, _, Ht⟩, simp [real.Ioo_eq_ball, is_open_ball] } }, -- reinterpret on domain rcases hMVT with ⟨t, Ht, hMVT'⟩, use g t, refine ⟨hseg t $ hIccIoo Ht, _⟩, simp [g, hMVT'], end /-! ### Vector-valued functions `f : E → F`. Strict differentiability. -/ /-- Over the reals, a continuously differentiable function is strictly differentiable. -/ lemma strict_fderiv_of_cont_diff {f : E → F} {s : set E} {x : E} {f' : E → (E →L[ℝ] F)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hcont : continuous_on f' s) (hs : s ∈ 𝓝 x) : has_strict_fderiv_at f (f' x) x := begin -- turn little-o definition of strict_fderiv into an epsilon-delta statement apply is_o_iff_forall_is_O_with.mpr, intros c hc, refine is_O_with.of_bound (eventually_iff.mpr (mem_nhds_iff.mpr _)), -- the correct ε is the modulus of continuity of f', shrunk to be inside s rcases (metric.continuous_on_iff.mp hcont x (mem_of_nhds hs) c hc) with ⟨ε₁, H₁, hcont'⟩, rcases (mem_nhds_iff.mp hs) with ⟨ε₂, H₂, hε₂⟩, refine ⟨min ε₁ ε₂, lt_min H₁ H₂, _⟩, -- mess with ε construction set t := ball x (min ε₁ ε₂), have hts : t ⊆ s := λ _ hy, hε₂ (ball_subset_ball (min_le_right ε₁ ε₂) hy), have Hf : ∀ y ∈ t, has_fderiv_within_at f (f' y) t y := λ y yt, has_fderiv_within_at.mono (hf y (hts yt)) hts, have hconv := convex_ball x (min ε₁ ε₂), -- simplify formulas involving the product E × E rintros ⟨a, b⟩ h, simp only [mem_set_of_eq, map_sub], have hab : a ∈ t ∧ b ∈ t := by rwa [mem_ball, prod.dist_eq, max_lt_iff] at h, -- exploit the choice of ε as the modulus of continuity of f' have hf' : ∀ x' ∈ t, ∥f' x' - f' x∥ ≤ c, { intros x' H', refine le_of_lt (hcont' x' (hts H') _), exact ball_subset_ball (min_le_left ε₁ ε₂) H' }, -- apply mean value theorem simpa using convex.norm_image_sub_le_of_norm_has_fderiv_within_le' Hf hf' hconv hab.2 hab.1, end
455c9822ab41a99874df1250fa7464d87540655d
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/group_theory/free_abelian_group.lean
621d2cc1b4936a4b9adf982eca14c6e4afd116ba
[ "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
18,855
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.group.pi import group_theory.free_group import group_theory.abelianization import algebra.module.basic -- we use the ℤ-module structure on an add_comm_group in punit_equiv /-! # Free abelian groups The free abelian group on a type `α`, defined as the abelianisation of the free group on `α`. The free abelian group on `α` can be abstractly defined as the left adjoint of the forgetful functor from abelian groups to types. Alternatively, one could define it as the functions `α → ℤ` which send all but finitely many `(a : α)` to `0`, under pointwise addition. In this file, it is defined as the abelianisation of the free group on `α`. All the constructions and theorems required to show the adjointness of the construction and the forgetful functor are proved in this file, but the category-theoretic adjunction statement is in `algebra.category.Group.adjunctions` . ## Main definitions Here we use the following variables: `(α β : Type*) (A : Type*) [add_comm_group A]` * `free_abelian_group α` : the free abelian group on a type `α`. As an abelian group it is `α →₀ ℤ`, the functions from `α` to `ℤ` such that all but finitely many elements get mapped to zero, however this is not how it is implemented. * `lift f : free_abelian_group α →+ A` : the group homomorphism induced by the map `f : α → A`. * `map (f : α → β) : free_abelian_group α →+ free_abelian_group β` : functoriality of `free_abelian_group` * `instance [monoid α] : semigroup (free_abelian_group α)` * `instance [comm_monoid α] : comm_ring (free_abelian_group α)` It has been suggested that we would be better off refactoring this file and using `finsupp` instead. ## Implementation issues The definition is `def free_abelian_group : Type u := additive $ abelianization $ free_group α` Chris Hughes has suggested that this all be rewritten in terms of `finsupp`. Johan Commelin has written all the API relating the definition to `finsupp` in the lean-liquid repo. The lemmas `map_pure`, `map_of`, `map_zero`, `map_add`, `map_neg` and `map_sub` are proved about the `functor.map` `<$>` construction, and need `α` and `β` to be in the same universe. But `free_abelian_group.map (f : α → β)` is defined to be the `add_group` homomorphism `free_abelian_group α →+ free_abelian_group β` (with `α` and `β` now allowed to be in different universes), so `(map f).map_add` etc can be used to prove that `free_abelian_group.map` preserves addition. The functions `map_id`, `map_id_apply`, `map_comp`, `map_comp_apply` and `map_of_apply` are about `free_abelian_group.map`. -/ universes u v variables (α : Type u) /-- The free abelian group on a type. -/ def free_abelian_group : Type u := additive $ abelianization $ free_group α instance : add_comm_group (free_abelian_group α) := @additive.add_comm_group _ $ abelianization.comm_group _ instance : inhabited (free_abelian_group α) := ⟨0⟩ variable {α} namespace free_abelian_group /-- The canonical map from α to `free_abelian_group α` -/ def of (x : α) : free_abelian_group α := abelianization.of $ free_group.of x /-- The map `free_abelian_group α →+ A` induced by a map of types `α → A`. -/ def lift {β : Type v} [add_comm_group β] : (α → β) ≃ (free_abelian_group α →+ β) := (@free_group.lift _ (multiplicative β) _).trans $ (@abelianization.lift _ _ (multiplicative β) _).trans monoid_hom.to_additive namespace lift variables {β : Type v} [add_comm_group β] (f : α → β) open free_abelian_group @[simp] protected lemma of (x : α) : lift f (of x) = f x := begin convert @abelianization.lift.of (free_group α) _ (multiplicative β) _ _ _, convert free_group.lift.of.symm end protected theorem unique (g : free_abelian_group α →+ β) (hg : ∀ x, g (of x) = f x) {x} : g x = lift f x := add_monoid_hom.congr_fun ((lift.symm_apply_eq).mp (funext hg : g ∘ of = f)) _ /-- See note [partially-applied ext lemmas]. -/ @[ext] protected theorem ext (g h : free_abelian_group α →+ β) (H : ∀ x, g (of x) = h (of x)) : g = h := lift.symm.injective $ funext H lemma map_hom {α β γ} [add_comm_group β] [add_comm_group γ] (a : free_abelian_group α) (f : α → β) (g : β →+ γ) : g (lift f a) = lift (g ∘ f) a := begin suffices : (g.comp (lift f)) a = lift (g ∘ f) a, exact this, apply @lift.unique, assume a, show g ((lift f) (of a)) = g (f a), simp only [(∘), lift.of], end end lift section open_locale classical lemma of_injective : function.injective (of : α → free_abelian_group α) := λ x y hoxy, classical.by_contradiction $ assume hxy : x ≠ y, let f : free_abelian_group α →+ ℤ := lift (λ z, if x = z then (1 : ℤ) else 0) in have hfx1 : f (of x) = 1, from (lift.of _ _).trans $ if_pos rfl, have hfy1 : f (of y) = 1, from hoxy ▸ hfx1, have hfy0 : f (of y) = 0, from (lift.of _ _).trans $ if_neg hxy, one_ne_zero $ hfy1.symm.trans hfy0 end local attribute [instance] quotient_group.left_rel @[elab_as_eliminator] protected theorem induction_on {C : free_abelian_group α → Prop} (z : free_abelian_group α) (C0 : C 0) (C1 : ∀ x, C $ of x) (Cn : ∀ x, C (of x) → C (-of x)) (Cp : ∀ x y, C x → C y → C (x + y)) : C z := quotient.induction_on' z $ λ x, quot.induction_on x $ λ L, list.rec_on L C0 $ λ ⟨x, b⟩ tl ih, bool.rec_on b (Cp _ _ (Cn _ (C1 x)) ih) (Cp _ _ (C1 x) ih) theorem lift.add' {α β} [add_comm_group β] (a : free_abelian_group α) (f g : α → β) : lift (f + g) a = lift f a + lift g a := begin refine free_abelian_group.induction_on a _ _ _ _, { simp only [(lift _).map_zero, zero_add] }, { assume x, simp only [lift.of, pi.add_apply] }, { assume x h, simp only [(lift _).map_neg, lift.of, pi.add_apply, neg_add] }, { assume x y hx hy, simp only [(lift _).map_add, hx, hy], ac_refl } end instance is_add_group_hom_lift' {α} (β) [add_comm_group β] (a : free_abelian_group α) : is_add_group_hom (λf, (lift f a : β)) := { map_add := λ f g, lift.add' a f g } section monad variables {β : Type u} instance : monad free_abelian_group.{u} := { pure := λ α, of, bind := λ α β x f, lift f x } @[elab_as_eliminator] protected theorem induction_on' {C : free_abelian_group α → Prop} (z : free_abelian_group α) (C0 : C 0) (C1 : ∀ x, C $ pure x) (Cn : ∀ x, C (pure x) → C (-pure x)) (Cp : ∀ x y, C x → C y → C (x + y)) : C z := free_abelian_group.induction_on z C0 C1 Cn Cp @[simp] lemma map_pure (f : α → β) (x : α) : f <$> (pure x : free_abelian_group α) = pure (f x) := rfl @[simp] lemma map_zero (f : α → β) : f <$> (0 : free_abelian_group α) = 0 := (lift (of ∘ f)).map_zero @[simp] lemma map_add (f : α → β) (x y : free_abelian_group α) : f <$> (x + y) = f <$> x + f <$> y := (lift _).map_add _ _ @[simp] lemma map_neg (f : α → β) (x : free_abelian_group α) : f <$> (-x) = -(f <$> x) := (lift _).map_neg _ @[simp] lemma map_sub (f : α → β) (x y : free_abelian_group α) : f <$> (x - y) = f <$> x - f <$> y := (lift _).map_sub _ _ @[simp] lemma map_of (f : α → β) (y : α) : f <$> of y = of (f y) := rfl @[simp] lemma pure_bind (f : α → free_abelian_group β) (x) : pure x >>= f = f x := lift.of _ _ @[simp] lemma zero_bind (f : α → free_abelian_group β) : 0 >>= f = 0 := (lift f).map_zero @[simp] lemma add_bind (f : α → free_abelian_group β) (x y : free_abelian_group α) : x + y >>= f = (x >>= f) + (y >>= f) := (lift _).map_add _ _ @[simp] lemma neg_bind (f : α → free_abelian_group β) (x : free_abelian_group α) : -x >>= f = -(x >>= f) := (lift _).map_neg _ @[simp] lemma sub_bind (f : α → free_abelian_group β) (x y : free_abelian_group α) : x - y >>= f = (x >>= f) - (y >>= f) := (lift _).map_sub _ _ @[simp] lemma pure_seq (f : α → β) (x : free_abelian_group α) : pure f <*> x = f <$> x := pure_bind _ _ @[simp] lemma zero_seq (x : free_abelian_group α) : (0 : free_abelian_group (α → β)) <*> x = 0 := zero_bind _ @[simp] lemma add_seq (f g : free_abelian_group (α → β)) (x : free_abelian_group α) : f + g <*> x = (f <*> x) + (g <*> x) := add_bind _ _ _ @[simp] lemma neg_seq (f : free_abelian_group (α → β)) (x : free_abelian_group α) : -f <*> x = -(f <*> x) := neg_bind _ _ @[simp] lemma sub_seq (f g : free_abelian_group (α → β)) (x : free_abelian_group α) : f - g <*> x = (f <*> x) - (g <*> x) := sub_bind _ _ _ instance is_add_group_hom_seq (f : free_abelian_group (α → β)) : is_add_group_hom ((<*>) f) := { map_add := λ x y, show lift (<$> (x+y)) _ = _, by simp only [map_add]; exact @@is_add_hom.map_add _ _ _ (@@free_abelian_group.is_add_group_hom_lift' (free_abelian_group β) _ _).to_is_add_hom _ _ } @[simp] lemma seq_zero (f : free_abelian_group (α → β)) : f <*> 0 = 0 := is_add_group_hom.map_zero _ @[simp] lemma seq_add (f : free_abelian_group (α → β)) (x y : free_abelian_group α) : f <*> (x + y) = (f <*> x) + (f <*> y) := is_add_hom.map_add _ _ _ @[simp] lemma seq_neg (f : free_abelian_group (α → β)) (x : free_abelian_group α) : f <*> (-x) = -(f <*> x) := is_add_group_hom.map_neg _ _ @[simp] lemma seq_sub (f : free_abelian_group (α → β)) (x y : free_abelian_group α) : f <*> (x - y) = (f <*> x) - (f <*> y) := is_add_group_hom.map_sub _ _ _ instance : is_lawful_monad free_abelian_group.{u} := { id_map := λ α x, free_abelian_group.induction_on' x (map_zero id) (λ x, map_pure id x) (λ x ih, by rw [map_neg, ih]) (λ x y ihx ihy, by rw [map_add, ihx, ihy]), pure_bind := λ α β x f, pure_bind f x, bind_assoc := λ α β γ x f g, free_abelian_group.induction_on' x (by iterate 3 { rw zero_bind }) (λ x, by iterate 2 { rw pure_bind }) (λ x ih, by iterate 3 { rw neg_bind }; rw ih) (λ x y ihx ihy, by iterate 3 { rw add_bind }; rw [ihx, ihy]) } instance : is_comm_applicative free_abelian_group.{u} := { commutative_prod := λ α β x y, free_abelian_group.induction_on' x (by rw [map_zero, zero_seq, seq_zero]) (λ p, by rw [map_pure, pure_seq]; exact free_abelian_group.induction_on' y (by rw [map_zero, map_zero, zero_seq]) (λ q, by rw [map_pure, map_pure, pure_seq, map_pure]) (λ q ih, by rw [map_neg, map_neg, neg_seq, ih]) (λ y₁ y₂ ih1 ih2, by rw [map_add, map_add, add_seq, ih1, ih2])) (λ p ih, by rw [map_neg, neg_seq, seq_neg, ih]) (λ x₁ x₂ ih1 ih2, by rw [map_add, add_seq, seq_add, ih1, ih2]) } end monad universe w variables {β : Type v} {γ : Type w} /-- The additive group homomorphism `free_abelian_group α →+ free_abelian_group β` induced from a map `α → β` -/ def map (f : α → β) : free_abelian_group α →+ free_abelian_group β := lift (of ∘ f) lemma lift_comp {α} {β} {γ} [add_comm_group γ] (f : α → β) (g : β → γ) (x : free_abelian_group α) : lift (g ∘ f) x = lift g (map f x) := begin apply free_abelian_group.induction_on x, { exact add_monoid_hom.map_zero _ }, { intro y, refl }, { intros x h, simp only [h, add_monoid_hom.map_neg] }, { intros x y h₁ h₂, simp only [h₁, h₂, add_monoid_hom.map_add] } end lemma map_id : map id = add_monoid_hom.id (free_abelian_group α) := eq.symm $ lift.ext _ _ $ λ x, lift.unique of (add_monoid_hom.id _) $ λ y, add_monoid_hom.id_apply _ _ lemma map_id_apply (x : free_abelian_group α) : map id x = x := by {rw map_id, refl } lemma map_comp {f : α → β} {g : β → γ} : map (g ∘ f) = (map g).comp (map f) := eq.symm $ lift.ext _ _ $ λ x, eq.symm $ lift_comp _ _ _ lemma map_comp_apply {f : α → β} {g : β → γ} (x : free_abelian_group α) : map (g ∘ f) x = (map g) ((map f) x) := by { rw map_comp, refl } -- version of map_of which uses `map` lemma map_of_apply {f : α → β} (a : α) : map f (of a) = of (f a) := rfl variable (α) section monoid variables {R : Type*} [monoid α] [ring R] instance : semigroup (free_abelian_group α) := { mul := λ x, lift $ λ x₂, lift (λ x₁, of $ x₁ * x₂) x, mul_assoc := λ x y z, begin unfold has_mul.mul, refine free_abelian_group.induction_on z (by simp) _ _ _, { intros L3, rw [lift.of, lift.of], refine free_abelian_group.induction_on y (by simp) _ _ _, { intros L2, iterate 3 { rw lift.of }, refine free_abelian_group.induction_on x (by simp) _ _ _, { intros L1, iterate 3 { rw lift.of }, congr' 1, exact mul_assoc _ _ _ }, { intros L1 ih, iterate 3 { rw (lift _).map_neg }, rw ih }, { intros x1 x2 ih1 ih2, iterate 3 { rw (lift _).map_add }, rw [ih1, ih2] } }, { intros L2 ih, iterate 4 { rw (lift _).map_neg }, rw ih }, { intros y1 y2 ih1 ih2, iterate 4 { rw (lift _).map_add }, rw [ih1, ih2] } }, { intros L3 ih, iterate 3 { rw (lift _).map_neg }, rw ih }, { intros z1 z2 ih1 ih2, iterate 2 { rw (lift _).map_add }, rw [ih1, ih2], exact ((lift _).map_add _ _).symm } end } variable {α} lemma mul_def (x y : free_abelian_group α) : x * y = lift (λ x₂, lift (λ x₁, of (x₁ * x₂)) x) y := rfl lemma of_mul_of (x y : α) : of x * of y = of (x * y) := rfl lemma of_mul (x y : α) : of (x * y) = of x * of y := rfl variable (α) instance : ring (free_abelian_group α) := { one := free_abelian_group.of 1, mul_one := λ x, begin unfold has_mul.mul semigroup.mul has_one.one, rw lift.of, refine free_abelian_group.induction_on x rfl _ _ _, { intros L, erw [lift.of], congr' 1, exact mul_one L }, { intros L ih, rw [(lift _).map_neg, ih] }, { intros x1 x2 ih1 ih2, rw [(lift _).map_add, ih1, ih2] } end, one_mul := λ x, begin unfold has_mul.mul semigroup.mul has_one.one, refine free_abelian_group.induction_on x rfl _ _ _, { intros L, rw [lift.of, lift.of], congr' 1, exact one_mul L }, { intros L ih, rw [(lift _).map_neg, ih] }, { intros x1 x2 ih1 ih2, rw [(lift _).map_add, ih1, ih2] } end, left_distrib := λ x y z, (lift _).map_add _ _, right_distrib := λ x y z, begin unfold has_mul.mul semigroup.mul, refine free_abelian_group.induction_on z rfl _ _ _, { intros L, iterate 3 { rw lift.of }, rw (lift _).map_add, refl }, { intros L ih, iterate 3 { rw (lift _).map_neg }, rw [ih, neg_add], refl }, { intros z1 z2 ih1 ih2, iterate 3 { rw (lift _).map_add }, rw [ih1, ih2], rw [add_assoc, add_assoc], congr' 1, apply add_left_comm } end, .. free_abelian_group.add_comm_group α, .. free_abelian_group.semigroup α } variable {α} /-- `free_abelian_group.of` is a `monoid_hom` when `α` is a `monoid`. -/ def of_mul_hom : α →* free_abelian_group α := { to_fun := of, map_one' := rfl, map_mul' := of_mul } @[simp] lemma of_mul_hom_coe : (of_mul_hom : α → free_abelian_group α) = of := rfl /-- If `f` preserves multiplication, then so does `lift f`. -/ def lift_monoid : (α →* R) ≃ (free_abelian_group α →+* R) := { to_fun := λ f, { map_one' := (lift.of f _).trans f.map_one, map_mul' := λ x y, begin simp only [add_monoid_hom.to_fun_eq_coe], refine free_abelian_group.induction_on y (mul_zero _).symm _ _ _, { intros L2, rw mul_def x, simp only [lift.of], refine free_abelian_group.induction_on x (zero_mul _).symm _ _ _, { intros L1, iterate 3 { rw lift.of }, exact f.map_mul _ _ }, { intros L1 ih, iterate 3 { rw (lift _).map_neg }, rw [ih, neg_mul_eq_neg_mul] }, { intros x1 x2 ih1 ih2, iterate 3 { rw (lift _).map_add }, rw [ih1, ih2, add_mul] } }, { intros L2 ih, rw [mul_neg_eq_neg_mul_symm, add_monoid_hom.map_neg, add_monoid_hom.map_neg, mul_neg_eq_neg_mul_symm, ih] }, { intros y1 y2 ih1 ih2, rw [mul_add, add_monoid_hom.map_add, add_monoid_hom.map_add, mul_add, ih1, ih2] }, end, .. lift f }, inv_fun := λ F, monoid_hom.comp ↑F of_mul_hom, left_inv := λ f, monoid_hom.ext $ lift.of _, right_inv := λ F, ring_hom.coe_add_monoid_hom_injective $ lift.apply_symm_apply (↑F : free_abelian_group α →+ R) } @[simp] lemma lift_monoid_coe_add_monoid_hom (f : α →* R) : ↑(lift_monoid f) = lift f := rfl @[simp] lemma lift_monoid_coe (f : α →* R) : ⇑(lift_monoid f) = lift f := rfl @[simp] lemma lift_monoid_symm_coe (f : free_abelian_group α →+* R) : ⇑(lift_monoid.symm f) = lift.symm ↑f := rfl lemma one_def : (1 : free_abelian_group α) = of 1 := rfl lemma of_one : (of 1 : free_abelian_group α) = 1 := rfl end monoid instance [comm_monoid α] : comm_ring (free_abelian_group α) := { mul_comm := λ x y, begin refine free_abelian_group.induction_on x (zero_mul y) _ _ _, { intros s, refine free_abelian_group.induction_on y (zero_mul _).symm _ _ _, { intros t, unfold has_mul.mul semigroup.mul ring.mul, iterate 4 { rw lift.of }, congr' 1, exact mul_comm _ _ }, { intros t ih, rw [mul_neg_eq_neg_mul_symm, ih, neg_mul_eq_neg_mul] }, { intros y1 y2 ih1 ih2, rw [mul_add, add_mul, ih1, ih2] } }, { intros s ih, rw [neg_mul_eq_neg_mul_symm, ih, neg_mul_eq_mul_neg] }, { intros x1 x2 ih1 ih2, rw [add_mul, mul_add, ih1, ih2] } end, .. free_abelian_group.ring α } instance pempty_unique : unique (free_abelian_group pempty) := { default := 0, uniq := λ x, free_abelian_group.induction_on x rfl (λ x, pempty.elim x) (λ x, pempty.elim x) (by { rintros - - rfl rfl, simp }) } /-- The free abelian group on a type with one term is isomorphic to `ℤ`. -/ def punit_equiv (T : Type*) [unique T] : free_abelian_group T ≃+ ℤ := { to_fun := free_abelian_group.lift (λ _, (1 : ℤ)), inv_fun := λ n, n • of (inhabited.default T), left_inv := λ z, free_abelian_group.induction_on z (by simp only [zero_smul, add_monoid_hom.map_zero]) (unique.forall_iff.2 $ by simp only [one_smul, lift.of]) (unique.forall_iff.2 $ by simp) (λ x y hx hy, by { simp only [add_monoid_hom.map_add, add_smul] at *, rw [hx, hy]}), right_inv := λ n, begin rw [add_monoid_hom.map_int_module_smul, lift.of], exact gsmul_int_one n end, map_add' := add_monoid_hom.map_add _ } /-- Isomorphic types have isomorphic free abelian groups. -/ def equiv_of_equiv {α β : Type*} (f : α ≃ β) : free_abelian_group α ≃+ free_abelian_group β := { to_fun := map f, inv_fun := map f.symm, left_inv := begin intros x, rw [← map_comp_apply, equiv.symm_comp_self, map_id], refl, end, right_inv := begin intros x, rw [← map_comp_apply, equiv.self_comp_symm, map_id], refl, end, map_add' := add_monoid_hom.map_add _ } end free_abelian_group
e46f010719274baa56f1db08c78287b1bef9ba4e
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Data/Options.lean
ad8498e6695616568d239b7e0999218eebfd08dd
[ "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
5,429
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich and Leonardo de Moura -/ import Lean.ImportingFlag import Lean.Data.KVMap namespace Lean def Options := KVMap def Options.empty : Options := {} instance : Inhabited Options where default := {} instance : ToString Options := inferInstanceAs (ToString KVMap) instance : ForIn m Options (Name × DataValue) := inferInstanceAs (ForIn _ KVMap _) structure OptionDecl where defValue : DataValue group : String := "" descr : String := "" deriving Inhabited def OptionDecls := NameMap OptionDecl instance : Inhabited OptionDecls := ⟨({} : NameMap OptionDecl)⟩ private builtin_initialize optionDeclsRef : IO.Ref OptionDecls ← IO.mkRef (mkNameMap OptionDecl) @[export lean_register_option] def registerOption (name : Name) (decl : OptionDecl) : IO Unit := do unless (← initializing) do throw (IO.userError "failed to register option, options can only be registered during initialization") let decls ← optionDeclsRef.get if decls.contains name then throw $ IO.userError s!"invalid option declaration '{name}', option already exists" optionDeclsRef.set $ decls.insert name decl def getOptionDecls : IO OptionDecls := optionDeclsRef.get @[export lean_get_option_decls_array] def getOptionDeclsArray : IO (Array (Name × OptionDecl)) := do let decls ← getOptionDecls pure $ decls.fold (fun (r : Array (Name × OptionDecl)) k v => r.push (k, v)) #[] def getOptionDecl (name : Name) : IO OptionDecl := do let decls ← getOptionDecls let (some decl) ← pure (decls.find? name) | throw $ IO.userError s!"unknown option '{name}'" pure decl def getOptionDefaulValue (name : Name) : IO DataValue := do let decl ← getOptionDecl name pure decl.defValue def getOptionDescr (name : Name) : IO String := do let decl ← getOptionDecl name pure decl.descr def setOptionFromString (opts : Options) (entry : String) : IO Options := do let ps := (entry.splitOn "=").map String.trim let [key, val] ← pure ps | throw $ IO.userError "invalid configuration option entry, it must be of the form '<key> = <value>'" let key := Name.mkSimple key let defValue ← getOptionDefaulValue key match defValue with | DataValue.ofString v => pure $ opts.setString key val | DataValue.ofBool v => if key == `true then pure $ opts.setBool key true else if key == `false then pure $ opts.setBool key false else throw $ IO.userError s!"invalid Bool option value '{val}'" | DataValue.ofName v => pure $ opts.setName key val.toName | DataValue.ofNat v => match val.toNat? with | none => throw (IO.userError s!"invalid Nat option value '{val}'") | some v => pure $ opts.setNat key v | DataValue.ofInt v => match val.toInt? with | none => throw (IO.userError s!"invalid Int option value '{val}'") | some v => pure $ opts.setInt key v | DataValue.ofSyntax _ => throw (IO.userError s!"invalid Syntax option value") class MonadOptions (m : Type → Type) where getOptions : m Options export MonadOptions (getOptions) instance [MonadLift m n] [MonadOptions m] : MonadOptions n where getOptions := liftM (getOptions : m _) variable [Monad m] [MonadOptions m] def getBoolOption (k : Name) (defValue := false) : m Bool := do let opts ← getOptions return opts.getBool k defValue def getNatOption (k : Name) (defValue := 0) : m Nat := do let opts ← getOptions return opts.getNat k defValue class MonadWithOptions (m : Type → Type) where withOptions (f : Options → Options) (x : m α) : m α export MonadWithOptions (withOptions) instance [MonadFunctor m n] [MonadWithOptions m] : MonadWithOptions n where withOptions f x := monadMap (m := m) (withOptions f) x /-- A strongly-typed reference to an option. -/ protected structure Option (α : Type) where name : Name defValue : α deriving Inhabited namespace Option protected structure Decl (α : Type) where defValue : α group : String := "" descr : String := "" protected def get? [KVMap.Value α] (opts : Options) (opt : Lean.Option α) : Option α := opts.get? opt.name protected def get [KVMap.Value α] (opts : Options) (opt : Lean.Option α) : α := opts.get opt.name opt.defValue protected def set [KVMap.Value α] (opts : Options) (opt : Lean.Option α) (val : α) : Options := opts.set opt.name val /-- Similar to `set`, but update `opts` only if it doesn't already contains an setting for `opt.name` -/ protected def setIfNotSet [KVMap.Value α] (opts : Options) (opt : Lean.Option α) (val : α) : Options := if opts.contains opt.name then opts else opt.set opts val protected def register [KVMap.Value α] (name : Name) (decl : Lean.Option.Decl α) : IO (Lean.Option α) := do registerOption name { defValue := KVMap.Value.toDataValue decl.defValue, group := decl.group, descr := decl.descr } return { name := name, defValue := decl.defValue } macro "register_builtin_option" name:ident " : " type:term " := " decl:term : command => `(builtin_initialize $name : Lean.Option $type ← Lean.Option.register $(quote name.getId) $decl) macro "register_option" name:ident " : " type:term " := " decl:term : command => `(initialize $name : Lean.Option $type ← Lean.Option.register $(quote name.getId) $decl) end Option end Lean
87573dd02f7de76836b2e9ecda267b6436b80ad2
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/category_theory/functor.lean
664f21bf590358af322920e1ac9b130ff9e2ced0
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
3,460
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison Defines a functor between categories. (As it is a 'bundled' object rather than the `is_functorial` typeclass parametrised by the underlying function on objects, the name is capitalised.) Introduces notations `C ⥤ D` for the type of all functors from `C` to `D`. (I would like a better arrow here, unfortunately ⇒ (`\functor`) is taken by core.) -/ import category_theory.category namespace category_theory universes v v₁ v₂ v₃ u u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation /-- `functor C D` represents a functor between categories `C` and `D`. To apply a functor `F` to an object use `F.obj X`, and to a morphism use `F.map f`. The axiom `map_id_lemma` expresses preservation of identities, and `map_comp_lemma` expresses functoriality. -/ structure functor (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] : Type (max v₁ v₂ u₁ u₂) := (obj : C → D) (map : Π {X Y : C}, (X ⟶ Y) → ((obj X) ⟶ (obj Y))) (map_id' : ∀ (X : C), map (𝟙 X) = 𝟙 (obj X) . obviously) (map_comp' : ∀ {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z), map (f ≫ g) = (map f) ≫ (map g) . obviously) -- A functor is basically a function, so give ⥤ a similar precedence to → (25). -- For example, `C × D ⥤ E` should parse as `(C × D) ⥤ E` not `C × (D ⥤ E)`. infixr ` ⥤ `:26 := functor -- type as \func -- restate_axiom functor.map_id' attribute [simp] functor.map_id restate_axiom functor.map_comp' attribute [simp] functor.map_comp namespace functor section variables (C : Type u₁) [𝒞 : category.{v₁} C] include 𝒞 /-- `𝟭 C` is the identity functor on a category `C`. -/ protected def id : C ⥤ C := { obj := λ X, X, map := λ _ _ f, f } notation `𝟭` := functor.id variable {C} @[simp] lemma id_obj (X : C) : (𝟭 C).obj X = X := rfl @[simp] lemma id_map {X Y : C} (f : X ⟶ Y) : (𝟭 C).map f = f := rfl end section variables {C : Type u₁} [𝒞 : category.{v₁} C] {D : Type u₂} [𝒟 : category.{v₂} D] {E : Type u₃} [ℰ : category.{v₃} E] include 𝒞 𝒟 ℰ /-- `F ⋙ G` is the composition of a functor `F` and a functor `G` (`F` first, then `G`). -/ def comp (F : C ⥤ D) (G : D ⥤ E) : C ⥤ E := { obj := λ X, G.obj (F.obj X), map := λ _ _ f, G.map (F.map f) } infixr ` ⋙ `:80 := comp @[simp] lemma comp_obj (F : C ⥤ D) (G : D ⥤ E) (X : C) : (F ⋙ G).obj X = G.obj (F.obj X) := rfl @[simp] lemma comp_map (F : C ⥤ D) (G : D ⥤ E) (X Y : C) (f : X ⟶ Y) : (F ⋙ G).map f = G.map (F.map f) := rfl omit ℰ -- These are not simp lemmas because rewriting along equalities between functors -- is not necessarily a good idea. -- Natural isomorphisms are also provided in `whiskering.lean`. protected lemma comp_id (F : C ⥤ D) : F ⋙ (𝟭 D) = F := by cases F; refl protected lemma id_comp (F : C ⥤ D) : (𝟭 C) ⋙ F = F := by cases F; refl end section variables (C : Type u₁) [𝒞 : category.{v₁} C] include 𝒞 @[simp] def ulift_down : (ulift.{u₂} C) ⥤ C := { obj := λ X, X.down, map := λ X Y f, f } @[simp] def ulift_up : C ⥤ (ulift.{u₂} C) := { obj := λ X, ⟨ X ⟩, map := λ X Y f, f } end end functor end category_theory
0d756ac11dd26d9c390a599e1311f7835c2fd586
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/monad/coequalizer.lean
8e08c1c3ff8c64b0094249b3598c995d6079875e
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,990
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.shapes.reflexive import category_theory.limits.shapes.split_coequalizer import category_theory.monad.algebra /-! # Special coequalizers associated to a monad Associated to a monad `T : C ⥤ C` we have important coequalizer constructions: Any algebra is a coequalizer (in the category of algebras) of free algebras. Furthermore, this coequalizer is reflexive. In `C`, this cofork diagram is a split coequalizer (in particular, it is still a coequalizer). This split coequalizer is known as the Beck coequalizer (as it features heavily in Beck's monadicity theorem). -/ universes v₁ u₁ namespace category_theory namespace monad open limits variables {C : Type u₁} variables [category.{v₁} C] variables {T : monad C} (X : algebra T) /-! Show that any algebra is a coequalizer of free algebras. -/ /-- The top map in the coequalizer diagram we will construct. -/ @[simps] def free_coequalizer.top_map : (monad.free T).obj (T.obj X.A) ⟶ (monad.free T).obj X.A := (monad.free T).map X.a /-- The bottom map in the coequalizer diagram we will construct. -/ @[simps] def free_coequalizer.bottom_map : (monad.free T).obj (T.obj X.A) ⟶ (monad.free T).obj X.A := { f := T.μ.app X.A, h' := T.assoc X.A } /-- The cofork map in the coequalizer diagram we will construct. -/ @[simps] def free_coequalizer.π : (monad.free T).obj X.A ⟶ X := { f := X.a, h' := X.assoc.symm } lemma free_coequalizer.condition : free_coequalizer.top_map X ≫ free_coequalizer.π X = free_coequalizer.bottom_map X ≫ free_coequalizer.π X := algebra.hom.ext _ _ X.assoc.symm instance : is_reflexive_pair (free_coequalizer.top_map X) (free_coequalizer.bottom_map X) := begin apply is_reflexive_pair.mk' _ _ _, apply (free T).map (T.η.app X.A), { ext, dsimp, rw [← functor.map_comp, X.unit, functor.map_id] }, { ext, apply monad.right_unit } end /-- Construct the Beck cofork in the category of algebras. This cofork is reflexive as well as a coequalizer. -/ @[simps] def beck_algebra_cofork : cofork (free_coequalizer.top_map X) (free_coequalizer.bottom_map X) := cofork.of_π _ (free_coequalizer.condition X) /-- The cofork constructed is a colimit. This shows that any algebra is a (reflexive) coequalizer of free algebras. -/ def beck_algebra_coequalizer : is_colimit (beck_algebra_cofork X) := cofork.is_colimit.mk' _ $ λ s, begin have h₁ : (T : C ⥤ C).map X.a ≫ s.π.f = T.μ.app X.A ≫ s.π.f := congr_arg monad.algebra.hom.f s.condition, have h₂ : (T : C ⥤ C).map s.π.f ≫ s.X.a = T.μ.app X.A ≫ s.π.f := s.π.h, refine ⟨⟨T.η.app _ ≫ s.π.f, _⟩, _, _⟩, { dsimp, rw [functor.map_comp, category.assoc, h₂, monad.right_unit_assoc, (show X.a ≫ _ ≫ _ = _, from T.η.naturality_assoc _ _), h₁, monad.left_unit_assoc] }, { ext, simpa [← T.η.naturality_assoc, T.left_unit_assoc] using T.η.app ((T : C ⥤ C).obj X.A) ≫= h₁ }, { intros m hm, ext, dsimp only, rw ← hm, apply (X.unit_assoc _).symm } end /-- The Beck cofork is a split coequalizer. -/ def beck_split_coequalizer : is_split_coequalizer (T.map X.a) (T.μ.app _) X.a := ⟨T.η.app _, T.η.app _, X.assoc.symm, X.unit, T.left_unit _, (T.η.naturality _).symm⟩ /-- This is the Beck cofork. It is a split coequalizer, in particular a coequalizer. -/ @[simps X] def beck_cofork : cofork (T.map X.a) (T.μ.app _) := (beck_split_coequalizer X).as_cofork @[simp] lemma beck_cofork_π : (beck_cofork X).π = X.a := rfl /-- The Beck cofork is a coequalizer. -/ def beck_coequalizer : is_colimit (beck_cofork X) := (beck_split_coequalizer X).is_coequalizer @[simp] lemma beck_coequalizer_desc (s : cofork (T.to_functor.map X.a) (T.μ.app X.A)) : (beck_coequalizer X).desc s = T.η.app _ ≫ s.π := rfl end monad end category_theory
c26b8b46c85685d612e30696b22a1ee881dacbe6
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Init/System/Promise.lean
dcbbe302a58e526c7466b443527bd51e8310248c
[ "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,862
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ prelude import Init.System.IO namespace IO /-- Internally, a `Promise` is just a `Task` that is in the "Promised" or "Finished" state. -/ private opaque PromiseImpl (α : Type) : { P : Type // Nonempty α ↔ Nonempty P } := ⟨Task α, fun ⟨_⟩ => ⟨⟨‹_›⟩⟩, fun ⟨⟨_⟩⟩ => ⟨‹_›⟩⟩ /-- `Promise α` allows you to create a `Task α` whose value is provided later by calling `resolve`. Typical usage is as follows: 1. `let promise ← Promise.new` creates a promise 2. `promise.result : Task α` can now be passed around 3. `promise.result.get` blocks until the promise is resolved 4. `promise.resolve a` resolves the promise 5. `promise.result.get` now returns `a` Every promise must eventually be resolved. Otherwise the memory used for the promise will be leaked, and any tasks depending on the promise's result will wait forever. -/ def Promise (α : Type) : Type := (PromiseImpl α).1 instance [Nonempty α] : Nonempty (Promise α) := (PromiseImpl α).2.1 inferInstance /-- Creates a new `Promise`. -/ @[extern "lean_io_promise_new"] opaque Promise.new [Nonempty α] : BaseIO (Promise α) /-- Resolves a `Promise`. Only the first call to this function has an effect. -/ @[extern "lean_io_promise_resolve"] opaque Promise.resolve (value : α) (promise : @& Promise α) : BaseIO Unit private unsafe def Promise.resultImpl (promise : Promise α) : Task α := unsafeCast promise /-- The result task of a `Promise`. The task blocks until `Promise.resolve` is called. -/ @[implemented_by Promise.resultImpl] opaque Promise.result (promise : Promise α) : Task α := have : Nonempty α := (PromiseImpl α).2.2 ⟨promise⟩ Classical.choice inferInstance
3994a23717ab480168a49013fe014ccd1d82b19d
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/padics/default.lean
24308ab9d7da4fb97e05eecb47d6c31fab42a63f
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
33
lean
import data.padics.padic_integers
9fe0867bdf50c087c6e4d7de7575bbf8634e054c
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/algebra/algebra.lean
c5535d52c521c1e3bcb71dd5b907310878bf29d2
[ "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
3,973
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 algebra.algebra.subalgebra import topology.algebra.module /-! # Topological (sub)algebras A topological algebra over a topological semiring `R` is a topological ring with a compatible continuous scalar multiplication by elements of `R`. We reuse typeclass `has_continuous_smul` for topological algebras. ## Results This is just a minimal stub for now! The topological closure of a subalgebra is still a subalgebra, which as an algebra is a topological algebra. -/ open classical set topological_space algebra open_locale classical universes u v w section topological_algebra variables (R : Type*) [topological_space R] [comm_semiring R] variables (A : Type u) [topological_space A] variables [semiring A] lemma continuous_algebra_map_iff_smul [algebra R A] [topological_ring A] : continuous (algebra_map R A) ↔ continuous (λ p : R × A, p.1 • p.2) := begin refine ⟨λ h, _, λ h, _⟩, { simp only [algebra.smul_def], exact (h.comp continuous_fst).mul continuous_snd }, { rw algebra_map_eq_smul_one', exact h.comp (continuous_id.prod_mk continuous_const) } end @[continuity] lemma continuous_algebra_map [algebra R A] [topological_ring A] [has_continuous_smul R A] : continuous (algebra_map R A) := (continuous_algebra_map_iff_smul R A).2 continuous_smul lemma has_continuous_smul_of_algebra_map [algebra R A] [topological_ring A] (h : continuous (algebra_map R A)) : has_continuous_smul R A := ⟨(continuous_algebra_map_iff_smul R A).1 h⟩ end topological_algebra section topological_algebra variables {R : Type*} [comm_semiring R] variables {A : Type u} [topological_space A] variables [semiring A] variables [algebra R A] [topological_ring A] /-- The closure of a subalgebra in a topological algebra as a subalgebra. -/ def subalgebra.topological_closure (s : subalgebra R A) : subalgebra R A := { carrier := closure (s : set A), algebra_map_mem' := λ r, s.to_subsemiring.subring_topological_closure (s.algebra_map_mem r), .. s.to_subsemiring.topological_closure } @[simp] lemma subalgebra.topological_closure_coe (s : subalgebra R A) : (s.topological_closure : set A) = closure (s : set A) := rfl instance subalgebra.topological_closure_topological_ring (s : subalgebra R A) : topological_ring (s.topological_closure) := s.to_subsemiring.topological_closure_topological_ring instance subalgebra.topological_closure_topological_algebra [topological_space R] [has_continuous_smul R A] (s : subalgebra R A) : has_continuous_smul R (s.topological_closure) := s.to_submodule.topological_closure_has_continuous_smul lemma subalgebra.subalgebra_topological_closure (s : subalgebra R A) : s ≤ s.topological_closure := subset_closure lemma subalgebra.is_closed_topological_closure (s : subalgebra R A) : is_closed (s.topological_closure : set A) := by convert is_closed_closure lemma subalgebra.topological_closure_minimal (s : subalgebra R A) {t : subalgebra R A} (h : s ≤ t) (ht : is_closed (t : set A)) : s.topological_closure ≤ t := closure_minimal h ht /-- This is really a statement about topological algebra isomorphisms, but we don't have those, so we use the clunky approach of talking about an algebra homomorphism, and a separate homeomorphism, along with a witness that as functions they are the same. -/ lemma subalgebra.topological_closure_comap'_homeomorph (s : subalgebra R A) {B : Type*} [topological_space B] [ring B] [topological_ring B] [algebra R B] (f : B →ₐ[R] A) (f' : B ≃ₜ A) (w : (f : B → A) = f') : s.topological_closure.comap' f = (s.comap' f).topological_closure := begin apply set_like.ext', simp only [subalgebra.topological_closure_coe], simp only [subalgebra.coe_comap, subsemiring.coe_comap, alg_hom.coe_to_ring_hom], rw [w], exact f'.preimage_closure _, end end topological_algebra
81ebeccbcc483c75efc037b4529593b9da17f79f
eb9357a70318e50e095b58730bebfe0cffee457f
/lean/love10_denotational_semantics_demo.lean
ac9ea893c1e0878084f4ec828796a94effe4b543
[]
no_license
Vierkantor/logical_verification_2021
7485dd916953131d501760f023d5b30fbb74d36a
9500b9c194e22a9ab4067321cfed7a1f445afcfc
refs/heads/main
1,692,560,845,086
1,624,721,275,000
1,624,721,275,000
416,354,079
0
0
null
null
null
null
UTF-8
Lean
false
false
14,507
lean
import .love08_operational_semantics_demo /-! # LoVe Demo 10: Denotational Semantics We review a third way to specify the semantics of a programming language: denotational semantics. Denotational semantics attempt to directly specify the meaning of programs. If operational semantics is an idealized interpreter and Hoare logic is an idealized verifier, then denotational semantics is an idealized compiler. -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /-! ## Compositionality A __denotational semantics__ defines the meaning of each program as a mathematical object: `⟦ ⟧ : syntax → semantics` A key property of denotational semantics is __compositionality__: The meaning of a compound statement should be defined in terms of the meaning of its components. This disqualifies `⟦S⟧ = {st | (S, prod.fst st) ⟹ prod.snd st}` i.e. `⟦S⟧ = {(s, t) | (S, s) ⟹ t}` because operational semantics are not compositional. In short, we want `⟦S ; T⟧ = … ⟦S⟧ … ⟦T⟧ …` `⟦if b then S else T⟧ = … ⟦S⟧ … ⟦T⟧ …` `⟦while b do S⟧ = … ⟦S⟧ …` An evaluation function on arithmetic expressions `eval : aexp → ((string → ℤ) → ℤ)` is a denotational semantics. We want the same for imperative programs. ## A Relational Denotational Semantics We can represent the semantics of an imperative program as a function from initial state to final state or more generally as a relation between initial state and final state: `set (state × state)`. For `skip`, `:=`, `;`, and `if then else`, the denotational semantics is easy: -/ namespace sorry_defs def denote : stmt → set (state × state) | stmt.skip := Id | (stmt.assign n a) := {st | prod.snd st = (prod.fst st){n ↦ a (prod.fst st)}} | (stmt.seq S T) := denote S ◯ denote T | (stmt.ite b S T) := (denote S ⇃ b) ∪ (denote T ⇃ (λs, ¬ b s)) | (stmt.while b S) := sorry end sorry_defs /-! We write `⟦S⟧` for `denote S`. For `while`, we would like to write `((denote S ◯ denote (stmt.while b S)) ⇃ b) ∪ (Id ⇃ (λs, ¬ b s))` but this is ill-founded due to the recursive call on `stmt.while b S`. What we are looking for is an `X` such that `X = ((denote S ◯ X) ⇃ b) ∪ (Id ⇃ (λs, ¬ b s))` In other words, we are looking for a fixpoint. Most of this lecture is concerned with building a least fixpoint operator `lfp` that will allow us to define the `while` case as well: `lfp (λX, ((denote S ◯ X) ⇃ b) ∪ (Id ⇃ (λs, ¬ b s)))` ## Fixpoints A __fixpoint__ (or fixed point) of `f` is a solution for `X` in the equation `X = f X` In general, fixpoints may not exist at all (e.g., `f := nat.succ`), or there may be several fixpoints (e.g., `f := id`). But under some conditions on `f`, a unique __least fixpoint__ and a unique __greatest fixpoint__ are guaranteed to exist. Consider this __fixpoint equation__: `X = (λ(p : ℕ → Prop) (n : ℕ), n = 0 ∨ ∃m : ℕ, n = m + 2 ∧ p m) X` `= (λn : ℕ, n = 0 ∨ ∃m : ℕ, n = m + 2 ∧ X m)` where `X : ℕ → Prop` and `f := (λ(p : ℕ → Prop) (n : ℕ), n = 0 ∨ ∃m : ℕ, n = m + 2 ∧ p m)`. The above example admits only one fixpoint. The fixpoint equation uniquely specifies `X` as the set of even numbers. In general, the least and greatest fixpoint may be different: `X = X` Here, the least fixpoint is `(λ_, False)` and the greatest fixpoint is `(λ_, True)`. Conventionally, `False < True`, and thus `(λ_, False) < (λ_, True)`. Similarly, `∅ < @set.univ α` (assuming `α` is inhabited). For the semantics of programming languages: * `X` will have type `set (state × state)` (which is isomorphic to `state → state → Prop`), representing relations between states; * `f` will correspond to either taking one extra iteration of the loop (if the condition `b` is true) or the identity (if `b` is false). Kleene's fixpoint theorem: `f^0(∅) ∪ f^1(∅) ∪ f^2(∅) ∪ ⋯ = lfp f` The least fixpoint corresponds to finite executions of a program, which is all we care about. **Key observation**: Inductive predicates correspond to least fixpoints, but they are built into Lean's logic (the calculus of inductive constructions). ## Monotone Functions Let `α` and `β` be types with partial order `≤`. A function `f : α → β` is __monotone__ if `a ≤ b → f a ≤ f b` for all `a`, `b` Many operations on sets (e.g., `∪`), relations (e.g., `◯`), and functions (e.g., `λx, x`, `λ_, k`, `∘`) are monotone or preserve monotonicity. All monotone functions `f : set α → set α` admit least and greatest fixpoints. **Example of a nonmonotone function**: `f A = (if A = ∅ then set.univ else ∅)` Assuming `α` is inhabited, we have `∅ ⊆ set.univ`, but `f ∅ = set.univ ⊈ ∅ = f set.univ`. -/ def monotone {α β : Type} [partial_order α] [partial_order β] (f : α → β) : Prop := ∀a₁ a₂, a₁ ≤ a₂ → f a₁ ≤ f a₂ lemma monotone_id {α : Type} [partial_order α] : monotone (λa : α, a) := begin intros a₁ a₂ ha, exact ha end lemma monotone_const {α β : Type} [partial_order α] [partial_order β] (b : β) : monotone (λ_ : α, b) := begin intros a₁ a₂ ha, exact le_refl b end lemma monotone_union {α β : Type} [partial_order α] (f g : α → set β) (hf : monotone f) (hg : monotone g) : monotone (λa, f a ∪ g a) := begin intros a₁ a₂ ha b hb, cases' hb, { exact or.intro_left _ (hf a₁ a₂ ha h) }, { exact or.intro_right _ (hg a₁ a₂ ha h) } end /-! We will prove the following two lemmas in the exercise. -/ namespace sorry_lemmas lemma monotone_comp {α β : Type} [partial_order α] (f g : α → set (β × β)) (hf : monotone f) (hg : monotone g) : monotone (λa, f a ◯ g a) := sorry lemma monotone_restrict {α β : Type} [partial_order α] (f : α → set (β × β)) (p : β → Prop) (hf : monotone f) : monotone (λa, f a ⇃ p) := sorry end sorry_lemmas /-! ## Complete Lattices To define the least fixpoint on sets, we need `⊆` and `⋂`. Complete lattices capture this concept abstractly. A __complete lattice__ is an ordered type `α` for which each set of type `set α` has an infimum. More precisely, A complete lattice consists of * a partial order `≤ : α → α → Prop` (i.e., a reflexive, transitive, and antisymmetric binary predicate); * an operator `Inf : set α → α`, called __infimum__. Moreover, `Inf A` must satisfy these two properties: * `Inf A` is a lower bound of `A`: `Inf A ≤ b` for all `b ∈ A`; * `Inf A` is a greatest lower bound: `b ≤ Inf A` for all `b` such that `∀a, a ∈ A → b ≤ a`. **Warning:** `Inf A` is not necessarily an element of `A`. Examples: * `set α` is an instance w.r.t. `⊆` and `⋂` for all `α`; * `Prop` is an instance w.r.t. `→` and `∀` (`Inf A := ∀a ∈ A, a`); * `enat := ℕ ∪ {∞}`; * `ereal := ℝ ∪ {- ∞, ∞}`; * `β → α` if `α` is a complete lattice; * `α × β` if `α`, `β` are complete lattices. Finite example (with apologies for the ASCII art): Z Inf {} = ? / \ Inf {Z} = ? A B Inf {A, B} = ? \ / Inf {Z, A} = ? Y Inf {Z, A, B, Y} = ? Nonexamples: * `ℕ`, `ℤ`, `ℚ`, `ℝ`: no infimum for `∅`, `Inf ℕ`, etc. * `erat := ℚ ∪ {- ∞, ∞}`: `Inf {q | 2 < q * q} = sqrt 2` is not in `erat`. -/ @[class] structure complete_lattice (α : Type) extends partial_order α : Type := (Inf : set α → α) (Inf_le : ∀A b, b ∈ A → Inf A ≤ b) (le_Inf : ∀A b, (∀a, a ∈ A → b ≤ a) → b ≤ Inf A) /-! For sets: -/ @[instance] def set.complete_lattice {α : Type} : complete_lattice (set α) := { le := (⊆), le_refl := by tautology, le_trans := by tautology, le_antisymm := begin intros A B hAB hBA, apply set.ext, tautology end, Inf := λX, {a | ∀A, A ∈ X → a ∈ A}, Inf_le := by tautology, le_Inf := by tautology } /-! ## Least Fixpoint -/ def lfp {α : Type} [complete_lattice α] (f : α → α) : α := complete_lattice.Inf ({a | f a ≤ a}) lemma lfp_le {α : Type} [complete_lattice α] (f : α → α) (a : α) (h : f a ≤ a) : lfp f ≤ a := complete_lattice.Inf_le _ _ h lemma le_lfp {α : Type} [complete_lattice α] (f : α → α) (a : α) (h : ∀a', f a' ≤ a' → a ≤ a') : a ≤ lfp f := complete_lattice.le_Inf _ _ h /-! **Knaster-Tarski theorem:** For any monotone function `f`: * `lfp f` is a fixpoint: `lfp f = f (lfp f)` (lemma `lfp_eq`); * `lfp f` is smaller than any other fixpoint: `X = f X → lfp f ≤ X`. -/ lemma lfp_eq {α : Type} [complete_lattice α] (f : α → α) (hf : monotone f) : lfp f = f (lfp f) := begin have h : f (lfp f) ≤ lfp f := begin apply le_lfp, intros a' ha', apply @le_trans _ _ _ (f a'), { apply hf, apply lfp_le, assumption }, { assumption } end, apply le_antisymm, { apply lfp_le, apply hf, assumption }, { assumption } end /-! ## A Relational Denotational Semantics, Continued -/ def denote : stmt → set (state × state) | stmt.skip := Id | (stmt.assign x a) := {st | prod.snd st = (prod.fst st){x ↦ a (prod.fst st)}} | (stmt.seq S T) := denote S ◯ denote T | (stmt.ite b S T) := (denote S ⇃ b) ∪ (denote T ⇃ (λs, ¬ b s)) | (stmt.while b S) := lfp (λX, ((denote S ◯ X) ⇃ b) ∪ (Id ⇃ (λs, ¬ b s))) notation `⟦` S `⟧`:= denote S /-! ## Application to Program Equivalence Based on the denotational semantics, we introduce the notion of program equivalence: `S₁ ~ S₂`. (Compare with exercise 8.) -/ def denote_equiv (S₁ S₂ : stmt) : Prop := ⟦S₁⟧ = ⟦S₂⟧ infix ` ~ ` := denote_equiv /-! It is obvious from the definition that `~` is an equivalence relation. Program equivalence can be used to replace subprograms by other subprograms with the same semantics. This is achieved by the following congruence rules: -/ lemma denote_equiv.seq_congr {S₁ S₂ T₁ T₂ : stmt} (hS : S₁ ~ S₂) (hT : T₁ ~ T₂) : S₁ ;; T₁ ~ S₂ ;; T₂ := by simp [denote_equiv, denote, *] at * lemma denote_equiv.ite_congr {b} {S₁ S₂ T₁ T₂ : stmt} (hS : S₁ ~ S₂) (hT : T₁ ~ T₂) : stmt.ite b S₁ T₁ ~ stmt.ite b S₂ T₂ := by simp [denote_equiv, denote, *] at * lemma denote_equiv.while_congr {b} {S₁ S₂ : stmt} (hS : S₁ ~ S₂) : stmt.while b S₁ ~ stmt.while b S₂ := by simp [denote_equiv, denote, *] at * /-! Compare the simplicity of these proofs with the corresponding proofs for a big-step semantics (exercise 8). Let us prove some program equivalences. -/ lemma denote_equiv.skip_assign_id {x} : stmt.assign x (λs, s x) ~ stmt.skip := by simp [denote_equiv, denote, Id] lemma denote_equiv.seq_skip_left {S : stmt} : stmt.skip ;; S ~ S := by simp [denote_equiv, denote, Id, comp] lemma denote_equiv.seq_skip_right {S : stmt} : S ;; stmt.skip ~ S := by simp [denote_equiv, denote, Id, comp] lemma denote_equiv.ite_seq_while {b} {S : stmt} : stmt.ite b (S ;; stmt.while b S) stmt.skip ~ stmt.while b S := begin simp [denote_equiv, denote], apply eq.symm, apply lfp_eq, apply monotone_union, { apply sorry_lemmas.monotone_restrict, apply sorry_lemmas.monotone_comp, { exact monotone_const _ }, { exact monotone_id } }, { apply sorry_lemmas.monotone_restrict, exact monotone_const _ } end /-! ## Equivalence of the Denotational and the Big-Step Semantics ## (**optional**) -/ lemma denote_of_big_step (S : stmt) (s t : state) (h : (S, s) ⟹ t) : (s, t) ∈ ⟦S⟧ := begin induction' h; try { solve1 { simp [denote, *] } }, case seq { apply exists.intro t, exact and.intro ih_h ih_h_1 }, case while_true { rw eq.symm denote_equiv.ite_seq_while, simp [denote, *], apply exists.intro t, apply and.intro; assumption }, case while_false { rw eq.symm denote_equiv.ite_seq_while, simp [denote, *] } end lemma big_step_of_denote : ∀(S : stmt) (s t : state), (s, t) ∈ ⟦S⟧ → (S, s) ⟹ t | stmt.skip s t := by simp [denote] | (stmt.assign n a) s t := by simp [denote] | (stmt.seq S T) s t := begin intro h, cases' h with u hu, exact big_step.seq (big_step_of_denote S _ _ (and.elim_left hu)) (big_step_of_denote T _ _ (and.elim_right hu)) end | (stmt.ite b S T) s t := begin intro h, cases' h, case inl { cases' h, apply big_step.ite_true right, exact big_step_of_denote _ _ _ left }, case inr { cases' h, apply big_step.ite_false right, exact big_step_of_denote _ _ _ left } end | (stmt.while b S) s t := begin have hw : ⟦stmt.while b S⟧ ≤ {st | (stmt.while b S, prod.fst st) ⟹ prod.snd st} := begin apply lfp_le _ _ _, intros x hx, cases' x with s' t', simp at hx, cases' hx, case inl { cases' h with hst hs, cases' hst with u hu, apply big_step.while_true hs, { exact big_step_of_denote S _ _ (and.elim_left hu) }, { exact and.elim_right hu } }, case inr { cases' h, cases' left, apply big_step.while_false right } end, apply hw end lemma denote_iff_big_step (S : stmt) (s t : state) : (s, t) ∈ ⟦S⟧ ↔ (S, s) ⟹ t := iff.intro (big_step_of_denote S s t) (denote_of_big_step S s t) /-! ## A Simpler Approach Based on an Inductive Predicate (**optional**) -/ inductive awhile (b : state → Prop) (r : set (state × state)) : state → state → Prop | true {s t u} (hcond : b s) (hbody : (s, t) ∈ r) (hrest : awhile t u) : awhile s u | false {s} (hcond : ¬ b s) : awhile s s def denote_ind : stmt → set (state × state) | stmt.skip := Id | (stmt.assign x a) := {st | prod.snd st = (prod.fst st){x ↦ a (prod.fst st)}} | (stmt.seq S T) := denote_ind S ◯ denote_ind T | (stmt.ite b S T) := (denote_ind S ⇃ b) ∪ (denote_ind T ⇃ (λs, ¬ b s)) | (stmt.while b S) := {st | awhile b (denote_ind S) (prod.fst st) (prod.snd st)} end LoVe
24a718acd7972f9eb6e255cad92140709ca6b16b
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/list/sigma.lean
87240807b53649d6140175bbbaa35e72545598b3
[ "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
25,783
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Sean Leather -/ import data.list.perm import data.list.range import data.sigma /-! # Utilities for lists of sigmas This file includes several ways of interacting with `list (sigma β)`, treated as a key-value store. If `α : Type*` and `β : α → Type*`, then we regard `s : sigma β` as having key `s.1 : α` and value `s.2 : β s.1`. Hence, `list (sigma β)` behaves like a key-value store. ## Main Definitions - `list.keys` extracts the list of keys. - `list.nodupkeys` determines if the store has duplicate keys. - `list.lookup`/`lookup_all` accesses the value(s) of a particular key. - `list.kreplace` replaces the first value with a given key by a given value. - `list.kerase` removes a value. - `list.kinsert` inserts a value. - `list.kunion` computes the union of two stores. - `list.kextract` returns a value with a given key and the rest of the values. -/ universes u v namespace list variables {α : Type u} {β : α → Type v} /-! ### `keys` -/ /-- List of keys from a list of key-value pairs -/ def keys : list (sigma β) → list α := map sigma.fst @[simp] theorem keys_nil : @keys α β [] = [] := rfl @[simp] theorem keys_cons {s} {l : list (sigma β)} : (s :: l).keys = s.1 :: l.keys := rfl theorem mem_keys_of_mem {s : sigma β} {l : list (sigma β)} : s ∈ l → s.1 ∈ l.keys := mem_map_of_mem sigma.fst theorem exists_of_mem_keys {a} {l : list (sigma β)} (h : a ∈ l.keys) : ∃ (b : β a), sigma.mk a b ∈ l := let ⟨⟨a', b'⟩, m, e⟩ := exists_of_mem_map h in eq.rec_on e (exists.intro b' m) theorem mem_keys {a} {l : list (sigma β)} : a ∈ l.keys ↔ ∃ (b : β a), sigma.mk a b ∈ l := ⟨exists_of_mem_keys, λ ⟨b, h⟩, mem_keys_of_mem h⟩ theorem not_mem_keys {a} {l : list (sigma β)} : a ∉ l.keys ↔ ∀ b : β a, sigma.mk a b ∉ l := (not_iff_not_of_iff mem_keys).trans not_exists theorem not_eq_key {a} {l : list (sigma β)} : a ∉ l.keys ↔ ∀ s : sigma β, s ∈ l → a ≠ s.1 := iff.intro (λ h₁ s h₂ e, absurd (mem_keys_of_mem h₂) (by rwa e at h₁)) (λ f h₁, let ⟨b, h₂⟩ := exists_of_mem_keys h₁ in f _ h₂ rfl) /-! ### `nodupkeys` -/ /-- Determines whether the store uses a key several times. -/ def nodupkeys (l : list (sigma β)) : Prop := l.keys.nodup theorem nodupkeys_iff_pairwise {l} : nodupkeys l ↔ pairwise (λ s s' : sigma β, s.1 ≠ s'.1) l := pairwise_map _ theorem nodupkeys.pairwise_ne {l} (h : nodupkeys l) : pairwise (λ s s' : sigma β, s.1 ≠ s'.1) l := nodupkeys_iff_pairwise.1 h @[simp] theorem nodupkeys_nil : @nodupkeys α β [] := pairwise.nil @[simp] theorem nodupkeys_cons {s : sigma β} {l : list (sigma β)} : nodupkeys (s::l) ↔ s.1 ∉ l.keys ∧ nodupkeys l := by simp [keys, nodupkeys] theorem nodupkeys.eq_of_fst_eq {l : list (sigma β)} (nd : nodupkeys l) {s s' : sigma β} (h : s ∈ l) (h' : s' ∈ l) : s.1 = s'.1 → s = s' := @forall_of_forall_of_pairwise _ (λ s s' : sigma β, s.1 = s'.1 → s = s') (λ s s' H h, (H h.symm).symm) _ (λ x h _, rfl) ((nodupkeys_iff_pairwise.1 nd).imp (λ s s' h h', (h h').elim)) _ h _ h' theorem nodupkeys.eq_of_mk_mem {a : α} {b b' : β a} {l : list (sigma β)} (nd : nodupkeys l) (h : sigma.mk a b ∈ l) (h' : sigma.mk a b' ∈ l) : b = b' := by cases nd.eq_of_fst_eq h h' rfl; refl theorem nodupkeys_singleton (s : sigma β) : nodupkeys [s] := nodup_singleton _ theorem nodupkeys_of_sublist {l₁ l₂ : list (sigma β)} (h : l₁ <+ l₂) : nodupkeys l₂ → nodupkeys l₁ := nodup_of_sublist (h.map _) theorem nodup_of_nodupkeys {l : list (sigma β)} : nodupkeys l → nodup l := nodup_of_nodup_map _ theorem perm_nodupkeys {l₁ l₂ : list (sigma β)} (h : l₁ ~ l₂) : nodupkeys l₁ ↔ nodupkeys l₂ := (h.map _).nodup_iff theorem nodupkeys_join {L : list (list (sigma β))} : nodupkeys (join L) ↔ (∀ l ∈ L, nodupkeys l) ∧ pairwise disjoint (L.map keys) := begin rw [nodupkeys_iff_pairwise, pairwise_join, pairwise_map], refine and_congr (ball_congr $ λ l h, by simp [nodupkeys_iff_pairwise]) _, apply iff_of_eq, congr' with l₁ l₂, simp [keys, disjoint_iff_ne] end theorem nodup_enum_map_fst (l : list α) : (l.enum.map prod.fst).nodup := by simp [list.nodup_range] lemma mem_ext {l₀ l₁ : list (sigma β)} (nd₀ : l₀.nodup) (nd₁ : l₁.nodup) (h : ∀ x, x ∈ l₀ ↔ x ∈ l₁) : l₀ ~ l₁ := begin induction l₀ with x xs generalizing l₁; cases l₁ with y ys, { constructor }, iterate 2 { specialize h x <|> specialize h y, simp at h, cases h }, simp at nd₀ nd₁, classical, cases nd₀, cases nd₁, by_cases h' : x = y, { subst y, constructor, apply l₀_ih ‹ _ › ‹ nodup ys ›, intro a, specialize h a, simp at h, by_cases h' : a = x, { subst a, rw ← not_iff_not, split; intro; assumption }, { simp [h'] at h, exact h } }, { transitivity x :: y :: ys.erase x, { constructor, apply l₀_ih ‹ _ ›, { simp, split, { intro, apply nd₁_left, apply mem_of_mem_erase ‹_› }, apply nodup_erase_of_nodup; assumption }, { intro a, specialize h a, simp at h, by_cases h' : a = x, { subst a, rw ← not_iff_not, split; intro, simp [mem_erase_of_nodup,*], assumption }, { simp [h'] at h, simp [h], apply or_congr, refl, simp [mem_erase_of_ne,*] } } }, transitivity y :: x :: ys.erase x, { constructor }, { constructor, symmetry, apply perm_cons_erase, specialize h x, simp [h'] at h, exact h } } end variables [decidable_eq α] /-! ### `lookup` -/ /-- `lookup a l` is the first value in `l` corresponding to the key `a`, or `none` if no such element exists. -/ def lookup (a : α) : list (sigma β) → option (β a) | [] := none | (⟨a', b⟩ :: l) := if h : a' = a then some (eq.rec_on h b) else lookup l @[simp] theorem lookup_nil (a : α) : lookup a [] = @none (β a) := rfl @[simp] theorem lookup_cons_eq (l) (a : α) (b : β a) : lookup a (⟨a, b⟩::l) = some b := dif_pos rfl @[simp] theorem lookup_cons_ne (l) {a} : ∀ s : sigma β, a ≠ s.1 → lookup a (s::l) = lookup a l | ⟨a', b⟩ h := dif_neg h.symm theorem lookup_is_some {a : α} : ∀ {l : list (sigma β)}, (lookup a l).is_some ↔ a ∈ l.keys | [] := by simp | (⟨a', b⟩ :: l) := begin by_cases h : a = a', { subst a', simp }, { simp [h, lookup_is_some] }, end theorem lookup_eq_none {a : α} {l : list (sigma β)} : lookup a l = none ↔ a ∉ l.keys := by simp [← lookup_is_some, option.is_none_iff_eq_none] theorem of_mem_lookup {a : α} {b : β a} : ∀ {l : list (sigma β)}, b ∈ lookup a l → sigma.mk a b ∈ l | (⟨a', b'⟩ :: l) H := begin by_cases h : a = a', { subst a', simp at H, simp [H] }, { simp [h] at H, exact or.inr (of_mem_lookup H) } end theorem mem_lookup {a} {b : β a} {l : list (sigma β)} (nd : l.nodupkeys) (h : sigma.mk a b ∈ l) : b ∈ lookup a l := begin cases option.is_some_iff_exists.mp (lookup_is_some.mpr (mem_keys_of_mem h)) with b' h', cases nd.eq_of_mk_mem h (of_mem_lookup h'), exact h' end theorem map_lookup_eq_find (a : α) : ∀ l : list (sigma β), (lookup a l).map (sigma.mk a) = find (λ s, a = s.1) l | [] := rfl | (⟨a', b'⟩ :: l) := begin by_cases h : a = a', { subst a', simp }, { simp [h, map_lookup_eq_find] } end theorem mem_lookup_iff {a : α} {b : β a} {l : list (sigma β)} (nd : l.nodupkeys) : b ∈ lookup a l ↔ sigma.mk a b ∈ l := ⟨of_mem_lookup, mem_lookup nd⟩ theorem perm_lookup (a : α) {l₁ l₂ : list (sigma β)} (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) (p : l₁ ~ l₂) : lookup a l₁ = lookup a l₂ := by ext b; simp [mem_lookup_iff, nd₁, nd₂]; exact p.mem_iff lemma lookup_ext {l₀ l₁ : list (sigma β)} (nd₀ : l₀.nodupkeys) (nd₁ : l₁.nodupkeys) (h : ∀ x y, y ∈ l₀.lookup x ↔ y ∈ l₁.lookup x) : l₀ ~ l₁ := mem_ext (nodup_of_nodupkeys nd₀) (nodup_of_nodupkeys nd₁) (λ ⟨a,b⟩, by rw [← mem_lookup_iff, ← mem_lookup_iff, h]; assumption) /-! ### `lookup_all` -/ /-- `lookup_all a l` is the list of all values in `l` corresponding to the key `a`. -/ def lookup_all (a : α) : list (sigma β) → list (β a) | [] := [] | (⟨a', b⟩ :: l) := if h : a' = a then eq.rec_on h b :: lookup_all l else lookup_all l @[simp] theorem lookup_all_nil (a : α) : lookup_all a [] = @nil (β a) := rfl @[simp] theorem lookup_all_cons_eq (l) (a : α) (b : β a) : lookup_all a (⟨a, b⟩::l) = b :: lookup_all a l := dif_pos rfl @[simp] theorem lookup_all_cons_ne (l) {a} : ∀ s : sigma β, a ≠ s.1 → lookup_all a (s::l) = lookup_all a l | ⟨a', b⟩ h := dif_neg h.symm theorem lookup_all_eq_nil {a : α} : ∀ {l : list (sigma β)}, lookup_all a l = [] ↔ ∀ b : β a, sigma.mk a b ∉ l | [] := by simp | (⟨a', b⟩ :: l) := begin by_cases h : a = a', { subst a', simp }, { simp [h, lookup_all_eq_nil] }, end theorem head_lookup_all (a : α) : ∀ l : list (sigma β), head' (lookup_all a l) = lookup a l | [] := by simp | (⟨a', b⟩ :: l) := by by_cases h : a = a'; [{subst h, simp}, simp *] theorem mem_lookup_all {a : α} {b : β a} : ∀ {l : list (sigma β)}, b ∈ lookup_all a l ↔ sigma.mk a b ∈ l | [] := by simp | (⟨a', b'⟩ :: l) := by by_cases h : a = a'; [{subst h, simp *}, simp *] theorem lookup_all_sublist (a : α) : ∀ l : list (sigma β), (lookup_all a l).map (sigma.mk a) <+ l | [] := by simp | (⟨a', b'⟩ :: l) := begin by_cases h : a = a', { subst h, simp, exact (lookup_all_sublist l).cons2 _ _ _ }, { simp [h], exact (lookup_all_sublist l).cons _ _ _ } end theorem lookup_all_length_le_one (a : α) {l : list (sigma β)} (h : l.nodupkeys) : length (lookup_all a l) ≤ 1 := by have := nodup_of_sublist ((lookup_all_sublist a l).map _) h; rw map_map at this; rwa [← nodup_repeat, ← map_const _ a] theorem lookup_all_eq_lookup (a : α) {l : list (sigma β)} (h : l.nodupkeys) : lookup_all a l = (lookup a l).to_list := begin rw ← head_lookup_all, have := lookup_all_length_le_one a h, revert this, rcases lookup_all a l with _|⟨b, _|⟨c, l⟩⟩; intro; try {refl}, exact absurd this dec_trivial end theorem lookup_all_nodup (a : α) {l : list (sigma β)} (h : l.nodupkeys) : (lookup_all a l).nodup := by rw lookup_all_eq_lookup a h; apply option.to_list_nodup theorem perm_lookup_all (a : α) {l₁ l₂ : list (sigma β)} (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) (p : l₁ ~ l₂) : lookup_all a l₁ = lookup_all a l₂ := by simp [lookup_all_eq_lookup, nd₁, nd₂, perm_lookup a nd₁ nd₂ p] /-! ### `kreplace` -/ /-- Replaces the first value with key `a` by `b`. -/ def kreplace (a : α) (b : β a) : list (sigma β) → list (sigma β) := lookmap $ λ s, if a = s.1 then some ⟨a, b⟩ else none theorem kreplace_of_forall_not (a : α) (b : β a) {l : list (sigma β)} (H : ∀ b : β a, sigma.mk a b ∉ l) : kreplace a b l = l := lookmap_of_forall_not _ $ begin rintro ⟨a', b'⟩ h, dsimp, split_ifs, { subst a', exact H _ h }, {refl} end theorem kreplace_self {a : α} {b : β a} {l : list (sigma β)} (nd : nodupkeys l) (h : sigma.mk a b ∈ l) : kreplace a b l = l := begin refine (lookmap_congr _).trans (lookmap_id' (option.guard (λ s, a = s.1)) _ _), { rintro ⟨a', b'⟩ h', dsimp [option.guard], split_ifs, { subst a', exact ⟨rfl, heq_of_eq $ nd.eq_of_mk_mem h h'⟩ }, { refl } }, { rintro ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, dsimp [option.guard], split_ifs, { subst a₁, rintro ⟨⟩, simp }, { rintro ⟨⟩ } }, end theorem keys_kreplace (a : α) (b : β a) : ∀ l : list (sigma β), (kreplace a b l).keys = l.keys := lookmap_map_eq _ _ $ by rintro ⟨a₁, b₂⟩ ⟨a₂, b₂⟩; dsimp; split_ifs; simp [h] {contextual := tt} theorem kreplace_nodupkeys (a : α) (b : β a) {l : list (sigma β)} : (kreplace a b l).nodupkeys ↔ l.nodupkeys := by simp [nodupkeys, keys_kreplace] theorem perm.kreplace {a : α} {b : β a} {l₁ l₂ : list (sigma β)} (nd : l₁.nodupkeys) : l₁ ~ l₂ → kreplace a b l₁ ~ kreplace a b l₂ := perm_lookmap _ $ begin refine nd.pairwise_ne.imp _, intros x y h z h₁ w h₂, split_ifs at h₁ h₂; cases h₁; cases h₂, exact (h (h_2.symm.trans h_1)).elim end /-! ### `kerase` -/ /-- Remove the first pair with the key `a`. -/ def kerase (a : α) : list (sigma β) → list (sigma β) := erasep $ λ s, a = s.1 @[simp] theorem kerase_nil {a} : @kerase _ β _ a [] = [] := rfl @[simp, priority 990] theorem kerase_cons_eq {a} {s : sigma β} {l : list (sigma β)} (h : a = s.1) : kerase a (s :: l) = l := by simp [kerase, h] @[simp, priority 990] theorem kerase_cons_ne {a} {s : sigma β} {l : list (sigma β)} (h : a ≠ s.1) : kerase a (s :: l) = s :: kerase a l := by simp [kerase, h] @[simp, priority 980] theorem kerase_of_not_mem_keys {a} {l : list (sigma β)} (h : a ∉ l.keys) : kerase a l = l := by induction l with _ _ ih; [refl, { simp [not_or_distrib] at h, simp [h.1, ih h.2] }] theorem kerase_sublist (a : α) (l : list (sigma β)) : kerase a l <+ l := erasep_sublist _ theorem kerase_keys_subset (a) (l : list (sigma β)) : (kerase a l).keys ⊆ l.keys := ((kerase_sublist a l).map _).subset theorem mem_keys_of_mem_keys_kerase {a₁ a₂} {l : list (sigma β)} : a₁ ∈ (kerase a₂ l).keys → a₁ ∈ l.keys := @kerase_keys_subset _ _ _ _ _ _ theorem exists_of_kerase {a : α} {l : list (sigma β)} (h : a ∈ l.keys) : ∃ (b : β a) (l₁ l₂ : list (sigma β)), a ∉ l₁.keys ∧ l = l₁ ++ ⟨a, b⟩ :: l₂ ∧ kerase a l = l₁ ++ l₂ := begin induction l, case list.nil { cases h }, case list.cons : hd tl ih { by_cases e : a = hd.1, { subst e, exact ⟨hd.2, [], tl, by simp, by cases hd; refl, by simp⟩ }, { simp at h, cases h, case or.inl : h { exact absurd h e }, case or.inr : h { rcases ih h with ⟨b, tl₁, tl₂, h₁, h₂, h₃⟩, exact ⟨b, hd :: tl₁, tl₂, not_mem_cons_of_ne_of_not_mem e h₁, by rw h₂; refl, by simp [e, h₃]⟩ } } } end @[simp, priority 990] theorem mem_keys_kerase_of_ne {a₁ a₂} {l : list (sigma β)} (h : a₁ ≠ a₂) : a₁ ∈ (kerase a₂ l).keys ↔ a₁ ∈ l.keys := iff.intro mem_keys_of_mem_keys_kerase $ λ p, if q : a₂ ∈ l.keys then match l, kerase a₂ l, exists_of_kerase q, p with | _, _, ⟨_, _, _, _, rfl, rfl⟩, p := by simpa [keys, h] using p end else by simp [q, p] theorem keys_kerase {a} {l : list (sigma β)} : (kerase a l).keys = l.keys.erase a := by rw [keys, kerase, ←erasep_map sigma.fst l, erase_eq_erasep] theorem kerase_kerase {a a'} {l : list (sigma β)} : (kerase a' l).kerase a = (kerase a l).kerase a' := begin by_cases a = a', { subst a' }, induction l with x xs, { refl }, { by_cases a' = x.1, { subst a', simp [kerase_cons_ne h,kerase_cons_eq rfl] }, by_cases h' : a = x.1, { subst a, simp [kerase_cons_eq rfl,kerase_cons_ne (ne.symm h)] }, { simp [kerase_cons_ne,*] } } end theorem kerase_nodupkeys (a : α) {l : list (sigma β)} : nodupkeys l → (kerase a l).nodupkeys := nodupkeys_of_sublist $ kerase_sublist _ _ theorem perm.kerase {a : α} {l₁ l₂ : list (sigma β)} (nd : l₁.nodupkeys) : l₁ ~ l₂ → kerase a l₁ ~ kerase a l₂ := perm.erasep _ $ (nodupkeys_iff_pairwise.1 nd).imp $ by rintro x y h rfl; exact h @[simp] theorem not_mem_keys_kerase (a) {l : list (sigma β)} (nd : l.nodupkeys) : a ∉ (kerase a l).keys := begin induction l, case list.nil { simp }, case list.cons : hd tl ih { simp at nd, by_cases h : a = hd.1, { subst h, simp [nd.1] }, { simp [h, ih nd.2] } } end @[simp] theorem lookup_kerase (a) {l : list (sigma β)} (nd : l.nodupkeys) : lookup a (kerase a l) = none := lookup_eq_none.mpr (not_mem_keys_kerase a nd) @[simp] theorem lookup_kerase_ne {a a'} {l : list (sigma β)} (h : a ≠ a') : lookup a (kerase a' l) = lookup a l := begin induction l, case list.nil { refl }, case list.cons : hd tl ih { cases hd with ah bh, by_cases h₁ : a = ah; by_cases h₂ : a' = ah, { substs h₁ h₂, cases ne.irrefl h }, { subst h₁, simp [h₂] }, { subst h₂, simp [h] }, { simp [h₁, h₂, ih] } } end theorem kerase_append_left {a} : ∀ {l₁ l₂ : list (sigma β)}, a ∈ l₁.keys → kerase a (l₁ ++ l₂) = kerase a l₁ ++ l₂ | [] _ h := by cases h | (s :: l₁) l₂ h₁ := if h₂ : a = s.1 then by simp [h₂] else by simp at h₁; cases h₁; [exact absurd h₁ h₂, simp [h₂, kerase_append_left h₁]] theorem kerase_append_right {a} : ∀ {l₁ l₂ : list (sigma β)}, a ∉ l₁.keys → kerase a (l₁ ++ l₂) = l₁ ++ kerase a l₂ | [] _ h := rfl | (_ :: l₁) l₂ h := by simp [not_or_distrib] at h; simp [h.1, kerase_append_right h.2] theorem kerase_comm (a₁ a₂) (l : list (sigma β)) : kerase a₂ (kerase a₁ l) = kerase a₁ (kerase a₂ l) := if h : a₁ = a₂ then by simp [h] else if ha₁ : a₁ ∈ l.keys then if ha₂ : a₂ ∈ l.keys then match l, kerase a₁ l, exists_of_kerase ha₁, ha₂ with | _, _, ⟨b₁, l₁, l₂, a₁_nin_l₁, rfl, rfl⟩, a₂_in_l₁_app_l₂ := if h' : a₂ ∈ l₁.keys then by simp [kerase_append_left h', kerase_append_right (mt (mem_keys_kerase_of_ne h).mp a₁_nin_l₁)] else by simp [kerase_append_right h', kerase_append_right a₁_nin_l₁, @kerase_cons_ne _ _ _ a₂ ⟨a₁, b₁⟩ _ (ne.symm h)] end else by simp [ha₂, mt mem_keys_of_mem_keys_kerase ha₂] else by simp [ha₁, mt mem_keys_of_mem_keys_kerase ha₁] lemma sizeof_kerase {α} {β : α → Type*} [decidable_eq α] [has_sizeof (sigma β)] (x : α) (xs : list (sigma β)) : sizeof (list.kerase x xs) ≤ sizeof xs := begin unfold_wf, induction xs with y ys, { simp }, { by_cases x = y.1; simp [*, list.sizeof] }, end /-! ### `kinsert` -/ /-- Insert the pair `⟨a, b⟩` and erase the first pair with the key `a`. -/ def kinsert (a : α) (b : β a) (l : list (sigma β)) : list (sigma β) := ⟨a, b⟩ :: kerase a l @[simp] theorem kinsert_def {a} {b : β a} {l : list (sigma β)} : kinsert a b l = ⟨a, b⟩ :: kerase a l := rfl theorem mem_keys_kinsert {a a'} {b' : β a'} {l : list (sigma β)} : a ∈ (kinsert a' b' l).keys ↔ a = a' ∨ a ∈ l.keys := by by_cases h : a = a'; simp [h] theorem kinsert_nodupkeys (a) (b : β a) {l : list (sigma β)} (nd : l.nodupkeys) : (kinsert a b l).nodupkeys := nodupkeys_cons.mpr ⟨not_mem_keys_kerase a nd, kerase_nodupkeys a nd⟩ theorem perm.kinsert {a} {b : β a} {l₁ l₂ : list (sigma β)} (nd₁ : l₁.nodupkeys) (p : l₁ ~ l₂) : kinsert a b l₁ ~ kinsert a b l₂ := (p.kerase nd₁).cons _ theorem lookup_kinsert {a} {b : β a} (l : list (sigma β)) : lookup a (kinsert a b l) = some b := by simp only [kinsert, lookup_cons_eq] theorem lookup_kinsert_ne {a a'} {b' : β a'} {l : list (sigma β)} (h : a ≠ a') : lookup a (kinsert a' b' l) = lookup a l := by simp [h] /-! ### `kextract` -/ /-- Finds the first entry with a given key `a` and returns its value (as an `option` because there might be no entry with key `a`) alongside with the rest of the entries. -/ def kextract (a : α) : list (sigma β) → option (β a) × list (sigma β) | [] := (none, []) | (s::l) := if h : s.1 = a then (some (eq.rec_on h s.2), l) else let (b', l') := kextract l in (b', s :: l') @[simp] theorem kextract_eq_lookup_kerase (a : α) : ∀ l : list (sigma β), kextract a l = (lookup a l, kerase a l) | [] := rfl | (⟨a', b⟩::l) := begin simp [kextract], dsimp, split_ifs, { subst a', simp [kerase] }, { simp [kextract, ne.symm h, kextract_eq_lookup_kerase l, kerase] } end /-! ### `erase_dupkeys` -/ /-- Remove entries with duplicate keys from `l : list (sigma β)`. -/ def erase_dupkeys : list (sigma β) → list (sigma β) := list.foldr (λ x, kinsert x.1 x.2) [] lemma erase_dupkeys_cons {x : sigma β} (l : list (sigma β)) : erase_dupkeys (x :: l) = kinsert x.1 x.2 (erase_dupkeys l) := rfl lemma nodupkeys_erase_dupkeys (l : list (sigma β)) : nodupkeys (erase_dupkeys l) := begin dsimp [erase_dupkeys], generalize hl : nil = l', have : nodupkeys l', { rw ← hl, apply nodup_nil }, clear hl, induction l with x xs, { apply this }, { cases x, simp [erase_dupkeys], split, { simp [keys_kerase], apply mem_erase_of_nodup l_ih }, apply kerase_nodupkeys _ l_ih, } end lemma lookup_erase_dupkeys (a : α) (l : list (sigma β)) : lookup a (erase_dupkeys l) = lookup a l := begin induction l, refl, cases l_hd with a' b, by_cases a = a', { subst a', rw [erase_dupkeys_cons,lookup_kinsert,lookup_cons_eq] }, { rw [erase_dupkeys_cons,lookup_kinsert_ne h,l_ih,lookup_cons_ne], exact h }, end lemma sizeof_erase_dupkeys {α} {β : α → Type*} [decidable_eq α] [has_sizeof (sigma β)] (xs : list (sigma β)) : sizeof (list.erase_dupkeys xs) ≤ sizeof xs := begin unfold_wf, induction xs with x xs, { simp [list.erase_dupkeys] }, { simp only [erase_dupkeys_cons, list.sizeof, kinsert_def, add_le_add_iff_left, sigma.eta], transitivity, apply sizeof_kerase, assumption } end /-! ### `kunion` -/ /-- `kunion l₁ l₂` is the append to l₁ of l₂ after, for each key in l₁, the first matching pair in l₂ is erased. -/ def kunion : list (sigma β) → list (sigma β) → list (sigma β) | [] l₂ := l₂ | (s :: l₁) l₂ := s :: kunion l₁ (kerase s.1 l₂) @[simp] theorem nil_kunion {l : list (sigma β)} : kunion [] l = l := rfl @[simp] theorem kunion_nil : ∀ {l : list (sigma β)}, kunion l [] = l | [] := rfl | (_ :: l) := by rw [kunion, kerase_nil, kunion_nil] @[simp] theorem kunion_cons {s} {l₁ l₂ : list (sigma β)} : kunion (s :: l₁) l₂ = s :: kunion l₁ (kerase s.1 l₂) := rfl @[simp] theorem mem_keys_kunion {a} {l₁ l₂ : list (sigma β)} : a ∈ (kunion l₁ l₂).keys ↔ a ∈ l₁.keys ∨ a ∈ l₂.keys := begin induction l₁ generalizing l₂, case list.nil { simp }, case list.cons : s l₁ ih { by_cases h : a = s.1; [simp [h], simp [h, ih]] } end @[simp] theorem kunion_kerase {a} : ∀ {l₁ l₂ : list (sigma β)}, kunion (kerase a l₁) (kerase a l₂) = kerase a (kunion l₁ l₂) | [] _ := rfl | (s :: _) l := by by_cases h : a = s.1; simp [h, kerase_comm a s.1 l, kunion_kerase] theorem kunion_nodupkeys {l₁ l₂ : list (sigma β)} (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) : (kunion l₁ l₂).nodupkeys := begin induction l₁ generalizing l₂, case list.nil { simp only [nil_kunion, nd₂] }, case list.cons : s l₁ ih { simp at nd₁, simp [not_or_distrib, nd₁.1, nd₂, ih nd₁.2 (kerase_nodupkeys s.1 nd₂)] } end theorem perm.kunion_right {l₁ l₂ : list (sigma β)} (p : l₁ ~ l₂) (l) : kunion l₁ l ~ kunion l₂ l := begin induction p generalizing l, case list.perm.nil { refl }, case list.perm.cons : hd tl₁ tl₂ p ih { simp [ih (kerase hd.1 l), perm.cons] }, case list.perm.swap : s₁ s₂ l { simp [kerase_comm, perm.swap] }, case list.perm.trans : l₁ l₂ l₃ p₁₂ p₂₃ ih₁₂ ih₂₃ { exact perm.trans (ih₁₂ l) (ih₂₃ l) } end theorem perm.kunion_left : ∀ l {l₁ l₂ : list (sigma β)}, l₁.nodupkeys → l₁ ~ l₂ → kunion l l₁ ~ kunion l l₂ | [] _ _ _ p := p | (s :: l) l₁ l₂ nd₁ p := by simp [((p.kerase nd₁).kunion_left l (kerase_nodupkeys s.1 nd₁)).cons s] theorem perm.kunion {l₁ l₂ l₃ l₄ : list (sigma β)} (nd₃ : l₃.nodupkeys) (p₁₂ : l₁ ~ l₂) (p₃₄ : l₃ ~ l₄) : kunion l₁ l₃ ~ kunion l₂ l₄ := (p₁₂.kunion_right l₃).trans (p₃₄.kunion_left l₂ nd₃) @[simp] theorem lookup_kunion_left {a} {l₁ l₂ : list (sigma β)} (h : a ∈ l₁.keys) : lookup a (kunion l₁ l₂) = lookup a l₁ := begin induction l₁ with s _ ih generalizing l₂; simp at h; cases h; cases s with a', { subst h, simp }, { rw kunion_cons, by_cases h' : a = a', { subst h', simp }, { simp [h', ih h] } } end @[simp] theorem lookup_kunion_right {a} {l₁ l₂ : list (sigma β)} (h : a ∉ l₁.keys) : lookup a (kunion l₁ l₂) = lookup a l₂ := begin induction l₁ generalizing l₂, case list.nil { simp }, case list.cons : _ _ ih { simp [not_or_distrib] at h, simp [h.1, ih h.2] } end @[simp] theorem mem_lookup_kunion {a} {b : β a} {l₁ l₂ : list (sigma β)} : b ∈ lookup a (kunion l₁ l₂) ↔ b ∈ lookup a l₁ ∨ a ∉ l₁.keys ∧ b ∈ lookup a l₂ := begin induction l₁ generalizing l₂, case list.nil { simp }, case list.cons : s _ ih { cases s with a', by_cases h₁ : a = a', { subst h₁, simp }, { let h₂ := @ih (kerase a' l₂), simp [h₁] at h₂, simp [h₁, h₂] } } end theorem mem_lookup_kunion_middle {a} {b : β a} {l₁ l₂ l₃ : list (sigma β)} (h₁ : b ∈ lookup a (kunion l₁ l₃)) (h₂ : a ∉ keys l₂) : b ∈ lookup a (kunion (kunion l₁ l₂) l₃) := match mem_lookup_kunion.mp h₁ with | or.inl h := mem_lookup_kunion.mpr (or.inl (mem_lookup_kunion.mpr (or.inl h))) | or.inr h := mem_lookup_kunion.mpr $ or.inr ⟨mt mem_keys_kunion.mp (not_or_distrib.mpr ⟨h.1, h₂⟩), h.2⟩ end end list
992d02c1a9d5775caae7acbb398c5479f2c4390c
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/polynomial/ring_division.lean
176fc768f91a8663f424aec280e3f9661a25191b
[ "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
27,919
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, Johan Commelin -/ import data.polynomial.basic import data.polynomial.div import data.polynomial.algebra_map import data.set.finite /-! # Theory of univariate polynomials This file starts looking like the ring theory of $ R[X] $ -/ noncomputable theory open_locale classical 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) lemma aeval_mod_by_monic_eq_self_of_root [algebra R S] {p q : polynomial R} (hq : q.monic) {x : S} (hx : aeval x q = 0) : aeval x (p %ₘ q) = aeval x p := eval₂_mod_by_monic_eq_self_of_root hq hx end comm_ring section no_zero_divisors variables [comm_ring R] [no_zero_divisors R] {p q : polynomial R} instance : no_zero_divisors (polynomial R) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, begin rw [← leading_coeff_eq_zero, ← leading_coeff_eq_zero], refine eq_zero_or_eq_zero_of_mul_eq_zero _, rw [← leading_coeff_zero, ← leading_coeff_mul, h], end } 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 end no_zero_divisors section integral_domain variables [integral_domain R] {p q : polynomial R} instance : integral_domain (polynomial R) := { ..polynomial.no_zero_divisors, ..polynomial.nontrivial, ..polynomial.comm_ring } lemma nat_trailing_degree_mul (hp : p ≠ 0) (hq : q ≠ 0) : (p * q).nat_trailing_degree = p.nat_trailing_degree + q.nat_trailing_degree := begin simp only [←nat.sub_eq_of_eq_add (nat_degree_eq_reverse_nat_degree_add_nat_trailing_degree _)], rw [reverse_mul_of_domain, nat_degree_mul hp hq, nat_degree_mul (mt reverse_eq_zero.mp hp) (mt reverse_eq_zero.mp hq), reverse_nat_degree, reverse_nat_degree, ←nat.sub_sub, nat.add_comm, nat.add_sub_assoc (nat.sub_le _ _), add_comm, nat.add_sub_assoc (nat.sub_le _ _)], end section roots open multiset local attribute [semireducible] 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 /-- The multiplicity of `a` as root of `(X - a) ^ n` is `n`. -/ lemma root_multiplicity_X_sub_C_pow (a : R) (n : ℕ) : root_multiplicity a ((X - C a) ^ n) = n := begin induction n with n hn, { refine root_multiplicity_eq_zero _, simp only [eval_one, is_root.def, not_false_iff, one_ne_zero, pow_zero] }, have hzero := (ne_zero_of_monic (monic_pow (monic_X_sub_C a) n.succ)), rw pow_succ (X - C a) n at hzero ⊢, simp only [root_multiplicity_mul hzero, root_multiplicity_X_sub_C_self, hn, nat.one_add] end /-- If `(X - a) ^ n` divides a polynomial `p` then the multiplicity of `a` as root of `p` is at least `n`. -/ lemma root_multiplicity_of_dvd {p : polynomial R} {a : R} {n : ℕ} (hzero : p ≠ 0) (h : (X - C a) ^ n ∣ p) : n ≤ root_multiplicity a p := begin obtain ⟨q, hq⟩ := exists_eq_mul_right_of_dvd h, rw hq at hzero, simp only [hq, root_multiplicity_mul hzero, root_multiplicity_X_sub_C_pow, ge_iff_le, _root_.zero_le, le_add_iff_nonneg_right], end /-- The multiplicity of `p + q` is at least the minimum of the multiplicities. -/ lemma root_multiplicity_add {p q : polynomial R} (a : R) (hzero : p + q ≠ 0) : min (root_multiplicity a p) (root_multiplicity a q) ≤ root_multiplicity a (p + q) := begin refine root_multiplicity_of_dvd hzero _, have hdivp : (X - C a) ^ root_multiplicity a p ∣ p := pow_root_multiplicity_dvd p a, have hdivq : (X - C a) ^ root_multiplicity a q ∣ q := pow_root_multiplicity_dvd q a, exact min_pow_dvd_add hdivp hdivq 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 x) 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 eq_zero_of_infinite_is_root (p : polynomial R) (h : set.infinite {x | is_root p x}) : p = 0 := begin by_contradiction hp, apply h, convert p.roots.to_finset.finite_to_set using 1, ext1 r, simp only [mem_roots hp, multiset.mem_to_finset, set.mem_set_of_eq, finset.mem_coe] end lemma exists_max_root [linear_order R] (p : polynomial R) (hp : p ≠ 0) : ∃ x₀, ∀ x, p.is_root x → x ≤ x₀ := set.exists_upper_bound_image _ _ $ not_not.mp (mt (eq_zero_of_infinite_is_root p) hp) lemma exists_min_root [linear_order R] (p : polynomial R) (hp : p ≠ 0) : ∃ x₀, ∀ x, p.is_root x → x₀ ≤ x := set.exists_lower_bound_image _ _ $ not_not.mp (mt (eq_zero_of_infinite_is_root p) hp) lemma eq_of_infinite_eval_eq {R : Type*} [integral_domain R] (p q : polynomial R) (h : set.infinite {x | eval x p = eval x q}) : p = q := begin rw [← sub_eq_zero], apply eq_zero_of_infinite_is_root, simpa only [is_root, eval_sub, sub_eq_zero] end 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)) : ((0 : polynomial R) ∉ L) → L.prod.roots = (L : multiset (polynomial R)).bind roots := list.rec_on L (λ _, roots_one) $ λ hd tl ih H, begin rw [list.mem_cons_iff, not_or_distrib] at H, rw [list.prod_cons, roots_mul (mul_ne_zero (ne.symm 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)) : (0 : polynomial R) ∉ m → m.prod.roots = m.bind roots := by { rcases m with ⟨L⟩, simpa only [coe_prod, quot_mk_to_coe''] using roots_list_prod L } 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 rcases s with ⟨m, hm⟩, simpa [multiset.prod_eq_zero_iff, bind_map] using roots_multiset_prod (m.map f) 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 section nth_roots /-- `nth_roots n a` noncomputably returns the solutions to `x ^ n = a`-/ def nth_roots (n : ℕ) (a : R) : multiset R := roots ((X : polynomial R) ^ n - C a) @[simp] lemma mem_nth_roots {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] @[simp] lemma nth_roots_zero (r : R) : nth_roots 0 r = 0 := by simp only [empty_eq_zero, pow_zero, nth_roots, ← C_1, ← C_sub, roots_C] lemma card_nth_roots (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) /-- The multiset `nth_roots ↑n (1 : R)` as a finset. -/ def nth_roots_finset (n : ℕ) (R : Type*) [integral_domain R] : finset R := multiset.to_finset (nth_roots n (1 : R)) @[simp] lemma mem_nth_roots_finset {n : ℕ} (h : 0 < n) {x : R} : x ∈ nth_roots_finset n R ↔ x ^ (n : ℕ) = 1 := by rw [nth_roots_finset, mem_to_finset, mem_nth_roots h] end nth_roots 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, by rwa mem_support_iff at hbs, refine coeff_eq_zero_of_degree_lt _, erw [degree_mul, degree_C this, degree_pow, zero_add, degree_eq_nat_degree hq0, ← with_bot.coe_nsmul, nsmul_eq_mul, with_bot.coe_lt_coe, nat.cast_id, mul_lt_mul_right (pos_iff_ne_zero.mpr hqd0)], exact lt_of_le_of_ne (le_nat_degree_of_ne_zero this) hbp, end begin intro h, contrapose! hp0, rw 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) lemma comp_eq_zero_iff : p.comp q = 0 ↔ p = 0 ∨ (p.eval (q.coeff 0) = 0 ∧ q = C (q.coeff 0)) := begin split, { intro h, have key : p.nat_degree = 0 ∨ q.nat_degree = 0, { rw [←mul_eq_zero, ←nat_degree_comp, h, nat_degree_zero] }, replace key := or.imp eq_C_of_nat_degree_eq_zero eq_C_of_nat_degree_eq_zero key, cases key, { rw [key, C_comp] at h, exact or.inl (key.trans h) }, { rw [key, comp_C, C_eq_zero] at h, exact or.inr ⟨h, key⟩ }, }, { exact λ h, or.rec (λ h, by rw [h, zero_comp]) (λ h, by rw [h.2, comp_C, h.1, C_0]) h }, end lemma zero_of_eval_zero [infinite R] (p : polynomial R) (h : ∀ x, p.eval x = 0) : p = 0 := by classical; by_contradiction hp; exact fintype.false ⟨p.roots.to_finset, λ x, multiset.mem_to_finset.mpr ((mem_roots hp).mpr (h _))⟩ lemma funext [infinite R] {p q : polynomial R} (ext : ∀ r : R, p.eval r = q.eval r) : p = q := begin rw ← sub_eq_zero, apply zero_of_eval_zero, intro x, rw [eval_sub, sub_eq_zero, ext], end /-- The set of distinct roots of `p` in `E`. If you have a non-separable polynomial, use `polynomial.roots` for the multiset where multiple roots have the appropriate multiplicity. -/ def root_set (p : polynomial R) (S) [integral_domain S] [algebra R S] : set S := (p.map (algebra_map R S)).roots.to_finset lemma root_set_def (p : polynomial R) (S) [integral_domain S] [algebra R S] : p.root_set S = (p.map (algebra_map R S)).roots.to_finset := rfl @[simp] lemma root_set_zero (S) [integral_domain S] [algebra R S] : (0 : polynomial R).root_set S = ∅ := by rw [root_set_def, polynomial.map_zero, roots_zero, to_finset_zero, finset.coe_empty] @[simp] lemma root_set_C [integral_domain S] [algebra R S] (a : R) : (C a).root_set S = ∅ := by rw [root_set_def, map_C, roots_C, multiset.to_finset_zero, finset.coe_empty] instance root_set_fintype {R : Type*} [integral_domain R] (p : polynomial R) (S : Type*) [integral_domain S] [algebra R S] : fintype (p.root_set S) := finset_coe.fintype _ lemma root_set_finite {R : Type*} [integral_domain R] (p : polynomial R) (S : Type*) [integral_domain S] [algebra R S] : (p.root_set S).finite := ⟨polynomial.root_set_fintype p S⟩ 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.is_unit_or_is_unit 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]) /-- Division by a monic polynomial doesn't change the leading coefficient. -/ lemma leading_coeff_div_by_monic_of_monic {R : Type u} [integral_domain R] {p q : polynomial R} (hmonic : q.monic) (hdegree : q.degree ≤ p.degree) : (p /ₘ q).leading_coeff = p.leading_coeff := begin have hp := mod_by_monic_add_div p hmonic, have hzero : (p /ₘ q) ≠ 0, { intro h, exact not_lt_of_le hdegree ((div_by_monic_eq_zero_iff hmonic (monic.ne_zero hmonic)).1 h) }, have deglt : (p %ₘ q).degree < (q * (p /ₘ q)).degree, { rw degree_mul, refine lt_of_lt_of_le (degree_mod_by_monic_lt p hmonic (monic.ne_zero hmonic)) _, rw [degree_eq_nat_degree (monic.ne_zero hmonic), degree_eq_nat_degree hzero], norm_cast, simp only [zero_le, le_add_iff_nonneg_right] }, have hrew := (leading_coeff_add_of_degree_lt deglt), rw leading_coeff_mul q (p /ₘ q) at hrew, simp only [hmonic, one_mul, monic.leading_coeff] at hrew, nth_rewrite 1 ← hp, exact hrew.symm end lemma eq_of_monic_of_dvd_of_nat_degree_le (hp : p.monic) (hq : q.monic) (hdiv : p ∣ q) (hdeg : q.nat_degree ≤ p.nat_degree) : q = p := begin obtain ⟨r, hr⟩ := hdiv, have rzero : r ≠ 0, { intro h, simpa [h, monic.ne_zero hq] using hr }, rw [hr, nat_degree_mul (monic.ne_zero hp) rzero] at hdeg, have hdegeq : p.nat_degree + r.nat_degree = p.nat_degree, { suffices hdegle : p.nat_degree ≤ p.nat_degree + r.nat_degree, { exact le_antisymm hdeg hdegle }, exact nat.le.intro rfl }, replace hdegeq := eq_C_of_nat_degree_eq_zero (((@add_right_inj _ _ p.nat_degree) _ 0).1 hdegeq), suffices hlead : 1 = r.leading_coeff, { have hcoeff := leading_coeff_C (r.coeff 0), rw [← hdegeq, ← hlead] at hcoeff, rw [← hcoeff, C_1] at hdegeq, rwa [hdegeq, mul_one] at hr }, have hprod : q.leading_coeff = p.leading_coeff * r.leading_coeff, { simp only [hr, leading_coeff_mul] }, rwa [monic.leading_coeff hp, monic.leading_coeff hq, one_mul] at hprod end 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 monic.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.not_unit (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.is_unit_or_is_unit 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
ac751fc2a26f616ce768ff27414f76dde1535c24
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/limits/concrete_category.lean
4891c3a8f2da708d5f0f0dd9e9484cd1fa6ed1ca
[ "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
3,064
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.limits import category_theory.concrete_category.basic /-! # Facts about (co)limits of functors into concrete categories -/ universes w v u open category_theory namespace category_theory.limits variables {J : Type v} [small_category J] variables {C : Type u} [category.{v} C] [concrete_category.{v} C] local attribute [instance] concrete_category.has_coe_to_sort local attribute [instance] concrete_category.has_coe_to_fun -- We now prove a lemma about naturality of cones over functors into bundled categories. namespace cone /-- Naturality of a cone over functors to a concrete category. -/ @[simp] lemma w_apply {F : J ⥤ C} (s : cone F) {j j' : J} (f : j ⟶ j') (x : s.X) : F.map f (s.π.app j x) = s.π.app j' x := begin convert congr_fun (congr_arg (λ k : s.X ⟶ F.obj j', (k : s.X → F.obj j')) (s.w f)) x, simp only [coe_comp], end @[simp] lemma w_forget_apply (F : J ⥤ C) (s : cone (F ⋙ forget C)) {j j' : J} (f : j ⟶ j') (x : s.X) : F.map f (s.π.app j x) = s.π.app j' x := congr_fun (s.w f : _) x end cone namespace cocone /-- Naturality of a cocone over functors into a concrete category. -/ @[simp] lemma w_apply {F : J ⥤ C} (s : cocone F) {j j' : J} (f : j ⟶ j') (x : F.obj j) : s.ι.app j' (F.map f x) = s.ι.app j x := begin convert congr_fun (congr_arg (λ k : F.obj j ⟶ s.X, (k : F.obj j → s.X)) (s.w f)) x, simp only [coe_comp], end @[simp] lemma w_forget_apply (F : J ⥤ C) (s : cocone (F ⋙ forget C)) {j j' : J} (f : j ⟶ j') (x : F.obj j) : s.ι.app j' (F.map f x) = (s.ι.app j x : s.X) := congr_fun (s.w f : _) x end cocone section has_limit @[simp] lemma limit.lift_π_apply (F : J ⥤ C) [has_limit F] (s : cone F) (j : J) (x : s.X) : limit.π F j (limit.lift F s x) = s.π.app j x := begin have w := congr_arg (λ f : s.X ⟶ F.obj j, (f : s.X → F.obj j) x) (limit.lift_π s j), dsimp at w, rwa coe_comp at w, end @[simp] lemma limit.w_apply (F : J ⥤ C) [has_limit F] {j j' : J} (f : j ⟶ j') (x : limit F) : F.map f (limit.π F j x) = limit.π F j' x := begin have w := congr_arg (λ f : limit F ⟶ F.obj j', (f : limit F → F.obj j') x) (limit.w F f), dsimp at w, rwa coe_comp at w, end end has_limit section has_colimit @[simp] lemma colimit.ι_desc_apply (F : J ⥤ C) [has_colimit F] (s : cocone F) (j : J) (x : F.obj j) : colimit.desc F s (colimit.ι F j x) = s.ι.app j x := begin have w := congr_arg (λ f : F.obj j ⟶ s.X, (f : F.obj j → s.X) x) (colimit.ι_desc s j), dsimp at w, rwa coe_comp at w, end @[simp] lemma colimit.w_apply (F : J ⥤ C) [has_colimit F] {j j' : J} (f : j ⟶ j') (x : F.obj j) : colimit.ι F j' (F.map f x) = colimit.ι F j x := begin have w := congr_arg (λ f : F.obj j ⟶ colimit F, (f : F.obj j → colimit F) x) (colimit.w F f), dsimp at w, rwa coe_comp at w, end end has_colimit end category_theory.limits
04b4e8ae8a7f976a43637fc91c60fd6cf4b94fba
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/analysis/normed_space/units.lean
8d42ad7b18e2fc66b9d3d1f7f2c1ac7b9cb749f3
[ "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
11,564
lean
/- Copyright (c) 2020 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.specific_limits import analysis.asymptotics /-! # The group of units of a complete normed ring This file contains the basic theory for the group of units (invertible elements) of a complete normed ring (Banach algebras being a notable special case). ## Main results The constructions `one_sub`, `add` and `unit_of_nearby` state, in varying forms, that perturbations of a unit are units. The latter two are not stated in their optimal form; more precise versions would use the spectral radius. The first main result is `is_open`: the group of units of a complete normed ring is an open subset of the ring. The function `inverse` (defined in `algebra.ring`), for a ring `R`, sends `a : R` to `a⁻¹` if `a` is a unit and 0 if not. The other major results of this file (notably `inverse_add`, `inverse_add_norm` and `inverse_add_norm_diff_nth_order`) cover the asymptotic properties of `inverse (x + t)` as `t → 0`. -/ noncomputable theory open_locale topological_space variables {R : Type*} [normed_ring R] [complete_space R] namespace units /-- In a complete normed ring, a perturbation of `1` by an element `t` of distance less than `1` from `1` is a unit. Here we construct its `units` structure. -/ def one_sub (t : R) (h : ∥t∥ < 1) : units R := { val := 1 - t, inv := ∑' (n : ℕ), t ^ n, val_inv := mul_neg_geom_series t h, inv_val := geom_series_mul_neg t h } @[simp] lemma one_sub_coe (t : R) (h : ∥t∥ < 1) : ↑(one_sub t h) = 1 - t := rfl /-- In a complete normed ring, a perturbation of a unit `x` by an element `t` of distance less than `∥x⁻¹∥⁻¹` from `x` is a unit. Here we construct its `units` structure. -/ def add (x : units R) (t : R) (h : ∥t∥ < ∥(↑x⁻¹ : R)∥⁻¹) : units R := x * (units.one_sub (-(↑x⁻¹ * t)) begin rcases subsingleton_or_nontrivial R with _i|_i; resetI, { rw subsingleton.elim (↑x⁻¹ : R) 0, have : (0:ℝ) < 1 := by norm_num, simpa, }, { have hpos : 0 < ∥(↑x⁻¹ : R)∥ := units.norm_pos x⁻¹, calc ∥-(↑x⁻¹ * t)∥ = ∥↑x⁻¹ * t∥ : by { rw norm_neg } ... ≤ ∥(↑x⁻¹ : R)∥ * ∥t∥ : norm_mul_le x.inv _ ... < ∥(↑x⁻¹ : R)∥ * ∥(↑x⁻¹ : R)∥⁻¹ : by nlinarith only [h, hpos] ... = 1 : mul_inv_cancel (ne_of_gt hpos) }, end ) @[simp] lemma add_coe (x : units R) (t : R) (h : ∥t∥ < ∥(↑x⁻¹ : R)∥⁻¹) : ((x.add t h) : R) = x + t := by { unfold units.add, simp [mul_add] } /-- In a complete normed ring, an element `y` of distance less than `∥x⁻¹∥⁻¹` from `x` is a unit. Here we construct its `units` structure. -/ def unit_of_nearby (x : units R) (y : R) (h : ∥y - x∥ < ∥(↑x⁻¹ : R)∥⁻¹) : units R := x.add ((y : R) - x) h @[simp] lemma unit_of_nearby_coe (x : units R) (y : R) (h : ∥y - x∥ < ∥(↑x⁻¹ : R)∥⁻¹) : ↑(x.unit_of_nearby y h) = y := by { unfold units.unit_of_nearby, simp } /-- The group of units of a complete normed ring is an open subset of the ring. -/ lemma is_open : is_open {x : R | is_unit x} := begin rcases subsingleton_or_nontrivial R with _i|_i; resetI, { exact is_open_discrete is_unit }, { apply metric.is_open_iff.mpr, rintros x' ⟨x, h⟩, refine ⟨∥(↑x⁻¹ : R)∥⁻¹, inv_pos.mpr (units.norm_pos x⁻¹), _⟩, intros y hy, rw [metric.mem_ball, dist_eq_norm, ←h] at hy, use x.unit_of_nearby y hy, simp } end lemma nhds (x : units R) : {x : R | is_unit x} ∈ 𝓝 (x : R) := mem_nhds_sets is_open (by { rw [set.mem_set_of_eq], exact is_unit_unit x }) end units namespace normed_ring open_locale classical big_operators open asymptotics filter metric finset ring lemma inverse_one_sub (t : R) (h : ∥t∥ < 1) : inverse (1 - t) = ↑(units.one_sub t h)⁻¹ := begin rw ← inverse_unit (units.one_sub t h), refl, end /-- The formula `inverse (x + t) = inverse (1 + x⁻¹ * t) * x⁻¹` holds for `t` sufficiently small. -/ lemma inverse_add (x : units R) : ∀ᶠ t in (𝓝 0), inverse ((x : R) + t) = inverse (1 + ↑x⁻¹ * t) * ↑x⁻¹ := begin rw [eventually_iff, mem_nhds_iff], casesI subsingleton_or_nontrivial R, { use [1, by norm_num] }, { have hinv : 0 < ∥(↑x⁻¹ : R)∥⁻¹, { cancel_denoms, exact x⁻¹.norm_pos }, use [∥(↑x⁻¹ : R)∥⁻¹, hinv], intros t ht, simp only [mem_ball, dist_zero_right] at ht, have ht' : ∥-↑x⁻¹ * t∥ < 1, { refine lt_of_le_of_lt (norm_mul_le _ _) _, rw norm_neg, refine lt_of_lt_of_le (mul_lt_mul_of_pos_left ht x⁻¹.norm_pos) _, cancel_denoms }, have hright := inverse_one_sub (-↑x⁻¹ * t) ht', have hleft := inverse_unit (x.add t ht), simp only [neg_mul_eq_neg_mul_symm, sub_neg_eq_add] at hright, simp only [units.add_coe] at hleft, simp [hleft, hright, units.add] } end lemma inverse_one_sub_nth_order (n : ℕ) : ∀ᶠ t in (𝓝 0), inverse ((1:R) - t) = (∑ i in range n, t ^ i) + (t ^ n) * inverse (1 - t) := begin simp only [eventually_iff, mem_nhds_iff], use [1, by norm_num], intros t ht, simp only [mem_ball, dist_zero_right] at ht, simp only [inverse_one_sub t ht, set.mem_set_of_eq], have h : 1 = ((range n).sum (λ i, t ^ i)) * (units.one_sub t ht) + t ^ n, { simp only [units.one_sub_coe], rw [← geom_series, geom_sum_mul_neg], simp }, rw [← one_mul ↑(units.one_sub t ht)⁻¹, h, add_mul], congr, { rw [mul_assoc, (units.one_sub t ht).mul_inv], simp }, { simp only [units.one_sub_coe], rw [← add_mul, ← geom_series, geom_sum_mul_neg], simp } end /-- The formula `inverse (x + t) = (∑ i in range n, (- x⁻¹ * t) ^ i) * x⁻¹ + (- x⁻¹ * t) ^ n * inverse (x + t)` holds for `t` sufficiently small. -/ lemma inverse_add_nth_order (x : units R) (n : ℕ) : ∀ᶠ t in (𝓝 0), inverse ((x : R) + t) = (∑ i in range n, (- ↑x⁻¹ * t) ^ i) * ↑x⁻¹ + (- ↑x⁻¹ * t) ^ n * inverse (x + t) := begin refine (inverse_add x).mp _, have hzero : tendsto (λ (t : R), - ↑x⁻¹ * t) (𝓝 0) (𝓝 0), { convert ((mul_left_continuous (- (↑x⁻¹ : R))).tendsto 0).comp tendsto_id, simp }, refine (hzero.eventually (inverse_one_sub_nth_order n)).mp (eventually_of_forall _), simp only [neg_mul_eq_neg_mul_symm, sub_neg_eq_add], intros t h1 h2, have h := congr_arg (λ (a : R), a * ↑x⁻¹) h1, dsimp at h, convert h, rw [add_mul, mul_assoc], simp [h2.symm] end lemma inverse_one_sub_norm : is_O (λ t, inverse ((1:R) - t)) (λ t, (1:ℝ)) (𝓝 (0:R)) := begin simp only [is_O, is_O_with, eventually_iff, mem_nhds_iff], refine ⟨∥(1:R)∥ + 1, (2:ℝ)⁻¹, by norm_num, _⟩, intros t ht, simp only [ball, dist_zero_right, set.mem_set_of_eq] at ht, have ht' : ∥t∥ < 1, { have : (2:ℝ)⁻¹ < 1 := by cancel_denoms, linarith }, simp only [inverse_one_sub t ht', normed_field.norm_one, mul_one, set.mem_set_of_eq], change ∥(∑' (n : ℕ), t ^ n)∥ ≤ _, have := normed_ring.tsum_geometric_of_norm_lt_1 t ht', have : (1 - ∥t∥)⁻¹ ≤ 2, { rw ← inv_inv' (2:ℝ), refine inv_le_inv_of_le (by norm_num) _, have : (2:ℝ)⁻¹ + (2:ℝ)⁻¹ = 1 := by ring, linarith }, linarith end /-- The function `λ t, inverse (x + t)` is O(1) as `t → 0`. -/ lemma inverse_add_norm (x : units R) : is_O (λ t, inverse (↑x + t)) (λ t, (1:ℝ)) (𝓝 (0:R)) := begin simp only [is_O_iff, normed_field.norm_one, mul_one], cases subsingleton_or_nontrivial R; resetI, { refine ⟨1, eventually_of_forall (λ t, _)⟩, have : ∥inverse (↑x + t)∥ = 0 := by simp, linarith }, { cases is_O_iff.mp (@inverse_one_sub_norm R _ _) with C hC, use C * ∥((x⁻¹:units R):R)∥, have hzero : tendsto (λ t, - (↑x⁻¹ : R) * t) (𝓝 0) (𝓝 0), { convert ((mul_left_continuous (-↑x⁻¹ : R)).tendsto 0).comp tendsto_id, simp }, refine (inverse_add x).mp ((hzero.eventually hC).mp (eventually_of_forall _)), intros t bound iden, rw iden, simp at bound, have hmul := norm_mul_le (inverse (1 + ↑x⁻¹ * t)) ↑x⁻¹, nlinarith [norm_nonneg (↑x⁻¹ : R)] } end /-- The function `λ t, inverse (x + t) - (∑ i in range n, (- x⁻¹ * t) ^ i) * x⁻¹` is `O(t ^ n)` as `t → 0`. -/ lemma inverse_add_norm_diff_nth_order (x : units R) (n : ℕ) : is_O (λ (t : R), inverse (↑x + t) - (∑ i in range n, (- ↑x⁻¹ * t) ^ i) * ↑x⁻¹) (λ t, ∥t∥ ^ n) (𝓝 (0:R)) := begin by_cases h : n = 0, { simpa [h] using inverse_add_norm x }, have hn : 0 < n := nat.pos_of_ne_zero h, simp [is_O_iff], cases (is_O_iff.mp (inverse_add_norm x)) with C hC, use C * ∥(1:ℝ)∥ * ∥(↑x⁻¹ : R)∥ ^ n, have h : eventually_eq (𝓝 (0:R)) (λ t, inverse (↑x + t) - (∑ i in range n, (- ↑x⁻¹ * t) ^ i) * ↑x⁻¹) (λ t, ((- ↑x⁻¹ * t) ^ n) * inverse (x + t)), { refine (inverse_add_nth_order x n).mp (eventually_of_forall _), intros t ht, convert congr_arg (λ a, a - (range n).sum (pow (-↑x⁻¹ * t)) * ↑x⁻¹) ht, simp }, refine h.mp (hC.mp (eventually_of_forall _)), intros t _ hLHS, simp only [neg_mul_eq_neg_mul_symm] at hLHS, rw hLHS, refine le_trans (norm_mul_le _ _ ) _, have h' : ∥(-(↑x⁻¹ * t)) ^ n∥ ≤ ∥(↑x⁻¹ : R)∥ ^ n * ∥t∥ ^ n, { calc ∥(-(↑x⁻¹ * t)) ^ n∥ ≤ ∥(-(↑x⁻¹ * t))∥ ^ n : norm_pow_le _ hn ... = ∥↑x⁻¹ * t∥ ^ n : by rw norm_neg ... ≤ (∥(↑x⁻¹ : R)∥ * ∥t∥) ^ n : _ ... = ∥(↑x⁻¹ : R)∥ ^ n * ∥t∥ ^ n : mul_pow _ _ n, exact pow_le_pow_of_le_left (norm_nonneg _) (norm_mul_le ↑x⁻¹ t) n }, have h'' : 0 ≤ ∥(↑x⁻¹ : R)∥ ^ n * ∥t∥ ^ n, { refine mul_nonneg _ _; exact pow_nonneg (norm_nonneg _) n }, nlinarith [norm_nonneg (inverse (↑x + t))], end /-- The function `λ t, inverse (x + t) - x⁻¹` is `O(t)` as `t → 0`. -/ lemma inverse_add_norm_diff_first_order (x : units R) : is_O (λ t, inverse (↑x + t) - ↑x⁻¹) (λ t, ∥t∥) (𝓝 (0:R)) := by { convert inverse_add_norm_diff_nth_order x 1; simp } /-- The function `λ t, inverse (x + t) - x⁻¹ + x⁻¹ * t * x⁻¹` is `O(t ^ 2)` as `t → 0`. -/ lemma inverse_add_norm_diff_second_order (x : units R) : is_O (λ t, inverse (↑x + t) - ↑x⁻¹ + ↑x⁻¹ * t * ↑x⁻¹) (λ t, ∥t∥ ^ 2) (𝓝 (0:R)) := begin convert inverse_add_norm_diff_nth_order x 2, ext t, simp only [range_succ, range_one, sum_insert, mem_singleton, sum_singleton, not_false_iff, one_ne_zero, pow_zero, add_mul], abel, simp end /-- The function `inverse` is continuous at each unit of `R`. -/ lemma inverse_continuous_at (x : units R) : continuous_at inverse (x : R) := begin have h_is_o : is_o (λ (t : R), ∥inverse (↑x + t) - ↑x⁻¹∥) (λ (t : R), (1:ℝ)) (𝓝 0), { refine is_o_norm_left.mpr ((inverse_add_norm_diff_first_order x).trans_is_o _), exact is_o_norm_left.mpr (is_o_id_const one_ne_zero) }, have h_lim : tendsto (λ (y:R), y - x) (𝓝 x) (𝓝 0), { refine tendsto_zero_iff_norm_tendsto_zero.mpr _, exact tendsto_iff_norm_tendsto_zero.mp tendsto_id }, simp only [continuous_at], rw [tendsto_iff_norm_tendsto_zero, inverse_unit], convert h_is_o.tendsto_0.comp h_lim, ext, simp end end normed_ring
e4b6a09b1646d3ccb269b04a6855d9532d0257c7
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/abel.lean
49c67499f6b44672978ce7320b0f733c3ca36b8a
[]
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,311
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.norm_num import Mathlib.PostPort universes u_1 l namespace Mathlib /-! # The `abel` tactic Evaluate expressions in the language of additive, commutative monoids and groups. -/ namespace tactic namespace abel def term {α : Type u_1} [add_comm_monoid α] (n : ℕ) (x : α) (a : α) : α := n •ℕ x + a def termg {α : Type u_1} [add_comm_group α] (n : ℤ) (x : α) (a : α) : α := n •ℤ x + a theorem const_add_term {α : Type u_1} [add_comm_monoid α] (k : α) (n : ℕ) (x : α) (a : α) (a' : α) (h : k + a = a') : k + term n x a = term n x a' := sorry theorem const_add_termg {α : Type u_1} [add_comm_group α] (k : α) (n : ℤ) (x : α) (a : α) (a' : α) (h : k + a = a') : k + termg n x a = termg n x a' := sorry theorem term_add_const {α : Type u_1} [add_comm_monoid α] (n : ℕ) (x : α) (a : α) (k : α) (a' : α) (h : a + k = a') : term n x a + k = term n x a' := sorry theorem term_add_constg {α : Type u_1} [add_comm_group α] (n : ℤ) (x : α) (a : α) (k : α) (a' : α) (h : a + k = a') : termg n x a + k = termg n x a' := sorry theorem term_add_term {α : Type u_1} [add_comm_monoid α] (n₁ : ℕ) (x : α) (a₁ : α) (n₂ : ℕ) (a₂ : α) (n' : ℕ) (a' : α) (h₁ : n₁ + n₂ = n') (h₂ : a₁ + a₂ = a') : term n₁ x a₁ + term n₂ x a₂ = term n' x a' := sorry theorem term_add_termg {α : Type u_1} [add_comm_group α] (n₁ : ℤ) (x : α) (a₁ : α) (n₂ : ℤ) (a₂ : α) (n' : ℤ) (a' : α) (h₁ : n₁ + n₂ = n') (h₂ : a₁ + a₂ = a') : termg n₁ x a₁ + termg n₂ x a₂ = termg n' x a' := sorry theorem zero_term {α : Type u_1} [add_comm_monoid α] (x : α) (a : α) : term 0 x a = a := sorry theorem zero_termg {α : Type u_1} [add_comm_group α] (x : α) (a : α) : termg 0 x a = a := sorry theorem term_neg {α : Type u_1} [add_comm_group α] (n : ℤ) (x : α) (a : α) (n' : ℤ) (a' : α) (h₁ : -n = n') (h₂ : -a = a') : -termg n x a = termg n' x a' := sorry def smul {α : Type u_1} [add_comm_monoid α] (n : ℕ) (x : α) : α := n •ℕ x def smulg {α : Type u_1} [add_comm_group α] (n : ℤ) (x : α) : α := n •ℤ x theorem zero_smul {α : Type u_1} [add_comm_monoid α] (c : ℕ) : smul c 0 = 0 := sorry theorem zero_smulg {α : Type u_1} [add_comm_group α] (c : ℤ) : smulg c 0 = 0 := sorry theorem term_smul {α : Type u_1} [add_comm_monoid α] (c : ℕ) (n : ℕ) (x : α) (a : α) (n' : ℕ) (a' : α) (h₁ : c * n = n') (h₂ : smul c a = a') : smul c (term n x a) = term n' x a' := sorry theorem term_smulg {α : Type u_1} [add_comm_group α] (c : ℤ) (n : ℤ) (x : α) (a : α) (n' : ℤ) (a' : α) (h₁ : c * n = n') (h₂ : smulg c a = a') : smulg c (termg n x a) = termg n' x a' := sorry theorem term_atom {α : Type u_1} [add_comm_monoid α] (x : α) : x = term 1 x 0 := sorry theorem term_atomg {α : Type u_1} [add_comm_group α] (x : α) : x = termg 1 x 0 := sorry theorem unfold_sub {α : Type u_1} [add_group α] (a : α) (b : α) (c : α) (h : a + -b = c) : a - b = c := eq.mpr (id (Eq._oldrec (Eq.refl (a - b = c)) (sub_eq_add_neg a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + -b = c)) h)) (Eq.refl c)) theorem unfold_smul {α : Type u_1} [add_comm_monoid α] (n : ℕ) (x : α) (y : α) (h : smul n x = y) : n •ℕ x = y := h theorem unfold_smulg {α : Type u_1} [add_comm_group α] (n : ℕ) (x : α) (y : α) (h : smulg (Int.ofNat n) x = y) : n •ℕ x = y := h theorem unfold_gsmul {α : Type u_1} [add_comm_group α] (n : ℤ) (x : α) (y : α) (h : smulg n x = y) : n •ℤ x = y := h theorem subst_into_smul {α : Type u_1} [add_comm_monoid α] (l : ℕ) (r : α) (tl : ℕ) (tr : α) (t : α) (prl : l = tl) (prr : r = tr) (prt : smul tl tr = t) : smul l r = t := sorry theorem subst_into_smulg {α : Type u_1} [add_comm_group α] (l : ℤ) (r : α) (tl : ℤ) (tr : α) (t : α) (prl : l = tl) (prr : r = tr) (prt : smulg tl tr = t) : smulg l r = t := sorry inductive normalize_mode where | raw : normalize_mode | term : normalize_mode protected instance normalize_mode.inhabited : Inhabited normalize_mode := { default := normalize_mode.term } end abel namespace interactive /-- Tactic for solving equations in the language of *additive*, commutative monoids and groups. This version of `abel` fails if the target is not an equality that is provable by the axioms of commutative monoids/groups. -/ /-- Evaluate expressions in the language of *additive*, commutative monoids and groups. It attempts to prove the goal outright if there is no `at` specifier and the target is an equality, but if this fails, it falls back to rewriting all monoid expressions into a normal form. If there is an `at` specifier, it rewrites the given target into a normal form. ```lean example {α : Type*} {a b : α} [add_comm_monoid α] : a + (b + a) = a + a + b := by abel example {α : Type*} {a b : α} [add_comm_group α] : (a + b) - ((b + a) + a) = -a := by abel example {α : Type*} {a b : α} [add_comm_group α] (hyp : a + a - a = b - b) : a = 0 := by { abel at hyp, exact hyp } ``` -/
786a2cfaa736c604906fd0dcafc68a16b9aace64
bdb33f8b7ea65f7705fc342a178508e2722eb851
/data/set/intervals.lean
606f211a0e2c52e599c7acf3793b26c222f455ae
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
4,118
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 Intervals Nameing conventions: `i`: infinite `o`: open `c`: closed Each interval has the name `I` + letter for left side + letter for right side TODO: This is just the beginning a lot of interavals and rules are missing -/ import data.set.lattice algebra.order algebra.order_functions namespace set open set section intervals variables {α : Type*} [preorder α] /-- Left-open right-open interval -/ def Ioo (a b : α) := {x | a < x ∧ x < b} /-- Left-closed right-open interval -/ def Ico (a b : α) := {x | a ≤ x ∧ x < b} /-- Left-infinite right-open interval -/ def Iio (a : α) := {x | x < a} end intervals section decidable_linear_order variables {α : Type*} [decidable_linear_order α] {a a₁ a₂ b b₁ b₂ : α} @[simp] lemma Ioo_eq_empty_of_ge {h : b ≤ a} : Ioo a b = ∅ := set.ext $ assume x, have a < x → b ≤ x, from assume ha, le_trans h $ le_of_lt ha, by simp [Ioo]; exact this lemma Ico_eq_empty_iff : Ico a b = ∅ ↔ (b ≤ a) := by rw ← not_lt; exact ⟨assume eq h, have a ∈ Ico a b, from ⟨le_refl a, h⟩, by rwa [eq] at this, assume h, eq_empty_iff_forall_not_mem.2 $ assume x ⟨h₁, h₂⟩, h $ lt_of_le_of_lt h₁ h₂⟩ @[simp] lemma Ico_eq_empty : b ≤ a → Ico a b = ∅ := Ico_eq_empty_iff.mpr lemma Ico_self : Ico a a = ∅ := Ico_eq_empty $ le_refl _ lemma Ico_subset_Ico_iff (h₁ : a₁ < b₁) : Ico a₁ b₁ ⊆ Ico a₂ b₂ ↔ (a₂ ≤ a₁ ∧ b₁ ≤ b₂) := ⟨assume h, have h' : a₁ ∈ Ico a₂ b₂, from h ⟨le_refl _, h₁⟩, have ¬ b₂ < b₁, from assume : b₂ < b₁, have b₂ ∈ Ico a₂ b₂, from h ⟨le_of_lt h'.right, this⟩, lt_irrefl b₂ this.right, ⟨h'.left, not_lt.1 $ this⟩, assume ⟨h₁, h₂⟩ x ⟨hx₁, hx₂⟩, ⟨le_trans h₁ hx₁, lt_of_lt_of_le hx₂ h₂⟩⟩ lemma Ico_subset_Ico_left (h₁ : a₁ ≤ a₂) : Ico a₂ b ⊆ Ico a₁ b := λ c, and.imp_left $ le_trans h₁ lemma Ico_subset_Ico_right (h₁ : b₁ ≤ b₂) : Ico a b₁ ⊆ Ico a b₂ := λ c, and.imp_right $ λ h₂, lt_of_lt_of_le h₂ h₁ lemma Ico_subset_Ioo_left (h₁ : a₁ < a₂) : Ico a₂ b ⊆ Ioo a₁ b := λ c, and.imp_left $ lt_of_lt_of_le h₁ lemma Ioo_subset_Ico_self : Ioo a b ⊆ Ico a b := λ c, and.imp_left le_of_lt lemma Ico_subset_Iio_self : Ioo a b ⊆ Iio b := λ c, and.right lemma Ioo_self : Ioo a a = ∅ := subset_eq_empty Ioo_subset_Ico_self Ico_self lemma Ico_sdiff_Ioo_eq_singleton (h : a < b) : Ico a b \ Ioo a b = {a} := set.ext $ λ c, by simp [Ioo, Ico, -not_lt]; exact ⟨λ ⟨⟨ac, cb⟩, h⟩, ((lt_or_eq_of_le ac).resolve_left $ λ hn, h hn cb).symm, λ e, e.symm ▸ ⟨⟨le_refl _, h⟩, (lt_irrefl _).elim⟩⟩ lemma Ico_eq_Ico_iff : Ico a₁ b₁ = Ico a₂ b₂ ↔ ((b₁ ≤ a₁ ∧ b₂ ≤ a₂) ∨ (a₁ = a₂ ∧ b₁ = b₂)) := begin cases lt_or_le a₁ b₁ with h₁ h₁; cases lt_or_le a₂ b₂ with h₂ h₂, { rw [subset.antisymm_iff, Ico_subset_Ico_iff h₁, Ico_subset_Ico_iff h₂], simp [iff_def, le_antisymm_iff, or_imp_distrib, not_le_of_gt h₁] {contextual := tt} }, { rw [Ico_eq_empty_iff.mpr h₂, Ico_eq_empty_iff], simp [iff_def, h₂, or_imp_distrib] {contextual := tt} }, { rw [Ico_eq_empty_iff.mpr h₁, eq_comm, Ico_eq_empty_iff], simp [iff_def, h₁, or_imp_distrib] {contextual := tt}, cc }, { rw [Ico_eq_empty_iff.mpr h₁, Ico_eq_empty_iff.mpr h₂], simp [iff_def, h₁, h₂] {contextual := tt} } end @[simp] lemma Ico_sdiff_Iio_eq {a b c : α} : Ico a b \ Iio c = Ico (max a c) b := set.ext $ by simp [Ico, Iio, iff_def, max_le_iff] {contextual:=tt} @[simp] lemma Ico_inter_Iio_eq {a b c : α} : Ico a b ∩ Iio c = Ico a (min b c) := set.ext $ by simp [Ico, Iio, iff_def, lt_min_iff] {contextual:=tt} lemma Ioo_inter_Ioo {a b c d : α} : Ioo a b ∩ Ioo c d = Ioo (max a c) (min b d) := set.ext $ by simp [iff_def, Ioo, lt_min_iff, max_lt_iff] {contextual := tt} end decidable_linear_order end set
662e0a1b069d5323e5f3bb1468a719760970c87e
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/field_theory/finite.lean
41c025489dfd5a0fdbb66ac02bc327243650f6cb
[ "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
7,426
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Joey van Langen, Casper Putz -/ import data.equiv.ring import data.zmod.basic import linear_algebra.basis import ring_theory.integral_domain universes u v variables {K : Type u} {R : Type v} open function finset polynomial nat namespace finite_field def field_of_integral_domain [fintype R] [decidable_eq R] [integral_domain R] : field R := { inv := λ a, if h : a = 0 then 0 else fintype.bij_inv (show function.bijective (* a), from fintype.injective_iff_bijective.1 $ λ _ _, (domain.mul_left_inj h).1) 1, mul_inv_cancel := λ a ha, show a * dite _ _ _ = _, by rw [dif_neg ha, mul_comm]; exact fintype.right_inverse_bij_inv (show function.bijective (* a), from _) 1, inv_zero := dif_pos rfl, ..show integral_domain R, by apply_instance } section polynomial variables [fintype R] [integral_domain R] open finset polynomial /-- The cardinality of a field is at most n times the cardinality of the image of a degree n polynomial -/ lemma card_image_polynomial_eval [decidable_eq R] {p : polynomial R} (hp : 0 < p.degree) : fintype.card R ≤ nat_degree p * (univ.image (λ x, eval x p)).card := finset.card_le_mul_card_image _ _ (λ a _, calc _ = (p - C a).roots.card : congr_arg card (by simp [finset.ext, mem_roots_sub_C hp, -sub_eq_add_neg]) ... ≤ _ : card_roots_sub_C' hp) /-- If `f` and `g` are quadratic polynomials, then the `f.eval a + g.eval b = 0` has a solution. -/ lemma exists_root_sum_quadratic {f g : polynomial R} (hf2 : degree f = 2) (hg2 : degree g = 2) (hR : fintype.card R % 2 = 1) : ∃ a b, f.eval a + g.eval b = 0 := by letI := classical.dec_eq R; exact suffices ¬ disjoint (univ.image (λ x : R, eval x f)) (univ.image (λ x : R, eval x (-g))), begin simp only [disjoint_left, mem_image] at this, push_neg at this, rcases this with ⟨x, ⟨a, _, ha⟩, ⟨b, _, hb⟩⟩, exact ⟨a, b, by rw [ha, ← hb, eval_neg, neg_add_self]⟩ end, assume hd : disjoint _ _, lt_irrefl (2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card) $ calc 2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card ≤ 2 * fintype.card R : nat.mul_le_mul_left _ (finset.card_le_of_subset (subset_univ _)) ... = fintype.card R + fintype.card R : two_mul _ ... < nat_degree f * (univ.image (λ x : R, eval x f)).card + nat_degree (-g) * (univ.image (λ x : R, eval x (-g))).card : add_lt_add_of_lt_of_le (lt_of_le_of_ne (card_image_polynomial_eval (by rw hf2; exact dec_trivial)) (mt (congr_arg (%2)) (by simp [nat_degree_eq_of_degree_eq_some hf2, hR]))) (card_image_polynomial_eval (by rw [degree_neg, hg2]; exact dec_trivial)) ... = 2 * (univ.image (λ x : R, eval x f) ∪ univ.image (λ x : R, eval x (-g))).card : by rw [card_disjoint_union hd]; simp [nat_degree_eq_of_degree_eq_some hf2, nat_degree_eq_of_degree_eq_some hg2, bit0, mul_add] end polynomial section variables [field K] [fintype K] lemma card_units : fintype.card (units K) = fintype.card K - 1 := begin classical, rw [eq_comm, nat.sub_eq_iff_eq_add (fintype.card_pos_iff.2 ⟨(0 : K)⟩)], haveI := set_fintype {a : K | a ≠ 0}, haveI := set_fintype (@set.univ K), rw [fintype.card_congr (equiv.units_equiv_ne_zero _), ← @set.card_insert _ _ {a : K | a ≠ 0} _ (not_not.2 (eq.refl (0 : K))) (set.fintype_insert _ _), fintype.card_congr (equiv.set.univ K).symm], congr; simp [set.ext_iff, classical.em] end instance : is_cyclic (units K) := by haveI := classical.dec_eq K; haveI := set_fintype (@set.univ (units K)); exact let ⟨g, hg⟩ := is_cyclic.exists_generator (@set.univ (units K)) in ⟨⟨g, λ x, let ⟨n, hn⟩ := hg ⟨x, trivial⟩ in ⟨n, by rw [← is_subgroup.coe_gpow, hn]; refl⟩⟩⟩ lemma prod_univ_units_id_eq_neg_one : univ.prod (λ x, x) = (-1 : units K) := begin classical, have : ((@univ (units K) _).erase (-1)).prod (λ x, x) = 1, from prod_involution (λ x _, x⁻¹) (by simp) (λ a, by simp [units.inv_eq_self_iff] {contextual := tt}) (λ a, by simp [@inv_eq_iff_inv_eq _ _ a, eq_comm] {contextual := tt}) (by simp), rw [← insert_erase (mem_univ (-1 : units K)), prod_insert (not_mem_erase _ _), this, mul_one] end theorem card (p : ℕ) [char_p K p] : ∃ (n : ℕ+), nat.prime p ∧ fintype.card K = p^(n : ℕ) := begin haveI hp : fact p.prime := char_p.char_is_prime K p, have V : vector_space (zmod p) K, from { .. (zmod.cast_hom p K).to_module }, obtain ⟨n, h⟩ := @vector_space.card_fintype _ _ _ _ V _ _, rw zmod.card at h, refine ⟨⟨n, _⟩, hp, h⟩, apply or.resolve_left (nat.eq_zero_or_pos n), rintro rfl, rw nat.pow_zero at h, have : (0 : K) = 1, { apply fintype.card_le_one_iff.mp (le_of_eq h) }, exact absurd this zero_ne_one, end theorem card' : ∃ (p : ℕ) (n : ℕ+), nat.prime p ∧ fintype.card K = p^(n : ℕ) := let ⟨p, hc⟩ := char_p.exists K in ⟨p, @finite_field.card K _ _ p hc⟩ end lemma pow_card_sub_one_eq_one [field K] [fintype K] (a : K) (ha : a ≠ 0) : a ^ (fintype.card K - 1) = 1 := calc a ^ (fintype.card K - 1) = (units.mk0 a ha ^ (fintype.card K - 1) : units K) : by rw [units.coe_pow, units.coe_mk0] ... = 1 : by { classical, rw [← card_units, pow_card_eq_one], refl } end finite_field namespace zmod open finite_field lemma sum_two_squares (p : ℕ) [hp : fact p.prime] (x : zmod p) : ∃ a b : zmod p, a^2 + b^2 = x := begin cases hp.eq_two_or_odd with hp2 hp_odd, { unfreezeI, subst p, revert x, exact dec_trivial }, let f : polynomial (zmod p) := X^2, let g : polynomial (zmod p) := X^2 - C x, obtain ⟨a, b, hab⟩ : ∃ a b, f.eval a + g.eval b = 0 := @exists_root_sum_quadratic _ _ _ f g (degree_X_pow 2) (degree_X_pow_sub_C dec_trivial _) (by rw [zmod.card, hp_odd]), refine ⟨a, b, _⟩, rw ← sub_eq_zero, simpa only [eval_C, eval_X, eval_pow, eval_sub, ← add_sub_assoc] using hab, end end zmod namespace char_p lemma sum_two_squares (R : Type*) [integral_domain R] (p : ℕ) [fact (0 < p)] [char_p R p] (x : ℤ) : ∃ a b : ℕ, (a^2 + b^2 : R) = x := begin haveI := char_is_prime_of_pos R p, obtain ⟨a, b, hab⟩ := zmod.sum_two_squares p x, refine ⟨a.val, b.val, _⟩, have := congr_arg (zmod.cast_hom p R) hab, simpa only [zmod.cast_int_cast, zmod.cast_hom_apply, zmod.cast_add, zmod.nat_cast_val, _root_.pow_two, zmod.cast_mul] end end char_p open_locale nat open zmod /-- The Fermat-Euler totient theorem. `nat.modeq.pow_totient` is an alternative statement of the same theorem. -/ @[simp] lemma zmod.pow_totient {n : ℕ} [fact (0 < n)] (x : units (zmod n)) : x ^ φ n = 1 := by rw [← card_units_eq_totient, pow_card_eq_one] /-- The Fermat-Euler totient theorem. `zmod.pow_totient` is an alternative statement of the same theorem. -/ lemma nat.modeq.pow_totient {x n : ℕ} (h : nat.coprime x n) : x ^ φ n ≡ 1 [MOD n] := begin cases n, {simp}, rw ← zmod.eq_iff_modeq_nat, let x' : units (zmod (n+1)) := zmod.unit_of_coprime _ h, have := zmod.pow_totient x', apply_fun (coe : units (zmod (n+1)) → zmod (n+1)) at this, simpa only [-zmod.pow_totient, succ_eq_add_one, cast_pow, units.coe_one, nat.cast_one, cast_unit_of_coprime, units.coe_pow], end
b34da950d9217ed171a8e9a344774da5dee9ca12
bb31430994044506fa42fd667e2d556327e18dfe
/src/analysis/normed_space/int.lean
51d30e87fa72d20169537fed49855516684af7dd
[ "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
1,439
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import analysis.normed.field.basic /-! # The integers as normed ring This file contains basic facts about the integers as normed ring. Recall that `‖n‖` denotes the norm of `n` as real number. This norm is always nonnegative, so we can bundle the norm together with this fact, to obtain a term of type `nnreal` (the nonnegative real numbers). The resulting nonnegative real number is denoted by `‖n‖₊`. -/ open_locale big_operators namespace int lemma nnnorm_coe_units (e : ℤˣ) : ‖(e : ℤ)‖₊ = 1 := begin obtain (rfl|rfl) := int.units_eq_one_or e; simp only [units.coe_neg_one, units.coe_one, nnnorm_neg, nnnorm_one], end lemma norm_coe_units (e : ℤˣ) : ‖(e : ℤ)‖ = 1 := by rw [← coe_nnnorm, int.nnnorm_coe_units, nnreal.coe_one] @[simp] lemma nnnorm_coe_nat (n : ℕ) : ‖(n : ℤ)‖₊ = n := real.nnnorm_coe_nat _ @[simp] lemma to_nat_add_to_nat_neg_eq_nnnorm (n : ℤ) : ↑(n.to_nat) + ↑((-n).to_nat) = ‖n‖₊ := by rw [← nat.cast_add, to_nat_add_to_nat_neg_eq_nat_abs, nnreal.coe_nat_abs] @[simp] lemma to_nat_add_to_nat_neg_eq_norm (n : ℤ) : ↑(n.to_nat) + ↑((-n).to_nat) = ‖n‖ := by simpa only [nnreal.coe_nat_cast, nnreal.coe_add] using congr_arg (coe : _ → ℝ) (to_nat_add_to_nat_neg_eq_nnnorm n) end int
30a8629b6e5ea34dca7529e71dc3604cc52ecf99
29cc89d6158dd3b90acbdbcab4d2c7eb9a7dbf0f
/Exercises week 2/12_exercise_sheet.lean
9e62a2124eebc092b613feb6030c2e8682e0794d
[]
no_license
KjellZijlemaker/Logical_Verification_VU
ced0ba95316a30e3c94ba8eebd58ea004fa6f53b
4578b93bf1615466996157bb333c84122b201d99
refs/heads/master
1,585,966,086,108
1,549,187,704,000
1,549,187,704,000
155,690,284
0
0
null
null
null
null
UTF-8
Lean
false
false
3,942
lean
/- Exercise 1.2: Basics — Proofs -/ /- Question 1: Natural numbers -/ namespace my_nat def add : ℕ → ℕ → ℕ | 0 n := n | (nat.succ m) n := nat.succ (add m n) -- def add : nat → nat → nat -- | m nat.zero := m -- | m (nat.succ n) := nat.succ (add m n) -- lemma natzero (m : nat) : add m 0 = m:= -- begin -- induction m, -- simp[add], -- refl -- end -- lemma test (m : nat) : add 0 m = m:= -- begin -- induction m, -- refl, -- simp[add] -- end -- lemma add_commm (m n : nat) : add m n = add n m := -- begin -- induction m, -- simp[natzero, add], -- end /- We proved these properties already in the lecture, so we take them as axioms here. (We will often use `sorry` like this to avoid clutter in exercise sheets. But keep in mind that `sorry` is a risky construct.) -/ lemma add_zero : ∀m : ℕ, add m 0 = m | (nat.succ m) := by simp [add, add_zero m] | 0 := by refl lemma add_succ : ∀m n : ℕ, add m (nat.succ n) = nat.succ (add m n) | m 0 := by refl lemma add_comm : ∀m n : ℕ, add m n = add n m := sorry lemma add_assoc : ∀l m n : ℕ, add (add l m) n = add l (add m n) := sorry instance : is_commutative ℕ add := ⟨add_comm⟩ instance : is_associative ℕ add := ⟨add_assoc⟩ def mul : ℕ → ℕ → ℕ | 0 _ := 0 | (nat.succ m) n := add n (mul m n) lemma mul_add (m n : ℕ) : ∀l : ℕ, mul (add l m) n = add (mul l n) (mul m n) := sorry /- 1.1. Prove the following recursive equations on the second argument of `mul`. -/ lemma mul_zero : ∀m : ℕ, mul m 0 = 0 := sorry lemma mul_succ : ∀m n : ℕ, mul m (nat.succ n) = add (mul m n) m := sorry /- 1.2. Prove commutativity and associativity of multiplication by induction. Choose the induction variable carefully. Use the pattern matching syntax. -/ lemma mul_comm : ∀m n : ℕ, mul m n = mul n m := sorry lemma mul_assoc : ∀l m n : ℕ, mul (mul l m) n = mul l (mul m n) := sorry /- 1.3. Prove the same lemmas as in 1.2, but using the `induction` tactic instead. Try to reuse as much of the above proof as possible. -/ example (m n : ℕ) : mul m n = mul n m := sorry example : ∀l m n : ℕ, mul (mul l m) n = mul l (mul m n) := sorry /- 1.4. Prove the symmetric variant of `mul_add` using `rw`. To apply commutativity at a specific position instantiate the rule (e.g. `mul_comm n`). -/ lemma add_mul (m n l : ℕ) : mul n (add l m) = add (mul n l) (mul n m) := sorry end my_nat /- Question 2: Lists -/ def reverse {α : Type} : list α → list α | [] := [] | (x :: xs) := reverse xs ++ [x] /- Another pesky `sorry`. -/ lemma reverse_reverse {α : Type} (xs : list α) : reverse (reverse xs) = xs := sorry /- We define a new accumulator-based version of `reverse`. The first argument serves as the accumulator. This definition is *tail-recursive*, meaning that compilers and interpreters can easily optimize the recursion away, resulting in more efficient code. -/ def areverse {α : Type} : list α → list α → list α | ys [] := ys | ys (x :: xs) := areverse (x :: ys) xs /- 2.1. Our intention is that `areverse [] xs` should be equal to `reverse xs`. But if we start an induction, we quickly see that the induction hypothesis is not strong enough. Start by proving the following generalization (using pattern matching or the `induction` tactic): -/ -- lemma areverse_eq_reverse_append {α : Type} : ∀ys xs : list α, areverse ys xs = reverse xs ++ ys -- | [] xs := begin simp[reverse], simp[areverse_eq_reverse] end /- 2.2. Derive the desired equation. -/ lemma areverse_eq_reverse {α : Type} (xs : list α) : areverse [] xs = reverse xs:= begin induction xs, simp[areverse], simp[reverse], simp[areverse], simp[reverse], rw[<-xs_ih], end /- 2.3. Prove the following property. Hint: A one-line inductionless proof is possible. -/ lemma areverse_areverse {α : Type} (xs : list α) : areverse [] (areverse [] xs) = xs := := sorry
22db126791e5fc0c99db57a8f643e08850944341
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/analysis/normed/group/hom.lean
4142138d6d579c4151bc243d8f014f0afbdd8d15
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
34,755
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import analysis.normed_space.basic import analysis.specific_limits import topology.sequences /-! # Normed groups homomorphisms This file gathers definitions and elementary constructions about bounded group homomorphisms between normed (abelian) groups (abbreviated to "normed group homs"). The main lemmas relate the boundedness condition to continuity and Lipschitzness. The main construction is to endow the type of normed group homs between two given normed groups with a group structure and a norm, giving rise to a normed group structure. We provide several simple constructions for normed group homs, like kernel, range and equalizer. Some easy other constructions are related to subgroups of normed groups. Since a lot of elementary properties don't require `∥x∥ = 0 → x = 0` we start setting up the theory of `semi_normed_group_hom` and we specialize to `normed_group_hom` when needed. -/ noncomputable theory open_locale nnreal big_operators /-- A morphism of seminormed abelian groups is a bounded group homomorphism. -/ structure normed_group_hom (V W : Type*) [semi_normed_group V] [semi_normed_group W] := (to_fun : V → W) (map_add' : ∀ v₁ v₂, to_fun (v₁ + v₂) = to_fun v₁ + to_fun v₂) (bound' : ∃ C, ∀ v, ∥to_fun v∥ ≤ C * ∥v∥) namespace add_monoid_hom variables {V W : Type*} [semi_normed_group V] [semi_normed_group W] {f g : normed_group_hom V W} /-- Associate to a group homomorphism a bounded group homomorphism under a norm control condition. See `add_monoid_hom.mk_normed_group_hom'` for a version that uses `ℝ≥0` for the bound. -/ def mk_normed_group_hom (f : V →+ W) (C : ℝ) (h : ∀ v, ∥f v∥ ≤ C * ∥v∥) : normed_group_hom V W := { bound' := ⟨C, h⟩, ..f } /-- Associate to a group homomorphism a bounded group homomorphism under a norm control condition. See `add_monoid_hom.mk_normed_group_hom` for a version that uses `ℝ` for the bound. -/ def mk_normed_group_hom' (f : V →+ W) (C : ℝ≥0) (hC : ∀ x, nnnorm (f x) ≤ C * nnnorm x) : normed_group_hom V W := { bound' := ⟨C, hC⟩ .. f} end add_monoid_hom lemma exists_pos_bound_of_bound {V W : Type*} [semi_normed_group V] [semi_normed_group W] {f : V → W} (M : ℝ) (h : ∀x, ∥f x∥ ≤ M * ∥x∥) : ∃ N, 0 < N ∧ ∀x, ∥f x∥ ≤ N * ∥x∥ := ⟨max M 1, lt_of_lt_of_le zero_lt_one (le_max_right _ _), λx, calc ∥f x∥ ≤ M * ∥x∥ : h x ... ≤ max M 1 * ∥x∥ : mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _) ⟩ namespace normed_group_hom variables {V V₁ V₂ V₃ : Type*} variables [semi_normed_group V] [semi_normed_group V₁] [semi_normed_group V₂] [semi_normed_group V₃] variables {f g : normed_group_hom V₁ V₂} instance : has_coe_to_fun (normed_group_hom V₁ V₂) := ⟨_, normed_group_hom.to_fun⟩ initialize_simps_projections normed_group_hom (to_fun → apply) lemma coe_inj (H : ⇑f = g) : f = g := by cases f; cases g; congr'; exact funext H lemma coe_injective : @function.injective (normed_group_hom V₁ V₂) (V₁ → V₂) coe_fn := by apply coe_inj lemma coe_inj_iff : f = g ↔ ⇑f = g := ⟨congr_arg _, coe_inj⟩ @[ext] lemma ext (H : ∀ x, f x = g x) : f = g := coe_inj $ funext H lemma ext_iff : f = g ↔ ∀ x, f x = g x := ⟨by rintro rfl x; refl, ext⟩ variables (f g) @[simp] lemma to_fun_eq_coe : f.to_fun = f := rfl @[simp] lemma coe_mk (f) (h₁) (h₂) (h₃) : ⇑(⟨f, h₁, h₂, h₃⟩ : normed_group_hom V₁ V₂) = f := rfl @[simp] lemma coe_mk_normed_group_hom (f : V₁ →+ V₂) (C) (hC) : ⇑(f.mk_normed_group_hom C hC) = f := rfl @[simp] lemma coe_mk_normed_group_hom' (f : V₁ →+ V₂) (C) (hC) : ⇑(f.mk_normed_group_hom' C hC) = f := rfl /-- The group homomorphism underlying a bounded group homomorphism. -/ def to_add_monoid_hom (f : normed_group_hom V₁ V₂) : V₁ →+ V₂ := add_monoid_hom.mk' f f.map_add' @[simp] lemma coe_to_add_monoid_hom : ⇑f.to_add_monoid_hom = f := rfl lemma to_add_monoid_hom_injective : function.injective (@normed_group_hom.to_add_monoid_hom V₁ V₂ _ _) := λ f g h, coe_inj $ show ⇑f.to_add_monoid_hom = g, by { rw h, refl } @[simp] lemma mk_to_add_monoid_hom (f) (h₁) (h₂) : (⟨f, h₁, h₂⟩ : normed_group_hom V₁ V₂).to_add_monoid_hom = add_monoid_hom.mk' f h₁ := rfl @[simp] lemma map_zero : f 0 = 0 := f.to_add_monoid_hom.map_zero @[simp] lemma map_add (x y) : f (x + y) = f x + f y := f.to_add_monoid_hom.map_add _ _ @[simp] lemma map_sum {ι : Type*} (v : ι → V₁) (s : finset ι) : f (∑ i in s, v i) = ∑ i in s, f (v i) := f.to_add_monoid_hom.map_sum _ _ @[simp] lemma map_sub (x y) : f (x - y) = f x - f y := f.to_add_monoid_hom.map_sub _ _ @[simp] lemma map_neg (x) : f (-x) = -(f x) := f.to_add_monoid_hom.map_neg _ lemma bound : ∃ C, 0 < C ∧ ∀ x, ∥f x∥ ≤ C * ∥x∥ := let ⟨C, hC⟩ := f.bound' in exists_pos_bound_of_bound _ hC theorem antilipschitz_of_norm_ge {K : ℝ≥0} (h : ∀ x, ∥x∥ ≤ K * ∥f x∥) : antilipschitz_with K f := antilipschitz_with.of_le_mul_dist $ λ x y, by simpa only [dist_eq_norm, f.map_sub] using h (x - y) /-- A normed group hom is surjective on the subgroup `K` with constant `C` if every element `x` of `K` has a preimage whose norm is bounded above by `C*∥x∥`. This is a more abstract version of `f` having a right inverse defined on `K` with operator norm at most `C`. -/ def surjective_on_with (f : normed_group_hom V₁ V₂) (K : add_subgroup V₂) (C : ℝ) : Prop := ∀ h ∈ K, ∃ g, f g = h ∧ ∥g∥ ≤ C*∥h∥ lemma surjective_on_with.mono {f : normed_group_hom V₁ V₂} {K : add_subgroup V₂} {C C' : ℝ} (h : f.surjective_on_with K C) (H : C ≤ C') : f.surjective_on_with K C' := begin intros k k_in, rcases h k k_in with ⟨g, rfl, hg⟩, use [g, rfl], by_cases Hg : ∥f g∥ = 0, { simpa [Hg] using hg }, { exact hg.trans ((mul_le_mul_right $ (ne.symm Hg).le_iff_lt.mp (norm_nonneg _)).mpr H) } end lemma surjective_on_with.exists_pos {f : normed_group_hom V₁ V₂} {K : add_subgroup V₂} {C : ℝ} (h : f.surjective_on_with K C) : ∃ C' > 0, f.surjective_on_with K C' := begin refine ⟨|C| + 1, _, _⟩, { linarith [abs_nonneg C] }, { apply h.mono, linarith [le_abs_self C] } end lemma surjective_on_with.surj_on {f : normed_group_hom V₁ V₂} {K : add_subgroup V₂} {C : ℝ} (h : f.surjective_on_with K C) : set.surj_on f set.univ K := λ x hx, (h x hx).imp $ λ a ⟨ha, _⟩, ⟨set.mem_univ _, ha⟩ /-! ### The operator norm -/ /-- The operator norm of a seminormed group homomorphism is the inf of all its bounds. -/ def op_norm (f : normed_group_hom V₁ V₂) := Inf {c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥} instance has_op_norm : has_norm (normed_group_hom V₁ V₂) := ⟨op_norm⟩ lemma norm_def : ∥f∥ = Inf {c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥} := rfl -- So that invocations of `le_cInf` make sense: we show that the set of -- bounds is nonempty and bounded below. lemma bounds_nonempty {f : normed_group_hom V₁ V₂} : ∃ c, c ∈ { c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥ } := let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩ lemma bounds_bdd_below {f : normed_group_hom V₁ V₂} : bdd_below {c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥} := ⟨0, λ _ ⟨hn, _⟩, hn⟩ lemma op_norm_nonneg : 0 ≤ ∥f∥ := le_cInf bounds_nonempty (λ _ ⟨hx, _⟩, hx) /-- The fundamental property of the operator norm: `∥f x∥ ≤ ∥f∥ * ∥x∥`. -/ theorem le_op_norm (x : V₁) : ∥f x∥ ≤ ∥f∥ * ∥x∥ := begin obtain ⟨C, Cpos, hC⟩ := f.bound, replace hC := hC x, by_cases h : ∥x∥ = 0, { rwa [h, mul_zero] at ⊢ hC }, have hlt : 0 < ∥x∥ := lt_of_le_of_ne (norm_nonneg x) (ne.symm h), exact (div_le_iff hlt).mp (le_cInf bounds_nonempty (λ c ⟨_, hc⟩, (div_le_iff hlt).mpr $ by { apply hc })), end theorem le_op_norm_of_le {c : ℝ} {x} (h : ∥x∥ ≤ c) : ∥f x∥ ≤ ∥f∥ * c := le_trans (f.le_op_norm x) (mul_le_mul_of_nonneg_left h f.op_norm_nonneg) theorem le_of_op_norm_le {c : ℝ} (h : ∥f∥ ≤ c) (x : V₁) : ∥f x∥ ≤ c * ∥x∥ := (f.le_op_norm x).trans (mul_le_mul_of_nonneg_right h (norm_nonneg x)) /-- continuous linear maps are Lipschitz continuous. -/ theorem lipschitz : lipschitz_with ⟨∥f∥, op_norm_nonneg f⟩ f := lipschitz_with.of_dist_le_mul $ λ x y, by { rw [dist_eq_norm, dist_eq_norm, ←map_sub], apply le_op_norm } protected lemma uniform_continuous (f : normed_group_hom V₁ V₂) : uniform_continuous f := f.lipschitz.uniform_continuous @[continuity] protected lemma continuous (f : normed_group_hom V₁ V₂) : continuous f := f.uniform_continuous.continuous lemma ratio_le_op_norm (x : V₁) : ∥f x∥ / ∥x∥ ≤ ∥f∥ := div_le_of_nonneg_of_le_mul (norm_nonneg _) f.op_norm_nonneg (le_op_norm _ _) /-- If one controls the norm of every `f x`, then one controls the norm of `f`. -/ lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ x, ∥f x∥ ≤ M * ∥x∥) : ∥f∥ ≤ M := cInf_le bounds_bdd_below ⟨hMp, hM⟩ lemma op_norm_eq_of_bounds {M : ℝ} (M_nonneg : 0 ≤ M) (h_above : ∀ x, ∥f x∥ ≤ M*∥x∥) (h_below : ∀ N ≥ 0, (∀ x, ∥f x∥ ≤ N*∥x∥) → M ≤ N) : ∥f∥ = M := le_antisymm (f.op_norm_le_bound M_nonneg h_above) ((le_cInf_iff normed_group_hom.bounds_bdd_below ⟨M, M_nonneg, h_above⟩).mpr $ λ N ⟨N_nonneg, hN⟩, h_below N N_nonneg hN) theorem op_norm_le_of_lipschitz {f : normed_group_hom V₁ V₂} {K : ℝ≥0} (hf : lipschitz_with K f) : ∥f∥ ≤ K := f.op_norm_le_bound K.2 $ λ x, by simpa only [dist_zero_right, f.map_zero] using hf.dist_le_mul x 0 /-- If a bounded group homomorphism map is constructed from a group homomorphism via the constructor `mk_normed_group_hom`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma mk_normed_group_hom_norm_le (f : V₁ →+ V₂) {C : ℝ} (hC : 0 ≤ C) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : ∥f.mk_normed_group_hom C h∥ ≤ C := op_norm_le_bound _ hC h /-- If a bounded group homomorphism map is constructed from a group homomorphism via the constructor `mk_normed_group_hom`, then its norm is bounded by the bound given to the constructor or zero if this bound is negative. -/ lemma mk_normed_group_hom_norm_le' (f : V₁ →+ V₂) {C : ℝ} (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : ∥f.mk_normed_group_hom C h∥ ≤ max C 0 := op_norm_le_bound _ (le_max_right _ _) $ λ x, (h x).trans $ mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg x) alias mk_normed_group_hom_norm_le ← add_monoid_hom.mk_normed_group_hom_norm_le alias mk_normed_group_hom_norm_le' ← add_monoid_hom.mk_normed_group_hom_norm_le' /-! ### Addition of normed group homs -/ /-- Addition of normed group homs. -/ instance : has_add (normed_group_hom V₁ V₂) := ⟨λ f g, (f.to_add_monoid_hom + g.to_add_monoid_hom).mk_normed_group_hom (∥f∥ + ∥g∥) $ λ v, calc ∥f v + g v∥ ≤ ∥f v∥ + ∥g v∥ : norm_add_le _ _ ... ≤ ∥f∥ * ∥v∥ + ∥g∥ * ∥v∥ : add_le_add (le_op_norm f v) (le_op_norm g v) ... = (∥f∥ + ∥g∥) * ∥v∥ : by rw add_mul⟩ /-- The operator norm satisfies the triangle inequality. -/ theorem op_norm_add_le : ∥f + g∥ ≤ ∥f∥ + ∥g∥ := mk_normed_group_hom_norm_le _ (add_nonneg (op_norm_nonneg _) (op_norm_nonneg _)) _ /-- Terms containing `@has_add.add (has_coe_to_fun.F ...) pi.has_add` seem to cause leanchecker to [crash due to an out-of-memory condition](https://github.com/leanprover-community/lean/issues/543). As a workaround, we add a type annotation: `(f + g : V₁ → V₂)` -/ library_note "addition on function coercions" -- see Note [addition on function coercions] @[simp] lemma coe_add (f g : normed_group_hom V₁ V₂) : ⇑(f + g) = (f + g : V₁ → V₂) := rfl @[simp] lemma add_apply (f g : normed_group_hom V₁ V₂) (v : V₁) : (f + g : normed_group_hom V₁ V₂) v = f v + g v := rfl /-! ### The zero normed group hom -/ instance : has_zero (normed_group_hom V₁ V₂) := ⟨(0 : V₁ →+ V₂).mk_normed_group_hom 0 (by simp)⟩ instance : inhabited (normed_group_hom V₁ V₂) := ⟨0⟩ /-- The norm of the `0` operator is `0`. -/ theorem op_norm_zero : ∥(0 : normed_group_hom V₁ V₂)∥ = 0 := le_antisymm (cInf_le bounds_bdd_below ⟨ge_of_eq rfl, λ _, le_of_eq (by { rw [zero_mul], exact norm_zero })⟩) (op_norm_nonneg _) /-- For normed groups, an operator is zero iff its norm vanishes. -/ theorem op_norm_zero_iff {V₁ V₂ : Type*} [normed_group V₁] [normed_group V₂] {f : normed_group_hom V₁ V₂} : ∥f∥ = 0 ↔ f = 0 := iff.intro (λ hn, ext (λ x, norm_le_zero_iff.1 (calc _ ≤ ∥f∥ * ∥x∥ : le_op_norm _ _ ... = _ : by rw [hn, zero_mul]))) (λ hf, by rw [hf, op_norm_zero] ) -- see Note [addition on function coercions] @[simp] lemma coe_zero : ⇑(0 : normed_group_hom V₁ V₂) = (0 : V₁ → V₂) := rfl @[simp] lemma zero_apply (v : V₁) : (0 : normed_group_hom V₁ V₂) v = 0 := rfl variables {f g} /-! ### The identity normed group hom -/ variable (V) /-- The identity as a continuous normed group hom. -/ @[simps] def id : normed_group_hom V V := (add_monoid_hom.id V).mk_normed_group_hom 1 (by simp [le_refl]) /-- The norm of the identity is at most `1`. It is in fact `1`, except when the norm of every element vanishes, where it is `0`. (Since we are working with seminorms this can happen even if the space is non-trivial.) It means that one can not do better than an inequality in general. -/ lemma norm_id_le : ∥(id V : normed_group_hom V V)∥ ≤ 1 := op_norm_le_bound _ zero_le_one (λx, by simp) /-- If there is an element with norm different from `0`, then the norm of the identity equals `1`. (Since we are working with seminorms supposing that the space is non-trivial is not enough.) -/ lemma norm_id_of_nontrivial_seminorm (h : ∃ (x : V), ∥x∥ ≠ 0 ) : ∥(id V)∥ = 1 := le_antisymm (norm_id_le V) $ let ⟨x, hx⟩ := h in have _ := (id V).ratio_le_op_norm x, by rwa [id_apply, div_self hx] at this /-- If a normed space is non-trivial, then the norm of the identity equals `1`. -/ lemma norm_id {V : Type*} [normed_group V] [nontrivial V] : ∥(id V)∥ = 1 := begin refine norm_id_of_nontrivial_seminorm V _, obtain ⟨x, hx⟩ := exists_ne (0 : V), exact ⟨x, ne_of_gt (norm_pos_iff.2 hx)⟩, end lemma coe_id : ((normed_group_hom.id V) : V → V) = (_root_.id : V → V) := rfl /-! ### The negation of a normed group hom -/ /-- Opposite of a normed group hom. -/ instance : has_neg (normed_group_hom V₁ V₂) := ⟨λ f, (-f.to_add_monoid_hom).mk_normed_group_hom (∥f∥) (λ v, by simp [le_op_norm f v])⟩ -- see Note [addition on function coercions] @[simp] lemma coe_neg (f : normed_group_hom V₁ V₂) : ⇑(-f) = (-f : V₁ → V₂) := rfl @[simp] lemma neg_apply (f : normed_group_hom V₁ V₂) (v : V₁) : (-f : normed_group_hom V₁ V₂) v = - (f v) := rfl lemma op_norm_neg (f : normed_group_hom V₁ V₂) : ∥-f∥ = ∥f∥ := by simp only [norm_def, coe_neg, norm_neg, pi.neg_apply] /-! ### Subtraction of normed group homs -/ /-- Subtraction of normed group homs. -/ instance : has_sub (normed_group_hom V₁ V₂) := ⟨λ f g, { bound' := begin simp only [add_monoid_hom.sub_apply, add_monoid_hom.to_fun_eq_coe, sub_eq_add_neg], exact (f + -g).bound' end, .. (f.to_add_monoid_hom - g.to_add_monoid_hom) }⟩ -- see Note [addition on function coercions] @[simp] lemma coe_sub (f g : normed_group_hom V₁ V₂) : ⇑(f - g) = (f - g : V₁ → V₂) := rfl @[simp] lemma sub_apply (f g : normed_group_hom V₁ V₂) (v : V₁) : (f - g : normed_group_hom V₁ V₂) v = f v - g v := rfl /-! ### Normed group structure on normed group homs -/ /-- Homs between two given normed groups form a commutative additive group. -/ instance : add_comm_group (normed_group_hom V₁ V₂) := coe_injective.add_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- Normed group homomorphisms themselves form a seminormed group with respect to the operator norm. -/ instance to_semi_normed_group : semi_normed_group (normed_group_hom V₁ V₂) := semi_normed_group.of_core _ ⟨op_norm_zero, op_norm_add_le, op_norm_neg⟩ /-- Normed group homomorphisms themselves form a normed group with respect to the operator norm. -/ instance to_normed_group {V₁ V₂ : Type*} [normed_group V₁] [normed_group V₂] : normed_group (normed_group_hom V₁ V₂) := normed_group.of_core _ ⟨λ f, op_norm_zero_iff, op_norm_add_le, op_norm_neg⟩ /-- Coercion of a `normed_group_hom` is an `add_monoid_hom`. Similar to `add_monoid_hom.coe_fn` -/ @[simps] def coe_fn_add_hom : normed_group_hom V₁ V₂ →+ (V₁ → V₂) := { to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add} @[simp] lemma coe_sum {ι : Type*} (s : finset ι) (f : ι → normed_group_hom V₁ V₂) : ⇑(∑ i in s, f i) = ∑ i in s, (f i) := (coe_fn_add_hom : _ →+ (V₁ → V₂)).map_sum f s lemma sum_apply {ι : Type*} (s : finset ι) (f : ι → normed_group_hom V₁ V₂) (v : V₁) : (∑ i in s, f i) v = ∑ i in s, (f i v) := by simp only [coe_sum, finset.sum_apply] /-! ### Composition of normed group homs -/ /-- The composition of continuous normed group homs. -/ @[simps] protected def comp (g : normed_group_hom V₂ V₃) (f : normed_group_hom V₁ V₂) : normed_group_hom V₁ V₃ := (g.to_add_monoid_hom.comp f.to_add_monoid_hom).mk_normed_group_hom (∥g∥ * ∥f∥) $ λ v, calc ∥g (f v)∥ ≤ ∥g∥ * ∥f v∥ : le_op_norm _ _ ... ≤ ∥g∥ * (∥f∥ * ∥v∥) : mul_le_mul_of_nonneg_left (le_op_norm _ _) (op_norm_nonneg _) ... = ∥g∥ * ∥f∥ * ∥v∥ : by rw mul_assoc lemma norm_comp_le (g : normed_group_hom V₂ V₃) (f : normed_group_hom V₁ V₂) : ∥g.comp f∥ ≤ ∥g∥ * ∥f∥ := mk_normed_group_hom_norm_le _ (mul_nonneg (op_norm_nonneg _) (op_norm_nonneg _)) _ lemma norm_comp_le_of_le {g : normed_group_hom V₂ V₃} {C₁ C₂ : ℝ} (hg : ∥g∥ ≤ C₂) (hf : ∥f∥ ≤ C₁) : ∥g.comp f∥ ≤ C₂ * C₁ := le_trans (norm_comp_le g f) $ mul_le_mul hg hf (norm_nonneg _) (le_trans (norm_nonneg _) hg) lemma norm_comp_le_of_le' {g : normed_group_hom V₂ V₃} (C₁ C₂ C₃ : ℝ) (h : C₃ = C₂ * C₁) (hg : ∥g∥ ≤ C₂) (hf : ∥f∥ ≤ C₁) : ∥g.comp f∥ ≤ C₃ := by { rw h, exact norm_comp_le_of_le hg hf } /-- Composition of normed groups hom as an additive group morphism. -/ def comp_hom : (normed_group_hom V₂ V₃) →+ (normed_group_hom V₁ V₂) →+ (normed_group_hom V₁ V₃) := add_monoid_hom.mk' (λ g, add_monoid_hom.mk' (λ f, g.comp f) (by { intros, ext, exact g.map_add _ _ })) (by { intros, ext, simp only [comp_apply, pi.add_apply, function.comp_app, add_monoid_hom.add_apply, add_monoid_hom.mk'_apply, coe_add] }) @[simp] lemma comp_zero (f : normed_group_hom V₂ V₃) : f.comp (0 : normed_group_hom V₁ V₂) = 0 := by { ext, exact f.map_zero } @[simp] lemma zero_comp (f : normed_group_hom V₁ V₂) : (0 : normed_group_hom V₂ V₃).comp f = 0 := by { ext, refl } lemma comp_assoc {V₄: Type* } [semi_normed_group V₄] (h : normed_group_hom V₃ V₄) (g : normed_group_hom V₂ V₃) (f : normed_group_hom V₁ V₂) : (h.comp g).comp f = h.comp (g.comp f) := by { ext, refl } lemma coe_comp (f : normed_group_hom V₁ V₂) (g : normed_group_hom V₂ V₃) : (g.comp f : V₁ → V₃) = (g : V₂ → V₃) ∘ (f : V₁ → V₂) := rfl end normed_group_hom namespace normed_group_hom variables {V W V₁ V₂ V₃ : Type*} variables [semi_normed_group V] [semi_normed_group W] [semi_normed_group V₁] [semi_normed_group V₂] [semi_normed_group V₃] /-- The inclusion of an `add_subgroup`, as bounded group homomorphism. -/ @[simps] def incl (s : add_subgroup V) : normed_group_hom s V := { to_fun := (coe : s → V), map_add' := λ v w, add_subgroup.coe_add _ _ _, bound' := ⟨1, λ v, by { rw [one_mul], refl }⟩ } lemma norm_incl {V' : add_subgroup V} (x : V') : ∥incl _ x∥ = ∥x∥ := rfl /-!### Kernel -/ section kernels variables (f : normed_group_hom V₁ V₂) (g : normed_group_hom V₂ V₃) /-- The kernel of a bounded group homomorphism. Naturally endowed with a `semi_normed_group` instance. -/ def ker : add_subgroup V₁ := f.to_add_monoid_hom.ker lemma mem_ker (v : V₁) : v ∈ f.ker ↔ f v = 0 := by { erw f.to_add_monoid_hom.mem_ker, refl } /-- Given a normed group hom `f : V₁ → V₂` satisfying `g.comp f = 0` for some `g : V₂ → V₃`, the corestriction of `f` to the kernel of `g`. -/ @[simps] def ker.lift (h : g.comp f = 0) : normed_group_hom V₁ g.ker := { to_fun := λ v, ⟨f v, by { erw g.mem_ker, show (g.comp f) v = 0, rw h, refl }⟩, map_add' := λ v w, by { simp only [map_add], refl }, bound' := f.bound' } @[simp] lemma ker.incl_comp_lift (h : g.comp f = 0) : (incl g.ker).comp (ker.lift f g h) = f := by { ext, refl } @[simp] lemma ker_zero : (0 : normed_group_hom V₁ V₂).ker = ⊤ := by { ext, simp [mem_ker] } lemma coe_ker : (f.ker : set V₁) = (f : V₁ → V₂) ⁻¹' {0} := rfl lemma is_closed_ker {V₂ : Type*} [normed_group V₂] (f : normed_group_hom V₁ V₂) : is_closed (f.ker : set V₁) := f.coe_ker ▸ is_closed.preimage f.continuous (t1_space.t1 0) end kernels /-! ### Range -/ section range variables (f : normed_group_hom V₁ V₂) (g : normed_group_hom V₂ V₃) /-- The image of a bounded group homomorphism. Naturally endowed with a `semi_normed_group` instance. -/ def range : add_subgroup V₂ := f.to_add_monoid_hom.range lemma mem_range (v : V₂) : v ∈ f.range ↔ ∃ w, f w = v := by { rw [range, add_monoid_hom.mem_range], refl } @[simp] lemma mem_range_self (v : V₁) : f v ∈ f.range := ⟨v, rfl⟩ lemma comp_range : (g.comp f).range = add_subgroup.map g.to_add_monoid_hom f.range := by { erw add_monoid_hom.map_range, refl } lemma incl_range (s : add_subgroup V₁) : (incl s).range = s := by { ext x, exact ⟨λ ⟨y, hy⟩, by { rw ← hy; simp }, λ hx, ⟨⟨x, hx⟩, by simp⟩⟩ } @[simp] lemma range_comp_incl_top : (f.comp (incl (⊤ : add_subgroup V₁))).range = f.range := by simpa [comp_range, incl_range, ← add_monoid_hom.range_eq_map] end range variables {f : normed_group_hom V W} /-- A `normed_group_hom` is *norm-nonincreasing* if `∥f v∥ ≤ ∥v∥` for all `v`. -/ def norm_noninc (f : normed_group_hom V W) : Prop := ∀ v, ∥f v∥ ≤ ∥v∥ namespace norm_noninc lemma norm_noninc_iff_norm_le_one : f.norm_noninc ↔ ∥f∥ ≤ 1 := begin refine ⟨λ h, _, λ h, λ v, _⟩, { refine op_norm_le_bound _ (zero_le_one) (λ v, _), simpa [one_mul] using h v }, { simpa using le_of_op_norm_le f h v } end lemma zero : (0 : normed_group_hom V₁ V₂).norm_noninc := λ v, by simp lemma id : (id V).norm_noninc := λ v, le_rfl lemma comp {g : normed_group_hom V₂ V₃} {f : normed_group_hom V₁ V₂} (hg : g.norm_noninc) (hf : f.norm_noninc) : (g.comp f).norm_noninc := λ v, (hg (f v)).trans (hf v) @[simp] lemma neg_iff {f : normed_group_hom V₁ V₂} : (-f).norm_noninc ↔ f.norm_noninc := ⟨λ h x, by { simpa using h x }, λ h x, (norm_neg (f x)).le.trans (h x)⟩ end norm_noninc section isometry lemma isometry_iff_norm (f : normed_group_hom V W) : isometry f ↔ ∀ v, ∥f v∥ = ∥v∥ := add_monoid_hom.isometry_iff_norm f.to_add_monoid_hom lemma isometry_of_norm (f : normed_group_hom V W) (hf : ∀ v, ∥f v∥ = ∥v∥) : isometry f := f.isometry_iff_norm.mpr hf lemma norm_eq_of_isometry {f : normed_group_hom V W} (hf : isometry f) (v : V) : ∥f v∥ = ∥v∥ := f.isometry_iff_norm.mp hf v lemma isometry_id : @isometry V V _ _ (id V) := isometry_id lemma isometry_comp {g : normed_group_hom V₂ V₃} {f : normed_group_hom V₁ V₂} (hg : isometry g) (hf : isometry f) : isometry (g.comp f) := hg.comp hf lemma norm_noninc_of_isometry (hf : isometry f) : f.norm_noninc := λ v, le_of_eq $ norm_eq_of_isometry hf v end isometry variables {W₁ W₂ W₃ : Type*} [semi_normed_group W₁] [semi_normed_group W₂] [semi_normed_group W₃] variables (f) (g : normed_group_hom V W) variables {f₁ g₁ : normed_group_hom V₁ W₁} variables {f₂ g₂ : normed_group_hom V₂ W₂} variables {f₃ g₃ : normed_group_hom V₃ W₃} /-- The equalizer of two morphisms `f g : normed_group_hom V W`. -/ def equalizer := (f - g).ker namespace equalizer /-- The inclusion of `f.equalizer g` as a `normed_group_hom`. -/ def ι : normed_group_hom (f.equalizer g) V := incl _ lemma comp_ι_eq : f.comp (ι f g) = g.comp (ι f g) := by { ext, rw [comp_apply, comp_apply, ← sub_eq_zero, ← normed_group_hom.sub_apply], exact x.2 } variables {f g} /-- If `φ : normed_group_hom V₁ V` is such that `f.comp φ = g.comp φ`, the induced morphism `normed_group_hom V₁ (f.equalizer g)`. -/ @[simps] def lift (φ : normed_group_hom V₁ V) (h : f.comp φ = g.comp φ) : normed_group_hom V₁ (f.equalizer g) := { to_fun := λ v, ⟨φ v, show (f - g) (φ v) = 0, by rw [normed_group_hom.sub_apply, sub_eq_zero, ← comp_apply, h, comp_apply]⟩, map_add' := λ v₁ v₂, by { ext, simp only [map_add, add_subgroup.coe_add, subtype.coe_mk] }, bound' := by { obtain ⟨C, C_pos, hC⟩ := φ.bound, exact ⟨C, hC⟩ } } @[simp] lemma ι_comp_lift (φ : normed_group_hom V₁ V) (h : f.comp φ = g.comp φ) : (ι _ _).comp (lift φ h) = φ := by { ext, refl } /-- The lifting property of the equalizer as an equivalence. -/ @[simps] def lift_equiv : {φ : normed_group_hom V₁ V // f.comp φ = g.comp φ} ≃ normed_group_hom V₁ (f.equalizer g) := { to_fun := λ φ, lift φ φ.prop, inv_fun := λ ψ, ⟨(ι f g).comp ψ, by { rw [← comp_assoc, ← comp_assoc, comp_ι_eq] }⟩, left_inv := λ φ, by simp, right_inv := λ ψ, by { ext, refl } } /-- Given `φ : normed_group_hom V₁ V₂` and `ψ : normed_group_hom W₁ W₂` such that `ψ.comp f₁ = f₂.comp φ` and `ψ.comp g₁ = g₂.comp φ`, the induced morphism `normed_group_hom (f₁.equalizer g₁) (f₂.equalizer g₂)`. -/ def map (φ : normed_group_hom V₁ V₂) (ψ : normed_group_hom W₁ W₂) (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) : normed_group_hom (f₁.equalizer g₁) (f₂.equalizer g₂) := lift (φ.comp $ ι _ _) $ by { simp only [← comp_assoc, ← hf, ← hg], simp only [comp_assoc, comp_ι_eq] } variables {φ : normed_group_hom V₁ V₂} {ψ : normed_group_hom W₁ W₂} variables {φ' : normed_group_hom V₂ V₃} {ψ' : normed_group_hom W₂ W₃} @[simp] lemma ι_comp_map (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) : (ι f₂ g₂).comp (map φ ψ hf hg) = φ.comp (ι _ _) := ι_comp_lift _ _ @[simp] lemma map_id : map (id V₁) (id W₁) rfl rfl = id (f₁.equalizer g₁) := by { ext, refl } lemma comm_sq₂ (hf : ψ.comp f₁ = f₂.comp φ) (hf' : ψ'.comp f₂ = f₃.comp φ') : (ψ'.comp ψ).comp f₁ = f₃.comp (φ'.comp φ) := by rw [comp_assoc, hf, ← comp_assoc, hf', comp_assoc] lemma map_comp_map (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) (hf' : ψ'.comp f₂ = f₃.comp φ') (hg' : ψ'.comp g₂ = g₃.comp φ') : (map φ' ψ' hf' hg').comp (map φ ψ hf hg) = map (φ'.comp φ) (ψ'.comp ψ) (comm_sq₂ hf hf') (comm_sq₂ hg hg') := by { ext, refl } lemma ι_norm_noninc : (ι f g).norm_noninc := λ v, le_rfl /-- The lifting of a norm nonincreasing morphism is norm nonincreasing. -/ lemma lift_norm_noninc (φ : normed_group_hom V₁ V) (h : f.comp φ = g.comp φ) (hφ : φ.norm_noninc) : (lift φ h).norm_noninc := hφ /-- If `φ` satisfies `∥φ∥ ≤ C`, then the same is true for the lifted morphism. -/ lemma norm_lift_le (φ : normed_group_hom V₁ V) (h : f.comp φ = g.comp φ) (C : ℝ) (hφ : ∥φ∥ ≤ C) : ∥(lift φ h)∥ ≤ C := hφ lemma map_norm_noninc (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) (hφ : φ.norm_noninc) : (map φ ψ hf hg).norm_noninc := lift_norm_noninc _ _ $ hφ.comp ι_norm_noninc lemma norm_map_le (hf : ψ.comp f₁ = f₂.comp φ) (hg : ψ.comp g₁ = g₂.comp φ) (C : ℝ) (hφ : ∥φ.comp (ι f₁ g₁)∥ ≤ C) : ∥map φ ψ hf hg∥ ≤ C := norm_lift_le _ _ _ hφ end equalizer end normed_group_hom section controlled_closure open filter finset open_locale topological_space variables {G : Type*} [normed_group G] [complete_space G] variables {H : Type*} [normed_group H] /-- Given `f : normed_group_hom G H` for some complete `G` and a subgroup `K` of `H`, if every element `x` of `K` has a preimage under `f` whose norm is at most `C*∥x∥` then the same holds for elements of the (topological) closure of `K` with constant `C+ε` instead of `C`, for any positive `ε`. -/ lemma controlled_closure_of_complete {f : normed_group_hom G H} {K : add_subgroup H} {C ε : ℝ} (hC : 0 < C) (hε : 0 < ε) (hyp : f.surjective_on_with K C) : f.surjective_on_with K.topological_closure (C + ε) := begin rintros (h : H) (h_in : h ∈ K.topological_closure), /- We first get rid of the easy case where `h = 0`.-/ by_cases hyp_h : h = 0, { rw hyp_h, use 0, simp }, /- The desired preimage will be constructed as the sum of a series. Convergence of the series will be guaranteed by completeness of `G`. We first write `h` as the sum of a sequence `v` of elements of `K` which starts close to `h` and then quickly goes to zero. The sequence `b` below quantifies this. -/ set b : ℕ → ℝ := λ i, (1/2)^i*(ε*∥h∥/2)/C, have b_pos : ∀ i, 0 < b i, { intro i, field_simp [b, hC], exact div_pos (mul_pos hε (norm_pos_iff.mpr hyp_h)) (mul_pos (by norm_num : (0 : ℝ) < 2^i*2) hC) }, obtain ⟨v : ℕ → H, lim_v : tendsto (λ (n : ℕ), ∑ k in range (n + 1), v k) at_top (𝓝 h), v_in : ∀ n, v n ∈ K, hv₀ : ∥v 0 - h∥ < b 0, hv : ∀ n > 0, ∥v n∥ < b n⟩ := controlled_sum_of_mem_closure h_in b_pos, /- The controlled surjectivity assumption on `f` allows to build preimages `u n` for all elements `v n` of the `v` sequence.-/ have : ∀ n, ∃ m' : G, f m' = v n ∧ ∥m'∥ ≤ C * ∥v n∥ := λ (n : ℕ), hyp (v n) (v_in n), choose u hu hnorm_u using this, /- The desired series `s` is then obtained by summing `u`. We then check our choice of `b` ensures `s` is Cauchy. -/ set s : ℕ → G := λ n, ∑ k in range (n+1), u k, have : cauchy_seq s, { apply normed_group.cauchy_series_of_le_geometric'' (by norm_num) one_half_lt_one, rintro n (hn : n ≥ 1), calc ∥u n∥ ≤ C*∥v n∥ : hnorm_u n ... ≤ C * b n : mul_le_mul_of_nonneg_left (hv _ $ nat.succ_le_iff.mp hn).le hC.le ... = (1/2)^n * (ε * ∥h∥/2) : by simp [b, mul_div_cancel' _ hC.ne.symm] ... = (ε * ∥h∥/2) * (1/2)^n : mul_comm _ _ }, /- We now show that the limit `g` of `s` is the desired preimage. -/ obtain ⟨g : G, hg⟩ := cauchy_seq_tendsto_of_complete this, refine ⟨g, _, _⟩, { /- We indeed get a preimage. First note: -/ have : f ∘ s = λ n, ∑ k in range (n + 1), v k, { ext n, simp [f.map_sum, hu] }, /- In the above equality, the left-hand-side converges to `f g` by continuity of `f` and definition of `g` while the right-hand-side converges to `h` by construction of `v` so `g` is indeed a preimage of `h`. -/ rw ← this at lim_v, exact tendsto_nhds_unique ((f.continuous.tendsto g).comp hg) lim_v }, { /- Then we need to estimate the norm of `g`, using our careful choice of `b`. -/ suffices : ∀ n, ∥s n∥ ≤ (C + ε) * ∥h∥, from le_of_tendsto' (continuous_norm.continuous_at.tendsto.comp hg) this, intros n, have hnorm₀ : ∥u 0∥ ≤ C*b 0 + C*∥h∥, { have := calc ∥v 0∥ ≤ ∥h∥ + ∥v 0 - h∥ : norm_le_insert' _ _ ... ≤ ∥h∥ + b 0 : by apply add_le_add_left hv₀.le, calc ∥u 0∥ ≤ C*∥v 0∥ : hnorm_u 0 ... ≤ C*(∥h∥ + b 0) : mul_le_mul_of_nonneg_left this hC.le ... = C * b 0 + C * ∥h∥ : by rw [add_comm, mul_add] }, have : ∑ k in range (n + 1), C * b k ≤ ε * ∥h∥ := calc ∑ k in range (n + 1), C * b k = (∑ k in range (n + 1), (1 / 2) ^ k) * (ε * ∥h∥ / 2) : by simp only [b, mul_div_cancel' _ hC.ne.symm, ← sum_mul] ... ≤ 2 * (ε * ∥h∥ / 2) : mul_le_mul_of_nonneg_right (sum_geometric_two_le _) (by nlinarith [hε, norm_nonneg h]) ... = ε * ∥h∥ : mul_div_cancel' _ two_ne_zero, calc ∥s n∥ ≤ ∑ k in range (n+1), ∥u k∥ : norm_sum_le _ _ ... = ∑ k in range n, ∥u (k + 1)∥ + ∥u 0∥ : sum_range_succ' _ _ ... ≤ ∑ k in range n, C*∥v (k + 1)∥ + ∥u 0∥ : add_le_add_right (sum_le_sum (λ _ _, hnorm_u _)) _ ... ≤ ∑ k in range n, C*b (k+1) + (C*b 0 + C*∥h∥) : add_le_add (sum_le_sum (λ k _, mul_le_mul_of_nonneg_left (hv _ k.succ_pos).le hC.le)) hnorm₀ ... = ∑ k in range (n+1), C*b k + C*∥h∥ : by rw [← add_assoc, sum_range_succ'] ... ≤ (C+ε)*∥h∥ : by { rw [add_comm, add_mul], apply add_le_add_left this } } end /-- Given `f : normed_group_hom G H` for some complete `G`, if every element `x` of the image of an isometric immersion `j : normed_group_hom K H` has a preimage under `f` whose norm is at most `C*∥x∥` then the same holds for elements of the (topological) closure of this image with constant `C+ε` instead of `C`, for any positive `ε`. This is useful in particular if `j` is the inclusion of a normed group into its completion (in this case the closure is the full target group). -/ lemma controlled_closure_range_of_complete {f : normed_group_hom G H} {K : Type*} [semi_normed_group K] {j : normed_group_hom K H} (hj : ∀ x, ∥j x∥ = ∥x∥) {C ε : ℝ} (hC : 0 < C) (hε : 0 < ε) (hyp : ∀ k, ∃ g, f g = j k ∧ ∥g∥ ≤ C*∥k∥) : f.surjective_on_with j.range.topological_closure (C + ε) := begin replace hyp : ∀ h ∈ j.range, ∃ g, f g = h ∧ ∥g∥ ≤ C*∥h∥, { intros h h_in, rcases (j.mem_range _).mp h_in with ⟨k, rfl⟩, rw hj, exact hyp k }, exact controlled_closure_of_complete hC hε hyp end end controlled_closure
d8417fe64732de44c9b38007b0b0c65ecdfd25d8
f57570f33b51ef0271f8c366142363d5ae8fff45
/src/aristotelian_logic.lean
1ef5fd3be91b6afd2f5851e0cc51899ecea152fd
[]
no_license
maxd13/lean-logic
4083cb3fbb45b423befca7fda7268b8ba85ff3a6
ddcab46b77adca91b120a5f37afbd48794da8b52
refs/heads/master
1,692,257,681,488
1,631,740,832,000
1,631,740,832,000
246,324,437
0
0
null
null
null
null
UTF-8
Lean
false
false
10,092
lean
import predicate_logic tactic.find open logic universe u namespace syllogistic section syllogism parameters {sort : Type u} [decidable_eq sort] parameter {σ : @signature sort _} -- Aristotelian syllogistic calculus with individuals and conversions (including singular conversion). -- "singular conversion" means we support the conception of treating individual/singular assertions -- as categorical ones. -- We had been told this is a port royal idea, but maybe it is due to Aristotle as well. -- must check. -- individuals -- Notice that "term" for Aristotle is, in the context of a categorical syllogism, -- almost the opposite of what "term" in mathematical logic means. def atomo := @uterm sort _ σ -- Aristotelian terms. -- Those will be monadic predicates in the signature, -- and also the terms generated from equality -- universals (καθόλου) def katholou := @nrary sort _ σ 1 inductive horos | universal : katholou → horos -- In predicate logic this constructor would be equivalent to the language having equality. | singular : atomo → horos instance : has_lift katholou horos := ⟨horos.universal⟩ -- Apophansis (ἀπόφανσις) -- i.e. the type of assertions inductive apophansis -- universal affirmative individual/singular (καθόλου κατάφασις άτομο) -- Socrates is human. | AI : atomo → katholou → apophansis -- universal negative individual/singular -- Socrates is not human. | EI : atomo → katholou → apophansis -- undefined/indefinite affirmative. -- Supposed to stand either for vague quantification, or the predication of a proper accident. -- Rougly, a proper accident is a property that is not part of the definition of the term, -- but is either deducible from the definition or present almost universally in the instance of the universal term. -- humans are mortal. | UA : katholou → katholou → apophansis -- undefined/indefinite negative. -- humans are not mortal. | UE : katholou → katholou → apophansis -- universal affirmative -- All men are mortal. | A : horos → katholou → apophansis -- universal negative -- No man is mortal. | E : horos → katholou → apophansis -- particular affirmative -- Some men are mortal. | I : katholou → katholou → apophansis -- particular negative -- Some men are not mortal. | O : katholou → katholou → apophansis -- Existence claims, used for introducing existential import in Aristotelian logic. -- e.g. "Some human exists". | EXI : katholou → apophansis -- e.g. "No human exists". | EXE : katholou → apophansis -- trivially true -- Used as a default value in some functions -- e.g. "All humans exist". | EXA : katholou → apophansis -- Always false. -- Used as a default value in some functions -- e.g. "Some humans do not exist". | EXO : katholou → apophansis def AI := apophansis.AI def EI := apophansis.EI def UA := apophansis.UA def UE := apophansis.UE def A := apophansis.A def E := apophansis.E def I := apophansis.I def O := apophansis.O def EXA := apophansis.EXA def EXE := apophansis.EXE def EXI := apophansis.EXI def EXO := apophansis.EXO --square of opposition (up to singular conversion). def contradictory : apophansis → apophansis | (apophansis.AI socrates mortal) := EI socrates mortal | (apophansis.EI socrates mortal) := AI socrates mortal | (apophansis.UA human mortal) := UE human mortal | (apophansis.UE human mortal) := UA human mortal | (apophansis.A (horos.universal human) mortal) := O human mortal | (apophansis.A (horos.singular socrates) mortal) := EI socrates mortal | (apophansis.E (horos.universal human) mortal) := I human mortal | (apophansis.E (horos.singular socrates) mortal) := EI socrates mortal | (apophansis.I human mortal) := E ↑human mortal | (apophansis.O human mortal) := A ↑human mortal | (apophansis.EXA human) := EXO human | (apophansis.EXE human) := EXI human | (apophansis.EXI human) := EXE human | (apophansis.EXO human) := EXA human -- EXO can be interpreted as "undefined". -- But it is also logically true that it is contrary to everything. def contrary : apophansis → apophansis | (apophansis.AI socrates mortal) := EI socrates mortal | (apophansis.EI socrates mortal) := AI socrates mortal | (apophansis.UA human mortal) := EXO human | (apophansis.UE human mortal) := EXO human | (apophansis.A human mortal) := E human mortal | (apophansis.E human mortal) := A human mortal | (apophansis.I human mortal) := EXO human | (apophansis.O human mortal) := EXO human | (apophansis.EXA human) := EXO human | (apophansis.EXE human) := EXO human | (apophansis.EXI human) := EXO human | (apophansis.EXO human) := EXO human -- EXA is the "undefined". -- It is logically true that it is compatible with everthing (except EXO). -- def subalternate : apophansis → apophansis -- | (apophansis.AI socrates mortal) := EI socrates mortal -- | (apophansis.EI socrates mortal) := AI socrates mortal -- | (apophansis.UA human mortal) := EXA human -- | (apophansis.UE human mortal) := EXA human -- | (apophansis.A (horos.universal human) mortal) := I human mortal -- | (apophansis.A (horos.singular socrates) mortal) := I human mortal -- | (apophansis.E human mortal) := A human mortal -- | (apophansis.I human mortal) := EXO human -- | (apophansis.O human mortal) := EXO human -- | (apophansis.EXA human) := EXO human -- | (apophansis.EXE human) := EXO human -- | (apophansis.EXI human) := EXO human -- | (apophansis.EXO human) := EXO human -- convert singular assertions to categorical ones. -- e.g. convert from "Socrates is human" to "All Socrates are human" -- (meaning all entities equal to Socrates are human). -- e.g. convert from "Socrates is not human" to "No Socrates is human". def singular_conversion : apophansis → apophansis | (apophansis.AI socrates human) := A (horos.singular socrates) human | (apophansis.EI socrates human) := E (horos.singular socrates) human | φ := φ -- inverse function. -- converts from "All Socrates are human" to "Socrates is human". -- converts from "No Socrates is human" to "Socrates is not human". def singular_reversion : apophansis → apophansis | (apophansis.A (horos.singular socrates) human) := AI socrates human | (apophansis.E (horos.singular socrates) human) := EI socrates human | φ := φ def categorical : apophansis → Prop | (apophansis.AI _ _) := false | (apophansis.EI _ _) := false | _ := true lemma singular_conversion_gives_categorical : ∀ φ : apophansis, categorical (singular_conversion φ) := begin intros φ, cases φ; simp [singular_conversion] end -- Eba→Eab -- if no human is a donkey, no donkey is a human. def disjoint_conversion : apophansis → apophansis | (apophansis.E (horos.universal human) donkey) := E ↑donkey human | φ := φ -- Iba→Iab -- If some humans are idiots, some idiots are human. def conjoint_conversion : apophansis → apophansis | (apophansis.I human idiot) := I idiot human | φ := φ -- Aba → Iab -- If all men are mortal, some mortals are men. -- requires existential import. def existential_conversion : apophansis → apophansis | (apophansis.A (horos.universal human) mortal) := I mortal human | φ := φ -- All moods can be reduced to Barbara and Celarent, -- although Darii and Ferio are also considered perfect. inductive perfect_syllogism : apophansis → apophansis → apophansis → Prop | Barbara (middle major : katholou) (minor : horos) : perfect_syllogism (A ↑middle major) (A minor middle) (A minor major) | Celarent (major middle minor : katholou) : perfect_syllogism (E ↑middle major) (A ↑minor middle) (E ↑minor major) inductive syllogism : apophansis → apophansis → apophansis → Prop -- teleios syllogism | perfect {p₁ p₂ c₁ : apophansis} (h : perfect_syllogism p₁ p₂ c₁) : syllogism p₁ p₂ c₁ -- singular_conversion | SC {p₁ p₂ c₁ : apophansis} (h : syllogism p₁ p₂ c₁) : syllogism (singular_conversion p₁) (singular_conversion p₂) (singular_conversion c₁) -- singular_reversion | SR {p₁ p₂ c₁ : apophansis} (h : syllogism p₁ p₂ c₁) : syllogism (singular_reversion p₁) (singular_reversion p₂) (singular_reversion c₁) -- disjoint_conversion | DC {p₁ p₂ c₁ : apophansis} (h : syllogism p₁ p₂ c₁) : syllogism (disjoint_conversion p₁) (disjoint_conversion p₂) (disjoint_conversion c₁) -- conjoint_conversion | CC {p₁ p₂ c₁ : apophansis} (h : syllogism p₁ p₂ c₁) : syllogism (conjoint_conversion p₁) (conjoint_conversion p₂) (conjoint_conversion c₁) -- existential_conversion | EC {p₁ p₂ c₁ : apophansis} (h : syllogism p₁ p₂ c₁) : syllogism (existential_conversion p₁) (existential_conversion p₂) (existential_conversion c₁) -- parameters {p₁ p₂ c : apophansis} lemma BarbaraI (middle major : katholou) (minor : atomo) : syllogism (singular_conversion (A ↑middle major)) (singular_conversion (A (horos.singular minor) middle)) (singular_conversion (A (horos.singular minor) major)) := syllogism.SC (syllogism.perfect (perfect_syllogism.Barbara middle major (horos.singular minor))) -- | CelarentI (major middle : katholou) (minor : atomo) : syllogism (E ↑middle major) (AI minor middle) (EI minor major) inductive deduction : apophansis → Type u | immediate (p₁ p₂ c : apophansis) (h : syllogism p₁ p₂ c) : deduction c | first_recursive (p₂ c₀ c : apophansis) (proof : deduction c₀) (h : syllogism c₀ p₂ c) : deduction c | second_recursive (p₁ c₀ c : apophansis) (proof : deduction c₀) (h : syllogism p₁ c₀ c) : deduction c | birecursive (p₁ c₁ c₂ c : apophansis) (proof₁ : deduction c₁) (proof₂ : deduction c₂) (h : syllogism c₁ c₂ c) : deduction c -- | second_recursive (p₁ p₂ c₁ p₃ c₂ : apophansis) (h₀ : syllogism p₁ p₂ c₁) -- (h₀ : syllogism p₃ c₁ c₂) -- : deduction [p₁, p₂, c₁, p₃, c₂] end syllogism end syllogistic
169a9e7c4ceaedb801f3e3149e020a3c1cb8bfac
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/invertible.lean
dc54958a6766bc0cf2b9962bd9d7b9a3d6d48ee4
[ "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
9,594
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Anne Baanen A typeclass for the two-sided multiplicative inverse. -/ import algebra.char_zero import algebra.char_p /-! # Invertible elements This file defines a typeclass `invertible a` for elements `a` with a multiplicative inverse. The intent of the typeclass is to provide a way to write e.g. `⅟2` in a ring like `ℤ[1/2]` where some inverses exist but there is no general `⁻¹` operator; or to specify that a field has characteristic `≠ 2`. It is the `Type`-valued analogue to the `Prop`-valued `is_unit`. This file also includes some instances of `invertible` for specific numbers in characteristic zero. Some more cases are given as a `def`, to be included only when needed. To construct instances for concrete numbers, `invertible_of_nonzero` is a useful definition. ## Notation * `⅟a` is `invertible.inv_of a`, the inverse of `a` ## Implementation notes The `invertible` class lives in `Type`, not `Prop`, to make computation easier. If multiplication is associative, `invertible` is a subsingleton anyway. The `simp` normal form tries to normalize `⅟a` to `a ⁻¹`. Otherwise, it pushes `⅟` inside the expression as much as possible. ## Tags invertible, inverse element, inv_of, a half, one half, a third, one third, ½, ⅓ -/ universes u variables {α : Type u} /-- `invertible a` gives a two-sided multiplicative inverse of `a`. -/ class invertible [has_mul α] [has_one α] (a : α) : Type u := (inv_of : α) (inv_of_mul_self : inv_of * a = 1) (mul_inv_of_self : a * inv_of = 1) -- This notation has the same precedence as `has_inv.inv`. notation `⅟`:1034 := invertible.inv_of @[simp] lemma inv_of_mul_self [has_mul α] [has_one α] (a : α) [invertible a] : ⅟a * a = 1 := invertible.inv_of_mul_self @[simp] lemma mul_inv_of_self [has_mul α] [has_one α] (a : α) [invertible a] : a * ⅟a = 1 := invertible.mul_inv_of_self @[simp] lemma inv_of_mul_self_assoc [monoid α] (a b : α) [invertible a] : ⅟a * (a * b) = b := by rw [←mul_assoc, inv_of_mul_self, one_mul] @[simp] lemma mul_inv_of_self_assoc [monoid α] (a b : α) [invertible a] : a * (⅟a * b) = b := by rw [←mul_assoc, mul_inv_of_self, one_mul] @[simp] lemma mul_inv_of_mul_self_cancel [monoid α] (a b : α) [invertible b] : a * ⅟b * b = a := by simp [mul_assoc] @[simp] lemma mul_mul_inv_of_self_cancel [monoid α] (a b : α) [invertible b] : a * b * ⅟b = a := by simp [mul_assoc] lemma inv_of_eq_right_inv [monoid α] {a b : α} [invertible a] (hac : a * b = 1) : ⅟a = b := left_inv_eq_right_inv (inv_of_mul_self _) hac lemma invertible_unique {α : Type u} [monoid α] (a b : α) (h : a = b) [invertible a] [invertible b] : ⅟a = ⅟b := by { apply inv_of_eq_right_inv, rw [h, mul_inv_of_self], } instance [monoid α] (a : α) : subsingleton (invertible a) := ⟨ λ ⟨b, hba, hab⟩ ⟨c, hca, hac⟩, by { congr, exact left_inv_eq_right_inv hba hac } ⟩ /-- An `invertible` element is a unit. -/ def unit_of_invertible [monoid α] (a : α) [invertible a] : units α := { val := a, inv := ⅟a, val_inv := by simp, inv_val := by simp, } @[simp] lemma unit_of_invertible_val [monoid α] (a : α) [invertible a] : (unit_of_invertible a : α) = a := rfl @[simp] lemma unit_of_invertible_inv [monoid α] (a : α) [invertible a] : (↑(unit_of_invertible a)⁻¹ : α) = ⅟a := rfl lemma is_unit_of_invertible [monoid α] (a : α) [invertible a] : is_unit a := ⟨unit_of_invertible a, rfl⟩ /-- Each element of a group is invertible. -/ def invertible_of_group [group α] (a : α) : invertible a := ⟨a⁻¹, inv_mul_self a, mul_inv_self a⟩ @[simp] lemma inv_of_eq_group_inv [group α] (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_self a) /-- `1` is the inverse of itself -/ def invertible_one [monoid α] : invertible (1 : α) := ⟨ 1, mul_one _, one_mul _ ⟩ @[simp] lemma inv_of_one [monoid α] [invertible (1 : α)] : ⅟(1 : α) = 1 := inv_of_eq_right_inv (mul_one _) /-- `-⅟a` is the inverse of `-a` -/ def invertible_neg [ring α] (a : α) [invertible a] : invertible (-a) := ⟨ -⅟a, by simp, by simp ⟩ @[simp] lemma inv_of_neg [ring α] (a : α) [invertible a] [invertible (-a)] : ⅟(-a) = -⅟a := inv_of_eq_right_inv (by simp) @[simp] lemma one_sub_inv_of_two [ring α] [invertible (2:α)] : 1 - (⅟2:α) = ⅟2 := (is_unit_of_invertible (2:α)).mul_right_inj.1 $ by rw [mul_sub, mul_inv_of_self, mul_one, bit0, add_sub_cancel] /-- `a` is the inverse of `⅟a`. -/ instance invertible_inv_of [has_one α] [has_mul α] {a : α} [invertible a] : invertible (⅟a) := ⟨ a, mul_inv_of_self a, inv_of_mul_self a ⟩ @[simp] lemma inv_of_inv_of [monoid α] {a : α} [invertible a] [invertible (⅟a)] : ⅟(⅟a) = a := inv_of_eq_right_inv (inv_of_mul_self _) /-- `⅟b * ⅟a` is the inverse of `a * b` -/ def invertible_mul [monoid α] (a b : α) [invertible a] [invertible b] : invertible (a * b) := ⟨ ⅟b * ⅟a, by simp [←mul_assoc], by simp [←mul_assoc] ⟩ @[simp] lemma inv_of_mul [monoid α] (a b : α) [invertible a] [invertible b] [invertible (a * b)] : ⅟(a * b) = ⅟b * ⅟a := inv_of_eq_right_inv (by simp [←mul_assoc]) /-- If `r` is invertible and `s = r`, then `s` is invertible. -/ def invertible.copy [monoid α] {r : α} (hr : invertible r) (s : α) (hs : s = r) : invertible s := { inv_of := ⅟r, inv_of_mul_self := by rw [hs, inv_of_mul_self], mul_inv_of_self := by rw [hs, mul_inv_of_self] } lemma commute_inv_of {M : Type*} [has_one M] [has_mul M] (m : M) [invertible m] : commute m (⅟m) := calc m * ⅟m = 1 : mul_inv_of_self m ... = ⅟ m * m : (inv_of_mul_self m).symm instance invertible_pow {M : Type*} [monoid M] (m : M) [invertible m] (n : ℕ) : invertible (m ^ n) := { inv_of := ⅟ m ^ n, inv_of_mul_self := by rw [← (commute_inv_of m).symm.mul_pow, inv_of_mul_self, one_pow], mul_inv_of_self := by rw [← (commute_inv_of m).mul_pow, mul_inv_of_self, one_pow] } section group_with_zero variable [group_with_zero α] lemma nonzero_of_invertible (a : α) [invertible a] : a ≠ 0 := λ ha, zero_ne_one $ calc 0 = ⅟a * a : by simp [ha] ... = 1 : inv_of_mul_self a /-- `a⁻¹` is an inverse of `a` if `a ≠ 0` -/ def invertible_of_nonzero {a : α} (h : a ≠ 0) : invertible a := ⟨ a⁻¹, inv_mul_cancel h, mul_inv_cancel h ⟩ @[simp] lemma inv_of_eq_inv (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_cancel (nonzero_of_invertible a)) @[simp] lemma inv_mul_cancel_of_invertible (a : α) [invertible a] : a⁻¹ * a = 1 := inv_mul_cancel (nonzero_of_invertible a) @[simp] lemma mul_inv_cancel_of_invertible (a : α) [invertible a] : a * a⁻¹ = 1 := mul_inv_cancel (nonzero_of_invertible a) @[simp] lemma div_mul_cancel_of_invertible (a b : α) [invertible b] : a / b * b = a := div_mul_cancel a (nonzero_of_invertible b) @[simp] lemma mul_div_cancel_of_invertible (a b : α) [invertible b] : a * b / b = a := mul_div_cancel a (nonzero_of_invertible b) @[simp] lemma div_self_of_invertible (a : α) [invertible a] : a / a = 1 := div_self (nonzero_of_invertible a) /-- `b / a` is the inverse of `a / b` -/ def invertible_div (a b : α) [invertible a] [invertible b] : invertible (a / b) := ⟨b / a, by simp [←mul_div_assoc], by simp [←mul_div_assoc]⟩ @[simp] lemma inv_of_div (a b : α) [invertible a] [invertible b] [invertible (a / b)] : ⅟(a / b) = b / a := inv_of_eq_right_inv (by simp [←mul_div_assoc]) /-- `a` is the inverse of `a⁻¹` -/ def invertible_inv {a : α} [invertible a] : invertible (a⁻¹) := ⟨ a, by simp, by simp ⟩ end group_with_zero /-- Monoid homs preserve invertibility. -/ def invertible.map {R : Type*} {S : Type*} [monoid R] [monoid S] (f : R →* S) (r : R) [invertible r] : invertible (f r) := { inv_of := f (⅟r), inv_of_mul_self := by rw [← f.map_mul, inv_of_mul_self, f.map_one], mul_inv_of_self := by rw [← f.map_mul, mul_inv_of_self, f.map_one] } section ring_char /-- A natural number `t` is invertible in a field `K` if the charactistic of `K` does not divide `t`. -/ def invertible_of_ring_char_not_dvd {K : Type*} [field K] {t : ℕ} (not_dvd : ¬(ring_char K ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((ring_char.spec K t).mp h)) end ring_char section char_p /-- A natural number `t` is invertible in a field `K` of charactistic `p` if `p` does not divide `t`. -/ def invertible_of_char_p_not_dvd {K : Type*} [field K] {p : ℕ} [char_p K p] {t : ℕ} (not_dvd : ¬(p ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((char_p.cast_eq_zero_iff K p t).mp h)) instance invertible_of_pos {K : Type*} [field K] [char_zero K] (n : ℕ) [h : fact (0 < n)] : invertible (n : K) := invertible_of_nonzero $ by simpa [nat.pos_iff_ne_zero] using h end char_p section division_ring variable [division_ring α] instance invertible_succ [char_zero α] (n : ℕ) : invertible (n.succ : α) := invertible_of_nonzero (nat.cast_ne_zero.mpr (nat.succ_ne_zero _)) /-! A few `invertible n` instances for small numerals `n`. Feel free to add your own number when you need its inverse. -/ instance invertible_two [char_zero α] : invertible (2 : α) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 2 ≠ 0)) instance invertible_three [char_zero α] : invertible (3 : α) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 3 ≠ 0)) end division_ring
52a14181f30587313bc82d883ade84590d2f9326
d406927ab5617694ec9ea7001f101b7c9e3d9702
/archive/100-theorems-list/82_cubing_a_cube.lean
330a23188f703087e43aa8e686e1072d0f9d8381
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
22,932
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import data.real.basic import data.set.finite import data.set.intervals /-! Proof that a cube (in dimension n ≥ 3) cannot be cubed: There does not exist a partition of a cube into finitely many smaller cubes (at least two) of different sizes. We follow the proof described here: http://www.alaricstephen.com/main-featured/2017/9/28/cubing-a-cube-proof -/ open real set function fin noncomputable theory variable {n : ℕ} /-- Given three intervals `I, J, K` such that `J ⊂ I`, neither endpoint of `J` coincides with an endpoint of `I`, `¬ (K ⊆ J)` and `K` does not lie completely to the left nor completely to the right of `J`. Then `I ∩ K \ J` is nonempty. -/ lemma Ico_lemma {α} [linear_order α] {x₁ x₂ y₁ y₂ z₁ z₂ w : α} (h₁ : x₁ < y₁) (hy : y₁ < y₂) (h₂ : y₂ < x₂) (hz₁ : z₁ ≤ y₂) (hz₂ : y₁ ≤ z₂) (hw : w ∉ Ico y₁ y₂ ∧ w ∈ Ico z₁ z₂) : ∃w, w ∈ Ico x₁ x₂ ∧ w ∉ Ico y₁ y₂ ∧ w ∈ Ico z₁ z₂ := begin simp only [not_and, not_lt, mem_Ico] at hw, refine ⟨max x₁ (min w y₂), _, _, _⟩, { simp [le_refl, lt_trans h₁ (lt_trans hy h₂), h₂] }, { simp [hw, lt_irrefl, not_le_of_lt h₁] {contextual := tt} }, { simp [hw.2.1, hw.2.2, hz₁, lt_of_lt_of_le h₁ hz₂] at ⊢ } end /-- A (hyper)-cube (in standard orientation) is a vector `b` consisting of the bottom-left point of the cube, a width `w` and a proof that `w > 0`. We use functions from `fin n` to denote vectors. -/ structure cube (n : ℕ) : Type := (b : fin n → ℝ) -- bottom-left coordinate (w : ℝ) -- width (hw : 0 < w) namespace cube lemma hw' (c : cube n) : 0 ≤ c.w := le_of_lt c.hw /-- The j-th side of a cube is the half-open interval `[b j, b j + w)` -/ def side (c : cube n) (j : fin n) : set ℝ := Ico (c.b j) (c.b j + c.w) @[simp] lemma b_mem_side (c : cube n) (j : fin n) : c.b j ∈ c.side j := by simp [side, cube.hw, le_refl] def to_set (c : cube n) : set (fin n → ℝ) := { x | ∀j, x j ∈ side c j } lemma side_nonempty (c : cube n) (i : fin n) : (side c i).nonempty := by simp [side, c.hw] lemma univ_pi_side (c : cube n) : pi univ (side c) = c.to_set := ext $ λ x, mem_univ_pi lemma to_set_subset {c c' : cube n} : c.to_set ⊆ c'.to_set ↔ ∀j, c.side j ⊆ c'.side j := by simp only [← univ_pi_side, univ_pi_subset_univ_pi_iff, (c.side_nonempty _).ne_empty, exists_false, or_false] lemma to_set_disjoint {c c' : cube n} : disjoint c.to_set c'.to_set ↔ ∃ j, disjoint (c.side j) (c'.side j) := by simp only [← univ_pi_side, disjoint_univ_pi] lemma b_mem_to_set (c : cube n) : c.b ∈ c.to_set := by simp [to_set] protected def tail (c : cube (n+1)) : cube n := ⟨tail c.b, c.w, c.hw⟩ lemma side_tail (c : cube (n+1)) (j : fin n) : c.tail.side j = c.side j.succ := rfl def bottom (c : cube (n+1)) : set (fin (n+1) → ℝ) := { x | x 0 = c.b 0 ∧ tail x ∈ c.tail.to_set } lemma b_mem_bottom (c : cube (n+1)) : c.b ∈ c.bottom := by simp [bottom, to_set, side, cube.hw, le_refl, cube.tail] def xm (c : cube (n+1)) : ℝ := c.b 0 + c.w lemma b_lt_xm (c : cube (n+1)) : c.b 0 < c.xm := by simp [xm, hw] lemma b_ne_xm (c : cube (n+1)) : c.b 0 ≠ c.xm := ne_of_lt c.b_lt_xm def shift_up (c : cube (n+1)) : cube (n+1) := ⟨cons c.xm $ tail c.b, c.w, c.hw⟩ @[simp] lemma tail_shift_up (c : cube (n+1)) : c.shift_up.tail = c.tail := by simp [shift_up, cube.tail] @[simp] lemma head_shift_up (c : cube (n+1)) : c.shift_up.b 0 = c.xm := rfl def unit_cube : cube n := ⟨λ _, 0, 1, by norm_num⟩ @[simp] lemma side_unit_cube {j : fin n} : unit_cube.side j = Ico 0 1 := by norm_num [unit_cube, side] end cube open cube variables {ι : Type} {cs : ι → cube (n+1)} {i i' : ι} /-- A finite family of (at least 2) cubes partitioning the unit cube with different sizes -/ @[protect_proj] structure correct (cs : ι → cube n) : Prop := (pairwise_disjoint : pairwise (disjoint on (cube.to_set ∘ cs))) (Union_eq : (⋃(i : ι), (cs i).to_set) = unit_cube.to_set) (injective : injective (cube.w ∘ cs)) (three_le : 3 ≤ n) namespace correct variable (h : correct cs) include h lemma to_set_subset_unit_cube {i} : (cs i).to_set ⊆ unit_cube.to_set := h.Union_eq ▸ subset_Union _ i lemma side_subset {i j} : (cs i).side j ⊆ Ico 0 1 := by simpa only [side_unit_cube] using to_set_subset.1 h.to_set_subset_unit_cube j lemma zero_le_of_mem_side {i j x} (hx : x ∈ (cs i).side j) : 0 ≤ x := (side_subset h hx).1 lemma zero_le_of_mem {i p} (hp : p ∈ (cs i).to_set) (j) : 0 ≤ p j := zero_le_of_mem_side h (hp j) lemma zero_le_b {i j} : 0 ≤ (cs i).b j := zero_le_of_mem h (cs i).b_mem_to_set j lemma b_add_w_le_one {j} : (cs i).b j + (cs i).w ≤ 1 := by { have := side_subset h, rw [side, Ico_subset_Ico_iff] at this, convert this.2, simp [hw] } lemma nontrivial_fin : nontrivial (fin n) := fin.nontrivial_iff_two_le.2 (nat.le_of_succ_le_succ h.three_le) /-- The width of any cube in the partition cannot be 1. -/ lemma w_ne_one [nontrivial ι] (i : ι) : (cs i).w ≠ 1 := begin intro hi, cases exists_ne i with i' hi', let p := (cs i').b, have hp : p ∈ (cs i').to_set := (cs i').b_mem_to_set, have h2p : p ∈ (cs i).to_set, { intro j, split, transitivity (0 : ℝ), { rw [←add_le_add_iff_right (1 : ℝ)], convert b_add_w_le_one h, rw hi, rw zero_add }, apply zero_le_b h, apply lt_of_lt_of_le (side_subset h $ (cs i').b_mem_side j).2, simp [hi, zero_le_b h] }, exact (h.pairwise_disjoint hi').le_bot ⟨hp, h2p⟩ end /-- The top of a cube (which is the bottom of the cube shifted up by its width) must be covered by bottoms of (other) cubes in the family. -/ lemma shift_up_bottom_subset_bottoms (hc : (cs i).xm ≠ 1) : (cs i).shift_up.bottom ⊆ ⋃(i : ι), (cs i).bottom := begin intros p hp, cases hp with hp0 hps, rw [tail_shift_up] at hps, have : p ∈ (unit_cube : cube (n+1)).to_set, { simp only [to_set, forall_fin_succ, hp0, side_unit_cube, mem_set_of_eq, mem_Ico, head_shift_up], refine ⟨⟨_, _⟩, _⟩, { rw [←zero_add (0 : ℝ)], apply add_le_add, apply zero_le_b h, apply (cs i).hw' }, { exact lt_of_le_of_ne (b_add_w_le_one h) hc }, intro j, exact side_subset h (hps j) }, rw [← h.2, mem_Union] at this, rcases this with ⟨i', hi'⟩, rw [mem_Union], use i', refine ⟨_, λ j, hi' j.succ⟩, have : i ≠ i', { rintro rfl, apply not_le_of_lt (hi' 0).2, rw [hp0], refl }, have := h.1 this, rw [on_fun, to_set_disjoint, exists_fin_succ] at this, rcases this with h0|⟨j, hj⟩, rw [hp0], symmetry, apply eq_of_Ico_disjoint h0 (by simp [hw]) _, convert hi' 0, rw [hp0], refl, exfalso, apply not_disjoint_iff.mpr ⟨tail p j, hps j, hi' j.succ⟩ hj end end correct /-- A valley is a square on which cubes in the family of cubes are placed, so that the cubes completely cover the valley and none of those cubes is partially outside the square. We also require that no cube on it has the same size as the valley (so that there are at least two cubes on the valley). This is the main concept in the formalization. We prove that the smallest cube on a valley has another valley on the top of it, which gives an infinite sequence of cubes in the partition, which contradicts the finiteness. A valley is characterized by a cube `c` (which is not a cube in the family cs) by considering the bottom face of `c`. -/ def valley (cs : ι → cube (n+1)) (c : cube (n+1)) : Prop := c.bottom ⊆ (⋃(i : ι), (cs i).bottom) ∧ (∀i, (cs i).b 0 = c.b 0 → (∃x, x ∈ (cs i).tail.to_set ∩ c.tail.to_set) → (cs i).tail.to_set ⊆ c.tail.to_set) ∧ ∀(i : ι), (cs i).b 0 = c.b 0 → (cs i).w ≠ c.w variables {c : cube (n+1)} (h : correct cs) (v : valley cs c) /-- The bottom of the unit cube is a valley -/ lemma valley_unit_cube [nontrivial ι] (h : correct cs) : valley cs unit_cube := begin refine ⟨_, _, _⟩, { intro v, simp only [bottom, and_imp, mem_Union, mem_set_of_eq], intros h0 hv, have : v ∈ (unit_cube : cube (n+1)).to_set, { dsimp only [to_set, unit_cube, mem_set_of_eq], rw [forall_fin_succ, h0], split, norm_num [side, unit_cube], exact hv }, rw [← h.2, mem_Union] at this, rcases this with ⟨i, hi⟩, use i, split, { apply le_antisymm, rw h0, exact h.zero_le_b, exact (hi 0).1 }, intro j, exact hi _ }, { intros i hi h', rw to_set_subset, intro j, convert h.side_subset using 1, simp [side_tail] }, { intros i hi, exact h.w_ne_one i } end /-- the cubes which lie in the valley `c` -/ def bcubes (cs : ι → cube (n+1)) (c : cube (n+1)) : set ι := { i : ι | (cs i).b 0 = c.b 0 ∧ (cs i).tail.to_set ⊆ c.tail.to_set } /-- A cube which lies on the boundary of a valley in dimension `j` -/ def on_boundary (hi : i ∈ bcubes cs c) (j : fin n) : Prop := c.b j.succ = (cs i).b j.succ ∨ (cs i).b j.succ + (cs i).w = c.b j.succ + c.w lemma tail_sub (hi : i ∈ bcubes cs c) : ∀j, (cs i).tail.side j ⊆ c.tail.side j := by { rw [←to_set_subset], exact hi.2 } lemma bottom_mem_side (hi : i ∈ bcubes cs c) : c.b 0 ∈ (cs i).side 0 := by { convert b_mem_side (cs i) _ using 1, rw hi.1 } lemma b_le_b (hi : i ∈ bcubes cs c) (j : fin n) : c.b j.succ ≤ (cs i).b j.succ := (tail_sub hi j $ b_mem_side _ _).1 lemma t_le_t (hi : i ∈ bcubes cs c) (j : fin n) : (cs i).b j.succ + (cs i).w ≤ c.b j.succ + c.w := begin have h' := tail_sub hi j, dsimp only [side] at h', rw [Ico_subset_Ico_iff] at h', exact h'.2, simp [hw] end include h v /-- Every cube in the valley must be smaller than it -/ lemma w_lt_w (hi : i ∈ bcubes cs c) : (cs i).w < c.w := begin apply lt_of_le_of_ne _ (v.2.2 i hi.1), have j : fin n := ⟨1, nat.le_of_succ_le_succ h.three_le⟩, rw [←add_le_add_iff_left ((cs i).b j.succ)], apply le_trans (t_le_t hi j), rw [add_le_add_iff_right], apply b_le_b hi, end /-- There are at least two cubes in a valley -/ lemma nontrivial_bcubes : (bcubes cs c).nontrivial := begin rcases v.1 c.b_mem_bottom with ⟨_, ⟨i, rfl⟩, hi⟩, have h2i : i ∈ bcubes cs c := ⟨hi.1.symm, v.2.1 i hi.1.symm ⟨tail c.b, hi.2, λ j, c.b_mem_side j.succ⟩⟩, let j : fin (n+1) := ⟨2, h.three_le⟩, have hj : 0 ≠ j := by { simp only [fin.ext_iff, ne.def], contradiction }, let p : fin (n+1) → ℝ := λ j', if j' = j then c.b j + (cs i).w else c.b j', have hp : p ∈ c.bottom, { split, { simp only [bottom, p, if_neg hj] }, intro j', simp only [tail, side_tail], by_cases hj' : j'.succ = j, { simp [p, -add_comm, if_pos, side, hj', hw', w_lt_w h v h2i] }, { simp [p, -add_comm, if_neg hj'] }}, rcases v.1 hp with ⟨_, ⟨i', rfl⟩, hi'⟩, have h2i' : i' ∈ bcubes cs c := ⟨hi'.1.symm, v.2.1 i' hi'.1.symm ⟨tail p, hi'.2, hp.2⟩⟩, refine ⟨i, h2i, i', h2i', _⟩, rintro rfl, apply not_le_of_lt (hi'.2 ⟨1, nat.le_of_succ_le_succ h.three_le⟩).2, simp only [tail, cube.tail, p], rw [if_pos, add_le_add_iff_right], { exact (hi.2 _).1 }, refl end /-- There is a cube in the valley -/ lemma nonempty_bcubes : (bcubes cs c).nonempty := (nontrivial_bcubes h v).nonempty variables [finite ι] /-- There is a smallest cube in the valley -/ lemma exists_mi : ∃ i ∈ bcubes cs c, ∀ i' ∈ bcubes cs c, (cs i).w ≤ (cs i').w := (bcubes cs c).exists_min_image (λ i, (cs i).w) (set.to_finite _) (nonempty_bcubes h v) /-- We let `mi` be the (index for the) smallest cube in the valley `c` -/ def mi : ι := classical.some $ exists_mi h v variables {h v} lemma mi_mem_bcubes : mi h v ∈ bcubes cs c := (classical.some_spec $ exists_mi h v).fst lemma mi_minimal (hi : i ∈ bcubes cs c) : (cs $ mi h v).w ≤ (cs i).w := (classical.some_spec $ exists_mi h v).snd i hi lemma mi_strict_minimal (hii' : mi h v ≠ i) (hi : i ∈ bcubes cs c) : (cs $ mi h v).w < (cs i).w := (mi_minimal hi).lt_of_ne $ h.injective.ne hii' /-- The top of `mi` cannot be 1, since there is a larger cube in the valley -/ lemma mi_xm_ne_one : (cs $ mi h v).xm ≠ 1 := begin apply ne_of_lt, rcases (nontrivial_bcubes h v).exists_ne (mi h v) with ⟨i, hi, h2i⟩, apply lt_of_lt_of_le _ h.b_add_w_le_one, exact i, exact 0, rw [xm, mi_mem_bcubes.1, hi.1, _root_.add_lt_add_iff_left], exact mi_strict_minimal h2i.symm hi end /-- If `mi` lies on the boundary of the valley in dimension j, then this lemma expresses that all other cubes on the same boundary extend further from the boundary. More precisely, there is a j-th coordinate `x : ℝ` in the valley, but not in `mi`, such that every cube that shares a (particular) j-th coordinate with `mi` also contains j-th coordinate `x` -/ lemma smallest_on_boundary {j} (bi : on_boundary (mi_mem_bcubes : mi h v ∈ _) j) : ∃(x : ℝ), x ∈ c.side j.succ \ (cs $ mi h v).side j.succ ∧ ∀ ⦃i'⦄ (hi' : i' ∈ bcubes cs c), i' ≠ mi h v → (cs $ mi h v).b j.succ ∈ (cs i').side j.succ → x ∈ (cs i').side j.succ := begin let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes, cases bi, { refine ⟨(cs i).b j.succ + (cs i).w, ⟨_, _⟩, _⟩, { simp [side, bi, hw', w_lt_w h v hi] }, { intro h', simpa [i, lt_irrefl] using h'.2 }, intros i' hi' i'_i h2i', split, apply le_trans h2i'.1, { simp [hw'] }, apply lt_of_lt_of_le (add_lt_add_left (mi_strict_minimal i'_i.symm hi') _), simp [bi.symm, b_le_b hi'] }, let s := bcubes cs c \ { i }, have hs : s.nonempty, { rcases (nontrivial_bcubes h v).exists_ne i with ⟨i', hi', h2i'⟩, exact ⟨i', hi', h2i'⟩ }, rcases set.exists_min_image s (w ∘ cs) (set.to_finite _) hs with ⟨i', ⟨hi', h2i'⟩, h3i'⟩, rw [mem_singleton_iff] at h2i', let x := c.b j.succ + c.w - (cs i').w, have hx : x < (cs i).b j.succ, { dsimp only [x], rw [←bi, add_sub_assoc, add_lt_iff_neg_left, sub_lt_zero], apply mi_strict_minimal (ne.symm h2i') hi' }, refine ⟨x, ⟨_, _⟩, _⟩, { simp only [side, x, -add_comm, -add_assoc, neg_lt_zero, hw, add_lt_iff_neg_left, and_true, mem_Ico, sub_eq_add_neg], rw [add_assoc, le_add_iff_nonneg_right, ←sub_eq_add_neg, sub_nonneg], apply le_of_lt (w_lt_w h v hi') }, { simp only [side, not_and_distrib, not_lt, add_comm, not_le, mem_Ico], left, exact hx }, intros i'' hi'' h2i'' h3i'', split, swap, apply lt_trans hx h3i''.2, simp only [x], rw [le_sub_iff_add_le], refine le_trans _ (t_le_t hi'' j), rw [add_le_add_iff_left], apply h3i' i'' ⟨hi'', _⟩, simp [mem_singleton, h2i''] end variables (h v) /-- `mi` cannot lie on the boundary of the valley. Otherwise, the cube adjacent to it in the `j`-th direction will intersect one of the neighbouring cubes on the same boundary as `mi`. -/ lemma mi_not_on_boundary (j : fin n) : ¬on_boundary (mi_mem_bcubes : mi h v ∈ _) j := begin let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes, haveI := h.nontrivial_fin, rcases exists_ne j with ⟨j', hj'⟩, swap, intro hj, rcases smallest_on_boundary hj with ⟨x, ⟨hx, h2x⟩, h3x⟩, let p : fin (n+1) → ℝ := cons (c.b 0) (λ j₂, if j₂ = j then x else (cs i).b j₂.succ), have hp : p ∈ c.bottom, { suffices : ∀ (j' : fin n), ite (j' = j) x ((cs i).b j'.succ) ∈ c.side j'.succ, { simpa [bottom, p, to_set, tail, side_tail] }, intro j₂, by_cases hj₂ : j₂ = j, { simp [hj₂, hx] }, simp only [hj₂, if_false], apply tail_sub hi, apply b_mem_side }, rcases v.1 hp with ⟨_, ⟨i', rfl⟩, hi'⟩, have h2i' : i' ∈ bcubes cs c := ⟨hi'.1.symm, v.2.1 i' hi'.1.symm ⟨tail p, hi'.2, hp.2⟩⟩, have i_i' : i ≠ i', { rintro rfl, simpa [p, side_tail, i, h2x] using hi'.2 j }, have : nonempty ↥((cs i').tail.side j' \ (cs i).tail.side j'), { apply nonempty_Ico_sdiff, apply mi_strict_minimal i_i' h2i', apply hw }, rcases this with ⟨⟨x', hx'⟩⟩, let p' : fin (n+1) → ℝ := cons (c.b 0) (λ j₂, if j₂ = j' then x' else (cs i).b j₂.succ), have hp' : p' ∈ c.bottom, { suffices : ∀ (j : fin n), ite (j = j') x' ((cs i).b j.succ) ∈ c.side j.succ, { simpa [bottom, p', to_set, tail, side_tail] }, intro j₂, by_cases hj₂ : j₂ = j', simp [hj₂], apply tail_sub h2i', apply hx'.1, simp only [if_congr, if_false, hj₂], apply tail_sub hi, apply b_mem_side }, rcases v.1 hp' with ⟨_, ⟨i'', rfl⟩, hi''⟩, have h2i'' : i'' ∈ bcubes cs c := ⟨hi''.1.symm, v.2.1 i'' hi''.1.symm ⟨tail p', hi''.2, hp'.2⟩⟩, have i'_i'' : i' ≠ i'', { rintro ⟨⟩, have : (cs i).b ∈ (cs i').to_set, { simp only [to_set, forall_fin_succ, hi.1, bottom_mem_side h2i', true_and, mem_set_of_eq], intro j₂, by_cases hj₂ : j₂ = j, { simpa [side_tail, p', hj'.symm, hj₂] using hi''.2 j }, { simpa [hj₂] using hi'.2 j₂ } }, apply not_disjoint_iff.mpr ⟨(cs i).b, (cs i).b_mem_to_set, this⟩ (h.1 i_i') }, have i_i'' : i ≠ i'', { intro h, induction h, simpa [hx'.2] using hi''.2 j' }, apply not.elim _ (h.1 i'_i''), simp only [on_fun, to_set_disjoint, not_disjoint_iff, forall_fin_succ, not_exists, comp_app], refine ⟨⟨c.b 0, bottom_mem_side h2i', bottom_mem_side h2i''⟩, _⟩, intro j₂, by_cases hj₂ : j₂ = j, { cases hj₂, refine ⟨x, _, _⟩, { convert hi'.2 j, simp [p] }, apply h3x h2i'' i_i''.symm, convert hi''.2 j, simp [p', hj'.symm] }, by_cases h2j₂ : j₂ = j', { cases h2j₂, refine ⟨x', hx'.1, _⟩, convert hi''.2 j', simp }, refine ⟨(cs i).b j₂.succ, _, _⟩, { convert hi'.2 j₂, simp [hj₂] }, { convert hi''.2 j₂, simp [h2j₂] } end variables {h v} /-- The same result that `mi` cannot lie on the boundary of the valley written as inequalities. -/ lemma mi_not_on_boundary' (j : fin n) : c.tail.b j < (cs (mi h v)).tail.b j ∧ (cs (mi h v)).tail.b j + (cs (mi h v)).w < c.tail.b j + c.w := begin have := mi_not_on_boundary h v j, simp only [on_boundary, not_or_distrib] at this, cases this with h1 h2, split, apply lt_of_le_of_ne (b_le_b mi_mem_bcubes _) h1, apply lt_of_le_of_ne _ h2, apply ((Ico_subset_Ico_iff _).mp (tail_sub mi_mem_bcubes j)).2, simp [hw] end /-- The top of `mi` gives rise to a new valley, since the neighbouring cubes extend further upward than `mi`. -/ lemma valley_mi : valley cs ((cs (mi h v)).shift_up) := begin let i := mi h v, have hi : i ∈ bcubes cs c := mi_mem_bcubes, refine ⟨_, _, _⟩, { intro p, apply h.shift_up_bottom_subset_bottoms mi_xm_ne_one }, { rintros i' hi' ⟨p2, hp2, h2p2⟩, simp only [head_shift_up] at hi', classical, by_contra h2i', rw [tail_shift_up] at h2p2, simp only [not_subset, tail_shift_up] at h2i', rcases h2i' with ⟨p1, hp1, h2p1⟩, have : ∃p3, p3 ∈ (cs i').tail.to_set ∧ p3 ∉ (cs i).tail.to_set ∧ p3 ∈ c.tail.to_set, { simp only [to_set, not_forall, mem_set_of_eq] at h2p1, cases h2p1 with j hj, rcases Ico_lemma (mi_not_on_boundary' j).1 (by simp [hw]) (mi_not_on_boundary' j).2 (le_trans (hp2 j).1 $ le_of_lt (h2p2 j).2) (le_trans (h2p2 j).1 $ le_of_lt (hp2 j).2) ⟨hj, hp1 j⟩ with ⟨w, hw, h2w, h3w⟩, refine ⟨λ j', if j' = j then w else p2 j', _, _, _⟩, { intro j', by_cases h : j' = j, { simp only [if_pos h], convert h3w }, { simp only [if_neg h], exact hp2 j' } }, { simp only [to_set, not_forall, mem_set_of_eq], use j, rw [if_pos rfl], convert h2w }, { intro j', by_cases h : j' = j, { simp only [if_pos h, side_tail], convert hw }, { simp only [if_neg h], apply hi.2, apply h2p2 } } }, rcases this with ⟨p3, h1p3, h2p3, h3p3⟩, let p := @cons n (λ_, ℝ) (c.b 0) p3, have hp : p ∈ c.bottom, { refine ⟨rfl, _⟩, rwa [tail_cons] }, rcases v.1 hp with ⟨_, ⟨i'', rfl⟩, hi''⟩, have h2i'' : i'' ∈ bcubes cs c, { use hi''.1.symm, apply v.2.1 i'' hi''.1.symm, use tail p, split, exact hi''.2, rw [tail_cons], exact h3p3 }, have h3i'' : (cs i).w < (cs i'').w, { apply mi_strict_minimal _ h2i'', rintro rfl, apply h2p3, convert hi''.2, rw [tail_cons] }, let p' := @cons n (λ_, ℝ) (cs i).xm p3, have hp' : p' ∈ (cs i').to_set, { simpa [to_set, forall_fin_succ, p', hi'.symm] using h1p3 }, have h2p' : p' ∈ (cs i'').to_set, { simp only [to_set, forall_fin_succ, p', cons_succ, cons_zero, mem_set_of_eq], refine ⟨_, by simpa [to_set, p] using hi''.2⟩, have : (cs i).b 0 = (cs i'').b 0, { rw [hi.1, h2i''.1] }, simp [side, hw', xm, this, h3i''] }, apply not_disjoint_iff.mpr ⟨p', hp', h2p'⟩, apply h.1, rintro rfl, apply (cs i).b_ne_xm, rw [←hi', ←hi''.1, hi.1], refl }, { intros i' hi' h2i', dsimp only [shift_up] at h2i', replace h2i' := h.injective h2i'.symm, induction h2i', exact b_ne_xm (cs i) hi' } end variables (h) [nontrivial ι] omit v /-- We get a sequence of cubes whose size is decreasing -/ noncomputable def sequence_of_cubes : ℕ → { i : ι // valley cs ((cs i).shift_up) } | 0 := let v := valley_unit_cube h in ⟨mi h v, valley_mi⟩ | (k+1) := let v := (sequence_of_cubes k).2 in ⟨mi h v, valley_mi⟩ def decreasing_sequence (k : ℕ) : ℝ := (cs (sequence_of_cubes h k).1).w lemma strict_anti_sequence_of_cubes : strict_anti $ decreasing_sequence h := strict_anti_nat_of_succ_lt $ λ k, begin let v := (sequence_of_cubes h k).2, dsimp only [decreasing_sequence, sequence_of_cubes], apply w_lt_w h v (mi_mem_bcubes : mi h v ∈ _), end lemma injective_sequence_of_cubes : injective (sequence_of_cubes h) := @injective.of_comp _ _ _ (λ x : {i : ι // _}, (cs x.1).w) _ (strict_anti_sequence_of_cubes h).injective omit h /-- The infinite sequence of cubes contradicts the finiteness of the family. -/ theorem not_correct : ¬correct cs := λ h, (finite.of_injective _ $ injective_sequence_of_cubes h).false /-- **Dissection of Cubes**: A cube cannot be cubed. -/ theorem cannot_cube_a_cube : ∀ {n : ℕ}, n ≥ 3 → -- In ℝ^n for n ≥ 3 ∀ {s : set (cube n)}, s.finite → -- given a finite collection of (hyper)cubes s.nontrivial → -- containing at least two elements s.pairwise_disjoint cube.to_set → -- which is pairwise disjoint (⋃ c ∈ s, cube.to_set c) = unit_cube.to_set → -- whose union is the unit cube inj_on cube.w s → -- such that the widths of all cubes are different false := -- then we can derive a contradiction begin intros n hn s hfin h2 hd hU hinj, cases n, { cases hn }, exact @not_correct n s coe hfin.to_subtype h2.coe_sort ⟨hd.subtype _ _, (Union_subtype _ _).trans hU, hinj.injective, hn⟩ end
2154f8923e9ddbd9365ec60f68691f60b58b027a
2bafba05c98c1107866b39609d15e849a4ca2bb8
/src/week_1/Part_B_sets.lean
649dabff3b9c1cfc7d9b0d3fbe39652269780e0d
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/formalising-mathematics
b54c83c94b5c315024ff09997fcd6b303892a749
7cf1d51c27e2038d2804561d63c74711924044a1
refs/heads/master
1,651,267,046,302
1,638,888,459,000
1,638,888,459,000
331,592,375
284
24
Apache-2.0
1,669,593,705,000
1,611,224,849,000
Lean
UTF-8
Lean
false
false
3,071
lean
import tactic -- Let `Ω` be a "big underlying set" and let `X` and `Y` and `Z` be subsets variables (Ω : Type) (X Y Z : set Ω) (a b c x y z : Ω) namespace xena /-! # subsets Let's think about `X ⊆ Y`. Typeset `⊆` with `\sub` or `\ss` -/ -- `X ⊆ Y` is the same as `∀ a, a ∈ X → a ∈ Y` , by definition. lemma subset_def : X ⊆ Y ↔ ∀ a, a ∈ X → a ∈ Y := begin -- true by definition refl end lemma subset_refl : X ⊆ X := begin sorry, end lemma subset_trans (hXY : X ⊆ Y) (hYZ : Y ⊆ Z) : X ⊆ Z := begin -- If you start with `rw subset_def at *` then you -- will have things like `hYZ : ∀ (a : Ω), a ∈ Y → a ∈ Z` -- You need to think of `hYZ` as a function, which has two -- inputs: first a term `a` of type `Ω`, and second a proof `haY` that `a ∈ Y`. -- It then produces one output `haZ`, a proof that `a ∈ Z`. -- You can also think of it as an implication: -- "if a is in Ω, and if a ∈ Y, then a ∈ Z". Because it's an implication, -- you can `apply hYZ`. This is a really useful skill! sorry end /-! # Equality of sets Two sets are equal if and only if they have the same elements. The name of this theorem is `set.ext_iff`. -/ example : X = Y ↔ (∀ a, a ∈ X ↔ a ∈ Y) := begin exact set.ext_iff end -- In practice, you often have a goal `⊢ X = Y` and you want to reduce -- it to `a ∈ X ↔ a ∈ Y` for an arbitary `a : Ω`. This can be done with -- the `ext` tactic. lemma subset.antisymm (hXY : X ⊆ Y) (hYX : Y ⊆ X) : X = Y := begin -- start with `ext a`, sorry end /-! ### Unions and intersections Type `\cup` or `\un` for `∪`, and `\cap` or `\i` for `∩` -/ lemma union_def : a ∈ X ∪ Y ↔ a ∈ X ∨ a ∈ Y := begin -- true by definition refl, end lemma inter_def : a ∈ X ∩ Y ↔ a ∈ X ∧ a ∈ Y := begin -- true by definition refl, end -- You can rewrite with those lemmas above if you're not comfortable with -- assuming they're true by definition. -- union lemmas lemma union_self : X ∪ X = X := begin sorry end lemma subset_union_left : X ⊆ X ∪ Y := begin sorry end lemma subset_union_right : Y ⊆ X ∪ Y := begin sorry end lemma union_subset_iff : X ∪ Y ⊆ Z ↔ X ⊆ Z ∧ Y ⊆ Z := begin sorry end variable (W : set Ω) lemma union_subset_union (hWX : W ⊆ X) (hYZ : Y ⊆ Z) : W ∪ Y ⊆ X ∪ Z := begin sorry end lemma union_subset_union_left (hXY : X ⊆ Y) : X ∪ Z ⊆ Y ∪ Z := begin sorry end -- etc etc -- intersection lemmas lemma inter_subset_left : X ∩ Y ⊆ X := begin sorry end -- don't forget `ext` to make progress with equalities of sets lemma inter_self : X ∩ X = X := begin sorry end lemma inter_comm : X ∩ Y = Y ∩ X := begin sorry end lemma inter_assoc : X ∩ (Y ∩ Z) = (X ∩ Y) ∩ Z := begin sorry end /-! ### Forall and exists -/ lemma not_exists_iff_forall_not : ¬ (∃ a, a ∈ X) ↔ ∀ b, ¬ (b ∈ X) := begin sorry, end example : ¬ (∀ a, a ∈ X) ↔ ∃ b, ¬ (b ∈ X) := begin sorry, end end xena
5eae272e39c11dfa2d53c2cd730cf93d3fc37ab3
a1179fa077c09acc49e4fbc8d67084ba89ac4f4c
/tutorials/src/exercises/07_first_negations.lean
bd269a172e4e953f7cc5ce094288b7a4a9469c43
[]
no_license
Seeram/Lean-proof-assistant
11ca0ca0e0446bacdd1773c4c481a3653b2f1074
e672d46e0e5f39d8de2933ad4f4cac095ca6094f
refs/heads/master
1,682,754,224,366
1,620,959,431,000
1,620,959,431,000
299,000,950
0
1
null
1,620,680,462,000
1,601,200,258,000
Lean
UTF-8
Lean
false
false
6,787
lean
import tuto_lib import data.int.parity /- Negations, proof by contradiction and contraposition. This file introduces the logical rules and tactics related to negation: exfalso, by_contradiction, contrapose, by_cases and push_neg. There is a special statement denoted by `false` which, by definition, has no proof. So `false` implies everything. Indeed `false → P` means any proof of `false` could be turned into a proof of P. This fact is known by its latin name "ex falso quod libet" (from false follows whatever you want). Hence Lean's tactic to invoke this is called `exfalso`. -/ example : false → 0 = 1 := begin intro h, exfalso, exact h, end /- The preceding example suggests that this definition of `false` isn't very useful. But actually it allows us to define the negation of a statement P as "P implies false" that we can read as "if P were true, we would get a contradiction". Lean denotes this by `¬ P`. One can prove that (¬ P) ↔ (P ↔ false). But in practice we directly use the definition of `¬ P`. -/ example {x : ℝ} : ¬ x < x := begin intro hyp, rw lt_iff_le_and_ne at hyp, cases hyp with hyp_inf hyp_non, clear hyp_inf, -- we won't use that one, so let's discard it change x = x → false at hyp_non, -- Lean doesn't need this psychological line apply hyp_non, refl, end open int -- 0045 example (n : ℤ) (h_pair : even n) (h_non_pair : ¬ even n) : 0 = 1 := begin sorry end -- 0046 example (P Q : Prop) (h₁ : P ∨ Q) (h₂ : ¬ (P ∧ Q)) : ¬ P ↔ Q := begin sorry end /- The definition of negation easily implies that, for every statement P, P → ¬ ¬ P The excluded middle axiom, which asserts P ∨ ¬ P allows us to prove the converse implication. Together those two implications form the principle of double negation elimination. not_not {P : Prop} : (¬ ¬ P) ↔ P The implication `¬ ¬ P → P` is the basis for proofs by contradiction: in order to prove P, it suffices to prove ¬¬ P, ie `¬ P → false`. Of course there is no need to keep explaining all this. The tactic `by_contradiction Hyp` will transform any goal P into `false` and add Hyp : ¬ P to the local context. Let's return to a proof from the 5th file: uniqueness of limits for a sequence. This cannot be proved without using some version of the excluded middle axiom. We used it secretely in eq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y (we'll prove a variation on this lemma below). -/ example (u : ℕ → ℝ) (l l' : ℝ) : seq_limit u l → seq_limit u l' → l = l' := begin intros hl hl', by_contradiction H, change l ≠ l' at H, -- Lean does not need this line have ineg : |l-l'| > 0, exact abs_pos.mpr (sub_ne_zero_of_ne H), cases hl ( |l-l'|/4 ) (by linarith) with N hN, cases hl' ( |l-l'|/4 ) (by linarith) with N' hN', let N₀ := max N N', -- this is a new tactic, whose effect should be clear specialize hN N₀ (le_max_left _ _), specialize hN' N₀ (le_max_right _ _), have clef : |l-l'| < |l-l'|, calc |l - l'| = |(l-u N₀) + (u N₀ -l')| : by ring ... ≤ |l - u N₀| + |u N₀ - l'| : by apply abs_add ... = |u N₀ - l| + |u N₀ - l'| : by rw abs_sub ... < |l-l'| : by linarith, linarith, -- linarith can also find simple numerical contradictions end /- Another incarnation of the excluded middle axiom is the principle of contraposition: in order to prove P ⇒ Q, it suffices to prove non Q ⇒ non P. -/ -- Using a proof by contradiction, let's prove the contraposition principle -- 0047 example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q := begin sorry end /- Again Lean doesn't need to be explain this principle. We can use the `contrapose` tactic. -/ example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q := begin contrapose, exact h, end /- In the next exercise, we'll use odd n : ∃ k, n = 2*k + 1 int.odd_iff_not_even {n : ℤ} : odd n ↔ ¬ even n -/ -- 0048 example (n : ℤ) : even (n^2) ↔ even n := begin sorry end /- As a last step on our law of the excluded middle tour, let's notice that, especially in pure logic exercises, it can sometimes be useful to use the excluded middle axiom in its original form: classical.em : ∀ P, P ∨ ¬ P Instead of applying this lemma and then using the `cases` tactic, we have the shortcut by_cases h : P, combining both steps to create two proof branches: one assuming h : P, and the other assuming h : ¬ P For instance, let's prove a reformulation of this implication relation, which is sometimes used as a definition in other logical foundations, especially those based on truth tables (hence very strongly using excluded middle from the very beginning). -/ variables (P Q : Prop) example : (P → Q) ↔ (¬ P ∨ Q) := begin split, { intro h, by_cases hP : P, { right, exact h hP }, { left, exact hP } }, { intros h hP, cases h with hnP hQ, { exfalso, exact hnP hP }, { exact hQ } }, end -- 0049 example : ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q := begin sorry end /- It is crucial to understand negation of quantifiers. Let's do it by hand for a little while. In the first exercise, only the definition of negation is needed. -/ -- 0050 example (n : ℤ) : ¬ (∃ k, n = 2*k) ↔ ∀ k, n ≠ 2*k := begin sorry end /- Contrary to negation of the existential quantifier, negation of the universal quantifier requires excluded middle for the first implication. In order to prove this, we can use either * a double proof by contradiction * a contraposition, not_not : (¬ ¬ P) ↔ P) and a proof by contradiction. -/ def even_fun (f : ℝ → ℝ) := ∀ x, f (-x) = f x -- 0051 example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x := begin sorry end /- Of course we can't keep repeating the above proofs, especially the second one. So we use the `push_neg` tactic. -/ example : ¬ even_fun (λ x, 2*x) := begin unfold even_fun, -- Here unfolding is important because push_neg won't do it. push_neg, use 42, linarith, end -- 0052 example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x := begin sorry end def bounded_above (f : ℝ → ℝ) := ∃ M, ∀ x, f x ≤ M example : ¬ bounded_above (λ x, x) := begin unfold bounded_above, push_neg, intro M, use M + 1, linarith, end -- Let's contrapose -- 0053 example (x : ℝ) : (∀ ε > 0, x ≤ ε) → x ≤ 0 := begin sorry end /- The "contrapose, push_neg" combo is so common that we can abreviate it to `contrapose!` Let's use this trick, together with: eq_or_lt_of_le : a ≤ b → a = b ∨ a < b -/ -- 0054 example (f : ℝ → ℝ) : (∀ x y, x < y → f x < f y) ↔ (∀ x y, (x ≤ y ↔ f x ≤ f y)) := begin sorry end
116f728c76be0ed4b6ac3d5b5c66f1befd52e695
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/group_theory/free_group.lean
9be9c7c676ea280e4a0c9bdb0a6f7f3ef4ef86f4
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
30,948
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Free groups as a quotient over the reduction relation `a * x * x⁻¹ * b = a * b`. First we introduce the one step reduction relation `free_group.red.step`: w * x * x⁻¹ * v ~> w * v its reflexive transitive closure: `free_group.red.trans` and proof that its join is an equivalence relation. Then we introduce `free_group α` as a quotient over `free_group.red.step`. -/ import logic.relation import algebra.group_power import data.fintype import group_theory.subgroup open relation universes u v w variables {α : Type u} local attribute [simp] list.append_eq_has_append namespace free_group variables {L L₁ L₂ L₃ L₄ : list (α × bool)} /-- Reduction step: `w * x * x⁻¹ * v ~> w * v` -/ inductive red.step : list (α × bool) → list (α × bool) → Prop | bnot {L₁ L₂ x b} : red.step (L₁ ++ (x, b) :: (x, bnot b) :: L₂) (L₁ ++ L₂) attribute [simp] red.step.bnot /-- Reflexive-transitive closure of red.step -/ def red : list (α × bool) → list (α × bool) → Prop := refl_trans_gen red.step @[refl] lemma red.refl : red L L := refl_trans_gen.refl @[trans] lemma red.trans : red L₁ L₂ → red L₂ L₃ → red L₁ L₃ := refl_trans_gen.trans namespace red /-- Predicate asserting that word `w₁` can be reduced to `w₂` in one step, i.e. there are words `w₃ w₄` and letter `x` such that `w₁ = w₃xx⁻¹w₄` and `w₂ = w₃w₄` -/ theorem step.length : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.length + 2 = L₁.length | _ _ (@red.step.bnot _ L1 L2 x b) := by rw [list.length_append, list.length_append]; refl @[simp] lemma step.bnot_rev {x b} : step (L₁ ++ (x, bnot b) :: (x, b) :: L₂) (L₁ ++ L₂) := by cases b; from step.bnot @[simp] lemma step.cons_bnot {x b} : red.step ((x, b) :: (x, bnot b) :: L) L := @step.bnot _ [] _ _ _ @[simp] lemma step.cons_bnot_rev {x b} : red.step ((x, bnot b) :: (x, b) :: L) L := @red.step.bnot_rev _ [] _ _ _ theorem step.append_left : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₂ L₃ → step (L₁ ++ L₂) (L₁ ++ L₃) | _ _ _ red.step.bnot := by rw [← list.append_assoc, ← list.append_assoc]; constructor theorem step.cons {x} (H : red.step L₁ L₂) : red.step (x :: L₁) (x :: L₂) := @step.append_left _ [x] _ _ H theorem step.append_right : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₁ L₂ → step (L₁ ++ L₃) (L₂ ++ L₃) | _ _ _ red.step.bnot := by simp lemma not_step_nil : ¬ step [] L := begin generalize h' : [] = L', assume h, cases h with L₁ L₂, simp [list.nil_eq_append_iff] at h', contradiction end lemma step.cons_left_iff {a : α} {b : bool} : step ((a, b) :: L₁) L₂ ↔ (∃L, step L₁ L ∧ L₂ = (a, b) :: L) ∨ (L₁ = (a, bnot b)::L₂) := begin split, { generalize hL : ((a, b) :: L₁ : list _) = L, assume h, rcases h with ⟨_ | ⟨p, s'⟩, e, a', b'⟩, { simp at hL, simp [*] }, { simp at hL, rcases hL with ⟨rfl, rfl⟩, refine or.inl ⟨s' ++ e, step.bnot, _⟩, simp } }, { assume h, rcases h with ⟨L, h, rfl⟩ | rfl, { exact step.cons h }, { exact step.cons_bnot } } end lemma not_step_singleton : ∀ {p : α × bool}, ¬ step [p] L | (a, b) := by simp [step.cons_left_iff, not_step_nil] lemma step.cons_cons_iff : ∀{p : α × bool}, step (p :: L₁) (p :: L₂) ↔ step L₁ L₂ := by simp [step.cons_left_iff, iff_def, or_imp_distrib] {contextual := tt} lemma step.append_left_iff : ∀L, step (L ++ L₁) (L ++ L₂) ↔ step L₁ L₂ | [] := by simp | (p :: l) := by simp [step.append_left_iff l, step.cons_cons_iff] private theorem step.diamond_aux : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)} {x1 b1 x2 b2}, L₁ ++ (x1, b1) :: (x1, bnot b1) :: L₂ = L₃ ++ (x2, b2) :: (x2, bnot b2) :: L₄ → L₁ ++ L₂ = L₃ ++ L₄ ∨ ∃ L₅, red.step (L₁ ++ L₂) L₅ ∧ red.step (L₃ ++ L₄) L₅ | [] _ [] _ _ _ _ _ H := by injections; subst_vars; simp | [] _ [(x3,b3)] _ _ _ _ _ H := by injections; subst_vars; simp | [(x3,b3)] _ [] _ _ _ _ _ H := by injections; subst_vars; simp | [] _ ((x3,b3)::(x4,b4)::tl) _ _ _ _ _ H := by injections; subst_vars; simp; right; exact ⟨_, red.step.bnot, red.step.cons_bnot⟩ | ((x3,b3)::(x4,b4)::tl) _ [] _ _ _ _ _ H := by injections; subst_vars; simp; right; exact ⟨_, red.step.cons_bnot, red.step.bnot⟩ | ((x3,b3)::tl) _ ((x4,b4)::tl2) _ _ _ _ _ H := let ⟨H1, H2⟩ := list.cons.inj H in match step.diamond_aux H2 with | or.inl H3 := or.inl $ by simp [H1, H3] | or.inr ⟨L₅, H3, H4⟩ := or.inr ⟨_, step.cons H3, by simpa [H1] using step.cons H4⟩ end theorem step.diamond : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)}, red.step L₁ L₃ → red.step L₂ L₄ → L₁ = L₂ → L₃ = L₄ ∨ ∃ L₅, red.step L₃ L₅ ∧ red.step L₄ L₅ | _ _ _ _ red.step.bnot red.step.bnot H := step.diamond_aux H lemma step.to_red : step L₁ L₂ → red L₁ L₂ := refl_trans_gen.single /-- Church-Rosser theorem for word reduction: If `w1 w2 w3` are words such that `w1` reduces to `w2` and `w3` respectively, then there is a word `w4` such that `w2` and `w3` reduce to `w4` respectively. -/ theorem church_rosser : red L₁ L₂ → red L₁ L₃ → join red L₂ L₃ := relation.church_rosser (assume a b c hab hac, match b, c, red.step.diamond hab hac rfl with | b, _, or.inl rfl := ⟨b, by refl, by refl⟩ | b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, hcd.to_red⟩ end) lemma cons_cons {p} : red L₁ L₂ → red (p :: L₁) (p :: L₂) := refl_trans_gen_lift (list.cons p) (assume a b, step.cons) lemma cons_cons_iff (p) : red (p :: L₁) (p :: L₂) ↔ red L₁ L₂ := iff.intro begin generalize eq₁ : (p :: L₁ : list _) = LL₁, generalize eq₂ : (p :: L₂ : list _) = LL₂, assume h, induction h using relation.refl_trans_gen.head_induction_on with L₁ L₂ h₁₂ h ih generalizing L₁ L₂, { subst_vars, cases eq₂, constructor }, { subst_vars, cases p with a b, rw [step.cons_left_iff] at h₁₂, rcases h₁₂ with ⟨L, h₁₂, rfl⟩ | rfl, { exact (ih rfl rfl).head h₁₂ }, { exact (cons_cons h).tail step.cons_bnot_rev } } end cons_cons lemma append_append_left_iff : ∀L, red (L ++ L₁) (L ++ L₂) ↔ red L₁ L₂ | [] := iff.rfl | (p :: L) := by simp [append_append_left_iff L, cons_cons_iff] lemma append_append (h₁ : red L₁ L₃) (h₂ : red L₂ L₄) : red (L₁ ++ L₂) (L₃ ++ L₄) := (refl_trans_gen_lift (λL, L ++ L₂) (assume a b, step.append_right) h₁).trans ((append_append_left_iff _).2 h₂) lemma to_append_iff : red L (L₁ ++ L₂) ↔ (∃L₃ L₄, L = L₃ ++ L₄ ∧ red L₃ L₁ ∧ red L₄ L₂) := iff.intro begin generalize eq : L₁ ++ L₂ = L₁₂, assume h, induction h with L' L₁₂ hLL' h ih generalizing L₁ L₂, { exact ⟨_, _, eq.symm, by refl, by refl⟩ }, { cases h with s e a b, rcases list.append_eq_append_iff.1 eq with ⟨s', rfl, rfl⟩ | ⟨e', rfl, rfl⟩, { have : L₁ ++ (s' ++ ((a, b) :: (a, bnot b) :: e)) = (L₁ ++ s') ++ ((a, b) :: (a, bnot b) :: e), { simp }, rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩, exact ⟨w₁, w₂, rfl, h₁, h₂.tail step.bnot⟩ }, { have : (s ++ ((a, b) :: (a, bnot b) :: e')) ++ L₂ = s ++ ((a, b) :: (a, bnot b) :: (e' ++ L₂)), { simp }, rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩, exact ⟨w₁, w₂, rfl, h₁.tail step.bnot, h₂⟩ }, } end (assume ⟨L₃, L₄, eq, h₃, h₄⟩, eq.symm ▸ append_append h₃ h₄) /-- The empty word `[]` only reduces to itself. -/ theorem nil_iff : red [] L ↔ L = [] := refl_trans_gen_iff_eq (assume l, red.not_step_nil) /-- A letter only reduces to itself. -/ theorem singleton_iff {x} : red [x] L₁ ↔ L₁ = [x] := refl_trans_gen_iff_eq (assume l, not_step_singleton) /-- If `x` is a letter and `w` is a word such that `xw` reduces to the empty word, then `w` reduces to `x⁻¹` -/ theorem cons_nil_iff_singleton {x b} : red ((x, b) :: L) [] ↔ red L [(x, bnot b)] := iff.intro (assume h, have h₁ : red ((x, bnot b) :: (x, b) :: L) [(x, bnot b)], from cons_cons h, have h₂ : red ((x, bnot b) :: (x, b) :: L) L, from refl_trans_gen.single step.cons_bnot_rev, let ⟨L', h₁, h₂⟩ := church_rosser h₁ h₂ in by rw [singleton_iff] at h₁; subst L'; assumption) (assume h, (cons_cons h).tail step.cons_bnot) theorem red_iff_irreducible {x1 b1 x2 b2} (h : (x1, b1) ≠ (x2, b2)) : red [(x1, bnot b1), (x2, b2)] L ↔ L = [(x1, bnot b1), (x2, b2)] := begin apply refl_trans_gen_iff_eq, generalize eq : [(x1, bnot b1), (x2, b2)] = L', assume L h', cases h', simp [list.cons_eq_append_iff, list.nil_eq_append_iff] at eq, rcases eq with ⟨rfl, ⟨rfl, rfl⟩, ⟨rfl, rfl⟩, rfl⟩, subst_vars, simp at h, contradiction end /-- If `x` and `y` are distinct letters and `w₁ w₂` are words such that `xw₁` reduces to `yw₂`, then `w₁` reduces to `x⁻¹yw₂`. -/ theorem inv_of_red_of_ne {x1 b1 x2 b2} (H1 : (x1, b1) ≠ (x2, b2)) (H2 : red ((x1, b1) :: L₁) ((x2, b2) :: L₂)) : red L₁ ((x1, bnot b1) :: (x2, b2) :: L₂) := begin have : red ((x1, b1) :: L₁) ([(x2, b2)] ++ L₂), from H2, rcases to_append_iff.1 this with ⟨_ | ⟨p, L₃⟩, L₄, eq, h₁, h₂⟩, { simp [nil_iff] at h₁, contradiction }, { cases eq, show red (L₃ ++ L₄) ([(x1, bnot b1), (x2, b2)] ++ L₂), apply append_append _ h₂, have h₁ : red ((x1, bnot b1) :: (x1, b1) :: L₃) [(x1, bnot b1), (x2, b2)], { exact cons_cons h₁ }, have h₂ : red ((x1, bnot b1) :: (x1, b1) :: L₃) L₃, { exact step.cons_bnot_rev.to_red }, rcases church_rosser h₁ h₂ with ⟨L', h₁, h₂⟩, rw [red_iff_irreducible H1] at h₁, rwa [h₁] at h₂ } end theorem step.sublist (H : red.step L₁ L₂) : L₂ <+ L₁ := by cases H; simp; constructor; constructor; refl /-- If `w₁ w₂` are words such that `w₁` reduces to `w₂`, then `w₂` is a sublist of `w₁`. -/ theorem sublist : red L₁ L₂ → L₂ <+ L₁ := refl_trans_gen_of_transitive_reflexive (λl, list.sublist.refl l) (λa b c hab hbc, list.sublist.trans hbc hab) (λa b, red.step.sublist) theorem sizeof_of_step : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.sizeof < L₁.sizeof | _ _ (@step.bnot _ L1 L2 x b) := begin induction L1 with hd tl ih, case list.nil { dsimp [list.sizeof], have H : 1 + sizeof (x, b) + (1 + sizeof (x, bnot b) + list.sizeof L2) = (list.sizeof L2 + 1) + (sizeof (x, b) + sizeof (x, bnot b) + 1), { ac_refl }, rw H, exact nat.le_add_right _ _ }, case list.cons { dsimp [list.sizeof], exact nat.add_lt_add_left ih _ } end theorem length (h : red L₁ L₂) : ∃ n, L₁.length = L₂.length + 2 * n := begin induction h with L₂ L₃ h₁₂ h₂₃ ih, { exact ⟨0, rfl⟩ }, { rcases ih with ⟨n, eq⟩, existsi (1 + n), simp [mul_add, eq, (step.length h₂₃).symm] } end theorem antisymm (h₁₂ : red L₁ L₂) : red L₂ L₁ → L₁ = L₂ := match L₁, h₁₂.cases_head with | _, or.inl rfl := assume h, rfl | L₁, or.inr ⟨L₃, h₁₃, h₃₂⟩ := assume h₂₁, let ⟨n, eq⟩ := length (h₃₂.trans h₂₁) in have list.length L₃ + 0 = list.length L₃ + (2 * n + 2), by simpa [(step.length h₁₃).symm, add_comm, add_assoc] using eq, (nat.no_confusion $ nat.add_left_cancel this) end end red theorem equivalence_join_red : equivalence (join (@red α)) := equivalence_join_refl_trans_gen $ assume a b c hab hac, (match b, c, red.step.diamond hab hac rfl with | b, _, or.inl rfl := ⟨b, by refl, by refl⟩ | b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, refl_trans_gen.single hcd⟩ end) theorem join_red_of_step (h : red.step L₁ L₂) : join red L₁ L₂ := join_of_single reflexive_refl_trans_gen h.to_red theorem eqv_gen_step_iff_join_red : eqv_gen red.step L₁ L₂ ↔ join red L₁ L₂ := iff.intro (assume h, have eqv_gen (join red) L₁ L₂ := eqv_gen_mono (assume a b, join_red_of_step) h, (eqv_gen_iff_of_equivalence $ equivalence_join_red).1 this) (join_of_equivalence (eqv_gen.is_equivalence _) $ assume a b, refl_trans_gen_of_equivalence (eqv_gen.is_equivalence _) eqv_gen.rel) end free_group /-- The free group over a type, i.e. the words formed by the elements of the type and their formal inverses, quotient by one step reduction. -/ def free_group (α : Type u) : Type u := quot $ @free_group.red.step α namespace free_group variables {α} {L L₁ L₂ L₃ L₄ : list (α × bool)} def mk (L) : free_group α := quot.mk red.step L @[simp] lemma quot_mk_eq_mk : quot.mk red.step L = mk L := rfl @[simp] lemma quot_lift_mk (β : Type v) (f : list (α × bool) → β) (H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) : quot.lift f H (mk L) = f L := rfl @[simp] lemma quot_lift_on_mk (β : Type v) (f : list (α × bool) → β) (H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) : quot.lift_on (mk L) f H = f L := rfl instance : has_one (free_group α) := ⟨mk []⟩ lemma one_eq_mk : (1 : free_group α) = mk [] := rfl instance : inhabited (free_group α) := ⟨1⟩ instance : has_mul (free_group α) := ⟨λ x y, quot.lift_on x (λ L₁, quot.lift_on y (λ L₂, mk $ L₁ ++ L₂) (λ L₂ L₃ H, quot.sound $ red.step.append_left H)) (λ L₁ L₂ H, quot.induction_on y $ λ L₃, quot.sound $ red.step.append_right H)⟩ @[simp] lemma mul_mk : mk L₁ * mk L₂ = mk (L₁ ++ L₂) := rfl instance : has_inv (free_group α) := ⟨λx, quot.lift_on x (λ L, mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse) (assume a b h, quot.sound $ by cases h; simp)⟩ @[simp] lemma inv_mk : (mk L)⁻¹ = mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse := rfl instance : group (free_group α) := { mul := (*), one := 1, inv := has_inv.inv, mul_assoc := by rintros ⟨L₁⟩ ⟨L₂⟩ ⟨L₃⟩; simp, one_mul := by rintros ⟨L⟩; refl, mul_one := by rintros ⟨L⟩; simp [one_eq_mk], mul_left_inv := by rintros ⟨L⟩; exact (list.rec_on L rfl $ λ ⟨x, b⟩ tl ih, eq.trans (quot.sound $ by simp [one_eq_mk]) ih) } /-- `of x` is the canonical injection from the type to the free group over that type by sending each element to the equivalence class of the letter that is the element. -/ def of (x : α) : free_group α := mk [(x, tt)] theorem red.exact : mk L₁ = mk L₂ ↔ join red L₁ L₂ := calc (mk L₁ = mk L₂) ↔ eqv_gen red.step L₁ L₂ : iff.intro (quot.exact _) quot.eqv_gen_sound ... ↔ join red L₁ L₂ : eqv_gen_step_iff_join_red /-- The canonical injection from the type to the free group is an injection. -/ theorem of.inj {x y : α} (H : of x = of y) : x = y := let ⟨L₁, hx, hy⟩ := red.exact.1 H in by simp [red.singleton_iff] at hx hy; cc section to_group variables {β : Type v} [group β] (f : α → β) {x y : free_group α} def to_group.aux : list (α × bool) → β := λ L, list.prod $ L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹ theorem red.step.to_group {f : α → β} (H : red.step L₁ L₂) : to_group.aux f L₁ = to_group.aux f L₂ := by cases H with _ _ _ b; cases b; simp [to_group.aux] /-- If `β` is a group, then any function from `α` to `β` extends uniquely to a group homomorphism from the free group over `α` to `β` -/ def to_group : free_group α → β := quot.lift (to_group.aux f) $ λ L₁ L₂ H, red.step.to_group H variable {f} @[simp] lemma to_group.mk : to_group f (mk L) = list.prod (L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹) := rfl @[simp] lemma to_group.of {x} : to_group f (of x) = f x := one_mul _ instance to_group.is_group_hom : is_group_hom (to_group f) := { map_mul := by rintros ⟨L₁⟩ ⟨L₂⟩; simp } @[simp] lemma to_group.mul : to_group f (x * y) = to_group f x * to_group f y := is_mul_hom.map_mul _ _ _ @[simp] lemma to_group.one : to_group f 1 = 1 := is_group_hom.map_one _ @[simp] lemma to_group.inv : to_group f x⁻¹ = (to_group f x)⁻¹ := is_group_hom.map_inv _ _ theorem to_group.unique (g : free_group α → β) [is_group_hom g] (hg : ∀ x, g (of x) = f x) : ∀{x}, g x = to_group f x := by rintros ⟨L⟩; exact list.rec_on L (is_group_hom.map_one g) (λ ⟨x, b⟩ t (ih : g (mk t) = _), bool.rec_on b (show g ((of x)⁻¹ * mk t) = to_group f (mk ((x, ff) :: t)), by simp [is_mul_hom.map_mul g, is_group_hom.map_inv g, hg, ih, to_group, to_group.aux]) (show g (of x * mk t) = to_group f (mk ((x, tt) :: t)), by simp [is_mul_hom.map_mul g, is_group_hom.map_inv g, hg, ih, to_group, to_group.aux])) theorem to_group.of_eq (x : free_group α) : to_group of x = x := eq.symm $ to_group.unique id (λ x, rfl) theorem to_group.range_subset {s : set β} [is_subgroup s] (H : set.range f ⊆ s) : set.range (to_group f) ⊆ s := by rintros _ ⟨⟨L⟩, rfl⟩; exact list.rec_on L (is_submonoid.one_mem s) (λ ⟨x, b⟩ tl ih, bool.rec_on b (by simp at ih ⊢; from is_submonoid.mul_mem (is_subgroup.inv_mem $ H ⟨x, rfl⟩) ih) (by simp at ih ⊢; from is_submonoid.mul_mem (H ⟨x, rfl⟩) ih)) theorem to_group.range_eq_closure : set.range (to_group f) = group.closure (set.range f) := set.subset.antisymm (to_group.range_subset group.subset_closure) (group.closure_subset $ λ y ⟨x, hx⟩, ⟨of x, by simpa⟩) end to_group section map variables {β : Type v} (f : α → β) {x y : free_group α} def map.aux (L : list (α × bool)) : list (β × bool) := L.map $ λ x, (f x.1, x.2) /-- Any function from `α` to `β` extends uniquely to a group homomorphism from the free group ver `α` to the free group over `β`. -/ def map (x : free_group α) : free_group β := x.lift_on (λ L, mk $ map.aux f L) $ λ L₁ L₂ H, quot.sound $ by cases H; simp [map.aux] instance map.is_group_hom : is_group_hom (map f) := { map_mul := by rintros ⟨L₁⟩ ⟨L₂⟩; simp [map, map.aux] } variable {f} @[simp] lemma map.mk : map f (mk L) = mk (L.map (λ x, (f x.1, x.2))) := rfl @[simp] lemma map.id : map id x = x := have H1 : (λ (x : α × bool), x) = id := rfl, by rcases x with ⟨L⟩; simp [H1] @[simp] lemma map.id' : map (λ z, z) x = x := map.id theorem map.comp {γ : Type w} {f : α → β} {g : β → γ} {x} : map g (map f x) = map (g ∘ f) x := by rcases x with ⟨L⟩; simp @[simp] lemma map.of {x} : map f (of x) = of (f x) := rfl @[simp] lemma map.mul : map f (x * y) = map f x * map f y := is_mul_hom.map_mul _ x y @[simp] lemma map.one : map f 1 = 1 := is_group_hom.map_one _ @[simp] lemma map.inv : map f x⁻¹ = (map f x)⁻¹ := is_group_hom.map_inv _ x theorem map.unique (g : free_group α → free_group β) [is_group_hom g] (hg : ∀ x, g (of x) = of (f x)) : ∀{x}, g x = map f x := by rintros ⟨L⟩; exact list.rec_on L (is_group_hom.map_one g) (λ ⟨x, b⟩ t (ih : g (mk t) = map f (mk t)), bool.rec_on b (show g ((of x)⁻¹ * mk t) = map f ((of x)⁻¹ * mk t), by simp [is_mul_hom.map_mul g, is_group_hom.map_inv g, hg, ih]) (show g (of x * mk t) = map f (of x * mk t), by simp [is_mul_hom.map_mul g, hg, ih])) /-- Equivalent types give rise to equivalent free groups. -/ def free_group_congr {α β} (e : α ≃ β) : free_group α ≃ free_group β := ⟨map e, map e.symm, λ x, by simp [function.comp, map.comp], λ x, by simp [function.comp, map.comp]⟩ theorem map_eq_to_group : map f x = to_group (of ∘ f) x := eq.symm $ map.unique _ $ λ x, by simp end map section prod variables [group α] (x y : free_group α) /-- If `α` is a group, then any function from `α` to `α` extends uniquely to a homomorphism from the free group over `α` to `α`. This is the multiplicative version of `sum`. -/ def prod : α := to_group id x variables {x y} @[simp] lemma prod_mk : prod (mk L) = list.prod (L.map $ λ x, cond x.2 x.1 x.1⁻¹) := rfl @[simp] lemma prod.of {x : α} : prod (of x) = x := to_group.of instance prod.is_group_hom : is_group_hom (@prod α _) := to_group.is_group_hom @[simp] lemma prod.mul : prod (x * y) = prod x * prod y := to_group.mul @[simp] lemma prod.one : prod (1:free_group α) = 1 := to_group.one @[simp] lemma prod.inv : prod x⁻¹ = (prod x)⁻¹ := to_group.inv lemma prod.unique (g : free_group α → α) [is_group_hom g] (hg : ∀ x, g (of x) = x) {x} : g x = prod x := to_group.unique g hg end prod theorem to_group_eq_prod_map {β : Type v} [group β] {f : α → β} {x} : to_group f x = prod (map f x) := have is_group_hom (prod ∘ map f) := is_group_hom.comp _ _, by exactI (eq.symm $ to_group.unique (prod ∘ map f) $ λ _, by simp) section sum variables [add_group α] (x y : free_group α) /-- If `α` is a group, then any function from `α` to `α` extends uniquely to a homomorphism from the free group over `α` to `α`. This is the additive version of `prod`. -/ def sum : α := @prod (multiplicative _) _ x variables {x y} @[simp] lemma sum_mk : sum (mk L) = list.sum (L.map $ λ x, cond x.2 x.1 (-x.1)) := rfl @[simp] lemma sum.of {x : α} : sum (of x) = x := prod.of instance sum.is_group_hom : is_group_hom (@sum α _) := prod.is_group_hom @[simp] lemma sum.mul : sum (x * y) = sum x + sum y := prod.mul @[simp] lemma sum.one : sum (1:free_group α) = 0 := prod.one @[simp] lemma sum.inv : sum x⁻¹ = -sum x := prod.inv end sum def free_group_empty_equiv_unit : free_group empty ≃ unit := { to_fun := λ _, (), inv_fun := λ _, 1, left_inv := by rintros ⟨_ | ⟨⟨⟨⟩, _⟩, _⟩⟩; refl, right_inv := λ ⟨⟩, rfl } def free_group_unit_equiv_int : free_group unit ≃ int := { to_fun := λ x, sum $ map (λ _, 1) x, inv_fun := λ x, of () ^ x, left_inv := by rintros ⟨L⟩; exact list.rec_on L rfl (λ ⟨⟨⟩, b⟩ tl ih, by cases b; simp [gpow_add] at ih ⊢; rw ih; refl), right_inv := λ x, int.induction_on x (by simp) (λ i ih, by simp at ih; simp [gpow_add, ih]) (λ i ih, by simp at ih; simp [gpow_add, ih, sub_eq_add_neg]) } section category variables {β : Type u} instance : monad free_group.{u} := { pure := λ α, of, map := λ α β, map, bind := λ α β x f, to_group f x } @[elab_as_eliminator] protected theorem induction_on {C : free_group α → Prop} (z : free_group α) (C1 : C 1) (Cp : ∀ x, C $ pure x) (Ci : ∀ x, C (pure x) → C (pure x)⁻¹) (Cm : ∀ x y, C x → C y → C (x * y)) : C z := quot.induction_on z $ λ L, list.rec_on L C1 $ λ ⟨x, b⟩ tl ih, bool.rec_on b (Cm _ _ (Ci _ $ Cp x) ih) (Cm _ _ (Cp x) ih) @[simp] lemma map_pure (f : α → β) (x : α) : f <$> (pure x : free_group α) = pure (f x) := map.of @[simp] lemma map_one (f : α → β) : f <$> (1 : free_group α) = 1 := map.one @[simp] lemma map_mul (f : α → β) (x y : free_group α) : f <$> (x * y) = f <$> x * f <$> y := map.mul @[simp] lemma map_inv (f : α → β) (x : free_group α) : f <$> (x⁻¹) = (f <$> x)⁻¹ := map.inv @[simp] lemma pure_bind (f : α → free_group β) (x) : pure x >>= f = f x := to_group.of @[simp] lemma one_bind (f : α → free_group β) : 1 >>= f = 1 := @@to_group.one _ f @[simp] lemma mul_bind (f : α → free_group β) (x y : free_group α) : x * y >>= f = (x >>= f) * (y >>= f) := to_group.mul @[simp] lemma inv_bind (f : α → free_group β) (x : free_group α) : x⁻¹ >>= f = (x >>= f)⁻¹ := to_group.inv instance : is_lawful_monad free_group.{u} := { id_map := λ α x, free_group.induction_on x (map_one id) (λ x, map_pure id x) (λ x ih, by rw [map_inv, ih]) (λ x y ihx ihy, by rw [map_mul, ihx, ihy]), pure_bind := λ α β x f, pure_bind f x, bind_assoc := λ α β γ x f g, free_group.induction_on x (by iterate 3 { rw one_bind }) (λ x, by iterate 2 { rw pure_bind }) (λ x ih, by iterate 3 { rw inv_bind }; rw ih) (λ x y ihx ihy, by iterate 3 { rw mul_bind }; rw [ihx, ihy]), bind_pure_comp_eq_map := λ α β f x, free_group.induction_on x (by rw [one_bind, map_one]) (λ x, by rw [pure_bind, map_pure]) (λ x ih, by rw [inv_bind, map_inv, ih]) (λ x y ihx ihy, by rw [mul_bind, map_mul, ihx, ihy]) } end category section reduce variable [decidable_eq α] /-- The maximal reduction of a word. It is computable iff `α` has decidable equality. -/ def reduce (L : list (α × bool)) : list (α × bool) := list.rec_on L [] $ λ hd1 tl1 ih, list.cases_on ih [hd1] $ λ hd2 tl2, if hd1.1 = hd2.1 ∧ hd1.2 = bnot hd2.2 then tl2 else hd1 :: hd2 :: tl2 @[simp] lemma reduce.cons (x) : reduce (x :: L) = list.cases_on (reduce L) [x] (λ hd tl, if x.1 = hd.1 ∧ x.2 = bnot hd.2 then tl else x :: hd :: tl) := rfl /-- The first theorem that characterises the function `reduce`: a word reduces to its maximal reduction. -/ theorem reduce.red : red L (reduce L) := begin induction L with hd1 tl1 ih, case list.nil { constructor }, case list.cons { dsimp, revert ih, generalize htl : reduce tl1 = TL, intro ih, cases TL with hd2 tl2, case list.nil { exact red.cons_cons ih }, case list.cons { dsimp, by_cases h : hd1.fst = hd2.fst ∧ hd1.snd = bnot (hd2.snd), { rw [if_pos h], transitivity, { exact red.cons_cons ih }, { cases hd1, cases hd2, cases h, dsimp at *, subst_vars, exact red.step.cons_bnot_rev.to_red } }, { rw [if_neg h], exact red.cons_cons ih } } } end theorem reduce.not {p : Prop} : ∀ {L₁ L₂ L₃ : list (α × bool)} {x b}, reduce L₁ = L₂ ++ (x, b) :: (x, bnot b) :: L₃ → p | [] L2 L3 _ _ := λ h, by cases L2; injections | ((x,b)::L1) L2 L3 x' b' := begin dsimp, cases r : reduce L1, { dsimp, intro h, have := congr_arg list.length h, simp [-add_comm] at this, exact absurd this dec_trivial }, cases hd with y c, by_cases x = y ∧ b = bnot c; simp [h]; intro H, { rw H at r, exact @reduce.not L1 ((y,c)::L2) L3 x' b' r }, rcases L2 with _|⟨a, L2⟩, { injections, subst_vars, simp at h, cc }, { refine @reduce.not L1 L2 L3 x' b' _, injection H with _ H, rw [r, H], refl } end /-- The second theorem that characterises the function `reduce`: the maximal reduction of a word only reduces to itself. -/ theorem reduce.min (H : red (reduce L₁) L₂) : reduce L₁ = L₂ := begin induction H with L1 L' L2 H1 H2 ih, { refl }, { cases H1 with L4 L5 x b, exact reduce.not H2 } end /-- `reduce` is idempotent, i.e. the maximal reduction of the maximal reduction of a word is the maximal reduction of the word. -/ theorem reduce.idem : reduce (reduce L) = reduce L := eq.symm $ reduce.min reduce.red theorem reduce.step.eq (H : red.step L₁ L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (reduce.red.head H) in (reduce.min HR13).trans (reduce.min HR23).symm /-- If a word reduces to another word, then they have a common maximal reduction. -/ theorem reduce.eq_of_red (H : red L₁ L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (red.trans H reduce.red) in (reduce.min HR13).trans (reduce.min HR23).symm /-- If two words correspond to the same element in the free group, then they have a common maximal reduction. This is the proof that the function that sends an element of the free group to its maximal reduction is well-defined. -/ theorem reduce.sound (H : mk L₁ = mk L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, H13, H23⟩ := red.exact.1 H in (reduce.eq_of_red H13).trans (reduce.eq_of_red H23).symm /-- If two words have a common maximal reduction, then they correspond to the same element in the free group. -/ theorem reduce.exact (H : reduce L₁ = reduce L₂) : mk L₁ = mk L₂ := red.exact.2 ⟨reduce L₂, H ▸ reduce.red, reduce.red⟩ /-- A word and its maximal reduction correspond to the same element of the free group. -/ theorem reduce.self : mk (reduce L) = mk L := reduce.exact reduce.idem /-- If words `w₁ w₂` are such that `w₁` reduces to `w₂`, then `w₂` reduces to the maximal reduction of `w₁`. -/ theorem reduce.rev (H : red L₁ L₂) : red L₂ (reduce L₁) := (reduce.eq_of_red H).symm ▸ reduce.red /-- The function that sends an element of the free group to its maximal reduction. -/ def to_word : free_group α → list (α × bool) := quot.lift reduce $ λ L₁ L₂ H, reduce.step.eq H lemma to_word.mk : ∀{x : free_group α}, mk (to_word x) = x := by rintros ⟨L⟩; exact reduce.self lemma to_word.inj : ∀(x y : free_group α), to_word x = to_word y → x = y := by rintros ⟨L₁⟩ ⟨L₂⟩; exact reduce.exact /-- Constructive Church-Rosser theorem (compare `church_rosser`). -/ def reduce.church_rosser (H12 : red L₁ L₂) (H13 : red L₁ L₃) : { L₄ // red L₂ L₄ ∧ red L₃ L₄ } := ⟨reduce L₁, reduce.rev H12, reduce.rev H13⟩ instance : decidable_eq (free_group α) := function.injective.decidable_eq to_word.inj instance red.decidable_rel : decidable_rel (@red α) | [] [] := is_true red.refl | [] (hd2::tl2) := is_false $ λ H, list.no_confusion (red.nil_iff.1 H) | ((x,b)::tl) [] := match red.decidable_rel tl [(x, bnot b)] with | is_true H := is_true $ red.trans (red.cons_cons H) $ (@red.step.bnot _ [] [] _ _).to_red | is_false H := is_false $ λ H2, H $ red.cons_nil_iff_singleton.1 H2 end | ((x1,b1)::tl1) ((x2,b2)::tl2) := if h : (x1, b1) = (x2, b2) then match red.decidable_rel tl1 tl2 with | is_true H := is_true $ h ▸ red.cons_cons H | is_false H := is_false $ λ H2, H $ h ▸ (red.cons_cons_iff _).1 $ H2 end else match red.decidable_rel tl1 ((x1,bnot b1)::(x2,b2)::tl2) with | is_true H := is_true $ (red.cons_cons H).tail red.step.cons_bnot | is_false H := is_false $ λ H2, H $ red.inv_of_red_of_ne h H2 end /-- A list containing every word that `w₁` reduces to. -/ def red.enum (L₁ : list (α × bool)) : list (list (α × bool)) := list.filter (λ L₂, red L₁ L₂) (list.sublists L₁) theorem red.enum.sound (H : L₂ ∈ red.enum L₁) : red L₁ L₂ := list.of_mem_filter H theorem red.enum.complete (H : red L₁ L₂) : L₂ ∈ red.enum L₁ := list.mem_filter_of_mem (list.mem_sublists.2 $ red.sublist H) H instance : fintype { L₂ // red L₁ L₂ } := fintype.subtype (list.to_finset $ red.enum L₁) $ λ L₂, ⟨λ H, red.enum.sound $ list.mem_to_finset.1 H, λ H, list.mem_to_finset.2 $ red.enum.complete H⟩ end reduce end free_group
747d875ec4701216b8dd04aebd0b650303ecc6d0
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/lie/matrix.lean
635b07e9bce2e977d7a7f31866fe62287c4c6fa3
[ "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,472
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.of_associative import linear_algebra.matrix.reindex import linear_algebra.matrix.to_linear_equiv /-! # Lie algebras of matrices An important class of Lie algebras are those arising from the associative algebra structure on square matrices over a commutative ring. This file provides some very basic definitions whose primary value stems from their utility when constructing the classical Lie algebras using matrices. ## Main definitions * `lie_equiv_matrix'` * `matrix.lie_conj` * `matrix.reindex_lie_equiv` ## Tags lie algebra, matrix -/ universes u v w w₁ w₂ section matrices open_locale matrix variables {R : Type u} [comm_ring R] variables {n : Type w} [decidable_eq n] [fintype n] /-- The natural equivalence between linear endomorphisms of finite free modules and square matrices is compatible with the Lie algebra structures. -/ def lie_equiv_matrix' : module.End R (n → R) ≃ₗ⁅R⁆ matrix n n R := { map_lie' := λ T S, begin let f := @linear_map.to_matrix' R _ n n _ _ _, change f (T.comp S - S.comp T) = (f T) * (f S) - (f S) * (f T), have h : ∀ (T S : module.End R _), f (T.comp S) = (f T) ⬝ (f S) := linear_map.to_matrix'_comp, rw [linear_equiv.map_sub, h, h, matrix.mul_eq_mul, matrix.mul_eq_mul], end, ..linear_map.to_matrix' } @[simp] lemma lie_equiv_matrix'_apply (f : module.End R (n → R)) : lie_equiv_matrix' f = f.to_matrix' := rfl @[simp] lemma lie_equiv_matrix'_symm_apply (A : matrix n n R) : (@lie_equiv_matrix' R _ n _ _).symm A = A.to_lin' := rfl /-- An invertible matrix induces a Lie algebra equivalence from the space of matrices to itself. -/ noncomputable def matrix.lie_conj (P : matrix n n R) (h : is_unit P) : matrix n n R ≃ₗ⁅R⁆ matrix n n R := ((@lie_equiv_matrix' R _ n _ _).symm.trans (P.to_linear_equiv' h).lie_conj).trans lie_equiv_matrix' @[simp] lemma matrix.lie_conj_apply (P A : matrix n n R) (h : is_unit P) : P.lie_conj h A = P ⬝ A ⬝ P⁻¹ := by simp [linear_equiv.conj_apply, matrix.lie_conj, linear_map.to_matrix'_comp, linear_map.to_matrix'_to_lin'] @[simp] lemma matrix.lie_conj_symm_apply (P A : matrix n n R) (h : is_unit P) : (P.lie_conj h).symm A = P⁻¹ ⬝ A ⬝ P := by simp [linear_equiv.symm_conj_apply, matrix.lie_conj, linear_map.to_matrix'_comp, linear_map.to_matrix'_to_lin'] /-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent types, `matrix.reindex`, is an equivalence of Lie algebras. -/ def matrix.reindex_lie_equiv {m : Type w₁} [decidable_eq m] [fintype m] (e : n ≃ m) : matrix n n R ≃ₗ⁅R⁆ matrix m m R := { to_fun := matrix.reindex e e, map_lie' := λ M N, by simp only [lie_ring.of_associative_ring_bracket, matrix.reindex_apply, ←matrix.minor_mul_equiv _ _ _ _, matrix.mul_eq_mul, matrix.minor_sub, pi.sub_apply], ..(matrix.reindex_linear_equiv e e) } @[simp] lemma matrix.reindex_lie_equiv_apply {m : Type w₁} [decidable_eq m] [fintype m] (e : n ≃ m) (M : matrix n n R) : matrix.reindex_lie_equiv e M = matrix.reindex e e M := rfl @[simp] lemma matrix.reindex_lie_equiv_symm {m : Type w₁} [decidable_eq m] [fintype m] (e : n ≃ m) : (matrix.reindex_lie_equiv e : _ ≃ₗ⁅R⁆ _).symm = matrix.reindex_lie_equiv e.symm := rfl end matrices
7ec18b770e94dc5f05e01ea1197d25bdeb6c17b0
d642a6b1261b2cbe691e53561ac777b924751b63
/src/topology/metric_space/emetric_space.lean
98341c1736d4d449cabd3afe6b662620a6079326
[ "Apache-2.0" ]
permissive
cipher1024/mathlib
fee56b9954e969721715e45fea8bcb95f9dc03fe
d077887141000fefa5a264e30fa57520e9f03522
refs/heads/master
1,651,806,490,504
1,573,508,694,000
1,573,508,694,000
107,216,176
0
0
Apache-2.0
1,647,363,136,000
1,508,213,014,000
Lean
UTF-8
Lean
false
false
32,036
lean
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Extended metric spaces. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel This file is devoted to the definition and study of `emetric_spaces`, i.e., metric spaces in which the distance is allowed to take the value ∞. This extended distance is called `edist`, and takes values in `ennreal`. Many definitions and theorems expected on emetric spaces are already introduced on uniform spaces and topological spaces. For example: open and closed sets, compactness, completeness, continuity and uniform continuity The class `emetric_space` therefore extends `uniform_space` (and `topological_space`). -/ import data.real.nnreal data.real.ennreal import topology.uniform_space.separation topology.uniform_space.uniform_embedding topology.uniform_space.pi import topology.bases open lattice set filter classical noncomputable theory open_locale uniformity universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- Characterizing uniformities associated to a (generalized) distance function `D` in terms of the elements of the uniformity. -/ theorem uniformity_dist_of_mem_uniformity [linear_order β] {U : filter (α × α)} (z : β) (D : α → α → β) (H : ∀ s, s ∈ U ↔ ∃ε>z, ∀{a b:α}, D a b < ε → (a, b) ∈ s) : U = ⨅ ε>z, principal {p:α×α | D p.1 p.2 < ε} := le_antisymm (le_infi $ λ ε, le_infi $ λ ε0, le_principal_iff.2 $ (H _).2 ⟨ε, ε0, λ a b, id⟩) (λ r ur, let ⟨ε, ε0, h⟩ := (H _).1 ur in mem_infi_sets ε $ mem_infi_sets ε0 $ mem_principal_sets.2 $ λ ⟨a, b⟩, h) class has_edist (α : Type*) := (edist : α → α → ennreal) export has_edist (edist) /- Design note: one could define an `emetric_space` just by giving `edist`, and then derive an instance of `uniform_space` by taking the natural uniform structure associated to the distance. This creates diamonds problem for products, as the uniform structure on the product of two emetric spaces could be obtained first by obtaining two uniform spaces and then taking their products, or by taking the product of the emetric spaces and then the associated uniform structure. The two uniform structure we have just described are equal, but not defeq, which creates a lot of problem. The idea is to add, in the very definition of an `emetric_space`, a uniform structure with a uniformity which equal to the one given by the distance, but maybe not defeq. And the instance from `emetric_space` to `uniform_space` uses this uniformity. In this way, when we create the product of emetric spaces, we put in the product the uniformity corresponding to the product of the uniformities. There is one more proof obligation, that this product uniformity is equal to the uniformity corresponding to the product metric. But the diamond problem disappears. The same trick is used in the definition of a metric space, where one stores as well a uniform structure and an edistance. -/ /-- Creating a uniform space from an extended distance. -/ def uniform_space_of_edist (edist : α → α → ennreal) (edist_self : ∀ x : α, edist x x = 0) (edist_comm : ∀ x y : α, edist x y = edist y x) (edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z) : uniform_space α := uniform_space.of_core { uniformity := (⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε}), refl := le_infi $ assume ε, le_infi $ by simp [set.subset_def, id_rel, edist_self, (>)] {contextual := tt}, comp := le_infi $ assume ε, le_infi $ assume h, have (2 : ennreal) = (2 : ℕ) := by simp, have A : 0 < ε / 2 := ennreal.div_pos_iff.2 ⟨ne_of_gt h, this ▸ ennreal.nat_ne_top 2⟩, lift'_le (mem_infi_sets (ε / 2) $ mem_infi_sets A (subset.refl _)) $ have ∀ (a b c : α), edist a c < ε / 2 → edist c b < ε / 2 → edist a b < ε, from assume a b c hac hcb, calc edist a b ≤ edist a c + edist c b : edist_triangle _ _ _ ... < ε / 2 + ε / 2 : ennreal.add_lt_add hac hcb ... = ε : by rw [ennreal.add_halves], by simpa [comp_rel], symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h, tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [edist_comm] } /-- Extended metric spaces, with an extended distance `edist` possibly taking the value ∞ Each emetric space induces a canonical `uniform_space` and hence a canonical `topological_space`. This is enforced in the type class definition, by extending the `uniform_space` structure. When instantiating an `emetric_space` structure, the uniformity fields are not necessary, they will be filled in by default. There is a default value for the uniformity, that can be substituted in cases of interest, for instance when instantiating an `emetric_space` structure on a product. Continuity of `edist` is finally proving in `topology.instances.ennreal` -/ class emetric_space (α : Type u) extends has_edist α : Type u := (edist_self : ∀ x : α, edist x x = 0) (eq_of_edist_eq_zero : ∀ {x y : α}, edist x y = 0 → x = y) (edist_comm : ∀ x y : α, edist x y = edist y x) (edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z) (to_uniform_space : uniform_space α := uniform_space_of_edist edist edist_self edist_comm edist_triangle) (uniformity_edist : 𝓤 α = ⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε} . control_laws_tac) /- emetric spaces are less common than metric spaces. Therefore, we work in a dedicated namespace, while notions associated to metric spaces are mostly in the root namespace. -/ variables [emetric_space α] instance emetric_space.to_uniform_space' : uniform_space α := emetric_space.to_uniform_space α export emetric_space (edist_self eq_of_edist_eq_zero edist_comm edist_triangle) attribute [simp] edist_self /-- Characterize the equality of points by the vanishing of their extended distance -/ @[simp] theorem edist_eq_zero {x y : α} : edist x y = 0 ↔ x = y := iff.intro eq_of_edist_eq_zero (assume : x = y, this ▸ edist_self _) @[simp] theorem zero_eq_edist {x y : α} : 0 = edist x y ↔ x = y := iff.intro (assume h, eq_of_edist_eq_zero (h.symm)) (assume : x = y, this ▸ (edist_self _).symm) /-- Triangle inequality for the extended distance -/ theorem edist_triangle_left (x y z : α) : edist x y ≤ edist z x + edist z y := by rw edist_comm z; apply edist_triangle theorem edist_triangle_right (x y z : α) : edist x y ≤ edist x z + edist y z := by rw edist_comm y; apply edist_triangle lemma edist_triangle4 (x y z t : α) : edist x t ≤ edist x y + edist y z + edist z t := calc edist x t ≤ edist x z + edist z t : edist_triangle x z t ... ≤ (edist x y + edist y z) + edist z t : add_le_add_right' (edist_triangle x y z) /-- Two points coincide if their distance is `< ε` for all positive ε -/ theorem eq_of_forall_edist_le {x y : α} (h : ∀ε, ε > 0 → edist x y ≤ ε) : x = y := eq_of_edist_eq_zero (eq_of_le_of_forall_le_of_dense (by simp) h) /-- Reformulation of the uniform structure in terms of the extended distance -/ theorem uniformity_edist' : 𝓤 α = (⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε}) := emetric_space.uniformity_edist _ /-- Reformulation of the uniform structure in terms of the extended distance on a subtype -/ theorem uniformity_edist'' : 𝓤 α = (⨅ε:{ε:ennreal // ε>0}, principal {p:α×α | edist p.1 p.2 < ε.val}) := by simp [infi_subtype]; exact uniformity_edist' theorem uniformity_edist_nnreal : 𝓤 α = (⨅(ε:nnreal) (h : ε > 0), principal {p:α×α | edist p.1 p.2 < ε}) := begin rw [uniformity_edist', ennreal.infi_ennreal, inf_of_le_left], { congr, funext ε, refine infi_congr_Prop ennreal.coe_pos _, assume h, refl }, refine le_infi (assume h, infi_le_of_le 1 $ infi_le_of_le ennreal.zero_lt_one $ _), exact principal_mono.2 (assume p h, lt_of_lt_of_le h le_top) end /-- Characterization of the elements of the uniformity in terms of the extended distance -/ theorem mem_uniformity_edist {s : set (α×α)} : s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, edist a b < ε → (a, b) ∈ s) := begin rw [uniformity_edist'', mem_infi], simp [subset_def], exact assume ⟨r, hr⟩ ⟨p, hp⟩, ⟨⟨min r p, lt_min hr hp⟩, by simp [lt_min_iff, (≥)] {contextual := tt}⟩, exact ⟨⟨1, ennreal.zero_lt_one⟩⟩ end /-- Fixed size neighborhoods of the diagonal belong to the uniform structure -/ theorem edist_mem_uniformity {ε:ennreal} (ε0 : 0 < ε) : {p:α×α | edist p.1 p.2 < ε} ∈ 𝓤 α := mem_uniformity_edist.2 ⟨ε, ε0, λ a b, id⟩ namespace emetric /-- ε-δ characterization of uniform continuity on emetric spaces -/ theorem uniform_continuous_iff [emetric_space β] {f : α → β} : uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0, ∀{a b:α}, edist a b < δ → edist (f a) (f b) < ε := uniform_continuous_def.trans ⟨λ H ε ε0, mem_uniformity_edist.1 $ H _ $ edist_mem_uniformity ε0, λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru, ⟨δ, δ0, hδ⟩ := H _ ε0 in mem_uniformity_edist.2 ⟨δ, δ0, λ a b h, hε (hδ h)⟩⟩ /-- ε-δ characterization of uniform embeddings on emetric spaces -/ theorem uniform_embedding_iff [emetric_space β] {f : α → β} : uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧ ∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, edist (f a) (f b) < ε → edist a b < δ := uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl ⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (edist_mem_uniformity δ0), ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 tu in ⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩, λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_edist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in ⟨_, edist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩ /-- ε-δ characterization of Cauchy sequences on emetric spaces -/ protected lemma cauchy_iff {f : filter α} : cauchy f ↔ f ≠ ⊥ ∧ ∀ ε > 0, ∃ t ∈ f, ∀ x y ∈ t, edist x y < ε := cauchy_iff.trans $ and_congr iff.rfl ⟨λ H ε ε0, let ⟨t, tf, ts⟩ := H _ (edist_mem_uniformity ε0) in ⟨t, tf, λ x y xt yt, @ts (x, y) ⟨xt, yt⟩⟩, λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru, ⟨t, tf, h⟩ := H ε ε0 in ⟨t, tf, λ ⟨x, y⟩ ⟨hx, hy⟩, hε (h x y hx hy)⟩⟩ end emetric open emetric /-- An emetric space is separated -/ instance to_separated : separated α := separated_def.2 $ λ x y h, eq_of_forall_edist_le $ λ ε ε0, le_of_lt (h _ (edist_mem_uniformity ε0)) /-- Auxiliary function to replace the uniformity on an emetric space with a uniformity which is equal to the original one, but maybe not defeq. This is useful if one wants to construct an emetric space with a specified uniformity. -/ def emetric_space.replace_uniformity {α} [U : uniform_space α] (m : emetric_space α) (H : @uniformity _ U = @uniformity _ (emetric_space.to_uniform_space α)) : emetric_space α := { edist := @edist _ m.to_has_edist, edist_self := edist_self, eq_of_edist_eq_zero := @eq_of_edist_eq_zero _ _, edist_comm := edist_comm, edist_triangle := edist_triangle, to_uniform_space := U, uniformity_edist := H.trans (@emetric_space.uniformity_edist α _) } /-- The extended metric induced by an injective function taking values in an emetric space. -/ def emetric_space.induced {α β} (f : α → β) (hf : function.injective f) (m : emetric_space β) : emetric_space α := { edist := λ x y, edist (f x) (f y), edist_self := λ x, edist_self _, eq_of_edist_eq_zero := λ x y h, hf (edist_eq_zero.1 h), edist_comm := λ x y, edist_comm _ _, edist_triangle := λ x y z, edist_triangle _ _ _, to_uniform_space := uniform_space.comap f m.to_uniform_space, uniformity_edist := begin apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, edist (f x) (f y)), refine λ s, mem_comap_sets.trans _, split; intro H, { rcases H with ⟨r, ru, rs⟩, rcases mem_uniformity_edist.1 ru with ⟨ε, ε0, hε⟩, refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h }, { rcases H with ⟨ε, ε0, hε⟩, exact ⟨_, edist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ } end } /-- Emetric space instance on subsets of emetric spaces -/ instance {α : Type*} {p : α → Prop} [t : emetric_space α] : emetric_space (subtype p) := t.induced subtype.val (λ x y, subtype.eq) /-- The extended distance on a subset of an emetric space is the restriction of the original distance, by definition -/ theorem subtype.edist_eq {p : α → Prop} (x y : subtype p) : edist x y = edist x.1 y.1 := rfl /-- The product of two emetric spaces, with the max distance, is an extended metric spaces. We make sure that the uniform structure thus constructed is the one corresponding to the product of uniform spaces, to avoid diamond problems. -/ instance prod.emetric_space_max [emetric_space β] : emetric_space (α × β) := { edist := λ x y, max (edist x.1 y.1) (edist x.2 y.2), edist_self := λ x, by simp, eq_of_edist_eq_zero := λ x y h, begin cases max_le_iff.1 (le_of_eq h) with h₁ h₂, have A : x.fst = y.fst := eq_of_edist_eq_zero (by simpa using h₁), have B : x.snd = y.snd := eq_of_edist_eq_zero (by simpa using h₂), exact prod.ext_iff.2 ⟨A, B⟩ end, edist_comm := λ x y, by simp [edist_comm], edist_triangle := λ x y z, max_le (le_trans (edist_triangle _ _ _) (add_le_add' (le_max_left _ _) (le_max_left _ _))) (le_trans (edist_triangle _ _ _) (add_le_add' (le_max_right _ _) (le_max_right _ _))), uniformity_edist := begin refine uniformity_prod.trans _, simp [emetric_space.uniformity_edist, comap_infi], rw ← infi_inf_eq, congr, funext, rw ← infi_inf_eq, congr, funext, simp [inf_principal, ext_iff, max_lt_iff] end, to_uniform_space := prod.uniform_space } section pi open finset variables {π : β → Type*} [fintype β] /-- The product of a finite number of emetric spaces, with the max distance, is still an emetric space. This construction would also work for infinite products, but it would not give rise to the product topology. Hence, we only formalize it in the good situation of finitely many spaces. -/ instance emetric_space_pi [∀b, emetric_space (π b)] : emetric_space (Πb, π b) := { edist := λ f g, finset.sup univ (λb, edist (f b) (g b)), edist_self := assume f, bot_unique $ finset.sup_le $ by simp, edist_comm := assume f g, by unfold edist; congr; funext a; exact edist_comm _ _, edist_triangle := assume f g h, begin simp only [finset.sup_le_iff], assume b hb, exact le_trans (edist_triangle _ (g b) _) (add_le_add' (le_sup hb) (le_sup hb)) end, eq_of_edist_eq_zero := assume f g eq0, begin have eq1 : sup univ (λ (b : β), edist (f b) (g b)) ≤ 0 := le_of_eq eq0, simp only [finset.sup_le_iff] at eq1, exact (funext $ assume b, eq_of_edist_eq_zero $ bot_unique $ eq1 b $ mem_univ b), end, to_uniform_space := Pi.uniform_space _, uniformity_edist := begin simp only [Pi.uniformity, emetric_space.uniformity_edist, comap_infi, gt_iff_lt, preimage_set_of_eq, comap_principal], rw infi_comm, congr, funext ε, rw infi_comm, congr, funext εpos, change 0 < ε at εpos, simp [ext_iff, εpos] end } end pi namespace emetric variables {x y z : α} {ε ε₁ ε₂ : ennreal} {s : set α} /-- `emetric.ball x ε` is the set of all points `y` with `edist y x < ε` -/ def ball (x : α) (ε : ennreal) : set α := {y | edist y x < ε} @[simp] theorem mem_ball : y ∈ ball x ε ↔ edist y x < ε := iff.rfl theorem mem_ball' : y ∈ ball x ε ↔ edist x y < ε := by rw edist_comm; refl /-- `emetric.closed_ball x ε` is the set of all points `y` with `edist y x ≤ ε` -/ def closed_ball (x : α) (ε : ennreal) := {y | edist y x ≤ ε} @[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ edist y x ≤ ε := iff.rfl theorem ball_subset_closed_ball : ball x ε ⊆ closed_ball x ε := assume y, by simp; intros h; apply le_of_lt h theorem pos_of_mem_ball (hy : y ∈ ball x ε) : 0 < ε := lt_of_le_of_lt (zero_le _) hy theorem mem_ball_self (h : 0 < ε) : x ∈ ball x ε := show edist x x < ε, by rw edist_self; assumption theorem mem_closed_ball_self : x ∈ closed_ball x ε := show edist x x ≤ ε, by rw edist_self; exact bot_le theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε := by simp [edist_comm] theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ := λ y (yx : _ < ε₁), lt_of_lt_of_le yx h theorem closed_ball_subset_closed_ball (h : ε₁ ≤ ε₂) : closed_ball x ε₁ ⊆ closed_ball x ε₂ := λ y (yx : _ ≤ ε₁), le_trans yx h theorem ball_disjoint (h : ε₁ + ε₂ ≤ edist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ := eq_empty_iff_forall_not_mem.2 $ λ z ⟨h₁, h₂⟩, not_lt_of_le (edist_triangle_left x y z) (lt_of_lt_of_le (ennreal.add_lt_add h₁ h₂) h) theorem ball_subset (h : edist x y + ε₁ ≤ ε₂) (h' : edist x y < ⊤) : ball x ε₁ ⊆ ball y ε₂ := λ z zx, calc edist z y ≤ edist z x + edist x y : edist_triangle _ _ _ ... = edist x y + edist z x : add_comm _ _ ... < edist x y + ε₁ : (ennreal.add_lt_add_iff_left h').2 zx ... ≤ ε₂ : h theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε := begin have : 0 < ε - edist y x := by simpa using h, refine ⟨ε - edist y x, this, ball_subset _ _⟩, { rw ennreal.add_sub_cancel_of_le (le_of_lt h), apply le_refl _}, { have : edist y x ≠ ⊤ := lattice.ne_top_of_lt h, apply lt_top_iff_ne_top.2 this } end theorem ball_eq_empty_iff : ball x ε = ∅ ↔ ε = 0 := eq_empty_iff_forall_not_mem.trans ⟨λh, le_bot_iff.1 (le_of_not_gt (λ ε0, h _ (mem_ball_self ε0))), λε0 y h, not_lt_of_le (le_of_eq ε0) (pos_of_mem_ball h)⟩ theorem nhds_eq : nhds x = (⨅ε:{ε:ennreal // ε>0}, principal (ball x ε.val)) := begin rw [nhds_eq_uniformity, uniformity_edist'', lift'_infi], { apply congr_arg, funext ε, rw [lift'_principal], { simp [ball, edist_comm] }, { exact monotone_preimage } }, { exact ⟨⟨1, ennreal.zero_lt_one⟩⟩ }, { intros, refl } end theorem mem_nhds_iff : s ∈ nhds x ↔ ∃ε>0, ball x ε ⊆ s := begin rw [nhds_eq, mem_infi], { simp }, { intros y z, cases y with y hy, cases z with z hz, refine ⟨⟨min y z, lt_min hy hz⟩, _⟩, simp [ball_subset_ball, min_le_left, min_le_right, (≥)] }, { exact ⟨⟨1, ennreal.zero_lt_one⟩⟩ } end theorem is_open_iff : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s := by simp [is_open_iff_nhds, mem_nhds_iff] theorem is_open_ball : is_open (ball x ε) := is_open_iff.2 $ λ y, exists_ball_subset_ball theorem ball_mem_nhds (x : α) {ε : ennreal} (ε0 : 0 < ε) : ball x ε ∈ nhds x := mem_nhds_sets is_open_ball (mem_ball_self ε0) /-- ε-characterization of the closure in emetric spaces -/ theorem mem_closure_iff' : x ∈ closure s ↔ ∀ε>0, ∃y ∈ s, edist x y < ε := ⟨begin intros hx ε hε, have A : ball x ε ∩ s ≠ ∅ := mem_closure_iff.1 hx _ is_open_ball (mem_ball_self hε), cases ne_empty_iff_exists_mem.1 A with y hy, simp, exact ⟨y, ⟨hy.2, by have B := hy.1; simpa [mem_ball'] using B⟩⟩ end, begin intros H, apply mem_closure_iff.2, intros o ho xo, rcases is_open_iff.1 ho x xo with ⟨ε, ⟨εpos, hε⟩⟩, rcases H ε εpos with ⟨y, ⟨ys, ydist⟩⟩, have B : y ∈ o ∩ s := ⟨hε (by simpa [edist_comm]), ys⟩, apply ne_empty_of_mem B end⟩ theorem tendsto_nhds {f : filter β} {u : β → α} {a : α} : tendsto u f (nhds a) ↔ ∀ ε > 0, ∃ n ∈ f, ∀x ∈ n, edist (u x) a < ε := ⟨λ H ε ε0, ⟨u⁻¹' (ball a ε), H (ball_mem_nhds _ ε0), by simp⟩, λ H s hs, let ⟨ε, ε0, hε⟩ := mem_nhds_iff.1 hs, ⟨δ, δ0, hδ⟩ := H _ ε0 in f.sets_of_superset δ0 (λx xδ, hε (hδ x xδ))⟩ theorem tendsto_at_top [inhabited β] [semilattice_sup β] (u : β → α) {a : α} : tendsto u at_top (nhds a) ↔ ∀ε>0, ∃N, ∀n≥N, edist (u n) a < ε := begin rw tendsto_nhds, apply forall_congr, intro ε, apply forall_congr, intro hε, simp, exact ⟨λ ⟨s, ⟨N, hN⟩, hs⟩, ⟨N, λn hn, hs _ (hN _ hn)⟩, λ ⟨N, hN⟩, ⟨{n | n ≥ N}, ⟨⟨N, by simp⟩, hN⟩⟩⟩, end /-- In an emetric space, Cauchy sequences are characterized by the fact that, eventually, the edistance between its elements is arbitrarily small -/ theorem cauchy_seq_iff [inhabited β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ε>0, ∃N, ∀m n≥N, edist (u n) (u m) < ε := begin simp only [cauchy_seq, emetric.cauchy_iff, true_and, exists_prop, filter.mem_at_top_sets, filter.at_top_ne_bot, filter.mem_map, ne.def, filter.map_eq_bot_iff, not_false_iff, set.mem_set_of_eq], split, { intros H ε εpos, rcases H ε εpos with ⟨t, ⟨N, hN⟩, ht⟩, exact ⟨N, λm n hm hn, ht _ _ (hN _ hn) (hN _ hm)⟩ }, { intros H ε εpos, rcases H (ε/2) (ennreal.half_pos εpos) with ⟨N, hN⟩, existsi ball (u N) (ε/2), split, { exact ⟨N, λx hx, hN _ _ (le_refl N) hx⟩ }, { exact λx y hx hy, calc edist x y ≤ edist x (u N) + edist y (u N) : edist_triangle_right _ _ _ ... < ε/2 + ε/2 : ennreal.add_lt_add hx hy ... = ε : ennreal.add_halves _ } } end /-- A variation around the emetric characterization of Cauchy sequences -/ theorem cauchy_seq_iff' [inhabited β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ε>(0 : ennreal), ∃N, ∀n≥N, edist (u n) (u N) < ε := begin rw cauchy_seq_iff, split, { intros H ε εpos, rcases H ε εpos with ⟨N, hN⟩, exact ⟨N, λn hn, hN _ _ (le_refl N) hn⟩ }, { intros H ε εpos, rcases H (ε/2) (ennreal.half_pos εpos) with ⟨N, hN⟩, exact ⟨N, λ m n hm hn, calc edist (u n) (u m) ≤ edist (u n) (u N) + edist (u m) (u N) : edist_triangle_right _ _ _ ... < ε/2 + ε/2 : ennreal.add_lt_add (hN _ hn) (hN _ hm) ... = ε : ennreal.add_halves _⟩ } end theorem totally_bounded_iff {s : set α} : totally_bounded s ↔ ∀ ε > 0, ∃t : set α, finite t ∧ s ⊆ ⋃y∈t, ball y ε := ⟨λ H ε ε0, H _ (edist_mem_uniformity ε0), λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru, ⟨t, ft, h⟩ := H ε ε0 in ⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩ theorem totally_bounded_iff' {s : set α} : totally_bounded s ↔ ∀ ε > 0, ∃t⊆s, finite t ∧ s ⊆ ⋃y∈t, ball y ε := ⟨λ H ε ε0, (totally_bounded_iff_subset.1 H) _ (edist_mem_uniformity ε0), λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru, ⟨t, _, ft, h⟩ := H ε ε0 in ⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩ section compact /-- A compact set in an emetric space is separable, i.e., it is the closure of a countable set -/ lemma countable_closure_of_compact {α : Type u} [emetric_space α] {s : set α} (hs : compact s) : ∃ t ⊆ s, (countable t ∧ s = closure t) := begin have A : ∀ (e:ennreal), e > 0 → ∃ t ⊆ s, (finite t ∧ s ⊆ (⋃x∈t, ball x e)) := totally_bounded_iff'.1 (compact_iff_totally_bounded_complete.1 hs).1, -- assume e, finite_cover_balls_of_compact hs, have B : ∀ (e:ennreal), ∃ t ⊆ s, finite t ∧ (e > 0 → s ⊆ (⋃x∈t, ball x e)), { intro e, cases le_or_gt e 0 with h, { exact ⟨∅, by finish⟩ }, { rcases A e h with ⟨s, ⟨finite_s, closure_s⟩⟩, existsi s, finish }}, /-The desired countable set is obtained by taking for each `n` the centers of a finite cover by balls of radius `1/n`, and then the union over `n`. -/ choose T T_in_s finite_T using B, let t := ⋃n:ℕ, T n⁻¹, have T₁ : t ⊆ s := begin apply Union_subset, assume n, apply T_in_s end, have T₂ : countable t := by finish [countable_Union, countable_finite], have T₃ : s ⊆ closure t, { intros x x_in_s, apply mem_closure_iff'.2, intros ε εpos, rcases ennreal.exists_inv_nat_lt (bot_lt_iff_ne_bot.1 εpos) with ⟨n, hn⟩, have inv_n_pos : (0 : ennreal) < (n : ℕ)⁻¹ := by simp [ennreal.bot_lt_iff_ne_bot], have C : x ∈ (⋃y∈ T (n : ℕ)⁻¹, ball y (n : ℕ)⁻¹) := mem_of_mem_of_subset x_in_s ((finite_T (n : ℕ)⁻¹).2 inv_n_pos), rcases mem_Union.1 C with ⟨y, _, ⟨y_in_T, rfl⟩, Dxy⟩, simp at Dxy, -- Dxy : edist x y < 1 / ↑n have : y ∈ t := mem_of_mem_of_subset y_in_T (by apply subset_Union (λ (n:ℕ), T (n : ℕ)⁻¹)), have : edist x y < ε := lt_trans Dxy hn, exact ⟨y, ‹y ∈ t›, ‹edist x y < ε›⟩ }, have T₄ : closure t ⊆ s := calc closure t ⊆ closure s : closure_mono T₁ ... = s : closure_eq_of_is_closed (closed_of_compact _ hs), exact ⟨t, ⟨T₁, T₂, subset.antisymm T₃ T₄⟩⟩ end end compact section first_countable instance (α : Type u) [emetric_space α] : topological_space.first_countable_topology α := ⟨assume a, ⟨⋃ i:ℕ, {ball a i⁻¹}, countable_Union $ assume n, countable_singleton _, suffices (⨅ i:{ i : ennreal // i > 0}, principal (ball a i)) = ⨅ (n : ℕ), principal (ball a n⁻¹), by simpa [nhds_eq, @infi_comm _ _ ℕ], begin apply le_antisymm, { refine le_infi (assume n, infi_le_of_le _ _), exact ⟨n⁻¹, by apply bot_lt_iff_ne_bot.2; simp⟩, exact le_refl _ }, refine le_infi (assume ε, _), rcases ennreal.exists_inv_nat_lt (bot_lt_iff_ne_bot.1 ε.2) with ⟨n, εn⟩, exact infi_le_of_le n (principal_mono.2 $ ball_subset_ball $ le_of_lt εn) end⟩⟩ end first_countable section second_countable open topological_space /-- A separable emetric space is second countable: one obtains a countable basis by taking the balls centered at points in a dense subset, and with rational radii. We do not register this as an instance, as there is already an instance going in the other direction from second countable spaces to separable spaces, and we want to avoid loops. -/ lemma second_countable_of_separable (α : Type u) [emetric_space α] [separable_space α] : second_countable_topology α := let ⟨S, ⟨S_countable, S_dense⟩⟩ := separable_space.exists_countable_closure_eq_univ α in ⟨⟨⋃x ∈ S, ⋃ (n : nat), {ball x (n⁻¹)}, ⟨show countable ⋃x ∈ S, ⋃ (n : nat), {ball x (n⁻¹)}, { apply countable_bUnion S_countable, intros a aS, apply countable_Union, simp }, show uniform_space.to_topological_space α = generate_from (⋃x ∈ S, ⋃ (n : nat), {ball x (n⁻¹)}), { have A : ∀ (u : set α), (u ∈ ⋃x ∈ S, ⋃ (n : nat), ({ball x ((n : ennreal)⁻¹)} : set (set α))) → is_open u, { simp only [and_imp, exists_prop, set.mem_Union, set.mem_singleton_iff, exists_imp_distrib], intros u x hx i u_ball, rw [u_ball], exact is_open_ball }, have B : is_topological_basis (⋃x ∈ S, ⋃ (n : nat), ({ball x (n⁻¹)} : set (set α))), { refine is_topological_basis_of_open_of_nhds A (λa u au open_u, _), rcases is_open_iff.1 open_u a au with ⟨ε, εpos, εball⟩, have : ε / 2 > 0 := ennreal.half_pos εpos, /- The ball `ball a ε` is included in `u`. We need to find one of our balls `ball x (n⁻¹)` containing `a` and contained in `ball a ε`. For this, we take `n` larger than `2/ε`, and then `x` in `S` at distance at most `n⁻¹` of `a` -/ rcases ennreal.exists_inv_nat_lt (bot_lt_iff_ne_bot.1 (ennreal.half_pos εpos)) with ⟨n, εn⟩, have : (0 : ennreal) < n⁻¹ := by simp [ennreal.bot_lt_iff_ne_bot], have : (a : α) ∈ closure (S : set α) := by rw [S_dense]; simp, rcases mem_closure_iff'.1 this _ ‹(0 : ennreal) < n⁻¹› with ⟨x, xS, xdist⟩, existsi ball x (↑n)⁻¹, have I : ball x (n⁻¹) ⊆ ball a ε := λy ydist, calc edist y a = edist a y : edist_comm _ _ ... ≤ edist a x + edist y x : edist_triangle_right _ _ _ ... < n⁻¹ + n⁻¹ : ennreal.add_lt_add xdist ydist ... < ε/2 + ε/2 : ennreal.add_lt_add εn εn ... = ε : ennreal.add_halves _, simp only [emetric.mem_ball, exists_prop, set.mem_Union, set.mem_singleton_iff], exact ⟨⟨x, ⟨xS, ⟨n, rfl⟩⟩⟩, ⟨by simpa, subset.trans I εball⟩⟩ }, exact B.2.2 }⟩⟩⟩ end second_countable section diam /-- The diameter of a set in an emetric space, named `emetric.diam` -/ def diam (s : set α) := Sup ((λp : α × α, edist p.1 p.2) '' (set.prod s s)) /-- If two points belong to some set, their edistance is bounded by the diameter of the set -/ lemma edist_le_diam_of_mem (hx : x ∈ s) (hy : y ∈ s) : edist x y ≤ diam s := le_Sup ((mem_image _ _ _).2 ⟨(⟨x, y⟩ : α × α), by simp [hx, hy]⟩) /-- If the distance between any two points in a set is bounded by some constant, this constant bounds the diameter. -/ lemma diam_le_of_forall_edist_le {d : ennreal} (h : ∀x y ∈ s, edist x y ≤ d) : diam s ≤ d := begin apply Sup_le _, simp only [and_imp, set.mem_image, set.mem_prod, exists_imp_distrib, prod.exists], assume b x y xs ys dxy, rw ← dxy, exact h x y xs ys end /-- The diameter of the empty set vanishes -/ @[simp] lemma diam_empty : diam (∅ : set α) = 0 := by simp [diam] /-- The diameter of a singleton vanishes -/ @[simp] lemma diam_singleton : diam ({x} : set α) = 0 := by simp [diam] /-- The diameter is monotonous with respect to inclusion -/ lemma diam_mono {s t : set α} (h : s ⊆ t) : diam s ≤ diam t := begin refine Sup_le_Sup (λp hp, _), simp only [set.mem_image, set.mem_prod, prod.exists] at hp, rcases hp with ⟨x, y, ⟨⟨xs, ys⟩, dxy⟩⟩, exact (mem_image _ _ _).2 ⟨⟨x, y⟩, ⟨⟨h xs, h ys⟩, dxy⟩⟩ end /-- The diameter of a union is controlled by the diameter of the sets, and the edistance between two points in the sets. -/ lemma diam_union {t : set α} (xs : x ∈ s) (yt : y ∈ t) : diam (s ∪ t) ≤ diam s + edist x y + diam t := begin have A : ∀a ∈ s, ∀b ∈ t, edist a b ≤ diam s + edist x y + diam t := λa ha b hb, calc edist a b ≤ edist a x + edist x y + edist y b : edist_triangle4 _ _ _ _ ... ≤ diam s + edist x y + diam t : add_le_add' (add_le_add' (edist_le_diam_of_mem ha xs) (le_refl _)) (edist_le_diam_of_mem yt hb), refine diam_le_of_forall_edist_le (λa b ha hb, _), cases (mem_union _ _ _).1 ha with h'a h'a; cases (mem_union _ _ _).1 hb with h'b h'b, { calc edist a b ≤ diam s : edist_le_diam_of_mem h'a h'b ... ≤ diam s + (edist x y + diam t) : le_add_right (le_refl _) ... = diam s + edist x y + diam t : by simp only [add_comm, eq_self_iff_true, add_left_comm] }, { exact A a h'a b h'b }, { have Z := A b h'b a h'a, rwa [edist_comm] at Z }, { calc edist a b ≤ diam t : edist_le_diam_of_mem h'a h'b ... ≤ (diam s + edist x y) + diam t : le_add_left (le_refl _) } end lemma diam_union' {t : set α} (h : s ∩ t ≠ ∅) : diam (s ∪ t) ≤ diam s + diam t := let ⟨x, ⟨xs, xt⟩⟩ := ne_empty_iff_exists_mem.1 h in by simpa using diam_union xs xt lemma diam_closed_ball {r : ennreal} : diam (closed_ball x r) ≤ 2 * r := diam_le_of_forall_edist_le $ λa b ha hb, calc edist a b ≤ edist a x + edist b x : edist_triangle_right _ _ _ ... ≤ r + r : add_le_add' ha hb ... = 2 * r : by simp [mul_two, mul_comm] lemma diam_ball {r : ennreal} : diam (ball x r) ≤ 2 * r := le_trans (diam_mono ball_subset_closed_ball) diam_closed_ball end diam end emetric --namespace
e59edf253c7e825f06fd6d5600ccdd1e8c034d88
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/simple_auto.lean
72e59046f99692c9f51f5755b6246fb8047158c0
[]
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,942
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel, Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.shapes.zero import Mathlib.category_theory.limits.shapes.kernels import Mathlib.category_theory.abelian.basic import Mathlib.PostPort universes v u l namespace Mathlib namespace category_theory /-- An object is simple if monomorphisms into it are (exclusively) either isomorphisms or zero. -/ -- This is a constructive definition, from which we can extract an inverse for `f` given `f ≠ 0`. -- We show below that although it contains data, it is a subsingleton. class simple {C : Type u} [category C] [limits.has_zero_morphisms C] (X : C) where mono_is_iso_equiv_nonzero : {Y : C} → (f : Y ⟶ X) → [_inst_3 : mono f] → is_iso f ≃ f ≠ 0 theorem simple.ext {C : Type u} [category C] [limits.has_zero_morphisms C] {X : C} {a : simple X} {b : simple X} : a = b := sorry protected instance subsingleton_simple {C : Type u} [category C] [limits.has_zero_morphisms C] (X : C) : subsingleton (simple X) := subsingleton.intro simple.ext /-- A nonzero monomorphism to a simple object is an isomorphism. -/ def is_iso_of_mono_of_nonzero {C : Type u} [category C] [limits.has_zero_morphisms C] {X : C} {Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : f ≠ 0) : is_iso f := coe_fn (equiv.symm (simple.mono_is_iso_equiv_nonzero f)) w theorem kernel_zero_of_nonzero_from_simple {C : Type u} [category C] [limits.has_zero_morphisms C] {X : C} {Y : C} [simple X] {f : X ⟶ Y} [limits.has_kernel f] (w : f ≠ 0) : limits.kernel.ι f = 0 := decidable.by_contradiction fun (h : ¬limits.kernel.ι f = 0) => w (limits.eq_zero_of_epi_kernel f) theorem mono_to_simple_zero_of_not_iso {C : Type u} [category C] [limits.has_zero_morphisms C] {X : C} {Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : is_iso f → False) : f = 0 := decidable.by_contradiction fun (h : ¬f = 0) => w (is_iso_of_mono_of_nonzero h) theorem id_nonzero {C : Type u} [category C] [limits.has_zero_morphisms C] (X : C) [simple X] : 𝟙 ≠ 0 := coe_fn (simple.mono_is_iso_equiv_nonzero 𝟙) (is_iso.id X) /-- We don't want the definition of 'simple' to include the zero object, so we check that here. -/ theorem zero_not_simple {C : Type u} [category C] [limits.has_zero_morphisms C] [limits.has_zero_object C] [simple 0] : False := coe_fn (simple.mono_is_iso_equiv_nonzero 0) (is_iso.mk 0) rfl -- We next make the dual arguments, but for this we must be in an abelian category. /-- In an abelian category, an object satisfying the dual of the definition of a simple object is simple. -/ def simple_of_cosimple {C : Type u} [category C] [abelian C] (X : C) (h : {Z : C} → (f : X ⟶ Z) → [_inst_3 : epi f] → is_iso f ≃ f ≠ 0) : simple X := simple.mk fun (Y : C) (f : Y ⟶ X) (I : mono f) => equiv_of_subsingleton_of_subsingleton sorry fun (hf : f ≠ 0) => abelian.is_iso_of_mono_of_epi f /-- A nonzero epimorphism from a simple object is an isomorphism. -/ def is_iso_of_epi_of_nonzero {C : Type u} [category C] [abelian C] {X : C} {Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : f ≠ 0) : is_iso f := abelian.is_iso_of_mono_of_epi f theorem cokernel_zero_of_nonzero_to_simple {C : Type u} [category C] [abelian C] {X : C} {Y : C} [simple Y] {f : X ⟶ Y} [limits.has_cokernel f] (w : f ≠ 0) : limits.cokernel.π f = 0 := decidable.by_contradiction fun (h : ¬limits.cokernel.π f = 0) => w (limits.eq_zero_of_mono_cokernel f) theorem epi_from_simple_zero_of_not_iso {C : Type u} [category C] [abelian C] {X : C} {Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : is_iso f → False) : f = 0 := decidable.by_contradiction fun (h : ¬f = 0) => w (is_iso_of_epi_of_nonzero h) end Mathlib
9ff9cfce7007cbba55f19ebe262bb3dd7ece144a
618003631150032a5676f229d13a079ac875ff77
/src/order/filter/basic.lean
9153d7320a3a5df07ac7d1e2443da5e9592982c7
[ "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
112,099
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 -/ import order.zorn import order.copy import data.set.finite /-! # Theory of filters on sets ## Main definitions * `filter` : filters on a set; * `at_top`, `at_bot`, `cofinite`, `principal` : specific filters; * `map`, `comap`, `prod` : operations on filters; * `tendsto` : limit with respect to filters; * `eventually` : `f.eventually p` means `{x | p x} ∈ f`; * `frequently` : `f.frequently p` means `{x | ¬p x} ∉ f`. * `filter_upwards [h₁, ..., hₙ]` : takes a list of proofs `hᵢ : sᵢ ∈ f`, and replaces a goal `s ∈ f` with `∀ x, x ∈ s₁ → ... → x ∈ sₙ → x ∈ s`; Filters on a type `X` are sets of sets of `X` satisfying three conditions. They are mostly used to abstract two related kinds of ideas: * *limits*, including finite or infinite limits of sequences, finite or infinite limits of functions at a point or at infinity, etc... * *things happening eventually*, including things happening for large enough `n : ℕ`, or near enough a point `x`, or for close enough pairs of points, or things happening almost everywhere in the sense of measure theory. Dually, filters can also express the idea of *things happening often*: for arbitrarily large `n`, or at a point in any neighborhood of given a point etc... In this file, we define the type `filter X` of filters on `X`, and endow it with a complete lattice structure. This structure is lifted from the lattice structure on `set (set X)` using the Galois insertion which maps a filter to its elements in one direction, and an arbitrary set of sets to the smallest filter containing it in the other direction. We also prove `filter` is a monadic functor, with a push-forward operation `filter.map` and a pull-back operation `filter.comap` that form a Galois connections for the order on filters. Finally we describe a product operation `filter X → filter Y → filter (X × Y)`. The examples of filters appearing in the description of the two motivating ideas are: * `(at_top : filter ℕ)` : made of sets of `ℕ` containing `{n | n ≥ N}` for some `N` * `𝓝 x` : made of neighborhoods of `x` in a topological space (defined in topology.basic) * `𝓤 X` : made of entourages of a uniform space (those space are generalizations of metric spaces defined in topology.uniform_space.basic) * `μ.a_e` : made of sets whose complement has zero measure with respect to `μ` (defined in measure_theory.measure_space) The general notion of limit of a map with respect to filters on the source and target types is `filter.tendsto`. It is defined in terms of the order and the push-forward operation. The predicate "happening eventually" is `filter.eventually`, and "happening often" is `filter.frequently`, whose definitions are immediate after `filter` is defined (but they come rather late in this file in order to immediately relate them to the lattice structure). For instance, anticipating on topology.basic, the statement: "if a sequence `u` converges to some `x` and `u n` belongs to a set `M` for `n` large enough then `x` is in the closure of `M`" is formalized as: `tendsto u at_top (𝓝 x) → (∀ᶠ n in at_top, u n ∈ M) → x ∈ closure M`, which is a special case of `mem_closure_of_tendsto` from topology.basic. ## Notations * `∀ᶠ x in f, p x` : `f.eventually p`; * `∃ᶠ x in f, p x` : `f.frequently p`. * `f ×ᶠ g` : `filter.prod f g`, localized in `filter`. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] Important note: Bourbaki requires that a filter on `X` cannot contain all sets of `X`, which we do *not* require. This gives `filter X` better formal properties, in particular a bottom element `⊥` for its lattice structure, at the cost of including the assumption `f ≠ ⊥` in a number of lemmas and definitions. -/ open set universes u v w x y open_locale classical /-- A filter `F` on a type `α` is a collection of sets of `α` which contains the whole `α`, is upwards-closed, and is stable under intersection. We do not forbid this collection to be all sets of `α`. -/ structure filter (α : Type*) := (sets : set (set α)) (univ_sets : set.univ ∈ sets) (sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets) (inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets) /-- If `F` is a filter on `α`, and `U` a subset of `α` then we can write `U ∈ F` as on paper. -/ @[reducible] instance {α : Type*}: has_mem (set α) (filter α) := ⟨λ U F, U ∈ F.sets⟩ namespace filter variables {α : Type u} {f g : filter α} {s t : set α} lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g | ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl lemma filter_eq_iff : f = g ↔ f.sets = g.sets := ⟨congr_arg _, filter_eq⟩ protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g := by rw [filter_eq_iff, ext_iff] @[ext] protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g := filter.ext_iff.2 lemma univ_mem_sets : univ ∈ f := f.univ_sets lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f → x ⊆ y → y ∈ f := f.sets_of_superset lemma inter_mem_sets : ∀{s t}, s ∈ f → t ∈ f → s ∩ t ∈ f := f.inter_sets lemma univ_mem_sets' (h : ∀ a, a ∈ s) : s ∈ f := mem_sets_of_superset univ_mem_sets (assume x _, h x) lemma mp_sets (hs : s ∈ f) (h : {x | x ∈ s → x ∈ t} ∈ f) : t ∈ f := mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁ lemma congr_sets (h : {x | x ∈ s ↔ x ∈ t} ∈ f) : s ∈ f ↔ t ∈ f := ⟨λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mp)), λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mpr))⟩ lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) : (∀i∈is, s i ∈ f) → (⋂i∈is, s i) ∈ f := finite.induction_on hf (assume hs, by simp only [univ_mem_sets, mem_empty_eq, Inter_neg, Inter_univ, not_false_iff]) (assume i is _ hf hi hs, have h₁ : s i ∈ f, from hs i (by simp), have h₂ : (⋂x∈is, s x) ∈ f, from hi $ assume a ha, hs _ $ by simp only [ha, mem_insert_iff, or_true], by simp [inter_mem_sets h₁ h₂]) lemma sInter_mem_sets_of_finite {s : set (set α)} (hfin : finite s) (h_in : ∀ U ∈ s, U ∈ f) : ⋂₀ s ∈ f := by { rw sInter_eq_bInter, exact Inter_mem_sets hfin h_in } lemma Inter_mem_sets_of_fintype {β : Type v} {s : β → set α} [fintype β] (h : ∀i, s i ∈ f) : (⋂i, s i) ∈ f := by simpa using Inter_mem_sets finite_univ (λi hi, h i) lemma exists_sets_subset_iff : (∃t ∈ f, t ⊆ s) ↔ s ∈ f := ⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩ lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f) := assume s t hst h, mem_sets_of_superset h hst end filter namespace tactic.interactive open tactic interactive /-- `filter_upwards [h1, ⋯, hn]` replaces a goal of the form `s ∈ f` and terms `h1 : t1 ∈ f, ⋯, hn : tn ∈ f` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`. `filter_upwards [h1, ⋯, hn] e` is a short form for `{ filter_upwards [h1, ⋯, hn], exact e }`. -/ meta def filter_upwards (s : parse types.pexpr_list) (e' : parse $ optional types.texpr) : tactic unit := do s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e), eapplyc `filter.univ_mem_sets', match e' with | some e := interactive.exact e | none := skip end end tactic.interactive namespace filter variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section principal /-- The principal filter of `s` is the collection of all supersets of `s`. -/ def principal (s : set α) : filter α := { sets := {t | s ⊆ t}, univ_sets := subset_univ s, sets_of_superset := assume x y hx hy, subset.trans hx hy, inter_sets := assume x y, subset_inter } instance : inhabited (filter α) := ⟨principal ∅⟩ @[simp] lemma mem_principal_sets {s t : set α} : s ∈ principal t ↔ t ⊆ s := iff.rfl lemma mem_principal_self (s : set α) : s ∈ principal s := subset.refl _ end principal section join /-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/ def join (f : filter (filter α)) : filter α := { sets := {s | {t : filter α | s ∈ t} ∈ f}, univ_sets := by simp only [univ_mem_sets, mem_set_of_eq]; exact univ_mem_sets, sets_of_superset := assume x y hx xy, mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy, inter_sets := assume x y hx hy, mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ } @[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} : s ∈ join f ↔ {t | s ∈ t} ∈ f := iff.rfl end join section lattice instance : partial_order (filter α) := { le := λf g, ∀ ⦃U : set α⦄, U ∈ g → U ∈ f, le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁, le_refl := assume a, subset.refl _, le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ } theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g, x ∈ f := iff.rfl /-- `generate_sets g s`: `s` is in the filter closure of `g`. -/ inductive generate_sets (g : set (set α)) : set α → Prop | basic {s : set α} : s ∈ g → generate_sets s | univ : generate_sets univ | superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t | inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t) /-- `generate g` is the smallest filter containing the sets `g`. -/ def generate (g : set (set α)) : filter α := { sets := generate_sets g, univ_sets := generate_sets.univ, sets_of_superset := assume x y, generate_sets.superset, inter_sets := assume s t, generate_sets.inter } lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets := iff.intro (assume h u hu, h $ generate_sets.basic $ hu) (assume h u hu, hu.rec_on h univ_mem_sets (assume x y _ hxy hx, mem_sets_of_superset hx hxy) (assume x y _ _ hx hy, inter_mem_sets hx hy)) lemma mem_generate_iff (s : set $ set α) {U : set α} : U ∈ generate s ↔ ∃ t ⊆ s, finite t ∧ ⋂₀ t ⊆ U := begin split ; intro h, { induction h with V V_in V W V_in hVW hV V W V_in W_in hV hW, { use {V}, simp [V_in] }, { use ∅, simp [subset.refl, univ] }, { rcases hV with ⟨t, hts, htfin, hinter⟩, exact ⟨t, hts, htfin, subset.trans hinter hVW⟩ }, { rcases hV with ⟨t, hts, htfin, htinter⟩, rcases hW with ⟨z, hzs, hzfin, hzinter⟩, refine ⟨t ∪ z, union_subset hts hzs, finite_union htfin hzfin, _⟩, rw sInter_union, exact inter_subset_inter htinter hzinter } }, { rcases h with ⟨t, ts, tfin, h⟩, apply generate_sets.superset _ h, revert ts, apply finite.induction_on tfin, { intro h, rw sInter_empty, exact generate_sets.univ }, { intros V r hV rfin hinter h, cases insert_subset.mp h with V_in r_sub, rw [insert_eq V r, sInter_union], apply generate_sets.inter _ (hinter r_sub), rw sInter_singleton, exact generate_sets.basic V_in } }, end /-- `mk_of_closure s hs` constructs a filter on `α` whose elements set is exactly `s : set (set α)`, provided one gives the assumption `hs : (generate s).sets = s`. -/ protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α := { sets := s, univ_sets := hs ▸ (univ_mem_sets : univ ∈ generate s), sets_of_superset := assume x y, hs ▸ (mem_sets_of_superset : x ∈ generate s → x ⊆ y → y ∈ generate s), inter_sets := assume x y, hs ▸ (inter_mem_sets : x ∈ generate s → y ∈ generate s → x ∩ y ∈ generate s) } lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} : filter.mk_of_closure s hs = generate s := filter.ext $ assume u, show u ∈ (filter.mk_of_closure s hs).sets ↔ u ∈ (generate s).sets, from hs.symm ▸ iff.rfl /-- Galois insertion from sets of sets into filters. -/ def gi_generate (α : Type*) : @galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets := { gc := assume s f, sets_iff_generate, le_l_u := assume f u h, generate_sets.basic h, choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _), choice_eq := assume s hs, mk_of_closure_sets } /-- The infimum of filters is the filter generated by intersections of elements of the two filters. -/ instance : has_inf (filter α) := ⟨λf g : filter α, { sets := {s | ∃ (a ∈ f) (b ∈ g), a ∩ b ⊆ s }, univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩, sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩, inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩, ⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd, calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl ... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩ @[simp] lemma mem_inf_sets {f g : filter α} {s : set α} : s ∈ f ⊓ g ↔ ∃t₁∈f, ∃t₂∈g, t₁ ∩ t₂ ⊆ s := iff.rfl lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f) : s ∈ f ⊓ g := ⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩ lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g) : s ∈ f ⊓ g := ⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩ lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g := inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht) instance : has_top (filter α) := ⟨{ sets := {s | ∀x, x ∈ s}, univ_sets := assume x, mem_univ x, sets_of_superset := assume x y hx hxy a, hxy (hx a), inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩ lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α) ↔ (∀x, x ∈ s) := iff.rfl @[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α) ↔ s = univ := by rw [mem_top_sets_iff_forall, eq_univ_iff_forall] section complete_lattice /- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately, we want to have different definitional equalities for the lattice operations. So we define them upfront and change the lattice operations for the complete lattice instance. -/ private def original_complete_lattice : complete_lattice (filter α) := @order_dual.complete_lattice _ (gi_generate α).lift_complete_lattice local attribute [instance] original_complete_lattice instance : complete_lattice (filter α) := original_complete_lattice.copy /- le -/ filter.partial_order.le rfl /- top -/ (filter.has_top).1 (top_unique $ assume s hs, by have := univ_mem_sets ; finish) /- bot -/ _ rfl /- sup -/ _ rfl /- inf -/ (filter.has_inf).1 begin ext f g : 2, exact le_antisymm (le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right)) (assume s ⟨a, ha, b, hb, hs⟩, show s ∈ complete_lattice.inf f g, from mem_sets_of_superset (inter_mem_sets (@inf_le_left (filter α) _ _ _ _ ha) (@inf_le_right (filter α) _ _ _ _ hb)) hs) end /- Sup -/ (join ∘ principal) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm) /- Inf -/ _ rfl end complete_lattice lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets := (gi_generate α).gc.u_inf lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) := (gi_generate α).gc.u_Inf lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) := (gi_generate α).gc.u_infi lemma generate_empty : filter.generate ∅ = (⊤ : filter α) := (gi_generate α).gc.l_bot lemma generate_univ : filter.generate univ = (⊥ : filter α) := mk_of_closure_sets.symm lemma generate_union {s t : set (set α)} : filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t := (gi_generate α).gc.l_sup lemma generate_Union {s : ι → set (set α)} : filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) := (gi_generate α).gc.l_supr @[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α) := trivial @[simp] lemma mem_sup_sets {f g : filter α} {s : set α} : s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g := iff.rfl @[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} : x ∈ Sup s ↔ (∀f∈s, x ∈ (f:filter α)) := iff.rfl @[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} : x ∈ supr f ↔ (∀i, x ∈ f i) := by simp only [supr_sets_eq, iff_self, mem_Inter] lemma infi_eq_generate (s : ι → filter α) : infi s = generate (⋃ i, (s i).sets) := show generate _ = generate _, from congr_arg _ supr_range lemma mem_infi_iff {ι} {s : ι → filter α} {U : set α} : (U ∈ ⨅ i, s i) ↔ ∃ I : set ι, finite I ∧ ∃ V : {i | i ∈ I} → set α, (∀ i, V i ∈ s i) ∧ (⋂ i, V i) ⊆ U := begin rw [infi_eq_generate, mem_generate_iff], split, { rintro ⟨t, tsub, tfin, tinter⟩, rcases eq_finite_Union_of_finite_subset_Union tfin tsub with ⟨I, Ifin, σ, σfin, σsub, rfl⟩, rw sInter_Union at tinter, let V := λ i, ⋂₀ σ i, have V_in : ∀ i, V i ∈ s i, { rintro ⟨i, i_in⟩, apply sInter_mem_sets_of_finite (σfin _), apply σsub }, exact ⟨I, Ifin, V, V_in, tinter⟩ }, { rintro ⟨I, Ifin, V, V_in, h⟩, refine ⟨range V, _, _, h⟩, { rintro _ ⟨i, rfl⟩, rw mem_Union, use [i, V_in i] }, { haveI : fintype {i : ι | i ∈ I} := finite.fintype Ifin, exact finite_range _ } }, end @[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ principal s ↔ s ∈ f := show (∀{t}, s ⊆ t → t ∈ f) ↔ s ∈ f, from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩ lemma principal_mono {s t : set α} : principal s ≤ principal t ↔ s ⊆ t := by simp only [le_principal_iff, iff_self, mem_principal_sets] lemma monotone_principal : monotone (principal : set α → filter α) := λ _ _, principal_mono.2 @[simp] lemma principal_eq_iff_eq {s t : set α} : principal s = principal t ↔ s = t := by simp only [le_antisymm_iff, le_principal_iff, mem_principal_sets]; refl @[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (principal s) = Sup s := rfl lemma principal_univ : principal (univ : set α) = ⊤ := top_unique $ by simp only [le_principal_iff, mem_top_sets, eq_self_iff_true] lemma principal_empty : principal (∅ : set α) = ⊥ := bot_unique $ assume s _, empty_subset _ /-! ### Lattice equations -/ lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f ↔ f = ⊥ := ⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s), assume : f = ⊥, this.symm ▸ mem_bot_sets⟩ lemma nonempty_of_mem_sets {f : filter α} (hf : f ≠ ⊥) {s : set α} (hs : s ∈ f) : s.nonempty := s.eq_empty_or_nonempty.elim (λ h, absurd hs (h.symm ▸ mt empty_in_sets_eq_bot.mp hf)) id lemma nonempty_of_ne_bot {f : filter α} (hf : f ≠ ⊥) : nonempty α := nonempty_of_exists $ nonempty_of_mem_sets hf univ_mem_sets lemma filter_eq_bot_of_not_nonempty {f : filter α} (ne : ¬ nonempty α) : f = ⊥ := empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩) lemma forall_sets_nonempty_iff_ne_bot {f : filter α} : (∀ (s : set α), s ∈ f → s.nonempty) ↔ f ≠ ⊥ := ⟨λ h hf, empty_not_nonempty (h ∅ $ hf.symm ▸ mem_bot_sets), nonempty_of_mem_sets⟩ lemma mem_sets_of_eq_bot {f : filter α} {s : set α} (h : f ⊓ principal (-s) = ⊥) : s ∈ f := have ∅ ∈ f ⊓ principal (- s), from h.symm ▸ mem_bot_sets, let ⟨s₁, hs₁, s₂, (hs₂ : -s ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩ lemma inf_ne_bot_iff {f g : filter α} : f ⊓ g ≠ ⊥ ↔ ∀ {U V}, U ∈ f → V ∈ g → set.nonempty (U ∩ V) := begin rw ← forall_sets_nonempty_iff_ne_bot, simp_rw mem_inf_sets, split ; intro h, { intros U V U_in V_in, exact h (U ∩ V) ⟨U, U_in, V, V_in, subset.refl _⟩ }, { rintros S ⟨U, U_in, V, V_in, hUV⟩, cases h U_in V_in with a ha, use [a, hUV ha] } end lemma eq_Inf_of_mem_sets_iff_exists_mem {S : set (filter α)} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ f ∈ S, s ∈ f) : l = Inf S := le_antisymm (le_Inf $ λ f hf s hs, h.2 ⟨f, hf, hs⟩) (λ s hs, let ⟨f, hf, hs⟩ := h.1 hs in (Inf_le hf : Inf S ≤ f) hs) lemma eq_infi_of_mem_sets_iff_exists_mem {f : ι → filter α} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i, s ∈ f i) : l = infi f := eq_Inf_of_mem_sets_iff_exists_mem $ λ s, h.trans exists_range_iff.symm lemma eq_binfi_of_mem_sets_iff_exists_mem {f : ι → filter α} {p : ι → Prop} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i (_ : p i), s ∈ f i) : l = ⨅ i (_ : p i), f i := begin rw [infi_subtype'], apply eq_infi_of_mem_sets_iff_exists_mem, intro s, exact h.trans ⟨λ ⟨i, pi, si⟩, ⟨⟨i, pi⟩, si⟩, λ ⟨⟨i, pi⟩, si⟩, ⟨i, pi, si⟩⟩ end lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) : (infi f).sets = (⋃ i, (f i).sets) := let ⟨i⟩ := ne, u := { filter . sets := (⋃ i, (f i).sets), univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem_sets⟩, sets_of_superset := by simp only [mem_Union, exists_imp_distrib]; intros x y i hx hxy; exact ⟨i, mem_sets_of_superset hx hxy⟩, inter_sets := begin simp only [mem_Union, exists_imp_distrib], assume x y a hx b hy, rcases h a b with ⟨c, ha, hb⟩, exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩ end } in have u = infi f, from eq_infi_of_mem_sets_iff_exists_mem (λ s, by simp only [mem_Union]), congr_arg filter.sets this.symm lemma mem_infi {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) (s) : s ∈ infi f ↔ ∃ i, s ∈ f i := by simp only [infi_sets_eq h ne, mem_Union] @[nolint ge_or_gt] -- Intentional use of `≥` lemma binfi_sets_eq {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) : (⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) := let ⟨i, hi⟩ := ne in calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl ... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq (assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end) ⟨⟨i, hi⟩⟩ ... = (⨆ t ∈ {t | t ∈ s}, (f t).sets) : by rw [supr_subtype]; refl @[nolint ge_or_gt] -- Intentional use of `≥` lemma mem_binfi {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) {t : set α} : t ∈ (⨅ i∈s, f i) ↔ ∃ i ∈ s, t ∈ f i := by simp only [binfi_sets_eq h ne, mem_bUnion_iff] lemma infi_sets_eq_finite (f : ι → filter α) : (⨅i, f i).sets = (⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets) := begin rw [infi_eq_infi_finset, infi_sets_eq], exact (directed_of_sup $ λs₁ s₂ hs, infi_le_infi $ λi, infi_le_infi_const $ λh, hs h), apply_instance end lemma mem_infi_finite {f : ι → filter α} (s) : s ∈ infi f ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets := show s ∈ (infi f).sets ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets, by rw infi_sets_eq_finite @[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) := filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, join, mem_sup_sets, iff_self, mem_set_of_eq] @[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} : (⨆x, join (f x)) = join (⨆x, f x) := filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, join, iff_self, mem_Inter, mem_set_of_eq] instance : bounded_distrib_lattice (filter α) := { le_sup_inf := begin assume x y z s, simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp], intros hs t₁ ht₁ t₂ ht₂ hts, exact ⟨s ∪ t₁, x.sets_of_superset hs $ subset_union_left _ _, y.sets_of_superset ht₁ $ subset_union_right _ _, s ∪ t₂, x.sets_of_superset hs $ subset_union_left _ _, z.sets_of_superset ht₂ $ subset_union_right _ _, subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩ end, ..filter.complete_lattice } /- the complementary version with ⨆i, f ⊓ g i does not hold! -/ lemma infi_sup_eq {f : filter α} {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g := begin refine le_antisymm _ (le_infi $ assume i, sup_le_sup_left (infi_le _ _) _), rintros t ⟨h₁, h₂⟩, rw [infi_sets_eq_finite] at h₂, simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at h₂, rcases h₂ with ⟨s, hs⟩, suffices : (⨅i, f ⊔ g i) ≤ f ⊔ s.inf (λi, g i.down), { exact this ⟨h₁, hs⟩ }, refine finset.induction_on s _ _, { exact le_sup_right_of_le le_top }, { rintros ⟨i⟩ s his ih, rw [finset.inf_insert, sup_inf_left], exact le_inf (infi_le _ _) ih } end lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} : ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⋂a∈s, p a) ⊆ t) := show ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⨅a∈s, p a) ≤ t), begin simp only [(finset.inf_eq_infi _ _).symm], refine finset.induction_on s _ _, { simp only [finset.not_mem_empty, false_implies_iff, finset.inf_empty, top_le_iff, imp_true_iff, mem_top_sets, true_and, exists_const], intros; refl }, { intros a s has ih t, simp only [ih, finset.forall_mem_insert, finset.inf_insert, mem_inf_sets, exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt}, split, { intros t₁ ht₁ t₂ p hp ht₂ ht, existsi function.update p a t₁, have : ∀a'∈s, function.update p a t₁ a' = p a', from assume a' ha', have a' ≠ a, from assume h, has $ h ▸ ha', function.update_noteq this _ _, have eq : s.inf (λj, function.update p a t₁ j) = s.inf (λj, p j) := finset.inf_congr rfl this, simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt}, exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht }, assume p hpa hp ht, exact ⟨p a, hpa, (s.inf p), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ } end /-- If `f : ι → filter α` is directed, `ι` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`. See also `infi_ne_bot_of_directed` for a version assuming `nonempty α` instead of `nonempty ι`. -/ lemma infi_ne_bot_of_directed' {f : ι → filter α} (hn : nonempty ι) (hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥) : (infi f) ≠ ⊥ := begin intro h, have he: ∅ ∈ (infi f), from h.symm ▸ (mem_bot_sets : ∅ ∈ (⊥ : filter α)), obtain ⟨i, hi⟩ : ∃i, ∅ ∈ f i, from (mem_infi hd hn ∅).1 he, exact hb i (empty_in_sets_eq_bot.1 hi) end /-- If `f : ι → filter α` is directed, `α` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`. See also `infi_ne_bot_of_directed'` for a version assuming `nonempty ι` instead of `nonempty α`. -/ lemma infi_ne_bot_of_directed {f : ι → filter α} (hn : nonempty α) (hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥) : (infi f) ≠ ⊥ := if hι : nonempty ι then infi_ne_bot_of_directed' hι hd hb else assume h : infi f = ⊥, have univ ⊆ (∅ : set α), begin rw [←principal_mono, principal_univ, principal_empty, ←h], exact (le_infi $ assume i, false.elim $ hι ⟨i⟩) end, let ⟨x⟩ := hn in this (mem_univ x) lemma infi_ne_bot_iff_of_directed' {f : ι → filter α} (hn : nonempty ι) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) := ⟨assume ne_bot i, ne_bot_of_le_ne_bot ne_bot (infi_le _ i), infi_ne_bot_of_directed' hn hd⟩ lemma infi_ne_bot_iff_of_directed {f : ι → filter α} (hn : nonempty α) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) := ⟨assume ne_bot i, ne_bot_of_le_ne_bot ne_bot (infi_le _ i), infi_ne_bot_of_directed hn hd⟩ lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ f i → s ∈ ⨅i, f i := show (⨅i, f i) ≤ f i, from infi_le _ _ @[elab_as_eliminator] lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ infi f) {p : set α → Prop} (uni : p univ) (ins : ∀{i s₁ s₂}, s₁ ∈ f i → p s₂ → p (s₁ ∩ s₂)) (upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s := begin rw [mem_infi_finite] at hs, simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at hs, rcases hs with ⟨is, his⟩, revert s, refine finset.induction_on is _ _, { assume s hs, rwa [mem_top_sets.1 hs] }, { rintros ⟨i⟩ js his ih s hs, rw [finset.inf_insert, mem_inf_sets] at hs, rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩, exact upw hs (ins hs₁ (ih hs₂)) } end /- principal equations -/ @[simp] lemma inf_principal {s t : set α} : principal s ⊓ principal t = principal (s ∩ t) := le_antisymm (by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩) (by simp [le_inf_iff, inter_subset_left, inter_subset_right]) @[simp] lemma sup_principal {s t : set α} : principal s ⊔ principal t = principal (s ∪ t) := filter_eq $ set.ext $ by simp only [union_subset_iff, union_subset_iff, mem_sup_sets, forall_const, iff_self, mem_principal_sets] @[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, principal (s x)) = principal (⋃i, s i) := filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, mem_principal_sets, mem_Inter]; exact (@supr_le_iff (set α) _ _ _ _).symm @[simp] lemma principal_eq_bot_iff {s : set α} : principal s = ⊥ ↔ s = ∅ := empty_in_sets_eq_bot.symm.trans $ mem_principal_sets.trans subset_empty_iff lemma principal_ne_bot_iff {s : set α} : principal s ≠ ⊥ ↔ s.nonempty := (not_congr principal_eq_bot_iff).trans ne_empty_iff_nonempty lemma is_compl_principal (s : set α) : is_compl (principal s) (principal (-s)) := ⟨by simp only [inf_principal, inter_compl_self, principal_empty, le_refl], by simp only [sup_principal, union_compl_self, principal_univ, le_refl]⟩ lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : -s ∈ f) : f ⊓ principal s = ⊥ := empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩ theorem mem_inf_principal (f : filter α) (s t : set α) : s ∈ f ⊓ principal t ↔ {x | x ∈ t → x ∈ s} ∈ f := begin simp only [← le_principal_iff, (is_compl_principal s).le_left_iff, disjoint, inf_assoc, inf_principal, imp_iff_not_or], rw [← disjoint, ← (is_compl_principal (t ∩ -s)).le_right_iff, compl_inter, compl_compl], refl end @[simp] lemma infi_principal_finset {ι : Type w} (s : finset ι) (f : ι → set α) : (⨅i∈s, principal (f i)) = principal (⋂i∈s, f i) := begin ext t, simp [mem_infi_sets_finset], split, { rintros ⟨p, hp, ht⟩, calc (⋂ (i : ι) (H : i ∈ s), f i) ≤ (⋂ (i : ι) (H : i ∈ s), p i) : infi_le_infi (λi, infi_le_infi (λhi, mem_principal_sets.1 (hp i hi))) ... ≤ t : ht }, { assume h, exact ⟨f, λi hi, subset.refl _, h⟩ } end @[simp] lemma infi_principal_fintype {ι : Type w} [fintype ι] (f : ι → set α) : (⨅i, principal (f i)) = principal (⋂i, f i) := by simpa using infi_principal_finset finset.univ f end lattice /-! ### Eventually -/ /-- `f.eventually p` or `∀ᶠ x in f, p x` mean that `{x | p x} ∈ f`. E.g., `∀ᶠ x in at_top, p x` means that `p` holds true for sufficiently large `x`. -/ protected def eventually (p : α → Prop) (f : filter α) : Prop := {x | p x} ∈ f notation `∀ᶠ` binders ` in ` f `, ` r:(scoped p, filter.eventually p f) := r lemma eventually_iff {f : filter α} {P : α → Prop} : (∀ᶠ x in f, P x) ↔ {x | P x} ∈ f := iff.rfl lemma eventually_of_mem {f : filter α} {P : α → Prop} {U : set α} (hU : U ∈ f) (h : ∀ x ∈ U, P x) : ∀ᶠ x in f, P x := mem_sets_of_superset hU h protected lemma eventually.and {p q : α → Prop} {f : filter α} : f.eventually p → f.eventually q → ∀ᶠ x in f, p x ∧ q x := inter_mem_sets @[simp] lemma eventually_true (f : filter α) : ∀ᶠ x in f, true := univ_mem_sets lemma eventually_of_forall {p : α → Prop} (f : filter α) (hp : ∀ x, p x) : ∀ᶠ x in f, p x := univ_mem_sets' hp @[simp] lemma eventually_false_iff_eq_bot {f : filter α} : (∀ᶠ x in f, false) ↔ f = ⊥ := empty_in_sets_eq_bot @[simp] lemma eventually_const {f : filter α} (hf : f ≠ ⊥) {p : Prop} : (∀ᶠ x in f, p) ↔ p := classical.by_cases (λ h : p, by simp [h]) (λ h, by simp [h, hf]) lemma eventually.mp {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ᶠ x in f, p x → q x) : ∀ᶠ x in f, q x := mp_sets hp hq lemma eventually.mono {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ x, p x → q x) : ∀ᶠ x in f, q x := hp.mp (f.eventually_of_forall hq) @[simp] lemma eventually_and {p q : α → Prop} {f : filter α} : (∀ᶠ x in f, p x ∧ q x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in f, q x) := ⟨λ h, ⟨h.mono $ λ _, and.left, h.mono $ λ _, and.right⟩, λ h, h.1.and h.2⟩ lemma eventually.congr {f : filter α} {p q : α → Prop} (h' : ∀ᶠ x in f, p x) (h : ∀ᶠ x in f, p x ↔ q x) : ∀ᶠ x in f, q x := h'.mp (h.mono $ λ x hx, hx.mp) lemma eventually_congr {f : filter α} {p q : α → Prop} (h : ∀ᶠ x in f, p x ↔ q x) : (∀ᶠ x in f, p x) ↔ (∀ᶠ x in f, q x) := ⟨λ hp, hp.congr h, λ hq, hq.congr $ by simpa only [iff.comm] using h⟩ @[simp] lemma eventually_or_distrib_left {f : filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p ∨ q x) ↔ (p ∨ ∀ᶠ x in f, q x) := classical.by_cases (λ h : p, by simp [h]) (λ h, by simp [h]) @[simp] lemma eventually_or_distrib_right {f : filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x ∨ q) ↔ ((∀ᶠ x in f, p x) ∨ q) := by simp only [or_comm _ q, eventually_or_distrib_left] @[simp] lemma eventually_imp_distrib_left {f : filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p → q x) ↔ (p → ∀ᶠ x in f, q x) := by simp only [imp_iff_not_or, eventually_or_distrib_left] @[simp] lemma eventually_bot {p : α → Prop} : ∀ᶠ x in ⊥, p x := ⟨⟩ @[simp] lemma eventually_top {p : α → Prop} : (∀ᶠ x in ⊤, p x) ↔ (∀ x, p x) := iff.rfl lemma eventually_sup {p : α → Prop} {f g : filter α} : (∀ᶠ x in f ⊔ g, p x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in g, p x) := iff.rfl @[simp] lemma eventually_Sup {p : α → Prop} {fs : set (filter α)} : (∀ᶠ x in Sup fs, p x) ↔ (∀ f ∈ fs, ∀ᶠ x in f, p x) := iff.rfl @[simp] lemma eventually_supr {p : α → Prop} {fs : β → filter α} : (∀ᶠ x in (⨆ b, fs b), p x) ↔ (∀ b, ∀ᶠ x in fs b, p x) := mem_supr_sets @[simp] lemma eventually_principal {a : set α} {p : α → Prop} : (∀ᶠ x in principal a, p x) ↔ (∀ x ∈ a, p x) := iff.rfl /-! ### Frequently -/ /-- `f.frequently p` or `∃ᶠ x in f, p x` mean that `{x | ¬p x} ∉ f`. E.g., `∃ᶠ x in at_top, p x` means that there exist arbitrarily large `x` for which `p` holds true. -/ protected def frequently (p : α → Prop) (f : filter α) : Prop := ¬∀ᶠ x in f, ¬p x notation `∃ᶠ` binders ` in ` f `, ` r:(scoped p, filter.frequently p f) := r lemma eventually.frequently {f : filter α} (hf : f ≠ ⊥) {p : α → Prop} (h : ∀ᶠ x in f, p x) : ∃ᶠ x in f, p x := begin assume h', have := h.and h', simp only [and_not_self, eventually_false_iff_eq_bot] at this, exact hf this end lemma frequently.mp {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ᶠ x in f, p x → q x) : ∃ᶠ x in f, q x := mt (λ hq, hq.mp $ hpq.mono $ λ x, mt) h lemma frequently.mono {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ x, p x → q x) : ∃ᶠ x in f, q x := h.mp (f.eventually_of_forall hpq) lemma frequently.and_eventually {p q : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) (hq : ∀ᶠ x in f, q x) : ∃ᶠ x in f, p x ∧ q x := begin refine mt (λ h, hq.mp $ h.mono _) hp, assume x hpq hq hp, exact hpq ⟨hp, hq⟩ end lemma frequently.exists {p : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) : ∃ x, p x := begin by_contradiction H, replace H : ∀ᶠ x in f, ¬ p x, from f.eventually_of_forall (not_exists.1 H), exact hp H end lemma eventually.exists {p : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hf : f ≠ ⊥) : ∃ x, p x := (hp.frequently hf).exists lemma frequently_iff_forall_eventually_exists_and {p : α → Prop} {f : filter α} : (∃ᶠ x in f, p x) ↔ ∀ {q : α → Prop}, (∀ᶠ x in f, q x) → ∃ x, p x ∧ q x := ⟨assume hp q hq, (hp.and_eventually hq).exists, assume H hp, by simpa only [and_not_self, exists_false] using H hp⟩ lemma frequently_iff {f : filter α} {P : α → Prop} : (∃ᶠ x in f, P x) ↔ ∀ {U}, U ∈ f → ∃ x ∈ U, P x := begin rw frequently_iff_forall_eventually_exists_and, split ; intro h, { intros U U_in, simpa [exists_prop, and_comm] using h U_in }, { intros H H', simpa [and_comm] using h H' }, end @[simp] lemma not_eventually {p : α → Prop} {f : filter α} : (¬ ∀ᶠ x in f, p x) ↔ (∃ᶠ x in f, ¬ p x) := by simp [filter.frequently] @[simp] lemma not_frequently {p : α → Prop} {f : filter α} : (¬ ∃ᶠ x in f, p x) ↔ (∀ᶠ x in f, ¬ p x) := by simp only [filter.frequently, not_not] @[simp] lemma frequently_true_iff_ne_bot (f : filter α) : (∃ᶠ x in f, true) ↔ f ≠ ⊥ := by simp [filter.frequently, -not_eventually, eventually_false_iff_eq_bot] @[simp] lemma frequently_false (f : filter α) : ¬ ∃ᶠ x in f, false := by simp @[simp] lemma frequently_const {f : filter α} (hf : f ≠ ⊥) {p : Prop} : (∃ᶠ x in f, p) ↔ p := classical.by_cases (λ h : p, by simp [*]) (λ h, by simp [*]) @[simp] lemma frequently_or_distrib {f : filter α} {p q : α → Prop} : (∃ᶠ x in f, p x ∨ q x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in f, q x) := by simp only [filter.frequently, ← not_and_distrib, not_or_distrib, eventually_and] lemma frequently_or_distrib_left {f : filter α} (hf : f ≠ ⊥) {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p ∨ q x) ↔ (p ∨ ∃ᶠ x in f, q x) := by simp [hf] lemma frequently_or_distrib_right {f : filter α} (hf : f ≠ ⊥) {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x ∨ q) ↔ (∃ᶠ x in f, p x) ∨ q := by simp [hf] @[simp] lemma frequently_imp_distrib {f : filter α} {p q : α → Prop} : (∃ᶠ x in f, p x → q x) ↔ ((∀ᶠ x in f, p x) → ∃ᶠ x in f, q x) := by simp [imp_iff_not_or, not_eventually, frequently_or_distrib] lemma frequently_imp_distrib_left {f : filter α} (hf : f ≠ ⊥) {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p → q x) ↔ (p → ∃ᶠ x in f, q x) := by simp [hf] lemma frequently_imp_distrib_right {f : filter α} (hf : f ≠ ⊥) {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x → q) ↔ ((∀ᶠ x in f, p x) → q) := by simp [hf] @[simp] lemma eventually_imp_distrib_right {f : filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x → q) ↔ ((∃ᶠ x in f, p x) → q) := by simp only [imp_iff_not_or, eventually_or_distrib_right, not_frequently] @[simp] lemma frequently_bot {p : α → Prop} : ¬ ∃ᶠ x in ⊥, p x := by simp @[simp] lemma frequently_top {p : α → Prop} : (∃ᶠ x in ⊤, p x) ↔ (∃ x, p x) := by simp [filter.frequently] lemma inf_ne_bot_iff_frequently_left {f g : filter α} : f ⊓ g ≠ ⊥ ↔ ∀ {p : α → Prop}, (∀ᶠ x in f, p x) → ∃ᶠ x in g, p x := begin rw filter.inf_ne_bot_iff, split ; intro h, { intros U U_in H, rcases h U_in H with ⟨x, hx, hx'⟩, exact hx' hx}, { intros U V U_in V_in, classical, by_contra H, exact h U_in (mem_sets_of_superset V_in $ λ v v_in v_in', H ⟨v, v_in', v_in⟩) } end lemma inf_ne_bot_iff_frequently_right {f g : filter α} : f ⊓ g ≠ ⊥ ↔ ∀ {p : α → Prop}, (∀ᶠ x in g, p x) → ∃ᶠ x in f, p x := by { rw inf_comm, exact filter.inf_ne_bot_iff_frequently_left } @[simp] lemma frequently_principal {a : set α} {p : α → Prop} : (∃ᶠ x in principal a, p x) ↔ (∃ x ∈ a, p x) := by simp [filter.frequently, not_forall] lemma frequently_sup {p : α → Prop} {f g : filter α} : (∃ᶠ x in f ⊔ g, p x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in g, p x) := by simp only [filter.frequently, eventually_sup, not_and_distrib] @[simp] lemma frequently_Sup {p : α → Prop} {fs : set (filter α)} : (∃ᶠ x in Sup fs, p x) ↔ (∃ f ∈ fs, ∃ᶠ x in f, p x) := by simp [filter.frequently, -not_eventually, not_forall] @[simp] lemma frequently_supr {p : α → Prop} {fs : β → filter α} : (∃ᶠ x in (⨆ b, fs b), p x) ↔ (∃ b, ∃ᶠ x in fs b, p x) := by simp [filter.frequently, -not_eventually, not_forall] /-! ### Push-forwards, pull-backs, and the monad structure -/ section map /-- The forward map of a filter -/ def map (m : α → β) (f : filter α) : filter β := { sets := preimage m ⁻¹' f.sets, univ_sets := univ_mem_sets, sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st, inter_sets := assume s t hs ht, inter_mem_sets hs ht } @[simp] lemma map_principal {s : set α} {f : α → β} : map f (principal s) = principal (set.image f s) := filter_eq $ set.ext $ assume a, image_subset_iff.symm variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] lemma eventually_map {P : β → Prop} : (∀ᶠ b in map m f, P b) ↔ ∀ᶠ a in f, P (m a) := iff.rfl @[simp] lemma frequently_map {P : β → Prop} : (∃ᶠ b in map m f, P b) ↔ ∃ᶠ a in f, P (m a) := iff.rfl @[simp] lemma mem_map : t ∈ map m f ↔ {x | m x ∈ t} ∈ f := iff.rfl lemma image_mem_map (hs : s ∈ f) : m '' s ∈ map m f := f.sets_of_superset hs $ subset_preimage_image m s lemma range_mem_map : range m ∈ map m f := by rw ←image_univ; exact image_mem_map univ_mem_sets lemma mem_map_sets_iff : t ∈ map m f ↔ (∃s∈f, m '' s ⊆ t) := iff.intro (assume ht, ⟨set.preimage m t, ht, image_preimage_subset _ _⟩) (assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht) @[simp] lemma map_id : filter.map id f = f := filter_eq $ rfl @[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) := funext $ assume _, filter_eq $ rfl @[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f := congr_fun (@@filter.map_compose m m') f end map section comap /-- The inverse map of a filter -/ def comap (m : α → β) (f : filter β) : filter α := { sets := { s | ∃t∈ f, m ⁻¹' t ⊆ s }, univ_sets := ⟨univ, univ_mem_sets, by simp only [subset_univ, preimage_univ]⟩, sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab, ⟨a', ha', subset.trans ma'a ab⟩, inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩, ⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ } @[simp] lemma eventually_comap {f : filter β} {φ : α → β} {P : α → Prop} : (∀ᶠ a in comap φ f, P a) ↔ ∀ᶠ b in f, ∀ a, φ a = b → P a := begin split ; intro h, { rcases h with ⟨t, t_in, ht⟩, apply mem_sets_of_superset t_in, rintros y y_in _ rfl, apply ht y_in }, { exact ⟨_, h, λ _ x_in, x_in _ rfl⟩ } end @[simp] lemma frequently_comap {f : filter β} {φ : α → β} {P : α → Prop} : (∃ᶠ a in comap φ f, P a) ↔ ∃ᶠ b in f, ∃ a, φ a = b ∧ P a := begin classical, erw [← not_iff_not, not_not, not_not, filter.eventually_comap], simp only [not_exists, not_and], end end comap /-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`. Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the applicative instance. -/ def bind (f : filter α) (m : α → filter β) : filter β := join (map m f) /-- The applicative sequentiation operation. This is not induced by the bind operation. -/ def seq (f : filter (α → β)) (g : filter α) : filter β := ⟨{ s | ∃u∈ f, ∃t∈ g, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) }, ⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩, assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩, assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩, ⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁, assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩ /-- `pure x` is the set of sets that contain `x`. It is equal to `principal {x}` but with this definition we have `s ∈ pure a` defeq `a ∈ s`. -/ instance : has_pure filter := ⟨λ (α : Type u) x, { sets := {s | x ∈ s}, inter_sets := λ s t, and.intro, sets_of_superset := λ s t hs hst, hst hs, univ_sets := trivial }⟩ instance : has_bind filter := ⟨@filter.bind⟩ instance : has_seq filter := ⟨@filter.seq⟩ instance : functor filter := { map := @filter.map } lemma pure_sets (a : α) : (pure a : filter α).sets = {s | a ∈ s} := rfl @[simp] lemma mem_pure_sets {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s := iff.rfl lemma pure_eq_principal (a : α) : (pure a : filter α) = principal {a} := filter.ext $ λ s, by simp only [mem_pure_sets, mem_principal_sets, singleton_subset_iff] @[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) := filter.ext $ λ s, iff.rfl @[simp] lemma join_pure (f : filter α) : join (pure f) = f := filter.ext $ λ s, iff.rfl @[simp] lemma pure_bind (a : α) (m : α → filter β) : bind (pure a) m = m a := by simp only [has_bind.bind, bind, map_pure, join_pure] section -- this section needs to be before applicative, otherwise the wrong instance will be chosen /-- The monad structure on filters. -/ protected def monad : monad filter := { map := @filter.map } local attribute [instance] filter.monad protected lemma is_lawful_monad : is_lawful_monad filter := { id_map := assume α f, filter_eq rfl, pure_bind := assume α β, pure_bind, bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl, bind_pure_comp_eq_map := assume α β f x, filter.ext $ λ s, by simp only [has_bind.bind, bind, functor.map, mem_map, mem_join_sets, mem_set_of_eq, function.comp, mem_pure_sets] } end instance : applicative filter := { map := @filter.map, seq := @filter.seq } instance : alternative filter := { failure := λα, ⊥, orelse := λα x y, x ⊔ y } @[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl @[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl /- map and comap equations -/ section map variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] theorem mem_comap_sets : s ∈ comap m g ↔ ∃t∈ g, m ⁻¹' t ⊆ s := iff.rfl theorem preimage_mem_comap (ht : t ∈ g) : m ⁻¹' t ∈ comap m g := ⟨t, ht, subset.refl _⟩ lemma comap_id : comap id f = f := le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst) lemma comap_comap_comp {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f := le_antisymm (assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩) (assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩, ⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩) @[simp] theorem comap_principal {t : set β} : comap m (principal t) = principal (m ⁻¹' t) := filter_eq $ set.ext $ assume s, ⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b, assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩ lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g := ⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩ lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) := assume f g, map_le_iff_le_comap lemma map_mono : monotone (map m) := (gc_map_comap m).monotone_l lemma comap_mono : monotone (comap m) := (gc_map_comap m).monotone_u @[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot @[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup @[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) := (gc_map_comap m).l_supr @[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top @[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf @[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) := (gc_map_comap m).u_infi lemma le_comap_top (f : α → β) (l : filter α) : l ≤ comap f ⊤ := by rw [comap_top]; exact le_top lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _ lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _ @[simp] lemma comap_bot : comap m ⊥ = ⊥ := bot_unique $ assume s _, ⟨∅, by simp only [mem_bot_sets], by simp only [empty_subset, preimage_empty]⟩ lemma comap_supr {ι} {f : ι → filter β} {m : α → β} : comap m (supr f) = (⨆i, comap m (f i)) := le_antisymm (assume s hs, have ∀i, ∃t, t ∈ f i ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap_sets, exists_prop, mem_supr_sets] using mem_supr_sets.1 hs, let ⟨t, ht⟩ := classical.axiom_of_choice this in ⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _), begin rw [preimage_Union, Union_subset_iff], assume i, exact (ht i).2 end⟩) (supr_le $ assume i, comap_mono $ le_supr _ _) lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) := by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true] lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ := le_antisymm (assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩, ⟨t₁ ∪ t₂, ⟨g₁.sets_of_superset ht₁ (subset_union_left _ _), g₂.sets_of_superset ht₂ (subset_union_right _ _)⟩, union_subset hs₁ hs₂⟩) ((@comap_mono _ _ m).le_map_sup _ _) lemma map_comap {f : filter β} {m : α → β} (hf : range m ∈ f) : (f.comap m).map m = f := le_antisymm map_comap_le (assume t' ⟨t, ht, sub⟩, by filter_upwards [ht, hf]; rintros x hxt ⟨y, rfl⟩; exact sub hxt) lemma comap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) : comap m (map m f) = f := have ∀s, preimage m (image m s) = s, from assume s, preimage_image_eq s h, le_antisymm (assume s hs, ⟨ image m s, f.sets_of_superset hs $ by simp only [this, subset.refl], by simp only [this, subset.refl]⟩) le_comap_map lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f ≤ map m g) : f ≤ g := assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)] assume a has ⟨b, ⟨hbs, hb⟩, h⟩, have b = a, from hm _ hbs _ has h, this ▸ hb lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) : map m f ≤ map m g ↔ f ≤ g := iff.intro (le_of_map_le_map_inj' hsf hsg hm) (λ h, map_mono h) lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f = map m g) : f = g := le_antisymm (le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h) (le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm) lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) : f = g := have comap m (map m f) = comap m (map m g), by rw h, by rwa [comap_map hm, comap_map hm] at this theorem le_map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l) (hf : ∀ y ∈ u, ∃ x, f x = y) : l ≤ map f (comap f l) := assume s ⟨t, tl, ht⟩, have t ∩ u ⊆ s, from assume x ⟨xt, xu⟩, exists.elim (hf x xu) $ λ a faeq, by { rw ←faeq, apply ht, change f a ∈ t, rw faeq, exact xt }, mem_sets_of_superset (inter_mem_sets tl ul) this theorem map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l) (hf : ∀ y ∈ u, ∃ x, f x = y) : map f (comap f l) = l := le_antisymm map_comap_le (le_map_comap_of_surjective' ul hf) theorem le_map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) : l ≤ map f (comap f l) := le_map_comap_of_surjective' univ_mem_sets (λ y _, hf y) theorem map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) : map f (comap f l) = l := le_antisymm map_comap_le (le_map_comap_of_surjective hf l) lemma comap_ne_bot {f : filter β} {m : α → β} (hm : ∀t∈ f, ∃a, m a ∈ t) : comap m f ≠ ⊥ := forall_sets_nonempty_iff_ne_bot.mp $ assume s ⟨t, ht, t_s⟩, set.nonempty.mono t_s (hm t ht) lemma comap_ne_bot_of_range_mem {f : filter β} {m : α → β} (hf : f ≠ ⊥) (hm : range m ∈ f) : comap m f ≠ ⊥ := comap_ne_bot $ assume t ht, let ⟨_, ha, a, rfl⟩ := nonempty_of_mem_sets hf (inter_mem_sets ht hm) in ⟨a, ha⟩ lemma comap_inf_principal_ne_bot_of_image_mem {f : filter β} {m : α → β} (hf : f ≠ ⊥) {s : set α} (hs : m '' s ∈ f) : (comap m f ⊓ principal s) ≠ ⊥ := begin refine compl_compl s ▸ mt mem_sets_of_eq_bot _, rintros ⟨t, ht, hts⟩, rcases nonempty_of_mem_sets hf (inter_mem_sets hs ht) with ⟨_, ⟨x, hxs, rfl⟩, hxt⟩, exact absurd hxs (hts hxt) end lemma comap_ne_bot_of_surj {f : filter β} {m : α → β} (hf : f ≠ ⊥) (hm : function.surjective m) : comap m f ≠ ⊥ := comap_ne_bot_of_range_mem hf $ univ_mem_sets' hm lemma comap_ne_bot_of_image_mem {f : filter β} {m : α → β} (hf : f ≠ ⊥) {s : set α} (hs : m '' s ∈ f) : comap m f ≠ ⊥ := ne_bot_of_le_ne_bot (comap_inf_principal_ne_bot_of_image_mem hf hs) inf_le_left @[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ := ⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id, assume h, by simp only [h, eq_self_iff_true, map_bot]⟩ lemma map_ne_bot (hf : f ≠ ⊥) : map m f ≠ ⊥ := assume h, hf $ by rwa [map_eq_bot_iff] at h lemma map_ne_bot_iff (f : α → β) {F : filter α} : map f F ≠ ⊥ ↔ F ≠ ⊥ := by rw [not_iff_not, map_eq_bot_iff] lemma sInter_comap_sets (f : α → β) (F : filter β) : ⋂₀(comap f F).sets = ⋂ U ∈ F, f ⁻¹' U := begin ext x, suffices : (∀ (A : set α) (B : set β), B ∈ F → f ⁻¹' B ⊆ A → x ∈ A) ↔ ∀ (B : set β), B ∈ F → f x ∈ B, by simp only [mem_sInter, mem_Inter, mem_comap_sets, this, and_imp, mem_comap_sets, exists_prop, mem_sInter, iff_self, mem_Inter, mem_preimage, exists_imp_distrib], split, { intros h U U_in, simpa only [set.subset.refl, forall_prop_of_true, mem_preimage] using h (f ⁻¹' U) U U_in }, { intros h V U U_in f_U_V, exact f_U_V (h U U_in) }, end end map lemma map_cong {m₁ m₂ : α → β} {f : filter α} (h : {x | m₁ x = m₂ x} ∈ f) : map m₁ f = map m₂ f := have ∀(m₁ m₂ : α → β) (h : {x | m₁ x = m₂ x} ∈ f), map m₁ f ≤ map m₂ f, begin intros m₁ m₂ h s hs, show {x | m₁ x ∈ s} ∈ f, filter_upwards [h, hs], simp only [subset_def, mem_preimage, mem_set_of_eq, forall_true_iff] {contextual := tt} end, le_antisymm (this m₁ m₂ h) (this m₂ m₁ $ mem_sets_of_superset h $ assume x, eq.symm) -- this is a generic rule for monotone functions: lemma map_infi_le {f : ι → filter α} {m : α → β} : map m (infi f) ≤ (⨅ i, map m (f i)) := le_infi $ assume i, map_mono $ infi_le _ _ lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) (hι : nonempty ι) : map m (infi f) = (⨅ i, map m (f i)) := le_antisymm map_infi_le (assume s (hs : preimage m s ∈ infi f), have ∃i, preimage m s ∈ f i, by simp only [infi_sets_eq hf hι, mem_Union] at hs; assumption, let ⟨i, hi⟩ := this in have (⨅ i, map m (f i)) ≤ principal s, from infi_le_of_le i $ by simp only [le_principal_iff, mem_map]; assumption, by simp only [filter.le_principal_iff] at this; assumption) lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop} (h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) : map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) := let ⟨i, hi⟩ := ne in calc map m (⨅i (h : p i), f i) = map m (⨅i:subtype p, f i.val) : by simp only [infi_subtype, eq_self_iff_true] ... = (⨅i:subtype p, map m (f i.val)) : map_infi_eq (assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end) ⟨⟨i, hi⟩⟩ ... = (⨅i (h : p i), map m (f i)) : by simp only [infi_subtype, eq_self_iff_true] lemma map_inf_le {f g : filter α} {m : α → β} : map m (f ⊓ g) ≤ map m f ⊓ map m g := (@map_mono _ _ m).map_inf_le f g lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f) (htg : t ∈ g) (h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g := begin refine le_antisymm map_inf_le (assume s hs, _), simp only [map, mem_inf_sets, exists_prop, mem_map, mem_preimage, mem_inf_sets] at hs ⊢, rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩, refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩, { filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { rw [image_inter_on], { refine image_subset_iff.2 _, exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ }, { exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } } end lemma map_inf {f g : filter α} {m : α → β} (h : function.injective m) : map m (f ⊓ g) = map m f ⊓ map m g := map_inf' univ_mem_sets univ_mem_sets (assume x _ y _ hxy, h hxy) lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α} (h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f := le_antisymm (assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $ calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true] ... ⊆ preimage m b : preimage_mono h) (assume b (hb : preimage m b ∈ f), ⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩) lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f := map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀s∈ f, m '' s ∈ g) : g ≤ f.map m := assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _ protected lemma push_pull (f : α → β) (F : filter α) (G : filter β) : map f (F ⊓ comap f G) = map f F ⊓ G := begin apply le_antisymm, { calc map f (F ⊓ comap f G) ≤ map f F ⊓ (map f $ comap f G) : map_inf_le ... ≤ map f F ⊓ G : inf_le_inf_left (map f F) map_comap_le }, { rintros U ⟨V, V_in, W, ⟨Z, Z_in, hZ⟩, h⟩, rw ← image_subset_iff at h, use [f '' V, image_mem_map V_in, Z, Z_in], refine subset.trans _ h, have : f '' (V ∩ f ⁻¹' Z) ⊆ f '' (V ∩ W), from image_subset _ (inter_subset_inter_right _ ‹_›), rwa set.push_pull at this } end protected lemma push_pull' (f : α → β) (F : filter α) (G : filter β) : map f (comap f G ⊓ F) = G ⊓ map f F := by simp only [filter.push_pull, inf_comm] section applicative lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α) := mem_singleton a lemma pure_inj : function.injective (pure : α → filter α) := assume a b hab, (filter.ext_iff.1 hab {x | a = x}).1 rfl @[simp] lemma pure_ne_bot {α : Type u} {a : α} : pure a ≠ (⊥ : filter α) := mt empty_in_sets_eq_bot.2 $ not_mem_empty a @[simp] lemma le_pure_iff {f : filter α} {a : α} : f ≤ pure a ↔ {a} ∈ f := ⟨λ h, h singleton_mem_pure_sets, λ h s hs, mem_sets_of_superset h $ singleton_subset_iff.2 hs⟩ lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) := iff.rfl lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, set.seq u t ⊆ s) := by simp only [mem_seq_sets_def, seq_subset, exists_prop, iff_self] lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} : s ∈ (f.map m).seq g ↔ (∃t u, t ∈ g ∧ u ∈ f ∧ ∀x∈u, ∀y∈t, m x y ∈ s) := iff.intro (assume ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, assume a, hts _⟩) (assume ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, assume f ⟨a, has, eq⟩, eq ▸ hts _ has⟩) lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α} (hs : s ∈ f) (ht : t ∈ g) : s.seq t ∈ f.seq g := ⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩ lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β} (hh : ∀t ∈ f, ∀u ∈ g, set.seq t u ∈ h) : h ≤ seq f g := assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $ assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ := le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht) @[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← singleton_seq, apply seq_mem_seq_sets _ hs, exact singleton_mem_pure_sets }, { refine sets_of_superset (map g f) (image_mem_map ht) _, rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ } end @[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← seq_singleton, exact seq_mem_seq_sets hs singleton_mem_pure_sets }, { refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _, rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ } end @[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) : seq h (seq g x) = seq (seq (map (∘) h) g) x := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)), rw ← set.seq_seq, exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) }, { rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht), rw set.seq_seq, exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv } end lemma prod_map_seq_comm (f : filter α) (g : filter β) : (map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw ← set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu }, { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu } end instance : is_lawful_functor (filter : Type u → Type u) := { id_map := assume α f, map_id, comp_map := assume α β γ f g a, map_map.symm } instance : is_lawful_applicative (filter : Type u → Type u) := { pure_seq_eq_map := assume α β, pure_seq_eq_map, map_pure := assume α β, map_pure, seq_pure := assume α β, seq_pure, seq_assoc := assume α β γ, seq_assoc } instance : is_comm_applicative (filter : Type u → Type u) := ⟨assume α β f g, prod_map_seq_comm f g⟩ lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) : f <*> g = seq f g := rfl end applicative /- bind equations -/ section bind @[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} : s ∈ bind f m ↔ ∃t ∈ f, ∀x ∈ t, s ∈ m x := calc s ∈ bind f m ↔ {a | s ∈ m a} ∈ f : by simp only [bind, mem_map, iff_self, mem_join_sets, mem_set_of_eq] ... ↔ (∃t ∈ f, t ⊆ {a | s ∈ m a}) : exists_sets_subset_iff.symm ... ↔ (∃t ∈ f, ∀x ∈ t, s ∈ m x) : iff.rfl lemma bind_mono {f : filter α} {g h : α → filter β} (h₁ : {a | g a ≤ h a} ∈ f) : bind f g ≤ bind f h := assume x h₂, show (_ ∈ f), by filter_upwards [h₁, h₂] assume s gh' h', gh' h' lemma bind_sup {f g : filter α} {h : α → filter β} : bind (f ⊔ g) h = bind f h ⊔ bind g h := by simp only [bind, sup_join, map_sup, eq_self_iff_true] lemma bind_mono2 {f g : filter α} {h : α → filter β} (h₁ : f ≤ g) : bind f h ≤ bind g h := assume s h', h₁ h' lemma principal_bind {s : set α} {f : α → filter β} : (bind (principal s) f) = (⨆x ∈ s, f x) := show join (map f (principal s)) = (⨆x ∈ s, f x), by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true] end bind section list_traverse /- This is a separate section in order to open `list`, but mostly because of universe equality requirements in `traverse` -/ open list lemma sequence_mono : ∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs | [] [] forall₂.nil := le_refl _ | (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs) variables {α' β' γ' : Type u} {f : β' → filter α'} {s : γ' → set α'} lemma mem_traverse_sets : ∀(fs : list β') (us : list γ'), forall₂ (λb c, s c ∈ f b) fs us → traverse s us ∈ traverse f fs | [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _ | (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs) lemma mem_traverse_sets_iff (fs : list β') (t : set (list α')) : t ∈ traverse f fs ↔ (∃us:list (set α'), forall₂ (λb (s : set α'), s ∈ f b) fs us ∧ sequence us ⊆ t) := begin split, { induction fs generalizing t, case nil { simp only [sequence, mem_pure_sets, imp_self, forall₂_nil_left_iff, exists_eq_left, set.pure_def, singleton_subset_iff, traverse_nil] }, case cons : b fs ih t { assume ht, rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩, rcases ih v hv with ⟨us, hus, hu⟩, exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } }, { rintros ⟨us, hus, hs⟩, exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs } end end list_traverse /-! ### Limits -/ /-- `tendsto` is the generic "limit of a function" predicate. `tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`, the `f`-preimage of `a` is an `l₁` neighborhood. -/ def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂ lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f ⁻¹' s ∈ l₁ := iff.rfl lemma tendsto.eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop} (hf : tendsto f l₁ l₂) (h : ∀ᶠ y in l₂, p y) : ∀ᶠ x in l₁, p (f x) := hf h lemma eventually_eq_of_left_inv_of_right_inv {f : α → β} {g₁ g₂ : β → α} {fa : filter α} {fb : filter β} (hleft : ∀ᶠ x in fa, g₁ (f x) = x) (hright : ∀ᶠ y in fb, f (g₂ y) = y) (htendsto : tendsto g₂ fb fa) : ∀ᶠ y in fb, g₁ y = g₂ y := (htendsto.eventually hleft).mp $ hright.mono $ λ y hr hl, (congr_arg g₁ hr.symm).trans hl lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f := map_le_iff_le_comap lemma tendsto_congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : {x | f₁ x = f₂ x} ∈ l₁) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ := by rw [tendsto, tendsto, map_cong hl] lemma tendsto.congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : {x | f₁ x = f₂ x} ∈ l₁) (h : tendsto f₁ l₁ l₂) : tendsto f₂ l₁ l₂ := (tendsto_congr' hl).1 h theorem tendsto_congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ := tendsto_congr' (univ_mem_sets' h) theorem tendsto.congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ → tendsto f₂ l₁ l₂ := (tendsto_congr h).1 lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y := by simp only [tendsto, map_id, forall_true_iff] {contextual := tt} lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ} (hg : tendsto g y z) (hf : tendsto f x y) : tendsto (g ∘ f) x z := calc map (g ∘ f) x = map g (map f x) : by rw [map_map] ... ≤ map g y : map_mono hf ... ≤ z : hg lemma tendsto_le_left {f : α → β} {x y : filter α} {z : filter β} (h : y ≤ x) : tendsto f x z → tendsto f y z := le_trans (map_mono h) lemma tendsto_le_right {f : α → β} {x : filter α} {y z : filter β} (h₁ : y ≤ z) (h₂ : tendsto f x y) : tendsto f x z := le_trans h₂ h₁ lemma tendsto.ne_bot {f : α → β} {x : filter α} {y : filter β} (h : tendsto f x y) (hx : x ≠ ⊥) : y ≠ ⊥ := ne_bot_of_le_ne_bot (map_ne_bot hx) h lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x) lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} (h : tendsto (f ∘ g) x y) : tendsto f (map g x) y := by rwa [tendsto, map_map] lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} : tendsto f (map g x) y ↔ tendsto (f ∘ g) x y := by rw [tendsto, map_map]; refl lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x := map_comap_le lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} : tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c := ⟨assume h, tendsto_comap.comp h, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩ lemma tendsto_comap'_iff {m : α → β} {f : filter α} {g : filter β} {i : γ → α} (h : range i ∈ f) : tendsto (m ∘ i) (comap i f) g ↔ tendsto m f g := by rw [tendsto, ← map_compose]; simp only [(∘), map_comap h, tendsto] lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f := begin refine le_antisymm (le_trans (comap_mono $ map_le_iff_le_comap.1 hψ) _) (map_le_iff_le_comap.1 hφ), rw [comap_comap_comp, eq, comap_id], exact le_refl _ end lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g := begin refine le_antisymm hφ (le_trans _ (map_mono hψ)), rw [map_map, eq, map_id], exact le_refl _ end lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} : tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ := by simp only [tendsto, le_inf_iff, iff_self] lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_left) h lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_right) h lemma tendsto.inf {f : α → β} {x₁ x₂ : filter α} {y₁ y₂ : filter β} (h₁ : tendsto f x₁ y₁) (h₂ : tendsto f x₂ y₂) : tendsto f (x₁ ⊓ x₂) (y₁ ⊓ y₂) := tendsto_inf.2 ⟨tendsto_inf_left h₁, tendsto_inf_right h₂⟩ lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} : tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) := by simp only [tendsto, iff_self, le_infi_iff] lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) : tendsto f (x i) y → tendsto f (⨅i, x i) y := tendsto_le_left (infi_le _ _) lemma tendsto_principal {f : α → β} {l : filter α} {s : set β} : tendsto f l (principal s) ↔ ∀ᶠ a in l, f a ∈ s := by simp only [tendsto, le_principal_iff, mem_map, iff_self, filter.eventually] lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} : tendsto f (principal s) (principal t) ↔ ∀a∈s, f a ∈ t := by simp only [tendsto, image_subset_iff, le_principal_iff, map_principal, mem_principal_sets]; refl lemma tendsto_pure {f : α → β} {a : filter α} {b : β} : tendsto f a (pure b) ↔ {x | f x = b} ∈ a := by simp only [tendsto, le_pure_iff, mem_map, mem_singleton_iff] lemma tendsto_pure_pure (f : α → β) (a : α) : tendsto f (pure a) (pure (f a)) := tendsto_pure.2 rfl lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λx, b) a (pure b) := tendsto_pure.2 $ univ_mem_sets' $ λ _, rfl lemma tendsto_if {l₁ : filter α} {l₂ : filter β} {f g : α → β} {p : α → Prop} [decidable_pred p] (h₀ : tendsto f (l₁ ⊓ principal p) l₂) (h₁ : tendsto g (l₁ ⊓ principal { x | ¬ p x }) l₂) : tendsto (λ x, if p x then f x else g x) l₁ l₂ := begin revert h₀ h₁, simp only [tendsto_def, mem_inf_principal], intros h₀ h₁ s hs, apply mem_sets_of_superset (inter_mem_sets (h₀ s hs) (h₁ s hs)), rintros x ⟨hp₀, hp₁⟩, simp only [mem_preimage], by_cases h : p x, { rw if_pos h, exact hp₀ h }, rw if_neg h, exact hp₁ h end /-! ### Products of filters -/ section prod variables {s : set α} {t : set β} {f : filter α} {g : filter β} /- The product filter cannot be defined using the monad structure on filters. For example: F := do {x ← seq, y ← top, return (x, y)} hence: s ∈ F ↔ ∃n, [n..∞] × univ ⊆ s G := do {y ← top, x ← seq, return (x, y)} hence: s ∈ G ↔ ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s Now ⋃i, [i..∞] × {i} is in G but not in F. As product filter we want to have F as result. -/ /-- Product of filters. This is the filter generated by cartesian products of elements of the component filters. -/ protected def prod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊓ g.comap prod.snd localized "infix ` ×ᶠ `:60 := filter.prod" in filter lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β} (hs : s ∈ f) (ht : t ∈ g) : set.prod s t ∈ f ×ᶠ g := inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht) lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, set.prod t₁ t₂ ⊆ s) := begin simp only [filter.prod], split, exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩, ⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩, exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩, ⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩ end lemma eventually_prod_iff {p : α × β → Prop} {f : filter α} {g : filter β} : (∀ᶠ x in f ×ᶠ g, p x) ↔ ∃ (pa : α → Prop) (ha : ∀ᶠ x in f, pa x) (pb : β → Prop) (hb : ∀ᶠ y in g, pb y), ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) := by simpa only [set.prod_subset_iff] using @mem_prod_iff α β p f g lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f := tendsto_inf_left tendsto_comap lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g := tendsto_inf_right tendsto_comap lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ} (h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (g ×ᶠ h) := tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 := tendsto_fst.eventually h lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 := tendsto_snd.eventually h lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x) {lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) : ∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 := (ha.prod_inl lb).and (hb.prod_inr la) lemma eventually.curry {la : filter α} {lb : filter β} {p : α × β → Prop} (h : ∀ᶠ x in la.prod lb, p x) : ∀ᶠ x in la, ∀ᶠ y in lb, p (x, y) := begin rcases eventually_prod_iff.1 h with ⟨pa, ha, pb, hb, h⟩, exact ha.mono (λ a ha, hb.mono $ λ b hb, h ha hb) end lemma prod_infi_left {f : ι → filter α} {g : filter β} (i : ι) : (⨅i, f i) ×ᶠ g = (⨅i, (f i) ×ᶠ g) := by rw [filter.prod, comap_infi, infi_inf i]; simp only [filter.prod, eq_self_iff_true] lemma prod_infi_right {f : filter α} {g : ι → filter β} (i : ι) : f ×ᶠ (⨅i, g i) = (⨅i, f ×ᶠ (g i)) := by rw [filter.prod, comap_infi, inf_infi i]; simp only [filter.prod, eq_self_iff_true] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ := inf_le_inf (comap_mono hf) (comap_mono hg) lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} : (comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := by simp only [filter.prod, comap_comap_comp, eq_self_iff_true, comap_inf] lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) := by simp only [filter.prod, comap_comap_comp, (∘), inf_comm, prod.fst_swap, eq_self_iff_true, prod.snd_swap, comap_inf] lemma prod_comm : f ×ᶠ g = map (λp:β×α, (p.2, p.1)) (g ×ᶠ f) := by rw [prod_comm', ← map_swap_eq_comap_swap]; refl lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : (map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := le_antisymm (assume s hs, let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $ calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ : set.prod_image_image_eq ... ⊆ _ : by rwa [image_subset_iff]) ((tendsto.comp (le_refl _) tendsto_fst).prod_mk (tendsto.comp (le_refl _) tendsto_snd)) lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) : map m (f.prod g) = (f.map (λa b, m (a, b))).seq g := begin simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff], assume s, split, exact assume ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, assume x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩, exact assume ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, assume ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩ end lemma prod_eq {f : filter α} {g : filter β} : f.prod g = (f.map prod.mk).seq g := have h : _ := map_prod id f g, by rwa [map_id] at h lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} : (f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) := by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm] @[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod] @[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod] @[simp] lemma prod_principal_principal {s : set α} {t : set β} : (principal s) ×ᶠ (principal t) = principal (set.prod s t) := by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl @[simp] lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) := by simp [pure_eq_principal] lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) := begin split, { assume h, rcases mem_prod_iff.1 (empty_in_sets_eq_bot.2 h) with ⟨s, hs, t, ht, hst⟩, rw [subset_empty_iff, set.prod_eq_empty_iff] at hst, cases hst with s_eq t_eq, { left, exact empty_in_sets_eq_bot.1 (s_eq ▸ hs) }, { right, exact empty_in_sets_eq_bot.1 (t_eq ▸ ht) } }, { rintros (rfl | rfl), exact bot_prod, exact prod_bot } end lemma prod_ne_bot {f : filter α} {g : filter β} : f ×ᶠ g ≠ ⊥ ↔ (f ≠ ⊥ ∧ g ≠ ⊥) := by rw [(≠), prod_eq_bot, not_or_distrib] lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} : filter.tendsto f (x ×ᶠ y) z ↔ ∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W := by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self] end prod /-! ### at_top and at_bot filters on preorded sets, monoids and groups. -/ /-- `at_top` is the filter representing the limit `→ ∞` on an ordered set. It is generated by the collection of up-sets `{b | a ≤ b}`. (The preorder need not have a top element for this to be well defined, and indeed is trivial when a top element exists.) -/ def at_top [preorder α] : filter α := ⨅ a, principal {b | a ≤ b} /-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set. It is generated by the collection of down-sets `{b | b ≤ a}`. (The preorder need not have a bottom element for this to be well defined, and indeed is trivial when a bottom element exists.) -/ def at_bot [preorder α] : filter α := ⨅ a, principal {b | b ≤ a} lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ @at_top α _ := mem_infi_sets a $ subset.refl _ @[simp] lemma at_top_ne_bot [nonempty α] [semilattice_sup α] : (at_top : filter α) ≠ ⊥ := infi_ne_bot_of_directed (by apply_instance) (assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of, mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩) (assume a, principal_ne_bot_iff.2 nonempty_Ici) @[simp, nolint ge_or_gt] lemma mem_at_top_sets [nonempty α] [semilattice_sup α] {s : set α} : s ∈ (at_top : filter α) ↔ ∃a:α, ∀b≥a, b ∈ s := let ⟨a⟩ := ‹nonempty α› in iff.intro (assume h, infi_sets_induct h ⟨a, by simp only [forall_const, mem_univ, forall_true_iff]⟩ (assume a s₁ s₂ ha ⟨b, hb⟩, ⟨a ⊔ b, assume c hc, ⟨ha $ le_trans le_sup_left hc, hb _ $ le_trans le_sup_right hc⟩⟩) (assume s₁ s₂ h ⟨a, ha⟩, ⟨a, assume b hb, h $ ha _ hb⟩)) (assume ⟨a, h⟩, mem_infi_sets a $ assume x, h x) @[nolint ge_or_gt] lemma eventually_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} : (∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ b ≥ a, p b) := by simp only [filter.eventually, filter.mem_at_top_sets, mem_set_of_eq] @[nolint ge_or_gt] lemma eventually.exists_forall_of_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} (h : ∀ᶠ x in at_top, p x) : ∃ a, ∀ b ≥ a, p b := eventually_at_top.mp h @[nolint ge_or_gt] lemma frequently_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} : (∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b ≥ a, p b) := by simp only [filter.frequently, eventually_at_top, not_exists, not_forall, not_not] @[nolint ge_or_gt] lemma frequently_at_top' {α} [semilattice_sup α] [nonempty α] [no_top_order α] {p : α → Prop} : (∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b > a, p b) := begin rw frequently_at_top, split ; intros h a, { cases no_top a with a' ha', rcases h a' with ⟨b, hb, hb'⟩, exact ⟨b, lt_of_lt_of_le ha' hb, hb'⟩ }, { rcases h a with ⟨b, hb, hb'⟩, exact ⟨b, le_of_lt hb, hb'⟩ }, end @[nolint ge_or_gt] lemma frequently.forall_exists_of_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} (h : ∃ᶠ x in at_top, p x) : ∀ a, ∃ b ≥ a, p b := frequently_at_top.mp h lemma map_at_top_eq [nonempty α] [semilattice_sup α] {f : α → β} : at_top.map f = (⨅a, principal $ f '' {a' | a ≤ a'}) := calc map f (⨅a, principal {a' | a ≤ a'}) = (⨅a, map f $ principal {a' | a ≤ a'}) : map_infi_eq (assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of, mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩) (by apply_instance) ... = (⨅a, principal $ f '' {a' | a ≤ a'}) : by simp only [map_principal, eq_self_iff_true] lemma tendsto_at_top [preorder β] (m : α → β) (f : filter α) : tendsto m f at_top ↔ (∀b, {a | b ≤ m a} ∈ f) := by simp only [at_top, tendsto_infi, tendsto_principal]; refl lemma tendsto_at_top_mono' [preorder β] (l : filter α) ⦃f₁ f₂ : α → β⦄ (h : {x | f₁ x ≤ f₂ x} ∈ l) : tendsto f₁ l at_top → tendsto f₂ l at_top := assume h₁, (tendsto_at_top _ _).2 $ λ b, mp_sets ((tendsto_at_top _ _).1 h₁ b) (monotone_mem_sets (λ a ha ha₁, le_trans ha₁ ha) h) lemma tendsto_at_top_mono [preorder β] (l : filter α) : monotone (λ f : α → β, tendsto f l at_top) := λ f₁ f₂ h, tendsto_at_top_mono' l $ univ_mem_sets' h @[nolint ge_or_gt] -- see Note [nolint_ge] lemma map_at_top_inf_ne_bot_iff [semilattice_sup α] [nonempty α] {F : filter β} {u : α → β} : (map u at_top) ⊓ F ≠ ⊥ ↔ ∀ U ∈ F, ∀ N, ∃ n ≥ N, u n ∈ U := by simp_rw [inf_ne_bot_iff_frequently_right, frequently_map, frequently_at_top] ; trivial section ordered_add_monoid variables [ordered_cancel_add_comm_monoid β] (l : filter α) {f g : α → β} lemma tendsto_at_top_add_nonneg_left' (hf : {x | 0 ≤ f x} ∈ l) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_mono' l (monotone_mem_sets (λ x, le_add_of_nonneg_left) hf) hg lemma tendsto_at_top_add_nonneg_left (hf : ∀ x, 0 ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_left' l (univ_mem_sets' hf) hg lemma tendsto_at_top_add_nonneg_right' (hf : tendsto f l at_top) (hg : {x | 0 ≤ g x} ∈ l) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_mono' l (monotone_mem_sets (λ x, le_add_of_nonneg_right) hg) hf lemma tendsto_at_top_add_nonneg_right (hf : tendsto f l at_top) (hg : ∀ x, 0 ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_right' l hf (univ_mem_sets' hg) lemma tendsto_at_top_of_add_const_left (C : β) (hf : tendsto (λ x, C + f x) l at_top) : tendsto f l at_top := (tendsto_at_top _ l).2 $ assume b, monotone_mem_sets (λ x, le_of_add_le_add_left) ((tendsto_at_top _ _).1 hf (C + b)) lemma tendsto_at_top_of_add_const_right (C : β) (hf : tendsto (λ x, f x + C) l at_top) : tendsto f l at_top := (tendsto_at_top _ l).2 $ assume b, monotone_mem_sets (λ x, le_of_add_le_add_right) ((tendsto_at_top _ _).1 hf (b + C)) lemma tendsto_at_top_of_add_bdd_above_left' (C) (hC : {x | f x ≤ C} ∈ l) (h : tendsto (λ x, f x + g x) l at_top) : tendsto g l at_top := tendsto_at_top_of_add_const_left l C (tendsto_at_top_mono' l (monotone_mem_sets (λ x (hx : f x ≤ C), add_le_add_right hx (g x)) hC) h) lemma tendsto_at_top_of_add_bdd_above_left (C) (hC : ∀ x, f x ≤ C) : tendsto (λ x, f x + g x) l at_top → tendsto g l at_top := tendsto_at_top_of_add_bdd_above_left' l C (univ_mem_sets' hC) lemma tendsto_at_top_of_add_bdd_above_right' (C) (hC : {x | g x ≤ C} ∈ l) (h : tendsto (λ x, f x + g x) l at_top) : tendsto f l at_top := tendsto_at_top_of_add_const_right l C (tendsto_at_top_mono' l (monotone_mem_sets (λ x (hx : g x ≤ C), add_le_add_left hx (f x)) hC) h) lemma tendsto_at_top_of_add_bdd_above_right (C) (hC : ∀ x, g x ≤ C) : tendsto (λ x, f x + g x) l at_top → tendsto f l at_top := tendsto_at_top_of_add_bdd_above_right' l C (univ_mem_sets' hC) end ordered_add_monoid section ordered_group variables [ordered_add_comm_group β] (l : filter α) {f g : α → β} lemma tendsto_at_top_add_left_of_le' (C : β) (hf : {x | C ≤ f x} ∈ l) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := @tendsto_at_top_of_add_bdd_above_left' _ _ _ l (λ x, -(f x)) (λ x, f x + g x) (-C) (by simp [hf]) (by simp [hg]) lemma tendsto_at_top_add_left_of_le (C : β) (hf : ∀ x, C ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_left_of_le' l C (univ_mem_sets' hf) hg lemma tendsto_at_top_add_right_of_le' (C : β) (hf : tendsto f l at_top) (hg : {x | C ≤ g x} ∈ l) : tendsto (λ x, f x + g x) l at_top := @tendsto_at_top_of_add_bdd_above_right' _ _ _ l (λ x, f x + g x) (λ x, -(g x)) (-C) (by simp [hg]) (by simp [hf]) lemma tendsto_at_top_add_right_of_le (C : β) (hf : tendsto f l at_top) (hg : ∀ x, C ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_right_of_le' l C hf (univ_mem_sets' hg) lemma tendsto_at_top_add_const_left (C : β) (hf : tendsto f l at_top) : tendsto (λ x, C + f x) l at_top := tendsto_at_top_add_left_of_le' l C (univ_mem_sets' $ λ _, le_refl C) hf lemma tendsto_at_top_add_const_right (C : β) (hf : tendsto f l at_top) : tendsto (λ x, f x + C) l at_top := tendsto_at_top_add_right_of_le' l C hf (univ_mem_sets' $ λ _, le_refl C) end ordered_group open_locale filter @[nolint ge_or_gt] lemma tendsto_at_top' [nonempty α] [semilattice_sup α] (f : α → β) (l : filter β) : tendsto f at_top l ↔ (∀s ∈ l, ∃a, ∀b≥a, f b ∈ s) := by simp only [tendsto_def, mem_at_top_sets]; refl @[nolint ge_or_gt] theorem tendsto_at_top_principal [nonempty β] [semilattice_sup β] {f : β → α} {s : set α} : tendsto f at_top (principal s) ↔ ∃N, ∀n≥N, f n ∈ s := by rw [tendsto_iff_comap, comap_principal, le_principal_iff, mem_at_top_sets]; refl /-- A function `f` grows to infinity independent of an order-preserving embedding `e`. -/ lemma tendsto_at_top_embedding {α β γ : Type*} [preorder β] [preorder γ] {f : α → β} {e : β → γ} {l : filter α} (hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) : tendsto (e ∘ f) l at_top ↔ tendsto f l at_top := begin rw [tendsto_at_top, tendsto_at_top], split, { assume hc b, filter_upwards [hc (e b)] assume a, (hm b (f a)).1 }, { assume hb c, rcases hu c with ⟨b, hc⟩, filter_upwards [hb b] assume a ha, le_trans hc ((hm b (f a)).2 ha) } end lemma tendsto_at_top_at_top [nonempty α] [semilattice_sup α] [preorder β] (f : α → β) : tendsto f at_top at_top ↔ ∀ b : β, ∃ i : α, ∀ a : α, i ≤ a → b ≤ f a := iff.trans tendsto_infi $ forall_congr $ assume b, tendsto_at_top_principal @[nolint ge_or_gt] lemma tendsto_at_top_at_bot [nonempty α] [decidable_linear_order α] [preorder β] (f : α → β) : tendsto f at_top at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), i ≤ a → b ≥ f a := @tendsto_at_top_at_top α (order_dual β) _ _ _ f lemma tendsto_at_top_at_top_of_monotone [nonempty α] [semilattice_sup α] [preorder β] {f : α → β} (hf : monotone f) : tendsto f at_top at_top ↔ ∀ b : β, ∃ a : α, b ≤ f a := (tendsto_at_top_at_top f).trans $ forall_congr $ λ b, exists_congr $ λ a, ⟨λ h, h a (le_refl a), λ h a' ha', le_trans h $ hf ha'⟩ alias tendsto_at_top_at_top_of_monotone ← monotone.tendsto_at_top_at_top lemma tendsto_finset_range : tendsto finset.range at_top at_top := finset.range_mono.tendsto_at_top_at_top.2 finset.exists_nat_subset_range lemma monotone.tendsto_at_top_finset [nonempty β] [semilattice_sup β] {f : β → finset α} (h : monotone f) (h' : ∀ x : α, ∃ n, x ∈ f n) : tendsto f at_top at_top := begin classical, apply (tendsto_at_top_at_top_of_monotone h).2, choose N hN using h', assume b, have : bdd_above ↑(b.image N) := finset.bdd_above _, rcases this with ⟨n, hn⟩, refine ⟨n, _⟩, assume i ib, have : N i ∈ ↑(finset.image N b), by { rw finset.mem_coe, exact finset.mem_image_of_mem _ ib }, exact (h (hn this)) (hN i) end lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : ∀x, j (i x) = x) : tendsto (finset.image j) at_top at_top := have j ∘ i = id, from funext h, (finset.image_mono j).tendsto_at_top_at_top.2 $ assume s, ⟨s.image i, by simp only [finset.image_image, this, finset.image_id, le_refl]⟩ lemma prod_at_top_at_top_eq {β₁ β₂ : Type*} [nonempty β₁] [nonempty β₂] [semilattice_sup β₁] [semilattice_sup β₂] : (@at_top β₁ _) ×ᶠ (@at_top β₂ _) = @at_top (β₁ × β₂) _ := by inhabit β₁; inhabit β₂; simp [at_top, prod_infi_left (default β₁), prod_infi_right (default β₂), infi_prod]; exact infi_comm lemma prod_map_at_top_eq {α₁ α₂ β₁ β₂ : Type*} [nonempty β₁] [nonempty β₂] [semilattice_sup β₁] [semilattice_sup β₂] (u₁ : β₁ → α₁) (u₂ : β₂ → α₂) : (map u₁ at_top) ×ᶠ (map u₂ at_top) = map (prod.map u₁ u₂) at_top := by rw [prod_map_map_eq, prod_at_top_at_top_eq, prod.map_def] /-- A function `f` maps upwards closed sets (at_top sets) to upwards closed sets when it is a Galois insertion. The Galois "insertion" and "connection" is weakened to only require it to be an insertion and a connetion above `b'`. -/ lemma map_at_top_eq_of_gc [semilattice_sup α] [semilattice_sup β] {f : α → β} (g : β → α) (b' : β) (hf : monotone f) (gc : ∀a, ∀b≥b', f a ≤ b ↔ a ≤ g b) (hgi : ∀b≥b', b ≤ f (g b)) : map f at_top = at_top := begin rw [@map_at_top_eq α _ ⟨g b'⟩], refine le_antisymm (le_infi $ assume b, infi_le_of_le (g (b ⊔ b')) $ principal_mono.2 $ image_subset_iff.2 _) (le_infi $ assume a, infi_le_of_le (f a ⊔ b') $ principal_mono.2 _), { assume a ha, exact (le_trans le_sup_left $ le_trans (hgi _ le_sup_right) $ hf ha) }, { assume b hb, have hb' : b' ≤ b := le_trans le_sup_right hb, exact ⟨g b, (gc _ _ hb').1 (le_trans le_sup_left hb), le_antisymm ((gc _ _ hb').2 (le_refl _)) (hgi _ hb')⟩ } end lemma map_add_at_top_eq_nat (k : ℕ) : map (λa, a + k) at_top = at_top := map_at_top_eq_of_gc (λa, a - k) k (assume a b h, add_le_add_right h k) (assume a b h, (nat.le_sub_right_iff_add_le h).symm) (assume a h, by rw [nat.sub_add_cancel h]) lemma map_sub_at_top_eq_nat (k : ℕ) : map (λa, a - k) at_top = at_top := map_at_top_eq_of_gc (λa, a + k) 0 (assume a b h, nat.sub_le_sub_right h _) (assume a b _, nat.sub_le_right_iff_le_add) (assume b _, by rw [nat.add_sub_cancel]) lemma tendsto_add_at_top_nat (k : ℕ) : tendsto (λa, a + k) at_top at_top := le_of_eq (map_add_at_top_eq_nat k) lemma tendsto_sub_at_top_nat (k : ℕ) : tendsto (λa, a - k) at_top at_top := le_of_eq (map_sub_at_top_eq_nat k) lemma tendsto_add_at_top_iff_nat {f : ℕ → α} {l : filter α} (k : ℕ) : tendsto (λn, f (n + k)) at_top l ↔ tendsto f at_top l := show tendsto (f ∘ (λn, n + k)) at_top l ↔ tendsto f at_top l, by rw [← tendsto_map'_iff, map_add_at_top_eq_nat] lemma map_div_at_top_eq_nat (k : ℕ) (hk : k > 0) : map (λa, a / k) at_top = at_top := map_at_top_eq_of_gc (λb, b * k + (k - 1)) 1 (assume a b h, nat.div_le_div_right h) (assume a b _, calc a / k ≤ b ↔ a / k < b + 1 : by rw [← nat.succ_eq_add_one, nat.lt_succ_iff] ... ↔ a < (b + 1) * k : nat.div_lt_iff_lt_mul _ _ hk ... ↔ _ : begin cases k, exact (lt_irrefl _ hk).elim, simp [mul_add, add_mul, nat.succ_add, nat.lt_succ_iff] end) (assume b _, calc b = (b * k) / k : by rw [nat.mul_div_cancel b hk] ... ≤ (b * k + (k - 1)) / k : nat.div_le_div_right $ nat.le_add_right _ _) /-! ### The cofinite filter -/ /-- The cofinite filter is the filter of subsets whose complements are finite. -/ def cofinite : filter α := { sets := {s | finite (- s)}, univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq], sets_of_superset := assume s t (hs : finite (-s)) (st: s ⊆ t), finite_subset hs $ compl_subset_compl.2 st, inter_sets := assume s t (hs : finite (-s)) (ht : finite (-t)), by simp only [compl_inter, finite_union, ht, hs, mem_set_of_eq] } @[simp] lemma mem_cofinite {s : set α} : s ∈ (@cofinite α) ↔ finite (-s) := iff.rfl lemma cofinite_ne_bot [infinite α] : @cofinite α ≠ ⊥ := mt empty_in_sets_eq_bot.mpr $ by { simp only [mem_cofinite, compl_empty], exact infinite_univ } lemma frequently_cofinite_iff_infinite {p : α → Prop} : (∃ᶠ x in cofinite, p x) ↔ set.infinite {x | p x} := by simp only [filter.frequently, filter.eventually, mem_cofinite, compl_set_of, not_not, set.infinite] lemma set.infinite_iff_frequently_cofinite {α : Type u} {s : set α} : set.infinite s ↔ (∃ᶠ x in cofinite, x ∈ s) := frequently_cofinite_iff_infinite.symm /-- For natural numbers the filters `cofinite` and `at_top` coincide. -/ lemma nat.cofinite_eq_at_top : @cofinite ℕ = at_top := begin ext s, simp only [mem_cofinite, mem_at_top_sets], split, { assume hs, use (hs.to_finset.sup id) + 1, assume b hb, by_contradiction hbs, have := hs.to_finset.subset_range_sup_succ (finite.mem_to_finset.2 hbs), exact not_lt_of_le hb (finset.mem_range.1 this) }, { rintros ⟨N, hN⟩, apply finite_subset (finite_lt_nat N), assume n hn, change n < N, exact lt_of_not_ge (λ hn', hn $ hN n hn') } end /-! ### Ultrafilters -/ section ultrafilter open zorn variables {f g : filter α} /-- An ultrafilter is a minimal (maximal in the set order) proper filter. -/ def is_ultrafilter (f : filter α) := f ≠ ⊥ ∧ ∀g, g ≠ ⊥ → g ≤ f → f ≤ g lemma ultrafilter_unique (hg : is_ultrafilter g) (hf : f ≠ ⊥) (h : f ≤ g) : f = g := le_antisymm h (hg.right _ hf h) lemma le_of_ultrafilter {g : filter α} (hf : is_ultrafilter f) (h : f ⊓ g ≠ ⊥) : f ≤ g := le_of_inf_eq $ ultrafilter_unique hf h inf_le_left /-- Equivalent characterization of ultrafilters: A filter f is an ultrafilter if and only if for each set s, -s belongs to f if and only if s does not belong to f. -/ lemma ultrafilter_iff_compl_mem_iff_not_mem : is_ultrafilter f ↔ (∀ s, -s ∈ f ↔ s ∉ f) := ⟨assume hf s, ⟨assume hns hs, hf.1 $ empty_in_sets_eq_bot.mp $ by convert f.inter_sets hs hns; rw [inter_compl_self], assume hs, have f ≤ principal (-s), from le_of_ultrafilter hf $ assume h, hs $ mem_sets_of_eq_bot $ by simp only [h, eq_self_iff_true, compl_compl], by simp only [le_principal_iff] at this; assumption⟩, assume hf, ⟨mt empty_in_sets_eq_bot.mpr ((hf ∅).mp (by convert f.univ_sets; rw [compl_empty])), assume g hg g_le s hs, classical.by_contradiction $ mt (hf s).mpr $ assume : - s ∈ f, have s ∩ -s ∈ g, from inter_mem_sets hs (g_le this), by simp only [empty_in_sets_eq_bot, hg, inter_compl_self] at this; contradiction⟩⟩ lemma mem_or_compl_mem_of_ultrafilter (hf : is_ultrafilter f) (s : set α) : s ∈ f ∨ - s ∈ f := classical.or_iff_not_imp_left.2 (ultrafilter_iff_compl_mem_iff_not_mem.mp hf s).mpr lemma mem_or_mem_of_ultrafilter {s t : set α} (hf : is_ultrafilter f) (h : s ∪ t ∈ f) : s ∈ f ∨ t ∈ f := (mem_or_compl_mem_of_ultrafilter hf s).imp_right (assume : -s ∈ f, by filter_upwards [this, h] assume x hnx hx, hx.resolve_left hnx) lemma mem_of_finite_sUnion_ultrafilter {s : set (set α)} (hf : is_ultrafilter f) (hs : finite s) : ⋃₀ s ∈ f → ∃t∈s, t ∈ f := finite.induction_on hs (by simp only [empty_in_sets_eq_bot, hf.left, mem_empty_eq, sUnion_empty, forall_prop_of_false, exists_false, not_false_iff, exists_prop_of_false]) $ λ t s' ht' hs' ih, by simp only [exists_prop, mem_insert_iff, set.sUnion_insert]; exact assume h, (mem_or_mem_of_ultrafilter hf h).elim (assume : t ∈ f, ⟨t, or.inl rfl, this⟩) (assume h, let ⟨t, hts', ht⟩ := ih h in ⟨t, or.inr hts', ht⟩) lemma mem_of_finite_Union_ultrafilter {is : set β} {s : β → set α} (hf : is_ultrafilter f) (his : finite is) (h : (⋃i∈is, s i) ∈ f) : ∃i∈is, s i ∈ f := have his : finite (image s is), from finite_image s his, have h : (⋃₀ image s is) ∈ f, from by simp only [sUnion_image, set.sUnion_image]; assumption, let ⟨t, ⟨i, hi, h_eq⟩, (ht : t ∈ f)⟩ := mem_of_finite_sUnion_ultrafilter hf his h in ⟨i, hi, h_eq.symm ▸ ht⟩ lemma ultrafilter_map {f : filter α} {m : α → β} (h : is_ultrafilter f) : is_ultrafilter (map m f) := by rw ultrafilter_iff_compl_mem_iff_not_mem at ⊢ h; exact assume s, h (m ⁻¹' s) lemma ultrafilter_pure {a : α} : is_ultrafilter (pure a) := begin rw ultrafilter_iff_compl_mem_iff_not_mem, intro s, rw [mem_pure_sets, mem_pure_sets], exact iff.rfl end lemma ultrafilter_bind {f : filter α} (hf : is_ultrafilter f) {m : α → filter β} (hm : ∀ a, is_ultrafilter (m a)) : is_ultrafilter (f.bind m) := begin simp only [ultrafilter_iff_compl_mem_iff_not_mem] at ⊢ hf hm, intro s, dsimp [bind, join, map, preimage], simp only [hm], apply hf end /-- The ultrafilter lemma: Any proper filter is contained in an ultrafilter. -/ lemma exists_ultrafilter (h : f ≠ ⊥) : ∃u, u ≤ f ∧ is_ultrafilter u := let τ := {f' // f' ≠ ⊥ ∧ f' ≤ f}, r : τ → τ → Prop := λt₁ t₂, t₂.val ≤ t₁.val, ⟨a, ha⟩ := nonempty_of_mem_sets h univ_mem_sets, top : τ := ⟨f, h, le_refl f⟩, sup : Π(c:set τ), chain r c → τ := λc hc, ⟨⨅a:{a:τ // a ∈ insert top c}, a.val.val, infi_ne_bot_of_directed ⟨a⟩ (directed_of_chain $ chain_insert hc $ assume ⟨b, _, hb⟩ _ _, or.inl hb) (assume ⟨⟨a, ha, _⟩, _⟩, ha), infi_le_of_le ⟨top, mem_insert _ _⟩ (le_refl _)⟩ in have ∀c (hc: chain r c) a (ha : a ∈ c), r a (sup c hc), from assume c hc a ha, infi_le_of_le ⟨a, mem_insert_of_mem _ ha⟩ (le_refl _), have (∃ (u : τ), ∀ (a : τ), r u a → r a u), from exists_maximal_of_chains_bounded (assume c hc, ⟨sup c hc, this c hc⟩) (assume f₁ f₂ f₃ h₁ h₂, le_trans h₂ h₁), let ⟨uτ, hmin⟩ := this in ⟨uτ.val, uτ.property.right, uτ.property.left, assume g hg₁ hg₂, hmin ⟨g, hg₁, le_trans hg₂ uτ.property.right⟩ hg₂⟩ /-- Construct an ultrafilter extending a given filter. The ultrafilter lemma is the assertion that such a filter exists; we use the axiom of choice to pick one. -/ noncomputable def ultrafilter_of (f : filter α) : filter α := if h : f = ⊥ then ⊥ else classical.epsilon (λu, u ≤ f ∧ is_ultrafilter u) lemma ultrafilter_of_spec (h : f ≠ ⊥) : ultrafilter_of f ≤ f ∧ is_ultrafilter (ultrafilter_of f) := begin have h' := classical.epsilon_spec (exists_ultrafilter h), simp only [ultrafilter_of, dif_neg, h, dif_neg, not_false_iff], simp only at h', assumption end lemma ultrafilter_of_le : ultrafilter_of f ≤ f := if h : f = ⊥ then by simp only [ultrafilter_of, dif_pos, h, dif_pos, eq_self_iff_true, le_bot_iff]; exact le_refl _ else (ultrafilter_of_spec h).left lemma ultrafilter_ultrafilter_of (h : f ≠ ⊥) : is_ultrafilter (ultrafilter_of f) := (ultrafilter_of_spec h).right lemma ultrafilter_of_ultrafilter (h : is_ultrafilter f) : ultrafilter_of f = f := ultrafilter_unique h (ultrafilter_ultrafilter_of h.left).left ultrafilter_of_le /-- A filter equals the intersection of all the ultrafilters which contain it. -/ lemma sup_of_ultrafilters (f : filter α) : f = ⨆ (g) (u : is_ultrafilter g) (H : g ≤ f), g := begin refine le_antisymm _ (supr_le $ λ g, supr_le $ λ u, supr_le $ λ H, H), intros s hs, -- If s ∉ f.sets, we'll apply the ultrafilter lemma to the restriction of f to -s. by_contradiction hs', let j : (-s) → α := subtype.val, have j_inv_s : j ⁻¹' s = ∅, by erw [←preimage_inter_range, subtype.val_range, inter_compl_self, preimage_empty], let f' := comap j f, have : f' ≠ ⊥, { apply mt empty_in_sets_eq_bot.mpr, rintro ⟨t, htf, ht⟩, suffices : t ⊆ s, from absurd (f.sets_of_superset htf this) hs', rw [subset_empty_iff] at ht, have : j '' (j ⁻¹' t) = ∅, by rw [ht, image_empty], erw [image_preimage_eq_inter_range, subtype.val_range, ←subset_compl_iff_disjoint, set.compl_compl] at this, exact this }, rcases exists_ultrafilter this with ⟨g', g'f', u'⟩, simp only [supr_sets_eq, mem_Inter] at hs, have := hs (g'.map subtype.val) (ultrafilter_map u') (map_le_iff_le_comap.mpr g'f'), rw [←le_principal_iff, map_le_iff_le_comap, comap_principal, j_inv_s, principal_empty, le_bot_iff] at this, exact absurd this u'.1 end /-- The `tendsto` relation can be checked on ultrafilters. -/ lemma tendsto_iff_ultrafilter (f : α → β) (l₁ : filter α) (l₂ : filter β) : tendsto f l₁ l₂ ↔ ∀ g, is_ultrafilter g → g ≤ l₁ → g.map f ≤ l₂ := ⟨assume h g u gx, le_trans (map_mono gx) h, assume h, by rw [sup_of_ultrafilters l₁]; simpa only [tendsto, map_supr, supr_le_iff]⟩ /-- The ultrafilter monad. The monad structure on ultrafilters is the restriction of the one on filters. -/ def ultrafilter (α : Type u) : Type u := {f : filter α // is_ultrafilter f} /-- Push-forward for ultra-filters. -/ def ultrafilter.map (m : α → β) (u : ultrafilter α) : ultrafilter β := ⟨u.val.map m, ultrafilter_map u.property⟩ /-- The principal ultra-filter associated to a point `x`. -/ def ultrafilter.pure (x : α) : ultrafilter α := ⟨pure x, ultrafilter_pure⟩ /-- Monadic bind for ultra-filters, coming from the one on filters defined in terms of map and join.-/ def ultrafilter.bind (u : ultrafilter α) (m : α → ultrafilter β) : ultrafilter β := ⟨u.val.bind (λ a, (m a).val), ultrafilter_bind u.property (λ a, (m a).property)⟩ instance ultrafilter.has_pure : has_pure ultrafilter := ⟨@ultrafilter.pure⟩ 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 } instance ultrafilter.inhabited [inhabited α] : inhabited (ultrafilter α) := ⟨pure (default _)⟩ /-- The ultra-filter extending the cofinite filter. -/ noncomputable def hyperfilter : filter α := ultrafilter_of cofinite lemma hyperfilter_le_cofinite : @hyperfilter α ≤ cofinite := ultrafilter_of_le lemma is_ultrafilter_hyperfilter [infinite α] : is_ultrafilter (@hyperfilter α) := (ultrafilter_of_spec cofinite_ne_bot).2 theorem nmem_hyperfilter_of_finite [infinite α] {s : set α} (hf : s.finite) : s ∉ @hyperfilter α := λ hy, have hx : -s ∉ hyperfilter := λ hs, (ultrafilter_iff_compl_mem_iff_not_mem.mp is_ultrafilter_hyperfilter s).mp hs hy, have ht : -s ∈ cofinite.sets := by show -s ∈ {s | _}; rwa [set.mem_set_of_eq, compl_compl], hx $ hyperfilter_le_cofinite ht theorem compl_mem_hyperfilter_of_finite [infinite α] {s : set α} (hf : set.finite s) : -s ∈ @hyperfilter α := (ultrafilter_iff_compl_mem_iff_not_mem.mp is_ultrafilter_hyperfilter s).mpr $ nmem_hyperfilter_of_finite hf theorem mem_hyperfilter_of_finite_compl [infinite α] {s : set α} (hf : set.finite (-s)) : s ∈ @hyperfilter α := s.compl_compl ▸ compl_mem_hyperfilter_of_finite hf section local attribute [instance] filter.monad filter.is_lawful_monad instance ultrafilter.is_lawful_monad : is_lawful_monad ultrafilter := { id_map := assume α f, subtype.eq (id_map f.val), pure_bind := assume α β a f, subtype.eq (pure_bind a (subtype.val ∘ f)), bind_assoc := assume α β γ f m₁ m₂, subtype.eq (filter_eq rfl), bind_pure_comp_eq_map := assume α β f x, subtype.eq (bind_pure_comp_eq_map f x.val) } end lemma ultrafilter.eq_iff_val_le_val {u v : ultrafilter α} : u = v ↔ u.val ≤ v.val := ⟨assume h, by rw h; exact le_refl _, assume h, by rw subtype.ext; apply ultrafilter_unique v.property u.property.1 h⟩ lemma exists_ultrafilter_iff (f : filter α) : (∃ (u : ultrafilter α), u.val ≤ f) ↔ f ≠ ⊥ := ⟨assume ⟨u, uf⟩, ne_bot_of_le_ne_bot u.property.1 uf, assume h, let ⟨u, uf, hu⟩ := exists_ultrafilter h in ⟨⟨u, hu⟩, uf⟩⟩ end ultrafilter end filter
611f563a238f8d881ae5c78716a218608cda34d9
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/probability/density.lean
5545c5925759df88a89576002c15ab63b65c6737
[ "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
19,163
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.decomposition.radon_nikodym import measure_theory.measure.lebesgue /-! # Probability density function This file defines the probability density function of random variables, by which we mean measurable functions taking values in a Borel space. In particular, a measurable function `f` is said to the probability density function of a random variable `X` if for all measurable sets `S`, `ℙ(X ∈ S) = ∫ x in S, f x dx`. Probability density functions are one way of describing the distribution of a random variable, and are useful for calculating probabilities and finding moments (although the latter is better achieved with moment generating functions). This file also defines the continuous uniform distribution and proves some properties about random variables with this distribution. ## Main definitions * `measure_theory.has_pdf` : A random variable `X : Ω → E` is said to `has_pdf` with respect to the measure `ℙ` on `Ω` and `μ` on `E` if there exists a measurable function `f` such that the push-forward measure of `ℙ` along `X` equals `μ.with_density f`. * `measure_theory.pdf` : If `X` is a random variable that `has_pdf X ℙ μ`, then `pdf X` is the measurable function `f` such that the push-forward measure of `ℙ` along `X` equals `μ.with_density f`. * `measure_theory.pdf.uniform` : A random variable `X` is said to follow the uniform distribution if it has a constant probability density function with a compact, non-null support. ## Main results * `measure_theory.pdf.integral_fun_mul_eq_integral` : Law of the unconscious statistician, i.e. if a random variable `X : Ω → E` has pdf `f`, then `𝔼(g(X)) = ∫ x, g x * f x dx` for all measurable `g : E → ℝ`. * `measure_theory.pdf.integral_mul_eq_integral` : A real-valued random variable `X` with pdf `f` has expectation `∫ x, x * f x dx`. * `measure_theory.pdf.uniform.integral_eq` : If `X` follows the uniform distribution with its pdf having support `s`, then `X` has expectation `(λ s)⁻¹ * ∫ x in s, x dx` where `λ` is the Lebesgue measure. ## TODOs Ultimately, we would also like to define characteristic functions to describe distributions as it exists for all random variables. However, to define this, we will need Fourier transforms which we currently do not have. -/ noncomputable theory open_locale classical measure_theory nnreal ennreal namespace measure_theory open topological_space measure_theory.measure variables {Ω E : Type*} [measurable_space E] /-- A random variable `X : Ω → E` is said to `has_pdf` with respect to the measure `ℙ` on `Ω` and `μ` on `E` if there exists a measurable function `f` such that the push-forward measure of `ℙ` along `X` equals `μ.with_density f`. -/ class has_pdf {m : measurable_space Ω} (X : Ω → E) (ℙ : measure Ω) (μ : measure E . volume_tac) : Prop := (pdf' : measurable X ∧ ∃ (f : E → ℝ≥0∞), measurable f ∧ map X ℙ = μ.with_density f) @[measurability] lemma has_pdf.measurable {m : measurable_space Ω} (X : Ω → E) (ℙ : measure Ω) (μ : measure E . volume_tac) [hX : has_pdf X ℙ μ] : measurable X := hX.pdf'.1 /-- If `X` is a random variable that `has_pdf X ℙ μ`, then `pdf X` is the measurable function `f` such that the push-forward measure of `ℙ` along `X` equals `μ.with_density f`. -/ def pdf {m : measurable_space Ω} (X : Ω → E) (ℙ : measure Ω) (μ : measure E . volume_tac) := if hX : has_pdf X ℙ μ then classical.some hX.pdf'.2 else 0 lemma pdf_undef {m : measurable_space Ω} {ℙ : measure Ω} {μ : measure E} {X : Ω → E} (h : ¬ has_pdf X ℙ μ) : pdf X ℙ μ = 0 := by simp only [pdf, dif_neg h] lemma has_pdf_of_pdf_ne_zero {m : measurable_space Ω} {ℙ : measure Ω} {μ : measure E} {X : Ω → E} (h : pdf X ℙ μ ≠ 0) : has_pdf X ℙ μ := begin by_contra hpdf, rw [pdf, dif_neg hpdf] at h, exact hpdf (false.rec (has_pdf X ℙ μ) (h rfl)) end lemma pdf_eq_zero_of_not_measurable {m : measurable_space Ω} {ℙ : measure Ω} {μ : measure E} {X : Ω → E} (hX : ¬ measurable X) : pdf X ℙ μ = 0 := pdf_undef (λ hpdf, hX hpdf.pdf'.1) lemma measurable_of_pdf_ne_zero {m : measurable_space Ω} {ℙ : measure Ω} {μ : measure E} (X : Ω → E) (h : pdf X ℙ μ ≠ 0) : measurable X := by { by_contra hX, exact h (pdf_eq_zero_of_not_measurable hX) } @[measurability] lemma measurable_pdf {m : measurable_space Ω} (X : Ω → E) (ℙ : measure Ω) (μ : measure E . volume_tac) : measurable (pdf X ℙ μ) := begin by_cases hX : has_pdf X ℙ μ, { rw [pdf, dif_pos hX], exact (classical.some_spec hX.pdf'.2).1 }, { rw [pdf, dif_neg hX], exact measurable_zero } end lemma map_eq_with_density_pdf {m : measurable_space Ω} (X : Ω → E) (ℙ : measure Ω) (μ : measure E . volume_tac) [hX : has_pdf X ℙ μ] : measure.map X ℙ = μ.with_density (pdf X ℙ μ) := begin rw [pdf, dif_pos hX], exact (classical.some_spec hX.pdf'.2).2 end lemma map_eq_set_lintegral_pdf {m : measurable_space Ω} (X : Ω → E) (ℙ : measure Ω) (μ : measure E . volume_tac) [hX : has_pdf X ℙ μ] {s : set E} (hs : measurable_set s) : measure.map X ℙ s = ∫⁻ x in s, pdf X ℙ μ x ∂μ := by rw [← with_density_apply _ hs, map_eq_with_density_pdf X ℙ μ] namespace pdf variables {m : measurable_space Ω} {ℙ : measure Ω} {μ : measure E} lemma lintegral_eq_measure_univ {X : Ω → E} [has_pdf X ℙ μ] : ∫⁻ x, pdf X ℙ μ x ∂μ = ℙ set.univ := begin rw [← set_lintegral_univ, ← map_eq_set_lintegral_pdf X ℙ μ measurable_set.univ, measure.map_apply (has_pdf.measurable X ℙ μ) measurable_set.univ, set.preimage_univ], end lemma ae_lt_top [is_finite_measure ℙ] {μ : measure E} {X : Ω → E} : ∀ᵐ x ∂μ, pdf X ℙ μ x < ∞ := begin by_cases hpdf : has_pdf X ℙ μ, { haveI := hpdf, refine ae_lt_top (measurable_pdf X ℙ μ) _, rw lintegral_eq_measure_univ, exact (measure_lt_top _ _).ne }, { rw [pdf, dif_neg hpdf], exact filter.eventually_of_forall (λ x, with_top.zero_lt_top) } end lemma of_real_to_real_ae_eq [is_finite_measure ℙ] {X : Ω → E} : (λ x, ennreal.of_real (pdf X ℙ μ x).to_real) =ᵐ[μ] pdf X ℙ μ := of_real_to_real_ae_eq ae_lt_top lemma integrable_iff_integrable_mul_pdf [is_finite_measure ℙ] {X : Ω → E} [has_pdf X ℙ μ] {f : E → ℝ} (hf : measurable f) : integrable (λ x, f (X x)) ℙ ↔ integrable (λ x, f x * (pdf X ℙ μ x).to_real) μ := begin rw [← integrable_map_measure hf.ae_strongly_measurable (has_pdf.measurable X ℙ μ).ae_measurable, map_eq_with_density_pdf X ℙ μ, integrable_with_density_iff (measurable_pdf _ _ _) ae_lt_top], apply_instance end /-- **The Law of the Unconscious Statistician**: Given a random variable `X` and a measurable function `f`, `f ∘ X` is a random variable with expectation `∫ x, f x * pdf X ∂μ` where `μ` is a measure on the codomain of `X`. -/ lemma integral_fun_mul_eq_integral [is_finite_measure ℙ] {X : Ω → E} [has_pdf X ℙ μ] {f : E → ℝ} (hf : measurable f) : ∫ x, f x * (pdf X ℙ μ x).to_real ∂μ = ∫ x, f (X x) ∂ℙ := begin by_cases hpdf : integrable (λ x, f x * (pdf X ℙ μ x).to_real) μ, { rw [← integral_map (has_pdf.measurable X ℙ μ).ae_measurable hf.ae_strongly_measurable, map_eq_with_density_pdf X ℙ μ, integral_eq_lintegral_pos_part_sub_lintegral_neg_part hpdf, integral_eq_lintegral_pos_part_sub_lintegral_neg_part, lintegral_with_density_eq_lintegral_mul _ (measurable_pdf X ℙ μ) hf.neg.ennreal_of_real, lintegral_with_density_eq_lintegral_mul _ (measurable_pdf X ℙ μ) hf.ennreal_of_real], { congr' 2, { have : ∀ x, ennreal.of_real (f x * (pdf X ℙ μ x).to_real) = ennreal.of_real (pdf X ℙ μ x).to_real * ennreal.of_real (f x), { intro x, rw [mul_comm, ennreal.of_real_mul ennreal.to_real_nonneg] }, simp_rw [this], exact lintegral_congr_ae (filter.eventually_eq.mul of_real_to_real_ae_eq (ae_eq_refl _)) }, { have : ∀ x, ennreal.of_real (- (f x * (pdf X ℙ μ x).to_real)) = ennreal.of_real (pdf X ℙ μ x).to_real * ennreal.of_real (-f x), { intro x, rw [neg_mul_eq_neg_mul, mul_comm, ennreal.of_real_mul ennreal.to_real_nonneg] }, simp_rw [this], exact lintegral_congr_ae (filter.eventually_eq.mul of_real_to_real_ae_eq (ae_eq_refl _)) } }, { refine ⟨hf.ae_strongly_measurable, _⟩, rw [has_finite_integral, lintegral_with_density_eq_lintegral_mul _ (measurable_pdf _ _ _) hf.nnnorm.coe_nnreal_ennreal], have : (λ x, (pdf X ℙ μ * λ x, ↑‖f x‖₊) x) =ᵐ[μ] (λ x, ‖f x * (pdf X ℙ μ x).to_real‖₊), { simp_rw [← smul_eq_mul, nnnorm_smul, ennreal.coe_mul], rw [smul_eq_mul, mul_comm], refine filter.eventually_eq.mul (ae_eq_refl _) (ae_eq_trans of_real_to_real_ae_eq.symm _), convert ae_eq_refl _, ext1 x, exact real.ennnorm_eq_of_real ennreal.to_real_nonneg }, rw lintegral_congr_ae this, exact hpdf.2 } }, { rw [integral_undef hpdf, integral_undef], rwa ← integrable_iff_integrable_mul_pdf hf at hpdf, all_goals { apply_instance } } end lemma map_absolutely_continuous {X : Ω → E} [has_pdf X ℙ μ] : map X ℙ ≪ μ := by { rw map_eq_with_density_pdf X ℙ μ, exact with_density_absolutely_continuous _ _, } /-- A random variable that `has_pdf` is quasi-measure preserving. -/ lemma to_quasi_measure_preserving {X : Ω → E} [has_pdf X ℙ μ] : quasi_measure_preserving X ℙ μ := { measurable := has_pdf.measurable X ℙ μ, absolutely_continuous := map_absolutely_continuous, } lemma have_lebesgue_decomposition_of_has_pdf {X : Ω → E} [hX' : has_pdf X ℙ μ] : (map X ℙ).have_lebesgue_decomposition μ := ⟨⟨⟨0, pdf X ℙ μ⟩, by simp only [zero_add, measurable_pdf X ℙ μ, true_and, mutually_singular.zero_left, map_eq_with_density_pdf X ℙ μ] ⟩⟩ lemma has_pdf_iff {X : Ω → E} : has_pdf X ℙ μ ↔ measurable X ∧ (map X ℙ).have_lebesgue_decomposition μ ∧ map X ℙ ≪ μ := begin split, { intro hX', exactI ⟨hX'.pdf'.1, have_lebesgue_decomposition_of_has_pdf, map_absolutely_continuous⟩ }, { rintros ⟨hX, h_decomp, h⟩, haveI := h_decomp, refine ⟨⟨hX, (measure.map X ℙ).rn_deriv μ, measurable_rn_deriv _ _, _⟩⟩, rwa with_density_rn_deriv_eq } end lemma has_pdf_iff_of_measurable {X : Ω → E} (hX : measurable X) : has_pdf X ℙ μ ↔ (map X ℙ).have_lebesgue_decomposition μ ∧ map X ℙ ≪ μ := by { rw has_pdf_iff, simp only [hX, true_and], } section variables {F : Type*} [measurable_space F] {ν : measure F} /-- A random variable that `has_pdf` transformed under a `quasi_measure_preserving` map also `has_pdf` if `(map g (map X ℙ)).have_lebesgue_decomposition μ`. `quasi_measure_preserving_has_pdf'` is more useful in the case we are working with a probability measure and a real-valued random variable. -/ lemma quasi_measure_preserving_has_pdf {X : Ω → E} [has_pdf X ℙ μ] {g : E → F} (hg : quasi_measure_preserving g μ ν) (hmap : (map g (map X ℙ)).have_lebesgue_decomposition ν) : has_pdf (g ∘ X) ℙ ν := begin rw [has_pdf_iff, ← map_map hg.measurable (has_pdf.measurable X ℙ μ)], refine ⟨hg.measurable.comp (has_pdf.measurable X ℙ μ), hmap, _⟩, rw [map_eq_with_density_pdf X ℙ μ], refine absolutely_continuous.mk (λ s hsm hs, _), rw [map_apply hg.measurable hsm, with_density_apply _ (hg.measurable hsm)], have := hg.absolutely_continuous hs, rw map_apply hg.measurable hsm at this, exact set_lintegral_measure_zero _ _ this, end lemma quasi_measure_preserving_has_pdf' [is_finite_measure ℙ] [sigma_finite ν] {X : Ω → E} [has_pdf X ℙ μ] {g : E → F} (hg : quasi_measure_preserving g μ ν) : has_pdf (g ∘ X) ℙ ν := quasi_measure_preserving_has_pdf hg infer_instance end section real variables [is_finite_measure ℙ] {X : Ω → ℝ} /-- A real-valued random variable `X` `has_pdf X ℙ λ` (where `λ` is the Lebesgue measure) if and only if the push-forward measure of `ℙ` along `X` is absolutely continuous with respect to `λ`. -/ lemma real.has_pdf_iff_of_measurable (hX : measurable X) : has_pdf X ℙ ↔ map X ℙ ≪ volume := begin rw [has_pdf_iff_of_measurable hX, and_iff_right_iff_imp], exact λ h, infer_instance, end lemma real.has_pdf_iff : has_pdf X ℙ ↔ measurable X ∧ map X ℙ ≪ volume := begin by_cases hX : measurable X, { rw [real.has_pdf_iff_of_measurable hX, iff_and_self], exact λ h, hX, apply_instance }, { exact ⟨λ h, false.elim (hX h.pdf'.1), λ h, false.elim (hX h.1)⟩, } end /-- If `X` is a real-valued random variable that has pdf `f`, then the expectation of `X` equals `∫ x, x * f x ∂λ` where `λ` is the Lebesgue measure. -/ lemma integral_mul_eq_integral [has_pdf X ℙ] : ∫ x, x * (pdf X ℙ volume x).to_real = ∫ x, X x ∂ℙ := integral_fun_mul_eq_integral measurable_id lemma has_finite_integral_mul {f : ℝ → ℝ} {g : ℝ → ℝ≥0∞} (hg : pdf X ℙ =ᵐ[volume] g) (hgi : ∫⁻ x, ‖f x‖₊ * g x ≠ ∞) : has_finite_integral (λ x, f x * (pdf X ℙ volume x).to_real) := begin rw has_finite_integral, have : (λ x, ↑‖f x‖₊ * g x) =ᵐ[volume] (λ x, ‖f x * (pdf X ℙ volume x).to_real‖₊), { refine ae_eq_trans (filter.eventually_eq.mul (ae_eq_refl (λ x, ‖f x‖₊)) (ae_eq_trans hg.symm of_real_to_real_ae_eq.symm)) _, simp_rw [← smul_eq_mul, nnnorm_smul, ennreal.coe_mul, smul_eq_mul], refine filter.eventually_eq.mul (ae_eq_refl _) _, convert ae_eq_refl _, ext1 x, exact real.ennnorm_eq_of_real ennreal.to_real_nonneg }, rwa [lt_top_iff_ne_top, ← lintegral_congr_ae this], end end real section /-! **Uniform Distribution** -/ /-- A random variable `X` has uniform distribution if it has a probability density function `f` with support `s` such that `f = (μ s)⁻¹ 1ₛ` a.e. where `1ₛ` is the indicator function for `s`. -/ def is_uniform {m : measurable_space Ω} (X : Ω → E) (support : set E) (ℙ : measure Ω) (μ : measure E . volume_tac) := pdf X ℙ μ =ᵐ[μ] support.indicator ((μ support)⁻¹ • 1) namespace is_uniform lemma has_pdf {m : measurable_space Ω} {X : Ω → E} {ℙ : measure Ω} {μ : measure E} {s : set E} (hns : μ s ≠ 0) (hnt : μ s ≠ ∞) (hu : is_uniform X s ℙ μ) : has_pdf X ℙ μ := has_pdf_of_pdf_ne_zero begin intro hpdf, rw [is_uniform, hpdf] at hu, suffices : μ (s ∩ function.support ((μ s)⁻¹ • 1)) = 0, { have heq : function.support ((μ s)⁻¹ • (1 : E → ℝ≥0∞)) = set.univ, { ext x, rw [function.mem_support], simp [hnt] }, rw [heq, set.inter_univ] at this, exact hns this }, exact set.indicator_ae_eq_zero hu.symm, end lemma pdf_to_real_ae_eq {m : measurable_space Ω} {X : Ω → E} {ℙ : measure Ω} {μ : measure E} {s : set E} (hX : is_uniform X s ℙ μ) : (λ x, (pdf X ℙ μ x).to_real) =ᵐ[μ] (λ x, (s.indicator ((μ s)⁻¹ • (1 : E → ℝ≥0∞)) x).to_real) := filter.eventually_eq.fun_comp hX ennreal.to_real lemma measure_preimage {m : measurable_space Ω} {X : Ω → E} {ℙ : measure Ω} {μ : measure E} {s : set E} (hns : μ s ≠ 0) (hnt : μ s ≠ ∞) (hms : measurable_set s) (hu : is_uniform X s ℙ μ) {A : set E} (hA : measurable_set A) : ℙ (X ⁻¹' A) = μ (s ∩ A) / μ s := begin haveI := hu.has_pdf hns hnt, rw [←measure.map_apply (has_pdf.measurable X ℙ μ) hA, map_eq_set_lintegral_pdf X ℙ μ hA, lintegral_congr_ae hu.restrict], simp only [hms, hA, lintegral_indicator, pi.smul_apply, pi.one_apply, algebra.id.smul_eq_mul, mul_one, lintegral_const, restrict_apply', set.univ_inter], rw ennreal.div_eq_inv_mul, end lemma is_probability_measure {m : measurable_space Ω} {X : Ω → E} {ℙ : measure Ω} {μ : measure E} {s : set E} (hns : μ s ≠ 0) (hnt : μ s ≠ ∞) (hms : measurable_set s) (hu : is_uniform X s ℙ μ) : is_probability_measure ℙ := ⟨begin have : X ⁻¹' set.univ = set.univ, { simp only [set.preimage_univ] }, rw [←this, hu.measure_preimage hns hnt hms measurable_set.univ, set.inter_univ, ennreal.div_self hns hnt], end⟩ variables {X : Ω → ℝ} {s : set ℝ} (hms : measurable_set s) (hns : volume s ≠ 0) include hms hns lemma mul_pdf_integrable [is_finite_measure ℙ] (hcs : is_compact s) (huX : is_uniform X s ℙ) : integrable (λ x : ℝ, x * (pdf X ℙ volume x).to_real) := begin by_cases hsupp : volume s = ∞, { have : pdf X ℙ =ᵐ[volume] 0, { refine ae_eq_trans huX _, simp [hsupp] }, refine integrable.congr (integrable_zero _ _ _) _, rw [(by simp : (λ x, 0 : ℝ → ℝ) = (λ x, x * (0 : ℝ≥0∞).to_real))], refine filter.eventually_eq.mul (ae_eq_refl _) (filter.eventually_eq.fun_comp this.symm ennreal.to_real) }, refine ⟨ae_strongly_measurable_id.mul (measurable_pdf X ℙ).ae_measurable.ennreal_to_real.ae_strongly_measurable, _⟩, refine has_finite_integral_mul huX _, set ind := (volume s)⁻¹ • (1 : ℝ → ℝ≥0∞) with hind, have : ∀ x, ↑‖x‖₊ * s.indicator ind x = s.indicator (λ x, ‖x‖₊ * ind x) x := λ x, (s.indicator_mul_right (λ x, ↑‖x‖₊) ind).symm, simp only [this, lintegral_indicator _ hms, hind, mul_one, algebra.id.smul_eq_mul, pi.one_apply, pi.smul_apply], rw lintegral_mul_const _ measurable_nnnorm.coe_nnreal_ennreal, { refine (ennreal.mul_lt_top (set_lintegral_lt_top_of_is_compact hsupp hcs continuous_nnnorm).ne (ennreal.inv_lt_top.2 (pos_iff_ne_zero.mpr hns)).ne).ne }, { apply_instance } end /-- A real uniform random variable `X` with support `s` has expectation `(λ s)⁻¹ * ∫ x in s, x ∂λ` where `λ` is the Lebesgue measure. -/ lemma integral_eq (hnt : volume s ≠ ∞) (huX : is_uniform X s ℙ) : ∫ x, X x ∂ℙ = (volume s)⁻¹.to_real * ∫ x in s, x := begin haveI := has_pdf hns hnt huX, haveI := huX.is_probability_measure hns hnt hms, rw ← integral_mul_eq_integral, rw integral_congr_ae (filter.eventually_eq.mul (ae_eq_refl _) (pdf_to_real_ae_eq huX)), have : ∀ x, x * (s.indicator ((volume s)⁻¹ • (1 : ℝ → ℝ≥0∞)) x).to_real = x * (s.indicator ((volume s)⁻¹.to_real • (1 : ℝ → ℝ)) x), { refine λ x, congr_arg ((*) x) _, by_cases hx : x ∈ s, { simp [set.indicator_of_mem hx] }, { simp [set.indicator_of_not_mem hx] }}, simp_rw [this, ← s.indicator_mul_right (λ x, x), integral_indicator hms], change ∫ x in s, x * ((volume s)⁻¹.to_real • 1) ∂(volume) = _, rw [integral_mul_right, mul_comm, algebra.id.smul_eq_mul, mul_one], end . end is_uniform end end pdf end measure_theory
21fe3a5efad98bed3f7c76032f8c886ac8f17de0
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/iterate_hom.lean
e3508f841a1e2dc3b7523a653d4956576a4e0a69
[]
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,546
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.group_power.default import Mathlib.logic.function.iterate import Mathlib.PostPort universes u_1 u_3 u_5 namespace Mathlib /-! # Iterates of monoid and ring homomorphisms Iterate of a monoid/ring homomorphism is a monoid/ring homomorphism but it has a wrong type, so Lean can't apply lemmas like `monoid_hom.map_one` to `f^[n] 1`. Though it is possible to define a monoid structure on the endomorphisms, quite often we do not want to convert from `M →* M` to (not yet defined) `monoid.End M` and from `f^[n]` to `f^n` just to apply a simple lemma. So, we restate standard `*_hom.map_*` lemmas under names `*_hom.iterate_map_*`. We also prove formulas for iterates of add/mul left/right. ## Tags homomorphism, iterate -/ namespace monoid_hom @[simp] theorem Mathlib.add_monoid_hom.iterate_map_zero {M : Type u_1} [add_monoid M] (f : M →+ M) (n : ℕ) : nat.iterate (⇑f) n 0 = 0 := function.iterate_fixed (add_monoid_hom.map_zero f) n @[simp] theorem iterate_map_mul {M : Type u_1} [monoid M] (f : M →* M) (n : ℕ) (x : M) (y : M) : nat.iterate (⇑f) n (x * y) = nat.iterate (⇑f) n x * nat.iterate (⇑f) n y := function.semiconj₂.iterate (map_mul f) n x y @[simp] theorem iterate_map_inv {G : Type u_3} [group G] (f : G →* G) (n : ℕ) (x : G) : nat.iterate (⇑f) n (x⁻¹) = (nat.iterate (⇑f) n x⁻¹) := function.commute.iterate_left (map_inv f) n x theorem iterate_map_pow {M : Type u_1} [monoid M] (f : M →* M) (a : M) (n : ℕ) (m : ℕ) : nat.iterate (⇑f) n (a ^ m) = nat.iterate (⇑f) n a ^ m := function.commute.iterate_left (fun (x : M) => map_pow f x m) n a theorem iterate_map_gpow {G : Type u_3} [group G] (f : G →* G) (a : G) (n : ℕ) (m : ℤ) : nat.iterate (⇑f) n (a ^ m) = nat.iterate (⇑f) n a ^ m := function.commute.iterate_left (fun (x : G) => map_gpow f x m) n a end monoid_hom namespace add_monoid_hom @[simp] theorem iterate_map_sub {G : Type u_3} [add_group G] (f : G →+ G) (n : ℕ) (x : G) (y : G) : nat.iterate (⇑f) n (x - y) = nat.iterate (⇑f) n x - nat.iterate (⇑f) n y := function.semiconj₂.iterate (map_sub f) n x y theorem iterate_map_smul {M : Type u_1} [add_monoid M] (f : M →+ M) (n : ℕ) (m : ℕ) (x : M) : nat.iterate (⇑f) n (m •ℕ x) = m •ℕ nat.iterate (⇑f) n x := monoid_hom.iterate_map_pow (coe_fn to_multiplicative f) x n m theorem iterate_map_gsmul {G : Type u_3} [add_group G] (f : G →+ G) (n : ℕ) (m : ℤ) (x : G) : nat.iterate (⇑f) n (m •ℤ x) = m •ℤ nat.iterate (⇑f) n x := monoid_hom.iterate_map_gpow (coe_fn to_multiplicative f) x n m end add_monoid_hom namespace ring_hom theorem coe_pow {R : Type u_5} [semiring R] (f : R →+* R) (n : ℕ) : ⇑(f ^ n) = nat.iterate (⇑f) n := sorry theorem iterate_map_one {R : Type u_5} [semiring R] (f : R →+* R) (n : ℕ) : nat.iterate (⇑f) n 1 = 1 := monoid_hom.iterate_map_one (to_monoid_hom f) n theorem iterate_map_zero {R : Type u_5} [semiring R] (f : R →+* R) (n : ℕ) : nat.iterate (⇑f) n 0 = 0 := add_monoid_hom.iterate_map_zero (to_add_monoid_hom f) n theorem iterate_map_add {R : Type u_5} [semiring R] (f : R →+* R) (n : ℕ) (x : R) (y : R) : nat.iterate (⇑f) n (x + y) = nat.iterate (⇑f) n x + nat.iterate (⇑f) n y := add_monoid_hom.iterate_map_add (to_add_monoid_hom f) n x y theorem iterate_map_mul {R : Type u_5} [semiring R] (f : R →+* R) (n : ℕ) (x : R) (y : R) : nat.iterate (⇑f) n (x * y) = nat.iterate (⇑f) n x * nat.iterate (⇑f) n y := monoid_hom.iterate_map_mul (to_monoid_hom f) n x y theorem iterate_map_pow {R : Type u_5} [semiring R] (f : R →+* R) (a : R) (n : ℕ) (m : ℕ) : nat.iterate (⇑f) n (a ^ m) = nat.iterate (⇑f) n a ^ m := monoid_hom.iterate_map_pow (to_monoid_hom f) a n m theorem iterate_map_smul {R : Type u_5} [semiring R] (f : R →+* R) (n : ℕ) (m : ℕ) (x : R) : nat.iterate (⇑f) n (m •ℕ x) = m •ℕ nat.iterate (⇑f) n x := add_monoid_hom.iterate_map_smul (to_add_monoid_hom f) n m x theorem iterate_map_sub {R : Type u_5} [ring R] (f : R →+* R) (n : ℕ) (x : R) (y : R) : nat.iterate (⇑f) n (x - y) = nat.iterate (⇑f) n x - nat.iterate (⇑f) n y := add_monoid_hom.iterate_map_sub (to_add_monoid_hom f) n x y theorem iterate_map_neg {R : Type u_5} [ring R] (f : R →+* R) (n : ℕ) (x : R) : nat.iterate (⇑f) n (-x) = -nat.iterate (⇑f) n x := add_monoid_hom.iterate_map_neg (to_add_monoid_hom f) n x theorem iterate_map_gsmul {R : Type u_5} [ring R] (f : R →+* R) (n : ℕ) (m : ℤ) (x : R) : nat.iterate (⇑f) n (m •ℤ x) = m •ℤ nat.iterate (⇑f) n x := add_monoid_hom.iterate_map_gsmul (to_add_monoid_hom f) n m x end ring_hom @[simp] theorem mul_left_iterate {M : Type u_1} [monoid M] (a : M) (n : ℕ) : nat.iterate (Mul.mul a) n = Mul.mul (a ^ n) := sorry @[simp] theorem add_left_iterate {M : Type u_1} [add_monoid M] (a : M) (n : ℕ) : nat.iterate (Add.add a) n = Add.add (n •ℕ a) := mul_left_iterate a n @[simp] theorem mul_right_iterate {M : Type u_1} [monoid M] (a : M) (n : ℕ) : nat.iterate (fun (x : M) => x * a) n = fun (x : M) => x * a ^ n := sorry @[simp] theorem add_right_iterate {M : Type u_1} [add_monoid M] (a : M) (n : ℕ) : nat.iterate (fun (x : M) => x + a) n = fun (x : M) => x + n •ℕ a := mul_right_iterate a n
3b1e7c02a901f79396ed35aeac4be3953faac6a3
246309748072bf9f8da313401699689ebbecd94d
/src/category_theory/limits/shapes/types.lean
c507a34ee76db819b678f2bfc7a767997926bb99
[ "Apache-2.0" ]
permissive
YJMD/mathlib
b703a641e5f32a996f7842f7c0043bab2b462ee2
7310eab9fa8c1b1229dca42682f1fa6bfb7dbbf9
refs/heads/master
1,670,714,479,314
1,599,035,445,000
1,599,035,445,000
292,279,930
0
0
null
1,599,050,561,000
1,599,050,560,000
null
UTF-8
Lean
false
false
6,950
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.limits.shapes.products import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.terminal /-! # Special shapes for limits in `Type`. The general shape (co)limits defined in `category_theory.limits.types` are intended for use through the limits API, and the actual implementation should mostly be considered "sealed". In this file, we provide definitions of the "standard" special shapes of limits in `Type`, giving the expected definitional implementation: * the terminal object is `punit` * the binary product of `X` and `Y` is `X × Y` * the product of a family `f : J → Type` is `Π j, f j`. It is not intended that these definitions will be global instances: they should be turned on as needed. As an example, when setting up the monoidal category structure on `Type` we use the `types_has_terminal` and `types_has_binary_products` instances. -/ universes u open category_theory open category_theory.limits namespace category_theory.limits.types /-- A restatement of `types.lift_π_apply` that uses `pi.π` and `pi.lift`. -/ @[simp] lemma lift_π_apply' {β : Type u} (f : β → Type u) {P : Type u} (s : Π b, P ⟶ f b) (b : β) (x : P) : (pi.π f b : (∏ f) → f b) (@pi.lift β _ _ f _ P s x) = s b x := congr_fun (limit.lift_π (fan.mk s) b) x /-- The category of types has `punit` as a terminal object. -/ def types_has_terminal : has_terminal (Type u) := { has_limit := λ F, { cone := { X := punit, π := by tidy, }, is_limit := by tidy, } } /-- The category of types has `pempty` as an initial object. -/ def types_has_initial : has_initial (Type u) := { has_colimit := λ F, { cocone := { X := pempty, ι := by tidy, }, is_colimit := by tidy, } } open category_theory.limits.walking_pair local attribute [tidy] tactic.case_bash /-- The category of types has `X × Y`, the usual cartesian product, as the binary product of `X` and `Y`. -/ def types_has_binary_products : has_binary_products (Type u) := { has_limit := λ F, { cone := { X := F.obj left × F.obj right, π := { app := by { rintro ⟨_|_⟩, exact prod.fst, exact prod.snd, } }, }, is_limit := { lift := λ s x, (s.π.app left x, s.π.app right x), uniq' := λ s m w, begin ext, exact congr_fun (w left) x, exact congr_fun (w right) x, end }, } } /-- The category of types has `X ⊕ Y`, as the binary coproduct of `X` and `Y`. -/ def types_has_binary_coproducts : has_binary_coproducts (Type u) := { has_colimit := λ F, { cocone := { X := F.obj left ⊕ F.obj right, ι := { app := by { rintro ⟨_|_⟩, exact sum.inl, exact sum.inr, } }, }, is_colimit := { desc := λ s x, sum.elim (s.ι.app left) (s.ι.app right) x, uniq' := λ s m w, begin ext (x|x), exact (congr_fun (w left) x : _), exact (congr_fun (w right) x : _), end }, } } /-- The category of types has `Π j, f j` as the product of a type family `f : J → Type`. -/ def types_has_products : has_products (Type u) := λ J, { has_limit := λ F, { cone := { X := Π j, F.obj j, π := { app := λ j f, f j }, }, is_limit := { lift := λ s x j, s.π.app j x, uniq' := λ s m w, begin ext x j, have := congr_fun (w j) x, exact this, end }, } } /-- The category of types has `Σ j, f j` as the coproduct of a type family `f : J → Type`. -/ def types_has_coproducts : has_coproducts (Type u) := λ J, { has_colimit := λ F, { cocone := { X := Σ j, F.obj j, ι := { app := λ j x, ⟨j, x⟩ }, }, is_colimit := { desc := λ s x, s.ι.app x.1 x.2, uniq' := λ s m w, begin ext ⟨j, x⟩, have := congr_fun (w j) x, exact this, end }, } } local attribute [instance, priority 200] types_has_products types_has_coproducts -- We slightly increase the priority of `types_has_terminal` and `types_has_binary_products` -- so that they come ahead of `types_has_products`. local attribute [instance, priority 300] types_has_terminal types_has_initial local attribute [instance, priority 300] types_has_binary_products types_has_binary_coproducts @[simp] lemma terminal : (⊤_ (Type u)) = punit := rfl lemma terminal_from {P : Type u} (f : P ⟶ ⊤_ (Type u)) (p : P) : f p = punit.star := by ext @[simp] lemma initial : (⊥_ (Type u)) = pempty := rfl @[simp] lemma prod (X Y : Type u) : limits.prod X Y = prod X Y := rfl @[simp] lemma prod_fst {X Y : Type u} (p : limits.prod X Y) : (@limits.prod.fst.{u} _ _ X Y _ : limits.prod X Y → X) p = p.1 := rfl @[simp] lemma prod_snd {X Y : Type u} (p : limits.prod X Y) : (@limits.prod.snd.{u} _ _ X Y _ : limits.prod X Y → Y) p = p.2 := rfl @[simp] lemma prod_lift {X Y Z : Type u} {f : X ⟶ Y} {g : X ⟶ Z} : limits.prod.lift f g = (λ x, (f x, g x)) := rfl @[simp] lemma prod_map {W X Y Z : Type u} {f : W ⟶ X} {g : Y ⟶ Z} : limits.prod.map f g = (λ p : W × Y, (f p.1, g p.2)) := rfl @[simp] lemma coprod (X Y : Type u) : limits.coprod X Y = (X ⊕ Y) := rfl @[simp] lemma coprod_inl {X Y : Type u} (x : X) : (@limits.coprod.inl.{u} _ _ X Y _ : X → limits.coprod X Y ) x = sum.inl x := rfl @[simp] lemma coprod_inr {X Y : Type u} (y : Y) : (@limits.coprod.inr.{u} _ _ X Y _ : Y → limits.coprod X Y) y = sum.inr y := rfl @[simp] lemma coprod_desc {X Y Z : Type u} {f : X ⟶ Z} {g : Y ⟶ Z} : limits.coprod.desc f g = sum.elim f g := rfl @[simp] lemma coprod_map {W X Y Z : Type u} {f : W ⟶ X} {g : Y ⟶ Z} : limits.coprod.map f g = (λ p : W ⊕ Y, sum.elim (λ w, sum.inl (f w)) (λ y, sum.inr (g y)) p) := rfl @[simp] lemma pi {J : Type u} (f : J → Type u) : pi_obj f = Π j, f j := rfl @[simp] lemma pi_π {J : Type u} {f : J → Type u} (j : J) (g : pi_obj f) : (pi.π f j : pi_obj f → f j) g = g j := rfl @[simp] lemma pi_lift {J : Type u} {f : J → Type u} {W : Type u} {g : Π j, W ⟶ f j} : pi.lift g = (λ w j, g j w) := rfl @[simp] lemma pi_map {J : Type u} {f g : J → Type u} {h : Π j, f j ⟶ g j} : pi.map h = λ (k : Π j, f j) j, h j (k j) := rfl @[simp] lemma sigma {J : Type u} (f : J → Type u) : sigma_obj f = Σ j, f j := rfl @[simp] lemma sigma_ι {J : Type u} {f : J → Type u} (j : J) (x : f j) : (sigma.ι f j : f j → sigma_obj f) x = ⟨j, x⟩ := rfl @[simp] lemma sigma_desc {J : Type u} {f : J → Type u} {W : Type u} {g : Π j, f j ⟶ W} {p : Σ j, f j} : (sigma.desc g) p = g p.1 p.2 := rfl @[simp] lemma sigma_map {J : Type u} {f g : J → Type u} {h : Π j, f j ⟶ g j} : limits.sigma.map h = λ (k : Σ j, f j), (⟨k.1, h k.1 (k.2)⟩ : Σ j, g j) := rfl end category_theory.limits.types
0821c2d35fb136f4dfeeb8bcab24e417f3a46659
3863d2564418bccb1859e057bf5a4ef240e75fd7
/hott/types/equiv.hlean
149cfe4f53b289833b2bbcfdc866aa7e670a7bfb
[ "Apache-2.0" ]
permissive
JacobGross/lean
118bbb067ff4d4af48a266face2c7eb9868fa91c
eb26087df940c54337cb807b4bc6d345d1fc1085
refs/heads/master
1,582,735,011,532
1,462,557,826,000
1,462,557,826,000
46,451,196
0
0
null
1,462,557,826,000
1,447,885,161,000
C++
UTF-8
Lean
false
false
11,283
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 ..prop_trunc cubical.square open eq is_trunc sigma sigma.ops pi fiber function equiv 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 f] ... = (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_right, 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 B (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_right, 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_prop_is_equiv [instance] : is_prop (is_equiv f) := is_prop_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_prop.elim /- contractible fibers -/ definition is_contr_fun_of_is_equiv [H : is_equiv f] : is_contr_fun f := is_contr_fiber_of_is_equiv f definition is_prop_is_contr_fun (f : A → B) : is_prop (is_contr_fun f) := _ 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_prop _ (λH, !is_equiv_of_is_contr_fun) end is_equiv /- Moving equivalences around in homotopies -/ namespace is_equiv variables {A B C : Type} (f : A → B) [Hf : is_equiv f] include Hf section pre_compose variables (α : A → C) (β : B → C) -- homotopy_inv_of_homotopy_pre is in init.equiv protected definition inv_homotopy_of_homotopy_pre.is_equiv : is_equiv (inv_homotopy_of_homotopy_pre f α β) := adjointify _ (homotopy_of_inv_homotopy_pre f α β) abstract begin intro q, apply eq_of_homotopy, intro b, unfold inv_homotopy_of_homotopy_pre, unfold homotopy_of_inv_homotopy_pre, apply inverse, apply eq_bot_of_square, apply eq_hconcat (ap02 α (adj_inv f b)), apply eq_hconcat (ap_compose α f⁻¹ (right_inv f b))⁻¹, apply natural_square_tr q (right_inv f b) end end abstract begin intro p, apply eq_of_homotopy, intro a, unfold inv_homotopy_of_homotopy_pre, unfold homotopy_of_inv_homotopy_pre, apply trans (con.assoc (ap α (left_inv f a))⁻¹ (p (f⁻¹ (f a))) (ap β (right_inv f (f a))))⁻¹, apply inverse, apply eq_bot_of_square, refine hconcat_eq _ (ap02 β (adj f a))⁻¹, refine hconcat_eq _ (ap_compose β f (left_inv f a)), apply natural_square_tr p (left_inv f a) end end end pre_compose section post_compose variables (α : C → A) (β : C → B) -- homotopy_inv_of_homotopy_post is in init.equiv protected definition inv_homotopy_of_homotopy_post.is_equiv : is_equiv (inv_homotopy_of_homotopy_post f α β) := adjointify _ (homotopy_of_inv_homotopy_post f α β) abstract begin intro q, apply eq_of_homotopy, intro c, unfold inv_homotopy_of_homotopy_post, unfold homotopy_of_inv_homotopy_post, apply trans (whisker_right (ap_con f⁻¹ (right_inv f (β c))⁻¹ (ap f (q c)) ⬝ whisker_right (ap_inv f⁻¹ (right_inv f (β c))) (ap f⁻¹ (ap f (q c)))) (left_inv f (α c))), apply inverse, apply eq_bot_of_square, apply eq_hconcat (adj_inv f (β c))⁻¹, apply eq_vconcat (ap_compose f⁻¹ f (q c))⁻¹, refine vconcat_eq _ (ap_id (q c)), apply natural_square (left_inv f) (q c) end end abstract begin intro p, apply eq_of_homotopy, intro c, unfold inv_homotopy_of_homotopy_post, unfold homotopy_of_inv_homotopy_post, apply trans (whisker_left (right_inv f (β c))⁻¹ (ap_con f (ap f⁻¹ (p c)) (left_inv f (α c)))), apply trans (con.assoc (right_inv f (β c))⁻¹ (ap f (ap f⁻¹ (p c))) (ap f (left_inv f (α c))))⁻¹, apply inverse, apply eq_bot_of_square, refine hconcat_eq _ (adj f (α c)), apply eq_vconcat (ap_compose f f⁻¹ (p c))⁻¹, refine vconcat_eq _ (ap_id (p c)), apply natural_square (right_inv f) (p c) end end end post_compose end is_equiv namespace is_equiv /- Theorem 4.7.7 -/ variables {A : Type} {P Q : A → Type} variable (f : Πa, P a → Q a) definition is_fiberwise_equiv [reducible] := Πa, is_equiv (f a) definition is_equiv_total_of_is_fiberwise_equiv [H : is_fiberwise_equiv f] : is_equiv (total f) := is_equiv_sigma_functor id f definition is_fiberwise_equiv_of_is_equiv_total [H : is_equiv (total f)] : is_fiberwise_equiv f := begin intro a, apply is_equiv_of_is_contr_fun, intro q, apply @is_contr_equiv_closed _ _ (fiber_total_equiv f q) end 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_prop.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_prop.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_prop.elim H H'))), {apply idp}, generalize is_prop.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_set.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 pathover_of_fn_pathover_fn, { intro a, apply equiv.sigma_char}, { fapply sigma_pathover, esimp, apply arrow_pathover, exact r, apply is_prop.elimo} end definition is_contr_equiv (A B : Type) [HA : is_contr A] [HB : is_contr B] : is_contr (A ≃ B) := begin apply @is_contr_of_inhabited_prop, apply is_prop.mk, intro x y, cases x with fx Hx, cases y with fy Hy, generalize Hy, apply (eq_of_homotopy (λ a, !eq_of_is_contr)) ▸ (λ Hy, !is_prop.elim ▸ rfl), apply equiv_of_is_contr_of_is_contr end definition is_trunc_succ_equiv (n : trunc_index) (A B : Type) [HA : is_trunc n.+1 A] [HB : is_trunc n.+1 B] : is_trunc n.+1 (A ≃ B) := @is_trunc_equiv_closed _ _ n.+1 (equiv.symm !equiv.sigma_char) (@is_trunc_sigma _ _ _ _ (λ f, !is_trunc_succ_of_is_prop)) definition is_trunc_equiv (n : trunc_index) (A B : Type) [HA : is_trunc n A] [HB : is_trunc n B] : is_trunc n (A ≃ B) := by cases n; apply !is_contr_equiv; apply !is_trunc_succ_equiv end equiv
f9f78ab19e278b0b64b39ef3c386a85a7f1a30ff
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/archive/miu_language/decision_nec.lean
5763e59fbc93f0afd4072f1867a272e4ec89e297
[ "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
6,707
lean
/- Copyright (c) 2020 Gihan Marasingha. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gihan Marasingha -/ import .basic import data.nat.modeq import tactic.ring /-! # Decision procedure: necessary condition We introduce a condition `decstr` and show that if a string `en` is `derivable`, then `decstr en` holds. Using this, we give a negative answer to the question: is `"MU"` derivable? ## Tags miu, decision procedure -/ namespace miu open miu_atom nat list /-! ### Numerical condition on the `I` count Suppose `st : miustr`. Then `count I st` is the number of `I`s in `st`. We'll show, if `derivable st`, then `count I st` must be 1 or 2 modulo 3. To do this, it suffices to show that if the `en : miustr` is derived from `st`, then `count I en` moudulo 3 is either equal to or is twice `count I st`, modulo 3. -/ /-- Given `st en : miustr`, the relation `count_equiv_or_equiv_two_mul_mod3 st en` holds if `st` and `en` either have equal `count I`, modulo 3, or `count I en` is twice `count I st`, modulo 3. -/ def count_equiv_or_equiv_two_mul_mod3 (st en : miustr) : Prop := let a := (count I st) in let b := (count I en) in b ≡ a [MOD 3] ∨ b ≡ 2*a [MOD 3] example : count_equiv_or_equiv_two_mul_mod3 "II" "MIUI" := or.inl rfl example : count_equiv_or_equiv_two_mul_mod3 "IUIM" "MI" := or.inr rfl /-- If `a` is 1 or 2 mod 3 and if `b` is `a` or twice `a` mod 3, then `b` is 1 or 2 mod 3. -/ lemma mod3_eq_1_or_mod3_eq_2 {a b : ℕ} (h1 : a % 3 = 1 ∨ a % 3 = 2) (h2 : b % 3 = a % 3 ∨ b % 3 = (2 * a % 3)) : b % 3 = 1 ∨ b % 3 = 2 := begin cases h2, { rw h2, exact h1, }, { cases h1, { right, simpa [h2,mul_mod,h1], }, { left, simpa [h2,mul_mod,h1], }, }, end /-- `count_equiv_one_or_two_mod3_of_derivable` shows any derivable string must have a `count I` that is 1 or 2 modulo 3. -/ theorem count_equiv_one_or_two_mod3_of_derivable (en : miustr): derivable en → (count I en) % 3 = 1 ∨ (count I en) % 3 = 2:= begin intro h, induction h, { left, apply mod_def, }, any_goals {apply mod3_eq_1_or_mod3_eq_2 h_ih}, { left, simp only [count_append], refl, }, { right, simp only [count, countp, count_append, if_false,two_mul], }, { left, simp only [count, count_append, countp, if_false, if_pos], rw [add_right_comm, add_mod_right], }, { left, simp only [count ,countp, countp_append, if_false, add_zero], }, end /-- Using the above theorem, we solve the MU puzzle, showing that `"MU"` is not derivable. Once we have proved that `derivable` is an instance of `decidable_pred`, this will follow immediately from `dec_trivial`. -/ theorem not_derivable_mu : ¬(derivable "MU") := begin intro h, cases (count_equiv_one_or_two_mod3_of_derivable _ h); contradiction, end /-! ### Condition on `M` That solves the MU puzzle, but we'll proceed by demonstrating the other necessary condition for a string to be derivable, namely that the string must start with an M and contain no M in its tail. -/ /-- `goodm xs` holds if `xs : miustr` begins with `M` and has no `M` in its tail. -/ @[derive decidable_pred] def goodm (xs : miustr) : Prop := list.head xs = M ∧ ¬(M ∈ list.tail xs) /-- Demonstration that `"MI"` starts with `M` and has no `M` in its tail. -/ lemma goodmi : goodm [M,I] := begin split, { refl }, { rw [tail ,mem_singleton], trivial }, end /-! We'll show, for each `i` from 1 to 4, that if `en` follows by Rule `i` from `st` and if `goodm st` holds, then so does `goodm en`. -/ lemma goodm_of_rule1 (xs : miustr) (h₁ : derivable (xs ++ [I])) (h₂ : goodm (xs ++ [I])) : goodm (xs ++ [I,U]) := begin cases h₂ with mhead nmtail, have : xs ≠ nil, { intro h, rw h at *, rw [nil_append, head] at mhead, contradiction, }, split, { rwa [head_append] at *; exact this, }, { change [I,U] with [I] ++ [U], rw [←append_assoc, tail_append_singleton_of_ne_nil], { simp only [mem_append, nmtail, false_or, mem_singleton, not_false_iff], }, { exact append_ne_nil_of_ne_nil_left _ _ this, }, }, end lemma goodm_of_rule2 (xs : miustr) (h₁ : derivable (M :: xs)) (h₂ : goodm (M :: xs)) : goodm (M :: xs ++ xs) := begin split, { refl, }, { cases h₂ with mhead mtail, contrapose! mtail, rw cons_append at mtail, rw tail at *, exact (or_self _).mp (mem_append.mp mtail), }, end lemma goodm_of_rule3 (as bs : miustr) (h₁ : derivable (as ++ [I,I,I] ++ bs)) (h₂ : goodm (as ++ [I,I,I] ++ bs)) : goodm (as ++ U :: bs) := begin cases h₂ with mhead nmtail, have k : as ≠ nil , { intro h, rw h at mhead, rw [nil_append] at mhead, contradiction, }, split, { revert mhead, simp only [append_assoc,head_append _ k], exact id, }, { contrapose! nmtail, rcases (exists_cons_of_ne_nil k) with ⟨x,xs,rfl⟩, simp only [cons_append, tail, mem_append, mem_cons_iff, false_or, mem_nil_iff, or_false] at *, exact nmtail, }, end /-! The proof of the next lemma is identical, on the tactic level, to the previous proof. -/ lemma goodm_of_rule4 (as bs : miustr) (h₁ : derivable (as ++ [U,U] ++ bs)) (h₂ : goodm (as ++ [U,U] ++ bs)) : goodm (as ++ bs) := begin cases h₂ with mhead nmtail, have k : as ≠ nil , { intro h, rw h at mhead, rw [nil_append] at mhead, contradiction, }, split, { revert mhead, simp only [append_assoc,head_append _ k], exact id, }, { contrapose! nmtail, rcases (exists_cons_of_ne_nil k) with ⟨x,xs,rfl⟩, simp only [cons_append, tail, mem_append, mem_cons_iff, false_or, mem_nil_iff, or_false] at *, exact nmtail, }, end /-- Any derivable string must begin with `M` and have no `M` in its tail. -/ theorem goodm_of_derivable (en : miustr): derivable en → goodm en:= begin intro h, induction h, { exact goodmi, }, { apply goodm_of_rule1; assumption, }, { apply goodm_of_rule2; assumption, }, { apply goodm_of_rule3; assumption, }, { apply goodm_of_rule4; assumption, }, end /-! We put togther our two conditions to give one necessary condition `decstr` for an `miustr` to be derivable. -/ /-- `decstr en` is the condition that `count I en` is 1 or 2 modulo 3, that `en` starts with `M`, and that `en` has no `M` in its tail. We automatically derive that this is a decidable predicate. -/ @[derive decidable_pred] def decstr (en : miustr) := goodm en ∧ ((count I en) % 3 = 1 ∨ (count I en) % 3 = 2) /-- Suppose `en : miustr`. If `en` is `derivable`, then the condition `decstr en` holds. -/ theorem decstr_of_der {en : miustr} : derivable en → decstr en := begin intro h, split, { exact goodm_of_derivable en h, }, { exact count_equiv_one_or_two_mod3_of_derivable en h, }, end end miu
a5cf66bc5ceaf41dd9e011eaf1f3a76ca03f7664
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/hott/apply_class_issue.hlean
db6a6a09d0f115840d88ff142f81fd94222cb083
[ "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
305
hlean
open is_trunc section parameters {P : Π(A : Type), A → Type} definition my_contr {A : Type} [H : is_contr A] (a : A) : P A a := sorry definition foo2 (A : Type) (B : A → Type) (a : A) (x : B a) (H : Π (a : A), is_contr (B a)) --(H : is_contr (B a)) : P (B a) x := by apply (@my_contr _ _) end
9c9e2881a6a717775b3a4aacbd8a478f84e0e583
6df8d5ae3acf20ad0d7f0247d2cee1957ef96df1
/ExamPractice/FinalCS.lean
01ab7b8d7dbd52de12310638a0a03c69c8766b02
[]
no_license
derekjohnsonva/CS2102
8ed45daa6658e6121bac0f6691eac6147d08246d
b3f507d4be824a2511838a1054d04fc9aef3304c
refs/heads/master
1,648,529,162,527
1,578,851,859,000
1,578,851,859,000
233,433,207
0
0
null
null
null
null
UTF-8
Lean
false
false
322
lean
namespace hidden -- Namespace hidden allows us do define our own inductive --types even if they have a standard parallel /- To calculate the number of functions that can be made x = # of inputs, y = # of output # of functions = (2^y)^(2^x) assuming it is a binary function -/ end hidden
37cb50f3c5cbe05511776375e29b810a5c1a3090
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/data/equiv/basic.lean
a517f73b4c0cf2763dce8db03cc0347c9d1e7330
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
79,990
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.set.function import algebra.group.basic /-! # Equivalence between types In this file we define two types: * `equiv α β` a.k.a. `α ≃ β`: a bijective map `α → β` bundled with its inverse map; we use this (and not equality!) to express that various `Type`s or `Sort`s are equivalent. * `equiv.perm α`: the group of permutations `α ≃ α`. Then we define * canonical isomorphisms between various types: e.g., - `equiv.refl α` is the identity map interpreted as `α ≃ α`; - `equiv.sum_equiv_sigma_bool` is the canonical equivalence between the sum of two types `α ⊕ β` and the sigma-type `Σ b : bool, cond b α β`; - `equiv.prod_sum_distrib : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ)` shows that type product and type sum satisfy the distributive law up to a canonical equivalence; * operations on equivalences: e.g., - `equiv.symm e : β ≃ α` is the inverse of `e : α ≃ β`; - `equiv.trans e₁ e₂ : α ≃ γ` is the composition of `e₁ : α ≃ β` and `e₂ : β ≃ γ` (note the order of the arguments!); - `equiv.prod_congr ea eb : α₁ × β₁ ≃ α₂ × β₂`: combine two equivalences `ea : α₁ ≃ α₂` and `eb : β₁ ≃ β₂` using `prod.map`. * definitions that transfer some instances along an equivalence. By convention, we transfer instances from right to left. - `equiv.inhabited` takes `e : α ≃ β` and `[inhabited β]` and returns `inhabited α`; - `equiv.unique` takes `e : α ≃ β` and `[unique β]` and returns `unique α`; - `equiv.decidable_eq` takes `e : α ≃ β` and `[decidable_eq β]` and returns `decidable_eq α`. More definitions of this kind can be found in other files. E.g., `data/equiv/transfer_instance` does it for many algebraic type classes like `group`, `module`, etc. * group structure on `equiv.perm α`. More lemmas about `equiv.perm` can be found in `data/equiv/perm`. ## Tags equivalence, congruence, bijective map -/ open function universes u v w z variables {α : Sort u} {β : Sort v} {γ : Sort w} /-- `α ≃ β` is the type of functions from `α → β` with a two-sided inverse. -/ @[nolint has_inhabited_instance] structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix ` ≃ `:25 := equiv /-- Convert an involutive function `f` to an equivalence with `to_fun = inv_fun = f`. -/ def function.involutive.to_equiv (f : α → α) (h : involutive f) : α ≃ α := ⟨f, f, h.left_inverse, h.right_inverse⟩ namespace equiv /-- `perm α` is the type of bijections from `α` to itself. -/ @[reducible] def perm (α : Sort*) := equiv α α instance : has_coe_to_fun (α ≃ β) := ⟨_, to_fun⟩ @[simp] theorem coe_fn_mk (f : α → β) (g l r) : (equiv.mk f g l r : α → β) = f := rfl /-- The map `coe_fn : (r ≃ s) → (r → s)` is injective. -/ theorem injective_coe_fn : function.injective (λ (e : α ≃ β) (x : α), e x) | ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ h := have f₁ = f₂, from h, have g₁ = g₂, from l₁.eq_right_inverse (this.symm ▸ r₂), by simp * @[simp, norm_cast] protected lemma coe_inj {e₁ e₂ : α ≃ β} : ⇑e₁ = e₂ ↔ e₁ = e₂ := injective_coe_fn.eq_iff @[ext] lemma ext {f g : equiv α β} (H : ∀ x, f x = g x) : f = g := injective_coe_fn (funext H) @[ext] lemma perm.ext {σ τ : equiv.perm α} (H : ∀ x, σ x = τ x) : σ = τ := equiv.ext H lemma ext_iff {f g : equiv α β} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, ext⟩ lemma perm.ext_iff {σ τ : equiv.perm α} : σ = τ ↔ ∀ x, σ x = τ x := ext_iff /-- Any type is equivalent to itself. -/ @[refl] protected def refl (α : Sort*) : α ≃ α := ⟨id, id, λ x, rfl, λ x, rfl⟩ instance inhabited' : inhabited (α ≃ α) := ⟨equiv.refl α⟩ /-- Inverse of an equivalence `e : α ≃ β`. -/ @[symm] protected def symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩ /-- See Note [custom simps projection] -/ def simps.inv_fun (e : α ≃ β) : β → α := e.symm initialize_simps_projections equiv (to_fun → apply, inv_fun → symm_apply) /-- Composition of equivalences `e₁ : α ≃ β` and `e₂ : β ≃ γ`. -/ @[trans] protected def trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ := ⟨e₂ ∘ e₁, e₁.symm ∘ e₂.symm, e₂.left_inv.comp e₁.left_inv, e₂.right_inv.comp e₁.right_inv⟩ @[simp] lemma to_fun_as_coe (e : α ≃ β) : e.to_fun = e := rfl @[simp] lemma inv_fun_as_coe (e : α ≃ β) : e.inv_fun = e.symm := rfl protected theorem injective (e : α ≃ β) : injective e := e.left_inv.injective protected theorem surjective (e : α ≃ β) : surjective e := e.right_inv.surjective protected theorem bijective (f : α ≃ β) : bijective f := ⟨f.injective, f.surjective⟩ @[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : set.range e = set.univ := set.eq_univ_of_forall e.surjective protected theorem subsingleton (e : α ≃ β) [subsingleton β] : subsingleton α := e.injective.subsingleton /-- Transfer `decidable_eq` across an equivalence. -/ protected def decidable_eq (e : α ≃ β) [decidable_eq β] : decidable_eq α := e.injective.decidable_eq lemma nonempty_iff_nonempty (e : α ≃ β) : nonempty α ↔ nonempty β := nonempty.congr e e.symm /-- If `α ≃ β` and `β` is inhabited, then so is `α`. -/ protected def inhabited [inhabited β] (e : α ≃ β) : inhabited α := ⟨e.symm (default _)⟩ /-- If `α ≃ β` and `β` is a singleton type, then so is `α`. -/ protected def unique [unique β] (e : α ≃ β) : unique α := e.symm.surjective.unique /-- Equivalence between equal types. -/ protected def cast {α β : Sort*} (h : α = β) : α ≃ β := ⟨cast h, cast h.symm, λ x, by { cases h, refl }, λ x, by { cases h, refl }⟩ @[simp] theorem coe_fn_symm_mk (f : α → β) (g l r) : ((equiv.mk f g l r).symm : β → α) = g := rfl @[simp] theorem coe_refl : ⇑(equiv.refl α) = id := rfl theorem refl_apply (x : α) : equiv.refl α x = x := rfl @[simp] theorem coe_trans (f : α ≃ β) (g : β ≃ γ) : ⇑(f.trans g) = g ∘ f := rfl theorem trans_apply (f : α ≃ β) (g : β ≃ γ) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem apply_symm_apply (e : α ≃ β) (x : β) : e (e.symm x) = x := e.right_inv x @[simp] theorem symm_apply_apply (e : α ≃ β) (x : α) : e.symm (e x) = x := e.left_inv x @[simp] theorem symm_comp_self (e : α ≃ β) : e.symm ∘ e = id := funext e.symm_apply_apply @[simp] theorem self_comp_symm (e : α ≃ β) : e ∘ e.symm = id := funext e.apply_symm_apply @[simp] lemma symm_trans_apply (f : α ≃ β) (g : β ≃ γ) (a : γ) : (f.trans g).symm a = f.symm (g.symm a) := rfl @[simp] theorem apply_eq_iff_eq (f : α ≃ β) {x y : α} : f x = f y ↔ x = y := f.injective.eq_iff theorem apply_eq_iff_eq_symm_apply {α β : Sort*} (f : α ≃ β) {x : α} {y : β} : f x = y ↔ x = f.symm y := begin conv_lhs { rw ←apply_symm_apply f y, }, rw apply_eq_iff_eq, end @[simp] theorem cast_apply {α β} (h : α = β) (x : α) : equiv.cast h x = cast h x := rfl lemma symm_apply_eq {α β} (e : α ≃ β) {x y} : e.symm x = y ↔ x = e y := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_symm_apply {α β} (e : α ≃ β) {x y} : y = e.symm x ↔ e y = x := (eq_comm.trans e.symm_apply_eq).trans eq_comm @[simp] theorem symm_symm (e : α ≃ β) : e.symm.symm = e := by { cases e, refl } @[simp] theorem trans_refl (e : α ≃ β) : e.trans (equiv.refl β) = e := by { cases e, refl } @[simp] theorem refl_symm : (equiv.refl α).symm = equiv.refl α := rfl @[simp] theorem refl_trans (e : α ≃ β) : (equiv.refl α).trans e = e := by { cases e, refl } @[simp] theorem symm_trans (e : α ≃ β) : e.symm.trans e = equiv.refl β := ext (by simp) @[simp] theorem trans_symm (e : α ≃ β) : e.trans e.symm = equiv.refl α := ext (by simp) lemma trans_assoc {δ} (ab : α ≃ β) (bc : β ≃ γ) (cd : γ ≃ δ) : (ab.trans bc).trans cd = ab.trans (bc.trans cd) := equiv.ext $ assume a, rfl theorem left_inverse_symm (f : equiv α β) : left_inverse f.symm f := f.left_inv theorem right_inverse_symm (f : equiv α β) : function.right_inverse f.symm f := f.right_inv /-- If `α` is equivalent to `β` and `γ` is equivalent to `δ`, then the type of equivalences `α ≃ γ` is equivalent to the type of equivalences `β ≃ δ`. -/ def equiv_congr {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (α ≃ γ) ≃ (β ≃ δ) := ⟨ λac, (ab.symm.trans ac).trans cd, λbd, ab.trans $ bd.trans $ cd.symm, assume ac, by { ext x, simp }, assume ac, by { ext x, simp } ⟩ /-- If `α` is equivalent to `β`, then `perm α` is equivalent to `perm β`. -/ def perm_congr {α : Type*} {β : Type*} (e : α ≃ β) : perm α ≃ perm β := equiv_congr e e @[simp] lemma perm_congr_apply {α β : Type*} (e : α ≃ β) (p : equiv.perm α) (x) : e.perm_congr p x = e (p (e.symm x)) := rfl protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s := set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : t ⊆ e '' s ↔ e.symm '' t ⊆ s := by rw [set.image_subset_iff, e.image_eq_preimage] lemma symm_image_image {α β} (f : equiv α β) (s : set α) : f.symm '' (f '' s) = s := by { rw [← set.image_comp], simp } protected lemma image_compl {α β} (f : equiv α β) (s : set α) : f '' sᶜ = (f '' s)ᶜ := set.image_compl_eq f.bijective /-! ### The group of permutations (self-equivalences) of a type `α` -/ namespace perm instance perm_group {α : Type u} : group (perm α) := begin refine { mul := λ f g, equiv.trans g f, one := equiv.refl α, inv:= equiv.symm, ..}; intros; apply equiv.ext; try { apply trans_apply }, apply symm_apply_apply end theorem mul_apply {α : Type u} (f g : perm α) (x) : (f * g) x = f (g x) := equiv.trans_apply _ _ _ theorem one_apply {α : Type u} (x) : (1 : perm α) x = x := rfl @[simp] lemma inv_apply_self {α : Type u} (f : perm α) (x) : f⁻¹ (f x) = x := equiv.symm_apply_apply _ _ @[simp] lemma apply_inv_self {α : Type u} (f : perm α) (x) : f (f⁻¹ x) = x := equiv.apply_symm_apply _ _ lemma one_def {α : Type u} : (1 : perm α) = equiv.refl α := rfl lemma mul_def {α : Type u} (f g : perm α) : f * g = g.trans f := rfl lemma inv_def {α : Type u} (f : perm α) : f⁻¹ = f.symm := rfl @[simp] lemma coe_mul {α : Type u} (f g : perm α) : ⇑(f * g) = f ∘ g := rfl @[simp] lemma coe_one {α : Type u} : ⇑(1 : perm α) = id := rfl end perm /-- If `α` is an empty type, then it is equivalent to the `empty` type. -/ def equiv_empty (h : α → false) : α ≃ empty := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ /-- `false` is equivalent to `empty`. -/ def false_equiv_empty : false ≃ empty := equiv_empty _root_.id /-- If `α` is an empty type, then it is equivalent to the `pempty` type in any universe. -/ def {u' v'} equiv_pempty {α : Sort v'} (h : α → false) : α ≃ pempty.{u'} := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ /-- `false` is equivalent to `pempty`. -/ def false_equiv_pempty : false ≃ pempty := equiv_pempty _root_.id /-- `empty` is equivalent to `pempty`. -/ def empty_equiv_pempty : empty ≃ pempty := equiv_pempty $ empty.rec _ /-- `pempty` types from any two universes are equivalent. -/ def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} := equiv_pempty pempty.elim /-- If `α` is not `nonempty`, then it is equivalent to `empty`. -/ def empty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ empty := equiv_empty $ assume a, h ⟨a⟩ /-- If `α` is not `nonempty`, then it is equivalent to `pempty`. -/ def pempty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ pempty := equiv_pempty $ assume a, h ⟨a⟩ /-- The `Sort` of proofs of a true proposition is equivalent to `punit`. -/ def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit := ⟨λ x, (), λ x, h, λ _, rfl, λ ⟨⟩, rfl⟩ /-- `true` is equivalent to `punit`. -/ def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial /-- `ulift α` is equivalent to `α`. -/ @[simps apply symm_apply {fully_applied := ff}] protected def ulift {α : Type v} : ulift.{u} α ≃ α := ⟨ulift.down, ulift.up, ulift.up_down, λ a, rfl⟩ /-- `plift α` is equivalent to `α`. -/ @[simps apply symm_apply {fully_applied := ff}] protected def plift : plift α ≃ α := ⟨plift.down, plift.up, plift.up_down, plift.down_up⟩ /-- equivalence of propositions is the same as iff -/ def of_iff {P Q : Prop} (h : P ↔ Q) : P ≃ Q := { to_fun := h.mp, inv_fun := h.mpr, left_inv := λ x, rfl, right_inv := λ y, rfl } /-- If `α₁` is equivalent to `α₂` and `β₁` is equivalent to `β₂`, then the type of maps `α₁ → β₁` is equivalent to the type of maps `α₂ → β₂`. -/ @[congr, simps apply] def arrow_congr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := { to_fun := λ f, e₂ ∘ f ∘ e₁.symm, inv_fun := λ f, e₂.symm ∘ f ∘ e₁, left_inv := λ f, funext $ λ x, by simp, right_inv := λ f, funext $ λ x, by simp } lemma arrow_congr_comp {α₁ β₁ γ₁ α₂ β₂ γ₂ : Sort*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) (ec : γ₁ ≃ γ₂) (f : α₁ → β₁) (g : β₁ → γ₁) : arrow_congr ea ec (g ∘ f) = (arrow_congr eb ec g) ∘ (arrow_congr ea eb f) := by { ext, simp only [comp, arrow_congr_apply, eb.symm_apply_apply] } @[simp] lemma arrow_congr_refl {α β : Sort*} : arrow_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Sort*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := rfl @[simp] lemma arrow_congr_symm {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := rfl /-- A version of `equiv.arrow_congr` in `Type`, rather than `Sort`. The `equiv_rw` tactic is not able to use the default `Sort` level `equiv.arrow_congr`, because Lean's universe rules will not unify `?l_1` with `imax (1 ?m_1)`. -/ @[congr, simps apply {rhs_md := semireducible, simp_rhs := tt}] def arrow_congr' {α₁ β₁ α₂ β₂ : Type*} (hα : α₁ ≃ α₂) (hβ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := equiv.arrow_congr hα hβ @[simp] lemma arrow_congr'_refl {α β : Type*} : arrow_congr' (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr'_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Type*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr' (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr' e₁ e₁').trans (arrow_congr' e₂ e₂') := rfl @[simp] lemma arrow_congr'_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr' e₁ e₂).symm = arrow_congr' e₁.symm e₂.symm := rfl /-- Conjugate a map `f : α → α` by an equivalence `α ≃ β`. -/ @[simps apply {rhs_md := semireducible, simp_rhs := tt}] def conj (e : α ≃ β) : (α → α) ≃ (β → β) := arrow_congr e e @[simp] lemma conj_refl : conj (equiv.refl α) = equiv.refl (α → α) := rfl @[simp] lemma conj_symm (e : α ≃ β) : e.conj.symm = e.symm.conj := rfl @[simp] lemma conj_trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : (e₁.trans e₂).conj = e₁.conj.trans e₂.conj := rfl -- This should not be a simp lemma as long as `(∘)` is reducible: -- when `(∘)` is reducible, Lean can unify `f₁ ∘ f₂` with any `g` using -- `f₁ := g` and `f₂ := λ x, x`. This causes nontermination. lemma conj_comp (e : α ≃ β) (f₁ f₂ : α → α) : e.conj (f₁ ∘ f₂) = (e.conj f₁) ∘ (e.conj f₂) := by apply arrow_congr_comp section binary_op variables {α₁ β₁ : Type*} (e : α₁ ≃ β₁) (f : α₁ → α₁ → α₁) lemma semiconj_conj (f : α₁ → α₁) : semiconj e f (e.conj f) := λ x, by simp lemma semiconj₂_conj : semiconj₂ e f (e.arrow_congr e.conj f) := λ x y, by simp instance [is_associative α₁ f] : is_associative β₁ (e.arrow_congr (e.arrow_congr e) f) := (e.semiconj₂_conj f).is_associative_right e.surjective instance [is_idempotent α₁ f] : is_idempotent β₁ (e.arrow_congr (e.arrow_congr e) f) := (e.semiconj₂_conj f).is_idempotent_right e.surjective instance [is_left_cancel α₁ f] : is_left_cancel β₁ (e.arrow_congr (e.arrow_congr e) f) := ⟨e.surjective.forall₃.2 $ λ x y z, by simpa using @is_left_cancel.left_cancel _ f _ x y z⟩ instance [is_right_cancel α₁ f] : is_right_cancel β₁ (e.arrow_congr (e.arrow_congr e) f) := ⟨e.surjective.forall₃.2 $ λ x y z, by simpa using @is_right_cancel.right_cancel _ f _ x y z⟩ end binary_op /-- `punit` sorts in any two universes are equivalent. -/ def punit_equiv_punit : punit.{v} ≃ punit.{w} := ⟨λ _, punit.star, λ _, punit.star, λ u, by { cases u, refl }, λ u, by { cases u, reflexivity }⟩ section /-- The sort of maps to `punit.{v}` is equivalent to `punit.{w}`. -/ def arrow_punit_equiv_punit (α : Sort*) : (α → punit.{v}) ≃ punit.{w} := ⟨λ f, punit.star, λ u f, punit.star, λ f, by { funext x, cases f x, refl }, λ u, by { cases u, reflexivity }⟩ /-- The sort of maps from `punit` is equivalent to the codomain. -/ def punit_arrow_equiv (α : Sort*) : (punit.{u} → α) ≃ α := ⟨λ f, f punit.star, λ a u, a, λ f, by { ext ⟨⟩, refl }, λ u, rfl⟩ /-- The sort of maps from `empty` is equivalent to `punit`. -/ def empty_arrow_equiv_punit (α : Sort*) : (empty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩ /-- The sort of maps from `pempty` is equivalent to `punit`. -/ def pempty_arrow_equiv_punit (α : Sort*) : (pempty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩ /-- The sort of maps from `false` is equivalent to `punit`. -/ def false_arrow_equiv_punit (α : Sort*) : (false → α) ≃ punit.{u} := calc (false → α) ≃ (empty → α) : arrow_congr false_equiv_empty (equiv.refl _) ... ≃ punit : empty_arrow_equiv_punit _ end /-- Product of two equivalences. If `α₁ ≃ α₂` and `β₁ ≃ β₂`, then `α₁ × β₁ ≃ α₂ × β₂`. -/ @[congr, simps apply] def prod_congr {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : α₁ × β₁ ≃ α₂ × β₂ := ⟨prod.map e₁ e₂, prod.map e₁.symm e₂.symm, λ ⟨a, b⟩, by simp, λ ⟨a, b⟩, by simp⟩ @[simp] theorem prod_congr_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (prod_congr e₁ e₂).symm = prod_congr e₁.symm e₂.symm := rfl /-- Type product is commutative up to an equivalence: `α × β ≃ β × α`. -/ @[simps apply] def prod_comm (α β : Type*) : α × β ≃ β × α := ⟨prod.swap, prod.swap, λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩ @[simp] lemma prod_comm_symm (α β) : (prod_comm α β).symm = prod_comm β α := rfl /-- Type product is associative up to an equivalence. -/ @[simps] def prod_assoc (α β γ : Sort*) : (α × β) × γ ≃ α × (β × γ) := ⟨λ p, (p.1.1, p.1.2, p.2), λp, ((p.1, p.2.1), p.2.2), λ ⟨⟨a, b⟩, c⟩, rfl, λ ⟨a, ⟨b, c⟩⟩, rfl⟩ lemma prod_assoc_preimage {α β γ} {s : set α} {t : set β} {u : set γ} : equiv.prod_assoc α β γ ⁻¹' s.prod (t.prod u) = (s.prod t).prod u := by { ext, simp [and_assoc] } section /-- `punit` is a right identity for type product up to an equivalence. -/ @[simps apply] def prod_punit (α : Type*) : α × punit.{u+1} ≃ α := ⟨λ p, p.1, λ a, (a, punit.star), λ ⟨_, punit.star⟩, rfl, λ a, rfl⟩ /-- `punit` is a left identity for type product up to an equivalence. -/ @[simps apply {rhs_md := semireducible, simp_rhs := tt}] def punit_prod (α : Type*) : punit.{u+1} × α ≃ α := calc punit × α ≃ α × punit : prod_comm _ _ ... ≃ α : prod_punit _ /-- `empty` type is a right absorbing element for type product up to an equivalence. -/ def prod_empty (α : Type*) : α × empty ≃ empty := equiv_empty (λ ⟨_, e⟩, e.rec _) /-- `empty` type is a left absorbing element for type product up to an equivalence. -/ def empty_prod (α : Type*) : empty × α ≃ empty := equiv_empty (λ ⟨e, _⟩, e.rec _) /-- `pempty` type is a right absorbing element for type product up to an equivalence. -/ def prod_pempty (α : Type*) : α × pempty ≃ pempty := equiv_pempty (λ ⟨_, e⟩, e.rec _) /-- `pempty` type is a left absorbing element for type product up to an equivalence. -/ def pempty_prod (α : Type*) : pempty × α ≃ pempty := equiv_pempty (λ ⟨e, _⟩, e.rec _) end section open sum /-- `psum` is equivalent to `sum`. -/ def psum_equiv_sum (α β : Type*) : psum α β ≃ α ⊕ β := ⟨λ s, psum.cases_on s inl inr, λ s, sum.cases_on s psum.inl psum.inr, λ s, by cases s; refl, λ s, by cases s; refl⟩ /-- If `α ≃ α'` and `β ≃ β'`, then `α ⊕ β ≃ α' ⊕ β'`. -/ @[simps apply] def sum_congr {α₁ β₁ α₂ β₂ : Type*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) : α₁ ⊕ β₁ ≃ α₂ ⊕ β₂ := ⟨sum.map ea eb, sum.map ea.symm eb.symm, λ x, by simp, λ x, by simp⟩ @[simp] lemma sum_congr_symm {α β γ δ : Type u} (e : α ≃ β) (f : γ ≃ δ) : (equiv.sum_congr e f).symm = (equiv.sum_congr (e.symm) (f.symm)) := rfl /-- `bool` is equivalent the sum of two `punit`s. -/ def bool_equiv_punit_sum_punit : bool ≃ punit.{u+1} ⊕ punit.{v+1} := ⟨λ b, cond b (inr punit.star) (inl punit.star), λ s, sum.rec_on s (λ_, ff) (λ_, tt), λ b, by cases b; refl, λ s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩ /-- `Prop` is noncomputably equivalent to `bool`. -/ noncomputable def Prop_equiv_bool : Prop ≃ bool := ⟨λ p, @to_bool p (classical.prop_decidable _), λ b, b, λ p, by simp, λ b, by simp⟩ /-- Sum of types is commutative up to an equivalence. -/ @[simps apply] def sum_comm (α β : Sort*) : α ⊕ β ≃ β ⊕ α := ⟨sum.swap, sum.swap, sum.swap_swap, sum.swap_swap⟩ @[simp] lemma sum_comm_symm (α β) : (sum_comm α β).symm = sum_comm β α := rfl /-- Sum of types is associative up to an equivalence. -/ def sum_assoc (α β γ : Sort*) : (α ⊕ β) ⊕ γ ≃ α ⊕ (β ⊕ γ) := ⟨sum.elim (sum.elim sum.inl (sum.inr ∘ sum.inl)) (sum.inr ∘ sum.inr), sum.elim (sum.inl ∘ sum.inl) $ sum.elim (sum.inl ∘ sum.inr) sum.inr, by rintros (⟨_ | _⟩ | _); refl, by rintros (_ | ⟨_ | _⟩); refl⟩ @[simp] theorem sum_assoc_apply_in1 {α β γ} (a) : sum_assoc α β γ (inl (inl a)) = inl a := rfl @[simp] theorem sum_assoc_apply_in2 {α β γ} (b) : sum_assoc α β γ (inl (inr b)) = inr (inl b) := rfl @[simp] theorem sum_assoc_apply_in3 {α β γ} (c) : sum_assoc α β γ (inr c) = inr (inr c) := rfl /-- Sum with `empty` is equivalent to the original type. -/ def sum_empty (α : Type*) : α ⊕ empty ≃ α := ⟨sum.elim id (empty.rec _), inl, λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, λ a, rfl⟩ @[simp] lemma sum_empty_apply_inl {α} (a) : sum_empty α (sum.inl a) = a := rfl /-- The sum of `empty` with any `Sort*` is equivalent to the right summand. -/ def empty_sum (α : Sort*) : empty ⊕ α ≃ α := (sum_comm _ _).trans $ sum_empty _ @[simp] lemma empty_sum_apply_inr {α} (a) : empty_sum α (sum.inr a) = a := rfl /-- Sum with `pempty` is equivalent to the original type. -/ def sum_pempty (α : Type*) : α ⊕ pempty ≃ α := ⟨sum.elim id (pempty.rec _), inl, λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, λ a, rfl⟩ @[simp] lemma sum_pempty_apply_inl {α} (a) : sum_pempty α (sum.inl a) = a := rfl /-- The sum of `pempty` with any `Sort*` is equivalent to the right summand. -/ def pempty_sum (α : Sort*) : pempty ⊕ α ≃ α := (sum_comm _ _).trans $ sum_pempty _ @[simp] lemma pempty_sum_apply_inr {α} (a) : pempty_sum α (sum.inr a) = a := rfl /-- `option α` is equivalent to `α ⊕ punit` -/ def option_equiv_sum_punit (α : Type*) : option α ≃ α ⊕ punit.{u+1} := ⟨λ o, match o with none := inr punit.star | some a := inl a end, λ s, match s with inr _ := none | inl a := some a end, λ o, by cases o; refl, λ s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩ @[simp] lemma option_equiv_sum_punit_none {α} : option_equiv_sum_punit α none = sum.inr punit.star := rfl @[simp] lemma option_equiv_sum_punit_some {α} (a) : option_equiv_sum_punit α (some a) = sum.inl a := rfl @[simp] lemma option_equiv_sum_punit_coe {α} (a : α) : option_equiv_sum_punit α a = sum.inl a := rfl @[simp] lemma option_equiv_sum_punit_symm_inl {α} (a) : (option_equiv_sum_punit α).symm (sum.inl a) = a := rfl @[simp] lemma option_equiv_sum_punit_symm_inr {α} (a) : (option_equiv_sum_punit α).symm (sum.inr a) = none := rfl /-- The set of `x : option α` such that `is_some x` is equivalent to `α`. -/ def option_is_some_equiv (α : Type*) : {x : option α // x.is_some} ≃ α := { to_fun := λ o, option.get o.2, inv_fun := λ x, ⟨some x, dec_trivial⟩, left_inv := λ o, subtype.eq $ option.some_get _, right_inv := λ x, option.get_some _ _ } /-- `α ⊕ β` is equivalent to a `sigma`-type over `bool`. -/ def sum_equiv_sigma_bool (α β : Sort*) : α ⊕ β ≃ (Σ b: bool, cond b α β) := ⟨λ s, match s with inl a := ⟨tt, a⟩ | inr b := ⟨ff, b⟩ end, λ s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end, λ s, by cases s; refl, λ s, by rcases s with ⟨_|_, _⟩; refl⟩ /-- `sigma_preimage_equiv f` for `f : α → β` is the natural equivalence between the type of all fibres of `f` and the total space `α`. -/ @[simps] def sigma_preimage_equiv {α β : Type*} (f : α → β) : (Σ y : β, {x // f x = y}) ≃ α := ⟨λ x, ↑x.2, λ x, ⟨f x, x, rfl⟩, λ ⟨y, x, rfl⟩, rfl, λ x, rfl⟩ /-- A set `s` in `α × β` is equivalent to the sigma-type `Σ x, {y | (x, y) ∈ s}`. -/ def set_prod_equiv_sigma {α β : Type*} (s : set (α × β)) : s ≃ Σ x : α, {y | (x, y) ∈ s} := { to_fun := λ x, ⟨x.1.1, x.1.2, by simp⟩, inv_fun := λ x, ⟨(x.1, x.2.1), x.2.2⟩, left_inv := λ ⟨⟨x, y⟩, h⟩, rfl, right_inv := λ ⟨x, y, h⟩, rfl } end section sum_compl /-- For any predicate `p` on `α`, the sum of the two subtypes `{a // p a}` and its complement `{a // ¬ p a}` is naturally equivalent to `α`. -/ def sum_compl {α : Type*} (p : α → Prop) [decidable_pred p] : {a // p a} ⊕ {a // ¬ p a} ≃ α := { to_fun := sum.elim coe coe, inv_fun := λ a, if h : p a then sum.inl ⟨a, h⟩ else sum.inr ⟨a, h⟩, left_inv := by { rintros (⟨x,hx⟩|⟨x,hx⟩); dsimp; [rw dif_pos, rw dif_neg], }, right_inv := λ a, by { dsimp, split_ifs; refl } } @[simp] lemma sum_compl_apply_inl {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // p a}) : sum_compl p (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // ¬ p a}) : sum_compl p (sum.inr x) = x := rfl @[simp] lemma sum_compl_apply_symm_of_pos {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : p a) : (sum_compl p).symm a = sum.inl ⟨a, h⟩ := dif_pos h @[simp] lemma sum_compl_apply_symm_of_neg {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : ¬ p a) : (sum_compl p).symm a = sum.inr ⟨a, h⟩ := dif_neg h end sum_compl section subtype_preimage variables (p : α → Prop) [decidable_pred p] (x₀ : {a // p a} → β) /-- For a fixed function `x₀ : {a // p a} → β` defined on a subtype of `α`, the subtype of functions `x : α → β` that agree with `x₀` on the subtype `{a // p a}` is naturally equivalent to the type of functions `{a // ¬ p a} → β`. -/ @[simps] def subtype_preimage : {x : α → β // x ∘ coe = x₀} ≃ ({a // ¬ p a} → β) := { to_fun := λ (x : {x : α → β // x ∘ coe = x₀}) a, (x : α → β) a, inv_fun := λ x, ⟨λ a, if h : p a then x₀ ⟨a, h⟩ else x ⟨a, h⟩, funext $ λ ⟨a, h⟩, dif_pos h⟩, left_inv := λ ⟨x, hx⟩, subtype.val_injective $ funext $ λ a, (by { dsimp, split_ifs; [ rw ← hx, skip ]; refl }), right_inv := λ x, funext $ λ ⟨a, h⟩, show dite (p a) _ _ = _, by { dsimp, rw [dif_neg h] } } lemma subtype_preimage_symm_apply_coe_pos (x : {a // ¬ p a} → β) (a : α) (h : p a) : ((subtype_preimage p x₀).symm x : α → β) a = x₀ ⟨a, h⟩ := dif_pos h lemma subtype_preimage_symm_apply_coe_neg (x : {a // ¬ p a} → β) (a : α) (h : ¬ p a) : ((subtype_preimage p x₀).symm x : α → β) a = x ⟨a, h⟩ := dif_neg h end subtype_preimage section fun_unique variables (α β) [unique α] /-- If `α` has a unique term, then the type of function `α → β` is equivalent to `β`. -/ @[simps] def fun_unique : (α → β) ≃ β := { to_fun := λ f, f (default α), inv_fun := λ b a, b, left_inv := λ f, funext $ λ a, congr_arg f $ subsingleton.elim _ _, right_inv := λ b, rfl } end fun_unique section /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Π a, β₁ a` and `Π a, β₂ a`. -/ def Pi_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Π a, β₁ a) ≃ (Π a, β₂ a) := ⟨λ H a, F a (H a), λ H a, (F a).symm (H a), λ H, funext $ by simp, λ H, funext $ by simp⟩ /-- Dependent `curry` equivalence: the type of dependent functions on `Σ i, β i` is equivalent to the type of dependent functions of two arguments (i.e., functions to the space of functions). -/ def Pi_curry {α} {β : α → Sort*} (γ : Π a, β a → Sort*) : (Π x : Σ i, β i, γ x.1 x.2) ≃ (Π a b, γ a b) := { to_fun := λ f x y, f ⟨x,y⟩, inv_fun := λ f x, f x.1 x.2, left_inv := λ f, funext $ λ ⟨x,y⟩, rfl, right_inv := λ f, funext $ λ x, funext $ λ y, rfl } end section /-- A `psigma`-type is equivalent to the corresponding `sigma`-type. -/ @[simps apply symm_apply] def psigma_equiv_sigma {α} (β : α → Sort*) : (Σ' i, β i) ≃ Σ i, β i := ⟨λ a, ⟨a.1, a.2⟩, λ a, ⟨a.1, a.2⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Σ a, β₁ a` and `Σ a, β₂ a`. -/ @[simps apply symm_apply] def sigma_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Σ a, β₁ a) ≃ Σ a, β₂ a := ⟨λ a, ⟨a.1, F a.1 a.2⟩, λ a, ⟨a.1, (F a.1).symm a.2⟩, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_symm_apply (F a) b⟩ /-- An equivalence `f : α₁ ≃ α₂` generates an equivalence between `Σ a, β (f a)` and `Σ a, β a`. -/ @[simps apply] def sigma_congr_left {α₁ α₂} {β : α₂ → Sort*} (e : α₁ ≃ α₂) : (Σ a:α₁, β (e a)) ≃ (Σ a:α₂, β a) := ⟨λ a, ⟨e a.1, a.2⟩, λ a, ⟨e.symm a.1, @@eq.rec β a.2 (e.right_inv a.1).symm⟩, λ ⟨a, b⟩, match e.symm (e a), e.left_inv a : ∀ a' (h : a' = a), @sigma.mk _ (β ∘ e) _ (@@eq.rec β b (congr_arg e h.symm)) = ⟨a, b⟩ with | _, rfl := rfl end, λ ⟨a, b⟩, match e (e.symm a), _ : ∀ a' (h : a' = a), sigma.mk a' (@@eq.rec β b h.symm) = ⟨a, b⟩ with | _, rfl := rfl end⟩ /-- Transporting a sigma type through an equivalence of the base -/ def sigma_congr_left' {α₁ α₂} {β : α₁ → Sort*} (f : α₁ ≃ α₂) : (Σ a:α₁, β a) ≃ (Σ a:α₂, β (f.symm a)) := (sigma_congr_left f.symm).symm /-- Transporting a sigma type through an equivalence of the base and a family of equivalences of matching fibers -/ def sigma_congr {α₁ α₂} {β₁ : α₁ → Sort*} {β₂ : α₂ → Sort*} (f : α₁ ≃ α₂) (F : ∀ a, β₁ a ≃ β₂ (f a)) : sigma β₁ ≃ sigma β₂ := (sigma_congr_right F).trans (sigma_congr_left f) /-- `sigma` type with a constant fiber is equivalent to the product. -/ @[simps apply symm_apply] def sigma_equiv_prod (α β : Type*) : (Σ_:α, β) ≃ α × β := ⟨λ a, ⟨a.1, a.2⟩, λ a, ⟨a.1, a.2⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ /-- If each fiber of a `sigma` type is equivalent to a fixed type, then the sigma type is equivalent to the product. -/ def sigma_equiv_prod_of_equiv {α β} {β₁ : α → Sort*} (F : Π a, β₁ a ≃ β) : sigma β₁ ≃ α × β := (sigma_congr_right F).trans (sigma_equiv_prod α β) end section prod_congr variables {α₁ β₁ β₂ : Type*} (e : α₁ → β₁ ≃ β₂) /-- A family of equivalences `Π (a : α₁), β₁ ≃ β₂` generates an equivalence between `β₁ × α₁` and `β₂ × α₁`. -/ def prod_congr_left : β₁ × α₁ ≃ β₂ × α₁ := { to_fun := λ ab, ⟨e ab.2 ab.1, ab.2⟩, inv_fun := λ ab, ⟨(e ab.2).symm ab.1, ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_left_apply (b : β₁) (a : α₁) : prod_congr_left e (b, a) = (e a b, a) := rfl lemma prod_congr_refl_right (e : β₁ ≃ β₂) : prod_congr e (equiv.refl α₁) = prod_congr_left (λ _, e) := by { ext ⟨a, b⟩ : 1, simp } /-- A family of equivalences `Π (a : α₁), β₁ ≃ β₂` generates an equivalence between `α₁ × β₁` and `α₁ × β₂`. -/ def prod_congr_right : α₁ × β₁ ≃ α₁ × β₂ := { to_fun := λ ab, ⟨ab.1, e ab.1 ab.2⟩, inv_fun := λ ab, ⟨ab.1, (e ab.1).symm ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_right_apply (a : α₁) (b : β₁) : prod_congr_right e (a, b) = (a, e a b) := rfl lemma prod_congr_refl_left (e : β₁ ≃ β₂) : prod_congr (equiv.refl α₁) e = prod_congr_right (λ _, e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_left_trans_prod_comm : (prod_congr_left e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_right_trans_prod_comm : (prod_congr_right e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_left e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_congr_right_sigma_equiv_prod : (sigma_congr_right e).trans (sigma_equiv_prod α₁ β₂) = (sigma_equiv_prod α₁ β₁).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_equiv_prod_sigma_congr_right : (sigma_equiv_prod α₁ β₁).symm.trans (sigma_congr_right e) = (prod_congr_right e).trans (sigma_equiv_prod α₁ β₂).symm := by { ext ⟨a, b⟩ : 1, simp } end prod_congr namespace perm variables {α₁ β₁ β₂ : Type*} [decidable_eq α₁] (a : α₁) (e : perm β₁) /-- `prod_extend_right a e` extends `e : perm β` to `perm (α × β)` by sending `(a, b)` to `(a, e b)` and keeping the other `(a', b)` fixed. -/ def prod_extend_right : perm (α₁ × β₁) := { to_fun := λ ab, if ab.fst = a then (a, e ab.snd) else ab, inv_fun := λ ab, if ab.fst = a then (a, e⁻¹ ab.snd) else ab, left_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] }, right_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] } } @[simp] lemma prod_extend_right_apply_eq (b : β₁) : prod_extend_right a e (a, b) = (a, e b) := if_pos rfl lemma prod_extend_right_apply_ne {a a' : α₁} (h : a' ≠ a) (b : β₁) : prod_extend_right a e (a', b) = (a', b) := if_neg h lemma eq_of_prod_extend_right_ne {e : perm β₁} {a a' : α₁} {b : β₁} (h : prod_extend_right a e (a', b) ≠ (a', b)) : a' = a := by { contrapose! h, exact prod_extend_right_apply_ne _ h _ } @[simp] lemma fst_prod_extend_right (ab : α₁ × β₁) : (prod_extend_right a e ab).fst = ab.fst := begin rw [prod_extend_right, coe_fn_mk], split_ifs with h, { rw h }, { refl } end end perm section /-- The type of functions to a product `α × β` is equivalent to the type of pairs of functions `γ → α` and `γ → β`. -/ def arrow_prod_equiv_prod_arrow (α β γ : Type*) : (γ → α × β) ≃ (γ → α) × (γ → β) := ⟨λ f, (λ c, (f c).1, λ c, (f c).2), λ p c, (p.1 c, p.2 c), λ f, funext $ λ c, prod.mk.eta, λ p, by { cases p, refl }⟩ /-- Functions `α → β → γ` are equivalent to functions on `α × β`. -/ def arrow_arrow_equiv_prod_arrow (α β γ : Sort*) : (α → β → γ) ≃ (α × β → γ) := ⟨uncurry, curry, curry_uncurry, uncurry_curry⟩ open sum /-- The type of functions on a sum type `α ⊕ β` is equivalent to the type of pairs of functions on `α` and on `β`. -/ def sum_arrow_equiv_prod_arrow (α β γ : Type*) : ((α ⊕ β) → γ) ≃ (α → γ) × (β → γ) := ⟨λ f, (f ∘ inl, f ∘ inr), λ p, sum.elim p.1 p.2, λ f, by { ext ⟨⟩; refl }, λ p, by { cases p, refl }⟩ /-- Type product is right distributive with respect to type sum up to an equivalence. -/ def sum_prod_distrib (α β γ : Sort*) : (α ⊕ β) × γ ≃ (α × γ) ⊕ (β × γ) := ⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end, λ s, match s with inl q := (inl q.1, q.2) | inr q := (inr q.1, q.2) end, λ p, by rcases p with ⟨_ | _, _⟩; refl, λ s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩ @[simp] theorem sum_prod_distrib_apply_left {α β γ} (a : α) (c : γ) : sum_prod_distrib α β γ (sum.inl a, c) = sum.inl (a, c) := rfl @[simp] theorem sum_prod_distrib_apply_right {α β γ} (b : β) (c : γ) : sum_prod_distrib α β γ (sum.inr b, c) = sum.inr (b, c) := rfl /-- Type product is left distributive with respect to type sum up to an equivalence. -/ def prod_sum_distrib (α β γ : Sort*) : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ) := calc α × (β ⊕ γ) ≃ (β ⊕ γ) × α : prod_comm _ _ ... ≃ (β × α) ⊕ (γ × α) : sum_prod_distrib _ _ _ ... ≃ (α × β) ⊕ (α × γ) : sum_congr (prod_comm _ _) (prod_comm _ _) @[simp] theorem prod_sum_distrib_apply_left {α β γ} (a : α) (b : β) : prod_sum_distrib α β γ (a, sum.inl b) = sum.inl (a, b) := rfl @[simp] theorem prod_sum_distrib_apply_right {α β γ} (a : α) (c : γ) : prod_sum_distrib α β γ (a, sum.inr c) = sum.inr (a, c) := rfl /-- The product of an indexed sum of types (formally, a `sigma`-type `Σ i, α i`) by a type `β` is equivalent to the sum of products `Σ i, (α i × β)`. -/ def sigma_prod_distrib {ι : Type*} (α : ι → Type*) (β : Type*) : ((Σ i, α i) × β) ≃ (Σ i, (α i × β)) := ⟨λ p, ⟨p.1.1, (p.1.2, p.2)⟩, λ p, (⟨p.1, p.2.1⟩, p.2.2), λ p, by { rcases p with ⟨⟨_, _⟩, _⟩, refl }, λ p, by { rcases p with ⟨_, ⟨_, _⟩⟩, refl }⟩ /-- The product `bool × α` is equivalent to `α ⊕ α`. -/ def bool_prod_equiv_sum (α : Type u) : bool × α ≃ α ⊕ α := calc bool × α ≃ (unit ⊕ unit) × α : prod_congr bool_equiv_punit_sum_punit (equiv.refl _) ... ≃ (unit × α) ⊕ (unit × α) : sum_prod_distrib _ _ _ ... ≃ α ⊕ α : sum_congr (punit_prod _) (punit_prod _) /-- The function type `bool → α` is equivalent to `α × α`. -/ def bool_to_equiv_prod (α : Type u) : (bool → α) ≃ α × α := calc (bool → α) ≃ ((unit ⊕ unit) → α) : (arrow_congr bool_equiv_punit_sum_punit (equiv.refl α)) ... ≃ (unit → α) × (unit → α) : sum_arrow_equiv_prod_arrow _ _ _ ... ≃ α × α : prod_congr (punit_arrow_equiv _) (punit_arrow_equiv _) @[simp] lemma bool_to_equiv_prod_apply {α : Type u} (f : bool → α) : bool_to_equiv_prod α f = (f ff, f tt) := rfl @[simp] lemma bool_to_equiv_prod_symm_apply_ff {α : Type u} (p : α × α) : (bool_to_equiv_prod α).symm p ff = p.1 := rfl @[simp] lemma bool_to_equiv_prod_symm_apply_tt {α : Type u} (p : α × α) : (bool_to_equiv_prod α).symm p tt = p.2 := rfl end section open sum nat /-- The set of natural numbers is equivalent to `ℕ ⊕ punit`. -/ def nat_equiv_nat_sum_punit : ℕ ≃ ℕ ⊕ punit.{u+1} := ⟨λ n, match n with zero := inr punit.star | succ a := inl a end, λ s, match s with inl n := succ n | inr punit.star := zero end, λ n, begin cases n, repeat { refl } end, λ s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩ /-- `ℕ ⊕ punit` is equivalent to `ℕ`. -/ def nat_sum_punit_equiv_nat : ℕ ⊕ punit.{u+1} ≃ ℕ := nat_equiv_nat_sum_punit.symm /-- The type of integer numbers is equivalent to `ℕ ⊕ ℕ`. -/ def int_equiv_nat_sum_nat : ℤ ≃ ℕ ⊕ ℕ := by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl} end /-- An equivalence between `α` and `β` generates an equivalence between `list α` and `list β`. -/ def list_equiv_of_equiv {α β : Type*} (e : α ≃ β) : list α ≃ list β := { to_fun := list.map e, inv_fun := list.map e.symm, left_inv := λ l, by rw [list.map_map, e.symm_comp_self, list.map_id], right_inv := λ l, by rw [list.map_map, e.self_comp_symm, list.map_id] } /-- `fin n` is equivalent to `{m // m < n}`. -/ def fin_equiv_subtype (n : ℕ) : fin n ≃ {m // m < n} := ⟨λ x, ⟨x.1, x.2⟩, λ x, ⟨x.1, x.2⟩, λ ⟨a, b⟩, rfl,λ ⟨a, b⟩, rfl⟩ /-- If `α` is equivalent to `β`, then `unique α` is equivalent to `β`. -/ def unique_congr (e : α ≃ β) : unique α ≃ unique β := { to_fun := λ h, @equiv.unique _ _ h e.symm, inv_fun := λ h, @equiv.unique _ _ h e, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } section open subtype /-- If `α` is equivalent to `β` and the predicates `p : α → Prop` and `q : β → Prop` are equivalent at corresponding points, then `{a // p a}` is equivalent to `{b // q b}`. -/ def subtype_congr {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ a, p a ↔ q (e a)) : {a : α // p a} ≃ {b : β // q b} := ⟨λ x, ⟨e x, (h _).1 x.2⟩, λ y, ⟨e.symm y, (h _).2 (by { simp, exact y.2 })⟩, λ ⟨x, h⟩, subtype.ext_val $ by simp, λ ⟨y, h⟩, subtype.ext_val $ by simp⟩ @[simp] lemma subtype_congr_apply {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ (a : α), p a ↔ q (e a)) (x : {x // p x}) : e.subtype_congr h x = ⟨e x, (h _).1 x.2⟩ := rfl @[simp] lemma subtype_congr_symm_apply {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ (a : α), p a ↔ q (e a)) (y : {y // q y}) : (e.subtype_congr h).symm y = ⟨e.symm y, (h _).2 $ (e.apply_symm_apply y).symm ▸ y.2⟩ := rfl /-- If two predicates `p` and `q` are pointwise equivalent, then `{x // p x}` is equivalent to `{x // q x}`. -/ @[simps {rhs_md := semireducible, simp_rhs := tt}] def subtype_congr_right {p q : α → Prop} (e : ∀x, p x ↔ q x) : {x // p x} ≃ {x // q x} := subtype_congr (equiv.refl _) e /-- If `α ≃ β`, then for any predicate `p : β → Prop` the subtype `{a // p (e a)}` is equivalent to the subtype `{b // p b}`. -/ def subtype_equiv_of_subtype {p : β → Prop} (e : α ≃ β) : {a : α // p (e a)} ≃ {b : β // p b} := subtype_congr e $ by simp /-- If `α ≃ β`, then for any predicate `p : α → Prop` the subtype `{a // p a}` is equivalent to the subtype `{b // p (e.symm b)}`. This version is used by `equiv_rw`. -/ def subtype_equiv_of_subtype' {p : α → Prop} (e : α ≃ β) : {a : α // p a} ≃ {b : β // p (e.symm b)} := e.symm.subtype_equiv_of_subtype.symm /-- If two predicates are equal, then the corresponding subtypes are equivalent. -/ def subtype_congr_prop {α : Type*} {p q : α → Prop} (h : p = q) : subtype p ≃ subtype q := subtype_congr (equiv.refl α) (assume a, h ▸ iff.rfl) /-- The subtypes corresponding to equal sets are equivalent. -/ @[simps apply {rhs_md := semireducible, simp_rhs := tt}] def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t := subtype_congr_prop h /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. This version allows the “inner” predicate to depend on `h : p a`. -/ def subtype_subtype_equiv_subtype_exists {α : Type u} (p : α → Prop) (q : subtype p → Prop) : subtype q ≃ {a : α // ∃h:p a, q ⟨a, h⟩ } := ⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩, λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by { cases ha, exact ha_h }⟩, assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, h₂⟩, rfl⟩ /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. -/ def subtype_subtype_equiv_subtype_inter {α : Type u} (p q : α → Prop) : {x : subtype p // q x.1} ≃ subtype (λ x, p x ∧ q x) := (subtype_subtype_equiv_subtype_exists p _).trans $ subtype_congr_right $ λ x, exists_prop /-- If the outer subtype has more restrictive predicate than the inner one, then we can drop the latter. -/ def subtype_subtype_equiv_subtype {α : Type u} {p q : α → Prop} (h : ∀ {x}, q x → p x) : {x : subtype p // q x.1} ≃ subtype q := (subtype_subtype_equiv_subtype_inter p _).trans $ subtype_congr_right $ assume x, ⟨and.right, λ h₁, ⟨h h₁, h₁⟩⟩ /-- If a proposition holds for all elements, then the subtype is equivalent to the original type. -/ def subtype_univ_equiv {α : Type u} {p : α → Prop} (h : ∀ x, p x) : subtype p ≃ α := ⟨λ x, x, λ x, ⟨x, h x⟩, λ x, subtype.eq rfl, λ x, rfl⟩ /-- A subtype of a sigma-type is a sigma-type over a subtype. -/ def subtype_sigma_equiv {α : Type u} (p : α → Type v) (q : α → Prop) : { y : sigma p // q y.1 } ≃ Σ(x : subtype q), p x.1 := ⟨λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ ⟨⟨x, h⟩, y⟩, rfl, λ ⟨⟨x, y⟩, h⟩, rfl⟩ /-- A sigma type over a subtype is equivalent to the sigma set over the original type, if the fiber is empty outside of the subset -/ def sigma_subtype_equiv_of_subset {α : Type u} (p : α → Type v) (q : α → Prop) (h : ∀ x, p x → q x) : (Σ x : subtype q, p x) ≃ Σ x : α, p x := (subtype_sigma_equiv p q).symm.trans $ subtype_univ_equiv $ λ x, h x.1 x.2 /-- If a predicate `p : β → Prop` is true on the range of a map `f : α → β`, then `Σ y : {y // p y}, {x // f x = y}` is equivalent to `α`. -/ def sigma_subtype_preimage_equiv {α : Type u} {β : Type v} (f : α → β) (p : β → Prop) (h : ∀ x, p (f x)) : (Σ y : subtype p, {x : α // f x = y}) ≃ α := calc _ ≃ Σ y : β, {x : α // f x = y} : sigma_subtype_equiv_of_subset _ p (λ y ⟨x, h'⟩, h' ▸ h x) ... ≃ α : sigma_preimage_equiv f /-- If for each `x` we have `p x ↔ q (f x)`, then `Σ y : {y // q y}, f ⁻¹' {y}` is equivalent to `{x // p x}`. -/ def sigma_subtype_preimage_equiv_subtype {α : Type u} {β : Type v} (f : α → β) {p : α → Prop} {q : β → Prop} (h : ∀ x, p x ↔ q (f x)) : (Σ y : subtype q, {x : α // f x = y}) ≃ subtype p := calc (Σ y : subtype q, {x : α // f x = y}) ≃ Σ y : subtype q, {x : subtype p // subtype.mk (f x) ((h x).1 x.2) = y} : begin apply sigma_congr_right, assume y, symmetry, refine (subtype_subtype_equiv_subtype_exists _ _).trans (subtype_congr_right _), assume x, exact ⟨λ ⟨hp, h'⟩, congr_arg subtype.val h', λ h', ⟨(h x).2 (h'.symm ▸ y.2), subtype.eq h'⟩⟩ end ... ≃ subtype p : sigma_preimage_equiv (λ x : subtype p, (⟨f x, (h x).1 x.property⟩ : subtype q)) /-- The `pi`-type `Π i, π i` is equivalent to the type of sections `f : ι → Σ i, π i` of the `sigma` type such that for all `i` we have `(f i).fst = i`. -/ def pi_equiv_subtype_sigma (ι : Type*) (π : ι → Type*) : (Πi, π i) ≃ {f : ι → Σi, π i | ∀i, (f i).1 = i } := ⟨ λf, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, λf i, begin rw ← f.2 i, exact (f.1 i).2 end, assume f, funext $ assume i, rfl, assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $ eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩ /-- The set of functions `f : Π a, β a` such that for all `a` we have `p a (f a)` is equivalent to the set of functions `Π a, {b : β a // p a b}`. -/ def subtype_pi_equiv_pi {α : Sort u} {β : α → Sort v} {p : Πa, β a → Prop} : {f : Πa, β a // ∀a, p a (f a) } ≃ Πa, { b : β a // p a b } := ⟨λf a, ⟨f.1 a, f.2 a⟩, λf, ⟨λa, (f a).1, λa, (f a).2⟩, by { rintro ⟨f, h⟩, refl }, by { rintro f, funext a, exact subtype.ext_val rfl }⟩ /-- A subtype of a product defined by componentwise conditions is equivalent to a product of subtypes. -/ def subtype_prod_equiv_prod {α : Type u} {β : Type v} {p : α → Prop} {q : β → Prop} : {c : α × β // p c.1 ∧ q c.2} ≃ ({a // p a} × {b // q b}) := ⟨λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl⟩ end section subtype_equiv_codomain variables {X : Type*} {Y : Type*} [decidable_eq X] {x : X} /-- The type of all functions `X → Y` with prescribed values for all `x' ≠ x` is equivalent to the codomain `Y`. -/ def subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : {g : X → Y // g ∘ coe = f} ≃ Y := (subtype_preimage _ f).trans $ @fun_unique {x' // ¬ x' ≠ x} _ $ show unique {x' // ¬ x' ≠ x}, from @equiv.unique _ _ (show unique {x' // x' = x}, from { default := ⟨x, rfl⟩, uniq := λ ⟨x', h⟩, subtype.val_injective h }) (subtype_congr_right $ λ a, not_not) @[simp] lemma coe_subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : (subtype_equiv_codomain f : {g : X → Y // g ∘ coe = f} → Y) = λ g, (g : X → Y) x := rfl @[simp] lemma subtype_equiv_codomain_apply (f : {x' // x' ≠ x} → Y) (g : {g : X → Y // g ∘ coe = f}) : subtype_equiv_codomain f g = (g : X → Y) x := rfl lemma coe_subtype_equiv_codomain_symm (f : {x' // x' ≠ x} → Y) : ((subtype_equiv_codomain f).symm : Y → {g : X → Y // g ∘ coe = f}) = λ y, ⟨λ x', if h : x' ≠ x then f ⟨x', h⟩ else y, by { funext x', dsimp, erw [dif_pos x'.2, subtype.coe_eta] }⟩ := rfl @[simp] lemma subtype_equiv_codomain_symm_apply (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) : ((subtype_equiv_codomain f).symm y : X → Y) x' = if h : x' ≠ x then f ⟨x', h⟩ else y := rfl @[simp] lemma subtype_equiv_codomain_symm_apply_eq (f : {x' // x' ≠ x} → Y) (y : Y) : ((subtype_equiv_codomain f).symm y : X → Y) x = y := dif_neg (not_not.mpr rfl) lemma subtype_equiv_codomain_symm_apply_ne (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) (h : x' ≠ x) : ((subtype_equiv_codomain f).symm y : X → Y) x' = f ⟨x', h⟩ := dif_pos h end subtype_equiv_codomain namespace set open set /-- `univ α` is equivalent to `α`. -/ @[simps apply symm_apply] protected def univ (α) : @univ α ≃ α := ⟨coe, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩ /-- An empty set is equivalent to the `empty` type. -/ protected def empty (α) : (∅ : set α) ≃ empty := equiv_empty $ λ ⟨x, h⟩, not_mem_empty x h /-- An empty set is equivalent to a `pempty` type. -/ protected def pempty (α) : (∅ : set α) ≃ pempty := equiv_pempty $ λ ⟨x, h⟩, not_mem_empty x h /-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union' {α} {s t : set α} (p : α → Prop) [decidable_pred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t := { to_fun := λ x, if hp : p x then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩, inv_fun := λ o, match o with | (sum.inl x) := ⟨x, or.inl x.2⟩ | (sum.inr x) := ⟨x, or.inr x.2⟩ end, left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := λ o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } /-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) : (s ∪ t : set α) ≃ s ⊕ t := set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩) lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg $ λ h, H ⟨h, ha⟩ @[simp] lemma union_symm_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : s) : (equiv.set.union H).symm (sum.inl a) = ⟨a, subset_union_left _ _ a.2⟩ := rfl @[simp] lemma union_symm_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : t) : (equiv.set.union H).symm (sum.inr a) = ⟨a, subset_union_right _ _ a.2⟩ := rfl -- TODO: Any reason to use the same universe? /-- A singleton set is equivalent to a `punit` type. -/ protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} := ⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩, λ ⟨x, h⟩, by { simp at h, subst x }, λ ⟨⟩, rfl⟩ /-- Equal sets are equivalent. -/ @[simps apply symm_apply] protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t := { to_fun := λ x, ⟨x, h ▸ x.2⟩, inv_fun := λ x, ⟨x, h.symm ▸ x.2⟩, left_inv := λ _, subtype.eq rfl, right_inv := λ _, subtype.eq rfl } /-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/ protected def insert {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) : (insert a s : set α) ≃ s ⊕ punit.{u+1} := calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp) ... ≃ s ⊕ ({a} : set α) : equiv.set.union (by finish [set.subset_def]) ... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) @[simp] lemma insert_symm_apply_inl {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) (b : s) : (equiv.set.insert H).symm (sum.inl b) = ⟨b, or.inr b.2⟩ := rfl @[simp] lemma insert_symm_apply_inr {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) (b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = ⟨a, or.inl rfl⟩ := rfl @[simp] lemma insert_apply_left {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) : equiv.set.insert H ⟨a, or.inl rfl⟩ = sum.inr punit.star := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma insert_apply_right {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) (b : s) : equiv.set.insert H ⟨b, or.inr b.2⟩ = sum.inl b := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl /-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/ protected def sum_compl {α} (s : set α) [decidable_pred s] : s ⊕ (sᶜ : set α) ≃ α := calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ α : equiv.set.of_eq (by simp) ... ≃ α : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred s] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred s] (x : sᶜ) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred s] {x : α} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred s] {x : α} (hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } @[simp] lemma sum_compl_symm_apply {α : Type*} {s : set α} [decidable_pred s] {x : s} : (equiv.set.sum_compl s).symm x = sum.inl x := by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx @[simp] lemma sum_compl_symm_apply_compl {α : Type*} {s : set α} [decidable_pred s] {x : sᶜ} : (equiv.set.sum_compl s).symm x = sum.inr x := by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx /-- `sum_diff_subset s t` is the natural equivalence between `s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] : s ⊕ (t \ s : set α) ≃ t := calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) : (equiv.set.union (by simp [inter_diff_self])).symm ... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] }) @[simp] lemma sum_diff_subset_apply_inl {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] (x : s) : equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl @[simp] lemma sum_diff_subset_apply_inr {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] (x : t \ s) : equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl lemma sum_diff_subset_symm_apply_of_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] {x : t} (hx : x.1 ∈ s) : (equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inl], exact subtype.eq rfl, end lemma sum_diff_subset_symm_apply_of_not_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] {x : t} (hx : x.1 ∉ s) : (equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inr], exact subtype.eq rfl, end /-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent to `s ⊕ t`. -/ protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred s] : (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t := calc (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self] ... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) : sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _) ... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _ ... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin refine (set.union' (∉ s) _ _).symm, exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1] end ... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } /-- Given an equivalence `e₀` between sets `s : set α` and `t : set β`, the set of equivalences `e : α ≃ β` such that `e ↑x = ↑(e₀ x)` for each `x : s` is equivalent to the set of equivalences between `sᶜ` and `tᶜ`. -/ protected def compl {α : Type u} {β : Type v} {s : set α} {t : set β} [decidable_pred s] [decidable_pred t] (e₀ : s ≃ t) : {e : α ≃ β // ∀ x : s, e x = e₀ x} ≃ ((sᶜ : set α) ≃ (tᶜ : set β)) := { to_fun := λ e, subtype_congr e (λ a, not_congr $ iff.symm $ maps_to.mem_iff (maps_to_iff_exists_map_subtype.2 ⟨e₀, e.2⟩) (surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2 ⟨t, e₀, subset.refl t, e₀.surjective, e.2⟩) e.1.injective)), inv_fun := λ e₁, subtype.mk (calc α ≃ s ⊕ (sᶜ : set α) : (set.sum_compl s).symm ... ≃ t ⊕ (tᶜ : set β) : e₀.sum_congr e₁ ... ≃ β : set.sum_compl t) (λ x, by simp only [sum.map_inl, trans_apply, sum_congr_apply, set.sum_compl_apply_inl, set.sum_compl_symm_apply]), left_inv := λ e, begin ext x, by_cases hx : x ∈ s, { simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩, sum.map_inl, sum_congr_apply, trans_apply, subtype.coe_mk, set.sum_compl_apply_inl] }, { simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr, subtype_congr_apply, set.sum_compl_apply_inr, trans_apply, sum_congr_apply, subtype.coe_mk] }, end, right_inv := λ e, equiv.ext $ λ x, by simp only [sum.map_inr, subtype_congr_apply, set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans, subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] } /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {α β} (s : set α) (t : set β) : s.prod t ≃ s × t := @subtype_prod_equiv_prod α β s t /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩, λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩, λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ @[simps apply {rhs_md := semireducible}] protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (H.inj_on s) lemma image_symm_preimage {α β} {f : α → β} (hf : injective f) (u s : set α) : (λ x, (set.image f s hf).symm x : f '' s → α) ⁻¹' u = coe ⁻¹' (f '' u) := begin ext ⟨b, a, has, rfl⟩, have : ∀(h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λ h, (classical.some_spec h).2, simp [equiv.set.image, equiv.set.image_of_inj_on, hf, this], end /-- If `f : α → β` is an injective function, then `α` is equivalent to the range of `f`. -/ @[simps apply] protected noncomputable def range {α β} (f : α → β) (H : injective f) : α ≃ range f := { to_fun := λ x, ⟨f x, mem_range_self _⟩, inv_fun := λ x, classical.some x.2, left_inv := λ x, H (classical.some_spec (show f x ∈ range f, from mem_range_self _)), right_inv := λ x, subtype.eq $ classical.some_spec x.2 } theorem apply_range_symm {α β} (f : α → β) (H : injective f) (b : range f) : f ((set.range f H).symm b) = b := begin conv_rhs { rw ←((set.range f H).right_inv b), }, simp, end /-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/ protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β := ⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {α : Type u} (s : set α) (t : α → Prop) : ({ x ∈ s | t x } : set α) ≃ { x : s | t x } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm /-- The set `𝒫 S := {x | x ⊆ S}` is equivalent to the type `set S`. -/ protected def powerset {α} (S : set α) : 𝒫 S ≃ set S := { to_fun := λ x : 𝒫 S, coe ⁻¹' (x : set α), inv_fun := λ x : set S, ⟨coe '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩, left_inv := λ x, by ext y; exact ⟨λ ⟨⟨_, _⟩, h, rfl⟩, h, λ h, ⟨⟨_, x.2 h⟩, h, rfl⟩⟩, right_inv := λ x, by ext; simp } end set /-- If `f` is a bijective function, then its domain is equivalent to its codomain. -/ @[simps apply {rhs_md := semireducible, simp_rhs := tt}] noncomputable def of_bijective {α β} (f : α → β) (hf : bijective f) : α ≃ β := (equiv.set.range f hf.1).trans $ (set_congr hf.2.range_eq).trans $ equiv.set.univ β /-- If `f` is an injective function, then its domain is equivalent to its range. -/ @[simps apply {rhs_md := semireducible, simp_rhs := tt}] noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ _root_.set.range f := of_bijective (λ x, ⟨f x, set.mem_range_self x⟩) ⟨λ x y hxy, hf $ by injections, λ ⟨_, x, rfl⟩, ⟨x, rfl⟩⟩ /-- Subtype of the quotient is equivalent to the quotient of the subtype. Let `α` be a setoid with equivalence relation `~`. Let `p₂` be a predicate on the quotient type `α/~`, and `p₁` be the lift of this predicate to `α`: `p₁ a ↔ p₂ ⟦a⟧`. Let `~₂` be the restriction of `~` to `{x // p₁ x}`. Then `{x // p₂ x}` is equivalent to the quotient of `{x // p₁ x}` by `~₂`. -/ def subtype_quotient_equiv_quotient_subtype (p₁ : α → Prop) [s₁ : setoid α] [s₂ : setoid (subtype p₁)] (p₂ : quotient s₁ → Prop) (hp₂ : ∀ a, p₁ a ↔ p₂ ⟦a⟧) (h : ∀ x y : subtype p₁, @setoid.r _ s₂ x y ↔ (x : α) ≈ y) : {x // p₂ x} ≃ quotient s₂ := { to_fun := λ a, quotient.hrec_on a.1 (λ a h, ⟦⟨a, (hp₂ _).2 h⟩⟧) (λ a b hab, hfunext (by rw quotient.sound hab) (λ h₁ h₂ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2, inv_fun := λ a, quotient.lift_on a (λ a, (⟨⟦a.1⟧, (hp₂ _).1 a.2⟩ : {x // p₂ x})) (λ a b hab, subtype.ext_val (quotient.sound ((h _ _).1 hab))), left_inv := λ ⟨a, ha⟩, quotient.induction_on a (λ a ha, rfl) ha, right_inv := λ a, quotient.induction_on a (λ ⟨a, ha⟩, rfl) } section swap variable [decidable_eq α] /-- A helper function for `equiv.swap`. -/ def swap_core (a b r : α) : α := if r = a then b else if r = b then a else r theorem swap_core_self (r a : α) : swap_core a a r = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_swap_core (r a b : α) : swap_core a b (swap_core a b r) = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_comm (r a b : α) : swap_core a b r = swap_core b a r := by { unfold swap_core, split_ifs; cc } /-- `swap a b` is the permutation that swaps `a` and `b` and leaves other values as is. -/ def swap (a b : α) : perm α := ⟨swap_core a b, swap_core a b, λr, swap_core_swap_core r a b, λr, swap_core_swap_core r a b⟩ theorem swap_self (a : α) : swap a a = equiv.refl _ := ext $ λ r, swap_core_self r a theorem swap_comm (a b : α) : swap a b = swap b a := ext $ λ r, swap_core_comm r _ _ theorem swap_apply_def (a b x : α) : swap a b x = if x = a then b else if x = b then a else x := rfl @[simp] theorem swap_apply_left (a b : α) : swap a b a = b := if_pos rfl @[simp] theorem swap_apply_right (a b : α) : swap a b b = a := by { by_cases h : b = a; simp [swap_apply_def, h], } theorem swap_apply_of_ne_of_ne {a b x : α} : x ≠ a → x ≠ b → swap a b x = x := by simp [swap_apply_def] {contextual := tt} @[simp] theorem swap_swap (a b : α) : (swap a b).trans (swap a b) = equiv.refl _ := ext $ λ x, swap_core_swap_core _ _ _ theorem swap_comp_apply {a b x : α} (π : perm α) : π.trans (swap a b) x = if π x = a then b else if π x = b then a else π x := by { cases π, refl } @[simp] lemma swap_inv {α : Type*} [decidable_eq α] (x y : α) : (swap x y)⁻¹ = swap x y := rfl @[simp] lemma symm_trans_swap_trans [decidable_eq β] (a b : α) (e : α ≃ β) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) := equiv.ext (λ x, begin have : ∀ a, e.symm x = a ↔ x = e a := λ a, by { rw @eq_comm _ (e.symm x), split; intros; simp * at * }, simp [swap_apply_def, this], split_ifs; simp end) @[simp] lemma swap_mul_self {α : Type*} [decidable_eq α] (i j : α) : swap i j * swap i j = 1 := equiv.swap_swap i j @[simp] lemma swap_apply_self {α : Type*} [decidable_eq α] (i j a : α) : swap i j (swap i j a) = a := by rw [← perm.mul_apply, swap_mul_self, perm.one_apply] /-- Augment an equivalence with a prescribed mapping `f a = b` -/ def set_value (f : α ≃ β) (a : α) (b : β) : α ≃ β := (swap a (f.symm b)).trans f @[simp] theorem set_value_eq (f : α ≃ β) (a : α) (b : β) : set_value f a b a = b := by { dsimp [set_value], simp [swap_apply_left] } end swap protected lemma exists_unique_congr {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p x ↔ q (f x)) : (∃! x, p x) ↔ ∃! y, q y := begin split, { rintro ⟨a, ha₁, ha₂⟩, exact ⟨f a, h.1 ha₁, λ b hb, f.symm_apply_eq.1 (ha₂ (f.symm b) (h.2 (by simpa using hb)))⟩ }, { rintro ⟨b, hb₁, hb₂⟩, exact ⟨f.symm b, h.2 (by simpa using hb₁), λ y hy, (eq_symm_apply f).2 (hb₂ _ (h.1 hy))⟩ } end protected lemma exists_unique_congr_left' {p : α → Prop} (f : α ≃ β) : (∃! x, p x) ↔ (∃! y, p (f.symm y)) := equiv.exists_unique_congr f (λx, by simp) protected lemma exists_unique_congr_left {p : β → Prop} (f : α ≃ β) : (∃! x, p (f x)) ↔ (∃! y, p y) := (equiv.exists_unique_congr_left' f.symm).symm protected lemma forall_congr {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p x ↔ q (f x)) : (∀x, p x) ↔ (∀y, q y) := begin split; intros h₂ x, { rw [←f.right_inv x], apply h.mp, apply h₂ }, apply h.mpr, apply h₂ end protected lemma forall_congr' {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p (f.symm x) ↔ q x) : (∀x, p x) ↔ (∀y, q y) := (equiv.forall_congr f.symm (λ x, h.symm)).symm -- We next build some higher arity versions of `equiv.forall_congr`. -- Although they appear to just be repeated applications of `equiv.forall_congr`, -- unification of metavariables works better with these versions. -- In particular, they are necessary in `equiv_rw`. -- (Stopping at ternary functions seems reasonable: at least in 1-categorical mathematics, -- it's rare to have axioms involving more than 3 elements at once.) universes ua1 ua2 ub1 ub2 ug1 ug2 variables {α₁ : Sort ua1} {α₂ : Sort ua2} {β₁ : Sort ub1} {β₂ : Sort ub2} {γ₁ : Sort ug1} {γ₂ : Sort ug2} protected lemma forall₂_congr {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p x y ↔ q (eα x) (eβ y)) : (∀x y, p x y) ↔ (∀x y, q x y) := begin apply equiv.forall_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₂_congr' {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p (eα.symm x) (eβ.symm y) ↔ q x y) : (∀x y, p x y) ↔ (∀x y, q x y) := (equiv.forall₂_congr eα.symm eβ.symm (λ x y, h.symm)).symm protected lemma forall₃_congr {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p x y z ↔ q (eα x) (eβ y) (eγ z)) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := begin apply equiv.forall₂_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₃_congr' {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p (eα.symm x) (eβ.symm y) (eγ.symm z) ↔ q x y z) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := (equiv.forall₃_congr eα.symm eβ.symm eγ.symm (λ x y z, h.symm)).symm protected lemma forall_congr_left' {p : α → Prop} (f : α ≃ β) : (∀x, p x) ↔ (∀y, p (f.symm y)) := equiv.forall_congr f (λx, by simp) protected lemma forall_congr_left {p : β → Prop} (f : α ≃ β) : (∀x, p (f x)) ↔ (∀y, p y) := (equiv.forall_congr_left' f.symm).symm section variables (P : α → Sort w) (e : α ≃ β) /-- Transport dependent functions through an equivalence of the base space. -/ @[simps] def Pi_congr_left' : (Π a, P a) ≃ (Π b, P (e.symm b)) := { to_fun := λ f x, f (e.symm x), inv_fun := λ f x, begin rw [← e.symm_apply_apply x], exact f (e x) end, left_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { dsimp, rw e.symm_apply_apply })), right_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { rw e.apply_symm_apply })) } end section variables (P : β → Sort w) (e : α ≃ β) /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". -/ def Pi_congr_left : (Π a, P (e a)) ≃ (Π b, P b) := (Pi_congr_left' P e.symm).symm end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π a : α, (W a ≃ Z (h₁ a))) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibers. -/ def Pi_congr : (Π a, W a) ≃ (Π b, Z b) := (equiv.Pi_congr_right h₂).trans (equiv.Pi_congr_left _ h₁) end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π b : β, (W (h₁.symm b) ≃ Z b)) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibres. -/ def Pi_congr' : (Π a, W a) ≃ (Π b, Z b) := (Pi_congr h₁.symm (λ b, (h₂ b).symm)).symm end end equiv instance {α} [subsingleton α] : subsingleton (ulift α) := equiv.ulift.subsingleton instance {α} [subsingleton α] : subsingleton (plift α) := equiv.plift.subsingleton instance {α} [decidable_eq α] : decidable_eq (ulift α) := equiv.ulift.decidable_eq instance {α} [decidable_eq α] : decidable_eq (plift α) := equiv.plift.decidable_eq /-- If both `α` and `β` are singletons, then `α ≃ β`. -/ def equiv_of_unique_of_unique [unique α] [unique β] : α ≃ β := { to_fun := λ _, default β, inv_fun := λ _, default α, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- If `α` is a singleton, then it is equivalent to any `punit`. -/ def equiv_punit_of_unique [unique α] : α ≃ punit.{v} := equiv_of_unique_of_unique /-- If `α` is a subsingleton, then it is equivalent to `α × α`. -/ def subsingleton_prod_self_equiv {α : Type*} [subsingleton α] : α × α ≃ α := { to_fun := λ p, p.1, inv_fun := λ a, (a, a), left_inv := λ p, subsingleton.elim _ _, right_inv := λ p, subsingleton.elim _ _, } /-- To give an equivalence between two subsingleton types, it is sufficient to give any two functions between them. -/ def equiv_of_subsingleton_of_subsingleton [subsingleton α] [subsingleton β] (f : α → β) (g : β → α) : α ≃ β := { to_fun := f, inv_fun := g, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- `unique (unique α)` is equivalent to `unique α`. -/ def unique_unique_equiv : unique (unique α) ≃ unique α := equiv_of_subsingleton_of_subsingleton (λ h, h.default) (λ h, { default := h, uniq := λ _, subsingleton.elim _ _ }) namespace quot /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : α → α → Prop} {rb : β → β → Prop} (e : α ≃ β) (eq : ∀a₁ a₂, ra a₁ a₂ ↔ rb (e a₁) (e a₂)) : quot ra ≃ quot rb := { to_fun := quot.map e (assume a₁ a₂, (eq a₁ a₂).1), inv_fun := quot.map e.symm (assume b₁ b₂ h, (eq (e.symm b₁) (e.symm b₂)).2 ((e.apply_symm_apply b₁).symm ▸ (e.apply_symm_apply b₂).symm ▸ h)), left_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.symm_apply_apply] }, right_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.apply_symm_apply] } } /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : α → α → Prop} (eq : ∀a₁ a₂, r a₁ a₂ ↔ r' a₁ a₂) : quot r ≃ quot r' := quot.congr (equiv.refl α) eq /-- An equivalence `e : α ≃ β` generates an equivalence between the quotient space of `α` by a relation `ra` and the quotient space of `β` by the image of this relation under `e`. -/ protected def congr_left {r : α → α → Prop} (e : α ≃ β) : quot r ≃ quot (λ b b', r (e.symm b) (e.symm b')) := @quot.congr α β r (λ b b', r (e.symm b) (e.symm b')) e (λ a₁ a₂, by simp only [e.symm_apply_apply]) end quot namespace quotient /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : setoid α} {rb : setoid β} (e : α ≃ β) (eq : ∀a₁ a₂, @setoid.r α ra a₁ a₂ ↔ @setoid.r β rb (e a₁) (e a₂)) : quotient ra ≃ quotient rb := quot.congr e eq /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : setoid α} (eq : ∀a₁ a₂, @setoid.r α r a₁ a₂ ↔ @setoid.r α r' a₁ a₂) : quotient r ≃ quotient r' := quot.congr_right eq end quotient /-- If a function is a bijection between `univ` and a set `s` in the target type, it induces an equivalence between the original type and the type `↑s`. -/ noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set β} (f : α → β) (h : set.bij_on f set.univ s) : α ≃ s := begin have : function.bijective (λ (x : α), (⟨f x, begin exact h.maps_to (set.mem_univ x) end⟩ : s)), { split, { assume x y hxy, apply h.inj_on (set.mem_univ x) (set.mem_univ y) (subtype.mk.inj hxy) }, { assume x, rcases h.surj_on x.2 with ⟨y, hy⟩, exact ⟨y, subtype.eq hy.2⟩ } }, exact equiv.of_bijective _ this end /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {α : Type*} {β : Type*} {γ : Type*} {s : set α} (e : β ≃ s) (v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α] [∀ j, decidable (j ∈ s)] : (λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { simp only [h, dif_pos], have A : e.symm ⟨i, h⟩ = j ↔ i = e j, by { rw equiv.symm_apply_eq, exact subtype.ext_iff_val }, by_cases h' : i = e j, { rw [A.2 h', h'], simp }, { have : ¬ e.symm ⟨i, h⟩ = j, by simpa [← A] using h', simp [h, h', this] } }, { have : i ≠ e j, by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
9352c586ec60254f7ea86cf3f2dbf973096a4ee9
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/algebra/order/monotone_continuity.lean
957caae659ca6efbf58ed3f63affc4472dee5d5d
[ "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
17,258
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, Heather Macbeth -/ import topology.algebra.order.basic import topology.algebra.order.left_right /-! # Continuity of monotone functions In this file we prove the following fact: if `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood is a neighborhood of `f a`, then `f` is continuous at `a`, see `continuous_at_of_monotone_on_of_image_mem_nhds`, as well as several similar facts. We also prove that an `order_iso` is continuous. ## Tags continuous, monotone -/ open set filter open_locale topological_space section linear_order variables {α β : Type*} [linear_order α] [topological_space α] [order_topology α] variables [linear_order β] [topological_space β] [order_topology β] /-- If `f` is a function strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x ≤ 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_on.continuous_at_right_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within] with _ hxs hxa using hb.trans_le ((h_mono.le_iff_le has hxs).2 hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, rw [h_mono.lt_iff_lt has hcs] at hac, filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 hac)], rintros x hx ⟨hax, hxc⟩, exact ((h_mono.lt_iff_lt hx hcs).2 hxc).trans_le hcb } end /-- If `f` is a monotone function on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b` cannot be replaced by the weaker assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` we use for strictly monotone functions because otherwise the function `ceil : ℝ → ℤ` would be a counter-example at `a = 0`. -/ lemma continuous_at_right_of_monotone_on_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within] with _ hxs hxa using hb.trans_le (h_mono has hxs hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, have : a < c, from not_le.1 (λ h, hac.not_le $ h_mono hcs has h), filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 this)], rintros x hx ⟨hax, hxc⟩, exact (h_mono hx hcs hxc.le).trans_lt hcb } end /-- If a function `f` with a densely ordered codomain is monotone on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : closure (f '' s) ∈ 𝓝[≥] (f a)) : continuous_within_at f (Ici a) a := begin refine continuous_at_right_of_monotone_on_of_exists_between h_mono hs (λ b hb, _), rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hb).1 hfs with ⟨b', ⟨hab', hbb'⟩, hb'⟩, rcases exists_between hab' with ⟨c', hc'⟩, rcases mem_closure_iff.1 (hb' ⟨hc'.1.le, hc'.2⟩) (Ioo (f a) b') is_open_Ioo hc' with ⟨_, hc, ⟨c, hcs, rfl⟩⟩, exact ⟨c, hcs, hc.1, hc.2.trans_le hbb'⟩ end /-- If a function `f` with a densely ordered codomain is monotone on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_monotone_on_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : f '' s ∈ 𝓝[≥] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within h_mono hs $ mem_of_superset hfs subset_closure /-- If a function `f` with a densely ordered codomain is strictly monotone on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_on.continuous_at_right_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : closure (f '' s) ∈ 𝓝[≥] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within (λ x hx y hy, (h_mono.le_iff_le hx hy).2) hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_on.continuous_at_right_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : f '' s ∈ 𝓝[≥] (f a)) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_closure_image_mem_nhds_within hs (mem_of_superset hfs subset_closure) /-- If a function `f` is strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` includes `Ioi (f a)`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_on.continuous_at_right_of_surj_on {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : surj_on f s (Ioi (f a))) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_exists_between hs $ λ b hb, let ⟨c, hcs, hcb⟩ := hfs hb in ⟨c, hcs, hcb.symm ▸ hb, hcb.le⟩ /-- If `f` is a strictly monotone function on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x < 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_on.continuous_at_left_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_exists_between hs $ λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If `f` is a monotone function on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)` cannot be replaced by the weaker assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` we use for strictly monotone functions because otherwise the function `floor : ℝ → ℤ` would be a counter-example at `a = 0`. -/ lemma continuous_at_left_of_monotone_on_of_exists_between {f : α → β} {s : set α} {a : α} (hf : monotone_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_monotone_on_of_exists_between αᵒᵈ βᵒᵈ _ _ _ _ _ _ f s a hf.dual hs $ λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If a function `f` with a densely ordered codomain is monotone on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left -/ lemma continuous_at_left_of_monotone_on_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (hf : monotone_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : closure (f '' s) ∈ 𝓝[≤] (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within αᵒᵈ βᵒᵈ _ _ _ _ _ _ _ f s a hf.dual hs hfs /-- If a function `f` with a densely ordered codomain is monotone on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma continuous_at_left_of_monotone_on_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : f '' s ∈ 𝓝[≤] (f a)) : continuous_within_at f (Iic a) a := continuous_at_left_of_monotone_on_of_closure_image_mem_nhds_within h_mono hs (mem_of_superset hfs subset_closure) /-- If a function `f` with a densely ordered codomain is strictly monotone on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_on.continuous_at_left_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : closure (f '' s) ∈ 𝓝[≤] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_closure_image_mem_nhds_within hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotone on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_on.continuous_at_left_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : f '' s ∈ 𝓝[≤] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_image_mem_nhds_within hs hfs /-- If a function `f` is strictly monotone on a left neighborhood of `a` and the image of this neighborhood under `f` includes `Iio (f a)`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_on.continuous_at_left_of_surj_on {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : surj_on f s (Iio (f a))) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_surj_on hs hfs /-- If a function `f` is strictly monotone on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, and every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a`. -/ lemma strict_mono_on.continuous_at_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝 a) (hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_l, h_mono.continuous_at_right_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is strictly monotone on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_on.continuous_at_of_closure_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), h_mono.continuous_at_right_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is strictly monotone on a neighborhood of `a` and the image of this set under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_on.continuous_at_of_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := h_mono.continuous_at_of_closure_image_mem_nhds hs (mem_of_superset hfs subset_closure) /-- If `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, and every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_monotone_on_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝 a) (hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_monotone_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_l, continuous_at_right_of_monotone_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is monotone on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_monotone_on_of_closure_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_monotone_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is monotone on a neighborhood of `a` and the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_monotone_on_of_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_of_monotone_on_of_closure_image_mem_nhds h_mono hs (mem_of_superset hfs subset_closure) /-- A monotone function with densely ordered codomain and a dense range is continuous. -/ lemma monotone.continuous_of_dense_range [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_dense : dense_range f) : continuous f := continuous_iff_continuous_at.mpr $ λ a, continuous_at_of_monotone_on_of_closure_image_mem_nhds (λ x hx y hy hxy, h_mono hxy) univ_mem $ by simp only [image_univ, h_dense.closure_eq, univ_mem] /-- A monotone surjective function with a densely ordered codomain is continuous. -/ lemma monotone.continuous_of_surjective [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_surj : function.surjective f) : continuous f := h_mono.continuous_of_dense_range h_surj.dense_range end linear_order /-! ### Continuity of order isomorphisms In this section we prove that an `order_iso` is continuous, hence it is a `homeomorph`. We prove this for an `order_iso` between to partial orders with order topology. -/ namespace order_iso variables {α β : Type*} [partial_order α] [partial_order β] [topological_space α] [topological_space β] [order_topology α] [order_topology β] protected lemma continuous (e : α ≃o β) : continuous e := begin rw [‹order_topology β›.topology_eq_generate_intervals], refine continuous_generated_from (λ s hs, _), rcases hs with ⟨a, rfl|rfl⟩, { rw e.preimage_Ioi, apply is_open_lt' }, { rw e.preimage_Iio, apply is_open_gt' } end /-- An order isomorphism between two linear order `order_topology` spaces is a homeomorphism. -/ def to_homeomorph (e : α ≃o β) : α ≃ₜ β := { continuous_to_fun := e.continuous, continuous_inv_fun := e.symm.continuous, .. e } @[simp] lemma coe_to_homeomorph (e : α ≃o β) : ⇑e.to_homeomorph = e := rfl @[simp] lemma coe_to_homeomorph_symm (e : α ≃o β) : ⇑e.to_homeomorph.symm = e.symm := rfl end order_iso
c1c1efeba6acedd1f84a5ea9ec33054fa62959b4
1a61aba1b67cddccce19532a9596efe44be4285f
/library/theories/number_theory/irrational_roots.lean
919e593e171a5abef7eccbc2b8870e251de35d80
[ "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,149
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad A proof that if n > 1 and a > 0, then the nth root of a is irrational, unless a is a perfect nth power. -/ import data.rat .prime_factorization open eq.ops /- First, a textbook proof that sqrt 2 is irrational. -/ section open nat theorem sqrt_two_irrational {a b : ℕ} (co : coprime a b) : a^2 ≠ 2 * b^2 := assume H : a^2 = 2 * b^2, have even (a^2), from even_of_exists (exists.intro _ H), have even a, from even_of_even_pow this, obtain c (aeq : a = 2 * c), from exists_of_even this, have 2 * (2 * c^2) = 2 * b^2, by rewrite [-H, aeq, *pow_two, mul.assoc, mul.left_comm c], have 2 * c^2 = b^2, from eq_of_mul_eq_mul_left dec_trivial this, have even (b^2), from even_of_exists (exists.intro _ (eq.symm this)), have even b, from even_of_even_pow this, have 2 ∣ gcd a b, from dvd_gcd (dvd_of_even `even a`) (dvd_of_even `even b`), have 2 ∣ 1, from co ▸ this, absurd `2 ∣ 1` dec_trivial end /- Replacing 2 by an arbitrary prime and the power 2 by any n ≥ 1 yields the stronger result that the nth root of an integer is irrational, unless the integer is already a perfect nth power. -/ section open nat decidable theorem root_irrational {a b c n : ℕ} (npos : n > 0) (apos : a > 0) (co : coprime a b) (H : a^n = c * b^n) : b = 1 := have bpos : b > 0, from pos_of_ne_zero (suppose b = 0, have a^n = 0, by rewrite [H, this, zero_pow npos], assert a = 0, from eq_zero_of_pow_eq_zero this, show false, from ne_of_lt `0 < a` this⁻¹), have H₁ : ∀ p, prime p → ¬ p ∣ b, from take p, suppose prime p, suppose p ∣ b, assert p ∣ b^n, from dvd_pow_of_dvd_of_pos `p ∣ b` `n > 0`, have p ∣ a^n, by rewrite H; apply dvd_mul_of_dvd_right this, have p ∣ a, from dvd_of_prime_of_dvd_pow `prime p` this, have ¬ coprime a b, from not_coprime_of_dvd_of_dvd (gt_one_of_prime `prime p`) `p ∣ a` `p ∣ b`, show false, from this `coprime a b`, have b < 2, from by_contradiction (suppose ¬ b < 2, have b ≥ 2, from le_of_not_gt this, obtain p [primep pdvdb], from exists_prime_and_dvd this, show false, from H₁ p primep pdvdb), show b = 1, from (le.antisymm (le_of_lt_succ `b < 2`) (succ_le_of_lt `b > 0`)) end /- Here we state this in terms of the rationals, ℚ. The main difficulty is casting between ℕ, ℤ, and ℚ. -/ section open rat int nat decidable theorem denom_eq_one_of_pow_eq {q : ℚ} {n : ℕ} {c : ℤ} (npos : n > 0) (H : q^n = c) : denom q = 1 := let a := num q, b := denom q in have b ≠ 0, from ne_of_gt (denom_pos q), have bnz : b ≠ (0 : ℚ), from assume H, `b ≠ 0` (of_int.inj H), have bnnz : (#rat b^n ≠ 0), from assume bneqz, bnz (eq_zero_of_pow_eq_zero bneqz), have a^n / b^n = c, using bnz, by rewrite [*of_int_pow, -div_pow, -eq_num_div_denom, -H], have a^n = c * b^n, from eq.symm (!mul_eq_of_eq_div bnnz this⁻¹), have a^n = c * b^n, -- int version using this, by rewrite [-of_int_pow at this, -of_int_mul at this]; exact of_int.inj this, have (abs a)^n = abs c * (abs b)^n, using this, by rewrite [-int.abs_pow, this, int.abs_mul, int.abs_pow], have H₁ : (nat_abs a)^n = nat_abs c * (nat_abs b)^n, using this, by apply of_nat.inj; rewrite [int.of_nat_mul, +of_nat_pow, +of_nat_nat_abs]; assumption, have H₂ : nat.coprime (nat_abs a) (nat_abs b), from of_nat.inj !coprime_num_denom, have nat_abs b = 1, from by_cases (suppose q = 0, by rewrite this) (suppose q ≠ 0, have a ≠ 0, from suppose a = 0, `q ≠ 0` (by rewrite [eq_num_div_denom, `a = 0`, zero_div]), have nat_abs a ≠ 0, from suppose nat_abs a = 0, `a ≠ 0` (eq_zero_of_nat_abs_eq_zero this), show nat_abs b = 1, from (root_irrational npos (pos_of_ne_zero this) H₂ H₁)), show b = 1, using this, by rewrite [-of_nat_nat_abs_of_nonneg (le_of_lt !denom_pos), this] theorem eq_num_pow_of_pow_eq {q : ℚ} {n : ℕ} {c : ℤ} (npos : n > 0) (H : q^n = c) : c = (num q)^n := have denom q = 1, from denom_eq_one_of_pow_eq npos H, have of_int c = (num q)^n, using this, by rewrite [-H, eq_num_div_denom q at {1}, this, div_one, of_int_pow], show c = (num q)^n , from of_int.inj this end /- As a corollary, for n > 1, the nth root of a prime is irrational. -/ section open nat theorem not_eq_pow_of_prime {p n : ℕ} (a : ℕ) (ngt1 : n > 1) (primep : prime p) : p ≠ a^n := assume peq : p = a^n, have npos : n > 0, from lt.trans dec_trivial ngt1, have pnez : p ≠ 0, from (suppose p = 0, show false, by let H := (pos_of_prime primep); rewrite this at H; exfalso; exact !lt.irrefl H), have agtz : a > 0, from pos_of_ne_zero (suppose a = 0, show false, using npos pnez, by revert peq; rewrite [this, zero_pow npos]; exact pnez), have n * mult p a = 1, from calc n * mult p a = mult p (a^n) : using agtz, by rewrite [mult_pow n agtz primep] ... = mult p p : peq ... = 1 : mult_self (gt_one_of_prime primep), have n ∣ 1, from dvd_of_mul_right_eq this, have n = 1, from eq_one_of_dvd_one this, show false, using this, by rewrite this at ngt1; exact !lt.irrefl ngt1 open int rat theorem root_prime_irrational {p n : ℕ} {q : ℚ} (qnonneg : q ≥ 0) (ngt1 : n > 1) (primep : prime p) : q^n ≠ p := have numq : num q ≥ 0, from num_nonneg_of_nonneg qnonneg, have npos : n > 0, from lt.trans dec_trivial ngt1, suppose q^n = p, have p = (num q)^n, from eq_num_pow_of_pow_eq npos this, have p = (nat_abs (num q))^n, using this numq, by apply of_nat.inj; rewrite [this, of_nat_pow, of_nat_nat_abs_of_nonneg numq], show false, from not_eq_pow_of_prime _ ngt1 primep this end /- Thaetetus, who lives in the fourth century BC, is said to have proved the irrationality of square roots up to seventeen. In Chapter 4 of /Why Prove it Again/, John Dawson notes that Thaetetus may have used an approach similar to the one below. (See data/nat/gcd.lean for the key theorem, "div_gcd_eq_div_gcd".) -/ section open int example {a b c : ℤ} (co : coprime a b) (apos : a > 0) (bpos : b > 0) (H : a * a = c * (b * b)) : b = 1 := assert H₁ : gcd (c * b) a = gcd c a, from gcd_mul_right_cancel_of_coprime _ (coprime_swap co), have a * a = c * b * b, by rewrite -mul.assoc at H; apply H, have a div (gcd a b) = c * b div gcd (c * b) a, from div_gcd_eq_div_gcd this bpos apos, have a = c * b div gcd c a, using this, by revert this; rewrite [↑coprime at co, co, div_one, H₁]; intros; assumption, have a = b * (c div gcd c a), using this, by revert this; rewrite [mul.comm, !mul_div_assoc !gcd_dvd_left]; intros; assumption, have b ∣ a, from dvd_of_mul_right_eq this⁻¹, have b ∣ gcd a b, from dvd_gcd this !dvd.refl, have b ∣ 1, using this, by rewrite [↑coprime at co, co at this]; apply this, show b = 1, from eq_one_of_dvd_one (le_of_lt bpos) this end
81c14af860ed7f95e5bd2c31b74c1c1e4de71ece
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/group/to_additive.lean
090fc59d2b84b842418e485cdfb90bd715dd500d
[ "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
25,272
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Yury Kudryashov, Floris van Doorn -/ import tactic.transform_decl import tactic.algebra /-! # Transport multiplicative to additive This file defines an attribute `to_additive` that can be used to automatically transport theorems and definitions (but not inductive types and structures) from a multiplicative theory to an additive theory. Usage information is contained in the doc string of `to_additive.attr`. ### Missing features * Automatically transport structures and other inductive types. * For structures, automatically generate theorems like `group α ↔ add_group (additive α)`. -/ namespace to_additive open tactic setup_tactic_parser section performance_hack -- see Note [user attribute parameters] local attribute [semireducible] reflected /-- Temporarily change the `has_reflect` instance for `name`. -/ local attribute [instance, priority 9000] meta def hacky_name_reflect : has_reflect name := λ n, `(id %%(expr.const n []) : name) /-- An auxiliary attribute used to store the names of the additive versions of declarations that have been processed by `to_additive`. -/ @[user_attribute] meta def aux_attr : user_attribute (name_map name) name := { name := `to_additive_aux, descr := "Auxiliary attribute for `to_additive`. DON'T USE IT", parser := failed, cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n', do let n := match n' with | name.mk_string s pre := if s = "_to_additive" then pre else n' | _ := n' end, param ← aux_attr.get_param_untyped n', pure $ dict.insert n param.app_arg.const_name) mk_name_map, []⟩ } end performance_hack section extra_attributes /-- An attribute that tells `@[to_additive]` that certain arguments of this definition are not involved when using `@[to_additive]`. This helps the heuristic of `@[to_additive]` by also transforming definitions if `ℕ` or another fixed type occurs as one of these arguments. -/ @[user_attribute] meta def ignore_args_attr : user_attribute (name_map $ list ℕ) (list ℕ) := { name := `to_additive_ignore_args, descr := "Auxiliary attribute for `to_additive` stating that certain arguments are not additivized.", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do param ← ignore_args_attr.get_param_untyped n, -- see Note [user attribute parameters] return $ dict.insert n (param.to_list expr.to_nat).iget) mk_name_map, []⟩, parser := (lean.parser.small_nat)* } /-- An attribute that is automatically added to declarations tagged with `@[to_additive]`, if needed. This attribute tells which argument is the type where this declaration uses the multiplicative structure. If there are multiple argument, we typically tag the first one. If this argument contains a fixed type, this declaration will note be additivized. See the Heuristics section of `to_additive.attr` for more details. If a declaration is not tagged, it is presumed that the first argument is relevant. `@[to_additive]` uses the function `to_additive.first_multiplicative_arg` to automatically tag declarations. It is ok to update it manually if the automatic tagging made an error. Implementation note: we only allow exactly 1 relevant argument, even though some declarations (like `prod.group`) have multiple arguments with a multiplicative structure on it. The reason is that whether we additivize a declaration is an all-or-nothing decision, and if we will not be able to additivize declarations that (e.g.) talk about multiplication on `ℕ × α` anyway. Warning: adding `@[to_additive_reorder]` with an equal or smaller number than the number in this attribute is currently not supported. -/ @[user_attribute] meta def relevant_arg_attr : user_attribute (name_map ℕ) ℕ := { name := `to_additive_relevant_arg, descr := "Auxiliary attribute for `to_additive` stating which arguments are the types with a " ++ "multiplicative structure.", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do param ← relevant_arg_attr.get_param_untyped n, -- see Note [user attribute parameters] -- we subtract 1 from the values provided by the user. return $ dict.insert n $ param.to_nat.iget.pred) mk_name_map, []⟩, parser := lean.parser.small_nat } /-- An attribute that stores all the declarations that needs their arguments reordered when applying `@[to_additive]`. Currently, we only support swapping consecutive arguments. The list of the natural numbers contains the positions of the first of the two arguments to be swapped. If the first two arguments are swapped, the first two universe variables are also swapped. Example: `@[to_additive_reorder 1 4]` swaps the first two arguments and the arguments in positions 4 and 5. -/ @[user_attribute] meta def reorder_attr : user_attribute (name_map $ list ℕ) (list ℕ) := { name := `to_additive_reorder, descr := "Auxiliary attribute for `to_additive` that stores arguments that need to be reordered.", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do param ← reorder_attr.get_param_untyped n, -- see Note [user attribute parameters] return $ dict.insert n (param.to_list expr.to_nat).iget) mk_name_map, []⟩, parser := do l ← (lean.parser.small_nat)*, guard (l.all (≠ 0)) <|> exceptional.fail "The reorder positions must be positive", return l } end extra_attributes /-- Find the first argument of `nm` that has a multiplicative type-class on it. Returns 1 if there are no types with a multiplicative class as arguments. E.g. `prod.group` returns 1, and `pi.has_one` returns 2. -/ meta def first_multiplicative_arg (nm : name) : tactic ℕ := do d ← get_decl nm, let (es, _) := d.type.pi_binders, l ← es.mmap_with_index $ λ n bi, do { let tgt := bi.type.pi_codomain, let n_bi := bi.type.pi_binders.fst.length, tt ← has_attribute' `to_additive tgt.get_app_fn.const_name | return none, let n2 := tgt.get_app_args.head.get_app_fn.match_var.map $ λ m, n + n_bi - m, return $ n2 }, let l := l.reduce_option, return $ if l = [] then 1 else l.foldr min l.head /-- A command that can be used to have future uses of `to_additive` change the `src` namespace to the `tgt` namespace. For example: ``` run_cmd to_additive.map_namespace `quotient_group `quotient_add_group ``` Later uses of `to_additive` on declarations in the `quotient_group` namespace will be created in the `quotient_add_group` namespaces. -/ meta def map_namespace (src tgt : name) : command := do let n := src.mk_string "_to_additive", let decl := declaration.thm n [] `(unit) (pure (reflect ())), add_decl decl, aux_attr.set n tgt tt /-- `value_type` is the type of the arguments that can be provided to `to_additive`. `to_additive.parser` parses the provided arguments: * `replace_all`: replace all multiplicative declarations, do not use the heuristic. * `trace`: output the generated additive declaration. * `tgt : name`: the name of the target (the additive declaration). * `doc`: an optional doc string. * if `allow_auto_name` is `ff` (default) then `@[to_additive]` will check whether the given name can be auto-generated. -/ @[derive has_reflect, derive inhabited] structure value_type : Type := (replace_all : bool) (trace : bool) (tgt : name) (doc : option string) (allow_auto_name : bool) /-- `add_comm_prefix x s` returns `"comm_" ++ s` if `x = tt` and `s` otherwise. -/ meta def add_comm_prefix : bool → string → string | tt s := "comm_" ++ s | ff s := s /-- Dictionary used by `to_additive.guess_name` to autogenerate names. -/ meta def tr : bool → list string → list string | is_comm ("one" :: "le" :: s) := add_comm_prefix is_comm "nonneg" :: tr ff s | is_comm ("one" :: "lt" :: s) := add_comm_prefix is_comm "pos" :: tr ff s | is_comm ("le" :: "one" :: s) := add_comm_prefix is_comm "nonpos" :: tr ff s | is_comm ("lt" :: "one" :: s) := add_comm_prefix is_comm "neg" :: tr ff s | is_comm ("mul" :: "support" :: s) := add_comm_prefix is_comm "support" :: tr ff s | is_comm ("mul" :: "indicator" :: s) := add_comm_prefix is_comm "indicator" :: tr ff s | is_comm ("mul" :: s) := add_comm_prefix is_comm "add" :: tr ff s | is_comm ("smul" :: s) := add_comm_prefix is_comm "vadd" :: tr ff s | is_comm ("inv" :: s) := add_comm_prefix is_comm "neg" :: tr ff s | is_comm ("div" :: s) := add_comm_prefix is_comm "sub" :: tr ff s | is_comm ("one" :: s) := add_comm_prefix is_comm "zero" :: tr ff s | is_comm ("prod" :: s) := add_comm_prefix is_comm "sum" :: tr ff s | is_comm ("finprod" :: s) := add_comm_prefix is_comm "finsum" :: tr ff s | is_comm ("npow" :: s) := add_comm_prefix is_comm "nsmul" :: tr ff s | is_comm ("zpow" :: s) := add_comm_prefix is_comm "zsmul" :: tr ff s | is_comm ("monoid" :: s) := ("add_" ++ add_comm_prefix is_comm "monoid") :: tr ff s | is_comm ("submonoid" :: s) := ("add_" ++ add_comm_prefix is_comm "submonoid") :: tr ff s | is_comm ("group" :: s) := ("add_" ++ add_comm_prefix is_comm "group") :: tr ff s | is_comm ("subgroup" :: s) := ("add_" ++ add_comm_prefix is_comm "subgroup") :: tr ff s | is_comm ("semigroup" :: s) := ("add_" ++ add_comm_prefix is_comm "semigroup") :: tr ff s | is_comm ("magma" :: s) := ("add_" ++ add_comm_prefix is_comm "magma") :: tr ff s | is_comm ("haar" :: s) := ("add_" ++ add_comm_prefix is_comm "haar") :: tr ff s | is_comm ("prehaar" :: s) := ("add_" ++ add_comm_prefix is_comm "prehaar") :: tr ff s | is_comm ("comm" :: s) := tr tt s | is_comm (x :: s) := (add_comm_prefix is_comm x :: tr ff s) | tt [] := ["comm"] | ff [] := [] /-- Autogenerate target name for `to_additive`. -/ meta def guess_name : string → string := string.map_tokens ''' $ λ s, string.intercalate (string.singleton '_') $ tr ff (s.split_on '_') /-- Return the provided target name or autogenerate one if one was not provided. -/ meta def target_name (src tgt : name) (dict : name_map name) (allow_auto_name : bool) : tactic name := (if tgt.get_prefix ≠ name.anonymous ∨ allow_auto_name -- `tgt` is a full name then pure tgt else match src with | (name.mk_string s pre) := do let tgt_auto := guess_name s, guard (tgt.to_string ≠ tgt_auto ∨ tgt = src) <|> trace ("`to_additive " ++ src.to_string ++ "`: correctly autogenerated target " ++ "name, you may remove the explicit " ++ tgt_auto ++ " argument."), pure $ name.mk_string (if tgt = name.anonymous then tgt_auto else tgt.to_string) (pre.map_prefix dict.find) | _ := fail ("to_additive: can't transport " ++ src.to_string) end) >>= (λ res, if res = src ∧ tgt ≠ src then fail ("to_additive: can't transport " ++ src.to_string ++ " to itself. Give the desired additive name explicitly using `@[to_additive additive_name]`. ") else pure res) /-- the parser for the arguments to `to_additive`. -/ meta def parser : lean.parser value_type := do bang ← option.is_some <$> (tk "!")?, ques ← option.is_some <$> (tk "?")?, tgt ← ident?, e ← texpr?, doc ← match e with | some pe := some <$> ((to_expr pe >>= eval_expr string) : tactic string) | none := pure none end, return ⟨bang, ques, tgt.get_or_else name.anonymous, doc, ff⟩ private meta def proceed_fields_aux (src tgt : name) (prio : ℕ) (f : name → tactic (list string)) : command := do src_fields ← f src, tgt_fields ← f tgt, guard (src_fields.length = tgt_fields.length) <|> fail ("Failed to map fields of " ++ src.to_string), (src_fields.zip tgt_fields).mmap' $ λ names, guard (names.fst = names.snd) <|> aux_attr.set (src.append names.fst) (tgt.append names.snd) tt prio /-- Add the `aux_attr` attribute to the structure fields of `src` so that future uses of `to_additive` will map them to the corresponding `tgt` fields. -/ meta def proceed_fields (env : environment) (src tgt : name) (prio : ℕ) : command := let aux := proceed_fields_aux src tgt prio in do aux (λ n, pure $ list.map name.to_string $ (env.structure_fields n).get_or_else []) >> aux (λ n, (list.map (λ (x : name), "to_" ++ x.to_string) <$> get_tagged_ancestors n)) >> aux (λ n, (env.constructors_of n).mmap $ λ cs, match cs with | (name.mk_string s pre) := (guard (pre = n) <|> fail "Bad constructor name") >> pure s | _ := fail "Bad constructor name" end) /-- The attribute `to_additive` can be used to automatically transport theorems and definitions (but not inductive types and structures) from a multiplicative theory to an additive theory. To use this attribute, just write: ``` @[to_additive] theorem mul_comm' {α} [comm_semigroup α] (x y : α) : x * y = y * x := comm_semigroup.mul_comm ``` This code will generate a theorem named `add_comm'`. It is also possible to manually specify the name of the new declaration, and provide a documentation string: ``` @[to_additive add_foo "add_foo doc string"] /-- foo doc string -/ theorem foo := sorry ``` The transport tries to do the right thing in most cases using several heuristics described below. However, in some cases it fails, and requires manual intervention. If the declaration to be transported has attributes which need to be copied to the additive version, then `to_additive` should come last: ``` @[simp, to_additive] lemma mul_one' {G : Type*} [group G] (x : G) : x * 1 = x := mul_one x ``` The following attributes are supported and should be applied correctly by `to_additive` to the new additivized declaration, if they were present on the original one: ``` reducible, _refl_lemma, simp, norm_cast, instance, refl, symm, trans, elab_as_eliminator, no_rsimp, continuity, ext, ematch, measurability, alias, _ext_core, _ext_lemma_core, nolint ``` The exception to this rule is the `simps` attribute, which should come after `to_additive`: ``` @[to_additive, simps] instance {M N} [has_mul M] [has_mul N] : has_mul (M × N) := ⟨λ p q, ⟨p.1 * q.1, p.2 * q.2⟩⟩ ``` Additionally the `mono` attribute is not handled by `to_additive` and should be applied afterwards to both the original and additivized lemma. ## Implementation notes The transport process generally works by taking all the names of identifiers appearing in the name, type, and body of a declaration and creating a new declaration by mapping those names to additive versions using a simple string-based dictionary and also using all declarations that have previously been labeled with `to_additive`. In the `mul_comm'` example above, `to_additive` maps: * `mul_comm'` to `add_comm'`, * `comm_semigroup` to `add_comm_semigroup`, * `x * y` to `x + y` and `y * x` to `y + x`, and * `comm_semigroup.mul_comm'` to `add_comm_semigroup.add_comm'`. ### Heuristics `to_additive` uses heuristics to determine whether a particular identifier has to be mapped to its additive version. The basic heuristic is * Only map an identifier to its additive version if its first argument doesn't contain any unapplied identifiers. Examples: * `@has_mul.mul ℕ n m` (i.e. `(n * m : ℕ)`) will not change to `+`, since its first argument is `ℕ`, an identifier not applied to any arguments. * `@has_mul.mul (α × β) x y` will change to `+`. It's first argument contains only the identifier `prod`, but this is applied to arguments, `α` and `β`. * `@has_mul.mul (α × ℤ) x y` will not change to `+`, since its first argument contains `ℤ`. The reasoning behind the heuristic is that the first argument is the type which is "additivized", and this usually doesn't make sense if this is on a fixed type. There are some exceptions to this heuristic: * Identifiers that have the `@[to_additive]` attribute are ignored. For example, multiplication in `↥Semigroup` is replaced by addition in `↥AddSemigroup`. * If an identifier `d` has attribute `@[to_additive_relevant_arg n]` then the argument in position `n` is checked for a fixed type, instead of checking the first argument. `@[to_additive]` will automatically add the attribute `@[to_additive_relevant_arg n]` to a declaration when the first argument has no multiplicative type-class, but argument `n` does. * If an identifier has attribute `@[to_additive_ignore_args n1 n2 ...]` then all the arguments in positions `n1`, `n2`, ... will not be checked for unapplied identifiers (start counting from 1). For example, `times_cont_mdiff_map` has attribute `@[to_additive_ignore_args 21]`, which means that its 21st argument `(n : with_top ℕ)` can contain `ℕ` (usually in the form `has_top.top ℕ ...`) and still be additivized. So `@has_mul.mul (C^∞⟮I, N; I', G⟯) _ f g` will be additivized. ### Troubleshooting If `@[to_additive]` fails because the additive declaration raises a type mismatch, there are various things you can try. The first thing to do is to figure out what `@[to_additive]` did wrong by looking at the type mismatch error. * Option 1: It additivized a declaration `d` that should remain multiplicative. Solution: * Make sure the first argument of `d` is a type with a multiplicative structure. If not, can you reorder the (implicit) arguments of `d` so that the first argument becomes a type with a multiplicative structure (and not some indexing type)? The reason is that `@[to_additive]` doesn't additivize declarations if their first argument contains fixed types like `ℕ` or `ℝ`. See section Heuristics. If the first argument is not the argument with a multiplicative type-class, `@[to_additive]` should have automatically added the attribute `@[to_additive_relevant_arg]` to the declaration. You can test this by running the following (where `d` is the full name of the declaration): ``` run_cmd to_additive.relevant_arg_attr.get_param `d >>= tactic.trace ``` The expected output is `n` where the `n`-th argument of `d` is a type (family) with a multiplicative structure on it. If you get a different output (or a failure), you could add the attribute `@[to_additive_relevant_arg n]` manually, where `n` is an argument with a multiplicative structure. * Option 2: It didn't additivize a declaration that should be additivized. This happened because the heuristic applied, and the first argument contains a fixed type, like `ℕ` or `ℝ`. Solutions: * If the fixed type has an additive counterpart (like `↥Semigroup`), give it the `@[to_additive]` attribute. * If the fixed type occurs inside the `k`-th argument of a declaration `d`, and the `k`-th argument is not connected to the multiplicative structure on `d`, consider adding attribute `[to_additive_ignore_args k]` to `d`. * If you want to disable the heuristic and replace all multiplicative identifiers with their additive counterpart, use `@[to_additive!]`. * Option 3: Arguments / universe levels are incorrectly ordered in the additive version. This likely only happens when the multiplicative declaration involves `pow`/`^`. Solutions: * Ensure that the order of arguments of all relevant declarations are the same for the multiplicative and additive version. This might mean that arguments have an "unnatural" order (e.g. `monoid.npow n x` corresponds to `x ^ n`, but it is convenient that `monoid.npow` has this argument order, since it matches `add_monoid.nsmul n x`. * If this is not possible, add the `[to_additive_reorder k]` to the multiplicative declaration to indicate that the `k`-th and `(k+1)`-st arguments are reordered in the additive version. If neither of these solutions work, and `to_additive` is unable to automatically generate the additive version of a declaration, manually write and prove the additive version. Often the proof of a lemma/theorem can just be the multiplicative version of the lemma applied to `multiplicative G`. Afterwards, apply the attribute manually: ``` attribute [to_additive foo_add_bar] foo_bar ``` This will allow future uses of `to_additive` to recognize that `foo_bar` should be replaced with `foo_add_bar`. ### Handling of hidden definitions Before transporting the “main” declaration `src`, `to_additive` first scans its type and value for names starting with `src`, and transports them. This includes auxiliary definitions like `src._match_1`, `src._proof_1`. In addition to transporting the “main” declaration, `to_additive` transports its equational lemmas and tags them as equational lemmas for the new declaration, attributes present on the original equational lemmas are also transferred first (notably `_refl_lemma`). ### Structure fields and constructors If `src` is a structure, then `to_additive` automatically adds structure fields to its mapping, and similarly for constructors of inductive types. For new structures this means that `to_additive` automatically handles coercions, and for old structures it does the same, if ancestry information is present in `@[ancestor]` attributes. The `ancestor` attribute must come before the `to_additive` attribute, and it is essential that the order of the base structures passed to `ancestor` matches between the multiplicative and additive versions of the structure. ### Name generation * If `@[to_additive]` is called without a `name` argument, then the new name is autogenerated. First, it takes the longest prefix of the source name that is already known to `to_additive`, and replaces this prefix with its additive counterpart. Second, it takes the last part of the name (i.e., after the last dot), and replaces common name parts (“mul”, “one”, “inv”, “prod”) with their additive versions. * Namespaces can be transformed using `map_namespace`. For example: ``` run_cmd to_additive.map_namespace `quotient_group `quotient_add_group ``` Later uses of `to_additive` on declarations in the `quotient_group` namespace will be created in the `quotient_add_group` namespaces. * If `@[to_additive]` is called with a `name` argument `new_name` /without a dot/, then `to_additive` updates the prefix as described above, then replaces the last part of the name with `new_name`. * If `@[to_additive]` is called with a `name` argument `new_namespace.new_name` /with a dot/, then `to_additive` uses this new name as is. As a safety check, in the first case `to_additive` double checks that the new name differs from the original one. -/ @[user_attribute] protected meta def attr : user_attribute unit value_type := { name := `to_additive, descr := "Transport multiplicative to additive", parser := parser, after_set := some $ λ src prio persistent, do guard persistent <|> fail "`to_additive` can't be used as a local attribute", env ← get_env, val ← attr.get_param src, dict ← aux_attr.get_cache, ignore ← ignore_args_attr.get_cache, relevant ← relevant_arg_attr.get_cache, reorder ← reorder_attr.get_cache, tgt ← target_name src val.tgt dict val.allow_auto_name, aux_attr.set src tgt tt, let dict := dict.insert src tgt, first_mult_arg ← first_multiplicative_arg src, when (first_mult_arg ≠ 1) $ relevant_arg_attr.set src first_mult_arg tt, if env.contains tgt then proceed_fields env src tgt prio else do transform_decl_with_prefix_dict dict val.replace_all val.trace relevant ignore reorder src tgt [`reducible, `_refl_lemma, `simp, `norm_cast, `instance, `refl, `symm, `trans, `elab_as_eliminator, `no_rsimp, `continuity, `ext, `ematch, `measurability, `alias, `_ext_core, `_ext_lemma_core, `nolint], mwhen (has_attribute' `simps src) (trace "Apply the simps attribute after the to_additive attribute"), mwhen (has_attribute' `mono src) (trace $ "to_additive does not work with mono, apply the mono attribute to both" ++ "versions after"), match val.doc with | some doc := add_doc_string tgt doc | none := skip end } add_tactic_doc { name := "to_additive", category := doc_category.attr, decl_names := [`to_additive.attr], tags := ["transport", "environment", "lemma derivation"] } end to_additive /- map operations -/ attribute [to_additive] has_mul has_one has_inv has_div /- the following types are supported by `@[to_additive]` and mapped to themselves. -/ attribute [to_additive empty] empty attribute [to_additive pempty] pempty attribute [to_additive punit] punit attribute [to_additive unit] unit
670db8b4da0389fd2125288aef6f14c6c0f992e2
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/algebra/group/pi.lean
ff31bdd8c6cf549c06584eab42fc2f8008185464
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
5,610
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import algebra.ordered_group import tactic.pi_instances /-! # Pi instances for groups and monoids This file defines instances for group, monoid, semigroup and related structures on Pi Types -/ universes u v w variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) namespace pi @[to_additive] instance has_one [∀ i, has_one $ f i] : has_one (Π i : I, f i) := ⟨λ _, 1⟩ @[simp, to_additive] lemma one_apply [∀ i, has_one $ f i] : (1 : Π i, f i) i = 1 := rfl @[to_additive] instance has_mul [∀ i, has_mul $ f i] : has_mul (Π i : I, f i) := ⟨λ f g i, f i * g i⟩ @[simp, to_additive] lemma mul_apply [∀ i, has_mul $ f i] : (x * y) i = x i * y i := rfl @[to_additive] instance has_inv [∀ i, has_inv $ f i] : has_inv (Π i : I, f i) := ⟨λ f i, (f i)⁻¹⟩ @[simp, to_additive] lemma inv_apply [∀ i, has_inv $ f i] : x⁻¹ i = (x i)⁻¹ := rfl @[to_additive add_semigroup] instance semigroup [∀ i, semigroup $ f i] : semigroup (Π i : I, f i) := by refine_struct { mul := (*), .. }; tactic.pi_instance_derive_field @[to_additive add_comm_semigroup] instance comm_semigroup [∀ i, comm_semigroup $ f i] : comm_semigroup (Π i : I, f i) := by refine_struct { mul := (*), .. }; tactic.pi_instance_derive_field @[to_additive add_monoid] instance monoid [∀ i, monoid $ f i] : monoid (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field @[to_additive add_comm_monoid] instance comm_monoid [∀ i, comm_monoid $ f i] : comm_monoid (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), .. }; tactic.pi_instance_derive_field @[to_additive add_group] instance group [∀ i, group $ f i] : group (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), inv := has_inv.inv, .. }; tactic.pi_instance_derive_field @[simp] lemma sub_apply [∀ i, add_group $ f i] : (x - y) i = x i - y i := rfl @[to_additive add_comm_group] instance comm_group [∀ i, comm_group $ f i] : comm_group (Π i : I, f i) := by refine_struct { one := (1 : Π i, f i), mul := (*), inv := has_inv.inv, .. }; tactic.pi_instance_derive_field @[to_additive add_left_cancel_semigroup] instance left_cancel_semigroup [∀ i, left_cancel_semigroup $ f i] : left_cancel_semigroup (Π i : I, f i) := by refine_struct { mul := (*) }; tactic.pi_instance_derive_field @[to_additive add_right_cancel_semigroup] instance right_cancel_semigroup [∀ i, right_cancel_semigroup $ f i] : right_cancel_semigroup (Π i : I, f i) := by refine_struct { mul := (*) }; tactic.pi_instance_derive_field @[to_additive ordered_cancel_add_comm_monoid] instance ordered_cancel_comm_monoid [∀ i, ordered_cancel_comm_monoid $ f i] : ordered_cancel_comm_monoid (Π i : I, f i) := by refine_struct { mul := (*), one := (1 : Π i, f i), le := (≤), lt := (<), .. pi.partial_order }; tactic.pi_instance_derive_field @[to_additive ordered_add_comm_group] instance ordered_comm_group [∀ i, ordered_comm_group $ f i] : ordered_comm_group (Π i : I, f i) := { mul_le_mul_left := λ x y hxy c i, mul_le_mul_left' (hxy i) _, ..pi.comm_group, ..pi.partial_order } variables [decidable_eq I] variables [Π i, has_zero (f i)] /-- The function supported at `i`, with value `x` there. -/ def single (i : I) (x : f i) : Π i, f i := λ i', if h : i' = i then (by { subst h, exact x }) else 0 @[simp] lemma single_eq_same (i : I) (x : f i) : single i x i = x := begin dsimp [single], split_ifs, { refl, }, { exfalso, exact h rfl, } end @[simp] lemma single_eq_of_ne {i i' : I} (h : i' ≠ i) (x : f i) : single i x i' = 0 := begin dsimp [single], split_ifs with h', { exfalso, exact h h', }, { refl, } end end pi section monoid_hom variables (f) [Π i, monoid (f i)] /-- Evaluation of functions into an indexed collection of monoids at a point is a monoid homomorphism. -/ @[to_additive "Evaluation of functions into an indexed collection of additive monoids at a point is an additive monoid homomorphism."] def monoid_hom.apply (i : I) : (Π i, f i) →* f i := { to_fun := λ g, g i, map_one' := rfl, map_mul' := λ x y, rfl, } @[simp, to_additive] lemma monoid_hom.apply_apply (i : I) (g : Π i, f i) : (monoid_hom.apply f i) g = g i := rfl end monoid_hom section add_monoid_single variables [decidable_eq I] (f) [Π i, add_monoid (f i)] open pi /-- The additive monoid homomorphism including a single additive monoid into a dependent family of additive monoids, as functions supported at a point. -/ def add_monoid_hom.single (i : I) : f i →+ Π i, f i := { to_fun := λ x, single i x, map_zero' := begin ext i', by_cases h : i' = i, { subst h, simp only [single_eq_same], refl, }, { simp only [h, single_eq_of_ne, ne.def, not_false_iff], refl, }, end, map_add' := λ x y, begin ext i', by_cases h : i' = i, -- FIXME in the next two `simp only`s, -- it would be really nice to not have to provide the arguments to `add_apply`. { subst h, simp only [single_eq_same, add_apply (single i' x) (single i' y) i'], }, { simp only [h, add_zero, single_eq_of_ne, add_apply (single i x) (single i y) i', ne.def, not_false_iff], }, end, } @[simp] lemma add_monoid_hom.single_apply {i : I} (x : f i) : (add_monoid_hom.single f i) x = single i x := rfl end add_monoid_single
44b52de91490688dd748c5b507349b088f5f7786
649957717d58c43b5d8d200da34bf374293fe739
/src/algebra/Mon/colimits.lean
2d268830355d0144a0e49cb8d42499bfc3402c89
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
6,533
lean
import algebra.Mon.basic import category_theory.limits.limits universes v open category_theory open category_theory.limits /- We build colimits of monoids. We do so knowing nothing about monoids. In particular, I want to claim that this file could be produced by a python script that just looks at the output of `#print monoid`: -- structure monoid : Type u → Type u -- fields: -- monoid.mul : Π {α : Type u} [c : monoid α], α → α → α -- monoid.mul_assoc : ∀ {α : Type u} [c : monoid α] (a b c_1 : α), a * b * c_1 = a * (b * c_1) -- monoid.one : Π (α : Type u) [c : monoid α], α -- monoid.one_mul : ∀ {α : Type u} [c : monoid α] (a : α), 1 * a = a -- monoid.mul_one : ∀ {α : Type u} [c : monoid α] (a : α), a * 1 = a and if we'd fed it the output of `#print comm_ring`, this file would instead build colimits of commutative rings. A slightly bolder claim is that we could do this with tactics, as well. -/ namespace Mon.colimits variables {J : Type v} [small_category J] (F : J ⥤ Mon.{v}) inductive prequotient -- There's always `of` | of : Π (j : J) (x : (F.obj j).α), prequotient -- Then one generator for each operation | one {} : prequotient | mul : prequotient → prequotient → prequotient open prequotient inductive relation : prequotient F → prequotient F → Prop -- Make it an equivalence relation: | refl : Π (x), relation x x | symm : Π (x y) (h : relation x y), relation y x | trans : Π (x y z) (h : relation x y) (k : relation y z), relation x z -- There's always a `map` relation | map : Π (j j' : J) (f : j ⟶ j') (x : (F.obj j).α), relation (of j' ((F.map f) x)) (of j x) -- Then one relation per operation, describing the interaction with `of` | mul : Π (j) (x y : (F.obj j).α), relation (of j (x * y)) (mul (of j x) (of j y)) | one : Π (j), relation (of j 1) one -- Then one relation per argument of each operation | mul_1 : Π (x x' y) (r : relation x x'), relation (mul x y) (mul x' y) | mul_2 : Π (x y y') (r : relation y y'), relation (mul x y) (mul x y') -- And one relation per axiom | mul_assoc : Π (x y z), relation (mul (mul x y) z) (mul x (mul y z)) | one_mul : Π (x), relation (mul one x) x | mul_one : Π (x), relation (mul x one) x def colimit_setoid : setoid (prequotient F) := { r := relation F, iseqv := ⟨relation.refl, relation.symm, relation.trans⟩ } attribute [instance] colimit_setoid def colimit_type : Type v := quotient (colimit_setoid F) instance monoid_colimit_type : monoid (colimit_type F) := { mul := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (mul x y) }, { intros y y' r, apply quot.sound, exact relation.mul_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.mul_1 _ _ _ r }, { refl } }, end, one := begin exact quot.mk _ one end, mul_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.mul_assoc, refl, refl, refl, end, one_mul := λ x, begin induction x, dsimp, apply quot.sound, apply relation.one_mul, refl, end, mul_one := λ x, begin induction x, dsimp, apply quot.sound, apply relation.mul_one, refl, end } @[simp] lemma quot_one : quot.mk setoid.r one = (1 : colimit_type F) := rfl @[simp] lemma quot_mul (x y) : quot.mk setoid.r (mul x y) = ((quot.mk setoid.r x) * (quot.mk setoid.r y) : colimit_type F) := rfl def colimit : Mon := ⟨colimit_type F, by apply_instance⟩ def cocone_fun (j : J) (x : (F.obj j).α) : colimit_type F := quot.mk _ (of j x) instance cocone_is_hom (j : J) : is_monoid_hom (cocone_fun F j) := { map_one := begin apply quot.sound, apply relation.one, end, map_mul := λ x y, begin apply quot.sound, apply relation.mul, end } def cocone_morphism (j : J) : F.obj j ⟶ colimit F := { val := cocone_fun F j, property := by apply_instance } @[simp] lemma cocone_naturality {j j' : J} (f : j ⟶ j') : F.map f ≫ (cocone_morphism F j') = cocone_morphism F j := begin ext, apply quot.sound, apply relation.map, end @[simp] lemma cocone_naturality_components (j j' : J) (f : j ⟶ j') (x : F.obj j): (cocone_morphism F j') (F.map f x) = (cocone_morphism F j) x := by { rw ←cocone_naturality F f, refl } def colimit_cocone : cocone F := { X := colimit F, ι := { app := cocone_morphism F, } }. @[simp] def desc_fun_lift (s : cocone F) : prequotient F → s.X | (of j x) := (s.ι.app j) x | one := 1 | (mul x y) := desc_fun_lift x * desc_fun_lift y def desc_fun (s : cocone F) : colimit_type F → s.X := begin fapply quot.lift, { exact desc_fun_lift F s }, { intros x y r, induction r; try { dsimp }, -- refl { refl }, -- symm { exact r_ih.symm }, -- trans { exact eq.trans r_ih_h r_ih_k }, -- map { rw cocone.naturality_bundled, }, -- mul { rw is_monoid_hom.map_mul ⇑((s.ι).app r_j) }, -- one { erw is_monoid_hom.map_one ⇑((s.ι).app r), refl }, -- mul_1 { rw r_ih, }, -- mul_2 { rw r_ih, }, -- mul_assoc { rw mul_assoc, }, -- one_mul { rw one_mul, }, -- mul_one { rw mul_one, } } end instance desc_fun_is_morphism (s : cocone F) : is_monoid_hom (desc_fun F s) := { map_one := rfl, map_mul := λ x y, begin induction x, induction y, refl, refl, refl, end, } @[simp] def desc_morphism (s : cocone F) : colimit F ⟶ s.X := { val := desc_fun F s, property := by apply_instance } def colimit_is_colimit : is_colimit (colimit_cocone F) := { desc := λ s, desc_morphism F s, uniq' := λ s m w, begin ext, induction x, induction x, { have w' := congr_fun (congr_arg (λ f : F.obj x_j ⟶ s.X, (f : F.obj x_j → s.X)) (w x_j)) x_x, erw w', refl, }, { simp only [desc_morphism, quot_one], erw is_monoid_hom.map_one ⇑m, refl, }, { simp only [desc_morphism, quot_mul], erw is_monoid_hom.map_mul ⇑m, rw [x_ih_a, x_ih_a_1], refl, }, refl end }. -- FIXME why is this infer_instance needed!? instance has_colimits_Mon : @has_colimits Mon.{v} infer_instance := { has_colimits_of_shape := λ J 𝒥, { has_colimit := λ F, by exactI { cocone := colimit_cocone F, is_colimit := colimit_is_colimit F } } } end Mon.colimits
066183f262dda4e9fce104d54a0f6ef357b6e650
d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6
/Papyrus/IR/ConstantRefs.lean
a842a7fa7703f3cbbfc0f55b2dce5268ea32324c
[ "Apache-2.0" ]
permissive
xubaiw/lean4-papyrus
c3fbbf8ba162eb5f210155ae4e20feb2d32c8182
02e82973a5badda26fc0f9fd15b3d37e2eb309e0
refs/heads/master
1,691,425,756,824
1,632,122,825,000
1,632,123,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,649
lean
import Papyrus.Context import Papyrus.IR.TypeRefs import Papyrus.IR.ConstantRef namespace Papyrus namespace TypeRef /- Get a reference to a null (`0`) constant of this type. -/ @[extern "papyrus_get_null_constant"] constant getNullConstant (self : @& TypeRef) : IO ConstantRef /- Get a reference to a constant of this type with all bits set to `1`. -/ @[extern "papyrus_get_all_ones_constant"] constant getAllOnesConstant (self : @& TypeRef) : IO ConstantRef end TypeRef /-- A reference to an external LLVM [ConstantData](https://llvm.org/doxygen/classllvm_1_1ConstantData.html). -/ structure ConstantDataRef extends ConstantRef instance : Coe ConstantDataRef ConstantRef := ⟨(·.toConstantRef)⟩ -------------------------------------------------------------------------------- -- Constant Ints (Words / Integers / Natural) -------------------------------------------------------------------------------- -- # Constant Word /-- A reference to an external LLVM [ConstantInt](https://llvm.org/doxygen/classllvm_1_1ConstantInt.html). Such a constant can be used to represent a block of bits (i.e., a word), an unsigned integer (a natural), or a true integer. -/ structure ConstantIntRef extends ConstantDataRef where is_constant_int : toValueRef.valueKind = ValueKind.constantInt instance : Coe ConstantIntRef ConstantDataRef := ⟨(·.toConstantDataRef)⟩ namespace ConstantIntRef /-- Cast a general `ValueRef` to a `ConstantIntRef` given proof it is one. -/ def cast (val : ValueRef) (h : val.valueKind = ValueKind.constantInt) : ConstantIntRef := {toValueRef := val, is_constant_int := h} /-- Get the LLVM true constant (i.e., `i1 0`). -/ @[extern "papyrus_get_constant_false"] constant getFalse : LlvmM ConstantIntRef /-- Get the LLVM true constant (i.e., `i1 1`). -/ @[extern "papyrus_get_constant_true"] constant getTrue : LlvmM ConstantIntRef /-- Get an i1 constant for a `Bool` (i.e., `1` for `true`, `0` for `false`). -/ def ofBool : (value : Bool) → LlvmM ConstantIntRef | false => getFalse | true => getTrue /-- Get an i8 constant for a `UInt8`. -/ @[extern "papyrus_get_constant_uint8"] constant ofUInt8 (value : UInt8) : LlvmM ConstantIntRef /-- Get an i16 constant for a `UInt16`. -/ @[extern "papyrus_get_constant_uint16"] constant ofUInt16 (value : UInt16) : LlvmM ConstantIntRef /-- Get an i32 constant for a `UInt32`. -/ @[extern "papyrus_get_constant_uint32"] constant ofUInt32 (value : UInt32) : LlvmM ConstantIntRef /-- Get an i64 constant for a `UInt64`. -/ @[extern "papyrus_get_constant_uint64"] constant ofUInt64 (value : UInt64) : LlvmM ConstantIntRef /-- Get a constant with the given Nat value truncated to `numBits`. -/ @[extern "papyrus_get_constant_nat_of_size"] constant ofNat (numBits : UInt32) (value : @& Nat) : LlvmM ConstantIntRef /-- Get a constant with the given Int value truncated to `numBits`. -/ @[extern "papyrus_get_constant_int_of_size"] constant ofInt (numBits : UInt32) (value : @& Int) : LlvmM ConstantIntRef /-- Get the integer type of this constant. -/ @[extern "papyrus_value_get_type"] constant getType (self : @& ConstantIntRef) : IO IntegerTypeRef /-- Get the value of this constant as a `Nat`. That is, treat its bits as representing a native unsigned integer. -/ @[extern "papyrus_constant_int_get_nat_value"] constant getNatValue (self : @& ConstantIntRef) : IO Nat /-- Get the value of this constant as an `Int`. That is, treat its bits as representing a native integer. -/ @[extern "papyrus_constant_int_get_int_value"] constant getIntValue (self : @& ConstantIntRef) : IO Int end ConstantIntRef -- # Integer Type -> ConstantInt/ConstantNat Convenience Functions namespace IntegerTypeRef /-- Get a reference to a constant of this type with the given `Int` value. The value will be truncated and/or extended as necessary to make it fit. -/ @[extern "papyrus_get_constant_int_of_type"] constant getConstantInt (value : @& Int) (self : @& IntegerTypeRef) : IO ConstantIntRef /-- Get a reference to a constant of this type with the given `Nat` value. The value will be truncated and/or extended as necessary to make it fit. -/ @[extern "papyrus_get_constant_nat_of_type"] constant getConstantNat (value : @& Nat) (self : @& IntegerTypeRef) : IO ConstantIntRef end IntegerTypeRef -------------------------------------------------------------------------------- -- Constant Data Arrays -------------------------------------------------------------------------------- /-- A reference to an external LLVM [ConstantDataSequential](https://llvm.org/doxygen/classllvm_1_1ConstantDataSequential.html). -/ structure ConstantDataSequentialRef extends ConstantDataRef instance : Coe ConstantDataSequentialRef ConstantDataRef := ⟨(·.toConstantDataRef)⟩ namespace ConstantDataSequentialRef /-- Check whether this constant is an i8 array. -/ @[extern "papyrus_constant_data_sequential_is_string"] constant isString (const : @& ConstantDataSequentialRef) : IO Bool /-- Get the value of this constant as a `String` by treating its bytes as characters. -/ @[extern "papyrus_constant_data_sequential_get_as_string"] constant getAsString (const : @& ConstantDataSequentialRef) : IO String end ConstantDataSequentialRef /-- A reference to an external LLVM [ConstantDataArray](https://llvm.org/doxygen/classllvm_1_1ConstantDataArray.html). -/ structure ConstantDataArrayRef extends ConstantDataSequentialRef where is_constant_data_array : toValueRef.valueKind = ValueKind.constantDataArray instance : Coe ConstantDataArrayRef ConstantDataSequentialRef := ⟨(·.toConstantDataSequentialRef)⟩ namespace ConstantDataArrayRef /-- Cast a general `ValueRef` to a `ConstantDataArrayRef` given proof it is one. -/ def cast (val : ValueRef) (h : val.valueKind = ValueKind.constantDataArray) : ConstantDataArrayRef := {toValueRef := val, is_constant_data_array := h} /-- Get the array type of this constant. -/ @[extern "papyrus_value_get_type"] constant getType (self : @& ConstantDataArrayRef) : IO ArrayTypeRef /-- Get the type of elements of this constant. -/ def getElementType (self : @& ConstantDataArrayRef) : IO TypeRef := do (← self.getType).getElementType /-- Get reference to a UTF-8 encoded string constant. If `withNull` is true, the string is null terminated. -/ @[extern "papyrus_get_constant_string"] constant ofString (str : @& String) (withNull := true) : LlvmM ConstantDataArrayRef end ConstantDataArrayRef -------------------------------------------------------------------------------- -- Constant Expressions -------------------------------------------------------------------------------- /-- A reference to an external LLVM [ConstantExpr](https://llvm.org/doxygen/classllvm_1_1ConstantExpr.html). -/ structure ConstantExprRef extends ConstantRef where is_constant_expr : toValueRef.valueKind = ValueKind.constantExpr instance : Coe ConstantExprRef ConstantRef := ⟨(·.toConstantRef)⟩ namespace ConstantExprRef /-- Cast a general `ValueRef` to a `ConstantExprRef` given proof it is one. -/ def cast (val : ValueRef) (h : val.valueKind = ValueKind.constantExpr) : ConstantExprRef := {toValueRef := val, is_constant_expr := h} -- ## `getelementptr` /-- Get a constant GEP expression that calculates the address of the given sub-element of an aggregate data structure. See the [`getelementptr`](https://llvm.org/docs/LangRef.html#getelementptr-instruction) docs for more details. -/ @[extern "papyrus_constant_expr_get_element_ptr"] constant getGetElementPtr (aggregate : @& ConstantRef) (indices : @& Array ConstantRef) (inBounds := false) : IO ConstantRef /-- Get a constant GEP expression with an additional `inrange` index. See the [`getelementptr`](https://llvm.org/docs/LangRef.html#getelementptr-instruction) docs for more details. -/ @[extern "papyrus_constant_expr_get_element_ptr_in_range"] constant getGetElementPtrInRange (aggregate : @& ConstantRef) (indices : @& Array ConstantRef) (inRange : UInt32) (inBounds := false) : IO ConstantRef -- ## `ptrtoint` /-- Get a reference to a constant `ptrtoint` expression that converts the given constant integer to the given pointer type. -/ @[extern "papyrus_constant_expr_get_ptr_to_int"] constant getPtrToInt (const : @& ConstantRef) (type : @& TypeRef) : IO ConstantRef -- ## `inttoptr` /-- Get a reference to a constant `inttoptr` expression that converts the given constant pointer to the given integer type. -/ @[extern "papyrus_constant_expr_get_int_to_ptr"] constant getIntToPtr (const : @& ConstantRef) (type : @& TypeRef) : IO ConstantRef end ConstantExprRef
5411a1dde727662d913bd8c7c03b1c3198489625
5df84495ec6c281df6d26411cc20aac5c941e745
/src/formal_ml/probability_space.lean
d2cc33db13a7f413b88374cb4102e4d260d7f794
[ "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
97,365
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 measure_theory.measurable_space import measure_theory.measure_space import measure_theory.outer_measure import measure_theory.lebesgue_measure import measure_theory.integration import measure_theory.borel_space import data.set.countable import formal_ml.nnreal import formal_ml.sum import formal_ml.lattice import formal_ml.measurable_space import formal_ml.classical import data.equiv.list /-! This file defines the basic concepts in probability theory. There are four fundamental principles: 1. Make theorems as readable as possible. Use Pr[A ∧ B], not μ (A ∩ B). Other examples: Pr[(X >ᵣ 3) ∨ (Y =ᵣ 7)]. While events are technically sets, in probability theory, they are better written as propositions that may or may not hold. 2. Avoid absurd statements where possible. Don't allow Pr[A] if A is not an event, or Pr[X ∈ᵣ S] if S is not measurable, or Pr[∀ᵣ a in S, E a] if S is not countable. It is always possible to write Pr[⟨S, ...proof S is an event...⟩]. 3. Embed measurability into the objects and operations themselves. An event is measurable by definition. When we take the and, or, not, for all, exists, measurability should be automatic. 4. Don't expect the reader to know measure theory, but at times it may be required by the author. Several concepts are defined in this module: probability_space: a measure_space where the measure has a value of 1. measurable_setB: a subtype of a set that is measurable (defined based upon the measurable space). event: a measurable_setB on a probability space (defined based upon the probability). Pr[E]: the probability of an event (note: expectations are defined in real_random_variable). measurable_fun: a subtype of a function that is measurable (denoted (M₁ →ₘ M₂)). random_variable: a measurable_fun whose domain is a probability space (denoted (P →ᵣ M)). Some symbols are defined as well: * (∀ᵣ i, E i): for all E * (∃ᵣ i, F i): exists i, such that F. * X ∈ᵣ S: the event that the random variable X is in the measurable set S. * and more! Also, various independence and identical definitions are specified. Some choices: * A and B are independent if A has zero probability. * an infinite family of events/random variables is independent if every finite subset is independent. * Two random variables are identical if they have equal probability on every measurable set. The probability spaces on which they are defined need not be equal. -/ /- In the latest src/data/equiv/list.lean, but not yet included -/ noncomputable def fintype.encodable (α : Type*) [fintype α] : encodable α := by { classical, exact (encodable.trunc_encodable_of_fintype α).out } def set.symmetric_difference {α :Type*} (A B:set α) := (A \ B) ∪ (B \ A) class has_symm_diff (α : Type*) := (symm_diff : α → α → α) -- U+2206: symmetric difference infixr ` ∆ `:70 := has_symm_diff.symm_diff instance set.has_symm_diff {α : Type*}: has_symm_diff (set α) := ⟨set.symmetric_difference⟩ lemma set.has_symm_diff.def {α : Type*} {A B:set α}:A ∆ B = (A \ B) ∪ (B \ A) := rfl class probability_space (α: Type*) extends measure_theory.measure_space α := (univ_one:volume.measure_of (set.univ) = 1) instance probability_space.to_measurable_space (α:Type*) [probability_space α]:measurable_space α := measure_theory.measure_space.to_measurable_space @[simp] lemma probability_space.univ_one' {α:Type*} (Pα:probability_space α): (@measure_theory.measure_space.volume α Pα.to_measure_space) (@set.univ α) = 1 := begin rw ← measure_theory.coe_to_outer_measure, rw ← measure_theory.outer_measure.measure_of_eq_coe, rw probability_space.univ_one end --measure_of_eq_coe /- In measure theory (and specifically, in probability theory), not all sets of outcomes have probabilities that can be measured. We represent those that can be measured as measurable sets. -/ def measurable_setB {α:Type*} (M:measurable_space α):Type* := subtype (M.measurable_set') def measurable_setB.mk {α:Type*} {M:measurable_space α} {S:set α} (H:measurable_set S):measurable_setB M := ⟨S, H⟩ lemma measurable_setB_val_eq_coe {Ω:Type*} {P:measurable_space Ω} (X:measurable_setB P):X.val = (@coe (subtype (@measurable_set Ω _)) (set Ω) _ X) := begin refl end /- A measurable set on a measurable space that has a probability measure is called an event. -/ def event {Ω:Type*} (M:probability_space Ω):Type* := measurable_setB (probability_space.to_measurable_space Ω) lemma event_val_eq_coe {Ω:Type*} {P:probability_space Ω} (X:event P):X.val = (@coe (subtype (@measurable_set Ω _)) (set Ω) _ X) := begin refl end lemma event.eq {Ω:Type*} {P:probability_space Ω} (A B:event P): A.val = B.val → A = B := begin intro A1, apply subtype.eq, exact A1 end def event_mem {Ω:Type*} [P:probability_space Ω] (a:Ω) (E:event P):Prop := a∈ E.val instance {Ω:Type*} [P:probability_space Ω]:has_mem Ω (event P) := { mem := event_mem } lemma event_mem_val {Ω:Type*} [P:probability_space Ω] (ω:Ω) (E:event P): (ω ∈ E) = (ω ∈ E.val) := rfl lemma prob_le_1 {Ω:Type*} {P:probability_space Ω} (S:set Ω): P.volume.measure_of S ≤ 1 := begin have A1:P.volume.measure_of set.univ = 1, { apply P.univ_one, }, have A2:S ⊆ set.univ, { simp, }, have A3:P.volume.measure_of S ≤ P.volume.measure_of set.univ, { apply P.volume.mono, apply A2, }, rw A1 at A3, exact A3, end /- There are a lot of long proofs here, but this one seems particularly roundabout. -/ lemma prob_not_infinite {Ω:Type*} {P:probability_space Ω} (S:set Ω): (P.volume.measure_of S) ≠ ⊤ := begin have A1:P.volume.measure_of S ≤ 1, { apply prob_le_1, }, intro A2, rw A2 at A1, have A3:(1:ennreal)=⊤, { apply complete_linear_order.le_antisymm, { apply (ennreal.complete_linear_order.le_top), }, { apply A1, } }, have A4:(1:ennreal) ≠ (⊤:ennreal), { apply ennreal.one_ne_top, }, rw A3 at A4, apply A4, refl, end lemma prob_nnreal {Ω:Type*} {P:probability_space Ω} (S:set Ω): ↑((P.volume.measure_of S).to_nnreal) = P.volume.measure_of S := begin apply ennreal.coe_to_nnreal, apply prob_not_infinite, end def event_prob {Ω:Type*} {P:probability_space Ω} (E:event P):nnreal := (P.volume.measure_of E.val).to_nnreal notation `Pr[`E`]` := event_prob E lemma event_prob_def {Ω:Type*} {p:probability_space Ω} (E:event p): ↑(Pr[E]) = (p.volume.measure_of E.val):= begin unfold event_prob, apply prob_nnreal, end lemma to_nnreal_almost_monotonic (a b:ennreal):(a≠ ⊤)→(b≠⊤)→(a ≤ b)→ (a.to_nnreal ≤ b.to_nnreal) := begin intros A1 A2 A3, have A4:↑(a.to_nnreal)=a, { apply ennreal.coe_to_nnreal, apply A1, }, have A5:↑(b.to_nnreal)=b, { apply ennreal.coe_to_nnreal, apply A2, }, rw ← A4 at A3, rw ← A5 at A3, simp at A3, apply A3, end lemma to_ennreal_monotonic (a b:nnreal):(a ≤ b)→ ((a:ennreal) ≤ (b:ennreal)) := begin intro A1, simp, apply A1, end -- See ennreal.add_eq_top lemma add_finite (a b:ennreal):(a≠ ⊤) → (b≠ ⊤) → (a + b≠ ⊤) := begin intros A1 A2 A3, rw ennreal.add_eq_top at A3, cases A3, { apply A1, apply A3, }, { apply A2, apply A3, } end lemma event_prob_mono1 {Ω:Type*} {p:probability_space Ω} (E F:event p): p.volume.measure_of E.val ≤ p.volume.measure_of F.val → Pr[E] ≤ Pr[F] := begin unfold event_prob, intro A1, apply to_nnreal_almost_monotonic, apply prob_not_infinite, apply prob_not_infinite, apply A1, end lemma event_prob_mono2 {Ω:Type*} {p:probability_space Ω} (E F:event p): (E.val ⊆ F.val) → Pr[E] ≤ Pr[F] := begin intro A1, apply event_prob_mono1, apply p.volume.mono, apply A1, end def measurable_setB_univ {Ω:Type*} {M:measurable_space Ω}:measurable_setB M := { val := set.univ, property := measurable_set.univ, } def event_univ {Ω:Type*} {p:probability_space Ω}:event p := measurable_setB_univ @[simp] lemma event_univ_val_def {Ω:Type*} {p:probability_space Ω}: (@event_univ Ω p).val = set.univ := begin unfold event_univ measurable_setB_univ, end @[simp] lemma Pr_event_univ {Ω:Type*} {p:probability_space Ω}: Pr[@event_univ Ω p] = 1 := begin have A1:↑(Pr[@event_univ Ω p]) = (1:ennreal), { rw event_prob_def, apply p.univ_one, }, simp at A1, apply A1 end @[simp] lemma Pr_le_one {Ω:Type*} {p:probability_space Ω} {E:event p}: Pr[E] ≤ 1 := begin have A1:Pr[E] ≤ Pr[@event_univ Ω p], { apply event_prob_mono2, rw event_univ_val_def, rw set.subset_def,simp, }, rw Pr_event_univ at A1, apply A1, end def measurable_setB_empty {Ω:Type*} {p:measurable_space Ω}:measurable_setB p := { val := ∅, property := measurable_set.empty, } instance has_emptyc_measurable_setB {Ω:Type*} {M:measurable_space Ω}:has_emptyc (measurable_setB M) := ⟨ @measurable_setB_empty Ω M ⟩ def event_empty {Ω:Type*} {p:probability_space Ω}:event p := @measurable_setB_empty Ω (probability_space.to_measurable_space Ω) instance has_emptyc_event {Ω:Type*} {P:probability_space Ω}:has_emptyc (event P) := ⟨ @event_empty Ω P ⟩ lemma has_emptyc_emptyc_event {Ω:Type*} {P:probability_space Ω}: ∅ = (@event_empty Ω P) := rfl @[simp] lemma event_empty_val_def {Ω:Type*} {p:probability_space Ω}: (@event_empty Ω p).val = ∅ := rfl @[simp] lemma event_empty_val_def2 {Ω:Type*} {p:probability_space Ω}: (@has_emptyc.emptyc (event p) _).val = ∅ := rfl @[simp] lemma Pr_event_empty {Ω:Type*} {p:probability_space Ω}: Pr[@event_empty Ω p] = 0 := begin have A1:↑(Pr[@event_empty Ω p]) = (0:ennreal), { rw event_prob_def, apply p.volume.empty, }, simp at A1, apply A1 end @[simp] lemma Pr_event_empty' {Ω:Type*} {p:probability_space Ω}: Pr[(∅:event p)] = 0 := begin rw has_emptyc_emptyc_event, apply Pr_event_empty, end /-Since Pr[E] is a nnreal, this establishes that the probability is in the interval [0,1] -/ lemma event_prob_le_1 {Ω:Type*} {p:probability_space Ω} {E:event p}: Pr[E] ≤ 1 := begin have A1:Pr[@event_univ Ω p] = 1, { apply Pr_event_univ, }, rw ← A1, apply event_prob_mono2, rw event_univ_val_def, simp, end def event_const {Ω:Type*} {p:probability_space Ω} (P:Prop):event p := { val := {ω:Ω|P}, property := measurable_set.const P, } @[simp] lemma event_const_val_def {Ω:Type*} {p:probability_space Ω} (P:Prop): (@event_const _ p P).val={ω:Ω|P} := rfl lemma event_const_true_eq_univ {Ω:Type*} {p:probability_space Ω} (P:Prop):P → (@event_const _ p P)=event_univ := begin intro A1, apply event.eq, simp [A1], end lemma event_const_false_eq_empty {Ω:Type*} {p:probability_space Ω} (P:Prop):¬P → (@event_const _ p P)=event_empty := begin intro A1, apply event.eq, simp [A1], end lemma Pr_event_const_true {Ω:Type*} {p:probability_space Ω} (P:Prop):P → Pr[(@event_const _ p P)]=1 := begin intro A1, rw event_const_true_eq_univ, apply Pr_event_univ, exact A1, end lemma Pr_event_const_false {Ω:Type*} {p:probability_space Ω} (P:Prop):¬P → Pr[(@event_const _ p P)]=0 := begin intro A1, rw event_const_false_eq_empty, apply Pr_event_empty, exact A1, end --The and of two events. def measurable_inter {Ω:Type*} {p:measurable_space Ω} (A B:measurable_setB p):measurable_setB p := { val:=A.val ∩ B.val, property := measurable_set.inter A.property B.property, } @[simp] lemma measurable_inter_val_def {Ω:Type*} {p:measurable_space Ω} (A B:measurable_setB p): (measurable_inter A B).val= A.val ∩ B.val := rfl instance measurable_setB_has_inter {Ω:Type*} {p:measurable_space Ω}:has_inter (measurable_setB p) := { inter := @measurable_inter Ω p, } @[simp] lemma measurable_inter_val_def2 {Ω:Type*} {p:measurable_space Ω} (A B:measurable_setB p): (A ∩ B).val= A.val ∩ B.val := rfl def eand {Ω:Type*} {p:probability_space Ω} (A B:event p):event p := measurable_inter A B /-{ val:=A.val ∩ B.val, property := measurable_set.inter A.property B.property, }-/ infixr `∧` := eand @[simp] lemma eand_val_def {Ω:Type*} {p:probability_space Ω} (A B:event p): (A ∧ B).val = A.val ∩ B.val := begin refl, end lemma eand_comm {Ω:Type*} {p:probability_space Ω} (A B:event p): (A ∧ B) = (B ∧ A) := begin apply event.eq, simp [set.inter_comm], end lemma eand_assoc {Ω:Type*} {p:probability_space Ω} (A B C:event p): ((A ∧ B) ∧ C) = (A ∧ (B ∧ C)) := begin apply event.eq, simp [set.inter_assoc], end lemma eand_eq_self_of_subset_left {Ω:Type*} {p:probability_space Ω} (A B:event p): (A.val ⊆ B.val) → (A ∧ B) = A := begin intro A1, apply event.eq, simp, --rw eand_val_def, apply set.inter_eq_self_of_subset_left, exact A1, end lemma eand_eq_self_of_subset_right {Ω:Type*} {p:probability_space Ω} (A B:event p): (B.val ⊆ A.val) → (A ∧ B) = B := begin intro A1, rw eand_comm, apply eand_eq_self_of_subset_left, exact A1, end lemma Pr_eand_le_left {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[A ∧ B]≤ Pr[A] := begin apply event_prob_mono2, rw eand_val_def, apply set.inter_subset_left, end lemma Pr_eand_le_right {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[A ∧ B]≤ Pr[B] := begin rw eand_comm, apply Pr_eand_le_left, end lemma Pr_eand_le_min {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[A ∧ B]≤ min Pr[A] Pr[B] := begin apply le_min, { apply Pr_eand_le_left, }, { apply Pr_eand_le_right, } end def measurable_union {Ω:Type*} {p:measurable_space Ω} (A B:measurable_setB p):measurable_setB p := { val:=A.val ∪ B.val, property := measurable_set.union A.property B.property, } @[simp] lemma measurable_union_val_def {Ω:Type*} {p:measurable_space Ω} (A B:measurable_setB p): (measurable_union A B).val=A.val ∪ B.val := rfl instance measurable_setB_has_union {Ω:Type*} {p:measurable_space Ω}:has_union (measurable_setB p) := { union := @measurable_union Ω p, } @[simp] lemma measurable_union_val_def2 {Ω:Type*} {p:measurable_space Ω} (A B:measurable_setB p): (A ∪ B).val = A.val ∪ B.val := rfl def eor {Ω:Type*} {p:probability_space Ω} (A B:event p):event p := measurable_union A B /-{ val:=A.val ∪ B.val, property := measurable_set.union A.property B.property, }-/ infixr `∨` := eor @[simp] lemma eor_val_def {Ω:Type*} {p:probability_space Ω} (A B:event p): (A ∨ B).val = A.val ∪ B.val := begin refl, end lemma eor_comm {Ω:Type*} {p:probability_space Ω} (A B:event p): (A ∨ B) = (B ∨ A) := begin apply event.eq, simp [set.union_comm], end lemma Pr_le_eor_left {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[A] ≤ Pr[A ∨ B] := begin apply event_prob_mono2, simp, end lemma Pr_le_eor_right {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[B] ≤ Pr[A ∨ B] := begin rw eor_comm, apply Pr_le_eor_left, end lemma Pr_le_eor_sum {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[A ∨ B]≤ Pr[A] + Pr[B] := begin have A1:↑(Pr[A ∨ B])≤ (Pr[A]:ennreal) + (Pr[B]:ennreal), { repeat {rw event_prob_def}, simp, apply measure_theory.outer_measure.union, }, have A2:↑(Pr[A ∨ B])≤ ((Pr[A] + Pr[B]):ennreal) → Pr[A ∨ B]≤ Pr[A] + Pr[B], { apply to_nnreal_almost_monotonic, { rw event_prob_def, apply prob_not_infinite, }, { apply add_finite, rw event_prob_def, apply prob_not_infinite, rw event_prob_def, apply prob_not_infinite, } }, apply A2, apply A1, end lemma Pr_disjoint_eor {Ω:Type*} {p:probability_space Ω} (A B:event p): disjoint A.val B.val → Pr[A ∨ B] = Pr[A] + Pr[B] := begin intro A1, have A2:↑(Pr[A ∨ B])= (Pr[A]:ennreal) + (Pr[B]:ennreal), { repeat {rw event_prob_def}, simp, apply measure_theory.measure_union, apply A1, apply A.property, apply B.property, }, have A3:((Pr[A ∨ B]):ennreal).to_nnreal= ((Pr[A]:ennreal) + (Pr[B]:ennreal)).to_nnreal, { rw A2, }, simp at A3, apply A3, end def enot {Ω:Type*} {p:probability_space Ω} (A:event p):event p := { val:=(A).valᶜ, property := measurable_set.compl A.property, } prefix `¬ₑ` :100 := enot @[simp] lemma enot_val_def {Ω:Type*} {p:probability_space Ω} (A:event p): (¬ₑ A).val = (A.val)ᶜ := begin refl, end /- Double negation elimination. However, it is hard to avoid in measure theory. -/ @[simp] lemma enot_enot_eq_self {Ω:Type*} {p:probability_space Ω} (A:event p): (¬ₑ (¬ₑ A)) = (A) := begin apply event.eq, simp, end instance measurable_setB_has_compl {α:Type*} [M:measurable_space α]:has_compl (@measurable_setB α M) := { compl := λ E, ⟨ E.valᶜ, measurable_set.compl E.property⟩, } instance has_sdiff.measurable_setB {α:Type*} {M:measurable_space α}: has_sdiff (measurable_setB M) := ⟨λ E F, E ∩ Fᶜ⟩ instance has_sdiff.event {α:Type*} {M:probability_space α}: has_sdiff (event M) := ⟨λ E F, E ∧ ¬ₑ F⟩ @[simp] lemma has_sdiff_measurable_setB_val {α:Type*} {M:measurable_space α} (E F:measurable_setB M): (E \ F).val = E.val \ F.val := rfl @[simp] lemma has_sdiff_event_val {α:Type*} {P:probability_space α} (E F:event P): (E \ F).val = E.val \ F.val := rfl instance measurable_setB_subtype_has_neg {α:Type*} [M:measurable_space α]:has_neg (subtype (@measurable_set α M)) := { neg := λ E, ⟨ E.valᶜ, measurable_set.compl E.property⟩, } lemma measurable_setB_neg_def {α:Type*} [M:measurable_space α] {E:@measurable_setB α M}: Eᶜ = ⟨ E.valᶜ, measurable_set.compl E.property⟩ :=rfl @[simp] lemma measurable_setB_compl_val_def {α:Type*} [M:measurable_space α] {E:@measurable_setB α M}: (Eᶜ).val = (E.val)ᶜ :=rfl instance event_has_compl {α:Type*} [M:probability_space α]:has_compl (@event α M) := { compl := λ E, ⟨E.valᶜ, measurable_set.compl E.property⟩, } lemma event_neg_def {α:Type*} [M:probability_space α] {E:@event α M}: Eᶜ = ⟨ E.valᶜ, measurable_set.compl E.property⟩ :=rfl @[simp] lemma event_neg_val_def {α:Type*} [M:probability_space α] {E:@event α M}: (Eᶜ).val = (E.val)ᶜ := rfl @[simp] lemma em_event {Ω:Type*} {p:probability_space Ω} (A:event p): (A ∨ (¬ₑ A))=event_univ := begin apply event.eq, simp, end lemma compl_eor_eq_compl_eand_compl {Ω:Type*} {p:probability_space Ω} (A B:event p): (A ∨ B)ᶜ = (Aᶜ ∧ Bᶜ) := begin apply event.eq, simp, end lemma Pr_add_enot_eq_1 {Ω:Type*} {p:probability_space Ω} (A:event p): Pr[A] + Pr[¬ₑ A] = 1 := begin have A1:disjoint (A.val) (enot A).val, { unfold disjoint, rw enot_val_def, simp, }, have A2:(A∨ (¬ₑ A)) = event_univ, { apply em_event, }, have A3:Pr[A∨ (¬ₑ A)] = Pr[event_univ], { rw A2, }, rw Pr_event_univ at A3, rw Pr_disjoint_eor at A3, apply A3, apply A1, end lemma Pr_one_minus_eq_not {Ω:Type*} {p:probability_space Ω} (A:event p): 1 - Pr[A] = Pr[¬ₑ A] := begin apply nnreal_add_sub_left, apply Pr_add_enot_eq_1, end lemma Pr_one_minus_not_eq {Ω:Type*} {p:probability_space Ω} (A:event p): 1 - Pr[enot A] = Pr[A] := begin apply nnreal_add_sub_right, apply Pr_add_enot_eq_1, end lemma Pr_not_ge_of_Pr_le {Ω:Type*} {p:probability_space Ω} (A:event p) (δ:nnreal): Pr[A] ≤ δ → Pr[¬ₑ A] ≥ 1 - δ := begin intros h1, rw ← Pr_one_minus_eq_not, simp, --apply nnreal.le_s have h2:1 - Pr[A] + Pr[A] ≤ 1 - Pr[A] + δ, { apply add_le_add, apply le_refl _, apply h1 }, apply le_trans _ h2, apply nnreal.le_sub_add', end lemma em_event_cond {Ω:Type*} {p:probability_space Ω} (A B:event p): ((A ∧ B) ∨ (A ∧ ¬ₑ B)) = A := begin apply event.eq, simp [set.inter_union_compl], end lemma Pr_em {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[A ∧ B] + Pr[A ∧ ¬ₑ B] = Pr[A] := begin rw ← Pr_disjoint_eor, { --Pr[(A∧ B)∨ A∧ ¬ₑ B] = Pr[A] rw em_event_cond, }, { --disjoint ((A∧ B).val) ((A∧ ¬ₑ B).val) simp [set.disjoint_inter_compl], } end lemma Pr_diff {Ω:Type*} {p:probability_space Ω} (A B:event p): A.val ⊆ B.val → Pr[B ∧ ¬ₑ A] = Pr[B] - Pr[A] := begin intro A1, have A2:Pr[B ∧ A] + Pr[B ∧ ¬ₑ A] = Pr[B], { apply Pr_em, }, have A3:(B ∧ A) = A, { apply eand_eq_self_of_subset_right, apply A1, }, rw A3 at A2, symmetry, apply nnreal_add_sub_left, exact A2, end def measurable_setB.sdiff {Ω:Type*} {M:measurable_space Ω} (A B:measurable_setB M):measurable_setB M := @measurable_setB.mk _ _ (A.val \ B.val) begin apply measurable_set.diff, apply A.property, apply B.property end instance measurable_setB.has_sdiff {Ω:Type*} {M:measurable_space Ω} :has_sdiff (measurable_setB M) := ⟨measurable_setB.sdiff⟩ @[simp] lemma measurable_setB.sdiff_val_def {Ω:Type*} {M:measurable_space Ω} (A B:measurable_setB M): (A \ B).val = A.val \ B.val := rfl def measurable_setB.symm_diff {Ω:Type*} {M:measurable_space Ω} (A B:measurable_setB M):measurable_setB M := (A \ B) ∪ (B \ A) instance measurable_setB.has_symm_diff {Ω:Type*} {M:measurable_space Ω}:has_symm_diff (measurable_setB M) := ⟨measurable_setB.symm_diff⟩ lemma measurable_setB.has_symm_diff.def {Ω : Type*} {M:measurable_space Ω} {A B:measurable_setB M}:A ∆ B = (A \ B) ∪ (B \ A) := rfl @[simp] lemma measurable_setB.symm_diff_val_def {Ω:Type*} {M:measurable_space Ω} (A B:measurable_setB M): (A ∆ B).val = A.val ∆ B.val := rfl def event_eqv {Ω:Type*} {p:probability_space Ω} (A B:event p):event p := (A ∧ B) ∨ ((¬ₑ A) ∧ (¬ₑ B)) infixr `=ₑ`:100 := event_eqv lemma event_eqv_def {Ω:Type*} {p:probability_space Ω} (A B:event p): (A =ₑ B) = ((A ∧ B) ∨ ((¬ₑ A) ∧ (¬ₑ B))) := rfl lemma eor_partition {Ω:Type*} {p:probability_space Ω} (A B:event p): (A ∨ B) = ((A ∧ ¬ₑ B) ∨ (A ∧ B) ∨ (¬ₑ A ∧ B)) := begin apply event.eq, simp, ext ω,split;intros A1;simp at A1;simp [A1], { cases A1 with A1 A1; simp [A1], rw or_comm, apply classical.em, apply classical.em, }, { cases A1 with A1 A1, simp [A1], cases A1 with A1 A1, simp [A1], simp [A1], }, end lemma Pr_eor_partition {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[A ∨ B] = Pr[A ∧ ¬ₑ B] + Pr[A ∧ B] + Pr[¬ₑ A ∧ B] := begin rw eor_partition A B, rw Pr_disjoint_eor, rw Pr_disjoint_eor, ring, simp, rw set.disjoint_left, intros ω A1, simp at A1, simp [A1], simp, split; {rw set.disjoint_left, intros ω A1, simp at A1, simp [A1]}, end lemma Pr_eor_plus_eand {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[A ∨ B] + Pr[A ∧ B] = (Pr[A] + Pr[B]) := begin rw ← Pr_em A B, rw ← Pr_em B A, rw eand_comm B A, rw eand_comm B (¬ₑ A), rw Pr_eor_partition A B, ring, end lemma Pr_eor_eq_minus_eand {Ω:Type*} {p:probability_space Ω} (A B:event p): Pr[A ∨ B] = (Pr[A] + Pr[B]) - Pr[A ∧ B] := begin rw ← Pr_eor_plus_eand, rw nnreal.add_sub_cancel, end lemma Pr_eor_eq_minus_eand_real {Ω:Type*} {p:probability_space Ω} (A B:event p): (Pr[A ∨ B]:real) = (Pr[A]:real) + (Pr[B]:real) - (Pr[A ∧ B]:real) := begin have A1:Pr[A ∨ B] + Pr[A ∧ B] = (Pr[A] + Pr[B]), {apply Pr_eor_plus_eand}, rw ← nnreal.coe_eq at A1, repeat {rw nnreal.coe_add at A1}, linarith, end def measurable_setB.Inter {Ω β:Type*} {M:measurable_space Ω} [encodable β] (A:β → measurable_setB M):measurable_setB M := { val:=(⋂ b:β, (A b).val), property := measurable_set.Inter (λ b:β, (A b).property), } --lemma compl_eor_eq_compl_and_compl --Rewrite to use measurable_setB.Inter def eall_encodable {Ω β:Type*} {p:probability_space Ω} [encodable β] (A:β → event p):event p := { val:=(⋂ b:β, (A b).val), property := measurable_set.Inter (λ b:β, (A b).property), } --Redundant to eall_encodable. def eall' {Ω β:Type*} {p:probability_space Ω} [encodable β] (A:β → event p):event p := { val:=(⋂ b:β, (A b).val), property := measurable_set.Inter (λ b:β, (A b).property), } /- The definition of has_eall.eall_val enforces that the eall function implements intersection. The proof of measurability is left to the implementer. -/ @[class] structure has_eall (Ω β:Type*) (p:probability_space Ω) := (eall:(β → event p) → event p) (eall_val:∀ (f:β → event p), (⋂ (b:β), (f b).val) = (eall f).val) -- ∀ᵣ is enforced to be intersection. notation `∀ᵣ` binders `, ` r:(scoped f, has_eall.eall f) := r @[class] structure has_eall_in (Ω β γ:Type*) (p:probability_space Ω) := (eall_in:γ → (β → event p) → event p) (as_set:γ → (set β)) (eall_in_val:∀ (g:γ) (f:β → event p), (⋂ b ∈ (as_set g), (f b).val) = (eall_in g f).val) --#check has_eall_in.has_mem' --TODO:Delete. class has_eall_in' (Ω β γ:Type*) (p:probability_space Ω) := { eall_in:γ → (β → event p) → event p } notation `∀ᵣ` binders ` in ` A, r:(scoped f, has_eall_in.eall_in A f) := r instance has_eall_encodable {Ω β:Type*} {p:probability_space Ω} [encodable β]:has_eall Ω β p := { eall := λ (A:β → event p), eall_encodable A, eall_val := begin simp [eall_encodable], end, } --Instead of a one-off, there should be variants for a variety of types. notation `∀ᵣ` binders `, ` r:(scoped f, has_eall.eall f) := r @[simp] lemma eall_val_def {Ω β:Type*} {p:probability_space Ω} [encodable β] (A:β → event p): (eall_encodable A).val = (⋂ b:β, (A b).val) := rfl lemma eall_binder_def {Ω β:Type*} {p:probability_space Ω} [encodable β] (A:β → event p): (∀ᵣ x, A x) = (eall_encodable A):= rfl @[simp] lemma eall_binder_val_def {Ω β:Type*} {p:probability_space Ω} [encodable β] (A:β → event p): (∀ᵣ x, A x).val = (⋂ b:β, (A b).val) := rfl def eall_prop {Ω β:Type*} {p:probability_space Ω} [E:encodable β] (P:β → Prop) [D:decidable_pred P] (A:β → event p):event p := @eall_encodable _ _ _ (@encodable.subtype β P E D) (λ (b:(subtype P)), A (b.val) ) def to_set_of_sets {Ω:Type*} {p:probability_space Ω} (A:set (event p)):set (set Ω) := (set.image (λ a:event p, a.val) A) lemma all_measurable_to_set_of_sets {Ω:Type*} {p:probability_space Ω} (A:set (event p)) (a∈ (to_set_of_sets A)):measurable_set a := begin unfold to_set_of_sets at H, simp at H, cases H with x H, cases H with A1 A2, subst a, exact x.property, end lemma countable_to_set_of_sets {Ω:Type*} {p:probability_space Ω} {A:set (event p)}: (set.countable A)→ (set.countable (to_set_of_sets A)) := begin unfold to_set_of_sets, intro A1, apply set.countable.image, apply A1, end def eall_set {Ω:Type*} {p:probability_space Ω} (A:set (event p)) (cA:set.countable A):event p:= { val:=set.sInter (to_set_of_sets A), property:=measurable_set.sInter (countable_to_set_of_sets cA) (all_measurable_to_set_of_sets A), } def eall_finset_val {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p):set Ω := ⋂ s∈ S, (A s).val lemma eall_finset_val_measurable {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p):measurable_set (eall_finset_val S A) := begin unfold eall_finset_val, apply finset_inter_measurable, intros, apply (A t).property, end -- def eall_finset {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p):event p := { val:=eall_finset_val S A, property:=eall_finset_val_measurable S A, } instance has_eall_in.finset {Ω β:Type*} {p:probability_space Ω}:has_eall_in Ω β (finset β) p := { eall_in := (λ S f, eall_finset S f), as_set := (λ (S:finset β), ↑S), eall_in_val := begin simp [eall_finset, eall_finset_val], end } @[simp] lemma eall_finset_val_def {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p):(eall_finset S A).val = ⋂ s∈ S, (A s).val := rfl lemma has_eall_in_finset_def {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p): (∀ᵣ s in S, A s) = (eall_finset S A) := rfl @[simp] lemma has_eall_in_finset_val_def {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p): (∀ᵣ s in S, A s).val = ⋂ s∈ S, (A s).val := rfl @[simp] lemma has_eall_in_finset_val_def2 {Ω β:Type*} {p:probability_space Ω} {S:finset β} {A:β → event p}: (has_eall_in.eall_in S A).val = ⋂ s∈ S, (A s).val := rfl --#print instances has_coe @[simp] lemma has_eall_in_finset_val_def3 {Ω β:Type*} {p:probability_space Ω} {S:finset β} {A:β → event p}: @has_coe.coe (event p) (set Ω) (coe_subtype) (has_eall_in.eall_in S A) = ⋂ s∈ S, (A s).val := rfl lemma has_eall_in_insert {Ω β:Type*} {p:probability_space Ω} [decidable_eq β] {T:finset β} {b:β} {E:β → event p}: (∀ᵣ b' in (insert b T), E b') = ((E b) ∧ (∀ᵣ b' in T, E b')) := begin apply event.eq, simp, end /--Since a fintype is encodable, this could be represented with eall, and then proven equivalent to eall_finset. -/ def eall_fintype {Ω β:Type*} {p:probability_space Ω} (F:fintype β) (A:β → event p):event p := eall_finset finset.univ A instance has_eall.fintype {Ω β:Type*} {p:probability_space Ω} [F:fintype β]:has_eall Ω β p := { eall := (λ A, eall_fintype F A), eall_val := by simp [eall_fintype], } lemma eall_fintype_eq_eall_finset {Ω β:Type*} {p:probability_space Ω} [F:fintype β] (A:β → event p):(∀ᵣ b, A b) = eall_finset finset.univ A := rfl lemma eall_fintype_def {Ω β:Type*} {p:probability_space Ω} (F:fintype β) {A:β → event p}: (eall_fintype F A) = (∀ᵣ b, A b) := rfl @[simp] lemma eall_fintype_val_def {Ω β:Type*} {p:probability_space Ω} (F:fintype β) (A:β → event p):(eall_fintype F A).val = ⋂ (s:β), (A s).val := begin unfold eall_fintype, simp, end def measurable_Union {Ω β:Type*} {p:measurable_space Ω} [encodable β] (A:β → measurable_setB p): measurable_setB p := { val:=(⋃ b:β, (A b).val), property := measurable_set.Union (λ b:β, (A b).property), } @[simp] lemma measurable_Union_val_def {Ω β:Type*} {p:measurable_space Ω} [E:encodable β] (A:β → measurable_setB p): (@measurable_Union Ω β p E A).val = (⋃ b:β, (A b).val) := rfl def eany {Ω β:Type*} {p:probability_space Ω} [encodable β] (A:β → event p):event p := measurable_Union A lemma measurable_Union_eq_any {Ω β:Type*} {p:probability_space Ω} [E:encodable β] (A:β → event p): measurable_Union A = eany A := rfl lemma sum_subst {β:Type*} [encodable β] {f g:β → ennreal}:(f = g) → (tsum f) = (tsum g) := begin intro A1, rw A1, end lemma Pr_measurable_Union_sum_dummy {Ω β:Type*} [M:probability_space Ω] [E:encodable β] (A:β → set Ω):(∀ (i j:β), i ≠ j → (A i ∩ A j = ∅))→ (∀ i, measurable_set (A i)) → ((@measure_theory.measure_space.volume Ω (probability_space.to_measure_space)) (⋃ (n:β), A n)) = (∑' (i:β), (@measure_theory.measure_space.volume Ω (probability_space.to_measure_space)) (A i)) := begin intros A1 A3, rw measure_theory.measure_Union, { intros i j A2, simp, unfold disjoint function.on_fun, simp, rw subset_empty_iff, apply A1 i j A2, }, { apply A3, }, end lemma measure_eq_measure {Ω:Type*} [P:probability_space Ω] {S:set Ω}: measure_theory.measure_space.volume.to_outer_measure.measure_of S = (@measure_theory.measure_space.volume Ω (probability_space.to_measure_space)) S := rfl @[simp] lemma eany_val_def {Ω β:Type*} {p:probability_space Ω} [encodable β] (A:β → event p):(eany A).val=(⋃ b:β, (A b).val) := rfl @[class] structure has_eany (Ω β:Type*) (p:probability_space Ω) := (eany:(β → event p) → event p) (eany_val:(∀ (f:β → event p), ((⋃ b, (f b).val) = (eany f).val))) notation `∃ᵣ ` binders `, ` r:(scoped f, has_eany.eany f) := r @[class] structure has_eany_in (Ω β γ:Type*) (p:probability_space Ω) := (eany_in:γ → (β → event p) → event p) (as_set:γ → (set β)) (eany_in_val:∀ (g:γ) (f:β → event p), (⋃ b ∈ (as_set g), (f b).val) = (eany_in g f).val) notation `∃ᵣ ` binders ` in ` S `, ` r:(scoped f, has_eany_in.eany_in S f) := r instance has_eany.encodable {Ω β:Type*} {p:probability_space Ω} [E:encodable β]:has_eany Ω β p := { eany := (λ A:β → (event p), eany A), eany_val := by simp } lemma eany_encodable_notation_def {Ω β:Type*} {p:probability_space Ω} [encodable β] (A:β → event p):(∃ᵣ a, A a) = (eany A) := rfl @[simp] lemma eany_encodable_val_def {Ω β:Type*} {p:probability_space Ω} [encodable β] (A:β → event p):(∃ᵣ a, A a).val = (⋃ (b:β), (A b).val) := begin rw eany_encodable_notation_def, refl end def eany_finset_val {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p):set Ω := ⋃ s∈ S, (A s).val lemma eany_finset_val_measurable {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p):measurable_set (eany_finset_val S A) := begin unfold eany_finset_val, apply finset_union_measurable, intros, apply (A t).property, end def eany_finset {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p):event p := { val:=eany_finset_val S A, property:=eany_finset_val_measurable S A, } instance has_eany_in.finset {Ω β:Type*} {p:probability_space Ω}:has_eany_in Ω β (finset β) p := { eany_in := (λ (S:finset β) (A:β → (event p)), eany_finset S A), as_set := (λ (S:finset β), ↑S), eany_in_val := begin simp [eany_finset, eany_finset_val], end } @[simp] lemma eany_finset_val_def {Ω β:Type*} {p:probability_space Ω} (S:finset β) (A:β → event p):(eany_finset S A).val = ⋃ s∈ S, (A s).val := rfl lemma eany_in_finset_def {Ω β:Type*} {p:probability_space Ω} {S:finset β} (A:β → event p): (∃ᵣ s in S, A s) = eany_finset S A := rfl @[simp] lemma eany_in_finset_val_def {Ω β:Type*} {p:probability_space Ω} {S:finset β} (A:β → event p): (∃ᵣ s in S, A s).val = ⋃ s∈ S, (A s).val := rfl def eany_fintype {Ω β:Type*} {p:probability_space Ω} (F:fintype β) (A:β → event p):event p := eany_finset finset.univ A lemma eany_fintype_def {Ω β:Type*} {p:probability_space Ω} (F:fintype β) (A:β → event p):eany_fintype F A = eany_finset finset.univ A := rfl instance has_eany.fintype {Ω β:Type*} {p:probability_space Ω} [F:fintype β]:has_eany Ω β p := { eany := (λ (A:β → (event p)), eany_fintype F A), eany_val := by simp [eany_fintype], } lemma has_eany_fintype_def {Ω β:Type*} {p:probability_space Ω} [F:fintype β] {A:β→ event p}: (∃ᵣ s, A s) = (eany_fintype F A) := rfl @[simp] lemma has_eany_fintype_val_def {Ω β:Type*} {p:probability_space Ω} [F:fintype β] {A:β→ event p}: (∃ᵣ s, A s).val = ⋃ (s:β), (A s).val := begin rw [has_eany_fintype_def,eany_fintype_def], simp, end lemma eany_eq_eany_fintype {Ω β:Type*} {p:probability_space Ω} (F:fintype β) (E:encodable β) (A:β → event p): eany A = eany_fintype F A := begin apply event.eq, rw ← has_eany_fintype_def, simp, end @[simp] lemma exists_empty {α Ω:Type*} {P:probability_space Ω} (f:α → event P): (∃ᵣ a in (∅:finset α), f a) = (∅:event P) := begin apply event.eq, simp, end @[simp] lemma eall_finset_empty {Ω β:Type*} {p:probability_space Ω} (A:β → event p): (∀ᵣ s in (∅:finset β), A s) = event_univ := begin apply event.eq, simp, end lemma eany_finset_insert {Ω β:Type*} [D:decidable_eq β] {p:probability_space Ω} {S:finset β} {A:β → event p} {a:β}: (∃ᵣ (a':β) in (insert a S), A a') = ((A a) ∨ (∃ᵣ a' in S, A a')) := begin apply event.eq, simp, end lemma eall_finset_insert {Ω β:Type*} [D:decidable_eq β] {p:probability_space Ω} {S:finset β} {A:β → event p} {a:β}: (∀ᵣ (a':β) in (insert a S), A a') = ((A a) ∧ (∀ᵣ a' in S, A a')) := begin apply event.eq, simp, end lemma eany_finset_bound {Ω β:Type*} [D:decidable_eq β] {p:probability_space Ω} (S:finset β) (A:β → event p):Pr[∃ᵣ a in S, A a] ≤ finset.sum S (λ a:β, Pr[A a]) := begin apply finset.induction_on S, { simp, }, { intros a S2 A1 A2, rw finset.sum_insert A1, rw eany_finset_insert, apply le_trans, apply Pr_le_eor_sum, apply add_le_add_left, apply A2, } end lemma eany_fintype_bound {Ω β:Type*} [D:decidable_eq β] {p:probability_space Ω} [F:fintype β] (A:β → event p):Pr[∃ᵣ (s:β), A s] ≤ ∑' a:β, Pr[A a] := begin rw tsum_fintype, apply eany_finset_bound, end lemma eany_fintype_bound2 {Ω β:Type*} {p:probability_space Ω} (F:fintype β) (A:β → event p) (k:nnreal): (∀ a:β, Pr[A a]≤ k) → Pr[∃ᵣ (s:β), A s] ≤ (fintype.card β) * k := begin intro A1, have A2:decidable_eq β := classical.decidable_eq β, apply le_trans, apply @eany_fintype_bound Ω β A2, rw tsum_fintype, unfold fintype.card, apply @finset_sum_le_const β A2, intros s A3, apply A1, end def independent_event_pair {Ω:Type*} {p:probability_space Ω} (A B:event p):Prop := --(event_prob (eand A B)) = (event_prob A) * (event_prob B) Pr[ A ∧ B] = Pr[A] * Pr[B] def independent_events {Ω β:Type*} {p:probability_space Ω} (A:β → event p):Prop := ∀ (S:finset β), (finset.prod S (λ b, Pr[A b])) = Pr[∀ᵣ s in S, A s] def events_IID {Ω β:Type*} {p:probability_space Ω} (A:β → event p):Prop := independent_events A ∧ (∀ x y:β, Pr[A x] = Pr[A y]) lemma events_IID_pow {α : Type*} {p : probability_space α} {β : Type*} [I:inhabited β] (A:β → event p) (S:finset β): events_IID A → Pr[eall_finset S A] = Pr[A I.default]^(S.card) := begin intros A1, unfold events_IID at A1, cases A1 with A2 A3, unfold independent_events at A2, have A4:(finset.prod S (λ b, Pr[A b])) = Pr[eall_finset S A], { apply A2, }, rw ← A4, have A5:(λ (b : β), Pr[A b]) = (λ (b:β), Pr[A (inhabited.default β)]), { ext b, rw A3, }, rw A5, apply finset.prod_const, end @[simp] lemma forall_fintype_val {α Ω:Type*} {P:probability_space Ω} (f:α → event P) [F:fintype α]: (∀ᵣ a, f a).val = ⋂ (a:α), (f a).val := begin rw ← eall_fintype_def, simp, end lemma exists_not_eq_not_forall {α Ω:Type*} {P:probability_space Ω} (f:α → event P) {S:finset α}: (∃ᵣ a in S, ¬ₑ(f a)) = ¬ₑ (∀ᵣ a in S, f a) := begin apply event.eq, simp, rw set.Union_eq_comp_Inter_comp, simp, end lemma not_forall_not_eq_exists {α Ω:Type*} {P:probability_space Ω} (f:α → event P) {S:finset α}: ¬ₑ (∀ᵣ a in S, ¬ₑ f a) = (∃ᵣ a in S, f a) := begin apply event.eq, simp, rw set.Union_eq_comp_Inter_comp, simp, end lemma not_forall_not_eq_exists' {α Ω:Type*} {P:probability_space Ω} (f:α → event P) [fintype α]: ¬ₑ (∀ᵣ a, ¬ₑ f a) = (∃ᵣ a, f a) := begin apply event.eq, simp, rw set.Union_eq_comp_Inter_comp, end lemma not_exists_eq_forall_not {α Ω:Type*} {P:probability_space Ω} (f:α → event P) {S:finset α}: ¬ₑ (∃ᵣ a in S, (f a)) = (∀ᵣ a in S, ¬ₑ (f a)) := begin apply event.eq, simp, end @[simp] lemma forall_singleton {α Ω:Type*} {P:probability_space Ω} (f:α → event P) {x:α}: (∀ᵣ a in ({x}:finset α), f a) = f x := begin apply event.eq, simp, end @[simp] lemma exists_singleton {α Ω:Type*} {P:probability_space Ω} (f:α → event P) {x:α}: (∃ᵣ a in ({x}:finset α), f a) = f x := begin apply event.eq, simp, end @[simp] lemma distrib_exists_and {α Ω:Type*} {P:probability_space Ω} (f:α → event P) {S:finset α} {A:event P}: (∃ᵣ a in S, A ∧ (f a)) = (A ∧ (∃ᵣ a in S, f a)) := begin apply event.eq, simp, ext ω,split;intros A1;simp at A1;simp [A1], cases A1 with i A1, simp [A1], apply exists.intro i, simp [A1], end lemma finset.pair_erase {α:Type*} {x y:α} [decidable_eq α]:x ≠ y → ({x, y}:finset α).erase x = {y} := begin intros A1, rw finset.erase_insert, simp [A1], end lemma finset.singleton_erase {α:Type*} {x:α} [decidable_eq α]:({x}:finset α).erase x = ∅ := begin have A1:{x} = insert x (∅:finset α), {simp}, rw A1, rw finset.erase_insert, simp, end lemma finset.union_erase {α:Type*} {S T:finset α} {x:α} [decidable_eq α]: (S ∪ T).erase x = (S.erase x) ∪ (T.erase x) := begin ext a;split;intros A1;simp at A1;simp, {simp [A1]}, {cases A1;simp [A1]}, end lemma finset.image_sum {α β:Type*} [add_comm_monoid β] [decidable_eq α] {S:finset α} {f:α → β} {g:α → α}: (∀ (s t:α),s∈ S → t∈ S→ s ≠ t → g s ≠ g t) → (finset.image g S).sum f = S.sum (f ∘ g) := begin apply finset.induction_on S, { intros A1, refl, }, { intros a s B1 B2 B3, simp, rw finset.sum_insert, rw finset.sum_insert, rw B2, intros s' t' B4 B5 B6, apply B3, {simp [B4]}, {simp [B5]}, apply B6, apply B1, intro B4, simp at B4, cases B4 with u B4, apply B3 u a, {simp [B4]}, {simp}, intro B5, subst u, apply B1, {simp [B4]}, {simp [B4]}, }, end -- A specialized lemma of little generic value. lemma finset.powerset_sum {α β:Type*} [add_comm_monoid β][decidable_eq α] {x:α} {S:finset α} (f:finset α → β): (x ∉ S) → ((insert x S).powerset.erase ∅).sum f = (S.powerset.erase ∅).sum f + (S.powerset).sum (f ∘ (insert x)) := begin intros A0, rw finset.powerset_insert, rw finset.union_erase, rw finset.sum_union, have A1:((finset.image (insert x) S.powerset).erase ∅).sum f = (finset.image (insert x) S.powerset).sum f, {have A1A:((finset.image (insert x) S.powerset).erase ∅) = (finset.image (insert x) S.powerset), { rw finset.ext_iff, intros T;split;intros A1A1;simp at A1A1, { simp,cases A1A1 with A1A1 A1A2, cases A1A2 with U A1A2, apply exists.intro U, apply A1A2, }, simp, split, {cases A1A1 with U A1A1,intros A1,subst T,simp at A1A1,apply A1A1}, {apply A1A1}, }, rw A1A, }, rw A1, have B1:(finset.image (insert x) S.powerset).sum f = S.powerset.sum (f ∘ (insert x)), { apply @finset.image_sum (finset α) β, intros s t B1A B1B B1C B1D, apply B1C, simp at B1A, simp at B1B, rw finset.subset_iff at B1B, rw finset.subset_iff at B1A, rw finset.ext_iff at B1D, rw finset.ext_iff, intros a;split;intros B1F, { have B1G:a ∈ insert x s, {simp [B1F] }, have B1H := (B1D a).mp B1G, simp at B1H, cases B1H, subst a, exfalso, apply A0, apply B1A, apply B1F, apply B1H, }, { have B1G:a ∈ insert x t,{simp [B1F]}, have B1H := (B1D a).mpr B1G, simp at B1H, cases B1H, subst a, exfalso, apply A0, apply B1B, apply B1F, apply B1H, }, }, rw B1, rw finset.disjoint_left, intros T A2, simp at A2, simp, intros A3 U A5 A6, subst T, apply A0, cases A2 with A2 A7, rw finset.subset_iff at A7, apply A7, simp, end lemma distrib_forall_eand {α Ω:Type*} {P:probability_space Ω} (f:α → event P) [decidable_eq α] {S:finset α} (A:event P):S.nonempty → (∀ᵣ a in S, A ∧ f a) = (A ∧ (∀ᵣ a in S, f a)) := begin intros A1, apply event.eq, simp,ext ω,split;intros A2;simp at A2;simp [A2], {have A3:=finset.nonempty.bex A1, cases A3 with a A3, have A4 := A2 a A3, simp [A4], intros b A5, apply (A2 b A5).right, }, { apply A2.right, }, end lemma Pr_exists_eq_powerset {α Ω:Type*} {P:probability_space Ω} (f:α → event P) [decidable_eq α] {S:finset α}: (Pr[(∃ᵣ a in S, (f a))]:real) = -(S.powerset.erase ∅).sum (λ T:finset α, (Pr[∀ᵣ a in T, f a]:real) * (-1)^(T.card)) := begin revert f, apply finset.case_strong_induction_on S, {intros f, simp [finset.singleton_erase],}, intros x s A3 A1 f, have A6 := (A1 s (finset.subset.refl s) f), rw finset.powerset_sum, rw eany_finset_insert, rw Pr_eor_eq_minus_eand_real, simp, rw ← distrib_exists_and, rw A6, have A8:=(A1 s (finset.subset.refl s) (λ a, f x∧ f a)), rw A8, have A9: -s.powerset.sum (λ (x_1 : finset α), (Pr[has_eall_in.eall_in (insert x x_1) f]:real) * (-1) ^ (insert x x_1).card) = (Pr[f x]:real) + (s.powerset.erase ∅).sum (λ (T : finset α), (Pr[∀ᵣ (a : α) in T,f x∧ f a]:real) * (-1) ^ T.card), { have A9A:insert ∅ (s.powerset.erase ∅) = (s).powerset, {rw finset.insert_erase, simp}, have A9B: -(s).powerset.sum (λ (x_1 : finset α), (Pr[has_eall_in.eall_in (insert x x_1) f]:real) * (-1) ^ (insert x x_1).card) = -(insert ∅ (s.powerset.erase ∅)).sum (λ (x_1 : finset α), (Pr[has_eall_in.eall_in (insert x x_1) f]:real) * (-1) ^ (insert x x_1).card), {rw A9A}, rw A9B, clear A9A A9B, rw add_comm (Pr[f x]:real), --rw finset.sum_insert, simp, have A9C:-((s).powerset.erase ∅).sum (λ (x_1 : finset α), (Pr[has_eall_in.eall_in (insert x x_1) f]:real) * (-1) ^ (insert x x_1).card) = ((s).powerset.erase ∅).sum ((λ (x_1 : finset α),- (Pr[has_eall_in.eall_in (insert x x_1) f]:real) * (-1) ^ (insert x x_1).card)), {simp}, rw A9C, clear A9C, apply finset.sum_congr, {refl}, intros T A9D, simp at A9D, rw distrib_forall_eand, rw eall_finset_insert, rw finset.card_insert_of_not_mem, rw pow_succ, {simp}, intro A9F, cases A9D with A9D A9E, rw finset.subset_iff at A9E, have A9G := A9E A9F, apply A3, apply A9G, apply finset.nonempty_of_ne_empty, apply A9D.left, }, rw A9, linarith, apply A3, end lemma Pr_all_not_eq_powerset {α Ω:Type*} {P:probability_space Ω} (f:α → event P) [decidable_eq α] {S:finset α}: (Pr[(∀ᵣ a in S, ¬ₑ (f a))]:real) = (S.powerset).sum (λ T:finset α, (Pr[∀ᵣ a in T, f a]:real) * (-1)^(T.card)) := begin --rw Pr_exists_eq_powerset, have A1:(insert ∅ ((S.powerset).erase ∅)).sum (λ T:finset α, (Pr[∀ᵣ a in T, f a]:real) * (-1)^(T.card)) = S.powerset.sum (λ (T : finset α), ↑Pr[∀ᵣ (a : α) in T,f a] * (-1) ^ T.card), { rw finset.insert_erase, simp, }, have A1:∅ ∈ S.powerset, {simp}, rw ← finset.insert_erase A1, rw finset.sum_insert,simp, rw ← not_exists_eq_forall_not, rw ← Pr_one_minus_eq_not, rw nnreal.coe_sub, rw Pr_exists_eq_powerset, repeat {simp}, end lemma independent_events_not_of_independent_events {α Ω:Type*} {P:probability_space Ω} (f:α → event P):independent_events f → independent_events (enot ∘ f) := begin intros A1, unfold independent_events, intros S, haveI A3:=classical.decidable_eq α, apply finset.case_strong_induction_on S, {simp}, intros a s B1 B2, rw ← not_exists_eq_forall_not, have A2:∀ (A:event P), 1 - (Pr[A]:real) = (Pr[¬ₑ A]:real), { intro A,rw ← Pr_one_minus_eq_not, rw nnreal.coe_sub, {simp}, apply Pr_le_one, }, rw ← nnreal.coe_eq, rw ← A2, rw @Pr_exists_eq_powerset α Ω P f A3, unfold independent_events at A1, rw finset.prod_insert, rw finset.powerset_sum, rw B2 s (finset.subset.refl s), rw nnreal.coe_mul, rw ← A2, simp, rw ← @Pr_exists_eq_powerset α Ω P f A3, have A4:∀ x∈ s.powerset, (Pr[has_eall_in.eall_in (insert a x) f]:real) * (-1) ^ (insert a x).card = -(Pr[f a]:real) * ((Pr[has_eall_in.eall_in x f]:real) * (-1) ^ (x).card), {intros x A4A, have A4B:a ∉ x, {simp at A4A,intro A4B,apply B1,apply A4A,apply A4B}, rw ← A1,rw finset.prod_insert A4B, rw A1, rw nnreal.coe_mul, rw finset.card_insert_of_not_mem A4B, rw pow_succ, simp, repeat {rw ← mul_assoc}, }, have A5:s.powerset = s.powerset := rfl, rw finset.sum_congr A5 A4, have A6:s.powerset.sum (λ (x : finset α), -(Pr[f a]:real) * (↑Pr[has_eall_in.eall_in x f] * (-1) ^ x.card)) = -((Pr[f a]:real)) * s.powerset.sum (λ (x : finset α), (↑Pr[has_eall_in.eall_in x f] * (-1) ^ x.card)), {simp,rw finset.sum_distrib_left}, rw A6, rw ← Pr_all_not_eq_powerset, rw ← not_forall_not_eq_exists, rw ← A2, simp, ring, repeat {exact B1}, end lemma events_IID_not_of_events_IID {α Ω:Type*} {P:probability_space Ω} (f:α → event P):events_IID f → events_IID (enot ∘ f) := begin intros A1, unfold events_IID, split, { apply independent_events_not_of_independent_events, unfold events_IID at A1, apply A1.left, }, { unfold events_IID at A1, intros x y, have C2 := A1.right x y, simp, rw ← Pr_one_minus_eq_not, rw ← Pr_one_minus_eq_not, rw C2, }, end lemma events_IID_iff_events_IID_enot {α Ω:Type*} {P:probability_space Ω} (f:α → event P):events_IID f ↔ events_IID (enot ∘ f) := begin split, { apply events_IID_not_of_events_IID, }, { intros A1, have A2:f = enot ∘ (enot ∘ f), { apply funext, intro a, simp }, rw A2, apply events_IID_not_of_events_IID, apply A1 }, end def measurable_fun {α:Type*} {β:Type*} (Mα:measurable_space α) (Mβ:measurable_space β):Type* := subtype (@measurable α β _ _) def random_variable {α:Type*} (p:probability_space α) {β:Type*} (Mβ:measurable_space β):Type* := measurable_fun (probability_space.to_measurable_space α) Mβ infixr ` →ₘ `:80 := measurable_fun infixr ` →ᵣ `:80 := random_variable lemma random_variable_val_eq_coe {Ω α:Type*} {P:probability_space Ω} {M:measurable_space α} (X:P →ᵣ M):X.val = (@coe (subtype (@measurable Ω α _ _)) (Ω → α) _ X) := begin refl end lemma measurable_setB_preimageh {α:Type*} {β:Type*} [Mα:measurable_space α] [Mβ:measurable_space β] (f:measurable_fun Mα Mβ) (S:measurable_setB Mβ):measurable_set (set.preimage (f.val) (S.val)) := begin apply measurable_elim, apply f.property, apply S.property end def measurable_setB_preimage {α:Type*} {β:Type*} [Mα:measurable_space α] [Mβ:measurable_space β] (f:measurable_fun Mα Mβ) (S:measurable_setB Mβ):measurable_setB Mα := { val:= (set.preimage (f.val) (S.val)), property:=measurable_setB_preimageh f S, } def rv_event {α:Type*} {p:probability_space α} {β:Type*} {Mβ:measurable_space β} (X:random_variable p Mβ) (S:measurable_setB Mβ):event p := measurable_setB_preimage X S infixr ` ∈ᵣ `:80 := rv_event --infixr `∈` := rv_event def rv_event_compl {α:Type*} {p:probability_space α} {β:Type*} [Mβ:measurable_space β] (X:random_variable p Mβ) (S:measurable_setB Mβ):event p := ¬ₑ(rv_event X S) infixr `∉ᵣ`:80 := rv_event_compl @[simp] def rv_event_compl_val {α:Type*} {p:probability_space α} {β:Type*} [Mβ:measurable_space β] (X:random_variable p Mβ) (S:measurable_setB Mβ): (rv_event_compl X S).val = (¬ₑ (rv_event X S)).val := rfl @[simp] lemma rv_event_val_def {α:Type*} {p : probability_space α} {β : Type*} [Mβ : measurable_space β] (X:p →ᵣ Mβ) (S:measurable_setB Mβ):(X ∈ᵣ S).val = {a:α|X.val a ∈ S.val} := begin refl end /- Which of these is simpler is subjective. -/ lemma rv_event_compl_preimage {α:Type*} {p: probability_space α} {β:Type*} [Mβ:measurable_space β] (X:random_variable p Mβ) (S:measurable_setB Mβ): (X ∈ᵣ Sᶜ) = (X ∈ᵣ S)ᶜ := rfl lemma rv_event_empty {α:Type*} {p:probability_space α} {β:Type*} [Mβ:measurable_space β] (X:random_variable p Mβ):X ∈ᵣ ∅ = ∅ := begin apply event.eq, rw rv_event_val_def, rw event_empty_val_def2, ext ω;split;intros A1, { exfalso, simp at A1, apply A1, }, { exfalso, apply A1, }, end lemma rv_event_measurable_union {α:Type*} {p:probability_space α} {β:Type*} [Mβ:measurable_space β] (X:random_variable p Mβ) {A B:measurable_setB Mβ}:X ∈ᵣ (measurable_union A B) = ((X ∈ᵣ A) ∨ (X∈ᵣ B)) := begin apply event.eq, repeat {rw rv_event_val_def}, rw eor_val_def, repeat {rw rv_event_val_def}, rw measurable_union_val_def, ext ω;split;intros A1;simp;simp at A1;apply A1 end lemma rv_event_val_def' {α:Type*} {p : probability_space α} {β : Type*} [Mβ : measurable_space β] (X:p →ᵣ Mβ) (S:measurable_setB Mβ) {ω:α}: (ω ∈ ((X ∈ᵣ S)))↔ (X.val ω ∈ S.val) := begin refl end lemma set.mem_Prop_def {α:Type*} {P:α → Prop} {a:α}: (a ∈ {a':α|P a'} )= P a := rfl lemma rv_event_measurable_Union {α:Type*} {p:probability_space α} {β:Type*} [Mβ:measurable_space β] {γ:Type*} [E:encodable γ] (X:random_variable p Mβ) {f:γ → measurable_setB Mβ}:X ∈ᵣ (measurable_Union f) = measurable_Union (λ i, X ∈ᵣ (f i)) := begin apply event.eq, ext ω, rw rv_event_val_def, rw measurable_Union_val_def, rw measurable_Union_val_def, split;intro A1, { rw set.mem_Prop_def at A1, rw set.mem_Union, rw set.mem_Union at A1, cases A1 with i A1, apply exists.intro i, rw @rv_event_val_def α p β Mβ X (f i), rw set.mem_Prop_def, apply A1, }, { rw set.mem_Prop_def, rw set.mem_Union, rw set.mem_Union at A1, cases A1 with i A1, rw @rv_event_val_def α p β Mβ X (f i) at A1, apply exists.intro i, apply A1, }, end lemma Pr_compl_ge_of_Pr_le {Ω:Type*} {p:probability_space Ω} (A:event p) (δ:nnreal): Pr[A] ≤ δ → Pr[Aᶜ] ≥ 1 - δ := begin intros h1, apply Pr_not_ge_of_Pr_le, apply h1, end --@[simp] lemma event_simp_def {α:Type*} [p:probability_space α] {X:set α} {H:measurable_set X}: (subtype.mk X H).val = X := rfl --@[simp] lemma measurable_setB_simp_def {α:Type*} [p:measurable_space α] {X:set α} {H:measurable_set X}: (subtype.mk X H).val = X := rfl lemma pr_comp_event {Ω:Type*} {p:probability_space Ω} {X:p →ᵣ borel real} {E:@measurable_setB ℝ (borel ℝ)}: (X ∈ᵣ (Eᶜ)) = (X ∈ᵣ E)ᶜ := begin apply event.eq, simp, refl, end lemma rv_event_compl' {Ω:Type*} {MΩ:probability_space Ω} {β:Type*} [Mβ:measurable_space β] (X:MΩ →ᵣ Mβ) (S:measurable_setB Mβ):(X ∈ᵣ (Sᶜ)) = (rv_event X S)ᶜ := begin apply event.eq, simp, refl, end lemma neg_eq_not {Ω:Type*} {p:probability_space Ω} (A:event p): Aᶜ = ¬ₑ A := begin apply event.eq, simp, end /-def random_variable_identical {α:Type*} {p:probability_space α} {β:Type*} [Mβ:measurable_space β] (X Y:random_variable p Mβ):Prop := ∀ (S:measurable_setB Mβ), Pr[X ∈ᵣ S] = Pr[Y ∈ᵣ S]-/ def random_variable_identical {α α':Type*} {p:probability_space α} {p':probability_space α'} {β:Type*} {Mβ:measurable_space β} (X:random_variable p Mβ) (Y:random_variable p' Mβ):Prop := ∀ (S:measurable_setB Mβ), Pr[X ∈ᵣ S] = Pr[Y ∈ᵣ S] def random_variable_independent_pair {α:Type*} {p:probability_space α} {β:Type*} {Mβ:measurable_space β} {γ:Type*} {Mγ:measurable_space γ} (X:p →ᵣ Mβ) (Y:p →ᵣ Mγ):Prop := ∀ (S:measurable_setB Mβ) (T:measurable_setB Mγ), independent_event_pair (X ∈ᵣ S) (Y ∈ᵣ T) def random_variable_independent {α:Type*} {p:probability_space α} {β:Type*} {γ:β → Type*} {Mγ:Π b, measurable_space (γ b)} (X:Π b, random_variable p (Mγ b)):Prop := ∀ (S:Π b, measurable_setB (Mγ b)), independent_events (λ b:β, ((X b) ∈ᵣ (S b))) def random_variables_IID {α:Type*} {p:probability_space α} {β:Type*} {γ:Type*} {Mγ:measurable_space γ} (X:β → random_variable p Mγ):Prop := random_variable_independent X ∧ ∀ (i j:β), random_variable_identical (X i) (X j) /- There are a lot of types where everything is measurable. This is equivalent to ⊤. -/ class top_measurable (α:Type*) extends measurable_space α := (all_measurable:∀ S:set α,measurable_set S) def make_top_measurable_space (α:Type*) :top_measurable α := { to_measurable_space := ⊤, all_measurable := begin intros S, apply measurable_space.measurable_set_top, end, } instance top_measurable.has_coe_measurable_space (α:Type*):has_coe (top_measurable α) (measurable_space α) := { coe := λ TM, @top_measurable.to_measurable_space α TM } instance bool_top_measurable:top_measurable bool := { all_measurable:=@measurable_space.measurable_set_top bool, ..bool.measurable_space } instance int_top_measurable:top_measurable ℤ := { all_measurable:=@measurable_space.measurable_set_top ℤ, ..int.measurable_space } /- In a top measurable space (e.g. bool, ℕ, ℤ, et cetera), everything is measurable. So, we can make an event from everything. -/ def measurable_setB_top {β:Type*} [M:top_measurable β] (S:set β): (@measurable_setB β M.to_measurable_space) := { val := S, property := top_measurable.all_measurable S, } def measurable_setB_top' {β:Type*} (S:set β): (@measurable_setB β (⊤:measurable_space β)) := { val := S, property := begin apply measurable_space.measurable_set_top, end, } @[simp] lemma measurable_setB_top_val {β:Type*} [M:top_measurable β] (S:set β): (measurable_setB_top S).val = S := rfl lemma fun_top_measurable {β γ:Type*} [M:top_measurable β] [Mγ:measurable_space γ] {f:β → γ}: measurable f := begin intros S A1, apply top_measurable.all_measurable, end def top_measurable_fun {β γ:Type*} [M:top_measurable β] (f:β → γ) (Mγ:measurable_space γ): (@top_measurable.to_measurable_space β M) →ₘ Mγ := { val := f, property := fun_top_measurable } def rv_top_event {α:Type*} {p:probability_space α} {β:Type*} [Mβ:top_measurable β] (X:random_variable p Mβ.to_measurable_space) (S:set β):event p := rv_event X (measurable_setB_top S) --To apply this directly to a set, it has to be top measurable. infixr ` ∈t `:80 := rv_top_event lemma rv_top_event_val_def {α:Type*} {p:probability_space α} {β:Type*} [Mβ:top_measurable β] (X:random_variable p Mβ.to_measurable_space) (S:set β): (rv_top_event X S).val = {a:α|X.val a ∈ S} := rfl lemma compose_measurable_fun_measurable2 {α β γ:Type*} [Mα:measurable_space α] [Mβ:measurable_space β] [Mγ:measurable_space γ] (X:measurable_fun Mβ Mγ) (Y:measurable_fun Mα Mβ):measurable (X.val ∘ Y.val) := begin apply measurable_intro, intros B a, have A1:(X.val ∘ Y.val ⁻¹' B)=(Y.val ⁻¹' (X.val ⁻¹' B)), { refl, }, rw A1, apply measurable_elim Y.val _ Y.property, apply measurable_elim X.val _ X.property, apply a end def compose_measurable_fun {α β γ:Type*} {Mα:measurable_space α} {Mβ:measurable_space β} {Mγ:measurable_space γ} (X:measurable_fun Mβ Mγ) (Y:measurable_fun Mα Mβ):(measurable_fun Mα Mγ) := { val := X.val ∘ Y.val, property := compose_measurable_fun_measurable2 X Y } infixr ` ∘m `:80 := compose_measurable_fun @[simp] lemma compose_measurable_fun_val_def {Ω : Type*} {β : Type*} {γ : Type*} [MΩ : measurable_space Ω] [Mβ : measurable_space β] [Mγ : measurable_space γ] (Y:Mβ →ₘ Mγ) (X:MΩ →ₘ Mβ): (Y ∘m X).val = (λ ω:Ω, Y.val (X.val ω)) := begin refl end def rv_compose {α : Type*} {β : Type*} {γ : Type*} {p : probability_space α} {Mβ : measurable_space β} {Mγ : measurable_space γ} (X:measurable_fun Mβ Mγ) (Y:random_variable p Mβ):random_variable p Mγ := compose_measurable_fun X Y infixr ` ∘r `:80 := rv_compose @[simp] lemma rv_compose_val_def {Ω : Type*} {β : Type*} {γ : Type*} [Mβ : measurable_space β] [Mγ : measurable_space γ] {p : probability_space Ω} (Y:Mβ →ₘ Mγ) (X:p →ᵣ Mβ): (Y ∘r X).val = (λ ω:Ω, Y.val (X.val ω)) := begin refl end def prod_space {α β:Type*} (Mα:measurable_space α) (Mβ:measurable_space β):=(@prod.measurable_space α β Mα Mβ) infixr ` ×ₘ `:80 := prod_space def measurable_setB.prod {α β:Type*} {Mα:measurable_space α} {Mβ:measurable_space β} (A:measurable_setB Mα) (B:measurable_setB Mβ):measurable_setB (Mα ×ₘ Mβ) := @measurable_setB.mk (α × β) (Mα ×ₘ Mβ) (A.val.prod B.val) begin apply measurable_set.prod, apply A.property, apply B.property, end @[simp] lemma measurable_setB.prod_val {α β:Type*} {Mα:measurable_space α} {Mβ:measurable_space β} (A:measurable_setB Mα) (B:measurable_setB Mβ):(A.prod B).val = (A.val).prod (B.val) := rfl def mf_fst {α β:Type*} {Mα:measurable_space α} {Mβ:measurable_space β}:measurable_fun (Mα ×ₘ Mβ) Mα := { val:= (λ x:(α × β), x.fst), property := fst_measurable, } @[simp] lemma mf_fst_val {α β:Type*} {Mα:measurable_space α} {Mβ:measurable_space β}:(@mf_fst α β Mα Mβ).val = prod.fst := rfl def mf_snd {α β:Type*} {Mα:measurable_space α} {Mβ:measurable_space β}:measurable_fun (prod_space Mα Mβ) Mβ := { val:= (λ x:(α × β), x.snd), property := snd_measurable, } @[simp] lemma mf_snd_val {α β:Type*} {Mα:measurable_space α} {Mβ:measurable_space β}:(@mf_snd α β Mα Mβ).val = prod.snd := rfl def const_measurable_fun {Ω : Type*} [MΩ : measurable_space Ω] {β : Type*} [Mβ : measurable_space β] (c : β):MΩ →ₘ Mβ := { val := (λ (ω : Ω), c), property := const_measurable c, } lemma const_measurable_fun_val_def {Ω : Type*} [MΩ : measurable_space Ω] {β : Type*} [Mβ : measurable_space β] (c : β):(const_measurable_fun c).val = (λ (ω : Ω), c) := rfl def const_random_variable {Ω : Type*} {P:probability_space Ω} {β : Type*} [Mβ : measurable_space β] (c : β):P →ᵣ Mβ := const_measurable_fun c def prod_measurable_fun {α β γ:Type*} {Mα:measurable_space α} {Mβ:measurable_space β} {Mγ:measurable_space γ} (X:measurable_fun Mα Mβ) (Y:measurable_fun Mα Mγ):measurable_fun Mα (Mβ ×ₘ Mγ) := { val := (λ a:α, prod.mk (X.val a) (Y.val a)), property := measurable_fun_product_measurable X.val Y.val X.property Y.property, } lemma prod_measurable_fun_val_def {Ω : Type*} {β : Type*} {γ : Type*} {MΩ : measurable_space Ω} {Mβ : measurable_space β} {Mγ : measurable_space γ} {X:MΩ →ₘ Mβ} {Y:MΩ →ₘ Mγ}: (prod_measurable_fun X Y).val = λ ω:Ω, prod.mk (X.val ω) (Y.val ω) := begin refl end def prod_random_variable {α β γ:Type*} {P:probability_space α} {Mβ:measurable_space β} {Mγ:measurable_space γ} (X:random_variable P Mβ) (Y:random_variable P Mγ):random_variable P (Mβ ×ₘ Mγ) := prod_measurable_fun X Y infixr ` ×ᵣ `:80 := prod_random_variable @[simp] lemma prod_random_variable_val_def {Ω : Type*} {β : Type*} {γ : Type*} {P : probability_space Ω} {Mβ : measurable_space β} {Mγ : measurable_space γ} {X:P →ᵣ Mβ} {Y:P →ᵣ Mγ}: (X ×ᵣ Y).val = λ ω:Ω, prod.mk (X.val ω) (Y.val ω) := begin refl end def prod_measurable_setB {β : Type*} {γ : Type*} {Mβ : measurable_space β} {Mγ : measurable_space γ} (X:measurable_setB Mβ) (Y:measurable_setB Mγ):measurable_setB (Mβ ×ₘ Mγ) := { val := (set.prod X.val Y.val), property := measurable_set_prod' X.property Y.property } @[simp] lemma prod_measurable_setB_val_def {β : Type*} {γ : Type*} {Mβ : measurable_space β} {Mγ : measurable_space γ} (X:measurable_setB Mβ) (Y:measurable_setB Mγ): (prod_measurable_setB X Y).val = set.prod X.val Y.val := rfl class has_measurable_equality {α:Type*} (M:measurable_space α):Prop := (measurable_set_eq:measurable_set {p:α × α|p.fst = p.snd}) def measurable_setB_eq {α:Type*} {M:measurable_space α} [E:has_measurable_equality M] :measurable_setB (M ×ₘ M) := measurable_setB.mk E.measurable_set_eq lemma measurable_setB_eq_val {α:Type*} {M:measurable_space α} [E:has_measurable_equality M]: (@measurable_setB_eq α M E).val = {p:α × α|p.fst = p.snd} := rfl def measurable_setB_ne {α:Type*} {M:measurable_space α} [E:has_measurable_equality M] :measurable_setB (M ×ₘ M) := measurable_setB.mk (measurable_set.compl E.measurable_set_eq) lemma measurable_setB_ne_val {α:Type*} {M:measurable_space α} [E:has_measurable_equality M]: (@measurable_setB_ne α M E).val = {p:α × α|p.fst ≠ p.snd} := rfl lemma diagonal_eq {α:Type*}:{p:α × α|p.fst = p.snd}=⋃ (a:α), set.prod {a} {a} := begin ext,split;intros A1A;simp;simp at A1A, { apply exists.intro x.fst, ext;simp, rw A1A, }, { cases A1A with i A1A, cases A1A, simp, }, end instance measurable_setB_eq_top_measurable {α:Type*} (E:encodable α) (M:top_measurable α):has_measurable_equality (M.to_measurable_space) := { measurable_set_eq := begin rw diagonal_eq, apply measurable_set.Union, intros a, apply measurable_set_prod', repeat {apply M.all_measurable}, end } instance has_measurable_equality.fintype_top {α:Type*} [fintype α] [TM:top_measurable α]: has_measurable_equality (TM.to_measurable_space) := { measurable_set_eq := begin rw diagonal_eq, haveI:encodable α := fintype.encodable α, apply measurable_set.Union, intros b, apply measurable_set.prod, apply TM.all_measurable, apply TM.all_measurable, end } instance measurable_setB_eq_bool:has_measurable_equality bool.measurable_space := { measurable_set_eq := begin rw diagonal_eq, apply measurable_set.Union, intros a, apply measurable_set_prod', repeat {apply measurable_space.measurable_set_top}, end } instance measurable_setB_eq_int:has_measurable_equality int.measurable_space := { measurable_set_eq := begin rw diagonal_eq, apply measurable_set.Union, intros a, apply measurable_set_prod', repeat {apply measurable_space.measurable_set_top}, end } def random_variable_eq {Ω:Type*} {P:probability_space Ω} {α:Type*} [M:measurable_space α] [E:has_measurable_equality M] (X Y:P →ᵣ M):event P := (X ×ᵣ Y) ∈ᵣ (measurable_setB_eq) infixr ` =ᵣ `:100 := random_variable_eq @[simp] lemma rv_eq_val_def {α:Type*} {p : probability_space α} {β : Type*} [Mβ :measurable_space β] [has_measurable_equality Mβ] (X Y:p →ᵣ Mβ): (X =ᵣ Y).val = {a:α| X.val a = Y.val a} := begin unfold random_variable_eq, rw rv_event_val_def, rw prod_random_variable_val_def, rw measurable_setB_eq_val, simp, end def random_variable_ne {Ω:Type*} {P:probability_space Ω} {α:Type*} [M:measurable_space α] [E:has_measurable_equality M] (X Y:P →ᵣ M):event P := ¬ₑ (X =ᵣ Y) infixr ` ≠ᵣ `:100 := random_variable_ne @[simp] lemma rv_ne_val_def {α:Type*} {p : probability_space α} {β : Type*} [Mβ :measurable_space β] [has_measurable_equality Mβ] (X Y:p →ᵣ Mβ): (X ≠ᵣ Y).val = {a:α| X.val a ≠ Y.val a} := begin unfold random_variable_ne, rw enot_val_def, rw rv_eq_val_def, simp, ext a,split;intros A1;simp;simp at A1;apply A1, end lemma union_func_eq_sUnion_image {α β:Type*} [Tβ:measurable_space β] {A:set α} {f:α → set β}: (⋃ a∈ A, f a)=set.sUnion (@set.image α (set β) f A) := begin simp, end lemma measurable_set_countable_union_func {α β:Type*} [Tβ:measurable_space β] {A:set α} {f:α → set β}: set.countable A → (∀ a∈ A, measurable_set (f a)) → measurable_set (⋃ a∈ A, f a) := begin intros A1 A2, rw union_func_eq_sUnion_image, apply measurable_set.sUnion, { apply set.countable.image, apply A1, }, intros t A4, cases A4 with a A5, cases A5 with A6 A7, subst t, apply A2, apply A6, end -- cf set.prod_singleton_singleton lemma singleton_prod {α β:Type*} {ab:α × β}:{ab} = (@set.prod α β {ab.fst} {ab.snd}) := begin simp, end lemma top_measurable_prodh {α β:Type*} [encodable α] [encodable β] [Tα:top_measurable α] [Tβ:top_measurable β] (U:set (α × β)): measurable_set U := begin have A2:encodable (α × β):= encodable.prod, have A3:set.countable U := set.countable_encodable U, have A4:(⋃ a∈U,{a}) = U, { simp }, rw ← A4, apply measurable_set_countable_union_func A3, intros ab A5, rw singleton_prod, apply measurable_set.prod, { apply top_measurable.all_measurable, }, { apply top_measurable.all_measurable, }, end instance top_measurable_prod {α β:Type*} [encodable α] [encodable β] [Tα:top_measurable α] [Tβ:top_measurable β]:top_measurable (α × β) := { all_measurable := top_measurable_prodh, } def if_measurable_fun {α β:Type*} {Mα:measurable_space α} {Mβ:measurable_space β} (E:measurable_setB Mα) (D:decidable_pred E.val) (X:measurable_fun Mα Mβ) (Y:measurable_fun Mα Mβ):measurable_fun Mα Mβ :={ val := λ a:α, if (E.val a) then (X.val a) else (Y.val a), property := measurable.if E.property X.property Y.property, } def if_random_variable {α β:Type*} {P:probability_space α} {Mβ:measurable_space β} (E:event P) (D:decidable_pred E.val) (X:random_variable P Mβ) (Y:random_variable P Mβ):random_variable P Mβ := if_measurable_fun E D X Y lemma rv_event_IID {α : Type*} {p : probability_space α} {β : Type*} {γ : Type*} [Mγ : measurable_space γ] (X:β → p →ᵣ Mγ) (S:measurable_setB Mγ): random_variables_IID X → events_IID (λ b:β, (X b) ∈ᵣ S) := begin intro A1, unfold random_variables_IID at A1, cases A1 with A2 A3, unfold random_variable_independent at A2, unfold random_variable_identical at A3, split, { apply A2, }, { intros i j, simp, apply A3, } end @[simp] lemma measurable_setB_preimage_val_def {α:Type*} {β:Type*} [Mα:measurable_space α] [Mβ:measurable_space β] (f:measurable_fun Mα Mβ) (S:measurable_setB Mβ): (measurable_setB_preimage f S).val = (set.preimage (f.val) (S.val)) := rfl lemma compose_measurable_fun_measurable_setB {Ω : Type*} {β : Type*} {γ : Type*} [MΩ : measurable_space Ω] [Mβ : measurable_space β] [Mγ : measurable_space γ] (Y:Mβ →ₘ Mγ) (X:MΩ →ₘ Mβ) (S:measurable_setB Mγ): measurable_setB_preimage (Y ∘m X) S = measurable_setB_preimage X (measurable_setB_preimage Y S) := begin apply subtype.eq, rw measurable_setB_preimage_val_def, rw measurable_setB_preimage_val_def, rw measurable_setB_preimage_val_def, rw compose_measurable_fun_val_def, refl, end lemma rv_compose_measurable_setB {α : Type*} {β : Type*} {γ : Type*} {p : probability_space α} {Mβ : measurable_space β} {Mγ : measurable_space γ} (X:measurable_fun Mβ Mγ) (Y:random_variable p Mβ) (S:measurable_setB Mγ): (X ∘r Y) ∈ᵣ S = (Y ∈ᵣ (measurable_setB_preimage X S)) := begin apply compose_measurable_fun_measurable_setB, end lemma compose_independent' {Ω α:Type*} {p:probability_space Ω} {γ:α → Type*} [Mγ:Π (a:α), measurable_space (γ a)] {κ:α → Type*} [Mκ:Π (a:α), measurable_space (κ a)] (X:Π (b:α), p →ᵣ (Mγ b)) (Y:Π (b:α), (Mγ b) →ₘ (Mκ b)): random_variable_independent X → random_variable_independent (λ b:α, (Y b) ∘r (X b)) := begin unfold random_variable_independent, intros A1 S T, unfold independent_events, have A2:(λ (b : α), Pr[((Y b) ∘r (X b)) ∈ᵣ (S b)]) = (λ (b : α), Pr[(X b) ∈ᵣ (measurable_setB_preimage (Y b) (S b))]), { ext b, rw rv_compose_measurable_setB, }, rw A2, have A3:(λ (b : α), ((Y b) ∘r X b) ∈ᵣ S b) = (λ (b : α), (X b) ∈ᵣ (measurable_setB_preimage (Y b) (S b))), { ext b, rw rv_compose_measurable_setB, }, rw A3, apply A1, end lemma compose_independent {α:Type*} {p:probability_space α} {β:Type*} {γ:Type*} [Mγ:measurable_space γ] {κ:Type*} [Mκ:measurable_space κ] (X:β → random_variable p Mγ) (Y:Mγ →ₘ Mκ): random_variable_independent X → random_variable_independent (λ b:β, Y ∘r (X b)) := begin apply compose_independent', end lemma compose_identical {α:Type*} {p:probability_space α} {γ:Type*} [Mγ:measurable_space γ] {κ:Type*} [Mκ:measurable_space κ] (X₁ X₂:random_variable p Mγ) (Y:Mγ →ₘ Mκ): random_variable_identical X₁ X₂ → random_variable_identical (Y ∘r X₁) (Y ∘r X₂) := begin unfold random_variable_identical, intro A1, intros S, rw rv_compose_measurable_setB, rw rv_compose_measurable_setB, apply A1, end lemma compose_IID {α:Type*} {p:probability_space α} {β:Type*} {γ:Type*} [Mγ:measurable_space γ] {κ:Type*} [Mκ:measurable_space κ] (X:β → random_variable p Mγ) (Y:Mγ →ₘ Mκ): random_variables_IID X → random_variables_IID (λ b:β, Y ∘r (X b)) := begin unfold random_variables_IID, intro A1, cases A1 with A2 A3, split, { apply compose_independent, apply A2, }, { intros i j, apply compose_identical, apply A3, } end --For Pr_disjoint_summable below. lemma union_disjoint' {Ω β:Type*} {p:probability_space Ω} [D:decidable_eq β] (A:β → event p) (B:event p) {S:finset β}:(∀ a'∈ S, disjoint B.val (A a').val) → disjoint B.val (⋃ (b:β) (H:b∈ S), (A b).val) := begin intros A1, rw set.disjoint_right, intros ω A4 A3, simp at A4, cases A4 with i A4, have A5:= A1 i A4.left, rw set.disjoint_right at A5, apply A5 A4.right A3, end lemma union_disjoint {Ω β:Type*} {p:probability_space Ω} [D:decidable_eq β] (A:β → event p) {S:finset β} {a:β}:(pairwise (disjoint on λ (i : β), (A i).val)) → (a ∉ S) → disjoint (A a).val (⋃ (b:β) (H:b∈ S), (A b).val) := begin intros A1 A2, apply union_disjoint', intros a' h_a', apply A1, intros contra, subst a', apply A2 h_a', end lemma Pr_sum_disjoint_eq' {Ω β:Type*} {p:probability_space Ω} [D:decidable_eq β] (A:β → event p) {S:finset β}:(set.pairwise_on (↑S) (disjoint on (λ i, (A i).val))) → Pr[∃ᵣ a in S, A a] = finset.sum S (λ (b:β), Pr[A b]) := begin apply finset.induction_on S, { intros A1, simp, }, { intros a T A2 A3 B1, rw eany_finset_insert, rw Pr_disjoint_eor, rw finset.sum_insert A2, rw A3, { apply set.pairwise_on.mono _ B1, simp }, { apply union_disjoint', intros b h_b, apply B1, simp, simp [h_b], intros contra, subst b, apply A2 h_b } }, end lemma Pr_sum_disjoint_eq {Ω β:Type*} {p:probability_space Ω} [D:decidable_eq β] (A:β → event p) {S:finset β}:(pairwise (disjoint on λ (i : β), (A i).val)) → Pr[eany_finset S A] = finset.sum S (λ (b:β), Pr[A b]) := begin intros h0, have h1 := @Pr_sum_disjoint_eq' _ _ _ _ A S _, apply h1, apply pairwise.pairwise_on h0, end lemma Pr_sum_disjoint_bounded {Ω β:Type*} {p:probability_space Ω} [decidable_eq β] (A:β → event p) {S:finset β}:(pairwise (disjoint on λ (i : β), (A i).val)) → finset.sum S (λ (b:β), Pr[A b]) ≤ 1 := begin intro A1, rw ← Pr_sum_disjoint_eq, apply Pr_le_one, apply A1, end lemma Pr_disjoint_summable {Ω β:Type*} {p:probability_space Ω} [E:encodable β] [decidable_eq β] (A:β → event p):(pairwise (disjoint on λ (i : β), (A i).val)) → summable (λ (b:β), Pr[A b]) := begin intro A1, apply summable_bounded_nnreal, { intro S, apply Pr_sum_disjoint_bounded, apply A1, }, end lemma Pr_eany_sum {Ω β:Type*} {p:probability_space Ω} [E:encodable β] [decidable_eq β] (A:β → event p):(pairwise (disjoint on λ (i : β), (A i).val)) → Pr[eany A] = ∑' (b:β), Pr[A b] := begin intro B1, rw ← measurable_Union_eq_any, rw ← with_top.coe_eq_coe, rw event_prob_def, rw measurable_Union_val_def, rw ennreal.coe_tsum, have A1:(λ (b:β), ↑Pr[A b]) = (λ (b:β), (measure_theory.measure_space.volume (A b).val)), { ext b, rw event_prob_def, rw measure_eq_measure, }, rw measure_eq_measure, rw measure_theory.measure_Union, rw sum_subst, rw A1, apply B1, { intro i, apply (A i).property, }, { apply Pr_disjoint_summable, apply B1, }, end lemma mem_prod_random_variable_prod_measurable_setB {α β γ:Type*} {P:probability_space α} {Mβ:measurable_space β} {Mγ:measurable_space γ} (X:random_variable P Mβ) (Y:random_variable P Mγ) (S:measurable_setB Mβ) (T:measurable_setB Mγ): (X ×ᵣ Y) ∈ᵣ (prod_measurable_setB S T) = ((X ∈ᵣ S) ∧ (Y∈ᵣ T)) := begin apply event.eq, simp, refl end lemma joint_measurable.pi {Ω β:Type*} {γ:β → Type*} [measurable_space Ω] [Π (b:β), measurable_space (γ b)] (f:Π (b:β), Ω → (γ b)) (h:∀ b:β, measurable (f b)):measurable (λ (ω:Ω) (b:β), f b ω) := begin apply measurable_pi_lambda, apply h, end def measurable_space.pi_alt {δ:Type*} {π:δ → Type*} (M:Π (d:δ), measurable_space (π d)): measurable_space (Π (d:δ), π d) := @measurable_space.pi δ π M notation `Πₘ` binders `, ` r:(scoped f, measurable_space.pi_alt f) := r /- Given a function of measurable functions (X), create a measurable function whose codomain is a measurable space of functions. Alternate name: joint_measurable_fun? -/ def pi.measurable_fun {α β:Type*} {Mα:measurable_space α} {γ:β → Type*} {M:Π (b:β), measurable_space (γ b)} (X:Π (b:β), Mα →ₘ (M b)):measurable_fun Mα (@measurable_space.pi β γ M) := { val := (λ (a:α) (b:β), (X b).val a), property := begin apply measurable_pi_lambda, intros b, apply (X b).property, end, } /- Given a bunch of random variables in a common probability space, combine them to output a function. NOTE: this creates a new random variable in the existing probability space. -/ def pi.random_variable_combine {α β:Type*} {P:probability_space α} {γ:β → Type*} {M:Π (b:β), measurable_space (γ b)} (X:Π (b:β), P →ᵣ (M b)):P →ᵣ (@measurable_space.pi β γ M) := pi.measurable_fun X @[simp] def random_variable.fst {Ω:Type*} {p:probability_space Ω} {α:Type*} {Mα:measurable_space α} {β:Type*} {Mβ:measurable_space β} (X:p →ᵣ (Mα ×ₘ Mβ)):p →ᵣ Mα := mf_fst ∘r X @[simp] def random_variable.snd {Ω:Type*} {p:probability_space Ω} {α:Type*} {Mα:measurable_space α} {β:Type*} {Mβ:measurable_space β} (X:p →ᵣ (Mα ×ₘ Mβ)):p →ᵣ Mβ := mf_snd ∘r X instance const_measurable_fun.has_coe {α:Type*} [M:measurable_space α] {Ω:Type*} {MΩ:measurable_space Ω}:has_coe α (MΩ →ₘ M) := { coe := (λ (a:α), const_measurable_fun a) } instance const_random_variable.has_coe {α:Type*} [M:measurable_space α] {Ω:Type*} {p:probability_space Ω}:has_coe α (p →ᵣ M) := { coe := (λ (a:α), const_random_variable a) } instance bool.has_coe_to_rv {Ω:Type*} {p:probability_space Ω}:has_coe bool (p →ᵣ bool.measurable_space) := const_random_variable.has_coe instance int.has_coe_to_rv {Ω:Type*} {p:probability_space Ω}:has_coe int (p →ᵣ int.measurable_space) := const_random_variable.has_coe @[simp] lemma const_random_variable.has_coe.val {α:Type*} [M:measurable_space α] {Ω:Type*} {p:probability_space Ω} {a:α}: ((↑a):(p →ᵣ M)).val = (λ ω:Ω, a) := begin refl end @[simp] lemma const_measurable_fun.has_coe.val {α:Type*} [M:measurable_space α] {Ω:Type*} {MΩ:measurable_space Ω} {a:α}: ((↑a):(MΩ →ₘ M)).val = (λ ω:Ω, a) := begin refl end @[simp] lemma const_random_variable.has_coe.val_apply {α:Type*} [M:measurable_space α] {Ω:Type*} {p:probability_space Ω} {a:α} {ω:Ω}: ((↑a):(p →ᵣ M)).val ω = a := begin refl end @[simp] lemma const_measurable_fun.has_coe.val_apply {α:Type*} [M:measurable_space α] {Ω:Type*} {MΩ:measurable_space Ω} {a:α} {ω:Ω}: ((↑a):(MΩ →ₘ M)).val ω = a := begin refl end lemma random_variable_identical.symm {Ω₁ Ω₂ α:Type*} {P₁:probability_space Ω₁} {P₂:probability_space Ω₂} {M:measurable_space α} {X₁:P₁ →ᵣ M} {X₂:P₂ →ᵣ M}: random_variable_identical X₁ X₂ → random_variable_identical X₂ X₁ := begin intros h1 S, symmetry, apply h1, end lemma random_variable_identical.trans {Ω₁ Ω₂ Ω₃ α:Type*} {P₁:probability_space Ω₁} {P₂:probability_space Ω₂} {P₃:probability_space Ω₃} {M:measurable_space α} {X₁:P₁ →ᵣ M} {X₂:P₂ →ᵣ M} {X₃:P₃ →ᵣ M}: random_variable_identical X₁ X₂ → random_variable_identical X₂ X₃ → random_variable_identical X₁ X₃ := begin intros h1 h2 S, have h3:Pr[X₁ ∈ᵣ S] = Pr[X₂ ∈ᵣ S], { apply h1 }, rw h3, apply h2, end /-- This wraps `measure_theory.measure_Inter_eq_infi`. Note that this theorem uses monotonically decreasing instead of directed. This could be revisited. -/ lemma Pr_forall_eq_infi {Ω:Type*} {P:probability_space Ω} {f : ℕ → event P}: (∀ (i:ℕ), (f i.succ).val ⊆ (f i).val) → Pr[∀ᵣ i, f i] = ⨅ i, Pr[f i] := begin intros h1, rw ← ennreal.coe_eq_coe, rw event_prob_def, have h2:(∀ᵣ (i : ℕ), f i).val = ⋂ (i : ℕ),(f i).val, { simp, }, rw h2, simp, rw measure_theory.measure_Inter_eq_infi, --simp [infi], rw ennreal.coe_infi, have h3:(λ (i : ℕ), measure_theory.measure_space.volume ↑(f i)) = λ (i : ℕ), ↑Pr[f i], { ext1 i, rw event_prob_def, simp, }, rw h3, { intros i, apply (f i).property }, { --apply directed_superset_of_monotone_nat_dual, apply directed_superset_of_monotone_nat_dual, apply h1, }, apply exists.intro 0, apply lt_of_le_of_lt, apply prob_le_1, simp, end lemma Pr_forall_revent_eq_infi {Ω α:Type*} {P:probability_space Ω} {M:measurable_space α} {f : ℕ → measurable_setB M} {X:P →ᵣ M}: (∀ (i:ℕ), (f i.succ).val ⊆ (f i).val) → Pr[∀ᵣ i, X ∈ᵣ f i] = ⨅ i, Pr[X ∈ᵣ f i] := begin intros h1, apply Pr_forall_eq_infi, intros i, simp, intros ω h2, apply h1, apply h2 end /- Wraps measure_theory.measure_Union_eq_supr -/ lemma Pr_exists_eq_supr {Ω:Type*} {P:probability_space Ω} {f : ℕ → event P}: monotone (λ i, (f i).val) → Pr[∃ᵣ i, f i] = (⨆ (i:ℕ), Pr[f i]) := begin intros h1, rw ← ennreal.coe_eq_coe, rw event_prob_def, have h2:(∃ᵣ (i : ℕ), f i).val = ⋃ (i : ℕ),(f i).val, { simp, }, rw h2, simp, rw measure_theory.measure_Union_eq_supr, rw ennreal.coe_supr, have h3:(λ (i : ℕ), measure_theory.measure_space.volume ↑(f i)) = λ (i : ℕ), ↑Pr[f i], { ext1 i, rw event_prob_def, simp, }, rw h3, { simp [bdd_above], rw set.nonempty_def, apply exists.intro (1:nnreal), rw mem_upper_bounds, intros x h_mem, simp at h_mem, cases h_mem with i h_mem, subst x, apply Pr_le_one }, { intros i, apply (f i).property }, apply directed_subset_of_monotone, apply h1 end lemma Pr_exists_revent_eq_supr {Ω α:Type*} {P:probability_space Ω} {M:measurable_space α} {f : ℕ → measurable_setB M} {X:P →ᵣ M}: (monotone (λ (i:ℕ), (f i).val)) → Pr[∃ᵣ i, X ∈ᵣ f i] = ⨆ i, Pr[X ∈ᵣ f i] := begin intros h_mono, apply Pr_exists_eq_supr, intros i j h_le, simp, intros ω h2, have h_mono_i_j := h_mono h_le, simp at h_mono_i_j, apply h_mono_i_j, apply h2, end lemma disjoint_preimage {Ω γ:Type*} {P:probability_space Ω} {M:measurable_space γ} {S T:measurable_setB M} {X:P →ᵣ M}: disjoint S.val T.val → disjoint (X ∈ᵣ S).val (X ∈ᵣ T).val := begin simp [disjoint_iff], intros h, ext, split; intros h1, { simp at h1, have h2:(X.val x) ∈ (↑S ∩ ↑T), { simp [h1], rw set.mem_inter_iff, apply h1 }, rw h at h2, exfalso, apply h2 }, exfalso, apply h1, end lemma independent_event_pair.symm {Ω:Type*} {P:probability_space Ω} {E F:event P}:independent_event_pair E F → independent_event_pair F E := begin intros h1, unfold independent_event_pair, rw mul_comm, have h2:(F ∧ E) = (E ∧ F), { apply event.eq, simp, rw set.inter_comm }, rw h2, apply h1, end lemma random_variable_independent_pair.symm {Ω α β:Type*} {P:probability_space Ω} {Mα:measurable_space α} {Mβ:measurable_space β} {X:P →ᵣ Mα} {Y:P →ᵣ Mβ}:random_variable_independent_pair X Y → random_variable_independent_pair Y X := begin intros h1 S T, apply independent_event_pair.symm, apply h1, end instance measurable_setB_top.has_coe {α:Type*} [TM:top_measurable α]:has_coe (set α) (measurable_setB (TM.to_measurable_space)) := ⟨measurable_setB_top⟩ @[simp] lemma measurable_setB_top.coe_val {α:Type*} [TM:top_measurable α] (S:set α): (@coe (set α) (measurable_setB (TM.to_measurable_space)) _ S).val = S := rfl lemma event_eq_disjoint {α Ω:Type*} {P:probability_space Ω} {M:measurable_space α} {Y:P →ᵣ M} [has_measurable_equality M]: pairwise (disjoint on (λ (a:α), (Y =ᵣ a).val)) := begin intros i j h_ne, simp [function.on_fun, disjoint_iff], { ext ω, split; intros h3, { simp at h3, exfalso, apply h_ne, cases h3, subst i, subst j }, { exfalso, apply h3 } }, end lemma Pr_sum_univ_eq_one {α Ω:Type*} [fintype α] {P:probability_space Ω} {M:measurable_space α} {Y:P →ᵣ M} [has_measurable_equality M]: finset.univ.sum (λ (a:α), Pr[Y =ᵣ a]) = 1 := begin classical, have h1:(∃ᵣ (a:α), Y =ᵣ a) = event_univ, { apply event.eq, simp, ext ω, split; intros h1, simp at h1, simp, }, have h2:Pr[(∃ᵣ (a:α), Y =ᵣ a)] = 1, { rw h1, simp }, have h3:(∃ᵣ (a:α), Y =ᵣ a) = eany_finset (@finset.univ α _) (λ (a:α), Y =ᵣ a), { apply event.eq, simp }, rw h3 at h2, haveI:encodable α := fintype.encodable α, rw Pr_sum_disjoint_eq at h2, apply h2, apply event_eq_disjoint, end lemma equal_eq_mem {Ω α:Type*} {P:probability_space Ω} {TM:top_measurable α} [has_measurable_equality TM.to_measurable_space] (X:P →ᵣ TM.to_measurable_space) (a:α):(X =ᵣ a) = (X ∈ᵣ ({a}:set α)) := begin apply event.eq, simp, end lemma finset_empty_empty {α:Type*} (S:finset α):(¬(nonempty α)) → (S = ∅) := begin intros h1, ext,split;intros A1, { exfalso, apply h1, apply nonempty.intro a }, { exfalso, apply A1 }, end lemma independent_events_empty {Ω α:Type*} {P:probability_space Ω} (E:α → event P):(¬(nonempty α)) → independent_events E := begin intros h1, simp [independent_events], intros S, rw finset_empty_empty S h1, simp, end lemma random_variable_independent_empty {Ω α γ:Type*} {P:probability_space Ω} {M:measurable_space γ} (X:α → P →ᵣ M):(¬(nonempty α)) → random_variable_independent X := begin simp [random_variable_independent], intros h1 S, apply independent_events_empty, apply h1, end lemma random_variables_IID_empty {Ω α γ:Type*} {P:probability_space Ω} {M:measurable_space γ} (X:α → P →ᵣ M):(¬(nonempty α)) → random_variables_IID X := begin intros h1, simp [random_variables_IID], split, apply random_variable_independent_empty, apply h1, intros i, exfalso, apply h1, apply nonempty.intro i, end def set.pi_measurable {α:Type*} [F:fintype α] {β:α → Type*} {M:Π a, measurable_space (β a)} (T:set α) (S:Π a, measurable_setB (M a)):measurable_setB (@measurable_space.pi α β M) := { val := (set.pi T (λ a, (S a).val)), property := begin simp, apply @measurable_set.pi', intros a, apply (S a).property, end } lemma set.pi_measurable_univ {α:Type*} [F:fintype α] {β:α → Type*} {M:Π a, measurable_space (β a)} (S:Π a, measurable_setB (M a)) (T:set α) [decidable_pred T]:set.pi_measurable T S = set.pi_measurable (@set.univ α) (λ (a:α), if (a∈ T) then (S a) else (measurable_setB_univ)) := begin ext x, split;intros A1; simp [set.pi_measurable, measurable_setB_univ]; intros a; simp [set.pi_measurable, measurable_setB_univ] at A1, cases decidable.em (a ∈ T) with A2 A2, { rw if_pos, apply A1, apply A2, apply A2 }, { rw if_neg, simp, apply A2 }, { intros A3, have A4 := A1 a, rw if_pos A3 at A4, apply A4 }, end /-- Often, we use the independence of random variables directly. Specifically, many different kinds of relationships are implicitly writable in the form X ∈ᵣ C, but it is not necessarily to keep them in that form. For example, X =ᵣ 3 can be written as X ∈ᵣ {3}, but it is more convenient and clearer in the former form. More dramatically, there is a D such that (∀ᵣ (s : α) in T,X (b s) ∈ᵣ S s) = ((pi.random_variable_combine X) ∈ᵣ D). -/ lemma random_variable_independent_pair_apply {Ω α β:Type*} {P:probability_space Ω} {Mα:measurable_space α} {Mβ:measurable_space β} {X:P→ᵣ Mα} {Y:P →ᵣ Mβ} {A B:event P}: random_variable_independent_pair X Y → (∃ C:measurable_setB Mα, X ∈ᵣ C = A) → (∃ D:measurable_setB Mβ, Y ∈ᵣ D = B) → independent_event_pair A B := begin intros h1 h2 h3, cases h2 with C h2, cases h3 with D h3, rw ← h2, rw ← h3, apply h1, end lemma equal_eq_mem_exists {Ω α:Type*} {P:probability_space Ω} {TM:top_measurable α} [has_measurable_equality TM.to_measurable_space] (X:P →ᵣ TM.to_measurable_space) (a:α): ∃ (C:measurable_setB TM.to_measurable_space), (X ∈ᵣ C) = (X =ᵣ a) := begin apply exists.intro (measurable_setB_top {a}), rw equal_eq_mem, refl, end lemma or_mem_exists {Ω α:Type*} {P:probability_space Ω} {M:measurable_space α} (X:P →ᵣ M) (A B:event P): (∃ (C:measurable_setB M), (X ∈ᵣ C) = A) → (∃ (D:measurable_setB M), (X ∈ᵣ D) = B) → (∃ (E:measurable_setB M), (X ∈ᵣ E) = (A∨ B)) := begin intros h1 h2, cases h1 with C h1, cases h2 with D h2, subst A, subst B, apply exists.intro (C ∪ D), apply event.eq, ext ω, simp, end lemma and_mem_exists {Ω α:Type*} {P:probability_space Ω} {M:measurable_space α} (X:P →ᵣ M) (A B:event P): (∃ (C:measurable_setB M), (X ∈ᵣ C) = A) → (∃ (D:measurable_setB M), (X ∈ᵣ D) = B) → (∃ (E:measurable_setB M), (X ∈ᵣ E) = (A∧ B)) := begin intros h1 h2, cases h1 with C h1, cases h2 with D h2, subst A, subst B, apply exists.intro (C ∩ D), apply event.eq, ext ω, simp, end def ms_Inter {α β:Type*} {M:measurable_space α} (S:finset β) (X:β → measurable_setB M): measurable_setB M := { val := (⋂ b ∈ S, (X b).val), property := begin apply finset_inter_measurable, intros b h_b, apply (X b).property, end } lemma forall_in_mem_exists {Ω α β:Type*} {P:probability_space Ω} {M:measurable_space α} (X:P →ᵣ M) (A:β → event P) (T:finset β): (∀ b ∈ T, (∃ (C:measurable_setB M), (X ∈ᵣ C) = A b)) → (∃ (E:measurable_setB M), (X ∈ᵣ E) = (∀ᵣ b in T, A b)) := begin intros h1, have h2:∀ (b:β), (∃ (C:measurable_setB M), (b∈ T) → (X ∈ᵣ C) = A b), { intros b, cases classical.em (b∈ T) with h2_1 h2_1, { have h2_2 := h1 b h2_1, cases h2_2 with C h2_2, apply exists.intro C, intros h2_3, apply h2_2 }, { apply exists.intro measurable_setB_empty, intros contra, apply absurd contra h2_1, } }, rw classical.skolem at h2, cases h2 with f h2, apply exists.intro (ms_Inter T f), apply event.eq, ext1 a, split; intros h3; simp [ms_Inter] at h3; simp [ms_Inter, h3]; intros b h_b, { rw ← h2, apply h3, apply h_b, apply h_b }, { have h4 := h3 b h_b, have h5 := h2 b h_b, rw ← h5 at h4, apply h4 }, end lemma joint_random_variable_apply_exists {Ω α β:Type*} [fintype α] {P:probability_space Ω} {Mβ:measurable_space β} (X:α → P→ᵣ Mβ) (a:α) (E:event P): (∃ (C: measurable_setB Mβ), X a ∈ᵣ C = E) → (∃ (D : measurable_setB measurable_space.pi), pi.random_variable_combine X ∈ᵣ D = E) := begin classical, intros h1, cases h1 with C h1, let S := (set.preimage (λ (d:α → β), d a) C.val), begin have h_meas_S:measurable_set S, { apply measurable_pi_apply, apply C.property }, apply exists.intro (measurable_setB.mk h_meas_S), apply event.eq, rw ← h1, simp [pi.random_variable_combine, pi.measurable_fun, measurable_setB.mk], end end lemma joint_random_variable_mem_exists {Ω α β:Type*} [fintype α] {P:probability_space Ω} {Mβ:measurable_space β} (X:α → P→ᵣ Mβ) (T:finset α) (S:α → measurable_setB Mβ): ∃ (D : measurable_setB measurable_space.pi), pi.random_variable_combine X ∈ᵣ D = ∀ᵣ (s : α) in T,X s ∈ᵣ S s := begin apply forall_in_mem_exists, intros b h_b, apply joint_random_variable_apply_exists _ b, apply exists.intro (S b), refl, end lemma equiv_cancel_left {α β:Type*} (E:equiv α β) (a:α):E.inv_fun (E a) = a := begin have h1 := E.left_inv, apply h1, end lemma pairwise_disjoint_and_right {Ω α:Type*} {P:probability_space Ω} (A:event P) (X:α → event P): pairwise (disjoint on (λ (a:α), (X a).val)) → pairwise (disjoint on (λ (a:α), (A ∧ (X a)).val)) := begin intros h1, intros i j h_ne, have h2 := h1 i j h_ne, simp [function.on_fun] at h2, rw disjoint_iff at h2, simp at h2, simp [function.on_fun], rw disjoint_iff, simp, rw set.inter_comm (↑A) (↑(X j)), rw ← set.inter_assoc, rw set.inter_assoc (↑A), rw h2, simp, end lemma Pr_sum_partition {Ω α:Type*} {P:probability_space Ω} {TM:top_measurable α} [encodable α] [has_measurable_equality TM.to_measurable_space] (A:event P) (X:P →ᵣ TM.to_measurable_space): Pr[A] = ∑' (a:α), Pr[A ∧ (X =ᵣ a)] := begin classical, have h1:A = (eany (λ (a:α), A ∧ (X =ᵣ a))), { apply event.eq, ext ω, split; intros h_sub; simp at h_sub; simp [h_sub] }, have h2:Pr[A] = Pr[eany (λ (a:α), A ∧ (X =ᵣ a))], { rw ← h1 }, rw h2, rw Pr_eany_sum, apply pairwise_disjoint_and_right, apply event_eq_disjoint, end /- Needed for VC dimension proof connecting training and testing. -/ lemma conditional_independent_event_pair_limit {Ω α:Type*} {P:probability_space Ω} {TM:top_measurable α} [encodable α] [has_measurable_equality TM.to_measurable_space] (A B:event P) (X:P →ᵣ TM.to_measurable_space) (p:nnreal): (∀ (a:α), Pr[A ∧ (X =ᵣ a)] * p ≤ Pr[A ∧ B ∧ (X =ᵣ a)]) → (Pr[A] * p ≤ Pr[A ∧ B]) := begin classical, intros h1, rw Pr_sum_partition A X, rw Pr_sum_partition (A∧ B) X, rw mul_comm, rw ← summable.tsum_mul_left, apply tsum_le_tsum, { intros a, rw mul_comm, have h3:((A ∧ B)∧ (X =ᵣ a)) = (A∧B∧(X =ᵣ a)), { apply event.eq, ext ω, split; intros h_sub1; simp at h_sub1; simp [h_sub1] }, rw h3, apply h1 }, apply summable.mul_left, repeat { apply Pr_disjoint_summable, apply pairwise_disjoint_and_right, apply event_eq_disjoint }, end lemma compose_independent_pair_left {α β γ Ω:Type*} {P:probability_space Ω} {Mα:measurable_space α} {Mβ:measurable_space β} {Mγ:measurable_space γ} {X:P →ᵣ Mα} {Y:P →ᵣ Mβ} {Z:Mα →ₘ Mγ}:random_variable_independent_pair X Y → random_variable_independent_pair (Z ∘r X) Y := begin intros h1, intros A B, rw rv_compose_measurable_setB, apply h1, end lemma compose_independent_pair_right {α β γ Ω:Type*} {P:probability_space Ω} {Mα:measurable_space α} {Mβ:measurable_space β} {Mγ:measurable_space γ} {X:P →ᵣ Mα} {Y:P →ᵣ Mβ} {Z:Mβ →ₘ Mγ}:random_variable_independent_pair X Y → random_variable_independent_pair X (Z ∘r Y) := begin intros h1, intros A B, rw rv_compose_measurable_setB, apply h1, end
5443157ac1d46b3672a7d18d5a8588c72c25319e
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/simp33.lean
40d1c98dca81f38833fe04e521df7bdc9d671bc9
[ "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
229
lean
import tactic variable f {A : TypeU} : A → A axiom Ax1 (a : Bool) : f a = not a axiom Ax2 (a : Nat) : f a = 0 rewrite_set S add_rewrite Ax1 Ax2 not_false : S theorem T1 (a : Nat) : f (f a > 0) := by simp S print environment 1
bd00f008d43d44bc1a8b2b907c9598464a7726bb
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/algebra/category/Group/basic.lean
a62959c2448e5a2ada425f74b21f6b35390736e1
[ "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
8,773
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.category.Mon.basic import category_theory.endomorphism /-! # Category instances for group, add_group, comm_group, and add_comm_group. We introduce the bundled categories: * `Group` * `AddGroup` * `CommGroup` * `AddCommGroup` along with the relevant forgetful functors between them, and to the bundled monoid categories. -/ universes u v open category_theory /-- The category of groups and group morphisms. -/ @[to_additive AddGroup] def Group : Type (u+1) := bundled group /-- The category of additive groups and group morphisms -/ add_decl_doc AddGroup namespace Group @[to_additive] instance : bundled_hom.parent_projection group.to_monoid := ⟨⟩ attribute [derive [has_coe_to_sort, large_category, concrete_category]] Group AddGroup /-- Construct a bundled `Group` from the underlying type and typeclass. -/ @[to_additive] def of (X : Type u) [group X] : Group := bundled.of X /-- Construct a bundled `AddGroup` from the underlying type and typeclass. -/ add_decl_doc AddGroup.of @[to_additive add_group] instance (G : Group) : group G := G.str @[to_additive] instance : has_one Group := ⟨Group.of punit⟩ @[to_additive] instance : inhabited Group := ⟨1⟩ @[to_additive] instance : unique (1 : Group) := { default := 1, uniq := λ a, begin cases a, refl, end } @[simp, to_additive] lemma one_apply (G H : Group) (g : G) : (1 : G ⟶ H) g = 1 := rfl @[to_additive, ext] lemma ext (G H : Group) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ := by { ext1, apply w } attribute [ext] AddGroup.ext @[to_additive has_forget_to_AddMon] instance has_forget_to_Mon : has_forget₂ Group Mon := bundled_hom.forget₂ _ _ end Group /-- The category of commutative groups and group morphisms. -/ @[to_additive AddCommGroup] def CommGroup : Type (u+1) := bundled comm_group /-- The category of additive commutative groups and group morphisms. -/ add_decl_doc AddCommGroup /-- `Ab` is an abbreviation for `AddCommGroup`, for the sake of mathematicians' sanity. -/ abbreviation Ab := AddCommGroup namespace CommGroup @[to_additive] instance : bundled_hom.parent_projection comm_group.to_group := ⟨⟩ attribute [derive [has_coe_to_sort, large_category, concrete_category]] CommGroup AddCommGroup /-- Construct a bundled `CommGroup` from the underlying type and typeclass. -/ @[to_additive] def of (G : Type u) [comm_group G] : CommGroup := bundled.of G /-- Construct a bundled `AddCommGroup` from the underlying type and typeclass. -/ add_decl_doc AddCommGroup.of @[to_additive add_comm_group_instance] instance comm_group_instance (G : CommGroup) : comm_group G := G.str @[to_additive] instance : has_one CommGroup := ⟨CommGroup.of punit⟩ @[to_additive] instance : inhabited CommGroup := ⟨1⟩ @[to_additive] instance : unique (1 : CommGroup) := { default := 1, uniq := λ a, begin cases a, refl, end } @[simp, to_additive] lemma one_apply (G H : CommGroup) (g : G) : (1 : G ⟶ H) g = 1 := rfl @[to_additive,ext] lemma ext (G H : CommGroup) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ := by { ext1, apply w } attribute [ext] AddCommGroup.ext @[to_additive has_forget_to_AddGroup] instance has_forget_to_Group : has_forget₂ CommGroup Group := bundled_hom.forget₂ _ _ @[to_additive has_forget_to_AddCommMon] instance has_forget_to_CommMon : has_forget₂ CommGroup CommMon := induced_category.has_forget₂ (λ G : CommGroup, CommMon.of G) end CommGroup -- This example verifies an improvement possible in Lean 3.8. -- Before that, to have `monoid_hom.map_map` usable by `simp` here, -- we had to mark all the concrete category `has_coe_to_sort` instances reducible. -- Now, it just works. @[to_additive] example {R S : CommGroup} (i : R ⟶ S) (r : R) (h : r = 1) : i r = 1 := by simp [h] namespace AddCommGroup /-- Any element of an abelian group gives a unique morphism from `ℤ` sending `1` to that element. -/ -- Note that because `ℤ : Type 0`, this forces `G : AddCommGroup.{0}`, -- so we write this explicitly to be clear. -- TODO generalize this, requiring a `ulift_instances.lean` file def as_hom {G : AddCommGroup.{0}} (g : G) : (AddCommGroup.of ℤ) ⟶ G := gmultiples_hom G g @[simp] lemma as_hom_apply {G : AddCommGroup.{0}} (g : G) (i : ℤ) : (as_hom g) i = i • g := rfl lemma as_hom_injective {G : AddCommGroup.{0}} : function.injective (@as_hom G) := λ h k w, by convert congr_arg (λ k : (AddCommGroup.of ℤ) ⟶ G, (k : ℤ → G) (1 : ℤ)) w; simp @[ext] lemma int_hom_ext {G : AddCommGroup.{0}} (f g : (AddCommGroup.of ℤ) ⟶ G) (w : f (1 : ℤ) = g (1 : ℤ)) : f = g := add_monoid_hom.ext_int w -- TODO: this argument should be generalised to the situation where -- the forgetful functor is representable. lemma injective_of_mono {G H : AddCommGroup.{0}} (f : G ⟶ H) [mono f] : function.injective f := λ g₁ g₂ h, begin have t0 : as_hom g₁ ≫ f = as_hom g₂ ≫ f := begin ext, simpa [as_hom_apply] using h, end, have t1 : as_hom g₁ = as_hom g₂ := (cancel_mono _).1 t0, apply as_hom_injective t1, end end AddCommGroup variables {X Y : Type u} /-- Build an isomorphism in the category `Group` from a `mul_equiv` between `group`s. -/ @[to_additive add_equiv.to_AddGroup_iso] def mul_equiv.to_Group_iso [group X] [group Y] (e : X ≃* Y) : Group.of X ≅ Group.of Y := { hom := e.to_monoid_hom, inv := e.symm.to_monoid_hom } /-- Build an isomorphism in the category `AddGroup` from an `add_equiv` between `add_group`s. -/ add_decl_doc add_equiv.to_AddGroup_iso attribute [simps] mul_equiv.to_Group_iso add_equiv.to_AddGroup_iso /-- Build an isomorphism in the category `CommGroup` from a `mul_equiv` between `comm_group`s. -/ @[to_additive add_equiv.to_AddCommGroup_iso] def mul_equiv.to_CommGroup_iso [comm_group X] [comm_group Y] (e : X ≃* Y) : CommGroup.of X ≅ CommGroup.of Y := { hom := e.to_monoid_hom, inv := e.symm.to_monoid_hom } /-- Build an isomorphism in the category `AddCommGroup` from a `add_equiv` between `add_comm_group`s. -/ add_decl_doc add_equiv.to_AddCommGroup_iso attribute [simps] mul_equiv.to_CommGroup_iso add_equiv.to_AddCommGroup_iso namespace category_theory.iso /-- Build a `mul_equiv` from an isomorphism in the category `Group`. -/ @[to_additive AddGroup_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddGroup`."] def Group_iso_to_mul_equiv {X Y : Group} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := by tidy, right_inv := by tidy, map_mul' := by tidy }. attribute [simps] Group_iso_to_mul_equiv AddGroup_iso_to_add_equiv /-- Build a `mul_equiv` from an isomorphism in the category `CommGroup`. -/ @[to_additive AddCommGroup_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddCommGroup`."] def CommGroup_iso_to_mul_equiv {X Y : CommGroup} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := by tidy, right_inv := by tidy, map_mul' := by tidy }. attribute [simps] CommGroup_iso_to_mul_equiv AddCommGroup_iso_to_add_equiv end category_theory.iso /-- multiplicative equivalences between `group`s are the same as (isomorphic to) isomorphisms in `Group` -/ @[to_additive add_equiv_iso_AddGroup_iso "additive equivalences between `add_group`s are the same as (isomorphic to) isomorphisms in `AddGroup`"] def mul_equiv_iso_Group_iso {X Y : Type u} [group X] [group Y] : (X ≃* Y) ≅ (Group.of X ≅ Group.of Y) := { hom := λ e, e.to_Group_iso, inv := λ i, i.Group_iso_to_mul_equiv, } /-- multiplicative equivalences between `comm_group`s are the same as (isomorphic to) isomorphisms in `CommGroup` -/ @[to_additive add_equiv_iso_AddCommGroup_iso "additive equivalences between `add_comm_group`s are the same as (isomorphic to) isomorphisms in `AddCommGroup`"] def mul_equiv_iso_CommGroup_iso {X Y : Type u} [comm_group X] [comm_group Y] : (X ≃* Y) ≅ (CommGroup.of X ≅ CommGroup.of Y) := { hom := λ e, e.to_CommGroup_iso, inv := λ i, i.CommGroup_iso_to_mul_equiv, } namespace category_theory.Aut /-- The (bundled) group of automorphisms of a type is isomorphic to the (bundled) group of permutations. -/ def iso_perm {α : Type u} : Group.of (Aut α) ≅ Group.of (equiv.perm α) := { hom := ⟨λ g, g.to_equiv, (by tidy), (by tidy)⟩, inv := ⟨λ g, g.to_iso, (by tidy), (by tidy)⟩ } /-- The (unbundled) group of automorphisms of a type is `mul_equiv` to the (unbundled) group of permutations. -/ def mul_equiv_perm {α : Type u} : Aut α ≃* equiv.perm α := iso_perm.Group_iso_to_mul_equiv end category_theory.Aut
95a54f11b23e15a867efa0f33008f575755c5594
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/measure/lebesgue/integral.lean
126802eae5fd55f04deb8738672befe385714cfc
[ "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
4,419
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Sébastien Gouëzel, Yury Kudryashov -/ import measure_theory.integral.set_integral import measure_theory.measure.lebesgue.basic /-! # Properties of integration with respect to the Lebesgue measure > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4.-/ open set filter measure_theory measure_theory.measure topological_space section region_between variable {α : Type*} variables [measurable_space α] {μ : measure α} {f g : α → ℝ} {s : set α} theorem volume_region_between_eq_integral' [sigma_finite μ] (f_int : integrable_on f s μ) (g_int : integrable_on g s μ) (hs : measurable_set s) (hfg : f ≤ᵐ[μ.restrict s] g ) : μ.prod volume (region_between f g s) = ennreal.of_real (∫ y in s, (g - f) y ∂μ) := begin have h : g - f =ᵐ[μ.restrict s] (λ x, real.to_nnreal (g x - f x)), from hfg.mono (λ x hx, (real.coe_to_nnreal _ $ sub_nonneg.2 hx).symm), rw [volume_region_between_eq_lintegral f_int.ae_measurable g_int.ae_measurable hs, integral_congr_ae h, lintegral_congr_ae, lintegral_coe_eq_integral _ ((integrable_congr h).mp (g_int.sub f_int))], simpa only, end /-- If two functions are integrable on a measurable set, and one function is less than or equal to the other on that set, then the volume of the region between the two functions can be represented as an integral. -/ theorem volume_region_between_eq_integral [sigma_finite μ] (f_int : integrable_on f s μ) (g_int : integrable_on g s μ) (hs : measurable_set s) (hfg : ∀ x ∈ s, f x ≤ g x) : μ.prod volume (region_between f g s) = ennreal.of_real (∫ y in s, (g - f) y ∂μ) := volume_region_between_eq_integral' f_int g_int hs ((ae_restrict_iff' hs).mpr (eventually_of_forall hfg)) end region_between section summable_norm_Icc open continuous_map /- The following lemma is a minor variation on `integrable_of_summable_norm_restrict` in `measure_theory.integral.set_integral`, but it is placed here because it needs to know that `Icc a b` has volume `b - a`. -/ /-- If the sequence with `n`-th term the the sup norm of `λ x, f (x + n)` on the interval `Icc 0 1`, for `n ∈ ℤ`, is summable, then `f` is integrable on `ℝ`. -/ lemma real.integrable_of_summable_norm_Icc {E : Type*} [normed_add_comm_group E] {f : C(ℝ, E)} (hf : summable (λ n : ℤ, ‖(f.comp $ continuous_map.add_right n).restrict (Icc 0 1)‖)) : integrable f := begin refine integrable_of_summable_norm_restrict (summable_of_nonneg_of_le (λ n : ℤ, mul_nonneg (norm_nonneg (f.restrict (⟨Icc n (n + 1), is_compact_Icc⟩ : compacts ℝ))) ennreal.to_real_nonneg) (λ n, _) hf) (Union_Icc_int_cast ℝ), simp only [compacts.coe_mk, real.volume_Icc, add_sub_cancel', ennreal.to_real_of_real zero_le_one, mul_one, norm_le _ (norm_nonneg _)], intro x, have := ((f.comp $ continuous_map.add_right n).restrict (Icc 0 1)).norm_coe_le_norm ⟨x - n, ⟨sub_nonneg.mpr x.2.1, sub_le_iff_le_add'.mpr x.2.2⟩⟩, simpa only [continuous_map.restrict_apply, comp_apply, coe_add_right, subtype.coe_mk, sub_add_cancel] using this, end end summable_norm_Icc /-! ### Substituting `-x` for `x` These lemmas are stated in terms of either `Iic` or `Ioi` (neglecting `Iio` and `Ici`) to match mathlib's conventions for integrals over finite intervals (see `interval_integral`). For the case of finite integrals, see `interval_integral.integral_comp_neg`. -/ @[simp] lemma integral_comp_neg_Iic {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] (c : ℝ) (f : ℝ → E) : ∫ x in Iic c, f (-x) = ∫ x in Ioi (-c), f x := begin have A : measurable_embedding (λ x : ℝ, -x), from (homeomorph.neg ℝ).closed_embedding.measurable_embedding, have := A.set_integral_map f (Ici (-c)), rw measure.map_neg_eq_self (volume : measure ℝ) at this, simp_rw [←integral_Ici_eq_integral_Ioi, this, neg_preimage, preimage_neg_Ici, neg_neg], end @[simp] lemma integral_comp_neg_Ioi {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] (c : ℝ) (f : ℝ → E) : ∫ x in Ioi c, f (-x) = ∫ x in Iic (-c), f x := begin rw [←neg_neg c, ←integral_comp_neg_Iic], simp only [neg_neg], end
03d90acda09d8589342472bb8f8c6da180656abc
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/function/lp_space.lean
143ea4968c7be84a6390d8413bf6e5c468760ecf
[ "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
127,382
lean
/- Copyright (c) 2020 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne, Sébastien Gouëzel -/ import analysis.normed_space.indicator_function import analysis.normed.group.hom import measure_theory.function.ess_sup import measure_theory.function.ae_eq_fun import measure_theory.integral.mean_inequalities import measure_theory.function.strongly_measurable.inner import topology.continuous_function.compact /-! # ℒp space and Lp space This file describes properties of almost everywhere strongly measurable functions with finite seminorm, denoted by `snorm f p μ` and defined for `p:ℝ≥0∞` asmm_group (Lp E p μ) := `0` if `p=0`, `(∫ ‖f a‖^p ∂μ) ^ (1/p)` for `0 < p < ∞` and `ess_sup ‖f‖ μ` for `p=∞`. The Prop-valued `mem_ℒp f p μ` states that a function `f : α → E` has finite seminorm. The space `Lp E p μ` is the subtype of elements of `α →ₘ[μ] E` (see ae_eq_fun) such that `snorm f p μ` is finite. For `1 ≤ p`, `snorm` defines a norm and `Lp` is a complete metric space. ## Main definitions * `snorm' f p μ` : `(∫ ‖f a‖^p ∂μ) ^ (1/p)` for `f : α → F` and `p : ℝ`, where `α` is a measurable space and `F` is a normed group. * `snorm_ess_sup f μ` : seminorm in `ℒ∞`, equal to the essential supremum `ess_sup ‖f‖ μ`. * `snorm f p μ` : for `p : ℝ≥0∞`, seminorm in `ℒp`, equal to `0` for `p=0`, to `snorm' f p μ` for `0 < p < ∞` and to `snorm_ess_sup f μ` for `p = ∞`. * `mem_ℒp f p μ` : property that the function `f` is almost everywhere strongly measurable and has finite `p`-seminorm for the measure `μ` (`snorm f p μ < ∞`) * `Lp E p μ` : elements of `α →ₘ[μ] E` (see ae_eq_fun) such that `snorm f p μ` is finite. Defined as an `add_subgroup` of `α →ₘ[μ] E`. Lipschitz functions vanishing at zero act by composition on `Lp`. We define this action, and prove that it is continuous. In particular, * `continuous_linear_map.comp_Lp` defines the action on `Lp` of a continuous linear map. * `Lp.pos_part` is the positive part of an `Lp` function. * `Lp.neg_part` is the negative part of an `Lp` function. When `α` is a topological space equipped with a finite Borel measure, there is a bounded linear map from the normed space of bounded continuous functions (`α →ᵇ E`) to `Lp E p μ`. We construct this as `bounded_continuous_function.to_Lp`. ## Notations * `α →₁[μ] E` : the type `Lp E 1 μ`. * `α →₂[μ] E` : the type `Lp E 2 μ`. ## Implementation Since `Lp` is defined as an `add_subgroup`, dot notation does not work. Use `Lp.measurable f` to say that the coercion of `f` to a genuine function is measurable, instead of the non-working `f.measurable`. To prove that two `Lp` elements are equal, it suffices to show that their coercions to functions coincide almost everywhere (this is registered as an `ext` rule). This can often be done using `filter_upwards`. For instance, a proof from first principles that `f + (g + h) = (f + g) + h` could read (in the `Lp` namespace) ``` example (f g h : Lp E p μ) : (f + g) + h = f + (g + h) := begin ext1, filter_upwards [coe_fn_add (f + g) h, coe_fn_add f g, coe_fn_add f (g + h), coe_fn_add g h] with _ ha1 ha2 ha3 ha4, simp only [ha1, ha2, ha3, ha4, add_assoc], end ``` The lemma `coe_fn_add` states that the coercion of `f + g` coincides almost everywhere with the sum of the coercions of `f` and `g`. All such lemmas use `coe_fn` in their name, to distinguish the function coercion from the coercion to almost everywhere defined functions. -/ noncomputable theory open topological_space measure_theory filter open_locale nnreal ennreal big_operators topological_space measure_theory variables {α E F G : Type*} {m m0 : measurable_space α} {p : ℝ≥0∞} {q : ℝ} {μ ν : measure α} [normed_add_comm_group E] [normed_add_comm_group F] [normed_add_comm_group G] namespace measure_theory section ℒp /-! ### ℒp seminorm We define the ℒp seminorm, denoted by `snorm f p μ`. For real `p`, it is given by an integral formula (for which we use the notation `snorm' f p μ`), and for `p = ∞` it is the essential supremum (for which we use the notation `snorm_ess_sup f μ`). We also define a predicate `mem_ℒp f p μ`, requesting that a function is almost everywhere measurable and has finite `snorm f p μ`. This paragraph is devoted to the basic properties of these definitions. It is constructed as follows: for a given property, we prove it for `snorm'` and `snorm_ess_sup` when it makes sense, deduce it for `snorm`, and translate it in terms of `mem_ℒp`. -/ section ℒp_space_definition /-- `(∫ ‖f a‖^q ∂μ) ^ (1/q)`, which is a seminorm on the space of measurable functions for which this quantity is finite -/ def snorm' {m : measurable_space α} (f : α → F) (q : ℝ) (μ : measure α) : ℝ≥0∞ := (∫⁻ a, ‖f a‖₊^q ∂μ) ^ (1/q) /-- seminorm for `ℒ∞`, equal to the essential supremum of `‖f‖`. -/ def snorm_ess_sup {m : measurable_space α} (f : α → F) (μ : measure α) := ess_sup (λ x, (‖f x‖₊ : ℝ≥0∞)) μ /-- `ℒp` seminorm, equal to `0` for `p=0`, to `(∫ ‖f a‖^p ∂μ) ^ (1/p)` for `0 < p < ∞` and to `ess_sup ‖f‖ μ` for `p = ∞`. -/ def snorm {m : measurable_space α} (f : α → F) (p : ℝ≥0∞) (μ : measure α) : ℝ≥0∞ := if p = 0 then 0 else (if p = ∞ then snorm_ess_sup f μ else snorm' f (ennreal.to_real p) μ) lemma snorm_eq_snorm' (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) {f : α → F} : snorm f p μ = snorm' f (ennreal.to_real p) μ := by simp [snorm, hp_ne_zero, hp_ne_top] lemma snorm_eq_lintegral_rpow_nnnorm (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) {f : α → F} : snorm f p μ = (∫⁻ x, ‖f x‖₊ ^ p.to_real ∂μ) ^ (1 / p.to_real) := by rw [snorm_eq_snorm' hp_ne_zero hp_ne_top, snorm'] lemma snorm_one_eq_lintegral_nnnorm {f : α → F} : snorm f 1 μ = ∫⁻ x, ‖f x‖₊ ∂μ := by simp_rw [snorm_eq_lintegral_rpow_nnnorm one_ne_zero ennreal.coe_ne_top, ennreal.one_to_real, one_div_one, ennreal.rpow_one] @[simp] lemma snorm_exponent_top {f : α → F} : snorm f ∞ μ = snorm_ess_sup f μ := by simp [snorm] /-- The property that `f:α→E` is ae strongly measurable and `(∫ ‖f a‖^p ∂μ)^(1/p)` is finite if `p < ∞`, or `ess_sup f < ∞` if `p = ∞`. -/ def mem_ℒp {α} {m : measurable_space α} (f : α → E) (p : ℝ≥0∞) (μ : measure α . volume_tac) : Prop := ae_strongly_measurable f μ ∧ snorm f p μ < ∞ lemma mem_ℒp.ae_strongly_measurable {f : α → E} {p : ℝ≥0∞} (h : mem_ℒp f p μ) : ae_strongly_measurable f μ := h.1 lemma lintegral_rpow_nnnorm_eq_rpow_snorm' {f : α → F} (hq0_lt : 0 < q) : ∫⁻ a, ‖f a‖₊ ^ q ∂μ = (snorm' f q μ) ^ q := begin rw [snorm', ←ennreal.rpow_mul, one_div, inv_mul_cancel, ennreal.rpow_one], exact (ne_of_lt hq0_lt).symm, end end ℒp_space_definition section top lemma mem_ℒp.snorm_lt_top {f : α → E} (hfp : mem_ℒp f p μ) : snorm f p μ < ∞ := hfp.2 lemma mem_ℒp.snorm_ne_top {f : α → E} (hfp : mem_ℒp f p μ) : snorm f p μ ≠ ∞ := ne_of_lt (hfp.2) lemma lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top {f : α → F} (hq0_lt : 0 < q) (hfq : snorm' f q μ < ∞) : ∫⁻ a, ‖f a‖₊ ^ q ∂μ < ∞ := begin rw lintegral_rpow_nnnorm_eq_rpow_snorm' hq0_lt, exact ennreal.rpow_lt_top_of_nonneg (le_of_lt hq0_lt) (ne_of_lt hfq), end lemma lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top {f : α → F} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hfp : snorm f p μ < ∞) : ∫⁻ a, ‖f a‖₊ ^ p.to_real ∂μ < ∞ := begin apply lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top, { exact ennreal.to_real_pos hp_ne_zero hp_ne_top }, { simpa [snorm_eq_snorm' hp_ne_zero hp_ne_top] using hfp } end lemma snorm_lt_top_iff_lintegral_rpow_nnnorm_lt_top {f : α → F} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : snorm f p μ < ∞ ↔ ∫⁻ a, ‖f a‖₊ ^ p.to_real ∂μ < ∞ := ⟨lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp_ne_zero hp_ne_top, begin intros h, have hp' := ennreal.to_real_pos hp_ne_zero hp_ne_top, have : 0 < 1 / p.to_real := div_pos zero_lt_one hp', simpa [snorm_eq_lintegral_rpow_nnnorm hp_ne_zero hp_ne_top] using ennreal.rpow_lt_top_of_nonneg (le_of_lt this) (ne_of_lt h) end⟩ end top section zero @[simp] lemma snorm'_exponent_zero {f : α → F} : snorm' f 0 μ = 1 := by rw [snorm', div_zero, ennreal.rpow_zero] @[simp] lemma snorm_exponent_zero {f : α → F} : snorm f 0 μ = 0 := by simp [snorm] lemma mem_ℒp_zero_iff_ae_strongly_measurable {f : α → E} : mem_ℒp f 0 μ ↔ ae_strongly_measurable f μ := by simp [mem_ℒp, snorm_exponent_zero] @[simp] lemma snorm'_zero (hp0_lt : 0 < q) : snorm' (0 : α → F) q μ = 0 := by simp [snorm', hp0_lt] @[simp] lemma snorm'_zero' (hq0_ne : q ≠ 0) (hμ : μ ≠ 0) : snorm' (0 : α → F) q μ = 0 := begin cases le_or_lt 0 q with hq0 hq_neg, { exact snorm'_zero (lt_of_le_of_ne hq0 hq0_ne.symm), }, { simp [snorm', ennreal.rpow_eq_zero_iff, hμ, hq_neg], }, end @[simp] lemma snorm_ess_sup_zero : snorm_ess_sup (0 : α → F) μ = 0 := begin simp_rw [snorm_ess_sup, pi.zero_apply, nnnorm_zero, ennreal.coe_zero, ←ennreal.bot_eq_zero], exact ess_sup_const_bot, end @[simp] lemma snorm_zero : snorm (0 : α → F) p μ = 0 := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simp only [h_top, snorm_exponent_top, snorm_ess_sup_zero], }, rw ←ne.def at h0, simp [snorm_eq_snorm' h0 h_top, ennreal.to_real_pos h0 h_top], end @[simp] lemma snorm_zero' : snorm (λ x : α, (0 : F)) p μ = 0 := by convert snorm_zero lemma zero_mem_ℒp : mem_ℒp (0 : α → E) p μ := ⟨ae_strongly_measurable_zero, by { rw snorm_zero, exact ennreal.coe_lt_top, } ⟩ lemma zero_mem_ℒp' : mem_ℒp (λ x : α, (0 : E)) p μ := by convert zero_mem_ℒp variables [measurable_space α] lemma snorm'_measure_zero_of_pos {f : α → F} (hq_pos : 0 < q) : snorm' f q (0 : measure α) = 0 := by simp [snorm', hq_pos] lemma snorm'_measure_zero_of_exponent_zero {f : α → F} : snorm' f 0 (0 : measure α) = 1 := by simp [snorm'] lemma snorm'_measure_zero_of_neg {f : α → F} (hq_neg : q < 0) : snorm' f q (0 : measure α) = ∞ := by simp [snorm', hq_neg] @[simp] lemma snorm_ess_sup_measure_zero {f : α → F} : snorm_ess_sup f (0 : measure α) = 0 := by simp [snorm_ess_sup] @[simp] lemma snorm_measure_zero {f : α → F} : snorm f p (0 : measure α) = 0 := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simp [h_top], }, rw ←ne.def at h0, simp [snorm_eq_snorm' h0 h_top, snorm', ennreal.to_real_pos h0 h_top], end end zero section const lemma snorm'_const (c : F) (hq_pos : 0 < q) : snorm' (λ x : α , c) q μ = (‖c‖₊ : ℝ≥0∞) * (μ set.univ) ^ (1/q) := begin rw [snorm', lintegral_const, ennreal.mul_rpow_of_nonneg _ _ (by simp [hq_pos.le] : 0 ≤ 1 / q)], congr, rw ←ennreal.rpow_mul, suffices hq_cancel : q * (1/q) = 1, by rw [hq_cancel, ennreal.rpow_one], rw [one_div, mul_inv_cancel (ne_of_lt hq_pos).symm], end lemma snorm'_const' [is_finite_measure μ] (c : F) (hc_ne_zero : c ≠ 0) (hq_ne_zero : q ≠ 0) : snorm' (λ x : α , c) q μ = (‖c‖₊ : ℝ≥0∞) * (μ set.univ) ^ (1/q) := begin rw [snorm', lintegral_const, ennreal.mul_rpow_of_ne_top _ (measure_ne_top μ set.univ)], { congr, rw ←ennreal.rpow_mul, suffices hp_cancel : q * (1/q) = 1, by rw [hp_cancel, ennreal.rpow_one], rw [one_div, mul_inv_cancel hq_ne_zero], }, { rw [ne.def, ennreal.rpow_eq_top_iff, not_or_distrib, not_and_distrib, not_and_distrib], split, { left, rwa [ennreal.coe_eq_zero, nnnorm_eq_zero], }, { exact or.inl ennreal.coe_ne_top, }, }, end lemma snorm_ess_sup_const (c : F) (hμ : μ ≠ 0) : snorm_ess_sup (λ x : α, c) μ = (‖c‖₊ : ℝ≥0∞) := by rw [snorm_ess_sup, ess_sup_const _ hμ] lemma snorm'_const_of_is_probability_measure (c : F) (hq_pos : 0 < q) [is_probability_measure μ] : snorm' (λ x : α , c) q μ = (‖c‖₊ : ℝ≥0∞) := by simp [snorm'_const c hq_pos, measure_univ] lemma snorm_const (c : F) (h0 : p ≠ 0) (hμ : μ ≠ 0) : snorm (λ x : α , c) p μ = (‖c‖₊ : ℝ≥0∞) * (μ set.univ) ^ (1/(ennreal.to_real p)) := begin by_cases h_top : p = ∞, { simp [h_top, snorm_ess_sup_const c hμ], }, simp [snorm_eq_snorm' h0 h_top, snorm'_const, ennreal.to_real_pos h0 h_top], end lemma snorm_const' (c : F) (h0 : p ≠ 0) (h_top: p ≠ ∞) : snorm (λ x : α , c) p μ = (‖c‖₊ : ℝ≥0∞) * (μ set.univ) ^ (1/(ennreal.to_real p)) := begin simp [snorm_eq_snorm' h0 h_top, snorm'_const, ennreal.to_real_pos h0 h_top], end lemma snorm_const_lt_top_iff {p : ℝ≥0∞} {c : F} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : snorm (λ x : α, c) p μ < ∞ ↔ c = 0 ∨ μ set.univ < ∞ := begin have hp : 0 < p.to_real, from ennreal.to_real_pos hp_ne_zero hp_ne_top, by_cases hμ : μ = 0, { simp only [hμ, measure.coe_zero, pi.zero_apply, or_true, with_top.zero_lt_top, snorm_measure_zero], }, by_cases hc : c = 0, { simp only [hc, true_or, eq_self_iff_true, with_top.zero_lt_top, snorm_zero'], }, rw snorm_const' c hp_ne_zero hp_ne_top, by_cases hμ_top : μ set.univ = ∞, { simp [hc, hμ_top, hp], }, rw ennreal.mul_lt_top_iff, simp only [true_and, one_div, ennreal.rpow_eq_zero_iff, hμ, false_or, or_false, ennreal.coe_lt_top, nnnorm_eq_zero, ennreal.coe_eq_zero, measure_theory.measure.measure_univ_eq_zero, hp, inv_lt_zero, hc, and_false, false_and, _root_.inv_pos, or_self, hμ_top, ne.lt_top hμ_top, iff_true], exact ennreal.rpow_lt_top_of_nonneg (inv_nonneg.mpr hp.le) hμ_top, end lemma mem_ℒp_const (c : E) [is_finite_measure μ] : mem_ℒp (λ a:α, c) p μ := begin refine ⟨ae_strongly_measurable_const, _⟩, by_cases h0 : p = 0, { simp [h0], }, by_cases hμ : μ = 0, { simp [hμ], }, rw snorm_const c h0 hμ, refine ennreal.mul_lt_top ennreal.coe_ne_top _, refine (ennreal.rpow_lt_top_of_nonneg _ (measure_ne_top μ set.univ)).ne, simp, end lemma mem_ℒp_top_const (c : E) : mem_ℒp (λ a:α, c) ∞ μ := begin refine ⟨ae_strongly_measurable_const, _⟩, by_cases h : μ = 0, { simp only [h, snorm_measure_zero, with_top.zero_lt_top] }, { rw snorm_const _ ennreal.top_ne_zero h, simp only [ennreal.top_to_real, div_zero, ennreal.rpow_zero, mul_one, ennreal.coe_lt_top] } end lemma mem_ℒp_const_iff {p : ℝ≥0∞} {c : E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : mem_ℒp (λ x : α, c) p μ ↔ c = 0 ∨ μ set.univ < ∞ := begin rw ← snorm_const_lt_top_iff hp_ne_zero hp_ne_top, exact ⟨λ h, h.2, λ h, ⟨ae_strongly_measurable_const, h⟩⟩, end end const lemma snorm'_mono_ae {f : α → F} {g : α → G} (hq : 0 ≤ q) (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ ‖g x‖) : snorm' f q μ ≤ snorm' g q μ := begin rw [snorm'], refine ennreal.rpow_le_rpow _ (one_div_nonneg.2 hq), refine lintegral_mono_ae (h.mono $ λ x hx, _), exact ennreal.rpow_le_rpow (ennreal.coe_le_coe.2 hx) hq end lemma snorm'_congr_norm_ae {f g : α → F} (hfg : ∀ᵐ x ∂μ, ‖f x‖ = ‖g x‖) : snorm' f q μ = snorm' g q μ := begin have : (λ x, (‖f x‖₊ ^ q : ℝ≥0∞)) =ᵐ[μ] (λ x, ‖g x‖₊ ^ q), from hfg.mono (λ x hx, by { simp only [← coe_nnnorm, nnreal.coe_eq] at hx, simp [hx] }), simp only [snorm', lintegral_congr_ae this] end lemma snorm'_congr_ae {f g : α → F} (hfg : f =ᵐ[μ] g) : snorm' f q μ = snorm' g q μ := snorm'_congr_norm_ae (hfg.fun_comp _) lemma snorm_ess_sup_congr_ae {f g : α → F} (hfg : f =ᵐ[μ] g) : snorm_ess_sup f μ = snorm_ess_sup g μ := ess_sup_congr_ae (hfg.fun_comp (coe ∘ nnnorm)) lemma snorm_mono_ae {f : α → F} {g : α → G} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ ‖g x‖) : snorm f p μ ≤ snorm g p μ := begin simp only [snorm], split_ifs, { exact le_rfl }, { refine ess_sup_mono_ae (h.mono $ λ x hx, _), exact_mod_cast hx }, { exact snorm'_mono_ae ennreal.to_real_nonneg h } end lemma snorm_mono_ae_real {f : α → F} {g : α → ℝ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ g x) : snorm f p μ ≤ snorm g p μ := snorm_mono_ae $ h.mono (λ x hx, hx.trans ((le_abs_self _).trans (real.norm_eq_abs _).symm.le)) lemma snorm_mono {f : α → F} {g : α → G} (h : ∀ x, ‖f x‖ ≤ ‖g x‖) : snorm f p μ ≤ snorm g p μ := snorm_mono_ae (eventually_of_forall (λ x, h x)) lemma snorm_mono_real {f : α → F} {g : α → ℝ} (h : ∀ x, ‖f x‖ ≤ g x) : snorm f p μ ≤ snorm g p μ := snorm_mono_ae_real (eventually_of_forall (λ x, h x)) lemma snorm_ess_sup_le_of_ae_bound {f : α → F} {C : ℝ} (hfC : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : snorm_ess_sup f μ ≤ ennreal.of_real C:= begin simp_rw [snorm_ess_sup, ← of_real_norm_eq_coe_nnnorm], refine ess_sup_le_of_ae_le (ennreal.of_real C) (hfC.mono (λ x hx, _)), exact ennreal.of_real_le_of_real hx, end lemma snorm_ess_sup_lt_top_of_ae_bound {f : α → F} {C : ℝ} (hfC : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : snorm_ess_sup f μ < ∞ := (snorm_ess_sup_le_of_ae_bound hfC).trans_lt ennreal.of_real_lt_top lemma snorm_le_of_ae_bound {f : α → F} {C : ℝ} (hfC : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : snorm f p μ ≤ ((μ set.univ) ^ p.to_real⁻¹) * (ennreal.of_real C) := begin by_cases hμ : μ = 0, { simp [hμ] }, haveI : μ.ae.ne_bot := ae_ne_bot.mpr hμ, by_cases hp : p = 0, { simp [hp] }, have hC : 0 ≤ C, from le_trans (norm_nonneg _) hfC.exists.some_spec, have hC' : ‖C‖ = C := by rw [real.norm_eq_abs, abs_eq_self.mpr hC], have : ∀ᵐ x ∂μ, ‖f x‖ ≤ ‖(λ _, C) x‖, from hfC.mono (λ x hx, hx.trans (le_of_eq hC'.symm)), convert snorm_mono_ae this, rw [snorm_const _ hp hμ, mul_comm, ← of_real_norm_eq_coe_nnnorm, hC', one_div] end lemma snorm_congr_norm_ae {f : α → F} {g : α → G} (hfg : ∀ᵐ x ∂μ, ‖f x‖ = ‖g x‖) : snorm f p μ = snorm g p μ := le_antisymm (snorm_mono_ae $ eventually_eq.le hfg) (snorm_mono_ae $ (eventually_eq.symm hfg).le) @[simp] lemma snorm'_norm {f : α → F} : snorm' (λ a, ‖f a‖) q μ = snorm' f q μ := by simp [snorm'] @[simp] lemma snorm_norm (f : α → F) : snorm (λ x, ‖f x‖) p μ = snorm f p μ := snorm_congr_norm_ae $ eventually_of_forall $ λ x, norm_norm _ lemma snorm'_norm_rpow (f : α → F) (p q : ℝ) (hq_pos : 0 < q) : snorm' (λ x, ‖f x‖ ^ q) p μ = (snorm' f (p * q) μ) ^ q := begin simp_rw snorm', rw [← ennreal.rpow_mul, ←one_div_mul_one_div], simp_rw one_div, rw [mul_assoc, inv_mul_cancel hq_pos.ne.symm, mul_one], congr, ext1 x, simp_rw ← of_real_norm_eq_coe_nnnorm, rw [real.norm_eq_abs, abs_eq_self.mpr (real.rpow_nonneg_of_nonneg (norm_nonneg _) _), mul_comm, ← ennreal.of_real_rpow_of_nonneg (norm_nonneg _) hq_pos.le, ennreal.rpow_mul], end lemma snorm_norm_rpow (f : α → F) (hq_pos : 0 < q) : snorm (λ x, ‖f x‖ ^ q) p μ = (snorm f (p * ennreal.of_real q) μ) ^ q := begin by_cases h0 : p = 0, { simp [h0, ennreal.zero_rpow_of_pos hq_pos], }, by_cases hp_top : p = ∞, { simp only [hp_top, snorm_exponent_top, ennreal.top_mul, hq_pos.not_le, ennreal.of_real_eq_zero, if_false, snorm_exponent_top, snorm_ess_sup], have h_rpow : ess_sup (λ (x : α), (‖(‖f x‖ ^ q)‖₊ : ℝ≥0∞)) μ = ess_sup (λ (x : α), (↑‖f x‖₊) ^ q) μ, { congr, ext1 x, nth_rewrite 1 ← nnnorm_norm, rw [ennreal.coe_rpow_of_nonneg _ hq_pos.le, ennreal.coe_eq_coe], ext, push_cast, rw real.norm_rpow_of_nonneg (norm_nonneg _), }, rw h_rpow, have h_rpow_mono := ennreal.strict_mono_rpow_of_pos hq_pos, have h_rpow_surj := (ennreal.rpow_left_bijective hq_pos.ne.symm).2, let iso := h_rpow_mono.order_iso_of_surjective _ h_rpow_surj, exact (iso.ess_sup_apply (λ x, (‖f x‖₊ : ℝ≥0∞)) μ).symm, }, rw [snorm_eq_snorm' h0 hp_top, snorm_eq_snorm' _ _], swap, { refine mul_ne_zero h0 _, rwa [ne.def, ennreal.of_real_eq_zero, not_le], }, swap, { exact ennreal.mul_ne_top hp_top ennreal.of_real_ne_top, }, rw [ennreal.to_real_mul, ennreal.to_real_of_real hq_pos.le], exact snorm'_norm_rpow f p.to_real q hq_pos, end lemma snorm_congr_ae {f g : α → F} (hfg : f =ᵐ[μ] g) : snorm f p μ = snorm g p μ := snorm_congr_norm_ae $ hfg.mono (λ x hx, hx ▸ rfl) lemma mem_ℒp_congr_ae {f g : α → E} (hfg : f =ᵐ[μ] g) : mem_ℒp f p μ ↔ mem_ℒp g p μ := by simp only [mem_ℒp, snorm_congr_ae hfg, ae_strongly_measurable_congr hfg] lemma mem_ℒp.ae_eq {f g : α → E} (hfg : f =ᵐ[μ] g) (hf_Lp : mem_ℒp f p μ) : mem_ℒp g p μ := (mem_ℒp_congr_ae hfg).1 hf_Lp lemma mem_ℒp.of_le {f : α → E} {g : α → F} (hg : mem_ℒp g p μ) (hf : ae_strongly_measurable f μ) (hfg : ∀ᵐ x ∂μ, ‖f x‖ ≤ ‖g x‖) : mem_ℒp f p μ := ⟨hf, (snorm_mono_ae hfg).trans_lt hg.snorm_lt_top⟩ alias mem_ℒp.of_le ← mem_ℒp.mono lemma mem_ℒp.mono' {f : α → E} {g : α → ℝ} (hg : mem_ℒp g p μ) (hf : ae_strongly_measurable f μ) (h : ∀ᵐ a ∂μ, ‖f a‖ ≤ g a) : mem_ℒp f p μ := hg.mono hf $ h.mono $ λ x hx, le_trans hx (le_abs_self _) lemma mem_ℒp.congr_norm {f : α → E} {g : α → F} (hf : mem_ℒp f p μ) (hg : ae_strongly_measurable g μ) (h : ∀ᵐ a ∂μ, ‖f a‖ = ‖g a‖) : mem_ℒp g p μ := hf.mono hg $ eventually_eq.le $ eventually_eq.symm h lemma mem_ℒp_congr_norm {f : α → E} {g : α → F} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) (h : ∀ᵐ a ∂μ, ‖f a‖ = ‖g a‖) : mem_ℒp f p μ ↔ mem_ℒp g p μ := ⟨λ h2f, h2f.congr_norm hg h, λ h2g, h2g.congr_norm hf $ eventually_eq.symm h⟩ lemma mem_ℒp_top_of_bound {f : α → E} (hf : ae_strongly_measurable f μ) (C : ℝ) (hfC : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : mem_ℒp f ∞ μ := ⟨hf, by { rw snorm_exponent_top, exact snorm_ess_sup_lt_top_of_ae_bound hfC, }⟩ lemma mem_ℒp.of_bound [is_finite_measure μ] {f : α → E} (hf : ae_strongly_measurable f μ) (C : ℝ) (hfC : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : mem_ℒp f p μ := (mem_ℒp_const C).of_le hf (hfC.mono (λ x hx, le_trans hx (le_abs_self _))) @[mono] lemma snorm'_mono_measure (f : α → F) (hμν : ν ≤ μ) (hq : 0 ≤ q) : snorm' f q ν ≤ snorm' f q μ := begin simp_rw snorm', suffices h_integral_mono : (∫⁻ a, (‖f a‖₊ : ℝ≥0∞) ^ q ∂ν) ≤ ∫⁻ a, ‖f a‖₊ ^ q ∂μ, from ennreal.rpow_le_rpow h_integral_mono (by simp [hq]), exact lintegral_mono' hμν le_rfl, end @[mono] lemma snorm_ess_sup_mono_measure (f : α → F) (hμν : ν ≪ μ) : snorm_ess_sup f ν ≤ snorm_ess_sup f μ := by { simp_rw snorm_ess_sup, exact ess_sup_mono_measure hμν, } @[mono] lemma snorm_mono_measure (f : α → F) (hμν : ν ≤ μ) : snorm f p ν ≤ snorm f p μ := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hp_top : p = ∞, { simp [hp_top, snorm_ess_sup_mono_measure f (measure.absolutely_continuous_of_le hμν)], }, simp_rw snorm_eq_snorm' hp0 hp_top, exact snorm'_mono_measure f hμν ennreal.to_real_nonneg, end lemma mem_ℒp.mono_measure {f : α → E} (hμν : ν ≤ μ) (hf : mem_ℒp f p μ) : mem_ℒp f p ν := ⟨hf.1.mono_measure hμν, (snorm_mono_measure f hμν).trans_lt hf.2⟩ lemma mem_ℒp.restrict (s : set α) {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp f p (μ.restrict s) := hf.mono_measure measure.restrict_le_self lemma snorm'_smul_measure {p : ℝ} (hp : 0 ≤ p) {f : α → F} (c : ℝ≥0∞) : snorm' f p (c • μ) = c ^ (1 / p) * snorm' f p μ := by { rw [snorm', lintegral_smul_measure, ennreal.mul_rpow_of_nonneg, snorm'], simp [hp], } lemma snorm_ess_sup_smul_measure {f : α → F} {c : ℝ≥0∞} (hc : c ≠ 0) : snorm_ess_sup f (c • μ) = snorm_ess_sup f μ := by { simp_rw [snorm_ess_sup], exact ess_sup_smul_measure hc, } /-- Use `snorm_smul_measure_of_ne_top` instead. -/ private lemma snorm_smul_measure_of_ne_zero_of_ne_top {p : ℝ≥0∞} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) {f : α → F} (c : ℝ≥0∞) : snorm f p (c • μ) = c ^ (1 / p).to_real • snorm f p μ := begin simp_rw snorm_eq_snorm' hp_ne_zero hp_ne_top, rw snorm'_smul_measure ennreal.to_real_nonneg, congr, simp_rw one_div, rw ennreal.to_real_inv, end lemma snorm_smul_measure_of_ne_zero {p : ℝ≥0∞} {f : α → F} {c : ℝ≥0∞} (hc : c ≠ 0) : snorm f p (c • μ) = c ^ (1 / p).to_real • snorm f p μ := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hp_top : p = ∞, { simp [hp_top, snorm_ess_sup_smul_measure hc], }, exact snorm_smul_measure_of_ne_zero_of_ne_top hp0 hp_top c, end lemma snorm_smul_measure_of_ne_top {p : ℝ≥0∞} (hp_ne_top : p ≠ ∞) {f : α → F} (c : ℝ≥0∞) : snorm f p (c • μ) = c ^ (1 / p).to_real • snorm f p μ := begin by_cases hp0 : p = 0, { simp [hp0], }, { exact snorm_smul_measure_of_ne_zero_of_ne_top hp0 hp_ne_top c, }, end lemma snorm_one_smul_measure {f : α → F} (c : ℝ≥0∞) : snorm f 1 (c • μ) = c * snorm f 1 μ := by { rw @snorm_smul_measure_of_ne_top _ _ _ μ _ 1 (@ennreal.coe_ne_top 1) f c, simp, } lemma mem_ℒp.of_measure_le_smul {μ' : measure α} (c : ℝ≥0∞) (hc : c ≠ ∞) (hμ'_le : μ' ≤ c • μ) {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp f p μ' := begin refine ⟨hf.1.mono' (measure.absolutely_continuous_of_le_smul hμ'_le), _⟩, refine (snorm_mono_measure f hμ'_le).trans_lt _, by_cases hc0 : c = 0, { simp [hc0], }, rw [snorm_smul_measure_of_ne_zero hc0, smul_eq_mul], refine ennreal.mul_lt_top _ hf.2.ne, simp [hc, hc0], end lemma mem_ℒp.smul_measure {f : α → E} {c : ℝ≥0∞} (hf : mem_ℒp f p μ) (hc : c ≠ ∞) : mem_ℒp f p (c • μ) := hf.of_measure_le_smul c hc le_rfl include m lemma snorm_one_add_measure (f : α → F) (μ ν : measure α) : snorm f 1 (μ + ν) = snorm f 1 μ + snorm f 1 ν := by { simp_rw snorm_one_eq_lintegral_nnnorm, rw lintegral_add_measure _ μ ν, } lemma snorm_le_add_measure_right (f : α → F) (μ ν : measure α) {p : ℝ≥0∞} : snorm f p μ ≤ snorm f p (μ + ν) := snorm_mono_measure f $ measure.le_add_right $ le_refl _ lemma snorm_le_add_measure_left (f : α → F) (μ ν : measure α) {p : ℝ≥0∞} : snorm f p ν ≤ snorm f p (μ + ν) := snorm_mono_measure f $ measure.le_add_left $ le_refl _ omit m lemma mem_ℒp.left_of_add_measure {f : α → E} (h : mem_ℒp f p (μ + ν)) : mem_ℒp f p μ := h.mono_measure $ measure.le_add_right $ le_refl _ lemma mem_ℒp.right_of_add_measure {f : α → E} (h : mem_ℒp f p (μ + ν)) : mem_ℒp f p ν := h.mono_measure $ measure.le_add_left $ le_refl _ lemma mem_ℒp.norm {f : α → E} (h : mem_ℒp f p μ) : mem_ℒp (λ x, ‖f x‖) p μ := h.of_le h.ae_strongly_measurable.norm (eventually_of_forall (λ x, by simp)) lemma mem_ℒp_norm_iff {f : α → E} (hf : ae_strongly_measurable f μ) : mem_ℒp (λ x, ‖f x‖) p μ ↔ mem_ℒp f p μ := ⟨λ h, ⟨hf, by { rw ← snorm_norm, exact h.2, }⟩, λ h, h.norm⟩ lemma snorm'_eq_zero_of_ae_zero {f : α → F} (hq0_lt : 0 < q) (hf_zero : f =ᵐ[μ] 0) : snorm' f q μ = 0 := by rw [snorm'_congr_ae hf_zero, snorm'_zero hq0_lt] lemma snorm'_eq_zero_of_ae_zero' (hq0_ne : q ≠ 0) (hμ : μ ≠ 0) {f : α → F} (hf_zero : f =ᵐ[μ] 0) : snorm' f q μ = 0 := by rw [snorm'_congr_ae hf_zero, snorm'_zero' hq0_ne hμ] lemma ae_eq_zero_of_snorm'_eq_zero {f : α → E} (hq0 : 0 ≤ q) (hf : ae_strongly_measurable f μ) (h : snorm' f q μ = 0) : f =ᵐ[μ] 0 := begin rw [snorm', ennreal.rpow_eq_zero_iff] at h, cases h, { rw lintegral_eq_zero_iff' (hf.ennnorm.pow_const q) at h, refine h.left.mono (λ x hx, _), rw [pi.zero_apply, ennreal.rpow_eq_zero_iff] at hx, cases hx, { cases hx with hx _, rwa [←ennreal.coe_zero, ennreal.coe_eq_coe, nnnorm_eq_zero] at hx, }, { exact absurd hx.left ennreal.coe_ne_top, }, }, { exfalso, rw [one_div, inv_lt_zero] at h, exact hq0.not_lt h.right }, end lemma snorm'_eq_zero_iff (hq0_lt : 0 < q) {f : α → E} (hf : ae_strongly_measurable f μ) : snorm' f q μ = 0 ↔ f =ᵐ[μ] 0 := ⟨ae_eq_zero_of_snorm'_eq_zero (le_of_lt hq0_lt) hf, snorm'_eq_zero_of_ae_zero hq0_lt⟩ lemma coe_nnnorm_ae_le_snorm_ess_sup {m : measurable_space α} (f : α → F) (μ : measure α) : ∀ᵐ x ∂μ, (‖f x‖₊ : ℝ≥0∞) ≤ snorm_ess_sup f μ := ennreal.ae_le_ess_sup (λ x, (‖f x‖₊ : ℝ≥0∞)) @[simp] lemma snorm_ess_sup_eq_zero_iff {f : α → F} : snorm_ess_sup f μ = 0 ↔ f =ᵐ[μ] 0 := by simp [eventually_eq, snorm_ess_sup] lemma snorm_eq_zero_iff {f : α → E} (hf : ae_strongly_measurable f μ) (h0 : p ≠ 0) : snorm f p μ = 0 ↔ f =ᵐ[μ] 0 := begin by_cases h_top : p = ∞, { rw [h_top, snorm_exponent_top, snorm_ess_sup_eq_zero_iff], }, rw snorm_eq_snorm' h0 h_top, exact snorm'_eq_zero_iff (ennreal.to_real_pos h0 h_top) hf, end lemma snorm'_add_le {f g : α → E} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) (hq1 : 1 ≤ q) : snorm' (f + g) q μ ≤ snorm' f q μ + snorm' g q μ := calc (∫⁻ a, ↑‖(f + g) a‖₊ ^ q ∂μ) ^ (1 / q) ≤ (∫⁻ a, (((λ a, (‖f a‖₊ : ℝ≥0∞)) + (λ a, (‖g a‖₊ : ℝ≥0∞))) a) ^ q ∂μ) ^ (1 / q) : begin refine ennreal.rpow_le_rpow _ (by simp [le_trans zero_le_one hq1] : 0 ≤ 1 / q), refine lintegral_mono (λ a, ennreal.rpow_le_rpow _ (le_trans zero_le_one hq1)), simp [←ennreal.coe_add, nnnorm_add_le], end ... ≤ snorm' f q μ + snorm' g q μ : ennreal.lintegral_Lp_add_le hf.ennnorm hg.ennnorm hq1 lemma snorm_ess_sup_add_le {f g : α → F} : snorm_ess_sup (f + g) μ ≤ snorm_ess_sup f μ + snorm_ess_sup g μ := begin refine le_trans (ess_sup_mono_ae (eventually_of_forall (λ x, _))) (ennreal.ess_sup_add_le _ _), simp_rw [pi.add_apply, ←ennreal.coe_add, ennreal.coe_le_coe], exact nnnorm_add_le _ _, end lemma snorm_add_le {f g : α → E} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) (hp1 : 1 ≤ p) : snorm (f + g) p μ ≤ snorm f p μ + snorm g p μ := begin by_cases hp0 : p = 0, { simp [hp0], }, by_cases hp_top : p = ∞, { simp [hp_top, snorm_ess_sup_add_le], }, have hp1_real : 1 ≤ p.to_real, by rwa [← ennreal.one_to_real, ennreal.to_real_le_to_real ennreal.one_ne_top hp_top], repeat { rw snorm_eq_snorm' hp0 hp_top, }, exact snorm'_add_le hf hg hp1_real, end lemma snorm_sub_le {f g : α → E} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) (hp1 : 1 ≤ p) : snorm (f - g) p μ ≤ snorm f p μ + snorm g p μ := calc snorm (f - g) p μ = snorm (f + - g) p μ : by rw sub_eq_add_neg -- We cannot use snorm_add_le on f and (-g) because we don't have `ae_measurable (-g) μ`, since -- we don't suppose `[borel_space E]`. ... = snorm (λ x, ‖f x + - g x‖) p μ : (snorm_norm (f + - g)).symm ... ≤ snorm (λ x, ‖f x‖ + ‖- g x‖) p μ : by { refine snorm_mono_real (λ x, _), rw norm_norm, exact norm_add_le _ _, } ... = snorm (λ x, ‖f x‖ + ‖g x‖) p μ : by simp_rw norm_neg ... ≤ snorm (λ x, ‖f x‖) p μ + snorm (λ x, ‖g x‖) p μ : snorm_add_le hf.norm hg.norm hp1 ... = snorm f p μ + snorm g p μ : by rw [← snorm_norm f, ← snorm_norm g] lemma snorm_add_lt_top_of_one_le {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) (hq1 : 1 ≤ p) : snorm (f + g) p μ < ∞ := lt_of_le_of_lt (snorm_add_le hf.1 hg.1 hq1) (ennreal.add_lt_top.mpr ⟨hf.2, hg.2⟩) lemma snorm'_add_lt_top_of_le_one {f g : α → E} (hf : ae_strongly_measurable f μ) (hf_snorm : snorm' f q μ < ∞) (hg_snorm : snorm' g q μ < ∞) (hq_pos : 0 < q) (hq1 : q ≤ 1) : snorm' (f + g) q μ < ∞ := calc (∫⁻ a, ↑‖(f + g) a‖₊ ^ q ∂μ) ^ (1 / q) ≤ (∫⁻ a, (((λ a, (‖f a‖₊ : ℝ≥0∞)) + (λ a, (‖g a‖₊ : ℝ≥0∞))) a) ^ q ∂μ) ^ (1 / q) : begin refine ennreal.rpow_le_rpow _ (by simp [hq_pos.le] : 0 ≤ 1 / q), refine lintegral_mono (λ a, ennreal.rpow_le_rpow _ hq_pos.le), simp [←ennreal.coe_add, nnnorm_add_le], end ... ≤ (∫⁻ a, (‖f a‖₊ : ℝ≥0∞) ^ q + (‖g a‖₊ : ℝ≥0∞) ^ q ∂μ) ^ (1 / q) : begin refine ennreal.rpow_le_rpow (lintegral_mono (λ a, _)) (by simp [hq_pos.le] : 0 ≤ 1 / q), exact ennreal.rpow_add_le_add_rpow _ _ hq_pos.le hq1, end ... < ∞ : begin refine ennreal.rpow_lt_top_of_nonneg (by simp [hq_pos.le] : 0 ≤ 1 / q) _, rw [lintegral_add_left' (hf.ennnorm.pow_const q), ennreal.add_ne_top], exact ⟨(lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top hq_pos hf_snorm).ne, (lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top hq_pos hg_snorm).ne⟩, end lemma snorm_add_lt_top {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : snorm (f + g) p μ < ∞ := begin by_cases h0 : p = 0, { simp [h0], }, rw ←ne.def at h0, cases le_total 1 p with hp1 hp1, { exact snorm_add_lt_top_of_one_le hf hg hp1, }, have hp_top : p ≠ ∞, from (lt_of_le_of_lt hp1 ennreal.coe_lt_top).ne, have hp_pos : 0 < p.to_real, { rw [← ennreal.zero_to_real, @ennreal.to_real_lt_to_real 0 p ennreal.coe_ne_top hp_top], exact ((zero_le p).lt_of_ne h0.symm), }, have hp1_real : p.to_real ≤ 1, { rwa [← ennreal.one_to_real, @ennreal.to_real_le_to_real p 1 hp_top ennreal.coe_ne_top], }, rw snorm_eq_snorm' h0 hp_top, rw [mem_ℒp, snorm_eq_snorm' h0 hp_top] at hf hg, exact snorm'_add_lt_top_of_le_one hf.1 hf.2 hg.2 hp_pos hp1_real, end section map_measure variables {β : Type*} {mβ : measurable_space β} {f : α → β} {g : β → E} include mβ lemma snorm_ess_sup_map_measure (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : snorm_ess_sup g (measure.map f μ) = snorm_ess_sup (g ∘ f) μ := ess_sup_map_measure hg.ennnorm hf lemma snorm_map_measure (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : snorm g p (measure.map f μ) = snorm (g ∘ f) p μ := begin by_cases hp_zero : p = 0, { simp only [hp_zero, snorm_exponent_zero], }, by_cases hp_top : p = ∞, { simp_rw [hp_top, snorm_exponent_top], exact snorm_ess_sup_map_measure hg hf, }, simp_rw snorm_eq_lintegral_rpow_nnnorm hp_zero hp_top, rw lintegral_map' (hg.ennnorm.pow_const p.to_real) hf, end lemma mem_ℒp_map_measure_iff (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : mem_ℒp g p (measure.map f μ) ↔ mem_ℒp (g ∘ f) p μ := by simp [mem_ℒp, snorm_map_measure hg hf, hg.comp_ae_measurable hf, hg] lemma _root_.measurable_embedding.snorm_ess_sup_map_measure {g : β → F} (hf : measurable_embedding f) : snorm_ess_sup g (measure.map f μ) = snorm_ess_sup (g ∘ f) μ := hf.ess_sup_map_measure lemma _root_.measurable_embedding.snorm_map_measure {g : β → F} (hf : measurable_embedding f) : snorm g p (measure.map f μ) = snorm (g ∘ f) p μ := begin by_cases hp_zero : p = 0, { simp only [hp_zero, snorm_exponent_zero], }, by_cases hp : p = ∞, { simp_rw [hp, snorm_exponent_top], exact hf.ess_sup_map_measure, }, { simp_rw snorm_eq_lintegral_rpow_nnnorm hp_zero hp, rw hf.lintegral_map, }, end lemma _root_.measurable_embedding.mem_ℒp_map_measure_iff {g : β → F} (hf : measurable_embedding f) : mem_ℒp g p (measure.map f μ) ↔ mem_ℒp (g ∘ f) p μ := by simp_rw [mem_ℒp, hf.ae_strongly_measurable_map_iff, hf.snorm_map_measure] lemma _root_.measurable_equiv.mem_ℒp_map_measure_iff (f : α ≃ᵐ β) {g : β → F} : mem_ℒp g p (measure.map f μ) ↔ mem_ℒp (g ∘ f) p μ := f.measurable_embedding.mem_ℒp_map_measure_iff omit mβ end map_measure section trim lemma snorm'_trim (hm : m ≤ m0) {f : α → E} (hf : strongly_measurable[m] f) : snorm' f q (ν.trim hm) = snorm' f q ν := begin simp_rw snorm', congr' 1, refine lintegral_trim hm _, refine @measurable.pow_const _ _ _ _ _ _ _ m _ (@measurable.coe_nnreal_ennreal _ m _ _) _, apply @strongly_measurable.measurable, exact (@strongly_measurable.nnnorm α m _ _ _ hf), end lemma limsup_trim (hm : m ≤ m0) {f : α → ℝ≥0∞} (hf : measurable[m] f) : (ν.trim hm).ae.limsup f = ν.ae.limsup f := begin simp_rw limsup_eq, suffices h_set_eq : {a : ℝ≥0∞ | ∀ᵐ n ∂(ν.trim hm), f n ≤ a} = {a : ℝ≥0∞ | ∀ᵐ n ∂ν, f n ≤ a}, by rw h_set_eq, ext1 a, suffices h_meas_eq : ν {x | ¬ f x ≤ a} = ν.trim hm {x | ¬ f x ≤ a}, by simp_rw [set.mem_set_of_eq, ae_iff, h_meas_eq], refine (trim_measurable_set_eq hm _).symm, refine @measurable_set.compl _ _ m (@measurable_set_le ℝ≥0∞ _ _ _ _ m _ _ _ _ _ hf _), exact @measurable_const _ _ _ m _, end lemma ess_sup_trim (hm : m ≤ m0) {f : α → ℝ≥0∞} (hf : measurable[m] f) : ess_sup f (ν.trim hm) = ess_sup f ν := by { simp_rw ess_sup, exact limsup_trim hm hf, } lemma snorm_ess_sup_trim (hm : m ≤ m0) {f : α → E} (hf : strongly_measurable[m] f) : snorm_ess_sup f (ν.trim hm) = snorm_ess_sup f ν := ess_sup_trim _ (@strongly_measurable.ennnorm _ m _ _ _ hf) lemma snorm_trim (hm : m ≤ m0) {f : α → E} (hf : strongly_measurable[m] f) : snorm f p (ν.trim hm) = snorm f p ν := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simpa only [h_top, snorm_exponent_top] using snorm_ess_sup_trim hm hf, }, simpa only [snorm_eq_snorm' h0 h_top] using snorm'_trim hm hf, end lemma snorm_trim_ae (hm : m ≤ m0) {f : α → E} (hf : ae_strongly_measurable f (ν.trim hm)) : snorm f p (ν.trim hm) = snorm f p ν := begin rw [snorm_congr_ae hf.ae_eq_mk, snorm_congr_ae (ae_eq_of_ae_eq_trim hf.ae_eq_mk)], exact snorm_trim hm hf.strongly_measurable_mk, end lemma mem_ℒp_of_mem_ℒp_trim (hm : m ≤ m0) {f : α → E} (hf : mem_ℒp f p (ν.trim hm)) : mem_ℒp f p ν := ⟨ae_strongly_measurable_of_ae_strongly_measurable_trim hm hf.1, (le_of_eq (snorm_trim_ae hm hf.1).symm).trans_lt hf.2⟩ end trim @[simp] lemma snorm'_neg {f : α → F} : snorm' (-f) q μ = snorm' f q μ := by simp [snorm'] @[simp] lemma snorm_neg {f : α → F} : snorm (-f) p μ = snorm f p μ := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simp [h_top, snorm_ess_sup], }, simp [snorm_eq_snorm' h0 h_top], end section borel_space -- variable [borel_space E] lemma mem_ℒp.neg {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp (-f) p μ := ⟨ae_strongly_measurable.neg hf.1, by simp [hf.right]⟩ lemma mem_ℒp_neg_iff {f : α → E} : mem_ℒp (-f) p μ ↔ mem_ℒp f p μ := ⟨λ h, neg_neg f ▸ h.neg, mem_ℒp.neg⟩ lemma snorm'_le_snorm'_mul_rpow_measure_univ {p q : ℝ} (hp0_lt : 0 < p) (hpq : p ≤ q) {f : α → E} (hf : ae_strongly_measurable f μ) : snorm' f p μ ≤ snorm' f q μ * (μ set.univ) ^ (1/p - 1/q) := begin have hq0_lt : 0 < q, from lt_of_lt_of_le hp0_lt hpq, by_cases hpq_eq : p = q, { rw [hpq_eq, sub_self, ennreal.rpow_zero, mul_one], exact le_rfl, }, have hpq : p < q, from lt_of_le_of_ne hpq hpq_eq, let g := λ a : α, (1 : ℝ≥0∞), have h_rw : ∫⁻ a, ↑‖f a‖₊^p ∂ μ = ∫⁻ a, (‖f a‖₊ * (g a))^p ∂ μ, from lintegral_congr (λ a, by simp), repeat {rw snorm'}, rw h_rw, let r := p * q / (q - p), have hpqr : 1/p = 1/q + 1/r, { field_simp [(ne_of_lt hp0_lt).symm, (ne_of_lt hq0_lt).symm], ring, }, calc (∫⁻ (a : α), (↑‖f a‖₊ * g a) ^ p ∂μ) ^ (1/p) ≤ (∫⁻ (a : α), ↑‖f a‖₊ ^ q ∂μ) ^ (1/q) * (∫⁻ (a : α), (g a) ^ r ∂μ) ^ (1/r) : ennreal.lintegral_Lp_mul_le_Lq_mul_Lr hp0_lt hpq hpqr μ hf.ennnorm ae_measurable_const ... = (∫⁻ (a : α), ↑‖f a‖₊ ^ q ∂μ) ^ (1/q) * μ set.univ ^ (1/p - 1/q) : by simp [hpqr], end lemma snorm'_le_snorm_ess_sup_mul_rpow_measure_univ (hq_pos : 0 < q) {f : α → F} : snorm' f q μ ≤ snorm_ess_sup f μ * (μ set.univ) ^ (1/q) := begin have h_le : ∫⁻ (a : α), ↑‖f a‖₊ ^ q ∂μ ≤ ∫⁻ (a : α), (snorm_ess_sup f μ) ^ q ∂μ, { refine lintegral_mono_ae _, have h_nnnorm_le_snorm_ess_sup := coe_nnnorm_ae_le_snorm_ess_sup f μ, refine h_nnnorm_le_snorm_ess_sup.mono (λ x hx, ennreal.rpow_le_rpow hx (le_of_lt hq_pos)), }, rw [snorm', ←ennreal.rpow_one (snorm_ess_sup f μ)], nth_rewrite 1 ←mul_inv_cancel (ne_of_lt hq_pos).symm, rw [ennreal.rpow_mul, one_div, ←ennreal.mul_rpow_of_nonneg _ _ (by simp [hq_pos.le] : 0 ≤ q⁻¹)], refine ennreal.rpow_le_rpow _ (by simp [hq_pos.le]), rwa lintegral_const at h_le, end lemma snorm_le_snorm_mul_rpow_measure_univ {p q : ℝ≥0∞} (hpq : p ≤ q) {f : α → E} (hf : ae_strongly_measurable f μ) : snorm f p μ ≤ snorm f q μ * (μ set.univ) ^ (1/p.to_real - 1/q.to_real) := begin by_cases hp0 : p = 0, { simp [hp0, zero_le], }, rw ← ne.def at hp0, have hp0_lt : 0 < p, from lt_of_le_of_ne (zero_le _) hp0.symm, have hq0_lt : 0 < q, from lt_of_lt_of_le hp0_lt hpq, by_cases hq_top : q = ∞, { simp only [hq_top, div_zero, one_div, ennreal.top_to_real, sub_zero, snorm_exponent_top, inv_zero], by_cases hp_top : p = ∞, { simp only [hp_top, ennreal.rpow_zero, mul_one, ennreal.top_to_real, sub_zero, inv_zero, snorm_exponent_top], exact le_rfl, }, rw snorm_eq_snorm' hp0 hp_top, have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp0_lt.ne' hp_top, refine (snorm'_le_snorm_ess_sup_mul_rpow_measure_univ hp_pos).trans (le_of_eq _), congr, exact one_div _, }, have hp_lt_top : p < ∞, from hpq.trans_lt (lt_top_iff_ne_top.mpr hq_top), have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp0_lt.ne' hp_lt_top.ne, rw [snorm_eq_snorm' hp0_lt.ne.symm hp_lt_top.ne, snorm_eq_snorm' hq0_lt.ne.symm hq_top], have hpq_real : p.to_real ≤ q.to_real, by rwa ennreal.to_real_le_to_real hp_lt_top.ne hq_top, exact snorm'_le_snorm'_mul_rpow_measure_univ hp_pos hpq_real hf, end lemma snorm'_le_snorm'_of_exponent_le {m : measurable_space α} {p q : ℝ} (hp0_lt : 0 < p) (hpq : p ≤ q) (μ : measure α) [is_probability_measure μ] {f : α → E} (hf : ae_strongly_measurable f μ) : snorm' f p μ ≤ snorm' f q μ := begin have h_le_μ := snorm'_le_snorm'_mul_rpow_measure_univ hp0_lt hpq hf, rwa [measure_univ, ennreal.one_rpow, mul_one] at h_le_μ, end lemma snorm'_le_snorm_ess_sup (hq_pos : 0 < q) {f : α → F} [is_probability_measure μ] : snorm' f q μ ≤ snorm_ess_sup f μ := le_trans (snorm'_le_snorm_ess_sup_mul_rpow_measure_univ hq_pos) (le_of_eq (by simp [measure_univ])) lemma snorm_le_snorm_of_exponent_le {p q : ℝ≥0∞} (hpq : p ≤ q) [is_probability_measure μ] {f : α → E} (hf : ae_strongly_measurable f μ) : snorm f p μ ≤ snorm f q μ := (snorm_le_snorm_mul_rpow_measure_univ hpq hf).trans (le_of_eq (by simp [measure_univ])) lemma snorm'_lt_top_of_snorm'_lt_top_of_exponent_le {p q : ℝ} [is_finite_measure μ] {f : α → E} (hf : ae_strongly_measurable f μ) (hfq_lt_top : snorm' f q μ < ∞) (hp_nonneg : 0 ≤ p) (hpq : p ≤ q) : snorm' f p μ < ∞ := begin cases le_or_lt p 0 with hp_nonpos hp_pos, { rw le_antisymm hp_nonpos hp_nonneg, simp, }, have hq_pos : 0 < q, from lt_of_lt_of_le hp_pos hpq, calc snorm' f p μ ≤ snorm' f q μ * (μ set.univ) ^ (1/p - 1/q) : snorm'_le_snorm'_mul_rpow_measure_univ hp_pos hpq hf ... < ∞ : begin rw ennreal.mul_lt_top_iff, refine or.inl ⟨hfq_lt_top, ennreal.rpow_lt_top_of_nonneg _ (measure_ne_top μ set.univ)⟩, rwa [le_sub_comm, sub_zero, one_div, one_div, inv_le_inv hq_pos hp_pos], end end variables (μ) lemma pow_mul_meas_ge_le_snorm {f : α → E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ae_strongly_measurable f μ) (ε : ℝ≥0∞) : (ε * μ {x | ε ≤ ‖f x‖₊ ^ p.to_real}) ^ (1 / p.to_real) ≤ snorm f p μ := begin rw snorm_eq_lintegral_rpow_nnnorm hp_ne_zero hp_ne_top, exact ennreal.rpow_le_rpow (mul_meas_ge_le_lintegral₀ (hf.ennnorm.pow_const _) ε) (one_div_nonneg.2 ennreal.to_real_nonneg), end lemma mul_meas_ge_le_pow_snorm {f : α → E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ae_strongly_measurable f μ) (ε : ℝ≥0∞) : ε * μ {x | ε ≤ ‖f x‖₊ ^ p.to_real} ≤ snorm f p μ ^ p.to_real := begin have : 1 / p.to_real * p.to_real = 1, { refine one_div_mul_cancel _, rw [ne, ennreal.to_real_eq_zero_iff], exact not_or hp_ne_zero hp_ne_top }, rw [← ennreal.rpow_one (ε * μ {x | ε ≤ ‖f x‖₊ ^ p.to_real}), ← this, ennreal.rpow_mul], exact ennreal.rpow_le_rpow (pow_mul_meas_ge_le_snorm μ hp_ne_zero hp_ne_top hf ε) ennreal.to_real_nonneg, end /-- A version of Markov's inequality using Lp-norms. -/ lemma mul_meas_ge_le_pow_snorm' {f : α → E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ae_strongly_measurable f μ) (ε : ℝ≥0∞) : ε ^ p.to_real * μ {x | ε ≤ ‖f x‖₊} ≤ snorm f p μ ^ p.to_real := begin convert mul_meas_ge_le_pow_snorm μ hp_ne_zero hp_ne_top hf (ε ^ p.to_real), ext x, rw ennreal.rpow_le_rpow_iff (ennreal.to_real_pos hp_ne_zero hp_ne_top), end lemma meas_ge_le_mul_pow_snorm {f : α → E} (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ae_strongly_measurable f μ) {ε : ℝ≥0∞} (hε : ε ≠ 0) : μ {x | ε ≤ ‖f x‖₊} ≤ ε⁻¹ ^ p.to_real * snorm f p μ ^ p.to_real := begin by_cases ε = ∞, { simp [h] }, have hεpow : ε ^ p.to_real ≠ 0 := (ennreal.rpow_pos (pos_iff_ne_zero.2 hε) h).ne.symm, have hεpow' : ε ^ p.to_real ≠ ∞ := (ennreal.rpow_ne_top_of_nonneg ennreal.to_real_nonneg h), rw [ennreal.inv_rpow, ← ennreal.mul_le_mul_left hεpow hεpow', ← mul_assoc, ennreal.mul_inv_cancel hεpow hεpow', one_mul], exact mul_meas_ge_le_pow_snorm' μ hp_ne_zero hp_ne_top hf ε, end variables {μ} lemma mem_ℒp.mem_ℒp_of_exponent_le {p q : ℝ≥0∞} [is_finite_measure μ] {f : α → E} (hfq : mem_ℒp f q μ) (hpq : p ≤ q) : mem_ℒp f p μ := begin cases hfq with hfq_m hfq_lt_top, by_cases hp0 : p = 0, { rwa [hp0, mem_ℒp_zero_iff_ae_strongly_measurable], }, rw ←ne.def at hp0, refine ⟨hfq_m, _⟩, by_cases hp_top : p = ∞, { have hq_top : q = ∞, by rwa [hp_top, top_le_iff] at hpq, rw [hp_top], rwa hq_top at hfq_lt_top, }, have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp0 hp_top, by_cases hq_top : q = ∞, { rw snorm_eq_snorm' hp0 hp_top, rw [hq_top, snorm_exponent_top] at hfq_lt_top, refine lt_of_le_of_lt (snorm'_le_snorm_ess_sup_mul_rpow_measure_univ hp_pos) _, refine ennreal.mul_lt_top hfq_lt_top.ne _, exact (ennreal.rpow_lt_top_of_nonneg (by simp [hp_pos.le]) (measure_ne_top μ set.univ)).ne }, have hq0 : q ≠ 0, { by_contra hq_eq_zero, have hp_eq_zero : p = 0, from le_antisymm (by rwa hq_eq_zero at hpq) (zero_le _), rw [hp_eq_zero, ennreal.zero_to_real] at hp_pos, exact (lt_irrefl _) hp_pos, }, have hpq_real : p.to_real ≤ q.to_real, by rwa ennreal.to_real_le_to_real hp_top hq_top, rw snorm_eq_snorm' hp0 hp_top, rw snorm_eq_snorm' hq0 hq_top at hfq_lt_top, exact snorm'_lt_top_of_snorm'_lt_top_of_exponent_le hfq_m hfq_lt_top (le_of_lt hp_pos) hpq_real, end section has_measurable_add -- variable [has_measurable_add₂ E] lemma snorm'_sum_le {ι} {f : ι → α → E} {s : finset ι} (hfs : ∀ i, i ∈ s → ae_strongly_measurable (f i) μ) (hq1 : 1 ≤ q) : snorm' (∑ i in s, f i) q μ ≤ ∑ i in s, snorm' (f i) q μ := finset.le_sum_of_subadditive_on_pred (λ (f : α → E), snorm' f q μ) (λ f, ae_strongly_measurable f μ) (snorm'_zero (zero_lt_one.trans_le hq1)) (λ f g hf hg, snorm'_add_le hf hg hq1) (λ f g hf hg, hf.add hg) _ hfs lemma snorm_sum_le {ι} {f : ι → α → E} {s : finset ι} (hfs : ∀ i, i ∈ s → ae_strongly_measurable (f i) μ) (hp1 : 1 ≤ p) : snorm (∑ i in s, f i) p μ ≤ ∑ i in s, snorm (f i) p μ := finset.le_sum_of_subadditive_on_pred (λ (f : α → E), snorm f p μ) (λ f, ae_strongly_measurable f μ) snorm_zero (λ f g hf hg, snorm_add_le hf hg hp1) (λ f g hf hg, hf.add hg) _ hfs lemma mem_ℒp.add {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : mem_ℒp (f + g) p μ := ⟨ae_strongly_measurable.add hf.1 hg.1, snorm_add_lt_top hf hg⟩ lemma mem_ℒp.sub {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : mem_ℒp (f - g) p μ := by { rw sub_eq_add_neg, exact hf.add hg.neg } lemma mem_ℒp_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, mem_ℒp (f i) p μ) : mem_ℒp (λ a, ∑ i in s, f i a) p μ := begin haveI : decidable_eq ι := classical.dec_eq _, revert hf, refine finset.induction_on s _ _, { simp only [zero_mem_ℒp', finset.sum_empty, implies_true_iff], }, { intros i s his ih hf, simp only [his, finset.sum_insert, not_false_iff], exact (hf i (s.mem_insert_self i)).add (ih (λ j hj, hf j (finset.mem_insert_of_mem hj))), }, end lemma mem_ℒp_finset_sum' {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, mem_ℒp (f i) p μ) : mem_ℒp (∑ i in s, f i) p μ := begin convert mem_ℒp_finset_sum s hf, ext x, simp, end end has_measurable_add end borel_space section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] [normed_space 𝕜 F] lemma snorm'_const_smul {f : α → F} (c : 𝕜) (hq_pos : 0 < q) : snorm' (c • f) q μ = (‖c‖₊ : ℝ≥0∞) * snorm' f q μ := begin rw snorm', simp_rw [pi.smul_apply, nnnorm_smul, ennreal.coe_mul, ennreal.mul_rpow_of_nonneg _ _ hq_pos.le], suffices h_integral : ∫⁻ a, ↑(‖c‖₊) ^ q * ↑‖f a‖₊ ^ q ∂μ = (‖c‖₊ : ℝ≥0∞)^q * ∫⁻ a, ‖f a‖₊ ^ q ∂μ, { apply_fun (λ x, x ^ (1/q)) at h_integral, rw [h_integral, ennreal.mul_rpow_of_nonneg _ _ (by simp [hq_pos.le] : 0 ≤ 1 / q)], congr, simp_rw [←ennreal.rpow_mul, one_div, mul_inv_cancel hq_pos.ne.symm, ennreal.rpow_one], }, rw lintegral_const_mul', rw ennreal.coe_rpow_of_nonneg _ hq_pos.le, exact ennreal.coe_ne_top, end lemma snorm_ess_sup_const_smul {f : α → F} (c : 𝕜) : snorm_ess_sup (c • f) μ = (‖c‖₊ : ℝ≥0∞) * snorm_ess_sup f μ := by simp_rw [snorm_ess_sup, pi.smul_apply, nnnorm_smul, ennreal.coe_mul, ennreal.ess_sup_const_mul] lemma snorm_const_smul {f : α → F} (c : 𝕜) : snorm (c • f) p μ = (‖c‖₊ : ℝ≥0∞) * snorm f p μ := begin by_cases h0 : p = 0, { simp [h0], }, by_cases h_top : p = ∞, { simp [h_top, snorm_ess_sup_const_smul], }, repeat { rw snorm_eq_snorm' h0 h_top, }, rw ←ne.def at h0, exact snorm'_const_smul c (ennreal.to_real_pos h0 h_top), end lemma mem_ℒp.const_smul {f : α → E} (hf : mem_ℒp f p μ) (c : 𝕜) : mem_ℒp (c • f) p μ := ⟨ae_strongly_measurable.const_smul hf.1 c, (snorm_const_smul c).le.trans_lt (ennreal.mul_lt_top ennreal.coe_ne_top hf.2.ne)⟩ lemma mem_ℒp.const_mul {f : α → 𝕜} (hf : mem_ℒp f p μ) (c : 𝕜) : mem_ℒp (λ x, c * f x) p μ := hf.const_smul c lemma snorm'_smul_le_mul_snorm' {p q r : ℝ} {f : α → E} (hf : ae_strongly_measurable f μ) {φ : α → 𝕜} (hφ : ae_strongly_measurable φ μ) (hp0_lt : 0 < p) (hpq : p < q) (hpqr : 1/p = 1/q + 1/r) : snorm' (φ • f) p μ ≤ snorm' φ q μ * snorm' f r μ := begin simp_rw [snorm', pi.smul_apply', nnnorm_smul, ennreal.coe_mul], exact ennreal.lintegral_Lp_mul_le_Lq_mul_Lr hp0_lt hpq hpqr μ hφ.ennnorm hf.ennnorm, end lemma snorm_smul_le_snorm_top_mul_snorm (p : ℝ≥0∞) {f : α → E} (hf : ae_strongly_measurable f μ) (φ : α → 𝕜) : snorm (φ • f) p μ ≤ snorm φ ∞ μ * snorm f p μ := begin by_cases hp_top : p = ∞, { simp_rw [hp_top, snorm_exponent_top, snorm_ess_sup, pi.smul_apply', nnnorm_smul, ennreal.coe_mul], exact ennreal.ess_sup_mul_le _ _, }, by_cases hp_zero : p = 0, { simp only [hp_zero, snorm_exponent_zero, mul_zero, le_zero_iff], }, simp_rw [snorm_eq_lintegral_rpow_nnnorm hp_zero hp_top, snorm_exponent_top, snorm_ess_sup], calc (∫⁻ x, ↑‖(φ • f) x‖₊ ^ p.to_real ∂μ) ^ (1 / p.to_real) = (∫⁻ x, ↑‖φ x‖₊ ^ p.to_real * ↑‖f x‖₊ ^ p.to_real ∂μ) ^ (1 / p.to_real) : begin congr, ext1 x, rw [pi.smul_apply', nnnorm_smul, ennreal.coe_mul, ennreal.mul_rpow_of_nonneg _ _ (ennreal.to_real_nonneg)], end ... ≤ (∫⁻ x, (ess_sup (λ x, ↑‖φ x‖₊) μ) ^ p.to_real * ↑‖f x‖₊ ^ p.to_real ∂μ) ^ (1 / p.to_real) : begin refine ennreal.rpow_le_rpow _ _, swap, { rw one_div_nonneg, exact ennreal.to_real_nonneg, }, refine lintegral_mono_ae _, filter_upwards [@ennreal.ae_le_ess_sup _ _ μ (λ x, ↑‖φ x‖₊)] with x hx, refine ennreal.mul_le_mul _ le_rfl, exact ennreal.rpow_le_rpow hx ennreal.to_real_nonneg, end ... = ess_sup (λ x, ↑‖φ x‖₊) μ * (∫⁻ x, ↑‖f x‖₊ ^ p.to_real ∂μ) ^ (1 / p.to_real) : begin rw lintegral_const_mul'', swap, { exact hf.nnnorm.ae_measurable.coe_nnreal_ennreal.pow ae_measurable_const, }, rw ennreal.mul_rpow_of_nonneg, swap, { rw one_div_nonneg, exact ennreal.to_real_nonneg, }, rw [← ennreal.rpow_mul, one_div, mul_inv_cancel, ennreal.rpow_one], rw [ne.def, ennreal.to_real_eq_zero_iff, auto.not_or_eq], exact ⟨hp_zero, hp_top⟩, end end lemma snorm_smul_le_snorm_mul_snorm_top (p : ℝ≥0∞) (f : α → E) {φ : α → 𝕜} (hφ : ae_strongly_measurable φ μ) : snorm (φ • f) p μ ≤ snorm φ p μ * snorm f ∞ μ := begin rw ← snorm_norm, simp_rw [pi.smul_apply', norm_smul], have : (λ x, ‖φ x‖ * ‖f x‖) = (λ x, ‖f x‖) • (λ x, ‖φ x‖), { rw [smul_eq_mul, mul_comm], refl, }, rw this, have h := snorm_smul_le_snorm_top_mul_snorm p hφ.norm (λ x, ‖f x‖), refine h.trans_eq _, simp_rw snorm_norm, rw mul_comm, end /-- Hölder's inequality, as an inequality on the `ℒp` seminorm of a scalar product `φ • f`. -/ lemma snorm_smul_le_mul_snorm {p q r : ℝ≥0∞} {f : α → E} (hf : ae_strongly_measurable f μ) {φ : α → 𝕜} (hφ : ae_strongly_measurable φ μ) (hpqr : 1/p = 1/q + 1/r) : snorm (φ • f) p μ ≤ snorm φ q μ * snorm f r μ := begin by_cases hp_zero : p = 0, { simp [hp_zero], }, have hq_ne_zero : q ≠ 0, { intro hq_zero, simp only [hq_zero, hp_zero, one_div, ennreal.inv_zero, ennreal.top_add, ennreal.inv_eq_top] at hpqr, exact hpqr, }, have hr_ne_zero : r ≠ 0, { intro hr_zero, simp only [hr_zero, hp_zero, one_div, ennreal.inv_zero, ennreal.add_top, ennreal.inv_eq_top] at hpqr, exact hpqr, }, by_cases hq_top : q = ∞, { have hpr : p = r, { simpa only [hq_top, one_div, ennreal.div_top, zero_add, inv_inj] using hpqr, }, rw [← hpr, hq_top], exact snorm_smul_le_snorm_top_mul_snorm p hf φ, }, by_cases hr_top : r = ∞, { have hpq : p = q, { simpa only [hr_top, one_div, ennreal.div_top, add_zero, inv_inj] using hpqr, }, rw [← hpq, hr_top], exact snorm_smul_le_snorm_mul_snorm_top p f hφ, }, have hpq : p < q, { suffices : 1 / q < 1 / p, { rwa [one_div, one_div, ennreal.inv_lt_inv] at this, }, rw hpqr, refine ennreal.lt_add_right _ _, { simp only [hq_ne_zero, one_div, ne.def, ennreal.inv_eq_top, not_false_iff], }, { simp only [hr_top, one_div, ne.def, ennreal.inv_eq_zero, not_false_iff], }, }, rw [snorm_eq_snorm' hp_zero (hpq.trans_le le_top).ne, snorm_eq_snorm' hq_ne_zero hq_top, snorm_eq_snorm' hr_ne_zero hr_top], refine snorm'_smul_le_mul_snorm' hf hφ _ _ _, { exact ennreal.to_real_pos hp_zero (hpq.trans_le le_top).ne, }, { exact ennreal.to_real_strict_mono hq_top hpq, }, rw [← ennreal.one_to_real, ← ennreal.to_real_div, ← ennreal.to_real_div, ← ennreal.to_real_div, hpqr, ennreal.to_real_add], { simp only [hq_ne_zero, one_div, ne.def, ennreal.inv_eq_top, not_false_iff], }, { simp only [hr_ne_zero, one_div, ne.def, ennreal.inv_eq_top, not_false_iff], }, end lemma mem_ℒp.smul {p q r : ℝ≥0∞} {f : α → E} {φ : α → 𝕜} (hf : mem_ℒp f r μ) (hφ : mem_ℒp φ q μ) (hpqr : 1/p = 1/q + 1/r) : mem_ℒp (φ • f) p μ := ⟨hφ.1.smul hf.1, (snorm_smul_le_mul_snorm hf.1 hφ.1 hpqr).trans_lt (ennreal.mul_lt_top hφ.snorm_ne_top hf.snorm_ne_top)⟩ end normed_space section monotonicity lemma snorm_le_mul_snorm_aux_of_nonneg {f : α → F} {g : α → G} {c : ℝ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ c * ‖g x‖) (hc : 0 ≤ c) (p : ℝ≥0∞) : snorm f p μ ≤ (ennreal.of_real c) * snorm g p μ := begin lift c to ℝ≥0 using hc, rw [ennreal.of_real_coe_nnreal, ← c.nnnorm_eq, ← snorm_norm g, ← snorm_const_smul (c : ℝ)], swap, apply_instance, refine snorm_mono_ae _, simpa end lemma snorm_le_mul_snorm_aux_of_neg {f : α → F} {g : α → G} {c : ℝ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ c * ‖g x‖) (hc : c < 0) (p : ℝ≥0∞) : snorm f p μ = 0 ∧ snorm g p μ = 0 := begin suffices : f =ᵐ[μ] 0 ∧ g =ᵐ[μ] 0, by simp [snorm_congr_ae this.1, snorm_congr_ae this.2], refine ⟨h.mono $ λ x hx, _, h.mono $ λ x hx, _⟩, { refine norm_le_zero_iff.1 (hx.trans _), exact mul_nonpos_of_nonpos_of_nonneg hc.le (norm_nonneg _) }, { refine norm_le_zero_iff.1 (nonpos_of_mul_nonneg_right _ hc), exact (norm_nonneg _).trans hx } end lemma snorm_le_mul_snorm_of_ae_le_mul {f : α → F} {g : α → G} {c : ℝ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ c * ‖g x‖) (p : ℝ≥0∞) : snorm f p μ ≤ (ennreal.of_real c) * snorm g p μ := begin cases le_or_lt 0 c with hc hc, { exact snorm_le_mul_snorm_aux_of_nonneg h hc p }, { simp [snorm_le_mul_snorm_aux_of_neg h hc p] } end lemma mem_ℒp.of_le_mul {f : α → E} {g : α → F} {c : ℝ} (hg : mem_ℒp g p μ) (hf : ae_strongly_measurable f μ) (hfg : ∀ᵐ x ∂μ, ‖f x‖ ≤ c * ‖g x‖) : mem_ℒp f p μ := begin simp only [mem_ℒp, hf, true_and], apply lt_of_le_of_lt (snorm_le_mul_snorm_of_ae_le_mul hfg p), simp [lt_top_iff_ne_top, hg.snorm_ne_top], end end monotonicity lemma snorm_indicator_ge_of_bdd_below (hp : p ≠ 0) (hp' : p ≠ ∞) {f : α → F} (C : ℝ≥0) {s : set α} (hs : measurable_set s) (hf : ∀ᵐ x ∂μ, x ∈ s → C ≤ ‖s.indicator f x‖₊) : C • μ s ^ (1 / p.to_real) ≤ snorm (s.indicator f) p μ := begin rw [ennreal.smul_def, smul_eq_mul, snorm_eq_lintegral_rpow_nnnorm hp hp', ennreal.le_rpow_one_div_iff (ennreal.to_real_pos hp hp'), ennreal.mul_rpow_of_nonneg _ _ ennreal.to_real_nonneg, ← ennreal.rpow_mul, one_div_mul_cancel (ennreal.to_real_pos hp hp').ne.symm, ennreal.rpow_one, ← set_lintegral_const, ← lintegral_indicator _ hs], refine lintegral_mono_ae _, filter_upwards [hf] with x hx, rw nnnorm_indicator_eq_indicator_nnnorm, by_cases hxs : x ∈ s, { simp only [set.indicator_of_mem hxs] at ⊢ hx, exact ennreal.rpow_le_rpow (ennreal.coe_le_coe.2 (hx hxs)) ennreal.to_real_nonneg }, { simp [set.indicator_of_not_mem hxs] }, end section is_R_or_C variables {𝕜 : Type*} [is_R_or_C 𝕜] {f : α → 𝕜} lemma mem_ℒp.re (hf : mem_ℒp f p μ) : mem_ℒp (λ x, is_R_or_C.re (f x)) p μ := begin have : ∀ x, ‖is_R_or_C.re (f x)‖ ≤ 1 * ‖f x‖, by { intro x, rw one_mul, exact is_R_or_C.norm_re_le_norm (f x), }, exact hf.of_le_mul hf.1.re (eventually_of_forall this), end lemma mem_ℒp.im (hf : mem_ℒp f p μ) : mem_ℒp (λ x, is_R_or_C.im (f x)) p μ := begin have : ∀ x, ‖is_R_or_C.im (f x)‖ ≤ 1 * ‖f x‖, by { intro x, rw one_mul, exact is_R_or_C.norm_im_le_norm (f x), }, exact hf.of_le_mul hf.1.im (eventually_of_forall this), end end is_R_or_C section inner_product variables {E' 𝕜 : Type*} [is_R_or_C 𝕜] [inner_product_space 𝕜 E'] local notation `⟪`x`, `y`⟫` := @inner 𝕜 E' _ x y lemma mem_ℒp.const_inner (c : E') {f : α → E'} (hf : mem_ℒp f p μ) : mem_ℒp (λ a, ⟪c, f a⟫) p μ := hf.of_le_mul (ae_strongly_measurable.inner ae_strongly_measurable_const hf.1) (eventually_of_forall (λ x, norm_inner_le_norm _ _)) lemma mem_ℒp.inner_const {f : α → E'} (hf : mem_ℒp f p μ) (c : E') : mem_ℒp (λ a, ⟪f a, c⟫) p μ := hf.of_le_mul (ae_strongly_measurable.inner hf.1 ae_strongly_measurable_const) (eventually_of_forall (λ x, by { rw mul_comm, exact norm_inner_le_norm _ _, })) end inner_product section liminf variables [measurable_space E] [opens_measurable_space E] {R : ℝ≥0} lemma ae_bdd_liminf_at_top_rpow_of_snorm_bdd {p : ℝ≥0∞} {f : ℕ → α → E} (hfmeas : ∀ n, measurable (f n)) (hbdd : ∀ n, snorm (f n) p μ ≤ R) : ∀ᵐ x ∂μ, liminf (λ n, (‖f n x‖₊ ^ p.to_real : ℝ≥0∞)) at_top < ∞ := begin by_cases hp0 : p.to_real = 0, { simp only [hp0, ennreal.rpow_zero], refine eventually_of_forall (λ x, _), rw liminf_const (1 : ℝ≥0∞), exacts [ennreal.one_lt_top, at_top_ne_bot] }, have hp : p ≠ 0 := λ h, by simpa [h] using hp0, have hp' : p ≠ ∞ := λ h, by simpa [h] using hp0, refine ae_lt_top (measurable_liminf (λ n, (hfmeas n).nnnorm.coe_nnreal_ennreal.pow_const p.to_real)) (lt_of_le_of_lt (lintegral_liminf_le (λ n, (hfmeas n).nnnorm.coe_nnreal_ennreal.pow_const p.to_real)) (lt_of_le_of_lt _ (ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg ennreal.coe_ne_top : ↑R ^ p.to_real < ∞))).ne, simp_rw snorm_eq_lintegral_rpow_nnnorm hp hp' at hbdd, simp_rw [liminf_eq, eventually_at_top], exact Sup_le (λ b ⟨a, ha⟩, (ha a le_rfl).trans ((ennreal.rpow_one_div_le_iff (ennreal.to_real_pos hp hp')).1 (hbdd _))), end lemma ae_bdd_liminf_at_top_of_snorm_bdd {p : ℝ≥0∞} (hp : p ≠ 0) {f : ℕ → α → E} (hfmeas : ∀ n, measurable (f n)) (hbdd : ∀ n, snorm (f n) p μ ≤ R) : ∀ᵐ x ∂μ, liminf (λ n, (‖f n x‖₊ : ℝ≥0∞)) at_top < ∞ := begin by_cases hp' : p = ∞, { subst hp', simp_rw snorm_exponent_top at hbdd, have : ∀ n, ∀ᵐ x ∂μ, (‖f n x‖₊ : ℝ≥0∞) < R + 1 := λ n, ae_lt_of_ess_sup_lt (lt_of_le_of_lt (hbdd n) $ ennreal.lt_add_right ennreal.coe_ne_top one_ne_zero), rw ← ae_all_iff at this, filter_upwards [this] with x hx using lt_of_le_of_lt (liminf_le_of_frequently_le' $ frequently_of_forall $ λ n, (hx n).le) (ennreal.add_lt_top.2 ⟨ennreal.coe_lt_top, ennreal.one_lt_top⟩) }, filter_upwards [ae_bdd_liminf_at_top_rpow_of_snorm_bdd hfmeas hbdd] with x hx, have hppos : 0 < p.to_real := ennreal.to_real_pos hp hp', have : liminf (λ n, (‖f n x‖₊ ^ p.to_real : ℝ≥0∞)) at_top = (liminf (λ n, (‖f n x‖₊ : ℝ≥0∞)) at_top)^ p.to_real, { change liminf (λ n, ennreal.order_iso_rpow p.to_real hppos (‖f n x‖₊ : ℝ≥0∞)) at_top = ennreal.order_iso_rpow p.to_real hppos (liminf (λ n, (‖f n x‖₊ : ℝ≥0∞)) at_top), refine (order_iso.liminf_apply (ennreal.order_iso_rpow p.to_real _) _ _ _ _).symm; is_bounded_default }, rw this at hx, rw [← ennreal.rpow_one (liminf (λ n, ‖f n x‖₊) at_top), ← mul_inv_cancel hppos.ne.symm, ennreal.rpow_mul], exact ennreal.rpow_lt_top_of_nonneg (inv_nonneg.2 hppos.le) hx.ne, end end liminf end ℒp /-! ### Lp space The space of equivalence classes of measurable functions for which `snorm f p μ < ∞`. -/ @[simp] lemma snorm_ae_eq_fun {α E : Type*} [measurable_space α] {μ : measure α} [normed_add_comm_group E] {p : ℝ≥0∞} {f : α → E} (hf : ae_strongly_measurable f μ) : snorm (ae_eq_fun.mk f hf) p μ = snorm f p μ := snorm_congr_ae (ae_eq_fun.coe_fn_mk _ _) lemma mem_ℒp.snorm_mk_lt_top {α E : Type*} [measurable_space α] {μ : measure α} [normed_add_comm_group E] {p : ℝ≥0∞} {f : α → E} (hfp : mem_ℒp f p μ) : snorm (ae_eq_fun.mk f hfp.1) p μ < ∞ := by simp [hfp.2] /-- Lp space -/ def Lp {α} (E : Type*) {m : measurable_space α} [normed_add_comm_group E] (p : ℝ≥0∞) (μ : measure α . volume_tac) : add_subgroup (α →ₘ[μ] E) := { carrier := {f | snorm f p μ < ∞}, zero_mem' := by simp [snorm_congr_ae ae_eq_fun.coe_fn_zero, snorm_zero], add_mem' := λ f g hf hg, by simp [snorm_congr_ae (ae_eq_fun.coe_fn_add _ _), snorm_add_lt_top ⟨f.ae_strongly_measurable, hf⟩ ⟨g.ae_strongly_measurable, hg⟩], neg_mem' := λ f hf, by rwa [set.mem_set_of_eq, snorm_congr_ae (ae_eq_fun.coe_fn_neg _), snorm_neg] } localized "notation (name := measure_theory.L1) α ` →₁[`:25 μ `] ` E := measure_theory.Lp E 1 μ" in measure_theory localized "notation (name := measure_theory.L2) α ` →₂[`:25 μ `] ` E := measure_theory.Lp E 2 μ" in measure_theory namespace mem_ℒp /-- make an element of Lp from a function verifying `mem_ℒp` -/ def to_Lp (f : α → E) (h_mem_ℒp : mem_ℒp f p μ) : Lp E p μ := ⟨ae_eq_fun.mk f h_mem_ℒp.1, h_mem_ℒp.snorm_mk_lt_top⟩ lemma coe_fn_to_Lp {f : α → E} (hf : mem_ℒp f p μ) : hf.to_Lp f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk _ _ lemma to_Lp_congr {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) (hfg : f =ᵐ[μ] g) : hf.to_Lp f = hg.to_Lp g := by simp [to_Lp, hfg] @[simp] lemma to_Lp_eq_to_Lp_iff {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : hf.to_Lp f = hg.to_Lp g ↔ f =ᵐ[μ] g := by simp [to_Lp] @[simp] lemma to_Lp_zero (h : mem_ℒp (0 : α → E) p μ) : h.to_Lp 0 = 0 := rfl lemma to_Lp_add {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : (hf.add hg).to_Lp (f + g) = hf.to_Lp f + hg.to_Lp g := rfl lemma to_Lp_neg {f : α → E} (hf : mem_ℒp f p μ) : hf.neg.to_Lp (-f) = - hf.to_Lp f := rfl lemma to_Lp_sub {f g : α → E} (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : (hf.sub hg).to_Lp (f - g) = hf.to_Lp f - hg.to_Lp g := rfl end mem_ℒp namespace Lp instance : has_coe_to_fun (Lp E p μ) (λ _, α → E) := ⟨λ f, ((f : α →ₘ[μ] E) : α → E)⟩ @[ext] lemma ext {f g : Lp E p μ} (h : f =ᵐ[μ] g) : f = g := begin cases f, cases g, simp only [subtype.mk_eq_mk], exact ae_eq_fun.ext h end lemma ext_iff {f g : Lp E p μ} : f = g ↔ f =ᵐ[μ] g := ⟨λ h, by rw h, λ h, ext h⟩ lemma mem_Lp_iff_snorm_lt_top {f : α →ₘ[μ] E} : f ∈ Lp E p μ ↔ snorm f p μ < ∞ := iff.refl _ lemma mem_Lp_iff_mem_ℒp {f : α →ₘ[μ] E} : f ∈ Lp E p μ ↔ mem_ℒp f p μ := by simp [mem_Lp_iff_snorm_lt_top, mem_ℒp, f.strongly_measurable.ae_strongly_measurable] protected lemma antitone [is_finite_measure μ] {p q : ℝ≥0∞} (hpq : p ≤ q) : Lp E q μ ≤ Lp E p μ := λ f hf, (mem_ℒp.mem_ℒp_of_exponent_le ⟨f.ae_strongly_measurable, hf⟩ hpq).2 @[simp] lemma coe_fn_mk {f : α →ₘ[μ] E} (hf : snorm f p μ < ∞) : ((⟨f, hf⟩ : Lp E p μ) : α → E) = f := rfl @[simp] lemma coe_mk {f : α →ₘ[μ] E} (hf : snorm f p μ < ∞) : ((⟨f, hf⟩ : Lp E p μ) : α →ₘ[μ] E) = f := rfl @[simp] lemma to_Lp_coe_fn (f : Lp E p μ) (hf : mem_ℒp f p μ) : hf.to_Lp f = f := by { cases f, simp [mem_ℒp.to_Lp] } lemma snorm_lt_top (f : Lp E p μ) : snorm f p μ < ∞ := f.prop lemma snorm_ne_top (f : Lp E p μ) : snorm f p μ ≠ ∞ := (snorm_lt_top f).ne @[measurability] protected lemma strongly_measurable (f : Lp E p μ) : strongly_measurable f := f.val.strongly_measurable @[measurability] protected lemma ae_strongly_measurable (f : Lp E p μ) : ae_strongly_measurable f μ := f.val.ae_strongly_measurable protected lemma mem_ℒp (f : Lp E p μ) : mem_ℒp f p μ := ⟨Lp.ae_strongly_measurable f, f.prop⟩ variables (E p μ) lemma coe_fn_zero : ⇑(0 : Lp E p μ) =ᵐ[μ] 0 := ae_eq_fun.coe_fn_zero variables {E p μ} lemma coe_fn_neg (f : Lp E p μ) : ⇑(-f) =ᵐ[μ] -f := ae_eq_fun.coe_fn_neg _ lemma coe_fn_add (f g : Lp E p μ) : ⇑(f + g) =ᵐ[μ] f + g := ae_eq_fun.coe_fn_add _ _ lemma coe_fn_sub (f g : Lp E p μ) : ⇑(f - g) =ᵐ[μ] f - g := ae_eq_fun.coe_fn_sub _ _ lemma mem_Lp_const (α) {m : measurable_space α} (μ : measure α) (c : E) [is_finite_measure μ] : @ae_eq_fun.const α _ _ μ _ c ∈ Lp E p μ := (mem_ℒp_const c).snorm_mk_lt_top instance : has_norm (Lp E p μ) := { norm := λ f, ennreal.to_real (snorm f p μ) } instance : has_dist (Lp E p μ) := { dist := λ f g, ‖f - g‖} instance : has_edist (Lp E p μ) := { edist := λ f g, snorm (f - g) p μ } lemma norm_def (f : Lp E p μ) : ‖f‖ = ennreal.to_real (snorm f p μ) := rfl @[simp] lemma norm_to_Lp (f : α → E) (hf : mem_ℒp f p μ) : ‖hf.to_Lp f‖ = ennreal.to_real (snorm f p μ) := by rw [norm_def, snorm_congr_ae (mem_ℒp.coe_fn_to_Lp hf)] lemma dist_def (f g : Lp E p μ) : dist f g = (snorm (f - g) p μ).to_real := begin simp_rw [dist, norm_def], congr' 1, apply snorm_congr_ae (coe_fn_sub _ _), end lemma edist_def (f g : Lp E p μ) : edist f g = snorm (f - g) p μ := rfl @[simp] lemma edist_to_Lp_to_Lp (f g : α → E) (hf : mem_ℒp f p μ) (hg : mem_ℒp g p μ) : edist (hf.to_Lp f) (hg.to_Lp g) = snorm (f - g) p μ := by { rw edist_def, exact snorm_congr_ae (hf.coe_fn_to_Lp.sub hg.coe_fn_to_Lp) } @[simp] lemma edist_to_Lp_zero (f : α → E) (hf : mem_ℒp f p μ) : edist (hf.to_Lp f) 0 = snorm f p μ := by { convert edist_to_Lp_to_Lp f 0 hf zero_mem_ℒp, simp } @[simp] lemma norm_zero : ‖(0 : Lp E p μ)‖ = 0 := begin change (snorm ⇑(0 : α →ₘ[μ] E) p μ).to_real = 0, simp [snorm_congr_ae ae_eq_fun.coe_fn_zero, snorm_zero] end lemma norm_eq_zero_iff {f : Lp E p μ} (hp : 0 < p) : ‖f‖ = 0 ↔ f = 0 := begin refine ⟨λ hf, _, λ hf, by simp [hf]⟩, rw [norm_def, ennreal.to_real_eq_zero_iff] at hf, cases hf, { rw snorm_eq_zero_iff (Lp.ae_strongly_measurable f) hp.ne.symm at hf, exact subtype.eq (ae_eq_fun.ext (hf.trans ae_eq_fun.coe_fn_zero.symm)), }, { exact absurd hf (snorm_ne_top f), }, end lemma eq_zero_iff_ae_eq_zero {f : Lp E p μ} : f = 0 ↔ f =ᵐ[μ] 0 := begin split, { assume h, rw h, exact ae_eq_fun.coe_fn_const _ _ }, { assume h, ext1, filter_upwards [h, ae_eq_fun.coe_fn_const α (0 : E)] with _ ha h'a, rw ha, exact h'a.symm, }, end @[simp] lemma norm_neg {f : Lp E p μ} : ‖-f‖ = ‖f‖ := by rw [norm_def, norm_def, snorm_congr_ae (coe_fn_neg _), snorm_neg] lemma norm_le_mul_norm_of_ae_le_mul {c : ℝ} {f : Lp E p μ} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ c * ‖g x‖) : ‖f‖ ≤ c * ‖g‖ := begin by_cases pzero : p = 0, { simp [pzero, norm_def] }, cases le_or_lt 0 c with hc hc, { have := snorm_le_mul_snorm_aux_of_nonneg h hc p, rw [← ennreal.to_real_le_to_real, ennreal.to_real_mul, ennreal.to_real_of_real hc] at this, { exact this }, { exact (Lp.mem_ℒp _).snorm_ne_top }, { simp [(Lp.mem_ℒp _).snorm_ne_top] } }, { have := snorm_le_mul_snorm_aux_of_neg h hc p, simp only [snorm_eq_zero_iff (Lp.ae_strongly_measurable _) pzero, ← eq_zero_iff_ae_eq_zero] at this, simp [this] } end lemma norm_le_norm_of_ae_le {f : Lp E p μ} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ ‖g x‖) : ‖f‖ ≤ ‖g‖ := begin rw [norm_def, norm_def, ennreal.to_real_le_to_real (snorm_ne_top _) (snorm_ne_top _)], exact snorm_mono_ae h end lemma mem_Lp_of_ae_le_mul {c : ℝ} {f : α →ₘ[μ] E} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ c * ‖g x‖) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_le_mul (Lp.mem_ℒp g) f.ae_strongly_measurable h lemma mem_Lp_of_ae_le {f : α →ₘ[μ] E} {g : Lp F p μ} (h : ∀ᵐ x ∂μ, ‖f x‖ ≤ ‖g x‖) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_le (Lp.mem_ℒp g) f.ae_strongly_measurable h lemma mem_Lp_of_ae_bound [is_finite_measure μ] {f : α →ₘ[μ] E} (C : ℝ) (hfC : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : f ∈ Lp E p μ := mem_Lp_iff_mem_ℒp.2 $ mem_ℒp.of_bound f.ae_strongly_measurable _ hfC lemma norm_le_of_ae_bound [is_finite_measure μ] {f : Lp E p μ} {C : ℝ} (hC : 0 ≤ C) (hfC : ∀ᵐ x ∂μ, ‖f x‖ ≤ C) : ‖f‖ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * C := begin by_cases hμ : μ = 0, { by_cases hp : p.to_real⁻¹ = 0, { simpa [hp, hμ, norm_def] using hC }, { simp [hμ, norm_def, real.zero_rpow hp] } }, let A : ℝ≥0 := (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * ⟨C, hC⟩, suffices : snorm f p μ ≤ A, { exact ennreal.to_real_le_coe_of_le_coe this }, convert snorm_le_of_ae_bound hfC, rw [← coe_measure_univ_nnreal μ, ennreal.coe_rpow_of_ne_zero (measure_univ_nnreal_pos hμ).ne', ennreal.coe_mul], congr, rw max_eq_left hC end instance [hp : fact (1 ≤ p)] : normed_add_comm_group (Lp E p μ) := { edist := edist, edist_dist := λ f g, by rw [edist_def, dist_def, ←snorm_congr_ae (coe_fn_sub _ _), ennreal.of_real_to_real (snorm_ne_top (f - g))], ..add_group_norm.to_normed_add_comm_group { to_fun := (norm : Lp E p μ → ℝ), map_zero' := norm_zero, neg' := by simp, add_le' := λ f g, begin simp only [norm_def], rw ← ennreal.to_real_add (snorm_ne_top f) (snorm_ne_top g), suffices h_snorm : snorm ⇑(f + g) p μ ≤ snorm ⇑f p μ + snorm ⇑g p μ, { rwa ennreal.to_real_le_to_real (snorm_ne_top (f + g)), exact ennreal.add_ne_top.mpr ⟨snorm_ne_top f, snorm_ne_top g⟩, }, rw [snorm_congr_ae (coe_fn_add _ _)], exact snorm_add_le (Lp.ae_strongly_measurable f) (Lp.ae_strongly_measurable g) hp.1, end, eq_zero_of_map_eq_zero' := λ f, (norm_eq_zero_iff $ ennreal.zero_lt_one.trans_le hp.1).1 } } -- check no diamond is created example [fact (1 ≤ p)] : pseudo_emetric_space.to_has_edist = (Lp.has_edist : has_edist (Lp E p μ)) := rfl section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] lemma mem_Lp_const_smul (c : 𝕜) (f : Lp E p μ) : c • ↑f ∈ Lp E p μ := begin rw [mem_Lp_iff_snorm_lt_top, snorm_congr_ae (ae_eq_fun.coe_fn_smul _ _), snorm_const_smul, ennreal.mul_lt_top_iff], exact or.inl ⟨ennreal.coe_lt_top, f.prop⟩, end variables (E p μ 𝕜) /-- The `𝕜`-submodule of elements of `α →ₘ[μ] E` whose `Lp` norm is finite. This is `Lp E p μ`, with extra structure. -/ def Lp_submodule : submodule 𝕜 (α →ₘ[μ] E) := { smul_mem' := λ c f hf, by simpa using mem_Lp_const_smul c ⟨f, hf⟩, .. Lp E p μ } variables {E p μ 𝕜} lemma coe_Lp_submodule : (Lp_submodule E p μ 𝕜).to_add_subgroup = Lp E p μ := rfl instance : module 𝕜 (Lp E p μ) := { .. (Lp_submodule E p μ 𝕜).module } lemma coe_fn_smul (c : 𝕜) (f : Lp E p μ) : ⇑(c • f) =ᵐ[μ] c • f := ae_eq_fun.coe_fn_smul _ _ lemma norm_const_smul (c : 𝕜) (f : Lp E p μ) : ‖c • f‖ = ‖c‖ * ‖f‖ := by rw [norm_def, snorm_congr_ae (coe_fn_smul _ _), snorm_const_smul c, ennreal.to_real_mul, ennreal.coe_to_real, coe_nnnorm, norm_def] instance [fact (1 ≤ p)] : normed_space 𝕜 (Lp E p μ) := { norm_smul_le := λ _ _, by simp [norm_const_smul] } end normed_space end Lp namespace mem_ℒp variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 E] lemma to_Lp_const_smul {f : α → E} (c : 𝕜) (hf : mem_ℒp f p μ) : (hf.const_smul c).to_Lp (c • f) = c • hf.to_Lp f := rfl end mem_ℒp /-! ### Indicator of a set as an element of Lᵖ For a set `s` with `(hs : measurable_set s)` and `(hμs : μ s < ∞)`, we build `indicator_const_Lp p hs hμs c`, the element of `Lp` corresponding to `s.indicator (λ x, c)`. -/ section indicator variables {s : set α} {hs : measurable_set s} {c : E} {f : α → E} {hf : ae_strongly_measurable f μ} lemma snorm_ess_sup_indicator_le (s : set α) (f : α → G) : snorm_ess_sup (s.indicator f) μ ≤ snorm_ess_sup f μ := begin refine ess_sup_mono_ae (eventually_of_forall (λ x, _)), rw [ennreal.coe_le_coe, nnnorm_indicator_eq_indicator_nnnorm], exact set.indicator_le_self s _ x, end lemma snorm_ess_sup_indicator_const_le (s : set α) (c : G) : snorm_ess_sup (s.indicator (λ x : α , c)) μ ≤ ‖c‖₊ := begin by_cases hμ0 : μ = 0, { rw [hμ0, snorm_ess_sup_measure_zero], exact ennreal.coe_nonneg }, { exact (snorm_ess_sup_indicator_le s (λ x, c)).trans (snorm_ess_sup_const c hμ0).le, }, end lemma snorm_ess_sup_indicator_const_eq (s : set α) (c : G) (hμs : μ s ≠ 0) : snorm_ess_sup (s.indicator (λ x : α , c)) μ = ‖c‖₊ := begin refine le_antisymm (snorm_ess_sup_indicator_const_le s c) _, by_contra' h, have h' := ae_iff.mp (ae_lt_of_ess_sup_lt h), push_neg at h', refine hμs (measure_mono_null (λ x hx_mem, _) h'), rw [set.mem_set_of_eq, set.indicator_of_mem hx_mem], exact le_rfl, end variables (hs) lemma snorm_indicator_le {E : Type*} [normed_add_comm_group E] (f : α → E) : snorm (s.indicator f) p μ ≤ snorm f p μ := begin refine snorm_mono_ae (eventually_of_forall (λ x, _)), suffices : ‖s.indicator f x‖₊ ≤ ‖f x‖₊, { exact nnreal.coe_mono this }, rw nnnorm_indicator_eq_indicator_nnnorm, exact s.indicator_le_self _ x, end variables {hs} lemma snorm_indicator_const {c : G} (hs : measurable_set s) (hp : p ≠ 0) (hp_top : p ≠ ∞) : snorm (s.indicator (λ x, c)) p μ = ‖c‖₊ * (μ s) ^ (1 / p.to_real) := begin have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp hp_top, rw snorm_eq_lintegral_rpow_nnnorm hp hp_top, simp_rw [nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator], have h_indicator_pow : (λ a : α, s.indicator (λ (x : α), (‖c‖₊ : ℝ≥0∞)) a ^ p.to_real) = s.indicator (λ (x : α), ↑‖c‖₊ ^ p.to_real), { rw set.comp_indicator_const (‖c‖₊ : ℝ≥0∞) (λ x, x ^ p.to_real) _, simp [hp_pos], }, rw [h_indicator_pow, lintegral_indicator _ hs, set_lintegral_const, ennreal.mul_rpow_of_nonneg], { rw [← ennreal.rpow_mul, mul_one_div_cancel hp_pos.ne.symm, ennreal.rpow_one], }, { simp [hp_pos.le], }, end lemma snorm_indicator_const' {c : G} (hs : measurable_set s) (hμs : μ s ≠ 0) (hp : p ≠ 0) : snorm (s.indicator (λ _, c)) p μ = ‖c‖₊ * (μ s) ^ (1 / p.to_real) := begin by_cases hp_top : p = ∞, { simp [hp_top, snorm_ess_sup_indicator_const_eq s c hμs], }, { exact snorm_indicator_const hs hp hp_top, }, end lemma mem_ℒp.indicator (hs : measurable_set s) (hf : mem_ℒp f p μ) : mem_ℒp (s.indicator f) p μ := ⟨hf.ae_strongly_measurable.indicator hs, lt_of_le_of_lt (snorm_indicator_le f) hf.snorm_lt_top⟩ lemma snorm_ess_sup_indicator_eq_snorm_ess_sup_restrict {f : α → F} (hs : measurable_set s) : snorm_ess_sup (s.indicator f) μ = snorm_ess_sup f (μ.restrict s) := begin simp_rw [snorm_ess_sup, nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator], by_cases hs_null : μ s = 0, { rw measure.restrict_zero_set hs_null, simp only [ess_sup_measure_zero, ennreal.ess_sup_eq_zero_iff, ennreal.bot_eq_zero], have hs_empty : s =ᵐ[μ] (∅ : set α), by { rw ae_eq_set, simpa using hs_null, }, refine (indicator_ae_eq_of_ae_eq_set hs_empty).trans _, rw set.indicator_empty, refl, }, rw ess_sup_indicator_eq_ess_sup_restrict (eventually_of_forall (λ x, _)) hs hs_null, rw pi.zero_apply, exact zero_le _, end lemma snorm_indicator_eq_snorm_restrict {f : α → F} (hs : measurable_set s) : snorm (s.indicator f) p μ = snorm f p (μ.restrict s) := begin by_cases hp_zero : p = 0, { simp only [hp_zero, snorm_exponent_zero], }, by_cases hp_top : p = ∞, { simp_rw [hp_top, snorm_exponent_top], exact snorm_ess_sup_indicator_eq_snorm_ess_sup_restrict hs, }, simp_rw snorm_eq_lintegral_rpow_nnnorm hp_zero hp_top, suffices : ∫⁻ x, ‖s.indicator f x‖₊ ^ p.to_real ∂μ = ∫⁻ x in s, ‖f x‖₊ ^ p.to_real ∂μ, by rw this, rw ← lintegral_indicator _ hs, congr, simp_rw [nnnorm_indicator_eq_indicator_nnnorm, ennreal.coe_indicator], have h_zero : (λ x, x ^ p.to_real) (0 : ℝ≥0∞) = 0, by simp [ennreal.to_real_pos hp_zero hp_top], exact (set.indicator_comp_of_zero h_zero).symm, end lemma mem_ℒp_indicator_iff_restrict (hs : measurable_set s) : mem_ℒp (s.indicator f) p μ ↔ mem_ℒp f p (μ.restrict s) := by simp [mem_ℒp, ae_strongly_measurable_indicator_iff hs, snorm_indicator_eq_snorm_restrict hs] lemma mem_ℒp_indicator_const (p : ℝ≥0∞) (hs : measurable_set s) (c : E) (hμsc : c = 0 ∨ μ s ≠ ∞) : mem_ℒp (s.indicator (λ _, c)) p μ := begin rw mem_ℒp_indicator_iff_restrict hs, by_cases hp_zero : p = 0, { rw hp_zero, exact mem_ℒp_zero_iff_ae_strongly_measurable.mpr ae_strongly_measurable_const, }, by_cases hp_top : p = ∞, { rw hp_top, exact mem_ℒp_top_of_bound ae_strongly_measurable_const (‖c‖) (eventually_of_forall (λ x, le_rfl)), }, rw [mem_ℒp_const_iff hp_zero hp_top, measure.restrict_apply_univ], cases hμsc, { exact or.inl hμsc, }, { exact or.inr hμsc.lt_top, }, end end indicator section indicator_const_Lp open set function variables {s : set α} {hs : measurable_set s} {hμs : μ s ≠ ∞} {c : E} /-- Indicator of a set as an element of `Lp`. -/ def indicator_const_Lp (p : ℝ≥0∞) (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) : Lp E p μ := mem_ℒp.to_Lp (s.indicator (λ _, c)) (mem_ℒp_indicator_const p hs c (or.inr hμs)) lemma indicator_const_Lp_coe_fn : ⇑(indicator_const_Lp p hs hμs c) =ᵐ[μ] s.indicator (λ _, c) := mem_ℒp.coe_fn_to_Lp (mem_ℒp_indicator_const p hs c (or.inr hμs)) lemma indicator_const_Lp_coe_fn_mem : ∀ᵐ (x : α) ∂μ, x ∈ s → indicator_const_Lp p hs hμs c x = c := indicator_const_Lp_coe_fn.mono (λ x hx hxs, hx.trans (set.indicator_of_mem hxs _)) lemma indicator_const_Lp_coe_fn_nmem : ∀ᵐ (x : α) ∂μ, x ∉ s → indicator_const_Lp p hs hμs c x = 0 := indicator_const_Lp_coe_fn.mono (λ x hx hxs, hx.trans (set.indicator_of_not_mem hxs _)) lemma norm_indicator_const_Lp (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : ‖indicator_const_Lp p hs hμs c‖ = ‖c‖ * (μ s).to_real ^ (1 / p.to_real) := by rw [Lp.norm_def, snorm_congr_ae indicator_const_Lp_coe_fn, snorm_indicator_const hs hp_ne_zero hp_ne_top, ennreal.to_real_mul, ennreal.to_real_rpow, ennreal.coe_to_real, coe_nnnorm] lemma norm_indicator_const_Lp_top (hμs_ne_zero : μ s ≠ 0) : ‖indicator_const_Lp ∞ hs hμs c‖ = ‖c‖ := by rw [Lp.norm_def, snorm_congr_ae indicator_const_Lp_coe_fn, snorm_indicator_const' hs hμs_ne_zero ennreal.top_ne_zero, ennreal.top_to_real, div_zero, ennreal.rpow_zero, mul_one, ennreal.coe_to_real, coe_nnnorm] lemma norm_indicator_const_Lp' (hp_pos : p ≠ 0) (hμs_pos : μ s ≠ 0) : ‖indicator_const_Lp p hs hμs c‖ = ‖c‖ * (μ s).to_real ^ (1 / p.to_real) := begin by_cases hp_top : p = ∞, { rw [hp_top, ennreal.top_to_real, div_zero, real.rpow_zero, mul_one], exact norm_indicator_const_Lp_top hμs_pos, }, { exact norm_indicator_const_Lp hp_pos hp_top, }, end @[simp] lemma indicator_const_empty : indicator_const_Lp p measurable_set.empty (by simp : μ ∅ ≠ ∞) c = 0 := begin rw Lp.eq_zero_iff_ae_eq_zero, convert indicator_const_Lp_coe_fn, simp [set.indicator_empty'], end lemma mem_ℒp_add_of_disjoint {f g : α → E} (h : disjoint (support f) (support g)) (hf : strongly_measurable f) (hg : strongly_measurable g) : mem_ℒp (f + g) p μ ↔ mem_ℒp f p μ ∧ mem_ℒp g p μ := begin borelize E, refine ⟨λ hfg, ⟨_, _⟩, λ h, h.1.add h.2⟩, { rw ← indicator_add_eq_left h, exact hfg.indicator (measurable_set_support hf.measurable) }, { rw ← indicator_add_eq_right h, exact hfg.indicator (measurable_set_support hg.measurable) } end /-- The indicator of a disjoint union of two sets is the sum of the indicators of the sets. -/ lemma indicator_const_Lp_disjoint_union {s t : set α} (hs : measurable_set s) (ht : measurable_set t) (hμs : μ s ≠ ∞) (hμt : μ t ≠ ∞) (hst : s ∩ t = ∅) (c : E) : (indicator_const_Lp p (hs.union ht) ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hμs, hμt⟩))).ne c) = indicator_const_Lp p hs hμs c + indicator_const_Lp p ht hμt c := begin ext1, refine indicator_const_Lp_coe_fn.trans (eventually_eq.trans _ (Lp.coe_fn_add _ _).symm), refine eventually_eq.trans _ (eventually_eq.add indicator_const_Lp_coe_fn.symm indicator_const_Lp_coe_fn.symm), rw set.indicator_union_of_disjoint (set.disjoint_iff_inter_eq_empty.mpr hst) _, end end indicator_const_Lp lemma mem_ℒp.norm_rpow_div {f : α → E} (hf : mem_ℒp f p μ) (q : ℝ≥0∞) : mem_ℒp (λ (x : α), ‖f x‖ ^ q.to_real) (p/q) μ := begin refine ⟨(hf.1.norm.ae_measurable.pow_const q.to_real).ae_strongly_measurable, _⟩, by_cases q_top : q = ∞, { simp [q_top] }, by_cases q_zero : q = 0, { simp [q_zero], by_cases p_zero : p = 0, { simp [p_zero] }, rw ennreal.div_zero p_zero, exact (mem_ℒp_top_const (1 : ℝ)).2 }, rw snorm_norm_rpow _ (ennreal.to_real_pos q_zero q_top), apply ennreal.rpow_lt_top_of_nonneg ennreal.to_real_nonneg, rw [ennreal.of_real_to_real q_top, div_eq_mul_inv, mul_assoc, ennreal.inv_mul_cancel q_zero q_top, mul_one], exact hf.2.ne end lemma mem_ℒp_norm_rpow_iff {q : ℝ≥0∞} {f : α → E} (hf : ae_strongly_measurable f μ) (q_zero : q ≠ 0) (q_top : q ≠ ∞) : mem_ℒp (λ (x : α), ‖f x‖ ^ q.to_real) (p/q) μ ↔ mem_ℒp f p μ := begin refine ⟨λ h, _, λ h, h.norm_rpow_div q⟩, apply (mem_ℒp_norm_iff hf).1, convert h.norm_rpow_div (q⁻¹), { ext x, rw [real.norm_eq_abs, real.abs_rpow_of_nonneg (norm_nonneg _), ← real.rpow_mul (abs_nonneg _), ennreal.to_real_inv, mul_inv_cancel, abs_of_nonneg (norm_nonneg _), real.rpow_one], simp [ennreal.to_real_eq_zero_iff, not_or_distrib, q_zero, q_top] }, { rw [div_eq_mul_inv, inv_inv, div_eq_mul_inv, mul_assoc, ennreal.inv_mul_cancel q_zero q_top, mul_one] } end lemma mem_ℒp.norm_rpow {f : α → E} (hf : mem_ℒp f p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) : mem_ℒp (λ (x : α), ‖f x‖ ^ p.to_real) 1 μ := begin convert hf.norm_rpow_div p, rw [div_eq_mul_inv, ennreal.mul_inv_cancel hp_ne_zero hp_ne_top], end end measure_theory open measure_theory /-! ### Composition on `L^p` We show that Lipschitz functions vanishing at zero act by composition on `L^p`, and specialize this to the composition with continuous linear maps, and to the definition of the positive part of an `L^p` function. -/ section composition variables {g : E → F} {c : ℝ≥0} lemma lipschitz_with.comp_mem_ℒp {α E F} {K} [measurable_space α] {μ : measure α} [normed_add_comm_group E] [normed_add_comm_group F] {f : α → E} {g : E → F} (hg : lipschitz_with K g) (g0 : g 0 = 0) (hL : mem_ℒp f p μ) : mem_ℒp (g ∘ f) p μ := begin have : ∀ᵐ x ∂μ, ‖g (f x)‖ ≤ K * ‖f x‖, { apply filter.eventually_of_forall (λ x, _), rw [← dist_zero_right, ← dist_zero_right, ← g0], apply hg.dist_le_mul }, exact hL.of_le_mul (hg.continuous.comp_ae_strongly_measurable hL.1) this, end lemma measure_theory.mem_ℒp.of_comp_antilipschitz_with {α E F} {K'} [measurable_space α] {μ : measure α} [normed_add_comm_group E] [normed_add_comm_group F] {f : α → E} {g : E → F} (hL : mem_ℒp (g ∘ f) p μ) (hg : uniform_continuous g) (hg' : antilipschitz_with K' g) (g0 : g 0 = 0) : mem_ℒp f p μ := begin have A : ∀ᵐ x ∂μ, ‖f x‖ ≤ K' * ‖g (f x)‖, { apply filter.eventually_of_forall (λ x, _), rw [← dist_zero_right, ← dist_zero_right, ← g0], apply hg'.le_mul_dist }, have B : ae_strongly_measurable f μ := ((hg'.uniform_embedding hg).embedding.ae_strongly_measurable_comp_iff.1 hL.1), exact hL.of_le_mul B A, end namespace lipschitz_with lemma mem_ℒp_comp_iff_of_antilipschitz {α E F} {K K'} [measurable_space α] {μ : measure α} [normed_add_comm_group E] [normed_add_comm_group F] {f : α → E} {g : E → F} (hg : lipschitz_with K g) (hg' : antilipschitz_with K' g) (g0 : g 0 = 0) : mem_ℒp (g ∘ f) p μ ↔ mem_ℒp f p μ := ⟨λ h, h.of_comp_antilipschitz_with hg.uniform_continuous hg' g0, λ h, hg.comp_mem_ℒp g0 h⟩ /-- When `g` is a Lipschitz function sending `0` to `0` and `f` is in `Lp`, then `g ∘ f` is well defined as an element of `Lp`. -/ def comp_Lp (hg : lipschitz_with c g) (g0 : g 0 = 0) (f : Lp E p μ) : Lp F p μ := ⟨ae_eq_fun.comp g hg.continuous (f : α →ₘ[μ] E), begin suffices : ∀ᵐ x ∂μ, ‖ae_eq_fun.comp g hg.continuous (f : α →ₘ[μ] E) x‖ ≤ c * ‖f x‖, { exact Lp.mem_Lp_of_ae_le_mul this }, filter_upwards [ae_eq_fun.coe_fn_comp g hg.continuous (f : α →ₘ[μ] E)] with a ha, simp only [ha], rw [← dist_zero_right, ← dist_zero_right, ← g0], exact hg.dist_le_mul (f a) 0, end⟩ lemma coe_fn_comp_Lp (hg : lipschitz_with c g) (g0 : g 0 = 0) (f : Lp E p μ) : hg.comp_Lp g0 f =ᵐ[μ] g ∘ f := ae_eq_fun.coe_fn_comp _ _ _ @[simp] lemma comp_Lp_zero (hg : lipschitz_with c g) (g0 : g 0 = 0) : hg.comp_Lp g0 (0 : Lp E p μ) = 0 := begin rw Lp.eq_zero_iff_ae_eq_zero, apply (coe_fn_comp_Lp _ _ _).trans, filter_upwards [Lp.coe_fn_zero E p μ] with _ ha, simp [ha, g0], end lemma norm_comp_Lp_sub_le (hg : lipschitz_with c g) (g0 : g 0 = 0) (f f' : Lp E p μ) : ‖hg.comp_Lp g0 f - hg.comp_Lp g0 f'‖ ≤ c * ‖f - f'‖ := begin apply Lp.norm_le_mul_norm_of_ae_le_mul, filter_upwards [hg.coe_fn_comp_Lp g0 f, hg.coe_fn_comp_Lp g0 f', Lp.coe_fn_sub (hg.comp_Lp g0 f) (hg.comp_Lp g0 f'), Lp.coe_fn_sub f f'] with a ha1 ha2 ha3 ha4, simp [ha1, ha2, ha3, ha4, ← dist_eq_norm], exact hg.dist_le_mul (f a) (f' a) end lemma norm_comp_Lp_le (hg : lipschitz_with c g) (g0 : g 0 = 0) (f : Lp E p μ) : ‖hg.comp_Lp g0 f‖ ≤ c * ‖f‖ := by simpa using hg.norm_comp_Lp_sub_le g0 f 0 lemma lipschitz_with_comp_Lp [fact (1 ≤ p)] (hg : lipschitz_with c g) (g0 : g 0 = 0) : lipschitz_with c (hg.comp_Lp g0 : Lp E p μ → Lp F p μ) := lipschitz_with.of_dist_le_mul $ λ f g, by simp [dist_eq_norm, norm_comp_Lp_sub_le] lemma continuous_comp_Lp [fact (1 ≤ p)] (hg : lipschitz_with c g) (g0 : g 0 = 0) : continuous (hg.comp_Lp g0 : Lp E p μ → Lp F p μ) := (lipschitz_with_comp_Lp hg g0).continuous end lipschitz_with namespace continuous_linear_map variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] [normed_space 𝕜 F] /-- Composing `f : Lp ` with `L : E →L[𝕜] F`. -/ def comp_Lp (L : E →L[𝕜] F) (f : Lp E p μ) : Lp F p μ := L.lipschitz.comp_Lp (map_zero L) f lemma coe_fn_comp_Lp (L : E →L[𝕜] F) (f : Lp E p μ) : ∀ᵐ a ∂μ, (L.comp_Lp f) a = L (f a) := lipschitz_with.coe_fn_comp_Lp _ _ _ lemma coe_fn_comp_Lp' (L : E →L[𝕜] F) (f : Lp E p μ) : L.comp_Lp f =ᵐ[μ] λ a, L (f a) := L.coe_fn_comp_Lp f lemma comp_mem_ℒp (L : E →L[𝕜] F) (f : Lp E p μ) : mem_ℒp (L ∘ f) p μ := (Lp.mem_ℒp (L.comp_Lp f)).ae_eq (L.coe_fn_comp_Lp' f) lemma comp_mem_ℒp' (L : E →L[𝕜] F) {f : α → E} (hf : mem_ℒp f p μ) : mem_ℒp (L ∘ f) p μ := (L.comp_mem_ℒp (hf.to_Lp f)).ae_eq (eventually_eq.fun_comp (hf.coe_fn_to_Lp) _) section is_R_or_C variables {K : Type*} [is_R_or_C K] lemma _root_.measure_theory.mem_ℒp.of_real {f : α → ℝ} (hf : mem_ℒp f p μ) : mem_ℒp (λ x, (f x : K)) p μ := (@is_R_or_C.of_real_clm K _).comp_mem_ℒp' hf lemma _root_.measure_theory.mem_ℒp_re_im_iff {f : α → K} : mem_ℒp (λ x, is_R_or_C.re (f x)) p μ ∧ mem_ℒp (λ x, is_R_or_C.im (f x)) p μ ↔ mem_ℒp f p μ := begin refine ⟨_, λ hf, ⟨hf.re, hf.im⟩⟩, rintro ⟨hre, him⟩, convert hre.of_real.add (him.of_real.const_mul is_R_or_C.I), { ext1 x, rw [pi.add_apply, mul_comm, is_R_or_C.re_add_im] }, all_goals { apply_instance } end end is_R_or_C lemma add_comp_Lp (L L' : E →L[𝕜] F) (f : Lp E p μ) : (L + L').comp_Lp f = L.comp_Lp f + L'.comp_Lp f := begin ext1, refine (coe_fn_comp_Lp' (L + L') f).trans _, refine eventually_eq.trans _ (Lp.coe_fn_add _ _).symm, refine eventually_eq.trans _ (eventually_eq.add (L.coe_fn_comp_Lp' f).symm (L'.coe_fn_comp_Lp' f).symm), refine eventually_of_forall (λ x, _), refl, end lemma smul_comp_Lp {𝕜'} [normed_field 𝕜'] [normed_space 𝕜' F] [smul_comm_class 𝕜 𝕜' F] (c : 𝕜') (L : E →L[𝕜] F) (f : Lp E p μ) : (c • L).comp_Lp f = c • L.comp_Lp f := begin ext1, refine (coe_fn_comp_Lp' (c • L) f).trans _, refine eventually_eq.trans _ (Lp.coe_fn_smul _ _).symm, refine (L.coe_fn_comp_Lp' f).mono (λ x hx, _), rw [pi.smul_apply, hx], refl, end lemma norm_comp_Lp_le (L : E →L[𝕜] F) (f : Lp E p μ) : ‖L.comp_Lp f‖ ≤ ‖L‖ * ‖f‖ := lipschitz_with.norm_comp_Lp_le _ _ _ variables (μ p) /-- Composing `f : Lp E p μ` with `L : E →L[𝕜] F`, seen as a `𝕜`-linear map on `Lp E p μ`. -/ def comp_Lpₗ (L : E →L[𝕜] F) : (Lp E p μ) →ₗ[𝕜] (Lp F p μ) := { to_fun := λ f, L.comp_Lp f, map_add' := begin intros f g, ext1, filter_upwards [Lp.coe_fn_add f g, coe_fn_comp_Lp L (f + g), coe_fn_comp_Lp L f, coe_fn_comp_Lp L g, Lp.coe_fn_add (L.comp_Lp f) (L.comp_Lp g)], assume a ha1 ha2 ha3 ha4 ha5, simp only [ha1, ha2, ha3, ha4, ha5, map_add, pi.add_apply], end, map_smul' := begin intros c f, dsimp, ext1, filter_upwards [Lp.coe_fn_smul c f, coe_fn_comp_Lp L (c • f), Lp.coe_fn_smul c (L.comp_Lp f), coe_fn_comp_Lp L f] with _ ha1 ha2 ha3 ha4, simp only [ha1, ha2, ha3, ha4, smul_hom_class.map_smul, pi.smul_apply], end } /-- Composing `f : Lp E p μ` with `L : E →L[𝕜] F`, seen as a continuous `𝕜`-linear map on `Lp E p μ`. See also the similar * `linear_map.comp_left` for functions, * `continuous_linear_map.comp_left_continuous` for continuous functions, * `continuous_linear_map.comp_left_continuous_bounded` for bounded continuous functions, * `continuous_linear_map.comp_left_continuous_compact` for continuous functions on compact spaces. -/ def comp_LpL [fact (1 ≤ p)] (L : E →L[𝕜] F) : (Lp E p μ) →L[𝕜] (Lp F p μ) := linear_map.mk_continuous (L.comp_Lpₗ p μ) ‖L‖ L.norm_comp_Lp_le variables {μ p} lemma coe_fn_comp_LpL [fact (1 ≤ p)] (L : E →L[𝕜] F) (f : Lp E p μ) : L.comp_LpL p μ f =ᵐ[μ] λ a, L (f a) := L.coe_fn_comp_Lp f lemma add_comp_LpL [fact (1 ≤ p)] (L L' : E →L[𝕜] F) : (L + L').comp_LpL p μ = L.comp_LpL p μ + L'.comp_LpL p μ := by { ext1 f, exact add_comp_Lp L L' f } lemma smul_comp_LpL [fact (1 ≤ p)] (c : 𝕜) (L : E →L[𝕜] F) : (c • L).comp_LpL p μ = c • (L.comp_LpL p μ) := by { ext1 f, exact smul_comp_Lp c L f } /-- TODO: written in an "apply" way because of a missing `has_smul` instance. -/ lemma smul_comp_LpL_apply [fact (1 ≤ p)] {𝕜'} [normed_field 𝕜'] [normed_space 𝕜' F] [smul_comm_class 𝕜 𝕜' F] (c : 𝕜') (L : E →L[𝕜] F) (f : Lp E p μ) : (c • L).comp_LpL p μ f = c • (L.comp_LpL p μ f) := smul_comp_Lp c L f lemma norm_compLpL_le [fact (1 ≤ p)] (L : E →L[𝕜] F) : ‖L.comp_LpL p μ‖ ≤ ‖L‖ := linear_map.mk_continuous_norm_le _ (norm_nonneg _) _ end continuous_linear_map namespace measure_theory lemma indicator_const_Lp_eq_to_span_singleton_comp_Lp {s : set α} [normed_space ℝ F] (hs : measurable_set s) (hμs : μ s ≠ ∞) (x : F) : indicator_const_Lp 2 hs hμs x = (continuous_linear_map.to_span_singleton ℝ x).comp_Lp (indicator_const_Lp 2 hs hμs (1 : ℝ)) := begin ext1, refine indicator_const_Lp_coe_fn.trans _, have h_comp_Lp := (continuous_linear_map.to_span_singleton ℝ x).coe_fn_comp_Lp (indicator_const_Lp 2 hs hμs (1 : ℝ)), rw ← eventually_eq at h_comp_Lp, refine eventually_eq.trans _ h_comp_Lp.symm, refine (@indicator_const_Lp_coe_fn _ _ _ 2 μ _ s hs hμs (1 : ℝ)).mono (λ y hy, _), dsimp only, rw hy, simp_rw [continuous_linear_map.to_span_singleton_apply], by_cases hy_mem : y ∈ s; simp [hy_mem, continuous_linear_map.lsmul_apply], end namespace Lp section pos_part lemma lipschitz_with_pos_part : lipschitz_with 1 (λ (x : ℝ), max x 0) := lipschitz_with.of_dist_le_mul $ λ x y, by simp [real.dist_eq, abs_max_sub_max_le_abs] lemma _root_.measure_theory.mem_ℒp.pos_part {f : α → ℝ} (hf : mem_ℒp f p μ) : mem_ℒp (λ x, max (f x) 0) p μ := lipschitz_with_pos_part.comp_mem_ℒp (max_eq_right le_rfl) hf lemma _root_.measure_theory.mem_ℒp.neg_part {f : α → ℝ} (hf : mem_ℒp f p μ) : mem_ℒp (λ x, max (-f x) 0) p μ := lipschitz_with_pos_part.comp_mem_ℒp (max_eq_right le_rfl) hf.neg /-- Positive part of a function in `L^p`. -/ def pos_part (f : Lp ℝ p μ) : Lp ℝ p μ := lipschitz_with_pos_part.comp_Lp (max_eq_right le_rfl) f /-- Negative part of a function in `L^p`. -/ def neg_part (f : Lp ℝ p μ) : Lp ℝ p μ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : Lp ℝ p μ) : (pos_part f : α →ₘ[μ] ℝ) = (f : α →ₘ[μ] ℝ).pos_part := rfl lemma coe_fn_pos_part (f : Lp ℝ p μ) : ⇑(pos_part f) =ᵐ[μ] λ a, max (f a) 0 := ae_eq_fun.coe_fn_pos_part _ lemma coe_fn_neg_part_eq_max (f : Lp ℝ p μ) : ∀ᵐ a ∂μ, neg_part f a = max (- f a) 0 := begin rw neg_part, filter_upwards [coe_fn_pos_part (-f), coe_fn_neg f] with _ h₁ h₂, rw [h₁, h₂, pi.neg_apply], end lemma coe_fn_neg_part (f : Lp ℝ p μ) : ∀ᵐ a ∂μ, neg_part f a = - min (f a) 0 := (coe_fn_neg_part_eq_max f).mono $ assume a h, by rw [h, ← max_neg_neg, neg_zero] lemma continuous_pos_part [fact (1 ≤ p)] : continuous (λf : Lp ℝ p μ, pos_part f) := lipschitz_with.continuous_comp_Lp _ _ lemma continuous_neg_part [fact (1 ≤ p)] : continuous (λf : Lp ℝ p μ, neg_part f) := have eq : (λf : Lp ℝ p μ, neg_part f) = (λf : Lp ℝ p μ, pos_part (-f)) := rfl, by { rw eq, exact continuous_pos_part.comp continuous_neg } end pos_part end Lp end measure_theory end composition /-! ## `L^p` is a complete space We show that `L^p` is a complete space for `1 ≤ p`. -/ section complete_space namespace measure_theory namespace Lp lemma snorm'_lim_eq_lintegral_liminf {ι} [nonempty ι] [linear_order ι] {f : ι → α → G} {p : ℝ} (hp_nonneg : 0 ≤ p) {f_lim : α → G} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm' f_lim p μ = (∫⁻ a, at_top.liminf (λ m, (‖f m a‖₊ : ℝ≥0∞)^p) ∂μ) ^ (1/p) := begin suffices h_no_pow : (∫⁻ a, ‖f_lim a‖₊ ^ p ∂μ) = (∫⁻ a, at_top.liminf (λ m, (‖f m a‖₊ : ℝ≥0∞)^p) ∂μ), { rw [snorm', h_no_pow], }, refine lintegral_congr_ae (h_lim.mono (λ a ha, _)), rw tendsto.liminf_eq, simp_rw [ennreal.coe_rpow_of_nonneg _ hp_nonneg, ennreal.tendsto_coe], refine ((nnreal.continuous_rpow_const hp_nonneg).tendsto (‖f_lim a‖₊)).comp _, exact (continuous_nnnorm.tendsto (f_lim a)).comp ha, end lemma snorm'_lim_le_liminf_snorm' {E} [normed_add_comm_group E] {f : ℕ → α → E} {p : ℝ} (hp_pos : 0 < p) (hf : ∀ n, ae_strongly_measurable (f n) μ) {f_lim : α → E} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm' f_lim p μ ≤ at_top.liminf (λ n, snorm' (f n) p μ) := begin rw snorm'_lim_eq_lintegral_liminf hp_pos.le h_lim, rw [←ennreal.le_rpow_one_div_iff (by simp [hp_pos] : 0 < 1 / p), one_div_one_div], refine (lintegral_liminf_le' (λ m, ((hf m).ennnorm.pow_const _))).trans_eq _, have h_pow_liminf : at_top.liminf (λ n, snorm' (f n) p μ) ^ p = at_top.liminf (λ n, (snorm' (f n) p μ) ^ p), { have h_rpow_mono := ennreal.strict_mono_rpow_of_pos hp_pos, have h_rpow_surj := (ennreal.rpow_left_bijective hp_pos.ne.symm).2, refine (h_rpow_mono.order_iso_of_surjective _ h_rpow_surj).liminf_apply _ _ _ _, all_goals { is_bounded_default }, }, rw h_pow_liminf, simp_rw [snorm', ← ennreal.rpow_mul, one_div, inv_mul_cancel hp_pos.ne.symm, ennreal.rpow_one], end lemma snorm_exponent_top_lim_eq_ess_sup_liminf {ι} [nonempty ι] [linear_order ι] {f : ι → α → G} {f_lim : α → G} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm f_lim ∞ μ = ess_sup (λ x, at_top.liminf (λ m, (‖f m x‖₊ : ℝ≥0∞))) μ := begin rw [snorm_exponent_top, snorm_ess_sup], refine ess_sup_congr_ae (h_lim.mono (λ x hx, _)), rw tendsto.liminf_eq, rw ennreal.tendsto_coe, exact (continuous_nnnorm.tendsto (f_lim x)).comp hx, end lemma snorm_exponent_top_lim_le_liminf_snorm_exponent_top {ι} [nonempty ι] [countable ι] [linear_order ι] {f : ι → α → F} {f_lim : α → F} (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm f_lim ∞ μ ≤ at_top.liminf (λ n, snorm (f n) ∞ μ) := begin rw snorm_exponent_top_lim_eq_ess_sup_liminf h_lim, simp_rw [snorm_exponent_top, snorm_ess_sup], exact ennreal.ess_sup_liminf_le (λ n, (λ x, (‖f n x‖₊ : ℝ≥0∞))), end lemma snorm_lim_le_liminf_snorm {E} [normed_add_comm_group E] {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) (f_lim : α → E) (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : snorm f_lim p μ ≤ at_top.liminf (λ n, snorm (f n) p μ) := begin by_cases hp0 : p = 0, { simp [hp0], }, rw ← ne.def at hp0, by_cases hp_top : p = ∞, { simp_rw [hp_top], exact snorm_exponent_top_lim_le_liminf_snorm_exponent_top h_lim, }, simp_rw snorm_eq_snorm' hp0 hp_top, have hp_pos : 0 < p.to_real, from ennreal.to_real_pos hp0 hp_top, exact snorm'_lim_le_liminf_snorm' hp_pos hf h_lim, end /-! ### `Lp` is complete iff Cauchy sequences of `ℒp` have limits in `ℒp` -/ lemma tendsto_Lp_iff_tendsto_ℒp' {ι} {fi : filter ι} [fact (1 ≤ p)] (f : ι → Lp E p μ) (f_lim : Lp E p μ) : fi.tendsto f (𝓝 f_lim) ↔ fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin rw tendsto_iff_dist_tendsto_zero, simp_rw dist_def, rw [← ennreal.zero_to_real, ennreal.tendsto_to_real_iff (λ n, _) ennreal.zero_ne_top], rw snorm_congr_ae (Lp.coe_fn_sub _ _).symm, exact Lp.snorm_ne_top _, end lemma tendsto_Lp_iff_tendsto_ℒp {ι} {fi : filter ι} [fact (1 ≤ p)] (f : ι → Lp E p μ) (f_lim : α → E) (f_lim_ℒp : mem_ℒp f_lim p μ) : fi.tendsto f (𝓝 (f_lim_ℒp.to_Lp f_lim)) ↔ fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin rw tendsto_Lp_iff_tendsto_ℒp', suffices h_eq : (λ n, snorm (f n - mem_ℒp.to_Lp f_lim f_lim_ℒp) p μ) = (λ n, snorm (f n - f_lim) p μ), by rw h_eq, exact funext (λ n, snorm_congr_ae (eventually_eq.rfl.sub (mem_ℒp.coe_fn_to_Lp f_lim_ℒp))), end lemma tendsto_Lp_iff_tendsto_ℒp'' {ι} {fi : filter ι} [fact (1 ≤ p)] (f : ι → α → E) (f_ℒp : ∀ n, mem_ℒp (f n) p μ) (f_lim : α → E) (f_lim_ℒp : mem_ℒp f_lim p μ) : fi.tendsto (λ n, (f_ℒp n).to_Lp (f n)) (𝓝 (f_lim_ℒp.to_Lp f_lim)) ↔ fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin convert Lp.tendsto_Lp_iff_tendsto_ℒp' _ _, ext1 n, apply snorm_congr_ae, filter_upwards [((f_ℒp n).sub f_lim_ℒp).coe_fn_to_Lp, Lp.coe_fn_sub ((f_ℒp n).to_Lp (f n)) (f_lim_ℒp.to_Lp f_lim)] with _ hx₁ hx₂, rw ← hx₂, exact hx₁.symm, end lemma tendsto_Lp_of_tendsto_ℒp {ι} {fi : filter ι} [hp : fact (1 ≤ p)] {f : ι → Lp E p μ} (f_lim : α → E) (f_lim_ℒp : mem_ℒp f_lim p μ) (h_tendsto : fi.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0)) : fi.tendsto f (𝓝 (f_lim_ℒp.to_Lp f_lim)) := (tendsto_Lp_iff_tendsto_ℒp f f_lim f_lim_ℒp).mpr h_tendsto lemma cauchy_seq_Lp_iff_cauchy_seq_ℒp {ι} [nonempty ι] [semilattice_sup ι] [hp : fact (1 ≤ p)] (f : ι → Lp E p μ) : cauchy_seq f ↔ tendsto (λ (n : ι × ι), snorm (f n.fst - f n.snd) p μ) at_top (𝓝 0) := begin simp_rw [cauchy_seq_iff_tendsto_dist_at_top_0, dist_def], rw [← ennreal.zero_to_real, ennreal.tendsto_to_real_iff (λ n, _) ennreal.zero_ne_top], rw snorm_congr_ae (Lp.coe_fn_sub _ _).symm, exact snorm_ne_top _, end lemma complete_space_Lp_of_cauchy_complete_ℒp [hp : fact (1 ≤ p)] (H : ∀ (f : ℕ → α → E) (hf : ∀ n, mem_ℒp (f n) p μ) (B : ℕ → ℝ≥0∞) (hB : ∑' i, B i < ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N), ∃ (f_lim : α → E) (hf_lim_meas : mem_ℒp f_lim p μ), at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0)) : complete_space (Lp E p μ) := begin let B := λ n : ℕ, ((1:ℝ) / 2) ^ n, have hB_pos : ∀ n, 0 < B n, from λ n, pow_pos (div_pos zero_lt_one zero_lt_two) n, refine metric.complete_of_convergent_controlled_sequences B hB_pos (λ f hf, _), rsuffices ⟨f_lim, hf_lim_meas, h_tendsto⟩ : ∃ (f_lim : α → E) (hf_lim_meas : mem_ℒp f_lim p μ), at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0), { exact ⟨hf_lim_meas.to_Lp f_lim, tendsto_Lp_of_tendsto_ℒp f_lim hf_lim_meas h_tendsto⟩, }, have hB : summable B, from summable_geometric_two, cases hB with M hB, let B1 := λ n, ennreal.of_real (B n), have hB1_has : has_sum B1 (ennreal.of_real M), { have h_tsum_B1 : ∑' i, B1 i = (ennreal.of_real M), { change (∑' (n : ℕ), ennreal.of_real (B n)) = ennreal.of_real M, rw ←hB.tsum_eq, exact (ennreal.of_real_tsum_of_nonneg (λ n, le_of_lt (hB_pos n)) hB.summable).symm, }, have h_sum := (@ennreal.summable _ B1).has_sum, rwa h_tsum_B1 at h_sum, }, have hB1 : ∑' i, B1 i < ∞, by {rw hB1_has.tsum_eq, exact ennreal.of_real_lt_top, }, let f1 : ℕ → α → E := λ n, f n, refine H f1 (λ n, Lp.mem_ℒp (f n)) B1 hB1 (λ N n m hn hm, _), specialize hf N n m hn hm, rw dist_def at hf, simp_rw [f1, B1], rwa ennreal.lt_of_real_iff_to_real_lt, rw snorm_congr_ae (Lp.coe_fn_sub _ _).symm, exact Lp.snorm_ne_top _, end /-! ### Prove that controlled Cauchy sequences of `ℒp` have limits in `ℒp` -/ private lemma snorm'_sum_norm_sub_le_tsum_of_cauchy_snorm' {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm' (f n - f m) p μ < B N) (n : ℕ) : snorm' (λ x, ∑ i in finset.range (n + 1), ‖f (i + 1) x - f i x‖) p μ ≤ ∑' i, B i := begin let f_norm_diff := λ i x, ‖f (i + 1) x - f i x‖, have hgf_norm_diff : ∀ n, (λ x, ∑ i in finset.range (n + 1), ‖f (i + 1) x - f i x‖) = ∑ i in finset.range (n + 1), f_norm_diff i, from λ n, funext (λ x, by simp [f_norm_diff]), rw hgf_norm_diff, refine (snorm'_sum_le (λ i _, ((hf (i+1)).sub (hf i)).norm) hp1).trans _, simp_rw [←pi.sub_apply, snorm'_norm], refine (finset.sum_le_sum _).trans (sum_le_tsum _ (λ m _, zero_le _) ennreal.summable), exact λ m _, (h_cau m (m + 1) m (nat.le_succ m) (le_refl m)).le, end private lemma lintegral_rpow_sum_coe_nnnorm_sub_le_rpow_tsum {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (n : ℕ) (hn : snorm' (λ x, ∑ i in finset.range (n + 1), ‖f (i + 1) x - f i x‖) p μ ≤ ∑' i, B i) : ∫⁻ a, (∑ i in finset.range (n + 1), ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p := begin have hp_pos : 0 < p := zero_lt_one.trans_le hp1, rw [←one_div_one_div p, @ennreal.le_rpow_one_div_iff _ _ (1/p) (by simp [hp_pos]), one_div_one_div p], simp_rw snorm' at hn, have h_nnnorm_nonneg : (λ a, (‖∑ i in finset.range (n + 1), ‖f (i + 1) a - f i a‖‖₊ : ℝ≥0∞) ^ p) = λ a, (∑ i in finset.range (n + 1), (‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)) ^ p, { ext1 a, congr, simp_rw ←of_real_norm_eq_coe_nnnorm, rw ←ennreal.of_real_sum_of_nonneg, { rw real.norm_of_nonneg _, exact finset.sum_nonneg (λ x hx, norm_nonneg _), }, { exact λ x hx, norm_nonneg _, }, }, change (∫⁻ a, (λ x, ↑‖∑ i in finset.range (n + 1), ‖f (i+1) x - f i x‖‖₊^p) a ∂μ)^(1/p) ≤ ∑' i, B i at hn, rwa h_nnnorm_nonneg at hn, end private lemma lintegral_rpow_tsum_coe_nnnorm_sub_le_tsum {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (h : ∀ n, ∫⁻ a, (∑ i in finset.range (n + 1), ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p) : (∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ) ^ (1/p) ≤ ∑' i, B i := begin have hp_pos : 0 < p := zero_lt_one.trans_le hp1, suffices h_pow : ∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p, by rwa [←ennreal.le_rpow_one_div_iff (by simp [hp_pos] : 0 < 1 / p), one_div_one_div], have h_tsum_1 : ∀ g : ℕ → ℝ≥0∞, ∑' i, g i = at_top.liminf (λ n, ∑ i in finset.range (n + 1), g i), by { intro g, rw [ennreal.tsum_eq_liminf_sum_nat, ← liminf_nat_add _ 1], }, simp_rw h_tsum_1 _, rw ← h_tsum_1, have h_liminf_pow : ∫⁻ a, at_top.liminf (λ n, ∑ i in finset.range (n + 1), (‖f (i + 1) a - f i a‖₊))^p ∂μ = ∫⁻ a, at_top.liminf (λ n, (∑ i in finset.range (n + 1), (‖f (i + 1) a - f i a‖₊))^p) ∂μ, { refine lintegral_congr (λ x, _), have h_rpow_mono := ennreal.strict_mono_rpow_of_pos (zero_lt_one.trans_le hp1), have h_rpow_surj := (ennreal.rpow_left_bijective hp_pos.ne.symm).2, refine (h_rpow_mono.order_iso_of_surjective _ h_rpow_surj).liminf_apply _ _ _ _, all_goals { is_bounded_default }, }, rw h_liminf_pow, refine (lintegral_liminf_le' _).trans _, { exact λ n, (finset.ae_measurable_sum (finset.range (n+1)) (λ i _, ((hf (i+1)).sub (hf i)).ennnorm)).pow_const _, }, { exact liminf_le_of_frequently_le' (frequently_of_forall h), }, end private lemma tsum_nnnorm_sub_ae_lt_top {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) {p : ℝ} (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h : (∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ) ^ (1/p) ≤ ∑' i, B i) : ∀ᵐ x ∂μ, (∑' i, ‖f (i + 1) x - f i x‖₊ : ℝ≥0∞) < ∞ := begin have hp_pos : 0 < p := zero_lt_one.trans_le hp1, have h_integral : ∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ < ∞, { have h_tsum_lt_top : (∑' i, B i) ^ p < ∞, from ennreal.rpow_lt_top_of_nonneg hp_pos.le hB, refine lt_of_le_of_lt _ h_tsum_lt_top, rwa [←ennreal.le_rpow_one_div_iff (by simp [hp_pos] : 0 < 1 / p), one_div_one_div] at h, }, have rpow_ae_lt_top : ∀ᵐ x ∂μ, (∑' i, ‖f (i + 1) x - f i x‖₊ : ℝ≥0∞)^p < ∞, { refine ae_lt_top' (ae_measurable.pow_const _ _) h_integral.ne, exact ae_measurable.ennreal_tsum (λ n, ((hf (n+1)).sub (hf n)).ennnorm), }, refine rpow_ae_lt_top.mono (λ x hx, _), rwa [←ennreal.lt_rpow_one_div_iff hp_pos, ennreal.top_rpow_of_pos (by simp [hp_pos] : 0 < 1 / p)] at hx, end lemma ae_tendsto_of_cauchy_snorm' [complete_space E] {f : ℕ → α → E} {p : ℝ} (hf : ∀ n, ae_strongly_measurable (f n) μ) (hp1 : 1 ≤ p) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm' (f n - f m) p μ < B N) : ∀ᵐ x ∂μ, ∃ l : E, at_top.tendsto (λ n, f n x) (𝓝 l) := begin have h_summable : ∀ᵐ x ∂μ, summable (λ (i : ℕ), f (i + 1) x - f i x), { have h1 : ∀ n, snorm' (λ x, ∑ i in finset.range (n + 1), ‖f (i + 1) x - f i x‖) p μ ≤ ∑' i, B i, from snorm'_sum_norm_sub_le_tsum_of_cauchy_snorm' hf hp1 h_cau, have h2 : ∀ n, ∫⁻ a, (∑ i in finset.range (n + 1), ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ ≤ (∑' i, B i) ^ p, from λ n, lintegral_rpow_sum_coe_nnnorm_sub_le_rpow_tsum hf hp1 n (h1 n), have h3 : (∫⁻ a, (∑' i, ‖f (i + 1) a - f i a‖₊ : ℝ≥0∞)^p ∂μ) ^ (1/p) ≤ ∑' i, B i, from lintegral_rpow_tsum_coe_nnnorm_sub_le_tsum hf hp1 h2, have h4 : ∀ᵐ x ∂μ, (∑' i, ‖f (i + 1) x - f i x‖₊ : ℝ≥0∞) < ∞, from tsum_nnnorm_sub_ae_lt_top hf hp1 hB h3, exact h4.mono (λ x hx, summable_of_summable_nnnorm (ennreal.tsum_coe_ne_top_iff_summable.mp (lt_top_iff_ne_top.mp hx))), }, have h : ∀ᵐ x ∂μ, ∃ l : E, at_top.tendsto (λ n, ∑ i in finset.range n, (f (i + 1) x - f i x)) (𝓝 l), { refine h_summable.mono (λ x hx, _), let hx_sum := hx.has_sum.tendsto_sum_nat, exact ⟨∑' i, (f (i + 1) x - f i x), hx_sum⟩, }, refine h.mono (λ x hx, _), cases hx with l hx, have h_rw_sum : (λ n, ∑ i in finset.range n, (f (i + 1) x - f i x)) = λ n, f n x - f 0 x, { ext1 n, change ∑ (i : ℕ) in finset.range n, ((λ m, f m x) (i + 1) - (λ m, f m x) i) = f n x - f 0 x, rw finset.sum_range_sub, }, rw h_rw_sum at hx, have hf_rw : (λ n, f n x) = λ n, f n x - f 0 x + f 0 x, by { ext1 n, abel, }, rw hf_rw, exact ⟨l + f 0 x, tendsto.add_const _ hx⟩, end lemma ae_tendsto_of_cauchy_snorm [complete_space E] {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) (hp : 1 ≤ p) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N) : ∀ᵐ x ∂μ, ∃ l : E, at_top.tendsto (λ n, f n x) (𝓝 l) := begin by_cases hp_top : p = ∞, { simp_rw [hp_top] at *, have h_cau_ae : ∀ᵐ x ∂μ, ∀ N n m, N ≤ n → N ≤ m → (‖(f n - f m) x‖₊ : ℝ≥0∞) < B N, { simp_rw ae_all_iff, exact λ N n m hnN hmN, ae_lt_of_ess_sup_lt (h_cau N n m hnN hmN), }, simp_rw [snorm_exponent_top, snorm_ess_sup] at h_cau, refine h_cau_ae.mono (λ x hx, cauchy_seq_tendsto_of_complete _), refine cauchy_seq_of_le_tendsto_0 (λ n, (B n).to_real) _ _, { intros n m N hnN hmN, specialize hx N n m hnN hmN, rw [dist_eq_norm, ←ennreal.to_real_of_real (norm_nonneg _), ennreal.to_real_le_to_real ennreal.of_real_ne_top (ennreal.ne_top_of_tsum_ne_top hB N)], rw ←of_real_norm_eq_coe_nnnorm at hx, exact hx.le, }, { rw ← ennreal.zero_to_real, exact tendsto.comp (ennreal.tendsto_to_real ennreal.zero_ne_top) (ennreal.tendsto_at_top_zero_of_tsum_ne_top hB), }, }, have hp1 : 1 ≤ p.to_real, { rw [← ennreal.of_real_le_iff_le_to_real hp_top, ennreal.of_real_one], exact hp, }, have h_cau' : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm' (f n - f m) (p.to_real) μ < B N, { intros N n m hn hm, specialize h_cau N n m hn hm, rwa snorm_eq_snorm' (ennreal.zero_lt_one.trans_le hp).ne.symm hp_top at h_cau, }, exact ae_tendsto_of_cauchy_snorm' hf hp1 hB h_cau', end lemma cauchy_tendsto_of_tendsto {f : ℕ → α → E} (hf : ∀ n, ae_strongly_measurable (f n) μ) (f_lim : α → E) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N) (h_lim : ∀ᵐ (x : α) ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x))) : at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin rw ennreal.tendsto_at_top_zero, intros ε hε, have h_B : ∃ (N : ℕ), B N ≤ ε, { suffices h_tendsto_zero : ∃ (N : ℕ), ∀ n : ℕ, N ≤ n → B n ≤ ε, from ⟨h_tendsto_zero.some, h_tendsto_zero.some_spec _ le_rfl⟩, exact (ennreal.tendsto_at_top_zero.mp (ennreal.tendsto_at_top_zero_of_tsum_ne_top hB)) ε hε, }, cases h_B with N h_B, refine ⟨N, λ n hn, _⟩, have h_sub : snorm (f n - f_lim) p μ ≤ at_top.liminf (λ m, snorm (f n - f m) p μ), { refine snorm_lim_le_liminf_snorm (λ m, (hf n).sub (hf m)) (f n - f_lim) _, refine h_lim.mono (λ x hx, _), simp_rw sub_eq_add_neg, exact tendsto.add tendsto_const_nhds (tendsto.neg hx), }, refine h_sub.trans _, refine liminf_le_of_frequently_le' (frequently_at_top.mpr _), refine λ N1, ⟨max N N1, le_max_right _ _, _⟩, exact (h_cau N n (max N N1) hn (le_max_left _ _)).le.trans h_B, end lemma mem_ℒp_of_cauchy_tendsto (hp : 1 ≤ p) {f : ℕ → α → E} (hf : ∀ n, mem_ℒp (f n) p μ) (f_lim : α → E) (h_lim_meas : ae_strongly_measurable f_lim μ) (h_tendsto : at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0)) : mem_ℒp f_lim p μ := begin refine ⟨h_lim_meas, _⟩, rw ennreal.tendsto_at_top_zero at h_tendsto, cases (h_tendsto 1 ennreal.zero_lt_one) with N h_tendsto_1, specialize h_tendsto_1 N (le_refl N), have h_add : f_lim = f_lim - f N + f N, by abel, rw h_add, refine lt_of_le_of_lt (snorm_add_le (h_lim_meas.sub (hf N).1) (hf N).1 hp) _, rw ennreal.add_lt_top, split, { refine lt_of_le_of_lt _ ennreal.one_lt_top, have h_neg : f_lim - f N = -(f N - f_lim), by simp, rwa [h_neg, snorm_neg], }, { exact (hf N).2, }, end lemma cauchy_complete_ℒp [complete_space E] (hp : 1 ≤ p) {f : ℕ → α → E} (hf : ∀ n, mem_ℒp (f n) p μ) {B : ℕ → ℝ≥0∞} (hB : ∑' i, B i ≠ ∞) (h_cau : ∀ (N n m : ℕ), N ≤ n → N ≤ m → snorm (f n - f m) p μ < B N) : ∃ (f_lim : α → E) (hf_lim_meas : mem_ℒp f_lim p μ), at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0) := begin obtain ⟨f_lim, h_f_lim_meas, h_lim⟩ : ∃ (f_lim : α → E) (hf_lim_meas : strongly_measurable f_lim), ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (nhds (f_lim x)), from exists_strongly_measurable_limit_of_tendsto_ae (λ n, (hf n).1) (ae_tendsto_of_cauchy_snorm (λ n, (hf n).1) hp hB h_cau), have h_tendsto' : at_top.tendsto (λ n, snorm (f n - f_lim) p μ) (𝓝 0), from cauchy_tendsto_of_tendsto (λ m, (hf m).1) f_lim hB h_cau h_lim, have h_ℒp_lim : mem_ℒp f_lim p μ, from mem_ℒp_of_cauchy_tendsto hp hf f_lim h_f_lim_meas.ae_strongly_measurable h_tendsto', exact ⟨f_lim, h_ℒp_lim, h_tendsto'⟩, end /-! ### `Lp` is complete for `1 ≤ p` -/ instance [complete_space E] [hp : fact (1 ≤ p)] : complete_space (Lp E p μ) := complete_space_Lp_of_cauchy_complete_ℒp $ λ f hf B hB h_cau, cauchy_complete_ℒp hp.elim hf hB.ne h_cau end Lp end measure_theory end complete_space /-! ### Continuous functions in `Lp` -/ open_locale bounded_continuous_function open bounded_continuous_function section variables [topological_space α] [borel_space α] [second_countable_topology_either α E] variables (E p μ) /-- An additive subgroup of `Lp E p μ`, consisting of the equivalence classes which contain a bounded continuous representative. -/ def measure_theory.Lp.bounded_continuous_function : add_subgroup (Lp E p μ) := add_subgroup.add_subgroup_of ((continuous_map.to_ae_eq_fun_add_hom μ).comp (to_continuous_map_add_hom α E)).range (Lp E p μ) variables {E p μ} /-- By definition, the elements of `Lp.bounded_continuous_function E p μ` are the elements of `Lp E p μ` which contain a bounded continuous representative. -/ lemma measure_theory.Lp.mem_bounded_continuous_function_iff {f : (Lp E p μ)} : f ∈ measure_theory.Lp.bounded_continuous_function E p μ ↔ ∃ f₀ : (α →ᵇ E), f₀.to_continuous_map.to_ae_eq_fun μ = (f : α →ₘ[μ] E) := add_subgroup.mem_add_subgroup_of namespace bounded_continuous_function variables [is_finite_measure μ] /-- A bounded continuous function on a finite-measure space is in `Lp`. -/ lemma mem_Lp (f : α →ᵇ E) : f.to_continuous_map.to_ae_eq_fun μ ∈ Lp E p μ := begin refine Lp.mem_Lp_of_ae_bound (‖f‖) _, filter_upwards [f.to_continuous_map.coe_fn_to_ae_eq_fun μ] with x _, convert f.norm_coe_le_norm x end /-- The `Lp`-norm of a bounded continuous function is at most a constant (depending on the measure of the whole space) times its sup-norm. -/ lemma Lp_norm_le (f : α →ᵇ E) : ‖(⟨f.to_continuous_map.to_ae_eq_fun μ, mem_Lp f⟩ : Lp E p μ)‖ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ * ‖f‖ := begin apply Lp.norm_le_of_ae_bound (norm_nonneg f), { refine (f.to_continuous_map.coe_fn_to_ae_eq_fun μ).mono _, intros x hx, convert f.norm_coe_le_norm x }, { apply_instance } end variables (p μ) /-- The normed group homomorphism of considering a bounded continuous function on a finite-measure space as an element of `Lp`. -/ def to_Lp_hom [fact (1 ≤ p)] : normed_add_group_hom (α →ᵇ E) (Lp E p μ) := { bound' := ⟨_, Lp_norm_le⟩, .. add_monoid_hom.cod_restrict ((continuous_map.to_ae_eq_fun_add_hom μ).comp (to_continuous_map_add_hom α E)) (Lp E p μ) mem_Lp } lemma range_to_Lp_hom [fact (1 ≤ p)] : ((to_Lp_hom p μ).range : add_subgroup (Lp E p μ)) = measure_theory.Lp.bounded_continuous_function E p μ := begin symmetry, convert add_monoid_hom.add_subgroup_of_range_eq_of_le ((continuous_map.to_ae_eq_fun_add_hom μ).comp (to_continuous_map_add_hom α E)) (by { rintros - ⟨f, rfl⟩, exact mem_Lp f } : _ ≤ Lp E p μ), end variables (𝕜 : Type*) /-- The bounded linear map of considering a bounded continuous function on a finite-measure space as an element of `Lp`. -/ def to_Lp [normed_field 𝕜] [normed_space 𝕜 E] [fact (1 ≤ p)] : (α →ᵇ E) →L[𝕜] (Lp E p μ) := linear_map.mk_continuous (linear_map.cod_restrict (Lp.Lp_submodule E p μ 𝕜) ((continuous_map.to_ae_eq_fun_linear_map μ).comp (to_continuous_map_linear_map α E 𝕜)) mem_Lp) _ Lp_norm_le variables {𝕜} lemma range_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] [fact (1 ≤ p)] : ((linear_map.range (to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] Lp E p μ)).to_add_subgroup) = measure_theory.Lp.bounded_continuous_function E p μ := range_to_Lp_hom p μ variables {p} lemma coe_fn_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] [fact (1 ≤ p)] (f : α →ᵇ E) : to_Lp p μ 𝕜 f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk f _ lemma to_Lp_norm_le [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] [fact (1 ≤ p)] : ‖(to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] (Lp E p μ))‖ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ := linear_map.mk_continuous_norm_le _ ((measure_univ_nnreal μ) ^ (p.to_real)⁻¹).coe_nonneg _ end bounded_continuous_function namespace continuous_map variables [compact_space α] [is_finite_measure μ] variables (𝕜 : Type*) (p μ) [fact (1 ≤ p)] /-- The bounded linear map of considering a continuous function on a compact finite-measure space `α` as an element of `Lp`. By definition, the norm on `C(α, E)` is the sup-norm, transferred from the space `α →ᵇ E` of bounded continuous functions, so this construction is just a matter of transferring the structure from `bounded_continuous_function.to_Lp` along the isometry. -/ def to_Lp [normed_field 𝕜] [normed_space 𝕜 E] : C(α, E) →L[𝕜] (Lp E p μ) := (bounded_continuous_function.to_Lp p μ 𝕜).comp (linear_isometry_bounded_of_compact α E 𝕜).to_linear_isometry.to_continuous_linear_map variables {𝕜} lemma range_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] : (linear_map.range (to_Lp p μ 𝕜 : C(α, E) →L[𝕜] Lp E p μ)).to_add_subgroup = measure_theory.Lp.bounded_continuous_function E p μ := begin refine set_like.ext' _, have := (linear_isometry_bounded_of_compact α E 𝕜).surjective, convert function.surjective.range_comp this (bounded_continuous_function.to_Lp p μ 𝕜), rw ←bounded_continuous_function.range_to_Lp p μ, refl, end variables {p} lemma coe_fn_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] (f : C(α, E)) : to_Lp p μ 𝕜 f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk f _ lemma to_Lp_def [normed_field 𝕜] [normed_space 𝕜 E] (f : C(α, E)) : to_Lp p μ 𝕜 f = bounded_continuous_function.to_Lp p μ 𝕜 (linear_isometry_bounded_of_compact α E 𝕜 f) := rfl @[simp] lemma to_Lp_comp_to_continuous_map [normed_field 𝕜] [normed_space 𝕜 E] (f : α →ᵇ E) : to_Lp p μ 𝕜 f.to_continuous_map = bounded_continuous_function.to_Lp p μ 𝕜 f := rfl @[simp] lemma coe_to_Lp [normed_field 𝕜] [normed_space 𝕜 E] (f : C(α, E)) : (to_Lp p μ 𝕜 f : α →ₘ[μ] E) = f.to_ae_eq_fun μ := rfl variables [nontrivially_normed_field 𝕜] [normed_space 𝕜 E] lemma to_Lp_norm_eq_to_Lp_norm_coe : ‖(to_Lp p μ 𝕜 : C(α, E) →L[𝕜] (Lp E p μ))‖ = ‖(bounded_continuous_function.to_Lp p μ 𝕜 : (α →ᵇ E) →L[𝕜] (Lp E p μ))‖ := continuous_linear_map.op_norm_comp_linear_isometry_equiv _ _ /-- Bound for the operator norm of `continuous_map.to_Lp`. -/ lemma to_Lp_norm_le : ‖(to_Lp p μ 𝕜 : C(α, E) →L[𝕜] (Lp E p μ))‖ ≤ (measure_univ_nnreal μ) ^ (p.to_real)⁻¹ := by { rw to_Lp_norm_eq_to_Lp_norm_coe, exact bounded_continuous_function.to_Lp_norm_le μ } end continuous_map end namespace measure_theory namespace Lp lemma pow_mul_meas_ge_le_norm (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (ε : ℝ≥0∞) : (ε * μ {x | ε ≤ ‖f x‖₊ ^ p.to_real}) ^ (1 / p.to_real) ≤ (ennreal.of_real ‖f‖) := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ pow_mul_meas_ge_le_snorm μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) ε lemma mul_meas_ge_le_pow_norm (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (ε : ℝ≥0∞) : ε * μ {x | ε ≤ ‖f x‖₊ ^ p.to_real} ≤ (ennreal.of_real ‖f‖) ^ p.to_real := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ mul_meas_ge_le_pow_snorm μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) ε /-- A version of Markov's inequality with elements of Lp. -/ lemma mul_meas_ge_le_pow_norm' (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (ε : ℝ≥0∞) : ε ^ p.to_real * μ {x | ε ≤ ‖f x‖₊} ≤ (ennreal.of_real ‖f‖) ^ p.to_real := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ mul_meas_ge_le_pow_snorm' μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) ε lemma meas_ge_le_mul_pow_norm (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : μ {x | ε ≤ ‖f x‖₊} ≤ ε⁻¹ ^ p.to_real * (ennreal.of_real ‖f‖) ^ p.to_real := (ennreal.of_real_to_real (snorm_ne_top f)).symm ▸ meas_ge_le_mul_pow_snorm μ hp_ne_zero hp_ne_top (Lp.ae_strongly_measurable f) hε end Lp end measure_theory
bda6e0e9811756b7cbf41843748e6e46cec9c74d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/number_theory/arithmetic_function_auto.lean
e48ecb4ad3b397a831b40d9f7955d85ef6e31f2f
[]
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
21,579
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.big_operators.ring import Mathlib.number_theory.divisors import Mathlib.algebra.squarefree import Mathlib.algebra.invertible import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Arithmetic Functions and Dirichlet Convolution This file defines arithmetic functions, which are functions from `ℕ` to a specified type that map 0 to 0. In the literature, they are often instead defined as functions from `ℕ+`. These arithmetic functions are endowed with a multiplication, given by Dirichlet convolution, and pointwise addition, to form the Dirichlet ring. ## Main Definitions * `arithmetic_function R` consists of functions `f : ℕ → R` such that `f 0 = 0`. * An arithmetic function `f` `is_multiplicative` when `x.coprime y → f (x * y) = f x * f y`. * The pointwise operations `pmul` and `ppow` differ from the multiplication and power instances on `arithmetic_function R`, which use Dirichlet multiplication. * `ζ` is the arithmetic function such that `ζ x = 1` for `0 < x`. * `σ k` is the arithmetic function such that `σ k x = ∑ y in divisors x, y ^ k` for `0 < x`. * `pow k` is the arithmetic function such that `pow k x = x ^ k` for `0 < x`. * `id` is the identity arithmetic function on `ℕ`. * `ω n` is the number of distinct prime factors of `n`. * `Ω n` is the number of prime factors of `n` counted with multiplicity. * `μ` is the Möbius function. ## Main Results * Several forms of Möbius inversion: * `sum_eq_iff_sum_mul_moebius_eq` for functions to a `comm_ring` * `sum_eq_iff_sum_smul_moebius_eq` for functions to an `add_comm_group` * `prod_eq_iff_prod_pow_moebius_eq` for functions to a `comm_group` * `prod_eq_iff_prod_pow_moebius_eq_of_nonzero` for functions to a `comm_group_with_zero` ## Notation The arithmetic functions `ζ` and `σ` have Greek letter names, which are localized notation in the namespace `arithmetic_function`. ## Tags arithmetic functions, dirichlet convolution, divisors -/ namespace nat /-- An arithmetic function is a function from `ℕ` that maps 0 to 0. In the literature, they are often instead defined as functions from `ℕ+`. Multiplication on `arithmetic_functions` is by Dirichlet convolution. -/ def arithmetic_function (R : Type u_1) [HasZero R] := zero_hom ℕ R namespace arithmetic_function @[simp] theorem to_fun_eq {R : Type u_1} [HasZero R] (f : arithmetic_function R) : zero_hom.to_fun f = ⇑f := rfl @[simp] theorem map_zero {R : Type u_1} [HasZero R] {f : arithmetic_function R} : coe_fn f 0 = 0 := zero_hom.map_zero' f theorem coe_inj {R : Type u_1} [HasZero R] {f : arithmetic_function R} {g : arithmetic_function R} : ⇑f = ⇑g ↔ f = g := { mp := fun (h : ⇑f = ⇑g) => zero_hom.coe_inj h, mpr := fun (h : f = g) => h ▸ rfl } @[simp] theorem zero_apply {R : Type u_1} [HasZero R] {x : ℕ} : coe_fn 0 x = 0 := zero_hom.zero_apply x theorem ext {R : Type u_1} [HasZero R] {f : arithmetic_function R} {g : arithmetic_function R} (h : ∀ (x : ℕ), coe_fn f x = coe_fn g x) : f = g := zero_hom.ext h theorem ext_iff {R : Type u_1} [HasZero R] {f : arithmetic_function R} {g : arithmetic_function R} : f = g ↔ ∀ (x : ℕ), coe_fn f x = coe_fn g x := zero_hom.ext_iff protected instance has_one {R : Type u_1} [HasZero R] [HasOne R] : HasOne (arithmetic_function R) := { one := zero_hom.mk (fun (x : ℕ) => ite (x = 1) 1 0) sorry } @[simp] theorem one_one {R : Type u_1} [HasZero R] [HasOne R] : coe_fn 1 1 = 1 := rfl @[simp] theorem one_apply_ne {R : Type u_1} [HasZero R] [HasOne R] {x : ℕ} (h : x ≠ 1) : coe_fn 1 x = 0 := if_neg h protected instance nat_coe {R : Type u_1} [HasZero R] [HasOne R] [Add R] : has_coe (arithmetic_function ℕ) (arithmetic_function R) := has_coe.mk fun (f : arithmetic_function ℕ) => zero_hom.mk ↑⇑f sorry @[simp] theorem nat_coe_nat (f : arithmetic_function ℕ) : ↑f = f := ext fun (_x : ℕ) => cast_id (coe_fn f _x) @[simp] theorem nat_coe_apply {R : Type u_1} [HasZero R] [HasOne R] [Add R] {f : arithmetic_function ℕ} {x : ℕ} : coe_fn (↑f) x = ↑(coe_fn f x) := rfl protected instance int_coe {R : Type u_1} [HasZero R] [HasOne R] [Add R] [Neg R] : has_coe (arithmetic_function ℤ) (arithmetic_function R) := has_coe.mk fun (f : arithmetic_function ℤ) => zero_hom.mk ↑⇑f sorry @[simp] theorem int_coe_int (f : arithmetic_function ℤ) : ↑f = f := ext fun (_x : ℕ) => int.cast_id (coe_fn f _x) @[simp] theorem int_coe_apply {R : Type u_1} [HasZero R] [HasOne R] [Add R] [Neg R] {f : arithmetic_function ℤ} {x : ℕ} : coe_fn (↑f) x = ↑(coe_fn f x) := rfl @[simp] theorem coe_coe {R : Type u_1} [HasZero R] [HasOne R] [Add R] [Neg R] {f : arithmetic_function ℕ} : ↑↑f = ↑f := sorry protected instance has_add {R : Type u_1} [add_monoid R] : Add (arithmetic_function R) := { add := fun (f g : arithmetic_function R) => zero_hom.mk (fun (n : ℕ) => coe_fn f n + coe_fn g n) sorry } @[simp] theorem add_apply {R : Type u_1} [add_monoid R] {f : arithmetic_function R} {g : arithmetic_function R} {n : ℕ} : coe_fn (f + g) n = coe_fn f n + coe_fn g n := rfl protected instance add_monoid {R : Type u_1} [add_monoid R] : add_monoid (arithmetic_function R) := add_monoid.mk Add.add sorry 0 sorry sorry protected instance add_comm_monoid {R : Type u_1} [add_comm_monoid R] : add_comm_monoid (arithmetic_function R) := add_comm_monoid.mk add_monoid.add sorry add_monoid.zero sorry sorry sorry protected instance add_group {R : Type u_1} [add_group R] : add_group (arithmetic_function R) := add_group.mk add_monoid.add sorry add_monoid.zero sorry sorry (fun (f : arithmetic_function R) => zero_hom.mk (fun (n : ℕ) => -coe_fn f n) sorry) (sub_neg_monoid.sub._default add_monoid.add sorry add_monoid.zero sorry sorry fun (f : arithmetic_function R) => zero_hom.mk (fun (n : ℕ) => -coe_fn f n) sorry) sorry protected instance add_comm_group {R : Type u_1} [add_comm_group R] : add_comm_group (arithmetic_function R) := add_comm_group.mk add_comm_monoid.add sorry add_comm_monoid.zero sorry sorry add_group.neg add_group.sub sorry sorry /-- The Dirichlet convolution of two arithmetic functions `f` and `g` is another arithmetic function such that `(f * g) n` is the sum of `f x * g y` over all `(x,y)` such that `x * y = n`. -/ protected instance has_scalar {R : Type u_1} {M : Type u_2} [HasZero R] [add_comm_monoid M] [has_scalar R M] : has_scalar (arithmetic_function R) (arithmetic_function M) := has_scalar.mk fun (f : arithmetic_function R) (g : arithmetic_function M) => zero_hom.mk (fun (n : ℕ) => finset.sum (divisors_antidiagonal n) fun (x : ℕ × ℕ) => coe_fn f (prod.fst x) • coe_fn g (prod.snd x)) sorry @[simp] theorem smul_apply {R : Type u_1} {M : Type u_2} [HasZero R] [add_comm_monoid M] [has_scalar R M] {f : arithmetic_function R} {g : arithmetic_function M} {n : ℕ} : coe_fn (f • g) n = finset.sum (divisors_antidiagonal n) fun (x : ℕ × ℕ) => coe_fn f (prod.fst x) • coe_fn g (prod.snd x) := rfl /-- The Dirichlet convolution of two arithmetic functions `f` and `g` is another arithmetic function such that `(f * g) n` is the sum of `f x * g y` over all `(x,y)` such that `x * y = n`. -/ protected instance has_mul {R : Type u_1} [semiring R] : Mul (arithmetic_function R) := { mul := has_scalar.smul } @[simp] theorem mul_apply {R : Type u_1} [semiring R] {f : arithmetic_function R} {g : arithmetic_function R} {n : ℕ} : coe_fn (f * g) n = finset.sum (divisors_antidiagonal n) fun (x : ℕ × ℕ) => coe_fn f (prod.fst x) * coe_fn g (prod.snd x) := rfl theorem mul_smul' {R : Type u_1} {M : Type u_2} [semiring R] [add_comm_monoid M] [semimodule R M] (f : arithmetic_function R) (g : arithmetic_function R) (h : arithmetic_function M) : (f * g) • h = f • g • h := sorry theorem one_smul' {R : Type u_1} {M : Type u_2} [semiring R] [add_comm_monoid M] [semimodule R M] (b : arithmetic_function M) : 1 • b = b := sorry protected instance monoid {R : Type u_1} [semiring R] : monoid (arithmetic_function R) := monoid.mk Mul.mul sorry 1 sorry sorry protected instance semiring {R : Type u_1} [semiring R] : semiring (arithmetic_function R) := semiring.mk Add.add sorry 0 sorry sorry sorry Mul.mul sorry monoid.one sorry sorry sorry sorry sorry sorry protected instance comm_semiring {R : Type u_1} [comm_semiring R] : comm_semiring (arithmetic_function R) := comm_semiring.mk semiring.add sorry semiring.zero sorry sorry sorry semiring.mul sorry semiring.one sorry sorry sorry sorry sorry sorry sorry protected instance comm_ring {R : Type u_1} [comm_ring R] : comm_ring (arithmetic_function R) := comm_ring.mk add_comm_group.add sorry add_comm_group.zero sorry sorry add_comm_group.neg add_comm_group.sub sorry sorry comm_semiring.mul sorry comm_semiring.one sorry sorry sorry sorry sorry protected instance semimodule {R : Type u_1} {M : Type u_2} [semiring R] [add_comm_monoid M] [semimodule R M] : semimodule (arithmetic_function R) (arithmetic_function M) := semimodule.mk sorry sorry /-- `ζ 0 = 0`, otherwise `ζ x = 1`. The Dirichlet Series is the Riemann ζ. -/ def zeta : arithmetic_function ℕ := zero_hom.mk (fun (x : ℕ) => ite (x = 0) 0 1) sorry @[simp] theorem zeta_apply {x : ℕ} : coe_fn zeta x = ite (x = 0) 0 1 := rfl theorem zeta_apply_ne {x : ℕ} (h : x ≠ 0) : coe_fn zeta x = 1 := if_neg h @[simp] theorem coe_zeta_mul_apply {R : Type u_1} [semiring R] {f : arithmetic_function R} {x : ℕ} : coe_fn (↑zeta * f) x = finset.sum (divisors x) fun (i : ℕ) => coe_fn f i := sorry theorem coe_zeta_smul_apply {R : Type u_1} {M : Type u_2} [comm_ring R] [add_comm_group M] [module R M] {f : arithmetic_function M} {x : ℕ} : coe_fn (↑zeta • f) x = finset.sum (divisors x) fun (i : ℕ) => coe_fn f i := sorry @[simp] theorem coe_mul_zeta_apply {R : Type u_1} [semiring R] {f : arithmetic_function R} {x : ℕ} : coe_fn (f * ↑zeta) x = finset.sum (divisors x) fun (i : ℕ) => coe_fn f i := sorry theorem zeta_mul_apply {f : arithmetic_function ℕ} {x : ℕ} : coe_fn (zeta * f) x = finset.sum (divisors x) fun (i : ℕ) => coe_fn f i := sorry theorem mul_zeta_apply {f : arithmetic_function ℕ} {x : ℕ} : coe_fn (f * zeta) x = finset.sum (divisors x) fun (i : ℕ) => coe_fn f i := sorry /-- This is the pointwise product of `arithmetic_function`s. -/ def pmul {R : Type u_1} [mul_zero_class R] (f : arithmetic_function R) (g : arithmetic_function R) : arithmetic_function R := zero_hom.mk (fun (x : ℕ) => coe_fn f x * coe_fn g x) sorry @[simp] theorem pmul_apply {R : Type u_1} [mul_zero_class R] {f : arithmetic_function R} {g : arithmetic_function R} {x : ℕ} : coe_fn (pmul f g) x = coe_fn f x * coe_fn g x := rfl theorem pmul_comm {R : Type u_1} [comm_monoid_with_zero R] (f : arithmetic_function R) (g : arithmetic_function R) : pmul f g = pmul g f := sorry @[simp] theorem pmul_zeta {R : Type u_1} [semiring R] (f : arithmetic_function R) : pmul f ↑zeta = f := sorry @[simp] theorem zeta_pmul {R : Type u_1} [semiring R] (f : arithmetic_function R) : pmul (↑zeta) f = f := sorry /-- This is the pointwise power of `arithmetic_function`s. -/ def ppow {R : Type u_1} [semiring R] (f : arithmetic_function R) (k : ℕ) : arithmetic_function R := dite (k = 0) (fun (h0 : k = 0) => ↑zeta) fun (h0 : ¬k = 0) => zero_hom.mk (fun (x : ℕ) => coe_fn f x ^ k) sorry @[simp] theorem ppow_zero {R : Type u_1} [semiring R] {f : arithmetic_function R} : ppow f 0 = ↑zeta := sorry @[simp] theorem ppow_apply {R : Type u_1} [semiring R] {f : arithmetic_function R} {k : ℕ} {x : ℕ} (kpos : 0 < k) : coe_fn (ppow f k) x = coe_fn f x ^ k := sorry theorem ppow_succ {R : Type u_1} [semiring R] {f : arithmetic_function R} {k : ℕ} : ppow f (k + 1) = pmul f (ppow f k) := sorry theorem ppow_succ' {R : Type u_1} [semiring R] {f : arithmetic_function R} {k : ℕ} {kpos : 0 < k} : ppow f (k + 1) = pmul (ppow f k) f := sorry /-- Multiplicative functions -/ def is_multiplicative {R : Type u_1} [monoid_with_zero R] (f : arithmetic_function R) := coe_fn f 1 = 1 ∧ ∀ {m n : ℕ}, coprime m n → coe_fn f (m * n) = coe_fn f m * coe_fn f n namespace is_multiplicative @[simp] theorem map_one {R : Type u_1} [monoid_with_zero R] {f : arithmetic_function R} (h : is_multiplicative f) : coe_fn f 1 = 1 := and.left h @[simp] theorem map_mul_of_coprime {R : Type u_1} [monoid_with_zero R] {f : arithmetic_function R} (hf : is_multiplicative f) {m : ℕ} {n : ℕ} (h : coprime m n) : coe_fn f (m * n) = coe_fn f m * coe_fn f n := and.right hf m n h theorem nat_cast {R : Type u_1} {f : arithmetic_function ℕ} [semiring R] (h : is_multiplicative f) : is_multiplicative ↑f := sorry theorem int_cast {R : Type u_1} {f : arithmetic_function ℤ} [ring R] (h : is_multiplicative f) : is_multiplicative ↑f := sorry theorem mul {R : Type u_1} [comm_semiring R] {f : arithmetic_function R} {g : arithmetic_function R} (hf : is_multiplicative f) (hg : is_multiplicative g) : is_multiplicative (f * g) := sorry theorem pmul {R : Type u_1} [comm_semiring R] {f : arithmetic_function R} {g : arithmetic_function R} (hf : is_multiplicative f) (hg : is_multiplicative g) : is_multiplicative (pmul f g) := sorry end is_multiplicative /-- The identity on `ℕ` as an `arithmetic_function`. -/ def id : arithmetic_function ℕ := zero_hom.mk id sorry @[simp] theorem id_apply {x : ℕ} : coe_fn id x = x := rfl /-- `pow k n = n ^ k`, except `pow 0 0 = 0`. -/ def pow (k : ℕ) : arithmetic_function ℕ := ppow id k @[simp] theorem pow_apply {k : ℕ} {n : ℕ} : coe_fn (pow k) n = ite (k = 0 ∧ n = 0) 0 (n ^ k) := sorry /-- `σ k n` is the sum of the `k`th powers of the divisors of `n` -/ def sigma (k : ℕ) : arithmetic_function ℕ := zero_hom.mk (fun (n : ℕ) => finset.sum (divisors n) fun (d : ℕ) => d ^ k) sorry @[simp] theorem sigma_apply {k : ℕ} {n : ℕ} : coe_fn (sigma k) n = finset.sum (divisors n) fun (d : ℕ) => d ^ k := rfl theorem sigma_one_apply {n : ℕ} : coe_fn (sigma 1) n = finset.sum (divisors n) fun (d : ℕ) => d := sorry theorem zeta_mul_pow_eq_sigma {k : ℕ} : zeta * pow k = sigma k := sorry theorem is_multiplicative_zeta : is_multiplicative zeta := sorry theorem is_multiplicative_id : is_multiplicative id := { left := rfl, right := fun (_x _x_1 : ℕ) (_x_2 : coprime _x _x_1) => rfl } theorem is_multiplicative.ppow {R : Type u_1} [comm_semiring R] {f : arithmetic_function R} (hf : is_multiplicative f) {k : ℕ} : is_multiplicative (ppow f k) := sorry theorem is_multiplicative_pow {k : ℕ} : is_multiplicative (pow k) := is_multiplicative.ppow is_multiplicative_id theorem is_multiplicative_sigma {k : ℕ} : is_multiplicative (sigma k) := eq.mpr (id (Eq._oldrec (Eq.refl (is_multiplicative (sigma k))) (Eq.symm zeta_mul_pow_eq_sigma))) (is_multiplicative.mul is_multiplicative_zeta is_multiplicative_pow) /-- `Ω n` is the number of prime factors of `n`. -/ def card_factors : arithmetic_function ℕ := zero_hom.mk (fun (n : ℕ) => list.length (factors n)) sorry theorem card_factors_apply {n : ℕ} : coe_fn card_factors n = list.length (factors n) := rfl @[simp] theorem card_factors_one : coe_fn card_factors 1 = 0 := rfl theorem card_factors_eq_one_iff_prime {n : ℕ} : coe_fn card_factors n = 1 ↔ prime n := sorry theorem card_factors_mul {m : ℕ} {n : ℕ} (m0 : m ≠ 0) (n0 : n ≠ 0) : coe_fn card_factors (m * n) = coe_fn card_factors m + coe_fn card_factors n := sorry theorem card_factors_multiset_prod {s : multiset ℕ} (h0 : multiset.prod s ≠ 0) : coe_fn card_factors (multiset.prod s) = multiset.sum (multiset.map (⇑card_factors) s) := sorry /-- `ω n` is the number of distinct prime factors of `n`. -/ def card_distinct_factors : arithmetic_function ℕ := zero_hom.mk (fun (n : ℕ) => list.length (list.erase_dup (factors n))) sorry @[simp] theorem card_distinct_factors_zero : coe_fn card_distinct_factors 0 = 0 := rfl theorem card_distinct_factors_apply {n : ℕ} : coe_fn card_distinct_factors n = list.length (list.erase_dup (factors n)) := rfl theorem card_distinct_factors_eq_card_factors_iff_squarefree {n : ℕ} (h0 : n ≠ 0) : coe_fn card_distinct_factors n = coe_fn card_factors n ↔ squarefree n := sorry /-- `μ` is the Möbius function. If `n` is squarefree with an even number of distinct prime factors, `μ n = 1`. If `n` is squarefree with an odd number of distinct prime factors, `μ n = -1`. If `n` is not squarefree, `μ n = 0`. -/ def moebius : arithmetic_function ℤ := zero_hom.mk (fun (n : ℕ) => ite (squarefree n) ((-1) ^ coe_fn card_factors n) 0) sorry @[simp] theorem moebius_apply_of_squarefree {n : ℕ} (h : squarefree n) : coe_fn moebius n = (-1) ^ coe_fn card_factors n := if_pos h @[simp] theorem moebius_eq_zero_of_not_squarefree {n : ℕ} (h : ¬squarefree n) : coe_fn moebius n = 0 := if_neg h theorem moebius_ne_zero_iff_squarefree {n : ℕ} : coe_fn moebius n ≠ 0 ↔ squarefree n := sorry theorem moebius_ne_zero_iff_eq_or {n : ℕ} : coe_fn moebius n ≠ 0 ↔ coe_fn moebius n = 1 ∨ coe_fn moebius n = -1 := sorry @[simp] theorem coe_moebius_mul_coe_zeta {R : Type u_1} [comm_ring R] : ↑moebius * ↑zeta = 1 := sorry @[simp] theorem coe_zeta_mul_coe_moebius {R : Type u_1} [comm_ring R] : ↑zeta * ↑moebius = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (↑zeta * ↑moebius = 1)) (mul_comm ↑zeta ↑moebius))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑moebius * ↑zeta = 1)) (coe_moebius_mul_coe_zeta R _inst_1))) (Eq.refl 1)) @[simp] theorem moebius_mul_coe_zeta : moebius * ↑zeta = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (moebius * ↑zeta = 1)) (Eq.symm (int_coe_int moebius)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑moebius * ↑zeta = 1)) (coe_moebius_mul_coe_zeta ℤ int.comm_ring))) (Eq.refl 1)) @[simp] theorem coe_zeta_mul_moebius : ↑zeta * moebius = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (↑zeta * moebius = 1)) (Eq.symm (int_coe_int moebius)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑zeta * ↑moebius = 1)) (coe_zeta_mul_coe_moebius ℤ int.comm_ring))) (Eq.refl 1)) protected instance zeta.invertible {R : Type u_1} [comm_ring R] : invertible ↑zeta := invertible.mk (↑moebius) (coe_moebius_mul_coe_zeta R _inst_1) (coe_zeta_mul_coe_moebius R _inst_1) /-- A unit in `arithmetic_function R` that evaluates to `ζ`, with inverse `μ`. -/ def zeta_unit {R : Type u_1} [comm_ring R] : units (arithmetic_function R) := units.mk (↑zeta) (↑moebius) (coe_zeta_mul_coe_moebius R _inst_1) (coe_moebius_mul_coe_zeta R _inst_1) @[simp] theorem coe_zeta_unit {R : Type u_1} [comm_ring R] : ↑(zeta_unit R _inst_1) = ↑zeta := rfl @[simp] theorem inv_zeta_unit {R : Type u_1} [comm_ring R] : ↑(zeta_unit R _inst_1⁻¹) = ↑moebius := rfl /-- Möbius inversion for functions to an `add_comm_group`. -/ theorem sum_eq_iff_sum_smul_moebius_eq {R : Type u_1} [add_comm_group R] {f : ℕ → R} {g : ℕ → R} : (∀ (n : ℕ), 0 < n → (finset.sum (divisors n) fun (i : ℕ) => f i) = g n) ↔ ∀ (n : ℕ), 0 < n → (finset.sum (divisors_antidiagonal n) fun (x : ℕ × ℕ) => coe_fn moebius (prod.fst x) • g (prod.snd x)) = f n := sorry /-- Möbius inversion for functions to a `comm_ring`. -/ theorem sum_eq_iff_sum_mul_moebius_eq {R : Type u_1} [comm_ring R] {f : ℕ → R} {g : ℕ → R} : (∀ (n : ℕ), 0 < n → (finset.sum (divisors n) fun (i : ℕ) => f i) = g n) ↔ ∀ (n : ℕ), 0 < n → (finset.sum (divisors_antidiagonal n) fun (x : ℕ × ℕ) => ↑(coe_fn moebius (prod.fst x)) * g (prod.snd x)) = f n := sorry /-- Möbius inversion for functions to a `comm_group`. -/ theorem prod_eq_iff_prod_pow_moebius_eq {R : Type u_1} [comm_group R] {f : ℕ → R} {g : ℕ → R} : (∀ (n : ℕ), 0 < n → (finset.prod (divisors n) fun (i : ℕ) => f i) = g n) ↔ ∀ (n : ℕ), 0 < n → (finset.prod (divisors_antidiagonal n) fun (x : ℕ × ℕ) => g (prod.snd x) ^ coe_fn moebius (prod.fst x)) = f n := sum_eq_iff_sum_smul_moebius_eq (additive R) additive.add_comm_group (fun (i : ℕ) => f i) fun (n : ℕ) => g n /-- Möbius inversion for functions to a `comm_group_with_zero`. -/ theorem prod_eq_iff_prod_pow_moebius_eq_of_nonzero {R : Type u_1} [comm_group_with_zero R] {f : ℕ → R} {g : ℕ → R} (hf : ∀ (n : ℕ), 0 < n → f n ≠ 0) (hg : ∀ (n : ℕ), 0 < n → g n ≠ 0) : (∀ (n : ℕ), 0 < n → (finset.prod (divisors n) fun (i : ℕ) => f i) = g n) ↔ ∀ (n : ℕ), 0 < n → (finset.prod (divisors_antidiagonal n) fun (x : ℕ × ℕ) => g (prod.snd x) ^ coe_fn moebius (prod.fst x)) = f n := sorry end Mathlib
4f4dea638fb8db99a0db7d7216235b64d150e307
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/metric_space/gromov_hausdorff_realized.lean
aabf4b28c8c9b7d03673c62e13b42de287b3568f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
8,953
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sébastien Gouëzel Construction of a good coupling between nonempty compact metric spaces, minimizing their Hausdorff distance. This construction is instrumental to study the Gromov-Hausdorff distance between nonempty compact metric spaces -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.metric_space.gluing import Mathlib.topology.metric_space.hausdorff_distance import Mathlib.PostPort universes u v namespace Mathlib namespace Gromov_Hausdorff /- This section shows that the Gromov-Hausdorff distance is realized. For this, we consider candidate distances on the disjoint union α ⊕ β of two compact nonempty metric spaces, almost realizing the Gromov-Hausdorff distance, and show that they form a compact family by applying Arzela-Ascoli theorem. The existence of a minimizer follows. -/ /-- The set of functions on α ⊕ β that are candidates distances to realize the minimum of the Hausdorff distances between α and β in a coupling -/ def candidates (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] : set (prod_space_fun α β) := set_of fun (f : prod_space_fun α β) => (((((∀ (x y : α), f (sum.inl x, sum.inl y) = dist x y) ∧ ∀ (x y : β), f (sum.inr x, sum.inr y) = dist x y) ∧ ∀ (x y : α ⊕ β), f (x, y) = f (y, x)) ∧ ∀ (x y z : α ⊕ β), f (x, z) ≤ f (x, y) + f (y, z)) ∧ ∀ (x : α ⊕ β), f (x, x) = 0) ∧ ∀ (x y : α ⊕ β), f (x, y) ≤ ↑(max_var α β) /-- Version of the set of candidates in bounded_continuous_functions, to apply Arzela-Ascoli -/ /-- candidates are bounded by max_var α β -/ /-- Technical lemma to prove that candidates are Lipschitz -/ /-- Candidates are Lipschitz -/ /-- candidates give rise to elements of bounded_continuous_functions -/ def candidates_b_of_candidates {α : Type u} {β : Type v} [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] (f : prod_space_fun α β) (fA : f ∈ candidates α β) : Cb α β := bounded_continuous_function.mk_of_compact f sorry theorem candidates_b_of_candidates_mem {α : Type u} {β : Type v} [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] (f : prod_space_fun α β) (fA : f ∈ candidates α β) : candidates_b_of_candidates f fA ∈ candidates_b α β := fA /-- The distance on α ⊕ β is a candidate -/ def candidates_b_dist (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Inhabited α] [metric_space β] [compact_space β] [Inhabited β] : Cb α β := candidates_b_of_candidates (fun (p : (α ⊕ β) × (α ⊕ β)) => dist (prod.fst p) (prod.snd p)) sorry theorem candidates_b_dist_mem_candidates_b {α : Type u} {β : Type v} [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] : candidates_b_dist α β ∈ candidates_b α β := candidates_b_of_candidates_mem (fun (p : (α ⊕ β) × (α ⊕ β)) => dist (prod.fst p) (prod.snd p)) (candidates_b_dist._proof_1 α β) /-- To apply Arzela-Ascoli, we need to check that the set of candidates is closed and equicontinuous. Equicontinuity follows from the Lipschitz control, we check closedness -/ /-- Compactness of candidates (in bounded_continuous_functions) follows -/ /-- We will then choose the candidate minimizing the Hausdorff distance. Except that we are not in a metric space setting, so we need to define our custom version of Hausdorff distance, called HD, and prove its basic properties. -/ def HD {α : Type u} {β : Type v} [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] (f : Cb α β) : ℝ := max (supr fun (x : α) => infi fun (y : β) => coe_fn f (sum.inl x, sum.inr y)) (supr fun (y : β) => infi fun (x : α) => coe_fn f (sum.inl x, sum.inr y)) /- We will show that HD is continuous on bounded_continuous_functions, to deduce that its minimum on the compact set candidates_b is attained. Since it is defined in terms of infimum and supremum on ℝ, which is only conditionnally complete, we will need all the time to check that the defining sets are bounded below or above. This is done in the next few technical lemmas -/ theorem HD_below_aux1 {α : Type u} {β : Type v} [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] {f : Cb α β} (C : ℝ) {x : α} : bdd_below (set.range fun (y : β) => coe_fn f (sum.inl x, sum.inr y) + C) := sorry theorem HD_below_aux2 {α : Type u} {β : Type v} [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] {f : Cb α β} (C : ℝ) {y : β} : bdd_below (set.range fun (x : α) => coe_fn f (sum.inl x, sum.inr y) + C) := sorry /-- Explicit bound on HD (dist). This means that when looking for minimizers it will be sufficient to look for functions with HD(f) bounded by this bound. -/ theorem HD_candidates_b_dist_le {α : Type u} {β : Type v} [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] : HD (candidates_b_dist α β) ≤ metric.diam set.univ + 1 + metric.diam set.univ := sorry /- To check that HD is continuous, we check that it is Lipschitz. As HD is a max, we prove separately inequalities controlling the two terms (relying too heavily on copy-paste...) -/ /-- Conclude that HD, being Lipschitz, is continuous -/ /- Now that we have proved that the set of candidates is compact, and that HD is continuous, we can finally select a candidate minimizing HD. This will be the candidate realizing the optimal coupling. -/ /-- With the optimal candidate, construct a premetric space structure on α ⊕ β, on which the predistance is given by the candidate. Then, we will identify points at 0 predistance to obtain a genuine metric space -/ def premetric_optimal_GH_dist (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] : premetric_space (α ⊕ β) := premetric_space.mk sorry sorry sorry /-- A metric space which realizes the optimal coupling between α and β -/ def optimal_GH_coupling (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] := premetric.metric_quot (α ⊕ β) /-- Injection of α in the optimal coupling between α and β -/ def optimal_GH_injl (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] (x : α) : optimal_GH_coupling α β := quotient.mk (sum.inl x) /-- The injection of α in the optimal coupling between α and β is an isometry. -/ theorem isometry_optimal_GH_injl (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] : isometry (optimal_GH_injl α β) := iff.mpr isometry_emetric_iff_metric fun (x y : α) => id (candidates_dist_inl (optimal_GH_dist_mem_candidates_b α β) x y) /-- Injection of β in the optimal coupling between α and β -/ def optimal_GH_injr (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] (y : β) : optimal_GH_coupling α β := quotient.mk (sum.inr y) /-- The injection of β in the optimal coupling between α and β is an isometry. -/ theorem isometry_optimal_GH_injr (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] : isometry (optimal_GH_injr α β) := iff.mpr isometry_emetric_iff_metric fun (x y : β) => id (candidates_dist_inr (optimal_GH_dist_mem_candidates_b α β) x y) /-- The optimal coupling between two compact spaces α and β is still a compact space -/ protected instance compact_space_optimal_GH_coupling (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] : compact_space (optimal_GH_coupling α β) := sorry /-- For any candidate f, HD(f) is larger than or equal to the Hausdorff distance in the optimal coupling. This follows from the fact that HD of the optimal candidate is exactly the Hausdorff distance in the optimal coupling, although we only prove here the inequality we need. -/ theorem Hausdorff_dist_optimal_le_HD (α : Type u) (β : Type v) [metric_space α] [compact_space α] [Nonempty α] [metric_space β] [compact_space β] [Nonempty β] {f : Cb α β} (h : f ∈ candidates_b α β) : metric.Hausdorff_dist (set.range (optimal_GH_injl α β)) (set.range (optimal_GH_injr α β)) ≤ HD f := sorry
5be43321c15102fe40d6b37b3781973c3d77c073
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Util/RecDepth.lean
d1308c042a123d024563765a68c3eee44fc99ccb
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
480
lean
/- 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.Data.Options namespace Lean def getMaxRecDepth (opts : Options) : Nat := opts.getNat `maxRecDepth defaultMaxRecDepth @[init] def maxRecDepth : IO Unit := registerOption `maxRecDepth { defValue := defaultMaxRecDepth, group := "", descr := "maximum recursion depth for many Lean procedures" } end Lean
7fd20598c967fee9b0d316b16833dd2578aa9209
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/big_operators/part_enat.lean
e5ac611f40870d0044e216a1eb1ab968b42a813b
[ "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
539
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import algebra.big_operators.basic import data.nat.part_enat /-! # Big operators in `part_enat` A simple lemma about sums in `part_enat`. -/ open_locale big_operators variables {α : Type*} namespace finset lemma sum_nat_coe_enat (s : finset α) (f : α → ℕ) : (∑ x in s, (f x : part_enat)) = (∑ x in s, f x : ℕ) := (part_enat.coe_hom.map_sum _ _).symm end finset
9d9084bad55c7ce5497cd9c89f873864d8b21b5d
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/init/sum.lean
5bcdc1c4246907fba6532fecad9f861836c9cba9
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
591
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad The sum type, aka disjoint union. -/ prelude import init.logic notation A ⊕ B := sum A B universe variables u v variables {A : Type u} {B : Type v} attribute [instance] protected definition is_inhabited_left [h : inhabited A] : inhabited (A ⊕ B) := ⟨sum.inl (default A)⟩ attribute [instance] protected definition is_inhabited_right [h : inhabited B] : inhabited (A ⊕ B) := ⟨sum.inr (default B)⟩
f5ac89bb228ca9e3b4c15cc61d7625de5c27c8b2
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Init/Control/Reader.lean
d75bf990c3e21f86db0b2e3a37c3758388becce1
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
1,053
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich The Reader monad transformer for passing immutable State. -/ prelude import Init.Control.Basic import Init.Control.Id import Init.Control.Except namespace ReaderT @[inline] protected def orElse [Alternative m] (x₁ : ReaderT ρ m α) (x₂ : Unit → ReaderT ρ m α) : ReaderT ρ m α := fun s => x₁ s <|> x₂ () s @[inline] protected def failure [Alternative m] : ReaderT ρ m α := fun _ => failure instance [Alternative m] [Monad m] : Alternative (ReaderT ρ m) where failure := ReaderT.failure orElse := ReaderT.orElse end ReaderT instance : MonadControl m (ReaderT ρ m) where stM := id liftWith f ctx := f fun x => x ctx restoreM x _ := x instance ReaderT.tryFinally [MonadFinally m] [Monad m] : MonadFinally (ReaderT ρ m) where tryFinally' x h ctx := tryFinally' (x ctx) (fun a? => h a? ctx) @[reducible] def ReaderM (ρ : Type u) := ReaderT ρ Id
19690b5260b4b13c6d670d0276306a2efe63b2f9
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/data/option/basic.lean
54db24c5eb7c6bd4ba5f6966c41f279414c79d8f
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,280
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.logic init.category open decidable universe variables u v namespace option def to_monad {m : Type → Type} [monad m] [alternative m] {A} : option A → m A | none := failure | (some a) := return a def get_or_else {α : Type u} : option α → α → α | (some x) _ := x | none e := e def is_some {α : Type u} : option α → bool | (some _) := tt | none := ff end option instance (α : Type u) : inhabited (option α) := ⟨none⟩ instance {α : Type u} [d : decidable_eq α] : decidable_eq (option α) | none none := is_true rfl | none (some v₂) := is_false (λ h, option.no_confusion h) | (some v₁) none := is_false (λ h, option.no_confusion h) | (some v₁) (some v₂) := match (d v₁ v₂) with | (is_true e) := is_true (congr_arg (@some α) e) | (is_false n) := is_false (λ h, option.no_confusion h (λ e, absurd e n)) end @[inline] def option_fmap {α : Type u} {β : Type v} (f : α → β) : option α → option β | none := none | (some a) := some (f a) @[inline] def option_bind {α : Type u} {β : Type v} : option α → (α → option β) → option β | none b := none | (some a) b := b a instance : monad option := {map := @option_fmap, ret := @some, bind := @option_bind} def option_orelse {α : Type u} : option α → option α → option α | (some a) o := some a | none (some a) := some a | none none := none instance {α : Type u} : alternative option := alternative.mk @option_fmap @some (@fapp _ _) @none @option_orelse def option_t (m : Type u → Type v) [monad m] (α : Type u) : Type v := m (option α) @[inline] def option_t_fmap {m : Type u → Type v} [monad m] {α β : Type u} (f : α → β) (e : option_t m α) : option_t m β := show m (option β), from do o ← e, match o with | none := return none | (some a) := return (some (f a)) end @[inline] def option_t_bind {m : Type u → Type v} [monad m] {α β : Type u} (a : option_t m α) (b : α → option_t m β) : option_t m β := show m (option β), from do o ← a, match o with | none := return none | (some a) := b a end @[inline] def option_t_return {m : Type u → Type v} [monad m] {α : Type u} (a : α) : option_t m α := show m (option α), from return (some a) instance {m : Type u → Type v} [monad m] : monad (option_t m) := {map := @option_t_fmap m _, ret := @option_t_return m _, bind := @option_t_bind m _} def option_t_orelse {m : Type u → Type v} [monad m] {α : Type u} (a : option_t m α) (b : option_t m α) : option_t m α := show m (option α), from do o ← a, match o with | none := b | (some v) := return (some v) end def option_t_fail {m : Type u → Type v} [monad m] {α : Type u} : option_t m α := show m (option α), from return none instance {m : Type u → Type v} [monad m] : alternative (option_t m) := {map := @option_t_fmap m _, pure := @option_t_return m _, seq := @fapp (option_t m) (@option_t.monad m _), failure := @option_t_fail m _, orelse := @option_t_orelse m _}
4fd3136af6f4b688da2cc6bdbb2fdbada81291b0
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category_theory/whiskering.lean
e7234c434629730714701f4627a12b1da3dcbde3
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
7,850
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.natural_isomorphism namespace category_theory universes u₁ v₁ u₂ v₂ u₃ v₃ u₄ v₄ section variables (C : Type u₁) [𝒞 : category.{v₁} C] (D : Type u₂) [𝒟 : category.{v₂} D] (E : Type u₃) [ℰ : category.{v₃} E] include 𝒞 𝒟 ℰ def whiskering_left : (C ⥤ D) ⥤ ((D ⥤ E) ⥤ (C ⥤ E)) := { obj := λ F, { obj := λ G, F ⋙ G, map := λ G H α, { app := λ c, α.app (F.obj c), naturality' := by intros X Y f; rw [functor.comp_map, functor.comp_map, α.naturality] } }, map := λ F G τ, { app := λ H, { app := λ c, H.map (τ.app c), naturality' := λ X Y f, begin dsimp, rw [←H.map_comp, ←H.map_comp, ←τ.naturality] end }, naturality' := λ X Y f, begin ext1, dsimp, rw [f.naturality] end } } def whiskering_right : (D ⥤ E) ⥤ ((C ⥤ D) ⥤ (C ⥤ E)) := { obj := λ H, { obj := λ F, F ⋙ H, map := λ _ _ α, { app := λ c, H.map (α.app c), naturality' := by intros X Y f; rw [functor.comp_map, functor.comp_map, ←H.map_comp, ←H.map_comp, α.naturality] } }, map := λ G H τ, { app := λ F, { app := λ c, τ.app (F.obj c), naturality' := λ X Y f, begin dsimp, rw [τ.naturality] end }, naturality' := λ X Y f, begin ext1, dsimp, rw [←nat_trans.naturality] end } } variables {C} {D} {E} def whisker_left (F : C ⥤ D) {G H : D ⥤ E} (α : G ⟶ H) : (F ⋙ G) ⟶ (F ⋙ H) := ((whiskering_left C D E).obj F).map α @[simp] lemma whiskering_left_obj_obj (F : C ⥤ D) (G : D ⥤ E) : ((whiskering_left C D E).obj F).obj G = F ⋙ G := rfl @[simp] lemma whiskering_left_obj_map (F : C ⥤ D) {G H : D ⥤ E} (α : G ⟶ H) : ((whiskering_left C D E).obj F).map α = whisker_left F α := rfl @[simp] lemma whiskering_left_map_app_app {F G : C ⥤ D} (τ : F ⟶ G) (H : D ⥤ E) (c) : (((whiskering_left C D E).map τ).app H).app c = H.map (τ.app c) := rfl @[simp] lemma whisker_left.app (F : C ⥤ D) {G H : D ⥤ E} (α : G ⟶ H) (X : C) : (whisker_left F α).app X = α.app (F.obj X) := rfl def whisker_right {G H : C ⥤ D} (α : G ⟶ H) (F : D ⥤ E) : (G ⋙ F) ⟶ (H ⋙ F) := ((whiskering_right C D E).obj F).map α @[simp] lemma whiskering_right_obj_obj (G : C ⥤ D) (F : D ⥤ E) : ((whiskering_right C D E).obj F).obj G = G ⋙ F := rfl @[simp] lemma whiskering_right_obj_map {G H : C ⥤ D} (α : G ⟶ H) (F : D ⥤ E) : ((whiskering_right C D E).obj F).map α = whisker_right α F := rfl @[simp] lemma whiskering_right_map_app_app (F : C ⥤ D) {G H : D ⥤ E} (τ : G ⟶ H) (c) : (((whiskering_right C D E).map τ).app F).app c = τ.app (F.obj c) := rfl @[simp] lemma whisker_right.app {G H : C ⥤ D} (α : G ⟶ H) (F : D ⥤ E) (X : C) : (whisker_right α F).app X = F.map (α.app X) := rfl @[simp] lemma whisker_left_id (F : C ⥤ D) {G : D ⥤ E} : whisker_left F (nat_trans.id G) = nat_trans.id (F.comp G) := rfl @[simp] lemma whisker_left_id' (F : C ⥤ D) {G : D ⥤ E} : whisker_left F (𝟙 G) = 𝟙 (F.comp G) := rfl @[simp] lemma whisker_right_id {G : C ⥤ D} (F : D ⥤ E) : whisker_right (nat_trans.id G) F = nat_trans.id (G.comp F) := ((whiskering_right C D E).obj F).map_id _ @[simp] lemma whisker_right_id' {G : C ⥤ D} (F : D ⥤ E) : whisker_right (𝟙 G) F = 𝟙 (G.comp F) := ((whiskering_right C D E).obj F).map_id _ @[simp] lemma whisker_left_comp (F : C ⥤ D) {G H K : D ⥤ E} (α : G ⟶ H) (β : H ⟶ K) : whisker_left F (α ≫ β) = (whisker_left F α) ≫ (whisker_left F β) := rfl @[simp] lemma whisker_right_comp {G H K : C ⥤ D} (α : G ⟶ H) (β : H ⟶ K) (F : D ⥤ E) : whisker_right (α ≫ β) F = (whisker_right α F) ≫ (whisker_right β F) := ((whiskering_right C D E).obj F).map_comp α β def iso_whisker_left (F : C ⥤ D) {G H : D ⥤ E} (α : G ≅ H) : (F ⋙ G) ≅ (F ⋙ H) := ((whiskering_left C D E).obj F).map_iso α @[simp] lemma iso_whisker_left_hom (F : C ⥤ D) {G H : D ⥤ E} (α : G ≅ H) : (iso_whisker_left F α).hom = whisker_left F α.hom := rfl @[simp] lemma iso_whisker_left_inv (F : C ⥤ D) {G H : D ⥤ E} (α : G ≅ H) : (iso_whisker_left F α).inv = whisker_left F α.inv := rfl def iso_whisker_right {G H : C ⥤ D} (α : G ≅ H) (F : D ⥤ E) : (G ⋙ F) ≅ (H ⋙ F) := ((whiskering_right C D E).obj F).map_iso α @[simp] lemma iso_whisker_right_hom {G H : C ⥤ D} (α : G ≅ H) (F : D ⥤ E) : (iso_whisker_right α F).hom = whisker_right α.hom F := rfl @[simp] lemma iso_whisker_right_inv {G H : C ⥤ D} (α : G ≅ H) (F : D ⥤ E) : (iso_whisker_right α F).inv = whisker_right α.inv F := rfl instance is_iso_whisker_left (F : C ⥤ D) {G H : D ⥤ E} (α : G ⟶ H) [is_iso α] : is_iso (whisker_left F α) := { .. iso_whisker_left F (as_iso α) } instance is_iso_whisker_right {G H : C ⥤ D} (α : G ⟶ H) (F : D ⥤ E) [is_iso α] : is_iso (whisker_right α F) := { .. iso_whisker_right (as_iso α) F } variables {B : Type u₄} [ℬ : category.{v₄} B] include ℬ local attribute [elab_simple] whisker_left whisker_right @[simp] lemma whisker_left_twice (F : B ⥤ C) (G : C ⥤ D) {H K : D ⥤ E} (α : H ⟶ K) : whisker_left F (whisker_left G α) = whisker_left (F ⋙ G) α := rfl @[simp] lemma whisker_right_twice {H K : B ⥤ C} (F : C ⥤ D) (G : D ⥤ E) (α : H ⟶ K) : whisker_right (whisker_right α F) G = whisker_right α (F ⋙ G) := rfl lemma whisker_right_left (F : B ⥤ C) {G H : C ⥤ D} (α : G ⟶ H) (K : D ⥤ E) : whisker_right (whisker_left F α) K = whisker_left F (whisker_right α K) := rfl end namespace functor universes u₅ v₅ variables {A : Type u₁} [𝒜 : category.{v₁} A] variables {B : Type u₂} [ℬ : category.{v₂} B] include 𝒜 ℬ def left_unitor (F : A ⥤ B) : ((functor.id _) ⋙ F) ≅ F := { hom := { app := λ X, 𝟙 (F.obj X) }, inv := { app := λ X, 𝟙 (F.obj X) } } @[simp] lemma left_unitor_hom_app {F : A ⥤ B} {X} : F.left_unitor.hom.app X = 𝟙 _ := rfl @[simp] lemma left_unitor_inv_app {F : A ⥤ B} {X} : F.left_unitor.inv.app X = 𝟙 _ := rfl def right_unitor (F : A ⥤ B) : (F ⋙ (functor.id _)) ≅ F := { hom := { app := λ X, 𝟙 (F.obj X) }, inv := { app := λ X, 𝟙 (F.obj X) } } @[simp] lemma right_unitor_hom_app {F : A ⥤ B} {X} : F.right_unitor.hom.app X = 𝟙 _ := rfl @[simp] lemma right_unitor_inv_app {F : A ⥤ B} {X} : F.right_unitor.inv.app X = 𝟙 _ := rfl variables {C : Type u₃} [𝒞 : category.{v₃} C] variables {D : Type u₄} [𝒟 : category.{v₄} D] include 𝒞 𝒟 def associator (F : A ⥤ B) (G : B ⥤ C) (H : C ⥤ D) : ((F ⋙ G) ⋙ H) ≅ (F ⋙ (G ⋙ H)) := { hom := { app := λ _, 𝟙 _ }, inv := { app := λ _, 𝟙 _ } } @[simp] lemma associator_hom_app {F : A ⥤ B} {G : B ⥤ C} {H : C ⥤ D} {X} : (associator F G H).hom.app X = 𝟙 _ := rfl @[simp] lemma associator_inv_app {F : A ⥤ B} {G : B ⥤ C} {H : C ⥤ D} {X} : (associator F G H).inv.app X = 𝟙 _ := rfl omit 𝒟 lemma triangle (F : A ⥤ B) (G : B ⥤ C) : (associator F (functor.id B) G).hom ≫ (whisker_left F (left_unitor G).hom) = (whisker_right (right_unitor F).hom G) := begin ext1, dsimp [associator, left_unitor, right_unitor], simp end variables {E : Type u₅} [ℰ : category.{v₅} E] include 𝒟 ℰ variables (F : A ⥤ B) (G : B ⥤ C) (H : C ⥤ D) (K : D ⥤ E) lemma pentagon : (whisker_right (associator F G H).hom K) ≫ (associator F (G ⋙ H) K).hom ≫ (whisker_left F (associator G H K).hom) = ((associator (F ⋙ G) H K).hom ≫ (associator F G (H ⋙ K)).hom) := begin ext1, dsimp [associator], simp, end end functor end category_theory
bfb7ec99a93fae70157f9024f6cf978de11aaddb
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/tactic_id_bug.lean
a32786a22134357d536a2f57aa8443e051e87f11
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
712
lean
import algebra.function import logic.funext open function structure bijection (A : Type) := (func finv : A → A) (linv : finv ∘ func = id) (rinv : func ∘ finv = id) attribute bijection.func [coercion] namespace bijection variable {A : Type} protected theorem eq {f g : bijection A} (func_eq : func f = func g) (finv_eq : finv f = finv g) : f = g := begin revert finv_eq, revert func_eq, cases g with [gfunc, gfinv, glinv, grinv], cases f with [func, finv, linv, rinv], state, esimp, intros [func_eq, finv_eq], revert grinv, revert glinv, revert rinv, revert linv, rewrite [func_eq, finv_eq], intros [H1, H2, H3, H4], apply rfl end end bijection
be36b5534d052cc6414d888ebf0039826643cbe5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Compiler/LCNF/Simp/DefaultAlt.lean
c279d8cc6d4b1522bccd2eadf673fd0f2c77bbdf
[ "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,963
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.LCNF.Simp.SimpM namespace Lean.Compiler.LCNF namespace Simp /-- Return the alternative in `alts` whose body appears in most arms, and the number of occurrences. We use this function to decide whether to create a `.default` case or not. -/ private def getMaxOccs (alts : Array Alt) : Alt × Nat := Id.run do let mut maxAlt := alts[0]! let mut max := getNumOccsOf alts 0 for i in [1:alts.size] do let curr := getNumOccsOf alts i if curr > max then maxAlt := alts[i]! max := curr return (maxAlt, max) where /-- Return the number of occurrences of `alts[i]` in `alts`. We use alpha equivalence. Note that the number of occurrences can be greater than 1 only when the alternative does not depend on field parameters -/ getNumOccsOf (alts : Array Alt) (i : Nat) : Nat := Id.run do let code := alts[i]!.getCode let mut n := 1 for j in [i+1:alts.size] do if Code.alphaEqv alts[j]!.getCode code then n := n+1 return n /-- Add a default case to the given `cases` alternatives if there are alternatives with equivalent (aka alpha equivalent) right hand sides. -/ def addDefaultAlt (alts : Array Alt) : SimpM (Array Alt) := do if alts.size <= 1 || alts.any (· matches .default ..) then return alts else let (max, noccs) := getMaxOccs alts if noccs == 1 then return alts else let mut altsNew := #[] let mut first := true markSimplified for alt in alts do if Code.alphaEqv alt.getCode max.getCode then let .alt _ ps k := alt | unreachable! eraseParams ps unless first do eraseCode k first := false else altsNew := altsNew.push alt return altsNew.push (.default max.getCode)
31eed9dd004126f95bc67d6edd358e2ad3bae1aa
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/set_theory/game/impartial_auto.lean
c001092bfd3d99134b873136e0a0232892c4c5f5
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
4,243
lean
/- Copyright (c) 2020 Fox Thomson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Fox Thomson -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.set_theory.game.winner import Mathlib.tactic.nth_rewrite.default import Mathlib.tactic.equiv_rw import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Basic definitions about impartial (pre-)games We will define an impartial game, one in which left and right can make exactly the same moves. Our definition differs slightly by saying that the game is always equivalent to its negative, no matter what moves are played. This allows for games such as poker-nim to be classifed as impartial. -/ namespace pgame /-- The definition for a impartial game, defined using Conway induction -/ def impartial : pgame → Prop := sorry theorem impartial_def {G : pgame} : impartial G ↔ equiv G (-G) ∧ (∀ (i : left_moves G), impartial (move_left G i)) ∧ ∀ (j : right_moves G), impartial (move_right G j) := sorry namespace impartial protected instance impartial_zero : impartial 0 := sorry theorem neg_equiv_self (G : pgame) [h : impartial G] : equiv G (-G) := and.left (iff.mp impartial_def h) protected instance move_left_impartial {G : pgame} [h : impartial G] (i : left_moves G) : impartial (move_left G i) := and.left (and.right (iff.mp impartial_def h)) i protected instance move_right_impartial {G : pgame} [h : impartial G] (j : right_moves G) : impartial (move_right G j) := and.right (and.right (iff.mp impartial_def h)) j protected instance impartial_add (G : pgame) (H : pgame) [impartial G] [impartial H] : impartial (G + H) := sorry protected instance impartial_neg (G : pgame) [impartial G] : impartial (-G) := sorry theorem winner_cases (G : pgame) [impartial G] : first_loses G ∨ first_wins G := sorry theorem not_first_wins (G : pgame) [impartial G] : ¬first_wins G ↔ first_loses G := sorry theorem not_first_loses (G : pgame) [impartial G] : ¬first_loses G ↔ first_wins G := iff.symm (iff.mp iff_not_comm (iff.symm (not_first_wins G))) theorem add_self (G : pgame) [impartial G] : first_loses (G + G) := iff.mpr first_loses_is_zero (equiv_trans (add_congr (neg_equiv_self G) (equiv_refl G)) add_left_neg_equiv) theorem equiv_iff_sum_first_loses (G : pgame) (H : pgame) [impartial G] [impartial H] : equiv G H ↔ first_loses (G + H) := sorry theorem le_zero_iff {G : pgame} [impartial G] : G ≤ 0 ↔ 0 ≤ G := eq.mpr (id (Eq._oldrec (Eq.refl (G ≤ 0 ↔ 0 ≤ G)) (propext le_zero_iff_zero_le_neg))) (eq.mpr (id (Eq._oldrec (Eq.refl (0 ≤ -G ↔ 0 ≤ G)) (propext (le_congr (equiv_refl 0) (neg_equiv_self G))))) (iff.refl (0 ≤ -G))) theorem lt_zero_iff {G : pgame} [impartial G] : G < 0 ↔ 0 < G := sorry theorem first_loses_symm (G : pgame) [impartial G] : first_loses G ↔ G ≤ 0 := { mp := and.left, mpr := fun (h : G ≤ 0) => { left := h, right := iff.mp le_zero_iff h } } theorem first_wins_symm (G : pgame) [impartial G] : first_wins G ↔ G < 0 := { mp := and.right, mpr := fun (h : G < 0) => { left := iff.mp lt_zero_iff h, right := h } } theorem first_loses_symm' (G : pgame) [impartial G] : first_loses G ↔ 0 ≤ G := { mp := and.right, mpr := fun (h : 0 ≤ G) => { left := iff.mpr le_zero_iff h, right := h } } theorem first_wins_symm' (G : pgame) [impartial G] : first_wins G ↔ 0 < G := { mp := and.left, mpr := fun (h : 0 < G) => { left := h, right := iff.mpr lt_zero_iff h } } theorem no_good_left_moves_iff_first_loses (G : pgame) [impartial G] : (∀ (i : left_moves G), first_wins (move_left G i)) ↔ first_loses G := sorry theorem no_good_right_moves_iff_first_loses (G : pgame) [impartial G] : (∀ (j : right_moves G), first_wins (move_right G j)) ↔ first_loses G := sorry theorem good_left_move_iff_first_wins (G : pgame) [impartial G] : (∃ (i : left_moves G), first_loses (move_left G i)) ↔ first_wins G := sorry theorem good_right_move_iff_first_wins (G : pgame) [impartial G] : (∃ (j : right_moves G), first_loses (move_right G j)) ↔ first_wins G := sorry end Mathlib
90c8251878ae4008bf8e64811bece4710d99c8cc
9dc8cecdf3c4634764a18254e94d43da07142918
/src/linear_algebra/general_linear_group.lean
ecd9604d7dce605ed8cda0c5a58f17189c8f6528
[ "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
8,467
lean
/- Copyright (c) 2021 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck -/ import linear_algebra.matrix.nonsingular_inverse import linear_algebra.special_linear_group /-! # The General Linear group $GL(n, R)$ This file defines the elements of the General Linear group `general_linear_group n R`, consisting of all invertible `n` by `n` `R`-matrices. ## Main definitions * `matrix.general_linear_group` is the type of matrices over R which are units in the matrix ring. * `matrix.GL_pos` gives the subgroup of matrices with positive determinant (over a linear ordered ring). ## Tags matrix group, group, matrix inverse -/ namespace matrix universes u v open_locale matrix open linear_map -- disable this instance so we do not accidentally use it in lemmas. local attribute [-instance] special_linear_group.has_coe_to_fun /-- `GL n R` is the group of `n` by `n` `R`-matrices with unit determinant. Defined as a subtype of matrices-/ abbreviation general_linear_group (n : Type u) (R : Type v) [decidable_eq n] [fintype n] [comm_ring R] : Type* := (matrix n n R)ˣ notation `GL` := general_linear_group namespace general_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] /-- The determinant of a unit matrix is itself a unit. -/ @[simps] def det : GL n R →* Rˣ := { to_fun := λ A, { val := (↑A : matrix n n R).det, inv := (↑(A⁻¹) : matrix n n R).det, val_inv := by rw [←det_mul, ←mul_eq_mul, A.mul_inv, det_one], inv_val := by rw [←det_mul, ←mul_eq_mul, A.inv_mul, det_one]}, map_one' := units.ext det_one, map_mul' := λ A B, units.ext $ det_mul _ _ } /--The `GL n R` and `general_linear_group R n` groups are multiplicatively equivalent-/ def to_lin : (GL n R) ≃* (linear_map.general_linear_group R (n → R)) := units.map_equiv to_lin_alg_equiv'.to_mul_equiv /--Given a matrix with invertible determinant we get an element of `GL n R`-/ def mk' (A : matrix n n R) (h : invertible (matrix.det A)) : GL n R := unit_of_det_invertible A /--Given a matrix with unit determinant we get an element of `GL n R`-/ noncomputable def mk'' (A : matrix n n R) (h : is_unit (matrix.det A)) : GL n R := nonsing_inv_unit A h /--Given a matrix with non-zero determinant over a field, we get an element of `GL n K`-/ def mk_of_det_ne_zero {K : Type*} [field K] (A : matrix n n K) (h : matrix.det A ≠ 0) : GL n K := mk' A (invertible_of_nonzero h) lemma ext_iff (A B : GL n R) : A = B ↔ (∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) := units.ext_iff.trans matrix.ext_iff.symm /-- Not marked `@[ext]` as the `ext` tactic already solves this. -/ lemma ext ⦃A B : GL n R⦄ (h : ∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) : A = B := units.ext $ matrix.ext h section coe_lemmas variables (A B : GL n R) @[simp] lemma coe_mul : ↑(A * B) = (↑A : matrix n n R) ⬝ (↑B : matrix n n R) := rfl @[simp] lemma coe_one : ↑(1 : GL n R) = (1 : matrix n n R) := rfl lemma coe_inv : ↑(A⁻¹) = (↑A : matrix n n R)⁻¹ := begin letI := A.invertible, exact inv_of_eq_nonsing_inv (↑A : matrix n n R), end /-- An element of the matrix general linear group on `(n) [fintype n]` can be considered as an element of the endomorphism general linear group on `n → R`. -/ def to_linear : general_linear_group n R ≃* linear_map.general_linear_group R (n → R) := units.map_equiv matrix.to_lin_alg_equiv'.to_ring_equiv.to_mul_equiv -- Note that without the `@` and `‹_›`, lean infers `λ a b, _inst a b` instead of `_inst` as the -- decidability argument, which prevents `simp` from obtaining the instance by unification. -- These `λ a b, _inst a b` terms also appear in the type of `A`, but simp doesn't get confused by -- them so for now we do not care. @[simp] lemma coe_to_linear : (@to_linear n ‹_› ‹_› _ _ A : (n → R) →ₗ[R] (n → R)) = matrix.mul_vec_lin A := rfl @[simp] lemma to_linear_apply (v : n → R) : (@to_linear n ‹_› ‹_› _ _ A) v = matrix.mul_vec_lin ↑A v := rfl end coe_lemmas end general_linear_group namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] instance has_coe_to_general_linear_group : has_coe (special_linear_group n R) (GL n R) := ⟨λ A, ⟨↑A, ↑(A⁻¹), congr_arg coe (mul_right_inv A), congr_arg coe (mul_left_inv A)⟩⟩ @[simp] lemma coe_to_GL_det (g : special_linear_group n R) : (g : GL n R).det = 1 := units.ext g.prop end special_linear_group section variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ] section variables (n R) /-- This is the subgroup of `nxn` matrices with entries over a linear ordered ring and positive determinant. -/ def GL_pos : subgroup (GL n R) := (units.pos_subgroup R).comap general_linear_group.det end @[simp] lemma mem_GL_pos (A : GL n R) : A ∈ GL_pos n R ↔ 0 < (A.det : R) := iff.rfl end section has_neg variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ] [fact (even (fintype.card n))] /-- Formal operation of negation on general linear group on even cardinality `n` given by negating each element. -/ instance : has_neg (GL_pos n R) := ⟨λ g, ⟨-g, begin rw [mem_GL_pos, general_linear_group.coe_det_apply, units.coe_neg, det_neg, (fact.out $ even $ fintype.card n).neg_one_pow, one_mul], exact g.prop, end⟩⟩ @[simp] lemma GL_pos.coe_neg_GL (g : GL_pos n R) : ↑(-g) = -(g : GL n R) := rfl @[simp] lemma GL_pos.coe_neg (g : GL_pos n R) : ↑(-g) = -(g : matrix n n R) := rfl @[simp] lemma GL_pos.coe_neg_apply (g : GL_pos n R) (i j : n) : (↑(-g) : matrix n n R) i j = -((↑g : matrix n n R) i j) := rfl instance : has_distrib_neg (GL_pos n R) := subtype.coe_injective.has_distrib_neg _ GL_pos.coe_neg_GL (GL_pos n R).coe_mul end has_neg namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [linear_ordered_comm_ring R] /-- `special_linear_group n R` embeds into `GL_pos n R` -/ def to_GL_pos : special_linear_group n R →* GL_pos n R := { to_fun := λ A, ⟨(A : GL n R), show 0 < (↑A : matrix n n R).det, from A.prop.symm ▸ zero_lt_one⟩, map_one' := subtype.ext $ units.ext $ rfl, map_mul' := λ A₁ A₂, subtype.ext $ units.ext $ rfl } instance : has_coe (special_linear_group n R) (GL_pos n R) := ⟨to_GL_pos⟩ lemma coe_eq_to_GL_pos : (coe : special_linear_group n R → GL_pos n R) = to_GL_pos := rfl lemma to_GL_pos_injective : function.injective (to_GL_pos : special_linear_group n R → GL_pos n R) := (show function.injective ((coe : GL_pos n R → matrix n n R) ∘ to_GL_pos), from subtype.coe_injective).of_comp /-- Coercing a `special_linear_group` via `GL_pos` and `GL` is the same as coercing striaght to a matrix. -/ @[simp] lemma coe_GL_pos_coe_GL_coe_matrix (g : special_linear_group n R) : (↑(↑(↑g : GL_pos n R) : GL n R) : matrix n n R) = ↑g := rfl @[simp] lemma coe_to_GL_pos_to_GL_det (g : special_linear_group n R) : ((g : GL_pos n R) : GL n R).det = 1 := units.ext g.prop variable [fact (even (fintype.card n))] @[norm_cast] lemma coe_GL_pos_neg (g : special_linear_group n R) : ↑(-g) = -(↑g : GL_pos n R) := subtype.ext $ units.ext rfl end special_linear_group section examples /-- The matrix [a, -b; b, a] (inspired by multiplication by a complex number); it is an element of $GL_2(R)$ if `a ^ 2 + b ^ 2` is nonzero. -/ @[simps coe {fully_applied := ff}] def plane_conformal_matrix {R} [field R] (a b : R) (hab : a ^ 2 + b ^ 2 ≠ 0) : matrix.general_linear_group (fin 2) R := general_linear_group.mk_of_det_ne_zero !![a, -b; b, a] (by simpa [det_fin_two, sq] using hab) /- TODO: Add Iwasawa matrices `n_x=!![1,x; 0,1]`, `a_t=!![exp(t/2),0;0,exp(-t/2)]` and `k_θ=!![cos θ, sin θ; -sin θ, cos θ]` -/ end examples namespace general_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] -- this section should be last to ensure we do not use it in lemmas section coe_fn_instance /-- This instance is here for convenience, but is not the simp-normal form. -/ instance : has_coe_to_fun (GL n R) (λ _, n → n → R) := { coe := λ A, A.val } @[simp] lemma coe_fn_eq_coe (A : GL n R) : ⇑A = (↑A : matrix n n R) := rfl end coe_fn_instance end general_linear_group end matrix
e25862705e0e3b31ae460e5430aaa1e0b14c94d6
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1267.lean
2f0730174e56e814caa800a208922ce388fba29a
[ "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
177
lean
example : 1 = 2 := calc _ = _ := sorry example : 1 = 2 := calc _ = _ := sorry example : 1 = 2 := by calc _ = _ := sorry example : 1 = 2 := by calc _ = _ := sorry
c56355fc1874483360a873605bcc2aaf5bea293e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/filter/cofinite.lean
a9257107c807bb0cc472b6cd4f2e4a8b3a74371a
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,816
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.at_top_bot import order.filter.pi /-! # The cofinite filter > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define `cofinite`: the filter of sets with finite complement and prove its basic properties. In particular, we prove that for `ℕ` it is equal to `at_top`. ## TODO Define filters for other cardinalities of the complement. -/ open set function open_locale classical variables {ι α β : Type*} {l : filter α} namespace filter /-- The cofinite filter is the filter of subsets whose complements are finite. -/ def cofinite : filter α := { sets := {s | sᶜ.finite}, univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq], sets_of_superset := assume s t (hs : sᶜ.finite) (st: s ⊆ t), hs.subset $ compl_subset_compl.2 st, inter_sets := assume s t (hs : sᶜ.finite) (ht : tᶜ.finite), by simp only [compl_inter, finite.union, ht, hs, mem_set_of_eq] } @[simp] lemma mem_cofinite {s : set α} : s ∈ (@cofinite α) ↔ sᶜ.finite := iff.rfl @[simp] lemma eventually_cofinite {p : α → Prop} : (∀ᶠ x in cofinite, p x) ↔ {x | ¬p x}.finite := iff.rfl lemma has_basis_cofinite : has_basis cofinite (λ s : set α, s.finite) compl := ⟨λ s, ⟨λ h, ⟨sᶜ, h, (compl_compl s).subset⟩, λ ⟨t, htf, hts⟩, htf.subset $ compl_subset_comm.2 hts⟩⟩ instance cofinite_ne_bot [infinite α] : ne_bot (@cofinite α) := has_basis_cofinite.ne_bot_iff.2 $ λ s hs, hs.infinite_compl.nonempty lemma frequently_cofinite_iff_infinite {p : α → Prop} : (∃ᶠ x in cofinite, p x) ↔ set.infinite {x | p x} := by simp only [filter.frequently, filter.eventually, mem_cofinite, compl_set_of, not_not, set.infinite] lemma _root_.set.finite.compl_mem_cofinite {s : set α} (hs : s.finite) : sᶜ ∈ (@cofinite α) := mem_cofinite.2 $ (compl_compl s).symm ▸ hs lemma _root_.set.finite.eventually_cofinite_nmem {s : set α} (hs : s.finite) : ∀ᶠ x in cofinite, x ∉ s := hs.compl_mem_cofinite lemma _root_.finset.eventually_cofinite_nmem (s : finset α) : ∀ᶠ x in cofinite, x ∉ s := s.finite_to_set.eventually_cofinite_nmem lemma _root_.set.infinite_iff_frequently_cofinite {s : set α} : set.infinite s ↔ (∃ᶠ x in cofinite, x ∈ s) := frequently_cofinite_iff_infinite.symm lemma eventually_cofinite_ne (x : α) : ∀ᶠ a in cofinite, a ≠ x := (set.finite_singleton x).eventually_cofinite_nmem lemma le_cofinite_iff_compl_singleton_mem : l ≤ cofinite ↔ ∀ x, {x}ᶜ ∈ l := begin refine ⟨λ h x, h (finite_singleton x).compl_mem_cofinite, λ h s (hs : sᶜ.finite), _⟩, rw [← compl_compl s, ← bUnion_of_singleton sᶜ, compl_Union₂,filter.bInter_mem hs], exact λ x _, h x end lemma le_cofinite_iff_eventually_ne : l ≤ cofinite ↔ ∀ x, ∀ᶠ y in l, y ≠ x := le_cofinite_iff_compl_singleton_mem /-- If `α` is a preorder with no maximal element, then `at_top ≤ cofinite`. -/ lemma at_top_le_cofinite [preorder α] [no_max_order α] : (at_top : filter α) ≤ cofinite := le_cofinite_iff_eventually_ne.mpr eventually_ne_at_top lemma comap_cofinite_le (f : α → β) : comap f cofinite ≤ cofinite := le_cofinite_iff_eventually_ne.mpr $ λ x, mem_comap.2 ⟨{f x}ᶜ, (finite_singleton _).compl_mem_cofinite, λ y, ne_of_apply_ne f⟩ /-- The coproduct of the cofinite filters on two types is the cofinite filter on their product. -/ lemma coprod_cofinite : (cofinite : filter α).coprod (cofinite : filter β) = cofinite := filter.coext $ λ s, by simp only [compl_mem_coprod, mem_cofinite, compl_compl, finite_image_fst_and_snd_iff] /-- Finite product of finite sets is finite -/ lemma Coprod_cofinite {α : ι → Type*} [finite ι] : filter.Coprod (λ i, (cofinite : filter (α i))) = cofinite := filter.coext $ λ s, by simp only [compl_mem_Coprod, mem_cofinite, compl_compl, forall_finite_image_eval_iff] @[simp] lemma disjoint_cofinite_left : disjoint cofinite l ↔ ∃ s ∈ l, set.finite s := begin simp only [has_basis_cofinite.disjoint_iff l.basis_sets, id, disjoint_compl_left_iff_subset], exact ⟨λ ⟨s, hs, t, ht, hts⟩, ⟨t, ht, hs.subset hts⟩, λ ⟨s, hs, hsf⟩, ⟨s, hsf, s, hs, subset.rfl⟩⟩ end @[simp] lemma disjoint_cofinite_right : disjoint l cofinite ↔ ∃ s ∈ l, set.finite s := disjoint.comm.trans disjoint_cofinite_left end filter open filter /-- For natural numbers the filters `cofinite` and `at_top` coincide. -/ lemma nat.cofinite_eq_at_top : @cofinite ℕ = at_top := begin refine le_antisymm _ at_top_le_cofinite, refine at_top_basis.ge_iff.2 (λ N hN, _), simpa only [mem_cofinite, compl_Ici] using finite_lt_nat N end lemma nat.frequently_at_top_iff_infinite {p : ℕ → Prop} : (∃ᶠ n in at_top, p n) ↔ set.infinite {n | p n} := by rw [← nat.cofinite_eq_at_top, frequently_cofinite_iff_infinite] lemma filter.tendsto.exists_within_forall_le {α β : Type*} [linear_order β] {s : set α} (hs : s.nonempty) {f : α → β} (hf : filter.tendsto f filter.cofinite filter.at_top) : ∃ a₀ ∈ s, ∀ a ∈ s, f a₀ ≤ f a := begin rcases em (∃ y ∈ s, ∃ x, f y < x) with ⟨y, hys, x, hx⟩|not_all_top, { -- the set of points `{y | f y < x}` is nonempty and finite, so we take `min` over this set have : {y | ¬x ≤ f y}.finite := (filter.eventually_cofinite.mp (tendsto_at_top.1 hf x)), simp only [not_le] at this, obtain ⟨a₀, ⟨ha₀ : f a₀ < x, ha₀s⟩, others_bigger⟩ := exists_min_image _ f (this.inter_of_left s) ⟨y, hx, hys⟩, refine ⟨a₀, ha₀s, λ a has, (lt_or_le (f a) x).elim _ (le_trans ha₀.le)⟩, exact λ h, others_bigger a ⟨h, has⟩ }, { -- in this case, f is constant because all values are at top push_neg at not_all_top, obtain ⟨a₀, ha₀s⟩ := hs, exact ⟨a₀, ha₀s, λ a ha, not_all_top a ha (f a₀)⟩ } end lemma filter.tendsto.exists_forall_le [nonempty α] [linear_order β] {f : α → β} (hf : tendsto f cofinite at_top) : ∃ a₀, ∀ a, f a₀ ≤ f a := let ⟨a₀, _, ha₀⟩ := hf.exists_within_forall_le univ_nonempty in ⟨a₀, λ a, ha₀ a (mem_univ _)⟩ lemma filter.tendsto.exists_within_forall_ge [linear_order β] {s : set α} (hs : s.nonempty) {f : α → β} (hf : filter.tendsto f filter.cofinite filter.at_bot) : ∃ a₀ ∈ s, ∀ a ∈ s, f a ≤ f a₀ := @filter.tendsto.exists_within_forall_le _ βᵒᵈ _ _ hs _ hf lemma filter.tendsto.exists_forall_ge [nonempty α] [linear_order β] {f : α → β} (hf : tendsto f cofinite at_bot) : ∃ a₀, ∀ a, f a ≤ f a₀ := @filter.tendsto.exists_forall_le _ βᵒᵈ _ _ _ hf /-- For an injective function `f`, inverse images of finite sets are finite. See also `filter.comap_cofinite_le` and `function.injective.comap_cofinite_eq`. -/ lemma function.injective.tendsto_cofinite {f : α → β} (hf : injective f) : tendsto f cofinite cofinite := λ s h, h.preimage (hf.inj_on _) /-- The pullback of the `filter.cofinite` under an injective function is equal to `filter.cofinite`. See also `filter.comap_cofinite_le` and `function.injective.tendsto_cofinite`. -/ lemma function.injective.comap_cofinite_eq {f : α → β} (hf : injective f) : comap f cofinite = cofinite := (comap_cofinite_le f).antisymm hf.tendsto_cofinite.le_comap /-- An injective sequence `f : ℕ → ℕ` tends to infinity at infinity. -/ lemma function.injective.nat_tendsto_at_top {f : ℕ → ℕ} (hf : injective f) : tendsto f at_top at_top := nat.cofinite_eq_at_top ▸ hf.tendsto_cofinite
f308b3e984c5fb73ee2627dcaccee46e143441eb
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/real/golden_ratio.lean
dcb176a4d26680c35a8bac85a12a1e926f37dc43
[ "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,663
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Alexey Soloyev, Junyan Xu -/ import data.real.irrational import data.nat.fib import data.matrix.notation import tactic.ring_exp import algebra.linear_recurrence /-! # The golden ratio and its conjugate This file defines the golden ratio `φ := (1 + √5)/2` and its conjugate `ψ := (1 - √5)/2`, which are the two real roots of `X² - X - 1`. Along with various computational facts about them, we prove their irrationality, and we link them to the Fibonacci sequence by proving Binet's formula. -/ noncomputable theory /-- The golden ratio `φ := (1 + √5)/2`. -/ @[reducible] def golden_ratio := (1 + real.sqrt 5)/2 /-- The conjugate of the golden ratio `ψ := (1 - √5)/2`. -/ @[reducible] def golden_conj := (1 - real.sqrt 5)/2 localized "notation `φ` := golden_ratio" in real localized "notation `ψ` := golden_conj" in real /-- The inverse of the golden ratio is the opposite of its conjugate. -/ lemma inv_gold : φ⁻¹ = -ψ := begin have : 1 + real.sqrt 5 ≠ 0, from ne_of_gt (add_pos (by norm_num) $ real.sqrt_pos.mpr (by norm_num)), field_simp, apply mul_left_cancel' this, rw mul_div_cancel' _ this, rw [add_comm, ← sq_sub_sq], norm_num end /-- The opposite of the golden ratio is the inverse of its conjugate. -/ lemma inv_gold_conj : ψ⁻¹ = -φ := begin rw [inv_eq_iff, ← neg_inv, neg_eq_iff_neg_eq], exact inv_gold.symm, end @[simp] lemma gold_mul_gold_conj : φ * ψ = -1 := by {field_simp, rw ← sq_sub_sq, norm_num} @[simp] lemma gold_conj_mul_gold : ψ * φ = -1 := by {rw mul_comm, exact gold_mul_gold_conj} @[simp] lemma gold_add_gold_conj : φ + ψ = 1 := by {rw [golden_ratio, golden_conj], ring} lemma one_sub_gold_conj : 1 - φ = ψ := by linarith [gold_add_gold_conj] lemma one_sub_gold : 1 - ψ = φ := by linarith [gold_add_gold_conj] @[simp] lemma gold_sub_gold_conj : φ - ψ = real.sqrt 5 := by {rw [golden_ratio, golden_conj], ring} @[simp] lemma gold_sq : φ^2 = φ + 1 := begin rw [golden_ratio, ←sub_eq_zero], ring_exp, rw real.sqr_sqrt; norm_num, end @[simp] lemma gold_conj_sq : ψ^2 = ψ + 1 := begin rw [golden_conj, ←sub_eq_zero], ring_exp, rw real.sqr_sqrt; norm_num, end lemma gold_pos : 0 < φ := begin rw golden_ratio, linarith [real.sqrt_nonneg 5] end lemma gold_ne_zero : φ ≠ 0 := ne_of_gt gold_pos lemma one_lt_gold : 1 < φ := begin refine lt_of_mul_lt_mul_left _ (le_of_lt gold_pos), simp [← pow_two, gold_pos, zero_lt_one] end lemma gold_conj_neg : ψ < 0 := by linarith [one_sub_gold_conj, one_lt_gold] lemma gold_conj_ne_zero : ψ ≠ 0 := ne_of_lt gold_conj_neg lemma neg_one_lt_gold_conj : -1 < ψ := begin rw [neg_lt, ← inv_gold], exact inv_lt_one one_lt_gold, end /-! ## Irrationality -/ /-- The golden ratio is irrational. -/ theorem gold_irrational : irrational φ := begin have := nat.prime.irrational_sqrt (show nat.prime 5, by norm_num), have := this.rat_add 1, have := this.rat_mul (show (0.5 : ℚ) ≠ 0, by norm_num), convert this, field_simp end /-- The conjugate of the golden ratio is irrational. -/ theorem gold_conj_irrational : irrational ψ := begin have := nat.prime.irrational_sqrt (show nat.prime 5, by norm_num), have := this.rat_sub 1, have := this.rat_mul (show (0.5 : ℚ) ≠ 0, by norm_num), convert this, field_simp end /-! ## Links with Fibonacci sequence -/ section fibrec variables {α : Type*} [comm_semiring α] /-- The recurrence relation satisfied by the Fibonacci sequence. -/ def fib_rec : linear_recurrence α := { order := 2, coeffs := ![1, 1]} section poly open polynomial /-- The characteristic polynomial of `fib_rec` is `X² - (X + 1)`. -/ lemma fib_rec_char_poly_eq {β : Type*} [comm_ring β] : fib_rec.char_poly = X^2 - (X + (1 : polynomial β)) := begin rw [fib_rec, linear_recurrence.char_poly], simp [finset.sum_fin_eq_sum_range, finset.sum_range_succ', monomial_eq_smul_X] end end poly /-- As expected, the Fibonacci sequence is a solution of `fib_rec`. -/ lemma fib_is_sol_fib_rec : fib_rec.is_solution (λ x, x.fib : ℕ → α) := begin rw fib_rec, intros n, simp only, rw [nat.fib_succ_succ, add_comm], simp [finset.sum_fin_eq_sum_range, finset.sum_range_succ'] end /-- The geometric sequence `λ n, φ^n` is a solution of `fib_rec`. -/ lemma geom_gold_is_sol_fib_rec : fib_rec.is_solution (pow φ) := begin rw [fib_rec.geom_sol_iff_root_char_poly, fib_rec_char_poly_eq], simp [sub_eq_zero_iff_eq] end /-- The geometric sequence `λ n, ψ^n` is a solution of `fib_rec`. -/ lemma geom_gold_conj_is_sol_fib_rec : fib_rec.is_solution (pow ψ) := begin rw [fib_rec.geom_sol_iff_root_char_poly, fib_rec_char_poly_eq], simp [sub_eq_zero_iff_eq] end end fibrec /-- Binet's formula as a function equality. -/ theorem real.coe_fib_eq' : (λ n, nat.fib n : ℕ → ℝ) = λ n, (φ^n - ψ^n) / real.sqrt 5 := begin rw fib_rec.sol_eq_of_eq_init, { intros i hi, fin_cases hi, { simp }, { simp only [golden_ratio, golden_conj], ring_exp, rw mul_inv_cancel; norm_num } }, { exact fib_is_sol_fib_rec }, { ring, exact (@fib_rec ℝ _).sol_space.sub_mem (submodule.smul_mem fib_rec.sol_space (real.sqrt 5)⁻¹ geom_gold_is_sol_fib_rec) (submodule.smul_mem fib_rec.sol_space (real.sqrt 5)⁻¹ geom_gold_conj_is_sol_fib_rec) } end /-- Binet's formula as a dependent equality. -/ theorem real.coe_fib_eq : ∀ n, (nat.fib n : ℝ) = (φ^n - ψ^n) / real.sqrt 5 := by rw [← function.funext_iff, real.coe_fib_eq']
7d361452de48029a4c6d81e5823b868be77dee62
82e44445c70db0f03e30d7be725775f122d72f3e
/src/data/sym/basic.lean
8a473e659f09533eb4b5072957bce88d755258d3
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
4,653
lean
/- Copyright (c) 2020 Kyle Miller All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller -/ import data.multiset.basic import data.vector2 /-! # Symmetric powers This file defines symmetric powers of a type. The nth symmetric power consists of homogeneous n-tuples modulo permutations by the symmetric group. The special case of 2-tuples is called the symmetric square, which is addressed in more detail in `data.sym.sym2`. TODO: This was created as supporting material for `sym2`; it needs a fleshed-out interface. ## Tags symmetric powers -/ universes u /-- The nth symmetric power is n-tuples up to permutation. We define it as a subtype of `multiset` since these are well developed in the library. We also give a definition `sym.sym'` in terms of vectors, and we show these are equivalent in `sym.sym_equiv_sym'`. -/ def sym (α : Type u) (n : ℕ) := {s : multiset α // s.card = n} /-- This is the `list.perm` setoid lifted to `vector`. See note [reducible non-instances]. -/ @[reducible] def vector.perm.is_setoid (α : Type u) (n : ℕ) : setoid (vector α n) := { r := λ a b, list.perm a.1 b.1, iseqv := by { rcases list.perm.eqv α with ⟨hr, hs, ht⟩, tidy, } } local attribute [instance] vector.perm.is_setoid namespace sym variables {α : Type u} {n : ℕ} /-- This is the quotient map that takes a list of n elements as an n-tuple and produces an nth symmetric power. -/ def of_vector (x : vector α n) : sym α n := ⟨↑x.val, by { rw multiset.coe_card, exact x.2 }⟩ instance : has_lift (vector α n) (sym α n) := { lift := of_vector } /-- The unique element in `sym α 0`. -/ @[pattern] def nil : sym α 0 := ⟨0, by tidy⟩ /-- Inserts an element into the term of `sym α n`, increasing the length by one. -/ @[pattern] def cons : α → sym α n → sym α (nat.succ n) | a ⟨s, h⟩ := ⟨a ::ₘ s, by rw [multiset.card_cons, h]⟩ notation a :: b := cons a b @[simp] lemma cons_inj_right (a : α) (s s' : sym α n) : a :: s = a :: s' ↔ s = s' := by { cases s, cases s', delta cons, simp, } @[simp] lemma cons_inj_left (a a' : α) (s : sym α n) : a :: s = a' :: s ↔ a = a' := by { cases s, delta cons, simp, } lemma cons_swap (a b : α) (s : sym α n) : a :: b :: s = b :: a :: s := by { cases s, ext, delta cons, rw subtype.coe_mk, dsimp, exact multiset.cons_swap a b s_val } /-- `α ∈ s` means that `a` appears as one of the factors in `s`. -/ def mem (a : α) (s : sym α n) : Prop := a ∈ s.1 instance : has_mem α (sym α n) := ⟨mem⟩ instance decidable_mem [decidable_eq α] (a : α) (s : sym α n) : decidable (a ∈ s) := by { cases s, change decidable (a ∈ s_val), apply_instance } @[simp] lemma mem_cons {a b : α} {s : sym α n} : a ∈ b :: s ↔ a = b ∨ a ∈ s := begin cases s, change a ∈ b ::ₘ s_val ↔ a = b ∨ a ∈ s_val, simp, end lemma mem_cons_of_mem {a b : α} {s : sym α n} (h : a ∈ s) : a ∈ b :: s := mem_cons.2 (or.inr h) @[simp] lemma mem_cons_self (a : α) (s : sym α n) : a ∈ a :: s := mem_cons.2 (or.inl rfl) lemma cons_of_coe_eq (a : α) (v : vector α n) : a :: (↑v : sym α n) = ↑(a ::ᵥ v) := by { unfold_coes, delta of_vector, delta cons, delta vector.cons, tidy } lemma sound {a b : vector α n} (h : a.val ~ b.val) : (↑a : sym α n) = ↑b := begin cases a, cases b, unfold_coes, dunfold of_vector, simp only [subtype.mk_eq_mk, multiset.coe_eq_coe], exact h, end /-- Another definition of the nth symmetric power, using vectors modulo permutations. (See `sym`.) -/ def sym' (α : Type u) (n : ℕ) := quotient (vector.perm.is_setoid α n) /-- This is `cons` but for the alternative `sym'` definition. -/ def cons' {α : Type u} {n : ℕ} : α → sym' α n → sym' α (nat.succ n) := λ a, quotient.map (vector.cons a) (λ ⟨l₁, h₁⟩ ⟨l₂, h₂⟩ h, list.perm.cons _ h) notation a :: b := cons' a b /-- Multisets of cardinality n are equivalent to length-n vectors up to permutations. -/ def sym_equiv_sym' {α : Type u} {n : ℕ} : sym α n ≃ sym' α n := equiv.subtype_quotient_equiv_quotient_subtype _ _ (λ _, by refl) (λ _ _, by refl) lemma cons_equiv_eq_equiv_cons (α : Type u) (n : ℕ) (a : α) (s : sym α n) : a :: sym_equiv_sym' s = sym_equiv_sym' (a :: s) := by tidy section inhabited -- Instances to make the linter happy instance inhabited_sym [inhabited α] (n : ℕ) : inhabited (sym α n) := ⟨⟨multiset.repeat (default α) n, multiset.card_repeat _ _⟩⟩ instance inhabited_sym' [inhabited α] (n : ℕ) : inhabited (sym' α n) := ⟨quotient.mk' (vector.repeat (default α) n)⟩ end inhabited end sym
350dac8a4331f346940900ee47a6a360e6f22b48
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/smt/prove_auto.lean
4dafa2e611fe5d820b31d47a317e2bce25bc1423
[]
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
91
lean
import Mathlib.PrePort import Mathlib.Lean3Lib.init.default namespace Mathlib end Mathlib
f39ab54479a4000f0db554e720df0536a3ef6596
271e26e338b0c14544a889c31c30b39c989f2e0f
/src/Init/Lean/Compiler/Util.lean
974d5b133100145a5931e08ed3d8dce679af1a27
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,906
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Lean.Environment namespace Lean namespace Compiler def neutralExpr : Expr := mkConst `_neutral def unreachableExpr : Expr := mkConst `_unreachable def objectType : Expr := mkConst `_obj def voidType : Expr := mkConst `_void def mkLcProof (pred : Expr) := mkApp (mkConst `lcProof []) pred namespace atMostOnce structure AtMostOnceData := (found result : Bool) def Visitor := AtMostOnceData → AtMostOnceData @[inline] def seq (f g : Visitor) : Visitor := fun d => match f d with | ⟨found, false⟩ => ⟨found, false⟩ | other => g other instance : HasAndthen Visitor := ⟨seq⟩ @[inline] def skip : Visitor := id @[inline] def visitFVar (x y : Name) : Visitor | d@{result := false, ..} => d | {found := false, result := true} => {found := x == y, result := true} | {found := true, result := true} => {found := true, result := x != y} def visit (x : Name) : Expr → Visitor | Expr.fvar y _ => visitFVar y x | Expr.app f a _ => visit a >> visit f | Expr.lam _ d b _ => visit d >> visit b | Expr.forallE _ d b _ => visit d >> visit b | Expr.letE _ t v b _ => visit t >> visit v >> visit b | Expr.mdata _ e _ => visit e | Expr.proj _ _ e _ => visit e | _ => skip end atMostOnce /-- Return true iff the free variable with id `x` occurs at most once in `e` -/ @[export lean_at_most_once] def atMostOnce (e : Expr) (x : Name) : Bool := let {result := result, ..} := atMostOnce.visit x e {found := false, result := true}; result /- Helper functions for creating auxiliary names used in compiler passes. -/ @[export lean_mk_eager_lambda_lifting_name] def mkEagerLambdaLiftingName (n : Name) (idx : Nat) : Name := mkNameStr n ("_elambda_" ++ toString idx) @[export lean_is_eager_lambda_lifting_name] def isEagerLambdaLiftingName : Name → Bool | Name.str p s _ => "_elambda".isPrefixOf s || isEagerLambdaLiftingName p | Name.num p _ _ => isEagerLambdaLiftingName p | _ => false /-- Return the name of new definitions in the a given declaration. Here we consider only declarations we generate code for. We use this definition to implement `add_and_compile`. -/ @[export lean_get_decl_names_for_code_gen] private def getDeclNamesForCodeGen : Declaration → List Name | Declaration.defnDecl { name := n, .. } => [n] | Declaration.mutualDefnDecl defs => defs.map $ fun d => d.name | _ => [] def checkIsDefinition (env : Environment) (n : Name) : Except String Unit := match env.find? n with | (some (ConstantInfo.defnInfo _)) => Except.ok () | none => Except.error "unknow declaration" | _ => Except.error "declaration is not a definition" end Compiler end Lean
d33b11aba0523942631997f7e2c335214d87198b
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/data/prod/default.lean
d3e25a6d4f331ccbaa1341f06e9aacdc3be4bd7f
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
231
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura, Jeremy Avigad import data.prod.decl data.prod.thms data.prod.wf
0bbfe2d32d3c925055282d2369e0f1581654599b
5883d9218e6f144e20eee6ca1dab8529fa1a97c0
/src/data/has_mem.lean
2f8808dc419c73f2067d630deabd4049205eeee7
[]
no_license
spl/alpha-conversion-is-easy
0d035bc570e52a6345d4890e4d0c9e3f9b8126c1
ed937fe85d8495daffd9412a5524c77b9fcda094
refs/heads/master
1,607,649,280,020
1,517,380,240,000
1,517,380,240,000
52,174,747
4
0
null
1,456,052,226,000
1,456,001,163,000
Lean
UTF-8
Lean
false
false
290
lean
/- This file defines the `decidable_eq` instance of the `has_mem` class. -/ universes u v variables {α : Type u} {γ : Type v} instance has_mem.decidable_eq (a : α) (b : γ) [has_mem α γ] : decidable_eq (has_mem.mem a b) := λ p₁ p₂, decidable.is_true (proof_irrel p₁ p₂)
6215a708a3174194fb54740e452ba7c130b07d60
ec62863c729b7eedee77b86d974f2c529fa79d25
/11/a.lean
e5fdc65d0bd21489ab15cfdac02f922e9c24fb4a
[]
no_license
rwbarton/advent-of-lean-4
2ac9b17ba708f66051e3d8cd694b0249bc433b65
417c7e2718253ba7148c0279fcb251b6fc291477
refs/heads/main
1,675,917,092,057
1,609,864,581,000
1,609,864,581,000
317,700,289
24
0
null
null
null
null
UTF-8
Lean
false
false
1,814
lean
def Array.mkFun {α : Type} (n : Nat) (f : Nat → α) : Array α := Array.mk ((List.range n).map f) inductive Seat | floor | empty | occupied open Seat def Seat.beq : Seat → Seat → Bool | floor, floor => true | empty, empty => true | occupied, occupied => true | _, _ => false instance : BEq Seat := ⟨Seat.beq⟩ instance : Inhabited Seat := ⟨floor⟩ def State := Array (Array Seat) instance : BEq State := show BEq (Array (Array Seat)) from inferInstance def State.get_or_floor (s : State) (r c : Int) : Seat := if r < 0 || r ≥ s.size then floor else if c < 0 || c ≥ (s.get! r.toNat).size then floor else (s.get! r.toNat).get! c.toNat instance : Monad List := { pure := λ a => [a], map := List.map, bind := λ x f => (x.map f).join } def adjs (r c : Int) : List (Int × Int) := do let dr ← [(-1 : Int), 0, 1] let dc ← [(-1 : Int), 0, 1] if dr == 0 && dc == 0 then [] pure (r + dr, c + dc) def step (s : State) : State := Array.mkFun s.size $ λ row => Array.mkFun (s.get! 0).size $ λ col => let nbrs := ((adjs row col).filter (λ ⟨r, c⟩ => s.get_or_floor r c == occupied)).length match (s.get! row).get! col with | floor => floor | empty => if nbrs == 0 then occupied else empty | occupied => if nbrs ≥ 4 then empty else occupied def count (s : State) : Int := s.foldl (λ c s' => c + s'.foldl (λ c' seat => c' + if seat == occupied then 1 else 0) 0) 0 partial def solve (s : State) : Int := -- dbgTrace s!"{count s}" $ λ _ => let s' := step s if s == s' then count s else solve s' def parse (str : Array String) : State := str.map $ λ s => Array.mk $ s.toList.map $ λ c => match c with | '.' => floor | 'L' => empty | '#' => occupied | _ => panic! "invalid input" def main : IO Unit := do let input ← IO.FS.lines "a.in" IO.print s!"{solve (parse input)}\n"
9d2c11339f81f58172393d8b44a571c6e3da7c2c
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/commutative_algebra/localization.lean
40730551395370ce9145d38db5b4552c82d729f8
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,207
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland This is about localization of commutative rings and semirings. The most common approach is to start with a commutative ring `A` and a subset `S` that contains `1` and is closed under multiplication. We then define a set `A[S⁻¹]` of equivalence classes of fractions `a/s` (with `a ∈ A` and `s ∈ S`), and introduce a ring structure on `A[S⁻¹]`, and prove various things about it. In particular, we prove a universal property. In this file, we deviate in various ways from the above approach. Firstly, we work with semirings rather than rings. This wider generality has some applications in stable homotopy theory, for example: given a finite group `G` it is useful to be able to work with the Burnside semiring of isomorphism classes of finite `G`-sets, or the representation semiring of isomorphism classes of finite-dimensional representation, rather than immediately passing to the group completion. Secondly, it is technically convenient to take `S` to be a commutative monoid equipped with a homomorphism `i` from `S` to the multiplicative monoid of `A`, without insisting that `i` is just the inclusion of a submonoid, or even that `i` is injective. For example, when forming `A[a⁻¹]` for a single element `a`, we take `S = ℕ` and `i n = aⁿ`. This does not cause any real issues. Triples `(A,S,i)` of this type play a central role in the theory of log geometry, so we have chosen to call them "prelog semirings". We have bundled the ingredients together as a structure; it is not clear whether this is the best approach. Next, to stay as long as possible in the constructive/ effectively computable world, it is useful to introduce as much structure as possible on the set `A × S` of unreduced fractions, before passing to equivalence classes. All semiring operations can be defined in this context, and most axioms are satisfied, and the remaining axioms fail in a well-controlled way. One thing that is currently missing is as follows. Suppose we have `S` and `i` as before, and a semiring homomorphism `f : A → B`. Suppose that the homomorphism `f ∘ i` lands in invertible elements of `B`, or more constructively, that we have a given homomorphism `j : S → units B` lifting `f ∘ i`. (There are some wrinkles here about the fact that unique existence statements do not give computable witnesses, which is why we need to qualify the above statement.) We then get an induced map `A[S⁻¹] → B`. We would like to have a simple criterion for when this map is an isomorphism, or equivalently when `B` also has the universal property that characterises `A[S⁻¹]`. It is not yet clear what is the best way to formulate this. Ideally, the resulting maps out of `B` should be effectively computable in some natural cases. -/ import algebra.ring group_theory.submonoid ring_theory.ideals linear_algebra.basic import tactic.ring tactic.abel namespace localization_alt universes u v w x structure prelog_semiring := (A : Type u) (S : Type v) (i : S → A) (A_comm_semiring : comm_semiring A) (S_comm_monoid : comm_monoid S) (i_hom : is_monoid_hom i) namespace prelog_semiring open prelog_semiring instance to_comm_semiring (P : prelog_semiring) : comm_semiring P.A := P.A_comm_semiring instance to_comm_monoid_S (P : prelog_semiring) : comm_monoid P.S := P.S_comm_monoid instance to_comm_monoid_A (P : prelog_semiring) : comm_monoid P.A := @comm_semiring.to_comm_monoid P.A P.A_comm_semiring instance to_monoid_S (P : prelog_semiring) : monoid P.S := @comm_monoid.to_monoid P.S (@prelog_semiring.to_comm_monoid_S P) instance to_monoid_A (P : prelog_semiring) : monoid P.A := @comm_monoid.to_monoid P.A (@prelog_semiring.to_comm_monoid_A P) instance to_monoid_hom (P : prelog_semiring) : is_monoid_hom P.i := P.i_hom structure preloc (P : prelog_semiring) := (numer : P.A) (denom : P.S) namespace preloc open preloc variable {P : prelog_semiring} notation a `/₀` s := preloc.mk a s @[extensionality] lemma ext : ∀ (a₁ a₂ : P.A) (s₁ s₂ : P.S), a₁ = a₂ → s₁ = s₂ → (a₁ /₀ s₁) = (a₂ /₀ s₂) := by { intros; congr; assumption } lemma ext' : ∀ {x₁ x₂ : preloc P}, x₁ = x₂ ↔ (x₁.numer = x₂.numer) ∧ (x₁.denom = x₂.denom) | ⟨_,_⟩ ⟨_,_⟩ := by { simp } instance : comm_monoid (preloc P) := begin let one : (preloc P) := (1 /₀ 1), let mul : (preloc P) → (preloc P) → (preloc P) := λ x y, ((x.numer * y.numer) /₀ (x.denom * y.denom)), let one_mul : ∀ (x : preloc P), mul one x = x := begin intro x,cases x with a s, exact congr (congr_arg preloc.mk (one_mul a)) (one_mul s), end, let mul_one : ∀ (x : preloc P), mul x one = x := begin intro x,cases x with a s, exact congr (congr_arg preloc.mk (mul_one a)) (mul_one s), end, let mul_comm : ∀ x y, mul x y = mul y x := begin intros x y,cases x with a s,cases y with b t, exact @congr P.S (preloc P) _ _ _ _ (congr_arg (@preloc.mk P) (mul_comm a b)) (mul_comm s t), end, let mul_assoc : ∀ x y z, mul (mul x y) z = mul x (mul y z) := begin intros x y z,cases x with a s, cases y with b t, cases z with c u, exact @congr P.S (preloc P) _ _ _ _ (congr_arg (@preloc.mk P) (mul_assoc a b c)) (mul_assoc s t u), end, exact { one := one, mul := mul, mul_one := mul_one, one_mul := one_mul, mul_comm := mul_comm, mul_assoc := mul_assoc } end instance : add_comm_monoid (preloc P) := begin let zero : (preloc P) := (0 /₀ 1), let add : (preloc P) → (preloc P) → (preloc P) := λ x y, ((x.numer * (P.i y.denom) + (P.i x.denom) * y.numer) /₀ (x.denom * y.denom)), let zero_add : ∀ (x : preloc P), add zero x = x := begin intro x,cases x with a s, let h0 := (congr_fun (congr_arg P.A_comm_semiring.mul P.i_hom.map_one) a), let h1 := congr (congr_arg P.A_comm_semiring.add (zero_mul (P.i s))) h0, let h2 := h1.trans ((zero_add (1 * a)).trans (one_mul a)), exact congr (congr_arg preloc.mk h2) (one_mul s), end, let add_zero : ∀ (x : preloc P), add x zero = x := begin intro x,cases x with a s, dsimp[zero,add], let h0 := (congr_arg ((*) a) P.i_hom.map_one).trans (mul_one a), let h1 := (congr (congr_arg (+) h0) (mul_zero (P.i s))).trans (add_zero a), exact congr (congr_arg preloc.mk h1) (mul_one s), end, let add_comm : ∀ x y, add x y = add y x := begin intros x y,cases x with a s,cases y with b t, dsimp[add], rw[mul_comm s t,mul_comm a (P.i t),mul_comm (P.i s) b,add_comm], end, let add_assoc : ∀ x y z, add (add x y) z = add x (add y z) := begin intros x y z,cases x with a s, cases y with b t, cases z with c u, dsimp[add], rw[P.i_hom.map_mul,P.i_hom.map_mul,add_mul,mul_add], rw[add_assoc,mul_assoc s t u,mul_assoc a (P.i t) (P.i u), mul_assoc (P.i s) b (P.i u),mul_assoc (P.i s) (P.i t) c], end, exact { zero := zero, add := add, add_zero := add_zero, zero_add := zero_add, add_comm := add_comm, add_assoc := add_assoc }, end lemma numer_def (a : P.A) (s : P.S) : (a /₀ s).numer = a := rfl lemma denom_def (a : P.A) (s : P.S) : (a /₀ s).denom = s := rfl lemma numer_zero : @preloc.numer P 0 = 0 := rfl lemma denom_zero : @preloc.denom P 0 = 1 := rfl lemma numer_one : @preloc.numer P 1 = 1 := rfl lemma denom_one : @preloc.denom P 1 = 1 := rfl lemma one_def : (1 : preloc P) = (1 /₀ 1) := rfl lemma mul_def (x y : preloc P) : x * y = ((x.numer * y.numer) /₀ (x.denom * y.denom)) := rfl lemma mul_def' (a b : P.A) (s t : P.S) : (a /₀ s) * (b /₀ t) = ((a * b) /₀ (s * t)) := rfl lemma zero_def : (0 : preloc P) = (0 /₀ 1) := rfl lemma add_def (x y : preloc P) : x + y = (((x.numer * (P.i y.denom) + (P.i x.denom) * y.numer):P.A) /₀ (x.denom * y.denom)) := rfl lemma add_def' (a b : P.A) (s t : P.S) : (a /₀ s) + (b /₀ t) = (((a * (P.i t) + (P.i s) * b)) /₀ (s * t)) := rfl def η₀ (a : P.A) : preloc P := a /₀ 1 lemma numer_eta (a : P.A) : (η₀ a).numer = a := rfl lemma denom_eta (a : P.A) : (η₀ a).denom = 1 := rfl lemma eta_map_zero : @η₀ P 0 = 0 := rfl lemma eta_map_one : @η₀ P 1 = 1 := rfl lemma eta_map_mul (a b : P.A) : η₀ (a * b) = (η₀ a) * (η₀ b) := begin ext, rw[numer_eta,numer_eta], rw[denom_eta,denom_eta,mul_one] end lemma eta_map_add (a b : P.A) : η₀ (a + b) = (η₀ a) + (η₀ b) := begin ext, rw[numer_eta,numer_eta,denom_eta,denom_eta, P.i_hom.map_one,mul_one,one_mul], rw[denom_eta,denom_eta,mul_one], end structure R (x x' : preloc P) := (s : P.S) (e : P.i (s * x'.denom) * x.numer = P.i (s * x.denom) * x'.numer) def R_refl (x : preloc P) : R x x := {s := 1, e := rfl} def R_symm {x x' : preloc P} (m : R x x') : R x' x := {s := m.s, e := m.e.symm} def R_trans {x x' x'' : preloc P} (m : R x x') (n : R x' x'') : R x x'' := begin rcases x with ⟨a,u⟩, rcases x' with ⟨a',u'⟩, rcases x'' with ⟨a'',u''⟩, let ss := m.s * n.s * u', let ee := calc P.i (ss * u'') * a = P.i (n.s * u'') * (P.i (m.s * u') * a) : by {repeat {rw[P.i_hom.map_mul]}, ring,} ... = P.i (n.s * u'') * (P.i (m.s * u) * a') : by rw[m.e] ... = P.i (m.s * u) * (P.i (n.s * u'') * a') : by ring ... = P.i (m.s * u) * (P.i (n.s * u') * a'') : by rw[n.e] ... = P.i (ss * u) * a'' : by {repeat {rw[P.i_hom.map_mul]}, ring,}, exact { s := ss, e := ee } end def R_zero : (R 0 0) := R_refl (0 : preloc P) def R_one : (R 1 1) := R_refl (1 : preloc P) def R_add {x y x' y' : preloc P} (m : R x x') (n : R y y') : R (x + y) (x' + y') := begin rcases x with ⟨a,u⟩, rcases x' with ⟨a',u'⟩, rcases y with ⟨b,v⟩, rcases y' with ⟨b',v'⟩, let ss := m.s * n.s, let ee := calc (P.i (ss * (u' * v'))) * (a * (P.i v) + (P.i u) * b) = P.i (n.s * v * v') * (P.i (m.s * u') * a) + P.i (m.s * u * u') * (P.i (n.s * v') * b) : by {repeat {rw[P.i_hom.map_mul]}, ring,} ... = P.i (n.s * v * v') * (P.i (m.s * u) * a') + P.i (m.s * u * u') * (P.i (n.s * v) * b') : by rw[m.e,n.e] ... = (P.i (ss * (u * v))) * (a' * (P.i v') + (P.i u') * b') : by {repeat {rw[P.i_hom.map_mul]}, ring,}, exact { s := ss, e := ee }, end def R_mul {x y x' y' : preloc P} (m : R x x') (n : R y y') : R (x * y) (x' * y') := begin rcases x with ⟨a,u⟩, rcases x' with ⟨a',u'⟩, rcases y with ⟨b,v⟩, rcases y' with ⟨b',v'⟩, let ss := m.s * n.s, let ee := calc (P.i (ss * (u' * v'))) * (a * b) = (P.i (m.s * u') * a) * (P.i (n.s * v') * b) : by {repeat {rw[P.i_hom.map_mul]}, ring,} ... = (P.i (m.s * u) * a') * (P.i (n.s * v) * b') : by rw[m.e,n.e] ... = (P.i (ss * (u * v))) * (a' * b') : by {repeat {rw[P.i_hom.map_mul]}, ring,}, exact { s := ss, e := ee }, end def R_zero_mul : ∀ (x : preloc P), R (0 * x) 0 | ⟨a,u⟩ := {s := 1,e := begin show (P.i (1 * 1)) * (0 * a) = (P.i (1 * (1 * u))) * 0, rw[_root_.zero_mul,_root_.mul_zero,_root_.mul_zero], end} def R_mul_zero : ∀ (x : preloc P), R (x * 0) 0 | ⟨a,u⟩ := {s := 1,e := begin show (P.i (1 * 1)) * (a * 0) = (P.i (1 * (u * 1))) * 0, repeat {rw[_root_.mul_zero]}, end} def R_mul_add (x y z : preloc P) : R (x * (y + z)) (x * y + x * z) := begin rcases x with ⟨a,u⟩, rcases y with ⟨b,v⟩, rcases z with ⟨c,w⟩, have e : (P.i (1 * ((u * v) * (u * w)))) * (a * (b * (P.i w) + (P.i v) * c)) = (P.i (1 * (u * (v *w)))) * ((a * b) * P.i (u * w) + P.i (u * v) * (a * c)) := by {repeat {rw[P.i_hom.map_mul]}, ring,}, exact {s := 1, e := e}, end def R_add_mul (x y z : preloc P) : R ((x + y) * z) (x * z + y * z) := begin rcases x with ⟨a,u⟩, rcases y with ⟨b,v⟩, rcases z with ⟨c,w⟩, have e : (P.i (1 * ((u * w) * (v * w)))) * ((a * (P.i v) + (P.i u) * b) * c) = (P.i (1 * (u * v *w))) * ((a * c) * P.i (v * w) + P.i (u * w) * (b * c)) := by {repeat {rw[P.i_hom.map_mul]}, ring,}, exact {s := 1, e := e}, end variable (P) instance loc_equiv : setoid (preloc P) := { r := λ x x', nonempty (R x x'), iseqv := ⟨λ x,⟨R_refl x⟩, λ x x',by {rintro ⟨m⟩,exact ⟨R_symm m⟩}, λ x x' x'',by {rintros ⟨m⟩ ⟨n⟩, exact ⟨R_trans m n⟩}⟩ } lemma r_add {x y x' y' : preloc P} : ∀ (m : x ≈ x') (n : y ≈ y'), (x + y) ≈ (x' + y') | ⟨m⟩ ⟨n⟩ := ⟨R_add m n⟩ lemma r_mul {x y x' y' : preloc P} : ∀ (m : x ≈ x') (n : y ≈ y'), (x * y) ≈ (x' * y') | ⟨m⟩ ⟨n⟩ := ⟨R_mul m n⟩ end preloc def loc (P : prelog_semiring) := quotient (preloc.loc_equiv P) namespace loc variable {P : prelog_semiring} def mk0 : (preloc P) → (loc P) := quotient.mk def mk (a : P.A) (s : P.S) : loc P := mk0 (a /₀ s) notation a `/₁` s := loc.mk a s instance : comm_semiring (loc P) := { zero := mk0 0, one := mk0 1, add := quotient.lift₂ (λ x y, mk0 (x + y)) (λ x y x' y' m n, quotient.sound (@preloc.r_add P x y x' y' m n)), mul := quotient.lift₂ (λ x y, mk0 (x * y)) (λ x y x' y' m n, quotient.sound (@preloc.r_mul P x y x' y' m n)), zero_add := λ x,by {rcases x,exact congr_arg mk0 (zero_add x),}, add_zero := λ x,by {rcases x,exact congr_arg mk0 (add_zero x),}, one_mul := λ x,by {rcases x,exact congr_arg mk0 (one_mul x),}, mul_one := λ x,by {rcases x,exact congr_arg mk0 (mul_one x),}, zero_mul := λ x,by {rcases x,exact quotient.sound ⟨preloc.R_zero_mul x⟩,}, mul_zero := λ x,by {rcases x,exact quotient.sound ⟨preloc.R_mul_zero x⟩,}, add_comm := λ x y,by {rcases x, rcases y, exact congr_arg mk0 (add_comm x y)}, mul_comm := λ x y,by {rcases x, rcases y, exact congr_arg mk0 (mul_comm x y)}, add_assoc := λ x y z,by {rcases x,rcases y,rcases z, exact congr_arg mk0 (add_assoc x y z)}, mul_assoc := λ x y z,by {rcases x,rcases y,rcases z, exact congr_arg mk0 (mul_assoc x y z)}, left_distrib := λ x y z,by {rcases x,rcases y,rcases z, exact quotient.sound ⟨@preloc.R_mul_add P x y z⟩, }, right_distrib := λ x y z,by {rcases x,rcases y,rcases z, exact quotient.sound ⟨@preloc.R_add_mul P x y z⟩, }, } def η (a : P.A) : loc P := a /₁ 1 instance (P : prelog_semiring) : is_semiring_hom (@η P) := { map_zero := rfl, map_one := rfl, map_add := λ a b,congr_arg mk0 (preloc.eta_map_add a b), map_mul := λ a b,congr_arg mk0 (preloc.eta_map_mul a b), } def ζ (s : P.S) : units (loc P) := begin let val : loc P := ((P.i s) /₁ 1), let inv : loc P := (1 /₁ s), let val_inv : val * inv = 1 := begin show mk0 (((P.i s) * 1) /₀ (1 * s)) = mk0 (1 /₀ 1), let h : (((P.i s) * 1) /₀ (1 * s)) ≈ (1 /₀ 1) := ⟨{ s := 1 , e := by {simp[P.i_hom.map_one],}}⟩ , exact quotient.sound h, end, let inv_val := (mul_comm inv val).trans val_inv, exact { val := val, inv := inv, val_inv := val_inv, inv_val := inv_val }, end end loc end prelog_semiring end localization_alt
1bedc9e9f757f9de0503a6308505c32731dfe60f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/category/Semilat.lean
49a62906cbed40e2aea3287d003aabeee6da4c25
[ "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
5,105
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 order.category.PartOrd import order.hom.lattice /-! # The categories of semilattices > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This defines `SemilatSup` and `SemilatInf`, the categories of sup-semilattices with a bottom element and inf-semilattices with a top element. ## References * [nLab, *semilattice*](https://ncatlab.org/nlab/show/semilattice) -/ universes u open category_theory /-- The category of sup-semilattices with a bottom element. -/ structure SemilatSup : Type.{u+1} := (X : Type.{u}) [is_semilattice_sup : semilattice_sup X] [is_order_bot : order_bot X] /-- The category of inf-semilattices with a top element. -/ structure SemilatInf : Type.{u+1} := (X : Type.{u}) [is_semilattice_inf : semilattice_inf X] [is_order_top : order_top X] attribute [protected] SemilatSup.X SemilatInf.X namespace SemilatSup instance : has_coe_to_sort SemilatSup Type* := ⟨SemilatSup.X⟩ attribute [instance] is_semilattice_sup is_order_bot /-- Construct a bundled `SemilatSup` from a `semilattice_sup`. -/ def of (α : Type*) [semilattice_sup α] [order_bot α] : SemilatSup := ⟨α⟩ @[simp] lemma coe_of (α : Type*) [semilattice_sup α] [order_bot α] : ↥(of α) = α := rfl instance : inhabited SemilatSup := ⟨of punit⟩ instance : large_category.{u} SemilatSup := { hom := λ X Y, sup_bot_hom X Y, id := λ X, sup_bot_hom.id X, comp := λ X Y Z f g, g.comp f, id_comp' := λ X Y, sup_bot_hom.comp_id, comp_id' := λ X Y, sup_bot_hom.id_comp, assoc' := λ W X Y Z _ _ _, sup_bot_hom.comp_assoc _ _ _ } instance : concrete_category SemilatSup := { forget := { obj := SemilatSup.X, map := λ X Y, coe_fn }, forget_faithful := ⟨λ X Y, fun_like.coe_injective⟩ } instance has_forget_to_PartOrd : has_forget₂ SemilatSup PartOrd := { forget₂ := { obj := λ X, ⟨X⟩, map := λ X Y f, f } } @[simp] lemma coe_forget_to_PartOrd (X : SemilatSup) : ↥((forget₂ SemilatSup PartOrd).obj X) = ↥X := rfl end SemilatSup namespace SemilatInf instance : has_coe_to_sort SemilatInf Type* := ⟨SemilatInf.X⟩ attribute [instance] is_semilattice_inf is_order_top /-- Construct a bundled `SemilatInf` from a `semilattice_inf`. -/ def of (α : Type*) [semilattice_inf α] [order_top α] : SemilatInf := ⟨α⟩ @[simp] lemma coe_of (α : Type*) [semilattice_inf α] [order_top α] : ↥(of α) = α := rfl instance : inhabited SemilatInf := ⟨of punit⟩ instance : large_category.{u} SemilatInf := { hom := λ X Y, inf_top_hom X Y, id := λ X, inf_top_hom.id X, comp := λ X Y Z f g, g.comp f, id_comp' := λ X Y, inf_top_hom.comp_id, comp_id' := λ X Y, inf_top_hom.id_comp, assoc' := λ W X Y Z _ _ _, inf_top_hom.comp_assoc _ _ _ } instance : concrete_category SemilatInf := { forget := { obj := SemilatInf.X, map := λ X Y, coe_fn }, forget_faithful := ⟨λ X Y, fun_like.coe_injective⟩ } instance has_forget_to_PartOrd : has_forget₂ SemilatInf PartOrd := { forget₂ := { obj := λ X, ⟨X⟩, map := λ X Y f, f } } @[simp] lemma coe_forget_to_PartOrd (X : SemilatInf) : ↥((forget₂ SemilatInf PartOrd).obj X) = ↥X := rfl end SemilatInf /-! ### Order dual -/ namespace SemilatSup /-- Constructs an isomorphism of lattices from an order isomorphism between them. -/ @[simps] def iso.mk {α β : SemilatSup.{u}} (e : α ≃o β) : α ≅ β := { hom := e, inv := e.symm, hom_inv_id' := by { ext, exact e.symm_apply_apply _ }, inv_hom_id' := by { ext, exact e.apply_symm_apply _ } } /-- `order_dual` as a functor. -/ @[simps] def dual : SemilatSup ⥤ SemilatInf := { obj := λ X, SemilatInf.of Xᵒᵈ, map := λ X Y, sup_bot_hom.dual } end SemilatSup namespace SemilatInf /-- Constructs an isomorphism of lattices from an order isomorphism between them. -/ @[simps] def iso.mk {α β : SemilatInf.{u}} (e : α ≃o β) : α ≅ β := { hom := e, inv := e.symm, hom_inv_id' := by { ext, exact e.symm_apply_apply _ }, inv_hom_id' := by { ext, exact e.apply_symm_apply _ } } /-- `order_dual` as a functor. -/ @[simps] def dual : SemilatInf ⥤ SemilatSup := { obj := λ X, SemilatSup.of Xᵒᵈ, map := λ X Y, inf_top_hom.dual } end SemilatInf /-- The equivalence between `SemilatSup` and `SemilatInf` induced by `order_dual` both ways. -/ @[simps functor inverse] def SemilatSup_equiv_SemilatInf : SemilatSup ≌ SemilatInf := equivalence.mk SemilatSup.dual SemilatInf.dual (nat_iso.of_components (λ X, SemilatSup.iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl) (nat_iso.of_components (λ X, SemilatInf.iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl) lemma SemilatSup_dual_comp_forget_to_PartOrd : SemilatSup.dual ⋙ forget₂ SemilatInf PartOrd = forget₂ SemilatSup PartOrd ⋙ PartOrd.dual := rfl lemma SemilatInf_dual_comp_forget_to_PartOrd : SemilatInf.dual ⋙ forget₂ SemilatSup PartOrd = forget₂ SemilatInf PartOrd ⋙ PartOrd.dual := rfl
30ff531b3019b96fb4cb9b9a4ae81789c584c93b
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/geometry/manifold/instances/sphere.lean
c7a7a668c29bf9550bff941acff3f20202fb4013
[ "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
25,815
lean
/- Copyright (c) 2021 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.complex.circle import analysis.normed_space.ball_action import analysis.inner_product_space.calculus import analysis.inner_product_space.pi_L2 import geometry.manifold.algebra.lie_group import geometry.manifold.instances.real /-! # Manifold structure on the sphere This file defines stereographic projection from the sphere in an inner product space `E`, and uses it to put a smooth manifold structure on the sphere. ## Main results For a unit vector `v` in `E`, the definition `stereographic` gives the stereographic projection centred at `v`, a local homeomorphism from the sphere to `(ℝ ∙ v)ᗮ` (the orthogonal complement of `v`). For finite-dimensional `E`, we then construct a smooth manifold instance on the sphere; the charts here are obtained by composing the local homeomorphisms `stereographic` with arbitrary isometries from `(ℝ ∙ v)ᗮ` to Euclidean space. We prove two lemmas about smooth maps: * `cont_mdiff_coe_sphere` states that the coercion map from the sphere into `E` is smooth; this is a useful tool for constructing smooth maps *from* the sphere. * `cont_mdiff.cod_restrict_sphere` states that a map from a manifold into the sphere is smooth if its lift to a map to `E` is smooth; this is a useful tool for constructing smooth maps *to* the sphere. As an application we prove `cont_mdiff_neg_sphere`, that the antipodal map is smooth. Finally, we equip the `circle` (defined in `analysis.complex.circle` to be the sphere in `ℂ` centred at `0` of radius `1`) with the following structure: * a charted space with model space `euclidean_space ℝ (fin 1)` (inherited from `metric.sphere`) * a Lie group with model with corners `𝓡 1` We furthermore show that `exp_map_circle` (defined in `analysis.complex.circle` to be the natural map `λ t, exp (t * I)` from `ℝ` to `circle`) is smooth. ## Implementation notes The model space for the charted space instance is `euclidean_space ℝ (fin n)`, where `n` is a natural number satisfying the typeclass assumption `[fact (finrank ℝ E = n + 1)]`. This may seem a little awkward, but it is designed to circumvent the problem that the literal expression for the dimension of the model space (up to definitional equality) determines the type. If one used the naive expression `euclidean_space ℝ (fin (finrank ℝ E - 1))` for the model space, then the sphere in `ℂ` would be a manifold with model space `euclidean_space ℝ (fin (2 - 1))` but not with model space `euclidean_space ℝ (fin 1)`. -/ variables {E : Type*} [inner_product_space ℝ E] noncomputable theory open metric finite_dimensional function open_locale manifold local attribute [instance] fact_finite_dimensional_of_finrank_eq_succ section stereographic_projection variables (v : E) /-! ### Construction of the stereographic projection -/ /-- Stereographic projection, forward direction. This is a map from an inner product space `E` to the orthogonal complement of an element `v` of `E`. It is smooth away from the affine hyperplane through `v` parallel to the orthogonal complement. It restricts on the sphere to the stereographic projection. -/ def stereo_to_fun [complete_space E] (x : E) : (ℝ ∙ v)ᗮ := (2 / ((1:ℝ) - innerSL v x)) • orthogonal_projection (ℝ ∙ v)ᗮ x variables {v} @[simp] lemma stereo_to_fun_apply [complete_space E] (x : E) : stereo_to_fun v x = (2 / ((1:ℝ) - innerSL v x)) • orthogonal_projection (ℝ ∙ v)ᗮ x := rfl lemma cont_diff_on_stereo_to_fun [complete_space E] : cont_diff_on ℝ ⊤ (stereo_to_fun v) {x : E | innerSL v x ≠ (1:ℝ)} := begin refine cont_diff_on.smul _ (orthogonal_projection ((ℝ ∙ v)ᗮ)).cont_diff.cont_diff_on, refine cont_diff_const.cont_diff_on.div _ _, { exact (cont_diff_const.sub (innerSL v : E →L[ℝ] ℝ).cont_diff).cont_diff_on }, { intros x h h', exact h (sub_eq_zero.mp h').symm } end lemma continuous_on_stereo_to_fun [complete_space E] : continuous_on (stereo_to_fun v) {x : E | innerSL v x ≠ (1:ℝ)} := (@cont_diff_on_stereo_to_fun E _ v _).continuous_on variables (v) /-- Auxiliary function for the construction of the reverse direction of the stereographic projection. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to `E`; we will later prove that it takes values in the unit sphere. For most purposes, use `stereo_inv_fun`, not `stereo_inv_fun_aux`. -/ def stereo_inv_fun_aux (w : E) : E := (‖w‖ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (‖w‖ ^ 2 - 4) • v) variables {v} @[simp] lemma stereo_inv_fun_aux_apply (w : E) : stereo_inv_fun_aux v w = (‖w‖ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (‖w‖ ^ 2 - 4) • v) := rfl lemma stereo_inv_fun_aux_mem (hv : ‖v‖ = 1) {w : E} (hw : w ∈ (ℝ ∙ v)ᗮ) : stereo_inv_fun_aux v w ∈ (sphere (0:E) 1) := begin have h₁ : 0 ≤ ‖w‖ ^ 2 + 4 := by nlinarith, suffices : ‖(4:ℝ) • w + (‖w‖ ^ 2 - 4) • v‖ = ‖w‖ ^ 2 + 4, { have h₂ : ‖w‖ ^ 2 + 4 ≠ 0 := by nlinarith, simp only [mem_sphere_zero_iff_norm, norm_smul, real.norm_eq_abs, abs_inv, this, abs_of_nonneg h₁, stereo_inv_fun_aux_apply], field_simp }, suffices : ‖(4:ℝ) • w + (‖w‖ ^ 2 - 4) • v‖ ^ 2 = (‖w‖ ^ 2 + 4) ^ 2, { have h₃ : 0 ≤ ‖stereo_inv_fun_aux v w‖ := norm_nonneg _, simpa [h₁, h₃, -one_pow] using this }, simp [norm_add_sq_real, norm_smul, inner_smul_left, inner_smul_right, inner_left_of_mem_orthogonal_singleton _ hw, mul_pow, real.norm_eq_abs, hv], ring end lemma has_fderiv_at_stereo_inv_fun_aux (v : E) : has_fderiv_at (stereo_inv_fun_aux v) (continuous_linear_map.id ℝ E) 0 := begin have h₀ : has_fderiv_at (λ w : E, ‖w‖ ^ 2) (0 : E →L[ℝ] ℝ) 0, { convert (has_strict_fderiv_at_norm_sq _).has_fderiv_at, simp }, have h₁ : has_fderiv_at (λ w : E, (‖w‖ ^ 2 + 4)⁻¹) (0 : E →L[ℝ] ℝ) 0, { convert (has_fderiv_at_inv _).comp _ (h₀.add (has_fderiv_at_const 4 0)); simp }, have h₂ : has_fderiv_at (λ w, (4:ℝ) • w + (‖w‖ ^ 2 - 4) • v) ((4:ℝ) • continuous_linear_map.id ℝ E) 0, { convert ((has_fderiv_at_const (4:ℝ) 0).smul (has_fderiv_at_id 0)).add ((h₀.sub (has_fderiv_at_const (4:ℝ) 0)).smul (has_fderiv_at_const v 0)), ext w, simp, }, convert h₁.smul h₂, ext w, simp, end lemma has_fderiv_at_stereo_inv_fun_aux_comp_coe (v : E) : has_fderiv_at (stereo_inv_fun_aux v ∘ (coe : (ℝ ∙ v)ᗮ → E)) (ℝ ∙ v)ᗮ.subtypeL 0 := begin have : has_fderiv_at (stereo_inv_fun_aux v) (continuous_linear_map.id ℝ E) ((ℝ ∙ v)ᗮ.subtypeL 0) := has_fderiv_at_stereo_inv_fun_aux v, convert this.comp (0 : (ℝ ∙ v)ᗮ) (by apply continuous_linear_map.has_fderiv_at), end lemma cont_diff_stereo_inv_fun_aux : cont_diff ℝ ⊤ (stereo_inv_fun_aux v) := begin have h₀ : cont_diff ℝ ⊤ (λ w : E, ‖w‖ ^ 2) := cont_diff_norm_sq, have h₁ : cont_diff ℝ ⊤ (λ w : E, (‖w‖ ^ 2 + 4)⁻¹), { refine (h₀.add cont_diff_const).inv _, intros x, nlinarith }, have h₂ : cont_diff ℝ ⊤ (λ w, (4:ℝ) • w + (‖w‖ ^ 2 - 4) • v), { refine (cont_diff_const.smul cont_diff_id).add _, refine (h₀.sub cont_diff_const).smul cont_diff_const }, exact h₁.smul h₂ end /-- Stereographic projection, reverse direction. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to the unit sphere in `E`. -/ def stereo_inv_fun (hv : ‖v‖ = 1) (w : (ℝ ∙ v)ᗮ) : sphere (0:E) 1 := ⟨stereo_inv_fun_aux v (w:E), stereo_inv_fun_aux_mem hv w.2⟩ @[simp] lemma stereo_inv_fun_apply (hv : ‖v‖ = 1) (w : (ℝ ∙ v)ᗮ) : (stereo_inv_fun hv w : E) = (‖w‖ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (‖w‖ ^ 2 - 4) • v) := rfl lemma stereo_inv_fun_ne_north_pole (hv : ‖v‖ = 1) (w : (ℝ ∙ v)ᗮ) : stereo_inv_fun hv w ≠ (⟨v, by simp [hv]⟩ : sphere (0:E) 1) := begin refine subtype.ne_of_val_ne _, rw ← inner_lt_one_iff_real_of_norm_one _ hv, { have hw : ⟪v, w⟫_ℝ = 0 := inner_right_of_mem_orthogonal_singleton v w.2, have hw' : (‖(w:E)‖ ^ 2 + 4)⁻¹ * (‖(w:E)‖ ^ 2 - 4) < 1, { refine (inv_mul_lt_iff' _).mpr _, { nlinarith }, linarith }, simpa [real_inner_comm, inner_add_right, inner_smul_right, real_inner_self_eq_norm_mul_norm, hw, hv] using hw' }, { simpa using stereo_inv_fun_aux_mem hv w.2 } end lemma continuous_stereo_inv_fun (hv : ‖v‖ = 1) : continuous (stereo_inv_fun hv) := continuous_induced_rng.2 (cont_diff_stereo_inv_fun_aux.continuous.comp continuous_subtype_coe) variables [complete_space E] lemma stereo_left_inv (hv : ‖v‖ = 1) {x : sphere (0:E) 1} (hx : (x:E) ≠ v) : stereo_inv_fun hv (stereo_to_fun v x) = x := begin ext, simp only [stereo_to_fun_apply, stereo_inv_fun_apply, smul_add], -- name two frequently-occuring quantities and write down their basic properties set a : ℝ := innerSL v x, set y := orthogonal_projection (ℝ ∙ v)ᗮ x, have split : ↑x = a • v + ↑y, { convert eq_sum_orthogonal_projection_self_orthogonal_complement (ℝ ∙ v) x, exact (orthogonal_projection_unit_singleton ℝ hv x).symm }, have hvy : ⟪v, y⟫_ℝ = 0 := inner_right_of_mem_orthogonal_singleton v y.2, have pythag : 1 = a ^ 2 + ‖y‖ ^ 2, { have hvy' : ⟪a • v, y⟫_ℝ = 0 := by simp [inner_smul_left, hvy], convert norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero _ _ hvy' using 2, { simp [← split] }, { simp [norm_smul, hv, ← sq, sq_abs] }, { exact sq _ } }, -- two facts which will be helpful for clearing denominators in the main calculation have ha : 1 - a ≠ 0, { have : a < 1 := (inner_lt_one_iff_real_of_norm_one hv (by simp)).mpr hx.symm, linarith }, have : 2 ^ 2 * ‖y‖ ^ 2 + 4 * (1 - a) ^ 2 ≠ 0, { refine ne_of_gt _, have := norm_nonneg (y:E), have : 0 < (1 - a) ^ 2 := sq_pos_of_ne_zero (1 - a) ha, nlinarith }, -- the core of the problem is these two algebraic identities: have h₁ : (2 ^ 2 / (1 - a) ^ 2 * ‖y‖ ^ 2 + 4)⁻¹ * 4 * (2 / (1 - a)) = 1, { field_simp, simp only [submodule.coe_norm] at *, nlinarith }, have h₂ : (2 ^ 2 / (1 - a) ^ 2 * ‖y‖ ^ 2 + 4)⁻¹ * (2 ^ 2 / (1 - a) ^ 2 * ‖y‖ ^ 2 - 4) = a, { field_simp, transitivity (1 - a) ^ 2 * (a * (2 ^ 2 * ‖y‖ ^ 2 + 4 * (1 - a) ^ 2)), { congr, simp only [submodule.coe_norm] at *, nlinarith }, ring }, -- deduce the result convert congr_arg2 has_add.add (congr_arg (λ t, t • (y:E)) h₁) (congr_arg (λ t, t • v) h₂) using 1, { simp [inner_add_right, inner_smul_right, hvy, real_inner_self_eq_norm_mul_norm, hv, mul_smul, mul_pow, real.norm_eq_abs, sq_abs, norm_smul] }, { simp [split, add_comm] } end lemma stereo_right_inv (hv : ‖v‖ = 1) (w : (ℝ ∙ v)ᗮ) : stereo_to_fun v (stereo_inv_fun hv w) = w := begin have : 2 / (1 - (‖(w:E)‖ ^ 2 + 4)⁻¹ * (‖(w:E)‖ ^ 2 - 4)) * (‖(w:E)‖ ^ 2 + 4)⁻¹ * 4 = 1, { have : ‖(w:E)‖ ^ 2 + 4 ≠ 0 := by nlinarith, have : (4:ℝ) + 4 ≠ 0 := by nlinarith, field_simp, ring }, convert congr_arg (λ c, c • w) this, { have h₁ : orthogonal_projection (ℝ ∙ v)ᗮ v = 0 := orthogonal_projection_orthogonal_complement_singleton_eq_zero v, have h₂ : orthogonal_projection (ℝ ∙ v)ᗮ w = w := orthogonal_projection_mem_subspace_eq_self w, have h₃ : innerSL v w = (0:ℝ) := inner_right_of_mem_orthogonal_singleton v w.2, have h₄ : innerSL v v = (1:ℝ) := by simp [real_inner_self_eq_norm_mul_norm, hv], simp [h₁, h₂, h₃, h₄, continuous_linear_map.map_add, continuous_linear_map.map_smul, mul_smul] }, { simp } end /-- Stereographic projection from the unit sphere in `E`, centred at a unit vector `v` in `E`; this is the version as a local homeomorphism. -/ def stereographic (hv : ‖v‖ = 1) : local_homeomorph (sphere (0:E) 1) (ℝ ∙ v)ᗮ := { to_fun := (stereo_to_fun v) ∘ coe, inv_fun := stereo_inv_fun hv, source := {⟨v, by simp [hv]⟩}ᶜ, target := set.univ, map_source' := by simp, map_target' := λ w _, stereo_inv_fun_ne_north_pole hv w, left_inv' := λ _ hx, stereo_left_inv hv (λ h, hx (subtype.ext h)), right_inv' := λ w _, stereo_right_inv hv w, open_source := is_open_compl_singleton, open_target := is_open_univ, continuous_to_fun := continuous_on_stereo_to_fun.comp continuous_subtype_coe.continuous_on (λ w h, h ∘ subtype.ext ∘ eq.symm ∘ (inner_eq_norm_mul_iff_of_norm_one hv (by simp)).mp), continuous_inv_fun := (continuous_stereo_inv_fun hv).continuous_on } lemma stereographic_apply (hv : ‖v‖ = 1) (x : sphere (0 : E) 1) : stereographic hv x = (2 / ((1:ℝ) - inner v x)) • orthogonal_projection (ℝ ∙ v)ᗮ x := rfl @[simp] lemma stereographic_source (hv : ‖v‖ = 1) : (stereographic hv).source = {⟨v, by simp [hv]⟩}ᶜ := rfl @[simp] lemma stereographic_target (hv : ‖v‖ = 1) : (stereographic hv).target = set.univ := rfl @[simp] lemma stereographic_apply_neg (v : sphere (0:E) 1) : stereographic (norm_eq_of_mem_sphere v) (-v) = 0 := by simp [stereographic_apply, orthogonal_projection_orthogonal_complement_singleton_eq_zero] @[simp] lemma stereographic_neg_apply (v : sphere (0:E) 1) : stereographic (norm_eq_of_mem_sphere (-v)) v = 0 := begin convert stereographic_apply_neg (-v), ext1, simp, end end stereographic_projection section charted_space /-! ### Charted space structure on the sphere In this section we construct a charted space structure on the unit sphere in a finite-dimensional real inner product space `E`; that is, we show that it is locally homeomorphic to the Euclidean space of dimension one less than `E`. The restriction to finite dimension is for convenience. The most natural `charted_space` structure for the sphere uses the stereographic projection from the antipodes of a point as the canonical chart at this point. However, the codomain of the stereographic projection constructed in the previous section is `(ℝ ∙ v)ᗮ`, the orthogonal complement of the vector `v` in `E` which is the "north pole" of the projection, so a priori these charts all have different codomains. So it is necessary to prove that these codomains are all continuously linearly equivalent to a fixed normed space. This could be proved in general by a simple case of Gram-Schmidt orthogonalization, but in the finite-dimensional case it follows more easily by dimension-counting. -/ /-- Variant of the stereographic projection, for the sphere in an `n + 1`-dimensional inner product space `E`. This version has codomain the Euclidean space of dimension `n`, and is obtained by composing the original sterographic projection (`stereographic`) with an arbitrary linear isometry from `(ℝ ∙ v)ᗮ` to the Euclidean space. -/ def stereographic' (n : ℕ) [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : local_homeomorph (sphere (0:E) 1) (euclidean_space ℝ (fin n)) := (stereographic (norm_eq_of_mem_sphere v)) ≫ₕ (orthonormal_basis.from_orthogonal_span_singleton n (ne_zero_of_mem_unit_sphere v)).repr.to_homeomorph.to_local_homeomorph @[simp] lemma stereographic'_source {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : (stereographic' n v).source = {v}ᶜ := by simp [stereographic'] @[simp] lemma stereographic'_target {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : (stereographic' n v).target = set.univ := by simp [stereographic'] /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is a charted space modelled on the Euclidean space of dimension `n`. -/ instance {n : ℕ} [fact (finrank ℝ E = n + 1)] : charted_space (euclidean_space ℝ (fin n)) (sphere (0:E) 1) := { atlas := {f | ∃ v : (sphere (0:E) 1), f = stereographic' n v}, chart_at := λ v, stereographic' n (-v), mem_chart_source := λ v, by simpa using ne_neg_of_mem_unit_sphere ℝ v, chart_mem_atlas := λ v, ⟨-v, rfl⟩ } end charted_space section smooth_manifold lemma sphere_ext_iff (u v : sphere (0:E) 1) : u = v ↔ ⟪(u:E), v⟫_ℝ = 1 := by simp [subtype.ext_iff, inner_eq_norm_mul_iff_of_norm_one] lemma stereographic'_symm_apply {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) (x : euclidean_space ℝ (fin n)) : ((stereographic' n v).symm x : E) = let U : (ℝ ∙ (v:E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := (orthonormal_basis.from_orthogonal_span_singleton n (ne_zero_of_mem_unit_sphere v)).repr in ((‖(U.symm x : E)‖ ^ 2 + 4)⁻¹ • (4 : ℝ) • (U.symm x : E) + (‖(U.symm x : E)‖ ^ 2 + 4)⁻¹ • (‖(U.symm x : E)‖ ^ 2 - 4) • v) := by simp [real_inner_comm, stereographic, stereographic', ← submodule.coe_norm] /-! ### Smooth manifold structure on the sphere -/ /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is a smooth manifold, modelled on the Euclidean space of dimension `n`. -/ instance {n : ℕ} [fact (finrank ℝ E = n + 1)] : smooth_manifold_with_corners (𝓡 n) (sphere (0:E) 1) := smooth_manifold_with_corners_of_cont_diff_on (𝓡 n) (sphere (0:E) 1) begin rintros _ _ ⟨v, rfl⟩ ⟨v', rfl⟩, let U := -- Removed type ascription, and this helped for some reason with timeout issues? (orthonormal_basis.from_orthogonal_span_singleton n (ne_zero_of_mem_unit_sphere v)).repr, let U' :=-- Removed type ascription, and this helped for some reason with timeout issues? (orthonormal_basis.from_orthogonal_span_singleton n (ne_zero_of_mem_unit_sphere v')).repr, have hUv : stereographic' n v = (stereographic (norm_eq_of_mem_sphere v)) ≫ₕ U.to_homeomorph.to_local_homeomorph := rfl, have hU'v' : stereographic' n v' = (stereographic (norm_eq_of_mem_sphere v')).trans U'.to_homeomorph.to_local_homeomorph := rfl, have H₁ := U'.cont_diff.comp_cont_diff_on cont_diff_on_stereo_to_fun, have H₂ := (cont_diff_stereo_inv_fun_aux.comp (ℝ ∙ (v:E))ᗮ.subtypeL.cont_diff).comp U.symm.cont_diff, convert H₁.comp' (H₂.cont_diff_on : cont_diff_on ℝ ⊤ _ set.univ) using 1, ext, simp [sphere_ext_iff, stereographic'_symm_apply, real_inner_comm] end /-- The inclusion map (i.e., `coe`) from the sphere in `E` to `E` is smooth. -/ lemma cont_mdiff_coe_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] : cont_mdiff (𝓡 n) 𝓘(ℝ, E) ∞ (coe : (sphere (0:E) 1) → E) := begin rw cont_mdiff_iff, split, { exact continuous_subtype_coe }, { intros v _, let U := -- Again, removing type ascription... (orthonormal_basis.from_orthogonal_span_singleton n (ne_zero_of_mem_unit_sphere (-v))).repr, exact ((cont_diff_stereo_inv_fun_aux.comp (ℝ ∙ ((-v):E))ᗮ.subtypeL.cont_diff).comp U.symm.cont_diff).cont_diff_on } end variables {F : Type*} [normed_add_comm_group F] [normed_space ℝ F] variables {H : Type*} [topological_space H] {I : model_with_corners ℝ F H} variables {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] /-- If a `cont_mdiff` function `f : M → E`, where `M` is some manifold, takes values in the sphere, then it restricts to a `cont_mdiff` function from `M` to the sphere. -/ lemma cont_mdiff.cod_restrict_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] {m : ℕ∞} {f : M → E} (hf : cont_mdiff I 𝓘(ℝ, E) m f) (hf' : ∀ x, f x ∈ sphere (0:E) 1) : cont_mdiff I (𝓡 n) m (set.cod_restrict _ _ hf' : M → (sphere (0:E) 1)) := begin rw cont_mdiff_iff_target, refine ⟨continuous_induced_rng.2 hf.continuous, _⟩, intros v, let U := -- Again, removing type ascription... Weird that this helps! (orthonormal_basis.from_orthogonal_span_singleton n (ne_zero_of_mem_unit_sphere (-v))).repr, have h : cont_diff_on ℝ ⊤ _ set.univ := U.cont_diff.cont_diff_on, have H₁ := (h.comp' cont_diff_on_stereo_to_fun).cont_mdiff_on, have H₂ : cont_mdiff_on _ _ _ _ set.univ := hf.cont_mdiff_on, convert (H₁.of_le le_top).comp' H₂ using 1, ext x, have hfxv : f x = -↑v ↔ ⟪f x, -↑v⟫_ℝ = 1, { have hfx : ‖f x‖ = 1 := by simpa using hf' x, rw inner_eq_norm_mul_iff_of_norm_one hfx, exact norm_eq_of_mem_sphere (-v) }, dsimp [chart_at], simp [not_iff_not, subtype.ext_iff, hfxv, real_inner_comm] end /-- The antipodal map is smooth. -/ lemma cont_mdiff_neg_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] : cont_mdiff (𝓡 n) (𝓡 n) ∞ (λ x : sphere (0:E) 1, -x) := begin -- this doesn't elaborate well in term mode apply cont_mdiff.cod_restrict_sphere, apply cont_diff_neg.cont_mdiff.comp _, exact cont_mdiff_coe_sphere, end /-- Consider the differential of the inclusion of the sphere in `E` at the point `v` as a continuous linear map from `tangent_space (𝓡 n) v` to `E`. The range of this map is the orthogonal complement of `v` in `E`. Note that there is an abuse here of the defeq between `E` and the tangent space to `E` at `(v:E`). In general this defeq is not canonical, but in this case (the tangent space of a vector space) it is canonical. -/ lemma range_mfderiv_coe_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : (mfderiv (𝓡 n) 𝓘(ℝ, E) (coe : sphere (0:E) 1 → E) v : tangent_space (𝓡 n) v →L[ℝ] E).range = (ℝ ∙ (v:E))ᗮ := begin rw ((cont_mdiff_coe_sphere v).mdifferentiable_at le_top).mfderiv, simp only [chart_at, stereographic', stereographic_neg_apply, fderiv_within_univ, linear_isometry_equiv.to_homeomorph_symm, linear_isometry_equiv.coe_to_homeomorph, linear_isometry_equiv.map_zero] with mfld_simps, let U := (orthonormal_basis.from_orthogonal_span_singleton n (ne_zero_of_mem_unit_sphere (-v))).repr, change (fderiv ℝ ((stereo_inv_fun_aux (-v : E) ∘ coe) ∘ U.symm) 0).range = (ℝ ∙ (v:E))ᗮ, have : has_fderiv_at (stereo_inv_fun_aux (-v : E) ∘ (coe : (ℝ ∙ (↑(-v):E))ᗮ → E)) (ℝ ∙ (↑(-v):E))ᗮ.subtypeL (U.symm 0), { convert has_fderiv_at_stereo_inv_fun_aux_comp_coe (-v:E), simp }, rw (this.comp 0 U.symm.to_continuous_linear_equiv.has_fderiv_at).fderiv, convert (U.symm : euclidean_space ℝ (fin n) ≃ₗᵢ[ℝ] (ℝ ∙ (↑(-v):E))ᗮ).range_comp (ℝ ∙ (↑(-v):E))ᗮ.subtype using 1, simp only [submodule.range_subtype, coe_neg_sphere], congr' 1, -- we must show `submodule.span ℝ {v} = submodule.span ℝ {-v}` apply submodule.span_eq_span, { simp only [set.singleton_subset_iff, set_like.mem_coe], rw ← submodule.neg_mem_iff, exact submodule.mem_span_singleton_self (-v) }, { simp only [set.singleton_subset_iff, set_like.mem_coe], rw submodule.neg_mem_iff, exact submodule.mem_span_singleton_self v }, end /-- Consider the differential of the inclusion of the sphere in `E` at the point `v` as a continuous linear map from `tangent_space (𝓡 n) v` to `E`. This map is injective. -/ lemma mfderiv_coe_sphere_injective {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : injective (mfderiv (𝓡 n) 𝓘(ℝ, E) (coe : sphere (0:E) 1 → E) v) := begin rw ((cont_mdiff_coe_sphere v).mdifferentiable_at le_top).mfderiv, simp only [chart_at, stereographic', stereographic_neg_apply, fderiv_within_univ, linear_isometry_equiv.to_homeomorph_symm, linear_isometry_equiv.coe_to_homeomorph, linear_isometry_equiv.map_zero] with mfld_simps, let U := (orthonormal_basis.from_orthogonal_span_singleton n (ne_zero_of_mem_unit_sphere (-v))).repr, change injective (fderiv ℝ ((stereo_inv_fun_aux (-v : E) ∘ coe) ∘ U.symm) 0), have : has_fderiv_at (stereo_inv_fun_aux (-v : E) ∘ (coe : (ℝ ∙ (↑(-v):E))ᗮ → E)) (ℝ ∙ (↑(-v):E))ᗮ.subtypeL (U.symm 0), { convert has_fderiv_at_stereo_inv_fun_aux_comp_coe (-v:E), simp }, rw (this.comp 0 U.symm.to_continuous_linear_equiv.has_fderiv_at).fderiv, simpa using subtype.coe_injective, end end smooth_manifold section circle open complex local attribute [instance] finrank_real_complex_fact /-- The unit circle in `ℂ` is a charted space modelled on `euclidean_space ℝ (fin 1)`. This follows by definition from the corresponding result for `metric.sphere`. -/ instance : charted_space (euclidean_space ℝ (fin 1)) circle := metric.sphere.charted_space instance : smooth_manifold_with_corners (𝓡 1) circle := metric.sphere.smooth_manifold_with_corners /-- The unit circle in `ℂ` is a Lie group. -/ instance : lie_group (𝓡 1) circle := { smooth_mul := begin apply cont_mdiff.cod_restrict_sphere, let c : circle → ℂ := coe, have h₂ : cont_mdiff (𝓘(ℝ, ℂ).prod 𝓘(ℝ, ℂ)) 𝓘(ℝ, ℂ) ∞ (λ (z : ℂ × ℂ), z.fst * z.snd), { rw cont_mdiff_iff, exact ⟨continuous_mul, λ x y, cont_diff_mul.cont_diff_on⟩ }, suffices h₁ : cont_mdiff _ _ _ (prod.map c c), { apply h₂.comp h₁ }, -- this elaborates much faster with `apply` apply cont_mdiff.prod_map; exact cont_mdiff_coe_sphere, end, smooth_inv := begin apply cont_mdiff.cod_restrict_sphere, simp only [← coe_inv_circle, coe_inv_circle_eq_conj], exact complex.conj_cle.cont_diff.cont_mdiff.comp cont_mdiff_coe_sphere end } /-- The map `λ t, exp (t * I)` from `ℝ` to the unit circle in `ℂ` is smooth. -/ lemma cont_mdiff_exp_map_circle : cont_mdiff 𝓘(ℝ, ℝ) (𝓡 1) ∞ exp_map_circle := ((cont_diff_exp.comp (cont_diff_id.smul cont_diff_const)).cont_mdiff).cod_restrict_sphere _ end circle
cc38aaf3cf0275c815a365348abd099614a7cdb7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/continuous_function/ideals.lean
0ca31fed642c9e41c2d0a42b19216f9054678b5f
[ "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
21,632
lean
/- Copyright (c) 2022 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import topology.algebra.algebra import topology.continuous_function.compact import topology.urysohns_lemma import data.complex.is_R_or_C import analysis.normed_space.units import topology.algebra.module.character_space /-! # Ideals of continuous functions For a topological ring `R` and a topological space `X` there is a Galois connection between `ideal C(X, R)` and `set X` given by sending each `I : ideal C(X, R)` to `{x : X | ∀ f ∈ I, f x = 0}ᶜ` and mapping `s : set X` to the ideal with carrier `{f : C(X, R) | ∀ x ∈ sᶜ, f x = 0}`, and we call these maps `continuous_map.set_of_ideal` and `continuous_map.ideal_of_set`. As long as `R` is Hausdorff, `continuous_map.set_of_ideal I` is open, and if, in addition, `X` is locally compact, then `continuous_map.set_of_ideal s` is closed. When `R = 𝕜` with `is_R_or_C 𝕜` and `X` is compact Hausdorff, then this Galois connection can be improved to a true Galois correspondence (i.e., order isomorphism) between the type `opens X` and the subtype of closed ideals of `C(X, 𝕜)`. Because we do not have a bundled type of closed ideals, we simply register this as a Galois insertion between `ideal C(X, 𝕜)` and `opens X`, which is `continuous_map.ideal_opens_gi`. Consequently, the maximal ideals of `C(X, 𝕜)` are precisely those ideals corresponding to (complements of) singletons in `X`. In addition, when `X` is locally compact and `𝕜` is a nontrivial topological integral domain, then there is a natural continuous map from `X` to `character_space 𝕜 C(X, 𝕜)` given by point evaluation, which is herein called `weak_dual.character_space.continuous_map_eval`. Again, when `X` is compact Hausdorff and `is_R_or_C 𝕜`, more can be obtained. In particular, in that context this map is bijective, and since the domain is compact and the codomain is Hausdorff, it is a homeomorphism, herein called `weak_dual.character_space.homeo_eval`. ## Main definitions * `continuous_map.ideal_of_set`: ideal of functions which vanish on the complement of a set. * `continuous_map.set_of_ideal`: complement of the set on which all functions in the ideal vanish. * `continuous_map.opens_of_ideal`: `continuous_map.set_of_ideal` as a term of `opens X`. * `continuous_map.ideal_opens_gi`: The Galois insertion `continuous_map.opens_of_ideal` and `λ s, continuous_map.ideal_of_set ↑s`. * `weak_dual.character_space.continuous_map_eval`: the natural continuous map from a locally compact topological space `X` to the `character_space 𝕜 C(X, 𝕜)` which sends `x : X` to point evaluation at `x`, with modest hypothesis on `𝕜`. * `weak_dual.character_space.homeo_eval`: this is `weak_dual.character_space.continuous_map_eval` upgraded to a homeomorphism when `X` is compact Hausdorff and `is_R_or_C 𝕜`. ## Main statements * `continuous_map.ideal_of_set_of_ideal_eq_closure`: when `X` is compact Hausdorff and `is_R_or_C 𝕜`, `ideal_of_set 𝕜 (set_of_ideal I) = I.closure` for any ideal `I : ideal C(X, 𝕜)`. * `continuous_map.set_of_ideal_of_set_eq_interior`: when `X` is compact Hausdorff and `is_R_or_C 𝕜`, `set_of_ideal (ideal_of_set 𝕜 s) = interior s` for any `s : set X`. * `continuous_map.ideal_is_maximal_iff`: when `X` is compact Hausdorff and `is_R_or_C 𝕜`, a closed ideal of `C(X, 𝕜)` is maximal if and only if it is `ideal_of_set 𝕜 {x}ᶜ` for some `x : X`. ## Implementation details Because there does not currently exist a bundled type of closed ideals, we don't provide the actual order isomorphism described above, and instead we only consider the Galois insertion `continuous_map.ideal_opens_gi`. ## Tags ideal, continuous function, compact, Hausdorff -/ open_locale nnreal namespace continuous_map open topological_space section topological_ring variables {X R : Type*} [topological_space X] [ring R] [topological_space R] [topological_ring R] variable (R) /-- Given a topological ring `R` and `s : set X`, construct the ideal in `C(X, R)` of functions which vanish on the complement of `s`. -/ def ideal_of_set (s : set X) : ideal C(X, R) := { carrier := {f : C(X, R) | ∀ x ∈ sᶜ, f x = 0}, add_mem' := λ f g hf hg x hx, by simp only [hf x hx, hg x hx, coe_add, pi.add_apply, add_zero], zero_mem' := λ _ _, rfl, smul_mem' := λ c f hf x hx, mul_zero (c x) ▸ congr_arg (λ y, c x * y) (hf x hx), } lemma ideal_of_set_closed [locally_compact_space X] [t2_space R] (s : set X) : is_closed (ideal_of_set R s : set C(X, R) ) := begin simp only [ideal_of_set, submodule.coe_set_mk, set.set_of_forall], exact is_closed_Inter (λ x, is_closed_Inter $ λ hx, is_closed_eq (continuous_eval_const' x) continuous_const), end variable {R} lemma mem_ideal_of_set {s : set X} {f : C(X, R)} : f ∈ ideal_of_set R s ↔ ∀ ⦃x : X⦄, x ∈ sᶜ → f x = 0 := iff.rfl lemma not_mem_ideal_of_set {s : set X} {f : C(X, R)} : f ∉ ideal_of_set R s ↔ ∃ x ∈ sᶜ, f x ≠ 0 := by { simp_rw [mem_ideal_of_set, exists_prop], push_neg } /-- Given an ideal `I` of `C(X, R)`, construct the set of points for which every function in the ideal vanishes on the complement. -/ def set_of_ideal (I : ideal C(X, R)) : set X := {x : X | ∀ f ∈ I, (f : C(X, R)) x = 0}ᶜ lemma not_mem_set_of_ideal {I : ideal C(X, R)} {x : X} : x ∉ set_of_ideal I ↔ ∀ ⦃f : C(X, R)⦄, f ∈ I → f x = 0 := by rw [←set.mem_compl_iff, set_of_ideal, compl_compl, set.mem_set_of] lemma mem_set_of_ideal {I : ideal C(X, R)} {x : X} : x ∈ set_of_ideal I ↔ ∃ f ∈ I, (f : C(X, R)) x ≠ 0 := by { simp_rw [set_of_ideal, set.mem_compl_iff, set.mem_set_of, exists_prop], push_neg } lemma set_of_ideal_open [t2_space R] (I : ideal C(X, R)) : is_open (set_of_ideal I) := begin simp only [set_of_ideal, set.set_of_forall, is_open_compl_iff], exact is_closed_Inter (λ f, is_closed_Inter $ λ hf, is_closed_eq (map_continuous f) continuous_const) end /-- The open set `set_of_ideal I` realized as a term of `opens X`. -/ @[simps] def opens_of_ideal [t2_space R] (I : ideal C(X, R)) : opens X := ⟨set_of_ideal I, set_of_ideal_open I⟩ @[simp] lemma set_of_top_eq_univ [nontrivial R] : (set_of_ideal (⊤ : ideal C(X, R))) = set.univ := set.univ_subset_iff.mp $ λ x hx, mem_set_of_ideal.mpr ⟨1, submodule.mem_top, one_ne_zero⟩ @[simp] lemma ideal_of_empty_eq_bot : (ideal_of_set R (∅ : set X)) = ⊥ := ideal.ext (λ f, by simpa only [mem_ideal_of_set, set.compl_empty, set.mem_univ, forall_true_left, ideal.mem_bot, fun_like.ext_iff] using iff.rfl) @[simp] lemma mem_ideal_of_set_compl_singleton (x : X) (f : C(X, R)) : f ∈ ideal_of_set R ({x}ᶜ : set X) ↔ f x = 0 := by simp only [mem_ideal_of_set, compl_compl, set.mem_singleton_iff, forall_eq] variables (X R) lemma ideal_gc : galois_connection (set_of_ideal : ideal C(X, R) → set X) (ideal_of_set R) := begin refine λ I s, ⟨λ h f hf, _, λ h x hx, _⟩, { by_contra h', rcases not_mem_ideal_of_set.mp h' with ⟨x, hx, hfx⟩, exact hfx (not_mem_set_of_ideal.mp (mt (@h x) hx) hf) }, { obtain ⟨f, hf, hfx⟩ := mem_set_of_ideal.mp hx, by_contra hx', exact not_mem_ideal_of_set.mpr ⟨x, hx', hfx⟩ (h hf) }, end end topological_ring section is_R_or_C open is_R_or_C variables {X 𝕜 : Type*} [is_R_or_C 𝕜] [topological_space X] /-- An auxiliary lemma used in the proof of `ideal_of_set_of_ideal_eq_closure` which may be useful on its own. -/ lemma exists_mul_le_one_eq_on_ge (f : C(X, ℝ≥0)) {c : ℝ≥0} (hc : 0 < c) : ∃ g : C(X, ℝ≥0), (∀ x : X, (g * f) x ≤ 1) ∧ {x : X | c ≤ f x}.eq_on (g * f) 1 := ⟨{ to_fun := (f ⊔ (const X c))⁻¹, continuous_to_fun := ((map_continuous f).sup $ map_continuous _).inv₀ (λ _, (hc.trans_le le_sup_right).ne')}, λ x, (inv_mul_le_iff (hc.trans_le le_sup_right)).mpr ((mul_one (f x ⊔ c)).symm ▸ le_sup_left), λ x hx, by simpa only [coe_const, coe_mk, pi.mul_apply, pi.inv_apply, pi.sup_apply, function.const_apply, pi.one_apply, sup_eq_left.mpr (set.mem_set_of.mp hx)] using inv_mul_cancel (hc.trans_le hx).ne'⟩ variables [compact_space X] [t2_space X] @[simp] lemma ideal_of_set_of_ideal_eq_closure (I : ideal C(X, 𝕜)) : ideal_of_set 𝕜 (set_of_ideal I) = I.closure := begin /- Since `ideal_of_set 𝕜 (set_of_ideal I)` is closed and contains `I`, it contains `I.closure`. For the reverse inclusion, given `f ∈ ideal_of_set 𝕜 (set_of_ideal I)` and `(ε : ℝ≥0) > 0` it suffices to show that `f` is within `ε` of `I`.-/ refine le_antisymm (λ f hf, metric.mem_closure_iff.mpr (λ ε hε, _)) ((ideal_of_set_closed 𝕜 $ set_of_ideal I).closure_subset_iff.mpr $ λ f hf x hx, not_mem_set_of_ideal.mp hx hf), lift ε to ℝ≥0 using hε.lt.le, replace hε := (show (0 : ℝ≥0) < ε, from hε), simp_rw dist_nndist, norm_cast, -- Let `t := {x : X | ε / 2 ≤ ‖f x‖₊}}` which is closed and disjoint from `set_of_ideal I`. set t := {x : X | ε / 2 ≤ ‖f x‖₊}, have ht : is_closed t := is_closed_le continuous_const (map_continuous f).nnnorm, have htI : disjoint t (set_of_ideal I)ᶜ, { refine set.subset_compl_iff_disjoint_left.mp (λ x hx, _), simpa only [t, set.mem_set_of, set.mem_compl_iff, not_le] using (nnnorm_eq_zero.mpr (mem_ideal_of_set.mp hf hx)).trans_lt (half_pos hε), }, /- It suffices to produce `g : C(X, ℝ≥0)` which takes values in `[0,1]` and is constantly `1` on `t` such that when composed with the natural embedding of `ℝ≥0` into `𝕜` lies in the ideal `I`. Indeed, then `‖f - f * ↑g‖ ≤ ‖f * (1 - ↑g)‖ ≤ ⨆ ‖f * (1 - ↑g) x‖`. When `x ∉ t`, `‖f x‖ < ε / 2` and `‖(1 - ↑g) x‖ ≤ 1`, and when `x ∈ t`, `(1 - ↑g) x = 0`, and clearly `f * ↑g ∈ I`. -/ suffices : ∃ g : C(X, ℝ≥0), (algebra_map_clm ℝ≥0 𝕜 : C(ℝ≥0, 𝕜)).comp g ∈ I ∧ (∀ x, g x ≤ 1) ∧ t.eq_on g 1, { obtain ⟨g, hgI, hg, hgt⟩ := this, refine ⟨f * (algebra_map_clm ℝ≥0 𝕜 : C(ℝ≥0, 𝕜)).comp g, I.mul_mem_left f hgI, _⟩, rw nndist_eq_nnnorm, refine (nnnorm_lt_iff _ hε).2 (λ x, _), simp only [coe_sub, coe_mul, pi.sub_apply, pi.mul_apply], by_cases hx : x ∈ t, { simpa only [hgt hx, comp_apply, pi.one_apply, continuous_map.coe_coe, algebra_map_clm_apply, map_one, mul_one, sub_self, nnnorm_zero] using hε, }, { refine lt_of_le_of_lt _ (half_lt_self hε), have := calc ‖((1 - (algebra_map_clm ℝ≥0 𝕜 : C(ℝ≥0, 𝕜)).comp g) x : 𝕜)‖₊ = ‖1 - algebra_map ℝ≥0 𝕜 (g x)‖₊ : by simp only [coe_sub, coe_one, coe_comp, continuous_map.coe_coe, pi.sub_apply, pi.one_apply, function.comp_app, algebra_map_clm_apply] ... = ‖algebra_map ℝ≥0 𝕜 (1 - g x)‖₊ : by simp only [algebra.algebra_map_eq_smul_one, nnreal.smul_def, nnreal.coe_sub (hg x), sub_smul, nonneg.coe_one, one_smul] ... ≤ 1 : (nnnorm_algebra_map_nnreal 𝕜 (1 - g x)).trans_le tsub_le_self, calc ‖f x - f x * (algebra_map_clm ℝ≥0 𝕜 : C(ℝ≥0, 𝕜)).comp g x‖₊ = ‖f x * (1 - (algebra_map_clm ℝ≥0 𝕜 : C(ℝ≥0, 𝕜)).comp g) x‖₊ : by simp only [mul_sub, coe_sub, coe_one, pi.sub_apply, pi.one_apply, mul_one] ... ≤ (ε / 2) * ‖(1 - (algebra_map_clm ℝ≥0 𝕜 : C(ℝ≥0, 𝕜)).comp g) x‖₊ : (nnnorm_mul_le _ _).trans (mul_le_mul_right' (not_le.mp $ show ¬ ε / 2 ≤ ‖f x‖₊, from hx).le _) ... ≤ ε / 2 : by simpa only [mul_one] using mul_le_mul_left' this _, } }, /- There is some `g' : C(X, ℝ≥0)` which is strictly positive on `t` such that the composition `↑g` with the natural embedding of `ℝ≥0` into `𝕜` lies in `I`. This follows from compactness of `t` and that we can do it in any neighborhood of a point `x ∈ t`. Indeed, since `x ∈ t`, then `fₓ x ≠ 0` for some `fₓ ∈ I` and so `λ y, ‖(star fₓ * fₓ) y‖₊` is strictly posiive in a neighborhood of `y`. Moreover, `(‖(star fₓ * fₓ) y‖₊ : 𝕜) = (star fₓ * fₓ) y`, so composition of this map with the natural embedding is just `star fₓ * fₓ ∈ I`. -/ have : ∃ g' : C(X, ℝ≥0), (algebra_map_clm ℝ≥0 𝕜 : C(ℝ≥0, 𝕜)).comp g' ∈ I ∧ (∀ x ∈ t, 0 < g' x), { refine @is_compact.induction_on _ _ _ ht.is_compact (λ s, ∃ g' : C(X, ℝ≥0), (algebra_map_clm ℝ≥0 𝕜 : C(ℝ≥0, 𝕜)).comp g' ∈ I ∧ (∀ x ∈ s, 0 < g' x)) _ _ _ _, { refine ⟨0, _, λ x hx, false.elim hx⟩, convert I.zero_mem, ext, simp only [coe_zero, pi.zero_apply, continuous_map.coe_coe, continuous_map.coe_comp, map_zero, pi.comp_zero] }, { rintro s₁ s₂ hs ⟨g, hI, hgt⟩, exact ⟨g, hI, λ x hx, hgt x (hs hx)⟩, }, { rintro s₁ s₂ ⟨g₁, hI₁, hgt₁⟩ ⟨g₂, hI₂, hgt₂⟩, refine ⟨g₁ + g₂, _, λ x hx, _⟩, { convert I.add_mem hI₁ hI₂, ext y, simp only [coe_add, pi.add_apply, map_add, coe_comp, function.comp_app, continuous_map.coe_coe]}, { rcases hx with (hx | hx), simpa only [zero_add] using add_lt_add_of_lt_of_le (hgt₁ x hx) zero_le', simpa only [zero_add] using add_lt_add_of_le_of_lt zero_le' (hgt₂ x hx), } }, { intros x hx, replace hx := htI.subset_compl_right hx, rw [compl_compl, mem_set_of_ideal] at hx, obtain ⟨g, hI, hgx⟩ := hx, have := (map_continuous g).continuous_at.eventually_ne hgx, refine ⟨{y : X | g y ≠ 0} ∩ t, mem_nhds_within_iff_exists_mem_nhds_inter.mpr ⟨_, this, set.subset.rfl⟩, ⟨⟨λ x, ‖g x‖₊ ^ 2, (map_continuous g).nnnorm.pow 2⟩, _, λ x hx, pow_pos (norm_pos_iff.mpr hx.1) 2⟩⟩, convert I.mul_mem_left (star g) hI, ext, simp only [comp_apply, coe_mk, algebra_map_clm_coe, map_pow, coe_mul, coe_star, pi.mul_apply, pi.star_apply, star_def, continuous_map.coe_coe], simpa only [norm_sq_eq_def', conj_mul_eq_norm_sq_left, of_real_pow], }, }, /- Get the function `g'` which is guaranteed to exist above. By the extreme value theorem and compactness of `t`, there is some `0 < c` such that `c ≤ g' x` for all `x ∈ t`. Then by `main_lemma_aux` there is some `g` for which `g * g'` is the desired function. -/ obtain ⟨g', hI', hgt'⟩ := this, obtain (⟨c, hc, hgc'⟩ : ∃ c (hc : 0 < c), ∀ y : X, y ∈ t → c ≤ g' y) := t.eq_empty_or_nonempty.elim (λ ht', ⟨1, zero_lt_one, λ y hy, false.elim (by rwa ht' at hy)⟩) (λ ht', let ⟨x, hx, hx'⟩ := ht.is_compact.exists_forall_le ht' (map_continuous g').continuous_on in ⟨g' x, hgt' x hx, hx'⟩), obtain ⟨g, hg, hgc⟩ := exists_mul_le_one_eq_on_ge g' hc, refine ⟨g * g', _, hg, hgc.mono hgc'⟩, convert I.mul_mem_left ((algebra_map_clm ℝ≥0 𝕜 : C(ℝ≥0, 𝕜)).comp g) hI', ext, simp only [algebra_map_clm_coe, continuous_map.coe_coe, comp_apply, coe_mul, pi.mul_apply, map_mul], end lemma ideal_of_set_of_ideal_is_closed {I : ideal C(X, 𝕜)} (hI : is_closed (I : set C(X, 𝕜))) : ideal_of_set 𝕜 (set_of_ideal I) = I := (ideal_of_set_of_ideal_eq_closure I).trans (ideal.ext $ set.ext_iff.mp hI.closure_eq) variable (𝕜) @[simp] lemma set_of_ideal_of_set_eq_interior (s : set X) : set_of_ideal (ideal_of_set 𝕜 s) = interior s:= begin refine set.subset.antisymm ((set_of_ideal_open (ideal_of_set 𝕜 s)).subset_interior_iff.mpr (λ x hx, let ⟨f, hf, hfx⟩ := mem_set_of_ideal.mp hx in set.not_mem_compl_iff.mp (mt (@hf x) hfx))) (λ x hx, _), /- If `x ∉ closure sᶜ`, we must produce `f : C(X, 𝕜)` which is zero on `sᶜ` and `f x ≠ 0`. -/ rw [←compl_compl (interior s), ←closure_compl] at hx, simp_rw [mem_set_of_ideal, mem_ideal_of_set], haveI : normal_space X := normal_of_compact_t2, /- Apply Urysohn's lemma to get `g : C(X, ℝ)` which is zero on `sᶜ` and `g x ≠ 0`, then compose with the natural embedding `ℝ ↪ 𝕜` to produce the desired `f`. -/ obtain ⟨g, hgs, (hgx : set.eq_on g 1 {x}), -⟩ := exists_continuous_zero_one_of_closed is_closed_closure is_closed_singleton (set.disjoint_singleton_right.mpr hx), exact ⟨⟨λ x, g x, continuous_of_real.comp (map_continuous g)⟩, by simpa only [coe_mk, of_real_eq_zero] using λ x hx, hgs (subset_closure hx), by simpa only [coe_mk, hgx (set.mem_singleton x), pi.one_apply, is_R_or_C.of_real_one] using one_ne_zero⟩, end lemma set_of_ideal_of_set_of_is_open {s : set X} (hs : is_open s) : set_of_ideal (ideal_of_set 𝕜 s) = s := (set_of_ideal_of_set_eq_interior 𝕜 s).trans hs.interior_eq variable (X) /-- The Galois insertion `continuous_map.opens_of_ideal : ideal C(X, 𝕜) → opens X` and `λ s, continuous_map.ideal_of_set ↑s`. -/ @[simps] def ideal_opens_gi : galois_insertion (opens_of_ideal : ideal C(X, 𝕜) → opens X) (λ s, ideal_of_set 𝕜 s) := { choice := λ I hI, opens_of_ideal I.closure, gc := λ I s, ideal_gc X 𝕜 I s, le_l_u := λ s, (set_of_ideal_of_set_of_is_open 𝕜 s.prop).ge, choice_eq := λ I hI, congr_arg _ $ ideal.ext (set.ext_iff.mp (is_closed_of_closure_subset $ (ideal_of_set_of_ideal_eq_closure I ▸ hI : I.closure ≤ I)).closure_eq) } variables {X} lemma ideal_of_set_is_maximal_iff (s : opens X) : (ideal_of_set 𝕜 (s : set X)).is_maximal ↔ is_coatom s := begin rw ideal.is_maximal_def, refine (ideal_opens_gi X 𝕜).is_coatom_iff (λ I hI, _) s, rw ←ideal.is_maximal_def at hI, resetI, exact ideal_of_set_of_ideal_is_closed infer_instance, end lemma ideal_of_compl_singleton_is_maximal (x : X) : (ideal_of_set 𝕜 ({x}ᶜ : set X)).is_maximal := (ideal_of_set_is_maximal_iff 𝕜 (closeds.singleton x).compl).mpr $ opens.is_coatom_iff.mpr ⟨x, rfl⟩ variables {𝕜} lemma set_of_ideal_eq_compl_singleton (I : ideal C(X, 𝕜)) [hI : I.is_maximal] : ∃ x : X, set_of_ideal I = {x}ᶜ := begin have h : (ideal_of_set 𝕜 (set_of_ideal I)).is_maximal, from (ideal_of_set_of_ideal_is_closed (infer_instance : is_closed (I : set C(X, 𝕜)))).symm ▸ hI, obtain ⟨x, hx⟩ := opens.is_coatom_iff.1 ((ideal_of_set_is_maximal_iff 𝕜 (opens_of_ideal I)).1 h), exact ⟨x, congr_arg coe hx⟩, end lemma ideal_is_maximal_iff (I : ideal C(X, 𝕜)) [hI : is_closed (I : set C(X, 𝕜))] : I.is_maximal ↔ ∃ x : X, ideal_of_set 𝕜 {x}ᶜ = I := begin refine ⟨_, λ h, let ⟨x, hx⟩ := h in hx ▸ ideal_of_compl_singleton_is_maximal 𝕜 x⟩, introI hI', obtain ⟨x, hx⟩ := set_of_ideal_eq_compl_singleton I, exact ⟨x, by simpa only [ideal_of_set_of_ideal_eq_closure, ideal.closure_eq_of_is_closed] using congr_arg (ideal_of_set 𝕜) hx.symm⟩, end end is_R_or_C end continuous_map namespace weak_dual namespace character_space open function continuous_map variables (X 𝕜 : Type*) [topological_space X] section continuous_map_eval variables [locally_compact_space X] [comm_ring 𝕜] [topological_space 𝕜] [topological_ring 𝕜] variables [nontrivial 𝕜] [no_zero_divisors 𝕜] /-- The natural continuous map from a locally compact topological space `X` to the `character_space 𝕜 C(X, 𝕜)` which sends `x : X` to point evaluation at `x`. -/ def continuous_map_eval : C(X, character_space 𝕜 C(X, 𝕜)) := { to_fun := λ x, ⟨{ to_fun := λ f, f x, map_add' := λ f g, rfl, map_smul' := λ z f, rfl, cont := continuous_eval_const' x }, by { rw character_space.eq_set_map_one_map_mul, exact ⟨rfl, λ f g, rfl⟩ }⟩, continuous_to_fun := continuous.subtype_mk (continuous_of_continuous_eval map_continuous) _ } @[simp] lemma continuous_map_eval_apply_apply (x : X) (f : C(X, 𝕜)) : continuous_map_eval X 𝕜 x f = f x := rfl end continuous_map_eval variables [compact_space X] [t2_space X] [is_R_or_C 𝕜] lemma continuous_map_eval_bijective : bijective (continuous_map_eval X 𝕜) := begin refine ⟨λ x y hxy, _, λ φ, _⟩, { contrapose! hxy, haveI := @normal_of_compact_t2 X _ _ _, rcases exists_continuous_zero_one_of_closed (is_closed_singleton : _root_.is_closed {x}) (is_closed_singleton : _root_.is_closed {y}) (set.disjoint_singleton.mpr hxy) with ⟨f, fx, fy, -⟩, rw [←ne.def, fun_like.ne_iff], use (⟨coe, is_R_or_C.continuous_of_real⟩ : C(ℝ, 𝕜)).comp f, simpa only [continuous_map_eval_apply_apply, continuous_map.comp_apply, coe_mk, ne.def, is_R_or_C.of_real_inj] using ((fx (set.mem_singleton x)).symm ▸ (fy (set.mem_singleton y)).symm ▸ zero_ne_one : f x ≠ f y) }, { obtain ⟨x, hx⟩ := (ideal_is_maximal_iff (ring_hom.ker φ)).mp infer_instance, refine ⟨x, ext_ker $ ideal.ext $ λ f, _⟩, simpa only [ring_hom.mem_ker, continuous_map_eval_apply_apply, mem_ideal_of_set_compl_singleton, ring_hom.mem_ker] using set_like.ext_iff.mp hx f } end /-- This is the natural homeomorphism between a compact Hausdorff space `X` and the `character_space 𝕜 C(X, 𝕜)`. -/ noncomputable def homeo_eval : X ≃ₜ character_space 𝕜 C(X, 𝕜) := @continuous.homeo_of_equiv_compact_to_t2 _ _ _ _ _ _ { to_fun := (continuous_map_eval X 𝕜), .. equiv.of_bijective _ (continuous_map_eval_bijective X 𝕜) } (map_continuous (continuous_map_eval X 𝕜)) end character_space end weak_dual
49460076f1313814e9c7aeb4520bd0818a62886e
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/measure_theory/bochner_integration.lean
9ba815e253006be11ca5b6bb3533ae691fa95e15
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
54,985
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 measure_theory.simple_func_dense import analysis.normed_space.bounded_linear_maps /-! # Bochner integral The Bochner integral extends the definition of the Lebesgue integral to functions that map from a measure space into a Banach space (complete normed vector space). It is constructed here by extending the integral on simple functions. ## Main definitions The Bochner integral is defined following these steps: 1. Define the integral on simple functions of the type `simple_func α β` (notation : `α →ₛ β`) where `β` is a real normed space. (See `simple_func.bintegral` and section `bintegral` for details. Also see `simple_func.integral` for the integral on simple functions of the type `simple_func α ennreal`.) 2. Use `simple_func α β` to cut out the simple functions from L1 functions, and define integral on these. The type of simple functions in L1 space is written as `α →₁ₛ β`. 3. Show that the embedding of `α →₁ₛ β` into L1 is a dense and uniform one. 4. Show that the integral defined on `α →₁ₛ β` is a continuous linear map. 5. Define the Bochner integral on L1 functions by extending the integral on integrable simple functions `α →₁ₛ β` using `continuous_linear_map.extend`. Define the Bochner integral on functions as the Bochner integral of its equivalence class in L1 space. ## Main statements 1. Basic properties of the Bochner integral on functions of type `α → β`, where `α` is a measure space and `β` is a real normed space. * `integral_zero` : `∫ 0 = 0` * `integral_add` : `∫ f + g = ∫ f + ∫ g` * `integral_neg` : `∫ -f = - ∫ f` * `integral_sub` : `∫ f - g = ∫ f - ∫ g` * `integral_smul` : `∫ r • f = r • ∫ f` * `integral_congr_ae` : `∀ₘ a, f a = g a → ∫ f = ∫ g` * `norm_integral_le_integral_norm` : `∥∫ f∥ ≤ ∫ ∥f∥` 2. Basic properties of the Bochner integral on functions of type `α → ℝ`, where `α` is a measure space. * `integral_nonneg_of_ae` : `∀ₘ a, 0 ≤ f a → 0 ≤ ∫ f` * `integral_nonpos_of_nonpos_ae` : `∀ₘ a, f a ≤ 0 → ∫ f ≤ 0` * `integral_le_integral_of_le_ae` : `∀ₘ a, f a ≤ g a → ∫ f ≤ ∫ g` 3. Propositions connecting the Bochner integral with the integral on `ennreal`-valued functions, which is called `lintegral` and has the notation `∫⁻`. * `integral_eq_lintegral_max_sub_lintegral_min` : `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, where `f⁺` is the positive part of `f` and `f⁻` is the negative part of `f`. * `integral_eq_lintegral_of_nonneg_ae` : `∀ₘ a, 0 ≤ f a → ∫ f = ∫⁻ f` 4. `tendsto_integral_of_dominated_convergence` : the Lebesgue dominated convergence theorem ## Notes Some tips on how to prove a proposition if the API for the Bochner integral is not enough so that you need to unfold the definition of the Bochner integral and go back to simple functions. See `integral_eq_lintegral_max_sub_lintegral_min` for a complicated example, which proves that `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, with the first integral sign being the Bochner integral of a real-valued function f : α → ℝ, and second and third integral sign being the integral on ennreal-valued functions (called `lintegral`). The proof of `integral_eq_lintegral_max_sub_lintegral_min` is scattered in sections with the name `pos_part`. Here are the usual steps of proving that a property `p`, say `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, holds for all functions : 1. First go to the `L¹` space. For example, if you see `ennreal.to_real (∫⁻ a, ennreal.of_real $ ∥f a∥)`, that is the norm of `f` in `L¹` space. Rewrite using `l1.norm_of_fun_eq_lintegral_norm`. 2. Show that the set `{f ∈ L¹ | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}` is closed in `L¹` using `is_closed_eq`. 3. Show that the property holds for all simple functions `s` in `L¹` space. Typically, you need to convert various notions to their `simple_func` counterpart, using lemmas like `l1.integral_coe_eq_integral`. 4. Since simple functions are dense in `L¹`, ``` univ = closure {s simple} = closure {s simple | ∫ s = ∫⁻ s⁺ - ∫⁻ s⁻} : the property holds for all simple functions ⊆ closure {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} = {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} : closure of a closed set is itself ``` Use `is_closed_property` or `dense_range.induction_on` for this argument. ## Notations * `α →ₛ β` : simple functions (defined in `measure_theory/integration`) * `α →₁ β` : functions in L1 space, i.e., equivalence classes of integrable functions (defined in `measure_theory/l1_space`) * `α →₁ₛ β` : simple functions in L1 space, i.e., equivalence classes of integrable simple functions Note : `ₛ` is typed using `\_s`. Sometimes it shows as a box if font is missing. ## Tags Bochner integral, simple function, function space, Lebesgue dominated convergence theorem -/ noncomputable theory open_locale classical topological_space set_option class.instance_max_depth 100 -- Typeclass inference has difficulty finding `has_scalar ℝ β` where `β` is a `normed_space` on `ℝ` local attribute [instance, priority 10000] mul_action.to_has_scalar distrib_mul_action.to_mul_action add_comm_group.to_add_comm_monoid normed_group.to_add_comm_group normed_space.to_module module.to_semimodule namespace measure_theory universes u v w variables {α : Type u} [measurable_space α] {β : Type v} [decidable_linear_order β] [has_zero β] local infixr ` →ₛ `:25 := simple_func namespace simple_func section pos_part /-- Positive part of a simple function. -/ def pos_part (f : α →ₛ β) : α →ₛ β := f.map (λb, max b 0) /-- Negative part of a simple function. -/ def neg_part [has_neg β] (f : α →ₛ β) : α →ₛ β := pos_part (-f) lemma pos_part_map_norm (f : α →ₛ ℝ) : (pos_part f).map norm = pos_part f := begin ext, rw [map_apply, real.norm_eq_abs, abs_of_nonneg], rw [pos_part, map_apply], exact le_max_right _ _ end lemma neg_part_map_norm (f : α →ₛ ℝ) : (neg_part f).map norm = neg_part f := by { rw neg_part, exact pos_part_map_norm _ } lemma pos_part_sub_neg_part (f : α →ₛ ℝ) : f.pos_part - f.neg_part = f := begin simp only [pos_part, neg_part], ext, exact max_zero_sub_eq_self (f a) end end pos_part end simple_func end measure_theory namespace measure_theory open set filter topological_space ennreal emetric universes u v w variables {α : Type u} [measure_space α] {β : Type v} {γ : Type w} local infixr ` →ₛ `:25 := simple_func namespace simple_func section bintegral /-! ### The Bochner integral of simple functions Define the Bochner integral of simple functions of the type `α →ₛ β` where `β` is a normed group, and prove basic property of this integral. -/ open finset variables [normed_group β] [normed_group γ] lemma integrable_iff_integral_lt_top {f : α →ₛ β} : integrable f ↔ integral (f.map (coe ∘ nnnorm)) < ⊤ := by { rw [integrable, ← lintegral_eq_integral, lintegral_map] } lemma fin_vol_supp_of_integrable {f : α →ₛ β} (hf : integrable f) : f.fin_vol_supp := begin rw [integrable_iff_integral_lt_top] at hf, have hf := fin_vol_supp_of_integral_lt_top hf, refine fin_vol_supp_of_fin_vol_supp_map f hf _, assume b, simp [nnnorm_eq_zero] end lemma integrable_of_fin_vol_supp {f : α →ₛ β} (h : f.fin_vol_supp) : integrable f := by { rw [integrable_iff_integral_lt_top], exact integral_map_coe_lt_top h nnnorm_zero } /-- For simple functions with a `normed_group` as codomain, being integrable is the same as having finite volume support. -/ lemma integrable_iff_fin_vol_supp (f : α →ₛ β) : integrable f ↔ f.fin_vol_supp := iff.intro fin_vol_supp_of_integrable integrable_of_fin_vol_supp lemma integrable_pair {f : α →ₛ β} {g : α →ₛ γ} (hf : integrable f) (hg : integrable g) : integrable (pair f g) := by { rw integrable_iff_fin_vol_supp at *, apply fin_vol_supp_pair; assumption } variables [normed_space ℝ γ] /-- Bochner integral of simple functions whose codomain is a real `normed_space`. The name `simple_func.integral` has been taken in the file `integration.lean`, which calculates the integral of a simple function with type `α → ennreal`. The name `bintegral` stands for Bochner integral. -/ def bintegral [normed_space ℝ β] (f : α →ₛ β) : β := f.range.sum (λ x, (ennreal.to_real (volume (f ⁻¹' {x}))) • x) /-- Calculate the integral of `g ∘ f : α →ₛ γ`, where `f` is an integrable function from `α` to `β` and `g` is a function from `β` to `γ`. We require `g 0 = 0` so that `g ∘ f` is integrable. -/ lemma map_bintegral (f : α →ₛ β) (g : β → γ) (hf : integrable f) (hg : g 0 = 0) : (f.map g).bintegral = f.range.sum (λ x, (ennreal.to_real (volume (f ⁻¹' {x}))) • (g x)) := begin /- Just a complicated calculation with `finset.sum`. Real work is done by `map_preimage_singleton`, `simple_func.volume_bUnion_preimage` and `ennreal.to_real_sum` -/ rw integrable_iff_fin_vol_supp at hf, simp only [bintegral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, let s' := f.range.filter (λb, g b = g (f a)), calc (ennreal.to_real (volume ((f.map g) ⁻¹' {g (f a)}))) • (g (f a)) = (ennreal.to_real (volume (⋃b∈s', f ⁻¹' {b}))) • (g (f a)) : by rw map_preimage_singleton ... = (ennreal.to_real (s'.sum (λb, volume (f ⁻¹' {b})))) • (g (f a)) : by rw volume_bUnion_preimage ... = (s'.sum (λb, ennreal.to_real (volume (f ⁻¹' {b})))) • (g (f a)) : begin by_cases h : g (f a) = 0, { rw [h, smul_zero, smul_zero] }, { rw ennreal.to_real_sum, simp only [mem_filter], rintros b ⟨_, hb⟩, have : b ≠ 0, { assume hb', rw [← hb, hb'] at h, contradiction }, apply hf, assumption } end ... = s'.sum (λb, (ennreal.to_real (volume (f ⁻¹' {b}))) • (g (f a))) : finset.sum_smul ... = s'.sum (λb, (ennreal.to_real (volume (f ⁻¹' {b}))) • (g b)) : finset.sum_congr rfl $ by { assume x, simp only [mem_filter], rintro ⟨_, h⟩, rw h } end /-- `simple_func.bintegral` and `simple_func.integral` agree when the integrand has type `α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion. See `bintegral_eq_integral'` for a simpler version. -/ lemma bintegral_eq_integral {f : α →ₛ β} {g : β → ennreal} (hf : integrable f) (hg0 : g 0 = 0) (hgt : ∀b, g b < ⊤): (f.map (ennreal.to_real ∘ g)).bintegral = ennreal.to_real (f.map g).integral := begin have hf' : f.fin_vol_supp, { rwa integrable_iff_fin_vol_supp at hf }, rw [map_bintegral f _ hf, map_integral, ennreal.to_real_sum], { refine finset.sum_congr rfl (λb hb, _), rw [smul_eq_mul], rw [to_real_mul_to_real, mul_comm] }, { assume a ha, by_cases a0 : a = 0, { rw [a0, hg0, zero_mul], exact with_top.zero_lt_top }, apply mul_lt_top (hgt a) (hf' _ a0) }, { simp [hg0] } end /-- `simple_func.bintegral` and `lintegral : (α → ennreal) → ennreal` are the same when the integrand has type `α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion. See `bintegral_eq_lintegral'` for a simpler version. -/ lemma bintegral_eq_lintegral (f : α →ₛ β) (g : β → ennreal) (hf : integrable f) (hg0 : g 0 = 0) (hgt : ∀b, g b < ⊤): (f.map (ennreal.to_real ∘ g)).bintegral = ennreal.to_real (∫⁻ a, g (f a)) := by { rw [bintegral_eq_integral hf hg0 hgt, ← lintegral_eq_integral], refl } variables [normed_space ℝ β] lemma bintegral_congr {f g : α →ₛ β} (hf : integrable f) (hg : integrable g) (h : ∀ₘ a, f a = g a): bintegral f = bintegral g := show ((pair f g).map prod.fst).bintegral = ((pair f g).map prod.snd).bintegral, from begin have inte := integrable_pair hf hg, rw [map_bintegral (pair f g) _ inte prod.fst_zero, map_bintegral (pair f g) _ inte prod.snd_zero], refine finset.sum_congr rfl (assume p hp, _), rcases mem_range.1 hp with ⟨a, rfl⟩, by_cases eq : f a = g a, { dsimp only [pair_apply], rw eq }, { have : volume ((pair f g) ⁻¹' {(f a, g a)}) = 0, { refine volume_mono_null (assume a' ha', _) h, simp only [set.mem_preimage, mem_singleton_iff, pair_apply, prod.mk.inj_iff] at ha', show f a' ≠ g a', rwa [ha'.1, ha'.2] }, simp only [this, pair_apply, zero_smul, ennreal.zero_to_real] }, end /-- `simple_func.bintegral` and `simple_func.integral` agree when the integrand has type `α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion. -/ lemma bintegral_eq_integral' {f : α →ₛ ℝ} (hf : integrable f) (h_pos : ∀ₘ a, 0 ≤ f a) : f.bintegral = ennreal.to_real (f.map ennreal.of_real).integral := begin have : ∀ₘ a, f a = (f.map (ennreal.to_real ∘ ennreal.of_real)) a, { filter_upwards [h_pos], assume a, simp only [mem_set_of_eq, map_apply, function.comp_apply], assume h, exact (ennreal.to_real_of_real h).symm }, rw ← bintegral_eq_integral hf, { refine bintegral_congr hf _ this, exact integrable_of_ae_eq hf this }, { exact ennreal.of_real_zero }, { assume b, rw ennreal.lt_top_iff_ne_top, exact ennreal.of_real_ne_top } end /-- `simple_func.bintegral` and `lintegral : (α → ennreal) → ennreal` agree when the integrand has type `α →ₛ ennreal`. But since `ennreal` is not a `normed_space`, we need some form of coercion. -/ lemma bintegral_eq_lintegral' {f : α →ₛ ℝ} (hf : integrable f) (h_pos : ∀ₘ a, 0 ≤ f a) : f.bintegral = ennreal.to_real (∫⁻ a, (f.map ennreal.of_real a)) := by rw [bintegral_eq_integral' hf h_pos, ← lintegral_eq_integral] lemma bintegral_add {f g : α →ₛ β} (hf : integrable f) (hg : integrable g) : bintegral (f + g) = bintegral f + bintegral g := calc bintegral (f + g) = (pair f g).range.sum (λx, ennreal.to_real (volume ((pair f g) ⁻¹' {x})) • (x.fst + x.snd)) : begin rw [add_eq_map₂, map_bintegral (pair f g)], { exact integrable_pair hf hg }, { simp only [add_zero, prod.fst_zero, prod.snd_zero] } end ... = (pair f g).range.sum (λx, ennreal.to_real (volume ((pair f g) ⁻¹' {x})) • x.fst + ennreal.to_real (volume ((pair f g) ⁻¹' {x})) • x.snd) : finset.sum_congr rfl $ assume a ha, smul_add _ _ _ ... = (simple_func.range (pair f g)).sum (λ (x : β × β), ennreal.to_real (volume ((pair f g) ⁻¹' {x})) • x.fst) + (simple_func.range (pair f g)).sum (λ (x : β × β), ennreal.to_real (volume ((pair f g) ⁻¹' {x})) • x.snd) : by rw finset.sum_add_distrib ... = ((pair f g).map prod.fst).bintegral + ((pair f g).map prod.snd).bintegral : begin rw [map_bintegral (pair f g), map_bintegral (pair f g)], { exact integrable_pair hf hg }, { refl }, { exact integrable_pair hf hg }, { refl } end ... = bintegral f + bintegral g : rfl lemma bintegral_neg {f : α →ₛ β} (hf : integrable f) : bintegral (-f) = - bintegral f := calc bintegral (-f) = bintegral (f.map (has_neg.neg)) : rfl ... = - bintegral f : begin rw [map_bintegral f _ hf neg_zero, bintegral, ← sum_neg_distrib], refine finset.sum_congr rfl (λx h, smul_neg _ _), end lemma bintegral_sub {f g : α →ₛ β} (hf : integrable f) (hg : integrable g) : bintegral (f - g) = bintegral f - bintegral g := begin have : f - g = f + (-g) := rfl, rw [this, bintegral_add hf _, bintegral_neg hg], { refl }, exact hg.neg end lemma bintegral_smul (r : ℝ) {f : α →ₛ β} (hf : integrable f) : bintegral (r • f) = r • bintegral f := calc bintegral (r • f) = f.range.sum (λx, ennreal.to_real (volume (f ⁻¹' {x})) • r • x) : by rw [smul_eq_map r f, map_bintegral f _ hf (smul_zero _)] ... = f.range.sum (λ (x : β), ((ennreal.to_real (volume (f ⁻¹' {x}))) * r) • x) : finset.sum_congr rfl $ λb hb, by apply smul_smul ... = r • bintegral f : begin rw [bintegral, smul_sum], refine finset.sum_congr rfl (λb hb, _), rw [smul_smul, mul_comm] end lemma norm_bintegral_le_bintegral_norm (f : α →ₛ β) (hf : integrable f) : ∥f.bintegral∥ ≤ (f.map norm).bintegral := begin rw map_bintegral f norm hf norm_zero, rw bintegral, calc ∥f.range.sum (λx, ennreal.to_real (volume (f ⁻¹' {x})) • x)∥ ≤ f.range.sum (λx, ∥ennreal.to_real (volume (f ⁻¹' {x})) • x∥) : norm_sum_le _ _ ... = f.range.sum (λx, ennreal.to_real (volume (f ⁻¹' {x})) • ∥x∥) : begin refine finset.sum_congr rfl (λb hb, _), rw [norm_smul, smul_eq_mul, real.norm_eq_abs, abs_of_nonneg to_real_nonneg] end end end bintegral end simple_func namespace l1 open ae_eq_fun variables [normed_group β] [second_countable_topology β] [normed_group γ] [second_countable_topology γ] variables (α β) /-- `l1.simple_func` is a subspace of L1 consisting of equivalence classes of an integrable simple function. -/ def simple_func : Type (max u v) := { f : α →₁ β // ∃ (s : α →ₛ β), integrable s ∧ ae_eq_fun.mk s s.measurable = f} variables {α β} infixr ` →₁ₛ `:25 := measure_theory.l1.simple_func namespace simple_func section instances /-! Simple functions in L1 space form a `normed_space`. -/ instance : has_coe (α →₁ₛ β) (α →₁ β) := ⟨subtype.val⟩ protected lemma eq {f g : α →₁ₛ β} : (f : α →₁ β) = (g : α →₁ β) → f = g := subtype.eq protected lemma eq' {f g : α →₁ₛ β} : (f : α →ₘ β) = (g : α →ₘ β) → f = g := subtype.eq ∘ subtype.eq @[elim_cast] protected lemma eq_iff {f g : α →₁ₛ β} : (f : α →₁ β) = (g : α →₁ β) ↔ f = g := iff.intro (subtype.eq) (congr_arg coe) @[elim_cast] protected lemma eq_iff' {f g : α →₁ₛ β} : (f : α →ₘ β) = (g : α →ₘ β) ↔ f = g := iff.intro (simple_func.eq') (congr_arg _) /-- L1 simple functions forms a `emetric_space`, with the emetric being inherited from L1 space, i.e., `edist f g = ∫⁻ a, edist (f a) (g a)`. Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def emetric_space : emetric_space (α →₁ₛ β) := subtype.emetric_space /-- L1 simple functions forms a `metric_space`, with the metric being inherited from L1 space, i.e., `dist f g = ennreal.to_real (∫⁻ a, edist (f a) (g a)`). Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def metric_space : metric_space (α →₁ₛ β) := subtype.metric_space local attribute [instance] protected lemma is_add_subgroup : is_add_subgroup (λf:α →₁ β, ∃ (s : α →ₛ β), integrable s ∧ ae_eq_fun.mk s s.measurable = f) := { zero_mem := ⟨0, integrable_zero _ _, rfl⟩, add_mem := begin rintros f g ⟨s, hsi, hs⟩ ⟨t, hti, ht⟩, use s + t, split, { exact hsi.add s.measurable t.measurable hti }, { rw [coe_add, ← hs, ← ht], refl } end, neg_mem := begin rintros f ⟨s, hsi, hs⟩, use -s, split, { exact hsi.neg }, { rw [coe_neg, ← hs], refl } end } /-- Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def add_comm_group : add_comm_group (α →₁ₛ β) := subtype.add_comm_group local attribute [instance] simple_func.add_comm_group simple_func.metric_space simple_func.emetric_space instance : inhabited (α →₁ₛ β) := ⟨0⟩ @[simp, elim_cast] lemma coe_zero : ((0 : α →₁ₛ β) : α →₁ β) = 0 := rfl @[simp, move_cast] lemma coe_add (f g : α →₁ₛ β) : ((f + g : α →₁ₛ β) : α →₁ β) = f + g := rfl @[simp, move_cast] lemma coe_neg (f : α →₁ₛ β) : ((-f : α →₁ₛ β) : α →₁ β) = -f := rfl @[simp, move_cast] lemma coe_sub (f g : α →₁ₛ β) : ((f - g : α →₁ₛ β) : α →₁ β) = f - g := rfl @[simp] lemma edist_eq (f g : α →₁ₛ β) : edist f g = edist (f : α →₁ β) (g : α →₁ β) := rfl @[simp] lemma dist_eq (f g : α →₁ₛ β) : dist f g = dist (f : α →₁ β) (g : α →₁ β) := rfl /-- The norm on `α →₁ₛ β` is inherited from L1 space. That is, `∥f∥ = ∫⁻ a, edist (f a) 0`. Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def has_norm : has_norm (α →₁ₛ β) := ⟨λf, ∥(f : α →₁ β)∥⟩ local attribute [instance] simple_func.has_norm lemma norm_eq (f : α →₁ₛ β) : ∥f∥ = ∥(f : α →₁ β)∥ := rfl lemma norm_eq' (f : α →₁ₛ β) : ∥f∥ = ennreal.to_real (edist (f : α →ₘ β) 0) := rfl /-- Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def normed_group : normed_group (α →₁ₛ β) := normed_group.of_add_dist (λ x, rfl) $ by { intros, simp only [dist_eq, coe_add, l1.dist_eq, l1.coe_add], rw edist_eq_add_add } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] /-- Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def has_scalar : has_scalar 𝕜 (α →₁ₛ β) := ⟨λk f, ⟨k • f, begin rcases f with ⟨f, ⟨s, hsi, hs⟩⟩, use k • s, split, { exact integrable.smul _ hsi }, { rw [coe_smul, subtype.coe_mk, ← hs], refl } end ⟩⟩ local attribute [instance, priority 10000] simple_func.has_scalar @[simp, move_cast] lemma coe_smul (c : 𝕜) (f : α →₁ₛ β) : ((c • f : α →₁ₛ β) : α →₁ β) = c • (f : α →₁ β) := rfl /-- Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def semimodule : semimodule 𝕜 (α →₁ₛ β) := { one_smul := λf, simple_func.eq (by { simp only [coe_smul], exact one_smul _ _ }), mul_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }), smul_add := λx f g, simple_func.eq (by { simp only [coe_smul, coe_add], exact smul_add _ _ _ }), smul_zero := λx, simple_func.eq (by { simp only [coe_zero, coe_smul], exact smul_zero _ }), add_smul := λx y f, simple_func.eq (by { simp only [coe_smul], exact add_smul _ _ _ }), zero_smul := λf, simple_func.eq (by { simp only [coe_smul], exact zero_smul _ _ }) } /-- Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def module : module 𝕜 (α →₁ₛ β) := { .. simple_func.semimodule } /-- Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def vector_space : vector_space 𝕜 (α →₁ₛ β) := { .. simple_func.semimodule } local attribute [instance] simple_func.vector_space simple_func.normed_group /-- Not declared as an instance as `α →₁ₛ β` will only be useful in the construction of the bochner integral. -/ protected def normed_space : normed_space 𝕜 (α →₁ₛ β) := ⟨ λc f, by { rw [norm_eq, norm_eq, coe_smul, norm_smul] } ⟩ end instances local attribute [instance] simple_func.normed_group simple_func.normed_space section of_simple_func /-- Construct the equivalence class `[f]` of an integrable simple function `f`. -/ @[reducible] def of_simple_func (f : α →ₛ β) (hf : integrable f) : (α →₁ₛ β) := ⟨l1.of_fun f f.measurable hf, ⟨f, ⟨hf, rfl⟩⟩⟩ lemma of_simple_func_eq_of_fun (f : α →ₛ β) (hf : integrable f) : (of_simple_func f hf : α →₁ β) = l1.of_fun f f.measurable hf := rfl lemma of_simple_func_eq_mk (f : α →ₛ β) (hf : integrable f) : (of_simple_func f hf : α →ₘ β) = ae_eq_fun.mk f f.measurable := rfl lemma of_simple_func_zero : of_simple_func (0 : α →ₛ β) (integrable_zero α β) = 0 := rfl lemma of_simple_func_add (f g : α →ₛ β) (hf hg) : of_simple_func (f + g) (integrable.add f.measurable hf g.measurable hg) = of_simple_func f hf + of_simple_func g hg := rfl lemma of_simple_func_neg (f : α →ₛ β) (hf) : of_simple_func (-f) (integrable.neg hf) = -of_simple_func f hf := rfl lemma of_simple_func_sub (f g : α →ₛ β) (hf hg) : of_simple_func (f - g) (integrable.sub f.measurable hf g.measurable hg) = of_simple_func f hf - of_simple_func g hg := rfl variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma of_simple_func_smul (f : α →ₛ β) (hf) (c : 𝕜) : of_simple_func (c • f) (integrable.smul _ hf) = c • of_simple_func f hf := rfl lemma norm_of_simple_func (f : α →ₛ β) (hf) : ∥of_simple_func f hf∥ = ennreal.to_real (∫⁻ a, edist (f a) 0) := rfl end of_simple_func section to_simple_func /-- Find a representative of a `l1.simple_func`. -/ def to_simple_func (f : α →₁ₛ β) : α →ₛ β := classical.some f.2 /-- `f.to_simple_func` is measurable. -/ protected lemma measurable (f : α →₁ₛ β) : measurable f.to_simple_func := f.to_simple_func.measurable /-- `f.to_simple_func` is integrable. -/ protected lemma integrable (f : α →₁ₛ β) : integrable f.to_simple_func := let ⟨h, _⟩ := classical.some_spec f.2 in h lemma of_simple_func_to_simple_func (f : α →₁ₛ β) : of_simple_func (f.to_simple_func) f.integrable = f := by { rw ← simple_func.eq_iff', exact (classical.some_spec f.2).2 } lemma to_simple_func_of_simple_func (f : α →ₛ β) (hfi) : ∀ₘ a, (of_simple_func f hfi).to_simple_func a = f a := by { rw ← mk_eq_mk, exact (classical.some_spec (of_simple_func f hfi).2).2 } lemma to_simple_func_eq_to_fun (f : α →₁ₛ β) : ∀ₘ a, (f.to_simple_func) a = (f : α →₁ β).to_fun a := begin rw [← of_fun_eq_of_fun (f.to_simple_func) (f : α →₁ β).to_fun f.measurable f.integrable (f:α→₁β).measurable (f:α→₁β).integrable, ← l1.eq_iff], simp only [of_fun_eq_mk], rcases classical.some_spec f.2 with ⟨_, h⟩, convert h, rw mk_to_fun, refl end variables (α β) lemma zero_to_simple_func : ∀ₘ a, (0 : α →₁ₛ β).to_simple_func a = 0 := begin filter_upwards [to_simple_func_eq_to_fun (0 : α →₁ₛ β), l1.zero_to_fun α β], assume a, simp only [mem_set_of_eq], assume h, rw h, assume h, exact h end variables {α β} lemma add_to_simple_func (f g : α →₁ₛ β) : ∀ₘ a, (f + g).to_simple_func a = f.to_simple_func a + g.to_simple_func a := begin filter_upwards [to_simple_func_eq_to_fun (f + g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, l1.add_to_fun (f:α→₁β) g], assume a, simp only [mem_set_of_eq], repeat { assume h, rw h }, assume h, rw ← h, refl end lemma neg_to_simple_func (f : α →₁ₛ β) : ∀ₘ a, (-f).to_simple_func a = - f.to_simple_func a := begin filter_upwards [to_simple_func_eq_to_fun (-f), to_simple_func_eq_to_fun f, l1.neg_to_fun (f:α→₁β)], assume a, simp only [mem_set_of_eq], repeat { assume h, rw h }, assume h, rw ← h, refl end lemma sub_to_simple_func (f g : α →₁ₛ β) : ∀ₘ a, (f - g).to_simple_func a = f.to_simple_func a - g.to_simple_func a := begin filter_upwards [to_simple_func_eq_to_fun (f - g), to_simple_func_eq_to_fun f, to_simple_func_eq_to_fun g, l1.sub_to_fun (f:α→₁β) g], assume a, simp only [mem_set_of_eq], repeat { assume h, rw h }, assume h, rw ← h, refl end variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma smul_to_simple_func (k : 𝕜) (f : α →₁ₛ β) : ∀ₘ a, (k • f).to_simple_func a = k • f.to_simple_func a := begin filter_upwards [to_simple_func_eq_to_fun (k • f), to_simple_func_eq_to_fun f, l1.smul_to_fun k (f:α→₁β)], assume a, simp only [mem_set_of_eq], repeat { assume h, rw h }, assume h, rw ← h, refl end lemma lintegral_edist_to_simple_func_lt_top (f g : α →₁ₛ β) : (∫⁻ (x : α), edist ((to_simple_func f) x) ((to_simple_func g) x)) < ⊤ := begin rw lintegral_rw₂ (to_simple_func_eq_to_fun f) (to_simple_func_eq_to_fun g), exact lintegral_edist_to_fun_lt_top _ _ end lemma dist_to_simple_func (f g : α →₁ₛ β) : dist f g = ennreal.to_real (∫⁻ x, edist (f.to_simple_func x) (g.to_simple_func x)) := begin rw [dist_eq, l1.dist_to_fun, ennreal.to_real_eq_to_real], { rw lintegral_rw₂, repeat { exact all_ae_eq_symm (to_simple_func_eq_to_fun _) } }, { exact l1.lintegral_edist_to_fun_lt_top _ _ }, { exact lintegral_edist_to_simple_func_lt_top _ _ } end lemma norm_to_simple_func (f : α →₁ₛ β) : ∥f∥ = ennreal.to_real (∫⁻ (a : α), nnnorm ((to_simple_func f) a)) := calc ∥f∥ = ennreal.to_real (∫⁻x, edist (f.to_simple_func x) ((0 : α →₁ₛ β).to_simple_func x)) : begin rw [← dist_zero_right, dist_to_simple_func] end ... = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) (f.to_simple_func x)) : begin rw lintegral_nnnorm_eq_lintegral_edist, have : (∫⁻ (x : α), edist ((to_simple_func f) x) ((to_simple_func (0:α→₁ₛβ)) x)) = ∫⁻ (x : α), edist ((to_simple_func f) x) 0, { apply lintegral_congr_ae, filter_upwards [zero_to_simple_func α β], assume a, simp only [mem_set_of_eq], assume h, rw h }, rw [ennreal.to_real_eq_to_real], { exact this }, { exact lintegral_edist_to_simple_func_lt_top _ _ }, { rw ← this, exact lintegral_edist_to_simple_func_lt_top _ _ } end lemma norm_eq_bintegral (f : α →₁ₛ β) : ∥f∥ = (f.to_simple_func.map norm).bintegral := calc ∥f∥ = ennreal.to_real (∫⁻ (x : α), (coe ∘ nnnorm) (f.to_simple_func x)) : by { rw norm_to_simple_func } ... = (f.to_simple_func.map norm).bintegral : begin rw ← f.to_simple_func.bintegral_eq_lintegral (coe ∘ nnnorm) f.integrable, { congr }, { simp only [nnnorm_zero, function.comp_app, ennreal.coe_zero] }, { assume b, exact coe_lt_top } end end to_simple_func section coe_to_l1 /-! The embedding of integrable simple functions `α →₁ₛ β` into L1 is a uniform and dense embedding. -/ lemma exists_simple_func_near (f : α →₁ β) {ε : ℝ} (ε0 : 0 < ε) : ∃ s : α →₁ₛ β, dist f s < ε := begin rcases f with ⟨⟨f, hfm⟩, hfi⟩, simp only [integrable_mk, quot_mk_eq_mk] at hfi, rcases simple_func_sequence_tendsto' hfm hfi with ⟨F, ⟨h₁, h₂⟩⟩, rw ennreal.tendsto_at_top at h₂, rcases h₂ (ennreal.of_real (ε/2)) (of_real_pos.2 $ half_pos ε0) with ⟨N, hN⟩, have : (∫⁻ (x : α), nndist (F N x) (f x)) < ennreal.of_real ε := calc (∫⁻ (x : α), nndist (F N x) (f x)) ≤ 0 + ennreal.of_real (ε/2) : (hN N (le_refl _)).2 ... < ennreal.of_real ε : by { simp only [zero_add, of_real_lt_of_real_iff ε0], exact half_lt_self ε0 }, { refine ⟨of_simple_func (F N) (h₁ N), _⟩, rw dist_comm, rw lt_of_real_iff_to_real_lt _ at this, { simpa [edist_mk_mk', of_simple_func, l1.of_fun, l1.dist_eq] }, rw ← lt_top_iff_ne_top, exact lt_trans this (by simp [lt_top_iff_ne_top, of_real_ne_top]) }, { exact zero_ne_top } end protected lemma uniform_continuous : uniform_continuous (coe : (α →₁ₛ β) → (α →₁ β)) := uniform_continuous_comap protected lemma uniform_embedding : uniform_embedding (coe : (α →₁ₛ β) → (α →₁ β)) := uniform_embedding_comap subtype.val_injective protected lemma uniform_inducing : uniform_inducing (coe : (α →₁ₛ β) → (α →₁ β)) := simple_func.uniform_embedding.to_uniform_inducing protected lemma dense_embedding : dense_embedding (coe : (α →₁ₛ β) → (α →₁ β)) := simple_func.uniform_embedding.dense_embedding $ λ f, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε,ε0, hε⟩ := metric.mem_nhds_iff.1 ht in let ⟨s, h⟩ := exists_simple_func_near f ε0 in ⟨_, hε (metric.mem_ball'.2 h), s, rfl⟩ protected lemma dense_inducing : dense_inducing (coe : (α →₁ₛ β) → (α →₁ β)) := simple_func.dense_embedding.to_dense_inducing protected lemma dense_range : dense_range (coe : (α →₁ₛ β) → (α →₁ β)) := simple_func.dense_inducing.dense variables (𝕜 : Type*) [normed_field 𝕜] [normed_space 𝕜 β] variables (α β) /-- The uniform and dense embedding of L1 simple functions into L1 functions. -/ def coe_to_l1 : (α →₁ₛ β) →L[𝕜] (α →₁ β) := { to_fun := (coe : (α →₁ₛ β) → (α →₁ β)), add := λf g, rfl, smul := λk f, rfl, cont := l1.simple_func.uniform_continuous.continuous, } variables {α β 𝕜} end coe_to_l1 section pos_part /-- Positive part of a simple function in L1 space. -/ def pos_part (f : α →₁ₛ ℝ) : α →₁ₛ ℝ := ⟨l1.pos_part (f : α →₁ ℝ), begin rcases f with ⟨f, s, hsi, hsf⟩, use s.pos_part, split, { exact integrable.max_zero hsi }, { simp only [subtype.coe_mk], rw [l1.coe_pos_part, ← hsf, ae_eq_fun.pos_part, ae_eq_fun.zero_def, comp₂_mk_mk, mk_eq_mk], filter_upwards [], simp only [mem_set_of_eq], assume a, refl } end ⟩ /-- Negative part of a simple function in L1 space. -/ def neg_part (f : α →₁ₛ ℝ) : α →₁ₛ ℝ := pos_part (-f) @[move_cast] lemma coe_pos_part (f : α →₁ₛ ℝ) : (f.pos_part : α →₁ ℝ) = (f : α →₁ ℝ).pos_part := rfl @[move_cast] lemma coe_neg_part (f : α →₁ₛ ℝ) : (f.neg_part : α →₁ ℝ) = (f : α →₁ ℝ).neg_part := rfl end pos_part section simple_func_integral /-! Define the Bochner integral on `α →₁ₛ β` and prove basic properties of this integral. -/ variables [normed_space ℝ β] /-- The Bochner integral over simple functions in l1 space. -/ def integral (f : α →₁ₛ β) : β := (f.to_simple_func).bintegral lemma integral_eq_bintegral (f : α →₁ₛ β) : integral f = (f.to_simple_func).bintegral := rfl lemma integral_eq_lintegral {f : α →₁ₛ ℝ} (h_pos : ∀ₘ a, 0 ≤ f.to_simple_func a) : integral f = ennreal.to_real (∫⁻ a, ennreal.of_real (f.to_simple_func a)) := by { rw [integral, simple_func.bintegral_eq_lintegral' f.integrable h_pos], refl } lemma integral_congr (f g : α →₁ₛ β) (h : ∀ₘ a, f.to_simple_func a = g.to_simple_func a) : integral f = integral g := by { simp only [integral], apply simple_func.bintegral_congr f.integrable g.integrable, exact h } lemma integral_add (f g : α →₁ₛ β) : integral (f + g) = integral f + integral g := begin simp only [integral], rw ← simple_func.bintegral_add f.integrable g.integrable, apply simple_func.bintegral_congr (f + g).integrable, { exact f.integrable.add f.measurable g.measurable g.integrable }, { apply add_to_simple_func }, end lemma integral_smul (r : ℝ) (f : α →₁ₛ β) : integral (r • f) = r • integral f := begin simp only [integral], rw ← simple_func.bintegral_smul _ f.integrable, apply simple_func.bintegral_congr (r • f).integrable, { exact integrable.smul _ f.integrable }, { apply smul_to_simple_func } end lemma norm_integral_le_norm (f : α →₁ₛ β) : ∥ integral f ∥ ≤ ∥f∥ := begin rw [integral, norm_eq_bintegral], exact f.to_simple_func.norm_bintegral_le_bintegral_norm f.integrable end /-- The Bochner integral over simple functions in l1 space as a continuous linear map. -/ def integral_clm : (α →₁ₛ β) →L[ℝ] β := linear_map.mk_continuous ⟨integral, integral_add, integral_smul⟩ 1 (λf, le_trans (norm_integral_le_norm _) $ by rw one_mul) local notation `Integral` := @integral_clm α _ β _ _ _ open continuous_linear_map lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := linear_map.mk_continuous_norm_le _ (zero_le_one) _ section pos_part lemma pos_part_to_simple_func (f : α →₁ₛ ℝ) : ∀ₘ a, f.pos_part.to_simple_func a = f.to_simple_func.pos_part a := begin have eq : ∀ a, f.to_simple_func.pos_part a = max (f.to_simple_func a) 0 := λa, rfl, have ae_eq : ∀ₘ a, f.pos_part.to_simple_func a = max (f.to_simple_func a) 0, { filter_upwards [to_simple_func_eq_to_fun f.pos_part, pos_part_to_fun (f : α →₁ ℝ), to_simple_func_eq_to_fun f], simp only [mem_set_of_eq], assume a h₁ h₂ h₃, rw [h₁, coe_pos_part, h₂, ← h₃] }, filter_upwards [ae_eq], simp only [mem_set_of_eq], assume a h, rw [h, eq] end lemma neg_part_to_simple_func (f : α →₁ₛ ℝ) : ∀ₘ a, f.neg_part.to_simple_func a = f.to_simple_func.neg_part a := begin rw [simple_func.neg_part, measure_theory.simple_func.neg_part], filter_upwards [pos_part_to_simple_func (-f), neg_to_simple_func f], simp only [mem_set_of_eq], assume a h₁ h₂, rw h₁, show max _ _ = max _ _, rw h₂, refl end lemma integral_eq_norm_pos_part_sub (f : α →₁ₛ ℝ) : f.integral = ∥f.pos_part∥ - ∥f.neg_part∥ := begin -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₁ : ∀ₘ a, f.to_simple_func.pos_part a = (f.pos_part).to_simple_func.map norm a, { filter_upwards [pos_part_to_simple_func f], simp only [mem_set_of_eq], assume a h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.pos_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq₂ : ∀ₘ a, f.to_simple_func.neg_part a = (f.neg_part).to_simple_func.map norm a, { filter_upwards [neg_part_to_simple_func f], simp only [mem_set_of_eq], assume a h, rw [simple_func.map_apply, h], conv_lhs { rw [← simple_func.neg_part_map_norm, simple_func.map_apply] } }, -- Convert things in `L¹` to their `simple_func` counterpart have ae_eq : ∀ₘ a, f.to_simple_func.pos_part a - f.to_simple_func.neg_part a = (f.pos_part).to_simple_func.map norm a - (f.neg_part).to_simple_func.map norm a, { filter_upwards [ae_eq₁, ae_eq₂], simp only [mem_set_of_eq], assume a h₁ h₂, rw [h₁, h₂] }, rw [integral, norm_eq_bintegral, norm_eq_bintegral, ← simple_func.bintegral_sub], { show f.to_simple_func.bintegral = ((f.pos_part.to_simple_func).map norm - f.neg_part.to_simple_func.map norm).bintegral, apply simple_func.bintegral_congr f.integrable, { show integrable (f.pos_part.to_simple_func.map norm - f.neg_part.to_simple_func.map norm), refine integrable_of_ae_eq _ _, { exact (f.to_simple_func.pos_part - f.to_simple_func.neg_part) }, { exact (integrable.max_zero f.integrable).sub f.to_simple_func.pos_part.measurable f.to_simple_func.neg_part.measurable (integrable.max_zero f.integrable.neg) }, exact ae_eq }, filter_upwards [ae_eq₁, ae_eq₂], simp only [mem_set_of_eq], assume a h₁ h₂, show _ = _ - _, rw [← h₁, ← h₂], have := f.to_simple_func.pos_part_sub_neg_part, conv_lhs {rw ← this}, refl }, { refine integrable_of_ae_eq (integrable.max_zero f.integrable) ae_eq₁ }, { refine integrable_of_ae_eq (integrable.max_zero f.integrable.neg) ae_eq₂ } end end pos_part end simple_func_integral end simple_func open simple_func variables [normed_space ℝ β] [normed_space ℝ γ] [complete_space β] section integration_in_l1 local notation `to_l1` := coe_to_l1 α β ℝ local attribute [instance] simple_func.normed_group simple_func.normed_space open continuous_linear_map /-- The Bochner integral in l1 space as a continuous linear map. -/ def integral_clm : (α →₁ β) →L[ℝ] β := integral_clm.extend to_l1 simple_func.dense_range simple_func.uniform_inducing /-- The Bochner integral in l1 space -/ def integral (f : α →₁ β) : β := (integral_clm).to_fun f lemma integral_eq (f : α →₁ β) : integral f = (integral_clm).to_fun f := rfl @[elim_cast] lemma simple_func.integral_eq_integral (f : α →₁ₛ β) : integral (f : α →₁ β) = f.integral := by { refine uniformly_extend_of_ind _ _ _ _, exact simple_func.integral_clm.uniform_continuous } variables (α β) @[simp] lemma integral_zero : integral (0 : α →₁ β) = 0 := map_zero integral_clm variables {α β} lemma integral_add (f g : α →₁ β) : integral (f + g) = integral f + integral g := map_add integral_clm f g lemma integral_neg (f : α →₁ β) : integral (-f) = - integral f := map_neg integral_clm f lemma integral_sub (f g : α →₁ β) : integral (f - g) = integral f - integral g := map_sub integral_clm f g lemma integral_smul (r : ℝ) (f : α →₁ β) : integral (r • f) = r • integral f := map_smul r integral_clm f local notation `Integral` := @integral_clm α _ β _ _ _ _ local notation `sIntegral` := @simple_func.integral_clm α _ β _ _ _ lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 := calc ∥Integral∥ ≤ (1 : nnreal) * ∥sIntegral∥ : op_norm_extend_le _ _ _ $ λs, by {rw [nnreal.coe_one, one_mul], refl} ... = ∥sIntegral∥ : one_mul _ ... ≤ 1 : norm_Integral_le_one lemma norm_integral_le (f : α →₁ β) : ∥integral f∥ ≤ ∥f∥ := calc ∥integral f∥ = ∥Integral f∥ : rfl ... ≤ ∥Integral∥ * ∥f∥ : le_op_norm _ _ ... ≤ 1 * ∥f∥ : mul_le_mul_of_nonneg_right norm_Integral_le_one $ norm_nonneg _ ... = ∥f∥ : one_mul _ section pos_part lemma integral_eq_norm_pos_part_sub (f : α →₁ ℝ) : integral f = ∥pos_part f∥ - ∥neg_part f∥ := begin -- Use `is_closed_property` and `is_closed_eq` refine @is_closed_property _ _ _ (coe : (α →₁ₛ ℝ) → (α →₁ ℝ)) (λ f : α →₁ ℝ, integral f = ∥pos_part f∥ - ∥neg_part f∥) l1.simple_func.dense_range (is_closed_eq _ _) _ f, { exact cont _ }, { refine continuous.sub (continuous_norm.comp l1.continuous_pos_part) (continuous_norm.comp l1.continuous_neg_part) }, -- Show that the property holds for all simple functions in the `L¹` space. { assume s, norm_cast, rw [← simple_func.norm_eq, ← simple_func.norm_eq], exact simple_func.integral_eq_norm_pos_part_sub _} end end pos_part end integration_in_l1 end l1 variables [normed_group β] [second_countable_topology β] [normed_space ℝ β] [complete_space β] [normed_group γ] [second_countable_topology γ] [normed_space ℝ γ] [complete_space γ] /-- The Bochner integral -/ def integral (f : α → β) : β := if hf : measurable f ∧ integrable f then (l1.of_fun f hf.1 hf.2).integral else 0 notation `∫` binders `, ` r:(scoped f, integral f) := r section properties open continuous_linear_map measure_theory.simple_func variables {f g : α → β} lemma integral_eq (f : α → β) (h₁ : measurable f) (h₂ : integrable f) : (∫ a, f a) = (l1.of_fun f h₁ h₂).integral := dif_pos ⟨h₁, h₂⟩ lemma integral_undef (h : ¬ (measurable f ∧ integrable f)) : (∫ a, f a) = 0 := dif_neg h lemma integral_non_integrable (h : ¬ integrable f) : (∫ a, f a) = 0 := integral_undef $ not_and_of_not_right _ h lemma integral_non_measurable (h : ¬ measurable f) : (∫ a, f a) = 0 := integral_undef $ not_and_of_not_left _ h variables (α β) @[simp] lemma integral_zero : (∫ a : α, (0:β)) = 0 := by rw [integral_eq, l1.of_fun_zero, l1.integral_zero] variables {α β} lemma integral_add (hfm : measurable f) (hfi : integrable f) (hgm : measurable g) (hgi : integrable g) : (∫ a, f a + g a) = (∫ a, f a) + (∫ a, g a) := by rw [integral_eq, integral_eq f hfm hfi, integral_eq g hgm hgi, l1.of_fun_add, l1.integral_add] lemma integral_neg (f : α → β) : (∫ a, -f a) = - (∫ a, f a) := begin by_cases hf : measurable f ∧ integrable f, { rw [integral_eq f hf.1 hf.2, integral_eq (λa, - f a) hf.1.neg hf.2.neg, l1.of_fun_neg, l1.integral_neg] }, { have hf' : ¬(measurable (λa, -f a) ∧ integrable (λa, -f a)), { rwa [measurable_neg_iff, integrable_neg_iff] }, rw [integral_undef hf, integral_undef hf', neg_zero] } end lemma integral_sub (hfm : measurable f) (hfi : integrable f) (hgm : measurable g) (hgi : integrable g) : (∫ a, f a - g a) = (∫ a, f a) - (∫ a, g a) := by simp only [sub_eq_add_neg, integral_neg, integral_add, measurable_neg_iff, integrable_neg_iff, *] lemma integral_smul (r : ℝ) (f : α → β) : (∫ a, r • (f a)) = r • (∫ a, f a) := begin by_cases hf : measurable f ∧ integrable f, { rw [integral_eq f hf.1 hf.2, integral_eq (λa, r • (f a)), l1.of_fun_smul, l1.integral_smul] }, { by_cases hr : r = 0, { simp only [hr, measure_theory.integral_zero, zero_smul] }, have hf' : ¬(measurable (λa, r • f a) ∧ integrable (λa, r • f a)), { rwa [← measurable_smul_iff hr f, ← integrable_smul_iff hr f] at hf }, rw [integral_undef hf, integral_undef hf', smul_zero] } end lemma integral_mul_left (r : ℝ) (f : α → ℝ) : (∫ a, r * (f a)) = r * (∫ a, f a) := integral_smul r f lemma integral_mul_right (r : ℝ) (f : α → ℝ) : (∫ a, (f a) * r) = (∫ a, f a) * r := by { simp only [mul_comm], exact integral_mul_left r f } lemma integral_div (r : ℝ) (f : α → ℝ) : (∫ a, (f a) / r) = (∫ a, f a) / r := integral_mul_right r⁻¹ f lemma integral_congr_ae (hfm : measurable f) (hgm : measurable g) (h : ∀ₘ a, f a = g a) : (∫ a, f a) = (∫ a, g a) := begin by_cases hfi : integrable f, { have hgi : integrable g := integrable_of_ae_eq hfi h, rw [integral_eq f hfm hfi, integral_eq g hgm hgi, (l1.of_fun_eq_of_fun f g hfm hfi hgm hgi).2 h] }, { have hgi : ¬ integrable g, { rw integrable_congr_ae h at hfi, exact hfi }, rw [integral_non_integrable hfi, integral_non_integrable hgi] }, end lemma norm_integral_le_lintegral_norm (f : α → β) : ∥(∫ a, f a)∥ ≤ ennreal.to_real (∫⁻ a, ennreal.of_real ∥f a∥) := begin by_cases hf : measurable f ∧ integrable f, { rw [integral_eq f hf.1 hf.2, ← l1.norm_of_fun_eq_lintegral_norm f hf.1 hf.2], exact l1.norm_integral_le _ }, { rw [integral_undef hf, _root_.norm_zero], exact to_real_nonneg } end /-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost everywhere convergence of a sequence of functions implies the convergence of their integrals. -/ theorem tendsto_integral_of_dominated_convergence {F : ℕ → α → β} {f : α → β} (bound : α → ℝ) (F_measurable : ∀ n, measurable (F n)) (f_measurable : measurable f) (bound_integrable : integrable bound) (h_bound : ∀ n, ∀ₘ a, ∥F n a∥ ≤ bound a) (h_lim : ∀ₘ a, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫ a, F n a) at_top (𝓝 $ (∫ a, f a)) := begin /- To show `(∫ a, F n a) --> (∫ f)`, suffices to show `∥∫ a, F n a - ∫ f∥ --> 0` -/ rw tendsto_iff_norm_tendsto_zero, /- But `0 ≤ ∥∫ a, F n a - ∫ f∥ = ∥∫ a, (F n a - f a) ∥ ≤ ∫ a, ∥F n a - f a∥, and thus we apply the sandwich theorem and prove that `∫ a, ∥F n a - f a∥ --> 0` -/ have lintegral_norm_tendsto_zero : tendsto (λn, ennreal.to_real $ ∫⁻ a, ennreal.of_real ∥F n a - f a∥) at_top (𝓝 0) := (tendsto_to_real (zero_ne_top)).comp (tendsto_lintegral_norm_of_dominated_convergence F_measurable f_measurable bound_integrable h_bound h_lim), -- Use the sandwich theorem refine squeeze_zero (λ n, norm_nonneg _) _ lintegral_norm_tendsto_zero, -- Show `∥∫ a, F n a - ∫ f∥ ≤ ∫ a, ∥F n a - f a∥` for all `n` { assume n, have h₁ : integrable (F n) := integrable_of_integrable_bound bound_integrable (h_bound _), have h₂ : integrable f := integrable_of_dominated_convergence bound_integrable h_bound h_lim, rw ← integral_sub (F_measurable _) h₁ f_measurable h₂, exact norm_integral_le_lintegral_norm _ } end /-- Lebesgue dominated convergence theorem for filters with a countable basis -/ lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι} {F : ι → α → β} {f : α → β} (bound : α → ℝ) (hl_cb : l.has_countable_basis) (hF_meas : ∀ᶠ n in l, measurable (F n)) (f_measurable : measurable f) (h_bound : ∀ᶠ n in l, ∀ₘ a, ∥F n a∥ ≤ bound a) (bound_integrable : integrable bound) (h_lim : ∀ₘ a, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫ a, F n a) l (𝓝 $ (∫ a, f a)) := begin rw hl_cb.tendsto_iff_seq_tendsto, { intros x xl, have hxl, { rw tendsto_at_top' at xl, exact xl }, have h := inter_mem_sets hF_meas h_bound, replace h := hxl _ h, rcases h with ⟨k, h⟩, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_integral_of_dominated_convergence _ _ _ _ _ _, { exact bound }, { intro, refine (h _ _).1, exact nat.le_add_left _ _ }, { assumption }, { assumption }, { intro, refine (h _ _).2, exact nat.le_add_left _ _ }, { filter_upwards [h_lim], simp only [mem_set_of_eq], assume a h_lim, apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a), { assumption }, rw tendsto_add_at_top_iff_nat, assumption } }, end /-- The Bochner integral of a real-valued function `f : α → ℝ` is the difference between the integral of the positive part of `f` and the integral of the negative part of `f`. -/ lemma integral_eq_lintegral_max_sub_lintegral_min {f : α → ℝ} (hfm : measurable f) (hfi : integrable f) : (∫ a, f a) = ennreal.to_real (∫⁻ a, ennreal.of_real $ max (f a) 0) - ennreal.to_real (∫⁻ a, ennreal.of_real $ - min (f a) 0) := let f₁ : α →₁ ℝ := l1.of_fun f hfm hfi in -- Go to the `L¹` space have eq₁ : ennreal.to_real (∫⁻ a, ennreal.of_real $ max (f a) 0) = ∥l1.pos_part f₁∥ := begin rw l1.norm_eq_norm_to_fun, congr' 1, apply lintegral_congr_ae, filter_upwards [l1.pos_part_to_fun f₁, l1.to_fun_of_fun f hfm hfi], simp only [mem_set_of_eq], assume a h₁ h₂, rw [h₁, h₂, real.norm_eq_abs, abs_of_nonneg], exact le_max_right _ _ end, -- Go to the `L¹` space have eq₂ : ennreal.to_real (∫⁻ a, ennreal.of_real $ -min (f a) 0) = ∥l1.neg_part f₁∥ := begin rw l1.norm_eq_norm_to_fun, congr' 1, apply lintegral_congr_ae, filter_upwards [l1.neg_part_to_fun_eq_min f₁, l1.to_fun_of_fun f hfm hfi], simp only [mem_set_of_eq], assume a h₁ h₂, rw [h₁, h₂, real.norm_eq_abs, abs_of_nonneg], rw [min_eq_neg_max_neg_neg, _root_.neg_neg, neg_zero], exact le_max_right _ _ end, begin rw [eq₁, eq₂, integral, dif_pos], exact l1.integral_eq_norm_pos_part_sub _, { exact ⟨hfm, hfi⟩ } end lemma integral_eq_lintegral_of_nonneg_ae {f : α → ℝ} (hf : ∀ₘ a, 0 ≤ f a) (hfm : measurable f) : (∫ a, f a) = ennreal.to_real (∫⁻ a, ennreal.of_real $ f a) := begin by_cases hfi : integrable f, { rw integral_eq_lintegral_max_sub_lintegral_min hfm hfi, have h_min : (∫⁻ a, ennreal.of_real (-min (f a) 0)) = 0, { rw lintegral_eq_zero_iff, { filter_upwards [hf], simp only [mem_set_of_eq], assume a h, simp only [min_eq_right h, neg_zero, ennreal.of_real_zero] }, { refine measurable_of_real.comp ((measurable.neg measurable_id).comp $ measurable.min hfm measurable_const) } }, have h_max : (∫⁻ a, ennreal.of_real (max (f a) 0)) = (∫⁻ a, ennreal.of_real $ f a), { apply lintegral_congr_ae, filter_upwards [hf], simp only [mem_set_of_eq], assume a h, rw max_eq_left h }, rw [h_min, h_max, zero_to_real, _root_.sub_zero] }, { rw integral_non_integrable hfi, rw [integrable_iff_norm, lt_top_iff_ne_top, ne.def, not_not] at hfi, have : (∫⁻ (a : α), ennreal.of_real (f a)) = (∫⁻ a, ennreal.of_real ∥f a∥), { apply lintegral_congr_ae, filter_upwards [hf], simp only [mem_set_of_eq], assume a h, rw [real.norm_eq_abs, abs_of_nonneg h] }, rw [this, hfi], refl } end lemma integral_nonneg_of_ae {f : α → ℝ} (hf : ∀ₘ a, 0 ≤ f a) : 0 ≤ (∫ a, f a) := begin by_cases hfm : measurable f, { rw integral_eq_lintegral_of_nonneg_ae hf hfm, exact to_real_nonneg }, { rw integral_non_measurable hfm } end lemma integral_nonpos_of_nonpos_ae {f : α → ℝ} (hf : ∀ₘ a, f a ≤ 0) : (∫ a, f a) ≤ 0 := begin have hf : ∀ₘ a, 0 ≤ (-f) a, { filter_upwards [hf], simp only [mem_set_of_eq], assume a h, rwa [pi.neg_apply, neg_nonneg] }, have : 0 ≤ (∫ a, -f a) := integral_nonneg_of_ae hf, rwa [integral_neg, neg_nonneg] at this, end lemma integral_le_integral_ae {f g : α → ℝ} (hfm : measurable f) (hfi : integrable f) (hgm : measurable g) (hgi : integrable g) (h : ∀ₘ a, f a ≤ g a) : (∫ a, f a) ≤ (∫ a, g a) := le_of_sub_nonneg begin rw ← integral_sub hgm hgi hfm hfi, apply integral_nonneg_of_ae, filter_upwards [h], simp only [mem_set_of_eq], assume a, exact sub_nonneg_of_le end lemma integral_le_integral {f g : α → ℝ} (hfm : measurable f) (hfi : integrable f) (hgm : measurable g) (hgi : integrable g) (h : ∀ a, f a ≤ g a) : (∫ a, f a) ≤ (∫ a, g a) := integral_le_integral_ae hfm hfi hgm hgi $ univ_mem_sets' h lemma norm_integral_le_integral_norm (f : α → β) : ∥(∫ a, f a)∥ ≤ ∫ a, ∥f a∥ := have le_ae : ∀ₘ (a : α), 0 ≤ ∥f a∥ := by filter_upwards [] λa, norm_nonneg _, classical.by_cases ( λh : measurable f, calc ∥(∫ a, f a)∥ ≤ ennreal.to_real (∫⁻ a, ennreal.of_real ∥f a∥) : norm_integral_le_lintegral_norm _ ... = ∫ a, ∥f a∥ : (integral_eq_lintegral_of_nonneg_ae le_ae $ measurable.norm h).symm ) ( λh : ¬measurable f, begin rw [integral_non_measurable h, _root_.norm_zero], exact integral_nonneg_of_ae le_ae end ) lemma integral_finset_sum {ι} (s : finset ι) {f : ι → α → β} (hfm : ∀ i, measurable (f i)) (hfi : ∀ i, integrable (f i)) : (∫ a, s.sum (λ i, f i a)) = s.sum (λ i, ∫ a, f i a) := begin refine finset.induction_on s _ _, { simp only [integral_zero, finset.sum_empty] }, { assume i s his ih, simp only [his, finset.sum_insert, not_false_iff], rw [integral_add (hfm _) (hfi _) (measurable_finset_sum s hfm) (integrable_finset_sum s hfm hfi), ih] } end end properties mk_simp_attribute integral_simps "Simp set for integral rules." attribute [integral_simps] integral_neg integral_smul l1.integral_add l1.integral_sub l1.integral_smul l1.integral_neg attribute [irreducible] integral l1.integral end measure_theory
d590a5f187e5fdcf78a8cef8370053d92a5ca653
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/ring_theory/valuation/valuation_subring.lean
847de810756997f2d6ba4b274b3ec62dfeb491a3
[ "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
25,351
lean
/- Copyright (c) 2022 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Junyan Xu, Jack McKoen -/ import ring_theory.valuation.valuation_ring import ring_theory.localization.as_subring import algebraic_geometry.prime_spectrum.basic /-! # Valuation subrings of a field # Projects The order structure on `valuation_subring K`. -/ open_locale classical noncomputable theory variables (K : Type*) [field K] /-- A valuation subring of a field `K` is a subring `A` such that for every `x : K`, either `x ∈ A` or `x⁻¹ ∈ A`. -/ structure valuation_subring extends subring K := (mem_or_inv_mem' : ∀ x : K, x ∈ carrier ∨ x⁻¹ ∈ carrier) namespace valuation_subring variables {K} (A : valuation_subring K) instance : set_like (valuation_subring K) K := { coe := λ A, A.to_subring, coe_injective' := by { rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ _, congr' } } @[simp] lemma mem_carrier (x : K) : x ∈ A.carrier ↔ x ∈ A := iff.refl _ @[simp] lemma mem_to_subring (x : K) : x ∈ A.to_subring ↔ x ∈ A := iff.refl _ @[ext] lemma ext (A B : valuation_subring K) (h : ∀ x, x ∈ A ↔ x ∈ B) : A = B := set_like.ext h lemma zero_mem : (0 : K) ∈ A := A.to_subring.zero_mem lemma one_mem : (1 : K) ∈ A := A.to_subring.one_mem lemma add_mem (x y : K) : x ∈ A → y ∈ A → x + y ∈ A := A.to_subring.add_mem lemma mul_mem (x y : K) : x ∈ A → y ∈ A → x * y ∈ A := A.to_subring.mul_mem lemma neg_mem (x : K) : x ∈ A → (-x) ∈ A := A.to_subring.neg_mem lemma mem_or_inv_mem (x : K) : x ∈ A ∨ x⁻¹ ∈ A := A.mem_or_inv_mem' _ instance : comm_ring A := show comm_ring A.to_subring, by apply_instance instance : is_domain A := show is_domain A.to_subring, by apply_instance instance : has_top (valuation_subring K) := has_top.mk $ { mem_or_inv_mem' := λ x, or.inl trivial, ..(⊤ : subring K) } lemma mem_top (x : K) : x ∈ (⊤ : valuation_subring K) := trivial lemma le_top : A ≤ ⊤ := λ a ha, mem_top _ instance : order_top (valuation_subring K) := { top := ⊤, le_top := le_top } instance : inhabited (valuation_subring K) := ⟨⊤⟩ instance : valuation_ring A := { cond := λ a b, begin by_cases (b : K) = 0, { use 0, left, ext, simp [h] }, by_cases (a : K) = 0, { use 0, right, ext, simp [h] }, cases A.mem_or_inv_mem (a/b) with hh hh, { use ⟨a/b, hh⟩, right, ext, field_simp, ring }, { rw (show (a/b : K)⁻¹ = b/a, by field_simp) at hh, use ⟨b/a, hh⟩, left, ext, field_simp, ring }, end } instance : algebra A K := show algebra A.to_subring K, by apply_instance @[simp] lemma algebra_map_apply (a : A) : algebra_map A K a = a := rfl instance : is_fraction_ring A K := { map_units := λ ⟨y, hy⟩, (units.mk0 (y : K) (λ c, non_zero_divisors.ne_zero hy $ subtype.ext c)).is_unit, surj := λ z, begin by_cases z = 0, { use (0, 1), simp [h] }, cases A.mem_or_inv_mem z with hh hh, { use (⟨z, hh⟩, 1), simp }, { refine ⟨⟨1, ⟨⟨_, hh⟩, _⟩⟩, mul_inv_cancel h⟩, exact mem_non_zero_divisors_iff_ne_zero.2 (λ c, h (inv_eq_zero.mp (congr_arg coe c))) }, end, eq_iff_exists := λ a b, ⟨ λ h, ⟨1, by { ext, simpa using h }⟩, λ ⟨c, h⟩, congr_arg coe ((mul_eq_mul_right_iff.1 h).resolve_right (non_zero_divisors.ne_zero c.2)) ⟩ } /-- The value group of the valuation associated to `A`. -/ @[derive linear_ordered_comm_group_with_zero] def value_group := valuation_ring.value_group A K /-- Any valuation subring of `K` induces a natural valuation on `K`. -/ def valuation : valuation K A.value_group := valuation_ring.valuation A K instance inhabited_value_group : inhabited A.value_group := ⟨A.valuation 0⟩ lemma valuation_le_one (a : A) : A.valuation a ≤ 1 := (valuation_ring.mem_integer_iff A K _).2 ⟨a, rfl⟩ lemma mem_of_valuation_le_one (x : K) (h : A.valuation x ≤ 1) : x ∈ A := let ⟨a, ha⟩ := (valuation_ring.mem_integer_iff A K x).1 h in ha ▸ a.2 lemma valuation_le_one_iff (x : K) : A.valuation x ≤ 1 ↔ x ∈ A := ⟨mem_of_valuation_le_one _ _, λ ha, A.valuation_le_one ⟨x, ha⟩⟩ lemma valuation_eq_iff (x y : K) : A.valuation x = A.valuation y ↔ ∃ a : Aˣ, (a : K) * y = x := quotient.eq' lemma valuation_le_iff (x y : K) : A.valuation x ≤ A.valuation y ↔ ∃ a : A, (a : K) * y = x := iff.rfl lemma valuation_surjective : function.surjective A.valuation := surjective_quot_mk _ lemma valuation_unit (a : Aˣ) : A.valuation a = 1 := by { rw [← A.valuation.map_one, valuation_eq_iff], use a, simp } lemma valuation_eq_one_iff (a : A) : is_unit a ↔ A.valuation a = 1 := ⟨ λ h, A.valuation_unit h.unit, λ h, begin have ha : (a : K) ≠ 0, { intro c, rw [c, A.valuation.map_zero] at h, exact zero_ne_one h }, have ha' : (a : K)⁻¹ ∈ A, { rw [← valuation_le_one_iff, map_inv₀, h, inv_one] }, apply is_unit_of_mul_eq_one a ⟨a⁻¹, ha'⟩, ext, field_simp, end ⟩ lemma valuation_lt_one_or_eq_one (a : A) : A.valuation a < 1 ∨ A.valuation a = 1 := lt_or_eq_of_le (A.valuation_le_one a) lemma valuation_lt_one_iff (a : A) : a ∈ local_ring.maximal_ideal A ↔ A.valuation a < 1 := begin rw local_ring.mem_maximal_ideal, dsimp [nonunits], rw valuation_eq_one_iff, exact (A.valuation_le_one a).lt_iff_ne.symm, end /-- A subring `R` of `K` such that for all `x : K` either `x ∈ R` or `x⁻¹ ∈ R` is a valuation subring of `K`. -/ def of_subring (R : subring K) (hR : ∀ x : K, x ∈ R ∨ x⁻¹ ∈ R) : valuation_subring K := { mem_or_inv_mem' := hR, ..R } @[simp] lemma mem_of_subring (R : subring K) (hR : ∀ x : K, x ∈ R ∨ x⁻¹ ∈ R) (x : K) : x ∈ of_subring R hR ↔ x ∈ R := iff.refl _ /-- An overring of a valuation ring is a valuation ring. -/ def of_le (R : valuation_subring K) (S : subring K) (h : R.to_subring ≤ S) : valuation_subring K := { mem_or_inv_mem' := λ x, (R.mem_or_inv_mem x).imp (@h x) (@h _), ..S} section order instance : semilattice_sup (valuation_subring K) := { sup := λ R S, of_le R (R.to_subring ⊔ S.to_subring) $ le_sup_left, le_sup_left := λ R S x hx, (le_sup_left : R.to_subring ≤ R.to_subring ⊔ S.to_subring) hx, le_sup_right := λ R S x hx, (le_sup_right : S.to_subring ≤ R.to_subring ⊔ S.to_subring) hx, sup_le := λ R S T hR hT x hx, (sup_le hR hT : R.to_subring ⊔ S.to_subring ≤ T.to_subring) hx, ..(infer_instance : partial_order (valuation_subring K)) } /-- The ring homomorphism induced by the partial order. -/ def inclusion (R S : valuation_subring K) (h : R ≤ S) : R →+* S := subring.inclusion h /-- The canonical ring homomorphism from a valuation ring to its field of fractions. -/ def subtype (R : valuation_subring K) : R →+* K := subring.subtype R.to_subring /-- The canonical map on value groups induced by a coarsening of valuation rings. -/ def map_of_le (R S : valuation_subring K) (h : R ≤ S) : R.value_group →*₀ S.value_group := { to_fun := quotient.map' id $ λ x y ⟨u, hu⟩, ⟨units.map (R.inclusion S h).to_monoid_hom u, hu⟩, map_zero' := rfl, map_one' := rfl, map_mul' := by { rintro ⟨⟩ ⟨⟩, refl } } @[mono] lemma monotone_map_of_le (R S : valuation_subring K) (h : R ≤ S) : monotone (R.map_of_le S h) := by { rintros ⟨⟩ ⟨⟩ ⟨a, ha⟩, exact ⟨R.inclusion S h a, ha⟩ } @[simp] lemma map_of_le_comp_valuation (R S : valuation_subring K) (h : R ≤ S) : R.map_of_le S h ∘ R.valuation = S.valuation := by { ext, refl } @[simp] lemma map_of_le_valuation_apply (R S : valuation_subring K) (h : R ≤ S) (x : K) : R.map_of_le S h (R.valuation x) = S.valuation x := rfl /-- The ideal corresponding to a coarsening of a valuation ring. -/ def ideal_of_le (R S : valuation_subring K) (h : R ≤ S) : ideal R := (local_ring.maximal_ideal S).comap (R.inclusion S h) instance prime_ideal_of_le (R S : valuation_subring K) (h : R ≤ S) : (ideal_of_le R S h).is_prime := (local_ring.maximal_ideal S).comap_is_prime _ /-- The coarsening of a valuation ring associated to a prime ideal. -/ def of_prime (A : valuation_subring K) (P : ideal A) [P.is_prime] : valuation_subring K := of_le A (localization.subalgebra.of_field K P.prime_compl $ le_non_zero_divisors_of_no_zero_divisors $ not_not_intro P.zero_mem).to_subring $ λ a ha, subalgebra.algebra_map_mem _ (⟨a, ha⟩ : A) instance of_prime_algebra (A : valuation_subring K) (P : ideal A) [P.is_prime] : algebra A (A.of_prime P) := subalgebra.algebra _ instance of_prime_scalar_tower (A : valuation_subring K) (P : ideal A) [P.is_prime] : is_scalar_tower A (A.of_prime P) K := is_scalar_tower.subalgebra' A K K _ instance of_prime_localization (A : valuation_subring K) (P : ideal A) [P.is_prime] : is_localization.at_prime (A.of_prime P) P := by apply localization.subalgebra.is_localization_of_field K lemma le_of_prime (A : valuation_subring K) (P : ideal A) [P.is_prime] : A ≤ of_prime A P := λ a ha, subalgebra.algebra_map_mem _ (⟨a, ha⟩ : A) lemma of_prime_valuation_eq_one_iff_mem_prime_compl (A : valuation_subring K) (P : ideal A) [P.is_prime] (x : A) : (of_prime A P).valuation x = 1 ↔ x ∈ P.prime_compl := begin rw [← is_localization.at_prime.is_unit_to_map_iff (A.of_prime P) P x, valuation_eq_one_iff], refl, end @[simp] lemma ideal_of_le_of_prime (A : valuation_subring K) (P : ideal A) [P.is_prime] : ideal_of_le A (of_prime A P) (le_of_prime A P) = P := by { ext, apply is_localization.at_prime.to_map_mem_maximal_iff } @[simp] lemma of_prime_ideal_of_le (R S : valuation_subring K) (h : R ≤ S) : of_prime R (ideal_of_le R S h) = S := begin ext x, split, { rintro ⟨a, r, hr, rfl⟩, apply mul_mem, { exact h a.2 }, { rw [← valuation_le_one_iff, map_inv₀, ← inv_one, inv_le_inv₀], { exact not_lt.1 ((not_iff_not.2 $ valuation_lt_one_iff S _).1 hr) }, { intro hh, erw [valuation.zero_iff, subring.coe_eq_zero_iff] at hh, apply hr, rw hh, apply ideal.zero_mem (R.ideal_of_le S h) }, { exact one_ne_zero } } }, { intro hx, by_cases hr : x ∈ R, { exact R.le_of_prime _ hr }, have : x ≠ 0 := λ h, hr (by { rw h, exact R.zero_mem }), replace hr := (R.mem_or_inv_mem x).resolve_left hr, { use [1, x⁻¹, hr], split, { change (⟨x⁻¹, h hr⟩ : S) ∉ nonunits S, erw [mem_nonunits_iff, not_not], apply is_unit_of_mul_eq_one _ (⟨x, hx⟩ : S), ext, field_simp }, { field_simp } } }, end lemma of_prime_le_of_le (P Q : ideal A) [P.is_prime] [Q.is_prime] (h : P ≤ Q) : of_prime A Q ≤ of_prime A P := λ x ⟨a, s, hs, he⟩, ⟨a, s, λ c, hs (h c), he⟩ lemma ideal_of_le_le_of_le (R S : valuation_subring K) (hR : A ≤ R) (hS : A ≤ S) (h : R ≤ S) : ideal_of_le A S hS ≤ ideal_of_le A R hR := λ x hx, (valuation_lt_one_iff R _).2 begin by_contra c, push_neg at c, replace c := monotone_map_of_le R S h c, rw [(map_of_le _ _ _).map_one, map_of_le_valuation_apply] at c, apply not_le_of_lt ((valuation_lt_one_iff S _).1 hx) c, end /-- The equivalence between coarsenings of a valuation ring and its prime ideals.-/ @[simps] def prime_spectrum_equiv : prime_spectrum A ≃ { S | A ≤ S } := { to_fun := λ P, ⟨of_prime A P.as_ideal, le_of_prime _ _⟩, inv_fun := λ S, ⟨ideal_of_le _ S S.2, infer_instance⟩, left_inv := λ P, by { ext1, simpa }, right_inv := λ S, by { ext1, simp } } /-- An ordered variant of `prime_spectrum_equiv`. -/ @[simps] def prime_spectrum_order_equiv : (prime_spectrum A)ᵒᵈ ≃o {S | A ≤ S} := { map_rel_iff' := λ P Q, ⟨ λ h, begin have := ideal_of_le_le_of_le A _ _ _ _ h, iterate 2 { erw ideal_of_le_of_prime at this }, exact this, end, λ h, by { apply of_prime_le_of_le, exact h } ⟩, ..(prime_spectrum_equiv A) } instance linear_order_overring : linear_order { S | A ≤ S } := { le_total := let i : is_total (prime_spectrum A) (≤) := (subtype.rel_embedding _ _).is_total in by exactI (prime_spectrum_order_equiv A).symm.to_rel_embedding.is_total.total, decidable_le := infer_instance, ..(infer_instance : partial_order _) } end order end valuation_subring namespace valuation variables {K} {Γ Γ₁ Γ₂ : Type*} [linear_ordered_comm_group_with_zero Γ] [linear_ordered_comm_group_with_zero Γ₁] [linear_ordered_comm_group_with_zero Γ₂] (v : valuation K Γ) (v₁ : valuation K Γ₁) (v₂ : valuation K Γ₂) /-- The valuation subring associated to a valuation. -/ def valuation_subring : valuation_subring K := { mem_or_inv_mem' := begin intros x, cases le_or_lt (v x) 1, { left, exact h }, { right, change v x⁻¹ ≤ 1, rw [map_inv₀ v, ← inv_one, inv_le_inv₀], { exact le_of_lt h }, { intro c, simpa [c] using h }, { exact one_ne_zero } } end, .. v.integer } @[simp] lemma mem_valuation_subring_iff (x : K) : x ∈ v.valuation_subring ↔ v x ≤ 1 := iff.refl _ lemma is_equiv_iff_valuation_subring : v₁.is_equiv v₂ ↔ v₁.valuation_subring = v₂.valuation_subring := begin split, { intros h, ext x, specialize h x 1, simpa using h }, { intros h, apply is_equiv_of_val_le_one, intros x, have : x ∈ v₁.valuation_subring ↔ x ∈ v₂.valuation_subring, by rw h, simpa using this } end lemma is_equiv_valuation_valuation_subring : v.is_equiv v.valuation_subring.valuation := begin rw [is_equiv_iff_val_le_one], intro x, rw valuation_subring.valuation_le_one_iff, refl, end end valuation namespace valuation_subring variables {K} (A : valuation_subring K) @[simp] lemma valuation_subring_valuation : A.valuation.valuation_subring = A := by { ext, rw ← A.valuation_le_one_iff, refl } section unit_group /-- The unit group of a valuation subring, as a subgroup of `Kˣ`. -/ def unit_group : subgroup Kˣ := (A.valuation.to_monoid_with_zero_hom.to_monoid_hom.comp (units.coe_hom K)).ker lemma mem_unit_group_iff (x : Kˣ) : x ∈ A.unit_group ↔ A.valuation x = 1 := iff.rfl /-- For a valuation subring `A`, `A.unit_group` agrees with the units of `A`. -/ def unit_group_mul_equiv : A.unit_group ≃* Aˣ := { to_fun := λ x, { val := ⟨x, mem_of_valuation_le_one A _ x.prop.le⟩, inv := ⟨↑(x⁻¹), mem_of_valuation_le_one _ _ (x⁻¹).prop.le⟩, val_inv := subtype.ext (units.mul_inv x), inv_val := subtype.ext (units.inv_mul x) }, inv_fun := λ x, ⟨units.map A.subtype.to_monoid_hom x, A.valuation_unit x⟩, left_inv := λ a, by { ext, refl }, right_inv := λ a, by { ext, refl }, map_mul' := λ a b, by { ext, refl } } @[simp] lemma coe_unit_group_mul_equiv_apply (a : A.unit_group) : (A.unit_group_mul_equiv a : K) = a := rfl @[simp] lemma coe_unit_group_mul_equiv_symm_apply (a : Aˣ) : (A.unit_group_mul_equiv.symm a : K) = a := rfl lemma unit_group_le_unit_group {A B : valuation_subring K} : A.unit_group ≤ B.unit_group ↔ A ≤ B := begin split, { intros h x hx, rw [← A.valuation_le_one_iff x, le_iff_lt_or_eq] at hx, by_cases h_1 : x = 0, { simp only [h_1, zero_mem] }, by_cases h_2 : 1 + x = 0, { simp only [← add_eq_zero_iff_neg_eq.1 h_2, neg_mem _ _ (one_mem _)] }, cases hx, { have := h (show (units.mk0 _ h_2) ∈ A.unit_group, from A.valuation.map_one_add_of_lt hx), simpa using B.add_mem _ _ (show 1 + x ∈ B, from set_like.coe_mem ((B.unit_group_mul_equiv ⟨_, this⟩) : B)) (B.neg_mem _ B.one_mem) }, { have := h (show (units.mk0 x h_1) ∈ A.unit_group, from hx), refine set_like.coe_mem ((B.unit_group_mul_equiv ⟨_, this⟩) : B) } }, { rintros h x (hx : A.valuation x = 1), apply_fun A.map_of_le B h at hx, simpa using hx } end lemma unit_group_injective : function.injective (unit_group : valuation_subring K → subgroup _) := λ A B h, by { simpa only [le_antisymm_iff, unit_group_le_unit_group] using h} lemma eq_iff_unit_group {A B : valuation_subring K} : A = B ↔ A.unit_group = B.unit_group := unit_group_injective.eq_iff.symm /-- The map on valuation subrings to their unit groups is an order embedding. -/ def unit_group_order_embedding : valuation_subring K ↪o subgroup Kˣ := { to_fun := λ A, A.unit_group, inj' := unit_group_injective, map_rel_iff' := λ A B, unit_group_le_unit_group } lemma unit_group_strict_mono : strict_mono (unit_group : valuation_subring K → subgroup _) := unit_group_order_embedding.strict_mono end unit_group section nonunits /-- The nonunits of a valuation subring of `K`, as a subsemigroup of `K`-/ def nonunits : subsemigroup K := { carrier := { x | A.valuation x < 1 }, mul_mem' := λ a b ha hb, (mul_lt_mul₀ ha hb).trans_eq $ mul_one _ } lemma mem_nonunits_iff {x : K} : x ∈ A.nonunits ↔ A.valuation x < 1 := iff.rfl lemma nonunits_le_nonunits {A B : valuation_subring K} : B.nonunits ≤ A.nonunits ↔ A ≤ B := begin split, { intros h x hx, by_cases h_1 : x = 0, { simp only [h_1, zero_mem] }, rw [← valuation_le_one_iff, ← not_lt, valuation.one_lt_val_iff _ h_1] at hx ⊢, by_contra h_2, from hx (h h_2) }, { intros h x hx, by_contra h_1, from not_lt.2 (monotone_map_of_le _ _ h (not_lt.1 h_1)) hx } end lemma nonunits_injective : function.injective (nonunits : valuation_subring K → subsemigroup _) := λ A B h, by { simpa only [le_antisymm_iff, nonunits_le_nonunits] using h.symm } lemma nonunits_inj {A B : valuation_subring K} : A.nonunits = B.nonunits ↔ A = B := nonunits_injective.eq_iff /-- The map on valuation subrings to their nonunits is a dual order embedding. -/ def nonunits_order_embedding : valuation_subring K ↪o (subsemigroup K)ᵒᵈ := { to_fun := λ A, A.nonunits, inj' := nonunits_injective, map_rel_iff' := λ A B, nonunits_le_nonunits } variables {A} /-- The elements of `A.nonunits` are those of the maximal ideal of `A` after coercion to `K`. See also `mem_nonunits_iff_exists_mem_maximal_ideal`, which gets rid of the coercion to `K`, at the expense of a more complicated right hand side. -/ theorem coe_mem_nonunits_iff {a : A} : (a : K) ∈ A.nonunits ↔ a ∈ local_ring.maximal_ideal A := (valuation_lt_one_iff _ _).symm lemma nonunits_le : A.nonunits ≤ A.to_subring.to_submonoid.to_subsemigroup := λ a ha, (A.valuation_le_one_iff _).mp (A.mem_nonunits_iff.mp ha).le lemma nonunits_subset : (A.nonunits : set K) ⊆ A := nonunits_le /-- The elements of `A.nonunits` are those of the maximal ideal of `A`. See also `coe_mem_nonunits_iff`, which has a simpler right hand side but requires the element to be in `A` already. -/ theorem mem_nonunits_iff_exists_mem_maximal_ideal {a : K} : a ∈ A.nonunits ↔ ∃ ha, (⟨a, ha⟩ : A) ∈ local_ring.maximal_ideal A := ⟨λ h, ⟨nonunits_subset h, coe_mem_nonunits_iff.mp h⟩, λ ⟨ha, h⟩, coe_mem_nonunits_iff.mpr h⟩ /-- `A.nonunits` agrees with the maximal ideal of `A`, after taking its image in `K`. -/ theorem image_maximal_ideal : (coe : A → K) '' local_ring.maximal_ideal A = A.nonunits := begin ext a, simp only [set.mem_image, set_like.mem_coe, mem_nonunits_iff_exists_mem_maximal_ideal], erw subtype.exists, simp_rw [subtype.coe_mk, exists_and_distrib_right, exists_eq_right], end end nonunits section principal_unit_group /-- The principal unit group of a valuation subring, as a subgroup of `Kˣ`. -/ def principal_unit_group : subgroup Kˣ := { carrier := { x | A.valuation (x - 1) < 1 }, mul_mem' := begin intros a b ha hb, refine lt_of_le_of_lt _ (max_lt hb ha), rw [← one_mul (A.valuation (b - 1)), ← A.valuation.map_one_add_of_lt ha, add_sub_cancel'_right, ← valuation.map_mul, mul_sub_one, ← sub_add_sub_cancel], exact A.valuation.map_add _ _, end, one_mem' := by simpa using zero_lt_one₀, inv_mem' := begin dsimp, intros a ha, conv {to_lhs, rw [← mul_one (A.valuation _), ← A.valuation.map_one_add_of_lt ha]}, rwa [add_sub_cancel'_right, ← valuation.map_mul, sub_mul, units.inv_mul, ← neg_sub, one_mul, valuation.map_neg], end } lemma principal_units_le_units : A.principal_unit_group ≤ A.unit_group := λ a h, by simpa only [add_sub_cancel'_right] using A.valuation.map_one_add_of_lt h lemma mem_principal_unit_group_iff (x : Kˣ) : x ∈ A.principal_unit_group ↔ A.valuation ((x : K) - 1) < 1 := iff.rfl lemma principal_unit_group_le_principal_unit_group {A B : valuation_subring K} : B.principal_unit_group ≤ A.principal_unit_group ↔ A ≤ B := begin split, { intros h x hx, by_cases h_1 : x = 0, { simp only [h_1, zero_mem] }, by_cases h_2 : x⁻¹ + 1 = 0, { rw [add_eq_zero_iff_eq_neg, inv_eq_iff_inv_eq, inv_neg, inv_one] at h_2, simpa only [h_2] using B.neg_mem _ B.one_mem }, { rw [← valuation_le_one_iff, ← not_lt, valuation.one_lt_val_iff _ h_1, ← add_sub_cancel x⁻¹, ← units.coe_mk0 h_2, ← mem_principal_unit_group_iff] at hx ⊢, simpa only [hx] using @h (units.mk0 (x⁻¹ + 1) h_2) } }, { intros h x hx, by_contra h_1, from not_lt.2 (monotone_map_of_le _ _ h (not_lt.1 h_1)) hx } end lemma principal_unit_group_injective : function.injective (principal_unit_group : valuation_subring K → subgroup _) := λ A B h, by { simpa [le_antisymm_iff, principal_unit_group_le_principal_unit_group] using h.symm } lemma eq_iff_principal_unit_group {A B : valuation_subring K} : A = B ↔ A.principal_unit_group = B.principal_unit_group := principal_unit_group_injective.eq_iff.symm /-- The map on valuation subrings to their principal unit groups is an order embedding. -/ def principal_unit_group_order_embedding : valuation_subring K ↪o (subgroup Kˣ)ᵒᵈ := { to_fun := λ A, A.principal_unit_group, inj' := principal_unit_group_injective, map_rel_iff' := λ A B, principal_unit_group_le_principal_unit_group } lemma coe_mem_principal_unit_group_iff {x : A.unit_group} : (x : Kˣ) ∈ A.principal_unit_group ↔ A.unit_group_mul_equiv x ∈ (units.map (local_ring.residue A).to_monoid_hom).ker := begin rw [monoid_hom.mem_ker, units.ext_iff], dsimp, let π := ideal.quotient.mk (local_ring.maximal_ideal A), change _ ↔ π _ = _, rw [← π.map_one, ← sub_eq_zero, ← π.map_sub, ideal.quotient.eq_zero_iff_mem, valuation_lt_one_iff], simpa, end /-- The principal unit group agrees with the kernel of the canonical map from the units of `A` to the units of the residue field of `A`. -/ def principal_unit_group_equiv : A.principal_unit_group ≃* (units.map (local_ring.residue A).to_monoid_hom).ker := { to_fun := λ x, ⟨A.unit_group_mul_equiv ⟨_, A.principal_units_le_units x.2⟩, A.coe_mem_principal_unit_group_iff.1 x.2⟩, inv_fun := λ x, ⟨A.unit_group_mul_equiv.symm x, by { rw A.coe_mem_principal_unit_group_iff, simpa using set_like.coe_mem x }⟩, left_inv := λ x, by simp, right_inv := λ x, by simp, map_mul' := λ x y, by refl, } @[simp] lemma principal_unit_group_equiv_apply (a : A.principal_unit_group) : (principal_unit_group_equiv A a : K) = a := rfl @[simp] lemma principal_unit_group_symm_apply (a : (units.map (local_ring.residue A).to_monoid_hom).ker) : (A.principal_unit_group_equiv.symm a : K) = a := rfl /-- The canonical map from the unit group of `A` to the units of the residue field of `A`. -/ def unit_group_to_residue_field_units : A.unit_group →* (local_ring.residue_field A)ˣ := monoid_hom.comp (units.map $ (ideal.quotient.mk _).to_monoid_hom) A.unit_group_mul_equiv.to_monoid_hom @[simp] lemma coe_unit_group_to_residue_field_units_apply (x : A.unit_group) : (A.unit_group_to_residue_field_units x : (local_ring.residue_field A) ) = (ideal.quotient.mk _ (A.unit_group_mul_equiv x : A)) := rfl lemma ker_unit_group_to_residue_field_units : A.unit_group_to_residue_field_units.ker = A.principal_unit_group.comap A.unit_group.subtype := by { ext, simpa only [subgroup.mem_comap, subgroup.coe_subtype, coe_mem_principal_unit_group_iff] } lemma surjective_unit_group_to_residue_field_units : function.surjective A.unit_group_to_residue_field_units := (local_ring.surjective_units_map_of_local_ring_hom _ ideal.quotient.mk_surjective local_ring.is_local_ring_hom_residue).comp (mul_equiv.surjective _) /-- The quotient of the unit group of `A` by the principal unit group of `A` agrees with the units of the residue field of `A`. -/ def units_mod_principal_units_equiv_residue_field_units : (A.unit_group ⧸ (A.principal_unit_group.comap A.unit_group.subtype)) ≃* (local_ring.residue_field A)ˣ := mul_equiv.trans (quotient_group.equiv_quotient_of_eq A.ker_unit_group_to_residue_field_units.symm) (quotient_group.quotient_ker_equiv_of_surjective _ A.surjective_unit_group_to_residue_field_units) @[simp] lemma units_mod_principal_units_equiv_residue_field_units_comp_quotient_group_mk : A.units_mod_principal_units_equiv_residue_field_units.to_monoid_hom.comp (quotient_group.mk' _) = A.unit_group_to_residue_field_units := rfl @[simp] lemma units_mod_principal_units_equiv_residue_field_units_comp_quotient_group_mk_apply (x : A.unit_group) : A.units_mod_principal_units_equiv_residue_field_units.to_monoid_hom (quotient_group.mk x) = A.unit_group_to_residue_field_units x := rfl end principal_unit_group end valuation_subring
e3226cff402aa47ee29423a07663107d657b52f5
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/src/sieve.lean
ac6a3501832cdaddc83d79ab7e88ee19d72da8cf
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
7,866
lean
/- Author: E.W.Ayers -/ import category_theory.comma import category_theory.limits.shapes.finite_limits import category_theory.yoneda import order.complete_lattice import data.set.lattice import .comma universes u v namespace category_theory /-- A sieve on X is a set of morphisms to X that is closed under left composition. -/ structure sieve {C : Type u} [𝒞 : category.{v} C] (X : C) := (arrows : set (over X)) (subs : ∀ (f : over X) (_ : f ∈ arrows) (Z : C) (g : Z ⟶ f.left), (over.mk (g ≫ f.hom)) ∈ arrows) namespace sieve variables {C : Type u} variables [𝒞 : category.{v} C] include 𝒞 variables {X Y Z : C} {S R : sieve X} instance mem_hom : has_mem (Y ⟶ X) (sieve X) := ⟨λ f S, over.mk f ∈ S.arrows⟩ instance mem_over : has_mem (over X) (sieve X) := ⟨λ f S, f ∈ S.arrows⟩ instance : has_subset (sieve X) := ⟨λ S R, S.arrows ⊆ R.arrows⟩ @[ext] def extensionality : Π {R S : sieve X}, R.arrows = S.arrows → R = S |⟨Ra,_⟩ ⟨Sa, _⟩ rfl := rfl open lattice protected def Sup (𝒮 : set (sieve X)) : (sieve X) := { arrows := ⋃ (S : {S : sieve X // S ∈ 𝒮}), sieve.arrows S.1 , subs := begin rintros f ⟨R,⟨S,e⟩,h₁⟩ Z g, refine ⟨R,⟨S,e⟩,_ ⟩, cases e, apply sieve.subs, assumption end } protected def Inf (𝒮 : set (sieve X)) : (sieve X) := { arrows := ⋂ (S : {S // S ∈ 𝒮}), sieve.arrows S.1, subs := begin rintros f h₁ Z g R h₂, have h₃, apply h₁, apply h₂, rcases h₂ with ⟨SS,rfl⟩, apply sieve.subs, assumption, end } protected def union (S R : sieve X) : sieve X := { arrows := S.arrows ∪ R.arrows, subs := begin rintros ⟨Z,f⟩ c Y g,cases c, left, apply sieve.subs, assumption, right, apply sieve.subs, assumption end } protected def inter (S R : sieve X) : sieve X := { arrows := S.arrows ∩ R.arrows, subs := begin rintros f ⟨fS,fR⟩ Z g, split, apply sieve.subs, assumption, apply sieve.subs, assumption, end } instance : complete_lattice (sieve X) := { le := λ S R, S ⊆ R, le_refl := λ S, set.subset.refl _, le_trans := λ S R T, set.subset.trans, le_antisymm := begin intros S R p q, apply sieve.extensionality, apply set.subset.antisymm; assumption end, top := { arrows := set.univ, subs := λ a aa Z g, ⟨⟩ }, bot := { arrows := ∅, subs := λ a aa Z g, false.rec_on _ aa }, sup := sieve.union, inf := sieve.inter, Sup := sieve.Sup, Inf := sieve.Inf, le_Sup := begin intros 𝒮 S SS f fS, refine ⟨_,⟨⟨_,SS⟩,rfl⟩,fS⟩ end, Sup_le := begin rintros 𝒮 S h f ⟨F,⟨⟨R,hR⟩,rfl⟩,fF⟩, apply h _ hR fF end, Inf_le := begin intros 𝒮 S hS f hf, apply hf, refine ⟨⟨_,hS⟩,rfl⟩ end, le_Inf := begin rintros 𝒮 S h f hf fs ⟨⟨R,hR⟩,rfl⟩, apply h _ hR hf end, le_sup_left := begin intros _ _ _ _, apply set.subset_union_left, assumption end, le_sup_right := begin intros _ _ _ _, apply set.subset_union_right, assumption end, sup_le := begin intros _ _ _ _ _, apply set.union_subset; assumption end, inf_le_left := begin intros _ _ _ _, apply set.inter_subset_left, assumption end, inf_le_right := begin intros _ _ _ _, apply set.inter_subset_right, assumption end, le_inf := begin intros _ _ _ _ _, apply set.subset_inter; assumption end, le_top := begin intros _ _ _, trivial end, bot_le := begin intros _ _ h, exfalso, apply h end } instance : preorder (sieve X) := by apply_instance instance : partial_order (sieve X) := by apply_instance inductive generate_sets (𝒢 : set (over X)) : over X → Prop |basic : Π {f : over X}, f ∈ 𝒢 → generate_sets f |subs : Π {f : over X} {Y} (g : Y ⟶ f.1), generate_sets f → generate_sets (over.mk $ g ≫ f.hom) def generate (𝒢 : set (over X)) : sieve X := { arrows := generate_sets 𝒢, subs := λ f h Z g, generate_sets.subs _ h } open order lattice lemma sets_iff_generate {𝒢 : set (over X)}: generate 𝒢 ≤ S ↔ 𝒢 ⊆ S.arrows := iff.intro (λ H _ H2, H $ generate_sets.basic H2 ) (λ ss g f, begin induction f, apply ss f_a, apply sieve.subs, apply f_ih end) /-- Show that there is a galois insertion (generate, .arrows). -/ def gi_generate : @galois_insertion (set (over X)) (sieve X) (by apply_instance) _ generate sieve.arrows := { gc := λ s f, sets_iff_generate, choice := λ 𝒢 f, generate 𝒢, choice_eq := λ 𝒢 h, rfl, le_l_u := λ S _, generate_sets.basic } /-- Given a morhpism `h : Y ⟶ X`, send a sieve S on X to a sieve on Y as the inverse image of S with `_ ≫ h`. That is, `sieve.pullback S h := (≫ h) '⁻¹ S`. -/ def pullback (S : sieve X) (h : Y ⟶ X) : sieve Y := { arrows := {sl | (over.mk $ sl.hom ≫ h) ∈ S }, subs := begin intros, suffices : over.mk ((g ≫ f.hom) ≫ h) ∈ S, by assumption, let j := over.mk (f.hom ≫ h), have jS : j ∈ S, by assumption, suffices : over.mk (g ≫ j.hom) ∈ S, by simpa, apply sieve.subs S j jS, end } def comp (R : sieve Y) (f : Y ⟶ X) : sieve X := { arrows := λ gf, ∃ (g : gf.1 ⟶ Y) (_ : over.mk g ∈ R), gf.hom = g ≫ f , subs := begin rintros ⟨Z,g⟩ ⟨j,ir,e⟩ W h, refine ⟨h ≫ j,_,_⟩, refine sieve.subs R _ ir _ _, simp, simp at e, rw e end } def comps (R : Π (f : over X), sieve f.left) (S : sieve X) : sieve X := ⨆ (f ∈ S), comp (R f) f.hom @[simp] lemma pullback_def (h : Y ⟶ X) {f : Z ⟶ Y} : ((over.mk f) ∈ (pullback S h)) = ((over.mk $ f ≫ h) ∈ S) := rfl @[simp] lemma pullback_def2 (h : Y ⟶ X) {f : over Y} : (f ∈ (pullback S h)) = ((over.mk $ f.hom ≫ h) ∈ S) := rfl lemma pullback_le_map {S R : sieve X} (Hss : S ≤ R) (f : Y ⟶ X) : pullback S f ≤ pullback R f := begin rintros ⟨Z,g⟩ H, apply Hss, apply H end lemma pullback_top {f : Y ⟶ X} : pullback ⊤ f = ⊤ := begin apply top_unique, rintros g Hg, trivial end lemma le_pullback_comp {R : sieve Y} {f : Y ⟶ X} : R ≤ pullback (comp R f) f := begin rintros g b, refine ⟨_,_,rfl⟩, simp, assumption end lemma top_of_has_id : over.mk (𝟙 X) ∈ S → S = ⊤ := begin intro h, apply top_unique, rintros f ⟨⟩, suffices : over.mk (f.hom ≫ (𝟙 _)) ∈ S, simp at this, exact this, refine @sieve.subs _ _ _ S (over.mk (𝟙 _)) _ _ _, apply h, end lemma comp_le_comps (R : Π (f : over X), sieve f.1) (S : sieve X) (f : over X) (H : f ∈ S) : comp (R f) f.hom ≤ comps R S := calc comp (R f) f.hom ≤ ⨆ (_ : f ∈ S), comp (R f) f.hom : lattice.le_supr _ H ... ≤ comps R S : lattice.le_supr _ f lemma comps_le (R : Π (f : over X), sieve f.left) (S : sieve X) : comps R S ≤ S := begin apply lattice.supr_le _, rintros f, apply lattice.supr_le _, rintros H g ⟨a,b,e⟩, suffices : over.mk (g.hom) ∈ S, simp at this, apply this, rw e, apply sieve.subs, apply H, end def as_functor (S : sieve X) : Cᵒᵖ ⥤ Type v := { obj := λ Y, {g : Y.unop ⟶ X // over.mk g ∈ S}, map := λ Y Z f g, subtype.mk (f.unop ≫ g.1) (begin cases g with g gS, apply sieve.subs S (over.mk g) gS _ f.unop, end) } /-- The natural inclusion from the functor induced by a sieve to the yoneda embedding. -/ def functor_inclusion (S : sieve X) : S.as_functor ⟶ yoneda.obj X := nat_trans.mk (λ Y f, f.1) (λ Y Z g, rfl) def functor_inclusion_is_mono : mono (functor_inclusion S) := begin refine ⟨_⟩, intros Z f g h, ext Y y, apply subtype.ext.2, have : (f ≫ functor_inclusion S).app Y y = (g ≫ functor_inclusion S).app Y y, rw h, apply this end end sieve end category_theory
9a6192556e57950f248442a5cc3b6a00b568e53f
f0f12a5b81106a798deda31dca238c11997a605e
/Playlean4/Group/Normal.lean
0cb5d2be45a6136dea1dbe238ee36e4330185495
[ "MIT" ]
permissive
thejohncrafter/playlean4
fe7119d492aab07048f78333eeda9862f6471740
81df180a71b8d84d0f45bc98db367aad203cf5df
refs/heads/master
1,683,152,783,765
1,621,879,382,000
1,621,879,382,000
366,563,501
2
0
null
null
null
null
UTF-8
Lean
false
false
9,147
lean
import Playlean4.Group.Basic import Playlean4.Group.Subgroup import Playlean4.Group.LRClasses import Playlean4.Group.OnSet namespace Group namespace Subgroup variable {G : Type} (law : G → G → G) [grp : Group G law] local infixl:70 " * " => id' law @[appUnexpander id'] def normal.UnexpandGMul : Lean.PrettyPrinter.Unexpander | `(id' law $x $y) => `($x * $y) | _ => throw () local notation "one" => grp.one' -- HACK local notation g"⁻¹" => grp.inv g section variable (H : Set G) [sg : Subgroup G law H] local infixl:70 " * " => id' (subgroupLaw law H) @[appUnexpander id'] def normal.unexpandHMul : Lean.PrettyPrinter.Unexpander | `(id' (subgroupLaw law H) $x $y) => `($x * $y) | _ => throw () class Normal where stable : ∀ (g : G), ∀ {h}, h ∈ H → (g * h * g⁻¹) ∈ H end end Subgroup -- Start with the simple things : the kernel is always normal namespace Morphism variable {G : Type} {lawG : G → G → G} [grpG : Group G lawG] variable {H : Type} {lawH : H → H → H} [grpH : Group H lawH] variable (φ : Morphism G lawG H lawH) local infixl:70 " * " => id' lawG local notation g"⁻¹" => grpG.inv g local infix:70 " * " => id' lawH local notation g"⁻¹" => grpH.inv g def kernel : Set G := λ g => φ g = grpH.one' def kernelIsSubgroup : Subgroup G lawG (kernel φ) := Subgroup.ofInhabitedMulInvStable lawG (⟨ grpG.one', φ.respectOne ⟩) (λ {h} hIn h' h'In => by simp [kernel, Set.mem]; rw [hIn, h'In]; simp ) instance kernelIsNormal : Subgroup.Normal lawG (φ.kernel) where stable := λ g k kIn => by suffices p : φ (g * (k : G) * g⁻¹) = grpH.one' by exact p have p : φ k = grpH.one' := kIn simp [φ.respectMul, p] end Morphism namespace Subgroup variable {G : Type} {law : G → G → G} [grp : Group G law] local infixl:70 " * " => id' law @[appUnexpander id'] def normal.UnexpandGMul' : Lean.PrettyPrinter.Unexpander | `(id' law $x $y) => `($x * $y) | _ => throw () local notation "one" => grp.one' -- HACK local notation g"⁻¹" => grp.inv g -- Now prove the converse : a normal subgroup is the kernel of some function namespace Normal variable (H : Set G) [sg : Subgroup G law H] local infixl:70 " * " => id' (subgroupLaw law H) @[appUnexpander id'] def normal.unexpandHMul : Lean.PrettyPrinter.Unexpander | `(id' Magma.law H $x $y) => `($x * $y) | _ => throw () theorem normalIff.lemma₁ : (∀ (g : G), ∀ {h}, h ∈ H → g * h * g⁻¹ ∈ H) ↔ (∀ (g : G), ∀ {h}, h ∈ H → ∃ h', h' ∈ H ∧ g * h = h' * g) := by apply Iff.intro focus intro p g h hIn suffices p : ∃ h', h' ∈ H ∧ g * h * g⁻¹ = h' from match p with | ⟨ h', ⟨ h'In, p ⟩ ⟩ => ⟨ h', ⟨ h'In, by simp [p.symm] ⟩ ⟩ exact ⟨ g * h * g⁻¹, ⟨ p g hIn, rfl ⟩ ⟩ focus intro p g h hIn match p g hIn with | ⟨ h', ⟨ h'In, p ⟩ ⟩ => rw [show g * h * g⁻¹ = h' by simp [p]] simp [h'In] theorem normalIff.lemma₂ : (∀ (g : G), ∀ {h}, h ∈ H → ∃ h', h' ∈ H ∧ g * h = h' * g) → (∀ (g : G), ∀ {h}, h ∈ H → ∃ h', h' ∈ H ∧ g * h' = h * g) := by intro h g h₀ h₀In match h (g⁻¹) (sg.invMem h₀In) with | ⟨ h', ⟨ h'In, p ⟩ ⟩ => have ∀ k k' : G, k = k' → k⁻¹ = k'⁻¹ by intro _ _ h; rw [h] have p' := this _ _ p simp at p' exact ⟨ h'⁻¹, ⟨ sg.invMem h'In, p'.symm ⟩ ⟩ theorem normalIff.lemma₃ : (∀ (g : G), ∀ {h}, h ∈ H → ∃ h', h' ∈ H ∧ g * h = h' * g) → (∀ (g : G), ∀ {h}, h ∈ H → ∃ h', h' ∈ H ∧ g * h' = h * g) → ∀ (P : G → Prop), P ∈ leftClasses law H → P ∈ rightClasses law H := by intro h₀ h₁ p h' match h' with | ⟨ g, h' ⟩ => apply Exists.intro g rw [h'] simp [Action.Remarkable.onSelf] exact funext <| λ x => propext ⟨ (λ h'' => match h'' with | ⟨ k, ⟨ kIn, h'' ⟩ ⟩ => match h₀ g kIn with | ⟨ k', ⟨ k'In, h''' ⟩ ⟩ => ⟨ k', ⟨ k'In, by rw [h'']; simp [id'] at h'''; simp [id']; rw [h''']; rfl ⟩ ⟩), (λ h'' => match h'' with | ⟨ k, ⟨ kIn, h'' ⟩ ⟩ => match h₁ g kIn with | ⟨ k', ⟨ k'In, h''' ⟩ ⟩ => ⟨ k', ⟨ k'In, by rw [h'']; simp [id'] at h'''; simp [id']; rw [h''']; rfl ⟩ ⟩) ⟩ theorem normalIff.lemma₄ : (∀ (g : G), ∀ {h}, h ∈ H → ∃ h', h' ∈ H ∧ g * h = h' * g) → (∀ (g : G), ∀ {h}, h ∈ H → ∃ h', h' ∈ H ∧ g * h' = h * g) → ∀ (P : G → Prop), P ∈ rightClasses law H → P ∈ leftClasses law H := by rw [← leftClassesOnOp, ← rightClassesOnOp] exact λ h₁ h₂ p h => @normalIff.lemma₃ _ (lawᵒᵖ) _ H (λ g h hIn => match h₂ g hIn with | ⟨ k, ⟨ kIn, h₂ ⟩ ⟩ => ⟨ k, ⟨ kIn, h₂.symm ⟩ ⟩) (λ g h hIn => match h₁ g hIn with | ⟨ k, ⟨ kIn, h₁ ⟩ ⟩ => ⟨ k, ⟨ kIn, h₁.symm ⟩ ⟩) p h theorem normalIff.lemma₅ : (∀ (P : Set G), P ∈ rightClasses law H ↔ P ∈ leftClasses law H) → (∀ (g : G), ∀ {h}, h ∈ H → ∃ h', h' ∈ H ∧ g * h = h' * g) := by intro h g h₀ h₀In have p := ((leftClassOf law H g).property) rw [← h] at p have p₁ := leftClassMemIff law H ((leftClassOf law H g).2) (memOfLeftClassOf law H g) (g * h₀) have p₂ := rightClassMemIff law H p (memOfLeftClassOf law H g) (g * h₀) exact (p₁.trans p₂).1 ⟨ h₀, ⟨ h₀In, rfl ⟩ ⟩ section variable [normal : Normal law H] open Action.Remarkable.OnSet local infix:70 " •ₗ " => leftTranslationOnSet law local notation:70 lhs:70 " •ᵣ " rhs:70 => rightTranslationOnSet law rhs lhs local infix:70 " •• " => conjugationOnSet law local infixl:70 " ** " => mulOnSet law theorem conjH (g : G) : g •• H = H := by funext h apply propext suffices (∃ h', h' ∈ H ∧ h = g * h' * g⁻¹) ↔ h ∈ H by exact this exact ⟨ λ p => match p with | ⟨ h', ⟨ h'In, p ⟩ ⟩ => p ▸ normal.stable g h'In, λ p => ⟨ (g⁻¹ * h * (g⁻¹⁻¹)), ⟨ normal.stable (g⁻¹) p, by simp ⟩ ⟩ ⟩ theorem moveLeft (g : G) : H •ᵣ g = g •ₗ H := by rw [show H •ᵣ g = (g •• H) •ᵣ g by rw [conjH]] rw [conjugationCompat, ← rightTranslationCompat, invCancelLeft, rightTranslationIdentity] theorem neutralRight (g : G) : (g •ₗ H) ** H = g •ₗ H := by funext x apply propext apply Iff.intro exact λ h => match h with | ⟨ res, ⟨ h₁, h₁In, resEq ⟩, h₂, h₂In, h₂Eq ⟩ => ⟨ h₁ * h₂, sg.mulMem h₁In h₂In, by rw [h₂Eq, resEq] suffices g * h₁ * h₂ = g * (h₁ * h₂) by exact this simp ⟩ exact λ h => match h with | ⟨ h, hIn, xEq ⟩ => ⟨ g * h, ⟨ h, hIn, rfl ⟩, one, oneMem, by rw [xEq] suffices g * h = g * h * one by exact this simp ⟩ /-! "pseudo morphism" because there is no notion of magma morphisms (yet !) -/ theorem pseudoMorphism (g : G) (g' : G) : (g •ₗ H) ** (g' •ₗ H) = (g * g') •ₗ H := by have p : g •ₗ (g' •ₗ H) = (g * g') •ₗ H from ((leftActionOnSet law).compat g g' H).symm rw [mulOnSetCompat₁, translationCompat, moveLeft, p, neutralRight] theorem leftClassesStable {P Q : Set G} (PIn : P ∈ leftClasses law H) (QIn : Q ∈ leftClasses law H) : P ** Q ∈ leftClasses law H := by rw [leftClassIff] at PIn rw [leftClassIff] at QIn rw [leftClassIff] match PIn, QIn with | ⟨ g, PIs ⟩, ⟨ g', QIs ⟩ => rw [PIs, QIs] rw [pseudoMorphism] exact ⟨ g * g', rfl ⟩ def quotientLaw : leftClasses law H → leftClasses law H → leftClasses law H := λ P Q => ⟨ P ** Q, leftClassesStable H P.2 Q.2 ⟩ instance quotientGroup : Group (leftClasses law H) (quotientLaw H) where one' := subgroupAsLeftClass law H assoc := λ P₁ P₂ P₃ => by apply Subtype.eq simp [id', quotientLaw] match (leftClassIff law _ _).2 P₁.2, (leftClassIff law _ _).2 P₂.2, (leftClassIff law _ _).2 P₃.2 with | ⟨ g₁, (P₁Is : P₁ = g₁ •ₗ H) ⟩, ⟨ g₂, (P₂Is : P₂ = g₂ •ₗ H) ⟩, ⟨ g₃, (P₃Is : P₃ = g₃ •ₗ H) ⟩ => rw [P₁Is, P₂Is, P₃Is] simp [pseudoMorphism] oneNeutralRight := λ P => by apply Subtype.eq simp only [id', quotientLaw] exact match (leftClassIff law _ _).2 P.2 with | ⟨ g, PIs ⟩ => PIs ▸ neutralRight _ _ invertible := λ P => by simp only [id', quotientLaw] match (leftClassIff law _ _).2 P.2 with | ⟨ g, (PIs : P = g •ₗ H)⟩ => apply Exists.intro (leftClassOf law H (g⁻¹)) apply Subtype.eq suffices P.1 ** (g⁻¹ •ₗ H) = H by exact this rw [PIs] simp only [pseudoMorphism, invCancelRight] exact (leftActionOnSet law).identity H def canonicalSurjection : Morphism G law (leftClasses law H) (quotientLaw H) where f := λ g => ⟨ g •ₗ H, by rw [leftClassIff] exact ⟨ g, rfl ⟩ ⟩ respectMul' := λ g g' => Subtype.eq <| (pseudoMorphism H _ _).symm end end Normal end Subgroup end Group
a2c9e780833f74d831bf67cd621223e35c7ae03e
08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4
/src/Lean/Elab/Tactic/BuiltinTactic.lean
e417872a6b95042b5f76a453f361317576c34878
[ "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", "Apache-2.0", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
gebner/lean4
d51c4922640a52a6f7426536ea669ef18a1d9af5
8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f
refs/heads/master
1,685,732,780,391
1,672,962,627,000
1,673,459,398,000
373,307,283
0
0
Apache-2.0
1,691,316,730,000
1,622,669,271,000
Lean
UTF-8
Lean
false
false
15,592
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.Assumption import Lean.Meta.Tactic.Contradiction import Lean.Meta.Tactic.Refl import Lean.Elab.Binders import Lean.Elab.Open import Lean.Elab.SetOption import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.ElabTerm namespace Lean.Elab.Tactic open Meta open Parser.Tactic @[builtin_tactic withAnnotateState] def evalWithAnnotateState : Tactic | `(tactic| with_annotate_state $stx $t) => withTacticInfoContext stx (evalTactic t) | _ => throwUnsupportedSyntax @[builtin_tactic Lean.Parser.Tactic.«done»] def evalDone : Tactic := fun _ => done @[builtin_tactic seq1] def evalSeq1 : Tactic := fun stx => do let args := stx[0].getArgs for i in [:args.size] do if i % 2 == 0 then evalTactic args[i]! else saveTacticInfoForToken args[i]! -- add `TacticInfo` node for `;` @[builtin_tactic paren] def evalParen : Tactic := fun stx => evalTactic stx[1] def isCheckpointableTactic (arg : Syntax) : TacticM Bool := do -- TODO: make it parametric let kind := arg.getKind return kind == ``Lean.Parser.Tactic.save /-- Takes a `sepByIndent tactic "; "`, and inserts `checkpoint` blocks for `save` tactics. Input: ``` a b save c d save e ``` Output: ``` checkpoint a b save checkpoint c d save e ``` -/ -- Note that we need to preserve the separators to show the right goals after semicolons. def addCheckpoints (stx : Syntax) : TacticM Syntax := do -- if (← readThe Term.Context).tacticCache? |>.isSome then if !(← stx.getSepArgs.anyM isCheckpointableTactic) then return stx let mut currentCheckpointBlock := #[] let mut output := #[] for i in [:stx.getArgs.size / 2] do let tac := stx[2*i] let sep? := stx.getArgs[2*i+1]? if (← isCheckpointableTactic tac) then let checkpoint : Syntax := mkNode ``checkpoint #[ mkAtomFrom tac "checkpoint", mkNode ``tacticSeq #[ mkNode ``tacticSeq1Indented #[ -- HACK: null node is not a valid tactic, but prevents infinite loop mkNullNode (currentCheckpointBlock.push tac) ] ] ] currentCheckpointBlock := #[] output := output.push checkpoint if let some sep := sep? then output := output.push sep else currentCheckpointBlock := currentCheckpointBlock.push tac if let some sep := sep? then currentCheckpointBlock := currentCheckpointBlock.push sep output := output ++ currentCheckpointBlock return stx.setArgs output /-- Evaluate `sepByIndent tactic "; " -/ def evalSepByIndentTactic (stx : Syntax) : TacticM Unit := do let stx ← addCheckpoints stx for arg in stx.getArgs, i in [:stx.getArgs.size] do if i % 2 == 0 then evalTactic arg else saveTacticInfoForToken arg @[builtin_tactic tacticSeq1Indented] def evalTacticSeq1Indented : Tactic := fun stx => evalSepByIndentTactic stx[0] @[builtin_tactic tacticSeqBracketed] def evalTacticSeqBracketed : Tactic := fun stx => do let initInfo ← mkInitialTacticInfo stx[0] withRef stx[2] <| closeUsingOrAdmit do -- save state before/after entering focus on `{` withInfoContext (pure ()) initInfo evalSepByIndentTactic stx[1] @[builtin_tactic Parser.Tactic.focus] def evalFocus : Tactic := fun stx => do let mkInfo ← mkInitialTacticInfo stx[0] focus do -- show focused state on `focus` withInfoContext (pure ()) mkInfo evalTactic stx[1] private def getOptRotation (stx : Syntax) : Nat := if stx.isNone then 1 else stx[0].toNat @[builtin_tactic Parser.Tactic.rotateLeft] def evalRotateLeft : Tactic := fun stx => do let n := getOptRotation stx[1] setGoals <| (← getGoals).rotateLeft n @[builtin_tactic Parser.Tactic.rotateRight] def evalRotateRight : Tactic := fun stx => do let n := getOptRotation stx[1] setGoals <| (← getGoals).rotateRight n @[builtin_tactic Parser.Tactic.open] def evalOpen : Tactic := fun stx => do let `(tactic| open $decl in $tac) := stx | throwUnsupportedSyntax try pushScope let openDecls ← elabOpenDecl decl withTheReader Core.Context (fun ctx => { ctx with openDecls := openDecls }) do evalTactic tac finally popScope @[builtin_tactic Parser.Tactic.set_option] def elabSetOption : Tactic := fun stx => do let options ← Elab.elabSetOption stx[1] stx[2] withTheReader Core.Context (fun ctx => { ctx with maxRecDepth := maxRecDepth.get options, options := options }) do evalTactic stx[4] @[builtin_tactic Parser.Tactic.allGoals] def evalAllGoals : Tactic := fun stx => do let mvarIds ← getGoals let mut mvarIdsNew := #[] for mvarId in mvarIds do unless (← mvarId.isAssigned) do setGoals [mvarId] try evalTactic stx[1] mvarIdsNew := mvarIdsNew ++ (← getUnsolvedGoals) catch ex => if (← read).recover then logException ex mvarIdsNew := mvarIdsNew.push mvarId else throw ex setGoals mvarIdsNew.toList @[builtin_tactic Parser.Tactic.anyGoals] def evalAnyGoals : Tactic := fun stx => do let mvarIds ← getGoals let mut mvarIdsNew := #[] let mut succeeded := false for mvarId in mvarIds do unless (← mvarId.isAssigned) do setGoals [mvarId] try evalTactic stx[1] mvarIdsNew := mvarIdsNew ++ (← getUnsolvedGoals) succeeded := true catch _ => mvarIdsNew := mvarIdsNew.push mvarId unless succeeded do throwError "failed on all goals" setGoals mvarIdsNew.toList @[builtin_tactic tacticSeq] def evalTacticSeq : Tactic := fun stx => evalTactic stx[0] partial def evalChoiceAux (tactics : Array Syntax) (i : Nat) : TacticM Unit := if h : i < tactics.size then let tactic := tactics.get ⟨i, h⟩ catchInternalId unsupportedSyntaxExceptionId (evalTactic tactic) (fun _ => evalChoiceAux tactics (i+1)) else throwUnsupportedSyntax @[builtin_tactic choice] def evalChoice : Tactic := fun stx => evalChoiceAux stx.getArgs 0 @[builtin_tactic skip] def evalSkip : Tactic := fun _ => pure () @[builtin_tactic unknown] def evalUnknown : Tactic := fun stx => do addCompletionInfo <| CompletionInfo.tactic stx (← getGoals) @[builtin_tactic failIfSuccess] def evalFailIfSuccess : Tactic := fun stx => Term.withoutErrToSorry <| withoutRecover do let tactic := stx[1] if (← try evalTactic tactic; pure true catch _ => pure false) then throwError "tactic succeeded" @[builtin_tactic traceState] def evalTraceState : Tactic := fun _ => do let gs ← getUnsolvedGoals addRawTrace (goalsToMessageData gs) @[builtin_tactic traceMessage] def evalTraceMessage : Tactic := fun stx => do match stx[1].isStrLit? with | none => throwIllFormedSyntax | some msg => withRef stx[0] <| addRawTrace msg @[builtin_tactic Lean.Parser.Tactic.assumption] def evalAssumption : Tactic := fun _ => liftMetaTactic fun mvarId => do mvarId.assumption; pure [] @[builtin_tactic Lean.Parser.Tactic.contradiction] def evalContradiction : Tactic := fun _ => liftMetaTactic fun mvarId => do mvarId.contradiction; pure [] @[builtin_tactic Lean.Parser.Tactic.refl] def evalRefl : Tactic := fun _ => liftMetaTactic fun mvarId => do mvarId.refl; pure [] @[builtin_tactic Lean.Parser.Tactic.intro] def evalIntro : Tactic := fun stx => do match stx with | `(tactic| intro) => introStep none `_ | `(tactic| intro $h:ident) => introStep h h.getId | `(tactic| intro _%$tk) => introStep tk `_ /- Type ascription -/ | `(tactic| intro ($h:ident : $type:term)) => introStep h h.getId type /- We use `@h` at the match-discriminant to disable the implicit lambda feature -/ | `(tactic| intro $pat:term) => evalTactic (← `(tactic| intro h; match @h with | $pat:term => ?_; try clear h)) | `(tactic| intro $h:term $hs:term*) => evalTactic (← `(tactic| intro $h:term; intro $hs:term*)) | _ => throwUnsupportedSyntax where introStep (ref : Option Syntax) (n : Name) (typeStx? : Option Syntax := none) : TacticM Unit := do let fvarId ← liftMetaTacticAux fun mvarId => do let (fvarId, mvarId) ← mvarId.intro n pure (fvarId, [mvarId]) if let some typeStx := typeStx? then withMainContext do let type ← Term.withSynthesize (mayPostpone := true) <| Term.elabType typeStx let fvar := mkFVar fvarId let fvarType ← inferType fvar unless (← isDefEqGuarded type fvarType) do throwError "type mismatch at `intro {fvar}`{← mkHasTypeButIsExpectedMsg fvarType type}" liftMetaTactic fun mvarId => return [← mvarId.replaceLocalDeclDefEq fvarId type] if let some stx := ref then withMainContext do Term.addLocalVarInfo stx (mkFVar fvarId) @[builtin_tactic Lean.Parser.Tactic.introMatch] def evalIntroMatch : Tactic := fun stx => do let matchAlts := stx[1] let stxNew ← liftMacroM <| Term.expandMatchAltsIntoMatchTactic stx matchAlts withMacroExpansion stx stxNew <| evalTactic stxNew @[builtin_tactic «intros»] def evalIntros : Tactic := fun stx => match stx with | `(tactic| intros) => liftMetaTactic fun mvarId => do let (_, mvarId) ← mvarId.intros return [mvarId] | `(tactic| intros $ids*) => do let fvars ← liftMetaTacticAux fun mvarId => do let (fvars, mvarId) ← mvarId.introN ids.size (ids.map getNameOfIdent').toList return (fvars, [mvarId]) withMainContext do for stx in ids, fvar in fvars do Term.addLocalVarInfo stx (mkFVar fvar) | _ => throwUnsupportedSyntax @[builtin_tactic Lean.Parser.Tactic.revert] def evalRevert : Tactic := fun stx => match stx with | `(tactic| revert $hs*) => do let (_, mvarId) ← (← getMainGoal).revert (← getFVarIds hs) replaceMainGoal [mvarId] | _ => throwUnsupportedSyntax @[builtin_tactic Lean.Parser.Tactic.clear] def evalClear : Tactic := fun stx => match stx with | `(tactic| clear $hs*) => do let fvarIds ← getFVarIds hs let fvarIds ← withMainContext <| sortFVarIds fvarIds for fvarId in fvarIds.reverse do withMainContext do let mvarId ← (← getMainGoal).clear fvarId replaceMainGoal [mvarId] | _ => throwUnsupportedSyntax def forEachVar (hs : Array Syntax) (tac : MVarId → FVarId → MetaM MVarId) : TacticM Unit := do for h in hs do withMainContext do let fvarId ← getFVarId h let mvarId ← tac (← getMainGoal) fvarId replaceMainGoal [mvarId] @[builtin_tactic Lean.Parser.Tactic.subst] def evalSubst : Tactic := fun stx => match stx with | `(tactic| subst $hs*) => forEachVar hs Meta.subst | _ => throwUnsupportedSyntax @[builtin_tactic Lean.Parser.Tactic.substVars] def evalSubstVars : Tactic := fun _ => liftMetaTactic fun mvarId => return [← substVars mvarId] /-- Searches for a metavariable `g` s.t. `tag` is its exact name. If none then searches for a metavariable `g` s.t. `tag` is a suffix of its name. If none, then it searches for a metavariable `g` s.t. `tag` is a prefix of its name. -/ private def findTag? (mvarIds : List MVarId) (tag : Name) : TacticM (Option MVarId) := do match (← mvarIds.findM? fun mvarId => return tag == (← mvarId.getDecl).userName) with | some mvarId => return mvarId | none => match (← mvarIds.findM? fun mvarId => return tag.isSuffixOf (← mvarId.getDecl).userName) with | some mvarId => return mvarId | none => mvarIds.findM? fun mvarId => return tag.isPrefixOf (← mvarId.getDecl).userName def renameInaccessibles (mvarId : MVarId) (hs : TSyntaxArray ``binderIdent) : TacticM MVarId := do if hs.isEmpty then return mvarId else let mvarDecl ← mvarId.getDecl let mut lctx := mvarDecl.lctx let mut hs := hs let mut info := #[] let mut found : NameSet := {} let n := lctx.numIndices for i in [:n] do let j := n - i - 1 match lctx.getAt? j with | none => pure () | some localDecl => if localDecl.userName.hasMacroScopes || found.contains localDecl.userName then if let `(binderIdent| $h:ident) := hs.back then let newName := h.getId lctx := lctx.setUserName localDecl.fvarId newName info := info.push (localDecl.fvarId, h) hs := hs.pop if hs.isEmpty then break found := found.insert localDecl.userName unless hs.isEmpty do logError m!"too many variable names provided" let mvarNew ← mkFreshExprMVarAt lctx mvarDecl.localInstances mvarDecl.type MetavarKind.syntheticOpaque mvarDecl.userName withSaveInfoContext <| mvarNew.mvarId!.withContext do for (fvarId, stx) in info do Term.addLocalVarInfo stx (mkFVar fvarId) mvarId.assign mvarNew return mvarNew.mvarId! private def getCaseGoals (tag : TSyntax ``binderIdent) : TacticM (MVarId × List MVarId) := do let gs ← getUnsolvedGoals let g ← if let `(binderIdent| $tag:ident) := tag then let tag := tag.getId let some g ← findTag? gs tag | throwError "tag not found" pure g else getMainGoal return (g, gs.erase g) @[builtin_tactic «case»] def evalCase : Tactic | stx@`(tactic| case $[$tag $hs*]|* =>%$arr $tac:tacticSeq) => for tag in tag, hs in hs do let (g, gs) ← getCaseGoals tag let g ← renameInaccessibles g hs setGoals [g] g.setTag Name.anonymous withCaseRef arr tac do closeUsingOrAdmit (withTacticInfoContext stx (evalTactic tac)) setGoals gs | _ => throwUnsupportedSyntax @[builtin_tactic «case'»] def evalCase' : Tactic | `(tactic| case' $[$tag $hs*]|* =>%$arr $tac:tacticSeq) => do let mut acc := #[] for tag in tag, hs in hs do let (g, gs) ← getCaseGoals tag let g ← renameInaccessibles g hs let mvarTag ← g.getTag setGoals [g] withCaseRef arr tac (evalTactic tac) let gs' ← getUnsolvedGoals if let [g'] := gs' then g'.setTag mvarTag acc := acc ++ gs' setGoals gs setGoals (acc.toList ++ (← getGoals)) | _ => throwUnsupportedSyntax @[builtin_tactic «renameI»] def evalRenameInaccessibles : Tactic | `(tactic| rename_i $hs*) => do replaceMainGoal [← renameInaccessibles (← getMainGoal) hs] | _ => throwUnsupportedSyntax @[builtin_tactic «first»] partial def evalFirst : Tactic := fun stx => do let tacs := stx[1].getArgs if tacs.isEmpty then throwUnsupportedSyntax loop tacs 0 where loop (tacs : Array Syntax) (i : Nat) := if i == tacs.size - 1 then evalTactic tacs[i]![1] else evalTactic tacs[i]![1] <|> loop tacs (i+1) @[builtin_tactic «fail»] def evalFail : Tactic := fun stx => do let goals ← getGoals let goalsMsg := MessageData.joinSep (goals.map MessageData.ofGoal) m!"\n\n" match stx with | `(tactic| fail) => throwError "tactic 'fail' failed\n{goalsMsg}" | `(tactic| fail $msg:str) => throwError "{msg.getString}\n{goalsMsg}" | _ => throwUnsupportedSyntax @[builtin_tactic Parser.Tactic.dbgTrace] def evalDbgTrace : Tactic := fun stx => do match stx[1].isStrLit? with | none => throwIllFormedSyntax | some msg => dbg_trace msg @[builtin_tactic sleep] def evalSleep : Tactic := fun stx => do match stx[1].isNatLit? with | none => throwIllFormedSyntax | some ms => IO.sleep ms.toUInt32 end Lean.Elab.Tactic
4ee4983f2998143c399d022701c37b43256ff8f2
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/order_iso_nat.lean
f74000f5e5e54bd9342e4acb4062b5b97893b1ce
[ "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,216
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.equiv.denumerable import data.nat.lattice import logic.function.iterate import order.hom.basic /-! # Relation embeddings from the naturals This file allows translation from monotone functions `ℕ → α` to order embeddings `ℕ ↪ α` and defines the limit value of an eventually-constant sequence. ## Main declarations * `nat_lt`/`nat_gt`: Make an order embedding `ℕ ↪ α` from an increasing/decreasing function `ℕ → α`. * `monotonic_sequence_limit`: The limit of an eventually-constant monotone sequence `ℕ →o α`. * `monotonic_sequence_limit_index`: The index of the first occurence of `monotonic_sequence_limit` in the sequence. -/ namespace rel_embedding variables {α : Type*} {r : α → α → Prop} [is_strict_order α r] /-- If `f` is a strictly `r`-increasing sequence, then this returns `f` as an order embedding. -/ def nat_lt (f : ℕ → α) (H : ∀ n : ℕ, r (f n) (f (n + 1))) : ((<) : ℕ → ℕ → Prop) ↪r r := of_monotone f $ nat.rel_of_forall_rel_succ_of_lt r H @[simp] lemma nat_lt_apply {f : ℕ → α} {H : ∀ n : ℕ, r (f n) (f (n + 1))} {n : ℕ} : nat_lt f H n = f n := rfl /-- If `f` is a strictly `r`-decreasing sequence, then this returns `f` as an order embedding. -/ def nat_gt (f : ℕ → α) (H : ∀ n : ℕ, r (f (n + 1)) (f n)) : ((>) : ℕ → ℕ → Prop) ↪r r := by haveI := is_strict_order.swap r; exact rel_embedding.swap (nat_lt f H) theorem well_founded_iff_no_descending_seq : well_founded r ↔ is_empty (((>) : ℕ → ℕ → Prop) ↪r r) := ⟨λ ⟨h⟩, ⟨λ ⟨f, o⟩, suffices ∀ a, acc r a → ∀ n, a ≠ f n, from this (f 0) (h _) 0 rfl, λ a ac, begin induction ac with a _ IH, intros n h, subst a, exact IH (f (n+1)) (o.2 (nat.lt_succ_self _)) _ rfl end⟩, λ E, ⟨λ a, classical.by_contradiction $ λ na, let ⟨f, h⟩ := classical.axiom_of_choice $ show ∀ x : {a // ¬ acc r a}, ∃ y : {a // ¬ acc r a}, r y.1 x.1, from λ ⟨x, h⟩, classical.by_contradiction $ λ hn, h $ ⟨_, λ y h, classical.by_contradiction $ λ na, hn ⟨⟨y, na⟩, h⟩⟩ in E.elim' (nat_gt (λ n, (f^[n] ⟨a, na⟩).1) $ λ n, by { rw [function.iterate_succ'], apply h })⟩⟩ end rel_embedding namespace nat variables (s : set ℕ) [decidable_pred (∈ s)] [infinite s] /-- An order embedding from `ℕ` to itself with a specified range -/ def order_embedding_of_set : ℕ ↪o ℕ := (rel_embedding.order_embedding_of_lt_embedding (rel_embedding.nat_lt (nat.subtype.of_nat s) (λ n, nat.subtype.lt_succ_self _))).trans (order_embedding.subtype s) /-- `nat.subtype.of_nat` as an order isomorphism between `ℕ` and an infinite decidable subset. See also `nat.nth` for a version where the subset may be finite. -/ noncomputable def subtype.order_iso_of_nat : ℕ ≃o s := rel_iso.of_surjective (rel_embedding.order_embedding_of_lt_embedding (rel_embedding.nat_lt (nat.subtype.of_nat s) (λ n, nat.subtype.lt_succ_self _))) nat.subtype.of_nat_surjective variable {s} @[simp] lemma coe_order_embedding_of_set : ⇑(order_embedding_of_set s) = coe ∘ subtype.of_nat s := rfl lemma order_embedding_of_set_apply {n : ℕ} : order_embedding_of_set s n = subtype.of_nat s n := rfl @[simp] lemma subtype.order_iso_of_nat_apply {n : ℕ} : subtype.order_iso_of_nat s n = subtype.of_nat s n := by { simp [subtype.order_iso_of_nat] } variable (s) lemma order_embedding_of_set_range : set.range (nat.order_embedding_of_set s) = s := subtype.coe_comp_of_nat_range theorem exists_subseq_of_forall_mem_union {α : Type*} {s t : set α} (e : ℕ → α) (he : ∀ n, e n ∈ s ∪ t) : ∃ g : ℕ ↪o ℕ, (∀ n, e (g n) ∈ s) ∨ (∀ n, e (g n) ∈ t) := begin classical, have : infinite (e ⁻¹' s) ∨ infinite (e ⁻¹' t), by simp only [set.infinite_coe_iff, ← set.infinite_union, ← set.preimage_union, set.eq_univ_of_forall (λ n, set.mem_preimage.2 (he n)), set.infinite_univ], casesI this, exacts [⟨nat.order_embedding_of_set (e ⁻¹' s), or.inl $ λ n, (nat.subtype.of_nat (e ⁻¹' s) _).2⟩, ⟨nat.order_embedding_of_set (e ⁻¹' t), or.inr $ λ n, (nat.subtype.of_nat (e ⁻¹' t) _).2⟩] end end nat theorem exists_increasing_or_nonincreasing_subseq' {α : Type*} (r : α → α → Prop) (f : ℕ → α) : ∃ (g : ℕ ↪o ℕ), (∀ n : ℕ, r (f (g n)) (f (g (n + 1)))) ∨ (∀ m n : ℕ, m < n → ¬ r (f (g m)) (f (g n))) := begin classical, let bad : set ℕ := { m | ∀ n, m < n → ¬ r (f m) (f n) }, by_cases hbad : infinite bad, { haveI := hbad, refine ⟨nat.order_embedding_of_set bad, or.intro_right _ (λ m n mn, _)⟩, have h := set.mem_range_self m, rw nat.order_embedding_of_set_range bad at h, exact h _ ((order_embedding.lt_iff_lt _).2 mn) }, { rw [set.infinite_coe_iff, set.infinite, not_not] at hbad, obtain ⟨m, hm⟩ : ∃ m, ∀ n, m ≤ n → ¬ n ∈ bad, { by_cases he : hbad.to_finset.nonempty, { refine ⟨(hbad.to_finset.max' he).succ, λ n hn nbad, nat.not_succ_le_self _ (hn.trans (hbad.to_finset.le_max' n (hbad.mem_to_finset.2 nbad)))⟩ }, { exact ⟨0, λ n hn nbad, he ⟨n, hbad.mem_to_finset.2 nbad⟩⟩ } }, have h : ∀ (n : ℕ), ∃ (n' : ℕ), n < n' ∧ r (f (n + m)) (f (n' + m)), { intro n, have h := hm _ (le_add_of_nonneg_left n.zero_le), simp only [exists_prop, not_not, set.mem_set_of_eq, not_forall] at h, obtain ⟨n', hn1, hn2⟩ := h, obtain ⟨x, hpos, rfl⟩ := exists_pos_add_of_lt hn1, refine ⟨n + x, add_lt_add_left hpos n, _⟩, rw [add_assoc, add_comm x m, ← add_assoc], exact hn2 }, let g' : ℕ → ℕ := @nat.rec (λ _, ℕ) m (λ n gn, nat.find (h gn)), exact ⟨(rel_embedding.nat_lt (λ n, g' n + m) (λ n, nat.add_lt_add_right (nat.find_spec (h (g' n))).1 m)).order_embedding_of_lt_embedding, or.intro_left _ (λ n, (nat.find_spec (h (g' n))).2)⟩ } end theorem exists_increasing_or_nonincreasing_subseq {α : Type*} (r : α → α → Prop) [is_trans α r] (f : ℕ → α) : ∃ (g : ℕ ↪o ℕ), (∀ m n : ℕ, m < n → r (f (g m)) (f (g n))) ∨ (∀ m n : ℕ, m < n → ¬ r (f (g m)) (f (g n))) := begin obtain ⟨g, hr | hnr⟩ := exists_increasing_or_nonincreasing_subseq' r f, { refine ⟨g, or.intro_left _ (λ m n mn, _)⟩, obtain ⟨x, rfl⟩ := le_iff_exists_add.1 (nat.succ_le_iff.2 mn), induction x with x ih, { apply hr }, { apply is_trans.trans _ _ _ _ (hr _), exact ih (lt_of_lt_of_le m.lt_succ_self (nat.le_add_right _ _)) } }, { exact ⟨g, or.intro_right _ hnr⟩ } end /-- The "monotone chain condition" below is sometimes a convenient form of well foundedness. -/ lemma well_founded.monotone_chain_condition (α : Type*) [partial_order α] : well_founded ((>) : α → α → Prop) ↔ ∀ (a : ℕ →o α), ∃ n, ∀ m, n ≤ m → a n = a m := begin split; intros h, { rw well_founded.well_founded_iff_has_max' at h, intros a, have hne : (set.range a).nonempty, { use a 0, simp, }, obtain ⟨x, ⟨n, hn⟩, range_bounded⟩ := h _ hne, use n, intros m hm, rw ← hn at range_bounded, symmetry, apply range_bounded (a m) (set.mem_range_self _) (a.monotone hm), }, { rw rel_embedding.well_founded_iff_no_descending_seq, refine ⟨λ a, _⟩, obtain ⟨n, hn⟩ := h (a.swap : ((<) : ℕ → ℕ → Prop) →r ((<) : α → α → Prop)).to_order_hom, exact n.succ_ne_self.symm (rel_embedding.to_order_hom_injective _ (hn _ n.le_succ)), }, end /-- Given an eventually-constant monotone sequence `a₀ ≤ a₁ ≤ a₂ ≤ ...` in a partially-ordered type, `monotonic_sequence_limit_index a` is the least natural number `n` for which `aₙ` reaches the constant value. For sequences that are not eventually constant, `monotonic_sequence_limit_index a` is defined, but is a junk value. -/ noncomputable def monotonic_sequence_limit_index {α : Type*} [partial_order α] (a : ℕ →o α) : ℕ := Inf { n | ∀ m, n ≤ m → a n = a m } /-- The constant value of an eventually-constant monotone sequence `a₀ ≤ a₁ ≤ a₂ ≤ ...` in a partially-ordered type. -/ noncomputable def monotonic_sequence_limit {α : Type*} [partial_order α] (a : ℕ →o α) := a (monotonic_sequence_limit_index a) lemma well_founded.supr_eq_monotonic_sequence_limit {α : Type*} [complete_lattice α] (h : well_founded ((>) : α → α → Prop)) (a : ℕ →o α) : (⨆ m, a m) = monotonic_sequence_limit a := begin suffices : (⨆ (m : ℕ), a m) ≤ monotonic_sequence_limit a, { exact le_antisymm this (le_supr a _), }, apply supr_le, intros m, by_cases hm : m ≤ monotonic_sequence_limit_index a, { exact a.monotone hm, }, { replace hm := le_of_not_le hm, let S := { n | ∀ m, n ≤ m → a n = a m }, have hInf : Inf S ∈ S, { refine nat.Inf_mem _, rw well_founded.monotone_chain_condition at h, exact h a, }, change Inf S ≤ m at hm, change a m ≤ a (Inf S), rw hInf m hm, }, end
8dc2d2617da14ec0f33ae5ab46d7bdce83cffc57
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/hott/algebra/category/strict.hlean
241fe32d6d48b9cb5c2cadf68aff44c488b8ca64
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
1,498
hlean
/- Copyright (c) 2015 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jakob von Raumer -/ import .functor.basic open is_trunc eq namespace category structure strict_precategory [class] (ob : Type) extends precategory ob := mk' :: (is_set_ob : is_set ob) attribute strict_precategory.is_set_ob [instance] definition strict_precategory.mk [reducible] {ob : Type} (C : precategory ob) (H : is_set ob) : strict_precategory ob := precategory.rec_on C strict_precategory.mk' H structure Strict_precategory : Type := (carrier : Type) (struct : strict_precategory carrier) attribute Strict_precategory.struct [instance] [coercion] definition Strict_precategory.to_Precategory [coercion] [reducible] (C : Strict_precategory) : Precategory := Precategory.mk (Strict_precategory.carrier C) _ open functor -- TODO: move to constructions.cat? definition precategory_strict_precategory [constructor] : precategory Strict_precategory := precategory.mk (λ A B, A ⇒ B) (λ A B C G F, G ∘f F) (λ A, 1) (λ A B C D, functor.assoc) (λ A B, functor.id_left) (λ A B, functor.id_right) definition Precategory_strict_precategory [constructor] := precategory.Mk precategory_strict_precategory namespace ops abbreviation Cat := Precategory_strict_precategory end ops end category
82d63844f2525d645992fc26ab5672ce90d1bb17
abd85493667895c57a7507870867b28124b3998f
/src/linear_algebra/nonsingular_inverse.lean
63b40a528928fca57a9c5d31c8f6560067ab1e3f
[ "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
15,504
lean
/- Copyright (c) 2019 Tim Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Tim Baanen. -/ import algebra.associated import linear_algebra.determinant import tactic.linarith import tactic.ring_exp /-! # Nonsingular inverses In this file, we define an inverse for square matrices of invertible determinant. For matrices that are not square or not of full rank, there is a more general notion of pseudoinverses. Unfortunately, the definition of pseudoinverses is typically in terms of inverses of nonsingular matrices, so we need to define those first. The file also doesn't define a `has_inv` instance for `matrix` so that can be used for the pseudoinverse instead. The definition of inverse used in this file is the adjugate divided by the determinant. The adjugate is calculated with Cramer's rule, which we introduce first. The vectors returned by Cramer's rule are given by the linear map `cramer`, which sends a matrix `A` and vector `b` to the vector consisting of the determinant of replacing the `i`th column of `A` with `b` at index `i` (written as `(A.update_column i b).det`). Using Cramer's rule, we can compute for each matrix `A` the matrix `adjugate A`. The entries of the adjugate are the determinants of each minor of `A`. Instead of defining a minor to be `A` with column `i` and row `j` deleted, we replace the `i`th column of `A` with the `j`th basis vector; this has the same determinant as the minor but more importantly equals Cramer's rule applied to `A` and the `j`th basis vector, simplifying the subsequent proofs. We prove the adjugate behaves like `det A • A⁻¹`. Finally, we show that dividing the adjugate by `det A` (if possible), giving a matrix `nonsing_inv A`, will result in a multiplicative inverse to `A`. ## References * https://en.wikipedia.org/wiki/Cramer's_rule#Finding_inverse_matrix ## Tags matrix inverse, cramer, cramer's rule, adjugate -/ namespace matrix universes u v variables {n : Type u} [fintype n] [decidable_eq n] {α : Type v} open_locale matrix big_operators open equiv equiv.perm finset section update /-- Update, i.e. replace the `i`th column of matrix `A` with the values in `b`. -/ def update_column (A : matrix n n α) (i : n) (b : n → α) : matrix n n α := function.update A i b /-- Update, i.e. replace the `i`th row of matrix `A` with the values in `b`. -/ def update_row (A : matrix n n α) (j : n) (b : n → α) : matrix n n α := λ i, function.update (A i) j (b i) variables {A : matrix n n α} {i j : n} {b : n → α} @[simp] lemma update_column_self : update_column A i b i = b := function.update_same i b A @[simp] lemma update_row_self : update_row A j b i j = b i := function.update_same j (b i) (A i) @[simp] lemma update_column_ne {i' : n} (i_ne : i' ≠ i) : update_column A i b i' = A i' := function.update_noteq i_ne b A @[simp] lemma update_row_ne {j' : n} (j_ne : j' ≠ j) : update_row A j b i j' = A i j' := function.update_noteq j_ne (b i) (A i) lemma update_column_val {i' : n} : update_column A i b i' j = if i' = i then b j else A i' j := begin by_cases i' = i, { rw [h, update_column_self, if_pos rfl] }, { rw [update_column_ne h, if_neg h] } end lemma update_row_val {j' : n} : update_row A j b i j' = if j' = j then b i else A i j' := begin by_cases j' = j, { rw [h, update_row_self, if_pos rfl] }, { rw [update_row_ne h, if_neg h] } end lemma update_column_transpose : update_column Aᵀ i b = (update_row A i b)ᵀ := begin ext i' j, rw [transpose_val, update_column_val, update_row_val], refl end end update section cramer /-! ### `cramer` section Introduce the linear map `cramer` with values defined by `cramer_map`. After defining `cramer_map` and showing it is linear, we will restrict our proofs to using `cramer`. -/ variables [comm_ring α] (A : matrix n n α) (b : n → α) /-- `cramer_map A b i` is the determinant of the matrix `A` with column `i` replaced with `b`, and thus `cramer_map A b` is the vector output by Cramer's rule on `A` and `b`. If `A ⬝ x = b` has a unique solution in `x`, `cramer_map` sends a square matrix `A` and vector `b` to the vector `x` such that `A ⬝ x = b`. Otherwise, the outcome of `cramer_map` is well-defined but not necessarily useful. -/ def cramer_map (i : n) : α := (A.update_column i b).det lemma cramer_map_is_linear (i : n) : is_linear_map α (λ b, cramer_map A b i) := begin have : Π {f : n → n} {i : n} (x : n → α), (∏ i' : n, (update_column A i x)ᵀ (f i') i') = (∏ i' : n, if i' = i then x (f i') else A i' (f i')), { intros, congr, ext i', rw [transpose_val, update_column_val] }, split, { intros x y, repeat { rw [cramer_map, ←det_transpose, det] }, rw [←sum_add_distrib], congr, ext σ, rw [←mul_add ↑↑(sign σ)], congr, repeat { erw [this, finset.prod_ite] }, erw [finset.filter_eq', if_pos (mem_univ i), prod_singleton, prod_singleton, prod_singleton, ←add_mul], refl }, { intros c x, repeat { rw [cramer_map, ←det_transpose, det] }, rw [smul_eq_mul, mul_sum], congr, ext σ, rw [←mul_assoc, mul_comm c, mul_assoc], congr, repeat { erw [this, finset.prod_ite] }, erw [finset.filter_eq', if_pos (mem_univ i), prod_singleton, prod_singleton, mul_assoc], } end lemma cramer_is_linear : is_linear_map α (cramer_map A) := begin split; intros; ext i, { apply (cramer_map_is_linear A i).1 }, { apply (cramer_map_is_linear A i).2 } end /-- The linear map of vectors associated to Cramer's rule. To help the elaborator, we need to make the type `α` an explicit argument to `cramer`. Otherwise, the coercion `⇑(cramer A) : (n → α) → (n → α)` gives an error because it fails to infer the type (even though `α` can be inferred from `A : matrix n n α`). -/ def cramer (α : Type v) [comm_ring α] (A : matrix n n α) : (n → α) →ₗ[α] (n → α) := is_linear_map.mk' (cramer_map A) (cramer_is_linear A) lemma cramer_apply (i : n) : cramer α A b i = (A.update_column i b).det := rfl /-- Applying Cramer's rule to a column of the matrix gives a scaled basis vector. -/ lemma cramer_column_self (i : n) : cramer α A (A i) = (λ j, if i = j then A.det else 0) := begin ext j, rw cramer_apply, by_cases i = j, { -- i = j: this entry should be `A.det` rw [if_pos h, ←h], congr, ext i', by_cases h : i' = i, { rw [h, update_column_self] }, { rw [update_column_ne h]} }, { -- i ≠ j: this entry should be 0 rw [if_neg h], apply det_zero_of_column_eq h, rw [update_column_self, update_column_ne], apply h } end /-- Use linearity of `cramer` to take it out of a summation. -/ lemma sum_cramer {β} (s : finset β) (f : β → n → α) : s.sum (λ x, cramer α A (f x)) = cramer α A (s.sum f) := (linear_map.map_sum (cramer α A)).symm /-- Use linearity of `cramer` and vector evaluation to take `cramer A _ i` out of a summation. -/ lemma sum_cramer_apply {β} (s : finset β) (f : n → β → α) (i : n) : s.sum (λ x, cramer α A (λ j, f j x) i) = cramer α A (λ (j : n), s.sum (f j)) i := calc s.sum (λ x, cramer α A (λ j, f j x) i) = s.sum (λ x, cramer α A (λ j, f j x)) i : (pi.finset_sum_apply i s _).symm ... = cramer α A (λ (j : n), s.sum (f j)) i : by { rw [sum_cramer, cramer_apply], congr, ext j, apply pi.finset_sum_apply } end cramer section adjugate /-! ### `adjugate` section Define the `adjugate` matrix and a few equations. These will hold for any matrix over a commutative ring, while the `inv` section is specifically for invertible matrices. -/ variable [comm_ring α] /-- The adjugate matrix is the transpose of the cofactor matrix. Typically, the cofactor matrix is defined by taking the determinant of minors, i.e. the matrix with a row and column removed. However, the proof of `mul_adjugate` becomes a lot easier if we define the minor as replacing a column with a basis vector, since it allows us to use facts about the `cramer` map. -/ def adjugate (A : matrix n n α) : matrix n n α := λ i, cramer α A (λ j, if i = j then 1 else 0) lemma adjugate_def (A : matrix n n α) : adjugate A = λ i, cramer α A (λ j, if i = j then 1 else 0) := rfl lemma adjugate_val (A : matrix n n α) (i j : n) : adjugate A i j = (A.update_column j (λ j, if i = j then 1 else 0)).det := rfl lemma adjugate_transpose (A : matrix n n α) : (adjugate A)ᵀ = adjugate (Aᵀ) := begin ext i j, rw [transpose_val, adjugate_val, adjugate_val, update_column_transpose, det_transpose], apply finset.sum_congr rfl, intros σ _, congr' 1, by_cases i = σ j, { -- Everything except `(i , j)` (= `(σ j , j)`) is given by A, and the rest is a single `1`. congr; ext j', have := (@equiv.injective _ _ σ j j' : σ j = σ j' → j = j'), rw [update_column_val, update_row_val], finish }, { -- Otherwise, we need to show that there is a `0` somewhere in the product. have : (∏ j' : n, update_row A j (λ (i' : n), ite (i = i') 1 0) (σ j') j') = 0, { apply prod_eq_zero (mem_univ j), rw [update_row_self], exact if_neg h }, rw this, apply prod_eq_zero (mem_univ (σ⁻¹ i)), erw [apply_symm_apply σ i, update_column_self], apply if_neg, intro h', exact h ((symm_apply_eq σ).mp h'.symm) } end lemma mul_adjugate_val (A : matrix n n α) (i j k) : A i k * adjugate A k j = cramer α A (λ j, if k = j then A i k else 0) j := begin erw [←smul_eq_mul, ←pi.smul_apply, ←linear_map.smul], congr, ext, rw [pi.smul_apply, smul_eq_mul, mul_boole], end lemma mul_adjugate (A : matrix n n α) : A ⬝ adjugate A = A.det • 1 := begin ext i j, rw [mul_val, smul_val, one_val, mul_boole], calc univ.sum (λ (k : n), A i k * adjugate A k j) = univ.sum (λ (k : n), cramer α A (λ j, if k = j then A i k else 0) j) : by {congr, ext k, apply mul_adjugate_val A i j k} ... = cramer α A (λ j, univ.sum (λ (k : n), if k = j then A i k else 0)) j : sum_cramer_apply A univ (λ (j k : n), if k = j then A i k else 0) j ... = cramer α A (A i) j : by { rw [cramer_apply], congr, ext, rw [sum_ite_eq' univ x (A i), if_pos (mem_univ _)] } ... = if i = j then det A else 0 : by rw [cramer_column_self] end lemma adjugate_mul (A : matrix n n α) : adjugate A ⬝ A = A.det • 1 := calc adjugate A ⬝ A = (Aᵀ ⬝ (adjugate Aᵀ))ᵀ : by rw [←adjugate_transpose, ←transpose_mul, transpose_transpose] ... = A.det • 1 : by rw [mul_adjugate (Aᵀ), det_transpose, transpose_smul, transpose_one] /-- `det_adjugate_of_cancel` is an auxiliary lemma for computing `(adjugate A).det`, used in `det_adjugate_eq_one` and `det_adjugate_of_is_unit`. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_cancel` covers the case that `det A` cancels on the left of the equation `A.det * b = A.det ^ n`. -/ lemma det_adjugate_of_cancel {A : matrix n n α} (h : ∀ b, A.det * b = A.det ^ fintype.card n → b = A.det ^ (fintype.card n - 1)) : (adjugate A).det = A.det ^ (fintype.card n - 1) := h (adjugate A).det (calc A.det * (adjugate A).det = (A ⬝ adjugate A).det : (det_mul _ _).symm ... = A.det ^ fintype.card n : by simp [mul_adjugate]) lemma adjugate_eq_one_of_card_eq_one {A : matrix n n α} (h : fintype.card n = 1) : adjugate A = 1 := begin ext i j, have univ_eq_i := univ_eq_singleton_of_card_one i h, have univ_eq_j := univ_eq_singleton_of_card_one j h, have i_eq_j : i = j := singleton_inj.mp (by rw [←univ_eq_i, univ_eq_j]), have perm_eq : (univ : finset (perm n)) = {1} := univ_eq_singleton_of_card_one (1 : perm n) (by simp [card_univ, fintype.card_perm, h]), simp [adjugate_val, det, univ_eq_i, perm_eq, i_eq_j] end @[simp] lemma adjugate_zero (h : 1 < fintype.card n) : adjugate (0 : matrix n n α) = 0 := begin ext i j, obtain ⟨j', hj'⟩ : ∃ j', j' ≠ j := fintype.exists_ne_of_one_lt_card h j, apply det_eq_zero_of_column_eq_zero j', intro j'', simp [update_column_ne hj'] end lemma det_adjugate_eq_one {A : matrix n n α} (h : A.det = 1) : (adjugate A).det = 1 := calc (adjugate A).det = A.det ^ (fintype.card n - 1) : det_adjugate_of_cancel (λ b hb, by simpa [h] using hb) ... = 1 : by rw [h, one_pow] /-- `det_adjugate_of_is_unit` gives the formula for `(adjugate A).det` if `A.det` has an inverse. The formula for the determinant of the adjugate of an `n` by `n` matrix `A` is in general `(adjugate A).det = A.det ^ (n - 1)`, but the proof differs in several cases. This lemma `det_adjugate_of_is_unit` covers the case that `det A` has an inverse. -/ lemma det_adjugate_of_is_unit {A : matrix n n α} (h : is_unit A.det) : (adjugate A).det = A.det ^ (fintype.card n - 1) := begin rcases is_unit_iff_exists_inv'.mp h with ⟨a, ha⟩, by_cases card_lt_zero : fintype.card n ≤ 0, { have h : fintype.card n = 0 := by linarith, simp [det_eq_one_of_card_eq_zero h] }, have zero_lt_card : 0 < fintype.card n := by linarith, have n_nonempty : nonempty n := fintype.card_pos_iff.mp zero_lt_card, by_cases card_lt_one : fintype.card n ≤ 1, { have h : fintype.card n = 1 := by linarith, simp [h, adjugate_eq_one_of_card_eq_one h] }, have one_lt_card : 1 < fintype.card n := by linarith, have zero_lt_card_sub_one : 0 < fintype.card n - 1 := (nat.sub_lt_sub_right_iff (refl 1)).mpr one_lt_card, apply det_adjugate_of_cancel, intros b hb, calc b = a * (det A ^ (fintype.card n - 1 + 1)) : by rw [←one_mul b, ←ha, mul_assoc, hb, nat.sub_add_cancel zero_lt_card] ... = a * det A * det A ^ (fintype.card n - 1) : by ring_exp ... = det A ^ (fintype.card n - 1) : by rw [ha, one_mul] end end adjugate section inv /-! ### `inv` section Defines the matrix `nonsing_inv A` and proves it is the inverse matrix of a square matrix `A` as long as `det A` has a multiplicative inverse. -/ variables [comm_ring α] [has_inv α] /-- The inverse of a nonsingular matrix. This is not the most general possible definition, so we don't instantiate `has_inv` (yet). -/ def nonsing_inv (A : matrix n n α) : matrix n n α := (A.det)⁻¹ • adjugate A lemma nonsing_inv_val (A : matrix n n α) (i j : n) : A.nonsing_inv i j = (A.det)⁻¹ * adjugate A i j := rfl lemma transpose_nonsing_inv (A : matrix n n α) : (A.nonsing_inv)ᵀ = (Aᵀ).nonsing_inv := by {ext, simp [transpose_val, nonsing_inv_val, det_transpose, (adjugate_transpose A).symm]} /-- The `nonsing_inv` of `A` is a right inverse. -/ theorem mul_nonsing_inv (A : matrix n n α) (inv_mul_cancel : A.det⁻¹ * A.det = 1) : A ⬝ nonsing_inv A = 1 := by erw [mul_smul, mul_adjugate, smul_smul, inv_mul_cancel, one_smul] /-- The `nonsing_inv` of `A` is a left inverse. -/ theorem nonsing_inv_mul (A : matrix n n α) (inv_mul_cancel : A.det⁻¹ * A.det = 1) : nonsing_inv A ⬝ A = 1 := have inv_mul_cancel' : (det Aᵀ)⁻¹ * det Aᵀ = 1 := by {rw det_transpose, assumption}, calc nonsing_inv A ⬝ A = (Aᵀ ⬝ nonsing_inv (Aᵀ))ᵀ : by rw [transpose_mul, transpose_nonsing_inv, transpose_transpose] ... = 1ᵀ : by rw [mul_nonsing_inv Aᵀ inv_mul_cancel'] ... = 1 : transpose_one end inv end matrix
64df43fde9cc6d02768e68030c1c27f697ac69cc
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/number_theory/basic.lean
e7dd31d3e6aeb0747543553465999d734d9b1471
[ "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
1,425
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Kenny Lau -/ import algebra.geom_sum import ring_theory.ideal.quotient /-! # Basic results in number theory This file should contain basic results in number theory. So far, it only contains the essential lemma in the construction of the ring of Witt vectors. ## Main statement `dvd_sub_pow_of_dvd_sub` proves that for elements `a` and `b` in a commutative ring `R` and for all natural numbers `p` and `k` if `p` divides `a-b` in `R`, then `p ^ (k + 1)` divides `a ^ (p ^ k) - b ^ (p ^ k)`. -/ section open ideal ideal.quotient lemma dvd_sub_pow_of_dvd_sub {R : Type*} [comm_ring R] {p : ℕ} {a b : R} (h : (p : R) ∣ a - b) (k : ℕ) : (p^(k+1) : R) ∣ a^(p^k) - b^(p^k) := begin induction k with k ih, { rwa [pow_one, pow_zero, pow_one, pow_one] }, rw [pow_succ' p k, pow_mul, pow_mul, ← geom_sum₂_mul, pow_succ], refine mul_dvd_mul _ ih, let I : ideal R := span {p}, let f : R →+* ideal.quotient I := mk I, have hp : (p : ideal.quotient I) = 0, { rw [← f.map_nat_cast, eq_zero_iff_mem, mem_span_singleton] }, rw [← mem_span_singleton, ← ideal.quotient.eq] at h, rw [← mem_span_singleton, ← eq_zero_iff_mem, ring_hom.map_geom_sum₂, ring_hom.map_pow, ring_hom.map_pow, h, geom_sum₂_self, hp, zero_mul], end end
10b8bb3c8fc8f5360d1a21dd03f65cf6e41794b8
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/doSeqRightIssue.lean
b050dd9450749e754517dd55c947cd37678d4faf
[ "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
314
lean
-- set_option autoImplicit false universe u variable {α : Type u} variable {β : α → Type v} infix:50 " ≅ " => HEq theorem ex {p₁ p₂ : Sigma (fun a => β a)} (h₁ : p₁.1 = p₂.1) (h : p₁.2 ≅ p₂.2) : p₁ = p₂ := match p₁, p₂, h₁, h with | ⟨_, _⟩, ⟨_, _⟩, rfl, HEq.refl _ => rfl
e964f85d8d1c444a947b2d749557e81cf9370e8a
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/control/lift_auto.lean
1b27a0142604c03abc7bf9fa3f51f7413a634a71
[]
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,317
lean
/- Copyright (c) 2016 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Sebastian Ullrich Classy functions for lifting monadic actions of different shapes. This theory is roughly modeled after the Haskell 'layers' package https://hackage.haskell.org/package/layers-0.1. Please see https://hackage.haskell.org/package/layers-0.1/docs/Documentation-Layers-Overview.html for an exhaustive discussion of the different approaches to lift functions. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.function import Mathlib.Lean3Lib.init.coe import Mathlib.Lean3Lib.init.control.monad universes u v w l u_1 u_2 u_3 u_4 namespace Mathlib /-- A function for lifting a computation from an inner monad to an outer monad. Like [MonadTrans](https://hackage.haskell.org/package/transformers-0.5.5.0/docs/Control-Monad-Trans-Class.html), but `n` does not have to be a monad transformer. Alternatively, an implementation of [MonadLayer](https://hackage.haskell.org/package/layers-0.1/docs/Control-Monad-Layer.html#t:MonadLayer) without `layerInvmap` (so far). -/ class has_monad_lift (m : Type u → Type v) (n : Type u → Type w) where monad_lift : {α : Type u} → m α → n α /-- The reflexive-transitive closure of `has_monad_lift`. `monad_lift` is used to transitively lift monadic computations such as `state_t.get` or `state_t.put s`. Corresponds to [MonadLift](https://hackage.haskell.org/package/layers-0.1/docs/Control-Monad-Layer.html#t:MonadLift). -/ class has_monad_lift_t (m : Type u → Type v) (n : Type u → Type w) where monad_lift : {α : Type u} → m α → n α /-- A coercion that may reduce the need for explicit lifting. Because of [limitations of the current coercion resolution](https://github.com/leanprover/lean/issues/1402), this definition is not marked as a global instance and should be marked locally instead. -/ def has_monad_lift_to_has_coe {m : Type u_1 → Type u_2} {n : Type u_1 → Type u_3} [has_monad_lift_t m n] {α : Type u_1} : has_coe (m α) (n α) := has_coe.mk monad_lift protected instance has_monad_lift_t_trans (m : Type u_1 → Type u_2) (n : Type u_1 → Type u_3) (o : Type u_1 → Type u_4) [has_monad_lift_t m n] [has_monad_lift n o] : has_monad_lift_t m o := has_monad_lift_t.mk fun (α : Type u_1) (ma : m α) => has_monad_lift.monad_lift (monad_lift ma) protected instance has_monad_lift_t_refl (m : Type u_1 → Type u_2) : has_monad_lift_t m m := has_monad_lift_t.mk fun (α : Type u_1) => id @[simp] theorem monad_lift_refl {m : Type u → Type v} {α : Type u} : monad_lift = id := rfl /-- A functor in the category of monads. Can be used to lift monad-transforming functions. Based on pipes' [MFunctor](https://hackage.haskell.org/package/pipes-2.4.0/docs/Control-MFunctor.html), but not restricted to monad transformers. Alternatively, an implementation of [MonadTransFunctor](http://duairc.netsoc.ie/layers-docs/Control-Monad-Layer.html#t:MonadTransFunctor). -/ class monad_functor (m : Type u → Type v) (m' : Type u → Type v) (n : Type u → Type w) (n' : Type u → Type w) where monad_map : {α : Type u} → ({α : Type u} → m α → m' α) → n α → n' α /-- The reflexive-transitive closure of `monad_functor`. `monad_map` is used to transitively lift monad morphisms such as `state_t.zoom`. A generalization of [MonadLiftFunctor](http://duairc.netsoc.ie/layers-docs/Control-Monad-Layer.html#t:MonadLiftFunctor), which can only lift endomorphisms (i.e. m = m', n = n'). -/ class monad_functor_t (m : Type u → Type v) (m' : Type u → Type v) (n : Type u → Type w) (n' : Type u → Type w) where monad_map : {α : Type u} → ({α : Type u} → m α → m' α) → n α → n' α protected instance monad_functor_t_trans (m : Type u_1 → Type u_2) (m' : Type u_1 → Type u_2) (n : Type u_1 → Type u_3) (n' : Type u_1 → Type u_3) (o : Type u_1 → Type u_4) (o' : Type u_1 → Type u_4) [monad_functor_t m m' n n'] [monad_functor n n' o o'] : monad_functor_t m m' o o' := monad_functor_t.mk fun (α : Type u_1) (f : {α : Type u_1} → m α → m' α) => monad_functor.monad_map fun (α : Type u_1) => monad_map f protected instance monad_functor_t_refl (m : Type u_1 → Type u_2) (m' : Type u_1 → Type u_2) : monad_functor_t m m' m m' := monad_functor_t.mk fun (α : Type u_1) (f : {α : Type u_1} → m α → m' α) => f @[simp] theorem monad_map_refl {m : Type u → Type v} {m' : Type u → Type v} (f : {α : Type u} → m α → m' α) {α : Type u} : monad_map f = f := rfl /-- Run a monad stack to completion. `run` should be the composition of the transformers' individual `run` functions. This class mostly saves some typing when using highly nested monad stacks: ``` @[reducible] def my_monad := reader_t my_cfg $ state_t my_state $ except_t my_err id -- def my_monad.run {α : Type} (x : my_monad α) (cfg : my_cfg) (st : my_state) := ((x.run cfg).run st).run def my_monad.run {α : Type} (x : my_monad α) := monad_run.run x ``` -/ class monad_run (out : outParam (Type u → Type v)) (m : Type u → Type v) where run : {α : Type u} → m α → out α end Mathlib