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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
acd7b2a25c0ff6ce3b8396cc882acaade892665d | 624f6f2ae8b3b1adc5f8f67a365c51d5126be45a | /tests/lean/run/inj2.lean | a66125cf71a0a180bfafa17f02ab115bae4ca370 | [
"Apache-2.0"
] | permissive | mhuisi/lean4 | 28d35a4febc2e251c7f05492e13f3b05d6f9b7af | dda44bc47f3e5d024508060dac2bcb59fd12e4c0 | refs/heads/master | 1,621,225,489,283 | 1,585,142,689,000 | 1,585,142,689,000 | 250,590,438 | 0 | 2 | Apache-2.0 | 1,602,443,220,000 | 1,585,327,814,000 | C | UTF-8 | Lean | false | false | 1,039 | lean | universes u v
inductive Vec2 (α : Type u) (β : Type v) : Nat → Type (max u v)
| nil : Vec2 0
| cons : α → β → forall {n}, Vec2 n → Vec2 (n+1)
inductive Fin2 : Nat → Type
| zero (n : Nat) : Fin2 (n+1)
| succ {n : Nat} (s : Fin2 n) : Fin2 (n+1)
new_frontend
theorem test1 {α β} {n} (a₁ a₂ : α) (b₁ b₂ : β) (v w : Vec2 α β n) (h : Vec2.cons a₁ b₁ v = Vec2.cons a₂ b₂ w) : a₁ = a₂ :=
begin
injection h;
assumption
end
theorem test2 {α β} {n} (a₁ a₂ : α) (b₁ b₂ : β) (v w : Vec2 α β n) (h : Vec2.cons a₁ b₁ v = Vec2.cons a₂ b₂ w) : v = w :=
begin
injection h with h1 h2 h3 h4;
assumption
end
theorem test3 {α β} {n} (a₁ a₂ : α) (b₁ b₂ : β) (v w : Vec2 α β n) (h : Vec2.cons a₁ b₁ v = Vec2.cons a₂ b₂ w) : v = w :=
begin
injection h with _ _ _ h4;
exact h4
end
theorem test4 {α} (v : Fin2 0) : α :=
begin
cases v
end
def test5 {α β} {n} (v : Vec2 α β (n+1)) : α :=
begin
cases v with
| cons h1 h2 n tail => h1
end
|
905785b81d6125fbd379ddfa65b0c84f0b16a5ba | 206422fb9edabf63def0ed2aa3f489150fb09ccb | /src/linear_algebra/tensor_product.lean | 7b22d1cbd1683e72e4f177bd901bef43b38a0a72 | [
"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 | 34,376 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Mario Carneiro
-/
import group_theory.congruence
import linear_algebra.basic
/-!
# Tensor product of semimodules over commutative semirings.
This file constructs the tensor product of semimodules over commutative semirings. Given a semiring
`R` and semimodules over it `M` and `N`, the standard construction of the tensor product is
`tensor_product R M N`. It is also a semimodule over `R`.
It comes with a canonical bilinear map `M → N → tensor_product R M N`.
Given any bilinear map `M → N → P`, there is a unique linear map `tensor_product R M N → P` whose
composition with the canonical bilinear map `M → N → tensor_product R M N` is the given bilinear
map `M → N → P`.
We start by proving basic lemmas about bilinear maps.
## Notations
This file uses the localized notation `M ⊗ N` and `M ⊗[R] N` for `tensor_product R M N`, as well
as `m ⊗ₜ n` and `m ⊗ₜ[R] n` for `tensor_product.tmul R m n`.
## Tags
bilinear, tensor, tensor product
-/
namespace linear_map
variables {R : Type*} [comm_semiring R]
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q]
[add_comm_monoid S]
variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S]
include R
variables (R)
/-- Create a bilinear map from a function that is linear in each component. -/
def mk₂ (f : M → N → P)
(H1 : ∀ m₁ m₂ n, f (m₁ + m₂) n = f m₁ n + f m₂ n)
(H2 : ∀ (c:R) m n, f (c • m) n = c • f m n)
(H3 : ∀ m n₁ n₂, f m (n₁ + n₂) = f m n₁ + f m n₂)
(H4 : ∀ (c:R) m n, f m (c • n) = c • f m n) : M →ₗ N →ₗ P :=
⟨λ m, ⟨f m, H3 m, λ c, H4 c m⟩,
λ m₁ m₂, linear_map.ext $ H1 m₁ m₂,
λ c m, linear_map.ext $ H2 c m⟩
variables {R}
@[simp] theorem mk₂_apply
(f : M → N → P) {H1 H2 H3 H4} (m : M) (n : N) :
(mk₂ R f H1 H2 H3 H4 : M →ₗ[R] N →ₗ P) m n = f m n := rfl
theorem ext₂ {f g : M →ₗ[R] N →ₗ[R] P}
(H : ∀ m n, f m n = g m n) : f = g :=
linear_map.ext (λ m, linear_map.ext $ λ n, H m n)
/-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map from `M × N` to
`P`, change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/
def flip (f : M →ₗ[R] N →ₗ[R] P) : N →ₗ M →ₗ P :=
mk₂ R (λ n m, f m n)
(λ n₁ n₂ m, (f m).map_add _ _)
(λ c n m, (f m).map_smul _ _)
(λ n m₁ m₂, by rw f.map_add; refl)
(λ c n m, by rw f.map_smul; refl)
variable (f : M →ₗ[R] N →ₗ[R] P)
@[simp] theorem flip_apply (m : M) (n : N) : flip f n m = f m n := rfl
variables {R}
theorem flip_inj {f g : M →ₗ[R] N →ₗ P} (H : flip f = flip g) : f = g :=
ext₂ $ λ m n, show flip f n m = flip g n m, by rw H
variables (R M N P)
/-- Given a linear map from `M` to linear maps from `N` to `P`, i.e., a bilinear map `M → N → P`,
change the order of variables and get a linear map from `N` to linear maps from `M` to `P`. -/
def lflip : (M →ₗ[R] N →ₗ P) →ₗ[R] N →ₗ M →ₗ P :=
⟨flip, λ _ _, rfl, λ _ _, rfl⟩
variables {R M N P}
@[simp] theorem lflip_apply (m : M) (n : N) : lflip R M N P f n m = f m n := rfl
theorem map_zero₂ (y) : f 0 y = 0 := (flip f y).map_zero
theorem map_neg₂ {R : Type*} [comm_semiring R] {M N P : Type*}
[add_comm_group M] [add_comm_monoid N] [add_comm_group P]
[semimodule R M] [semimodule R N] [semimodule R P] (f : M →ₗ[R] N →ₗ[R] P) (x y) :
f (-x) y = -f x y :=
(flip f y).map_neg _
theorem map_sub₂ {R : Type*} [comm_semiring R] {M N P : Type*}
[add_comm_group M] [add_comm_monoid N] [add_comm_group P]
[semimodule R M] [semimodule R N] [semimodule R P] (f : M →ₗ[R] N →ₗ[R] P) (x y z) :
f (x - y) z = f x z - f y z :=
(flip f z).map_sub _ _
theorem map_add₂ (x₁ x₂ y) : f (x₁ + x₂) y = f x₁ y + f x₂ y := (flip f y).map_add _ _
theorem map_smul₂ (r:R) (x y) : f (r • x) y = r • f x y := (flip f y).map_smul _ _
variables (R P)
/-- Composing a linear map `M → N` and a linear map `N → P` to form a linear map `M → P`. -/
def lcomp (f : M →ₗ[R] N) : (N →ₗ[R] P) →ₗ[R] M →ₗ[R] P :=
flip $ linear_map.comp (flip id) f
variables {R P}
@[simp] theorem lcomp_apply (f : M →ₗ[R] N) (g : N →ₗ P) (x : M) :
lcomp R P f g x = g (f x) := rfl
variables (R M N P)
/-- Composing a linear map `M → N` and a linear map `N → P` to form a linear map `M → P`. -/
def llcomp : (N →ₗ[R] P) →ₗ[R] (M →ₗ[R] N) →ₗ M →ₗ P :=
flip ⟨lcomp R P,
λ f f', ext₂ $ λ g x, g.map_add _ _,
λ c f, ext₂ $ λ g x, g.map_smul _ _⟩
variables {R M N P}
section
@[simp] theorem llcomp_apply (f : N →ₗ[R] P) (g : M →ₗ[R] N) (x : M) :
llcomp R M N P f g x = f (g x) := rfl
end
/-- Composing a linear map `Q → N` and a bilinear map `M → N → P` to
form a bilinear map `M → Q → P`. -/
def compl₂ (g : Q →ₗ N) : M →ₗ Q →ₗ P := (lcomp R _ g).comp f
@[simp] theorem compl₂_apply (g : Q →ₗ[R] N) (m : M) (q : Q) :
f.compl₂ g m q = f m (g q) := rfl
/-- Composing a linear map `P → Q` and a bilinear map `M × N → P` to
form a bilinear map `M → N → Q`. -/
def compr₂ (g : P →ₗ Q) : M →ₗ N →ₗ Q :=
linear_map.comp (llcomp R N P Q g) f
@[simp] theorem compr₂_apply (g : P →ₗ[R] Q) (m : M) (n : N) :
f.compr₂ g m n = g (f m n) := rfl
variables (R M)
/-- Scalar multiplication as a bilinear map `R → M → M`. -/
def lsmul : R →ₗ M →ₗ M :=
mk₂ R (•) add_smul (λ _ _ _, mul_smul _ _ _) smul_add
(λ r s m, by simp only [smul_smul, smul_eq_mul, mul_comm])
variables {R M}
@[simp] theorem lsmul_apply (r : R) (m : M) : lsmul R M r m = r • m := rfl
end linear_map
section semiring
variables {R : Type*} [comm_semiring R]
variables {R' : Type*} [comm_semiring R']
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] [add_comm_monoid Q]
[add_comm_monoid S]
variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S]
variables [semimodule R' M] [semimodule R' N]
include R
variables (M N)
namespace tensor_product
section
-- open free_add_monoid
variables (R)
/-- The relation on `free_add_monoid (M × N)` that generates a congruence whose quotient is
the tensor product. -/
inductive eqv : free_add_monoid (M × N) → free_add_monoid (M × N) → Prop
| of_zero_left : ∀ n : N, eqv (free_add_monoid.of (0, n)) 0
| of_zero_right : ∀ m : M, eqv (free_add_monoid.of (m, 0)) 0
| of_add_left : ∀ (m₁ m₂ : M) (n : N), eqv
(free_add_monoid.of (m₁, n) + free_add_monoid.of (m₂, n)) (free_add_monoid.of (m₁ + m₂, n))
| of_add_right : ∀ (m : M) (n₁ n₂ : N), eqv
(free_add_monoid.of (m, n₁) + free_add_monoid.of (m, n₂)) (free_add_monoid.of (m, n₁ + n₂))
| of_smul : ∀ (r : R) (m : M) (n : N), eqv
(free_add_monoid.of (r • m, n)) (free_add_monoid.of (m, r • n))
| add_comm : ∀ x y, eqv (x + y) (y + x)
end
end tensor_product
variables (R)
/-- The tensor product of two semimodules `M` and `N` over the same commutative semiring `R`.
The localized notations are `M ⊗ N` and `M ⊗[R] N`, accessed by `open_locale tensor_product`. -/
def tensor_product : Type* :=
(add_con_gen (tensor_product.eqv R M N)).quotient
variables {R}
localized "infix ` ⊗ `:100 := tensor_product _" in tensor_product
localized "notation M ` ⊗[`:100 R `] `:0 N:100 := tensor_product R M N" in tensor_product
namespace tensor_product
section module
instance : add_comm_monoid (M ⊗[R] N) :=
{ add_comm := λ x y, add_con.induction_on₂ x y $ λ x y, quotient.sound' $
add_con_gen.rel.of _ _ $ eqv.add_comm _ _,
.. (add_con_gen (tensor_product.eqv R M N)).add_monoid }
instance : inhabited (M ⊗[R] N) := ⟨0⟩
variables (R) {M N}
/-- The canonical function `M → N → M ⊗ N`. The localized notations are `m ⊗ₜ n` and `m ⊗ₜ[R] n`,
accessed by `open_locale tensor_product`. -/
def tmul (m : M) (n : N) : M ⊗[R] N := add_con.mk' _ $ free_add_monoid.of (m, n)
variables {R}
infix ` ⊗ₜ `:100 := tmul _
notation x ` ⊗ₜ[`:100 R `] `:0 y:100 := tmul R x y
@[elab_as_eliminator]
protected theorem induction_on
{C : (M ⊗[R] N) → Prop}
(z : M ⊗[R] N)
(C0 : C 0)
(C1 : ∀ {x y}, C $ x ⊗ₜ[R] y)
(Cp : ∀ {x y}, C x → C y → C (x + y)) : C z :=
add_con.induction_on z $ λ x, free_add_monoid.rec_on x C0 $ λ ⟨m, n⟩ y ih,
by { rw add_con.coe_add, exact Cp C1 ih }
variables (M)
@[simp] lemma zero_tmul (n : N) : (0 : M) ⊗ₜ[R] n = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_left _
variables {M}
lemma add_tmul (m₁ m₂ : M) (n : N) : (m₁ + m₂) ⊗ₜ n = m₁ ⊗ₜ n + m₂ ⊗ₜ[R] n :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_left _ _ _
variables (N)
@[simp] lemma tmul_zero (m : M) : m ⊗ₜ[R] (0 : N) = 0 :=
quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_zero_right _
variables {N}
lemma tmul_add (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ + n₂) = m ⊗ₜ n₁ + m ⊗ₜ[R] n₂ :=
eq.symm $ quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_add_right _ _ _
section
variables (R R' M N)
/--
A typeclass for `has_scalar` structures which can be moved across a tensor product.
This typeclass is generated automatically from a `is_scalar_tower` instance, but exists so that
we can also add an instance for `add_comm_group.int_module`, allowing `z •` to be moved even if
`R` does not support negation.
Note that `semimodule R' (M ⊗[R] N)` is available even without this typeclass on `R'`; it's only
needed if `tensor_product.smul_tmul`, `tensor_product.smul_tmul'`, or `tensor_product.tmul_smul` is
used.
-/
class compatible_smul :=
(smul_tmul : ∀ (r : R') (m : M) (n : N), (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n))
end
/-- Note that this provides the default `compatible_smul R R M N` instance through
`mul_action.is_scalar_tower.left`. -/
@[priority 100]
instance compatible_smul.is_scalar_tower
[has_scalar R' R] [is_scalar_tower R' R M] [is_scalar_tower R' R N] :
compatible_smul R R' M N :=
⟨λ r m n, begin
conv_lhs {rw ← one_smul R m},
conv_rhs {rw ← one_smul R n},
rw [←smul_assoc, ←smul_assoc],
exact (quotient.sound' $ add_con_gen.rel.of _ _ $ eqv.of_smul _ _ _),
end⟩
/-- `smul` can be moved from one side of the product to the other .-/
lemma smul_tmul [compatible_smul R R' M N] (r : R') (m : M) (n : N) :
(r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) :=
compatible_smul.smul_tmul _ _ _
/-- Auxiliary function to defining scalar multiplication on tensor product. -/
def smul.aux {R' : Type*} [has_scalar R' M] (r : R') : free_add_monoid (M × N) →+ M ⊗[R] N :=
free_add_monoid.lift $ λ p : M × N, (r • p.1) ⊗ₜ p.2
theorem smul.aux_of {R' : Type*} [has_scalar R' M] (r : R') (m : M) (n : N) :
smul.aux r (free_add_monoid.of (m, n)) = (r • m) ⊗ₜ[R] n :=
rfl
variables [smul_comm_class R R' M] [smul_comm_class R R' N]
-- Most of the time we want the instance below this one, which is easier for typeclass resolution
-- to find. The `unused_arguments` is from one of the two comm_classes - while we only make use
-- of one, it makes sense to make the API symmetric.
@[nolint unused_arguments]
instance has_scalar' : has_scalar R' (M ⊗[R] N) :=
⟨λ r, (add_con_gen (tensor_product.eqv R M N)).lift (smul.aux r : _ →+ M ⊗[R] N) $
add_con.add_con_gen_le $ λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, smul_zero, zero_tmul]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, smul.aux_of, tmul_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, smul_add, add_tmul]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, smul.aux_of, tmul_add]
| _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $
by rw [smul.aux_of, smul.aux_of, ←smul_comm, smul_tmul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end⟩
instance : has_scalar R (M ⊗[R] N) := tensor_product.has_scalar'
protected theorem smul_zero (r : R') : (r • 0 : M ⊗[R] N) = 0 :=
add_monoid_hom.map_zero _
protected theorem smul_add (r : R') (x y : M ⊗[R] N) :
r • (x + y) = r • x + r • y :=
add_monoid_hom.map_add _ _ _
-- Most of the time we want the instance below this one, which is easier for typeclass resolution
-- to find.
instance semimodule' : semimodule R' (M ⊗[R] N) :=
have ∀ (r : R') (m : M) (n : N), r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n := λ _ _ _, rfl,
{ smul := (•),
smul_add := λ r x y, tensor_product.smul_add r x y,
mul_smul := λ r s x, tensor_product.induction_on x
(by simp_rw tensor_product.smul_zero)
(λ m n, by simp_rw [this, mul_smul])
(λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy] }),
one_smul := λ x, tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(λ m n, by rw [this, one_smul])
(λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy]),
add_smul := λ r s x, tensor_product.induction_on x
(by simp_rw [tensor_product.smul_zero, add_zero])
(λ m n, by simp_rw [this, add_smul, add_tmul])
(λ x y ihx ihy, by { simp_rw tensor_product.smul_add, rw [ihx, ihy, add_add_add_comm] }),
smul_zero := λ r, tensor_product.smul_zero r,
zero_smul := λ x, tensor_product.induction_on x
(by rw tensor_product.smul_zero)
(λ m n, by rw [this, zero_smul, zero_tmul])
(λ x y ihx ihy, by rw [tensor_product.smul_add, ihx, ihy, add_zero]) }
instance : semimodule R (M ⊗[R] N) := tensor_product.semimodule'
-- note that we don't actually need `compatible_smul` here, but we include it for symmetry
-- with `tmul_smul` to avoid exposing our asymmetric definition.
@[nolint unused_arguments]
theorem smul_tmul' [compatible_smul R R' M N] (r : R') (m : M) (n : N) :
r • (m ⊗ₜ[R] n) = (r • m) ⊗ₜ n :=
rfl
@[simp] lemma tmul_smul [compatible_smul R R' M N] (r : R') (x : M) (y : N) :
x ⊗ₜ (r • y) = r • (x ⊗ₜ[R] y) :=
(smul_tmul _ _ _).symm
variables (R M N)
/-- The canonical bilinear map `M → N → M ⊗[R] N`. -/
def mk : M →ₗ N →ₗ M ⊗[R] N :=
linear_map.mk₂ R (⊗ₜ) add_tmul (λ c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul
variables {R M N}
@[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m ⊗ₜ n := rfl
lemma ite_tmul (x₁ : M) (x₂ : N) (P : Prop) [decidable P] :
(if P then x₁ else 0) ⊗ₜ[R] x₂ = if P then x₁ ⊗ₜ x₂ else 0 :=
by { split_ifs; simp }
lemma tmul_ite (x₁ : M) (x₂ : N) (P : Prop) [decidable P] :
x₁ ⊗ₜ[R] (if P then x₂ else 0) = if P then x₁ ⊗ₜ x₂ else 0 :=
by { split_ifs; simp }
section
open_locale big_operators
lemma sum_tmul {α : Type*} (s : finset α) (m : α → M) (n : N) :
(∑ a in s, m a) ⊗ₜ[R] n = ∑ a in s, m a ⊗ₜ[R] n :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, add_tmul, ih], },
end
lemma tmul_sum (m : M) {α : Type*} (s : finset α) (n : α → N) :
m ⊗ₜ[R] (∑ a in s, n a) = ∑ a in s, m ⊗ₜ[R] n a :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp, },
{ simp [finset.sum_insert has, tmul_add, ih], },
end
end
end module
section UMP
variables {M N P Q}
variables (f : M →ₗ[R] N →ₗ[R] P)
/-- Auxiliary function to constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift_aux : (M ⊗[R] N) →+ P :=
(add_con_gen (tensor_product.eqv R M N)).lift (free_add_monoid.lift $ λ p : M × N, f p.1 p.2) $
add_con.add_con_gen_le $ λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, f.map_zero₂]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, free_add_monoid.lift_eval_of, (f m).map_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, f.map_add₂]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, free_add_monoid.lift_eval_of, (f m).map_add]
| _, _, (eqv.of_smul r m n) := (add_con.ker_rel _).2 $
by simp_rw [free_add_monoid.lift_eval_of, f.map_smul₂, (f m).map_smul]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end
lemma lift_aux_tmul (m n) : lift_aux f (m ⊗ₜ n) = f m n :=
zero_add _
variable {f}
@[simp] lemma lift_aux.smul (r : R) (x) : lift_aux f (r • x) = r • lift_aux f x :=
tensor_product.induction_on x (smul_zero _).symm
(λ p q, by rw [← tmul_smul, lift_aux_tmul, lift_aux_tmul, (f p).map_smul])
(λ p q ih1 ih2, by rw [smul_add, (lift_aux f).map_add, ih1, ih2, (lift_aux f).map_add, smul_add])
variable (f)
/-- Constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P` with the property that
its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift : M ⊗ N →ₗ P :=
{ map_smul' := lift_aux.smul,
.. lift_aux f }
variable {f}
@[simp] lemma lift.tmul (x y) : lift f (x ⊗ₜ y) = f x y :=
zero_add _
@[simp] lemma lift.tmul' (x y) : (lift f).1 (x ⊗ₜ y) = f x y :=
lift.tmul _ _
@[ext]
theorem ext {g h : (M ⊗[R] N) →ₗ[R] P}
(H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h :=
linear_map.ext $ λ z, tensor_product.induction_on z (by simp_rw linear_map.map_zero) H $
λ x y ihx ihy, by rw [g.map_add, h.map_add, ihx, ihy]
theorem lift.unique {g : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = f x y) :
g = lift f :=
ext $ λ m n, by rw [H, lift.tmul]
theorem lift_mk : lift (mk R M N) = linear_map.id :=
eq.symm $ lift.unique $ λ x y, rfl
theorem lift_compr₂ (g : P →ₗ Q) : lift (f.compr₂ g) = g.comp (lift f) :=
eq.symm $ lift.unique $ λ x y, by simp
theorem lift_mk_compr₂ (f : M ⊗ N →ₗ P) : lift ((mk R M N).compr₂ f) = f :=
by rw [lift_compr₂ f, lift_mk, linear_map.comp_id]
theorem mk_compr₂_inj {g h : M ⊗ N →ₗ P}
(H : (mk R M N).compr₂ g = (mk R M N).compr₂ h) : g = h :=
by rw [← lift_mk_compr₂ g, H, lift_mk_compr₂]
example : M → N → (M → N → P) → P :=
λ m, flip $ λ f, f m
variables (R M N P)
/-- Linearly constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def uncurry : (M →ₗ[R] N →ₗ[R] P) →ₗ[R] M ⊗[R] N →ₗ[R] P :=
linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp (linear_map.flip linear_map.id)
variables {R M N P}
@[simp] theorem uncurry_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) :
uncurry R M N P f (m ⊗ₜ n) = f m n :=
by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl
variables (R M N P)
/-- A linear equivalence constructing a linear map `M ⊗ N → P` given a bilinear map `M → N → P`
with the property that its composition with the canonical bilinear map `M → N → M ⊗ N` is
the given bilinear map `M → N → P`. -/
def lift.equiv : (M →ₗ N →ₗ P) ≃ₗ (M ⊗ N →ₗ P) :=
{ inv_fun := λ f, (mk R M N).compr₂ f,
left_inv := λ f, linear_map.ext₂ $ λ m n, lift.tmul _ _,
right_inv := λ f, ext $ λ m n, lift.tmul _ _,
.. uncurry R M N P }
/-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to
form a bilinear map `M → N → P`. -/
def lcurry : (M ⊗[R] N →ₗ[R] P) →ₗ[R] M →ₗ[R] N →ₗ[R] P :=
(lift.equiv R M N P).symm
variables {R M N P}
@[simp] theorem lcurry_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) :
lcurry R M N P f m n = f (m ⊗ₜ n) := rfl
/-- Given a linear map `M ⊗ N → P`, compose it with the canonical bilinear map `M → N → M ⊗ N` to
form a bilinear map `M → N → P`. -/
def curry (f : M ⊗ N →ₗ P) : M →ₗ N →ₗ P := lcurry R M N P f
@[simp] theorem curry_apply (f : M ⊗ N →ₗ[R] P) (m : M) (n : N) :
curry f m n = f (m ⊗ₜ n) := rfl
theorem ext_threefold {g h : (M ⊗[R] N) ⊗[R] P →ₗ[R] Q}
(H : ∀ x y z, g ((x ⊗ₜ y) ⊗ₜ z) = h ((x ⊗ₜ y) ⊗ₜ z)) : g = h :=
begin
let e := linear_equiv.to_equiv (lift.equiv R (M ⊗[R] N) P Q),
apply e.symm.injective,
refine ext _,
intros x y,
ext z,
exact H x y z
end
-- We'll need this one for checking the pentagon identity!
theorem ext_fourfold {g h : ((M ⊗[R] N) ⊗[R] P) ⊗[R] Q →ₗ[R] S}
(H : ∀ w x y z, g (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z) = h (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z)) : g = h :=
begin
let e := linear_equiv.to_equiv (lift.equiv R ((M ⊗[R] N) ⊗[R] P) Q S),
apply e.symm.injective,
refine ext_threefold _,
intros x y z,
ext w,
exact H x y z w,
end
end UMP
variables {M N}
section
variables (R M)
/--
The base ring is a left identity for the tensor product of modules, up to linear equivalence.
-/
protected def lid : R ⊗ M ≃ₗ M :=
linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1)
(linear_map.ext $ λ _, by simp)
(ext $ λ r m, by simp; rw [← tmul_smul, ← smul_tmul, smul_eq_mul, mul_one])
end
@[simp] theorem lid_tmul (m : M) (r : R) :
((tensor_product.lid R M) : (R ⊗ M → M)) (r ⊗ₜ m) = r • m :=
begin
dsimp [tensor_product.lid],
simp,
end
@[simp] lemma lid_symm_apply (m : M) :
(tensor_product.lid R M).symm m = 1 ⊗ₜ m := rfl
section
variables (R M N)
/--
The tensor product of modules is commutative, up to linear equivalence.
-/
protected def comm : M ⊗ N ≃ₗ N ⊗ M :=
linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip)
(ext $ λ m n, rfl)
(ext $ λ m n, rfl)
@[simp] theorem comm_tmul (m : M) (n : N) :
(tensor_product.comm R M N) (m ⊗ₜ n) = n ⊗ₜ m := rfl
@[simp] theorem comm_symm_tmul (m : M) (n : N) :
(tensor_product.comm R M N).symm (n ⊗ₜ m) = m ⊗ₜ n := rfl
end
section
variables (R M)
/--
The base ring is a right identity for the tensor product of modules, up to linear equivalence.
-/
protected def rid : M ⊗[R] R ≃ₗ M :=
linear_equiv.trans (tensor_product.comm R M R) (tensor_product.lid R M)
end
@[simp] theorem rid_tmul (m : M) (r : R) :
(tensor_product.rid R M) (m ⊗ₜ r) = r • m :=
begin
dsimp [tensor_product.rid, tensor_product.comm, tensor_product.lid],
simp,
end
@[simp] lemma rid_symm_apply (m : M) :
(tensor_product.rid R M).symm m = m ⊗ₜ 1 := rfl
open linear_map
section
variables (R M N P)
/-- The associator for tensor product of R-modules, as a linear equivalence. -/
protected def assoc : (M ⊗[R] N) ⊗[R] P ≃ₗ[R] M ⊗[R] (N ⊗[R] P) :=
begin
refine linear_equiv.of_linear
(lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _)
(lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _)
(mk_compr₂_inj $ linear_map.ext $ λ m, ext $ λ n p, _)
(mk_compr₂_inj $ flip_inj $ linear_map.ext $ λ p, ext $ λ m n, _);
repeat { rw lift.tmul <|> rw compr₂_apply <|> rw comp_apply <|>
rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|>
rw uncurry_apply <|> rw curry_apply <|> rw id_apply }
end
end
@[simp] theorem assoc_tmul (m : M) (n : N) (p : P) :
(tensor_product.assoc R M N P) ((m ⊗ₜ n) ⊗ₜ p) = m ⊗ₜ (n ⊗ₜ p) := rfl
@[simp] theorem assoc_symm_tmul (m : M) (n : N) (p : P) :
(tensor_product.assoc R M N P).symm (m ⊗ₜ (n ⊗ₜ p)) = (m ⊗ₜ n) ⊗ₜ p := rfl
/-- The tensor product of a pair of linear maps between modules. -/
def map (f : M →ₗ[R] P) (g : N →ₗ Q) : M ⊗ N →ₗ[R] P ⊗ Q :=
lift $ comp (compl₂ (mk _ _ _) g) f
@[simp] theorem map_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (m : M) (n : N) :
map f g (m ⊗ₜ n) = f m ⊗ₜ g n :=
rfl
section
variables {P' Q' : Type*}
variables [add_comm_monoid P'] [semimodule R P']
variables [add_comm_monoid Q'] [semimodule R Q']
lemma map_comp (f₂ : P →ₗ[R] P') (f₁ : M →ₗ[R] P) (g₂ : Q →ₗ[R] Q') (g₁ : N →ₗ[R] Q) :
map (f₂.comp f₁) (g₂.comp g₁) = (map f₂ g₂).comp (map f₁ g₁) :=
by { ext1, simp only [linear_map.comp_apply, map_tmul] }
lemma lift_comp_map (i : P →ₗ[R] Q →ₗ[R] Q') (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(lift i).comp (map f g) = lift ((i.comp f).compl₂ g) :=
by { ext1, simp only [lift.tmul, map_tmul, linear_map.compl₂_apply, linear_map.comp_apply] }
end
/-- If `M` and `P` are linearly equivalent and `N` and `Q` are linearly equivalent
then `M ⊗ N` and `P ⊗ Q` are linearly equivalent. -/
def congr (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) : M ⊗ N ≃ₗ[R] P ⊗ Q :=
linear_equiv.of_linear (map f g) (map f.symm g.symm)
(ext $ λ m n, by simp; simp only [linear_equiv.apply_symm_apply])
(ext $ λ m n, by simp; simp only [linear_equiv.symm_apply_apply])
@[simp] theorem congr_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (m : M) (n : N) :
congr f g (m ⊗ₜ n) = f m ⊗ₜ g n :=
rfl
@[simp] theorem congr_symm_tmul (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) (p : P) (q : Q) :
(congr f g).symm (p ⊗ₜ q) = f.symm p ⊗ₜ g.symm q :=
rfl
end tensor_product
namespace linear_map
variables {R} (M) {N P Q}
/-- `ltensor M f : M ⊗ N →ₗ M ⊗ P` is the natural linear map induced by `f : N →ₗ P`. -/
def ltensor (f : N →ₗ[R] P) : M ⊗ N →ₗ[R] M ⊗ P :=
tensor_product.map id f
/-- `rtensor f M : N₁ ⊗ M →ₗ N₂ ⊗ M` is the natural linear map induced by `f : N₁ →ₗ N₂`. -/
def rtensor (f : N →ₗ[R] P) : N ⊗ M →ₗ[R] P ⊗ M :=
tensor_product.map f id
variables (g : P →ₗ[R] Q) (f : N →ₗ[R] P)
@[simp] lemma ltensor_tmul (m : M) (n : N) : f.ltensor M (m ⊗ₜ n) = m ⊗ₜ (f n) := rfl
@[simp] lemma rtensor_tmul (m : M) (n : N) : f.rtensor M (n ⊗ₜ m) = (f n) ⊗ₜ m := rfl
open tensor_product
/-- `ltensor_hom M` is the natural linear map that sends a linear map `f : N →ₗ P` to `M ⊗ f`. -/
def ltensor_hom : (N →ₗ[R] P) →ₗ[R] (M ⊗[R] N →ₗ[R] M ⊗[R] P) :=
{ to_fun := ltensor M,
map_add' := λ f g, by { ext x y, simp only [add_apply, ltensor_tmul, tmul_add] },
map_smul' := λ r f, by { ext x y, simp only [tmul_smul, smul_apply, ltensor_tmul] } }
/-- `rtensor_hom M` is the natural linear map that sends a linear map `f : N →ₗ P` to `M ⊗ f`. -/
def rtensor_hom : (N →ₗ[R] P) →ₗ[R] (N ⊗[R] M →ₗ[R] P ⊗[R] M) :=
{ to_fun := λ f, f.rtensor M,
map_add' := λ f g, by { ext x y, simp only [add_apply, rtensor_tmul, add_tmul] },
map_smul' := λ r f, by { ext x y, simp only [smul_tmul, tmul_smul, smul_apply, rtensor_tmul] } }
@[simp] lemma coe_ltensor_hom :
(ltensor_hom M : (N →ₗ[R] P) → (M ⊗[R] N →ₗ[R] M ⊗[R] P)) = ltensor M := rfl
@[simp] lemma coe_rtensor_hom :
(rtensor_hom M : (N →ₗ[R] P) → (N ⊗[R] M →ₗ[R] P ⊗[R] M)) = rtensor M := rfl
@[simp] lemma ltensor_add (f g : N →ₗ[R] P) : (f + g).ltensor M = f.ltensor M + g.ltensor M :=
(ltensor_hom M).map_add f g
@[simp] lemma rtensor_add (f g : N →ₗ[R] P) : (f + g).rtensor M = f.rtensor M + g.rtensor M :=
(rtensor_hom M).map_add f g
@[simp] lemma ltensor_zero : ltensor M (0 : N →ₗ[R] P) = 0 :=
(ltensor_hom M).map_zero
@[simp] lemma rtensor_zero : rtensor M (0 : N →ₗ[R] P) = 0 :=
(rtensor_hom M).map_zero
@[simp] lemma ltensor_smul (r : R) (f : N →ₗ[R] P) : (r • f).ltensor M = r • (f.ltensor M) :=
(ltensor_hom M).map_smul r f
@[simp] lemma rtensor_smul (r : R) (f : N →ₗ[R] P) : (r • f).rtensor M = r • (f.rtensor M) :=
(rtensor_hom M).map_smul r f
lemma ltensor_comp : (g.comp f).ltensor M = (g.ltensor M).comp (f.ltensor M) :=
by { ext m n, simp only [comp_apply, ltensor_tmul] }
lemma rtensor_comp : (g.comp f).rtensor M = (g.rtensor M).comp (f.rtensor M) :=
by { ext m n, simp only [comp_apply, rtensor_tmul] }
variables (N)
@[simp] lemma ltensor_id : (id : N →ₗ[R] N).ltensor M = id :=
by { ext m n, simp only [id_coe, id.def, ltensor_tmul] }
@[simp] lemma rtensor_id : (id : N →ₗ[R] N).rtensor M = id :=
by { ext m n, simp only [id_coe, id.def, rtensor_tmul] }
variables {N}
@[simp] lemma ltensor_comp_rtensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(g.ltensor P).comp (f.rtensor N) = map f g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma rtensor_comp_ltensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(f.rtensor Q).comp (g.ltensor M) = map f g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma map_comp_rtensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (f' : S →ₗ[R] M) :
(map f g).comp (f'.rtensor _) = map (f.comp f') g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma map_comp_ltensor (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (g' : S →ₗ[R] N) :
(map f g).comp (g'.ltensor _) = map f (g.comp g') :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma rtensor_comp_map (f' : P →ₗ[R] S) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(f'.rtensor _).comp (map f g) = map (f'.comp f) g :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
@[simp] lemma ltensor_comp_map (g' : Q →ₗ[R] S) (f : M →ₗ[R] P) (g : N →ₗ[R] Q) :
(g'.ltensor _).comp (map f g) = map f (g'.comp g) :=
by simp only [ltensor, rtensor, ← map_comp, id_comp, comp_id]
end linear_map
end semiring
section ring
variables {R : Type*} [comm_semiring R]
variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*}
variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q]
[add_comm_group S]
variables [semimodule R M] [semimodule R N] [semimodule R P] [semimodule R Q] [semimodule R S]
namespace tensor_product
open_locale tensor_product
open linear_map
variables (R)
/-- Auxiliary function to defining negation multiplication on tensor product. -/
def neg.aux : free_add_monoid (M × N) →+ M ⊗[R] N :=
free_add_monoid.lift $ λ p : M × N, (-p.1) ⊗ₜ p.2
variables {R}
theorem neg.aux_of (m : M) (n : N) :
neg.aux R (free_add_monoid.of (m, n)) = (-m) ⊗ₜ[R] n :=
rfl
instance : has_neg (M ⊗[R] N) :=
{ neg := (add_con_gen (tensor_product.eqv R M N)).lift (neg.aux R) $ add_con.add_con_gen_le $
λ x y hxy, match x, y, hxy with
| _, _, (eqv.of_zero_left n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, neg.aux_of, neg_zero, zero_tmul]
| _, _, (eqv.of_zero_right m) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_zero, neg.aux_of, tmul_zero]
| _, _, (eqv.of_add_left m₁ m₂ n) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, neg.aux_of, neg_add, add_tmul]
| _, _, (eqv.of_add_right m n₁ n₂) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, neg.aux_of, tmul_add]
| _, _, (eqv.of_smul s m n) := (add_con.ker_rel _).2 $
by simp_rw [neg.aux_of, tmul_smul s, smul_tmul', smul_neg]
| _, _, (eqv.add_comm x y) := (add_con.ker_rel _).2 $
by simp_rw [add_monoid_hom.map_add, add_comm]
end }
instance : add_comm_group (M ⊗[R] N) :=
{ neg := has_neg.neg,
sub := _,
sub_eq_add_neg := λ _ _, rfl,
add_left_neg := λ x, tensor_product.induction_on x
(by { rw [add_zero], apply (neg.aux R).map_zero, })
(λ x y, by { convert (add_tmul (-x) x y).symm, rw [add_left_neg, zero_tmul], })
(λ x y hx hy, by {
unfold has_neg.neg sub_neg_monoid.neg,
rw add_monoid_hom.map_add,
ac_change (-x + x) + (-y + y) = 0,
rw [hx, hy, add_zero], }),
..(infer_instance : add_comm_monoid (M ⊗[R] N)) }
lemma neg_tmul (m : M) (n : N) : (-m) ⊗ₜ n = -(m ⊗ₜ[R] n) := rfl
lemma tmul_neg (m : M) (n : N) : m ⊗ₜ (-n) = -(m ⊗ₜ[R] n) := (mk R M N _).map_neg _
lemma tmul_sub (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ - n₂) = (m ⊗ₜ[R] n₁) - (m ⊗ₜ[R] n₂) :=
(mk R M N _).map_sub _ _
lemma sub_tmul (m₁ m₂ : M) (n : N) : (m₁ - m₂) ⊗ₜ n = (m₁ ⊗ₜ[R] n) - (m₂ ⊗ₜ[R] n) :=
(mk R M N).map_sub₂ _ _ _
/--
While the tensor product will automatically inherit a ℤ-module structure from
`add_comm_group.int_module`, that structure won't be compatible with lemmas like `tmul_smul` unless
we use a `ℤ-module` instance provided by `tensor_product.semimodule'`.
When `R` is a `ring` we get the required `tensor_product.compatible_smul` instance through
`is_scalar_tower`, but when it is only a `semiring` we need to build it from scratch.
The instance diamond in `compatible_smul` doesn't matter because it's in `Prop`.
-/
instance compatible_smul.int [semimodule ℤ M] [semimodule ℤ N] : compatible_smul R ℤ M N :=
⟨λ r m n, int.induction_on r
(by simp)
(λ r ih, by simpa [add_smul, tmul_add, add_tmul] using ih)
(λ r ih, by simpa [sub_smul, tmul_sub, sub_tmul] using ih)⟩
end tensor_product
namespace linear_map
@[simp] lemma ltensor_sub (f g : N →ₗ[R] P) : (f - g).ltensor M = f.ltensor M - g.ltensor M :=
by simp only [← coe_ltensor_hom, map_sub]
@[simp] lemma rtensor_sub (f g : N →ₗ[R] P) : (f - g).rtensor M = f.rtensor M - g.rtensor M :=
by simp only [← coe_rtensor_hom, map_sub]
@[simp] lemma ltensor_neg (f : N →ₗ[R] P) : (-f).ltensor M = -(f.ltensor M) :=
by simp only [← coe_ltensor_hom, map_neg]
@[simp] lemma rtensor_neg (f : N →ₗ[R] P) : (-f).rtensor M = -(f.rtensor M) :=
by simp only [← coe_rtensor_hom, map_neg]
end linear_map
end ring
|
dbe04f4daff5a87d2a0ddb0720c577cac73f9468 | a4673261e60b025e2c8c825dfa4ab9108246c32e | /src/Init/Data/Char/Basic.lean | 0712b64c26cd2ca46b614a33647658e0baba2358 | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,096 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
prelude
import Init.Data.UInt
@[inline, reducible] def isValidChar (n : UInt32) : Prop :=
n < 0xd800 ∨ (0xdfff < n ∧ n < 0x110000)
instance : SizeOf Char := ⟨fun c => c.val.toNat⟩
namespace Char
protected def Less (a b : Char) : Prop := a.val < b.val
protected def LessEq (a b : Char) : Prop := a.val ≤ b.val
instance : HasLess Char := ⟨Char.Less⟩
instance : HasLessEq Char := ⟨Char.LessEq⟩
protected def lt (a b : Char) : Bool := a.val < b.val
instance (a b : Char) : Decidable (a < b) :=
UInt32.decLt _ _
instance (a b : Char) : Decidable (a ≤ b) :=
UInt32.decLe _ _
abbrev isValidCharNat (n : Nat) : Prop :=
n < 0xd800 ∨ (0xdfff < n ∧ n < 0x110000)
theorem isValidUInt32 (n : Nat) (h : isValidCharNat n) : n < UInt32.size := by
match h with
| Or.inl h =>
apply Nat.ltTrans h
exact decide!
| Or.inr ⟨h₁, h₂⟩ =>
apply Nat.ltTrans h₂
exact decide!
theorem isValidCharOfValidNat (n : Nat) (h : isValidCharNat n) : isValidChar (UInt32.ofNat' n (isValidUInt32 n h)) :=
match h with
| Or.inl h => Or.inl h
| Or.inr ⟨h₁, h₂⟩ => Or.inr ⟨h₁, h₂⟩
theorem isValidChar0 : isValidChar 0 :=
Or.inl decide!
@[inline] def toNat (c : Char) : Nat :=
c.val.toNat
instance : Inhabited Char :=
⟨'A'⟩
def isWhitespace (c : Char) : Bool :=
c = ' ' || c = '\t' || c = '\r' || c = '\n'
def isUpper (c : Char) : Bool :=
c.val ≥ 65 && c.val ≤ 90
def isLower (c : Char) : Bool :=
c.val ≥ 97 && c.val ≤ 122
def isAlpha (c : Char) : Bool :=
c.isUpper || c.isLower
def isDigit (c : Char) : Bool :=
c.val ≥ 48 && c.val ≤ 57
def isAlphanum (c : Char) : Bool :=
c.isAlpha || c.isDigit
def toLower (c : Char) : Char :=
let n := toNat c;
if n >= 65 ∧ n <= 90 then ofNat (n + 32) else c
def toUpper (c : Char) : Char :=
let n := toNat c;
if n >= 97 ∧ n <= 122 then ofNat (n - 32) else c
end Char
|
3973dc124e7d7a8b79bab1b3b9a136d1a10ac830 | bb31430994044506fa42fd667e2d556327e18dfe | /src/topology/instances/ennreal.lean | 8d5af6c912c1682ee79ca86aa89ee5cc01d9743d | [
"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 | 73,293 | 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 topology.instances.nnreal
import topology.algebra.order.monotone_continuity
import analysis.normed.group.basic
/-!
# Extended non-negative reals
-/
noncomputable theory
open classical set filter metric
open_locale classical topological_space ennreal nnreal big_operators filter
variables {α : Type*} {β : Type*} {γ : Type*}
namespace ennreal
variables {a b c d : ℝ≥0∞} {r p q : ℝ≥0}
variables {x y z : ℝ≥0∞} {ε ε₁ ε₂ : ℝ≥0∞} {s : set ℝ≥0∞}
section topological_space
open topological_space
/-- Topology on `ℝ≥0∞`.
Note: this is different from the `emetric_space` topology. The `emetric_space` topology has
`is_open {⊤}`, while this topology doesn't have singleton elements. -/
instance : topological_space ℝ≥0∞ := preorder.topology ℝ≥0∞
instance : order_topology ℝ≥0∞ := ⟨rfl⟩
instance : t2_space ℝ≥0∞ := by apply_instance -- short-circuit type class inference
instance : normal_space ℝ≥0∞ := normal_of_compact_t2
instance : second_countable_topology ℝ≥0∞ :=
order_iso_unit_interval_birational.to_homeomorph.embedding.second_countable_topology
lemma embedding_coe : embedding (coe : ℝ≥0 → ℝ≥0∞) :=
⟨⟨begin
refine le_antisymm _ _,
{ rw [@order_topology.topology_eq_generate_intervals ℝ≥0∞ _,
← coinduced_le_iff_le_induced],
refine le_generate_from (assume s ha, _),
rcases ha with ⟨a, rfl | rfl⟩,
show is_open {b : ℝ≥0 | a < ↑b},
{ cases a; simp [none_eq_top, some_eq_coe, is_open_lt'] },
show is_open {b : ℝ≥0 | ↑b < a},
{ cases a; simp [none_eq_top, some_eq_coe, is_open_gt', is_open_const] } },
{ rw [@order_topology.topology_eq_generate_intervals ℝ≥0 _],
refine le_generate_from (assume s ha, _),
rcases ha with ⟨a, rfl | rfl⟩,
exact ⟨Ioi a, is_open_Ioi, by simp [Ioi]⟩,
exact ⟨Iio a, is_open_Iio, by simp [Iio]⟩ }
end⟩,
assume a b, coe_eq_coe.1⟩
lemma is_open_ne_top : is_open {a : ℝ≥0∞ | a ≠ ⊤} := is_open_ne
lemma is_open_Ico_zero : is_open (Ico 0 b) := by { rw ennreal.Ico_eq_Iio, exact is_open_Iio}
lemma open_embedding_coe : open_embedding (coe : ℝ≥0 → ℝ≥0∞) :=
⟨embedding_coe, by { convert is_open_ne_top, ext (x|_); simp [none_eq_top, some_eq_coe] }⟩
lemma coe_range_mem_nhds : range (coe : ℝ≥0 → ℝ≥0∞) ∈ 𝓝 (r : ℝ≥0∞) :=
is_open.mem_nhds open_embedding_coe.open_range $ mem_range_self _
@[norm_cast] lemma tendsto_coe {f : filter α} {m : α → ℝ≥0} {a : ℝ≥0} :
tendsto (λa, (m a : ℝ≥0∞)) f (𝓝 ↑a) ↔ tendsto m f (𝓝 a) :=
embedding_coe.tendsto_nhds_iff.symm
lemma continuous_coe : continuous (coe : ℝ≥0 → ℝ≥0∞) :=
embedding_coe.continuous
lemma continuous_coe_iff {α} [topological_space α] {f : α → ℝ≥0} :
continuous (λa, (f a : ℝ≥0∞)) ↔ continuous f :=
embedding_coe.continuous_iff.symm
lemma nhds_coe {r : ℝ≥0} : 𝓝 (r : ℝ≥0∞) = (𝓝 r).map coe :=
(open_embedding_coe.map_nhds_eq r).symm
lemma tendsto_nhds_coe_iff {α : Type*} {l : filter α} {x : ℝ≥0} {f : ℝ≥0∞ → α} :
tendsto f (𝓝 ↑x) l ↔ tendsto (f ∘ coe : ℝ≥0 → α) (𝓝 x) l :=
show _ ≤ _ ↔ _ ≤ _, by rw [nhds_coe, filter.map_map]
lemma continuous_at_coe_iff {α : Type*} [topological_space α] {x : ℝ≥0} {f : ℝ≥0∞ → α} :
continuous_at f (↑x) ↔ continuous_at (f ∘ coe : ℝ≥0 → α) x :=
tendsto_nhds_coe_iff
lemma nhds_coe_coe {r p : ℝ≥0} :
𝓝 ((r : ℝ≥0∞), (p : ℝ≥0∞)) = (𝓝 (r, p)).map (λp:ℝ≥0×ℝ≥0, (p.1, p.2)) :=
((open_embedding_coe.prod open_embedding_coe).map_nhds_eq (r, p)).symm
lemma continuous_of_real : continuous ennreal.of_real :=
(continuous_coe_iff.2 continuous_id).comp continuous_real_to_nnreal
lemma tendsto_of_real {f : filter α} {m : α → ℝ} {a : ℝ} (h : tendsto m f (𝓝 a)) :
tendsto (λa, ennreal.of_real (m a)) f (𝓝 (ennreal.of_real a)) :=
tendsto.comp (continuous.tendsto continuous_of_real _) h
lemma tendsto_to_nnreal {a : ℝ≥0∞} (ha : a ≠ ⊤) :
tendsto ennreal.to_nnreal (𝓝 a) (𝓝 a.to_nnreal) :=
begin
lift a to ℝ≥0 using ha,
rw [nhds_coe, tendsto_map'_iff],
exact tendsto_id
end
lemma eventually_eq_of_to_real_eventually_eq {l : filter α} {f g : α → ℝ≥0∞}
(hfi : ∀ᶠ x in l, f x ≠ ∞) (hgi : ∀ᶠ x in l, g x ≠ ∞)
(hfg : (λ x, (f x).to_real) =ᶠ[l] (λ x, (g x).to_real)) :
f =ᶠ[l] g :=
begin
filter_upwards [hfi, hgi, hfg] with _ hfx hgx _,
rwa ← ennreal.to_real_eq_to_real hfx hgx,
end
lemma continuous_on_to_nnreal : continuous_on ennreal.to_nnreal {a | a ≠ ∞} :=
λ a ha, continuous_at.continuous_within_at (tendsto_to_nnreal ha)
lemma tendsto_to_real {a : ℝ≥0∞} (ha : a ≠ ⊤) : tendsto ennreal.to_real (𝓝 a) (𝓝 a.to_real) :=
nnreal.tendsto_coe.2 $ tendsto_to_nnreal ha
/-- The set of finite `ℝ≥0∞` numbers is homeomorphic to `ℝ≥0`. -/
def ne_top_homeomorph_nnreal : {a | a ≠ ∞} ≃ₜ ℝ≥0 :=
{ continuous_to_fun := continuous_on_iff_continuous_restrict.1 continuous_on_to_nnreal,
continuous_inv_fun := continuous_coe.subtype_mk _,
.. ne_top_equiv_nnreal }
/-- The set of finite `ℝ≥0∞` numbers is homeomorphic to `ℝ≥0`. -/
def lt_top_homeomorph_nnreal : {a | a < ∞} ≃ₜ ℝ≥0 :=
by refine (homeomorph.set_congr $ set.ext $ λ x, _).trans ne_top_homeomorph_nnreal;
simp only [mem_set_of_eq, lt_top_iff_ne_top]
lemma nhds_top : 𝓝 ∞ = ⨅ a ≠ ∞, 𝓟 (Ioi a) :=
nhds_top_order.trans $ by simp [lt_top_iff_ne_top, Ioi]
lemma nhds_top' : 𝓝 ∞ = ⨅ r : ℝ≥0, 𝓟 (Ioi r) :=
nhds_top.trans $ infi_ne_top _
lemma nhds_top_basis : (𝓝 ∞).has_basis (λ a, a < ∞) (λ a, Ioi a) := nhds_top_basis
lemma tendsto_nhds_top_iff_nnreal {m : α → ℝ≥0∞} {f : filter α} :
tendsto m f (𝓝 ⊤) ↔ ∀ x : ℝ≥0, ∀ᶠ a in f, ↑x < m a :=
by simp only [nhds_top', tendsto_infi, tendsto_principal, mem_Ioi]
lemma tendsto_nhds_top_iff_nat {m : α → ℝ≥0∞} {f : filter α} :
tendsto m f (𝓝 ⊤) ↔ ∀ n : ℕ, ∀ᶠ a in f, ↑n < m a :=
tendsto_nhds_top_iff_nnreal.trans ⟨λ h n, by simpa only [ennreal.coe_nat] using h n,
λ h x, let ⟨n, hn⟩ := exists_nat_gt x in
(h n).mono (λ y, lt_trans $ by rwa [← ennreal.coe_nat, coe_lt_coe])⟩
lemma tendsto_nhds_top {m : α → ℝ≥0∞} {f : filter α}
(h : ∀ n : ℕ, ∀ᶠ a in f, ↑n < m a) : tendsto m f (𝓝 ⊤) :=
tendsto_nhds_top_iff_nat.2 h
lemma tendsto_nat_nhds_top : tendsto (λ n : ℕ, ↑n) at_top (𝓝 ∞) :=
tendsto_nhds_top $ λ n, mem_at_top_sets.2
⟨n+1, λ m hm, ennreal.coe_nat_lt_coe_nat.2 $ nat.lt_of_succ_le hm⟩
@[simp, norm_cast] lemma tendsto_coe_nhds_top {f : α → ℝ≥0} {l : filter α} :
tendsto (λ x, (f x : ℝ≥0∞)) l (𝓝 ∞) ↔ tendsto f l at_top :=
by rw [tendsto_nhds_top_iff_nnreal, at_top_basis_Ioi.tendsto_right_iff];
[simp, apply_instance, apply_instance]
lemma tendsto_of_real_at_top : tendsto ennreal.of_real at_top (𝓝 ∞) :=
tendsto_coe_nhds_top.2 tendsto_real_to_nnreal_at_top
lemma nhds_zero : 𝓝 (0 : ℝ≥0∞) = ⨅a ≠ 0, 𝓟 (Iio a) :=
nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot, Iio]
lemma nhds_zero_basis : (𝓝 (0 : ℝ≥0∞)).has_basis (λ a : ℝ≥0∞, 0 < a) (λ a, Iio a) := nhds_bot_basis
lemma nhds_zero_basis_Iic : (𝓝 (0 : ℝ≥0∞)).has_basis (λ a : ℝ≥0∞, 0 < a) Iic := nhds_bot_basis_Iic
@[instance] lemma nhds_within_Ioi_coe_ne_bot {r : ℝ≥0} : (𝓝[>] (r : ℝ≥0∞)).ne_bot :=
nhds_within_Ioi_self_ne_bot' ⟨⊤, ennreal.coe_lt_top⟩
@[instance] lemma nhds_within_Ioi_zero_ne_bot : (𝓝[>] (0 : ℝ≥0∞)).ne_bot :=
nhds_within_Ioi_coe_ne_bot
-- using Icc because
-- • don't have 'Ioo (x - ε) (x + ε) ∈ 𝓝 x' unless x > 0
-- • (x - y ≤ ε ↔ x ≤ ε + y) is true, while (x - y < ε ↔ x < ε + y) is not
lemma Icc_mem_nhds (xt : x ≠ ⊤) (ε0 : ε ≠ 0) : Icc (x - ε) (x + ε) ∈ 𝓝 x :=
begin
rw _root_.mem_nhds_iff,
by_cases x0 : x = 0,
{ use Iio (x + ε),
have : Iio (x + ε) ⊆ Icc (x - ε) (x + ε), assume a, rw x0, simpa using le_of_lt,
use this, exact ⟨is_open_Iio, mem_Iio_self_add xt ε0⟩ },
{ use Ioo (x - ε) (x + ε), use Ioo_subset_Icc_self,
exact ⟨is_open_Ioo, mem_Ioo_self_sub_add xt x0 ε0 ε0 ⟩ }
end
lemma nhds_of_ne_top (xt : x ≠ ⊤) : 𝓝 x = ⨅ ε > 0, 𝓟 (Icc (x - ε) (x + ε)) :=
begin
refine le_antisymm _ _,
-- first direction
simp only [le_infi_iff, le_principal_iff], assume ε ε0, exact Icc_mem_nhds xt ε0.lt.ne',
-- second direction
rw nhds_generate_from, refine le_infi (assume s, le_infi $ assume hs, _),
rcases hs with ⟨xs, ⟨a, (rfl : s = Ioi a)|(rfl : s = Iio a)⟩⟩,
{ rcases exists_between xs with ⟨b, ab, bx⟩,
have xb_pos : 0 < x - b := tsub_pos_iff_lt.2 bx,
have xxb : x - (x - b) = b := sub_sub_cancel xt bx.le,
refine infi_le_of_le (x - b) (infi_le_of_le xb_pos _),
simp only [mem_principal, le_principal_iff],
assume y, rintros ⟨h₁, h₂⟩, rw xxb at h₁, calc a < b : ab ... ≤ y : h₁ },
{ rcases exists_between xs with ⟨b, xb, ba⟩,
have bx_pos : 0 < b - x := tsub_pos_iff_lt.2 xb,
have xbx : x + (b - x) = b := add_tsub_cancel_of_le xb.le,
refine infi_le_of_le (b - x) (infi_le_of_le bx_pos _),
simp only [mem_principal, le_principal_iff],
assume y, rintros ⟨h₁, h₂⟩, rw xbx at h₂, calc y ≤ b : h₂ ... < a : ba },
end
/-- Characterization of neighborhoods for `ℝ≥0∞` numbers. See also `tendsto_order`
for a version with strict inequalities. -/
protected theorem tendsto_nhds {f : filter α} {u : α → ℝ≥0∞} {a : ℝ≥0∞} (ha : a ≠ ⊤) :
tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, (u x) ∈ Icc (a - ε) (a + ε) :=
by simp only [nhds_of_ne_top ha, tendsto_infi, tendsto_principal, mem_Icc]
protected lemma tendsto_nhds_zero {f : filter α} {u : α → ℝ≥0∞} :
tendsto u f (𝓝 0) ↔ ∀ ε > 0, ∀ᶠ x in f, u x ≤ ε :=
begin
rw ennreal.tendsto_nhds zero_ne_top,
simp only [true_and, zero_tsub, zero_le, zero_add, set.mem_Icc],
end
protected lemma tendsto_at_top [nonempty β] [semilattice_sup β] {f : β → ℝ≥0∞} {a : ℝ≥0∞}
(ha : a ≠ ⊤) : tendsto f at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, (f n) ∈ Icc (a - ε) (a + ε) :=
by simp only [ennreal.tendsto_nhds ha, mem_at_top_sets, mem_set_of_eq, filter.eventually]
instance : has_continuous_add ℝ≥0∞ :=
begin
refine ⟨continuous_iff_continuous_at.2 _⟩,
rintro ⟨(_|a), b⟩,
{ exact tendsto_nhds_top_mono' continuous_at_fst (λ p, le_add_right le_rfl) },
rcases b with (_|b),
{ exact tendsto_nhds_top_mono' continuous_at_snd (λ p, le_add_left le_rfl) },
simp only [continuous_at, some_eq_coe, nhds_coe_coe, ← coe_add, tendsto_map'_iff, (∘),
tendsto_coe, tendsto_add]
end
protected lemma tendsto_at_top_zero [hβ : nonempty β] [semilattice_sup β] {f : β → ℝ≥0∞} :
filter.at_top.tendsto f (𝓝 0) ↔ ∀ ε > 0, ∃ N, ∀ n ≥ N, f n ≤ ε :=
begin
rw ennreal.tendsto_at_top zero_ne_top,
{ simp_rw [set.mem_Icc, zero_add, zero_tsub, zero_le _, true_and], },
{ exact hβ, },
end
lemma tendsto_sub {a b : ℝ≥0∞} (h : a ≠ ∞ ∨ b ≠ ∞) :
tendsto (λ p : ℝ≥0∞ × ℝ≥0∞, p.1 - p.2) (𝓝 (a, b)) (𝓝 (a - b)) :=
begin
cases a; cases b,
{ simp only [eq_self_iff_true, not_true, ne.def, none_eq_top, or_self] at h, contradiction },
{ simp only [some_eq_coe, with_top.top_sub_coe, none_eq_top],
apply tendsto_nhds_top_iff_nnreal.2 (λ n, _),
rw [nhds_prod_eq, eventually_prod_iff],
refine ⟨λ z, ((n + (b + 1)) : ℝ≥0∞) < z,
Ioi_mem_nhds (by simp only [one_lt_top, add_lt_top, coe_lt_top, and_self]),
λ z, z < b + 1, Iio_mem_nhds ((ennreal.lt_add_right coe_ne_top one_ne_zero)),
λ x hx y hy, _⟩,
dsimp,
rw lt_tsub_iff_right,
have : ((n : ℝ≥0∞) + y) + (b + 1) < x + (b + 1) := calc
((n : ℝ≥0∞) + y) + (b + 1) = ((n : ℝ≥0∞) + (b + 1)) + y : by abel
... < x + (b + 1) : ennreal.add_lt_add hx hy,
exact lt_of_add_lt_add_right this },
{ simp only [some_eq_coe, with_top.sub_top, none_eq_top],
suffices H : ∀ᶠ (p : ℝ≥0∞ × ℝ≥0∞) in 𝓝 (a, ∞), 0 = p.1 - p.2,
from tendsto_const_nhds.congr' H,
rw [nhds_prod_eq, eventually_prod_iff],
refine ⟨λ z, z < a + 1, Iio_mem_nhds (ennreal.lt_add_right coe_ne_top one_ne_zero),
λ z, (a : ℝ≥0∞) + 1 < z,
Ioi_mem_nhds (by simp only [one_lt_top, add_lt_top, coe_lt_top, and_self]),
λ x hx y hy, _⟩,
rw eq_comm,
simp only [tsub_eq_zero_iff_le, (has_lt.lt.trans hx hy).le], },
{ simp only [some_eq_coe, nhds_coe_coe, tendsto_map'_iff, function.comp, ← ennreal.coe_sub,
tendsto_coe],
exact continuous.tendsto (by continuity) _ }
end
protected lemma tendsto.sub {f : filter α} {ma : α → ℝ≥0∞} {mb : α → ℝ≥0∞} {a b : ℝ≥0∞}
(hma : tendsto ma f (𝓝 a)) (hmb : tendsto mb f (𝓝 b)) (h : a ≠ ∞ ∨ b ≠ ∞) :
tendsto (λ a, ma a - mb a) f (𝓝 (a - b)) :=
show tendsto ((λ p : ℝ≥0∞ × ℝ≥0∞, p.1 - p.2) ∘ (λa, (ma a, mb a))) f (𝓝 (a - b)), from
tendsto.comp (ennreal.tendsto_sub h) (hma.prod_mk_nhds hmb)
protected lemma tendsto_mul (ha : a ≠ 0 ∨ b ≠ ⊤) (hb : b ≠ 0 ∨ a ≠ ⊤) :
tendsto (λp:ℝ≥0∞×ℝ≥0∞, p.1 * p.2) (𝓝 (a, b)) (𝓝 (a * b)) :=
have ht : ∀b:ℝ≥0∞, b ≠ 0 → tendsto (λp:ℝ≥0∞×ℝ≥0∞, p.1 * p.2) (𝓝 ((⊤:ℝ≥0∞), b)) (𝓝 ⊤),
begin
refine assume b hb, tendsto_nhds_top_iff_nnreal.2 $ assume n, _,
rcases lt_iff_exists_nnreal_btwn.1 (pos_iff_ne_zero.2 hb) with ⟨ε, hε, hεb⟩,
have : ∀ᶠ c : ℝ≥0∞ × ℝ≥0∞ in 𝓝 (∞, b), ↑n / ↑ε < c.1 ∧ ↑ε < c.2,
from (lt_mem_nhds $ div_lt_top coe_ne_top hε.ne').prod_nhds (lt_mem_nhds hεb),
refine this.mono (λ c hc, _),
exact (ennreal.div_mul_cancel hε.ne' coe_ne_top).symm.trans_lt (mul_lt_mul hc.1 hc.2)
end,
begin
cases a, {simp [none_eq_top] at hb, simp [none_eq_top, ht b hb, top_mul, hb] },
cases b,
{ simp [none_eq_top] at ha,
simp [*, nhds_swap (a : ℝ≥0∞) ⊤, none_eq_top, some_eq_coe, top_mul, tendsto_map'_iff, (∘),
mul_comm] },
simp [some_eq_coe, nhds_coe_coe, tendsto_map'_iff, (∘)],
simp only [coe_mul.symm, tendsto_coe, tendsto_mul]
end
protected lemma tendsto.mul {f : filter α} {ma : α → ℝ≥0∞} {mb : α → ℝ≥0∞} {a b : ℝ≥0∞}
(hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) :
tendsto (λa, ma a * mb a) f (𝓝 (a * b)) :=
show tendsto ((λp:ℝ≥0∞×ℝ≥0∞, p.1 * p.2) ∘ (λa, (ma a, mb a))) f (𝓝 (a * b)), from
tendsto.comp (ennreal.tendsto_mul ha hb) (hma.prod_mk_nhds hmb)
lemma _root_.continuous_on.ennreal_mul [topological_space α] {f g : α → ℝ≥0∞} {s : set α}
(hf : continuous_on f s) (hg : continuous_on g s) (h₁ : ∀ x ∈ s, f x ≠ 0 ∨ g x ≠ ∞)
(h₂ : ∀ x ∈ s, g x ≠ 0 ∨ f x ≠ ∞) :
continuous_on (λ x, f x * g x) s :=
λ x hx, ennreal.tendsto.mul (hf x hx) (h₁ x hx) (hg x hx) (h₂ x hx)
lemma _root_.continuous.ennreal_mul [topological_space α] {f g : α → ℝ≥0∞} (hf : continuous f)
(hg : continuous g) (h₁ : ∀ x, f x ≠ 0 ∨ g x ≠ ∞) (h₂ : ∀ x, g x ≠ 0 ∨ f x ≠ ∞) :
continuous (λ x, f x * g x) :=
continuous_iff_continuous_at.2 $
λ x, ennreal.tendsto.mul hf.continuous_at (h₁ x) hg.continuous_at (h₂ x)
protected lemma tendsto.const_mul {f : filter α} {m : α → ℝ≥0∞} {a b : ℝ≥0∞}
(hm : tendsto m f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λb, a * m b) f (𝓝 (a * b)) :=
by_cases
(assume : a = 0, by simp [this, tendsto_const_nhds])
(assume ha : a ≠ 0, ennreal.tendsto.mul tendsto_const_nhds (or.inl ha) hm hb)
protected lemma tendsto.mul_const {f : filter α} {m : α → ℝ≥0∞} {a b : ℝ≥0∞}
(hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) : tendsto (λx, m x * b) f (𝓝 (a * b)) :=
by simpa only [mul_comm] using ennreal.tendsto.const_mul hm ha
lemma tendsto_finset_prod_of_ne_top {ι : Type*} {f : ι → α → ℝ≥0∞} {x : filter α} {a : ι → ℝ≥0∞}
(s : finset ι) (h : ∀ i ∈ s, tendsto (f i) x (𝓝 (a i))) (h' : ∀ i ∈ s, a i ≠ ∞):
tendsto (λ b, ∏ c in s, f c b) x (𝓝 (∏ c in s, a c)) :=
begin
induction s using finset.induction with a s has IH, { simp [tendsto_const_nhds] },
simp only [finset.prod_insert has],
apply tendsto.mul (h _ (finset.mem_insert_self _ _)),
{ right,
exact (prod_lt_top (λ i hi, h' _ (finset.mem_insert_of_mem hi))).ne },
{ exact IH (λ i hi, h _ (finset.mem_insert_of_mem hi))
(λ i hi, h' _ (finset.mem_insert_of_mem hi)) },
{ exact or.inr (h' _ (finset.mem_insert_self _ _)) }
end
protected lemma continuous_at_const_mul {a b : ℝ≥0∞} (h : a ≠ ⊤ ∨ b ≠ 0) :
continuous_at ((*) a) b :=
tendsto.const_mul tendsto_id h.symm
protected lemma continuous_at_mul_const {a b : ℝ≥0∞} (h : a ≠ ⊤ ∨ b ≠ 0) :
continuous_at (λ x, x * a) b :=
tendsto.mul_const tendsto_id h.symm
protected lemma continuous_const_mul {a : ℝ≥0∞} (ha : a ≠ ⊤) : continuous ((*) a) :=
continuous_iff_continuous_at.2 $ λ x, ennreal.continuous_at_const_mul (or.inl ha)
protected lemma continuous_mul_const {a : ℝ≥0∞} (ha : a ≠ ⊤) : continuous (λ x, x * a) :=
continuous_iff_continuous_at.2 $ λ x, ennreal.continuous_at_mul_const (or.inl ha)
protected lemma continuous_div_const (c : ℝ≥0∞) (c_ne_zero : c ≠ 0) :
continuous (λ (x : ℝ≥0∞), x / c) :=
begin
simp_rw [div_eq_mul_inv, continuous_iff_continuous_at],
intro x,
exact ennreal.continuous_at_mul_const (or.intro_left _ (inv_ne_top.mpr c_ne_zero)),
end
@[continuity]
lemma continuous_pow (n : ℕ) : continuous (λ a : ℝ≥0∞, a ^ n) :=
begin
induction n with n IH,
{ simp [continuous_const] },
simp_rw [nat.succ_eq_add_one, pow_add, pow_one, continuous_iff_continuous_at],
assume x,
refine ennreal.tendsto.mul (IH.tendsto _) _ tendsto_id _;
by_cases H : x = 0,
{ simp only [H, zero_ne_top, ne.def, or_true, not_false_iff]},
{ exact or.inl (λ h, H (pow_eq_zero h)) },
{ simp only [H, pow_eq_top_iff, zero_ne_top, false_or, eq_self_iff_true,
not_true, ne.def, not_false_iff, false_and], },
{ simp only [H, true_or, ne.def, not_false_iff] }
end
lemma continuous_on_sub :
continuous_on (λ p : ℝ≥0∞ × ℝ≥0∞, p.fst - p.snd) { p : ℝ≥0∞ × ℝ≥0∞ | p ≠ ⟨∞, ∞⟩ } :=
begin
rw continuous_on,
rintros ⟨x, y⟩ hp,
simp only [ne.def, set.mem_set_of_eq, prod.mk.inj_iff] at hp,
refine tendsto_nhds_within_of_tendsto_nhds (tendsto_sub (not_and_distrib.mp hp)),
end
lemma continuous_sub_left {a : ℝ≥0∞} (a_ne_top : a ≠ ⊤) :
continuous (λ x, a - x) :=
begin
rw (show (λ x, a - x) = (λ p : ℝ≥0∞ × ℝ≥0∞, p.fst - p.snd) ∘ (λ x, ⟨a, x⟩), by refl),
apply continuous_on.comp_continuous continuous_on_sub (continuous.prod.mk a),
intro x,
simp only [a_ne_top, ne.def, mem_set_of_eq, prod.mk.inj_iff, false_and, not_false_iff],
end
lemma continuous_nnreal_sub {a : ℝ≥0} :
continuous (λ (x : ℝ≥0∞), (a : ℝ≥0∞) - x) :=
continuous_sub_left coe_ne_top
lemma continuous_on_sub_left (a : ℝ≥0∞) :
continuous_on (λ x, a - x) {x : ℝ≥0∞ | x ≠ ∞} :=
begin
rw (show (λ x, a - x) = (λ p : ℝ≥0∞ × ℝ≥0∞, p.fst - p.snd) ∘ (λ x, ⟨a, x⟩), by refl),
apply continuous_on.comp continuous_on_sub (continuous.continuous_on (continuous.prod.mk a)),
rintros _ h (_|_),
exact h none_eq_top,
end
lemma continuous_sub_right (a : ℝ≥0∞) :
continuous (λ x : ℝ≥0∞, x - a) :=
begin
by_cases a_infty : a = ∞,
{ simp [a_infty, continuous_const], },
{ rw (show (λ x, x - a) = (λ p : ℝ≥0∞ × ℝ≥0∞, p.fst - p.snd) ∘ (λ x, ⟨x, a⟩), by refl),
apply continuous_on.comp_continuous
continuous_on_sub (continuous_id'.prod_mk continuous_const),
intro x,
simp only [a_infty, ne.def, mem_set_of_eq, prod.mk.inj_iff, and_false, not_false_iff], },
end
protected lemma tendsto.pow {f : filter α} {m : α → ℝ≥0∞} {a : ℝ≥0∞} {n : ℕ}
(hm : tendsto m f (𝓝 a)) :
tendsto (λ x, (m x) ^ n) f (𝓝 (a ^ n)) :=
((continuous_pow n).tendsto a).comp hm
lemma le_of_forall_lt_one_mul_le {x y : ℝ≥0∞} (h : ∀ a < 1, a * x ≤ y) : x ≤ y :=
begin
have : tendsto (* x) (𝓝[<] 1) (𝓝 (1 * x)) :=
(ennreal.continuous_at_mul_const (or.inr one_ne_zero)).mono_left inf_le_left,
rw one_mul at this,
haveI : (𝓝[<] (1 : ℝ≥0∞)).ne_bot := nhds_within_Iio_self_ne_bot' ⟨0, ennreal.zero_lt_one⟩,
exact le_of_tendsto this (eventually_nhds_within_iff.2 $ eventually_of_forall h)
end
lemma infi_mul_left' {ι} {f : ι → ℝ≥0∞} {a : ℝ≥0∞}
(h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) (h0 : a = 0 → nonempty ι) :
(⨅ i, a * f i) = a * ⨅ i, f i :=
begin
by_cases H : a = ⊤ ∧ (⨅ i, f i) = 0,
{ rcases h H.1 H.2 with ⟨i, hi⟩,
rw [H.2, mul_zero, ← bot_eq_zero, infi_eq_bot],
exact λ b hb, ⟨i, by rwa [hi, mul_zero, ← bot_eq_zero]⟩ },
{ rw not_and_distrib at H,
casesI is_empty_or_nonempty ι,
{ rw [infi_of_empty, infi_of_empty, mul_top, if_neg],
exact mt h0 (not_nonempty_iff.2 ‹_›) },
{ exact (ennreal.mul_left_mono.map_infi_of_continuous_at'
(ennreal.continuous_at_const_mul H)).symm } }
end
lemma infi_mul_left {ι} [nonempty ι] {f : ι → ℝ≥0∞} {a : ℝ≥0∞}
(h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) :
(⨅ i, a * f i) = a * ⨅ i, f i :=
infi_mul_left' h (λ _, ‹nonempty ι›)
lemma infi_mul_right' {ι} {f : ι → ℝ≥0∞} {a : ℝ≥0∞}
(h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) (h0 : a = 0 → nonempty ι) :
(⨅ i, f i * a) = (⨅ i, f i) * a :=
by simpa only [mul_comm a] using infi_mul_left' h h0
lemma infi_mul_right {ι} [nonempty ι] {f : ι → ℝ≥0∞} {a : ℝ≥0∞}
(h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) :
(⨅ i, f i * a) = (⨅ i, f i) * a :=
infi_mul_right' h (λ _, ‹nonempty ι›)
lemma inv_map_infi {ι : Sort*} {x : ι → ℝ≥0∞} :
(infi x)⁻¹ = (⨆ i, (x i)⁻¹) :=
order_iso.inv_ennreal.map_infi x
lemma inv_map_supr {ι : Sort*} {x : ι → ℝ≥0∞} :
(supr x)⁻¹ = (⨅ i, (x i)⁻¹) :=
order_iso.inv_ennreal.map_supr x
lemma inv_limsup {ι : Sort*} {x : ι → ℝ≥0∞} {l : filter ι} :
(limsup x l)⁻¹ = liminf (λ i, (x i)⁻¹) l :=
by simp only [limsup_eq_infi_supr, inv_map_infi, inv_map_supr, liminf_eq_supr_infi]
lemma inv_liminf {ι : Sort*} {x : ι → ℝ≥0∞} {l : filter ι} :
(liminf x l)⁻¹ = limsup (λ i, (x i)⁻¹) l :=
by simp only [limsup_eq_infi_supr, inv_map_infi, inv_map_supr, liminf_eq_supr_infi]
instance : has_continuous_inv ℝ≥0∞ := ⟨order_iso.inv_ennreal.continuous⟩
@[simp] protected lemma tendsto_inv_iff {f : filter α} {m : α → ℝ≥0∞} {a : ℝ≥0∞} :
tendsto (λ x, (m x)⁻¹) f (𝓝 a⁻¹) ↔ tendsto m f (𝓝 a) :=
⟨λ h, by simpa only [inv_inv] using tendsto.inv h, tendsto.inv⟩
protected lemma tendsto.div {f : filter α} {ma : α → ℝ≥0∞} {mb : α → ℝ≥0∞} {a b : ℝ≥0∞}
(hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) :
tendsto (λa, ma a / mb a) f (𝓝 (a / b)) :=
by { apply tendsto.mul hma _ (ennreal.tendsto_inv_iff.2 hmb) _; simp [ha, hb] }
protected lemma tendsto.const_div {f : filter α} {m : α → ℝ≥0∞} {a b : ℝ≥0∞}
(hm : tendsto m f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) : tendsto (λb, a / m b) f (𝓝 (a / b)) :=
by { apply tendsto.const_mul (ennreal.tendsto_inv_iff.2 hm), simp [hb] }
protected lemma tendsto.div_const {f : filter α} {m : α → ℝ≥0∞} {a b : ℝ≥0∞}
(hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) : tendsto (λx, m x / b) f (𝓝 (a / b)) :=
by { apply tendsto.mul_const hm, simp [ha] }
protected lemma tendsto_inv_nat_nhds_zero : tendsto (λ n : ℕ, (n : ℝ≥0∞)⁻¹) at_top (𝓝 0) :=
ennreal.inv_top ▸ ennreal.tendsto_inv_iff.2 tendsto_nat_nhds_top
lemma supr_add {ι : Sort*} {s : ι → ℝ≥0∞} [h : nonempty ι] : supr s + a = ⨆b, s b + a :=
monotone.map_supr_of_continuous_at' (continuous_at_id.add continuous_at_const) $
monotone_id.add monotone_const
lemma bsupr_add' {ι : Sort*} {p : ι → Prop} (h : ∃ i, p i) {f : ι → ℝ≥0∞} :
(⨆ i (hi : p i), f i) + a = ⨆ i (hi : p i), f i + a :=
by { haveI : nonempty {i // p i} := nonempty_subtype.2 h, simp only [supr_subtype', supr_add] }
lemma add_bsupr' {ι : Sort*} {p : ι → Prop} (h : ∃ i, p i) {f : ι → ℝ≥0∞} :
a + (⨆ i (hi : p i), f i) = ⨆ i (hi : p i), a + f i :=
by simp only [add_comm a, bsupr_add' h]
lemma bsupr_add {ι} {s : set ι} (hs : s.nonempty) {f : ι → ℝ≥0∞} :
(⨆ i ∈ s, f i) + a = ⨆ i ∈ s, f i + a :=
bsupr_add' hs
lemma add_bsupr {ι} {s : set ι} (hs : s.nonempty) {f : ι → ℝ≥0∞} :
a + (⨆ i ∈ s, f i) = ⨆ i ∈ s, a + f i :=
add_bsupr' hs
lemma Sup_add {s : set ℝ≥0∞} (hs : s.nonempty) : Sup s + a = ⨆b∈s, b + a :=
by rw [Sup_eq_supr, bsupr_add hs]
lemma add_supr {ι : Sort*} {s : ι → ℝ≥0∞} [nonempty ι] : a + supr s = ⨆b, a + s b :=
by rw [add_comm, supr_add]; simp [add_comm]
lemma supr_add_supr_le {ι ι' : Sort*} [nonempty ι] [nonempty ι']
{f : ι → ℝ≥0∞} {g : ι' → ℝ≥0∞} {a : ℝ≥0∞} (h : ∀ i j, f i + g j ≤ a) :
supr f + supr g ≤ a :=
by simpa only [add_supr, supr_add] using supr₂_le h
lemma bsupr_add_bsupr_le' {ι ι'} {p : ι → Prop} {q : ι' → Prop} (hp : ∃ i, p i) (hq : ∃ j, q j)
{f : ι → ℝ≥0∞} {g : ι' → ℝ≥0∞} {a : ℝ≥0∞} (h : ∀ i (hi : p i) j (hj : q j), f i + g j ≤ a) :
(⨆ i (hi : p i), f i) + (⨆ j (hj : q j), g j) ≤ a :=
by { simp_rw [bsupr_add' hp, add_bsupr' hq], exact supr₂_le (λ i hi, supr₂_le (h i hi)) }
lemma bsupr_add_bsupr_le {ι ι'} {s : set ι} {t : set ι'} (hs : s.nonempty) (ht : t.nonempty)
{f : ι → ℝ≥0∞} {g : ι' → ℝ≥0∞} {a : ℝ≥0∞} (h : ∀ (i ∈ s) (j ∈ t), f i + g j ≤ a) :
(⨆ i ∈ s, f i) + (⨆ j ∈ t, g j) ≤ a :=
bsupr_add_bsupr_le' hs ht h
lemma supr_add_supr {ι : Sort*} {f g : ι → ℝ≥0∞} (h : ∀i j, ∃k, f i + g j ≤ f k + g k) :
supr f + supr g = (⨆ a, f a + g a) :=
begin
casesI is_empty_or_nonempty ι,
{ simp only [supr_of_empty, bot_eq_zero, zero_add] },
{ refine le_antisymm _ (supr_le $ λ a, add_le_add (le_supr _ _) (le_supr _ _)),
refine supr_add_supr_le (λ i j, _),
rcases h i j with ⟨k, hk⟩,
exact le_supr_of_le k hk }
end
lemma supr_add_supr_of_monotone {ι : Sort*} [semilattice_sup ι]
{f g : ι → ℝ≥0∞} (hf : monotone f) (hg : monotone g) :
supr f + supr g = (⨆ a, f a + g a) :=
supr_add_supr $ assume i j, ⟨i ⊔ j, add_le_add (hf $ le_sup_left) (hg $ le_sup_right)⟩
lemma finset_sum_supr_nat {α} {ι} [semilattice_sup ι] {s : finset α} {f : α → ι → ℝ≥0∞}
(hf : ∀a, monotone (f a)) :
∑ a in s, supr (f a) = (⨆ n, ∑ a in s, f a n) :=
begin
refine finset.induction_on s _ _,
{ simp, },
{ assume a s has ih,
simp only [finset.sum_insert has],
rw [ih, supr_add_supr_of_monotone (hf a)],
assume i j h,
exact (finset.sum_le_sum $ assume a ha, hf a h) }
end
lemma mul_supr {ι : Sort*} {f : ι → ℝ≥0∞} {a : ℝ≥0∞} : a * supr f = ⨆i, a * f i :=
begin
by_cases hf : ∀ i, f i = 0,
{ obtain rfl : f = (λ _, 0), from funext hf,
simp only [supr_zero_eq_zero, mul_zero] },
{ refine (monotone_id.const_mul' _).map_supr_of_continuous_at _ (mul_zero a),
refine ennreal.tendsto.const_mul tendsto_id (or.inl _),
exact mt supr_eq_zero.1 hf }
end
lemma mul_Sup {s : set ℝ≥0∞} {a : ℝ≥0∞} : a * Sup s = ⨆i∈s, a * i :=
by simp only [Sup_eq_supr, mul_supr]
lemma supr_mul {ι : Sort*} {f : ι → ℝ≥0∞} {a : ℝ≥0∞} : supr f * a = ⨆i, f i * a :=
by rw [mul_comm, mul_supr]; congr; funext; rw [mul_comm]
lemma supr_div {ι : Sort*} {f : ι → ℝ≥0∞} {a : ℝ≥0∞} : supr f / a = ⨆i, f i / a :=
supr_mul
protected lemma tendsto_coe_sub : ∀{b:ℝ≥0∞}, tendsto (λb:ℝ≥0∞, ↑r - b) (𝓝 b) (𝓝 (↑r - b)) :=
begin
refine forall_ennreal.2 ⟨λ a, _, _⟩,
{ simp [@nhds_coe a, tendsto_map'_iff, (∘), tendsto_coe, ← with_top.coe_sub],
exact tendsto_const_nhds.sub tendsto_id },
simp,
exact (tendsto.congr' (mem_of_superset (lt_mem_nhds $ @coe_lt_top r) $
by simp [le_of_lt] {contextual := tt})) tendsto_const_nhds
end
lemma sub_supr {ι : Sort*} [nonempty ι] {b : ι → ℝ≥0∞} (hr : a < ⊤) :
a - (⨆i, b i) = (⨅i, a - b i) :=
let ⟨r, eq, _⟩ := lt_iff_exists_coe.mp hr in
have Inf ((λb, ↑r - b) '' range b) = ↑r - (⨆i, b i),
from is_glb.Inf_eq $ is_lub_supr.is_glb_of_tendsto
(assume x _ y _, tsub_le_tsub (le_refl (r : ℝ≥0∞)))
(range_nonempty _)
(ennreal.tendsto_coe_sub.comp (tendsto_id'.2 inf_le_left)),
by rw [eq, ←this]; simp [Inf_image, infi_range, -mem_range]; exact le_rfl
lemma exists_countable_dense_no_zero_top :
∃ (s : set ℝ≥0∞), s.countable ∧ dense s ∧ 0 ∉ s ∧ ∞ ∉ s :=
begin
obtain ⟨s, s_count, s_dense, hs⟩ : ∃ s : set ℝ≥0∞, s.countable ∧ dense s ∧
(∀ x, is_bot x → x ∉ s) ∧ (∀ x, is_top x → x ∉ s) := exists_countable_dense_no_bot_top ℝ≥0∞,
exact ⟨s, s_count, s_dense, λ h, hs.1 0 (by simp) h, λ h, hs.2 ∞ (by simp) h⟩,
end
lemma exists_lt_add_of_lt_add {x y z : ℝ≥0∞} (h : x < y + z) (hy : y ≠ 0) (hz : z ≠ 0) :
∃ y' z', y' < y ∧ z' < z ∧ x < y' + z' :=
begin
haveI : ne_bot (𝓝[<] y) := nhds_within_Iio_self_ne_bot' ⟨0, pos_iff_ne_zero.2 hy⟩,
haveI : ne_bot (𝓝[<] z) := nhds_within_Iio_self_ne_bot' ⟨0, pos_iff_ne_zero.2 hz⟩,
have A : tendsto (λ (p : ℝ≥0∞ × ℝ≥0∞), p.1 + p.2) ((𝓝[<] y).prod (𝓝[<] z)) (𝓝 (y + z)),
{ apply tendsto.mono_left _ (filter.prod_mono nhds_within_le_nhds nhds_within_le_nhds),
rw ← nhds_prod_eq,
exact tendsto_add },
rcases (((tendsto_order.1 A).1 x h).and
(filter.prod_mem_prod self_mem_nhds_within self_mem_nhds_within)).exists
with ⟨⟨y', z'⟩, hx, hy', hz'⟩,
exact ⟨y', z', hy', hz', hx⟩,
end
end topological_space
section liminf
lemma exists_frequently_lt_of_liminf_ne_top
{ι : Type*} {l : filter ι} {x : ι → ℝ} (hx : liminf (λ n, (‖x n‖₊ : ℝ≥0∞)) l ≠ ∞) :
∃ R, ∃ᶠ n in l, x n < R :=
begin
by_contra h,
simp_rw [not_exists, not_frequently, not_lt] at h,
refine hx (ennreal.eq_top_of_forall_nnreal_le $ λ r, le_Liminf_of_le (by is_bounded_default) _),
simp only [eventually_map, ennreal.coe_le_coe],
filter_upwards [h r] with i hi using hi.trans ((coe_nnnorm (x i)).symm ▸ le_abs_self (x i)),
end
lemma exists_frequently_lt_of_liminf_ne_top'
{ι : Type*} {l : filter ι} {x : ι → ℝ} (hx : liminf (λ n, (‖x n‖₊ : ℝ≥0∞)) l ≠ ∞) :
∃ R, ∃ᶠ n in l, R < x n :=
begin
by_contra h,
simp_rw [not_exists, not_frequently, not_lt] at h,
refine hx (ennreal.eq_top_of_forall_nnreal_le $ λ r, le_Liminf_of_le (by is_bounded_default) _),
simp only [eventually_map, ennreal.coe_le_coe],
filter_upwards [h (-r)] with i hi using (le_neg.1 hi).trans (neg_le_abs_self _),
end
lemma exists_upcrossings_of_not_bounded_under
{ι : Type*} {l : filter ι} {x : ι → ℝ}
(hf : liminf (λ i, (‖x i‖₊ : ℝ≥0∞)) l ≠ ∞)
(hbdd : ¬ is_bounded_under (≤) l (λ i, |x i|)) :
∃ a b : ℚ, a < b ∧ (∃ᶠ i in l, x i < a) ∧ (∃ᶠ i in l, ↑b < x i) :=
begin
rw [is_bounded_under_le_abs, not_and_distrib] at hbdd,
obtain hbdd | hbdd := hbdd,
{ obtain ⟨R, hR⟩ := exists_frequently_lt_of_liminf_ne_top hf,
obtain ⟨q, hq⟩ := exists_rat_gt R,
refine ⟨q, q + 1, (lt_add_iff_pos_right _).2 zero_lt_one, _, _⟩,
{ refine λ hcon, hR _,
filter_upwards [hcon] with x hx using not_lt.2 (lt_of_lt_of_le hq (not_lt.1 hx)).le },
{ simp only [is_bounded_under, is_bounded, eventually_map, eventually_at_top,
ge_iff_le, not_exists, not_forall, not_le, exists_prop] at hbdd,
refine λ hcon, hbdd ↑(q + 1) _,
filter_upwards [hcon] with x hx using not_lt.1 hx } },
{ obtain ⟨R, hR⟩ := exists_frequently_lt_of_liminf_ne_top' hf,
obtain ⟨q, hq⟩ := exists_rat_lt R,
refine ⟨q - 1, q, (sub_lt_self_iff _).2 zero_lt_one, _, _⟩,
{ simp only [is_bounded_under, is_bounded, eventually_map, eventually_at_top,
ge_iff_le, not_exists, not_forall, not_le, exists_prop] at hbdd,
refine λ hcon, hbdd ↑(q - 1) _,
filter_upwards [hcon] with x hx using not_lt.1 hx },
{ refine λ hcon, hR _,
filter_upwards [hcon] with x hx using not_lt.2 ((not_lt.1 hx).trans hq.le) } }
end
end liminf
section tsum
variables {f g : α → ℝ≥0∞}
@[norm_cast] protected lemma has_sum_coe {f : α → ℝ≥0} {r : ℝ≥0} :
has_sum (λa, (f a : ℝ≥0∞)) ↑r ↔ has_sum f r :=
have (λs:finset α, ∑ a in s, ↑(f a)) = (coe : ℝ≥0 → ℝ≥0∞) ∘ (λs:finset α, ∑ a in s, f a),
from funext $ assume s, ennreal.coe_finset_sum.symm,
by unfold has_sum; rw [this, tendsto_coe]
protected lemma tsum_coe_eq {f : α → ℝ≥0} (h : has_sum f r) : ∑'a, (f a : ℝ≥0∞) = r :=
(ennreal.has_sum_coe.2 h).tsum_eq
protected lemma coe_tsum {f : α → ℝ≥0} : summable f → ↑(tsum f) = ∑'a, (f a : ℝ≥0∞)
| ⟨r, hr⟩ := by rw [hr.tsum_eq, ennreal.tsum_coe_eq hr]
protected lemma has_sum : has_sum f (⨆s:finset α, ∑ a in s, f a) :=
tendsto_at_top_supr $ λ s t, finset.sum_le_sum_of_subset
@[simp] protected lemma summable : summable f := ⟨_, ennreal.has_sum⟩
lemma tsum_coe_ne_top_iff_summable {f : β → ℝ≥0} :
∑' b, (f b:ℝ≥0∞) ≠ ∞ ↔ summable f :=
begin
refine ⟨λ h, _, λ h, ennreal.coe_tsum h ▸ ennreal.coe_ne_top⟩,
lift (∑' b, (f b:ℝ≥0∞)) to ℝ≥0 using h with a ha,
refine ⟨a, ennreal.has_sum_coe.1 _⟩,
rw ha,
exact ennreal.summable.has_sum
end
protected lemma tsum_eq_supr_sum : ∑'a, f a = (⨆s:finset α, ∑ a in s, f a) :=
ennreal.has_sum.tsum_eq
protected lemma tsum_eq_supr_sum' {ι : Type*} (s : ι → finset α) (hs : ∀ t, ∃ i, t ⊆ s i) :
∑' a, f a = ⨆ i, ∑ a in s i, f a :=
begin
rw [ennreal.tsum_eq_supr_sum],
symmetry,
change (⨆i:ι, (λ t : finset α, ∑ a in t, f a) (s i)) = ⨆s:finset α, ∑ a in s, f a,
exact (finset.sum_mono_set f).supr_comp_eq hs
end
protected lemma tsum_sigma {β : α → Type*} (f : Πa, β a → ℝ≥0∞) :
∑'p:Σa, β a, f p.1 p.2 = ∑'a b, f a b :=
tsum_sigma' (assume b, ennreal.summable) ennreal.summable
protected lemma tsum_sigma' {β : α → Type*} (f : (Σ a, β a) → ℝ≥0∞) :
∑'p:(Σa, β a), f p = ∑'a b, f ⟨a, b⟩ :=
tsum_sigma' (assume b, ennreal.summable) ennreal.summable
protected lemma tsum_prod {f : α → β → ℝ≥0∞} : ∑' p : α × β, f p.1 p.2 = ∑' a b, f a b :=
tsum_prod' ennreal.summable $ λ _, ennreal.summable
protected lemma tsum_prod' {f : α × β → ℝ≥0∞} : ∑' p : α × β, f p = ∑' a b, f (a, b) :=
tsum_prod' ennreal.summable $ λ _, ennreal.summable
protected lemma tsum_comm {f : α → β → ℝ≥0∞} : ∑'a, ∑'b, f a b = ∑'b, ∑'a, f a b :=
tsum_comm' ennreal.summable (λ _, ennreal.summable) (λ _, ennreal.summable)
protected lemma tsum_add : ∑'a, (f a + g a) = (∑'a, f a) + (∑'a, g a) :=
tsum_add ennreal.summable ennreal.summable
protected lemma tsum_le_tsum (h : ∀a, f a ≤ g a) : ∑'a, f a ≤ ∑'a, g a :=
tsum_le_tsum h ennreal.summable ennreal.summable
protected lemma sum_le_tsum {f : α → ℝ≥0∞} (s : finset α) : ∑ x in s, f x ≤ ∑' x, f x :=
sum_le_tsum s (λ x hx, zero_le _) ennreal.summable
protected lemma tsum_eq_supr_nat' {f : ℕ → ℝ≥0∞} {N : ℕ → ℕ} (hN : tendsto N at_top at_top) :
∑'i:ℕ, f i = (⨆i:ℕ, ∑ a in finset.range (N i), f a) :=
ennreal.tsum_eq_supr_sum' _ $ λ t,
let ⟨n, hn⟩ := t.exists_nat_subset_range,
⟨k, _, hk⟩ := exists_le_of_tendsto_at_top hN 0 n in
⟨k, finset.subset.trans hn (finset.range_mono hk)⟩
protected lemma tsum_eq_supr_nat {f : ℕ → ℝ≥0∞} :
∑'i:ℕ, f i = (⨆i:ℕ, ∑ a in finset.range i, f a) :=
ennreal.tsum_eq_supr_sum' _ finset.exists_nat_subset_range
protected lemma tsum_eq_liminf_sum_nat {f : ℕ → ℝ≥0∞} :
∑' i, f i = liminf (λ n, ∑ i in finset.range n, f i) at_top :=
begin
rw [ennreal.tsum_eq_supr_nat, filter.liminf_eq_supr_infi_of_nat],
congr,
refine funext (λ n, le_antisymm _ _),
{ refine le_infi₂ (λ i hi, finset.sum_le_sum_of_subset_of_nonneg _ (λ _ _ _, zero_le _)),
simpa only [finset.range_subset, add_le_add_iff_right] using hi, },
{ refine le_trans (infi_le _ n) _,
simp [le_refl n, le_refl ((finset.range n).sum f)], },
end
protected lemma le_tsum (a : α) : f a ≤ ∑'a, f a :=
le_tsum' ennreal.summable a
@[simp] protected lemma tsum_eq_zero : ∑' i, f i = 0 ↔ ∀ i, f i = 0 :=
⟨λ h i, nonpos_iff_eq_zero.1 $ h ▸ ennreal.le_tsum i, λ h, by simp [h]⟩
protected lemma tsum_eq_top_of_eq_top : (∃ a, f a = ∞) → ∑' a, f a = ∞
| ⟨a, ha⟩ := top_unique $ ha ▸ ennreal.le_tsum a
protected lemma lt_top_of_tsum_ne_top {a : α → ℝ≥0∞} (tsum_ne_top : ∑' i, a i ≠ ∞) (j : α) :
a j < ∞ :=
begin
have key := not_imp_not.mpr ennreal.tsum_eq_top_of_eq_top,
simp only [not_exists] at key,
exact lt_top_iff_ne_top.mpr (key tsum_ne_top j),
end
@[simp] protected lemma tsum_top [nonempty α] : ∑' a : α, ∞ = ∞ :=
let ⟨a⟩ := ‹nonempty α› in ennreal.tsum_eq_top_of_eq_top ⟨a, rfl⟩
lemma tsum_const_eq_top_of_ne_zero {α : Type*} [infinite α] {c : ℝ≥0∞} (hc : c ≠ 0) :
(∑' (a : α), c) = ∞ :=
begin
have A : tendsto (λ (n : ℕ), (n : ℝ≥0∞) * c) at_top (𝓝 (∞ * c)),
{ apply ennreal.tendsto.mul_const tendsto_nat_nhds_top,
simp only [true_or, top_ne_zero, ne.def, not_false_iff] },
have B : ∀ (n : ℕ), (n : ℝ≥0∞) * c ≤ (∑' (a : α), c),
{ assume n,
rcases infinite.exists_subset_card_eq α n with ⟨s, hs⟩,
simpa [hs] using @ennreal.sum_le_tsum α (λ i, c) s },
simpa [hc] using le_of_tendsto' A B,
end
protected lemma ne_top_of_tsum_ne_top (h : ∑' a, f a ≠ ∞) (a : α) : f a ≠ ∞ :=
λ ha, h $ ennreal.tsum_eq_top_of_eq_top ⟨a, ha⟩
protected lemma tsum_mul_left : ∑'i, a * f i = a * ∑'i, f i :=
if h : ∀i, f i = 0 then by simp [h] else
let ⟨i, (hi : f i ≠ 0)⟩ := not_forall.mp h in
have sum_ne_0 : ∑'i, f i ≠ 0, from ne_of_gt $
calc 0 < f i : lt_of_le_of_ne (zero_le _) hi.symm
... ≤ ∑'i, f i : ennreal.le_tsum _,
have tendsto (λs:finset α, ∑ j in s, a * f j) at_top (𝓝 (a * ∑'i, f i)),
by rw [← show (*) a ∘ (λs:finset α, ∑ j in s, f j) = λs, ∑ j in s, a * f j,
from funext $ λ s, finset.mul_sum];
exact ennreal.tendsto.const_mul ennreal.summable.has_sum (or.inl sum_ne_0),
has_sum.tsum_eq this
protected lemma tsum_mul_right : (∑'i, f i * a) = (∑'i, f i) * a :=
by simp [mul_comm, ennreal.tsum_mul_left]
@[simp] lemma tsum_supr_eq {α : Type*} (a : α) {f : α → ℝ≥0∞} :
∑'b:α, (⨆ (h : a = b), f b) = f a :=
le_antisymm
(by rw [ennreal.tsum_eq_supr_sum]; exact supr_le (assume s,
calc (∑ b in s, ⨆ (h : a = b), f b) ≤ ∑ b in {a}, ⨆ (h : a = b), f b :
finset.sum_le_sum_of_ne_zero $ assume b _ hb,
suffices a = b, by simpa using this.symm,
classical.by_contradiction $ assume h,
by simpa [h] using hb
... = f a : by simp))
(calc f a ≤ (⨆ (h : a = a), f a) : le_supr (λh:a=a, f a) rfl
... ≤ (∑'b:α, ⨆ (h : a = b), f b) : ennreal.le_tsum _)
lemma has_sum_iff_tendsto_nat {f : ℕ → ℝ≥0∞} (r : ℝ≥0∞) :
has_sum f r ↔ tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) :=
begin
refine ⟨has_sum.tendsto_sum_nat, assume h, _⟩,
rw [← supr_eq_of_tendsto _ h, ← ennreal.tsum_eq_supr_nat],
{ exact ennreal.summable.has_sum },
{ exact assume s t hst, finset.sum_le_sum_of_subset (finset.range_subset.2 hst) }
end
lemma tendsto_nat_tsum (f : ℕ → ℝ≥0∞) :
tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 (∑' n, f n)) :=
by { rw ← has_sum_iff_tendsto_nat, exact ennreal.summable.has_sum }
lemma to_nnreal_apply_of_tsum_ne_top {α : Type*} {f : α → ℝ≥0∞} (hf : ∑' i, f i ≠ ∞) (x : α) :
(((ennreal.to_nnreal ∘ f) x : ℝ≥0) : ℝ≥0∞) = f x :=
coe_to_nnreal $ ennreal.ne_top_of_tsum_ne_top hf _
lemma summable_to_nnreal_of_tsum_ne_top {α : Type*} {f : α → ℝ≥0∞} (hf : ∑' i, f i ≠ ∞) :
summable (ennreal.to_nnreal ∘ f) :=
by simpa only [←tsum_coe_ne_top_iff_summable, to_nnreal_apply_of_tsum_ne_top hf] using hf
lemma tendsto_cofinite_zero_of_tsum_ne_top {α} {f : α → ℝ≥0∞} (hf : ∑' x, f x ≠ ∞) :
tendsto f cofinite (𝓝 0) :=
begin
have f_ne_top : ∀ n, f n ≠ ∞, from ennreal.ne_top_of_tsum_ne_top hf,
have h_f_coe : f = λ n, ((f n).to_nnreal : ennreal),
from funext (λ n, (coe_to_nnreal (f_ne_top n)).symm),
rw [h_f_coe, ←@coe_zero, tendsto_coe],
exact nnreal.tendsto_cofinite_zero_of_summable (summable_to_nnreal_of_tsum_ne_top hf),
end
lemma tendsto_at_top_zero_of_tsum_ne_top {f : ℕ → ℝ≥0∞} (hf : ∑' x, f x ≠ ∞) :
tendsto f at_top (𝓝 0) :=
by { rw ←nat.cofinite_eq_at_top, exact tendsto_cofinite_zero_of_tsum_ne_top hf }
/-- The sum over the complement of a finset tends to `0` when the finset grows to cover the whole
space. This does not need a summability assumption, as otherwise all sums are zero. -/
lemma tendsto_tsum_compl_at_top_zero {α : Type*} {f : α → ℝ≥0∞} (hf : ∑' x, f x ≠ ∞) :
tendsto (λ (s : finset α), ∑' b : {x // x ∉ s}, f b) at_top (𝓝 0) :=
begin
lift f to α → ℝ≥0 using ennreal.ne_top_of_tsum_ne_top hf,
convert ennreal.tendsto_coe.2 (nnreal.tendsto_tsum_compl_at_top_zero f),
ext1 s,
rw ennreal.coe_tsum,
exact nnreal.summable_comp_injective (tsum_coe_ne_top_iff_summable.1 hf) subtype.coe_injective
end
protected lemma tsum_apply {ι α : Type*} {f : ι → α → ℝ≥0∞} {x : α} :
(∑' i, f i) x = ∑' i, f i x :=
tsum_apply $ pi.summable.mpr $ λ _, ennreal.summable
lemma tsum_sub {f : ℕ → ℝ≥0∞} {g : ℕ → ℝ≥0∞} (h₁ : ∑' i, g i ≠ ∞) (h₂ : g ≤ f) :
∑' i, (f i - g i) = (∑' i, f i) - (∑' i, g i) :=
begin
have h₃: ∑' i, (f i - g i) = ∑' i, (f i - g i + g i) - ∑' i, g i,
{ rw [ennreal.tsum_add, ennreal.add_sub_cancel_right h₁]},
have h₄:(λ i, (f i - g i) + (g i)) = f,
{ ext n, rw tsub_add_cancel_of_le (h₂ n)},
rw h₄ at h₃, apply h₃,
end
lemma tsum_mono_subtype (f : α → ℝ≥0∞) {s t : set α} (h : s ⊆ t) :
∑' (x : s), f x ≤ ∑' (x : t), f x :=
begin
simp only [tsum_subtype],
apply ennreal.tsum_le_tsum,
exact indicator_le_indicator_of_subset h (λ _, zero_le _),
end
lemma tsum_union_le (f : α → ℝ≥0∞) (s t : set α) :
∑' (x : s ∪ t), f x ≤ ∑' (x : s), f x + ∑' (x : t), f x :=
calc ∑' (x : s ∪ t), f x = ∑' (x : s ∪ (t \ s)), f x :
by { apply tsum_congr_subtype, rw union_diff_self }
... = ∑' (x : s), f x + ∑' (x : t \ s), f x :
tsum_union_disjoint disjoint_sdiff_self_right ennreal.summable ennreal.summable
... ≤ ∑' (x : s), f x + ∑' (x : t), f x :
add_le_add le_rfl (tsum_mono_subtype _ (diff_subset _ _))
lemma tsum_bUnion_le {ι : Type*} (f : α → ℝ≥0∞) (s : finset ι) (t : ι → set α) :
∑' (x : ⋃ (i ∈ s), t i), f x ≤ ∑ i in s, ∑' (x : t i), f x :=
begin
classical,
induction s using finset.induction_on with i s hi ihs h, { simp },
have : (⋃ (j ∈ insert i s), t j) = t i ∪ (⋃ (j ∈ s), t j), by simp,
rw tsum_congr_subtype f this,
calc ∑' (x : (t i ∪ (⋃ (j ∈ s), t j))), f x ≤
∑' (x : t i), f x + ∑' (x : ⋃ (j ∈ s), t j), f x : tsum_union_le _ _ _
... ≤ ∑' (x : t i), f x + ∑ i in s, ∑' (x : t i), f x : add_le_add le_rfl ihs
... = ∑ j in insert i s, ∑' (x : t j), f x : (finset.sum_insert hi).symm
end
lemma tsum_Union_le {ι : Type*} [fintype ι] (f : α → ℝ≥0∞) (t : ι → set α) :
∑' (x : ⋃ i, t i), f x ≤ ∑ i, ∑' (x : t i), f x :=
begin
classical,
have : (⋃ i, t i) = (⋃ (i ∈ (finset.univ : finset ι)), t i), by simp,
rw tsum_congr_subtype f this,
exact tsum_bUnion_le _ _ _
end
lemma tsum_add_one_eq_top {f : ℕ → ℝ≥0∞} (hf : ∑' n, f n = ∞) (hf0 : f 0 ≠ ∞) :
∑' n, f (n + 1) = ∞ :=
begin
rw ← tsum_eq_tsum_of_has_sum_iff_has_sum (λ _, (not_mem_range_equiv 1).has_sum_iff),
swap, { apply_instance },
have h₁ : (∑' b : {n // n ∈ finset.range 1}, f b) + (∑' b : {n // n ∉ finset.range 1}, f b) =
∑' b, f b,
{ exact tsum_add_tsum_compl ennreal.summable ennreal.summable },
rw [finset.tsum_subtype, finset.sum_range_one, hf, ennreal.add_eq_top] at h₁,
rw ← h₁.resolve_left hf0,
apply tsum_congr,
rintro ⟨i, hi⟩,
simp only [multiset.mem_range, not_lt] at hi,
simp only [tsub_add_cancel_of_le hi, coe_not_mem_range_equiv, function.comp_app, subtype.coe_mk],
end
/-- A sum of extended nonnegative reals which is finite can have only finitely many terms
above any positive threshold.-/
lemma finite_const_le_of_tsum_ne_top {ι : Type*} {a : ι → ℝ≥0∞}
(tsum_ne_top : ∑' i, a i ≠ ∞) {ε : ℝ≥0∞} (ε_ne_zero : ε ≠ 0) :
{i : ι | ε ≤ a i}.finite :=
begin
by_cases ε_infty : ε = ∞,
{ rw ε_infty,
by_contra maybe_infinite,
obtain ⟨j, hj⟩ := set.infinite.nonempty maybe_infinite,
exact tsum_ne_top (le_antisymm le_top (le_trans hj (le_tsum' (@ennreal.summable _ a) j))), },
have key := (nnreal.summable_coe.mpr
(summable_to_nnreal_of_tsum_ne_top tsum_ne_top)).tendsto_cofinite_zero
(Iio_mem_nhds (to_real_pos ε_ne_zero ε_infty)),
simp only [filter.mem_map, filter.mem_cofinite, preimage] at key,
have obs : {i : ι | ↑((a i).to_nnreal) ∈ Iio ε.to_real}ᶜ = {i : ι | ε ≤ a i},
{ ext i,
simpa only [mem_Iio, mem_compl_iff, mem_set_of_eq, not_lt]
using to_real_le_to_real ε_infty (ennreal.ne_top_of_tsum_ne_top tsum_ne_top _), },
rwa obs at key,
end
/-- Markov's inequality for `finset.card` and `tsum` in `ℝ≥0∞`. -/
lemma finset_card_const_le_le_of_tsum_le {ι : Type*} {a : ι → ℝ≥0∞}
{c : ℝ≥0∞} (c_ne_top : c ≠ ∞) (tsum_le_c : ∑' i, a i ≤ c)
{ε : ℝ≥0∞} (ε_ne_zero : ε ≠ 0) :
∃ hf : {i : ι | ε ≤ a i}.finite, ↑hf.to_finset.card ≤ c / ε :=
begin
by_cases ε = ∞,
{ have obs : {i : ι | ε ≤ a i} = ∅,
{ rw eq_empty_iff_forall_not_mem,
intros i hi,
have oops := (le_trans hi (le_tsum' (@ennreal.summable _ a) i)).trans tsum_le_c,
rw h at oops,
exact c_ne_top (le_antisymm le_top oops), },
simp only [obs, finite_empty, finite.to_finset_empty, finset.card_empty,
algebra_map.coe_zero, zero_le', exists_true_left], },
have hf : {i : ι | ε ≤ a i}.finite,
from ennreal.finite_const_le_of_tsum_ne_top
(lt_of_le_of_lt tsum_le_c c_ne_top.lt_top).ne ε_ne_zero,
use hf,
have at_least : ∀ i ∈ hf.to_finset, ε ≤ a i,
{ intros i hi,
simpa only [finite.mem_to_finset, mem_set_of_eq] using hi, },
have partial_sum := @sum_le_tsum _ _ _ _ _ a
hf.to_finset (λ _ _, zero_le') (@ennreal.summable _ a),
have lower_bound := finset.sum_le_sum at_least,
simp only [finset.sum_const, nsmul_eq_mul] at lower_bound,
have key := (ennreal.le_div_iff_mul_le (or.inl ε_ne_zero) (or.inl h)).mpr lower_bound,
exact le_trans key (ennreal.div_le_div_right (partial_sum.trans tsum_le_c) _),
end
end tsum
lemma tendsto_to_real_iff {ι} {fi : filter ι} {f : ι → ℝ≥0∞} (hf : ∀ i, f i ≠ ∞) {x : ℝ≥0∞}
(hx : x ≠ ∞) :
fi.tendsto (λ n, (f n).to_real) (𝓝 x.to_real) ↔ fi.tendsto f (𝓝 x) :=
begin
refine ⟨λ h, _, λ h, tendsto.comp (ennreal.tendsto_to_real hx) h⟩,
have h_eq : f = (λ n, ennreal.of_real (f n).to_real),
by { ext1 n, rw ennreal.of_real_to_real (hf n), },
rw [h_eq, ← ennreal.of_real_to_real hx],
exact ennreal.tendsto_of_real h,
end
lemma tsum_coe_ne_top_iff_summable_coe {f : α → ℝ≥0} :
∑' a, (f a : ℝ≥0∞) ≠ ∞ ↔ summable (λ a, (f a : ℝ)) :=
begin
rw nnreal.summable_coe,
exact tsum_coe_ne_top_iff_summable,
end
lemma tsum_coe_eq_top_iff_not_summable_coe {f : α → ℝ≥0} :
∑' a, (f a : ℝ≥0∞) = ∞ ↔ ¬ summable (λ a, (f a : ℝ)) :=
begin
rw [← @not_not (∑' a, ↑(f a) = ⊤)],
exact not_congr tsum_coe_ne_top_iff_summable_coe
end
lemma has_sum_to_real {f : α → ℝ≥0∞} (hsum : ∑' x, f x ≠ ∞) :
has_sum (λ x, (f x).to_real) (∑' x, (f x).to_real) :=
begin
lift f to α → ℝ≥0 using ennreal.ne_top_of_tsum_ne_top hsum,
simp only [coe_to_real, ← nnreal.coe_tsum, nnreal.has_sum_coe],
exact (tsum_coe_ne_top_iff_summable.1 hsum).has_sum
end
lemma summable_to_real {f : α → ℝ≥0∞} (hsum : ∑' x, f x ≠ ∞) :
summable (λ x, (f x).to_real) :=
(has_sum_to_real hsum).summable
end ennreal
namespace nnreal
open_locale nnreal
lemma tsum_eq_to_nnreal_tsum {f : β → ℝ≥0} :
(∑' b, f b) = (∑' b, (f b : ℝ≥0∞)).to_nnreal :=
begin
by_cases h : summable f,
{ rw [← ennreal.coe_tsum h, ennreal.to_nnreal_coe] },
{ have A := tsum_eq_zero_of_not_summable h,
simp only [← ennreal.tsum_coe_ne_top_iff_summable, not_not] at h,
simp only [h, ennreal.top_to_nnreal, A] }
end
/-- Comparison test of convergence of `ℝ≥0`-valued series. -/
lemma exists_le_has_sum_of_le {f g : β → ℝ≥0} {r : ℝ≥0}
(hgf : ∀b, g b ≤ f b) (hfr : has_sum f r) : ∃p≤r, has_sum g p :=
have ∑'b, (g b : ℝ≥0∞) ≤ r,
begin
refine has_sum_le (assume b, _) ennreal.summable.has_sum (ennreal.has_sum_coe.2 hfr),
exact ennreal.coe_le_coe.2 (hgf _)
end,
let ⟨p, eq, hpr⟩ := ennreal.le_coe_iff.1 this in
⟨p, hpr, ennreal.has_sum_coe.1 $ eq ▸ ennreal.summable.has_sum⟩
/-- Comparison test of convergence of `ℝ≥0`-valued series. -/
lemma summable_of_le {f g : β → ℝ≥0} (hgf : ∀b, g b ≤ f b) : summable f → summable g
| ⟨r, hfr⟩ := let ⟨p, _, hp⟩ := exists_le_has_sum_of_le hgf hfr in hp.summable
/-- A series of non-negative real numbers converges to `r` in the sense of `has_sum` if and only if
the sequence of partial sum converges to `r`. -/
lemma has_sum_iff_tendsto_nat {f : ℕ → ℝ≥0} {r : ℝ≥0} :
has_sum f r ↔ tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) :=
begin
rw [← ennreal.has_sum_coe, ennreal.has_sum_iff_tendsto_nat],
simp only [ennreal.coe_finset_sum.symm],
exact ennreal.tendsto_coe
end
lemma not_summable_iff_tendsto_nat_at_top {f : ℕ → ℝ≥0} :
¬ summable f ↔ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top :=
begin
split,
{ intros h,
refine ((tendsto_of_monotone _).resolve_right h).comp _,
exacts [finset.sum_mono_set _, tendsto_finset_range] },
{ rintros hnat ⟨r, hr⟩,
exact not_tendsto_nhds_of_tendsto_at_top hnat _ (has_sum_iff_tendsto_nat.1 hr) }
end
lemma summable_iff_not_tendsto_nat_at_top {f : ℕ → ℝ≥0} :
summable f ↔ ¬ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top :=
by rw [← not_iff_not, not_not, not_summable_iff_tendsto_nat_at_top]
lemma summable_of_sum_range_le {f : ℕ → ℝ≥0} {c : ℝ≥0}
(h : ∀ n, ∑ i in finset.range n, f i ≤ c) : summable f :=
begin
apply summable_iff_not_tendsto_nat_at_top.2 (λ H, _),
rcases exists_lt_of_tendsto_at_top H 0 c with ⟨n, -, hn⟩,
exact lt_irrefl _ (hn.trans_le (h n)),
end
lemma tsum_le_of_sum_range_le {f : ℕ → ℝ≥0} {c : ℝ≥0}
(h : ∀ n, ∑ i in finset.range n, f i ≤ c) : ∑' n, f n ≤ c :=
tsum_le_of_sum_range_le (summable_of_sum_range_le h) h
lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → ℝ≥0} (hf : summable f)
{i : β → α} (hi : function.injective i) : ∑' x, f (i x) ≤ ∑' x, f x :=
tsum_le_tsum_of_inj i hi (λ c hc, zero_le _) (λ b, le_rfl) (summable_comp_injective hf hi) hf
lemma summable_sigma {β : Π x : α, Type*} {f : (Σ x, β x) → ℝ≥0} :
summable f ↔ (∀ x, summable (λ y, f ⟨x, y⟩)) ∧ summable (λ x, ∑' y, f ⟨x, y⟩) :=
begin
split,
{ simp only [← nnreal.summable_coe, nnreal.coe_tsum],
exact λ h, ⟨h.sigma_factor, h.sigma⟩ },
{ rintro ⟨h₁, h₂⟩,
simpa only [← ennreal.tsum_coe_ne_top_iff_summable, ennreal.tsum_sigma', ennreal.coe_tsum, h₁]
using h₂ }
end
lemma indicator_summable {f : α → ℝ≥0} (hf : summable f) (s : set α) :
summable (s.indicator f) :=
begin
refine nnreal.summable_of_le (λ a, le_trans (le_of_eq (s.indicator_apply f a)) _) hf,
split_ifs,
exact le_refl (f a),
exact zero_le_coe,
end
lemma tsum_indicator_ne_zero {f : α → ℝ≥0} (hf : summable f) {s : set α} (h : ∃ a ∈ s, f a ≠ 0) :
∑' x, (s.indicator f) x ≠ 0 :=
λ h', let ⟨a, ha, hap⟩ := h in
hap (trans (set.indicator_apply_eq_self.mpr (absurd ha)).symm
(((tsum_eq_zero_iff (indicator_summable hf s)).1 h') a))
open finset
/-- For `f : ℕ → ℝ≥0`, then `∑' k, f (k + i)` tends to zero. This does not require a summability
assumption on `f`, as otherwise all sums are zero. -/
lemma tendsto_sum_nat_add (f : ℕ → ℝ≥0) : tendsto (λ i, ∑' k, f (k + i)) at_top (𝓝 0) :=
begin
rw ← tendsto_coe,
convert tendsto_sum_nat_add (λ i, (f i : ℝ)),
norm_cast,
end
lemma has_sum_lt {f g : α → ℝ≥0} {sf sg : ℝ≥0} {i : α} (h : ∀ (a : α), f a ≤ g a) (hi : f i < g i)
(hf : has_sum f sf) (hg : has_sum g sg) : sf < sg :=
begin
have A : ∀ (a : α), (f a : ℝ) ≤ g a := λ a, nnreal.coe_le_coe.2 (h a),
have : (sf : ℝ) < sg :=
has_sum_lt A (nnreal.coe_lt_coe.2 hi) (has_sum_coe.2 hf) (has_sum_coe.2 hg),
exact nnreal.coe_lt_coe.1 this
end
@[mono] lemma has_sum_strict_mono
{f g : α → ℝ≥0} {sf sg : ℝ≥0} (hf : has_sum f sf) (hg : has_sum g sg) (h : f < g) : sf < sg :=
let ⟨hle, i, hi⟩ := pi.lt_def.mp h in has_sum_lt hle hi hf hg
lemma tsum_lt_tsum {f g : α → ℝ≥0} {i : α} (h : ∀ (a : α), f a ≤ g a) (hi : f i < g i)
(hg : summable g) : ∑' n, f n < ∑' n, g n :=
has_sum_lt h hi (summable_of_le h hg).has_sum hg.has_sum
@[mono] lemma tsum_strict_mono {f g : α → ℝ≥0} (hg : summable g) (h : f < g) :
∑' n, f n < ∑' n, g n :=
let ⟨hle, i, hi⟩ := pi.lt_def.mp h in tsum_lt_tsum hle hi hg
lemma tsum_pos {g : α → ℝ≥0} (hg : summable g) (i : α) (hi : 0 < g i) :
0 < ∑' b, g b :=
by { rw ← tsum_zero, exact tsum_lt_tsum (λ a, zero_le _) hi hg }
end nnreal
namespace ennreal
lemma tsum_to_nnreal_eq {f : α → ℝ≥0∞} (hf : ∀ a, f a ≠ ∞) :
(∑' a, f a).to_nnreal = ∑' a, (f a).to_nnreal :=
(congr_arg ennreal.to_nnreal (tsum_congr $ λ x, (coe_to_nnreal (hf x)).symm)).trans
nnreal.tsum_eq_to_nnreal_tsum.symm
lemma tsum_to_real_eq {f : α → ℝ≥0∞} (hf : ∀ a, f a ≠ ∞) :
(∑' a, f a).to_real = ∑' a, (f a).to_real :=
by simp only [ennreal.to_real, tsum_to_nnreal_eq hf, nnreal.coe_tsum]
lemma tendsto_sum_nat_add (f : ℕ → ℝ≥0∞) (hf : ∑' i, f i ≠ ∞) :
tendsto (λ i, ∑' k, f (k + i)) at_top (𝓝 0) :=
begin
lift f to ℕ → ℝ≥0 using ennreal.ne_top_of_tsum_ne_top hf,
replace hf : summable f := tsum_coe_ne_top_iff_summable.1 hf,
simp only [← ennreal.coe_tsum, nnreal.summable_nat_add _ hf, ← ennreal.coe_zero],
exact_mod_cast nnreal.tendsto_sum_nat_add f
end
lemma tsum_le_of_sum_range_le {f : ℕ → ℝ≥0∞} {c : ℝ≥0∞}
(h : ∀ n, ∑ i in finset.range n, f i ≤ c) : ∑' n, f n ≤ c :=
tsum_le_of_sum_range_le ennreal.summable h
lemma has_sum_lt {f g : α → ℝ≥0∞} {sf sg : ℝ≥0∞} {i : α} (h : ∀ (a : α), f a ≤ g a)
(hi : f i < g i) (hsf : sf ≠ ⊤) (hf : has_sum f sf) (hg : has_sum g sg) : sf < sg :=
begin
by_cases hsg : sg = ⊤,
{ exact hsg.symm ▸ lt_of_le_of_ne le_top hsf },
{ have hg' : ∀ x, g x ≠ ⊤:= ennreal.ne_top_of_tsum_ne_top (hg.tsum_eq.symm ▸ hsg),
lift f to α → ℝ≥0 using λ x, ne_of_lt (lt_of_le_of_lt (h x) $ lt_of_le_of_ne le_top (hg' x)),
lift g to α → ℝ≥0 using hg',
lift sf to ℝ≥0 using hsf,
lift sg to ℝ≥0 using hsg,
simp only [coe_le_coe, coe_lt_coe] at h hi ⊢,
exact nnreal.has_sum_lt h hi (ennreal.has_sum_coe.1 hf) (ennreal.has_sum_coe.1 hg) }
end
lemma tsum_lt_tsum {f g : α → ℝ≥0∞} {i : α} (hfi : tsum f ≠ ⊤) (h : ∀ (a : α), f a ≤ g a)
(hi : f i < g i) : ∑' x, f x < ∑' x, g x :=
has_sum_lt h hi hfi ennreal.summable.has_sum ennreal.summable.has_sum
end ennreal
lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → ℝ} (hf : summable f) (hn : ∀ a, 0 ≤ f a)
{i : β → α} (hi : function.injective i) : tsum (f ∘ i) ≤ tsum f :=
begin
lift f to α → ℝ≥0 using hn,
rw nnreal.summable_coe at hf,
simpa only [(∘), ← nnreal.coe_tsum] using nnreal.tsum_comp_le_tsum_of_inj hf hi
end
/-- Comparison test of convergence of series of non-negative real numbers. -/
lemma summable_of_nonneg_of_le {f g : β → ℝ}
(hg : ∀b, 0 ≤ g b) (hgf : ∀b, g b ≤ f b) (hf : summable f) : summable g :=
begin
lift f to β → ℝ≥0 using λ b, (hg b).trans (hgf b),
lift g to β → ℝ≥0 using hg,
rw nnreal.summable_coe at hf ⊢,
exact nnreal.summable_of_le (λ b, nnreal.coe_le_coe.1 (hgf b)) hf
end
lemma summable.to_nnreal {f : α → ℝ} (hf : summable f) :
summable (λ n, (f n).to_nnreal) :=
begin
apply nnreal.summable_coe.1,
refine summable_of_nonneg_of_le (λ n, nnreal.coe_nonneg _) (λ n, _) hf.abs,
simp only [le_abs_self, real.coe_to_nnreal', max_le_iff, abs_nonneg, and_self]
end
/-- A series of non-negative real numbers converges to `r` in the sense of `has_sum` if and only if
the sequence of partial sum converges to `r`. -/
lemma has_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} (hf : ∀i, 0 ≤ f i) (r : ℝ) :
has_sum f r ↔ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) :=
begin
lift f to ℕ → ℝ≥0 using hf,
simp only [has_sum, ← nnreal.coe_sum, nnreal.tendsto_coe'],
exact exists_congr (λ hr, nnreal.has_sum_iff_tendsto_nat)
end
lemma ennreal.of_real_tsum_of_nonneg {f : α → ℝ} (hf_nonneg : ∀ n, 0 ≤ f n) (hf : summable f) :
ennreal.of_real (∑' n, f n) = ∑' n, ennreal.of_real (f n) :=
by simp_rw [ennreal.of_real, ennreal.tsum_coe_eq
(nnreal.has_sum_real_to_nnreal_of_nonneg hf_nonneg hf)]
lemma not_summable_iff_tendsto_nat_at_top_of_nonneg {f : ℕ → ℝ} (hf : ∀ n, 0 ≤ f n) :
¬ summable f ↔ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top :=
begin
lift f to ℕ → ℝ≥0 using hf,
exact_mod_cast nnreal.not_summable_iff_tendsto_nat_at_top
end
lemma summable_iff_not_tendsto_nat_at_top_of_nonneg {f : ℕ → ℝ} (hf : ∀ n, 0 ≤ f n) :
summable f ↔ ¬ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top :=
by rw [← not_iff_not, not_not, not_summable_iff_tendsto_nat_at_top_of_nonneg hf]
lemma summable_sigma_of_nonneg {β : Π x : α, Type*} {f : (Σ x, β x) → ℝ} (hf : ∀ x, 0 ≤ f x) :
summable f ↔ (∀ x, summable (λ y, f ⟨x, y⟩)) ∧ summable (λ x, ∑' y, f ⟨x, y⟩) :=
by { lift f to (Σ x, β x) → ℝ≥0 using hf, exact_mod_cast nnreal.summable_sigma }
lemma summable_of_sum_le {ι : Type*} {f : ι → ℝ} {c : ℝ} (hf : 0 ≤ f)
(h : ∀ u : finset ι, ∑ x in u, f x ≤ c) :
summable f :=
⟨ ⨆ u : finset ι, ∑ x in u, f x,
tendsto_at_top_csupr (finset.sum_mono_set_of_nonneg hf) ⟨c, λ y ⟨u, hu⟩, hu ▸ h u⟩ ⟩
lemma summable_of_sum_range_le {f : ℕ → ℝ} {c : ℝ} (hf : ∀ n, 0 ≤ f n)
(h : ∀ n, ∑ i in finset.range n, f i ≤ c) : summable f :=
begin
apply (summable_iff_not_tendsto_nat_at_top_of_nonneg hf).2 (λ H, _),
rcases exists_lt_of_tendsto_at_top H 0 c with ⟨n, -, hn⟩,
exact lt_irrefl _ (hn.trans_le (h n)),
end
lemma real.tsum_le_of_sum_range_le {f : ℕ → ℝ} {c : ℝ} (hf : ∀ n, 0 ≤ f n)
(h : ∀ n, ∑ i in finset.range n, f i ≤ c) : ∑' n, f n ≤ c :=
tsum_le_of_sum_range_le (summable_of_sum_range_le hf h) h
/-- If a sequence `f` with non-negative terms is dominated by a sequence `g` with summable
series and at least one term of `f` is strictly smaller than the corresponding term in `g`,
then the series of `f` is strictly smaller than the series of `g`. -/
lemma tsum_lt_tsum_of_nonneg {i : ℕ} {f g : ℕ → ℝ}
(h0 : ∀ (b : ℕ), 0 ≤ f b) (h : ∀ (b : ℕ), f b ≤ g b) (hi : f i < g i) (hg : summable g) :
∑' n, f n < ∑' n, g n :=
tsum_lt_tsum h hi (summable_of_nonneg_of_le h0 h hg) hg
section
variables [emetric_space β]
open ennreal filter emetric
/-- In an emetric ball, the distance between points is everywhere finite -/
lemma edist_ne_top_of_mem_ball {a : β} {r : ℝ≥0∞} (x y : ball a r) : edist x.1 y.1 ≠ ⊤ :=
lt_top_iff_ne_top.1 $
calc edist x y ≤ edist a x + edist a y : edist_triangle_left x.1 y.1 a
... < r + r : by rw [edist_comm a x, edist_comm a y]; exact add_lt_add x.2 y.2
... ≤ ⊤ : le_top
/-- Each ball in an extended metric space gives us a metric space, as the edist
is everywhere finite. -/
def metric_space_emetric_ball (a : β) (r : ℝ≥0∞) : metric_space (ball a r) :=
emetric_space.to_metric_space edist_ne_top_of_mem_ball
local attribute [instance] metric_space_emetric_ball
lemma nhds_eq_nhds_emetric_ball (a x : β) (r : ℝ≥0∞) (h : x ∈ ball a r) :
𝓝 x = map (coe : ball a r → β) (𝓝 ⟨x, h⟩) :=
(map_nhds_subtype_coe_eq _ $ is_open.mem_nhds emetric.is_open_ball h).symm
end
section
variable [pseudo_emetric_space α]
open emetric
lemma tendsto_iff_edist_tendsto_0 {l : filter β} {f : β → α} {y : α} :
tendsto f l (𝓝 y) ↔ tendsto (λ x, edist (f x) y) l (𝓝 0) :=
by simp only [emetric.nhds_basis_eball.tendsto_right_iff, emetric.mem_ball,
@tendsto_order ℝ≥0∞ β _ _, forall_prop_of_false ennreal.not_lt_zero, forall_const, true_and]
/-- Yet another metric characterization of Cauchy sequences on integers. This one is often the
most efficient. -/
lemma emetric.cauchy_seq_iff_le_tendsto_0 [nonempty β] [semilattice_sup β] {s : β → α} :
cauchy_seq s ↔ (∃ (b: β → ℝ≥0∞), (∀ n m N : β, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N)
∧ (tendsto b at_top (𝓝 0))) :=
⟨begin
assume hs,
rw emetric.cauchy_seq_iff at hs,
/- `s` is Cauchy sequence. The sequence `b` will be constructed by taking
the supremum of the distances between `s n` and `s m` for `n m ≥ N`-/
let b := λN, Sup ((λ(p : β × β), edist (s p.1) (s p.2))''{p | p.1 ≥ N ∧ p.2 ≥ N}),
--Prove that it bounds the distances of points in the Cauchy sequence
have C : ∀ n m N, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N,
{ refine λm n N hm hn, le_Sup _,
use (prod.mk m n),
simp only [and_true, eq_self_iff_true, set.mem_set_of_eq],
exact ⟨hm, hn⟩ },
--Prove that it tends to `0`, by using the Cauchy property of `s`
have D : tendsto b at_top (𝓝 0),
{ refine tendsto_order.2 ⟨λa ha, absurd ha (ennreal.not_lt_zero), λε εpos, _⟩,
rcases exists_between εpos with ⟨δ, δpos, δlt⟩,
rcases hs δ δpos with ⟨N, hN⟩,
refine filter.mem_at_top_sets.2 ⟨N, λn hn, _⟩,
have : b n ≤ δ := Sup_le begin
simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib, prod.exists],
intros d p q hp hq hd,
rw ← hd,
exact le_of_lt (hN p (le_trans hn hp) q (le_trans hn hq))
end,
simpa using lt_of_le_of_lt this δlt },
-- Conclude
exact ⟨b, ⟨C, D⟩⟩
end,
begin
rintros ⟨b, ⟨b_bound, b_lim⟩⟩,
/-b : ℕ → ℝ, b_bound : ∀ (n m N : ℕ), N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N,
b_lim : tendsto b at_top (𝓝 0)-/
refine emetric.cauchy_seq_iff.2 (λε εpos, _),
have : ∀ᶠ n in at_top, b n < ε := (tendsto_order.1 b_lim ).2 _ εpos,
rcases filter.mem_at_top_sets.1 this with ⟨N, hN⟩,
exact ⟨N, λ m hm n hn, calc
edist (s m) (s n) ≤ b N : b_bound m n N hm hn
... < ε : (hN _ (le_refl N)) ⟩
end⟩
lemma continuous_of_le_add_edist {f : α → ℝ≥0∞} (C : ℝ≥0∞)
(hC : C ≠ ⊤) (h : ∀x y, f x ≤ f y + C * edist x y) : continuous f :=
begin
rcases eq_or_ne C 0 with (rfl|C0),
{ simp only [zero_mul, add_zero] at h,
exact continuous_of_const (λ x y, le_antisymm (h _ _) (h _ _)) },
{ refine continuous_iff_continuous_at.2 (λ x, _),
by_cases hx : f x = ∞,
{ have : f =ᶠ[𝓝 x] (λ _, ∞),
{ filter_upwards [emetric.ball_mem_nhds x ennreal.coe_lt_top],
refine λ y (hy : edist y x < ⊤), _, rw edist_comm at hy,
simpa [hx, hC, hy.ne] using h x y },
exact this.continuous_at },
{ refine (ennreal.tendsto_nhds hx).2 (λ ε (ε0 : 0 < ε), _),
filter_upwards [emetric.closed_ball_mem_nhds x (ennreal.div_pos_iff.2 ⟨ε0.ne', hC⟩)],
have hεC : C * (ε / C) = ε := ennreal.mul_div_cancel' C0 hC,
refine λ y (hy : edist y x ≤ ε / C), ⟨tsub_le_iff_right.2 _, _⟩,
{ rw edist_comm at hy,
calc f x ≤ f y + C * edist x y : h x y
... ≤ f y + C * (ε / C) : add_le_add_left (mul_le_mul_left' hy C) (f y)
... = f y + ε : by rw hεC },
{ calc f y ≤ f x + C * edist y x : h y x
... ≤ f x + C * (ε / C) : add_le_add_left (mul_le_mul_left' hy C) (f x)
... = f x + ε : by rw hεC } } }
end
theorem continuous_edist : continuous (λp:α×α, edist p.1 p.2) :=
begin
apply continuous_of_le_add_edist 2 (by norm_num),
rintros ⟨x, y⟩ ⟨x', y'⟩,
calc edist x y ≤ edist x x' + edist x' y' + edist y' y : edist_triangle4 _ _ _ _
... = edist x' y' + (edist x x' + edist y y') : by simp [edist_comm]; cc
... ≤ edist x' y' + (edist (x, y) (x', y') + edist (x, y) (x', y')) :
add_le_add_left (add_le_add (le_max_left _ _) (le_max_right _ _)) _
... = edist x' y' + 2 * edist (x, y) (x', y') : by rw [← mul_two, mul_comm]
end
@[continuity] theorem continuous.edist [topological_space β] {f g : β → α}
(hf : continuous f) (hg : continuous g) : continuous (λb, edist (f b) (g b)) :=
continuous_edist.comp (hf.prod_mk hg : _)
theorem filter.tendsto.edist {f g : β → α} {x : filter β} {a b : α}
(hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) :
tendsto (λx, edist (f x) (g x)) x (𝓝 (edist a b)) :=
(continuous_edist.tendsto (a, b)).comp (hf.prod_mk_nhds hg)
lemma cauchy_seq_of_edist_le_of_tsum_ne_top {f : ℕ → α} (d : ℕ → ℝ≥0∞)
(hf : ∀ n, edist (f n) (f n.succ) ≤ d n) (hd : tsum d ≠ ∞) :
cauchy_seq f :=
begin
lift d to (ℕ → nnreal) using (λ i, ennreal.ne_top_of_tsum_ne_top hd i),
rw ennreal.tsum_coe_ne_top_iff_summable at hd,
exact cauchy_seq_of_edist_le_of_summable d hf hd
end
lemma emetric.is_closed_ball {a : α} {r : ℝ≥0∞} : is_closed (closed_ball a r) :=
is_closed_le (continuous_id.edist continuous_const) continuous_const
@[simp] lemma emetric.diam_closure (s : set α) : diam (closure s) = diam s :=
begin
refine le_antisymm (diam_le $ λ x hx y hy, _) (diam_mono subset_closure),
have : edist x y ∈ closure (Iic (diam s)),
from map_mem_closure₂ continuous_edist hx hy (λ x hx y hy, edist_le_diam_of_mem hx hy),
rwa closure_Iic at this
end
@[simp] lemma metric.diam_closure {α : Type*} [pseudo_metric_space α] (s : set α) :
metric.diam (closure s) = diam s :=
by simp only [metric.diam, emetric.diam_closure]
lemma is_closed_set_of_lipschitz_on_with {α β} [pseudo_emetric_space α] [pseudo_emetric_space β]
(K : ℝ≥0) (s : set α) :
is_closed {f : α → β | lipschitz_on_with K f s} :=
begin
simp only [lipschitz_on_with, set_of_forall],
refine is_closed_bInter (λ x hx, is_closed_bInter $ λ y hy, is_closed_le _ _),
exacts [continuous.edist (continuous_apply x) (continuous_apply y), continuous_const]
end
lemma is_closed_set_of_lipschitz_with {α β} [pseudo_emetric_space α] [pseudo_emetric_space β]
(K : ℝ≥0) :
is_closed {f : α → β | lipschitz_with K f} :=
by simp only [← lipschitz_on_univ, is_closed_set_of_lipschitz_on_with]
namespace real
/-- For a bounded set `s : set ℝ`, its `emetric.diam` is equal to `Sup s - Inf s` reinterpreted as
`ℝ≥0∞`. -/
lemma ediam_eq {s : set ℝ} (h : bounded s) :
emetric.diam s = ennreal.of_real (Sup s - Inf s) :=
begin
rcases eq_empty_or_nonempty s with rfl|hne, { simp },
refine le_antisymm (metric.ediam_le_of_forall_dist_le $ λ x hx y hy, _) _,
{ have := real.subset_Icc_Inf_Sup_of_bounded h,
exact real.dist_le_of_mem_Icc (this hx) (this hy) },
{ apply ennreal.of_real_le_of_le_to_real,
rw [← metric.diam, ← metric.diam_closure],
have h' := real.bounded_iff_bdd_below_bdd_above.1 h,
calc Sup s - Inf s ≤ dist (Sup s) (Inf s) : le_abs_self _
... ≤ diam (closure s) :
dist_le_diam_of_mem h.closure (cSup_mem_closure hne h'.2) (cInf_mem_closure hne h'.1) }
end
/-- For a bounded set `s : set ℝ`, its `metric.diam` is equal to `Sup s - Inf s`. -/
lemma diam_eq {s : set ℝ} (h : bounded s) : metric.diam s = Sup s - Inf s :=
begin
rw [metric.diam, real.ediam_eq h, ennreal.to_real_of_real],
rw real.bounded_iff_bdd_below_bdd_above at h,
exact sub_nonneg.2 (real.Inf_le_Sup s h.1 h.2)
end
@[simp] lemma ediam_Ioo (a b : ℝ) :
emetric.diam (Ioo a b) = ennreal.of_real (b - a) :=
begin
rcases le_or_lt b a with h|h,
{ simp [h] },
{ rw [real.ediam_eq (bounded_Ioo _ _), cSup_Ioo h, cInf_Ioo h] },
end
@[simp] lemma ediam_Icc (a b : ℝ) :
emetric.diam (Icc a b) = ennreal.of_real (b - a) :=
begin
rcases le_or_lt a b with h|h,
{ rw [real.ediam_eq (bounded_Icc _ _), cSup_Icc h, cInf_Icc h] },
{ simp [h, h.le] }
end
@[simp] lemma ediam_Ico (a b : ℝ) :
emetric.diam (Ico a b) = ennreal.of_real (b - a) :=
le_antisymm (ediam_Icc a b ▸ diam_mono Ico_subset_Icc_self)
(ediam_Ioo a b ▸ diam_mono Ioo_subset_Ico_self)
@[simp] lemma ediam_Ioc (a b : ℝ) :
emetric.diam (Ioc a b) = ennreal.of_real (b - a) :=
le_antisymm (ediam_Icc a b ▸ diam_mono Ioc_subset_Icc_self)
(ediam_Ioo a b ▸ diam_mono Ioo_subset_Ioc_self)
lemma diam_Icc {a b : ℝ} (h : a ≤ b) : metric.diam (Icc a b) = b - a :=
by simp [metric.diam, ennreal.to_real_of_real, sub_nonneg.2 h]
lemma diam_Ico {a b : ℝ} (h : a ≤ b) : metric.diam (Ico a b) = b - a :=
by simp [metric.diam, ennreal.to_real_of_real, sub_nonneg.2 h]
lemma diam_Ioc {a b : ℝ} (h : a ≤ b) : metric.diam (Ioc a b) = b - a :=
by simp [metric.diam, ennreal.to_real_of_real, sub_nonneg.2 h]
lemma diam_Ioo {a b : ℝ} (h : a ≤ b) : metric.diam (Ioo a b) = b - a :=
by simp [metric.diam, ennreal.to_real_of_real, sub_nonneg.2 h]
end real
/-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ℝ≥0∞`,
then the distance from `f n` to the limit is bounded by `∑'_{k=n}^∞ d k`. -/
lemma edist_le_tsum_of_edist_le_of_tendsto {f : ℕ → α} (d : ℕ → ℝ≥0∞)
(hf : ∀ n, edist (f n) (f n.succ) ≤ d n)
{a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) :
edist (f n) a ≤ ∑' m, d (n + m) :=
begin
refine le_of_tendsto (tendsto_const_nhds.edist ha)
(mem_at_top_sets.2 ⟨n, λ m hnm, _⟩),
refine le_trans (edist_le_Ico_sum_of_edist_le hnm (λ k _ _, hf k)) _,
rw [finset.sum_Ico_eq_sum_range],
exact sum_le_tsum _ (λ _ _, zero_le _) ennreal.summable
end
/-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ℝ≥0∞`,
then the distance from `f 0` to the limit is bounded by `∑'_{k=0}^∞ d k`. -/
lemma edist_le_tsum_of_edist_le_of_tendsto₀ {f : ℕ → α} (d : ℕ → ℝ≥0∞)
(hf : ∀ n, edist (f n) (f n.succ) ≤ d n)
{a : α} (ha : tendsto f at_top (𝓝 a)) :
edist (f 0) a ≤ ∑' m, d m :=
by simpa using edist_le_tsum_of_edist_le_of_tendsto d hf ha 0
end --section
|
be08229877f0ce64492194af5e87592911a013ed | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/linear_algebra/determinant.lean | 2011e369bfe35440e3a79b3bd9d75fb10b494404 | [
"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 | 23,637 | lean | /-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen
-/
import linear_algebra.multilinear.basis
import linear_algebra.matrix.reindex
import ring_theory.algebra_tower
import tactic.field_simp
import linear_algebra.matrix.nonsingular_inverse
import linear_algebra.matrix.basis
/-!
# Determinant of families of vectors
This file defines the determinant of an endomorphism, and of a family of vectors
with respect to some basis. For the determinant of a matrix, see the file
`linear_algebra.matrix.determinant`.
## Main definitions
In the list below, and in all this file, `R` is a commutative ring (semiring
is sometimes enough), `M` and its variations are `R`-modules, `ι`, `κ`, `n` and `m` are finite
types used for indexing.
* `basis.det`: the determinant of a family of vectors with respect to a basis,
as a multilinear map
* `linear_map.det`: the determinant of an endomorphism `f : End R M` as a
multiplicative homomorphism (if `M` does not have a finite `R`-basis, the
result is `1` instead)
* `linear_equiv.det`: the determinant of an isomorphism `f : M ≃ₗ[R] M` as a
multiplicative homomorphism (if `M` does not have a finite `R`-basis, the
result is `1` instead)
## Tags
basis, det, determinant
-/
noncomputable theory
open_locale big_operators
open_locale matrix
open linear_map
open submodule
universes u v w
open linear_map matrix set function
variables {R : Type*} [comm_ring R]
variables {M : Type*} [add_comm_group M] [module R M]
variables {M' : Type*} [add_comm_group M'] [module R M']
variables {ι : Type*} [decidable_eq ι] [fintype ι]
variables (e : basis ι R M)
section conjugate
variables {A : Type*} [comm_ring A]
variables {m n : Type*} [fintype m] [fintype n]
/-- If `R^m` and `R^n` are linearly equivalent, then `m` and `n` are also equivalent. -/
def equiv_of_pi_lequiv_pi {R : Type*} [comm_ring R] [nontrivial R]
(e : (m → R) ≃ₗ[R] (n → R)) : m ≃ n :=
basis.index_equiv (basis.of_equiv_fun e.symm) (pi.basis_fun _ _)
namespace matrix
/-- If `M` and `M'` are each other's inverse matrices, they are square matrices up to
equivalence of types. -/
def index_equiv_of_inv [nontrivial A] [decidable_eq m] [decidable_eq n]
{M : matrix m n A} {M' : matrix n m A}
(hMM' : M ⬝ M' = 1) (hM'M : M' ⬝ M = 1) :
m ≃ n :=
equiv_of_pi_lequiv_pi (to_lin'_of_inv hMM' hM'M)
lemma det_comm [decidable_eq n] (M N : matrix n n A) : det (M ⬝ N) = det (N ⬝ M) :=
by rw [det_mul, det_mul, mul_comm]
/-- If there exists a two-sided inverse `M'` for `M` (indexed differently),
then `det (N ⬝ M) = det (M ⬝ N)`. -/
lemma det_comm' [decidable_eq m] [decidable_eq n]
{M : matrix n m A} {N : matrix m n A} {M' : matrix m n A}
(hMM' : M ⬝ M' = 1) (hM'M : M' ⬝ M = 1) :
det (M ⬝ N) = det (N ⬝ M) :=
begin
nontriviality A,
-- Although `m` and `n` are different a priori, we will show they have the same cardinality.
-- This turns the problem into one for square matrices, which is easy.
let e := index_equiv_of_inv hMM' hM'M,
rw [← det_submatrix_equiv_self e, ← submatrix_mul_equiv _ _ _ (equiv.refl n) _, det_comm,
submatrix_mul_equiv, equiv.coe_refl, submatrix_id_id]
end
/-- If `M'` is a two-sided inverse for `M` (indexed differently), `det (M ⬝ N ⬝ M') = det N`.
See `matrix.det_conj` and `matrix.det_conj'` for the case when `M' = M⁻¹` or vice versa. -/
lemma det_conj_of_mul_eq_one [decidable_eq m] [decidable_eq n]
{M : matrix m n A} {M' : matrix n m A} {N : matrix n n A}
(hMM' : M ⬝ M' = 1) (hM'M : M' ⬝ M = 1) :
det (M ⬝ N ⬝ M') = det N :=
by rw [← det_comm' hM'M hMM', ← matrix.mul_assoc, hM'M, matrix.one_mul]
end matrix
end conjugate
namespace linear_map
/-! ### Determinant of a linear map -/
variables {A : Type*} [comm_ring A] [module A M]
variables {κ : Type*} [fintype κ]
/-- The determinant of `linear_map.to_matrix` does not depend on the choice of basis. -/
lemma det_to_matrix_eq_det_to_matrix [decidable_eq κ]
(b : basis ι A M) (c : basis κ A M) (f : M →ₗ[A] M) :
det (linear_map.to_matrix b b f) = det (linear_map.to_matrix c c f) :=
by rw [← linear_map_to_matrix_mul_basis_to_matrix c b c,
← basis_to_matrix_mul_linear_map_to_matrix b c b,
matrix.det_conj_of_mul_eq_one]; rw [basis.to_matrix_mul_to_matrix, basis.to_matrix_self]
/-- The determinant of an endomorphism given a basis.
See `linear_map.det` for a version that populates the basis non-computably.
Although the `trunc (basis ι A M)` parameter makes it slightly more convenient to switch bases,
there is no good way to generalize over universe parameters, so we can't fully state in `det_aux`'s
type that it does not depend on the choice of basis. Instead you can use the `det_aux_def'` lemma,
or avoid mentioning a basis at all using `linear_map.det`.
-/
def det_aux : trunc (basis ι A M) → (M →ₗ[A] M) →* A :=
trunc.lift
(λ b : basis ι A M,
(det_monoid_hom).comp (to_matrix_alg_equiv b : (M →ₗ[A] M) →* matrix ι ι A))
(λ b c, monoid_hom.ext $ det_to_matrix_eq_det_to_matrix b c)
/-- Unfold lemma for `det_aux`.
See also `det_aux_def'` which allows you to vary the basis.
-/
lemma det_aux_def (b : basis ι A M) (f : M →ₗ[A] M) :
linear_map.det_aux (trunc.mk b) f = matrix.det (linear_map.to_matrix b b f) :=
rfl
-- Discourage the elaborator from unfolding `det_aux` and producing a huge term.
attribute [irreducible] linear_map.det_aux
lemma det_aux_def' {ι' : Type*} [fintype ι'] [decidable_eq ι']
(tb : trunc $ basis ι A M) (b' : basis ι' A M) (f : M →ₗ[A] M) :
linear_map.det_aux tb f = matrix.det (linear_map.to_matrix b' b' f) :=
by { apply trunc.induction_on tb, intro b, rw [det_aux_def, det_to_matrix_eq_det_to_matrix b b'] }
@[simp]
lemma det_aux_id (b : trunc $ basis ι A M) : linear_map.det_aux b (linear_map.id) = 1 :=
(linear_map.det_aux b).map_one
@[simp]
lemma det_aux_comp (b : trunc $ basis ι A M) (f g : M →ₗ[A] M) :
linear_map.det_aux b (f.comp g) = linear_map.det_aux b f * linear_map.det_aux b g :=
(linear_map.det_aux b).map_mul f g
section
open_locale classical
-- Discourage the elaborator from unfolding `det` and producing a huge term by marking it
-- as irreducible.
/-- The determinant of an endomorphism independent of basis.
If there is no finite basis on `M`, the result is `1` instead.
-/
@[irreducible] protected def det : (M →ₗ[A] M) →* A :=
if H : ∃ (s : finset M), nonempty (basis s A M)
then linear_map.det_aux (trunc.mk H.some_spec.some)
else 1
lemma coe_det [decidable_eq M] : ⇑(linear_map.det : (M →ₗ[A] M) →* A) =
if H : ∃ (s : finset M), nonempty (basis s A M)
then linear_map.det_aux (trunc.mk H.some_spec.some)
else 1 :=
by { ext, unfold linear_map.det,
split_ifs,
{ congr }, -- use the correct `decidable_eq` instance
refl }
end
-- Auxiliary lemma, the `simp` normal form goes in the other direction
-- (using `linear_map.det_to_matrix`)
lemma det_eq_det_to_matrix_of_finset [decidable_eq M]
{s : finset M} (b : basis s A M) (f : M →ₗ[A] M) :
f.det = matrix.det (linear_map.to_matrix b b f) :=
have ∃ (s : finset M), nonempty (basis s A M),
from ⟨s, ⟨b⟩⟩,
by rw [linear_map.coe_det, dif_pos, det_aux_def' _ b]; assumption
@[simp] lemma det_to_matrix
(b : basis ι A M) (f : M →ₗ[A] M) :
matrix.det (to_matrix b b f) = f.det :=
by { haveI := classical.dec_eq M,
rw [det_eq_det_to_matrix_of_finset b.reindex_finset_range, det_to_matrix_eq_det_to_matrix b] }
@[simp] lemma det_to_matrix' {ι : Type*} [fintype ι] [decidable_eq ι]
(f : (ι → A) →ₗ[A] (ι → A)) :
det f.to_matrix' = f.det :=
by simp [← to_matrix_eq_to_matrix']
@[simp] lemma det_to_lin (b : basis ι R M) (f : matrix ι ι R) :
linear_map.det (matrix.to_lin b b f) = f.det :=
by rw [← linear_map.det_to_matrix b, linear_map.to_matrix_to_lin]
/-- To show `P f.det` it suffices to consider `P (to_matrix _ _ f).det` and `P 1`. -/
@[elab_as_eliminator]
lemma det_cases [decidable_eq M] {P : A → Prop} (f : M →ₗ[A] M)
(hb : ∀ (s : finset M) (b : basis s A M), P (to_matrix b b f).det) (h1 : P 1) :
P f.det :=
begin
unfold linear_map.det,
split_ifs with h,
{ convert hb _ h.some_spec.some,
apply det_aux_def' },
{ exact h1 }
end
@[simp]
lemma det_comp (f g : M →ₗ[A] M) : (f.comp g).det = f.det * g.det :=
linear_map.det.map_mul f g
@[simp]
lemma det_id : (linear_map.id : M →ₗ[A] M).det = 1 :=
linear_map.det.map_one
/-- Multiplying a map by a scalar `c` multiplies its determinant by `c ^ dim M`. -/
@[simp] lemma det_smul {𝕜 : Type*} [field 𝕜] {M : Type*} [add_comm_group M] [module 𝕜 M]
(c : 𝕜) (f : M →ₗ[𝕜] M) :
linear_map.det (c • f) = c ^ (finite_dimensional.finrank 𝕜 M) * linear_map.det f :=
begin
by_cases H : ∃ (s : finset M), nonempty (basis s 𝕜 M),
{ haveI : finite_dimensional 𝕜 M,
{ rcases H with ⟨s, ⟨hs⟩⟩, exact finite_dimensional.of_finset_basis hs },
simp only [← det_to_matrix (finite_dimensional.fin_basis 𝕜 M), linear_equiv.map_smul,
fintype.card_fin, det_smul] },
{ classical,
have : finite_dimensional.finrank 𝕜 M = 0 := finrank_eq_zero_of_not_exists_basis H,
simp [coe_det, H, this] }
end
lemma det_zero' {ι : Type*} [fintype ι] [nonempty ι] (b : basis ι A M) :
linear_map.det (0 : M →ₗ[A] M) = 0 :=
by { haveI := classical.dec_eq ι,
rw [← det_to_matrix b, linear_equiv.map_zero, det_zero],
assumption }
/-- In a finite-dimensional vector space, the zero map has determinant `1` in dimension `0`,
and `0` otherwise. We give a formula that also works in infinite dimension, where we define
the determinant to be `1`. -/
@[simp] lemma det_zero {𝕜 : Type*} [field 𝕜] {M : Type*} [add_comm_group M] [module 𝕜 M] :
linear_map.det (0 : M →ₗ[𝕜] M) = (0 : 𝕜) ^ (finite_dimensional.finrank 𝕜 M) :=
by simp only [← zero_smul 𝕜 (1 : M →ₗ[𝕜] M), det_smul, mul_one, monoid_hom.map_one]
/-- Conjugating a linear map by a linear equiv does not change its determinant. -/
@[simp] lemma det_conj {N : Type*} [add_comm_group N] [module A N]
(f : M →ₗ[A] M) (e : M ≃ₗ[A] N) :
linear_map.det ((e : M →ₗ[A] N) ∘ₗ (f ∘ₗ (e.symm : N →ₗ[A] M))) = linear_map.det f :=
begin
classical,
by_cases H : ∃ (s : finset M), nonempty (basis s A M),
{ rcases H with ⟨s, ⟨b⟩⟩,
rw [← det_to_matrix b f, ← det_to_matrix (b.map e), to_matrix_comp (b.map e) b (b.map e),
to_matrix_comp (b.map e) b b, ← matrix.mul_assoc, matrix.det_conj_of_mul_eq_one],
{ rw [← to_matrix_comp, linear_equiv.comp_coe, e.symm_trans_self,
linear_equiv.refl_to_linear_map, to_matrix_id] },
{ rw [← to_matrix_comp, linear_equiv.comp_coe, e.self_trans_symm,
linear_equiv.refl_to_linear_map, to_matrix_id] } },
{ have H' : ¬ (∃ (t : finset N), nonempty (basis t A N)),
{ contrapose! H,
rcases H with ⟨s, ⟨b⟩⟩,
exact ⟨_, ⟨(b.map e.symm).reindex_finset_range⟩⟩ },
simp only [coe_det, H, H', pi.one_apply, dif_neg, not_false_iff] }
end
/-- If a linear map is invertible, so is its determinant. -/
lemma is_unit_det {A : Type*} [comm_ring A] [module A M]
(f : M →ₗ[A] M) (hf : is_unit f) : is_unit f.det :=
begin
obtain ⟨g, hg⟩ : ∃ g, f.comp g = 1 := hf.exists_right_inv,
have : linear_map.det f * linear_map.det g = 1,
by simp only [← linear_map.det_comp, hg, monoid_hom.map_one],
exact is_unit_of_mul_eq_one _ _ this,
end
/-- If a linear map has determinant different from `1`, then the space is finite-dimensional. -/
lemma finite_dimensional_of_det_ne_one {𝕜 : Type*} [field 𝕜] [module 𝕜 M]
(f : M →ₗ[𝕜] M) (hf : f.det ≠ 1) : finite_dimensional 𝕜 M :=
begin
by_cases H : ∃ (s : finset M), nonempty (basis s 𝕜 M),
{ rcases H with ⟨s, ⟨hs⟩⟩, exact finite_dimensional.of_finset_basis hs },
{ classical,
simp [linear_map.coe_det, H] at hf,
exact hf.elim }
end
/-- If the determinant of a map vanishes, then the map is not onto. -/
lemma range_lt_top_of_det_eq_zero {𝕜 : Type*} [field 𝕜] [module 𝕜 M]
{f : M →ₗ[𝕜] M} (hf : f.det = 0) : f.range < ⊤ :=
begin
haveI : finite_dimensional 𝕜 M, by simp [f.finite_dimensional_of_det_ne_one, hf],
contrapose hf,
simp only [lt_top_iff_ne_top, not_not, ← is_unit_iff_range_eq_top] at hf,
exact is_unit_iff_ne_zero.1 (f.is_unit_det hf)
end
/-- If the determinant of a map vanishes, then the map is not injective. -/
lemma bot_lt_ker_of_det_eq_zero {𝕜 : Type*} [field 𝕜] [module 𝕜 M]
{f : M →ₗ[𝕜] M} (hf : f.det = 0) : ⊥ < f.ker :=
begin
haveI : finite_dimensional 𝕜 M, by simp [f.finite_dimensional_of_det_ne_one, hf],
contrapose hf,
simp only [bot_lt_iff_ne_bot, not_not, ← is_unit_iff_ker_eq_bot] at hf,
exact is_unit_iff_ne_zero.1 (f.is_unit_det hf)
end
end linear_map
namespace linear_equiv
/-- On a `linear_equiv`, the domain of `linear_map.det` can be promoted to `Rˣ`. -/
protected def det : (M ≃ₗ[R] M) →* Rˣ :=
(units.map (linear_map.det : (M →ₗ[R] M) →* R)).comp
(linear_map.general_linear_group.general_linear_equiv R M).symm.to_monoid_hom
@[simp] lemma coe_det (f : M ≃ₗ[R] M) : ↑f.det = linear_map.det (f : M →ₗ[R] M) := rfl
@[simp] lemma coe_inv_det (f : M ≃ₗ[R] M) : ↑(f.det⁻¹) = linear_map.det (f.symm : M →ₗ[R] M) := rfl
@[simp] lemma det_refl : (linear_equiv.refl R M).det = 1 := units.ext $ linear_map.det_id
@[simp] lemma det_trans (f g : M ≃ₗ[R] M) : (f.trans g).det = g.det * f.det := map_mul _ g f
@[simp] lemma det_symm (f : M ≃ₗ[R] M) : f.symm.det = f.det⁻¹ := map_inv _ f
/-- Conjugating a linear equiv by a linear equiv does not change its determinant. -/
@[simp] lemma det_conj (f : M ≃ₗ[R] M) (e : M ≃ₗ[R] M') :
((e.symm.trans f).trans e).det = f.det :=
by rw [←units.eq_iff, coe_det, coe_det, ←comp_coe, ←comp_coe, linear_map.det_conj]
end linear_equiv
/-- The determinants of a `linear_equiv` and its inverse multiply to 1. -/
@[simp] lemma linear_equiv.det_mul_det_symm {A : Type*} [comm_ring A] [module A M]
(f : M ≃ₗ[A] M) : (f : M →ₗ[A] M).det * (f.symm : M →ₗ[A] M).det = 1 :=
by simp [←linear_map.det_comp]
/-- The determinants of a `linear_equiv` and its inverse multiply to 1. -/
@[simp] lemma linear_equiv.det_symm_mul_det {A : Type*} [comm_ring A] [module A M]
(f : M ≃ₗ[A] M) : (f.symm : M →ₗ[A] M).det * (f : M →ₗ[A] M).det = 1 :=
by simp [←linear_map.det_comp]
-- Cannot be stated using `linear_map.det` because `f` is not an endomorphism.
lemma linear_equiv.is_unit_det (f : M ≃ₗ[R] M') (v : basis ι R M) (v' : basis ι R M') :
is_unit (linear_map.to_matrix v v' f).det :=
begin
apply is_unit_det_of_left_inverse,
simpa using (linear_map.to_matrix_comp v v' v f.symm f).symm
end
/-- Specialization of `linear_equiv.is_unit_det` -/
lemma linear_equiv.is_unit_det' {A : Type*} [comm_ring A] [module A M]
(f : M ≃ₗ[A] M) : is_unit (linear_map.det (f : M →ₗ[A] M)) :=
is_unit_of_mul_eq_one _ _ f.det_mul_det_symm
/-- The determinant of `f.symm` is the inverse of that of `f` when `f` is a linear equiv. -/
lemma linear_equiv.det_coe_symm {𝕜 : Type*} [field 𝕜] [module 𝕜 M]
(f : M ≃ₗ[𝕜] M) : (f.symm : M →ₗ[𝕜] M).det = (f : M →ₗ[𝕜] M).det ⁻¹ :=
by field_simp [is_unit.ne_zero f.is_unit_det']
/-- Builds a linear equivalence from a linear map whose determinant in some bases is a unit. -/
@[simps]
def linear_equiv.of_is_unit_det {f : M →ₗ[R] M'} {v : basis ι R M} {v' : basis ι R M'}
(h : is_unit (linear_map.to_matrix v v' f).det) : M ≃ₗ[R] M' :=
{ to_fun := f,
map_add' := f.map_add,
map_smul' := f.map_smul,
inv_fun := to_lin v' v (to_matrix v v' f)⁻¹,
left_inv := λ x,
calc to_lin v' v (to_matrix v v' f)⁻¹ (f x)
= to_lin v v ((to_matrix v v' f)⁻¹ ⬝ to_matrix v v' f) x :
by { rw [to_lin_mul v v' v, to_lin_to_matrix, linear_map.comp_apply] }
... = x : by simp [h],
right_inv := λ x,
calc f (to_lin v' v (to_matrix v v' f)⁻¹ x)
= to_lin v' v' (to_matrix v v' f ⬝ (to_matrix v v' f)⁻¹) x :
by { rw [to_lin_mul v' v v', linear_map.comp_apply, to_lin_to_matrix v v'] }
... = x : by simp [h] }
@[simp] lemma linear_equiv.coe_of_is_unit_det {f : M →ₗ[R] M'} {v : basis ι R M} {v' : basis ι R M'}
(h : is_unit (linear_map.to_matrix v v' f).det) :
(linear_equiv.of_is_unit_det h : M →ₗ[R] M') = f :=
by { ext x, refl }
/-- Builds a linear equivalence from a linear map on a finite-dimensional vector space whose
determinant is nonzero. -/
@[reducible] def linear_map.equiv_of_det_ne_zero
{𝕜 : Type*} [field 𝕜] {M : Type*} [add_comm_group M] [module 𝕜 M]
[finite_dimensional 𝕜 M] (f : M →ₗ[𝕜] M) (hf : linear_map.det f ≠ 0) :
M ≃ₗ[𝕜] M :=
have is_unit (linear_map.to_matrix (finite_dimensional.fin_basis 𝕜 M)
(finite_dimensional.fin_basis 𝕜 M) f).det :=
by simp only [linear_map.det_to_matrix, is_unit_iff_ne_zero.2 hf],
linear_equiv.of_is_unit_det this
lemma linear_map.associated_det_of_eq_comp (e : M ≃ₗ[R] M) (f f' : M →ₗ[R] M)
(h : ∀ x, f x = f' (e x)) : associated f.det f'.det :=
begin
suffices : associated (f' ∘ₗ ↑e).det f'.det,
{ convert this using 2, ext x, exact h x },
rw [← mul_one f'.det, linear_map.det_comp],
exact associated.mul_left _ (associated_one_iff_is_unit.mpr e.is_unit_det')
end
lemma linear_map.associated_det_comp_equiv {N : Type*} [add_comm_group N] [module R N]
(f : N →ₗ[R] M) (e e' : M ≃ₗ[R] N) :
associated (f ∘ₗ ↑e).det (f ∘ₗ ↑e').det :=
begin
refine linear_map.associated_det_of_eq_comp (e.trans e'.symm) _ _ _,
intro x,
simp only [linear_map.comp_apply, linear_equiv.coe_coe, linear_equiv.trans_apply,
linear_equiv.apply_symm_apply],
end
/-- The determinant of a family of vectors with respect to some basis, as an alternating
multilinear map. -/
def basis.det : alternating_map R M R ι :=
{ to_fun := λ v, det (e.to_matrix v),
map_add' := begin
intros v i x y,
simp only [e.to_matrix_update, linear_equiv.map_add],
apply det_update_column_add
end,
map_smul' := begin
intros u i c x,
simp only [e.to_matrix_update, algebra.id.smul_eq_mul, linear_equiv.map_smul],
apply det_update_column_smul
end,
map_eq_zero_of_eq' := begin
intros v i j h hij,
rw [←function.update_eq_self i v, h, ←det_transpose, e.to_matrix_update,
←update_row_transpose, ←e.to_matrix_transpose_apply],
apply det_zero_of_row_eq hij,
rw [update_row_ne hij.symm, update_row_self],
end }
lemma basis.det_apply (v : ι → M) : e.det v = det (e.to_matrix v) := rfl
lemma basis.det_self : e.det e = 1 :=
by simp [e.det_apply]
/-- `basis.det` is not the zero map. -/
lemma basis.det_ne_zero [nontrivial R] : e.det ≠ 0 :=
λ h, by simpa [h] using e.det_self
lemma is_basis_iff_det {v : ι → M} :
linear_independent R v ∧ span R (set.range v) = ⊤ ↔ is_unit (e.det v) :=
begin
split,
{ rintro ⟨hli, hspan⟩,
set v' := basis.mk hli hspan.ge with v'_eq,
rw e.det_apply,
convert linear_equiv.is_unit_det (linear_equiv.refl _ _) v' e using 2,
ext i j,
simp },
{ intro h,
rw [basis.det_apply, basis.to_matrix_eq_to_matrix_constr] at h,
set v' := basis.map e (linear_equiv.of_is_unit_det h) with v'_def,
have : ⇑ v' = v,
{ ext i, rw [v'_def, basis.map_apply, linear_equiv.of_is_unit_det_apply, e.constr_basis] },
rw ← this,
exact ⟨v'.linear_independent, v'.span_eq⟩ },
end
lemma basis.is_unit_det (e' : basis ι R M) : is_unit (e.det e') :=
(is_basis_iff_det e).mp ⟨e'.linear_independent, e'.span_eq⟩
/-- Any alternating map to `R` where `ι` has the cardinality of a basis equals the determinant
map with respect to that basis, multiplied by the value of that alternating map on that basis. -/
lemma alternating_map.eq_smul_basis_det (f : alternating_map R M R ι) : f = f e • e.det :=
begin
refine basis.ext_alternating e (λ i h, _),
let σ : equiv.perm ι := equiv.of_bijective i (finite.injective_iff_bijective.1 h),
change f (e ∘ σ) = (f e • e.det) (e ∘ σ),
simp [alternating_map.map_perm, basis.det_self]
end
@[simp] lemma alternating_map.map_basis_eq_zero_iff (f : alternating_map R M R ι) :
f e = 0 ↔ f = 0 :=
⟨λ h, by simpa [h] using f.eq_smul_basis_det e, λ h, h.symm ▸ alternating_map.zero_apply _⟩
lemma alternating_map.map_basis_ne_zero_iff (f : alternating_map R M R ι) :
f e ≠ 0 ↔ f ≠ 0 :=
not_congr $ f.map_basis_eq_zero_iff e
variables {A : Type*} [comm_ring A] [module A M]
@[simp] lemma basis.det_comp (e : basis ι A M) (f : M →ₗ[A] M) (v : ι → M) :
e.det (f ∘ v) = f.det * e.det v :=
by { rw [basis.det_apply, basis.det_apply, ← f.det_to_matrix e, ← matrix.det_mul,
e.to_matrix_eq_to_matrix_constr (f ∘ v), e.to_matrix_eq_to_matrix_constr v,
← to_matrix_comp, e.constr_comp] }
lemma basis.det_reindex {ι' : Type*} [fintype ι'] [decidable_eq ι']
(b : basis ι R M) (v : ι' → M) (e : ι ≃ ι') :
(b.reindex e).det v = b.det (v ∘ e) :=
by rw [basis.det_apply, basis.to_matrix_reindex', det_reindex_alg_equiv, basis.det_apply]
lemma basis.det_reindex_symm {ι' : Type*} [fintype ι'] [decidable_eq ι']
(b : basis ι R M) (v : ι → M) (e : ι' ≃ ι) :
(b.reindex e.symm).det (v ∘ e) = b.det v :=
by rw [basis.det_reindex, function.comp.assoc, e.self_comp_symm, function.comp.right_id]
@[simp]
lemma basis.det_map (b : basis ι R M) (f : M ≃ₗ[R] M') (v : ι → M') :
(b.map f).det v = b.det (f.symm ∘ v) :=
by { rw [basis.det_apply, basis.to_matrix_map, basis.det_apply] }
lemma basis.det_map' (b : basis ι R M) (f : M ≃ₗ[R] M') :
(b.map f).det = b.det.comp_linear_map f.symm :=
alternating_map.ext $ b.det_map f
@[simp] lemma pi.basis_fun_det : (pi.basis_fun R ι).det = matrix.det_row_alternating :=
begin
ext M,
rw [basis.det_apply, basis.coe_pi_basis_fun.to_matrix_eq_transpose, det_transpose],
end
/-- If we fix a background basis `e`, then for any other basis `v`, we can characterise the
coordinates provided by `v` in terms of determinants relative to `e`. -/
lemma basis.det_smul_mk_coord_eq_det_update {v : ι → M}
(hli : linear_independent R v) (hsp : ⊤ ≤ span R (range v)) (i : ι) :
(e.det v) • (basis.mk hli hsp).coord i = e.det.to_multilinear_map.to_linear_map v i :=
begin
apply (basis.mk hli hsp).ext,
intros k,
rcases eq_or_ne k i with rfl | hik;
simp only [algebra.id.smul_eq_mul, basis.coe_mk, linear_map.smul_apply, linear_map.coe_mk,
multilinear_map.to_linear_map_apply],
{ rw [basis.mk_coord_apply_eq, mul_one, update_eq_self], congr, },
{ rw [basis.mk_coord_apply_ne hik, mul_zero, eq_comm],
exact e.det.map_eq_zero_of_eq _ (by simp [hik, function.update_apply]) hik, },
end
/-- The determinant of a basis constructed by `units_smul` is the product of the given units. -/
@[simp] lemma basis.det_units_smul (w : ι → Rˣ) : e.det (e.units_smul w) = ∏ i, w i :=
by simp [basis.det_apply]
/-- The determinant of a basis constructed by `is_unit_smul` is the product of the given units. -/
@[simp] lemma basis.det_is_unit_smul {w : ι → R} (hw : ∀ i, is_unit (w i)) :
e.det (e.is_unit_smul hw) = ∏ i, w i :=
e.det_units_smul _
|
06ed7c25576ab5118a9c744ac225f111f9bcfc3a | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/polynomial/splits.lean | e833cce8ec92d313290f6cbd3347cf8a00b0713d | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 17,568 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import data.list.prime
import data.polynomial.field_division
import data.polynomial.lifts
/-!
# Split polynomials
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
A polynomial `f : K[X]` splits over a field extension `L` of `K` if it is zero or all of its
irreducible factors over `L` have degree `1`.
## Main definitions
* `polynomial.splits i f`: A predicate on a homomorphism `i : K →+* L` from a commutative ring to a
field and a polynomial `f` saying that `f.map i` is zero or all of its irreducible factors over
`L` have degree `1`.
## Main statements
* `lift_of_splits`: If `K` and `L` are field extensions of a field `F` and for some finite subset
`S` of `K`, the minimal polynomial of every `x ∈ K` splits as a polynomial with coefficients in
`L`, then `algebra.adjoin F S` embeds into `L`.
-/
noncomputable theory
open_locale classical big_operators polynomial
universes u v w
variables {F : Type u} {K : Type v} {L : Type w}
namespace polynomial
open polynomial
section splits
section comm_ring
variables [comm_ring K] [field L] [field F]
variables (i : K →+* L)
/-- A polynomial `splits` iff it is zero or all of its irreducible factors have `degree` 1. -/
def splits (f : K[X]) : Prop :=
f.map i = 0 ∨ ∀ {g : L[X]}, irreducible g → g ∣ f.map i → degree g = 1
@[simp] lemma splits_zero : splits i (0 : K[X]) := or.inl (polynomial.map_zero i)
lemma splits_of_map_eq_C {f : K[X]} {a : L} (h : f.map i = C a) : splits i f :=
if ha : a = 0 then or.inl (h.trans (ha.symm ▸ C_0))
else or.inr $ λ g hg ⟨p, hp⟩, absurd hg.1 $ not_not.2 $ is_unit_iff_degree_eq_zero.2 $
begin
have := congr_arg degree hp,
rw [h, degree_C ha, degree_mul, @eq_comm (with_bot ℕ) 0, nat.with_bot.add_eq_zero_iff] at this,
exact this.1,
end
@[simp] lemma splits_C (a : K) : splits i (C a) := splits_of_map_eq_C i (map_C i)
lemma splits_of_map_degree_eq_one {f : K[X]} (hf : degree (f.map i) = 1) : splits i f :=
or.inr $ λ g hg ⟨p, hp⟩,
by have := congr_arg degree hp;
simp [nat.with_bot.add_eq_one_iff, hf, @eq_comm (with_bot ℕ) 1,
mt is_unit_iff_degree_eq_zero.2 hg.1] at this;
clear _fun_match; tauto
lemma splits_of_degree_le_one {f : K[X]} (hf : degree f ≤ 1) : splits i f :=
if hif : degree (f.map i) ≤ 0 then splits_of_map_eq_C i (degree_le_zero_iff.mp hif)
else begin
push_neg at hif,
rw [← order.succ_le_iff, ← with_bot.coe_zero, with_bot.succ_coe, nat.succ_eq_succ] at hif,
exact splits_of_map_degree_eq_one i (le_antisymm ((degree_map_le i _).trans hf) hif),
end
lemma splits_of_degree_eq_one {f : K[X]} (hf : degree f = 1) : splits i f :=
splits_of_degree_le_one i hf.le
lemma splits_of_nat_degree_le_one {f : K[X]} (hf : nat_degree f ≤ 1) : splits i f :=
splits_of_degree_le_one i (degree_le_of_nat_degree_le hf)
lemma splits_of_nat_degree_eq_one {f : K[X]} (hf : nat_degree f = 1) : splits i f :=
splits_of_nat_degree_le_one i (le_of_eq hf)
lemma splits_mul {f g : K[X]} (hf : splits i f) (hg : splits i g) : splits i (f * g) :=
if h : (f * g).map i = 0 then or.inl h
else or.inr $ λ p hp hpf, ((principal_ideal_ring.irreducible_iff_prime.1 hp).2.2 _ _
(show p ∣ map i f * map i g, by convert hpf; rw polynomial.map_mul)).elim
(hf.resolve_left (λ hf, by simpa [hf] using h) hp)
(hg.resolve_left (λ hg, by simpa [hg] using h) hp)
lemma splits_of_splits_mul' {f g : K[X]} (hfg : (f * g).map i ≠ 0) (h : splits i (f * g)) :
splits i f ∧ splits i g :=
⟨or.inr $ λ g hgi hg, or.resolve_left h hfg hgi
(by rw polynomial.map_mul; exact hg.trans (dvd_mul_right _ _)),
or.inr $ λ g hgi hg, or.resolve_left h hfg hgi
(by rw polynomial.map_mul; exact hg.trans (dvd_mul_left _ _))⟩
lemma splits_map_iff (j : L →+* F) {f : K[X]} :
splits j (f.map i) ↔ splits (j.comp i) f :=
by simp [splits, polynomial.map_map]
theorem splits_one : splits i 1 :=
splits_C i 1
theorem splits_of_is_unit [is_domain K] {u : K[X]} (hu : is_unit u) : u.splits i :=
(is_unit_iff.mp hu).some_spec.2 ▸ splits_C _ _
theorem splits_X_sub_C {x : K} : (X - C x).splits i :=
splits_of_degree_le_one _ $ degree_X_sub_C_le _
theorem splits_X : X.splits i :=
splits_of_degree_le_one _ degree_X_le
theorem splits_prod {ι : Type u} {s : ι → K[X]} {t : finset ι} :
(∀ j ∈ t, (s j).splits i) → (∏ x in t, s x).splits i :=
begin
refine finset.induction_on t (λ _, splits_one i) (λ a t hat ih ht, _),
rw finset.forall_mem_insert at ht, rw finset.prod_insert hat,
exact splits_mul i ht.1 (ih ht.2)
end
lemma splits_pow {f : K[X]} (hf : f.splits i) (n : ℕ) : (f ^ n).splits i :=
begin
rw [←finset.card_range n, ←finset.prod_const],
exact splits_prod i (λ j hj, hf),
end
lemma splits_X_pow (n : ℕ) : (X ^ n).splits i := splits_pow i (splits_X i) n
theorem splits_id_iff_splits {f : K[X]} :
(f.map i).splits (ring_hom.id L) ↔ f.splits i :=
by rw [splits_map_iff, ring_hom.id_comp]
lemma exists_root_of_splits' {f : K[X]} (hs : splits i f) (hf0 : degree (f.map i) ≠ 0) :
∃ x, eval₂ i x f = 0 :=
if hf0' : f.map i = 0 then by simp [eval₂_eq_eval_map, hf0']
else
let ⟨g, hg⟩ := wf_dvd_monoid.exists_irreducible_factor
(show ¬ is_unit (f.map i), from mt is_unit_iff_degree_eq_zero.1 hf0) hf0' in
let ⟨x, hx⟩ := exists_root_of_degree_eq_one (hs.resolve_left hf0' hg.1 hg.2) in
let ⟨i, hi⟩ := hg.2 in
⟨x, by rw [← eval_map, hi, eval_mul, show _ = _, from hx, zero_mul]⟩
lemma roots_ne_zero_of_splits' {f : K[X]} (hs : splits i f) (hf0 : nat_degree (f.map i) ≠ 0) :
(f.map i).roots ≠ 0 :=
let ⟨x, hx⟩ := exists_root_of_splits' i hs (λ h, hf0 $ nat_degree_eq_of_degree_eq_some h) in
λ h, by { rw ← eval_map at hx,
cases h.subst ((mem_roots _).2 hx), exact ne_zero_of_nat_degree_gt (nat.pos_of_ne_zero hf0) }
/-- Pick a root of a polynomial that splits. See `root_of_splits` for polynomials over a field
which has simpler assumptions. -/
def root_of_splits' {f : K[X]} (hf : f.splits i) (hfd : (f.map i).degree ≠ 0) : L :=
classical.some $ exists_root_of_splits' i hf hfd
theorem map_root_of_splits' {f : K[X]} (hf : f.splits i) (hfd) :
f.eval₂ i (root_of_splits' i hf hfd) = 0 :=
classical.some_spec $ exists_root_of_splits' i hf hfd
lemma nat_degree_eq_card_roots' {p : K[X]} {i : K →+* L}
(hsplit : splits i p) : (p.map i).nat_degree = (p.map i).roots.card :=
begin
by_cases hp : p.map i = 0,
{ rw [hp, nat_degree_zero, roots_zero, multiset.card_zero] },
obtain ⟨q, he, hd, hr⟩ := exists_prod_multiset_X_sub_C_mul (p.map i),
rw [← splits_id_iff_splits, ← he] at hsplit,
rw ← he at hp,
have hq : q ≠ 0 := λ h, hp (by rw [h, mul_zero]),
rw [← hd, add_right_eq_self],
by_contra,
have h' : (map (ring_hom.id L) q).nat_degree ≠ 0, { simp [h], },
have := roots_ne_zero_of_splits' (ring_hom.id L) (splits_of_splits_mul' _ _ hsplit).2 h',
{ rw map_id at this, exact this hr },
{ rw [map_id], exact mul_ne_zero monic_prod_multiset_X_sub_C.ne_zero hq },
end
lemma degree_eq_card_roots' {p : K[X]} {i : K →+* L} (p_ne_zero : p.map i ≠ 0)
(hsplit : splits i p) : (p.map i).degree = (p.map i).roots.card :=
by rw [degree_eq_nat_degree p_ne_zero, nat_degree_eq_card_roots' hsplit]
end comm_ring
variables [field K] [field L] [field F]
variables (i : K →+* L)
/-- This lemma is for polynomials over a field. -/
lemma splits_iff (f : K[X]) :
splits i f ↔ f = 0 ∨ ∀ {g : L[X]}, irreducible g → g ∣ f.map i → degree g = 1 :=
by rw [splits, map_eq_zero]
/-- This lemma is for polynomials over a field. -/
lemma splits.def {i : K →+* L} {f : K[X]} (h : splits i f) :
f = 0 ∨ ∀ {g : L[X]}, irreducible g → g ∣ f.map i → degree g = 1 :=
(splits_iff i f).mp h
lemma splits_of_splits_mul {f g : K[X]} (hfg : f * g ≠ 0) (h : splits i (f * g)) :
splits i f ∧ splits i g :=
splits_of_splits_mul' i (map_ne_zero hfg) h
lemma splits_of_splits_of_dvd {f g : K[X]} (hf0 : f ≠ 0) (hf : splits i f) (hgf : g ∣ f) :
splits i g :=
by { obtain ⟨f, rfl⟩ := hgf, exact (splits_of_splits_mul i hf0 hf).1 }
lemma splits_of_splits_gcd_left {f g : K[X]} (hf0 : f ≠ 0) (hf : splits i f) :
splits i (euclidean_domain.gcd f g) :=
polynomial.splits_of_splits_of_dvd i hf0 hf (euclidean_domain.gcd_dvd_left f g)
lemma splits_of_splits_gcd_right {f g : K[X]} (hg0 : g ≠ 0) (hg : splits i g) :
splits i (euclidean_domain.gcd f g) :=
polynomial.splits_of_splits_of_dvd i hg0 hg (euclidean_domain.gcd_dvd_right f g)
theorem splits_mul_iff {f g : K[X]} (hf : f ≠ 0) (hg : g ≠ 0) :
(f * g).splits i ↔ f.splits i ∧ g.splits i :=
⟨splits_of_splits_mul i (mul_ne_zero hf hg), λ ⟨hfs, hgs⟩, splits_mul i hfs hgs⟩
theorem splits_prod_iff {ι : Type u} {s : ι → K[X]} {t : finset ι} :
(∀ j ∈ t, s j ≠ 0) → ((∏ x in t, s x).splits i ↔ ∀ j ∈ t, (s j).splits i) :=
begin
refine finset.induction_on t (λ _, ⟨λ _ _ h, h.elim, λ _, splits_one i⟩) (λ a t hat ih ht, _),
rw finset.forall_mem_insert at ht ⊢,
rw [finset.prod_insert hat, splits_mul_iff i ht.1 (finset.prod_ne_zero_iff.2 ht.2), ih ht.2]
end
lemma degree_eq_one_of_irreducible_of_splits {p : K[X]}
(hp : irreducible p) (hp_splits : splits (ring_hom.id K) p) :
p.degree = 1 :=
begin
rcases hp_splits,
{ exfalso, simp * at *, },
{ apply hp_splits hp, simp }
end
lemma exists_root_of_splits {f : K[X]} (hs : splits i f) (hf0 : degree f ≠ 0) :
∃ x, eval₂ i x f = 0 :=
exists_root_of_splits' i hs ((f.degree_map i).symm ▸ hf0)
lemma roots_ne_zero_of_splits {f : K[X]} (hs : splits i f) (hf0 : nat_degree f ≠ 0) :
(f.map i).roots ≠ 0 :=
roots_ne_zero_of_splits' i hs (ne_of_eq_of_ne (nat_degree_map i) hf0)
/-- Pick a root of a polynomial that splits. This version is for polynomials over a field and has
simpler assumptions. -/
def root_of_splits {f : K[X]} (hf : f.splits i) (hfd : f.degree ≠ 0) : L :=
root_of_splits' i hf ((f.degree_map i).symm ▸ hfd)
/-- `root_of_splits'` is definitionally equal to `root_of_splits`. -/
lemma root_of_splits'_eq_root_of_splits {f : K[X]} (hf : f.splits i) (hfd) :
root_of_splits' i hf hfd = root_of_splits i hf (f.degree_map i ▸ hfd) := rfl
theorem map_root_of_splits {f : K[X]} (hf : f.splits i) (hfd) :
f.eval₂ i (root_of_splits i hf hfd) = 0 :=
map_root_of_splits' i hf (ne_of_eq_of_ne (degree_map f i) hfd)
lemma nat_degree_eq_card_roots {p : K[X]} {i : K →+* L}
(hsplit : splits i p) : p.nat_degree = (p.map i).roots.card :=
(nat_degree_map i).symm.trans $ nat_degree_eq_card_roots' hsplit
lemma degree_eq_card_roots {p : K[X]} {i : K →+* L} (p_ne_zero : p ≠ 0)
(hsplit : splits i p) : p.degree = (p.map i).roots.card :=
by rw [degree_eq_nat_degree p_ne_zero, nat_degree_eq_card_roots hsplit]
theorem roots_map {f : K[X]} (hf : f.splits $ ring_hom.id K) :
(f.map i).roots = f.roots.map i :=
(roots_map_of_injective_of_card_eq_nat_degree i.injective $
by { convert (nat_degree_eq_card_roots hf).symm, rw map_id }).symm
lemma image_root_set [algebra F K] [algebra F L] {p : F[X]} (h : p.splits (algebra_map F K))
(f : K →ₐ[F] L) : f '' p.root_set K = p.root_set L :=
begin
classical,
rw [root_set, ←finset.coe_image, ←multiset.to_finset_map, ←f.coe_to_ring_hom, ←roots_map ↑f
((splits_id_iff_splits (algebra_map F K)).mpr h), map_map, f.comp_algebra_map, ←root_set],
end
lemma adjoin_root_set_eq_range [algebra F K] [algebra F L] {p : F[X]}
(h : p.splits (algebra_map F K)) (f : K →ₐ[F] L) :
algebra.adjoin F (p.root_set L) = f.range ↔ algebra.adjoin F (p.root_set K) = ⊤ :=
begin
rw [←image_root_set h f, algebra.adjoin_image, ←algebra.map_top],
exact (subalgebra.map_injective f.to_ring_hom.injective).eq_iff,
end
lemma eq_prod_roots_of_splits {p : K[X]} {i : K →+* L} (hsplit : splits i p) :
p.map i = C (i p.leading_coeff) * ((p.map i).roots.map (λ a, X - C a)).prod :=
begin
rw ← leading_coeff_map, symmetry,
apply C_leading_coeff_mul_prod_multiset_X_sub_C,
rw nat_degree_map, exact (nat_degree_eq_card_roots hsplit).symm,
end
lemma eq_prod_roots_of_splits_id {p : K[X]}
(hsplit : splits (ring_hom.id K) p) :
p = C p.leading_coeff * (p.roots.map (λ a, X - C a)).prod :=
by simpa using eq_prod_roots_of_splits hsplit
lemma eq_prod_roots_of_monic_of_splits_id {p : K[X]}
(m : monic p) (hsplit : splits (ring_hom.id K) p) :
p = (p.roots.map (λ a, X - C a)).prod :=
begin
convert eq_prod_roots_of_splits_id hsplit,
simp [m],
end
lemma eq_X_sub_C_of_splits_of_single_root {x : K} {h : K[X]} (h_splits : splits i h)
(h_roots : (h.map i).roots = {i x}) : h = C h.leading_coeff * (X - C x) :=
begin
apply polynomial.map_injective _ i.injective,
rw [eq_prod_roots_of_splits h_splits, h_roots],
simp,
end
theorem mem_lift_of_splits_of_roots_mem_range (R : Type*) [comm_ring R] [algebra R K] {f : K[X]}
(hs : f.splits (ring_hom.id K)) (hm : f.monic)
(hr : ∀ a ∈ f.roots, a ∈ (algebra_map R K).range) : f ∈ polynomial.lifts (algebra_map R K) :=
begin
rw [eq_prod_roots_of_monic_of_splits_id hm hs, lifts_iff_lifts_ring],
refine subring.multiset_prod_mem _ _ (λ P hP, _),
obtain ⟨b, hb, rfl⟩ := multiset.mem_map.1 hP,
exact subring.sub_mem _ (X_mem_lifts _) (C'_mem_lifts (hr _ hb))
end
section UFD
local attribute [instance, priority 10] principal_ideal_ring.to_unique_factorization_monoid
local infix ` ~ᵤ ` : 50 := associated
open unique_factorization_monoid associates
lemma splits_of_exists_multiset {f : K[X]} {s : multiset L}
(hs : f.map i = C (i f.leading_coeff) * (s.map (λ a : L, X - C a)).prod) :
splits i f :=
if hf0 : f = 0 then hf0.symm ▸ splits_zero i
else or.inr $ λ p hp hdp, begin
rw irreducible_iff_prime at hp,
rw [hs, ← multiset.prod_to_list] at hdp,
obtain (hd|hd) := hp.2.2 _ _ hdp,
{ refine (hp.2.1 $ is_unit_of_dvd_unit hd _).elim,
exact is_unit_C.2 ((leading_coeff_ne_zero.2 hf0).is_unit.map i) },
{ obtain ⟨q, hq, hd⟩ := hp.dvd_prod_iff.1 hd,
obtain ⟨a, ha, rfl⟩ := multiset.mem_map.1 (multiset.mem_to_list.1 hq),
rw degree_eq_degree_of_associated ((hp.dvd_prime_iff_associated $ prime_X_sub_C a).1 hd),
exact degree_X_sub_C a },
end
lemma splits_of_splits_id {f : K[X]} : splits (ring_hom.id K) f → splits i f :=
unique_factorization_monoid.induction_on_prime f (λ _, splits_zero _)
(λ _ hu _, splits_of_degree_le_one _
((is_unit_iff_degree_eq_zero.1 hu).symm ▸ dec_trivial))
(λ a p ha0 hp ih hfi, splits_mul _
(splits_of_degree_eq_one _
((splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).1.def.resolve_left
hp.1 hp.irreducible (by rw map_id)))
(ih (splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).2))
end UFD
lemma splits_iff_exists_multiset {f : K[X]} : splits i f ↔
∃ (s : multiset L), f.map i = C (i f.leading_coeff) * (s.map (λ a : L, X - C a)).prod :=
⟨λ hf, ⟨(f.map i).roots, eq_prod_roots_of_splits hf⟩, λ ⟨s, hs⟩, splits_of_exists_multiset i hs⟩
lemma splits_comp_of_splits (j : L →+* F) {f : K[X]}
(h : splits i f) : splits (j.comp i) f :=
begin
change i with ((ring_hom.id _).comp i) at h,
rw [← splits_map_iff],
rw [← splits_map_iff i] at h,
exact splits_of_splits_id _ h
end
/-- A polynomial splits if and only if it has as many roots as its degree. -/
lemma splits_iff_card_roots {p : K[X]} :
splits (ring_hom.id K) p ↔ p.roots.card = p.nat_degree :=
begin
split,
{ intro H, rw [nat_degree_eq_card_roots H, map_id] },
{ intro hroots,
rw splits_iff_exists_multiset (ring_hom.id K),
use p.roots,
simp only [ring_hom.id_apply, map_id],
exact (C_leading_coeff_mul_prod_multiset_X_sub_C hroots).symm },
end
lemma aeval_root_derivative_of_splits [algebra K L] {P : K[X]} (hmo : P.monic)
(hP : P.splits (algebra_map K L)) {r : L} (hr : r ∈ (P.map (algebra_map K L)).roots) :
aeval r P.derivative = (((P.map $ algebra_map K L).roots.erase r).map (λ a, r - a)).prod :=
begin
replace hmo := hmo.map (algebra_map K L),
replace hP := (splits_id_iff_splits (algebra_map K L)).2 hP,
rw [aeval_def, ← eval_map, ← derivative_map],
nth_rewrite 0 [eq_prod_roots_of_monic_of_splits_id hmo hP],
rw [eval_multiset_prod_X_sub_C_derivative hr]
end
/-- If `P` is a monic polynomial that splits, then `coeff P 0` equals the product of the roots. -/
lemma prod_roots_eq_coeff_zero_of_monic_of_split {P : K[X]} (hmo : P.monic)
(hP : P.splits (ring_hom.id K)) : coeff P 0 = (-1) ^ P.nat_degree * P.roots.prod :=
begin
nth_rewrite 0 [eq_prod_roots_of_monic_of_splits_id hmo hP],
rw [coeff_zero_eq_eval_zero, eval_multiset_prod, multiset.map_map],
simp_rw [function.comp_app, eval_sub, eval_X, zero_sub, eval_C],
conv_lhs { congr, congr, funext,
rw [neg_eq_neg_one_mul] },
rw [multiset.prod_map_mul, multiset.map_const, multiset.prod_replicate, multiset.map_id',
splits_iff_card_roots.1 hP]
end
/-- If `P` is a monic polynomial that splits, then `P.next_coeff` equals the sum of the roots. -/
lemma sum_roots_eq_next_coeff_of_monic_of_split {P : K[X]} (hmo : P.monic)
(hP : P.splits (ring_hom.id K)) : P.next_coeff = - P.roots.sum :=
begin
nth_rewrite 0 [eq_prod_roots_of_monic_of_splits_id hmo hP],
rw [monic.next_coeff_multiset_prod _ _ (λ a ha, _)],
{ simp_rw [next_coeff_X_sub_C, multiset.sum_map_neg'] },
{ exact monic_X_sub_C a }
end
end splits
end polynomial
|
e41f70c8e66cc2a6e47ec73366936306b15b21df | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/topology/instances/polynomial.lean | bf08c967c0063e62072c15ee8481d048a67fb2de | [
"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 | 2,079 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
Analytic facts about polynomials.
-/
import topology.algebra.ring data.polynomial data.real.cau_seq
open polynomial is_absolute_value
lemma polynomial.tendsto_infinity {α β : Type*} [comm_ring α] [discrete_linear_ordered_field β]
(abv : α → β) [is_absolute_value abv] [decidable_eq α] {p : polynomial α} (h : 0 < degree p) :
∀ x : β, ∃ r > 0, ∀ z : α, r < abv z → x < abv (p.eval z) :=
degree_pos_induction_on p h
(λ a ha x, ⟨max (x / abv a) 1, (lt_max_iff.2 (or.inr zero_lt_one)), λ z hz,
by simp [max_lt_iff, div_lt_iff' ((abv_pos abv).2 ha), abv_mul abv] at *; tauto⟩)
(λ p hp ih x, let ⟨r, hr0, hr⟩ := ih x in
⟨max r 1, lt_max_iff.2 (or.inr zero_lt_one), λ z hz, by rw [eval_mul, eval_X, abv_mul abv];
calc x < abv (p.eval z) : hr _ (max_lt_iff.1 hz).1
... ≤ abv (eval z p) * abv z : le_mul_of_ge_one_right
(abv_nonneg _ _) (max_le_iff.1 (le_of_lt hz)).2⟩)
(λ p a hp ih x, let ⟨r, hr0, hr⟩ := ih (x + abv a) in
⟨r, hr0, λ z hz, by rw [eval_add, eval_C, ← sub_neg_eq_add];
exact lt_of_lt_of_le (lt_sub_iff_add_lt.2
(by rw abv_neg abv; exact (hr z hz)))
(le_trans (le_abs_self _) (abs_abv_sub_le_abv_sub _ _ _))⟩)
lemma polynomial.continuous_eval {α} [comm_semiring α] [decidable_eq α] [topological_space α]
[topological_semiring α] (p : polynomial α) : continuous (λ x, p.eval x) :=
begin
apply p.induction,
{ convert continuous_const,
ext, show polynomial.eval x 0 = 0, from rfl },
{ intros a b f haf hb hcts,
simp only [polynomial.eval_add],
refine continuous_add _ hcts,
have : ∀ x, finsupp.sum (finsupp.single a b) (λ (e : ℕ) (a : α), a * x ^ e) = b * x ^a,
from λ x, finsupp.sum_single_index (by simp),
convert continuous_mul _ _,
{ ext, apply this },
{ apply_instance },
{ apply continuous_const },
{ apply continuous_pow }}
end
|
607d4c635324684ea818a46161ca390c5d54c9db | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/topology/metric_space/metrizable_uniformity.lean | f993f298846bf0f077c11f2e423e60000e4bfdd4 | [
"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 | 14,394 | lean | /-
Copyright (c) 2022 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import topology.metric_space.metrizable
/-!
# Metrizable uniform spaces
In this file we prove that a uniform space with countably generated uniformity filter is
pseudometrizable: there exists a `pseudo_metric_space` structure that generates the same uniformity.
The proof follows [Sergey Melikhov, Metrizable uniform spaces][melikhov2011].
## Main definitions
* `pseudo_metric_space.of_prenndist`: given a function `d : X → X → ℝ≥0` such that `d x x = 0` and
`d x y = d y x` for all `x y : X`, constructs the maximal pseudo metric space structure such that
`nndist x y ≤ d x y` for all `x y : X`.
* `uniform_space.pseudo_metric_space`: given a uniform space `X` with countably generated `𝓤 X`,
constructs a `pseudo_metric_space X` instance that is compatible with the uniform space structure.
* `uniform_space.metric_space`: given a T₀ uniform space `X` with countably generated `𝓤 X`,
constructs a `metric_space X` instance that is compatible with the uniform space structure.
## Main statements
* `uniform_space.metrizable_uniformity`: if `X` is a uniform space with countably generated `𝓤 X`,
then there exists a `pseudo_metric_space` structure that is compatible with this `uniform_space`
structure. Use `uniform_space.pseudo_metric_space` or `uniform_space.metric_space` instead.
* `uniform_space.pseudo_metrizable_space`: a uniform space with countably generated `𝓤 X` is pseudo
metrizable.
* `uniform_space.metrizable_space`: a T₀ uniform space with countably generated `𝓤 X` is
metrizable. This is not an instance to avoid loops.
## Tags
metrizable space, uniform space
-/
open set function metric list filter
open_locale nnreal filter uniformity
variables {X : Type*}
namespace pseudo_metric_space
/-- The maximal pseudo metric space structure on `X` such that `dist x y ≤ d x y` for all `x y`,
where `d : X → X → ℝ≥0` is a function such that `d x x = 0` and `d x y = d y x` for all `x`, `y`. -/
noncomputable def of_prenndist (d : X → X → ℝ≥0) (dist_self : ∀ x, d x x = 0)
(dist_comm : ∀ x y, d x y = d y x) :
pseudo_metric_space X :=
{ dist := λ x y, ↑(⨅ l : list X, ((x :: l).zip_with d (l ++ [y])).sum : ℝ≥0),
dist_self := λ x, (nnreal.coe_eq_zero _).2 $ nonpos_iff_eq_zero.1 $
(cinfi_le (order_bot.bdd_below _) []).trans_eq $ by simp [dist_self],
dist_comm := λ x y, nnreal.coe_eq.2 $
begin
refine reverse_surjective.infi_congr _ (λ l, _),
rw [← sum_reverse, zip_with_distrib_reverse, reverse_append, reverse_reverse,
reverse_singleton, singleton_append, reverse_cons, reverse_reverse,
zip_with_comm _ dist_comm],
simp only [length, length_append]
end,
dist_triangle := λ x y z,
begin
rw [← nnreal.coe_add, nnreal.coe_le_coe],
refine nnreal.le_infi_add_infi (λ lxy lyz, _),
calc (⨅ l, (zip_with d (x :: l) (l ++ [z])).sum) ≤
(zip_with d (x :: (lxy ++ y :: lyz)) ((lxy ++ y :: lyz) ++ [z])).sum :
cinfi_le (order_bot.bdd_below _) (lxy ++ y :: lyz)
... = (zip_with d (x :: lxy) (lxy ++ [y])).sum + (zip_with d (y :: lyz) (lyz ++ [z])).sum : _,
rw [← sum_append, ← zip_with_append, cons_append, ← @singleton_append _ y, append_assoc,
append_assoc, append_assoc],
rw [length_cons, length_append, length_singleton]
end }
lemma dist_of_prenndist (d : X → X → ℝ≥0) (dist_self : ∀ x, d x x = 0)
(dist_comm : ∀ x y, d x y = d y x) (x y : X) :
@dist X (@pseudo_metric_space.to_has_dist X
(pseudo_metric_space.of_prenndist d dist_self dist_comm)) x y =
↑(⨅ l : list X, ((x :: l).zip_with d (l ++ [y])).sum : ℝ≥0) := rfl
lemma dist_of_prenndist_le (d : X → X → ℝ≥0) (dist_self : ∀ x, d x x = 0)
(dist_comm : ∀ x y, d x y = d y x) (x y : X) :
@dist X (@pseudo_metric_space.to_has_dist X
(pseudo_metric_space.of_prenndist d dist_self dist_comm)) x y ≤ d x y :=
nnreal.coe_le_coe.2 $ (cinfi_le (order_bot.bdd_below _) []).trans_eq $ by simp
/-- Consider a function `d : X → X → ℝ≥0` such that `d x x = 0` and `d x y = d y x` for all `x`,
`y`. Let `dist` be the largest pseudometric distance such that `dist x y ≤ d x y`, see
`pseudo_metric_space.of_prenndist`. Suppose that `d` satisfies the following triangle-like
inequality: `d x₁ x₄ ≤ 2 * max (d x₁ x₂, d x₂ x₃, d x₃ x₄)`. Then `d x y ≤ 2 * dist x y` for all
`x`, `y`. -/
lemma le_two_mul_dist_of_prenndist (d : X → X → ℝ≥0) (dist_self : ∀ x, d x x = 0)
(dist_comm : ∀ x y, d x y = d y x)
(hd : ∀ x₁ x₂ x₃ x₄, d x₁ x₄ ≤ 2 * max (d x₁ x₂) (max (d x₂ x₃) (d x₃ x₄))) (x y : X) :
↑(d x y) ≤ 2 * @dist X (@pseudo_metric_space.to_has_dist X
(pseudo_metric_space.of_prenndist d dist_self dist_comm)) x y :=
begin
/- We need to show that `d x y` is at most twice the sum `L` of `d xᵢ xᵢ₊₁` over a path
`x₀=x, ..., xₙ=y`. We prove it by induction on the length `n` of the sequence. Find an edge that
splits the path into two parts of almost equal length: both `d x₀ x₁ + ... + d xₖ₋₁ xₖ` and
`d xₖ₊₁ xₖ₊₂ + ... + d xₙ₋₁ xₙ` are less than or equal to `L / 2`.
Then `d x₀ xₖ ≤ L`, `d xₖ xₖ₊₁ ≤ L`, and `d xₖ₊₁ xₙ ≤ L`, thus `d x₀ xₙ ≤ 2 * L`. -/
rw [dist_of_prenndist, ← nnreal.coe_two, ← nnreal.coe_mul, nnreal.mul_infi, nnreal.coe_le_coe],
refine le_cinfi (λ l, _),
have hd₀_trans : transitive (λ x y, d x y = 0),
{ intros a b c hab hbc,
rw ← nonpos_iff_eq_zero,
simpa only [*, max_eq_right, mul_zero] using hd a b c c },
haveI : is_trans X (λ x y, d x y = 0) := ⟨hd₀_trans⟩,
induction hn : length l using nat.strong_induction_on with n ihn generalizing x y l,
simp only at ihn, subst n,
set L := zip_with d (x :: l) (l ++ [y]),
have hL_len : length L = length l + 1, by simp,
cases eq_or_ne (d x y) 0 with hd₀ hd₀, { simp only [hd₀, zero_le] },
rsuffices ⟨z, z', hxz, hzz', hz'y⟩ : ∃ z z' : X, d x z ≤ L.sum ∧ d z z' ≤ L.sum ∧ d z' y ≤ L.sum,
{ exact (hd x z z' y).trans (mul_le_mul_left' (max_le hxz (max_le hzz' hz'y)) _) },
set s : set ℕ := {m : ℕ | 2 * (take m L).sum ≤ L.sum},
have hs₀ : 0 ∈ s, by simp [s],
have hsne : s.nonempty, from ⟨0, hs₀⟩,
obtain ⟨M, hMl, hMs⟩ : ∃ M ≤ length l, is_greatest s M,
{ have hs_ub : length l ∈ upper_bounds s,
{ intros m hm,
rw [← not_lt, nat.lt_iff_add_one_le, ← hL_len],
intro hLm,
rw [mem_set_of_eq, take_all_of_le hLm, two_mul, add_le_iff_nonpos_left, nonpos_iff_eq_zero,
sum_eq_zero_iff, ← all₂_iff_forall, all₂_zip_with, ← chain_append_singleton_iff_forall₂]
at hm; [skip, by simp],
exact hd₀ (hm.rel (mem_append.2 $ or.inr $ mem_singleton_self _)) },
have hs_bdd : bdd_above s, from ⟨length l, hs_ub⟩,
exact ⟨Sup s, cSup_le hsne hs_ub, ⟨nat.Sup_mem hsne hs_bdd, λ k, le_cSup hs_bdd⟩⟩ },
have hM_lt : M < length L, by rwa [hL_len, nat.lt_succ_iff],
have hM_ltx : M < length (x :: l), from lt_length_left_of_zip_with hM_lt,
have hM_lty : M < length (l ++ [y]), from lt_length_right_of_zip_with hM_lt,
refine ⟨(x :: l).nth_le M hM_ltx, (l ++ [y]).nth_le M hM_lty, _, _, _⟩,
{ cases M, { simp [dist_self] },
rw nat.succ_le_iff at hMl,
have hMl' : length (take M l) = M, from (length_take _ _).trans (min_eq_left hMl.le),
simp only [nth_le],
refine (ihn _ hMl _ _ _ hMl').trans _,
convert hMs.1.out,
rw [zip_with_distrib_take, take, take_succ, nth_append hMl, nth_le_nth hMl,
← option.coe_def, option.to_list_some, take_append_of_le_length hMl.le],
refl },
{ refine single_le_sum (λ x hx, zero_le x) _ (mem_iff_nth_le.2 ⟨M, hM_lt, _⟩),
apply nth_le_zip_with },
{ rcases hMl.eq_or_lt with rfl|hMl,
{ simp only [nth_le_append_right le_rfl, sub_self, nth_le_singleton, dist_self, zero_le] },
rw [nth_le_append _ hMl],
have hlen : length (drop (M + 1) l) = length l - (M + 1), from length_drop _ _,
have hlen_lt : length l - (M + 1) < length l, from nat.sub_lt_of_pos_le _ _ M.succ_pos hMl,
refine (ihn _ hlen_lt _ y _ hlen).trans _,
rw [cons_nth_le_drop_succ],
have hMs' : L.sum ≤ 2 * (L.take (M + 1)).sum,
from not_lt.1 (λ h, (hMs.2 h.le).not_lt M.lt_succ_self),
rw [← sum_take_add_sum_drop L (M + 1), two_mul, add_le_add_iff_left,
← add_le_add_iff_right, sum_take_add_sum_drop, ← two_mul] at hMs',
convert hMs',
rwa [zip_with_distrib_drop, drop, drop_append_of_le_length] }
end
end pseudo_metric_space
/-- If `X` is a uniform space with countably generated uniformity filter, there exists a
`pseudo_metric_space` structure compatible with the `uniform_space` structure. Use
`uniform_space.pseudo_metric_space` or `uniform_space.metric_space` instead. -/
protected lemma uniform_space.metrizable_uniformity (X : Type*) [uniform_space X]
[is_countably_generated (𝓤 X)] :
∃ I : pseudo_metric_space X, I.to_uniform_space = ‹_› :=
begin
/- Choose a fast decreasing antitone basis `U : ℕ → set (X × X)` of the uniformity filter `𝓤 X`.
Define `d x y : ℝ≥0` to be `(1 / 2) ^ n`, where `n` is the minimal index of `U n` that separates
`x` and `y`: `(x, y) ∉ U n`, or `0` if `x` is not separated from `y`. This function satisfies the
assumptions of `pseudo_metric_space.of_prenndist` and
`pseudo_metric_space.le_two_mul_dist_of_prenndist`, hence the distance given by the former pseudo
metric space structure is Lipschitz equivalent to the `d`. Thus the uniformities generated by
`d` and `dist` are equal. Since the former uniformity is equal to `𝓤 X`, the latter is equal to
`𝓤 X` as well. -/
classical,
obtain ⟨U, hU_symm, hU_comp, hB⟩ : ∃ U : ℕ → set (X × X), (∀ n, symmetric_rel (U n)) ∧
(∀ ⦃m n⦄, m < n → U n ○ (U n ○ U n) ⊆ U m) ∧ (𝓤 X).has_antitone_basis U,
{ rcases uniform_space.has_seq_basis X with ⟨V, hB, hV_symm⟩,
rcases hB.subbasis_with_rel (λ m, hB.tendsto_small_sets.eventually
(eventually_uniformity_iterate_comp_subset (hB.mem m) 2)) with ⟨φ, hφ_mono, hφ_comp, hφB⟩,
exact ⟨V ∘ φ, λ n, hV_symm _, hφ_comp, hφB⟩ },
letI := uniform_space.separation_setoid X,
set d : X → X → ℝ≥0 := λ x y, if h : ∃ n, (x, y) ∉ U n then (1 / 2) ^ nat.find h else 0,
have hd₀ : ∀ {x y}, d x y = 0 ↔ x ≈ y,
{ intros x y, dsimp only [d],
refine iff.trans _ hB.to_has_basis.mem_separation_rel.symm,
simp only [true_implies_iff],
split_ifs with h,
{ rw [← not_forall] at h, simp [h, pow_eq_zero_iff'] },
{ simpa only [not_exists, not_not, eq_self_iff_true, true_iff] using h } },
have hd_symm : ∀ x y, d x y = d y x,
{ intros x y, dsimp only [d],
simp only [@symmetric_rel.mk_mem_comm _ _ (hU_symm _) x y] },
have hr : (1 / 2 : ℝ≥0) ∈ Ioo (0 : ℝ≥0) 1,
from ⟨nnreal.half_pos one_pos, nnreal.half_lt_self one_ne_zero⟩,
letI I := pseudo_metric_space.of_prenndist d (λ x, hd₀.2 (setoid.refl _)) hd_symm,
have hdist_le : ∀ x y, dist x y ≤ d x y,
from pseudo_metric_space.dist_of_prenndist_le _ _ _,
have hle_d : ∀ {x y : X} {n : ℕ}, (1 / 2) ^ n ≤ d x y ↔ (x, y) ∉ U n,
{ intros x y n,
simp only [d], split_ifs with h,
{ rw [(strict_anti_pow hr.1 hr.2).le_iff_le, nat.find_le_iff],
exact ⟨λ ⟨m, hmn, hm⟩ hn, hm (hB.antitone hmn hn), λ h, ⟨n, le_rfl, h⟩⟩ },
{ push_neg at h,
simp only [h, not_true, (pow_pos hr.1 _).not_le] } },
have hd_le : ∀ x y, ↑(d x y) ≤ 2 * dist x y,
{ refine pseudo_metric_space.le_two_mul_dist_of_prenndist _ _ _ (λ x₁ x₂ x₃ x₄, _),
by_cases H : ∃ n, (x₁, x₄) ∉ U n,
{ refine (dif_pos H).trans_le _,
rw [← nnreal.div_le_iff' two_ne_zero, ← mul_one_div (_ ^ _), ← pow_succ'],
simp only [le_max_iff, hle_d, ← not_and_distrib],
rintro ⟨h₁₂, h₂₃, h₃₄⟩,
refine nat.find_spec H (hU_comp (lt_add_one $ nat.find H) _),
exact ⟨x₂, h₁₂, x₃, h₂₃, h₃₄⟩ },
{ exact (dif_neg H).trans_le (zero_le _) } },
refine ⟨I, uniform_space_eq $ (uniformity_basis_dist_pow hr.1 hr.2).ext hB.to_has_basis _ _⟩,
{ refine λ n hn, ⟨n, hn, λ x hx, (hdist_le _ _).trans_lt _⟩,
rwa [← nnreal.coe_pow, nnreal.coe_lt_coe, ← not_le, hle_d, not_not, prod.mk.eta] },
{ refine λ n hn, ⟨n + 1, trivial, λ x hx, _⟩,
rw [mem_set_of_eq] at hx,
contrapose! hx,
refine le_trans _ ((div_le_iff' (@two_pos ℝ _ _)).2 (hd_le x.1 x.2)),
rwa [← nnreal.coe_two, ← nnreal.coe_div, ← nnreal.coe_pow, nnreal.coe_le_coe, pow_succ',
mul_one_div, nnreal.div_le_iff two_ne_zero, div_mul_cancel _ (@two_ne_zero ℝ≥0 _ _),
hle_d, prod.mk.eta] }
end
/-- A `pseudo_metric_space` instance compatible with a given `uniform_space` structure. -/
protected noncomputable def uniform_space.pseudo_metric_space (X : Type*) [uniform_space X]
[is_countably_generated (𝓤 X)] : pseudo_metric_space X :=
(uniform_space.metrizable_uniformity X).some.replace_uniformity $
congr_arg _ (uniform_space.metrizable_uniformity X).some_spec.symm
/-- A `metric_space` instance compatible with a given `uniform_space` structure. -/
protected noncomputable def uniform_space.metric_space (X : Type*) [uniform_space X]
[is_countably_generated (𝓤 X)] [t0_space X] : metric_space X :=
@of_t0_pseudo_metric_space X (uniform_space.pseudo_metric_space X) _
/-- A uniform space with countably generated `𝓤 X` is pseudo metrizable. -/
@[priority 100]
instance uniform_space.pseudo_metrizable_space [uniform_space X] [is_countably_generated (𝓤 X)] :
topological_space.pseudo_metrizable_space X :=
by { letI := uniform_space.pseudo_metric_space X, apply_instance }
/-- A T₀ uniform space with countably generated `𝓤 X` is metrizable. This is not an instance to
avoid loops. -/
lemma uniform_space.metrizable_space [uniform_space X] [is_countably_generated (𝓤 X)] [t0_space X] :
topological_space.metrizable_space X :=
by { letI := uniform_space.metric_space X, apply_instance }
|
35c9d0344d3bb114f4f268ebea960aff92d279a7 | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/data/list/basic.lean | 54edaa549f5f1498d6953b532edbc6d7e17a987d | [
"Apache-2.0"
] | permissive | jumpy4/mathlib | d3829e75173012833e9f15ac16e481e17596de0f | af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13 | refs/heads/master | 1,693,508,842,818 | 1,636,203,271,000 | 1,636,203,271,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 189,556 | lean | /-
Copyright (c) 2014 Parikshit Khanna. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro
-/
import control.monad.basic
import data.nat.basic
/-!
# Basic properties of lists
-/
open function nat
namespace list
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x}
attribute [inline] list.head
instance : is_left_id (list α) has_append.append [] :=
⟨ nil_append ⟩
instance : is_right_id (list α) has_append.append [] :=
⟨ append_nil ⟩
instance : is_associative (list α) has_append.append :=
⟨ append_assoc ⟩
theorem cons_ne_nil (a : α) (l : list α) : a::l ≠ [].
theorem cons_ne_self (a : α) (l : list α) : a::l ≠ l :=
mt (congr_arg length) (nat.succ_ne_self _)
theorem head_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} :
(h₁::t₁) = (h₂::t₂) → h₁ = h₂ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq)
theorem tail_eq_of_cons_eq {h₁ h₂ : α} {t₁ t₂ : list α} :
(h₁::t₁) = (h₂::t₂) → t₁ = t₂ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq)
@[simp] theorem cons_injective {a : α} : injective (cons a) :=
assume l₁ l₂, assume Pe, tail_eq_of_cons_eq Pe
theorem cons_inj (a : α) {l l' : list α} : a::l = a::l' ↔ l = l' :=
cons_injective.eq_iff
theorem exists_cons_of_ne_nil {l : list α} (h : l ≠ nil) : ∃ b L, l = b :: L :=
by { induction l with c l', contradiction, use [c,l'], }
/-! ### mem -/
theorem mem_singleton_self (a : α) : a ∈ [a] := mem_cons_self _ _
theorem eq_of_mem_singleton {a b : α} : a ∈ [b] → a = b :=
assume : a ∈ [b], or.elim (eq_or_mem_of_mem_cons this)
(assume : a = b, this)
(assume : a ∈ [], absurd this (not_mem_nil a))
@[simp] theorem mem_singleton {a b : α} : a ∈ [b] ↔ a = b :=
⟨eq_of_mem_singleton, or.inl⟩
theorem mem_of_mem_cons_of_mem {a b : α} {l : list α} : a ∈ b::l → b ∈ l → a ∈ l :=
assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl)
(assume : a = b, begin subst a, exact binl end)
(assume : a ∈ l, this)
theorem _root_.decidable.list.eq_or_ne_mem_of_mem [decidable_eq α]
{a b : α} {l : list α} (h : a ∈ b :: l) : a = b ∨ (a ≠ b ∧ a ∈ l) :=
decidable.by_cases or.inl $ assume : a ≠ b, h.elim or.inl $ assume h, or.inr ⟨this, h⟩
theorem eq_or_ne_mem_of_mem {a b : α} {l : list α} : a ∈ b :: l → a = b ∨ (a ≠ b ∧ a ∈ l) :=
by classical; exact decidable.list.eq_or_ne_mem_of_mem
theorem not_mem_append {a : α} {s t : list α} (h₁ : a ∉ s) (h₂ : a ∉ t) : a ∉ s ++ t :=
mt mem_append.1 $ not_or_distrib.2 ⟨h₁, h₂⟩
theorem ne_nil_of_mem {a : α} {l : list α} (h : a ∈ l) : l ≠ [] :=
by intro e; rw e at h; cases h
theorem mem_split {a : α} {l : list α} (h : a ∈ l) : ∃ s t : list α, l = s ++ a :: t :=
begin
induction l with b l ih, {cases h}, rcases h with rfl | h,
{ exact ⟨[], l, rfl⟩ },
{ rcases ih h with ⟨s, t, rfl⟩,
exact ⟨b::s, t, rfl⟩ }
end
theorem mem_of_ne_of_mem {a y : α} {l : list α} (h₁ : a ≠ y) (h₂ : a ∈ y :: l) : a ∈ l :=
or.elim (eq_or_mem_of_mem_cons h₂) (λe, absurd e h₁) (λr, r)
theorem ne_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ≠ b :=
assume nin aeqb, absurd (or.inl aeqb) nin
theorem not_mem_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ∉ l :=
assume nin nainl, absurd (or.inr nainl) nin
theorem not_mem_cons_of_ne_of_not_mem {a y : α} {l : list α} : a ≠ y → a ∉ l → a ∉ y::l :=
assume p1 p2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2))
theorem ne_and_not_mem_of_not_mem_cons {a y : α} {l : list α} : a ∉ y::l → a ≠ y ∧ a ∉ l :=
assume p, and.intro (ne_of_not_mem_cons p) (not_mem_of_not_mem_cons p)
@[simp] theorem mem_map {f : α → β} {b : β} {l : list α} : b ∈ map f l ↔ ∃ a, a ∈ l ∧ f a = b :=
begin
-- This proof uses no axioms, that's why it's longer that `induction`; simp [...]
induction l with a l ihl,
{ split, { rintro ⟨_⟩ }, { rintro ⟨a, ⟨_⟩, _⟩ } },
{ refine (or_congr eq_comm ihl).trans _,
split,
{ rintro (h|⟨c, hcl, h⟩),
exacts [⟨a, or.inl rfl, h⟩, ⟨c, or.inr hcl, h⟩] },
{ rintro ⟨c, (hc|hc), h⟩,
exacts [or.inl $ (congr_arg f hc.symm).trans h, or.inr ⟨c, hc, h⟩] } }
end
alias mem_map ↔ list.exists_of_mem_map _
theorem mem_map_of_mem (f : α → β) {a : α} {l : list α} (h : a ∈ l) : f a ∈ map f l :=
mem_map.2 ⟨a, h, rfl⟩
theorem mem_map_of_injective {f : α → β} (H : injective f) {a : α} {l : list α} :
f a ∈ map f l ↔ a ∈ l :=
⟨λ m, let ⟨a', m', e⟩ := exists_of_mem_map m in H e ▸ m', mem_map_of_mem _⟩
lemma forall_mem_map_iff {f : α → β} {l : list α} {P : β → Prop} :
(∀ i ∈ l.map f, P i) ↔ ∀ j ∈ l, P (f j) :=
begin
split,
{ assume H j hj,
exact H (f j) (mem_map_of_mem f hj) },
{ assume H i hi,
rcases mem_map.1 hi with ⟨j, hj, ji⟩,
rw ← ji,
exact H j hj }
end
@[simp] lemma map_eq_nil {f : α → β} {l : list α} : list.map f l = [] ↔ l = [] :=
⟨by cases l; simp only [forall_prop_of_true, map, forall_prop_of_false, not_false_iff],
λ h, h.symm ▸ rfl⟩
@[simp] theorem mem_join {a : α} : ∀ {L : list (list α)}, a ∈ join L ↔ ∃ l, l ∈ L ∧ a ∈ l
| [] := ⟨false.elim, λ⟨_, h, _⟩, false.elim h⟩
| (c :: L) := by simp only [join, mem_append, @mem_join L, mem_cons_iff, or_and_distrib_right,
exists_or_distrib, exists_eq_left]
theorem exists_of_mem_join {a : α} {L : list (list α)} : a ∈ join L → ∃ l, l ∈ L ∧ a ∈ l :=
mem_join.1
theorem mem_join_of_mem {a : α} {L : list (list α)} {l} (lL : l ∈ L) (al : a ∈ l) : a ∈ join L :=
mem_join.2 ⟨l, lL, al⟩
@[simp]
theorem mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ list.bind l f ↔ ∃ a ∈ l, b ∈ f a :=
iff.trans mem_join
⟨λ ⟨l', h1, h2⟩, let ⟨a, al, fa⟩ := exists_of_mem_map h1 in ⟨a, al, fa.symm ▸ h2⟩,
λ ⟨a, al, bfa⟩, ⟨f a, mem_map_of_mem _ al, bfa⟩⟩
theorem exists_of_mem_bind {b : β} {l : list α} {f : α → list β} :
b ∈ list.bind l f → ∃ a ∈ l, b ∈ f a :=
mem_bind.1
theorem mem_bind_of_mem {b : β} {l : list α} {f : α → list β} {a} (al : a ∈ l) (h : b ∈ f a) :
b ∈ list.bind l f :=
mem_bind.2 ⟨a, al, h⟩
lemma bind_map {g : α → list β} {f : β → γ} :
∀(l : list α), list.map f (l.bind g) = l.bind (λa, (g a).map f)
| [] := rfl
| (a::l) := by simp only [cons_bind, map_append, bind_map l]
lemma map_bind (g : β → list γ) (f : α → β) :
∀ l : list α, (list.map f l).bind g = l.bind (λ a, g (f a))
| [] := rfl
| (a::l) := by simp only [cons_bind, map_cons, map_bind l]
/-- If each element of a list can be lifted to some type, then the whole list can be lifted to this
type. -/
instance [h : can_lift α β] : can_lift (list α) (list β) :=
{ coe := list.map h.coe,
cond := λ l, ∀ x ∈ l, can_lift.cond β x,
prf := λ l H,
begin
induction l with a l ihl, { exact ⟨[], rfl⟩ },
rcases ihl (λ x hx, H x (or.inr hx)) with ⟨l, rfl⟩,
rcases can_lift.prf a (H a (or.inl rfl)) with ⟨a, rfl⟩,
exact ⟨a :: l, rfl⟩
end}
/-! ### length -/
theorem length_eq_zero {l : list α} : length l = 0 ↔ l = [] :=
⟨eq_nil_of_length_eq_zero, λ h, h.symm ▸ rfl⟩
@[simp] lemma length_singleton (a : α) : length [a] = 1 := rfl
theorem length_pos_of_mem {a : α} : ∀ {l : list α}, a ∈ l → 0 < length l
| (b::l) _ := zero_lt_succ _
theorem exists_mem_of_length_pos : ∀ {l : list α}, 0 < length l → ∃ a, a ∈ l
| (b::l) _ := ⟨b, mem_cons_self _ _⟩
theorem length_pos_iff_exists_mem {l : list α} : 0 < length l ↔ ∃ a, a ∈ l :=
⟨exists_mem_of_length_pos, λ ⟨a, h⟩, length_pos_of_mem h⟩
theorem ne_nil_of_length_pos {l : list α} : 0 < length l → l ≠ [] :=
λ h1 h2, lt_irrefl 0 ((length_eq_zero.2 h2).subst h1)
theorem length_pos_of_ne_nil {l : list α} : l ≠ [] → 0 < length l :=
λ h, pos_iff_ne_zero.2 $ λ h0, h $ length_eq_zero.1 h0
theorem length_pos_iff_ne_nil {l : list α} : 0 < length l ↔ l ≠ [] :=
⟨ne_nil_of_length_pos, length_pos_of_ne_nil⟩
lemma exists_mem_of_ne_nil (l : list α) (h : l ≠ []) : ∃ x, x ∈ l :=
exists_mem_of_length_pos (length_pos_of_ne_nil h)
theorem length_eq_one {l : list α} : length l = 1 ↔ ∃ a, l = [a] :=
⟨match l with [a], _ := ⟨a, rfl⟩ end, λ ⟨a, e⟩, e.symm ▸ rfl⟩
lemma exists_of_length_succ {n} :
∀ l : list α, l.length = n + 1 → ∃ h t, l = h :: t
| [] H := absurd H.symm $ succ_ne_zero n
| (h :: t) H := ⟨h, t, rfl⟩
@[simp] lemma length_injective_iff : injective (list.length : list α → ℕ) ↔ subsingleton α :=
begin
split,
{ intro h, refine ⟨λ x y, _⟩, suffices : [x] = [y], { simpa using this }, apply h, refl },
{ intros hα l1 l2 hl, induction l1 generalizing l2; cases l2,
{ refl }, { cases hl }, { cases hl },
congr, exactI subsingleton.elim _ _, apply l1_ih, simpa using hl }
end
@[simp] lemma length_injective [subsingleton α] : injective (length : list α → ℕ) :=
length_injective_iff.mpr $ by apply_instance
/-! ### set-theoretic notation of lists -/
lemma empty_eq : (∅ : list α) = [] := by refl
lemma singleton_eq (x : α) : ({x} : list α) = [x] := rfl
lemma insert_neg [decidable_eq α] {x : α} {l : list α} (h : x ∉ l) :
has_insert.insert x l = x :: l :=
if_neg h
lemma insert_pos [decidable_eq α] {x : α} {l : list α} (h : x ∈ l) :
has_insert.insert x l = l :=
if_pos h
lemma doubleton_eq [decidable_eq α] {x y : α} (h : x ≠ y) : ({x, y} : list α) = [x, y] :=
by { rw [insert_neg, singleton_eq], rwa [singleton_eq, mem_singleton] }
/-! ### bounded quantifiers over lists -/
theorem forall_mem_nil (p : α → Prop) : ∀ x ∈ @nil α, p x.
theorem forall_mem_cons : ∀ {p : α → Prop} {a : α} {l : list α},
(∀ x ∈ a :: l, p x) ↔ p a ∧ ∀ x ∈ l, p x :=
ball_cons
theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : list α}
(h : ∀ x ∈ a :: l, p x) :
∀ x ∈ l, p x :=
(forall_mem_cons.1 h).2
theorem forall_mem_singleton {p : α → Prop} {a : α} : (∀ x ∈ [a], p x) ↔ p a :=
by simp only [mem_singleton, forall_eq]
theorem forall_mem_append {p : α → Prop} {l₁ l₂ : list α} :
(∀ x ∈ l₁ ++ l₂, p x) ↔ (∀ x ∈ l₁, p x) ∧ (∀ x ∈ l₂, p x) :=
by simp only [mem_append, or_imp_distrib, forall_and_distrib]
theorem not_exists_mem_nil (p : α → Prop) : ¬ ∃ x ∈ @nil α, p x.
theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : list α) (h : p a) :
∃ x ∈ a :: l, p x :=
bex.intro a (mem_cons_self _ _) h
theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ l, p x) :
∃ x ∈ a :: l, p x :=
bex.elim h (λ x xl px, bex.intro x (mem_cons_of_mem _ xl) px)
theorem or_exists_of_exists_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ a :: l, p x) :
p a ∨ ∃ x ∈ l, p x :=
bex.elim h (λ x xal px,
or.elim (eq_or_mem_of_mem_cons xal)
(assume : x = a, begin rw ←this, left, exact px end)
(assume : x ∈ l, or.inr (bex.intro x this px)))
theorem exists_mem_cons_iff (p : α → Prop) (a : α) (l : list α) :
(∃ x ∈ a :: l, p x) ↔ p a ∨ ∃ x ∈ l, p x :=
iff.intro or_exists_of_exists_mem_cons
(assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists)
/-! ### list subset -/
theorem subset_def {l₁ l₂ : list α} : l₁ ⊆ l₂ ↔ ∀ ⦃a : α⦄, a ∈ l₁ → a ∈ l₂ := iff.rfl
theorem subset_append_of_subset_left (l l₁ l₂ : list α) : l ⊆ l₁ → l ⊆ l₁++l₂ :=
λ s, subset.trans s $ subset_append_left _ _
theorem subset_append_of_subset_right (l l₁ l₂ : list α) : l ⊆ l₂ → l ⊆ l₁++l₂ :=
λ s, subset.trans s $ subset_append_right _ _
@[simp] theorem cons_subset {a : α} {l m : list α} :
a::l ⊆ m ↔ a ∈ m ∧ l ⊆ m :=
by simp only [subset_def, mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq]
theorem cons_subset_of_subset_of_mem {a : α} {l m : list α}
(ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m :=
cons_subset.2 ⟨ainm, lsubm⟩
theorem append_subset_of_subset_of_subset {l₁ l₂ l : list α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) :
l₁ ++ l₂ ⊆ l :=
λ a h, (mem_append.1 h).elim (@l₁subl _) (@l₂subl _)
@[simp] theorem append_subset_iff {l₁ l₂ l : list α} :
l₁ ++ l₂ ⊆ l ↔ l₁ ⊆ l ∧ l₂ ⊆ l :=
begin
split,
{ intro h, simp only [subset_def] at *, split; intros; simp* },
{ rintro ⟨h1, h2⟩, apply append_subset_of_subset_of_subset h1 h2 }
end
theorem eq_nil_of_subset_nil : ∀ {l : list α}, l ⊆ [] → l = []
| [] s := rfl
| (a::l) s := false.elim $ s $ mem_cons_self a l
theorem eq_nil_iff_forall_not_mem {l : list α} : l = [] ↔ ∀ a, a ∉ l :=
show l = [] ↔ l ⊆ [], from ⟨λ e, e ▸ subset.refl _, eq_nil_of_subset_nil⟩
theorem map_subset {l₁ l₂ : list α} (f : α → β) (H : l₁ ⊆ l₂) : map f l₁ ⊆ map f l₂ :=
λ x, by simp only [mem_map, not_and, exists_imp_distrib, and_imp]; exact λ a h e, ⟨a, H h, e⟩
theorem map_subset_iff {l₁ l₂ : list α} (f : α → β) (h : injective f) :
map f l₁ ⊆ map f l₂ ↔ l₁ ⊆ l₂ :=
begin
refine ⟨_, map_subset f⟩, intros h2 x hx,
rcases mem_map.1 (h2 (mem_map_of_mem f hx)) with ⟨x', hx', hxx'⟩,
cases h hxx', exact hx'
end
/-! ### append -/
lemma append_eq_has_append {L₁ L₂ : list α} : list.append L₁ L₂ = L₁ ++ L₂ := rfl
@[simp] lemma singleton_append {x : α} {l : list α} : [x] ++ l = x :: l := rfl
theorem append_ne_nil_of_ne_nil_left (s t : list α) : s ≠ [] → s ++ t ≠ [] :=
by induction s; intros; contradiction
theorem append_ne_nil_of_ne_nil_right (s t : list α) : t ≠ [] → s ++ t ≠ [] :=
by induction s; intros; contradiction
@[simp] lemma append_eq_nil {p q : list α} : (p ++ q) = [] ↔ p = [] ∧ q = [] :=
by cases p; simp only [nil_append, cons_append, eq_self_iff_true, true_and, false_and]
@[simp] lemma nil_eq_append_iff {a b : list α} : [] = a ++ b ↔ a = [] ∧ b = [] :=
by rw [eq_comm, append_eq_nil]
lemma append_eq_cons_iff {a b c : list α} {x : α} :
a ++ b = x :: c ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) :=
by cases a; simp only [and_assoc, @eq_comm _ c, nil_append, cons_append, eq_self_iff_true,
true_and, false_and, exists_false, false_or, or_false, exists_and_distrib_left, exists_eq_left']
lemma cons_eq_append_iff {a b c : list α} {x : α} :
(x :: c : list α) = a ++ b ↔ (a = [] ∧ b = x :: c) ∨ (∃a', a = x :: a' ∧ c = a' ++ b) :=
by rw [eq_comm, append_eq_cons_iff]
lemma append_eq_append_iff {a b c d : list α} :
a ++ b = c ++ d ↔ (∃a', c = a ++ a' ∧ b = a' ++ d) ∨ (∃c', a = c ++ c' ∧ d = c' ++ b) :=
begin
induction a generalizing c,
case nil { rw nil_append, split,
{ rintro rfl, left, exact ⟨_, rfl, rfl⟩ },
{ rintro (⟨a', rfl, rfl⟩ | ⟨a', H, rfl⟩), {refl}, {rw [← append_assoc, ← H], refl} } },
case cons : a as ih {
cases c,
{ simp only [cons_append, nil_append, false_and, exists_false, false_or, exists_eq_left'],
exact eq_comm },
{ simp only [cons_append, @eq_comm _ a, ih, and_assoc, and_or_distrib_left,
exists_and_distrib_left] } }
end
@[simp] theorem take_append_drop : ∀ (n : ℕ) (l : list α), take n l ++ drop n l = l
| 0 a := rfl
| (succ n) [] := rfl
| (succ n) (x :: xs) := congr_arg (cons x) $ take_append_drop n xs
-- TODO(Leo): cleanup proof after arith dec proc
theorem append_inj :
∀ {s₁ s₂ t₁ t₂ : list α}, s₁ ++ t₁ = s₂ ++ t₂ → length s₁ = length s₂ → s₁ = s₂ ∧ t₁ = t₂
| [] [] t₁ t₂ h hl := ⟨rfl, h⟩
| (a::s₁) [] t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl
| [] (b::s₂) t₁ t₂ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl.symm
| (a::s₁) (b::s₂) t₁ t₂ h hl := list.no_confusion h $ λab hap,
let ⟨e1, e2⟩ := @append_inj s₁ s₂ t₁ t₂ hap (succ.inj hl) in
by rw [ab, e1, e2]; exact ⟨rfl, rfl⟩
theorem append_inj_right {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length s₁ = length s₂) : t₁ = t₂ :=
(append_inj h hl).right
theorem append_inj_left {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length s₁ = length s₂) : s₁ = s₂ :=
(append_inj h hl).left
theorem append_inj' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂) (hl : length t₁ = length t₂) :
s₁ = s₂ ∧ t₁ = t₂ :=
append_inj h $ @nat.add_right_cancel _ (length t₁) _ $
let hap := congr_arg length h in by simp only [length_append] at hap; rwa [← hl] at hap
theorem append_inj_right' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length t₁ = length t₂) : t₁ = t₂ :=
(append_inj' h hl).right
theorem append_inj_left' {s₁ s₂ t₁ t₂ : list α} (h : s₁ ++ t₁ = s₂ ++ t₂)
(hl : length t₁ = length t₂) : s₁ = s₂ :=
(append_inj' h hl).left
theorem append_left_cancel {s t₁ t₂ : list α} (h : s ++ t₁ = s ++ t₂) : t₁ = t₂ :=
append_inj_right h rfl
theorem append_right_cancel {s₁ s₂ t : list α} (h : s₁ ++ t = s₂ ++ t) : s₁ = s₂ :=
append_inj_left' h rfl
theorem append_right_injective (s : list α) : function.injective (λ t, s ++ t) :=
λ t₁ t₂, append_left_cancel
theorem append_right_inj {t₁ t₂ : list α} (s) : s ++ t₁ = s ++ t₂ ↔ t₁ = t₂ :=
(append_right_injective s).eq_iff
theorem append_left_injective (t : list α) : function.injective (λ s, s ++ t) :=
λ s₁ s₂, append_right_cancel
theorem append_left_inj {s₁ s₂ : list α} (t) : s₁ ++ t = s₂ ++ t ↔ s₁ = s₂ :=
(append_left_injective t).eq_iff
theorem map_eq_append_split {f : α → β} {l : list α} {s₁ s₂ : list β}
(h : map f l = s₁ ++ s₂) : ∃ l₁ l₂, l = l₁ ++ l₂ ∧ map f l₁ = s₁ ∧ map f l₂ = s₂ :=
begin
have := h, rw [← take_append_drop (length s₁) l] at this ⊢,
rw map_append at this,
refine ⟨_, _, rfl, append_inj this _⟩,
rw [length_map, length_take, min_eq_left],
rw [← length_map f l, h, length_append],
apply nat.le_add_right
end
/-! ### repeat -/
@[simp] theorem repeat_succ (a : α) (n) : repeat a (n + 1) = a :: repeat a n := rfl
theorem mem_repeat {a b : α} : ∀ {n}, b ∈ repeat a n ↔ n ≠ 0 ∧ b = a
| 0 := by simp
| (n + 1) := by simp [mem_repeat]
theorem eq_of_mem_repeat {a b : α} {n} (h : b ∈ repeat a n) : b = a :=
(mem_repeat.1 h).2
theorem eq_repeat_of_mem {a : α} : ∀ {l : list α}, (∀ b ∈ l, b = a) → l = repeat a l.length
| [] H := rfl
| (b::l) H := by cases forall_mem_cons.1 H with H₁ H₂;
unfold length repeat; congr; [exact H₁, exact eq_repeat_of_mem H₂]
theorem eq_repeat' {a : α} {l : list α} : l = repeat a l.length ↔ ∀ b ∈ l, b = a :=
⟨λ h, h.symm ▸ λ b, eq_of_mem_repeat, eq_repeat_of_mem⟩
theorem eq_repeat {a : α} {n} {l : list α} : l = repeat a n ↔ length l = n ∧ ∀ b ∈ l, b = a :=
⟨λ h, h.symm ▸ ⟨length_repeat _ _, λ b, eq_of_mem_repeat⟩,
λ ⟨e, al⟩, e ▸ eq_repeat_of_mem al⟩
theorem repeat_add (a : α) (m n) : repeat a (m + n) = repeat a m ++ repeat a n :=
by induction m; simp only [*, zero_add, succ_add, repeat]; split; refl
theorem repeat_subset_singleton (a : α) (n) : repeat a n ⊆ [a] :=
λ b h, mem_singleton.2 (eq_of_mem_repeat h)
@[simp] theorem map_const (l : list α) (b : β) : map (function.const α b) l = repeat b l.length :=
by induction l; [refl, simp only [*, map]]; split; refl
theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) :
b₁ = b₂ :=
by rw map_const at h; exact eq_of_mem_repeat h
@[simp] theorem map_repeat (f : α → β) (a : α) (n) : map f (repeat a n) = repeat (f a) n :=
by induction n; [refl, simp only [*, repeat, map]]; split; refl
@[simp] theorem tail_repeat (a : α) (n) : tail (repeat a n) = repeat a n.pred :=
by cases n; refl
@[simp] theorem join_repeat_nil (n : ℕ) : join (repeat [] n) = @nil α :=
by induction n; [refl, simp only [*, repeat, join, append_nil]]
lemma repeat_left_injective {n : ℕ} (hn : n ≠ 0) :
function.injective (λ a : α, repeat a n) :=
λ a b h, (eq_repeat.1 h).2 _ $ mem_repeat.2 ⟨hn, rfl⟩
lemma repeat_left_inj {a b : α} {n : ℕ} (hn : n ≠ 0) :
repeat a n = repeat b n ↔ a = b :=
(repeat_left_injective hn).eq_iff
@[simp] lemma repeat_left_inj' {a b : α} :
∀ {n}, repeat a n = repeat b n ↔ n = 0 ∨ a = b
| 0 := by simp
| (n + 1) := (repeat_left_inj n.succ_ne_zero).trans $ by simp only [n.succ_ne_zero, false_or]
lemma repeat_right_injective (a : α) : function.injective (repeat a) :=
function.left_inverse.injective (length_repeat a)
@[simp] lemma repeat_right_inj {a : α} {n m : ℕ} :
repeat a n = repeat a m ↔ n = m :=
(repeat_right_injective a).eq_iff
/-! ### pure -/
@[simp] theorem mem_pure {α} (x y : α) :
x ∈ (pure y : list α) ↔ x = y := by simp! [pure,list.ret]
/-! ### bind -/
@[simp] theorem bind_eq_bind {α β} (f : α → list β) (l : list α) :
l >>= f = l.bind f := rfl
-- TODO: duplicate of a lemma in core
theorem bind_append (f : α → list β) (l₁ l₂ : list α) :
(l₁ ++ l₂).bind f = l₁.bind f ++ l₂.bind f :=
append_bind _ _ _
@[simp] theorem bind_singleton (f : α → list β) (x : α) : [x].bind f = f x :=
append_nil (f x)
@[simp] theorem bind_singleton' (l : list α) : l.bind (λ x, [x]) = l := bind_pure l
theorem map_eq_bind {α β} (f : α → β) (l : list α) : map f l = l.bind (λ x, [f x]) :=
by { transitivity, rw [← bind_singleton' l, bind_map], refl }
theorem bind_assoc {α β} (l : list α) (f : α → list β) (g : β → list γ) :
(l.bind f).bind g = l.bind (λ x, (f x).bind g) :=
by induction l; simp *
/-! ### concat -/
theorem concat_nil (a : α) : concat [] a = [a] := rfl
theorem concat_cons (a b : α) (l : list α) : concat (a :: l) b = a :: concat l b := rfl
@[simp] theorem concat_eq_append (a : α) (l : list α) : concat l a = l ++ [a] :=
by induction l; simp only [*, concat]; split; refl
theorem init_eq_of_concat_eq {a : α} {l₁ l₂ : list α} : concat l₁ a = concat l₂ a → l₁ = l₂ :=
begin
intro h,
rw [concat_eq_append, concat_eq_append] at h,
exact append_right_cancel h
end
theorem last_eq_of_concat_eq {a b : α} {l : list α} : concat l a = concat l b → a = b :=
begin
intro h,
rw [concat_eq_append, concat_eq_append] at h,
exact head_eq_of_cons_eq (append_left_cancel h)
end
theorem concat_ne_nil (a : α) (l : list α) : concat l a ≠ [] :=
by simp
theorem concat_append (a : α) (l₁ l₂ : list α) : concat l₁ a ++ l₂ = l₁ ++ a :: l₂ :=
by simp
theorem length_concat (a : α) (l : list α) : length (concat l a) = succ (length l) :=
by simp only [concat_eq_append, length_append, length]
theorem append_concat (a : α) (l₁ l₂ : list α) : l₁ ++ concat l₂ a = concat (l₁ ++ l₂) a :=
by simp
/-! ### reverse -/
@[simp] theorem reverse_nil : reverse (@nil α) = [] := rfl
local attribute [simp] reverse_core
@[simp] theorem reverse_cons (a : α) (l : list α) : reverse (a::l) = reverse l ++ [a] :=
have aux : ∀ l₁ l₂, reverse_core l₁ l₂ ++ [a] = reverse_core l₁ (l₂ ++ [a]),
by intro l₁; induction l₁; intros; [refl, simp only [*, reverse_core, cons_append]],
(aux l nil).symm
theorem reverse_core_eq (l₁ l₂ : list α) : reverse_core l₁ l₂ = reverse l₁ ++ l₂ :=
by induction l₁ generalizing l₂; [refl, simp only [*, reverse_core, reverse_cons, append_assoc]];
refl
theorem reverse_cons' (a : α) (l : list α) : reverse (a::l) = concat (reverse l) a :=
by simp only [reverse_cons, concat_eq_append]
@[simp] theorem reverse_singleton (a : α) : reverse [a] = [a] := rfl
@[simp] theorem reverse_append (s t : list α) : reverse (s ++ t) = (reverse t) ++ (reverse s) :=
by induction s; [rw [nil_append, reverse_nil, append_nil],
simp only [*, cons_append, reverse_cons, append_assoc]]
theorem reverse_concat (l : list α) (a : α) : reverse (concat l a) = a :: reverse l :=
by rw [concat_eq_append, reverse_append, reverse_singleton, singleton_append]
@[simp] theorem reverse_reverse (l : list α) : reverse (reverse l) = l :=
by induction l; [refl, simp only [*, reverse_cons, reverse_append]]; refl
@[simp] theorem reverse_involutive : involutive (@reverse α) :=
λ l, reverse_reverse l
@[simp] theorem reverse_injective : injective (@reverse α) :=
reverse_involutive.injective
@[simp] theorem reverse_inj {l₁ l₂ : list α} : reverse l₁ = reverse l₂ ↔ l₁ = l₂ :=
reverse_injective.eq_iff
lemma reverse_eq_iff {l l' : list α} :
l.reverse = l' ↔ l = l'.reverse :=
reverse_involutive.eq_iff
@[simp] theorem reverse_eq_nil {l : list α} : reverse l = [] ↔ l = [] :=
@reverse_inj _ l []
theorem concat_eq_reverse_cons (a : α) (l : list α) : concat l a = reverse (a :: reverse l) :=
by simp only [concat_eq_append, reverse_cons, reverse_reverse]
@[simp] theorem length_reverse (l : list α) : length (reverse l) = length l :=
by induction l; [refl, simp only [*, reverse_cons, length_append, length]]
@[simp] theorem map_reverse (f : α → β) (l : list α) : map f (reverse l) = reverse (map f l) :=
by induction l; [refl, simp only [*, map, reverse_cons, map_append]]
theorem map_reverse_core (f : α → β) (l₁ l₂ : list α) :
map f (reverse_core l₁ l₂) = reverse_core (map f l₁) (map f l₂) :=
by simp only [reverse_core_eq, map_append, map_reverse]
@[simp] theorem mem_reverse {a : α} {l : list α} : a ∈ reverse l ↔ a ∈ l :=
by induction l; [refl, simp only [*, reverse_cons, mem_append, mem_singleton, mem_cons_iff,
not_mem_nil, false_or, or_false, or_comm]]
@[simp] theorem reverse_repeat (a : α) (n) : reverse (repeat a n) = repeat a n :=
eq_repeat.2 ⟨by simp only [length_reverse, length_repeat],
λ b h, eq_of_mem_repeat (mem_reverse.1 h)⟩
/-! ### empty -/
attribute [simp] list.empty
lemma empty_iff_eq_nil {l : list α} : l.empty ↔ l = [] :=
list.cases_on l (by simp) (by simp)
/-! ### init -/
@[simp] theorem length_init : ∀ (l : list α), length (init l) = length l - 1
| [] := rfl
| [a] := rfl
| (a :: b :: l) :=
begin
rw init,
simp only [add_left_inj, length, succ_add_sub_one],
exact length_init (b :: l)
end
/-! ### last -/
@[simp] theorem last_cons {a : α} {l : list α} :
∀ (h₁ : a :: l ≠ nil) (h₂ : l ≠ nil), last (a :: l) h₁ = last l h₂ :=
by {induction l; intros, contradiction, reflexivity}
@[simp] theorem last_append {a : α} (l : list α) (h : l ++ [a] ≠ []) : last (l ++ [a]) h = a :=
by induction l;
[refl, simp only [cons_append, last_cons _ (λ H, cons_ne_nil _ _ (append_eq_nil.1 H).2), *]]
theorem last_concat {a : α} (l : list α) (h : concat l a ≠ []) : last (concat l a) h = a :=
by simp only [concat_eq_append, last_append]
@[simp] theorem last_singleton (a : α) (h : [a] ≠ []) : last [a] h = a := rfl
@[simp] theorem last_cons_cons (a₁ a₂ : α) (l : list α) (h : a₁::a₂::l ≠ []) :
last (a₁::a₂::l) h = last (a₂::l) (cons_ne_nil a₂ l) := rfl
theorem init_append_last : ∀ {l : list α} (h : l ≠ []), init l ++ [last l h] = l
| [] h := absurd rfl h
| [a] h := rfl
| (a::b::l) h :=
begin
rw [init, cons_append, last_cons (cons_ne_nil _ _) (cons_ne_nil _ _)],
congr,
exact init_append_last (cons_ne_nil b l)
end
theorem last_congr {l₁ l₂ : list α} (h₁ : l₁ ≠ []) (h₂ : l₂ ≠ []) (h₃ : l₁ = l₂) :
last l₁ h₁ = last l₂ h₂ :=
by subst l₁
theorem last_mem : ∀ {l : list α} (h : l ≠ []), last l h ∈ l
| [] h := absurd rfl h
| [a] h := or.inl rfl
| (a::b::l) h := or.inr $ by { rw [last_cons_cons], exact last_mem (cons_ne_nil b l) }
lemma last_repeat_succ (a m : ℕ) :
(repeat a m.succ).last (ne_nil_of_length_eq_succ
(show (repeat a m.succ).length = m.succ, by rw length_repeat)) = a :=
begin
induction m with k IH,
{ simp },
{ simpa only [repeat_succ, last] }
end
/-! ### last' -/
@[simp] theorem last'_is_none :
∀ {l : list α}, (last' l).is_none ↔ l = []
| [] := by simp
| [a] := by simp
| (a::b::l) := by simp [@last'_is_none (b::l)]
@[simp] theorem last'_is_some : ∀ {l : list α}, l.last'.is_some ↔ l ≠ []
| [] := by simp
| [a] := by simp
| (a::b::l) := by simp [@last'_is_some (b::l)]
theorem mem_last'_eq_last : ∀ {l : list α} {x : α}, x ∈ l.last' → ∃ h, x = last l h
| [] x hx := false.elim $ by simpa using hx
| [a] x hx := have a = x, by simpa using hx, this ▸ ⟨cons_ne_nil a [], rfl⟩
| (a::b::l) x hx :=
begin
rw last' at hx,
rcases mem_last'_eq_last hx with ⟨h₁, h₂⟩,
use cons_ne_nil _ _,
rwa [last_cons]
end
theorem last'_eq_last_of_ne_nil : ∀ {l : list α} (h : l ≠ []), l.last' = some (l.last h)
| [] h := (h rfl).elim
| [a] _ := by {unfold last, unfold last'}
| (a::b::l) _ := @last'_eq_last_of_ne_nil (b::l) (cons_ne_nil _ _)
theorem mem_last'_cons {x y : α} : ∀ {l : list α} (h : x ∈ l.last'), x ∈ (y :: l).last'
| [] _ := by contradiction
| (a::l) h := h
theorem mem_of_mem_last' {l : list α} {a : α} (ha : a ∈ l.last') : a ∈ l :=
let ⟨h₁, h₂⟩ := mem_last'_eq_last ha in h₂.symm ▸ last_mem _
theorem init_append_last' : ∀ {l : list α} (a ∈ l.last'), init l ++ [a] = l
| [] a ha := (option.not_mem_none a ha).elim
| [a] _ rfl := rfl
| (a :: b :: l) c hc := by { rw [last'] at hc, rw [init, cons_append, init_append_last' _ hc] }
theorem ilast_eq_last' [inhabited α] : ∀ l : list α, l.ilast = l.last'.iget
| [] := by simp [ilast, arbitrary]
| [a] := rfl
| [a, b] := rfl
| [a, b, c] := rfl
| (a :: b :: c :: l) := by simp [ilast, ilast_eq_last' (c :: l)]
@[simp] theorem last'_append_cons : ∀ (l₁ : list α) (a : α) (l₂ : list α),
last' (l₁ ++ a :: l₂) = last' (a :: l₂)
| [] a l₂ := rfl
| [b] a l₂ := rfl
| (b::c::l₁) a l₂ := by rw [cons_append, cons_append, last', ← cons_append, last'_append_cons]
@[simp] theorem last'_cons_cons (x y : α) (l : list α) :
last' (x :: y :: l) = last' (y :: l) := rfl
theorem last'_append_of_ne_nil (l₁ : list α) : ∀ {l₂ : list α} (hl₂ : l₂ ≠ []),
last' (l₁ ++ l₂) = last' l₂
| [] hl₂ := by contradiction
| (b::l₂) _ := last'_append_cons l₁ b l₂
/-! ### head(') and tail -/
theorem head_eq_head' [inhabited α] (l : list α) : head l = (head' l).iget :=
by cases l; refl
theorem mem_of_mem_head' {x : α} : ∀ {l : list α}, x ∈ l.head' → x ∈ l
| [] h := (option.not_mem_none _ h).elim
| (a::l) h := by { simp only [head', option.mem_def] at h, exact h ▸ or.inl rfl }
@[simp] theorem head_cons [inhabited α] (a : α) (l : list α) : head (a::l) = a := rfl
@[simp] theorem tail_nil : tail (@nil α) = [] := rfl
@[simp] theorem tail_cons (a : α) (l : list α) : tail (a::l) = l := rfl
@[simp] theorem head_append [inhabited α] (t : list α) {s : list α} (h : s ≠ []) :
head (s ++ t) = head s :=
by {induction s, contradiction, refl}
theorem head'_append {s t : list α} {x : α} (h : x ∈ s.head') :
x ∈ (s ++ t).head' :=
by { cases s, contradiction, exact h }
theorem tail_append_singleton_of_ne_nil {a : α} {l : list α} (h : l ≠ nil) :
tail (l ++ [a]) = tail l ++ [a] :=
by { induction l, contradiction, rw [tail,cons_append,tail], }
theorem cons_head'_tail : ∀ {l : list α} {a : α} (h : a ∈ head' l), a :: tail l = l
| [] a h := by contradiction
| (b::l) a h := by { simp at h, simp [h] }
theorem head_mem_head' [inhabited α] : ∀ {l : list α} (h : l ≠ []), head l ∈ head' l
| [] h := by contradiction
| (a::l) h := rfl
theorem cons_head_tail [inhabited α] {l : list α} (h : l ≠ []) : (head l)::(tail l) = l :=
cons_head'_tail (head_mem_head' h)
lemma head_mem_self [inhabited α] {l : list α} (h : l ≠ nil) : l.head ∈ l :=
begin
have h' := mem_cons_self l.head l.tail,
rwa cons_head_tail h at h',
end
@[simp] theorem head'_map (f : α → β) (l) : head' (map f l) = (head' l).map f := by cases l; refl
lemma tail_append_of_ne_nil (l l' : list α) (h : l ≠ []) :
(l ++ l').tail = l.tail ++ l' :=
begin
cases l,
{ contradiction },
{ simp }
end
@[simp]
lemma nth_le_tail (l : list α) (i) (h : i < l.tail.length)
(h' : i + 1 < l.length := by simpa [←lt_tsub_iff_right] using h) :
l.tail.nth_le i h = l.nth_le (i + 1) h' :=
begin
cases l,
{ cases h, },
{ simpa }
end
/-! ### Induction from the right -/
/-- Induction principle from the right for lists: if a property holds for the empty list, and
for `l ++ [a]` if it holds for `l`, then it holds for all lists. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
@[elab_as_eliminator] def reverse_rec_on {C : list α → Sort*}
(l : list α) (H0 : C [])
(H1 : ∀ (l : list α) (a : α), C l → C (l ++ [a])) : C l :=
begin
rw ← reverse_reverse l,
induction reverse l,
{ exact H0 },
{ rw reverse_cons, exact H1 _ _ ih }
end
/-- Bidirectional induction principle for lists: if a property holds for the empty list, the
singleton list, and `a :: (l ++ [b])` from `l`, then it holds for all lists. This can be used to
prove statements about palindromes. The principle is given for a `Sort`-valued predicate, i.e., it
can also be used to construct data. -/
def bidirectional_rec {C : list α → Sort*}
(H0 : C []) (H1 : ∀ (a : α), C [a])
(Hn : ∀ (a : α) (l : list α) (b : α), C l → C (a :: (l ++ [b]))) : ∀ l, C l
| [] := H0
| [a] := H1 a
| (a :: b :: l) :=
let l' := init (b :: l), b' := last (b :: l) (cons_ne_nil _ _) in
have length l' < length (a :: b :: l), by { change _ < length l + 2, simp },
begin
rw ←init_append_last (cons_ne_nil b l),
have : C l', from bidirectional_rec l',
exact Hn a l' b' ‹C l'›
end
using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf list.length⟩] }
/-- Like `bidirectional_rec`, but with the list parameter placed first. -/
@[elab_as_eliminator] def bidirectional_rec_on {C : list α → Sort*}
(l : list α) (H0 : C []) (H1 : ∀ (a : α), C [a])
(Hn : ∀ (a : α) (l : list α) (b : α), C l → C (a :: (l ++ [b]))) : C l :=
bidirectional_rec H0 H1 Hn l
/-! ### sublists -/
@[simp] theorem nil_sublist : Π (l : list α), [] <+ l
| [] := sublist.slnil
| (a :: l) := sublist.cons _ _ a (nil_sublist l)
@[refl, simp] theorem sublist.refl : Π (l : list α), l <+ l
| [] := sublist.slnil
| (a :: l) := sublist.cons2 _ _ a (sublist.refl l)
@[trans] theorem sublist.trans {l₁ l₂ l₃ : list α} (h₁ : l₁ <+ l₂) (h₂ : l₂ <+ l₃) : l₁ <+ l₃ :=
sublist.rec_on h₂ (λ_ s, s)
(λl₂ l₃ a h₂ IH l₁ h₁, sublist.cons _ _ _ (IH l₁ h₁))
(λl₂ l₃ a h₂ IH l₁ h₁, @sublist.cases_on _ (λl₁ l₂', l₂' = a :: l₂ → l₁ <+ a :: l₃) _ _ h₁
(λ_, nil_sublist _)
(λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ :=
sublist.cons _ _ _ (IH _ h₁) end)
(λl₁ l₂' a' h₁' e, match a', l₂', e, h₁' with ._, ._, rfl, h₁ :=
sublist.cons2 _ _ _ (IH _ h₁) end) rfl)
l₁ h₁
@[simp] theorem sublist_cons (a : α) (l : list α) : l <+ a::l :=
sublist.cons _ _ _ (sublist.refl l)
theorem sublist_of_cons_sublist {a : α} {l₁ l₂ : list α} : a::l₁ <+ l₂ → l₁ <+ l₂ :=
sublist.trans (sublist_cons a l₁)
theorem sublist.cons_cons {l₁ l₂ : list α} (a : α) (s : l₁ <+ l₂) : a::l₁ <+ a::l₂ :=
sublist.cons2 _ _ _ s
@[simp] theorem sublist_append_left : Π (l₁ l₂ : list α), l₁ <+ l₁++l₂
| [] l₂ := nil_sublist _
| (a::l₁) l₂ := (sublist_append_left l₁ l₂).cons_cons _
@[simp] theorem sublist_append_right : Π (l₁ l₂ : list α), l₂ <+ l₁++l₂
| [] l₂ := sublist.refl _
| (a::l₁) l₂ := sublist.cons _ _ _ (sublist_append_right l₁ l₂)
theorem sublist_cons_of_sublist (a : α) {l₁ l₂ : list α} : l₁ <+ l₂ → l₁ <+ a::l₂ :=
sublist.cons _ _ _
theorem sublist_append_of_sublist_left {l l₁ l₂ : list α} (s : l <+ l₁) : l <+ l₁++l₂ :=
s.trans $ sublist_append_left _ _
theorem sublist_append_of_sublist_right {l l₁ l₂ : list α} (s : l <+ l₂) : l <+ l₁++l₂ :=
s.trans $ sublist_append_right _ _
theorem sublist_of_cons_sublist_cons {l₁ l₂ : list α} : ∀ {a : α}, a::l₁ <+ a::l₂ → l₁ <+ l₂
| ._ (sublist.cons ._ ._ a s) := sublist_of_cons_sublist s
| ._ (sublist.cons2 ._ ._ a s) := s
theorem cons_sublist_cons_iff {l₁ l₂ : list α} {a : α} : a::l₁ <+ a::l₂ ↔ l₁ <+ l₂ :=
⟨sublist_of_cons_sublist_cons, sublist.cons_cons _⟩
@[simp] theorem append_sublist_append_left {l₁ l₂ : list α} : ∀ l, l++l₁ <+ l++l₂ ↔ l₁ <+ l₂
| [] := iff.rfl
| (a::l) := cons_sublist_cons_iff.trans (append_sublist_append_left l)
theorem sublist.append_right {l₁ l₂ : list α} (h : l₁ <+ l₂) (l) : l₁++l <+ l₂++l :=
begin
induction h with _ _ a _ ih _ _ a _ ih,
{ refl },
{ apply sublist_cons_of_sublist a ih },
{ apply ih.cons_cons a }
end
theorem sublist_or_mem_of_sublist {l l₁ l₂ : list α} {a : α} (h : l <+ l₁ ++ a::l₂) :
l <+ l₁ ++ l₂ ∨ a ∈ l :=
begin
induction l₁ with b l₁ IH generalizing l,
{ cases h, { left, exact ‹l <+ l₂› }, { right, apply mem_cons_self } },
{ cases h with _ _ _ h _ _ _ h,
{ exact or.imp_left (sublist_cons_of_sublist _) (IH h) },
{ exact (IH h).imp (sublist.cons_cons _) (mem_cons_of_mem _) } }
end
theorem sublist.reverse {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.reverse <+ l₂.reverse :=
begin
induction h with _ _ _ _ ih _ _ a _ ih, {refl},
{ rw reverse_cons, exact sublist_append_of_sublist_left ih },
{ rw [reverse_cons, reverse_cons], exact ih.append_right [a] }
end
@[simp] theorem reverse_sublist_iff {l₁ l₂ : list α} : l₁.reverse <+ l₂.reverse ↔ l₁ <+ l₂ :=
⟨λ h, l₁.reverse_reverse ▸ l₂.reverse_reverse ▸ h.reverse, sublist.reverse⟩
@[simp] theorem append_sublist_append_right {l₁ l₂ : list α} (l) : l₁++l <+ l₂++l ↔ l₁ <+ l₂ :=
⟨λ h, by simpa only [reverse_append, append_sublist_append_left, reverse_sublist_iff]
using h.reverse,
λ h, h.append_right l⟩
theorem sublist.append {l₁ l₂ r₁ r₂ : list α}
(hl : l₁ <+ l₂) (hr : r₁ <+ r₂) : l₁ ++ r₁ <+ l₂ ++ r₂ :=
(hl.append_right _).trans ((append_sublist_append_left _).2 hr)
theorem sublist.subset : Π {l₁ l₂ : list α}, l₁ <+ l₂ → l₁ ⊆ l₂
| ._ ._ sublist.slnil b h := h
| ._ ._ (sublist.cons l₁ l₂ a s) b h := mem_cons_of_mem _ (sublist.subset s h)
| ._ ._ (sublist.cons2 l₁ l₂ a s) b h :=
match eq_or_mem_of_mem_cons h with
| or.inl h := h ▸ mem_cons_self _ _
| or.inr h := mem_cons_of_mem _ (sublist.subset s h)
end
@[simp] theorem singleton_sublist {a : α} {l} : [a] <+ l ↔ a ∈ l :=
⟨λ h, h.subset (mem_singleton_self _), λ h,
let ⟨s, t, e⟩ := mem_split h in e.symm ▸
((nil_sublist _).cons_cons _ ).trans (sublist_append_right _ _)⟩
theorem eq_nil_of_sublist_nil {l : list α} (s : l <+ []) : l = [] :=
eq_nil_of_subset_nil $ s.subset
@[simp] theorem sublist_nil_iff_eq_nil {l : list α} : l <+ [] ↔ l = [] :=
⟨eq_nil_of_sublist_nil, λ H, H ▸ sublist.refl _⟩
@[simp] theorem repeat_sublist_repeat (a : α) {m n} : repeat a m <+ repeat a n ↔ m ≤ n :=
⟨λ h, by simpa only [length_repeat] using length_le_of_sublist h,
λ h, by induction h; [refl, simp only [*, repeat_succ, sublist.cons]] ⟩
theorem eq_of_sublist_of_length_eq : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → length l₁ = length l₂ → l₁ = l₂
| ._ ._ sublist.slnil h := rfl
| ._ ._ (sublist.cons l₁ l₂ a s) h :=
absurd (length_le_of_sublist s) $ not_le_of_gt $ by rw h; apply lt_succ_self
| ._ ._ (sublist.cons2 l₁ l₂ a s) h :=
by rw [length, length] at h; injection h with h; rw eq_of_sublist_of_length_eq s h
theorem eq_of_sublist_of_length_le {l₁ l₂ : list α} (s : l₁ <+ l₂) (h : length l₂ ≤ length l₁) :
l₁ = l₂ :=
eq_of_sublist_of_length_eq s (le_antisymm (length_le_of_sublist s) h)
theorem sublist.antisymm {l₁ l₂ : list α} (s₁ : l₁ <+ l₂) (s₂ : l₂ <+ l₁) : l₁ = l₂ :=
eq_of_sublist_of_length_le s₁ (length_le_of_sublist s₂)
instance decidable_sublist [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+ l₂)
| [] l₂ := is_true $ nil_sublist _
| (a::l₁) [] := is_false $ λh, list.no_confusion $ eq_nil_of_sublist_nil h
| (a::l₁) (b::l₂) :=
if h : a = b then
decidable_of_decidable_of_iff (decidable_sublist l₁ l₂) $
by rw [← h]; exact ⟨sublist.cons_cons _, sublist_of_cons_sublist_cons⟩
else decidable_of_decidable_of_iff (decidable_sublist (a::l₁) l₂)
⟨sublist_cons_of_sublist _, λs, match a, l₁, s, h with
| a, l₁, sublist.cons ._ ._ ._ s', h := s'
| ._, ._, sublist.cons2 t ._ ._ s', h := absurd rfl h
end⟩
/-! ### index_of -/
section index_of
variable [decidable_eq α]
@[simp] theorem index_of_nil (a : α) : index_of a [] = 0 := rfl
theorem index_of_cons (a b : α) (l : list α) :
index_of a (b::l) = if a = b then 0 else succ (index_of a l) := rfl
theorem index_of_cons_eq {a b : α} (l : list α) : a = b → index_of a (b::l) = 0 :=
assume e, if_pos e
@[simp] theorem index_of_cons_self (a : α) (l : list α) : index_of a (a::l) = 0 :=
index_of_cons_eq _ rfl
@[simp, priority 990]
theorem index_of_cons_ne {a b : α} (l : list α) : a ≠ b → index_of a (b::l) = succ (index_of a l) :=
assume n, if_neg n
theorem index_of_eq_length {a : α} {l : list α} : index_of a l = length l ↔ a ∉ l :=
begin
induction l with b l ih,
{ exact iff_of_true rfl (not_mem_nil _) },
simp only [length, mem_cons_iff, index_of_cons], split_ifs,
{ exact iff_of_false (by rintro ⟨⟩) (λ H, H $ or.inl h) },
{ simp only [h, false_or], rw ← ih, exact succ_inj' }
end
@[simp, priority 980]
theorem index_of_of_not_mem {l : list α} {a : α} : a ∉ l → index_of a l = length l :=
index_of_eq_length.2
theorem index_of_le_length {a : α} {l : list α} : index_of a l ≤ length l :=
begin
induction l with b l ih, {refl},
simp only [length, index_of_cons],
by_cases h : a = b, {rw if_pos h, exact nat.zero_le _},
rw if_neg h, exact succ_le_succ ih
end
theorem index_of_lt_length {a} {l : list α} : index_of a l < length l ↔ a ∈ l :=
⟨λh, decidable.by_contradiction $ λ al, ne_of_lt h $ index_of_eq_length.2 al,
λal, lt_of_le_of_ne index_of_le_length $ λ h, index_of_eq_length.1 h al⟩
end index_of
/-! ### nth element -/
theorem nth_le_of_mem : ∀ {a} {l : list α}, a ∈ l → ∃ n h, nth_le l n h = a
| a (_ :: l) (or.inl rfl) := ⟨0, succ_pos _, rfl⟩
| a (b :: l) (or.inr m) :=
let ⟨n, h, e⟩ := nth_le_of_mem m in ⟨n+1, succ_lt_succ h, e⟩
theorem nth_le_nth : ∀ {l : list α} {n} h, nth l n = some (nth_le l n h)
| (a :: l) 0 h := rfl
| (a :: l) (n+1) h := @nth_le_nth l n _
theorem nth_len_le : ∀ {l : list α} {n}, length l ≤ n → nth l n = none
| [] n h := rfl
| (a :: l) (n+1) h := nth_len_le (le_of_succ_le_succ h)
theorem nth_eq_some {l : list α} {n a} : nth l n = some a ↔ ∃ h, nth_le l n h = a :=
⟨λ e,
have h : n < length l, from lt_of_not_ge $ λ hn,
by rw nth_len_le hn at e; contradiction,
⟨h, by rw nth_le_nth h at e;
injection e with e; apply nth_le_mem⟩,
λ ⟨h, e⟩, e ▸ nth_le_nth _⟩
@[simp]
theorem nth_eq_none_iff : ∀ {l : list α} {n}, nth l n = none ↔ length l ≤ n :=
begin
intros, split,
{ intro h, by_contradiction h',
have h₂ : ∃ h, l.nth_le n h = l.nth_le n (lt_of_not_ge h') := ⟨lt_of_not_ge h', rfl⟩,
rw [← nth_eq_some, h] at h₂, cases h₂ },
{ solve_by_elim [nth_len_le] },
end
theorem nth_of_mem {a} {l : list α} (h : a ∈ l) : ∃ n, nth l n = some a :=
let ⟨n, h, e⟩ := nth_le_of_mem h in ⟨n, by rw [nth_le_nth, e]⟩
theorem nth_le_mem : ∀ (l : list α) n h, nth_le l n h ∈ l
| (a :: l) 0 h := mem_cons_self _ _
| (a :: l) (n+1) h := mem_cons_of_mem _ (nth_le_mem l _ _)
theorem nth_mem {l : list α} {n a} (e : nth l n = some a) : a ∈ l :=
let ⟨h, e⟩ := nth_eq_some.1 e in e ▸ nth_le_mem _ _ _
theorem mem_iff_nth_le {a} {l : list α} : a ∈ l ↔ ∃ n h, nth_le l n h = a :=
⟨nth_le_of_mem, λ ⟨n, h, e⟩, e ▸ nth_le_mem _ _ _⟩
theorem mem_iff_nth {a} {l : list α} : a ∈ l ↔ ∃ n, nth l n = some a :=
mem_iff_nth_le.trans $ exists_congr $ λ n, nth_eq_some.symm
lemma nth_zero (l : list α) : l.nth 0 = l.head' := by cases l; refl
lemma nth_injective {α : Type u} {xs : list α} {i j : ℕ}
(h₀ : i < xs.length)
(h₁ : nodup xs)
(h₂ : xs.nth i = xs.nth j) : i = j :=
begin
induction xs with x xs generalizing i j,
{ cases h₀ },
{ cases i; cases j,
case nat.zero nat.zero
{ refl },
case nat.succ nat.succ
{ congr, cases h₁,
apply xs_ih;
solve_by_elim [lt_of_succ_lt_succ] },
iterate 2
{ dsimp at h₂,
cases h₁ with _ _ h h',
cases h x _ rfl,
rw mem_iff_nth,
exact ⟨_, h₂.symm⟩ <|>
exact ⟨_, h₂⟩ } },
end
@[simp] theorem nth_map (f : α → β) : ∀ l n, nth (map f l) n = (nth l n).map f
| [] n := rfl
| (a :: l) 0 := rfl
| (a :: l) (n+1) := nth_map l n
theorem nth_le_map (f : α → β) {l n} (H1 H2) : nth_le (map f l) n H1 = f (nth_le l n H2) :=
option.some.inj $ by rw [← nth_le_nth, nth_map, nth_le_nth]; refl
/-- A version of `nth_le_map` that can be used for rewriting. -/
theorem nth_le_map_rev (f : α → β) {l n} (H) :
f (nth_le l n H) = nth_le (map f l) n ((length_map f l).symm ▸ H) :=
(nth_le_map f _ _).symm
@[simp] theorem nth_le_map' (f : α → β) {l n} (H) :
nth_le (map f l) n H = f (nth_le l n (length_map f l ▸ H)) :=
nth_le_map f _ _
/-- If one has `nth_le L i hi` in a formula and `h : L = L'`, one can not `rw h` in the formula as
`hi` gives `i < L.length` and not `i < L'.length`. The lemma `nth_le_of_eq` can be used to make
such a rewrite, with `rw (nth_le_of_eq h)`. -/
lemma nth_le_of_eq {L L' : list α} (h : L = L') {i : ℕ} (hi : i < L.length) :
nth_le L i hi = nth_le L' i (h ▸ hi) :=
by { congr, exact h}
@[simp] lemma nth_le_singleton (a : α) {n : ℕ} (hn : n < 1) :
nth_le [a] n hn = a :=
have hn0 : n = 0 := le_zero_iff.1 (le_of_lt_succ hn),
by subst hn0; refl
lemma nth_le_zero [inhabited α] {L : list α} (h : 0 < L.length) :
L.nth_le 0 h = L.head :=
by { cases L, cases h, simp, }
lemma nth_le_append : ∀ {l₁ l₂ : list α} {n : ℕ} (hn₁) (hn₂),
(l₁ ++ l₂).nth_le n hn₁ = l₁.nth_le n hn₂
| [] _ n hn₁ hn₂ := (nat.not_lt_zero _ hn₂).elim
| (a::l) _ 0 hn₁ hn₂ := rfl
| (a::l) _ (n+1) hn₁ hn₂ := by simp only [nth_le, cons_append];
exact nth_le_append _ _
lemma nth_le_append_right_aux {l₁ l₂ : list α} {n : ℕ}
(h₁ : l₁.length ≤ n) (h₂ : n < (l₁ ++ l₂).length) : n - l₁.length < l₂.length :=
begin
rw list.length_append at h₂,
convert (tsub_lt_tsub_iff_right h₁).mpr h₂,
simp,
end
lemma nth_le_append_right : ∀ {l₁ l₂ : list α} {n : ℕ} (h₁ : l₁.length ≤ n) (h₂),
(l₁ ++ l₂).nth_le n h₂ = l₂.nth_le (n - l₁.length) (nth_le_append_right_aux h₁ h₂)
| [] _ n h₁ h₂ := rfl
| (a :: l) _ (n+1) h₁ h₂ :=
begin
dsimp,
conv { to_rhs, congr, skip,
rw [tsub_add_eq_tsub_tsub, tsub_right_comm, add_tsub_cancel_right], },
rw nth_le_append_right (nat.lt_succ_iff.mp h₁),
end
@[simp] lemma nth_le_repeat (a : α) {n m : ℕ} (h : m < (list.repeat a n).length) :
(list.repeat a n).nth_le m h = a :=
eq_of_mem_repeat (nth_le_mem _ _ _)
lemma nth_append {l₁ l₂ : list α} {n : ℕ} (hn : n < l₁.length) :
(l₁ ++ l₂).nth n = l₁.nth n :=
have hn' : n < (l₁ ++ l₂).length := lt_of_lt_of_le hn
(by rw length_append; exact nat.le_add_right _ _),
by rw [nth_le_nth hn, nth_le_nth hn', nth_le_append]
lemma nth_append_right {l₁ l₂ : list α} {n : ℕ} (hn : l₁.length ≤ n) :
(l₁ ++ l₂).nth n = l₂.nth (n - l₁.length) :=
begin
by_cases hl : n < (l₁ ++ l₂).length,
{ rw [nth_le_nth hl, nth_le_nth, nth_le_append_right hn] },
{ rw [nth_len_le (le_of_not_lt hl), nth_len_le],
rw [not_lt, length_append] at hl,
exact le_tsub_of_add_le_left hl }
end
lemma last_eq_nth_le : ∀ (l : list α) (h : l ≠ []),
last l h = l.nth_le (l.length - 1) (nat.sub_lt (length_pos_of_ne_nil h) one_pos)
| [] h := rfl
| [a] h := by rw [last_singleton, nth_le_singleton]
| (a :: b :: l) h := by { rw [last_cons, last_eq_nth_le (b :: l)],
refl, exact cons_ne_nil b l }
@[simp] lemma nth_concat_length : ∀ (l : list α) (a : α), (l ++ [a]).nth l.length = some a
| [] a := rfl
| (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length]
lemma nth_le_cons_length (x : α) (xs : list α) (n : ℕ) (h : n = xs.length) :
(x :: xs).nth_le n (by simp [h]) = (x :: xs).last (cons_ne_nil x xs) :=
begin
rw last_eq_nth_le,
congr,
simp [h]
end
@[ext]
theorem ext : ∀ {l₁ l₂ : list α}, (∀n, nth l₁ n = nth l₂ n) → l₁ = l₂
| [] [] h := rfl
| (a::l₁) [] h := by have h0 := h 0; contradiction
| [] (a'::l₂) h := by have h0 := h 0; contradiction
| (a::l₁) (a'::l₂) h := by have h0 : some a = some a' := h 0; injection h0 with aa;
simp only [aa, ext (λn, h (n+1))]; split; refl
theorem ext_le {l₁ l₂ : list α} (hl : length l₁ = length l₂)
(h : ∀n h₁ h₂, nth_le l₁ n h₁ = nth_le l₂ n h₂) : l₁ = l₂ :=
ext $ λn, if h₁ : n < length l₁
then by rw [nth_le_nth, nth_le_nth, h n h₁ (by rwa [← hl])]
else let h₁ := le_of_not_gt h₁ in by { rw [nth_len_le h₁, nth_len_le], rwa [←hl], }
@[simp] theorem index_of_nth_le [decidable_eq α] {a : α} :
∀ {l : list α} h, nth_le l (index_of a l) h = a
| (b::l) h := by by_cases h' : a = b;
simp only [h', if_pos, if_false, index_of_cons, nth_le, @index_of_nth_le l]
@[simp] theorem index_of_nth [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) :
nth l (index_of a l) = some a :=
by rw [nth_le_nth, index_of_nth_le (index_of_lt_length.2 h)]
theorem nth_le_reverse_aux1 :
∀ (l r : list α) (i h1 h2), nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2
| [] r i := λh1 h2, rfl
| (a :: l) r i :=
by rw (show i + length (a :: l) = i + 1 + length l, from add_right_comm i (length l) 1);
exact λh1 h2, nth_le_reverse_aux1 l (a :: r) (i+1) h1 (succ_lt_succ h2)
lemma index_of_inj [decidable_eq α] {l : list α} {x y : α}
(hx : x ∈ l) (hy : y ∈ l) : index_of x l = index_of y l ↔ x = y :=
⟨λ h, have nth_le l (index_of x l) (index_of_lt_length.2 hx) =
nth_le l (index_of y l) (index_of_lt_length.2 hy),
by simp only [h],
by simpa only [index_of_nth_le],
λ h, by subst h⟩
theorem nth_le_reverse_aux2 : ∀ (l r : list α) (i : nat) (h1) (h2),
nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2
| [] r i h1 h2 := absurd h2 (nat.not_lt_zero _)
| (a :: l) r 0 h1 h2 := begin
have aux := nth_le_reverse_aux1 l (a :: r) 0,
rw zero_add at aux,
exact aux _ (zero_lt_succ _)
end
| (a :: l) r (i+1) h1 h2 := begin
have aux := nth_le_reverse_aux2 l (a :: r) i,
have heq := calc length (a :: l) - 1 - (i + 1)
= length l - (1 + i) : by rw add_comm; refl
... = length l - 1 - i : by rw ← tsub_add_eq_tsub_tsub,
rw [← heq] at aux,
apply aux
end
@[simp] theorem nth_le_reverse (l : list α) (i : nat) (h1 h2) :
nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 :=
nth_le_reverse_aux2 _ _ _ _ _
lemma nth_le_reverse' (l : list α) (n : ℕ) (hn : n < l.reverse.length) (hn') :
l.reverse.nth_le n hn = l.nth_le (l.length - 1 - n) hn' :=
begin
rw eq_comm,
convert nth_le_reverse l.reverse _ _ _ using 1,
{ simp },
{ simpa }
end
lemma eq_cons_of_length_one {l : list α} (h : l.length = 1) :
l = [l.nth_le 0 (h.symm ▸ zero_lt_one)] :=
begin
refine ext_le (by convert h) (λ n h₁ h₂, _),
simp only [nth_le_singleton],
congr,
exact eq_bot_iff.mpr (nat.lt_succ_iff.mp h₂)
end
lemma modify_nth_tail_modify_nth_tail {f g : list α → list α} (m : ℕ) :
∀n (l:list α), (l.modify_nth_tail f n).modify_nth_tail g (m + n) =
l.modify_nth_tail (λl, (f l).modify_nth_tail g m) n
| 0 l := rfl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_modify_nth_tail n l)
lemma modify_nth_tail_modify_nth_tail_le
{f g : list α → list α} (m n : ℕ) (l : list α) (h : n ≤ m) :
(l.modify_nth_tail f n).modify_nth_tail g m =
l.modify_nth_tail (λl, (f l).modify_nth_tail g (m - n)) n :=
begin
rcases le_iff_exists_add.1 h with ⟨m, rfl⟩,
rw [add_tsub_cancel_left, add_comm, modify_nth_tail_modify_nth_tail]
end
lemma modify_nth_tail_modify_nth_tail_same {f g : list α → list α} (n : ℕ) (l:list α) :
(l.modify_nth_tail f n).modify_nth_tail g n = l.modify_nth_tail (g ∘ f) n :=
by rw [modify_nth_tail_modify_nth_tail_le n n l (le_refl n), tsub_self]; refl
lemma modify_nth_tail_id :
∀n (l:list α), l.modify_nth_tail id n = l
| 0 l := rfl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_id n l)
theorem remove_nth_eq_nth_tail : ∀ n (l : list α), remove_nth l n = modify_nth_tail tail n l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (cons _) (remove_nth_eq_nth_tail _ _)
theorem update_nth_eq_modify_nth (a : α) : ∀ n (l : list α),
update_nth l n a = modify_nth (λ _, a) n l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (b::l) := congr_arg (cons _) (update_nth_eq_modify_nth _ _)
theorem modify_nth_eq_update_nth (f : α → α) : ∀ n (l : list α),
modify_nth f n l = ((λ a, update_nth l n (f a)) <$> nth l n).get_or_else l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (b::l) := (congr_arg (cons b)
(modify_nth_eq_update_nth n l)).trans $ by cases nth l n; refl
theorem nth_modify_nth (f : α → α) : ∀ n (l : list α) m,
nth (modify_nth f n l) m = (λ a, if n = m then f a else a) <$> nth l m
| n l 0 := by cases l; cases n; refl
| n [] (m+1) := by cases n; refl
| 0 (a::l) (m+1) := by cases nth l m; refl
| (n+1) (a::l) (m+1) := (nth_modify_nth n l m).trans $
by cases nth l m with b; by_cases n = m;
simp only [h, if_pos, if_true, if_false, option.map_none, option.map_some, mt succ.inj,
not_false_iff]
theorem modify_nth_tail_length (f : list α → list α) (H : ∀ l, length (f l) = length l) :
∀ n l, length (modify_nth_tail f n l) = length l
| 0 l := H _
| (n+1) [] := rfl
| (n+1) (a::l) := @congr_arg _ _ _ _ (+1) (modify_nth_tail_length _ _)
@[simp] theorem modify_nth_length (f : α → α) :
∀ n l, length (modify_nth f n l) = length l :=
modify_nth_tail_length _ (λ l, by cases l; refl)
@[simp] theorem update_nth_length (l : list α) (n) (a : α) :
length (update_nth l n a) = length l :=
by simp only [update_nth_eq_modify_nth, modify_nth_length]
@[simp] theorem nth_modify_nth_eq (f : α → α) (n) (l : list α) :
nth (modify_nth f n l) n = f <$> nth l n :=
by simp only [nth_modify_nth, if_pos]
@[simp] theorem nth_modify_nth_ne (f : α → α) {m n} (l : list α) (h : m ≠ n) :
nth (modify_nth f m l) n = nth l n :=
by simp only [nth_modify_nth, if_neg h, id_map']
theorem nth_update_nth_eq (a : α) (n) (l : list α) :
nth (update_nth l n a) n = (λ _, a) <$> nth l n :=
by simp only [update_nth_eq_modify_nth, nth_modify_nth_eq]
theorem nth_update_nth_of_lt (a : α) {n} {l : list α} (h : n < length l) :
nth (update_nth l n a) n = some a :=
by rw [nth_update_nth_eq, nth_le_nth h]; refl
theorem nth_update_nth_ne (a : α) {m n} (l : list α) (h : m ≠ n) :
nth (update_nth l m a) n = nth l n :=
by simp only [update_nth_eq_modify_nth, nth_modify_nth_ne _ _ h]
@[simp] lemma update_nth_nil (n : ℕ) (a : α) : [].update_nth n a = [] := rfl
@[simp] lemma update_nth_succ (x : α) (xs : list α) (n : ℕ) (a : α) :
(x :: xs).update_nth n.succ a = x :: xs.update_nth n a := rfl
lemma update_nth_comm (a b : α) : Π {n m : ℕ} (l : list α) (h : n ≠ m),
(l.update_nth n a).update_nth m b = (l.update_nth m b).update_nth n a
| _ _ [] _ := by simp
| 0 0 (x :: t) h := absurd rfl h
| (n + 1) 0 (x :: t) h := by simp [list.update_nth]
| 0 (m + 1) (x :: t) h := by simp [list.update_nth]
| (n + 1) (m + 1) (x :: t) h := by { simp only [update_nth, true_and, eq_self_iff_true],
exact update_nth_comm t (λ h', h $ nat.succ_inj'.mpr h'), }
@[simp] lemma nth_le_update_nth_eq (l : list α) (i : ℕ) (a : α)
(h : i < (l.update_nth i a).length) : (l.update_nth i a).nth_le i h = a :=
by rw [← option.some_inj, ← nth_le_nth, nth_update_nth_eq, nth_le_nth]; simp * at *
@[simp] lemma nth_le_update_nth_of_ne {l : list α} {i j : ℕ} (h : i ≠ j) (a : α)
(hj : j < (l.update_nth i a).length) :
(l.update_nth i a).nth_le j hj = l.nth_le j (by simpa using hj) :=
by rw [← option.some_inj, ← list.nth_le_nth, list.nth_update_nth_ne _ _ h, list.nth_le_nth]
lemma mem_or_eq_of_mem_update_nth : ∀ {l : list α} {n : ℕ} {a b : α}
(h : a ∈ l.update_nth n b), a ∈ l ∨ a = b
| [] n a b h := false.elim h
| (c::l) 0 a b h := ((mem_cons_iff _ _ _).1 h).elim
or.inr (or.inl ∘ mem_cons_of_mem _)
| (c::l) (n+1) a b h := ((mem_cons_iff _ _ _).1 h).elim
(λ h, h ▸ or.inl (mem_cons_self _ _))
(λ h, (mem_or_eq_of_mem_update_nth h).elim
(or.inl ∘ mem_cons_of_mem _) or.inr)
section insert_nth
variable {a : α}
@[simp] lemma insert_nth_zero (s : list α) (x : α) : insert_nth 0 x s = x :: s := rfl
@[simp] lemma insert_nth_succ_nil (n : ℕ) (a : α) : insert_nth (n + 1) a [] = [] := rfl
@[simp] lemma insert_nth_succ_cons (s : list α) (hd x : α) (n : ℕ) :
insert_nth (n + 1) x (hd :: s) = hd :: (insert_nth n x s) := rfl
lemma length_insert_nth : ∀n as, n ≤ length as → length (insert_nth n a as) = length as + 1
| 0 as h := rfl
| (n+1) [] h := (nat.not_succ_le_zero _ h).elim
| (n+1) (a'::as) h := congr_arg nat.succ $ length_insert_nth n as (nat.le_of_succ_le_succ h)
lemma remove_nth_insert_nth (n:ℕ) (l : list α) : (l.insert_nth n a).remove_nth n = l :=
by rw [remove_nth_eq_nth_tail, insert_nth, modify_nth_tail_modify_nth_tail_same];
from modify_nth_tail_id _ _
lemma insert_nth_remove_nth_of_ge : ∀n m as, n < length as → n ≤ m →
insert_nth m a (as.remove_nth n) = (as.insert_nth (m + 1) a).remove_nth n
| 0 0 [] has _ := (lt_irrefl _ has).elim
| 0 0 (a::as) has hmn := by simp [remove_nth, insert_nth]
| 0 (m+1) (a::as) has hmn := rfl
| (n+1) (m+1) (a::as) has hmn :=
congr_arg (cons a) $
insert_nth_remove_nth_of_ge n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn)
lemma insert_nth_remove_nth_of_le : ∀n m as, n < length as → m ≤ n →
insert_nth m a (as.remove_nth n) = (as.insert_nth m a).remove_nth (n + 1)
| n 0 (a :: as) has hmn := rfl
| (n + 1) (m + 1) (a :: as) has hmn :=
congr_arg (cons a) $
insert_nth_remove_nth_of_le n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn)
lemma insert_nth_comm (a b : α) :
∀(i j : ℕ) (l : list α) (h : i ≤ j) (hj : j ≤ length l),
(l.insert_nth i a).insert_nth (j + 1) b = (l.insert_nth j b).insert_nth i a
| 0 j l := by simp [insert_nth]
| (i + 1) 0 l := assume h, (nat.not_lt_zero _ h).elim
| (i + 1) (j+1) [] := by simp
| (i + 1) (j+1) (c::l) :=
assume h₀ h₁,
by simp [insert_nth];
exact insert_nth_comm i j l (nat.le_of_succ_le_succ h₀) (nat.le_of_succ_le_succ h₁)
lemma mem_insert_nth {a b : α} : ∀ {n : ℕ} {l : list α} (hi : n ≤ l.length),
a ∈ l.insert_nth n b ↔ a = b ∨ a ∈ l
| 0 as h := iff.rfl
| (n+1) [] h := (nat.not_succ_le_zero _ h).elim
| (n+1) (a'::as) h := begin
dsimp [list.insert_nth],
erw [list.mem_cons_iff, mem_insert_nth (nat.le_of_succ_le_succ h), list.mem_cons_iff,
← or.assoc, or_comm (a = a'), or.assoc]
end
lemma inj_on_insert_nth_index_of_not_mem (l : list α) (x : α) (hx : x ∉ l) :
set.inj_on (λ k, insert_nth k x l) {n | n ≤ l.length} :=
begin
induction l with hd tl IH,
{ intros n hn m hm h,
simp only [set.mem_singleton_iff, set.set_of_eq_eq_singleton, length, nonpos_iff_eq_zero]
at hn hm,
simp [hn, hm] },
{ intros n hn m hm h,
simp only [length, set.mem_set_of_eq] at hn hm,
simp only [mem_cons_iff, not_or_distrib] at hx,
cases n;
cases m,
{ refl },
{ simpa [hx.left] using h },
{ simpa [ne.symm hx.left] using h },
{ simp only [true_and, eq_self_iff_true, insert_nth_succ_cons] at h,
rw nat.succ_inj',
refine IH hx.right _ _ h,
{ simpa [nat.succ_le_succ_iff] using hn },
{ simpa [nat.succ_le_succ_iff] using hm } } }
end
lemma insert_nth_of_length_lt (l : list α) (x : α) (n : ℕ) (h : l.length < n) :
insert_nth n x l = l :=
begin
induction l with hd tl IH generalizing n,
{ cases n,
{ simpa using h },
{ simp } },
{ cases n,
{ simpa using h },
{ simp only [nat.succ_lt_succ_iff, length] at h,
simpa using IH _ h } }
end
@[simp] lemma insert_nth_length_self (l : list α) (x : α) :
insert_nth l.length x l = l ++ [x] :=
begin
induction l with hd tl IH,
{ simp },
{ simpa using IH }
end
lemma length_le_length_insert_nth (l : list α) (x : α) (n : ℕ) :
l.length ≤ (insert_nth n x l).length :=
begin
cases le_or_lt n l.length with hn hn,
{ rw length_insert_nth _ _ hn,
exact (nat.lt_succ_self _).le },
{ rw insert_nth_of_length_lt _ _ _ hn }
end
lemma length_insert_nth_le_succ (l : list α) (x : α) (n : ℕ) :
(insert_nth n x l).length ≤ l.length + 1 :=
begin
cases le_or_lt n l.length with hn hn,
{ rw length_insert_nth _ _ hn },
{ rw insert_nth_of_length_lt _ _ _ hn,
exact (nat.lt_succ_self _).le }
end
lemma nth_le_insert_nth_of_lt (l : list α) (x : α) (n k : ℕ) (hn : k < n)
(hk : k < l.length)
(hk' : k < (insert_nth n x l).length := hk.trans_le (length_le_length_insert_nth _ _ _)):
(insert_nth n x l).nth_le k hk' = l.nth_le k hk :=
begin
induction n with n IH generalizing k l,
{ simpa using hn },
{ cases l with hd tl,
{ simp },
{ cases k,
{ simp },
{ rw nat.succ_lt_succ_iff at hn,
simpa using IH _ _ hn _ } } }
end
@[simp] lemma nth_le_insert_nth_self (l : list α) (x : α) (n : ℕ)
(hn : n ≤ l.length) (hn' : n < (insert_nth n x l).length :=
by rwa [length_insert_nth _ _ hn, nat.lt_succ_iff]) :
(insert_nth n x l).nth_le n hn' = x :=
begin
induction l with hd tl IH generalizing n,
{ simp only [length, nonpos_iff_eq_zero] at hn,
simp [hn] },
{ cases n,
{ simp },
{ simp only [nat.succ_le_succ_iff, length] at hn,
simpa using IH _ hn } }
end
lemma nth_le_insert_nth_add_succ (l : list α) (x : α) (n k : ℕ)
(hk' : n + k < l.length)
(hk : n + k + 1 < (insert_nth n x l).length :=
by rwa [length_insert_nth _ _ (le_self_add.trans hk'.le), nat.succ_lt_succ_iff]) :
(insert_nth n x l).nth_le (n + k + 1) hk = nth_le l (n + k) hk' :=
begin
induction l with hd tl IH generalizing n k,
{ simpa using hk' },
{ cases n,
{ simpa },
{ simpa [succ_add] using IH _ _ _ } }
end
lemma insert_nth_injective (n : ℕ) (x : α) : function.injective (insert_nth n x) :=
begin
induction n with n IH,
{ have : insert_nth 0 x = cons x := funext (λ _, rfl),
simp [this] },
{ rintros (_|⟨a, as⟩) (_|⟨b, bs⟩) h;
simpa [IH.eq_iff] using h <|> refl }
end
end insert_nth
/-! ### map -/
@[simp] lemma map_nil (f : α → β) : map f [] = [] := rfl
theorem map_eq_foldr (f : α → β) (l : list α) :
map f l = foldr (λ a bs, f a :: bs) [] l :=
by induction l; simp *
lemma map_congr {f g : α → β} : ∀ {l : list α}, (∀ x ∈ l, f x = g x) → map f l = map g l
| [] _ := rfl
| (a::l) h := let ⟨h₁, h₂⟩ := forall_mem_cons.1 h in
by rw [map, map, h₁, map_congr h₂]
lemma map_eq_map_iff {f g : α → β} {l : list α} : map f l = map g l ↔ (∀ x ∈ l, f x = g x) :=
begin
refine ⟨_, map_congr⟩, intros h x hx,
rw [mem_iff_nth_le] at hx, rcases hx with ⟨n, hn, rfl⟩,
rw [nth_le_map_rev f, nth_le_map_rev g], congr, exact h
end
theorem map_concat (f : α → β) (a : α) (l : list α) : map f (concat l a) = concat (map f l) (f a) :=
by induction l; [refl, simp only [*, concat_eq_append, cons_append, map, map_append]]; split; refl
theorem map_id' {f : α → α} (h : ∀ x, f x = x) (l : list α) : map f l = l :=
by induction l; [refl, simp only [*, map]]; split; refl
theorem eq_nil_of_map_eq_nil {f : α → β} {l : list α} (h : map f l = nil) : l = nil :=
eq_nil_of_length_eq_zero $ by rw [← length_map f l, h]; refl
@[simp] theorem map_join (f : α → β) (L : list (list α)) :
map f (join L) = join (map (map f) L) :=
by induction L; [refl, simp only [*, join, map, map_append]]
theorem bind_ret_eq_map (f : α → β) (l : list α) :
l.bind (list.ret ∘ f) = map f l :=
by unfold list.bind; induction l; simp only [map, join, list.ret, cons_append, nil_append, *];
split; refl
@[simp] theorem map_eq_map {α β} (f : α → β) (l : list α) : f <$> l = map f l := rfl
@[simp] theorem map_tail (f : α → β) (l) : map f (tail l) = tail (map f l) :=
by cases l; refl
@[simp] theorem map_injective_iff {f : α → β} : injective (map f) ↔ injective f :=
begin
split; intros h x y hxy,
{ suffices : [x] = [y], { simpa using this }, apply h, simp [hxy] },
{ induction y generalizing x, simpa using hxy,
cases x, simpa using hxy, simp at hxy, simp [y_ih hxy.2, h hxy.1] }
end
/--
A single `list.map` of a composition of functions is equal to
composing a `list.map` with another `list.map`, fully applied.
This is the reverse direction of `list.map_map`.
-/
lemma comp_map (h : β → γ) (g : α → β) (l : list α) :
map (h ∘ g) l = map h (map g l) := (map_map _ _ _).symm
/--
Composing a `list.map` with another `list.map` is equal to
a single `list.map` of composed functions.
-/
@[simp] lemma map_comp_map (g : β → γ) (f : α → β) :
map g ∘ map f = map (g ∘ f) :=
by { ext l, rw comp_map }
theorem map_filter_eq_foldr (f : α → β) (p : α → Prop) [decidable_pred p] (as : list α) :
map f (filter p as) = foldr (λ a bs, if p a then f a :: bs else bs) [] as :=
by { induction as, { refl }, { simp! [*, apply_ite (map f)] } }
lemma last_map (f : α → β) {l : list α} (hl : l ≠ []) :
(l.map f).last (mt eq_nil_of_map_eq_nil hl) = f (l.last hl) :=
begin
induction l with l_ih l_tl l_ih,
{ apply (hl rfl).elim },
{ cases l_tl,
{ simp },
{ simpa using l_ih } }
end
/-! ### map₂ -/
theorem nil_map₂ (f : α → β → γ) (l : list β) : map₂ f [] l = [] :=
by cases l; refl
theorem map₂_nil (f : α → β → γ) (l : list α) : map₂ f l [] = [] :=
by cases l; refl
@[simp] theorem map₂_flip (f : α → β → γ) :
∀ as bs, map₂ (flip f) bs as = map₂ f as bs
| [] [] := rfl
| [] (b :: bs) := rfl
| (a :: as) [] := rfl
| (a :: as) (b :: bs) := by { simp! [map₂_flip], refl }
/-! ### take, drop -/
@[simp] theorem take_zero (l : list α) : take 0 l = [] := rfl
@[simp] theorem take_nil : ∀ n, take n [] = ([] : list α)
| 0 := rfl
| (n+1) := rfl
theorem take_cons (n) (a : α) (l : list α) : take (succ n) (a::l) = a :: take n l := rfl
@[simp] theorem take_length : ∀ (l : list α), take (length l) l = l
| [] := rfl
| (a::l) := begin change a :: (take (length l) l) = a :: l, rw take_length end
theorem take_all_of_le : ∀ {n} {l : list α}, length l ≤ n → take n l = l
| 0 [] h := rfl
| 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _))
| (n+1) [] h := rfl
| (n+1) (a::l) h :=
begin
change a :: take n l = a :: l,
rw [take_all_of_le (le_of_succ_le_succ h)]
end
@[simp] theorem take_left : ∀ l₁ l₂ : list α, take (length l₁) (l₁ ++ l₂) = l₁
| [] l₂ := rfl
| (a::l₁) l₂ := congr_arg (cons a) (take_left l₁ l₂)
theorem take_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) :
take n (l₁ ++ l₂) = l₁ :=
by rw ← h; apply take_left
theorem take_take : ∀ (n m) (l : list α), take n (take m l) = take (min n m) l
| n 0 l := by rw [min_zero, take_zero, take_nil]
| 0 m l := by rw [zero_min, take_zero, take_zero]
| (succ n) (succ m) nil := by simp only [take_nil]
| (succ n) (succ m) (a::l) := by simp only [take, min_succ_succ, take_take n m l]; split; refl
theorem take_repeat (a : α) : ∀ (n m : ℕ), take n (repeat a m) = repeat a (min n m)
| n 0 := by simp
| 0 m := by simp
| (succ n) (succ m) := by simp [min_succ_succ, take_repeat]
lemma map_take {α β : Type*} (f : α → β) :
∀ (L : list α) (i : ℕ), (L.take i).map f = (L.map f).take i
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [map_take], }
/-- Taking the first `n` elements in `l₁ ++ l₂` is the same as appending the first `n` elements
of `l₁` to the first `n - l₁.length` elements of `l₂`. -/
lemma take_append_eq_append_take {l₁ l₂ : list α} {n : ℕ} :
take n (l₁ ++ l₂) = take n l₁ ++ take (n - l₁.length) l₂ :=
begin
induction l₁ generalizing n, { simp },
cases n, { simp }, simp *
end
lemma take_append_of_le_length {l₁ l₂ : list α} {n : ℕ} (h : n ≤ l₁.length) :
(l₁ ++ l₂).take n = l₁.take n :=
by simp [take_append_eq_append_take, tsub_eq_zero_iff_le.mpr h]
/-- Taking the first `l₁.length + i` elements in `l₁ ++ l₂` is the same as appending the first
`i` elements of `l₂` to `l₁`. -/
lemma take_append {l₁ l₂ : list α} (i : ℕ) :
take (l₁.length + i) (l₁ ++ l₂) = l₁ ++ (take i l₂) :=
by simp [take_append_eq_append_take, take_all_of_le le_self_add]
/-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of
length `> i`. Version designed to rewrite from the big list to the small list. -/
lemma nth_le_take (L : list α) {i j : ℕ} (hi : i < L.length) (hj : i < j) :
nth_le L i hi = nth_le (L.take j) i (by { rw length_take, exact lt_min hj hi }) :=
by { rw nth_le_of_eq (take_append_drop j L).symm hi, exact nth_le_append _ _ }
/-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of
length `> i`. Version designed to rewrite from the small list to the big list. -/
lemma nth_le_take' (L : list α) {i j : ℕ} (hi : i < (L.take j).length) :
nth_le (L.take j) i hi = nth_le L i (lt_of_lt_of_le hi (by simp [le_refl])) :=
by { simp at hi, rw nth_le_take L _ hi.1 }
lemma nth_take {l : list α} {n m : ℕ} (h : m < n) :
(l.take n).nth m = l.nth m :=
begin
induction n with n hn generalizing l m,
{ simp only [nat.nat_zero_eq_zero] at h,
exact absurd h (not_lt_of_le m.zero_le) },
{ cases l with hd tl,
{ simp only [take_nil] },
{ cases m,
{ simp only [nth, take] },
{ simpa only using hn (nat.lt_of_succ_lt_succ h) } } },
end
@[simp] lemma nth_take_of_succ {l : list α} {n : ℕ} :
(l.take (n + 1)).nth n = l.nth n :=
nth_take (nat.lt_succ_self n)
lemma take_succ {l : list α} {n : ℕ} :
l.take (n + 1) = l.take n ++ (l.nth n).to_list :=
begin
induction l with hd tl hl generalizing n,
{ simp only [option.to_list, nth, take_nil, append_nil]},
{ cases n,
{ simp only [option.to_list, nth, eq_self_iff_true, and_self, take, nil_append] },
{ simp only [hl, cons_append, nth, eq_self_iff_true, and_self, take] } }
end
@[simp] lemma take_eq_nil_iff {l : list α} {k : ℕ} :
l.take k = [] ↔ l = [] ∨ k = 0 :=
by { cases l; cases k; simp [nat.succ_ne_zero] }
lemma init_eq_take (l : list α) : l.init = l.take l.length.pred :=
begin
cases l with x l,
{ simp [init] },
{ induction l with hd tl hl generalizing x,
{ simp [init], },
{ simp [init, hl] } }
end
lemma init_take {n : ℕ} {l : list α} (h : n < l.length) :
(l.take n).init = l.take n.pred :=
by simp [init_eq_take, min_eq_left_of_lt h, take_take, pred_le]
@[simp] lemma init_cons_of_ne_nil {α : Type*} {x : α} :
∀ {l : list α} (h : l ≠ []), (x :: l).init = x :: l.init
| [] h := false.elim (h rfl)
| (a :: l) _ := by simp [init]
@[simp] lemma init_append_of_ne_nil {α : Type*} {l : list α} :
∀ (l' : list α) (h : l ≠ []), (l' ++ l).init = l' ++ l.init
| [] _ := by simp only [nil_append]
| (a :: l') h := by simp [append_ne_nil_of_ne_nil_right l' l h, init_append_of_ne_nil l' h]
@[simp] lemma drop_eq_nil_of_le {l : list α} {k : ℕ} (h : l.length ≤ k) :
l.drop k = [] :=
by simpa [←length_eq_zero] using tsub_eq_zero_iff_le.mpr h
lemma drop_eq_nil_iff_le {l : list α} {k : ℕ} :
l.drop k = [] ↔ l.length ≤ k :=
begin
refine ⟨λ h, _, drop_eq_nil_of_le⟩,
induction k with k hk generalizing l,
{ simp only [drop] at h,
simp [h] },
{ cases l,
{ simp },
{ simp only [drop] at h,
simpa [nat.succ_le_succ_iff] using hk h } }
end
lemma tail_drop (l : list α) (n : ℕ) : (l.drop n).tail = l.drop (n + 1) :=
begin
induction l with hd tl hl generalizing n,
{ simp },
{ cases n,
{ simp },
{ simp [hl] } }
end
lemma cons_nth_le_drop_succ {l : list α} {n : ℕ} (hn : n < l.length) :
l.nth_le n hn :: l.drop (n + 1) = l.drop n :=
begin
induction l with hd tl hl generalizing n,
{ exact absurd n.zero_le (not_le_of_lt (by simpa using hn)) },
{ cases n,
{ simp },
{ simp only [nat.succ_lt_succ_iff, list.length] at hn,
simpa [list.nth_le, list.drop] using hl hn } }
end
theorem drop_nil : ∀ n, drop n [] = ([] : list α) :=
λ _, drop_eq_nil_of_le (nat.zero_le _)
@[simp] theorem drop_one : ∀ l : list α, drop 1 l = tail l
| [] := rfl
| (a :: l) := rfl
theorem drop_add : ∀ m n (l : list α), drop (m + n) l = drop m (drop n l)
| m 0 l := rfl
| m (n+1) [] := (drop_nil _).symm
| m (n+1) (a::l) := drop_add m n _
@[simp] theorem drop_left : ∀ l₁ l₂ : list α, drop (length l₁) (l₁ ++ l₂) = l₂
| [] l₂ := rfl
| (a::l₁) l₂ := drop_left l₁ l₂
theorem drop_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) :
drop n (l₁ ++ l₂) = l₂ :=
by rw ← h; apply drop_left
theorem drop_eq_nth_le_cons : ∀ {n} {l : list α} h,
drop n l = nth_le l n h :: drop (n+1) l
| 0 (a::l) h := rfl
| (n+1) (a::l) h := @drop_eq_nth_le_cons n _ _
@[simp] lemma drop_length (l : list α) : l.drop l.length = [] :=
calc l.drop l.length = (l ++ []).drop l.length : by simp
... = [] : drop_left _ _
/-- Dropping the elements up to `n` in `l₁ ++ l₂` is the same as dropping the elements up to `n`
in `l₁`, dropping the elements up to `n - l₁.length` in `l₂`, and appending them. -/
lemma drop_append_eq_append_drop {l₁ l₂ : list α} {n : ℕ} :
drop n (l₁ ++ l₂) = drop n l₁ ++ drop (n - l₁.length) l₂ :=
begin
induction l₁ generalizing n, { simp },
cases n, { simp }, simp *
end
lemma drop_append_of_le_length {l₁ l₂ : list α} {n : ℕ} (h : n ≤ l₁.length) :
(l₁ ++ l₂).drop n = l₁.drop n ++ l₂ :=
by simp [drop_append_eq_append_drop, tsub_eq_zero_iff_le.mpr h]
/-- Dropping the elements up to `l₁.length + i` in `l₁ + l₂` is the same as dropping the elements
up to `i` in `l₂`. -/
lemma drop_append {l₁ l₂ : list α} (i : ℕ) :
drop (l₁.length + i) (l₁ ++ l₂) = drop i l₂ :=
by simp [drop_append_eq_append_drop, take_all_of_le le_self_add]
/-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by
dropping the first `i` elements. Version designed to rewrite from the big list to the small list. -/
lemma nth_le_drop (L : list α) {i j : ℕ} (h : i + j < L.length) :
nth_le L (i + j) h = nth_le (L.drop i) j
begin
have A : i < L.length := lt_of_le_of_lt (nat.le.intro rfl) h,
rw (take_append_drop i L).symm at h,
simpa only [le_of_lt A, min_eq_left, add_lt_add_iff_left, length_take, length_append] using h
end :=
begin
have A : length (take i L) = i, by simp [le_of_lt (lt_of_le_of_lt (nat.le.intro rfl) h)],
rw [nth_le_of_eq (take_append_drop i L).symm h, nth_le_append_right];
simp [A]
end
/-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by
dropping the first `i` elements. Version designed to rewrite from the small list to the big list. -/
lemma nth_le_drop' (L : list α) {i j : ℕ} (h : j < (L.drop i).length) :
nth_le (L.drop i) j h = nth_le L (i + j) (lt_tsub_iff_left.mp ((length_drop i L) ▸ h)) :=
by rw nth_le_drop
lemma nth_drop (L : list α) (i j : ℕ) :
nth (L.drop i) j = nth L (i + j) :=
begin
ext,
simp only [nth_eq_some, nth_le_drop', option.mem_def],
split;
exact λ ⟨h, ha⟩, ⟨by simpa [lt_tsub_iff_left] using h, ha⟩
end
@[simp] theorem drop_drop (n : ℕ) : ∀ (m) (l : list α), drop n (drop m l) = drop (n + m) l
| m [] := by simp
| 0 l := by simp
| (m+1) (a::l) :=
calc drop n (drop (m + 1) (a :: l)) = drop n (drop m l) : rfl
... = drop (n + m) l : drop_drop m l
... = drop (n + (m + 1)) (a :: l) : rfl
theorem drop_take : ∀ (m : ℕ) (n : ℕ) (l : list α),
drop m (take (m + n) l) = take n (drop m l)
| 0 n _ := by simp
| (m+1) n nil := by simp
| (m+1) n (_::l) :=
have h: m + 1 + n = (m+n) + 1, by ac_refl,
by simpa [take_cons, h] using drop_take m n l
lemma map_drop {α β : Type*} (f : α → β) :
∀ (L : list α) (i : ℕ), (L.drop i).map f = (L.map f).drop i
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [map_drop], }
theorem modify_nth_tail_eq_take_drop (f : list α → list α) (H : f [] = []) :
∀ n l, modify_nth_tail f n l = take n l ++ f (drop n l)
| 0 l := rfl
| (n+1) [] := H.symm
| (n+1) (b::l) := congr_arg (cons b) (modify_nth_tail_eq_take_drop n l)
theorem modify_nth_eq_take_drop (f : α → α) :
∀ n l, modify_nth f n l = take n l ++ modify_head f (drop n l) :=
modify_nth_tail_eq_take_drop _ rfl
theorem modify_nth_eq_take_cons_drop (f : α → α) {n l} (h) :
modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n+1) l :=
by rw [modify_nth_eq_take_drop, drop_eq_nth_le_cons h]; refl
theorem update_nth_eq_take_cons_drop (a : α) {n l} (h : n < length l) :
update_nth l n a = take n l ++ a :: drop (n+1) l :=
by rw [update_nth_eq_modify_nth, modify_nth_eq_take_cons_drop _ h]
lemma reverse_take {α} {xs : list α} (n : ℕ)
(h : n ≤ xs.length) :
xs.reverse.take n = (xs.drop (xs.length - n)).reverse :=
begin
induction xs generalizing n;
simp only [reverse_cons, drop, reverse_nil, zero_tsub, length, take_nil],
cases h.lt_or_eq_dec with h' h',
{ replace h' := le_of_succ_le_succ h',
rwa [take_append_of_le_length, xs_ih _ h'],
rw [show xs_tl.length + 1 - n = succ (xs_tl.length - n), from _, drop],
{ rwa [succ_eq_add_one, ← tsub_add_eq_add_tsub] },
{ rwa length_reverse } },
{ subst h', rw [length, tsub_self, drop],
suffices : xs_tl.length + 1 = (xs_tl.reverse ++ [xs_hd]).length,
by rw [this, take_length, reverse_cons],
rw [length_append, length_reverse], refl }
end
@[simp] lemma update_nth_eq_nil (l : list α) (n : ℕ) (a : α) : l.update_nth n a = [] ↔ l = [] :=
by cases l; cases n; simp only [update_nth]
section take'
variable [inhabited α]
@[simp] theorem take'_length : ∀ n l, length (@take' α _ n l) = n
| 0 l := rfl
| (n+1) l := congr_arg succ (take'_length _ _)
@[simp] theorem take'_nil : ∀ n, take' n (@nil α) = repeat (default _) n
| 0 := rfl
| (n+1) := congr_arg (cons _) (take'_nil _)
theorem take'_eq_take : ∀ {n} {l : list α},
n ≤ length l → take' n l = take n l
| 0 l h := rfl
| (n+1) (a::l) h := congr_arg (cons _) $
take'_eq_take $ le_of_succ_le_succ h
@[simp] theorem take'_left (l₁ l₂ : list α) : take' (length l₁) (l₁ ++ l₂) = l₁ :=
(take'_eq_take (by simp only [length_append, nat.le_add_right])).trans (take_left _ _)
theorem take'_left' {l₁ l₂ : list α} {n} (h : length l₁ = n) :
take' n (l₁ ++ l₂) = l₁ :=
by rw ← h; apply take'_left
end take'
/-! ### foldl, foldr -/
lemma foldl_ext (f g : α → β → α) (a : α)
{l : list β} (H : ∀ a : α, ∀ b ∈ l, f a b = g a b) :
foldl f a l = foldl g a l :=
begin
induction l with hd tl ih generalizing a, {refl},
unfold foldl,
rw [ih (λ a b bin, H a b $ mem_cons_of_mem _ bin), H a hd (mem_cons_self _ _)]
end
lemma foldr_ext (f g : α → β → β) (b : β)
{l : list α} (H : ∀ a ∈ l, ∀ b : β, f a b = g a b) :
foldr f b l = foldr g b l :=
begin
induction l with hd tl ih, {refl},
simp only [mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] at H,
simp only [foldr, ih H.2, H.1]
end
@[simp] theorem foldl_nil (f : α → β → α) (a : α) : foldl f a [] = a := rfl
@[simp] theorem foldl_cons (f : α → β → α) (a : α) (b : β) (l : list β) :
foldl f a (b::l) = foldl f (f a b) l := rfl
@[simp] theorem foldr_nil (f : α → β → β) (b : β) : foldr f b [] = b := rfl
@[simp] theorem foldr_cons (f : α → β → β) (b : β) (a : α) (l : list α) :
foldr f b (a::l) = f a (foldr f b l) := rfl
@[simp] theorem foldl_append (f : α → β → α) :
∀ (a : α) (l₁ l₂ : list β), foldl f a (l₁++l₂) = foldl f (foldl f a l₁) l₂
| a [] l₂ := rfl
| a (b::l₁) l₂ := by simp only [cons_append, foldl_cons, foldl_append (f a b) l₁ l₂]
@[simp] theorem foldr_append (f : α → β → β) :
∀ (b : β) (l₁ l₂ : list α), foldr f b (l₁++l₂) = foldr f (foldr f b l₂) l₁
| b [] l₂ := rfl
| b (a::l₁) l₂ := by simp only [cons_append, foldr_cons, foldr_append b l₁ l₂]
@[simp] theorem foldl_join (f : α → β → α) :
∀ (a : α) (L : list (list β)), foldl f a (join L) = foldl (foldl f) a L
| a [] := rfl
| a (l::L) := by simp only [join, foldl_append, foldl_cons, foldl_join (foldl f a l) L]
@[simp] theorem foldr_join (f : α → β → β) :
∀ (b : β) (L : list (list α)), foldr f b (join L) = foldr (λ l b, foldr f b l) b L
| a [] := rfl
| a (l::L) := by simp only [join, foldr_append, foldr_join a L, foldr_cons]
theorem foldl_reverse (f : α → β → α) (a : α) (l : list β) :
foldl f a (reverse l) = foldr (λx y, f y x) a l :=
by induction l; [refl, simp only [*, reverse_cons, foldl_append, foldl_cons, foldl_nil, foldr]]
theorem foldr_reverse (f : α → β → β) (a : β) (l : list α) :
foldr f a (reverse l) = foldl (λx y, f y x) a l :=
let t := foldl_reverse (λx y, f y x) a (reverse l) in
by rw reverse_reverse l at t; rwa t
@[simp] theorem foldr_eta : ∀ (l : list α), foldr cons [] l = l
| [] := rfl
| (x::l) := by simp only [foldr_cons, foldr_eta l]; split; refl
@[simp] theorem reverse_foldl {l : list α} : reverse (foldl (λ t h, h :: t) [] l) = l :=
by rw ←foldr_reverse; simp
@[simp] theorem foldl_map (g : β → γ) (f : α → γ → α) (a : α) (l : list β) :
foldl f a (map g l) = foldl (λx y, f x (g y)) a l :=
by revert a; induction l; intros; [refl, simp only [*, map, foldl]]
@[simp] theorem foldr_map (g : β → γ) (f : γ → α → α) (a : α) (l : list β) :
foldr f a (map g l) = foldr (f ∘ g) a l :=
by revert a; induction l; intros; [refl, simp only [*, map, foldr]]
theorem foldl_map' {α β: Type u} (g : α → β) (f : α → α → α) (f' : β → β → β)
(a : α) (l : list α) (h : ∀ x y, f' (g x) (g y) = g (f x y)) :
list.foldl f' (g a) (l.map g) = g (list.foldl f a l) :=
begin
induction l generalizing a,
{ simp }, { simp [l_ih, h] }
end
theorem foldr_map' {α β: Type u} (g : α → β) (f : α → α → α) (f' : β → β → β)
(a : α) (l : list α) (h : ∀ x y, f' (g x) (g y) = g (f x y)) :
list.foldr f' (g a) (l.map g) = g (list.foldr f a l) :=
begin
induction l generalizing a,
{ simp }, { simp [l_ih, h] }
end
theorem foldl_hom (l : list γ) (f : α → β) (op : α → γ → α) (op' : β → γ → β) (a : α)
(h : ∀a x, f (op a x) = op' (f a) x) : foldl op' (f a) l = f (foldl op a l) :=
eq.symm $ by { revert a, induction l; intros; [refl, simp only [*, foldl]] }
theorem foldr_hom (l : list γ) (f : α → β) (op : γ → α → α) (op' : γ → β → β) (a : α)
(h : ∀x a, f (op x a) = op' x (f a)) : foldr op' (f a) l = f (foldr op a l) :=
by { revert a, induction l; intros; [refl, simp only [*, foldr]] }
lemma injective_foldl_comp {α : Type*} {l : list (α → α)} {f : α → α}
(hl : ∀ f ∈ l, function.injective f) (hf : function.injective f):
function.injective (@list.foldl (α → α) (α → α) function.comp f l) :=
begin
induction l generalizing f,
{ exact hf },
{ apply l_ih (λ _ h, hl _ (list.mem_cons_of_mem _ h)),
apply function.injective.comp hf,
apply hl _ (list.mem_cons_self _ _) }
end
/-- Induction principle for values produced by a `foldr`: if a property holds
for the seed element `b : β` and for all incremental `op : α → β → β`
performed on the elements `(a : α) ∈ l`. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
def foldr_rec_on {C : β → Sort*} (l : list α) (op : α → β → β) (b : β) (hb : C b)
(hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ l), C (op a b)) :
C (foldr op b l) :=
begin
induction l with hd tl IH,
{ exact hb },
{ refine hl _ _ hd (mem_cons_self hd tl),
refine IH _,
intros y hy x hx,
exact hl y hy x (mem_cons_of_mem hd hx) }
end
/-- Induction principle for values produced by a `foldl`: if a property holds
for the seed element `b : β` and for all incremental `op : β → α → β`
performed on the elements `(a : α) ∈ l`. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
def foldl_rec_on {C : β → Sort*} (l : list α) (op : β → α → β) (b : β) (hb : C b)
(hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ l), C (op b a)) :
C (foldl op b l) :=
begin
induction l with hd tl IH generalizing b,
{ exact hb },
{ refine IH _ _ _,
{ intros y hy x hx,
exact hl y hy x (mem_cons_of_mem hd hx) },
{ exact hl b hb hd (mem_cons_self hd tl) } }
end
@[simp] lemma foldr_rec_on_nil {C : β → Sort*} (op : α → β → β) (b) (hb : C b) (hl) :
foldr_rec_on [] op b hb hl = hb := rfl
@[simp] lemma foldr_rec_on_cons {C : β → Sort*} (x : α) (l : list α)
(op : α → β → β) (b) (hb : C b)
(hl : ∀ (b : β) (hb : C b) (a : α) (ha : a ∈ (x :: l)), C (op a b)) :
foldr_rec_on (x :: l) op b hb hl = hl _ (foldr_rec_on l op b hb
(λ b hb a ha, hl b hb a (mem_cons_of_mem _ ha))) x (mem_cons_self _ _) := rfl
@[simp] lemma foldl_rec_on_nil {C : β → Sort*} (op : β → α → β) (b) (hb : C b) (hl) :
foldl_rec_on [] op b hb hl = hb := rfl
/- scanl -/
section scanl
variables {f : β → α → β} {b : β} {a : α} {l : list α}
lemma length_scanl :
∀ a l, length (scanl f a l) = l.length + 1
| a [] := rfl
| a (x :: l) := by erw [length_cons, length_cons, length_scanl]
@[simp] lemma scanl_nil (b : β) : scanl f b nil = [b] := rfl
@[simp] lemma scanl_cons :
scanl f b (a :: l) = [b] ++ scanl f (f b a) l :=
by simp only [scanl, eq_self_iff_true, singleton_append, and_self]
@[simp] lemma nth_zero_scanl : (scanl f b l).nth 0 = some b :=
begin
cases l,
{ simp only [nth, scanl_nil] },
{ simp only [nth, scanl_cons, singleton_append] }
end
@[simp] lemma nth_le_zero_scanl {h : 0 < (scanl f b l).length} :
(scanl f b l).nth_le 0 h = b :=
begin
cases l,
{ simp only [nth_le, scanl_nil] },
{ simp only [nth_le, scanl_cons, singleton_append] }
end
lemma nth_succ_scanl {i : ℕ} :
(scanl f b l).nth (i + 1) = ((scanl f b l).nth i).bind (λ x, (l.nth i).map (λ y, f x y)) :=
begin
induction l with hd tl hl generalizing b i,
{ symmetry,
simp only [option.bind_eq_none', nth, forall_2_true_iff, not_false_iff, option.map_none',
scanl_nil, option.not_mem_none, forall_true_iff] },
{ simp only [nth, scanl_cons, singleton_append],
cases i,
{ simp only [option.map_some', nth_zero_scanl, nth, option.some_bind'] },
{ simp only [hl, nth] } }
end
lemma nth_le_succ_scanl {i : ℕ} {h : i + 1 < (scanl f b l).length} :
(scanl f b l).nth_le (i + 1) h =
f ((scanl f b l).nth_le i (nat.lt_of_succ_lt h))
(l.nth_le i (nat.lt_of_succ_lt_succ (lt_of_lt_of_le h (le_of_eq (length_scanl b l))))) :=
begin
induction i with i hi generalizing b l,
{ cases l,
{ simp only [length, zero_add, scanl_nil] at h,
exact absurd h (lt_irrefl 1) },
{ simp only [scanl_cons, singleton_append, nth_le_zero_scanl, nth_le] } },
{ cases l,
{ simp only [length, add_lt_iff_neg_right, scanl_nil] at h,
exact absurd h (not_lt_of_lt nat.succ_pos') },
{ simp_rw scanl_cons,
rw nth_le_append_right _,
{ simpa only [hi, length, succ_add_sub_one] },
{ simp only [length, nat.zero_le, le_add_iff_nonneg_left] } } }
end
end scanl
/- scanr -/
@[simp] theorem scanr_nil (f : α → β → β) (b : β) : scanr f b [] = [b] := rfl
@[simp] theorem scanr_aux_cons (f : α → β → β) (b : β) : ∀ (a : α) (l : list α),
scanr_aux f b (a::l) = (foldr f b (a::l), scanr f b l)
| a [] := rfl
| a (x::l) := let t := scanr_aux_cons x l in
by simp only [scanr, scanr_aux, t, foldr_cons]
@[simp] theorem scanr_cons (f : α → β → β) (b : β) (a : α) (l : list α) :
scanr f b (a::l) = foldr f b (a::l) :: scanr f b l :=
by simp only [scanr, scanr_aux_cons, foldr_cons]; split; refl
section foldl_eq_foldr
-- foldl and foldr coincide when f is commutative and associative
variables {f : α → α → α} (hcomm : commutative f) (hassoc : associative f)
include hassoc
theorem foldl1_eq_foldr1 : ∀ a b l, foldl f a (l++[b]) = foldr f b (a::l)
| a b nil := rfl
| a b (c :: l) :=
by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l]; rw hassoc
include hcomm
theorem foldl_eq_of_comm_of_assoc : ∀ a b l, foldl f a (b::l) = f b (foldl f a l)
| a b nil := hcomm a b
| a b (c::l) := by simp only [foldl_cons];
rw [← foldl_eq_of_comm_of_assoc, right_comm _ hcomm hassoc]; refl
theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l
| a nil := rfl
| a (b :: l) :=
by simp only [foldr_cons, foldl_eq_of_comm_of_assoc hcomm hassoc]; rw (foldl_eq_foldr a l)
end foldl_eq_foldr
section foldl_eq_foldlr'
variables {f : α → β → α}
variables hf : ∀ a b c, f (f a b) c = f (f a c) b
include hf
theorem foldl_eq_of_comm' : ∀ a b l, foldl f a (b::l) = f (foldl f a l) b
| a b [] := rfl
| a b (c :: l) := by rw [foldl,foldl,foldl,← foldl_eq_of_comm',foldl,hf]
theorem foldl_eq_foldr' : ∀ a l, foldl f a l = foldr (flip f) a l
| a [] := rfl
| a (b :: l) := by rw [foldl_eq_of_comm' hf,foldr,foldl_eq_foldr']; refl
end foldl_eq_foldlr'
section foldl_eq_foldlr'
variables {f : α → β → β}
variables hf : ∀ a b c, f a (f b c) = f b (f a c)
include hf
theorem foldr_eq_of_comm' : ∀ a b l, foldr f a (b::l) = foldr f (f b a) l
| a b [] := rfl
| a b (c :: l) := by rw [foldr,foldr,foldr,hf,← foldr_eq_of_comm']; refl
end foldl_eq_foldlr'
section
variables {op : α → α → α} [ha : is_associative α op] [hc : is_commutative α op]
local notation a * b := op a b
local notation l <*> a := foldl op a l
include ha
lemma foldl_assoc : ∀ {l : list α} {a₁ a₂}, l <*> (a₁ * a₂) = a₁ * (l <*> a₂)
| [] a₁ a₂ := rfl
| (a :: l) a₁ a₂ :=
calc a::l <*> (a₁ * a₂) = l <*> (a₁ * (a₂ * a)) : by simp only [foldl_cons, ha.assoc]
... = a₁ * (a::l <*> a₂) : by rw [foldl_assoc, foldl_cons]
lemma foldl_op_eq_op_foldr_assoc : ∀{l : list α} {a₁ a₂}, (l <*> a₁) * a₂ = a₁ * l.foldr (*) a₂
| [] a₁ a₂ := rfl
| (a :: l) a₁ a₂ := by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc];
rw [foldl_op_eq_op_foldr_assoc]
include hc
lemma foldl_assoc_comm_cons {l : list α} {a₁ a₂} : (a₁ :: l) <*> a₂ = a₁ * (l <*> a₂) :=
by rw [foldl_cons, hc.comm, foldl_assoc]
end
/-! ### mfoldl, mfoldr, mmap -/
section mfoldl_mfoldr
variables {m : Type v → Type w} [monad m]
@[simp] theorem mfoldl_nil (f : β → α → m β) {b} : mfoldl f b [] = pure b := rfl
@[simp] theorem mfoldr_nil (f : α → β → m β) {b} : mfoldr f b [] = pure b := rfl
@[simp] theorem mfoldl_cons {f : β → α → m β} {b a l} :
mfoldl f b (a :: l) = f b a >>= λ b', mfoldl f b' l := rfl
@[simp] theorem mfoldr_cons {f : α → β → m β} {b a l} :
mfoldr f b (a :: l) = mfoldr f b l >>= f a := rfl
theorem mfoldr_eq_foldr (f : α → β → m β) (b l) :
mfoldr f b l = foldr (λ a mb, mb >>= f a) (pure b) l :=
by induction l; simp *
attribute [simp] mmap mmap'
variables [is_lawful_monad m]
theorem mfoldl_eq_foldl (f : β → α → m β) (b l) :
mfoldl f b l = foldl (λ mb a, mb >>= λ b, f b a) (pure b) l :=
begin
suffices h : ∀ (mb : m β),
(mb >>= λ b, mfoldl f b l) = foldl (λ mb a, mb >>= λ b, f b a) mb l,
by simp [←h (pure b)],
induction l; intro,
{ simp },
{ simp only [mfoldl, foldl, ←l_ih] with monad_norm }
end
@[simp] theorem mfoldl_append {f : β → α → m β} : ∀ {b l₁ l₂},
mfoldl f b (l₁ ++ l₂) = mfoldl f b l₁ >>= λ x, mfoldl f x l₂
| _ [] _ := by simp only [nil_append, mfoldl_nil, pure_bind]
| _ (_::_) _ := by simp only [cons_append, mfoldl_cons, mfoldl_append, is_lawful_monad.bind_assoc]
@[simp] theorem mfoldr_append {f : α → β → m β} : ∀ {b l₁ l₂},
mfoldr f b (l₁ ++ l₂) = mfoldr f b l₂ >>= λ x, mfoldr f x l₁
| _ [] _ := by simp only [nil_append, mfoldr_nil, bind_pure]
| _ (_::_) _ := by simp only [mfoldr_cons, cons_append, mfoldr_append, is_lawful_monad.bind_assoc]
end mfoldl_mfoldr
/-! ### prod and sum -/
-- list.sum was already defined in defs.lean, but we couldn't tag it with `to_additive` yet.
attribute [to_additive] list.prod
section monoid
variables [monoid α] {l l₁ l₂ : list α} {a : α}
@[simp, to_additive]
theorem prod_nil : ([] : list α).prod = 1 := rfl
@[to_additive]
theorem prod_singleton : [a].prod = a := one_mul a
@[simp, to_additive]
theorem prod_cons : (a::l).prod = a * l.prod :=
calc (a::l).prod = foldl (*) (a * 1) l : by simp only [list.prod, foldl_cons, one_mul, mul_one]
... = _ : foldl_assoc
@[simp, to_additive]
theorem prod_append : (l₁ ++ l₂).prod = l₁.prod * l₂.prod :=
calc (l₁ ++ l₂).prod = foldl (*) (foldl (*) 1 l₁ * 1) l₂ : by simp [list.prod]
... = l₁.prod * l₂.prod : foldl_assoc
@[to_additive]
theorem prod_concat : (l.concat a).prod = l.prod * a :=
by rw [concat_eq_append, prod_append, prod_cons, prod_nil, mul_one]
@[simp, to_additive]
theorem prod_join {l : list (list α)} : l.join.prod = (l.map list.prod).prod :=
by induction l; [refl, simp only [*, list.join, map, prod_append, prod_cons]]
/-- If zero is an element of a list `L`, then `list.prod L = 0`. If the domain is a nontrivial
monoid with zero with no divisors, then this implication becomes an `iff`, see
`list.prod_eq_zero_iff`. -/
theorem prod_eq_zero {M₀ : Type*} [monoid_with_zero M₀] {L : list M₀} (h : (0 : M₀) ∈ L) :
L.prod = 0 :=
begin
induction L with a L ihL,
{ exact absurd h (not_mem_nil _) },
{ rw prod_cons,
cases (mem_cons_iff _ _ _).1 h with ha hL,
exacts [mul_eq_zero_of_left ha.symm _, mul_eq_zero_of_right _ (ihL hL)] }
end
/-- Product of elements of a list `L` equals zero if and only if `0 ∈ L`. See also
`list.prod_eq_zero` for an implication that needs weaker typeclass assumptions. -/
@[simp] theorem prod_eq_zero_iff {M₀ : Type*} [monoid_with_zero M₀] [nontrivial M₀]
[no_zero_divisors M₀] {L : list M₀} :
L.prod = 0 ↔ (0 : M₀) ∈ L :=
begin
induction L with a L ihL,
{ simp },
{ rw [prod_cons, mul_eq_zero, ihL, mem_cons_iff, eq_comm] }
end
theorem prod_ne_zero {M₀ : Type*} [monoid_with_zero M₀] [nontrivial M₀] [no_zero_divisors M₀]
{L : list M₀} (hL : (0 : M₀) ∉ L) : L.prod ≠ 0 :=
mt prod_eq_zero_iff.1 hL
@[to_additive]
theorem prod_eq_foldr : l.prod = foldr (*) 1 l :=
list.rec_on l rfl $ λ a l ihl, by rw [prod_cons, foldr_cons, ihl]
@[to_additive]
theorem prod_hom_rel {α β γ : Type*} [monoid β] [monoid γ] (l : list α) {r : β → γ → Prop}
{f : α → β} {g : α → γ} (h₁ : r 1 1) (h₂ : ∀⦃a b c⦄, r b c → r (f a * b) (g a * c)) :
r (l.map f).prod (l.map g).prod :=
list.rec_on l h₁ (λ a l hl, by simp only [map_cons, prod_cons, h₂ hl])
@[to_additive]
theorem prod_hom [monoid β] (l : list α) (f : α →* β) :
(l.map f).prod = f l.prod :=
by { simp only [prod, foldl_map, f.map_one.symm],
exact l.foldl_hom _ _ _ 1 f.map_mul }
@[to_additive]
lemma prod_is_unit [monoid β] : Π {L : list β} (u : ∀ m ∈ L, is_unit m), is_unit L.prod
| [] _ := by simp
| (h :: t) u :=
begin
simp only [list.prod_cons],
exact is_unit.mul (u h (mem_cons_self h t)) (prod_is_unit (λ m mt, u m (mem_cons_of_mem h mt)))
end
@[simp, to_additive]
lemma prod_take_mul_prod_drop :
∀ (L : list α) (i : ℕ), (L.take i).prod * (L.drop i).prod = L.prod
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [prod_cons, prod_cons, mul_assoc, prod_take_mul_prod_drop], }
@[simp, to_additive]
lemma prod_take_succ :
∀ (L : list α) (i : ℕ) (p), (L.take (i + 1)).prod = (L.take i).prod * L.nth_le i p
| [] i p := by cases p
| (h :: t) 0 _ := by simp
| (h :: t) (n+1) _ := by { dsimp, rw [prod_cons, prod_cons, prod_take_succ, mul_assoc], }
/-- A list with product not one must have positive length. -/
@[to_additive]
lemma length_pos_of_prod_ne_one (L : list α) (h : L.prod ≠ 1) : 0 < L.length :=
by { cases L, { simp at h, cases h, }, { simp, }, }
@[to_additive]
lemma prod_update_nth : ∀ (L : list α) (n : ℕ) (a : α),
(L.update_nth n a).prod =
(L.take n).prod * (if n < L.length then a else 1) * (L.drop (n + 1)).prod
| (x::xs) 0 a := by simp [update_nth]
| (x::xs) (i+1) a := by simp [update_nth, prod_update_nth xs i a, mul_assoc]
| [] _ _ := by simp [update_nth, (nat.zero_le _).not_lt]
open opposite
lemma _root_.opposite.op_list_prod : ∀ (l : list α), op (l.prod) = (l.map op).reverse.prod
| [] := rfl
| (x :: xs) := by rw [list.prod_cons, list.map_cons, list.reverse_cons', list.prod_concat, op_mul,
_root_.opposite.op_list_prod]
lemma _root_.opposite.unop_list_prod : ∀ (l : list αᵒᵖ), (l.prod).unop = (l.map unop).reverse.prod
| [] := rfl
| (x :: xs) := by rw [list.prod_cons, list.map_cons, list.reverse_cons', list.prod_concat, unop_mul,
_root_.opposite.unop_list_prod]
end monoid
section group
variables [group α]
/-- This is the `list.prod` version of `mul_inv_rev` -/
@[to_additive "This is the `list.sum` version of `add_neg_rev`"]
lemma prod_inv_reverse : ∀ (L : list α), L.prod⁻¹ = (L.map (λ x, x⁻¹)).reverse.prod
| [] := by simp
| (x :: xs) := by simp [prod_inv_reverse xs]
/-- A non-commutative variant of `list.prod_reverse` -/
@[to_additive "A non-commutative variant of `list.sum_reverse`"]
lemma prod_reverse_noncomm : ∀ (L : list α), L.reverse.prod = (L.map (λ x, x⁻¹)).prod⁻¹ :=
by simp [prod_inv_reverse]
/-- Counterpart to `list.prod_take_succ` when we have an inverse operation -/
@[simp, to_additive /-"Counterpart to `list.sum_take_succ` when we have an negation operation"-/]
lemma prod_drop_succ :
∀ (L : list α) (i : ℕ) (p), (L.drop (i + 1)).prod = (L.nth_le i p)⁻¹ * (L.drop i).prod
| [] i p := false.elim (nat.not_lt_zero _ p)
| (x :: xs) 0 p := by simp
| (x :: xs) (i + 1) p := prod_drop_succ xs i _
end group
section comm_group
variables [comm_group α]
/-- This is the `list.prod` version of `mul_inv` -/
@[to_additive "This is the `list.sum` version of `add_neg`"]
lemma prod_inv : ∀ (L : list α), L.prod⁻¹ = (L.map (λ x, x⁻¹)).prod
| [] := by simp
| (x :: xs) := by simp [mul_comm, prod_inv xs]
/-- Alternative version of `list.prod_update_nth` when the list is over a group -/
@[to_additive /-"Alternative version of `list.sum_update_nth` when the list is over a group"-/]
lemma prod_update_nth' (L : list α) (n : ℕ) (a : α) :
(L.update_nth n a).prod =
L.prod * (if hn : n < L.length then (L.nth_le n hn)⁻¹ * a else 1) :=
begin
refine (prod_update_nth L n a).trans _,
split_ifs with hn hn,
{ rw [mul_comm _ a, mul_assoc a, prod_drop_succ L n hn, mul_comm _ (drop n L).prod,
← mul_assoc (take n L).prod, prod_take_mul_prod_drop, mul_comm a, mul_assoc] },
{ simp only [take_all_of_le (le_of_not_lt hn), prod_nil, mul_one,
drop_eq_nil_of_le ((le_of_not_lt hn).trans n.le_succ)] }
end
end comm_group
lemma eq_of_sum_take_eq [add_left_cancel_monoid α] {L L' : list α} (h : L.length = L'.length)
(h' : ∀ i ≤ L.length, (L.take i).sum = (L'.take i).sum) : L = L' :=
begin
apply ext_le h (λ i h₁ h₂, _),
have : (L.take (i + 1)).sum = (L'.take (i + 1)).sum := h' _ (nat.succ_le_of_lt h₁),
rw [sum_take_succ L i h₁, sum_take_succ L' i h₂, h' i (le_of_lt h₁)] at this,
exact add_left_cancel this
end
lemma monotone_sum_take [canonically_ordered_add_monoid α] (L : list α) :
monotone (λ i, (L.take i).sum) :=
begin
apply monotone_nat_of_le_succ (λ n, _),
by_cases h : n < L.length,
{ rw sum_take_succ _ _ h,
exact le_self_add },
{ push_neg at h,
simp [take_all_of_le h, take_all_of_le (le_trans h (nat.le_succ _))] }
end
@[to_additive sum_nonneg]
lemma one_le_prod_of_one_le [ordered_comm_monoid α] {l : list α} (hl₁ : ∀ x ∈ l, (1 : α) ≤ x) :
1 ≤ l.prod :=
begin
induction l with hd tl ih,
{ simp },
rw prod_cons,
exact one_le_mul (hl₁ hd (mem_cons_self hd tl)) (ih (λ x h, hl₁ x (mem_cons_of_mem hd h))),
end
@[to_additive]
lemma single_le_prod [ordered_comm_monoid α] {l : list α} (hl₁ : ∀ x ∈ l, (1 : α) ≤ x) :
∀ x ∈ l, x ≤ l.prod :=
begin
induction l,
{ simp },
simp_rw [prod_cons, forall_mem_cons] at ⊢ hl₁,
split,
{ exact le_mul_of_one_le_right' (one_le_prod_of_one_le hl₁.2) },
{ exact λ x H, le_mul_of_one_le_of_le hl₁.1 (l_ih hl₁.right x H) },
end
@[to_additive all_zero_of_le_zero_le_of_sum_eq_zero]
lemma all_one_of_le_one_le_of_prod_eq_one [ordered_comm_monoid α]
{l : list α} (hl₁ : ∀ x ∈ l, (1 : α) ≤ x) (hl₂ : l.prod = 1) :
∀ x ∈ l, x = (1 : α) :=
λ x hx, le_antisymm (hl₂ ▸ single_le_prod hl₁ _ hx) (hl₁ x hx)
lemma sum_eq_zero_iff [canonically_ordered_add_monoid α] (l : list α) :
l.sum = 0 ↔ ∀ x ∈ l, x = (0 : α) :=
⟨all_zero_of_le_zero_le_of_sum_eq_zero (λ _ _, zero_le _),
begin
induction l,
{ simp },
{ intro h,
rw [sum_cons, add_eq_zero_iff],
rw forall_mem_cons at h,
exact ⟨h.1, l_ih h.2⟩ },
end⟩
/-- If all elements in a list are bounded below by `1`, then the length of the list is bounded
by the sum of the elements. -/
lemma length_le_sum_of_one_le (L : list ℕ) (h : ∀ i ∈ L, 1 ≤ i) : L.length ≤ L.sum :=
begin
induction L with j L IH h, { simp },
rw [sum_cons, length, add_comm],
exact add_le_add (h _ (set.mem_insert _ _)) (IH (λ i hi, h i (set.mem_union_right _ hi)))
end
/-- A list with positive sum must have positive length. -/
-- This is an easy consequence of `length_pos_of_sum_ne_zero`, but often useful in applications.
lemma length_pos_of_sum_pos [ordered_cancel_add_comm_monoid α] (L : list α) (h : 0 < L.sum) :
0 < L.length :=
length_pos_of_sum_ne_zero L (ne_of_gt h)
-- TODO: develop theory of tropical rings
lemma sum_le_foldr_max [add_monoid α] [add_monoid β] [linear_order β] (f : α → β)
(h0 : f 0 ≤ 0) (hadd : ∀ x y, f (x + y) ≤ max (f x) (f y)) (l : list α) :
f l.sum ≤ (l.map f).foldr max 0 :=
begin
induction l with hd tl IH,
{ simpa using h0 },
{ simp only [list.sum_cons, list.foldr_map, le_max_iff, list.foldr] at IH ⊢,
cases le_or_lt (f tl.sum) (f hd),
{ left,
refine (hadd _ _).trans _,
simpa using h },
{ right,
refine (hadd _ _).trans _,
simp only [IH, max_le_iff, and_true, h.le.trans IH] } }
end
@[simp, to_additive]
theorem prod_erase [decidable_eq α] [comm_monoid α] {a} :
Π {l : list α}, a ∈ l → a * (l.erase a).prod = l.prod
| (b::l) h :=
begin
rcases decidable.list.eq_or_ne_mem_of_mem h with rfl | ⟨ne, h⟩,
{ simp only [list.erase, if_pos, prod_cons] },
{ simp only [list.erase, if_neg (mt eq.symm ne), prod_cons, prod_erase h, mul_left_comm a b] }
end
lemma dvd_prod [comm_monoid α] {a} {l : list α} (ha : a ∈ l) : a ∣ l.prod :=
let ⟨s, t, h⟩ := mem_split ha in
by rw [h, prod_append, prod_cons, mul_left_comm]; exact dvd_mul_right _ _
@[simp] theorem sum_const_nat (m n : ℕ) : sum (list.repeat m n) = m * n :=
by induction n; [refl, simp only [*, repeat_succ, sum_cons, nat.mul_succ, add_comm]]
theorem dvd_sum [comm_semiring α] {a} {l : list α} (h : ∀ x ∈ l, a ∣ x) : a ∣ l.sum :=
begin
induction l with x l ih,
{ exact dvd_zero _ },
{ rw [list.sum_cons],
exact dvd_add (h _ (mem_cons_self _ _)) (ih (λ x hx, h x (mem_cons_of_mem _ hx))) }
end
@[simp] theorem length_join (L : list (list α)) : length (join L) = sum (map length L) :=
by induction L; [refl, simp only [*, join, map, sum_cons, length_append]]
@[simp] theorem length_bind (l : list α) (f : α → list β) :
length (list.bind l f) = sum (map (length ∘ f) l) :=
by rw [list.bind, length_join, map_map]
lemma exists_lt_of_sum_lt [linear_ordered_cancel_add_comm_monoid β] {l : list α}
(f g : α → β) (h : (l.map f).sum < (l.map g).sum) : ∃ x ∈ l, f x < g x :=
begin
induction l with x l,
{ exfalso, exact lt_irrefl _ h },
{ by_cases h' : f x < g x, exact ⟨x, mem_cons_self _ _, h'⟩,
rcases l_ih _ with ⟨y, h1y, h2y⟩, refine ⟨y, mem_cons_of_mem x h1y, h2y⟩, simp at h,
exact lt_of_add_lt_add_left (lt_of_lt_of_le h $ add_le_add_right (le_of_not_gt h') _) }
end
lemma exists_le_of_sum_le [linear_ordered_cancel_add_comm_monoid β] {l : list α}
(hl : l ≠ []) (f g : α → β) (h : (l.map f).sum ≤ (l.map g).sum) : ∃ x ∈ l, f x ≤ g x :=
begin
cases l with x l,
{ contradiction },
{ by_cases h' : f x ≤ g x, exact ⟨x, mem_cons_self _ _, h'⟩,
rcases exists_lt_of_sum_lt f g _ with ⟨y, h1y, h2y⟩,
exact ⟨y, mem_cons_of_mem x h1y, le_of_lt h2y⟩, simp at h,
exact lt_of_add_lt_add_left (lt_of_le_of_lt h $ add_lt_add_right (lt_of_not_ge h') _) }
end
/--
We'd like to state this as `L.head * L.tail.prod = L.prod`,
but because `L.head` relies on an inhabited instances and
returns a garbage value for the empty list, this is not possible.
Instead we write the statement in terms of `(L.nth 0).get_or_else 1`,
and below, restate the lemma just for `ℕ`.
-/
@[to_additive]
lemma nth_zero_mul_tail_prod [monoid α] (L : list α) :
(L.nth 0).get_or_else 1 * L.tail.prod = L.prod :=
by cases L; simp
/-- In the case where the list is not empty the above complication can be avoided. -/
@[to_additive]
lemma head_mul_tail_prod_of_ne_nil [monoid α] [inhabited α] (L : list α) (h: L ≠ []) :
L.head * L.tail.prod = L.prod :=
by {cases L, { contradiction }, { simp }}
/-- The product of a list of positive natural numbers is positive,
and likewise for any nontrivial ordered semiring. -/
lemma prod_pos {S : Type} [ordered_semiring S] [nontrivial S] (L : list S)
(h: ∀ n:S, n ∈ L → 0 < n) : 0 < L.prod :=
begin
induction L, {simp},
{ simp, apply mul_pos,
{ apply h, simp },
{ exact L_ih (λ n hn, h n (mem_cons_of_mem L_hd hn)) }}
end
/-!
Several lemmas about sum/head/tail for `list ℕ`.
These are hard to generalize well, as they rely on the fact that `default ℕ = 0`.
If desired, we could add a class stating that `default α = 0` at some point
(extending `inhabited` and `has_zero`).
-/
/-- This relies on `default ℕ = 0`. -/
lemma head_add_tail_sum (L : list ℕ) : L.head + L.tail.sum = L.sum :=
by { cases L, { simp, refl, }, { simp, }, }
/-- This relies on `default ℕ = 0`. -/
lemma head_le_sum (L : list ℕ) : L.head ≤ L.sum :=
nat.le.intro (head_add_tail_sum L)
/-- This relies on `default ℕ = 0`. -/
lemma tail_sum (L : list ℕ) : L.tail.sum = L.sum - L.head :=
by rw [← head_add_tail_sum L, add_comm, add_tsub_cancel_right]
section
variables {G : Type*} [comm_group G]
attribute [to_additive] alternating_prod
@[simp, to_additive] lemma alternating_prod_nil :
alternating_prod ([] : list G) = 1 := rfl
@[simp, to_additive] lemma alternating_prod_singleton (g : G) :
alternating_prod [g] = g := rfl
@[simp, to_additive alternating_sum_cons_cons']
lemma alternating_prod_cons_cons (g h : G) (l : list G) :
alternating_prod (g :: h :: l) = g * h⁻¹ * alternating_prod l := rfl
lemma alternating_sum_cons_cons {G : Type*} [add_comm_group G] (g h : G) (l : list G) :
alternating_sum (g :: h :: l) = g - h + alternating_sum l :=
by rw [sub_eq_add_neg, alternating_sum]
end
/-! ### join -/
attribute [simp] join
@[simp] lemma join_nil {α : Type u} : [([] : list α)].join = [] := rfl
@[simp] theorem join_eq_nil : ∀ {L : list (list α)}, join L = [] ↔ ∀ l ∈ L, l = []
| [] := iff_of_true rfl (forall_mem_nil _)
| (l::L) := by simp only [join, append_eq_nil, join_eq_nil, forall_mem_cons]
@[simp] theorem join_append (L₁ L₂ : list (list α)) : join (L₁ ++ L₂) = join L₁ ++ join L₂ :=
by induction L₁; [refl, simp only [*, join, cons_append, append_assoc]]
@[simp] theorem join_filter_empty_eq_ff [decidable_pred (λ l : list α, l.empty = ff)] :
∀ {L : list (list α)}, join (L.filter (λ l, l.empty = ff)) = L.join
| [] := rfl
| ([]::L) := by simp [@join_filter_empty_eq_ff L]
| ((a::l)::L) := by simp [@join_filter_empty_eq_ff L]
@[simp] theorem join_filter_ne_nil [decidable_pred (λ l : list α, l ≠ [])] {L : list (list α)} :
join (L.filter (λ l, l ≠ [])) = L.join :=
by simp [join_filter_empty_eq_ff, ← empty_iff_eq_nil]
lemma join_join (l : list (list (list α))) : l.join.join = (l.map join).join :=
by { induction l, simp, simp [l_ih] }
/-- In a join, taking the first elements up to an index which is the sum of the lengths of the
first `i` sublists, is the same as taking the join of the first `i` sublists. -/
lemma take_sum_join (L : list (list α)) (i : ℕ) :
L.join.take ((L.map length).take i).sum = (L.take i).join :=
begin
induction L generalizing i, { simp },
cases i, { simp },
simp [take_append, L_ih]
end
/-- In a join, dropping all the elements up to an index which is the sum of the lengths of the
first `i` sublists, is the same as taking the join after dropping the first `i` sublists. -/
lemma drop_sum_join (L : list (list α)) (i : ℕ) :
L.join.drop ((L.map length).take i).sum = (L.drop i).join :=
begin
induction L generalizing i, { simp },
cases i, { simp },
simp [drop_append, L_ih],
end
/-- Taking only the first `i+1` elements in a list, and then dropping the first `i` ones, one is
left with a list of length `1` made of the `i`-th element of the original list. -/
lemma drop_take_succ_eq_cons_nth_le (L : list α) {i : ℕ} (hi : i < L.length) :
(L.take (i+1)).drop i = [nth_le L i hi] :=
begin
induction L generalizing i,
{ simp only [length] at hi, exact (nat.not_succ_le_zero i hi).elim },
cases i, { simp },
have : i < L_tl.length,
{ simp at hi,
exact nat.lt_of_succ_lt_succ hi },
simp [L_ih this],
refl
end
/-- In a join of sublists, taking the slice between the indices `A` and `B - 1` gives back the
original sublist of index `i` if `A` is the sum of the lenghts of sublists of index `< i`, and
`B` is the sum of the lengths of sublists of index `≤ i`. -/
lemma drop_take_succ_join_eq_nth_le (L : list (list α)) {i : ℕ} (hi : i < L.length) :
(L.join.take ((L.map length).take (i+1)).sum).drop ((L.map length).take i).sum = nth_le L i hi :=
begin
have : (L.map length).take i = ((L.take (i+1)).map length).take i, by simp [map_take, take_take],
simp [take_sum_join, this, drop_sum_join, drop_take_succ_eq_cons_nth_le _ hi]
end
/-- Auxiliary lemma to control elements in a join. -/
lemma sum_take_map_length_lt1 (L : list (list α)) {i j : ℕ}
(hi : i < L.length) (hj : j < (nth_le L i hi).length) :
((L.map length).take i).sum + j < ((L.map length).take (i+1)).sum :=
by simp [hi, sum_take_succ, hj]
/-- Auxiliary lemma to control elements in a join. -/
lemma sum_take_map_length_lt2 (L : list (list α)) {i j : ℕ}
(hi : i < L.length) (hj : j < (nth_le L i hi).length) :
((L.map length).take i).sum + j < L.join.length :=
begin
convert lt_of_lt_of_le (sum_take_map_length_lt1 L hi hj) (monotone_sum_take _ hi),
have : L.length = (L.map length).length, by simp,
simp [this, -length_map]
end
/-- The `n`-th element in a join of sublists is the `j`-th element of the `i`th sublist,
where `n` can be obtained in terms of `i` and `j` by adding the lengths of all the sublists
of index `< i`, and adding `j`. -/
lemma nth_le_join (L : list (list α)) {i j : ℕ}
(hi : i < L.length) (hj : j < (nth_le L i hi).length) :
nth_le L.join (((L.map length).take i).sum + j) (sum_take_map_length_lt2 L hi hj) =
nth_le (nth_le L i hi) j hj :=
by rw [nth_le_take L.join (sum_take_map_length_lt2 L hi hj) (sum_take_map_length_lt1 L hi hj),
nth_le_drop, nth_le_of_eq (drop_take_succ_join_eq_nth_le L hi)]
/-- Two lists of sublists are equal iff their joins coincide, as well as the lengths of the
sublists. -/
theorem eq_iff_join_eq (L L' : list (list α)) :
L = L' ↔ L.join = L'.join ∧ map length L = map length L' :=
begin
refine ⟨λ H, by simp [H], _⟩,
rintros ⟨join_eq, length_eq⟩,
apply ext_le,
{ have : length (map length L) = length (map length L'), by rw length_eq,
simpa using this },
{ assume n h₁ h₂,
rw [← drop_take_succ_join_eq_nth_le, ← drop_take_succ_join_eq_nth_le, join_eq, length_eq] }
end
/-! ### intersperse -/
@[simp] lemma intersperse_nil {α : Type u} (a : α) : intersperse a [] = [] := rfl
@[simp] lemma intersperse_singleton {α : Type u} (a b : α) : intersperse a [b] = [b] := rfl
@[simp] lemma intersperse_cons_cons {α : Type u} (a b c : α) (tl : list α) :
intersperse a (b :: c :: tl) = b :: a :: intersperse a (c :: tl) := rfl
/-! ### split_at and split_on -/
@[simp] theorem split_at_eq_take_drop : ∀ (n : ℕ) (l : list α), split_at n l = (take n l, drop n l)
| 0 a := rfl
| (succ n) [] := rfl
| (succ n) (x :: xs) := by simp only [split_at, split_at_eq_take_drop n xs, take, drop]
@[simp] lemma split_on_nil {α : Type u} [decidable_eq α] (a : α) : [].split_on a = [[]] := rfl
/-- An auxiliary definition for proving a specification lemma for `split_on_p`.
`split_on_p_aux' P xs ys` splits the list `ys ++ xs` at every element satisfying `P`,
where `ys` is an accumulating parameter for the initial segment of elements not satisfying `P`.
-/
def split_on_p_aux' {α : Type u} (P : α → Prop) [decidable_pred P] : list α → list α → list (list α)
| [] xs := [xs]
| (h :: t) xs :=
if P h then xs :: split_on_p_aux' t []
else split_on_p_aux' t (xs ++ [h])
lemma split_on_p_aux_eq {α : Type u} (P : α → Prop) [decidable_pred P] (xs ys : list α) :
split_on_p_aux' P xs ys = split_on_p_aux P xs ((++) ys) :=
begin
induction xs with a t ih generalizing ys; simp! only [append_nil, eq_self_iff_true, and_self],
split_ifs; rw ih,
{ refine ⟨rfl, rfl⟩ },
{ congr, ext, simp }
end
lemma split_on_p_aux_nil {α : Type u} (P : α → Prop) [decidable_pred P] (xs : list α) :
split_on_p_aux P xs id = split_on_p_aux' P xs [] :=
by { rw split_on_p_aux_eq, refl }
/-- The original list `L` can be recovered by joining the lists produced by `split_on_p p L`,
interspersed with the elements `L.filter p`. -/
lemma split_on_p_spec {α : Type u} (p : α → Prop) [decidable_pred p] (as : list α) :
join (zip_with (++) (split_on_p p as) ((as.filter p).map (λ x, [x]) ++ [[]])) = as :=
begin
rw [split_on_p, split_on_p_aux_nil],
suffices : ∀ xs,
join (zip_with (++) (split_on_p_aux' p as xs) ((as.filter p).map(λ x, [x]) ++ [[]])) = xs ++ as,
{ rw this, refl },
induction as; intro; simp! only [split_on_p_aux', append_nil],
split_ifs; simp [zip_with, join, *],
end
/-! ### all & any -/
@[simp] theorem all_nil (p : α → bool) : all [] p = tt := rfl
@[simp] theorem all_cons (p : α → bool) (a : α) (l : list α) :
all (a::l) p = (p a && all l p) := rfl
theorem all_iff_forall {p : α → bool} {l : list α} : all l p ↔ ∀ a ∈ l, p a :=
begin
induction l with a l ih,
{ exact iff_of_true rfl (forall_mem_nil _) },
simp only [all_cons, band_coe_iff, ih, forall_mem_cons]
end
theorem all_iff_forall_prop {p : α → Prop} [decidable_pred p]
{l : list α} : all l (λ a, p a) ↔ ∀ a ∈ l, p a :=
by simp only [all_iff_forall, bool.of_to_bool_iff]
@[simp] theorem any_nil (p : α → bool) : any [] p = ff := rfl
@[simp] theorem any_cons (p : α → bool) (a : α) (l : list α) :
any (a::l) p = (p a || any l p) := rfl
theorem any_iff_exists {p : α → bool} {l : list α} : any l p ↔ ∃ a ∈ l, p a :=
begin
induction l with a l ih,
{ exact iff_of_false bool.not_ff (not_exists_mem_nil _) },
simp only [any_cons, bor_coe_iff, ih, exists_mem_cons_iff]
end
theorem any_iff_exists_prop {p : α → Prop} [decidable_pred p]
{l : list α} : any l (λ a, p a) ↔ ∃ a ∈ l, p a :=
by simp [any_iff_exists]
theorem any_of_mem {p : α → bool} {a : α} {l : list α} (h₁ : a ∈ l) (h₂ : p a) : any l p :=
any_iff_exists.2 ⟨_, h₁, h₂⟩
@[priority 500] instance decidable_forall_mem {p : α → Prop} [decidable_pred p] (l : list α) :
decidable (∀ x ∈ l, p x) :=
decidable_of_iff _ all_iff_forall_prop
instance decidable_exists_mem {p : α → Prop} [decidable_pred p] (l : list α) :
decidable (∃ x ∈ l, p x) :=
decidable_of_iff _ any_iff_exists_prop
/-! ### map for partial functions -/
/-- Partial map. If `f : Π a, p a → β` is a partial function defined on
`a : α` satisfying `p`, then `pmap f l h` is essentially the same as `map f l`
but is defined only when all members of `l` satisfy `p`, using the proof
to apply `f`. -/
@[simp] def pmap {p : α → Prop} (f : Π a, p a → β) : Π l : list α, (∀ a ∈ l, p a) → list β
| [] H := []
| (a::l) H := f a (forall_mem_cons.1 H).1 :: pmap l (forall_mem_cons.1 H).2
/-- "Attach" the proof that the elements of `l` are in `l` to produce a new list
with the same elements but in the type `{x // x ∈ l}`. -/
def attach (l : list α) : list {x // x ∈ l} := pmap subtype.mk l (λ a, id)
theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {l : list α} (hx : x ∈ l) :
sizeof x < sizeof l :=
begin
induction l with h t ih; cases hx,
{ rw hx, exact lt_add_of_lt_of_nonneg (lt_one_add _) (nat.zero_le _) },
{ exact lt_add_of_pos_of_le (zero_lt_one_add _) (le_of_lt (ih hx)) }
end
@[simp] theorem pmap_eq_map (p : α → Prop) (f : α → β) (l : list α) (H) :
@pmap _ _ p (λ a _, f a) l H = map f l :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β}
(l : list α) {H₁ H₂} (h : ∀ a h₁ h₂, f a h₁ = g a h₂) :
pmap f l H₁ = pmap g l H₂ :=
by induction l with _ _ ih; [refl, rw [pmap, pmap, h, ih]]
theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β)
(l H) : map g (pmap f l H) = pmap (λ a h, g (f a h)) l H :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_map {p : β → Prop} (g : ∀ b, p b → γ) (f : α → β)
(l H) : pmap g (map f l) H = pmap (λ a h, g (f a) h) l (λ a h, H _ (mem_map_of_mem _ h)) :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β)
(l H) : pmap f l H = l.attach.map (λ x, f x.1 (H _ x.2)) :=
by rw [attach, map_pmap]; exact pmap_congr l (λ a h₁ h₂, rfl)
theorem attach_map_val (l : list α) : l.attach.map subtype.val = l :=
by rw [attach, map_pmap]; exact (pmap_eq_map _ _ _ _).trans (map_id l)
@[simp] theorem mem_attach (l : list α) : ∀ x, x ∈ l.attach | ⟨a, h⟩ :=
by have := mem_map.1 (by rw [attach_map_val]; exact h);
{ rcases this with ⟨⟨_, _⟩, m, rfl⟩, exact m }
@[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β}
{l H b} : b ∈ pmap f l H ↔ ∃ a (h : a ∈ l), f a (H a h) = b :=
by simp only [pmap_eq_map_attach, mem_map, mem_attach, true_and, subtype.exists]
@[simp] theorem length_pmap {p : α → Prop} {f : Π a, p a → β}
{l H} : length (pmap f l H) = length l :=
by induction l; [refl, simp only [*, pmap, length]]
@[simp] lemma length_attach (L : list α) : L.attach.length = L.length := length_pmap
@[simp] lemma pmap_eq_nil {p : α → Prop} {f : Π a, p a → β}
{l H} : pmap f l H = [] ↔ l = [] :=
by rw [← length_eq_zero, length_pmap, length_eq_zero]
@[simp] lemma attach_eq_nil (l : list α) : l.attach = [] ↔ l = [] := pmap_eq_nil
lemma last_pmap {α β : Type*} (p : α → Prop) (f : Π a, p a → β)
(l : list α) (hl₁ : ∀ a ∈ l, p a) (hl₂ : l ≠ []) :
(l.pmap f hl₁).last (mt list.pmap_eq_nil.1 hl₂) = f (l.last hl₂) (hl₁ _ (list.last_mem hl₂)) :=
begin
induction l with l_hd l_tl l_ih,
{ apply (hl₂ rfl).elim },
{ cases l_tl,
{ simp },
{ apply l_ih } }
end
lemma nth_pmap {p : α → Prop} (f : Π a, p a → β) {l : list α} (h : ∀ a ∈ l, p a) (n : ℕ) :
nth (pmap f l h) n = option.pmap f (nth l n) (λ x H, h x (nth_mem H)) :=
begin
induction l with hd tl hl generalizing n,
{ simp },
{ cases n; simp [hl] }
end
lemma nth_le_pmap {p : α → Prop} (f : Π a, p a → β) {l : list α} (h : ∀ a ∈ l, p a) {n : ℕ}
(hn : n < (pmap f l h).length) :
nth_le (pmap f l h) n hn = f (nth_le l n (@length_pmap _ _ p f l h ▸ hn))
(h _ (nth_le_mem l n (@length_pmap _ _ p f l h ▸ hn))) :=
begin
induction l with hd tl hl generalizing n,
{ simp only [length, pmap] at hn,
exact absurd hn (not_lt_of_le n.zero_le) },
{ cases n,
{ simp },
{ simpa [hl] } }
end
/-! ### find -/
section find
variables {p : α → Prop} [decidable_pred p] {l : list α} {a : α}
@[simp] theorem find_nil (p : α → Prop) [decidable_pred p] : find p [] = none :=
rfl
@[simp] theorem find_cons_of_pos (l) (h : p a) : find p (a::l) = some a :=
if_pos h
@[simp] theorem find_cons_of_neg (l) (h : ¬ p a) : find p (a::l) = find p l :=
if_neg h
@[simp] theorem find_eq_none : find p l = none ↔ ∀ x ∈ l, ¬ p x :=
begin
induction l with a l IH,
{ exact iff_of_true rfl (forall_mem_nil _) },
rw forall_mem_cons, by_cases h : p a,
{ simp only [find_cons_of_pos _ h, h, not_true, false_and] },
{ rwa [find_cons_of_neg _ h, iff_true_intro h, true_and] }
end
theorem find_some (H : find p l = some a) : p a :=
begin
induction l with b l IH, {contradiction},
by_cases h : p b,
{ rw find_cons_of_pos _ h at H, cases H, exact h },
{ rw find_cons_of_neg _ h at H, exact IH H }
end
@[simp] theorem find_mem (H : find p l = some a) : a ∈ l :=
begin
induction l with b l IH, {contradiction},
by_cases h : p b,
{ rw find_cons_of_pos _ h at H, cases H, apply mem_cons_self },
{ rw find_cons_of_neg _ h at H, exact mem_cons_of_mem _ (IH H) }
end
end find
/-! ### lookmap -/
section lookmap
variables (f : α → option α)
@[simp] theorem lookmap_nil : [].lookmap f = [] := rfl
@[simp] theorem lookmap_cons_none {a : α} (l : list α) (h : f a = none) :
(a :: l).lookmap f = a :: l.lookmap f :=
by simp [lookmap, h]
@[simp] theorem lookmap_cons_some {a b : α} (l : list α) (h : f a = some b) :
(a :: l).lookmap f = b :: l :=
by simp [lookmap, h]
theorem lookmap_some : ∀ l : list α, l.lookmap some = l
| [] := rfl
| (a::l) := rfl
theorem lookmap_none : ∀ l : list α, l.lookmap (λ _, none) = l
| [] := rfl
| (a::l) := congr_arg (cons a) (lookmap_none l)
theorem lookmap_congr {f g : α → option α} :
∀ {l : list α}, (∀ a ∈ l, f a = g a) → l.lookmap f = l.lookmap g
| [] H := rfl
| (a::l) H := begin
cases forall_mem_cons.1 H with H₁ H₂,
cases h : g a with b,
{ simp [h, H₁.trans h, lookmap_congr H₂] },
{ simp [lookmap_cons_some _ _ h, lookmap_cons_some _ _ (H₁.trans h)] }
end
theorem lookmap_of_forall_not {l : list α} (H : ∀ a ∈ l, f a = none) : l.lookmap f = l :=
(lookmap_congr H).trans (lookmap_none l)
theorem lookmap_map_eq (g : α → β) (h : ∀ a (b ∈ f a), g a = g b) :
∀ l : list α, map g (l.lookmap f) = map g l
| [] := rfl
| (a::l) := begin
cases h' : f a with b,
{ simp [h', lookmap_map_eq] },
{ simp [lookmap_cons_some _ _ h', h _ _ h'] }
end
theorem lookmap_id' (h : ∀ a (b ∈ f a), a = b) (l : list α) : l.lookmap f = l :=
by rw [← map_id (l.lookmap f), lookmap_map_eq, map_id]; exact h
theorem length_lookmap (l : list α) : length (l.lookmap f) = length l :=
by rw [← length_map, lookmap_map_eq _ (λ _, ()), length_map]; simp
end lookmap
/-! ### filter_map -/
@[simp] theorem filter_map_nil (f : α → option β) : filter_map f [] = [] := rfl
@[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (l : list α) (h : f a = none) :
filter_map f (a :: l) = filter_map f l :=
by simp only [filter_map, h]
@[simp] theorem filter_map_cons_some (f : α → option β)
(a : α) (l : list α) {b : β} (h : f a = some b) :
filter_map f (a :: l) = b :: filter_map f l :=
by simp only [filter_map, h]; split; refl
theorem filter_map_cons (f : α → option β) (a : α) (l : list α) :
filter_map f (a :: l) = option.cases_on (f a) (filter_map f l) (λb, b :: filter_map f l) :=
begin
generalize eq : f a = b,
cases b,
{ rw filter_map_cons_none _ _ eq },
{ rw filter_map_cons_some _ _ _ eq },
end
lemma filter_map_append {α β : Type*} (l l' : list α) (f : α → option β) :
filter_map f (l ++ l') = filter_map f l ++ filter_map f l' :=
begin
induction l with hd tl hl generalizing l',
{ simp },
{ rw [cons_append, filter_map, filter_map],
cases f hd;
simp only [filter_map, hl, cons_append, eq_self_iff_true, and_self] }
end
theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f :=
begin
funext l,
induction l with a l IH, {refl},
simp only [filter_map_cons_some (some ∘ f) _ _ rfl, IH, map_cons], split; refl
end
theorem filter_map_eq_filter (p : α → Prop) [decidable_pred p] :
filter_map (option.guard p) = filter p :=
begin
funext l,
induction l with a l IH, {refl},
by_cases pa : p a,
{ simp only [filter_map, option.guard, IH, if_pos pa, filter_cons_of_pos _ pa], split; refl },
{ simp only [filter_map, option.guard, IH, if_neg pa, filter_cons_of_neg _ pa] }
end
theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (l : list α) :
filter_map g (filter_map f l) = filter_map (λ x, (f x).bind g) l :=
begin
induction l with a l IH, {refl},
cases h : f a with b,
{ rw [filter_map_cons_none _ _ h, filter_map_cons_none, IH],
simp only [h, option.none_bind'] },
rw filter_map_cons_some _ _ _ h,
cases h' : g b with c;
[ rw [filter_map_cons_none _ _ h', filter_map_cons_none, IH],
rw [filter_map_cons_some _ _ _ h', filter_map_cons_some, IH] ];
simp only [h, h', option.some_bind']
end
theorem map_filter_map (f : α → option β) (g : β → γ) (l : list α) :
map g (filter_map f l) = filter_map (λ x, (f x).map g) l :=
by rw [← filter_map_eq_map, filter_map_filter_map]; refl
theorem filter_map_map (f : α → β) (g : β → option γ) (l : list α) :
filter_map g (map f l) = filter_map (g ∘ f) l :=
by rw [← filter_map_eq_map, filter_map_filter_map]; refl
theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (l : list α) :
filter p (filter_map f l) = filter_map (λ x, (f x).filter p) l :=
by rw [← filter_map_eq_filter, filter_map_filter_map]; refl
theorem filter_map_filter (p : α → Prop) [decidable_pred p] (f : α → option β) (l : list α) :
filter_map f (filter p l) = filter_map (λ x, if p x then f x else none) l :=
begin
rw [← filter_map_eq_filter, filter_map_filter_map], congr,
funext x,
show (option.guard p x).bind f = ite (p x) (f x) none,
by_cases h : p x,
{ simp only [option.guard, if_pos h, option.some_bind'] },
{ simp only [option.guard, if_neg h, option.none_bind'] }
end
@[simp] theorem filter_map_some (l : list α) : filter_map some l = l :=
by rw filter_map_eq_map; apply map_id
@[simp] theorem mem_filter_map (f : α → option β) (l : list α) {b : β} :
b ∈ filter_map f l ↔ ∃ a, a ∈ l ∧ f a = some b :=
begin
induction l with a l IH,
{ split, { intro H, cases H }, { rintro ⟨_, H, _⟩, cases H } },
cases h : f a with b',
{ have : f a ≠ some b, {rw h, intro, contradiction},
simp only [filter_map_cons_none _ _ h, IH, mem_cons_iff,
or_and_distrib_right, exists_or_distrib, exists_eq_left, this, false_or] },
{ have : f a = some b ↔ b = b',
{ split; intro t, {rw t at h; injection h}, {exact t.symm ▸ h} },
simp only [filter_map_cons_some _ _ _ h, IH, mem_cons_iff,
or_and_distrib_right, exists_or_distrib, this, exists_eq_left] }
end
theorem map_filter_map_of_inv (f : α → option β) (g : β → α)
(H : ∀ x : α, (f x).map g = some x) (l : list α) :
map g (filter_map f l) = l :=
by simp only [map_filter_map, H, filter_map_some]
theorem sublist.filter_map (f : α → option β) {l₁ l₂ : list α}
(s : l₁ <+ l₂) : filter_map f l₁ <+ filter_map f l₂ :=
by induction s with l₁ l₂ a s IH l₁ l₂ a s IH;
simp only [filter_map]; cases f a with b;
simp only [filter_map, IH, sublist.cons, sublist.cons2]
theorem sublist.map (f : α → β) {l₁ l₂ : list α}
(s : l₁ <+ l₂) : map f l₁ <+ map f l₂ :=
filter_map_eq_map f ▸ s.filter_map _
/-! ### reduce_option -/
@[simp] lemma reduce_option_cons_of_some (x : α) (l : list (option α)) :
reduce_option (some x :: l) = x :: l.reduce_option :=
by simp only [reduce_option, filter_map, id.def, eq_self_iff_true, and_self]
@[simp] lemma reduce_option_cons_of_none (l : list (option α)) :
reduce_option (none :: l) = l.reduce_option :=
by simp only [reduce_option, filter_map, id.def]
@[simp] lemma reduce_option_nil : @reduce_option α [] = [] := rfl
@[simp] lemma reduce_option_map {l : list (option α)} {f : α → β} :
reduce_option (map (option.map f) l) = map f (reduce_option l) :=
begin
induction l with hd tl hl,
{ simp only [reduce_option_nil, map_nil] },
{ cases hd;
simpa only [true_and, option.map_some', map, eq_self_iff_true,
reduce_option_cons_of_some] using hl },
end
lemma reduce_option_append (l l' : list (option α)) :
(l ++ l').reduce_option = l.reduce_option ++ l'.reduce_option :=
filter_map_append l l' id
lemma reduce_option_length_le (l : list (option α)) :
l.reduce_option.length ≤ l.length :=
begin
induction l with hd tl hl,
{ simp only [reduce_option_nil, length] },
{ cases hd,
{ exact nat.le_succ_of_le hl },
{ simpa only [length, add_le_add_iff_right, reduce_option_cons_of_some] using hl} }
end
lemma reduce_option_length_eq_iff {l : list (option α)} :
l.reduce_option.length = l.length ↔ ∀ x ∈ l, option.is_some x :=
begin
induction l with hd tl hl,
{ simp only [forall_const, reduce_option_nil, not_mem_nil,
forall_prop_of_false, eq_self_iff_true, length, not_false_iff] },
{ cases hd,
{ simp only [mem_cons_iff, forall_eq_or_imp, bool.coe_sort_ff, false_and,
reduce_option_cons_of_none, length, option.is_some_none, iff_false],
intro H,
have := reduce_option_length_le tl,
rw H at this,
exact absurd (nat.lt_succ_self _) (not_lt_of_le this) },
{ simp only [hl, true_and, mem_cons_iff, forall_eq_or_imp, add_left_inj,
bool.coe_sort_tt, length, option.is_some_some, reduce_option_cons_of_some] } }
end
lemma reduce_option_length_lt_iff {l : list (option α)} :
l.reduce_option.length < l.length ↔ none ∈ l :=
begin
rw [(reduce_option_length_le l).lt_iff_ne, ne, reduce_option_length_eq_iff],
induction l; simp *,
rw [eq_comm, ← option.not_is_some_iff_eq_none, decidable.imp_iff_not_or]
end
lemma reduce_option_singleton (x : option α) :
[x].reduce_option = x.to_list :=
by cases x; refl
lemma reduce_option_concat (l : list (option α)) (x : option α) :
(l.concat x).reduce_option = l.reduce_option ++ x.to_list :=
begin
induction l with hd tl hl generalizing x,
{ cases x;
simp [option.to_list] },
{ simp only [concat_eq_append, reduce_option_append] at hl,
cases hd;
simp [hl, reduce_option_append] }
end
lemma reduce_option_concat_of_some (l : list (option α)) (x : α) :
(l.concat (some x)).reduce_option = l.reduce_option.concat x :=
by simp only [reduce_option_nil, concat_eq_append, reduce_option_append, reduce_option_cons_of_some]
lemma reduce_option_mem_iff {l : list (option α)} {x : α} :
x ∈ l.reduce_option ↔ (some x) ∈ l :=
by simp only [reduce_option, id.def, mem_filter_map, exists_eq_right]
lemma reduce_option_nth_iff {l : list (option α)} {x : α} :
(∃ i, l.nth i = some (some x)) ↔ ∃ i, l.reduce_option.nth i = some x :=
by rw [←mem_iff_nth, ←mem_iff_nth, reduce_option_mem_iff]
/-! ### filter -/
section filter
variables {p : α → Prop} [decidable_pred p]
theorem filter_eq_foldr (p : α → Prop) [decidable_pred p] (l : list α) :
filter p l = foldr (λ a out, if p a then a :: out else out) [] l :=
by induction l; simp [*, filter]
lemma filter_congr {p q : α → Prop} [decidable_pred p] [decidable_pred q]
: ∀ {l : list α}, (∀ x ∈ l, p x ↔ q x) → filter p l = filter q l
| [] _ := rfl
| (a::l) h := by rw forall_mem_cons at h; by_cases pa : p a;
[simp only [filter_cons_of_pos _ pa, filter_cons_of_pos _ (h.1.1 pa), filter_congr h.2],
simp only [filter_cons_of_neg _ pa, filter_cons_of_neg _ (mt h.1.2 pa), filter_congr h.2]];
split; refl
@[simp] theorem filter_subset (l : list α) : filter p l ⊆ l :=
(filter_sublist l).subset
theorem of_mem_filter {a : α} : ∀ {l}, a ∈ filter p l → p a
| (b::l) ain :=
if pb : p b then
have a ∈ b :: filter p l, by simpa only [filter_cons_of_pos _ pb] using ain,
or.elim (eq_or_mem_of_mem_cons this)
(assume : a = b, begin rw [← this] at pb, exact pb end)
(assume : a ∈ filter p l, of_mem_filter this)
else
begin simp only [filter_cons_of_neg _ pb] at ain, exact (of_mem_filter ain) end
theorem mem_of_mem_filter {a : α} {l} (h : a ∈ filter p l) : a ∈ l :=
filter_subset l h
theorem mem_filter_of_mem {a : α} : ∀ {l}, a ∈ l → p a → a ∈ filter p l
| (_::l) (or.inl rfl) pa := by rw filter_cons_of_pos _ pa; apply mem_cons_self
| (b::l) (or.inr ain) pa := if pb : p b
then by rw [filter_cons_of_pos _ pb]; apply mem_cons_of_mem; apply mem_filter_of_mem ain pa
else by rw [filter_cons_of_neg _ pb]; apply mem_filter_of_mem ain pa
@[simp] theorem mem_filter {a : α} {l} : a ∈ filter p l ↔ a ∈ l ∧ p a :=
⟨λ h, ⟨mem_of_mem_filter h, of_mem_filter h⟩, λ ⟨h₁, h₂⟩, mem_filter_of_mem h₁ h₂⟩
lemma monotone_filter_left (p : α → Prop) [decidable_pred p]
⦃l l' : list α⦄ (h : l ⊆ l') : filter p l ⊆ filter p l' :=
begin
intros x hx,
rw [mem_filter] at hx ⊢,
exact ⟨h hx.left, hx.right⟩
end
theorem filter_eq_self {l} : filter p l = l ↔ ∀ a ∈ l, p a :=
begin
induction l with a l ih,
{ exact iff_of_true rfl (forall_mem_nil _) },
rw forall_mem_cons, by_cases p a,
{ rw [filter_cons_of_pos _ h, cons_inj, ih, and_iff_right h] },
{ rw [filter_cons_of_neg _ h],
refine iff_of_false _ (mt and.left h), intro e,
have := filter_sublist l, rw e at this,
exact not_lt_of_ge (length_le_of_sublist this) (lt_succ_self _) }
end
theorem filter_eq_nil {l} : filter p l = [] ↔ ∀ a ∈ l, ¬p a :=
by simp only [eq_nil_iff_forall_not_mem, mem_filter, not_and]
variable (p)
theorem sublist.filter {l₁ l₂} (s : l₁ <+ l₂) : filter p l₁ <+ filter p l₂ :=
filter_map_eq_filter p ▸ s.filter_map _
lemma monotone_filter_right (l : list α) ⦃p q : α → Prop⦄ [decidable_pred p] [decidable_pred q]
(h : p ≤ q) : l.filter p <+ l.filter q :=
begin
induction l with hd tl IH,
{ refl },
{ by_cases hp : p hd,
{ rw [filter_cons_of_pos _ hp, filter_cons_of_pos _ (h _ hp)],
exact IH.cons_cons hd },
{ rw filter_cons_of_neg _ hp,
by_cases hq : q hd,
{ rw filter_cons_of_pos _ hq,
exact sublist_cons_of_sublist hd IH },
{ rw filter_cons_of_neg _ hq,
exact IH } } }
end
theorem map_filter (f : β → α) (l : list β) :
filter p (map f l) = map f (filter (p ∘ f) l) :=
by rw [← filter_map_eq_map, filter_filter_map, filter_map_filter]; refl
@[simp] theorem filter_filter (q) [decidable_pred q] : ∀ l,
filter p (filter q l) = filter (λ a, p a ∧ q a) l
| [] := rfl
| (a :: l) := by by_cases hp : p a; by_cases hq : q a; simp only [hp, hq, filter, if_true, if_false,
true_and, false_and, filter_filter l, eq_self_iff_true]
@[simp] lemma filter_true {h : decidable_pred (λ a : α, true)} (l : list α) :
@filter α (λ _, true) h l = l :=
by convert filter_eq_self.2 (λ _ _, trivial)
@[simp] lemma filter_false {h : decidable_pred (λ a : α, false)} (l : list α) :
@filter α (λ _, false) h l = [] :=
by convert filter_eq_nil.2 (λ _ _, id)
@[simp] theorem span_eq_take_drop : ∀ (l : list α), span p l = (take_while p l, drop_while p l)
| [] := rfl
| (a::l) :=
if pa : p a then by simp only [span, if_pos pa, span_eq_take_drop l, take_while, drop_while]
else by simp only [span, take_while, drop_while, if_neg pa]
@[simp] theorem take_while_append_drop : ∀ (l : list α), take_while p l ++ drop_while p l = l
| [] := rfl
| (a::l) := if pa : p a then by rw [take_while, drop_while, if_pos pa, if_pos pa, cons_append,
take_while_append_drop l]
else by rw [take_while, drop_while, if_neg pa, if_neg pa, nil_append]
@[simp] theorem countp_nil : countp p [] = 0 := rfl
@[simp] theorem countp_cons_of_pos {a : α} (l) (pa : p a) : countp p (a::l) = countp p l + 1 :=
if_pos pa
@[simp] theorem countp_cons_of_neg {a : α} (l) (pa : ¬ p a) : countp p (a::l) = countp p l :=
if_neg pa
theorem length_eq_countp_add_countp (l) : length l = countp p l + countp (λ a, ¬p a) l :=
by induction l with x h ih; [refl, by_cases p x];
[simp only [countp_cons_of_pos _ _ h, countp_cons_of_neg (λ a, ¬p a) _ (decidable.not_not.2 h),
ih, length],
simp only [countp_cons_of_pos (λ a, ¬p a) _ h, countp_cons_of_neg _ _ h, ih, length]]; ac_refl
theorem countp_eq_length_filter (l) : countp p l = length (filter p l) :=
by induction l with x l ih; [refl, by_cases (p x)];
[simp only [filter_cons_of_pos _ h, countp, ih, if_pos h],
simp only [countp_cons_of_neg _ _ h, ih, filter_cons_of_neg _ h]]; refl
@[simp] theorem countp_append (l₁ l₂) : countp p (l₁ ++ l₂) = countp p l₁ + countp p l₂ :=
by simp only [countp_eq_length_filter, filter_append, length_append]
theorem countp_pos {l} : 0 < countp p l ↔ ∃ a ∈ l, p a :=
by simp only [countp_eq_length_filter, length_pos_iff_exists_mem, mem_filter, exists_prop]
theorem length_filter_lt_length_iff_exists (l) : length (filter p l) < length l ↔ ∃ x ∈ l, ¬p x :=
by rw [length_eq_countp_add_countp p l, ← countp_pos, countp_eq_length_filter, lt_add_iff_pos_right]
theorem countp_le_of_sublist {l₁ l₂} (s : l₁ <+ l₂) : countp p l₁ ≤ countp p l₂ :=
by simpa only [countp_eq_length_filter] using length_le_of_sublist (s.filter p)
@[simp] theorem countp_filter {q} [decidable_pred q] (l : list α) :
countp p (filter q l) = countp (λ a, p a ∧ q a) l :=
by simp only [countp_eq_length_filter, filter_filter]
end filter
/-! ### count -/
section count
variable [decidable_eq α]
@[simp] theorem count_nil (a : α) : count a [] = 0 := rfl
theorem count_cons (a b : α) (l : list α) :
count a (b :: l) = if a = b then succ (count a l) else count a l := rfl
theorem count_cons' (a b : α) (l : list α) :
count a (b :: l) = count a l + (if a = b then 1 else 0) :=
begin rw count_cons, split_ifs; refl end
@[simp] theorem count_cons_self (a : α) (l : list α) : count a (a::l) = succ (count a l) :=
if_pos rfl
@[simp, priority 990]
theorem count_cons_of_ne {a b : α} (h : a ≠ b) (l : list α) : count a (b::l) = count a l :=
if_neg h
theorem count_tail : Π (l : list α) (a : α) (h : 0 < l.length),
l.tail.count a = l.count a - ite (a = list.nth_le l 0 h) 1 0
| (_ :: _) a h := by { rw [count_cons], split_ifs; simp }
theorem count_le_of_sublist (a : α) {l₁ l₂} : l₁ <+ l₂ → count a l₁ ≤ count a l₂ :=
countp_le_of_sublist _
theorem count_le_count_cons (a b : α) (l : list α) : count a l ≤ count a (b :: l) :=
count_le_of_sublist _ (sublist_cons _ _)
theorem count_singleton (a : α) : count a [a] = 1 := if_pos rfl
@[simp] theorem count_append (a : α) : ∀ l₁ l₂, count a (l₁ ++ l₂) = count a l₁ + count a l₂ :=
countp_append _
theorem count_concat (a : α) (l : list α) : count a (concat l a) = succ (count a l) :=
by simp [-add_comm]
theorem count_pos {a : α} {l : list α} : 0 < count a l ↔ a ∈ l :=
by simp only [count, countp_pos, exists_prop, exists_eq_right']
@[simp, priority 980]
theorem count_eq_zero_of_not_mem {a : α} {l : list α} (h : a ∉ l) : count a l = 0 :=
decidable.by_contradiction $ λ h', h $ count_pos.1 (nat.pos_of_ne_zero h')
theorem not_mem_of_count_eq_zero {a : α} {l : list α} (h : count a l = 0) : a ∉ l :=
λ h', ne_of_gt (count_pos.2 h') h
@[simp] theorem count_repeat (a : α) (n : ℕ) : count a (repeat a n) = n :=
by rw [count, countp_eq_length_filter, filter_eq_self.2, length_repeat];
exact λ b m, (eq_of_mem_repeat m).symm
theorem le_count_iff_repeat_sublist {a : α} {l : list α} {n : ℕ} :
n ≤ count a l ↔ repeat a n <+ l :=
⟨λ h, ((repeat_sublist_repeat a).2 h).trans $
have filter (eq a) l = repeat a (count a l), from eq_repeat.2
⟨by simp only [count, countp_eq_length_filter], λ b m, (of_mem_filter m).symm⟩,
by rw ← this; apply filter_sublist,
λ h, by simpa only [count_repeat] using count_le_of_sublist a h⟩
theorem repeat_count_eq_of_count_eq_length {a : α} {l : list α} (h : count a l = length l) :
repeat a (count a l) = l :=
eq_of_sublist_of_length_eq (le_count_iff_repeat_sublist.mp (le_refl (count a l)))
(eq.trans (length_repeat a (count a l)) h)
@[simp] theorem count_filter {p} [decidable_pred p]
{a} {l : list α} (h : p a) : count a (filter p l) = count a l :=
by simp only [count, countp_filter]; congr; exact
set.ext (λ b, and_iff_left_of_imp (λ e, e ▸ h))
lemma count_bind {α β} [decidable_eq β] (l : list α) (f : α → list β) (x : β) :
count x (l.bind f) = sum (map (count x ∘ f) l) :=
begin
induction l with hd tl IH,
{ simp },
{ simpa }
end
@[simp] lemma count_map_map {α β} [decidable_eq α] [decidable_eq β] (l : list α) (f : α → β)
(hf : function.injective f) (x : α) :
count (f x) (map f l) = count x l :=
begin
induction l with y l IH generalizing x,
{ simp },
{ rw map_cons,
by_cases h : x = y,
{ simpa [h] using IH _ },
{ simpa [h, hf.ne h] using IH _ } }
end
end count
/-! ### prefix, suffix, infix -/
@[simp] theorem prefix_append (l₁ l₂ : list α) : l₁ <+: l₁ ++ l₂ := ⟨l₂, rfl⟩
@[simp] theorem suffix_append (l₁ l₂ : list α) : l₂ <:+ l₁ ++ l₂ := ⟨l₁, rfl⟩
theorem infix_append (l₁ l₂ l₃ : list α) : l₂ <:+: l₁ ++ l₂ ++ l₃ := ⟨l₁, l₃, rfl⟩
@[simp] theorem infix_append' (l₁ l₂ l₃ : list α) : l₂ <:+: l₁ ++ (l₂ ++ l₃) :=
by rw ← list.append_assoc; apply infix_append
theorem nil_prefix (l : list α) : [] <+: l := ⟨l, rfl⟩
theorem nil_suffix (l : list α) : [] <:+ l := ⟨l, append_nil _⟩
@[refl] theorem prefix_refl (l : list α) : l <+: l := ⟨[], append_nil _⟩
@[refl] theorem suffix_refl (l : list α) : l <:+ l := ⟨[], rfl⟩
@[simp] theorem suffix_cons (a : α) : ∀ l, l <:+ a :: l := suffix_append [a]
theorem prefix_concat (a : α) (l) : l <+: concat l a := by simp
theorem is_prefix.is_infix {l₁ l₂ : list α} : l₁ <+: l₂ → l₁ <:+: l₂ :=
λ⟨t, h⟩, ⟨[], t, h⟩
theorem is_suffix.is_infix {l₁ l₂ : list α} : l₁ <:+ l₂ → l₁ <:+: l₂ :=
λ⟨t, h⟩, ⟨t, [], by simp only [h, append_nil]⟩
@[refl] theorem infix_refl (l : list α) : l <:+: l := (prefix_refl l).is_infix
theorem nil_infix (l : list α) : [] <:+: l := (nil_prefix l).is_infix
theorem infix_cons {L₁ L₂ : list α} {x : α} : L₁ <:+: L₂ → L₁ <:+: x :: L₂ :=
λ⟨LP, LS, H⟩, ⟨x :: LP, LS, H ▸ rfl⟩
@[trans] theorem is_prefix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <+: l₂ → l₂ <+: l₃ → l₁ <+: l₃
| l ._ ._ ⟨r₁, rfl⟩ ⟨r₂, rfl⟩ := ⟨r₁ ++ r₂, (append_assoc _ _ _).symm⟩
@[trans] theorem is_suffix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+ l₂ → l₂ <:+ l₃ → l₁ <:+ l₃
| l ._ ._ ⟨l₁, rfl⟩ ⟨l₂, rfl⟩ := ⟨l₂ ++ l₁, append_assoc _ _ _⟩
@[trans] theorem is_infix.trans : ∀ {l₁ l₂ l₃ : list α}, l₁ <:+: l₂ → l₂ <:+: l₃ → l₁ <:+: l₃
| l ._ ._ ⟨l₁, r₁, rfl⟩ ⟨l₂, r₂, rfl⟩ := ⟨l₂ ++ l₁, r₁ ++ r₂, by simp only [append_assoc]⟩
protected theorem is_infix.sublist {l₁ l₂ : list α} : l₁ <:+: l₂ → l₁ <+ l₂ :=
λ ⟨s, t, h⟩, by rw [← h]; exact (sublist_append_right _ _).trans (sublist_append_left _ _)
protected theorem is_prefix.sublist {l₁ l₂ : list α} (h : l₁ <+: l₂) : l₁ <+ l₂ :=
h.is_infix.sublist
protected theorem is_suffix.sublist {l₁ l₂ : list α} (h : l₁ <:+ l₂) : l₁ <+ l₂ :=
h.is_infix.sublist
@[simp] theorem reverse_suffix {l₁ l₂ : list α} : reverse l₁ <:+ reverse l₂ ↔ l₁ <+: l₂ :=
⟨λ ⟨r, e⟩, ⟨reverse r,
by rw [← reverse_reverse l₁, ← reverse_append, e, reverse_reverse]⟩,
λ ⟨r, e⟩, ⟨reverse r, by rw [← reverse_append, e]⟩⟩
@[simp] theorem reverse_prefix {l₁ l₂ : list α} : reverse l₁ <+: reverse l₂ ↔ l₁ <:+ l₂ :=
by rw ← reverse_suffix; simp only [reverse_reverse]
theorem infix.length_le {l₁ l₂ : list α} (s : l₁ <:+: l₂) : length l₁ ≤ length l₂ :=
length_le_of_sublist s.sublist
theorem eq_nil_of_infix_nil {l : list α} (s : l <:+: []) : l = [] :=
eq_nil_of_sublist_nil s.sublist
@[simp] theorem eq_nil_iff_infix_nil {l : list α} : l <:+: [] ↔ l = [] :=
⟨eq_nil_of_infix_nil, λ h, h ▸ infix_refl _⟩
theorem eq_nil_of_prefix_nil {l : list α} (s : l <+: []) : l = [] :=
eq_nil_of_infix_nil s.is_infix
@[simp] theorem eq_nil_iff_prefix_nil {l : list α} : l <+: [] ↔ l = [] :=
⟨eq_nil_of_prefix_nil, λ h, h ▸ prefix_refl _⟩
theorem eq_nil_of_suffix_nil {l : list α} (s : l <:+ []) : l = [] :=
eq_nil_of_infix_nil s.is_infix
@[simp] theorem eq_nil_iff_suffix_nil {l : list α} : l <:+ [] ↔ l = [] :=
⟨eq_nil_of_suffix_nil, λ h, h ▸ suffix_refl _⟩
theorem infix_iff_prefix_suffix (l₁ l₂ : list α) : l₁ <:+: l₂ ↔ ∃ t, l₁ <+: t ∧ t <:+ l₂ :=
⟨λ⟨s, t, e⟩, ⟨l₁ ++ t, ⟨_, rfl⟩, by rw [← e, append_assoc]; exact ⟨_, rfl⟩⟩,
λ⟨._, ⟨t, rfl⟩, ⟨s, e⟩⟩, ⟨s, t, by rw append_assoc; exact e⟩⟩
theorem eq_of_infix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+: l₂) :
length l₁ = length l₂ → l₁ = l₂ :=
eq_of_sublist_of_length_eq s.sublist
theorem eq_of_prefix_of_length_eq {l₁ l₂ : list α} (s : l₁ <+: l₂) :
length l₁ = length l₂ → l₁ = l₂ :=
eq_of_sublist_of_length_eq s.sublist
theorem eq_of_suffix_of_length_eq {l₁ l₂ : list α} (s : l₁ <:+ l₂) :
length l₁ = length l₂ → l₁ = l₂ :=
eq_of_sublist_of_length_eq s.sublist
theorem prefix_of_prefix_length_le : ∀ {l₁ l₂ l₃ : list α},
l₁ <+: l₃ → l₂ <+: l₃ → length l₁ ≤ length l₂ → l₁ <+: l₂
| [] l₂ l₃ h₁ h₂ _ := nil_prefix _
| (a::l₁) (b::l₂) _ ⟨r₁, rfl⟩ ⟨r₂, e⟩ ll := begin
injection e with _ e', subst b,
rcases prefix_of_prefix_length_le ⟨_, rfl⟩ ⟨_, e'⟩
(le_of_succ_le_succ ll) with ⟨r₃, rfl⟩,
exact ⟨r₃, rfl⟩
end
theorem prefix_or_prefix_of_prefix {l₁ l₂ l₃ : list α}
(h₁ : l₁ <+: l₃) (h₂ : l₂ <+: l₃) : l₁ <+: l₂ ∨ l₂ <+: l₁ :=
(le_total (length l₁) (length l₂)).imp
(prefix_of_prefix_length_le h₁ h₂)
(prefix_of_prefix_length_le h₂ h₁)
theorem suffix_of_suffix_length_le {l₁ l₂ l₃ : list α}
(h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) (ll : length l₁ ≤ length l₂) : l₁ <:+ l₂ :=
reverse_prefix.1 $ prefix_of_prefix_length_le
(reverse_prefix.2 h₁) (reverse_prefix.2 h₂) (by simp [ll])
theorem suffix_or_suffix_of_suffix {l₁ l₂ l₃ : list α}
(h₁ : l₁ <:+ l₃) (h₂ : l₂ <:+ l₃) : l₁ <:+ l₂ ∨ l₂ <:+ l₁ :=
(prefix_or_prefix_of_prefix (reverse_prefix.2 h₁) (reverse_prefix.2 h₂)).imp
reverse_prefix.1 reverse_prefix.1
theorem suffix_cons_iff {x : α} {l₁ l₂ : list α} :
l₁ <:+ x :: l₂ ↔ l₁ = x :: l₂ ∨ l₁ <:+ l₂ :=
begin
split,
{ rintro ⟨⟨hd, tl⟩, hl₃⟩,
{ exact or.inl hl₃ },
{ simp only [cons_append] at hl₃,
exact or.inr ⟨_, hl₃.2⟩ } },
{ rintro (rfl | hl₁),
{ exact (x :: l₂).suffix_refl },
{ exact hl₁.trans (l₂.suffix_cons _) } }
end
theorem infix_of_mem_join : ∀ {L : list (list α)} {l}, l ∈ L → l <:+: join L
| (_ :: L) l (or.inl rfl) := infix_append [] _ _
| (l' :: L) l (or.inr h) :=
is_infix.trans (infix_of_mem_join h) $ (suffix_append _ _).is_infix
theorem prefix_append_right_inj {l₁ l₂ : list α} (l) : l ++ l₁ <+: l ++ l₂ ↔ l₁ <+: l₂ :=
exists_congr $ λ r, by rw [append_assoc, append_right_inj]
theorem prefix_cons_inj {l₁ l₂ : list α} (a) : a :: l₁ <+: a :: l₂ ↔ l₁ <+: l₂ :=
prefix_append_right_inj [a]
theorem take_prefix (n) (l : list α) : take n l <+: l := ⟨_, take_append_drop _ _⟩
theorem take_sublist (n) (l : list α) : take n l <+ l := (take_prefix n l).sublist
theorem take_subset (n) (l : list α) : take n l ⊆ l := (take_sublist n l).subset
theorem mem_of_mem_take {n} {l : list α} {x : α} (h : x ∈ l.take n) : x ∈ l := take_subset n l h
theorem drop_suffix (n) (l : list α) : drop n l <:+ l := ⟨_, take_append_drop _ _⟩
theorem drop_sublist (n) (l : list α) : drop n l <+ l := (drop_suffix n l).sublist
theorem drop_subset (n) (l : list α) : drop n l ⊆ l := (drop_sublist n l).subset
theorem mem_of_mem_drop {n} {l : list α} {x : α} (h : x ∈ l.drop n) : x ∈ l := drop_subset n l h
theorem init_prefix : ∀ (l : list α), l.init <+: l
| [] := ⟨nil, by rw [init, list.append_nil]⟩
| (a :: l) := ⟨_, init_append_last (cons_ne_nil a l)⟩
theorem init_sublist (l : list α) : l.init <+ l := (init_prefix l).sublist
theorem init_subset (l : list α) : l.init ⊆ l := (init_sublist l).subset
theorem mem_of_mem_init {l : list α} {a : α} (h : a ∈ l.init) : a ∈ l := init_subset l h
theorem tail_suffix (l : list α) : tail l <:+ l := by rw ← drop_one; apply drop_suffix
theorem tail_sublist (l : list α) : l.tail <+ l := (tail_suffix l).sublist
theorem tail_subset (l : list α) : tail l ⊆ l := (tail_sublist l).subset
theorem mem_of_mem_tail {l : list α} {a : α} (h : a ∈ l.tail) : a ∈ l := tail_subset l h
theorem prefix_iff_eq_append {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ ++ drop (length l₁) l₂ = l₂ :=
⟨by rintros ⟨r, rfl⟩; rw drop_left, λ e, ⟨_, e⟩⟩
theorem suffix_iff_eq_append {l₁ l₂ : list α} :
l₁ <:+ l₂ ↔ take (length l₂ - length l₁) l₂ ++ l₁ = l₂ :=
⟨by rintros ⟨r, rfl⟩; simp only [length_append, add_tsub_cancel_right, take_left], λ e, ⟨_, e⟩⟩
theorem prefix_iff_eq_take {l₁ l₂ : list α} : l₁ <+: l₂ ↔ l₁ = take (length l₁) l₂ :=
⟨λ h, append_right_cancel $
(prefix_iff_eq_append.1 h).trans (take_append_drop _ _).symm,
λ e, e.symm ▸ take_prefix _ _⟩
theorem suffix_iff_eq_drop {l₁ l₂ : list α} : l₁ <:+ l₂ ↔ l₁ = drop (length l₂ - length l₁) l₂ :=
⟨λ h, append_left_cancel $
(suffix_iff_eq_append.1 h).trans (take_append_drop _ _).symm,
λ e, e.symm ▸ drop_suffix _ _⟩
instance decidable_prefix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+: l₂)
| [] l₂ := is_true ⟨l₂, rfl⟩
| (a::l₁) [] := is_false $ λ ⟨t, te⟩, list.no_confusion te
| (a::l₁) (b::l₂) :=
if h : a = b then
@decidable_of_iff _ _ (by rw [← h, prefix_cons_inj])
(decidable_prefix l₁ l₂)
else
is_false $ λ ⟨t, te⟩, h $ by injection te
-- Alternatively, use mem_tails
instance decidable_suffix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+ l₂)
| [] l₂ := is_true ⟨l₂, append_nil _⟩
| (a::l₁) [] := is_false $ mt (length_le_of_sublist ∘ is_suffix.sublist) dec_trivial
| l₁ l₂ := let len1 := length l₁, len2 := length l₂ in
if hl : len1 ≤ len2 then
decidable_of_iff' (l₁ = drop (len2-len1) l₂) suffix_iff_eq_drop
else is_false $ λ h, hl $ length_le_of_sublist $ h.sublist
lemma prefix_take_le_iff {L : list (list (option α))} {m n : ℕ} (hm : m < L.length) :
(take m L) <+: (take n L) ↔ m ≤ n :=
begin
simp only [prefix_iff_eq_take, length_take],
induction m with m IH generalizing L n,
{ simp only [min_eq_left, eq_self_iff_true, nat.zero_le, take] },
{ cases n,
{ simp only [nat.nat_zero_eq_zero, nonpos_iff_eq_zero, take, take_nil],
split,
{ cases L,
{ exact absurd hm (not_lt_of_le m.succ.zero_le) },
{ simp only [forall_prop_of_false, not_false_iff, take] } },
{ intro h,
contradiction } },
{ cases L with l ls,
{ exact absurd hm (not_lt_of_le m.succ.zero_le) },
{ simp only [length] at hm,
specialize @IH ls n (nat.lt_of_succ_lt_succ hm),
simp only [le_of_lt (nat.lt_of_succ_lt_succ hm), min_eq_left] at IH,
simp only [le_of_lt hm, IH, true_and, min_eq_left, eq_self_iff_true, length, take],
exact ⟨nat.succ_le_succ, nat.le_of_succ_le_succ⟩ } } },
end
lemma cons_prefix_iff {l l' : list α} {x y : α} :
x :: l <+: y :: l' ↔ x = y ∧ l <+: l' :=
begin
split,
{ rintro ⟨L, hL⟩,
simp only [cons_append] at hL,
exact ⟨hL.left, ⟨L, hL.right⟩⟩ },
{ rintro ⟨rfl, h⟩,
rwa [prefix_cons_inj] },
end
lemma map_prefix {l l' : list α} (f : α → β) (h : l <+: l') :
l.map f <+: l'.map f :=
begin
induction l with hd tl hl generalizing l',
{ simp only [nil_prefix, map_nil] },
{ cases l' with hd' tl',
{ simpa only using eq_nil_of_prefix_nil h },
{ rw cons_prefix_iff at h,
simp only [h, prefix_cons_inj, hl, map] } },
end
lemma is_prefix.filter_map {l l' : list α} (h : l <+: l') (f : α → option β) :
l.filter_map f <+: l'.filter_map f :=
begin
induction l with hd tl hl generalizing l',
{ simp only [nil_prefix, filter_map_nil] },
{ cases l' with hd' tl',
{ simpa only using eq_nil_of_prefix_nil h },
{ rw cons_prefix_iff at h,
rw [←@singleton_append _ hd _, ←@singleton_append _ hd' _, filter_map_append,
filter_map_append, h.left, prefix_append_right_inj],
exact hl h.right } },
end
lemma is_prefix.reduce_option {l l' : list (option α)} (h : l <+: l') :
l.reduce_option <+: l'.reduce_option :=
h.filter_map id
lemma is_prefix.filter (p : α → Prop) [decidable_pred p]
⦃l l' : list α⦄ (h : l <+: l') : filter p l <+: filter p l' :=
begin
obtain ⟨xs, rfl⟩ := h,
rw filter_append,
exact prefix_append _ _
end
lemma is_suffix.filter (p : α → Prop) [decidable_pred p]
⦃l l' : list α⦄ (h : l <:+ l') : filter p l <:+ filter p l' :=
begin
obtain ⟨xs, rfl⟩ := h,
rw filter_append,
exact suffix_append _ _
end
lemma is_infix.filter (p : α → Prop) [decidable_pred p]
⦃l l' : list α⦄ (h : l <:+: l') : filter p l <:+: filter p l' :=
begin
obtain ⟨xs, ys, rfl⟩ := h,
rw [filter_append, filter_append],
exact infix_append _ _ _
end
@[simp] theorem mem_inits : ∀ (s t : list α), s ∈ inits t ↔ s <+: t
| s [] := suffices s = nil ↔ s <+: nil, by simpa only [inits, mem_singleton],
⟨λh, h.symm ▸ prefix_refl [], eq_nil_of_prefix_nil⟩
| s (a::t) :=
suffices (s = nil ∨ ∃ l ∈ inits t, a :: l = s) ↔ s <+: a :: t, by simpa,
⟨λo, match s, o with
| ._, or.inl rfl := ⟨_, rfl⟩
| s, or.inr ⟨r, hr, hs⟩ := let ⟨s, ht⟩ := (mem_inits _ _).1 hr in
by rw [← hs, ← ht]; exact ⟨s, rfl⟩
end, λmi, match s, mi with
| [], ⟨._, rfl⟩ := or.inl rfl
| (b::s), ⟨r, hr⟩ := list.no_confusion hr $ λba (st : s++r = t), or.inr $
by rw ba; exact ⟨_, (mem_inits _ _).2 ⟨_, st⟩, rfl⟩
end⟩
@[simp] theorem mem_tails : ∀ (s t : list α), s ∈ tails t ↔ s <:+ t
| s [] := by simp only [tails, mem_singleton];
exact ⟨λh, by rw h; exact suffix_refl [], eq_nil_of_suffix_nil⟩
| s (a::t) := by simp only [tails, mem_cons_iff, mem_tails s t];
exact show s = a :: t ∨ s <:+ t ↔ s <:+ a :: t, from
⟨λo, match s, t, o with
| ._, t, or.inl rfl := suffix_refl _
| s, ._, or.inr ⟨l, rfl⟩ := ⟨a::l, rfl⟩
end, λe, match s, t, e with
| ._, t, ⟨[], rfl⟩ := or.inl rfl
| s, t, ⟨b::l, he⟩ := list.no_confusion he (λab lt, or.inr ⟨l, lt⟩)
end⟩
lemma inits_cons (a : α) (l : list α) : inits (a :: l) = [] :: l.inits.map (λ t, a :: t) :=
by simp
lemma tails_cons (a : α) (l : list α) : tails (a :: l) = (a :: l) :: l.tails :=
by simp
@[simp]
lemma inits_append : ∀ (s t : list α), inits (s ++ t) = s.inits ++ t.inits.tail.map (λ l, s ++ l)
| [] [] := by simp
| [] (a::t) := by simp
| (a::s) t := by simp [inits_append s t]
@[simp]
lemma tails_append : ∀ (s t : list α), tails (s ++ t) = s.tails.map (λ l, l ++ t) ++ t.tails.tail
| [] [] := by simp
| [] (a::t) := by simp
| (a::s) t := by simp [tails_append s t]
-- the lemma names `inits_eq_tails` and `tails_eq_inits` are like `sublists_eq_sublists'`
lemma inits_eq_tails :
∀ (l : list α), l.inits = (reverse $ map reverse $ tails $ reverse l)
| [] := by simp
| (a :: l) := by simp [inits_eq_tails l, map_eq_map_iff]
lemma tails_eq_inits :
∀ (l : list α), l.tails = (reverse $ map reverse $ inits $ reverse l)
| [] := by simp
| (a :: l) := by simp [tails_eq_inits l, append_left_inj]
lemma inits_reverse (l : list α) : inits (reverse l) = reverse (map reverse l.tails) :=
by { rw tails_eq_inits l, simp [reverse_involutive.comp_self], }
lemma tails_reverse (l : list α) : tails (reverse l) = reverse (map reverse l.inits) :=
by { rw inits_eq_tails l, simp [reverse_involutive.comp_self], }
lemma map_reverse_inits (l : list α) : map reverse l.inits = (reverse $ tails $ reverse l) :=
by { rw inits_eq_tails l, simp [reverse_involutive.comp_self], }
lemma map_reverse_tails (l : list α) : map reverse l.tails = (reverse $ inits $ reverse l) :=
by { rw tails_eq_inits l, simp [reverse_involutive.comp_self], }
@[simp] lemma length_tails (l : list α) : length (tails l) = length l + 1 :=
begin
induction l with x l IH,
{ simp },
{ simpa using IH }
end
@[simp] lemma length_inits (l : list α) : length (inits l) = length l + 1 :=
by simp [inits_eq_tails]
@[simp] lemma nth_le_tails (l : list α) (n : ℕ) (hn : n < length (tails l)) :
nth_le (tails l) n hn = l.drop n :=
begin
induction l with x l IH generalizing n,
{ simp },
{ cases n,
{ simp },
{ simpa using IH n _ } },
end
@[simp] lemma nth_le_inits (l : list α) (n : ℕ) (hn : n < length (inits l)) :
nth_le (inits l) n hn = l.take n :=
begin
induction l with x l IH generalizing n,
{ simp },
{ cases n,
{ simp },
{ simpa using IH n _ } }
end
instance decidable_infix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+: l₂)
| [] l₂ := is_true ⟨[], l₂, rfl⟩
| (a::l₁) [] := is_false $ λ⟨s, t, te⟩, absurd te $ append_ne_nil_of_ne_nil_left _ _ $
append_ne_nil_of_ne_nil_right _ _ $ λh, list.no_confusion h
| l₁ l₂ := decidable_of_decidable_of_iff (list.decidable_bex (λt, l₁ <+: t) (tails l₂)) $
by refine (exists_congr (λt, _)).trans (infix_iff_prefix_suffix _ _).symm;
exact ⟨λ⟨h1, h2⟩, ⟨h2, (mem_tails _ _).1 h1⟩, λ⟨h2, h1⟩, ⟨(mem_tails _ _).2 h1, h2⟩⟩
/-! ### insert -/
section insert
variable [decidable_eq α]
@[simp] theorem insert_nil (a : α) : insert a nil = [a] := rfl
theorem insert.def (a : α) (l : list α) : insert a l = if a ∈ l then l else a :: l := rfl
@[simp, priority 980]
theorem insert_of_mem {a : α} {l : list α} (h : a ∈ l) : insert a l = l :=
by simp only [insert.def, if_pos h]
@[simp, priority 970]
theorem insert_of_not_mem {a : α} {l : list α} (h : a ∉ l) : insert a l = a :: l :=
by simp only [insert.def, if_neg h]; split; refl
@[simp] theorem mem_insert_iff {a b : α} {l : list α} : a ∈ insert b l ↔ a = b ∨ a ∈ l :=
begin
by_cases h' : b ∈ l,
{ simp only [insert_of_mem h'],
apply (or_iff_right_of_imp _).symm,
exact λ e, e.symm ▸ h' },
simp only [insert_of_not_mem h', mem_cons_iff]
end
@[simp] theorem suffix_insert (a : α) (l : list α) : l <:+ insert a l :=
by by_cases a ∈ l; [simp only [insert_of_mem h], simp only [insert_of_not_mem h, suffix_cons]]
theorem infix_insert (a : α) (l : list α) : l <:+: insert a l := (suffix_insert a l).is_infix
theorem sublist_insert (a : α) (l : list α) : l <+ insert a l := (suffix_insert a l).sublist
theorem subset_insert (a : α) (l : list α) : l ⊆ insert a l := (sublist_insert a l).subset
@[simp] theorem mem_insert_self (a : α) (l : list α) : a ∈ insert a l :=
mem_insert_iff.2 (or.inl rfl)
theorem mem_insert_of_mem {a b : α} {l : list α} (h : a ∈ l) : a ∈ insert b l :=
mem_insert_iff.2 (or.inr h)
theorem eq_or_mem_of_mem_insert {a b : α} {l : list α} (h : a ∈ insert b l) : a = b ∨ a ∈ l :=
mem_insert_iff.1 h
@[simp] theorem length_insert_of_mem {a : α} {l : list α} (h : a ∈ l) :
length (insert a l) = length l :=
by rw insert_of_mem h
@[simp] theorem length_insert_of_not_mem {a : α} {l : list α} (h : a ∉ l) :
length (insert a l) = length l + 1 :=
by rw insert_of_not_mem h; refl
end insert
/-! ### erasep -/
section erasep
variables {p : α → Prop} [decidable_pred p]
@[simp] theorem erasep_nil : [].erasep p = [] := rfl
theorem erasep_cons (a : α) (l : list α) :
(a :: l).erasep p = if p a then l else a :: l.erasep p := rfl
@[simp] theorem erasep_cons_of_pos {a : α} {l : list α} (h : p a) : (a :: l).erasep p = l :=
by simp [erasep_cons, h]
@[simp] theorem erasep_cons_of_neg {a : α} {l : list α} (h : ¬ p a) :
(a::l).erasep p = a :: l.erasep p :=
by simp [erasep_cons, h]
theorem erasep_of_forall_not {l : list α}
(h : ∀ a ∈ l, ¬ p a) : l.erasep p = l :=
by induction l with _ _ ih; [refl,
simp [h _ (or.inl rfl), ih (forall_mem_of_forall_mem_cons h)]]
theorem exists_of_erasep {l : list α} {a} (al : a ∈ l) (pa : p a) :
∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ :=
begin
induction l with b l IH, {cases al},
by_cases pb : p b,
{ exact ⟨b, [], l, forall_mem_nil _, pb, by simp [pb]⟩ },
{ rcases al with rfl | al, {exact pb.elim pa},
rcases IH al with ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩,
exact ⟨c, b::l₁, l₂, forall_mem_cons.2 ⟨pb, h₁⟩,
h₂, by rw h₃; refl, by simp [pb, h₄]⟩ }
end
theorem exists_or_eq_self_of_erasep (p : α → Prop) [decidable_pred p] (l : list α) :
l.erasep p = l ∨ ∃ a l₁ l₂, (∀ b ∈ l₁, ¬ p b) ∧ p a ∧ l = l₁ ++ a :: l₂ ∧ l.erasep p = l₁ ++ l₂ :=
begin
by_cases h : ∃ a ∈ l, p a,
{ rcases h with ⟨a, ha, pa⟩,
exact or.inr (exists_of_erasep ha pa) },
{ simp at h, exact or.inl (erasep_of_forall_not h) }
end
@[simp] theorem length_erasep_of_mem {l : list α} {a} (al : a ∈ l) (pa : p a) :
length (l.erasep p) = pred (length l) :=
by rcases exists_of_erasep al pa with ⟨_, l₁, l₂, _, _, e₁, e₂⟩;
rw e₂; simp [-add_comm, e₁]; refl
theorem erasep_append_left {a : α} (pa : p a) :
∀ {l₁ : list α} (l₂), a ∈ l₁ → (l₁++l₂).erasep p = l₁.erasep p ++ l₂
| (x::xs) l₂ h := begin
by_cases h' : p x; simp [h'],
rw erasep_append_left l₂ (mem_of_ne_of_mem (mt _ h') h),
rintro rfl, exact pa
end
theorem erasep_append_right :
∀ {l₁ : list α} (l₂), (∀ b ∈ l₁, ¬ p b) → (l₁++l₂).erasep p = l₁ ++ l₂.erasep p
| [] l₂ h := rfl
| (x::xs) l₂ h := by simp [(forall_mem_cons.1 h).1,
erasep_append_right _ (forall_mem_cons.1 h).2]
theorem erasep_sublist (l : list α) : l.erasep p <+ l :=
by rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩;
[rw h, {rw [h₄, h₃], simp}]
theorem erasep_subset (l : list α) : l.erasep p ⊆ l :=
(erasep_sublist l).subset
theorem sublist.erasep {l₁ l₂ : list α} (s : l₁ <+ l₂) : l₁.erasep p <+ l₂.erasep p :=
begin
induction s,
case list.sublist.slnil { refl },
case list.sublist.cons : l₁ l₂ a s IH {
by_cases h : p a; simp [h],
exacts [IH.trans (erasep_sublist _), IH.cons _ _ _] },
case list.sublist.cons2 : l₁ l₂ a s IH {
by_cases h : p a; simp [h],
exacts [s, IH.cons2 _ _ _] }
end
theorem mem_of_mem_erasep {a : α} {l : list α} : a ∈ l.erasep p → a ∈ l :=
@erasep_subset _ _ _ _ _
@[simp] theorem mem_erasep_of_neg {a : α} {l : list α} (pa : ¬ p a) : a ∈ l.erasep p ↔ a ∈ l :=
⟨mem_of_mem_erasep, λ al, begin
rcases exists_or_eq_self_of_erasep p l with h | ⟨c, l₁, l₂, h₁, h₂, h₃, h₄⟩,
{ rwa h },
{ rw h₄, rw h₃ at al,
have : a ≠ c, {rintro rfl, exact pa.elim h₂},
simpa [this] using al }
end⟩
theorem erasep_map (f : β → α) :
∀ (l : list β), (map f l).erasep p = map f (l.erasep (p ∘ f))
| [] := rfl
| (b::l) := by by_cases p (f b); simp [h, erasep_map l]
@[simp] theorem extractp_eq_find_erasep :
∀ l : list α, extractp p l = (find p l, erasep p l)
| [] := rfl
| (a::l) := by by_cases pa : p a; simp [extractp, pa, extractp_eq_find_erasep l]
end erasep
/-! ### erase -/
section erase
variable [decidable_eq α]
@[simp] theorem erase_nil (a : α) : [].erase a = [] := rfl
theorem erase_cons (a b : α) (l : list α) :
(b :: l).erase a = if b = a then l else b :: l.erase a := rfl
@[simp] theorem erase_cons_head (a : α) (l : list α) : (a :: l).erase a = l :=
by simp only [erase_cons, if_pos rfl]
@[simp] theorem erase_cons_tail {a b : α} (l : list α) (h : b ≠ a) :
(b::l).erase a = b :: l.erase a :=
by simp only [erase_cons, if_neg h]; split; refl
theorem erase_eq_erasep (a : α) (l : list α) : l.erase a = l.erasep (eq a) :=
by { induction l with b l, {refl},
by_cases a = b; [simp [h], simp [h, ne.symm h, *]] }
@[simp, priority 980]
theorem erase_of_not_mem {a : α} {l : list α} (h : a ∉ l) : l.erase a = l :=
by rw [erase_eq_erasep, erasep_of_forall_not]; rintro b h' rfl; exact h h'
theorem exists_erase_eq {a : α} {l : list α} (h : a ∈ l) :
∃ l₁ l₂, a ∉ l₁ ∧ l = l₁ ++ a :: l₂ ∧ l.erase a = l₁ ++ l₂ :=
by rcases exists_of_erasep h rfl with ⟨_, l₁, l₂, h₁, rfl, h₂, h₃⟩;
rw erase_eq_erasep; exact ⟨l₁, l₂, λ h, h₁ _ h rfl, h₂, h₃⟩
@[simp] theorem length_erase_of_mem {a : α} {l : list α} (h : a ∈ l) :
length (l.erase a) = pred (length l) :=
by rw erase_eq_erasep; exact length_erasep_of_mem h rfl
theorem erase_append_left {a : α} {l₁ : list α} (l₂) (h : a ∈ l₁) :
(l₁++l₂).erase a = l₁.erase a ++ l₂ :=
by simp [erase_eq_erasep]; exact erasep_append_left (by refl) l₂ h
theorem erase_append_right {a : α} {l₁ : list α} (l₂) (h : a ∉ l₁) :
(l₁++l₂).erase a = l₁ ++ l₂.erase a :=
by rw [erase_eq_erasep, erase_eq_erasep, erasep_append_right];
rintro b h' rfl; exact h h'
theorem erase_sublist (a : α) (l : list α) : l.erase a <+ l :=
by rw erase_eq_erasep; apply erasep_sublist
theorem erase_subset (a : α) (l : list α) : l.erase a ⊆ l :=
(erase_sublist a l).subset
theorem sublist.erase (a : α) {l₁ l₂ : list α} (h : l₁ <+ l₂) : l₁.erase a <+ l₂.erase a :=
by simp [erase_eq_erasep]; exact sublist.erasep h
theorem mem_of_mem_erase {a b : α} {l : list α} : a ∈ l.erase b → a ∈ l :=
@erase_subset _ _ _ _ _
@[simp] theorem mem_erase_of_ne {a b : α} {l : list α} (ab : a ≠ b) : a ∈ l.erase b ↔ a ∈ l :=
by rw erase_eq_erasep; exact mem_erasep_of_neg ab.symm
theorem erase_comm (a b : α) (l : list α) : (l.erase a).erase b = (l.erase b).erase a :=
if ab : a = b then by rw ab else
if ha : a ∈ l then
if hb : b ∈ l then match l, l.erase a, exists_erase_eq ha, hb with
| ._, ._, ⟨l₁, l₂, ha', rfl, rfl⟩, hb :=
if h₁ : b ∈ l₁ then
by rw [erase_append_left _ h₁, erase_append_left _ h₁,
erase_append_right _ (mt mem_of_mem_erase ha'), erase_cons_head]
else
by rw [erase_append_right _ h₁, erase_append_right _ h₁, erase_append_right _ ha',
erase_cons_tail _ ab, erase_cons_head]
end
else by simp only [erase_of_not_mem hb, erase_of_not_mem (mt mem_of_mem_erase hb)]
else by simp only [erase_of_not_mem ha, erase_of_not_mem (mt mem_of_mem_erase ha)]
theorem map_erase [decidable_eq β] {f : α → β} (finj : injective f) {a : α}
(l : list α) : map f (l.erase a) = (map f l).erase (f a) :=
by rw [erase_eq_erasep, erase_eq_erasep, erasep_map]; congr;
ext b; simp [finj.eq_iff]
theorem map_foldl_erase [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} :
map f (foldl list.erase l₁ l₂) = foldl (λ l a, l.erase (f a)) (map f l₁) l₂ :=
by induction l₂ generalizing l₁; [refl,
simp only [foldl_cons, map_erase finj, *]]
@[simp] theorem count_erase_self (a : α) :
∀ (s : list α), count a (list.erase s a) = pred (count a s)
| [] := by simp
| (h :: t) :=
begin
rw erase_cons,
by_cases p : h = a,
{ rw [if_pos p, count_cons', if_pos p.symm], simp },
{ rw [if_neg p, count_cons', count_cons', if_neg (λ x : a = h, p x.symm), count_erase_self],
simp, }
end
@[simp] theorem count_erase_of_ne {a b : α} (ab : a ≠ b) :
∀ (s : list α), count a (list.erase s b) = count a s
| [] := by simp
| (x :: xs) :=
begin
rw erase_cons,
split_ifs with h,
{ rw [count_cons', h, if_neg ab], simp },
{ rw [count_cons', count_cons', count_erase_of_ne] }
end
end erase
/-! ### diff -/
section diff
variable [decidable_eq α]
@[simp] theorem diff_nil (l : list α) : l.diff [] = l := rfl
@[simp] theorem diff_cons (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.erase a).diff l₂ :=
if h : a ∈ l₁ then by simp only [list.diff, if_pos h]
else by simp only [list.diff, if_neg h, erase_of_not_mem h]
lemma diff_cons_right (l₁ l₂ : list α) (a : α) : l₁.diff (a::l₂) = (l₁.diff l₂).erase a :=
begin
induction l₂ with b l₂ ih generalizing l₁ a,
{ simp_rw [diff_cons, diff_nil] },
{ rw [diff_cons, diff_cons, erase_comm, ← diff_cons, ih, ← diff_cons] }
end
lemma diff_erase (l₁ l₂ : list α) (a : α) : (l₁.diff l₂).erase a = (l₁.erase a).diff l₂ :=
by rw [← diff_cons_right, diff_cons]
@[simp] theorem nil_diff (l : list α) : [].diff l = [] :=
by induction l; [refl, simp only [*, diff_cons, erase_of_not_mem (not_mem_nil _)]]
theorem diff_eq_foldl : ∀ (l₁ l₂ : list α), l₁.diff l₂ = foldl list.erase l₁ l₂
| l₁ [] := rfl
| l₁ (a::l₂) := (diff_cons l₁ l₂ a).trans (diff_eq_foldl _ _)
@[simp] theorem diff_append (l₁ l₂ l₃ : list α) : l₁.diff (l₂ ++ l₃) = (l₁.diff l₂).diff l₃ :=
by simp only [diff_eq_foldl, foldl_append]
@[simp] theorem map_diff [decidable_eq β] {f : α → β} (finj : injective f) {l₁ l₂ : list α} :
map f (l₁.diff l₂) = (map f l₁).diff (map f l₂) :=
by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj]
theorem diff_sublist : ∀ l₁ l₂ : list α, l₁.diff l₂ <+ l₁
| l₁ [] := sublist.refl _
| l₁ (a::l₂) := calc l₁.diff (a :: l₂) = (l₁.erase a).diff l₂ : diff_cons _ _ _
... <+ l₁.erase a : diff_sublist _ _
... <+ l₁ : list.erase_sublist _ _
theorem diff_subset (l₁ l₂ : list α) : l₁.diff l₂ ⊆ l₁ :=
(diff_sublist _ _).subset
theorem mem_diff_of_mem {a : α} : ∀ {l₁ l₂ : list α}, a ∈ l₁ → a ∉ l₂ → a ∈ l₁.diff l₂
| l₁ [] h₁ h₂ := h₁
| l₁ (b::l₂) h₁ h₂ := by rw diff_cons; exact
mem_diff_of_mem ((mem_erase_of_ne (ne_of_not_mem_cons h₂)).2 h₁) (not_mem_of_not_mem_cons h₂)
theorem sublist.diff_right : ∀ {l₁ l₂ l₃: list α}, l₁ <+ l₂ → l₁.diff l₃ <+ l₂.diff l₃
| l₁ l₂ [] h := h
| l₁ l₂ (a::l₃) h := by simp only
[diff_cons, (h.erase _).diff_right]
theorem erase_diff_erase_sublist_of_sublist {a : α} : ∀ {l₁ l₂ : list α},
l₁ <+ l₂ → (l₂.erase a).diff (l₁.erase a) <+ l₂.diff l₁
| [] l₂ h := erase_sublist _ _
| (b::l₁) l₂ h := if heq : b = a then by simp only [heq, erase_cons_head, diff_cons]
else by simpa only [erase_cons_head, erase_cons_tail _ heq, diff_cons,
erase_comm a b l₂]
using erase_diff_erase_sublist_of_sublist (h.erase b)
end diff
/-! ### enum -/
theorem length_enum_from : ∀ n (l : list α), length (enum_from n l) = length l
| n [] := rfl
| n (a::l) := congr_arg nat.succ (length_enum_from _ _)
theorem length_enum : ∀ (l : list α), length (enum l) = length l := length_enum_from _
@[simp] theorem enum_from_nth : ∀ n (l : list α) m,
nth (enum_from n l) m = (λ a, (n + m, a)) <$> nth l m
| n [] m := rfl
| n (a :: l) 0 := rfl
| n (a :: l) (m+1) := (enum_from_nth (n+1) l m).trans $
by rw [add_right_comm]; refl
@[simp] theorem enum_nth : ∀ (l : list α) n,
nth (enum l) n = (λ a, (n, a)) <$> nth l n :=
by simp only [enum, enum_from_nth, zero_add]; intros; refl
@[simp] theorem enum_from_map_snd : ∀ n (l : list α),
map prod.snd (enum_from n l) = l
| n [] := rfl
| n (a :: l) := congr_arg (cons _) (enum_from_map_snd _ _)
@[simp] theorem enum_map_snd : ∀ (l : list α),
map prod.snd (enum l) = l := enum_from_map_snd _
theorem mem_enum_from {x : α} {i : ℕ} :
∀ {j : ℕ} (xs : list α), (i, x) ∈ xs.enum_from j → j ≤ i ∧ i < j + xs.length ∧ x ∈ xs
| j [] := by simp [enum_from]
| j (y :: ys) :=
suffices i = j ∧ x = y ∨ (i, x) ∈ enum_from (j + 1) ys →
j ≤ i ∧ i < j + (length ys + 1) ∧ (x = y ∨ x ∈ ys),
by simpa [enum_from, mem_enum_from ys],
begin
rintro (h|h),
{ refine ⟨le_of_eq h.1.symm,h.1 ▸ _,or.inl h.2⟩,
apply nat.lt_add_of_pos_right; simp },
{ obtain ⟨hji, hijlen, hmem⟩ := mem_enum_from _ h,
refine ⟨_, _, _⟩,
{ exact le_trans (nat.le_succ _) hji },
{ convert hijlen using 1, ac_refl },
{ simp [hmem] } }
end
/-! ### product -/
@[simp] theorem nil_product (l : list β) : product (@nil α) l = [] := rfl
@[simp] theorem product_cons (a : α) (l₁ : list α) (l₂ : list β)
: product (a::l₁) l₂ = map (λ b, (a, b)) l₂ ++ product l₁ l₂ := rfl
@[simp] theorem product_nil : ∀ (l : list α), product l (@nil β) = []
| [] := rfl
| (a::l) := by rw [product_cons, product_nil]; refl
@[simp] theorem mem_product {l₁ : list α} {l₂ : list β} {a : α} {b : β} :
(a, b) ∈ product l₁ l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ :=
by simp only [product, mem_bind, mem_map, prod.ext_iff, exists_prop,
and.left_comm, exists_and_distrib_left, exists_eq_left, exists_eq_right]
theorem length_product (l₁ : list α) (l₂ : list β) :
length (product l₁ l₂) = length l₁ * length l₂ :=
by induction l₁ with x l₁ IH; [exact (zero_mul _).symm,
simp only [length, product_cons, length_append, IH,
right_distrib, one_mul, length_map, add_comm]]
/-! ### sigma -/
section
variable {σ : α → Type*}
@[simp] theorem nil_sigma (l : Π a, list (σ a)) : (@nil α).sigma l = [] := rfl
@[simp] theorem sigma_cons (a : α) (l₁ : list α) (l₂ : Π a, list (σ a))
: (a::l₁).sigma l₂ = map (sigma.mk a) (l₂ a) ++ l₁.sigma l₂ := rfl
@[simp] theorem sigma_nil : ∀ (l : list α), l.sigma (λ a, @nil (σ a)) = []
| [] := rfl
| (a::l) := by rw [sigma_cons, sigma_nil]; refl
@[simp] theorem mem_sigma {l₁ : list α} {l₂ : Π a, list (σ a)} {a : α} {b : σ a} :
sigma.mk a b ∈ l₁.sigma l₂ ↔ a ∈ l₁ ∧ b ∈ l₂ a :=
by simp only [list.sigma, mem_bind, mem_map, exists_prop, exists_and_distrib_left,
and.left_comm, exists_eq_left, heq_iff_eq, exists_eq_right]
theorem length_sigma (l₁ : list α) (l₂ : Π a, list (σ a)) :
length (l₁.sigma l₂) = (l₁.map (λ a, length (l₂ a))).sum :=
by induction l₁ with x l₁ IH; [refl,
simp only [map, sigma_cons, length_append, length_map, IH, sum_cons]]
end
section choose
variables (p : α → Prop) [decidable_pred p] (l : list α)
lemma choose_spec (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : ∃ a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1
lemma choose_property (hp : ∃ a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
/-! ### map₂_left' -/
section map₂_left'
-- The definitional equalities for `map₂_left'` can already be used by the
-- simplifie because `map₂_left'` is marked `@[simp]`.
@[simp] theorem map₂_left'_nil_right (f : α → option β → γ) (as) :
map₂_left' f as [] = (as.map (λ a, f a none), []) :=
by cases as; refl
end map₂_left'
/-! ### map₂_right' -/
section map₂_right'
variables (f : option α → β → γ) (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem map₂_right'_nil_left :
map₂_right' f [] bs = (bs.map (f none), []) :=
by cases bs; refl
@[simp] theorem map₂_right'_nil_right :
map₂_right' f as [] = ([], as) :=
rfl
@[simp] theorem map₂_right'_nil_cons :
map₂_right' f [] (b :: bs) = (f none b :: bs.map (f none), []) :=
rfl
@[simp] theorem map₂_right'_cons_cons :
map₂_right' f (a :: as) (b :: bs) =
let rec := map₂_right' f as bs in
(f (some a) b :: rec.fst, rec.snd) :=
rfl
end map₂_right'
/-! ### zip_left' -/
section zip_left'
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_left'_nil_right :
zip_left' as ([] : list β) = (as.map (λ a, (a, none)), []) :=
by cases as; refl
@[simp] theorem zip_left'_nil_left :
zip_left' ([] : list α) bs = ([], bs) :=
rfl
@[simp] theorem zip_left'_cons_nil :
zip_left' (a :: as) ([] : list β) = ((a, none) :: as.map (λ a, (a, none)), []) :=
rfl
@[simp] theorem zip_left'_cons_cons :
zip_left' (a :: as) (b :: bs) =
let rec := zip_left' as bs in
((a, some b) :: rec.fst, rec.snd) :=
rfl
end zip_left'
/-! ### zip_right' -/
section zip_right'
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_right'_nil_left :
zip_right' ([] : list α) bs = (bs.map (λ b, (none, b)), []) :=
by cases bs; refl
@[simp] theorem zip_right'_nil_right :
zip_right' as ([] : list β) = ([], as) :=
rfl
@[simp] theorem zip_right'_nil_cons :
zip_right' ([] : list α) (b :: bs) = ((none, b) :: bs.map (λ b, (none, b)), []) :=
rfl
@[simp] theorem zip_right'_cons_cons :
zip_right' (a :: as) (b :: bs) =
let rec := zip_right' as bs in
((some a, b) :: rec.fst, rec.snd) :=
rfl
end zip_right'
/-! ### map₂_left -/
section map₂_left
variables (f : α → option β → γ) (as : list α)
-- The definitional equalities for `map₂_left` can already be used by the
-- simplifier because `map₂_left` is marked `@[simp]`.
@[simp] theorem map₂_left_nil_right :
map₂_left f as [] = as.map (λ a, f a none) :=
by cases as; refl
theorem map₂_left_eq_map₂_left' : ∀ as bs,
map₂_left f as bs = (map₂_left' f as bs).fst
| [] bs := by simp!
| (a :: as) [] := by simp!
| (a :: as) (b :: bs) := by simp! [*]
theorem map₂_left_eq_map₂ : ∀ as bs,
length as ≤ length bs →
map₂_left f as bs = map₂ (λ a b, f a (some b)) as bs
| [] [] h := by simp!
| [] (b :: bs) h := by simp!
| (a :: as) [] h := by { simp at h, contradiction }
| (a :: as) (b :: bs) h := by { simp at h, simp! [*] }
end map₂_left
/-! ### map₂_right -/
section map₂_right
variables (f : option α → β → γ) (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem map₂_right_nil_left :
map₂_right f [] bs = bs.map (f none) :=
by cases bs; refl
@[simp] theorem map₂_right_nil_right :
map₂_right f as [] = [] :=
rfl
@[simp] theorem map₂_right_nil_cons :
map₂_right f [] (b :: bs) = f none b :: bs.map (f none) :=
rfl
@[simp] theorem map₂_right_cons_cons :
map₂_right f (a :: as) (b :: bs) = f (some a) b :: map₂_right f as bs :=
rfl
theorem map₂_right_eq_map₂_right' :
map₂_right f as bs = (map₂_right' f as bs).fst :=
by simp only [map₂_right, map₂_right', map₂_left_eq_map₂_left']
theorem map₂_right_eq_map₂ (h : length bs ≤ length as) :
map₂_right f as bs = map₂ (λ a b, f (some a) b) as bs :=
begin
have : (λ a b, flip f a (some b)) = (flip (λ a b, f (some a) b)) := rfl,
simp only [map₂_right, map₂_left_eq_map₂, map₂_flip, *]
end
end map₂_right
/-! ### zip_left -/
section zip_left
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_left_nil_right :
zip_left as ([] : list β) = as.map (λ a, (a, none)) :=
by cases as; refl
@[simp] theorem zip_left_nil_left :
zip_left ([] : list α) bs = [] :=
rfl
@[simp] theorem zip_left_cons_nil :
zip_left (a :: as) ([] : list β) = (a, none) :: as.map (λ a, (a, none)) :=
rfl
@[simp] theorem zip_left_cons_cons :
zip_left (a :: as) (b :: bs) = (a, some b) :: zip_left as bs :=
rfl
theorem zip_left_eq_zip_left' :
zip_left as bs = (zip_left' as bs).fst :=
by simp only [zip_left, zip_left', map₂_left_eq_map₂_left']
end zip_left
/-! ### zip_right -/
section zip_right
variables (a : α) (as : list α) (b : β) (bs : list β)
@[simp] theorem zip_right_nil_left :
zip_right ([] : list α) bs = bs.map (λ b, (none, b)) :=
by cases bs; refl
@[simp] theorem zip_right_nil_right :
zip_right as ([] : list β) = [] :=
rfl
@[simp] theorem zip_right_nil_cons :
zip_right ([] : list α) (b :: bs) = (none, b) :: bs.map (λ b, (none, b)) :=
rfl
@[simp] theorem zip_right_cons_cons :
zip_right (a :: as) (b :: bs) = (some a, b) :: zip_right as bs :=
rfl
theorem zip_right_eq_zip_right' :
zip_right as bs = (zip_right' as bs).fst :=
by simp only [zip_right, zip_right', map₂_right_eq_map₂_right']
end zip_right
/-! ### to_chunks -/
section to_chunks
@[simp] theorem to_chunks_nil (n) : @to_chunks α n [] = [] := by cases n; refl
theorem to_chunks_aux_eq (n) : ∀ xs i,
@to_chunks_aux α n xs i = (xs.take i, (xs.drop i).to_chunks (n+1))
| [] i := by cases i; refl
| (x::xs) 0 := by rw [to_chunks_aux, drop, to_chunks]; cases to_chunks_aux n xs n; refl
| (x::xs) (i+1) := by rw [to_chunks_aux, to_chunks_aux_eq]; refl
theorem to_chunks_eq_cons' (n) : ∀ {xs : list α} (h : xs ≠ []),
xs.to_chunks (n+1) = xs.take (n+1) :: (xs.drop (n+1)).to_chunks (n+1)
| [] e := (e rfl).elim
| (x::xs) _ := by rw [to_chunks, to_chunks_aux_eq]; refl
theorem to_chunks_eq_cons : ∀ {n} {xs : list α} (n0 : n ≠ 0) (x0 : xs ≠ []),
xs.to_chunks n = xs.take n :: (xs.drop n).to_chunks n
| 0 _ e := (e rfl).elim
| (n+1) xs _ := to_chunks_eq_cons' _
theorem to_chunks_aux_join {n} : ∀ {xs i l L}, @to_chunks_aux α n xs i = (l, L) → l ++ L.join = xs
| [] _ _ _ rfl := rfl
| (x::xs) i l L e := begin
cases i; [
cases e' : to_chunks_aux n xs n with l L,
cases e' : to_chunks_aux n xs i with l L];
{ rw [to_chunks_aux, e', to_chunks_aux] at e, cases e,
exact (congr_arg (cons x) (to_chunks_aux_join e') : _) }
end
@[simp] theorem to_chunks_join : ∀ n xs, (@to_chunks α n xs).join = xs
| n [] := by cases n; refl
| 0 (x::xs) := by simp only [to_chunks, join]; rw append_nil
| (n+1) (x::xs) := begin
rw to_chunks,
cases e : to_chunks_aux n xs n with l L,
exact (congr_arg (cons x) (to_chunks_aux_join e) : _),
end
theorem to_chunks_length_le : ∀ n xs, n ≠ 0 → ∀ l : list α,
l ∈ @to_chunks α n xs → l.length ≤ n
| 0 _ e _ := (e rfl).elim
| (n+1) xs _ l := begin
refine (measure_wf length).induction xs _, intros xs IH h,
by_cases x0 : xs = [], {subst xs, cases h},
rw to_chunks_eq_cons' _ x0 at h, rcases h with rfl|h,
{ apply length_take_le },
{ refine IH _ _ h,
simp only [measure, inv_image, length_drop],
exact tsub_lt_self (length_pos_iff_ne_nil.2 x0) (succ_pos _) },
end
end to_chunks
/-! ### Miscellaneous lemmas -/
theorem ilast'_mem : ∀ a l, @ilast' α a l ∈ a :: l
| a [] := or.inl rfl
| a (b::l) := or.inr (ilast'_mem b l)
@[simp] lemma nth_le_attach (L : list α) (i) (H : i < L.attach.length) :
(L.attach.nth_le i H).1 = L.nth_le i (length_attach L ▸ H) :=
calc (L.attach.nth_le i H).1
= (L.attach.map subtype.val).nth_le i (by simpa using H) : by rw nth_le_map'
... = L.nth_le i _ : by congr; apply attach_map_val
end list
@[to_additive]
theorem monoid_hom.map_list_prod {α β : Type*} [monoid α] [monoid β] (f : α →* β) (l : list α) :
f l.prod = (l.map f).prod :=
(l.prod_hom f).symm
open opposite
/-- A morphism into the opposite monoid acts on the product by acting on the reversed elements -/
lemma monoid_hom.unop_map_list_prod {α β : Type*} [monoid α] [monoid β] (f : α →* βᵒᵖ) (l : list α):
unop (f l.prod) = (l.map (unop ∘ f)).reverse.prod :=
begin
rw [f.map_list_prod l, opposite.unop_list_prod, list.map_map],
end
namespace list
@[to_additive]
theorem prod_map_hom {α β γ : Type*} [monoid β] [monoid γ] (L : list α) (f : α → β) (g : β →* γ) :
(L.map (g ∘ f)).prod = g ((L.map f).prod) :=
by {rw g.map_list_prod, exact congr_arg _ (map_map _ _ _).symm}
theorem sum_map_mul_left {α : Type*} [semiring α] {β : Type*} (L : list β)
(f : β → α) (r : α) :
(L.map (λ b, r * f b)).sum = r * (L.map f).sum :=
sum_map_hom L f $ add_monoid_hom.mul_left r
theorem sum_map_mul_right {α : Type*} [semiring α] {β : Type*} (L : list β)
(f : β → α) (r : α) :
(L.map (λ b, f b * r)).sum = (L.map f).sum * r :=
sum_map_hom L f $ add_monoid_hom.mul_right r
universes u v
@[simp]
theorem mem_map_swap {α : Type u} {β : Type v} (x : α) (y : β) (xs : list (α × β)) :
(y, x) ∈ map prod.swap xs ↔ (x, y) ∈ xs :=
begin
induction xs with x xs,
{ simp only [not_mem_nil, map_nil] },
{ cases x with a b,
simp only [mem_cons_iff, prod.mk.inj_iff, map, prod.swap_prod_mk,
prod.exists, xs_ih, and_comm] },
end
lemma slice_eq {α} (xs : list α) (n m : ℕ) :
slice n m xs = xs.take n ++ xs.drop (n+m) :=
begin
induction n generalizing xs,
{ simp [slice] },
{ cases xs; simp [slice, *, nat.succ_add], }
end
lemma sizeof_slice_lt {α} [has_sizeof α] (i j : ℕ) (hj : 0 < j) (xs : list α) (hi : i < xs.length) :
sizeof (list.slice i j xs) < sizeof xs :=
begin
induction xs generalizing i j,
case list.nil : i j h
{ cases hi },
case list.cons : x xs xs_ih i j h
{ cases i; simp only [-slice_eq, list.slice],
{ cases j, cases h,
dsimp only [drop], unfold_wf,
apply @lt_of_le_of_lt _ _ _ xs.sizeof,
{ clear_except,
induction xs generalizing j; unfold_wf,
case list.nil : j
{ refl },
case list.cons : xs_hd xs_tl xs_ih j
{ cases j; unfold_wf, refl,
transitivity, apply xs_ih,
simp }, },
unfold_wf, apply zero_lt_one_add, },
{ unfold_wf, apply xs_ih _ _ h,
apply lt_of_succ_lt_succ hi, } },
end
end list
|
2bf0d4ecc5491219552e991198e25b8c2b2d3483 | a46270e2f76a375564f3b3e9c1bf7b635edc1f2c | /11.2.lean | e1cf84ca0e8039f2e62e49603b8cd3f3f4cc7ef2 | [
"CC0-1.0"
] | permissive | wudcscheme/lean-exercise | 88ea2506714eac343de2a294d1132ee8ee6d3a20 | 5b23b9be3d361fff5e981d5be3a0a1175504b9f6 | refs/heads/master | 1,678,958,930,293 | 1,583,197,205,000 | 1,583,197,205,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 552 | lean | variables a b c d e : Prop
variable p : Prop → Prop
theorem thm₁ (h : a ↔ b) : (c ∧ a ∧ d → e) ↔ (c ∧ b ∧ d → e) :=
propext h ▸ iff.refl _
theorem thm₂ (h : a ↔ b) (h₁ : p a) : p b :=
propext h ▸ h₁
-- BEGIN
#print axioms thm₁ -- propext
#print axioms thm₂ -- propext
-- END
#check propext
#check thm₂
theorem pe_from_thm₂ {a b: Prop}: (a ↔ b) -> a = b := begin
intro h,
let P: Prop -> Prop := λ x, a = x,
have: implies (a = a) (a = b), from @thm₂ a b P h,
from this rfl,
end
|
29adf75fe2c84d2029e22f650088bb77e4140d35 | 97f752b44fd85ec3f635078a2dd125ddae7a82b6 | /hott/homotopy/sphere.hlean | 19c46b28aa454f119912a4fa2f16b93d1a296a4d | [
"Apache-2.0"
] | permissive | tectronics/lean | ab977ba6be0fcd46047ddbb3c8e16e7c26710701 | f38af35e0616f89c6e9d7e3eb1d48e47ee666efe | refs/heads/master | 1,532,358,526,384 | 1,456,276,623,000 | 1,456,276,623,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,868 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
Declaration of the n-spheres
-/
import .susp types.trunc
open eq nat susp bool is_trunc unit pointed
/-
We can define spheres with the following possible indices:
- trunc_index (defining S^-2 = S^-1 = empty)
- nat (forgetting that S^-1 = empty)
- nat, but counting wrong (S^0 = empty, S^1 = bool, ...)
- some new type "integers >= -1"
We choose the last option here.
-/
/- Sphere levels -/
inductive sphere_index : Type₀ :=
| minus_one : sphere_index
| succ : sphere_index → sphere_index
namespace trunc_index
definition sub_one [reducible] (n : sphere_index) : trunc_index :=
sphere_index.rec_on n -2 (λ n k, k.+1)
postfix `.-1`:(max+1) := sub_one
end trunc_index
namespace sphere_index
/-
notation for sphere_index is -1, 0, 1, ...
from 0 and up this comes from a coercion from num to sphere_index (via nat)
-/
definition has_zero_sphere_index [instance] [reducible] : has_zero sphere_index :=
has_zero.mk (succ minus_one)
definition has_one_sphere_index [instance] [reducible] : has_one sphere_index :=
has_one.mk (succ (succ minus_one))
postfix `.+1`:(max+1) := sphere_index.succ
postfix `.+2`:(max+1) := λ(n : sphere_index), (n .+1 .+1)
notation `-1` := minus_one
notation `ℕ₋₁` := sphere_index
definition add (n m : sphere_index) : sphere_index :=
sphere_index.rec_on m n (λ k l, l .+1)
definition leq (n m : sphere_index) : Type₀ :=
sphere_index.rec_on n (λm, unit) (λ n p m, sphere_index.rec_on m (λ p, empty) (λ m q p, p m) p) m
infix `+1+`:65 := sphere_index.add
definition has_le_sphere_index [instance] [reducible] : has_le sphere_index :=
has_le.mk leq
definition succ_le_succ {n m : sphere_index} (H : n ≤ m) : n.+1 ≤ m.+1 := proof H qed
definition le_of_succ_le_succ {n m : sphere_index} (H : n.+1 ≤ m.+1) : n ≤ m := proof H qed
definition minus_two_le (n : sphere_index) : -1 ≤ n := star
definition empty_of_succ_le_minus_two {n : sphere_index} (H : n .+1 ≤ -1) : empty := H
definition of_nat [coercion] [reducible] (n : nat) : sphere_index :=
(nat.rec_on n -1 (λ n k, k.+1)).+1
definition trunc_index_of_sphere_index [coercion] [reducible] (n : sphere_index) : trunc_index :=
(sphere_index.rec_on n -2 (λ n k, k.+1)).+1
definition sub_one [reducible] (n : ℕ) : sphere_index :=
nat.rec_on n -1 (λ n k, k.+1)
postfix `.-1`:(max+1) := sub_one
open trunc_index
definition sub_two_eq_sub_one_sub_one (n : ℕ) : n.-2 = n.-1.-1 :=
nat.rec_on n idp (λn p, ap trunc_index.succ p)
end sphere_index
open sphere_index equiv
definition sphere : sphere_index → Type₀
| -1 := empty
| n.+1 := susp (sphere n)
namespace sphere
definition base {n : ℕ} : sphere n := north
definition pointed_sphere [instance] [constructor] (n : ℕ) : pointed (sphere n) :=
pointed.mk base
definition psphere [constructor] (n : ℕ) : Type* := pointed.mk' (sphere n)
namespace ops
abbreviation S := sphere
notation `S.` := psphere
end ops
open sphere.ops
definition equator (n : ℕ) : map₊ (S. n) (Ω (S. (succ n))) :=
pmap.mk (λa, merid a ⬝ (merid base)⁻¹) !con.right_inv
definition surf {n : ℕ} : Ω[n] S. n :=
nat.rec_on n (proof base qed)
(begin intro m s, refine cast _ (apn m (equator m) s),
exact ap carrier !loop_space_succ_eq_in⁻¹ end)
definition bool_of_sphere : S 0 → bool :=
proof susp.rec ff tt (λx, empty.elim x) qed
definition sphere_of_bool : bool → S 0
| ff := proof north qed
| tt := proof south qed
definition sphere_equiv_bool : S 0 ≃ bool :=
equiv.MK bool_of_sphere
sphere_of_bool
(λb, match b with | tt := idp | ff := idp end)
(λx, proof susp.rec_on x idp idp (empty.rec _) qed)
definition sphere_eq_bool : S 0 = bool :=
ua sphere_equiv_bool
definition sphere_eq_bool_pointed : S. 0 = pbool :=
pType_eq sphere_equiv_bool idp
-- TODO: the commented-out part makes the forward function below "apn _ surf"
definition pmap_sphere (A : Type*) (n : ℕ) : map₊ (S. n) A ≃ Ω[n] A :=
begin
-- fapply equiv_change_fun,
-- {
revert A, induction n with n IH: intro A,
{ rewrite [sphere_eq_bool_pointed], apply pmap_bool_equiv},
{ refine susp_adjoint_loop (S. n) A ⬝e !IH ⬝e _, rewrite [loop_space_succ_eq_in]}
-- },
-- { intro f, exact apn n f surf},
-- { revert A, induction n with n IH: intro A f,
-- { exact sorry},
-- { exact sorry}}
end
protected definition elim {n : ℕ} {P : Type*} (p : Ω[n] P) : map₊ (S. n) P :=
to_inv !pmap_sphere p
-- definition elim_surf {n : ℕ} {P : Type*} (p : Ω[n] P) : apn n (sphere.elim p) surf = p :=
-- begin
-- induction n with n IH,
-- { esimp [apn,surf,sphere.elim,pmap_sphere], apply sorry},
-- { apply sorry}
-- end
end sphere
open sphere sphere.ops
namespace is_trunc
open trunc_index
variables {n : ℕ} {A : Type}
definition is_trunc_of_pmap_sphere_constant
(H : Π(a : A) (f : map₊ (S. n) (pointed.Mk a)) (x : S n), f x = f base) : is_trunc (n.-2.+1) A :=
begin
apply iff.elim_right !is_trunc_iff_is_contr_loop,
intro a,
apply is_trunc_equiv_closed, apply pmap_sphere,
fapply is_contr.mk,
{ exact pmap.mk (λx, a) idp},
{ intro f, fapply pmap_eq,
{ intro x, esimp, refine !respect_pt⁻¹ ⬝ (!H ⬝ !H⁻¹)},
{ rewrite [▸*,con.right_inv,▸*,con.left_inv]}}
end
definition is_trunc_iff_map_sphere_constant
(H : Π(f : S n → A) (x : S n), f x = f base) : is_trunc (n.-2.+1) A :=
begin
apply is_trunc_of_pmap_sphere_constant,
intros, cases f with f p, esimp at *, apply H
end
definition pmap_sphere_constant_of_is_trunc' [H : is_trunc (n.-2.+1) A]
(a : A) (f : map₊ (S. n) (pointed.Mk a)) (x : S n) : f x = f base :=
begin
let H' := iff.elim_left (is_trunc_iff_is_contr_loop n A) H a,
note H'' := @is_trunc_equiv_closed_rev _ _ _ !pmap_sphere H',
assert p : (f = pmap.mk (λx, f base) (respect_pt f)),
apply is_prop.elim,
exact ap10 (ap pmap.to_fun p) x
end
definition pmap_sphere_constant_of_is_trunc [H : is_trunc (n.-2.+1) A]
(a : A) (f : map₊ (S. n) (pointed.Mk a)) (x y : S n) : f x = f y :=
let H := pmap_sphere_constant_of_is_trunc' a f in !H ⬝ !H⁻¹
definition map_sphere_constant_of_is_trunc [H : is_trunc (n.-2.+1) A]
(f : S n → A) (x y : S n) : f x = f y :=
pmap_sphere_constant_of_is_trunc (f base) (pmap.mk f idp) x y
definition map_sphere_constant_of_is_trunc_self [H : is_trunc (n.-2.+1) A]
(f : S n → A) (x : S n) : map_sphere_constant_of_is_trunc f x x = idp :=
!con.right_inv
end is_trunc
|
77fe255dfa9285e787098d9f34b5e337358faea7 | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/category_theory/closed/cartesian.lean | fbc5d241430c26c13020a7451407cb7e1a481f4e | [
"Apache-2.0"
] | permissive | hjvromen/lewis | 40b035973df7c77ebf927afab7878c76d05ff758 | 105b675f73630f028ad5d890897a51b3c1146fb0 | refs/heads/master | 1,677,944,636,343 | 1,676,555,301,000 | 1,676,555,301,000 | 327,553,599 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 14,508 | lean | /-
Copyright (c) 2020 Bhavik Mehta, Edward Ayers, Thomas Read. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta, Edward Ayers, Thomas Read
-/
import category_theory.limits.shapes.finite_products
import category_theory.limits.preserves.shapes.binary_products
import category_theory.closed.monoidal
import category_theory.monoidal.of_has_finite_products
import category_theory.adjunction
import category_theory.adjunction.mates
import category_theory.epi_mono
/-!
# Cartesian closed categories
Given a category with finite products, the cartesian monoidal structure is provided by the local
instance `monoidal_of_has_finite_products`.
We define exponentiable objects to be closed objects with respect to this monoidal structure,
i.e. `(X × -)` is a left adjoint.
We say a category is cartesian closed if every object is exponentiable
(equivalently, that the category equipped with the cartesian monoidal structure is closed monoidal).
Show that exponential forms a difunctor and define the exponential comparison morphisms.
## TODO
Some of the results here are true more generally for closed objects and
for closed monoidal categories, and these could be generalised.
-/
universes v u u₂
noncomputable theory
namespace category_theory
open category_theory category_theory.category category_theory.limits
local attribute [instance] monoidal_of_has_finite_products
/--
An object `X` is *exponentiable* if `(X × -)` is a left adjoint.
We define this as being `closed` in the cartesian monoidal structure.
-/
abbreviation exponentiable {C : Type u} [category.{v} C] [has_finite_products C] (X : C) :=
closed X
/--
If `X` and `Y` are exponentiable then `X ⨯ Y` is.
This isn't an instance because it's not usually how we want to construct exponentials, we'll usually
prove all objects are exponential uniformly.
-/
def binary_product_exponentiable {C : Type u} [category.{v} C] [has_finite_products C] {X Y : C}
(hX : exponentiable X) (hY : exponentiable Y) : exponentiable (X ⨯ Y) :=
{ is_adj :=
begin
haveI := hX.is_adj,
haveI := hY.is_adj,
exact adjunction.left_adjoint_of_nat_iso (monoidal_category.tensor_left_tensor _ _).symm
end }
/--
The terminal object is always exponentiable.
This isn't an instance because most of the time we'll prove cartesian closed for all objects
at once, rather than just for this one.
-/
def terminal_exponentiable {C : Type u} [category.{v} C] [has_finite_products C] :
exponentiable ⊤_C :=
unit_closed
/--
A category `C` is cartesian closed if it has finite products and every object is exponentiable.
We define this as `monoidal_closed` with respect to the cartesian monoidal structure.
-/
abbreviation cartesian_closed (C : Type u) [category.{v} C] [has_finite_products C] :=
monoidal_closed C
variables {C : Type u} [category.{v} C] (A B : C) {X X' Y Y' Z : C}
section exp
variables [has_finite_products C] [exponentiable A]
/-- This is (-)^A. -/
def exp : C ⥤ C :=
(@closed.is_adj _ _ _ A _).right
/-- The adjunction between A ⨯ - and (-)^A. -/
def exp.adjunction : prod.functor.obj A ⊣ exp A :=
closed.is_adj.adj
/-- The evaluation natural transformation. -/
def ev : exp A ⋙ prod.functor.obj A ⟶ 𝟭 C :=
closed.is_adj.adj.counit
/-- The coevaluation natural transformation. -/
def coev : 𝟭 C ⟶ prod.functor.obj A ⋙ exp A :=
closed.is_adj.adj.unit
@[simp] lemma exp_adjunction_counit : (exp.adjunction A).counit = ev A := rfl
@[simp] lemma exp_adjunction_unit : (exp.adjunction A).unit = coev A := rfl
@[simp, reassoc]
lemma ev_naturality {X Y : C} (f : X ⟶ Y) :
limits.prod.map (𝟙 A) ((exp A).map f) ≫ (ev A).app Y = (ev A).app X ≫ f :=
(ev A).naturality f
@[simp, reassoc]
lemma coev_naturality {X Y : C} (f : X ⟶ Y) :
f ≫ (coev A).app Y = (coev A).app X ≫ (exp A).map (limits.prod.map (𝟙 A) f) :=
(coev A).naturality f
notation A ` ⟹ `:20 B:20 := (exp A).obj B
notation B ` ^^ `:30 A:30 := (exp A).obj B
@[simp, reassoc] lemma ev_coev :
limits.prod.map (𝟙 A) ((coev A).app B) ≫ (ev A).app (A ⨯ B) = 𝟙 (A ⨯ B) :=
adjunction.left_triangle_components (exp.adjunction A)
@[simp, reassoc] lemma coev_ev : (coev A).app (A⟹B) ≫ (exp A).map ((ev A).app B) = 𝟙 (A⟹B) :=
adjunction.right_triangle_components (exp.adjunction A)
instance : preserves_colimits (prod.functor.obj A) :=
(exp.adjunction A).left_adjoint_preserves_colimits
end exp
variables {A}
-- Wrap these in a namespace so we don't clash with the core versions.
namespace cartesian_closed
variables [has_finite_products C] [exponentiable A]
/-- Currying in a cartesian closed category. -/
def curry : (A ⨯ Y ⟶ X) → (Y ⟶ A ⟹ X) :=
(closed.is_adj.adj.hom_equiv _ _).to_fun
/-- Uncurrying in a cartesian closed category. -/
def uncurry : (Y ⟶ A ⟹ X) → (A ⨯ Y ⟶ X) :=
(closed.is_adj.adj.hom_equiv _ _).inv_fun
end cartesian_closed
open cartesian_closed
variables [has_finite_products C] [exponentiable A]
@[reassoc]
lemma curry_natural_left (f : X ⟶ X') (g : A ⨯ X' ⟶ Y) :
curry (limits.prod.map (𝟙 _) f ≫ g) = f ≫ curry g :=
adjunction.hom_equiv_naturality_left _ _ _
@[reassoc]
lemma curry_natural_right (f : A ⨯ X ⟶ Y) (g : Y ⟶ Y') :
curry (f ≫ g) = curry f ≫ (exp _).map g :=
adjunction.hom_equiv_naturality_right _ _ _
@[reassoc]
lemma uncurry_natural_right (f : X ⟶ A⟹Y) (g : Y ⟶ Y') :
uncurry (f ≫ (exp _).map g) = uncurry f ≫ g :=
adjunction.hom_equiv_naturality_right_symm _ _ _
@[reassoc]
lemma uncurry_natural_left (f : X ⟶ X') (g : X' ⟶ A⟹Y) :
uncurry (f ≫ g) = limits.prod.map (𝟙 _) f ≫ uncurry g :=
adjunction.hom_equiv_naturality_left_symm _ _ _
@[simp]
lemma uncurry_curry (f : A ⨯ X ⟶ Y) : uncurry (curry f) = f :=
(closed.is_adj.adj.hom_equiv _ _).left_inv f
@[simp]
lemma curry_uncurry (f : X ⟶ A⟹Y) : curry (uncurry f) = f :=
(closed.is_adj.adj.hom_equiv _ _).right_inv f
lemma curry_eq_iff (f : A ⨯ Y ⟶ X) (g : Y ⟶ A ⟹ X) :
curry f = g ↔ f = uncurry g :=
adjunction.hom_equiv_apply_eq _ f g
lemma eq_curry_iff (f : A ⨯ Y ⟶ X) (g : Y ⟶ A ⟹ X) :
g = curry f ↔ uncurry g = f :=
adjunction.eq_hom_equiv_apply _ f g
-- I don't think these two should be simp.
lemma uncurry_eq (g : Y ⟶ A ⟹ X) : uncurry g = limits.prod.map (𝟙 A) g ≫ (ev A).app X :=
adjunction.hom_equiv_counit _
lemma curry_eq (g : A ⨯ Y ⟶ X) : curry g = (coev A).app Y ≫ (exp A).map g :=
adjunction.hom_equiv_unit _
lemma uncurry_id_eq_ev (A X : C) [exponentiable A] : uncurry (𝟙 (A ⟹ X)) = (ev A).app X :=
by rw [uncurry_eq, prod.map_id_id, id_comp]
lemma curry_id_eq_coev (A X : C) [exponentiable A] : curry (𝟙 _) = (coev A).app X :=
by { rw [curry_eq, (exp A).map_id (A ⨯ _)], apply comp_id }
lemma curry_injective : function.injective (curry : (A ⨯ Y ⟶ X) → (Y ⟶ A ⟹ X)) :=
(closed.is_adj.adj.hom_equiv _ _).injective
lemma uncurry_injective : function.injective (uncurry : (Y ⟶ A ⟹ X) → (A ⨯ Y ⟶ X)) :=
(closed.is_adj.adj.hom_equiv _ _).symm.injective
/--
Show that the exponential of the terminal object is isomorphic to itself, i.e. `X^1 ≅ X`.
The typeclass argument is explicit: any instance can be used.
-/
def exp_terminal_iso_self [exponentiable ⊤_C] : (⊤_C ⟹ X) ≅ X :=
yoneda.ext (⊤_ C ⟹ X) X
(λ Y f, (prod.left_unitor Y).inv ≫ uncurry f)
(λ Y f, curry ((prod.left_unitor Y).hom ≫ f))
(λ Z g, by rw [curry_eq_iff, iso.hom_inv_id_assoc] )
(λ Z g, by simp)
(λ Z W f g, by rw [uncurry_natural_left, prod.left_unitor_inv_naturality_assoc f] )
/-- The internal element which points at the given morphism. -/
def internalize_hom (f : A ⟶ Y) : ⊤_C ⟶ (A ⟹ Y) :=
curry (limits.prod.fst ≫ f)
section pre
variables {B}
/-- Pre-compose an internal hom with an external hom. -/
def pre (f : B ⟶ A) [exponentiable B] : exp A ⟶ exp B :=
transfer_nat_trans_self (exp.adjunction _) (exp.adjunction _) (prod.functor.map f)
lemma prod_map_pre_app_comp_ev (f : B ⟶ A) [exponentiable B] (X : C) :
limits.prod.map (𝟙 B) ((pre f).app X) ≫ (ev B).app X =
limits.prod.map f (𝟙 (A ⟹ X)) ≫ (ev A).app X :=
transfer_nat_trans_self_counit _ _ (prod.functor.map f) X
lemma uncurry_pre (f : B ⟶ A) [exponentiable B] (X : C) :
uncurry ((pre f).app X) = limits.prod.map f (𝟙 _) ≫ (ev A).app X :=
begin
rw [uncurry_eq, prod_map_pre_app_comp_ev]
end
lemma coev_app_comp_pre_app (f : B ⟶ A) [exponentiable B] :
(coev A).app X ≫ (pre f).app (A ⨯ X) = (coev B).app X ≫ (exp B).map (limits.prod.map f (𝟙 _)) :=
unit_transfer_nat_trans_self _ _ (prod.functor.map f) X
@[simp]
lemma pre_id (A : C) [exponentiable A] : pre (𝟙 A) = 𝟙 _ :=
by simp [pre]
@[simp]
lemma pre_map {A₁ A₂ A₃ : C} [exponentiable A₁] [exponentiable A₂] [exponentiable A₃]
(f : A₁ ⟶ A₂) (g : A₂ ⟶ A₃) :
pre (f ≫ g) = pre g ≫ pre f :=
by rw [pre, pre, pre, transfer_nat_trans_self_comp, prod.functor.map_comp]
end pre
/-- The internal hom functor given by the cartesian closed structure. -/
def internal_hom [cartesian_closed C] : Cᵒᵖ ⥤ C ⥤ C :=
{ obj := λ X, exp X.unop,
map := λ X Y f, pre f.unop }
/-- If an initial object `I` exists in a CCC, then `A ⨯ I ≅ I`. -/
@[simps]
def zero_mul {I : C} (t : is_initial I) : A ⨯ I ≅ I :=
{ hom := limits.prod.snd,
inv := t.to _,
hom_inv_id' :=
begin
have: (limits.prod.snd : A ⨯ I ⟶ I) = uncurry (t.to _),
rw ← curry_eq_iff,
apply t.hom_ext,
rw [this, ← uncurry_natural_right, ← eq_curry_iff],
apply t.hom_ext,
end,
inv_hom_id' := t.hom_ext _ _ }
/-- If an initial object `0` exists in a CCC, then `0 ⨯ A ≅ 0`. -/
def mul_zero {I : C} (t : is_initial I) : I ⨯ A ≅ I :=
limits.prod.braiding _ _ ≪≫ zero_mul t
/-- If an initial object `0` exists in a CCC then `0^B ≅ 1` for any `B`. -/
def pow_zero {I : C} (t : is_initial I) [cartesian_closed C] : I ⟹ B ≅ ⊤_ C :=
{ hom := default _,
inv := curry ((mul_zero t).hom ≫ t.to _),
hom_inv_id' :=
begin
rw [← curry_natural_left, curry_eq_iff, ← cancel_epi (mul_zero t).inv],
{ apply t.hom_ext },
{ apply_instance },
{ apply_instance }
end }
-- TODO: Generalise the below to its commutated variants.
-- TODO: Define a distributive category, so that zero_mul and friends can be derived from this.
/-- In a CCC with binary coproducts, the distribution morphism is an isomorphism. -/
def prod_coprod_distrib [has_binary_coproducts C] [cartesian_closed C] (X Y Z : C) :
(Z ⨯ X) ⨿ (Z ⨯ Y) ≅ Z ⨯ (X ⨿ Y) :=
{ hom := coprod.desc (limits.prod.map (𝟙 _) coprod.inl) (limits.prod.map (𝟙 _) coprod.inr),
inv := uncurry (coprod.desc (curry coprod.inl) (curry coprod.inr)),
hom_inv_id' :=
begin
apply coprod.hom_ext,
rw [coprod.inl_desc_assoc, comp_id, ←uncurry_natural_left, coprod.inl_desc, uncurry_curry],
rw [coprod.inr_desc_assoc, comp_id, ←uncurry_natural_left, coprod.inr_desc, uncurry_curry],
end,
inv_hom_id' :=
begin
rw [← uncurry_natural_right, ←eq_curry_iff],
apply coprod.hom_ext,
rw [coprod.inl_desc_assoc, ←curry_natural_right, coprod.inl_desc, ←curry_natural_left, comp_id],
rw [coprod.inr_desc_assoc, ←curry_natural_right, coprod.inr_desc, ←curry_natural_left, comp_id],
end }
/--
If an initial object `I` exists in a CCC then it is a strict initial object,
i.e. any morphism to `I` is an iso.
This actually shows a slightly stronger version: any morphism to an initial object from an
exponentiable object is an isomorphism.
-/
def strict_initial {I : C} (t : is_initial I) (f : A ⟶ I) : is_iso f :=
begin
haveI : mono (limits.prod.lift (𝟙 A) f ≫ (zero_mul t).hom) := mono_comp _ _,
rw [zero_mul_hom, prod.lift_snd] at _inst,
haveI: split_epi f := ⟨t.to _, t.hom_ext _ _⟩,
apply is_iso_of_mono_of_split_epi
end
instance to_initial_is_iso [has_initial C] (f : A ⟶ ⊥_ C) : is_iso f :=
strict_initial initial_is_initial _
/-- If an initial object `0` exists in a CCC then every morphism from it is monic. -/
lemma initial_mono {I : C} (B : C) (t : is_initial I) [cartesian_closed C] : mono (t.to B) :=
⟨λ B g h _, by { haveI := strict_initial t g, haveI := strict_initial t h, exact eq_of_inv_eq_inv (t.hom_ext _ _) }⟩
instance initial.mono_to [has_initial C] (B : C) [cartesian_closed C] : mono (initial.to B) :=
initial_mono B initial_is_initial
variables {D : Type u₂} [category.{v} D]
section functor
variables [has_finite_products D]
/--
Transport the property of being cartesian closed across an equivalence of categories.
Note we didn't require any coherence between the choice of finite products here, since we transport
along the `prod_comparison` isomorphism.
-/
def cartesian_closed_of_equiv (e : C ≌ D) [h : cartesian_closed C] : cartesian_closed D :=
{ closed := λ X,
{ is_adj :=
begin
haveI q : exponentiable (e.inverse.obj X) := infer_instance,
have : is_left_adjoint (prod.functor.obj (e.inverse.obj X)) := q.is_adj,
have : e.functor ⋙ prod.functor.obj X ⋙ e.inverse ≅ prod.functor.obj (e.inverse.obj X),
apply nat_iso.of_components _ _,
intro Y,
{ apply as_iso (prod_comparison e.inverse X (e.functor.obj Y)) ≪≫ _,
apply prod.map_iso (iso.refl _) (e.unit_iso.app Y).symm },
{ intros Y Z g,
dsimp [prod_comparison],
simp [prod.comp_lift, ← e.inverse.map_comp, ← e.inverse.map_comp_assoc],
-- I wonder if it would be a good idea to make `map_comp` a simp lemma the other way round
dsimp, simp -- See note [dsimp, simp]
},
{ have : is_left_adjoint (e.functor ⋙ prod.functor.obj X ⋙ e.inverse) :=
by exactI adjunction.left_adjoint_of_nat_iso this.symm,
have : is_left_adjoint (e.inverse ⋙ e.functor ⋙ prod.functor.obj X ⋙ e.inverse) :=
by exactI adjunction.left_adjoint_of_comp e.inverse _,
have : (e.inverse ⋙ e.functor ⋙ prod.functor.obj X ⋙ e.inverse) ⋙ e.functor ≅
prod.functor.obj X,
{ apply iso_whisker_right e.counit_iso (prod.functor.obj X ⋙ e.inverse ⋙ e.functor) ≪≫ _,
change prod.functor.obj X ⋙ e.inverse ⋙ e.functor ≅ prod.functor.obj X,
apply iso_whisker_left (prod.functor.obj X) e.counit_iso, },
resetI,
apply adjunction.left_adjoint_of_nat_iso this },
end } }
end functor
end category_theory
|
50cb9e16f773e557bc2fd591fd1f66b82e520fd4 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/mv_polynomial/comap_auto.lean | 428c89b08804c050b6dd01f9a3551e8d7f082ae3 | [] | 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,685 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.mv_polynomial.rename
import Mathlib.PostPort
universes u_1 u_2 u_4 u_3
namespace Mathlib
/-!
# `comap` operation on `mv_polynomial`
This file defines the `comap` function on `mv_polynomial`.
`mv_polynomial.comap` is a low-tech example of a map of "algebraic varieties," modulo the fact that
`mathlib` does not yet define varieties.
## Notation
As in other polynomial files, we typically use the notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[comm_semiring R]` (the coefficients)
-/
namespace mv_polynomial
/--
Given an algebra hom `f : mv_polynomial σ R →ₐ[R] mv_polynomial τ R`
and a variable evaluation `v : τ → R`,
`comap f v` produces a variable evaluation `σ → R`.
-/
def comap {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R]
(f : alg_hom R (mv_polynomial σ R) (mv_polynomial τ R)) : (τ → R) → σ → R :=
fun (x : τ → R) (i : σ) => coe_fn (aeval x) (coe_fn f (X i))
@[simp] theorem comap_apply {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R]
(f : alg_hom R (mv_polynomial σ R) (mv_polynomial τ R)) (x : τ → R) (i : σ) :
comap f x i = coe_fn (aeval x) (coe_fn f (X i)) :=
rfl
@[simp] theorem comap_id_apply {σ : Type u_1} {R : Type u_4} [comm_semiring R] (x : σ → R) :
comap (alg_hom.id R (mv_polynomial σ R)) x = x :=
sorry
theorem comap_id (σ : Type u_1) (R : Type u_4) [comm_semiring R] :
comap (alg_hom.id R (mv_polynomial σ R)) = id :=
funext fun (x : σ → R) => comap_id_apply x
theorem comap_comp_apply {σ : Type u_1} {τ : Type u_2} {υ : Type u_3} {R : Type u_4}
[comm_semiring R] (f : alg_hom R (mv_polynomial σ R) (mv_polynomial τ R))
(g : alg_hom R (mv_polynomial τ R) (mv_polynomial υ R)) (x : υ → R) :
comap (alg_hom.comp g f) x = comap f (comap g x) :=
sorry
theorem comap_comp {σ : Type u_1} {τ : Type u_2} {υ : Type u_3} {R : Type u_4} [comm_semiring R]
(f : alg_hom R (mv_polynomial σ R) (mv_polynomial τ R))
(g : alg_hom R (mv_polynomial τ R) (mv_polynomial υ R)) :
comap (alg_hom.comp g f) = comap f ∘ comap g :=
funext fun (x : υ → R) => comap_comp_apply f g x
theorem comap_eq_id_of_eq_id {σ : Type u_1} {R : Type u_4} [comm_semiring R]
(f : alg_hom R (mv_polynomial σ R) (mv_polynomial σ R))
(hf : ∀ (φ : mv_polynomial σ R), coe_fn f φ = φ) (x : σ → R) : comap f x = x :=
sorry
theorem comap_rename {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R] (f : σ → τ)
(x : τ → R) : comap (rename f) x = x ∘ f :=
sorry
/--
If two polynomial types over the same coefficient ring `R` are equivalent,
there is a bijection between the types of functions from their variable types to `R`.
-/
def comap_equiv {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R]
(f : alg_equiv R (mv_polynomial σ R) (mv_polynomial τ R)) : (τ → R) ≃ (σ → R) :=
equiv.mk (comap ↑f) (comap ↑(alg_equiv.symm f)) sorry sorry
@[simp] theorem comap_equiv_coe {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R]
(f : alg_equiv R (mv_polynomial σ R) (mv_polynomial τ R)) : ⇑(comap_equiv f) = comap ↑f :=
rfl
@[simp] theorem comap_equiv_symm_coe {σ : Type u_1} {τ : Type u_2} {R : Type u_4} [comm_semiring R]
(f : alg_equiv R (mv_polynomial σ R) (mv_polynomial τ R)) :
⇑(equiv.symm (comap_equiv f)) = comap ↑(alg_equiv.symm f) :=
rfl
end Mathlib |
173176ca193abc75268409df82164b3480bbdb8b | a4673261e60b025e2c8c825dfa4ab9108246c32e | /stage0/src/Lean/Compiler/Util.lean | 320847a5cd22aea03915135cae77756afe917717 | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,669 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Environment
namespace Lean.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 : AndThen 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 x a >> visit x f
| Expr.lam _ d b _ => visit x d >> visit x b
| Expr.forallE _ d b _ => visit x d >> visit x b
| Expr.letE _ t v b _ => visit x t >> visit x v >> visit x b
| Expr.mdata _ e _ => visit x e
| Expr.proj _ _ e _ => visit x e
| _ => skip
end atMostOnce
open atMostOnce (visit) in
/-- 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, ..} := 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 :=
Name.mkStr 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
| Declaration.opaqueDecl { name := n, .. } => [n]
| _ => []
def checkIsDefinition (env : Environment) (n : Name) : Except String Unit :=
match env.find? n with
| (some (ConstantInfo.defnInfo _)) => Except.ok ()
| (some (ConstantInfo.opaqueInfo _)) => Except.ok ()
| none => Except.error s!"unknow declaration '{n}'"
| _ => Except.error s!"declaration is not a definition '{n}'"
/--
We generate auxiliary unsafe definitions for regular recursive definitions.
The auxiliary unsafe definition has a clear runtime cost execution model.
This function returns the auxiliary unsafe definition name for the given name. -/
@[export lean_mk_unsafe_rec_name]
def mkUnsafeRecName (declName : Name) : Name :=
Name.mkStr declName "_unsafe_rec"
/-- Return `some _` if the given name was created using `mkUnsafeRecName` -/
@[export lean_is_unsafe_rec_name]
def isUnsafeRecName? : Name → Option Name
| Name.str n "_unsafe_rec" _ => some n
| _ => none
end Lean.Compiler
|
f37bdbea0decf28b2b78d4a1277c4ca5791e7495 | 90edd5cdcf93124fe15627f7304069fdce3442dd | /stage0/src/Lean/Aesop/Main.lean | 7cecef5d1bc5e48f63a3e2f00fb392aca5c6bc4d | [
"Apache-2.0"
] | permissive | JLimperg/lean4-aesop | 8a9d9cd3ee484a8e67fda2dd9822d76708098712 | 5c4b9a3e05c32f69a4357c3047c274f4b94f9c71 | refs/heads/master | 1,689,415,944,104 | 1,627,383,284,000 | 1,627,383,284,000 | 377,536,770 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 538 | lean | /-
Copyright (c) 2021 Jannis Limperg. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jannis Limperg
-/
import Lean.Aesop.BestFirstSearch
import Lean.Aesop.Config
import Lean.Aesop.DefaultRules
import Lean.Elab.Tactic
namespace Lean.Aesop
open Lean.Elab.Tactic
syntax (name := aesop) "aesop" : tactic
@[tactic aesop]
def evalAesop : Tactic := λ stx => do
let rs ← getRuleSet
let rs := rs.addArray (← defaultRules)
trace[Aesop.RuleSet] m!"{rs}"
search rs
end Lean.Aesop
|
217f0ab276b9f7fa6350335dd0b05d1163c2389c | e30ff3aabdac29f8ea40ad76887544d0f9be9018 | /ircbot/modules/help.lean | 094d8849d1d47682b63cc6948baf6ae4010380b1 | [] | no_license | forked-from-1kasper/leanbot | bdef0efa3e4d0eb75b06c1707fb4e35086bb57fa | c61c8c7fdad7b05877e0d232719ce23d2999557f | refs/heads/master | 1,651,846,081,986 | 1,646,404,009,000 | 1,646,404,009,000 | 127,132,795 | 12 | 1 | null | 1,605,183,650,000 | 1,522,237,998,000 | Lean | UTF-8 | Lean | false | false | 662 | lean | import ircbot.types ircbot.support
open types support
namespace modules.help
def help_func (funcs : list bot_function) (input : irc_text) : list irc_text :=
match input with
| irc_text.parsed_normal
{ object := some ~nick!ident, type := message.privmsg,
args := [subject], text := "\\help" } :=
[ privmsg nick "Loaded modules:" ] ++
list.map (privmsg nick ∘ to_string) funcs
| _ := []
end
/-- Autogenerate and print loaded modules list. -/
def help (funcs : list bot_function) : bot_function :=
{ name := "help",
syntax := some "\\help",
description := "Print loaded modules.",
func := pure ∘ help_func funcs }
end modules.help
|
f6e051aa9f4692cd4b3dbadf9faafe25c13c6d45 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/algebra/polynomial/group_ring_action.lean | 1eefc0fe0a0da4bdd08471f7cdd81987c6867619 | [
"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 | 4,921 | lean | /-
Copyright (c) 2020 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import data.polynomial.monic
import data.polynomial.algebra_map
import algebra.group_ring_action
import algebra.group_action_hom
/-!
# Group action on rings applied to polynomials
This file contains instances and definitions relating `mul_semiring_action` to `polynomial`.
-/
variables (M : Type*) [monoid M]
namespace polynomial
variables (R : Type*) [semiring R]
variables {M}
lemma smul_eq_map [mul_semiring_action M R] (m : M) :
((•) m) = map (mul_semiring_action.to_ring_hom M R m) :=
begin
suffices :
distrib_mul_action.to_add_monoid_hom (polynomial R) m =
(map_ring_hom (mul_semiring_action.to_ring_hom M R m)).to_add_monoid_hom,
{ ext1 r, exact add_monoid_hom.congr_fun this r, },
ext n r : 2,
change m • monomial n r = map (mul_semiring_action.to_ring_hom M R m) (monomial n r),
simpa only [polynomial.map_monomial, polynomial.smul_monomial],
end
variables (M)
noncomputable instance [mul_semiring_action M R] : mul_semiring_action M (polynomial R) :=
{ smul := (•),
smul_one := λ m, (smul_eq_map R m).symm ▸ map_one (mul_semiring_action.to_ring_hom M R m),
smul_mul := λ m p q, (smul_eq_map R m).symm ▸ map_mul (mul_semiring_action.to_ring_hom M R m),
..polynomial.distrib_mul_action }
variables {M R}
variables [mul_semiring_action M R]
@[simp] lemma smul_X (m : M) : (m • X : polynomial R) = X :=
(smul_eq_map R m).symm ▸ map_X _
variables (S : Type*) [comm_semiring S] [mul_semiring_action M S]
theorem smul_eval_smul (m : M) (f : polynomial S) (x : S) :
(m • f).eval (m • x) = m • f.eval x :=
polynomial.induction_on f
(λ r, by rw [smul_C, eval_C, eval_C])
(λ f g ihf ihg, by rw [smul_add, eval_add, ihf, ihg, eval_add, smul_add])
(λ n r ih, by rw [smul_mul', smul_pow', smul_C, smul_X, eval_mul, eval_C, eval_pow, eval_X,
eval_mul, eval_C, eval_pow, eval_X, smul_mul', smul_pow'])
variables (G : Type*) [group G]
theorem eval_smul' [mul_semiring_action G S] (g : G) (f : polynomial S) (x : S) :
f.eval (g • x) = g • (g⁻¹ • f).eval x :=
by rw [← smul_eval_smul, smul_inv_smul]
theorem smul_eval [mul_semiring_action G S] (g : G) (f : polynomial S) (x : S) :
(g • f).eval x = g • f.eval (g⁻¹ • x) :=
by rw [← smul_eval_smul, smul_inv_smul]
end polynomial
section comm_ring
variables (G : Type*) [group G] [fintype G]
variables (R : Type*) [comm_ring R] [mul_semiring_action G R]
open mul_action
open_locale classical
/-- the product of `(X - g • x)` over distinct `g • x`. -/
noncomputable def prod_X_sub_smul (x : R) : polynomial R :=
(finset.univ : finset (G ⧸ mul_action.stabilizer G x)).prod $
λ g, polynomial.X - polynomial.C (of_quotient_stabilizer G x g)
theorem prod_X_sub_smul.monic (x : R) : (prod_X_sub_smul G R x).monic :=
polynomial.monic_prod_of_monic _ _ $ λ g _, polynomial.monic_X_sub_C _
theorem prod_X_sub_smul.eval (x : R) : (prod_X_sub_smul G R x).eval x = 0 :=
(monoid_hom.map_prod
((polynomial.aeval x).to_ring_hom.to_monoid_hom : polynomial R →* R) _ _).trans $
finset.prod_eq_zero (finset.mem_univ $ quotient_group.mk 1) $
by simp
theorem prod_X_sub_smul.smul (x : R) (g : G) :
g • prod_X_sub_smul G R x = prod_X_sub_smul G R x :=
finset.smul_prod.trans $ fintype.prod_bijective _ (mul_action.bijective g) _ _
(λ g', by rw [of_quotient_stabilizer_smul, smul_sub, polynomial.smul_X, polynomial.smul_C])
theorem prod_X_sub_smul.coeff (x : R) (g : G) (n : ℕ) :
g • (prod_X_sub_smul G R x).coeff n =
(prod_X_sub_smul G R x).coeff n :=
by rw [← polynomial.coeff_smul, prod_X_sub_smul.smul]
end comm_ring
namespace mul_semiring_action_hom
variables {M}
variables {P : Type*} [comm_semiring P] [mul_semiring_action M P]
variables {Q : Type*} [comm_semiring Q] [mul_semiring_action M Q]
open polynomial
/-- An equivariant map induces an equivariant map on polynomials. -/
protected noncomputable def polynomial (g : P →+*[M] Q) : polynomial P →+*[M] polynomial Q :=
{ to_fun := map g,
map_smul' := λ m p, polynomial.induction_on p
(λ b, by rw [smul_C, map_C, coe_fn_coe, g.map_smul, map_C, coe_fn_coe, smul_C])
(λ p q ihp ihq, by rw [smul_add, polynomial.map_add, ihp, ihq, polynomial.map_add, smul_add])
(λ n b ih, by rw [smul_mul', smul_C, smul_pow', smul_X, polynomial.map_mul, map_C,
polynomial.map_pow, map_X, coe_fn_coe, g.map_smul, polynomial.map_mul, map_C,
polynomial.map_pow, map_X, smul_mul', smul_C, smul_pow', smul_X, coe_fn_coe]),
map_zero' := polynomial.map_zero g,
map_add' := λ p q, polynomial.map_add g,
map_one' := polynomial.map_one g,
map_mul' := λ p q, polynomial.map_mul g }
@[simp] theorem coe_polynomial (g : P →+*[M] Q) :
(g.polynomial : polynomial P → polynomial Q) = map g :=
rfl
end mul_semiring_action_hom
|
46cfb0f11de65a3ecd61828a32fb22a72687ffdf | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/measure_theory/constructions/borel_space.lean | ebad4a99706d2f267cd0364917a3ba8359616235 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 68,798 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Yury Kudryashov
-/
import measure_theory.function.ae_measurable_sequence
import analysis.complex.basic
import analysis.normed_space.finite_dimension
import topology.G_delta
import measure_theory.group.arithmetic
import topology.semicontinuous
import topology.instances.ereal
/-!
# Borel (measurable) space
## Main definitions
* `borel α` : the least `σ`-algebra that contains all open sets;
* `class borel_space` : a space with `topological_space` and `measurable_space` structures
such that `‹measurable_space α› = borel α`;
* `class opens_measurable_space` : a space with `topological_space` and `measurable_space`
structures such that all open sets are measurable; equivalently, `borel α ≤ ‹measurable_space α›`.
* `borel_space` instances on `empty`, `unit`, `bool`, `nat`, `int`, `rat`;
* `measurable` and `borel_space` instances on `ℝ`, `ℝ≥0`, `ℝ≥0∞`.
## Main statements
* `is_open.measurable_set`, `is_closed.measurable_set`: open and closed sets are measurable;
* `continuous.measurable` : a continuous function is measurable;
* `continuous.measurable2` : if `f : α → β` and `g : α → γ` are measurable and `op : β × γ → δ`
is continuous, then `λ x, op (f x, g y)` is measurable;
* `measurable.add` etc : dot notation for arithmetic operations on `measurable` predicates,
and similarly for `dist` and `edist`;
* `ae_measurable.add` : similar dot notation for almost everywhere measurable functions;
* `measurable.ennreal*` : special cases for arithmetic operations on `ℝ≥0∞`.
-/
noncomputable theory
open classical set filter measure_theory
open_locale classical big_operators topological_space nnreal ennreal interval
universes u v w x y
variables {α β γ γ₂ δ : Type*} {ι : Sort y} {s t u : set α}
open measurable_space topological_space
/-- `measurable_space` structure generated by `topological_space`. -/
def borel (α : Type u) [topological_space α] : measurable_space α :=
generate_from {s : set α | is_open s}
lemma borel_eq_top_of_discrete [topological_space α] [discrete_topology α] :
borel α = ⊤ :=
top_le_iff.1 $ λ s hs, generate_measurable.basic s (is_open_discrete s)
lemma borel_eq_top_of_encodable [topological_space α] [t1_space α] [encodable α] :
borel α = ⊤ :=
begin
refine (top_le_iff.1 $ λ s hs, bUnion_of_singleton s ▸ _),
apply measurable_set.bUnion s.countable_encodable,
intros x hx,
apply measurable_set.of_compl,
apply generate_measurable.basic,
exact is_closed_singleton.is_open_compl
end
lemma borel_eq_generate_from_of_subbasis {s : set (set α)}
[t : topological_space α] [second_countable_topology α] (hs : t = generate_from s) :
borel α = generate_from s :=
le_antisymm
(generate_from_le $ assume u (hu : t.is_open u),
begin
rw [hs] at hu,
induction hu,
case generate_open.basic : u hu
{ exact generate_measurable.basic u hu },
case generate_open.univ
{ exact @measurable_set.univ α (generate_from s) },
case generate_open.inter : s₁ s₂ _ _ hs₁ hs₂
{ exact @measurable_set.inter α (generate_from s) _ _ hs₁ hs₂ },
case generate_open.sUnion : f hf ih {
rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩,
rw ← vu,
exact @measurable_set.sUnion α (generate_from s) _ hv
(λ x xv, ih _ (vf xv)) }
end)
(generate_from_le $ assume u hu, generate_measurable.basic _ $
show t.is_open u, by rw [hs]; exact generate_open.basic _ hu)
lemma topological_space.is_topological_basis.borel_eq_generate_from [topological_space α]
[second_countable_topology α] {s : set (set α)} (hs : is_topological_basis s) :
borel α = generate_from s :=
borel_eq_generate_from_of_subbasis hs.eq_generate_from
lemma is_pi_system_is_open [topological_space α] : is_pi_system (is_open : set α → Prop) :=
λ s t hs ht hst, is_open.inter hs ht
lemma borel_eq_generate_from_is_closed [topological_space α] :
borel α = generate_from {s | is_closed s} :=
le_antisymm
(generate_from_le $ λ t ht, @measurable_set.of_compl α _ (generate_from {s | is_closed s})
(generate_measurable.basic _ $ is_closed_compl_iff.2 ht))
(generate_from_le $ λ t ht, @measurable_set.of_compl α _ (borel α)
(generate_measurable.basic _ $ is_open_compl_iff.2 ht))
section order_topology
variable (α)
variables [topological_space α] [second_countable_topology α] [linear_order α] [order_topology α]
lemma is_pi_system_Ioo_mem {α : Type*} [linear_order α] (s t : set α) :
is_pi_system {S | ∃ (l ∈ s) (u ∈ t), l < u ∧ Ioo l u = S} :=
begin
rintro _ _ ⟨l₁, hls₁, u₁, hut₁, hlu₁, rfl⟩ ⟨l₂, hls₂, u₂, hut₂, hlu₂, rfl⟩
⟨x, ⟨hlx₁ : l₁ < x, hxu₁ : x < u₁⟩, ⟨hlx₂ : l₂ < x, hxu₂ : x < u₂⟩⟩,
refine ⟨l₁ ⊔ l₂, sup_ind l₁ l₂ hls₁ hls₂, u₁ ⊓ u₂, inf_ind u₁ u₂ hut₁ hut₂, _,
Ioo_inter_Ioo.symm⟩,
simp [hlx₂.trans hxu₁, hlx₁.trans hxu₂, *]
end
lemma is_pi_system_Ioo {α β : Type*} [linear_order β] (f : α → β) :
@is_pi_system β (⋃ l u (h : f l < f u), {Ioo (f l) (f u)}) :=
begin
convert is_pi_system_Ioo_mem (range f) (range f),
ext s,
simp [@eq_comm _ _ s]
end
lemma borel_eq_generate_Iio : borel α = generate_from (range Iio) :=
begin
refine le_antisymm _ (generate_from_le _),
{ rw borel_eq_generate_from_of_subbasis (@order_topology.topology_eq_generate_intervals α _ _ _),
letI : measurable_space α := measurable_space.generate_from (range Iio),
have H : ∀ a : α, measurable_set (Iio a) := λ a, generate_measurable.basic _ ⟨_, rfl⟩,
refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H],
by_cases h : ∃ a', ∀ b, a < b ↔ a' ≤ b,
{ rcases h with ⟨a', ha'⟩,
rw (_ : Ioi a = (Iio a')ᶜ), { exact (H _).compl },
simp [set.ext_iff, ha'] },
{ rcases is_open_Union_countable
(λ a' : {a' : α // a < a'}, {b | a'.1 < b})
(λ a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩,
simp [set.ext_iff] at vu,
have : Ioi a = ⋃ x : v, (Iio x.1.1)ᶜ,
{ simp [set.ext_iff],
refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩,
rcases (vu x).2 _ with ⟨a', h₁, h₂⟩,
{ exact ⟨a', h₁, le_of_lt h₂⟩ },
refine not_imp_comm.1 (λ h, _) h,
exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩),
lt_of_lt_of_le ax⟩⟩ },
rw this, resetI,
apply measurable_set.Union,
exact λ _, (H _).compl } },
{ rw forall_range_iff,
intro a,
exact generate_measurable.basic _ is_open_Iio }
end
lemma borel_eq_generate_Ioi : borel α = generate_from (range Ioi) :=
@borel_eq_generate_Iio (order_dual α) _ (by apply_instance : second_countable_topology α) _ _
end order_topology
lemma borel_comap {f : α → β} {t : topological_space β} :
@borel α (t.induced f) = (@borel β t).comap f :=
comap_generate_from.symm
lemma continuous.borel_measurable [topological_space α] [topological_space β]
{f : α → β} (hf : continuous f) :
@measurable α β (borel α) (borel β) f :=
measurable.of_le_map $ generate_from_le $
λ s hs, generate_measurable.basic (f ⁻¹' s) (hs.preimage hf)
/-- A space with `measurable_space` and `topological_space` structures such that
all open sets are measurable. -/
class opens_measurable_space (α : Type*) [topological_space α] [h : measurable_space α] : Prop :=
(borel_le : borel α ≤ h)
/-- A space with `measurable_space` and `topological_space` structures such that
the `σ`-algebra of measurable sets is exactly the `σ`-algebra generated by open sets. -/
class borel_space (α : Type*) [topological_space α] [measurable_space α] : Prop :=
(measurable_eq : ‹measurable_space α› = borel α)
/-- In a `borel_space` all open sets are measurable. -/
@[priority 100]
instance borel_space.opens_measurable {α : Type*} [topological_space α] [measurable_space α]
[borel_space α] : opens_measurable_space α :=
⟨ge_of_eq $ borel_space.measurable_eq⟩
instance subtype.borel_space {α : Type*} [topological_space α] [measurable_space α]
[hα : borel_space α] (s : set α) :
borel_space s :=
⟨by { rw [hα.1, subtype.measurable_space, ← borel_comap], refl }⟩
instance subtype.opens_measurable_space {α : Type*} [topological_space α] [measurable_space α]
[h : opens_measurable_space α] (s : set α) :
opens_measurable_space s :=
⟨by { rw [borel_comap], exact comap_mono h.1 }⟩
section
variables [topological_space α] [measurable_space α] [opens_measurable_space α]
[topological_space β] [measurable_space β] [opens_measurable_space β]
[topological_space γ] [measurable_space γ] [borel_space γ]
[topological_space γ₂] [measurable_space γ₂] [borel_space γ₂]
[measurable_space δ]
lemma is_open.measurable_set (h : is_open s) : measurable_set s :=
opens_measurable_space.borel_le _ $ generate_measurable.basic _ h
@[measurability]
lemma measurable_set_interior : measurable_set (interior s) := is_open_interior.measurable_set
lemma is_Gδ.measurable_set (h : is_Gδ s) : measurable_set s :=
begin
rcases h with ⟨S, hSo, hSc, rfl⟩,
exact measurable_set.sInter hSc (λ t ht, (hSo t ht).measurable_set)
end
lemma measurable_set_of_continuous_at {β} [emetric_space β] (f : α → β) :
measurable_set {x | continuous_at f x} :=
(is_Gδ_set_of_continuous_at f).measurable_set
lemma is_closed.measurable_set (h : is_closed s) : measurable_set s :=
h.is_open_compl.measurable_set.of_compl
lemma is_compact.measurable_set [t2_space α] (h : is_compact s) : measurable_set s :=
h.is_closed.measurable_set
@[measurability]
lemma measurable_set_closure : measurable_set (closure s) :=
is_closed_closure.measurable_set
lemma measurable_of_is_open {f : δ → γ} (hf : ∀ s, is_open s → measurable_set (f ⁻¹' s)) :
measurable f :=
by { rw [‹borel_space γ›.measurable_eq], exact measurable_generate_from hf }
lemma measurable_of_is_closed {f : δ → γ} (hf : ∀ s, is_closed s → measurable_set (f ⁻¹' s)) :
measurable f :=
begin
apply measurable_of_is_open, intros s hs,
rw [← measurable_set.compl_iff, ← preimage_compl], apply hf, rw [is_closed_compl_iff], exact hs
end
lemma measurable_of_is_closed' {f : δ → γ}
(hf : ∀ s, is_closed s → s.nonempty → s ≠ univ → measurable_set (f ⁻¹' s)) : measurable f :=
begin
apply measurable_of_is_closed, intros s hs,
cases eq_empty_or_nonempty s with h1 h1, { simp [h1] },
by_cases h2 : s = univ, { simp [h2] },
exact hf s hs h1 h2
end
instance nhds_is_measurably_generated (a : α) : (𝓝 a).is_measurably_generated :=
begin
rw [nhds, infi_subtype'],
refine @filter.infi_is_measurably_generated _ _ _ _ (λ i, _),
exact i.2.2.measurable_set.principal_is_measurably_generated
end
/-- If `s` is a measurable set, then `𝓝[s] a` is a measurably generated filter for
each `a`. This cannot be an `instance` because it depends on a non-instance `hs : measurable_set s`.
-/
lemma measurable_set.nhds_within_is_measurably_generated {s : set α} (hs : measurable_set s)
(a : α) :
(𝓝[s] a).is_measurably_generated :=
by haveI := hs.principal_is_measurably_generated; exact filter.inf_is_measurably_generated _ _
@[priority 100] -- see Note [lower instance priority]
instance opens_measurable_space.to_measurable_singleton_class [t1_space α] :
measurable_singleton_class α :=
⟨λ x, is_closed_singleton.measurable_set⟩
instance pi.opens_measurable_space {ι : Type*} {π : ι → Type*} [fintype ι]
[t' : Π i, topological_space (π i)]
[Π i, measurable_space (π i)] [∀ i, second_countable_topology (π i)]
[∀ i, opens_measurable_space (π i)] :
opens_measurable_space (Π i, π i) :=
begin
constructor,
have : Pi.topological_space =
generate_from {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ countable_basis (π a)) ∧
t = pi ↑i s},
{ rw [funext (λ a, @eq_generate_from_countable_basis (π a) _ _), pi_generate_from_eq] },
rw [borel_eq_generate_from_of_subbasis this],
apply generate_from_le,
rintros _ ⟨s, i, hi, rfl⟩,
refine measurable_set.pi i.countable_to_set (λ a ha, is_open.measurable_set _),
rw [eq_generate_from_countable_basis (π a)],
exact generate_open.basic _ (hi a ha)
end
instance prod.opens_measurable_space [second_countable_topology α] [second_countable_topology β] :
opens_measurable_space (α × β) :=
begin
constructor,
rw [((is_basis_countable_basis α).prod (is_basis_countable_basis β)).borel_eq_generate_from],
apply generate_from_le,
rintros _ ⟨u, v, hu, hv, rfl⟩,
exact (is_open_of_mem_countable_basis hu).measurable_set.prod
(is_open_of_mem_countable_basis hv).measurable_set
end
variables {α' : Type*} [topological_space α'] [measurable_space α']
lemma meas_interior_of_null_bdry {μ : measure α'} {s : set α'}
(h_nullbdry : μ (frontier s) = 0) : μ (interior s) = μ s :=
meas_eq_meas_smaller_of_between_null_diff
interior_subset subset_closure h_nullbdry
lemma meas_closure_of_null_bdry {μ : measure α'} {s : set α'}
(h_nullbdry : μ (frontier s) = 0) : μ (closure s) = μ s :=
(meas_eq_meas_larger_of_between_null_diff
interior_subset subset_closure h_nullbdry).symm
section preorder
variables [preorder α] [order_closed_topology α] {a b x : α}
@[simp, measurability]
lemma measurable_set_Ici : measurable_set (Ici a) := is_closed_Ici.measurable_set
@[simp, measurability]
lemma measurable_set_Iic : measurable_set (Iic a) := is_closed_Iic.measurable_set
@[simp, measurability]
lemma measurable_set_Icc : measurable_set (Icc a b) := is_closed_Icc.measurable_set
instance nhds_within_Ici_is_measurably_generated :
(𝓝[Ici b] a).is_measurably_generated :=
measurable_set_Ici.nhds_within_is_measurably_generated _
instance nhds_within_Iic_is_measurably_generated :
(𝓝[Iic b] a).is_measurably_generated :=
measurable_set_Iic.nhds_within_is_measurably_generated _
instance nhds_within_Icc_is_measurably_generated :
is_measurably_generated (𝓝[Icc a b] x) :=
by { rw [← Ici_inter_Iic, nhds_within_inter], apply_instance }
instance at_top_is_measurably_generated : (filter.at_top : filter α).is_measurably_generated :=
@filter.infi_is_measurably_generated _ _ _ _ $
λ a, (measurable_set_Ici : measurable_set (Ici a)).principal_is_measurably_generated
instance at_bot_is_measurably_generated : (filter.at_bot : filter α).is_measurably_generated :=
@filter.infi_is_measurably_generated _ _ _ _ $
λ a, (measurable_set_Iic : measurable_set (Iic a)).principal_is_measurably_generated
end preorder
section partial_order
variables [partial_order α] [order_closed_topology α] [second_countable_topology α]
{a b : α}
@[measurability]
lemma measurable_set_le' : measurable_set {p : α × α | p.1 ≤ p.2} :=
order_closed_topology.is_closed_le'.measurable_set
@[measurability]
lemma measurable_set_le {f g : δ → α} (hf : measurable f) (hg : measurable g) :
measurable_set {a | f a ≤ g a} :=
hf.prod_mk hg measurable_set_le'
end partial_order
section linear_order
variables [linear_order α] [order_closed_topology α] {a b x : α}
@[simp, measurability]
lemma measurable_set_Iio : measurable_set (Iio a) := is_open_Iio.measurable_set
@[simp, measurability]
lemma measurable_set_Ioi : measurable_set (Ioi a) := is_open_Ioi.measurable_set
@[simp, measurability]
lemma measurable_set_Ioo : measurable_set (Ioo a b) := is_open_Ioo.measurable_set
@[simp, measurability] lemma measurable_set_Ioc : measurable_set (Ioc a b) :=
measurable_set_Ioi.inter measurable_set_Iic
@[simp, measurability] lemma measurable_set_Ico : measurable_set (Ico a b) :=
measurable_set_Ici.inter measurable_set_Iio
instance nhds_within_Ioi_is_measurably_generated :
(𝓝[Ioi b] a).is_measurably_generated :=
measurable_set_Ioi.nhds_within_is_measurably_generated _
instance nhds_within_Iio_is_measurably_generated :
(𝓝[Iio b] a).is_measurably_generated :=
measurable_set_Iio.nhds_within_is_measurably_generated _
instance nhds_within_interval_is_measurably_generated :
is_measurably_generated (𝓝[[a, b]] x) :=
nhds_within_Icc_is_measurably_generated
@[measurability]
lemma measurable_set_lt' [second_countable_topology α] : measurable_set {p : α × α | p.1 < p.2} :=
(is_open_lt continuous_fst continuous_snd).measurable_set
@[measurability]
lemma measurable_set_lt [second_countable_topology α] {f g : δ → α} (hf : measurable f)
(hg : measurable g) : measurable_set {a | f a < g a} :=
hf.prod_mk hg measurable_set_lt'
lemma set.ord_connected.measurable_set (h : ord_connected s) : measurable_set s :=
begin
let u := ⋃ (x ∈ s) (y ∈ s), Ioo x y,
have huopen : is_open u := is_open_bUnion (λ x hx, is_open_bUnion (λ y hy, is_open_Ioo)),
have humeas : measurable_set u := huopen.measurable_set,
have hfinite : (s \ u).finite,
{ refine set.finite_of_forall_between_eq_endpoints (s \ u) (λ x hx y hy z hz hxy hyz, _),
by_contra h,
push_neg at h,
exact hy.2 (mem_bUnion_iff.mpr ⟨x, hx.1,
mem_bUnion_iff.mpr ⟨z, hz.1, lt_of_le_of_ne hxy h.1, lt_of_le_of_ne hyz h.2⟩⟩) },
have : u ⊆ s :=
bUnion_subset (λ x hx, bUnion_subset (λ y hy, Ioo_subset_Icc_self.trans (h.out hx hy))),
rw ← union_diff_cancel this,
exact humeas.union hfinite.measurable_set
end
lemma is_preconnected.measurable_set
(h : is_preconnected s) : measurable_set s :=
h.ord_connected.measurable_set
end linear_order
section linear_order
variables [linear_order α] [order_closed_topology α]
@[measurability]
lemma measurable_set_interval {a b : α} : measurable_set (interval a b) :=
measurable_set_Icc
variables [second_countable_topology α]
@[measurability]
lemma measurable.max {f g : δ → α} (hf : measurable f) (hg : measurable g) :
measurable (λ a, max (f a) (g a)) :=
hf.piecewise (measurable_set_le hg hf) hg
@[measurability]
lemma ae_measurable.max {f g : δ → α} {μ : measure δ}
(hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, max (f a) (g a)) μ :=
⟨λ a, max (hf.mk f a) (hg.mk g a), hf.measurable_mk.max hg.measurable_mk,
eventually_eq.comp₂ hf.ae_eq_mk _ hg.ae_eq_mk⟩
@[measurability]
lemma measurable.min {f g : δ → α} (hf : measurable f) (hg : measurable g) :
measurable (λ a, min (f a) (g a)) :=
hf.piecewise (measurable_set_le hf hg) hg
@[measurability]
lemma ae_measurable.min {f g : δ → α} {μ : measure δ}
(hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, min (f a) (g a)) μ :=
⟨λ a, min (hf.mk f a) (hg.mk g a), hf.measurable_mk.min hg.measurable_mk,
eventually_eq.comp₂ hf.ae_eq_mk _ hg.ae_eq_mk⟩
end linear_order
/-- A continuous function from an `opens_measurable_space` to a `borel_space`
is measurable. -/
lemma continuous.measurable {f : α → γ} (hf : continuous f) :
measurable f :=
hf.borel_measurable.mono opens_measurable_space.borel_le
(le_of_eq $ borel_space.measurable_eq)
/-- A continuous function from an `opens_measurable_space` to a `borel_space`
is ae-measurable. -/
lemma continuous.ae_measurable {f : α → γ} (h : continuous f) (μ : measure α) : ae_measurable f μ :=
h.measurable.ae_measurable
lemma closed_embedding.measurable {f : α → γ} (hf : closed_embedding f) :
measurable f :=
hf.continuous.measurable
@[priority 100, to_additive]
instance has_continuous_mul.has_measurable_mul [has_mul γ] [has_continuous_mul γ] :
has_measurable_mul γ :=
{ measurable_const_mul := λ c, (continuous_const.mul continuous_id).measurable,
measurable_mul_const := λ c, (continuous_id.mul continuous_const).measurable }
@[priority 100]
instance has_continuous_sub.has_measurable_sub [has_sub γ] [has_continuous_sub γ] :
has_measurable_sub γ :=
{ measurable_const_sub := λ c, (continuous_const.sub continuous_id).measurable,
measurable_sub_const := λ c, (continuous_id.sub continuous_const).measurable }
@[priority 100, to_additive]
instance topological_group.has_measurable_inv [group γ] [topological_group γ] :
has_measurable_inv γ :=
⟨continuous_inv.measurable⟩
@[priority 100]
instance has_continuous_smul.has_measurable_smul {M α} [topological_space M]
[topological_space α] [measurable_space M] [measurable_space α]
[opens_measurable_space M] [borel_space α] [has_scalar M α] [has_continuous_smul M α] :
has_measurable_smul M α :=
⟨λ c, (continuous_const.smul continuous_id).measurable,
λ y, (continuous_id.smul continuous_const).measurable⟩
section homeomorph
/-- A homeomorphism between two Borel spaces is a measurable equivalence.-/
def homeomorph.to_measurable_equiv (h : γ ≃ₜ γ₂) : γ ≃ᵐ γ₂ :=
{ measurable_to_fun := h.continuous_to_fun.measurable,
measurable_inv_fun := h.continuous_inv_fun.measurable,
.. h }
@[simp]
lemma homeomorph.to_measurable_equiv_coe (h : γ ≃ₜ γ₂) : (h.to_measurable_equiv : γ → γ₂) = h :=
rfl
@[simp] lemma homeomorph.to_measurable_equiv_symm_coe (h : γ ≃ₜ γ₂) :
(h.to_measurable_equiv.symm : γ₂ → γ) = h.symm :=
rfl
@[measurability]
lemma homeomorph.measurable (h : α ≃ₜ γ) : measurable h :=
h.continuous.measurable
end homeomorph
lemma measurable_of_continuous_on_compl_singleton [t1_space α] {f : α → γ} (a : α)
(hf : continuous_on f {a}ᶜ) :
measurable f :=
measurable_of_measurable_on_compl_singleton a
(continuous_on_iff_continuous_restrict.1 hf).measurable
lemma continuous.measurable2 [second_countable_topology α] [second_countable_topology β]
{f : δ → α} {g : δ → β} {c : α → β → γ}
(h : continuous (λ p : α × β, c p.1 p.2)) (hf : measurable f) (hg : measurable g) :
measurable (λ a, c (f a) (g a)) :=
h.measurable.comp (hf.prod_mk hg)
lemma continuous.ae_measurable2 [second_countable_topology α] [second_countable_topology β]
{f : δ → α} {g : δ → β} {c : α → β → γ} {μ : measure δ}
(h : continuous (λ p : α × β, c p.1 p.2)) (hf : ae_measurable f μ) (hg : ae_measurable g μ) :
ae_measurable (λ a, c (f a) (g a)) μ :=
h.measurable.comp_ae_measurable (hf.prod_mk hg)
@[priority 100]
instance has_continuous_inv'.has_measurable_inv [group_with_zero γ] [t1_space γ]
[has_continuous_inv' γ] :
has_measurable_inv γ :=
⟨measurable_of_continuous_on_compl_singleton 0 continuous_on_inv'⟩
@[priority 100, to_additive]
instance has_continuous_mul.has_measurable_mul₂ [second_countable_topology γ] [has_mul γ]
[has_continuous_mul γ] : has_measurable_mul₂ γ :=
⟨continuous_mul.measurable⟩
@[priority 100]
instance has_continuous_sub.has_measurable_sub₂ [second_countable_topology γ] [has_sub γ]
[has_continuous_sub γ] : has_measurable_sub₂ γ :=
⟨continuous_sub.measurable⟩
@[priority 100]
instance has_continuous_smul.has_measurable_smul₂ {M α} [topological_space M]
[second_countable_topology M] [measurable_space M] [opens_measurable_space M]
[topological_space α] [second_countable_topology α] [measurable_space α]
[borel_space α] [has_scalar M α] [has_continuous_smul M α] :
has_measurable_smul₂ M α :=
⟨continuous_smul.measurable⟩
end
section borel_space
variables [topological_space α] [measurable_space α] [borel_space α]
[topological_space β] [measurable_space β] [borel_space β]
[topological_space γ] [measurable_space γ] [borel_space γ]
[measurable_space δ]
lemma pi_le_borel_pi {ι : Type*} {π : ι → Type*} [Π i, topological_space (π i)]
[Π i, measurable_space (π i)] [∀ i, borel_space (π i)] :
measurable_space.pi ≤ borel (Π i, π i) :=
begin
have : ‹Π i, measurable_space (π i)› = λ i, borel (π i) :=
funext (λ i, borel_space.measurable_eq),
rw [this],
exact supr_le (λ i, comap_le_iff_le_map.2 $ (continuous_apply i).borel_measurable)
end
lemma prod_le_borel_prod : prod.measurable_space ≤ borel (α × β) :=
begin
rw [‹borel_space α›.measurable_eq, ‹borel_space β›.measurable_eq],
refine sup_le _ _,
{ exact comap_le_iff_le_map.mpr continuous_fst.borel_measurable },
{ exact comap_le_iff_le_map.mpr continuous_snd.borel_measurable }
end
instance pi.borel_space {ι : Type*} {π : ι → Type*} [fintype ι]
[t' : Π i, topological_space (π i)]
[Π i, measurable_space (π i)] [∀ i, second_countable_topology (π i)]
[∀ i, borel_space (π i)] :
borel_space (Π i, π i) :=
⟨le_antisymm pi_le_borel_pi opens_measurable_space.borel_le⟩
instance prod.borel_space [second_countable_topology α] [second_countable_topology β] :
borel_space (α × β) :=
⟨le_antisymm prod_le_borel_prod opens_measurable_space.borel_le⟩
lemma closed_embedding.measurable_inv_fun [n : nonempty β] {g : β → γ} (hg : closed_embedding g) :
measurable (function.inv_fun g) :=
begin
refine measurable_of_is_closed (λ s hs, _),
by_cases h : classical.choice n ∈ s,
{ rw preimage_inv_fun_of_mem hg.to_embedding.inj h,
exact (hg.closed_iff_image_closed.mp hs).measurable_set.union
hg.closed_range.measurable_set.compl },
{ rw preimage_inv_fun_of_not_mem hg.to_embedding.inj h,
exact (hg.closed_iff_image_closed.mp hs).measurable_set }
end
lemma measurable_comp_iff_of_closed_embedding {f : δ → β} (g : β → γ) (hg : closed_embedding g) :
measurable (g ∘ f) ↔ measurable f :=
begin
refine ⟨λ hf, _, λ hf, hg.measurable.comp hf⟩,
apply measurable_of_is_closed, intros s hs,
convert hf (hg.is_closed_map s hs).measurable_set,
rw [@preimage_comp _ _ _ f g, preimage_image_eq _ hg.to_embedding.inj]
end
lemma ae_measurable_comp_iff_of_closed_embedding {f : δ → β} {μ : measure δ}
(g : β → γ) (hg : closed_embedding g) : ae_measurable (g ∘ f) μ ↔ ae_measurable f μ :=
begin
casesI is_empty_or_nonempty β,
{ haveI := function.is_empty f,
simp only [(measurable_of_empty (g ∘ f)).ae_measurable,
(measurable_of_empty f).ae_measurable] },
{ refine ⟨λ hf, _, λ hf, hg.measurable.comp_ae_measurable hf⟩,
convert hg.measurable_inv_fun.comp_ae_measurable hf,
ext x,
exact (function.left_inverse_inv_fun hg.to_embedding.inj (f x)).symm },
end
lemma ae_measurable_comp_right_iff_of_closed_embedding {g : α → β} {μ : measure α}
{f : β → δ} (hg : closed_embedding g) :
ae_measurable (f ∘ g) μ ↔ ae_measurable f (measure.map g μ) :=
begin
refine ⟨λ h, _, λ h, h.comp_measurable hg.measurable⟩,
casesI is_empty_or_nonempty α, { simp [μ.eq_zero_of_is_empty] },
refine ⟨(h.mk _) ∘ (function.inv_fun g), h.measurable_mk.comp hg.measurable_inv_fun, _⟩,
have : μ = measure.map (function.inv_fun g) (measure.map g μ),
by rw [measure.map_map hg.measurable_inv_fun hg.measurable,
(function.left_inverse_inv_fun hg.to_embedding.inj).comp_eq_id, measure.map_id],
rw this at h,
filter_upwards [ae_of_ae_map hg.measurable_inv_fun h.ae_eq_mk,
ae_map_mem_range g hg.closed_range.measurable_set μ],
assume x hx₁ hx₂,
convert hx₁,
exact ((function.left_inverse_inv_fun hg.to_embedding.inj).right_inv_on_range hx₂).symm,
end
section linear_order
variables [linear_order α] [order_topology α] [second_countable_topology α]
lemma measurable_of_Iio {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Iio x)) : measurable f :=
begin
convert measurable_generate_from _,
exact borel_space.measurable_eq.trans (borel_eq_generate_Iio _),
rintro _ ⟨x, rfl⟩, exact hf x
end
lemma upper_semicontinuous.measurable [topological_space δ] [opens_measurable_space δ]
{f : δ → α} (hf : upper_semicontinuous f) : measurable f :=
measurable_of_Iio (λ y, (hf.is_open_preimage y).measurable_set)
lemma measurable_of_Ioi {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Ioi x)) : measurable f :=
begin
convert measurable_generate_from _,
exact borel_space.measurable_eq.trans (borel_eq_generate_Ioi _),
rintro _ ⟨x, rfl⟩, exact hf x
end
lemma lower_semicontinuous.measurable [topological_space δ] [opens_measurable_space δ]
{f : δ → α} (hf : lower_semicontinuous f) : measurable f :=
measurable_of_Ioi (λ y, (hf.is_open_preimage y).measurable_set)
lemma measurable_of_Iic {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Iic x)) : measurable f :=
begin
apply measurable_of_Ioi,
simp_rw [← compl_Iic, preimage_compl, measurable_set.compl_iff],
assumption
end
lemma measurable_of_Ici {f : δ → α} (hf : ∀ x, measurable_set (f ⁻¹' Ici x)) : measurable f :=
begin
apply measurable_of_Iio,
simp_rw [← compl_Ici, preimage_compl, measurable_set.compl_iff],
assumption
end
lemma measurable.is_lub {ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i))
(hg : ∀ b, is_lub {a | ∃ i, f i b = a} (g b)) :
measurable g :=
begin
change ∀ b, is_lub (range $ λ i, f i b) (g b) at hg,
rw [‹borel_space α›.measurable_eq, borel_eq_generate_Ioi α],
apply measurable_generate_from,
rintro _ ⟨a, rfl⟩,
simp_rw [set.preimage, mem_Ioi, lt_is_lub_iff (hg _), exists_range_iff, set_of_exists],
exact measurable_set.Union (λ i, hf i (is_open_lt' _).measurable_set)
end
private lemma ae_measurable.is_lub_of_nonempty {ι} (hι : nonempty ι)
{μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α}
(hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_lub {a | ∃ i, f i b = a} (g b)) :
ae_measurable g μ :=
begin
let p : δ → (ι → α) → Prop := λ x f', is_lub {a | ∃ i, f' i = a} (g x),
let g_seq := λ x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty α).some,
have hg_seq : ∀ b, is_lub {a | ∃ i, ae_seq hf p i b = a} (g_seq b),
{ intro b,
haveI hα : nonempty α := nonempty.map g ⟨b⟩,
simp only [ae_seq, g_seq],
split_ifs,
{ have h_set_eq : {a : α | ∃ (i : ι), (hf i).mk (f i) b = a} = {a : α | ∃ (i : ι), f i b = a},
{ ext x,
simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], },
rw h_set_eq,
exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, },
{ have h_singleton : {a : α | ∃ (i : ι), hα.some = a} = {hα.some},
{ ext1 x,
exact ⟨λ hx, hx.some_spec.symm, λ hx, ⟨hι.some, hx.symm⟩⟩, },
rw h_singleton,
exact is_lub_singleton, }, },
refine ⟨g_seq, measurable.is_lub (ae_seq.measurable hf p) hg_seq, _⟩,
exact (ite_ae_eq_of_measure_compl_zero g (λ x, (⟨g x⟩ : nonempty α).some) (ae_seq_set hf p)
(ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm,
end
lemma ae_measurable.is_lub {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α}
(hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_lub {a | ∃ i, f i b = a} (g b)) :
ae_measurable g μ :=
begin
by_cases hμ : μ = 0, { rw hμ, exact ae_measurable_zero_measure },
haveI : μ.ae.ne_bot, { simpa [ne_bot_iff] },
by_cases hι : nonempty ι, { exact ae_measurable.is_lub_of_nonempty hι hf hg, },
suffices : ∃ x, g =ᵐ[μ] λ y, g x,
by { exact ⟨(λ y, g this.some), measurable_const, this.some_spec⟩, },
have h_empty : ∀ x, {a : α | ∃ (i : ι), f i x = a} = ∅,
{ intro x,
ext1 y,
rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false],
exact λ hi, hι (nonempty_of_exists hi), },
simp_rw h_empty at hg,
exact ⟨hg.exists.some, hg.mono (λ y hy, is_lub.unique hy hg.exists.some_spec)⟩,
end
lemma measurable.is_glb {ι} [encodable ι] {f : ι → δ → α} {g : δ → α} (hf : ∀ i, measurable (f i))
(hg : ∀ b, is_glb {a | ∃ i, f i b = a} (g b)) :
measurable g :=
begin
change ∀ b, is_glb (range $ λ i, f i b) (g b) at hg,
rw [‹borel_space α›.measurable_eq, borel_eq_generate_Iio α],
apply measurable_generate_from,
rintro _ ⟨a, rfl⟩,
simp_rw [set.preimage, mem_Iio, is_glb_lt_iff (hg _), exists_range_iff, set_of_exists],
exact measurable_set.Union (λ i, hf i (is_open_gt' _).measurable_set)
end
private lemma ae_measurable.is_glb_of_nonempty {ι} (hι : nonempty ι)
{μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α}
(hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_glb {a | ∃ i, f i b = a} (g b)) :
ae_measurable g μ :=
begin
let p : δ → (ι → α) → Prop := λ x f', is_glb {a | ∃ i, f' i = a} (g x),
let g_seq := λ x, ite (x ∈ ae_seq_set hf p) (g x) (⟨g x⟩ : nonempty α).some,
have hg_seq : ∀ b, is_glb {a | ∃ i, ae_seq hf p i b = a} (g_seq b),
{ intro b,
haveI hα : nonempty α := nonempty.map g ⟨b⟩,
simp only [ae_seq, g_seq],
split_ifs,
{ have h_set_eq : {a : α | ∃ (i : ι), (hf i).mk (f i) b = a} = {a : α | ∃ (i : ι), f i b = a},
{ ext x,
simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], },
rw h_set_eq,
exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, },
{ have h_singleton : {a : α | ∃ (i : ι), hα.some = a} = {hα.some},
{ ext1 x,
exact ⟨λ hx, hx.some_spec.symm, λ hx, ⟨hι.some, hx.symm⟩⟩, },
rw h_singleton,
exact is_glb_singleton, }, },
refine ⟨g_seq, measurable.is_glb (ae_seq.measurable hf p) hg_seq, _⟩,
exact (ite_ae_eq_of_measure_compl_zero g (λ x, (⟨g x⟩ : nonempty α).some) (ae_seq_set hf p)
(ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm,
end
lemma ae_measurable.is_glb {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α} {g : δ → α}
(hf : ∀ i, ae_measurable (f i) μ) (hg : ∀ᵐ b ∂μ, is_glb {a | ∃ i, f i b = a} (g b)) :
ae_measurable g μ :=
begin
by_cases hμ : μ = 0, { rw hμ, exact ae_measurable_zero_measure },
haveI : μ.ae.ne_bot, { simpa [ne_bot_iff] },
by_cases hι : nonempty ι, { exact ae_measurable.is_glb_of_nonempty hι hf hg, },
suffices : ∃ x, g =ᵐ[μ] λ y, g x,
by { exact ⟨(λ y, g this.some), measurable_const, this.some_spec⟩, },
have h_empty : ∀ x, {a : α | ∃ (i : ι), f i x = a} = ∅,
{ intro x,
ext1 y,
rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false],
exact λ hi, hι (nonempty_of_exists hi), },
simp_rw h_empty at hg,
exact ⟨hg.exists.some, hg.mono (λ y hy, is_glb.unique hy hg.exists.some_spec)⟩,
end
lemma measurable_of_monotone [linear_order β] [order_closed_topology β] {f : β → α}
(hf : monotone f) : measurable f :=
suffices h : ∀ x, ord_connected (f ⁻¹' Ioi x),
from measurable_of_Ioi (λ x, (h x).measurable_set),
λ x, ord_connected_def.mpr (λ a ha b hb c hc, lt_of_lt_of_le ha (hf hc.1))
alias measurable_of_monotone ← monotone.measurable
lemma ae_measurable_restrict_of_monotone_on [linear_order β] [order_closed_topology β]
{μ : measure β} {s : set β} (hs : measurable_set s) {f : β → α}
(hf : ∀ ⦃x y⦄, x ∈ s → y ∈ s → x ≤ y → f x ≤ f y) : ae_measurable f (μ.restrict s) :=
have this : monotone (f ∘ coe : s → α), from λ ⟨x, hx⟩ ⟨y, hy⟩ (hxy : x ≤ y), hf hx hy hxy,
ae_measurable_restrict_of_measurable_subtype hs this.measurable
lemma measurable_of_antimono [linear_order β] [order_closed_topology β] {f : β → α}
(hf : ∀ ⦃x y : β⦄, x ≤ y → f y ≤ f x) :
measurable f :=
@measurable_of_monotone (order_dual α) β _ _ ‹_› _ _ _ _ _ ‹_› _ _ _ hf
lemma ae_measurable_restrict_of_antimono_on [linear_order β] [order_closed_topology β]
{μ : measure β} {s : set β} (hs : measurable_set s) {f : β → α}
(hf : ∀ ⦃x y⦄, x ∈ s → y ∈ s → x ≤ y → f y ≤ f x) : ae_measurable f (μ.restrict s) :=
@ae_measurable_restrict_of_monotone_on (order_dual α) β _ _ ‹_› _ _ _ _ _ ‹_› _ _ _ _ hs _ hf
end linear_order
@[measurability]
lemma measurable.supr_Prop {α} [measurable_space α] [complete_lattice α]
(p : Prop) {f : δ → α} (hf : measurable f) :
measurable (λ b, ⨆ h : p, f b) :=
classical.by_cases
(assume h : p, begin convert hf, funext, exact supr_pos h end)
(assume h : ¬p, begin convert measurable_const, funext, exact supr_neg h end)
@[measurability]
lemma measurable.infi_Prop {α} [measurable_space α] [complete_lattice α]
(p : Prop) {f : δ → α} (hf : measurable f) :
measurable (λ b, ⨅ h : p, f b) :=
classical.by_cases
(assume h : p, begin convert hf, funext, exact infi_pos h end )
(assume h : ¬p, begin convert measurable_const, funext, exact infi_neg h end)
section complete_linear_order
variables [complete_linear_order α] [order_topology α] [second_countable_topology α]
@[measurability]
lemma measurable_supr {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ b, ⨆ i, f i b) :=
measurable.is_lub hf $ λ b, is_lub_supr
@[measurability]
lemma ae_measurable_supr {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α}
(hf : ∀ i, ae_measurable (f i) μ) :
ae_measurable (λ b, ⨆ i, f i b) μ :=
ae_measurable.is_lub hf $ (ae_of_all μ (λ b, is_lub_supr))
@[measurability]
lemma measurable_infi {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ b, ⨅ i, f i b) :=
measurable.is_glb hf $ λ b, is_glb_infi
@[measurability]
lemma ae_measurable_infi {ι} {μ : measure δ} [encodable ι] {f : ι → δ → α}
(hf : ∀ i, ae_measurable (f i) μ) :
ae_measurable (λ b, ⨅ i, f i b) μ :=
ae_measurable.is_glb hf $ (ae_of_all μ (λ b, is_glb_infi))
lemma measurable_bsupr {ι} (s : set ι) {f : ι → δ → α} (hs : countable s)
(hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i ∈ s, f i b) :=
by { haveI : encodable s := hs.to_encodable, simp only [supr_subtype'],
exact measurable_supr (λ i, hf i) }
lemma ae_measurable_bsupr {ι} {μ : measure δ} (s : set ι) {f : ι → δ → α} (hs : countable s)
(hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨆ i ∈ s, f i b) μ :=
begin
haveI : encodable s := hs.to_encodable,
simp only [supr_subtype'],
exact ae_measurable_supr (λ i, hf i),
end
lemma measurable_binfi {ι} (s : set ι) {f : ι → δ → α} (hs : countable s)
(hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i ∈ s, f i b) :=
by { haveI : encodable s := hs.to_encodable, simp only [infi_subtype'],
exact measurable_infi (λ i, hf i) }
lemma ae_measurable_binfi {ι} {μ : measure δ} (s : set ι) {f : ι → δ → α} (hs : countable s)
(hf : ∀ i, ae_measurable (f i) μ) : ae_measurable (λ b, ⨅ i ∈ s, f i b) μ :=
begin
haveI : encodable s := hs.to_encodable,
simp only [infi_subtype'],
exact ae_measurable_infi (λ i, hf i),
end
/-- `liminf` over a general filter is measurable. See `measurable_liminf` for the version over `ℕ`.
-/
lemma measurable_liminf' {ι ι'} {f : ι → δ → α} {u : filter ι} (hf : ∀ i, measurable (f i))
{p : ι' → Prop} {s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) :
measurable (λ x, liminf u (λ i, f i x)) :=
begin
simp_rw [hu.to_has_basis.liminf_eq_supr_infi],
refine measurable_bsupr _ hu.countable _,
exact λ i, measurable_binfi _ (hs i) hf
end
/-- `limsup` over a general filter is measurable. See `measurable_limsup` for the version over `ℕ`.
-/
lemma measurable_limsup' {ι ι'} {f : ι → δ → α} {u : filter ι} (hf : ∀ i, measurable (f i))
{p : ι' → Prop} {s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) :
measurable (λ x, limsup u (λ i, f i x)) :=
begin
simp_rw [hu.to_has_basis.limsup_eq_infi_supr],
refine measurable_binfi _ hu.countable _,
exact λ i, measurable_bsupr _ (hs i) hf
end
/-- `liminf` over `ℕ` is measurable. See `measurable_liminf'` for a version with a general filter.
-/
@[measurability]
lemma measurable_liminf {f : ℕ → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ x, liminf at_top (λ i, f i x)) :=
measurable_liminf' hf at_top_countable_basis (λ i, countable_encodable _)
/-- `limsup` over `ℕ` is measurable. See `measurable_limsup'` for a version with a general filter.
-/
@[measurability]
lemma measurable_limsup {f : ℕ → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ x, limsup at_top (λ i, f i x)) :=
measurable_limsup' hf at_top_countable_basis (λ i, countable_encodable _)
end complete_linear_order
section conditionally_complete_linear_order
variables [conditionally_complete_linear_order α] [order_topology α] [second_countable_topology α]
lemma measurable_cSup {ι} {f : ι → δ → α} {s : set ι} (hs : s.countable)
(hf : ∀ i, measurable (f i)) (bdd : ∀ x, bdd_above ((λ i, f i x) '' s)) :
measurable (λ x, Sup ((λ i, f i x) '' s)) :=
begin
cases eq_empty_or_nonempty s with h2s h2s,
{ simp [h2s, measurable_const] },
{ apply measurable_of_Iic, intro y,
simp_rw [preimage, mem_Iic, cSup_le_iff (bdd _) (h2s.image _), ball_image_iff, set_of_forall],
exact measurable_set.bInter hs (λ i hi, measurable_set_le (hf i) measurable_const) }
end
end conditionally_complete_linear_order
/-- Convert a `homeomorph` to a `measurable_equiv`. -/
def homemorph.to_measurable_equiv (h : α ≃ₜ β) : α ≃ᵐ β :=
{ to_equiv := h.to_equiv,
measurable_to_fun := h.continuous_to_fun.measurable,
measurable_inv_fun := h.continuous_inv_fun.measurable }
end borel_space
instance empty.borel_space : borel_space empty := ⟨borel_eq_top_of_discrete.symm⟩
instance unit.borel_space : borel_space unit := ⟨borel_eq_top_of_discrete.symm⟩
instance bool.borel_space : borel_space bool := ⟨borel_eq_top_of_discrete.symm⟩
instance nat.borel_space : borel_space ℕ := ⟨borel_eq_top_of_discrete.symm⟩
instance int.borel_space : borel_space ℤ := ⟨borel_eq_top_of_discrete.symm⟩
instance rat.borel_space : borel_space ℚ := ⟨borel_eq_top_of_encodable.symm⟩
instance real.measurable_space : measurable_space ℝ := borel ℝ
instance real.borel_space : borel_space ℝ := ⟨rfl⟩
instance nnreal.measurable_space : measurable_space ℝ≥0 := subtype.measurable_space
instance nnreal.borel_space : borel_space ℝ≥0 := subtype.borel_space _
instance ennreal.measurable_space : measurable_space ℝ≥0∞ := borel ℝ≥0∞
instance ennreal.borel_space : borel_space ℝ≥0∞ := ⟨rfl⟩
instance ereal.measurable_space : measurable_space ereal := borel ereal
instance ereal.borel_space : borel_space ereal := ⟨rfl⟩
instance complex.measurable_space : measurable_space ℂ := borel ℂ
instance complex.borel_space : borel_space ℂ := ⟨rfl⟩
section metric_space
variables [metric_space α] [measurable_space α] [opens_measurable_space α]
variables [measurable_space β] {x : α} {ε : ℝ}
open metric
@[measurability]
lemma measurable_set_ball : measurable_set (metric.ball x ε) :=
metric.is_open_ball.measurable_set
@[measurability]
lemma measurable_set_closed_ball : measurable_set (metric.closed_ball x ε) :=
metric.is_closed_ball.measurable_set
@[measurability]
lemma measurable_inf_dist {s : set α} : measurable (λ x, inf_dist x s) :=
(continuous_inf_dist_pt s).measurable
@[measurability]
lemma measurable.inf_dist {f : β → α} (hf : measurable f) {s : set α} :
measurable (λ x, inf_dist (f x) s) :=
measurable_inf_dist.comp hf
@[measurability]
lemma measurable_inf_nndist {s : set α} : measurable (λ x, inf_nndist x s) :=
(continuous_inf_nndist_pt s).measurable
@[measurability]
lemma measurable.inf_nndist {f : β → α} (hf : measurable f) {s : set α} :
measurable (λ x, inf_nndist (f x) s) :=
measurable_inf_nndist.comp hf
variables [second_countable_topology α]
@[measurability]
lemma measurable_dist : measurable (λ p : α × α, dist p.1 p.2) :=
continuous_dist.measurable
@[measurability]
lemma measurable.dist {f g : β → α} (hf : measurable f) (hg : measurable g) :
measurable (λ b, dist (f b) (g b)) :=
(@continuous_dist α _).measurable2 hf hg
@[measurability]
lemma measurable_nndist : measurable (λ p : α × α, nndist p.1 p.2) :=
continuous_nndist.measurable
@[measurability]
lemma measurable.nndist {f g : β → α} (hf : measurable f) (hg : measurable g) :
measurable (λ b, nndist (f b) (g b)) :=
(@continuous_nndist α _).measurable2 hf hg
end metric_space
section emetric_space
variables [emetric_space α] [measurable_space α] [opens_measurable_space α]
variables [measurable_space β] {x : α} {ε : ℝ≥0∞}
open emetric
@[measurability]
lemma measurable_set_eball : measurable_set (emetric.ball x ε) :=
emetric.is_open_ball.measurable_set
@[measurability]
lemma measurable_edist_right : measurable (edist x) :=
(continuous_const.edist continuous_id).measurable
@[measurability]
lemma measurable_edist_left : measurable (λ y, edist y x) :=
(continuous_id.edist continuous_const).measurable
@[measurability]
lemma measurable_inf_edist {s : set α} : measurable (λ x, inf_edist x s) :=
continuous_inf_edist.measurable
@[measurability]
lemma measurable.inf_edist {f : β → α} (hf : measurable f) {s : set α} :
measurable (λ x, inf_edist (f x) s) :=
measurable_inf_edist.comp hf
variables [second_countable_topology α]
@[measurability]
lemma measurable_edist : measurable (λ p : α × α, edist p.1 p.2) :=
continuous_edist.measurable
@[measurability]
lemma measurable.edist {f g : β → α} (hf : measurable f) (hg : measurable g) :
measurable (λ b, edist (f b) (g b)) :=
(@continuous_edist α _).measurable2 hf hg
@[measurability]
lemma ae_measurable.edist {f g : β → α} {μ : measure β}
(hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, edist (f a) (g a)) μ :=
(@continuous_edist α _).ae_measurable2 hf hg
end emetric_space
namespace real
open measurable_space measure_theory
lemma borel_eq_generate_from_Ioo_rat :
borel ℝ = generate_from (⋃(a b : ℚ) (h : a < b), {Ioo a b}) :=
is_topological_basis_Ioo_rat.borel_eq_generate_from
lemma is_pi_system_Ioo_rat : @is_pi_system ℝ (⋃ (a b : ℚ) (h : a < b), {Ioo a b}) :=
by simpa using is_pi_system_Ioo (coe : ℚ → ℝ)
/-- The intervals `(-(n + 1), (n + 1))` form a finite spanning sets in the set of open intervals
with rational endpoints for a locally finite measure `μ` on `ℝ`. -/
def finite_spanning_sets_in_Ioo_rat (μ : measure ℝ) [is_locally_finite_measure μ] :
μ.finite_spanning_sets_in (⋃ (a b : ℚ) (h : a < b), {Ioo a b}) :=
{ set := λ n, Ioo (-(n + 1)) (n + 1),
set_mem := λ n,
begin
simp only [mem_Union, mem_singleton_iff],
refine ⟨-(n + 1), n + 1, _, by norm_cast⟩,
exact (neg_nonpos.2 (@nat.cast_nonneg ℚ _ (n + 1))).trans_lt n.cast_add_one_pos
end,
finite := λ n,
calc μ (Ioo _ _) ≤ μ (Icc _ _) : μ.mono Ioo_subset_Icc_self
... < ∞ : is_compact_Icc.is_finite_measure,
spanning := Union_eq_univ_iff.2 $ λ x,
⟨⌊abs x⌋₊, neg_lt.1 ((neg_le_abs_self x).trans_lt (lt_nat_floor_add_one _)),
(le_abs_self x).trans_lt (lt_nat_floor_add_one _)⟩ }
lemma measure_ext_Ioo_rat {μ ν : measure ℝ} [is_locally_finite_measure μ]
(h : ∀ a b : ℚ, μ (Ioo a b) = ν (Ioo a b)) : μ = ν :=
(finite_spanning_sets_in_Ioo_rat μ).ext borel_eq_generate_from_Ioo_rat is_pi_system_Ioo_rat $
by { simp only [mem_Union, mem_singleton_iff], rintro _ ⟨a, b, -, rfl⟩, apply h }
lemma borel_eq_generate_from_Iio_rat :
borel ℝ = generate_from (⋃ a : ℚ, {Iio a}) :=
begin
let g : measurable_space ℝ := generate_from (⋃ a : ℚ, {Iio a}),
apply le_antisymm _ (measurable_space.generate_from_le (λ t, _)),
{ rw borel_eq_generate_from_Ioo_rat,
refine generate_from_le (λ t, _),
simp only [mem_Union, mem_singleton_iff], rintro ⟨a, b, h, rfl⟩,
rw (set.ext (λ x, _) : Ioo (a : ℝ) b = (⋃c>a, (Iio c)ᶜ) ∩ Iio b),
{ have hg : ∀ q : ℚ, g.measurable_set' (Iio q) :=
λ q, generate_measurable.basic (Iio q) (by { simp, exact ⟨_, rfl⟩ }),
refine @measurable_set.inter _ g _ _ _ (hg _),
refine @measurable_set.bUnion _ _ g _ _ (countable_encodable _) (λ c h, _),
exact @measurable_set.compl _ _ g (hg _) },
{ suffices : x < ↑b → (↑a < x ↔ ∃ (i : ℚ), a < i ∧ ↑i ≤ x), by simpa,
refine λ _, ⟨λ h, _, λ ⟨i, hai, hix⟩, (rat.cast_lt.2 hai).trans_le hix⟩,
rcases exists_rat_btwn h with ⟨c, ac, cx⟩,
exact ⟨c, rat.cast_lt.1 ac, cx.le⟩ } },
{ simp only [mem_Union, mem_singleton_iff], rintro ⟨r, rfl⟩, exact measurable_set_Iio }
end
end real
variable [measurable_space α]
@[measurability]
lemma measurable_real_to_nnreal : measurable (real.to_nnreal) :=
nnreal.continuous_of_real.measurable
@[measurability]
lemma measurable.real_to_nnreal {f : α → ℝ} (hf : measurable f) :
measurable (λ x, real.to_nnreal (f x)) :=
measurable_real_to_nnreal.comp hf
@[measurability]
lemma ae_measurable.real_to_nnreal {f : α → ℝ} {μ : measure α} (hf : ae_measurable f μ) :
ae_measurable (λ x, real.to_nnreal (f x)) μ :=
measurable_real_to_nnreal.comp_ae_measurable hf
@[measurability]
lemma measurable_coe_nnreal_real : measurable (coe : ℝ≥0 → ℝ) :=
nnreal.continuous_coe.measurable
@[measurability]
lemma measurable.coe_nnreal_real {f : α → ℝ≥0} (hf : measurable f) :
measurable (λ x, (f x : ℝ)) :=
measurable_coe_nnreal_real.comp hf
@[measurability]
lemma ae_measurable.coe_nnreal_real {f : α → ℝ≥0} {μ : measure α} (hf : ae_measurable f μ) :
ae_measurable (λ x, (f x : ℝ)) μ :=
measurable_coe_nnreal_real.comp_ae_measurable hf
@[measurability]
lemma measurable_coe_nnreal_ennreal : measurable (coe : ℝ≥0 → ℝ≥0∞) :=
ennreal.continuous_coe.measurable
@[measurability]
lemma measurable.coe_nnreal_ennreal {f : α → ℝ≥0} (hf : measurable f) :
measurable (λ x, (f x : ℝ≥0∞)) :=
ennreal.continuous_coe.measurable.comp hf
@[measurability]
lemma ae_measurable.coe_nnreal_ennreal {f : α → ℝ≥0} {μ : measure α} (hf : ae_measurable f μ) :
ae_measurable (λ x, (f x : ℝ≥0∞)) μ :=
ennreal.continuous_coe.measurable.comp_ae_measurable hf
@[measurability]
lemma measurable.ennreal_of_real {f : α → ℝ} (hf : measurable f) :
measurable (λ x, ennreal.of_real (f x)) :=
ennreal.continuous_of_real.measurable.comp hf
/-- The set of finite `ℝ≥0∞` numbers is `measurable_equiv` to `ℝ≥0`. -/
def measurable_equiv.ennreal_equiv_nnreal : {r : ℝ≥0∞ | r ≠ ∞} ≃ᵐ ℝ≥0 :=
ennreal.ne_top_homeomorph_nnreal.to_measurable_equiv
namespace ennreal
lemma measurable_of_measurable_nnreal {f : ℝ≥0∞ → α}
(h : measurable (λ p : ℝ≥0, f p)) : measurable f :=
measurable_of_measurable_on_compl_singleton ∞
(measurable_equiv.ennreal_equiv_nnreal.symm.measurable_coe_iff.1 h)
/-- `ℝ≥0∞` is `measurable_equiv` to `ℝ≥0 ⊕ unit`. -/
def ennreal_equiv_sum : ℝ≥0∞ ≃ᵐ ℝ≥0 ⊕ unit :=
{ measurable_to_fun := measurable_of_measurable_nnreal measurable_inl,
measurable_inv_fun := measurable_sum measurable_coe_nnreal_ennreal
(@measurable_const ℝ≥0∞ unit _ _ ∞),
.. equiv.option_equiv_sum_punit ℝ≥0 }
open function (uncurry)
lemma measurable_of_measurable_nnreal_prod [measurable_space β] [measurable_space γ]
{f : ℝ≥0∞ × β → γ} (H₁ : measurable (λ p : ℝ≥0 × β, f (p.1, p.2)))
(H₂ : measurable (λ x, f (∞, x))) :
measurable f :=
let e : ℝ≥0∞ × β ≃ᵐ ℝ≥0 × β ⊕ unit × β :=
(ennreal_equiv_sum.prod_congr (measurable_equiv.refl β)).trans
(measurable_equiv.sum_prod_distrib _ _ _) in
e.symm.measurable_coe_iff.1 $ measurable_sum H₁ (H₂.comp measurable_id.snd)
lemma measurable_of_measurable_nnreal_nnreal [measurable_space β]
{f : ℝ≥0∞ × ℝ≥0∞ → β} (h₁ : measurable (λ p : ℝ≥0 × ℝ≥0, f (p.1, p.2)))
(h₂ : measurable (λ r : ℝ≥0, f (∞, r))) (h₃ : measurable (λ r : ℝ≥0, f (r, ∞))) :
measurable f :=
measurable_of_measurable_nnreal_prod
(measurable_swap_iff.1 $ measurable_of_measurable_nnreal_prod (h₁.comp measurable_swap) h₃)
(measurable_of_measurable_nnreal h₂)
@[measurability]
lemma measurable_of_real : measurable ennreal.of_real :=
ennreal.continuous_of_real.measurable
@[measurability]
lemma measurable_to_real : measurable ennreal.to_real :=
ennreal.measurable_of_measurable_nnreal measurable_coe_nnreal_real
@[measurability]
lemma measurable_to_nnreal : measurable ennreal.to_nnreal :=
ennreal.measurable_of_measurable_nnreal measurable_id
instance : has_measurable_mul₂ ℝ≥0∞ :=
begin
refine ⟨measurable_of_measurable_nnreal_nnreal _ _ _⟩,
{ simp only [← ennreal.coe_mul, measurable_mul.coe_nnreal_ennreal] },
{ simp only [ennreal.top_mul, ennreal.coe_eq_zero],
exact measurable_const.piecewise (measurable_set_singleton _) measurable_const },
{ simp only [ennreal.mul_top, ennreal.coe_eq_zero],
exact measurable_const.piecewise (measurable_set_singleton _) measurable_const }
end
instance : has_measurable_sub₂ ℝ≥0∞ :=
⟨by apply measurable_of_measurable_nnreal_nnreal;
simp [← ennreal.coe_sub, continuous_sub.measurable.coe_nnreal_ennreal]⟩
instance : has_measurable_inv ℝ≥0∞ := ⟨ennreal.continuous_inv.measurable⟩
end ennreal
@[measurability]
lemma measurable.ennreal_to_nnreal {f : α → ℝ≥0∞} (hf : measurable f) :
measurable (λ x, (f x).to_nnreal) :=
ennreal.measurable_to_nnreal.comp hf
@[measurability]
lemma ae_measurable.ennreal_to_nnreal {f : α → ℝ≥0∞} {μ : measure α} (hf : ae_measurable f μ) :
ae_measurable (λ x, (f x).to_nnreal) μ :=
ennreal.measurable_to_nnreal.comp_ae_measurable hf
lemma measurable_coe_nnreal_ennreal_iff {f : α → ℝ≥0} :
measurable (λ x, (f x : ℝ≥0∞)) ↔ measurable f :=
⟨λ h, h.ennreal_to_nnreal, λ h, h.coe_nnreal_ennreal⟩
@[measurability]
lemma measurable.ennreal_to_real {f : α → ℝ≥0∞} (hf : measurable f) :
measurable (λ x, ennreal.to_real (f x)) :=
ennreal.measurable_to_real.comp hf
@[measurability]
lemma ae_measurable.ennreal_to_real {f : α → ℝ≥0∞} {μ : measure α} (hf : ae_measurable f μ) :
ae_measurable (λ x, ennreal.to_real (f x)) μ :=
ennreal.measurable_to_real.comp_ae_measurable hf
/-- note: `ℝ≥0∞` can probably be generalized in a future version of this lemma. -/
@[measurability]
lemma measurable.ennreal_tsum {ι} [encodable ι] {f : ι → α → ℝ≥0∞} (h : ∀ i, measurable (f i)) :
measurable (λ x, ∑' i, f i x) :=
by { simp_rw [ennreal.tsum_eq_supr_sum], apply measurable_supr,
exact λ s, s.measurable_sum (λ i _, h i) }
@[measurability]
lemma measurable.ennreal_tsum' {ι} [encodable ι] {f : ι → α → ℝ≥0∞} (h : ∀ i, measurable (f i)) :
measurable (∑' i, f i) :=
begin
convert measurable.ennreal_tsum h,
ext1 x,
exact tsum_apply (pi.summable.2 (λ _, ennreal.summable)),
end
@[measurability]
lemma measurable.nnreal_tsum {ι} [encodable ι] {f : ι → α → ℝ≥0} (h : ∀ i, measurable (f i)) :
measurable (λ x, ∑' i, f i x) :=
begin
simp_rw [nnreal.tsum_eq_to_nnreal_tsum],
exact (measurable.ennreal_tsum (λ i, (h i).coe_nnreal_ennreal)).ennreal_to_nnreal,
end
@[measurability]
lemma ae_measurable.ennreal_tsum {ι} [encodable ι] {f : ι → α → ℝ≥0∞} {μ : measure α}
(h : ∀ i, ae_measurable (f i) μ) :
ae_measurable (λ x, ∑' i, f i x) μ :=
by { simp_rw [ennreal.tsum_eq_supr_sum], apply ae_measurable_supr,
exact λ s, finset.ae_measurable_sum s (λ i _, h i) }
@[measurability]
lemma measurable_coe_real_ereal : measurable (coe : ℝ → ereal) :=
continuous_coe_real_ereal.measurable
@[measurability]
lemma measurable.coe_real_ereal {f : α → ℝ} (hf : measurable f) :
measurable (λ x, (f x : ereal)) :=
measurable_coe_real_ereal.comp hf
@[measurability]
lemma ae_measurable.coe_real_ereal {f : α → ℝ} {μ : measure α} (hf : ae_measurable f μ) :
ae_measurable (λ x, (f x : ereal)) μ :=
measurable_coe_real_ereal.comp_ae_measurable hf
/-- The set of finite `ereal` numbers is `measurable_equiv` to `ℝ`. -/
def measurable_equiv.ereal_equiv_real : ({⊥, ⊤} : set ereal).compl ≃ᵐ ℝ :=
ereal.ne_bot_top_homeomorph_real.to_measurable_equiv
lemma ereal.measurable_of_measurable_real {f : ereal → α}
(h : measurable (λ p : ℝ, f p)) : measurable f :=
measurable_of_measurable_on_compl_finite {⊥, ⊤} (by simp)
(measurable_equiv.ereal_equiv_real.symm.measurable_coe_iff.1 h)
@[measurability]
lemma measurable_ereal_to_real : measurable ereal.to_real :=
ereal.measurable_of_measurable_real (by simpa using measurable_id)
@[measurability]
lemma measurable.ereal_to_real {f : α → ereal} (hf : measurable f) :
measurable (λ x, (f x).to_real) :=
measurable_ereal_to_real.comp hf
@[measurability]
lemma ae_measurable.ereal_to_real {f : α → ereal} {μ : measure α} (hf : ae_measurable f μ) :
ae_measurable (λ x, (f x).to_real) μ :=
measurable_ereal_to_real.comp_ae_measurable hf
@[measurability]
lemma measurable_coe_ennreal_ereal : measurable (coe : ℝ≥0∞ → ereal) :=
continuous_coe_ennreal_ereal.measurable
@[measurability]
lemma measurable.coe_ereal_ennreal {f : α → ℝ≥0∞} (hf : measurable f) :
measurable (λ x, (f x : ereal)) :=
measurable_coe_ennreal_ereal.comp hf
@[measurability]
lemma ae_measurable.coe_ereal_ennreal {f : α → ℝ≥0∞} {μ : measure α} (hf : ae_measurable f μ) :
ae_measurable (λ x, (f x : ereal)) μ :=
measurable_coe_ennreal_ereal.comp_ae_measurable hf
section normed_group
variables [normed_group α] [opens_measurable_space α] [measurable_space β]
@[measurability]
lemma measurable_norm : measurable (norm : α → ℝ) :=
continuous_norm.measurable
@[measurability]
lemma measurable.norm {f : β → α} (hf : measurable f) : measurable (λ a, norm (f a)) :=
measurable_norm.comp hf
@[measurability]
lemma ae_measurable.norm {f : β → α} {μ : measure β} (hf : ae_measurable f μ) :
ae_measurable (λ a, norm (f a)) μ :=
measurable_norm.comp_ae_measurable hf
@[measurability]
lemma measurable_nnnorm : measurable (nnnorm : α → ℝ≥0) :=
continuous_nnnorm.measurable
@[measurability]
lemma measurable.nnnorm {f : β → α} (hf : measurable f) : measurable (λ a, nnnorm (f a)) :=
measurable_nnnorm.comp hf
@[measurability]
lemma ae_measurable.nnnorm {f : β → α} {μ : measure β} (hf : ae_measurable f μ) :
ae_measurable (λ a, nnnorm (f a)) μ :=
measurable_nnnorm.comp_ae_measurable hf
@[measurability]
lemma measurable_ennnorm : measurable (λ x : α, (nnnorm x : ℝ≥0∞)) :=
measurable_nnnorm.coe_nnreal_ennreal
@[measurability]
lemma measurable.ennnorm {f : β → α} (hf : measurable f) :
measurable (λ a, (nnnorm (f a) : ℝ≥0∞)) :=
hf.nnnorm.coe_nnreal_ennreal
@[measurability]
lemma ae_measurable.ennnorm {f : β → α} {μ : measure β} (hf : ae_measurable f μ) :
ae_measurable (λ a, (nnnorm (f a) : ℝ≥0∞)) μ :=
measurable_ennnorm.comp_ae_measurable hf
end normed_group
section limits
variables [measurable_space β] [metric_space β] [borel_space β]
open metric
/-- A limit (over a general filter) of measurable `ℝ≥0` valued functions is measurable.
The assumption `hs` can be dropped using `filter.is_countably_generated.has_antimono_basis`, but we
don't need that case yet. -/
lemma measurable_of_tendsto_nnreal' {ι ι'} {f : ι → α → ℝ≥0} {g : α → ℝ≥0} (u : filter ι)
[ne_bot u] (hf : ∀ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) {p : ι' → Prop}
{s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) : measurable g :=
begin
rw [tendsto_pi] at lim, rw [← measurable_coe_nnreal_ennreal_iff],
have : ∀ x, liminf u (λ n, (f n x : ℝ≥0∞)) = (g x : ℝ≥0∞) :=
λ x, ((ennreal.continuous_coe.tendsto (g x)).comp (lim x)).liminf_eq,
simp_rw [← this],
show measurable (λ x, liminf u (λ n, (f n x : ℝ≥0∞))),
exact measurable_liminf' (λ i, (hf i).coe_nnreal_ennreal) hu hs,
end
/-- A sequential limit of measurable `ℝ≥0` valued functions is measurable. -/
lemma measurable_of_tendsto_nnreal {f : ℕ → α → ℝ≥0} {g : α → ℝ≥0}
(hf : ∀ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) : measurable g :=
measurable_of_tendsto_nnreal' at_top hf lim at_top_countable_basis (λ i, countable_encodable _)
/-- A limit (over a general filter) of measurable functions valued in a metric space is measurable.
The assumption `hs` can be dropped using `filter.is_countably_generated.has_antimono_basis`, but we
don't need that case yet. -/
lemma measurable_of_tendsto_metric' {ι ι'} {f : ι → α → β} {g : α → β}
(u : filter ι) [ne_bot u] (hf : ∀ i, measurable (f i)) (lim : tendsto f u (𝓝 g)) {p : ι' → Prop}
{s : ι' → set ι} (hu : u.has_countable_basis p s) (hs : ∀ i, (s i).countable) :
measurable g :=
begin
apply measurable_of_is_closed', intros s h1s h2s h3s,
have : measurable (λ x, inf_nndist (g x) s),
{ refine measurable_of_tendsto_nnreal' u (λ i, (hf i).inf_nndist) _ hu hs, swap,
rw [tendsto_pi], rw [tendsto_pi] at lim, intro x,
exact ((continuous_inf_nndist_pt s).tendsto (g x)).comp (lim x) },
have h4s : g ⁻¹' s = (λ x, inf_nndist (g x) s) ⁻¹' {0},
{ ext x, simp [h1s, ← mem_iff_inf_dist_zero_of_closed h1s h2s, ← nnreal.coe_eq_zero] },
rw [h4s], exact this (measurable_set_singleton 0),
end
/-- A sequential limit of measurable functions valued in a metric space is measurable. -/
lemma measurable_of_tendsto_metric {f : ℕ → α → β} {g : α → β}
(hf : ∀ i, measurable (f i)) (lim : tendsto f at_top (𝓝 g)) :
measurable g :=
measurable_of_tendsto_metric' at_top hf lim at_top_countable_basis (λ i, countable_encodable _)
lemma ae_measurable_of_tendsto_metric_ae {μ : measure α} {f : ℕ → α → β} {g : α → β}
(hf : ∀ n, ae_measurable (f n) μ)
(h_ae_tendsto : ∀ᵐ x ∂μ, filter.at_top.tendsto (λ n, f n x) (𝓝 (g x))) :
ae_measurable g μ :=
begin
let p : α → (ℕ → β) → Prop := λ x f', filter.at_top.tendsto (λ n, f' n) (𝓝 (g x)),
let hp : ∀ᵐ x ∂μ, p x (λ n, f n x), from h_ae_tendsto,
let ae_seq_lim := λ x, ite (x ∈ ae_seq_set hf p) (g x) (⟨f 0 x⟩ : nonempty β).some,
refine ⟨ae_seq_lim, _, (ite_ae_eq_of_measure_compl_zero g (λ x, (⟨f 0 x⟩ : nonempty β).some)
(ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hp)).symm⟩,
refine measurable_of_tendsto_metric (@ae_seq.measurable α β _ _ _ f μ hf p) _,
refine tendsto_pi.mpr (λ x, _),
simp_rw [ae_seq, ae_seq_lim],
split_ifs with hx,
{ simp_rw ae_seq.mk_eq_fun_of_mem_ae_seq_set hf hx,
exact @ae_seq.fun_prop_of_mem_ae_seq_set α β _ _ _ _ _ _ hf x hx, },
{ exact tendsto_const_nhds, },
end
lemma measurable_of_tendsto_metric_ae {μ : measure α} [μ.is_complete] {f : ℕ → α → β} {g : α → β}
(hf : ∀ n, measurable (f n))
(h_ae_tendsto : ∀ᵐ x ∂μ, filter.at_top.tendsto (λ n, f n x) (𝓝 (g x))) :
measurable g :=
ae_measurable_iff_measurable.mp
(ae_measurable_of_tendsto_metric_ae (λ i, (hf i).ae_measurable) h_ae_tendsto)
lemma measurable_limit_of_tendsto_metric_ae {μ : measure α} {f : ℕ → α → β}
(hf : ∀ n, ae_measurable (f n) μ)
(h_ae_tendsto : ∀ᵐ x ∂μ, ∃ l : β, filter.at_top.tendsto (λ n, f n x) (𝓝 l)) :
∃ (f_lim : α → β) (hf_lim_meas : measurable f_lim),
∀ᵐ x ∂μ, filter.at_top.tendsto (λ n, f n x) (𝓝 (f_lim x)) :=
begin
let p : α → (ℕ → β) → Prop := λ x f', ∃ l : β, filter.at_top.tendsto (λ n, f' n) (𝓝 l),
have hp_mem : ∀ x, x ∈ ae_seq_set hf p → p x (λ n, f n x),
from λ x hx, ae_seq.fun_prop_of_mem_ae_seq_set hf hx,
have hμ_compl : μ (ae_seq_set hf p)ᶜ = 0,
from ae_seq.measure_compl_ae_seq_set_eq_zero hf h_ae_tendsto,
let f_lim : α → β := λ x, dite (x ∈ ae_seq_set hf p) (λ h, (hp_mem x h).some)
(λ h, (⟨f 0 x⟩ : nonempty β).some),
have hf_lim_conv : ∀ x, x ∈ ae_seq_set hf p → filter.at_top.tendsto (λ n, f n x) (𝓝 (f_lim x)),
{ intros x hx_conv,
simp only [f_lim, hx_conv, dif_pos],
exact (hp_mem x hx_conv).some_spec, },
have hf_lim : ∀ x, filter.at_top.tendsto (λ n, ae_seq hf p n x) (𝓝 (f_lim x)),
{ intros x,
simp only [f_lim, ae_seq],
split_ifs,
{ rw funext (λ n, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h n),
exact (hp_mem x h).some_spec, },
{ exact tendsto_const_nhds, }, },
have h_ae_tendsto_f_lim : ∀ᵐ x ∂μ, filter.at_top.tendsto (λ n, f n x) (𝓝 (f_lim x)),
{ refine le_antisymm (le_of_eq (measure_mono_null _ hμ_compl)) (zero_le _),
exact set.compl_subset_compl.mpr (λ x hx, hf_lim_conv x hx), },
have h_f_lim_meas : measurable f_lim,
from measurable_of_tendsto_metric (ae_seq.measurable hf p) (tendsto_pi.mpr (λ x, hf_lim x)),
exact ⟨f_lim, h_f_lim_meas, h_ae_tendsto_f_lim⟩,
end
end limits
namespace continuous_linear_map
variables {𝕜 : Type*} [normed_field 𝕜]
variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E]
variables [opens_measurable_space E]
variables {F : Type*} [normed_group F] [normed_space 𝕜 F] [measurable_space F] [borel_space F]
@[measurability]
protected lemma measurable (L : E →L[𝕜] F) : measurable L :=
L.continuous.measurable
lemma measurable_comp (L : E →L[𝕜] F) {φ : α → E} (φ_meas : measurable φ) :
measurable (λ (a : α), L (φ a)) :=
L.measurable.comp φ_meas
end continuous_linear_map
namespace continuous_linear_map
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
variables {E : Type*} [normed_group E] [normed_space 𝕜 E]
{F : Type*} [normed_group F] [normed_space 𝕜 F]
instance : measurable_space (E →L[𝕜] F) := borel _
instance : borel_space (E →L[𝕜] F) := ⟨rfl⟩
@[measurability]
lemma measurable_apply [measurable_space F] [borel_space F] (x : E) :
measurable (λ f : E →L[𝕜] F, f x) :=
(apply 𝕜 F x).continuous.measurable
@[measurability]
lemma measurable_apply' [measurable_space E] [opens_measurable_space E]
[measurable_space F] [borel_space F] :
measurable (λ (x : E) (f : E →L[𝕜] F), f x) :=
measurable_pi_lambda _ $ λ f, f.measurable
@[measurability]
lemma measurable_coe [measurable_space F] [borel_space F] :
measurable (λ (f : E →L[𝕜] F) (x : E), f x) :=
measurable_pi_lambda _ measurable_apply
end continuous_linear_map
section continuous_linear_map_nondiscrete_normed_field
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] [borel_space E]
variables {F : Type*} [normed_group F] [normed_space 𝕜 F]
@[measurability]
lemma measurable.apply_continuous_linear_map {φ : α → F →L[𝕜] E} (hφ : measurable φ) (v : F) :
measurable (λ a, φ a v) :=
(continuous_linear_map.apply 𝕜 E v).measurable.comp hφ
@[measurability]
lemma ae_measurable.apply_continuous_linear_map {φ : α → F →L[𝕜] E} {μ : measure α}
(hφ : ae_measurable φ μ) (v : F) : ae_measurable (λ a, φ a v) μ :=
(continuous_linear_map.apply 𝕜 E v).measurable.comp_ae_measurable hφ
end continuous_linear_map_nondiscrete_normed_field
section normed_space
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] [measurable_space 𝕜]
variables [borel_space 𝕜]
variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] [borel_space E]
lemma measurable_smul_const {f : α → 𝕜} {c : E} (hc : c ≠ 0) :
measurable (λ x, f x • c) ↔ measurable f :=
measurable_comp_iff_of_closed_embedding (λ y : 𝕜, y • c) (closed_embedding_smul_left hc)
lemma ae_measurable_smul_const {f : α → 𝕜} {μ : measure α} {c : E} (hc : c ≠ 0) :
ae_measurable (λ x, f x • c) μ ↔ ae_measurable f μ :=
ae_measurable_comp_iff_of_closed_embedding (λ y : 𝕜, y • c) (closed_embedding_smul_left hc)
end normed_space
lemma is_compact.measure_lt_top_of_nhds_within [topological_space α]
{s : set α} {μ : measure α} (h : is_compact s) (hμ : ∀ x ∈ s, μ.finite_at_filter (𝓝[s] x)) :
μ s < ∞ :=
is_compact.induction_on h (by simp) (λ s t hst ht, (measure_mono hst).trans_lt ht)
(λ s t hs ht, (measure_union_le s t).trans_lt (ennreal.add_lt_top.2 ⟨hs, ht⟩)) hμ
lemma is_compact.measure_lt_top [topological_space α] {s : set α} {μ : measure α}
[is_locally_finite_measure μ] (h : is_compact s) :
μ s < ∞ :=
h.measure_lt_top_of_nhds_within $ λ x hx, μ.finite_at_nhds_within _ _
|
18be2c6d02a4f067feb834046e168ef95e29c32d | 8cb37a089cdb4af3af9d8bf1002b417e407a8e9e | /library/init/meta/pexpr.lean | 8fc648c8555b617649397b3fd51d253490571f59 | [
"Apache-2.0"
] | permissive | kbuzzard/lean | ae3c3db4bb462d750dbf7419b28bafb3ec983ef7 | ed1788fd674bb8991acffc8fca585ec746711928 | refs/heads/master | 1,620,983,366,617 | 1,618,937,600,000 | 1,618,937,600,000 | 359,886,396 | 1 | 0 | Apache-2.0 | 1,618,936,987,000 | 1,618,936,987,000 | null | UTF-8 | Lean | false | false | 1,537 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.meta.expr
universe u
/-- Quoted expressions. They can be converted into expressions by using a tactic. -/
@[reducible] meta def pexpr := expr ff
protected meta constant pexpr.of_expr : expr → pexpr
meta constant pexpr.is_placeholder : pexpr → bool
meta constant pexpr.mk_placeholder : pexpr
meta constant pexpr.mk_field_macro : pexpr → name → pexpr
meta constant pexpr.mk_explicit : pexpr → pexpr
/-- Choice macros are used to implement overloading. -/
meta constant pexpr.is_choice_macro : pexpr → bool
/-- Information about unelaborated structure instance expressions. -/
meta structure structure_instance_info :=
(struct : option name := none)
(field_names : list name)
(field_values : list pexpr)
(sources : list pexpr := [])
/-- Create a structure instance expression. -/
meta constant pexpr.mk_structure_instance : structure_instance_info → pexpr
meta constant pexpr.get_structure_instance_info : pexpr → option structure_instance_info
meta class has_to_pexpr (α : Sort u) :=
(to_pexpr : α → pexpr)
meta def to_pexpr {α : Sort u} [has_to_pexpr α] : α → pexpr :=
has_to_pexpr.to_pexpr
meta instance : has_to_pexpr pexpr :=
⟨id⟩
meta instance : has_to_pexpr expr :=
⟨pexpr.of_expr⟩
meta instance (α : Sort u) (a : α) : has_to_pexpr (reflected a) :=
⟨pexpr.of_expr ∘ reflected.to_expr⟩
|
e2950c63ca4ccd903881bd1970b6f2c470a02066 | 737dc4b96c97368cb66b925eeea3ab633ec3d702 | /stage0/src/Lean/Meta/Tactic/Subst.lean | 2e6690862ad7829e86d7b10fd8f3f6ec203e9dc6 | [
"Apache-2.0"
] | permissive | Bioye97/lean4 | 1ace34638efd9913dc5991443777b01a08983289 | bc3900cbb9adda83eed7e6affeaade7cfd07716d | refs/heads/master | 1,690,589,820,211 | 1,631,051,000,000 | 1,631,067,598,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,357 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.AppBuilder
import Lean.Meta.MatchUtil
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.Revert
import Lean.Meta.Tactic.Assert
import Lean.Meta.Tactic.Intro
import Lean.Meta.Tactic.Clear
import Lean.Meta.Tactic.FVarSubst
namespace Lean.Meta
def substCore (mvarId : MVarId) (hFVarId : FVarId) (symm := false) (fvarSubst : FVarSubst := {}) (clearH := true) (tryToSkip := false) : MetaM (FVarSubst × MVarId) :=
withMVarContext mvarId do
let tag ← getMVarTag mvarId
checkNotAssigned mvarId `subst
let hFVarIdOriginal := hFVarId
let hLocalDecl ← getLocalDecl hFVarId
match (← matchEq? hLocalDecl.type) with
| none => throwTacticEx `subst mvarId "argument must be an equality proof"
| some (α, lhs, rhs) => do
let a ← instantiateMVars <| if symm then rhs else lhs
let b ← instantiateMVars <| if symm then lhs else rhs
match a with
| Expr.fvar aFVarId _ => do
let aFVarIdOriginal := aFVarId
trace[Meta.Tactic.subst] "substituting {a} (id: {aFVarId}) with {b}"
let mctx ← getMCtx
if mctx.exprDependsOn b aFVarId then
throwTacticEx `subst mvarId m!"'{a}' occurs at{indentExpr b}"
let aLocalDecl ← getLocalDecl aFVarId
let (vars, mvarId) ← revert mvarId #[aFVarId, hFVarId] true
trace[Meta.Tactic.subst] "after revert {MessageData.ofGoal mvarId}"
let (twoVars, mvarId) ← introNP mvarId 2
trace[Meta.Tactic.subst] "after intro2 {MessageData.ofGoal mvarId}"
trace[Meta.Tactic.subst] "reverted variables {vars}"
let aFVarId := twoVars[0]
let a := mkFVar aFVarId
let hFVarId := twoVars[1]
let h := mkFVar hFVarId
/- Set skip to true if there is no local variable nor the target depend on the equality -/
let skip ←
if !tryToSkip || vars.size != 2 then
pure false
else
let mvarType ← getMVarType mvarId
let mctx ← getMCtx
pure (!mctx.exprDependsOn mvarType aFVarId && !mctx.exprDependsOn mvarType hFVarId)
if skip then
if clearH then
let mvarId ← clear mvarId hFVarId
let mvarId ← clear mvarId aFVarId
pure ({}, mvarId)
else
pure ({}, mvarId)
else
withMVarContext mvarId do
let mvarDecl ← getMVarDecl mvarId
let type := mvarDecl.type
let hLocalDecl ← getLocalDecl hFVarId
match (← matchEq? hLocalDecl.type) with
| none => unreachable!
| some (α, lhs, rhs) => do
let b ← instantiateMVars <| if symm then lhs else rhs
let mctx ← getMCtx
let depElim := mctx.exprDependsOn mvarDecl.type hFVarId
let cont (motive : Expr) (newType : Expr) : MetaM (FVarSubst × MVarId) := do
let major ← if symm then pure h else mkEqSymm h
let newMVar ← mkFreshExprSyntheticOpaqueMVar newType tag
let minor := newMVar
let newVal ← if depElim then mkEqRec motive minor major else mkEqNDRec motive minor major
assignExprMVar mvarId newVal
let mvarId := newMVar.mvarId!
let mvarId ←
if clearH then
let mvarId ← clear mvarId hFVarId
clear mvarId aFVarId
else
pure mvarId
let (newFVars, mvarId) ← introNP mvarId (vars.size - 2)
trace[Meta.Tactic.subst] "after intro rest {vars.size - 2} {MessageData.ofGoal mvarId}"
let fvarSubst ← newFVars.size.foldM (init := fvarSubst) fun i (fvarSubst : FVarSubst) =>
let var := vars[i+2]
let newFVar := newFVars[i]
pure $ fvarSubst.insert var (mkFVar newFVar)
let fvarSubst := fvarSubst.insert aFVarIdOriginal (if clearH then b else mkFVar aFVarId)
let fvarSubst := fvarSubst.insert hFVarIdOriginal (mkFVar hFVarId)
pure (fvarSubst, mvarId)
if depElim then do
let newType := type.replaceFVar a b
let reflB ← mkEqRefl b
let newType := newType.replaceFVar h reflB
if symm then
let motive ← mkLambdaFVars #[a, h] type
cont motive newType
else
/- `type` depends on (h : a = b). So, we use the following trick to avoid a type incorrect motive.
1- Create a new local (hAux : b = a)
2- Create newType := type [hAux.symm / h]
`newType` is type correct because `h` and `hAux.symm` are definitionally equal by proof irrelevance.
3- Create motive by abstracting `a` and `hAux` in `newType`. -/
let hAuxType ← mkEq b a
let motive ← withLocalDeclD `_h hAuxType fun hAux => do
let hAuxSymm ← mkEqSymm hAux
/- replace h in type with hAuxSymm -/
let newType := type.replaceFVar h hAuxSymm
mkLambdaFVars #[a, hAux] newType
cont motive newType
else
let motive ← mkLambdaFVars #[a] type
let newType := type.replaceFVar a b
cont motive newType
| _ =>
let eqMsg := if symm then "(t = x)" else "(x = t)"
throwTacticEx `subst mvarId
m!"invalid equality proof, it is not of the form {eqMsg}{indentExpr hLocalDecl.type}\nafter WHNF, variable expected, but obtained{indentExpr a}"
def subst (mvarId : MVarId) (hFVarId : FVarId) : MetaM MVarId :=
withMVarContext mvarId do
let hLocalDecl ← getLocalDecl hFVarId
match (← matchEq? hLocalDecl.type) with
| some (α, lhs, rhs) =>
let substReduced (newType : Expr) (symm : Bool) : MetaM MVarId := do
let mvarId ← assert mvarId hLocalDecl.userName newType (mkFVar hFVarId)
let (hFVarId', mvarId) ← intro1P mvarId
let mvarId ← clear mvarId hFVarId
return (← substCore mvarId hFVarId' (symm := symm) (tryToSkip := true)).2
let rhs' ← whnf rhs
if rhs'.isFVar then
if rhs != rhs' then
substReduced (← mkEq lhs rhs') true
else
return (← substCore mvarId hFVarId (symm := true) (tryToSkip := true)).2
else do
let lhs' ← whnf lhs
if lhs'.isFVar then
if lhs != lhs' then
substReduced (← mkEq lhs' rhs) false
else
return (← substCore mvarId hFVarId (symm := false) (tryToSkip := true)).2
else do
throwTacticEx `subst mvarId m!"invalid equality proof, it is not of the form (x = t) or (t = x){indentExpr hLocalDecl.type}"
| none =>
if hLocalDecl.isLet then
throwTacticEx `subst mvarId m!"variable '{mkFVar hFVarId}' is a let-declaration"
let mctx ← getMCtx
let lctx ← getLCtx
let some (fvarId, symm) ← lctx.findDeclM? fun localDecl => do
if localDecl.isAuxDecl then
return none
else
match (← matchEq? localDecl.type) with
| some (α, lhs, rhs) =>
if rhs.isFVar && rhs.fvarId! == hFVarId && !mctx.exprDependsOn lhs hFVarId then
return some (localDecl.fvarId, true)
else if lhs.isFVar && lhs.fvarId! == hFVarId && !mctx.exprDependsOn rhs hFVarId then
return some (localDecl.fvarId, false)
else
return none
| _ => return none
| throwTacticEx `subst mvarId m!"did not find equation for eliminating '{mkFVar hFVarId}'"
return (← substCore mvarId fvarId (symm := symm) (tryToSkip := true)).2
def trySubst (mvarId : MVarId) (hFVarId : FVarId) : MetaM MVarId := do
match (← observing? (subst mvarId hFVarId)) with
| some mvarId => return mvarId
| none => return mvarId
builtin_initialize registerTraceClass `Meta.Tactic.subst
end Meta
end Lean
|
9d8c5c8092ccb4140840b366ba9bb0f309a72bba | 853df553b1d6ca524e3f0a79aedd32dde5d27ec3 | /src/order/filter/filter_product.lean | dfc28f23781fb57bdeb866e571748d50d94fd6ac | [
"Apache-2.0"
] | permissive | DanielFabian/mathlib | efc3a50b5dde303c59eeb6353ef4c35a345d7112 | f520d07eba0c852e96fe26da71d85bf6d40fcc2a | refs/heads/master | 1,668,739,922,971 | 1,595,201,756,000 | 1,595,201,756,000 | 279,469,476 | 0 | 0 | null | 1,594,696,604,000 | 1,594,696,604,000 | null | UTF-8 | Lean | false | false | 8,261 | lean | /-
Copyright (c) 2019 Abhimanyu Pallavi Sudhir. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Abhimanyu Pallavi Sudhir
-/
import order.filter.ultrafilter
import order.filter.germ
import algebra.pi_instances
/-!
# Ultraproducts
If `φ` is an ultrafilter, then the space of germs of functions `f : α → β` at `φ` is called
the *ultraproduct*. In this file we prove properties of ultraproducts that rely on `φ` being an
ultrafilter. Definitions and properties that work for any filter should go to `order.filter.germ`.
## Tags
ultrafilter, ultraproduct
-/
universes u v
variables {α : Type u} {β : Type v} {φ : filter α}
open_locale classical
namespace filter
local notation `∀*` binders `, ` r:(scoped p, filter.eventually p φ) := r
namespace germ
local notation `β*` := germ φ β
/-- If `φ` is an ultrafilter then the ultraproduct is a division ring.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected def division_ring [division_ring β] (U : is_ultrafilter φ) : division_ring β* :=
{ mul_inv_cancel := λ f, induction_on f $ λ f hf, coe_eq.2 $ (U.em (λ y, f y = 0)).elim
(λ H, (hf $ coe_eq.2 H).elim) (λ H, H.mono $ λ x, mul_inv_cancel),
inv_mul_cancel := λ f, induction_on f $ λ f hf, coe_eq.2 $ (U.em (λ y, f y = 0)).elim
(λ H, (hf $ coe_eq.2 H).elim) (λ H, H.mono $ λ x, inv_mul_cancel),
inv_zero := coe_eq.2 $ by simp only [(∘), inv_zero],
.. germ.ring, .. germ.has_inv, .. germ.nontrivial U.1 }
/-- If `φ` is an ultrafilter then the ultraproduct is a field.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected def field [field β] (U : is_ultrafilter φ) : field β* :=
{ .. germ.comm_ring, .. germ.division_ring U }
/-- If `φ` is an ultrafilter then the ultraproduct is a linear order.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected def linear_order [linear_order β] (U : is_ultrafilter φ) : linear_order β* :=
{ le_total := λ f g, induction_on₂ f g $ λ f g, U.eventually_or.1 $ eventually_of_forall $
λ x, le_total _ _,
.. germ.partial_order }
@[simp, norm_cast] lemma const_div [division_ring β] (U : is_ultrafilter φ) (x y : β) :
(↑(x / y) : β*) = @has_div.div _ (@division_ring_has_div _ (germ.division_ring U)) ↑x ↑y :=
rfl
lemma coe_lt [preorder β] (U : is_ultrafilter φ) {f g : α → β} :
(f : β*) < g ↔ ∀* x, f x < g x :=
by simp only [lt_iff_le_not_le, eventually_and, coe_le, U.eventually_not, eventually_le]
lemma coe_pos [preorder β] [has_zero β] (U : is_ultrafilter φ) {f : α → β} :
0 < (f : β*) ↔ ∀* x, 0 < f x :=
coe_lt U
lemma const_lt [preorder β] (U : is_ultrafilter φ) {x y : β} :
(↑x : β*) < ↑y ↔ x < y :=
(coe_lt U).trans $ lift_rel_const_iff U.1
lemma lt_def [preorder β] (U : is_ultrafilter φ) :
((<) : β* → β* → Prop) = lift_rel (<) :=
by { ext ⟨f⟩ ⟨g⟩, exact coe_lt U }
/-- If `φ` is an ultrafilter then the ultraproduct is an ordered ring.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected def ordered_ring [ordered_ring β] (U : is_ultrafilter φ) : ordered_ring β* :=
{ mul_pos := λ x y, induction_on₂ x y $ λ f g hf hg, (coe_pos U).2 $
((coe_pos U).1 hg).mp $ ((coe_pos U).1 hf).mono $ λ x, mul_pos,
.. germ.ring, .. germ.ordered_add_comm_group, .. germ.nontrivial U.1 }
/-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered ring.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected def linear_ordered_ring [linear_ordered_ring β] (U : is_ultrafilter φ) :
linear_ordered_ring β* :=
{ zero_lt_one := by rw lt_def U; show (∀* i, (0 : β) < 1); simp [zero_lt_one],
.. germ.ordered_ring U, .. germ.linear_order U }
/-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered field.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected def linear_ordered_field [linear_ordered_field β] (U : is_ultrafilter φ) :
linear_ordered_field β* :=
{ .. germ.linear_ordered_ring U, .. germ.field U }
/-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered commutative ring.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected def linear_ordered_comm_ring [linear_ordered_comm_ring β] (U : is_ultrafilter φ) :
linear_ordered_comm_ring β* :=
{ .. germ.linear_ordered_ring U, .. germ.comm_monoid }
/-- If `φ` is an ultrafilter then the ultraproduct is a decidable linear order.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected noncomputable def decidable_linear_order [decidable_linear_order β]
(U : is_ultrafilter φ) :
decidable_linear_order β* :=
{ decidable_le := by apply_instance,
.. germ.linear_order U }
/-- If `φ` is an ultrafilter then the ultraproduct is a decidable linear ordered commutative group.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected noncomputable def decidable_linear_ordered_add_comm_group
[decidable_linear_ordered_add_comm_group β] (U : is_ultrafilter φ) :
decidable_linear_ordered_add_comm_group β* :=
{ .. germ.ordered_add_comm_group, .. germ.decidable_linear_order U }
/-- If `φ` is an ultrafilter then the ultraproduct is a decidable linear ordered commutative ring.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected noncomputable def decidable_linear_ordered_comm_ring
[decidable_linear_ordered_comm_ring β] (U : is_ultrafilter φ) :
decidable_linear_ordered_comm_ring β* :=
{ .. germ.linear_ordered_comm_ring U,
.. germ.decidable_linear_ordered_add_comm_group U }
/-- If `φ` is an ultrafilter then the ultraproduct is a discrete linear ordered field.
This cannot be an instance, since it depends on `φ` being an ultrafilter. -/
protected noncomputable def discrete_linear_ordered_field [discrete_linear_ordered_field β]
(U : is_ultrafilter φ) : discrete_linear_ordered_field β* :=
{ .. germ.linear_ordered_field U, .. germ.decidable_linear_ordered_comm_ring U,
.. germ.field U }
lemma max_def [K : decidable_linear_order β] (U : is_ultrafilter φ) (x y : β*) :
@max β* (germ.decidable_linear_order U) x y = map₂ max x y :=
quotient.induction_on₂' x y $ λ a b, by unfold max;
begin
split_ifs,
exact quotient.sound'(by filter_upwards [h] λ i hi, (max_eq_right hi).symm),
exact quotient.sound'(by filter_upwards [@le_of_not_le _ (germ.linear_order U) _ _ h]
λ i hi, (max_eq_left hi).symm),
end
lemma min_def [K : decidable_linear_order β] (U : is_ultrafilter φ) (x y : β*) :
@min β* (germ.decidable_linear_order U) x y = map₂ min x y :=
quotient.induction_on₂' x y $ λ a b, by unfold min;
begin
split_ifs,
exact quotient.sound'(by filter_upwards [h] λ i hi, (min_eq_left hi).symm),
exact quotient.sound'(by filter_upwards [@le_of_not_le _ (germ.linear_order U) _ _ h]
λ i hi, (min_eq_right hi).symm),
end
lemma abs_def [decidable_linear_ordered_add_comm_group β] (U : is_ultrafilter φ) (x : β*) :
@abs _ (germ.decidable_linear_ordered_add_comm_group U) x = map abs x :=
quotient.induction_on' x $ λ a, by unfold abs; rw max_def;
exact quotient.sound' (show ∀* i, abs _ = _, by simp)
@[simp] lemma const_max [decidable_linear_order β] (U : is_ultrafilter φ) (x y : β) :
(↑(max x y : β) : β*) = @max _ (germ.decidable_linear_order U) ↑x ↑y :=
begin
unfold max, split_ifs,
{ refl },
{ exact false.elim (h_1 $ const_le h) },
{ exact false.elim (h ((const_le_iff U.1).mp h_1)) },
{ refl }
end
@[simp] lemma const_min [decidable_linear_order β] (U : is_ultrafilter φ) (x y : β) :
(↑(min x y : β) : β*) = @min _ (germ.decidable_linear_order U) ↑x ↑y :=
begin
unfold min, split_ifs; try { refl }; apply false.elim,
{ exact (h_1 $ const_le h) },
{ exact (h $ (const_le_iff U.1).mp h_1) },
end
@[simp] lemma const_abs [decidable_linear_ordered_add_comm_group β] (U : is_ultrafilter φ) (x : β) :
(↑(abs x) : β*) = @abs _ (germ.decidable_linear_ordered_add_comm_group U) ↑x :=
const_max U x (-x)
end germ
end filter
|
412df6d2dc3a4e795442afd8dcf9678fb89b8f42 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/algebra/group/inj_surj.lean | de314a72f5592999ca4c8fdc32111ae6daec7e39 | [
"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,886 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import algebra.group.defs
import logic.function.basic
/-!
# Lifting algebraic data classes along injective/surjective maps
This file provides definitions that are meant to deal with
situations such as the following:
Suppose that `G` is a group, and `H` is a type endowed with
`has_one H`, `has_mul H`, and `has_inv H`.
Suppose furthermore, that `f : G → H` is a surjective map
that respects the multiplication, and the unit elements.
Then `H` satisfies the group axioms.
The relevant definition in this case is `function.surjective.group`.
Dually, there is also `function.injective.group`.
And there are versions for (additive) (commutative) semigroups/monoids.
-/
namespace function
/-!
### Injective
-/
namespace injective
variables {M₁ : Type*} {M₂ : Type*} [has_mul M₁]
/-- A type endowed with `*` is a semigroup,
if it admits an injective map that preserves `*` to a semigroup. -/
@[to_additive
"A type endowed with `+` is an additive semigroup,
if it admits an injective map that preserves `+` to an additive semigroup."]
protected def semigroup [semigroup M₂] (f : M₁ → M₂) (hf : injective f)
(mul : ∀ x y, f (x * y) = f x * f y) :
semigroup M₁ :=
{ mul_assoc := λ x y z, hf $ by erw [mul, mul, mul, mul, mul_assoc],
..‹has_mul M₁› }
/-- A type endowed with `*` is a commutative semigroup,
if it admits an injective map that preserves `*` to a commutative semigroup. -/
@[to_additive
"A type endowed with `+` is an additive commutative semigroup,
if it admits an injective map that preserves `+` to an additive commutative semigroup."]
protected def comm_semigroup [comm_semigroup M₂] (f : M₁ → M₂) (hf : injective f)
(mul : ∀ x y, f (x * y) = f x * f y) :
comm_semigroup M₁ :=
{ mul_comm := λ x y, hf $ by erw [mul, mul, mul_comm],
.. hf.semigroup f mul }
/-- A type endowed with `*` is a left cancel semigroup,
if it admits an injective map that preserves `*` to a left cancel semigroup. -/
@[to_additive add_left_cancel_semigroup
"A type endowed with `+` is an additive left cancel semigroup,
if it admits an injective map that preserves `+` to an additive left cancel semigroup."]
protected def left_cancel_semigroup [left_cancel_semigroup M₂] (f : M₁ → M₂) (hf : injective f)
(mul : ∀ x y, f (x * y) = f x * f y) :
left_cancel_semigroup M₁ :=
{ mul := (*),
mul_left_cancel := λ x y z H, hf $ (mul_right_inj (f x)).1 $ by erw [← mul, ← mul, H]; refl,
.. hf.semigroup f mul }
/-- A type endowed with `*` is a right cancel semigroup,
if it admits an injective map that preserves `*` to a right cancel semigroup. -/
@[to_additive add_right_cancel_semigroup
"A type endowed with `+` is an additive right cancel semigroup,
if it admits an injective map that preserves `+` to an additive right cancel semigroup."]
protected def right_cancel_semigroup [right_cancel_semigroup M₂] (f : M₁ → M₂) (hf : injective f)
(mul : ∀ x y, f (x * y) = f x * f y) :
right_cancel_semigroup M₁ :=
{ mul := (*),
mul_right_cancel := λ x y z H, hf $ (mul_left_inj (f y)).1 $ by erw [← mul, ← mul, H]; refl,
.. hf.semigroup f mul }
variables [has_one M₁]
/-- A type endowed with `1` and `*` is a monoid,
if it admits an injective map that preserves `1` and `*` to a monoid. -/
@[to_additive
"A type endowed with `0` and `+` is an additive monoid,
if it admits an injective map that preserves `0` and `+` to an additive monoid."]
protected def monoid [monoid M₂] (f : M₁ → M₂) (hf : injective f)
(one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) :
monoid M₁ :=
{ one_mul := λ x, hf $ by erw [mul, one, one_mul],
mul_one := λ x, hf $ by erw [mul, one, mul_one],
.. hf.semigroup f mul, ..‹has_one M₁› }
/-- A type endowed with `1` and `*` is a left cancel monoid,
if it admits an injective map that preserves `1` and `*` to a left cancel monoid. -/
@[to_additive add_left_cancel_monoid
"A type endowed with `0` and `+` is an additive left cancel monoid,
if it admits an injective map that preserves `0` and `+` to an additive left cancel monoid."]
protected def left_cancel_monoid [left_cancel_monoid M₂] (f : M₁ → M₂) (hf : injective f)
(one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) :
left_cancel_monoid M₁ :=
{ .. hf.left_cancel_semigroup f mul, .. hf.monoid f one mul }
/-- A type endowed with `1` and `*` is a commutative monoid,
if it admits an injective map that preserves `1` and `*` to a commutative monoid. -/
@[to_additive
"A type endowed with `0` and `+` is an additive commutative monoid,
if it admits an injective map that preserves `0` and `+` to an additive commutative monoid."]
protected def comm_monoid [comm_monoid M₂] (f : M₁ → M₂) (hf : injective f)
(one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) :
comm_monoid M₁ :=
{ .. hf.comm_semigroup f mul, .. hf.monoid f one mul }
variables [has_inv M₁]
/-- A type endowed with `1`, `*` and `⁻¹` is a group,
if it admits an injective map that preserves `1`, `*` and `⁻¹` to a group. -/
@[to_additive
"A type endowed with `0` and `+` is an additive group,
if it admits an injective map that preserves `0` and `+` to an additive group."]
protected def group [group M₂] (f : M₁ → M₂) (hf : injective f)
(one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) :
group M₁ :=
{ mul_left_inv := λ x, hf $ by erw [mul, inv, mul_left_inv, one],
.. hf.monoid f one mul, ..‹has_inv M₁› }
/-- A type endowed with `1`, `*` and `⁻¹` is a commutative group,
if it admits an injective map that preserves `1`, `*` and `⁻¹` to a commutative group. -/
@[to_additive
"A type endowed with `0` and `+` is an additive commutative group,
if it admits an injective map that preserves `0` and `+` to an additive commutative group."]
protected def comm_group [comm_group M₂] (f : M₁ → M₂) (hf : injective f)
(one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) :
comm_group M₁ :=
{ .. hf.comm_monoid f one mul, .. hf.group f one mul inv }
end injective
/-!
### Surjective
-/
namespace surjective
variables {M₁ : Type*} {M₂ : Type*} [has_mul M₂]
/-- A type endowed with `*` is a semigroup,
if it admits a surjective map that preserves `*` from a semigroup. -/
@[to_additive
"A type endowed with `+` is an additive semigroup,
if it admits a surjective map that preserves `+` from an additive semigroup."]
protected def semigroup [semigroup M₁] (f : M₁ → M₂) (hf : surjective f)
(mul : ∀ x y, f (x * y) = f x * f y) :
semigroup M₂ :=
{ mul_assoc := hf.forall₃.2 $ λ x y z, by simp only [← mul, mul_assoc],
..‹has_mul M₂› }
/-- A type endowed with `*` is a commutative semigroup,
if it admits a surjective map that preserves `*` from a commutative semigroup. -/
@[to_additive
"A type endowed with `+` is an additive commutative semigroup,
if it admits a surjective map that preserves `+` from an additive commutative semigroup."]
protected def comm_semigroup [comm_semigroup M₁] (f : M₁ → M₂) (hf : surjective f)
(mul : ∀ x y, f (x * y) = f x * f y) :
comm_semigroup M₂ :=
{ mul_comm := hf.forall₂.2 $ λ x y, by erw [← mul, ← mul, mul_comm],
.. hf.semigroup f mul }
variables [has_one M₂]
/-- A type endowed with `1` and `*` is a monoid,
if it admits a surjective map that preserves `1` and `*` from a monoid. -/
@[to_additive
"A type endowed with `0` and `+` is an additive monoid,
if it admits a surjective map that preserves `0` and `+` to an additive monoid."]
protected def monoid [monoid M₁] (f : M₁ → M₂) (hf : surjective f)
(one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) :
monoid M₂ :=
{ one_mul := hf.forall.2 $ λ x, by erw [← one, ← mul, one_mul],
mul_one := hf.forall.2 $ λ x, by erw [← one, ← mul, mul_one],
..‹has_one M₂›, .. hf.semigroup f mul }
/-- A type endowed with `1` and `*` is a commutative monoid,
if it admits a surjective map that preserves `1` and `*` from a commutative monoid. -/
@[to_additive
"A type endowed with `0` and `+` is an additive commutative monoid,
if it admits a surjective map that preserves `0` and `+` to an additive commutative monoid."]
protected def comm_monoid [comm_monoid M₁] (f : M₁ → M₂) (hf : surjective f)
(one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) :
comm_monoid M₂ :=
{ .. hf.comm_semigroup f mul, .. hf.monoid f one mul }
variables [has_inv M₂]
/-- A type endowed with `1`, `*` and `⁻¹` is a group,
if it admits a surjective map that preserves `1`, `*` and `⁻¹` from a group. -/
@[to_additive
"A type endowed with `0` and `+` is an additive group,
if it admits a surjective map that preserves `0` and `+` to an additive group."]
protected def group [group M₁] (f : M₁ → M₂) (hf : surjective f)
(one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) :
group M₂ :=
{ mul_left_inv := hf.forall.2 $ λ x, by erw [← inv, ← mul, mul_left_inv, one]; refl,
..‹has_inv M₂›, .. hf.monoid f one mul }
/-- A type endowed with `1`, `*` and `⁻¹` is a commutative group,
if it admits a surjective map that preserves `1`, `*` and `⁻¹` from a commutative group. -/
@[to_additive
"A type endowed with `0` and `+` is an additive commutative group,
if it admits a surjective map that preserves `0` and `+` to an additive commutative group."]
protected def comm_group [comm_group M₁] (f : M₁ → M₂) (hf : surjective f)
(one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f (x⁻¹) = (f x)⁻¹) :
comm_group M₂ :=
{ .. hf.comm_monoid f one mul, .. hf.group f one mul inv }
end surjective
end function
|
82d813803e0648d9cfeb5a0f476f7ca614164d49 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/finset/sort.lean | 2e7d38585c847463baaff8cdd613a29f387d09de | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 9,614 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import order.rel_iso.set
import data.fintype.lattice
import data.multiset.sort
import data.list.nodup_equiv_fin
/-!
# Construct a sorted list from a finset.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
namespace finset
open multiset nat
variables {α β : Type*}
/-! ### sort -/
section sort
variables (r : α → α → Prop) [decidable_rel r]
[is_trans α r] [is_antisymm α r] [is_total α r]
/-- `sort s` constructs a sorted list from the unordered set `s`.
(Uses merge sort algorithm.) -/
def sort (s : finset α) : list α := sort r s.1
@[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) :=
sort_sorted _ _
@[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 :=
sort_eq _ _
@[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup :=
(by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s))
@[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s :=
list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s)
@[simp] theorem mem_sort {s : finset α} {a : α} : a ∈ sort r s ↔ a ∈ s :=
multiset.mem_sort _
@[simp] theorem length_sort {s : finset α} : (sort r s).length = s.card :=
multiset.length_sort _
@[simp] theorem sort_empty : sort r ∅ = [] :=
multiset.sort_zero r
@[simp] theorem sort_singleton (a : α) : sort r {a} = [a] :=
multiset.sort_singleton r a
lemma sort_perm_to_list (s : finset α) : sort r s ~ s.to_list :=
by { rw ←multiset.coe_eq_coe, simp only [coe_to_list, sort_eq] }
end sort
section sort_linear_order
variables [linear_order α]
theorem sort_sorted_lt (s : finset α) : list.sorted (<) (sort (≤) s) :=
(sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _)
lemma sorted_zero_eq_min'_aux (s : finset α) (h : 0 < (s.sort (≤)).length) (H : s.nonempty) :
(s.sort (≤)).nth_le 0 h = s.min' H :=
begin
let l := s.sort (≤),
apply le_antisymm,
{ have : s.min' H ∈ l := (finset.mem_sort (≤)).mpr (s.min'_mem H),
obtain ⟨i, i_lt, hi⟩ : ∃ i (hi : i < l.length), l.nth_le i hi = s.min' H :=
list.mem_iff_nth_le.1 this,
rw ← hi,
exact (s.sort_sorted (≤)).rel_nth_le_of_le _ _ (nat.zero_le i) },
{ have : l.nth_le 0 h ∈ s := (finset.mem_sort (≤)).1 (list.nth_le_mem l 0 h),
exact s.min'_le _ this }
end
lemma sorted_zero_eq_min' {s : finset α} {h : 0 < (s.sort (≤)).length} :
(s.sort (≤)).nth_le 0 h = s.min' (card_pos.1 $ by rwa length_sort at h) :=
sorted_zero_eq_min'_aux _ _ _
lemma min'_eq_sorted_zero {s : finset α} {h : s.nonempty} :
s.min' h = (s.sort (≤)).nth_le 0 (by { rw length_sort, exact card_pos.2 h }) :=
(sorted_zero_eq_min'_aux _ _ _).symm
lemma sorted_last_eq_max'_aux (s : finset α) (h : (s.sort (≤)).length - 1 < (s.sort (≤)).length)
(H : s.nonempty) : (s.sort (≤)).nth_le ((s.sort (≤)).length - 1) h = s.max' H :=
begin
let l := s.sort (≤),
apply le_antisymm,
{ have : l.nth_le ((s.sort (≤)).length - 1) h ∈ s :=
(finset.mem_sort (≤)).1 (list.nth_le_mem l _ h),
exact s.le_max' _ this },
{ have : s.max' H ∈ l := (finset.mem_sort (≤)).mpr (s.max'_mem H),
obtain ⟨i, i_lt, hi⟩ : ∃ i (hi : i < l.length), l.nth_le i hi = s.max' H :=
list.mem_iff_nth_le.1 this,
rw ← hi,
have : i ≤ l.length - 1 := nat.le_pred_of_lt i_lt,
exact (s.sort_sorted (≤)).rel_nth_le_of_le _ _ (nat.le_pred_of_lt i_lt) },
end
lemma sorted_last_eq_max' {s : finset α} {h : (s.sort (≤)).length - 1 < (s.sort (≤)).length} :
(s.sort (≤)).nth_le ((s.sort (≤)).length - 1) h =
s.max' (by { rw length_sort at h, exact card_pos.1 (lt_of_le_of_lt bot_le h) }) :=
sorted_last_eq_max'_aux _ _ _
lemma max'_eq_sorted_last {s : finset α} {h : s.nonempty} :
s.max' h = (s.sort (≤)).nth_le ((s.sort (≤)).length - 1)
(by simpa using nat.sub_lt (card_pos.mpr h) zero_lt_one) :=
(sorted_last_eq_max'_aux _ _ _).symm
/-- Given a finset `s` of cardinality `k` in a linear order `α`, the map `order_iso_of_fin s h`
is the increasing bijection between `fin k` and `s` as an `order_iso`. Here, `h` is a proof that
the cardinality of `s` is `k`. We use this instead of an iso `fin s.card ≃o s` to avoid
casting issues in further uses of this function. -/
def order_iso_of_fin (s : finset α) {k : ℕ} (h : s.card = k) : fin k ≃o s :=
order_iso.trans (fin.cast ((length_sort (≤)).trans h).symm) $
(s.sort_sorted_lt.nth_le_iso _).trans $ order_iso.set_congr _ _ $
set.ext $ λ x, mem_sort _
/-- Given a finset `s` of cardinality `k` in a linear order `α`, the map `order_emb_of_fin s h` is
the increasing bijection between `fin k` and `s` as an order embedding into `α`. Here, `h` is a
proof that the cardinality of `s` is `k`. We use this instead of an embedding `fin s.card ↪o α` to
avoid casting issues in further uses of this function. -/
def order_emb_of_fin (s : finset α) {k : ℕ} (h : s.card = k) : fin k ↪o α :=
(order_iso_of_fin s h).to_order_embedding.trans (order_embedding.subtype _)
@[simp] lemma coe_order_iso_of_fin_apply (s : finset α) {k : ℕ} (h : s.card = k) (i : fin k) :
↑(order_iso_of_fin s h i) = order_emb_of_fin s h i :=
rfl
lemma order_iso_of_fin_symm_apply (s : finset α) {k : ℕ} (h : s.card = k) (x : s) :
↑((s.order_iso_of_fin h).symm x) = (s.sort (≤)).index_of x :=
rfl
lemma order_emb_of_fin_apply (s : finset α) {k : ℕ} (h : s.card = k) (i : fin k) :
s.order_emb_of_fin h i = (s.sort (≤)).nth_le i (by { rw [length_sort, h], exact i.2 }) :=
rfl
@[simp] lemma order_emb_of_fin_mem (s : finset α) {k : ℕ} (h : s.card = k) (i : fin k) :
s.order_emb_of_fin h i ∈ s :=
(s.order_iso_of_fin h i).2
@[simp] lemma range_order_emb_of_fin (s : finset α) {k : ℕ} (h : s.card = k) :
set.range (s.order_emb_of_fin h) = s :=
by simp only [order_emb_of_fin, set.range_comp coe (s.order_iso_of_fin h), rel_embedding.coe_trans,
set.image_univ,
finset.order_emb_of_fin.equations._eqn_1,
rel_iso.range_eq,
order_embedding.subtype_apply,
order_iso.coe_to_order_embedding,
eq_self_iff_true,
subtype.range_coe_subtype,
finset.set_of_mem,
finset.coe_inj]
/-- The bijection `order_emb_of_fin s h` sends `0` to the minimum of `s`. -/
lemma order_emb_of_fin_zero {s : finset α} {k : ℕ} (h : s.card = k) (hz : 0 < k) :
order_emb_of_fin s h ⟨0, hz⟩ = s.min' (card_pos.mp (h.symm ▸ hz)) :=
by simp only [order_emb_of_fin_apply, fin.coe_mk, sorted_zero_eq_min']
/-- The bijection `order_emb_of_fin s h` sends `k-1` to the maximum of `s`. -/
lemma order_emb_of_fin_last {s : finset α} {k : ℕ} (h : s.card = k) (hz : 0 < k) :
order_emb_of_fin s h ⟨k-1, buffer.lt_aux_2 hz⟩ = s.max' (card_pos.mp (h.symm ▸ hz)) :=
by simp [order_emb_of_fin_apply, max'_eq_sorted_last, h]
/-- `order_emb_of_fin {a} h` sends any argument to `a`. -/
@[simp] lemma order_emb_of_fin_singleton (a : α) (i : fin 1) :
order_emb_of_fin {a} (card_singleton a) i = a :=
by rw [subsingleton.elim i ⟨0, zero_lt_one⟩, order_emb_of_fin_zero _ zero_lt_one, min'_singleton]
/-- Any increasing map `f` from `fin k` to a finset of cardinality `k` has to coincide with
the increasing bijection `order_emb_of_fin s h`. -/
lemma order_emb_of_fin_unique {s : finset α} {k : ℕ} (h : s.card = k) {f : fin k → α}
(hfs : ∀ x, f x ∈ s) (hmono : strict_mono f) : f = s.order_emb_of_fin h :=
begin
apply fin.strict_mono_unique hmono (s.order_emb_of_fin h).strict_mono,
rw [range_order_emb_of_fin, ← set.image_univ, ← coe_univ, ← coe_image, coe_inj],
refine eq_of_subset_of_card_le (λ x hx, _) _,
{ rcases mem_image.1 hx with ⟨x, hx, rfl⟩, exact hfs x },
{ rw [h, card_image_of_injective _ hmono.injective, card_univ, fintype.card_fin] }
end
/-- An order embedding `f` from `fin k` to a finset of cardinality `k` has to coincide with
the increasing bijection `order_emb_of_fin s h`. -/
lemma order_emb_of_fin_unique' {s : finset α} {k : ℕ} (h : s.card = k) {f : fin k ↪o α}
(hfs : ∀ x, f x ∈ s) : f = s.order_emb_of_fin h :=
rel_embedding.ext $ function.funext_iff.1 $ order_emb_of_fin_unique h hfs f.strict_mono
/-- Two parametrizations `order_emb_of_fin` of the same set take the same value on `i` and `j` if
and only if `i = j`. Since they can be defined on a priori not defeq types `fin k` and `fin l`
(although necessarily `k = l`), the conclusion is rather written `(i : ℕ) = (j : ℕ)`. -/
@[simp] lemma order_emb_of_fin_eq_order_emb_of_fin_iff
{k l : ℕ} {s : finset α} {i : fin k} {j : fin l} {h : s.card = k} {h' : s.card = l} :
s.order_emb_of_fin h i = s.order_emb_of_fin h' j ↔ (i : ℕ) = (j : ℕ) :=
begin
substs k l,
exact (s.order_emb_of_fin rfl).eq_iff_eq.trans fin.ext_iff
end
/-- Given a finset `s` of size at least `k` in a linear order `α`, the map `order_emb_of_card_le`
is an order embedding from `fin k` to `α` whose image is contained in `s`. Specifically, it maps
`fin k` to an initial segment of `s`. -/
def order_emb_of_card_le (s : finset α) {k : ℕ} (h : k ≤ s.card) : fin k ↪o α :=
(fin.cast_le h).trans (s.order_emb_of_fin rfl)
lemma order_emb_of_card_le_mem (s : finset α) {k : ℕ} (h : k ≤ s.card) (a) :
order_emb_of_card_le s h a ∈ s :=
by simp only [order_emb_of_card_le, rel_embedding.coe_trans, finset.order_emb_of_fin_mem]
end sort_linear_order
meta instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩
end finset
|
f5e2cb86fabe01d11eb8ae7492b1629b61a0a759 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/finmap.lean | 804f9bbb5ce414c216aba3eeda0686d1c006c957 | [
"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,057 | lean | /-
Copyright (c) 2018 Sean Leather. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sean Leather, Mario Carneiro
-/
import data.list.alist
import data.finset.basic
import data.part
/-!
# Finite maps over `multiset`
-/
universes u v w
open list
variables {α : Type u} {β : α → Type v}
/-! ### multisets of sigma types-/
namespace multiset
/-- Multiset of keys of an association multiset. -/
def keys (s : multiset (sigma β)) : multiset α :=
s.map sigma.fst
@[simp] theorem coe_keys {l : list (sigma β)} :
keys (l : multiset (sigma β)) = (l.keys : multiset α) :=
rfl
/-- `nodupkeys s` means that `s` has no duplicate keys. -/
def nodupkeys (s : multiset (sigma β)) : Prop :=
quot.lift_on s list.nodupkeys (λ s t p, propext $ perm_nodupkeys p)
@[simp] theorem coe_nodupkeys {l : list (sigma β)} : @nodupkeys α β l ↔ l.nodupkeys := iff.rfl
end multiset
/-! ### finmap -/
/-- `finmap β` is the type of finite maps over a multiset. It is effectively
a quotient of `alist β` by permutation of the underlying list. -/
structure finmap (β : α → Type v) : Type (max u v) :=
(entries : multiset (sigma β))
(nodupkeys : entries.nodupkeys)
/-- The quotient map from `alist` to `finmap`. -/
def alist.to_finmap (s : alist β) : finmap β := ⟨s.entries, s.nodupkeys⟩
local notation (name := to_finmap) `⟦`:max a `⟧`:0 := alist.to_finmap a
theorem alist.to_finmap_eq {s₁ s₂ : alist β} :
⟦s₁⟧ = ⟦s₂⟧ ↔ s₁.entries ~ s₂.entries :=
by cases s₁; cases s₂; simp [alist.to_finmap]
@[simp] theorem alist.to_finmap_entries (s : alist β) : ⟦s⟧.entries = s.entries := rfl
/-- Given `l : list (sigma β)`, create a term of type `finmap β` by removing
entries with duplicate keys. -/
def list.to_finmap [decidable_eq α] (s : list (sigma β)) : finmap β := s.to_alist.to_finmap
namespace finmap
open alist
/-! ### lifting from alist -/
/-- Lift a permutation-respecting function on `alist` to `finmap`. -/
@[elab_as_eliminator] def lift_on
{γ} (s : finmap β) (f : alist β → γ)
(H : ∀ a b : alist β, a.entries ~ b.entries → f a = f b) : γ :=
begin
refine (quotient.lift_on s.1 (λ l, (⟨_, λ nd, f ⟨l, nd⟩⟩ : part γ))
(λ l₁ l₂ p, part.ext' (perm_nodupkeys p) _) : part γ).get _,
{ exact λ h₁ h₂, H _ _ (by exact p) },
{ have := s.nodupkeys, rcases s.entries with ⟨l⟩, exact id }
end
@[simp] theorem lift_on_to_finmap {γ} (s : alist β) (f : alist β → γ) (H) :
lift_on ⟦s⟧ f H = f s := by cases s; refl
/-- Lift a permutation-respecting function on 2 `alist`s to 2 `finmap`s. -/
@[elab_as_eliminator] def lift_on₂
{γ} (s₁ s₂ : finmap β) (f : alist β → alist β → γ)
(H : ∀ a₁ b₁ a₂ b₂ : alist β, a₁.entries ~ a₂.entries → b₁.entries ~ b₂.entries →
f a₁ b₁ = f a₂ b₂) : γ :=
lift_on s₁
(λ l₁, lift_on s₂ (f l₁) (λ b₁ b₂ p, H _ _ _ _ (perm.refl _) p))
(λ a₁ a₂ p, have H' : f a₁ = f a₂ := funext (λ _, H _ _ _ _ p (perm.refl _)), by simp only [H'])
@[simp] theorem lift_on₂_to_finmap {γ} (s₁ s₂ : alist β) (f : alist β → alist β → γ) (H) :
lift_on₂ ⟦s₁⟧ ⟦s₂⟧ f H = f s₁ s₂ :=
by cases s₁; cases s₂; refl
/-! ### induction -/
@[elab_as_eliminator] theorem induction_on
{C : finmap β → Prop} (s : finmap β) (H : ∀ (a : alist β), C ⟦a⟧) : C s :=
by rcases s with ⟨⟨a⟩, h⟩; exact H ⟨a, h⟩
@[elab_as_eliminator] theorem induction_on₂ {C : finmap β → finmap β → Prop}
(s₁ s₂ : finmap β) (H : ∀ (a₁ a₂ : alist β), C ⟦a₁⟧ ⟦a₂⟧) : C s₁ s₂ :=
induction_on s₁ $ λ l₁, induction_on s₂ $ λ l₂, H l₁ l₂
@[elab_as_eliminator] theorem induction_on₃ {C : finmap β → finmap β → finmap β → Prop}
(s₁ s₂ s₃ : finmap β) (H : ∀ (a₁ a₂ a₃ : alist β), C ⟦a₁⟧ ⟦a₂⟧ ⟦a₃⟧) : C s₁ s₂ s₃ :=
induction_on₂ s₁ s₂ $ λ l₁ l₂, induction_on s₃ $ λ l₃, H l₁ l₂ l₃
/-! ### extensionality -/
@[ext] theorem ext : ∀ {s t : finmap β}, s.entries = t.entries → s = t
| ⟨l₁, h₁⟩ ⟨l₂, h₂⟩ H := by congr'
@[simp] theorem ext_iff {s t : finmap β} : s.entries = t.entries ↔ s = t :=
⟨ext, congr_arg _⟩
/-! ### mem -/
/-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/
instance : has_mem α (finmap β) := ⟨λ a s, a ∈ s.entries.keys⟩
theorem mem_def {a : α} {s : finmap β} :
a ∈ s ↔ a ∈ s.entries.keys := iff.rfl
@[simp] theorem mem_to_finmap {a : α} {s : alist β} :
a ∈ ⟦s⟧ ↔ a ∈ s := iff.rfl
/-! ### keys -/
/-- The set of keys of a finite map. -/
def keys (s : finmap β) : finset α :=
⟨s.entries.keys, induction_on s keys_nodup⟩
@[simp] theorem keys_val (s : alist β) : (keys ⟦s⟧).val = s.keys := rfl
@[simp] theorem keys_ext {s₁ s₂ : alist β} :
keys ⟦s₁⟧ = keys ⟦s₂⟧ ↔ s₁.keys ~ s₂.keys :=
by simp [keys, alist.keys]
theorem mem_keys {a : α} {s : finmap β} : a ∈ s.keys ↔ a ∈ s :=
induction_on s $ λ s, alist.mem_keys
/-! ### empty -/
/-- The empty map. -/
instance : has_emptyc (finmap β) := ⟨⟨0, nodupkeys_nil⟩⟩
instance : inhabited (finmap β) := ⟨∅⟩
@[simp] theorem empty_to_finmap : (⟦∅⟧ : finmap β) = ∅ := rfl
@[simp] theorem to_finmap_nil [decidable_eq α] : ([].to_finmap : finmap β) = ∅ := rfl
theorem not_mem_empty {a : α} : a ∉ (∅ : finmap β) :=
multiset.not_mem_zero a
@[simp] theorem keys_empty : (∅ : finmap β).keys = ∅ := rfl
/-! ### singleton -/
/-- The singleton map. -/
def singleton (a : α) (b : β a) : finmap β := ⟦alist.singleton a b⟧
@[simp] theorem keys_singleton (a : α) (b : β a) :
(singleton a b).keys = {a} := rfl
@[simp] lemma mem_singleton (x y : α) (b : β y) : x ∈ singleton y b ↔ x = y :=
by simp only [singleton]; erw [mem_cons_eq, mem_nil_iff, or_false]
section
variables [decidable_eq α]
instance has_decidable_eq [∀ a, decidable_eq (β a)] : decidable_eq (finmap β)
| s₁ s₂ := decidable_of_iff _ ext_iff
/-! ### lookup -/
/-- Look up the value associated to a key in a map. -/
def lookup (a : α) (s : finmap β) : option (β a) :=
lift_on s (lookup a) (λ s t, perm_lookup)
@[simp] theorem lookup_to_finmap (a : α) (s : alist β) :
lookup a ⟦s⟧ = s.lookup a := rfl
@[simp] theorem lookup_list_to_finmap (a : α) (s : list (sigma β)) :
lookup a s.to_finmap = s.lookup a :=
by rw [list.to_finmap, lookup_to_finmap, lookup_to_alist]
@[simp] theorem lookup_empty (a) : lookup a (∅ : finmap β) = none :=
rfl
theorem lookup_is_some {a : α} {s : finmap β} :
(s.lookup a).is_some ↔ a ∈ s :=
induction_on s $ λ s, alist.lookup_is_some
theorem lookup_eq_none {a} {s : finmap β} : lookup a s = none ↔ a ∉ s :=
induction_on s $ λ s, alist.lookup_eq_none
@[simp] lemma lookup_singleton_eq {a : α} {b : β a} : (singleton a b).lookup a = some b :=
by rw [singleton, lookup_to_finmap, alist.singleton, alist.lookup, lookup_cons_eq]
instance (a : α) (s : finmap β) : decidable (a ∈ s) :=
decidable_of_iff _ lookup_is_some
lemma mem_iff {a : α} {s : finmap β} : a ∈ s ↔ ∃ b, s.lookup a = some b :=
induction_on s $ λ s,
iff.trans list.mem_keys $ exists_congr $ λ b,
(mem_lookup_iff s.nodupkeys).symm
lemma mem_of_lookup_eq_some {a : α} {b : β a} {s : finmap β} (h : s.lookup a = some b) : a ∈ s :=
mem_iff.mpr ⟨_, h⟩
theorem ext_lookup {s₁ s₂ : finmap β} : (∀ x, s₁.lookup x = s₂.lookup x) → s₁ = s₂ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂ h,
begin
simp only [alist.lookup, lookup_to_finmap] at h,
rw [alist.to_finmap_eq],
apply lookup_ext s₁.nodupkeys s₂.nodupkeys,
intros x y,
rw h,
end
/-! ### replace -/
/-- Replace a key with a given value in a finite map.
If the key is not present it does nothing. -/
def replace (a : α) (b : β a) (s : finmap β) : finmap β :=
lift_on s (λ t, ⟦replace a b t⟧) $
λ s₁ s₂ p, to_finmap_eq.2 $ perm_replace p
@[simp] theorem replace_to_finmap (a : α) (b : β a) (s : alist β) :
replace a b ⟦s⟧ = ⟦s.replace a b⟧ := by simp [replace]
@[simp] theorem keys_replace (a : α) (b : β a) (s : finmap β) :
(replace a b s).keys = s.keys :=
induction_on s $ λ s, by simp
@[simp] theorem mem_replace {a a' : α} {b : β a} {s : finmap β} :
a' ∈ replace a b s ↔ a' ∈ s :=
induction_on s $ λ s, by simp
end
/-! ### foldl -/
/-- Fold a commutative function over the key-value pairs in the map -/
def foldl {δ : Type w} (f : δ → Π a, β a → δ)
(H : ∀ d a₁ b₁ a₂ b₂, f (f d a₁ b₁) a₂ b₂ = f (f d a₂ b₂) a₁ b₁)
(d : δ) (m : finmap β) : δ :=
m.entries.foldl (λ d s, f d s.1 s.2) (λ d s t, H _ _ _ _ _) d
/-- `any f s` returns `tt` iff there exists a value `v` in `s` such that `f v = tt`. -/
def any (f : Π x, β x → bool) (s : finmap β) : bool :=
s.foldl (λ x y z, x ∨ f y z) (by { intros, simp [or.right_comm] }) ff
/-- `all f s` returns `tt` iff `f v = tt` for all values `v` in `s`. -/
def all (f : Π x, β x → bool) (s : finmap β) : bool :=
s.foldl (λ x y z, x ∧ f y z) (by { intros, simp [and.right_comm] }) ff
/-! ### erase -/
section
variables [decidable_eq α]
/-- Erase a key from the map. If the key is not present it does nothing. -/
def erase (a : α) (s : finmap β) : finmap β :=
lift_on s (λ t, ⟦erase a t⟧) $
λ s₁ s₂ p, to_finmap_eq.2 $ perm_erase p
@[simp] theorem erase_to_finmap (a : α) (s : alist β) :
erase a ⟦s⟧ = ⟦s.erase a⟧ := by simp [erase]
@[simp] theorem keys_erase_to_finset (a : α) (s : alist β) :
keys ⟦s.erase a⟧ = (keys ⟦s⟧).erase a :=
by simp [finset.erase, keys, alist.erase, keys_kerase]
@[simp] theorem keys_erase (a : α) (s : finmap β) :
(erase a s).keys = s.keys.erase a :=
induction_on s $ λ s, by simp
@[simp] theorem mem_erase {a a' : α} {s : finmap β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s :=
induction_on s $ λ s, by simp
theorem not_mem_erase_self {a : α} {s : finmap β} : ¬ a ∈ erase a s :=
by rw [mem_erase, not_and_distrib, not_not]; left; refl
@[simp] theorem lookup_erase (a) (s : finmap β) : lookup a (erase a s) = none :=
induction_on s $ lookup_erase a
@[simp] theorem lookup_erase_ne {a a'} {s : finmap β} (h : a ≠ a') :
lookup a (erase a' s) = lookup a s :=
induction_on s $ λ s, lookup_erase_ne h
theorem erase_erase {a a' : α} {s : finmap β} : erase a (erase a' s) = erase a' (erase a s) :=
induction_on s $ λ s, ext (by simp only [erase_erase, erase_to_finmap])
/-! ### sdiff -/
/-- `sdiff s s'` consists of all key-value pairs from `s` and `s'` where the keys are in `s` or
`s'` but not both. -/
def sdiff (s s' : finmap β) : finmap β :=
s'.foldl (λ s x _, s.erase x) (λ a₀ a₁ _ a₂ _, erase_erase) s
instance : has_sdiff (finmap β) := ⟨sdiff⟩
/-! ### insert -/
/-- Insert a key-value pair into a finite map, replacing any existing pair with
the same key. -/
def insert (a : α) (b : β a) (s : finmap β) : finmap β :=
lift_on s (λ t, ⟦insert a b t⟧) $
λ s₁ s₂ p, to_finmap_eq.2 $ perm_insert p
@[simp] theorem insert_to_finmap (a : α) (b : β a) (s : alist β) :
insert a b ⟦s⟧ = ⟦s.insert a b⟧ := by simp [insert]
theorem insert_entries_of_neg {a : α} {b : β a} {s : finmap β} : a ∉ s →
(insert a b s).entries = ⟨a, b⟩ ::ₘ s.entries :=
induction_on s $ λ s h,
by simp [insert_entries_of_neg (mt mem_to_finmap.1 h)]
@[simp] theorem mem_insert {a a' : α} {b' : β a'} {s : finmap β} :
a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s :=
induction_on s mem_insert
@[simp] theorem lookup_insert {a} {b : β a} (s : finmap β) :
lookup a (insert a b s) = some b :=
induction_on s $ λ s,
by simp only [insert_to_finmap, lookup_to_finmap, lookup_insert]
@[simp] theorem lookup_insert_of_ne {a a'} {b : β a} (s : finmap β) (h : a' ≠ a) :
lookup a' (insert a b s) = lookup a' s :=
induction_on s $ λ s,
by simp only [insert_to_finmap, lookup_to_finmap, lookup_insert_ne h]
@[simp] theorem insert_insert {a} {b b' : β a} (s : finmap β) :
(s.insert a b).insert a b' = s.insert a b' :=
induction_on s $ λ s,
by simp only [insert_to_finmap, insert_insert]
theorem insert_insert_of_ne {a a'} {b : β a} {b' : β a'} (s : finmap β) (h : a ≠ a') :
(s.insert a b).insert a' b' = (s.insert a' b').insert a b :=
induction_on s $ λ s,
by simp only [insert_to_finmap, alist.to_finmap_eq, insert_insert_of_ne _ h]
theorem to_finmap_cons (a : α) (b : β a) (xs : list (sigma β)) :
list.to_finmap (⟨a,b⟩ :: xs) = insert a b xs.to_finmap := rfl
theorem mem_list_to_finmap (a : α) (xs : list (sigma β)) :
a ∈ xs.to_finmap ↔ (∃ b : β a, sigma.mk a b ∈ xs) :=
by { induction xs with x xs; [skip, cases x];
simp only [to_finmap_cons, *, not_mem_empty, exists_or_distrib, not_mem_nil, to_finmap_nil,
exists_false, mem_cons_iff, mem_insert, exists_and_distrib_left];
apply or_congr _ iff.rfl,
conv { to_lhs, rw ← and_true (a = x_fst) },
apply and_congr_right, rintro ⟨⟩, simp only [exists_eq, heq_iff_eq] }
@[simp] theorem insert_singleton_eq {a : α} {b b' : β a} :
insert a b (singleton a b') = singleton a b :=
by simp only [singleton, finmap.insert_to_finmap, alist.insert_singleton_eq]
/-! ### extract -/
/-- Erase a key from the map, and return the corresponding value, if found. -/
def extract (a : α) (s : finmap β) : option (β a) × finmap β :=
lift_on s (λ t, prod.map id to_finmap (extract a t)) $
λ s₁ s₂ p, by simp [perm_lookup p, to_finmap_eq, perm_erase p]
@[simp] theorem extract_eq_lookup_erase (a : α) (s : finmap β) :
extract a s = (lookup a s, erase a s) :=
induction_on s $ λ s, by simp [extract]
/-! ### union -/
/-- `s₁ ∪ s₂` is the key-based union of two finite maps. It is left-biased: if
there exists an `a ∈ s₁`, `lookup a (s₁ ∪ s₂) = lookup a s₁`. -/
def union (s₁ s₂ : finmap β) : finmap β :=
lift_on₂ s₁ s₂ (λ s₁ s₂, ⟦s₁ ∪ s₂⟧) $
λ s₁ s₂ s₃ s₄ p₁₃ p₂₄, to_finmap_eq.mpr $ perm_union p₁₃ p₂₄
instance : has_union (finmap β) := ⟨union⟩
@[simp] theorem mem_union {a} {s₁ s₂ : finmap β} :
a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ :=
induction_on₂ s₁ s₂ $ λ _ _, mem_union
@[simp] theorem union_to_finmap (s₁ s₂ : alist β) : ⟦s₁⟧ ∪ ⟦s₂⟧ = ⟦s₁ ∪ s₂⟧ :=
by simp [(∪), union]
theorem keys_union {s₁ s₂ : finmap β} : (s₁ ∪ s₂).keys = s₁.keys ∪ s₂.keys :=
induction_on₂ s₁ s₂ $ λ s₁ s₂, finset.ext $ by simp [keys]
@[simp] theorem lookup_union_left {a} {s₁ s₂ : finmap β} :
a ∈ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₁ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂, lookup_union_left
@[simp] theorem lookup_union_right {a} {s₁ s₂ : finmap β} :
a ∉ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₂ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂, lookup_union_right
theorem lookup_union_left_of_not_in {a} {s₁ s₂ : finmap β} (h : a ∉ s₂) :
lookup a (s₁ ∪ s₂) = lookup a s₁ :=
begin
by_cases h' : a ∈ s₁,
{ rw lookup_union_left h' },
{ rw [lookup_union_right h', lookup_eq_none.mpr h, lookup_eq_none.mpr h'] }
end
@[simp] theorem mem_lookup_union {a} {b : β a} {s₁ s₂ : finmap β} :
b ∈ lookup a (s₁ ∪ s₂) ↔ b ∈ lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ lookup a s₂ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂, mem_lookup_union
theorem mem_lookup_union_middle {a} {b : β a} {s₁ s₂ s₃ : finmap β} :
b ∈ lookup a (s₁ ∪ s₃) → a ∉ s₂ → b ∈ lookup a (s₁ ∪ s₂ ∪ s₃) :=
induction_on₃ s₁ s₂ s₃ $ λ s₁ s₂ s₃, mem_lookup_union_middle
theorem insert_union {a} {b : β a} {s₁ s₂ : finmap β} :
insert a b (s₁ ∪ s₂) = insert a b s₁ ∪ s₂ :=
induction_on₂ s₁ s₂ $ λ a₁ a₂, by simp [insert_union]
theorem union_assoc {s₁ s₂ s₃ : finmap β} : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) :=
induction_on₃ s₁ s₂ s₃ $ λ s₁ s₂ s₃,
by simp only [alist.to_finmap_eq, union_to_finmap, alist.union_assoc]
@[simp] theorem empty_union {s₁ : finmap β} : ∅ ∪ s₁ = s₁ :=
induction_on s₁ $ λ s₁, by rw ← empty_to_finmap;
simp [- empty_to_finmap, alist.to_finmap_eq, union_to_finmap, alist.union_assoc]
@[simp] theorem union_empty {s₁ : finmap β} : s₁ ∪ ∅ = s₁ :=
induction_on s₁ $ λ s₁, by rw ← empty_to_finmap;
simp [- empty_to_finmap, alist.to_finmap_eq, union_to_finmap, alist.union_assoc]
theorem erase_union_singleton (a : α) (b : β a) (s : finmap β) (h : s.lookup a = some b) :
s.erase a ∪ singleton a b = s :=
ext_lookup
(λ x, by { by_cases h' : x = a,
{ subst a, rw [lookup_union_right not_mem_erase_self, lookup_singleton_eq, h], },
{ have : x ∉ singleton a b, { rwa mem_singleton },
rw [lookup_union_left_of_not_in this, lookup_erase_ne h'] } } )
end
/-! ### disjoint -/
/-- `disjoint s₁ s₂` holds if `s₁` and `s₂` have no keys in common. -/
def disjoint (s₁ s₂ : finmap β) : Prop :=
∀ x ∈ s₁, ¬ x ∈ s₂
lemma disjoint_empty (x : finmap β) : disjoint ∅ x .
@[symm]
lemma disjoint.symm (x y : finmap β) (h : disjoint x y) : disjoint y x :=
λ p hy hx, h p hx hy
lemma disjoint.symm_iff (x y : finmap β) : disjoint x y ↔ disjoint y x :=
⟨disjoint.symm x y, disjoint.symm y x⟩
section
variables [decidable_eq α]
instance : decidable_rel (@disjoint α β) :=
λ x y, by dsimp only [disjoint]; apply_instance
lemma disjoint_union_left (x y z : finmap β) : disjoint (x ∪ y) z ↔ disjoint x z ∧ disjoint y z :=
by simp [disjoint, finmap.mem_union, or_imp_distrib, forall_and_distrib]
lemma disjoint_union_right (x y z : finmap β) : disjoint x (y ∪ z) ↔ disjoint x y ∧ disjoint x z :=
by rw [disjoint.symm_iff, disjoint_union_left, disjoint.symm_iff _ x, disjoint.symm_iff _ x]
theorem union_comm_of_disjoint {s₁ s₂ : finmap β} : disjoint s₁ s₂ → s₁ ∪ s₂ = s₂ ∪ s₁ :=
induction_on₂ s₁ s₂ $ λ s₁ s₂,
by { intros h, simp only [alist.to_finmap_eq, union_to_finmap, alist.union_comm_of_disjoint h] }
theorem union_cancel {s₁ s₂ s₃ : finmap β} (h : disjoint s₁ s₃) (h' : disjoint s₂ s₃) :
s₁ ∪ s₃ = s₂ ∪ s₃ ↔ s₁ = s₂ :=
⟨λ h'', begin
apply ext_lookup, intro x,
have : (s₁ ∪ s₃).lookup x = (s₂ ∪ s₃).lookup x, from h'' ▸ rfl,
by_cases hs₁ : x ∈ s₁,
{ rwa [lookup_union_left hs₁, lookup_union_left_of_not_in (h _ hs₁)] at this, },
{ by_cases hs₂ : x ∈ s₂,
{ rwa [lookup_union_left_of_not_in (h' _ hs₂), lookup_union_left hs₂] at this, },
{ rw [lookup_eq_none.mpr hs₁, lookup_eq_none.mpr hs₂] } }
end,
λ h, h ▸ rfl⟩
end
end finmap
|
cda049e588f8209d94eab90595257d7f2293f53e | 44023683920a51f2416cf51eeab3fc51c3ff0765 | /leanpkg/leanpkg/main.lean | 8f031c42b562a6c4847c3088d64256c13dc7cf3f | [
"Apache-2.0"
] | permissive | pirocks/lean | e6e3e3fd20c5e7877f7efc3b50e5e20271e8d0cf | 368f17d0b1392a5a72c9eb974f15b14462cc1475 | refs/heads/master | 1,620,671,385,768 | 1,516,152,564,000 | 1,516,152,564,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,784 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Gabriel Ebner
-/
import leanpkg.resolve leanpkg.git
variable [io.interface]
namespace leanpkg
def write_file (fn : string) (cnts : string) (mode := io.mode.write) : io unit := do
h ← io.mk_file_handle fn io.mode.write,
io.fs.write h cnts.to_char_buffer,
io.fs.close h
def read_manifest : io manifest := do
m ← manifest.from_file leanpkg_toml_fn,
when (m.lean_version ≠ lean_version_string) $
io.print_ln $ "\nWARNING: Lean version mismatch: installed version is " ++ lean_version_string
++ ", but package requires " ++ m.lean_version ++ "\n",
return m
def write_manifest (d : manifest) (fn := leanpkg_toml_fn) : io unit :=
write_file fn (repr d)
-- TODO(gabriel): implement a cross-platform api
def get_dot_lean_dir : io string := do
some home ← io.env.get "HOME" | io.fail "environment variable HOME is not set",
return $ home ++ "/.lean"
-- TODO(gabriel): file existence testing
def exists_file (f : string) : io bool := do
ch ← io.proc.spawn { cmd := "test", args := ["-f", f] },
ev ← io.proc.wait ch,
return $ ev = 0
def mk_path_file : ∀ (paths : list string), string
| [] := "builtin_path\n"
| (x :: xs) := mk_path_file xs ++ "path " ++ x ++ "\n"
def configure : io unit := do
d ← read_manifest,
io.put_str_ln $ "configuring " ++ d.name ++ " " ++ d.version,
when (d.path ≠ some "src") $ io.put_str_ln "WARNING: leanpkg configurations not specifying `path = \"src\"` are deprecated.",
assg ← solve_deps d,
path_file_cnts ← mk_path_file <$> construct_path assg,
write_file "leanpkg.path" path_file_cnts
def make (lean_args : list string) : io unit := do
manifest ← read_manifest,
exec_cmd {
cmd := "lean",
args := (match manifest.timeout with some t := ["-T", repr t] | none := [] end) ++
["--make"] ++ manifest.effective_path ++ lean_args,
env := [("LEAN_PATH", none)]
}
def build (lean_args : list string) := configure >> make lean_args
def make_test (lean_args : list string) : io unit :=
exec_cmd { cmd := "lean", args := ["--make", "test"] ++ lean_args, env := [("LEAN_PATH", none)] }
def test (lean_args : list string) := build lean_args >> make_test lean_args
def init_gitignore_contents :=
"*.olean
/_target
/leanpkg.path
"
def init_pkg (n : string) (from_new : bool) : io unit := do
write_manifest { name := n, path := "src", version := "0.1" } leanpkg_toml_fn,
src_ex ← dir_exists "src",
when (¬src_ex) (do
when ¬from_new $ io.put_str_ln "Move existing .lean files into the 'src' folder.",
exec_cmd {cmd := "mkdir", args := ["src"]}),
write_file ".gitignore" init_gitignore_contents io.mode.append,
git_ex ← dir_exists ".git",
when (¬git_ex) (do {
exec_cmd {cmd := "git", args := ["init", "-q"]},
when (upstream_git_branch ≠ "master") $
exec_cmd {cmd := "git", args := ["checkout", "-b", upstream_git_branch]}
} <|> io.print_ln "WARNING: failed to initialize git repository"),
configure
def init (n : string) := init_pkg n false
-- TODO(gabriel): windows
def basename (s : string) : string :=
s.fold "" $ λ s c, if c = '/' then "" else s.str c
def add_dep_to_manifest (dep : dependency) : io unit := do
d ← read_manifest,
let d' := { d with dependencies := d.dependencies.filter (λ old_dep, old_dep.name ≠ dep.name) ++ [dep] },
write_manifest d'
def strip_dot_git (url : string) : string :=
if url.backn 4 = ".git" then url.popn_back 4 else url
def looks_like_git_url (dep : string) : bool :=
':' ∈ dep.to_list
def parse_add_dep (dep : string) : io dependency :=
if looks_like_git_url dep then
pure { name := basename (strip_dot_git dep), src := source.git dep upstream_git_branch }
else do
ex ← dir_exists dep,
if ex then
pure { name := basename dep, src := source.path dep }
else do
[user, repo] ← pure $ dep.split (= '/')
| io.fail sformat!"path '{dep}' does not exist",
pure { name := repo, src := source.git sformat!"https://github.com/{user}/{repo}" upstream_git_branch }
def absolutize_dep (dep : dependency) : io dependency :=
match dep.src with
| source.path p := do
cwd ← io.env.get_cwd,
pure {src := source.path (resolve_dir p cwd), ..dep}
| _ := pure dep
end
def fixup_git_version (dir : string) : ∀ (src : source), io source
| (source.git url _) := source.git url <$> git_head_revision dir
| src := return src
def add (dep : dependency) : io unit := do
(_, assg) ← materialize "." dep assignment.empty,
some downloaded_path ← return (assg.find dep.name),
manif ← manifest.from_file (downloaded_path ++ "/" ++ leanpkg_toml_fn),
src ← fixup_git_version downloaded_path dep.src,
let dep := { dep with name := manif.name, src := src },
add_dep_to_manifest dep,
configure
def new (dir : string) := do
ex ← dir_exists dir,
when ex $ io.fail $ "directory already exists: " ++ dir,
exec_cmd {cmd := "mkdir", args := ["-p", dir]},
change_dir dir,
init_pkg (basename dir) true
def upgrade_dep (assg : assignment) (d : dependency) : io dependency :=
match d.src with
| (source.git url rev) := (do
some path ← return (assg.find d.name) | io.fail "unresolved dependency",
new_rev ← git_latest_origin_revision path,
return {d with src := source.git url new_rev})
<|> return d
| _ := return d
end
def upgrade := do
m ← read_manifest,
assg ← solve_deps m,
ds' ← m.dependencies.mmap (upgrade_dep assg),
write_manifest {m with dependencies := ds'},
configure
def usage :=
"Lean package manager, version " ++ ui_lean_version_string ++ "
Usage: leanpkg <command>
configure download dependencies
build [-- <lean-args>] download dependencies and build *.olean files
test [-- <lean-args>] download dependencies, build *.olean files, and run test files
new <dir> create a Lean package in a new directory
init <name> create a Lean package in the current directory
add <url> add a dependency from a git repository (uses latest upstream revision)
add <dir> add a local dependency
upgrade upgrade all git dependencies to the latest upstream version
install <url> install a user-wide package from git
install <dir> install a user-wide package from a local directory
dump print the parsed leanpkg.toml file (for debugging)
See `leanpkg help <command>` for more information on a specific command."
def main : ∀ (cmd : string) (leanpkg_args lean_args : list string), io unit
| "configure" [] [] := configure
| "build" _ lean_args := build lean_args
| "test" _ lean_args := test lean_args
| "new" [dir] [] := new dir
| "init" [name] [] := init name
| "add" [dep] [] := parse_add_dep dep >>= add
| "upgrade" [] [] := upgrade
| "install" [dep] [] := do
dep ← parse_add_dep dep,
dep ← absolutize_dep dep,
dot_lean_dir ← get_dot_lean_dir,
exec_cmd {cmd := "mkdir", args := ["-p", dot_lean_dir]},
let user_toml_fn := dot_lean_dir ++ "/" ++ leanpkg_toml_fn,
ex ← exists_file user_toml_fn,
when (¬ ex) $ write_manifest {
name := "_user_local_packages",
version := "1"
} user_toml_fn,
change_dir dot_lean_dir,
add dep,
build []
| "dump" [] [] := read_manifest >>= io.print_ln ∘ repr
| "help" ["configure"] [] := io.print_ln "Download dependencies
Usage:
leanpkg configure
This command sets up the `_target/deps` directory and the `leanpkg.path` file.
For each (transitive) git dependency, the specified commit is checked out
into a sub-directory of `_target/deps`. If there are dependencies on multiple
versions of the same package, the version materialized is undefined.
The `leanpkg.path` file used to resolve Lean imports is populated with paths
to the `src` directories of all (transitive) dependencies. No copy is made
of local dependencies."
| "help" ["build"] [] := io.print_ln "Download dependencies and build *.olean files
Usage:
leanpkg build [-- <lean-args>]
This command invokes `leanpkg configure` followed by
`lean --make src <lean-args>`, building the package's Lean files as well as
(transitively) imported files of dependencies. If defined, the `package.timeout`
configuration value is passed to Lean via its `-T` parameter."
| "help" ["test"] [] := io.print_ln "Download dependencies, build *.olean files, and run test files
Usage:
leanpkg test [-- <lean-args>]
This command invokes `leanpkg build <lean-args>` followed by
`lean --make test <lean-args>`, executing the package's test files. A failed
test should generate a Lean error message, which makes this command return a
non-zero exit code."
| "help" ["add"] [] := io.print_ln sformat!"Add a dependency
Usage:
leanpkg add <local-path>
leanpkg add <git-url>
leanpkg add <github-user>/<github-repo>
Examples:
leanpkg add ../mathlib
leanpkg add https://github.com/leanprover/mathlib
leanpkg add leanprover/mathlib
This command adds the specified local or git dependency, then calls
`leanpkg configure`. For git dependencies, the pinned commit is
the head of the branch `lean-<version>` (e.g. `lean-3.3.0`) on stable
releases of Lean, or else `master` (current branch: {upstream_git_branch})."
| "help" ["new"] [] := io.print_ln "Create a new Lean package in a new directory
Usage:
leanpkg new <path>/.../<name>
This command creates a new Lean package named '<name>' in a new directory
`<path>/.../<name>`. A new git repository is initialized to the branch name
expected by `leanpkg add` (see `leanpkg help add`).
For converting an existing directory into a Lean package, use `leanpkg init`."
| "help" ["init"] [] := io.print_ln "Create a new Lean package in the current directory
Usage:
leanpkg init <name>
This command creates a new Lean package with the given name in the current
directory. Existing Lean source files should be moved into the new `src`
directory."
| "help" ["upgrade"] [] := io.print_ln "Upgrade all git dependencies to the latest upstream version
Usage:
leanpkg upgrade
This command fetches the remote repositories of all git dependencies and updates
the pinned commits to the head of the respective branch (see
`leanpkg help add`)."
| "help" ["install"] [] := io.print_ln "Install a user-wide package
Usage:
leanpkg install <local-path>
leanpkg install <git-url>
leanpkg install <github-user>/<github-repo>
This command adds a dependency to a user-wide \"meta\" package in `~/.lean`.
For files not part of a Lean package, Lean falls back to the core library
and this meta package for import resolution.
For removing or upgrading user-wide dependencies, you currently have to change
into `~/.lean` yourself and edit the leanpkg.toml file or execute
`leanpkg upgrade`, respectively."
| "help" _ [] := io.print_ln usage
| _ _ _ := io.fail usage
private def split_cmdline_args_core : list string → list string × list string
| [] := ([], [])
| (arg::args) := if arg = "--"
then ([], args)
else match split_cmdline_args_core args with
| (outer_args, inner_args) := (arg::outer_args, inner_args)
end
def split_cmdline_args : list string → io (string × list string × list string)
| [] := io.fail usage
| [cmd] := return (cmd, [], [])
| (cmd::rest) := match split_cmdline_args_core rest with
| (outer_args, inner_args) := return (cmd, outer_args, inner_args)
end
end leanpkg
def main : io unit :=
do (cmd, outer_args, inner_args) ← io.cmdline_args >>= leanpkg.split_cmdline_args,
leanpkg.main cmd outer_args inner_args
|
2d4775078d52620323257da29f63270efdc22564 | 3aad12fe82645d2d3173fbedc2e5c2ba945a4d75 | /test/data/serial.lean | 8b3718d6080ebe1d3cec77e121e85b4a9c519be5 | [] | no_license | seanpm2001/LeanProver-Community_MathLIB-Nursery | 4f88d539cb18d73a94af983092896b851e6640b5 | 0479b31fa5b4d39f41e89b8584c9f5bf5271e8ec | refs/heads/master | 1,688,730,786,645 | 1,572,070,026,000 | 1,572,070,026,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,859 | lean |
import data.serial
open serial serializer
structure point :=
(x y z : ℕ)
instance : serial point :=
by mk_serializer (point.mk <$> ser_field point.x <*> ser_field point.y <*> ser_field point.z)
example : serial point :=
begin
apply of_serializer (point.mk <$> ser_field point.x <*> ser_field point.y <*> ser_field point.z),
intro w, cases w,
apply there_and_back_again_seq,
apply there_and_back_again_seq,
apply there_and_back_again_map,
{ simp },
{ refl },
{ simp },
{ refl },
{ simp },
end
@[derive serial]
inductive my_sum
| first : my_sum
| second : ℕ → my_sum
| third (n : ℕ) (xs : list ℕ) : n ≤ xs.length → my_sum
@[derive serial]
structure my_struct :=
(x : ℕ)
(xs : list ℕ)
(bounded : xs.length ≤ x)
@[derive [serial, decidable_eq]]
inductive tree' (α : Type)
| leaf {} : tree'
| node2 : α → tree' → tree' → tree'
| node3 : α → tree' → tree' → tree' → tree'
open tree'
meta def tree'.repr {α} [has_repr α] : tree' α → string
| leaf := "leaf"
| (node2 x t₀ t₁) := to_string $ format!"(node2 {repr x} {tree'.repr t₀} {tree'.repr t₁})"
| (node3 x t₀ t₁ t₂) := to_string $ format!"(node3 {repr x} {tree'.repr t₀} {tree'.repr t₁} {tree'.repr t₂})"
meta instance {α} [has_repr α] : has_repr (tree' α) := ⟨ tree'.repr ⟩
def x := node2 2 (node3 77777777777777 leaf leaf (node2 1 leaf leaf)) leaf
#eval serialize x
-- [17, 1, 5, 2, 430029026, 72437, 0, 0, 1, 3, 0, 0, 0]
#eval deserialize (tree' ℕ) [17, 1, 5, 2, 430029026, 72437, 0, 0, 1, 3, 0, 0, 0]
-- (some (node2 2 (node3 77777777777777 leaf leaf (node2 1 leaf leaf)) leaf))
#eval (deserialize _ (serialize x) = some x : bool)
-- tt
open medium
example (x : tree' ℕ) : deserialize _ (serialize x) = some x :=
by { dsimp [serialize,deserialize],
rw [eval_eval,serial.correctness],
refl }
|
19579d34d452a835b726a16d1d4b4d28ec396193 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/linear_algebra/bilinear_form.lean | 2cb320b9440e528441996eb3675b5c3fb09f78b3 | [
"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 | 72,687 | lean | /-
Copyright (c) 2018 Andreas Swerdlow. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andreas Swerdlow, Kexing Ying
-/
import linear_algebra.dual
import linear_algebra.matrix.basis
import linear_algebra.matrix.nondegenerate
import linear_algebra.matrix.nonsingular_inverse
import linear_algebra.matrix.to_linear_equiv
import linear_algebra.tensor_product
/-!
# Bilinear form
This file defines a bilinear form over a module. Basic ideas
such as orthogonality are also introduced, as well as reflexivive,
symmetric, non-degenerate and alternating bilinear forms. Adjoints of
linear maps with respect to a bilinear form are also introduced.
A bilinear form on an R-(semi)module M, is a function from M x M to R,
that is linear in both arguments. Comments will typically abbreviate
"(semi)module" as just "module", but the definitions should be as general as
possible.
The result that there exists an orthogonal basis with respect to a symmetric,
nondegenerate bilinear form can be found in `quadratic_form.lean` with
`exists_orthogonal_basis`.
## Notations
Given any term B of type bilin_form, due to a coercion, can use
the notation B x y to refer to the function field, ie. B x y = B.bilin x y.
In this file we use the following type variables:
- `M`, `M'`, ... are modules over the semiring `R`,
- `M₁`, `M₁'`, ... are modules over the ring `R₁`,
- `M₂`, `M₂'`, ... are modules over the commutative semiring `R₂`,
- `M₃`, `M₃'`, ... are modules over the commutative ring `R₃`,
- `V`, ... is a vector space over the field `K`.
## References
* <https://en.wikipedia.org/wiki/Bilinear_form>
## Tags
Bilinear form,
-/
open_locale big_operators
universes u v w
/-- `bilin_form R M` is the type of `R`-bilinear functions `M → M → R`. -/
structure bilin_form (R : Type*) (M : Type*) [semiring R] [add_comm_monoid M] [module R M] :=
(bilin : M → M → R)
(bilin_add_left : ∀ (x y z : M), bilin (x + y) z = bilin x z + bilin y z)
(bilin_smul_left : ∀ (a : R) (x y : M), bilin (a • x) y = a * (bilin x y))
(bilin_add_right : ∀ (x y z : M), bilin x (y + z) = bilin x y + bilin x z)
(bilin_smul_right : ∀ (a : R) (x y : M), bilin x (a • y) = a * (bilin x y))
variables {R : Type*} {M : Type*} [semiring R] [add_comm_monoid M] [module R M]
variables {R₁ : Type*} {M₁ : Type*} [ring R₁] [add_comm_group M₁] [module R₁ M₁]
variables {R₂ : Type*} {M₂ : Type*} [comm_semiring R₂] [add_comm_monoid M₂] [module R₂ M₂]
variables {R₃ : Type*} {M₃ : Type*} [comm_ring R₃] [add_comm_group M₃] [module R₃ M₃]
variables {V : Type*} {K : Type*} [field K] [add_comm_group V] [module K V]
variables {B : bilin_form R M} {B₁ : bilin_form R₁ M₁} {B₂ : bilin_form R₂ M₂}
namespace bilin_form
instance : has_coe_to_fun (bilin_form R M) (λ _, M → M → R) := ⟨bilin⟩
initialize_simps_projections bilin_form (bilin -> apply)
@[simp] lemma coe_fn_mk (f : M → M → R) (h₁ h₂ h₃ h₄) :
(bilin_form.mk f h₁ h₂ h₃ h₄ : M → M → R) = f :=
rfl
lemma coe_fn_congr : Π {x x' y y' : M}, x = x' → y = y' → B x y = B x' y'
| _ _ _ _ rfl rfl := rfl
@[simp]
lemma add_left (x y z : M) : B (x + y) z = B x z + B y z := bilin_add_left B x y z
@[simp]
lemma smul_left (a : R) (x y : M) : B (a • x) y = a * (B x y) := bilin_smul_left B a x y
@[simp]
lemma add_right (x y z : M) : B x (y + z) = B x y + B x z := bilin_add_right B x y z
@[simp]
lemma smul_right (a : R) (x y : M) : B x (a • y) = a * (B x y) := bilin_smul_right B a x y
@[simp]
lemma zero_left (x : M) : B 0 x = 0 :=
by { rw [←@zero_smul R _ _ _ _ (0 : M), smul_left, zero_mul] }
@[simp]
lemma zero_right (x : M) : B x 0 = 0 :=
by rw [←@zero_smul _ _ _ _ _ (0 : M), smul_right, zero_mul]
@[simp]
lemma neg_left (x y : M₁) : B₁ (-x) y = -(B₁ x y) :=
by rw [←@neg_one_smul R₁ _ _, smul_left, neg_one_mul]
@[simp]
lemma neg_right (x y : M₁) : B₁ x (-y) = -(B₁ x y) :=
by rw [←@neg_one_smul R₁ _ _, smul_right, neg_one_mul]
@[simp]
lemma sub_left (x y z : M₁) : B₁ (x - y) z = B₁ x z - B₁ y z :=
by rw [sub_eq_add_neg, sub_eq_add_neg, add_left, neg_left]
@[simp]
lemma sub_right (x y z : M₁) : B₁ x (y - z) = B₁ x y - B₁ x z :=
by rw [sub_eq_add_neg, sub_eq_add_neg, add_right, neg_right]
variable {D : bilin_form R M}
@[ext] lemma ext (H : ∀ (x y : M), B x y = D x y) : B = D :=
by { cases B, cases D, congr, funext, exact H _ _ }
lemma congr_fun (h : B = D) (x y : M) : B x y = D x y := h ▸ rfl
lemma ext_iff : B = D ↔ (∀ x y, B x y = D x y) := ⟨congr_fun, ext⟩
instance : add_comm_monoid (bilin_form R M) :=
{ add := λ B D, { bilin := λ x y, B x y + D x y,
bilin_add_left := λ x y z, by { rw add_left, rw add_left, ac_refl },
bilin_smul_left := λ a x y, by { rw [smul_left, smul_left, mul_add] },
bilin_add_right := λ x y z, by { rw add_right, rw add_right, ac_refl },
bilin_smul_right := λ a x y, by { rw [smul_right, smul_right, mul_add] } },
add_assoc := by { intros, ext, unfold bilin coe_fn has_coe_to_fun.coe bilin, rw add_assoc },
zero := { bilin := λ x y, 0,
bilin_add_left := λ x y z, (add_zero 0).symm,
bilin_smul_left := λ a x y, (mul_zero a).symm,
bilin_add_right := λ x y z, (zero_add 0).symm,
bilin_smul_right := λ a x y, (mul_zero a).symm },
zero_add := by { intros, ext, unfold coe_fn has_coe_to_fun.coe bilin, rw zero_add },
add_zero := by { intros, ext, unfold coe_fn has_coe_to_fun.coe bilin, rw add_zero },
add_comm := by { intros, ext, unfold coe_fn has_coe_to_fun.coe bilin, rw add_comm } }
instance : add_comm_group (bilin_form R₁ M₁) :=
{ neg := λ B, { bilin := λ x y, - (B.1 x y),
bilin_add_left := λ x y z, by rw [bilin_add_left, neg_add],
bilin_smul_left := λ a x y, by rw [bilin_smul_left, mul_neg_eq_neg_mul_symm],
bilin_add_right := λ x y z, by rw [bilin_add_right, neg_add],
bilin_smul_right := λ a x y, by rw [bilin_smul_right, mul_neg_eq_neg_mul_symm] },
add_left_neg := by { intros, ext, unfold coe_fn has_coe_to_fun.coe bilin, rw neg_add_self },
.. bilin_form.add_comm_monoid }
@[simp]
lemma add_apply (x y : M) : (B + D) x y = B x y + D x y := rfl
@[simp]
lemma zero_apply (x y : M) : (0 : bilin_form R M) x y = 0 := rfl
@[simp]
lemma neg_apply (x y : M₁) : (-B₁) x y = -(B₁ x y) := rfl
instance : inhabited (bilin_form R M) := ⟨0⟩
section
/-- `bilin_form R M` inherits the scalar action from any commutative subalgebra `R₂` of `R`.
When `R` itself is commutative, this provides an `R`-action via `algebra.id`. -/
instance [algebra R₂ R] : module R₂ (bilin_form R M) :=
{ smul := λ c B,
{ bilin := λ x y, c • B x y,
bilin_add_left := λ x y z,
by { unfold coe_fn has_coe_to_fun.coe bilin, rw [bilin_add_left, smul_add] },
bilin_smul_left := λ a x y, by { unfold coe_fn has_coe_to_fun.coe bilin,
rw [bilin_smul_left, ←algebra.mul_smul_comm] },
bilin_add_right := λ x y z, by { unfold coe_fn has_coe_to_fun.coe bilin,
rw [bilin_add_right, smul_add] },
bilin_smul_right := λ a x y, by { unfold coe_fn has_coe_to_fun.coe bilin,
rw [bilin_smul_right, ←algebra.mul_smul_comm] } },
smul_add := λ c B D, by { ext, unfold coe_fn has_coe_to_fun.coe bilin, rw smul_add },
add_smul := λ c B D, by { ext, unfold coe_fn has_coe_to_fun.coe bilin, rw add_smul },
mul_smul := λ a c D, by { ext, unfold coe_fn has_coe_to_fun.coe bilin, rw ←smul_assoc, refl },
one_smul := λ B, by { ext, unfold coe_fn has_coe_to_fun.coe bilin, rw one_smul },
zero_smul := λ B, by { ext, unfold coe_fn has_coe_to_fun.coe bilin, rw zero_smul },
smul_zero := λ B, by { ext, unfold coe_fn has_coe_to_fun.coe bilin, rw smul_zero } }
@[simp] lemma smul_apply [algebra R₂ R] (B : bilin_form R M) (a : R₂) (x y : M) :
(a • B) x y = a • (B x y) :=
rfl
end
section flip
variables (R₂)
/-- Auxiliary construction for the flip of a bilinear form, obtained by exchanging the left and
right arguments. This version is a `linear_map`; it is later upgraded to a `linear_equiv`
in `flip_hom`. -/
def flip_hom_aux [algebra R₂ R] : bilin_form R M →ₗ[R₂] bilin_form R M :=
{ to_fun := λ A,
{ bilin := λ i j, A j i,
bilin_add_left := λ x y z, A.bilin_add_right z x y,
bilin_smul_left := λ a x y, A.bilin_smul_right a y x,
bilin_add_right := λ x y z, A.bilin_add_left y z x,
bilin_smul_right := λ a x y, A.bilin_smul_left a y x },
map_add' := λ A₁ A₂, by { ext, simp } ,
map_smul' := λ c A, by { ext, simp } }
variables {R₂}
lemma flip_flip_aux [algebra R₂ R] (A : bilin_form R M) :
(flip_hom_aux R₂) (flip_hom_aux R₂ A) = A :=
by { ext A x y, simp [flip_hom_aux] }
variables (R₂)
/-- The flip of a bilinear form, obtained by exchanging the left and right arguments. This is a
less structured version of the equiv which applies to general (noncommutative) rings `R` with a
distinguished commutative subring `R₂`; over a commutative ring use `flip`. -/
def flip_hom [algebra R₂ R] : bilin_form R M ≃ₗ[R₂] bilin_form R M :=
{ inv_fun := flip_hom_aux R₂,
left_inv := flip_flip_aux,
right_inv := flip_flip_aux,
.. flip_hom_aux R₂ }
variables {R₂}
@[simp] lemma flip_apply [algebra R₂ R] (A : bilin_form R M) (x y : M) :
flip_hom R₂ A x y = A y x :=
rfl
lemma flip_flip [algebra R₂ R] :
(flip_hom R₂).trans (flip_hom R₂) = linear_equiv.refl R₂ (bilin_form R M) :=
by { ext A x y, simp }
/-- The flip of a bilinear form over a ring, obtained by exchanging the left and right arguments,
here considered as an `ℕ`-linear equivalence, i.e. an additive equivalence. -/
abbreviation flip' : bilin_form R M ≃ₗ[ℕ] bilin_form R M := flip_hom ℕ
/-- The `flip` of a bilinear form over a commutative ring, obtained by exchanging the left and
right arguments. -/
abbreviation flip : bilin_form R₂ M₂ ≃ₗ[R₂] bilin_form R₂ M₂ := flip_hom R₂
end flip
section to_lin'
variables [algebra R₂ R] [module R₂ M] [is_scalar_tower R₂ R M]
/-- Auxiliary definition to define `to_lin_hom`; see below. -/
def to_lin_hom_aux₁ (A : bilin_form R M) (x : M) : M →ₗ[R] R :=
{ to_fun := λ y, A x y,
map_add' := A.bilin_add_right x,
map_smul' := λ c, A.bilin_smul_right c x }
/-- Auxiliary definition to define `to_lin_hom`; see below. -/
def to_lin_hom_aux₂ (A : bilin_form R M) : M →ₗ[R₂] M →ₗ[R] R :=
{ to_fun := to_lin_hom_aux₁ A,
map_add' := λ x₁ x₂, linear_map.ext $ λ x, by simp only [to_lin_hom_aux₁, linear_map.coe_mk,
linear_map.add_apply, add_left],
map_smul' := λ c x, linear_map.ext $
begin
dsimp [to_lin_hom_aux₁],
intros,
simp only [← algebra_map_smul R c x, algebra.smul_def, linear_map.coe_mk,
linear_map.smul_apply, smul_left]
end }
variables (R₂)
/-- The linear map obtained from a `bilin_form` by fixing the left co-ordinate and evaluating in
the right.
This is the most general version of the construction; it is `R₂`-linear for some distinguished
commutative subsemiring `R₂` of the scalar ring. Over a semiring with no particular distinguished
such subsemiring, use `to_lin'`, which is `ℕ`-linear. Over a commutative semiring, use `to_lin`,
which is linear. -/
def to_lin_hom : bilin_form R M →ₗ[R₂] M →ₗ[R₂] M →ₗ[R] R :=
{ to_fun := to_lin_hom_aux₂,
map_add' := λ A₁ A₂, linear_map.ext $ λ x,
begin
dsimp only [to_lin_hom_aux₁, to_lin_hom_aux₂],
apply linear_map.ext,
intros y,
simp only [to_lin_hom_aux₂, to_lin_hom_aux₁, linear_map.coe_mk,
linear_map.add_apply, add_apply],
end ,
map_smul' := λ c A,
begin
dsimp [to_lin_hom_aux₁, to_lin_hom_aux₂],
apply linear_map.ext,
intros x,
apply linear_map.ext,
intros y,
simp only [to_lin_hom_aux₂, to_lin_hom_aux₁,
linear_map.coe_mk, linear_map.smul_apply, smul_apply],
end }
variables {R₂}
@[simp] lemma to_lin'_apply (A : bilin_form R M) (x : M) :
⇑(to_lin_hom R₂ A x) = A x :=
rfl
/-- The linear map obtained from a `bilin_form` by fixing the left co-ordinate and evaluating in
the right.
Over a commutative semiring, use `to_lin`, which is linear rather than `ℕ`-linear. -/
abbreviation to_lin' : bilin_form R M →ₗ[ℕ] M →ₗ[ℕ] M →ₗ[R] R := to_lin_hom ℕ
@[simp]
lemma sum_left {α} (t : finset α) (g : α → M) (w : M) :
B (∑ i in t, g i) w = ∑ i in t, B (g i) w :=
(bilin_form.to_lin' B).map_sum₂ t g w
@[simp]
lemma sum_right {α} (t : finset α) (w : M) (g : α → M) :
B w (∑ i in t, g i) = ∑ i in t, B w (g i) :=
(bilin_form.to_lin' B w).map_sum
variables (R₂)
/-- The linear map obtained from a `bilin_form` by fixing the right co-ordinate and evaluating in
the left.
This is the most general version of the construction; it is `R₂`-linear for some distinguished
commutative subsemiring `R₂` of the scalar ring. Over semiring with no particular distinguished
such subsemiring, use `to_lin'_flip`, which is `ℕ`-linear. Over a commutative semiring, use
`to_lin_flip`, which is linear. -/
def to_lin_hom_flip : bilin_form R M →ₗ[R₂] M →ₗ[R₂] M →ₗ[R] R :=
(to_lin_hom R₂).comp (flip_hom R₂).to_linear_map
variables {R₂}
@[simp] lemma to_lin'_flip_apply (A : bilin_form R M) (x : M) :
⇑(to_lin_hom_flip R₂ A x) = λ y, A y x :=
rfl
/-- The linear map obtained from a `bilin_form` by fixing the right co-ordinate and evaluating in
the left.
Over a commutative semiring, use `to_lin_flip`, which is linear rather than `ℕ`-linear. -/
abbreviation to_lin'_flip : bilin_form R M →ₗ[ℕ] M →ₗ[ℕ] M →ₗ[R] R := to_lin_hom_flip ℕ
end to_lin'
end bilin_form
section equiv_lin
/-- A map with two arguments that is linear in both is a bilinear form.
This is an auxiliary definition for the full linear equivalence `linear_map.to_bilin`.
-/
def linear_map.to_bilin_aux (f : M₂ →ₗ[R₂] M₂ →ₗ[R₂] R₂) : bilin_form R₂ M₂ :=
{ bilin := λ x y, f x y,
bilin_add_left := λ x y z, (linear_map.map_add f x y).symm ▸ linear_map.add_apply (f x) (f y) z,
bilin_smul_left := λ a x y, by rw [linear_map.map_smul, linear_map.smul_apply, smul_eq_mul],
bilin_add_right := λ x y z, linear_map.map_add (f x) y z,
bilin_smul_right := λ a x y, linear_map.map_smul (f x) a y }
/-- Bilinear forms are linearly equivalent to maps with two arguments that are linear in both. -/
def bilin_form.to_lin : bilin_form R₂ M₂ ≃ₗ[R₂] (M₂ →ₗ[R₂] M₂ →ₗ[R₂] R₂) :=
{ inv_fun := linear_map.to_bilin_aux,
left_inv := λ B, by { ext, simp [linear_map.to_bilin_aux] },
right_inv := λ B, by { ext, simp [linear_map.to_bilin_aux] },
.. bilin_form.to_lin_hom R₂ }
/-- A map with two arguments that is linear in both is linearly equivalent to bilinear form. -/
def linear_map.to_bilin : (M₂ →ₗ[R₂] M₂ →ₗ[R₂] R₂) ≃ₗ[R₂] bilin_form R₂ M₂ :=
bilin_form.to_lin.symm
@[simp] lemma linear_map.to_bilin_aux_eq (f : M₂ →ₗ[R₂] M₂ →ₗ[R₂] R₂) :
linear_map.to_bilin_aux f = linear_map.to_bilin f :=
rfl
@[simp] lemma linear_map.to_bilin_symm :
(linear_map.to_bilin.symm : bilin_form R₂ M₂ ≃ₗ[R₂] _) = bilin_form.to_lin := rfl
@[simp] lemma bilin_form.to_lin_symm :
(bilin_form.to_lin.symm : _ ≃ₗ[R₂] bilin_form R₂ M₂) = linear_map.to_bilin :=
linear_map.to_bilin.symm_symm
@[simp, norm_cast]
lemma bilin_form.to_lin_apply (x : M₂) : ⇑(bilin_form.to_lin B₂ x) = B₂ x := rfl
end equiv_lin
namespace bilin_form
section comp
variables {M' : Type w} [add_comm_monoid M'] [module R M']
/-- Apply a linear map on the left and right argument of a bilinear form. -/
def comp (B : bilin_form R M') (l r : M →ₗ[R] M') : bilin_form R M :=
{ bilin := λ x y, B (l x) (r y),
bilin_add_left := λ x y z, by rw [linear_map.map_add, add_left],
bilin_smul_left := λ x y z, by rw [linear_map.map_smul, smul_left],
bilin_add_right := λ x y z, by rw [linear_map.map_add, add_right],
bilin_smul_right := λ x y z, by rw [linear_map.map_smul, smul_right] }
/-- Apply a linear map to the left argument of a bilinear form. -/
def comp_left (B : bilin_form R M) (f : M →ₗ[R] M) : bilin_form R M :=
B.comp f linear_map.id
/-- Apply a linear map to the right argument of a bilinear form. -/
def comp_right (B : bilin_form R M) (f : M →ₗ[R] M) : bilin_form R M :=
B.comp linear_map.id f
lemma comp_comp {M'' : Type*} [add_comm_monoid M''] [module R M'']
(B : bilin_form R M'') (l r : M →ₗ[R] M') (l' r' : M' →ₗ[R] M'') :
(B.comp l' r').comp l r = B.comp (l'.comp l) (r'.comp r) := rfl
@[simp] lemma comp_left_comp_right (B : bilin_form R M) (l r : M →ₗ[R] M) :
(B.comp_left l).comp_right r = B.comp l r := rfl
@[simp] lemma comp_right_comp_left (B : bilin_form R M) (l r : M →ₗ[R] M) :
(B.comp_right r).comp_left l = B.comp l r := rfl
@[simp] lemma comp_apply (B : bilin_form R M') (l r : M →ₗ[R] M') (v w) :
B.comp l r v w = B (l v) (r w) := rfl
@[simp] lemma comp_left_apply (B : bilin_form R M) (f : M →ₗ[R] M) (v w) :
B.comp_left f v w = B (f v) w := rfl
@[simp] lemma comp_right_apply (B : bilin_form R M) (f : M →ₗ[R] M) (v w) :
B.comp_right f v w = B v (f w) := rfl
@[simp] lemma comp_id_left (B : bilin_form R M) (r : M →ₗ[R] M) :
B.comp linear_map.id r = B.comp_right r :=
by { ext, refl }
@[simp] lemma comp_id_right (B : bilin_form R M) (l : M →ₗ[R] M) :
B.comp l linear_map.id = B.comp_left l :=
by { ext, refl }
@[simp] lemma comp_left_id (B : bilin_form R M) :
B.comp_left linear_map.id = B :=
by { ext, refl }
@[simp] lemma comp_right_id (B : bilin_form R M) :
B.comp_right linear_map.id = B :=
by { ext, refl }
-- Shortcut for `comp_id_{left,right}` followed by `comp_{right,left}_id`,
-- has to be declared after the former two to get the right priority
@[simp] lemma comp_id_id (B : bilin_form R M) :
B.comp linear_map.id linear_map.id = B :=
by { ext, refl }
lemma comp_injective (B₁ B₂ : bilin_form R M') {l r : M →ₗ[R] M'}
(hₗ : function.surjective l) (hᵣ : function.surjective r) :
B₁.comp l r = B₂.comp l r ↔ B₁ = B₂ :=
begin
split; intros h,
{ -- B₁.comp l r = B₂.comp l r → B₁ = B₂
ext,
cases hₗ x with x' hx, subst hx,
cases hᵣ y with y' hy, subst hy,
rw [←comp_apply, ←comp_apply, h], },
{ -- B₁ = B₂ → B₁.comp l r = B₂.comp l r
subst h, },
end
end comp
variables {M₂' M₂'' : Type*}
variables [add_comm_monoid M₂'] [add_comm_monoid M₂''] [module R₂ M₂'] [module R₂ M₂'']
section congr
/-- Apply a linear equivalence on the arguments of a bilinear form. -/
def congr (e : M₂ ≃ₗ[R₂] M₂') : bilin_form R₂ M₂ ≃ₗ[R₂] bilin_form R₂ M₂' :=
{ to_fun := λ B, B.comp e.symm e.symm,
inv_fun := λ B, B.comp e e,
left_inv :=
λ B, ext (λ x y, by simp only [comp_apply, linear_equiv.coe_coe, e.symm_apply_apply]),
right_inv :=
λ B, ext (λ x y, by simp only [comp_apply, linear_equiv.coe_coe, e.apply_symm_apply]),
map_add' := λ B B', ext (λ x y, by simp only [comp_apply, add_apply]),
map_smul' := λ B B', ext (λ x y, by simp [comp_apply, smul_apply]) }
@[simp] lemma congr_apply (e : M₂ ≃ₗ[R₂] M₂') (B : bilin_form R₂ M₂) (x y : M₂') :
congr e B x y = B (e.symm x) (e.symm y) := rfl
@[simp] lemma congr_symm (e : M₂ ≃ₗ[R₂] M₂') :
(congr e).symm = congr e.symm :=
by { ext B x y, simp only [congr_apply, linear_equiv.symm_symm], refl }
@[simp] lemma congr_refl : congr (linear_equiv.refl R₂ M₂) = linear_equiv.refl R₂ _ :=
linear_equiv.ext $ λ B, ext $ λ x y, rfl
lemma congr_trans (e : M₂ ≃ₗ[R₂] M₂') (f : M₂' ≃ₗ[R₂] M₂'') :
(congr e).trans (congr f) = congr (e.trans f) := rfl
lemma congr_congr (e : M₂' ≃ₗ[R₂] M₂'') (f : M₂ ≃ₗ[R₂] M₂') (B : bilin_form R₂ M₂) :
congr e (congr f B) = congr (f.trans e) B := rfl
lemma congr_comp (e : M₂ ≃ₗ[R₂] M₂') (B : bilin_form R₂ M₂) (l r : M₂'' →ₗ[R₂] M₂') :
(congr e B).comp l r = B.comp
(linear_map.comp (e.symm : M₂' →ₗ[R₂] M₂) l)
(linear_map.comp (e.symm : M₂' →ₗ[R₂] M₂) r) :=
rfl
lemma comp_congr (e : M₂' ≃ₗ[R₂] M₂'') (B : bilin_form R₂ M₂) (l r : M₂' →ₗ[R₂] M₂) :
congr e (B.comp l r) = B.comp
(l.comp (e.symm : M₂'' →ₗ[R₂] M₂'))
(r.comp (e.symm : M₂'' →ₗ[R₂] M₂')) :=
rfl
end congr
section lin_mul_lin
/-- `lin_mul_lin f g` is the bilinear form mapping `x` and `y` to `f x * g y` -/
def lin_mul_lin (f g : M₂ →ₗ[R₂] R₂) : bilin_form R₂ M₂ :=
{ bilin := λ x y, f x * g y,
bilin_add_left := λ x y z, by rw [linear_map.map_add, add_mul],
bilin_smul_left := λ x y z, by rw [linear_map.map_smul, smul_eq_mul, mul_assoc],
bilin_add_right := λ x y z, by rw [linear_map.map_add, mul_add],
bilin_smul_right := λ x y z, by rw [linear_map.map_smul, smul_eq_mul, mul_left_comm] }
variables {f g : M₂ →ₗ[R₂] R₂}
@[simp] lemma lin_mul_lin_apply (x y) : lin_mul_lin f g x y = f x * g y := rfl
@[simp] lemma lin_mul_lin_comp (l r : M₂' →ₗ[R₂] M₂) :
(lin_mul_lin f g).comp l r = lin_mul_lin (f.comp l) (g.comp r) :=
rfl
@[simp] lemma lin_mul_lin_comp_left (l : M₂ →ₗ[R₂] M₂) :
(lin_mul_lin f g).comp_left l = lin_mul_lin (f.comp l) g :=
rfl
@[simp] lemma lin_mul_lin_comp_right (r : M₂ →ₗ[R₂] M₂) :
(lin_mul_lin f g).comp_right r = lin_mul_lin f (g.comp r) :=
rfl
end lin_mul_lin
/-- The proposition that two elements of a bilinear form space are orthogonal. For orthogonality
of an indexed set of elements, use `bilin_form.is_Ortho`. -/
def is_ortho (B : bilin_form R M) (x y : M) : Prop :=
B x y = 0
lemma is_ortho_def {B : bilin_form R M} {x y : M} :
B.is_ortho x y ↔ B x y = 0 := iff.rfl
lemma is_ortho_zero_left (x : M) : is_ortho B (0 : M) x :=
zero_left x
lemma is_ortho_zero_right (x : M) : is_ortho B x (0 : M) :=
zero_right x
lemma ne_zero_of_not_is_ortho_self {B : bilin_form K V}
(x : V) (hx₁ : ¬ B.is_ortho x x) : x ≠ 0 :=
λ hx₂, hx₁ (hx₂.symm ▸ is_ortho_zero_left _)
/-- A set of vectors `v` is orthogonal with respect to some bilinear form `B` if and only
if for all `i ≠ j`, `B (v i) (v j) = 0`. For orthogonality between two elements, use
`bilin_form.is_ortho` -/
def is_Ortho {n : Type w} (B : bilin_form R M) (v : n → M) : Prop :=
pairwise (B.is_ortho on v)
lemma is_Ortho_def {n : Type w} {B : bilin_form R M} {v : n → M} :
B.is_Ortho v ↔ ∀ i j : n, i ≠ j → B (v i) (v j) = 0 := iff.rfl
section
variables {R₄ M₄ : Type*} [ring R₄] [is_domain R₄]
variables [add_comm_group M₄] [module R₄ M₄] {G : bilin_form R₄ M₄}
@[simp]
theorem is_ortho_smul_left {x y : M₄} {a : R₄} (ha : a ≠ 0) :
is_ortho G (a • x) y ↔ is_ortho G x y :=
begin
dunfold is_ortho,
split; intro H,
{ rw [smul_left, mul_eq_zero] at H,
cases H,
{ trivial },
{ exact H }},
{ rw [smul_left, H, mul_zero] },
end
@[simp]
theorem is_ortho_smul_right {x y : M₄} {a : R₄} (ha : a ≠ 0) :
is_ortho G x (a • y) ↔ is_ortho G x y :=
begin
dunfold is_ortho,
split; intro H,
{ rw [smul_right, mul_eq_zero] at H,
cases H,
{ trivial },
{ exact H }},
{ rw [smul_right, H, mul_zero] },
end
/-- A set of orthogonal vectors `v` with respect to some bilinear form `B` is linearly independent
if for all `i`, `B (v i) (v i) ≠ 0`. -/
lemma linear_independent_of_is_Ortho
{n : Type w} {B : bilin_form K V} {v : n → V}
(hv₁ : B.is_Ortho v) (hv₂ : ∀ i, ¬ B.is_ortho (v i) (v i)) :
linear_independent K v :=
begin
classical,
rw linear_independent_iff',
intros s w hs i hi,
have : B (s.sum $ λ (i : n), w i • v i) (v i) = 0,
{ rw [hs, zero_left] },
have hsum : s.sum (λ (j : n), w j * B (v j) (v i)) = w i * B (v i) (v i),
{ apply finset.sum_eq_single_of_mem i hi,
intros j hj hij,
rw [is_Ortho_def.1 hv₁ _ _ hij, mul_zero], },
simp_rw [sum_left, smul_left, hsum] at this,
exact eq_zero_of_ne_zero_of_mul_right_eq_zero (hv₂ i) this,
end
end
section basis
variables {B₃ F₃ : bilin_form R₃ M₃}
variables {ι : Type*} (b : basis ι R₃ M₃)
/-- Two bilinear forms are equal when they are equal on all basis vectors. -/
lemma ext_basis (h : ∀ i j, B₃ (b i) (b j) = F₃ (b i) (b j)) : B₃ = F₃ :=
to_lin.injective $ b.ext $ λ i, b.ext $ λ j, h i j
/-- Write out `B x y` as a sum over `B (b i) (b j)` if `b` is a basis. -/
lemma sum_repr_mul_repr_mul (x y : M₃) :
(b.repr x).sum (λ i xi, (b.repr y).sum (λ j yj, xi • yj • B₃ (b i) (b j))) = B₃ x y :=
begin
conv_rhs { rw [← b.total_repr x, ← b.total_repr y] },
simp_rw [finsupp.total_apply, finsupp.sum, sum_left, sum_right,
smul_left, smul_right, smul_eq_mul]
end
end basis
end bilin_form
section matrix
variables {n o : Type*}
open bilin_form finset linear_map matrix
open_locale matrix
/-- The map from `matrix n n R` to bilinear forms on `n → R`.
This is an auxiliary definition for the equivalence `matrix.to_bilin_form'`. -/
def matrix.to_bilin'_aux [fintype n] (M : matrix n n R₂) : bilin_form R₂ (n → R₂) :=
{ bilin := λ v w, ∑ i j, v i * M i j * w j,
bilin_add_left := λ x y z, by simp only [pi.add_apply, add_mul, sum_add_distrib],
bilin_smul_left := λ a x y, by simp only [pi.smul_apply, smul_eq_mul, mul_assoc, mul_sum],
bilin_add_right := λ x y z, by simp only [pi.add_apply, mul_add, sum_add_distrib],
bilin_smul_right := λ a x y,
by simp only [pi.smul_apply, smul_eq_mul, mul_assoc, mul_left_comm, mul_sum] }
lemma matrix.to_bilin'_aux_std_basis [fintype n] [decidable_eq n] (M : matrix n n R₂)
(i j : n) : M.to_bilin'_aux (std_basis R₂ (λ _, R₂) i 1) (std_basis R₂ (λ _, R₂) j 1) = M i j :=
begin
rw [matrix.to_bilin'_aux, coe_fn_mk, sum_eq_single i, sum_eq_single j],
{ simp only [std_basis_same, std_basis_same, one_mul, mul_one] },
{ rintros j' - hj',
apply mul_eq_zero_of_right,
exact std_basis_ne R₂ (λ _, R₂) _ _ hj' 1 },
{ intros,
have := finset.mem_univ j,
contradiction },
{ rintros i' - hi',
refine finset.sum_eq_zero (λ j _, _),
apply mul_eq_zero_of_left,
apply mul_eq_zero_of_left,
exact std_basis_ne R₂ (λ _, R₂) _ _ hi' 1 },
{ intros,
have := finset.mem_univ i,
contradiction }
end
/-- The linear map from bilinear forms to `matrix n n R` given an `n`-indexed basis.
This is an auxiliary definition for the equivalence `matrix.to_bilin_form'`. -/
def bilin_form.to_matrix_aux (b : n → M₂) : bilin_form R₂ M₂ →ₗ[R₂] matrix n n R₂ :=
{ to_fun := λ B i j, B (b i) (b j),
map_add' := λ f g, rfl,
map_smul' := λ f g, rfl }
variables [fintype n] [fintype o]
lemma to_bilin'_aux_to_matrix_aux [decidable_eq n] (B₃ : bilin_form R₃ (n → R₃)) :
matrix.to_bilin'_aux (bilin_form.to_matrix_aux (λ j, std_basis R₃ (λ _, R₃) j 1) B₃) = B₃ :=
begin
refine ext_basis (pi.basis_fun R₃ n) (λ i j, _),
rw [bilin_form.to_matrix_aux, linear_map.coe_mk, pi.basis_fun_apply, pi.basis_fun_apply,
matrix.to_bilin'_aux_std_basis]
end
section to_matrix'
/-! ### `to_matrix'` section
This section deals with the conversion between matrices and bilinear forms on `n → R₃`.
-/
variables [decidable_eq n] [decidable_eq o]
/-- The linear equivalence between bilinear forms on `n → R` and `n × n` matrices -/
def bilin_form.to_matrix' : bilin_form R₃ (n → R₃) ≃ₗ[R₃] matrix n n R₃ :=
{ inv_fun := matrix.to_bilin'_aux,
left_inv := by convert to_bilin'_aux_to_matrix_aux,
right_inv := λ M,
by { ext i j, simp only [bilin_form.to_matrix_aux, matrix.to_bilin'_aux_std_basis] },
..bilin_form.to_matrix_aux (λ j, std_basis R₃ (λ _, R₃) j 1) }
@[simp] lemma bilin_form.to_matrix_aux_std_basis (B : bilin_form R₃ (n → R₃)) :
bilin_form.to_matrix_aux (λ j, std_basis R₃ (λ _, R₃) j 1) B =
bilin_form.to_matrix' B :=
rfl
/-- The linear equivalence between `n × n` matrices and bilinear forms on `n → R` -/
def matrix.to_bilin' : matrix n n R₃ ≃ₗ[R₃] bilin_form R₃ (n → R₃) :=
bilin_form.to_matrix'.symm
@[simp] lemma matrix.to_bilin'_aux_eq (M : matrix n n R₃) :
matrix.to_bilin'_aux M = matrix.to_bilin' M :=
rfl
lemma matrix.to_bilin'_apply (M : matrix n n R₃) (x y : n → R₃) :
matrix.to_bilin' M x y = ∑ i j, x i * M i j * y j := rfl
lemma matrix.to_bilin'_apply' (M : matrix n n R₃) (v w : n → R₃) :
matrix.to_bilin' M v w = matrix.dot_product v (M.mul_vec w) :=
begin
simp_rw [matrix.to_bilin'_apply, matrix.dot_product,
matrix.mul_vec, matrix.dot_product],
refine finset.sum_congr rfl (λ _ _, _),
rw finset.mul_sum,
refine finset.sum_congr rfl (λ _ _, _),
rw ← mul_assoc,
end
@[simp] lemma matrix.to_bilin'_std_basis (M : matrix n n R₃) (i j : n) :
matrix.to_bilin' M (std_basis R₃ (λ _, R₃) i 1) (std_basis R₃ (λ _, R₃) j 1) =
M i j :=
matrix.to_bilin'_aux_std_basis M i j
@[simp] lemma bilin_form.to_matrix'_symm :
(bilin_form.to_matrix'.symm : matrix n n R₃ ≃ₗ[R₃] _) = matrix.to_bilin' :=
rfl
@[simp] lemma matrix.to_bilin'_symm :
(matrix.to_bilin'.symm : _ ≃ₗ[R₃] matrix n n R₃) = bilin_form.to_matrix' :=
bilin_form.to_matrix'.symm_symm
@[simp] lemma matrix.to_bilin'_to_matrix' (B : bilin_form R₃ (n → R₃)) :
matrix.to_bilin' (bilin_form.to_matrix' B) = B :=
matrix.to_bilin'.apply_symm_apply B
@[simp] lemma bilin_form.to_matrix'_to_bilin' (M : matrix n n R₃) :
bilin_form.to_matrix' (matrix.to_bilin' M) = M :=
bilin_form.to_matrix'.apply_symm_apply M
@[simp] lemma bilin_form.to_matrix'_apply (B : bilin_form R₃ (n → R₃)) (i j : n) :
bilin_form.to_matrix' B i j =
B (std_basis R₃ (λ _, R₃) i 1) (std_basis R₃ (λ _, R₃) j 1) :=
rfl
@[simp] lemma bilin_form.to_matrix'_comp (B : bilin_form R₃ (n → R₃))
(l r : (o → R₃) →ₗ[R₃] (n → R₃)) :
(B.comp l r).to_matrix' = l.to_matrix'ᵀ ⬝ B.to_matrix' ⬝ r.to_matrix' :=
begin
ext i j,
simp only [bilin_form.to_matrix'_apply, bilin_form.comp_apply, transpose_apply, matrix.mul_apply,
linear_map.to_matrix', linear_equiv.coe_mk, sum_mul],
rw sum_comm,
conv_lhs { rw ← sum_repr_mul_repr_mul (pi.basis_fun R₃ n) (l _) (r _) },
rw finsupp.sum_fintype,
{ apply sum_congr rfl,
rintros i' -,
rw finsupp.sum_fintype,
{ apply sum_congr rfl,
rintros j' -,
simp only [smul_eq_mul, pi.basis_fun_repr, mul_assoc, mul_comm, mul_left_comm,
pi.basis_fun_apply] },
{ intros, simp only [zero_smul, smul_zero] } },
{ intros, simp only [zero_smul, finsupp.sum_zero] }
end
lemma bilin_form.to_matrix'_comp_left (B : bilin_form R₃ (n → R₃))
(f : (n → R₃) →ₗ[R₃] (n → R₃)) : (B.comp_left f).to_matrix' = f.to_matrix'ᵀ ⬝ B.to_matrix' :=
by simp only [bilin_form.comp_left, bilin_form.to_matrix'_comp, to_matrix'_id, matrix.mul_one]
lemma bilin_form.to_matrix'_comp_right (B : bilin_form R₃ (n → R₃))
(f : (n → R₃) →ₗ[R₃] (n → R₃)) : (B.comp_right f).to_matrix' = B.to_matrix' ⬝ f.to_matrix' :=
by simp only [bilin_form.comp_right, bilin_form.to_matrix'_comp, to_matrix'_id,
transpose_one, matrix.one_mul]
lemma bilin_form.mul_to_matrix'_mul (B : bilin_form R₃ (n → R₃))
(M : matrix o n R₃) (N : matrix n o R₃) :
M ⬝ B.to_matrix' ⬝ N = (B.comp Mᵀ.to_lin' N.to_lin').to_matrix' :=
by simp only [B.to_matrix'_comp, transpose_transpose, to_matrix'_to_lin']
lemma bilin_form.mul_to_matrix' (B : bilin_form R₃ (n → R₃)) (M : matrix n n R₃) :
M ⬝ B.to_matrix' = (B.comp_left Mᵀ.to_lin').to_matrix' :=
by simp only [B.to_matrix'_comp_left, transpose_transpose, to_matrix'_to_lin']
lemma bilin_form.to_matrix'_mul (B : bilin_form R₃ (n → R₃)) (M : matrix n n R₃) :
B.to_matrix' ⬝ M = (B.comp_right M.to_lin').to_matrix' :=
by simp only [B.to_matrix'_comp_right, to_matrix'_to_lin']
lemma matrix.to_bilin'_comp (M : matrix n n R₃) (P Q : matrix n o R₃) :
M.to_bilin'.comp P.to_lin' Q.to_lin' = (Pᵀ ⬝ M ⬝ Q).to_bilin' :=
bilin_form.to_matrix'.injective
(by simp only [bilin_form.to_matrix'_comp, bilin_form.to_matrix'_to_bilin', to_matrix'_to_lin'])
end to_matrix'
section to_matrix
/-! ### `to_matrix` section
This section deals with the conversion between matrices and bilinear forms on
a module with a fixed basis.
-/
variables [decidable_eq n] (b : basis n R₃ M₃)
/-- `bilin_form.to_matrix b` is the equivalence between `R`-bilinear forms on `M` and
`n`-by-`n` matrices with entries in `R`, if `b` is an `R`-basis for `M`. -/
noncomputable def bilin_form.to_matrix : bilin_form R₃ M₃ ≃ₗ[R₃] matrix n n R₃ :=
(bilin_form.congr b.equiv_fun).trans bilin_form.to_matrix'
/-- `bilin_form.to_matrix b` is the equivalence between `R`-bilinear forms on `M` and
`n`-by-`n` matrices with entries in `R`, if `b` is an `R`-basis for `M`. -/
noncomputable def matrix.to_bilin : matrix n n R₃ ≃ₗ[R₃] bilin_form R₃ M₃ :=
(bilin_form.to_matrix b).symm
@[simp] lemma basis.equiv_fun_symm_std_basis (i : n) :
b.equiv_fun.symm (std_basis R₃ (λ _, R₃) i 1) = b i :=
begin
rw [b.equiv_fun_symm_apply, finset.sum_eq_single i],
{ rw [std_basis_same, one_smul] },
{ rintros j - hj,
rw [std_basis_ne _ _ _ _ hj, zero_smul] },
{ intro,
have := mem_univ i,
contradiction }
end
@[simp] lemma bilin_form.to_matrix_apply (B : bilin_form R₃ M₃) (i j : n) :
bilin_form.to_matrix b B i j = B (b i) (b j) :=
by rw [bilin_form.to_matrix, linear_equiv.trans_apply, bilin_form.to_matrix'_apply, congr_apply,
b.equiv_fun_symm_std_basis, b.equiv_fun_symm_std_basis]
@[simp] lemma matrix.to_bilin_apply (M : matrix n n R₃) (x y : M₃) :
matrix.to_bilin b M x y = ∑ i j, b.repr x i * M i j * b.repr y j :=
begin
rw [matrix.to_bilin, bilin_form.to_matrix, linear_equiv.symm_trans_apply, ← matrix.to_bilin'],
simp only [congr_symm, congr_apply, linear_equiv.symm_symm, matrix.to_bilin'_apply,
basis.equiv_fun_apply]
end
-- Not a `simp` lemma since `bilin_form.to_matrix` needs an extra argument
lemma bilinear_form.to_matrix_aux_eq (B : bilin_form R₃ M₃) :
bilin_form.to_matrix_aux b B = bilin_form.to_matrix b B :=
ext (λ i j, by rw [bilin_form.to_matrix_apply, bilin_form.to_matrix_aux, linear_map.coe_mk])
@[simp] lemma bilin_form.to_matrix_symm :
(bilin_form.to_matrix b).symm = matrix.to_bilin b :=
rfl
@[simp] lemma matrix.to_bilin_symm :
(matrix.to_bilin b).symm = bilin_form.to_matrix b :=
(bilin_form.to_matrix b).symm_symm
lemma matrix.to_bilin_basis_fun :
matrix.to_bilin (pi.basis_fun R₃ n) = matrix.to_bilin' :=
by { ext M, simp only [matrix.to_bilin_apply, matrix.to_bilin'_apply, pi.basis_fun_repr] }
lemma bilin_form.to_matrix_basis_fun :
bilin_form.to_matrix (pi.basis_fun R₃ n) = bilin_form.to_matrix' :=
by { ext B, rw [bilin_form.to_matrix_apply, bilin_form.to_matrix'_apply,
pi.basis_fun_apply, pi.basis_fun_apply] }
@[simp] lemma matrix.to_bilin_to_matrix (B : bilin_form R₃ M₃) :
matrix.to_bilin b (bilin_form.to_matrix b B) = B :=
(matrix.to_bilin b).apply_symm_apply B
@[simp] lemma bilin_form.to_matrix_to_bilin (M : matrix n n R₃) :
bilin_form.to_matrix b (matrix.to_bilin b M) = M :=
(bilin_form.to_matrix b).apply_symm_apply M
variables {M₃' : Type*} [add_comm_group M₃'] [module R₃ M₃']
variables (c : basis o R₃ M₃')
variables [decidable_eq o]
-- Cannot be a `simp` lemma because `b` must be inferred.
lemma bilin_form.to_matrix_comp
(B : bilin_form R₃ M₃) (l r : M₃' →ₗ[R₃] M₃) :
bilin_form.to_matrix c (B.comp l r) =
(to_matrix c b l)ᵀ ⬝ bilin_form.to_matrix b B ⬝ to_matrix c b r :=
begin
ext i j,
simp only [bilin_form.to_matrix_apply, bilin_form.comp_apply, transpose_apply, matrix.mul_apply,
linear_map.to_matrix', linear_equiv.coe_mk, sum_mul],
rw sum_comm,
conv_lhs { rw ← sum_repr_mul_repr_mul b },
rw finsupp.sum_fintype,
{ apply sum_congr rfl,
rintros i' -,
rw finsupp.sum_fintype,
{ apply sum_congr rfl,
rintros j' -,
simp only [smul_eq_mul, linear_map.to_matrix_apply,
basis.equiv_fun_apply, mul_assoc, mul_comm, mul_left_comm] },
{ intros, simp only [zero_smul, smul_zero] } },
{ intros, simp only [zero_smul, finsupp.sum_zero] }
end
lemma bilin_form.to_matrix_comp_left (B : bilin_form R₃ M₃) (f : M₃ →ₗ[R₃] M₃) :
bilin_form.to_matrix b (B.comp_left f) = (to_matrix b b f)ᵀ ⬝ bilin_form.to_matrix b B :=
by simp only [comp_left, bilin_form.to_matrix_comp b b, to_matrix_id, matrix.mul_one]
lemma bilin_form.to_matrix_comp_right (B : bilin_form R₃ M₃) (f : M₃ →ₗ[R₃] M₃) :
bilin_form.to_matrix b (B.comp_right f) = bilin_form.to_matrix b B ⬝ (to_matrix b b f) :=
by simp only [bilin_form.comp_right, bilin_form.to_matrix_comp b b, to_matrix_id,
transpose_one, matrix.one_mul]
@[simp]
lemma bilin_form.to_matrix_mul_basis_to_matrix (c : basis o R₃ M₃) (B : bilin_form R₃ M₃) :
(b.to_matrix c)ᵀ ⬝ bilin_form.to_matrix b B ⬝ b.to_matrix c = bilin_form.to_matrix c B :=
by rw [← linear_map.to_matrix_id_eq_basis_to_matrix, ← bilin_form.to_matrix_comp,
bilin_form.comp_id_id]
lemma bilin_form.mul_to_matrix_mul (B : bilin_form R₃ M₃)
(M : matrix o n R₃) (N : matrix n o R₃) :
M ⬝ bilin_form.to_matrix b B ⬝ N =
bilin_form.to_matrix c (B.comp (to_lin c b Mᵀ) (to_lin c b N)) :=
by simp only [B.to_matrix_comp b c, to_matrix_to_lin, transpose_transpose]
lemma bilin_form.mul_to_matrix (B : bilin_form R₃ M₃) (M : matrix n n R₃) :
M ⬝ bilin_form.to_matrix b B =
bilin_form.to_matrix b (B.comp_left (to_lin b b Mᵀ)) :=
by rw [B.to_matrix_comp_left b, to_matrix_to_lin, transpose_transpose]
lemma bilin_form.to_matrix_mul (B : bilin_form R₃ M₃) (M : matrix n n R₃) :
bilin_form.to_matrix b B ⬝ M =
bilin_form.to_matrix b (B.comp_right (to_lin b b M)) :=
by rw [B.to_matrix_comp_right b, to_matrix_to_lin]
lemma matrix.to_bilin_comp (M : matrix n n R₃) (P Q : matrix n o R₃) :
(matrix.to_bilin b M).comp (to_lin c b P) (to_lin c b Q) = matrix.to_bilin c (Pᵀ ⬝ M ⬝ Q) :=
(bilin_form.to_matrix c).injective
(by simp only [bilin_form.to_matrix_comp b c, bilin_form.to_matrix_to_bilin, to_matrix_to_lin])
end to_matrix
end matrix
namespace bilin_form
/-- The proposition that a bilinear form is reflexive -/
def is_refl (B : bilin_form R M) : Prop := ∀ (x y : M), B x y = 0 → B y x = 0
namespace is_refl
variable (H : B.is_refl)
lemma eq_zero : ∀ {x y : M}, B x y = 0 → B y x = 0 := λ x y, H x y
lemma ortho_comm {x y : M} :
is_ortho B x y ↔ is_ortho B y x := ⟨eq_zero H, eq_zero H⟩
end is_refl
/-- The proposition that a bilinear form is symmetric -/
def is_symm (B : bilin_form R M) : Prop := ∀ (x y : M), B x y = B y x
namespace is_symm
variable (H : B.is_symm)
protected lemma eq (x y : M) : B x y = B y x := H x y
lemma is_refl : B.is_refl := λ x y H1, H x y ▸ H1
lemma ortho_comm {x y : M} :
is_ortho B x y ↔ is_ortho B y x := H.is_refl.ortho_comm
end is_symm
lemma is_symm_iff_flip' [algebra R₂ R] : B.is_symm ↔ flip_hom R₂ B = B :=
begin
split,
{ intros h,
ext x y,
exact h y x },
{ intros h x y,
conv_lhs { rw ← h },
simp }
end
/-- The proposition that a bilinear form is alternating -/
def is_alt (B : bilin_form R M) : Prop := ∀ (x : M), B x x = 0
namespace is_alt
lemma self_eq_zero (H : B.is_alt) (x : M) : B x x = 0 := H x
lemma neg (H : B₁.is_alt) (x y : M₁) :
- B₁ x y = B₁ y x :=
begin
have H1 : B₁ (x + y) (x + y) = 0,
{ exact self_eq_zero H (x + y) },
rw [add_left, add_right, add_right,
self_eq_zero H, self_eq_zero H, ring.zero_add,
ring.add_zero, add_eq_zero_iff_neg_eq] at H1,
exact H1,
end
lemma is_refl (H : B₁.is_alt) : B₁.is_refl :=
begin
intros x y h,
rw [←neg H, h, neg_zero],
end
lemma ortho_comm (H : B₁.is_alt) {x y : M₁} :
is_ortho B₁ x y ↔ is_ortho B₁ y x := H.is_refl.ortho_comm
end is_alt
section linear_adjoints
variables (B) (F : bilin_form R M)
variables {M' : Type*} [add_comm_monoid M'] [module R M']
variables (B' : bilin_form R M') (f f' : M →ₗ[R] M') (g g' : M' →ₗ[R] M)
/-- Given a pair of modules equipped with bilinear forms, this is the condition for a pair of
maps between them to be mutually adjoint. -/
def is_adjoint_pair := ∀ ⦃x y⦄, B' (f x) y = B x (g y)
variables {B B' B₂ f f' g g'}
lemma is_adjoint_pair.eq (h : is_adjoint_pair B B' f g) :
∀ {x y}, B' (f x) y = B x (g y) := h
lemma is_adjoint_pair_iff_comp_left_eq_comp_right (f g : module.End R M) :
is_adjoint_pair B F f g ↔ F.comp_left f = B.comp_right g :=
begin
split; intros h,
{ ext x y, rw [comp_left_apply, comp_right_apply], apply h, },
{ intros x y, rw [←comp_left_apply, ←comp_right_apply], rw h, },
end
lemma is_adjoint_pair_zero : is_adjoint_pair B B' 0 0 :=
λ x y, by simp only [bilin_form.zero_left, bilin_form.zero_right, linear_map.zero_apply]
lemma is_adjoint_pair_id : is_adjoint_pair B B 1 1 := λ x y, rfl
lemma is_adjoint_pair.add (h : is_adjoint_pair B B' f g) (h' : is_adjoint_pair B B' f' g') :
is_adjoint_pair B B' (f + f') (g + g') :=
λ x y, by rw [linear_map.add_apply, linear_map.add_apply, add_left, add_right, h, h']
variables {M₁' : Type*} [add_comm_group M₁'] [module R₁ M₁']
variables {B₁' : bilin_form R₁ M₁'} {f₁ f₁' : M₁ →ₗ[R₁] M₁'} {g₁ g₁' : M₁' →ₗ[R₁] M₁}
lemma is_adjoint_pair.sub (h : is_adjoint_pair B₁ B₁' f₁ g₁) (h' : is_adjoint_pair B₁ B₁' f₁' g₁') :
is_adjoint_pair B₁ B₁' (f₁ - f₁') (g₁ - g₁') :=
λ x y, by rw [linear_map.sub_apply, linear_map.sub_apply, sub_left, sub_right, h, h']
variables {M₂' : Type*} [add_comm_monoid M₂'] [module R₂ M₂']
variables {B₂' : bilin_form R₂ M₂'} {f₂ f₂' : M₂ →ₗ[R₂] M₂'} {g₂ g₂' : M₂' →ₗ[R₂] M₂}
lemma is_adjoint_pair.smul (c : R₂) (h : is_adjoint_pair B₂ B₂' f₂ g₂) :
is_adjoint_pair B₂ B₂' (c • f₂) (c • g₂) :=
λ x y, by rw [linear_map.smul_apply, linear_map.smul_apply, smul_left, smul_right, h]
variables {M'' : Type*} [add_comm_monoid M''] [module R M'']
variables (B'' : bilin_form R M'')
lemma is_adjoint_pair.comp {f' : M' →ₗ[R] M''} {g' : M'' →ₗ[R] M'}
(h : is_adjoint_pair B B' f g) (h' : is_adjoint_pair B' B'' f' g') :
is_adjoint_pair B B'' (f'.comp f) (g.comp g') :=
λ x y, by rw [linear_map.comp_apply, linear_map.comp_apply, h', h]
lemma is_adjoint_pair.mul
{f g f' g' : module.End R M} (h : is_adjoint_pair B B f g) (h' : is_adjoint_pair B B f' g') :
is_adjoint_pair B B (f * f') (g' * g) :=
λ x y, by rw [linear_map.mul_apply, linear_map.mul_apply, h, h']
variables (B B' B₁ B₂) (F₂ : bilin_form R₂ M₂)
/-- The condition for an endomorphism to be "self-adjoint" with respect to a pair of bilinear forms
on the underlying module. In the case that these two forms are identical, this is the usual concept
of self adjointness. In the case that one of the forms is the negation of the other, this is the
usual concept of skew adjointness. -/
def is_pair_self_adjoint (f : module.End R M) := is_adjoint_pair B F f f
/-- The set of pair-self-adjoint endomorphisms are a submodule of the type of all endomorphisms. -/
def is_pair_self_adjoint_submodule : submodule R₂ (module.End R₂ M₂) :=
{ carrier := { f | is_pair_self_adjoint B₂ F₂ f },
zero_mem' := is_adjoint_pair_zero,
add_mem' := λ f g hf hg, hf.add hg,
smul_mem' := λ c f h, h.smul c, }
@[simp] lemma mem_is_pair_self_adjoint_submodule (f : module.End R₂ M₂) :
f ∈ is_pair_self_adjoint_submodule B₂ F₂ ↔ is_pair_self_adjoint B₂ F₂ f :=
by refl
variables {M₃' : Type*} [add_comm_group M₃'] [module R₃ M₃']
variables (B₃ F₃ : bilin_form R₃ M₃)
lemma is_pair_self_adjoint_equiv (e : M₃' ≃ₗ[R₃] M₃) (f : module.End R₃ M₃) :
is_pair_self_adjoint B₃ F₃ f ↔
is_pair_self_adjoint (B₃.comp ↑e ↑e) (F₃.comp ↑e ↑e) (e.symm.conj f) :=
begin
have hₗ : (F₃.comp ↑e ↑e).comp_left (e.symm.conj f) = (F₃.comp_left f).comp ↑e ↑e :=
by { ext, simp [linear_equiv.symm_conj_apply], },
have hᵣ : (B₃.comp ↑e ↑e).comp_right (e.symm.conj f) = (B₃.comp_right f).comp ↑e ↑e :=
by { ext, simp [linear_equiv.conj_apply], },
have he : function.surjective (⇑(↑e : M₃' →ₗ[R₃] M₃) : M₃' → M₃) := e.surjective,
show bilin_form.is_adjoint_pair _ _ _ _ ↔ bilin_form.is_adjoint_pair _ _ _ _,
rw [is_adjoint_pair_iff_comp_left_eq_comp_right, is_adjoint_pair_iff_comp_left_eq_comp_right,
hᵣ, hₗ, comp_injective _ _ he he],
end
/-- An endomorphism of a module is self-adjoint with respect to a bilinear form if it serves as an
adjoint for itself. -/
def is_self_adjoint (f : module.End R M) := is_adjoint_pair B B f f
/-- An endomorphism of a module is skew-adjoint with respect to a bilinear form if its negation
serves as an adjoint. -/
def is_skew_adjoint (f : module.End R₁ M₁) := is_adjoint_pair B₁ B₁ f (-f)
lemma is_skew_adjoint_iff_neg_self_adjoint (f : module.End R₁ M₁) :
B₁.is_skew_adjoint f ↔ is_adjoint_pair (-B₁) B₁ f f :=
show (∀ x y, B₁ (f x) y = B₁ x ((-f) y)) ↔ ∀ x y, B₁ (f x) y = (-B₁) x (f y),
by simp only [linear_map.neg_apply, bilin_form.neg_apply, bilin_form.neg_right]
/-- The set of self-adjoint endomorphisms of a module with bilinear form is a submodule. (In fact
it is a Jordan subalgebra.) -/
def self_adjoint_submodule := is_pair_self_adjoint_submodule B₂ B₂
@[simp] lemma mem_self_adjoint_submodule (f : module.End R₂ M₂) :
f ∈ B₂.self_adjoint_submodule ↔ B₂.is_self_adjoint f := iff.rfl
/-- The set of skew-adjoint endomorphisms of a module with bilinear form is a submodule. (In fact
it is a Lie subalgebra.) -/
def skew_adjoint_submodule := is_pair_self_adjoint_submodule (-B₃) B₃
@[simp] lemma mem_skew_adjoint_submodule (f : module.End R₃ M₃) :
f ∈ B₃.skew_adjoint_submodule ↔ B₃.is_skew_adjoint f :=
by { rw is_skew_adjoint_iff_neg_self_adjoint, exact iff.rfl, }
end linear_adjoints
end bilin_form
section matrix_adjoints
open_locale matrix
variables {n : Type w} [fintype n]
variables (b : basis n R₃ M₃)
variables (J J₃ A A' : matrix n n R₃)
/-- The condition for the square matrices `A`, `A'` to be an adjoint pair with respect to the square
matrices `J`, `J₃`. -/
def matrix.is_adjoint_pair := Aᵀ ⬝ J₃ = J ⬝ A'
/-- The condition for a square matrix `A` to be self-adjoint with respect to the square matrix
`J`. -/
def matrix.is_self_adjoint := matrix.is_adjoint_pair J J A A
/-- The condition for a square matrix `A` to be skew-adjoint with respect to the square matrix
`J`. -/
def matrix.is_skew_adjoint := matrix.is_adjoint_pair J J A (-A)
@[simp] lemma is_adjoint_pair_to_bilin' [decidable_eq n] :
bilin_form.is_adjoint_pair (matrix.to_bilin' J) (matrix.to_bilin' J₃)
(matrix.to_lin' A) (matrix.to_lin' A') ↔
matrix.is_adjoint_pair J J₃ A A' :=
begin
rw bilin_form.is_adjoint_pair_iff_comp_left_eq_comp_right,
have h : ∀ (B B' : bilin_form R₃ (n → R₃)), B = B' ↔
(bilin_form.to_matrix' B) = (bilin_form.to_matrix' B'),
{ intros B B',
split; intros h,
{ rw h },
{ exact bilin_form.to_matrix'.injective h } },
rw [h, bilin_form.to_matrix'_comp_left, bilin_form.to_matrix'_comp_right,
linear_map.to_matrix'_to_lin', linear_map.to_matrix'_to_lin',
bilin_form.to_matrix'_to_bilin', bilin_form.to_matrix'_to_bilin'],
refl,
end
@[simp] lemma is_adjoint_pair_to_bilin [decidable_eq n] :
bilin_form.is_adjoint_pair (matrix.to_bilin b J) (matrix.to_bilin b J₃)
(matrix.to_lin b b A) (matrix.to_lin b b A') ↔
matrix.is_adjoint_pair J J₃ A A' :=
begin
rw bilin_form.is_adjoint_pair_iff_comp_left_eq_comp_right,
have h : ∀ (B B' : bilin_form R₃ M₃), B = B' ↔
(bilin_form.to_matrix b B) = (bilin_form.to_matrix b B'),
{ intros B B',
split; intros h,
{ rw h },
{ exact (bilin_form.to_matrix b).injective h } },
rw [h, bilin_form.to_matrix_comp_left, bilin_form.to_matrix_comp_right,
linear_map.to_matrix_to_lin, linear_map.to_matrix_to_lin,
bilin_form.to_matrix_to_bilin, bilin_form.to_matrix_to_bilin],
refl,
end
lemma matrix.is_adjoint_pair_equiv [decidable_eq n] (P : matrix n n R₃) (h : is_unit P) :
(Pᵀ ⬝ J ⬝ P).is_adjoint_pair (Pᵀ ⬝ J ⬝ P) A A' ↔
J.is_adjoint_pair J (P ⬝ A ⬝ P⁻¹) (P ⬝ A' ⬝ P⁻¹) :=
have h' : is_unit P.det := P.is_unit_iff_is_unit_det.mp h,
begin
let u := P.nonsing_inv_unit h',
let v := Pᵀ.nonsing_inv_unit (P.is_unit_det_transpose h'),
let x := Aᵀ * Pᵀ * J,
let y := J * P * A',
suffices : x * ↑u = ↑v * y ↔ ↑v⁻¹ * x = y * ↑u⁻¹,
{ dunfold matrix.is_adjoint_pair,
repeat { rw matrix.transpose_mul, },
simp only [←matrix.mul_eq_mul, ←mul_assoc, P.transpose_nonsing_inv],
conv_lhs { to_rhs, rw [mul_assoc, mul_assoc], congr, skip, rw ←mul_assoc, },
conv_rhs { rw [mul_assoc, mul_assoc], conv { to_lhs, congr, skip, rw ←mul_assoc }, },
exact this, },
rw units.eq_mul_inv_iff_mul_eq, conv_rhs { rw mul_assoc, }, rw v.inv_mul_eq_iff_eq_mul,
end
variables [decidable_eq n]
/-- The submodule of pair-self-adjoint matrices with respect to bilinear forms corresponding to
given matrices `J`, `J₂`. -/
def pair_self_adjoint_matrices_submodule : submodule R₃ (matrix n n R₃) :=
(bilin_form.is_pair_self_adjoint_submodule (matrix.to_bilin' J) (matrix.to_bilin' J₃)).map
((linear_map.to_matrix' : ((n → R₃) →ₗ[R₃] (n → R₃)) ≃ₗ[R₃] matrix n n R₃) :
((n → R₃) →ₗ[R₃] (n → R₃)) →ₗ[R₃] matrix n n R₃)
@[simp] lemma mem_pair_self_adjoint_matrices_submodule :
A ∈ (pair_self_adjoint_matrices_submodule J J₃) ↔ matrix.is_adjoint_pair J J₃ A A :=
begin
simp only [pair_self_adjoint_matrices_submodule, linear_equiv.coe_coe,
linear_map.to_matrix'_apply, submodule.mem_map, bilin_form.mem_is_pair_self_adjoint_submodule],
split,
{ rintros ⟨f, hf, hA⟩,
have hf' : f = A.to_lin' := by rw [←hA, matrix.to_lin'_to_matrix'], rw hf' at hf,
rw ← is_adjoint_pair_to_bilin',
exact hf, },
{ intros h, refine ⟨A.to_lin', _, linear_map.to_matrix'_to_lin' _⟩,
exact (is_adjoint_pair_to_bilin' _ _ _ _).mpr h, },
end
/-- The submodule of self-adjoint matrices with respect to the bilinear form corresponding to
the matrix `J`. -/
def self_adjoint_matrices_submodule : submodule R₃ (matrix n n R₃) :=
pair_self_adjoint_matrices_submodule J J
@[simp] lemma mem_self_adjoint_matrices_submodule :
A ∈ self_adjoint_matrices_submodule J ↔ J.is_self_adjoint A :=
by { erw mem_pair_self_adjoint_matrices_submodule, refl, }
/-- The submodule of skew-adjoint matrices with respect to the bilinear form corresponding to
the matrix `J`. -/
def skew_adjoint_matrices_submodule : submodule R₃ (matrix n n R₃) :=
pair_self_adjoint_matrices_submodule (-J) J
@[simp] lemma mem_skew_adjoint_matrices_submodule :
A ∈ skew_adjoint_matrices_submodule J ↔ J.is_skew_adjoint A :=
begin
erw mem_pair_self_adjoint_matrices_submodule,
simp [matrix.is_skew_adjoint, matrix.is_adjoint_pair],
end
end matrix_adjoints
namespace bilin_form
section orthogonal
/-- The orthogonal complement of a submodule `N` with respect to some bilinear form is the set of
elements `x` which are orthogonal to all elements of `N`; i.e., for all `y` in `N`, `B x y = 0`.
Note that for general (neither symmetric nor antisymmetric) bilinear forms this definition has a
chirality; in addition to this "left" orthogonal complement one could define a "right" orthogonal
complement for which, for all `y` in `N`, `B y x = 0`. This variant definition is not currently
provided in mathlib. -/
def orthogonal (B : bilin_form R M) (N : submodule R M) : submodule R M :=
{ carrier := { m | ∀ n ∈ N, is_ortho B n m },
zero_mem' := λ x _, is_ortho_zero_right x,
add_mem' := λ x y hx hy n hn,
by rw [is_ortho, add_right, show B n x = 0, by exact hx n hn,
show B n y = 0, by exact hy n hn, zero_add],
smul_mem' := λ c x hx n hn,
by rw [is_ortho, smul_right, show B n x = 0, by exact hx n hn, mul_zero] }
variables {N L : submodule R M}
@[simp] lemma mem_orthogonal_iff {N : submodule R M} {m : M} :
m ∈ B.orthogonal N ↔ ∀ n ∈ N, is_ortho B n m := iff.rfl
lemma orthogonal_le (h : N ≤ L) : B.orthogonal L ≤ B.orthogonal N :=
λ _ hn l hl, hn l (h hl)
lemma le_orthogonal_orthogonal (b : B.is_refl) :
N ≤ B.orthogonal (B.orthogonal N) :=
λ n hn m hm, b _ _ (hm n hn)
-- ↓ This lemma only applies in fields as we require `a * b = 0 → a = 0 ∨ b = 0`
lemma span_singleton_inf_orthogonal_eq_bot
{B : bilin_form K V} {x : V} (hx : ¬ B.is_ortho x x) :
(K ∙ x) ⊓ B.orthogonal (K ∙ x) = ⊥ :=
begin
rw ← finset.coe_singleton,
refine eq_bot_iff.2 (λ y h, _),
rcases mem_span_finset.1 h.1 with ⟨μ, rfl⟩,
have := h.2 x _,
{ rw finset.sum_singleton at this ⊢,
suffices hμzero : μ x = 0,
{ rw [hμzero, zero_smul, submodule.mem_bot] },
change B x (μ x • x) = 0 at this, rw [smul_right] at this,
exact or.elim (zero_eq_mul.mp this.symm) id (λ hfalse, false.elim $ hx hfalse) },
{ rw submodule.mem_span; exact λ _ hp, hp $ finset.mem_singleton_self _ }
end
-- ↓ This lemma only applies in fields since we use the `mul_eq_zero`
lemma orthogonal_span_singleton_eq_to_lin_ker {B : bilin_form K V} (x : V) :
B.orthogonal (K ∙ x) = (bilin_form.to_lin B x).ker :=
begin
ext y,
simp_rw [mem_orthogonal_iff, linear_map.mem_ker,
submodule.mem_span_singleton ],
split,
{ exact λ h, h x ⟨1, one_smul _ _⟩ },
{ rintro h _ ⟨z, rfl⟩,
rw [is_ortho, smul_left, mul_eq_zero],
exact or.intro_right _ h }
end
lemma span_singleton_sup_orthogonal_eq_top {B : bilin_form K V}
{x : V} (hx : ¬ B.is_ortho x x) :
(K ∙ x) ⊔ B.orthogonal (K ∙ x) = ⊤ :=
begin
rw orthogonal_span_singleton_eq_to_lin_ker,
exact linear_map.span_singleton_sup_ker_eq_top _ hx,
end
/-- Given a bilinear form `B` and some `x` such that `B x x ≠ 0`, the span of the singleton of `x`
is complement to its orthogonal complement. -/
lemma is_compl_span_singleton_orthogonal {B : bilin_form K V}
{x : V} (hx : ¬ B.is_ortho x x) : is_compl (K ∙ x) (B.orthogonal $ K ∙ x) :=
{ inf_le_bot := eq_bot_iff.1 $ span_singleton_inf_orthogonal_eq_bot hx,
top_le_sup := eq_top_iff.1 $ span_singleton_sup_orthogonal_eq_top hx }
end orthogonal
/-- The restriction of a bilinear form on a submodule. -/
@[simps apply]
def restrict (B : bilin_form R M) (W : submodule R M) : bilin_form R W :=
{ bilin := λ a b, B a b,
bilin_add_left := λ _ _ _, add_left _ _ _,
bilin_smul_left := λ _ _ _, smul_left _ _ _,
bilin_add_right := λ _ _ _, add_right _ _ _,
bilin_smul_right := λ _ _ _, smul_right _ _ _}
/-- The restriction of a symmetric bilinear form on a submodule is also symmetric. -/
lemma restrict_symm (B : bilin_form R M) (b : B.is_symm)
(W : submodule R M) : (B.restrict W).is_symm :=
λ x y, b x y
/-- A nondegenerate bilinear form is a bilinear form such that the only element that is orthogonal
to every other element is `0`; i.e., for all nonzero `m` in `M`, there exists `n` in `M` with
`B m n ≠ 0`.
Note that for general (neither symmetric nor antisymmetric) bilinear forms this definition has a
chirality; in addition to this "left" nondegeneracy condition one could define a "right"
nondegeneracy condition that in the situation described, `B n m ≠ 0`. This variant definition is
not currently provided in mathlib. In finite dimension either definition implies the other. -/
def nondegenerate (B : bilin_form R M) : Prop :=
∀ m : M, (∀ n : M, B m n = 0) → m = 0
section
variables (R M)
/-- In a non-trivial module, zero is not non-degenerate. -/
lemma not_nondegenerate_zero [nontrivial M] : ¬(0 : bilin_form R M).nondegenerate :=
let ⟨m, hm⟩ := exists_ne (0 : M) in λ h, hm (h m $ λ n, rfl)
end
variables {M₂' : Type*}
variables [add_comm_monoid M₂'] [module R₂ M₂']
lemma nondegenerate.ne_zero [nontrivial M] {B : bilin_form R M} (h : B.nondegenerate) : B ≠ 0 :=
λ h0, not_nondegenerate_zero R M $ h0 ▸ h
lemma nondegenerate.congr {B : bilin_form R₂ M₂} (e : M₂ ≃ₗ[R₂] M₂') (h : B.nondegenerate) :
(congr e B).nondegenerate :=
λ m hm, (e.symm).map_eq_zero_iff.1 $ h (e.symm m) $
λ n, (congr_arg _ (e.symm_apply_apply n).symm).trans (hm (e n))
@[simp] lemma nondegenerate_congr_iff {B : bilin_form R₂ M₂} (e : M₂ ≃ₗ[R₂] M₂') :
(congr e B).nondegenerate ↔ B.nondegenerate :=
⟨λ h, begin
convert h.congr e.symm,
rw [congr_congr, e.self_trans_symm, congr_refl, linear_equiv.refl_apply],
end, nondegenerate.congr e⟩
/-- A bilinear form is nondegenerate if and only if it has a trivial kernel. -/
theorem nondegenerate_iff_ker_eq_bot {B : bilin_form R₂ M₂} :
B.nondegenerate ↔ B.to_lin.ker = ⊥ :=
begin
rw linear_map.ker_eq_bot',
split; intro h,
{ refine λ m hm, h _ (λ x, _),
rw [← to_lin_apply, hm], refl },
{ intros m hm, apply h,
ext x, exact hm x }
end
lemma nondegenerate.ker_eq_bot {B : bilin_form R₂ M₂} (h : B.nondegenerate) :
B.to_lin.ker = ⊥ := nondegenerate_iff_ker_eq_bot.mp h
/-- The restriction of a nondegenerate bilinear form `B` onto a submodule `W` is
nondegenerate if `disjoint W (B.orthogonal W)`. -/
lemma nondegenerate_restrict_of_disjoint_orthogonal
(B : bilin_form R₁ M₁) (b : B.is_symm)
{W : submodule R₁ M₁} (hW : disjoint W (B.orthogonal W)) :
(B.restrict W).nondegenerate :=
begin
rintro ⟨x, hx⟩ b₁,
rw [submodule.mk_eq_zero, ← submodule.mem_bot R₁],
refine hW ⟨hx, λ y hy, _⟩,
specialize b₁ ⟨y, hy⟩,
rwa [restrict_apply, submodule.coe_mk, submodule.coe_mk, b] at b₁
end
/-- An orthogonal basis with respect to a nondegenerate bilinear form has no self-orthogonal
elements. -/
lemma is_Ortho.not_is_ortho_basis_self_of_nondegenerate
{n : Type w} [nontrivial R] {B : bilin_form R M} {v : basis n R M}
(h : B.is_Ortho v) (hB : B.nondegenerate) (i : n) :
¬B.is_ortho (v i) (v i) :=
begin
intro ho,
refine v.ne_zero i (hB (v i) $ λ m, _),
obtain ⟨vi, rfl⟩ := v.repr.symm.surjective m,
rw [basis.repr_symm_apply, finsupp.total_apply, finsupp.sum, sum_right],
apply finset.sum_eq_zero,
rintros j -,
rw smul_right,
convert mul_zero _ using 2,
obtain rfl | hij := eq_or_ne i j,
{ exact ho },
{ exact h i j hij },
end
/-- Given an orthogonal basis with respect to a bilinear form, the bilinear form is nondegenerate
iff the basis has no elements which are self-orthogonal. -/
lemma is_Ortho.nondegenerate_iff_not_is_ortho_basis_self {n : Type w} [nontrivial R]
[no_zero_divisors R] (B : bilin_form R M) (v : basis n R M) (hO : B.is_Ortho v) :
B.nondegenerate ↔ ∀ i, ¬B.is_ortho (v i) (v i) :=
begin
refine ⟨hO.not_is_ortho_basis_self_of_nondegenerate, λ ho m hB, _⟩,
obtain ⟨vi, rfl⟩ := v.repr.symm.surjective m,
rw linear_equiv.map_eq_zero_iff,
ext i,
rw [finsupp.zero_apply],
specialize hB (v i),
simp_rw [basis.repr_symm_apply, finsupp.total_apply, finsupp.sum, sum_left, smul_left] at hB,
rw finset.sum_eq_single i at hB,
{ exact eq_zero_of_ne_zero_of_mul_right_eq_zero (ho i) hB, },
{ intros j hj hij, convert mul_zero _ using 2, exact hO j i hij, },
{ intros hi, convert zero_mul _ using 2, exact finsupp.not_mem_support_iff.mp hi }
end
section
lemma to_lin_restrict_ker_eq_inf_orthogonal
(B : bilin_form K V) (W : subspace K V) (b : B.is_symm) :
(B.to_lin.dom_restrict W).ker.map W.subtype = (W ⊓ B.orthogonal ⊤ : subspace K V) :=
begin
ext x, split; intro hx,
{ rcases hx with ⟨⟨x, hx⟩, hker, rfl⟩,
erw linear_map.mem_ker at hker,
split,
{ simp [hx] },
{ intros y _,
rw [is_ortho, b],
change (B.to_lin.dom_restrict W) ⟨x, hx⟩ y = 0,
rw hker, refl } },
{ simp_rw [submodule.mem_map, linear_map.mem_ker],
refine ⟨⟨x, hx.1⟩, _, rfl⟩,
ext y, change B x y = 0,
rw b,
exact hx.2 _ submodule.mem_top }
end
lemma to_lin_restrict_range_dual_annihilator_comap_eq_orthogonal
(B : bilin_form K V) (W : subspace K V) :
(B.to_lin.dom_restrict W).range.dual_annihilator_comap = B.orthogonal W :=
begin
ext x, split; rw [mem_orthogonal_iff]; intro hx,
{ intros y hy,
rw submodule.mem_dual_annihilator_comap_iff at hx,
refine hx (B.to_lin.dom_restrict W ⟨y, hy⟩) ⟨⟨y, hy⟩, rfl⟩ },
{ rw submodule.mem_dual_annihilator_comap_iff,
rintro _ ⟨⟨w, hw⟩, rfl⟩,
exact hx w hw }
end
variable [finite_dimensional K V]
open finite_dimensional
lemma finrank_add_finrank_orthogonal
{B : bilin_form K V} {W : subspace K V} (b₁ : B.is_symm) :
finrank K W + finrank K (B.orthogonal W) =
finrank K V + finrank K (W ⊓ B.orthogonal ⊤ : subspace K V) :=
begin
rw [← to_lin_restrict_ker_eq_inf_orthogonal _ _ b₁,
← to_lin_restrict_range_dual_annihilator_comap_eq_orthogonal _ _,
finrank_map_subtype_eq],
conv_rhs { rw [← @subspace.finrank_add_finrank_dual_annihilator_comap_eq K V _ _ _ _
(B.to_lin.dom_restrict W).range,
add_comm, ← add_assoc, add_comm (finrank K ↥((B.to_lin.dom_restrict W).ker)),
linear_map.finrank_range_add_finrank_ker] },
end
/-- A subspace is complement to its orthogonal complement with respect to some
bilinear form if that bilinear form restricted on to the subspace is nondegenerate. -/
lemma restrict_nondegenerate_of_is_compl_orthogonal
{B : bilin_form K V} {W : subspace K V}
(b₁ : B.is_symm) (b₂ : (B.restrict W).nondegenerate) :
is_compl W (B.orthogonal W) :=
begin
have : W ⊓ B.orthogonal W = ⊥,
{ rw eq_bot_iff,
intros x hx,
obtain ⟨hx₁, hx₂⟩ := submodule.mem_inf.1 hx,
refine subtype.mk_eq_mk.1 (b₂ ⟨x, hx₁⟩ _),
rintro ⟨n, hn⟩,
rw [restrict_apply, submodule.coe_mk, submodule.coe_mk, b₁],
exact hx₂ n hn },
refine ⟨this ▸ le_rfl, _⟩,
{ rw top_le_iff,
refine eq_top_of_finrank_eq _,
refine le_antisymm (submodule.finrank_le _) _,
conv_rhs { rw ← add_zero (finrank K _) },
rw [← finrank_bot K V, ← this, submodule.dim_sup_add_dim_inf_eq,
finrank_add_finrank_orthogonal b₁],
exact nat.le.intro rfl }
end
/-- A subspace is complement to its orthogonal complement with respect to some bilinear form
if and only if that bilinear form restricted on to the subspace is nondegenerate. -/
theorem restrict_nondegenerate_iff_is_compl_orthogonal
{B : bilin_form K V} {W : subspace K V} (b₁ : B.is_symm) :
(B.restrict W).nondegenerate ↔ is_compl W (B.orthogonal W) :=
⟨λ b₂, restrict_nondegenerate_of_is_compl_orthogonal b₁ b₂,
λ h, B.nondegenerate_restrict_of_disjoint_orthogonal b₁ h.1⟩
/-- Given a nondegenerate bilinear form `B` on a finite-dimensional vector space, `B.to_dual` is
the linear equivalence between a vector space and its dual with the underlying linear map
`B.to_lin`. -/
noncomputable def to_dual (B : bilin_form K V) (b : B.nondegenerate) :
V ≃ₗ[K] module.dual K V :=
B.to_lin.linear_equiv_of_injective
(linear_map.ker_eq_bot.mp $ b.ker_eq_bot) subspace.dual_finrank_eq.symm
lemma to_dual_def {B : bilin_form K V} (b : B.nondegenerate) {m n : V} :
B.to_dual b m n = B m n := rfl
section dual_basis
variables {ι : Type*} [decidable_eq ι] [fintype ι]
/-- The `B`-dual basis `B.dual_basis hB b` to a finite basis `b` satisfies
`B (B.dual_basis hB b i) (b j) = B (b i) (B.dual_basis hB b j) = if i = j then 1 else 0`,
where `B` is a nondegenerate (symmetric) bilinear form and `b` is a finite basis. -/
noncomputable def dual_basis (B : bilin_form K V) (hB : B.nondegenerate) (b : basis ι K V) :
basis ι K V :=
b.dual_basis.map (B.to_dual hB).symm
@[simp] lemma dual_basis_repr_apply (B : bilin_form K V) (hB : B.nondegenerate) (b : basis ι K V)
(x i) : (B.dual_basis hB b).repr x i = B x (b i) :=
by rw [dual_basis, basis.map_repr, linear_equiv.symm_symm, linear_equiv.trans_apply,
basis.dual_basis_repr, to_dual_def]
lemma apply_dual_basis_left (B : bilin_form K V) (hB : B.nondegenerate) (b : basis ι K V)
(i j) : B (B.dual_basis hB b i) (b j) = if j = i then 1 else 0 :=
by rw [dual_basis, basis.map_apply, basis.coe_dual_basis, ← to_dual_def hB,
linear_equiv.apply_symm_apply, basis.coord_apply, basis.repr_self,
finsupp.single_apply]
lemma apply_dual_basis_right (B : bilin_form K V) (hB : B.nondegenerate)
(sym : B.is_symm) (b : basis ι K V)
(i j) : B (b i) (B.dual_basis hB b j) = if i = j then 1 else 0 :=
by rw [sym, apply_dual_basis_left]
end dual_basis
end
/-! We note that we cannot use `bilin_form.restrict_nondegenerate_iff_is_compl_orthogonal` for the
lemma below since the below lemma does not require `V` to be finite dimensional. However,
`bilin_form.restrict_nondegenerate_iff_is_compl_orthogonal` does not require `B` to be nondegenerate
on the whole space. -/
/-- The restriction of a symmetric, non-degenerate bilinear form on the orthogonal complement of
the span of a singleton is also non-degenerate. -/
lemma restrict_orthogonal_span_singleton_nondegenerate (B : bilin_form K V)
(b₁ : B.nondegenerate) (b₂ : B.is_symm) {x : V} (hx : ¬ B.is_ortho x x) :
nondegenerate $ B.restrict $ B.orthogonal (K ∙ x) :=
begin
refine λ m hm, submodule.coe_eq_zero.1 (b₁ m.1 (λ n, _)),
have : n ∈ (K ∙ x) ⊔ B.orthogonal (K ∙ x) :=
(span_singleton_sup_orthogonal_eq_top hx).symm ▸ submodule.mem_top,
rcases submodule.mem_sup.1 this with ⟨y, hy, z, hz, rfl⟩,
specialize hm ⟨z, hz⟩,
rw restrict at hm,
erw [add_right, show B m.1 y = 0, by rw b₂; exact m.2 y hy, hm, add_zero]
end
section linear_adjoints
lemma comp_left_injective (B : bilin_form R₁ M₁) (b : B.nondegenerate) :
function.injective B.comp_left :=
λ φ ψ h, begin
ext w,
refine eq_of_sub_eq_zero (b _ _),
intro v,
rw [sub_left, ← comp_left_apply, ← comp_left_apply, ← h, sub_self]
end
lemma is_adjoint_pair_unique_of_nondegenerate (B : bilin_form R₁ M₁) (b : B.nondegenerate)
(φ ψ₁ ψ₂ : M₁ →ₗ[R₁] M₁) (hψ₁ : is_adjoint_pair B B ψ₁ φ) (hψ₂ : is_adjoint_pair B B ψ₂ φ) :
ψ₁ = ψ₂ :=
B.comp_left_injective b $ ext $ λ v w, by rw [comp_left_apply, comp_left_apply, hψ₁, hψ₂]
variable [finite_dimensional K V]
/-- Given bilinear forms `B₁, B₂` where `B₂` is nondegenerate, `symm_comp_of_nondegenerate`
is the linear map `B₂.to_lin⁻¹ ∘ B₁.to_lin`. -/
noncomputable def symm_comp_of_nondegenerate
(B₁ B₂ : bilin_form K V) (b₂ : B₂.nondegenerate) : V →ₗ[K] V :=
(B₂.to_dual b₂).symm.to_linear_map.comp B₁.to_lin
lemma comp_symm_comp_of_nondegenerate_apply (B₁ : bilin_form K V)
{B₂ : bilin_form K V} (b₂ : B₂.nondegenerate) (v : V) :
to_lin B₂ (B₁.symm_comp_of_nondegenerate B₂ b₂ v) = to_lin B₁ v :=
by erw [symm_comp_of_nondegenerate, linear_equiv.apply_symm_apply (B₂.to_dual b₂) _]
@[simp]
lemma symm_comp_of_nondegenerate_left_apply (B₁ : bilin_form K V)
{B₂ : bilin_form K V} (b₂ : B₂.nondegenerate) (v w : V) :
B₂ (symm_comp_of_nondegenerate B₁ B₂ b₂ w) v = B₁ w v :=
begin
conv_lhs { rw [← bilin_form.to_lin_apply, comp_symm_comp_of_nondegenerate_apply] },
refl,
end
/-- Given the nondegenerate bilinear form `B` and the linear map `φ`,
`left_adjoint_of_nondegenerate` provides the left adjoint of `φ` with respect to `B`.
The lemma proving this property is `bilin_form.is_adjoint_pair_left_adjoint_of_nondegenerate`. -/
noncomputable def left_adjoint_of_nondegenerate
(B : bilin_form K V) (b : B.nondegenerate) (φ : V →ₗ[K] V) : V →ₗ[K] V :=
symm_comp_of_nondegenerate (B.comp_right φ) B b
lemma is_adjoint_pair_left_adjoint_of_nondegenerate
(B : bilin_form K V) (b : B.nondegenerate) (φ : V →ₗ[K] V) :
is_adjoint_pair B B (B.left_adjoint_of_nondegenerate b φ) φ :=
λ x y, (B.comp_right φ).symm_comp_of_nondegenerate_left_apply b y x
/-- Given the nondegenerate bilinear form `B`, the linear map `φ` has a unique left adjoint given by
`bilin_form.left_adjoint_of_nondegenerate`. -/
theorem is_adjoint_pair_iff_eq_of_nondegenerate
(B : bilin_form K V) (b : B.nondegenerate) (ψ φ : V →ₗ[K] V) :
is_adjoint_pair B B ψ φ ↔ ψ = B.left_adjoint_of_nondegenerate b φ :=
⟨λ h, B.is_adjoint_pair_unique_of_nondegenerate b φ ψ _ h
(is_adjoint_pair_left_adjoint_of_nondegenerate _ _ _),
λ h, h.symm ▸ is_adjoint_pair_left_adjoint_of_nondegenerate _ _ _⟩
end linear_adjoints
section det
open matrix
variables {A : Type*} [comm_ring A] [is_domain A] [module A M₃] (B₃ : bilin_form A M₃)
variables {ι : Type*} [decidable_eq ι] [fintype ι]
lemma _root_.matrix.nondegenerate_to_bilin'_iff_nondegenerate_to_bilin {M : matrix ι ι R₃}
(b : basis ι R₃ M₃) : M.to_bilin'.nondegenerate ↔ (matrix.to_bilin b M).nondegenerate :=
(nondegenerate_congr_iff b.equiv_fun.symm).symm
-- Lemmas transferring nondegeneracy between a matrix and its associated bilinear form
theorem _root_.matrix.nondegenerate.to_bilin' {M : matrix ι ι R₃} (h : M.nondegenerate) :
M.to_bilin'.nondegenerate :=
λ x hx, h.eq_zero_of_ortho $ λ y, by simpa only [to_bilin'_apply'] using hx y
@[simp] lemma _root_.matrix.nondegenerate_to_bilin'_iff {M : matrix ι ι R₃} :
M.to_bilin'.nondegenerate ↔ M.nondegenerate :=
⟨λ h v hv, h v $ λ w, (M.to_bilin'_apply' _ _).trans $ hv w, matrix.nondegenerate.to_bilin'⟩
theorem _root_.matrix.nondegenerate.to_bilin {M : matrix ι ι R₃} (h : M.nondegenerate)
(b : basis ι R₃ M₃) : (to_bilin b M).nondegenerate :=
(matrix.nondegenerate_to_bilin'_iff_nondegenerate_to_bilin b).mp h.to_bilin'
@[simp] lemma _root_.matrix.nondegenerate_to_bilin_iff {M : matrix ι ι R₃} (b : basis ι R₃ M₃) :
(to_bilin b M).nondegenerate ↔ M.nondegenerate :=
by rw [←matrix.nondegenerate_to_bilin'_iff_nondegenerate_to_bilin,
matrix.nondegenerate_to_bilin'_iff]
-- Lemmas transferring nondegeneracy between a bilinear form and its associated matrix
@[simp] theorem nondegenerate_to_matrix'_iff {B : bilin_form R₃ (ι → R₃)} :
B.to_matrix'.nondegenerate ↔ B.nondegenerate :=
matrix.nondegenerate_to_bilin'_iff.symm.trans $ (matrix.to_bilin'_to_matrix' B).symm ▸ iff.rfl
theorem nondegenerate.to_matrix' {B : bilin_form R₃ (ι → R₃)} (h : B.nondegenerate) :
B.to_matrix'.nondegenerate :=
nondegenerate_to_matrix'_iff.mpr h
@[simp] theorem nondegenerate_to_matrix_iff {B : bilin_form R₃ M₃} (b : basis ι R₃ M₃) :
(to_matrix b B).nondegenerate ↔ B.nondegenerate :=
(matrix.nondegenerate_to_bilin_iff b).symm.trans $ (matrix.to_bilin_to_matrix b B).symm ▸ iff.rfl
theorem nondegenerate.to_matrix {B : bilin_form R₃ M₃} (h : B.nondegenerate)
(b : basis ι R₃ M₃) : (to_matrix b B).nondegenerate :=
(nondegenerate_to_matrix_iff b).mpr h
-- Some shorthands for combining the above with `matrix.nondegenerate_of_det_ne_zero`
lemma nondegenerate_to_bilin'_iff_det_ne_zero {M : matrix ι ι A} :
M.to_bilin'.nondegenerate ↔ M.det ≠ 0 :=
by rw [matrix.nondegenerate_to_bilin'_iff, matrix.nondegenerate_iff_det_ne_zero]
theorem nondegenerate_to_bilin'_of_det_ne_zero' (M : matrix ι ι A) (h : M.det ≠ 0) :
M.to_bilin'.nondegenerate :=
nondegenerate_to_bilin'_iff_det_ne_zero.mpr h
lemma nondegenerate_iff_det_ne_zero {B : bilin_form A M₃}
(b : basis ι A M₃) : B.nondegenerate ↔ (to_matrix b B).det ≠ 0 :=
by rw [←matrix.nondegenerate_iff_det_ne_zero, nondegenerate_to_matrix_iff]
theorem nondegenerate_of_det_ne_zero (b : basis ι A M₃) (h : (to_matrix b B₃).det ≠ 0) :
B₃.nondegenerate :=
(nondegenerate_iff_det_ne_zero b).mpr h
end det
end bilin_form
|
a226a7ba868214628619dd627e82d20a143ea576 | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /library/init/data/unsigned.lean | db6da9a2613216034b5c05edca52800e5b1a3098 | [
"Apache-2.0"
] | permissive | bre7k30/lean | de893411bcfa7b3c5572e61b9e1c52951b310aa4 | 5a924699d076dab1bd5af23a8f910b433e598d7a | refs/heads/master | 1,610,900,145,817 | 1,488,006,845,000 | 1,488,006,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 875 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.data.fin.basic
open nat
def unsigned_sz : nat := succ 4294967295
attribute [reducible]
def unsigned := fin unsigned_sz
namespace unsigned
/- We cannot use tactic dec_trivial here because the tactic framework has not been defined yet. -/
private lemma zero_lt_unsigned_sz : 0 < unsigned_sz :=
zero_lt_succ _
def of_nat (n : nat) : unsigned :=
if h : n < unsigned_sz then fin.mk n h else fin.mk 0 zero_lt_unsigned_sz
def to_nat (c : unsigned) : nat :=
fin.val c
def succ (i : unsigned) :=
of_nat i^.to_nat^.succ
end unsigned
instance : has_zero unsigned := ⟨unsigned.of_nat 0⟩
instance : decidable_eq unsigned :=
have decidable_eq (fin unsigned_sz), from fin.decidable_eq _,
this
|
ae22874ba73d306bee11a23a0a28a212074f63be | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/tactic/explode_widget.lean | 10211e6b164d618ec6be2354e02f7a8e618870b9 | [
"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 | 10,211 | lean | /-
Copyright (c) 2020 Minchao Wu. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Minchao Wu
-/
import tactic.explode
import tactic.interactive_expr
/-!
# `#explode_widget` command
Render a widget that displays an `#explode` proof, providing more
interactivity such as jumping to definitions and exploding constants
occurring in the exploded proofs.
-/
open widget tactic tactic.explode
meta instance widget.string_to_html {α} : has_coe string (html α) :=
⟨λ s, s⟩
namespace tactic
namespace explode_widget
open widget_override.interactive_expression
open tagged_format
open widget.html widget.attr
/-- Redefine some of the style attributes for better formatting. -/
meta def get_block_attrs {γ}: sf → tactic (sf × list (attr γ))
| (sf.block i a) := do
let s : attr (γ) := style [
("display", "inline-block"),
("white-space", "pre-wrap"),
("vertical-align", "top")
],
(a,rest) ← get_block_attrs a,
pure (a, s :: rest)
| (sf.highlight c a) := do
(a, rest) ← get_block_attrs a,
pure (a, (cn c.to_string) :: rest)
| a := pure (a,[])
/-- Explode button for subsequent exploding. -/
meta def insert_explode {γ} : expr → tactic (list (html (action γ)))
| (expr.const n _) := (do
pure $ [h "button" [
cn "pointer ba br3 mr1",
on_click (λ _, action.effect $ widget.effect.insert_text ("#explode_widget " ++ n.to_string)),
attr.val "title" "explode"] ["💥"]]
) <|> pure []
| e := pure []
/--
Render a subexpression as a list of html elements.
-/
meta def view {γ} (tooltip_component : tc subexpr (action γ))
(click_address : option expr.address)
(select_address : option expr.address) :
subexpr → sf → tactic (list (html (action γ)))
| ⟨ce, current_address⟩ (sf.tag_expr ea e m) := do
let new_address := current_address ++ ea,
let select_attrs : list (attr (action γ)) :=
if some new_address = select_address then
[className "highlight"] else [],
click_attrs : list (attr (action γ)) ←
if some new_address = click_address then do
content ← tc.to_html tooltip_component (e, new_address),
efmt : string ← format.to_string <$> tactic.pp e,
gd_btn ← goto_def_button e,
epld_btn ← insert_explode e,
pure [tooltip $ h "div" [] [
h "div" [cn "fr"] (gd_btn ++ epld_btn ++ [
h "button" [cn "pointer ba br3 mr1", on_click
(λ _, action.effect $ widget.effect.copy_text efmt),
attr.val "title" "copy expression to clipboard"] ["📋"],
h "button" [cn "pointer ba br3", on_click
(λ _, action.on_close_tooltip),
attr.val "title" "close"] ["×"]
]),
content
]]
else pure [],
(m, block_attrs) ← get_block_attrs m,
let as := [className "expr-boundary", key (ea)] ++ select_attrs ++
click_attrs ++ block_attrs,
inner ← view (e,new_address) m,
pure [h "span" as inner]
| ca (sf.compose x y) := pure (++) <*> view ca x <*> view ca y
| ca (sf.of_string s) := pure
[h "span" [
on_mouse_enter (λ _, action.on_mouse_enter ca),
on_click (λ _, action.on_click ca),
key s
] [html.of_string s]]
| ca b@(sf.block _ _) := do
(a, attrs) ← get_block_attrs b,
inner ← view ca a,
pure [h "span" attrs inner]
| ca b@(sf.highlight _ _) := do
(a, attrs) ← get_block_attrs b,
inner ← view ca a,
pure [h "span" attrs inner]
/-- Make an interactive expression. -/
meta def mk {γ} (tooltip : tc subexpr γ) : tc expr γ :=
let tooltip_comp :=
component.with_should_update
(λ (x y : tactic_state × expr × expr.address), x.2.2 ≠ y.2.2)
$ component.map_action (action.on_tooltip_action) tooltip in
component.filter_map_action
(λ _ (a : γ ⊕ widget.effect), sum.cases_on a some (λ _, none))
$ component.with_effects (λ _ (a : γ ⊕ widget.effect),
match a with
| (sum.inl g) := []
| (sum.inr s) := [s]
end
)
$ tc.mk_simple
(action γ)
(option subexpr × option subexpr)
(λ e, pure $ (none, none))
(λ e ⟨ca, sa⟩ act, pure $
match act with
| (action.on_mouse_enter ⟨e, ea⟩) := ((ca, some (e, ea)), none)
| (action.on_mouse_leave_all) := ((ca, none), none)
| (action.on_click ⟨e, ea⟩) := if some (e,ea) = ca then
((none, sa), none) else
((some (e, ea), sa), none)
| (action.on_tooltip_action g) := ((none, sa), some $ sum.inl g)
| (action.on_close_tooltip) := ((none, sa), none)
| (action.effect e) := ((ca,sa), some $ sum.inr $ e)
end
)
(λ e ⟨ca, sa⟩, do
m ← sf.of_eformat <$> tactic.pp_tagged e,
let m := m.elim_part_apps,
let m := m.flatten,
let m := m.tag_expr [] e,
v ← view tooltip_comp (prod.snd <$> ca) (prod.snd <$> sa) ⟨e, []⟩ m,
pure $
[ h "span" [
className "expr",
key e.hash,
on_mouse_leave (λ _, action.on_mouse_leave_all) ] $ v
]
)
/-- Render the implicit arguments for an expression in fancy, little pills. -/
meta def implicit_arg_list (tooltip : tc subexpr empty) (e : expr) : tactic $ html empty := do
fn ← (mk tooltip) $ expr.get_app_fn e,
args ← list.mmap (mk tooltip) $ expr.get_app_args e,
pure $ h "div" []
( (h "span" [className "bg-blue br3 ma1 ph2 white"] [fn]) ::
list.map (λ a, h "span" [className "bg-gray br3 ma1 ph2 white"] [a]) args
)
/--
Component for the type tooltip.
-/
meta def type_tooltip : tc subexpr empty :=
tc.stateless (λ ⟨e,ea⟩, do
y ← tactic.infer_type e,
y_comp ← mk type_tooltip y,
implicit_args ← implicit_arg_list type_tooltip e,
pure [h "div" [style [("minWidth", "12rem")]] [
h "div" [cn "pl1"] [y_comp],
h "hr" [] [],
implicit_args
]
]
)
/--
Component that shows a type.
-/
meta def show_type_component : tc expr empty :=
tc.stateless (λ x, do
y ← infer_type x,
y_comp ← mk type_tooltip $ y,
pure y_comp
)
/--
Component that shows a constant.
-/
meta def show_constant_component : tc expr empty :=
tc.stateless (λ x, do
y_comp ← mk type_tooltip x,
pure y_comp
)
/--
Search for an entry that has the specified line number.
-/
meta def lookup_lines : entries → nat → entry
| ⟨_, []⟩ n := ⟨default _, 0, 0, status.sintro, thm.string "", []⟩
| ⟨rb, (hd::tl)⟩ n := if hd.line = n then hd else lookup_lines ⟨rb, tl⟩ n
/--
Render a row that shows a goal.
-/
meta def goal_row (e : expr) (show_expr := tt): tactic (list (html empty)) :=
do t ← explode_widget.show_type_component e,
return $ [h "td" [cn "ba bg-dark-green tc"] "Goal",
h "td" [cn "ba tc"]
(if show_expr then [html.of_name e.local_pp_name, " : ", t] else t)]
/--
Render a row that shows the ID of a goal.
-/
meta def id_row {γ} (l : nat): tactic (list (html γ)) :=
return $ [h "td" [cn "ba bg-dark-green tc"] "ID",
h "td" [cn "ba tc"] (to_string l)]
/--
Render a row that shows the rule or theorem being applied.
-/
meta def rule_row : thm → tactic (list (html empty))
| (thm.expr e) := do t ← explode_widget.show_constant_component e,
return $ [h "td" [cn "ba bg-dark-green tc"] "Rule",
h "td" [cn "ba tc"] t]
| t := return $ [h "td" [cn "ba bg-dark-green tc"] "Rule",
h "td" [cn "ba tc"] t.to_string]
/--
Render a row that contains the sub-proofs, i.e., the proofs of the
arguments.
-/
meta def proof_row {γ} (args : list (html γ)): list (html γ) :=
[h "td" [cn "ba bg-dark-green tc"] "Proofs", h "td" [cn "ba tc"]
[h "details" [] $
(h "summary"
[attr.style [("color", "orange")]]
"Details")::args]
]
/--
Combine the goal row, id row, rule row and proof row to make them a table.
-/
meta def assemble_table {γ} (gr ir rr) : list (html γ) → html γ
| [] :=
h "table" [cn "collapse"]
[h "tbody" []
[h "tr" [] gr, h "tr" [] ir, h "tr" [] rr]
]
| pr :=
h "table" [cn "collapse"]
[h "tbody" []
[h "tr" [] gr, h "tr" [] ir, h "tr" [] rr, h "tr" [] pr]
]
/--
Render a table for a given entry.
-/
meta def assemble (es : entries): entry → tactic (html empty)
| ⟨e, l, d, status.sintro, t, ref⟩ := do
gr ← goal_row e, ir ← id_row l, rr ← rule_row $ thm.string "Assumption",
return $ assemble_table gr ir rr []
| ⟨e, l, d, status.intro, t, ref⟩ := do
gr ← goal_row e, ir ← id_row l, rr ← rule_row $ thm.string "Assumption",
return $ assemble_table gr ir rr []
| ⟨e, l, d, st, t, ref⟩ := do
gr ← goal_row e ff, ir ← id_row l, rr ← rule_row t,
let el : list entry := list.map (lookup_lines es) ref,
ls ← monad.mapm assemble el,
let pr := proof_row $ ls.intersperse (h "br" [] []),
return $ assemble_table gr ir rr pr
/--
Render a widget from given entries.
-/
meta def explode_component (es : entries) : tactic (html empty) :=
let concl := lookup_lines es (es.l.length - 1) in assemble es concl
/--
Explode a theorem and return entries.
-/
meta def explode_entries (n : name) (hide_non_prop := tt) : tactic entries :=
do expr.const n _ ← resolve_name n | fail "cannot resolve name",
d ← get_decl n,
v ← match d with
| (declaration.defn _ _ _ v _ _) := return v
| (declaration.thm _ _ _ v) := return v.get
| _ := fail "not a definition"
end,
t ← pp d.type,
explode_expr v hide_non_prop
end explode_widget
open lean lean.parser interactive explode_widget
/--
User command of the explode widget.
-/
@[user_command]
meta def explode_widget_cmd (_ : parse $ tk "#explode_widget") : lean.parser unit :=
do ⟨li,co⟩ ← cur_pos,
n ← ident,
es ← explode_entries n,
comp ← parser.of_tactic (do html ← explode_component es,
c ← pure $ component.stateless (λ _, [html]),
pure $ component.ignore_props $ component.ignore_action $ c),
save_widget ⟨li, co - "#explode_widget".length - 1⟩ comp,
trace "successfully rendered widget",
skip
.
end tactic
|
eaa9813ea70cf4471707e69fceed7738637faee4 | 76df16d6c3760cb415f1294caee997cc4736e09b | /lean/src/cs/util.lean | 7d2fc5297a19cd79741fd3b476259eb9c0264ed3 | [
"MIT"
] | permissive | uw-unsat/leanette-popl22-artifact | 70409d9cbd8921d794d27b7992bf1d9a4087e9fe | 80fea2519e61b45a283fbf7903acdf6d5528dbe7 | refs/heads/master | 1,681,592,449,670 | 1,637,037,431,000 | 1,637,037,431,000 | 414,331,908 | 6 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 4,361 | lean | import tactic.basic
import tactic.omega
import tactic.linarith
import tactic.apply_fun
import data.option.basic
namespace sym
lemma bool.tt_eq_true {b : bool} :
(b = tt) ↔ b := by { simp only [iff_self], }
lemma nat.lt2_implies_01 {j : ℕ} : j < 2 → (j = 0 ∨ j = 1) := by omega
lemma nat.le_and_ne_is_lt {i j : ℕ} : i ≠ j → i ≤ j → i < j := by { intros h1 hs, omega, }
lemma nat.lt_sub_pos {j n : ℕ} : j < n → 0 < n - j := by { omega, }
lemma nat.min_of_lt {k n : ℕ} : k < n → min k n = k :=
begin
intro h,
simp only [min, not_le, ite_eq_left_iff],
intro h',
linarith,
end
lemma list.map_bound {α1 α2 β}
{l1 : list α1} {l2 : list α2} {f1 : α1 → β} {f2 : α2 → β} {i : ℕ} :
l1.map f1 = l2.map f2 → i < l1.length → i < l2.length :=
begin
intro h,
apply_fun list.length at h,
simp only [list.length_map] at h,
cc,
end
lemma list.forall_mem_iff_forall_nth_le {α} {xs : list α} {p : α → Prop} :
(∀ (x : α), x ∈ xs → p x) ↔
(∀ (i : ℕ) (hi : i < xs.length), p (xs.nth_le i hi)) :=
begin
constructor; intro h,
{ intros i hi,
specialize h (xs.nth_le i hi) (list.nth_le_mem xs i hi),
exact h, },
{ intros x hx,
simp only [list.mem_iff_nth_le] at hx,
cases hx with n hn, cases hn with hn hmem,
specialize h n hn,
rewrite ←hmem,
exact h, }
end
lemma list.filter_length_ge_one {α} {xs : list α} {x : α} {p : α → Prop} [decidable_pred p] :
x ∈ xs → p x → 1 ≤ (xs.filter p).length :=
begin
intros h1 h2,
have h3 : x ∈ (xs.filter p) := by { simp only [h1, h2, list.mem_filter, and_self], },
have h4 : 0 < (xs.filter p).length := (list.length_pos_of_mem h3),
linarith,
end
lemma list.filter_length_gt_one_aux {α} {xs : list α} {i j : ℕ} {p : α → Prop} [decidable_pred p]
(hi : i < xs.length) (hj : j < xs.length) :
i < j → p (xs.nth_le i hi) → p (xs.nth_le j hj) →
(xs.filter p).length > 1 :=
begin
intros hn hpi hpj,
rcases (list.nth_le_mem xs i hi) with hi_mem,
rcases (list.nth_le_mem xs j hj) with hj_mem,
rcases (list.take_append_drop j xs) with hxs',
rewrite [←hxs', list.filter_append],
simp only [list.length_append, gt_iff_lt],
have hi' : i < (list.take j xs ++ list.drop j xs).length := by { simp only [list.take_append_drop], exact hi, },
have hti : i < (list.take j xs).length := by { simp only [hn, hi, lt_min_iff, list.length_take, and_self], },
rcases (list.nth_le_append hi' hti) with hl,
simp only [list.take_append_drop] at hl,
rcases (list.nth_le_mem (list.take j xs) i hti) with hlm,
rewrite ←hl at hlm,
have hj' : j < (list.take j xs ++ list.drop j xs).length := by { simp only [list.take_append_drop], exact hj, },
have htj : (list.take j xs).length ≤ j := by { simp only [list.length_take, min_le_iff], apply or.inl, refl, },
rcases (list.nth_le_append_right htj hj') with hr,
simp only [list.length_take, list.take_append_drop] at hr,
have hd : j - min j xs.length < (list.drop j xs).length := by {
rewrite nat.min_of_lt hj,
simp only [list.length_drop, nat.sub_self],
apply nat.lt_sub_pos hj, },
rcases (list.nth_le_mem (list.drop j xs) (j - min j xs.length) hd) with hrm,
rewrite ←hr at hrm,
rcases (list.filter_length_ge_one hlm hpi) with ht,
rcases (list.filter_length_ge_one hrm hpj) with hd,
linarith,
end
lemma list.filter_length_gt_one {α} {xs : list α} {i j : ℕ} {p : α → Prop} [decidable_pred p]
(hi : i < xs.length) (hj : j < xs.length) :
i ≠ j → p (xs.nth_le i hi) → p (xs.nth_le j hj) →
(xs.filter p).length > 1 :=
begin
intros hn hpi hpj,
cases classical.em (i < j),
{ apply list.filter_length_gt_one_aux hi hj h hpi hpj, },
{ simp only [not_lt] at h,
replace h : j < i := nat.le_and_ne_is_lt (by {cc}) h,
apply list.filter_length_gt_one_aux hj hi h hpj hpi, }
end
lemma list.filter_map_length_eq {α β} {xs : list α} {f : α → β} {p : β → Prop} [decidable_pred p] :
(list.filter p (list.map f xs)).length = (list.filter (λ x, p (f x)) xs).length :=
by { simp only [list.map_filter, list.length_map], }
lemma list.nth_le_eq_idx {α} (xs : list α) {i j : ℕ} :
(i = j) → ∀ (hi : i < xs.length) (hj : j < xs.length), xs.nth_le i hi = xs.nth_le j hj :=
begin
intros h hi hm,
congr,
exact h,
end
end sym
|
741c5ddf20607fbd881f0eed574d3decc870a5c0 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/algebra/category/Algebra/basic.lean | b0630ea5f999233d8a16cd22a0c518a130301530 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 5,846 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebra.algebra.subalgebra.basic
import algebra.free_algebra
import algebra.category.Ring.basic
import algebra.category.Module.basic
/-!
# Category instance for algebras over a commutative ring
We introduce the bundled category `Algebra` of algebras over a fixed commutative ring `R ` along
with the forgetful functors to `Ring` and `Module`. We furthermore show that the functor associating
to a type the free `R`-algebra on that type is left adjoint to the forgetful functor.
-/
open category_theory
open category_theory.limits
universes v u
variables (R : Type u) [comm_ring R]
/-- The category of R-algebras and their morphisms. -/
structure Algebra :=
(carrier : Type v)
[is_ring : ring carrier]
[is_algebra : algebra R carrier]
attribute [instance] Algebra.is_ring Algebra.is_algebra
namespace Algebra
instance : has_coe_to_sort (Algebra R) (Type v) := ⟨Algebra.carrier⟩
instance : category (Algebra.{v} R) :=
{ hom := λ A B, A →ₐ[R] B,
id := λ A, alg_hom.id R A,
comp := λ A B C f g, g.comp f }
instance : concrete_category.{v} (Algebra.{v} R) :=
{ forget := { obj := λ R, R, map := λ R S f, (f : R → S) },
forget_faithful := { } }
instance has_forget_to_Ring : has_forget₂ (Algebra.{v} R) Ring.{v} :=
{ forget₂ :=
{ obj := λ A, Ring.of A,
map := λ A₁ A₂ f, alg_hom.to_ring_hom f, } }
instance has_forget_to_Module : has_forget₂ (Algebra.{v} R) (Module.{v} R) :=
{ forget₂ :=
{ obj := λ M, Module.of R M,
map := λ M₁ M₂ f, alg_hom.to_linear_map f, } }
/-- The object in the category of R-algebras associated to a type equipped with the appropriate
typeclasses. -/
def of (X : Type v) [ring X] [algebra R X] : Algebra.{v} R := ⟨X⟩
/-- Typecheck a `alg_hom` as a morphism in `Algebra R`. -/
def of_hom {R : Type u} [comm_ring R] {X Y : Type v} [ring X] [algebra R X] [ring Y] [algebra R Y]
(f : X →ₐ[R] Y) : of R X ⟶ of R Y := f
@[simp] lemma of_hom_apply {R : Type u} [comm_ring R]
{X Y : Type v} [ring X] [algebra R X] [ring Y] [algebra R Y] (f : X →ₐ[R] Y) (x : X) :
of_hom f x = f x := rfl
instance : inhabited (Algebra R) := ⟨of R R⟩
@[simp]
lemma coe_of (X : Type u) [ring X] [algebra R X] : (of R X : Type u) = X := rfl
variables {R}
/-- Forgetting to the underlying type and then building the bundled object returns the original
algebra. -/
@[simps]
def of_self_iso (M : Algebra.{v} R) : Algebra.of R M ≅ M :=
{ hom := 𝟙 M, inv := 𝟙 M }
variables {R} {M N U : Module.{v} R}
@[simp] lemma id_apply (m : M) : (𝟙 M : M → M) m = m := rfl
@[simp] lemma coe_comp (f : M ⟶ N) (g : N ⟶ U) :
((f ≫ g) : M → U) = g ∘ f := rfl
variables (R)
/-- The "free algebra" functor, sending a type `S` to the free algebra on `S`. -/
@[simps]
def free : Type u ⥤ Algebra.{u} R :=
{ obj := λ S,
{ carrier := free_algebra R S,
is_ring := algebra.semiring_to_ring R },
map := λ S T f, free_algebra.lift _ $ (free_algebra.ι _) ∘ f,
-- obviously can fill the next two goals, but it is slow
map_id' := by { intros X, ext1, simp only [free_algebra.ι_comp_lift], refl },
map_comp' := by { intros, ext1, simp only [free_algebra.ι_comp_lift], ext1,
simp only [free_algebra.lift_ι_apply, category_theory.coe_comp, function.comp_app,
types_comp_apply] } }
/-- The free/forget adjunction for `R`-algebras. -/
def adj : free.{u} R ⊣ forget (Algebra.{u} R) :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X A, (free_algebra.lift _).symm,
-- Relying on `obviously` to fill out these proofs is very slow :(
hom_equiv_naturality_left_symm' := by { intros, ext,
simp only [free_map, equiv.symm_symm, free_algebra.lift_ι_apply, category_theory.coe_comp,
function.comp_app, types_comp_apply] },
hom_equiv_naturality_right' := by { intros, ext,
simp only [forget_map_eq_coe, category_theory.coe_comp, function.comp_app,
free_algebra.lift_symm_apply, types_comp_apply] } }
instance : is_right_adjoint (forget (Algebra.{u} R)) := ⟨_, adj R⟩
end Algebra
variables {R}
variables {X₁ X₂ : Type u}
/-- Build an isomorphism in the category `Algebra R` from a `alg_equiv` between `algebra`s. -/
@[simps]
def alg_equiv.to_Algebra_iso
{g₁ : ring X₁} {g₂ : ring X₂} {m₁ : algebra R X₁} {m₂ : algebra R X₂} (e : X₁ ≃ₐ[R] X₂) :
Algebra.of R X₁ ≅ Algebra.of R X₂ :=
{ hom := (e : X₁ →ₐ[R] X₂),
inv := (e.symm : X₂ →ₐ[R] X₁),
hom_inv_id' := begin ext, exact e.left_inv x, end,
inv_hom_id' := begin ext, exact e.right_inv x, end, }
namespace category_theory.iso
/-- Build a `alg_equiv` from an isomorphism in the category `Algebra R`. -/
@[simps]
def to_alg_equiv {X Y : Algebra R} (i : X ≅ Y) : X ≃ₐ[R] Y :=
{ to_fun := i.hom,
inv_fun := i.inv,
left_inv := by tidy,
right_inv := by tidy,
map_add' := by tidy,
map_mul' := by tidy,
commutes' := by tidy, }.
end category_theory.iso
/-- Algebra equivalences between `algebras`s are the same as (isomorphic to) isomorphisms in
`Algebra`. -/
@[simps]
def alg_equiv_iso_Algebra_iso {X Y : Type u}
[ring X] [ring Y] [algebra R X] [algebra R Y] :
(X ≃ₐ[R] Y) ≅ (Algebra.of R X ≅ Algebra.of R Y) :=
{ hom := λ e, e.to_Algebra_iso,
inv := λ i, i.to_alg_equiv, }
instance (X : Type u) [ring X] [algebra R X] : has_coe (subalgebra R X) (Algebra R) :=
⟨ λ N, Algebra.of R N ⟩
instance Algebra.forget_reflects_isos : reflects_isomorphisms (forget (Algebra.{u} R)) :=
{ reflects := λ X Y f _,
begin
resetI,
let i := as_iso ((forget (Algebra.{u} R)).map f),
let e : X ≃ₐ[R] Y := { ..f, ..i.to_equiv },
exact ⟨(is_iso.of_iso e.to_Algebra_iso).1⟩,
end }
|
ab030318b05c95aa53fbb2673f13d6be90c4ccde | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/ns.lean | 61f088dd26fcc176256aee1789bc73a1536d7b71 | [
"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 | 282 | lean | prelude
constant nat : Type.{1}
constant f : nat → nat
namespace foo
constant int : Type.{1}
constant f : int → int
constant a : nat
constant i : int
check _root_.f a
check f i
end foo
open foo
constants a : nat
constants i : int
check f foo.a
check f foo.i
|
8827872586f4d929d1b1416a126d78f23dcaadaa | 271e26e338b0c14544a889c31c30b39c989f2e0f | /stage0/src/Init/Lean/Parser/Term.lean | e24e5e93b83da3c50a99177e78cb9c4da8125c87 | [
"Apache-2.0"
] | permissive | dgorokho/lean4 | 805f99b0b60c545b64ac34ab8237a8504f89d7d4 | e949a052bad59b1c7b54a82d24d516a656487d8a | refs/heads/master | 1,607,061,363,851 | 1,578,006,086,000 | 1,578,006,086,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,689 | 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, Sebastian Ullrich
-/
prelude
import Init.Lean.Parser.Parser
import Init.Lean.Parser.Level
namespace Lean
namespace Parser
namespace Term
/- Helper functions for defining simple parsers -/
def unicodeInfixR (sym : String) (asciiSym : String) (lbp : Nat) : TrailingParser :=
pushLeading >> unicodeSymbol sym asciiSym lbp >> termParser (lbp - 1)
def infixR (sym : String) (lbp : Nat) : TrailingParser :=
pushLeading >> symbol sym lbp >> termParser (lbp - 1)
def unicodeInfixL (sym : String) (asciiSym : String) (lbp : Nat) : TrailingParser :=
pushLeading >> unicodeSymbol sym asciiSym lbp >> termParser lbp
def infixL (sym : String) (lbp : Nat) : TrailingParser :=
pushLeading >> symbol sym lbp >> termParser lbp
/- Built-in parsers -/
-- NOTE: `checkNoWsBefore` should be used *before* `parser!` so that it is also applied to the generated
-- antiquotation.
def explicitUniv := checkNoWsBefore "no space before '.{'" >> parser! ".{" >> sepBy1 levelParser ", " >> "}"
def namedPattern := checkNoWsBefore "no space before '@'" >> parser! "@" >> termParser appPrec
@[builtinTermParser] def id := parser! ident >> optional (explicitUniv <|> namedPattern)
@[builtinTermParser] def num : Parser := parser! numLit
@[builtinTermParser] def str : Parser := parser! strLit
@[builtinTermParser] def char : Parser := parser! charLit
@[builtinTermParser] def type := parser! symbol "Type" appPrec
@[builtinTermParser] def sort := parser! symbol "Sort" appPrec
@[builtinTermParser] def prop := parser! symbol "Prop" appPrec
@[builtinTermParser] def hole := parser! symbol "_" appPrec
@[builtinTermParser] def «sorry» := parser! symbol "sorry" appPrec
@[builtinTermParser] def cdot := parser! symbol "·" appPrec
@[builtinTermParser] def emptyC := parser! symbol "∅" appPrec
def typeAscription := parser! " : " >> termParser
def tupleTail := parser! ", " >> sepBy1 termParser ", "
def parenSpecial : Parser := optional (tupleTail <|> typeAscription)
@[builtinTermParser] def paren := parser! symbol "(" appPrec >> optional (termParser >> parenSpecial) >> ")"
@[builtinTermParser] def anonymousCtor := parser! symbol "⟨" appPrec >> sepBy termParser ", " >> "⟩"
def optIdent : Parser := optional (try (ident >> " : "))
@[builtinTermParser] def «if» := parser! "if " >> optIdent >> termParser >> " then " >> termParser >> " else " >> termParser
def fromTerm := parser! " from " >> termParser
def haveAssign := parser! " := " >> termParser
@[builtinTermParser] def «have» := parser! "have " >> optIdent >> termParser >> (haveAssign <|> fromTerm) >> "; " >> termParser
@[builtinTermParser] def «suffices» := parser! "suffices " >> optIdent >> termParser >> fromTerm >> "; " >> termParser
@[builtinTermParser] def «show» := parser! "show " >> termParser >> fromTerm
@[builtinTermParser] def «fun» := parser! unicodeSymbol "λ" "fun" >> many1 (termParser appPrec) >> unicodeSymbol "⇒" "=>" >> termParser
def structInstField := parser! ident >> " := " >> termParser
def structInstSource := parser! ".." >> optional termParser
@[builtinTermParser] def structInst := parser! symbol "{" appPrec >> optional (try (ident >> " . ")) >> sepBy (structInstField <|> structInstSource) ", " true >> "}"
def typeSpec := parser! " : " >> termParser
def optType : Parser := optional typeSpec
@[builtinTermParser] def subtype := parser! "{" >> ident >> optType >> " // " >> termParser >> "}"
@[builtinTermParser] def listLit := parser! symbol "[" appPrec >> sepBy termParser "," true >> "]"
@[builtinTermParser] def arrayLit := parser! symbol "#[" appPrec >> sepBy termParser "," true >> "]"
@[builtinTermParser] def explicit := parser! symbol "@" appPrec >> id
@[builtinTermParser] def inaccessible := parser! symbol ".(" appPrec >> termParser >> ")"
def binderIdent : Parser := ident <|> hole
def binderType (requireType := false) : Parser := if requireType then group (" : " >> termParser) else optional (" : " >> termParser)
def binderDefault := parser! " := " >> termParser
def binderTactic := parser! " . " >> termParser
def explicitBinder (requireType := false) := parser! "(" >> many1 binderIdent >> binderType requireType >> optional (binderDefault <|> binderTactic) >> ")"
def implicitBinder (requireType := false) := parser! "{" >> many1 binderIdent >> binderType requireType >> "}"
def instBinder := parser! "[" >> optIdent >> termParser >> "]"
def bracktedBinder (requireType := false) := explicitBinder requireType <|> implicitBinder requireType <|> instBinder
@[builtinTermParser] def depArrow := parser! bracktedBinder true >> unicodeSymbolCheckPrec " → " " -> " 25 >> termParser
def simpleBinder := parser! many1 binderIdent
@[builtinTermParser] def «forall» := parser! unicodeSymbol "∀" "forall" >> many1 (simpleBinder <|> bracktedBinder) >> ", " >> termParser
def matchAlt := parser! " | " >> sepBy1 termParser ", " >> unicodeSymbol "⇒" "=>" >> termParser
@[builtinTermParser] def «match» := parser! "match " >> sepBy1 termParser ", " >> optType >> " with " >> many1Indent matchAlt "'match' alternatives must be indented"
@[builtinTermParser] def «nomatch» := parser! "nomatch " >> termParser
@[builtinTermParser] def «parser!» := parser! "parser! " >> termParser
@[builtinTermParser] def «tparser!» := parser! "tparser! " >> termParser
@[builtinTermParser] def borrowed := parser! symbol "@&" appPrec >> termParser (appPrec - 1)
@[builtinTermParser] def quotedName := parser! symbol "`" appPrec >> rawIdent
@[builtinTermParser] def stxQuot := parser! symbol "`(" appPrec >> termParser >> ")"
@[builtinTermParser] def antiquot := (mkAntiquot "term" none true : Parser)
@[builtinTermParser] def «match_syntax» := parser! "match_syntax" >> termParser >> " with " >> many1Indent matchAlt "'match_syntax' alternatives must be indented"
/- Remark: we use `checkWsBefore` to ensure `let x[i] := e; b` is not parsed as `let x [i] := e; b` where `[i]` is an `instBinder`. -/
def letIdLhs : Parser := ident >> checkWsBefore "expected space before binders" >> many bracktedBinder >> optType
def letIdDecl := parser! try (letIdLhs >> " := ") >> termParser
def equation := matchAlt
def letEqns := parser! try (letIdLhs >> lookahead " | ") >> many1Indent equation "equations must be indented"
def letPatDecl := parser! termParser >> optType >> " := " >> termParser
def letDecl := try letIdDecl <|> letEqns <|> letPatDecl
@[builtinTermParser] def «let» := parser! "let " >> letDecl >> "; " >> termParser
def leftArrow : Parser := unicodeSymbol " ← " " <- "
def doLet := parser! "let " >> letDecl
def doId := parser! try (ident >> optType >> leftArrow) >> termParser
def doPat := parser! try (termParser >> leftArrow) >> termParser >> optional (" | " >> termParser)
def doExpr := parser! termParser
def doElem := doLet <|> doId <|> doPat <|> doExpr
def doSeq := parser! sepBy1 doElem "; "
def bracketedDoSeq := parser! "{" >> doSeq >> "}"
@[builtinTermParser] def «do» := parser! "do " >> (bracketedDoSeq <|> doSeq)
@[builtinTermParser] def not := parser! symbol "¬" 40 >> termParser 40
@[builtinTermParser] def bnot := parser! symbol "!" 40 >> termParser 40
@[builtinTermParser] def uminus := parser! "-" >> termParser 100
def namedArgument := tparser! try ("(" >> ident >> " := ") >> termParser >> ")"
@[builtinTermParser] def app := tparser! pushLeading >> (namedArgument <|> termParser appPrec)
def checkIsSort := checkLeading (fun leading => leading.isOfKind `Lean.Parser.Term.type || leading.isOfKind `Lean.Parser.Term.sort)
@[builtinTermParser] def sortApp := tparser! checkIsSort >> pushLeading >> levelParser appPrec
@[builtinTermParser] def proj := tparser! pushLeading >> symbolNoWs "." (appPrec+1) >> (fieldIdx <|> ident)
@[builtinTermParser] def arrow := tparser! unicodeInfixR " → " " -> " 25
@[builtinTermParser] def arrayRef := tparser! pushLeading >> symbolNoWs "[" (appPrec+1) >> termParser >>"]"
@[builtinTermParser] def dollar := tparser! try (pushLeading >> dollarSymbol >> checkWsBefore "space expected") >> termParser 0
@[builtinTermParser] def dollarProj := tparser! pushLeading >> symbol "$." 1 >> (fieldIdx <|> ident)
@[builtinTermParser] def «where» := tparser! pushLeading >> symbol " where " 1 >> sepBy1 (toTrailing letDecl) (group ("; " >> " where "))
@[builtinTermParser] def fcomp := tparser! infixR " ∘ " 90
@[builtinTermParser] def prod := tparser! infixR " × " 35
@[builtinTermParser] def add := tparser! infixL " + " 65
@[builtinTermParser] def sub := tparser! infixL " - " 65
@[builtinTermParser] def mul := tparser! infixL " * " 70
@[builtinTermParser] def div := tparser! infixL " / " 70
@[builtinTermParser] def mod := tparser! infixL " % " 70
@[builtinTermParser] def modN := tparser! infixL " %ₙ " 70
@[builtinTermParser] def pow := tparser! infixR " ^ " 80
@[builtinTermParser] def le := tparser! unicodeInfixL " ≤ " " <= " 50
@[builtinTermParser] def ge := tparser! unicodeInfixL " ≥ " " >= " 50
@[builtinTermParser] def lt := tparser! infixL " < " 50
@[builtinTermParser] def gt := tparser! infixL " > " 50
@[builtinTermParser] def eq := tparser! infixL " = " 50
@[builtinTermParser] def ne := tparser! infixL " ≠ " 50
@[builtinTermParser] def beq := tparser! infixL " == " 50
@[builtinTermParser] def bne := tparser! infixL " != " 50
@[builtinTermParser] def heq := tparser! unicodeInfixL " ≅ " " ~= " 50
@[builtinTermParser] def equiv := tparser! infixL " ≈ " 50
@[builtinTermParser] def subst := tparser! infixR " ▸ " 75
@[builtinTermParser] def and := tparser! unicodeInfixR " ∧ " " /\\ " 35
@[builtinTermParser] def or := tparser! unicodeInfixR " ∨ " " \\/ " 30
@[builtinTermParser] def iff := tparser! unicodeInfixL " ↔ " " <-> " 20
@[builtinTermParser] def band := tparser! infixL " && " 35
@[builtinTermParser] def bor := tparser! infixL " || " 30
@[builtinTermParser] def append := tparser! infixL " ++ " 65
@[builtinTermParser] def cons := tparser! infixR " :: " 67
@[builtinTermParser] def orelse := tparser! infixR " <|> " 2
@[builtinTermParser] def orM := tparser! infixR " <||> " 30
@[builtinTermParser] def andM := tparser! infixR " <&&> " 35
@[builtinTermParser] def andthen := tparser! infixR " >> " 60
@[builtinTermParser] def bindOp := tparser! infixR " >>= " 55
@[builtinTermParser] def mapRev := tparser! infixR " <&> " 100
@[builtinTermParser] def seq := tparser! infixL " <*> " 60
@[builtinTermParser] def seqLeft := tparser! infixL " <* " 60
@[builtinTermParser] def seqRight := tparser! infixR " *> " 60
@[builtinTermParser] def map := tparser! infixR " <$> " 100
@[builtinTermParser] def mapConst := tparser! infixR " <$ " 100
@[builtinTermParser] def mapConstRev := tparser! infixR " $> " 100
end Term
end Parser
open Parser
def mkAppStx (fn : Syntax) (args : Array Syntax) : Syntax :=
args.foldl (fun fn arg => Syntax.node `Lean.Parser.Term.app #[fn, arg]) fn
def Syntax.isTermId? (stx : Syntax) : Option (Syntax × Syntax) :=
stx.ifNode
(fun node =>
if node.getKind == `Lean.Parser.Term.id && node.getNumArgs == 2 then
some (node.getArg 0, node.getArg 1)
else
none)
(fun _ => none)
def Syntax.isSimpleTermId? (stx : Syntax) : Option Syntax :=
stx.ifNode
(fun node =>
if node.getKind == `Lean.Parser.Term.id && node.getNumArgs == 2 && (node.getArg 1).isNone then
some (node.getArg 0)
else
none)
(fun _ => none)
end Lean
|
17ee5b53642cf793791d71c15cd99a65409200a0 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/limits/constructions/epi_mono.lean | fc6d3c454c6fb4d8fbea66fe49b7ad41b97546e4 | [] | 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 | 1,283 | lean | /-
Copyright (c) 2021 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.limits.shapes.pullbacks
import Mathlib.category_theory.limits.shapes.binary_products
import Mathlib.category_theory.limits.preserves.shapes.pullbacks
import Mathlib.PostPort
universes v u₁ u₂
namespace Mathlib
/-!
# Relating monomorphisms and epimorphisms to limits and colimits
If `F` preserves (resp. reflects) pullbacks, then it preserves (resp. reflects) monomorphisms.
## TODO
Dualise and apply to functor categories.
-/
namespace category_theory
/-- If `F` preserves pullbacks, then it preserves monomorphisms. -/
protected instance preserves_mono {C : Type u₁} {D : Type u₂} [category C] [category D] (F : C ⥤ D) {X : C} {Y : C} (f : X ⟶ Y) [limits.preserves_limit (limits.cospan f f) F] [mono f] : mono (functor.map F f) := sorry
/-- If `F` reflects pullbacks, then it reflects monomorphisms. -/
theorem reflects_mono {C : Type u₁} {D : Type u₂} [category C] [category D] (F : C ⥤ D) {X : C} {Y : C} (f : X ⟶ Y) [limits.reflects_limit (limits.cospan f f) F] [mono (functor.map F f)] : mono f := sorry
|
0c5c72826fcf270c70cf07c4257e2b00affa9e4d | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/order/rel_iso_auto.lean | 1a2b95b4b7288cd2ebb931de60dc731a09c0a01d | [] | 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 | 41,259 | 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 Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.logic.embedding
import Mathlib.order.rel_classes
import Mathlib.data.set.intervals.basic
import Mathlib.PostPort
universes u_4 u_5 l u_1 u_2 u_3
namespace Mathlib
/-- A relation homomorphism with respect to a given pair of relations `r` and `s`
is a function `f : α → β` such that `r a b → s (f a) (f b)`. -/
structure rel_hom {α : Type u_4} {β : Type u_5} (r : α → α → Prop) (s : β → β → Prop) where
to_fun : α → β
map_rel' : ∀ {a b : α}, r a b → s (to_fun a) (to_fun b)
infixl:25 " →r " => Mathlib.rel_hom
namespace rel_hom
protected instance has_coe_to_fun {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} : has_coe_to_fun (r →r s) :=
has_coe_to_fun.mk (fun (_x : r →r s) => α → β) fun (o : r →r s) => to_fun o
theorem map_rel {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} (f : r →r s)
{a : α} {b : α} : r a b → s (coe_fn f a) (coe_fn f b) :=
map_rel' f
@[simp] theorem coe_fn_mk {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : α → β) (o : ∀ {a b : α}, r a b → s (f a) (f b)) : ⇑(mk f o) = f :=
rfl
@[simp] theorem coe_fn_to_fun {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r →r s) : to_fun f = ⇑f :=
rfl
/-- The map `coe_fn : (r →r s) → (α → β)` is injective. We can't use `function.injective`
here but mimic its signature by using `⦃e₁ e₂⦄`. -/
theorem coe_fn_inj {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} {e₁ : r →r s}
{e₂ : r →r s} : ⇑e₁ = ⇑e₂ → e₁ = e₂ :=
sorry
theorem ext {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} {f : r →r s}
{g : r →r s} (h : ∀ (x : α), coe_fn f x = coe_fn g x) : f = g :=
coe_fn_inj (funext h)
theorem ext_iff {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} {f : r →r s}
{g : r →r s} : f = g ↔ ∀ (x : α), coe_fn f x = coe_fn g x :=
{ mp := fun (h : f = g) (x : α) => h ▸ rfl,
mpr := fun (h : ∀ (x : α), coe_fn f x = coe_fn g x) => ext h }
/-- Identity map is a relation homomorphism. -/
protected def id {α : Type u_1} (r : α → α → Prop) : r →r r := mk id sorry
/-- Composition of two relation homomorphisms is a relation homomorphism. -/
protected def comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} {r : α → α → Prop}
{s : β → β → Prop} {t : γ → γ → Prop} (g : s →r t) (f : r →r s) : r →r t :=
mk (to_fun g ∘ to_fun f) sorry
@[simp] theorem id_apply {α : Type u_1} {r : α → α → Prop} (x : α) : coe_fn (rel_hom.id r) x = x :=
rfl
@[simp] theorem comp_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} {r : α → α → Prop}
{s : β → β → Prop} {t : γ → γ → Prop} (g : s →r t) (f : r →r s) (a : α) :
coe_fn (rel_hom.comp g f) a = coe_fn g (coe_fn f a) :=
rfl
/-- A relation homomorphism is also a relation homomorphism between dual relations. -/
protected def swap {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r →r s) : function.swap r →r function.swap s :=
mk ⇑f sorry
/-- A function is a relation homomorphism from the preimage relation of `s` to `s`. -/
def preimage {α : Type u_1} {β : Type u_2} (f : α → β) (s : β → β → Prop) : f ⁻¹'o s →r s :=
mk f sorry
protected theorem is_irrefl {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r →r s) [is_irrefl β s] : is_irrefl α r :=
sorry
protected theorem is_asymm {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r →r s) [is_asymm β s] : is_asymm α r :=
sorry
protected theorem acc {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r →r s) (a : α) : acc s (coe_fn f a) → acc r a :=
sorry
protected theorem well_founded {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r →r s) (h : well_founded s) : well_founded r :=
well_founded.dcases_on h
fun (h : ∀ (a : β), acc s a) =>
idRhs (well_founded r) (well_founded.intro fun (a : α) => rel_hom.acc f a (h (coe_fn f a)))
theorem map_inf {α : Type u_1} {β : Type u_2} [semilattice_inf α] [linear_order β]
(a : Less →r Less) (m : β) (n : β) : coe_fn a (m ⊓ n) = coe_fn a m ⊓ coe_fn a n :=
sorry
theorem map_sup {α : Type u_1} {β : Type u_2} [semilattice_sup α] [linear_order β] (a : gt →r gt)
(m : β) (n : β) : coe_fn a (m ⊔ n) = coe_fn a m ⊔ coe_fn a n :=
sorry
end rel_hom
/-- An increasing function is injective -/
theorem injective_of_increasing {α : Type u_1} {β : Type u_2} (r : α → α → Prop) (s : β → β → Prop)
[is_trichotomous α r] [is_irrefl β s] (f : α → β) (hf : ∀ {x y : α}, r x y → s (f x) (f y)) :
function.injective f :=
sorry
/-- An increasing function is injective -/
theorem rel_hom.injective_of_increasing {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} [is_trichotomous α r] [is_irrefl β s] (f : r →r s) : function.injective ⇑f :=
injective_of_increasing r s ⇑f fun (x y : α) => rel_hom.map_rel f
theorem surjective.well_founded_iff {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} {f : α → β} (hf : function.surjective f)
(o : ∀ {a b : α}, r a b ↔ s (f a) (f b)) : well_founded r ↔ well_founded s :=
sorry
/-- A relation embedding with respect to a given pair of relations `r` and `s`
is an embedding `f : α ↪ β` such that `r a b ↔ s (f a) (f b)`. -/
structure rel_embedding {α : Type u_4} {β : Type u_5} (r : α → α → Prop) (s : β → β → Prop)
extends α ↪ β where
map_rel_iff' : ∀ {a b : α}, s (coe_fn _to_embedding a) (coe_fn _to_embedding b) ↔ r a b
infixl:25 " ↪r " => Mathlib.rel_embedding
/-- An order embedding is an embedding `f : α ↪ β` such that `a ≤ b ↔ (f a) ≤ (f b)`.
This definition is an abbreviation of `rel_embedding (≤) (≤)`. -/
def order_embedding (α : Type u_1) (β : Type u_2) [HasLessEq α] [HasLessEq β] := LessEq ↪r LessEq
infixl:25 " ↪o " => Mathlib.order_embedding
/-- The induced relation on a subtype is an embedding under the natural inclusion. -/
def subtype.rel_embedding {X : Type u_1} (r : X → X → Prop) (p : X → Prop) :
subtype.val ⁻¹'o r ↪r r :=
rel_embedding.mk (function.embedding.subtype p) sorry
theorem preimage_equivalence {α : Sort u_1} {β : Sort u_2} (f : α → β) {s : β → β → Prop}
(hs : equivalence s) : equivalence (f ⁻¹'o s) :=
sorry
namespace rel_embedding
/-- A relation embedding is also a relation homomorphism -/
def to_rel_hom {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} (f : r ↪r s) :
r →r s :=
rel_hom.mk (function.embedding.to_fun (to_embedding f)) sorry
-- see Note [function coercion]
protected instance rel_hom.has_coe {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} : has_coe (r ↪r s) (r →r s) :=
has_coe.mk to_rel_hom
protected instance has_coe_to_fun {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} : has_coe_to_fun (r ↪r s) :=
has_coe_to_fun.mk (fun (_x : r ↪r s) => α → β) fun (o : r ↪r s) => ⇑(to_embedding o)
@[simp] theorem to_rel_hom_eq_coe {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (f : r ↪r s) : to_rel_hom f = ↑f :=
rfl
@[simp] theorem coe_coe_fn {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) : ⇑↑f = ⇑f :=
rfl
theorem injective {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} (f : r ↪r s) :
function.injective ⇑f :=
function.embedding.inj' (to_embedding f)
theorem map_rel_iff {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} (f : r ↪r s)
{a : α} {b : α} : s (coe_fn f a) (coe_fn f b) ↔ r a b :=
map_rel_iff' f
@[simp] theorem coe_fn_mk {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : α ↪ β) (o : ∀ {a b : α}, s (coe_fn f a) (coe_fn f b) ↔ r a b) : ⇑(mk f o) = ⇑f :=
rfl
@[simp] theorem coe_fn_to_embedding {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (f : r ↪r s) : ⇑(to_embedding f) = ⇑f :=
rfl
/-- The map `coe_fn : (r ↪r s) → (α → β)` is injective. We can't use `function.injective`
here but mimic its signature by using `⦃e₁ e₂⦄`. -/
theorem coe_fn_inj {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} {e₁ : r ↪r s}
{e₂ : r ↪r s} : ⇑e₁ = ⇑e₂ → e₁ = e₂ :=
sorry
theorem ext {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} {f : r ↪r s}
{g : r ↪r s} (h : ∀ (x : α), coe_fn f x = coe_fn g x) : f = g :=
coe_fn_inj (funext h)
theorem ext_iff {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} {f : r ↪r s}
{g : r ↪r s} : f = g ↔ ∀ (x : α), coe_fn f x = coe_fn g x :=
{ mp := fun (h : f = g) (x : α) => h ▸ rfl,
mpr := fun (h : ∀ (x : α), coe_fn f x = coe_fn g x) => ext h }
/-- Identity map is a relation embedding. -/
protected def refl {α : Type u_1} (r : α → α → Prop) : r ↪r r :=
mk (function.embedding.refl α) sorry
/-- Composition of two relation embeddings is a relation embedding. -/
protected def trans {α : Type u_1} {β : Type u_2} {γ : Type u_3} {r : α → α → Prop}
{s : β → β → Prop} {t : γ → γ → Prop} (f : r ↪r s) (g : s ↪r t) : r ↪r t :=
mk (function.embedding.trans (to_embedding f) (to_embedding g)) sorry
protected instance inhabited {α : Type u_1} (r : α → α → Prop) : Inhabited (r ↪r r) :=
{ default := rel_embedding.refl r }
@[simp] theorem refl_apply {α : Type u_1} {r : α → α → Prop} (x : α) :
coe_fn (rel_embedding.refl r) x = x :=
rfl
theorem trans_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} {r : α → α → Prop}
{s : β → β → Prop} {t : γ → γ → Prop} (f : r ↪r s) (g : s ↪r t) (a : α) :
coe_fn (rel_embedding.trans f g) a = coe_fn g (coe_fn f a) :=
rfl
@[simp] theorem coe_trans {α : Type u_1} {β : Type u_2} {γ : Type u_3} {r : α → α → Prop}
{s : β → β → Prop} {t : γ → γ → Prop} (f : r ↪r s) (g : s ↪r t) :
⇑(rel_embedding.trans f g) = ⇑g ∘ ⇑f :=
rfl
/-- A relation embedding is also a relation embedding between dual relations. -/
protected def swap {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) : function.swap r ↪r function.swap s :=
mk (to_embedding f) sorry
/-- If `f` is injective, then it is a relation embedding from the
preimage relation of `s` to `s`. -/
def preimage {α : Type u_1} {β : Type u_2} (f : α ↪ β) (s : β → β → Prop) : ⇑f ⁻¹'o s ↪r s :=
mk f sorry
theorem eq_preimage {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) : r = ⇑f ⁻¹'o s :=
funext fun (a : α) => funext fun (b : α) => propext (iff.symm (map_rel_iff f))
protected theorem is_irrefl {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) [is_irrefl β s] : is_irrefl α r :=
is_irrefl.mk fun (a : α) => mt (iff.mpr (map_rel_iff f)) (irrefl (coe_fn f a))
protected theorem is_refl {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) [is_refl β s] : is_refl α r :=
is_refl.mk fun (a : α) => iff.mp (map_rel_iff f) (refl (coe_fn f a))
protected theorem is_symm {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) [is_symm β s] : is_symm α r :=
is_symm.mk fun (a b : α) => imp_imp_imp (iff.mpr (map_rel_iff f)) (iff.mp (map_rel_iff f)) symm
protected theorem is_asymm {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) [is_asymm β s] : is_asymm α r :=
is_asymm.mk
fun (a b : α) (h₁ : r a b) (h₂ : r b a) =>
asymm (iff.mpr (map_rel_iff f) h₁) (iff.mpr (map_rel_iff f) h₂)
protected theorem is_antisymm {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) [is_antisymm β s] : is_antisymm α r :=
sorry
protected theorem is_trans {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) [is_trans β s] : is_trans α r :=
sorry
protected theorem is_total {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) [is_total β s] : is_total α r :=
sorry
protected theorem is_preorder {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) [is_preorder β s] : is_preorder α r :=
idRhs (is_preorder α r) is_preorder.mk
protected theorem is_partial_order {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (f : r ↪r s) [is_partial_order β s] : is_partial_order α r :=
idRhs (is_partial_order α r) is_partial_order.mk
protected theorem is_linear_order {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (f : r ↪r s) [is_linear_order β s] : is_linear_order α r :=
idRhs (is_linear_order α r) is_linear_order.mk
protected theorem is_strict_order {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (f : r ↪r s) [is_strict_order β s] : is_strict_order α r :=
idRhs (is_strict_order α r) is_strict_order.mk
protected theorem is_trichotomous {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (f : r ↪r s) [is_trichotomous β s] : is_trichotomous α r :=
sorry
protected theorem is_strict_total_order' {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (f : r ↪r s) [is_strict_total_order' β s] : is_strict_total_order' α r :=
idRhs (is_strict_total_order' α r) is_strict_total_order'.mk
protected theorem acc {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) (a : α) : acc s (coe_fn f a) → acc r a :=
sorry
protected theorem well_founded {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) (h : well_founded s) : well_founded r :=
well_founded.dcases_on h
fun (h : ∀ (a : β), acc s a) =>
idRhs (well_founded r)
(well_founded.intro fun (a : α) => rel_embedding.acc f a (h (coe_fn f a)))
protected theorem is_well_order {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ↪r s) [is_well_order β s] : is_well_order α r :=
idRhs (is_well_order α r) (is_well_order.mk (rel_embedding.well_founded f is_well_order.wf))
/-- It suffices to prove `f` is monotone between strict relations
to show it is a relation embedding. -/
def of_monotone {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
[is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ (a b : α), r a b → s (f a) (f b)) :
r ↪r s :=
mk (function.embedding.mk f sorry) sorry
@[simp] theorem of_monotone_coe {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
[is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ (a b : α), r a b → s (f a) (f b)) :
⇑(of_monotone f H) = f :=
rfl
/-- Embeddings of partial orders that preserve `<` also preserve `≤` -/
def order_embedding_of_lt_embedding {α : Type u_1} {β : Type u_2} [partial_order α]
[partial_order β] (f : Less ↪r Less) : α ↪o β :=
mk (to_embedding f) sorry
end rel_embedding
namespace order_embedding
/-- lt is preserved by order embeddings of preorders -/
def lt_embedding {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ↪o β) :
Less ↪r Less :=
rel_embedding.mk (rel_embedding.to_embedding f) sorry
@[simp] theorem lt_embedding_apply {α : Type u_1} {β : Type u_2} [preorder α] [preorder β]
(f : α ↪o β) (x : α) : coe_fn (lt_embedding f) x = coe_fn f x :=
rfl
@[simp] theorem le_iff_le {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ↪o β)
{a : α} {b : α} : coe_fn f a ≤ coe_fn f b ↔ a ≤ b :=
rel_embedding.map_rel_iff f
@[simp] theorem lt_iff_lt {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ↪o β)
{a : α} {b : α} : coe_fn f a < coe_fn f b ↔ a < b :=
rel_embedding.map_rel_iff (lt_embedding f)
@[simp] theorem eq_iff_eq {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ↪o β)
{a : α} {b : α} : coe_fn f a = coe_fn f b ↔ a = b :=
function.injective.eq_iff (rel_embedding.injective f)
protected theorem monotone {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ↪o β) :
monotone ⇑f :=
fun (x y : α) => iff.mpr (le_iff_le f)
protected theorem strict_mono {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ↪o β) :
strict_mono ⇑f :=
fun (x y : α) => iff.mpr (lt_iff_lt f)
protected theorem acc {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ↪o β) (a : α) :
acc Less (coe_fn f a) → acc Less a :=
rel_embedding.acc (lt_embedding f) a
protected theorem well_founded {α : Type u_1} {β : Type u_2} [preorder α] [preorder β]
(f : α ↪o β) : well_founded Less → well_founded Less :=
rel_embedding.well_founded (lt_embedding f)
protected theorem is_well_order {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ↪o β)
[is_well_order β Less] : is_well_order α Less :=
rel_embedding.is_well_order (lt_embedding f)
/-- An order embedding is also an order embedding between dual orders. -/
protected def dual {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ↪o β) :
order_dual α ↪o order_dual β :=
rel_embedding.mk (rel_embedding.to_embedding f) sorry
/-- A sctrictly monotone map from a linear order is an order embedding. --/
def of_strict_mono {α : Type u_1} {β : Type u_2} [linear_order α] [preorder β] (f : α → β)
(h : strict_mono f) : α ↪o β :=
rel_embedding.mk (function.embedding.mk f (strict_mono.injective h)) sorry
@[simp] theorem coe_of_strict_mono {α : Type u_1} {β : Type u_2} [linear_order α] [preorder β]
{f : α → β} (h : strict_mono f) : ⇑(of_strict_mono f h) = f :=
rfl
/-- Embedding of a subtype into the ambient type as an `order_embedding`. -/
def subtype {α : Type u_1} [preorder α] (p : α → Prop) : Subtype p ↪o α :=
rel_embedding.mk (function.embedding.subtype p) sorry
@[simp] theorem coe_subtype {α : Type u_1} [preorder α] (p : α → Prop) : ⇑(subtype p) = coe := rfl
end order_embedding
/-- A relation isomorphism is an equivalence that is also a relation embedding. -/
structure rel_iso {α : Type u_4} {β : Type u_5} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β
where
map_rel_iff' : ∀ {a b : α}, s (coe_fn _to_equiv a) (coe_fn _to_equiv b) ↔ r a b
infixl:25 " ≃r " => Mathlib.rel_iso
/-- An order isomorphism is an equivalence such that `a ≤ b ↔ (f a) ≤ (f b)`.
This definition is an abbreviation of `rel_iso (≤) (≤)`. -/
def order_iso (α : Type u_1) (β : Type u_2) [HasLessEq α] [HasLessEq β] := LessEq ≃r LessEq
infixl:25 " ≃o " => Mathlib.order_iso
namespace rel_iso
/-- Convert an `rel_iso` to an `rel_embedding`. This function is also available as a coercion
but often it is easier to write `f.to_rel_embedding` than to write explicitly `r` and `s`
in the target type. -/
def to_rel_embedding {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ≃r s) : r ↪r s :=
rel_embedding.mk (equiv.to_embedding (to_equiv f)) (map_rel_iff' f)
-- see Note [function coercion]
protected instance rel_embedding.has_coe {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} : has_coe (r ≃r s) (r ↪r s) :=
has_coe.mk to_rel_embedding
protected instance has_coe_to_fun {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} : has_coe_to_fun (r ≃r s) :=
has_coe_to_fun.mk (fun (_x : r ≃r s) => α → β) fun (f : r ≃r s) => ⇑f
@[simp] theorem to_rel_embedding_eq_coe {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (f : r ≃r s) : to_rel_embedding f = ↑f :=
rfl
@[simp] theorem coe_coe_fn {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ≃r s) : ⇑↑f = ⇑f :=
rfl
theorem map_rel_iff {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} (f : r ≃r s)
{a : α} {b : α} : s (coe_fn f a) (coe_fn f b) ↔ r a b :=
map_rel_iff' f
@[simp] theorem coe_fn_mk {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : α ≃ β) (o : ∀ {a b : α}, s (coe_fn f a) (coe_fn f b) ↔ r a b) : ⇑(mk f o) = ⇑f :=
rfl
@[simp] theorem coe_fn_to_equiv {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ≃r s) : ⇑(to_equiv f) = ⇑f :=
rfl
theorem injective_to_equiv {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} :
function.injective to_equiv :=
sorry
/-- The map `coe_fn : (r ≃r s) → (α → β)` is injective. Lean fails to parse
`function.injective (λ e : r ≃r s, (e : α → β))`, so we use a trick to say the same. -/
theorem injective_coe_fn {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} :
function.injective fun (e : r ≃r s) (x : α) => coe_fn e x :=
function.injective.comp equiv.injective_coe_fn injective_to_equiv
theorem ext {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} {f : r ≃r s}
{g : r ≃r s} (h : ∀ (x : α), coe_fn f x = coe_fn g x) : f = g :=
injective_coe_fn (funext h)
theorem ext_iff {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} {f : r ≃r s}
{g : r ≃r s} : f = g ↔ ∀ (x : α), coe_fn f x = coe_fn g x :=
{ mp := fun (h : f = g) (x : α) => h ▸ rfl,
mpr := fun (h : ∀ (x : α), coe_fn f x = coe_fn g x) => ext h }
/-- Identity map is a relation isomorphism. -/
protected def refl {α : Type u_1} (r : α → α → Prop) : r ≃r r := mk (equiv.refl α) sorry
/-- Inverse map of a relation isomorphism is a relation isomorphism. -/
protected def symm {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ≃r s) : s ≃r r :=
mk (equiv.symm (to_equiv f)) sorry
/-- Composition of two relation isomorphisms is a relation isomorphism. -/
protected def trans {α : Type u_1} {β : Type u_2} {γ : Type u_3} {r : α → α → Prop}
{s : β → β → Prop} {t : γ → γ → Prop} (f₁ : r ≃r s) (f₂ : s ≃r t) : r ≃r t :=
mk (equiv.trans (to_equiv f₁) (to_equiv f₂)) sorry
protected instance inhabited {α : Type u_1} (r : α → α → Prop) : Inhabited (r ≃r r) :=
{ default := rel_iso.refl r }
@[simp] theorem default_def {α : Type u_1} (r : α → α → Prop) :
Inhabited.default = rel_iso.refl r :=
rfl
/-- a relation isomorphism is also a relation isomorphism between dual relations. -/
protected def swap {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ≃r s) : function.swap r ≃r function.swap s :=
mk (to_equiv f) sorry
@[simp] theorem coe_fn_symm_mk {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : α ≃ β) (o : ∀ {a b : α}, s (coe_fn f a) (coe_fn f b) ↔ r a b) :
⇑(rel_iso.symm (mk f o)) = ⇑(equiv.symm f) :=
rfl
@[simp] theorem refl_apply {α : Type u_1} {r : α → α → Prop} (x : α) :
coe_fn (rel_iso.refl r) x = x :=
rfl
@[simp] theorem trans_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} {r : α → α → Prop}
{s : β → β → Prop} {t : γ → γ → Prop} (f : r ≃r s) (g : s ≃r t) (a : α) :
coe_fn (rel_iso.trans f g) a = coe_fn g (coe_fn f a) :=
rfl
@[simp] theorem apply_symm_apply {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(e : r ≃r s) (x : β) : coe_fn e (coe_fn (rel_iso.symm e) x) = x :=
equiv.apply_symm_apply (to_equiv e) x
@[simp] theorem symm_apply_apply {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(e : r ≃r s) (x : α) : coe_fn (rel_iso.symm e) (coe_fn e x) = x :=
equiv.symm_apply_apply (to_equiv e) x
theorem rel_symm_apply {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(e : r ≃r s) {x : α} {y : β} : r x (coe_fn (rel_iso.symm e) y) ↔ s (coe_fn e x) y :=
sorry
theorem symm_apply_rel {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(e : r ≃r s) {x : β} {y : α} : r (coe_fn (rel_iso.symm e) x) y ↔ s x (coe_fn e y) :=
sorry
protected theorem bijective {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(e : r ≃r s) : function.bijective ⇑e :=
equiv.bijective (to_equiv e)
protected theorem injective {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(e : r ≃r s) : function.injective ⇑e :=
equiv.injective (to_equiv e)
protected theorem surjective {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(e : r ≃r s) : function.surjective ⇑e :=
equiv.surjective (to_equiv e)
@[simp] theorem range_eq {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(e : r ≃r s) : set.range ⇑e = set.univ :=
function.surjective.range_eq (rel_iso.surjective e)
@[simp] theorem eq_iff_eq {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(f : r ≃r s) {a : α} {b : α} : coe_fn f a = coe_fn f b ↔ a = b :=
function.injective.eq_iff (rel_iso.injective f)
/-- Any equivalence lifts to a relation isomorphism between `s` and its preimage. -/
protected def preimage {α : Type u_1} {β : Type u_2} (f : α ≃ β) (s : β → β → Prop) :
⇑f ⁻¹'o s ≃r s :=
mk f sorry
/-- A surjective relation embedding is a relation isomorphism. -/
def of_surjective {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop} (f : r ↪r s)
(H : function.surjective ⇑f) : r ≃r s :=
mk (equiv.of_bijective ⇑f sorry) sorry
@[simp] theorem of_surjective_coe {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (f : r ↪r s) (H : function.surjective ⇑f) : ⇑(of_surjective f H) = ⇑f :=
rfl
/--
Given relation isomorphisms `r₁ ≃r r₂` and `s₁ ≃r s₂`, construct a relation isomorphism for the
lexicographic orders on the sum.
-/
def sum_lex_congr {α₁ : Type u_1} {α₂ : Type u_2} {β₁ : Type u_3} {β₂ : Type u_4}
{r₁ : α₁ → α₁ → Prop} {r₂ : α₂ → α₂ → Prop} {s₁ : β₁ → β₁ → Prop} {s₂ : β₂ → β₂ → Prop}
(e₁ : r₁ ≃r r₂) (e₂ : s₁ ≃r s₂) : sum.lex r₁ s₁ ≃r sum.lex r₂ s₂ :=
mk (equiv.sum_congr (to_equiv e₁) (to_equiv e₂)) sorry
/--
Given relation isomorphisms `r₁ ≃r r₂` and `s₁ ≃r s₂`, construct a relation isomorphism for the
lexicographic orders on the product.
-/
def prod_lex_congr {α₁ : Type u_1} {α₂ : Type u_2} {β₁ : Type u_3} {β₂ : Type u_4}
{r₁ : α₁ → α₁ → Prop} {r₂ : α₂ → α₂ → Prop} {s₁ : β₁ → β₁ → Prop} {s₂ : β₂ → β₂ → Prop}
(e₁ : r₁ ≃r r₂) (e₂ : s₁ ≃r s₂) : prod.lex r₁ s₁ ≃r prod.lex r₂ s₂ :=
mk (equiv.prod_congr (to_equiv e₁) (to_equiv e₂)) sorry
protected instance group {α : Type u_1} {r : α → α → Prop} : group (r ≃r r) :=
group.mk (fun (f₁ f₂ : r ≃r r) => rel_iso.trans f₂ f₁) sorry (rel_iso.refl r) sorry sorry
rel_iso.symm
(div_inv_monoid.div._default (fun (f₁ f₂ : r ≃r r) => rel_iso.trans f₂ f₁) sorry
(rel_iso.refl r) sorry sorry rel_iso.symm)
sorry
@[simp] theorem coe_one {α : Type u_1} {r : α → α → Prop} : ⇑1 = id := rfl
@[simp] theorem coe_mul {α : Type u_1} {r : α → α → Prop} (e₁ : r ≃r r) (e₂ : r ≃r r) :
⇑(e₁ * e₂) = ⇑e₁ ∘ ⇑e₂ :=
rfl
theorem mul_apply {α : Type u_1} {r : α → α → Prop} (e₁ : r ≃r r) (e₂ : r ≃r r) (x : α) :
coe_fn (e₁ * e₂) x = coe_fn e₁ (coe_fn e₂ x) :=
rfl
@[simp] theorem inv_apply_self {α : Type u_1} {r : α → α → Prop} (e : r ≃r r) (x : α) :
coe_fn (e⁻¹) (coe_fn e x) = x :=
symm_apply_apply e x
@[simp] theorem apply_inv_self {α : Type u_1} {r : α → α → Prop} (e : r ≃r r) (x : α) :
coe_fn e (coe_fn (e⁻¹) x) = x :=
apply_symm_apply e x
end rel_iso
namespace order_iso
/-- Reinterpret an order isomorphism as an order embedding. -/
def to_order_embedding {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β] (e : α ≃o β) :
α ↪o β :=
rel_iso.to_rel_embedding e
@[simp] theorem coe_to_order_embedding {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β]
(e : α ≃o β) : ⇑(to_order_embedding e) = ⇑e :=
rfl
protected theorem bijective {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β] (e : α ≃o β) :
function.bijective ⇑e :=
equiv.bijective (rel_iso.to_equiv e)
protected theorem injective {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β] (e : α ≃o β) :
function.injective ⇑e :=
equiv.injective (rel_iso.to_equiv e)
protected theorem surjective {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β]
(e : α ≃o β) : function.surjective ⇑e :=
equiv.surjective (rel_iso.to_equiv e)
@[simp] theorem range_eq {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β] (e : α ≃o β) :
set.range ⇑e = set.univ :=
function.surjective.range_eq (order_iso.surjective e)
@[simp] theorem apply_eq_iff_eq {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β]
(e : α ≃o β) {x : α} {y : α} : coe_fn e x = coe_fn e y ↔ x = y :=
equiv.apply_eq_iff_eq (rel_iso.to_equiv e)
/-- Inverse of an order isomorphism. -/
def symm {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β] (e : α ≃o β) : β ≃o α :=
rel_iso.symm e
@[simp] theorem apply_symm_apply {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β]
(e : α ≃o β) (x : β) : coe_fn e (coe_fn (symm e) x) = x :=
equiv.apply_symm_apply (rel_iso.to_equiv e) x
@[simp] theorem symm_apply_apply {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β]
(e : α ≃o β) (x : α) : coe_fn (symm e) (coe_fn e x) = x :=
equiv.symm_apply_apply (rel_iso.to_equiv e) x
theorem symm_apply_eq {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β] (e : α ≃o β) {x : α}
{y : β} : coe_fn (symm e) y = x ↔ y = coe_fn e x :=
equiv.symm_apply_eq (rel_iso.to_equiv e)
@[simp] theorem symm_symm {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β] (e : α ≃o β) :
symm (symm e) = e :=
rel_iso.ext fun (x : α) => Eq.refl (coe_fn (symm (symm e)) x)
theorem symm_injective {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β] :
function.injective symm :=
sorry
@[simp] theorem to_equiv_symm {α : Type u_1} {β : Type u_2} [HasLessEq α] [HasLessEq β]
(e : α ≃o β) : equiv.symm (rel_iso.to_equiv e) = rel_iso.to_equiv (symm e) :=
rfl
/-- Composition of two order isomorphisms is an order isomorphism. -/
def trans {α : Type u_1} {β : Type u_2} {γ : Type u_3} [HasLessEq α] [HasLessEq β] [HasLessEq γ]
(e : α ≃o β) (e' : β ≃o γ) : α ≃o γ :=
rel_iso.trans e e'
@[simp] theorem coe_trans {α : Type u_1} {β : Type u_2} {γ : Type u_3} [HasLessEq α] [HasLessEq β]
[HasLessEq γ] (e : α ≃o β) (e' : β ≃o γ) : ⇑(trans e e') = ⇑e' ∘ ⇑e :=
rfl
theorem trans_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} [HasLessEq α] [HasLessEq β]
[HasLessEq γ] (e : α ≃o β) (e' : β ≃o γ) (x : α) :
coe_fn (trans e e') x = coe_fn e' (coe_fn e x) :=
rfl
protected theorem monotone {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β) :
monotone ⇑e :=
order_embedding.monotone (to_order_embedding e)
protected theorem strict_mono {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β) :
strict_mono ⇑e :=
order_embedding.strict_mono (to_order_embedding e)
@[simp] theorem le_iff_le {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
{x : α} {y : α} : coe_fn e x ≤ coe_fn e y ↔ x ≤ y :=
rel_iso.map_rel_iff e
@[simp] theorem lt_iff_lt {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
{x : α} {y : α} : coe_fn e x < coe_fn e y ↔ x < y :=
order_embedding.lt_iff_lt (to_order_embedding e)
@[simp] theorem preimage_Iic {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
(b : β) : ⇑e ⁻¹' set.Iic b = set.Iic (coe_fn (symm e) b) :=
sorry
@[simp] theorem preimage_Ici {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
(b : β) : ⇑e ⁻¹' set.Ici b = set.Ici (coe_fn (symm e) b) :=
sorry
@[simp] theorem preimage_Iio {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
(b : β) : ⇑e ⁻¹' set.Iio b = set.Iio (coe_fn (symm e) b) :=
sorry
@[simp] theorem preimage_Ioi {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
(b : β) : ⇑e ⁻¹' set.Ioi b = set.Ioi (coe_fn (symm e) b) :=
sorry
@[simp] theorem preimage_Icc {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
(a : β) (b : β) : ⇑e ⁻¹' set.Icc a b = set.Icc (coe_fn (symm e) a) (coe_fn (symm e) b) :=
sorry
@[simp] theorem preimage_Ico {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
(a : β) (b : β) : ⇑e ⁻¹' set.Ico a b = set.Ico (coe_fn (symm e) a) (coe_fn (symm e) b) :=
sorry
@[simp] theorem preimage_Ioc {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
(a : β) (b : β) : ⇑e ⁻¹' set.Ioc a b = set.Ioc (coe_fn (symm e) a) (coe_fn (symm e) b) :=
sorry
@[simp] theorem preimage_Ioo {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (e : α ≃o β)
(a : β) (b : β) : ⇑e ⁻¹' set.Ioo a b = set.Ioo (coe_fn (symm e) a) (coe_fn (symm e) b) :=
sorry
/-- To show that `f : α → β`, `g : β → α` make up an order isomorphism of linear orders,
it suffices to prove `cmp a (g b) = cmp (f a) b`. --/
def of_cmp_eq_cmp {α : Type u_1} {β : Type u_2} [linear_order α] [linear_order β] (f : α → β)
(g : β → α) (h : ∀ (a : α) (b : β), cmp a (g b) = cmp (f a) b) : α ≃o β :=
(fun (gf : ∀ (a : α), a = g (f a)) => rel_iso.mk (equiv.mk f g sorry sorry) sorry) sorry
/-- Order isomorphism between two equal sets. -/
def set_congr {α : Type u_1} [preorder α] (s : set α) (t : set α) (h : s = t) : ↥s ≃o ↥t :=
rel_iso.mk (equiv.set_congr h) sorry
/-- Order isomorphism between `univ : set α` and `α`. -/
def set.univ {α : Type u_1} [preorder α] : ↥set.univ ≃o α := rel_iso.mk (equiv.set.univ α) sorry
end order_iso
/-- If a function `f` is strictly monotone on a set `s`, then it defines an order isomorphism
between `s` and its image. -/
protected def strict_mono_incr_on.order_iso {α : Type u_1} {β : Type u_2} [linear_order α]
[preorder β] (f : α → β) (s : set α) (hf : strict_mono_incr_on f s) : ↥s ≃o ↥(f '' s) :=
rel_iso.mk (set.bij_on.equiv f sorry) sorry
/-- A strictly monotone function from a linear order is an order isomorphism between its domain and
its range. -/
protected def strict_mono.order_iso {α : Type u_1} {β : Type u_2} [linear_order α] [preorder β]
(f : α → β) (h_mono : strict_mono f) : α ≃o ↥(set.range f) :=
rel_iso.mk (equiv.set.range f (strict_mono.injective h_mono)) sorry
/-- A strictly monotone surjective function from a linear order is an order isomorphism. -/
def strict_mono.order_iso_of_surjective {α : Type u_1} {β : Type u_2} [linear_order α] [preorder β]
(f : α → β) (h_mono : strict_mono f) (h_surj : function.surjective f) : α ≃o β :=
order_iso.trans (strict_mono.order_iso f h_mono)
(order_iso.trans
(order_iso.set_congr (set.range f) set.univ (function.surjective.range_eq h_surj))
order_iso.set.univ)
/-- `subrel r p` is the inherited relation on a subset. -/
def subrel {α : Type u_1} (r : α → α → Prop) (p : set α) : ↥p → ↥p → Prop := coe ⁻¹'o r
@[simp] theorem subrel_val {α : Type u_1} (r : α → α → Prop) (p : set α) {a : ↥p} {b : ↥p} :
subrel r p a b ↔ r (subtype.val a) (subtype.val b) :=
iff.rfl
namespace subrel
/-- The relation embedding from the inherited relation on a subset. -/
protected def rel_embedding {α : Type u_1} (r : α → α → Prop) (p : set α) : subrel r p ↪r r :=
rel_embedding.mk (function.embedding.subtype fun (x : α) => x ∈ p) sorry
@[simp] theorem rel_embedding_apply {α : Type u_1} (r : α → α → Prop) (p : set α) (a : ↥p) :
coe_fn (subrel.rel_embedding r p) a = subtype.val a :=
rfl
protected instance is_well_order {α : Type u_1} (r : α → α → Prop) [is_well_order α r] (p : set α) :
is_well_order (↥p) (subrel r p) :=
rel_embedding.is_well_order (subrel.rel_embedding r p)
end subrel
/-- Restrict the codomain of a relation embedding. -/
def rel_embedding.cod_restrict {α : Type u_1} {β : Type u_2} {r : α → α → Prop} {s : β → β → Prop}
(p : set β) (f : r ↪r s) (H : ∀ (a : α), coe_fn f a ∈ p) : r ↪r subrel s p :=
rel_embedding.mk (function.embedding.cod_restrict p (rel_embedding.to_embedding f) H)
(rel_embedding.map_rel_iff' f)
@[simp] theorem rel_embedding.cod_restrict_apply {α : Type u_1} {β : Type u_2} {r : α → α → Prop}
{s : β → β → Prop} (p : set β) (f : r ↪r s) (H : ∀ (a : α), coe_fn f a ∈ p) (a : α) :
coe_fn (rel_embedding.cod_restrict p f H) a = { val := coe_fn f a, property := H a } :=
rfl
protected def order_iso.dual {α : Type u_1} {β : Type u_2} [preorder α] [preorder β] (f : α ≃o β) :
order_dual α ≃o order_dual β :=
rel_iso.mk (rel_iso.to_equiv f) sorry
theorem order_iso.map_bot' {α : Type u_1} {β : Type u_2} [partial_order α] [partial_order β]
(f : α ≃o β) {x : α} {y : β} (hx : ∀ (x' : α), x ≤ x') (hy : ∀ (y' : β), y ≤ y') :
coe_fn f x = y :=
sorry
theorem order_iso.map_bot {α : Type u_1} {β : Type u_2} [order_bot α] [order_bot β] (f : α ≃o β) :
coe_fn f ⊥ = ⊥ :=
order_iso.map_bot' f (fun (_x : α) => bot_le) fun (_x : β) => bot_le
theorem order_iso.map_top' {α : Type u_1} {β : Type u_2} [partial_order α] [partial_order β]
(f : α ≃o β) {x : α} {y : β} (hx : ∀ (x' : α), x' ≤ x) (hy : ∀ (y' : β), y' ≤ y) :
coe_fn f x = y :=
order_iso.map_bot' (order_iso.dual f) hx hy
theorem order_iso.map_top {α : Type u_1} {β : Type u_2} [order_top α] [order_top β] (f : α ≃o β) :
coe_fn f ⊤ = ⊤ :=
order_iso.map_bot (order_iso.dual f)
theorem order_embedding.map_inf_le {α : Type u_1} {β : Type u_2} [semilattice_inf α]
[semilattice_inf β] (f : α ↪o β) (x : α) (y : α) : coe_fn f (x ⊓ y) ≤ coe_fn f x ⊓ coe_fn f y :=
monotone.map_inf_le (order_embedding.monotone f) x y
theorem order_iso.map_inf {α : Type u_1} {β : Type u_2} [semilattice_inf α] [semilattice_inf β]
(f : α ≃o β) (x : α) (y : α) : coe_fn f (x ⊓ y) = coe_fn f x ⊓ coe_fn f y :=
sorry
theorem order_embedding.le_map_sup {α : Type u_1} {β : Type u_2} [semilattice_sup α]
[semilattice_sup β] (f : α ↪o β) (x : α) (y : α) : coe_fn f x ⊔ coe_fn f y ≤ coe_fn f (x ⊔ y) :=
monotone.le_map_sup (order_embedding.monotone f) x y
theorem order_iso.map_sup {α : Type u_1} {β : Type u_2} [semilattice_sup α] [semilattice_sup β]
(f : α ≃o β) (x : α) (y : α) : coe_fn f (x ⊔ y) = coe_fn f x ⊔ coe_fn f y :=
order_iso.map_inf (order_iso.dual f) x y
end Mathlib |
90c2e0eae608b256d450ba5c7ebb42b62206cd73 | f3849be5d845a1cb97680f0bbbe03b85518312f0 | /tests/lean/let3.lean | e9b61f32595b522e4d16fb3ceecb73b3c22d1b81 | [
"Apache-2.0"
] | permissive | bjoeris/lean | 0ed95125d762b17bfcb54dad1f9721f953f92eeb | 4e496b78d5e73545fa4f9a807155113d8e6b0561 | refs/heads/master | 1,611,251,218,281 | 1,495,337,658,000 | 1,495,337,658,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 142 | lean | --
constant f : num → num → num → num
#check
let a : num := 10
in f a 10
/-
#check
let a := 10,
b := 10
in f a b 10
-/
|
a039b3e10af32b2f8cca9f896de7e244e69acdc1 | cf39355caa609c0f33405126beee2739aa3cb77e | /library/init/meta/widget/html_cmd.lean | 7d1aa096b160cebbc226886630dc2c38611a2565 | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 1,001 | lean | /-
Copyright (c) E.W.Ayers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: E.W.Ayers
-/
prelude
import init.meta.widget.basic
import init.meta.lean.parser
import init.meta.interactive_base
import init.data.punit
open lean
open lean.parser
open interactive
open tactic
open widget
/-- Accepts terms with the type `component tactic_state empty` or `html empty` and
renders them interactively. -/
@[user_command]
meta def show_widget_cmd (x : parse $ tk "#html") : parser unit := do
⟨l,c⟩ ← cur_pos,
y ← parser.pexpr 0,
comp ← parser.of_tactic ((do
tactic.eval_pexpr (component tactic_state empty) y
) <|> (do
htm : html empty ← tactic.eval_pexpr (html empty) y,
c : component unit empty ← pure $ component.stateless (λ _, [htm]),
pure $ component.ignore_props $ component.ignore_action $ c
)),
save_widget ⟨l,c - ("#html").length - 1⟩ comp,
trace "successfully rendered widget"
pure ()
run_cmd skip
|
9935f579dfbea600a5fb581970e3ffdc7c2bf3f3 | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/linear_algebra/linear_independent.lean | caf1390b0b976b8f831dec1183634e74fb1f0b15 | [
"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 | 44,485 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Alexander Bentkamp, Anne Baanen
-/
import linear_algebra.finsupp
import linear_algebra.prod
import order.zorn
import data.finset.order
import data.equiv.fin
/-!
# Linear independence
This file defines linear independence in a module or vector space.
It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light.
We define `linear_independent R v` as `ker (finsupp.total ι M R v) = ⊥`. Here `finsupp.total` is the
linear map sending a function `f : ι →₀ R` with finite support to the linear combination of vectors
from `v` with these coefficients. Then we prove that several other statements are equivalent to this
one, including injectivity of `finsupp.total ι M R v` and some versions with explicitly written
linear combinations.
## Main definitions
All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or
vector space and `ι : Type*` is an arbitrary indexing type.
* `linear_independent R v` states that the elements of the family `v` are linearly independent.
* `linear_independent.repr hv x` returns the linear combination representing `x : span R (range v)`
on the linearly independent vectors `v`, given `hv : linear_independent R v`
(using classical choice). `linear_independent.repr hv` is provided as a linear map.
## Main statements
We prove several specialized tests for linear independence of families of vectors and of sets of
vectors.
* `fintype.linear_independent_iff`: if `ι` is a finite type, then any function `f : ι → R` has
finite support, so we can reformulate the statement using `∑ i : ι, f i • v i` instead of a sum
over an auxiliary `s : finset ι`;
* `linear_independent_empty_type`: a family indexed by an empty type is linearly independent;
* `linear_independent_unique_iff`: if `ι` is a singleton, then `linear_independent K v` is
equivalent to `v (default ι) ≠ 0`;
* linear_independent_option`, `linear_independent_sum`, `linear_independent_fin_cons`,
`linear_independent_fin_succ`: type-specific tests for linear independence of families of vector
fields;
* `linear_independent_insert`, `linear_independent_union`, `linear_independent_pair`,
`linear_independent_singleton`: linear independence tests for set operations.
In many cases we additionally provide dot-style operations (e.g., `linear_independent.union`) to
make the linear independence tests usable as `hv.insert ha` etc.
We also prove that any family of vectors includes a linear independent subfamily spanning the same
submodule.
## Implementation notes
We use families instead of sets because it allows us to say that two identical vectors are linearly
dependent.
If you want to use sets, use the family `(λ x, x : s → M)` given a set `s : set M`. The lemmas
`linear_independent.to_subtype_range` and `linear_independent.of_subtype_range` connect those two
worlds.
## Tags
linearly dependent, linear dependence, linearly independent, linear independence
-/
noncomputable theory
open function set submodule
open_locale classical big_operators
universe u
variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*}
variables {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*}
section semimodule
variables {v : ι → M}
variables [semiring R] [add_comm_monoid M] [add_comm_monoid M'] [add_comm_monoid M'']
variables [semimodule R M] [semimodule R M'] [semimodule R M'']
variables {a b : R} {x y : M}
variables (R) (v)
/-- `linear_independent R v` states the family of vectors `v` is linearly independent over `R`. -/
def linear_independent : Prop := (finsupp.total ι M R v).ker = ⊥
variables {R} {v}
theorem linear_independent_iff : linear_independent R v ↔
∀l, finsupp.total ι M R v l = 0 → l = 0 :=
by simp [linear_independent, linear_map.ker_eq_bot']
theorem linear_independent_iff' : linear_independent R v ↔
∀ s : finset ι, ∀ g : ι → R, ∑ i in s, g i • v i = 0 → ∀ i ∈ s, g i = 0 :=
linear_independent_iff.trans
⟨λ hf s g hg i his, have h : _ := hf (∑ i in s, finsupp.single i (g i)) $
by simpa only [linear_map.map_sum, finsupp.total_single] using hg, calc
g i = (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single i (g i)) :
by rw [finsupp.lapply_apply, finsupp.single_eq_same]
... = ∑ j in s, (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single j (g j)) :
eq.symm $ finset.sum_eq_single i
(λ j hjs hji, by rw [finsupp.lapply_apply, finsupp.single_eq_of_ne hji])
(λ hnis, hnis.elim his)
... = (∑ j in s, finsupp.single j (g j)) i : (finsupp.lapply i : (ι →₀ R) →ₗ[R] R).map_sum.symm
... = 0 : finsupp.ext_iff.1 h i,
λ hf l hl, finsupp.ext $ λ i, classical.by_contradiction $ λ hni, hni $ hf _ _ hl _ $
finsupp.mem_support_iff.2 hni⟩
theorem linear_independent_iff'' :
linear_independent R v ↔ ∀ (s : finset ι) (g : ι → R) (hg : ∀ i ∉ s, g i = 0),
∑ i in s, g i • v i = 0 → ∀ i, g i = 0 :=
linear_independent_iff'.trans ⟨λ H s g hg hv i, if his : i ∈ s then H s g hv i his else hg i his,
λ H s g hg i hi, by { convert H s (λ j, if j ∈ s then g j else 0) (λ j hj, if_neg hj)
(by simp_rw [ite_smul, zero_smul, finset.sum_extend_by_zero, hg]) i,
exact (if_pos hi).symm }⟩
theorem linear_dependent_iff : ¬ linear_independent R v ↔
∃ s : finset ι, ∃ g : ι → R, (∑ i in s, g i • v i) = 0 ∧ (∃ i ∈ s, g i ≠ 0) :=
begin
rw linear_independent_iff',
simp only [exists_prop, not_forall],
end
theorem fintype.linear_independent_iff [fintype ι] :
linear_independent R v ↔ ∀ g : ι → R, ∑ i, g i • v i = 0 → ∀ i, g i = 0 :=
begin
refine ⟨λ H g, by simpa using linear_independent_iff'.1 H finset.univ g,
λ H, linear_independent_iff''.2 $ λ s g hg hs i, H _ _ _⟩,
rw ← hs,
refine (finset.sum_subset (finset.subset_univ _) (λ i _ hi, _)).symm,
rw [hg i hi, zero_smul]
end
lemma linear_independent_empty_type (h : ¬ nonempty ι) : linear_independent R v :=
begin
rw [linear_independent_iff],
intros,
ext i,
exact false.elim (h ⟨i⟩)
end
lemma linear_independent.ne_zero [nontrivial R]
(i : ι) (hv : linear_independent R v) : v i ≠ 0 :=
λ h, @zero_ne_one R _ _ $ eq.symm begin
suffices : (finsupp.single i 1 : ι →₀ R) i = 0, {simpa},
rw linear_independent_iff.1 hv (finsupp.single i 1),
{simp},
{simp [h]}
end
/-- A subfamily of a linearly independent family (i.e., a composition with an injective map) is a
linearly independent family. -/
lemma linear_independent.comp
(h : linear_independent R v) (f : ι' → ι) (hf : injective f) : linear_independent R (v ∘ f) :=
begin
rw [linear_independent_iff, finsupp.total_comp],
intros l hl,
have h_map_domain : ∀ x, (finsupp.map_domain f l) (f x) = 0,
by rw linear_independent_iff.1 h (finsupp.map_domain f l) hl; simp,
ext x,
convert h_map_domain x,
rw [finsupp.map_domain_apply hf]
end
/-- If `v` is a linearly independent family of vectors and the kernel of a linear map `f` is
disjoint with the sumodule spaned by the vectors of `v`, then `f ∘ v` is a linearly independent
family of vectors. See also `linear_independent.map'` for a special case assuming `ker f = ⊥`. -/
lemma linear_independent.map (hv : linear_independent R v) {f : M →ₗ[R] M'}
(hf_inj : disjoint (span R (range v)) f.ker) : linear_independent R (f ∘ v) :=
begin
rw [disjoint, ← set.image_univ, finsupp.span_eq_map_total, map_inf_eq_map_inf_comap,
map_le_iff_le_comap, comap_bot, finsupp.supported_univ, top_inf_eq] at hf_inj,
unfold linear_independent at hv ⊢,
rw [hv, le_bot_iff] at hf_inj,
haveI : inhabited M := ⟨0⟩,
rw [finsupp.total_comp, @finsupp.lmap_domain_total _ _ R _ _ _ _ _ _ _ _ _ _ f,
linear_map.ker_comp, hf_inj],
exact λ _, rfl,
end
/-- An injective linear map sends linearly independent families of vectors to linearly independent
families of vectors. See also `linear_independent.map` for a more general statement. -/
lemma linear_independent.map' (hv : linear_independent R v) (f : M →ₗ[R] M')
(hf_inj : f.ker = ⊥) : linear_independent R (f ∘ v) :=
hv.map $ by simp [hf_inj]
/-- If the image of a family of vectors under a linear map is linearly independent, then so is
the original family. -/
lemma linear_independent.of_comp (f : M →ₗ[R] M') (hfv : linear_independent R (f ∘ v)) :
linear_independent R v :=
linear_independent_iff'.2 $ λ s g hg i his,
have ∑ (i : ι) in s, g i • f (v i) = 0,
by simp_rw [← f.map_smul, ← f.map_sum, hg, f.map_zero],
linear_independent_iff'.1 hfv s g this i his
/-- If `f` is an injective linear map, then the family `f ∘ v` is linearly independent
if and only if the family `v` is linearly independent. -/
protected lemma linear_map.linear_independent_iff (f : M →ₗ[R] M') (hf_inj : f.ker = ⊥) :
linear_independent R (f ∘ v) ↔ linear_independent R v :=
⟨λ h, h.of_comp f, λ h, h.map $ by simp only [hf_inj, disjoint_bot_right]⟩
@[nontriviality]
lemma linear_independent_of_subsingleton [subsingleton R] : linear_independent R v :=
linear_independent_iff.2 (λ l hl, subsingleton.elim _ _)
theorem linear_independent_equiv (e : ι ≃ ι') {f : ι' → M} :
linear_independent R (f ∘ e) ↔ linear_independent R f :=
⟨λ h, function.comp.right_id f ▸ e.self_comp_symm ▸ h.comp _ e.symm.injective,
λ h, h.comp _ e.injective⟩
theorem linear_independent_equiv' (e : ι ≃ ι') {f : ι' → M} {g : ι → M} (h : f ∘ e = g) :
linear_independent R g ↔ linear_independent R f :=
h ▸ linear_independent_equiv e
theorem linear_independent_subtype_range {ι} {f : ι → M} (hf : injective f) :
linear_independent R (coe : range f → M) ↔ linear_independent R f :=
iff.symm $ linear_independent_equiv' (equiv.of_injective f hf) rfl
alias linear_independent_subtype_range ↔ linear_independent.of_subtype_range _
theorem linear_independent_image {ι} {s : set ι} {f : ι → M} (hf : set.inj_on f s) :
linear_independent R (λ x : s, f x) ↔ linear_independent R (λ x : f '' s, (x : M)) :=
linear_independent_equiv' (equiv.set.image_of_inj_on _ _ hf) rfl
lemma linear_independent_span (hs : linear_independent R v) :
@linear_independent ι R (span R (range v))
(λ i : ι, ⟨v i, subset_span (mem_range_self i)⟩) _ _ _ :=
linear_independent.of_comp (span R (range v)).subtype hs
/-- See `linear_independent.fin_cons` for a family of elements in a vector space. -/
lemma linear_independent.fin_cons' {m : ℕ} (x : M) (v : fin m → M)
(hli : linear_independent R v)
(x_ortho : (∀ (c : R) (y : submodule.span R (set.range v)), c • x + y = (0 : M) → c = 0)) :
linear_independent R (fin.cons x v : fin m.succ → M) :=
begin
rw fintype.linear_independent_iff at hli ⊢,
rintros g total_eq j,
have zero_not_mem : (0 : fin m.succ) ∉ finset.univ.image (fin.succ : fin m → fin m.succ),
{ rw finset.mem_image,
rintro ⟨x, hx, succ_eq⟩,
exact fin.succ_ne_zero _ succ_eq },
simp only [submodule.coe_mk, fin.univ_succ, finset.sum_insert zero_not_mem,
fin.cons_zero, fin.cons_succ,
forall_true_iff, imp_self, fin.succ_inj, finset.sum_image] at total_eq,
have : g 0 = 0,
{ refine x_ortho (g 0) ⟨∑ (i : fin m), g i.succ • v i, _⟩ total_eq,
exact sum_mem _ (λ i _, smul_mem _ _ (subset_span ⟨i, rfl⟩)) },
refine fin.cases this (λ j, _) j,
apply hli (λ i, g i.succ),
simpa only [this, zero_smul, zero_add] using total_eq
end
section subtype
/-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/
theorem linear_independent_comp_subtype {s : set ι} :
linear_independent R (v ∘ coe : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total ι M R v) l = 0 → l = 0 :=
begin
simp only [linear_independent_iff, (∘), finsupp.mem_supported, finsupp.total_apply,
set.subset_def, finset.mem_coe],
split,
{ intros h l hl₁ hl₂,
have := h (l.subtype_domain s) ((finsupp.sum_subtype_domain_index hl₁).trans hl₂),
exact (finsupp.subtype_domain_eq_zero_iff hl₁).1 this },
{ intros h l hl,
refine finsupp.emb_domain_eq_zero.1 (h (l.emb_domain $ function.embedding.subtype s) _ _),
{ suffices : ∀ i hi, ¬l ⟨i, hi⟩ = 0 → i ∈ s, by simpa,
intros, assumption },
{ rwa [finsupp.emb_domain_eq_map_domain, finsupp.sum_map_domain_index],
exacts [λ _, zero_smul _ _, λ _ _ _, add_smul _ _ _] } }
end
theorem linear_independent_subtype {s : set M} :
linear_independent R (λ x, x : s → M) ↔
∀ l ∈ (finsupp.supported R R s), (finsupp.total M M R id) l = 0 → l = 0 :=
by apply @linear_independent_comp_subtype _ _ _ id
theorem linear_independent_comp_subtype_disjoint {s : set ι} :
linear_independent R (v ∘ coe : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total ι M R v).ker :=
by rw [linear_independent_comp_subtype, linear_map.disjoint_ker]
theorem linear_independent_subtype_disjoint {s : set M} :
linear_independent R (λ x, x : s → M) ↔
disjoint (finsupp.supported R R s) (finsupp.total M M R id).ker :=
by apply @linear_independent_comp_subtype_disjoint _ _ _ id
theorem linear_independent_iff_total_on {s : set M} :
linear_independent R (λ x, x : s → M) ↔ (finsupp.total_on M M R id s).ker = ⊥ :=
by rw [finsupp.total_on, linear_map.ker, linear_map.comap_cod_restrict, map_bot, comap_bot,
linear_map.ker_comp, linear_independent_subtype_disjoint, disjoint, ← map_comap_subtype,
map_le_iff_le_comap, comap_bot, ker_subtype, le_bot_iff]
lemma linear_independent.restrict_of_comp_subtype {s : set ι}
(hs : linear_independent R (v ∘ coe : s → M)) :
linear_independent R (s.restrict v) :=
hs
variables (R M)
lemma linear_independent_empty : linear_independent R (λ x, x : (∅ : set M) → M) :=
by simp [linear_independent_subtype_disjoint]
variables {R M}
lemma linear_independent.mono {t s : set M} (h : t ⊆ s) :
linear_independent R (λ x, x : s → M) → linear_independent R (λ x, x : t → M) :=
begin
simp only [linear_independent_subtype_disjoint],
exact (disjoint.mono_left (finsupp.supported_mono h))
end
lemma linear_independent_of_finite (s : set M)
(H : ∀ t ⊆ s, finite t → linear_independent R (λ x, x : t → M)) :
linear_independent R (λ x, x : s → M) :=
linear_independent_subtype.2 $
λ l hl, linear_independent_subtype.1 (H _ hl (finset.finite_to_set _)) l (subset.refl _)
lemma linear_independent_Union_of_directed {η : Type*}
{s : η → set M} (hs : directed (⊆) s)
(h : ∀ i, linear_independent R (λ x, x : s i → M)) :
linear_independent R (λ x, x : (⋃ i, s i) → M) :=
begin
by_cases hη : nonempty η,
{ resetI,
refine linear_independent_of_finite (⋃ i, s i) (λ t ht ft, _),
rcases finite_subset_Union ft ht with ⟨I, fi, hI⟩,
rcases hs.finset_le fi.to_finset with ⟨i, hi⟩,
exact (h i).mono (subset.trans hI $ bUnion_subset $
λ j hj, hi j (fi.mem_to_finset.2 hj)) },
{ refine (linear_independent_empty _ _).mono _,
rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩ }
end
lemma linear_independent_sUnion_of_directed {s : set (set M)}
(hs : directed_on (⊆) s)
(h : ∀ a ∈ s, linear_independent R (λ x, x : (a : set M) → M)) :
linear_independent R (λ x, x : (⋃₀ s) → M) :=
by rw sUnion_eq_Union; exact
linear_independent_Union_of_directed hs.directed_coe (by simpa using h)
lemma linear_independent_bUnion_of_directed {η} {s : set η} {t : η → set M}
(hs : directed_on (t ⁻¹'o (⊆)) s) (h : ∀a∈s, linear_independent R (λ x, x : t a → M)) :
linear_independent R (λ x, x : (⋃a∈s, t a) → M) :=
by rw bUnion_eq_Union; exact
linear_independent_Union_of_directed (directed_comp.2 $ hs.directed_coe) (by simpa using h)
end subtype
end semimodule
/-! ### Properties which require `ring R` -/
section module
variables {v : ι → M}
variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M'']
variables [module R M] [module R M'] [module R M'']
variables {a b : R} {x y : M}
theorem linear_independent_iff_injective_total : linear_independent R v ↔
function.injective (finsupp.total ι M R v) :=
linear_independent_iff.trans (finsupp.total ι M R v).to_add_monoid_hom.injective_iff.symm
alias linear_independent_iff_injective_total ↔ linear_independent.injective_total _
lemma linear_independent.injective [nontrivial R] (hv : linear_independent R v) :
injective v :=
begin
intros i j hij,
let l : ι →₀ R := finsupp.single i (1 : R) - finsupp.single j 1,
have h_total : finsupp.total ι M R v l = 0,
{ simp_rw [linear_map.map_sub, finsupp.total_apply],
simp [hij] },
have h_single_eq : finsupp.single i (1 : R) = finsupp.single j 1,
{ rw linear_independent_iff at hv,
simp [eq_add_of_sub_eq' (hv l h_total)] },
simpa [finsupp.single_eq_single_iff] using h_single_eq
end
theorem linear_independent.to_subtype_range {ι} {f : ι → M} (hf : linear_independent R f) :
linear_independent R (coe : range f → M) :=
begin
nontriviality R,
exact (linear_independent_subtype_range hf.injective).2 hf
end
theorem linear_independent.to_subtype_range' {ι} {f : ι → M} (hf : linear_independent R f)
{t} (ht : range f = t) :
linear_independent R (coe : t → M) :=
ht ▸ hf.to_subtype_range
theorem linear_independent.image_of_comp {ι ι'} (s : set ι) (f : ι → ι') (g : ι' → M)
(hs : linear_independent R (λ x : s, g (f x))) :
linear_independent R (λ x : f '' s, g x) :=
begin
nontriviality R,
have : inj_on f s, from inj_on_iff_injective.2 hs.injective.of_comp,
exact (linear_independent_equiv' (equiv.set.image_of_inj_on f s this) rfl).1 hs
end
theorem linear_independent.image {ι} {s : set ι} {f : ι → M}
(hs : linear_independent R (λ x : s, f x)) : linear_independent R (λ x : f '' s, (x : M)) :=
by convert linear_independent.image_of_comp s f id hs
section subtype
/-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/
lemma linear_independent.disjoint_span_image (hv : linear_independent R v) {s t : set ι}
(hs : disjoint s t) :
disjoint (submodule.span R $ v '' s) (submodule.span R $ v '' t) :=
begin
simp only [disjoint_def, finsupp.mem_span_iff_total],
rintros _ ⟨l₁, hl₁, rfl⟩ ⟨l₂, hl₂, H⟩,
rw [hv.injective_total.eq_iff] at H, subst l₂,
have : l₁ = 0 := finsupp.disjoint_supported_supported hs (submodule.mem_inf.2 ⟨hl₁, hl₂⟩),
simp [this]
end
lemma linear_independent_sum {v : ι ⊕ ι' → M} :
linear_independent R v ↔ linear_independent R (v ∘ sum.inl) ∧
linear_independent R (v ∘ sum.inr) ∧
disjoint (submodule.span R (range (v ∘ sum.inl))) (submodule.span R (range (v ∘ sum.inr))) :=
begin
rw [range_comp v, range_comp v],
refine ⟨λ h, ⟨h.comp _ sum.inl_injective, h.comp _ sum.inr_injective,
h.disjoint_span_image is_compl_range_inl_range_inr.1⟩, _⟩,
rintro ⟨hl, hr, hlr⟩,
rw [linear_independent_iff'] at *,
intros s g hg i hi,
have : ∑ i in s.preimage sum.inl (sum.inl_injective.inj_on _), (λ x, g x • v x) (sum.inl i) +
∑ i in s.preimage sum.inr (sum.inr_injective.inj_on _), (λ x, g x • v x) (sum.inr i) = 0,
{ rw [finset.sum_preimage', finset.sum_preimage', ← finset.sum_union, ← finset.filter_or],
{ simpa only [← mem_union, range_inl_union_range_inr, mem_univ, finset.filter_true] },
{ exact finset.disjoint_filter.2 (λ x hx, disjoint_left.1 is_compl_range_inl_range_inr.1) } },
{ rw ← eq_neg_iff_add_eq_zero at this,
rw [disjoint_def'] at hlr,
have A := hlr _ (sum_mem _ $ λ i hi, _) _ (neg_mem _ $ sum_mem _ $ λ i hi, _) this,
{ cases i with i i,
{ exact hl _ _ A i (finset.mem_preimage.2 hi) },
{ rw [this, neg_eq_zero] at A,
exact hr _ _ A i (finset.mem_preimage.2 hi) } },
{ exact smul_mem _ _ (subset_span ⟨sum.inl i, mem_range_self _, rfl⟩) },
{ exact smul_mem _ _ (subset_span ⟨sum.inr i, mem_range_self _, rfl⟩) } }
end
lemma linear_independent.sum_type {v' : ι' → M} (hv : linear_independent R v)
(hv' : linear_independent R v')
(h : disjoint (submodule.span R (range v)) (submodule.span R (range v'))) :
linear_independent R (sum.elim v v') :=
linear_independent_sum.2 ⟨hv, hv', h⟩
lemma linear_independent.union {s t : set M}
(hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M))
(hst : disjoint (span R s) (span R t)) :
linear_independent R (λ x, x : (s ∪ t) → M) :=
(hs.sum_type ht $ by simpa).to_subtype_range' $ by simp
lemma linear_independent_Union_finite_subtype {ι : Type*} {f : ι → set M}
(hl : ∀i, linear_independent R (λ x, x : f i → M))
(hd : ∀i, ∀t:set ι, finite t → i ∉ t → disjoint (span R (f i)) (⨆i∈t, span R (f i))) :
linear_independent R (λ x, x : (⋃i, f i) → M) :=
begin
rw [Union_eq_Union_finset f],
apply linear_independent_Union_of_directed,
apply directed_of_sup,
exact (assume t₁ t₂ ht, Union_subset_Union $ assume i, Union_subset_Union_const $ assume h, ht h),
assume t, rw [set.Union, ← finset.sup_eq_supr],
refine t.induction_on _ _,
{ rw finset.sup_empty,
apply linear_independent_empty_type (not_nonempty_iff_imp_false.2 _),
exact λ x, set.not_mem_empty x (subtype.mem x) },
{ rintros i s his ih,
rw [finset.sup_insert],
refine (hl _).union ih _,
rw [finset.sup_eq_supr],
refine (hd i _ _ his).mono_right _,
{ simp only [(span_Union _).symm],
refine span_mono (@supr_le_supr2 (set M) _ _ _ _ _ _),
rintros i, exact ⟨i, le_refl _⟩ },
{ exact s.finite_to_set } }
end
lemma linear_independent_Union_finite {η : Type*} {ιs : η → Type*}
{f : Π j : η, ιs j → M}
(hindep : ∀j, linear_independent R (f j))
(hd : ∀i, ∀t:set η, finite t → i ∉ t →
disjoint (span R (range (f i))) (⨆i∈t, span R (range (f i)))) :
linear_independent R (λ ji : Σ j, ιs j, f ji.1 ji.2) :=
begin
nontriviality R,
apply linear_independent.of_subtype_range,
{ rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ hxy,
by_cases h_cases : x₁ = y₁,
subst h_cases,
{ apply sigma.eq,
rw linear_independent.injective (hindep _) hxy,
refl },
{ have h0 : f x₁ x₂ = 0,
{ apply disjoint_def.1 (hd x₁ {y₁} (finite_singleton y₁)
(λ h, h_cases (eq_of_mem_singleton h))) (f x₁ x₂) (subset_span (mem_range_self _)),
rw supr_singleton,
simp only at hxy,
rw hxy,
exact (subset_span (mem_range_self y₂)) },
exact false.elim ((hindep x₁).ne_zero _ h0) } },
rw range_sigma_eq_Union_range,
apply linear_independent_Union_finite_subtype (λ j, (hindep j).to_subtype_range) hd,
end
end subtype
section repr
variables (hv : linear_independent R v)
/-- Canonical isomorphism between linear combinations and the span of linearly independent vectors.
-/
def linear_independent.total_equiv (hv : linear_independent R v) :
(ι →₀ R) ≃ₗ[R] span R (range v) :=
begin
apply linear_equiv.of_bijective
(linear_map.cod_restrict (span R (range v)) (finsupp.total ι M R v) _),
{ rw linear_map.ker_cod_restrict,
apply hv },
{ rw [linear_map.range, linear_map.map_cod_restrict, ← linear_map.range_le_iff_comap,
range_subtype, map_top],
rw finsupp.range_total,
apply le_refl (span R (range v)) },
{ intro l,
rw ← finsupp.range_total,
rw linear_map.mem_range,
apply mem_range_self l }
end
/-- Linear combination representing a vector in the span of linearly independent vectors.
Given a family of linearly independent vectors, we can represent any vector in their span as
a linear combination of these vectors. These are provided by this linear map.
It is simply one direction of `linear_independent.total_equiv`. -/
def linear_independent.repr (hv : linear_independent R v) :
span R (range v) →ₗ[R] ι →₀ R := hv.total_equiv.symm
lemma linear_independent.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x :=
subtype.ext_iff.1 (linear_equiv.apply_symm_apply hv.total_equiv x)
lemma linear_independent.total_comp_repr :
(finsupp.total ι M R v).comp hv.repr = submodule.subtype _ :=
linear_map.ext $ hv.total_repr
lemma linear_independent.repr_ker : hv.repr.ker = ⊥ :=
by rw [linear_independent.repr, linear_equiv.ker]
lemma linear_independent.repr_range : hv.repr.range = ⊤ :=
by rw [linear_independent.repr, linear_equiv.range]
lemma linear_independent.repr_eq
{l : ι →₀ R} {x} (eq : finsupp.total ι M R v l = ↑x) :
hv.repr x = l :=
begin
have : ↑((linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l)
= finsupp.total ι M R v l := rfl,
have : (linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l = x,
{ rw eq at this,
exact subtype.ext_iff.2 this },
rw ←linear_equiv.symm_apply_apply hv.total_equiv l,
rw ←this,
refl,
end
lemma linear_independent.repr_eq_single (i) (x) (hx : ↑x = v i) :
hv.repr x = finsupp.single i 1 :=
begin
apply hv.repr_eq,
simp [finsupp.total_single, hx]
end
-- TODO: why is this so slow?
lemma linear_independent_iff_not_smul_mem_span :
linear_independent R v ↔ (∀ (i : ι) (a : R), a • (v i) ∈ span R (v '' (univ \ {i})) → a = 0) :=
⟨ λ hv i a ha, begin
rw [finsupp.span_eq_map_total, mem_map] at ha,
rcases ha with ⟨l, hl, e⟩,
rw sub_eq_zero.1 (linear_independent_iff.1 hv (l - finsupp.single i a) (by simp [e])) at hl,
by_contra hn,
exact (not_mem_of_mem_diff (hl $ by simp [hn])) (mem_singleton _),
end, λ H, linear_independent_iff.2 $ λ l hl, begin
ext i, simp only [finsupp.zero_apply],
by_contra hn,
refine hn (H i _ _),
refine (finsupp.mem_span_iff_total _).2 ⟨finsupp.single i (l i) - l, _, _⟩,
{ rw finsupp.mem_supported',
intros j hj,
have hij : j = i :=
not_not.1
(λ hij : j ≠ i, hj ((mem_diff _).2 ⟨mem_univ _, λ h, hij (eq_of_mem_singleton h)⟩)),
simp [hij] },
{ simp [hl] }
end⟩
end repr
lemma surjective_of_linear_independent_of_span [nontrivial R]
(hv : linear_independent R v) (f : ι' ↪ ι)
(hss : range v ⊆ span R (range (v ∘ f))) :
surjective f :=
begin
intros i,
let repr : (span R (range (v ∘ f)) : Type*) → ι' →₀ R := (hv.comp f f.injective).repr,
let l := (repr ⟨v i, hss (mem_range_self i)⟩).map_domain f,
have h_total_l : finsupp.total ι M R v l = v i,
{ dsimp only [l],
rw finsupp.total_map_domain,
rw (hv.comp f f.injective).total_repr,
{ refl },
{ exact f.injective } },
have h_total_eq : (finsupp.total ι M R v) l = (finsupp.total ι M R v) (finsupp.single i 1),
by rw [h_total_l, finsupp.total_single, one_smul],
have l_eq : l = _ := linear_map.ker_eq_bot.1 hv h_total_eq,
dsimp only [l] at l_eq,
rw ←finsupp.emb_domain_eq_map_domain at l_eq,
rcases finsupp.single_of_emb_domain_single (repr ⟨v i, _⟩) f i (1 : R) zero_ne_one.symm l_eq
with ⟨i', hi'⟩,
use i',
exact hi'.2
end
lemma eq_of_linear_independent_of_span_subtype [nontrivial R] {s t : set M}
(hs : linear_independent R (λ x, x : s → M)) (h : t ⊆ s) (hst : s ⊆ span R t) : s = t :=
begin
let f : t ↪ s := ⟨λ x, ⟨x.1, h x.2⟩, λ a b hab, subtype.coe_injective (subtype.mk.inj hab)⟩,
have h_surj : surjective f,
{ apply surjective_of_linear_independent_of_span hs f _,
convert hst; simp [f, comp], },
show s = t,
{ apply subset.antisymm _ h,
intros x hx,
rcases h_surj ⟨x, hx⟩ with ⟨y, hy⟩,
convert y.mem,
rw ← subtype.mk.inj hy,
refl }
end
open linear_map
lemma linear_independent.image_subtype {s : set M} {f : M →ₗ M'}
(hs : linear_independent R (λ x, x : s → M))
(hf_inj : disjoint (span R s) f.ker) : linear_independent R (λ x, x : f '' s → M') :=
begin
rw [← @subtype.range_coe _ s] at hf_inj,
refine (hs.map hf_inj).to_subtype_range' _,
simp [set.range_comp f]
end
lemma linear_independent.inl_union_inr {s : set M} {t : set M'}
(hs : linear_independent R (λ x, x : s → M))
(ht : linear_independent R (λ x, x : t → M')) :
linear_independent R (λ x, x : inl R M M' '' s ∪ inr R M M' '' t → M × M') :=
begin
refine (hs.image_subtype _).union (ht.image_subtype _) _; [simp, simp, skip],
simp only [span_image],
simp [disjoint_iff, prod_inf_prod]
end
lemma linear_independent_inl_union_inr' {v : ι → M} {v' : ι' → M'}
(hv : linear_independent R v) (hv' : linear_independent R v') :
linear_independent R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) :=
(hv.map' (inl R M M') ker_inl).sum_type (hv'.map' (inr R M M') ker_inr) $
begin
refine is_compl_range_inl_inr.disjoint.mono _ _;
simp only [span_le, range_coe, range_comp_subset_range],
end
/-- Dedekind's linear independence of characters -/
-- See, for example, Keith Conrad's note
-- <https://kconrad.math.uconn.edu/blurbs/galoistheory/linearchar.pdf>
theorem linear_independent_monoid_hom (G : Type*) [monoid G] (L : Type*) [comm_ring L]
[no_zero_divisors L] :
@linear_independent _ L (G → L) (λ f, f : (G →* L) → (G → L)) _ _ _ :=
by letI := classical.dec_eq (G →* L);
letI : mul_action L L := distrib_mul_action.to_mul_action;
-- We prove linear independence by showing that only the trivial linear combination vanishes.
exact linear_independent_iff'.2
-- To do this, we use `finset` induction,
(λ s, finset.induction_on s (λ g hg i, false.elim) $ λ a s has ih g hg,
-- Here
-- * `a` is a new character we will insert into the `finset` of characters `s`,
-- * `ih` is the fact that only the trivial linear combination of characters in `s` is zero
-- * `hg` is the fact that `g` are the coefficients of a linear combination summing to zero
-- and it remains to prove that `g` vanishes on `insert a s`.
-- We now make the key calculation:
-- For any character `i` in the original `finset`, we have `g i • i = g i • a` as functions on the
-- monoid `G`.
have h1 : ∀ i ∈ s, (g i • i : G → L) = g i • a, from λ i his, funext $ λ x : G,
-- We prove these expressions are equal by showing
-- the differences of their values on each monoid element `x` is zero
eq_of_sub_eq_zero $ ih (λ j, g j * j x - g j * a x)
(funext $ λ y : G, calc
-- After that, it's just a chase scene.
(∑ i in s, ((g i * i x - g i * a x) • i : G → L)) y
= ∑ i in s, (g i * i x - g i * a x) * i y : finset.sum_apply _ _ _
... = ∑ i in s, (g i * i x * i y - g i * a x * i y) : finset.sum_congr rfl
(λ _ _, sub_mul _ _ _)
... = ∑ i in s, g i * i x * i y - ∑ i in s, g i * a x * i y : finset.sum_sub_distrib
... = (g a * a x * a y + ∑ i in s, g i * i x * i y)
- (g a * a x * a y + ∑ i in s, g i * a x * i y) : by rw add_sub_add_left_eq_sub
... = ∑ i in insert a s, g i * i x * i y - ∑ i in insert a s, g i * a x * i y :
by rw [finset.sum_insert has, finset.sum_insert has]
... = ∑ i in insert a s, g i * i (x * y) - ∑ i in insert a s, a x * (g i * i y) :
congr (congr_arg has_sub.sub (finset.sum_congr rfl $ λ i _, by rw [i.map_mul, mul_assoc]))
(finset.sum_congr rfl $ λ _ _, by rw [mul_assoc, mul_left_comm])
... = (∑ i in insert a s, (g i • i : G → L)) (x * y)
- a x * (∑ i in insert a s, (g i • i : G → L)) y :
by rw [finset.sum_apply, finset.sum_apply, finset.mul_sum]; refl
... = 0 - a x * 0 : by rw hg; refl
... = 0 : by rw [mul_zero, sub_zero])
i
his,
-- On the other hand, since `a` is not already in `s`, for any character `i ∈ s`
-- there is some element of the monoid on which it differs from `a`.
have h2 : ∀ i : G →* L, i ∈ s → ∃ y, i y ≠ a y, from λ i his,
classical.by_contradiction $ λ h,
have hia : i = a, from monoid_hom.ext $ λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩,
has $ hia ▸ his,
-- From these two facts we deduce that `g` actually vanishes on `s`,
have h3 : ∀ i ∈ s, g i = 0, from λ i his, let ⟨y, hy⟩ := h2 i his in
have h : g i • i y = g i • a y, from congr_fun (h1 i his) y,
or.resolve_right (mul_eq_zero.1 $ by rw [mul_sub, sub_eq_zero]; exact h) (sub_ne_zero_of_ne hy),
-- And so, using the fact that the linear combination over `s` and over `insert a s` both vanish,
-- we deduce that `g a = 0`.
have h4 : g a = 0, from calc
g a = g a * 1 : (mul_one _).symm
... = (g a • a : G → L) 1 : by rw ← a.map_one; refl
... = (∑ i in insert a s, (g i • i : G → L)) 1 : begin
rw finset.sum_eq_single a,
{ intros i his hia, rw finset.mem_insert at his,
rw [h3 i (his.resolve_left hia), zero_smul] },
{ intros haas, exfalso, apply haas, exact finset.mem_insert_self a s }
end
... = 0 : by rw hg; refl,
-- Now we're done; the last two facts together imply that `g` vanishes on every element
-- of `insert a s`.
(finset.forall_mem_insert _ _ _).2 ⟨h4, h3⟩)
lemma le_of_span_le_span [nontrivial R] {s t u: set M}
(hl : linear_independent R (coe : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u)
(hst : span R s ≤ span R t) : s ⊆ t :=
begin
have := eq_of_linear_independent_of_span_subtype
(hl.mono (set.union_subset hsu htu))
(set.subset_union_right _ _)
(set.union_subset (set.subset.trans subset_span hst) subset_span),
rw ← this, apply set.subset_union_left
end
lemma span_le_span_iff [nontrivial R] {s t u: set M}
(hl : linear_independent R (coe : u → M)) (hsu : s ⊆ u) (htu : t ⊆ u) :
span R s ≤ span R t ↔ s ⊆ t :=
⟨le_of_span_le_span hl hsu htu, span_mono⟩
end module
/-!
### Properties which require `division_ring K`
These can be considered generalizations of properties of linear independence in `vector_space`s.
-/
section vector_space
variables [division_ring K] [add_comm_group V] [add_comm_group V']
variables [semimodule K V] [semimodule K V']
variables {v : ι → V} {s t : set V} {x y z : V}
open submodule
/- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class
(instead of a data containing type class) -/
lemma mem_span_insert_exchange : x ∈ span K (insert y s) → x ∉ span K s → y ∈ span K (insert x s) :=
begin
simp [mem_span_insert],
rintro a z hz rfl h,
refine ⟨a⁻¹, -a⁻¹ • z, smul_mem _ _ hz, _⟩,
have a0 : a ≠ 0, {rintro rfl, simp * at *},
simp [a0, smul_add, smul_smul]
end
lemma linear_independent_iff_not_mem_span :
linear_independent K v ↔ (∀i, v i ∉ span K (v '' (univ \ {i}))) :=
begin
apply linear_independent_iff_not_smul_mem_span.trans,
split,
{ intros h i h_in_span,
apply one_ne_zero (h i 1 (by simp [h_in_span])) },
{ intros h i a ha,
by_contradiction ha',
exact false.elim (h _ ((smul_mem_iff _ ha').1 ha)) }
end
lemma linear_independent_unique_iff [unique ι] :
linear_independent K v ↔ v (default ι) ≠ 0 :=
begin
simp only [linear_independent_iff, finsupp.total_unique, smul_eq_zero],
refine ⟨λ h hv, _, λ hv l hl, finsupp.unique_ext $ hl.resolve_right hv⟩,
have := h (finsupp.single (default ι) 1) (or.inr hv),
exact one_ne_zero (finsupp.single_eq_zero.1 this)
end
alias linear_independent_unique_iff ↔ _ linear_independent_unique
lemma linear_independent_singleton {x : V} (hx : x ≠ 0) :
linear_independent K (λ x, x : ({x} : set V) → V) :=
@linear_independent_unique _ _ _ _ _ _ _ (set.unique_singleton _) ‹_›
lemma linear_independent_option' :
linear_independent K (λ o, option.cases_on' o x v : option ι → V) ↔
linear_independent K v ∧ (x ∉ submodule.span K (range v)) :=
begin
rw [← linear_independent_equiv (equiv.option_equiv_sum_punit ι).symm, linear_independent_sum,
@range_unique _ punit, @linear_independent_unique_iff punit, disjoint_span_singleton],
dsimp [(∘)],
refine ⟨λ h, ⟨h.1, λ hx, h.2.1 $ h.2.2 hx⟩, λ h, ⟨h.1, _, λ hx, (h.2 hx).elim⟩⟩,
rintro rfl,
exact h.2 (zero_mem _)
end
lemma linear_independent.option (hv : linear_independent K v)
(hx : x ∉ submodule.span K (range v)) :
linear_independent K (λ o, option.cases_on' o x v : option ι → V) :=
linear_independent_option'.2 ⟨hv, hx⟩
lemma linear_independent_option {v : option ι → V} :
linear_independent K v ↔
linear_independent K (v ∘ coe : ι → V) ∧ v none ∉ submodule.span K (range (v ∘ coe : ι → V)) :=
by simp only [← linear_independent_option', option.cases_on'_none_coe]
lemma linear_independent.insert (hs : linear_independent K (λ b, b : s → V)) (hx : x ∉ span K s) :
linear_independent K (λ b, b : insert x s → V) :=
begin
rw ← union_singleton,
have x0 : x ≠ 0 := mt (by rintro rfl; apply zero_mem _) hx,
apply hs.union (linear_independent_singleton x0),
rwa [disjoint_span_singleton' x0]
end
theorem linear_independent_insert' {ι} {s : set ι} {a : ι} {f : ι → V} (has : a ∉ s) :
linear_independent K (λ x : insert a s, f x) ↔
linear_independent K (λ x : s, f x) ∧ f a ∉ submodule.span K (f '' s) :=
by { rw [← linear_independent_equiv ((equiv.option_equiv_sum_punit _).trans
(equiv.set.insert has).symm), linear_independent_option], simp [(∘), range_comp f] }
theorem linear_independent_insert (hxs : x ∉ s) :
linear_independent K (λ b : insert x s, (b : V)) ↔
linear_independent K (λ b : s, (b : V)) ∧ x ∉ submodule.span K s :=
(@linear_independent_insert' _ _ _ _ _ _ _ _ id hxs).trans $ by simp
lemma linear_independent_pair {x y : V} (hx : x ≠ 0) (hy : ∀ a : K, a • x ≠ y) :
linear_independent K (coe : ({x, y} : set V) → V) :=
pair_comm y x ▸ (linear_independent_singleton hx).insert $ mt mem_span_singleton.1
(not_exists.2 hy)
lemma linear_independent_fin_cons {n} {v : fin n → V} :
linear_independent K (fin.cons x v : fin (n + 1) → V) ↔
linear_independent K v ∧ x ∉ submodule.span K (range v) :=
begin
rw [← linear_independent_equiv (fin_succ_equiv n).symm, linear_independent_option],
convert iff.rfl,
{ ext,
-- TODO: why doesn't simp use `fin_succ_equiv_symm_coe` here?
rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] },
{ rw [comp_app, fin_succ_equiv_symm_none, fin.cons_zero] },
{ ext,
rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] }
end
lemma linear_independent_fin_snoc {n} {v : fin n → V} :
linear_independent K (fin.snoc v x : fin (n + 1) → V) ↔
linear_independent K v ∧ x ∉ submodule.span K (range v) :=
by rw [fin.snoc_eq_cons_rotate, linear_independent_equiv, linear_independent_fin_cons]
/-- See `linear_independent.fin_cons'` for an uglier version that works if you
only have a semimodule. -/
lemma linear_independent.fin_cons {n} {v : fin n → V} (hv : linear_independent K v)
(hx : x ∉ submodule.span K (range v)) :
linear_independent K (fin.cons x v : fin (n + 1) → V) :=
linear_independent_fin_cons.2 ⟨hv, hx⟩
lemma linear_independent_fin_succ {n} {v : fin (n + 1) → V} :
linear_independent K v ↔
linear_independent K (fin.tail v) ∧ v 0 ∉ submodule.span K (range $ fin.tail v) :=
by rw [← linear_independent_fin_cons, fin.cons_self_tail]
lemma linear_independent_fin_succ' {n} {v : fin (n + 1) → V} :
linear_independent K v ↔
linear_independent K (fin.init v) ∧ v (fin.last _) ∉ submodule.span K (range $ fin.init v) :=
by rw [← linear_independent_fin_snoc, fin.snoc_init_self]
lemma linear_independent_fin2 {f : fin 2 → V} :
linear_independent K f ↔ f 1 ≠ 0 ∧ ∀ a : K, a • f 1 ≠ f 0 :=
by rw [linear_independent_fin_succ, linear_independent_unique_iff, range_unique,
mem_span_singleton, not_exists]; refl
lemma exists_linear_independent (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) :
∃b⊆t, s ⊆ b ∧ t ⊆ span K b ∧ linear_independent K (λ x, x : b → V) :=
begin
rcases zorn.zorn_subset₀ {b | b ⊆ t ∧ linear_independent K (λ x, x : b → V)} _ _
⟨hst, hs⟩ with ⟨b, ⟨bt, bi⟩, sb, h⟩,
{ refine ⟨b, bt, sb, λ x xt, _, bi⟩,
by_contra hn,
apply hn,
rw ← h _ ⟨insert_subset.2 ⟨xt, bt⟩, bi.insert hn⟩ (subset_insert _ _),
exact subset_span (mem_insert _ _) },
{ refine λ c hc cc c0, ⟨⋃₀ c, ⟨_, _⟩, λ x, _⟩,
{ exact sUnion_subset (λ x xc, (hc xc).1) },
{ exact linear_independent_sUnion_of_directed cc.directed_on (λ x xc, (hc xc).2) },
{ exact subset_sUnion_of_mem } }
end
variables {K V}
-- TODO(Mario): rewrite?
lemma exists_of_linear_independent_of_finite_span {t : finset V}
(hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ (span K ↑t : submodule K V)) :
∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card :=
have ∀t, ∀(s' : finset V), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ (span K ↑(s' ∪ t) : submodule K V) →
∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = (s' ∪ t).card :=
assume t, finset.induction_on t
(assume s' hs' _ hss',
have s = ↑s',
from eq_of_linear_independent_of_span_subtype hs hs' $
by simpa using hss',
⟨s', by simp [this]⟩)
(assume b₁ t hb₁t ih s' hs' hst hss',
have hb₁s : b₁ ∉ s,
from assume h,
have b₁ ∈ s ∩ ↑(insert b₁ t), from ⟨h, finset.mem_insert_self _ _⟩,
by rwa [hst] at this,
have hb₁s' : b₁ ∉ s', from assume h, hb₁s $ hs' h,
have hst : s ∩ ↑t = ∅,
from eq_empty_of_subset_empty $ subset.trans
(by simp [inter_subset_inter, subset.refl]) (le_of_eq hst),
classical.by_cases
(assume : s ⊆ (span K ↑(s' ∪ t) : submodule K V),
let ⟨u, hust, hsu, eq⟩ := ih _ hs' hst this in
have hb₁u : b₁ ∉ u, from assume h, (hust h).elim hb₁s hb₁t,
⟨insert b₁ u, by simp [insert_subset_insert hust],
subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩)
(assume : ¬ s ⊆ (span K ↑(s' ∪ t) : submodule K V),
let ⟨b₂, hb₂s, hb₂t⟩ := not_subset.mp this in
have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h,
have s ⊆ (span K ↑(insert b₂ s' ∪ t) : submodule K V), from
assume b₃ hb₃,
have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set V),
by simp [insert_eq, -singleton_union, -union_singleton, union_subset_union, subset.refl,
subset_union_right],
have hb₃ : b₃ ∈ span K (insert b₁ (insert b₂ ↑(s' ∪ t) : set V)),
from span_mono this (hss' hb₃),
have s ⊆ (span K (insert b₁ ↑(s' ∪ t)) : submodule K V),
by simpa [insert_eq, -singleton_union, -union_singleton] using hss',
have hb₁ : b₁ ∈ span K (insert b₂ ↑(s' ∪ t)),
from mem_span_insert_exchange (this hb₂s) hb₂t,
by rw [span_insert_eq_span hb₁] at hb₃; simpa using hb₃,
let ⟨u, hust, hsu, eq⟩ := ih _ (by simp [insert_subset, hb₂s, hs']) hst this in
⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]),
hsu, by simp [eq, hb₂t', hb₁t, hb₁s']⟩)),
begin
have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t,
{ ext1 x,
by_cases x ∈ s; simp * },
apply exists.elim (this (t.filter (λx, x ∉ s)) (t.filter (λx, x ∈ s))
(by simp [set.subset_def]) (by simp [set.ext_iff] {contextual := tt}) (by rwa [eq])),
intros u h,
exact ⟨u, subset.trans h.1 (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}),
h.2.1, by simp only [h.2.2, eq]⟩
end
lemma exists_finite_card_le_of_finite_of_linear_independent_of_span
(ht : finite t) (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ span K t) :
∃h : finite s, h.to_finset.card ≤ ht.to_finset.card :=
have s ⊆ (span K ↑(ht.to_finset) : submodule K V), by simp; assumption,
let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in
have finite s, from u.finite_to_set.subset hsu,
⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subset.mp $ by simp [hsu])⟩
end vector_space
|
b68678347cd606f091cb84a33356d17ccd56cc12 | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/data/multiset/basic.lean | 24da2627ae69ebd7a27b74da35e831b0436d0396 | [
"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 | 93,076 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
import data.list.perm
import algebra.group_power
/-!
# Multisets
These are implemented as the quotient of a list by permutations.
## Notation
We define the global infix notation `::ₘ` for `multiset.cons`.
-/
open list subtype nat
variables {α : Type*} {β : Type*} {γ : Type*}
/-- `multiset α` is the quotient of `list α` by list permutation. The result
is a type of finite sets with duplicates allowed. -/
def {u} multiset (α : Type u) : Type u :=
quotient (list.is_setoid α)
namespace multiset
instance : has_coe (list α) (multiset α) := ⟨quot.mk _⟩
@[simp] theorem quot_mk_to_coe (l : list α) : @eq (multiset α) ⟦l⟧ l := rfl
@[simp] theorem quot_mk_to_coe' (l : list α) : @eq (multiset α) (quot.mk (≈) l) l := rfl
@[simp] theorem quot_mk_to_coe'' (l : list α) : @eq (multiset α) (quot.mk setoid.r l) l := rfl
@[simp] theorem coe_eq_coe {l₁ l₂ : list α} : (l₁ : multiset α) = l₂ ↔ l₁ ~ l₂ := quotient.eq
instance has_decidable_eq [decidable_eq α] : decidable_eq (multiset α)
| s₁ s₂ := quotient.rec_on_subsingleton₂ s₁ s₂ $ λ l₁ l₂,
decidable_of_iff' _ quotient.eq
/-- defines a size for a multiset by referring to the size of the underlying list -/
protected def sizeof [has_sizeof α] (s : multiset α) : ℕ :=
quot.lift_on s sizeof $ λ l₁ l₂, perm.sizeof_eq_sizeof
instance has_sizeof [has_sizeof α] : has_sizeof (multiset α) := ⟨multiset.sizeof⟩
/-! ### Empty multiset -/
/-- `0 : multiset α` is the empty set -/
protected def zero : multiset α := @nil α
instance : has_zero (multiset α) := ⟨multiset.zero⟩
instance : has_emptyc (multiset α) := ⟨0⟩
instance inhabited_multiset : inhabited (multiset α) := ⟨0⟩
@[simp] theorem coe_nil_eq_zero : (@nil α : multiset α) = 0 := rfl
@[simp] theorem empty_eq_zero : (∅ : multiset α) = 0 := rfl
theorem coe_eq_zero (l : list α) : (l : multiset α) = 0 ↔ l = [] :=
iff.trans coe_eq_coe perm_nil
/-! ### `multiset.cons` -/
/-- `cons a s` is the multiset which contains `s` plus one more
instance of `a`. -/
def cons (a : α) (s : multiset α) : multiset α :=
quot.lift_on s (λ l, (a :: l : multiset α))
(λ l₁ l₂ p, quot.sound (p.cons a))
infixr ` ::ₘ `:67 := multiset.cons
instance : has_insert α (multiset α) := ⟨cons⟩
@[simp] theorem insert_eq_cons (a : α) (s : multiset α) :
insert a s = a ::ₘ s := rfl
@[simp] theorem cons_coe (a : α) (l : list α) :
(a ::ₘ l : multiset α) = (a::l : list α) := rfl
theorem singleton_coe (a : α) : (a ::ₘ 0 : multiset α) = ([a] : list α) := rfl
@[simp] theorem cons_inj_left {a b : α} (s : multiset α) :
a ::ₘ s = b ::ₘ s ↔ a = b :=
⟨quot.induction_on s $ λ l e,
have [a] ++ l ~ [b] ++ l, from quotient.exact e,
singleton_perm_singleton.1 $ (perm_append_right_iff _).1 this, congr_arg _⟩
@[simp] theorem cons_inj_right (a : α) : ∀{s t : multiset α}, a ::ₘ s = a ::ₘ t ↔ s = t :=
by rintros ⟨l₁⟩ ⟨l₂⟩; simp
@[recursor 5] protected theorem induction {p : multiset α → Prop}
(h₁ : p 0) (h₂ : ∀ ⦃a : α⦄ {s : multiset α}, p s → p (a ::ₘ s)) : ∀s, p s :=
by rintros ⟨l⟩; induction l with _ _ ih; [exact h₁, exact h₂ ih]
@[elab_as_eliminator] protected theorem induction_on {p : multiset α → Prop}
(s : multiset α) (h₁ : p 0) (h₂ : ∀ ⦃a : α⦄ {s : multiset α}, p s → p (a ::ₘ s)) : p s :=
multiset.induction h₁ h₂ s
theorem cons_swap (a b : α) (s : multiset α) : a ::ₘ b ::ₘ s = b ::ₘ a ::ₘ s :=
quot.induction_on s $ λ l, quotient.sound $ perm.swap _ _ _
section rec
variables {C : multiset α → Sort*}
/-- Dependent recursor on multisets.
TODO: should be @[recursor 6], but then the definition of `multiset.pi` fails with a stack
overflow in `whnf`.
-/
protected def rec
(C_0 : C 0)
(C_cons : Πa m, C m → C (a ::ₘ m))
(C_cons_heq : ∀ a a' m b, C_cons a (a' ::ₘ m) (C_cons a' m b) ==
C_cons a' (a ::ₘ m) (C_cons a m b))
(m : multiset α) : C m :=
quotient.hrec_on m (@list.rec α (λl, C ⟦l⟧) C_0 (λa l b, C_cons a ⟦l⟧ b)) $
assume l l' h,
h.rec_heq
(assume a l l' b b' hl, have ⟦l⟧ = ⟦l'⟧, from quot.sound hl, by cc)
(assume a a' l, C_cons_heq a a' ⟦l⟧)
@[elab_as_eliminator]
protected def rec_on (m : multiset α)
(C_0 : C 0)
(C_cons : Πa m, C m → C (a ::ₘ m))
(C_cons_heq : ∀a a' m b, C_cons a (a' ::ₘ m) (C_cons a' m b) ==
C_cons a' (a ::ₘ m) (C_cons a m b)) :
C m :=
multiset.rec C_0 C_cons C_cons_heq m
variables {C_0 : C 0} {C_cons : Πa m, C m → C (a ::ₘ m)}
{C_cons_heq : ∀a a' m b, C_cons a (a' ::ₘ m) (C_cons a' m b) ==
C_cons a' (a ::ₘ m) (C_cons a m b)}
@[simp] lemma rec_on_0 : @multiset.rec_on α C (0:multiset α) C_0 C_cons C_cons_heq = C_0 :=
rfl
@[simp] lemma rec_on_cons (a : α) (m : multiset α) :
(a ::ₘ m).rec_on C_0 C_cons C_cons_heq = C_cons a m (m.rec_on C_0 C_cons C_cons_heq) :=
quotient.induction_on m $ assume l, rfl
end rec
section mem
/-- `a ∈ s` means that `a` has nonzero multiplicity in `s`. -/
def mem (a : α) (s : multiset α) : Prop :=
quot.lift_on s (λ l, a ∈ l) (λ l₁ l₂ (e : l₁ ~ l₂), propext $ e.mem_iff)
instance : has_mem α (multiset α) := ⟨mem⟩
@[simp] lemma mem_coe {a : α} {l : list α} : a ∈ (l : multiset α) ↔ a ∈ l := iff.rfl
instance decidable_mem [decidable_eq α] (a : α) (s : multiset α) : decidable (a ∈ s) :=
quot.rec_on_subsingleton s $ list.decidable_mem a
@[simp] theorem mem_cons {a b : α} {s : multiset α} : a ∈ b ::ₘ s ↔ a = b ∨ a ∈ s :=
quot.induction_on s $ λ l, iff.rfl
lemma mem_cons_of_mem {a b : α} {s : multiset α} (h : a ∈ s) : a ∈ b ::ₘ s :=
mem_cons.2 $ or.inr h
@[simp] theorem mem_cons_self (a : α) (s : multiset α) : a ∈ a ::ₘ s :=
mem_cons.2 (or.inl rfl)
theorem forall_mem_cons {p : α → Prop} {a : α} {s : multiset α} :
(∀ x ∈ (a ::ₘ s), p x) ↔ p a ∧ ∀ x ∈ s, p x :=
quotient.induction_on' s $ λ L, list.forall_mem_cons
theorem exists_cons_of_mem {s : multiset α} {a : α} : a ∈ s → ∃ t, s = a ::ₘ t :=
quot.induction_on s $ λ l (h : a ∈ l),
let ⟨l₁, l₂, e⟩ := mem_split h in
e.symm ▸ ⟨(l₁++l₂ : list α), quot.sound perm_middle⟩
@[simp] theorem not_mem_zero (a : α) : a ∉ (0 : multiset α) := id
theorem eq_zero_of_forall_not_mem {s : multiset α} : (∀x, x ∉ s) → s = 0 :=
quot.induction_on s $ λ l H, by rw eq_nil_iff_forall_not_mem.mpr H; refl
theorem eq_zero_iff_forall_not_mem {s : multiset α} : s = 0 ↔ ∀ a, a ∉ s :=
⟨λ h, h.symm ▸ λ _, not_false, eq_zero_of_forall_not_mem⟩
theorem exists_mem_of_ne_zero {s : multiset α} : s ≠ 0 → ∃ a : α, a ∈ s :=
quot.induction_on s $ assume l hl,
match l, hl with
| [] := assume h, false.elim $ h rfl
| (a :: l) := assume _, ⟨a, by simp⟩
end
@[simp] lemma zero_ne_cons {a : α} {m : multiset α} : 0 ≠ a ::ₘ m :=
assume h, have a ∈ (0:multiset α), from h.symm ▸ mem_cons_self _ _, not_mem_zero _ this
@[simp] lemma cons_ne_zero {a : α} {m : multiset α} : a ::ₘ m ≠ 0 := zero_ne_cons.symm
lemma cons_eq_cons {a b : α} {as bs : multiset α} :
a ::ₘ as = b ::ₘ bs ↔ ((a = b ∧ as = bs) ∨ (a ≠ b ∧ ∃cs, as = b ::ₘ cs ∧ bs = a ::ₘ cs)) :=
begin
haveI : decidable_eq α := classical.dec_eq α,
split,
{ assume eq,
by_cases a = b,
{ subst h, simp * at * },
{ have : a ∈ b ::ₘ bs, from eq ▸ mem_cons_self _ _,
have : a ∈ bs, by simpa [h],
rcases exists_cons_of_mem this with ⟨cs, hcs⟩,
simp [h, hcs],
have : a ::ₘ as = b ::ₘ a ::ₘ cs, by simp [eq, hcs],
have : a ::ₘ as = a ::ₘ b ::ₘ cs, by rwa [cons_swap],
simpa using this } },
{ assume h,
rcases h with ⟨eq₁, eq₂⟩ | ⟨h, cs, eq₁, eq₂⟩,
{ simp * },
{ simp [*, cons_swap a b] } }
end
end mem
/-! ### `multiset.subset` -/
section subset
/-- `s ⊆ t` is the lift of the list subset relation. It means that any
element with nonzero multiplicity in `s` has nonzero multiplicity in `t`,
but it does not imply that the multiplicity of `a` in `s` is less or equal than in `t`;
see `s ≤ t` for this relation. -/
protected def subset (s t : multiset α) : Prop := ∀ ⦃a : α⦄, a ∈ s → a ∈ t
instance : has_subset (multiset α) := ⟨multiset.subset⟩
@[simp] theorem coe_subset {l₁ l₂ : list α} : (l₁ : multiset α) ⊆ l₂ ↔ l₁ ⊆ l₂ := iff.rfl
@[simp] theorem subset.refl (s : multiset α) : s ⊆ s := λ a h, h
theorem subset.trans {s t u : multiset α} : s ⊆ t → t ⊆ u → s ⊆ u :=
λ h₁ h₂ a m, h₂ (h₁ m)
theorem subset_iff {s t : multiset α} : s ⊆ t ↔ (∀⦃x⦄, x ∈ s → x ∈ t) := iff.rfl
theorem mem_of_subset {s t : multiset α} {a : α} (h : s ⊆ t) : a ∈ s → a ∈ t := @h _
@[simp] theorem zero_subset (s : multiset α) : 0 ⊆ s :=
λ a, (not_mem_nil a).elim
@[simp] theorem cons_subset {a : α} {s t : multiset α} : (a ::ₘ s) ⊆ t ↔ a ∈ t ∧ s ⊆ t :=
by simp [subset_iff, or_imp_distrib, forall_and_distrib]
theorem eq_zero_of_subset_zero {s : multiset α} (h : s ⊆ 0) : s = 0 :=
eq_zero_of_forall_not_mem h
theorem subset_zero {s : multiset α} : s ⊆ 0 ↔ s = 0 :=
⟨eq_zero_of_subset_zero, λ xeq, xeq.symm ▸ subset.refl 0⟩
lemma induction_on' {p : multiset α → Prop} (S : multiset α)
(h₁ : p ∅) (h₂ : ∀ {a s}, a ∈ S → s ⊆ S → p s → p (insert a s)) : p S :=
@multiset.induction_on α (λ T, T ⊆ S → p T) S (λ _, h₁) (λ a s hps hs,
let ⟨hS, sS⟩ := cons_subset.1 hs in h₂ hS sS (hps sS)) (subset.refl S)
end subset
section to_list
/-- Produces a list of the elements in the multiset using choice. -/
@[reducible] noncomputable def to_list {α : Type*} (s : multiset α) :=
classical.some (quotient.exists_rep s)
@[simp] lemma to_list_zero {α : Type*} : (multiset.to_list 0 : list α) = [] :=
(multiset.coe_eq_zero _).1 (classical.some_spec (quotient.exists_rep multiset.zero))
lemma coe_to_list {α : Type*} (s : multiset α) : (s.to_list : multiset α) = s :=
classical.some_spec (quotient.exists_rep _)
lemma mem_to_list {α : Type*} (a : α) (s : multiset α) : a ∈ s.to_list ↔ a ∈ s :=
by rw [←multiset.mem_coe, multiset.coe_to_list]
end to_list
/-! ### Partial order on `multiset`s -/
/-- `s ≤ t` means that `s` is a sublist of `t` (up to permutation).
Equivalently, `s ≤ t` means that `count a s ≤ count a t` for all `a`. -/
protected def le (s t : multiset α) : Prop :=
quotient.lift_on₂ s t (<+~) $ λ v₁ v₂ w₁ w₂ p₁ p₂,
propext (p₂.subperm_left.trans p₁.subperm_right)
instance : partial_order (multiset α) :=
{ le := multiset.le,
le_refl := by rintros ⟨l⟩; exact subperm.refl _,
le_trans := by rintros ⟨l₁⟩ ⟨l₂⟩ ⟨l₃⟩; exact @subperm.trans _ _ _ _,
le_antisymm := by rintros ⟨l₁⟩ ⟨l₂⟩ h₁ h₂; exact quot.sound (subperm.antisymm h₁ h₂) }
theorem subset_of_le {s t : multiset α} : s ≤ t → s ⊆ t :=
quotient.induction_on₂ s t $ λ l₁ l₂, subperm.subset
theorem mem_of_le {s t : multiset α} {a : α} (h : s ≤ t) : a ∈ s → a ∈ t :=
mem_of_subset (subset_of_le h)
@[simp] theorem coe_le {l₁ l₂ : list α} : (l₁ : multiset α) ≤ l₂ ↔ l₁ <+~ l₂ := iff.rfl
@[elab_as_eliminator] theorem le_induction_on {C : multiset α → multiset α → Prop}
{s t : multiset α} (h : s ≤ t)
(H : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → C l₁ l₂) : C s t :=
quotient.induction_on₂ s t (λ l₁ l₂ ⟨l, p, s⟩,
(show ⟦l⟧ = ⟦l₁⟧, from quot.sound p) ▸ H s) h
theorem zero_le (s : multiset α) : 0 ≤ s :=
quot.induction_on s $ λ l, (nil_sublist l).subperm
theorem le_zero {s : multiset α} : s ≤ 0 ↔ s = 0 :=
⟨λ h, le_antisymm h (zero_le _), le_of_eq⟩
theorem lt_cons_self (s : multiset α) (a : α) : s < a ::ₘ s :=
quot.induction_on s $ λ l,
suffices l <+~ a :: l ∧ (¬l ~ a :: l),
by simpa [lt_iff_le_and_ne],
⟨(sublist_cons _ _).subperm,
λ p, ne_of_lt (lt_succ_self (length l)) p.length_eq⟩
theorem le_cons_self (s : multiset α) (a : α) : s ≤ a ::ₘ s :=
le_of_lt $ lt_cons_self _ _
theorem cons_le_cons_iff (a : α) {s t : multiset α} : a ::ₘ s ≤ a ::ₘ t ↔ s ≤ t :=
quotient.induction_on₂ s t $ λ l₁ l₂, subperm_cons a
theorem cons_le_cons (a : α) {s t : multiset α} : s ≤ t → a ::ₘ s ≤ a ::ₘ t :=
(cons_le_cons_iff a).2
theorem le_cons_of_not_mem {a : α} {s t : multiset α} (m : a ∉ s) : s ≤ a ::ₘ t ↔ s ≤ t :=
begin
refine ⟨_, λ h, le_trans h $ le_cons_self _ _⟩,
suffices : ∀ {t'} (_ : s ≤ t') (_ : a ∈ t'), a ::ₘ s ≤ t',
{ exact λ h, (cons_le_cons_iff a).1 (this h (mem_cons_self _ _)) },
introv h, revert m, refine le_induction_on h _,
introv s m₁ m₂,
rcases mem_split m₂ with ⟨r₁, r₂, rfl⟩,
exact perm_middle.subperm_left.2 ((subperm_cons _).2 $
((sublist_or_mem_of_sublist s).resolve_right m₁).subperm)
end
/-! ### Additive monoid -/
/-- The sum of two multisets is the lift of the list append operation.
This adds the multiplicities of each element,
i.e. `count a (s + t) = count a s + count a t`. -/
protected def add (s₁ s₂ : multiset α) : multiset α :=
quotient.lift_on₂ s₁ s₂ (λ l₁ l₂, ((l₁ ++ l₂ : list α) : multiset α)) $
λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ p₁.append p₂
instance : has_add (multiset α) := ⟨multiset.add⟩
@[simp] theorem coe_add (s t : list α) : (s + t : multiset α) = (s ++ t : list α) := rfl
protected theorem add_comm (s t : multiset α) : s + t = t + s :=
quotient.induction_on₂ s t $ λ l₁ l₂, quot.sound perm_append_comm
protected theorem zero_add (s : multiset α) : 0 + s = s :=
quot.induction_on s $ λ l, rfl
theorem singleton_add (a : α) (s : multiset α) : ↑[a] + s = a ::ₘ s := rfl
protected theorem add_le_add_left (s) {t u : multiset α} : s + t ≤ s + u ↔ t ≤ u :=
quotient.induction_on₃ s t u $ λ l₁ l₂ l₃, subperm_append_left _
protected theorem add_left_cancel (s) {t u : multiset α} (h : s + t = s + u) : t = u :=
le_antisymm ((multiset.add_le_add_left _).1 (le_of_eq h))
((multiset.add_le_add_left _).1 (le_of_eq h.symm))
instance : ordered_cancel_add_comm_monoid (multiset α) :=
{ zero := 0,
add := (+),
add_comm := multiset.add_comm,
add_assoc := λ s₁ s₂ s₃, quotient.induction_on₃ s₁ s₂ s₃ $ λ l₁ l₂ l₃,
congr_arg coe $ append_assoc l₁ l₂ l₃,
zero_add := multiset.zero_add,
add_zero := λ s, by rw [multiset.add_comm, multiset.zero_add],
add_left_cancel := multiset.add_left_cancel,
add_right_cancel := λ s₁ s₂ s₃ h, multiset.add_left_cancel s₂ $
by simpa [multiset.add_comm] using h,
add_le_add_left := λ s₁ s₂ h s₃, (multiset.add_le_add_left _).2 h,
le_of_add_le_add_left := λ s₁ s₂ s₃, (multiset.add_le_add_left _).1,
..@multiset.partial_order α }
theorem le_add_right (s t : multiset α) : s ≤ s + t :=
by simpa using add_le_add_left (zero_le t) s
theorem le_add_left (s t : multiset α) : s ≤ t + s :=
by simpa using add_le_add_right (zero_le t) s
theorem le_iff_exists_add {s t : multiset α} : s ≤ t ↔ ∃ u, t = s + u :=
⟨λ h, le_induction_on h $ λ l₁ l₂ s,
let ⟨l, p⟩ := s.exists_perm_append in ⟨l, quot.sound p⟩,
λ ⟨u, e⟩, e.symm ▸ le_add_right _ _⟩
instance : canonically_ordered_add_monoid (multiset α) :=
{ lt_of_add_lt_add_left := @lt_of_add_lt_add_left _ _,
le_iff_exists_add := @le_iff_exists_add _,
bot := 0,
bot_le := multiset.zero_le,
..multiset.ordered_cancel_add_comm_monoid }
@[simp] theorem cons_add (a : α) (s t : multiset α) : a ::ₘ s + t = a ::ₘ (s + t) :=
by rw [← singleton_add, ← singleton_add, add_assoc]
@[simp] theorem add_cons (a : α) (s t : multiset α) : s + a ::ₘ t = a ::ₘ (s + t) :=
by rw [add_comm, cons_add, add_comm]
@[simp] theorem mem_add {a : α} {s t : multiset α} : a ∈ s + t ↔ a ∈ s ∨ a ∈ t :=
quotient.induction_on₂ s t $ λ l₁ l₂, mem_append
/-! ### Cardinality -/
/-- The cardinality of a multiset is the sum of the multiplicities
of all its elements, or simply the length of the underlying list. -/
def card : multiset α →+ ℕ :=
{ to_fun := λ s, quot.lift_on s length $ λ l₁ l₂, perm.length_eq,
map_zero' := rfl,
map_add' := λ s t, quotient.induction_on₂ s t length_append }
@[simp] theorem coe_card (l : list α) : card (l : multiset α) = length l := rfl
@[simp] theorem card_zero : @card α 0 = 0 := rfl
theorem card_add (s t : multiset α) : card (s + t) = card s + card t :=
card.map_add s t
lemma card_smul (s : multiset α) (n : ℕ) :
(n •ℕ s).card = n * s.card :=
by rw [card.map_nsmul s n, nat.nsmul_eq_mul]
@[simp] theorem card_cons (a : α) (s : multiset α) : card (a ::ₘ s) = card s + 1 :=
quot.induction_on s $ λ l, rfl
@[simp] theorem card_singleton (a : α) : card (a ::ₘ 0) = 1 := by simp
theorem card_le_of_le {s t : multiset α} (h : s ≤ t) : card s ≤ card t :=
le_induction_on h $ λ l₁ l₂, length_le_of_sublist
theorem eq_of_le_of_card_le {s t : multiset α} (h : s ≤ t) : card t ≤ card s → s = t :=
le_induction_on h $ λ l₁ l₂ s h₂, congr_arg coe $ eq_of_sublist_of_length_le s h₂
theorem card_lt_of_lt {s t : multiset α} (h : s < t) : card s < card t :=
lt_of_not_ge $ λ h₂, ne_of_lt h $ eq_of_le_of_card_le (le_of_lt h) h₂
theorem lt_iff_cons_le {s t : multiset α} : s < t ↔ ∃ a, a ::ₘ s ≤ t :=
⟨quotient.induction_on₂ s t $ λ l₁ l₂ h,
subperm.exists_of_length_lt (le_of_lt h) (card_lt_of_lt h),
λ ⟨a, h⟩, lt_of_lt_of_le (lt_cons_self _ _) h⟩
@[simp] theorem card_eq_zero {s : multiset α} : card s = 0 ↔ s = 0 :=
⟨λ h, (eq_of_le_of_card_le (zero_le _) (le_of_eq h)).symm, λ e, by simp [e]⟩
theorem card_pos {s : multiset α} : 0 < card s ↔ s ≠ 0 :=
pos_iff_ne_zero.trans $ not_congr card_eq_zero
theorem card_pos_iff_exists_mem {s : multiset α} : 0 < card s ↔ ∃ a, a ∈ s :=
quot.induction_on s $ λ l, length_pos_iff_exists_mem
@[elab_as_eliminator] def strong_induction_on {p : multiset α → Sort*} :
∀ (s : multiset α), (∀ s, (∀t < s, p t) → p s) → p s
| s := λ ih, ih s $ λ t h,
have card t < card s, from card_lt_of_lt h,
strong_induction_on t ih
using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf card⟩]}
theorem strong_induction_eq {p : multiset α → Sort*}
(s : multiset α) (H) : @strong_induction_on _ p s H =
H s (λ t h, @strong_induction_on _ p t H) :=
by rw [strong_induction_on]
@[elab_as_eliminator] lemma case_strong_induction_on {p : multiset α → Prop}
(s : multiset α) (h₀ : p 0) (h₁ : ∀ a s, (∀t ≤ s, p t) → p (a ::ₘ s)) : p s :=
multiset.strong_induction_on s $ assume s,
multiset.induction_on s (λ _, h₀) $ λ a s _ ih, h₁ _ _ $
λ t h, ih _ $ lt_of_le_of_lt h $ lt_cons_self _ _
/-! ### Singleton -/
instance : has_singleton α (multiset α) := ⟨λ a, a ::ₘ 0⟩
instance : is_lawful_singleton α (multiset α) := ⟨λ a, rfl⟩
@[simp] theorem singleton_eq_singleton (a : α) : singleton a = a ::ₘ 0 := rfl
@[simp] theorem mem_singleton {a b : α} : b ∈ a ::ₘ 0 ↔ b = a := by simp
theorem mem_singleton_self (a : α) : a ∈ (a ::ₘ 0 : multiset α) := mem_cons_self _ _
theorem singleton_inj {a b : α} : a ::ₘ 0 = b ::ₘ 0 ↔ a = b := cons_inj_left _
@[simp] theorem singleton_ne_zero (a : α) : a ::ₘ 0 ≠ 0 :=
ne_of_gt (lt_cons_self _ _)
@[simp] theorem singleton_le {a : α} {s : multiset α} : a ::ₘ 0 ≤ s ↔ a ∈ s :=
⟨λ h, mem_of_le h (mem_singleton_self _),
λ h, let ⟨t, e⟩ := exists_cons_of_mem h in e.symm ▸ cons_le_cons _ (zero_le _)⟩
theorem card_eq_one {s : multiset α} : card s = 1 ↔ ∃ a, s = a ::ₘ 0 :=
⟨quot.induction_on s $ λ l h,
(list.length_eq_one.1 h).imp $ λ a, congr_arg coe,
λ ⟨a, e⟩, e.symm ▸ rfl⟩
/-! ### `multiset.repeat` -/
/-- `repeat a n` is the multiset containing only `a` with multiplicity `n`. -/
def repeat (a : α) (n : ℕ) : multiset α := repeat a n
@[simp] lemma repeat_zero (a : α) : repeat a 0 = 0 := rfl
@[simp] lemma repeat_succ (a : α) (n) : repeat a (n+1) = a ::ₘ repeat a n := by simp [repeat]
@[simp] lemma repeat_one (a : α) : repeat a 1 = a ::ₘ 0 := by simp
@[simp] lemma card_repeat : ∀ (a : α) n, card (repeat a n) = n := length_repeat
theorem eq_of_mem_repeat {a b : α} {n} : b ∈ repeat a n → b = a := eq_of_mem_repeat
theorem eq_repeat' {a : α} {s : multiset α} : s = repeat a s.card ↔ ∀ b ∈ s, b = a :=
quot.induction_on s $ λ l, iff.trans ⟨λ h,
(perm_repeat.1 $ (quotient.exact h)), congr_arg coe⟩ eq_repeat'
theorem eq_repeat_of_mem {a : α} {s : multiset α} : (∀ b ∈ s, b = a) → s = repeat a s.card :=
eq_repeat'.2
theorem eq_repeat {a : α} {n} {s : multiset α} : s = repeat a n ↔ card s = n ∧ ∀ b ∈ s, b = a :=
⟨λ h, h.symm ▸ ⟨card_repeat _ _, λ b, eq_of_mem_repeat⟩,
λ ⟨e, al⟩, e ▸ eq_repeat_of_mem al⟩
theorem repeat_subset_singleton : ∀ (a : α) n, repeat a n ⊆ a ::ₘ 0 := repeat_subset_singleton
theorem repeat_le_coe {a : α} {n} {l : list α} : repeat a n ≤ l ↔ list.repeat a n <+ l :=
⟨λ ⟨l', p, s⟩, (perm_repeat.1 p) ▸ s, sublist.subperm⟩
/-! ### Erasing one copy of an element -/
section erase
variables [decidable_eq α] {s t : multiset α} {a b : α}
/-- `erase s a` is the multiset that subtracts 1 from the
multiplicity of `a`. -/
def erase (s : multiset α) (a : α) : multiset α :=
quot.lift_on s (λ l, (l.erase a : multiset α))
(λ l₁ l₂ p, quot.sound (p.erase a))
@[simp] theorem coe_erase (l : list α) (a : α) :
erase (l : multiset α) a = l.erase a := rfl
@[simp] theorem erase_zero (a : α) : (0 : multiset α).erase a = 0 := rfl
@[simp] theorem erase_cons_head (a : α) (s : multiset α) : (a ::ₘ s).erase a = s :=
quot.induction_on s $ λ l, congr_arg coe $ erase_cons_head a l
@[simp, priority 990]
theorem erase_cons_tail {a b : α} (s : multiset α) (h : b ≠ a) :
(b ::ₘ s).erase a = b ::ₘ s.erase a :=
quot.induction_on s $ λ l, congr_arg coe $ erase_cons_tail l h
@[simp, priority 980]
theorem erase_of_not_mem {a : α} {s : multiset α} : a ∉ s → s.erase a = s :=
quot.induction_on s $ λ l h, congr_arg coe $ erase_of_not_mem h
@[simp, priority 980]
theorem cons_erase {s : multiset α} {a : α} : a ∈ s → a ::ₘ s.erase a = s :=
quot.induction_on s $ λ l h, quot.sound (perm_cons_erase h).symm
theorem le_cons_erase (s : multiset α) (a : α) : s ≤ a ::ₘ s.erase a :=
if h : a ∈ s then le_of_eq (cons_erase h).symm
else by rw erase_of_not_mem h; apply le_cons_self
theorem erase_add_left_pos {a : α} {s : multiset α} (t) : a ∈ s → (s + t).erase a = s.erase a + t :=
quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ erase_append_left l₂ h
theorem erase_add_right_pos {a : α} (s) {t : multiset α} (h : a ∈ t) :
(s + t).erase a = s + t.erase a :=
by rw [add_comm, erase_add_left_pos s h, add_comm]
theorem erase_add_right_neg {a : α} {s : multiset α} (t) :
a ∉ s → (s + t).erase a = s + t.erase a :=
quotient.induction_on₂ s t $ λ l₁ l₂ h, congr_arg coe $ erase_append_right l₂ h
theorem erase_add_left_neg {a : α} (s) {t : multiset α} (h : a ∉ t) :
(s + t).erase a = s.erase a + t :=
by rw [add_comm, erase_add_right_neg s h, add_comm]
theorem erase_le (a : α) (s : multiset α) : s.erase a ≤ s :=
quot.induction_on s $ λ l, (erase_sublist a l).subperm
@[simp] theorem erase_lt {a : α} {s : multiset α} : s.erase a < s ↔ a ∈ s :=
⟨λ h, not_imp_comm.1 erase_of_not_mem (ne_of_lt h),
λ h, by simpa [h] using lt_cons_self (s.erase a) a⟩
theorem erase_subset (a : α) (s : multiset α) : s.erase a ⊆ s :=
subset_of_le (erase_le a s)
theorem mem_erase_of_ne {a b : α} {s : multiset α} (ab : a ≠ b) : a ∈ s.erase b ↔ a ∈ s :=
quot.induction_on s $ λ l, list.mem_erase_of_ne ab
theorem mem_of_mem_erase {a b : α} {s : multiset α} : a ∈ s.erase b → a ∈ s :=
mem_of_subset (erase_subset _ _)
theorem erase_comm (s : multiset α) (a b : α) : (s.erase a).erase b = (s.erase b).erase a :=
quot.induction_on s $ λ l, congr_arg coe $ l.erase_comm a b
theorem erase_le_erase {s t : multiset α} (a : α) (h : s ≤ t) : s.erase a ≤ t.erase a :=
le_induction_on h $ λ l₁ l₂ h, (h.erase _).subperm
theorem erase_le_iff_le_cons {s t : multiset α} {a : α} : s.erase a ≤ t ↔ s ≤ a ::ₘ t :=
⟨λ h, le_trans (le_cons_erase _ _) (cons_le_cons _ h),
λ h, if m : a ∈ s
then by rw ← cons_erase m at h; exact (cons_le_cons_iff _).1 h
else le_trans (erase_le _ _) ((le_cons_of_not_mem m).1 h)⟩
@[simp] theorem card_erase_of_mem {a : α} {s : multiset α} :
a ∈ s → card (s.erase a) = pred (card s) :=
quot.induction_on s $ λ l, length_erase_of_mem
theorem card_erase_lt_of_mem {a : α} {s : multiset α} : a ∈ s → card (s.erase a) < card s :=
λ h, card_lt_of_lt (erase_lt.mpr h)
theorem card_erase_le {a : α} {s : multiset α} : card (s.erase a) ≤ card s :=
card_le_of_le (erase_le a s)
end erase
@[simp] theorem coe_reverse (l : list α) : (reverse l : multiset α) = l :=
quot.sound $ reverse_perm _
/-! ### `multiset.map` -/
/-- `map f s` is the lift of the list `map` operation. The multiplicity
of `b` in `map f s` is the number of `a ∈ s` (counting multiplicity)
such that `f a = b`. -/
def map (f : α → β) (s : multiset α) : multiset β :=
quot.lift_on s (λ l : list α, (l.map f : multiset β))
(λ l₁ l₂ p, quot.sound (p.map f))
theorem forall_mem_map_iff {f : α → β} {p : β → Prop} {s : multiset α} :
(∀ y ∈ s.map f, p y) ↔ (∀ x ∈ s, p (f x)) :=
quotient.induction_on' s $ λ L, list.forall_mem_map_iff
@[simp] theorem coe_map (f : α → β) (l : list α) : map f ↑l = l.map f := rfl
@[simp] theorem map_zero (f : α → β) : map f 0 = 0 := rfl
@[simp] theorem map_cons (f : α → β) (a s) : map f (a ::ₘ s) = f a ::ₘ map f s :=
quot.induction_on s $ λ l, rfl
lemma map_singleton (f : α → β) (a : α) : ({a} : multiset α).map f = {f a} := rfl
theorem map_repeat (f : α → β) (a : α) (k : ℕ) : (repeat a k).map f = repeat (f a) k := by
{ induction k, simp, simpa }
@[simp] theorem map_add (f : α → β) (s t) : map f (s + t) = map f s + map f t :=
quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ map_append _ _ _
instance (f : α → β) : is_add_monoid_hom (map f) :=
{ map_add := map_add _, map_zero := map_zero _ }
theorem map_nsmul (f : α → β) (n s) : map f (n •ℕ s) = n •ℕ map f s :=
(add_monoid_hom.of (map f)).map_nsmul _ _
@[simp] theorem mem_map {f : α → β} {b : β} {s : multiset α} :
b ∈ map f s ↔ ∃ a, a ∈ s ∧ f a = b :=
quot.induction_on s $ λ l, mem_map
@[simp] theorem card_map (f : α → β) (s) : card (map f s) = card s :=
quot.induction_on s $ λ l, length_map _ _
@[simp] theorem map_eq_zero {s : multiset α} {f : α → β} : s.map f = 0 ↔ s = 0 :=
by rw [← multiset.card_eq_zero, multiset.card_map, multiset.card_eq_zero]
theorem mem_map_of_mem (f : α → β) {a : α} {s : multiset α} (h : a ∈ s) : f a ∈ map f s :=
mem_map.2 ⟨_, h, rfl⟩
theorem mem_map_of_injective {f : α → β} (H : function.injective f) {a : α} {s : multiset α} :
f a ∈ map f s ↔ a ∈ s :=
quot.induction_on s $ λ l, mem_map_of_injective H
@[simp] theorem map_map (g : β → γ) (f : α → β) (s : multiset α) :
map g (map f s) = map (g ∘ f) s :=
quot.induction_on s $ λ l, congr_arg coe $ list.map_map _ _ _
theorem map_id (s : multiset α) : map id s = s :=
quot.induction_on s $ λ l, congr_arg coe $ map_id _
@[simp] lemma map_id' (s : multiset α) : map (λx, x) s = s := map_id s
@[simp] theorem map_const (s : multiset α) (b : β) : map (function.const α b) s = repeat b s.card :=
quot.induction_on s $ λ l, congr_arg coe $ map_const _ _
@[congr] theorem map_congr {f g : α → β} {s : multiset α} :
(∀ x ∈ s, f x = g x) → map f s = map g s :=
quot.induction_on s $ λ l H, congr_arg coe $ map_congr H
lemma map_hcongr {β' : Type*} {m : multiset α} {f : α → β} {f' : α → β'}
(h : β = β') (hf : ∀a∈m, f a == f' a) : map f m == map f' m :=
begin subst h, simp at hf, simp [map_congr hf] end
theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) :
b₁ = b₂ :=
eq_of_mem_repeat $ by rwa map_const at h
@[simp] theorem map_le_map {f : α → β} {s t : multiset α} (h : s ≤ t) : map f s ≤ map f t :=
le_induction_on h $ λ l₁ l₂ h, (h.map f).subperm
@[simp] theorem map_subset_map {f : α → β} {s t : multiset α} (H : s ⊆ t) : map f s ⊆ map f t :=
λ b m, let ⟨a, h, e⟩ := mem_map.1 m in mem_map.2 ⟨a, H h, e⟩
/-! ### `multiset.fold` -/
/-- `foldl f H b s` is the lift of the list operation `foldl f b l`,
which folds `f` over the multiset. It is well defined when `f` is right-commutative,
that is, `f (f b a₁) a₂ = f (f b a₂) a₁`. -/
def foldl (f : β → α → β) (H : right_commutative f) (b : β) (s : multiset α) : β :=
quot.lift_on s (λ l, foldl f b l)
(λ l₁ l₂ p, p.foldl_eq H b)
@[simp] theorem foldl_zero (f : β → α → β) (H b) : foldl f H b 0 = b := rfl
@[simp] theorem foldl_cons (f : β → α → β) (H b a s) :
foldl f H b (a ::ₘ s) = foldl f H (f b a) s :=
quot.induction_on s $ λ l, rfl
@[simp] theorem foldl_add (f : β → α → β) (H b s t) :
foldl f H b (s + t) = foldl f H (foldl f H b s) t :=
quotient.induction_on₂ s t $ λ l₁ l₂, foldl_append _ _ _ _
/-- `foldr f H b s` is the lift of the list operation `foldr f b l`,
which folds `f` over the multiset. It is well defined when `f` is left-commutative,
that is, `f a₁ (f a₂ b) = f a₂ (f a₁ b)`. -/
def foldr (f : α → β → β) (H : left_commutative f) (b : β) (s : multiset α) : β :=
quot.lift_on s (λ l, foldr f b l)
(λ l₁ l₂ p, p.foldr_eq H b)
@[simp] theorem foldr_zero (f : α → β → β) (H b) : foldr f H b 0 = b := rfl
@[simp] theorem foldr_cons (f : α → β → β) (H b a s) :
foldr f H b (a ::ₘ s) = f a (foldr f H b s) :=
quot.induction_on s $ λ l, rfl
@[simp] theorem foldr_add (f : α → β → β) (H b s t) :
foldr f H b (s + t) = foldr f H (foldr f H b t) s :=
quotient.induction_on₂ s t $ λ l₁ l₂, foldr_append _ _ _ _
@[simp] theorem coe_foldr (f : α → β → β) (H : left_commutative f) (b : β) (l : list α) :
foldr f H b l = l.foldr f b := rfl
@[simp] theorem coe_foldl (f : β → α → β) (H : right_commutative f) (b : β) (l : list α) :
foldl f H b l = l.foldl f b := rfl
theorem coe_foldr_swap (f : α → β → β) (H : left_commutative f) (b : β) (l : list α) :
foldr f H b l = l.foldl (λ x y, f y x) b :=
(congr_arg (foldr f H b) (coe_reverse l)).symm.trans $ foldr_reverse _ _ _
theorem foldr_swap (f : α → β → β) (H : left_commutative f) (b : β) (s : multiset α) :
foldr f H b s = foldl (λ x y, f y x) (λ x y z, (H _ _ _).symm) b s :=
quot.induction_on s $ λ l, coe_foldr_swap _ _ _ _
theorem foldl_swap (f : β → α → β) (H : right_commutative f) (b : β) (s : multiset α) :
foldl f H b s = foldr (λ x y, f y x) (λ x y z, (H _ _ _).symm) b s :=
(foldr_swap _ _ _ _).symm
lemma foldr_induction' (f : α → β → β) (H : left_commutative f) (x : β) (q : α → Prop)
(p : β → Prop) (s : multiset α) (hpqf : ∀ a b, q a → p b → p (f a b)) (px : p x)
(q_s : ∀ a ∈ s, q a) :
p (foldr f H x s) :=
begin
revert s,
refine multiset.induction (by simp [px]) _,
intros a s hs hsa,
rw foldr_cons,
have hps : ∀ (x : α), x ∈ s → q x, from λ x hxs, hsa x (mem_cons_of_mem hxs),
exact hpqf a (foldr f H x s) (hsa a (mem_cons_self a s)) (hs hps),
end
lemma foldr_induction (f : α → α → α) (H : left_commutative f) (x : α) (p : α → Prop)
(s : multiset α) (p_f : ∀ a b, p a → p b → p (f a b)) (px : p x) (p_s : ∀ a ∈ s, p a) :
p (foldr f H x s) :=
foldr_induction' f H x p p s p_f px p_s
lemma foldl_induction' (f : β → α → β) (H : right_commutative f) (x : β) (q : α → Prop)
(p : β → Prop) (s : multiset α) (hpqf : ∀ a b, q a → p b → p (f b a)) (px : p x)
(q_s : ∀ a ∈ s, q a) :
p (foldl f H x s) :=
begin
rw foldl_swap,
exact foldr_induction' (λ x y, f y x) (λ x y z, (H _ _ _).symm) x q p s hpqf px q_s,
end
lemma foldl_induction (f : α → α → α) (H : right_commutative f) (x : α) (p : α → Prop)
(s : multiset α) (p_f : ∀ a b, p a → p b → p (f b a)) (px : p x) (p_s : ∀ a ∈ s, p a) :
p (foldl f H x s) :=
foldl_induction' f H x p p s p_f px p_s
/-- Product of a multiset given a commutative monoid structure on `α`.
`prod {a, b, c} = a * b * c` -/
@[to_additive]
def prod [comm_monoid α] : multiset α → α :=
foldr (*) (λ x y z, by simp [mul_left_comm]) 1
@[to_additive]
theorem prod_eq_foldr [comm_monoid α] (s : multiset α) :
prod s = foldr (*) (λ x y z, by simp [mul_left_comm]) 1 s := rfl
@[to_additive]
theorem prod_eq_foldl [comm_monoid α] (s : multiset α) :
prod s = foldl (*) (λ x y z, by simp [mul_right_comm]) 1 s :=
(foldr_swap _ _ _ _).trans (by simp [mul_comm])
@[simp, to_additive]
theorem coe_prod [comm_monoid α] (l : list α) : prod ↑l = l.prod :=
prod_eq_foldl _
attribute [norm_cast] coe_prod coe_sum
@[simp, to_additive]
theorem prod_zero [comm_monoid α] : @prod α _ 0 = 1 := rfl
@[simp, to_additive]
theorem prod_cons [comm_monoid α] (a : α) (s) : prod (a ::ₘ s) = a * prod s :=
foldr_cons _ _ _ _ _
@[to_additive]
theorem prod_singleton [comm_monoid α] (a : α) : prod (a ::ₘ 0) = a := by simp
@[simp, to_additive]
theorem prod_add [comm_monoid α] (s t : multiset α) : prod (s + t) = prod s * prod t :=
quotient.induction_on₂ s t $ λ l₁ l₂, by simp
instance sum.is_add_monoid_hom [add_comm_monoid α] : is_add_monoid_hom (sum : multiset α → α) :=
{ map_add := sum_add, map_zero := sum_zero }
lemma prod_smul {α : Type*} [comm_monoid α] (m : multiset α) :
∀n, (n •ℕ m).prod = m.prod ^ n
| 0 := rfl
| (n + 1) :=
by rw [add_nsmul, one_nsmul, pow_add, pow_one, prod_add, prod_smul n]
@[simp] theorem prod_repeat [comm_monoid α] (a : α) (n : ℕ) : prod (multiset.repeat a n) = a ^ n :=
by simp [repeat, list.prod_repeat]
@[simp] theorem sum_repeat [add_comm_monoid α] :
∀ (a : α) (n : ℕ), sum (multiset.repeat a n) = n •ℕ a :=
@prod_repeat (multiplicative α) _
attribute [to_additive] prod_repeat
lemma prod_map_one [comm_monoid γ] {m : multiset α} :
prod (m.map (λa, (1 : γ))) = (1 : γ) :=
by simp
lemma sum_map_zero [add_comm_monoid γ] {m : multiset α} :
sum (m.map (λa, (0 : γ))) = (0 : γ) :=
by simp
attribute [to_additive] prod_map_one
@[simp, to_additive]
lemma prod_map_mul [comm_monoid γ] {m : multiset α} {f g : α → γ} :
prod (m.map $ λa, f a * g a) = prod (m.map f) * prod (m.map g) :=
multiset.induction_on m (by simp) (assume a m ih, by simp [ih]; cc)
lemma prod_map_prod_map [comm_monoid γ] (m : multiset α) (n : multiset β) {f : α → β → γ} :
prod (m.map $ λa, prod $ n.map $ λb, f a b) = prod (n.map $ λb, prod $ m.map $ λa, f a b) :=
multiset.induction_on m (by simp) (assume a m ih, by simp [ih])
lemma sum_map_sum_map [add_comm_monoid γ] : ∀ (m : multiset α) (n : multiset β) {f : α → β → γ},
sum (m.map $ λa, sum $ n.map $ λb, f a b) = sum (n.map $ λb, sum $ m.map $ λa, f a b) :=
@prod_map_prod_map _ _ (multiplicative γ) _
attribute [to_additive] prod_map_prod_map
lemma sum_map_mul_left [semiring β] {b : β} {s : multiset α} {f : α → β} :
sum (s.map (λa, b * f a)) = b * sum (s.map f) :=
multiset.induction_on s (by simp) (assume a s ih, by simp [ih, mul_add])
lemma sum_map_mul_right [semiring β] {b : β} {s : multiset α} {f : α → β} :
sum (s.map (λa, f a * b)) = sum (s.map f) * b :=
multiset.induction_on s (by simp) (assume a s ih, by simp [ih, add_mul])
lemma prod_eq_zero {M₀ : Type*} [comm_monoid_with_zero M₀] {s : multiset M₀} (h : (0 : M₀) ∈ s) :
multiset.prod s = 0 :=
begin
rcases multiset.exists_cons_of_mem h with ⟨s', hs'⟩,
simp [hs', multiset.prod_cons]
end
lemma prod_eq_zero_iff {M₀ : Type*} [comm_monoid_with_zero M₀] [no_zero_divisors M₀] [nontrivial M₀]
{s : multiset M₀} :
multiset.prod s = 0 ↔ (0 : M₀) ∈ s :=
by { rcases s with ⟨l⟩, simp }
theorem prod_ne_zero {M₀ : Type*} [comm_monoid_with_zero M₀] [no_zero_divisors M₀] [nontrivial M₀]
{m : multiset M₀} (h : (0 : M₀) ∉ m) : m.prod ≠ 0 :=
mt prod_eq_zero_iff.1 h
@[to_additive]
lemma prod_hom [comm_monoid α] [comm_monoid β] (s : multiset α) (f : α →* β) :
(s.map f).prod = f s.prod :=
quotient.induction_on s $ λ l, by simp only [l.prod_hom f, quot_mk_to_coe, coe_map, coe_prod]
@[to_additive]
theorem prod_hom_rel [comm_monoid β] [comm_monoid γ] (s : multiset α) {r : β → γ → Prop}
{f : α → β} {g : α → γ} (h₁ : r 1 1) (h₂ : ∀⦃a b c⦄, r b c → r (f a * b) (g a * c)) :
r (s.map f).prod (s.map g).prod :=
quotient.induction_on s $ λ l,
by simp only [l.prod_hom_rel h₁ h₂, quot_mk_to_coe, coe_map, coe_prod]
lemma dvd_prod [comm_monoid α] {a : α} {s : multiset α} : a ∈ s → a ∣ s.prod :=
quotient.induction_on s (λ l a h, by simpa using list.dvd_prod h) a
lemma prod_dvd_prod [comm_monoid α] {s t : multiset α} (h : s ≤ t) :
s.prod ∣ t.prod :=
begin
rcases multiset.le_iff_exists_add.1 h with ⟨z, rfl⟩,
simp,
end
@[to_additive sum_nonneg]
lemma one_le_prod_of_one_le [ordered_comm_monoid α] {m : multiset α} :
(∀ x ∈ m, (1 : α) ≤ x) → 1 ≤ m.prod :=
quotient.induction_on m $ λ l hl, by simpa using list.one_le_prod_of_one_le hl
@[to_additive]
lemma single_le_prod [ordered_comm_monoid α] {m : multiset α} :
(∀ x ∈ m, (1 : α) ≤ x) → ∀ x ∈ m, x ≤ m.prod :=
quotient.induction_on m $ λ l hl x hx, by simpa using list.single_le_prod hl x hx
@[to_additive all_zero_of_le_zero_le_of_sum_eq_zero]
lemma all_one_of_le_one_le_of_prod_eq_one [ordered_comm_monoid α] {m : multiset α} :
(∀ x ∈ m, (1 : α) ≤ x) → m.prod = 1 → (∀ x ∈ m, x = (1 : α)) :=
begin
apply quotient.induction_on m,
simp only [quot_mk_to_coe, coe_prod, mem_coe],
intros l hl₁ hl₂ x hx,
apply all_one_of_le_one_le_of_prod_eq_one hl₁ hl₂ _ hx,
end
lemma sum_eq_zero_iff [canonically_ordered_add_monoid α] {m : multiset α} :
m.sum = 0 ↔ ∀ x ∈ m, x = (0 : α) :=
quotient.induction_on m $ λ l, by simpa using list.sum_eq_zero_iff l
@[to_additive]
lemma prod_induction {M : Type*} [comm_monoid M] (p : M → Prop) (s : multiset M)
(p_mul : ∀ a b, p a → p b → p (a * b)) (p_one : p 1) (p_s : ∀ a ∈ s, p a) :
p s.prod :=
begin
rw prod_eq_foldr,
exact foldr_induction (*) (λ x y z, by simp [mul_left_comm]) 1 p s p_mul p_one p_s,
end
@[to_additive le_sum_of_subadditive_on_pred]
lemma le_prod_of_submultiplicative_on_pred [comm_monoid α] [ordered_comm_monoid β]
(f : α → β) (p : α → Prop) (h_one : f 1 = 1) (hp_one : p 1)
(h_mul : ∀ a b, p a → p b → f (a * b) ≤ f a * f b)
(hp_mul : ∀ a b, p a → p b → p (a * b)) (s : multiset α) (hps : ∀ a, a ∈ s → p a) :
f s.prod ≤ (s.map f).prod :=
begin
revert s,
refine multiset.induction _ _,
{ simp [le_of_eq h_one], },
intros a s hs hpsa,
have hps : ∀ x, x ∈ s → p x, from λ x hx, hpsa x (mem_cons_of_mem hx),
have hp_prod : p s.prod, from prod_induction p s hp_mul hp_one hps,
rw [prod_cons, map_cons, prod_cons],
exact (h_mul a s.prod (hpsa a (mem_cons_self a s)) hp_prod).trans (mul_le_mul_left' (hs hps) _),
end
@[to_additive le_sum_of_subadditive]
lemma le_prod_of_submultiplicative [comm_monoid α] [ordered_comm_monoid β]
(f : α → β) (h_one : f 1 = 1) (h_mul : ∀ a b, f (a * b) ≤ f a * f b) (s : multiset α) :
f s.prod ≤ (s.map f).prod :=
le_prod_of_submultiplicative_on_pred f (λ i, true) h_one trivial (λ x y _ _ , h_mul x y) (by simp)
s (by simp)
@[to_additive]
lemma prod_induction_nonempty {M : Type*} [comm_monoid M] (p : M → Prop)
(p_mul : ∀ a b, p a → p b → p (a * b)) {s : multiset M} (hs_nonempty : s ≠ ∅)
(p_s : ∀ a ∈ s, p a) :
p s.prod :=
begin
revert s,
refine multiset.induction _ _,
{ intro h,
exfalso,
simpa using h, },
intros a s hs hsa hpsa,
rw prod_cons,
by_cases hs_empty : s = ∅,
{ simp [hs_empty, hpsa a], },
have hps : ∀ (x : M), x ∈ s → p x, from λ x hxs, hpsa x (mem_cons_of_mem hxs),
exact p_mul a s.prod (hpsa a (mem_cons_self a s)) (hs hs_empty hps),
end
@[to_additive le_sum_nonempty_of_subadditive_on_pred]
lemma le_prod_nonempty_of_submultiplicative_on_pred [comm_monoid α] [ordered_comm_monoid β]
(f : α → β) (p : α → Prop) (h_mul : ∀ a b, p a → p b → f (a * b) ≤ f a * f b)
(hp_mul : ∀ a b, p a → p b → p (a * b)) (s : multiset α) (hs_nonempty : s ≠ ∅)
(hs : ∀ a, a ∈ s → p a) :
f s.prod ≤ (s.map f).prod :=
begin
revert s,
refine multiset.induction _ _,
{ intro h,
exfalso,
exact h rfl, },
rintros a s hs hsa_nonempty hsa_prop,
rw [prod_cons, map_cons, prod_cons],
by_cases hs_empty : s = ∅,
{ simp [hs_empty], },
have hsa_restrict : (∀ x, x ∈ s → p x), from λ x hx, hsa_prop x (mem_cons_of_mem hx),
have hp_sup : p s.prod,
from prod_induction_nonempty p hp_mul hs_empty hsa_restrict,
have hp_a : p a, from hsa_prop a (mem_cons_self a s),
exact (h_mul a _ hp_a hp_sup).trans (mul_le_mul_left' (hs hs_empty hsa_restrict) _),
end
@[to_additive le_sum_nonempty_of_subadditive]
lemma le_prod_nonempty_of_submultiplicative [comm_monoid α] [ordered_comm_monoid β]
(f : α → β) (h_mul : ∀ a b, f (a * b) ≤ f a * f b) (s : multiset α) (hs_nonempty : s ≠ ∅) :
f s.prod ≤ (s.map f).prod :=
le_prod_nonempty_of_submultiplicative_on_pred f (λ i, true) (by simp [h_mul]) (by simp) s
hs_nonempty (by simp)
lemma abs_sum_le_sum_abs [linear_ordered_field α] {s : multiset α} :
abs s.sum ≤ (s.map abs).sum :=
le_sum_of_subadditive _ abs_zero abs_add s
theorem dvd_sum [comm_semiring α] {a : α} {s : multiset α} : (∀ x ∈ s, a ∣ x) → a ∣ s.sum :=
multiset.induction_on s (λ _, dvd_zero _)
(λ x s ih h, by rw sum_cons; exact dvd_add
(h _ (mem_cons_self _ _)) (ih (λ y hy, h _ (mem_cons.2 (or.inr hy)))))
@[simp] theorem sum_map_singleton (s : multiset α) : (s.map (λ a, a ::ₘ 0)).sum = s :=
multiset.induction_on s (by simp) (by simp)
/-! ### Join -/
/-- `join S`, where `S` is a multiset of multisets, is the lift of the list join
operation, that is, the union of all the sets.
join {{1, 2}, {1, 2}, {0, 1}} = {0, 1, 1, 1, 2, 2} -/
def join : multiset (multiset α) → multiset α := sum
theorem coe_join : ∀ L : list (list α),
join (L.map (@coe _ (multiset α) _) : multiset (multiset α)) = L.join
| [] := rfl
| (l :: L) := congr_arg (λ s : multiset α, ↑l + s) (coe_join L)
@[simp] theorem join_zero : @join α 0 = 0 := rfl
@[simp] theorem join_cons (s S) : @join α (s ::ₘ S) = s + join S :=
sum_cons _ _
@[simp] theorem join_add (S T) : @join α (S + T) = join S + join T :=
sum_add _ _
@[simp] theorem mem_join {a S} : a ∈ @join α S ↔ ∃ s ∈ S, a ∈ s :=
multiset.induction_on S (by simp) $
by simp [or_and_distrib_right, exists_or_distrib] {contextual := tt}
@[simp] theorem card_join (S) : card (@join α S) = sum (map card S) :=
multiset.induction_on S (by simp) (by simp)
/-! ### `multiset.bind` -/
/-- `bind s f` is the monad bind operation, defined as `join (map f s)`.
It is the union of `f a` as `a` ranges over `s`. -/
def bind (s : multiset α) (f : α → multiset β) : multiset β :=
join (map f s)
@[simp] theorem coe_bind (l : list α) (f : α → list β) :
@bind α β l (λ a, f a) = l.bind f :=
by rw [list.bind, ← coe_join, list.map_map]; refl
@[simp] theorem zero_bind (f : α → multiset β) : bind 0 f = 0 := rfl
@[simp] theorem cons_bind (a s) (f : α → multiset β) : bind (a ::ₘ s) f = f a + bind s f :=
by simp [bind]
@[simp] theorem add_bind (s t) (f : α → multiset β) : bind (s + t) f = bind s f + bind t f :=
by simp [bind]
@[simp] theorem bind_zero (s : multiset α) : bind s (λa, 0 : α → multiset β) = 0 :=
by simp [bind, join]
@[simp] theorem bind_add (s : multiset α) (f g : α → multiset β) :
bind s (λa, f a + g a) = bind s f + bind s g :=
by simp [bind, join]
@[simp] theorem bind_cons (s : multiset α) (f : α → β) (g : α → multiset β) :
bind s (λa, f a ::ₘ g a) = map f s + bind s g :=
multiset.induction_on s (by simp) (by simp [add_comm, add_left_comm] {contextual := tt})
@[simp] theorem mem_bind {b s} {f : α → multiset β} : b ∈ bind s f ↔ ∃ a ∈ s, b ∈ f a :=
by simp [bind]; simp [-exists_and_distrib_right, exists_and_distrib_right.symm];
rw exists_swap; simp [and_assoc]
@[simp] theorem card_bind (s) (f : α → multiset β) : card (bind s f) = sum (map (card ∘ f) s) :=
by simp [bind]
lemma bind_congr {f g : α → multiset β} {m : multiset α} :
(∀a∈m, f a = g a) → bind m f = bind m g :=
by simp [bind] {contextual := tt}
lemma bind_hcongr {β' : Type*} {m : multiset α} {f : α → multiset β} {f' : α → multiset β'}
(h : β = β') (hf : ∀a∈m, f a == f' a) : bind m f == bind m f' :=
begin subst h, simp at hf, simp [bind_congr hf] end
lemma map_bind (m : multiset α) (n : α → multiset β) (f : β → γ) :
map f (bind m n) = bind m (λa, map f (n a)) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
lemma bind_map (m : multiset α) (n : β → multiset γ) (f : α → β) :
bind (map f m) n = bind m (λa, n (f a)) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
lemma bind_assoc {s : multiset α} {f : α → multiset β} {g : β → multiset γ} :
(s.bind f).bind g = s.bind (λa, (f a).bind g) :=
multiset.induction_on s (by simp) (by simp {contextual := tt})
lemma bind_bind (m : multiset α) (n : multiset β) {f : α → β → multiset γ} :
(bind m $ λa, bind n $ λb, f a b) = (bind n $ λb, bind m $ λa, f a b) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
lemma bind_map_comm (m : multiset α) (n : multiset β) {f : α → β → γ} :
(bind m $ λa, n.map $ λb, f a b) = (bind n $ λb, m.map $ λa, f a b) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
@[simp, to_additive]
lemma prod_bind [comm_monoid β] (s : multiset α) (t : α → multiset β) :
prod (bind s t) = prod (s.map $ λa, prod (t a)) :=
multiset.induction_on s (by simp) (assume a s ih, by simp [ih, cons_bind])
/-! ### Product of two `multiset`s -/
/-- The multiplicity of `(a, b)` in `product s t` is
the product of the multiplicity of `a` in `s` and `b` in `t`. -/
def product (s : multiset α) (t : multiset β) : multiset (α × β) :=
s.bind $ λ a, t.map $ prod.mk a
@[simp] theorem coe_product (l₁ : list α) (l₂ : list β) :
@product α β l₁ l₂ = l₁.product l₂ :=
by rw [product, list.product, ← coe_bind]; simp
@[simp] theorem zero_product (t) : @product α β 0 t = 0 := rfl
@[simp] theorem cons_product (a : α) (s : multiset α) (t : multiset β) :
product (a ::ₘ s) t = map (prod.mk a) t + product s t :=
by simp [product]
@[simp] theorem product_singleton (a : α) (b : β) : product (a ::ₘ 0) (b ::ₘ 0) = (a,b) ::ₘ 0 := rfl
@[simp] theorem add_product (s t : multiset α) (u : multiset β) :
product (s + t) u = product s u + product t u :=
by simp [product]
@[simp] theorem product_add (s : multiset α) : ∀ t u : multiset β,
product s (t + u) = product s t + product s u :=
multiset.induction_on s (λ t u, rfl) $ λ a s IH t u,
by rw [cons_product, IH]; simp; cc
@[simp] theorem mem_product {s t} : ∀ {p : α × β}, p ∈ @product α β s t ↔ p.1 ∈ s ∧ p.2 ∈ t
| (a, b) := by simp [product, and.left_comm]
@[simp] theorem card_product (s : multiset α) (t : multiset β) :
card (product s t) = card s * card t :=
by simp [product, repeat, (∘), mul_comm]
/-! ### Sigma multiset -/
section
variable {σ : α → Type*}
/-- `sigma s t` is the dependent version of `product`. It is the sum of
`(a, b)` as `a` ranges over `s` and `b` ranges over `t a`. -/
protected def sigma (s : multiset α) (t : Π a, multiset (σ a)) : multiset (Σ a, σ a) :=
s.bind $ λ a, (t a).map $ sigma.mk a
@[simp] theorem coe_sigma (l₁ : list α) (l₂ : Π a, list (σ a)) :
@multiset.sigma α σ l₁ (λ a, l₂ a) = l₁.sigma l₂ :=
by rw [multiset.sigma, list.sigma, ← coe_bind]; simp
@[simp] theorem zero_sigma (t) : @multiset.sigma α σ 0 t = 0 := rfl
@[simp] theorem cons_sigma (a : α) (s : multiset α) (t : Π a, multiset (σ a)) :
(a ::ₘ s).sigma t = map (sigma.mk a) (t a) + s.sigma t :=
by simp [multiset.sigma]
@[simp] theorem sigma_singleton (a : α) (b : α → β) :
(a ::ₘ 0).sigma (λ a, b a ::ₘ 0) = ⟨a, b a⟩ ::ₘ 0 := rfl
@[simp] theorem add_sigma (s t : multiset α) (u : Π a, multiset (σ a)) :
(s + t).sigma u = s.sigma u + t.sigma u :=
by simp [multiset.sigma]
@[simp] theorem sigma_add (s : multiset α) : ∀ t u : Π a, multiset (σ a),
s.sigma (λ a, t a + u a) = s.sigma t + s.sigma u :=
multiset.induction_on s (λ t u, rfl) $ λ a s IH t u,
by rw [cons_sigma, IH]; simp; cc
@[simp] theorem mem_sigma {s t} : ∀ {p : Σ a, σ a},
p ∈ @multiset.sigma α σ s t ↔ p.1 ∈ s ∧ p.2 ∈ t p.1
| ⟨a, b⟩ := by simp [multiset.sigma, and_assoc, and.left_comm]
@[simp] theorem card_sigma (s : multiset α) (t : Π a, multiset (σ a)) :
card (s.sigma t) = sum (map (λ a, card (t a)) s) :=
by simp [multiset.sigma, (∘)]
end
/-! ### Map for partial functions -/
/-- Lift of the list `pmap` operation. Map a partial function `f` over a multiset
`s` whose elements are all in the domain of `f`. -/
def pmap {p : α → Prop} (f : Π a, p a → β) (s : multiset α) : (∀ a ∈ s, p a) → multiset β :=
quot.rec_on s (λ l H, ↑(pmap f l H)) $ λ l₁ l₂ (pp : l₁ ~ l₂),
funext $ λ (H₂ : ∀ a ∈ l₂, p a),
have H₁ : ∀ a ∈ l₁, p a, from λ a h, H₂ a (pp.subset h),
have ∀ {s₂ e H}, @eq.rec (multiset α) l₁
(λ s, (∀ a ∈ s, p a) → multiset β) (λ _, ↑(pmap f l₁ H₁))
s₂ e H = ↑(pmap f l₁ H₁), by intros s₂ e _; subst e,
this.trans $ quot.sound $ pp.pmap f
@[simp] theorem coe_pmap {p : α → Prop} (f : Π a, p a → β)
(l : list α) (H : ∀ a ∈ l, p a) : pmap f l H = l.pmap f H := rfl
@[simp] lemma pmap_zero {p : α → Prop} (f : Π a, p a → β) (h : ∀a∈(0:multiset α), p a) :
pmap f 0 h = 0 := rfl
@[simp] lemma pmap_cons {p : α → Prop} (f : Π a, p a → β) (a : α) (m : multiset α) :
∀(h : ∀b∈a ::ₘ m, p b), pmap f (a ::ₘ m) h =
f a (h a (mem_cons_self a m)) ::ₘ pmap f m (λa ha, h a $ mem_cons_of_mem ha) :=
quotient.induction_on m $ assume l h, rfl
/-- "Attach" a proof that `a ∈ s` to each element `a` in `s` to produce
a multiset on `{x // x ∈ s}`. -/
def attach (s : multiset α) : multiset {x // x ∈ s} := pmap subtype.mk s (λ a, id)
@[simp] theorem coe_attach (l : list α) :
@eq (multiset {x // x ∈ l}) (@attach α l) l.attach := rfl
theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {s : multiset α} (hx : x ∈ s) :
sizeof x < sizeof s := by
{ induction s with l a b, exact list.sizeof_lt_sizeof_of_mem hx, refl }
theorem pmap_eq_map (p : α → Prop) (f : α → β) (s : multiset α) :
∀ H, @pmap _ _ p (λ a _, f a) s H = map f s :=
quot.induction_on s $ λ l H, congr_arg coe $ pmap_eq_map p f l H
theorem pmap_congr {p q : α → Prop} {f : Π a, p a → β} {g : Π a, q a → β}
(s : multiset α) {H₁ H₂} (h : ∀ a h₁ h₂, f a h₁ = g a h₂) :
pmap f s H₁ = pmap g s H₂ :=
quot.induction_on s (λ l H₁ H₂, congr_arg coe $ pmap_congr l h) H₁ H₂
theorem map_pmap {p : α → Prop} (g : β → γ) (f : Π a, p a → β)
(s) : ∀ H, map g (pmap f s H) = pmap (λ a h, g (f a h)) s H :=
quot.induction_on s $ λ l H, congr_arg coe $ map_pmap g f l H
theorem pmap_eq_map_attach {p : α → Prop} (f : Π a, p a → β)
(s) : ∀ H, pmap f s H = s.attach.map (λ x, f x.1 (H _ x.2)) :=
quot.induction_on s $ λ l H, congr_arg coe $ pmap_eq_map_attach f l H
theorem attach_map_val (s : multiset α) : s.attach.map subtype.val = s :=
quot.induction_on s $ λ l, congr_arg coe $ attach_map_val l
@[simp] theorem mem_attach (s : multiset α) : ∀ x, x ∈ s.attach :=
quot.induction_on s $ λ l, mem_attach _
@[simp] theorem mem_pmap {p : α → Prop} {f : Π a, p a → β}
{s H b} : b ∈ pmap f s H ↔ ∃ a (h : a ∈ s), f a (H a h) = b :=
quot.induction_on s (λ l H, mem_pmap) H
@[simp] theorem card_pmap {p : α → Prop} (f : Π a, p a → β)
(s H) : card (pmap f s H) = card s :=
quot.induction_on s (λ l H, length_pmap) H
@[simp] theorem card_attach {m : multiset α} : card (attach m) = card m := card_pmap _ _ _
@[simp] lemma attach_zero : (0 : multiset α).attach = 0 := rfl
lemma attach_cons (a : α) (m : multiset α) :
(a ::ₘ m).attach = ⟨a, mem_cons_self a m⟩ ::ₘ (m.attach.map $ λp, ⟨p.1, mem_cons_of_mem p.2⟩) :=
quotient.induction_on m $ assume l, congr_arg coe $ congr_arg (list.cons _) $
by rw [list.map_pmap]; exact list.pmap_congr _ (assume a' h₁ h₂, subtype.eq rfl)
section decidable_pi_exists
variables {m : multiset α}
protected def decidable_forall_multiset {p : α → Prop} [hp : ∀a, decidable (p a)] :
decidable (∀a∈m, p a) :=
quotient.rec_on_subsingleton m (λl, decidable_of_iff (∀a∈l, p a) $ by simp)
instance decidable_dforall_multiset {p : Πa∈m, Prop} [hp : ∀a (h : a ∈ m), decidable (p a h)] :
decidable (∀a (h : a ∈ m), p a h) :=
decidable_of_decidable_of_iff
(@multiset.decidable_forall_multiset {a // a ∈ m} m.attach (λa, p a.1 a.2) _)
(iff.intro (assume h a ha, h ⟨a, ha⟩ (mem_attach _ _)) (assume h ⟨a, ha⟩ _, h _ _))
/-- decidable equality for functions whose domain is bounded by multisets -/
instance decidable_eq_pi_multiset {β : α → Type*} [h : ∀a, decidable_eq (β a)] :
decidable_eq (Πa∈m, β a) :=
assume f g, decidable_of_iff (∀a (h : a ∈ m), f a h = g a h) (by simp [function.funext_iff])
def decidable_exists_multiset {p : α → Prop} [decidable_pred p] :
decidable (∃ x ∈ m, p x) :=
quotient.rec_on_subsingleton m list.decidable_exists_mem
instance decidable_dexists_multiset {p : Πa∈m, Prop} [hp : ∀a (h : a ∈ m), decidable (p a h)] :
decidable (∃a (h : a ∈ m), p a h) :=
decidable_of_decidable_of_iff
(@multiset.decidable_exists_multiset {a // a ∈ m} m.attach (λa, p a.1 a.2) _)
(iff.intro (λ ⟨⟨a, ha₁⟩, _, ha₂⟩, ⟨a, ha₁, ha₂⟩)
(λ ⟨a, ha₁, ha₂⟩, ⟨⟨a, ha₁⟩, mem_attach _ _, ha₂⟩))
end decidable_pi_exists
/-! ### Subtraction -/
section
variables [decidable_eq α] {s t u : multiset α} {a b : α}
/-- `s - t` is the multiset such that
`count a (s - t) = count a s - count a t` for all `a`. -/
protected def sub (s t : multiset α) : multiset α :=
quotient.lift_on₂ s t (λ l₁ l₂, (l₁.diff l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂,
quot.sound $ p₁.diff p₂
instance : has_sub (multiset α) := ⟨multiset.sub⟩
@[simp] theorem coe_sub (s t : list α) : (s - t : multiset α) = (s.diff t : list α) := rfl
theorem sub_eq_fold_erase (s t : multiset α) : s - t = foldl erase erase_comm s t :=
quotient.induction_on₂ s t $ λ l₁ l₂,
show ↑(l₁.diff l₂) = foldl erase erase_comm ↑l₁ ↑l₂,
by { rw diff_eq_foldl l₁ l₂, symmetry, exact foldl_hom _ _ _ _ _ (λ x y, rfl) }
@[simp] theorem sub_zero (s : multiset α) : s - 0 = s :=
quot.induction_on s $ λ l, rfl
@[simp] theorem sub_cons (a : α) (s t : multiset α) : s - a ::ₘ t = s.erase a - t :=
quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ diff_cons _ _ _
theorem add_sub_of_le (h : s ≤ t) : s + (t - s) = t :=
begin
revert t,
refine multiset.induction_on s (by simp) (λ a s IH t h, _),
have := cons_erase (mem_of_le h (mem_cons_self _ _)),
rw [cons_add, sub_cons, IH, this],
exact (cons_le_cons_iff a).1 (this.symm ▸ h)
end
theorem sub_add' : s - (t + u) = s - t - u :=
quotient.induction_on₃ s t u $
λ l₁ l₂ l₃, congr_arg coe $ diff_append _ _ _
theorem sub_add_cancel (h : t ≤ s) : s - t + t = s :=
by rw [add_comm, add_sub_of_le h]
@[simp] theorem add_sub_cancel_left (s : multiset α) : ∀ t, s + t - s = t :=
multiset.induction_on s (by simp)
(λ a s IH t, by rw [cons_add, sub_cons, erase_cons_head, IH])
@[simp] theorem add_sub_cancel (s t : multiset α) : s + t - t = s :=
by rw [add_comm, add_sub_cancel_left]
theorem sub_le_sub_right (h : s ≤ t) (u) : s - u ≤ t - u :=
by revert s t h; exact
multiset.induction_on u (by simp {contextual := tt})
(λ a u IH s t h, by simp [IH, erase_le_erase a h])
theorem sub_le_sub_left (h : s ≤ t) : ∀ u, u - t ≤ u - s :=
le_induction_on h $ λ l₁ l₂ h, begin
induction h with l₁ l₂ a s IH l₁ l₂ a s IH; intro u,
{ refl },
{ rw [← cons_coe, sub_cons],
exact le_trans (sub_le_sub_right (erase_le _ _) _) (IH u) },
{ rw [← cons_coe, sub_cons, ← cons_coe, sub_cons],
exact IH _ }
end
theorem sub_le_iff_le_add : s - t ≤ u ↔ s ≤ u + t :=
by revert s; exact
multiset.induction_on t (by simp)
(λ a t IH s, by simp [IH, erase_le_iff_le_cons])
theorem le_sub_add (s t : multiset α) : s ≤ s - t + t :=
sub_le_iff_le_add.1 (le_refl _)
theorem sub_le_self (s t : multiset α) : s - t ≤ s :=
sub_le_iff_le_add.2 (le_add_right _ _)
@[simp] theorem card_sub {s t : multiset α} (h : t ≤ s) : card (s - t) = card s - card t :=
(nat.sub_eq_of_eq_add $ by rw [add_comm, ← card_add, sub_add_cancel h]).symm
/-! ### Union -/
/-- `s ∪ t` is the lattice join operation with respect to the
multiset `≤`. The multiplicity of `a` in `s ∪ t` is the maximum
of the multiplicities in `s` and `t`. -/
def union (s t : multiset α) : multiset α := s - t + t
instance : has_union (multiset α) := ⟨union⟩
theorem union_def (s t : multiset α) : s ∪ t = s - t + t := rfl
theorem le_union_left (s t : multiset α) : s ≤ s ∪ t := le_sub_add _ _
theorem le_union_right (s t : multiset α) : t ≤ s ∪ t := le_add_left _ _
theorem eq_union_left : t ≤ s → s ∪ t = s := sub_add_cancel
theorem union_le_union_right (h : s ≤ t) (u) : s ∪ u ≤ t ∪ u :=
add_le_add_right (sub_le_sub_right h _) u
theorem union_le (h₁ : s ≤ u) (h₂ : t ≤ u) : s ∪ t ≤ u :=
by rw ← eq_union_left h₂; exact union_le_union_right h₁ t
@[simp] theorem mem_union : a ∈ s ∪ t ↔ a ∈ s ∨ a ∈ t :=
⟨λ h, (mem_add.1 h).imp_left (mem_of_le $ sub_le_self _ _),
or.rec (mem_of_le $ le_union_left _ _) (mem_of_le $ le_union_right _ _)⟩
@[simp] theorem map_union [decidable_eq β] {f : α → β} (finj : function.injective f)
{s t : multiset α} :
map f (s ∪ t) = map f s ∪ map f t :=
quotient.induction_on₂ s t $ λ l₁ l₂,
congr_arg coe (by rw [list.map_append f, list.map_diff finj])
/-! ### Intersection -/
/-- `s ∩ t` is the lattice meet operation with respect to the
multiset `≤`. The multiplicity of `a` in `s ∩ t` is the minimum
of the multiplicities in `s` and `t`. -/
def inter (s t : multiset α) : multiset α :=
quotient.lift_on₂ s t (λ l₁ l₂, (l₁.bag_inter l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂,
quot.sound $ p₁.bag_inter p₂
instance : has_inter (multiset α) := ⟨inter⟩
@[simp] theorem inter_zero (s : multiset α) : s ∩ 0 = 0 :=
quot.induction_on s $ λ l, congr_arg coe l.bag_inter_nil
@[simp] theorem zero_inter (s : multiset α) : 0 ∩ s = 0 :=
quot.induction_on s $ λ l, congr_arg coe l.nil_bag_inter
@[simp] theorem cons_inter_of_pos {a} (s : multiset α) {t} :
a ∈ t → (a ::ₘ s) ∩ t = a ::ₘ s ∩ t.erase a :=
quotient.induction_on₂ s t $ λ l₁ l₂ h,
congr_arg coe $ cons_bag_inter_of_pos _ h
@[simp] theorem cons_inter_of_neg {a} (s : multiset α) {t} :
a ∉ t → (a ::ₘ s) ∩ t = s ∩ t :=
quotient.induction_on₂ s t $ λ l₁ l₂ h,
congr_arg coe $ cons_bag_inter_of_neg _ h
theorem inter_le_left (s t : multiset α) : s ∩ t ≤ s :=
quotient.induction_on₂ s t $ λ l₁ l₂,
(bag_inter_sublist_left _ _).subperm
theorem inter_le_right (s : multiset α) : ∀ t, s ∩ t ≤ t :=
multiset.induction_on s (λ t, (zero_inter t).symm ▸ zero_le _) $
λ a s IH t, if h : a ∈ t
then by simpa [h] using cons_le_cons a (IH (t.erase a))
else by simp [h, IH]
theorem le_inter (h₁ : s ≤ t) (h₂ : s ≤ u) : s ≤ t ∩ u :=
begin
revert s u, refine multiset.induction_on t _ (λ a t IH, _); intros,
{ simp [h₁] },
by_cases a ∈ u,
{ rw [cons_inter_of_pos _ h, ← erase_le_iff_le_cons],
exact IH (erase_le_iff_le_cons.2 h₁) (erase_le_erase _ h₂) },
{ rw cons_inter_of_neg _ h,
exact IH ((le_cons_of_not_mem $ mt (mem_of_le h₂) h).1 h₁) h₂ }
end
@[simp] theorem mem_inter : a ∈ s ∩ t ↔ a ∈ s ∧ a ∈ t :=
⟨λ h, ⟨mem_of_le (inter_le_left _ _) h, mem_of_le (inter_le_right _ _) h⟩,
λ ⟨h₁, h₂⟩, by rw [← cons_erase h₁, cons_inter_of_pos _ h₂]; apply mem_cons_self⟩
instance : lattice (multiset α) :=
{ sup := (∪),
sup_le := @union_le _ _,
le_sup_left := le_union_left,
le_sup_right := le_union_right,
inf := (∩),
le_inf := @le_inter _ _,
inf_le_left := inter_le_left,
inf_le_right := inter_le_right,
..@multiset.partial_order α }
@[simp] theorem sup_eq_union (s t : multiset α) : s ⊔ t = s ∪ t := rfl
@[simp] theorem inf_eq_inter (s t : multiset α) : s ⊓ t = s ∩ t := rfl
@[simp] theorem le_inter_iff : s ≤ t ∩ u ↔ s ≤ t ∧ s ≤ u := le_inf_iff
@[simp] theorem union_le_iff : s ∪ t ≤ u ↔ s ≤ u ∧ t ≤ u := sup_le_iff
instance : semilattice_inf_bot (multiset α) :=
{ bot := 0, bot_le := zero_le, ..multiset.lattice }
theorem union_comm (s t : multiset α) : s ∪ t = t ∪ s := sup_comm
theorem inter_comm (s t : multiset α) : s ∩ t = t ∩ s := inf_comm
theorem eq_union_right (h : s ≤ t) : s ∪ t = t :=
by rw [union_comm, eq_union_left h]
theorem union_le_union_left (h : s ≤ t) (u) : u ∪ s ≤ u ∪ t :=
sup_le_sup_left h _
theorem union_le_add (s t : multiset α) : s ∪ t ≤ s + t :=
union_le (le_add_right _ _) (le_add_left _ _)
theorem union_add_distrib (s t u : multiset α) : (s ∪ t) + u = (s + u) ∪ (t + u) :=
by simpa [(∪), union, eq_comm, add_assoc] using show s + u - (t + u) = s - t,
by rw [add_comm t, sub_add', add_sub_cancel]
theorem add_union_distrib (s t u : multiset α) : s + (t ∪ u) = (s + t) ∪ (s + u) :=
by rw [add_comm, union_add_distrib, add_comm s, add_comm s]
theorem cons_union_distrib (a : α) (s t : multiset α) : a ::ₘ (s ∪ t) = (a ::ₘ s) ∪ (a ::ₘ t) :=
by simpa using add_union_distrib (a ::ₘ 0) s t
theorem inter_add_distrib (s t u : multiset α) : (s ∩ t) + u = (s + u) ∩ (t + u) :=
begin
by_contra h,
cases lt_iff_cons_le.1 (lt_of_le_of_ne (le_inter
(add_le_add_right (inter_le_left s t) u)
(add_le_add_right (inter_le_right s t) u)) h) with a hl,
rw ← cons_add at hl,
exact not_le_of_lt (lt_cons_self (s ∩ t) a) (le_inter
(le_of_add_le_add_right (le_trans hl (inter_le_left _ _)))
(le_of_add_le_add_right (le_trans hl (inter_le_right _ _))))
end
theorem add_inter_distrib (s t u : multiset α) : s + (t ∩ u) = (s + t) ∩ (s + u) :=
by rw [add_comm, inter_add_distrib, add_comm s, add_comm s]
theorem cons_inter_distrib (a : α) (s t : multiset α) : a ::ₘ (s ∩ t) = (a ::ₘ s) ∩ (a ::ₘ t) :=
by simp
theorem union_add_inter (s t : multiset α) : s ∪ t + s ∩ t = s + t :=
begin
apply le_antisymm,
{ rw union_add_distrib,
refine union_le (add_le_add_left (inter_le_right _ _) _) _,
rw add_comm, exact add_le_add_right (inter_le_left _ _) _ },
{ rw [add_comm, add_inter_distrib],
refine le_inter (add_le_add_right (le_union_right _ _) _) _,
rw add_comm, exact add_le_add_right (le_union_left _ _) _ }
end
theorem sub_add_inter (s t : multiset α) : s - t + s ∩ t = s :=
begin
rw [inter_comm],
revert s, refine multiset.induction_on t (by simp) (λ a t IH s, _),
by_cases a ∈ s,
{ rw [cons_inter_of_pos _ h, sub_cons, add_cons, IH, cons_erase h] },
{ rw [cons_inter_of_neg _ h, sub_cons, erase_of_not_mem h, IH] }
end
theorem sub_inter (s t : multiset α) : s - (s ∩ t) = s - t :=
add_right_cancel $
by rw [sub_add_inter s t, sub_add_cancel (inter_le_left _ _)]
end
/-! ### `multiset.filter` -/
section
variables (p : α → Prop) [decidable_pred p]
/-- `filter p s` returns the elements in `s` (with the same multiplicities)
which satisfy `p`, and removes the rest. -/
def filter (s : multiset α) : multiset α :=
quot.lift_on s (λ l, (filter p l : multiset α))
(λ l₁ l₂ h, quot.sound $ h.filter p)
@[simp] theorem coe_filter (l : list α) : filter p (↑l) = l.filter p := rfl
@[simp] theorem filter_zero : filter p 0 = 0 := rfl
lemma filter_congr {p q : α → Prop} [decidable_pred p] [decidable_pred q]
{s : multiset α} : (∀ x ∈ s, p x ↔ q x) → filter p s = filter q s :=
quot.induction_on s $ λ l h, congr_arg coe $ filter_congr h
@[simp] theorem filter_add (s t : multiset α) : filter p (s + t) = filter p s + filter p t :=
quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ filter_append _ _
@[simp] theorem filter_le (s : multiset α) : filter p s ≤ s :=
quot.induction_on s $ λ l, (filter_sublist _).subperm
@[simp] theorem filter_subset (s : multiset α) : filter p s ⊆ s :=
subset_of_le $ filter_le _ _
theorem filter_le_filter {s t} (h : s ≤ t) : filter p s ≤ filter p t :=
le_induction_on h $ λ l₁ l₂ h, (filter_sublist_filter p h).subperm
variable {p}
@[simp] theorem filter_cons_of_pos {a : α} (s) : p a → filter p (a ::ₘ s) = a ::ₘ filter p s :=
quot.induction_on s $ λ l h, congr_arg coe $ filter_cons_of_pos l h
@[simp] theorem filter_cons_of_neg {a : α} (s) : ¬ p a → filter p (a ::ₘ s) = filter p s :=
quot.induction_on s $ λ l h, @congr_arg _ _ _ _ coe $ filter_cons_of_neg l h
@[simp] theorem mem_filter {a : α} {s} : a ∈ filter p s ↔ a ∈ s ∧ p a :=
quot.induction_on s $ λ l, mem_filter
theorem of_mem_filter {a : α} {s} (h : a ∈ filter p s) : p a :=
(mem_filter.1 h).2
theorem mem_of_mem_filter {a : α} {s} (h : a ∈ filter p s) : a ∈ s :=
(mem_filter.1 h).1
theorem mem_filter_of_mem {a : α} {l} (m : a ∈ l) (h : p a) : a ∈ filter p l :=
mem_filter.2 ⟨m, h⟩
theorem filter_eq_self {s} : filter p s = s ↔ ∀ a ∈ s, p a :=
quot.induction_on s $ λ l, iff.trans ⟨λ h,
eq_of_sublist_of_length_eq (filter_sublist _) (@congr_arg _ _ _ _ card h),
congr_arg coe⟩ filter_eq_self
theorem filter_eq_nil {s} : filter p s = 0 ↔ ∀ a ∈ s, ¬p a :=
quot.induction_on s $ λ l, iff.trans ⟨λ h,
eq_nil_of_length_eq_zero (@congr_arg _ _ _ _ card h),
congr_arg coe⟩ filter_eq_nil
theorem le_filter {s t} : s ≤ filter p t ↔ s ≤ t ∧ ∀ a ∈ s, p a :=
⟨λ h, ⟨le_trans h (filter_le _ _), λ a m, of_mem_filter (mem_of_le h m)⟩,
λ ⟨h, al⟩, filter_eq_self.2 al ▸ filter_le_filter p h⟩
variable (p)
@[simp] theorem filter_sub [decidable_eq α] (s t : multiset α) :
filter p (s - t) = filter p s - filter p t :=
begin
revert s, refine multiset.induction_on t (by simp) (λ a t IH s, _),
rw [sub_cons, IH],
by_cases p a,
{ rw [filter_cons_of_pos _ h, sub_cons], congr,
by_cases m : a ∈ s,
{ rw [← cons_inj_right a, ← filter_cons_of_pos _ h,
cons_erase (mem_filter_of_mem m h), cons_erase m] },
{ rw [erase_of_not_mem m, erase_of_not_mem (mt mem_of_mem_filter m)] } },
{ rw [filter_cons_of_neg _ h],
by_cases m : a ∈ s,
{ rw [(by rw filter_cons_of_neg _ h : filter p (erase s a) = filter p (a ::ₘ erase s a)),
cons_erase m] },
{ rw [erase_of_not_mem m] } }
end
@[simp] theorem filter_union [decidable_eq α] (s t : multiset α) :
filter p (s ∪ t) = filter p s ∪ filter p t :=
by simp [(∪), union]
@[simp] theorem filter_inter [decidable_eq α] (s t : multiset α) :
filter p (s ∩ t) = filter p s ∩ filter p t :=
le_antisymm (le_inter
(filter_le_filter _ $ inter_le_left _ _)
(filter_le_filter _ $ inter_le_right _ _)) $ le_filter.2
⟨inf_le_inf (filter_le _ _) (filter_le _ _),
λ a h, of_mem_filter (mem_of_le (inter_le_left _ _) h)⟩
@[simp] theorem filter_filter (q) [decidable_pred q] (s : multiset α) :
filter p (filter q s) = filter (λ a, p a ∧ q a) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_filter p q l
theorem filter_add_filter (q) [decidable_pred q] (s : multiset α) :
filter p s + filter q s = filter (λ a, p a ∨ q a) s + filter (λ a, p a ∧ q a) s :=
multiset.induction_on s rfl $ λ a s IH,
by by_cases p a; by_cases q a; simp *
theorem filter_add_not (s : multiset α) :
filter p s + filter (λ a, ¬ p a) s = s :=
by rw [filter_add_filter, filter_eq_self.2, filter_eq_nil.2]; simp [decidable.em]
theorem map_filter (f : β → α) (s : multiset β) :
filter p (map f s) = map f (filter (p ∘ f) s) :=
quot.induction_on s (λ l, by simp [map_filter])
/-! ### Simultaneously filter and map elements of a multiset -/
/-- `filter_map f s` is a combination filter/map operation on `s`.
The function `f : α → option β` is applied to each element of `s`;
if `f a` is `some b` then `b` is added to the result, otherwise
`a` is removed from the resulting multiset. -/
def filter_map (f : α → option β) (s : multiset α) : multiset β :=
quot.lift_on s (λ l, (filter_map f l : multiset β))
(λ l₁ l₂ h, quot.sound $ h.filter_map f)
@[simp] theorem coe_filter_map (f : α → option β) (l : list α) :
filter_map f l = l.filter_map f := rfl
@[simp] theorem filter_map_zero (f : α → option β) : filter_map f 0 = 0 := rfl
@[simp] theorem filter_map_cons_none {f : α → option β} (a : α) (s : multiset α) (h : f a = none) :
filter_map f (a ::ₘ s) = filter_map f s :=
quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ filter_map_cons_none a l h
@[simp] theorem filter_map_cons_some (f : α → option β)
(a : α) (s : multiset α) {b : β} (h : f a = some b) :
filter_map f (a ::ₘ s) = b ::ₘ filter_map f s :=
quot.induction_on s $ λ l, @congr_arg _ _ _ _ coe $ filter_map_cons_some f a l h
theorem filter_map_eq_map (f : α → β) : filter_map (some ∘ f) = map f :=
funext $ λ s, quot.induction_on s $ λ l,
@congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_map f) l
theorem filter_map_eq_filter : filter_map (option.guard p) = filter p :=
funext $ λ s, quot.induction_on s $ λ l,
@congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_filter p) l
theorem filter_map_filter_map (f : α → option β) (g : β → option γ) (s : multiset α) :
filter_map g (filter_map f s) = filter_map (λ x, (f x).bind g) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_map_filter_map f g l
theorem map_filter_map (f : α → option β) (g : β → γ) (s : multiset α) :
map g (filter_map f s) = filter_map (λ x, (f x).map g) s :=
quot.induction_on s $ λ l, congr_arg coe $ map_filter_map f g l
theorem filter_map_map (f : α → β) (g : β → option γ) (s : multiset α) :
filter_map g (map f s) = filter_map (g ∘ f) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_map_map f g l
theorem filter_filter_map (f : α → option β) (p : β → Prop) [decidable_pred p] (s : multiset α) :
filter p (filter_map f s) = filter_map (λ x, (f x).filter p) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_filter_map f p l
theorem filter_map_filter (f : α → option β) (s : multiset α) :
filter_map f (filter p s) = filter_map (λ x, if p x then f x else none) s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_map_filter p f l
@[simp] theorem filter_map_some (s : multiset α) : filter_map some s = s :=
quot.induction_on s $ λ l, congr_arg coe $ filter_map_some l
@[simp] theorem mem_filter_map (f : α → option β) (s : multiset α) {b : β} :
b ∈ filter_map f s ↔ ∃ a, a ∈ s ∧ f a = some b :=
quot.induction_on s $ λ l, mem_filter_map f l
theorem map_filter_map_of_inv (f : α → option β) (g : β → α)
(H : ∀ x : α, (f x).map g = some x) (s : multiset α) :
map g (filter_map f s) = s :=
quot.induction_on s $ λ l, congr_arg coe $ map_filter_map_of_inv f g H l
theorem filter_map_le_filter_map (f : α → option β) {s t : multiset α}
(h : s ≤ t) : filter_map f s ≤ filter_map f t :=
le_induction_on h $ λ l₁ l₂ h, (h.filter_map _).subperm
/-! ### countp -/
/-- `countp p s` counts the number of elements of `s` (with multiplicity) that
satisfy `p`. -/
def countp (s : multiset α) : ℕ :=
quot.lift_on s (countp p) (λ l₁ l₂, perm.countp_eq p)
@[simp] theorem coe_countp (l : list α) : countp p l = l.countp p := rfl
@[simp] theorem countp_zero : countp p 0 = 0 := rfl
variable {p}
@[simp] theorem countp_cons_of_pos {a : α} (s) : p a → countp p (a ::ₘ s) = countp p s + 1 :=
quot.induction_on s $ countp_cons_of_pos p
@[simp] theorem countp_cons_of_neg {a : α} (s) : ¬ p a → countp p (a ::ₘ s) = countp p s :=
quot.induction_on s $ countp_cons_of_neg p
variable (p)
theorem countp_eq_card_filter (s) : countp p s = card (filter p s) :=
quot.induction_on s $ λ l, countp_eq_length_filter _ _
@[simp] theorem countp_add (s t) : countp p (s + t) = countp p s + countp p t :=
by simp [countp_eq_card_filter]
instance countp.is_add_monoid_hom : is_add_monoid_hom (countp p : multiset α → ℕ) :=
{ map_add := countp_add _, map_zero := countp_zero _ }
@[simp] theorem countp_sub [decidable_eq α] {s t : multiset α} (h : t ≤ s) :
countp p (s - t) = countp p s - countp p t :=
by simp [countp_eq_card_filter, h, filter_le_filter]
theorem countp_le_of_le {s t} (h : s ≤ t) : countp p s ≤ countp p t :=
by simpa [countp_eq_card_filter] using card_le_of_le (filter_le_filter p h)
@[simp] theorem countp_filter (q) [decidable_pred q] (s : multiset α) :
countp p (filter q s) = countp (λ a, p a ∧ q a) s :=
by simp [countp_eq_card_filter]
variable {p}
theorem countp_pos {s} : 0 < countp p s ↔ ∃ a ∈ s, p a :=
by simp [countp_eq_card_filter, card_pos_iff_exists_mem]
theorem countp_pos_of_mem {s a} (h : a ∈ s) (pa : p a) : 0 < countp p s :=
countp_pos.2 ⟨_, h, pa⟩
end
/-! ### Multiplicity of an element -/
section
variable [decidable_eq α]
/-- `count a s` is the multiplicity of `a` in `s`. -/
def count (a : α) : multiset α → ℕ := countp (eq a)
@[simp] theorem coe_count (a : α) (l : list α) : count a (↑l) = l.count a := coe_countp _ _
@[simp] theorem count_zero (a : α) : count a 0 = 0 := rfl
@[simp] theorem count_cons_self (a : α) (s : multiset α) : count a (a ::ₘ s) = succ (count a s) :=
countp_cons_of_pos _ rfl
@[simp, priority 990]
theorem count_cons_of_ne {a b : α} (h : a ≠ b) (s : multiset α) : count a (b ::ₘ s) = count a s :=
countp_cons_of_neg _ h
theorem count_le_of_le (a : α) {s t} : s ≤ t → count a s ≤ count a t :=
countp_le_of_le _
theorem count_le_count_cons (a b : α) (s : multiset α) : count a s ≤ count a (b ::ₘ s) :=
count_le_of_le _ (le_cons_self _ _)
theorem count_cons (a b : α) (s : multiset α) :
count a (b ::ₘ s) = count a s + (if a = b then 1 else 0) :=
by by_cases h : a = b; simp [h]
theorem count_singleton (a : α) : count a (a ::ₘ 0) = 1 :=
by simp
@[simp] theorem count_add (a : α) : ∀ s t, count a (s + t) = count a s + count a t :=
countp_add _
instance count.is_add_monoid_hom (a : α) : is_add_monoid_hom (count a : multiset α → ℕ) :=
countp.is_add_monoid_hom _
@[simp] theorem count_smul (a : α) (n s) : count a (n •ℕ s) = n * count a s :=
by induction n; simp [*, succ_nsmul', succ_mul]
theorem count_pos {a : α} {s : multiset α} : 0 < count a s ↔ a ∈ s :=
by simp [count, countp_pos]
@[simp, priority 980]
theorem count_eq_zero_of_not_mem {a : α} {s : multiset α} (h : a ∉ s) : count a s = 0 :=
by_contradiction $ λ h', h $ count_pos.1 (nat.pos_of_ne_zero h')
@[simp] theorem count_eq_zero {a : α} {s : multiset α} : count a s = 0 ↔ a ∉ s :=
iff_not_comm.1 $ count_pos.symm.trans pos_iff_ne_zero
theorem count_ne_zero {a : α} {s : multiset α} : count a s ≠ 0 ↔ a ∈ s :=
by simp [ne.def, count_eq_zero]
@[simp] theorem count_repeat_self (a : α) (n : ℕ) : count a (repeat a n) = n :=
by simp [repeat]
theorem count_repeat (a b : α) (n : ℕ) :
count a (repeat b n) = if (a = b) then n else 0 :=
begin
split_ifs with h₁,
{ rw [h₁, count_repeat_self] },
{ rw [count_eq_zero],
apply mt eq_of_mem_repeat h₁ },
end
@[simp] theorem count_erase_self (a : α) (s : multiset α) :
count a (erase s a) = pred (count a s) :=
begin
by_cases a ∈ s,
{ rw [(by rw cons_erase h : count a s = count a (a ::ₘ erase s a)),
count_cons_self]; refl },
{ rw [erase_of_not_mem h, count_eq_zero.2 h]; refl }
end
@[simp, priority 980] theorem count_erase_of_ne {a b : α} (ab : a ≠ b) (s : multiset α) :
count a (erase s b) = count a s :=
begin
by_cases b ∈ s,
{ rw [← count_cons_of_ne ab, cons_erase h] },
{ rw [erase_of_not_mem h] }
end
@[simp] theorem count_sub (a : α) (s t : multiset α) : count a (s - t) = count a s - count a t :=
begin
revert s, refine multiset.induction_on t (by simp) (λ b t IH s, _),
rw [sub_cons, IH],
by_cases ab : a = b,
{ subst b, rw [count_erase_self, count_cons_self, sub_succ, pred_sub] },
{ rw [count_erase_of_ne ab, count_cons_of_ne ab] }
end
@[simp] theorem count_union (a : α) (s t : multiset α) :
count a (s ∪ t) = max (count a s) (count a t) :=
by simp [(∪), union, sub_add_eq_max, -add_comm]
@[simp] theorem count_inter (a : α) (s t : multiset α) :
count a (s ∩ t) = min (count a s) (count a t) :=
begin
apply @nat.add_left_cancel (count a (s - t)),
rw [← count_add, sub_add_inter, count_sub, sub_add_min],
end
lemma count_sum {m : multiset β} {f : β → multiset α} {a : α} :
count a (map f m).sum = sum (m.map $ λb, count a $ f b) :=
multiset.induction_on m (by simp) ( by simp)
lemma count_bind {m : multiset β} {f : β → multiset α} {a : α} :
count a (bind m f) = sum (m.map $ λb, count a $ f b) := count_sum
theorem le_count_iff_repeat_le {a : α} {s : multiset α} {n : ℕ} : n ≤ count a s ↔ repeat a n ≤ s :=
quot.induction_on s $ λ l, le_count_iff_repeat_sublist.trans repeat_le_coe.symm
@[simp] theorem count_filter_of_pos {p} [decidable_pred p]
{a} {s : multiset α} (h : p a) : count a (filter p s) = count a s :=
quot.induction_on s $ λ l, count_filter h
@[simp] theorem count_filter_of_neg {p} [decidable_pred p]
{a} {s : multiset α} (h : ¬ p a) : count a (filter p s) = 0 :=
multiset.count_eq_zero_of_not_mem (λ t, h (of_mem_filter t))
theorem ext {s t : multiset α} : s = t ↔ ∀ a, count a s = count a t :=
quotient.induction_on₂ s t $ λ l₁ l₂, quotient.eq.trans perm_iff_count
@[ext]
theorem ext' {s t : multiset α} : (∀ a, count a s = count a t) → s = t :=
ext.2
@[simp] theorem coe_inter (s t : list α) : (s ∩ t : multiset α) = (s.bag_inter t : list α) :=
by ext; simp
theorem le_iff_count {s t : multiset α} : s ≤ t ↔ ∀ a, count a s ≤ count a t :=
⟨λ h a, count_le_of_le a h, λ al,
by rw ← (ext.2 (λ a, by simp [max_eq_right (al a)]) : s ∪ t = t);
apply le_union_left⟩
instance : distrib_lattice (multiset α) :=
{ le_sup_inf := λ s t u, le_of_eq $ eq.symm $
ext.2 $ λ a, by simp only [max_min_distrib_left,
multiset.count_inter, multiset.sup_eq_union, multiset.count_union, multiset.inf_eq_inter],
..multiset.lattice }
instance : semilattice_sup_bot (multiset α) :=
{ bot := 0,
bot_le := zero_le,
..multiset.lattice }
end
@[simp]
lemma mem_nsmul {a : α} {s : multiset α} {n : ℕ} (h0 : n ≠ 0) :
a ∈ n •ℕ s ↔ a ∈ s :=
begin
classical,
cases n,
{ exfalso, apply h0 rfl },
rw [← not_iff_not, ← count_eq_zero, ← count_eq_zero],
simp [h0],
end
/-! ### Lift a relation to `multiset`s -/
section rel
/-- `rel r s t` -- lift the relation `r` between two elements to a relation between `s` and `t`,
s.t. there is a one-to-one mapping betweem elements in `s` and `t` following `r`. -/
@[mk_iff] inductive rel (r : α → β → Prop) : multiset α → multiset β → Prop
| zero : rel 0 0
| cons {a b as bs} : r a b → rel as bs → rel (a ::ₘ as) (b ::ₘ bs)
variables {δ : Type*} {r : α → β → Prop} {p : γ → δ → Prop}
private lemma rel_flip_aux {s t} (h : rel r s t) : rel (flip r) t s :=
rel.rec_on h rel.zero (assume _ _ _ _ h₀ h₁ ih, rel.cons h₀ ih)
lemma rel_flip {s t} : rel (flip r) s t ↔ rel r t s :=
⟨rel_flip_aux, rel_flip_aux⟩
lemma rel_eq_refl {s : multiset α} : rel (=) s s :=
multiset.induction_on s rel.zero (assume a s, rel.cons rfl)
lemma rel_eq {s t : multiset α} : rel (=) s t ↔ s = t :=
begin
split,
{ assume h, induction h; simp * },
{ assume h, subst h, exact rel_eq_refl }
end
lemma rel.mono {p : α → β → Prop} {s t} (h : ∀a b, r a b → p a b) (hst : rel r s t) : rel p s t :=
begin
induction hst,
case rel.zero { exact rel.zero },
case rel.cons : a b s t hab hst ih { exact ih.cons (h a b hab) }
end
lemma rel.add {s t u v} (hst : rel r s t) (huv : rel r u v) : rel r (s + u) (t + v) :=
begin
induction hst,
case rel.zero { simpa using huv },
case rel.cons : a b s t hab hst ih { simpa using ih.cons hab }
end
lemma rel_flip_eq {s t : multiset α} : rel (λa b, b = a) s t ↔ s = t :=
show rel (flip (=)) s t ↔ s = t, by rw [rel_flip, rel_eq, eq_comm]
@[simp] lemma rel_zero_left {b : multiset β} : rel r 0 b ↔ b = 0 :=
by rw [rel_iff]; simp
@[simp] lemma rel_zero_right {a : multiset α} : rel r a 0 ↔ a = 0 :=
by rw [rel_iff]; simp
lemma rel_cons_left {a as bs} :
rel r (a ::ₘ as) bs ↔ (∃b bs', r a b ∧ rel r as bs' ∧ bs = b ::ₘ bs') :=
begin
split,
{ generalize hm : a ::ₘ as = m,
assume h,
induction h generalizing as,
case rel.zero { simp at hm, contradiction },
case rel.cons : a' b as' bs ha'b h ih {
rcases cons_eq_cons.1 hm with ⟨eq₁, eq₂⟩ | ⟨h, cs, eq₁, eq₂⟩,
{ subst eq₁, subst eq₂, exact ⟨b, bs, ha'b, h, rfl⟩ },
{ rcases ih eq₂.symm with ⟨b', bs', h₁, h₂, eq⟩,
exact ⟨b', b ::ₘ bs', h₁, eq₁.symm ▸ rel.cons ha'b h₂, eq.symm ▸ cons_swap _ _ _⟩ }
} },
{ exact assume ⟨b, bs', hab, h, eq⟩, eq.symm ▸ rel.cons hab h }
end
lemma rel_cons_right {as b bs} :
rel r as (b ::ₘ bs) ↔ (∃a as', r a b ∧ rel r as' bs ∧ as = a ::ₘ as') :=
begin
rw [← rel_flip, rel_cons_left],
apply exists_congr, assume a,
apply exists_congr, assume as',
rw [rel_flip, flip]
end
lemma rel_add_left {as₀ as₁} :
∀{bs}, rel r (as₀ + as₁) bs ↔ (∃bs₀ bs₁, rel r as₀ bs₀ ∧ rel r as₁ bs₁ ∧ bs = bs₀ + bs₁) :=
multiset.induction_on as₀ (by simp)
begin
assume a s ih bs,
simp only [ih, cons_add, rel_cons_left],
split,
{ assume h,
rcases h with ⟨b, bs', hab, h, rfl⟩,
rcases h with ⟨bs₀, bs₁, h₀, h₁, rfl⟩,
exact ⟨b ::ₘ bs₀, bs₁, ⟨b, bs₀, hab, h₀, rfl⟩, h₁, by simp⟩ },
{ assume h,
rcases h with ⟨bs₀, bs₁, h, h₁, rfl⟩,
rcases h with ⟨b, bs, hab, h₀, rfl⟩,
exact ⟨b, bs + bs₁, hab, ⟨bs, bs₁, h₀, h₁, rfl⟩, by simp⟩ }
end
lemma rel_add_right {as bs₀ bs₁} :
rel r as (bs₀ + bs₁) ↔ (∃as₀ as₁, rel r as₀ bs₀ ∧ rel r as₁ bs₁ ∧ as = as₀ + as₁) :=
by rw [← rel_flip, rel_add_left]; simp [rel_flip]
lemma rel_map_left {s : multiset γ} {f : γ → α} :
∀{t}, rel r (s.map f) t ↔ rel (λa b, r (f a) b) s t :=
multiset.induction_on s (by simp) (by simp [rel_cons_left] {contextual := tt})
lemma rel_map_right {s : multiset α} {t : multiset γ} {f : γ → β} :
rel r s (t.map f) ↔ rel (λa b, r a (f b)) s t :=
by rw [← rel_flip, rel_map_left, ← rel_flip]; refl
lemma rel_join {s t} (h : rel (rel r) s t) : rel r s.join t.join :=
begin
induction h,
case rel.zero { simp },
case rel.cons : a b s t hab hst ih { simpa using hab.add ih }
end
lemma rel_map {p : γ → δ → Prop} {s t} {f : α → γ} {g : β → δ} (h : (r ⇒ p) f g) (hst : rel r s t) :
rel p (s.map f) (t.map g) :=
by rw [rel_map_left, rel_map_right]; exact hst.mono h
lemma rel_bind {p : γ → δ → Prop} {s t} {f : α → multiset γ} {g : β → multiset δ}
(h : (r ⇒ rel p) f g) (hst : rel r s t) :
rel p (s.bind f) (t.bind g) :=
by apply rel_join; apply rel_map; assumption
lemma card_eq_card_of_rel {r : α → β → Prop} {s : multiset α} {t : multiset β} (h : rel r s t) :
card s = card t :=
by induction h; simp [*]
lemma exists_mem_of_rel_of_mem {r : α → β → Prop} {s : multiset α} {t : multiset β}
(h : rel r s t) :
∀ {a : α} (ha : a ∈ s), ∃ b ∈ t, r a b :=
begin
induction h with x y s t hxy hst ih,
{ simp },
{ assume a ha,
cases mem_cons.1 ha with ha ha,
{ exact ⟨y, mem_cons_self _ _, ha.symm ▸ hxy⟩ },
{ rcases ih ha with ⟨b, hbt, hab⟩,
exact ⟨b, mem_cons.2 (or.inr hbt), hab⟩ } }
end
end rel
section map
theorem map_eq_map {f : α → β} (hf : function.injective f) {s t : multiset α} :
s.map f = t.map f ↔ s = t :=
by rw [← rel_eq, ← rel_eq, rel_map_left, rel_map_right]; simp [hf.eq_iff]
theorem map_injective {f : α → β} (hf : function.injective f) :
function.injective (multiset.map f) :=
assume x y, (map_eq_map hf).1
end map
section quot
theorem map_mk_eq_map_mk_of_rel {r : α → α → Prop} {s t : multiset α} (hst : s.rel r t) :
s.map (quot.mk r) = t.map (quot.mk r) :=
rel.rec_on hst rfl $ assume a b s t hab hst ih, by simp [ih, quot.sound hab]
theorem exists_multiset_eq_map_quot_mk {r : α → α → Prop} (s : multiset (quot r)) :
∃t:multiset α, s = t.map (quot.mk r) :=
multiset.induction_on s ⟨0, rfl⟩ $
assume a s ⟨t, ht⟩, quot.induction_on a $ assume a, ht.symm ▸ ⟨a ::ₘ t, (map_cons _ _ _).symm⟩
theorem induction_on_multiset_quot
{r : α → α → Prop} {p : multiset (quot r) → Prop} (s : multiset (quot r)) :
(∀s:multiset α, p (s.map (quot.mk r))) → p s :=
match s, exists_multiset_eq_map_quot_mk s with _, ⟨t, rfl⟩ := assume h, h _ end
end quot
/-! ### Disjoint multisets -/
/-- `disjoint s t` means that `s` and `t` have no elements in common. -/
def disjoint (s t : multiset α) : Prop := ∀ ⦃a⦄, a ∈ s → a ∈ t → false
@[simp] theorem coe_disjoint (l₁ l₂ : list α) : @disjoint α l₁ l₂ ↔ l₁.disjoint l₂ := iff.rfl
theorem disjoint.symm {s t : multiset α} (d : disjoint s t) : disjoint t s
| a i₂ i₁ := d i₁ i₂
theorem disjoint_comm {s t : multiset α} : disjoint s t ↔ disjoint t s :=
⟨disjoint.symm, disjoint.symm⟩
theorem disjoint_left {s t : multiset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := iff.rfl
theorem disjoint_right {s t : multiset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s :=
disjoint_comm
theorem disjoint_iff_ne {s t : multiset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b :=
by simp [disjoint_left, imp_not_comm]
theorem disjoint_of_subset_left {s t u : multiset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t
| x m₁ := d (h m₁)
theorem disjoint_of_subset_right {s t u : multiset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t
| x m m₁ := d m (h m₁)
theorem disjoint_of_le_left {s t u : multiset α} (h : s ≤ u) : disjoint u t → disjoint s t :=
disjoint_of_subset_left (subset_of_le h)
theorem disjoint_of_le_right {s t u : multiset α} (h : t ≤ u) : disjoint s u → disjoint s t :=
disjoint_of_subset_right (subset_of_le h)
@[simp] theorem zero_disjoint (l : multiset α) : disjoint 0 l
| a := (not_mem_nil a).elim
@[simp, priority 1100]
theorem singleton_disjoint {l : multiset α} {a : α} : disjoint (a ::ₘ 0) l ↔ a ∉ l :=
by simp [disjoint]; refl
@[simp, priority 1100]
theorem disjoint_singleton {l : multiset α} {a : α} : disjoint l (a ::ₘ 0) ↔ a ∉ l :=
by rw disjoint_comm; simp
@[simp] theorem disjoint_add_left {s t u : multiset α} :
disjoint (s + t) u ↔ disjoint s u ∧ disjoint t u :=
by simp [disjoint, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_add_right {s t u : multiset α} :
disjoint s (t + u) ↔ disjoint s t ∧ disjoint s u :=
by rw [disjoint_comm, disjoint_add_left]; tauto
@[simp] theorem disjoint_cons_left {a : α} {s t : multiset α} :
disjoint (a ::ₘ s) t ↔ a ∉ t ∧ disjoint s t :=
(@disjoint_add_left _ (a ::ₘ 0) s t).trans $ by simp
@[simp] theorem disjoint_cons_right {a : α} {s t : multiset α} :
disjoint s (a ::ₘ t) ↔ a ∉ s ∧ disjoint s t :=
by rw [disjoint_comm, disjoint_cons_left]; tauto
theorem inter_eq_zero_iff_disjoint [decidable_eq α] {s t : multiset α} : s ∩ t = 0 ↔ disjoint s t :=
by rw ← subset_zero; simp [subset_iff, disjoint]
@[simp] theorem disjoint_union_left [decidable_eq α] {s t u : multiset α} :
disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u :=
by simp [disjoint, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_union_right [decidable_eq α] {s t u : multiset α} :
disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u :=
by simp [disjoint, or_imp_distrib, forall_and_distrib]
lemma disjoint_map_map {f : α → γ} {g : β → γ} {s : multiset α} {t : multiset β} :
disjoint (s.map f) (t.map g) ↔ (∀a∈s, ∀b∈t, f a ≠ g b) :=
by { simp [disjoint, @eq_comm _ (f _) (g _)], refl }
/-- `pairwise r m` states that there exists a list of the elements s.t. `r` holds pairwise on this
list. -/
def pairwise (r : α → α → Prop) (m : multiset α) : Prop :=
∃l:list α, m = l ∧ l.pairwise r
lemma pairwise_coe_iff_pairwise {r : α → α → Prop} (hr : symmetric r) {l : list α} :
multiset.pairwise r l ↔ l.pairwise r :=
iff.intro
(assume ⟨l', eq, h⟩, ((quotient.exact eq).pairwise_iff hr).2 h)
(assume h, ⟨l, rfl, h⟩)
end multiset
namespace multiset
section choose
variables (p : α → Prop) [decidable_pred p] (l : multiset α)
/-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose_x p l hp` returns
that `a` together with proofs of `a ∈ l` and `p a`. -/
def choose_x : Π hp : (∃! a, a ∈ l ∧ p a), { a // a ∈ l ∧ p a } :=
quotient.rec_on l (λ l' ex_unique, list.choose_x p l' (exists_of_exists_unique ex_unique)) begin
intros,
funext hp,
suffices all_equal : ∀ x y : { t // t ∈ b ∧ p t }, x = y,
{ apply all_equal },
{ rintros ⟨x, px⟩ ⟨y, py⟩,
rcases hp with ⟨z, ⟨z_mem_l, pz⟩, z_unique⟩,
congr,
calc x = z : z_unique x px
... = y : (z_unique y py).symm }
end
/-- Given a proof `hp` that there exists a unique `a ∈ l` such that `p a`, `choose p l hp` returns
that `a`. -/
def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp
lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1
lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
variable (α)
/-- The equivalence between lists and multisets of a subsingleton type. -/
def subsingleton_equiv [subsingleton α] : list α ≃ multiset α :=
{ to_fun := coe,
inv_fun := quot.lift id $ λ (a b : list α) (h : a ~ b),
list.ext_le h.length_eq $ λ n h₁ h₂, subsingleton.elim _ _,
left_inv := λ l, rfl,
right_inv := λ m, quot.induction_on m $ λ l, rfl }
variable {α}
@[simp]
lemma coe_subsingleton_equiv [subsingleton α] :
(subsingleton_equiv α : list α → multiset α) = coe :=
rfl
end multiset
@[to_additive]
theorem monoid_hom.map_multiset_prod [comm_monoid α] [comm_monoid β] (f : α →* β) (s : multiset α) :
f s.prod = (s.map f).prod :=
(s.prod_hom f).symm
|
cfaab75ccd03755e2782aee066c894cce64f033b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/probability/process/filtration.lean | f498406b4836c4bd46b1139da68c4064096255bc | [
"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 | 13,081 | lean | /-
Copyright (c) 2021 Kexing Ying. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying, Rémy Degenne
-/
import measure_theory.function.conditional_expectation.real
/-!
# Filtrations
This file defines filtrations of a measurable space and σ-finite filtrations.
## Main definitions
* `measure_theory.filtration`: a filtration on a measurable space. That is, a monotone sequence of
sub-σ-algebras.
* `measure_theory.sigma_finite_filtration`: a filtration `f` is σ-finite with respect to a measure
`μ` if for all `i`, `μ.trim (f.le i)` is σ-finite.
* `measure_theory.filtration.natural`: the smallest filtration that makes a process adapted. That
notion `adapted` is not defined yet in this file. See `measure_theory.adapted`.
## Main results
* `measure_theory.filtration.complete_lattice`: filtrations are a complete lattice.
## Tags
filtration, stochastic process
-/
open filter order topological_space
open_locale classical measure_theory nnreal ennreal topological_space big_operators
namespace measure_theory
/-- A `filtration` on a measurable space `Ω` with σ-algebra `m` is a monotone
sequence of sub-σ-algebras of `m`. -/
structure filtration {Ω : Type*} (ι : Type*) [preorder ι] (m : measurable_space Ω) :=
(seq : ι → measurable_space Ω)
(mono' : monotone seq)
(le' : ∀ i : ι, seq i ≤ m)
variables {Ω β ι : Type*} {m : measurable_space Ω}
instance [preorder ι] : has_coe_to_fun (filtration ι m) (λ _, ι → measurable_space Ω) :=
⟨λ f, f.seq⟩
namespace filtration
variables [preorder ι]
protected lemma mono {i j : ι} (f : filtration ι m) (hij : i ≤ j) : f i ≤ f j := f.mono' hij
protected lemma le (f : filtration ι m) (i : ι) : f i ≤ m := f.le' i
@[ext] protected lemma ext {f g : filtration ι m} (h : (f : ι → measurable_space Ω) = g) : f = g :=
by { cases f, cases g, simp only, exact h, }
variable (ι)
/-- The constant filtration which is equal to `m` for all `i : ι`. -/
def const (m' : measurable_space Ω) (hm' : m' ≤ m) : filtration ι m :=
⟨λ _, m', monotone_const, λ _, hm'⟩
variable {ι}
@[simp]
lemma const_apply {m' : measurable_space Ω} {hm' : m' ≤ m} (i : ι) : const ι m' hm' i = m' := rfl
instance : inhabited (filtration ι m) := ⟨const ι m le_rfl⟩
instance : has_le (filtration ι m) := ⟨λ f g, ∀ i, f i ≤ g i⟩
instance : has_bot (filtration ι m) := ⟨const ι ⊥ bot_le⟩
instance : has_top (filtration ι m) := ⟨const ι m le_rfl⟩
instance : has_sup (filtration ι m) := ⟨λ f g,
{ seq := λ i, f i ⊔ g i,
mono' := λ i j hij, sup_le ((f.mono hij).trans le_sup_left) ((g.mono hij).trans le_sup_right),
le' := λ i, sup_le (f.le i) (g.le i) }⟩
@[norm_cast] lemma coe_fn_sup {f g : filtration ι m} : ⇑(f ⊔ g) = f ⊔ g := rfl
instance : has_inf (filtration ι m) := ⟨λ f g,
{ seq := λ i, f i ⊓ g i,
mono' := λ i j hij, le_inf (inf_le_left.trans (f.mono hij)) (inf_le_right.trans (g.mono hij)),
le' := λ i, inf_le_left.trans (f.le i) }⟩
@[norm_cast] lemma coe_fn_inf {f g : filtration ι m} : ⇑(f ⊓ g) = f ⊓ g := rfl
instance : has_Sup (filtration ι m) := ⟨λ s,
{ seq := λ i, Sup ((λ f : filtration ι m, f i) '' s),
mono' := λ i j hij,
begin
refine Sup_le (λ m' hm', _),
rw [set.mem_image] at hm',
obtain ⟨f, hf_mem, hfm'⟩ := hm',
rw ← hfm',
refine (f.mono hij).trans _,
have hfj_mem : f j ∈ ((λ g : filtration ι m, g j) '' s), from ⟨f, hf_mem, rfl⟩,
exact le_Sup hfj_mem,
end,
le' := λ i,
begin
refine Sup_le (λ m' hm', _),
rw [set.mem_image] at hm',
obtain ⟨f, hf_mem, hfm'⟩ := hm',
rw ← hfm',
exact f.le i,
end, }⟩
lemma Sup_def (s : set (filtration ι m)) (i : ι) :
Sup s i = Sup ((λ f : filtration ι m, f i) '' s) :=
rfl
noncomputable
instance : has_Inf (filtration ι m) := ⟨λ s,
{ seq := λ i, if set.nonempty s then Inf ((λ f : filtration ι m, f i) '' s) else m,
mono' := λ i j hij,
begin
by_cases h_nonempty : set.nonempty s,
swap, { simp only [h_nonempty, set.nonempty_image_iff, if_false, le_refl], },
simp only [h_nonempty, if_true, le_Inf_iff, set.mem_image, forall_exists_index, and_imp,
forall_apply_eq_imp_iff₂],
refine λ f hf_mem, le_trans _ (f.mono hij),
have hfi_mem : f i ∈ ((λ g : filtration ι m, g i) '' s), from ⟨f, hf_mem, rfl⟩,
exact Inf_le hfi_mem,
end,
le' := λ i,
begin
by_cases h_nonempty : set.nonempty s,
swap, { simp only [h_nonempty, if_false, le_refl], },
simp only [h_nonempty, if_true],
obtain ⟨f, hf_mem⟩ := h_nonempty,
exact le_trans (Inf_le ⟨f, hf_mem, rfl⟩) (f.le i),
end, }⟩
lemma Inf_def (s : set (filtration ι m)) (i : ι) :
Inf s i = if set.nonempty s then Inf ((λ f : filtration ι m, f i) '' s) else m :=
rfl
noncomputable
instance : complete_lattice (filtration ι m) :=
{ le := (≤),
le_refl := λ f i, le_rfl,
le_trans := λ f g h h_fg h_gh i, (h_fg i).trans (h_gh i),
le_antisymm := λ f g h_fg h_gf, filtration.ext $ funext $ λ i, (h_fg i).antisymm (h_gf i),
sup := (⊔),
le_sup_left := λ f g i, le_sup_left,
le_sup_right := λ f g i, le_sup_right,
sup_le := λ f g h h_fh h_gh i, sup_le (h_fh i) (h_gh _),
inf := (⊓),
inf_le_left := λ f g i, inf_le_left,
inf_le_right := λ f g i, inf_le_right,
le_inf := λ f g h h_fg h_fh i, le_inf (h_fg i) (h_fh i),
Sup := Sup,
le_Sup := λ s f hf_mem i, le_Sup ⟨f, hf_mem, rfl⟩,
Sup_le := λ s f h_forall i, Sup_le $ λ m' hm',
begin
obtain ⟨g, hg_mem, hfm'⟩ := hm',
rw ← hfm',
exact h_forall g hg_mem i,
end,
Inf := Inf,
Inf_le := λ s f hf_mem i,
begin
have hs : s.nonempty := ⟨f, hf_mem⟩,
simp only [Inf_def, hs, if_true],
exact Inf_le ⟨f, hf_mem, rfl⟩,
end,
le_Inf := λ s f h_forall i,
begin
by_cases hs : s.nonempty,
swap, { simp only [Inf_def, hs, if_false], exact f.le i, },
simp only [Inf_def, hs, if_true, le_Inf_iff, set.mem_image, forall_exists_index, and_imp,
forall_apply_eq_imp_iff₂],
exact λ g hg_mem, h_forall g hg_mem i,
end,
top := ⊤,
bot := ⊥,
le_top := λ f i, f.le' i,
bot_le := λ f i, bot_le, }
end filtration
lemma measurable_set_of_filtration [preorder ι] {f : filtration ι m} {s : set Ω} {i : ι}
(hs : measurable_set[f i] s) : measurable_set[m] s :=
f.le i s hs
/-- A measure is σ-finite with respect to filtration if it is σ-finite with respect
to all the sub-σ-algebra of the filtration. -/
class sigma_finite_filtration [preorder ι] (μ : measure Ω) (f : filtration ι m) : Prop :=
(sigma_finite : ∀ i : ι, sigma_finite (μ.trim (f.le i)))
instance sigma_finite_of_sigma_finite_filtration [preorder ι] (μ : measure Ω) (f : filtration ι m)
[hf : sigma_finite_filtration μ f] (i : ι) :
sigma_finite (μ.trim (f.le i)) :=
by apply hf.sigma_finite -- can't exact here
@[priority 100]
instance is_finite_measure.sigma_finite_filtration [preorder ι] (μ : measure Ω) (f : filtration ι m)
[is_finite_measure μ] :
sigma_finite_filtration μ f :=
⟨λ n, by apply_instance⟩
/-- Given a integrable function `g`, the conditional expectations of `g` with respect to a
filtration is uniformly integrable. -/
lemma integrable.uniform_integrable_condexp_filtration
[preorder ι] {μ : measure Ω} [is_finite_measure μ] {f : filtration ι m}
{g : Ω → ℝ} (hg : integrable g μ) :
uniform_integrable (λ i, μ[g | f i]) 1 μ :=
hg.uniform_integrable_condexp f.le
section of_set
variables [preorder ι]
/-- Given a sequence of measurable sets `(sₙ)`, `filtration_of_set` is the smallest filtration
such that `sₙ` is measurable with respect to the `n`-the sub-σ-algebra in `filtration_of_set`. -/
def filtration_of_set {s : ι → set Ω} (hsm : ∀ i, measurable_set (s i)) : filtration ι m :=
{ seq := λ i, measurable_space.generate_from {t | ∃ j ≤ i, s j = t},
mono' := λ n m hnm, measurable_space.generate_from_mono
(λ t ⟨k, hk₁, hk₂⟩, ⟨k, hk₁.trans hnm, hk₂⟩),
le' := λ n, measurable_space.generate_from_le (λ t ⟨k, hk₁, hk₂⟩, hk₂ ▸ hsm k) }
lemma measurable_set_filtration_of_set {s : ι → set Ω}
(hsm : ∀ i, measurable_set[m] (s i)) (i : ι) {j : ι} (hj : j ≤ i) :
measurable_set[filtration_of_set hsm i] (s j) :=
measurable_space.measurable_set_generate_from ⟨j, hj, rfl⟩
lemma measurable_set_filtration_of_set' {s : ι → set Ω}
(hsm : ∀ n, measurable_set[m] (s n)) (i : ι) :
measurable_set[filtration_of_set hsm i] (s i) :=
measurable_set_filtration_of_set hsm i le_rfl
end of_set
namespace filtration
variables [topological_space β] [metrizable_space β] [mβ : measurable_space β] [borel_space β]
[preorder ι]
include mβ
/-- Given a sequence of functions, the natural filtration is the smallest sequence
of σ-algebras such that that sequence of functions is measurable with respect to
the filtration. -/
def natural (u : ι → Ω → β) (hum : ∀ i, strongly_measurable (u i)) : filtration ι m :=
{ seq := λ i, ⨆ j ≤ i, measurable_space.comap (u j) mβ,
mono' := λ i j hij, bsupr_mono $ λ k, ge_trans hij,
le' := λ i,
begin
refine supr₂_le _,
rintros j hj s ⟨t, ht, rfl⟩,
exact (hum j).measurable ht,
end }
section
open measurable_space
lemma filtration_of_set_eq_natural [mul_zero_one_class β] [nontrivial β]
{s : ι → set Ω} (hsm : ∀ i, measurable_set[m] (s i)) :
filtration_of_set hsm = natural (λ i, (s i).indicator (λ ω, 1 : Ω → β))
(λ i, strongly_measurable_one.indicator (hsm i)) :=
begin
simp only [natural, filtration_of_set, measurable_space_supr_eq],
ext1 i,
refine le_antisymm (generate_from_le _) (generate_from_le _),
{ rintro _ ⟨j, hij, rfl⟩,
refine measurable_set_generate_from ⟨j, measurable_set_generate_from ⟨hij, _⟩⟩,
rw comap_eq_generate_from,
refine measurable_set_generate_from ⟨{1}, measurable_set_singleton 1, _⟩,
ext x,
simp [set.indicator_const_preimage_eq_union] },
{ rintro t ⟨n, ht⟩,
suffices : measurable_space.generate_from
{t | ∃ (H : n ≤ i), measurable_set[(measurable_space.comap
((s n).indicator (λ ω, 1 : Ω → β)) mβ)] t}
≤ generate_from {t | ∃ (j : ι) (H : j ≤ i), s j = t},
{ exact this _ ht },
refine generate_from_le _,
rintro t ⟨hn, u, hu, hu'⟩,
obtain heq | heq | heq | heq := set.indicator_const_preimage (s n) u (1 : β),
swap 4, rw set.mem_singleton_iff at heq,
all_goals { rw heq at hu', rw ← hu' },
exacts [measurable_set_empty _, measurable_set.univ, measurable_set_generate_from ⟨n, hn, rfl⟩,
measurable_set.compl (measurable_set_generate_from ⟨n, hn, rfl⟩)] }
end
end
section limit
omit mβ
variables {E : Type*} [has_zero E] [topological_space E]
{ℱ : filtration ι m} {f : ι → Ω → E} {μ : measure Ω}
/-- Given a process `f` and a filtration `ℱ`, if `f` converges to some `g` almost everywhere and
`g` is `⨆ n, ℱ n`-measurable, then `limit_process f ℱ μ` chooses said `g`, else it returns 0.
This definition is used to phrase the a.e. martingale convergence theorem
`submartingale.ae_tendsto_limit_process` where an L¹-bounded submartingale `f` adapted to `ℱ`
converges to `limit_process f ℱ μ` `μ`-almost everywhere. -/
noncomputable
def limit_process (f : ι → Ω → E) (ℱ : filtration ι m) (μ : measure Ω . volume_tac) :=
if h : ∃ g : Ω → E, strongly_measurable[⨆ n, ℱ n] g ∧
∀ᵐ ω ∂μ, tendsto (λ n, f n ω) at_top (𝓝 (g ω)) then classical.some h else 0
lemma strongly_measurable_limit_process :
strongly_measurable[⨆ n, ℱ n] (limit_process f ℱ μ) :=
begin
rw limit_process,
split_ifs with h h,
exacts [(classical.some_spec h).1, strongly_measurable_zero]
end
lemma strongly_measurable_limit_process' :
strongly_measurable[m] (limit_process f ℱ μ) :=
strongly_measurable_limit_process.mono (Sup_le (λ m ⟨n, hn⟩, hn ▸ ℱ.le _))
lemma mem_ℒp_limit_process_of_snorm_bdd {R : ℝ≥0} {p : ℝ≥0∞}
{F : Type*} [normed_add_comm_group F] {ℱ : filtration ℕ m} {f : ℕ → Ω → F}
(hfm : ∀ n, ae_strongly_measurable (f n) μ) (hbdd : ∀ n, snorm (f n) p μ ≤ R) :
mem_ℒp (limit_process f ℱ μ) p μ :=
begin
rw limit_process,
split_ifs with h,
{ refine ⟨strongly_measurable.ae_strongly_measurable
((classical.some_spec h).1.mono (Sup_le (λ m ⟨n, hn⟩, hn ▸ ℱ.le _))),
lt_of_le_of_lt (Lp.snorm_lim_le_liminf_snorm hfm _ (classical.some_spec h).2)
(lt_of_le_of_lt _ (ennreal.coe_lt_top : ↑R < ∞))⟩,
simp_rw [liminf_eq, eventually_at_top],
exact Sup_le (λ b ⟨a, ha⟩, (ha a le_rfl).trans (hbdd _)) },
{ exact zero_mem_ℒp }
end
end limit
end filtration
end measure_theory
|
4d1ad0c4461c8bde89eaf5624642f4078c44cb1f | c777c32c8e484e195053731103c5e52af26a25d1 | /src/algebra/continued_fractions/computation/terminates_iff_rat.lean | d4977bd208740bfca177e9203d3401738dfad7a9 | [
"Apache-2.0"
] | permissive | kbuzzard/mathlib | 2ff9e85dfe2a46f4b291927f983afec17e946eb8 | 58537299e922f9c77df76cb613910914a479c1f7 | refs/heads/master | 1,685,313,702,744 | 1,683,974,212,000 | 1,683,974,212,000 | 128,185,277 | 1 | 0 | null | 1,522,920,600,000 | 1,522,920,600,000 | null | UTF-8 | Lean | false | false | 14,214 | lean | /-
Copyright (c) 2020 Kevin Kappelmann. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Kappelmann
-/
import algebra.continued_fractions.computation.approximations
import algebra.continued_fractions.computation.correctness_terminating
import data.rat.floor
/-!
# Termination of Continued Fraction Computations (`gcf.of`)
## Summary
We show that the continued fraction for a value `v`, as defined in
`algebra.continued_fractions.computation.basic`, terminates if and only if `v` corresponds to a
rational number, that is `↑v = q` for some `q : ℚ`.
## Main Theorems
- `generalized_continued_fraction.coe_of_rat` shows that
`generalized_continued_fraction.of v = generalized_continued_fraction.of q` for `v : α` given that
`↑v = q` and `q : ℚ`.
- `generalized_continued_fraction.terminates_iff_rat` shows that
`generalized_continued_fraction.of v` terminates if and only if `↑v = q` for some `q : ℚ`.
## Tags
rational, continued fraction, termination
-/
namespace generalized_continued_fraction
open stream.seq as seq
open generalized_continued_fraction (of)
variables {K : Type*} [linear_ordered_field K] [floor_ring K]
/-
We will have to constantly coerce along our structures in the following proofs using their provided
map functions.
-/
local attribute [simp] pair.map int_fract_pair.mapFr
section rat_of_terminates
/-!
### Terminating Continued Fractions Are Rational
We want to show that the computation of a continued fraction `generalized_continued_fraction.of v`
terminates if and only if `v ∈ ℚ`. In this section, we show the implication from left to right.
We first show that every finite convergent corresponds to a rational number `q` and then use the
finite correctness proof (`of_correctness_of_terminates`) of `generalized_continued_fraction.of` to
show that `v = ↑q`.
-/
variables (v : K) (n : ℕ)
lemma exists_gcf_pair_rat_eq_of_nth_conts_aux :
∃ (conts : pair ℚ),
(of v).continuants_aux n = (conts.map coe : pair K) :=
nat.strong_induction_on n
begin
clear n,
let g := of v,
assume n IH,
rcases n with _|_|n,
-- n = 0
{ suffices : ∃ (gp : pair ℚ), pair.mk (1 : K) 0 = gp.map coe, by simpa [continuants_aux],
use (pair.mk 1 0),
simp },
-- n = 1
{ suffices : ∃ (conts : pair ℚ), pair.mk g.h 1 = conts.map coe, by
simpa [continuants_aux],
use (pair.mk ⌊v⌋ 1),
simp },
-- 2 ≤ n
{ cases (IH (n + 1) $ lt_add_one (n + 1)) with pred_conts pred_conts_eq, -- invoke the IH
cases s_ppred_nth_eq : (g.s.nth n) with gp_n,
-- option.none
{ use pred_conts,
have : g.continuants_aux (n + 2) = g.continuants_aux (n + 1), from
continuants_aux_stable_of_terminated (n + 1).le_succ s_ppred_nth_eq,
simp only [this, pred_conts_eq] },
-- option.some
{ -- invoke the IH a second time
cases (IH n $ lt_of_le_of_lt (n.le_succ) $ lt_add_one $ n + 1)
with ppred_conts ppred_conts_eq,
obtain ⟨a_eq_one, z, b_eq_z⟩ : gp_n.a = 1 ∧ ∃ (z : ℤ), gp_n.b = (z : K), from
of_part_num_eq_one_and_exists_int_part_denom_eq s_ppred_nth_eq,
-- finally, unfold the recurrence to obtain the required rational value.
simp only [a_eq_one, b_eq_z,
(continuants_aux_recurrence s_ppred_nth_eq ppred_conts_eq pred_conts_eq)],
use (next_continuants 1 (z : ℚ) ppred_conts pred_conts),
cases ppred_conts, cases pred_conts,
simp [next_continuants, next_numerator, next_denominator] } }
end
lemma exists_gcf_pair_rat_eq_nth_conts :
∃ (conts : pair ℚ), (of v).continuants n = (conts.map coe : pair K) :=
by { rw [nth_cont_eq_succ_nth_cont_aux], exact (exists_gcf_pair_rat_eq_of_nth_conts_aux v $ n + 1) }
lemma exists_rat_eq_nth_numerator : ∃ (q : ℚ), (of v).numerators n = (q : K) :=
begin
rcases (exists_gcf_pair_rat_eq_nth_conts v n) with ⟨⟨a, _⟩, nth_cont_eq⟩,
use a,
simp [num_eq_conts_a, nth_cont_eq],
end
lemma exists_rat_eq_nth_denominator : ∃ (q : ℚ), (of v).denominators n = (q : K) :=
begin
rcases (exists_gcf_pair_rat_eq_nth_conts v n) with ⟨⟨_, b⟩, nth_cont_eq⟩,
use b,
simp [denom_eq_conts_b, nth_cont_eq]
end
/-- Every finite convergent corresponds to a rational number. -/
lemma exists_rat_eq_nth_convergent : ∃ (q : ℚ), (of v).convergents n = (q : K) :=
begin
rcases (exists_rat_eq_nth_numerator v n) with ⟨Aₙ, nth_num_eq⟩,
rcases (exists_rat_eq_nth_denominator v n) with ⟨Bₙ, nth_denom_eq⟩,
use (Aₙ / Bₙ),
simp [nth_num_eq, nth_denom_eq, convergent_eq_num_div_denom]
end
variable {v}
/-- Every terminating continued fraction corresponds to a rational number. -/
theorem exists_rat_eq_of_terminates
(terminates : (of v).terminates) :
∃ (q : ℚ), v = ↑q :=
begin
obtain ⟨n, v_eq_conv⟩ : ∃ n, v = (of v).convergents n, from
of_correctness_of_terminates terminates,
obtain ⟨q, conv_eq_q⟩ :
∃ (q : ℚ), (of v).convergents n = (↑q : K), from exists_rat_eq_nth_convergent v n,
have : v = (↑q : K), from eq.trans v_eq_conv conv_eq_q,
use [q, this]
end
end rat_of_terminates
section rat_translation
/-!
### Technical Translation Lemmas
Before we can show that the continued fraction of a rational number terminates, we have to prove
some technical translation lemmas. More precisely, in this section, we show that, given a rational
number `q : ℚ` and value `v : K` with `v = ↑q`, the continued fraction of `q` and `v` coincide.
In particular, we show that
```lean
(↑(generalized_continued_fraction.of q : generalized_continued_fraction ℚ)
: generalized_continued_fraction K)
= generalized_continued_fraction.of v`
```
in `generalized_continued_fraction.coe_of_rat`.
To do this, we proceed bottom-up, showing the correspondence between the basic functions involved in
the computation first and then lift the results step-by-step.
-/
/- The lifting works for arbitrary linear ordered fields with a floor function. -/
variables {v : K} {q : ℚ} (v_eq_q : v = (↑q : K)) (n : ℕ)
include v_eq_q
/-! First, we show the correspondence for the very basic functions in
`generalized_continued_fraction.int_fract_pair`. -/
namespace int_fract_pair
lemma coe_of_rat_eq :
((int_fract_pair.of q).mapFr coe : int_fract_pair K) = int_fract_pair.of v :=
by simp [int_fract_pair.of, v_eq_q]
lemma coe_stream_nth_rat_eq :
((int_fract_pair.stream q n).map (mapFr coe) : option $ int_fract_pair K)
= int_fract_pair.stream v n :=
begin
induction n with n IH,
case nat.zero : { simp [int_fract_pair.stream, (coe_of_rat_eq v_eq_q)] },
case nat.succ :
{ rw v_eq_q at IH,
cases stream_q_nth_eq : (int_fract_pair.stream q n) with ifp_n,
case option.none : { simp [int_fract_pair.stream, IH.symm, v_eq_q, stream_q_nth_eq] },
case option.some :
{ cases ifp_n with b fr,
cases decidable.em (fr = 0) with fr_zero fr_ne_zero,
{ simp [int_fract_pair.stream, IH.symm, v_eq_q, stream_q_nth_eq, fr_zero] },
{ replace IH : some (int_fract_pair.mk b ↑fr) = int_fract_pair.stream ↑q n, by
rwa [stream_q_nth_eq] at IH,
have : (fr : K)⁻¹ = ((fr⁻¹ : ℚ) : K), by norm_cast,
have coe_of_fr := (coe_of_rat_eq this),
simpa [int_fract_pair.stream, IH.symm, v_eq_q, stream_q_nth_eq, fr_ne_zero] } } }
end
lemma coe_stream_rat_eq :
((int_fract_pair.stream q).map (option.map (mapFr coe)) : stream $ option $ int_fract_pair K) =
int_fract_pair.stream v :=
by { funext n, exact (int_fract_pair.coe_stream_nth_rat_eq v_eq_q n) }
end int_fract_pair
/-! Now we lift the coercion results to the continued fraction computation. -/
lemma coe_of_h_rat_eq : (↑((of q).h : ℚ) : K) = (of v).h :=
begin
unfold of int_fract_pair.seq1,
rw ←(int_fract_pair.coe_of_rat_eq v_eq_q),
simp
end
lemma coe_of_s_nth_rat_eq :
(((of q).s.nth n).map (pair.map coe) : option $ pair K) = (of v).s.nth n :=
begin
simp only [of, int_fract_pair.seq1, seq.map_nth, seq.nth_tail],
simp only [seq.nth],
rw [←(int_fract_pair.coe_stream_rat_eq v_eq_q)],
rcases succ_nth_stream_eq : (int_fract_pair.stream q (n + 1)) with _ | ⟨_, _⟩;
simp [stream.map, stream.nth, succ_nth_stream_eq]
end
lemma coe_of_s_rat_eq : (((of q).s).map (pair.map coe) : seq $ pair K) = (of v).s :=
by { ext n, rw ←(coe_of_s_nth_rat_eq v_eq_q), refl }
/-- Given `(v : K), (q : ℚ), and v = q`, we have that `gcf.of q = gcf.of v` -/
lemma coe_of_rat_eq :
(⟨(of q).h, (of q).s.map (pair.map coe)⟩ : generalized_continued_fraction K) = of v :=
begin
cases gcf_v_eq : (of v) with h s, subst v,
obtain rfl : ↑⌊↑q⌋ = h, by { injection gcf_v_eq },
simp [coe_of_h_rat_eq rfl, coe_of_s_rat_eq rfl, gcf_v_eq]
end
lemma of_terminates_iff_of_rat_terminates {v : K} {q : ℚ} (v_eq_q : v = (q : K)) :
(of v).terminates ↔ (of q).terminates :=
begin
split;
intro h;
cases h with n h;
use n;
simp only [seq.terminated_at, (coe_of_s_nth_rat_eq v_eq_q n).symm] at h ⊢;
cases ((of q).s.nth n);
trivial
end
end rat_translation
section terminates_of_rat
/-!
### Continued Fractions of Rationals Terminate
Finally, we show that the continued fraction of a rational number terminates.
The crucial insight is that, given any `q : ℚ` with `0 < q < 1`, the numerator of `int.fract q` is
smaller than the numerator of `q`. As the continued fraction computation recursively operates on
the fractional part of a value `v` and `0 ≤ int.fract v < 1`, we infer that the numerator of the
fractional part in the computation decreases by at least one in each step. As `0 ≤ int.fract v`,
this process must stop after finite number of steps, and the computation hence terminates.
-/
namespace int_fract_pair
variables {q : ℚ} {n : ℕ}
/--
Shows that for any `q : ℚ` with `0 < q < 1`, the numerator of the fractional part of
`int_fract_pair.of q⁻¹` is smaller than the numerator of `q`.
-/
lemma of_inv_fr_num_lt_num_of_pos (q_pos : 0 < q) :
(int_fract_pair.of q⁻¹).fr.num < q.num :=
rat.fract_inv_num_lt_num_of_pos q_pos
/-- Shows that the sequence of numerators of the fractional parts of the stream is strictly
antitone. -/
lemma stream_succ_nth_fr_num_lt_nth_fr_num_rat {ifp_n ifp_succ_n : int_fract_pair ℚ}
(stream_nth_eq : int_fract_pair.stream q n = some ifp_n)
(stream_succ_nth_eq : int_fract_pair.stream q (n + 1) = some ifp_succ_n) :
ifp_succ_n.fr.num < ifp_n.fr.num :=
begin
obtain ⟨ifp_n', stream_nth_eq', ifp_n_fract_ne_zero, int_fract_pair.of_eq_ifp_succ_n⟩ :
∃ ifp_n', int_fract_pair.stream q n = some ifp_n' ∧ ifp_n'.fr ≠ 0
∧ int_fract_pair.of ifp_n'.fr⁻¹ = ifp_succ_n, from
succ_nth_stream_eq_some_iff.elim_left stream_succ_nth_eq,
have : ifp_n = ifp_n', by injection (eq.trans stream_nth_eq.symm stream_nth_eq'),
cases this,
rw [←int_fract_pair.of_eq_ifp_succ_n],
cases (nth_stream_fr_nonneg_lt_one stream_nth_eq) with zero_le_ifp_n_fract ifp_n_fract_lt_one,
have : 0 < ifp_n.fr, from (lt_of_le_of_ne zero_le_ifp_n_fract $ ifp_n_fract_ne_zero.symm),
exact (of_inv_fr_num_lt_num_of_pos this)
end
lemma stream_nth_fr_num_le_fr_num_sub_n_rat : ∀ {ifp_n : int_fract_pair ℚ},
int_fract_pair.stream q n = some ifp_n → ifp_n.fr.num ≤ (int_fract_pair.of q).fr.num - n :=
begin
induction n with n IH,
case nat.zero
{ assume ifp_zero stream_zero_eq,
have : int_fract_pair.of q = ifp_zero, by injection stream_zero_eq,
simp [le_refl, this.symm] },
case nat.succ
{ assume ifp_succ_n stream_succ_nth_eq,
suffices : ifp_succ_n.fr.num + 1 ≤ (int_fract_pair.of q).fr.num - n, by
{ rw [int.coe_nat_succ, sub_add_eq_sub_sub],
solve_by_elim [le_sub_right_of_add_le] },
rcases (succ_nth_stream_eq_some_iff.elim_left stream_succ_nth_eq) with
⟨ifp_n, stream_nth_eq, -⟩,
have : ifp_succ_n.fr.num < ifp_n.fr.num, from
stream_succ_nth_fr_num_lt_nth_fr_num_rat stream_nth_eq stream_succ_nth_eq,
have : ifp_succ_n.fr.num + 1 ≤ ifp_n.fr.num, from int.add_one_le_of_lt this,
exact (le_trans this (IH stream_nth_eq)) }
end
lemma exists_nth_stream_eq_none_of_rat (q : ℚ) : ∃ (n : ℕ), int_fract_pair.stream q n = none :=
begin
let fract_q_num := (int.fract q).num, let n := fract_q_num.nat_abs + 1,
cases stream_nth_eq : (int_fract_pair.stream q n) with ifp,
{ use n, exact stream_nth_eq },
{ -- arrive at a contradiction since the numerator decreased num + 1 times but every fractional
-- value is nonnegative.
have ifp_fr_num_le_q_fr_num_sub_n : ifp.fr.num ≤ fract_q_num - n, from
stream_nth_fr_num_le_fr_num_sub_n_rat stream_nth_eq,
have : fract_q_num - n = -1, by
{ have : 0 ≤ fract_q_num, from rat.num_nonneg_iff_zero_le.elim_right (int.fract_nonneg q),
simp [(int.nat_abs_of_nonneg this), sub_add_eq_sub_sub_swap, sub_right_comm] },
have : ifp.fr.num ≤ -1, by rwa this at ifp_fr_num_le_q_fr_num_sub_n,
have : 0 ≤ ifp.fr, from (nth_stream_fr_nonneg_lt_one stream_nth_eq).left,
have : 0 ≤ ifp.fr.num, from rat.num_nonneg_iff_zero_le.elim_right this,
linarith }
end
end int_fract_pair
/-- The continued fraction of a rational number terminates. -/
theorem terminates_of_rat (q : ℚ) : (of q).terminates :=
exists.elim (int_fract_pair.exists_nth_stream_eq_none_of_rat q)
( assume n stream_nth_eq_none,
exists.intro n
( have int_fract_pair.stream q (n + 1) = none, from
int_fract_pair.stream_is_seq q stream_nth_eq_none,
(of_terminated_at_n_iff_succ_nth_int_fract_pair_stream_eq_none.elim_right this) ) )
end terminates_of_rat
/--
The continued fraction `generalized_continued_fraction.of v` terminates if and only if `v ∈ ℚ`.
-/
theorem terminates_iff_rat (v : K) : (of v).terminates ↔ ∃ (q : ℚ), v = (q : K) :=
iff.intro
( assume terminates_v : (of v).terminates,
show ∃ (q : ℚ), v = (q : K), from exists_rat_eq_of_terminates terminates_v )
( assume exists_q_eq_v : ∃ (q : ℚ), v = (↑q : K),
exists.elim exists_q_eq_v
( assume q,
assume v_eq_q : v = ↑q,
have (of q).terminates, from terminates_of_rat q,
(of_terminates_iff_of_rat_terminates v_eq_q).elim_right this ) )
end generalized_continued_fraction
|
d81c759f816c87ea73eb9796a665fe7f3db437f6 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Meta/Tactic/Constructor.lean | ff6ed556924f3cb89be7c23871169f8c704d2893 | [
"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 | 2,154 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.Check
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.Apply
namespace Lean.Meta
/--
When the goal `mvarId` type is an inductive datatype,
`constructor` calls `apply` with the first matching constructor.
-/
def _root_.Lean.MVarId.constructor (mvarId : MVarId) (cfg : ApplyConfig := {}) : MetaM (List MVarId) := do
mvarId.withContext do
mvarId.checkNotAssigned `constructor
let target ← mvarId.getType'
matchConstInduct target.getAppFn
(fun _ => throwTacticEx `constructor mvarId "target is not an inductive datatype")
fun ival us => do
for ctor in ival.ctors do
try
return ← mvarId.apply (Lean.mkConst ctor us) cfg
catch _ =>
pure ()
throwTacticEx `constructor mvarId "no applicable constructor found"
@[deprecated MVarId.constructor]
def constructor (mvarId : MVarId) (cfg : ApplyConfig := {}) : MetaM (List MVarId) := do
mvarId.constructor cfg
def _root_.Lean.MVarId.existsIntro (mvarId : MVarId) (w : Expr) : MetaM MVarId := do
mvarId.withContext do
mvarId.checkNotAssigned `exists
let target ← mvarId.getType'
matchConstStruct target.getAppFn
(fun _ => throwTacticEx `exists mvarId "target is not an inductive datatype with one constructor")
fun _ us cval => do
if cval.numFields < 2 then
throwTacticEx `exists mvarId "constructor must have at least two fields"
let ctor := mkAppN (Lean.mkConst cval.name us) target.getAppArgs[:cval.numParams]
let ctorType ← inferType ctor
let (mvars, _, _) ← forallMetaTelescopeReducing ctorType (some (cval.numFields-2))
let f := mkAppN ctor mvars
checkApp f w
let [mvarId] ← mvarId.apply <| mkApp f w
| throwTacticEx `exists mvarId "unexpected number of subgoals"
pure mvarId
@[deprecated MVarId.existsIntro]
def existsIntro (mvarId : MVarId) (w : Expr) : MetaM MVarId := do
mvarId.existsIntro w
end Lean.Meta
|
5cb8cb270430b6e710dff86725a3ebb61155ac05 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/fin/basic.lean | 3c38b7ab48370b1077c18a33e8c28fd7e616133f | [
"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 | 70,868 | lean | /-
Copyright (c) 2017 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis, Keeley Hoek
-/
import algebra.ne_zero
import algebra.order.with_zero
import order.rel_iso.basic
import data.nat.order.basic
import order.hom.set
/-!
# The finite type with `n` elements
`fin n` is the type whose elements are natural numbers smaller than `n`.
This file expands on the development in the core library.
## Main definitions
### Induction principles
* `fin_zero_elim` : Elimination principle for the empty set `fin 0`, generalizes `fin.elim0`.
* `fin.succ_rec` : Define `C n i` by induction on `i : fin n` interpreted
as `(0 : fin (n - i)).succ.succ…`. This function has two arguments: `H0 n` defines
`0`-th element `C (n+1) 0` of an `(n+1)`-tuple, and `Hs n i` defines `(i+1)`-st element
of `(n+1)`-tuple based on `n`, `i`, and `i`-th element of `n`-tuple.
* `fin.succ_rec_on` : same as `fin.succ_rec` but `i : fin n` is the first argument;
* `fin.induction` : Define `C i` by induction on `i : fin (n + 1)`, separating into the
`nat`-like base cases of `C 0` and `C (i.succ)`.
* `fin.induction_on` : same as `fin.induction` but with `i : fin (n + 1)` as the first argument.
* `fin.cases` : define `f : Π i : fin n.succ, C i` by separately handling the cases `i = 0` and
`i = fin.succ j`, `j : fin n`, defined using `fin.induction`.
* `fin.reverse_induction`: reverse induction on `i : fin (n + 1)`; given `C (fin.last n)` and
`∀ i : fin n, C (fin.succ i) → C (fin.cast_succ i)`, constructs all values `C i` by going down;
* `fin.last_cases`: define `f : Π i, fin (n + 1), C i` by separately handling the cases
`i = fin.last n` and `i = fin.cast_succ j`, a special case of `fin.reverse_induction`;
* `fin.add_cases`: define a function on `fin (m + n)` by separately handling the cases
`fin.cast_add n i` and `fin.nat_add m i`;
* `fin.succ_above_cases`: given `i : fin (n + 1)`, define a function on `fin (n + 1)` by separately
handling the cases `j = i` and `j = fin.succ_above i k`, same as `fin.insert_nth` but marked
as eliminator and works for `Sort*`.
### Order embeddings and an order isomorphism
* `fin.order_iso_subtype` : coercion to `{ i // i < n }` as an `order_iso`;
* `fin.coe_embedding` : coercion to natural numbers as an `embedding`;
* `fin.coe_order_embedding` : coercion to natural numbers as an `order_embedding`;
* `fin.succ_embedding` : `fin.succ` as an `order_embedding`;
* `fin.cast_le h` : embed `fin n` into `fin m`, `h : n ≤ m`;
* `fin.cast eq` : order isomorphism between `fin n` and fin m` provided that `n = m`,
see also `equiv.fin_congr`;
* `fin.cast_add m` : embed `fin n` into `fin (n+m)`;
* `fin.cast_succ` : embed `fin n` into `fin (n+1)`;
* `fin.succ_above p` : embed `fin n` into `fin (n + 1)` with a hole around `p`;
* `fin.add_nat m i` : add `m` on `i` on the right, generalizes `fin.succ`;
* `fin.nat_add n i` adds `n` on `i` on the left;
### Other casts
* `fin.of_nat'`: given a positive number `n` (deduced from `[ne_zero n]`), `fin.of_nat' i` is
`i % n` interpreted as an element of `fin n`;
* `fin.cast_lt i h` : embed `i` into a `fin` where `h` proves it belongs into;
* `fin.pred_above (p : fin n) i` : embed `i : fin (n+1)` into `fin n` by subtracting one if `p < i`;
* `fin.cast_pred` : embed `fin (n + 2)` into `fin (n + 1)` by mapping `fin.last (n + 1)` to
`fin.last n`;
* `fin.sub_nat i h` : subtract `m` from `i ≥ m`, generalizes `fin.pred`;
* `fin.clamp n m` : `min n m` as an element of `fin (m + 1)`;
* `fin.div_nat i` : divides `i : fin (m * n)` by `n`;
* `fin.mod_nat i` : takes the mod of `i : fin (m * n)` by `n`;
### Misc definitions
* `fin.last n` : The greatest value of `fin (n+1)`.
* `fin.rev : fin n → fin n` : the antitone involution given by `i ↦ n-(i+1)`
-/
universes u v
open fin nat function
/-- Elimination principle for the empty set `fin 0`, dependent version. -/
def fin_zero_elim {α : fin 0 → Sort u} (x : fin 0) : α x := x.elim0
namespace fin
/-- A non-dependent variant of `elim0`. -/
def elim0' {α : Sort*} (x : fin 0) : α := x.elim0
variables {n m : ℕ} {a b : fin n}
instance fin_to_nat (n : ℕ) : has_coe (fin n) nat := ⟨fin.val⟩
lemma val_injective : function.injective (@fin.val n) := @fin.eq_of_veq n
protected lemma prop (a : fin n) : a.val < n := a.2
@[simp] lemma is_lt (a : fin n) : (a : ℕ) < n := a.2
protected lemma pos (i : fin n) : 0 < n := lt_of_le_of_lt (nat.zero_le _) i.is_lt
lemma pos_iff_nonempty {n : ℕ} : 0 < n ↔ nonempty (fin n) := ⟨λ h, ⟨⟨0, h⟩⟩, λ ⟨i⟩, i.pos⟩
/-- Equivalence between `fin n` and `{ i // i < n }`. -/
@[simps apply symm_apply]
def equiv_subtype : fin n ≃ { i // i < n } :=
{ to_fun := λ a, ⟨a.1, a.2⟩, inv_fun := λ a, ⟨a.1, a.2⟩,
left_inv := λ ⟨_, _⟩, rfl, right_inv := λ ⟨_, _⟩, rfl }
section coe
/-!
### coercions and constructions
-/
@[simp] protected lemma eta (a : fin n) (h : (a : ℕ) < n) : (⟨(a : ℕ), h⟩ : fin n) = a :=
by cases a; refl
@[ext]
lemma ext {a b : fin n} (h : (a : ℕ) = b) : a = b := eq_of_veq h
lemma ext_iff {a b : fin n} : a = b ↔ (a : ℕ) = b :=
iff.intro (congr_arg _) fin.eq_of_veq
lemma coe_injective {n : ℕ} : injective (coe : fin n → ℕ) := fin.val_injective
lemma coe_eq_coe (a b : fin n) : (a : ℕ) = b ↔ a = b :=
ext_iff.symm
lemma eq_iff_veq (a b : fin n) : a = b ↔ a.1 = b.1 :=
⟨veq_of_eq, eq_of_veq⟩
lemma ne_iff_vne (a b : fin n) : a ≠ b ↔ a.1 ≠ b.1 :=
⟨vne_of_ne, ne_of_vne⟩
@[simp, nolint simp_nf] -- built-in reduction doesn't always work
theorem mk_eq_mk {a h a' h'} : @mk n a h = @mk n a' h' ↔ a = a' :=
ext_iff
protected lemma mk.inj_iff {n a b : ℕ} {ha : a < n} {hb : b < n} :
(⟨a, ha⟩ : fin n) = ⟨b, hb⟩ ↔ a = b :=
eq_iff_veq _ _
lemma mk_val {m n : ℕ} (h : m < n) : (⟨m, h⟩ : fin n).val = m := rfl
lemma eq_mk_iff_coe_eq {k : ℕ} {hk : k < n} : a = ⟨k, hk⟩ ↔ (a : ℕ) = k :=
fin.eq_iff_veq a ⟨k, hk⟩
@[simp, norm_cast] lemma coe_mk {m n : ℕ} (h : m < n) : ((⟨m, h⟩ : fin n) : ℕ) = m := rfl
lemma mk_coe (i : fin n) : (⟨i, i.property⟩ : fin n) = i :=
fin.eta _ _
lemma coe_eq_val (a : fin n) : (a : ℕ) = a.val := rfl
@[simp] lemma val_eq_coe (a : fin n) : a.val = a := rfl
/-- Assume `k = l`. If two functions defined on `fin k` and `fin l` are equal on each element,
then they coincide (in the heq sense). -/
protected lemma heq_fun_iff {α : Sort*} {k l : ℕ} (h : k = l) {f : fin k → α} {g : fin l → α} :
f == g ↔ (∀ (i : fin k), f i = g ⟨(i : ℕ), h ▸ i.2⟩) :=
by { subst h, simp [function.funext_iff] }
protected lemma heq_ext_iff {k l : ℕ} (h : k = l) {i : fin k} {j : fin l} :
i == j ↔ (i : ℕ) = (j : ℕ) :=
by { subst h, simp [coe_eq_coe] }
lemma exists_iff {p : fin n → Prop} : (∃ i, p i) ↔ ∃ i h, p ⟨i, h⟩ :=
⟨λ h, exists.elim h (λ ⟨i, hi⟩ hpi, ⟨i, hi, hpi⟩),
λ h, exists.elim h (λ i hi, ⟨⟨i, hi.fst⟩, hi.snd⟩)⟩
lemma forall_iff {p : fin n → Prop} : (∀ i, p i) ↔ ∀ i h, p ⟨i, h⟩ :=
⟨λ h i hi, h ⟨i, hi⟩, λ h ⟨i, hi⟩, h i hi⟩
end coe
section order
/-!
### order
-/
lemma is_le (i : fin (n + 1)) : (i : ℕ) ≤ n := le_of_lt_succ i.is_lt
@[simp] lemma is_le' : (a : ℕ) ≤ n := le_of_lt a.is_lt
lemma lt_iff_coe_lt_coe : a < b ↔ (a : ℕ) < b := iff.rfl
lemma le_iff_coe_le_coe : a ≤ b ↔ (a : ℕ) ≤ b := iff.rfl
lemma mk_lt_of_lt_coe {a : ℕ} (h : a < b) : (⟨a, h.trans b.is_lt⟩ : fin n) < b := h
lemma mk_le_of_le_coe {a : ℕ} (h : a ≤ b) : (⟨a, h.trans_lt b.is_lt⟩ : fin n) ≤ b := h
/-- `a < b` as natural numbers if and only if `a < b` in `fin n`. -/
@[norm_cast, simp] lemma coe_fin_lt {n : ℕ} {a b : fin n} : (a : ℕ) < (b : ℕ) ↔ a < b :=
iff.rfl
/-- `a ≤ b` as natural numbers if and only if `a ≤ b` in `fin n`. -/
@[norm_cast, simp] lemma coe_fin_le {n : ℕ} {a b : fin n} : (a : ℕ) ≤ (b : ℕ) ↔ a ≤ b :=
iff.rfl
instance {n : ℕ} : linear_order (fin n) :=
@linear_order.lift (fin n) _ _ ⟨λ x y, ⟨max x y, max_rec' (< n) x.2 y.2⟩⟩
⟨λ x y, ⟨min x y, min_rec' (< n) x.2 y.2⟩⟩ fin.val fin.val_injective (λ _ _, rfl) (λ _ _, rfl)
@[simp] lemma mk_le_mk {x y : nat} {hx} {hy} : (⟨x, hx⟩ : fin n) ≤ ⟨y, hy⟩ ↔ x ≤ y := iff.rfl
@[simp] lemma mk_lt_mk {x y : nat} {hx} {hy} : (⟨x, hx⟩ : fin n) < ⟨y, hy⟩ ↔ x < y := iff.rfl
@[simp] lemma min_coe : min (a : ℕ) n = a := by simp
@[simp] lemma max_coe : max (a : ℕ) n = n := by simp
instance {n : ℕ} : partial_order (fin n) := by apply_instance
lemma coe_strict_mono : strict_mono (coe : fin n → ℕ) := λ _ _, id
/-- The equivalence `fin n ≃ { i // i < n }` is an order isomorphism. -/
@[simps apply symm_apply]
def order_iso_subtype : fin n ≃o { i // i < n } :=
equiv_subtype.to_order_iso (by simp [monotone]) (by simp [monotone])
/-- The inclusion map `fin n → ℕ` is an embedding. -/
@[simps apply]
def coe_embedding : fin n ↪ ℕ :=
⟨coe, coe_injective⟩
@[simp] lemma equiv_subtype_symm_trans_val_embedding :
equiv_subtype.symm.to_embedding.trans coe_embedding = embedding.subtype (< n) :=
rfl
/-- The inclusion map `fin n → ℕ` is an order embedding. -/
@[simps apply]
def coe_order_embedding (n) : (fin n) ↪o ℕ :=
⟨coe_embedding, λ a b, iff.rfl⟩
/-- The ordering on `fin n` is a well order. -/
instance fin.lt.is_well_order (n) : is_well_order (fin n) (<) :=
(coe_order_embedding n).is_well_order
/-- Use the ordering on `fin n` for checking recursive definitions.
For example, the following definition is not accepted by the termination checker,
unless we declare the `has_well_founded` instance:
```lean
def factorial {n : ℕ} : fin n → ℕ
| ⟨0, _⟩ := 1
| ⟨i + 1, hi⟩ := (i + 1) * factorial ⟨i, i.lt_succ_self.trans hi⟩
```
-/
instance {n : ℕ} : has_well_founded (fin n) :=
⟨_, measure_wf coe⟩
@[simp] lemma coe_zero {n : ℕ} : ((0 : fin (n+1)) : ℕ) = 0 := rfl
attribute [simp] val_zero
@[simp] lemma val_zero' (n) : (0 : fin (n+1)).val = 0 := rfl
@[simp] lemma mk_zero : (⟨0, nat.succ_pos'⟩ : fin (n + 1)) = (0 : fin _) := rfl
@[simp] lemma zero_le (a : fin (n + 1)) : 0 ≤ a := zero_le a.1
lemma zero_lt_one : (0 : fin (n + 2)) < 1 := nat.zero_lt_one
@[simp] lemma not_lt_zero (a : fin n.succ) : ¬a < 0.
lemma pos_iff_ne_zero (a : fin (n+1)) : 0 < a ↔ a ≠ 0 :=
by rw [← coe_fin_lt, coe_zero, pos_iff_ne_zero, ne.def, ne.def, ext_iff, coe_zero]
lemma eq_zero_or_eq_succ {n : ℕ} (i : fin (n+1)) : i = 0 ∨ ∃ j : fin n, i = j.succ :=
begin
rcases i with ⟨_|j, h⟩,
{ left, refl, },
{ right, exact ⟨⟨j, nat.lt_of_succ_lt_succ h⟩, rfl⟩, }
end
lemma eq_succ_of_ne_zero {n : ℕ} {i : fin (n + 1)} (hi : i ≠ 0) : ∃ j : fin n, i = j.succ :=
(eq_zero_or_eq_succ i).resolve_left hi
/-- The antitone involution `fin n → fin n` given by `i ↦ n-(i+1)`. -/
def rev : equiv.perm (fin n) :=
involutive.to_perm (λ i, ⟨n - (i + 1), tsub_lt_self i.pos (nat.succ_pos _)⟩) $
λ i, ext $ by rw [coe_mk, coe_mk, ← tsub_tsub,
tsub_tsub_cancel_of_le (nat.add_one_le_iff.2 i.is_lt), add_tsub_cancel_right]
@[simp] lemma coe_rev (i : fin n) : (i.rev : ℕ) = n - (i + 1) := rfl
lemma rev_involutive : involutive (@rev n) := involutive.to_perm_involutive _
lemma rev_injective : injective (@rev n) := rev_involutive.injective
lemma rev_surjective : surjective (@rev n) := rev_involutive.surjective
lemma rev_bijective : bijective (@rev n) := rev_involutive.bijective
@[simp] lemma rev_inj {i j : fin n} : i.rev = j.rev ↔ i = j := rev_injective.eq_iff
@[simp] lemma rev_rev (i : fin n) : i.rev.rev = i := rev_involutive _
@[simp] lemma rev_symm : (@rev n).symm = rev := rfl
lemma rev_eq {n a : ℕ} (i : fin (n+1)) (h : n=a+i) :
i.rev = ⟨a, nat.lt_succ_iff.mpr (nat.le.intro (h.symm))⟩ :=
begin
ext,
dsimp,
conv_lhs { congr, rw h, },
rw [add_assoc, add_tsub_cancel_right],
end
@[simp] lemma rev_le_rev {i j : fin n} : i.rev ≤ j.rev ↔ j ≤ i :=
by simp only [le_iff_coe_le_coe, coe_rev, tsub_le_tsub_iff_left (nat.add_one_le_iff.2 j.is_lt),
add_le_add_iff_right]
@[simp] lemma rev_lt_rev {i j : fin n} : i.rev < j.rev ↔ j < i :=
lt_iff_lt_of_le_iff_le rev_le_rev
/-- `fin.rev n` as an order-reversing isomorphism. -/
@[simps apply to_equiv] def rev_order_iso {n} : (fin n)ᵒᵈ ≃o fin n :=
⟨order_dual.of_dual.trans rev, λ i j, rev_le_rev⟩
@[simp] lemma rev_order_iso_symm_apply (i : fin n) :
rev_order_iso.symm i = order_dual.to_dual i.rev := rfl
/-- The greatest value of `fin (n+1)` -/
def last (n : ℕ) : fin (n+1) := ⟨_, n.lt_succ_self⟩
@[simp, norm_cast] lemma coe_last (n : ℕ) : (last n : ℕ) = n := rfl
lemma last_val (n : ℕ) : (last n).val = n := rfl
theorem le_last (i : fin (n+1)) : i ≤ last n :=
le_of_lt_succ i.is_lt
instance : bounded_order (fin (n + 1)) :=
{ top := last n,
le_top := le_last,
bot := 0,
bot_le := zero_le }
instance : lattice (fin (n + 1)) := linear_order.to_lattice
lemma last_pos : (0 : fin (n + 2)) < last (n + 1) :=
by simp [lt_iff_coe_lt_coe]
lemma eq_last_of_not_lt {i : fin (n+1)} (h : ¬ (i : ℕ) < n) : i = last n :=
le_antisymm (le_last i) (not_lt.1 h)
lemma top_eq_last (n : ℕ) : ⊤ = fin.last n := rfl
lemma bot_eq_zero (n : ℕ) : ⊥ = (0 : fin (n + 1)) := rfl
section
variables {α : Type*} [preorder α]
open set
/-- If `e` is an `order_iso` between `fin n` and `fin m`, then `n = m` and `e` is the identity
map. In this lemma we state that for each `i : fin n` we have `(e i : ℕ) = (i : ℕ)`. -/
@[simp] lemma coe_order_iso_apply (e : fin n ≃o fin m) (i : fin n) : (e i : ℕ) = i :=
begin
rcases i with ⟨i, hi⟩,
rw [fin.coe_mk],
induction i using nat.strong_induction_on with i h,
refine le_antisymm (forall_lt_iff_le.1 $ λ j hj, _) (forall_lt_iff_le.1 $ λ j hj, _),
{ have := e.symm.lt_iff_lt.2 (mk_lt_of_lt_coe hj),
rw e.symm_apply_apply at this,
convert this,
simpa using h _ this (e.symm _).is_lt },
{ rwa [← h j hj (hj.trans hi), ← lt_iff_coe_lt_coe, e.lt_iff_lt] }
end
instance order_iso_subsingleton : subsingleton (fin n ≃o α) :=
⟨λ e e', by { ext i,
rw [← e.symm.apply_eq_iff_eq, e.symm_apply_apply, ← e'.trans_apply, ext_iff,
coe_order_iso_apply] }⟩
instance order_iso_subsingleton' : subsingleton (α ≃o fin n) :=
order_iso.symm_injective.subsingleton
instance order_iso_unique : unique (fin n ≃o fin n) := unique.mk' _
/-- Two strictly monotone functions from `fin n` are equal provided that their ranges
are equal. -/
lemma strict_mono_unique {f g : fin n → α} (hf : strict_mono f) (hg : strict_mono g)
(h : range f = range g) : f = g :=
have (hf.order_iso f).trans (order_iso.set_congr _ _ h) = hg.order_iso g,
from subsingleton.elim _ _,
congr_arg (function.comp (coe : range g → α)) (funext $ rel_iso.ext_iff.1 this)
/-- Two order embeddings of `fin n` are equal provided that their ranges are equal. -/
lemma order_embedding_eq {f g : fin n ↪o α} (h : range f = range g) : f = g :=
rel_embedding.ext $ funext_iff.1 $ strict_mono_unique f.strict_mono g.strict_mono h
end
end order
section add
/-!
### addition, numerals, and coercion from nat
-/
/-- Given a positive `n`, `fin.of_nat' i` is `i % n` as an element of `fin n`. -/
def of_nat' [ne_zero n] (i : ℕ) : fin n := ⟨i%n, mod_lt _ $ ne_zero.pos n⟩
lemma one_val {n : ℕ} : (1 : fin (n+1)).val = 1 % (n+1) := rfl
lemma coe_one' {n : ℕ} : ((1 : fin (n+1)) : ℕ) = 1 % (n+1) := rfl
@[simp] lemma val_one {n : ℕ} : (1 : fin (n+2)).val = 1 := rfl
@[simp] lemma coe_one {n : ℕ} : ((1 : fin (n+2)) : ℕ) = 1 := rfl
@[simp] lemma mk_one : (⟨1, nat.succ_lt_succ (nat.succ_pos n)⟩ : fin (n + 2)) = (1 : fin _) := rfl
instance {n : ℕ} : nontrivial (fin (n + 2)) := ⟨⟨0, 1, dec_trivial⟩⟩
lemma nontrivial_iff_two_le : nontrivial (fin n) ↔ 2 ≤ n :=
by rcases n with _|_|n; simp [fin.nontrivial, not_nontrivial, nat.succ_le_iff]
lemma subsingleton_iff_le_one : subsingleton (fin n) ↔ n ≤ 1 :=
by rcases n with _|_|n; simp [is_empty.subsingleton, unique.subsingleton, not_subsingleton]
section monoid
@[simp] protected lemma add_zero (k : fin (n + 1)) : k + 0 = k :=
by simp [eq_iff_veq, add_def, mod_eq_of_lt (is_lt k)]
@[simp] protected lemma zero_add (k : fin (n + 1)) : (0 : fin (n + 1)) + k = k :=
by simp [eq_iff_veq, add_def, mod_eq_of_lt (is_lt k)]
instance add_comm_monoid (n : ℕ) : add_comm_monoid (fin (n + 1)) :=
{ add := (+),
add_assoc := by simp [eq_iff_veq, add_def, add_assoc],
zero := 0,
zero_add := fin.zero_add,
add_zero := fin.add_zero,
add_comm := by simp [eq_iff_veq, add_def, add_comm] }
instance : add_monoid_with_one (fin (n + 1)) :=
{ one := 1,
nat_cast := fin.of_nat,
nat_cast_zero := rfl,
nat_cast_succ := λ i, eq_of_veq (add_mod _ _ _),
.. fin.add_comm_monoid n }
end monoid
lemma val_add {n : ℕ} : ∀ a b : fin n, (a + b).val = (a.val + b.val) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma coe_add {n : ℕ} : ∀ a b : fin n, ((a + b : fin n) : ℕ) = (a + b) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma coe_add_eq_ite {n : ℕ} (a b : fin n) :
(↑(a + b) : ℕ) = if n ≤ a + b then a + b - n else a + b :=
by rw [fin.coe_add, nat.add_mod_eq_ite,
nat.mod_eq_of_lt (show ↑a < n, from a.2), nat.mod_eq_of_lt (show ↑b < n, from b.2)]
lemma coe_bit0 {n : ℕ} (k : fin n) : ((bit0 k : fin n) : ℕ) = bit0 (k : ℕ) % n :=
by { cases k, refl }
lemma coe_bit1 {n : ℕ} (k : fin (n + 1)) :
((bit1 k : fin (n + 1)) : ℕ) = bit1 (k : ℕ) % (n + 1) :=
begin
cases n, { cases k with k h, cases k, {show _ % _ = _, simp}, cases h with _ h, cases h },
simp [bit1, fin.coe_bit0, fin.coe_add, fin.coe_one],
end
lemma coe_add_one_of_lt {n : ℕ} {i : fin n.succ} (h : i < last _) :
(↑(i + 1) : ℕ) = i + 1 :=
begin
-- First show that `((1 : fin n.succ) : ℕ) = 1`, because `n.succ` is at least 2.
cases n,
{ cases h },
-- Then just unfold the definitions.
rw [fin.coe_add, fin.coe_one, nat.mod_eq_of_lt (nat.succ_lt_succ _)],
exact h
end
@[simp] lemma last_add_one : ∀ n, last n + 1 = 0
| 0 := subsingleton.elim _ _
| (n + 1) := by { ext, rw [coe_add, coe_zero, coe_last, coe_one, nat.mod_self] }
lemma coe_add_one {n : ℕ} (i : fin (n + 1)) :
((i + 1 : fin (n + 1)) : ℕ) = if i = last _ then 0 else i + 1 :=
begin
rcases (le_last i).eq_or_lt with rfl|h,
{ simp },
{ simpa [h.ne] using coe_add_one_of_lt h }
end
section bit
@[simp] lemma mk_bit0 {m n : ℕ} (h : bit0 m < n) :
(⟨bit0 m, h⟩ : fin n) = (bit0 ⟨m, (nat.le_add_right m m).trans_lt h⟩ : fin _) :=
eq_of_veq (nat.mod_eq_of_lt h).symm
@[simp] lemma mk_bit1 {m n : ℕ} (h : bit1 m < n + 1) :
(⟨bit1 m, h⟩ : fin (n + 1)) = (bit1 ⟨m, (nat.le_add_right m m).trans_lt
((m + m).lt_succ_self.trans h)⟩ : fin _) :=
begin
ext,
simp only [bit1, bit0] at h,
simp only [bit1, bit0, coe_add, coe_one', coe_mk, ←nat.add_mod, nat.mod_eq_of_lt h],
end
end bit
@[simp] lemma val_two {n : ℕ} : (2 : fin (n+3)).val = 2 := rfl
@[simp] lemma coe_two {n : ℕ} : ((2 : fin (n+3)) : ℕ) = 2 := rfl
section of_nat_coe
@[simp]
lemma of_nat_eq_coe (n : ℕ) (a : ℕ) : (of_nat a : fin (n+1)) = a :=
rfl
/-- Converting an in-range number to `fin (n + 1)` produces a result
whose value is the original number. -/
lemma coe_val_of_lt {n : ℕ} {a : ℕ} (h : a < n + 1) :
((a : fin (n + 1)).val) = a :=
begin
rw ←of_nat_eq_coe,
exact nat.mod_eq_of_lt h
end
/-- Converting the value of a `fin (n + 1)` to `fin (n + 1)` results
in the same value. -/
lemma coe_val_eq_self {n : ℕ} (a : fin (n + 1)) : (a.val : fin (n + 1)) = a :=
begin
rw fin.eq_iff_veq,
exact coe_val_of_lt a.property
end
/-- Coercing an in-range number to `fin (n + 1)`, and converting back
to `ℕ`, results in that number. -/
lemma coe_coe_of_lt {n : ℕ} {a : ℕ} (h : a < n + 1) :
((a : fin (n + 1)) : ℕ) = a :=
coe_val_of_lt h
/-- Converting a `fin (n + 1)` to `ℕ` and back results in the same
value. -/
@[simp] lemma coe_coe_eq_self {n : ℕ} (a : fin (n + 1)) : ((a : ℕ) : fin (n + 1)) = a :=
coe_val_eq_self a
lemma coe_nat_eq_last (n) : (n : fin (n + 1)) = fin.last n :=
by { rw [←fin.of_nat_eq_coe, fin.of_nat, fin.last], simp only [nat.mod_eq_of_lt n.lt_succ_self] }
lemma le_coe_last (i : fin (n + 1)) : i ≤ n :=
by { rw fin.coe_nat_eq_last, exact fin.le_last i }
end of_nat_coe
lemma add_one_pos (i : fin (n + 1)) (h : i < fin.last n) : (0 : fin (n + 1)) < i + 1 :=
begin
cases n,
{ exact absurd h (nat.not_lt_zero _) },
{ rw [lt_iff_coe_lt_coe, coe_last, ←add_lt_add_iff_right 1] at h,
rw [lt_iff_coe_lt_coe, coe_add, coe_zero, coe_one, nat.mod_eq_of_lt h],
exact nat.zero_lt_succ _ }
end
lemma one_pos : (0 : fin (n + 2)) < 1 := succ_pos 0
lemma zero_ne_one : (0 : fin (n + 2)) ≠ 1 := ne_of_lt one_pos
@[simp] lemma zero_eq_one_iff : (0 : fin (n + 1)) = 1 ↔ n = 0 :=
begin
split,
{ cases n; intro h,
{ refl },
{ have := zero_ne_one, contradiction } },
{ rintro rfl, refl }
end
@[simp] lemma one_eq_zero_iff : (1 : fin (n + 1)) = 0 ↔ n = 0 :=
by rw [eq_comm, zero_eq_one_iff]
end add
section succ
/-!
### succ and casts into larger fin types
-/
@[simp] lemma coe_succ (j : fin n) : (j.succ : ℕ) = j + 1 :=
by cases j; simp [fin.succ]
@[simp]
lemma succ_pos (a : fin n) : (0 : fin (n + 1)) < a.succ := by simp [lt_iff_coe_lt_coe]
/-- `fin.succ` as an `order_embedding` -/
def succ_embedding (n : ℕ) : fin n ↪o fin (n + 1) :=
order_embedding.of_strict_mono fin.succ $ λ ⟨i, hi⟩ ⟨j, hj⟩ h, succ_lt_succ h
@[simp] lemma coe_succ_embedding : ⇑(succ_embedding n) = fin.succ := rfl
@[simp] lemma succ_le_succ_iff : a.succ ≤ b.succ ↔ a ≤ b :=
(succ_embedding n).le_iff_le
@[simp] lemma succ_lt_succ_iff : a.succ < b.succ ↔ a < b :=
(succ_embedding n).lt_iff_lt
lemma succ_injective (n : ℕ) : injective (@fin.succ n) :=
(succ_embedding n).injective
@[simp] lemma succ_inj {a b : fin n} : a.succ = b.succ ↔ a = b :=
(succ_injective n).eq_iff
lemma succ_ne_zero {n} : ∀ k : fin n, fin.succ k ≠ 0
| ⟨k, hk⟩ heq := nat.succ_ne_zero k $ ext_iff.1 heq
@[simp] lemma succ_zero_eq_one : fin.succ (0 : fin (n + 1)) = 1 := rfl
@[simp] lemma succ_one_eq_two : fin.succ (1 : fin (n + 2)) = 2 := rfl
@[simp] lemma succ_mk (n i : ℕ) (h : i < n) : fin.succ ⟨i, h⟩ = ⟨i + 1, nat.succ_lt_succ h⟩ :=
rfl
lemma mk_succ_pos (i : ℕ) (h : i < n) : (0 : fin (n + 1)) < ⟨i.succ, add_lt_add_right h 1⟩ :=
by { rw [lt_iff_coe_lt_coe, coe_zero], exact nat.succ_pos i }
lemma one_lt_succ_succ (a : fin n) : (1 : fin (n + 2)) < a.succ.succ :=
begin
cases n,
{ exact fin_zero_elim a },
{ rw [←succ_zero_eq_one, succ_lt_succ_iff], exact succ_pos a }
end
@[simp] lemma add_one_lt_iff {n : ℕ} {k : fin (n + 2)} :
k + 1 < k ↔ k = last _ :=
begin
simp only [lt_iff_coe_lt_coe, coe_add, coe_last, ext_iff],
cases k with k hk,
rcases (le_of_lt_succ hk).eq_or_lt with rfl|hk',
{ simp },
{ simp [hk'.ne, mod_eq_of_lt (succ_lt_succ hk'), le_succ _] }
end
@[simp] lemma add_one_le_iff {n : ℕ} {k : fin (n + 1)} :
k + 1 ≤ k ↔ k = last _ :=
begin
cases n,
{ simp [subsingleton.elim (k + 1) k, subsingleton.elim (fin.last _) k] },
rw [←not_iff_not, ←add_one_lt_iff, lt_iff_le_and_ne, not_and'],
refine ⟨λ h _, h, λ h, h _⟩,
rw [ne.def, ext_iff, coe_add_one],
split_ifs with hk hk;
simp [hk, eq_comm],
end
@[simp] lemma last_le_iff {n : ℕ} {k : fin (n + 1)} :
last n ≤ k ↔ k = last n :=
top_le_iff
@[simp] lemma lt_add_one_iff {n : ℕ} {k : fin (n + 1)} :
k < k + 1 ↔ k < last n :=
begin
rw ←not_iff_not,
simp
end
@[simp] lemma le_zero_iff {n : ℕ} {k : fin (n + 1)} :
k ≤ 0 ↔ k = 0 :=
le_bot_iff
lemma succ_succ_ne_one (a : fin n) : fin.succ (fin.succ a) ≠ 1 := ne_of_gt (one_lt_succ_succ a)
/-- `cast_lt i h` embeds `i` into a `fin` where `h` proves it belongs into. -/
def cast_lt (i : fin m) (h : i.1 < n) : fin n := ⟨i.1, h⟩
@[simp] lemma coe_cast_lt (i : fin m) (h : i.1 < n) : (cast_lt i h : ℕ) = i := rfl
@[simp] lemma cast_lt_mk (i n m : ℕ) (hn : i < n) (hm : i < m) : cast_lt ⟨i, hn⟩ hm = ⟨i, hm⟩ := rfl
/-- `cast_le h i` embeds `i` into a larger `fin` type. -/
def cast_le (h : n ≤ m) : fin n ↪o fin m :=
order_embedding.of_strict_mono (λ a, cast_lt a (lt_of_lt_of_le a.2 h)) $ λ a b h, h
@[simp] lemma coe_cast_le (h : n ≤ m) (i : fin n) : (cast_le h i : ℕ) = i := rfl
@[simp] lemma cast_le_mk (i n m : ℕ) (hn : i < n) (h : n ≤ m) :
cast_le h ⟨i, hn⟩ = ⟨i, lt_of_lt_of_le hn h⟩ := rfl
@[simp] lemma cast_le_zero {n m : ℕ} (h : n.succ ≤ m.succ) :
cast_le h 0 = 0 :=
by simp [eq_iff_veq]
@[simp] lemma range_cast_le {n k : ℕ} (h : n ≤ k) :
set.range (cast_le h) = {i | (i : ℕ) < n} :=
set.ext (λ x, ⟨λ ⟨y, hy⟩, hy ▸ y.2, λ hx, ⟨⟨x, hx⟩, fin.ext rfl⟩⟩)
@[simp] lemma coe_of_injective_cast_le_symm {n k : ℕ} (h : n ≤ k) (i : fin k) (hi) :
((equiv.of_injective _ (cast_le h).injective).symm ⟨i, hi⟩ : ℕ) = i :=
begin
rw ← coe_cast_le,
exact congr_arg coe (equiv.apply_of_injective_symm _ _)
end
@[simp] lemma cast_le_succ {m n : ℕ} (h : (m + 1) ≤ (n + 1)) (i : fin m) :
cast_le h i.succ = (cast_le (nat.succ_le_succ_iff.mp h) i).succ :=
by simp [fin.eq_iff_veq]
@[simp] lemma cast_le_cast_le {k m n} (km : k ≤ m) (mn : m ≤ n) (i : fin k) :
fin.cast_le mn (fin.cast_le km i) = fin.cast_le (km.trans mn) i :=
fin.ext (by simp only [coe_cast_le])
@[simp] lemma cast_le_comp_cast_le {k m n} (km : k ≤ m) (mn : m ≤ n) :
fin.cast_le mn ∘ fin.cast_le km = fin.cast_le (km.trans mn) :=
funext (cast_le_cast_le km mn)
/-- `cast eq i` embeds `i` into a equal `fin` type, see also `equiv.fin_congr`. -/
def cast (eq : n = m) : fin n ≃o fin m :=
{ to_equiv := ⟨cast_le eq.le, cast_le eq.symm.le, λ a, eq_of_veq rfl, λ a, eq_of_veq rfl⟩,
map_rel_iff' := λ a b, iff.rfl }
@[simp] lemma symm_cast (h : n = m) : (cast h).symm = cast h.symm := rfl
/-- While `fin.coe_order_iso_apply` is a more general case of this, we mark this `simp` anyway
as it is eligible for `dsimp`. -/
@[simp]
lemma coe_cast (h : n = m) (i : fin n) : (cast h i : ℕ) = i := rfl
@[simp] lemma cast_zero {n' : ℕ} {h : n + 1 = n' + 1} :
cast h (0 : fin (n + 1)) = 0 :=
ext rfl
@[simp] lemma cast_last {n' : ℕ} {h : n + 1 = n' + 1} :
cast h (last n) = last n' :=
ext (by rw [coe_cast, coe_last, coe_last, nat.succ_injective h])
@[simp] lemma cast_mk (h : n = m) (i : ℕ) (hn : i < n) :
cast h ⟨i, hn⟩ = ⟨i, lt_of_lt_of_le hn h.le⟩ := rfl
@[simp] lemma cast_trans {k : ℕ} (h : n = m) (h' : m = k) {i : fin n} :
cast h' (cast h i) = cast (eq.trans h h') i := rfl
@[simp] lemma cast_refl (h : n = n := rfl) : cast h = order_iso.refl (fin n) :=
by { ext, refl }
lemma cast_le_of_eq {m n : ℕ} (h : m = n) {h' : m ≤ n} :
(cast_le h' : fin m → fin n) = fin.cast h :=
funext (λ _, rfl)
/-- While in many cases `fin.cast` is better than `equiv.cast`/`cast`, sometimes we want to apply
a generic theorem about `cast`. -/
lemma cast_to_equiv (h : n = m) : (cast h).to_equiv = equiv.cast (h ▸ rfl) :=
by { subst h, simp }
/-- While in many cases `fin.cast` is better than `equiv.cast`/`cast`, sometimes we want to apply
a generic theorem about `cast`. -/
lemma cast_eq_cast (h : n = m) : (cast h : fin n → fin m) = _root_.cast (h ▸ rfl) :=
by { subst h, ext, simp }
/-- `cast_add m i` embeds `i : fin n` in `fin (n+m)`. See also `fin.nat_add` and `fin.add_nat`. -/
def cast_add (m) : fin n ↪o fin (n + m) := cast_le $ nat.le_add_right n m
@[simp] lemma coe_cast_add (m : ℕ) (i : fin n) : (cast_add m i : ℕ) = i := rfl
@[simp] lemma cast_add_zero : (cast_add 0 : fin n → fin (n + 0)) = cast rfl := rfl
lemma cast_add_lt {m : ℕ} (n : ℕ) (i : fin m) : (cast_add n i : ℕ) < m := i.2
@[simp] lemma cast_add_mk (m : ℕ) (i : ℕ) (h : i < n) :
cast_add m ⟨i, h⟩ = ⟨i, lt_add_right i n m h⟩ := rfl
@[simp] lemma cast_add_cast_lt (m : ℕ) (i : fin (n + m)) (hi : i.val < n) :
cast_add m (cast_lt i hi) = i :=
ext rfl
@[simp] lemma cast_lt_cast_add (m : ℕ) (i : fin n) :
cast_lt (cast_add m i) (cast_add_lt m i) = i :=
ext rfl
/-- For rewriting in the reverse direction, see `fin.cast_cast_add_left`. -/
lemma cast_add_cast {n n' : ℕ} (m : ℕ) (i : fin n') (h : n' = n) :
cast_add m (fin.cast h i) = fin.cast (congr_arg _ h) (cast_add m i) :=
ext rfl
lemma cast_cast_add_left {n n' m : ℕ} (i : fin n') (h : n' + m = n + m) :
cast h (cast_add m i) = cast_add m (cast (add_right_cancel h) i) :=
ext rfl
@[simp] lemma cast_cast_add_right {n m m' : ℕ} (i : fin n) (h : n + m' = n + m) :
cast h (cast_add m' i) = cast_add m i :=
ext rfl
/-- The cast of the successor is the succesor of the cast. See `fin.succ_cast_eq` for rewriting in
the reverse direction. -/
@[simp] lemma cast_succ_eq {n' : ℕ} (i : fin n) (h : n.succ = n'.succ) :
cast h i.succ = (cast (nat.succ.inj h) i).succ :=
ext $ by simp
lemma succ_cast_eq {n' : ℕ} (i : fin n) (h : n = n') : (cast h i).succ = cast (by rw h) i.succ :=
ext $ by simp
/-- `cast_succ i` embeds `i : fin n` in `fin (n+1)`. -/
def cast_succ : fin n ↪o fin (n + 1) := cast_add 1
@[simp] lemma coe_cast_succ (i : fin n) : (i.cast_succ : ℕ) = i := rfl
@[simp] lemma cast_succ_mk (n i : ℕ) (h : i < n) : cast_succ ⟨i, h⟩ = ⟨i, nat.lt.step h⟩ := rfl
@[simp] lemma cast_cast_succ {n' : ℕ} {h : n + 1 = n' + 1} {i : fin n} :
cast h (cast_succ i) = cast_succ (cast (nat.succ_injective h) i) :=
by { ext, simp only [coe_cast, coe_cast_succ] }
lemma cast_succ_lt_succ (i : fin n) : i.cast_succ < i.succ :=
lt_iff_coe_lt_coe.2 $ by simp only [coe_cast_succ, coe_succ, nat.lt_succ_self]
lemma le_cast_succ_iff {i : fin (n + 1)} {j : fin n} : i ≤ j.cast_succ ↔ i < j.succ :=
by simpa [lt_iff_coe_lt_coe, le_iff_coe_le_coe] using nat.succ_le_succ_iff.symm
lemma cast_succ_lt_iff_succ_le {n : ℕ} {i : fin n} {j : fin (n+1)} :
i.cast_succ < j ↔ i.succ ≤ j :=
by simpa only [fin.lt_iff_coe_lt_coe, fin.le_iff_coe_le_coe, fin.coe_succ, fin.coe_cast_succ]
using nat.lt_iff_add_one_le
@[simp] lemma succ_last (n : ℕ) : (last n).succ = last (n.succ) := rfl
@[simp] lemma succ_eq_last_succ {n : ℕ} (i : fin n.succ) :
i.succ = last (n + 1) ↔ i = last n :=
by rw [← succ_last, (succ_injective _).eq_iff]
@[simp] lemma cast_succ_cast_lt (i : fin (n + 1)) (h : (i : ℕ) < n) : cast_succ (cast_lt i h) = i :=
fin.eq_of_veq rfl
@[simp] lemma cast_lt_cast_succ {n : ℕ} (a : fin n) (h : (a : ℕ) < n) :
cast_lt (cast_succ a) h = a :=
by cases a; refl
@[simp] lemma cast_succ_lt_cast_succ_iff : a.cast_succ < b.cast_succ ↔ a < b :=
(@cast_succ n).lt_iff_lt
lemma cast_succ_injective (n : ℕ) : injective (@fin.cast_succ n) :=
(cast_succ : fin n ↪o _).injective
lemma cast_succ_inj {a b : fin n} : a.cast_succ = b.cast_succ ↔ a = b :=
(cast_succ_injective n).eq_iff
lemma cast_succ_lt_last (a : fin n) : cast_succ a < last n := lt_iff_coe_lt_coe.mpr a.is_lt
@[simp] lemma cast_succ_zero : cast_succ (0 : fin (n + 1)) = 0 := rfl
@[simp] lemma cast_succ_one {n : ℕ} : fin.cast_succ (1 : fin (n + 2)) = 1 := rfl
/-- `cast_succ i` is positive when `i` is positive -/
lemma cast_succ_pos {i : fin (n + 1)} (h : 0 < i) : 0 < cast_succ i :=
by simpa [lt_iff_coe_lt_coe] using h
@[simp] lemma cast_succ_eq_zero_iff (a : fin (n + 1)) : a.cast_succ = 0 ↔ a = 0 :=
fin.ext_iff.trans $ (fin.ext_iff.trans $ by exact iff.rfl).symm
lemma cast_succ_ne_zero_iff (a : fin (n + 1)) : a.cast_succ ≠ 0 ↔ a ≠ 0 :=
not_iff_not.mpr $ cast_succ_eq_zero_iff a
lemma cast_succ_fin_succ (n : ℕ) (j : fin n) :
cast_succ (fin.succ j) = fin.succ (cast_succ j) :=
by simp [fin.ext_iff]
@[norm_cast, simp] lemma coe_eq_cast_succ : (a : fin (n + 1)) = a.cast_succ :=
begin
ext,
exact coe_val_of_lt (nat.lt.step a.is_lt),
end
@[simp] lemma coe_succ_eq_succ : a.cast_succ + 1 = a.succ :=
begin
cases n,
{ exact fin_zero_elim a },
{ simp [a.is_lt, eq_iff_veq, add_def, nat.mod_eq_of_lt] }
end
lemma lt_succ : a.cast_succ < a.succ :=
by { rw [cast_succ, lt_iff_coe_lt_coe, coe_cast_add, coe_succ], exact lt_add_one a.val }
@[simp] lemma range_cast_succ {n : ℕ} :
set.range (cast_succ : fin n → fin n.succ) = {i | (i : ℕ) < n} :=
range_cast_le _
@[simp] lemma coe_of_injective_cast_succ_symm {n : ℕ} (i : fin n.succ) (hi) :
((equiv.of_injective cast_succ (cast_succ_injective _)).symm ⟨i, hi⟩ : ℕ) = i :=
begin
rw ← coe_cast_succ,
exact congr_arg coe (equiv.apply_of_injective_symm _ _)
end
lemma succ_cast_succ {n : ℕ} (i : fin n) :
i.cast_succ.succ = i.succ.cast_succ :=
fin.ext (by simp)
/-- `add_nat m i` adds `m` to `i`, generalizes `fin.succ`. -/
def add_nat (m) : fin n ↪o fin (n + m) :=
order_embedding.of_strict_mono (λ i, ⟨(i : ℕ) + m, add_lt_add_right i.2 _⟩) $
λ i j h, lt_iff_coe_lt_coe.2 $ add_lt_add_right h _
@[simp] lemma coe_add_nat (m : ℕ) (i : fin n) : (add_nat m i : ℕ) = i + m := rfl
@[simp] lemma add_nat_one {i : fin n} : add_nat 1 i = i.succ :=
by { ext, rw [coe_add_nat, coe_succ] }
lemma le_coe_add_nat (m : ℕ) (i : fin n) : m ≤ add_nat m i := nat.le_add_left _ _
@[simp] lemma add_nat_mk (n i : ℕ) (hi : i < m) :
add_nat n ⟨i, hi⟩ = ⟨i + n, add_lt_add_right hi n⟩ := rfl
@[simp] lemma cast_add_nat_zero {n n' : ℕ} (i : fin n) (h : n + 0 = n') :
cast h (add_nat 0 i) = cast ((add_zero _).symm.trans h) i :=
ext $ add_zero _
/-- For rewriting in the reverse direction, see `fin.cast_add_nat_left`. -/
lemma add_nat_cast {n n' m : ℕ} (i : fin n') (h : n' = n) :
add_nat m (cast h i) = cast (congr_arg _ h) (add_nat m i) :=
ext rfl
lemma cast_add_nat_left {n n' m : ℕ} (i : fin n') (h : n' + m = n + m) :
cast h (add_nat m i) = add_nat m (cast (add_right_cancel h) i) :=
ext rfl
@[simp] lemma cast_add_nat_right {n m m' : ℕ} (i : fin n) (h : n + m' = n + m) :
cast h (add_nat m' i) = add_nat m i :=
ext $ (congr_arg ((+) (i : ℕ)) (add_left_cancel h) : _)
/-- `nat_add n i` adds `n` to `i` "on the left". -/
def nat_add (n) {m} : fin m ↪o fin (n + m) :=
order_embedding.of_strict_mono (λ i, ⟨n + (i : ℕ), add_lt_add_left i.2 _⟩) $
λ i j h, lt_iff_coe_lt_coe.2 $ add_lt_add_left h _
@[simp] lemma coe_nat_add (n : ℕ) {m : ℕ} (i : fin m) : (nat_add n i : ℕ) = n + i := rfl
@[simp] lemma nat_add_mk (n i : ℕ) (hi : i < m) :
nat_add n ⟨i, hi⟩ = ⟨n + i, add_lt_add_left hi n⟩ := rfl
lemma le_coe_nat_add (m : ℕ) (i : fin n) : m ≤ nat_add m i := nat.le_add_right _ _
lemma nat_add_zero {n : ℕ} : fin.nat_add 0 = (fin.cast (zero_add n).symm).to_rel_embedding :=
by { ext, apply zero_add }
/-- For rewriting in the reverse direction, see `fin.cast_nat_add_right`. -/
lemma nat_add_cast {n n' : ℕ} (m : ℕ) (i : fin n') (h : n' = n) :
nat_add m (cast h i) = cast (congr_arg _ h) (nat_add m i) :=
ext rfl
lemma cast_nat_add_right {n n' m : ℕ} (i : fin n') (h : m + n' = m + n) :
cast h (nat_add m i) = nat_add m (cast (add_left_cancel h) i) :=
ext rfl
@[simp] lemma cast_nat_add_left {n m m' : ℕ} (i : fin n) (h : m' + n = m + n) :
cast h (nat_add m' i) = nat_add m i :=
ext $ (congr_arg (+ (i : ℕ)) (add_right_cancel h) : _)
@[simp] lemma cast_nat_add_zero {n n' : ℕ} (i : fin n) (h : 0 + n = n') :
cast h (nat_add 0 i) = cast ((zero_add _).symm.trans h) i :=
ext $ zero_add _
@[simp] lemma cast_nat_add (n : ℕ) {m : ℕ} (i : fin m) :
cast (add_comm _ _) (nat_add n i) = add_nat n i :=
ext $ add_comm _ _
@[simp] lemma cast_add_nat {n : ℕ} (m : ℕ) (i : fin n) :
cast (add_comm _ _) (add_nat m i) = nat_add m i :=
ext $ add_comm _ _
@[simp] lemma nat_add_last {m n : ℕ} : nat_add n (last m) = last (n + m) := rfl
lemma nat_add_cast_succ {m n : ℕ} {i : fin m} :
nat_add n (cast_succ i) = cast_succ (nat_add n i) := rfl
end succ
section pred
/-!
### pred
-/
@[simp] lemma coe_pred (j : fin (n+1)) (h : j ≠ 0) : (j.pred h : ℕ) = j - 1 :=
by { cases j, refl }
@[simp] lemma succ_pred : ∀(i : fin (n+1)) (h : i ≠ 0), (i.pred h).succ = i
| ⟨0, h⟩ hi := by contradiction
| ⟨n + 1, h⟩ hi := rfl
@[simp] lemma pred_succ (i : fin n) {h : i.succ ≠ 0} : i.succ.pred h = i :=
by { cases i, refl }
lemma pred_eq_iff_eq_succ {n : ℕ} (i : fin (n+1)) (hi : i ≠ 0) (j : fin n) :
i.pred hi = j ↔ i = j.succ :=
⟨λ h, by simp only [← h, fin.succ_pred], λ h, by simp only [h, fin.pred_succ]⟩
@[simp] lemma pred_mk_succ (i : ℕ) (h : i < n + 1) :
fin.pred ⟨i + 1, add_lt_add_right h 1⟩ (ne_of_vne (ne_of_gt (mk_succ_pos i h))) = ⟨i, h⟩ :=
by simp only [ext_iff, coe_pred, coe_mk, add_tsub_cancel_right]
-- This is not a simp lemma by default, because `pred_mk_succ` is nicer when it applies.
lemma pred_mk {n : ℕ} (i : ℕ) (h : i < n + 1) (w) :
fin.pred ⟨i, h⟩ w =
⟨i - 1, by rwa tsub_lt_iff_right (nat.succ_le_of_lt $ nat.pos_of_ne_zero (fin.vne_of_ne w))⟩ :=
rfl
@[simp] lemma pred_le_pred_iff {n : ℕ} {a b : fin n.succ} {ha : a ≠ 0} {hb : b ≠ 0} :
a.pred ha ≤ b.pred hb ↔ a ≤ b :=
by rw [←succ_le_succ_iff, succ_pred, succ_pred]
@[simp] lemma pred_lt_pred_iff {n : ℕ} {a b : fin n.succ} {ha : a ≠ 0} {hb : b ≠ 0} :
a.pred ha < b.pred hb ↔ a < b :=
by rw [←succ_lt_succ_iff, succ_pred, succ_pred]
@[simp] lemma pred_inj :
∀ {a b : fin (n + 1)} {ha : a ≠ 0} {hb : b ≠ 0}, a.pred ha = b.pred hb ↔ a = b
| ⟨0, _⟩ b ha hb := by contradiction
| ⟨i+1, _⟩ ⟨0, _⟩ ha hb := by contradiction
| ⟨i+1, hi⟩ ⟨j+1, hj⟩ ha hb := by simp [fin.eq_iff_veq]
@[simp] lemma pred_one {n : ℕ} : fin.pred (1 : fin (n + 2)) (ne.symm (ne_of_lt one_pos)) = 0 := rfl
lemma pred_add_one (i : fin (n + 2)) (h : (i : ℕ) < n + 1) :
pred (i + 1) (ne_of_gt (add_one_pos _ (lt_iff_coe_lt_coe.mpr h))) = cast_lt i h :=
begin
rw [ext_iff, coe_pred, coe_cast_lt, coe_add, coe_one, mod_eq_of_lt, add_tsub_cancel_right],
exact add_lt_add_right h 1,
end
/-- `sub_nat i h` subtracts `m` from `i`, generalizes `fin.pred`. -/
def sub_nat (m) (i : fin (n + m)) (h : m ≤ (i : ℕ)) : fin n :=
⟨(i : ℕ) - m, by { rw [tsub_lt_iff_right h], exact i.is_lt }⟩
@[simp] lemma coe_sub_nat (i : fin (n + m)) (h : m ≤ i) : (i.sub_nat m h : ℕ) = i - m :=
rfl
@[simp] lemma sub_nat_mk {i : ℕ} (h₁ : i < n + m) (h₂ : m ≤ i) :
sub_nat m ⟨i, h₁⟩ h₂ = ⟨i - m, (tsub_lt_iff_right h₂).2 h₁⟩ :=
rfl
@[simp] lemma pred_cast_succ_succ (i : fin n) :
pred (cast_succ i.succ) (ne_of_gt (cast_succ_pos i.succ_pos)) = i.cast_succ :=
by simp [eq_iff_veq]
@[simp] lemma add_nat_sub_nat {i : fin (n + m)} (h : m ≤ i) :
add_nat m (sub_nat m i h) = i :=
ext $ tsub_add_cancel_of_le h
@[simp] lemma sub_nat_add_nat (i : fin n) (m : ℕ) (h : m ≤ add_nat m i := le_coe_add_nat m i) :
sub_nat m (add_nat m i) h = i :=
ext $ add_tsub_cancel_right i m
@[simp] lemma nat_add_sub_nat_cast {i : fin (n + m)} (h : n ≤ i) :
nat_add n (sub_nat n (cast (add_comm _ _) i) h) = i :=
by simp [← cast_add_nat]
end pred
section div_mod
/-- Compute `i / n`, where `n` is a `nat` and inferred the type of `i`. -/
def div_nat (i : fin (m * n)) : fin m :=
⟨i / n, nat.div_lt_of_lt_mul $ mul_comm m n ▸ i.prop⟩
@[simp] lemma coe_div_nat (i : fin (m * n)) : (i.div_nat : ℕ) = i / n := rfl
/-- Compute `i % n`, where `n` is a `nat` and inferred the type of `i`. -/
def mod_nat (i : fin (m * n)) : fin n :=
⟨i % n, nat.mod_lt _ $ pos_of_mul_pos_right i.pos m.zero_le⟩
@[simp] lemma coe_mod_nat (i : fin (m * n)) : (i.mod_nat : ℕ) = i % n := rfl
end div_mod
section rec
/-!
### recursion and induction principles
-/
/-- Define `C n i` by induction on `i : fin n` interpreted as `(0 : fin (n - i)).succ.succ…`.
This function has two arguments: `H0 n` defines `0`-th element `C (n+1) 0` of an `(n+1)`-tuple,
and `Hs n i` defines `(i+1)`-st element of `(n+1)`-tuple based on `n`, `i`, and `i`-th element
of `n`-tuple. -/
@[elab_as_eliminator] def succ_rec
{C : Π n, fin n → Sort*}
(H0 : Π n, C (succ n) 0)
(Hs : Π n i, C n i → C (succ n) i.succ) : Π {n : ℕ} (i : fin n), C n i
| 0 i := i.elim0
| (succ n) ⟨0, _⟩ := H0 _
| (succ n) ⟨succ i, h⟩ := Hs _ _ (succ_rec ⟨i, lt_of_succ_lt_succ h⟩)
/-- Define `C n i` by induction on `i : fin n` interpreted as `(0 : fin (n - i)).succ.succ…`.
This function has two arguments: `H0 n` defines `0`-th element `C (n+1) 0` of an `(n+1)`-tuple,
and `Hs n i` defines `(i+1)`-st element of `(n+1)`-tuple based on `n`, `i`, and `i`-th element
of `n`-tuple.
A version of `fin.succ_rec` taking `i : fin n` as the first argument. -/
@[elab_as_eliminator] def succ_rec_on {n : ℕ} (i : fin n)
{C : Π n, fin n → Sort*}
(H0 : Π n, C (succ n) 0)
(Hs : Π n i, C n i → C (succ n) i.succ) : C n i :=
i.succ_rec H0 Hs
@[simp] theorem succ_rec_on_zero {C : ∀ n, fin n → Sort*} {H0 Hs} (n) :
@fin.succ_rec_on (succ n) 0 C H0 Hs = H0 n :=
rfl
@[simp] theorem succ_rec_on_succ {C : ∀ n, fin n → Sort*} {H0 Hs} {n} (i : fin n) :
@fin.succ_rec_on (succ n) i.succ C H0 Hs = Hs n i (fin.succ_rec_on i H0 Hs) :=
by cases i; refl
/--
Define `C i` by induction on `i : fin (n + 1)` via induction on the underlying `nat` value.
This function has two arguments: `h0` handles the base case on `C 0`,
and `hs` defines the inductive step using `C i.cast_succ`.
-/
@[elab_as_eliminator] def induction
{C : fin (n + 1) → Sort*}
(h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) :
Π (i : fin (n + 1)), C i :=
begin
rintro ⟨i, hi⟩,
induction i with i IH,
{ rwa [fin.mk_zero] },
{ refine hs ⟨i, lt_of_succ_lt_succ hi⟩ _,
exact IH (lt_of_succ_lt hi) }
end
@[simp] lemma induction_zero {C : fin (n + 1) → Sort*} (h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) :
(induction h0 hs : _) 0 = h0 := rfl
@[simp] lemma induction_succ {C : fin (n + 1) → Sort*} (h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) (i : fin n) :
(induction h0 hs : _) i.succ = hs i (induction h0 hs i.cast_succ) := by cases i; refl
/--
Define `C i` by induction on `i : fin (n + 1)` via induction on the underlying `nat` value.
This function has two arguments: `h0` handles the base case on `C 0`,
and `hs` defines the inductive step using `C i.cast_succ`.
A version of `fin.induction` taking `i : fin (n + 1)` as the first argument.
-/
@[elab_as_eliminator] def induction_on (i : fin (n + 1))
{C : fin (n + 1) → Sort*}
(h0 : C 0)
(hs : ∀ i : fin n, C i.cast_succ → C i.succ) : C i :=
induction h0 hs i
/-- Define `f : Π i : fin n.succ, C i` by separately handling the cases `i = 0` and
`i = j.succ`, `j : fin n`. -/
@[elab_as_eliminator] def cases
{C : fin (succ n) → Sort*} (H0 : C 0) (Hs : Π i : fin n, C (i.succ)) :
Π (i : fin (succ n)), C i :=
induction H0 (λ i _, Hs i)
@[simp] theorem cases_zero {n} {C : fin (succ n) → Sort*} {H0 Hs} : @fin.cases n C H0 Hs 0 = H0 :=
rfl
@[simp] theorem cases_succ {n} {C : fin (succ n) → Sort*} {H0 Hs} (i : fin n) :
@fin.cases n C H0 Hs i.succ = Hs i :=
by cases i; refl
@[simp] theorem cases_succ' {n} {C : fin (succ n) → Sort*} {H0 Hs} {i : ℕ} (h : i + 1 < n + 1) :
@fin.cases n C H0 Hs ⟨i.succ, h⟩ = Hs ⟨i, lt_of_succ_lt_succ h⟩ :=
by cases i; refl
lemma forall_fin_succ {P : fin (n+1) → Prop} :
(∀ i, P i) ↔ P 0 ∧ (∀ i:fin n, P i.succ) :=
⟨λ H, ⟨H 0, λ i, H _⟩, λ ⟨H0, H1⟩ i, fin.cases H0 H1 i⟩
lemma exists_fin_succ {P : fin (n+1) → Prop} :
(∃ i, P i) ↔ P 0 ∨ (∃i:fin n, P i.succ) :=
⟨λ ⟨i, h⟩, fin.cases or.inl (λ i hi, or.inr ⟨i, hi⟩) i h,
λ h, h.elim (λ h, ⟨0, h⟩) $ λ⟨i, hi⟩, ⟨i.succ, hi⟩⟩
lemma forall_fin_one {p : fin 1 → Prop} : (∀ i, p i) ↔ p 0 := @unique.forall_iff (fin 1) _ p
lemma exists_fin_one {p : fin 1 → Prop} : (∃ i, p i) ↔ p 0 := @unique.exists_iff (fin 1) _ p
lemma forall_fin_two {p : fin 2 → Prop} : (∀ i, p i) ↔ p 0 ∧ p 1 :=
forall_fin_succ.trans $ and_congr_right $ λ _, forall_fin_one
lemma exists_fin_two {p : fin 2 → Prop} : (∃ i, p i) ↔ p 0 ∨ p 1 :=
exists_fin_succ.trans $ or_congr_right' exists_fin_one
lemma fin_two_eq_of_eq_zero_iff {a b : fin 2} (h : a = 0 ↔ b = 0) : a = b :=
by { revert a b, simp [forall_fin_two] }
/--
Define `C i` by reverse induction on `i : fin (n + 1)` via induction on the underlying `nat` value.
This function has two arguments: `hlast` handles the base case on `C (fin.last n)`,
and `hs` defines the inductive step using `C i.succ`, inducting downwards.
-/
@[elab_as_eliminator]
def reverse_induction {n : ℕ}
{C : fin (n + 1) → Sort*}
(hlast : C (fin.last n))
(hs : ∀ i : fin n, C i.succ → C i.cast_succ) :
Π (i : fin (n + 1)), C i
| i :=
if hi : i = fin.last n
then _root_.cast (by rw hi) hlast
else
let j : fin n := ⟨i, lt_of_le_of_ne (nat.le_of_lt_succ i.2) (λ h, hi (fin.ext h))⟩ in
have wf : n + 1 - j.succ < n + 1 - i, begin
cases i,
rw [tsub_lt_tsub_iff_left_of_le];
simp [*, nat.succ_le_iff],
end,
have hi : i = fin.cast_succ j, from fin.ext rfl,
_root_.cast (by rw hi) (hs _ (reverse_induction j.succ))
using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ i : fin (n+1), n + 1 - i)⟩],
dec_tac := `[assumption] }
@[simp] lemma reverse_induction_last {n : ℕ}
{C : fin (n + 1) → Sort*}
(h0 : C (fin.last n))
(hs : ∀ i : fin n, C i.succ → C i.cast_succ) :
(reverse_induction h0 hs (fin.last n) : C (fin.last n)) = h0 :=
by rw [reverse_induction]; simp
@[simp] lemma reverse_induction_cast_succ {n : ℕ}
{C : fin (n + 1) → Sort*}
(h0 : C (fin.last n))
(hs : ∀ i : fin n, C i.succ → C i.cast_succ) (i : fin n):
(reverse_induction h0 hs i.cast_succ : C i.cast_succ) =
hs i (reverse_induction h0 hs i.succ) :=
begin
rw [reverse_induction, dif_neg (ne_of_lt (fin.cast_succ_lt_last i))],
cases i,
refl
end
/-- Define `f : Π i : fin n.succ, C i` by separately handling the cases `i = fin.last n` and
`i = j.cast_succ`, `j : fin n`. -/
@[elab_as_eliminator, elab_strategy]
def last_cases {n : ℕ} {C : fin (n + 1) → Sort*}
(hlast : C (fin.last n)) (hcast : (Π (i : fin n), C i.cast_succ)) (i : fin (n + 1)) : C i :=
reverse_induction hlast (λ i _, hcast i) i
@[simp] lemma last_cases_last {n : ℕ} {C : fin (n + 1) → Sort*}
(hlast : C (fin.last n)) (hcast : (Π (i : fin n), C i.cast_succ)) :
(fin.last_cases hlast hcast (fin.last n): C (fin.last n)) = hlast :=
reverse_induction_last _ _
@[simp] lemma last_cases_cast_succ {n : ℕ} {C : fin (n + 1) → Sort*}
(hlast : C (fin.last n)) (hcast : (Π (i : fin n), C i.cast_succ)) (i : fin n) :
(fin.last_cases hlast hcast (fin.cast_succ i): C (fin.cast_succ i)) = hcast i :=
reverse_induction_cast_succ _ _ _
/-- Define `f : Π i : fin (m + n), C i` by separately handling the cases `i = cast_add n i`,
`j : fin m` and `i = nat_add m j`, `j : fin n`. -/
@[elab_as_eliminator, elab_strategy]
def add_cases {m n : ℕ} {C : fin (m + n) → Sort u}
(hleft : Π i, C (cast_add n i))
(hright : Π i, C (nat_add m i)) (i : fin (m + n)) : C i :=
if hi : (i : ℕ) < m then eq.rec_on (cast_add_cast_lt n i hi) (hleft (cast_lt i hi))
else eq.rec_on (nat_add_sub_nat_cast (le_of_not_lt hi)) (hright _)
@[simp] lemma add_cases_left {m n : ℕ} {C : fin (m + n) → Sort*}
(hleft : Π i, C (cast_add n i)) (hright : Π i, C (nat_add m i)) (i : fin m) :
add_cases hleft hright (fin.cast_add n i) = hleft i :=
begin
cases i with i hi,
rw [add_cases, dif_pos (cast_add_lt _ _)],
refl
end
@[simp] lemma add_cases_right {m n : ℕ} {C : fin (m + n) → Sort*}
(hleft : Π i, C (cast_add n i)) (hright : Π i, C (nat_add m i)) (i : fin n) :
add_cases hleft hright (nat_add m i) = hright i :=
begin
have : ¬ (nat_add m i : ℕ) < m, from (le_coe_nat_add _ _).not_lt,
rw [add_cases, dif_neg this],
refine eq_of_heq ((eq_rec_heq _ _).trans _), congr' 1,
simp
end
end rec
lemma lift_fun_iff_succ {α : Type*} (r : α → α → Prop) [is_trans α r] {f : fin (n + 1) → α} :
((<) ⇒ r) f f ↔ ∀ i : fin n, r (f i.cast_succ) (f i.succ) :=
begin
split,
{ intros H i,
exact H i.cast_succ_lt_succ },
{ refine λ H i, fin.induction _ _,
{ exact λ h, (h.not_le (zero_le i)).elim },
{ intros j ihj hij,
rw [← le_cast_succ_iff] at hij,
rcases hij.eq_or_lt with rfl|hlt,
exacts [H j, trans (ihj hlt) (H j)] } }
end
/-- A function `f` on `fin (n + 1)` is strictly monotone if and only if `f i < f (i + 1)`
for all `i`. -/
lemma strict_mono_iff_lt_succ {α : Type*} [preorder α] {f : fin (n + 1) → α} :
strict_mono f ↔ ∀ i : fin n, f i.cast_succ < f i.succ :=
lift_fun_iff_succ (<)
/-- A function `f` on `fin (n + 1)` is monotone if and only if `f i ≤ f (i + 1)` for all `i`. -/
lemma monotone_iff_le_succ {α : Type*} [preorder α] {f : fin (n + 1) → α} :
monotone f ↔ ∀ i : fin n, f i.cast_succ ≤ f i.succ :=
monotone_iff_forall_lt.trans $ lift_fun_iff_succ (≤)
/-- A function `f` on `fin (n + 1)` is strictly antitone if and only if `f (i + 1) < f i`
for all `i`. -/
lemma strict_anti_iff_succ_lt {α : Type*} [preorder α] {f : fin (n + 1) → α} :
strict_anti f ↔ ∀ i : fin n, f i.succ < f i.cast_succ :=
lift_fun_iff_succ (>)
/-- A function `f` on `fin (n + 1)` is antitone if and only if `f (i + 1) ≤ f i` for all `i`. -/
lemma antitone_iff_succ_le {α : Type*} [preorder α] {f : fin (n + 1) → α} :
antitone f ↔ ∀ i : fin n, f i.succ ≤ f i.cast_succ :=
antitone_iff_forall_lt.trans $ lift_fun_iff_succ (≥)
section add_group
open nat int
/-- Negation on `fin n` -/
instance (n : ℕ) : has_neg (fin n) := ⟨λ a, ⟨(n - a) % n, nat.mod_lt _ a.pos⟩⟩
/-- Abelian group structure on `fin (n+1)`. -/
instance (n : ℕ) : add_comm_group (fin (n+1)) :=
{ add_left_neg := λ ⟨a, ha⟩, fin.ext $ trans (nat.mod_add_mod _ _ _) $
by { rw [fin.coe_mk, fin.coe_zero, tsub_add_cancel_of_le, nat.mod_self], exact le_of_lt ha },
sub_eq_add_neg := λ ⟨a, ha⟩ ⟨b, hb⟩, fin.ext $
show (a + (n + 1 - b)) % (n + 1) = (a + (n + 1 - b) % (n + 1)) % (n + 1), by simp,
sub := fin.sub,
..fin.add_comm_monoid n,
..fin.has_neg n.succ }
protected lemma coe_neg (a : fin n) : ((-a : fin n) : ℕ) = (n - a) % n := rfl
protected lemma coe_sub (a b : fin n) : ((a - b : fin n) : ℕ) = (a + (n - b)) % n :=
by cases a; cases b; refl
@[simp] lemma coe_fin_one (a : fin 1) : ↑a = 0 :=
by rw [subsingleton.elim a 0, fin.coe_zero]
@[simp] lemma coe_neg_one : ↑(-1 : fin (n + 1)) = n :=
begin
cases n,
{ simp },
rw [fin.coe_neg, fin.coe_one, nat.succ_sub_one, nat.mod_eq_of_lt],
constructor
end
lemma coe_sub_one {n} (a : fin (n + 1)) : ↑(a - 1) = if a = 0 then n else a - 1 :=
begin
cases n,
{ simp },
split_ifs,
{ simp [h] },
rw [sub_eq_add_neg, coe_add_eq_ite, coe_neg_one, if_pos, add_comm, add_tsub_add_eq_tsub_left],
rw [add_comm ↑a, add_le_add_iff_left, nat.one_le_iff_ne_zero],
rwa fin.ext_iff at h
end
lemma coe_sub_iff_le {n : ℕ} {a b : fin n} :
(↑(a - b) : ℕ) = a - b ↔ b ≤ a :=
begin
cases n, {exact fin_zero_elim a},
rw [le_iff_coe_le_coe, fin.coe_sub, ←add_tsub_assoc_of_le b.is_lt.le a],
cases le_or_lt (b : ℕ) a with h h,
{ simp [←tsub_add_eq_add_tsub h, h, nat.mod_eq_of_lt ((nat.sub_le _ _).trans_lt a.is_lt)] },
{ rw [nat.mod_eq_of_lt, tsub_eq_zero_of_le h.le, tsub_eq_zero_iff_le, ←not_iff_not],
{ simpa [b.is_lt.trans_le (le_add_self)] using h },
{ rwa [tsub_lt_iff_left (b.is_lt.le.trans (le_add_self)), add_lt_add_iff_right] } }
end
lemma coe_sub_iff_lt {n : ℕ} {a b : fin n} :
(↑(a - b) : ℕ) = n + a - b ↔ a < b :=
begin
cases n, {exact fin_zero_elim a},
rw [lt_iff_coe_lt_coe, fin.coe_sub, add_comm],
cases le_or_lt (b : ℕ) a with h h,
{ simpa [add_tsub_assoc_of_le h, ←not_le, h]
using ((nat.mod_lt _ (nat.succ_pos _)).trans_le le_self_add).ne },
{ simp [←tsub_tsub_assoc b.is_lt.le h.le, ←tsub_add_eq_add_tsub b.is_lt.le,
nat.mod_eq_of_lt (tsub_lt_self (nat.succ_pos _) (tsub_pos_of_lt h)), h] }
end
@[simp] lemma lt_sub_one_iff {n : ℕ} {k : fin (n + 2)} :
k < k - 1 ↔ k = 0 :=
begin
rcases k with ⟨(_|k), hk⟩,
simp [lt_iff_coe_lt_coe],
have : (k + 1 + (n + 1)) % (n + 2) = k % (n + 2),
{ rw [add_right_comm, add_assoc, add_mod_right] },
simp [lt_iff_coe_lt_coe, ext_iff, fin.coe_sub, succ_eq_add_one, this,
mod_eq_of_lt ((lt_succ_self _).trans hk)]
end
@[simp] lemma le_sub_one_iff {n : ℕ} {k : fin (n + 1)} :
k ≤ k - 1 ↔ k = 0 :=
begin
cases n,
{ simp [subsingleton.elim (k - 1) k, subsingleton.elim 0 k] },
rw [←lt_sub_one_iff, le_iff_lt_or_eq, lt_sub_one_iff, or_iff_left_iff_imp, eq_comm,
sub_eq_iff_eq_add],
simp
end
@[simp] lemma sub_one_lt_iff {n : ℕ} {k : fin (n + 1)} :
k - 1 < k ↔ 0 < k :=
not_iff_not.1 $ by simp only [not_lt, le_sub_one_iff, le_zero_iff]
lemma last_sub (i : fin (n + 1)) : last n - i = i.rev :=
ext $ by rw [coe_sub_iff_le.2 i.le_last, coe_last, coe_rev, nat.succ_sub_succ_eq_sub]
end add_group
section succ_above
lemma succ_above_aux (p : fin (n + 1)) :
strict_mono (λ i : fin n, if i.cast_succ < p then i.cast_succ else i.succ) :=
(cast_succ : fin n ↪o _).strict_mono.ite (succ_embedding n).strict_mono
(λ i j hij hj, lt_trans ((cast_succ : fin n ↪o _).lt_iff_lt.2 hij) hj)
(λ i, (cast_succ_lt_succ i).le)
/-- `succ_above p i` embeds `fin n` into `fin (n + 1)` with a hole around `p`. -/
def succ_above (p : fin (n + 1)) : fin n ↪o fin (n + 1) :=
order_embedding.of_strict_mono _ p.succ_above_aux
/-- Embedding `i : fin n` into `fin (n + 1)` with a hole around `p : fin (n + 1)`
embeds `i` by `cast_succ` when the resulting `i.cast_succ < p`. -/
lemma succ_above_below (p : fin (n + 1)) (i : fin n) (h : i.cast_succ < p) :
p.succ_above i = i.cast_succ :=
by { rw [succ_above], exact if_pos h }
@[simp] lemma succ_above_ne_zero_zero {a : fin (n + 2)} (ha : a ≠ 0) : a.succ_above 0 = 0 :=
begin
rw fin.succ_above_below,
{ refl },
{ exact bot_lt_iff_ne_bot.mpr ha }
end
lemma succ_above_eq_zero_iff {a : fin (n + 2)} {b : fin (n + 1)} (ha : a ≠ 0) :
a.succ_above b = 0 ↔ b = 0 :=
by simp only [←succ_above_ne_zero_zero ha, order_embedding.eq_iff_eq]
lemma succ_above_ne_zero {a : fin (n + 2)} {b : fin (n + 1)} (ha : a ≠ 0) (hb : b ≠ 0) :
a.succ_above b ≠ 0 :=
mt (succ_above_eq_zero_iff ha).mp hb
/-- Embedding `fin n` into `fin (n + 1)` with a hole around zero embeds by `succ`. -/
@[simp] lemma succ_above_zero : ⇑(succ_above (0 : fin (n + 1))) = fin.succ := rfl
/-- Embedding `fin n` into `fin (n + 1)` with a hole around `last n` embeds by `cast_succ`. -/
@[simp] lemma succ_above_last : succ_above (fin.last n) = cast_succ :=
by { ext, simp only [succ_above_below, cast_succ_lt_last] }
lemma succ_above_last_apply (i : fin n) : succ_above (fin.last n) i = i.cast_succ :=
by rw succ_above_last
/-- Embedding `i : fin n` into `fin (n + 1)` with a hole around `p : fin (n + 1)`
embeds `i` by `succ` when the resulting `p < i.succ`. -/
lemma succ_above_above (p : fin (n + 1)) (i : fin n) (h : p ≤ i.cast_succ) :
p.succ_above i = i.succ :=
by simp [succ_above, h.not_lt]
/-- Embedding `i : fin n` into `fin (n + 1)` is always about some hole `p`. -/
lemma succ_above_lt_ge (p : fin (n + 1)) (i : fin n) : i.cast_succ < p ∨ p ≤ i.cast_succ :=
lt_or_ge (cast_succ i) p
/-- Embedding `i : fin n` into `fin (n + 1)` is always about some hole `p`. -/
lemma succ_above_lt_gt (p : fin (n + 1)) (i : fin n) : i.cast_succ < p ∨ p < i.succ :=
or.cases_on (succ_above_lt_ge p i)
(λ h, or.inl h) (λ h, or.inr (lt_of_le_of_lt h (cast_succ_lt_succ i)))
/-- Embedding `i : fin n` into `fin (n + 1)` using a pivot `p` that is greater
results in a value that is less than `p`. -/
@[simp] lemma succ_above_lt_iff (p : fin (n + 1)) (i : fin n) :
p.succ_above i < p ↔ i.cast_succ < p :=
begin
refine iff.intro _ _,
{ intro h,
cases succ_above_lt_ge p i with H H,
{ exact H },
{ rw succ_above_above _ _ H at h,
exact lt_trans (cast_succ_lt_succ i) h } },
{ intro h,
rw succ_above_below _ _ h,
exact h }
end
/-- Embedding `i : fin n` into `fin (n + 1)` using a pivot `p` that is lesser
results in a value that is greater than `p`. -/
lemma lt_succ_above_iff (p : fin (n + 1)) (i : fin n) : p < p.succ_above i ↔ p ≤ i.cast_succ :=
begin
refine iff.intro _ _,
{ intro h,
cases succ_above_lt_ge p i with H H,
{ rw succ_above_below _ _ H at h,
exact le_of_lt h },
{ exact H } },
{ intro h,
rw succ_above_above _ _ h,
exact lt_of_le_of_lt h (cast_succ_lt_succ i) },
end
/-- Embedding `i : fin n` into `fin (n + 1)` with a hole around `p : fin (n + 1)`
never results in `p` itself -/
theorem succ_above_ne (p : fin (n + 1)) (i : fin n) : p.succ_above i ≠ p :=
begin
intro eq,
by_cases H : i.cast_succ < p,
{ simpa [lt_irrefl, ←succ_above_below _ _ H, eq] using H },
{ simpa [←succ_above_above _ _ (le_of_not_lt H), eq] using cast_succ_lt_succ i }
end
/-- Embedding a positive `fin n` results in a positive fin (n + 1)` -/
lemma succ_above_pos (p : fin (n + 2)) (i : fin (n + 1)) (h : 0 < i) : 0 < p.succ_above i :=
begin
by_cases H : i.cast_succ < p,
{ simpa [succ_above_below _ _ H] using cast_succ_pos h },
{ simp [succ_above_above _ _ (le_of_not_lt H)] },
end
@[simp] lemma succ_above_cast_lt {x y : fin (n + 1)} (h : x < y)
(hx : x.1 < n := lt_of_lt_of_le h y.le_last) :
y.succ_above (x.cast_lt hx) = x :=
by { rw [succ_above_below, cast_succ_cast_lt], exact h }
@[simp] lemma succ_above_pred {x y : fin (n + 1)} (h : x < y)
(hy : y ≠ 0 := (x.zero_le.trans_lt h).ne') :
x.succ_above (y.pred hy) = y :=
by { rw [succ_above_above, succ_pred], simpa [le_iff_coe_le_coe] using nat.le_pred_of_lt h }
lemma cast_lt_succ_above {x : fin n} {y : fin (n + 1)} (h : cast_succ x < y)
(h' : (y.succ_above x).1 < n := lt_of_lt_of_le ((succ_above_lt_iff _ _).2 h) (le_last y)) :
(y.succ_above x).cast_lt h' = x :=
by simp only [succ_above_below _ _ h, cast_lt_cast_succ]
lemma pred_succ_above {x : fin n} {y : fin (n + 1)} (h : y ≤ cast_succ x)
(h' : y.succ_above x ≠ 0 := (y.zero_le.trans_lt $ (lt_succ_above_iff _ _).2 h).ne') :
(y.succ_above x).pred h' = x :=
by simp only [succ_above_above _ _ h, pred_succ]
lemma exists_succ_above_eq {x y : fin (n + 1)} (h : x ≠ y) : ∃ z, y.succ_above z = x :=
begin
cases h.lt_or_lt with hlt hlt,
exacts [⟨_, succ_above_cast_lt hlt⟩, ⟨_, succ_above_pred hlt⟩],
end
@[simp] lemma exists_succ_above_eq_iff {x y : fin (n + 1)} : (∃ z, x.succ_above z = y) ↔ y ≠ x :=
begin
refine ⟨_, exists_succ_above_eq⟩,
rintro ⟨y, rfl⟩,
exact succ_above_ne _ _
end
/-- The range of `p.succ_above` is everything except `p`. -/
@[simp] lemma range_succ_above (p : fin (n + 1)) : set.range (p.succ_above) = {p}ᶜ :=
set.ext $ λ _, exists_succ_above_eq_iff
@[simp] lemma range_succ (n : ℕ) : set.range (fin.succ : fin n → fin (n + 1)) = {0}ᶜ :=
range_succ_above 0
@[simp] lemma exists_succ_eq_iff {x : fin (n + 1)} : (∃ y, fin.succ y = x) ↔ x ≠ 0 :=
@exists_succ_above_eq_iff n 0 x
/-- Given a fixed pivot `x : fin (n + 1)`, `x.succ_above` is injective -/
lemma succ_above_right_injective {x : fin (n + 1)} : injective (succ_above x) :=
(succ_above x).injective
/-- Given a fixed pivot `x : fin (n + 1)`, `x.succ_above` is injective -/
lemma succ_above_right_inj {x : fin (n + 1)} :
x.succ_above a = x.succ_above b ↔ a = b :=
succ_above_right_injective.eq_iff
/-- `succ_above` is injective at the pivot -/
lemma succ_above_left_injective : injective (@succ_above n) :=
λ _ _ h, by simpa [range_succ_above] using congr_arg (λ f : fin n ↪o fin (n + 1), (set.range f)ᶜ) h
/-- `succ_above` is injective at the pivot -/
@[simp] lemma succ_above_left_inj {x y : fin (n + 1)} :
x.succ_above = y.succ_above ↔ x = y :=
succ_above_left_injective.eq_iff
@[simp] lemma zero_succ_above {n : ℕ} (i : fin n) :
(0 : fin (n + 1)).succ_above i = i.succ :=
rfl
@[simp] lemma succ_succ_above_zero {n : ℕ} (i : fin (n + 1)) :
(i.succ).succ_above 0 = 0 :=
succ_above_below _ _ (succ_pos _)
@[simp] lemma succ_succ_above_succ {n : ℕ} (i : fin (n + 1)) (j : fin n) :
(i.succ).succ_above j.succ = (i.succ_above j).succ :=
(lt_or_ge j.cast_succ i).elim
(λ h, have h' : j.succ.cast_succ < i.succ, by simpa [lt_iff_coe_lt_coe] using h,
by { ext, simp [succ_above_below _ _ h, succ_above_below _ _ h'] })
(λ h, have h' : i.succ ≤ j.succ.cast_succ, by simpa [le_iff_coe_le_coe] using h,
by { ext, simp [succ_above_above _ _ h, succ_above_above _ _ h'] })
@[simp] lemma one_succ_above_zero {n : ℕ} :
(1 : fin (n + 2)).succ_above 0 = 0 :=
succ_succ_above_zero 0
/-- By moving `succ` to the outside of this expression, we create opportunities for further
simplification using `succ_above_zero` or `succ_succ_above_zero`. -/
@[simp] lemma succ_succ_above_one {n : ℕ} (i : fin (n + 2)) :
(i.succ).succ_above 1 = (i.succ_above 0).succ :=
succ_succ_above_succ i 0
@[simp] lemma one_succ_above_succ {n : ℕ} (j : fin n) :
(1 : fin (n + 2)).succ_above j.succ = j.succ.succ :=
succ_succ_above_succ 0 j
@[simp] lemma one_succ_above_one {n : ℕ} :
(1 : fin (n + 3)).succ_above 1 = 2 :=
succ_succ_above_succ 0 0
end succ_above
section pred_above
/-- `pred_above p i` embeds `i : fin (n+1)` into `fin n` by subtracting one if `p < i`. -/
def pred_above (p : fin n) (i : fin (n+1)) : fin n :=
if h : p.cast_succ < i then
i.pred (ne_of_lt (lt_of_le_of_lt (zero_le p.cast_succ) h)).symm
else
i.cast_lt (lt_of_le_of_lt (le_of_not_lt h) p.2)
lemma pred_above_right_monotone (p : fin n) : monotone p.pred_above :=
λ a b H,
begin
dsimp [pred_above],
split_ifs with ha hb hb,
all_goals { simp only [le_iff_coe_le_coe, coe_pred], },
{ exact pred_le_pred H, },
{ calc _ ≤ _ : nat.pred_le _
... ≤ _ : H, },
{ simp at ha, exact le_pred_of_lt (lt_of_le_of_lt ha hb), },
{ exact H, },
end
lemma pred_above_left_monotone (i : fin (n + 1)) : monotone (λ p, pred_above p i) :=
λ a b H,
begin
dsimp [pred_above],
split_ifs with ha hb hb,
all_goals { simp only [le_iff_coe_le_coe, coe_pred] },
{ exact pred_le _, },
{ have : b < a := cast_succ_lt_cast_succ_iff.mpr (hb.trans_le (le_of_not_gt ha)),
exact absurd H this.not_le }
end
/-- `cast_pred` embeds `i : fin (n + 2)` into `fin (n + 1)`
by lowering just `last (n + 1)` to `last n`. -/
def cast_pred (i : fin (n + 2)) : fin (n + 1) :=
pred_above (last n) i
@[simp] lemma cast_pred_zero : cast_pred (0 : fin (n + 2)) = 0 := rfl
@[simp] lemma cast_pred_one : cast_pred (1 : fin (n + 2)) = 1 :=
by { cases n, apply subsingleton.elim, refl }
@[simp] theorem pred_above_zero {i : fin (n + 2)} (hi : i ≠ 0) :
pred_above 0 i = i.pred hi :=
begin
dsimp [pred_above],
rw dif_pos,
exact (pos_iff_ne_zero _).mpr hi,
end
@[simp] lemma cast_pred_last : cast_pred (last (n + 1)) = last n :=
eq_of_veq (by simp [cast_pred, pred_above, cast_succ_lt_last])
@[simp] lemma cast_pred_mk (n i : ℕ) (h : i < n + 1) :
cast_pred ⟨i, lt_succ_of_lt h⟩ = ⟨i, h⟩ :=
begin
have : ¬cast_succ (last n) < ⟨i, lt_succ_of_lt h⟩,
{ simpa [lt_iff_coe_lt_coe] using le_of_lt_succ h },
simp [cast_pred, pred_above, this]
end
lemma coe_cast_pred {n : ℕ} (a : fin (n + 2)) (hx : a < fin.last _) :
(a.cast_pred : ℕ) = a :=
begin
rcases a with ⟨a, ha⟩,
rw cast_pred_mk,
exacts [rfl, hx],
end
lemma pred_above_below (p : fin (n + 1)) (i : fin (n + 2)) (h : i ≤ p.cast_succ) :
p.pred_above i = i.cast_pred :=
begin
have : i ≤ (last n).cast_succ := h.trans p.le_last,
simp [pred_above, cast_pred, h.not_lt, this.not_lt]
end
@[simp] lemma pred_above_last : pred_above (fin.last n) = cast_pred := rfl
lemma pred_above_last_apply (i : fin n) : pred_above (fin.last n) i = i.cast_pred :=
by rw pred_above_last
lemma pred_above_above (p : fin n) (i : fin (n + 1)) (h : p.cast_succ < i) :
p.pred_above i = i.pred (p.cast_succ.zero_le.trans_lt h).ne.symm :=
by simp [pred_above, h]
lemma cast_pred_monotone : monotone (@cast_pred n) :=
pred_above_right_monotone (last _)
/-- Sending `fin (n+1)` to `fin n` by subtracting one from anything above `p`
then back to `fin (n+1)` with a gap around `p` is the identity away from `p`. -/
@[simp] lemma succ_above_pred_above {p : fin n} {i : fin (n + 1)} (h : i ≠ p.cast_succ) :
p.cast_succ.succ_above (p.pred_above i) = i :=
begin
dsimp [pred_above, succ_above],
rcases p with ⟨p, _⟩,
rcases i with ⟨i, _⟩,
cases lt_or_le i p with H H,
{ rw dif_neg, rw if_pos, refl, exact H, simp, apply le_of_lt H, },
{ rw dif_pos, rw if_neg,
swap 3, -- For some reason `simp` doesn't fire fully unless we discharge the third goal.
{ exact lt_of_le_of_ne H (ne.symm h), },
{ simp, },
{ simp only [fin.mk_eq_mk, ne.def, fin.cast_succ_mk] at h,
simp only [pred, fin.mk_lt_mk, not_lt],
exact nat.le_pred_of_lt (nat.lt_of_le_and_ne H (ne.symm h)), }, },
end
/-- Sending `fin n` into `fin (n + 1)` with a gap at `p`
then back to `fin n` by subtracting one from anything above `p` is the identity. -/
@[simp] lemma pred_above_succ_above (p : fin n) (i : fin n) :
p.pred_above (p.cast_succ.succ_above i) = i :=
begin
dsimp [pred_above, succ_above],
rcases p with ⟨p, _⟩,
rcases i with ⟨i, _⟩,
split_ifs,
{ rw dif_neg,
{ refl },
{ simp_rw [if_pos h],
simp only [subtype.mk_lt_mk, not_lt],
exact le_of_lt h, }, },
{ rw dif_pos,
{ refl, },
{ simp_rw [if_neg h],
exact lt_succ_iff.mpr (not_lt.mp h), }, },
end
lemma cast_succ_pred_eq_pred_cast_succ {a : fin (n + 1)} (ha : a ≠ 0)
(ha' := a.cast_succ_ne_zero_iff.mpr ha) : (a.pred ha).cast_succ = a.cast_succ.pred ha' :=
by { cases a, refl }
/-- `pred` commutes with `succ_above`. -/
lemma pred_succ_above_pred {a : fin (n + 2)} {b : fin (n + 1)} (ha : a ≠ 0) (hb : b ≠ 0)
(hk := succ_above_ne_zero ha hb) :
(a.pred ha).succ_above (b.pred hb) = (a.succ_above b).pred hk :=
begin
obtain hbelow | habove := lt_or_le b.cast_succ a, -- `rwa` uses them
{ rw fin.succ_above_below,
{ rwa [cast_succ_pred_eq_pred_cast_succ , fin.pred_inj, fin.succ_above_below] },
{ rwa [cast_succ_pred_eq_pred_cast_succ , pred_lt_pred_iff] } },
{ rw fin.succ_above_above,
have : (b.pred hb).succ = b.succ.pred (fin.succ_ne_zero _), by rw [succ_pred, pred_succ],
{ rwa [this, fin.pred_inj, fin.succ_above_above] },
{ rwa [cast_succ_pred_eq_pred_cast_succ , fin.pred_le_pred_iff] } }
end
/-- `succ` commutes with `pred_above`. -/
@[simp]
lemma succ_pred_above_succ (a : fin n) (b : fin (n+1)) :
a.succ.pred_above b.succ = (a.pred_above b).succ :=
begin
obtain h₁ | h₂ := lt_or_le a.cast_succ b,
{ rw [fin.pred_above_above _ _ h₁, fin.succ_pred,
fin.pred_above_above, fin.pred_succ],
simpa only [fin.lt_iff_coe_lt_coe, fin.coe_cast_succ,
fin.coe_succ, add_lt_add_iff_right] using h₁, },
{ cases n,
{ exfalso,
exact not_lt_zero' a.is_lt, },
{ rw [fin.pred_above_below a b h₂, fin.pred_above_below a.succ b.succ
(by simpa only [le_iff_coe_le_coe, coe_succ, coe_cast_succ,
add_le_add_iff_right] using h₂)],
ext,
have h₀ : (b : ℕ) < n+1,
{ simp only [le_iff_coe_le_coe, coe_cast_succ] at h₂,
simpa only [lt_succ_iff] using h₂.trans a.is_le, },
have h₁ : (b.succ : ℕ) < n+2,
{ rw ← nat.succ_lt_succ_iff at h₀,
simpa only [coe_succ] using h₀, },
simp only [coe_cast_pred b h₀, coe_cast_pred b.succ h₁, coe_succ], }, },
end
@[simp] theorem cast_pred_cast_succ (i : fin (n + 1)) :
cast_pred i.cast_succ = i :=
by simp [cast_pred, pred_above, le_last]
lemma cast_succ_cast_pred {i : fin (n + 2)} (h : i < last _) : cast_succ i.cast_pred = i :=
begin
rw [cast_pred, pred_above, dif_neg],
{ simp [fin.eq_iff_veq] },
{ exact h.not_le }
end
lemma coe_cast_pred_le_self (i : fin (n + 2)) : (i.cast_pred : ℕ) ≤ i :=
begin
rcases i.le_last.eq_or_lt with rfl|h,
{ simp },
{ rw [cast_pred, pred_above, dif_neg],
{ simp },
{ simpa [lt_iff_coe_lt_coe, le_iff_coe_le_coe, lt_succ_iff] using h } }
end
lemma coe_cast_pred_lt_iff {i : fin (n + 2)} : (i.cast_pred : ℕ) < i ↔ i = fin.last _ :=
begin
rcases i.le_last.eq_or_lt with rfl|H,
{ simp },
{ simp only [ne_of_lt H],
rw ←cast_succ_cast_pred H,
simp }
end
lemma lt_last_iff_coe_cast_pred {i : fin (n + 2)} : i < fin.last _ ↔ (i.cast_pred : ℕ) = i :=
begin
rcases i.le_last.eq_or_lt with rfl|H,
{ simp },
{ simp only [H],
rw ←cast_succ_cast_pred H,
simp }
end
end pred_above
/-- `min n m` as an element of `fin (m + 1)` -/
def clamp (n m : ℕ) : fin (m + 1) := of_nat $ min n m
@[simp] lemma coe_clamp (n m : ℕ) : (clamp n m : ℕ) = min n m :=
nat.mod_eq_of_lt $ nat.lt_succ_iff.mpr $ min_le_right _ _
@[simp]
lemma coe_of_nat_eq_mod (m n : ℕ) :
((n : fin (succ m)) : ℕ) = n % succ m :=
by rw [← of_nat_eq_coe]; refl
@[simp] lemma coe_of_nat_eq_mod' (m n : ℕ) [I : ne_zero m] :
(@fin.of_nat' _ I n : ℕ) = n % m :=
rfl
section mul
/-!
### mul
-/
lemma val_mul {n : ℕ} : ∀ a b : fin n, (a * b).val = (a.val * b.val) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
lemma coe_mul {n : ℕ} : ∀ a b : fin n, ((a * b : fin n) : ℕ) = (a * b) % n
| ⟨_, _⟩ ⟨_, _⟩ := rfl
@[simp] protected lemma mul_one (k : fin (n + 1)) : k * 1 = k :=
by { cases n, simp, simp [eq_iff_veq, mul_def, mod_eq_of_lt (is_lt k)] }
@[simp] protected lemma one_mul (k : fin (n + 1)) : (1 : fin (n + 1)) * k = k :=
by { cases n, simp, simp [eq_iff_veq, mul_def, mod_eq_of_lt (is_lt k)] }
@[simp] protected lemma mul_zero (k : fin (n + 1)) : k * 0 = 0 :=
by simp [eq_iff_veq, mul_def]
@[simp] protected lemma zero_mul (k : fin (n + 1)) : (0 : fin (n + 1)) * k = 0 :=
by simp [eq_iff_veq, mul_def]
end mul
section
-- Note that here we are disabling the "safety" of reflected, to allow us to reuse `nat.mk_numeral`.
-- The usual way to provide the required `reflected` instance would be via rewriting to prove that
-- the expression we use here is equivalent.
local attribute [semireducible] reflected
meta instance reflect : Π n, has_reflect (fin n)
| 0 := fin_zero_elim
| (n + 1) := nat.mk_numeral `(fin n.succ)
`(by apply_instance : has_zero (fin n.succ))
`(by apply_instance : has_one (fin n.succ))
`(by apply_instance : has_add (fin n.succ)) ∘ fin.val
end
end fin
|
7ba7aeeb050b071dfeaced54c26bbea63884ffc6 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/group_theory/submonoid/center.lean | 2649bbec6bae563c9cae4c39151cc8ba2a477861 | [
"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 | 4,787 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser
-/
import group_theory.submonoid.operations
import data.fintype.basic
/-!
# Centers of magmas and monoids
## Main definitions
* `set.center`: the center of a magma
* `submonoid.center`: the center of a monoid
* `set.add_center`: the center of an additive magma
* `add_submonoid.center`: the center of an additive monoid
We provide `subgroup.center`, `add_subgroup.center`, `subsemiring.center`, and `subring.center` in
other files.
-/
variables {M : Type*}
namespace set
variables (M)
/-- The center of a magma. -/
@[to_additive add_center /-" The center of an additive magma. "-/]
def center [has_mul M] : set M := {z | ∀ m, m * z = z * m}
@[to_additive mem_add_center]
lemma mem_center_iff [has_mul M] {z : M} : z ∈ center M ↔ ∀ g, g * z = z * g := iff.rfl
instance decidable_mem_center [has_mul M] [decidable_eq M] [fintype M] :
decidable_pred (∈ center M) :=
λ _, decidable_of_iff' _ (mem_center_iff M)
@[simp, to_additive zero_mem_add_center]
lemma one_mem_center [mul_one_class M] : (1 : M) ∈ set.center M := by simp [mem_center_iff]
@[simp]
lemma zero_mem_center [mul_zero_class M] : (0 : M) ∈ set.center M := by simp [mem_center_iff]
variables {M}
@[simp, to_additive add_mem_add_center]
lemma mul_mem_center [semigroup M] {a b : M}
(ha : a ∈ set.center M) (hb : b ∈ set.center M) : a * b ∈ set.center M :=
λ g, by rw [mul_assoc, ←hb g, ← mul_assoc, ha g, mul_assoc]
@[simp, to_additive neg_mem_add_center]
lemma inv_mem_center [group M] {a : M} (ha : a ∈ set.center M) : a⁻¹ ∈ set.center M :=
λ g, by rw [← inv_inj, mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv]
@[simp]
lemma add_mem_center [distrib M] {a b : M}
(ha : a ∈ set.center M) (hb : b ∈ set.center M) : a + b ∈ set.center M :=
λ c, by rw [add_mul, mul_add, ha c, hb c]
@[simp]
lemma neg_mem_center [ring M] {a : M} (ha : a ∈ set.center M) : -a ∈ set.center M :=
λ c, by rw [←neg_mul_comm, ha (-c), neg_mul_comm]
@[to_additive subset_add_center_add_units]
lemma subset_center_units [monoid M] :
(coe : Mˣ → M) ⁻¹' center M ⊆ set.center Mˣ :=
λ a ha b, units.ext $ ha _
lemma center_units_subset [group_with_zero M] :
set.center Mˣ ⊆ (coe : Mˣ → M) ⁻¹' center M :=
λ a ha b, begin
obtain rfl | hb := eq_or_ne b 0,
{ rw [zero_mul, mul_zero], },
{ exact units.ext_iff.mp (ha (units.mk0 _ hb)) }
end
/-- In a group with zero, the center of the units is the preimage of the center. -/
lemma center_units_eq [group_with_zero M] :
set.center Mˣ = (coe : Mˣ → M) ⁻¹' center M :=
subset.antisymm center_units_subset subset_center_units
@[simp]
lemma inv_mem_center₀ [group_with_zero M] {a : M} (ha : a ∈ set.center M) : a⁻¹ ∈ set.center M :=
begin
obtain rfl | ha0 := eq_or_ne a 0,
{ rw inv_zero, exact zero_mem_center M },
rcases is_unit.mk0 _ ha0 with ⟨a, rfl⟩,
rw ←units.coe_inv',
exact center_units_subset (inv_mem_center (subset_center_units ha)),
end
@[simp, to_additive sub_mem_add_center]
lemma div_mem_center [group M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) :
a / b ∈ set.center M :=
begin
rw [div_eq_mul_inv],
exact mul_mem_center ha (inv_mem_center hb),
end
@[simp]
lemma div_mem_center₀ [group_with_zero M] {a b : M} (ha : a ∈ set.center M)
(hb : b ∈ set.center M) : a / b ∈ set.center M :=
begin
rw div_eq_mul_inv,
exact mul_mem_center ha (inv_mem_center₀ hb),
end
variables (M)
@[simp, to_additive add_center_eq_univ]
lemma center_eq_univ [comm_semigroup M] : center M = set.univ :=
subset.antisymm (subset_univ _) $ λ x _ y, mul_comm y x
end set
namespace submonoid
section
variables (M) [monoid M]
/-- The center of a monoid `M` is the set of elements that commute with everything in `M` -/
@[to_additive "The center of a monoid `M` is the set of elements that commute with everything in
`M`"]
def center : submonoid M :=
{ carrier := set.center M,
one_mem' := set.one_mem_center M,
mul_mem' := λ a b, set.mul_mem_center }
@[to_additive] lemma coe_center : ↑(center M) = set.center M := rfl
variables {M}
@[to_additive] lemma mem_center_iff {z : M} : z ∈ center M ↔ ∀ g, g * z = z * g := iff.rfl
instance decidable_mem_center [decidable_eq M] [fintype M] : decidable_pred (∈ center M) :=
λ _, decidable_of_iff' _ mem_center_iff
/-- The center of a monoid is commutative. -/
instance : comm_monoid (center M) :=
{ mul_comm := λ a b, subtype.ext $ b.prop _,
.. (center M).to_monoid }
end
section
variables (M) [comm_monoid M]
@[simp] lemma center_eq_top : center M = ⊤ :=
set_like.coe_injective (set.center_eq_univ M)
end
end submonoid
|
9114723476862b78c60bd4a3510a9a06a0f14b7d | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/geometry/manifold/metrizable.lean | 681a32a20b59ed8192ad51ce25125b17893dacef | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 1,316 | lean | /-
Copyright (c) 2022 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import geometry.manifold.smooth_manifold_with_corners
import topology.paracompact
import topology.metric_space.metrizable
/-!
# Metrizability of a σ-compact manifold
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we show that a σ-compact Hausdorff topological manifold over a finite dimensional real
vector space is metrizable.
-/
open topological_space
/-- A σ-compact Hausdorff topological manifold over a finite dimensional real vector space is
metrizable. -/
lemma manifold_with_corners.metrizable_space
{E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [finite_dimensional ℝ E]
{H : Type*} [topological_space H] (I : model_with_corners ℝ E H)
(M : Type*) [topological_space M] [charted_space H M]
[sigma_compact_space M] [t2_space M] : metrizable_space M :=
begin
haveI := I.locally_compact, haveI := charted_space.locally_compact H M,
haveI : normal_space M := normal_of_paracompact_t2,
haveI := I.second_countable_topology,
haveI := charted_space.second_countable_of_sigma_compact H M,
exact metrizable_space_of_t3_second_countable M
end
|
f2b76c1d1346e2ab264a8b3933c3d30cec687d0e | d72901cc240bd78b8b0384565e4f4dee8abd3a86 | /src/analysis/calculus/deriv.lean | 4f54a3a246f0343aba8c06cf857b730e1e1e3682 | [
"Apache-2.0"
] | permissive | leon-volq/mathlib | 513b24765349bb5187df9d898b92beadf96124d9 | 0cc93a137e9b2e243f8ae1f808fa7225ce0fe143 | refs/heads/master | 1,676,294,376,990 | 1,610,838,688,000 | 1,610,838,688,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 73,960 | 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
import data.polynomial.derivative
/-!
# 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`.
- `has_strict_deriv_at f f' x` states that the function `f` has the derivative `f'`
at the point `x` in the sense of strict differentiability, i.e.,
`f y - f z = (y - z) • f' + o (y - z)` as `y, z → 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
- sum of finitely many functions
- 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`
- inverse function (assuming that it exists; the inverse function theorem is in `inverse.lean`)
- division
- polynomials
For most binary operations we also define `const_op` and `op_const` theorems for the cases when
the first or second argument is a constant. This makes writing chains of `has_deriv_at`'s easier,
and they more frequently lead to the desired result.
We set up the simplifier so that it can compute the derivative of simple functions. For instance,
```lean
example (x : ℝ) : deriv (λ x, cos (sin x) * exp x) x = (cos(sin(x))-sin(sin(x))*cos(x))*exp(x) :=
by { simp, ring }
```
## Implementation notes
Most of the theorems are direct restatements of the corresponding theorems
for Fréchet derivatives.
The strategy to construct simp lemmas that give the simplifier the possibility to compute
derivatives is the same as the one for differentiability statements, as explained in `fderiv.lean`.
See the explanations there.
-/
universes u v w
noncomputable theory
open_locale classical topological_space big_operators filter
open filter asymptotics set
open continuous_linear_map (smul_right smul_right_one_eq_iff)
variables {𝕜 : Type u} [nondiscrete_normed_field 𝕜]
section
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 (𝓝[s] x)
/--
`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)
/-- `f` has the derivative `f'` at the point `x` in the sense of strict differentiability.
That is, `f y - f z = (y - z) • f' + o(y - z)` as `y, z → x`. -/
def has_strict_deriv_at (f : 𝕜 → F) (f' : F) (x : 𝕜) :=
has_strict_fderiv_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) 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]
lemma has_fderiv_at_filter.has_deriv_at_filter {f' : 𝕜 →L[𝕜] F} :
has_fderiv_at_filter f f' x L → has_deriv_at_filter f (f' 1) x L :=
has_fderiv_at_filter_iff_has_deriv_at_filter.mp
/-- 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 :=
has_fderiv_at_filter_iff_has_deriv_at_filter
/-- 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
lemma has_fderiv_within_at.has_deriv_within_at {f' : 𝕜 →L[𝕜] F} :
has_fderiv_within_at f f' s x → has_deriv_within_at f (f' 1) s x :=
has_fderiv_within_at_iff_has_deriv_within_at.mp
lemma has_deriv_within_at.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 :=
has_deriv_within_at_iff_has_fderiv_within_at.mp
/-- 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 :=
has_fderiv_at_filter_iff_has_deriv_at_filter
lemma has_fderiv_at.has_deriv_at {f' : 𝕜 →L[𝕜] F} :
has_fderiv_at f f' x → has_deriv_at f (f' 1) x :=
has_fderiv_at_iff_has_deriv_at.mp
lemma has_strict_fderiv_at_iff_has_strict_deriv_at {f' : 𝕜 →L[𝕜] F} :
has_strict_fderiv_at f f' x ↔ has_strict_deriv_at f (f' 1) x :=
by simp [has_strict_deriv_at, has_strict_fderiv_at]
protected lemma has_strict_fderiv_at.has_strict_deriv_at {f' : 𝕜 →L[𝕜] F} :
has_strict_fderiv_at f f' x → has_strict_deriv_at f (f' 1) x :=
has_strict_fderiv_at_iff_has_strict_deriv_at.mp
/-- 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'∥) (𝓝[s] x) (𝓝 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_strict_deriv_at.has_deriv_at (h : has_strict_deriv_at f f' x) :
has_deriv_at f f' x :=
h.has_fderiv_at
/-- If the domain has dimension one, then Fréchet derivative is equivalent to the classical
definition with a limit. In this version we have to take the limit along the subset `-{x}`,
because for `y=x` the slope equals zero due to the convention `0⁻¹=0`. -/
lemma has_deriv_at_filter_iff_tendsto_slope {x : 𝕜} {L : filter 𝕜} :
has_deriv_at_filter f f' x L ↔
tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (L ⊓ 𝓟 {x}ᶜ) (𝓝 f') :=
begin
conv_lhs { simp only [has_deriv_at_filter_iff_tendsto, (normed_field.norm_inv _).symm,
(norm_smul _ _).symm, tendsto_zero_iff_norm_tendsto_zero.symm] },
conv_rhs { rw [← nhds_translation f', tendsto_comap_iff] },
refine (tendsto_inf_principal_nhds_iff_of_forall_eq $ by simp).symm.trans (tendsto_congr' _),
refine (eventually_principal.2 $ λ z hz, _).filter_mono inf_le_right,
simp only [(∘)],
rw [smul_sub, ← mul_smul, inv_mul_cancel (sub_ne_zero.2 hz), one_smul]
end
lemma has_deriv_within_at_iff_tendsto_slope :
has_deriv_within_at f f' s x ↔
tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (𝓝[s \ {x}] x) (𝓝 f') :=
begin
simp only [has_deriv_within_at, nhds_within, diff_eq, inf_assoc.symm, inf_principal.symm],
exact has_deriv_at_filter_iff_tendsto_slope
end
lemma has_deriv_within_at_iff_tendsto_slope' (hs : x ∉ s) :
has_deriv_within_at f f' s x ↔
tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (𝓝[s] x) (𝓝 f') :=
begin
convert ← has_deriv_within_at_iff_tendsto_slope,
exact diff_singleton_eq_self hs
end
lemma has_deriv_at_iff_tendsto_slope :
has_deriv_at f f' x ↔
tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (𝓝[{x}ᶜ] x) (𝓝 f') :=
has_deriv_at_filter_iff_tendsto_slope
@[simp] lemma has_deriv_within_at_diff_singleton :
has_deriv_within_at f f' (s \ {x}) x ↔ has_deriv_within_at f f' s x :=
by simp only [has_deriv_within_at_iff_tendsto_slope, sdiff_idem_right]
@[simp] lemma has_deriv_within_at_Ioi_iff_Ici [partial_order 𝕜] :
has_deriv_within_at f f' (Ioi x) x ↔ has_deriv_within_at f f' (Ici x) x :=
by rw [← Ici_diff_left, has_deriv_within_at_diff_singleton]
alias has_deriv_within_at_Ioi_iff_Ici ↔
has_deriv_within_at.Ici_of_Ioi has_deriv_within_at.Ioi_of_Ici
@[simp] lemma has_deriv_within_at_Iio_iff_Iic [partial_order 𝕜] :
has_deriv_within_at f f' (Iio x) x ↔ has_deriv_within_at f f' (Iic x) x :=
by rw [← Iic_diff_right, has_deriv_within_at_diff_singleton]
alias has_deriv_within_at_Iio_iff_Iic ↔
has_deriv_within_at.Iic_of_Iio has_deriv_within_at.Iio_of_Iic
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 ∈ 𝓝[s] 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_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 hs.join ht,
end
lemma has_deriv_within_at.nhds_within (h : has_deriv_within_at f f' s x)
(ht : s ∈ 𝓝[t] x) : 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 }
lemma deriv_within_of_open (hs : is_open s) (hx : x ∈ s) :
deriv_within f s x = deriv f x :=
by { unfold deriv_within, rw fderiv_within_of_open hs hx, refl }
section congr
/-! ### Congruence properties of derivatives -/
theorem filter.eventually_eq.has_deriv_at_filter_iff
(h₀ : f₀ =ᶠ[L] f₁) (hx : f₀ x = f₁ x) (h₁ : f₀' = f₁') :
has_deriv_at_filter f₀ f₀' x L ↔ has_deriv_at_filter f₁ f₁' x L :=
h₀.has_fderiv_at_filter_iff hx (by simp [h₁])
lemma has_deriv_at_filter.congr_of_eventually_eq (h : has_deriv_at_filter f f' x L)
(hL : f₁ =ᶠ[L] f) (hx : f₁ x = f x) : has_deriv_at_filter f₁ f' x L :=
by rwa hL.has_deriv_at_filter_iff hx 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_eventually_eq (h : has_deriv_within_at f f' s x)
(h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : has_deriv_within_at f₁ f' s x :=
has_deriv_at_filter.congr_of_eventually_eq h h₁ hx
lemma has_deriv_within_at.congr_of_eventually_eq_of_mem (h : has_deriv_within_at f f' s x)
(h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : x ∈ s) : has_deriv_within_at f₁ f' s x :=
h.congr_of_eventually_eq h₁ (h₁.eq_of_nhds_within hx)
lemma has_deriv_at.congr_of_eventually_eq (h : has_deriv_at f f' x)
(h₁ : f₁ =ᶠ[𝓝 x] f) : has_deriv_at f₁ f' x :=
has_deriv_at_filter.congr_of_eventually_eq h h₁ (mem_of_nhds h₁ : _)
lemma filter.eventually_eq.deriv_within_eq (hs : unique_diff_within_at 𝕜 s x)
(hL : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) :
deriv_within f₁ s x = deriv_within f s x :=
by { unfold deriv_within, rw hL.fderiv_within_eq hs 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 filter.eventually_eq.deriv_eq (hL : f₁ =ᶠ[𝓝 x] f) : deriv f₁ x = deriv f x :=
by { unfold deriv, rwa filter.eventually_eq.fderiv_eq }
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 :=
(has_fderiv_at_filter_id x L).has_deriv_at_filter
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 _ _
theorem has_deriv_at_id' : has_deriv_at (λ (x : 𝕜), x) 1 x :=
has_deriv_at_filter_id _ _
theorem has_strict_deriv_at_id : has_strict_deriv_at id 1 x :=
(has_strict_fderiv_at_id x).has_strict_deriv_at
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
@[simp] lemma deriv_id'' : deriv (λ x : 𝕜, x) x = 1 :=
deriv_id x
lemma deriv_within_id (hxs : unique_diff_within_at 𝕜 s x) : deriv_within id s x = 1 :=
(has_deriv_within_at_id x s).deriv_within hxs
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 :=
(has_fderiv_at_filter_const c x L).has_deriv_at_filter
theorem has_strict_deriv_at_const : has_strict_deriv_at (λ x, c) 0 x :=
(has_strict_fderiv_at_const c x).has_strict_deriv_at
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 :=
(has_deriv_within_at_const _ _ _).deriv_within hxs
end const
section continuous_linear_map
/-! ### Derivative of continuous linear maps -/
variables (e : 𝕜 →L[𝕜] F)
lemma continuous_linear_map.has_deriv_at_filter : has_deriv_at_filter e (e 1) x L :=
e.has_fderiv_at_filter.has_deriv_at_filter
lemma continuous_linear_map.has_strict_deriv_at : has_strict_deriv_at e (e 1) x :=
e.has_strict_fderiv_at.has_strict_deriv_at
lemma continuous_linear_map.has_deriv_at : has_deriv_at e (e 1) x :=
e.has_deriv_at_filter
lemma continuous_linear_map.has_deriv_within_at : has_deriv_within_at e (e 1) s x :=
e.has_deriv_at_filter
@[simp] lemma continuous_linear_map.deriv : deriv e x = e 1 :=
e.has_deriv_at.deriv
lemma continuous_linear_map.deriv_within (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within e s x = e 1 :=
e.has_deriv_within_at.deriv_within hxs
end continuous_linear_map
section linear_map
/-! ### Derivative of bundled linear maps -/
variables (e : 𝕜 →ₗ[𝕜] F)
lemma linear_map.has_deriv_at_filter : has_deriv_at_filter e (e 1) x L :=
e.to_continuous_linear_map₁.has_deriv_at_filter
lemma linear_map.has_strict_deriv_at : has_strict_deriv_at e (e 1) x :=
e.to_continuous_linear_map₁.has_strict_deriv_at
lemma linear_map.has_deriv_at : has_deriv_at e (e 1) x :=
e.has_deriv_at_filter
lemma linear_map.has_deriv_within_at : has_deriv_within_at e (e 1) s x :=
e.has_deriv_at_filter
@[simp] lemma linear_map.deriv : deriv e x = e 1 :=
e.has_deriv_at.deriv
lemma linear_map.deriv_within (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within e s x = e 1 :=
e.has_deriv_within_at.deriv_within hxs
end 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 :=
by simpa using (hf.add hg).has_deriv_at_filter
theorem has_strict_deriv_at.add
(hf : has_strict_deriv_at f f' x) (hg : has_strict_deriv_at g g' x) :
has_strict_deriv_at (λ y, f y + g y) (f' + g') x :=
by simpa using (hf.add hg).has_strict_deriv_at
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
@[simp] 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
theorem has_deriv_at_filter.add_const
(hf : has_deriv_at_filter f f' x L) (c : F) :
has_deriv_at_filter (λ y, f y + c) f' x L :=
add_zero f' ▸ hf.add (has_deriv_at_filter_const x L c)
theorem has_deriv_within_at.add_const
(hf : has_deriv_within_at f f' s x) (c : F) :
has_deriv_within_at (λ y, f y + c) f' s x :=
hf.add_const c
theorem has_deriv_at.add_const
(hf : has_deriv_at f f' x) (c : F) :
has_deriv_at (λ x, f x + c) f' x :=
hf.add_const c
lemma deriv_within_add_const (hxs : unique_diff_within_at 𝕜 s x) (c : F) :
deriv_within (λy, f y + c) s x = deriv_within f s x :=
by simp only [deriv_within, fderiv_within_add_const hxs]
lemma deriv_add_const (c : F) : deriv (λy, f y + c) x = deriv f x :=
by simp only [deriv, fderiv_add_const]
theorem has_deriv_at_filter.const_add (c : F) (hf : has_deriv_at_filter f f' x L) :
has_deriv_at_filter (λ y, c + f y) f' x L :=
zero_add f' ▸ (has_deriv_at_filter_const x L c).add hf
theorem has_deriv_within_at.const_add (c : F) (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ y, c + f y) f' s x :=
hf.const_add c
theorem has_deriv_at.const_add (c : F) (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, c + f x) f' x :=
hf.const_add c
lemma deriv_within_const_add (hxs : unique_diff_within_at 𝕜 s x) (c : F) :
deriv_within (λy, c + f y) s x = deriv_within f s x :=
by simp only [deriv_within, fderiv_within_const_add hxs]
lemma deriv_const_add (c : F) : deriv (λy, c + f y) x = deriv f x :=
by simp only [deriv, fderiv_const_add]
end add
section sum
/-! ### Derivative of a finite sum of functions -/
open_locale big_operators
variables {ι : Type*} {u : finset ι} {A : ι → (𝕜 → F)} {A' : ι → F}
theorem has_deriv_at_filter.sum (h : ∀ i ∈ u, has_deriv_at_filter (A i) (A' i) x L) :
has_deriv_at_filter (λ y, ∑ i in u, A i y) (∑ i in u, A' i) x L :=
by simpa [continuous_linear_map.sum_apply] using (has_fderiv_at_filter.sum h).has_deriv_at_filter
theorem has_strict_deriv_at.sum (h : ∀ i ∈ u, has_strict_deriv_at (A i) (A' i) x) :
has_strict_deriv_at (λ y, ∑ i in u, A i y) (∑ i in u, A' i) x :=
by simpa [continuous_linear_map.sum_apply] using (has_strict_fderiv_at.sum h).has_strict_deriv_at
theorem has_deriv_within_at.sum (h : ∀ i ∈ u, has_deriv_within_at (A i) (A' i) s x) :
has_deriv_within_at (λ y, ∑ i in u, A i y) (∑ i in u, A' i) s x :=
has_deriv_at_filter.sum h
theorem has_deriv_at.sum (h : ∀ i ∈ u, has_deriv_at (A i) (A' i) x) :
has_deriv_at (λ y, ∑ i in u, A i y) (∑ i in u, A' i) x :=
has_deriv_at_filter.sum h
lemma deriv_within_sum (hxs : unique_diff_within_at 𝕜 s x)
(h : ∀ i ∈ u, differentiable_within_at 𝕜 (A i) s x) :
deriv_within (λ y, ∑ i in u, A i y) s x = ∑ i in u, deriv_within (A i) s x :=
(has_deriv_within_at.sum (λ i hi, (h i hi).has_deriv_within_at)).deriv_within hxs
@[simp] lemma deriv_sum (h : ∀ i ∈ u, differentiable_at 𝕜 (A i) x) :
deriv (λ y, ∑ i in u, A i y) x = ∑ i in u, deriv (A i) x :=
(has_deriv_at.sum (λ i hi, (h i hi).has_deriv_at)).deriv
end sum
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 :=
by simpa using (has_fderiv_within_at.smul hc hf).has_deriv_within_at
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
rw [← has_deriv_within_at_univ] at *,
exact hc.smul hf
end
theorem has_strict_deriv_at.smul
(hc : has_strict_deriv_at c c' x) (hf : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ y, c y • f y) (c x • f' + c' • f x) x :=
by simpa using (hc.smul hf).has_strict_deriv_at
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
theorem has_deriv_within_at.smul_const
(hc : has_deriv_within_at c c' s x) (f : F) :
has_deriv_within_at (λ y, c y • f) (c' • f) s x :=
begin
have := hc.smul (has_deriv_within_at_const x s f),
rwa [smul_zero, zero_add] at this
end
theorem has_deriv_at.smul_const
(hc : has_deriv_at c c' x) (f : F) :
has_deriv_at (λ y, c y • f) (c' • f) x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hc.smul_const f
end
lemma deriv_within_smul_const (hxs : unique_diff_within_at 𝕜 s x)
(hc : differentiable_within_at 𝕜 c s x) (f : F) :
deriv_within (λ y, c y • f) s x = (deriv_within c s x) • f :=
(hc.has_deriv_within_at.smul_const f).deriv_within hxs
lemma deriv_smul_const (hc : differentiable_at 𝕜 c x) (f : F) :
deriv (λ y, c y • f) x = (deriv c x) • f :=
(hc.has_deriv_at.smul_const f).deriv
theorem has_deriv_within_at.const_smul
(c : 𝕜) (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ y, c • f y) (c • f') s x :=
begin
convert (has_deriv_within_at_const x s c).smul hf,
rw [zero_smul, add_zero]
end
theorem has_deriv_at.const_smul (c : 𝕜) (hf : has_deriv_at f f' x) :
has_deriv_at (λ y, c • f y) (c • f') x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hf.const_smul c
end
lemma deriv_within_const_smul (hxs : unique_diff_within_at 𝕜 s x)
(c : 𝕜) (hf : differentiable_within_at 𝕜 f s x) :
deriv_within (λ y, c • f y) s x = c • deriv_within f s x :=
(hf.has_deriv_within_at.const_smul c).deriv_within hxs
lemma deriv_const_smul (c : 𝕜) (hf : differentiable_at 𝕜 f x) :
deriv (λ y, c • f y) x = c • deriv f x :=
(hf.has_deriv_at.const_smul c).deriv
end mul_vector
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 :=
by simpa using h.neg.has_deriv_at_filter
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
theorem has_strict_deriv_at.neg (h : has_strict_deriv_at f f' x) :
has_strict_deriv_at (λ x, -f x) (-f') x :=
by simpa using h.neg.has_strict_deriv_at
lemma deriv_within.neg (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λy, -f y) s x = - deriv_within f s x :=
by simp only [deriv_within, fderiv_within_neg hxs, continuous_linear_map.neg_apply]
lemma deriv.neg : deriv (λy, -f y) x = - deriv f x :=
by simp only [deriv, fderiv_neg, continuous_linear_map.neg_apply]
@[simp] lemma deriv.neg' : deriv (λy, -f y) = (λ x, - deriv f x) :=
funext $ λ x, deriv.neg
end neg
section neg2
/-! ### Derivative of the negation function (i.e `has_neg.neg`) -/
variables (s x L)
theorem has_deriv_at_filter_neg : has_deriv_at_filter has_neg.neg (-1) x L :=
has_deriv_at_filter.neg $ has_deriv_at_filter_id _ _
theorem has_deriv_within_at_neg : has_deriv_within_at has_neg.neg (-1) s x :=
has_deriv_at_filter_neg _ _
theorem has_deriv_at_neg : has_deriv_at has_neg.neg (-1) x :=
has_deriv_at_filter_neg _ _
theorem has_deriv_at_neg' : has_deriv_at (λ x, -x) (-1) x :=
has_deriv_at_filter_neg _ _
theorem has_strict_deriv_at_neg : has_strict_deriv_at has_neg.neg (-1) x :=
has_strict_deriv_at.neg $ has_strict_deriv_at_id _
lemma deriv_neg : deriv has_neg.neg x = -1 :=
has_deriv_at.deriv (has_deriv_at_neg x)
@[simp] lemma deriv_neg' : deriv (has_neg.neg : 𝕜 → 𝕜) = λ _, -1 :=
funext deriv_neg
@[simp] lemma deriv_neg'' : deriv (λ x : 𝕜, -x) x = -1 :=
deriv_neg x
lemma deriv_within_neg (hxs : unique_diff_within_at 𝕜 s x) : deriv_within has_neg.neg s x = -1 :=
(has_deriv_within_at_neg x s).deriv_within hxs
lemma differentiable_neg : differentiable 𝕜 (has_neg.neg : 𝕜 → 𝕜) :=
differentiable.neg differentiable_id
lemma differentiable_on_neg : differentiable_on 𝕜 (has_neg.neg : 𝕜 → 𝕜) s :=
differentiable_on.neg differentiable_on_id
end neg2
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 :=
by simpa only [sub_eq_add_neg] using 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
theorem has_strict_deriv_at.sub
(hf : has_strict_deriv_at f f' x) (hg : has_strict_deriv_at g g' x) :
has_strict_deriv_at (λ x, f x - g x) (f' - g') x :=
by simpa only [sub_eq_add_neg] using hf.add hg.neg
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
@[simp] 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
theorem has_deriv_at_filter.sub_const
(hf : has_deriv_at_filter f f' x L) (c : F) :
has_deriv_at_filter (λ x, f x - c) f' x L :=
by simpa only [sub_eq_add_neg] using hf.add_const (-c)
theorem has_deriv_within_at.sub_const
(hf : has_deriv_within_at f f' s x) (c : F) :
has_deriv_within_at (λ x, f x - c) f' s x :=
hf.sub_const c
theorem has_deriv_at.sub_const
(hf : has_deriv_at f f' x) (c : F) :
has_deriv_at (λ x, f x - c) f' x :=
hf.sub_const c
lemma deriv_within_sub_const (hxs : unique_diff_within_at 𝕜 s x) (c : F) :
deriv_within (λy, f y - c) s x = deriv_within f s x :=
by simp only [deriv_within, fderiv_within_sub_const hxs]
lemma deriv_sub_const (c : F) : deriv (λ y, f y - c) x = deriv f x :=
by simp only [deriv, fderiv_sub_const]
theorem has_deriv_at_filter.const_sub (c : F) (hf : has_deriv_at_filter f f' x L) :
has_deriv_at_filter (λ x, c - f x) (-f') x L :=
by simpa only [sub_eq_add_neg] using hf.neg.const_add c
theorem has_deriv_within_at.const_sub (c : F) (hf : has_deriv_within_at f f' s x) :
has_deriv_within_at (λ x, c - f x) (-f') s x :=
hf.const_sub c
theorem has_deriv_at.const_sub (c : F) (hf : has_deriv_at f f' x) :
has_deriv_at (λ x, c - f x) (-f') x :=
hf.const_sub c
lemma deriv_within_const_sub (hxs : unique_diff_within_at 𝕜 s x) (c : F) :
deriv_within (λy, c - f y) s x = -deriv_within f s x :=
by simp [deriv_within, fderiv_within_const_sub hxs]
lemma deriv_const_sub (c : F) : deriv (λ y, c - f y) x = -deriv f x :=
by simp only [← deriv_within_univ, deriv_within_const_sub unique_diff_within_at_univ]
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)) :=
h.tendsto_nhds hL
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 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 function and a scalar function
We use `scomp` in lemmas on composition of vector valued and scalar valued functions, and `comp`
in lemmas on composition of scalar valued functions, in analogy for `smul` and `mul` (and also
because the `comp` version with the shorter name will show up much more often in applications).
The formula for the derivative involves `smul` in `scomp` lemmas, which can be reduced to
usual multiplication in `comp` lemmas.
-/
variables {h h₁ h₂ : 𝕜 → 𝕜} {h' 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.scomp
(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 :=
by simpa using (hg.comp x hh).has_deriv_at_filter
theorem has_deriv_within_at.scomp {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 :=
has_deriv_at_filter.scomp _ (has_deriv_at_filter.mono hg $
hh.continuous_within_at.tendsto_nhds_within hst) hh
/-- The chain rule. -/
theorem has_deriv_at.scomp
(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).scomp x hh
theorem has_strict_deriv_at.scomp
(hg : has_strict_deriv_at g g' (h x)) (hh : has_strict_deriv_at h h' x) :
has_strict_deriv_at (g ∘ h) (h' • g') x :=
by simpa using (hg.comp x hh).has_strict_deriv_at
theorem has_deriv_at.scomp_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.scomp x hg hh subset_preimage_univ
end
lemma deriv_within.scomp
(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.scomp x (hg.has_deriv_within_at) (hh.has_deriv_within_at) hs
end
lemma deriv.scomp
(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.scomp x hg.has_deriv_at hh.has_deriv_at
end
/-! ### Derivative of the composition of a scalar and vector functions -/
theorem has_deriv_at_filter.comp_has_fderiv_at_filter {f : E → 𝕜} {f' : E →L[𝕜] 𝕜} (x)
{L : filter E} (hh₁ : has_deriv_at_filter h₁ h₁' (f x) (L.map f))
(hf : has_fderiv_at_filter f f' x L) :
has_fderiv_at_filter (h₁ ∘ f) (h₁' • f') x L :=
by { convert has_fderiv_at_filter.comp x hh₁ hf, ext x, simp [mul_comm] }
theorem has_deriv_at.comp_has_fderiv_at {f : E → 𝕜} {f' : E →L[𝕜] 𝕜} (x)
(hh₁ : has_deriv_at h₁ h₁' (f x)) (hf : has_fderiv_at f f' x) :
has_fderiv_at (h₁ ∘ f) (h₁' • f') x :=
(hh₁.mono hf.continuous_at).comp_has_fderiv_at_filter x hf
theorem has_deriv_at.comp_has_fderiv_within_at {f : E → 𝕜} {f' : E →L[𝕜] 𝕜} {s} (x)
(hh₁ : has_deriv_at h₁ h₁' (f x)) (hf : has_fderiv_within_at f f' s x) :
has_fderiv_within_at (h₁ ∘ f) (h₁' • f') s x :=
(hh₁.mono hf.continuous_within_at).comp_has_fderiv_at_filter x hf
theorem has_deriv_within_at.comp_has_fderiv_within_at {f : E → 𝕜} {f' : E →L[𝕜] 𝕜} {s t} (x)
(hh₁ : has_deriv_within_at h₁ h₁' t (f x)) (hf : has_fderiv_within_at f f' s x)
(hst : maps_to f s t) :
has_fderiv_within_at (h₁ ∘ f) (h₁' • f') s x :=
(has_deriv_at_filter.mono hh₁ $
hf.continuous_within_at.tendsto_nhds_within hst).comp_has_fderiv_at_filter x hf
/-! ### Derivative of the composition of two scalar functions -/
theorem has_deriv_at_filter.comp
(hh₁ : has_deriv_at_filter h₁ h₁' (h₂ x) (L.map h₂))
(hh₂ : has_deriv_at_filter h₂ h₂' x L) :
has_deriv_at_filter (h₁ ∘ h₂) (h₁' * h₂') x L :=
by { rw mul_comm, exact hh₁.scomp x hh₂ }
theorem has_deriv_within_at.comp {t : set 𝕜}
(hh₁ : has_deriv_within_at h₁ h₁' t (h₂ x))
(hh₂ : has_deriv_within_at h₂ h₂' s x) (hst : s ⊆ h₂ ⁻¹' t) :
has_deriv_within_at (h₁ ∘ h₂) (h₁' * h₂') s x :=
by { rw mul_comm, exact hh₁.scomp x hh₂ hst, }
/-- The chain rule. -/
theorem has_deriv_at.comp
(hh₁ : has_deriv_at h₁ h₁' (h₂ x)) (hh₂ : has_deriv_at h₂ h₂' x) :
has_deriv_at (h₁ ∘ h₂) (h₁' * h₂') x :=
(hh₁.mono hh₂.continuous_at).comp x hh₂
theorem has_strict_deriv_at.comp
(hh₁ : has_strict_deriv_at h₁ h₁' (h₂ x)) (hh₂ : has_strict_deriv_at h₂ h₂' x) :
has_strict_deriv_at (h₁ ∘ h₂) (h₁' * h₂') x :=
by { rw mul_comm, exact hh₁.scomp x hh₂ }
theorem has_deriv_at.comp_has_deriv_within_at
(hh₁ : has_deriv_at h₁ h₁' (h₂ x)) (hh₂ : has_deriv_within_at h₂ h₂' s x) :
has_deriv_within_at (h₁ ∘ h₂) (h₁' * h₂') s x :=
begin
rw ← has_deriv_within_at_univ at hh₁,
exact has_deriv_within_at.comp x hh₁ hh₂ subset_preimage_univ
end
lemma deriv_within.comp
(hh₁ : differentiable_within_at 𝕜 h₁ t (h₂ x)) (hh₂ : differentiable_within_at 𝕜 h₂ s x)
(hs : s ⊆ h₂ ⁻¹' t) (hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (h₁ ∘ h₂) s x = deriv_within h₁ t (h₂ x) * deriv_within h₂ s x :=
begin
apply has_deriv_within_at.deriv_within _ hxs,
exact has_deriv_within_at.comp x (hh₁.has_deriv_within_at) (hh₂.has_deriv_within_at) hs
end
lemma deriv.comp
(hh₁ : differentiable_at 𝕜 h₁ (h₂ x)) (hh₂ : differentiable_at 𝕜 h₂ x) :
deriv (h₁ ∘ h₂) x = deriv h₁ (h₂ x) * deriv h₂ x :=
begin
apply has_deriv_at.deriv,
exact has_deriv_at.comp x hh₁.has_deriv_at hh₂.has_deriv_at
end
protected lemma has_deriv_at_filter.iterate {f : 𝕜 → 𝕜} {f' : 𝕜}
(hf : has_deriv_at_filter f f' x L) (hL : tendsto f L L) (hx : f x = x) (n : ℕ) :
has_deriv_at_filter (f^[n]) (f'^n) x L :=
begin
have := hf.iterate hL hx n,
rwa [continuous_linear_map.smul_right_one_pow] at this
end
protected lemma has_deriv_at.iterate {f : 𝕜 → 𝕜} {f' : 𝕜}
(hf : has_deriv_at f f' x) (hx : f x = x) (n : ℕ) :
has_deriv_at (f^[n]) (f'^n) x :=
begin
have := has_fderiv_at.iterate hf hx n,
rwa [continuous_linear_map.smul_right_one_pow] at this
end
protected lemma has_deriv_within_at.iterate {f : 𝕜 → 𝕜} {f' : 𝕜}
(hf : has_deriv_within_at f f' s x) (hx : f x = x) (hs : maps_to f s s) (n : ℕ) :
has_deriv_within_at (f^[n]) (f'^n) s x :=
begin
have := has_fderiv_within_at.iterate hf hx hs n,
rwa [continuous_linear_map.smul_right_one_pow] at this
end
protected lemma has_strict_deriv_at.iterate {f : 𝕜 → 𝕜} {f' : 𝕜}
(hf : has_strict_deriv_at f f' x) (hx : f x = x) (n : ℕ) :
has_strict_deriv_at (f^[n]) (f'^n) x :=
begin
have := hf.iterate hx n,
rwa [continuous_linear_map.smul_right_one_pow] at this
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
/-! ### 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' * d x + c x * d') s x :=
begin
convert hc.smul hd using 1,
rw [smul_eq_mul, smul_eq_mul, add_comm]
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' * d x + c x * d') x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hc.mul hd
end
theorem has_strict_deriv_at.mul
(hc : has_strict_deriv_at c c' x) (hd : has_strict_deriv_at d d' x) :
has_strict_deriv_at (λ y, c y * d y) (c' * d x + c x * d') x :=
begin
convert hc.smul hd using 1,
rw [smul_eq_mul, smul_eq_mul, add_comm]
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 = deriv_within c s x * d x + c x * deriv_within d s x :=
(hc.has_deriv_within_at.mul hd.has_deriv_within_at).deriv_within hxs
@[simp] lemma deriv_mul (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) :
deriv (λ y, c y * d y) x = deriv c x * d x + c x * deriv d x :=
(hc.has_deriv_at.mul hd.has_deriv_at).deriv
theorem has_deriv_within_at.mul_const (hc : has_deriv_within_at c c' s x) (d : 𝕜) :
has_deriv_within_at (λ y, c y * d) (c' * d) s x :=
begin
convert hc.mul (has_deriv_within_at_const x s d),
rw [mul_zero, add_zero]
end
theorem has_deriv_at.mul_const (hc : has_deriv_at c c' x) (d : 𝕜) :
has_deriv_at (λ y, c y * d) (c' * d) x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hc.mul_const d
end
lemma deriv_within_mul_const (hxs : unique_diff_within_at 𝕜 s x)
(hc : differentiable_within_at 𝕜 c s x) (d : 𝕜) :
deriv_within (λ y, c y * d) s x = deriv_within c s x * d :=
(hc.has_deriv_within_at.mul_const d).deriv_within hxs
lemma deriv_mul_const (hc : differentiable_at 𝕜 c x) (d : 𝕜) :
deriv (λ y, c y * d) x = deriv c x * d :=
(hc.has_deriv_at.mul_const d).deriv
theorem has_deriv_within_at.const_mul (c : 𝕜) (hd : has_deriv_within_at d d' s x) :
has_deriv_within_at (λ y, c * d y) (c * d') s x :=
begin
convert (has_deriv_within_at_const x s c).mul hd,
rw [zero_mul, zero_add]
end
theorem has_deriv_at.const_mul (c : 𝕜) (hd : has_deriv_at d d' x) :
has_deriv_at (λ y, c * d y) (c * d') x :=
begin
rw [← has_deriv_within_at_univ] at *,
exact hd.const_mul c
end
lemma deriv_within_const_mul (hxs : unique_diff_within_at 𝕜 s x)
(c : 𝕜) (hd : differentiable_within_at 𝕜 d s x) :
deriv_within (λ y, c * d y) s x = c * deriv_within d s x :=
(hd.has_deriv_within_at.const_mul c).deriv_within hxs
lemma deriv_const_mul (c : 𝕜) (hd : differentiable_at 𝕜 d x) :
deriv (λ y, c * d y) x = c * deriv d x :=
(hd.has_deriv_at.const_mul c).deriv
end mul
section inverse
/-! ### Derivative of `x ↦ x⁻¹` -/
theorem has_strict_deriv_at_inv (hx : x ≠ 0) : has_strict_deriv_at has_inv.inv (-(x^2)⁻¹) x :=
begin
suffices : is_o (λ p : 𝕜 × 𝕜, (p.1 - p.2) * ((x * x)⁻¹ - (p.1 * p.2)⁻¹))
(λ (p : 𝕜 × 𝕜), (p.1 - p.2) * 1) (𝓝 (x, x)),
{ refine this.congr' _ (eventually_of_forall $ λ _, mul_one _),
refine eventually.mono (mem_nhds_sets (is_open_ne.prod is_open_ne) ⟨hx, hx⟩) _,
rintro ⟨y, z⟩ ⟨hy, hz⟩,
simp only [mem_set_of_eq] at hy hz, -- hy : y ≠ 0, hz : z ≠ 0
field_simp [hx, hy, hz], ring, },
refine (is_O_refl (λ p : 𝕜 × 𝕜, p.1 - p.2) _).mul_is_o ((is_o_one_iff _).2 _),
rw [← sub_self (x * x)⁻¹],
exact tendsto_const_nhds.sub ((continuous_mul.tendsto (x, x)).inv' $ mul_ne_zero hx hx)
end
theorem has_deriv_at_inv (x_ne_zero : x ≠ 0) :
has_deriv_at (λy, y⁻¹) (-(x^2)⁻¹) x :=
(has_strict_deriv_at_inv x_ne_zero).has_deriv_at
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 :=
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
variables {c : 𝕜 → 𝕜} {c' : 𝕜}
lemma has_deriv_within_at.inv
(hc : has_deriv_within_at c c' s x) (hx : c x ≠ 0) :
has_deriv_within_at (λ y, (c y)⁻¹) (- c' / (c x)^2) s x :=
begin
convert (has_deriv_at_inv hx).comp_has_deriv_within_at x hc,
field_simp
end
lemma has_deriv_at.inv (hc : has_deriv_at c c' x) (hx : c x ≠ 0) :
has_deriv_at (λ y, (c y)⁻¹) (- c' / (c x)^2) x :=
begin
rw ← has_deriv_within_at_univ at *,
exact hc.inv hx
end
lemma differentiable_within_at.inv (hc : differentiable_within_at 𝕜 c s x) (hx : c x ≠ 0) :
differentiable_within_at 𝕜 (λx, (c x)⁻¹) s x :=
(hc.has_deriv_within_at.inv hx).differentiable_within_at
@[simp] lemma differentiable_at.inv (hc : differentiable_at 𝕜 c x) (hx : c x ≠ 0) :
differentiable_at 𝕜 (λx, (c x)⁻¹) x :=
(hc.has_deriv_at.inv hx).differentiable_at
lemma differentiable_on.inv (hc : differentiable_on 𝕜 c s) (hx : ∀ x ∈ s, c x ≠ 0) :
differentiable_on 𝕜 (λx, (c x)⁻¹) s :=
λx h, (hc x h).inv (hx x h)
@[simp] lemma differentiable.inv (hc : differentiable 𝕜 c) (hx : ∀ x, c x ≠ 0) :
differentiable 𝕜 (λx, (c x)⁻¹) :=
λx, (hc x).inv (hx x)
lemma deriv_within_inv' (hc : differentiable_within_at 𝕜 c s x) (hx : c x ≠ 0)
(hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, (c x)⁻¹) s x = - (deriv_within c s x) / (c x)^2 :=
(hc.has_deriv_within_at.inv hx).deriv_within hxs
@[simp] lemma deriv_inv' (hc : differentiable_at 𝕜 c x) (hx : c x ≠ 0) :
deriv (λx, (c x)⁻¹) x = - (deriv c x) / (c x)^2 :=
(hc.has_deriv_at.inv hx).deriv
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, sub_eq_add_neg],
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
@[simp] 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)
@[simp] 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
@[simp] 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
lemma differentiable_within_at.div_const (hc : differentiable_within_at 𝕜 c s x) {d : 𝕜} :
differentiable_within_at 𝕜 (λx, c x / d) s x :=
by simp [div_eq_inv_mul, differentiable_within_at.const_mul, hc]
@[simp] lemma differentiable_at.div_const (hc : differentiable_at 𝕜 c x) {d : 𝕜} :
differentiable_at 𝕜 (λ x, c x / d) x :=
by simp [div_eq_inv_mul, hc]
lemma differentiable_on.div_const (hc : differentiable_on 𝕜 c s) {d : 𝕜} :
differentiable_on 𝕜 (λx, c x / d) s :=
by simp [div_eq_inv_mul, differentiable_on.const_mul, hc]
@[simp] lemma differentiable.div_const (hc : differentiable 𝕜 c) {d : 𝕜} :
differentiable 𝕜 (λx, c x / d) :=
by simp [div_eq_inv_mul, differentiable.const_mul, hc]
lemma deriv_within_div_const (hc : differentiable_within_at 𝕜 c s x) {d : 𝕜}
(hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, c x / d) s x = (deriv_within c s x) / d :=
by simp [div_eq_inv_mul, deriv_within_const_mul, hc, hxs]
@[simp] lemma deriv_div_const (hc : differentiable_at 𝕜 c x) {d : 𝕜} :
deriv (λx, c x / d) x = (deriv c x) / d :=
by simp [div_eq_inv_mul, deriv_const_mul, hc]
end division
theorem has_strict_deriv_at.has_strict_fderiv_at_equiv {f : 𝕜 → 𝕜} {f' x : 𝕜}
(hf : has_strict_deriv_at f f' x) (hf' : f' ≠ 0) :
has_strict_fderiv_at f
(continuous_linear_equiv.units_equiv_aut 𝕜 (units.mk0 f' hf') : 𝕜 →L[𝕜] 𝕜) x :=
hf
theorem has_deriv_at.has_fderiv_at_equiv {f : 𝕜 → 𝕜} {f' x : 𝕜}
(hf : has_deriv_at f f' x) (hf' : f' ≠ 0) :
has_fderiv_at f
(continuous_linear_equiv.units_equiv_aut 𝕜 (units.mk0 f' hf') : 𝕜 →L[𝕜] 𝕜) x :=
hf
/-- If `f (g y) = y` for `y` in some neighborhood of `a`, `g` is continuous at `a`, and `f` has an
invertible derivative `f'` at `g a` in the strict sense, then `g` has the derivative `f'⁻¹` at `a`
in the strict sense.
This is one of the easy parts of the inverse function theorem: it assumes that we already have an
inverse function. -/
theorem has_strict_deriv_at.of_local_left_inverse {f g : 𝕜 → 𝕜} {f' a : 𝕜}
(hg : continuous_at g a) (hf : has_strict_deriv_at f f' (g a)) (hf' : f' ≠ 0)
(hfg : ∀ᶠ y in 𝓝 a, f (g y) = y) :
has_strict_deriv_at g f'⁻¹ a :=
(hf.has_strict_fderiv_at_equiv hf').of_local_left_inverse hg hfg
/-- If `f (g y) = y` for `y` in some neighborhood of `a`, `g` is continuous at `a`, and `f` has an
invertible derivative `f'` at `g a`, then `g` has the derivative `f'⁻¹` at `a`.
This is one of the easy parts of the inverse function theorem: it assumes that we already have
an inverse function. -/
theorem has_deriv_at.of_local_left_inverse {f g : 𝕜 → 𝕜} {f' a : 𝕜}
(hg : continuous_at g a) (hf : has_deriv_at f f' (g a)) (hf' : f' ≠ 0)
(hfg : ∀ᶠ y in 𝓝 a, f (g y) = y) :
has_deriv_at g f'⁻¹ a :=
(hf.has_fderiv_at_equiv hf').of_local_left_inverse hg hfg
/-- If `f` is a local homeomorphism defined on a neighbourhood of `f.symm a`, and `f` has an
nonzero derivative `f'` at `f.symm a`, then `f.symm` has the derivative `f'⁻¹` at `a`.
This is one of the easy parts of the inverse function theorem: it assumes that we already have
an inverse function. -/
lemma local_homeomorph.has_deriv_at_symm (f : local_homeomorph 𝕜 𝕜) {a f' : 𝕜}
(ha : a ∈ f.target) (hf' : f' ≠ 0) (htff' : has_deriv_at f f' (f.symm a)) :
has_deriv_at f.symm f'⁻¹ a :=
htff'.of_local_left_inverse (f.symm.continuous_at ha) hf' (f.eventually_right_inverse ha)
lemma has_deriv_at.eventually_ne (h : has_deriv_at f f' x) (hf' : f' ≠ 0) :
∀ᶠ z in 𝓝[{x}ᶜ] x, f z ≠ f x :=
(has_deriv_at_iff_has_fderiv_at.1 h).eventually_ne
⟨∥f'∥⁻¹, λ z, by field_simp [norm_smul, mt norm_eq_zero.1 hf']⟩
theorem not_differentiable_within_at_of_local_left_inverse_has_deriv_within_at_zero
{f g : 𝕜 → 𝕜} {a : 𝕜} {s t : set 𝕜} (ha : a ∈ s) (hsu : unique_diff_within_at 𝕜 s a)
(hf : has_deriv_within_at f 0 t (g a)) (hst : maps_to g s t) (hfg : f ∘ g =ᶠ[𝓝[s] a] id) :
¬differentiable_within_at 𝕜 g s a :=
begin
intro hg,
have := (hf.comp a hg.has_deriv_within_at hst).congr_of_eventually_eq_of_mem hfg.symm ha,
simpa using hsu.eq_deriv _ this (has_deriv_within_at_id _ _)
end
theorem not_differentiable_at_of_local_left_inverse_has_deriv_at_zero
{f g : 𝕜 → 𝕜} {a : 𝕜} (hf : has_deriv_at f 0 (g a)) (hfg : f ∘ g =ᶠ[𝓝 a] id) :
¬differentiable_at 𝕜 g a :=
begin
intro hg,
have := (hf.comp a hg.has_deriv_at).congr_of_eventually_eq hfg.symm,
simpa using has_deriv_at_unique this (has_deriv_at_id a)
end
end
namespace polynomial
/-! ### Derivative of a polynomial -/
variables {x : 𝕜} {s : set 𝕜}
variable (p : polynomial 𝕜)
/-- The derivative (in the analysis sense) of a polynomial `p` is given by `p.derivative`. -/
protected lemma has_strict_deriv_at (x : 𝕜) :
has_strict_deriv_at (λx, p.eval x) (p.derivative.eval x) x :=
begin
apply p.induction_on,
{ simp [has_strict_deriv_at_const] },
{ assume p q hp hq,
convert hp.add hq;
simp },
{ assume n a h,
convert h.mul (has_strict_deriv_at_id x),
{ ext y, simp [pow_add, mul_assoc] },
{ simp [pow_add], ring } }
end
/-- 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 :=
(p.has_strict_deriv_at x).has_deriv_at
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 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 : ℕ` -/
variables {x : 𝕜} {s : set 𝕜} {c : 𝕜 → 𝕜} {c' : 𝕜}
variable {n : ℕ }
lemma has_strict_deriv_at_pow (n : ℕ) (x : 𝕜) :
has_strict_deriv_at (λx, x^n) ((n : 𝕜) * x^(n-1)) x :=
begin
convert (polynomial.C (1 : 𝕜) * (polynomial.X)^n).has_strict_deriv_at x,
{ simp },
{ rw [polynomial.derivative_C_mul_X_pow], simp }
end
lemma has_deriv_at_pow (n : ℕ) (x : 𝕜) : has_deriv_at (λx, x^n) ((n : 𝕜) * x^(n-1)) x :=
(has_strict_deriv_at_pow n x).has_deriv_at
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) :=
(has_deriv_within_at_pow n x s).deriv_within hxs
lemma iter_deriv_pow' {k : ℕ} :
deriv^[k] (λx:𝕜, x^n) = λ x, (∏ i in finset.range k, (n - i) : ℕ) * x^(n-k) :=
begin
induction k with k ihk,
{ simp only [one_mul, finset.prod_range_zero, function.iterate_zero_apply, nat.sub_zero,
nat.cast_one] },
{ simp only [function.iterate_succ_apply', ihk, finset.prod_range_succ],
ext x,
rw [((has_deriv_at_pow (n - k) x).const_mul _).deriv, nat.cast_mul, mul_left_comm, mul_assoc,
nat.succ_eq_add_one, nat.sub_sub] }
end
lemma iter_deriv_pow {k : ℕ} :
deriv^[k] (λx:𝕜, x^n) x = (∏ i in finset.range k, (n - i) : ℕ) * x^(n-k) :=
congr_fun iter_deriv_pow' x
lemma has_deriv_within_at.pow (hc : has_deriv_within_at c c' s x) :
has_deriv_within_at (λ y, (c y)^n) ((n : 𝕜) * (c x)^(n-1) * c') s x :=
(has_deriv_at_pow n (c x)).comp_has_deriv_within_at x hc
lemma has_deriv_at.pow (hc : has_deriv_at c c' x) :
has_deriv_at (λ y, (c y)^n) ((n : 𝕜) * (c x)^(n-1) * c') x :=
by { rw ← has_deriv_within_at_univ at *, exact hc.pow }
lemma differentiable_within_at.pow (hc : differentiable_within_at 𝕜 c s x) :
differentiable_within_at 𝕜 (λx, (c x)^n) s x :=
hc.has_deriv_within_at.pow.differentiable_within_at
@[simp] lemma differentiable_at.pow (hc : differentiable_at 𝕜 c x) :
differentiable_at 𝕜 (λx, (c x)^n) x :=
hc.has_deriv_at.pow.differentiable_at
lemma differentiable_on.pow (hc : differentiable_on 𝕜 c s) :
differentiable_on 𝕜 (λx, (c x)^n) s :=
λx h, (hc x h).pow
@[simp] lemma differentiable.pow (hc : differentiable 𝕜 c) :
differentiable 𝕜 (λx, (c x)^n) :=
λx, (hc x).pow
lemma deriv_within_pow' (hc : differentiable_within_at 𝕜 c s x)
(hxs : unique_diff_within_at 𝕜 s x) :
deriv_within (λx, (c x)^n) s x = (n : 𝕜) * (c x)^(n-1) * (deriv_within c s x) :=
hc.has_deriv_within_at.pow.deriv_within hxs
@[simp] lemma deriv_pow'' (hc : differentiable_at 𝕜 c x) :
deriv (λx, (c x)^n) x = (n : 𝕜) * (c x)^(n-1) * (deriv c x) :=
hc.has_deriv_at.pow.deriv
end pow
section fpow
/-! ### Derivative of `x ↦ x^m` for `m : ℤ` -/
variables {x : 𝕜} {s : set 𝕜}
variable {m : ℤ}
lemma has_strict_deriv_at_fpow (m : ℤ) (hx : x ≠ 0) :
has_strict_deriv_at (λx, x^m) ((m : 𝕜) * x^(m-1)) x :=
begin
have : ∀ m : ℤ, 0 < m → has_strict_deriv_at (λx, x^m) ((m:𝕜) * x^(m-1)) x,
{ assume m hm,
lift m to ℕ using (le_of_lt hm),
simp only [fpow_of_nat, int.cast_coe_nat],
convert has_strict_deriv_at_pow _ _ using 2,
rw [← int.coe_nat_one, ← int.coe_nat_sub, fpow_coe_nat],
norm_cast at hm,
exact nat.succ_le_of_lt hm },
rcases lt_trichotomy m 0 with hm|hm|hm,
{ have := (has_strict_deriv_at_inv _).scomp _ (this (-m) (neg_pos.2 hm));
[skip, exact fpow_ne_zero_of_ne_zero hx _],
simp only [(∘), fpow_neg, one_div, inv_inv', smul_eq_mul] at this,
convert this using 1,
rw [pow_two, mul_inv', inv_inv', int.cast_neg, ← neg_mul_eq_neg_mul, neg_mul_neg,
← fpow_add hx, mul_assoc, ← fpow_add hx], congr, abel },
{ simp only [hm, fpow_zero, int.cast_zero, zero_mul, has_strict_deriv_at_const] },
{ exact this m hm }
end
lemma has_deriv_at_fpow (m : ℤ) (hx : x ≠ 0) :
has_deriv_at (λx, x^m) ((m : 𝕜) * x^(m-1)) x :=
(has_strict_deriv_at_fpow m hx).has_deriv_at
theorem has_deriv_within_at_fpow (m : ℤ) (hx : x ≠ 0) (s : set 𝕜) :
has_deriv_within_at (λx, x^m) ((m : 𝕜) * x^(m-1)) s x :=
(has_deriv_at_fpow m hx).has_deriv_within_at
lemma differentiable_at_fpow (hx : x ≠ 0) : differentiable_at 𝕜 (λx, x^m) x :=
(has_deriv_at_fpow m hx).differentiable_at
lemma differentiable_within_at_fpow (hx : x ≠ 0) :
differentiable_within_at 𝕜 (λx, x^m) s x :=
(differentiable_at_fpow hx).differentiable_within_at
lemma differentiable_on_fpow (hs : (0:𝕜) ∉ s) : differentiable_on 𝕜 (λx, x^m) s :=
λ x hxs, differentiable_within_at_fpow (λ hx, hs $ hx ▸ hxs)
-- TODO : this is true at `x=0` as well
lemma deriv_fpow (hx : x ≠ 0) : deriv (λx, x^m) x = (m : 𝕜) * x^(m-1) :=
(has_deriv_at_fpow m hx).deriv
lemma deriv_within_fpow (hxs : unique_diff_within_at 𝕜 s x) (hx : x ≠ 0) :
deriv_within (λx, x^m) s x = (m : 𝕜) * x^(m-1) :=
(has_deriv_within_at_fpow m hx s).deriv_within hxs
lemma iter_deriv_fpow {k : ℕ} (hx : x ≠ 0) :
deriv^[k] (λx:𝕜, x^m) x = (∏ i in finset.range k, (m - i) : ℤ) * x^(m-k) :=
begin
induction k with k ihk generalizing x hx,
{ simp only [one_mul, finset.prod_range_zero, function.iterate_zero_apply, int.coe_nat_zero,
sub_zero, int.cast_one] },
{ rw [function.iterate_succ', finset.prod_range_succ, int.cast_mul, mul_assoc, mul_left_comm,
int.coe_nat_succ, ← sub_sub, ← ((has_deriv_at_fpow _ hx).const_mul _).deriv],
exact filter.eventually_eq.deriv_eq (eventually.mono (mem_nhds_sets is_open_ne hx) @ihk) }
end
end fpow
/-! ### Upper estimates on liminf and limsup -/
section real
variables {f : ℝ → ℝ} {f' : ℝ} {s : set ℝ} {x : ℝ} {r : ℝ}
lemma has_deriv_within_at.limsup_slope_le (hf : has_deriv_within_at f f' s x) (hr : f' < r) :
∀ᶠ z in 𝓝[s \ {x}] x, (z - x)⁻¹ * (f z - f x) < r :=
has_deriv_within_at_iff_tendsto_slope.1 hf (mem_nhds_sets is_open_Iio hr)
lemma has_deriv_within_at.limsup_slope_le' (hf : has_deriv_within_at f f' s x)
(hs : x ∉ s) (hr : f' < r) :
∀ᶠ z in 𝓝[s] x, (z - x)⁻¹ * (f z - f x) < r :=
(has_deriv_within_at_iff_tendsto_slope' hs).1 hf (mem_nhds_sets is_open_Iio hr)
lemma has_deriv_within_at.liminf_right_slope_le
(hf : has_deriv_within_at f f' (Ici x) x) (hr : f' < r) :
∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r :=
(hf.Ioi_of_Ici.limsup_slope_le' (lt_irrefl x) hr).frequently
end real
section real_space
open metric
variables {E : Type u} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {f' : E} {s : set ℝ}
{x r : ℝ}
/-- If `f` has derivative `f'` within `s` at `x`, then for any `r > ∥f'∥` the ratio
`∥f z - f x∥ / ∥z - x∥` is less than `r` in some neighborhood of `x` within `s`.
In other words, the limit superior of this ratio as `z` tends to `x` along `s`
is less than or equal to `∥f'∥`. -/
lemma has_deriv_within_at.limsup_norm_slope_le
(hf : has_deriv_within_at f f' s x) (hr : ∥f'∥ < r) :
∀ᶠ z in 𝓝[s] x, ∥z - x∥⁻¹ * ∥f z - f x∥ < r :=
begin
have hr₀ : 0 < r, from lt_of_le_of_lt (norm_nonneg f') hr,
have A : ∀ᶠ z in 𝓝[s \ {x}] x, ∥(z - x)⁻¹ • (f z - f x)∥ ∈ Iio r,
from (has_deriv_within_at_iff_tendsto_slope.1 hf).norm (mem_nhds_sets is_open_Iio hr),
have B : ∀ᶠ z in 𝓝[{x}] x, ∥(z - x)⁻¹ • (f z - f x)∥ ∈ Iio r,
from mem_sets_of_superset self_mem_nhds_within
(singleton_subset_iff.2 $ by simp [hr₀]),
have C := mem_sup_sets.2 ⟨A, B⟩,
rw [← nhds_within_union, diff_union_self, nhds_within_union, mem_sup_sets] at C,
filter_upwards [C.1],
simp only [norm_smul, mem_Iio, normed_field.norm_inv],
exact λ _, id
end
/-- If `f` has derivative `f'` within `s` at `x`, then for any `r > ∥f'∥` the ratio
`(∥f z∥ - ∥f x∥) / ∥z - x∥` is less than `r` in some neighborhood of `x` within `s`.
In other words, the limit superior of this ratio as `z` tends to `x` along `s`
is less than or equal to `∥f'∥`.
This lemma is a weaker version of `has_deriv_within_at.limsup_norm_slope_le`
where `∥f z∥ - ∥f x∥` is replaced by `∥f z - f x∥`. -/
lemma has_deriv_within_at.limsup_slope_norm_le
(hf : has_deriv_within_at f f' s x) (hr : ∥f'∥ < r) :
∀ᶠ z in 𝓝[s] x, ∥z - x∥⁻¹ * (∥f z∥ - ∥f x∥) < r :=
begin
apply (hf.limsup_norm_slope_le hr).mono,
assume z hz,
refine lt_of_le_of_lt (mul_le_mul_of_nonneg_left (norm_sub_norm_le _ _) _) hz,
exact inv_nonneg.2 (norm_nonneg _)
end
/-- If `f` has derivative `f'` within `(x, +∞)` at `x`, then for any `r > ∥f'∥` the ratio
`∥f z - f x∥ / ∥z - x∥` is frequently less than `r` as `z → x+0`.
In other words, the limit inferior of this ratio as `z` tends to `x+0`
is less than or equal to `∥f'∥`. See also `has_deriv_within_at.limsup_norm_slope_le`
for a stronger version using limit superior and any set `s`. -/
lemma has_deriv_within_at.liminf_right_norm_slope_le
(hf : has_deriv_within_at f f' (Ici x) x) (hr : ∥f'∥ < r) :
∃ᶠ z in 𝓝[Ioi x] x, ∥z - x∥⁻¹ * ∥f z - f x∥ < r :=
(hf.Ioi_of_Ici.limsup_norm_slope_le hr).frequently
/-- If `f` has derivative `f'` within `(x, +∞)` at `x`, then for any `r > ∥f'∥` the ratio
`(∥f z∥ - ∥f x∥) / (z - x)` is frequently less than `r` as `z → x+0`.
In other words, the limit inferior of this ratio as `z` tends to `x+0`
is less than or equal to `∥f'∥`.
See also
* `has_deriv_within_at.limsup_norm_slope_le` for a stronger version using
limit superior and any set `s`;
* `has_deriv_within_at.liminf_right_norm_slope_le` for a stronger version using
`∥f z - f x∥` instead of `∥f z∥ - ∥f x∥`. -/
lemma has_deriv_within_at.liminf_right_slope_norm_le
(hf : has_deriv_within_at f f' (Ici x) x) (hr : ∥f'∥ < r) :
∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (∥f z∥ - ∥f x∥) < r :=
begin
have := (hf.Ioi_of_Ici.limsup_slope_norm_le hr).frequently,
refine this.mp (eventually.mono self_mem_nhds_within _),
assume z hxz hz,
rwa [real.norm_eq_abs, abs_of_pos (sub_pos_of_lt hxz)] at hz
end
end real_space
|
db9ff4f0b737ab2ebe84206cd1f908222af32afa | 1dd482be3f611941db7801003235dc84147ec60a | /src/tactic/tidy.lean | e6f8035ca928d688b93dbb6db582afd0dde5a316 | [
"Apache-2.0"
] | permissive | sanderdahmen/mathlib | 479039302bd66434bb5672c2a4cecf8d69981458 | 8f0eae75cd2d8b7a083cf935666fcce4565df076 | refs/heads/master | 1,587,491,322,775 | 1,549,672,060,000 | 1,549,672,060,000 | 169,748,224 | 0 | 0 | Apache-2.0 | 1,549,636,694,000 | 1,549,636,694,000 | null | UTF-8 | Lean | false | false | 2,981 | 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 tactic
import tactic.auto_cases
import tactic.chain
import tactic.interactive
namespace tactic
namespace tidy
meta def tidy_attribute : user_attribute := {
name := `tidy,
descr := "A tactic that should be called by `tidy`."
}
run_cmd attribute.register ``tidy_attribute
meta def name_to_tactic (n : name) : tactic string :=
do d ← get_decl n,
e ← mk_const n,
let t := d.type,
if (t =ₐ `(tactic unit)) then
(eval_expr (tactic unit) e) >>= (λ t, t >> pure n.to_string)
else if (t =ₐ `(tactic string)) then
(eval_expr (tactic string) e) >>= (λ t, t)
else fail "invalid type for @[tidy] tactic"
meta def run_tactics : tactic string :=
do names ← attribute.get_instances `tidy,
first (names.map name_to_tactic) <|> fail "no @[tidy] tactics succeeded"
meta def ext1_wrapper : tactic string :=
do ng ← num_goals,
ext1 [] {apply_cfg . new_goals := new_goals.all},
ng' ← num_goals,
return $ if ng' > ng then
"tactic.ext1 [] {new_goals := tactic.new_goals.all}"
else "ext1"
meta def default_tactics : list (tactic string) :=
[ reflexivity >> pure "refl",
`[exact dec_trivial] >> pure "exact dec_trivial",
propositional_goal >> assumption >> pure "assumption",
ext1_wrapper,
intros1 >>= λ ns, pure ("intros " ++ (" ".intercalate (ns.map (λ e, e.to_string)))),
auto_cases,
`[apply_auto_param] >> pure "apply_auto_param",
`[dsimp at *] >> pure "dsimp at *",
`[simp at *] >> pure "simp at *",
fsplit >> pure "fsplit",
injections_and_clear >> pure "injections_and_clear",
propositional_goal >> (`[solve_by_elim]) >> pure "solve_by_elim",
`[unfold_aux] >> pure "unfold_aux",
tidy.run_tactics ]
meta structure cfg :=
(trace_result : bool := ff)
(trace_result_prefix : string := "/- `tidy` says -/ ")
(tactics : list (tactic string) := default_tactics)
declare_trace tidy
meta def core (cfg : cfg := {}) : tactic (list string) :=
do
results ← chain cfg.tactics,
when (cfg.trace_result ∨ is_trace_enabled_for `tidy) $
trace (cfg.trace_result_prefix ++ (", ".intercalate results)),
return results
end tidy
meta def tidy (cfg : tidy.cfg := {}) := tactic.tidy.core cfg >> skip
namespace interactive
meta def tidy (cfg : tidy.cfg := {}) := tactic.tidy cfg
end interactive
@[hole_command] meta def tidy_hole_cmd : hole_command :=
{ name := "tidy",
descr := "Use `tidy` to complete the goal.",
action := λ _, do script ← tidy.core, return [("begin " ++ (", ".intercalate script) ++ " end", "by tidy")] }
end tactic
|
a76cdd7e2d09f27f3f84d970297a5e467a587c94 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/notation.lean | e9cef7b601a0eb7c46c9a11947651b4853b00a42 | [
"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 | 404 | lean | import logic data.num
open num
constant b : num
check b + b + b
check true ∧ false ∧ true
check (true ∧ false) ∧ true
check (2:num) + (2 + 2)
check (2 + 2) + (2:num)
check (1:num) = (2 + 3)*2
check (2:num) + 3 * 2 = 3 * 2 + 2
check (true ∨ false) = (true ∨ false) ∧ true
check true ∧ (false ∨ true)
constant A : Type₁
constant a : A
notation 1 := a
check a
open nat
check ℕ → ℕ
|
b079f16c7873903d6b35cbc5a04ed90705fa8834 | 3489b85df841aeadbf61fa3d15cc866d97c45021 | /firstorder.lean | a1e57a3b2072292b8b80cc82e37456609d2420c5 | [] | no_license | nthomas103/firstorder | 257e26fc36ef79e7e9960dcc4cea90a1b800d84e | 1252a85a878d43fba1cc9844361bbb615e768171 | refs/heads/master | 1,611,165,707,952 | 1,471,292,864,000 | 1,471,292,864,000 | 65,762,786 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,323 | lean |
open tactic expr list environment nat bool
meta_definition intro' (n : name) : tactic unit := intro n >> skip
meta_definition cond_intros : tactic unit := do
t ← target,
when (is_pi t = ff) (fail "intros: not a pi type"),
intro' `h
--example (a b : Prop) : a → b := by do cond_intros
--example (a b : Prop) : b := by do cond_intros
meta_definition axioms' : tactic unit := do
solve [triv, assumption, contradiction]
private meta_definition unit_prop' (ctx : list expr) (h : expr) : tactic unit := do
t ← infer_type h,
match t with
| (pi _ _ a b) :=
do { assert `ab b,
apply h,
assumption,
clear h }
| _ := fail "unit propagation not possible because not a pi type"
end
meta_definition unit_prop (e : expr) : tactic unit := do
ctx ← local_context,
unit_prop' ctx e
private meta_definition get_constructors_for (e : expr) : tactic (list name) :=
do env ← get_env,
I ← return $ expr.const_name (expr.get_app_fn e),
when (environment.is_inductive env I = ff) (fail "constructor tactic failed, target is not an inductive datatype"),
return $ environment.constructors_of env I
meta_definition induction (e : expr) : tactic unit := do
env ← get_env,
t ← infer_type e,
I ← return $ expr.const_name (expr.get_app_fn t),
when (is_inductive env I = ff) (fail "constructor tactic failed; target is not an inductive datatype"),
induction_core semireducible e (I <.> "rec") []
-- non-branching induction
meta_definition nb_induction (e : expr) : tactic unit := do
env ← get_env,
t ← infer_type e,
I ← return $ expr.const_name (expr.get_app_fn t),
when (is_inductive env I = ff) (fail "constructor tactic failed; target is not an inductive datatype"),
when (length (constructors_of env I) > 1) (fail "we are only allowing non-branching induction"),
induction_core semireducible e (I <.> "rec") []
--example (T : Type) (l : list T) : length l ≥ 0 := by do get_local `l >>= induction
--example (a b : Prop) (hab : a ∨ b) : b ∨ a := by do get_local `hab >>= induction
--example (a b : Prop) (hab : a ∧ b) : b ∧ a := by do get_local `hab >>= induction, constructor; assumption
definition ar (a b : expr) := pi `h_ binder_info.default a b
meta_definition disj_ar (h : expr) : tactic unit := do
t ← infer_type h,
or ← mk_const `or,
when ((app_fn (app_fn (binding_domain t))) ≠ or) (fail "not or"),
let a := (app_arg (app_fn (binding_domain t))) in
let b := (app_arg (binding_domain t)) in
let c := (binding_body t) in
do { assert `hac (ar a c), intro `ha, apply h, left; assumption,
assert `hbc (ar b c), intro `hb, apply h, right; assumption },
clear h
--example (a b c g : Prop) (h : a ∨ b → c) : g := by do get_local `h >>= disj_ar
/-
inductive comb (a b c d e f : Prop) : Prop :=
| c1 : a → b → c → comb a b c d e f
| c2 : d → e → f → comb a b c d e f
example (a b c d g : Type) (h1 : a → b → c) (h2 : c → d) : g := by do
h1 ← get_local `h1,
h2 ← get_local `h2,
a ← get_local `a,
b ← get_local `b,
c ← get_local `c,
d ← get_local `d,
assert `h (ar a (ar b d)), intros >> skip, apply h2, apply h1; assumption
/-
example (a b c d e f g r : Prop) (h : comb a b c d e f → r) : g := by do
h ← get_local `h,
a ← get_local `a,
b ← get_local `b,
c ← get_local `c,
d ← get_local `d,
e ← get_local `e,
f ← get_local `f,
r ← get_local `r,
c1 ← mk_const `comb.c1,
c2 ← mk_const `comb.c2,
t ← infer_type c1,
trace (app_fn t),
assert `h1 (ar a (ar b (ar c r))), intros >> skip, apply h, apply c1; assumption,
assert `h2 (ar d (ar e (ar f r))), intros >> skip, apply h, apply c2; assumption,
clear h
-/
meta_definition helper : list expr → tactic unit
| [] := skip
| (c::cs) := skip--do assert `h (ar
meta_definition LIf (h : expr) : tactic unit := do
ty ← infer_type h,
match ty with
| (pi _ _ a b) := do
env ← get_env,
trace a,
I ← return $ const_name (get_app_fn a),
trace I,
when (environment.is_inductive env I = ff) (fail "constructor tactic failed, target is not an inductive datatype"),
cs ← get_constructors_for (get_app_fn a),
trace cs
| _ := fail "LIf failed"
end
--example (a b c g : Prop) (h : a ∨ b → c) : g := by do get_local `h >>= LIf
-/
meta_definition conj_ar (h : expr) : tactic unit := do
t ← infer_type h,
and ← mk_const `and,
when ((app_fn (app_fn (binding_domain t))) ≠ and) (fail "not and"),
assert `habc (ar (app_arg (app_fn (binding_domain t))) (ar (app_arg (binding_domain t)) (binding_body t))),
intro' `ha, intro' `hb, apply h, split; assumption,
clear h
--example (a b c g : Prop) (h : a ∧ b → c) : g := by do get_local `h >>= conj_ar
--example (a b c : Prop) (P : ℕ → Prop) (hP : ∀ n, P n) : P 0 := by do get_local `hP >>= conj_ar
meta_definition unfold_one_at (u : list name) (h : expr) : tactic unit := do
num_reverted : ℕ ← revert h,
(expr.pi n bi d b : expr) ← target | failed,
new_H : expr ← unfold_expr_core ff (occurrences.pos [1]) u d,
change $ expr.pi n bi new_H b,
intron num_reverted,
when (new_H = d) (fail "nothing unfolded")
--example (a : Prop) (h : ¬a) : ¬a := by do get_local `h >>= unfold_one_at [`not]
--example (a : Prop) (h : a) : ¬a := by do get_local `h >>= unfold_one_at [`not]
meta_definition unfold_one (u : list name) : tactic unit := do
goal ← target,
new_goal ← unfold_expr_core ff (occurrences.pos [1]) u goal,
when (goal = new_goal) (fail "nothing unfolded"),
change new_goal
--example (a : Prop) (h : ¬a) : ¬a := by do unfold_one [`not]
--example (a : Prop) (h : a) : a := by do unfold_one [`not]
meta_definition cleanup_true (e : expr) : tactic unit := do
tru ← mk_const `true,
t ← infer_type e,
when (t ≠ tru) (fail "no true to clean up"),
clear e
meta_definition cleanup_false (e : expr) : tactic unit := do
fal ← mk_const `false,
t ← infer_type e,
match t with
| (pi _ _ a _) := when (a ≠ fal) (fail " pi, but no false to clean up")
| _ := fail "no false to clean up"
end,
clear e
meta_definition cleanup (e : expr) : tactic unit := do
cleanup_true e <|> cleanup_false e
--example (a : Prop) (h : true) : a := by do get_local `h >>= cleanup
--example (a : Prop) (h : false → a) : a := by do get_local `h >>= cleanup
--example (a b : Prop) (h : b → a) : a := by do get_local `h >>= cleanup
private meta_definition simplif_aux : list expr → tactic unit
| [] := fail "simplif failed"
| (h::hs) := do t ← infer_type h, trace t,
(trace " nb_induction"; nb_induction h; trace " nb_induction succeeded") <|>
(trace " unfold_one_at iff"; unfold_one_at [`iff] h; trace " unfold_one_at iff succeeded") <|>
(trace " unfold_one_at not"; unfold_one_at [`not] h; trace " unfold_one_at not succeeded") <|>
(trace " unit prop"; unit_prop h; trace " unit prop succeeded") <|>
(trace " conj arrow"; conj_ar h; trace " conj arrow succeeded") <|>
(trace " disj arrow"; disj_ar h; trace " disj arrow succeeded") <|>
(trace " cleanup"; cleanup h; trace " cleanup succeeded") <|>
simplif_aux hs
meta_definition simplif : tactic unit := do local_context >>= simplif_aux
meta_definition Lff (H : expr) : tactic unit :=
do t ← infer_type H,
G ← target,
match t with
| pi _ _ (pi _ _ A B) C :=
do assert `h₁ (ar A (ar (ar B C) B)), rotate 1,
assert `h₂ (ar C G),
h₁ ← get_local `h₁, clear h₁, rotate 1,
h₂ ← get_local `h₂, apply h₂,
apply H, intro `ha,
apply h₁, assumption,
intro `hb, apply H,
intro `ha1, assumption, clear H,
intro `ha, intro `hbc, rotate 1, clear H,
intro `hc, rotate 1
| _ := fail "not right form to Lff"
end
meta_definition tauto' : ℕ → tactic unit
| 0 := fail "tauto failed"
| (n+1) := do trace "", trace "TAUTO TOP LEVEL", trace (n+1), trace_state, trace "",
(trace "trying axioms"; axioms') <|>
(trace "trying cond_intros"; cond_intros; trace "cond_intros succeeded"; tauto' (n+1)) <|>
(trace "trying simplif"; simplif; trace "simplif succeeded"; tauto' (n+1)) <|>
(trace "trying single-constructor"; solve [split; trace "single-constructor succeeded"; tauto' (n+1)]) <|>
(trace "trying unfolding iff in goal"; unfold_one [`iff]; tauto' (n+1)) <|>
(trace "trying unfolding not in goal"; unfold_one [`not]; tauto' (n+1)) <|>
(trace "trying Lff";
(do ctx ← local_context, solve (map (λ h, do Lff h; trace "Lff applied"; tauto' n) ctx))) <|>
(trace "trying branching induction";
(do ctx ← local_context, solve (map (λ h, do induction h; trace "branching induction applied"; tauto' n) ctx))) <|>
(trace "trying multi_constructor";
(do t ← target, cs ← get_constructors_for t, solve (map (λ c, mk_const c >>= apply; (do trace "branch applied:", trace c); tauto' n) cs))) <|>
(trace "END OF BRANCH"; trace ""; fail "tauto failed")
definition tauto_max_depth : ℕ := 4
meta_definition tauto : tactic unit := do tauto' tauto_max_depth
-- some tests from Coq (https://github.com/coq/coq/blob/trunk/test-suite/success/Tauto.v),
-- from http://www.lix.polytechnique.fr/coq/pylons/contribs/files/ATBR/v8.4/ATBR.BoolView.html,
-- and from old blast tests
section tauto_tests
variables (a b c d e f : Prop)
variable even : ℕ → Prop
variable P : ℕ → Prop
--Problem tests
--example : (∀ x, P x) ∧ b → (∀ y, P y) ∧ P 0 ∨ b ∧ P 0 := by tauto
--example : (∀ A, A ∨ ¬A) → ∀ x y : ℕ, x = y ∨ x ≠ y := by tauto
--example : ∀ b1 b2, b1 = b2 ↔ (b1 = tt ↔ b2 = tt) := by tauto
--example : ∀ (P Q : nat → Prop), (∀n, Q n → P n) → (∀n, Q n) → P 2 := by tauto
--example (a b c : Prop) : ¬ true ∨ false ∨ b ↔ b := by tauto -- should be taken care of by general ind-arrow tactic
--Successful tests
/-
example : true := by tauto
example : false → a := by tauto
example : a → a := by tauto
example : (a → b) → a → b := by tauto
example : ¬ a → ¬ a := by tauto
example : a → (false ∨ a) := by tauto
example : (a → b → c) → (a → b) → a → c := by tauto
example : a → ¬ a → (a → b) → (a ∨ b) → (a ∧ b) → a → false := by tauto
example : ((a ∧ b) ∧ c) → b := by tauto
example : ((a → b) → c) → b → c := by tauto
example : (a ∨ b) → (b ∨ a) := by tauto
example : (a → b ∧ c) → (a → b) ∨ (a → c) := by tauto
example : ∀ (x0 : a ∨ b) (x1 : b ∧ c), a → b := by tauto
example : a → b → (c ∨ b) := by tauto
example : (a ∧ b → c) → b → a → c := by tauto
example : (a ∨ b → c) → a → c := by tauto
example : (a ∨ b → c) → b → c := by tauto
example : (a ∧ b) → (b ∧ a) := by tauto
example : (a ↔ b) → a → b := by tauto
example : a → ¬¬a := by tauto
example : ¬¬(a ∨ ¬a) := by tauto
example : ¬¬(a ∨ b → a ∨ b) := by tauto
example : ¬¬((∀ n, even n) ∨ ¬(∀ m, even m)) := by tauto
example : (¬¬b → b) → (a → b) → ¬¬a → b := by tauto
example : (¬¬b → b) → (¬b → ¬ a) → ¬¬a → b := by tauto
example : ((a → b → false) → false) → (b → false) → false := by tauto
example : ((((c → false) → a) → ((b → false) → a) → false) → false) → (((c → b → false) → false) → false) → ¬a → a := by tauto
example (p q r : Prop) (a b : nat) : true → a = a → q → q → p → p := by tauto
example : ∀ (F F' : Prop), F ∧ F' → F := by tauto
example : ∀ (F1 F2 F3 : Prop), ((¬F1 ∧ F3) ∨ (F2 ∧ ¬F3)) → (F2 → F1) → (F2 → F3) → ¬F2 := by tauto
example : ∀ (f : nat → Prop), f 2 → ∃ x, f x := by tauto
example : true ∧ true ∧ true ∧ true ∧ true ∧ true ∧ true := by tauto
example : ∀ (P : nat → Prop), P 0 → (P 0 → P 1) → (P 1 → P 2) → (P 2) := by tauto
example : ¬¬¬¬¬a → ¬¬¬¬¬¬¬¬a → false := by tauto
example : ∀ n, ¬¬(even n ∨ ¬even n) := by tauto
example : ∀ (p q r s : Prop) (a b : nat), r ∨ s → p ∨ q → a = b → q ∨ p := by tauto
example : (∀ x, P x) → (∀ y, P y) := by tauto
example : ((a ↔ b) → (b ↔ c)) → ((b ↔ c) → (c ↔ a)) → ((c ↔ a) → (a ↔ b)) → (a ↔ b) := by tauto
example : ((¬a ∨ b) ∧ (¬b ∨ b) ∧ (¬a ∨ ¬b) ∧ (¬b ∨ ¬b) → false) → ¬((a → b) → b) → false := by tauto
example : ¬((a → b) → b) → ((¬b ∨ ¬b) ∧ (¬b ∨ ¬a) ∧ (b ∨ ¬b) ∧ (b ∨ ¬a) → false) → false := by tauto
example : (¬a ↔ b) → (¬b ↔ a) → (¬¬a ↔ a) := by tauto
example : (¬ a ↔ b) → (¬ (c ∨ e) ↔ d ∧ f) → (¬ (c ∨ a ∨ e) ↔ d ∧ b ∧ f) := by tauto
example {A : Type} (p q : A → Prop) (a b : A) : q a → p b → ∃ x, (p x ∧ x = b) ∨ q x := by tauto
example {A : Type} (p q : A → Prop) (a b : A) : p b → ∃ x, q x ∨ (p x ∧ x = b) := by tauto
example : ¬ a → b → a → c := by tauto
example : a → b → b → ¬ a → c := by tauto
example (a b : nat) : a = b → b = a := by tauto
example (a b c : nat) : a = b → a = c → b = c := by tauto
example (p : nat → Prop) (a b c : nat) : a = b → a = c → p b → p c := by tauto
example (p : Prop) (a b : nat) : a = b → p → p := by tauto
example (a : nat) : zero = succ a → a = a → false := by tauto
example (a b c : nat) : succ (succ a) = succ (succ b) → c = c := by tauto
example (p : Prop) (a b c : nat) : [a, b, c] = [] → p := by tauto
example (p : Prop) (a b : nat) : a = b → b ≠ a → p := by tauto
example : (a ↔ b) → ((b ↔ a) ↔ (a ↔ b)) := by tauto
example (a b c : nat) : b = c → (a = b ↔ c = a) := by tauto
example : ¬¬¬¬¬¬¬¬a → ¬¬¬¬¬a → false := by tauto
example (a b c : Prop) : a ∧ b ∧ c ↔ c ∧ b ∧ a := by tauto
example (a b c : Prop) : a ∧ false ∧ c ↔ false := by tauto
example (a b c : Prop) : a ∨ false ∨ b ↔ b ∨ a := by tauto
example : a ∧ not a ↔ false := by tauto
example : a ∧ b ∧ true → b ∧ a := by tauto
example (A : Type₁) (a₁ a₂ : A) : a₁ = a₂ → (λ (B : Type₁) (f : A → B), f a₁) = (λ (B : Type₁) (f : A → B), f a₂) := by tauto
example (a : nat) : ¬ a = a → false := by tauto
example (A : Type) (p : Prop) (a b c : A) : a = b → ¬ b = a → p := by tauto
example (A : Type) (p : Prop) (a b c : A) : a = b → b ≠ a → p := by tauto
example (p q r s : Prop) : r ∧ s → p ∧ q → q ∧ p := by tauto
example (p q : Prop) : p ∧ p ∧ q ∧ q → q ∧ p := by tauto
example (p : nat → Prop) (q : nat → nat → Prop) : (∃ x y, p x ∧ q x y) → q 0 0 ∧ q 1 1 → (∃ x, p x) := by tauto
example (p q r s : Prop) (a b : nat) : r ∨ s → p ∨ q → a = b → q ∨ p := by tauto
example (p q r : Prop) (a b : nat) : true → a = a → q → q → p → p := by tauto
example (a b : Prop) : a → b → a := by tauto
example (p q : nat → Prop) (a b : nat) : p a → q b → ∃ x, p x := by tauto
example : ∀ b1 b2, b1 && b2 = ff ↔ (b1 = ff ∨ b2 = ff) := by tauto
example : ∀ b1 b2, b1 && b2 = tt ↔ (b1 = tt ∧ b2 = tt) := by tauto
example : ∀ b1 b2, b1 || b2 = ff ↔ (b1 = ff ∧ b2 = ff) := by tauto
example : ∀ b1 b2, b1 || b2 = tt ↔ (b1 = tt ∨ b2 = tt) := by tauto
example : ∀ b, bnot b = tt ↔ b = ff := by tauto
example : ∀ b, bnot b = ff ↔ b = tt := by tauto
example : ∀ b c, b = c ↔ ¬ (b = bnot c) := by tauto
inductive and3 (a b c : Prop) : Prop :=
| mk : a → b → c → and3 a b c
example (h : and3 a b c) : and3 b c a := by tauto
inductive or3 (a b c : Prop) : Prop :=
| in1 : a → or3 a b c
| in2 : b → or3 a b c
| in3 : c → or3 a b c
example (h : a) : or3 a b c := by tauto
example (h : b) : or3 a b c := by tauto
example (h : c) : or3 a b c := by tauto
variables (A₁ A₂ A₃ A₄ B₁ B₂ B₃ B₄ : Prop)
-- H first, all pos
example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) : B₄ := by tauto
example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₄) : B₃ := by tauto
example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n3 : ¬B₃) (n3 : ¬B₄) : B₂ := by tauto
example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : B₁ := by tauto
example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : ¬A₃ := by tauto
example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : ¬A₂ := by tauto
example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : ¬A₁ := by tauto
-- H last, all pos
example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₄ := by tauto
example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₃ := by tauto
example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₂ := by tauto
example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₁ := by tauto
example (a1 : A₁) (a2 : A₂) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬A₃ := by tauto
example (a1 : A₁) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬A₂ := by tauto
example (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬A₁ := by tauto
-- H first, all neg
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) : ¬B₄ := by tauto
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b4 : B₄) : ¬B₃ := by tauto
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b3 : B₃) (b4 : B₄) : ¬B₂ := by tauto
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬B₁ := by tauto
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬¬A₃ := by tauto
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬¬A₂ := by tauto
example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬¬A₁ := by tauto
-- H last, all neg
example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₄ := by tauto
example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₃ := by tauto
example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₂ := by tauto
example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₁ := by tauto
example (n1 : ¬A₁) (n2 : ¬A₂) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬¬A₃ := by tauto
example (n1 : ¬A₁) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬¬A₂ := by tauto
example (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬¬A₁ := by tauto
section club
variables Scottish RedSocks WearKilt Married GoOutSunday : Prop
lemma NoMember : (¬Scottish → RedSocks) → (WearKilt ∨ ¬RedSocks) → (Married → ¬GoOutSunday) →
(GoOutSunday ↔ Scottish) → (WearKilt → Scottish ∧ Married) → (Scottish → WearKilt) → false := by tauto
end club
-/
end tauto_tests
|
6b055b53b5d51a2aed816ad79e7d05d182869f2c | e2fc96178628c7451e998a0db2b73877d0648be5 | /src/classes/unrestricted/closure_properties/reverse.lean | 2a21528de973226cd302761d6f83af37d8bcd935 | [
"BSD-2-Clause"
] | permissive | madvorak/grammars | cd324ae19b28f7b8be9c3ad010ef7bf0fabe5df2 | 1447343a45fcb7821070f1e20b57288d437323a6 | refs/heads/main | 1,692,383,644,884 | 1,692,032,429,000 | 1,692,032,429,000 | 453,948,141 | 7 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,890 | lean | import classes.unrestricted.basics.toolbox
import utilities.language_operations
import utilities.list_utils
variables {T : Type}
section auxiliary
private def reversal_grule {N : Type} (r : grule T N) : grule T N :=
grule.mk r.input_R.reverse r.input_N r.input_L.reverse r.output_string.reverse
private lemma dual_of_reversal_grule {N : Type} (r : grule T N) :
reversal_grule (reversal_grule r) = r :=
begin
cases r,
unfold reversal_grule,
dsimp only,
simp [list.reverse_reverse],
end
private lemma reversal_grule_reversal_grule {N : Type} :
@reversal_grule T N ∘ @reversal_grule T N = id :=
begin
ext,
apply dual_of_reversal_grule,
end
private def reversal_grammar (g : grammar T) : grammar T :=
grammar.mk g.nt g.initial (list.map reversal_grule g.rules)
private lemma dual_of_reversal_grammar (g : grammar T) :
reversal_grammar (reversal_grammar g) = g :=
begin
cases g,
unfold reversal_grammar,
dsimp only,
rw list.map_map,
rw reversal_grule_reversal_grule,
rw list.map_id,
end
private lemma derives_reversed (g : grammar T) (v : list (symbol T g.nt)) :
grammar_derives (reversal_grammar g) [symbol.nonterminal (reversal_grammar g).initial] v →
grammar_derives g [symbol.nonterminal g.initial] v.reverse :=
begin
intro hv,
induction hv with u w trash orig ih,
{
rw list.reverse_singleton,
apply grammar_deri_self,
},
apply grammar_deri_of_deri_tran ih,
rcases orig with ⟨r, rin, x, y, bef, aft⟩,
change r ∈ (list.map _ g.rules) at rin,
rw list.mem_map at rin,
rcases rin with ⟨r₀, rin₀, r_from_r₀⟩,
use r₀,
split,
{
exact rin₀,
},
use y.reverse,
use x.reverse,
split,
{
have rid₁ : r₀.input_L = r.input_R.reverse,
{
rw ←r_from_r₀,
unfold reversal_grule,
rw list.reverse_reverse,
},
have rid₂ : [symbol.nonterminal r₀.input_N] = [symbol.nonterminal r.input_N].reverse,
{
rw ←r_from_r₀,
rw list.reverse_singleton,
refl,
},
have rid₃ : r₀.input_R = r.input_L.reverse,
{
rw ←r_from_r₀,
unfold reversal_grule,
rw list.reverse_reverse,
},
rw [
rid₁, rid₂, rid₃,
←list.reverse_append_append, ←list.reverse_append_append,
←list.append_assoc, ←list.append_assoc
],
congr,
exact bef,
},
{
have snd_from_r : r₀.output_string = r.output_string.reverse,
{
rw ←r_from_r₀,
unfold reversal_grule,
rw list.reverse_reverse,
},
rw snd_from_r,
rw ←list.reverse_append_append,
exact congr_arg list.reverse aft,
},
end
private lemma reversed_word_in_original_language {g : grammar T} {w : list T}
(hyp : w ∈ grammar_language (reversal_grammar g)) :
w.reverse ∈ grammar_language g :=
begin
unfold grammar_language at *,
have almost_done := derives_reversed g (list.map symbol.terminal w) hyp,
rw ←list.map_reverse at almost_done,
exact almost_done,
end
end auxiliary
/-- The class of resursively-enumerable languages is closed under reversal. -/
theorem RE_of_reverse_RE (L : language T) :
is_RE L → is_RE (reverse_lang L) :=
begin
rintro ⟨g, hgL⟩,
rw ←hgL,
use reversal_grammar g,
unfold reverse_lang,
apply set.eq_of_subset_of_subset,
{
intros w hwL,
change w.reverse ∈ grammar_language g,
exact reversed_word_in_original_language hwL,
},
{
intros w hwL,
change w.reverse ∈ grammar_language g at hwL,
obtain ⟨g₀, pre_reversal⟩ : ∃ g₀, g = reversal_grammar g₀,
{
use reversal_grammar g,
rw dual_of_reversal_grammar,
},
rw pre_reversal at hwL ⊢,
have finished_up_to_reverses := reversed_word_in_original_language hwL,
rw dual_of_reversal_grammar,
rw list.reverse_reverse at finished_up_to_reverses,
exact finished_up_to_reverses,
},
end
|
b5680662ba4cf20679a5c909d50db96e3a5292e5 | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/data/polynomial/eval.lean | c48444ae3d5aae50d7536d9eafaa5eb14ac8397a | [
"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 | 26,583 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import data.polynomial.induction
import data.polynomial.degree.definitions
import deprecated.ring
/-!
# Theory of univariate polynomials
The main defs here are `eval₂`, `eval`, and `map`.
We give several lemmas about their interaction with each other and with module operations.
-/
noncomputable theory
open finsupp finset add_monoid_algebra
open_locale big_operators
namespace polynomial
universes u v w y
variables {R : Type u} {S : Type v} {T : Type w} {ι : Type y} {a b : R} {m n : ℕ}
section semiring
variables [semiring R] {p q r : polynomial R}
section
variables [semiring S]
variables (f : R →+* S) (x : S)
/-- Evaluate a polynomial `p` given a ring hom `f` from the scalar ring
to the target and a value `x` for the variable in the target -/
def eval₂ (p : polynomial R) : S :=
p.sum (λ e a, f a * x ^ e)
lemma eval₂_eq_sum {f : R →+* S} {x : S} : p.eval₂ f x = p.sum (λ e a, f a * x ^ e) := rfl
lemma eval₂_eq_lift_nc {f : R →+* S} {x : S} : eval₂ f x = lift_nc ↑f (powers_hom S x) := rfl
lemma eval₂_congr {R S : Type*} [semiring R] [semiring S]
{f g : R →+* S} {s t : S} {φ ψ : polynomial R} :
f = g → s = t → φ = ψ → eval₂ f s φ = eval₂ g t ψ :=
by rintro rfl rfl rfl; refl
@[simp] lemma eval₂_at_zero : p.eval₂ f 0 = f (coeff p 0) :=
begin
-- This proof is lame, and the `finsupp` API shows through.
simp only [eval₂_eq_sum, zero_pow_eq, mul_ite, mul_zero, mul_one, finsupp.sum_ite_eq'],
split_ifs,
{ refl, },
{ simp only [not_not, finsupp.mem_support_iff, ne.def] at h,
apply_fun f at h,
simpa using h.symm, },
end
@[simp] lemma eval₂_zero : (0 : polynomial R).eval₂ f x = 0 :=
finsupp.sum_zero_index
@[simp] lemma eval₂_C : (C a).eval₂ f x = f a :=
(sum_single_index $ by rw [f.map_zero, zero_mul]).trans $ by simp [pow_zero, mul_one]
@[simp] lemma eval₂_X : X.eval₂ f x = x :=
(sum_single_index $ by rw [f.map_zero, zero_mul]).trans $ by rw [f.map_one, one_mul, pow_one]
@[simp] lemma eval₂_monomial {n : ℕ} {r : R} : (monomial n r).eval₂ f x = (f r) * x^n :=
begin
apply sum_single_index,
simp,
end
@[simp] lemma eval₂_X_pow {n : ℕ} : (X^n).eval₂ f x = x^n :=
begin
rw X_pow_eq_monomial,
convert eval₂_monomial f x,
simp,
end
@[simp] lemma eval₂_add : (p + q).eval₂ f x = p.eval₂ f x + q.eval₂ f x :=
finsupp.sum_add_index
(λ _, by rw [f.map_zero, zero_mul])
(λ _ _ _, by rw [f.map_add, add_mul])
@[simp] lemma eval₂_one : (1 : polynomial R).eval₂ f x = 1 :=
by rw [← C_1, eval₂_C, f.map_one]
@[simp] lemma eval₂_bit0 : (bit0 p).eval₂ f x = bit0 (p.eval₂ f x) :=
by rw [bit0, eval₂_add, bit0]
@[simp] lemma eval₂_bit1 : (bit1 p).eval₂ f x = bit1 (p.eval₂ f x) :=
by rw [bit1, eval₂_add, eval₂_bit0, eval₂_one, bit1]
@[simp] lemma eval₂_smul (g : R →+* S) (p : polynomial R) (x : S) {s : R} :
eval₂ g x (s • p) = g s * eval₂ g x p :=
begin
simp only [eval₂, sum_smul_index, forall_const, zero_mul, g.map_zero, g.map_mul, mul_assoc],
rw [←finsupp.mul_sum],
end
@[simp] lemma eval₂_C_X : eval₂ C X p = p :=
polynomial.induction_on' p (λ p q hp hq, by simp [hp, hq])
(λ n x, by rw [eval₂_monomial, monomial_eq_smul_X, C_mul'])
instance eval₂.is_add_monoid_hom : is_add_monoid_hom (eval₂ f x) :=
{ map_zero := eval₂_zero _ _, map_add := λ _ _, eval₂_add _ _ }
@[simp] lemma eval₂_nat_cast (n : ℕ) : (n : polynomial R).eval₂ f x = n :=
nat.rec_on n rfl $ λ n ih, by rw [n.cast_succ, eval₂_add, ih, eval₂_one, n.cast_succ]
variables [semiring T]
lemma eval₂_sum (p : polynomial T) (g : ℕ → T → polynomial R) (x : S) :
(p.sum g).eval₂ f x = p.sum (λ n a, (g n a).eval₂ f x) :=
finsupp.sum_sum_index (by simp [is_add_monoid_hom.map_zero f])
(by intros; simp [right_distrib, is_add_monoid_hom.map_add f])
lemma eval₂_finset_sum (s : finset ι) (g : ι → polynomial R) (x : S) :
(∑ i in s, g i).eval₂ f x = ∑ i in s, (g i).eval₂ f x :=
begin
classical,
induction s using finset.induction with p hp s hs, simp,
rw [sum_insert, eval₂_add, hs, sum_insert]; assumption,
end
lemma eval₂_mul_noncomm (hf : ∀ k, commute (f $ q.coeff k) x) :
eval₂ f x (p * q) = eval₂ f x p * eval₂ f x q :=
begin
simp only [eval₂_eq_lift_nc],
exact lift_nc_mul _ _ p q (λ k n hn, (hf k).pow_right n)
end
@[simp] lemma eval₂_mul_X : eval₂ f x (p * X) = eval₂ f x p * x :=
begin
refine trans (eval₂_mul_noncomm _ _ $ λ k, _) (by rw eval₂_X),
rcases em (k = 1) with (rfl|hk),
{ simp },
{ simp [coeff_X_of_ne_one hk] }
end
@[simp] lemma eval₂_X_mul : eval₂ f x (X * p) = eval₂ f x p * x :=
by rw [X_mul, eval₂_mul_X]
lemma eval₂_mul_C' (h : commute (f a) x) : eval₂ f x (p * C a) = eval₂ f x p * f a :=
begin
rw [eval₂_mul_noncomm, eval₂_C],
intro k,
obtain (hk|(hk : _ = _)) : (C a).coeff k ∈ ({0, a} : set R) := finsupp.single_apply_mem _;
simp [hk, h]
end
lemma eval₂_list_prod_noncomm (ps : list (polynomial R))
(hf : ∀ (p ∈ ps) k, commute (f $ coeff p k) x) :
eval₂ f x ps.prod = (ps.map (polynomial.eval₂ f x)).prod :=
begin
induction ps using list.reverse_rec_on with ps p ihp,
{ simp },
{ simp only [list.forall_mem_append, list.forall_mem_singleton] at hf,
simp [eval₂_mul_noncomm _ _ hf.2, ihp hf.1] }
end
/-- `eval₂` as a `ring_hom` for noncommutative rings -/
def eval₂_ring_hom' (f : R →+* S) (x : S) (hf : ∀ a, commute (f a) x) : polynomial R →+* S :=
{ to_fun := eval₂ f x,
map_add' := λ _ _, eval₂_add _ _,
map_zero' := eval₂_zero _ _,
map_mul' := λ p q, eval₂_mul_noncomm f x (λ k, hf $ coeff q k),
map_one' := eval₂_one _ _ }
end
/-!
We next prove that eval₂ is multiplicative
as long as target ring is commutative
(even if the source ring is not).
-/
section eval₂
variables [comm_semiring S]
variables (f : R →+* S) (x : S)
@[simp] lemma eval₂_mul : (p * q).eval₂ f x = p.eval₂ f x * q.eval₂ f x :=
eval₂_mul_noncomm _ _ $ λ k, commute.all _ _
lemma eval₂_mul_eq_zero_of_left (q : polynomial R) (hp : p.eval₂ f x = 0) :
(p * q).eval₂ f x = 0 :=
begin
rw eval₂_mul f x,
exact mul_eq_zero_of_left hp (q.eval₂ f x)
end
lemma eval₂_mul_eq_zero_of_right (p : polynomial R) (hq : q.eval₂ f x = 0) :
(p * q).eval₂ f x = 0 :=
begin
rw eval₂_mul f x,
exact mul_eq_zero_of_right (p.eval₂ f x) hq
end
instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f x) :=
⟨eval₂_zero _ _, eval₂_one _ _, λ _ _, eval₂_add _ _, λ _ _, eval₂_mul _ _⟩
/-- `eval₂` as a `ring_hom` -/
def eval₂_ring_hom (f : R →+* S) (x) : polynomial R →+* S :=
ring_hom.of (eval₂ f x)
@[simp] lemma coe_eval₂_ring_hom (f : R →+* S) (x) : ⇑(eval₂_ring_hom f x) = eval₂ f x := rfl
lemma eval₂_pow (n : ℕ) : (p ^ n).eval₂ f x = p.eval₂ f x ^ n := (eval₂_ring_hom _ _).map_pow _ _
lemma eval₂_eq_sum_range :
p.eval₂ f x = ∑ i in finset.range (p.nat_degree + 1), f (p.coeff i) * x^i :=
trans (congr_arg _ p.as_sum_range) (trans (eval₂_finset_sum f _ _ x) (congr_arg _ (by simp)))
lemma eval₂_eq_sum_range' (f : R →+* S) {p : polynomial R} {n : ℕ} (hn : p.nat_degree < n) (x : S) :
eval₂ f x p = ∑ i in finset.range n, f (p.coeff i) * x ^ i :=
begin
rw [eval₂_eq_sum, p.sum_over_range' _ _ hn],
intro i,
rw [f.map_zero, zero_mul]
end
end eval₂
section eval
variables {x : R}
/-- `eval x p` is the evaluation of the polynomial `p` at `x` -/
def eval : R → polynomial R → R := eval₂ (ring_hom.id _)
lemma eval_eq_sum : p.eval x = sum p (λ e a, a * x ^ e) :=
rfl
lemma eval_eq_finset_sum (P : polynomial R) (x : R) :
eval x P = ∑ i in range (P.nat_degree + 1), P.coeff i * x ^ i :=
begin
rw eval_eq_sum,
refine P.sum_of_support_subset _ _ _,
{ intros a,
rw [mem_range, nat.lt_add_one_iff],
exact le_nat_degree_of_mem_supp a },
{ intros,
exact zero_mul _ }
end
lemma eval_eq_finset_sum' (P : polynomial R) :
(λ x, eval x P) = (λ x, ∑ i in range (P.nat_degree + 1), P.coeff i * x ^ i) :=
begin
ext,
exact P.eval_eq_finset_sum x
end
@[simp] lemma eval₂_at_apply {S : Type*} [semiring S] (f : R →+* S) (r : R) :
p.eval₂ f (f r) = f (p.eval r) :=
begin
rw [eval₂_eq_sum, eval_eq_sum, finsupp.sum, finsupp.sum, f.map_sum],
simp only [f.map_mul, f.map_pow],
end
@[simp] lemma eval₂_at_one {S : Type*} [semiring S] (f : R →+* S) : p.eval₂ f 1 = f (p.eval 1) :=
begin
convert eval₂_at_apply f 1,
simp,
end
@[simp] lemma eval₂_at_nat_cast {S : Type*} [semiring S] (f : R →+* S) (n : ℕ) :
p.eval₂ f n = f (p.eval n) :=
begin
convert eval₂_at_apply f n,
simp,
end
@[simp] lemma eval_C : (C a).eval x = a := eval₂_C _ _
@[simp] lemma eval_nat_cast {n : ℕ} : (n : polynomial R).eval x = n :=
by simp only [←C_eq_nat_cast, eval_C]
@[simp] lemma eval_X : X.eval x = x := eval₂_X _ _
@[simp] lemma eval_monomial {n a} : (monomial n a).eval x = a * x^n :=
eval₂_monomial _ _
@[simp] lemma eval_zero : (0 : polynomial R).eval x = 0 := eval₂_zero _ _
@[simp] lemma eval_add : (p + q).eval x = p.eval x + q.eval x := eval₂_add _ _
@[simp] lemma eval_one : (1 : polynomial R).eval x = 1 := eval₂_one _ _
@[simp] lemma eval_bit0 : (bit0 p).eval x = bit0 (p.eval x) := eval₂_bit0 _ _
@[simp] lemma eval_bit1 : (bit1 p).eval x = bit1 (p.eval x) := eval₂_bit1 _ _
@[simp] lemma eval_smul (p : polynomial R) (x : R) {s : R} :
(s • p).eval x = s * p.eval x :=
eval₂_smul (ring_hom.id _) _ _
@[simp] lemma eval_C_mul : (C a * p).eval x = a * p.eval x :=
begin
apply polynomial.induction_on' p,
{ intros p q ph qh,
simp only [mul_add, eval_add, ph, qh], },
{ intros n b,
simp [mul_assoc], }
end
@[simp] lemma eval_nat_cast_mul {n : ℕ} : ((n : polynomial R) * p).eval x = n * p.eval x :=
by rw [←C_eq_nat_cast, eval_C_mul]
@[simp] lemma eval_mul_X : (p * X).eval x = p.eval x * x :=
begin
apply polynomial.induction_on' p,
{ intros p q ph qh,
simp only [add_mul, eval_add, ph, qh], },
{ intros n a,
simp only [←monomial_one_one_eq_X, monomial_mul_monomial, eval_monomial,
mul_one, pow_succ', mul_assoc], }
end
@[simp] lemma eval_mul_X_pow {k : ℕ} : (p * X^k).eval x = p.eval x * x^k :=
begin
induction k with k ih,
{ simp, },
{ simp [pow_succ', ←mul_assoc, ih], }
end
lemma eval_sum (p : polynomial R) (f : ℕ → R → polynomial R) (x : R) :
(p.sum f).eval x = p.sum (λ n a, (f n a).eval x) :=
eval₂_sum _ _ _ _
lemma eval_finset_sum (s : finset ι) (g : ι → polynomial R) (x : R) :
(∑ i in s, g i).eval x = ∑ i in s, (g i).eval x := eval₂_finset_sum _ _ _ _
/-- `is_root p x` implies `x` is a root of `p`. The evaluation of `p` at `x` is zero -/
def is_root (p : polynomial R) (a : R) : Prop := p.eval a = 0
instance [decidable_eq R] : decidable (is_root p a) := by unfold is_root; apply_instance
@[simp] lemma is_root.def : is_root p a ↔ p.eval a = 0 := iff.rfl
lemma coeff_zero_eq_eval_zero (p : polynomial R) :
coeff p 0 = p.eval 0 :=
calc coeff p 0 = coeff p 0 * 0 ^ 0 : by simp
... = p.eval 0 : eq.symm $
finset.sum_eq_single _ (λ b _ hb, by simp [zero_pow (nat.pos_of_ne_zero hb)]) (by simp)
lemma zero_is_root_of_coeff_zero_eq_zero {p : polynomial R} (hp : p.coeff 0 = 0) :
is_root p 0 :=
by rwa coeff_zero_eq_eval_zero at hp
end eval
section comp
/-- The composition of polynomials as a polynomial. -/
def comp (p q : polynomial R) : polynomial R := p.eval₂ C q
lemma comp_eq_sum_left : p.comp q = p.sum (λ e a, C a * q ^ e) :=
rfl
@[simp] lemma comp_X : p.comp X = p :=
begin
simp only [comp, eval₂, ← single_eq_C_mul_X],
exact finsupp.sum_single _,
end
@[simp] lemma X_comp : X.comp p = p := eval₂_X _ _
@[simp] lemma comp_C : p.comp (C a) = C (p.eval a) :=
begin
dsimp [comp, eval₂, eval, sum_def],
rw [← p.support.sum_hom (@C R _)],
apply finset.sum_congr rfl; simp
end
@[simp] lemma C_comp : (C a).comp p = C a := eval₂_C _ _
@[simp] lemma nat_cast_comp {n : ℕ} : (n : polynomial R).comp p = n :=
by rw [←C_eq_nat_cast, C_comp]
@[simp] lemma comp_zero : p.comp (0 : polynomial R) = C (p.eval 0) :=
by rw [← C_0, comp_C]
@[simp] lemma zero_comp : comp (0 : polynomial R) p = 0 :=
by rw [← C_0, C_comp]
@[simp] lemma comp_one : p.comp 1 = C (p.eval 1) :=
by rw [← C_1, comp_C]
@[simp] lemma one_comp : comp (1 : polynomial R) p = 1 :=
by rw [← C_1, C_comp]
@[simp] lemma add_comp : (p + q).comp r = p.comp r + q.comp r := eval₂_add _ _
@[simp] lemma monomial_comp (n : ℕ) : (monomial n a).comp p = C a * p^n :=
eval₂_monomial _ _
@[simp] lemma mul_X_comp : (p * X).comp r = p.comp r * r :=
begin
apply polynomial.induction_on' p,
{ intros p q hp hq, simp [hp, hq, add_mul], },
{ intros n b, simp [pow_succ', mul_assoc], }
end
@[simp] lemma X_pow_comp {k : ℕ} : (X^k).comp p = p^k :=
begin
induction k with k ih,
{ simp, },
{ simp [pow_succ', mul_X_comp, ih], },
end
@[simp] lemma mul_X_pow_comp {k : ℕ} : (p * X^k).comp r = p.comp r * r^k :=
begin
induction k with k ih,
{ simp, },
{ simp [ih, pow_succ', ←mul_assoc, mul_X_comp], },
end
@[simp] lemma C_mul_comp : (C a * p).comp r = C a * p.comp r :=
begin
apply polynomial.induction_on' p,
{ intros p q hp hq, simp [hp, hq, mul_add], },
{ intros n b, simp [mul_assoc], }
end
@[simp] lemma nat_cast_mul_comp {n : ℕ} : ((n : polynomial R) * p).comp r = n * p.comp r :=
by rw [←C_eq_nat_cast, C_mul_comp, C_eq_nat_cast]
@[simp] lemma mul_comp {R : Type*} [comm_semiring R] (p q r : polynomial R) :
(p * q).comp r = p.comp r * q.comp r := eval₂_mul _ _
lemma prod_comp {R : Type*} [comm_semiring R] (s : multiset (polynomial R)) (p : polynomial R) :
s.prod.comp p = (s.map (λ q : polynomial R, q.comp p)).prod :=
(s.prod_hom (monoid_hom.mk (λ q : polynomial R, q.comp p) one_comp (λ q r, mul_comp q r p))).symm
@[simp] lemma pow_comp {R : Type*} [comm_semiring R] (p q : polynomial R) (n : ℕ) :
(p^n).comp q = (p.comp q)^n :=
((monoid_hom.mk (λ r : polynomial R, r.comp q)) one_comp (λ r s, mul_comp r s q)).map_pow p n
@[simp] lemma bit0_comp : comp (bit0 p : polynomial R) q = bit0 (p.comp q) :=
by simp only [bit0, add_comp]
@[simp] lemma bit1_comp : comp (bit1 p : polynomial R) q = bit1 (p.comp q) :=
by simp only [bit1, add_comp, bit0_comp, one_comp]
lemma comp_assoc {R : Type*} [comm_semiring R] (φ ψ χ : polynomial R) :
(φ.comp ψ).comp χ = φ.comp (ψ.comp χ) :=
begin
apply polynomial.induction_on φ;
{ intros, simp only [add_comp, mul_comp, C_comp, X_comp, pow_succ', ← mul_assoc, *] at * }
end
end comp
section map
variables [semiring S]
variables (f : R →+* S)
/-- `map f p` maps a polynomial `p` across a ring hom `f` -/
def map : polynomial R → polynomial S := eval₂ (C.comp f) X
instance is_semiring_hom_C_f : is_semiring_hom (C ∘ f) :=
is_semiring_hom.comp _ _
@[simp] lemma map_C : (C a).map f = C (f a) := eval₂_C _ _
@[simp] lemma map_X : X.map f = X := eval₂_X _ _
@[simp] lemma map_monomial {n a} : (monomial n a).map f = monomial n (f a) :=
begin
dsimp only [map],
rw [eval₂_monomial, single_eq_C_mul_X], refl,
end
@[simp] lemma map_zero : (0 : polynomial R).map f = 0 := eval₂_zero _ _
@[simp] lemma map_add : (p + q).map f = p.map f + q.map f := eval₂_add _ _
@[simp] lemma map_one : (1 : polynomial R).map f = 1 := eval₂_one _ _
@[simp] theorem map_nat_cast (n : ℕ) : (n : polynomial R).map f = n :=
nat.rec_on n rfl $ λ n ih, by rw [n.cast_succ, map_add, ih, map_one, n.cast_succ]
@[simp]
lemma coeff_map (n : ℕ) : coeff (p.map f) n = f (coeff p n) :=
begin
rw [map, eval₂, coeff_sum, sum_def],
conv_rhs { rw [← sum_C_mul_X_eq p, coeff_sum, sum_def,
← p.support.sum_hom f], },
refine finset.sum_congr rfl (λ x hx, _),
simp [function.comp, coeff_C_mul_X, f.map_mul],
split_ifs; simp [is_semiring_hom.map_zero f],
end
lemma map_map [semiring T] (g : S →+* T)
(p : polynomial R) : (p.map f).map g = p.map (g.comp f) :=
ext (by simp [coeff_map])
@[simp] lemma map_id : p.map (ring_hom.id _) = p := by simp [polynomial.ext_iff, coeff_map]
lemma eval₂_eq_eval_map {x : S} : p.eval₂ f x = (p.map f).eval x :=
begin
apply polynomial.induction_on' p,
{ intros p q hp hq, simp [hp, hq], },
{ intros n r, simp, }
end
lemma map_injective (hf : function.injective f) : function.injective (map f) :=
λ p q h, ext $ λ m, hf $ by rw [← coeff_map f, ← coeff_map f, h]
lemma map_surjective (hf : function.surjective f) : function.surjective (map f) :=
λ p, polynomial.induction_on' p
(λ p q hp hq, let ⟨p', hp'⟩ := hp, ⟨q', hq'⟩ := hq in ⟨p' + q', by rw [map_add f, hp', hq']⟩)
(λ n s, let ⟨r, hr⟩ := hf s in ⟨monomial n r, by rw [map_monomial f, hr]⟩)
variables {f}
lemma map_monic_eq_zero_iff (hp : p.monic) : p.map f = 0 ↔ ∀ x, f x = 0 :=
⟨ λ hfp x, calc f x = f x * f p.leading_coeff : by simp [hp]
... = f x * (p.map f).coeff p.nat_degree : by { congr, apply (coeff_map _ _).symm }
... = 0 : by simp [hfp],
λ h, ext (λ n, trans (coeff_map f n) (h _)) ⟩
lemma map_monic_ne_zero (hp : p.monic) [nontrivial S] : p.map f ≠ 0 :=
λ h, f.map_one_ne_zero ((map_monic_eq_zero_iff hp).mp h _)
variables (f)
open is_semiring_hom
-- If the rings were commutative, we could prove this just using `eval₂_mul`.
-- TODO this proof is just a hack job on the proof of `eval₂_mul`,
-- using that `X` is central. It should probably be golfed!
@[simp] lemma map_mul : (p * q).map f = p.map f * q.map f :=
begin
dunfold map,
dunfold eval₂,
rw [add_monoid_algebra.mul_def, finsupp.sum_mul _ p], simp only [finsupp.mul_sum _ q],
rw [sum_sum_index],
{ apply sum_congr rfl, assume i hi, dsimp only, rw [sum_sum_index],
{ apply sum_congr rfl, assume j hj, dsimp only,
rw [sum_single_index, (C.comp f).map_mul, pow_add],
{ simp [←mul_assoc], conv_lhs { rw ←@X_pow_mul_assoc _ _ _ _ i }, },
{ simp, } },
{ intro, simp, },
{ intros, simp [add_mul], } },
{ intro, simp, },
{ intros, simp [add_mul], }
end
instance map.is_semiring_hom : is_semiring_hom (map f) :=
{ map_zero := eval₂_zero _ _,
map_one := eval₂_one _ _,
map_add := λ _ _, eval₂_add _ _,
map_mul := λ _ _, map_mul f, }
/-- `polynomial.map` as a `ring_hom` -/
def map_ring_hom (f : R →+* S) : polynomial R →+* polynomial S :=
{ to_fun := polynomial.map f,
map_add' := λ _ _, eval₂_add _ _,
map_zero' := eval₂_zero _ _,
map_mul' := λ _ _, map_mul f,
map_one' := eval₂_one _ _ }
@[simp] lemma coe_map_ring_hom (f : R →+* S) : ⇑(map_ring_hom f) = map f := rfl
lemma map_list_prod (L : list (polynomial R)) : L.prod.map f = (L.map $ map f).prod :=
eq.symm $ list.prod_hom _ (monoid_hom.of (map f))
@[simp] lemma map_pow (n : ℕ) : (p ^ n).map f = p.map f ^ n := is_monoid_hom.map_pow (map f) _ _
lemma mem_map_range {p : polynomial S} :
p ∈ set.range (map f) ↔ ∀ n, p.coeff n ∈ (set.range f) :=
begin
split,
{ rintro ⟨p, rfl⟩ n, rw coeff_map, exact set.mem_range_self _ },
{ intro h, rw p.as_sum_range_C_mul_X_pow,
apply is_add_submonoid.finset_sum_mem,
intros i hi,
rcases h i with ⟨c, hc⟩,
use [C c * X^i],
rw [map_mul, map_C, hc, map_pow, map_X] }
end
lemma eval₂_map [semiring T] (g : S →+* T) (x : T) :
(p.map f).eval₂ g x = p.eval₂ (g.comp f) x :=
begin
convert finsupp.sum_map_range_index _,
{ change map f p = map_range f _ p,
ext,
rw map_range_apply,
exact coeff_map f a, },
{ exact f.map_zero, },
{ intro a, simp only [ring_hom.map_zero, zero_mul], },
end
lemma eval_map (x : S) : (p.map f).eval x = p.eval₂ f x :=
eval₂_map f (ring_hom.id _) x
lemma map_sum {ι : Type*} (g : ι → polynomial R) (s : finset ι) :
(∑ i in s, g i).map f = ∑ i in s, (g i).map f :=
eq.symm $ sum_hom _ _
lemma map_comp (p q : polynomial R) : map f (p.comp q) = (map f p).comp (map f q) :=
polynomial.induction_on p
(by simp)
(by simp {contextual := tt})
(by simp [pow_succ', ← mul_assoc, polynomial.comp] {contextual := tt})
@[simp]
lemma eval_zero_map (f : R →+* S) (p : polynomial R) :
(p.map f).eval 0 = f (p.eval 0) :=
by simp [←coeff_zero_eq_eval_zero]
@[simp]
lemma eval_one_map (f : R →+* S) (p : polynomial R) :
(p.map f).eval 1 = f (p.eval 1) :=
begin
apply polynomial.induction_on' p,
{ intros p q hp hq, simp [hp, hq], },
{ intros n r, simp, }
end
@[simp]
lemma eval_nat_cast_map (f : R →+* S) (p : polynomial R) (n : ℕ) :
(p.map f).eval n = f (p.eval n) :=
begin
apply polynomial.induction_on' p,
{ intros p q hp hq, simp [hp, hq], },
{ intros n r, simp, }
end
@[simp]
lemma eval_int_cast_map {R S : Type*} [ring R] [ring S]
(f : R →+* S) (p : polynomial R) (i : ℤ) :
(p.map f).eval i = f (p.eval i) :=
begin
apply polynomial.induction_on' p,
{ intros p q hp hq, simp [hp, hq], },
{ intros n r, simp, }
end
end map
/-!
After having set up the basic theory of `eval₂`, `eval`, `comp`, and `map`,
we make `eval₂` irreducible.
Perhaps we can make the others irreducible too?
-/
attribute [irreducible] polynomial.eval₂
section hom_eval₂
-- TODO: Here we need commutativity in both `S` and `T`?
variables [comm_semiring S] [comm_semiring T]
variables (f : R →+* S) (g : S →+* T) (p)
lemma hom_eval₂ (x : S) : g (p.eval₂ f x) = p.eval₂ (g.comp f) (g x) :=
begin
apply polynomial.induction_on p; clear p,
{ intros a, rw [eval₂_C, eval₂_C], refl, },
{ intros p q hp hq, simp only [hp, hq, eval₂_add, g.map_add] },
{ intros n a ih,
simp only [eval₂_mul, eval₂_C, eval₂_X_pow, g.map_mul, g.map_pow],
refl, }
end
end hom_eval₂
end semiring
section comm_semiring
section eval
variables [comm_semiring R] {p q : polynomial R} {x : R}
lemma eval₂_comp [comm_semiring S] (f : R →+* S) {x : S} :
eval₂ f x (p.comp q) = eval₂ f (eval₂ f x q) p :=
by rw [comp, p.as_sum_range]; simp [eval₂_finset_sum, eval₂_pow]
@[simp] lemma eval_mul : (p * q).eval x = p.eval x * q.eval x := eval₂_mul _ _
instance eval.is_semiring_hom : is_semiring_hom (eval x) := eval₂.is_semiring_hom _ _
@[simp] lemma eval_pow (n : ℕ) : (p ^ n).eval x = p.eval x ^ n := eval₂_pow _ _ _
@[simp]
lemma eval_comp : (p.comp q).eval x = p.eval (q.eval x) :=
begin
apply polynomial.induction_on' p,
{ intros r s hr hs, simp [add_comp, hr, hs], },
{ intros n a, simp, }
end
instance comp.is_semiring_hom : is_semiring_hom (λ q : polynomial R, q.comp p) :=
by unfold comp; apply_instance
lemma eval₂_hom [comm_semiring S] (f : R →+* S) (x : R) :
p.eval₂ f (f x) = f (p.eval x) :=
(ring_hom.comp_id f) ▸ (hom_eval₂ p (ring_hom.id R) f x).symm
lemma root_mul_left_of_is_root (p : polynomial R) {q : polynomial R} :
is_root q a → is_root (p * q) a :=
λ H, by rw [is_root, eval_mul, is_root.def.1 H, mul_zero]
lemma root_mul_right_of_is_root {p : polynomial R} (q : polynomial R) :
is_root p a → is_root (p * q) a :=
λ H, by rw [is_root, eval_mul, is_root.def.1 H, zero_mul]
/--
Polynomial evaluation commutes with finset.prod
-/
lemma eval_prod {ι : Type*} (s : finset ι) (p : ι → polynomial R) (x : R) :
eval x (∏ j in s, p j) = ∏ j in s, eval x (p j) :=
begin
classical,
apply finset.induction_on s,
{ simp only [finset.prod_empty, eval_one] },
{ intros j s hj hpj,
have h0 : ∏ i in insert j s, eval x (p i) = (eval x (p j)) * ∏ i in s, eval x (p i),
{ apply finset.prod_insert hj },
rw [h0, ← hpj, finset.prod_insert hj, eval_mul] },
end
end eval
section map
variables [comm_semiring R] [comm_semiring S] (f : R →+* S)
lemma map_multiset_prod (m : multiset (polynomial R)) : m.prod.map f = (m.map $ map f).prod :=
eq.symm $ multiset.prod_hom _ (monoid_hom.of (map f))
lemma map_prod {ι : Type*} (g : ι → polynomial R) (s : finset ι) :
(∏ i in s, g i).map f = ∏ i in s, (g i).map f :=
eq.symm $ prod_hom _ _
lemma support_map_subset (p : polynomial R) : (map f p).support ⊆ p.support :=
begin
intros x,
simp only [mem_support_iff],
contrapose!,
change p.coeff x = 0 → (map f p).coeff x = 0,
rw coeff_map,
intro hx,
rw hx,
exact ring_hom.map_zero f,
end
end map
end comm_semiring
section ring
variables [ring R] {p q r : polynomial R}
lemma C_neg : C (-a) = -C a := ring_hom.map_neg C a
lemma C_sub : C (a - b) = C a - C b := ring_hom.map_sub C a b
instance map.is_ring_hom {S} [ring S] (f : R →+* S) : is_ring_hom (map f) :=
by apply is_ring_hom.of_semiring
@[simp] lemma map_sub {S} [ring S] (f : R →+* S) :
(p - q).map f = p.map f - q.map f :=
is_ring_hom.map_sub _
@[simp] lemma map_neg {S} [ring S] (f : R →+* S) :
(-p).map f = -(p.map f) :=
is_ring_hom.map_neg _
@[simp] lemma map_int_cast {S} [ring S] (f : R →+* S) (n : ℤ) :
map f ↑n = ↑n :=
(ring_hom.of (map f)).map_int_cast n
@[simp] lemma eval_int_cast {n : ℤ} {x : R} : (n : polynomial R).eval x = n :=
by simp only [←C_eq_int_cast, eval_C]
@[simp] lemma eval₂_neg {S} [ring S] (f : R →+* S) {x : S} :
(-p).eval₂ f x = -p.eval₂ f x :=
by rw [eq_neg_iff_add_eq_zero, ←eval₂_add, add_left_neg, eval₂_zero]
@[simp] lemma eval₂_sub {S} [ring S] (f : R →+* S) {x : S} :
(p - q).eval₂ f x = p.eval₂ f x - q.eval₂ f x :=
by rw [sub_eq_add_neg, eval₂_add, eval₂_neg, sub_eq_add_neg]
@[simp] lemma eval_neg (p : polynomial R) (x : R) : (-p).eval x = -p.eval x :=
eval₂_neg _
@[simp] lemma eval_sub (p q : polynomial R) (x : R) : (p - q).eval x = p.eval x - q.eval x :=
eval₂_sub _
lemma root_X_sub_C : is_root (X - C a) b ↔ a = b :=
by rw [is_root.def, eval_sub, eval_X, eval_C, sub_eq_zero_iff_eq, eq_comm]
@[simp] lemma neg_comp : (-p).comp q = -p.comp q := eval₂_neg _
@[simp] lemma sub_comp : (p - q).comp r = p.comp r - q.comp r := eval₂_sub _
@[simp] lemma cast_int_comp (i : ℤ) : comp (i : polynomial R) p = i :=
by cases i; simp
end ring
section comm_ring
variables [comm_ring R] {p q : polynomial R}
instance eval₂.is_ring_hom {S} [comm_ring S]
(f : R →+* S) {x : S} : is_ring_hom (eval₂ f x) :=
by apply is_ring_hom.of_semiring
instance eval.is_ring_hom {x : R} : is_ring_hom (eval x) := eval₂.is_ring_hom _
end comm_ring
end polynomial
|
1ee6edcda26ad98d995345eb1eb1c5268258f2bc | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/number_theory/number_field/norm.lean | 1699edeb6c4c3a79ad03aa3d5427ce473984fbb7 | [
"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,900 | lean | /-
Copyright (c) 2022 Riccardo Brasca. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Riccardo Brasca, Eric Rodriguez
-/
import number_theory.number_field.basic
import ring_theory.norm
/-!
# Norm in number fields
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
Given a finite extension of number fields, we define the norm morphism as a function between the
rings of integers.
## Main definitions
* `ring_of_integers.norm K` : `algebra.norm` as a morphism `(𝓞 L) →* (𝓞 K)`.
## Main results
* `algebra.dvd_norm` : if `L/K` is a finite Galois extension of fields, then, for all `(x : 𝓞 L)`
we have that `x ∣ algebra_map (𝓞 K) (𝓞 L) (norm K x)`.
-/
open_locale number_field big_operators
open finset number_field algebra finite_dimensional
namespace ring_of_integers
variables {L : Type*} (K : Type*) [field K] [field L] [algebra K L] [finite_dimensional K L]
/-- `algebra.norm` as a morphism betwen the rings of integers. -/
@[simps] noncomputable def norm [is_separable K L] : (𝓞 L) →* (𝓞 K) :=
((algebra.norm K).restrict (𝓞 L)).cod_restrict (𝓞 K) (λ x, is_integral_norm K x.2)
local attribute [instance] number_field.ring_of_integers_algebra
lemma coe_algebra_map_norm [is_separable K L] (x : 𝓞 L) :
(algebra_map (𝓞 K) (𝓞 L) (norm K x) : L) = algebra_map K L (algebra.norm K (x : L)) := rfl
lemma coe_norm_algebra_map [is_separable K L] (x : 𝓞 K) :
(norm K (algebra_map (𝓞 K) (𝓞 L) x) : K) = algebra.norm K (algebra_map K L x) := rfl
lemma norm_algebra_map [is_separable K L] (x : 𝓞 K) :
norm K (algebra_map (𝓞 K) (𝓞 L) x) = x ^ finrank K L :=
by rw [← subtype.coe_inj, ring_of_integers.coe_norm_algebra_map, algebra.norm_algebra_map,
subsemiring_class.coe_pow]
lemma is_unit_norm_of_is_galois [is_galois K L] {x : 𝓞 L} :
is_unit (norm K x) ↔ is_unit x :=
begin
classical,
refine ⟨λ hx, _, is_unit.map _⟩,
replace hx : is_unit (algebra_map (𝓞 K) (𝓞 L) $ norm K x) := hx.map (algebra_map (𝓞 K) $ 𝓞 L),
refine @is_unit_of_mul_is_unit_right (𝓞 L) _
⟨(univ \ { alg_equiv.refl }).prod (λ (σ : L ≃ₐ[K] L), σ x),
prod_mem (λ σ hσ, map_is_integral (σ : L →+* L).to_int_alg_hom x.2)⟩ _ _,
convert hx using 1,
ext,
push_cast,
convert_to (univ \ { alg_equiv.refl }).prod (λ (σ : L ≃ₐ[K] L), σ x) * (∏ (σ : L ≃ₐ[K] L) in
{alg_equiv.refl}, σ (x : L)) = _,
{ rw [prod_singleton, alg_equiv.coe_refl, id] },
{ rw [prod_sdiff $ subset_univ _, ←norm_eq_prod_automorphisms, coe_algebra_map_norm] }
end
/-- If `L/K` is a finite Galois extension of fields, then, for all `(x : 𝓞 L)` we have that
`x ∣ algebra_map (𝓞 K) (𝓞 L) (norm K x)`. -/
lemma dvd_norm [is_galois K L] (x : 𝓞 L) : x ∣ algebra_map (𝓞 K) (𝓞 L) (norm K x) :=
begin
classical,
have hint : (∏ (σ : L ≃ₐ[K] L) in univ.erase alg_equiv.refl, σ x) ∈ 𝓞 L :=
subalgebra.prod_mem _ (λ σ hσ, (mem_ring_of_integers _ _).2
(map_is_integral σ (ring_of_integers.is_integral_coe x))),
refine ⟨⟨_, hint⟩, subtype.ext _⟩,
rw [coe_algebra_map_norm K x, norm_eq_prod_automorphisms],
simp [← finset.mul_prod_erase _ _ (mem_univ alg_equiv.refl)]
end
variables (F : Type*) [field F] [algebra K F] [is_separable K F] [finite_dimensional K F]
lemma norm_norm [is_separable K L] [algebra F L] [is_separable F L] [finite_dimensional F L]
[is_scalar_tower K F L] (x : 𝓞 L) : norm K (norm F x) = norm K x :=
by rw [← subtype.coe_inj, norm_apply_coe, norm_apply_coe, norm_apply_coe, algebra.norm_norm]
variable {F}
lemma is_unit_norm [char_zero K] {x : 𝓞 F} :
is_unit (norm K x) ↔ is_unit x :=
begin
letI : algebra K (algebraic_closure K) := algebraic_closure.algebra K,
let L := normal_closure K F (algebraic_closure F),
haveI : finite_dimensional F L := finite_dimensional.right K F L,
haveI : is_alg_closure K (algebraic_closure F) :=
is_alg_closure.of_algebraic K F (algebraic_closure F) (algebra.is_algebraic_of_finite K F),
haveI : is_galois F L := is_galois.tower_top_of_is_galois K F L,
calc
is_unit (norm K x) ↔ is_unit ((norm K) x ^ finrank F L) :
(is_unit_pow_iff (pos_iff_ne_zero.mp finrank_pos)).symm
... ↔ is_unit (norm K (algebra_map (𝓞 F) (𝓞 L) x)) :
by rw [← norm_norm K F (algebra_map (𝓞 F) (𝓞 L) x), norm_algebra_map F _, map_pow]
... ↔ is_unit (algebra_map (𝓞 F) (𝓞 L) x) : is_unit_norm_of_is_galois K
... ↔ is_unit (norm F (algebra_map (𝓞 F) (𝓞 L) x)) : (is_unit_norm_of_is_galois F).symm
... ↔ is_unit (x ^ finrank F L) : (congr_arg is_unit (norm_algebra_map F _)).to_iff
... ↔ is_unit x : is_unit_pow_iff (pos_iff_ne_zero.mp finrank_pos),
end
end ring_of_integers
|
d09b869ff222c89897a39becfe04548d7dde5a3d | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/data/seq/seq.lean | 2b5c0eef3c2259b6e96158f138cfd9950bee9076 | [
"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 | 27,600 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
import tactic.basic
import data.list.basic data.stream data.lazy_list data.seq.computation logic.basic
universes u v w
/-
coinductive seq (α : Type u) : Type u
| nil : seq α
| cons : α → seq α → seq α
-/
/--
A stream `s : option α` is a sequence if `s.nth n = none` implies `s.nth (n + 1) = none`.
-/
def stream.is_seq {α : Type u} (s : stream (option α)) : Prop :=
∀ {n : ℕ}, s n = none → s (n + 1) = none
/-- `seq α` is the type of possibly infinite lists (referred here as sequences).
It is encoded as an infinite stream of options such that if `f n = none`, then
`f m = none` for all `m ≥ n`. -/
def seq (α : Type u) : Type u := { f : stream (option α) // f.is_seq }
/-- `seq1 α` is the type of nonempty sequences. -/
def seq1 (α) := α × seq α
namespace seq
variables {α : Type u} {β : Type v} {γ : Type w}
/-- The empty sequence -/
def nil : seq α := ⟨stream.const none, λn h, rfl⟩
/-- Prepend an element to a sequence -/
def cons (a : α) : seq α → seq α
| ⟨f, al⟩ := ⟨some a :: f, λn h, by {cases n with n, contradiction, exact al h}⟩
/-- Get the nth element of a sequence (if it exists) -/
def nth : seq α → ℕ → option α := subtype.val
/-- Functorial action of the functor `option (α × _)` -/
@[simp] def omap (f : β → γ) : option (α × β) → option (α × γ)
| none := none
| (some (a, b)) := some (a, f b)
/-- Get the first element of a sequence -/
def head (s : seq α) : option α := nth s 0
/-- Get the tail of a sequence (or `nil` if the sequence is `nil`) -/
def tail : seq α → seq α
| ⟨f, al⟩ := ⟨f.tail, λ n, al⟩
protected def mem (a : α) (s : seq α) := some a ∈ s.1
instance : has_mem α (seq α) :=
⟨seq.mem⟩
theorem le_stable (s : seq α) {m n} (h : m ≤ n) :
s.1 m = none → s.1 n = none :=
by {cases s with f al, induction h with n h IH, exacts [id, λ h2, al (IH h2)]}
/--
If `s.nth n = some aₙ` for some value `aₙ`, then there is also some value `aₘ` such
that `s.nth = some aₘ` for `m ≤ n`.
-/
lemma ge_stable (s : seq α) {aₙ : α} {n m : ℕ} (m_le_n : m ≤ n)
(s_nth_eq_some : s.nth n = some aₙ) :
∃ (aₘ : α), s.nth m = some aₘ :=
have s.nth n ≠ none, by simp [s_nth_eq_some],
have s.nth m ≠ none, from mt (s.le_stable m_le_n) this,
with_one.ne_one_iff_exists.elim_left this
theorem not_mem_nil (a : α) : a ∉ @nil α :=
λ ⟨n, (h : some a = none)⟩, by injection h
theorem mem_cons (a : α) : ∀ (s : seq α), a ∈ cons a s
| ⟨f, al⟩ := stream.mem_cons (some a) _
theorem mem_cons_of_mem (y : α) {a : α} : ∀ {s : seq α}, a ∈ s → a ∈ cons y s
| ⟨f, al⟩ := stream.mem_cons_of_mem (some y)
theorem eq_or_mem_of_mem_cons {a b : α} : ∀ {s : seq α}, a ∈ cons b s → a = b ∨ a ∈ s
| ⟨f, al⟩ h := (stream.eq_or_mem_of_mem_cons h).imp_left (λh, by injection h)
@[simp] theorem mem_cons_iff {a b : α} {s : seq α} : a ∈ cons b s ↔ a = b ∨ a ∈ s :=
⟨eq_or_mem_of_mem_cons, λo, by cases o with e m;
[{rw e, apply mem_cons}, exact mem_cons_of_mem _ m]⟩
/-- Destructor for a sequence, resulting in either `none` (for `nil`) or
`some (a, s)` (for `cons a s`). -/
def destruct (s : seq α) : option (seq1 α) :=
(λa', (a', s.tail)) <$> nth s 0
theorem destruct_eq_nil {s : seq α} : destruct s = none → s = nil :=
begin
dsimp [destruct],
induction f0 : nth s 0; intro h,
{ apply subtype.eq,
funext n,
induction n with n IH, exacts [f0, s.2 IH] },
{ contradiction }
end
theorem destruct_eq_cons {s : seq α} {a s'} : destruct s = some (a, s') → s = cons a s' :=
begin
dsimp [destruct],
induction f0 : nth s 0 with a'; intro h,
{ contradiction },
{ unfold functor.map at h,
cases s with f al,
injections with _ h1 h2,
rw ←h2, apply subtype.eq, dsimp [tail, cons],
rw h1 at f0, rw ←f0,
exact (stream.eta f).symm }
end
@[simp] theorem destruct_nil : destruct (nil : seq α) = none := rfl
@[simp] theorem destruct_cons (a : α) : ∀ s, destruct (cons a s) = some (a, s)
| ⟨f, al⟩ := begin
unfold cons destruct functor.map,
apply congr_arg (λ s, some (a, s)),
apply subtype.eq, dsimp [tail], rw [stream.tail_cons]
end
theorem head_eq_destruct (s : seq α) : head s = prod.fst <$> destruct s :=
by unfold destruct head; cases nth s 0; refl
@[simp] theorem head_nil : head (nil : seq α) = none := rfl
@[simp] theorem head_cons (a : α) (s) : head (cons a s) = some a :=
by rw [head_eq_destruct, destruct_cons]; refl
@[simp] theorem tail_nil : tail (nil : seq α) = nil := rfl
@[simp] theorem tail_cons (a : α) (s) : tail (cons a s) = s :=
by cases s with f al; apply subtype.eq; dsimp [tail, cons]; rw [stream.tail_cons]
def cases_on {C : seq α → Sort v} (s : seq α)
(h1 : C nil) (h2 : ∀ x s, C (cons x s)) : C s := begin
induction H : destruct s with v v,
{ rw destruct_eq_nil H, apply h1 },
{ cases v with a s', rw destruct_eq_cons H, apply h2 }
end
theorem mem_rec_on {C : seq α → Prop} {a s} (M : a ∈ s)
(h1 : ∀ b s', (a = b ∨ C s') → C (cons b s')) : C s :=
begin
cases M with k e, unfold stream.nth at e,
induction k with k IH generalizing s,
{ have TH : s = cons a (tail s),
{ apply destruct_eq_cons,
unfold destruct nth functor.map, rw ←e, refl },
rw TH, apply h1 _ _ (or.inl rfl) },
revert e, apply s.cases_on _ (λ b s', _); intro e,
{ injection e },
{ have h_eq : (cons b s').val (nat.succ k) = s'.val k, { cases s'; refl },
rw [h_eq] at e,
apply h1 _ _ (or.inr (IH e)) }
end
def corec.F (f : β → option (α × β)) : option β → option α × option β
| none := (none, none)
| (some b) := match f b with none := (none, none) | some (a, b') := (some a, some b') end
/-- Corecursor for `seq α` as a coinductive type. Iterates `f` to produce new elements
of the sequence until `none` is obtained. -/
def corec (f : β → option (α × β)) (b : β) : seq α :=
begin
refine ⟨stream.corec' (corec.F f) (some b), λn h, _⟩,
rw stream.corec'_eq,
change stream.corec' (corec.F f) (corec.F f (some b)).2 n = none,
revert h, generalize : some b = o, revert o,
induction n with n IH; intro o,
{ change (corec.F f o).1 = none → (corec.F f (corec.F f o).2).1 = none,
cases o with b; intro h, { refl },
dsimp [corec.F] at h, dsimp [corec.F],
cases f b with s, { refl },
{ cases s with a b', contradiction } },
{ rw [stream.corec'_eq (corec.F f) (corec.F f o).2,
stream.corec'_eq (corec.F f) o],
exact IH (corec.F f o).2 }
end
@[simp] theorem corec_eq (f : β → option (α × β)) (b : β) :
destruct (corec f b) = omap (corec f) (f b) :=
begin
dsimp [corec, destruct, nth],
change stream.corec' (corec.F f) (some b) 0 with (corec.F f (some b)).1,
unfold functor.map, dsimp [corec.F],
induction h : f b with s, { refl },
cases s with a b', dsimp [corec.F],
apply congr_arg (λ b', some (a, b')),
apply subtype.eq,
dsimp [corec, tail],
rw [stream.corec'_eq, stream.tail_cons],
dsimp [corec.F], rw h, refl
end
/-- Embed a list as a sequence -/
def of_list (l : list α) : seq α :=
⟨list.nth l, λn h, begin
induction l with a l IH generalizing n, refl,
dsimp [list.nth], cases n with n; dsimp [list.nth] at h,
{ contradiction },
{ apply IH _ h }
end⟩
instance coe_list : has_coe (list α) (seq α) := ⟨of_list⟩
section bisim
variable (R : seq α → seq α → Prop)
local infix ~ := R
def bisim_o : option (seq1 α) → option (seq1 α) → Prop
| none none := true
| (some (a, s)) (some (a', s')) := a = a' ∧ R s s'
| _ _ := false
attribute [simp] bisim_o
def is_bisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → bisim_o R (destruct s₁) (destruct s₂)
-- If two streams are bisimilar, then they are equal
theorem eq_of_bisim (bisim : is_bisimulation R) {s₁ s₂} (r : s₁ ~ s₂) : s₁ = s₂ :=
begin
apply subtype.eq,
apply stream.eq_of_bisim (λx y, ∃ s s' : seq α, s.1 = x ∧ s'.1 = y ∧ R s s'),
dsimp [stream.is_bisimulation],
intros t₁ t₂ e,
exact match t₁, t₂, e with ._, ._, ⟨s, s', rfl, rfl, r⟩ :=
suffices head s = head s' ∧ R (tail s) (tail s'), from
and.imp id (λr, ⟨tail s, tail s',
by cases s; refl, by cases s'; refl, r⟩) this,
begin
have := bisim r, revert r this,
apply cases_on s _ _; intros; apply cases_on s' _ _; intros; intros r this,
{ constructor, refl, assumption },
{ rw [destruct_nil, destruct_cons] at this,
exact false.elim this },
{ rw [destruct_nil, destruct_cons] at this,
exact false.elim this },
{ rw [destruct_cons, destruct_cons] at this,
rw [head_cons, head_cons, tail_cons, tail_cons],
cases this with h1 h2,
constructor, rw h1, exact h2 }
end
end,
exact ⟨s₁, s₂, rfl, rfl, r⟩
end
end bisim
theorem coinduction : ∀ {s₁ s₂ : seq α}, head s₁ = head s₂ →
(∀ (β : Type u) (fr : seq α → β),
fr s₁ = fr s₂ → fr (tail s₁) = fr (tail s₂)) → s₁ = s₂
| ⟨f₁, a₁⟩ ⟨f₂, a₂⟩ hh ht :=
subtype.eq (stream.coinduction hh (λ β fr, ht β (λs, fr s.1)))
theorem coinduction2 (s) (f g : seq α → seq β)
(H : ∀ s, bisim_o (λ (s1 s2 : seq β), ∃ (s : seq α), s1 = f s ∧ s2 = g s)
(destruct (f s)) (destruct (g s)))
: f s = g s :=
begin
refine eq_of_bisim (λ s1 s2, ∃ s, s1 = f s ∧ s2 = g s) _ ⟨s, rfl, rfl⟩,
intros s1 s2 h, rcases h with ⟨s, h1, h2⟩,
rw [h1, h2], apply H
end
/-- Embed an infinite stream as a sequence -/
def of_stream (s : stream α) : seq α :=
⟨s.map some, λn h, by contradiction⟩
instance coe_stream : has_coe (stream α) (seq α) := ⟨of_stream⟩
/-- Embed a `lazy_list α` as a sequence. Note that even though this
is non-meta, it will produce infinite sequences if used with
cyclic `lazy_list`s created by meta constructions. -/
def of_lazy_list : lazy_list α → seq α :=
corec (λl, match l with
| lazy_list.nil := none
| lazy_list.cons a l' := some (a, l' ())
end)
instance coe_lazy_list : has_coe (lazy_list α) (seq α) := ⟨of_lazy_list⟩
/-- Translate a sequence into a `lazy_list`. Since `lazy_list` and `list`
are isomorphic as non-meta types, this function is necessarily meta. -/
meta def to_lazy_list : seq α → lazy_list α | s :=
match destruct s with
| none := lazy_list.nil
| some (a, s') := lazy_list.cons a (to_lazy_list s')
end
/-- Translate a sequence to a list. This function will run forever if
run on an infinite sequence. -/
meta def force_to_list (s : seq α) : list α := (to_lazy_list s).to_list
/-- The sequence of natural numbers some 0, some 1, ... -/
def nats : seq ℕ := stream.nats
@[simp]
lemma nats_nth (n : ℕ) : nats.nth n = some n := rfl
/-- Append two sequences. If `s₁` is infinite, then `s₁ ++ s₂ = s₁`,
otherwise it puts `s₂` at the location of the `nil` in `s₁`. -/
def append (s₁ s₂ : seq α) : seq α :=
@corec α (seq α × seq α) (λ⟨s₁, s₂⟩,
match destruct s₁ with
| none := omap (λs₂, (nil, s₂)) (destruct s₂)
| some (a, s₁') := some (a, s₁', s₂)
end) (s₁, s₂)
/-- Map a function over a sequence. -/
def map (f : α → β) : seq α → seq β | ⟨s, al⟩ :=
⟨s.map (option.map f),
λn, begin
dsimp [stream.map, stream.nth],
induction e : s n; intro,
{ rw al e, assumption }, { contradiction }
end⟩
/-- Flatten a sequence of sequences. (It is required that the
sequences be nonempty to ensure productivity; in the case
of an infinite sequence of `nil`, the first element is never
generated.) -/
def join : seq (seq1 α) → seq α :=
corec (λS, match destruct S with
| none := none
| some ((a, s), S') := some (a, match destruct s with
| none := S'
| some s' := cons s' S'
end)
end)
/-- Remove the first `n` elements from the sequence. -/
def drop (s : seq α) : ℕ → seq α
| 0 := s
| (n+1) := tail (drop n)
attribute [simp] drop
/-- Take the first `n` elements of the sequence (producing a list) -/
def take : ℕ → seq α → list α
| 0 s := []
| (n+1) s := match destruct s with
| none := []
| some (x, r) := list.cons x (take n r)
end
/-- Split a sequence at `n`, producing a finite initial segment
and an infinite tail. -/
def split_at : ℕ → seq α → list α × seq α
| 0 s := ([], s)
| (n+1) s := match destruct s with
| none := ([], nil)
| some (x, s') := let (l, r) := split_at n s' in (list.cons x l, r)
end
section zip_with
/-- Combine two sequences with a function -/
def zip_with (f : α → β → γ) : seq α → seq β → seq γ
| ⟨f₁, a₁⟩ ⟨f₂, a₂⟩ := ⟨λn,
match f₁ n, f₂ n with
| some a, some b := some (f a b)
| _, _ := none
end,
λn, begin
induction h1 : f₁ n,
{ intro H, simp only [(a₁ h1)], refl },
induction h2 : f₂ n; dsimp [seq.zip_with._match_1]; intro H,
{ rw (a₂ h2), cases f₁ (n + 1); refl },
{ rw [h1, h2] at H, contradiction }
end⟩
variables {s : seq α} {s' : seq β} {n : ℕ}
lemma zip_with_nth_some {a : α} {b : β} (s_nth_eq_some : s.nth n = some a)
(s_nth_eq_some' : s'.nth n = some b) (f : α → β → γ) :
(zip_with f s s').nth n = some (f a b) :=
begin
cases s with st,
have : st n = some a, from s_nth_eq_some,
cases s' with st',
have : st' n = some b, from s_nth_eq_some',
simp only [zip_with, seq.nth, *]
end
lemma zip_with_nth_none (s_nth_eq_none : s.nth n = none) (f : α → β → γ) :
(zip_with f s s').nth n = none :=
begin
cases s with st,
have : st n = none, from s_nth_eq_none,
cases s' with st',
cases st'_nth_eq : st' n;
simp only [zip_with, seq.nth, *]
end
lemma zip_with_nth_none' (s'_nth_eq_none : s'.nth n = none) (f : α → β → γ) :
(zip_with f s s').nth n = none :=
begin
cases s' with st',
have : st' n = none, from s'_nth_eq_none,
cases s with st,
cases st_nth_eq : st n;
simp only [zip_with, seq.nth, *]
end
end zip_with
/-- Pair two sequences into a sequence of pairs -/
def zip : seq α → seq β → seq (α × β) := zip_with prod.mk
/-- Separate a sequence of pairs into two sequences -/
def unzip (s : seq (α × β)) : seq α × seq β := (map prod.fst s, map prod.snd s)
/-- Convert a sequence which is known to terminate into a list -/
def to_list (s : seq α) (h : ∃ n, ¬ (nth s n).is_some) : list α :=
take (nat.find h) s
/-- Convert a sequence which is known not to terminate into a stream -/
def to_stream (s : seq α) (h : ∀ n, (nth s n).is_some) : stream α :=
λn, option.get (h n)
/-- Convert a sequence into either a list or a stream depending on whether
it is finite or infinite. (Without decidability of the infiniteness predicate,
this is not constructively possible.) -/
def to_list_or_stream (s : seq α) [decidable (∃ n, ¬ (nth s n).is_some)] :
list α ⊕ stream α :=
if h : ∃ n, ¬ (nth s n).is_some
then sum.inl (to_list s h)
else sum.inr (to_stream s (λn, decidable.by_contradiction (λ hn, h ⟨n, hn⟩)))
@[simp] theorem nil_append (s : seq α) : append nil s = s :=
begin
apply coinduction2, intro s,
dsimp [append], rw [corec_eq],
dsimp [append], apply cases_on s _ _,
{ trivial },
{ intros x s,
rw [destruct_cons], dsimp,
exact ⟨rfl, s, rfl, rfl⟩ }
end
@[simp] theorem cons_append (a : α) (s t) : append (cons a s) t = cons a (append s t) :=
destruct_eq_cons $ begin
dsimp [append], rw [corec_eq],
dsimp [append], rw [destruct_cons],
dsimp [append], refl
end
@[simp] theorem append_nil (s : seq α) : append s nil = s :=
begin
apply coinduction2 s, intro s,
apply cases_on s _ _,
{ trivial },
{ intros x s,
rw [cons_append, destruct_cons, destruct_cons], dsimp,
exact ⟨rfl, s, rfl, rfl⟩ }
end
@[simp] theorem append_assoc (s t u : seq α) :
append (append s t) u = append s (append t u) :=
begin
apply eq_of_bisim (λs1 s2, ∃ s t u,
s1 = append (append s t) u ∧ s2 = append s (append t u)),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, t, u, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on t; simp,
{ apply cases_on u; simp,
{ intros x u, refine ⟨nil, nil, u, _, _⟩; simp } },
{ intros x t, refine ⟨nil, t, u, _, _⟩; simp } },
{ intros x s, exact ⟨s, t, u, rfl, rfl⟩ }
end end },
{ exact ⟨s, t, u, rfl, rfl⟩ }
end
@[simp] theorem map_nil (f : α → β) : map f nil = nil := rfl
@[simp] theorem map_cons (f : α → β) (a) : ∀ s, map f (cons a s) = cons (f a) (map f s)
| ⟨s, al⟩ := by apply subtype.eq; dsimp [cons, map]; rw stream.map_cons; refl
@[simp] theorem map_id : ∀ (s : seq α), map id s = s
| ⟨s, al⟩ := begin
apply subtype.eq; dsimp [map],
rw [option.map_id, stream.map_id]; refl
end
@[simp] theorem map_tail (f : α → β) : ∀ s, map f (tail s) = tail (map f s)
| ⟨s, al⟩ := by apply subtype.eq; dsimp [tail, map]; rw stream.map_tail; refl
theorem map_comp (f : α → β) (g : β → γ) : ∀ (s : seq α), map (g ∘ f) s = map g (map f s)
| ⟨s, al⟩ := begin
apply subtype.eq; dsimp [map],
rw stream.map_map,
apply congr_arg (λ f : _ → option γ, stream.map f s),
funext x, cases x with x; refl
end
@[simp] theorem map_append (f : α → β) (s t) : map f (append s t) = append (map f s) (map f t) :=
begin
apply eq_of_bisim (λs1 s2, ∃ s t,
s1 = map f (append s t) ∧ s2 = append (map f s) (map f t)) _ ⟨s, t, rfl, rfl⟩,
intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, t, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on t; simp,
{ intros x t, refine ⟨nil, t, _, _⟩; simp } },
{ intros x s, refine ⟨s, t, rfl, rfl⟩ }
end end
end
@[simp] theorem map_nth (f : α → β) : ∀ s n, nth (map f s) n = (nth s n).map f
| ⟨s, al⟩ n := rfl
instance : functor seq := {map := @map}
instance : is_lawful_functor seq :=
{ id_map := @map_id, comp_map := @map_comp }
@[simp] theorem join_nil : join nil = (nil : seq α) := destruct_eq_nil rfl
@[simp] theorem join_cons_nil (a : α) (S) :
join (cons (a, nil) S) = cons a (join S) :=
destruct_eq_cons $ by simp [join]
@[simp] theorem join_cons_cons (a b : α) (s S) :
join (cons (a, cons b s) S) = cons a (join (cons (b, s) S)) :=
destruct_eq_cons $ by simp [join]
@[simp] theorem join_cons (a : α) (s S) :
join (cons (a, s) S) = cons a (append s (join S)) :=
begin
apply eq_of_bisim (λs1 s2, s1 = s2 ∨
∃ a s S, s1 = join (cons (a, s) S) ∧
s2 = cons a (append s (join S))) _ (or.inr ⟨a, s, S, rfl, rfl⟩),
intros s1 s2 h,
exact match s1, s2, h with
| _, _, (or.inl $ eq.refl s) := begin
apply cases_on s, { trivial },
{ intros x s, rw [destruct_cons], exact ⟨rfl, or.inl rfl⟩ }
end
| ._, ._, (or.inr ⟨a, s, S, rfl, rfl⟩) := begin
apply cases_on s,
{ simp },
{ intros x s, simp, refine or.inr ⟨x, s, S, rfl, rfl⟩ }
end
end
end
@[simp] theorem join_append (S T : seq (seq1 α)) :
join (append S T) = append (join S) (join T) :=
begin
apply eq_of_bisim (λs1 s2, ∃ s S T,
s1 = append s (join (append S T)) ∧
s2 = append s (append (join S) (join T))),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, T, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on S; simp,
{ apply cases_on T, { simp },
{ intros s T, cases s with a s; simp,
refine ⟨s, nil, T, _, _⟩; simp } },
{ intros s S, cases s with a s; simp,
exact ⟨s, S, T, rfl, rfl⟩ } },
{ intros x s, exact ⟨s, S, T, rfl, rfl⟩ }
end end },
{ refine ⟨nil, S, T, _, _⟩; simp }
end
@[simp] theorem of_list_nil : of_list [] = (nil : seq α) := rfl
@[simp] theorem of_list_cons (a : α) (l) :
of_list (a :: l) = cons a (of_list l) :=
begin
apply subtype.eq, simp [of_list, cons],
funext n, cases n; simp [list.nth, stream.cons]
end
@[simp] theorem of_stream_cons (a : α) (s) :
of_stream (a :: s) = cons a (of_stream s) :=
by apply subtype.eq; simp [of_stream, cons]; rw stream.map_cons
@[simp] theorem of_list_append (l l' : list α) :
of_list (l ++ l') = append (of_list l) (of_list l') :=
by induction l; simp [*]
@[simp] theorem of_stream_append (l : list α) (s : stream α) :
of_stream (l ++ₛ s) = append (of_list l) (of_stream s) :=
by induction l; simp [*, stream.nil_append_stream, stream.cons_append_stream]
/-- Convert a sequence into a list, embedded in a computation to allow for
the possibility of infinite sequences (in which case the computation
never returns anything). -/
def to_list' {α} (s : seq α) : computation (list α) :=
@computation.corec (list α) (list α × seq α) (λ⟨l, s⟩,
match destruct s with
| none := sum.inl l.reverse
| some (a, s') := sum.inr (a::l, s')
end) ([], s)
theorem dropn_add (s : seq α) (m) : ∀ n, drop s (m + n) = drop (drop s m) n
| 0 := rfl
| (n+1) := congr_arg tail (dropn_add n)
theorem dropn_tail (s : seq α) (n) : drop (tail s) n = drop s (n + 1) :=
by rw add_comm; symmetry; apply dropn_add
theorem nth_tail : ∀ (s : seq α) n, nth (tail s) n = nth s (n + 1)
| ⟨f, al⟩ n := rfl
@[extensionality]
protected lemma ext (s s': seq α) (hyp : ∀ (n : ℕ), s.nth n = s'.nth n) : s = s' :=
begin
let ext := (λ (s s' : seq α), ∀ n, s.nth n = s'.nth n),
apply seq.eq_of_bisim ext _ hyp,
-- we have to show that ext is a bisimulation
clear hyp s s',
assume s s' (hyp : ext s s'),
unfold seq.destruct,
rw (hyp 0),
cases (s'.nth 0),
{ simp [seq.bisim_o] }, -- option.none
{ -- option.some
suffices : ext s.tail s'.tail, by simpa,
assume n,
simp only [seq.nth_tail _ n, (hyp $ n + 1)] }
end
@[simp] theorem head_dropn (s : seq α) (n) : head (drop s n) = nth s n :=
begin
induction n with n IH generalizing s, { refl },
rw [nat.succ_eq_add_one, ←nth_tail, ←dropn_tail], apply IH
end
theorem mem_map (f : α → β) {a : α} : ∀ {s : seq α}, a ∈ s → f a ∈ map f s
| ⟨g, al⟩ := stream.mem_map (option.map f)
theorem exists_of_mem_map {f} {b : β} : ∀ {s : seq α}, b ∈ map f s → ∃ a, a ∈ s ∧ f a = b
| ⟨g, al⟩ h := let ⟨o, om, oe⟩ := stream.exists_of_mem_map h in
by cases o with a; injection oe with h'; exact ⟨a, om, h'⟩
theorem of_mem_append {s₁ s₂ : seq α} {a : α} (h : a ∈ append s₁ s₂) : a ∈ s₁ ∨ a ∈ s₂ :=
begin
have := h, revert this,
generalize e : append s₁ s₂ = ss, intro h, revert s₁,
apply mem_rec_on h _,
intros b s' o s₁,
apply s₁.cases_on _ (λ c t₁, _); intros m e;
have := congr_arg destruct e,
{ apply or.inr, simpa using m },
{ cases (show a = c ∨ a ∈ append t₁ s₂, by simpa using m) with e' m,
{ rw e', exact or.inl (mem_cons _ _) },
{ cases (show c = b ∧ append t₁ s₂ = s', by simpa) with i1 i2,
cases o with e' IH,
{ simp [i1, e'] },
{ exact or.imp_left (mem_cons_of_mem _) (IH m i2) } } }
end
theorem mem_append_left {s₁ s₂ : seq α} {a : α} (h : a ∈ s₁) : a ∈ append s₁ s₂ :=
by apply mem_rec_on h; intros; simp [*]
end seq
namespace seq1
variables {α : Type u} {β : Type v} {γ : Type w}
open seq
/-- Convert a `seq1` to a sequence. -/
def to_seq : seq1 α → seq α
| (a, s) := cons a s
instance coe_seq : has_coe (seq1 α) (seq α) := ⟨to_seq⟩
/-- Map a function on a `seq1` -/
def map (f : α → β) : seq1 α → seq1 β
| (a, s) := (f a, seq.map f s)
theorem map_id : ∀ (s : seq1 α), map id s = s | ⟨a, s⟩ := by simp [map]
/-- Flatten a nonempty sequence of nonempty sequences -/
def join : seq1 (seq1 α) → seq1 α
| ((a, s), S) := match destruct s with
| none := (a, seq.join S)
| some s' := (a, seq.join (cons s' S))
end
@[simp] theorem join_nil (a : α) (S) : join ((a, nil), S) = (a, seq.join S) := rfl
@[simp] theorem join_cons (a b : α) (s S) :
join ((a, cons b s), S) = (a, seq.join (cons (b, s) S)) :=
by dsimp [join]; rw [destruct_cons]; refl
/-- The `return` operator for the `seq1` monad,
which produces a singleton sequence. -/
def ret (a : α) : seq1 α := (a, nil)
/-- The `bind` operator for the `seq1` monad,
which maps `f` on each element of `s` and appends the results together.
(Not all of `s` may be evaluated, because the first few elements of `s`
may already produce an infinite result.) -/
def bind (s : seq1 α) (f : α → seq1 β) : seq1 β :=
join (map f s)
@[simp] theorem join_map_ret (s : seq α) : seq.join (seq.map ret s) = s :=
by apply coinduction2 s; intro s; apply cases_on s; simp [ret]
@[simp] theorem bind_ret (f : α → β) : ∀ s, bind s (ret ∘ f) = map f s
| ⟨a, s⟩ := begin
dsimp [bind, map], change (λx, ret (f x)) with (ret ∘ f),
rw [map_comp], simp [function.comp, ret]
end
@[simp] theorem ret_bind (a : α) (f : α → seq1 β) : bind (ret a) f = f a :=
begin
simp [ret, bind, map],
cases f a with a s,
apply cases_on s; intros; simp
end
@[simp] theorem map_join' (f : α → β) (S) :
seq.map f (seq.join S) = seq.join (seq.map (map f) S) :=
begin
apply eq_of_bisim (λs1 s2,
∃ s S, s1 = append s (seq.map f (seq.join S)) ∧
s2 = append s (seq.join (seq.map (map f) S))),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on S; simp,
{ intros x S, cases x with a s; simp [map],
exact ⟨_, _, rfl, rfl⟩ } },
{ intros x s, refine ⟨s, S, rfl, rfl⟩ }
end end },
{ refine ⟨nil, S, _, _⟩; simp }
end
@[simp] theorem map_join (f : α → β) : ∀ S, map f (join S) = join (map (map f) S)
| ((a, s), S) := by apply cases_on s; intros; simp [map]
@[simp] theorem join_join (SS : seq (seq1 (seq1 α))) :
seq.join (seq.join SS) = seq.join (seq.map join SS) :=
begin
apply eq_of_bisim (λs1 s2,
∃ s SS, s1 = seq.append s (seq.join (seq.join SS)) ∧
s2 = seq.append s (seq.join (seq.map join SS))),
{ intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, SS, rfl, rfl⟩ := begin
apply cases_on s; simp,
{ apply cases_on SS; simp,
{ intros S SS, cases S with s S; cases s with x s; simp [map],
apply cases_on s; simp,
{ exact ⟨_, _, rfl, rfl⟩ },
{ intros x s,
refine ⟨cons x (append s (seq.join S)), SS, _, _⟩; simp } } },
{ intros x s, exact ⟨s, SS, rfl, rfl⟩ }
end end },
{ refine ⟨nil, SS, _, _⟩; simp }
end
@[simp] theorem bind_assoc (s : seq1 α) (f : α → seq1 β) (g : β → seq1 γ) :
bind (bind s f) g = bind s (λ (x : α), bind (f x) g) :=
begin
cases s with a s,
simp [bind, map],
rw [←map_comp],
change (λ x, join (map g (f x))) with (join ∘ ((map g) ∘ f)),
rw [map_comp _ join],
generalize : seq.map (map g ∘ f) s = SS,
rcases map g (f a) with ⟨⟨a, s⟩, S⟩,
apply cases_on s; intros; apply cases_on S; intros; simp,
{ cases x with x t, apply cases_on t; intros; simp },
{ cases x_1 with y t; simp }
end
instance : monad seq1 :=
{ map := @map,
pure := @ret,
bind := @bind }
instance : is_lawful_monad seq1 :=
{ id_map := @map_id,
bind_pure_comp_eq_map := @bind_ret,
pure_bind := @ret_bind,
bind_assoc := @bind_assoc }
end seq1
|
dc1a24a707e1c80be63859499b60be677fc06114 | 957a80ea22c5abb4f4670b250d55534d9db99108 | /tests/lean/run/1590.lean | 70edc8a5a327213ee91fcac1318b60eccac749f2 | [
"Apache-2.0"
] | permissive | GaloisInc/lean | aa1e64d604051e602fcf4610061314b9a37ab8cd | f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0 | refs/heads/master | 1,592,202,909,807 | 1,504,624,387,000 | 1,504,624,387,000 | 75,319,626 | 2 | 1 | Apache-2.0 | 1,539,290,164,000 | 1,480,616,104,000 | C++ | UTF-8 | Lean | false | false | 95 | lean | #check `(true.intro)
#check (`(true.intro) : expr)
#check λ (h : true) [reflected h], `(id h)
|
d8106430ad5ce39358bff8e59c87b9fd44bdb2c0 | 6df8d5ae3acf20ad0d7f0247d2cee1957ef96df1 | /ExamPractice/exam_1_studyshi.lean | 59643e8e82f1577463adcf778e6357a8193c8158 | [] | 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 | 5,419 | lean | /-
Styles of function writing
1) "C" style
2) tactic script
3) lambda abstraction
-/
def square : ℕ → ℕ
| n := n*n
def square' : ℕ → ℕ :=
λ n, n^2
#eval (λ (n : ℕ ), n^2) 3
def x := λ (n:ℕ ), n^2
#check x
/-
Must really know how to write lambda expressions
-/
def x' := λ (n : bool), tt
/-
Be able to produce recursive function definitions (in Lean) for simple
arithmetic functions. Know how to reason about and write appropriate base
and recursive cases for functions involving natural numbers. A base case
generally involves an argument value of nat.zero. A recursive case generally
occurs when an argument value is of the form (nat.succ n') for some n'. These
two cases correspond to the two constructors for the nat data type. You might
be tempted to name the argument in a recursive case n, and to recurse on the
expression "n minus 1" but this will not work in Lean, because Lean cannot then
prove to itself that the recursion terminates. The definition of the factorial
function (in Lean) is a good example to study.
-/
--recursive find fibonaci
open nat
def fib : ℕ → ℕ
|nat.zero := nat.zero
| (succ(nat.zero)):= (succ(nat.zero))
| (succ(succ n')) := fib(n') + fib(n' + 1)
def fact : ℕ → ℕ
| nat.zero := succ nat.zero
| (succ(nat.zero)):= (succ(nat.zero))
| (succ n') := fact (n') * (succ n')
#eval fact 4
/-
Understand key tradeoffs when defining functions using mathematical logic, and
using imperative programming languages, respectively
Answer: mathematical logic can be easier to understand and explain but imperative
programming allows for more efficient use of computational resources as
well as fewer lines of code
-/
/-
Understand key shortcomings of natural languages for expressing mathematical
(including specification and implementation) concepts.
Answer: Natural Language: The language that we all speak.
Not the language that machines operate using. There is too much
ambiguity in natural language. It is not checkable by machines.
Formal Language: ex Python, boolean algebra. Well defined
language with no ambiguity. Some formal languages are based
off of the structure of natural language(ex. cobol)
-/
/-
Understand precisely what it means for a program to be correct with respect to
(or "equivalent" to) a specification written in math logic.
-/
/-
Know Boolean function truth tables
-/
/-
Know how lists ("sequences") are inductively defined and be able to define
recursive functions that operate on lists
inductive nat : Type
| zero : nat
| succ : nat → nat
-/
open nat
inductive list_nat : Type
| nil
| cons : nat → list_nat → list_nat
open list_nat
--Recursive list function
def length_nat : list_nat → ℕ
| nil := 0
| (cons h t) := 1 + (length_nat t)
def append_mnat : list_nat → list_nat → list_nat
| nil h := h
| (cons h t) o := cons h (append_mnat t o)
/-
Know how to define polymorphic functions that work with values of type
(list alpha), where alpha is a type parameter. Understand implicit
arguments and how to use them in Lean function definitions.
def unbox' (α : Type) : boxed α → α
| (box v) := v
def unbox {α : Type} : boxed α → α
-- Use curly braces to indicate implicit arg
| (box v) := v
-/
/-
Know how to define simple enumerated types in Lean and functions
that operate on values of such types. The "day" type that we covered
is an example of such a type.
-/
inductive day : Type
| sun : day
| mon : day
| tue : day
| wed : day
| thu : day
| fri : day
| sat : day
inductive mybool : Type
| ttt
| fff
open day
def nextDay : day → day
| sun := mon
| mon := tue
| tue := wed
| wed := thu
| thu := fri
| fri := sat
| sat := sun
/-
Understand the higher-order "map" and "reduce" functions
that we've studied. The key characteristic of higher-order
functions is that they take function arguments, or return
functions as results, or both. Be able to implement and use
map and reduce functions.
-/
open list
def map_pred : (ℕ → bool) → list nat → (list bool)
| _ [] := []
| f (cons h t) :=
if (f h)
then (cons tt (map_pred f t))
else (cons ff (map_pred f t))
-- Answer here
def b_list := [0,1,2,5,1,0]
def reduce_and : list bool → bool
| [] := tt
| (cons ff t) := ff
| (cons tt t) := reduce_and t
/-
Understand how to specify and work with "product" types.
The "prod" type we defined is an example of a type whose
values are ordered *pairs* of values of other specified types.
Ans: A polymorphic "product type", prod. A product
type is a type whose values are ordered pairs.
Here the types of the first and second elements
of such a pair are given by the values of two
type arguments, α and β respectively. The pair
constructor takes a value of type α and one of
type β and yeilds the term (pair a b), whic we
will interpret as representing the ordered pair,
(a, b), a concept that should be familiar from
basic high school algebra.
-/
inductive prod2 (α β : Type) : Type
| pair (a : α) (b : β) : prod2
def fst {α β : Type} : prod2 α β → α
| (prod2.pair a b) := a
def snd {α β : Type} : prod2 α β → β
| (prod2.pair a b) := b
/-
Understand the combinatorics of binary values. E.g.,
there are 2^n possible values of a sequence of n
binary variables.
(2^(n))^(2^m) = number of posible functions with n
input bits and m output bits
-/ |
656adbd96ade306ec621780750238f5b4a825909 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/e5.lean | cfa8c6d692526b8fd90cc4357663a25f50f97f40 | [
"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,561 | lean | prelude
definition Prop := Type.{0}
definition false : Prop := ∀x : Prop, x
check false
theorem false.elim (C : Prop) (H : false) : C
:= H C
definition eq {A : Type} (a b : A)
:= ∀ P : A → Prop, P a → P b
check eq
infix `=`:50 := eq
theorem refl {A : Type} (a : A) : a = a
:= λ P H, H
definition true : Prop
:= false = false
theorem trivial : true
:= refl false
attribute [elab_as_eliminator]
theorem subst {A : Type} {P : A -> Prop} {a b : A} (H1 : a = b) (H2 : P a) : P b
:= H1 _ H2
theorem symm {A : Type} {a b : A} (H : a = b) : b = a
:= subst H (refl a)
theorem trans {A : Type} {a b c : A} (H1 : a = b) (H2 : b = c) : a = c
:= subst H2 H1
inductive nat : Type
| zero : nat
| succ : nat → nat
namespace nat end nat open nat
print "using strict implicit arguments"
definition symmetric {A : Type} (R : A → A → Prop) := ∀ ⦃a b⦄, R a b → R b a
check symmetric
constant p : nat → nat → Prop
check symmetric p
axiom H1 : symmetric p
axiom H2 : p zero (succ zero)
check H1
check H1 H2
print "------------"
print "using implicit arguments"
definition symmetric2 {A : Type} (R : A → A → Prop) := ∀ {a b}, R a b → R b a
check symmetric2
check symmetric2 p
axiom H3 : symmetric2 p
axiom H4 : p zero (succ zero)
check H3
check H3 H4
print "-----------------"
print "using strict implicit arguments (ASCII notation)"
definition symmetric3 {A : Type} (R : A → A → Prop) := ∀ {{a b}}, R a b → R b a
check symmetric3
check symmetric3 p
axiom H5 : symmetric3 p
axiom H6 : p zero (succ zero)
check H5
check H5 H6
|
83d01f12b1e7bdb13dd710121aebd678b9061f54 | 86f6f4f8d827a196a32bfc646234b73328aeb306 | /examples/basics/unnamed_1955.lean | e24ee24127b56246e8cd3bd0b6a502face94edfb | [] | no_license | jamescheuk91/mathematics_in_lean | 09f1f87d2b0dce53464ff0cbe592c568ff59cf5e | 4452499264e2975bca2f42565c0925506ba5dda3 | refs/heads/master | 1,679,716,410,967 | 1,613,957,947,000 | 1,613,957,947,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 334 | lean | import order.lattice
variables {α : Type*} [lattice α]
variables a b c : α
example (h : ∀ x y z : α, x ⊓ (y ⊔ z) = (x ⊓ y) ⊔ (x ⊓ z)) :
(a ⊔ b) ⊓ c = (a ⊓ c) ⊔ (b ⊓ c) :=
sorry
example (h : ∀ x y z : α, x ⊔ (y ⊓ z) = (x ⊔ y) ⊓ (x ⊔ z)) :
(a ⊓ b) ⊔ c = (a ⊔ c) ⊓ (b ⊔ c) :=
sorry |
3d2de193cefed25b4895cfd582a44e6d06ce6807 | dd4e652c749fea9ac77e404005cb3470e5f75469 | /src/eigenvectors/adjoint.lean | 16c125189eb65acf13e6893cbe0f7c68768f9bad | [] | no_license | skbaek/cvx | e32822ad5943541539966a37dee162b0a5495f55 | c50c790c9116f9fac8dfe742903a62bdd7292c15 | refs/heads/master | 1,623,803,010,339 | 1,618,058,958,000 | 1,618,058,958,000 | 176,293,135 | 3 | 2 | null | null | null | null | UTF-8 | Lean | false | false | 7,385 | lean | import analysis.normed_space.real_inner_product
import missing_mathlib.linear_algebra.basic
noncomputable theory
open_locale classical
-- TODO: Same for complex inner product?
-- Or for bilinear forms?
variables {V : Type*} [inner_product_space V]
namespace linear_map
def has_adjoint (F G : V →ₗ[ℝ] V) : Prop :=
∀ x y, inner (F x) y = inner x (G y)
lemma has_adjoint_def {F G : V →ₗ[ℝ] V} (hG : has_adjoint F G) :
∀ x y, inner (F x) y = inner x (G y) := hG
lemma has_adjoint_symm {F G : V →ₗ[ℝ] V} :
has_adjoint F G ↔ has_adjoint G F :=
begin
unfold has_adjoint,
split,
repeat
{ intros h x y,
rw inner_product_space.comm _ y,
rw inner_product_space.comm x _,
apply (h y x).symm }
end
lemma has_adjoint_unique_left {F F' G : V →ₗ[ℝ] V}
(hF : has_adjoint F G) (hF' : has_adjoint F' G) : F = F' :=
begin
ext,
rw ←sub_eq_zero,
apply inner_product_space.definite,
simp only [inner_sub_left, inner_sub_right],
simp [has_adjoint_def hF, has_adjoint_def hF']
end
lemma has_adjoint_unique_right {F G G' : V →ₗ[ℝ] V}
(hG : has_adjoint F G) (hG' : has_adjoint F G') : G = G' :=
begin
rw has_adjoint_symm at *,
apply has_adjoint_unique_left hG hG'
end
lemma has_adjoint_zero : has_adjoint (0 : V →ₗ[ℝ] V) 0 :=
by simp [has_adjoint, linear_map.zero_apply, inner_zero_left, inner_zero_right]
lemma has_adjoint_id : has_adjoint (id : V →ₗ[ℝ] V) id :=
by simp [has_adjoint, linear_map.id_apply]
lemma has_adjoint_smul {F G : V →ₗ[ℝ] V} (c : ℝ) :
has_adjoint F G → has_adjoint (c • F) (c • G) :=
by simp [has_adjoint, inner_smul_left, inner_smul_right] {contextual := tt}
lemma has_adjoint_neg {F G : V →ₗ[ℝ] V} :
has_adjoint F G → has_adjoint (- F) (- G) :=
begin
rw ← neg_one_smul _ F,
rw ← neg_one_smul _ G,
apply has_adjoint_smul (-1),
end
lemma has_adjoint_add {F G F' G' : V →ₗ[ℝ] V} :
has_adjoint F G → has_adjoint F' G' → has_adjoint (F + F') (G + G') :=
by simp [has_adjoint, inner_add_left, inner_add_right] {contextual := tt}
lemma has_adjoint_sub {F G F' G' : V →ₗ[ℝ] V} :
has_adjoint F G → has_adjoint F' G' → has_adjoint (F - F') (G - G') :=
by simp [has_adjoint, inner_add_left, inner_add_right] {contextual := tt}
def adjoint (F : V →ₗ[ℝ] V) : V →ₗ[ℝ] V :=
if h : ∃ G, has_adjoint F G then classical.some h else 0
lemma adjoint_of_has_adjoint {F G : V →ₗ[ℝ] V} (hF : has_adjoint F G) :
F.adjoint = G :=
begin
apply has_adjoint_unique_right _ hF,
rw [adjoint, dif_pos],
apply classical.some_spec ⟨G, hF⟩,
end
lemma has_adjoint_adjoint {F : V →ₗ[ℝ] V} (h : ∃ G, has_adjoint F G) :
has_adjoint F (adjoint F) :=
begin
rcases h with ⟨w, hw⟩,
rw adjoint_of_has_adjoint hw,
apply hw,
end
lemma inner_adjoint {F : V →ₗ[ℝ] V} (h : ∃ G, has_adjoint F G) :
∀ x y, inner (F x) y = inner x (F.adjoint y) :=
has_adjoint_def (has_adjoint_adjoint h)
lemma adjoint_adjoint {F : V →ₗ[ℝ] V} (h : ∃ G, has_adjoint F G) :
F.adjoint.adjoint = F :=
begin
rw adjoint_of_has_adjoint,
rw has_adjoint_symm,
apply has_adjoint_adjoint h,
end
lemma adjoint_zero : (0 : V →ₗ[ℝ] V).adjoint = 0 :=
adjoint_of_has_adjoint has_adjoint_zero
lemma adjoint_id : (id : V →ₗ[ℝ] V).adjoint = id :=
adjoint_of_has_adjoint has_adjoint_id
lemma adjoint_smul (c : ℝ) (F : V →ₗ[ℝ] V) (h : ∃ G, has_adjoint F G) :
(c • F).adjoint = c • F.adjoint :=
adjoint_of_has_adjoint (has_adjoint_smul c (has_adjoint_adjoint h))
lemma adjoint_neg (F : V →ₗ[ℝ] V) (h : ∃ G, has_adjoint F G) :
(- F).adjoint = - F.adjoint :=
adjoint_of_has_adjoint (has_adjoint_neg (has_adjoint_adjoint h))
lemma adjoint_add (F G : V →ₗ[ℝ] V)
(hF : ∃ F', has_adjoint F F') (hG : ∃ G', has_adjoint G G'):
(F + G).adjoint = F.adjoint + G.adjoint :=
adjoint_of_has_adjoint (has_adjoint_add (has_adjoint_adjoint hF) (has_adjoint_adjoint hG))
lemma adjoint_sub (F G : V →ₗ[ℝ] V)
(hF : ∃ F', has_adjoint F F') (hG : ∃ G', has_adjoint G G'):
(F - G).adjoint = F.adjoint - G.adjoint :=
adjoint_of_has_adjoint (has_adjoint_sub (has_adjoint_adjoint hF) (has_adjoint_adjoint hG))
def normal (F : V →ₗ[ℝ] V) : Prop :=
∃ G, has_adjoint F G ∧ F.comp G = G.comp F
lemma has_adjoint_of_normal {F : V →ₗ[ℝ] V} (h : normal F) :
∃ G, has_adjoint F G :=
begin
unfold normal at h,
rcases h with ⟨w, hw⟩,
use w,
apply hw.left,
end
lemma adjoint_comm_of_normal {F : V →ₗ[ℝ] V} (h : normal F) :
F.comp F.adjoint = F.adjoint.comp F :=
begin
unfold normal at h,
rcases h with ⟨w, hw⟩,
rw adjoint_of_has_adjoint hw.left,
apply hw.right,
end
lemma normal_adjoint_of_normal {F : V →ₗ[ℝ] V} (h : normal F) :
normal F.adjoint :=
begin
use F,
split,
{ rw has_adjoint_symm,
apply has_adjoint_adjoint (has_adjoint_of_normal h) },
{ rw adjoint_comm_of_normal h },
end
lemma normal_of_has_adjoint_self {F : V →ₗ[ℝ] V} (h : has_adjoint F F) :
normal F :=
⟨F, ⟨h, rfl⟩⟩
lemma normal_zero : normal (0 : V →ₗ[ℝ] V) :=
normal_of_has_adjoint_self has_adjoint_zero
lemma normal_id : normal (id : V →ₗ[ℝ] V) :=
normal_of_has_adjoint_self has_adjoint_id
lemma normal_smul_of_normal {F : V →ₗ[ℝ] V} (c : ℝ) (h : normal F) :
normal (c • F) :=
begin
rcases h with ⟨G, hG_adjoint, hG_comm⟩,
refine ⟨c • G, ⟨has_adjoint_smul c hG_adjoint, _⟩⟩,
simp only [comp_smul, smul_comp, hG_comm]
end
lemma normal_neg_of_normal {F : V →ₗ[ℝ] V} (h : normal F) :
normal (- F) :=
begin
rw ← neg_one_smul _ F,
apply normal_smul_of_normal (-1) h,
end
lemma normal_sub_algebra_map_of_normal
{F G : V →ₗ[ℝ] V} (c : ℝ) (hF : normal F) :
normal (F - algebra_map _ c) :=
begin
rcases hF with ⟨G, hG_adjoint, hG_comm⟩,
refine ⟨G - algebra_map _ c, ⟨has_adjoint_sub hG_adjoint (has_adjoint_smul c has_adjoint_id), _⟩⟩,
simp only [comp_eq_mul] at *,
simp [add_mul, mul_add, hG_comm, algebra.commutes]
end
lemma inner_adjoint_eq_inner_of_normal {F : V →ₗ[ℝ] V} (h : normal F) :
∀ x, inner (F.adjoint x) (F.adjoint x) = inner (F x) (F x) :=
begin
intros x,
rw inner_adjoint (has_adjoint_of_normal h),
rw ←comp_apply,
rw ←inner_adjoint (has_adjoint_of_normal h),
rw ←comp_apply,
rw adjoint_comm_of_normal h,
apply inner_comm
end
lemma ker_adjoint_of_normal {F : V →ₗ[ℝ] V} (h : normal F) :
F.adjoint.ker = F.ker :=
begin
ext,
rw [mem_ker, ←inner_self_eq_zero, inner_adjoint_eq_inner_of_normal h,
mem_ker, ←inner_self_eq_zero],
end
lemma ker_pow_eq_ker_of_normal
{F : V →ₗ[ℝ] V} (k : ℕ) (hn : normal F) :
(F ^ k.succ).ker = F.ker :=
begin
induction k with k ih,
{ refl },
{ ext,
split,
{ assume h : x ∈ ker (F ^ k.succ.succ),
have h0 : F.adjoint ((F ^ k.succ) x) = 0,
{ rw [mem_ker, pow_succ, mul_app, ←mem_ker,
←ker_adjoint_of_normal hn, mem_ker] at h,
exact h },
show x ∈ ker F,
by rw [←ih, mem_ker, ←inner_self_eq_zero, pow_succ, mul_app,
inner_adjoint (has_adjoint_of_normal hn), ←mul_app F,
←pow_succ, h0, inner_zero_right] },
{ intro h,
rw [mem_ker] at *,
rw [pow_succ', mul_app, h, map_zero] } }
end
end linear_map |
4e66e54f5d663dfe6dfad3c5f305d4e9840dd449 | 4950bf76e5ae40ba9f8491647d0b6f228ddce173 | /src/field_theory/intermediate_field.lean | b95985232d37c2eaff6ba9a946b685fa23cb3e09 | [
"Apache-2.0"
] | permissive | ntzwq/mathlib | ca50b21079b0a7c6781c34b62199a396dd00cee2 | 36eec1a98f22df82eaccd354a758ef8576af2a7f | refs/heads/master | 1,675,193,391,478 | 1,607,822,996,000 | 1,607,822,996,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,246 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import field_theory.subfield
import field_theory.tower
import ring_theory.algebraic
/-!
# Intermediate fields
Let `L / K` be a field extension, given as an instance `algebra K L`.
This file defines the type of fields in between `K` and `L`, `intermediate_field K L`.
An `intermediate_field K L` is a subfield of `L` which contains (the image of) `K`,
i.e. it is a `subfield L` and a `subalgebra K L`.
## Main definitions
* `intermediate_field K L` : the type of intermediate fields between `K` and `L`.
* `subalgebra.to_intermediate_field`: turns a subalgebra closed under `⁻¹`
into an intermediate field
* `subfield.to_intermediate_field`: turns a subfield containing the image of `K`
into an intermediate field
* `intermediate_field.map`: map an intermediate field along an `alg_hom`
## Implementation notes
Intermediate fields are defined with a structure extending `subfield` and `subalgebra`.
A `subalgebra` is closed under all operations except `⁻¹`,
## Tags
intermediate field, field extension
-/
open finite_dimensional
open_locale big_operators
variables (K L : Type*) [field K] [field L] [algebra K L]
section
set_option old_structure_cmd true
/-- `S : intermediate_field K L` is a subset of `L` such that there is a field
tower `L / S / K`. -/
structure intermediate_field extends subalgebra K L, subfield L
/-- Reinterpret an `intermediate_field` as a `subalgebra`. -/
add_decl_doc intermediate_field.to_subalgebra
/-- Reinterpret an `intermediate_field` as a `subfield`. -/
add_decl_doc intermediate_field.to_subfield
end
variables {K L} (S : intermediate_field K L)
namespace intermediate_field
instance : has_coe (intermediate_field K L) (set L) :=
⟨intermediate_field.carrier⟩
@[simp] lemma coe_to_subalgebra : (S.to_subalgebra : set L) = S := rfl
@[simp] lemma coe_to_subfield : (S.to_subfield : set L) = S := rfl
instance : has_coe_to_sort (intermediate_field K L) := ⟨Type*, λ S, S.carrier⟩
instance : has_mem L (intermediate_field K L) := ⟨λ m S, m ∈ (S : set L)⟩
@[simp] lemma mem_mk (s : set L) (hK : ∀ x, algebra_map K L x ∈ s)
(ho hm hz ha hn hi) (x : L) :
x ∈ intermediate_field.mk s ho hm hz ha hK hn hi ↔ x ∈ s := iff.rfl
@[simp] lemma mem_coe (x : L) : x ∈ (S : set L) ↔ x ∈ S := iff.rfl
@[simp] lemma mem_to_subalgebra (s : intermediate_field K L) (x : L) :
x ∈ s.to_subalgebra ↔ x ∈ s := iff.rfl
@[simp] lemma mem_to_subfield (s : intermediate_field K L) (x : L) :
x ∈ s.to_subfield ↔ x ∈ s := iff.rfl
/-- Two intermediate fields are equal if the underlying subsets are equal. -/
theorem ext' ⦃s t : intermediate_field K L⦄ (h : (s : set L) = t) : s = t :=
by { cases s, cases t, congr' }
/-- Two intermediate fields are equal if and only if the underlying subsets are equal. -/
protected theorem ext'_iff {s t : intermediate_field K L} : s = t ↔ (s : set L) = t :=
⟨λ h, h ▸ rfl, λ h, ext' h⟩
/-- Two intermediate fields are equal if they have the same elements. -/
@[ext] theorem ext {S T : intermediate_field K L} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T :=
ext' $ set.ext h
/-- An intermediate field contains the image of the smaller field. -/
theorem algebra_map_mem (x : K) : algebra_map K L x ∈ S :=
S.algebra_map_mem' x
/-- An intermediate field contains the ring's 1. -/
theorem one_mem : (1 : L) ∈ S := S.one_mem'
/-- An intermediate field contains the ring's 0. -/
theorem zero_mem : (0 : L) ∈ S := S.zero_mem'
/-- An intermediate field is closed under multiplication. -/
theorem mul_mem : ∀ {x y : L}, x ∈ S → y ∈ S → x * y ∈ S := S.mul_mem'
/-- An intermediate field is closed under scalar multiplication. -/
theorem smul_mem {y : L} : y ∈ S → ∀ {x : K}, x • y ∈ S := S.to_subalgebra.smul_mem
/-- An intermediate field is closed under addition. -/
theorem add_mem : ∀ {x y : L}, x ∈ S → y ∈ S → x + y ∈ S := S.add_mem'
/-- An intermediate field is closed under subtraction -/
theorem sub_mem {x y : L} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S :=
S.to_subfield.sub_mem hx hy
/-- An intermediate field is closed under negation. -/
theorem neg_mem : ∀ {x : L}, x ∈ S → -x ∈ S := S.neg_mem'
/-- An intermediate field is closed under inverses. -/
theorem inv_mem : ∀ {x : L}, x ∈ S → x⁻¹ ∈ S := S.inv_mem'
/-- An intermediate field is closed under division. -/
theorem div_mem {x y : L} (hx : x ∈ S) (hy : y ∈ S) : x / y ∈ S :=
S.to_subfield.div_mem hx hy
/-- Product of a list of elements in an intermediate_field is in the intermediate_field. -/
lemma list_prod_mem {l : list L} : (∀ x ∈ l, x ∈ S) → l.prod ∈ S :=
S.to_subfield.list_prod_mem
/-- Sum of a list of elements in an intermediate field is in the intermediate_field. -/
lemma list_sum_mem {l : list L} : (∀ x ∈ l, x ∈ S) → l.sum ∈ S :=
S.to_subfield.list_sum_mem
/-- Product of a multiset of elements in an intermediate field is in the intermediate_field. -/
lemma multiset_prod_mem (m : multiset L) :
(∀ a ∈ m, a ∈ S) → m.prod ∈ S :=
S.to_subfield.multiset_prod_mem m
/-- Sum of a multiset of elements in a `intermediate_field` is in the `intermediate_field`. -/
lemma multiset_sum_mem (m : multiset L) :
(∀ a ∈ m, a ∈ S) → m.sum ∈ S :=
S.to_subfield.multiset_sum_mem m
/-- Product of elements of an intermediate field indexed by a `finset` is in the intermediate_field. -/
lemma prod_mem {ι : Type*} {t : finset ι} {f : ι → L} (h : ∀ c ∈ t, f c ∈ S) :
∏ i in t, f i ∈ S :=
S.to_subfield.prod_mem h
/-- Sum of elements in a `intermediate_field` indexed by a `finset` is in the `intermediate_field`. -/
lemma sum_mem {ι : Type*} {t : finset ι} {f : ι → L} (h : ∀ c ∈ t, f c ∈ S) :
∑ i in t, f i ∈ S :=
S.to_subfield.sum_mem h
lemma pow_mem {x : L} (hx : x ∈ S) (n : ℤ) : x^n ∈ S :=
begin
cases n,
{ exact @is_submonoid.pow_mem L _ S.to_subfield.to_submonoid x _ hx n, },
{ have h := @is_submonoid.pow_mem L _ S.to_subfield.to_submonoid x _ hx _,
exact subfield.inv_mem S.to_subfield h, },
end
lemma gsmul_mem {x : L} (hx : x ∈ S) (n : ℤ) :
n •ℤ x ∈ S := S.to_subfield.gsmul_mem hx n
lemma coe_int_mem (n : ℤ) : (n : L) ∈ S :=
by simp only [← gsmul_one, gsmul_mem, one_mem]
end intermediate_field
/-- Turn a subalgebra closed under inverses into an intermediate field -/
def subalgebra.to_intermediate_field (S : subalgebra K L) (inv_mem : ∀ x ∈ S, x⁻¹ ∈ S) :
intermediate_field K L :=
{ neg_mem' := λ x, S.neg_mem,
inv_mem' := inv_mem,
.. S }
@[simp] lemma to_subalgebra_to_intermediate_field
(S : subalgebra K L) (inv_mem : ∀ x ∈ S, x⁻¹ ∈ S) :
(S.to_intermediate_field inv_mem).to_subalgebra = S :=
by { ext, refl }
@[simp] lemma to_intermediate_field_to_subalgebra
(S : intermediate_field K L) (inv_mem : ∀ x ∈ S.to_subalgebra, x⁻¹ ∈ S) :
S.to_subalgebra.to_intermediate_field inv_mem = S :=
by { ext, refl }
/-- Turn a subfield of `L` containing the image of `K` into an intermediate field -/
def subfield.to_intermediate_field (S : subfield L)
(algebra_map_mem : ∀ x, algebra_map K L x ∈ S) :
intermediate_field K L :=
{ algebra_map_mem' := algebra_map_mem,
.. S }
namespace intermediate_field
/-- An intermediate field inherits a field structure -/
instance to_field : field S :=
S.to_subfield.to_field
@[simp, norm_cast] lemma coe_add (x y : S) : (↑(x + y) : L) = ↑x + ↑y := rfl
@[simp, norm_cast] lemma coe_neg (x : S) : (↑(-x) : L) = -↑x := rfl
@[simp, norm_cast] lemma coe_mul (x y : S) : (↑(x * y) : L) = ↑x * ↑y := rfl
@[simp, norm_cast] lemma coe_inv (x : S) : (↑(x⁻¹) : L) = (↑x)⁻¹ := rfl
@[simp, norm_cast] lemma coe_zero : ((0 : S) : L) = 0 := rfl
@[simp, norm_cast] lemma coe_one : ((1 : S) : L) = 1 := rfl
instance algebra : algebra K S :=
S.to_subalgebra.algebra
instance to_algebra : algebra S L :=
S.to_subalgebra.to_algebra
instance : is_scalar_tower K S L :=
is_scalar_tower.subalgebra' _ _ _ S.to_subalgebra
variables {L' : Type*} [field L'] [algebra K L']
/-- If `f : L →+* L'` fixes `K`, `S.map f` is the intermediate field between `L'` and `K`
such that `x ∈ S ↔ f x ∈ S.map f`. -/
def map (f : L →ₐ[K] L') : intermediate_field K L' :=
{ inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, S.inv_mem hx, f.map_inv x⟩ },
neg_mem' := λ x hx, (S.to_subalgebra.map f).neg_mem hx,
.. S.to_subalgebra.map f}
/-- The embedding from an intermediate field of `L / K` to `L`. -/
def val : S →ₐ[K] L :=
S.to_subalgebra.val
@[simp] theorem coe_val : ⇑S.val = coe := rfl
@[simp] lemma val_mk {x : L} (hx : x ∈ S) : S.val ⟨x, hx⟩ = x := rfl
variables {S}
lemma to_subalgebra_injective {S S' : intermediate_field K L}
(h : S.to_subalgebra = S'.to_subalgebra) : S = S' :=
by { ext, rw [← mem_to_subalgebra, ← mem_to_subalgebra, h] }
instance : partial_order (intermediate_field K L) :=
{ le := λ S T, (S : set L) ⊆ T,
le_refl := λ S, set.subset.refl S,
le_trans := λ _ _ _, set.subset.trans,
le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ }
variables (S)
lemma set_range_subset : set.range (algebra_map K L) ⊆ S :=
S.to_subalgebra.range_subset
lemma field_range_le : (algebra_map K L).field_range ≤ S.to_subfield :=
λ x hx, S.to_subalgebra.range_subset (by rwa [set.mem_range, ← ring_hom.mem_field_range])
@[simp] lemma to_subalgebra_le_to_subalgebra {S S' : intermediate_field K L} :
S.to_subalgebra ≤ S'.to_subalgebra ↔ S ≤ S' := iff.rfl
@[simp] lemma to_subalgebra_lt_to_subalgebra {S S' : intermediate_field K L} :
S.to_subalgebra < S'.to_subalgebra ↔ S < S' := iff.rfl
variables {S}
section tower
/-- Lift an intermediate_field of an intermediate_field -/
def lift1 {F : intermediate_field K L} (E : intermediate_field K F) : intermediate_field K L :=
map E (val F)
/-- Lift an intermediate_field of an intermediate_field -/
def lift2 {F : intermediate_field K L} (E : intermediate_field F L) : intermediate_field K L :=
{ carrier := E.carrier,
zero_mem' := zero_mem E,
add_mem' := λ x y, add_mem E,
neg_mem' := λ x, neg_mem E,
one_mem' := one_mem E,
mul_mem' := λ x y, mul_mem E,
inv_mem' := λ x, inv_mem E,
algebra_map_mem' := λ x, algebra_map_mem E (algebra_map K F x) }
instance has_lift1 {F : intermediate_field K L} :
has_lift_t (intermediate_field K F) (intermediate_field K L) := ⟨lift1⟩
instance has_lift2 {F : intermediate_field K L} :
has_lift_t (intermediate_field F L) (intermediate_field K L) := ⟨lift2⟩
@[simp] lemma mem_lift2 {F : intermediate_field K L} {E : intermediate_field F L} {x : L} :
x ∈ (↑E : intermediate_field K L) ↔ x ∈ E := iff.rfl
end tower
section finite_dimensional
instance finite_dimensional_left [finite_dimensional K L] (F : intermediate_field K L) :
finite_dimensional K F :=
finite_dimensional.finite_dimensional_submodule F.to_subalgebra.to_submodule
instance finite_dimensional_right [finite_dimensional K L] (F : intermediate_field K L) :
finite_dimensional F L :=
right K F L
lemma eq_of_le_of_findim_le [finite_dimensional K L] {F E : intermediate_field K L} (h_le : F ≤ E)
(h_findim : findim K E ≤ findim K F) : F = E :=
intermediate_field.ext'_iff.mpr (submodule.ext'_iff.mp (eq_of_le_of_findim_le
(show F.to_subalgebra.to_submodule ≤ E.to_subalgebra.to_submodule, by exact h_le) h_findim))
lemma eq_of_le_of_findim_eq [finite_dimensional K L] {F E : intermediate_field K L} (h_le : F ≤ E)
(h_findim : findim K F = findim K E) : F = E :=
eq_of_le_of_findim_le h_le h_findim.ge
lemma eq_of_le_of_findim_le' [finite_dimensional K L] {F E : intermediate_field K L} (h_le : F ≤ E)
(h_findim : findim F L ≤ findim E L) : F = E :=
begin
apply eq_of_le_of_findim_le h_le,
have h1 := findim_mul_findim K F L,
have h2 := findim_mul_findim K E L,
have h3 : 0 < findim E L := findim_pos,
nlinarith,
end
lemma eq_of_le_of_findim_eq' [finite_dimensional K L] {F E : intermediate_field K L} (h_le : F ≤ E)
(h_findim : findim F L = findim E L) : F = E :=
eq_of_le_of_findim_le' h_le h_findim.le
end finite_dimensional
end intermediate_field
/-- If `L/K` is algebraic, the `K`-subalgebras of `L` are all fields. -/
def subalgebra_equiv_intermediate_field (alg : algebra.is_algebraic K L) :
subalgebra K L ≃o intermediate_field K L :=
{ to_fun := λ S, S.to_intermediate_field (λ x hx, S.inv_mem_of_algebraic (alg (⟨x, hx⟩ : S))),
inv_fun := λ S, S.to_subalgebra,
left_inv := λ S, to_subalgebra_to_intermediate_field _ _,
right_inv := λ S, to_intermediate_field_to_subalgebra _ _,
map_rel_iff' := λ S S', iff.rfl }
@[simp] lemma mem_subalgebra_equiv_intermediate_field (alg : algebra.is_algebraic K L)
{S : subalgebra K L} {x : L} :
x ∈ subalgebra_equiv_intermediate_field alg S ↔ x ∈ S :=
iff.rfl
@[simp] lemma mem_subalgebra_equiv_intermediate_field_symm (alg : algebra.is_algebraic K L)
{S : intermediate_field K L} {x : L} :
x ∈ (subalgebra_equiv_intermediate_field alg).symm S ↔ x ∈ S :=
iff.rfl
|
1a8c1d5df77f50cc165b1bf022ae84fce5e56f1e | d1bbf1801b3dcb214451d48214589f511061da63 | /src/algebra/lie/classical.lean | f8be292c40c3244c59e2ef88c67f1a96baf35f7a | [
"Apache-2.0"
] | permissive | cheraghchi/mathlib | 5c366f8c4f8e66973b60c37881889da8390cab86 | f29d1c3038422168fbbdb2526abf7c0ff13e86db | refs/heads/master | 1,676,577,831,283 | 1,610,894,638,000 | 1,610,894,638,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,509 | lean | /-
Copyright (c) 2020 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import algebra.invertible
import algebra.lie.skew_adjoint
import linear_algebra.matrix
/-!
# Classical Lie algebras
This file is the place to find definitions and basic properties of the classical Lie algebras:
* Aₗ = sl(l+1)
* Bₗ ≃ so(l+1, l) ≃ so(2l+1)
* Cₗ = sp(l)
* Dₗ ≃ so(l, l) ≃ so(2l)
## Main definitions
* `lie_algebra.special_linear.sl`
* `lie_algebra.symplectic.sp`
* `lie_algebra.orthogonal.so`
* `lie_algebra.orthogonal.so'`
* `lie_algebra.orthogonal.so_indefinite_equiv`
* `lie_algebra.orthogonal.type_D`
* `lie_algebra.orthogonal.type_B`
* `lie_algebra.orthogonal.type_D_equiv_so'`
* `lie_algebra.orthogonal.type_B_equiv_so'`
## Implementation notes
### Matrices or endomorphisms
Given a finite type and a commutative ring, the corresponding square matrices are equivalent to the
endomorphisms of the corresponding finite-rank free module as Lie algebras, see `lie_equiv_matrix'`.
We can thus define the classical Lie algebras as Lie subalgebras either of matrices or of
endomorphisms. We have opted for the former. At the time of writing (August 2020) it is unclear
which approach should be preferred so the choice should be assumed to be somewhat arbitrary.
### Diagonal quadratic form or diagonal Cartan subalgebra
For the algebras of type `B` and `D`, there are two natural definitions. For example since the
the `2l × 2l` matrix:
$$
J = \left[\begin{array}{cc}
0_l & 1_l\\\\
1_l & 0_l
\end{array}\right]
$$
defines a symmetric bilinear form equivalent to that defined by the identity matrix `I`, we can
define the algebras of type `D` to be the Lie subalgebra of skew-adjoint matrices either for `J` or
for `I`. Both definitions have their advantages (in particular the `J`-skew-adjoint matrices define
a Lie algebra for which the diagonal matrices form a Cartan subalgebra) and so we provide both.
We thus also provide equivalences `type_D_equiv_so'`, `so_indefinite_equiv` which show the two
definitions are equivalent. Similarly for the algebras of type `B`.
## Tags
classical lie algebra, special linear, symplectic, orthogonal
-/
universes u₁ u₂
namespace lie_algebra
open_locale matrix
variables (n p q l : Type*) (R : Type u₂)
variables [fintype n] [fintype l] [fintype p] [fintype q]
variables [decidable_eq n] [decidable_eq p] [decidable_eq q] [decidable_eq l]
variables [comm_ring R]
@[simp] lemma matrix_trace_commutator_zero (X Y : matrix n n R) : matrix.trace n R R ⁅X, Y⁆ = 0 :=
begin
-- TODO: if we use matrix.mul here, we get a timeout
change matrix.trace n R R (X * Y - Y * X) = 0,
erw [linear_map.map_sub, matrix.trace_mul_comm, sub_self]
end
namespace special_linear
/-- The special linear Lie algebra: square matrices of trace zero. -/
def sl : lie_subalgebra R (matrix n n R) :=
{ lie_mem := λ X Y _ _, linear_map.mem_ker.2 $ matrix_trace_commutator_zero _ _ _ _,
..linear_map.ker (matrix.trace n R R) }
lemma sl_bracket (A B : sl n R) : ⁅A, B⁆.val = A.val ⬝ B.val - B.val ⬝ A.val := rfl
section elementary_basis
variables {n} (i j : n)
/-- It is useful to define these matrices for explicit calculations in sl n R. -/
abbreviation E : matrix n n R := λ i' j', if i = i' ∧ j = j' then 1 else 0
@[simp] lemma E_apply_one : E R i j i j = 1 := if_pos (and.intro rfl rfl)
@[simp] lemma E_apply_zero (i' j' : n) (h : ¬(i = i' ∧ j = j')) : E R i j i' j' = 0 := if_neg h
@[simp] lemma E_diag_zero (h : j ≠ i) : matrix.diag n R R (E R i j) = 0 :=
begin
ext k, rw matrix.diag_apply,
suffices : ¬(i = k ∧ j = k), by exact if_neg this,
rintros ⟨e₁, e₂⟩, apply h, subst e₁, exact e₂,
end
lemma E_trace_zero (h : j ≠ i) : matrix.trace n R R (E R i j) = 0 := by simp [h]
/-- When j ≠ i, the elementary matrices are elements of sl n R, in fact they are part of a natural
basis of sl n R. -/
def Eb (h : j ≠ i) : sl n R :=
⟨E R i j, by { change E R i j ∈ linear_map.ker (matrix.trace n R R), simp [E_trace_zero R i j h], }⟩
@[simp] lemma Eb_val (h : j ≠ i) : (Eb R i j h).val = E R i j := rfl
end elementary_basis
lemma sl_non_abelian [nontrivial R] (h : 1 < fintype.card n) : ¬is_lie_abelian ↥(sl n R) :=
begin
rcases fintype.exists_pair_of_one_lt_card h with ⟨j, i, hij⟩,
let A := Eb R i j hij,
let B := Eb R j i hij.symm,
intros c,
have c' : A.val ⬝ B.val = B.val ⬝ A.val, by { rw [← sub_eq_zero, ← sl_bracket, c.trivial], refl },
have : (1 : R) = 0 := by simpa [matrix.mul_apply, hij] using (congr_fun (congr_fun c' i) i),
exact one_ne_zero this,
end
end special_linear
namespace symplectic
/-- The matrix defining the canonical skew-symmetric bilinear form. -/
def J : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 0 (-1) 1 0
/-- The symplectic Lie algebra: skew-adjoint matrices with respect to the canonical skew-symmetric
bilinear form. -/
def sp : lie_subalgebra R (matrix (l ⊕ l) (l ⊕ l) R) :=
skew_adjoint_matrices_lie_subalgebra (J l R)
end symplectic
namespace orthogonal
/-- The definite orthogonal Lie subalgebra: skew-adjoint matrices with respect to the symmetric
bilinear form defined by the identity matrix. -/
def so : lie_subalgebra R (matrix n n R) :=
skew_adjoint_matrices_lie_subalgebra (1 : matrix n n R)
@[simp] lemma mem_so (A : matrix n n R) : A ∈ so n R ↔ Aᵀ = -A :=
begin
erw mem_skew_adjoint_matrices_submodule,
simp only [matrix.is_skew_adjoint, matrix.is_adjoint_pair, matrix.mul_one, matrix.one_mul],
end
/-- The indefinite diagonal matrix with `p` 1s and `q` -1s. -/
def indefinite_diagonal : matrix (p ⊕ q) (p ⊕ q) R :=
matrix.diagonal $ sum.elim (λ _, 1) (λ _, -1)
/-- The indefinite orthogonal Lie subalgebra: skew-adjoint matrices with respect to the symmetric
bilinear form defined by the indefinite diagonal matrix. -/
def so' : lie_subalgebra R (matrix (p ⊕ q) (p ⊕ q) R) :=
skew_adjoint_matrices_lie_subalgebra $ indefinite_diagonal p q R
/-- A matrix for transforming the indefinite diagonal bilinear form into the definite one, provided
the parameter `i` is a square root of -1. -/
def Pso (i : R) : matrix (p ⊕ q) (p ⊕ q) R :=
matrix.diagonal $ sum.elim (λ _, 1) (λ _, i)
lemma Pso_inv {i : R} (hi : i*i = -1) : (Pso p q R i) * (Pso p q R (-i)) = 1 :=
begin
ext x y, rcases x; rcases y,
{ -- x y : p
by_cases h : x = y; simp [Pso, indefinite_diagonal, h], },
{ -- x : p, y : q
simp [Pso, indefinite_diagonal], },
{ -- x : q, y : p
simp [Pso, indefinite_diagonal], },
{ -- x y : q
by_cases h : x = y; simp [Pso, indefinite_diagonal, h, hi], },
end
lemma is_unit_Pso {i : R} (hi : i*i = -1) : is_unit (Pso p q R i) :=
⟨{ val := Pso p q R i,
inv := Pso p q R (-i),
val_inv := Pso_inv p q R hi,
inv_val := by { apply matrix.nonsing_inv_left_right, exact Pso_inv p q R hi, }, },
rfl⟩
lemma indefinite_diagonal_transform {i : R} (hi : i*i = -1) :
(Pso p q R i)ᵀ ⬝ (indefinite_diagonal p q R) ⬝ (Pso p q R i) = 1 :=
begin
ext x y, rcases x; rcases y,
{ -- x y : p
by_cases h : x = y; simp [Pso, indefinite_diagonal, h], },
{ -- x : p, y : q
simp [Pso, indefinite_diagonal], },
{ -- x : q, y : p
simp [Pso, indefinite_diagonal], },
{ -- x y : q
by_cases h : x = y; simp [Pso, indefinite_diagonal, h, hi], },
end
/-- An equivalence between the indefinite and definite orthogonal Lie algebras, over a ring
containing a square root of -1. -/
noncomputable def so_indefinite_equiv {i : R} (hi : i*i = -1) : so' p q R ≃ₗ⁅R⁆ so (p ⊕ q) R :=
begin
apply (skew_adjoint_matrices_lie_subalgebra_equiv
(indefinite_diagonal p q R) (Pso p q R i) (is_unit_Pso p q R hi)).trans,
apply lie_algebra.equiv.of_eq,
ext A, rw indefinite_diagonal_transform p q R hi, refl,
end
lemma so_indefinite_equiv_apply {i : R} (hi : i*i = -1) (A : so' p q R) :
(so_indefinite_equiv p q R hi A : matrix (p ⊕ q) (p ⊕ q) R) =
(Pso p q R i)⁻¹ ⬝ (A : matrix (p ⊕ q) (p ⊕ q) R) ⬝ (Pso p q R i) :=
by erw [lie_algebra.equiv.trans_apply, lie_algebra.equiv.of_eq_apply,
skew_adjoint_matrices_lie_subalgebra_equiv_apply]
/-- A matrix defining a canonical even-rank symmetric bilinear form.
It looks like this as a `2l x 2l` matrix of `l x l` blocks:
[ 0 1 ]
[ 1 0 ]
-/
def JD : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 0 1 1 0
/-- The classical Lie algebra of type D as a Lie subalgebra of matrices associated to the matrix
`JD`. -/
def type_D := skew_adjoint_matrices_lie_subalgebra (JD l R)
/-- A matrix transforming the bilinear form defined by the matrix `JD` into a split-signature
diagonal matrix.
It looks like this as a `2l x 2l` matrix of `l x l` blocks:
[ 1 -1 ]
[ 1 1 ]
-/
def PD : matrix (l ⊕ l) (l ⊕ l) R := matrix.from_blocks 1 (-1) 1 1
/-- The split-signature diagonal matrix. -/
def S := indefinite_diagonal l l R
lemma S_as_blocks : S l R = matrix.from_blocks 1 0 0 (-1) :=
begin
rw [← matrix.diagonal_one, matrix.diagonal_neg, matrix.from_blocks_diagonal],
refl,
end
lemma JD_transform : (PD l R)ᵀ ⬝ (JD l R) ⬝ (PD l R) = (2 : R) • (S l R) :=
begin
have h : (PD l R)ᵀ ⬝ (JD l R) = matrix.from_blocks 1 1 1 (-1) := by
{ simp [PD, JD, matrix.from_blocks_transpose, matrix.from_blocks_multiply], },
erw [h, S_as_blocks, matrix.from_blocks_multiply, matrix.from_blocks_smul],
congr; simp [two_smul],
end
lemma PD_inv [invertible (2 : R)] : (PD l R) * (⅟(2 : R) • (PD l R)ᵀ) = 1 :=
begin
have h : ⅟(2 : R) • (1 : matrix l l R) + ⅟(2 : R) • 1 = 1 := by
rw [← smul_add, ← (two_smul R _), smul_smul, inv_of_mul_self, one_smul],
erw [matrix.from_blocks_transpose, matrix.from_blocks_smul, matrix.mul_eq_mul,
matrix.from_blocks_multiply],
simp [h],
end
lemma is_unit_PD [invertible (2 : R)] : is_unit (PD l R) :=
⟨{ val := PD l R,
inv := ⅟(2 : R) • (PD l R)ᵀ,
val_inv := PD_inv l R,
inv_val := by { apply matrix.nonsing_inv_left_right, exact PD_inv l R, }, },
rfl⟩
/-- An equivalence between two possible definitions of the classical Lie algebra of type D. -/
noncomputable def type_D_equiv_so' [invertible (2 : R)] :
type_D l R ≃ₗ⁅R⁆ so' l l R :=
begin
apply (skew_adjoint_matrices_lie_subalgebra_equiv (JD l R) (PD l R) (is_unit_PD l R)).trans,
apply lie_algebra.equiv.of_eq,
ext A,
rw [JD_transform, ← unit_of_invertible_val (2 : R), lie_subalgebra.mem_coe,
mem_skew_adjoint_matrices_lie_subalgebra_unit_smul],
refl,
end
/-- A matrix defining a canonical odd-rank symmetric bilinear form.
It looks like this as a `(2l+1) x (2l+1)` matrix of blocks:
[ 2 0 0 ]
[ 0 0 1 ]
[ 0 1 0 ]
where sizes of the blocks are:
[`1 x 1` `1 x l` `1 x l`]
[`l x 1` `l x l` `l x l`]
[`l x 1` `l x l` `l x l`]
-/
def JB := matrix.from_blocks ((2 : R) • 1 : matrix unit unit R) 0 0 (JD l R)
/-- The classical Lie algebra of type B as a Lie subalgebra of matrices associated to the matrix
`JB`. -/
def type_B := skew_adjoint_matrices_lie_subalgebra (JB l R)
/-- A matrix transforming the bilinear form defined by the matrix `JB` into an
almost-split-signature diagonal matrix.
It looks like this as a `(2l+1) x (2l+1)` matrix of blocks:
[ 1 0 0 ]
[ 0 1 -1 ]
[ 0 1 1 ]
where sizes of the blocks are:
[`1 x 1` `1 x l` `1 x l`]
[`l x 1` `l x l` `l x l`]
[`l x 1` `l x l` `l x l`]
-/
def PB := matrix.from_blocks (1 : matrix unit unit R) 0 0 (PD l R)
lemma PB_inv [invertible (2 : R)] : (PB l R) * (matrix.from_blocks 1 0 0 (PD l R)⁻¹) = 1 :=
begin
simp [PB, matrix.from_blocks_multiply, (PD l R).mul_nonsing_inv, is_unit_PD,
← (PD l R).is_unit_iff_is_unit_det]
end
lemma is_unit_PB [invertible (2 : R)] : is_unit (PB l R) :=
⟨{ val := PB l R,
inv := matrix.from_blocks 1 0 0 (PD l R)⁻¹,
val_inv := PB_inv l R,
inv_val := by { apply matrix.nonsing_inv_left_right, exact PB_inv l R, }, },
rfl⟩
lemma JB_transform : (PB l R)ᵀ ⬝ (JB l R) ⬝ (PB l R) = (2 : R) • matrix.from_blocks 1 0 0 (S l R) :=
by simp [PB, JB, JD_transform, matrix.from_blocks_transpose, matrix.from_blocks_multiply,
matrix.from_blocks_smul]
lemma indefinite_diagonal_assoc :
indefinite_diagonal (unit ⊕ l) l R =
matrix.reindex_lie_equiv (equiv.sum_assoc unit l l).symm
(matrix.from_blocks 1 0 0 (indefinite_diagonal l l R)) :=
begin
ext i j,
rcases i with ⟨⟨i₁ | i₂⟩ | i₃⟩;
rcases j with ⟨⟨j₁ | j₂⟩ | j₃⟩;
simp [indefinite_diagonal, matrix.diagonal],
end
/-- An equivalence between two possible definitions of the classical Lie algebra of type B. -/
noncomputable def type_B_equiv_so' [invertible (2 : R)] :
type_B l R ≃ₗ⁅R⁆ so' (unit ⊕ l) l R :=
begin
apply (skew_adjoint_matrices_lie_subalgebra_equiv (JB l R) (PB l R) (is_unit_PB l R)).trans,
symmetry,
apply (skew_adjoint_matrices_lie_subalgebra_equiv_transpose
(indefinite_diagonal (unit ⊕ l) l R)
(matrix.reindex_alg_equiv (equiv.sum_assoc punit l l)) (matrix.reindex_transpose _ _)).trans,
apply lie_algebra.equiv.of_eq,
ext A,
rw [JB_transform, ← unit_of_invertible_val (2 : R), lie_subalgebra.mem_coe,
lie_subalgebra.mem_coe, mem_skew_adjoint_matrices_lie_subalgebra_unit_smul],
simpa [indefinite_diagonal_assoc],
end
end orthogonal
end lie_algebra
|
70cf70c45618b3cc7eebc22f8a7e30dbb7245ed9 | 07c6143268cfb72beccd1cc35735d424ebcb187b | /src/data/padics/padic_norm.lean | 64396a56e41904a417e27ad658db954b990a5f15 | [
"Apache-2.0"
] | permissive | khoek/mathlib | bc49a842910af13a3c372748310e86467d1dc766 | aa55f8b50354b3e11ba64792dcb06cccb2d8ee28 | refs/heads/master | 1,588,232,063,837 | 1,587,304,803,000 | 1,587,304,803,000 | 176,688,517 | 0 | 0 | Apache-2.0 | 1,553,070,585,000 | 1,553,070,585,000 | null | UTF-8 | Lean | false | false | 17,080 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis
-/
import data.rat algebra.gcd_domain algebra.field_power
import ring_theory.multiplicity tactic.ring
import data.real.cau_seq
import tactic.norm_cast
/-!
# p-adic norm
This file defines the p-adic valuation and the p-adic norm on ℚ.
The p-adic valuation on ℚ is the difference of the multiplicities of `p` in the numerator and
denominator of `q`. This function obeys the standard properties of a valuation, with the appropriate
assumptions on p.
The valuation induces a norm on ℚ. This norm is a nonarchimedean absolute value.
It takes values in {0} ∪ {1/p^k | k ∈ ℤ}.
## Notations
This file uses the local notation `/.` for `rat.mk`.
## Implementation notes
Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically
by taking (prime p) as a type class argument.
## References
* [F. Q. Gouêva, *p-adic numbers*][gouvea1997]
* [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019]
* <https://en.wikipedia.org/wiki/P-adic_number>
## Tags
p-adic, p adic, padic, norm, valuation
-/
universe u
open nat
attribute [class] nat.prime
open_locale rat
open multiplicity
/--
For `p ≠ 1`, the p-adic valuation of an integer `z ≠ 0` is the largest natural number `n` such that
p^n divides z.
`padic_val_rat` defines the valuation of a rational `q` to be the valuation of `q.num` minus the
valuation of `q.denom`.
If `q = 0` or `p = 1`, then `padic_val_rat p q` defaults to 0.
-/
def padic_val_rat (p : ℕ) (q : ℚ) : ℤ :=
if h : q ≠ 0 ∧ p ≠ 1
then (multiplicity (p : ℤ) q.num).get
(multiplicity.finite_int_iff.2 ⟨h.2, rat.num_ne_zero_of_ne_zero h.1⟩) -
(multiplicity (p : ℤ) q.denom).get
(multiplicity.finite_int_iff.2 ⟨h.2, by exact_mod_cast rat.denom_ne_zero _⟩)
else 0
/--
A simplification of the definition of `padic_val_rat p q` when `q ≠ 0` and `p` is prime.
-/
lemma padic_val_rat_def (p : ℕ) [hp : p.prime] {q : ℚ} (hq : q ≠ 0) : padic_val_rat p q =
(multiplicity (p : ℤ) q.num).get (finite_int_iff.2 ⟨hp.ne_one, rat.num_ne_zero_of_ne_zero hq⟩) -
(multiplicity (p : ℤ) q.denom).get (finite_int_iff.2 ⟨hp.ne_one, by exact_mod_cast rat.denom_ne_zero _⟩) :=
dif_pos ⟨hq, hp.ne_one⟩
namespace padic_val_rat
open multiplicity
section padic_val_rat
variables {p : ℕ}
/--
`padic_val_rat p q` is symmetric in `q`.
-/
@[simp] protected lemma neg (q : ℚ) : padic_val_rat p (-q) = padic_val_rat p q :=
begin
unfold padic_val_rat,
split_ifs,
{ simp [-add_comm]; refl },
{ exfalso, simp * at * },
{ exfalso, simp * at * },
{ refl }
end
/--
`padic_val_rat p 1` is 0 for any `p`.
-/
@[simp] protected lemma one : padic_val_rat p 1 = 0 :=
by unfold padic_val_rat; split_ifs; simp *
/--
For `p ≠ 0, p ≠ 1, `padic_val_rat p p` is 1.
-/
@[simp] lemma padic_val_rat_self (hp : 1 < p) : padic_val_rat p p = 1 :=
by unfold padic_val_rat; split_ifs; simp [*, nat.one_lt_iff_ne_zero_and_ne_one] at *
/--
The p-adic value of an integer `z ≠ 0` is the multiplicity of `p` in `z`.
-/
lemma padic_val_rat_of_int (z : ℤ) (hp : p ≠ 1) (hz : z ≠ 0) :
padic_val_rat p (z : ℚ) = (multiplicity (p : ℤ) z).get
(finite_int_iff.2 ⟨hp, hz⟩) :=
by rw [padic_val_rat, dif_pos]; simp *; refl
end padic_val_rat
section padic_val_rat
open multiplicity
variables (p : ℕ) [p_prime : nat.prime p]
include p_prime
/--
The multiplicity of `p : ℕ` in `a : ℤ` is finite exactly when `a ≠ 0`.
-/
lemma finite_int_prime_iff {p : ℕ} [p_prime : p.prime] {a : ℤ} : finite (p : ℤ) a ↔ a ≠ 0 :=
by simp [finite_int_iff, ne.symm (ne_of_lt (p_prime.one_lt))]
/--
A rewrite lemma for `padic_val_rat p q` when `q` is expressed in terms of `rat.mk`.
-/
protected lemma defn {q : ℚ} {n d : ℤ} (hqz : q ≠ 0) (qdf : q = n /. d) :
padic_val_rat p q = (multiplicity (p : ℤ) n).get (finite_int_iff.2
⟨ne.symm $ ne_of_lt p_prime.one_lt, λ hn, by simp * at *⟩) -
(multiplicity (p : ℤ) d).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.one_lt,
λ hd, by simp * at *⟩) :=
have hn : n ≠ 0, from rat.mk_num_ne_zero_of_ne_zero hqz qdf,
have hd : d ≠ 0, from rat.mk_denom_ne_zero_of_ne_zero hqz qdf,
let ⟨c, hc1, hc2⟩ := rat.num_denom_mk hn hd qdf in
by rw [padic_val_rat, dif_pos];
simp [hc1, hc2, multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime),
(ne.symm (ne_of_lt p_prime.one_lt)), hqz]
/--
A rewrite lemma for `padic_val_rat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`.
-/
protected lemma mul {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) :
padic_val_rat p (q * r) = padic_val_rat p q + padic_val_rat p r :=
have q*r = (q.num * r.num) /. (↑q.denom * ↑r.denom), by rw_mod_cast rat.mul_num_denom,
have hq' : q.num /. q.denom ≠ 0, by rw rat.num_denom; exact hq,
have hr' : r.num /. r.denom ≠ 0, by rw rat.num_denom; exact hr,
have hp' : _root_.prime (p : ℤ), from nat.prime_iff_prime_int.1 p_prime,
begin
rw [padic_val_rat.defn p (mul_ne_zero hq hr) this],
conv_rhs { rw [←(@rat.num_denom q), padic_val_rat.defn p hq',
←(@rat.num_denom r), padic_val_rat.defn p hr'] },
rw [multiplicity.mul' hp', multiplicity.mul' hp']; simp [add_comm, add_left_comm, sub_eq_add_neg]
end
/--
A rewrite lemma for `padic_val_rat p (q^k) with condition `q ≠ 0`.
-/
protected lemma pow {q : ℚ} (hq : q ≠ 0) {k : ℕ} :
padic_val_rat p (q ^ k) = k * padic_val_rat p q :=
by induction k; simp [*, padic_val_rat.mul _ hq (pow_ne_zero _ hq),
_root_.pow_succ, add_mul, add_comm]
/--
A rewrite lemma for `padic_val_rat p (q⁻¹)` with condition `q ≠ 0`.
-/
protected lemma inv {q : ℚ} (hq : q ≠ 0) :
padic_val_rat p (q⁻¹) = -padic_val_rat p q :=
by rw [eq_neg_iff_add_eq_zero, ← padic_val_rat.mul p (inv_ne_zero hq) hq,
inv_mul_cancel hq, padic_val_rat.one]
/--
A rewrite lemma for `padic_val_rat p (q / r)` with conditions `q ≠ 0`, `r ≠ 0`.
-/
protected lemma div {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) :
padic_val_rat p (q / r) = padic_val_rat p q - padic_val_rat p r :=
by rw [div_eq_mul_inv, padic_val_rat.mul p hq (inv_ne_zero hr),
padic_val_rat.inv p hr, sub_eq_add_neg]
/--
A condition for `padic_val_rat p (n₁ / d₁) ≤ padic_val_rat p (n₂ / d₂),
in terms of divisibility by `p^n`.
-/
lemma padic_val_rat_le_padic_val_rat_iff {n₁ n₂ d₁ d₂ : ℤ}
(hn₁ : n₁ ≠ 0) (hn₂ : n₂ ≠ 0) (hd₁ : d₁ ≠ 0) (hd₂ : d₂ ≠ 0) :
padic_val_rat p (n₁ /. d₁) ≤ padic_val_rat p (n₂ /. d₂) ↔
∀ (n : ℕ), ↑p ^ n ∣ n₁ * d₂ → ↑p ^ n ∣ n₂ * d₁ :=
have hf1 : finite (p : ℤ) (n₁ * d₂),
from finite_int_prime_iff.2 (mul_ne_zero hn₁ hd₂),
have hf2 : finite (p : ℤ) (n₂ * d₁),
from finite_int_prime_iff.2 (mul_ne_zero hn₂ hd₁),
by conv {
to_lhs,
rw [padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₁ hd₁) rfl,
padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₂ hd₂) rfl,
sub_le_iff_le_add',
← add_sub_assoc,
le_sub_iff_add_le],
norm_cast,
rw [← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime) hf1, add_comm,
← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime) hf2,
enat.get_le_get, multiplicity_le_multiplicity_iff]
}
/--
Sufficient conditions to show that the p-adic valuation of `q` is less than or equal to the
p-adic vlauation of `q + r`.
-/
theorem le_padic_val_rat_add_of_le {q r : ℚ}
(hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0)
(h : padic_val_rat p q ≤ padic_val_rat p r) :
padic_val_rat p q ≤ padic_val_rat p (q + r) :=
have hqn : q.num ≠ 0, from rat.num_ne_zero_of_ne_zero hq,
have hqd : (q.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _,
have hrn : r.num ≠ 0, from rat.num_ne_zero_of_ne_zero hr,
have hrd : (r.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _,
have hqdv : q.num /. q.denom ≠ 0, from rat.mk_ne_zero_of_ne_zero hqn hqd,
have hrdv : r.num /. r.denom ≠ 0, from rat.mk_ne_zero_of_ne_zero hrn hrd,
have hqreq : q + r = (((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ)),
from rat.add_num_denom _ _,
have hqrd : q.num * ↑(r.denom) + ↑(q.denom) * r.num ≠ 0,
from rat.mk_num_ne_zero_of_ne_zero hqr hqreq,
begin
conv_lhs { rw ←(@rat.num_denom q) },
rw [hqreq, padic_val_rat_le_padic_val_rat_iff p hqn hqrd hqd (mul_ne_zero hqd hrd),
← multiplicity_le_multiplicity_iff, mul_left_comm,
multiplicity.mul (nat.prime_iff_prime_int.1 p_prime), add_mul],
rw [←(@rat.num_denom q), ←(@rat.num_denom r), padic_val_rat_le_padic_val_rat_iff p hqn hrn hqd hrd,
← multiplicity_le_multiplicity_iff] at h,
calc _ ≤ min (multiplicity ↑p (q.num * ↑(r.denom) * ↑(q.denom)))
(multiplicity ↑p (↑(q.denom) * r.num * ↑(q.denom))) : (le_min
(by rw [@multiplicity.mul _ _ _ _ (_ * _) _ (nat.prime_iff_prime_int.1 p_prime), add_comm])
(by rw [mul_assoc, @multiplicity.mul _ _ _ _ (q.denom : ℤ)
(_ * _) (nat.prime_iff_prime_int.1 p_prime)];
exact add_le_add_left' h))
... ≤ _ : min_le_multiplicity_add
end
/--
The minimum of the valuations of `q` and `r` is less than or equal to the valuation of `q + r`.
-/
theorem min_le_padic_val_rat_add {q r : ℚ}
(hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0) :
min (padic_val_rat p q) (padic_val_rat p r) ≤ padic_val_rat p (q + r) :=
(le_total (padic_val_rat p q) (padic_val_rat p r)).elim
(λ h, by rw [min_eq_left h]; exact le_padic_val_rat_add_of_le _ hq hr hqr h)
(λ h, by rw [min_eq_right h, add_comm]; exact le_padic_val_rat_add_of_le _ hr hq
(by rwa add_comm) h)
end padic_val_rat
end padic_val_rat
/--
If `q ≠ 0`, the p-adic norm of a rational `q` is `p ^ (-(padic_val_rat p q))`.
If `q = 0`, the p-adic norm of `q` is 0.
-/
def padic_norm (p : ℕ) (q : ℚ) : ℚ :=
if q = 0 then 0 else (↑p : ℚ) ^ (-(padic_val_rat p q))
namespace padic_norm
section padic_norm
open padic_val_rat
variables (p : ℕ)
/--
Unfolds the definition of the p-adic norm of `q` when `q ≠ 0`.
-/
@[simp] protected lemma eq_fpow_of_nonzero {q : ℚ} (hq : q ≠ 0) :
padic_norm p q = p ^ (-(padic_val_rat p q)) :=
by simp [hq, padic_norm]
/--
The p-adic norm is nonnegative.
-/
protected lemma nonneg (q : ℚ) : 0 ≤ padic_norm p q :=
if hq : q = 0 then by simp [hq]
else
begin
unfold padic_norm; split_ifs,
apply fpow_nonneg_of_nonneg,
exact_mod_cast nat.zero_le _
end
/--
The p-adic norm of 0 is 0.
-/
@[simp] protected lemma zero : padic_norm p 0 = 0 := by simp [padic_norm]
/--
The p-adic norm of 1 is 1.
-/
@[simp] protected lemma one : padic_norm p 1 = 1 := by simp [padic_norm]
/--
The image of `padic_norm p` is {0} ∪ {p^(-n) | n ∈ ℤ}.
-/
protected theorem image {q : ℚ} (hq : q ≠ 0) : ∃ n : ℤ, padic_norm p q = p ^ (-n) :=
⟨ (padic_val_rat p q), by simp [padic_norm, hq] ⟩
variable [hp : p.prime]
include hp
/--
If `q ≠ 0`, then `padic_norm p q ≠ 0`.
-/
protected lemma nonzero {q : ℚ} (hq : q ≠ 0) : padic_norm p q ≠ 0 :=
begin
rw padic_norm.eq_fpow_of_nonzero p hq,
apply fpow_ne_zero_of_ne_zero,
exact_mod_cast ne_of_gt hp.pos
end
/--
`padic_norm p` is symmetric.
-/
@[simp] protected lemma neg (q : ℚ) : padic_norm p (-q) = padic_norm p q :=
if hq : q = 0 then by simp [hq]
else by simp [padic_norm, hq, hp.one_lt]
/--
If the p-adic norm of `q` is 0, then `q` is 0.
-/
lemma zero_of_padic_norm_eq_zero {q : ℚ} (h : padic_norm p q = 0) : q = 0 :=
begin
apply by_contradiction, intro hq,
unfold padic_norm at h, rw if_neg hq at h,
apply absurd h,
apply fpow_ne_zero_of_ne_zero,
exact_mod_cast hp.ne_zero
end
/--
The p-adic norm is multiplicative.
-/
@[simp] protected theorem mul (q r : ℚ) : padic_norm p (q*r) = padic_norm p q * padic_norm p r :=
if hq : q = 0 then
by simp [hq]
else if hr : r = 0 then
by simp [hr]
else
have q*r ≠ 0, from mul_ne_zero hq hr,
have (↑p : ℚ) ≠ 0, by simp [hp.ne_zero],
by simp [padic_norm, *, padic_val_rat.mul, fpow_add this, mul_comm]
/--
The p-adic norm respects division.
-/
@[simp] protected theorem div (q r : ℚ) : padic_norm p (q / r) = padic_norm p q / padic_norm p r :=
if hr : r = 0 then by simp [hr] else
eq_div_of_mul_eq _ _ (padic_norm.nonzero _ hr) (by rw [←padic_norm.mul, div_mul_cancel _ hr])
/--
The p-adic norm of an integer is at most 1.
-/
protected theorem of_int (z : ℤ) : padic_norm p ↑z ≤ 1 :=
if hz : z = 0 then by simp [hz] else
begin
unfold padic_norm,
rw [if_neg _],
{ refine fpow_le_one_of_nonpos _ _,
{ exact_mod_cast le_of_lt hp.one_lt, },
{ rw [padic_val_rat_of_int _ hp.ne_one hz, neg_nonpos],
norm_cast, simp }},
exact_mod_cast hz
end
private lemma nonarchimedean_aux {q r : ℚ} (h : padic_val_rat p q ≤ padic_val_rat p r) :
padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) :=
have hnqp : padic_norm p q ≥ 0, from padic_norm.nonneg _ _,
have hnrp : padic_norm p r ≥ 0, from padic_norm.nonneg _ _,
if hq : q = 0 then
by simp [hq, max_eq_right hnrp, le_max_right]
else if hr : r = 0 then
by simp [hr, max_eq_left hnqp, le_max_left]
else if hqr : q + r = 0 then
le_trans (by simpa [hqr] using hnqp) (le_max_left _ _)
else
begin
unfold padic_norm, split_ifs,
apply le_max_iff.2,
left,
apply fpow_le_of_le,
{ exact_mod_cast le_of_lt hp.one_lt },
{ apply neg_le_neg,
have : padic_val_rat p q =
min (padic_val_rat p q) (padic_val_rat p r),
from (min_eq_left h).symm,
rw this,
apply min_le_padic_val_rat_add; assumption }
end
/--
The p-adic norm is nonarchimedean: the norm of `p + q` is at most the max of the norm of `p` and
the norm of `q`.
-/
protected theorem nonarchimedean {q r : ℚ} :
padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) :=
begin
wlog hle := le_total (padic_val_rat p q) (padic_val_rat p r) using [q r],
exact nonarchimedean_aux p hle
end
/--
The p-adic norm respects the triangle inequality: the norm of `p + q` is at most the norm of `p`
plus the norm of `q`.
-/
theorem triangle_ineq (q r : ℚ) : padic_norm p (q + r) ≤ padic_norm p q + padic_norm p r :=
calc padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) : padic_norm.nonarchimedean p
... ≤ padic_norm p q + padic_norm p r :
max_le_add_of_nonneg (padic_norm.nonneg p _) (padic_norm.nonneg p _)
/--
The p-adic norm of a difference is at most the max of each component. Restates the archimedean
property of the p-adic norm.
-/
protected theorem sub {q r : ℚ} : padic_norm p (q - r) ≤ max (padic_norm p q) (padic_norm p r) :=
by rw [sub_eq_add_neg, ←padic_norm.neg p r]; apply padic_norm.nonarchimedean
/--
If the p-adic norms of `q` and `r` are different, then the norm of `q + r` is equal to the max of
the norms of `q` and `r`.
-/
lemma add_eq_max_of_ne {q r : ℚ} (hne : padic_norm p q ≠ padic_norm p r) :
padic_norm p (q + r) = max (padic_norm p q) (padic_norm p r) :=
begin
wlog hle := le_total (padic_norm p r) (padic_norm p q) using [q r],
have hlt : padic_norm p r < padic_norm p q, from lt_of_le_of_ne hle hne.symm,
have : padic_norm p q ≤ max (padic_norm p (q + r)) (padic_norm p r), from calc
padic_norm p q = padic_norm p (q + r - r) : by congr; ring
... ≤ max (padic_norm p (q + r)) (padic_norm p (-r)) : padic_norm.nonarchimedean p
... = max (padic_norm p (q + r)) (padic_norm p r) : by simp,
have hnge : padic_norm p r ≤ padic_norm p (q + r),
{ apply le_of_not_gt,
intro hgt,
rw max_eq_right_of_lt hgt at this,
apply not_lt_of_ge this,
assumption },
have : padic_norm p q ≤ padic_norm p (q + r), by rwa [max_eq_left hnge] at this,
apply _root_.le_antisymm,
{ apply padic_norm.nonarchimedean p },
{ rw max_eq_left_of_lt hlt,
assumption }
end
/--
The p-adic norm is an absolute value: positive-definite and multiplicative, satisfying the triangle
inequality.
-/
instance : is_absolute_value (padic_norm p) :=
{ abv_nonneg := padic_norm.nonneg p,
abv_eq_zero :=
begin
intros,
constructor; intro,
{ apply zero_of_padic_norm_eq_zero p, assumption },
{ simp [*] }
end,
abv_add := padic_norm.triangle_ineq p,
abv_mul := padic_norm.mul p }
/--
If `p^n` divides an integer `z`, then the p-adic norm of `z` is at most `p^(-n)`.
-/
lemma le_of_dvd {n : ℕ} {z : ℤ} (hd : ↑(p^n) ∣ z) : padic_norm p z ≤ ↑p ^ (-n : ℤ) :=
begin
unfold padic_norm, split_ifs with hz hz,
{ apply fpow_nonneg_of_nonneg,
exact_mod_cast le_of_lt hp.pos },
{ apply fpow_le_of_le,
exact_mod_cast le_of_lt hp.one_lt,
apply neg_le_neg,
rw padic_val_rat_of_int _ hp.ne_one _,
{ norm_cast,
rw [← enat.coe_le_coe, enat.coe_get],
apply multiplicity.le_multiplicity_of_pow_dvd,
exact_mod_cast hd },
{ exact_mod_cast hz }},
end
end padic_norm
end padic_norm
|
d6e9178fe73d619df20b013210957664ca3b831c | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/order/lattice.lean | 5eee6deecf50b8224e36d50bbbea61108795ea97 | [
"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 | 18,103 | 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
Defines the inf/sup (semi)-lattice with optionally top/bot type class hierarchy.
-/
import order.basic
set_option old_structure_cmd true
universes u v w
variables {α : Type u} {β : Type v}
-- TODO: move this eventually, if we decide to use them
attribute [ematch] le_trans lt_of_le_of_lt lt_of_lt_of_le lt_trans
section
-- TODO: this seems crazy, but it also seems to work reasonably well
@[ematch] theorem le_antisymm' [partial_order α] : ∀ {a b : α}, (: a ≤ b :) → b ≤ a → a = b :=
@le_antisymm _ _
end
/- TODO: automatic construction of dual definitions / theorems -/
reserve infixl ` ⊓ `:70
reserve infixl ` ⊔ `:65
/-- Typeclass for the `⊔` (`\lub`) notation -/
class has_sup (α : Type u) := (sup : α → α → α)
/-- Typeclass for the `⊓` (`\glb`) notation -/
class has_inf (α : Type u) := (inf : α → α → α)
infix ⊔ := has_sup.sup
infix ⊓ := has_inf.inf
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A `semilattice_sup` is a join-semilattice, that is, a partial order
with a join (a.k.a. lub / least upper bound, sup / supremum) operation
`⊔` which is the least element larger than both factors. -/
class semilattice_sup (α : Type u) extends has_sup α, partial_order α :=
(le_sup_left : ∀ a b : α, a ≤ a ⊔ b)
(le_sup_right : ∀ a b : α, b ≤ a ⊔ b)
(sup_le : ∀ a b c : α, a ≤ c → b ≤ c → a ⊔ b ≤ c)
end prio
section semilattice_sup
variables [semilattice_sup α] {a b c d : α}
@[simp] theorem le_sup_left : a ≤ a ⊔ b :=
semilattice_sup.le_sup_left a b
@[ematch] theorem le_sup_left' : a ≤ (: a ⊔ b :) :=
le_sup_left
@[simp] theorem le_sup_right : b ≤ a ⊔ b :=
semilattice_sup.le_sup_right a b
@[ematch] theorem le_sup_right' : b ≤ (: a ⊔ b :) :=
le_sup_right
theorem le_sup_left_of_le (h : c ≤ a) : c ≤ a ⊔ b :=
le_trans h le_sup_left
theorem le_sup_right_of_le (h : c ≤ b) : c ≤ a ⊔ b :=
le_trans h le_sup_right
theorem sup_le : a ≤ c → b ≤ c → a ⊔ b ≤ c :=
semilattice_sup.sup_le a b c
@[simp] theorem sup_le_iff : a ⊔ b ≤ c ↔ a ≤ c ∧ b ≤ c :=
⟨assume h : a ⊔ b ≤ c, ⟨le_trans le_sup_left h, le_trans le_sup_right h⟩,
assume ⟨h₁, h₂⟩, sup_le h₁ h₂⟩
@[simp] theorem sup_eq_left : a ⊔ b = a ↔ b ≤ a :=
le_antisymm_iff.trans $ by simp [le_refl]
theorem sup_of_le_left (h : b ≤ a) : a ⊔ b = a :=
sup_eq_left.2 h
@[simp] theorem left_eq_sup : a = a ⊔ b ↔ b ≤ a :=
eq_comm.trans sup_eq_left
@[simp] theorem sup_eq_right : a ⊔ b = b ↔ a ≤ b :=
le_antisymm_iff.trans $ by simp [le_refl]
theorem sup_of_le_right (h : a ≤ b) : a ⊔ b = b :=
sup_eq_right.2 h
@[simp] theorem right_eq_sup : b = a ⊔ b ↔ a ≤ b :=
eq_comm.trans sup_eq_right
theorem sup_le_sup (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊔ c ≤ b ⊔ d :=
sup_le (le_sup_left_of_le h₁) (le_sup_right_of_le h₂)
theorem sup_le_sup_left (h₁ : a ≤ b) (c) : c ⊔ a ≤ c ⊔ b :=
sup_le_sup (le_refl _) h₁
theorem sup_le_sup_right (h₁ : a ≤ b) (c) : a ⊔ c ≤ b ⊔ c :=
sup_le_sup h₁ (le_refl _)
theorem le_of_sup_eq (h : a ⊔ b = b) : a ≤ b :=
by { rw ← h, simp }
lemma sup_ind [is_total α (≤)] (a b : α) {p : α → Prop} (ha : p a) (hb : p b) : p (a ⊔ b) :=
(is_total.total a b).elim (λ h : a ≤ b, by rwa sup_eq_right.2 h) (λ h, by rwa sup_eq_left.2 h)
@[simp] lemma sup_lt_iff [is_total α (≤)] {a b c : α} : b ⊔ c < a ↔ b < a ∧ c < a :=
⟨λ h, ⟨lt_of_le_of_lt le_sup_left h, lt_of_le_of_lt le_sup_right h⟩,
λ h, sup_ind b c h.1 h.2⟩
@[simp] theorem sup_idem : a ⊔ a = a :=
by apply le_antisymm; simp
instance sup_is_idempotent : is_idempotent α (⊔) := ⟨@sup_idem _ _⟩
theorem sup_comm : a ⊔ b = b ⊔ a :=
by apply le_antisymm; simp
instance sup_is_commutative : is_commutative α (⊔) := ⟨@sup_comm _ _⟩
theorem sup_assoc : a ⊔ b ⊔ c = a ⊔ (b ⊔ c) :=
le_antisymm
(sup_le
(sup_le le_sup_left (le_sup_right_of_le le_sup_left))
(le_sup_right_of_le le_sup_right))
(sup_le
(le_sup_left_of_le le_sup_left)
(sup_le (le_sup_left_of_le le_sup_right) le_sup_right))
instance sup_is_associative : is_associative α (⊔) := ⟨@sup_assoc _ _⟩
@[simp] lemma sup_left_idem : a ⊔ (a ⊔ b) = a ⊔ b :=
by rw [← sup_assoc, sup_idem]
@[simp] lemma sup_right_idem : (a ⊔ b) ⊔ b = a ⊔ b :=
by rw [sup_assoc, sup_idem]
lemma sup_left_comm (a b c : α) : a ⊔ (b ⊔ c) = b ⊔ (a ⊔ c) :=
by rw [← sup_assoc, ← sup_assoc, @sup_comm α _ a]
lemma forall_le_or_exists_lt_sup (a : α) : (∀b, b ≤ a) ∨ (∃b, a < b) :=
suffices (∃b, ¬b ≤ a) → (∃b, a < b),
by rwa [classical.or_iff_not_imp_left, classical.not_forall],
assume ⟨b, hb⟩,
⟨a ⊔ b, lt_of_le_of_ne le_sup_left $ mt left_eq_sup.1 hb⟩
theorem semilattice_sup.ext_sup {α} {A B : semilattice_sup α}
(H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y)
(x y : α) : (by haveI := A; exact (x ⊔ y)) = x ⊔ y :=
eq_of_forall_ge_iff $ λ c,
by simp only [sup_le_iff]; rw [← H, @sup_le_iff α A, H, H]
theorem semilattice_sup.ext {α} {A B : semilattice_sup α}
(H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B :=
begin
have := partial_order.ext H,
have ss := funext (λ x, funext $ semilattice_sup.ext_sup H x),
casesI A, casesI B,
injection this; congr'
end
end semilattice_sup
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A `semilattice_inf` is a meet-semilattice, that is, a partial order
with a meet (a.k.a. glb / greatest lower bound, inf / infimum) operation
`⊓` which is the greatest element smaller than both factors. -/
class semilattice_inf (α : Type u) extends has_inf α, partial_order α :=
(inf_le_left : ∀ a b : α, a ⊓ b ≤ a)
(inf_le_right : ∀ a b : α, a ⊓ b ≤ b)
(le_inf : ∀ a b c : α, a ≤ b → a ≤ c → a ≤ b ⊓ c)
end prio
section semilattice_inf
variables [semilattice_inf α] {a b c d : α}
@[simp] theorem inf_le_left : a ⊓ b ≤ a :=
semilattice_inf.inf_le_left a b
@[ematch] theorem inf_le_left' : (: a ⊓ b :) ≤ a :=
semilattice_inf.inf_le_left a b
@[simp] theorem inf_le_right : a ⊓ b ≤ b :=
semilattice_inf.inf_le_right a b
@[ematch] theorem inf_le_right' : (: a ⊓ b :) ≤ b :=
semilattice_inf.inf_le_right a b
theorem le_inf : a ≤ b → a ≤ c → a ≤ b ⊓ c :=
semilattice_inf.le_inf a b c
theorem inf_le_left_of_le (h : a ≤ c) : a ⊓ b ≤ c :=
le_trans inf_le_left h
theorem inf_le_right_of_le (h : b ≤ c) : a ⊓ b ≤ c :=
le_trans inf_le_right h
@[simp] theorem le_inf_iff : a ≤ b ⊓ c ↔ a ≤ b ∧ a ≤ c :=
⟨assume h : a ≤ b ⊓ c, ⟨le_trans h inf_le_left, le_trans h inf_le_right⟩,
assume ⟨h₁, h₂⟩, le_inf h₁ h₂⟩
@[simp] theorem inf_eq_left : a ⊓ b = a ↔ a ≤ b :=
le_antisymm_iff.trans $ by simp [le_refl]
theorem inf_of_le_left (h : a ≤ b) : a ⊓ b = a :=
inf_eq_left.2 h
@[simp] theorem left_eq_inf : a = a ⊓ b ↔ a ≤ b :=
eq_comm.trans inf_eq_left
@[simp] theorem inf_eq_right : a ⊓ b = b ↔ b ≤ a :=
le_antisymm_iff.trans $ by simp [le_refl]
theorem inf_of_le_right (h : b ≤ a) : a ⊓ b = b :=
inf_eq_right.2 h
@[simp] theorem right_eq_inf : b = a ⊓ b ↔ b ≤ a :=
eq_comm.trans inf_eq_right
theorem inf_le_inf (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊓ c ≤ b ⊓ d :=
le_inf (inf_le_left_of_le h₁) (inf_le_right_of_le h₂)
lemma inf_le_inf_right (a : α) {b c : α} (h : b ≤ c) : b ⊓ a ≤ c ⊓ a :=
inf_le_inf h (le_refl _)
lemma inf_le_inf_left (a : α) {b c : α} (h : b ≤ c) : a ⊓ b ≤ a ⊓ c :=
inf_le_inf (le_refl _) h
theorem le_of_inf_eq (h : a ⊓ b = a) : a ≤ b :=
by { rw ← h, simp }
lemma inf_ind [is_total α (≤)] (a b : α) {p : α → Prop} (ha : p a) (hb : p b) : p (a ⊓ b) :=
(is_total.total a b).elim (λ h : a ≤ b, by rwa inf_eq_left.2 h) (λ h, by rwa inf_eq_right.2 h)
@[simp] lemma lt_inf_iff [is_total α (≤)] {a b c : α} : a < b ⊓ c ↔ a < b ∧ a < c :=
⟨λ h, ⟨lt_of_lt_of_le h inf_le_left, lt_of_lt_of_le h inf_le_right⟩,
λ h, inf_ind b c h.1 h.2⟩
@[simp] theorem inf_idem : a ⊓ a = a :=
by apply le_antisymm; simp
instance inf_is_idempotent : is_idempotent α (⊓) := ⟨@inf_idem _ _⟩
theorem inf_comm : a ⊓ b = b ⊓ a :=
by apply le_antisymm; simp
instance inf_is_commutative : is_commutative α (⊓) := ⟨@inf_comm _ _⟩
theorem inf_assoc : a ⊓ b ⊓ c = a ⊓ (b ⊓ c) :=
le_antisymm
(le_inf
(inf_le_left_of_le inf_le_left)
(le_inf (inf_le_left_of_le inf_le_right) inf_le_right))
(le_inf
(le_inf inf_le_left (inf_le_right_of_le inf_le_left))
(inf_le_right_of_le inf_le_right))
instance inf_is_associative : is_associative α (⊓) := ⟨@inf_assoc _ _⟩
@[simp] lemma inf_left_idem : a ⊓ (a ⊓ b) = a ⊓ b :=
by rw [← inf_assoc, inf_idem]
@[simp] lemma inf_right_idem : (a ⊓ b) ⊓ b = a ⊓ b :=
by rw [inf_assoc, inf_idem]
lemma inf_left_comm (a b c : α) : a ⊓ (b ⊓ c) = b ⊓ (a ⊓ c) :=
by rw [← inf_assoc, ← inf_assoc, @inf_comm α _ a]
lemma forall_le_or_exists_lt_inf (a : α) : (∀b, a ≤ b) ∨ (∃b, b < a) :=
suffices (∃b, ¬a ≤ b) → (∃b, b < a),
by rwa [classical.or_iff_not_imp_left, classical.not_forall],
assume ⟨b, hb⟩,
have a ⊓ b ≠ a, from assume eq, hb $ eq ▸ inf_le_right,
⟨a ⊓ b, lt_of_le_of_ne inf_le_left ‹a ⊓ b ≠ a›⟩
theorem semilattice_inf.ext_inf {α} {A B : semilattice_inf α}
(H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y)
(x y : α) : (by haveI := A; exact (x ⊓ y)) = x ⊓ y :=
eq_of_forall_le_iff $ λ c,
by simp only [le_inf_iff]; rw [← H, @le_inf_iff α A, H, H]
theorem semilattice_inf.ext {α} {A B : semilattice_inf α}
(H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B :=
begin
have := partial_order.ext H,
have ss := funext (λ x, funext $ semilattice_inf.ext_inf H x),
casesI A, casesI B,
injection this; congr'
end
end semilattice_inf
/- Lattices -/
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A lattice is a join-semilattice which is also a meet-semilattice. -/
class lattice (α : Type u) extends semilattice_sup α, semilattice_inf α
end prio
section lattice
variables [lattice α] {a b c d : α}
/- Distributivity laws -/
/- TODO: better names? -/
theorem sup_inf_le : a ⊔ (b ⊓ c) ≤ (a ⊔ b) ⊓ (a ⊔ c) :=
le_inf (sup_le_sup_left inf_le_left _) (sup_le_sup_left inf_le_right _)
theorem le_inf_sup : (a ⊓ b) ⊔ (a ⊓ c) ≤ a ⊓ (b ⊔ c) :=
sup_le (inf_le_inf_left _ le_sup_left) (inf_le_inf_left _ le_sup_right)
theorem inf_sup_self : a ⊓ (a ⊔ b) = a :=
by simp
theorem sup_inf_self : a ⊔ (a ⊓ b) = a :=
by simp
theorem lattice.ext {α} {A B : lattice α}
(H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B :=
begin
have SS : @lattice.to_semilattice_sup α A =
@lattice.to_semilattice_sup α B := semilattice_sup.ext H,
have II := semilattice_inf.ext H,
casesI A, casesI B,
injection SS; injection II; congr'
end
end lattice
section prio
set_option default_priority 100 -- see Note [default priority]
/-- A distributive lattice is a lattice that satisfies any of four
equivalent distribution properties (of sup over inf or inf over sup,
on the left or right). A classic example of a distributive lattice
is the lattice of subsets of a set, and in fact this example is
generic in the sense that every distributive lattice is realizable
as a sublattice of a powerset lattice. -/
class distrib_lattice α extends lattice α :=
(le_sup_inf : ∀x y z : α, (x ⊔ y) ⊓ (x ⊔ z) ≤ x ⊔ (y ⊓ z))
end prio
section distrib_lattice
variables [distrib_lattice α] {x y z : α}
theorem le_sup_inf : ∀{x y z : α}, (x ⊔ y) ⊓ (x ⊔ z) ≤ x ⊔ (y ⊓ z) :=
distrib_lattice.le_sup_inf
theorem sup_inf_left : x ⊔ (y ⊓ z) = (x ⊔ y) ⊓ (x ⊔ z) :=
le_antisymm sup_inf_le le_sup_inf
theorem sup_inf_right : (y ⊓ z) ⊔ x = (y ⊔ x) ⊓ (z ⊔ x) :=
by simp only [sup_inf_left, λy:α, @sup_comm α _ y x, eq_self_iff_true]
theorem inf_sup_left : x ⊓ (y ⊔ z) = (x ⊓ y) ⊔ (x ⊓ z) :=
calc x ⊓ (y ⊔ z) = (x ⊓ (x ⊔ z)) ⊓ (y ⊔ z) : by rw [inf_sup_self]
... = x ⊓ ((x ⊓ y) ⊔ z) : by simp only [inf_assoc, sup_inf_right, eq_self_iff_true]
... = (x ⊔ (x ⊓ y)) ⊓ ((x ⊓ y) ⊔ z) : by rw [sup_inf_self]
... = ((x ⊓ y) ⊔ x) ⊓ ((x ⊓ y) ⊔ z) : by rw [sup_comm]
... = (x ⊓ y) ⊔ (x ⊓ z) : by rw [sup_inf_left]
theorem inf_sup_right : (y ⊔ z) ⊓ x = (y ⊓ x) ⊔ (z ⊓ x) :=
by simp only [inf_sup_left, λy:α, @inf_comm α _ y x, eq_self_iff_true]
lemma le_of_inf_le_sup_le (h₁ : x ⊓ z ≤ y ⊓ z) (h₂ : x ⊔ z ≤ y ⊔ z) : x ≤ y :=
calc x ≤ (y ⊓ z) ⊔ x : le_sup_right
... = (y ⊔ x) ⊓ (x ⊔ z) : by rw [sup_inf_right, @sup_comm _ _ x]
... ≤ (y ⊔ x) ⊓ (y ⊔ z) : inf_le_inf_left _ h₂
... = y ⊔ (x ⊓ z) : sup_inf_left.symm
... ≤ y ⊔ (y ⊓ z) : sup_le_sup_left h₁ _
... ≤ _ : sup_le (le_refl y) inf_le_left
lemma eq_of_inf_eq_sup_eq {α : Type u} [distrib_lattice α] {a b c : α}
(h₁ : b ⊓ a = c ⊓ a) (h₂ : b ⊔ a = c ⊔ a) : b = c :=
le_antisymm
(le_of_inf_le_sup_le (le_of_eq h₁) (le_of_eq h₂))
(le_of_inf_le_sup_le (le_of_eq h₁.symm) (le_of_eq h₂.symm))
end distrib_lattice
/- Lattices derived from linear orders -/
@[priority 100] -- see Note [lower instance priority]
instance lattice_of_decidable_linear_order {α : Type u} [o : decidable_linear_order α] : lattice α :=
{ sup := max,
le_sup_left := le_max_left,
le_sup_right := le_max_right,
sup_le := assume a b c, max_le,
inf := min,
inf_le_left := min_le_left,
inf_le_right := min_le_right,
le_inf := assume a b c, le_min,
..o }
theorem sup_eq_max [decidable_linear_order α] {x y : α} : x ⊔ y = max x y := rfl
theorem inf_eq_min [decidable_linear_order α] {x y : α} : x ⊓ y = min x y := rfl
@[priority 100] -- see Note [lower instance priority]
instance distrib_lattice_of_decidable_linear_order {α : Type u} [o : decidable_linear_order α] :
distrib_lattice α :=
{ le_sup_inf := assume a b c,
match le_total b c with
| or.inl h := inf_le_left_of_le $ sup_le_sup_left (le_inf (le_refl b) h) _
| or.inr h := inf_le_right_of_le $ sup_le_sup_left (le_inf h (le_refl c)) _
end,
..lattice_of_decidable_linear_order }
instance nat.distrib_lattice : distrib_lattice ℕ :=
by apply_instance
namespace monotone
lemma le_map_sup [semilattice_sup α] [semilattice_sup β]
{f : α → β} (h : monotone f) (x y : α) :
f x ⊔ f y ≤ f (x ⊔ y) :=
sup_le (h le_sup_left) (h le_sup_right)
lemma map_sup [semilattice_sup α] [is_total α (≤)] [semilattice_sup β] {f : α → β}
(hf : monotone f) (x y : α) :
f (x ⊔ y) = f x ⊔ f y :=
(is_total.total x y).elim
(λ h : x ≤ y, by simp only [h, hf h, sup_of_le_right])
(λ h, by simp only [h, hf h, sup_of_le_left])
lemma map_inf_le [semilattice_inf α] [semilattice_inf β]
{f : α → β} (h : monotone f) (x y : α) :
f (x ⊓ y) ≤ f x ⊓ f y :=
le_inf (h inf_le_left) (h inf_le_right)
lemma map_inf [semilattice_inf α] [is_total α (≤)] [semilattice_inf β] {f : α → β}
(hf : monotone f) (x y : α) :
f (x ⊓ y) = f x ⊓ f y :=
(is_total.total x y).elim
(λ h : x ≤ y, by simp only [h, hf h, inf_of_le_left])
(λ h, by simp only [h, hf h, inf_of_le_right])
end monotone
namespace order_dual
variable (α)
instance [has_inf α] : has_sup (order_dual α) := ⟨((⊓) : α → α → α)⟩
instance [has_sup α] : has_inf (order_dual α) := ⟨((⊔) : α → α → α)⟩
instance [semilattice_inf α] : semilattice_sup (order_dual α) :=
{ le_sup_left := @inf_le_left α _,
le_sup_right := @inf_le_right α _,
sup_le := assume a b c hca hcb, @le_inf α _ _ _ _ hca hcb,
.. order_dual.partial_order α, .. order_dual.has_sup α }
instance [semilattice_sup α] : semilattice_inf (order_dual α) :=
{ inf_le_left := @le_sup_left α _,
inf_le_right := @le_sup_right α _,
le_inf := assume a b c hca hcb, @sup_le α _ _ _ _ hca hcb,
.. order_dual.partial_order α, .. order_dual.has_inf α }
instance [lattice α] : lattice (order_dual α) :=
{ .. order_dual.semilattice_sup α, .. order_dual.semilattice_inf α }
instance [distrib_lattice α] : distrib_lattice (order_dual α) :=
{ le_sup_inf := assume x y z, le_of_eq inf_sup_left.symm,
.. order_dual.lattice α }
end order_dual
namespace prod
variables (α β)
instance [has_sup α] [has_sup β] : has_sup (α × β) := ⟨λp q, ⟨p.1 ⊔ q.1, p.2 ⊔ q.2⟩⟩
instance [has_inf α] [has_inf β] : has_inf (α × β) := ⟨λp q, ⟨p.1 ⊓ q.1, p.2 ⊓ q.2⟩⟩
instance [semilattice_sup α] [semilattice_sup β] : semilattice_sup (α × β) :=
{ sup_le := assume a b c h₁ h₂, ⟨sup_le h₁.1 h₂.1, sup_le h₁.2 h₂.2⟩,
le_sup_left := assume a b, ⟨le_sup_left, le_sup_left⟩,
le_sup_right := assume a b, ⟨le_sup_right, le_sup_right⟩,
.. prod.partial_order α β, .. prod.has_sup α β }
instance [semilattice_inf α] [semilattice_inf β] : semilattice_inf (α × β) :=
{ le_inf := assume a b c h₁ h₂, ⟨le_inf h₁.1 h₂.1, le_inf h₁.2 h₂.2⟩,
inf_le_left := assume a b, ⟨inf_le_left, inf_le_left⟩,
inf_le_right := assume a b, ⟨inf_le_right, inf_le_right⟩,
.. prod.partial_order α β, .. prod.has_inf α β }
instance [lattice α] [lattice β] : lattice (α × β) :=
{ .. prod.semilattice_inf α β, .. prod.semilattice_sup α β }
instance [distrib_lattice α] [distrib_lattice β] : distrib_lattice (α × β) :=
{ le_sup_inf := assume a b c, ⟨le_sup_inf, le_sup_inf⟩,
.. prod.lattice α β }
end prod
|
35e058d824457d73758bd663b66ed8bbf8ca1524 | 271e26e338b0c14544a889c31c30b39c989f2e0f | /stage0/src/Init/Lean/Attributes.lean | b374846675cbe43279fa8fa454f3e2d68822e5b0 | [
"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 | 15,562 | 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.Scopes
import Init.Lean.Syntax
namespace Lean
inductive AttributeApplicationTime
| afterTypeChecking | afterCompilation | beforeElaboration
-- TODO: after we delete the old frontend, we should use `EIO` with a richer exception kind at AttributeImpl.
-- We must perform a similar modification at `PersistentEnvExtension`
structure AttributeImpl :=
(name : Name)
(descr : String)
(add (env : Environment) (decl : Name) (args : Syntax) (persistent : Bool) : IO Environment)
(addScoped (env : Environment) (decl : Name) (args : Syntax) : IO Environment
:= throw (IO.userError ("attribute '" ++ toString name ++ "' does not support scopes")))
(erase (env : Environment) (decl : Name) (persistent : Bool) : IO Environment
:= throw (IO.userError ("attribute '" ++ toString name ++ "' does not support removal")))
(activateScoped (env : Environment) (scope : Name) : IO Environment := pure env)
(pushScope (env : Environment) : IO Environment := pure env)
(popScope (env : Environment) : IO Environment := pure env)
(applicationTime := AttributeApplicationTime.afterTypeChecking)
instance AttributeImpl.inhabited : Inhabited AttributeImpl :=
⟨{ name := arbitrary _, descr := arbitrary _, add := fun env _ _ _ => pure env }⟩
def mkAttributeMapRef : IO (IO.Ref (HashMap Name AttributeImpl)) :=
IO.mkRef {}
@[init mkAttributeMapRef]
constant attributeMapRef : IO.Ref (HashMap Name AttributeImpl) := arbitrary _
def mkAttributeArrayRef : IO (IO.Ref (Array AttributeImpl)) :=
IO.mkRef #[]
@[init mkAttributeArrayRef]
constant attributeArrayRef : IO.Ref (Array AttributeImpl) := arbitrary _
/- Low level attribute registration function. -/
def registerAttribute (attr : AttributeImpl) : IO Unit := do
m ← attributeMapRef.get;
when (m.contains attr.name) $ throw (IO.userError ("invalid attribute declaration, '" ++ toString attr.name ++ "' has already been used"));
initializing ← IO.initializing;
unless initializing $ throw (IO.userError ("failed to register attribute, attributes can only be registered during initialization"));
attributeMapRef.modify (fun m => m.insert attr.name attr);
attributeArrayRef.modify (fun attrs => attrs.push attr)
/- Return true iff `n` is the name of a registered attribute. -/
@[export lean_is_attribute]
def isAttribute (n : Name) : IO Bool := do
m ← attributeMapRef.get; pure (m.contains n)
/- Return the name of all registered attributes. -/
def getAttributeNames : IO (List Name) := do
m ← attributeMapRef.get; pure $ m.fold (fun r n _ => n::r) []
def getAttributeImpl (attrName : Name) : IO AttributeImpl := do
m ← attributeMapRef.get;
match m.find? attrName with
| some attr => pure attr
| none => throw (IO.userError ("unknown attribute '" ++ toString attrName ++ "'"))
@[export lean_attribute_application_time]
def attributeApplicationTime (n : Name) : IO AttributeApplicationTime := do
attr ← getAttributeImpl n;
pure attr.applicationTime
namespace Environment
/- Add attribute `attr` to declaration `decl` with arguments `args`. If `persistent == true`, then attribute is saved on .olean file.
It throws an error when
- `attr` is not the name of an attribute registered in the system.
- `attr` does not support `persistent == false`.
- `args` is not valid for `attr`. -/
@[export lean_add_attribute]
def addAttribute (env : Environment) (decl : Name) (attrName : Name) (args : Syntax := Syntax.missing) (persistent := true) : IO Environment := do
attr ← getAttributeImpl attrName;
attr.add env decl args persistent
/- Add a scoped attribute `attr` to declaration `decl` with arguments `args` and scope `decl.getPrefix`.
Scoped attributes are always persistent.
It returns `Except.error` when
- `attr` is not the name of an attribute registered in the system.
- `attr` does not support scoped attributes.
- `args` is not valid for `attr`.
Remark: the attribute will not be activated if `decl` is not inside the current namespace `env.getNamespace`. -/
@[export lean_add_scoped_attribute]
def addScopedAttribute (env : Environment) (decl : Name) (attrName : Name) (args : Syntax := Syntax.missing) : IO Environment := do
attr ← getAttributeImpl attrName;
attr.addScoped env decl args
/- Remove attribute `attr` from declaration `decl`. The effect is the current scope.
It returns `Except.error` when
- `attr` is not the name of an attribute registered in the system.
- `attr` does not support erasure.
- `args` is not valid for `attr`. -/
@[export lean_erase_attribute]
def eraseAttribute (env : Environment) (decl : Name) (attrName : Name) (persistent := true) : IO Environment := do
attr ← getAttributeImpl attrName;
attr.erase env decl persistent
/- Activate the scoped attribute `attr` for all declarations in scope `scope`.
We use this function to implement the command `open foo`. -/
@[export lean_activate_scoped_attribute]
def activateScopedAttribute (env : Environment) (attrName : Name) (scope : Name) : IO Environment := do
attr ← getAttributeImpl attrName;
attr.activateScoped env scope
/- Activate all scoped attributes at `scope` -/
@[export lean_activate_scoped_attributes]
def activateScopedAttributes (env : Environment) (scope : Name) : IO Environment := do
attrs ← attributeArrayRef.get;
attrs.foldlM (fun env attr => attr.activateScoped env scope) env
/- We use this function to implement commands `namespace foo` and `section foo`.
It activates scoped attributes in the new resulting namespace. -/
@[export lean_push_scope]
def pushScope (env : Environment) (header : Name) (isNamespace : Bool) : IO Environment := do
let env := env.pushScopeCore header isNamespace;
let ns := env.getNamespace;
attrs ← attributeArrayRef.get;
attrs.foldlM (fun env attr => do env ← attr.pushScope env; if isNamespace then attr.activateScoped env ns else pure env) env
/- We use this function to implement commands `end foo` for closing namespaces and sections. -/
@[export lean_pop_scope]
def popScope (env : Environment) : IO Environment := do
let env := env.popScopeCore;
attrs ← attributeArrayRef.get;
attrs.foldlM (fun env attr => attr.popScope env) env
end Environment
/--
Tag attributes are simple and efficient. They are useful for marking declarations in the modules where
they were defined.
The startup cost for this kind of attribute is very small since `addImportedFn` is a constant function.
They provide the predicate `tagAttr.hasTag env decl` which returns true iff declaration `decl`
is tagged in the environment `env`. -/
structure TagAttribute :=
(attr : AttributeImpl)
(ext : PersistentEnvExtension Name Name NameSet)
def registerTagAttribute (name : Name) (descr : String) (validate : Environment → Name → Except String Unit := fun _ _ => Except.ok ()) : IO TagAttribute := do
ext : PersistentEnvExtension Name Name NameSet ← registerPersistentEnvExtension {
name := name,
mkInitial := pure {},
addImportedFn := fun _ _ => pure {},
addEntryFn := fun (s : NameSet) n => s.insert n,
exportEntriesFn := fun es =>
let r : Array Name := es.fold (fun a e => a.push e) #[];
r.qsort Name.quickLt,
statsFn := fun s => "tag attribute" ++ Format.line ++ "number of local entries: " ++ format s.size
};
let attrImpl : AttributeImpl := {
name := name,
descr := descr,
add := fun env decl args persistent => do
unless args.isMissing $ throw (IO.userError ("invalid attribute '" ++ toString name ++ "', unexpected argument"));
unless persistent $ throw (IO.userError ("invalid attribute '" ++ toString name ++ "', must be persistent"));
unless (env.getModuleIdxFor? decl).isNone $
throw (IO.userError ("invalid attribute '" ++ toString name ++ "', declaration is in an imported module"));
match validate env decl with
| Except.error msg => throw (IO.userError ("invalid attribute '" ++ toString name ++ "', " ++ msg))
| _ => pure $ ext.addEntry env decl
};
registerAttribute attrImpl;
pure { attr := attrImpl, ext := ext }
namespace TagAttribute
instance : Inhabited TagAttribute := ⟨{attr := arbitrary _, ext := arbitrary _}⟩
def hasTag (attr : TagAttribute) (env : Environment) (decl : Name) : Bool :=
match env.getModuleIdxFor? decl with
| some modIdx => (attr.ext.getModuleEntries env modIdx).binSearchContains decl Name.quickLt
| none => (attr.ext.getState env).contains decl
end TagAttribute
/--
A `TagAttribute` variant where we can attach parameters to attributes.
It is slightly more expensive and consumes a little bit more memory than `TagAttribute`.
They provide the function `pAttr.getParam env decl` which returns `some p` iff declaration `decl`
contains the attribute `pAttr` with parameter `p`. -/
structure ParametricAttribute (α : Type) :=
(attr : AttributeImpl)
(ext : PersistentEnvExtension (Name × α) (Name × α) (NameMap α))
def registerParametricAttribute {α : Type} [Inhabited α] (name : Name) (descr : String)
(getParam : Environment → Name → Syntax → Except String α)
(afterSet : Environment → Name → α → Except String Environment := fun env _ _ => Except.ok env) : IO (ParametricAttribute α) := do
ext : PersistentEnvExtension (Name × α) (Name × α) (NameMap α) ← registerPersistentEnvExtension {
name := name,
mkInitial := pure {},
addImportedFn := fun _ _ => pure {},
addEntryFn := fun (s : NameMap α) (p : Name × α) => s.insert p.1 p.2,
exportEntriesFn := fun m =>
let r : Array (Name × α) := m.fold (fun a n p => a.push (n, p)) #[];
r.qsort (fun a b => Name.quickLt a.1 b.1),
statsFn := fun s => "parametric attribute" ++ Format.line ++ "number of local entries: " ++ format s.size
};
let attrImpl : AttributeImpl := {
name := name,
descr := descr,
add := fun env decl args persistent => do
unless persistent $ throw (IO.userError ("invalid attribute '" ++ toString name ++ "', must be persistent"));
unless (env.getModuleIdxFor? decl).isNone $
throw (IO.userError ("invalid attribute '" ++ toString name ++ "', declaration is in an imported module"));
match getParam env decl args with
| Except.error msg => throw (IO.userError ("invalid attribute '" ++ toString name ++ "', " ++ msg))
| Except.ok val => do
let env := ext.addEntry env (decl, val);
match afterSet env decl val with
| Except.error msg => throw (IO.userError ("invalid attribute '" ++ toString name ++ "', " ++ msg))
| Except.ok env => pure env
};
registerAttribute attrImpl;
pure { attr := attrImpl, ext := ext }
namespace ParametricAttribute
instance {α : Type} : Inhabited (ParametricAttribute α) := ⟨{attr := arbitrary _, ext := arbitrary _}⟩
def getParam {α : Type} [Inhabited α] (attr : ParametricAttribute α) (env : Environment) (decl : Name) : Option α :=
match env.getModuleIdxFor? decl with
| some modIdx =>
match (attr.ext.getModuleEntries env modIdx).binSearch (decl, arbitrary _) (fun a b => Name.quickLt a.1 b.1) with
| some (_, val) => some val
| none => none
| none => (attr.ext.getState env).find decl
def setParam {α : Type} (attr : ParametricAttribute α) (env : Environment) (decl : Name) (param : α) : Except String Environment :=
if (env.getModuleIdxFor? decl).isSome then
Except.error ("invalid '" ++ toString attr.attr.name ++ "'.setParam, declaration is in an imported module")
else if ((attr.ext.getState env).find decl).isSome then
Except.error ("invalid '" ++ toString attr.attr.name ++ "'.setParam, attribute has already been set")
else
Except.ok (attr.ext.addEntry env (decl, param))
end ParametricAttribute
/-
Given a list `[a₁, ..., a_n]` of elements of type `α`, `EnumAttributes` provides an attribute `Attr_i` for
associating a value `a_i` with an declaration. `α` is usually an enumeration type.
Note that whenever we register an `EnumAttributes`, we create `n` attributes, but only one environment extension. -/
structure EnumAttributes (α : Type) :=
(attrs : List AttributeImpl)
(ext : PersistentEnvExtension (Name × α) (Name × α) (NameMap α))
def registerEnumAttributes {α : Type} [Inhabited α] (extName : Name) (attrDescrs : List (Name × String × α)) (validate : Environment → Name → α → Except String Unit := fun _ _ _ => Except.ok ()) (applicationTime := AttributeApplicationTime.afterTypeChecking) : IO (EnumAttributes α) := do
ext : PersistentEnvExtension (Name × α) (Name × α) (NameMap α) ← registerPersistentEnvExtension {
name := extName,
mkInitial := pure {},
addImportedFn := fun _ _ => pure {},
addEntryFn := fun (s : NameMap α) (p : Name × α) => s.insert p.1 p.2,
exportEntriesFn := fun m =>
let r : Array (Name × α) := m.fold (fun a n p => a.push (n, p)) #[];
r.qsort (fun a b => Name.quickLt a.1 b.1),
statsFn := fun s => "enumeration attribute extension" ++ Format.line ++ "number of local entries: " ++ format s.size
};
let attrs := attrDescrs.map $ fun ⟨name, descr, val⟩ => { AttributeImpl .
name := name,
descr := descr,
applicationTime := applicationTime,
add := fun env decl args persistent => do
unless persistent $ throw (IO.userError ("invalid attribute '" ++ toString name ++ "', must be persistent"));
unless (env.getModuleIdxFor? decl).isNone $
throw (IO.userError ("invalid attribute '" ++ toString name ++ "', declaration is in an imported module"));
match validate env decl val with
| Except.error msg => throw (IO.userError ("invalid attribute '" ++ toString name ++ "', " ++ msg))
| _ => pure $ ext.addEntry env (decl, val)
};
attrs.forM registerAttribute;
pure { ext := ext, attrs := attrs }
namespace EnumAttributes
instance {α : Type} : Inhabited (EnumAttributes α) := ⟨{attrs := [], ext := arbitrary _}⟩
def getValue {α : Type} [Inhabited α] (attr : EnumAttributes α) (env : Environment) (decl : Name) : Option α :=
match env.getModuleIdxFor? decl with
| some modIdx =>
match (attr.ext.getModuleEntries env modIdx).binSearch (decl, arbitrary _) (fun a b => Name.quickLt a.1 b.1) with
| some (_, val) => some val
| none => none
| none => (attr.ext.getState env).find decl
def setValue {α : Type} (attrs : EnumAttributes α) (env : Environment) (decl : Name) (val : α) : Except String Environment :=
if (env.getModuleIdxFor? decl).isSome then
Except.error ("invalid '" ++ toString attrs.ext.name ++ "'.setValue, declaration is in an imported module")
else if ((attrs.ext.getState env).find decl).isSome then
Except.error ("invalid '" ++ toString attrs.ext.name ++ "'.setValue, attribute has already been set")
else
Except.ok (attrs.ext.addEntry env (decl, val))
end EnumAttributes
/--
Helper function for converting a Syntax object representing attribute parameters into an identifier.
It returns `none` if the parameter is not a simple identifier.
Remark: in the future, attributes should define their own parsers, and we should use `match_syntax` to
decode the Syntax object. -/
def attrParamSyntaxToIdentifier (s : Syntax) : Option Name :=
match s with
| Syntax.node k args =>
if k == nullKind && args.size == 1 then match args.get! 0 with
| Syntax.ident _ _ id _ => some id
| _ => none
else
none
| _ => none
end Lean
|
4d8fb4b29097cb66b234c12b3e3bbd221321d8a6 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/algebra/order/monoid_lemmas.lean | fa106dc3e3f4d3244929b2cf642451682f253c8f | [
"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 | 28,011 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl, Damiano Testa
-/
import algebra.covariant_and_contravariant
import order.monotone
/-!
# Ordered monoids
This file develops the basics of ordered monoids.
## Implementation details
Unfortunately, the number of `'` appended to lemmas in this file
may differ between the multiplicative and the additive version of a lemma.
The reason is that we did not want to change existing names in the library.
## Remark
Almost no monoid is actually present in this file: most assumptions have been generalized to
`has_mul` or `mul_one_class`.
-/
-- TODO: If possible, uniformize lemma names, taking special care of `'`,
-- after the `ordered`-refactor is done.
open function
variables {α β : Type*}
section has_mul
variables [has_mul α]
section has_le
variables [has_le α]
/- The prime on this lemma is present only on the multiplicative version. The unprimed version
is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/
@[to_additive add_le_add_left]
lemma mul_le_mul_left' [covariant_class α α (*) (≤)] {b c : α} (bc : b ≤ c) (a : α) :
a * b ≤ a * c :=
covariant_class.elim _ bc
@[to_additive le_of_add_le_add_left]
lemma le_of_mul_le_mul_left' [contravariant_class α α (*) (≤)]
{a b c : α} (bc : a * b ≤ a * c) :
b ≤ c :=
contravariant_class.elim _ bc
/- The prime on this lemma is present only on the multiplicative version. The unprimed version
is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/
@[to_additive add_le_add_right]
lemma mul_le_mul_right' [covariant_class α α (swap (*)) (≤)]
{b c : α} (bc : b ≤ c) (a : α) :
b * a ≤ c * a :=
covariant_class.elim a bc
@[to_additive le_of_add_le_add_right]
lemma le_of_mul_le_mul_right' [contravariant_class α α (swap (*)) (≤)]
{a b c : α} (bc : b * a ≤ c * a) :
b ≤ c :=
contravariant_class.elim a bc
@[simp, to_additive]
lemma mul_le_mul_iff_left [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)]
(a : α) {b c : α} :
a * b ≤ a * c ↔ b ≤ c :=
rel_iff_cov α α (*) (≤) a
@[simp, to_additive]
lemma mul_le_mul_iff_right
[covariant_class α α (swap (*)) (≤)] [contravariant_class α α (swap (*)) (≤)]
(a : α) {b c : α} :
b * a ≤ c * a ↔ b ≤ c :=
rel_iff_cov α α (swap (*)) (≤) a
end has_le
section has_lt
variables [has_lt α]
@[simp, to_additive]
lemma mul_lt_mul_iff_left [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)]
(a : α) {b c : α} :
a * b < a * c ↔ b < c :=
rel_iff_cov α α (*) (<) a
@[simp, to_additive]
lemma mul_lt_mul_iff_right
[covariant_class α α (swap (*)) (<)] [contravariant_class α α (swap (*)) (<)]
(a : α) {b c : α} :
b * a < c * a ↔ b < c :=
rel_iff_cov α α (swap (*)) (<) a
@[to_additive add_lt_add_left]
lemma mul_lt_mul_left' [covariant_class α α (*) (<)] {b c : α} (bc : b < c) (a : α) :
a * b < a * c :=
covariant_class.elim _ bc
@[to_additive lt_of_add_lt_add_left]
lemma lt_of_mul_lt_mul_left' [contravariant_class α α (*) (<)]
{a b c : α} (bc : a * b < a * c) :
b < c :=
contravariant_class.elim _ bc
@[to_additive add_lt_add_right]
lemma mul_lt_mul_right' [covariant_class α α (swap (*)) (<)]
{b c : α} (bc : b < c) (a : α) :
b * a < c * a :=
covariant_class.elim a bc
@[to_additive lt_of_add_lt_add_right]
lemma lt_of_mul_lt_mul_right' [contravariant_class α α (swap (*)) (<)]
{a b c : α} (bc : b * a < c * a) :
b < c :=
contravariant_class.elim a bc
end has_lt
end has_mul
-- using one
section mul_one_class
variables [mul_one_class α]
section has_le
variables [has_le α]
@[simp, to_additive le_add_iff_nonneg_right]
lemma le_mul_iff_one_le_right'
[covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)]
(a : α) {b : α} : a ≤ a * b ↔ 1 ≤ b :=
iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a)
@[simp, to_additive add_le_iff_nonpos_right]
lemma mul_le_iff_le_one_right'
[covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)]
(a : α) {b : α} :
a * b ≤ a ↔ b ≤ 1 :=
iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a)
@[simp, to_additive le_add_iff_nonneg_left]
lemma le_mul_iff_one_le_left'
[covariant_class α α (swap (*)) (≤)] [contravariant_class α α (swap (*)) (≤)]
(a : α) {b : α} :
a ≤ b * a ↔ 1 ≤ b :=
iff.trans (by rw one_mul) (mul_le_mul_iff_right a)
@[simp, to_additive add_le_iff_nonpos_left]
lemma mul_le_iff_le_one_left'
[covariant_class α α (swap (*)) (≤)] [contravariant_class α α (swap (*)) (≤)]
{a b : α} :
a * b ≤ b ↔ a ≤ 1 :=
iff.trans (by rw one_mul) (mul_le_mul_iff_right b)
end has_le
lemma exists_square_le {α : Type*} [mul_one_class α] [linear_order α] [covariant_class α α (*) (<)]
(a : α) : ∃ (b : α), b * b ≤ a :=
begin
by_cases h : a < 1,
{ use a,
have : a*a < a*1,
exact mul_lt_mul_left' h a,
rw mul_one at this,
exact le_of_lt this },
{ use 1,
push_neg at h,
rwa mul_one }
end
section has_lt
variable [has_lt α]
@[to_additive lt_add_of_pos_right]
lemma lt_mul_of_one_lt_right'
[covariant_class α α (*) (<)]
(a : α) {b : α} (h : 1 < b) : a < a * b :=
calc a = a * 1 : (mul_one _).symm
... < a * b : mul_lt_mul_left' h a
@[simp, to_additive lt_add_iff_pos_right]
lemma lt_mul_iff_one_lt_right'
[covariant_class α α (*) (<)] [contravariant_class α α (*) (<)]
(a : α) {b : α} :
a < a * b ↔ 1 < b :=
iff.trans (by rw mul_one) (mul_lt_mul_iff_left a)
@[simp, to_additive add_lt_iff_neg_left]
lemma mul_lt_iff_lt_one_left'
[covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] {a b : α} :
a * b < a ↔ b < 1 :=
iff.trans (by rw mul_one) (mul_lt_mul_iff_left a)
@[simp, to_additive lt_add_iff_pos_left]
lemma lt_mul_iff_one_lt_left'
[covariant_class α α (swap (*)) (<)] [contravariant_class α α (swap (*)) (<)]
(a : α) {b : α} : a < b * a ↔ 1 < b :=
iff.trans (by rw one_mul) (mul_lt_mul_iff_right a)
@[simp, to_additive add_lt_iff_neg_right]
lemma mul_lt_iff_lt_one_right'
[covariant_class α α (swap (*)) (<)] [contravariant_class α α (swap (*)) (<)]
{a : α} (b : α) :
a * b < b ↔ a < 1 :=
iff.trans (by rw one_mul) (mul_lt_mul_iff_right b)
end has_lt
section preorder
variable [preorder α]
@[to_additive]
lemma mul_le_of_le_of_le_one [covariant_class α α (*) (≤)]
{a b c : α} (hbc : b ≤ c) (ha : a ≤ 1) : b * a ≤ c :=
calc b * a ≤ b * 1 : mul_le_mul_left' ha b
... = b : mul_one b
... ≤ c : hbc
alias mul_le_of_le_of_le_one ← mul_le_one'
attribute [to_additive add_nonpos] mul_le_one'
@[to_additive]
lemma lt_mul_of_lt_of_one_le [covariant_class α α (*) (≤)]
{a b c : α} (hbc : b < c) (ha : 1 ≤ a) : b < c * a :=
calc b < c : hbc
... = c * 1 : (mul_one c).symm
... ≤ c * a : mul_le_mul_left' ha c
@[to_additive]
lemma mul_lt_of_lt_of_le_one [covariant_class α α (*) (≤)]
{a b c : α} (hbc : b < c) (ha : a ≤ 1) : b * a < c :=
calc b * a ≤ b * 1 : mul_le_mul_left' ha b
... = b : mul_one b
... < c : hbc
@[to_additive]
lemma lt_mul_of_le_of_one_lt [covariant_class α α (*) (<)]
{a b c : α} (hbc : b ≤ c) (ha : 1 < a) : b < c * a :=
calc b ≤ c : hbc
... = c * 1 : (mul_one c).symm
... < c * a : mul_lt_mul_left' ha c
@[to_additive]
lemma mul_lt_of_le_one_of_lt [covariant_class α α (swap (*)) (≤)]
{a b c : α} (ha : a ≤ 1) (hb : b < c) : a * b < c :=
calc a * b ≤ 1 * b : mul_le_mul_right' ha b
... = b : one_mul b
... < c : hb
@[to_additive]
lemma mul_le_of_le_one_of_le [covariant_class α α (swap (*)) (≤)]
{a b c : α} (ha : a ≤ 1) (hbc : b ≤ c) :
a * b ≤ c :=
calc a * b ≤ 1 * b : mul_le_mul_right' ha b
... = b : one_mul b
... ≤ c : hbc
@[to_additive]
lemma le_mul_of_one_le_of_le [covariant_class α α (swap (*)) (≤)]
{a b c: α} (ha : 1 ≤ a) (hbc : b ≤ c) : b ≤ a * c :=
calc b ≤ c : hbc
... = 1 * c : (one_mul c).symm
... ≤ a * c : mul_le_mul_right' ha c
/--
Assume monotonicity on the `left`. The lemma assuming `right` is `right.mul_lt_one`. -/
@[to_additive]
lemma left.mul_lt_one [covariant_class α α (*) (<)]
{a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 :=
calc a * b < a * 1 : mul_lt_mul_left' hb a
... = a : mul_one a
... < 1 : ha
/--
Assume monotonicity on the `right`. The lemma assuming `left` is `left.mul_lt_one`. -/
@[to_additive]
lemma right.mul_lt_one [covariant_class α α (swap (*)) (<)]
{a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 :=
calc a * b < 1 * b : mul_lt_mul_right' ha b
... = b : one_mul b
... < 1 : hb
@[to_additive]
lemma mul_lt_of_le_of_lt_one
[covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)]
{a b c: α} (hbc : b ≤ c) (ha : a < 1) : b * a < c :=
calc b * a ≤ c * a : mul_le_mul_right' hbc a
... < c * 1 : mul_lt_mul_left' ha c
... = c : mul_one c
@[to_additive]
lemma mul_lt_of_lt_one_of_le [covariant_class α α (swap (*)) (<)]
{a b c : α} (ha : a < 1) (hbc : b ≤ c) : a * b < c :=
calc a * b < 1 * b : mul_lt_mul_right' ha b
... = b : one_mul b
... ≤ c : hbc
@[to_additive]
lemma lt_mul_of_one_lt_of_le [covariant_class α α (swap (*)) (<)]
{a b c : α} (ha : 1 < a) (hbc : b ≤ c) : b < a * c :=
calc b ≤ c : hbc
... = 1 * c : (one_mul c).symm
... < a * c : mul_lt_mul_right' ha c
/-- Assumes left covariance. -/
@[to_additive]
lemma le_mul_of_le_of_le_one [covariant_class α α (*) (≤)]
{a b c : α} (ha : c ≤ a) (hb : 1 ≤ b) : c ≤ a * b :=
calc c ≤ a : ha
... = a * 1 : (mul_one a).symm
... ≤ a * b : mul_le_mul_left' hb a
/- This lemma is present to mimick the name of an existing one. -/
@[to_additive add_nonneg]
lemma one_le_mul [covariant_class α α (*) (≤)]
{a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b :=
le_mul_of_le_of_le_one ha hb
/-- Assumes left covariance. -/
@[to_additive]
lemma lt_mul_of_lt_of_one_lt [covariant_class α α (*) (<)]
{a b c : α} (ha : c < a) (hb : 1 < b) : c < a * b :=
calc c < a : ha
... = a * 1 : (mul_one _).symm
... < a * b : mul_lt_mul_left' hb a
/-- Assumes left covariance. -/
@[to_additive]
lemma left.mul_lt_one_of_lt_of_lt_one [covariant_class α α (*) (<)]
{a b c : α} (ha : a < c) (hb : b < 1) : a * b < c :=
calc a * b < a * 1 : mul_lt_mul_left' hb a
... = a : mul_one a
... < c : ha
/-- Assumes right covariance. -/
@[to_additive]
lemma right.mul_lt_one_of_lt_of_lt_one [covariant_class α α (swap (*)) (<)]
{a b c : α} (ha : a < 1) (hb : b < c) : a * b < c :=
calc a * b < 1 * b : mul_lt_mul_right' ha b
... = b : one_mul b
... < c : hb
/-- Assumes right covariance. -/
@[to_additive right.add_nonneg]
lemma right.one_le_mul [covariant_class α α (swap (*)) (≤)]
{a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b :=
calc 1 ≤ b : hb
... = 1 * b : (one_mul b).symm
... ≤ a * b : mul_le_mul_right' ha b
/-- Assumes right covariance. -/
@[to_additive right.add_pos]
lemma right.one_lt_mul [covariant_class α α (swap (*)) (<)]
{b : α} (hb : 1 < b) {a: α} (ha : 1 < a) : 1 < a * b :=
calc 1 < b : hb
... = 1 * b : (one_mul _).symm
... < a * b : mul_lt_mul_right' ha b
end preorder
@[to_additive le_add_of_nonneg_right]
lemma le_mul_of_one_le_right' [has_le α] [covariant_class α α (*) (≤)] {a b : α} (h : 1 ≤ b) :
a ≤ a * b :=
calc a = a * 1 : (mul_one _).symm
... ≤ a * b : mul_le_mul_left' h a
@[to_additive add_le_of_nonpos_right]
lemma mul_le_of_le_one_right' [has_le α] [covariant_class α α (*) (≤)] {a b : α} (h : b ≤ 1) :
a * b ≤ a :=
calc a * b ≤ a * 1 : mul_le_mul_left' h a
... = a : mul_one a
end mul_one_class
@[to_additive]
lemma mul_left_cancel'' [semigroup α] [partial_order α]
[contravariant_class α α (*) (≤)] {a b c : α} (h : a * b = a * c) : b = c :=
(le_of_mul_le_mul_left' h.le).antisymm (le_of_mul_le_mul_left' h.ge)
@[to_additive]
lemma mul_right_cancel'' [semigroup α] [partial_order α]
[contravariant_class α α (swap (*)) (≤)] {a b c : α} (h : a * b = c * b) :
a = c :=
le_antisymm (le_of_mul_le_mul_right' h.le) (le_of_mul_le_mul_right' h.ge)
/- This is not instance, since we want to have an instance from `left_cancel_semigroup`s
to the appropriate `covariant_class`. -/
/-- A semigroup with a partial order and satisfying `left_cancel_semigroup`
(i.e. `a * c < b * c → a < b`) is a `left_cancel semigroup`. -/
@[to_additive
"An additive semigroup with a partial order and satisfying `left_cancel_add_semigroup`
(i.e. `c + a < c + b → a < b`) is a `left_cancel add_semigroup`."]
def contravariant.to_left_cancel_semigroup [semigroup α] [partial_order α]
[contravariant_class α α (*) (≤)] :
left_cancel_semigroup α :=
{ mul_left_cancel := λ a b c, mul_left_cancel''
..‹semigroup α› }
/- This is not instance, since we want to have an instance from `right_cancel_semigroup`s
to the appropriate `covariant_class`. -/
/-- A semigroup with a partial order and satisfying `right_cancel_semigroup`
(i.e. `a * c < b * c → a < b`) is a `right_cancel semigroup`. -/
@[to_additive
"An additive semigroup with a partial order and satisfying `right_cancel_add_semigroup`
(`a + c < b + c → a < b`) is a `right_cancel add_semigroup`."]
def contravariant.to_right_cancel_semigroup [semigroup α] [partial_order α]
[contravariant_class α α (swap (*)) (≤)] :
right_cancel_semigroup α :=
{ mul_right_cancel := λ a b c, mul_right_cancel''
..‹semigroup α› }
variables {a b c d : α}
section left
variables [preorder α]
section has_mul
variables [has_mul α]
@[to_additive]
lemma mul_lt_mul_of_lt_of_lt
[covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)]
(h₁ : a < b) (h₂ : c < d) : a * c < b * d :=
calc a * c < a * d : mul_lt_mul_left' h₂ a
... < b * d : mul_lt_mul_right' h₁ d
section contravariant_mul_lt_left_le_right
variables [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)]
@[to_additive]
lemma mul_lt_mul_of_le_of_lt
(h₁ : a ≤ b) (h₂ : c < d) : a * c < b * d :=
(mul_le_mul_right' h₁ _).trans_lt (mul_lt_mul_left' h₂ b)
@[to_additive add_lt_add]
lemma mul_lt_mul''' (h₁ : a < b) (h₂ : c < d) : a * c < b * d :=
mul_lt_mul_of_le_of_lt h₁.le h₂
end contravariant_mul_lt_left_le_right
@[to_additive] lemma mul_eq_mul_iff_eq_and_eq {α : Type*} [semigroup α] [partial_order α]
[contravariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)]
[covariant_class α α (*) (<)] [contravariant_class α α (swap (*)) (≤)]
{a b c d : α} (hac : a ≤ c) (hbd : b ≤ d) : a * b = c * d ↔ a = c ∧ b = d :=
begin
refine ⟨λ h, _, λ h, congr_arg2 (*) h.1 h.2⟩,
rcases hac.eq_or_lt with rfl | hac,
{ exact ⟨rfl, mul_left_cancel'' h⟩ },
rcases eq_or_lt_of_le hbd with rfl | hbd,
{ exact ⟨mul_right_cancel'' h, rfl⟩ },
exact ((mul_lt_mul''' hac hbd).ne h).elim,
end
variable [covariant_class α α (*) (≤)]
@[to_additive]
lemma mul_lt_of_mul_lt_left (h : a * b < c) (hle : d ≤ b) :
a * d < c :=
(mul_le_mul_left' hle a).trans_lt h
@[to_additive]
lemma mul_le_of_mul_le_left (h : a * b ≤ c) (hle : d ≤ b) :
a * d ≤ c :=
@act_rel_of_rel_of_act_rel _ _ _ (≤) _ ⟨λ _ _ _, le_trans⟩ a _ _ _ hle h
@[to_additive]
lemma lt_mul_of_lt_mul_left (h : a < b * c) (hle : c ≤ d) :
a < b * d :=
h.trans_le (mul_le_mul_left' hle b)
@[to_additive]
lemma le_mul_of_le_mul_left (h : a ≤ b * c) (hle : c ≤ d) :
a ≤ b * d :=
@rel_act_of_rel_of_rel_act _ _ _ (≤) _ ⟨λ _ _ _, le_trans⟩ b _ _ _ hle h
@[to_additive]
lemma mul_lt_mul_of_lt_of_le [covariant_class α α (swap (*)) (<)]
(h₁ : a < b) (h₂ : c ≤ d) : a * c < b * d :=
(mul_le_mul_left' h₂ _).trans_lt (mul_lt_mul_right' h₁ d)
end has_mul
/-! Here we start using properties of one, on the left. -/
section mul_one_class
variables [mul_one_class α] [covariant_class α α (*) (≤)]
@[to_additive]
lemma lt_of_mul_lt_of_one_le_left (h : a * b < c) (hle : 1 ≤ b) : a < c :=
(le_mul_of_one_le_right' hle).trans_lt h
@[to_additive]
lemma le_of_mul_le_of_one_le_left (h : a * b ≤ c) (hle : 1 ≤ b) : a ≤ c :=
(le_mul_of_one_le_right' hle).trans h
@[to_additive]
lemma lt_of_lt_mul_of_le_one_left (h : a < b * c) (hle : c ≤ 1) : a < b :=
h.trans_le (mul_le_of_le_one_right' hle)
@[to_additive]
lemma le_of_le_mul_of_le_one_left (h : a ≤ b * c) (hle : c ≤ 1) : a ≤ b :=
h.trans (mul_le_of_le_one_right' hle)
@[to_additive]
theorem mul_lt_of_lt_of_lt_one (bc : b < c) (a1 : a < 1) :
b * a < c :=
calc b * a ≤ b * 1 : mul_le_mul_left' a1.le _
... = b : mul_one b
... < c : bc
end mul_one_class
end left
section right
section preorder
variables [preorder α]
section has_mul
variables [has_mul α]
variable [covariant_class α α (swap (*)) (≤)]
@[to_additive]
lemma mul_lt_of_mul_lt_right (h : a * b < c) (hle : d ≤ a) :
d * b < c :=
(mul_le_mul_right' hle b).trans_lt h
@[to_additive]
lemma mul_le_of_mul_le_right (h : a * b ≤ c) (hle : d ≤ a) :
d * b ≤ c :=
(mul_le_mul_right' hle b).trans h
@[to_additive]
lemma lt_mul_of_lt_mul_right (h : a < b * c) (hle : b ≤ d) :
a < d * c :=
h.trans_le (mul_le_mul_right' hle c)
@[to_additive]
lemma le_mul_of_le_mul_right (h : a ≤ b * c) (hle : b ≤ d) :
a ≤ d * c :=
h.trans (mul_le_mul_right' hle c)
variable [covariant_class α α (*) (≤)]
@[to_additive add_le_add]
lemma mul_le_mul' (h₁ : a ≤ b) (h₂ : c ≤ d) : a * c ≤ b * d :=
(mul_le_mul_left' h₂ _).trans (mul_le_mul_right' h₁ d)
@[to_additive]
lemma mul_le_mul_three {e f : α} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) :
a * b * c ≤ d * e * f :=
mul_le_mul' (mul_le_mul' h₁ h₂) h₃
end has_mul
/-! Here we start using properties of one, on the right. -/
section mul_one_class
variables [mul_one_class α]
section le_right
variable [covariant_class α α (swap (*)) (≤)]
@[to_additive le_add_of_nonneg_left]
lemma le_mul_of_one_le_left' (h : 1 ≤ b) : a ≤ b * a :=
calc a = 1 * a : (one_mul a).symm
... ≤ b * a : mul_le_mul_right' h a
@[to_additive add_le_of_nonpos_left]
lemma mul_le_of_le_one_left' (h : b ≤ 1) : b * a ≤ a :=
calc b * a ≤ 1 * a : mul_le_mul_right' h a
... = a : one_mul a
@[to_additive]
lemma lt_of_mul_lt_of_one_le_right (h : a * b < c) (hle : 1 ≤ a) : b < c :=
(le_mul_of_one_le_left' hle).trans_lt h
@[to_additive]
lemma le_of_mul_le_of_one_le_right (h : a * b ≤ c) (hle : 1 ≤ a) : b ≤ c :=
(le_mul_of_one_le_left' hle).trans h
@[to_additive]
lemma lt_of_lt_mul_of_le_one_right (h : a < b * c) (hle : b ≤ 1) : a < c :=
h.trans_le (mul_le_of_le_one_left' hle)
@[to_additive]
lemma le_of_le_mul_of_le_one_right (h : a ≤ b * c) (hle : b ≤ 1) : a ≤ c :=
h.trans (mul_le_of_le_one_left' hle)
theorem mul_lt_of_lt_one_of_lt (a1 : a < 1) (bc : b < c) :
a * b < c :=
calc a * b ≤ 1 * b : mul_le_mul_right' a1.le _
... = b : one_mul b
... < c : bc
end le_right
section lt_right
@[to_additive lt_add_of_pos_left]
lemma lt_mul_of_one_lt_left' [covariant_class α α (swap (*)) (<)]
(a : α) {b : α} (h : 1 < b) : a < b * a :=
calc a = 1 * a : (one_mul _).symm
... < b * a : mul_lt_mul_right' h a
end lt_right
end mul_one_class
end preorder
end right
section preorder
variables [preorder α]
section mul_one_class
variables [mul_one_class α]
section covariant_left
variable [covariant_class α α (*) (≤)]
@[to_additive add_pos_of_pos_of_nonneg]
lemma one_lt_mul_of_lt_of_le' (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b :=
lt_of_lt_of_le ha $ le_mul_of_one_le_right' hb
@[to_additive add_pos]
lemma one_lt_mul' (ha : 1 < a) (hb : 1 < b) : 1 < a * b :=
one_lt_mul_of_lt_of_le' ha hb.le
@[to_additive]
lemma lt_mul_of_lt_of_one_le' (hbc : b < c) (ha : 1 ≤ a) :
b < c * a :=
hbc.trans_le $ le_mul_of_one_le_right' ha
@[to_additive]
lemma lt_mul_of_lt_of_one_lt' (hbc : b < c) (ha : 1 < a) :
b < c * a :=
lt_mul_of_lt_of_one_le' hbc ha.le
@[to_additive]
lemma le_mul_of_le_of_one_le (hbc : b ≤ c) (ha : 1 ≤ a) : b ≤ c * a :=
calc b ≤ c : hbc
... = c * 1 : (mul_one c).symm
... ≤ c * a : mul_le_mul_left' ha c
@[to_additive add_nonneg]
lemma one_le_mul_right (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b :=
calc 1 ≤ a : ha
... = a * 1 : (mul_one a).symm
... ≤ a * b : mul_le_mul_left' hb a
end covariant_left
section covariant_right
variable [covariant_class α α (swap (*)) (≤)]
@[to_additive add_pos_of_nonneg_of_pos]
lemma one_lt_mul_of_le_of_lt' (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b :=
lt_of_lt_of_le hb $ le_mul_of_one_le_left' ha
@[to_additive]
lemma lt_mul_of_one_le_of_lt (ha : 1 ≤ a) (hbc : b < c) : b < a * c :=
hbc.trans_le $ le_mul_of_one_le_left' ha
@[to_additive]
lemma lt_mul_of_one_lt_of_lt (ha : 1 < a) (hbc : b < c) : b < a * c :=
lt_mul_of_one_le_of_lt ha.le hbc
end covariant_right
end mul_one_class
end preorder
section partial_order
/-! Properties assuming `partial_order`. -/
variables [mul_one_class α] [partial_order α]
[covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)]
@[to_additive]
lemma mul_eq_one_iff' (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 :=
iff.intro
(assume hab : a * b = 1,
have a ≤ 1, from hab ▸ le_mul_of_le_of_one_le le_rfl hb,
have a = 1, from le_antisymm this ha,
have b ≤ 1, from hab ▸ le_mul_of_one_le_of_le ha le_rfl,
have b = 1, from le_antisymm this hb,
and.intro ‹a = 1› ‹b = 1›)
(assume ⟨ha', hb'⟩, by rw [ha', hb', mul_one])
end partial_order
section mono
variables [has_mul α] [preorder α] [preorder β] {f g : β → α}
@[to_additive monotone.const_add]
lemma monotone.const_mul' [covariant_class α α (*) (≤)] (hf : monotone f) (a : α) :
monotone (λ x, a * f x) :=
λ x y h, mul_le_mul_left' (hf h) a
@[to_additive monotone.add_const]
lemma monotone.mul_const' [covariant_class α α (swap (*)) (≤)]
(hf : monotone f) (a : α) : monotone (λ x, f x * a) :=
λ x y h, mul_le_mul_right' (hf h) a
/-- The product of two monotone functions is monotone. -/
@[to_additive monotone.add "The sum of two monotone functions is monotone."]
lemma monotone.mul' [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)]
(hf : monotone f) (hg : monotone g) : monotone (λ x, f x * g x) :=
λ x y h, mul_le_mul' (hf h) (hg h)
section left
variables [covariant_class α α (*) (<)]
@[to_additive strict_mono.const_add]
lemma strict_mono.const_mul' (hf : strict_mono f) (c : α) :
strict_mono (λ x, c * f x) :=
λ a b ab, mul_lt_mul_left' (hf ab) c
end left
section right
variables [covariant_class α α (swap (*)) (<)]
@[to_additive strict_mono.add_const]
lemma strict_mono.mul_const' (hf : strict_mono f) (c : α) :
strict_mono (λ x, f x * c) :=
λ a b ab, mul_lt_mul_right' (hf ab) c
end right
/-- The product of two strictly monotone functions is strictly monotone. -/
@[to_additive strict_mono.add
"The sum of two strictly monotone functions is strictly monotone."]
lemma strict_mono.mul' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (<)]
(hf : strict_mono f) (hg : strict_mono g) :
strict_mono (λ x, f x * g x) :=
λ a b ab, mul_lt_mul_of_lt_of_lt (hf ab) (hg ab)
/-- The product of a monotone function and a strictly monotone function is strictly monotone. -/
@[to_additive monotone.add_strict_mono
"The sum of a monotone function and a strictly monotone function is strictly monotone."]
lemma monotone.mul_strict_mono' [covariant_class α α (*) (<)] [covariant_class α α (swap (*)) (≤)]
{f g : β → α} (hf : monotone f) (hg : strict_mono g) :
strict_mono (λ x, f x * g x) :=
λ x y h, mul_lt_mul_of_le_of_lt (hf h.le) (hg h)
variables [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (<)]
/-- The product of a strictly monotone function and a monotone function is strictly monotone. -/
@[to_additive strict_mono.add_monotone
"The sum of a strictly monotone function and a monotone function is strictly monotone."]
lemma strict_mono.mul_monotone' (hf : strict_mono f) (hg : monotone g) :
strict_mono (λ x, f x * g x) :=
λ x y h, mul_lt_mul_of_lt_of_le (hf h) (hg h.le)
end mono
/--
An element `a : α` is `mul_le_cancellable` if `x ↦ a * x` is order-reflecting.
We will make a separate version of many lemmas that require `[contravariant_class α α (*) (≤)]` with
`mul_le_cancellable` assumptions instead. These lemmas can then be instantiated to specific types,
like `ennreal`, where we can replace the assumption `add_le_cancellable x` by `x ≠ ∞`.
-/
@[to_additive /-" An element `a : α` is `add_le_cancellable` if `x ↦ a + x` is order-reflecting.
We will make a separate version of many lemmas that require `[contravariant_class α α (+) (≤)]` with
`mul_le_cancellable` assumptions instead. These lemmas can then be instantiated to specific types,
like `ennreal`, where we can replace the assumption `add_le_cancellable x` by `x ≠ ∞`. "-/
]
def mul_le_cancellable [has_mul α] [has_le α] (a : α) : Prop :=
∀ ⦃b c⦄, a * b ≤ a * c → b ≤ c
@[to_additive]
lemma contravariant.mul_le_cancellable [has_mul α] [has_le α] [contravariant_class α α (*) (≤)]
{a : α} : mul_le_cancellable a :=
λ b c, le_of_mul_le_mul_left'
namespace mul_le_cancellable
@[to_additive]
protected lemma injective [has_mul α] [partial_order α] {a : α} (ha : mul_le_cancellable a) :
injective ((*) a) :=
λ b c h, le_antisymm (ha h.le) (ha h.ge)
@[to_additive]
protected lemma inj [has_mul α] [partial_order α] {a b c : α} (ha : mul_le_cancellable a) :
a * b = a * c ↔ b = c :=
ha.injective.eq_iff
@[to_additive]
protected lemma injective_left [comm_semigroup α] [partial_order α] {a : α}
(ha : mul_le_cancellable a) : injective (* a) :=
λ b c h, ha.injective $ by rwa [mul_comm a, mul_comm a]
@[to_additive]
protected lemma inj_left [comm_semigroup α] [partial_order α] {a b c : α}
(hc : mul_le_cancellable c) : a * c = b * c ↔ a = b :=
hc.injective_left.eq_iff
variable [has_le α]
@[to_additive]
protected lemma mul_le_mul_iff_left [has_mul α] [covariant_class α α (*) (≤)]
{a b c : α} (ha : mul_le_cancellable a) : a * b ≤ a * c ↔ b ≤ c :=
⟨λ h, ha h, λ h, mul_le_mul_left' h a⟩
@[to_additive]
protected lemma mul_le_mul_iff_right [comm_semigroup α] [covariant_class α α (*) (≤)]
{a b c : α} (ha : mul_le_cancellable a) : b * a ≤ c * a ↔ b ≤ c :=
by rw [mul_comm b, mul_comm c, ha.mul_le_mul_iff_left]
@[to_additive]
protected lemma le_mul_iff_one_le_right [mul_one_class α] [covariant_class α α (*) (≤)]
{a b : α} (ha : mul_le_cancellable a) : a ≤ a * b ↔ 1 ≤ b :=
iff.trans (by rw [mul_one]) ha.mul_le_mul_iff_left
@[to_additive]
protected lemma mul_le_iff_le_one_right [mul_one_class α] [covariant_class α α (*) (≤)]
{a b : α} (ha : mul_le_cancellable a) : a * b ≤ a ↔ b ≤ 1 :=
iff.trans (by rw [mul_one]) ha.mul_le_mul_iff_left
@[to_additive]
protected lemma le_mul_iff_one_le_left [comm_monoid α] [covariant_class α α (*) (≤)]
{a b : α} (ha : mul_le_cancellable a) : a ≤ b * a ↔ 1 ≤ b :=
by rw [mul_comm, ha.le_mul_iff_one_le_right]
@[to_additive]
protected lemma mul_le_iff_le_one_left [comm_monoid α] [covariant_class α α (*) (≤)]
{a b : α} (ha : mul_le_cancellable a) : b * a ≤ a ↔ b ≤ 1 :=
by rw [mul_comm, ha.mul_le_iff_le_one_right]
end mul_le_cancellable
|
c5a5c1edc74194efa0a0d4b6da52768170b0b635 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/analysis/special_functions/log/basic.lean | 9dd6c92a4169eaa8acffaac3a8f3ab1fca60e180 | [
"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 | 11,008 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne
-/
import analysis.special_functions.exp
/-!
# Real logarithm
In this file we define `real.log` to be the logarithm of a real number. As usual, we extend it from
its domain `(0, +∞)` to a globally defined function. We choose to do it so that `log 0 = 0` and
`log (-x) = log x`.
We prove some basic properties of this function and show that it is continuous.
## Tags
logarithm, continuity
-/
open set filter function
open_locale topological_space
noncomputable theory
namespace real
variables {x y : ℝ}
/-- The real logarithm function, equal to the inverse of the exponential for `x > 0`,
to `log |x|` for `x < 0`, and to `0` for `0`. We use this unconventional extension to
`(-∞, 0]` as it gives the formula `log (x * y) = log x + log y` for all nonzero `x` and `y`, and
the derivative of `log` is `1/x` away from `0`. -/
@[pp_nodot] noncomputable def log (x : ℝ) : ℝ :=
if hx : x = 0 then 0 else exp_order_iso.symm ⟨|x|, abs_pos.2 hx⟩
lemma log_of_ne_zero (hx : x ≠ 0) : log x = exp_order_iso.symm ⟨|x|, abs_pos.2 hx⟩ := dif_neg hx
lemma log_of_pos (hx : 0 < x) : log x = exp_order_iso.symm ⟨x, hx⟩ :=
by { rw [log_of_ne_zero hx.ne'], congr, exact abs_of_pos hx }
lemma exp_log_eq_abs (hx : x ≠ 0) : exp (log x) = |x| :=
by rw [log_of_ne_zero hx, ← coe_exp_order_iso_apply, order_iso.apply_symm_apply, subtype.coe_mk]
lemma exp_log (hx : 0 < x) : exp (log x) = x :=
by { rw exp_log_eq_abs hx.ne', exact abs_of_pos hx }
lemma exp_log_of_neg (hx : x < 0) : exp (log x) = -x :=
by { rw exp_log_eq_abs (ne_of_lt hx), exact abs_of_neg hx }
@[simp] lemma log_exp (x : ℝ) : log (exp x) = x :=
exp_injective $ exp_log (exp_pos x)
lemma surj_on_log : surj_on log (Ioi 0) univ :=
λ x _, ⟨exp x, exp_pos x, log_exp x⟩
lemma log_surjective : surjective log :=
λ x, ⟨exp x, log_exp x⟩
@[simp] lemma range_log : range log = univ :=
log_surjective.range_eq
@[simp] lemma log_zero : log 0 = 0 := dif_pos rfl
@[simp] lemma log_one : log 1 = 0 :=
exp_injective $ by rw [exp_log zero_lt_one, exp_zero]
@[simp] lemma log_abs (x : ℝ) : log (|x|) = log x :=
begin
by_cases h : x = 0,
{ simp [h] },
{ rw [← exp_eq_exp, exp_log_eq_abs h, exp_log_eq_abs (abs_pos.2 h).ne', abs_abs] }
end
@[simp] lemma log_neg_eq_log (x : ℝ) : log (-x) = log x :=
by rw [← log_abs x, ← log_abs (-x), abs_neg]
lemma surj_on_log' : surj_on log (Iio 0) univ :=
λ x _, ⟨-exp x, neg_lt_zero.2 $ exp_pos x, by rw [log_neg_eq_log, log_exp]⟩
lemma log_mul (hx : x ≠ 0) (hy : y ≠ 0) : log (x * y) = log x + log y :=
exp_injective $
by rw [exp_log_eq_abs (mul_ne_zero hx hy), exp_add, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_mul]
lemma log_div (hx : x ≠ 0) (hy : y ≠ 0) : log (x / y) = log x - log y :=
exp_injective $
by rw [exp_log_eq_abs (div_ne_zero hx hy), exp_sub, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_div]
@[simp] lemma log_inv (x : ℝ) : log (x⁻¹) = -log x :=
begin
by_cases hx : x = 0, { simp [hx] },
rw [← exp_eq_exp, exp_log_eq_abs (inv_ne_zero hx), exp_neg, exp_log_eq_abs hx, abs_inv]
end
lemma log_le_log (h : 0 < x) (h₁ : 0 < y) : log x ≤ log y ↔ x ≤ y :=
by rw [← exp_le_exp, exp_log h, exp_log h₁]
lemma log_lt_log (hx : 0 < x) : x < y → log x < log y :=
by { intro h, rwa [← exp_lt_exp, exp_log hx, exp_log (lt_trans hx h)] }
lemma log_lt_log_iff (hx : 0 < x) (hy : 0 < y) : log x < log y ↔ x < y :=
by { rw [← exp_lt_exp, exp_log hx, exp_log hy] }
lemma log_le_iff_le_exp (hx : 0 < x) : log x ≤ y ↔ x ≤ exp y := by rw [←exp_le_exp, exp_log hx]
lemma log_lt_iff_lt_exp (hx : 0 < x) : log x < y ↔ x < exp y := by rw [←exp_lt_exp, exp_log hx]
lemma le_log_iff_exp_le (hy : 0 < y) : x ≤ log y ↔ exp x ≤ y := by rw [←exp_le_exp, exp_log hy]
lemma lt_log_iff_exp_lt (hy : 0 < y) : x < log y ↔ exp x < y := by rw [←exp_lt_exp, exp_log hy]
lemma log_pos_iff (hx : 0 < x) : 0 < log x ↔ 1 < x :=
by { rw ← log_one, exact log_lt_log_iff zero_lt_one hx }
lemma log_pos (hx : 1 < x) : 0 < log x :=
(log_pos_iff (lt_trans zero_lt_one hx)).2 hx
lemma log_neg_iff (h : 0 < x) : log x < 0 ↔ x < 1 :=
by { rw ← log_one, exact log_lt_log_iff h zero_lt_one }
lemma log_neg (h0 : 0 < x) (h1 : x < 1) : log x < 0 := (log_neg_iff h0).2 h1
lemma log_nonneg_iff (hx : 0 < x) : 0 ≤ log x ↔ 1 ≤ x :=
by rw [← not_lt, log_neg_iff hx, not_lt]
lemma log_nonneg (hx : 1 ≤ x) : 0 ≤ log x :=
(log_nonneg_iff (zero_lt_one.trans_le hx)).2 hx
lemma log_nonpos_iff (hx : 0 < x) : log x ≤ 0 ↔ x ≤ 1 :=
by rw [← not_lt, log_pos_iff hx, not_lt]
lemma log_nonpos_iff' (hx : 0 ≤ x) : log x ≤ 0 ↔ x ≤ 1 :=
begin
rcases hx.eq_or_lt with (rfl|hx),
{ simp [le_refl, zero_le_one] },
exact log_nonpos_iff hx
end
lemma log_nonpos (hx : 0 ≤ x) (h'x : x ≤ 1) : log x ≤ 0 :=
(log_nonpos_iff' hx).2 h'x
lemma strict_mono_on_log : strict_mono_on log (set.Ioi 0) :=
λ x hx y hy hxy, log_lt_log hx hxy
lemma strict_anti_on_log : strict_anti_on log (set.Iio 0) :=
begin
rintros x (hx : x < 0) y (hy : y < 0) hxy,
rw [← log_abs y, ← log_abs x],
refine log_lt_log (abs_pos.2 hy.ne) _,
rwa [abs_of_neg hy, abs_of_neg hx, neg_lt_neg_iff]
end
lemma log_inj_on_pos : set.inj_on log (set.Ioi 0) :=
strict_mono_on_log.inj_on
lemma eq_one_of_pos_of_log_eq_zero {x : ℝ} (h₁ : 0 < x) (h₂ : log x = 0) : x = 1 :=
log_inj_on_pos (set.mem_Ioi.2 h₁) (set.mem_Ioi.2 zero_lt_one) (h₂.trans real.log_one.symm)
lemma log_ne_zero_of_pos_of_ne_one {x : ℝ} (hx_pos : 0 < x) (hx : x ≠ 1) : log x ≠ 0 :=
mt (eq_one_of_pos_of_log_eq_zero hx_pos) hx
@[simp] lemma log_eq_zero {x : ℝ} : log x = 0 ↔ x = 0 ∨ x = 1 ∨ x = -1 :=
begin
split,
{ intros h,
rcases lt_trichotomy x 0 with x_lt_zero | rfl | x_gt_zero,
{ refine or.inr (or.inr (eq_neg_iff_eq_neg.mp _)),
rw [←log_neg_eq_log x] at h,
exact (eq_one_of_pos_of_log_eq_zero (neg_pos.mpr x_lt_zero) h).symm, },
{ exact or.inl rfl },
{ exact or.inr (or.inl (eq_one_of_pos_of_log_eq_zero x_gt_zero h)), }, },
{ rintro (rfl|rfl|rfl); simp only [log_one, log_zero, log_neg_eq_log], }
end
@[simp] lemma log_pow (x : ℝ) (n : ℕ) : log (x ^ n) = n * log x :=
begin
induction n with n ih,
{ simp },
rcases eq_or_ne x 0 with rfl | hx,
{ simp },
rw [pow_succ', log_mul (pow_ne_zero _ hx) hx, ih, nat.cast_succ, add_mul, one_mul],
end
@[simp] lemma log_zpow (x : ℝ) (n : ℤ) : log (x ^ n) = n * log x :=
begin
induction n,
{ rw [int.of_nat_eq_coe, zpow_coe_nat, log_pow, int.cast_coe_nat] },
rw [zpow_neg_succ_of_nat, log_inv, log_pow, int.cast_neg_succ_of_nat, nat.cast_add_one,
neg_mul_eq_neg_mul],
end
lemma log_le_sub_one_of_pos {x : ℝ} (hx : 0 < x) : log x ≤ x - 1 :=
begin
rw le_sub_iff_add_le,
convert add_one_le_exp (log x),
rw exp_log hx,
end
/-- Bound for `|log x * x|` in the interval `(0, 1]`. -/
lemma abs_log_mul_self_lt (x: ℝ) (h1 : 0 < x) (h2 : x ≤ 1) : |log x * x| < 1 :=
begin
have : 0 < 1/x := by simpa only [one_div, inv_pos] using h1,
replace := log_le_sub_one_of_pos this,
replace : log (1 / x) < 1/x := by linarith,
rw [log_div one_ne_zero h1.ne', log_one, zero_sub, lt_div_iff h1] at this,
have aux : 0 ≤ -log x * x,
{ refine mul_nonneg _ h1.le, rw ←log_inv, apply log_nonneg,
rw [←(le_inv h1 zero_lt_one), inv_one], exact h2, },
rw [←(abs_of_nonneg aux), neg_mul, abs_neg] at this, exact this,
end
/-- The real logarithm function tends to `+∞` at `+∞`. -/
lemma tendsto_log_at_top : tendsto log at_top at_top :=
tendsto_comp_exp_at_top.1 $ by simpa only [log_exp] using tendsto_id
lemma tendsto_log_nhds_within_zero : tendsto log (𝓝[≠] 0) at_bot :=
begin
rw [← (show _ = log, from funext log_abs)],
refine tendsto.comp _ tendsto_abs_nhds_within_zero,
simpa [← tendsto_comp_exp_at_bot] using tendsto_id
end
lemma continuous_on_log : continuous_on log {0}ᶜ :=
begin
rw [continuous_on_iff_continuous_restrict, restrict],
conv in (log _) { rw [log_of_ne_zero (show (x : ℝ) ≠ 0, from x.2)] },
exact exp_order_iso.symm.continuous.comp (continuous_subtype_mk _ continuous_subtype_coe.norm)
end
@[continuity] lemma continuous_log : continuous (λ x : {x : ℝ // x ≠ 0}, log x) :=
continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, hx
@[continuity] lemma continuous_log' : continuous (λ x : {x : ℝ // 0 < x}, log x) :=
continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, ne_of_gt hx
lemma continuous_at_log (hx : x ≠ 0) : continuous_at log x :=
(continuous_on_log x hx).continuous_at $ is_open.mem_nhds is_open_compl_singleton hx
@[simp] lemma continuous_at_log_iff : continuous_at log x ↔ x ≠ 0 :=
begin
refine ⟨_, continuous_at_log⟩,
rintros h rfl,
exact not_tendsto_nhds_of_tendsto_at_bot tendsto_log_nhds_within_zero _
(h.tendsto.mono_left inf_le_left)
end
open_locale big_operators
lemma log_prod {α : Type*} (s : finset α) (f : α → ℝ) (hf : ∀ x ∈ s, f x ≠ 0):
log (∏ i in s, f i) = ∑ i in s, log (f i) :=
begin
classical,
induction s using finset.induction_on with a s ha ih,
{ simp },
simp only [finset.mem_insert, forall_eq_or_imp] at hf,
simp [ha, ih hf.2, log_mul hf.1 (finset.prod_ne_zero_iff.2 hf.2)],
end
lemma tendsto_pow_log_div_mul_add_at_top (a b : ℝ) (n : ℕ) (ha : a ≠ 0) :
tendsto (λ x, log x ^ n / (a * x + b)) at_top (𝓝 0) :=
((tendsto_div_pow_mul_exp_add_at_top a b n ha.symm).comp tendsto_log_at_top).congr'
(by filter_upwards [eventually_gt_at_top (0 : ℝ)] with x hx using by simp [exp_log hx])
lemma is_o_pow_log_id_at_top {n : ℕ} :
asymptotics.is_o (λ x, log x ^ n) id at_top :=
begin
rw asymptotics.is_o_iff_tendsto',
{ simpa using tendsto_pow_log_div_mul_add_at_top 1 0 n one_ne_zero },
filter_upwards [eventually_ne_at_top (0 : ℝ)] with x h₁ h₂ using (h₁ h₂).elim,
end
end real
section continuity
open real
variables {α : Type*}
lemma filter.tendsto.log {f : α → ℝ} {l : filter α} {x : ℝ} (h : tendsto f l (𝓝 x)) (hx : x ≠ 0) :
tendsto (λ x, log (f x)) l (𝓝 (log x)) :=
(continuous_at_log hx).tendsto.comp h
variables [topological_space α] {f : α → ℝ} {s : set α} {a : α}
lemma continuous.log (hf : continuous f) (h₀ : ∀ x, f x ≠ 0) : continuous (λ x, log (f x)) :=
continuous_on_log.comp_continuous hf h₀
lemma continuous_at.log (hf : continuous_at f a) (h₀ : f a ≠ 0) :
continuous_at (λ x, log (f x)) a :=
hf.log h₀
lemma continuous_within_at.log (hf : continuous_within_at f s a) (h₀ : f a ≠ 0) :
continuous_within_at (λ x, log (f x)) s a :=
hf.log h₀
lemma continuous_on.log (hf : continuous_on f s) (h₀ : ∀ x ∈ s, f x ≠ 0) :
continuous_on (λ x, log (f x)) s :=
λ x hx, (hf x hx).log (h₀ x hx)
end continuity
|
cde06c1a402df0a74bff09e323af4007bc119b37 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/group_theory/perm/cycle/basic.lean | 98220da5fc040dfbaca458cdeb2520d46cb15e96 | [
"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 | 59,489 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import data.finset.noncomm_prod
import data.fintype.perm
import group_theory.perm.sign
import logic.equiv.fintype
/-!
# Cyclic permutations
## Main definitions
In the following, `f : equiv.perm β`.
* `equiv.perm.is_cycle`: `f.is_cycle` when two nonfixed points of `β`
are related by repeated application of `f`.
* `equiv.perm.same_cycle`: `f.same_cycle x y` when `x` and `y` are in the same cycle of `f`.
The following two definitions require that `β` is a `fintype`:
* `equiv.perm.cycle_of`: `f.cycle_of x` is the cycle of `f` that `x` belongs to.
* `equiv.perm.cycle_factors`: `f.cycle_factors` is a list of disjoint cyclic permutations that
multiply to `f`.
## Main results
* This file contains several closure results:
- `closure_is_cycle` : The symmetric group is generated by cycles
- `closure_cycle_adjacent_swap` : The symmetric group is generated by
a cycle and an adjacent transposition
- `closure_cycle_coprime_swap` : The symmetric group is generated by
a cycle and a coprime transposition
- `closure_prime_cycle_swap` : The symmetric group is generated by
a prime cycle and a transposition
-/
namespace equiv.perm
open equiv function finset
variables {α : Type*} {β : Type*} [decidable_eq α]
section sign_cycle
/-!
### `is_cycle`
-/
variables [fintype α]
/-- A permutation is a cycle when any two nonfixed points of the permutation are related by repeated
application of the permutation. -/
def is_cycle (f : perm β) : Prop := ∃ x, f x ≠ x ∧ ∀ y, f y ≠ y → ∃ i : ℤ, (f ^ i) x = y
lemma is_cycle.ne_one {f : perm β} (h : is_cycle f) : f ≠ 1 :=
λ hf, by simpa [hf, is_cycle] using h
@[simp] lemma not_is_cycle_one : ¬ (1 : perm β).is_cycle :=
λ H, H.ne_one rfl
lemma is_cycle.two_le_card_support {f : perm α} (h : is_cycle f) :
2 ≤ f.support.card :=
two_le_card_support_of_ne_one h.ne_one
lemma is_cycle_swap {α : Type*} [decidable_eq α] {x y : α} (hxy : x ≠ y) : is_cycle (swap x y) :=
⟨y, by rwa swap_apply_right,
λ a (ha : ite (a = x) y (ite (a = y) x a) ≠ a),
if hya : y = a then ⟨0, hya⟩
else ⟨1, by { rw [zpow_one, swap_apply_def], split_ifs at *; cc }⟩⟩
lemma is_swap.is_cycle {α : Type*} [decidable_eq α] {f : perm α} (hf : is_swap f) : is_cycle f :=
begin
obtain ⟨x, y, hxy, rfl⟩ := hf,
exact is_cycle_swap hxy,
end
lemma is_cycle.inv {f : perm β} (hf : is_cycle f) : is_cycle (f⁻¹) :=
let ⟨x, hx⟩ := hf in
⟨x, by { simp only [inv_eq_iff_eq, *, forall_prop_of_true, ne.def] at *, cc },
λ y hy, let ⟨i, hi⟩ := hx.2 y (by { simp only [inv_eq_iff_eq, *, forall_prop_of_true,
ne.def] at *, cc }) in
⟨-i, by rwa [zpow_neg, inv_zpow, inv_inv]⟩⟩
lemma is_cycle.is_cycle_conj {f g : perm β} (hf : is_cycle f) : is_cycle (g * f * g⁻¹) :=
begin
obtain ⟨a, ha1, ha2⟩ := hf,
refine ⟨g a, by simp [ha1], λ b hb, _⟩,
obtain ⟨i, hi⟩ := ha2 (g⁻¹ b) _,
{ refine ⟨i, _⟩,
rw conj_zpow,
simp [hi] },
{ contrapose! hb,
rw [perm.mul_apply, perm.mul_apply, hb, apply_inv_self] }
end
lemma is_cycle.exists_zpow_eq {f : perm β} (hf : is_cycle f) {x y : β}
(hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℤ, (f ^ i) x = y :=
let ⟨g, hg⟩ := hf in
let ⟨a, ha⟩ := hg.2 x hx in
let ⟨b, hb⟩ := hg.2 y hy in
⟨b - a, by rw [← ha, ← mul_apply, ← zpow_add, sub_add_cancel, hb]⟩
lemma is_cycle.exists_pow_eq [finite β] {f : perm β} (hf : is_cycle f) {x y : β}
(hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℕ, (f ^ i) x = y :=
let ⟨n, hn⟩ := hf.exists_zpow_eq hx hy in
by classical; exact ⟨(n % order_of f).to_nat, by
{ have := n.mod_nonneg (int.coe_nat_ne_zero.mpr (ne_of_gt (order_of_pos f))),
rwa [← zpow_coe_nat, int.to_nat_of_nonneg this, ← zpow_eq_mod_order_of] }⟩
lemma is_cycle.exists_pow_eq_one [finite β] {f : perm β} (hf : is_cycle f) :
∃ (k : ℕ) (hk : 1 < k), f ^ k = 1 :=
begin
classical,
have : is_of_fin_order f := exists_pow_eq_one f,
rw is_of_fin_order_iff_pow_eq_one at this,
obtain ⟨x, hx, hx'⟩ := hf,
obtain ⟨_ | _ | k, hk, hk'⟩ := this,
{ exact absurd hk (lt_asymm hk) },
{ rw pow_one at hk',
simpa [hk'] using hx },
{ exact ⟨k + 2, by simp, hk'⟩ }
end
/-- The subgroup generated by a cycle is in bijection with its support -/
noncomputable def is_cycle.zpowers_equiv_support {σ : perm α} (hσ : is_cycle σ) :
(↑(subgroup.zpowers σ) : set (perm α)) ≃ (↑(σ.support) : set α) :=
equiv.of_bijective (λ τ, ⟨τ (classical.some hσ),
begin
obtain ⟨τ, n, rfl⟩ := τ,
rw [finset.mem_coe, coe_fn_coe_base', subtype.coe_mk, zpow_apply_mem_support, mem_support],
exact (classical.some_spec hσ).1,
end⟩)
begin
split,
{ rintros ⟨a, m, rfl⟩ ⟨b, n, rfl⟩ h,
ext y,
by_cases hy : σ y = y,
{ simp_rw [subtype.coe_mk, zpow_apply_eq_self_of_apply_eq_self hy] },
{ obtain ⟨i, rfl⟩ := (classical.some_spec hσ).2 y hy,
rw [subtype.coe_mk, subtype.coe_mk, zpow_apply_comm σ m i, zpow_apply_comm σ n i],
exact congr_arg _ (subtype.ext_iff.mp h) } }, by
{ rintros ⟨y, hy⟩,
rw [finset.mem_coe, mem_support] at hy,
obtain ⟨n, rfl⟩ := (classical.some_spec hσ).2 y hy,
exact ⟨⟨σ ^ n, n, rfl⟩, rfl⟩ },
end
@[simp] lemma is_cycle.zpowers_equiv_support_apply {σ : perm α} (hσ : is_cycle σ) {n : ℕ} :
hσ.zpowers_equiv_support ⟨σ ^ n, n, rfl⟩ = ⟨(σ ^ n) (classical.some hσ),
pow_apply_mem_support.2 (mem_support.2 (classical.some_spec hσ).1)⟩ :=
rfl
@[simp] lemma is_cycle.zpowers_equiv_support_symm_apply {σ : perm α} (hσ : is_cycle σ) (n : ℕ) :
hσ.zpowers_equiv_support.symm ⟨(σ ^ n) (classical.some hσ),
pow_apply_mem_support.2 (mem_support.2 (classical.some_spec hσ).1)⟩ =
⟨σ ^ n, n, rfl⟩ :=
(equiv.symm_apply_eq _).2 hσ.zpowers_equiv_support_apply
lemma order_of_is_cycle {σ : perm α} (hσ : is_cycle σ) : order_of σ = σ.support.card :=
begin
rw [order_eq_card_zpowers, ←fintype.card_coe],
convert fintype.card_congr (is_cycle.zpowers_equiv_support hσ),
end
lemma is_cycle_swap_mul_aux₁ {α : Type*} [decidable_eq α] : ∀ (n : ℕ) {b x : α} {f : perm α}
(hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b),
∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b
| 0 := λ b x f hb h, ⟨0, h⟩
| (n+1 : ℕ) := λ b x f hb h,
if hfbx : f x = b then ⟨0, hfbx⟩
else
have f b ≠ b ∧ b ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hb,
have hb' : (swap x (f x) * f) (f⁻¹ b) ≠ f⁻¹ b,
by { rw [mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx),
ne.def, ← f.injective.eq_iff, apply_inv_self],
exact this.1 },
let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb'
(f.injective $ by { rw [apply_inv_self], rwa [pow_succ, mul_apply] at h }) in
⟨i + 1, by rw [add_comm, zpow_add, mul_apply, hi, zpow_one, mul_apply, apply_inv_self,
swap_apply_of_ne_of_ne (ne_and_ne_of_swap_mul_apply_ne_self hb).2 (ne.symm hfbx)]⟩
lemma is_cycle_swap_mul_aux₂ {α : Type*} [decidable_eq α] :
∀ (n : ℤ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b),
∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b
| (n : ℕ) := λ b x f, is_cycle_swap_mul_aux₁ n
| -[1+ n] := λ b x f hb h,
if hfbx' : f x = b then ⟨0, hfbx'⟩
else
have f b ≠ b ∧ b ≠ x := ne_and_ne_of_swap_mul_apply_ne_self hb,
have hb : (swap x (f⁻¹ x) * f⁻¹) (f⁻¹ b) ≠ f⁻¹ b,
by { rw [mul_apply, swap_apply_def],
split_ifs;
simp only [inv_eq_iff_eq, perm.mul_apply, zpow_neg_succ_of_nat, ne.def,
perm.apply_inv_self] at *;
cc },
let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb
(show (f⁻¹ ^ n) (f⁻¹ x) = f⁻¹ b, by
rw [← zpow_coe_nat, ← h, ← mul_apply, ← mul_apply, ← mul_apply, zpow_neg_succ_of_nat,
← inv_pow, pow_succ', mul_assoc, mul_assoc, inv_mul_self, mul_one, zpow_coe_nat,
← pow_succ', ← pow_succ]) in
have h : (swap x (f⁻¹ x) * f⁻¹) (f x) = f⁻¹ x, by rw [mul_apply, inv_apply_self, swap_apply_left],
⟨-i, by rw [← add_sub_cancel i 1, neg_sub, sub_eq_add_neg, zpow_add, zpow_one, zpow_neg,
← inv_zpow, mul_inv_rev, swap_inv, mul_swap_eq_swap_mul, inv_apply_self, swap_comm _ x,
zpow_add, zpow_one, mul_apply, mul_apply (_ ^ i), h, hi, mul_apply, apply_inv_self,
swap_apply_of_ne_of_ne this.2 (ne.symm hfbx')]⟩
lemma is_cycle.eq_swap_of_apply_apply_eq_self {α : Type*} [decidable_eq α]
{f : perm α} (hf : is_cycle f) {x : α}
(hfx : f x ≠ x) (hffx : f (f x) = x) : f = swap x (f x) :=
equiv.ext $ λ y,
let ⟨z, hz⟩ := hf in
let ⟨i, hi⟩ := hz.2 x hfx in
if hyx : y = x then by simp [hyx]
else if hfyx : y = f x then by simp [hfyx, hffx]
else begin
rw [swap_apply_of_ne_of_ne hyx hfyx],
refine by_contradiction (λ hy, _),
cases hz.2 y hy with j hj,
rw [← sub_add_cancel j i, zpow_add, mul_apply, hi] at hj,
cases zpow_apply_eq_of_apply_apply_eq_self hffx (j - i) with hji hji,
{ rw [← hj, hji] at hyx, cc },
{ rw [← hj, hji] at hfyx, cc }
end
lemma is_cycle.swap_mul {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α}
(hx : f x ≠ x) (hffx : f (f x) ≠ x) : is_cycle (swap x (f x) * f) :=
⟨f x, by { simp [swap_apply_def, mul_apply, if_neg hffx, f.injective.eq_iff, if_neg hx, hx], },
λ y hy,
let ⟨i, hi⟩ := hf.exists_zpow_eq hx (ne_and_ne_of_swap_mul_apply_ne_self hy).1 in
have hi : (f ^ (i - 1)) (f x) = y, from
calc (f ^ (i - 1)) (f x) = (f ^ (i - 1) * f ^ (1 : ℤ)) x : by rw [zpow_one, mul_apply]
... = y : by rwa [← zpow_add, sub_add_cancel],
is_cycle_swap_mul_aux₂ (i - 1) hy hi⟩
lemma is_cycle.sign : ∀ {f : perm α} (hf : is_cycle f),
sign f = -(-1) ^ f.support.card
| f := λ hf,
let ⟨x, hx⟩ := hf in
calc sign f = sign (swap x (f x) * (swap x (f x) * f)) :
by rw [← mul_assoc, mul_def, mul_def, swap_swap, trans_refl]
... = -(-1) ^ f.support.card :
if h1 : f (f x) = x
then
have h : swap x (f x) * f = 1,
begin
rw hf.eq_swap_of_apply_apply_eq_self hx.1 h1,
simp only [perm.mul_def, perm.one_def, swap_apply_left, swap_swap]
end,
by { rw [sign_mul, sign_swap hx.1.symm, h, sign_one, hf.eq_swap_of_apply_apply_eq_self hx.1 h1,
card_support_swap hx.1.symm], refl }
else
have h : card (support (swap x (f x) * f)) + 1 = card (support f),
by rw [← insert_erase (mem_support.2 hx.1), support_swap_mul_eq _ _ h1,
card_insert_of_not_mem (not_mem_erase _ _), sdiff_singleton_eq_erase],
have wf : card (support (swap x (f x) * f)) < card (support f),
from card_support_swap_mul hx.1,
by { rw [sign_mul, sign_swap hx.1.symm, (hf.swap_mul hx.1 h1).sign, ← h],
simp only [pow_add, mul_one, neg_neg, one_mul, mul_neg, eq_self_iff_true,
pow_one, neg_mul_neg] }
using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ f, f.support.card)⟩]}
lemma is_cycle_of_is_cycle_pow {σ : perm α} {n : ℕ}
(h1 : is_cycle (σ ^ n)) (h2 : σ.support ≤ (σ ^ n).support) : is_cycle σ :=
begin
have key : ∀ x : α, (σ ^ n) x ≠ x ↔ σ x ≠ x,
{ simp_rw [←mem_support],
exact finset.ext_iff.mp (le_antisymm (support_pow_le σ n) h2) },
obtain ⟨x, hx1, hx2⟩ := h1,
refine ⟨x, (key x).mp hx1, λ y hy, _⟩,
cases (hx2 y ((key y).mpr hy)) with i _,
exact ⟨n * i, by rwa zpow_mul⟩
end
-- The lemma `support_zpow_le` is relevant. It means that `h2` is equivalent to
-- `σ.support = (σ ^ n).support`, as well as to `σ.support.card ≤ (σ ^ n).support.card`.
lemma is_cycle_of_is_cycle_zpow {σ : perm α} {n : ℤ}
(h1 : is_cycle (σ ^ n)) (h2 : σ.support ≤ (σ ^ n).support) : is_cycle σ :=
begin
cases n,
{ exact is_cycle_of_is_cycle_pow h1 h2 },
{ simp only [le_eq_subset, zpow_neg_succ_of_nat, perm.support_inv] at h1 h2,
simpa using is_cycle_of_is_cycle_pow h1.inv h2 }
end
lemma is_cycle.extend_domain {α : Type*} {p : β → Prop} [decidable_pred p]
(f : α ≃ subtype p) {g : perm α} (h : is_cycle g) :
is_cycle (g.extend_domain f) :=
begin
obtain ⟨a, ha, ha'⟩ := h,
refine ⟨f a, _, λ b hb, _⟩,
{ rw extend_domain_apply_image,
exact λ con, ha (f.injective (subtype.coe_injective con)) },
by_cases pb : p b,
{ obtain ⟨i, hi⟩ := ha' (f.symm ⟨b, pb⟩) (λ con, hb _),
{ refine ⟨i, _⟩,
have hnat : ∀ (k : ℕ) (a : α), (g.extend_domain f ^ k) ↑(f a) = f ((g ^ k) a),
{ intros k a,
induction k with k ih, { refl },
rw [pow_succ, perm.mul_apply, ih, extend_domain_apply_image, pow_succ, perm.mul_apply] },
have hint : ∀ (k : ℤ) (a : α), (g.extend_domain f ^ k) ↑(f a) = f ((g ^ k) a),
{ intros k a,
induction k with k k,
{ rw [zpow_of_nat, zpow_of_nat, hnat] },
rw [zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, inv_eq_iff_eq, hnat, apply_inv_self] },
rw [hint, hi, apply_symm_apply, subtype.coe_mk] },
{ rw [extend_domain_apply_subtype _ _ pb, con, apply_symm_apply, subtype.coe_mk] } },
{ exact (hb (extend_domain_apply_not_subtype _ _ pb)).elim }
end
lemma nodup_of_pairwise_disjoint_cycles {l : list (perm β)} (h1 : ∀ f ∈ l, is_cycle f)
(h2 : l.pairwise disjoint) : l.nodup :=
nodup_of_pairwise_disjoint (λ h, (h1 1 h).ne_one rfl) h2
end sign_cycle
/-!
### `same_cycle`
-/
/-- The equivalence relation indicating that two points are in the same cycle of a permutation. -/
def same_cycle (f : perm β) (x y : β) : Prop := ∃ i : ℤ, (f ^ i) x = y
@[refl] lemma same_cycle.refl (f : perm β) (x : β) : same_cycle f x x := ⟨0, rfl⟩
@[symm] lemma same_cycle.symm {f : perm β} {x y : β} : same_cycle f x y → same_cycle f y x :=
λ ⟨i, hi⟩, ⟨-i, by rw [zpow_neg, ← hi, inv_apply_self]⟩
@[trans] lemma same_cycle.trans {f : perm β} {x y z : β} :
same_cycle f x y → same_cycle f y z → same_cycle f x z :=
λ ⟨i, hi⟩ ⟨j, hj⟩, ⟨j + i, by rw [zpow_add, mul_apply, hi, hj]⟩
lemma same_cycle.apply_eq_self_iff {f : perm β} {x y : β} :
same_cycle f x y → (f x = x ↔ f y = y) :=
λ ⟨i, hi⟩, by rw [← hi, ← mul_apply, ← zpow_one_add, add_comm, zpow_add_one, mul_apply,
(f ^ i).injective.eq_iff]
lemma is_cycle.same_cycle {f : perm β} (hf : is_cycle f) {x y : β}
(hx : f x ≠ x) (hy : f y ≠ y) : same_cycle f x y :=
hf.exists_zpow_eq hx hy
lemma same_cycle.nat' [finite β] {f : perm β} {x y : β} (h : same_cycle f x y) :
∃ (i : ℕ) (h : i < order_of f), (f ^ i) x = y :=
begin
classical,
obtain ⟨k, rfl⟩ := h,
use ((k % order_of f).nat_abs),
have h₀ := int.coe_nat_pos.mpr (order_of_pos f),
have h₁ := int.mod_nonneg k h₀.ne',
rw [←zpow_coe_nat, int.nat_abs_of_nonneg h₁, ←zpow_eq_mod_order_of],
refine ⟨_, rfl⟩,
rw [←int.coe_nat_lt, int.nat_abs_of_nonneg h₁],
exact int.mod_lt_of_pos _ h₀,
end
lemma same_cycle.nat'' [finite β] {f : perm β} {x y : β} (h : same_cycle f x y) :
∃ (i : ℕ) (hpos : 0 < i) (h : i ≤ order_of f), (f ^ i) x = y :=
begin
classical,
obtain ⟨_|i, hi, rfl⟩ := h.nat',
{ refine ⟨order_of f, order_of_pos f, le_rfl, _⟩,
rw [pow_order_of_eq_one, pow_zero] },
{ exact ⟨i.succ, i.zero_lt_succ, hi.le, rfl⟩ }
end
instance [fintype α] (f : perm α) : decidable_rel (same_cycle f) :=
λ x y, decidable_of_iff (∃ n ∈ list.range (fintype.card (perm α)), (f ^ n) x = y)
⟨λ ⟨n, _, hn⟩, ⟨n, hn⟩, λ ⟨i, hi⟩, ⟨(i % order_of f).nat_abs, list.mem_range.2
(int.coe_nat_lt.1 $
by { rw int.nat_abs_of_nonneg (int.mod_nonneg _
(int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))),
{ refine (int.mod_lt _ $ int.coe_nat_ne_zero_iff_pos.2 $ order_of_pos _).trans_le _,
simp [order_of_le_card_univ] },
apply_instance }),
by { rw [← zpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _
(int.coe_nat_ne_zero_iff_pos.2 (order_of_pos _))), ← zpow_eq_mod_order_of, hi],
apply_instance }⟩⟩
lemma same_cycle_apply {f : perm β} {x y : β} : same_cycle f x (f y) ↔ same_cycle f x y :=
⟨λ ⟨i, hi⟩, ⟨-1 + i, by rw [zpow_add, mul_apply, hi, zpow_neg_one, inv_apply_self]⟩,
λ ⟨i, hi⟩, ⟨1 + i, by rw [zpow_add, mul_apply, hi, zpow_one]⟩⟩
lemma same_cycle_cycle {f : perm β} {x : β} (hx : f x ≠ x) : is_cycle f ↔
(∀ {y}, same_cycle f x y ↔ f y ≠ y) :=
⟨λ hf y, ⟨λ ⟨i, hi⟩ hy, hx $
by { rw [← zpow_apply_eq_self_of_apply_eq_self hy i, (f ^ i).injective.eq_iff] at hi,
rw [hi, hy] },
hf.exists_zpow_eq hx⟩,
λ h, ⟨x, hx, λ y hy, h.2 hy⟩⟩
lemma same_cycle_inv (f : perm β) {x y : β} : same_cycle f⁻¹ x y ↔ same_cycle f x y :=
⟨λ ⟨i, hi⟩, ⟨-i, by rw [zpow_neg, ← inv_zpow, hi]⟩,
λ ⟨i, hi⟩, ⟨-i, by rw [zpow_neg, ← inv_zpow, inv_inv, hi]⟩ ⟩
lemma same_cycle_inv_apply {f : perm β} {x y : β} : same_cycle f x (f⁻¹ y) ↔ same_cycle f x y :=
by rw [← same_cycle_inv, same_cycle_apply, same_cycle_inv]
@[simp] lemma same_cycle_pow_left_iff {f : perm β} {x y : β} {n : ℕ} :
same_cycle f ((f ^ n) x) y ↔ same_cycle f x y :=
begin
split,
{ rintro ⟨k, rfl⟩,
use (k + n),
simp [zpow_add] },
{ rintro ⟨k, rfl⟩,
use (k - n),
rw [←zpow_coe_nat, ←mul_apply, ←zpow_add, int.sub_add_cancel] }
end
@[simp] lemma same_cycle_zpow_left_iff {f : perm β} {x y : β} {n : ℤ} :
same_cycle f ((f ^ n) x) y ↔ same_cycle f x y :=
begin
cases n,
{ exact same_cycle_pow_left_iff },
{ rw [zpow_neg_succ_of_nat, ←inv_pow, ←same_cycle_inv, same_cycle_pow_left_iff, same_cycle_inv] }
end
/-- Unlike `support_congr`, which assumes that `∀ (x ∈ g.support), f x = g x)`, here
we have the weaker assumption that `∀ (x ∈ f.support), f x = g x`. -/
lemma is_cycle.support_congr [fintype α] {f g : perm α} (hf : is_cycle f) (hg : is_cycle g)
(h : f.support ⊆ g.support) (h' : ∀ (x ∈ f.support), f x = g x) : f = g :=
begin
have : f.support = g.support,
{ refine le_antisymm h _,
intros z hz,
obtain ⟨x, hx, hf'⟩ := id hf,
have hx' : g x ≠ x,
{ rwa [←h' x (mem_support.mpr hx)] },
obtain ⟨m, hm⟩ := hg.exists_pow_eq hx' (mem_support.mp hz),
have h'' : ∀ (x ∈ f.support ∩ g.support), f x = g x,
{ intros x hx,
exact h' x (mem_of_mem_inter_left hx) },
rwa [←hm, ←pow_eq_on_of_mem_support h'' _ x (mem_inter_of_mem (mem_support.mpr hx)
(mem_support.mpr hx')), pow_apply_mem_support, mem_support] },
refine support_congr h _,
simpa [←this] using h'
end
/-- If two cyclic permutations agree on all terms in their intersection,
and that intersection is not empty, then the two cyclic permutations must be equal. -/
lemma is_cycle.eq_on_support_inter_nonempty_congr [fintype α] {f g : perm α}
(hf : is_cycle f) (hg : is_cycle g) (h : ∀ (x ∈ f.support ∩ g.support), f x = g x) {x : α}
(hx : f x = g x) (hx' : x ∈ f.support) : f = g :=
begin
have hx'' : x ∈ g.support,
{ rwa [mem_support, ←hx, ←mem_support] },
have : f.support ⊆ g.support,
{ intros y hy,
obtain ⟨k, rfl⟩ := hf.exists_pow_eq (mem_support.mp hx') (mem_support.mp hy),
rwa [pow_eq_on_of_mem_support h _ _ (mem_inter_of_mem hx' hx''), pow_apply_mem_support] },
rw (inter_eq_left_iff_subset _ _).mpr this at h,
exact hf.support_congr hg this h
end
lemma is_cycle.support_pow_eq_iff [fintype α] {f : perm α} (hf : is_cycle f) {n : ℕ} :
support (f ^ n) = support f ↔ ¬ order_of f ∣ n :=
begin
rw order_of_dvd_iff_pow_eq_one,
split,
{ intros h H,
refine hf.ne_one _,
rw [←support_eq_empty_iff, ←h, H, support_one] },
{ intro H,
apply le_antisymm (support_pow_le _ n) _,
intros x hx,
contrapose! H,
ext z,
by_cases hz : f z = z,
{ rw [pow_apply_eq_self_of_apply_eq_self hz, one_apply] },
{ obtain ⟨k, rfl⟩ := hf.exists_pow_eq hz (mem_support.mp hx),
apply (f ^ k).injective,
rw [←mul_apply, (commute.pow_pow_self _ _ _).eq, mul_apply],
simpa using H } }
end
lemma is_cycle.pow_iff [finite β] {f : perm β} (hf : is_cycle f) {n : ℕ} :
is_cycle (f ^ n) ↔ n.coprime (order_of f) :=
begin
classical,
casesI nonempty_fintype β,
split,
{ intro h,
have hr : support (f ^ n) = support f,
{ rw hf.support_pow_eq_iff,
rintro ⟨k, rfl⟩,
refine h.ne_one _,
simp [pow_mul, pow_order_of_eq_one] },
have : order_of (f ^ n) = order_of f,
{ rw [order_of_is_cycle h, hr, order_of_is_cycle hf] },
rw [order_of_pow, nat.div_eq_self] at this,
cases this,
{ exact absurd this (order_of_pos _).ne' },
{ rwa [nat.coprime_iff_gcd_eq_one, nat.gcd_comm] } },
{ intro h,
obtain ⟨m, hm⟩ := exists_pow_eq_self_of_coprime h,
have hf' : is_cycle ((f ^ n) ^ m) := by rwa hm,
refine is_cycle_of_is_cycle_pow hf' _,
intros x hx,
rw [hm],
exact support_pow_le _ n hx }
end
lemma is_cycle.pow_eq_one_iff [finite β] {f : perm β} (hf : is_cycle f) {n : ℕ} :
f ^ n = 1 ↔ ∃ x, f x ≠ x ∧ (f ^ n) x = x :=
begin
classical,
casesI nonempty_fintype β,
split,
{ intro h,
obtain ⟨x, hx, -⟩ := id hf,
exact ⟨x, hx, by simp [h]⟩ },
{ rintro ⟨x, hx, hx'⟩,
by_cases h : support (f ^ n) = support f,
{ rw [← mem_support, ← h, mem_support] at hx,
contradiction },
{ rw [hf.support_pow_eq_iff, not_not] at h,
obtain ⟨k, rfl⟩ := h,
rw [pow_mul, pow_order_of_eq_one, one_pow] } }
end
lemma is_cycle.pow_eq_pow_iff [finite β] {f : perm β} (hf : is_cycle f) {a b : ℕ} :
f ^ a = f ^ b ↔ ∃ x, f x ≠ x ∧ (f ^ a) x = (f ^ b) x :=
begin
classical,
casesI nonempty_fintype β,
split,
{ intro h,
obtain ⟨x, hx, -⟩ := id hf,
exact ⟨x, hx, by simp [h]⟩ },
{ rintro ⟨x, hx, hx'⟩,
wlog hab : a ≤ b,
suffices : f ^ (b - a) = 1,
{ rw [pow_sub _ hab, mul_inv_eq_one] at this,
rw this },
rw hf.pow_eq_one_iff,
by_cases hfa : (f ^ a) x ∈ f.support,
{ refine ⟨(f ^ a) x, mem_support.mp hfa, _⟩,
simp only [pow_sub _ hab, equiv.perm.coe_mul, function.comp_app,
inv_apply_self, ← hx'] },
{ have h := @equiv.perm.zpow_apply_comm _ f 1 a x,
simp only [zpow_one, zpow_coe_nat] at h,
rw [not_mem_support, h, function.injective.eq_iff (f ^ a).injective] at hfa,
contradiction }}
end
lemma is_cycle.mem_support_pos_pow_iff_of_lt_order_of [fintype α] {f : perm α} (hf : is_cycle f)
{n : ℕ} (npos : 0 < n) (hn : n < order_of f) {x : α} :
x ∈ (f ^ n).support ↔ x ∈ f.support :=
begin
have : ¬ order_of f ∣ n := nat.not_dvd_of_pos_of_lt npos hn,
rw ←hf.support_pow_eq_iff at this,
rw this
end
lemma is_cycle.is_cycle_pow_pos_of_lt_prime_order [finite β] {f : perm β} (hf : is_cycle f)
(hf' : (order_of f).prime) (n : ℕ) (hn : 0 < n) (hn' : n < order_of f) : is_cycle (f ^ n) :=
begin
classical,
casesI nonempty_fintype β,
have : n.coprime (order_of f),
{ refine nat.coprime.symm _,
rw nat.prime.coprime_iff_not_dvd hf',
exact nat.not_dvd_of_pos_of_lt hn hn' },
obtain ⟨m, hm⟩ := exists_pow_eq_self_of_coprime this,
have hf'' := hf,
rw ←hm at hf'',
refine is_cycle_of_is_cycle_pow hf'' _,
rw [hm],
exact support_pow_le f n
end
/-!
### `cycle_of`
-/
/-- `f.cycle_of x` is the cycle of the permutation `f` to which `x` belongs. -/
def cycle_of [fintype α] (f : perm α) (x : α) : perm α :=
of_subtype (@subtype_perm _ f (same_cycle f x) (λ _, same_cycle_apply.symm))
lemma cycle_of_apply [fintype α] (f : perm α) (x y : α) :
cycle_of f x y = if same_cycle f x y then f y else y :=
begin
dsimp only [cycle_of],
split_ifs,
{ apply of_subtype_apply_of_mem, exact h, },
{ apply of_subtype_apply_of_not_mem, exact h },
end
lemma cycle_of_inv [fintype α] (f : perm α) (x : α) :
(cycle_of f x)⁻¹ = cycle_of f⁻¹ x :=
equiv.ext $ λ y, begin
rw [inv_eq_iff_eq, cycle_of_apply, cycle_of_apply],
split_ifs; simp [*, same_cycle_inv, same_cycle_inv_apply] at *
end
@[simp] lemma cycle_of_pow_apply_self [fintype α] (f : perm α) (x : α) :
∀ n : ℕ, (cycle_of f x ^ n) x = (f ^ n) x
| 0 := rfl
| (n+1) := by { rw [pow_succ, mul_apply, cycle_of_apply,
cycle_of_pow_apply_self, if_pos, pow_succ, mul_apply],
exact ⟨n, rfl⟩ }
@[simp] lemma cycle_of_zpow_apply_self [fintype α] (f : perm α) (x : α) :
∀ n : ℤ, (cycle_of f x ^ n) x = (f ^ n) x
| (n : ℕ) := cycle_of_pow_apply_self f x n
| -[1+ n] := by rw [zpow_neg_succ_of_nat, ← inv_pow, cycle_of_inv,
zpow_neg_succ_of_nat, ← inv_pow, cycle_of_pow_apply_self]
lemma same_cycle.cycle_of_apply [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) :
cycle_of f x y = f y :=
begin
apply of_subtype_apply_of_mem, exact h,
end
lemma cycle_of_apply_of_not_same_cycle [fintype α] {f : perm α} {x y : α} (h : ¬same_cycle f x y) :
cycle_of f x y = y :=
begin
apply of_subtype_apply_of_not_mem, exact h,
end
lemma same_cycle.cycle_of_eq [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) :
cycle_of f x = cycle_of f y :=
begin
ext z,
rw cycle_of_apply,
split_ifs with hz hz,
{ exact (h.symm.trans hz).cycle_of_apply.symm },
{ exact (cycle_of_apply_of_not_same_cycle (mt h.trans hz)).symm }
end
@[simp] lemma cycle_of_apply_apply_zpow_self [fintype α] (f : perm α) (x : α) (k : ℤ) :
cycle_of f x ((f ^ k) x) = (f ^ (k + 1)) x :=
begin
rw same_cycle.cycle_of_apply,
{ rw [add_comm, zpow_add, zpow_one, mul_apply] },
{ exact ⟨k, rfl⟩ }
end
@[simp] lemma cycle_of_apply_apply_pow_self [fintype α] (f : perm α) (x : α) (k : ℕ) :
cycle_of f x ((f ^ k) x) = (f ^ (k + 1)) x :=
by convert cycle_of_apply_apply_zpow_self f x k using 1
@[simp] lemma cycle_of_apply_apply_self [fintype α] (f : perm α) (x : α) :
cycle_of f x (f x) = f (f x) :=
by convert cycle_of_apply_apply_pow_self f x 1 using 1
@[simp] lemma cycle_of_apply_self [fintype α] (f : perm α) (x : α) :
cycle_of f x x = f x := (same_cycle.refl _ _).cycle_of_apply
lemma is_cycle.cycle_of_eq [fintype α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) :
cycle_of f x = f :=
equiv.ext $ λ y,
if h : same_cycle f x y then by rw [h.cycle_of_apply]
else by rw [cycle_of_apply_of_not_same_cycle h, not_not.1 (mt ((same_cycle_cycle hx).1 hf).2 h)]
@[simp] lemma cycle_of_eq_one_iff [fintype α] (f : perm α) {x : α} : cycle_of f x = 1 ↔ f x = x :=
begin
simp_rw [ext_iff, cycle_of_apply, one_apply],
refine ⟨λ h, (if_pos (same_cycle.refl f x)).symm.trans (h x), λ h y, _⟩,
by_cases hy : f y = y,
{ rw [hy, if_t_t] },
{ exact if_neg (mt same_cycle.apply_eq_self_iff (by tauto)) },
end
@[simp] lemma cycle_of_self_apply [fintype α] (f : perm α) (x : α) :
cycle_of f (f x) = cycle_of f x :=
(same_cycle_apply.mpr (same_cycle.refl _ _)).symm.cycle_of_eq
@[simp] lemma cycle_of_self_apply_pow [fintype α] (f : perm α) (n : ℕ) (x : α) :
cycle_of f ((f ^ n) x) = cycle_of f x :=
(same_cycle_pow_left_iff.mpr (same_cycle.refl _ _)).cycle_of_eq
@[simp] lemma cycle_of_self_apply_zpow [fintype α] (f : perm α) (n : ℤ) (x : α) :
cycle_of f ((f ^ n) x) = cycle_of f x :=
(same_cycle_zpow_left_iff.mpr (same_cycle.refl _ _)).cycle_of_eq
lemma is_cycle.cycle_of [fintype α] {f : perm α} (hf : is_cycle f) {x : α} :
cycle_of f x = if f x = x then 1 else f :=
begin
by_cases hx : f x = x,
{ rwa [if_pos hx, cycle_of_eq_one_iff] },
{ rwa [if_neg hx, hf.cycle_of_eq] },
end
lemma cycle_of_one [fintype α] (x : α) : cycle_of 1 x = 1 :=
(cycle_of_eq_one_iff 1).mpr rfl
lemma is_cycle_cycle_of [fintype α] (f : perm α) {x : α} (hx : f x ≠ x) : is_cycle (cycle_of f x) :=
have cycle_of f x x ≠ x, by rwa [(same_cycle.refl _ _).cycle_of_apply],
(same_cycle_cycle this).2 $ λ y,
⟨λ h, mt h.apply_eq_self_iff.2 this,
λ h, if hxy : same_cycle f x y then
let ⟨i, hi⟩ := hxy in
⟨i, by rw [cycle_of_zpow_apply_self, hi]⟩
else by { rw [cycle_of_apply_of_not_same_cycle hxy] at h, exact (h rfl).elim }⟩
@[simp] lemma two_le_card_support_cycle_of_iff [fintype α] {f : perm α} {x : α} :
2 ≤ card (cycle_of f x).support ↔ f x ≠ x :=
begin
refine ⟨λ h, _, λ h, by simpa using (is_cycle_cycle_of _ h).two_le_card_support⟩,
contrapose! h,
rw ←cycle_of_eq_one_iff at h,
simp [h]
end
@[simp] lemma card_support_cycle_of_pos_iff [fintype α] {f : perm α} {x : α} :
0 < card (cycle_of f x).support ↔ f x ≠ x :=
begin
rw [←two_le_card_support_cycle_of_iff, ←nat.succ_le_iff],
exact ⟨λ h, or.resolve_left h.eq_or_lt (card_support_ne_one _).symm, zero_lt_two.trans_le⟩
end
lemma pow_apply_eq_pow_mod_order_of_cycle_of_apply [fintype α] (f : perm α) (n : ℕ) (x : α) :
(f ^ n) x = (f ^ (n % order_of (cycle_of f x))) x :=
by rw [←cycle_of_pow_apply_self f, ←cycle_of_pow_apply_self f, pow_eq_mod_order_of]
lemma cycle_of_mul_of_apply_right_eq_self [fintype α] {f g : perm α}
(h : _root_.commute f g) (x : α) (hx : g x = x) : (f * g).cycle_of x = f.cycle_of x :=
begin
ext y,
by_cases hxy : (f * g).same_cycle x y,
{ obtain ⟨z, rfl⟩ := hxy,
rw cycle_of_apply_apply_zpow_self,
simp [h.mul_zpow, zpow_apply_eq_self_of_apply_eq_self hx] },
{ rw [cycle_of_apply_of_not_same_cycle hxy, cycle_of_apply_of_not_same_cycle],
contrapose! hxy,
obtain ⟨z, rfl⟩ := hxy,
refine ⟨z, _⟩,
simp [h.mul_zpow, zpow_apply_eq_self_of_apply_eq_self hx] }
end
lemma disjoint.cycle_of_mul_distrib [fintype α] {f g : perm α} (h : f.disjoint g) (x : α) :
(f * g).cycle_of x = (f.cycle_of x * g.cycle_of x) :=
begin
cases (disjoint_iff_eq_or_eq.mp h) x with hfx hgx,
{ simp [h.commute.eq, cycle_of_mul_of_apply_right_eq_self h.symm.commute, hfx] },
{ simp [cycle_of_mul_of_apply_right_eq_self h.commute, hgx] }
end
lemma support_cycle_of_eq_nil_iff [fintype α] {f : perm α} {x : α} :
(f.cycle_of x).support = ∅ ↔ x ∉ f.support :=
by simp
lemma support_cycle_of_le [fintype α] (f : perm α) (x : α) :
support (f.cycle_of x) ≤ support f :=
begin
intros y hy,
rw [mem_support, cycle_of_apply] at hy,
split_ifs at hy,
{ exact mem_support.mpr hy },
{ exact absurd rfl hy }
end
lemma mem_support_cycle_of_iff [fintype α] {f : perm α} {x y : α} :
y ∈ support (f.cycle_of x) ↔ same_cycle f x y ∧ x ∈ support f :=
begin
by_cases hx : f x = x,
{ rw (cycle_of_eq_one_iff _).mpr hx,
simp [hx] },
{ rw [mem_support, cycle_of_apply],
split_ifs with hy,
{ simp only [hx, hy, iff_true, ne.def, not_false_iff, and_self, mem_support],
rcases hy with ⟨k, rfl⟩,
rw ←not_mem_support,
simpa using hx },
{ simpa [hx] using hy } }
end
lemma same_cycle.mem_support_iff [fintype α] {f : perm α} {x y : α} (h : same_cycle f x y) :
x ∈ support f ↔ y ∈ support f :=
⟨λ hx, support_cycle_of_le f x (mem_support_cycle_of_iff.mpr ⟨h, hx⟩),
λ hy, support_cycle_of_le f y (mem_support_cycle_of_iff.mpr ⟨h.symm, hy⟩)⟩
lemma pow_mod_card_support_cycle_of_self_apply [fintype α] (f : perm α) (n : ℕ) (x : α) :
(f ^ (n % (f.cycle_of x).support.card)) x = (f ^ n) x :=
begin
by_cases hx : f x = x,
{ rw [pow_apply_eq_self_of_apply_eq_self hx, pow_apply_eq_self_of_apply_eq_self hx] },
{ rw [←cycle_of_pow_apply_self, ←cycle_of_pow_apply_self f,
←order_of_is_cycle (is_cycle_cycle_of f hx), ←pow_eq_mod_order_of] }
end
/-- x is in the support of f iff cycle_of f x is a cycle.-/
lemma is_cycle_cycle_of_iff [fintype α] (f : perm α) {x : α} :
is_cycle (cycle_of f x) ↔ (f x ≠ x) :=
begin
split,
{ intro hx, rw ne.def, rw ← cycle_of_eq_one_iff f,
exact equiv.perm.is_cycle.ne_one hx, },
{ intro hx,
apply equiv.perm.is_cycle_cycle_of, exact hx }
end
/-!
### `cycle_factors`
-/
/-- Given a list `l : list α` and a permutation `f : perm α` whose nonfixed points are all in `l`,
recursively factors `f` into cycles. -/
def cycle_factors_aux [fintype α] : Π (l : list α) (f : perm α),
(∀ {x}, f x ≠ x → x ∈ l) →
{l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint}
| [] f h := ⟨[], by { simp only [imp_false, list.pairwise.nil, list.not_mem_nil, forall_const,
and_true, forall_prop_of_false, not_not, not_false_iff, list.prod_nil] at *,
ext, simp * }⟩
| (x::l) f h :=
if hx : f x = x then
cycle_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h, hy (by rwa h)) (h hy))
else let ⟨m, hm₁, hm₂, hm₃⟩ := cycle_factors_aux l ((cycle_of f x)⁻¹ * f)
(λ y hy, list.mem_of_ne_of_mem
(λ h : y = x,
by { rw [h, mul_apply, ne.def, inv_eq_iff_eq, cycle_of_apply_self] at hy, exact hy rfl })
(h (λ h : f y = y, by { rw [mul_apply, h, ne.def, inv_eq_iff_eq, cycle_of_apply] at hy,
split_ifs at hy; cc }))) in
⟨(cycle_of f x) :: m, by { rw [list.prod_cons, hm₁], simp },
λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ hg, hg.symm ▸ is_cycle_cycle_of _ hx)
(hm₂ g),
list.pairwise_cons.2 ⟨λ g hg y,
or_iff_not_imp_left.2 (λ hfy,
have hxy : same_cycle f x y := not_not.1 (mt cycle_of_apply_of_not_same_cycle hfy),
have hgm : g :: m.erase g ~ m := list.cons_perm_iff_perm_erase.2 ⟨hg, list.perm.refl _⟩,
have ∀ h ∈ m.erase g, disjoint g h, from
(list.pairwise_cons.1 ((hgm.pairwise_iff (λ a b (h : disjoint a b), h.symm)).2 hm₃)).1,
classical.by_cases id $ λ hgy : g y ≠ y,
(disjoint_prod_right _ this y).resolve_right $
have hsc : same_cycle f⁻¹ x (f y), by rwa [same_cycle_inv, same_cycle_apply],
by { rw [disjoint_prod_perm hm₃ hgm.symm, list.prod_cons,
← eq_inv_mul_iff_mul_eq] at hm₁,
rwa [hm₁, mul_apply, mul_apply, cycle_of_inv, hsc.cycle_of_apply,
inv_apply_self, inv_eq_iff_eq, eq_comm] }),
hm₃⟩⟩
lemma mem_list_cycles_iff {α : Type*} [finite α] {l : list (perm α)}
(h1 : ∀ σ : perm α, σ ∈ l → σ.is_cycle)
(h2 : l.pairwise disjoint) {σ : perm α} :
σ ∈ l ↔ σ.is_cycle ∧ ∀ (a : α) (h4 : σ a ≠ a), σ a = l.prod a :=
begin
suffices : σ.is_cycle → (σ ∈ l ↔ ∀ (a : α) (h4 : σ a ≠ a), σ a = l.prod a),
{ exact ⟨λ hσ, ⟨h1 σ hσ, (this (h1 σ hσ)).mp hσ⟩, λ hσ, (this hσ.1).mpr hσ.2⟩ },
intro h3,
classical,
casesI nonempty_fintype α,
split,
{ intros h a ha,
exact eq_on_support_mem_disjoint h h2 _ (mem_support.mpr ha) },
{ intros h,
have hσl : σ.support ⊆ l.prod.support,
{ intros x hx,
rw mem_support at hx,
rwa [mem_support, ←h _ hx] },
obtain ⟨a, ha, -⟩ := id h3,
rw ←mem_support at ha,
obtain ⟨τ, hτ, hτa⟩ := exists_mem_support_of_mem_support_prod (hσl ha),
have hτl : ∀ (x ∈ τ.support), τ x = l.prod x := eq_on_support_mem_disjoint hτ h2,
have key : ∀ (x ∈ σ.support ∩ τ.support), σ x = τ x,
{ intros x hx,
rw [h x (mem_support.mp (mem_of_mem_inter_left hx)), hτl x (mem_of_mem_inter_right hx)] },
convert hτ,
refine h3.eq_on_support_inter_nonempty_congr (h1 _ hτ) key _ ha,
exact key a (mem_inter_of_mem ha hτa) }
end
lemma list_cycles_perm_list_cycles {α : Type*} [finite α] {l₁ l₂ : list (perm α)}
(h₀ : l₁.prod = l₂.prod)
(h₁l₁ : ∀ σ : perm α, σ ∈ l₁ → σ.is_cycle) (h₁l₂ : ∀ σ : perm α, σ ∈ l₂ → σ.is_cycle)
(h₂l₁ : l₁.pairwise disjoint) (h₂l₂ : l₂.pairwise disjoint) :
l₁ ~ l₂ :=
begin
classical,
refine (list.perm_ext (nodup_of_pairwise_disjoint_cycles h₁l₁ h₂l₁)
(nodup_of_pairwise_disjoint_cycles h₁l₂ h₂l₂)).mpr (λ σ, _),
by_cases hσ : σ.is_cycle,
{ obtain ⟨a, ha⟩ := not_forall.mp (mt ext hσ.ne_one),
rw [mem_list_cycles_iff h₁l₁ h₂l₁, mem_list_cycles_iff h₁l₂ h₂l₂, h₀] },
{ exact iff_of_false (mt (h₁l₁ σ) hσ) (mt (h₁l₂ σ) hσ) }
end
/-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`. -/
def cycle_factors [fintype α] [linear_order α] (f : perm α) :
{l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} :=
cycle_factors_aux (univ.sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _))
/-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`,
without a linear order. -/
def trunc_cycle_factors [fintype α] (f : perm α) :
trunc {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} :=
quotient.rec_on_subsingleton (@univ α _).1
(λ l h, trunc.mk (cycle_factors_aux l f h))
(show ∀ x, f x ≠ x → x ∈ (@univ α _).1, from λ _ _, mem_univ _)
section cycle_factors_finset
variables [fintype α] (f : perm α)
/-- Factors a permutation `f` into a `finset` of disjoint cyclic permutations that multiply to `f`.
-/
def cycle_factors_finset : finset (perm α) :=
(trunc_cycle_factors f).lift
(λ (l : {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint}),
l.val.to_finset) (λ ⟨l, hl⟩ ⟨l', hl'⟩, list.to_finset_eq_of_perm _ _
(list_cycles_perm_list_cycles (hl'.left.symm ▸ hl.left) hl.right.left (hl'.right.left)
hl.right.right hl'.right.right))
lemma cycle_factors_finset_eq_list_to_finset {σ : perm α} {l : list (perm α)} (hn : l.nodup) :
σ.cycle_factors_finset = l.to_finset ↔ (∀ f : perm α, f ∈ l → f.is_cycle) ∧
l.pairwise disjoint ∧ l.prod = σ :=
begin
obtain ⟨⟨l', hp', hc', hd'⟩, hl⟩ := trunc.exists_rep σ.trunc_cycle_factors,
have ht : cycle_factors_finset σ = l'.to_finset,
{ rw [cycle_factors_finset, ←hl, trunc.lift_mk] },
rw ht,
split,
{ intro h,
have hn' : l'.nodup := nodup_of_pairwise_disjoint_cycles hc' hd',
have hperm : l ~ l' := list.perm_of_nodup_nodup_to_finset_eq hn hn' h.symm,
refine ⟨_, _, _⟩,
{ exact λ _ h, hc' _ (hperm.subset h) },
{ rwa list.perm.pairwise_iff disjoint.symmetric hperm },
{ rw [←hp', hperm.symm.prod_eq'],
refine hd'.imp _,
exact λ _ _, disjoint.commute } },
{ rintro ⟨hc, hd, hp⟩,
refine list.to_finset_eq_of_perm _ _ _,
refine list_cycles_perm_list_cycles _ hc' hc hd' hd,
rw [hp, hp'] }
end
lemma cycle_factors_finset_eq_finset {σ : perm α} {s : finset (perm α)} :
σ.cycle_factors_finset = s ↔ (∀ f : perm α, f ∈ s → f.is_cycle) ∧
∃ h : (s : set (perm α)).pairwise disjoint,
s.noncomm_prod id (h.mono' $ λ _ _, disjoint.commute) = σ :=
begin
obtain ⟨l, hl, rfl⟩ := s.exists_list_nodup_eq,
simp [cycle_factors_finset_eq_list_to_finset, hl],
end
lemma cycle_factors_finset_pairwise_disjoint :
(cycle_factors_finset f : set (perm α)).pairwise disjoint :=
(cycle_factors_finset_eq_finset.mp rfl).2.some
lemma cycle_factors_finset_mem_commute :
(cycle_factors_finset f : set (perm α)).pairwise commute :=
(cycle_factors_finset_pairwise_disjoint _).mono' $ λ _ _, disjoint.commute
/-- The product of cycle factors is equal to the original `f : perm α`. -/
lemma cycle_factors_finset_noncomm_prod
(comm : (cycle_factors_finset f : set (perm α)).pairwise commute :=
cycle_factors_finset_mem_commute f) :
f.cycle_factors_finset.noncomm_prod id comm = f :=
(cycle_factors_finset_eq_finset.mp rfl).2.some_spec
lemma mem_cycle_factors_finset_iff {f p : perm α} :
p ∈ cycle_factors_finset f ↔ p.is_cycle ∧ ∀ (a ∈ p.support), p a = f a :=
begin
obtain ⟨l, hl, hl'⟩ := f.cycle_factors_finset.exists_list_nodup_eq,
rw ←hl',
rw [eq_comm, cycle_factors_finset_eq_list_to_finset hl] at hl',
simpa [list.mem_to_finset, ne.def, ←hl'.right.right]
using mem_list_cycles_iff hl'.left hl'.right.left
end
lemma cycle_of_mem_cycle_factors_finset_iff {f : perm α} {x : α} :
cycle_of f x ∈ cycle_factors_finset f ↔ x ∈ f.support :=
begin
rw mem_cycle_factors_finset_iff,
split,
{ rintro ⟨hc, h⟩,
contrapose! hc,
rw [not_mem_support, ←cycle_of_eq_one_iff] at hc,
simp [hc] },
{ intros hx,
refine ⟨is_cycle_cycle_of _ (mem_support.mp hx), _⟩,
intros y hy,
rw mem_support at hy,
rw cycle_of_apply,
split_ifs with H,
{ refl },
{ rw cycle_of_apply_of_not_same_cycle H at hy,
contradiction } }
end
lemma mem_cycle_factors_finset_support_le {p f : perm α} (h : p ∈ cycle_factors_finset f) :
p.support ≤ f.support :=
begin
rw mem_cycle_factors_finset_iff at h,
intros x hx,
rwa [mem_support, ←h.right x hx, ←mem_support]
end
lemma cycle_factors_finset_eq_empty_iff {f : perm α} :
cycle_factors_finset f = ∅ ↔ f = 1 :=
by simpa [cycle_factors_finset_eq_finset] using eq_comm
@[simp] lemma cycle_factors_finset_one :
cycle_factors_finset (1 : perm α) = ∅ :=
by simp [cycle_factors_finset_eq_empty_iff]
@[simp] lemma cycle_factors_finset_eq_singleton_self_iff {f : perm α} :
f.cycle_factors_finset = {f} ↔ f.is_cycle :=
by simp [cycle_factors_finset_eq_finset]
lemma is_cycle.cycle_factors_finset_eq_singleton {f : perm α} (hf : is_cycle f) :
f.cycle_factors_finset = {f} :=
cycle_factors_finset_eq_singleton_self_iff.mpr hf
lemma cycle_factors_finset_eq_singleton_iff {f g : perm α} :
f.cycle_factors_finset = {g} ↔ f.is_cycle ∧ f = g :=
begin
suffices : f = g → (g.is_cycle ↔ f.is_cycle),
{ simpa [cycle_factors_finset_eq_finset, eq_comm] },
rintro rfl,
exact iff.rfl
end
/-- Two permutations `f g : perm α` have the same cycle factors iff they are the same. -/
lemma cycle_factors_finset_injective : function.injective (@cycle_factors_finset α _ _) :=
begin
intros f g h,
rw ←cycle_factors_finset_noncomm_prod f,
simpa [h] using cycle_factors_finset_noncomm_prod g
end
lemma disjoint.disjoint_cycle_factors_finset {f g : perm α} (h : disjoint f g) :
_root_.disjoint (cycle_factors_finset f) (cycle_factors_finset g) :=
begin
rw [disjoint_iff_disjoint_support] at h,
rw finset.disjoint_left,
intros x hx hy,
simp only [mem_cycle_factors_finset_iff, mem_support] at hx hy,
obtain ⟨⟨⟨a, ha, -⟩, hf⟩, -, hg⟩ := ⟨hx, hy⟩,
refine h.le_bot (_ : a ∈ f.support ∩ g.support),
simp [ha, ←hf a ha, ←hg a ha]
end
lemma disjoint.cycle_factors_finset_mul_eq_union {f g : perm α} (h : disjoint f g) :
cycle_factors_finset (f * g) = cycle_factors_finset f ∪ cycle_factors_finset g :=
begin
rw cycle_factors_finset_eq_finset,
refine ⟨_, _, _⟩,
{ simp [or_imp_distrib, mem_cycle_factors_finset_iff, forall_swap] },
{ rw [coe_union, set.pairwise_union_of_symmetric disjoint.symmetric],
exact ⟨cycle_factors_finset_pairwise_disjoint _, cycle_factors_finset_pairwise_disjoint _,
λ x hx y hy hxy, h.mono (mem_cycle_factors_finset_support_le hx)
(mem_cycle_factors_finset_support_le hy)⟩ },
{ rw noncomm_prod_union_of_disjoint h.disjoint_cycle_factors_finset,
rw [cycle_factors_finset_noncomm_prod, cycle_factors_finset_noncomm_prod] }
end
lemma disjoint_mul_inv_of_mem_cycle_factors_finset {f g : perm α} (h : f ∈ cycle_factors_finset g) :
disjoint (g * f⁻¹) f :=
begin
rw mem_cycle_factors_finset_iff at h,
intro x,
by_cases hx : f x = x,
{ exact or.inr hx },
{ refine or.inl _,
rw [mul_apply, ←h.right, apply_inv_self],
rwa [←support_inv, apply_mem_support, support_inv, mem_support] }
end
/-- If c is a cycle, a ∈ c.support and c is a cycle of f, then `c = f.cycle_of a` -/
lemma cycle_is_cycle_of {f c : equiv.perm α} {a : α}
(ha : a ∈ c.support) (hc : c ∈ f.cycle_factors_finset) : c = f.cycle_of a :=
begin
suffices : f.cycle_of a = c.cycle_of a,
{ rw this,
apply symm,
exact equiv.perm.is_cycle.cycle_of_eq
((equiv.perm.mem_cycle_factors_finset_iff.mp hc).left)
(equiv.perm.mem_support.mp ha), },
let hfc := (equiv.perm.disjoint_mul_inv_of_mem_cycle_factors_finset hc).symm,
let hfc2 := (perm.disjoint.commute hfc),
rw ← equiv.perm.cycle_of_mul_of_apply_right_eq_self hfc2,
simp only [hfc2.eq, inv_mul_cancel_right],
-- a est dans le support de c, donc pas dans celui de g c⁻¹
exact equiv.perm.not_mem_support.mp
(finset.disjoint_left.mp (equiv.perm.disjoint.disjoint_support hfc) ha),
end
end cycle_factors_finset
@[elab_as_eliminator] lemma cycle_induction_on [finite β] (P : perm β → Prop) (σ : perm β)
(base_one : P 1) (base_cycles : ∀ σ : perm β, σ.is_cycle → P σ)
(induction_disjoint : ∀ σ τ : perm β, disjoint σ τ → is_cycle σ → P σ → P τ → P (σ * τ)) :
P σ :=
begin
casesI nonempty_fintype β,
suffices :
∀ l : list (perm β), (∀ τ : perm β, τ ∈ l → τ.is_cycle) → l.pairwise disjoint → P l.prod,
{ classical,
let x := σ.trunc_cycle_factors.out,
exact (congr_arg P x.2.1).mp (this x.1 x.2.2.1 x.2.2.2) },
intro l,
induction l with σ l ih,
{ exact λ _ _, base_one },
{ intros h1 h2,
rw list.prod_cons,
exact induction_disjoint σ l.prod
(disjoint_prod_right _ (list.pairwise_cons.mp h2).1)
(h1 _ (list.mem_cons_self _ _))
(base_cycles σ (h1 σ (l.mem_cons_self σ)))
(ih (λ τ hτ, h1 τ (list.mem_cons_of_mem σ hτ)) h2.of_cons) }
end
lemma cycle_factors_finset_mul_inv_mem_eq_sdiff [fintype α] {f g : perm α}
(h : f ∈ cycle_factors_finset g) :
cycle_factors_finset (g * f⁻¹) = (cycle_factors_finset g) \ {f} :=
begin
revert f,
apply cycle_induction_on _ g,
{ simp },
{ intros σ hσ f hf,
simp only [cycle_factors_finset_eq_singleton_self_iff.mpr hσ, mem_singleton] at hf ⊢,
simp [hf] },
{ intros σ τ hd hc hσ hτ f,
simp_rw [hd.cycle_factors_finset_mul_eq_union, mem_union],
-- if only `wlog` could work here...
rintro (hf | hf),
{ rw [hd.commute.eq, union_comm, union_sdiff_distrib, sdiff_singleton_eq_erase,
erase_eq_of_not_mem, mul_assoc, disjoint.cycle_factors_finset_mul_eq_union, hσ hf],
{ rw mem_cycle_factors_finset_iff at hf,
intro x,
cases hd.symm x with hx hx,
{ exact or.inl hx },
{ refine or.inr _,
by_cases hfx : f x = x,
{ rw ←hfx,
simpa [hx] using hfx.symm },
{ rw mul_apply,
rw ←hf.right _ (mem_support.mpr hfx) at hx,
contradiction } } },
{ exact λ H, hd.disjoint_cycle_factors_finset.le_bot (mem_inter_of_mem hf H) } },
{ rw [union_sdiff_distrib, sdiff_singleton_eq_erase,
erase_eq_of_not_mem, mul_assoc, disjoint.cycle_factors_finset_mul_eq_union, hτ hf],
{ rw mem_cycle_factors_finset_iff at hf,
intro x,
cases hd x with hx hx,
{ exact or.inl hx },
{ refine or.inr _,
by_cases hfx : f x = x,
{ rw ←hfx,
simpa [hx] using hfx.symm },
{ rw mul_apply,
rw ←hf.right _ (mem_support.mpr hfx) at hx,
contradiction } } },
{ exact λ H, hd.disjoint_cycle_factors_finset.le_bot (mem_inter_of_mem H hf) } } }
end
lemma same_cycle.nat_of_mem_support [fintype α] (f : perm α) {x y : α} (h : same_cycle f x y)
(hx : x ∈ f.support) :
∃ (i : ℕ) (hi' : i < (f.cycle_of x).support.card), (f ^ i) x = y :=
begin
revert f,
intro f,
apply cycle_induction_on _ f,
{ simp },
{ intros g hg H hx,
rw mem_support at hx,
rw [hg.cycle_of_eq hx, ←order_of_is_cycle hg],
exact H.nat' },
{ rintros g h hd hg IH IH' ⟨m, rfl⟩ hx,
cases (disjoint_iff_eq_or_eq.mp hd) x with hgx hhx,
{ have hpow : ∀ (k : ℤ), ((g * h) ^ k) x = (h ^ k) x,
{ intro k,
suffices : (g ^ k) x = x,
{ simpa [hd.commute.eq, hd.commute.symm.mul_zpow] },
rw zpow_apply_eq_self_of_apply_eq_self,
simpa using hgx },
obtain ⟨k, hk, hk'⟩ := IH' _ _,
{ refine ⟨k, _, _⟩,
{ rw [←cycle_of_eq_one_iff] at hgx,
rwa [hd.cycle_of_mul_distrib, hgx, one_mul] },
{ simpa [←zpow_coe_nat, hpow] using hk' } },
{ use m,
simp [hpow] },
{ rw [mem_support, hd.commute.eq] at hx,
simpa [hgx] using hx } },
{ have hpow : ∀ (k : ℤ), ((g * h) ^ k) x = (g ^ k) x,
{ intro k,
suffices : (h ^ k) x = x,
{ simpa [hd.commute.mul_zpow] },
rw zpow_apply_eq_self_of_apply_eq_self,
simpa using hhx },
obtain ⟨k, hk, hk'⟩ := IH _ _,
{ refine ⟨k, _, _⟩,
{ rw [←cycle_of_eq_one_iff] at hhx,
rwa [hd.cycle_of_mul_distrib, hhx, mul_one] },
{ simpa [←zpow_coe_nat, hpow] using hk' } },
{ use m,
simp [hpow] },
{ simpa [hhx] using hx } } }
end
lemma same_cycle.nat [fintype α] (f : perm α) {x y : α} (h : same_cycle f x y) :
∃ (i : ℕ) (hi : 0 < i) (hi' : i ≤ (f.cycle_of x).support.card + 1), (f ^ i) x = y :=
begin
by_cases hx : x ∈ f.support,
{ obtain ⟨k, hk, hk'⟩ := same_cycle.nat_of_mem_support f h hx,
cases k,
{ refine ⟨(f.cycle_of x).support.card, _, self_le_add_right _ _, _⟩,
{ refine zero_lt_one.trans (one_lt_card_support_of_ne_one _),
simpa using hx },
{ simp only [perm.coe_one, id.def, pow_zero] at hk',
subst hk',
rw [←order_of_is_cycle (is_cycle_cycle_of _ (mem_support.mp hx)),
←cycle_of_pow_apply_self, pow_order_of_eq_one, one_apply] } },
{ exact ⟨k + 1, by simp, nat.le_succ_of_le hk.le, hk'⟩ } },
{ refine ⟨1, zero_lt_one, by simp, _⟩,
obtain ⟨k, rfl⟩ := h,
rw [not_mem_support] at hx,
rw [pow_apply_eq_self_of_apply_eq_self hx,
zpow_apply_eq_self_of_apply_eq_self hx] }
end
section generation
variables [finite β]
open subgroup
lemma closure_is_cycle : closure {σ : perm β | is_cycle σ} = ⊤ :=
begin
classical,
casesI nonempty_fintype β,
exact top_le_iff.mp (le_trans (ge_of_eq closure_is_swap) (closure_mono (λ _, is_swap.is_cycle))),
end
variables [fintype α]
lemma closure_cycle_adjacent_swap {σ : perm α} (h1 : is_cycle σ) (h2 : σ.support = ⊤) (x : α) :
closure ({σ, swap x (σ x)} : set (perm α)) = ⊤ :=
begin
let H := closure ({σ, swap x (σ x)} : set (perm α)),
have h3 : σ ∈ H := subset_closure (set.mem_insert σ _),
have h4 : swap x (σ x) ∈ H := subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)),
have step1 : ∀ (n : ℕ), swap ((σ ^ n) x) ((σ^(n+1)) x) ∈ H,
{ intro n,
induction n with n ih,
{ exact subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)) },
{ convert H.mul_mem (H.mul_mem h3 ih) (H.inv_mem h3),
rw [mul_swap_eq_swap_mul, mul_inv_cancel_right], refl } },
have step2 : ∀ (n : ℕ), swap x ((σ ^ n) x) ∈ H,
{ intro n,
induction n with n ih,
{ convert H.one_mem,
exact swap_self x },
{ by_cases h5 : x = (σ ^ n) x,
{ rw [pow_succ, mul_apply, ←h5], exact h4 },
by_cases h6 : x = (σ^(n+1)) x,
{ rw [←h6, swap_self], exact H.one_mem },
rw [swap_comm, ←swap_mul_swap_mul_swap h5 h6],
exact H.mul_mem (H.mul_mem (step1 n) ih) (step1 n) } },
have step3 : ∀ (y : α), swap x y ∈ H,
{ intro y,
have hx : x ∈ (⊤ : finset α) := finset.mem_univ x,
rw [←h2, mem_support] at hx,
have hy : y ∈ (⊤ : finset α) := finset.mem_univ y,
rw [←h2, mem_support] at hy,
cases is_cycle.exists_pow_eq h1 hx hy with n hn,
rw ← hn,
exact step2 n },
have step4 : ∀ (y z : α), swap y z ∈ H,
{ intros y z,
by_cases h5 : z = x,
{ rw [h5, swap_comm], exact step3 y },
by_cases h6 : z = y,
{ rw [h6, swap_self], exact H.one_mem },
rw [←swap_mul_swap_mul_swap h5 h6, swap_comm z x],
exact H.mul_mem (H.mul_mem (step3 y) (step3 z)) (step3 y) },
rw [eq_top_iff, ←closure_is_swap, closure_le],
rintros τ ⟨y, z, h5, h6⟩,
rw h6,
exact step4 y z,
end
lemma closure_cycle_coprime_swap {n : ℕ} {σ : perm α} (h0 : nat.coprime n (fintype.card α))
(h1 : is_cycle σ) (h2 : σ.support = finset.univ) (x : α) :
closure ({σ, swap x ((σ ^ n) x)} : set (perm α)) = ⊤ :=
begin
rw [←finset.card_univ, ←h2, ←order_of_is_cycle h1] at h0,
cases exists_pow_eq_self_of_coprime h0 with m hm,
have h2' : (σ ^ n).support = ⊤ := eq.trans (support_pow_coprime h0) h2,
have h1' : is_cycle ((σ ^ n) ^ (m : ℤ)) := by rwa ← hm at h1,
replace h1' : is_cycle (σ ^ n) := is_cycle_of_is_cycle_pow h1'
(le_trans (support_pow_le σ n) (ge_of_eq (congr_arg support hm))),
rw [eq_top_iff, ←closure_cycle_adjacent_swap h1' h2' x, closure_le, set.insert_subset],
exact ⟨subgroup.pow_mem (closure _) (subset_closure (set.mem_insert σ _)) n,
set.singleton_subset_iff.mpr (subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)))⟩,
end
lemma closure_prime_cycle_swap {σ τ : perm α} (h0 : (fintype.card α).prime) (h1 : is_cycle σ)
(h2 : σ.support = finset.univ) (h3 : is_swap τ) : closure ({σ, τ} : set (perm α)) = ⊤ :=
begin
obtain ⟨x, y, h4, h5⟩ := h3,
obtain ⟨i, hi⟩ := h1.exists_pow_eq (mem_support.mp
((finset.ext_iff.mp h2 x).mpr (finset.mem_univ x)))
(mem_support.mp ((finset.ext_iff.mp h2 y).mpr (finset.mem_univ y))),
rw [h5, ←hi],
refine closure_cycle_coprime_swap (nat.coprime.symm
(h0.coprime_iff_not_dvd.mpr (λ h, h4 _))) h1 h2 x,
cases h with m hm,
rwa [hm, pow_mul, ←finset.card_univ, ←h2, ←order_of_is_cycle h1,
pow_order_of_eq_one, one_pow, one_apply] at hi,
end
end generation
section
variables [fintype α] {σ τ : perm α}
noncomputable theory
lemma is_conj_of_support_equiv (f : {x // x ∈ (σ.support : set α)} ≃ {x // x ∈ (τ.support : set α)})
(hf : ∀ (x : α) (hx : x ∈ (σ.support : set α)), (f ⟨σ x, apply_mem_support.2 hx⟩ : α) =
τ ↑(f ⟨x,hx⟩)) :
is_conj σ τ :=
begin
refine is_conj_iff.2 ⟨equiv.extend_subtype f, _⟩,
rw mul_inv_eq_iff_eq_mul,
ext,
simp only [perm.mul_apply],
by_cases hx : x ∈ σ.support,
{ rw [equiv.extend_subtype_apply_of_mem, equiv.extend_subtype_apply_of_mem],
{ exact hf x (finset.mem_coe.2 hx) } },
{ rwa [not_not.1 ((not_congr mem_support).1 (equiv.extend_subtype_not_mem f _ _)),
not_not.1 ((not_congr mem_support).mp hx)] }
end
theorem is_cycle.is_conj (hσ : is_cycle σ) (hτ : is_cycle τ) (h : σ.support.card = τ.support.card) :
is_conj σ τ :=
begin
refine is_conj_of_support_equiv (hσ.zpowers_equiv_support.symm.trans
((zpowers_equiv_zpowers begin
rw [order_of_is_cycle hσ, h, order_of_is_cycle hτ],
end).trans hτ.zpowers_equiv_support)) _,
intros x hx,
simp only [perm.mul_apply, equiv.trans_apply, equiv.sum_congr_apply],
obtain ⟨n, rfl⟩ := hσ.exists_pow_eq (classical.some_spec hσ).1 (mem_support.1 hx),
apply eq.trans _ (congr rfl (congr rfl (congr rfl
(congr rfl (hσ.zpowers_equiv_support_symm_apply n).symm)))),
apply (congr rfl (congr rfl (congr rfl (hσ.zpowers_equiv_support_symm_apply (n + 1))))).trans _,
simp only [ne.def, is_cycle.zpowers_equiv_support_apply,
subtype.coe_mk, zpowers_equiv_zpowers_apply],
rw [pow_succ, perm.mul_apply],
end
theorem is_cycle.is_conj_iff (hσ : is_cycle σ) (hτ : is_cycle τ) :
is_conj σ τ ↔ σ.support.card = τ.support.card :=
⟨begin
intro h,
obtain ⟨π, rfl⟩ := is_conj_iff.1 h,
apply finset.card_congr (λ a ha, π a) (λ _ ha, _) (λ _ _ _ _ ab, π.injective ab) (λ b hb, _),
{ simp [mem_support.1 ha] },
{ refine ⟨π⁻¹ b, ⟨_, π.apply_inv_self b⟩⟩,
contrapose! hb,
rw [mem_support, not_not] at hb,
rw [mem_support, not_not, perm.mul_apply, perm.mul_apply, hb, perm.apply_inv_self] }
end, hσ.is_conj hτ⟩
@[simp]
lemma support_conj : (σ * τ * σ⁻¹).support = τ.support.map σ.to_embedding :=
begin
ext,
simp only [mem_map_equiv, perm.coe_mul, comp_app, ne.def, perm.mem_support, equiv.eq_symm_apply],
refl,
end
lemma card_support_conj : (σ * τ * σ⁻¹).support.card = τ.support.card :=
by simp
end
theorem disjoint.is_conj_mul {α : Type*} [finite α] {σ τ π ρ : perm α}
(hc1 : is_conj σ π) (hc2 : is_conj τ ρ)
(hd1 : disjoint σ τ) (hd2 : disjoint π ρ) :
is_conj (σ * τ) (π * ρ) :=
begin
classical,
casesI nonempty_fintype α,
obtain ⟨f, rfl⟩ := is_conj_iff.1 hc1,
obtain ⟨g, rfl⟩ := is_conj_iff.1 hc2,
have hd1' := coe_inj.2 hd1.support_mul,
have hd2' := coe_inj.2 hd2.support_mul,
rw [coe_union] at *,
have hd1'' := disjoint_coe.2 (disjoint_iff_disjoint_support.1 hd1),
have hd2'' := disjoint_coe.2 (disjoint_iff_disjoint_support.1 hd2),
refine is_conj_of_support_equiv _ _,
{ refine ((equiv.set.of_eq hd1').trans (equiv.set.union hd1''.le_bot)).trans
((equiv.sum_congr (subtype_equiv f (λ a, _)) (subtype_equiv g (λ a, _))).trans
((equiv.set.of_eq hd2').trans (equiv.set.union hd2''.le_bot)).symm);
{ simp only [set.mem_image, to_embedding_apply, exists_eq_right,
support_conj, coe_map, apply_eq_iff_eq] } },
{ intros x hx,
simp only [trans_apply, symm_trans_apply, set.of_eq_apply,
set.of_eq_symm_apply, equiv.sum_congr_apply],
rw [hd1', set.mem_union] at hx,
cases hx with hxσ hxτ,
{ rw [mem_coe, mem_support] at hxσ,
rw [set.union_apply_left hd1''.le_bot _, set.union_apply_left hd1''.le_bot _],
simp only [subtype_equiv_apply, perm.coe_mul, sum.map_inl, comp_app,
set.union_symm_apply_left, subtype.coe_mk, apply_eq_iff_eq],
{ have h := (hd2 (f x)).resolve_left _,
{ rw [mul_apply, mul_apply] at h,
rw [h, inv_apply_self, (hd1 x).resolve_left hxσ] },
{ rwa [mul_apply, mul_apply, inv_apply_self, apply_eq_iff_eq] } },
{ rwa [subtype.coe_mk, subtype.coe_mk, mem_coe, mem_support] },
{ rwa [subtype.coe_mk, subtype.coe_mk, perm.mul_apply,
(hd1 x).resolve_left hxσ, mem_coe, apply_mem_support, mem_support] } },
{ rw [mem_coe, ← apply_mem_support, mem_support] at hxτ,
rw [set.union_apply_right hd1''.le_bot _, set.union_apply_right hd1''.le_bot _],
simp only [subtype_equiv_apply, perm.coe_mul, sum.map_inr, comp_app,
set.union_symm_apply_right, subtype.coe_mk, apply_eq_iff_eq],
{ have h := (hd2 (g (τ x))).resolve_right _,
{ rw [mul_apply, mul_apply] at h,
rw [inv_apply_self, h, (hd1 (τ x)).resolve_right hxτ] },
{ rwa [mul_apply, mul_apply, inv_apply_self, apply_eq_iff_eq] } },
{ rwa [subtype.coe_mk, subtype.coe_mk, mem_coe, ← apply_mem_support, mem_support] },
{ rwa [subtype.coe_mk, subtype.coe_mk, perm.mul_apply,
(hd1 (τ x)).resolve_right hxτ, mem_coe, mem_support] } } }
end
section fixed_points
/-!
### Fixed points
-/
lemma fixed_point_card_lt_of_ne_one [fintype α] {σ : perm α} (h : σ ≠ 1) :
(filter (λ x, σ x = x) univ).card < fintype.card α - 1 :=
begin
rw [lt_tsub_iff_left, ← lt_tsub_iff_right, ← finset.card_compl,
finset.compl_filter],
exact one_lt_card_support_of_ne_one h
end
end fixed_points
end equiv.perm
|
232c2fed8c8ff99eaacbcfbee16326f9cb797b62 | 5c7fe6c4a9d4079b5457ffa5f061797d42a1cd65 | /src/exercises/src_34_constructing_inverses_axiom_of_choice.lean | d017bb3a6e13ba9476d7ca62a3566fe9a8f850ec | [] | no_license | gihanmarasingha/mth1001_tutorial | 8e0817feeb96e7c1bb3bac49b63e3c9a3a329061 | bb277eebd5013766e1418365b91416b406275130 | refs/heads/master | 1,675,008,746,310 | 1,607,993,443,000 | 1,607,993,443,000 | 321,511,270 | 3 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,053 | lean | import data.set.function
import .src_33_identity_and_inverse_functions
import tactic.norm_num
open function
namespace mth1001
/-
What about the 'converses' of these statements? Asserting the converses requires classical
reasoning and a principle called the *axiom of choice*.
Further, we'll use predicates `has_left_inverse` and `has_right_inverse`, which are merely
existentially quantified statements concerning left and right inverses.
-/
#print has_left_inverse
#print has_right_inverse
open classical
section aoc
/-
The axiom of choice is best explained via an example. Let `U` denote the type of all modules
currently running at Exeter and let `V` denote the type of all students at Exeter.
For `u : U` and `v : V`, we denote by `R u v` the notion that student `v` is enrolled on module `u`.
[Here, `R : U → V → Prop` is a predicate on the types `U` and `V`. A predicate on more than one
type is sometimes referred to as a *relation*. So `R` is a relation on `U` and `V`.]
We have `h : ∀ u : U, ∃ v : V, R u v`. That is, for every module `u`, there is a student `v` who is
enrolled on `u`.
The axiom of choice asserts, under these hypotheses, that there is a function `g : U → V` such that`
`R u (g u)` holds, for every `u : U`.
In our example, this means that for every module `u`, `g(u)` is a student enrolled on `u`.
In other words, the axiom of choice guarantees the existence of a function that has *chosen*, for
each module `u`, a student `v` enrolled on `u`. Clearly, this function is not unique! There are
many functions with this property, but we need the axiom of choice to ensure the *existence* of
such a function.
-/
variable U : Type*
variable V : Type*
variable R : U → V → Prop
example (h : ∀ u : U, ∃ v : V, R u v) : ∃ g : U → V, ∀ u : U, R u (g u) :=
axiom_of_choice h
end aoc
section constructing_inverses
variable {α : Type*}
variable {β : Type*}
/-
The axiom of choice gives a 'one-line' proof that a surjective function has a right inverse.
If you place your cursor after the last `unfold` below, you'll see that `h` asserts
`h : ∀ b : β, ∃ a : α, f a = b`.
In our expression of the axiom of choice, `β` takes the role of `U`, `α` the role of `V`, and
`f a = b` the role of `R b a`. The axiom of choice thus asserts the existence of `g : β → α`
such that `∀ β : β, f (g b) = b`. That is, `g` is a right inverse of `g`.
-/
theorem has_right_inverse_of_surjective {f : α → β} (h : surjective f) : has_right_inverse f :=
begin
unfold has_right_inverse, -- These four lines aren't neeeded for
unfold function.right_inverse, -- Lean, but help the human mathematician to
unfold function.left_inverse, -- understand the proof by unfolding
unfold function.surjective at h, -- the defintions.
exact axiom_of_choice h,
end
/-
The situation for left inverse is more complicated. In fact, it isn't even true in general that
an injective function has a left inverse. It's true precisely when the domain of the function is
inhabited (this is roughly the same as saying that the domain is non-empty, when viewed as a set).
In the proof below, we include the hypothesis `d : α`. This asserts that `d` is a term of type `α`.
Knowing this is equivalent to knowing that `α` is inhabited.
Here's a proof sketch:
Suppose `f : α → β`, `h : injective f`, and `d : α`. We want to construct `g : β → α` with the
property that `g (f a) = a`, for every `a : α`.
To begin, take `b : β`. There are two cases.
1. `∃ x : α, f x = b`
2. `¬(∃ x : α, f x = b)`
In the first case, we'll define `g b = x` (we need something like the axiom of choice for this).
In the second case, we'll define `g b = d`, where `d` is the 'default' term in `α` mentioned before.
It remains to show `g` is a left inverse of `f`.
Let `a : α`. We must show `g (f a) = a`. Write `b` for `f a`, so `f a = b`. We must show `g b = a`.
Certainly, `∃ x : α, f x = b`, as witnessed by `a : α`. Let `x : α` be the term used in the
construction to define `g b`, so `f x = b`. Then `g b = x`. It remains to show `x = a`.
But `f x = f a`. By injectivity of `f`, the goal follows.
-/
local attribute [instance] prop_decidable
/-
Finally, here's our proof that every injective function on an inhabited type has a left inverse.
-/
theorem has_left_inverse_of_injective {f : α → β} (h : injective f) (d : α) : has_left_inverse f :=
begin
unfold has_left_inverse, -- These three lines aren't needed by Lean
unfold left_inverse, -- but assist the human matheamtician in
unfold injective at h, -- understanding the proof.
-- Below, we produce the hypothesis required in applying the axiom of choice.
have h₂ : ∀ b : β, ∃ a : α, (f a = b) ∨ ¬(∃ x : α, f x = b),
{ intro b,
by_cases k : (∃ x : α, f x = b),
{ cases k with a k₂, -- This is the case where there is some `x` for which `f x = b`.
use a,
left,
exact k₂, },
{ use d, -- This is the case where there is no such `x`.
right,
exact k, }, },
have h₃ : ∃ g : β → α, ∀ b : β, f (g b) = b ∨ ¬(∃ x : α, f x = b) := axiom_of_choice h₂,
cases h₃ with g h₄,
use g,
intro a,
let b := f a,
specialize h₄ b,
cases h₄ with h₅ h₆,
{ have k₂ : g b = a, from h h₅,
show g (f a) = a, from k₂, },
{ exfalso,
apply h₆,
use a, },
end
/-
Finally, we have a partial converse to the result at the end of the previous file. Namely, we prove
that if `f : α → β` is bijective *and* if `α` is inhabited, then `f` is invertible.
-/
theorem invertible_of_bijective (f : α → β) (d : α) (k : bijective f) : invertible f :=
begin
cases k with fi fs, -- We have `fi : injective f` and `fs : surjective f`.
split, -- It suffices to prove `has_left_inverse f` and `has_right_inverse f`.
{ exact has_left_inverse_of_injective fi d, },
{ exact has_right_inverse_of_surjective fs, },
end
end constructing_inverses
end mth1001
|
5fb8f9fff46d95144a938fff69b30b9ec487d788 | 87a08a8e9b222ec02f3327dca4ae24590c1b3de9 | /src/topology/basic.lean | 847fed2874d0d6c405a6087ec1247762db1a3204 | [
"Apache-2.0"
] | permissive | naussicaa/mathlib | 86d05223517a39e80920549a8052f9cf0e0b77b8 | 1ef2c2df20cf45c21675d855436228c7ae02d47a | refs/heads/master | 1,592,104,950,080 | 1,562,073,069,000 | 1,562,073,069,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 37,437 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
Theory of topological spaces.
Parts of the formalization is based on the books:
N. Bourbaki: General Topology
I. M. James: Topologies and Uniformities
A major difference is that this formalization is heavily based on the filter library.
-/
import order.filter
open set filter lattice classical
local attribute [instance] prop_decidable
universes u v w
structure topological_space (α : Type u) :=
(is_open : set α → Prop)
(is_open_univ : is_open univ)
(is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t))
(is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s))
attribute [class] topological_space
section topological_space
variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop}
@[extensionality]
lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g
| ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl
section
variables [t : topological_space α]
include t
/-- `is_open s` means that `s` is open in the ambient topological space on `α` -/
def is_open (s : set α) : Prop := topological_space.is_open t s
@[simp]
lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t
lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) :=
topological_space.is_open_inter t s₁ s₂ h₁ h₂
lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) :=
topological_space.is_open_sUnion t s h
end
lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s :=
rfl
variables [topological_space α]
lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) :=
is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i
lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) :
is_open (⋃i∈s, f i) :=
is_open_Union $ assume i, is_open_Union $ assume hi, h i hi
lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) :=
by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩)
@[simp] lemma is_open_empty : is_open (∅ : set α) :=
by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim)
lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) :=
finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $
λ a s has hs ih h, by rw sInter_insert; exact
is_open_inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _)
lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) :
(∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) :=
finite.induction_on hs
(λ _, by rw bInter_empty; exact is_open_univ)
(λ a s has hs ih h, by rw bInter_insert; exact
is_open_inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi))))
lemma is_open_Inter [fintype β] {s : β → set α}
(h : ∀ i, is_open (s i)) : is_open (⋂ i, s i) :=
suffices is_open (⋂ (i : β) (hi : i ∈ @univ β), s i), by simpa,
is_open_bInter finite_univ (λ i _, h i)
lemma is_open_Inter_prop {p : Prop} {s : p → set α}
(h : ∀ h : p, is_open (s h)) : is_open (Inter s) :=
by by_cases p; simp *
lemma is_open_const {p : Prop} : is_open {a : α | p} :=
by_cases
(assume : p, begin simp only [this]; exact is_open_univ end)
(assume : ¬ p, begin simp only [this]; exact is_open_empty end)
lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} :=
is_open_inter
/-- A set is closed if its complement is open -/
def is_closed (s : set α) : Prop := is_open (-s)
@[simp] lemma is_closed_empty : is_closed (∅ : set α) :=
by unfold is_closed; rw compl_empty; exact is_open_univ
@[simp] lemma is_closed_univ : is_closed (univ : set α) :=
by unfold is_closed; rw compl_univ; exact is_open_empty
lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) :=
λ h₁ h₂, by unfold is_closed; rw compl_union; exact is_open_inter h₁ h₂
lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) :=
by simp only [is_closed, compl_sInter, sUnion_image]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht
lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) :=
is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i
@[simp] lemma is_open_compl_iff {s : set α} : is_open (-s) ↔ is_closed s := iff.rfl
@[simp] lemma is_closed_compl_iff {s : set α} : is_closed (-s) ↔ is_open s :=
by rw [←is_open_compl_iff, compl_compl]
lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) :=
is_open_inter h₁ $ is_open_compl_iff.mpr h₂
lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) :=
by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂
lemma is_closed_bUnion {s : set β} {f : β → set α} (hs : finite s) :
(∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) :=
finite.induction_on hs
(λ _, by rw bUnion_empty; exact is_closed_empty)
(λ a s has hs ih h, by rw bUnion_insert; exact
is_closed_union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi))))
lemma is_closed_Union [fintype β] {s : β → set α}
(h : ∀ i, is_closed (s i)) : is_closed (Union s) :=
suffices is_closed (⋃ (i : β) (hi : i ∈ @univ β), s i),
by convert this; simp [set.ext_iff],
is_closed_bUnion finite_univ (λ i _, h i)
lemma is_closed_Union_prop {p : Prop} {s : p → set α}
(h : ∀ h : p, is_closed (s h)) : is_closed (Union s) :=
by by_cases p; simp *
lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x})
(hq : is_closed {x | q x}) : is_closed {x | p x → q x} :=
have {x | p x → q x} = (- {x | p x}) ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or,
by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq
lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} :=
is_open_compl_iff.mpr
/-- The interior of a set `s` is the largest open subset of `s`. -/
def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s}
lemma mem_interior {s : set α} {x : α} :
x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t :=
by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm]
@[simp] lemma is_open_interior {s : set α} : is_open (interior s) :=
is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁
lemma interior_subset {s : set α} : interior s ⊆ s :=
sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂
lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s :=
subset_sUnion_of_mem ⟨h₂, h₁⟩
lemma interior_eq_of_open {s : set α} (h : is_open s) : interior s = s :=
subset.antisymm interior_subset (interior_maximal (subset.refl s) h)
lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s :=
⟨assume h, h ▸ is_open_interior, interior_eq_of_open⟩
lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s :=
by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and]
lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) :
s ⊆ interior t ↔ s ⊆ t :=
⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩
lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t :=
interior_maximal (subset.trans interior_subset h) is_open_interior
@[simp] lemma interior_empty : interior (∅ : set α) = ∅ :=
interior_eq_of_open is_open_empty
@[simp] lemma interior_univ : interior (univ : set α) = univ :=
interior_eq_of_open is_open_univ
@[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s :=
interior_eq_of_open is_open_interior
@[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t :=
subset.antisymm
(subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t))
(interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open_inter is_open_interior is_open_interior)
lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) :
interior (s ∪ t) = interior s :=
have interior (s ∪ t) ⊆ s, from
assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩,
classical.by_contradiction $ assume hx₂ : x ∉ s,
have u \ s ⊆ t,
from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂,
have u \ s ⊆ interior t,
by rwa subset_interior_iff_subset_of_open (is_open_diff hu₁ h₁),
have u \ s ⊆ ∅,
by rwa h₂ at this,
this ⟨hx₁, hx₂⟩,
subset.antisymm
(interior_maximal this is_open_interior)
(interior_mono $ subset_union_left _ _)
lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t :=
by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior]
/-- The closure of `s` is the smallest closed set containing `s`. -/
def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t}
@[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) :=
is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁
lemma subset_closure {s : set α} : s ⊆ closure s :=
subset_sInter $ assume t ⟨h₁, h₂⟩, h₂
lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t :=
sInter_subset_of_mem ⟨h₂, h₁⟩
lemma closure_eq_of_is_closed {s : set α} (h : is_closed s) : closure s = s :=
subset.antisymm (closure_minimal (subset.refl s) h) subset_closure
lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s :=
⟨assume h, h ▸ is_closed_closure, closure_eq_of_is_closed⟩
lemma closure_subset_iff_subset_of_is_closed {s t : set α} (h₁ : is_closed t) :
closure s ⊆ t ↔ s ⊆ t :=
⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩
lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t :=
closure_minimal (subset.trans h subset_closure) is_closed_closure
lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s :=
by rw subset.antisymm subset_closure h; exact is_closed_closure
@[simp] lemma closure_empty : closure (∅ : set α) = ∅ :=
closure_eq_of_is_closed is_closed_empty
lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ :=
begin
split; intro h,
{ rw set.eq_empty_iff_forall_not_mem,
intros x H,
simpa only [h] using subset_closure H },
{ exact (eq.symm h) ▸ closure_empty },
end
@[simp] lemma closure_univ : closure (univ : set α) = univ :=
closure_eq_of_is_closed is_closed_univ
@[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s :=
closure_eq_of_is_closed is_closed_closure
@[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t :=
subset.antisymm
(closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed_union is_closed_closure is_closed_closure)
(union_subset (closure_mono $ subset_union_left _ _) (closure_mono $ subset_union_right _ _))
lemma interior_subset_closure {s : set α} : interior s ⊆ closure s :=
subset.trans interior_subset subset_closure
lemma closure_eq_compl_interior_compl {s : set α} : closure s = - interior (- s) :=
begin
unfold interior closure is_closed,
rw [compl_sUnion, compl_image_set_of],
simp only [compl_subset_compl]
end
@[simp] lemma interior_compl {s : set α} : interior (- s) = - closure s :=
by simp [closure_eq_compl_interior_compl]
@[simp] lemma closure_compl {s : set α} : closure (- s) = - interior s :=
by simp [closure_eq_compl_interior_compl]
theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → o ∩ s ≠ ∅ :=
⟨λ h o oo ao os,
have s ⊆ -o, from λ x xs xo, @ne_empty_of_mem α (o∩s) x ⟨xo, xs⟩ os,
closure_minimal this (is_closed_compl_iff.2 oo) h ao,
λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc,
let ⟨x, hc, hs⟩ := exists_mem_of_ne_empty (H _ h₁ nc) in hc (h₂ hs)⟩
lemma dense_iff_inter_open {s : set α} : closure s = univ ↔ ∀ U, is_open U → U ≠ ∅ → U ∩ s ≠ ∅ :=
begin
split ; intro h,
{ intros U U_op U_ne,
cases exists_mem_of_ne_empty U_ne with x x_in,
exact mem_closure_iff.1 (by simp only [h]) U U_op x_in },
{ apply eq_univ_of_forall, intro x,
rw mem_closure_iff,
intros U U_op x_in,
exact h U U_op (ne_empty_of_mem x_in) },
end
/-- The frontier of a set is the set of points between the closure and interior. -/
def frontier (s : set α) : set α := closure s \ interior s
lemma frontier_eq_closure_inter_closure {s : set α} :
frontier s = closure s ∩ closure (- s) :=
by rw [closure_compl, frontier, diff_eq]
@[simp] lemma frontier_compl (s : set α) : frontier (-s) = frontier s :=
by simp only [frontier_eq_closure_inter_closure, lattice.neg_neg, inter_comm]
lemma is_closed_frontier {s : set α} : is_closed (frontier s) :=
by rw frontier_eq_closure_inter_closure; exact is_closed_inter is_closed_closure is_closed_closure
lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ :=
begin
have A : frontier s = s \ interior s, by rw [frontier, closure_eq_of_is_closed h],
have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _),
have C : interior (frontier s) ⊆ frontier s := interior_subset,
have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) :=
subset_inter B (by simpa [A] using C),
rwa [inter_diff_self, subset_empty_iff] at this,
end
/-- neighbourhood filter -/
def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s)
lemma nhds_def (a : α) : nhds a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) := rfl
lemma le_nhds_iff {f a} : f ≤ nhds a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f :=
by simp [nhds_def]
lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : principal s ≤ f) : nhds a ≤ f :=
by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf)
lemma nhds_sets {a : α} : (nhds a).sets = {s | ∃t⊆s, is_open t ∧ a ∈ t} :=
calc (nhds a).sets = (⋃s∈{s : set α| a ∈ s ∧ is_open s}, (principal s).sets) : infi_sets_eq'
(assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩,
⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩,
le_principal_iff.2 (inter_subset_left _ _),
le_principal_iff.2 (inter_subset_right _ _)⟩)
⟨univ, mem_univ _, is_open_univ⟩
... = {s | ∃t⊆s, is_open t ∧ a ∈ t} :
le_antisymm
(supr_le $ assume i, supr_le $ assume ⟨hi₁, hi₂⟩ t ht, ⟨i, ht, hi₂, hi₁⟩)
(assume t ⟨i, hi₁, hi₂, hi₃⟩, mem_Union.2 ⟨i, mem_Union.2 ⟨⟨hi₃, hi₂⟩, hi₁⟩⟩)
lemma map_nhds {a : α} {f : α → β} :
map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal (image f s)) :=
calc map f (nhds a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, map f (principal s)) :
map_binfi_eq
(assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩,
⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩,
le_principal_iff.2 (inter_subset_left _ _),
le_principal_iff.2 (inter_subset_right _ _)⟩)
⟨univ, mem_univ _, is_open_univ⟩
... = _ : by simp only [map_principal]
attribute [irreducible] nhds
lemma mem_nhds_sets_iff {a : α} {s : set α} :
s ∈ nhds a ↔ ∃t⊆s, is_open t ∧ a ∈ t :=
by simp only [nhds_sets, mem_set_of_eq, exists_prop]
lemma mem_of_nhds {a : α} {s : set α} : s ∈ nhds a → a ∈ s :=
λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs
lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) :
s ∈ nhds a :=
mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩
theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) :
(∀ s ∈ nhds x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) :=
iff.intro
(λ h s os xs, h s (mem_nhds_sets os xs))
(λ h t,
begin
change t ∈ (nhds x).sets → P t,
rw nhds_sets,
rintros ⟨s, hs, opens, xs⟩,
exact hP _ _ hs (h s opens xs),
end)
theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t)
(l : filter β) :
(∀ s ∈ nhds x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) :=
all_mem_nhds _ _ (λ s t ssubt h, mem_sets_of_superset h (hf s t ssubt))
theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} :
rtendsto r l (nhds a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) :=
all_mem_nhds_filter _ _ (λ s t h, λ x hx, λ y hy, h (hx y hy)) _
theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} :
rtendsto' r l (nhds a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) :=
by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono }
theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} :
ptendsto f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) :=
rtendsto_nhds
theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} :
ptendsto' f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) :=
rtendsto'_nhds
theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} :
tendsto f l (nhds a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) :=
all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _
lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (nhds a) :=
tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha
lemma pure_le_nhds : pure ≤ (nhds : α → filter α) :=
assume a, by rw nhds_def; exact le_infi
(assume s, le_infi $ assume ⟨h₁, _⟩, principal_mono.mpr $
singleton_subset_iff.2 h₁)
lemma tendsto_pure_nhds [topological_space β] (f : α → β) (a : α) :
tendsto f (pure a) (nhds (f a)) :=
begin
rw [tendsto, filter.map_pure],
exact pure_le_nhds (f a)
end
@[simp] lemma nhds_neq_bot {a : α} : nhds a ≠ ⊥ :=
assume : nhds a = ⊥,
have pure a = (⊥ : filter α),
from lattice.bot_unique $ this ▸ pure_le_nhds a,
pure_neq_bot this
lemma interior_eq_nhds {s : set α} : interior s = {a | nhds a ≤ principal s} :=
set.ext $ λ x, by simp only [mem_interior, le_principal_iff, mem_nhds_sets_iff]; refl
lemma mem_interior_iff_mem_nhds {s : set α} {a : α} :
a ∈ interior s ↔ s ∈ nhds a :=
by simp only [interior_eq_nhds, le_principal_iff]; refl
lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, nhds a ≤ principal s :=
calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm
... ↔ (∀a∈s, nhds a ≤ principal s) : by rw [interior_eq_nhds]; refl
lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ nhds a :=
is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff
lemma closure_eq_nhds {s : set α} : closure s = {a | nhds a ⊓ principal s ≠ ⊥} :=
calc closure s = - interior (- s) : closure_eq_compl_interior_compl
... = {a | ¬ nhds a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl
... = {a | nhds a ⊓ principal s ≠ ⊥} : set.ext $ assume a, not_congr
(inf_eq_bot_iff_le_compl
(show principal s ⊔ principal (-s) = ⊤, by simp only [sup_principal, union_compl_self, principal_univ])
(by simp only [inf_principal, inter_compl_self, principal_empty])).symm
theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ nhds a, t ∩ s ≠ ∅ :=
mem_closure_iff.trans
⟨λ H t ht, subset_ne_empty
(inter_subset_inter_left _ interior_subset)
(H _ is_open_interior (mem_interior_iff_mem_nhds.2 ht)),
λ H o oo ao, H _ (mem_nhds_sets oo ao)⟩
/-- `x` belongs to the closure of `s` if and only if some ultrafilter
supported on `s` converges to `x`. -/
lemma mem_closure_iff_ultrafilter {s : set α} {x : α} :
x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u.val ∧ u.val ≤ nhds x :=
begin
rw closure_eq_nhds, change nhds x ⊓ principal s ≠ ⊥ ↔ _, symmetry,
convert exists_ultrafilter_iff _, ext u,
rw [←le_principal_iff, inf_comm, le_inf_iff]
end
lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s :=
calc is_closed s ↔ closure s = s : by rw [closure_eq_iff_is_closed]
... ↔ closure s ⊆ s : ⟨assume h, by rw h, assume h, subset.antisymm h subset_closure⟩
... ↔ (∀a, nhds a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl
lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) :=
assume a ⟨hs, ht⟩,
have s ∈ nhds a, from mem_nhds_sets h hs,
have nhds a ⊓ principal s = nhds a, from inf_of_le_left $ by rwa le_principal_iff,
have nhds a ⊓ principal (s ∩ t) ≠ ⊥,
from calc nhds a ⊓ principal (s ∩ t) = nhds a ⊓ (principal s ⊓ principal t) : by rw inf_principal
... = nhds a ⊓ principal t : by rw [←inf_assoc, this]
... ≠ ⊥ : by rw [closure_eq_nhds] at ht; assumption,
by rw [closure_eq_nhds]; assumption
lemma closure_diff {s t : set α} : closure s - closure t ⊆ closure (s - t) :=
calc closure s \ closure t = (- closure t) ∩ closure s : by simp only [diff_eq, inter_comm]
... ⊆ closure (- closure t ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure
... = closure (s \ closure t) : by simp only [diff_eq, inter_comm]
... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure
lemma mem_of_closed_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α}
(hb : b ≠ ⊥) (hf : tendsto f b (nhds a)) (hs : is_closed s) (h : f ⁻¹' s ∈ b) : a ∈ s :=
have b.map f ≤ nhds a ⊓ principal s,
from le_trans (le_inf (le_refl _) (le_principal_iff.mpr h)) (inf_le_inf hf (le_refl _)),
is_closed_iff_nhds.mp hs a $ neq_bot_of_le_neq_bot (map_ne_bot hb) this
lemma mem_of_closed_of_tendsto' {f : β → α} {x : filter β} {a : α} {s : set α}
(hf : tendsto f x (nhds a)) (hs : is_closed s) (h : x ⊓ principal (f ⁻¹' s) ≠ ⊥) : a ∈ s :=
is_closed_iff_nhds.mp hs _ $ neq_bot_of_le_neq_bot (@map_ne_bot _ _ _ f h) $
le_inf (le_trans (map_mono $ inf_le_left) hf) $
le_trans (map_mono $ inf_le_right_of_le $ by simp only [comap_principal, le_principal_iff]; exact subset.refl _) (@map_comap_le _ _ _ f)
lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α}
(hb : b ≠ ⊥) (hf : tendsto f b (nhds a)) (h : f ⁻¹' s ∈ b) : a ∈ closure s :=
mem_of_closed_of_tendsto hb hf (is_closed_closure) $
filter.mem_sets_of_superset h (preimage_mono subset_closure)
section lim
variables [inhabited α]
/-- If `f` is a filter, then `lim f` is a limit of the filter, if it exists. -/
noncomputable def lim (f : filter α) : α := epsilon $ λa, f ≤ nhds a
lemma lim_spec {f : filter α} (h : ∃a, f ≤ nhds a) : f ≤ nhds (lim f) := epsilon_spec h
end lim
/-
The nhds_within filter.
-/
def nhds_within (a : α) (s : set α) : filter α := nhds a ⊓ principal s
theorem nhds_within_eq (a : α) (s : set α) :
nhds_within a s = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (t ∩ s) :=
have set.univ ∈ {s : set α | a ∈ s ∧ is_open s}, from ⟨set.mem_univ _, is_open_univ⟩,
begin
rw [nhds_within, nhds, lattice.binfi_inf]; try { exact this },
simp only [inf_principal]
end
theorem nhds_within_univ (a : α) : nhds_within a set.univ = nhds a :=
by rw [nhds_within, principal_univ, lattice.inf_top_eq]
theorem mem_nhds_within (t : set α) (a : α) (s : set α) :
t ∈ nhds_within a s ↔ ∃ u, is_open u ∧ a ∈ u ∧ u ∩ s ⊆ t :=
begin
rw [nhds_within, mem_inf_principal, mem_nhds_sets_iff], split,
{ rintros ⟨u, hu, openu, au⟩,
exact ⟨u, openu, au, λ x ⟨xu, xs⟩, hu xu xs⟩ },
rintros ⟨u, openu, au, hu⟩,
exact ⟨u, λ x xu xs, hu ⟨xu, xs⟩, openu, au⟩
end
theorem self_mem_nhds_within {a : α} {s : set α} : s ∈ nhds_within a s :=
begin
rw [nhds_within, mem_inf_principal],
simp only [imp_self],
exact univ_mem_sets
end
theorem inter_mem_nhds_within (s : set α) {t : set α} {a : α} (h : t ∈ nhds a) :
s ∩ t ∈ nhds_within a s :=
inter_mem_sets (mem_inf_sets_of_right (mem_principal_self s)) (mem_inf_sets_of_left h)
theorem nhds_within_mono (a : α) {s t : set α} (h : s ⊆ t) : nhds_within a s ≤ nhds_within a t :=
lattice.inf_le_inf (le_refl _) (principal_mono.mpr h)
theorem nhds_within_restrict'' {a : α} (s : set α) {t : set α} (h : t ∈ nhds_within a s) :
nhds_within a s = nhds_within a (s ∩ t) :=
le_antisymm
(lattice.le_inf lattice.inf_le_left (le_principal_iff.mpr (inter_mem_sets self_mem_nhds_within h)))
(lattice.inf_le_inf (le_refl _) (principal_mono.mpr (set.inter_subset_left _ _)))
theorem nhds_within_restrict' {a : α} (s : set α) {t : set α} (h : t ∈ nhds a) :
nhds_within a s = nhds_within a (s ∩ t) :=
nhds_within_restrict'' s $ mem_inf_sets_of_left h
theorem nhds_within_restrict {a : α} (s : set α) {t : set α} (h₀ : a ∈ t) (h₁ : is_open t) :
nhds_within a s = nhds_within a (s ∩ t) :=
nhds_within_restrict' s (mem_nhds_sets h₁ h₀)
theorem nhds_within_le_of_mem {a : α} {s t : set α} (h : s ∈ nhds_within a t) :
nhds_within a t ≤ nhds_within a s :=
begin
rcases (mem_nhds_within _ _ _).1 h with ⟨u, u_open, au, uts⟩,
have : nhds_within a t = nhds_within a (t ∩ u) := nhds_within_restrict _ au u_open,
rw [this, inter_comm],
exact nhds_within_mono _ uts
end
theorem nhds_within_eq_nhds_within {a : α} {s t u : set α}
(h₀ : a ∈ s) (h₁ : is_open s) (h₂ : t ∩ s = u ∩ s) :
nhds_within a t = nhds_within a u :=
by rw [nhds_within_restrict t h₀ h₁, nhds_within_restrict u h₀ h₁, h₂]
theorem nhds_within_eq_of_open {a : α} {s : set α} (h₀ : a ∈ s) (h₁ : is_open s) :
nhds_within a s = nhds a :=
by rw [←nhds_within_univ]; apply nhds_within_eq_nhds_within h₀ h₁;
rw [set.univ_inter, set.inter_self]
@[simp] theorem nhds_within_empty (a : α) : nhds_within a {} = ⊥ :=
by rw [nhds_within, principal_empty, lattice.inf_bot_eq]
theorem nhds_within_union (a : α) (s t : set α) :
nhds_within a (s ∪ t) = nhds_within a s ⊔ nhds_within a t :=
by unfold nhds_within; rw [←lattice.inf_sup_left, sup_principal]
theorem nhds_within_inter (a : α) (s t : set α) :
nhds_within a (s ∩ t) = nhds_within a s ⊓ nhds_within a t :=
by unfold nhds_within; rw [lattice.inf_left_comm, lattice.inf_assoc, inf_principal,
←lattice.inf_assoc, lattice.inf_idem]
theorem nhds_within_inter' (a : α) (s t : set α) :
nhds_within a (s ∩ t) = (nhds_within a s) ⊓ principal t :=
by { unfold nhds_within, rw [←inf_principal, lattice.inf_assoc] }
theorem tendsto_if_nhds_within {f g : α → β} {p : α → Prop} [decidable_pred p]
{a : α} {s : set α} {l : filter β}
(h₀ : tendsto f (nhds_within a (s ∩ p)) l)
(h₁ : tendsto g (nhds_within a (s ∩ {x | ¬ p x})) l) :
tendsto (λ x, if p x then f x else g x) (nhds_within a s) l :=
by apply tendsto_if; rw [←nhds_within_inter']; assumption
lemma map_nhds_within (f : α → β) (a : α) (s : set α) :
map f (nhds_within a s) =
⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, principal (set.image f (t ∩ s)) :=
have h₀ : directed_on ((λ (i : set α), principal (i ∩ s)) ⁻¹'o ge)
{x : set α | x ∈ {t : set α | a ∈ t ∧ is_open t}}, from
assume x ⟨ax, openx⟩ y ⟨ay, openy⟩,
⟨x ∩ y, ⟨⟨ax, ay⟩, is_open_inter openx openy⟩,
le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_left _ _)),
le_principal_iff.mpr (set.inter_subset_inter_left _ (set.inter_subset_right _ _))⟩,
have h₁ : ∃ (i : set α), i ∈ {t : set α | a ∈ t ∧ is_open t},
from ⟨set.univ, set.mem_univ _, is_open_univ⟩,
by { rw [nhds_within_eq, map_binfi_eq h₀ h₁], simp only [map_principal] }
theorem tendsto_nhds_within_mono_left {f : α → β} {a : α}
{s t : set α} {l : filter β} (hst : s ⊆ t) (h : tendsto f (nhds_within a t) l) :
tendsto f (nhds_within a s) l :=
tendsto_le_left (nhds_within_mono a hst) h
theorem tendsto_nhds_within_mono_right {f : β → α} {l : filter β}
{a : α} {s t : set α} (hst : s ⊆ t) (h : tendsto f l (nhds_within a s)) :
tendsto f l (nhds_within a t) :=
tendsto_le_right (nhds_within_mono a hst) h
theorem tendsto_nhds_within_of_tendsto_nhds {f : α → β} {a : α}
{s : set α} {l : filter β} (h : tendsto f (nhds a) l) :
tendsto f (nhds_within a s) l :=
by rw [←nhds_within_univ] at h; exact tendsto_nhds_within_mono_left (set.subset_univ _) h
/- locally finite family [General Topology (Bourbaki, 1995)] -/
section locally_finite
/-- A family of sets in `set α` is locally finite if at every point `x:α`,
there is a neighborhood of `x` which meets only finitely many sets in the family -/
def locally_finite (f : β → set α) :=
∀x:α, ∃t ∈ nhds x, finite {i | f i ∩ t ≠ ∅ }
lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f :=
assume x, ⟨univ, univ_mem_sets, finite_subset h $ subset_univ _⟩
lemma locally_finite_subset
{f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ :=
assume a,
let ⟨t, ht₁, ht₂⟩ := hf₂ a in
⟨t, ht₁, finite_subset ht₂ $ assume i hi,
neq_bot_of_le_neq_bot hi $ inter_subset_inter (hf i) $ subset.refl _⟩
lemma is_closed_Union_of_locally_finite {f : β → set α}
(h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) :=
is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i),
have ∀i, a ∈ -f i,
from assume i hi, h $ mem_Union.2 ⟨i, hi⟩,
have ∀i, - f i ∈ (nhds a).sets,
by rw [nhds_sets]; exact assume i, ⟨- f i, subset.refl _, h₂ i, this i⟩,
let ⟨t, h_sets, (h_fin : finite {i | f i ∩ t ≠ ∅ })⟩ := h₁ a in
calc nhds a ≤ principal (t ∩ (⋂ i∈{i | f i ∩ t ≠ ∅ }, - f i)) :
begin
rw [le_principal_iff],
apply @filter.inter_mem_sets _ (nhds a) _ _ h_sets,
apply @filter.Inter_mem_sets _ (nhds a) _ _ _ h_fin,
exact assume i h, this i
end
... ≤ principal (- ⋃i, f i) :
begin
simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq,
mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists,
not_eq_empty_iff_exists, exists_imp_distrib, (≠)],
exact assume x xt ht i xfi, ht i x xfi xt xfi
end
end locally_finite
end topological_space
section continuous
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
variables [topological_space α] [topological_space β] [topological_space γ]
/-- A function between topological spaces is continuous if the preimage
of every open set is open. -/
def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s)
def continuous_at (f : α → β) (x : α) := tendsto f (nhds x) (nhds (f x))
def continuous_within_at (f : α → β) (s : set α) (x : α) : Prop :=
tendsto f (nhds_within x s) (nhds (f x))
def continuous_on (f : α → β) (s : set α) : Prop := ∀ x ∈ s, continuous_within_at f s x
lemma continuous_id : continuous (id : α → α) :=
assume s h, h
lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) :
continuous (g ∘ f) :=
assume s h, hf _ (hg s h)
lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) :
tendsto f (nhds x) (nhds (f x)) | s :=
show s ∈ nhds (f x) → s ∈ map f (nhds x),
by simp [nhds_sets]; exact
assume t t_subset t_open fx_in_t,
⟨f ⁻¹' t, preimage_mono t_subset, hf t t_open, fx_in_t⟩
lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x :=
⟨continuous.tendsto,
assume hf : ∀x, tendsto f (nhds x) (nhds (f x)),
assume s, assume hs : is_open s,
have ∀a, f a ∈ s → s ∈ nhds (f a),
by simp [nhds_sets]; exact assume a ha, ⟨s, subset.refl s, hs, ha⟩,
show is_open (f ⁻¹' s),
by simp [is_open_iff_nhds]; exact assume a ha, hf a (this a ha)⟩
lemma continuous_const {b : β} : continuous (λa:α, b) :=
continuous_iff_continuous_at.mpr $ assume a, tendsto_const_nhds
lemma continuous_iff_is_closed {f : α → β} :
continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) :=
⟨assume hf s hs, hf (-s) hs,
assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩
lemma continuous_at_iff_ultrafilter {f : α → β} (x) : continuous_at f x ↔
∀ g, is_ultrafilter g → g ≤ nhds x → g.map f ≤ nhds (f x) :=
tendsto_iff_ultrafilter f (nhds x) (nhds (f x))
lemma continuous_iff_ultrafilter {f : α → β} :
continuous f ↔ ∀ x g, is_ultrafilter g → g ≤ nhds x → g.map f ≤ nhds (f x) :=
by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter]
lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)}
(hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) :
continuous (λa, @ite (p a) (h a) β (f a) (g a)) :=
continuous_iff_is_closed.mpr $
assume s hs,
have (λa, ite (p a) (f a) (g a)) ⁻¹' s =
(closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s),
from set.ext $ assume a,
classical.by_cases
(assume : a ∈ frontier {a | p a},
have hac : a ∈ closure {a | p a}, from this.left,
have hai : a ∈ closure {a | ¬ p a},
from have a ∈ - interior {a | p a}, from this.right, by rwa [←closure_compl] at this,
by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt})
(assume hf : a ∈ - frontier {a | p a},
classical.by_cases
(assume : p a,
have hc : a ∈ closure {a | p a}, from subset_closure this,
have hnc : a ∉ closure {a | ¬ p a},
by show a ∉ closure (- {a | p a}); rw [closure_compl]; simpa [frontier, hc] using hf,
by simp [this, hc, hnc])
(assume : ¬ p a,
have hc : a ∈ closure {a | ¬ p a}, from subset_closure this,
have hnc : a ∉ closure {a | p a},
begin
have hc : a ∈ closure (- {a | p a}), from hc,
simp [closure_compl] at hc,
simpa [frontier, hc] using hf
end,
by simp [this, hc, hnc])),
by rw [this]; exact is_closed_union
(is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs)
(is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs)
/- Continuity and partial functions -/
def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s)
lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom :=
by rw [←pfun.preimage_univ]; exact h _ is_open_univ
lemma pcontinuous_iff' {f : α →. β} :
pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (nhds x) (nhds y) :=
begin
split,
{ intros h x y h',
rw [ptendsto'_def],
change ∀ (s : set β), s ∈ (nhds y).sets → pfun.preimage f s ∈ (nhds x).sets,
rw [nhds_sets, nhds_sets],
rintros s ⟨t, tsubs, opent, yt⟩,
exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩
},
intros hf s os,
rw is_open_iff_nhds,
rintros x ⟨y, ys, fxy⟩ t,
rw [mem_principal_sets],
assume h : f.preimage s ⊆ t,
change t ∈ nhds x,
apply mem_sets_of_superset _ h,
have h' : ∀ s ∈ nhds y, f.preimage s ∈ nhds x,
{ intros s hs,
have : ptendsto' f (nhds x) (nhds y) := hf fxy,
rw ptendsto'_def at this,
exact this s hs },
show f.preimage s ∈ nhds x,
apply h', rw mem_nhds_sets_iff, exact ⟨s, set.subset.refl _, os, ys⟩
end
lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) :
f '' closure s ⊆ closure (f '' s) :=
have ∀ (a : α), nhds a ⊓ principal s ≠ ⊥ → nhds (f a) ⊓ principal (f '' s) ≠ ⊥,
from assume a ha,
have h₁ : ¬ map f (nhds a ⊓ principal s) = ⊥,
by rwa[map_eq_bot_iff],
have h₂ : map f (nhds a ⊓ principal s) ≤ nhds (f a) ⊓ principal (f '' s),
from le_inf
(le_trans (map_mono inf_le_left) $ by rw [continuous_iff_continuous_at] at h; exact h a)
(le_trans (map_mono inf_le_right) $ by simp; exact subset.refl _),
neq_bot_of_le_neq_bot h₁ h₂,
by simp [image_subset_iff, closure_eq_nhds]; assumption
lemma mem_closure [topological_space α] [topological_space β]
{s : set α} {t : set β} {f : α → β} {a : α}
(hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t :=
subset.trans (image_closure_subset_closure_image hf) (closure_mono $ image_subset_iff.2 ht) $
(mem_image_of_mem f ha)
end continuous
|
a43955b6f0ddc41244f786c7ac7ee0071196493b | 0ed3609caf1962115b28aeb010d2bda5f67ddc4c | /src/algebra/group_power.lean | b913b4b9abab5e0f847aad72ab4f9c6b6ee940f0 | [
"Apache-2.0"
] | permissive | jonaslippert/mathlib | 82dba29632969e3ed1c153a6454306f6bc9d9037 | 1435a196db69a7886a11e310e8923f3dcf249b81 | refs/heads/master | 1,609,938,673,069 | 1,582,018,388,000 | 1,582,018,388,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,718 | lean | /-
Copyright (c) 2015 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Robert Y. Lewis
The power operation on monoids and groups. We separate this from group, because it depends on
nat, which in turn depends on other parts of algebra.
We have "pow a n" for natural number powers, and "gpow a i" for integer powers. The notation
a^n is used for the first, but users can locally redefine it to gpow when needed.
Note: power adopts the convention that 0^0=1.
-/
import algebra.group
import data.int.basic data.list.basic
variables {M : Type*} {N : Type*} {G : Type*} {H : Type*} {A : Type*} {B : Type*}
{R : Type*} {S : Type*}
/-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/
def monoid.pow [monoid M] (a : M) : ℕ → M
| 0 := 1
| (n+1) := a * monoid.pow n
def add_monoid.smul [add_monoid A] (n : ℕ) (a : A) : A :=
@monoid.pow (multiplicative A) _ a n
precedence `•`:70
localized "infix ` • ` := add_monoid.smul" in add_monoid
@[priority 5] instance monoid.has_pow [monoid M] : has_pow M ℕ := ⟨monoid.pow⟩
/-!
### (Additive) monoid
-/
section monoid
variables [monoid M] [monoid N] [add_monoid A] [add_monoid B]
@[simp] theorem pow_zero (a : M) : a^0 = 1 := rfl
@[simp] theorem add_monoid.zero_smul (a : A) : 0 • a = 0 := rfl
theorem pow_succ (a : M) (n : ℕ) : a^(n+1) = a * a^n := rfl
theorem succ_smul (a : A) (n : ℕ) : (n+1)•a = a + n•a := rfl
@[simp] theorem pow_one (a : M) : a^1 = a := mul_one _
@[simp] theorem add_monoid.one_smul (a : A) : 1•a = a := add_zero _
theorem pow_mul_comm' (a : M) (n : ℕ) : a^n * a = a * a^n :=
by induction n with n ih; [rw [pow_zero, one_mul, mul_one],
rw [pow_succ, mul_assoc, ih]]
theorem smul_add_comm' : ∀ (a : A) (n : ℕ), n•a + a = a + n•a :=
@pow_mul_comm' (multiplicative A) _
theorem pow_succ' (a : M) (n : ℕ) : a^(n+1) = a^n * a :=
by rw [pow_succ, pow_mul_comm']
theorem succ_smul' (a : A) (n : ℕ) : (n+1)•a = n•a + a :=
by rw [succ_smul, smul_add_comm']
theorem pow_two (a : M) : a^2 = a * a :=
show a*(a*1)=a*a, by rw mul_one
theorem two_smul (a : A) : 2•a = a + a :=
show a+(a+0)=a+a, by rw add_zero
theorem pow_add (a : M) (m n : ℕ) : a^(m + n) = a^m * a^n :=
by induction n with n ih; [rw [add_zero, pow_zero, mul_one],
rw [pow_succ, ← pow_mul_comm', ← mul_assoc, ← ih, ← pow_succ']]; refl
theorem add_monoid.add_smul : ∀ (a : A) (m n : ℕ), (m + n)•a = m•a + n•a :=
@pow_add (multiplicative A) _
@[simp] theorem one_pow (n : ℕ) : (1 : M)^n = 1 :=
by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]]
@[simp] theorem add_monoid.smul_zero (n : ℕ) : n•(0 : A) = 0 :=
by induction n with n ih; [refl, rw [succ_smul, ih, zero_add]]
theorem pow_mul (a : M) (m n : ℕ) : a^(m * n) = (a^m)^n :=
by induction n with n ih; [rw mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl
theorem add_monoid.mul_smul' : ∀ (a : A) (m n : ℕ), m * n • a = n•(m•a) :=
@pow_mul (multiplicative A) _
theorem pow_mul' (a : M) (m n : ℕ) : a^(m * n) = (a^n)^m :=
by rw [mul_comm, pow_mul]
theorem add_monoid.mul_smul (a : A) (m n : ℕ) : m * n • a = m•(n•a) :=
by rw [mul_comm, add_monoid.mul_smul']
@[simp] theorem add_monoid.smul_one [has_one A] : ∀ n : ℕ, n • (1 : A) = n :=
nat.eq_cast _ (add_monoid.zero_smul _) (add_monoid.one_smul _) (add_monoid.add_smul _)
theorem pow_bit0 (a : M) (n : ℕ) : a ^ bit0 n = a^n * a^n := pow_add _ _ _
theorem bit0_smul (a : A) (n : ℕ) : bit0 n • a = n•a + n•a := add_monoid.add_smul _ _ _
theorem pow_bit1 (a : M) (n : ℕ) : a ^ bit1 n = a^n * a^n * a :=
by rw [bit1, pow_succ', pow_bit0]
theorem bit1_smul : ∀ (a : A) (n : ℕ), bit1 n • a = n•a + n•a + a :=
@pow_bit1 (multiplicative A) _
theorem pow_mul_comm (a : M) (m n : ℕ) : a^m * a^n = a^n * a^m :=
by rw [←pow_add, ←pow_add, add_comm]
theorem smul_add_comm : ∀ (a : A) (m n : ℕ), m•a + n•a = n•a + m•a :=
@pow_mul_comm (multiplicative A) _
@[simp] theorem list.prod_repeat (a : M) (n : ℕ) : (list.repeat a n).prod = a ^ n :=
by induction n with n ih; [refl, rw [list.repeat_succ, list.prod_cons, ih]]; refl
@[simp] theorem list.sum_repeat : ∀ (a : A) (n : ℕ), (list.repeat a n).sum = n • a :=
@list.prod_repeat (multiplicative A) _
theorem monoid_hom.map_pow (f : M →* N) (a : M) : ∀(n : ℕ), f (a ^ n) = (f a) ^ n
| 0 := f.map_one
| (n+1) := by rw [pow_succ, pow_succ, f.map_mul, monoid_hom.map_pow]
theorem add_monoid_hom.map_smul (f : A →+ B) (a : A) (n : ℕ) : f (n • a) = n • f a :=
f.to_multiplicative.map_pow a n
theorem is_monoid_hom.map_pow (f : M → N) [is_monoid_hom f] (a : M) :
∀(n : ℕ), f (a ^ n) = (f a) ^ n :=
(monoid_hom.of f).map_pow a
theorem is_add_monoid_hom.map_smul (f : A → B) [is_add_monoid_hom f] (a : A) (n : ℕ) :
f (n • a) = n • f a :=
(add_monoid_hom.of f).map_smul a n
@[simp] lemma units.coe_pow (u : units M) (n : ℕ) : ((u ^ n : units M) : M) = u ^ n :=
(units.coe_hom M).map_pow u n
end monoid
@[simp] theorem nat.pow_eq_pow (p q : ℕ) :
@has_pow.pow _ _ monoid.has_pow p q = p ^ q :=
by induction q with q ih; [refl, rw [nat.pow_succ, pow_succ, mul_comm, ih]]
@[simp] theorem nat.smul_eq_mul (m n : ℕ) : m • n = m * n :=
by induction m with m ih; [rw [add_monoid.zero_smul, zero_mul],
rw [succ_smul', ih, nat.succ_mul]]
/-!
### Commutative (additive) monoid
-/
section comm_monoid
variables [comm_monoid M] [add_comm_monoid A]
theorem mul_pow (a b : M) (n : ℕ) : (a * b)^n = a^n * b^n :=
by induction n with n ih; [exact (mul_one _).symm,
simp only [pow_succ, ih, mul_assoc, mul_left_comm]]
theorem add_monoid.smul_add : ∀ (a b : A) (n : ℕ), n•(a + b) = n•a + n•b :=
@mul_pow (multiplicative A) _
instance pow.is_monoid_hom (n : ℕ) : is_monoid_hom ((^ n) : M → M) :=
{ map_mul := λ _ _, mul_pow _ _ _, map_one := one_pow _ }
instance add_monoid.smul.is_add_monoid_hom (n : ℕ) : is_add_monoid_hom (add_monoid.smul n : A → A) :=
{ map_add := λ _ _, add_monoid.smul_add _ _ _, map_zero := add_monoid.smul_zero _ }
end comm_monoid
section group
variables [group G] [group H] [add_group A] [add_group B]
section nat
@[simp] theorem inv_pow (a : G) (n : ℕ) : (a⁻¹)^n = (a^n)⁻¹ :=
by induction n with n ih; [exact one_inv.symm,
rw [pow_succ', pow_succ, ih, mul_inv_rev]]
@[simp] theorem add_monoid.neg_smul : ∀ (a : A) (n : ℕ), n•(-a) = -(n•a) :=
@inv_pow (multiplicative A) _
theorem pow_sub (a : G) {m n : ℕ} (h : n ≤ m) : a^(m - n) = a^m * (a^n)⁻¹ :=
have h1 : m - n + n = m, from nat.sub_add_cancel h,
have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1],
eq_mul_inv_of_mul_eq h2
theorem add_monoid.smul_sub : ∀ (a : A) {m n : ℕ}, n ≤ m → (m - n)•a = m•a - n•a :=
@pow_sub (multiplicative A) _
theorem pow_inv_comm (a : G) (m n : ℕ) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m :=
by rw inv_pow; exact inv_comm_of_comm (pow_mul_comm _ _ _)
theorem add_monoid.smul_neg_comm : ∀ (a : A) (m n : ℕ), m•(-a) + n•a = n•a + m•(-a) :=
@pow_inv_comm (multiplicative A) _
end nat
open int
/--
The power operation in a group. This extends `monoid.pow` to negative integers
with the definition `a^(-n) = (a^n)⁻¹`.
-/
def gpow (a : G) : ℤ → G
| (of_nat n) := a^n
| -[1+n] := (a^(nat.succ n))⁻¹
def gsmul (n : ℤ) (a : A) : A :=
@gpow (multiplicative A) _ a n
@[priority 10] instance group.has_pow : has_pow G ℤ := ⟨gpow⟩
localized "infix ` • `:70 := gsmul" in add_group
localized "infix ` •ℕ `:70 := add_monoid.smul" in smul
localized "infix ` •ℤ `:70 := gsmul" in smul
@[simp] theorem gpow_coe_nat (a : G) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl
@[simp] theorem gsmul_coe_nat (a : A) (n : ℕ) : (n:ℤ) • a = n •ℕ a := rfl
@[simp] theorem gpow_of_nat (a : G) (n : ℕ) : a ^ of_nat n = a ^ n := rfl
@[simp] theorem gsmul_of_nat (a : A) (n : ℕ) : of_nat n • a = n •ℕ a := rfl
@[simp] theorem gpow_neg_succ (a : G) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl
@[simp] theorem gsmul_neg_succ (a : A) (n : ℕ) : -[1+n] • a = - (n.succ •ℕ a) := rfl
local attribute [ematch] le_of_lt
open nat
@[simp] theorem gpow_zero (a : G) : a ^ (0:ℤ) = 1 := rfl
@[simp] theorem zero_gsmul (a : A) : (0:ℤ) • a = 0 := rfl
@[simp] theorem gpow_one (a : G) : a ^ (1:ℤ) = a := mul_one _
@[simp] theorem one_gsmul (a : A) : (1:ℤ) • a = a := add_zero _
@[simp] theorem one_gpow : ∀ (n : ℤ), (1 : G) ^ n = 1
| (n : ℕ) := one_pow _
| -[1+ n] := show _⁻¹=(1:G), by rw [_root_.one_pow, one_inv]
@[simp] theorem gsmul_zero : ∀ (n : ℤ), n • (0 : A) = 0 :=
@one_gpow (multiplicative A) _
@[simp] theorem gpow_neg (a : G) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹
| (n+1:ℕ) := rfl
| 0 := one_inv.symm
| -[1+ n] := (inv_inv _).symm
@[simp] theorem neg_gsmul : ∀ (a : A) (n : ℤ), -n • a = -(n • a) :=
@gpow_neg (multiplicative A) _
theorem gpow_neg_one (x : G) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x
theorem neg_one_gsmul (x : A) : (-1:ℤ) • x = -x := congr_arg has_neg.neg $ add_monoid.one_smul x
theorem gsmul_one [has_one A] (n : ℤ) : n • (1 : A) = n :=
begin
cases n,
{ rw [gsmul_of_nat, add_monoid.smul_one, int.cast_of_nat] },
{ rw [gsmul_neg_succ, add_monoid.smul_one, int.cast_neg_succ_of_nat, nat.cast_succ] }
end
theorem inv_gpow (a : G) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹
| (n : ℕ) := inv_pow a n
| -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1)
theorem gsmul_neg (a : A) (n : ℤ) : gsmul n (- a) = - gsmul n a :=
@inv_gpow (multiplicative A) _ a n
private lemma gpow_add_aux (a : G) (m n : nat) :
a ^ ((of_nat m) + -[1+n]) = a ^ of_nat m * a ^ -[1+n] :=
or.elim (nat.lt_or_ge m (nat.succ n))
(assume h1 : m < succ n,
have h2 : m ≤ n, from le_of_lt_succ h1,
suffices a ^ -[1+ n-m] = a ^ of_nat m * a ^ -[1+n],
by rwa [of_nat_add_neg_succ_of_nat_of_lt h1],
show (a ^ nat.succ (n - m))⁻¹ = a ^ of_nat m * a ^ -[1+n],
by rw [← succ_sub h2, pow_sub _ (le_of_lt h1), mul_inv_rev, inv_inv]; refl)
(assume : m ≥ succ n,
suffices a ^ (of_nat (m - succ n)) = (a ^ (of_nat m)) * (a ^ -[1+ n]),
by rw [of_nat_add_neg_succ_of_nat_of_ge]; assumption,
suffices a ^ (m - succ n) = a ^ m * (a ^ n.succ)⁻¹, from this,
by rw pow_sub; assumption)
theorem gpow_add (a : G) : ∀ (i j : ℤ), a ^ (i + j) = a ^ i * a ^ j
| (of_nat m) (of_nat n) := pow_add _ _ _
| (of_nat m) -[1+n] := gpow_add_aux _ _ _
| -[1+m] (of_nat n) := by rw [add_comm, gpow_add_aux,
gpow_neg_succ, gpow_of_nat, ← inv_pow, ← pow_inv_comm]
| -[1+m] -[1+n] :=
suffices (a ^ (m + succ (succ n)))⁻¹ = (a ^ m.succ)⁻¹ * (a ^ n.succ)⁻¹, from this,
by rw [← succ_add_eq_succ_add, add_comm, _root_.pow_add, mul_inv_rev]
theorem add_gsmul : ∀ (a : A) (i j : ℤ), (i + j) • a = i • a + j • a :=
@gpow_add (multiplicative A) _
theorem gpow_add_one (a : G) (i : ℤ) : a ^ (i + 1) = a ^ i * a :=
by rw [gpow_add, gpow_one]
theorem add_one_gsmul : ∀ (a : A) (i : ℤ), (i + 1) • a = i • a + a :=
@gpow_add_one (multiplicative A) _
theorem gpow_one_add (a : G) (i : ℤ) : a ^ (1 + i) = a * a ^ i :=
by rw [gpow_add, gpow_one]
theorem one_add_gsmul : ∀ (a : A) (i : ℤ), (1 + i) • a = a + i • a :=
@gpow_one_add (multiplicative A) _
theorem gpow_mul_comm (a : G) (i j : ℤ) : a ^ i * a ^ j = a ^ j * a ^ i :=
by rw [← gpow_add, ← gpow_add, add_comm]
theorem gsmul_add_comm : ∀ (a : A) (i j), i • a + j • a = j • a + i • a :=
@gpow_mul_comm (multiplicative A) _
theorem gpow_mul (a : G) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n
| (m : ℕ) (n : ℕ) := pow_mul _ _ _
| (m : ℕ) -[1+ n] := (gpow_neg _ (m * succ n)).trans $
show (a ^ (m * succ n))⁻¹ = _, by rw pow_mul; refl
| -[1+ m] (n : ℕ) := (gpow_neg _ (succ m * n)).trans $
show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow]; refl
| -[1+ m] -[1+ n] := (pow_mul a (succ m) (succ n)).trans $
show _ = (_⁻¹^_)⁻¹, by rw [inv_pow, inv_inv]
theorem gsmul_mul' : ∀ (a : A) (m n : ℤ), m * n • a = n • (m • a) :=
@gpow_mul (multiplicative A) _
theorem gpow_mul' (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m :=
by rw [mul_comm, gpow_mul]
theorem gsmul_mul (a : A) (m n : ℤ) : m * n • a = m • (n • a) :=
by rw [mul_comm, gsmul_mul']
theorem gpow_bit0 (a : G) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _
theorem bit0_gsmul (a : A) (n : ℤ) : bit0 n • a = n • a + n • a := gpow_add _ _ _
theorem gpow_bit1 (a : G) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a :=
by rw [bit1, gpow_add]; simp [gpow_bit0]
theorem bit1_gsmul : ∀ (a : A) (n : ℤ), bit1 n • a = n • a + n • a + a :=
@gpow_bit1 (multiplicative A) _
theorem monoid_hom.map_gpow (f : G →* H) (a : G) (n : ℤ) : f (a ^ n) = f a ^ n :=
by cases n; [exact f.map_pow _ _, exact (f.map_inv _).trans (congr_arg _ $ f.map_pow _ _)]
theorem add_monoid_hom.map_gsmul (f : A →+ B) (a : A) (n : ℤ) : f (n • a) = n • f a :=
f.to_multiplicative.map_gpow a n
end group
open_locale smul
section comm_group
variables [comm_group G] [add_comm_group A]
theorem mul_gpow (a b : G) : ∀ n:ℤ, (a * b)^n = a^n * b^n
| (n : ℕ) := mul_pow a b n
| -[1+ n] := show _⁻¹=_⁻¹*_⁻¹, by rw [mul_pow, mul_inv_rev, mul_comm]
theorem gsmul_add : ∀ (a b : A) (n : ℤ), n •ℤ (a + b) = n •ℤ a + n •ℤ b :=
@mul_gpow (multiplicative A) _
theorem gsmul_sub (a b : A) (n : ℤ) : gsmul n (a - b) = gsmul n a - gsmul n b :=
by simp only [gsmul_add, gsmul_neg, sub_eq_add_neg]
instance gpow.is_group_hom (n : ℤ) : is_group_hom ((^ n) : G → G) :=
{ map_mul := λ _ _, mul_gpow _ _ n }
instance gsmul.is_add_group_hom (n : ℤ) : is_add_group_hom (gsmul n : A → A) :=
{ map_add := λ _ _, gsmul_add _ _ n }
end comm_group
@[simp] lemma with_bot.coe_smul [add_monoid A] (a : A) (n : ℕ) :
((add_monoid.smul n a : A) : with_bot A) = add_monoid.smul n a :=
add_monoid_hom.map_smul ⟨_, with_bot.coe_zero, with_bot.coe_add⟩ a n
theorem add_monoid.smul_eq_mul' [semiring R] (a : R) (n : ℕ) : n • a = a * n :=
by induction n with n ih; [rw [add_monoid.zero_smul, nat.cast_zero, mul_zero],
rw [succ_smul', ih, nat.cast_succ, mul_add, mul_one]]
theorem add_monoid.smul_eq_mul [semiring R] (n : ℕ) (a : R) : n • a = n * a :=
by rw [add_monoid.smul_eq_mul', nat.mul_cast_comm]
theorem add_monoid.mul_smul_left [semiring R] (a b : R) (n : ℕ) : n • (a * b) = a * (n • b) :=
by rw [add_monoid.smul_eq_mul', add_monoid.smul_eq_mul', mul_assoc]
theorem add_monoid.mul_smul_assoc [semiring R] (a b : R) (n : ℕ) : n • (a * b) = n • a * b :=
by rw [add_monoid.smul_eq_mul, add_monoid.smul_eq_mul, mul_assoc]
lemma zero_pow [semiring R] : ∀ {n : ℕ}, 0 < n → (0 : R) ^ n = 0
| (n+1) _ := zero_mul _
@[simp, move_cast] theorem nat.cast_pow [semiring R] (n m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m :=
by induction m with m ih; [exact nat.cast_one, rw [nat.pow_succ, pow_succ', nat.cast_mul, ih]]
@[simp, move_cast] theorem int.coe_nat_pow (n m : ℕ) : ((n ^ m : ℕ) : ℤ) = n ^ m :=
by induction m with m ih; [exact int.coe_nat_one, rw [nat.pow_succ, pow_succ', int.coe_nat_mul, ih]]
theorem int.nat_abs_pow (n : ℤ) (k : ℕ) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k :=
by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, nat.pow_succ, ih]]
@[simp] lemma ring_hom.map_pow [semiring R] [semiring S] (f : R →+* S) (a) :
∀ n : ℕ, f (a ^ n) = (f a) ^ n :=
f.to_monoid_hom.map_pow a
lemma is_semiring_hom.map_pow [semiring R] [semiring S] (f : R → S) [is_semiring_hom f] (a) :
∀ n : ℕ, f (a ^ n) = (f a) ^ n :=
is_monoid_hom.map_pow f a
theorem neg_one_pow_eq_or [ring R] : ∀ n : ℕ, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1
| 0 := or.inl rfl
| (n+1) := (neg_one_pow_eq_or n).swap.imp
(λ h, by rw [pow_succ, h, neg_one_mul, neg_neg])
(λ h, by rw [pow_succ, h, mul_one])
lemma pow_dvd_pow [comm_semiring R] (a : R) {m n : ℕ} (h : m ≤ n) :
a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_sub_cancel' h]⟩
theorem gsmul_eq_mul [ring R] (a : R) : ∀ n, n •ℤ a = n * a
| (n : ℕ) := add_monoid.smul_eq_mul _ _
| -[1+ n] := show -(_•_)=-_*_, by rw [neg_mul_eq_neg_mul_symm, add_monoid.smul_eq_mul, nat.cast_succ]
theorem gsmul_eq_mul' [ring R] (a : R) (n : ℤ) : n •ℤ a = a * n :=
by rw [gsmul_eq_mul, int.mul_cast_comm]
theorem mul_gsmul_left [ring R] (a b : R) (n : ℤ) : n •ℤ (a * b) = a * (n •ℤ b) :=
by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc]
theorem mul_gsmul_assoc [ring R] (a b : R) (n : ℤ) : n •ℤ (a * b) = n •ℤ a * b :=
by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc]
@[simp, move_cast] theorem int.cast_pow [ring R] (n : ℤ) (m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m :=
by induction m with m ih; [exact int.cast_one,
rw [pow_succ, pow_succ, int.cast_mul, ih]]
lemma neg_one_pow_eq_pow_mod_two [ring R] {n : ℕ} : (-1 : R) ^ n = -1 ^ (n % 2) :=
by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two]
theorem sq_sub_sq [comm_ring R] (a b : R) : a ^ 2 - b ^ 2 = (a + b) * (a - b) :=
by rw [pow_two, pow_two, mul_self_sub_mul_self]
theorem pow_eq_zero [domain R] {x : R} {n : ℕ} (H : x^n = 0) : x = 0 :=
begin
induction n with n ih,
{ rw pow_zero at H,
rw [← mul_one x, H, mul_zero] },
exact or.cases_on (mul_eq_zero.1 H) id ih
end
@[field_simps] theorem pow_ne_zero [domain R] {a : R} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 :=
mt pow_eq_zero h
@[simp] theorem one_div_pow [division_ring R] {a : R} (ha : a ≠ 0) (n : ℕ) : (1 / a) ^ n = 1 / a ^ n :=
by induction n with n ih; [exact (div_one _).symm,
rw [pow_succ', ih, division_ring.one_div_mul_one_div (pow_ne_zero _ ha) ha]]; refl
@[simp] theorem division_ring.inv_pow [division_ring R] {a : R} (ha : a ≠ 0) (n : ℕ) : a⁻¹ ^ n = (a ^ n)⁻¹ :=
by simpa only [inv_eq_one_div] using one_div_pow ha n
@[simp] theorem div_pow [field R] (a : R) {b : R} (hb : b ≠ 0) (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n :=
by rw [div_eq_mul_one_div, mul_pow, one_div_pow hb, ← div_eq_mul_one_div]
theorem add_monoid.smul_nonneg [ordered_comm_monoid R] {a : R} (H : 0 ≤ a) : ∀ n : ℕ, 0 ≤ n • a
| 0 := le_refl _
| (n+1) := add_nonneg' H (add_monoid.smul_nonneg n)
lemma pow_abs [decidable_linear_ordered_comm_ring R] (a : R) (n : ℕ) : (abs a)^n = abs (a^n) :=
by induction n with n ih; [exact (abs_one).symm,
rw [pow_succ, pow_succ, ih, abs_mul]]
lemma abs_neg_one_pow [decidable_linear_ordered_comm_ring R] (n : ℕ) : abs ((-1 : R)^n) = 1 :=
by rw [←pow_abs, abs_neg, abs_one, one_pow]
@[field_simps] lemma inv_pow' [discrete_field R] (a : R) (n : ℕ) : a⁻¹ ^ n = (a ^ n)⁻¹ :=
by induction n; simp [*, pow_succ, mul_inv', mul_comm]
@[field_simps] lemma pow_div [discrete_field R] (a b : R) (n : ℕ) : (a / b)^n = a^n / b^n :=
by simp [div_eq_mul_inv, mul_pow, inv_pow']
lemma pow_inv [division_ring R] (a : R) : ∀ n : ℕ, a ≠ 0 → (a^n)⁻¹ = (a⁻¹)^n
| 0 ha := inv_one
| (n+1) ha := by rw [pow_succ, pow_succ', mul_inv_eq (pow_ne_zero _ ha) ha, pow_inv _ ha]
namespace add_monoid
variable [ordered_comm_monoid A]
theorem smul_le_smul {a : A} {n m : ℕ} (ha : 0 ≤ a) (h : n ≤ m) : n • a ≤ m • a :=
let ⟨k, hk⟩ := nat.le.dest h in
calc n • a = n • a + 0 : (add_zero _).symm
... ≤ n • a + k • a : add_le_add_left' (smul_nonneg ha _)
... = m • a : by rw [← hk, add_smul]
lemma smul_le_smul_of_le_right {a b : A} (hab : a ≤ b) : ∀ i : ℕ, i • a ≤ i • b
| 0 := by simp
| (k+1) := add_le_add' hab (smul_le_smul_of_le_right _)
end add_monoid
namespace canonically_ordered_semiring
variable [canonically_ordered_comm_semiring R]
theorem pow_pos {a : R} (H : 0 < a) : ∀ n : ℕ, 0 < a ^ n
| 0 := canonically_ordered_semiring.zero_lt_one
| (n+1) := canonically_ordered_semiring.mul_pos.2 ⟨H, pow_pos n⟩
lemma pow_le_pow_of_le_left {a b : R} (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i
| 0 := by simp
| (k+1) := canonically_ordered_semiring.mul_le_mul hab (pow_le_pow_of_le_left k)
theorem one_le_pow_of_one_le {a : R} (H : 1 ≤ a) (n : ℕ) : 1 ≤ a ^ n :=
by simpa only [one_pow] using pow_le_pow_of_le_left H n
theorem pow_le_one {a : R} (H : a ≤ 1) (n : ℕ) : a ^ n ≤ 1:=
by simpa only [one_pow] using pow_le_pow_of_le_left H n
end canonically_ordered_semiring
section linear_ordered_semiring
variable [linear_ordered_semiring R]
theorem pow_pos {a : R} (H : 0 < a) : ∀ (n : ℕ), 0 < a ^ n
| 0 := zero_lt_one
| (n+1) := mul_pos H (pow_pos _)
theorem pow_nonneg {a : R} (H : 0 ≤ a) : ∀ (n : ℕ), 0 ≤ a ^ n
| 0 := zero_le_one
| (n+1) := mul_nonneg H (pow_nonneg _)
theorem pow_lt_pow_of_lt_left {x y : R} {n : ℕ} (Hxy : x < y) (Hxpos : 0 ≤ x) (Hnpos : 0 < n) :
x ^ n < y ^ n :=
begin
cases lt_or_eq_of_le Hxpos,
{ rw ←nat.sub_add_cancel Hnpos,
induction (n - 1), { simpa only [pow_one] },
rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one],
apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) },
{ rw [←h, zero_pow Hnpos], apply pow_pos (by rwa ←h at Hxy : 0 < y),}
end
theorem pow_right_inj {x y : R} {n : ℕ} (Hxpos : 0 ≤ x) (Hypos : 0 ≤ y) (Hnpos : 0 < n)
(Hxyn : x ^ n = y ^ n) : x = y :=
begin
rcases lt_trichotomy x y with hxy | rfl | hyx,
{ exact absurd Hxyn (ne_of_lt (pow_lt_pow_of_lt_left hxy Hxpos Hnpos)) },
{ refl },
{ exact absurd Hxyn (ne_of_gt (pow_lt_pow_of_lt_left hyx Hypos Hnpos)) },
end
theorem one_le_pow_of_one_le {a : R} (H : 1 ≤ a) : ∀ (n : ℕ), 1 ≤ a ^ n
| 0 := le_refl _
| (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n)
zero_le_one (le_trans zero_le_one H)
/-- Bernoulli's inequality. This version works for semirings but requires
an additional hypothesis `0 ≤ a * a`. -/
theorem one_add_mul_le_pow' {a : R} (Hsqr : 0 ≤ a * a) (H : 0 ≤ 1 + a) :
∀ (n : ℕ), 1 + n • a ≤ (1 + a) ^ n
| 0 := le_of_eq $ add_zero _
| (n+1) :=
calc 1 + (n + 1) • a ≤ (1 + a) * (1 + n • a) :
by simpa [succ_smul, mul_add, add_mul, add_monoid.mul_smul_left]
using add_monoid.smul_nonneg Hsqr n
... ≤ (1 + a)^(n+1) : mul_le_mul_of_nonneg_left (one_add_mul_le_pow' n) H
theorem pow_le_pow {a : R} {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m :=
let ⟨k, hk⟩ := nat.le.dest h in
calc a ^ n = a ^ n * 1 : (mul_one _).symm
... ≤ a ^ n * a ^ k : mul_le_mul_of_nonneg_left
(one_le_pow_of_one_le ha _)
(pow_nonneg (le_trans zero_le_one ha) _)
... = a ^ m : by rw [←hk, pow_add]
lemma pow_lt_pow {a : R} {n m : ℕ} (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m :=
begin
have h' : 1 ≤ a := le_of_lt h,
have h'' : 0 < a := lt_trans zero_lt_one h,
cases m, cases h2, rw [pow_succ, ←one_mul (a ^ n)],
exact mul_lt_mul h (pow_le_pow h' (nat.le_of_lt_succ h2)) (pow_pos h'' _) (le_of_lt h'')
end
lemma pow_le_pow_of_le_left {a b : R} (ha : 0 ≤ a) (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i
| 0 := by simp
| (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab)
lemma lt_of_pow_lt_pow {a b : R} (n : ℕ) (hb : 0 ≤ b) (h : a ^ n < b ^ n) : a < b :=
lt_of_not_ge $ λ hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h
private lemma pow_lt_pow_of_lt_one_aux {a : R} (h : 0 < a) (ha : a < 1) (i : ℕ) :
∀ k : ℕ, a ^ (i + k + 1) < a ^ i
| 0 :=
begin
simp only [add_zero],
rw ←one_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one
end
| (k+1) :=
begin
rw ←one_mul (a^i),
apply mul_lt_mul ha _ _ zero_le_one,
{ apply le_of_lt, apply pow_lt_pow_of_lt_one_aux },
{ show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h }
end
private lemma pow_le_pow_of_le_one_aux {a : R} (h : 0 ≤ a) (ha : a ≤ 1) (i : ℕ) :
∀ k : ℕ, a ^ (i + k) ≤ a ^ i
| 0 := by simp
| (k+1) := by rw [←add_assoc, ←one_mul (a^i)];
exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one
lemma pow_lt_pow_of_lt_one {a : R} (h : 0 < a) (ha : a < 1)
{i j : ℕ} (hij : i < j) : a ^ j < a ^ i :=
let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in
by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _
lemma pow_le_pow_of_le_one {a : R} (h : 0 ≤ a) (ha : a ≤ 1)
{i j : ℕ} (hij : i ≤ j) : a ^ j ≤ a ^ i :=
let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in
by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _
lemma pow_le_one {x : R} : ∀ (n : ℕ) (h0 : 0 ≤ x) (h1 : x ≤ 1), x ^ n ≤ 1
| 0 h0 h1 := le_refl (1 : R)
| (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1)
end linear_ordered_semiring
theorem pow_two_nonneg [linear_ordered_ring R] (a : R) : 0 ≤ a ^ 2 :=
by { rw pow_two, exact mul_self_nonneg _ }
/-- Bernoulli's inequality for `n : ℕ`, `-2 ≤ a`. -/
theorem one_add_mul_le_pow [linear_ordered_ring R] {a : R} (H : -2 ≤ a) :
∀ (n : ℕ), 1 + n • a ≤ (1 + a) ^ n
| 0 := le_of_eq $ add_zero _
| 1 := by simp
| (n+2) :=
have H' : 0 ≤ 2 + a,
from neg_le_iff_add_nonneg.1 H,
have 0 ≤ n • (a * a * (2 + a)) + a * a,
from add_nonneg (add_monoid.smul_nonneg (mul_nonneg (mul_self_nonneg a) H') n)
(mul_self_nonneg a),
calc 1 + (n + 2) • a ≤ 1 + (n + 2) • a + (n • (a * a * (2 + a)) + a * a) :
(le_add_iff_nonneg_right _).2 this
... = (1 + a) * (1 + a) * (1 + n • a) :
by { simp only [add_mul, mul_add, mul_two, mul_one, one_mul, succ_smul, add_monoid.smul_add,
add_monoid.mul_smul_assoc, (add_monoid.mul_smul_left _ _ _).symm],
ac_refl }
... ≤ (1 + a) * (1 + a) * (1 + a)^n :
mul_le_mul_of_nonneg_left (one_add_mul_le_pow n) (mul_self_nonneg (1 + a))
... = (1 + a)^(n + 2) : by simp only [pow_succ, mul_assoc]
/-- Bernoulli's inequality reformulated to estimate `a^n`. -/
theorem one_add_sub_mul_le_pow [linear_ordered_ring R]
{a : R} (H : -1 ≤ a) (n : ℕ) : 1 + n • (a - 1) ≤ a ^ n :=
have -2 ≤ a - 1, by { rw [bit0, neg_add], exact sub_le_sub_right H 1 },
by simpa only [add_sub_cancel'_right] using one_add_mul_le_pow this n
namespace int
lemma units_pow_two (u : units ℤ) : u ^ 2 = 1 :=
(units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl)
lemma units_pow_eq_pow_mod_two (u : units ℤ) (n : ℕ) : u ^ n = u ^ (n % 2) :=
by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one]
end int
@[simp] lemma neg_square {α} [ring α] (z : α) : (-z)^2 = z^2 :=
by simp [pow, monoid.pow]
lemma div_sq_cancel {α} [field α] {a : α} (ha : a ≠ 0) (b : α) : a^2 * b / a = a * b :=
by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
|
ef3178a33975a39befd0fc69337781a377732c3c | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/list_notation.lean | 6917236d74f02299a1a65c262b4797c9a4b44a1c | [
"Apache-2.0"
] | permissive | leanprover-community/lean | 12b87f69d92e614daea8bcc9d4de9a9ace089d0e | cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0 | refs/heads/master | 1,687,508,156,644 | 1,684,951,104,000 | 1,684,951,104,000 | 169,960,991 | 457 | 107 | Apache-2.0 | 1,686,744,372,000 | 1,549,790,268,000 | C++ | UTF-8 | Lean | false | false | 383 | lean | open nat
#eval [1, 2, 3]
#eval to_bool $ 1 ∈ [1, 2, 3]
#eval to_bool $ 4 ∈ [1, 2, 3]
#eval [1, 2, 3] ++ [3, 4]
#eval 2 :: [3, 4]
#eval ([] : list nat)
#eval (∅ : list nat)
#eval ({1, 3, 2, 2, 3, 1} : list nat)
#eval [1, 2, 3] ∪ [3, 4, 1, 5]
#eval [1, 2, 3] ∩ [3, 4, 1, 5]
#eval (*10) <$> [1, 2, 3]
#check ({1, 2, 3} : list nat)
#check ({1, 2, 3, 4} : set nat)
|
9e4a9d4ed3cdbab039516454e17a1f7250cb9629 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /archive/sensitivity.lean | 60a6b5d53ec07113edc1329b32b37f7e1000f102 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 16,693 | lean | /-
Copyright (c) 2019 Reid Barton, Johan Commelin, Jesse Han, Chris Hughes, Robert Y. Lewis, and Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton, Johan Commelin, Jesse Han, Chris Hughes, Robert Y. Lewis, and Patrick Massot
-/
import tactic.fin_cases
import tactic.apply_fun
import linear_algebra.finite_dimensional
import linear_algebra.dual
import analysis.normed_space.basic
/-!
A formalization of Hao Huang's sensitivity theorem: in the hypercube of
dimension n ≥ 1, if one colors more than half the vertices then at least one
vertex has at least √n colored neighbors.
A fun summer collaboration by
Reid Barton, Johan Commelin, Jesse Han, Chris Hughes, Robert Y. Lewis, and Patrick Massot,
based on Don Knuth's account of the story
(https://www.cs.stanford.edu/~knuth/papers/huang.pdf),
using the Lean theorem prover (http://leanprover.github.io/),
by Leonardo de Moura at Microsoft Research, and his collaborators
(https://leanprover.github.io/people/),
and using Lean's user maintained mathematics library
(https://github.com/leanprover-community/mathlib).
The project was developed at https://github.com/leanprover-community/lean-sensitivity
and is now archived at https://github.com/leanprover-community/mathlib/blob/master/archive/sensitivity.lean
-/
-- The next two lines assert we do not want to give a constructive proof,
-- but rather use classical logic.
noncomputable theory
open_locale classical
notation `|`x`|` := abs x
notation `√` := real.sqrt
open function bool linear_map fintype finite_dimensional dual_pair
/- -------------------------------------------------------------------------\
| The hypercube. |
\---------------------------------------------------------------------------/
/-
Notations:
ℕ denotes natural numbers (including zero).
fin n = {0, ⋯ , n - 1}.
bool = {tt, ff}.
-/
/-- The hypercube in dimension n. -/
@[derive [inhabited, fintype]] def Q (n : ℕ) := fin n → bool
/-- The projection from Q (n + 1) to Q n forgetting the first value
(ie. the image of zero). -/
def π {n : ℕ} : Q (n + 1) → Q n := λ p, p ∘ fin.succ
namespace Q
-- n will always denote a natural number.
variable (n : ℕ)
/-- Q 0 has a unique element. -/
instance : unique (Q 0) :=
⟨⟨λ _, tt⟩, by { intro, ext x, fin_cases x }⟩
/-- Q n has 2^n elements. -/
lemma card : card (Q n) = 2^n :=
by simp [Q]
-- Until the end of this namespace, n will be an implicit argument (still
-- a natural number).
variable {n}
lemma succ_n_eq (p q : Q (n+1)) : p = q ↔ (p 0 = q 0 ∧ π p = π q) :=
begin
split,
{ rintro rfl, exact ⟨rfl, rfl⟩, },
{ rintros ⟨h₀, h⟩,
ext x,
by_cases hx : x = 0,
{ rwa hx },
{ rw ← fin.succ_pred x hx,
convert congr_fun h (fin.pred x hx) } }
end
/-- The adjacency relation defining the graph structure on Q n:
p.adjacent q if there is an edge from p to q in Q n -/
def adjacent {n : ℕ} (p : Q n) : set (Q n) := λ q, ∃! i, p i ≠ q i
/-- In Q 0, no two vertices are adjacent. -/
lemma not_adjacent_zero (p q : Q 0) : ¬ p.adjacent q :=
by rintros ⟨v, _⟩; apply fin_zero_elim v
/-- If p and q in Q (n+1) have different values at zero then they are adjacent
iff their projections to Q n are equal. -/
lemma adj_iff_proj_eq {p q : Q (n+1)} (h₀ : p 0 ≠ q 0) :
p.adjacent q ↔ π p = π q :=
begin
split,
{ rintros ⟨i, h_eq, h_uni⟩,
ext x, by_contradiction hx,
apply fin.succ_ne_zero x,
rw [h_uni _ hx, h_uni _ h₀] },
{ intro heq,
use [0, h₀],
intros y hy,
contrapose! hy,
rw ←fin.succ_pred _ hy,
apply congr_fun heq }
end
/-- If p and q in Q (n+1) have the same value at zero then they are adjacent
iff their projections to Q n are adjacent. -/
lemma adj_iff_proj_adj {p q : Q (n+1)} (h₀ : p 0 = q 0) :
p.adjacent q ↔ (π p).adjacent (π q) :=
begin
split,
{ rintros ⟨i, h_eq, h_uni⟩,
have h_i : i ≠ 0, from λ h_i, absurd h₀ (by rwa h_i at h_eq),
use [i.pred h_i,
show p (fin.succ (fin.pred i _)) ≠ q (fin.succ (fin.pred i _)),
by rwa fin.succ_pred],
intros y hy,
simp [eq.symm (h_uni _ hy)] },
{ rintros ⟨i, h_eq, h_uni⟩,
use [i.succ, h_eq],
intros y hy,
rw [←fin.pred_inj, fin.pred_succ],
{ apply h_uni,
change p (fin.pred _ _).succ ≠ q (fin.pred _ _).succ,
simp [hy] },
{ contrapose! hy,
rw [hy, h₀] },
{ apply fin.succ_ne_zero } }
end
@[symm] lemma adjacent.symm {p q : Q n} : p.adjacent q ↔ q.adjacent p :=
by simp only [adjacent, ne_comm]
end Q
/- -------------------------------------------------------------------------\
| The vector space. |
\---------------------------------------------------------------------------/
/-- The free vector space on vertices of a hypercube, defined inductively. -/
def V : ℕ → Type
| 0 := ℝ
| (n+1) := V n × V n
namespace V
variables (n : ℕ)
-- V n is a real vector space whose equality relation is computable.
instance : decidable_eq (V n) :=
by { induction n ; { dunfold V, resetI, apply_instance } }
instance : add_comm_group (V n) :=
by { induction n ; { dunfold V, resetI, apply_instance } }
instance : vector_space ℝ (V n) :=
by { induction n ; { dunfold V, resetI, apply_instance } }
-- The next five definitions are short circuits helping Lean to quickly find
-- relevant structures on V n
def module : module ℝ (V n) := by apply_instance
def add_comm_semigroup : add_comm_semigroup (V n) := by apply_instance
def add_comm_monoid : add_comm_monoid (V n) := by apply_instance
def has_scalar : has_scalar ℝ (V n) := by apply_instance
def has_add : has_add (V n) := by apply_instance
end V
local attribute [instance, priority 100000]
V.module V.add_comm_semigroup V.add_comm_monoid V.has_scalar V.has_add
/-- The basis of V indexed by the hypercube, defined inductively. -/
noncomputable def e : Π {n}, Q n → V n
| 0 := λ _, (1:ℝ)
| (n+1) := λ x, cond (x 0) (e (π x), 0) (0, e (π x))
@[simp] lemma e_zero_apply (x : Q 0) : e x = (1 : ℝ) := rfl
/-- The dual basis to e, defined inductively. -/
noncomputable def ε : Π {n : ℕ} (p : Q n), V n →ₗ[ℝ] ℝ
| 0 _ := linear_map.id
| (n+1) p := cond (p 0) ((ε $ π p).comp $ linear_map.fst _ _ _) ((ε $ π p).comp $ linear_map.snd _ _ _)
variable {n : ℕ}
lemma duality (p q : Q n) : ε p (e q) = if p = q then 1 else 0 :=
begin
induction n with n IH,
{ rw (show p = q, from subsingleton.elim p q),
dsimp [ε, e],
simp },
{ dsimp [ε, e],
cases hp : p 0 ; cases hq : q 0,
all_goals {
repeat {rw cond_tt},
repeat {rw cond_ff},
simp only [linear_map.fst_apply, linear_map.snd_apply, linear_map.comp_apply, IH],
try { congr' 1, rw Q.succ_n_eq, finish },
try {
erw (ε _).map_zero,
have : p ≠ q, { intro h, rw p.succ_n_eq q at h, finish },
simp [this] } } }
end
/-- Any vector in V n annihilated by all ε p's is zero. -/
lemma epsilon_total {v : V n} (h : ∀ p : Q n, (ε p) v = 0) : v = 0 :=
begin
induction n with n ih,
{ dsimp [ε] at h, exact h (λ _, tt) },
{ cases v with v₁ v₂,
ext ; change _ = (0 : V n) ; simp only [] ; apply ih ; intro p ;
[ let q : Q (n+1) := λ i, if h : i = 0 then tt else p (i.pred h),
let q : Q (n+1) := λ i, if h : i = 0 then ff else p (i.pred h)],
all_goals {
specialize h q,
rw [ε, show q 0 = tt, from rfl, cond_tt] at h <|> rw [ε, show q 0 = ff, from rfl, cond_ff] at h,
rwa show p = π q, by { ext, simp [q, fin.succ_ne_zero, π] } } }
end
/-- e and ε are dual families of vectors. It implies that e is indeed a basis
and ε computes coefficients of decompositions of vectors on that basis. -/
def dual_pair_e_ε (n : ℕ) : dual_pair (@e n) (@ε n) :=
{ eval := duality,
total := @epsilon_total _ }
-- We will now derive the dimension of V, first as a cardinal in dim_V and,
-- since this cardinal is finite, as a natural number in findim_V
lemma dim_V : vector_space.dim ℝ (V n) = 2^n :=
have vector_space.dim ℝ (V n) = ↑(2^n : ℕ),
by { rw [dim_eq_card_basis (dual_pair_e_ε _).is_basis, Q.card]; apply_instance },
by assumption_mod_cast
instance : finite_dimensional ℝ (V n) :=
finite_dimensional.of_finite_basis (dual_pair_e_ε _).is_basis
lemma findim_V : findim ℝ (V n) = 2^n :=
have _ := @dim_V n,
by rw ←findim_eq_dim at this; assumption_mod_cast
/- -------------------------------------------------------------------------\
| The linear map. |
\---------------------------------------------------------------------------/
/-- The linear operator f_n corresponding to Huang's matrix A_n,
defined inductively as a ℝ-linear map from V n to V n. -/
noncomputable def f : Π n, V n →ₗ[ℝ] V n
| 0 := 0
| (n+1) := linear_map.pair
(linear_map.copair (f n) linear_map.id)
(linear_map.copair linear_map.id (-f n))
-- The preceding definition use linear map constructions to automatically
-- get that f is linear, but its values are somewhat buried as a side-effect.
-- The next two lemmas unbury them.
@[simp] lemma f_zero : f 0 = 0 := rfl
lemma f_succ_apply (v : V (n+1)) :
(f (n+1) : V (n+1) → V (n+1)) v = (f n v.1 + v.2, v.1 - f n v.2) :=
begin
cases v,
rw f,
simp only [linear_map.id_apply, linear_map.pair_apply, prod.mk.inj_iff,
linear_map.neg_apply, sub_eq_add_neg, linear_map.copair_apply],
exact ⟨rfl, rfl⟩
end
-- In the next statement, the explicit conversion (n : ℝ) of n to a real number
-- is necessary since otherwise `n • v` refers to the multiplication defined
-- using only the addition of V.
lemma f_squared : ∀ v : V n, (f n) (f n v) = (n : ℝ) • v :=
begin
induction n with n IH; intro,
{ simpa only [nat.cast_zero, zero_smul] },
{ cases v, simp [f_succ_apply, IH, add_smul] }
end
-- We now compute the matrix of f in the e basis (p is the line index,
-- q the column index.)
lemma f_matrix :
∀ p q : Q n, |ε q (f n (e p))| = if q.adjacent p then 1 else 0 :=
begin
induction n with n IH,
{ intros p q,
dsimp [f],
simp [Q.not_adjacent_zero] },
{ intros p q,
have ite_nonneg : ite (π q = π p) (1 : ℝ) 0 ≥ 0,
{ split_ifs ; norm_num },
have f_map_zero := (show linear_map ℝ (V (n+0)) (V n), from f n).map_zero,
dsimp [e, ε, f], cases hp : p 0 ; cases hq : q 0,
all_goals
{ repeat {rw cond_tt}, repeat {rw cond_ff},
simp [f_map_zero, hp, hq, IH, duality, abs_of_nonneg ite_nonneg, Q.adj_iff_proj_eq,
Q.adj_iff_proj_adj],
congr' 1 } }
end
/-- The linear operator g_m corresponding to Knuth's matrix B_m. -/
noncomputable def g (m : ℕ) : V m →ₗ[ℝ] V (m+1) :=
linear_map.pair (f m + √(m+1) • linear_map.id) linear_map.id
-- in the following lemmas, m will denote a natural number
variables {m : ℕ}
-- again we unpack what are the values of g
lemma g_apply : ∀ v, g m v = (f m v + √(m+1) • v, v) :=
by delta g; simp
lemma g_injective : injective (g m) :=
begin
rw g,
intros x₁ x₂ h,
simp only [linear_map.pair_apply, linear_map.id_apply, prod.mk.inj_iff] at h,
exact h.right
end
lemma f_image_g (w : V (m + 1)) (hv : ∃ v, g m v = w) :
(f (m + 1) : _) w = √(m + 1) • w :=
begin
rcases hv with ⟨v, rfl⟩,
have : √(m+1) * √(m+1) = m+1 :=
real.mul_self_sqrt (by exact_mod_cast zero_le _),
simp [-add_comm, this, f_succ_apply, g_apply, f_squared, smul_add, add_smul, smul_smul],
end
/- -------------------------------------------------------------------------\
| The main proof. |
\---------------------------------------------------------------------------/
-- In this section, in order to enforce that n is positive, we write it as
-- m + 1 for some natural number m.
-- dim X will denote the dimension of a subspace X as a cardinal
notation `dim` X:70 := vector_space.dim ℝ ↥X
-- fdim X will denote the (finite) dimension of a subspace X as a natural number
notation `fdim` := findim ℝ
-- Span S will denote the ℝ-subspace spanned by S
notation `Span` := submodule.span ℝ
-- Card X will denote the cardinal of a subset of a finite type, as a
-- natural number.
notation `Card` X:70 := X.to_finset.card
-- In the following, ⊓ and ⊔ will denote intersection and sums of ℝ-subspaces,
-- equipped with their subspace structures. The notations come from the general
-- theory of lattices, with inf and sup (also known as meet and join).
/-- If a subset H of Q (m+1) has cardinal at least 2^m + 1 then the
subspace of V (m+1) spanned by the corresponding basis vectors non-trivially
intersects the range of g m. -/
lemma exists_eigenvalue (H : set (Q (m + 1))) (hH : Card H ≥ 2^m + 1) :
∃ y ∈ Span (e '' H) ⊓ (g m).range, y ≠ (0 : _) :=
begin
let W := Span (e '' H),
let img := (g m).range,
suffices : 0 < dim (W ⊓ img),
{ simp only [exists_prop],
exact_mod_cast exists_mem_ne_zero_of_dim_pos this },
have dim_le : dim (W ⊔ img) ≤ 2^(m + 1),
{ convert ← dim_submodule_le (W ⊔ img),
apply dim_V },
have dim_add : dim (W ⊔ img) + dim (W ⊓ img) = dim W + 2^m,
{ convert ← dim_sup_add_dim_inf_eq W img,
rw ← dim_eq_injective (g m) g_injective,
apply dim_V },
have dimW : dim W = card H,
{ have li : linear_independent ℝ (restrict e H) :=
linear_independent.comp (dual_pair_e_ε _).is_basis.1 _ subtype.val_injective,
have hdW := dim_span li,
rw set.range_restrict at hdW,
convert hdW,
rw [cardinal.mk_image_eq ((dual_pair_e_ε _).is_basis.injective zero_ne_one), cardinal.fintype_card] },
rw ← findim_eq_dim ℝ at ⊢ dim_le dim_add dimW,
rw [← findim_eq_dim ℝ, ← findim_eq_dim ℝ] at dim_add,
norm_cast at ⊢ dim_le dim_add dimW,
rw nat.pow_succ at dim_le,
rw set.to_finset_card at hH,
linarith
end
theorem huang_degree_theorem (H : set (Q (m + 1))) (hH : Card H ≥ 2^m + 1) :
∃ q, q ∈ H ∧ √(m + 1) ≤ Card (H ∩ q.adjacent) :=
begin
rcases exists_eigenvalue H hH with ⟨y, ⟨⟨y_mem_H, y_mem_g⟩, y_ne⟩⟩,
have coeffs_support : ((dual_pair_e_ε (m+1)).coeffs y).support ⊆ H.to_finset,
{ intros p p_in,
rw finsupp.mem_support_iff at p_in,
rw set.mem_to_finset,
exact (dual_pair_e_ε _).mem_of_mem_span y_mem_H p p_in },
obtain ⟨q, H_max⟩ : ∃ q : Q (m+1), ∀ q' : Q (m+1), |(ε q' : _) y| ≤ |ε q y|,
from fintype.exists_max _,
have H_q_pos : 0 < |ε q y|,
{ contrapose! y_ne,
exact epsilon_total (λ p, abs_nonpos_iff.mp (le_trans (H_max p) y_ne)) },
refine ⟨q, (dual_pair_e_ε _).mem_of_mem_span y_mem_H q (abs_pos_iff.mp H_q_pos), _⟩,
let s := √(m+1),
suffices : s * |ε q y| ≤ ↑(_) * |ε q y|,
from (mul_le_mul_right H_q_pos).mp ‹_›,
let coeffs := (dual_pair_e_ε (m+1)).coeffs,
let φ : V (m+1) → V (m+1) := f (m+1),
calc
s * (abs (ε q y))
= abs (ε q (s • y)) : by rw [map_smul, smul_eq_mul, abs_mul, abs_of_nonneg (real.sqrt_nonneg _)]
... = abs (ε q (φ y)) : by rw [← f_image_g y (by simpa using y_mem_g)]
... = abs (ε q (φ (lc _ (coeffs y)))) : by rw (dual_pair_e_ε _).decomposition y
... = abs ((coeffs y).sum (λ (i : Q (m + 1)) (a : ℝ), a • ((ε q) ∘ (f (m + 1)) ∘ λ (i : Q (m + 1)), e i) i)): by
{ dsimp only [φ],
erw [(f $ m+1).map_finsupp_total, (ε q).map_finsupp_total, finsupp.total_apply] ; apply_instance }
... ≤ (coeffs y).support.sum (λ p,
|(coeffs y p) * (ε q $ φ $ e p)| ) : norm_sum_le _ $ λ p, coeffs y p * _
... = (coeffs y).support.sum (λ p, |coeffs y p| * ite (q.adjacent p) 1 0) : by simp only [abs_mul, f_matrix]
... = ((coeffs y).support.filter (Q.adjacent q)).sum (λ p, |coeffs y p| ) : by simp [finset.sum_filter]
... ≤ ((coeffs y).support.filter (Q.adjacent q)).sum (λ p, |coeffs y q| ) : finset.sum_le_sum (λ p _, H_max p)
... = (finset.card ((coeffs y).support.filter (Q.adjacent q)): ℝ) * |coeffs y q| : by rw [← smul_eq_mul, ← finset.sum_const']
... = (finset.card ((coeffs y).support ∩ (Q.adjacent q).to_finset): ℝ) * |coeffs y q| : by {congr, ext, simp, refl}
... ≤ (finset.card ((H ∩ Q.adjacent q).to_finset )) * |ε q y| :
(mul_le_mul_right H_q_pos).mpr (by {
norm_cast,
exact finset.card_le_of_subset (by rw set.to_finset_inter; apply finset.inter_subset_inter_right coeffs_support) })
end
|
10079f507c745293ae00cb9a8573cbd296068bde | 4727251e0cd73359b15b664c3170e5d754078599 | /src/tactic/abel.lean | 55b7519164569109f3226ab76bd6e90318cb1209 | [
"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 | 16,778 | 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 tactic.norm_num
/-!
# The `abel` tactic
Evaluate expressions in the language of additive, commutative monoids and groups.
-/
namespace tactic
namespace abel
/-- The `context` for a call to `abel`.
Stores a few options for this call, and caches some common subexpressions
such as typeclass instances and `0 : α`.
-/
meta structure context :=
(red : transparency)
(α : expr)
(univ : level)
(α0 : expr)
(is_group : bool)
(inst : expr)
/-- Populate a `context` object for evaluating `e`, up to reducibility level `red`. -/
meta def mk_context (red : transparency) (e : expr) : tactic context :=
do α ← infer_type e,
c ← mk_app ``add_comm_monoid [α] >>= mk_instance,
cg ← try_core (mk_app ``add_comm_group [α] >>= mk_instance),
u ← mk_meta_univ,
infer_type α >>= unify (expr.sort (level.succ u)),
u ← get_univ_assignment u,
α0 ← expr.of_nat α 0,
match cg with
| (some cg) := return ⟨red, α, u, α0, tt, cg⟩
| _ := return ⟨red, α, u, α0, ff, c⟩
end
/-- Apply the function `n : ∀ {α} [inst : add_whatever α], _` to the
implicit parameters in the context, and the given list of arguments. -/
meta def context.app (c : context) (n : name) (inst : expr) : list expr → expr :=
(@expr.const tt n [c.univ] c.α inst).mk_app
/-- Apply the function `n : ∀ {α} [inst α], _` to the implicit parameters in the
context, and the given list of arguments.
Compared to `context.app`, this takes the name of the typeclass, rather than an
inferred typeclass instance.
-/
meta def context.mk_app (c : context) (n inst : name) (l : list expr) : tactic expr :=
do m ← mk_instance ((expr.const inst [c.univ] : expr) c.α), return $ c.app n m l
/-- Add the letter "g" to the end of the name, e.g. turning `term` into `termg`.
This is used to choose between declarations taking `add_comm_monoid` and those
taking `add_comm_group` instances.
-/
meta def add_g : name → name
| (name.mk_string s p) := name.mk_string (s ++ "g") p
| n := n
/-- Apply the function `n : ∀ {α} [add_comm_{monoid,group} α]` to the given
list of arguments.
Will use the `add_comm_{monoid,group}` instance that has been cached in the context.
-/
meta def context.iapp (c : context) (n : name) : list expr → expr :=
c.app (if c.is_group then add_g n else n) c.inst
def term {α} [add_comm_monoid α] (n : ℕ) (x a : α) : α := n • x + a
def termg {α} [add_comm_group α] (n : ℤ) (x a : α) : α := n • x + a
/-- Evaluate a term with coefficient `n`, atom `x` and successor terms `a`. -/
meta def context.mk_term (c : context) (n x a : expr) : expr := c.iapp ``term [n, x, a]
/-- Interpret an integer as a coefficient to a term. -/
meta def context.int_to_expr (c : context) (n : ℤ) : tactic expr :=
expr.of_int (if c.is_group then `(ℤ) else `(ℕ)) n
meta inductive normal_expr : Type
| zero (e : expr) : normal_expr
| nterm (e : expr) (n : expr × ℤ) (x : expr) (a : normal_expr) : normal_expr
meta def normal_expr.e : normal_expr → expr
| (normal_expr.zero e) := e
| (normal_expr.nterm e _ _ _) := e
meta instance : has_coe normal_expr expr := ⟨normal_expr.e⟩
meta instance : has_coe_to_fun normal_expr (λ _, expr → expr) := ⟨λ e, ⇑(e : expr)⟩
meta def normal_expr.term' (c : context) (n : expr × ℤ) (x : expr) (a : normal_expr) :
normal_expr :=
normal_expr.nterm (c.mk_term n.1 x a) n x a
meta def normal_expr.zero' (c : context) : normal_expr := normal_expr.zero c.α0
meta def normal_expr.to_list : normal_expr → list (ℤ × expr)
| (normal_expr.zero _) := []
| (normal_expr.nterm _ (_, n) x a) := (n, x) :: a.to_list
open normal_expr
meta def normal_expr.to_string (e : normal_expr) : string :=
" + ".intercalate $ (to_list e).map $
λ ⟨n, e⟩, to_string n ++ " • (" ++ to_string e ++ ")"
meta def normal_expr.pp (e : normal_expr) : tactic format :=
do l ← (to_list e).mmap (λ ⟨n, e⟩, do
pe ← pp e, return (to_fmt n ++ " • (" ++ pe ++ ")")),
return $ format.join $ l.intersperse ↑" + "
meta instance : has_to_tactic_format normal_expr := ⟨normal_expr.pp⟩
meta def normal_expr.refl_conv (e : normal_expr) : tactic (normal_expr × expr) :=
do p ← mk_eq_refl e, return (e, p)
theorem const_add_term {α} [add_comm_monoid α] (k n x a a') (h : k + a = a') :
k + @term α _ n x a = term n x a' := by simp [h.symm, term]; ac_refl
theorem const_add_termg {α} [add_comm_group α] (k n x a a') (h : k + a = a') :
k + @termg α _ n x a = termg n x a' := by simp [h.symm, termg]; ac_refl
theorem term_add_const {α} [add_comm_monoid α] (n x a k a') (h : a + k = a') :
@term α _ n x a + k = term n x a' := by simp [h.symm, term, add_assoc]
theorem term_add_constg {α} [add_comm_group α] (n x a k a') (h : a + k = a') :
@termg α _ n x a + k = termg n x a' := by simp [h.symm, termg, add_assoc]
theorem term_add_term {α} [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' :=
by simp [h₁.symm, h₂.symm, term, add_nsmul]; ac_refl
theorem term_add_termg {α} [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' :=
by simp [h₁.symm, h₂.symm, termg, add_zsmul]; ac_refl
theorem zero_term {α} [add_comm_monoid α] (x a) : @term α _ 0 x a = a :=
by simp [term, zero_nsmul, one_nsmul]
theorem zero_termg {α} [add_comm_group α] (x a) : @termg α _ 0 x a = a :=
by simp [termg]
meta def eval_add (c : context) : normal_expr → normal_expr → tactic (normal_expr × expr)
| (zero _) e₂ := do
p ← mk_app ``zero_add [e₂],
return (e₂, p)
| e₁ (zero _) := do
p ← mk_app ``add_zero [e₁],
return (e₁, p)
| he₁@(nterm e₁ n₁ x₁ a₁) he₂@(nterm e₂ n₂ x₂ a₂) :=
(do
is_def_eq x₁ x₂ c.red,
(n', h₁) ← mk_app ``has_add.add [n₁.1, n₂.1] >>= norm_num.eval_field,
(a', h₂) ← eval_add a₁ a₂,
let k := n₁.2 + n₂.2,
let p₁ := c.iapp ``term_add_term [n₁.1, x₁, a₁, n₂.1, a₂, n', a', h₁, h₂],
if k = 0 then do
p ← mk_eq_trans p₁ (c.iapp ``zero_term [x₁, a']),
return (a', p)
else return (term' c (n', k) x₁ a', p₁))
<|> if expr.lex_lt x₁ x₂ then do
(a', h) ← eval_add a₁ he₂,
return (term' c n₁ x₁ a', c.iapp ``term_add_const [n₁.1, x₁, a₁, e₂, a', h])
else do
(a', h) ← eval_add he₁ a₂,
return (term' c n₂ x₂ a', c.iapp ``const_add_term [e₁, n₂.1, x₂, a₂, a', h])
theorem term_neg {α} [add_comm_group α] (n x a n' a')
(h₁ : -n = n') (h₂ : -a = a') :
-@termg α _ n x a = termg n' x a' :=
by simp [h₂.symm, h₁.symm, termg]; ac_refl
meta def eval_neg (c : context) : normal_expr → tactic (normal_expr × expr)
| (zero e) := do
p ← c.mk_app ``neg_zero ``subtraction_monoid [],
return (zero' c, p)
| (nterm e n x a) := do
(n', h₁) ← mk_app ``has_neg.neg [n.1] >>= norm_num.eval_field,
(a', h₂) ← eval_neg a,
return (term' c (n', -n.2) x a',
c.app ``term_neg c.inst [n.1, x, a, n', a', h₁, h₂])
def smul {α} [add_comm_monoid α] (n : ℕ) (x : α) : α := n • x
def smulg {α} [add_comm_group α] (n : ℤ) (x : α) : α := n • x
theorem zero_smul {α} [add_comm_monoid α] (c) : smul c (0 : α) = 0 :=
by simp [smul, nsmul_zero]
theorem zero_smulg {α} [add_comm_group α] (c) : smulg c (0 : α) = 0 :=
by simp [smulg, zsmul_zero]
theorem term_smul {α} [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' :=
by simp [h₂.symm, h₁.symm, term, smul, nsmul_add, mul_nsmul]
theorem term_smulg {α} [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' :=
by simp [h₂.symm, h₁.symm, termg, smulg, zsmul_add, mul_zsmul]
meta def eval_smul (c : context) (k : expr × ℤ) :
normal_expr → tactic (normal_expr × expr)
| (zero _) := return (zero' c, c.iapp ``zero_smul [k.1])
| (nterm e n x a) := do
(n', h₁) ← mk_app ``has_mul.mul [k.1, n.1] >>= norm_num.eval_field,
(a', h₂) ← eval_smul a,
return (term' c (n', k.2 * n.2) x a',
c.iapp ``term_smul [k.1, n.1, x, a, n', a', h₁, h₂])
theorem term_atom {α} [add_comm_monoid α] (x : α) : x = term 1 x 0 :=
by simp [term]
theorem term_atomg {α} [add_comm_group α] (x : α) : x = termg 1 x 0 :=
by simp [termg]
meta def eval_atom (c : context) (e : expr) : tactic (normal_expr × expr) :=
do n1 ← c.int_to_expr 1,
return (term' c (n1, 1) e (zero' c), c.iapp ``term_atom [e])
lemma unfold_sub {α} [subtraction_monoid α] (a b c : α) (h : a + -b = c) : a - b = c :=
by rw [sub_eq_add_neg, h]
theorem unfold_smul {α} [add_comm_monoid α] (n) (x y : α)
(h : smul n x = y) : n • x = y := h
theorem unfold_smulg {α} [add_comm_group α] (n : ℕ) (x y : α)
(h : smulg (int.of_nat n) x = y) : (n : ℤ) • x = y := h
theorem unfold_zsmul {α} [add_comm_group α] (n : ℤ) (x y : α)
(h : smulg n x = y) : n • x = y := h
lemma subst_into_smul {α} [add_comm_monoid α]
(l r tl tr t) (prl : l = tl) (prr : r = tr)
(prt : @smul α _ tl tr = t) : smul l r = t :=
by simp [prl, prr, prt]
lemma subst_into_smulg {α} [add_comm_group α]
(l r tl tr t) (prl : l = tl) (prr : r = tr)
(prt : @smulg α _ tl tr = t) : smulg l r = t :=
by simp [prl, prr, prt]
lemma subst_into_smul_upcast {α} [add_comm_group α]
(l r tl zl tr t) (prl₁ : l = tl) (prl₂ : ↑tl = zl) (prr : r = tr)
(prt : @smulg α _ zl tr = t) : smul l r = t :=
by simp [← prt, prl₁, ← prl₂, prr, smul, smulg]
/-- Normalize a term `orig` of the form `smul e₁ e₂` or `smulg e₁ e₂`.
Normalized terms use `smul` for monoids and `smulg` for groups,
so there are actually four cases to handle:
* Using `smul` in a monoid just simplifies the pieces using `subst_into_smul`
* Using `smulg` in a group just simplifies the pieces using `subst_into_smulg`
* Using `smul a b` in a group requires converting `a` from a nat to an int and
then simplifying `smulg ↑a b` using `subst_into_smul_upcast`
* Using `smulg` in a monoid is impossible (or at least out of scope),
because you need a group argument to write a `smulg` term -/
meta def eval_smul' (c : context) (eval : expr → tactic (normal_expr × expr))
(is_smulg : bool) (orig e₁ e₂ : expr) : tactic (normal_expr × expr) :=
do (e₁', p₁) ← norm_num.derive e₁ <|> refl_conv e₁,
match if is_smulg then e₁'.to_int else coe <$> e₁'.to_nat with
| some n := do
(e₂', p₂) ← eval e₂,
if c.is_group = is_smulg then do
(e', p) ← eval_smul c (e₁', n) e₂',
return (e', c.iapp ``subst_into_smul [e₁, e₂, e₁', e₂', e', p₁, p₂, p])
else do
guardb c.is_group,
ic ← mk_instance_cache `(ℤ),
nc ← mk_instance_cache `(ℕ),
(ic, zl) ← ic.of_int n,
(_, _, _, p₁') ← norm_num.prove_nat_uncast ic nc zl,
(e', p) ← eval_smul c (zl, n) e₂',
return (e', c.app ``subst_into_smul_upcast c.inst [e₁, e₂, e₁', zl, e₂', e', p₁, p₁', p₂, p])
| none := eval_atom c orig
end
meta def eval (c : context) : expr → tactic (normal_expr × expr)
| `(%%e₁ + %%e₂) := do
(e₁', p₁) ← eval e₁,
(e₂', p₂) ← eval e₂,
(e', p') ← eval_add c e₁' e₂',
p ← c.mk_app ``norm_num.subst_into_add ``has_add [e₁, e₂, e₁', e₂', e', p₁, p₂, p'],
return (e', p)
| `(%%e₁ - %%e₂) := do
e₂' ← mk_app ``has_neg.neg [e₂],
e ← mk_app ``has_add.add [e₁, e₂'],
(e', p) ← eval e,
p' ← c.mk_app ``unfold_sub ``subtraction_monoid [e₁, e₂, e', p],
return (e', p')
| `(- %%e) := do
(e₁, p₁) ← eval e,
(e₂, p₂) ← eval_neg c e₁,
p ← c.mk_app ``norm_num.subst_into_neg ``has_neg [e, e₁, e₂, p₁, p₂],
return (e₂, p)
| `(add_monoid.nsmul %%e₁ %%e₂) := do
n ← if c.is_group then mk_app ``int.of_nat [e₁] else return e₁,
(e', p) ← eval $ c.iapp ``smul [n, e₂],
return (e', c.iapp ``unfold_smul [e₁, e₂, e', p])
| `(sub_neg_monoid.zsmul %%e₁ %%e₂) := do
guardb c.is_group,
(e', p) ← eval $ c.iapp ``smul [e₁, e₂],
return (e', c.app ``unfold_zsmul c.inst [e₁, e₂, e', p])
| e@`(@has_scalar.smul nat _ add_monoid.has_scalar_nat %%e₁ %%e₂) :=
eval_smul' c eval ff e e₁ e₂
| e@`(@has_scalar.smul int _ sub_neg_monoid.has_scalar_int %%e₁ %%e₂) :=
eval_smul' c eval tt e e₁ e₂
| e@`(smul %%e₁ %%e₂) := eval_smul' c eval ff e e₁ e₂
| e@`(smulg %%e₁ %%e₂) := eval_smul' c eval tt e e₁ e₂
| e@`(@has_zero.zero _ _) := mcond (succeeds (is_def_eq e c.α0))
(mk_eq_refl c.α0 >>= λ p, pure (zero' c, p))
(eval_atom c e)
| e := eval_atom c e
meta def eval' (c : context) (e : expr) : tactic (expr × expr) :=
do (e', p) ← eval c e, return (e', p)
@[derive has_reflect]
inductive normalize_mode | raw | term
instance : inhabited normalize_mode := ⟨normalize_mode.term⟩
meta def normalize (red : transparency) (mode := normalize_mode.term) (e : expr) :
tactic (expr × expr) := do
pow_lemma ← simp_lemmas.mk.add_simp ``pow_one,
let lemmas := match mode with
| normalize_mode.term :=
[``term.equations._eqn_1, ``termg.equations._eqn_1, ``add_zero, ``one_nsmul, ``one_zsmul,
``zsmul_zero]
| _ := []
end,
lemmas ← lemmas.mfoldl simp_lemmas.add_simp simp_lemmas.mk,
(_, e', pr) ← ext_simplify_core () {}
simp_lemmas.mk (λ _, failed) (λ _ _ _ _ e, do
c ← mk_context red e,
(new_e, pr) ← match mode with
| normalize_mode.raw := eval' c
| normalize_mode.term := trans_conv (eval' c)
(λ e, do (e', prf, _) ← simplify lemmas [] e, return (e', prf))
end e,
guard (¬ new_e =ₐ e),
return ((), new_e, some pr, ff))
(λ _ _ _ _ _, failed) `eq e,
return (e', pr)
end abel
namespace interactive
open tactic.abel
setup_tactic_parser
/-- 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.
`abel1!` will use a more aggressive reducibility setting to identify atoms.
This can prove goals that `abel` cannot, but is more expensive.
-/
meta def abel1 (red : parse (tk "!")?) : tactic unit :=
do `(%%e₁ = %%e₂) ← target,
c ← mk_context (if red.is_some then semireducible else reducible) e₁,
(e₁', p₁) ← eval c e₁,
(e₂', p₂) ← eval c e₂,
is_def_eq e₁' e₂',
p ← mk_eq_symm p₂ >>= mk_eq_trans p₁,
tactic.exact p
meta def abel.mode : lean.parser abel.normalize_mode :=
with_desc "(raw|term)?" $
do mode ← ident?, match mode with
| none := return abel.normalize_mode.term
| some `term := return abel.normalize_mode.term
| some `raw := return abel.normalize_mode.raw
| _ := failed
end
/--
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.
`abel!` will use a more aggressive reducibility setting to identify atoms.
This can prove goals that `abel` cannot, but is more expensive.
```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 }
example {α : Type*} {a b : α} [add_comm_group α] : (a + b) - (id a + b) = 0 := by abel!
```
-/
meta def abel (red : parse (tk "!")?) (SOP : parse abel.mode) (loc : parse location) :
tactic unit :=
match loc with
| interactive.loc.ns [none] := abel1 red
| _ := failed
end <|>
do ns ← loc.get_locals,
let red := if red.is_some then semireducible else reducible,
tt ← tactic.replace_at (normalize red SOP) ns loc.include_goal
| fail "abel failed to simplify",
when loc.include_goal $ try tactic.reflexivity
add_tactic_doc
{ name := "abel",
category := doc_category.tactic,
decl_names := [`tactic.interactive.abel],
tags := ["arithmetic", "decision procedure"] }
end interactive
end tactic
|
a496240019cc28cf1cb607ff2e73b4a6dff2dfbb | 4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1 | /src/o_minimal/coordinates.lean | 35e0a839eadf4f6d10adb9db8798506ab1cfcd50 | [] | no_license | rwbarton/lean-omin | da209ed061d64db65a8f7f71f198064986f30eb9 | fd733c6d95ef6f4743aae97de5e15df79877c00e | refs/heads/master | 1,674,408,673,325 | 1,607,343,535,000 | 1,607,343,535,000 | 285,150,399 | 9 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,887 | lean | import data.equiv.basic
import data.fin
import for_mathlib.finvec
import for_mathlib.misc
namespace o_minimal
open function set
open_locale finvec
-- R is the type in which coordinates take values,
-- which we can imagine as being the real numbers,
-- though we do not assume anything about R.
variables (R : Type*)
section coordinates
/--
A type X *has coordinates* valued in R
when it is equipped with an embedding into Rⁿ for some specific n.
Mathematically, we can identify X with a subset of Rⁿ,
but in Lean, the type X is not restricted to being of the form
`subtype s` for some `s : set (fin n → R)`.
Informally, we denote this situation by "X ⊆ Rⁿ".
-/
class has_coordinates (X : Type*) :=
(ambdim : ℕ)
(coords : X → finvec ambdim R)
(inj : injective coords)
def coords {X : Type*} [cX : has_coordinates R X] : X → finvec cX.ambdim R :=
cX.coords
/-- Magic causing `simps` to use `coords` on the left hand side of generated `simp` lemmas. -/
def has_coordinates.simps.coords := @coords R
/- TODO: `simps` generates over-applied lemmas like
coords R x i = (...) i
rather than
coords R x = ...
because the type of `coords R x` is itself a function type (`fin n → R`).
We work around this with `{ fully_applied := ff }`
but then the lemmas are under-applied instead:
coords R = λ x, ...
-/
variables {R}
lemma injective_coords (X : Type*) [cX : has_coordinates R X] : injective (@coords R X cX) :=
cX.inj
-- TODO: generalize to `finvec n X` where `[has_coordinates R X]`;
-- but this will lose the definition as `id`.
/-- Rⁿ tautologically has coordinates given by the identity. -/
@[simps { fully_applied := ff }] instance has_coordinates.finvec (n : ℕ) : has_coordinates R (finvec n R) :=
{ ambdim := n,
coords := λ x, x,
inj := injective_id }
/-- R has coordinates given by the usual identification R ≃ R¹. -/
@[simps { fully_applied := ff }] instance has_coordinates.self : has_coordinates R R :=
{ ambdim := 1,
coords := const (fin 1),
inj := λ a b h, congr_fun h 0 }
/-- If X ⊆ Rⁿ and Y ⊆ Rᵐ then X × Y ⊆ Rⁿ⁺ᵐ. -/
@[simps { fully_applied := ff, simp_rhs := tt }] instance has_coordinates.prod
{X Y : Type*} [cX : has_coordinates R X] [cY : has_coordinates R Y] :
has_coordinates R (X × Y) :=
{ ambdim := cX.ambdim + cY.ambdim,
coords := λ p, coords R p.1 ++ coords R p.2,
inj := injective.comp (equiv.injective _) (injective.prod_map cX.inj cY.inj) }
/-- If X ⊆ Rⁿ and S is a subset of X then S ⊆ Rⁿ. -/
@[simps { fully_applied := ff }] instance has_coordinates.subtype {X : Type*} [cX : has_coordinates R X] (s : set X) :
has_coordinates R s :=
{ ambdim := cX.ambdim,
coords := λ a, coords R (subtype.val a),
inj := injective.comp cX.inj subtype.val_injective }
/-- Any subsingleton may be regarded as having coordinates valued in R⁰. -/
@[simps { fully_applied := ff }] def has_coordinates.subsingleton {X : Type*} [subsingleton X] : has_coordinates R X :=
{ ambdim := 0,
coords := λ x, fin_zero_elim,
inj := subsingleton_injective _ }
@[simps { fully_applied := ff, rhs_md := semireducible }]
instance pempty.has_coordinates : has_coordinates R pempty := has_coordinates.subsingleton
@[simps { fully_applied := ff, rhs_md := semireducible }]
instance punit.has_coordinates : has_coordinates R punit := has_coordinates.subsingleton
variables (R)
/-- The subset of Rⁿ which is mapped onto by X. -/
def coordinate_image (X : Type*) [cX : has_coordinates R X] := range (@coords R X _)
variables {R}
lemma coordinate_image_prod {X Y : Type*} [cX : has_coordinates R X] [cY : has_coordinates R Y] :
coordinate_image R (X × Y) = coordinate_image R X ⊠ coordinate_image R Y :=
begin
apply set.ext,
refine finvec.rec (λ v w, _),
rw finvec.mem_prod_iff,
simp [coordinate_image, finvec.append.inj_iff]
end
end coordinates
section reindexing
/--
If X and Y have coordinates valued in R,
then a function f : X → Y is a *reindexing*
if it fits into a diagram
X ⊆ Rⁿ
f ↓ ↓ r^*
Y ⊆ Rᵐ
where the map r^* is given by reindexing the coordinates
according to some map r : fin m → fin n, so that
r^*(x₁, ..., xₙ) = (xᵣ₍₁₎, ..., xᵣ₍ₘ₎).
For example, projections and subset inclusions are reindexings,
as are maps built from these by forming products.
A given map f may be a reindexing in more than one way (i.e., for several r),
for example if both X and Y are empty.
Here we don't care about the choice of r so we make `is_reindexing` a Prop.
A basic fact about definability is that
the preimage of a definable set under a map of the form r^* is definable.
This notion `is_reindexing` will let us reformulate this result
in the language of `has_coordinates`.
-/
inductive is_reindexing
{X Y : Type*} [cX : has_coordinates R X] [cY : has_coordinates R Y]
(f : X → Y) : Prop
| mk (σ : fin cY.ambdim → fin cX.ambdim)
(h : ∀ x i, coords R x (σ i) = coords R (f x) i)
: is_reindexing
variables {R}
lemma is_reindexing.id (X : Type*) [cX : has_coordinates R X] :
is_reindexing R (id : X → X) :=
⟨id, λ x i, rfl⟩
lemma is_reindexing.comp
{X Y Z : Type*} [cX : has_coordinates R X] [cY : has_coordinates R Y] [cZ : has_coordinates R Z]
{g : Y → Z} (hg : is_reindexing R g) {f : X → Y} (hf : is_reindexing R f) :
is_reindexing R (g ∘ f) :=
begin
cases hf with fσ hf,
cases hg with gσ hg,
refine ⟨fσ ∘ gσ, λ x i, _⟩,
simp [hf, hg]
end
lemma is_reindexing.fst
{X Y : Type*} [cX : has_coordinates R X] [cY : has_coordinates R Y] :
is_reindexing R (prod.fst : X × Y → X) :=
begin
refine ⟨fin.cast_add _, λ p i, _⟩,
change finvec.left (_ ++ _) i = _,
simp
end
lemma is_reindexing.snd
{X Y : Type*} [cX : has_coordinates R X] [cY : has_coordinates R Y] :
is_reindexing R (prod.snd : X × Y → Y) :=
begin
refine ⟨fin.nat_add _, λ p i, _⟩,
change finvec.right (_ ++ _) i = _,
simp
end
lemma is_reindexing.prod
{X Y Z : Type*} [cX : has_coordinates R X] [cY : has_coordinates R Y] [cZ : has_coordinates R Z]
{f : X → Y} (hf : is_reindexing R f) {g : X → Z} (hg : is_reindexing R g) :
is_reindexing R (λ x, (f x, g x)) :=
begin
cases hf with fσ hf,
cases hg with gσ hg,
let σ : fin cY.ambdim ⊕ fin cZ.ambdim → fin cX.ambdim := λ i, sum.cases_on i fσ gσ,
refine ⟨σ ∘ sum_fin_sum_equiv.symm, λ x, _⟩,
dsimp only [(∘)],
-- TODO: lemma for `e : α ≃ β` that `(∀ a, p a (e a)) ↔ (∀ b, p (e.symm b) b)`?
refine sum_fin_sum_equiv.forall_congr_left.mp _,
rintro (i|i); rw equiv.symm_apply_apply,
{ refine (hf _ i).trans _,
refine congr_fun _ i,
exact finvec.left_append.symm },
{ refine (hg _ i).trans _,
refine congr_fun _ i,
exact finvec.right_append.symm }
end
lemma is_reindexing.coords {X : Type*} [cX : has_coordinates R X] :
is_reindexing R (λ (x : X), coords R x) :=
⟨id, λ x j, rfl⟩
lemma is_reindexing.coord {X : Type*} [cX : has_coordinates R X] (i : fin cX.ambdim) :
is_reindexing R (λ x, coords R x i) :=
⟨λ _, i, λ x j, rfl⟩
lemma is_reindexing.subtype.val {X : Type*} [cX : has_coordinates R X] {s : set X} :
is_reindexing R (subtype.val : s → X) :=
⟨id, λ x j, rfl⟩
lemma is_reindexing.finvec.left {n m : ℕ} :
is_reindexing R (finvec.left : (finvec (n+m) R) → (finvec n R)) :=
⟨fin.cast_add m, λ x j, rfl⟩
lemma is_reindexing.finvec.right {n m : ℕ} :
is_reindexing R (finvec.right : (finvec (n+m) R) → (finvec m R)) :=
⟨fin.nat_add n, λ x j, rfl⟩
lemma is_reindexing.finvec.init {n : ℕ} :
is_reindexing R (finvec.init : (finvec (n+1) R) → (finvec n R)) :=
⟨fin.cast_succ, λ x j, rfl⟩
lemma is_reindexing.finvec.snoc {n : ℕ} :
is_reindexing R (λ (p : finvec n R × R), p.1.snoc p.2) :=
⟨id, by { intros x j, rw finvec.snoc_eq_append, refl }⟩
end reindexing
end o_minimal
|
2dc4ec10d58a85a25f60823ee995ffb5d697519d | b7f22e51856f4989b970961f794f1c435f9b8f78 | /library/algebra/module.lean | aa46547e7a376a003a5611a4c8f6f27cd0a6a8d0 | [
"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 | 2,654 | lean | /-
Copyright (c) 2015 Nathaniel Thomas. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nathaniel Thomas, Jeremy Avigad
Modules and vector spaces over a ring.
(We use "left_module," which is more precise, because "module" is a keyword.)
-/
import algebra.field
structure has_scalar [class] (F V : Type) :=
(smul : F → V → V)
infixl ` • `:73 := has_scalar.smul
/- modules over a ring -/
structure left_module [class] (R M : Type) [ringR : ring R]
extends has_scalar R M, add_comm_group M :=
(smul_left_distrib : ∀ (r : R) (x y : M), smul r (add x y) = (add (smul r x) (smul r y)))
(smul_right_distrib : ∀ (r s : R) (x : M), smul (ring.add r s) x = (add (smul r x) (smul s x)))
(mul_smul : ∀ r s x, smul (mul r s) x = smul r (smul s x))
(one_smul : ∀ x, smul one x = x)
section left_module
variables {R M : Type}
variable [ringR : ring R]
variable [moduleRM : left_module R M]
include ringR moduleRM
-- Note: the anonymous include does not work in the propositions below.
proposition smul_left_distrib (a : R) (u v : M) : a • (u + v) = a • u + a • v :=
!left_module.smul_left_distrib
proposition smul_right_distrib (a b : R) (u : M) : (a + b) • u = a • u + b • u :=
!left_module.smul_right_distrib
proposition mul_smul (a : R) (b : R) (u : M) : (a * b) • u = a • (b • u) :=
!left_module.mul_smul
proposition one_smul (u : M) : (1 : R) • u = u := !left_module.one_smul
proposition zero_smul (u : M) : (0 : R) • u = 0 :=
have (0 : R) • u + 0 • u = 0 • u + 0, by rewrite [-smul_right_distrib, *add_zero],
!add.left_cancel this
proposition smul_zero (a : R) : a • (0 : M) = 0 :=
have a • (0:M) + a • 0 = a • 0 + 0, by rewrite [-smul_left_distrib, *add_zero],
!add.left_cancel this
proposition neg_smul (a : R) (u : M) : (-a) • u = - (a • u) :=
eq_neg_of_add_eq_zero (by rewrite [-smul_right_distrib, add.left_inv, zero_smul])
proposition neg_one_smul (u : M) : -(1 : R) • u = -u :=
by rewrite [neg_smul, one_smul]
proposition smul_neg (a : R) (u : M) : a • (-u) = -(a • u) :=
by rewrite [-neg_one_smul, -mul_smul, mul_neg_one_eq_neg, neg_smul]
proposition smul_sub_left_distrib (a : R) (u v : M) : a • (u - v) = a • u - a • v :=
by rewrite [sub_eq_add_neg, smul_left_distrib, smul_neg]
proposition sub_smul_right_distrib (a b : R) (v : M) : (a - b) • v = a • v - b • v :=
by rewrite [sub_eq_add_neg, smul_right_distrib, neg_smul]
end left_module
/- vector spaces -/
structure vector_space [class] (F V : Type) [fieldF : field F]
extends left_module F V
|
757b16485b00b69d4045f93749ee6751dd3ed0a5 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/1360.lean | f3b95a0a44b1b6da570b4262c26b87ddb6d464d7 | [
"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 | 64 | lean | example: True :=
let f: {n: Nat} → Nat
| _ => 0
trivial
|
bad95ef0ebf6fce095b3fd3fbc60bd6b6e4e3739 | 43dcf09a5df9dc4729cdc024e8680eeaacd05af3 | /rational.lean | c8b2abed8f661ec5aa4ec73edb8c1c0cf9dde3f3 | [] | no_license | khoek/lmath | f9ea911aabee1eb276a5319e70a5e62e6bfe8fb1 | a4f35205e6b5a3f16926234bf8eb7f7b5f56e47f | refs/heads/master | 1,611,452,778,710 | 1,532,531,556,000 | 1,532,569,670,000 | 122,820,532 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,376 | lean | import tidy.tidy
import tactic.ring
import .number
import .binary_op
import .order
import .facts
--define pairs and the rational equality law
structure pair := mk ::
( x y : ℤ )
( non_zero : y ≠ 0)
def q_law : pair → pair → Prop := λ a b : pair, a.1 * b.2 = b.1 * a.2
theorem q_refl : ∀ ( a : pair ), q_law a a
:= by unfold q_law; intro a; refl
theorem q_symm : ∀ ( a b : pair ), q_law a b → q_law b a
:= by unfold q_law; intros a b; tidy
theorem q_trans : ∀ (a b c : pair), (q_law a b) → (q_law b c) → (q_law a c) := begin
intros a b c,
unfold q_law,
intros h1 h2,
have hr : b.2 * (c.1 * a.2) = b.2 * (a.1 * c.2), from calc
b.2 * (c.1 * a.2) = a.2 * (c.1 * b.2) : by ring
... = a.2 * (b.1 * c.2) : by rw h2
... = (b.1 * a.2) * c.2 : by ring
... = (a.1 * b.2) * c.2 : by rw h1
... = b.2 * (a.1 * c.2) : by ring,
exact eq.symm (cancellation_law b.non_zero hr),
end
instance pair_setoid := setoid.mk q_law (mk_equivalence q_law q_refl q_symm q_trans)
def pair_add : bop pair
:= λ a b : pair, pair.mk (a.1 * b.2 + b.1 * a.2) (a.2 * b.2)
(nonzero_mul a.non_zero b.non_zero)
def pair_mul : bop pair
:= λ a b : pair, pair.mk (a.1 * b.1) (a.2 * b.2)
(nonzero_mul a.non_zero b.non_zero)
--proving well-definedness, and then commutativity
lemma pair_add_assoc : bop_rel_assoc q_law pair_add
:= by intros a b c; unfold q_law; unfold pair_add; simp; ring
lemma pair_mul_assoc : bop_rel_assoc q_law pair_mul
:= by intros a b c; unfold q_law; unfold pair_mul; simp; ring
lemma pair_add_comm : bop_rel_comm q_law pair_add
:= by intros a b; unfold q_law; unfold pair_add; simp; ring
lemma pair_mul_comm : bop_rel_comm q_law pair_mul
:= by intros a b; unfold q_law; unfold pair_mul; simp; ring
lemma pair_add_invar_first : bop_rel_invar_first q_law pair_add :=
begin
intros a b c h,
unfold q_law,
--FIXME these three lines are need because obviously is really slow without
unfold pair_add,
simp,
ring,
have hh : a.x * b.y = b.x * a.y, by apply h,
obviously,
end
lemma pair_mul_invar_first : bop_rel_invar_first q_law pair_mul :=
begin
intros a b c h,
unfold q_law,
have hh : a.x * b.y = b.x * a.y, by apply h,
obviously,
end
lemma pair_add_invar : bop_rel_invar q_law pair_add
:= bop_comm_easy_invar pair_add_comm pair_add_invar_first
lemma pair_mul_invar : bop_rel_invar q_law pair_mul
:= bop_comm_easy_invar pair_mul_comm pair_mul_invar_first
def pair_add_l := liftable_bop.mk pair_setoid pair_add pair_add_invar
def pair_mul_l := liftable_bop.mk pair_setoid pair_mul pair_mul_invar
-- lift the above construction to give the rationals
def ℚℚ := quotient pair_setoid
def ℚℚ.mk (p : pair) : ℚℚ := quot.mk q_law p
def ℚℚ.divide (a b : ℤ) (h : b ≠ 0) : ℚℚ := ℚℚ.mk (pair.mk a b h)
def ℚℚ.from_int (n : ℤ) : ℚℚ := ℚℚ.mk (pair.mk n 1 one_ne_zero)
def rat_add : bop ℚℚ := lift_bop pair_add_l
def rat_mul : bop ℚℚ := lift_bop pair_mul_l
-- addition and multiplication are associative and commutative
theorem rat_add_assoc : bop_assoc rat_add
:= lift_assoc pair_add_l pair_add_assoc
theorem rat_add_comm : bop_comm rat_add
:= lift_comm pair_add_l pair_add_comm
theorem rat_mul_assoc : bop_assoc rat_mul
:= lift_assoc pair_mul_l pair_mul_assoc
theorem rat_mul_comm : bop_comm rat_mul
:= lift_comm pair_mul_l pair_mul_comm
--example of the equivalence of the general construction above to the ``direct way''
def pair_add_rat_direct : pair → pair → ℚℚ
:= λ a b : pair, (ℚℚ.mk (pair_add a b))
example : ∀ a b : pair, pair_add_rat_direct a b = (induced_quobop pair_setoid pair_add) a b := by tidy
--order
def rat_leq_rel (a b : ℚℚ) : Prop := (quot.out a).1 * (quot.out b).2 ≤ (quot.out b).1 * (quot.out a).2
lemma rat_req_refl : ∀ a : ℚℚ, rat_leq_rel a a := by unfold rat_leq_rel; tidy
lemma rat_req_antisymm : ∀ a b : ℚℚ, rat_leq_rel a b → rat_leq_rel b a → a = b :=
begin
intros a b h1 h2,
have hm : (quot.out b).x * (quot.out a).y = (quot.out a).x * (quot.out b).y,
unfold rat_leq_rel at *,
apply eq.symm (leq_fact h1 h2),
clear h1 h2,
have hn : ℚℚ.mk (quot.out a) = ℚℚ.mk (quot.out b),
have hl : q_law (quot.out a) (quot.out b),
unfold q_law,
apply eq.symm hm,
apply (@quotient_fact pair pair_setoid (quot.out a) (quot.out b)),
exact hl,
transitivity,
apply eq.symm (quotient.out_eq a),
transitivity,
apply hn,
apply quotient.out_eq,
end
#check quotient pair_setoid
#check @quotient.out_eq pair pair_setoid
lemma rat_req_trans : ∀ a b c : ℚℚ, rat_leq_rel a b → rat_leq_rel b c → rat_leq_rel a c :=
begin
intros a b c h1 h2,
unfold rat_leq_rel at *,
admit
end
#check rat_leq_rel
-- Scott: Why is this definition of rat_leq okay? Even though it secretly is, it
-- may not even be well-defined in the sense which I intend. I suppose the
-- ``constructive'' interpretation is that there is some distinguished function
-- quot.out : ℚℚ → pair which can reliably give the same element for each
-- equivalence class. This is still very strange to me! Am I really just
-- thinking that the Axiom of Choice is strange? |
917e20bd16c89ffa4af171185700339f3756cfd5 | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/ring_theory/ideal/operations.lean | b05ce608fd5635f3603bea9f7af2fa9c3572ef67 | [
"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 | 41,803 | 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 data.nat.choose.sum
import data.equiv.ring
import algebra.algebra.operations
import ring_theory.ideal.basic
/-!
# More operations on modules and ideals
-/
universes u v w x
open_locale big_operators
namespace submodule
variables {R : Type u} {M : Type v}
variables [comm_ring R] [add_comm_group M] [module R M]
instance has_scalar' : has_scalar (ideal R) (submodule R M) :=
⟨λ I N, ⨆ r : I, N.map (r.1 • linear_map.id)⟩
/-- `N.annihilator` is the ideal of all elements `r : R` such that `r • N = 0`. -/
def annihilator (N : submodule R M) : ideal R :=
(linear_map.lsmul R N).ker
/-- `N.colon P` is the ideal of all elements `r : R` such that `r • P ⊆ N`. -/
def colon (N P : submodule R M) : ideal R :=
annihilator (P.map N.mkq)
variables {I J : ideal R} {N N₁ N₂ P P₁ P₂ : submodule R M}
theorem mem_annihilator {r} : r ∈ N.annihilator ↔ ∀ n ∈ N, r • n = (0:M) :=
⟨λ hr n hn, congr_arg subtype.val (linear_map.ext_iff.1 (linear_map.mem_ker.1 hr) ⟨n, hn⟩),
λ h, linear_map.mem_ker.2 $ linear_map.ext $ λ n, subtype.eq $ h n.1 n.2⟩
theorem mem_annihilator' {r} : r ∈ N.annihilator ↔ N ≤ comap (r • linear_map.id) ⊥ :=
mem_annihilator.trans ⟨λ H n hn, (mem_bot R).2 $ H n hn, λ H n hn, (mem_bot R).1 $ H hn⟩
theorem annihilator_bot : (⊥ : submodule R M).annihilator = ⊤ :=
(ideal.eq_top_iff_one _).2 $ mem_annihilator'.2 bot_le
theorem annihilator_eq_top_iff : N.annihilator = ⊤ ↔ N = ⊥ :=
⟨λ H, eq_bot_iff.2 $ λ (n:M) hn, (mem_bot R).2 $ one_smul R n ▸ mem_annihilator.1 ((ideal.eq_top_iff_one _).1 H) n hn,
λ H, H.symm ▸ annihilator_bot⟩
theorem annihilator_mono (h : N ≤ P) : P.annihilator ≤ N.annihilator :=
λ r hrp, mem_annihilator.2 $ λ n hn, mem_annihilator.1 hrp n $ h hn
theorem annihilator_supr (ι : Sort w) (f : ι → submodule R M) :
(annihilator ⨆ i, f i) = ⨅ i, annihilator (f i) :=
le_antisymm (le_infi $ λ i, annihilator_mono $ le_supr _ _)
(λ r H, mem_annihilator'.2 $ supr_le $ λ i,
have _ := (mem_infi _).1 H i, mem_annihilator'.1 this)
theorem mem_colon {r} : r ∈ N.colon P ↔ ∀ p ∈ P, r • p ∈ N :=
mem_annihilator.trans ⟨λ H p hp, (quotient.mk_eq_zero N).1 (H (quotient.mk p) (mem_map_of_mem hp)),
λ H m ⟨p, hp, hpm⟩, hpm ▸ (N.mkq).map_smul r p ▸ (quotient.mk_eq_zero N).2 $ H p hp⟩
theorem mem_colon' {r} : r ∈ N.colon P ↔ P ≤ comap (r • linear_map.id) N :=
mem_colon
theorem colon_mono (hn : N₁ ≤ N₂) (hp : P₁ ≤ P₂) : N₁.colon P₂ ≤ N₂.colon P₁ :=
λ r hrnp, mem_colon.2 $ λ p₁ hp₁, hn $ mem_colon.1 hrnp p₁ $ hp hp₁
theorem infi_colon_supr (ι₁ : Sort w) (f : ι₁ → submodule R M)
(ι₂ : Sort x) (g : ι₂ → submodule R M) :
(⨅ i, f i).colon (⨆ j, g j) = ⨅ i j, (f i).colon (g j) :=
le_antisymm (le_infi $ λ i, le_infi $ λ j, colon_mono (infi_le _ _) (le_supr _ _))
(λ r H, mem_colon'.2 $ supr_le $ λ j, map_le_iff_le_comap.1 $ le_infi $ λ i,
map_le_iff_le_comap.2 $ mem_colon'.1 $ have _ := ((mem_infi _).1 H i),
have _ := ((mem_infi _).1 this j), this)
theorem smul_mem_smul {r} {n} (hr : r ∈ I) (hn : n ∈ N) : r • n ∈ I • N :=
(le_supr _ ⟨r, hr⟩ : _ ≤ I • N) ⟨n, hn, rfl⟩
theorem smul_le {P : submodule R M} : I • N ≤ P ↔ ∀ (r ∈ I) (n ∈ N), r • n ∈ P :=
⟨λ H r hr n hn, H $ smul_mem_smul hr hn,
λ H, supr_le $ λ r, map_le_iff_le_comap.2 $ λ n hn, H r.1 r.2 n hn⟩
@[elab_as_eliminator]
theorem smul_induction_on {p : M → Prop} {x} (H : x ∈ I • N)
(Hb : ∀ (r ∈ I) (n ∈ N), p (r • n)) (H0 : p 0)
(H1 : ∀ x y, p x → p y → p (x + y))
(H2 : ∀ (c:R) n, p n → p (c • n)) : p x :=
(@smul_le _ _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hb H
theorem mem_smul_span_singleton {I : ideal R} {m : M} {x : M} :
x ∈ I • span R ({m} : set M) ↔ ∃ y ∈ I, y • m = x :=
⟨λ hx, smul_induction_on hx
(λ r hri n hnm, let ⟨s, hs⟩ := mem_span_singleton.1 hnm in ⟨r * s, I.mul_mem_right hri, hs ▸ mul_smul r s m⟩)
⟨0, I.zero_mem, by rw [zero_smul]⟩
(λ m1 m2 ⟨y1, hyi1, hy1⟩ ⟨y2, hyi2, hy2⟩, ⟨y1 + y2, I.add_mem hyi1 hyi2, by rw [add_smul, hy1, hy2]⟩)
(λ c r ⟨y, hyi, hy⟩, ⟨c * y, I.mul_mem_left hyi, by rw [mul_smul, hy]⟩),
λ ⟨y, hyi, hy⟩, hy ▸ smul_mem_smul hyi (subset_span $ set.mem_singleton m)⟩
theorem smul_le_right : I • N ≤ N :=
smul_le.2 $ λ r hr n, N.smul_mem r
theorem smul_mono (hij : I ≤ J) (hnp : N ≤ P) : I • N ≤ J • P :=
smul_le.2 $ λ r hr n hn, smul_mem_smul (hij hr) (hnp hn)
theorem smul_mono_left (h : I ≤ J) : I • N ≤ J • N :=
smul_mono h (le_refl N)
theorem smul_mono_right (h : N ≤ P) : I • N ≤ I • P :=
smul_mono (le_refl I) h
variables (I J N P)
@[simp] theorem smul_bot : I • (⊥ : submodule R M) = ⊥ :=
eq_bot_iff.2 $ smul_le.2 $ λ r hri s hsb,
(submodule.mem_bot R).2 $ ((submodule.mem_bot R).1 hsb).symm ▸ smul_zero r
@[simp] theorem bot_smul : (⊥ : ideal R) • N = ⊥ :=
eq_bot_iff.2 $ smul_le.2 $ λ r hrb s hsi,
(submodule.mem_bot R).2 $ ((submodule.mem_bot R).1 hrb).symm ▸ zero_smul _ s
@[simp] theorem top_smul : (⊤ : ideal R) • N = N :=
le_antisymm smul_le_right $ λ r hri, one_smul R r ▸ smul_mem_smul mem_top hri
theorem smul_sup : I • (N ⊔ P) = I • N ⊔ I • P :=
le_antisymm (smul_le.2 $ λ r hri m hmnp, let ⟨n, hn, p, hp, hnpm⟩ := mem_sup.1 hmnp in
mem_sup.2 ⟨_, smul_mem_smul hri hn, _, smul_mem_smul hri hp, hnpm ▸ (smul_add _ _ _).symm⟩)
(sup_le (smul_mono_right le_sup_left)
(smul_mono_right le_sup_right))
theorem sup_smul : (I ⊔ J) • N = I • N ⊔ J • N :=
le_antisymm (smul_le.2 $ λ r hrij n hn, let ⟨ri, hri, rj, hrj, hrijr⟩ := mem_sup.1 hrij in
mem_sup.2 ⟨_, smul_mem_smul hri hn, _, smul_mem_smul hrj hn, hrijr ▸ (add_smul _ _ _).symm⟩)
(sup_le (smul_mono_left le_sup_left)
(smul_mono_left le_sup_right))
protected theorem smul_assoc : (I • J) • N = I • (J • N) :=
le_antisymm (smul_le.2 $ λ rs hrsij t htn,
smul_induction_on hrsij
(λ r hr s hs, (@smul_eq_mul R _ r s).symm ▸ smul_smul r s t ▸ smul_mem_smul hr (smul_mem_smul hs htn))
((zero_smul R t).symm ▸ submodule.zero_mem _)
(λ x y, (add_smul x y t).symm ▸ submodule.add_mem _)
(λ r s h, (@smul_eq_mul R _ r s).symm ▸ smul_smul r s t ▸ submodule.smul_mem _ _ h))
(smul_le.2 $ λ r hr sn hsn, suffices J • N ≤ submodule.comap (r • linear_map.id) ((I • J) • N), from this hsn,
smul_le.2 $ λ s hs n hn, show r • (s • n) ∈ (I • J) • N, from mul_smul r s n ▸ smul_mem_smul (smul_mem_smul hr hs) hn)
variables (S : set R) (T : set M)
theorem span_smul_span : (ideal.span S) • (span R T) =
span R (⋃ (s ∈ S) (t ∈ T), {s • t}) :=
le_antisymm (smul_le.2 $ λ r hrS n hnT, span_induction hrS
(λ r hrS, span_induction hnT
(λ n hnT, subset_span $ set.mem_bUnion hrS $
set.mem_bUnion hnT $ set.mem_singleton _)
((smul_zero r : r • 0 = (0:M)).symm ▸ submodule.zero_mem _)
(λ x y, (smul_add r x y).symm ▸ submodule.add_mem _)
(λ c m, by rw [smul_smul, mul_comm, mul_smul]; exact submodule.smul_mem _ _))
((zero_smul R n).symm ▸ submodule.zero_mem _)
(λ r s, (add_smul r s n).symm ▸ submodule.add_mem _)
(λ c r, by rw [smul_eq_mul, mul_smul]; exact submodule.smul_mem _ _)) $
span_le.2 $ set.bUnion_subset $ λ r hrS, set.bUnion_subset $ λ n hnT, set.singleton_subset_iff.2 $
smul_mem_smul (subset_span hrS) (subset_span hnT)
variables {M' : Type w} [add_comm_group M'] [module R M']
theorem map_smul'' (f : M →ₗ[R] M') : (I • N).map f = I • N.map f :=
le_antisymm (map_le_iff_le_comap.2 $ smul_le.2 $ λ r hr n hn, show f (r • n) ∈ I • N.map f,
from (f.map_smul r n).symm ▸ smul_mem_smul hr (mem_map_of_mem hn)) $
smul_le.2 $ λ r hr n hn, let ⟨p, hp, hfp⟩ := mem_map.1 hn in
hfp ▸ f.map_smul r p ▸ mem_map_of_mem (smul_mem_smul hr hp)
end submodule
namespace ideal
section chinese_remainder
variables {R : Type u} [comm_ring R] {ι : Type v}
theorem exists_sub_one_mem_and_mem (s : finset ι) {f : ι → ideal R}
(hf : ∀ i ∈ s, ∀ j ∈ s, i ≠ j → f i ⊔ f j = ⊤) (i : ι) (his : i ∈ s) :
∃ r : R, r - 1 ∈ f i ∧ ∀ j ∈ s, j ≠ i → r ∈ f j :=
begin
have : ∀ j ∈ s, j ≠ i → ∃ r : R, ∃ H : r - 1 ∈ f i, r ∈ f j,
{ intros j hjs hji, specialize hf i his j hjs hji.symm,
rw [eq_top_iff_one, submodule.mem_sup] at hf,
rcases hf with ⟨r, hri, s, hsj, hrs⟩, refine ⟨1 - r, _, _⟩,
{ rw [sub_right_comm, sub_self, zero_sub], exact (f i).neg_mem hri },
{ rw [← hrs, add_sub_cancel'], exact hsj } },
classical,
have : ∃ g : ι → R, (∀ j, g j - 1 ∈ f i) ∧ ∀ j ∈ s, j ≠ i → g j ∈ f j,
{ choose g hg1 hg2,
refine ⟨λ j, if H : j ∈ s ∧ j ≠ i then g j H.1 H.2 else 1, λ j, _, λ j, _⟩,
{ split_ifs with h, { apply hg1 }, rw sub_self, exact (f i).zero_mem },
{ intros hjs hji, rw dif_pos, { apply hg2 }, exact ⟨hjs, hji⟩ } },
rcases this with ⟨g, hgi, hgj⟩, use (∏ x in s.erase i, g x), split,
{ rw [← quotient.eq, ring_hom.map_one, ring_hom.map_prod],
apply finset.prod_eq_one, intros, rw [← ring_hom.map_one, quotient.eq], apply hgi },
intros j hjs hji, rw [← quotient.eq_zero_iff_mem, ring_hom.map_prod],
refine finset.prod_eq_zero (finset.mem_erase_of_ne_of_mem hji hjs) _,
rw quotient.eq_zero_iff_mem, exact hgj j hjs hji
end
theorem exists_sub_mem [fintype ι] {f : ι → ideal R}
(hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) (g : ι → R) :
∃ r : R, ∀ i, r - g i ∈ f i :=
begin
have : ∃ φ : ι → R, (∀ i, φ i - 1 ∈ f i) ∧ (∀ i j, i ≠ j → φ i ∈ f j),
{ have := exists_sub_one_mem_and_mem (finset.univ : finset ι) (λ i _ j _ hij, hf i j hij),
choose φ hφ,
existsi λ i, φ i (finset.mem_univ i),
exact ⟨λ i, (hφ i _).1, λ i j hij, (hφ i _).2 j (finset.mem_univ j) hij.symm⟩ },
rcases this with ⟨φ, hφ1, hφ2⟩,
use ∑ i, g i * φ i,
intros i,
rw [← quotient.eq, ring_hom.map_sum],
refine eq.trans (finset.sum_eq_single i _ _) _,
{ intros j _ hji, rw quotient.eq_zero_iff_mem, exact (f i).mul_mem_left (hφ2 j i hji) },
{ intros hi, exact (hi $ finset.mem_univ i).elim },
specialize hφ1 i, rw [← quotient.eq, ring_hom.map_one] at hφ1,
rw [ring_hom.map_mul, hφ1, mul_one]
end
/-- The homomorphism from `R/(⋂ i, f i)` to `∏ i, (R / f i)` featured in the Chinese
Remainder Theorem. It is bijective if the ideals `f i` are comaximal. -/
def quotient_inf_to_pi_quotient (f : ι → ideal R) :
(⨅ i, f i).quotient →+* Π i, (f i).quotient :=
begin
refine quotient.lift (⨅ i, f i) _ _,
{ convert @@pi.ring_hom (λ i, quotient (f i)) (λ i, ring.to_semiring) ring.to_semiring
(λ i, quotient.mk (f i)) },
{ intros r hr,
rw submodule.mem_infi at hr,
ext i,
exact quotient.eq_zero_iff_mem.2 (hr i) }
end
theorem quotient_inf_to_pi_quotient_bijective [fintype ι] {f : ι → ideal R}
(hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) :
function.bijective (quotient_inf_to_pi_quotient f) :=
⟨λ x y, quotient.induction_on₂' x y $ λ r s hrs, quotient.eq.2 $
(submodule.mem_infi _).2 $ λ i, quotient.eq.1 $
show quotient_inf_to_pi_quotient f (quotient.mk' r) i = _, by rw hrs; refl,
λ g, let ⟨r, hr⟩ := exists_sub_mem hf (λ i, quotient.out' (g i)) in
⟨quotient.mk _ r, funext $ λ i, quotient.out_eq' (g i) ▸ quotient.eq.2 (hr i)⟩⟩
/-- Chinese Remainder Theorem. Eisenbud Ex.2.6. Similar to Atiyah-Macdonald 1.10 and Stacks 00DT -/
noncomputable def quotient_inf_ring_equiv_pi_quotient [fintype ι] (f : ι → ideal R)
(hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) :
(⨅ i, f i).quotient ≃+* Π i, (f i).quotient :=
{ .. equiv.of_bijective _ (quotient_inf_to_pi_quotient_bijective hf),
.. quotient_inf_to_pi_quotient f }
end chinese_remainder
section mul_and_radical
variables {R : Type u} [comm_ring R]
variables {I J K L: ideal R}
instance : has_mul (ideal R) := ⟨(•)⟩
theorem mul_mem_mul {r s} (hr : r ∈ I) (hs : s ∈ J) : r * s ∈ I * J :=
submodule.smul_mem_smul hr hs
theorem mul_mem_mul_rev {r s} (hr : r ∈ I) (hs : s ∈ J) : s * r ∈ I * J :=
mul_comm r s ▸ mul_mem_mul hr hs
theorem mul_le : I * J ≤ K ↔ ∀ (r ∈ I) (s ∈ J), r * s ∈ K :=
submodule.smul_le
lemma mul_le_left : I * J ≤ J :=
ideal.mul_le.2 (λ r hr s, ideal.mul_mem_left _)
lemma mul_le_right : I * J ≤ I :=
ideal.mul_le.2 (λ r hr s hs, ideal.mul_mem_right _ hr)
@[simp] lemma sup_mul_right_self : I ⊔ (I * J) = I :=
sup_eq_left.2 ideal.mul_le_right
@[simp] lemma sup_mul_left_self : I ⊔ (J * I) = I :=
sup_eq_left.2 ideal.mul_le_left
@[simp] lemma mul_right_self_sup : (I * J) ⊔ I = I :=
sup_eq_right.2 ideal.mul_le_right
@[simp] lemma mul_left_self_sup : (J * I) ⊔ I = I :=
sup_eq_right.2 ideal.mul_le_left
variables (I J K)
protected theorem mul_comm : I * J = J * I :=
le_antisymm (mul_le.2 $ λ r hrI s hsJ, mul_mem_mul_rev hsJ hrI)
(mul_le.2 $ λ r hrJ s hsI, mul_mem_mul_rev hsI hrJ)
protected theorem mul_assoc : (I * J) * K = I * (J * K) :=
submodule.smul_assoc I J K
theorem span_mul_span (S T : set R) : span S * span T =
span ⋃ (s ∈ S) (t ∈ T), {s * t} :=
submodule.span_smul_span S T
variables {I J K}
lemma span_mul_span' (S T : set R) : span S * span T = span (S*T) :=
by { unfold span, rw submodule.span_mul_span,}
lemma span_singleton_mul_span_singleton (r s : R) : span {r} * span {s} = (span {r * s} : ideal R) :=
by { unfold span, rw [submodule.span_mul_span, set.singleton_mul_singleton],}
theorem mul_le_inf : I * J ≤ I ⊓ J :=
mul_le.2 $ λ r hri s hsj, ⟨I.mul_mem_right hri, J.mul_mem_left hsj⟩
theorem mul_eq_inf_of_coprime (h : I ⊔ J = ⊤) : I * J = I ⊓ J :=
le_antisymm mul_le_inf $ λ r ⟨hri, hrj⟩,
let ⟨s, hsi, t, htj, hst⟩ := submodule.mem_sup.1 ((eq_top_iff_one _).1 h) in
mul_one r ▸ hst ▸ (mul_add r s t).symm ▸ ideal.add_mem (I * J) (mul_mem_mul_rev hsi hrj) (mul_mem_mul hri htj)
variables (I)
theorem mul_bot : I * ⊥ = ⊥ :=
submodule.smul_bot I
theorem bot_mul : ⊥ * I = ⊥ :=
submodule.bot_smul I
theorem mul_top : I * ⊤ = I :=
ideal.mul_comm ⊤ I ▸ submodule.top_smul I
theorem top_mul : ⊤ * I = I :=
submodule.top_smul I
variables {I}
theorem mul_mono (hik : I ≤ K) (hjl : J ≤ L) : I * J ≤ K * L :=
submodule.smul_mono hik hjl
theorem mul_mono_left (h : I ≤ J) : I * K ≤ J * K :=
submodule.smul_mono_left h
theorem mul_mono_right (h : J ≤ K) : I * J ≤ I * K :=
submodule.smul_mono_right h
variables (I J K)
theorem mul_sup : I * (J ⊔ K) = I * J ⊔ I * K :=
submodule.smul_sup I J K
theorem sup_mul : (I ⊔ J) * K = I * K ⊔ J * K :=
submodule.sup_smul I J K
variables {I J K}
lemma pow_le_pow {m n : ℕ} (h : m ≤ n) :
I^n ≤ I^m :=
begin
cases nat.exists_eq_add_of_le h with k hk,
rw [hk, pow_add],
exact le_trans (mul_le_inf) (inf_le_left)
end
lemma mul_eq_bot {R : Type*} [integral_domain R] {I J : ideal R} :
I * J = ⊥ ↔ I = ⊥ ∨ J = ⊥ :=
⟨λ hij, or_iff_not_imp_left.mpr (λ I_ne_bot, J.eq_bot_iff.mpr (λ j hj,
let ⟨i, hi, ne0⟩ := I.ne_bot_iff.mp I_ne_bot in
or.resolve_left (mul_eq_zero.mp ((I * J).eq_bot_iff.mp hij _ (mul_mem_mul hi hj))) ne0)),
λ h, by cases h; rw [← ideal.mul_bot, h, ideal.mul_comm]⟩
/-- The radical of an ideal `I` consists of the elements `r` such that `r^n ∈ I` for some `n`. -/
def radical (I : ideal R) : ideal R :=
{ carrier := { r | ∃ n : ℕ, r ^ n ∈ I },
zero_mem' := ⟨1, (pow_one (0:R)).symm ▸ I.zero_mem⟩,
add_mem' := λ x y ⟨m, hxmi⟩ ⟨n, hyni⟩, ⟨m + n,
(add_pow x y (m + n)).symm ▸ I.sum_mem $
show ∀ c ∈ finset.range (nat.succ (m + n)), x ^ c * y ^ (m + n - c) * (nat.choose (m + n) c) ∈ I,
from λ c hc, or.cases_on (le_total c m)
(λ hcm, I.mul_mem_right $ I.mul_mem_left $ nat.add_comm n m ▸ (nat.add_sub_assoc hcm n).symm ▸
(pow_add y n (m-c)).symm ▸ I.mul_mem_right hyni)
(λ hmc, I.mul_mem_right $ I.mul_mem_right $ nat.add_sub_cancel' hmc ▸
(pow_add x m (c-m)).symm ▸ I.mul_mem_right hxmi)⟩,
smul_mem' := λ r s ⟨n, hsni⟩, ⟨n, show (r * s)^n ∈ I,
from (mul_pow r s n).symm ▸ I.mul_mem_left hsni⟩ }
theorem le_radical : I ≤ radical I :=
λ r hri, ⟨1, (pow_one r).symm ▸ hri⟩
variables (R)
theorem radical_top : (radical ⊤ : ideal R) = ⊤ :=
(eq_top_iff_one _).2 ⟨0, submodule.mem_top⟩
variables {R}
theorem radical_mono (H : I ≤ J) : radical I ≤ radical J :=
λ r ⟨n, hrni⟩, ⟨n, H hrni⟩
variables (I)
theorem radical_idem : radical (radical I) = radical I :=
le_antisymm (λ r ⟨n, k, hrnki⟩, ⟨n * k, (pow_mul r n k).symm ▸ hrnki⟩) le_radical
variables {I}
theorem radical_eq_top : radical I = ⊤ ↔ I = ⊤ :=
⟨λ h, (eq_top_iff_one _).2 $ let ⟨n, hn⟩ := (eq_top_iff_one _).1 h in
@one_pow R _ n ▸ hn, λ h, h.symm ▸ radical_top R⟩
theorem is_prime.radical (H : is_prime I) : radical I = I :=
le_antisymm (λ r ⟨n, hrni⟩, H.mem_of_pow_mem n hrni) le_radical
variables (I J)
theorem radical_sup : radical (I ⊔ J) = radical (radical I ⊔ radical J) :=
le_antisymm (radical_mono $ sup_le_sup le_radical le_radical) $
λ r ⟨n, hrnij⟩, let ⟨s, hs, t, ht, hst⟩ := submodule.mem_sup.1 hrnij in
@radical_idem _ _ (I ⊔ J) ▸ ⟨n, hst ▸ ideal.add_mem _
(radical_mono le_sup_left hs) (radical_mono le_sup_right ht)⟩
theorem radical_inf : radical (I ⊓ J) = radical I ⊓ radical J :=
le_antisymm (le_inf (radical_mono inf_le_left) (radical_mono inf_le_right))
(λ r ⟨⟨m, hrm⟩, ⟨n, hrn⟩⟩, ⟨m + n, (pow_add r m n).symm ▸ I.mul_mem_right hrm,
(pow_add r m n).symm ▸ J.mul_mem_left hrn⟩)
theorem radical_mul : radical (I * J) = radical I ⊓ radical J :=
le_antisymm (radical_inf I J ▸ radical_mono $ @mul_le_inf _ _ I J)
(λ r ⟨⟨m, hrm⟩, ⟨n, hrn⟩⟩, ⟨m + n, (pow_add r m n).symm ▸ mul_mem_mul hrm hrn⟩)
variables {I J}
theorem is_prime.radical_le_iff (hj : is_prime J) :
radical I ≤ J ↔ I ≤ J :=
⟨le_trans le_radical, λ hij r ⟨n, hrni⟩, hj.mem_of_pow_mem n $ hij hrni⟩
theorem radical_eq_Inf (I : ideal R) :
radical I = Inf { J : ideal R | I ≤ J ∧ is_prime J } :=
le_antisymm (le_Inf $ λ J hJ, hJ.2.radical_le_iff.2 hJ.1) $
λ r hr, classical.by_contradiction $ λ hri,
let ⟨m, (hrm : r ∉ radical m), him, hm⟩ := zorn.zorn_partial_order₀ {K : ideal R | r ∉ radical K}
(λ c hc hcc y hyc, ⟨Sup c, λ ⟨n, hrnc⟩, let ⟨y, hyc, hrny⟩ :=
(submodule.mem_Sup_of_directed ⟨y, hyc⟩ hcc.directed_on).1 hrnc in hc hyc ⟨n, hrny⟩,
λ z, le_Sup⟩) I hri in
have ∀ x ∉ m, r ∈ radical (m ⊔ span {x}) := λ x hxm, classical.by_contradiction $ λ hrmx, hxm $
hm (m ⊔ span {x}) hrmx le_sup_left ▸ (le_sup_right : _ ≤ m ⊔ span {x}) (subset_span $ set.mem_singleton _),
have is_prime m, from ⟨by rintro rfl; rw radical_top at hrm; exact hrm trivial,
λ x y hxym, or_iff_not_imp_left.2 $ λ hxm, classical.by_contradiction $ λ hym,
let ⟨n, hrn⟩ := this _ hxm, ⟨p, hpm, q, hq, hpqrn⟩ := submodule.mem_sup.1 hrn, ⟨c, hcxq⟩ := mem_span_singleton'.1 hq in
let ⟨k, hrk⟩ := this _ hym, ⟨f, hfm, g, hg, hfgrk⟩ := submodule.mem_sup.1 hrk, ⟨d, hdyg⟩ := mem_span_singleton'.1 hg in
hrm ⟨n + k, by rw [pow_add, ← hpqrn, ← hcxq, ← hfgrk, ← hdyg, add_mul, mul_add (c*x), mul_assoc c x (d*y), mul_left_comm x, ← mul_assoc];
refine m.add_mem (m.mul_mem_right hpm) (m.add_mem (m.mul_mem_left hfm) (m.mul_mem_left hxym))⟩⟩,
hrm $ this.radical.symm ▸ (Inf_le ⟨him, this⟩ : Inf {J : ideal R | I ≤ J ∧ is_prime J} ≤ m) hr
instance : comm_semiring (ideal R) := submodule.comm_semiring
@[simp] lemma add_eq_sup : I + J = I ⊔ J := rfl
@[simp] lemma zero_eq_bot : (0 : ideal R) = ⊥ := rfl
@[simp] lemma one_eq_top : (1 : ideal R) = ⊤ :=
by erw [submodule.one_eq_map_top, submodule.map_id]
variables (R)
theorem top_pow (n : ℕ) : (⊤ ^ n : ideal R) = ⊤ :=
nat.rec_on n one_eq_top $ λ n ih, by rw [pow_succ, ih, top_mul]
variables {R}
variables (I)
theorem radical_pow (n : ℕ) (H : n > 0) : radical (I^n) = radical I :=
nat.rec_on n (not.elim dec_trivial) (λ n ih H,
or.cases_on (lt_or_eq_of_le $ nat.le_of_lt_succ H)
(λ H, calc radical (I^(n+1))
= radical I ⊓ radical (I^n) : radical_mul _ _
... = radical I ⊓ radical I : by rw ih H
... = radical I : inf_idem)
(λ H, H ▸ (pow_one I).symm ▸ rfl)) H
end mul_and_radical
section map_and_comap
variables {R : Type u} {S : Type v} [comm_ring R] [comm_ring S]
variables (f : R →+* S)
variables {I J : ideal R} {K L : ideal S}
/-- `I.map f` is the span of the image of the ideal `I` under `f`, which may be bigger than
the image itself. -/
def map (I : ideal R) : ideal S :=
span (f '' I)
/-- `I.comap f` is the preimage of `I` under `f`. -/
def comap (I : ideal S) : ideal R :=
{ carrier := f ⁻¹' I,
smul_mem' := λ c x hx, show f (c * x) ∈ I, by { rw f.map_mul, exact I.mul_mem_left hx },
.. I.to_add_submonoid.comap (f : R →+ S) }
variables {f}
theorem map_mono (h : I ≤ J) : map f I ≤ map f J :=
span_mono $ set.image_subset _ h
theorem mem_map_of_mem {x} (h : x ∈ I) : f x ∈ map f I :=
subset_span ⟨x, h, rfl⟩
theorem map_le_iff_le_comap :
map f I ≤ K ↔ I ≤ comap f K :=
span_le.trans set.image_subset_iff
@[simp] theorem mem_comap {x} : x ∈ comap f K ↔ f x ∈ K := iff.rfl
theorem comap_mono (h : K ≤ L) : comap f K ≤ comap f L :=
set.preimage_mono (λ x hx, h hx)
variables (f)
theorem comap_ne_top (hK : K ≠ ⊤) : comap f K ≠ ⊤ :=
(ne_top_iff_one _).2 $ by rw [mem_comap, f.map_one];
exact (ne_top_iff_one _).1 hK
theorem is_prime.comap [hK : K.is_prime] : (comap f K).is_prime :=
⟨comap_ne_top _ hK.1, λ x y,
by simp only [mem_comap, f.map_mul]; apply hK.2⟩
variables (I J K L)
theorem map_top : map f ⊤ = ⊤ :=
(eq_top_iff_one _).2 $ subset_span ⟨1, trivial, f.map_one⟩
theorem map_mul : map f (I * J) = map f I * map f J :=
le_antisymm (map_le_iff_le_comap.2 $ mul_le.2 $ λ r hri s hsj,
show f (r * s) ∈ _, by rw f.map_mul;
exact mul_mem_mul (mem_map_of_mem hri) (mem_map_of_mem hsj))
(trans_rel_right _ (span_mul_span _ _) $ span_le.2 $
set.bUnion_subset $ λ i ⟨r, hri, hfri⟩,
set.bUnion_subset $ λ j ⟨s, hsj, hfsj⟩,
set.singleton_subset_iff.2 $ hfri ▸ hfsj ▸
by rw [← f.map_mul];
exact mem_map_of_mem (mul_mem_mul hri hsj))
variable (f)
lemma gc_map_comap : galois_connection (ideal.map f) (ideal.comap f) :=
λ I J, ideal.map_le_iff_le_comap
@[simp] lemma comap_id : I.comap (ring_hom.id R) = I :=
ideal.ext $ λ _, iff.rfl
@[simp] lemma map_id : I.map (ring_hom.id R) = I :=
(gc_map_comap (ring_hom.id R)).l_unique galois_connection.id comap_id
lemma comap_comap {T : Type*} [comm_ring T] {I : ideal T} (f : R →+* S)
(g : S →+*T) : (I.comap g).comap f = I.comap (g.comp f) := rfl
lemma map_map {T : Type*} [comm_ring T] {I : ideal R} (f : R →+* S)
(g : S →+*T) : (I.map f).map g = I.map (g.comp f) :=
((gc_map_comap f).compose _ _ _ _ (gc_map_comap g)).l_unique
(gc_map_comap (g.comp f)) (λ _, comap_comap _ _)
variables {f I J K L}
lemma map_le_of_le_comap : I ≤ K.comap f → I.map f ≤ K :=
(gc_map_comap f).l_le
lemma le_comap_of_map_le : I.map f ≤ K → I ≤ K.comap f :=
(gc_map_comap f).le_u
lemma le_comap_map : I ≤ (I.map f).comap f :=
(gc_map_comap f).le_u_l _
lemma map_comap_le : (K.comap f).map f ≤ K :=
(gc_map_comap f).l_u_le _
@[simp] lemma comap_top : (⊤ : ideal S).comap f = ⊤ :=
(gc_map_comap f).u_top
@[simp] lemma comap_eq_top_iff {I : ideal S} : I.comap f = ⊤ ↔ I = ⊤ :=
⟨ λ h, I.eq_top_iff_one.mpr (f.map_one ▸ mem_comap.mp ((I.comap f).eq_top_iff_one.mp h)),
λ h, by rw [h, comap_top] ⟩
@[simp] lemma map_bot : (⊥ : ideal R).map f = ⊥ :=
(gc_map_comap f).l_bot
variables (f I J K L)
@[simp] lemma map_comap_map : ((I.map f).comap f).map f = I.map f :=
congr_fun (gc_map_comap f).l_u_l_eq_l I
@[simp] lemma comap_map_comap : ((K.comap f).map f).comap f = K.comap f :=
congr_fun (gc_map_comap f).u_l_u_eq_u K
lemma map_sup : (I ⊔ J).map f = I.map f ⊔ J.map f :=
(gc_map_comap f).l_sup
theorem comap_inf : comap f (K ⊓ L) = comap f K ⊓ comap f L := rfl
variables {ι : Sort*}
lemma map_supr (K : ι → ideal R) : (supr K).map f = ⨆ i, (K i).map f :=
(gc_map_comap f).l_supr
lemma comap_infi (K : ι → ideal S) : (infi K).comap f = ⨅ i, (K i).comap f :=
(gc_map_comap f).u_infi
lemma map_Sup (s : set (ideal R)): (Sup s).map f = ⨆ I ∈ s, (I : ideal R).map f :=
(gc_map_comap f).l_Sup
lemma comap_Inf (s : set (ideal S)): (Inf s).comap f = ⨅ I ∈ s, (I : ideal S).comap f :=
(gc_map_comap f).u_Inf
lemma comap_Inf' (s : set (ideal S)) : (Inf s).comap f = ⨅ I ∈ (comap f '' s), I :=
trans (comap_Inf f s) (by rw infi_image)
theorem comap_radical : comap f (radical K) = radical (comap f K) :=
le_antisymm (λ r ⟨n, hfrnk⟩, ⟨n, show f (r ^ n) ∈ K,
from (f.map_pow r n).symm ▸ hfrnk⟩)
(λ r ⟨n, hfrnk⟩, ⟨n, f.map_pow r n ▸ hfrnk⟩)
theorem comap_is_prime [H : is_prime K] : is_prime (comap f K) :=
⟨comap_ne_top f H.left,
λ x y h, H.right (show f x * f y ∈ K, by rwa [mem_comap, ring_hom.map_mul] at h)⟩
@[simp] lemma map_quotient_self :
map (quotient.mk I) I = ⊥ :=
eq_bot_iff.2 $ ideal.map_le_iff_le_comap.2 $ λ x hx,
(submodule.mem_bot I.quotient).2 $ ideal.quotient.eq_zero_iff_mem.2 hx
variables {I J K L}
theorem map_inf_le : map f (I ⊓ J) ≤ map f I ⊓ map f J :=
(gc_map_comap f).monotone_l.map_inf_le _ _
theorem map_radical_le : map f (radical I) ≤ radical (map f I) :=
map_le_iff_le_comap.2 $ λ r ⟨n, hrni⟩, ⟨n, f.map_pow r n ▸ mem_map_of_mem hrni⟩
theorem le_comap_sup : comap f K ⊔ comap f L ≤ comap f (K ⊔ L) :=
(gc_map_comap f).monotone_u.le_map_sup _ _
theorem le_comap_mul : comap f K * comap f L ≤ comap f (K * L) :=
map_le_iff_le_comap.1 $ (map_mul f (comap f K) (comap f L)).symm ▸
mul_mono (map_le_iff_le_comap.2 $ le_refl _) (map_le_iff_le_comap.2 $ le_refl _)
section surjective
variables (hf : function.surjective f)
include hf
open function
theorem map_comap_of_surjective (I : ideal S) :
map f (comap f I) = I :=
le_antisymm (map_le_iff_le_comap.2 (le_refl _))
(λ s hsi, let ⟨r, hfrs⟩ := hf s in
hfrs ▸ (mem_map_of_mem $ show f r ∈ I, from hfrs.symm ▸ hsi))
/-- `map` and `comap` are adjoint, and the composition `map f ∘ comap f` is the
identity -/
def gi_map_comap : galois_insertion (map f) (comap f) :=
galois_insertion.monotone_intro
((gc_map_comap f).monotone_u)
((gc_map_comap f).monotone_l)
(λ _, le_comap_map)
(map_comap_of_surjective _ hf)
lemma map_surjective_of_surjective : surjective (map f) :=
(gi_map_comap f hf).l_surjective
lemma comap_injective_of_surjective : injective (comap f) :=
(gi_map_comap f hf).u_injective
lemma map_sup_comap_of_surjective (I J : ideal S) : (I.comap f ⊔ J.comap f).map f = I ⊔ J :=
(gi_map_comap f hf).l_sup_u _ _
lemma map_supr_comap_of_surjective (K : ι → ideal S) : (⨆i, (K i).comap f).map f = supr K :=
(gi_map_comap f hf).l_supr_u _
lemma map_inf_comap_of_surjective (I J : ideal S) : (I.comap f ⊓ J.comap f).map f = I ⊓ J :=
(gi_map_comap f hf).l_inf_u _ _
lemma map_infi_comap_of_surjective (K : ι → ideal S) : (⨅i, (K i).comap f).map f = infi K :=
(gi_map_comap f hf).l_infi_u _
theorem mem_image_of_mem_map_of_surjective {I : ideal R} {y}
(H : y ∈ map f I) : y ∈ f '' I :=
submodule.span_induction H (λ _, id) ⟨0, I.zero_mem, f.map_zero⟩
(λ y1 y2 ⟨x1, hx1i, hxy1⟩ ⟨x2, hx2i, hxy2⟩, ⟨x1 + x2, I.add_mem hx1i hx2i, hxy1 ▸ hxy2 ▸ f.map_add _ _⟩)
(λ c y ⟨x, hxi, hxy⟩, let ⟨d, hdc⟩ := hf c in ⟨d • x, I.smul_mem _ hxi, hdc ▸ hxy ▸ f.map_mul _ _⟩)
lemma mem_map_iff_of_surjective {I : ideal R} {y} :
y ∈ map f I ↔ ∃ x, x ∈ I ∧ f x = y :=
⟨λ h, (set.mem_image _ _ _).2 (mem_image_of_mem_map_of_surjective f hf h),
λ ⟨x, hx⟩, hx.right ▸ (mem_map_of_mem hx.left)⟩
theorem comap_map_of_surjective (I : ideal R) :
comap f (map f I) = I ⊔ comap f ⊥ :=
le_antisymm (assume r h, let ⟨s, hsi, hfsr⟩ := mem_image_of_mem_map_of_surjective f hf h in
submodule.mem_sup.2 ⟨s, hsi, r - s, (submodule.mem_bot S).2 $ by rw [f.map_sub, hfsr, sub_self],
add_sub_cancel'_right s r⟩)
(sup_le (map_le_iff_le_comap.1 (le_refl _)) (comap_mono bot_le))
lemma le_map_of_comap_le_of_surjective : comap f K ≤ I → K ≤ map f I :=
λ h, (map_comap_of_surjective f hf K) ▸ map_mono h
/-- Correspondence theorem -/
def rel_iso_of_surjective :
ideal S ≃o { p : ideal R // comap f ⊥ ≤ p } :=
{ to_fun := λ J, ⟨comap f J, comap_mono bot_le⟩,
inv_fun := λ I, map f I.1,
left_inv := λ J, map_comap_of_surjective f hf J,
right_inv := λ I, subtype.eq $ show comap f (map f I.1) = I.1,
from (comap_map_of_surjective f hf I).symm ▸ le_antisymm
(sup_le (le_refl _) I.2) le_sup_left,
map_rel_iff' := λ I1 I2, ⟨comap_mono, λ H, map_comap_of_surjective f hf I1 ▸
map_comap_of_surjective f hf I2 ▸ map_mono H⟩ }
/-- The map on ideals induced by a surjective map preserves inclusion. -/
def order_embedding_of_surjective : ideal S ↪o ideal R :=
(rel_iso_of_surjective f hf).to_rel_embedding.trans (subtype.rel_embedding _ _)
theorem map_eq_top_or_is_maximal_of_surjective (H : is_maximal I) :
(map f I) = ⊤ ∨ is_maximal (map f I) :=
begin
refine or_iff_not_imp_left.2 (λ ne_top, ⟨λ h, ne_top h, λ J hJ, _⟩),
{ refine (rel_iso_of_surjective f hf).injective
(subtype.ext_iff.2 (eq.trans (H.right (comap f J) (lt_of_le_of_ne _ _)) comap_top.symm)),
{ exact (map_le_iff_le_comap).1 (le_of_lt hJ) },
{ exact λ h, hJ.right (le_map_of_comap_le_of_surjective f hf (le_of_eq h.symm)) } }
end
theorem comap_is_maximal_of_surjective [H : is_maximal K] : is_maximal (comap f K) :=
begin
refine ⟨comap_ne_top _ H.left, λ J hJ, _⟩,
suffices : map f J = ⊤,
{ replace this := congr_arg (comap f) this,
rw [comap_top, comap_map_of_surjective _ hf, eq_top_iff] at this,
rw eq_top_iff,
exact le_trans this (sup_le (le_of_eq rfl) (le_trans (comap_mono (bot_le)) (le_of_lt hJ))) },
refine H.right (map f J) (lt_of_le_of_ne (le_map_of_comap_le_of_surjective _ hf (le_of_lt hJ))
(λ h, ne_of_lt hJ (trans (congr_arg (comap f) h) _))),
rw [comap_map_of_surjective _ hf, sup_eq_left],
exact le_trans (comap_mono bot_le) (le_of_lt hJ)
end
end surjective
lemma mem_quotient_iff_mem (hIJ : I ≤ J) {x : R} :
quotient.mk I x ∈ J.map (quotient.mk I) ↔ x ∈ J :=
begin
refine iff.trans (mem_map_iff_of_surjective _ quotient.mk_surjective) _,
split,
{ rintros ⟨x, x_mem, x_eq⟩,
simpa using J.add_mem (hIJ (quotient.eq.mp x_eq.symm)) x_mem },
{ intro x_mem,
exact ⟨x, x_mem, rfl⟩ }
end
section injective
variables (hf : function.injective f)
include hf
open function
lemma comap_bot_le_of_injective : comap f ⊥ ≤ I :=
begin
refine le_trans (λ x hx, _) bot_le,
rw [mem_comap, submodule.mem_bot, ← ring_hom.map_zero f] at hx,
exact eq.symm (hf hx) ▸ (submodule.zero_mem ⊥)
end
end injective
section bijective
variables (hf : function.bijective f)
include hf
open function
/-- Special case of the correspondence theorem for isomorphic rings -/
def rel_iso_of_bijective : ideal S ≃o ideal R :=
{ to_fun := comap f,
inv_fun := map f,
left_inv := (rel_iso_of_surjective f hf.right).left_inv,
right_inv := λ J, subtype.ext_iff.1
((rel_iso_of_surjective f hf.right).right_inv ⟨J, comap_bot_le_of_injective f hf.left⟩),
map_rel_iff' := (rel_iso_of_surjective f hf.right).map_rel_iff' }
lemma comap_le_iff_le_map : comap f K ≤ I ↔ K ≤ map f I :=
⟨λ h, le_map_of_comap_le_of_surjective f hf.right h,
λ h, ((rel_iso_of_bijective f hf).right_inv I) ▸ comap_mono h⟩
theorem map.is_maximal (H : is_maximal I) : is_maximal (map f I) :=
by refine or_iff_not_imp_left.1
(map_eq_top_or_is_maximal_of_surjective f hf.right H) (λ h, H.left _);
calc I = comap f (map f I) : ((rel_iso_of_bijective f hf).right_inv I).symm
... = comap f ⊤ : by rw h
... = ⊤ : by rw comap_top
end bijective
end map_and_comap
section is_primary
variables {R : Type u} [comm_ring R]
/-- A proper ideal `I` is primary iff `xy ∈ I` implies `x ∈ I` or `y ∈ radical I`. -/
def is_primary (I : ideal R) : Prop :=
I ≠ ⊤ ∧ ∀ {x y : R}, x * y ∈ I → x ∈ I ∨ y ∈ radical I
theorem is_primary.to_is_prime (I : ideal R) (hi : is_prime I) : is_primary I :=
⟨hi.1, λ x y hxy, (hi.2 hxy).imp id $ λ hyi, le_radical hyi⟩
theorem mem_radical_of_pow_mem {I : ideal R} {x : R} {m : ℕ} (hx : x ^ m ∈ radical I) : x ∈ radical I :=
radical_idem I ▸ ⟨m, hx⟩
theorem is_prime_radical {I : ideal R} (hi : is_primary I) : is_prime (radical I) :=
⟨mt radical_eq_top.1 hi.1, λ x y ⟨m, hxy⟩, begin
rw mul_pow at hxy, cases hi.2 hxy,
{ exact or.inl ⟨m, h⟩ },
{ exact or.inr (mem_radical_of_pow_mem h) }
end⟩
theorem is_primary_inf {I J : ideal R} (hi : is_primary I) (hj : is_primary J)
(hij : radical I = radical J) : is_primary (I ⊓ J) :=
⟨ne_of_lt $ lt_of_le_of_lt inf_le_left (lt_top_iff_ne_top.2 hi.1), λ x y ⟨hxyi, hxyj⟩,
begin
rw [radical_inf, hij, inf_idem],
cases hi.2 hxyi with hxi hyi, cases hj.2 hxyj with hxj hyj,
{ exact or.inl ⟨hxi, hxj⟩ },
{ exact or.inr hyj },
{ rw hij at hyi, exact or.inr hyi }
end⟩
end is_primary
end ideal
namespace ring_hom
variables {R : Type u} {S : Type v} [comm_ring R]
section comm_ring
variables [comm_ring S] (f : R →+* S)
/-- Kernel of a ring homomorphism as an ideal of the domain. -/
def ker : ideal R := ideal.comap f ⊥
/-- An element is in the kernel if and only if it maps to zero.-/
lemma mem_ker {r} : r ∈ ker f ↔ f r = 0 :=
by rw [ker, ideal.mem_comap, submodule.mem_bot]
lemma ker_eq : ((ker f) : set R) = is_add_group_hom.ker f := rfl
lemma ker_eq_comap_bot (f : R →+* S) : f.ker = ideal.comap f ⊥ := rfl
lemma injective_iff_ker_eq_bot : function.injective f ↔ ker f = ⊥ :=
by rw [submodule.ext'_iff, ker_eq]; exact is_add_group_hom.injective_iff_trivial_ker f
lemma ker_eq_bot_iff_eq_zero : ker f = ⊥ ↔ ∀ x, f x = 0 → x = 0 :=
by rw [submodule.ext'_iff, ker_eq]; exact is_add_group_hom.trivial_ker_iff_eq_zero f
/-- If the target is not the zero ring, then one is not in the kernel.-/
lemma not_one_mem_ker [nontrivial S] (f : R →+* S) : (1:R) ∉ ker f :=
by { rw [mem_ker, f.map_one], exact one_ne_zero }
@[simp] lemma ker_coe_equiv (f : R ≃+* S) : ker (f : R →+* S) = ⊥ :=
by simpa only [←injective_iff_ker_eq_bot] using f.injective
end comm_ring
/-- The kernel of a homomorphism to an integral domain is a prime ideal.-/
lemma ker_is_prime [integral_domain S] (f : R →+* S) :
(ker f).is_prime :=
⟨by { rw [ne.def, ideal.eq_top_iff_one], exact not_one_mem_ker f },
λ x y, by simpa only [mem_ker, f.map_mul] using @eq_zero_or_eq_zero_of_mul_eq_zero S _ _ _ _ _⟩
end ring_hom
namespace ideal
variables {R : Type*} {S : Type*} [comm_ring R] [comm_ring S]
lemma map_eq_bot_iff_le_ker {I : ideal R} (f : R →+* S) : I.map f = ⊥ ↔ I ≤ f.ker :=
by rw [ring_hom.ker, eq_bot_iff, map_le_iff_le_comap]
@[simp] lemma mk_ker {I : ideal R} : (quotient.mk I).ker = I :=
by ext; rw [ring_hom.ker, mem_comap, submodule.mem_bot, quotient.eq_zero_iff_mem]
lemma ker_le_comap {K : ideal S} (f : R →+* S) : f.ker ≤ comap f K :=
λ x hx, mem_comap.2 (((ring_hom.mem_ker f).1 hx).symm ▸ K.zero_mem)
lemma map_Inf {A : set (ideal R)} {f : R →+* S} (hf : function.surjective f) :
(∀ J ∈ A, ring_hom.ker f ≤ J) → map f (Inf A) = Inf (map f '' A) :=
begin
refine λ h, le_antisymm (le_Inf _) _,
{ intros j hj y hy,
cases (mem_map_iff_of_surjective f hf).1 hy with x hx,
cases (set.mem_image _ _ _).mp hj with J hJ,
rw [← hJ.right, ← hx.right],
exact mem_map_of_mem (Inf_le_of_le hJ.left (le_of_eq rfl) hx.left) },
{ intros y hy,
cases hf y with x hx,
refine hx ▸ (mem_map_of_mem _),
rw Inf_eq_infi at ⊢ hy,
simp at ⊢ hy,
intros J hJ,
cases (mem_map_iff_of_surjective f hf).1 (hy (map f J) J hJ rfl) with x' hx',
have : x - x' ∈ J,
{ apply h J hJ,
rw [ring_hom.mem_ker, ring_hom.map_sub, hx, hx'.right, sub_self y], },
convert J.add_mem this hx'.left,
ring, }
end
theorem map_is_prime_of_surjective {f : R →+* S} (hf : function.surjective f) {I : ideal R}
[H : is_prime I] (hk : ring_hom.ker f ≤ I) : is_prime (map f I) :=
begin
refine ⟨λ h, H.left (eq_top_iff.2 _), λ x y, _⟩,
{ replace h := congr_arg (comap f) h,
rw [comap_map_of_surjective _ hf, comap_top] at h,
exact h ▸ sup_le (le_of_eq rfl) hk },
{ refine λ hxy, (hf x).rec_on (λ a ha, (hf y).rec_on (λ b hb, _)),
rw [← ha, ← hb, ← ring_hom.map_mul, mem_map_iff_of_surjective _ hf] at hxy,
rcases hxy with ⟨c, hc, hc'⟩,
rw [← sub_eq_zero, ← ring_hom.map_sub] at hc',
have : a * b ∈ I,
{ convert I.sub_mem hc (hk (hc' : c - a * b ∈ f.ker)),
ring },
exact (H.right this).imp (λ h, ha ▸ mem_map_of_mem h) (λ h, hb ▸ mem_map_of_mem h) }
end
theorem map_is_prime_of_equiv (f : R ≃+* S) {I : ideal R} [is_prime I] :
is_prime (map (f : R →+* S) I) :=
map_is_prime_of_surjective f.surjective $ by simp
theorem map_radical_of_surjective {f : R →+* S} (hf : function.surjective f) {I : ideal R}
(h : ring_hom.ker f ≤ I) : map f (I.radical) = (map f I).radical :=
begin
rw [radical_eq_Inf, radical_eq_Inf],
have : ∀ J ∈ {J : ideal R | I ≤ J ∧ J.is_prime}, f.ker ≤ J := λ J hJ, le_trans h hJ.left,
convert map_Inf hf this,
refine funext (λ j, propext ⟨_, _⟩),
{ rintros ⟨hj, hj'⟩,
haveI : j.is_prime := hj',
exact ⟨comap f j, ⟨⟨map_le_iff_le_comap.1 hj, comap_is_prime f j⟩,
map_comap_of_surjective f hf j⟩⟩ },
{ rintro ⟨J, ⟨hJ, hJ'⟩⟩,
haveI : J.is_prime := hJ.right,
refine ⟨hJ' ▸ map_mono hJ.left, hJ' ▸ map_is_prime_of_surjective hf (le_trans h hJ.left)⟩ },
end
section quotient_algebra
/-- The ring hom `R/f⁻¹(I) →+* S/I` induced by a ring hom `f : R →+* S` -/
def quotient_map {I : ideal R} (J : ideal S) (f : R →+* S) (hIJ : I ≤ J.comap f) :
I.quotient →+* J.quotient :=
(quotient.lift I ((quotient.mk J).comp f) (λ _ ha,
by simpa [function.comp_app, ring_hom.coe_comp, quotient.eq_zero_iff_mem] using hIJ ha))
variables {I : ideal R} {J: ideal S} [algebra R S]
@[priority 100]
instance quotient_algebra : algebra (J.comap (algebra_map R S)).quotient J.quotient :=
(quotient_map J (algebra_map R S) (le_of_eq rfl)).to_algebra
lemma algebra_map_quotient_injective :
function.injective (algebra_map (J.comap (algebra_map R S)).quotient J.quotient) :=
begin
rintros ⟨a⟩ ⟨b⟩ hab,
replace hab := quotient.eq.mp hab,
rw ← ring_hom.map_sub at hab,
exact quotient.eq.mpr hab
end
end quotient_algebra
end ideal
namespace submodule
variables {R : Type u} {M : Type v}
variables [comm_ring R] [add_comm_group M] [module R M]
-- It is even a semialgebra. But those aren't in mathlib yet.
instance semimodule_submodule : semimodule (ideal R) (submodule R M) :=
{ smul_add := smul_sup,
add_smul := sup_smul,
mul_smul := submodule.smul_assoc,
one_smul := by simp,
zero_smul := bot_smul,
smul_zero := smul_bot }
end submodule
namespace ring_hom
variables {A B C : Type*} [comm_ring A] [comm_ring B] [comm_ring C]
variables (f : A →+* B)
/-- `lift_of_surjective f hf g hg` is the unique ring homomorphism `φ`
* such that `φ.comp f = g` (`lift_of_surjective_comp`),
* where `f : A →+* B` is surjective (`hf`),
* and `g : B →+* C` satisfies `hg : f.ker ≤ g.ker`.
See `lift_of_surjective_eq` for the uniqueness lemma.
```
A .
| \
f | \ g
| \
v \⌟
B ----> C
∃!φ
```
-/
noncomputable def lift_of_surjective
(hf : function.surjective f) (g : A →+* C) (hg : f.ker ≤ g.ker) :
B →+* C :=
{ to_fun := λ b, g (classical.some (hf b)),
map_one' :=
begin
rw [← g.map_one, ← sub_eq_zero, ← g.map_sub, ← g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_sub, sub_eq_zero, f.map_one],
exact classical.some_spec (hf 1)
end,
map_mul' :=
begin
intros x y,
rw [← g.map_mul, ← sub_eq_zero, ← g.map_sub, ← g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_sub, sub_eq_zero, f.map_mul],
simp only [classical.some_spec (hf _)],
end,
.. add_monoid_hom.lift_of_surjective f.to_add_monoid_hom hf g.to_add_monoid_hom hg }
@[simp] lemma lift_of_surjective_comp_apply
(hf : function.surjective f) (g : A →+* C) (hg : f.ker ≤ g.ker) (a : A) :
(f.lift_of_surjective hf g hg) (f a) = g a :=
f.to_add_monoid_hom.lift_of_surjective_comp_apply hf g.to_add_monoid_hom hg a
@[simp] lemma lift_of_surjective_comp (hf : function.surjective f) (g : A →+* C) (hg : f.ker ≤ g.ker) :
(f.lift_of_surjective hf g hg).comp f = g :=
by { ext, simp only [comp_apply, lift_of_surjective_comp_apply] }
lemma eq_lift_of_surjective (hf : function.surjective f) (g : A →+* C) (hg : f.ker ≤ g.ker)
(h : B →+* C) (hh : h.comp f = g) :
h = (f.lift_of_surjective hf g hg) :=
begin
ext b, rcases hf b with ⟨a, rfl⟩,
simp only [← comp_apply, hh, f.lift_of_surjective_comp],
end
end ring_hom
|
e712ce4cfe7572e967d5828844ac9d1040a5f466 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/data/nat/basic.lean | 673f6638a9ad009eaad7c086f7cc7155894c3184 | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 65,410 | lean | /-
Copyright (c) 2014 Floris van Doorn (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro
-/
import algebra.order.ring
/-!
# Basic operations on the natural numbers
This file contains:
- instances on the natural numbers
- some basic lemmas about natural numbers
- extra recursors:
* `le_rec_on`, `le_induction`: recursion and induction principles starting at non-zero numbers
* `decreasing_induction`: recursion growing downwards
* `le_rec_on'`, `decreasing_induction'`: versions with slightly weaker assumptions
* `strong_rec'`: recursion based on strong inequalities
- decidability instances on predicates about the natural numbers
-/
universes u v
/-! ### instances -/
instance : nontrivial ℕ :=
⟨⟨0, 1, nat.zero_ne_one⟩⟩
instance : comm_semiring ℕ :=
{ add := nat.add,
add_assoc := nat.add_assoc,
zero := nat.zero,
zero_add := nat.zero_add,
add_zero := nat.add_zero,
add_comm := nat.add_comm,
mul := nat.mul,
mul_assoc := nat.mul_assoc,
one := nat.succ nat.zero,
one_mul := nat.one_mul,
mul_one := nat.mul_one,
left_distrib := nat.left_distrib,
right_distrib := nat.right_distrib,
zero_mul := nat.zero_mul,
mul_zero := nat.mul_zero,
mul_comm := nat.mul_comm,
nat_cast := id,
nat_cast_zero := rfl,
nat_cast_succ := λ n, rfl,
nsmul := λ m n, m * n,
nsmul_zero' := nat.zero_mul,
nsmul_succ' := λ n x,
by rw [nat.succ_eq_add_one, nat.add_comm, nat.right_distrib, nat.one_mul] }
instance : linear_ordered_semiring nat :=
{ add_left_cancel := @nat.add_left_cancel,
lt := nat.lt,
add_le_add_left := @nat.add_le_add_left,
le_of_add_le_add_left := @nat.le_of_add_le_add_left,
zero_le_one := nat.le_of_lt (nat.zero_lt_succ 0),
mul_lt_mul_of_pos_left := @nat.mul_lt_mul_of_pos_left,
mul_lt_mul_of_pos_right := @nat.mul_lt_mul_of_pos_right,
decidable_eq := nat.decidable_eq,
exists_pair_ne := ⟨0, 1, ne_of_lt nat.zero_lt_one⟩,
..nat.comm_semiring, ..nat.linear_order }
-- all the fields are already included in the linear_ordered_semiring instance
instance : linear_ordered_cancel_add_comm_monoid ℕ :=
{ add_left_cancel := @nat.add_left_cancel,
..nat.linear_ordered_semiring }
instance : linear_ordered_comm_monoid_with_zero ℕ :=
{ mul_le_mul_left := λ a b h c, nat.mul_le_mul_left c h,
..nat.linear_ordered_semiring,
..(infer_instance : comm_monoid_with_zero ℕ)}
instance : ordered_comm_semiring ℕ := { .. nat.comm_semiring, .. nat.linear_ordered_semiring }
/-! Extra instances to short-circuit type class resolution -/
instance : add_comm_monoid nat := by apply_instance
instance : add_monoid nat := by apply_instance
instance : monoid nat := by apply_instance
instance : comm_monoid nat := by apply_instance
instance : comm_semigroup nat := by apply_instance
instance : semigroup nat := by apply_instance
instance : add_comm_semigroup nat := by apply_instance
instance : add_semigroup nat := by apply_instance
instance : distrib nat := by apply_instance
instance : semiring nat := by apply_instance
instance : ordered_semiring nat := by apply_instance
instance nat.order_bot : order_bot ℕ :=
{ bot := 0, bot_le := nat.zero_le }
instance : canonically_ordered_comm_semiring ℕ :=
{ exists_add_of_le := λ a b h, (nat.le.dest h).imp $ λ _, eq.symm,
le_self_add := nat.le_add_right,
eq_zero_or_eq_zero_of_mul_eq_zero := λ a b, nat.eq_zero_of_mul_eq_zero,
.. nat.nontrivial,
.. nat.order_bot,
.. (infer_instance : ordered_add_comm_monoid ℕ),
.. (infer_instance : linear_ordered_semiring ℕ),
.. (infer_instance : comm_semiring ℕ) }
instance : canonically_linear_ordered_add_monoid ℕ :=
{ .. (infer_instance : canonically_ordered_add_monoid ℕ),
.. nat.linear_order }
instance nat.subtype.order_bot (s : set ℕ) [decidable_pred (∈ s)] [h : nonempty s] :
order_bot s :=
{ bot := ⟨nat.find (nonempty_subtype.1 h), nat.find_spec (nonempty_subtype.1 h)⟩,
bot_le := λ x, nat.find_min' _ x.2 }
instance nat.subtype.semilattice_sup (s : set ℕ) :
semilattice_sup s :=
{ ..subtype.linear_order s,
..linear_order.to_lattice }
lemma nat.subtype.coe_bot {s : set ℕ} [decidable_pred (∈ s)]
[h : nonempty s] : ((⊥ : s) : ℕ) = nat.find (nonempty_subtype.1 h) := rfl
protected lemma nat.nsmul_eq_mul (m n : ℕ) : m • n = m * n := rfl
theorem nat.eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : 0 < m) (H : n * m = k * m) : n = k :=
by rw [mul_comm n m, mul_comm k m] at H; exact nat.eq_of_mul_eq_mul_left Hm H
instance nat.cancel_comm_monoid_with_zero : cancel_comm_monoid_with_zero ℕ :=
{ mul_left_cancel_of_ne_zero :=
λ _ _ _ h1 h2, nat.eq_of_mul_eq_mul_left (nat.pos_of_ne_zero h1) h2,
mul_right_cancel_of_ne_zero :=
λ _ _ _ h1 h2, nat.eq_of_mul_eq_mul_right (nat.pos_of_ne_zero h1) h2,
.. (infer_instance : comm_monoid_with_zero ℕ) }
attribute [simp] nat.not_lt_zero nat.succ_ne_zero nat.succ_ne_self
nat.zero_ne_one nat.one_ne_zero
nat.zero_ne_bit1 nat.bit1_ne_zero
nat.bit0_ne_one nat.one_ne_bit0
nat.bit0_ne_bit1 nat.bit1_ne_bit0
/-!
Inject some simple facts into the type class system.
This `fact` should not be confused with the factorial function `nat.fact`!
-/
section facts
instance succ_pos'' (n : ℕ) : fact (0 < n.succ) := ⟨n.succ_pos⟩
instance pos_of_one_lt (n : ℕ) [h : fact (1 < n)] : fact (0 < n) :=
⟨lt_trans zero_lt_one h.1⟩
end facts
variables {m n k : ℕ}
namespace nat
/-!
### Recursion and `set.range`
-/
section set
open set
theorem zero_union_range_succ : {0} ∪ range succ = univ :=
by { ext n, cases n; simp }
variables {α : Type*}
theorem range_of_succ (f : ℕ → α) : {f 0} ∪ range (f ∘ succ) = range f :=
by rw [← image_singleton, range_comp, ← image_union, zero_union_range_succ, image_univ]
theorem range_rec {α : Type*} (x : α) (f : ℕ → α → α) :
(set.range (λ n, nat.rec x f n) : set α) =
{x} ∪ set.range (λ n, nat.rec (f 0 x) (f ∘ succ) n) :=
begin
convert (range_of_succ _).symm,
ext n,
induction n with n ihn,
{ refl },
{ dsimp at ihn ⊢,
rw ihn }
end
theorem range_cases_on {α : Type*} (x : α) (f : ℕ → α) :
(set.range (λ n, nat.cases_on n x f) : set α) = {x} ∪ set.range f :=
(range_of_succ _).symm
end set
/-! ### The units of the natural numbers as a `monoid` and `add_monoid` -/
theorem units_eq_one (u : ℕˣ) : u = 1 :=
units.ext $ nat.eq_one_of_dvd_one ⟨u.inv, u.val_inv.symm⟩
theorem add_units_eq_zero (u : add_units ℕ) : u = 0 :=
add_units.ext $ (nat.eq_zero_of_add_eq_zero u.val_neg).1
@[simp] protected theorem is_unit_iff {n : ℕ} : is_unit n ↔ n = 1 :=
iff.intro
(λ ⟨u, hu⟩, match n, u, hu, nat.units_eq_one u with _, _, rfl, rfl := rfl end)
(λ h, h.symm ▸ ⟨1, rfl⟩)
instance unique_units : unique ℕˣ :=
{ default := 1, uniq := nat.units_eq_one }
instance unique_add_units : unique (add_units ℕ) :=
{ default := 0, uniq := nat.add_units_eq_zero }
/-! ### Equalities and inequalities involving zero and one -/
lemma one_le_iff_ne_zero {n : ℕ} : 1 ≤ n ↔ n ≠ 0 :=
(show 1 ≤ n ↔ 0 < n, from iff.rfl).trans pos_iff_ne_zero
lemma one_lt_iff_ne_zero_and_ne_one : ∀ {n : ℕ}, 1 < n ↔ n ≠ 0 ∧ n ≠ 1
| 0 := dec_trivial
| 1 := dec_trivial
| (n+2) := dec_trivial
protected theorem mul_ne_zero {n m : ℕ} (n0 : n ≠ 0) (m0 : m ≠ 0) : n * m ≠ 0
| nm := (eq_zero_of_mul_eq_zero nm).elim n0 m0
@[simp] protected theorem mul_eq_zero {a b : ℕ} : a * b = 0 ↔ a = 0 ∨ b = 0 :=
iff.intro eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt})
@[simp] protected theorem zero_eq_mul {a b : ℕ} : 0 = a * b ↔ a = 0 ∨ b = 0 :=
by rw [eq_comm, nat.mul_eq_zero]
lemma eq_zero_of_double_le {a : ℕ} (h : 2 * a ≤ a) : a = 0 :=
add_right_eq_self.mp $ le_antisymm ((two_mul a).symm.trans_le h) le_add_self
lemma eq_zero_of_mul_le {a b : ℕ} (hb : 2 ≤ b) (h : b * a ≤ a) : a = 0 :=
eq_zero_of_double_le $ le_trans (nat.mul_le_mul_right _ hb) h
theorem le_zero_iff {i : ℕ} : i ≤ 0 ↔ i = 0 :=
⟨nat.eq_zero_of_le_zero, λ h, h ▸ le_refl i⟩
lemma zero_max {m : ℕ} : max 0 m = m :=
max_eq_right (zero_le _)
@[simp] lemma min_eq_zero_iff {m n : ℕ} : min m n = 0 ↔ m = 0 ∨ n = 0 :=
begin
split,
{ intro h,
cases le_total n m with H H,
{ simpa [H] using or.inr h },
{ simpa [H] using or.inl h } },
{ rintro (rfl|rfl);
simp }
end
@[simp] lemma max_eq_zero_iff {m n : ℕ} : max m n = 0 ↔ m = 0 ∧ n = 0 :=
begin
split,
{ intro h,
cases le_total n m with H H,
{ simp only [H, max_eq_left] at h,
exact ⟨h, le_antisymm (H.trans h.le) (zero_le _)⟩ },
{ simp only [H, max_eq_right] at h,
exact ⟨le_antisymm (H.trans h.le) (zero_le _), h⟩ } },
{ rintro ⟨rfl, rfl⟩,
simp }
end
lemma add_eq_max_iff {n m : ℕ} :
n + m = max n m ↔ n = 0 ∨ m = 0 :=
begin
rw ←min_eq_zero_iff,
cases le_total n m with H H;
simp [H]
end
lemma add_eq_min_iff {n m : ℕ} :
n + m = min n m ↔ n = 0 ∧ m = 0 :=
begin
rw ←max_eq_zero_iff,
cases le_total n m with H H;
simp [H]
end
lemma one_le_of_lt {n m : ℕ} (h : n < m) : 1 ≤ m :=
lt_of_le_of_lt (nat.zero_le _) h
theorem eq_one_of_mul_eq_one_right {m n : ℕ} (H : m * n = 1) : m = 1 :=
eq_one_of_dvd_one ⟨n, H.symm⟩
theorem eq_one_of_mul_eq_one_left {m n : ℕ} (H : m * n = 1) : n = 1 :=
eq_one_of_mul_eq_one_right (by rwa mul_comm)
/-! ### `succ` -/
lemma _root_.has_lt.lt.nat_succ_le {n m : ℕ} (h : n < m) : succ n ≤ m := succ_le_of_lt h
lemma succ_eq_one_add (n : ℕ) : n.succ = 1 + n :=
by rw [nat.succ_eq_add_one, nat.add_comm]
theorem eq_of_lt_succ_of_not_lt {a b : ℕ} (h1 : a < b + 1) (h2 : ¬ a < b) : a = b :=
have h3 : a ≤ b, from le_of_lt_succ h1,
or.elim (eq_or_lt_of_not_lt h2) (λ h, h) (λ h, absurd h (not_lt_of_ge h3))
lemma eq_of_le_of_lt_succ {n m : ℕ} (h₁ : n ≤ m) (h₂ : m < n + 1) : m = n :=
nat.le_antisymm (le_of_succ_le_succ h₂) h₁
theorem one_add (n : ℕ) : 1 + n = succ n := by simp [add_comm]
@[simp] lemma succ_pos' {n : ℕ} : 0 < succ n := succ_pos n
theorem succ_inj' {n m : ℕ} : succ n = succ m ↔ n = m :=
⟨succ.inj, congr_arg _⟩
theorem succ_injective : function.injective nat.succ := λ x y, succ.inj
lemma succ_ne_succ {n m : ℕ} : succ n ≠ succ m ↔ n ≠ m :=
succ_injective.ne_iff
@[simp] lemma succ_succ_ne_one (n : ℕ) : n.succ.succ ≠ 1 :=
succ_ne_succ.mpr n.succ_ne_zero
@[simp] lemma one_lt_succ_succ (n : ℕ) : 1 < n.succ.succ :=
succ_lt_succ $ succ_pos n
lemma two_le_iff : ∀ n, 2 ≤ n ↔ n ≠ 0 ∧ n ≠ 1
| 0 := by simp
| 1 := by simp
| (n+2) := by simp
theorem succ_le_succ_iff {m n : ℕ} : succ m ≤ succ n ↔ m ≤ n :=
⟨le_of_succ_le_succ, succ_le_succ⟩
theorem max_succ_succ {m n : ℕ} :
max (succ m) (succ n) = succ (max m n) :=
begin
by_cases h1 : m ≤ n,
rw [max_eq_right h1, max_eq_right (succ_le_succ h1)],
{ rw not_le at h1, have h2 := le_of_lt h1,
rw [max_eq_left h2, max_eq_left (succ_le_succ h2)] }
end
lemma not_succ_lt_self {n : ℕ} : ¬succ n < n :=
not_lt_of_ge (nat.le_succ _)
theorem lt_succ_iff {m n : ℕ} : m < succ n ↔ m ≤ n :=
⟨le_of_lt_succ, lt_succ_of_le⟩
lemma succ_le_iff {m n : ℕ} : succ m ≤ n ↔ m < n :=
⟨lt_of_succ_le, succ_le_of_lt⟩
lemma lt_iff_add_one_le {m n : ℕ} : m < n ↔ m + 1 ≤ n :=
by rw succ_le_iff
-- Just a restatement of `nat.lt_succ_iff` using `+1`.
lemma lt_add_one_iff {a b : ℕ} : a < b + 1 ↔ a ≤ b :=
lt_succ_iff
-- A flipped version of `lt_add_one_iff`.
lemma lt_one_add_iff {a b : ℕ} : a < 1 + b ↔ a ≤ b :=
by simp only [add_comm, lt_succ_iff]
-- This is true reflexively, by the definition of `≤` on ℕ,
-- but it's still useful to have, to convince Lean to change the syntactic type.
lemma add_one_le_iff {a b : ℕ} : a + 1 ≤ b ↔ a < b :=
iff.refl _
lemma one_add_le_iff {a b : ℕ} : 1 + a ≤ b ↔ a < b :=
by simp only [add_comm, add_one_le_iff]
theorem of_le_succ {n m : ℕ} (H : n ≤ m.succ) : n ≤ m ∨ n = m.succ :=
H.lt_or_eq_dec.imp le_of_lt_succ id
lemma succ_lt_succ_iff {m n : ℕ} : succ m < succ n ↔ m < n :=
⟨lt_of_succ_lt_succ, succ_lt_succ⟩
@[simp] lemma lt_one_iff {n : ℕ} : n < 1 ↔ n = 0 :=
lt_succ_iff.trans le_zero_iff
lemma div_le_iff_le_mul_add_pred {m n k : ℕ} (n0 : 0 < n) : m / n ≤ k ↔ m ≤ n * k + (n - 1) :=
begin
rw [← lt_succ_iff, div_lt_iff_lt_mul n0, succ_mul, mul_comm],
cases n, {cases n0},
exact lt_succ_iff,
end
lemma two_lt_of_ne : ∀ {n}, n ≠ 0 → n ≠ 1 → n ≠ 2 → 2 < n
| 0 h _ _ := (h rfl).elim
| 1 _ h _ := (h rfl).elim
| 2 _ _ h := (h rfl).elim
| (n+3) _ _ _ := dec_trivial
theorem forall_lt_succ {P : ℕ → Prop} {n : ℕ} : (∀ m < n.succ, P m) ↔ (∀ m < n, P m) ∧ P n :=
⟨λ H, ⟨λ m hm, H m (lt_succ_iff.2 hm.le), H n (lt_succ_self n)⟩, begin
rintro ⟨H, hn⟩ m hm,
rcases eq_or_lt_of_le (lt_succ_iff.1 hm) with rfl | hmn,
{ exact hn },
{ exact H m hmn }
end⟩
theorem exists_lt_succ {P : ℕ → Prop} {n : ℕ} : (∃ m < n.succ, P m) ↔ (∃ m < n, P m) ∨ P n :=
by { rw ←not_iff_not, push_neg, exact forall_lt_succ }
/-! ### `add` -/
-- Sometimes a bare `nat.add` or similar appears as a consequence of unfolding
-- during pattern matching. These lemmas package them back up as typeclass
-- mediated operations.
@[simp] theorem add_def {a b : ℕ} : nat.add a b = a + b := rfl
@[simp] theorem mul_def {a b : ℕ} : nat.mul a b = a * b := rfl
lemma exists_eq_add_of_le : ∀ {m n : ℕ}, m ≤ n → ∃ k : ℕ, n = m + k
| 0 0 h := ⟨0, by simp⟩
| 0 (n+1) h := ⟨n+1, by simp⟩
| (m+1) (n+1) h :=
let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in
⟨k, by simp [hk, add_comm, add_left_comm]⟩
lemma exists_eq_add_of_lt : ∀ {m n : ℕ}, m < n → ∃ k : ℕ, n = m + k + 1
| 0 0 h := false.elim $ lt_irrefl _ h
| 0 (n+1) h := ⟨n, by simp⟩
| (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in
⟨k, by simp [hk]⟩
theorem add_pos_left {m : ℕ} (h : 0 < m) (n : ℕ) : 0 < m + n :=
calc
m + n > 0 + n : nat.add_lt_add_right h n
... = n : nat.zero_add n
... ≥ 0 : zero_le n
theorem add_pos_right (m : ℕ) {n : ℕ} (h : 0 < n) : 0 < m + n :=
begin rw add_comm, exact add_pos_left h m end
theorem add_pos_iff_pos_or_pos (m n : ℕ) : 0 < m + n ↔ 0 < m ∨ 0 < n :=
iff.intro
begin
intro h,
cases m with m,
{simp [zero_add] at h, exact or.inr h},
exact or.inl (succ_pos _)
end
begin
intro h, cases h with mpos npos,
{ apply add_pos_left mpos },
apply add_pos_right _ npos
end
lemma add_eq_one_iff : ∀ {a b : ℕ}, a + b = 1 ↔ (a = 0 ∧ b = 1) ∨ (a = 1 ∧ b = 0)
| 0 0 := dec_trivial
| 1 0 := dec_trivial
| (a+2) _ := by rw add_right_comm; exact dec_trivial
| _ (b+1) := by rw [← add_assoc]; simp only [nat.succ_inj', nat.succ_ne_zero]; simp
theorem le_add_one_iff {i j : ℕ} : i ≤ j + 1 ↔ (i ≤ j ∨ i = j + 1) :=
⟨λ h,
match nat.eq_or_lt_of_le h with
| or.inl h := or.inr h
| or.inr h := or.inl $ nat.le_of_succ_le_succ h
end,
or.rec (λ h, le_trans h $ nat.le_add_right _ _) le_of_eq⟩
lemma le_and_le_add_one_iff {x a : ℕ} :
a ≤ x ∧ x ≤ a + 1 ↔ x = a ∨ x = a + 1 :=
begin
rw [le_add_one_iff, and_or_distrib_left, ←le_antisymm_iff, eq_comm, and_iff_right_of_imp],
rintro rfl,
exact a.le_succ,
end
lemma add_succ_lt_add {a b c d : ℕ} (hab : a < b) (hcd : c < d) : a + c + 1 < b + d :=
begin
rw add_assoc,
exact add_lt_add_of_lt_of_le hab (nat.succ_le_iff.2 hcd)
end
/-! ### `pred` -/
@[simp]
lemma add_succ_sub_one (n m : ℕ) : (n + succ m) - 1 = n + m :=
by rw [add_succ, succ_sub_one]
@[simp]
lemma succ_add_sub_one (n m : ℕ) : (succ n + m) - 1 = n + m :=
by rw [succ_add, succ_sub_one]
lemma pred_eq_sub_one (n : ℕ) : pred n = n - 1 := rfl
theorem pred_eq_of_eq_succ {m n : ℕ} (H : m = n.succ) : m.pred = n := by simp [H]
@[simp] lemma pred_eq_succ_iff {n m : ℕ} : pred n = succ m ↔ n = m + 2 :=
by cases n; split; rintro ⟨⟩; refl
theorem pred_sub (n m : ℕ) : pred n - m = pred (n - m) :=
by rw [← nat.sub_one, nat.sub_sub, one_add, sub_succ]
lemma le_pred_of_lt {n m : ℕ} (h : m < n) : m ≤ n - 1 :=
nat.sub_le_sub_right h 1
lemma le_of_pred_lt {m n : ℕ} : pred m < n → m ≤ n :=
match m with
| 0 := le_of_lt
| m+1 := id
end
/-- This ensures that `simp` succeeds on `pred (n + 1) = n`. -/
@[simp] lemma pred_one_add (n : ℕ) : pred (1 + n) = n :=
by rw [add_comm, add_one, pred_succ]
lemma pred_le_iff {n m : ℕ} : pred n ≤ m ↔ n ≤ succ m :=
⟨le_succ_of_pred_le, by { cases n, { exact λ h, zero_le m }, exact le_of_succ_le_succ }⟩
/-! ### `sub`
Most lemmas come from the `has_ordered_sub` instance on `ℕ`. -/
instance : has_ordered_sub ℕ :=
begin
constructor,
intros m n k,
induction n with n ih generalizing k,
{ simp },
{ simp only [sub_succ, add_succ, succ_add, ih, pred_le_iff] }
end
lemma lt_pred_iff {n m : ℕ} : n < pred m ↔ succ n < m :=
show n < m - 1 ↔ n + 1 < m, from lt_tsub_iff_right
lemma lt_of_lt_pred {a b : ℕ} (h : a < b - 1) : a < b :=
lt_of_succ_lt (lt_pred_iff.1 h)
lemma le_or_le_of_add_eq_add_pred {a b c d : ℕ} (h : c + d = a + b - 1) : a ≤ c ∨ b ≤ d :=
begin
cases le_or_lt a c with h' h'; [left, right],
{ exact h', },
{ replace h' := add_lt_add_right h' d, rw h at h',
cases b.eq_zero_or_pos with hb hb, { rw hb, exact zero_le d, },
rw [a.add_sub_assoc hb, add_lt_add_iff_left] at h',
exact nat.le_of_pred_lt h', },
end
/-- A version of `nat.sub_succ` in the form `_ - 1` instead of `nat.pred _`. -/
lemma sub_succ' (a b : ℕ) : a - b.succ = a - b - 1 := rfl
/-! ### `mul` -/
lemma succ_mul_pos (m : ℕ) (hn : 0 < n) : 0 < (succ m) * n :=
mul_pos (succ_pos m) hn
theorem mul_self_le_mul_self {n m : ℕ} (h : n ≤ m) : n * n ≤ m * m :=
decidable.mul_le_mul h h (zero_le _) (zero_le _)
theorem mul_self_lt_mul_self : Π {n m : ℕ}, n < m → n * n < m * m
| 0 m h := mul_pos h h
| (succ n) m h := decidable.mul_lt_mul h (le_of_lt h) (succ_pos _) (zero_le _)
theorem mul_self_le_mul_self_iff {n m : ℕ} : n ≤ m ↔ n * n ≤ m * m :=
⟨mul_self_le_mul_self, le_imp_le_of_lt_imp_lt mul_self_lt_mul_self⟩
theorem mul_self_lt_mul_self_iff {n m : ℕ} : n < m ↔ n * n < m * m :=
le_iff_le_iff_lt_iff_lt.1 mul_self_le_mul_self_iff
theorem le_mul_self : Π (n : ℕ), n ≤ n * n
| 0 := le_rfl
| (n+1) := let t := nat.mul_le_mul_left (n+1) (succ_pos n) in by simp at t; exact t
lemma le_mul_of_pos_left {m n : ℕ} (h : 0 < n) : m ≤ n * m :=
begin
conv {to_lhs, rw [← one_mul(m)]},
exact decidable.mul_le_mul_of_nonneg_right h.nat_succ_le dec_trivial,
end
lemma le_mul_of_pos_right {m n : ℕ} (h : 0 < n) : m ≤ m * n :=
begin
conv {to_lhs, rw [← mul_one(m)]},
exact decidable.mul_le_mul_of_nonneg_left h.nat_succ_le dec_trivial,
end
theorem two_mul_ne_two_mul_add_one {n m} : 2 * n ≠ 2 * m + 1 :=
mt (congr_arg (%2)) (by { rw [add_comm, add_mul_mod_self_left, mul_mod_right, mod_eq_of_lt]; simp })
lemma mul_eq_one_iff : ∀ {a b : ℕ}, a * b = 1 ↔ a = 1 ∧ b = 1
| 0 0 := dec_trivial
| 0 1 := dec_trivial
| 1 0 := dec_trivial
| (a+2) 0 := by simp
| 0 (b+2) := by simp
| (a+1) (b+1) := ⟨
λ h, by simp only [add_mul, mul_add, mul_add, one_mul, mul_one,
(add_assoc _ _ _).symm, nat.succ_inj', add_eq_zero_iff] at h; simp [h.1.2, h.2],
λ h, by simp only [h, mul_one]⟩
protected theorem mul_left_inj {a b c : ℕ} (ha : 0 < a) : b * a = c * a ↔ b = c :=
⟨nat.eq_of_mul_eq_mul_right ha, λ e, e ▸ rfl⟩
protected theorem mul_right_inj {a b c : ℕ} (ha : 0 < a) : a * b = a * c ↔ b = c :=
⟨nat.eq_of_mul_eq_mul_left ha, λ e, e ▸ rfl⟩
lemma mul_left_injective {a : ℕ} (ha : 0 < a) : function.injective (λ x, x * a) :=
λ _ _, eq_of_mul_eq_mul_right ha
lemma mul_right_injective {a : ℕ} (ha : 0 < a) : function.injective (λ x, a * x) :=
λ _ _, nat.eq_of_mul_eq_mul_left ha
lemma mul_ne_mul_left {a b c : ℕ} (ha : 0 < a) : b * a ≠ c * a ↔ b ≠ c :=
(mul_left_injective ha).ne_iff
lemma mul_ne_mul_right {a b c : ℕ} (ha : 0 < a) : a * b ≠ a * c ↔ b ≠ c :=
(mul_right_injective ha).ne_iff
lemma mul_right_eq_self_iff {a b : ℕ} (ha : 0 < a) : a * b = a ↔ b = 1 :=
suffices a * b = a * 1 ↔ b = 1, by rwa mul_one at this,
nat.mul_right_inj ha
lemma mul_left_eq_self_iff {a b : ℕ} (hb : 0 < b) : a * b = b ↔ a = 1 :=
by rw [mul_comm, nat.mul_right_eq_self_iff hb]
lemma lt_succ_iff_lt_or_eq {n i : ℕ} : n < i.succ ↔ (n < i ∨ n = i) :=
lt_succ_iff.trans decidable.le_iff_lt_or_eq
theorem mul_self_inj {n m : ℕ} : n * n = m * m ↔ n = m :=
le_antisymm_iff.trans (le_antisymm_iff.trans
(and_congr mul_self_le_mul_self_iff mul_self_le_mul_self_iff)).symm
lemma le_add_pred_of_pos (n : ℕ) {i : ℕ} (hi : i ≠ 0) : n ≤ i + (n - 1) :=
begin
refine le_trans _ (add_tsub_le_assoc),
simp [add_comm, nat.add_sub_assoc, one_le_iff_ne_zero.2 hi]
end
/-!
### Recursion and induction principles
This section is here due to dependencies -- the lemmas here require some of the lemmas
proved above, and some of the results in later sections depend on the definitions in this section.
-/
@[simp] lemma rec_zero {C : ℕ → Sort u} (h0 : C 0) (h : ∀ n, C n → C (n + 1)) :
(nat.rec h0 h : Π n, C n) 0 = h0 :=
rfl
@[simp] lemma rec_add_one {C : ℕ → Sort u} (h0 : C 0) (h : ∀ n, C n → C (n + 1)) (n : ℕ) :
(nat.rec h0 h : Π n, C n) (n + 1) = h n ((nat.rec h0 h : Π n, C n) n) :=
rfl
/-- Recursion starting at a non-zero number: given a map `C k → C (k+1)` for each `k`,
there is a map from `C n` to each `C m`, `n ≤ m`. For a version where the assumption is only made
when `k ≥ n`, see `le_rec_on'`. -/
@[elab_as_eliminator]
def le_rec_on {C : ℕ → Sort u} {n : ℕ} : Π {m : ℕ}, n ≤ m → (Π {k}, C k → C (k+1)) → C n → C m
| 0 H next x := eq.rec_on (nat.eq_zero_of_le_zero H) x
| (m+1) H next x := or.by_cases (of_le_succ H) (λ h : n ≤ m, next $ le_rec_on h @next x)
(λ h : n = m + 1, eq.rec_on h x)
theorem le_rec_on_self {C : ℕ → Sort u} {n} {h : n ≤ n} {next} (x : C n) :
(le_rec_on h next x : C n) = x :=
by cases n; unfold le_rec_on or.by_cases; rw [dif_neg n.not_succ_le_self, dif_pos rfl]
theorem le_rec_on_succ {C : ℕ → Sort u} {n m} (h1 : n ≤ m) {h2 : n ≤ m+1} {next} (x : C n) :
(le_rec_on h2 @next x : C (m+1)) = next (le_rec_on h1 @next x : C m) :=
by conv { to_lhs, rw [le_rec_on, or.by_cases, dif_pos h1] }
theorem le_rec_on_succ' {C : ℕ → Sort u} {n} {h : n ≤ n+1} {next} (x : C n) :
(le_rec_on h next x : C (n+1)) = next x :=
by rw [le_rec_on_succ (le_refl n), le_rec_on_self]
theorem le_rec_on_trans {C : ℕ → Sort u} {n m k} (hnm : n ≤ m) (hmk : m ≤ k) {next} (x : C n) :
(le_rec_on (le_trans hnm hmk) @next x : C k) = le_rec_on hmk @next (le_rec_on hnm @next x) :=
begin
induction hmk with k hmk ih, { rw le_rec_on_self },
rw [le_rec_on_succ (le_trans hnm hmk), ih, le_rec_on_succ]
end
theorem le_rec_on_succ_left {C : ℕ → Sort u} {n m} (h1 : n ≤ m) (h2 : n+1 ≤ m)
{next : Π{{k}}, C k → C (k+1)} (x : C n) :
(le_rec_on h2 next (next x) : C m) = (le_rec_on h1 next x : C m) :=
begin
rw [subsingleton.elim h1 (le_trans (le_succ n) h2),
le_rec_on_trans (le_succ n) h2, le_rec_on_succ']
end
theorem le_rec_on_injective {C : ℕ → Sort u} {n m} (hnm : n ≤ m)
(next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.injective (next n)) :
function.injective (le_rec_on hnm next) :=
begin
induction hnm with m hnm ih, { intros x y H, rwa [le_rec_on_self, le_rec_on_self] at H },
intros x y H, rw [le_rec_on_succ hnm, le_rec_on_succ hnm] at H, exact ih (Hnext _ H)
end
theorem le_rec_on_surjective {C : ℕ → Sort u} {n m} (hnm : n ≤ m)
(next : Π n, C n → C (n+1)) (Hnext : ∀ n, function.surjective (next n)) :
function.surjective (le_rec_on hnm next) :=
begin
induction hnm with m hnm ih, { intros x, use x, rw le_rec_on_self },
intros x, rcases Hnext _ x with ⟨w, rfl⟩, rcases ih w with ⟨x, rfl⟩, use x, rw le_rec_on_succ
end
/-- Recursion principle based on `<`. -/
@[elab_as_eliminator]
protected def strong_rec' {p : ℕ → Sort u} (H : ∀ n, (∀ m, m < n → p m) → p n) : ∀ (n : ℕ), p n
| n := H n (λ m hm, strong_rec' m)
/-- Recursion principle based on `<` applied to some natural number. -/
@[elab_as_eliminator]
def strong_rec_on' {P : ℕ → Sort*} (n : ℕ) (h : ∀ n, (∀ m, m < n → P m) → P n) : P n :=
nat.strong_rec' h n
theorem strong_rec_on_beta' {P : ℕ → Sort*} {h} {n : ℕ} :
(strong_rec_on' n h : P n) = h n (λ m hmn, (strong_rec_on' m h : P m)) :=
by { simp only [strong_rec_on'], rw nat.strong_rec' }
/-- Induction principle starting at a non-zero number. For maps to a `Sort*` see `le_rec_on`. -/
@[elab_as_eliminator] lemma le_induction {P : nat → Prop} {m}
(h0 : P m) (h1 : ∀ n, m ≤ n → P n → P (n + 1)) :
∀ n, m ≤ n → P n :=
by apply nat.less_than_or_equal.rec h0; exact h1
/-- Decreasing induction: if `P (k+1)` implies `P k`, then `P n` implies `P m` for all `m ≤ n`.
Also works for functions to `Sort*`. For a version assuming only the assumption for `k < n`, see
`decreasing_induction'`. -/
@[elab_as_eliminator]
def decreasing_induction {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (mn : m ≤ n)
(hP : P n) : P m :=
le_rec_on mn (λ k ih hsk, ih $ h k hsk) (λ h, h) hP
@[simp] lemma decreasing_induction_self {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {n : ℕ}
(nn : n ≤ n) (hP : P n) : (decreasing_induction h nn hP : P n) = hP :=
by { dunfold decreasing_induction, rw [le_rec_on_self] }
lemma decreasing_induction_succ {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ} (mn : m ≤ n)
(msn : m ≤ n + 1) (hP : P (n+1)) :
(decreasing_induction h msn hP : P m) = decreasing_induction h mn (h n hP) :=
by { dunfold decreasing_induction, rw [le_rec_on_succ] }
@[simp] lemma decreasing_induction_succ' {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m : ℕ}
(msm : m ≤ m + 1) (hP : P (m+1)) : (decreasing_induction h msm hP : P m) = h m hP :=
by { dunfold decreasing_induction, rw [le_rec_on_succ'] }
lemma decreasing_induction_trans {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n k : ℕ}
(mn : m ≤ n) (nk : n ≤ k) (hP : P k) :
(decreasing_induction h (le_trans mn nk) hP : P m) =
decreasing_induction h mn (decreasing_induction h nk hP) :=
by { induction nk with k nk ih, rw [decreasing_induction_self],
rw [decreasing_induction_succ h (le_trans mn nk), ih, decreasing_induction_succ] }
lemma decreasing_induction_succ_left {P : ℕ → Sort*} (h : ∀n, P (n+1) → P n) {m n : ℕ}
(smn : m + 1 ≤ n) (mn : m ≤ n) (hP : P n) :
(decreasing_induction h mn hP : P m) = h m (decreasing_induction h smn hP) :=
by { rw [subsingleton.elim mn (le_trans (le_succ m) smn), decreasing_induction_trans,
decreasing_induction_succ'] }
/-- Given a predicate on two naturals `P : ℕ → ℕ → Prop`, `P a b` is true for all `a < b` if
`P (a + 1) (a + 1)` is true for all `a`, `P 0 (b + 1)` is true for all `b` and for all
`a < b`, `P (a + 1) b` is true and `P a (b + 1)` is true implies `P (a + 1) (b + 1)` is true. -/
@[elab_as_eliminator]
lemma diag_induction (P : ℕ → ℕ → Prop) (ha : ∀ a, P (a + 1) (a + 1)) (hb : ∀ b, P 0 (b + 1))
(hd : ∀ a b, a < b → P (a + 1) b → P a (b + 1) → P (a + 1) (b + 1)) :
∀ a b, a < b → P a b
| 0 (b + 1) h := hb _
| (a + 1) (b + 1) h :=
begin
apply hd _ _ ((add_lt_add_iff_right _).1 h),
{ have : a + 1 = b ∨ a + 1 < b,
{ rwa [← le_iff_eq_or_lt, ← nat.lt_succ_iff] },
rcases this with rfl | _,
{ exact ha _ },
apply diag_induction (a + 1) b this },
apply diag_induction a (b + 1),
apply lt_of_le_of_lt (nat.le_succ _) h,
end
using_well_founded { rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ p, p.1 + p.2.1)⟩] }
/-- Given `P : ℕ → ℕ → Sort*`, if for all `a b : ℕ` we can extend `P` from the rectangle
strictly below `(a,b)` to `P a b`, then we have `P n m` for all `n m : ℕ`.
Note that for non-`Prop` output it is preferable to use the equation compiler directly if possible,
since this produces equation lemmas. -/
@[elab_as_eliminator]
def strong_sub_recursion {P : ℕ → ℕ → Sort*}
(H : ∀ a b, (∀ x y, x < a → y < b → P x y) → P a b) : Π (n m : ℕ), P n m
| n m := H n m (λ x y hx hy, strong_sub_recursion x y)
/-- Given `P : ℕ → ℕ → Sort*`, if we have `P i 0` and `P 0 i` for all `i : ℕ`,
and for any `x y : ℕ` we can extend `P` from `(x,y+1)` and `(x+1,y)` to `(x+1,y+1)`
then we have `P n m` for all `n m : ℕ`.
Note that for non-`Prop` output it is preferable to use the equation compiler directly if possible,
since this produces equation lemmas. -/
@[elab_as_eliminator]
def pincer_recursion {P : ℕ → ℕ → Sort*} (Ha0 : ∀ a : ℕ, P a 0) (H0b : ∀ b : ℕ, P 0 b)
(H : ∀ x y : ℕ, P x y.succ → P x.succ y → P x.succ y.succ) : ∀ (n m : ℕ), P n m
| a 0 := Ha0 a
| 0 b := H0b b
| (nat.succ a) (nat.succ b) := H _ _ (pincer_recursion _ _) (pincer_recursion _ _)
/-- Recursion starting at a non-zero number: given a map `C k → C (k+1)` for each `k ≥ n`,
there is a map from `C n` to each `C m`, `n ≤ m`. -/
@[elab_as_eliminator]
def le_rec_on' {C : ℕ → Sort*} {n : ℕ} :
Π {m : ℕ}, n ≤ m → (Π ⦃k⦄, n ≤ k → C k → C (k+1)) → C n → C m
| 0 H next x := eq.rec_on (nat.eq_zero_of_le_zero H) x
| (m+1) H next x := or.by_cases (of_le_succ H) (λ h : n ≤ m, next h $ le_rec_on' h next x)
(λ h : n = m + 1, eq.rec_on h x)
/-- Decreasing induction: if `P (k+1)` implies `P k` for all `m ≤ k < n`, then `P n` implies `P m`.
Also works for functions to `Sort*`. Weakens the assumptions of `decreasing_induction`. -/
@[elab_as_eliminator]
def decreasing_induction' {P : ℕ → Sort*} {m n : ℕ} (h : ∀ k < n, m ≤ k → P (k+1) → P k)
(mn : m ≤ n) (hP : P n) : P m :=
begin
-- induction mn using nat.le_rec_on' generalizing h hP -- this doesn't work unfortunately
refine le_rec_on' mn _ _ h hP; clear h hP mn n,
{ intros n mn ih h hP,
apply ih,
{ exact λ k hk, h k hk.step },
{ exact h n (lt_succ_self n) mn hP } },
{ intros h hP, exact hP }
end
/-- A subset of `ℕ` containing `b : ℕ` and closed under `nat.succ` contains every `n ≥ b`. -/
lemma set_induction_bounded {b : ℕ} {S : set ℕ} (hb : b ∈ S) (h_ind: ∀ k : ℕ, k ∈ S → k + 1 ∈ S)
{n : ℕ} (hbn : b ≤ n) : n ∈ S :=
@le_rec_on (λ n, n ∈ S) b n hbn h_ind hb
/-- A subset of `ℕ` containing zero and closed under `nat.succ` contains all of `ℕ`. -/
lemma set_induction {S : set ℕ} (hb : 0 ∈ S) (h_ind: ∀ k : ℕ, k ∈ S → k + 1 ∈ S) (n : ℕ) : n ∈ S :=
set_induction_bounded hb h_ind (zero_le n)
lemma set_eq_univ {S : set ℕ} : S = set.univ ↔ 0 ∈ S ∧ ∀ k : ℕ, k ∈ S → k + 1 ∈ S :=
⟨by rintro rfl; simp, λ ⟨h0, hs⟩, set.eq_univ_of_forall (set_induction h0 hs)⟩
/-! ### `div` -/
attribute [simp] nat.div_self
protected lemma div_le_of_le_mul' {m n : ℕ} {k} (h : m ≤ k * n) : m / k ≤ n :=
(nat.eq_zero_or_pos k).elim
(λ k0, by rw [k0, nat.div_zero]; apply zero_le)
(λ k0, (_root_.mul_le_mul_left k0).1 $
calc k * (m / k)
≤ m % k + k * (m / k) : nat.le_add_left _ _
... = m : mod_add_div _ _
... ≤ k * n : h)
protected lemma div_le_self' (m n : ℕ) : m / n ≤ m :=
(nat.eq_zero_or_pos n).elim
(λ n0, by rw [n0, nat.div_zero]; apply zero_le)
(λ n0, nat.div_le_of_le_mul' $ calc
m = 1 * m : (one_mul _).symm
... ≤ n * m : nat.mul_le_mul_right _ n0)
/-- A version of `nat.div_lt_self` using successors, rather than additional hypotheses. -/
lemma div_lt_self' (n b : ℕ) : (n+1)/(b+2) < n+1 :=
nat.div_lt_self (nat.succ_pos n) (nat.succ_lt_succ (nat.succ_pos _))
theorem le_div_iff_mul_le' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x ≤ y / k ↔ x * k ≤ y :=
le_div_iff_mul_le k0
theorem div_lt_iff_lt_mul' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x / k < y ↔ x < y * k :=
lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le' k0
lemma one_le_div_iff {a b : ℕ} (hb : 0 < b) : 1 ≤ a / b ↔ b ≤ a :=
by rw [le_div_iff_mul_le hb, one_mul]
lemma div_lt_one_iff {a b : ℕ} (hb : 0 < b) : a / b < 1 ↔ a < b :=
lt_iff_lt_of_le_iff_le $ one_le_div_iff hb
protected theorem div_le_div_right {n m : ℕ} (h : n ≤ m) {k : ℕ} : n / k ≤ m / k :=
(nat.eq_zero_or_pos k).elim (λ k0, by simp [k0]) $ λ hk,
(le_div_iff_mul_le' hk).2 $ le_trans (nat.div_mul_le_self _ _) h
lemma lt_of_div_lt_div {m n k : ℕ} : m / k < n / k → m < n :=
lt_imp_lt_of_le_imp_le $ λ h, nat.div_le_div_right h
protected lemma div_pos {a b : ℕ} (hba : b ≤ a) (hb : 0 < b) : 0 < a / b :=
nat.pos_of_ne_zero (λ h, lt_irrefl a
(calc a = a % b : by simpa [h] using (mod_add_div a b).symm
... < b : nat.mod_lt a hb
... ≤ a : hba))
protected lemma div_lt_of_lt_mul {m n k : ℕ} (h : m < n * k) : m / n < k :=
lt_of_mul_lt_mul_left
(calc n * (m / n) ≤ m % n + n * (m / n) : nat.le_add_left _ _
... = m : mod_add_div _ _
... < n * k : h)
(nat.zero_le n)
lemma lt_mul_of_div_lt {a b c : ℕ} (h : a / c < b) (w : 0 < c) : a < b * c :=
lt_of_not_ge $ not_le_of_gt h ∘ (nat.le_div_iff_mul_le w).2
protected lemma div_eq_zero_iff {a b : ℕ} (hb : 0 < b) : a / b = 0 ↔ a < b :=
⟨λ h, by rw [← mod_add_div a b, h, mul_zero, add_zero]; exact mod_lt _ hb,
λ h, by rw [← nat.mul_right_inj hb, ← @add_left_cancel_iff _ _ (a % b), mod_add_div,
mod_eq_of_lt h, mul_zero, add_zero]⟩
protected lemma div_eq_zero {a b : ℕ} (hb : a < b) : a / b = 0 :=
(nat.div_eq_zero_iff $ (zero_le a).trans_lt hb).mpr hb
lemma eq_zero_of_le_div {a b : ℕ} (hb : 2 ≤ b) (h : a ≤ a / b) : a = 0 :=
eq_zero_of_mul_le hb $
by rw mul_comm; exact (nat.le_div_iff_mul_le' (lt_of_lt_of_le dec_trivial hb)).1 h
lemma mul_div_le_mul_div_assoc (a b c : ℕ) : a * (b / c) ≤ (a * b) / c :=
if hc0 : c = 0 then by simp [hc0]
else (nat.le_div_iff_mul_le (nat.pos_of_ne_zero hc0)).2
(by rw [mul_assoc]; exact nat.mul_le_mul_left _ (nat.div_mul_le_self _ _))
lemma div_mul_div_le_div (a b c : ℕ) : ((a / c) * b) / a ≤ b / c :=
if ha0 : a = 0 then by simp [ha0]
else calc a / c * b / a ≤ b * a / c / a :
nat.div_le_div_right (by rw [mul_comm];
exact mul_div_le_mul_div_assoc _ _ _)
... = b / c : by rw [nat.div_div_eq_div_mul, mul_comm b, mul_comm c,
nat.mul_div_mul _ _ (nat.pos_of_ne_zero ha0)]
lemma eq_zero_of_le_half {a : ℕ} (h : a ≤ a / 2) : a = 0 :=
eq_zero_of_le_div le_rfl h
protected theorem eq_mul_of_div_eq_right {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) :
a = b * c :=
by rw [← H2, nat.mul_div_cancel' H1]
protected theorem div_eq_iff_eq_mul_right {a b c : ℕ} (H : 0 < b) (H' : b ∣ a) :
a / b = c ↔ a = b * c :=
⟨nat.eq_mul_of_div_eq_right H', nat.div_eq_of_eq_mul_right H⟩
protected theorem div_eq_iff_eq_mul_left {a b c : ℕ} (H : 0 < b) (H' : b ∣ a) :
a / b = c ↔ a = c * b :=
by rw mul_comm; exact nat.div_eq_iff_eq_mul_right H H'
protected theorem eq_mul_of_div_eq_left {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) :
a = c * b :=
by rw [mul_comm, nat.eq_mul_of_div_eq_right H1 H2]
protected lemma lt_div_iff_mul_lt {n d : ℕ} (hnd : d ∣ n) (a : ℕ) : a < n / d ↔ d * a < n :=
begin
rcases d.eq_zero_or_pos with rfl | hd0, { simp [zero_dvd_iff.mp hnd] },
rw [←mul_lt_mul_left hd0, ←nat.eq_mul_of_div_eq_right hnd rfl],
end
protected theorem mul_div_cancel_left' {a b : ℕ} (Hd : a ∣ b) : a * (b / a) = b :=
by rw [mul_comm,nat.div_mul_cancel Hd]
/-- Alias of `nat.mul_div_mul` -/ --TODO: Update `nat.mul_div_mul` in the core?
protected lemma mul_div_mul_left (a b : ℕ) {c : ℕ} (hc : 0 < c) : c * a / (c * b) = a / b :=
nat.mul_div_mul a b hc
protected lemma mul_div_mul_right (a b : ℕ) {c : ℕ} (hc : 0 < c) : a * c / (b * c) = a / b :=
by rw [mul_comm, mul_comm b, a.mul_div_mul_left b hc]
lemma lt_div_mul_add {a b : ℕ} (hb : 0 < b) : a < a/b*b + b :=
begin
rw [←nat.succ_mul, ←nat.div_lt_iff_lt_mul hb],
exact nat.lt_succ_self _,
end
lemma div_eq_iff_eq_of_dvd_dvd {n x y : ℕ} (hn : n ≠ 0) (hx : x ∣ n) (hy : y ∣ n) :
n / x = n / y ↔ x = y :=
begin
split,
{ intros h,
rw ←mul_right_inj' hn,
apply nat.eq_mul_of_div_eq_left (dvd_mul_of_dvd_left hy x),
rw [eq_comm, mul_comm, nat.mul_div_assoc _ hy],
exact nat.eq_mul_of_div_eq_right hx h },
{ intros h, rw h },
end
lemma mul_div_mul_comm_of_dvd_dvd {a b c d : ℕ} (hac : c ∣ a) (hbd : d ∣ b) :
a * b / (c * d) = a / c * (b / d) :=
begin
rcases c.eq_zero_or_pos with rfl | hc0, { simp },
rcases d.eq_zero_or_pos with rfl | hd0, { simp },
obtain ⟨k1, rfl⟩ := hac,
obtain ⟨k2, rfl⟩ := hbd,
rw [mul_mul_mul_comm, nat.mul_div_cancel_left _ hc0, nat.mul_div_cancel_left _ hd0,
nat.mul_div_cancel_left _ (mul_pos hc0 hd0)],
end
@[simp]
protected lemma div_left_inj {a b d : ℕ} (hda : d ∣ a) (hdb : d ∣ b) : a / d = b / d ↔ a = b :=
begin
refine ⟨λ h, _, congr_arg _⟩,
rw [←nat.mul_div_cancel' hda, ←nat.mul_div_cancel' hdb, h],
end
/-! ### `mod`, `dvd` -/
lemma div_add_mod (m k : ℕ) : k * (m / k) + m % k = m :=
(nat.add_comm _ _).trans (mod_add_div _ _)
lemma mod_add_div' (m k : ℕ) : m % k + (m / k) * k = m :=
by { rw mul_comm, exact mod_add_div _ _ }
lemma div_add_mod' (m k : ℕ) : (m / k) * k + m % k = m :=
by { rw mul_comm, exact div_add_mod _ _ }
protected theorem div_mod_unique {n k m d : ℕ} (h : 0 < k) :
n / k = d ∧ n % k = m ↔ m + k * d = n ∧ m < k :=
⟨λ ⟨e₁, e₂⟩, e₁ ▸ e₂ ▸ ⟨mod_add_div _ _, mod_lt _ h⟩,
λ ⟨h₁, h₂⟩, h₁ ▸ by rw [add_mul_div_left _ _ h, add_mul_mod_self_left];
simp [div_eq_of_lt, mod_eq_of_lt, h₂]⟩
lemma two_mul_odd_div_two {n : ℕ} (hn : n % 2 = 1) : 2 * (n / 2) = n - 1 :=
by conv {to_rhs, rw [← nat.mod_add_div n 2, hn, add_tsub_cancel_left]}
lemma div_dvd_of_dvd {a b : ℕ} (h : b ∣ a) : (a / b) ∣ a :=
⟨b, (nat.div_mul_cancel h).symm⟩
protected lemma div_div_self : ∀ {a b : ℕ}, b ∣ a → 0 < a → a / (a / b) = b
| a 0 h₁ h₂ := by rw [eq_zero_of_zero_dvd h₁, nat.div_zero, nat.div_zero]
| 0 b h₁ h₂ := absurd h₂ dec_trivial
| (a+1) (b+1) h₁ h₂ :=
(nat.mul_left_inj (nat.div_pos (le_of_dvd (succ_pos a) h₁) (succ_pos b))).1 $
by rw [nat.div_mul_cancel (div_dvd_of_dvd h₁), nat.mul_div_cancel' h₁]
lemma mod_mul_right_div_self (a b c : ℕ) : a % (b * c) / b = (a / b) % c :=
begin
rcases nat.eq_zero_or_pos b with rfl|hb, { simp },
rcases nat.eq_zero_or_pos c with rfl|hc, { simp },
conv_rhs { rw ← mod_add_div a (b * c) },
rw [mul_assoc, nat.add_mul_div_left _ _ hb, add_mul_mod_self_left,
mod_eq_of_lt (nat.div_lt_of_lt_mul (mod_lt _ (mul_pos hb hc)))]
end
lemma mod_mul_left_div_self (a b c : ℕ) : a % (c * b) / b = (a / b) % c :=
by rw [mul_comm c, mod_mul_right_div_self]
@[simp] protected theorem dvd_one {n : ℕ} : n ∣ 1 ↔ n = 1 :=
⟨eq_one_of_dvd_one, λ e, e.symm ▸ dvd_rfl⟩
protected theorem dvd_add_left {k m n : ℕ} (h : k ∣ n) : k ∣ m + n ↔ k ∣ m :=
(nat.dvd_add_iff_left h).symm
protected theorem dvd_add_right {k m n : ℕ} (h : k ∣ m) : k ∣ m + n ↔ k ∣ n :=
(nat.dvd_add_iff_right h).symm
@[simp] protected theorem not_two_dvd_bit1 (n : ℕ) : ¬ 2 ∣ bit1 n :=
by { rw [bit1, nat.dvd_add_right two_dvd_bit0, nat.dvd_one], cc }
/-- A natural number `m` divides the sum `m + n` if and only if `m` divides `n`.-/
@[simp] protected lemma dvd_add_self_left {m n : ℕ} :
m ∣ m + n ↔ m ∣ n :=
nat.dvd_add_right (dvd_refl m)
/-- A natural number `m` divides the sum `n + m` if and only if `m` divides `n`.-/
@[simp] protected lemma dvd_add_self_right {m n : ℕ} :
m ∣ n + m ↔ m ∣ n :=
nat.dvd_add_left (dvd_refl m)
-- TODO: update `nat.dvd_sub` in core
lemma dvd_sub' {k m n : ℕ} (h₁ : k ∣ m) (h₂ : k ∣ n) : k ∣ m - n :=
begin
cases le_total n m with H H,
{ exact dvd_sub H h₁ h₂ },
{ rw tsub_eq_zero_iff_le.mpr H,
exact dvd_zero k },
end
lemma not_dvd_of_pos_of_lt {a b : ℕ} (h1 : 0 < b) (h2 : b < a) : ¬ a ∣ b :=
begin
rintros ⟨c, rfl⟩,
rcases nat.eq_zero_or_pos c with (rfl | hc),
{ exact lt_irrefl 0 h1 },
{ exact not_lt.2 (le_mul_of_pos_right hc) h2 },
end
protected theorem mul_dvd_mul_iff_left {a b c : ℕ} (ha : 0 < a) : a * b ∣ a * c ↔ b ∣ c :=
exists_congr $ λ d, by rw [mul_assoc, nat.mul_right_inj ha]
protected theorem mul_dvd_mul_iff_right {a b c : ℕ} (hc : 0 < c) : a * c ∣ b * c ↔ a ∣ b :=
exists_congr $ λ d, by rw [mul_right_comm, nat.mul_left_inj hc]
lemma succ_div : ∀ (a b : ℕ), (a + 1) / b =
a / b + if b ∣ a + 1 then 1 else 0
| a 0 := by simp
| 0 1 := by simp
| 0 (b+2) := have hb2 : b + 2 > 1, from dec_trivial,
by simp [ne_of_gt hb2, div_eq_of_lt hb2]
| (a+1) (b+1) := begin
rw [nat.div_def], conv_rhs { rw nat.div_def },
by_cases hb_eq_a : b = a + 1,
{ simp [hb_eq_a, le_refl] },
by_cases hb_le_a1 : b ≤ a + 1,
{ have hb_le_a : b ≤ a, from le_of_lt_succ (lt_of_le_of_ne hb_le_a1 hb_eq_a),
have h₁ : (0 < b + 1 ∧ b + 1 ≤ a + 1 + 1),
from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a1⟩,
have h₂ : (0 < b + 1 ∧ b + 1 ≤ a + 1),
from ⟨succ_pos _, (add_le_add_iff_right _).2 hb_le_a⟩,
have dvd_iff : b + 1 ∣ a - b + 1 ↔ b + 1 ∣ a + 1 + 1,
{ rw [nat.dvd_add_iff_left (dvd_refl (b + 1)),
← add_tsub_add_eq_tsub_right a 1 b, add_comm (_ - _), add_assoc,
tsub_add_cancel_of_le (succ_le_succ hb_le_a), add_comm 1] },
have wf : a - b < a + 1, from lt_succ_of_le tsub_le_self,
rw [if_pos h₁, if_pos h₂, add_tsub_add_eq_tsub_right, ← tsub_add_eq_add_tsub hb_le_a,
by exact have _ := wf, succ_div (a - b),
add_tsub_add_eq_tsub_right],
simp [dvd_iff, succ_eq_add_one, add_comm 1, add_assoc] },
{ have hba : ¬ b ≤ a,
from not_le_of_gt (lt_trans (lt_succ_self a) (lt_of_not_ge hb_le_a1)),
have hb_dvd_a : ¬ b + 1 ∣ a + 2,
from λ h, hb_le_a1 (le_of_succ_le_succ (le_of_dvd (succ_pos _) h)),
simp [hba, hb_le_a1, hb_dvd_a], }
end
lemma succ_div_of_dvd {a b : ℕ} (hba : b ∣ a + 1) :
(a + 1) / b = a / b + 1 :=
by rw [succ_div, if_pos hba]
lemma succ_div_of_not_dvd {a b : ℕ} (hba : ¬ b ∣ a + 1) :
(a + 1) / b = a / b :=
by rw [succ_div, if_neg hba, add_zero]
lemma dvd_iff_div_mul_eq (n d : ℕ) : d ∣ n ↔ n / d * d = n :=
⟨λ h, nat.div_mul_cancel h, λ h, dvd.intro_left (n / d) h⟩
lemma dvd_iff_le_div_mul (n d : ℕ) : d ∣ n ↔ n ≤ n / d * d :=
((dvd_iff_div_mul_eq _ _).trans le_antisymm_iff).trans (and_iff_right (div_mul_le_self n d))
lemma dvd_iff_dvd_dvd (n d : ℕ) : d ∣ n ↔ ∀ k : ℕ, k ∣ d → k ∣ n :=
⟨λ h k hkd, dvd_trans hkd h, λ h, h _ dvd_rfl⟩
@[simp] theorem mod_mod_of_dvd (n : nat) {m k : nat} (h : m ∣ k) : n % k % m = n % m :=
begin
conv { to_rhs, rw ←mod_add_div n k },
rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left]
end
@[simp] theorem mod_mod (a n : ℕ) : (a % n) % n = a % n :=
(nat.eq_zero_or_pos n).elim
(λ n0, by simp [n0])
(λ npos, mod_eq_of_lt (mod_lt _ npos))
/-- If `a` and `b` are equal mod `c`, `a - b` is zero mod `c`. -/
lemma sub_mod_eq_zero_of_mod_eq {a b c : ℕ} (h : a % c = b % c) : (a - b) % c = 0 :=
by rw [←nat.mod_add_div a c, ←nat.mod_add_div b c, ←h, tsub_add_eq_tsub_tsub, add_tsub_cancel_left,
←mul_tsub, nat.mul_mod_right]
@[simp] lemma one_mod (n : ℕ) : 1 % (n + 2) = 1 := nat.mod_eq_of_lt (add_lt_add_right n.succ_pos 1)
lemma dvd_sub_mod (k : ℕ) : n ∣ (k - (k % n)) :=
⟨k / n, tsub_eq_of_eq_add_rev (nat.mod_add_div k n).symm⟩
@[simp] theorem mod_add_mod (m n k : ℕ) : (m % n + k) % n = (m + k) % n :=
by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm;
rwa [add_right_comm, mod_add_div] at this
@[simp] theorem add_mod_mod (m n k : ℕ) : (m + n % k) % k = (m + n) % k :=
by rw [add_comm, mod_add_mod, add_comm]
lemma add_mod (a b n : ℕ) : (a + b) % n = ((a % n) + (b % n)) % n :=
by rw [add_mod_mod, mod_add_mod]
theorem add_mod_eq_add_mod_right {m n k : ℕ} (i : ℕ) (H : m % n = k % n) :
(m + i) % n = (k + i) % n :=
by rw [← mod_add_mod, ← mod_add_mod k, H]
theorem add_mod_eq_add_mod_left {m n k : ℕ} (i : ℕ) (H : m % n = k % n) :
(i + m) % n = (i + k) % n :=
by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm]
lemma add_mod_eq_ite {a b n : ℕ} :
(a + b) % n = if n ≤ a % n + b % n then a % n + b % n - n else a % n + b % n :=
begin
cases n, { simp },
rw nat.add_mod,
split_ifs with h,
{ rw [nat.mod_eq_sub_mod h, nat.mod_eq_of_lt],
exact (tsub_lt_iff_right h).mpr
(nat.add_lt_add (a.mod_lt n.zero_lt_succ) (b.mod_lt n.zero_lt_succ)) },
{ exact nat.mod_eq_of_lt (lt_of_not_ge h) }
end
lemma mul_mod (a b n : ℕ) : (a * b) % n = ((a % n) * (b % n)) % n :=
begin
conv_lhs
{ rw [←mod_add_div a n, ←mod_add_div' b n, right_distrib, left_distrib, left_distrib,
mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left, ← mul_assoc,
add_mul_mod_self_right] }
end
lemma dvd_div_of_mul_dvd {a b c : ℕ} (h : a * b ∣ c) : b ∣ c / a :=
if ha : a = 0 then
by simp [ha]
else
have ha : 0 < a, from nat.pos_of_ne_zero ha,
have h1 : ∃ d, c = a * b * d, from h,
let ⟨d, hd⟩ := h1 in
have h2 : c / a = b * d, from nat.div_eq_of_eq_mul_right ha (by simpa [mul_assoc] using hd),
show ∃ d, c / a = b * d, from ⟨d, h2⟩
lemma mul_dvd_of_dvd_div {a b c : ℕ} (hab : c ∣ b) (h : a ∣ b / c) : c * a ∣ b :=
have h1 : ∃ d, b / c = a * d, from h,
have h2 : ∃ e, b = c * e, from hab,
let ⟨d, hd⟩ := h1, ⟨e, he⟩ := h2 in
have h3 : b = a * d * c, from
nat.eq_mul_of_div_eq_left hab hd,
show ∃ d, b = c * a * d, from ⟨d, by cc⟩
@[simp] lemma dvd_div_iff {a b c : ℕ} (hbc : c ∣ b) : a ∣ b / c ↔ c * a ∣ b :=
⟨λ h, mul_dvd_of_dvd_div hbc h, λ h, dvd_div_of_mul_dvd h⟩
lemma div_mul_div_comm {a b c d : ℕ} (hab : b ∣ a) (hcd : d ∣ c) :
(a / b) * (c / d) = (a * c) / (b * d) :=
have exi1 : ∃ x, a = b * x, from hab,
have exi2 : ∃ y, c = d * y, from hcd,
if hb : b = 0 then by simp [hb]
else have 0 < b, from nat.pos_of_ne_zero hb,
if hd : d = 0 then by simp [hd]
else have 0 < d, from nat.pos_of_ne_zero hd,
begin
cases exi1 with x hx, cases exi2 with y hy,
rw [hx, hy, nat.mul_div_cancel_left, nat.mul_div_cancel_left],
symmetry,
apply nat.div_eq_of_eq_mul_left,
apply mul_pos,
repeat {assumption},
cc
end
@[simp]
lemma div_div_div_eq_div : ∀ {a b c : ℕ} (dvd : b ∣ a) (dvd2 : a ∣ c), (c / (a / b)) / b = c / a
| 0 _ := by simp
| (a + 1) 0 := λ _ dvd _, by simpa using dvd
| (a + 1) (c + 1) :=
have a_split : a + 1 ≠ 0 := succ_ne_zero a,
have c_split : c + 1 ≠ 0 := succ_ne_zero c,
λ b dvd dvd2,
begin
rcases dvd2 with ⟨k, rfl⟩,
rcases dvd with ⟨k2, pr⟩,
have k2_nonzero : k2 ≠ 0 := λ k2_zero, by simpa [k2_zero] using pr,
rw [nat.mul_div_cancel_left k (nat.pos_of_ne_zero a_split), pr,
nat.mul_div_cancel_left k2 (nat.pos_of_ne_zero c_split), nat.mul_comm ((c + 1) * k2) k,
←nat.mul_assoc k (c + 1) k2, nat.mul_div_cancel _ (nat.pos_of_ne_zero k2_nonzero),
nat.mul_div_cancel _ (nat.pos_of_ne_zero c_split)],
end
lemma eq_of_dvd_of_div_eq_one {a b : ℕ} (w : a ∣ b) (h : b / a = 1) : a = b :=
by rw [←nat.div_mul_cancel w, h, one_mul]
lemma eq_zero_of_dvd_of_div_eq_zero {a b : ℕ} (w : a ∣ b) (h : b / a = 0) : b = 0 :=
by rw [←nat.div_mul_cancel w, h, zero_mul]
/-- If a small natural number is divisible by a larger natural number,
the small number is zero. -/
lemma eq_zero_of_dvd_of_lt {a b : ℕ} (w : a ∣ b) (h : b < a) : b = 0 :=
nat.eq_zero_of_dvd_of_div_eq_zero w
((nat.div_eq_zero_iff (lt_of_le_of_lt (zero_le b) h)).elim_right h)
lemma div_le_div_left {a b c : ℕ} (h₁ : c ≤ b) (h₂ : 0 < c) : a / b ≤ a / c :=
(nat.le_div_iff_mul_le h₂).2 $
le_trans (nat.mul_le_mul_left _ h₁) (div_mul_le_self _ _)
lemma div_eq_self {a b : ℕ} : a / b = a ↔ a = 0 ∨ b = 1 :=
begin
split,
{ intro,
cases b,
{ simp * at * },
{ cases b,
{ right, refl },
{ left,
have : a / (b + 2) ≤ a / 2 := div_le_div_left (by simp) dec_trivial,
refine eq_zero_of_le_half _,
simp * at * } } },
{ rintros (rfl|rfl); simp }
end
lemma lt_iff_le_pred : ∀ {m n : ℕ}, 0 < n → (m < n ↔ m ≤ n - 1)
| m (n+1) _ := lt_succ_iff
lemma div_eq_sub_mod_div {m n : ℕ} : m / n = (m - m % n) / n :=
begin
by_cases n0 : n = 0,
{ rw [n0, nat.div_zero, nat.div_zero] },
{ rw [← mod_add_div m n] { occs := occurrences.pos [2] },
rw [add_tsub_cancel_left, mul_div_right _ (nat.pos_of_ne_zero n0)] }
end
lemma mul_div_le (m n : ℕ) : n * (m / n) ≤ m :=
begin
cases nat.eq_zero_or_pos n with n0 h,
{ rw [n0, zero_mul], exact m.zero_le },
{ rw [mul_comm, ← nat.le_div_iff_mul_le' h] },
end
lemma lt_mul_div_succ (m : ℕ) {n : ℕ} (n0 : 0 < n) : m < n * ((m / n) + 1) :=
begin
rw [mul_comm, ← nat.div_lt_iff_lt_mul' n0],
exact lt_succ_self _
end
@[simp] lemma mod_div_self (m n : ℕ) : m % n / n = 0 :=
begin
cases n,
{ exact (m % 0).div_zero },
{ exact nat.div_eq_zero (m.mod_lt n.succ_pos) }
end
/-- `n` is not divisible by `a` if it is between `a * k` and `a * (k + 1)` for some `k`. -/
lemma not_dvd_of_between_consec_multiples {n a k : ℕ} (h1 : a * k < n) (h2 : n < a * (k + 1)) :
¬ a ∣ n :=
begin
rintro ⟨d, rfl⟩,
exact monotone.ne_of_lt_of_lt_nat (covariant.monotone_of_const a) k h1 h2 d rfl,
end
/-- `n` is not divisible by `a` iff it is between `a * k` and `a * (k + 1)` for some `k`. -/
lemma not_dvd_iff_between_consec_multiples (n : ℕ) {a : ℕ} (ha : 0 < a) :
(∃ k : ℕ, a * k < n ∧ n < a * (k + 1)) ↔ ¬ a ∣ n :=
begin
refine ⟨λ ⟨k, hk1, hk2⟩, not_dvd_of_between_consec_multiples hk1 hk2,
λ han, ⟨n/a, ⟨lt_of_le_of_ne (mul_div_le n a) _, lt_mul_div_succ _ ha⟩⟩⟩,
exact mt (dvd.intro (n/a)) han,
end
/-- Two natural numbers are equal if and only if they have the same multiples. -/
lemma dvd_right_iff_eq {m n : ℕ} : (∀ a : ℕ, m ∣ a ↔ n ∣ a) ↔ m = n :=
⟨λ h, dvd_antisymm ((h _).mpr dvd_rfl) ((h _).mp dvd_rfl), λ h n, by rw h⟩
/-- Two natural numbers are equal if and only if they have the same divisors. -/
lemma dvd_left_iff_eq {m n : ℕ} : (∀ a : ℕ, a ∣ m ↔ a ∣ n) ↔ m = n :=
⟨λ h, dvd_antisymm ((h _).mp dvd_rfl) ((h _).mpr dvd_rfl), λ h n, by rw h⟩
/-- `dvd` is injective in the left argument -/
lemma dvd_left_injective : function.injective ((∣) : ℕ → ℕ → Prop) :=
λ m n h, dvd_right_iff_eq.mp $ λ a, iff_of_eq (congr_fun h a)
lemma div_lt_div_of_lt_of_dvd {a b d : ℕ} (hdb : d ∣ b) (h : a < b) : a / d < b / d :=
by { rw nat.lt_div_iff_mul_lt hdb, exact lt_of_le_of_lt (mul_div_le a d) h }
lemma mul_add_mod (a b c : ℕ) : (a * b + c) % b = c % b :=
by simp [nat.add_mod]
lemma mul_add_mod_of_lt {a b c : ℕ} (h : c < b) : (a * b + c) % b = c :=
by rw [nat.mul_add_mod, nat.mod_eq_of_lt h]
lemma pred_eq_self_iff {n : ℕ} : n.pred = n ↔ n = 0 :=
by { cases n; simp [(nat.succ_ne_self _).symm] }
/-! ### `find` -/
section find
variables {p q : ℕ → Prop} [decidable_pred p] [decidable_pred q]
lemma find_eq_iff (h : ∃ n : ℕ, p n) : nat.find h = m ↔ p m ∧ ∀ n < m, ¬ p n :=
begin
split,
{ rintro rfl, exact ⟨nat.find_spec h, λ _, nat.find_min h⟩ },
{ rintro ⟨hm, hlt⟩,
exact le_antisymm (nat.find_min' h hm) (not_lt.1 $ imp_not_comm.1 (hlt _) $ nat.find_spec h) }
end
@[simp] lemma find_lt_iff (h : ∃ n : ℕ, p n) (n : ℕ) : nat.find h < n ↔ ∃ m < n, p m :=
⟨λ h2, ⟨nat.find h, h2, nat.find_spec h⟩, λ ⟨m, hmn, hm⟩, (nat.find_min' h hm).trans_lt hmn⟩
@[simp] lemma find_le_iff (h : ∃ n : ℕ, p n) (n : ℕ) : nat.find h ≤ n ↔ ∃ m ≤ n, p m :=
by simp only [exists_prop, ← lt_succ_iff, find_lt_iff]
@[simp] lemma le_find_iff (h : ∃ (n : ℕ), p n) (n : ℕ) : n ≤ nat.find h ↔ ∀ m < n, ¬ p m :=
by simp_rw [← not_lt, find_lt_iff, not_exists]
@[simp] lemma lt_find_iff (h : ∃ n : ℕ, p n) (n : ℕ) : n < nat.find h ↔ ∀ m ≤ n, ¬ p m :=
by simp only [← succ_le_iff, le_find_iff, succ_le_succ_iff]
@[simp] lemma find_eq_zero (h : ∃ n : ℕ, p n) : nat.find h = 0 ↔ p 0 :=
by simp [find_eq_iff]
@[simp] lemma find_pos (h : ∃ n : ℕ, p n) : 0 < nat.find h ↔ ¬ p 0 :=
by rw [pos_iff_ne_zero, ne, nat.find_eq_zero]
theorem find_mono (h : ∀ n, q n → p n) {hp : ∃ n, p n} {hq : ∃ n, q n} :
nat.find hp ≤ nat.find hq :=
nat.find_min' _ (h _ (nat.find_spec hq))
lemma find_le {h : ∃ n, p n} (hn : p n) : nat.find h ≤ n :=
(nat.find_le_iff _ _).2 ⟨n, le_rfl, hn⟩
lemma find_add {hₘ : ∃ m, p (m + n)} {hₙ : ∃ n, p n} (hn : n ≤ nat.find hₙ) :
nat.find hₘ + n = nat.find hₙ :=
begin
refine ((le_find_iff _ _).2 (λ m hm hpm, hm.not_le _)).antisymm _,
{ have hnm : n ≤ m := hn.trans (find_le hpm),
refine add_le_of_le_tsub_right_of_le hnm (find_le _),
rwa tsub_add_cancel_of_le hnm },
{ rw ←tsub_le_iff_right,
refine (le_find_iff _ _).2 (λ m hm hpm, hm.not_le _),
rw tsub_le_iff_right,
exact find_le hpm }
end
lemma find_comp_succ (h₁ : ∃ n, p n) (h₂ : ∃ n, p (n + 1)) (h0 : ¬ p 0) :
nat.find h₁ = nat.find h₂ + 1 :=
begin
refine (find_eq_iff _).2 ⟨nat.find_spec h₂, λ n hn, _⟩,
cases n with n,
exacts [h0, @nat.find_min (λ n, p (n + 1)) _ h₂ _ (succ_lt_succ_iff.1 hn)]
end
end find
/-! ### `find_greatest` -/
section find_greatest
/-- `find_greatest P b` is the largest `i ≤ bound` such that `P i` holds, or `0` if no such `i`
exists -/
protected def find_greatest (P : ℕ → Prop) [decidable_pred P] : ℕ → ℕ
| 0 := 0
| (n + 1) := if P (n + 1) then n + 1 else find_greatest n
variables {P Q : ℕ → Prop} [decidable_pred P] {b : ℕ}
@[simp] lemma find_greatest_zero : nat.find_greatest P 0 = 0 := rfl
lemma find_greatest_succ (n : ℕ) :
nat.find_greatest P (n + 1) = if P (n + 1) then n + 1 else nat.find_greatest P n := rfl
@[simp] lemma find_greatest_eq : ∀ {b}, P b → nat.find_greatest P b = b
| 0 h := rfl
| (n + 1) h := by simp [nat.find_greatest, h]
@[simp] lemma find_greatest_of_not (h : ¬ P (b + 1)) :
nat.find_greatest P (b + 1) = nat.find_greatest P b :=
by simp [nat.find_greatest, h]
lemma find_greatest_eq_iff :
nat.find_greatest P b = m ↔ m ≤ b ∧ (m ≠ 0 → P m) ∧ (∀ ⦃n⦄, m < n → n ≤ b → ¬P n) :=
begin
induction b with b ihb generalizing m,
{ rw [eq_comm, iff.comm],
simp only [nonpos_iff_eq_zero, ne.def, and_iff_left_iff_imp, find_greatest_zero],
rintro rfl,
exact ⟨λ h, (h rfl).elim, λ n hlt heq, (hlt.ne heq.symm).elim⟩ },
{ by_cases hb : P (b + 1),
{ rw [find_greatest_eq hb], split,
{ rintro rfl,
exact ⟨le_rfl, λ _, hb, λ n hlt hle, (hlt.not_le hle).elim⟩ },
{ rintros ⟨hle, h0, hm⟩,
rcases decidable.eq_or_lt_of_le hle with rfl|hlt,
exacts [rfl, (hm hlt le_rfl hb).elim] } },
{ rw [find_greatest_of_not hb, ihb],
split,
{ rintros ⟨hle, hP, hm⟩,
refine ⟨hle.trans b.le_succ, hP, λ n hlt hle, _⟩,
rcases decidable.eq_or_lt_of_le hle with rfl|hlt',
exacts [hb, hm hlt $ lt_succ_iff.1 hlt'] },
{ rintros ⟨hle, hP, hm⟩,
refine ⟨lt_succ_iff.1 (hle.lt_of_ne _), hP, λ n hlt hle, hm hlt (hle.trans b.le_succ)⟩,
rintro rfl,
exact hb (hP b.succ_ne_zero) } } }
end
lemma find_greatest_eq_zero_iff : nat.find_greatest P b = 0 ↔ ∀ ⦃n⦄, 0 < n → n ≤ b → ¬P n :=
by simp [find_greatest_eq_iff]
lemma find_greatest_spec (hmb : m ≤ b) (hm : P m) : P (nat.find_greatest P b) :=
begin
by_cases h : nat.find_greatest P b = 0,
{ cases m, { rwa h },
exact ((find_greatest_eq_zero_iff.1 h) m.zero_lt_succ hmb hm).elim },
{ exact (find_greatest_eq_iff.1 rfl).2.1 h }
end
lemma find_greatest_le (n : ℕ) : nat.find_greatest P n ≤ n := (find_greatest_eq_iff.1 rfl).1
lemma le_find_greatest (hmb : m ≤ b) (hm : P m) : m ≤ nat.find_greatest P b :=
le_of_not_lt $ λ hlt, (find_greatest_eq_iff.1 rfl).2.2 hlt hmb hm
lemma find_greatest_mono_right (P : ℕ → Prop) [decidable_pred P] : monotone (nat.find_greatest P) :=
begin
refine monotone_nat_of_le_succ (λ n, _),
rw [find_greatest_succ],
split_ifs,
{ exact (find_greatest_le n).trans (le_succ _) },
{ refl }
end
lemma find_greatest_mono_left [decidable_pred Q] (hPQ : P ≤ Q) :
nat.find_greatest P ≤ nat.find_greatest Q :=
begin
intro n,
induction n with n hn,
{ refl },
by_cases P (n + 1),
{ rw [find_greatest_eq h, find_greatest_eq (hPQ _ h)] },
{ rw find_greatest_of_not h,
exact hn.trans (nat.find_greatest_mono_right _ $ le_succ _) }
end
lemma find_greatest_mono {a b : ℕ} [decidable_pred Q] (hPQ : P ≤ Q) (hab : a ≤ b) :
nat.find_greatest P a ≤ nat.find_greatest Q b :=
(nat.find_greatest_mono_right _ hab).trans $ find_greatest_mono_left hPQ _
lemma find_greatest_is_greatest (hk : nat.find_greatest P b < k) (hkb : k ≤ b) : ¬ P k :=
(find_greatest_eq_iff.1 rfl).2.2 hk hkb
lemma find_greatest_of_ne_zero (h : nat.find_greatest P b = m) (h0 : m ≠ 0) : P m :=
(find_greatest_eq_iff.1 h).2.1 h0
end find_greatest
/-! ### `bodd_div2` and `bodd` -/
@[simp] theorem bodd_div2_eq (n : ℕ) : bodd_div2 n = (bodd n, div2 n) :=
by unfold bodd div2; cases bodd_div2 n; refl
@[simp] lemma bodd_bit0 (n) : bodd (bit0 n) = ff := bodd_bit ff n
@[simp] lemma bodd_bit1 (n) : bodd (bit1 n) = tt := bodd_bit tt n
@[simp] lemma div2_bit0 (n) : div2 (bit0 n) = n := div2_bit ff n
@[simp] lemma div2_bit1 (n) : div2 (bit1 n) = n := div2_bit tt n
/-! ### `bit0` and `bit1` -/
-- There is no need to prove `bit0_eq_zero : bit0 n = 0 ↔ n = 0`
-- as this is true for any `[semiring R] [no_zero_divisors R] [char_zero R]`
-- However the lemmas `bit0_eq_bit0`, `bit1_eq_bit1`, `bit1_eq_one`, `one_eq_bit1`
-- need `[ring R] [no_zero_divisors R] [char_zero R]` in general,
-- so we prove `ℕ` specialized versions here.
@[simp] lemma bit0_eq_bit0 {m n : ℕ} : bit0 m = bit0 n ↔ m = n :=
⟨nat.bit0_inj, λ h, by subst h⟩
@[simp] lemma bit1_eq_bit1 {m n : ℕ} : bit1 m = bit1 n ↔ m = n :=
⟨nat.bit1_inj, λ h, by subst h⟩
@[simp] lemma bit1_eq_one {n : ℕ} : bit1 n = 1 ↔ n = 0 :=
⟨@nat.bit1_inj n 0, λ h, by subst h⟩
@[simp] lemma one_eq_bit1 {n : ℕ} : 1 = bit1 n ↔ n = 0 :=
⟨λ h, (@nat.bit1_inj 0 n h).symm, λ h, by subst h⟩
theorem bit_add : ∀ (b : bool) (n m : ℕ), bit b (n + m) = bit ff n + bit b m
| tt := bit1_add
| ff := bit0_add
theorem bit_add' : ∀ (b : bool) (n m : ℕ), bit b (n + m) = bit b n + bit ff m
| tt := bit1_add'
| ff := bit0_add
protected theorem bit0_le {n m : ℕ} (h : n ≤ m) : bit0 n ≤ bit0 m :=
add_le_add h h
protected theorem bit1_le {n m : ℕ} (h : n ≤ m) : bit1 n ≤ bit1 m :=
succ_le_succ (add_le_add h h)
theorem bit_le : ∀ (b : bool) {n m : ℕ}, n ≤ m → bit b n ≤ bit b m
| tt n m h := nat.bit1_le h
| ff n m h := nat.bit0_le h
theorem bit_ne_zero (b) {n} (h : n ≠ 0) : bit b n ≠ 0 :=
by cases b; [exact nat.bit0_ne_zero h, exact nat.bit1_ne_zero _]
theorem bit0_le_bit : ∀ (b) {m n : ℕ}, m ≤ n → bit0 m ≤ bit b n
| tt m n h := le_of_lt $ nat.bit0_lt_bit1 h
| ff m n h := nat.bit0_le h
theorem bit_le_bit1 : ∀ (b) {m n : ℕ}, m ≤ n → bit b m ≤ bit1 n
| ff m n h := le_of_lt $ nat.bit0_lt_bit1 h
| tt m n h := nat.bit1_le h
theorem bit_lt_bit0 : ∀ (b) {n m : ℕ}, n < m → bit b n < bit0 m
| tt n m h := nat.bit1_lt_bit0 h
| ff n m h := nat.bit0_lt h
theorem bit_lt_bit (a b) {n m : ℕ} (h : n < m) : bit a n < bit b m :=
lt_of_lt_of_le (bit_lt_bit0 _ h) (bit0_le_bit _ le_rfl)
@[simp] lemma bit0_le_bit1_iff : bit0 k ≤ bit1 n ↔ k ≤ n :=
⟨λ h, by rwa [← nat.lt_succ_iff, n.bit1_eq_succ_bit0, ← n.bit0_succ_eq,
bit0_lt_bit0, nat.lt_succ_iff] at h, λ h, le_of_lt (nat.bit0_lt_bit1 h)⟩
@[simp] lemma bit0_lt_bit1_iff : bit0 k < bit1 n ↔ k ≤ n :=
⟨λ h, bit0_le_bit1_iff.1 (le_of_lt h), nat.bit0_lt_bit1⟩
@[simp] lemma bit1_le_bit0_iff : bit1 k ≤ bit0 n ↔ k < n :=
⟨λ h, by rwa [k.bit1_eq_succ_bit0, succ_le_iff, bit0_lt_bit0] at h,
λ h, le_of_lt (nat.bit1_lt_bit0 h)⟩
@[simp] lemma bit1_lt_bit0_iff : bit1 k < bit0 n ↔ k < n :=
⟨λ h, bit1_le_bit0_iff.1 (le_of_lt h), nat.bit1_lt_bit0⟩
@[simp] lemma one_le_bit0_iff : 1 ≤ bit0 n ↔ 0 < n :=
by { convert bit1_le_bit0_iff, refl, }
@[simp] lemma one_lt_bit0_iff : 1 < bit0 n ↔ 1 ≤ n :=
by { convert bit1_lt_bit0_iff, refl, }
@[simp] lemma bit_le_bit_iff : ∀ {b : bool}, bit b k ≤ bit b n ↔ k ≤ n
| ff := bit0_le_bit0
| tt := bit1_le_bit1
@[simp] lemma bit_lt_bit_iff : ∀ {b : bool}, bit b k < bit b n ↔ k < n
| ff := bit0_lt_bit0
| tt := bit1_lt_bit1
@[simp] lemma bit_le_bit1_iff : ∀ {b : bool}, bit b k ≤ bit1 n ↔ k ≤ n
| ff := bit0_le_bit1_iff
| tt := bit1_le_bit1
@[simp] lemma bit0_mod_two : bit0 n % 2 = 0 := by { rw nat.mod_two_of_bodd, simp }
@[simp] lemma bit1_mod_two : bit1 n % 2 = 1 := by { rw nat.mod_two_of_bodd, simp }
lemma pos_of_bit0_pos {n : ℕ} (h : 0 < bit0 n) : 0 < n :=
by { cases n, cases h, apply succ_pos, }
@[simp] lemma bit_cases_on_bit {C : ℕ → Sort u} (H : Π b n, C (bit b n)) (b : bool) (n : ℕ) :
bit_cases_on (bit b n) H = H b n :=
eq_of_heq $ (eq_rec_heq _ _).trans $ by rw [bodd_bit, div2_bit]
@[simp] lemma bit_cases_on_bit0 {C : ℕ → Sort u} (H : Π b n, C (bit b n)) (n : ℕ) :
bit_cases_on (bit0 n) H = H ff n :=
bit_cases_on_bit H ff n
@[simp] lemma bit_cases_on_bit1 {C : ℕ → Sort u} (H : Π b n, C (bit b n)) (n : ℕ) :
bit_cases_on (bit1 n) H = H tt n :=
bit_cases_on_bit H tt n
lemma bit_cases_on_injective {C : ℕ → Sort u} :
function.injective (λ H : Π b n, C (bit b n), λ n, bit_cases_on n H) :=
begin
intros H₁ H₂ h,
ext b n,
simpa only [bit_cases_on_bit] using congr_fun h (bit b n)
end
@[simp] lemma bit_cases_on_inj {C : ℕ → Sort u} (H₁ H₂ : Π b n, C (bit b n)) :
(λ n, bit_cases_on n H₁) = (λ n, bit_cases_on n H₂) ↔ H₁ = H₂ :=
bit_cases_on_injective.eq_iff
/-! ### decidability of predicates -/
instance decidable_ball_lt (n : nat) (P : Π k < n, Prop) :
∀ [H : ∀ n h, decidable (P n h)], decidable (∀ n h, P n h) :=
begin
induction n with n IH; intro; resetI,
{ exact is_true (λ n, dec_trivial) },
cases IH (λ k h, P k (lt_succ_of_lt h)) with h,
{ refine is_false (mt _ h), intros hn k h, apply hn },
by_cases p : P n (lt_succ_self n),
{ exact is_true (λ k h',
(le_of_lt_succ h').lt_or_eq_dec.elim (h _)
(λ e, match k, e, h' with _, rfl, h := p end)) },
{ exact is_false (mt (λ hn, hn _ _) p) }
end
instance decidable_forall_fin {n : ℕ} (P : fin n → Prop)
[H : decidable_pred P] : decidable (∀ i, P i) :=
decidable_of_iff (∀ k h, P ⟨k, h⟩) ⟨λ a ⟨k, h⟩, a k h, λ a k h, a ⟨k, h⟩⟩
instance decidable_ball_le (n : ℕ) (P : Π k ≤ n, Prop)
[H : ∀ n h, decidable (P n h)] : decidable (∀ n h, P n h) :=
decidable_of_iff (∀ k (h : k < succ n), P k (le_of_lt_succ h))
⟨λ a k h, a k (lt_succ_of_le h), λ a k h, a k _⟩
instance decidable_lo_hi (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] :
decidable (∀x, lo ≤ x → x < hi → P x) :=
decidable_of_iff (∀ x < hi - lo, P (lo + x))
⟨λal x hl hh, by { have := al (x - lo) ((tsub_lt_tsub_iff_right hl).mpr hh),
rwa [add_tsub_cancel_of_le hl] at this, },
λal x h, al _ (nat.le_add_right _ _) (lt_tsub_iff_left.mp h)⟩
instance decidable_lo_hi_le (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] :
decidable (∀x, lo ≤ x → x ≤ hi → P x) :=
decidable_of_iff (∀x, lo ≤ x → x < hi + 1 → P x) $
ball_congr $ λ x hl, imp_congr lt_succ_iff iff.rfl
instance decidable_exists_lt {P : ℕ → Prop} [h : decidable_pred P] :
decidable_pred (λ n, ∃ (m : ℕ), m < n ∧ P m)
| 0 := is_false (by simp)
| (n + 1) := decidable_of_decidable_of_iff (@or.decidable _ _ (decidable_exists_lt n) (h n))
(by simp only [lt_succ_iff_lt_or_eq, or_and_distrib_right, exists_or_distrib, exists_eq_left])
instance decidable_exists_le {P : ℕ → Prop} [h : decidable_pred P] :
decidable_pred (λ n, ∃ (m : ℕ), m ≤ n ∧ P m) :=
λ n, decidable_of_iff (∃ m, m < n + 1 ∧ P m) (exists_congr (λ x, and_congr_left' lt_succ_iff))
end nat
|
527469ddae925a05d7aa01bdbe8204e86f4b1bf0 | 0003047346476c031128723dfd16fe273c6bc605 | /src/analysis/asymptotics.lean | ecb02431eec39c633dc6d9c67980bb2d522cef96 | [
"Apache-2.0"
] | permissive | ChandanKSingh/mathlib | d2bf4724ccc670bf24915c12c475748281d3fb73 | d60d1616958787ccb9842dc943534f90ea0bab64 | refs/heads/master | 1,588,238,823,679 | 1,552,867,469,000 | 1,552,867,469,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,152 | lean | /-
Copyright (c) 2019 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jeremy Avigad
We introduce these relations:
`is_O f g l` : "f is big O of g along l"
`is_o f g l` : "f is little o of g along l"
Here `l` is any filter on the domain of `f` and `g`, which are assumed to be the same. The codomains
of `f` and `g` do not need to be the same; all that is needed that there is a norm associated with
these types, and it is the norm that is compared asymptotically.
Often the ranges of `f` and `g` will be the real numbers, in which case the norm is the absolute
value. In general, we have
`is_O f g l ↔ is_O (λ x, ∥f x∥) (λ x, ∥g x∥) l`,
and similarly for `is_o`. But our setup allows us to use the notions e.g. with functions
to the integers, rationals, complex numbers, or any normed vector space without mentioning the
norm explicitly.
If `f` and `g` are functions to a normed field like the reals or complex numbers and `g` is always
nonzero, we have
`is_o f g l ↔ tendsto (λ x, f x / (g x)) (nhds 0) l`.
In fact, the right-to-left direction holds without the hypothesis on `g`, and in the other direction
it suffices to assume that `f` is zero wherever `g` is. (This generalization is useful in defining
the Fréchet derivative.)
-/
import analysis.normed_space.basic
open filter
namespace asymptotics
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
section
variables [has_norm β] [has_norm γ] [has_norm δ]
def is_O (f : α → β) (g : α → γ) (l : filter α) : Prop :=
∃ c > 0, { x | ∥ f x ∥ ≤ c * ∥ g x ∥ } ∈ l
def is_o (f : α → β) (g : α → γ) (l : filter α) : Prop :=
∀ c > 0, { x | ∥ f x ∥ ≤ c * ∥ g x ∥ } ∈ l
theorem is_O_refl (f : α → β) (l : filter α) : is_O f f l :=
⟨1, zero_lt_one, by { filter_upwards [univ_mem_sets], intros x _, simp }⟩
theorem is_O.comp {f : α → β} {g : α → γ} {l : filter α} (hfg : is_O f g l)
{δ : Type*} (k : δ → α) :
is_O (f ∘ k) (g ∘ k) (l.comap k) :=
let ⟨c, cpos, hfgc⟩ := hfg in
⟨c, cpos, mem_comap_sets.mpr ⟨_, hfgc, set.subset.refl _⟩⟩
theorem is_o.comp {f : α → β} {g : α → γ} {l : filter α} (hfg : is_o f g l)
{δ : Type*} (k : δ → α) :
is_o (f ∘ k) (g ∘ k) (l.comap k) :=
λ c cpos, mem_comap_sets.mpr ⟨_, hfg c cpos, set.subset.refl _⟩
theorem is_O.mono {f : α → β} {g : α → γ} {l₁ l₂ : filter α} (h : l₁ ≤ l₂)
(h' : is_O f g l₂) : is_O f g l₁ :=
let ⟨c, cpos, h'c⟩ := h' in ⟨c, cpos, h (h'c)⟩
theorem is_o.mono {f : α → β} {g : α → γ} {l₁ l₂ : filter α} (h : l₁ ≤ l₂)
(h' : is_o f g l₂) : is_o f g l₁ :=
λ c cpos, h (h' c cpos)
theorem is_o.to_is_O {f : α → β} {g : α → γ} {l : filter α} (hgf : is_o f g l) : is_O f g l :=
⟨1, zero_lt_one, hgf 1 zero_lt_one⟩
theorem is_O.trans {f : α → β} {g : α → γ} {k : α → δ} {l : filter α}
(h₁ : is_O f g l) (h₂ : is_O g k l) :
is_O f k l :=
let ⟨c, cpos, hc⟩ := h₁,
⟨c', c'pos, hc'⟩ := h₂ in
begin
use [c * c', mul_pos cpos c'pos],
filter_upwards [hc, hc'], dsimp,
intros x h₁x h₂x, rw mul_assoc,
exact le_trans h₁x (mul_le_mul_of_nonneg_left h₂x (le_of_lt cpos))
end
theorem is_o.trans_is_O {f : α → β} {g : α → γ} {k : α → δ} {l : filter α}
(h₁ : is_o f g l) (h₂ : is_O g k l) :
is_o f k l :=
begin
intros c cpos,
rcases h₂ with ⟨c', c'pos, hc'⟩,
have cc'pos := div_pos cpos c'pos,
filter_upwards [h₁ (c / c') cc'pos, hc'], dsimp,
intros x h₁x h₂x,
refine le_trans h₁x (le_trans (mul_le_mul_of_nonneg_left h₂x (le_of_lt cc'pos)) _),
rw [←mul_assoc, div_mul_cancel _ (ne_of_gt c'pos)]
end
theorem is_O.trans_is_o {f : α → β} {g : α → γ} {k : α → δ} {l : filter α}
(h₁ : is_O f g l) (h₂ : is_o g k l) :
is_o f k l :=
begin
intros c cpos,
rcases h₁ with ⟨c', c'pos, hc'⟩,
have cc'pos := div_pos cpos c'pos,
filter_upwards [hc', h₂ (c / c') cc'pos], dsimp,
intros x h₁x h₂x,
refine le_trans h₁x (le_trans (mul_le_mul_of_nonneg_left h₂x (le_of_lt c'pos)) _),
rw [←mul_assoc, mul_div_cancel' _ (ne_of_gt c'pos)]
end
theorem is_o.trans {f : α → β} {g : α → γ} {k : α → δ} {l : filter α}
(h₁ : is_o f g l) (h₂ : is_o g k l) :
is_o f k l :=
h₁.to_is_O.trans_is_o h₂
theorem is_o_join {f : α → β} {g : α → γ} {l₁ l₂ : filter α}
(h₁ : is_o f g l₁) (h₂ : is_o f g l₂) :
is_o f g (l₁ ⊔ l₂) :=
begin
intros c cpos,
rw mem_sup_sets,
exact ⟨h₁ c cpos, h₂ c cpos⟩
end
theorem is_O_congr {f₁ f₂ : α → β} {g₁ g₂ : α → γ} {l : filter α}
(hf : {x | f₁ x = f₂ x} ∈ l) (hg : {x | g₁ x = g₂ x} ∈ l) :
is_O f₁ g₁ l ↔ is_O f₂ g₂ l :=
bex_congr $ λ c _, filter.congr_sets $
by filter_upwards [hf, hg] λ x e₁ e₂,
by dsimp at e₁ e₂ ⊢; rw [e₁, e₂]
theorem is_o_congr {f₁ f₂ : α → β} {g₁ g₂ : α → γ} {l : filter α}
(hf : {x | f₁ x = f₂ x} ∈ l) (hg : {x | g₁ x = g₂ x} ∈ l) :
is_o f₁ g₁ l ↔ is_o f₂ g₂ l :=
ball_congr $ λ c _, filter.congr_sets $
by filter_upwards [hf, hg] λ x e₁ e₂,
by dsimp at e₁ e₂ ⊢; rw [e₁, e₂]
theorem is_O.congr {f₁ f₂ : α → β} {g₁ g₂ : α → γ} {l : filter α}
(hf : ∀ x, f₁ x = f₂ x) (hg : ∀ x, g₁ x = g₂ x) :
is_O f₁ g₁ l → is_O f₂ g₂ l :=
(is_O_congr (univ_mem_sets' hf) (univ_mem_sets' hg)).1
theorem is_o.congr {f₁ f₂ : α → β} {g₁ g₂ : α → γ} {l : filter α}
(hf : ∀ x, f₁ x = f₂ x) (hg : ∀ x, g₁ x = g₂ x) :
is_o f₁ g₁ l → is_o f₂ g₂ l :=
(is_o_congr (univ_mem_sets' hf) (univ_mem_sets' hg)).1
theorem is_O_congr_left {f₁ f₂ : α → β} {g : α → γ} {l : filter α}
(h : {x | f₁ x = f₂ x} ∈ l) :
is_O f₁ g l ↔ is_O f₂ g l :=
is_O_congr h (univ_mem_sets' $ λ _, rfl)
theorem is_o_congr_left {f₁ f₂ : α → β} {g : α → γ} {l : filter α}
(h : {x | f₁ x = f₂ x} ∈ l) :
is_o f₁ g l ↔ is_o f₂ g l :=
is_o_congr h (univ_mem_sets' $ λ _, rfl)
theorem is_O.congr_left {f₁ f₂ : α → β} {g : α → γ} {l : filter α}
(hf : ∀ x, f₁ x = f₂ x) : is_O f₁ g l → is_O f₂ g l :=
is_O.congr hf (λ _, rfl)
theorem is_o.congr_left {f₁ f₂ : α → β} {g : α → γ} {l : filter α}
(hf : ∀ x, f₁ x = f₂ x) : is_o f₁ g l → is_o f₂ g l :=
is_o.congr hf (λ _, rfl)
theorem is_O_congr_right {f : α → β} {g₁ g₂ : α → γ} {l : filter α}
(h : {x | g₁ x = g₂ x} ∈ l) :
is_O f g₁ l ↔ is_O f g₂ l :=
is_O_congr (univ_mem_sets' $ λ _, rfl) h
theorem is_o_congr_right {f : α → β} {g₁ g₂ : α → γ} {l : filter α}
(h : {x | g₁ x = g₂ x} ∈ l) :
is_o f g₁ l ↔ is_o f g₂ l :=
is_o_congr (univ_mem_sets' $ λ _, rfl) h
theorem is_O.congr_right {f : α → β} {g₁ g₂ : α → γ} {l : filter α}
(hg : ∀ x, g₁ x = g₂ x) : is_O f g₁ l → is_O f g₂ l :=
is_O.congr (λ _, rfl) hg
theorem is_o.congr_right {f : α → β} {g₁ g₂ : α → γ} {l : filter α}
(hg : ∀ x, g₁ x = g₂ x) : is_o f g₁ l → is_o f g₂ l :=
is_o.congr (λ _, rfl) hg
end
section
variables [has_norm β] [normed_group γ]
@[simp]
theorem is_O_norm_right {f : α → β} {g : α → γ} {l : filter α} :
is_O f (λ x, ∥g x∥) l ↔ is_O f g l :=
by simp only [is_O, norm_norm]
@[simp]
theorem is_o_norm_right {f : α → β} {g : α → γ} {l : filter α} :
is_o f (λ x, ∥g x∥) l ↔ is_o f g l :=
by simp only [is_o, norm_norm]
@[simp]
theorem is_O_neg_right {f : α → β} {g : α → γ} {l : filter α} :
is_O f (λ x, -(g x)) l ↔ is_O f g l :=
by { rw ←is_O_norm_right, simp only [norm_neg], rw is_O_norm_right }
@[simp]
theorem is_o_neg_right {f : α → β} {g : α → γ} {l : filter α} :
is_o f (λ x, -(g x)) l ↔ is_o f g l :=
by { rw ←is_o_norm_right, simp only [norm_neg], rw is_o_norm_right }
theorem is_O_iff {f : α → β} {g : α → γ} {l : filter α} :
is_O f g l ↔ ∃ c, { x | ∥f x∥ ≤ c * ∥g x∥ } ∈ l :=
suffices (∃ c, { x | ∥f x∥ ≤ c * ∥g x∥ } ∈ l) → is_O f g l,
from ⟨λ ⟨c, cpos, hc⟩, ⟨c, hc⟩, this⟩,
assume ⟨c, hc⟩,
or.elim (lt_or_ge 0 c)
(assume : c > 0, ⟨c, this, hc⟩)
(assume h'c : c ≤ 0,
have {x : α | ∥f x∥ ≤ 1 * ∥g x∥} ∈ l,
begin
filter_upwards [hc], intros x,
show ∥f x∥ ≤ c * ∥g x∥ → ∥f x∥ ≤ 1 * ∥g x∥,
{ intro hx, apply le_trans hx,
apply mul_le_mul_of_nonneg_right _ (norm_nonneg _),
show c ≤ 1, from le_trans h'c zero_le_one }
end,
⟨1, zero_lt_one, this⟩)
theorem is_O_join {f : α → β} {g : α → γ} {l₁ l₂ : filter α}
(h₁ : is_O f g l₁) (h₂ : is_O f g l₂) :
is_O f g (l₁ ⊔ l₂) :=
begin
rcases h₁ with ⟨c₁, c₁pos, hc₁⟩,
rcases h₂ with ⟨c₂, c₂pos, hc₂⟩,
have : 0 < max c₁ c₂, by { rw lt_max_iff, left, exact c₁pos },
use [max c₁ c₂, this],
rw mem_sup_sets, split,
{ filter_upwards [hc₁], dsimp, intros x hx,
exact le_trans hx (mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _)) },
filter_upwards [hc₂], dsimp, intros x hx,
exact le_trans hx (mul_le_mul_of_nonneg_right (le_max_right _ _) (norm_nonneg _))
end
end
section
variables [normed_group β] [has_norm γ]
@[simp] theorem is_O_norm_left {f : α → β} {g : α → γ} {l : filter α} :
is_O (λ x, ∥f x∥) g l ↔ is_O f g l :=
by simp only [is_O, norm_norm]
@[simp] theorem is_o_norm_left {f : α → β} {g : α → γ} {l : filter α} :
is_o (λ x, ∥f x∥) g l ↔ is_o f g l :=
by simp only [is_o, norm_norm]
@[simp] theorem is_O_neg_left {f : α → β} {g : α → γ} {l : filter α} :
is_O (λ x, -f x) g l ↔ is_O f g l :=
by { rw ←is_O_norm_left, simp only [norm_neg], rw is_O_norm_left }
@[simp] theorem is_o_neg_left {f : α → β} {g : α → γ} {l : filter α} :
is_o (λ x, -f x) g l ↔ is_o f g l :=
by { rw ←is_o_norm_left, simp only [norm_neg], rw is_o_norm_left }
theorem is_O.add {f₁ f₂ : α → β} {g : α → γ} {l : filter α} (h₁ : is_O f₁ g l) (h₂ : is_O f₂ g l) :
is_O (λ x, f₁ x + f₂ x) g l :=
let ⟨c₁, c₁pos, hc₁⟩ := h₁,
⟨c₂, c₂pos, hc₂⟩ := h₂ in
begin
use [c₁ + c₂, add_pos c₁pos c₂pos],
filter_upwards [hc₁, hc₂],
intros x hx₁ hx₂,
show ∥f₁ x + f₂ x∥ ≤ (c₁ + c₂) * ∥g x∥,
apply le_trans (norm_triangle _ _),
rw add_mul,
exact add_le_add hx₁ hx₂
end
theorem is_o.add {f₁ f₂ : α → β} {g : α → γ} {l : filter α} (h₁ : is_o f₁ g l) (h₂ : is_o f₂ g l) :
is_o (λ x, f₁ x + f₂ x) g l :=
begin
intros c cpos,
filter_upwards [h₁ (c / 2) (half_pos cpos), h₂ (c / 2) (half_pos cpos)],
intros x hx₁ hx₂, dsimp at hx₁ hx₂,
apply le_trans (norm_triangle _ _),
apply le_trans (add_le_add hx₁ hx₂),
rw [←mul_add, ←two_mul, ←mul_assoc, div_mul_cancel _ two_ne_zero]
end
theorem is_O.sub {f₁ f₂ : α → β} {g : α → γ} {l : filter α} (h₁ : is_O f₁ g l) (h₂ : is_O f₂ g l) :
is_O (λ x, f₁ x - f₂ x) g l :=
h₁.add (is_O_neg_left.mpr h₂)
theorem is_o.sub {f₁ f₂ : α → β} {g : α → γ} {l : filter α} (h₁ : is_o f₁ g l) (h₂ : is_o f₂ g l) :
is_o (λ x, f₁ x - f₂ x) g l :=
h₁.add (is_o_neg_left.mpr h₂)
theorem is_O_comm {f₁ f₂ : α → β} {g : α → γ} {l : filter α} :
is_O (λ x, f₁ x - f₂ x) g l ↔ is_O (λ x, f₂ x - f₁ x) g l :=
by simpa using @is_O_neg_left _ _ _ _ _ (λ x, f₂ x - f₁ x) g l
theorem is_O.symm {f₁ f₂ : α → β} {g : α → γ} {l : filter α} :
is_O (λ x, f₁ x - f₂ x) g l → is_O (λ x, f₂ x - f₁ x) g l :=
is_O_comm.1
theorem is_O.tri {f₁ f₂ f₃ : α → β} {g : α → γ} {l : filter α}
(h₁ : is_O (λ x, f₁ x - f₂ x) g l)
(h₂ : is_O (λ x, f₂ x - f₃ x) g l) :
is_O (λ x, f₁ x - f₃ x) g l :=
(h₁.add h₂).congr_left (by simp)
theorem is_O.congr_of_sub {f₁ f₂ : α → β} {g : α → γ} {l : filter α}
(h : is_O (λ x, f₁ x - f₂ x) g l) :
is_O f₁ g l ↔ is_O f₂ g l :=
⟨λ h', (h'.sub h).congr_left (λ x, sub_sub_cancel _ _),
λ h', (h.add h').congr_left (λ x, sub_add_cancel _ _)⟩
theorem is_o_comm {f₁ f₂ : α → β} {g : α → γ} {l : filter α} :
is_o (λ x, f₁ x - f₂ x) g l ↔ is_o (λ x, f₂ x - f₁ x) g l :=
by simpa using @is_o_neg_left _ _ _ _ _ (λ x, f₂ x - f₁ x) g l
theorem is_o.symm {f₁ f₂ : α → β} {g : α → γ} {l : filter α} :
is_o (λ x, f₁ x - f₂ x) g l → is_o (λ x, f₂ x - f₁ x) g l :=
is_o_comm.1
theorem is_o.tri {f₁ f₂ f₃ : α → β} {g : α → γ} {l : filter α}
(h₁ : is_o (λ x, f₁ x - f₂ x) g l)
(h₂ : is_o (λ x, f₂ x - f₃ x) g l) :
is_o (λ x, f₁ x - f₃ x) g l :=
(h₁.add h₂).congr_left (by simp)
theorem is_o.congr_of_sub {f₁ f₂ : α → β} {g : α → γ} {l : filter α}
(h : is_o (λ x, f₁ x - f₂ x) g l) :
is_o f₁ g l ↔ is_o f₂ g l :=
⟨λ h', (h'.sub h).congr_left (λ x, sub_sub_cancel _ _),
λ h', (h.add h').congr_left (λ x, sub_add_cancel _ _)⟩
end
section
variables [normed_group β] [normed_group γ]
theorem is_O_zero (g : α → γ) (l : filter α) :
is_O (λ x, (0 : β)) g l :=
⟨1, zero_lt_one, by { filter_upwards [univ_mem_sets], intros x _, simp }⟩
theorem is_O_refl_left {f : α → β} {g : α → γ} {l : filter α} :
is_O (λ x, f x - f x) g l :=
by simpa using is_O_zero g l
theorem is_O_zero_right_iff {f : α → β} {l : filter α} :
is_O f (λ x, (0 : γ)) l ↔ {x | f x = 0} ∈ l :=
begin
rw [is_O_iff], split,
{ rintros ⟨c, hc⟩,
filter_upwards [hc], dsimp,
intro x, rw [norm_zero, mul_zero], intro hx,
rw ←norm_eq_zero,
exact le_antisymm hx (norm_nonneg _) },
intro h, use 0,
filter_upwards [h], dsimp,
intros x hx,
rw [hx, norm_zero, zero_mul]
end
theorem is_o_zero (g : α → γ) (l : filter α) :
is_o (λ x, (0 : β)) g l :=
λ c cpos,
by { filter_upwards [univ_mem_sets], intros x _, simp,
exact mul_nonneg (le_of_lt cpos) (norm_nonneg _)}
theorem is_o_refl_left {f : α → β} {g : α → γ} {l : filter α} :
is_o (λ x, f x - f x) g l :=
by simpa using is_o_zero g l
theorem is_o_zero_right_iff {f : α → β} (l : filter α) :
is_o f (λ x, (0 : γ)) l ↔ {x | f x = 0} ∈ l :=
begin
split,
{ intro h, exact is_O_zero_right_iff.mp h.to_is_O },
intros h c cpos,
filter_upwards [h], dsimp,
intros x hx,
rw [hx, norm_zero, norm_zero, mul_zero]
end
end
section
variables [has_norm β] [normed_field γ]
theorem is_O_const_one (c : β) (l : filter α) :
is_O (λ x : α, c) (λ x, (1 : γ)) l :=
begin
rw is_O_iff,
refine ⟨∥c∥, _⟩,
simp only [norm_one, mul_one],
apply univ_mem_sets',
simp [le_refl],
end
end
section
variables [normed_field β] [normed_group γ]
theorem is_O_const_mul_left {f : α → β} {g : α → γ} {l : filter α} (h : is_O f g l) (c : β) :
is_O (λ x, c * f x) g l :=
begin
cases classical.em (c = 0) with h'' h'',
{ simp [h''], apply is_O_zero },
rcases h with ⟨c', c'pos, h'⟩,
have cne0 : ∥c∥ ≠ 0, from mt (norm_eq_zero _).mp h'',
have cpos : ∥c∥ > 0, from lt_of_le_of_ne (norm_nonneg _) (ne.symm cne0),
refine ⟨∥c∥ * c', mul_pos cpos c'pos, _⟩,
filter_upwards [h'], dsimp,
intros x h₀,
rw [normed_field.norm_mul, mul_assoc],
exact mul_le_mul_of_nonneg_left h₀ (norm_nonneg _)
end
theorem is_O_const_mul_left_iff {f : α → β} {g : α → γ} {l : filter α} {c : β} (hc : c ≠ 0) :
is_O (λ x, c * f x) g l ↔ is_O f g l :=
begin
split,
{ intro h,
convert is_O_const_mul_left h c⁻¹, ext,
rw [←mul_assoc, inv_mul_cancel hc, one_mul] },
intro h, apply is_O_const_mul_left h
end
theorem is_o_const_mul_left {f : α → β} {g : α → γ} {l : filter α}
(h : is_o f g l) (c : β) :
is_o (λ x, c * f x) g l :=
begin
cases classical.em (c = 0) with h'' h'',
{ simp [h''], apply is_o_zero },
have cne0 : ∥c∥ ≠ 0, from mt (norm_eq_zero _).mp h'',
have cpos : ∥c∥ > 0, from lt_of_le_of_ne (norm_nonneg _) (ne.symm cne0),
intros c' c'pos, dsimp,
filter_upwards [h (c' / ∥c∥) (div_pos c'pos cpos)], dsimp,
intros x hx, rw [normed_field.norm_mul],
apply le_trans (mul_le_mul_of_nonneg_left hx (le_of_lt cpos)),
rw [←mul_assoc, mul_div_cancel' _ cne0]
end
theorem is_o_const_mul_left_iff {f : α → β} {g : α → γ} {l : filter α} {c : β} (hc : c ≠ 0) :
is_o (λ x, c * f x) g l ↔ is_o f g l :=
begin
split,
{ intro h,
convert is_o_const_mul_left h c⁻¹, ext,
rw [←mul_assoc, inv_mul_cancel hc, one_mul] },
intro h',
apply is_o_const_mul_left h'
end
end
section
variables [normed_group β] [normed_field γ]
theorem is_O_of_is_O_const_mul_right {f : α → β} {g : α → γ} {l : filter α} {c : γ}
(h : is_O f (λ x, c * g x) l) :
is_O f g l :=
begin
cases classical.em (c = 0) with h' h',
{ simp [h', is_O_zero_right_iff] at h, rw is_O_congr_left h, apply is_O_zero },
have cne0 : ∥c∥ ≠ 0, from mt (norm_eq_zero _).mp h',
have cpos : ∥c∥ > 0, from lt_of_le_of_ne (norm_nonneg _) (ne.symm cne0),
rcases h with ⟨c', c'pos, h''⟩,
refine ⟨c' * ∥c∥, mul_pos c'pos cpos, _⟩,
convert h'', ext x, dsimp,
rw [normed_field.norm_mul, mul_assoc]
end
theorem is_O_const_mul_right_iff {f : α → β} {g : α → γ} {l : filter α} {c : γ} (hc : c ≠ 0) :
is_O f (λ x, c * g x) l ↔ is_O f g l :=
begin
split,
{ intro h, exact is_O_of_is_O_const_mul_right h },
intro h,
apply is_O_of_is_O_const_mul_right,
show is_O f (λ (x : α), c⁻¹ * (c * g x)) l,
convert h, ext, rw [←mul_assoc, inv_mul_cancel hc, one_mul]
end
theorem is_o_of_is_o_const_mul_right {f : α → β} {g : α → γ} {l : filter α} {c : γ}
(h : is_o f (λ x, c * g x) l) :
is_o f g l :=
begin
cases classical.em (c = 0) with h' h',
{ simp [h', is_o_zero_right_iff] at h, rw is_o_congr_left h, apply is_o_zero },
have cne0 : ∥c∥ ≠ 0, from mt (norm_eq_zero _).mp h',
have cpos : ∥c∥ > 0, from lt_of_le_of_ne (norm_nonneg _) (ne.symm cne0),
intros c' c'pos,
convert h (c' / ∥c∥) (div_pos c'pos cpos), dsimp,
ext x, rw [normed_field.norm_mul, ←mul_assoc, div_mul_cancel _ cne0]
end
theorem is_o_const_mul_right {f : α → β} {g : α → γ} {l : filter α} {c : γ} (hc : c ≠ 0) :
is_o f (λ x, c * g x) l ↔ is_o f g l :=
begin
split,
{ intro h, exact is_o_of_is_o_const_mul_right h },
intro h,
apply is_o_of_is_o_const_mul_right,
show is_o f (λ (x : α), c⁻¹ * (c * g x)) l,
convert h, ext, rw [←mul_assoc, inv_mul_cancel hc, one_mul]
end
theorem is_o_one_iff {f : α → β} {l : filter α} :
is_o f (λ x, (1 : γ)) l ↔ tendsto f l (nhds 0) :=
begin
rw [normed_space.tendsto_nhds_zero, is_o], split,
{ intros h e epos,
filter_upwards [h (e / 2) (half_pos epos)], simp,
intros x hx,
exact lt_of_le_of_lt hx (half_lt_self epos) },
intros h e epos,
filter_upwards [h e epos], simp,
intros x hx,
exact le_of_lt hx
end
end
section
variables [normed_group β] [normed_group γ]
theorem is_O.trans_tendsto {f : α → β} {g : α → γ} {l : filter α}
(h₁ : is_O f g l) (h₂ : tendsto g l (nhds 0)) :
tendsto f l (nhds 0) :=
(@is_o_one_iff _ _ ℝ _ _ _ _).1 $ h₁.trans_is_o $ is_o_one_iff.2 h₂
theorem is_o.trans_tendsto {f : α → β} {g : α → γ} {l : filter α}
(h₁ : is_o f g l) : tendsto g l (nhds 0) → tendsto f l (nhds 0) :=
h₁.to_is_O.trans_tendsto
end
section
variables [normed_field β] [normed_field γ]
theorem is_O_mul {f₁ f₂ : α → β} {g₁ g₂ : α → γ} {l : filter α}
(h₁ : is_O f₁ g₁ l) (h₂ : is_O f₂ g₂ l) :
is_O (λ x, f₁ x * f₂ x) (λ x, g₁ x * g₂ x) l :=
begin
rcases h₁ with ⟨c₁, c₁pos, hc₁⟩,
rcases h₂ with ⟨c₂, c₂pos, hc₂⟩,
refine ⟨c₁ * c₂, mul_pos c₁pos c₂pos, _⟩,
filter_upwards [hc₁, hc₂], dsimp,
intros x hx₁ hx₂,
rw [normed_field.norm_mul, normed_field.norm_mul, mul_assoc, mul_left_comm c₂, ←mul_assoc],
exact mul_le_mul hx₁ hx₂ (norm_nonneg _) (mul_nonneg (le_of_lt c₁pos) (norm_nonneg _))
end
theorem is_o_mul_left {f₁ f₂ : α → β} {g₁ g₂ : α → γ} {l : filter α}
(h₁ : is_O f₁ g₁ l) (h₂ : is_o f₂ g₂ l):
is_o (λ x, f₁ x * f₂ x) (λ x, g₁ x * g₂ x) l :=
begin
intros c cpos,
rcases h₁ with ⟨c₁, c₁pos, hc₁⟩,
filter_upwards [hc₁, h₂ (c / c₁) (div_pos cpos c₁pos)], dsimp,
intros x hx₁ hx₂,
rw [normed_field.norm_mul, normed_field.norm_mul],
apply le_trans (mul_le_mul hx₁ hx₂ (norm_nonneg _) (mul_nonneg (le_of_lt c₁pos) (norm_nonneg _))),
rw [mul_comm c₁, mul_assoc, mul_left_comm c₁, ←mul_assoc _ c₁, div_mul_cancel _ (ne_of_gt c₁pos)],
rw [mul_left_comm]
end
theorem is_o_mul_right {f₁ f₂ : α → β} {g₁ g₂ : α → γ} {l : filter α}
(h₁ : is_o f₁ g₁ l) (h₂ : is_O f₂ g₂ l):
is_o (λ x, f₁ x * f₂ x) (λ x, g₁ x * g₂ x) l :=
by convert is_o_mul_left h₂ h₁; simp only [mul_comm]
theorem is_o_mul {f₁ f₂ : α → β} {g₁ g₂ : α → γ} {l : filter α}
(h₁ : is_o f₁ g₁ l) (h₂ : is_o f₂ g₂ l):
is_o (λ x, f₁ x * f₂ x) (λ x, g₁ x * g₂ x) l :=
is_o_mul_left h₁.to_is_O h₂
end
/-
Note: the theorems in the next two sections can also be used for the integers, since
scalar multiplication is multiplication.
-/
section
variables {K : Type*} [normed_field K] [normed_space K β] [normed_group γ]
theorem is_O_const_smul_left {f : α → β} {g : α → γ} {l : filter α} (h : is_O f g l) (c : K) :
is_O (λ x, c • f x) g l :=
begin
rw [←is_O_norm_left], simp only [norm_smul],
apply is_O_const_mul_left,
rw [is_O_norm_left],
apply h
end
theorem is_O_const_smul_left_iff {f : α → β} {g : α → γ} {l : filter α} {c : K} (hc : c ≠ 0) :
is_O (λ x, c • f x) g l ↔ is_O f g l :=
begin
have cne0 : ∥c∥ ≠ 0, from mt (norm_eq_zero _).mp hc,
rw [←is_O_norm_left], simp only [norm_smul],
rw [is_O_const_mul_left_iff cne0, is_O_norm_left]
end
theorem is_o_const_smul_left {f : α → β} {g : α → γ} {l : filter α} (h : is_o f g l) (c : K) :
is_o (λ x, c • f x) g l :=
begin
rw [←is_o_norm_left], simp only [norm_smul],
apply is_o_const_mul_left,
rw [is_o_norm_left],
apply h
end
theorem is_o_const_smul_left_iff {f : α → β} {g : α → γ} {l : filter α} {c : K} (hc : c ≠ 0) :
is_o (λ x, c • f x) g l ↔ is_o f g l :=
begin
have cne0 : ∥c∥ ≠ 0, from mt (norm_eq_zero _).mp hc,
rw [←is_o_norm_left], simp only [norm_smul],
rw [is_o_const_mul_left_iff cne0, is_o_norm_left]
end
end
section
variables {K : Type*} [normed_group β] [normed_field K] [normed_space K γ]
theorem is_O_const_smul_right {f : α → β} {g : α → γ} {l : filter α} {c : K} (hc : c ≠ 0) :
is_O f (λ x, c • g x) l ↔ is_O f g l :=
begin
have cne0 : ∥c∥ ≠ 0, from mt (norm_eq_zero _).mp hc,
rw [←is_O_norm_right], simp only [norm_smul],
rw [is_O_const_mul_right_iff cne0, is_O_norm_right]
end
theorem is_o_const_smul_right {f : α → β} {g : α → γ} {l : filter α} {c : K} (hc : c ≠ 0) :
is_o f (λ x, c • g x) l ↔ is_o f g l :=
begin
have cne0 : ∥c∥ ≠ 0, from mt (norm_eq_zero _).mp hc,
rw [←is_o_norm_right], simp only [norm_smul],
rw [is_o_const_mul_right cne0, is_o_norm_right]
end
end
section
variables {K : Type*} [normed_field K] [normed_space K β] [normed_space K γ]
theorem is_O_smul {k : α → K} {f : α → β} {g : α → γ} {l : filter α} (h : is_O f g l) :
is_O (λ x, k x • f x) (λ x, k x • g x) l :=
begin
rw [←is_O_norm_left, ←is_O_norm_right], simp only [norm_smul],
apply is_O_mul (is_O_refl _ _),
rw [is_O_norm_left, is_O_norm_right],
exact h
end
theorem is_o_smul {k : α → K} {f : α → β} {g : α → γ} {l : filter α} (h : is_o f g l) :
is_o (λ x, k x • f x) (λ x, k x • g x) l :=
begin
rw [←is_o_norm_left, ←is_o_norm_right], simp only [norm_smul],
apply is_o_mul_left (is_O_refl _ _),
rw [is_o_norm_left, is_o_norm_right],
exact h
end
end
section
variables [normed_field β]
theorem tendsto_nhds_zero_of_is_o {f g : α → β} {l : filter α} (h : is_o f g l) :
tendsto (λ x, f x / (g x)) l (nhds 0) :=
have eq₁ : is_o (λ x, f x / g x) (λ x, g x / g x) l,
from is_o_mul_right h (is_O_refl _ _),
have eq₂ : is_O (λ x, g x / g x) (λ x, (1 : β)) l,
begin
use [1, zero_lt_one],
filter_upwards [univ_mem_sets], simp,
intro x,
cases classical.em (∥g x∥ = 0) with h' h'; simp [h'],
exact zero_le_one
end,
is_o_one_iff.mp (eq₁.trans_is_O eq₂)
private theorem is_o_of_tendsto {f g : α → β} {l : filter α}
(hgf : ∀ x, g x = 0 → f x = 0) (h : tendsto (λ x, f x / (g x)) l (nhds 0)) :
is_o f g l :=
have eq₁ : is_o (λ x, f x / (g x)) (λ x, (1 : β)) l,
from is_o_one_iff.mpr h,
have eq₂ : is_o (λ x, f x / g x * g x) g l,
by convert is_o_mul_right eq₁ (is_O_refl _ _); simp,
have eq₃ : is_O f (λ x, f x / g x * g x) l,
begin
use [1, zero_lt_one],
filter_upwards [univ_mem_sets], simp,
intro x,
cases classical.em (∥g x∥ = 0) with h' h',
{ rw hgf _ ((norm_eq_zero _).mp h'), simp },
rw [normed_field.norm_mul, norm_div, div_mul_cancel _ h']
end,
eq₃.trans_is_o eq₂
theorem is_o_iff_tendsto [normed_field β] {f g : α → β} {l : filter α}
(hgf : ∀ x, g x = 0 → f x = 0) :
is_o f g l ↔ tendsto (λ x, f x / (g x)) l (nhds 0) :=
iff.intro tendsto_nhds_zero_of_is_o (is_o_of_tendsto hgf)
end
end asymptotics
|
d077660b40ca7dfb14ec855ee3a55a1fc44aa1e8 | 367134ba5a65885e863bdc4507601606690974c1 | /src/algebra/lie/subalgebra.lean | cac8ada9f5f8a199750d0bc7088ca6adfdb1bf36 | [
"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 | 18,197 | 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.basic
import ring_theory.noetherian
/-!
# Lie subalgebras
This file defines Lie subalgebras of a Lie algebra and provides basic related definitions and
results.
## Main definitions
* `lie_subalgebra`
* `lie_subalgebra.incl`
* `lie_subalgebra.map`
* `lie_hom.range`
* `lie_equiv.of_injective`
* `lie_equiv.of_eq`
* `lie_equiv.of_subalgebra`
* `lie_equiv.of_subalgebras`
## Tags
lie algebra, lie subalgebra
-/
universes u v w w₁ w₂
section lie_subalgebra
variables (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L]
set_option old_structure_cmd true
/-- A Lie subalgebra of a Lie algebra is submodule that is closed under the Lie bracket.
This is a sufficient condition for the subset itself to form a Lie algebra. -/
structure lie_subalgebra extends submodule R L :=
(lie_mem' : ∀ {x y}, x ∈ carrier → y ∈ carrier → ⁅x, y⁆ ∈ carrier)
attribute [nolint doc_blame] lie_subalgebra.to_submodule
/-- The zero algebra is a subalgebra of any Lie algebra. -/
instance : has_zero (lie_subalgebra R L) :=
⟨{ lie_mem' := λ x y hx hy, by { rw [((submodule.mem_bot R).1 hx), zero_lie],
exact submodule.zero_mem (0 : submodule R L), },
..(0 : submodule R L) }⟩
instance : inhabited (lie_subalgebra R L) := ⟨0⟩
instance : has_coe (lie_subalgebra R L) (submodule R L) := ⟨lie_subalgebra.to_submodule⟩
instance : has_mem L (lie_subalgebra R L) := ⟨λ x L', x ∈ (L' : set L)⟩
/-- A Lie subalgebra forms a new Lie ring. -/
instance lie_subalgebra_lie_ring (L' : lie_subalgebra R L) : lie_ring L' :=
{ bracket := λ x y, ⟨⁅x.val, y.val⁆, L'.lie_mem' x.property y.property⟩,
lie_add := by { intros, apply set_coe.ext, apply lie_add, },
add_lie := by { intros, apply set_coe.ext, apply add_lie, },
lie_self := by { intros, apply set_coe.ext, apply lie_self, },
leibniz_lie := by { intros, apply set_coe.ext, apply leibniz_lie, } }
/-- A Lie subalgebra forms a new Lie algebra. -/
instance lie_subalgebra_lie_algebra (L' : lie_subalgebra R L) : lie_algebra R L' :=
{ lie_smul := by { intros, apply set_coe.ext, apply lie_smul } }
namespace lie_subalgebra
variables {R L} (L' : lie_subalgebra R L)
@[simp] lemma zero_mem : (0 : L) ∈ L' := (L' : submodule R L).zero_mem
lemma smul_mem (t : R) {x : L} (h : x ∈ L') : t • x ∈ L' := (L' : submodule R L).smul_mem t h
lemma add_mem {x y : L} (hx : x ∈ L') (hy : y ∈ L') : (x + y : L) ∈ L' :=
(L' : submodule R L).add_mem hx hy
lemma sub_mem {x y : L} (hx : x ∈ L') (hy : y ∈ L') : (x - y : L) ∈ L' :=
(L' : submodule R L).sub_mem hx hy
lemma lie_mem {x y : L} (hx : x ∈ L') (hy : y ∈ L') : (⁅x, y⁆ : L) ∈ L' := L'.lie_mem' hx hy
@[simp] lemma mem_carrier {x : L} : x ∈ L'.carrier ↔ x ∈ (L' : set L) := iff.rfl
@[simp] lemma mem_coe_submodule {x : L} : x ∈ (L' : submodule R L) ↔ x ∈ L' := iff.rfl
lemma mem_coe {x : L} : x ∈ (L' : set L) ↔ x ∈ L' := iff.rfl
@[simp, norm_cast] lemma coe_bracket (x y : L') : (↑⁅x, y⁆ : L) = ⁅(↑x : L), ↑y⁆ := rfl
lemma ext_iff (x y : L') : x = y ↔ (x : L) = y := subtype.ext_iff
lemma coe_zero_iff_zero (x : L') : (x : L) = 0 ↔ x = 0 := (ext_iff L' x 0).symm
@[ext] lemma ext (L₁' L₂' : lie_subalgebra R L) (h : ∀ x, x ∈ L₁' ↔ x ∈ L₂') :
L₁' = L₂' :=
by { cases L₁', cases L₂', simp only [], ext x, exact h x, }
lemma ext_iff' (L₁' L₂' : lie_subalgebra R L) : L₁' = L₂' ↔ ∀ x, x ∈ L₁' ↔ x ∈ L₂' :=
⟨λ h x, by rw h, ext L₁' L₂'⟩
@[simp] lemma mk_coe (S : set L) (h₁ h₂ h₃ h₄) :
((⟨S, h₁, h₂, h₃, h₄⟩ : lie_subalgebra R L) : set L) = S := rfl
@[simp] lemma coe_to_submodule_mk (p : submodule R L) (h) :
(({lie_mem' := h, ..p} : lie_subalgebra R L) : submodule R L) = p :=
by { cases p, refl, }
lemma coe_injective : function.injective (coe : lie_subalgebra R L → set L) :=
λ L₁' L₂' h, by cases L₁'; cases L₂'; congr'
@[norm_cast] theorem coe_set_eq (L₁' L₂' : lie_subalgebra R L) :
(L₁' : set L) = L₂' ↔ L₁' = L₂' := coe_injective.eq_iff
lemma to_submodule_injective :
function.injective (coe : lie_subalgebra R L → submodule R L) :=
λ L₁' L₂' h, by { rw submodule.ext'_iff at h, rw ← coe_set_eq, exact h, }
@[simp] lemma coe_to_submodule_eq_iff (L₁' L₂' : lie_subalgebra R L) :
(L₁' : submodule R L) = (L₂' : submodule R L) ↔ L₁' = L₂' :=
to_submodule_injective.eq_iff
@[norm_cast]
lemma coe_to_submodule : ((L' : submodule R L) : set L) = L' := rfl
end lie_subalgebra
variables {R L} {L₂ : Type w} [lie_ring L₂] [lie_algebra R L₂]
variables (f : L →ₗ⁅R⁆ L₂)
/-- The embedding of a Lie subalgebra into the ambient space as a Lie morphism. -/
def lie_subalgebra.incl (L' : lie_subalgebra R L) : L' →ₗ⁅R⁆ L :=
{ map_lie' := λ x y, by { rw [linear_map.to_fun_eq_coe, submodule.subtype_apply], refl, },
..L'.to_submodule.subtype }
namespace lie_hom
/-- The range of a morphism of Lie algebras is a Lie subalgebra. -/
def range : lie_subalgebra R L₂ :=
{ lie_mem' := λ x y,
show x ∈ f.to_linear_map.range → y ∈ f.to_linear_map.range → ⁅x, y⁆ ∈ f.to_linear_map.range,
by { repeat { rw linear_map.mem_range }, rintros ⟨x', hx⟩ ⟨y', hy⟩, refine ⟨⁅x', y'⁆, _⟩,
rw [←hx, ←hy], change f ⁅x', y'⁆ = ⁅f x', f y'⁆, rw map_lie, },
..(f : L →ₗ[R] L₂).range }
@[simp] lemma range_coe : (f.range : set L₂) = set.range f :=
linear_map.range_coe ↑f
@[simp] lemma mem_range (x : L₂) : x ∈ f.range ↔ ∃ (y : L), f y = x := linear_map.mem_range
lemma mem_range_self (x : L) : f x ∈ f.range := linear_map.mem_range_self f x
/-- We can restrict a morphism to a (surjective) map to its range. -/
def range_restrict : L →ₗ⁅R⁆ f.range :=
{ map_lie' := λ x y, by { apply subtype.ext, exact f.map_lie x y, },
..(f : L →ₗ[R] L₂).range_restrict, }
@[simp] lemma range_restrict_apply (x : L) : f.range_restrict x = ⟨f x, f.mem_range_self x⟩ := rfl
lemma surjective_range_restrict : function.surjective (f.range_restrict) :=
begin
rintros ⟨y, hy⟩,
erw mem_range at hy, obtain ⟨x, rfl⟩ := hy,
use x,
simp only [subtype.mk_eq_mk, range_restrict_apply],
end
end lie_hom
lemma submodule.exists_lie_subalgebra_coe_eq_iff (p : submodule R L) :
(∃ (K : lie_subalgebra R L), ↑K = p) ↔ ∀ (x y : L), x ∈ p → y ∈ p → ⁅x, y⁆ ∈ p :=
begin
split,
{ rintros ⟨K, rfl⟩, exact K.lie_mem', },
{ intros h, use { lie_mem' := h, ..p }, exact lie_subalgebra.coe_to_submodule_mk p _, },
end
namespace lie_subalgebra
variables (K K' : lie_subalgebra R L) (K₂ : lie_subalgebra R L₂)
@[simp] lemma incl_range : K.incl.range = K :=
by { rw ← coe_to_submodule_eq_iff, exact (K : submodule R L).range_subtype, }
/-- The image of a Lie subalgebra under a Lie algebra morphism is a Lie subalgebra of the
codomain. -/
def map : lie_subalgebra R L₂ :=
{ lie_mem' := λ x y hx hy, by {
erw submodule.mem_map at hx, rcases hx with ⟨x', hx', hx⟩, rw ←hx,
erw submodule.mem_map at hy, rcases hy with ⟨y', hy', hy⟩, rw ←hy,
erw submodule.mem_map,
exact ⟨⁅x', y'⁆, K.lie_mem hx' hy', f.map_lie x' y'⟩, },
..((K : submodule R L).map (f : L →ₗ[R] L₂)) }
@[simp] lemma mem_map (x : L₂) : x ∈ K.map f ↔ ∃ (y : L), y ∈ K ∧ f y = x := submodule.mem_map
-- TODO Rename and state for homs instead of equivs.
@[simp] lemma mem_map_submodule (e : L ≃ₗ⁅R⁆ L₂) (x : L₂) :
x ∈ K.map (e : L →ₗ⁅R⁆ L₂) ↔ x ∈ (K : submodule R L).map (e : L →ₗ[R] L₂) :=
iff.rfl
/-- The preimage of a Lie subalgebra under a Lie algebra morphism is a Lie subalgebra of the
domain. -/
def comap : lie_subalgebra R L :=
{ lie_mem' := λ x y hx hy, by
{ suffices : ⁅f x, f y⁆ ∈ K₂, by { simp [this], }, exact K₂.lie_mem hx hy, },
..((K₂ : submodule R L₂).comap (f : L →ₗ[R] L₂)), }
section lattice_structure
open set
instance : partial_order (lie_subalgebra R L) :=
{ le := λ N N', ∀ ⦃x⦄, x ∈ N → x ∈ N', -- Overriding `le` like this gives a better defeq.
..partial_order.lift (coe : lie_subalgebra R L → set L) coe_injective }
lemma le_def : K ≤ K' ↔ (K : set L) ⊆ K' := iff.rfl
@[simp, norm_cast] lemma coe_submodule_le_coe_submodule : (K : submodule R L) ≤ K' ↔ K ≤ K' :=
iff.rfl
instance : has_bot (lie_subalgebra R L) := ⟨0⟩
@[simp] lemma bot_coe : ((⊥ : lie_subalgebra R L) : set L) = {0} := rfl
@[simp] lemma bot_coe_submodule : ((⊥ : lie_subalgebra R L) : submodule R L) = ⊥ := rfl
@[simp] lemma mem_bot (x : L) : x ∈ (⊥ : lie_subalgebra R L) ↔ x = 0 := mem_singleton_iff
instance : has_top (lie_subalgebra R L) :=
⟨{ lie_mem' := λ x y hx hy, mem_univ ⁅x, y⁆,
..(⊤ : submodule R L) }⟩
@[simp] lemma top_coe : ((⊤ : lie_subalgebra R L) : set L) = univ := rfl
@[simp] lemma top_coe_submodule : ((⊤ : lie_subalgebra R L) : submodule R L) = ⊤ := rfl
@[simp] lemma mem_top (x : L) : x ∈ (⊤ : lie_subalgebra R L) := mem_univ x
instance : has_inf (lie_subalgebra R L) :=
⟨λ K K', { lie_mem' := λ x y hx hy, mem_inter (K.lie_mem hx.1 hy.1) (K'.lie_mem hx.2 hy.2),
..(K ⊓ K' : submodule R L) }⟩
instance : has_Inf (lie_subalgebra R L) :=
⟨λ S, { lie_mem' := λ x y hx hy, by
{ simp only [submodule.mem_carrier, mem_Inter, submodule.Inf_coe, mem_set_of_eq,
forall_apply_eq_imp_iff₂, exists_imp_distrib] at *,
intros K hK, exact K.lie_mem (hx K hK) (hy K hK), },
..Inf {(s : submodule R L) | s ∈ S} }⟩
@[simp] theorem inf_coe : (↑(K ⊓ K') : set L) = K ∩ K' := rfl
@[simp] lemma Inf_coe_to_submodule (S : set (lie_subalgebra R L)) :
(↑(Inf S) : submodule R L) = Inf {(s : submodule R L) | s ∈ S} := rfl
@[simp] lemma Inf_coe (S : set (lie_subalgebra R L)) : (↑(Inf S) : set L) = ⋂ s ∈ S, (s : set L) :=
begin
rw [← coe_to_submodule, Inf_coe_to_submodule, submodule.Inf_coe],
ext x,
simpa only [mem_Inter, mem_set_of_eq, forall_apply_eq_imp_iff₂, exists_imp_distrib],
end
lemma Inf_glb (S : set (lie_subalgebra R L)) : is_glb S (Inf S) :=
begin
have h : ∀ (K K' : lie_subalgebra R L), (K : set L) ≤ K' ↔ K ≤ K', { intros, exact iff.rfl, },
simp only [is_glb.of_image h, Inf_coe, is_glb_binfi],
end
/-- The set of Lie subalgebras of a Lie algebra form a complete lattice.
We provide explicit values for the fields `bot`, `top`, `inf` to get more convenient definitions
than we would otherwise obtain from `complete_lattice_of_Inf`. -/
instance : complete_lattice (lie_subalgebra R L) :=
{ bot := ⊥,
bot_le := λ N _ h, by { rw mem_bot at h, rw h, exact N.zero_mem', },
top := ⊤,
le_top := λ _ _ _, trivial,
inf := (⊓),
le_inf := λ N₁ N₂ N₃ h₁₂ h₁₃ m hm, ⟨h₁₂ hm, h₁₃ hm⟩,
inf_le_left := λ _ _ _, and.left,
inf_le_right := λ _ _ _, and.right,
..complete_lattice_of_Inf _ Inf_glb }
instance : add_comm_monoid (lie_subalgebra R L) :=
{ add := (⊔),
add_assoc := λ _ _ _, sup_assoc,
zero := ⊥,
zero_add := λ _, bot_sup_eq,
add_zero := λ _, sup_bot_eq,
add_comm := λ _ _, sup_comm, }
@[simp] lemma add_eq_sup : K + K' = K ⊔ K' := rfl
@[norm_cast, simp] lemma inf_coe_to_submodule :
(↑(K ⊓ K') : submodule R L) = (K : submodule R L) ⊓ (K' : submodule R L) := rfl
@[simp] lemma mem_inf (x : L) : x ∈ K ⊓ K' ↔ x ∈ K ∧ x ∈ K' :=
by rw [← mem_coe_submodule, ← mem_coe_submodule, ← mem_coe_submodule, inf_coe_to_submodule,
submodule.mem_inf]
lemma eq_bot_iff : K = ⊥ ↔ ∀ (x : L), x ∈ K → x = 0 :=
by { rw eq_bot_iff, exact iff.rfl, }
-- TODO[gh-6025]: make this an instance once safe to do so
lemma subsingleton_of_bot : subsingleton (lie_subalgebra R ↥(⊥ : lie_subalgebra R L)) :=
begin
apply subsingleton_of_bot_eq_top,
ext ⟨x, hx⟩, change x ∈ ⊥ at hx, rw submodule.mem_bot at hx, subst hx,
simp only [true_iff, eq_self_iff_true, submodule.mk_eq_zero, mem_bot],
end
variables (R L)
lemma well_founded_of_noetherian [is_noetherian R L] :
well_founded ((>) : lie_subalgebra R L → lie_subalgebra R L → Prop) :=
begin
let f : ((>) : lie_subalgebra R L → lie_subalgebra R L → Prop) →r
((>) : submodule R L → submodule R L → Prop) :=
{ to_fun := coe,
map_rel' := λ N N' h, h, },
apply f.well_founded, rw ← is_noetherian_iff_well_founded, apply_instance,
end
variables {R L K K' f}
section nested_subalgebras
variables (h : K ≤ K')
/-- Given two nested Lie subalgebras `K ⊆ K'`, the inclusion `K ↪ K'` is a morphism of Lie
algebras. -/
def hom_of_le : K →ₗ⁅R⁆ K' :=
{ map_lie' := λ x y, rfl,
..submodule.of_le h }
@[simp] lemma coe_hom_of_le (x : K) : (hom_of_le h x : L) = x := rfl
lemma hom_of_le_apply (x : K) : hom_of_le h x = ⟨x.1, h x.2⟩ := rfl
lemma hom_of_le_injective : function.injective (hom_of_le h) :=
λ x y, by simp only [hom_of_le_apply, imp_self, subtype.mk_eq_mk, submodule.coe_eq_coe,
subtype.val_eq_coe]
/-- Given two nested Lie subalgebras `K ⊆ K'`, we can view `K` as a Lie subalgebra of `K'`,
regarded as Lie algebra in its own right. -/
def of_le : lie_subalgebra R K' := (hom_of_le h).range
@[simp] lemma mem_of_le (x : K') : x ∈ of_le h ↔ (x : L) ∈ K :=
begin
simp only [of_le, hom_of_le_apply, lie_hom.mem_range],
split,
{ rintros ⟨y, rfl⟩, exact y.property, },
{ intros h, use ⟨(x : L), h⟩, simp, },
end
lemma of_le_eq_comap_incl : of_le h = K.comap K'.incl :=
by { ext, rw mem_of_le, refl, }
end nested_subalgebras
lemma map_le_iff_le_comap {K : lie_subalgebra R L} {K' : lie_subalgebra R L₂} :
map f K ≤ K' ↔ K ≤ comap f K' := set.image_subset_iff
lemma gc_map_comap : galois_connection (map f) (comap f) := λ K K', map_le_iff_le_comap
end lattice_structure
section lie_span
variables (R L) (s : set L)
/-- The Lie subalgebra of a Lie algebra `L` generated by a subset `s ⊆ L`. -/
def lie_span : lie_subalgebra R L := Inf {N | s ⊆ N}
variables {R L s}
lemma mem_lie_span {x : L} : x ∈ lie_span R L s ↔ ∀ K : lie_subalgebra R L, s ⊆ K → x ∈ K :=
by { change x ∈ (lie_span R L s : set L) ↔ _, erw Inf_coe, exact set.mem_bInter_iff, }
lemma subset_lie_span : s ⊆ lie_span R L s :=
by { intros m hm, erw mem_lie_span, intros K hK, exact hK hm, }
lemma submodule_span_le_lie_span : submodule.span R s ≤ lie_span R L s :=
by { rw submodule.span_le, apply subset_lie_span, }
lemma lie_span_le {K} : lie_span R L s ≤ K ↔ s ⊆ K :=
begin
split,
{ exact set.subset.trans subset_lie_span, },
{ intros hs m hm, rw mem_lie_span at hm, exact hm _ hs, },
end
lemma lie_span_mono {t : set L} (h : s ⊆ t) : lie_span R L s ≤ lie_span R L t :=
by { rw lie_span_le, exact set.subset.trans h subset_lie_span, }
lemma lie_span_eq : lie_span R L (K : set L) = K :=
le_antisymm (lie_span_le.mpr rfl.subset) subset_lie_span
lemma coe_lie_span_submodule_eq_iff {p : submodule R L} :
(lie_span R L (p : set L) : submodule R L) = p ↔ ∃ (K : lie_subalgebra R L), ↑K = p :=
begin
rw p.exists_lie_subalgebra_coe_eq_iff, split; intros h,
{ intros x m hm, rw [← h, mem_coe_submodule], exact lie_mem _ (subset_lie_span hm), },
{ rw [← coe_to_submodule_mk p h, coe_to_submodule, coe_to_submodule_eq_iff, lie_span_eq], },
end
end lie_span
end lie_subalgebra
end lie_subalgebra
namespace lie_equiv
variables {R : Type u} {L₁ : Type v} {L₂ : Type w}
variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_algebra R L₁] [lie_algebra R L₂]
/-- An injective Lie algebra morphism is an equivalence onto its range. -/
noncomputable def of_injective (f : L₁ →ₗ⁅R⁆ L₂) (h : function.injective f) :
L₁ ≃ₗ⁅R⁆ f.range :=
have h' : (f : L₁ →ₗ[R] L₂).ker = ⊥ := linear_map.ker_eq_bot_of_injective h,
{ map_lie' := λ x y, by { apply set_coe.ext, simpa, },
..(linear_equiv.of_injective ↑f h')}
@[simp] lemma of_injective_apply (f : L₁ →ₗ⁅R⁆ L₂) (h : function.injective f) (x : L₁) :
↑(of_injective f h x) = f x := rfl
variables (L₁' L₁'' : lie_subalgebra R L₁) (L₂' : lie_subalgebra R L₂)
/-- Lie subalgebras that are equal as sets are equivalent as Lie algebras. -/
def of_eq (h : (L₁' : set L₁) = L₁'') : L₁' ≃ₗ⁅R⁆ L₁'' :=
{ map_lie' := λ x y, by { apply set_coe.ext, simp, },
..(linear_equiv.of_eq ↑L₁' ↑L₁''
(by {ext x, change x ∈ (L₁' : set L₁) ↔ x ∈ (L₁'' : set L₁), rw h, } )) }
@[simp] lemma of_eq_apply (L L' : lie_subalgebra R L₁) (h : (L : set L₁) = L') (x : L) :
(↑(of_eq L L' h x) : L₁) = x := rfl
variables (e : L₁ ≃ₗ⁅R⁆ L₂)
/-- An equivalence of Lie algebras restricts to an equivalence from any Lie subalgebra onto its
image. -/
def of_subalgebra : L₁'' ≃ₗ⁅R⁆ (L₁''.map e : lie_subalgebra R L₂) :=
{ map_lie' := λ x y, by { apply set_coe.ext, exact lie_hom.map_lie (↑e : L₁ →ₗ⁅R⁆ L₂) ↑x ↑y, }
..(linear_equiv.of_submodule (e : L₁ ≃ₗ[R] L₂) ↑L₁'') }
@[simp] lemma of_subalgebra_apply (x : L₁'') : ↑(e.of_subalgebra _ x) = e x := rfl
/-- An equivalence of Lie algebras restricts to an equivalence from any Lie subalgebra onto its
image. -/
def of_subalgebras (h : L₁'.map ↑e = L₂') : L₁' ≃ₗ⁅R⁆ L₂' :=
{ map_lie' := λ x y, by { apply set_coe.ext, exact lie_hom.map_lie (↑e : L₁ →ₗ⁅R⁆ L₂) ↑x ↑y, },
..(linear_equiv.of_submodules (e : L₁ ≃ₗ[R] L₂) ↑L₁' ↑L₂' (by { rw ←h, refl, })) }
@[simp] lemma of_subalgebras_apply (h : L₁'.map ↑e = L₂') (x : L₁') :
↑(e.of_subalgebras _ _ h x) = e x := rfl
@[simp] lemma of_subalgebras_symm_apply (h : L₁'.map ↑e = L₂') (x : L₂') :
↑((e.of_subalgebras _ _ h).symm x) = e.symm x := rfl
end lie_equiv
|
606087423aab3e5600c64c36cb3d6cf3647122ac | ebbdcbd7ddc89a9ef7c3b397b301d5f5272a918f | /qp/p1_categories/c2_limits/s3_pullbacks.lean | 9ec1c862362da97f1dde04e96177d98def6db61a | [] | no_license | intoverflow/qvr | 34b9ef23604738381ca20b7d622fd0399d88f2dd | 0cfcd33fe4bf8d93851a00cec5bfd21e77105d74 | refs/heads/master | 1,616,591,570,371 | 1,492,575,772,000 | 1,492,575,772,000 | 80,061,627 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 27,573 | lean | /- -----------------------------------------------------------------------
Pullbacks and pushouts.
----------------------------------------------------------------------- -/
import .s1_limits
import .s2_products
namespace qp
open stdaux
universe variables ℓobjx ℓhomx ℓobj ℓhom ℓobj₁ ℓhom₁ ℓobj₂ ℓhom₂
/- -----------------------------------------------------------------------
Pullbacks.
----------------------------------------------------------------------- -/
/-! #brief Homs in a cospan category.
-/
inductive CoSpanHom (N : ℕ) : option (fin N) → option (fin N) → Type
| id : ∀ (x : option (fin N)), CoSpanHom x x
| hom : ∀ (n : fin N), CoSpanHom (some n) none
/-! #brief A cospan category.
-/
definition CoSpanCat (N : ℕ) : Cat.{0 1}
:= { obj := option (fin N)
, hom := CoSpanHom N
, id := CoSpanHom.id
, circ := λ x y z g f, begin cases f, { exact g }, { cases g, apply CoSpanHom.hom } end
, circ_assoc := λ x y z w h g f, begin cases f, { trivial }, { cases g, trivial } end
, circ_id_left := λ x y f, begin cases f, { trivial }, { trivial } end
, circ_id_right := λ x y f, begin cases f, { trivial }, { trivial } end
}
/-! #brief Functor which forgets the base hom.
-/
definition CoSpanCat.forget_base (N : ℕ)
: Fun (CoSpanCat N) (CoSpanCat (nat.succ N))
:= { obj := λ n, option.cases_on n option.none (λ n', option.some (stdaux.fin.add n' 1))
, hom := λ x y f
, begin
cases f,
{ apply CoSpanHom.id },
{ apply CoSpanHom.hom }
end
, hom_id := λ x, rfl
, hom_circ := λ x y z g f
, begin
cases f,
{ trivial },
{ cases g, trivial }
end
}
/-! #brief A cospan diagram.
-/
definition PullbackDrgm (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
: Fun (CoSpanCat (list.length factor)) C
:= { obj := λ a, option.cases_on a t (list.get factor)
, hom := λ a₁ a₂ f, begin cases f, { apply C^.id }, { exact HomsIn.get maps n } end
, hom_id := λ a, rfl
, hom_circ := λ a₁ a₂ a₃ g f
, begin
cases f,
{ apply eq.symm C^.circ_id_right },
{ cases g, apply eq.symm C^.circ_id_left }
end
}
/-! #brief Every functor out of CoSpanCat is a PullbackDrgm.
-/
definition PullbackDrgm.mk_doms {C : Cat.{ℓobj ℓhom}}
: ∀ {N : ℕ}
(F : Fun (CoSpanCat N) C)
, list C^.obj
| 0 F := []
| (nat.succ N) F := (F^.obj (some (fin_of 0))) :: @PullbackDrgm.mk_doms N (F □□ CoSpanCat.forget_base N)
/-! #brief Every functor out of CoSpanCat is a PullbackDrgm.
-/
theorem PullbackDrgm.length_mk_doms {C : Cat.{ℓobj ℓhom}}
: ∀ {N : ℕ}
(F : Fun (CoSpanCat N) C)
, list.length (PullbackDrgm.mk_doms F) = N
| 0 F := rfl
| (nat.succ N) F := congr_arg nat.succ (@PullbackDrgm.length_mk_doms N (F □□ CoSpanCat.forget_base N))
/-! #brief Every functor out of CoSpanCat is a PullbackDrgm.
-/
definition PullbackDrgm.mk_homs {C : Cat.{ℓobj ℓhom}}
: ∀ {N : ℕ}
(F : Fun (CoSpanCat N) C)
, @HomsIn C (PullbackDrgm.mk_doms F) (F^.obj none)
| 0 F := HomsIn.nil
| (nat.succ N) F
:= HomsIn.cons (F^.hom (CoSpanHom.hom (fin_of 0)))
(@PullbackDrgm.mk_homs N (F □□ CoSpanCat.forget_base N))
/-! #brief Every functor out of CoSpanCat is a PullbackDrgm.
-/
theorem PullbackDrgm.uniq {C : Cat.{ℓobj ℓhom}}
{N : ℕ}
(F : Fun (CoSpanCat N) C)
: PullbackDrgm C (PullbackDrgm.mk_homs F) == F
:= begin
apply Fun.heq,
{ exact congr_arg CoSpanCat (PullbackDrgm.length_mk_doms F) },
{ trivial },
{ intros n₁ n₂ ωn,
dsimp [PullbackDrgm],
cases n₁ with n₁ ωn₁,
{ cases n₂ with n₂ ωn₂,
{ trivial },
{ exact sorry } -- TODO
},
{ cases n₂ with n₂ ωn₂,
{ exact sorry }, -- TODO
{ exact sorry } -- TODO
}
},
{ intros x₁ y₁ x₂ y₂ f₁ f₂ ωf,
cases f₁,
{ cases f₂,
{ exact sorry }, -- TODO
{ exact sorry } -- TODO
},
{ cases f₂,
{ exact sorry }, -- TODO
{ exact sorry } -- TODO
}
}
end
/-! #brief A cone over a pullback.
-/
definition PullbackCone (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
: Type (max ℓobj ℓhom)
:= Cone (PullbackDrgm C maps)
/-! #brief Helper for making a pullback cone.
-/
definition PullbackCone.mk {C : Cat.{ℓobj ℓhom}}
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
(c : C^.obj)
(to_t : C^.hom c t)
(proj : HomsOut c factor)
(ωproj : HomsList.repeat to_t (list.length factor)
= homs_in_comp_out maps proj)
: PullbackCone C maps
:= { obj := c
, hom := λ x, option.cases_on x to_t (HomsOut.get proj)
, comm := λ x₁ x₂ f, begin
cases f,
{ exact eq.symm C^.circ_id_left },
{ unfold PullbackDrgm,
apply eq_of_heq,
refine heq.trans (heq.symm (HomsList.get_repeat to_t n)) _,
refine heq.trans _ (get_homs_in_comp_out maps proj),
rw ωproj
}
end
}
/-! #brief The projections out of a pullback cone.
-/
definition PullbackCone.Proj {C : Cat.{ℓobj ℓhom}}
{factor : list C^.obj} {t : C^.obj}
{maps : HomsIn factor t}
(cone : PullbackCone C maps)
: HomsOut cone^.obj factor
:= sorry -- HomsOut.enum (Cone.hom cone)
/-! #brief A pullback in a category.
-/
@[class] definition HasPullback (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
:= HasLimit (PullbackDrgm C maps)
instance HasPullback.HasLimit {C : Cat.{ℓobj ℓhom}}
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
[maps_HasPullback : HasPullback C maps]
: HasLimit (PullbackDrgm C maps)
:= maps_HasPullback
/-! #brief A category with all pullbacks.
-/
class HasAllPullbacks (C : Cat.{ℓobj ℓhom})
:= (has_pullback : ∀ {base : C^.obj} {factor : list C^.obj} {t : C^.obj}
(maps : HomsIn (base :: factor) t)
, HasPullback C maps)
instance HasAllPullbacks.HasPullback (C : Cat.{ℓobj ℓhom})
[C_HasAllPullbacks : HasAllPullbacks C]
{base : C^.obj} {factor : list C^.obj} {t : C^.obj}
(maps : HomsIn (base :: factor) t)
: HasPullback C maps
:= HasAllPullbacks.has_pullback maps
instance HasAllPullbacks.HasAllLimitsFrom (C : Cat.{ℓobj ℓhom})
[C_HasAllPullbacks : HasAllPullbacks C]
(N : ℕ)
: HasAllLimitsFrom C (CoSpanCat (nat.succ N))
:= { has_limit := λ L, let l := HasAllPullbacks.HasPullback C (PullbackDrgm.mk_homs L)
in cast (HasLimit.heq begin rw PullbackDrgm.length_mk_doms end rfl (PullbackDrgm.uniq L)) l
}
/-! #brief A category with all pullbacks along a given hom.
-/
class HasPullbacksAlong (C : Cat.{ℓobj ℓhom})
{base t : C^.obj} (f : C^.hom base t)
:= (has_pullback : ∀ {y : C^.obj} (map : C^.hom y t)
, HasPullback C (f ↗→ map ↗→↗))
instance HasPullbacksAlong.HasPullback (C : Cat.{ℓobj ℓhom})
{base t : C^.obj} (f : C^.hom base t)
{y : C^.obj} (map : C^.hom y t)
[f_HasPullbacksAlong : HasPullbacksAlong C f]
: HasPullback C (f ↗→ map ↗→↗)
:= HasPullbacksAlong.has_pullback f map
instance HasAllPullbacks.HasPullbacksAlong (C : Cat.{ℓobj ℓhom})
[C_HasAllPullbacks : HasAllPullbacks C]
{base t : C^.obj} (f : C^.hom base t)
: HasPullbacksAlong C f
:= { has_pullback := λ y map, HasAllPullbacks.has_pullback (f ↗→ map ↗→↗)
}
/-! #brief Helper for showing a category has a pullback.
-/
definition HasPullback.show (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
(p : C^.obj)
(to_t : C^.hom p t)
(proj : HomsOut p factor)
(ωproj : HomsList.repeat to_t (list.length factor)
= homs_in_comp_out maps proj)
(univ : ∀ {c : C^.obj} (to_t' : C^.hom c t) (hom : HomsOut c factor)
(ωhom : HomsList.repeat
to_t'
(list.length factor)
= homs_in_comp_out maps hom)
, C^.hom c p)
(ωuniv : ∀ {c : C^.obj} (to_t' : C^.hom c t) (hom : HomsOut c factor)
(ωhom : HomsList.repeat
to_t'
(list.length factor)
= homs_in_comp_out maps hom)
, hom = HomsOut.comp proj (univ to_t' hom ωhom))
(ωuniq : ∀ {c : C^.obj} (to_t' : C^.hom c t) (hom : HomsOut c factor)
(ωhom : HomsList.repeat
to_t'
(list.length factor)
= homs_in_comp_out maps hom)
(h : C^.hom c p)
(ωcomm : hom = HomsOut.comp proj h)
, h = univ to_t' hom ωhom)
: HasPullback C maps
:= HasLimit.show p (λ x, option.cases_on x to_t ((HomsOut.get proj)))
(λ x₁ x₂ f, begin
cases f,
{ exact eq.symm C^.circ_id_left },
{ unfold PullbackDrgm,
apply eq_of_heq,
refine heq.trans (heq.symm (HomsList.get_repeat to_t n)) _,
refine heq.trans _ (get_homs_in_comp_out maps proj),
rw ωproj
}
end)
(λ c hom ωcomm, univ (hom none) (HomsOut.enum (λ n, hom (some n)))
begin
exact sorry
end)
(λ c hom ωcomm a, begin exact sorry end)
(λ c hom ωcomm h ωh, begin exact sorry end)
/-! #brief Pullbacks are cones.
-/
definition pullback.cone (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
[maps_HasPullback : HasPullback C maps]
: PullbackCone C maps
:= limit.cone (PullbackDrgm C maps)
/-! #brief The pullback of a collection of homs.
-/
definition pullback (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
[maps_HasPullback : HasPullback C maps]
: C^.obj
:= limit (PullbackDrgm C maps)
/-! #brief Projection out of a pullback.
-/
definition pullback.π (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
[maps_HasPullback : HasPullback C maps]
(n : fin (list.length factor))
: C^.hom (pullback C maps) (list.get factor n)
:= limit.out (PullbackDrgm C maps) (some n)
/-! #brief The commutative square property of pullbacks.
-/
definition pullback.π_comm (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
[maps_HasPullback : HasPullback C maps]
(n₁ n₂ : fin (list.length factor))
: HomsIn.get maps n₁ ∘∘ pullback.π C maps n₁
= HomsIn.get maps n₂ ∘∘ pullback.π C maps n₂
:= sorry
/-! #brief Projection out of a pullback to the base.
-/
definition pullback.πbase (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
[maps_HasPullback : HasPullback C maps]
: C^.hom (pullback C maps) t
:= limit.out (PullbackDrgm C maps) none
/-! #brief Every cone is mediated through the pullback.
-/
definition pullback.univ (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
[maps_HasPullback : HasPullback C maps]
(c : PullbackCone C maps)
: C^.hom c^.obj (pullback C maps)
:= limit.univ _ c
/-! #brief Every cone is mediated through the pullback.
-/
definition pullback.univ.mediates (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
{maps_HasPullback : HasPullback C maps}
(c : PullbackCone C maps)
(n : fin (list.length factor))
: c^.hom (some n) = C^.circ (@pullback.π C factor t maps maps_HasPullback n) (pullback.univ C maps c)
:= limit.univ.mediates c (some n)
/-! #brief Every cone is mediated through the pullback.
-/
definition pullback.univ.mediates_base (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
{maps_HasPullback : HasPullback C maps}
(c : PullbackCone C maps)
: c^.hom none = C^.circ (@pullback.πbase C factor t maps maps_HasPullback) (pullback.univ C maps c)
:= limit.univ.mediates c none
/-! #brief The mediating map from the cone to the pullback is unique.
-/
definition pullback.univ.uniq (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
{maps_HasPullback : HasPullback C maps}
(c : PullbackCone C maps)
(m : C^.hom c^.obj (pullback C maps))
(ω : ∀ (n : fin (list.length factor))
, c^.hom (some n) = (@pullback.π C factor t maps maps_HasPullback n) ∘∘ m)
(ωbase : c^.hom none = (@pullback.πbase C factor t maps maps_HasPullback) ∘∘ m)
: m = pullback.univ C maps c
:= limit.univ.uniq c m (λ x, option.cases_on x ωbase ω)
/-! #brief The unique iso between two pullbacks of the same homs.
-/
definition pullback.iso {C : Cat.{ℓobj ℓhom}}
{factor : list C^.obj} {t : C^.obj}
{maps : HomsIn factor t}
(maps_HasPullback₁ maps_HasPullback₂ : HasPullback C maps)
: C^.hom (@pullback C factor t maps maps_HasPullback₁)
(@pullback C factor t maps maps_HasPullback₂)
:= limit.iso maps_HasPullback₁ maps_HasPullback₂
/-! #brief Pullbacks are unique up-to unique isomorphism.
-/
definition pullback.uniq {C : Cat.{ℓobj ℓhom}}
{factor : list C^.obj} {t : C^.obj}
{maps : HomsIn factor t}
(maps_HasPullback₁ maps_HasPullback₂ : HasPullback C maps)
: Iso (pullback.iso maps_HasPullback₁ maps_HasPullback₂)
(pullback.iso maps_HasPullback₂ maps_HasPullback₁)
:= limit.uniq maps_HasPullback₁ maps_HasPullback₂
/- -----------------------------------------------------------------------
Pullbacks in functor categories.
----------------------------------------------------------------------- -/
/-! #brief Pullbacks in functor categories can be computed pointwise.
-/
instance FunCat.HasPullback {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
[D_HasAllPullbacks : HasAllPullbacks D]
{base : Fun C D} {factor : list (Fun C D)} {t : Fun C D}
(maps : @HomsIn (FunCat C D) (base :: factor) t)
: HasPullback (FunCat C D) maps
:= @FunCat.HasLimit _ _ _ (HasAllPullbacks.HasAllLimitsFrom D _) (PullbackDrgm (FunCat C D) maps)
/-! #brief Pullbacks in functor categories can be computed pointwise.
-/
instance FunCat.HasAllPullbacks {C : Cat.{ℓobj₁ ℓhom₁}} {D : Cat.{ℓobj₂ ℓhom₂}}
[D_HasAllPullbacks : HasAllPullbacks D]
: HasAllPullbacks (FunCat C D)
:= { has_pullback := λ base factor t maps, FunCat.HasPullback maps
}
/- -----------------------------------------------------------------------
Pullback squares.
----------------------------------------------------------------------- -/
/-! #brief A pullback square.
-/
class IsPullback {C : Cat.{ℓobj ℓhom}}
{p x y t : C^.obj}
(base : C^.hom x t) (p₁ : C^.hom p x)
(map : C^.hom y t) (p₂ : C^.hom p y)
:= (has_pullback : HasPullback C (base ↗→ map ↗→↗))
(ωpullback : @pullback C [x, y] t _ has_pullback = p)
(ωπ₁ : @pullback.π C [x, y] t _ has_pullback (@fin_of 1 0) = p₁ ∘∘ cast_hom ωpullback)
(ωπ₂ : @pullback.π C [x, y] t _ has_pullback (@fin_of 0 1) = p₂ ∘∘ cast_hom ωpullback)
/-! #brief Pullback squares have the usual commutative diagram.
-/
theorem ispullback.square {C : Cat.{ℓobj ℓhom}}
{p x y t : C^.obj}
{base : C^.hom x t} {p₁ : C^.hom p x}
{map : C^.hom y t} {p₂ : C^.hom p y}
(isPullback : IsPullback base p₁ map p₂)
: C^.circ base p₁ = C^.circ map p₂
:= sorry
/-! #brief The universal map into a pullback square.
-/
definition ispullback.univ {C : Cat.{ℓobj ℓhom}}
{p x y t : C^.obj}
{base : C^.hom x t} {p₁ : C^.hom p x}
{map : C^.hom y t} {p₂ : C^.hom p y}
(isPullback : IsPullback base p₁ map p₂)
{c : C^.obj} (h₁ : C^.hom c x) (h₂ : C^.hom c y)
(ωsquare : C^.circ base h₁ = C^.circ map h₂)
: C^.hom c p
:= C^.circ
(cast_hom (IsPullback.ωpullback base p₁ map p₂))
(@pullback.univ _ _ _ _ (IsPullback.has_pullback base p₁ map p₂)
(PullbackCone.mk _ c (C^.circ base h₁) (h₁ ↗← h₂ ↗←↗)
begin
apply dlist.eq,
{ trivial },
apply dlist.eq,
{ exact ωsquare },
trivial
end))
/-! #brief Helper for showing one has a pullback square.
-/
definition IsPullback.show {C : Cat.{ℓobj ℓhom}}
{p x y t : C^.obj}
{base : C^.hom x t} {p₁ : C^.hom p x}
{map : C^.hom y t} {p₂ : C^.hom p y}
(univ
: ∀ {c : C^.obj} (h₁ : C^.hom c x) (h₂ : C^.hom c y)
(ωsquare : C^.circ base h₁ = C^.circ map h₂)
, C^.hom c p)
(ωsquare
: C^.circ base p₁ = C^.circ map p₂)
(ωuniv₁
: ∀ {c : C^.obj} (h₁ : C^.hom c x) (h₂ : C^.hom c y)
(ωsquare : C^.circ base h₁ = C^.circ map h₂)
, h₁ = C^.circ p₁ (univ h₁ h₂ ωsquare))
(ωuniv₂
: ∀ {c : C^.obj} (h₁ : C^.hom c x) (h₂ : C^.hom c y)
(ωsquare : C^.circ base h₁ = C^.circ map h₂)
, h₂ = C^.circ p₂ (univ h₁ h₂ ωsquare))
(ωuniv_uniq
: ∀ {c : C^.obj} (h₁ : C^.hom c x) (h₂ : C^.hom c y)
(ωsquare : C^.circ base h₁ = C^.circ map h₂)
(univ' : C^.hom c p)
(ωuniv'₁ : h₁ = C^.circ p₁ univ')
(ωuniv'₂ : h₂ = C^.circ p₂ univ')
, univ' = univ h₁ h₂ ωsquare)
: IsPullback base p₁ map p₂
:= { has_pullback
:= HasPullback.show C (base ↗→ map ↗→↗) p
(C^.circ base p₁)
(p₁ ↗← p₂ ↗←↗)
begin
apply dlist.eq,
{ trivial },
apply dlist.eq,
{ exact ωsquare },
trivial
end
(λ c to_t homs ωhoms
, begin
cases homs with _ h₁ _ homs,
cases homs with _ h₂ _ _,
cases bb,
apply univ h₁ h₂,
refine @eq.trans _ _ to_t _ _ _,
{ apply eq.symm,
apply HomsList.congr_get ωhoms (@fin_of 1 0)
},
{ apply HomsList.congr_get ωhoms (@fin_of 0 1) }
end)
(λ c to_t homs ωhoms
, begin
cases homs with _ h₁ _ homs,
cases homs with _ h₂ _ _,
cases bb,
apply dlist.eq,
{ apply ωuniv₁ },
apply dlist.eq,
{ apply ωuniv₂ },
trivial
end)
(λ c to_t homs ωhoms h ωh
, begin
cases homs with _ h₁ _ homs,
cases homs with _ h₂ _ _,
cases bb,
apply ωuniv_uniq,
{ apply dlist.congr_get ωh (@fin_of 1 0) },
{ apply dlist.congr_get ωh (@fin_of 0 1) }
end)
, ωpullback := rfl
, ωπ₁ := eq.symm C^.circ_id_right
, ωπ₂ := eq.symm C^.circ_id_right
}
/- -----------------------------------------------------------------------
Maps from pullbacks to products.
----------------------------------------------------------------------- -/
/-! #brief The map from a pullback to the underlying product.
-/
definition pullback.to_finproduct (C : Cat.{ℓobj ℓhom})
{factor : list C^.obj} {t : C^.obj}
(maps : HomsIn factor t)
[maps_HasPullback : HasPullback C maps]
[dom_HasFinProduct : HasFinProduct C factor]
: C^.hom (pullback C maps) (finproduct C factor)
:= sorry
/- -----------------------------------------------------------------------
Maps between pullbacks.
----------------------------------------------------------------------- -/
/-! #brief Building a map between pullbacks.
-/
definition pullback.hom (C : Cat.{ℓobj ℓhom})
(base : C^.obj × C^.obj) (factor : list (C^.obj × C^.obj)) {t : C^.obj}
(maps₁ : HomsIn (list.map prod.fst (base :: factor)) t)
[maps₁_HasPullback : HasPullback C maps₁]
(maps₂ : HomsIn (list.map prod.snd (base :: factor)) t)
[maps₂_HasPullback : HasPullback C maps₂]
(fns : HomsList C (base :: factor))
: C^.hom (pullback C maps₁)
(pullback C maps₂)
:= pullback.univ _ _
(PullbackCone.mk maps₂ (pullback C maps₁)
(HomsIn.get maps₂ fin.zero
∘∘ HomsList.get fns fin.zero
∘∘ pullback.π C maps₁ fin.zero)
(homs_comp_out fns (pullback.cone C maps₁)^.Proj)
sorry)
/- -----------------------------------------------------------------------
Fibers.
----------------------------------------------------------------------- -/
/-! #brief Fiber of a map over a global element.
-/
definition Fiber {C : Cat.{ℓobj ℓhom}}
[C_HasFinal : HasFinal C]
[C_HasAllPullbacks : HasAllPullbacks C]
{x y : C^.obj} (f : C^.hom x y)
(y₀ : C^.hom (final C) y)
: C^.obj
:= pullback C (f ↗→ y₀ ↗→↗)
/-! #brief Projection out of a fiber.
-/
definition Fiber.π {C : Cat.{ℓobj ℓhom}}
[C_HasFinal : HasFinal C]
[C_HasAllPullbacks : HasAllPullbacks C]
{x y : C^.obj} (f : C^.hom x y)
(y₀ : C^.hom (final C) y)
: C^.hom (Fiber f y₀) x
:= pullback.π C (f ↗→ y₀ ↗→↗) (@fin_of 1 0)
/-! #brief A hom into a fiber.
-/
definition Fiber.into {C : Cat.{ℓobj ℓhom}}
[C_HasFinal : HasFinal C]
[C_HasAllPullbacks : HasAllPullbacks C]
{x y : C^.obj} (f : C^.hom x y)
(y₀ : C^.hom (final C) y)
{z : C^.obj}
(h : C^.hom z x)
(ωh : f ∘∘ h = y₀ ∘∘ final_hom z)
: C^.hom z (Fiber f y₀)
:= pullback.univ C (f ↗→ y₀ ↗→↗)
(PullbackCone.mk (f ↗→ y₀ ↗→↗) z
(C^.circ f h)
(h ↗← final_hom z ↗←↗)
begin
apply HomsList.eq, { trivial },
apply HomsList.eq, { exact ωh },
trivial
end)
/-! #brief A cone over a fiber.
-/
definition Fiber.cone {C : Cat.{ℓobj ℓhom}}
[C_HasFinal : HasFinal C]
[C_HasAllPullbacks : HasAllPullbacks C]
{x₁ x₂ y : C^.obj} {f₁ : C^.hom x₁ y} {f₂ : C^.hom x₂ y}
{y₀ : C^.hom (final C) y}
(h : C^.hom x₁ x₂)
(ωh : y₀ ∘∘ final_hom (Fiber f₁ y₀)
= f₂ ∘∘ h ∘∘ pullback.π C (f₁ ↗→ y₀ ↗→↗) (@fin_of 1 0))
:= PullbackCone.mk (f₂ ↗→ y₀ ↗→↗) (Fiber f₁ y₀)
(y₀ ∘∘ final_hom (Fiber f₁ y₀))
(h ∘∘ pullback.π C (f₁ ↗→ y₀ ↗→↗) (@fin_of 1 0) ↗← final_hom (Fiber f₁ y₀) ↗←↗)
begin
apply HomsList.eq, { exact eq.trans ωh (eq.symm C^.circ_assoc) },
apply HomsList.eq, { trivial },
trivial
end
/-! #brief A hom between fibers.
-/
definition Fiber.hom {C : Cat.{ℓobj ℓhom}}
[C_HasFinal : HasFinal C]
[C_HasAllPullbacks : HasAllPullbacks C]
{x₁ x₂ y : C^.obj} {f₁ : C^.hom x₁ y} {f₂ : C^.hom x₂ y}
{y₀ : C^.hom (final C) y}
(h : C^.hom x₁ x₂)
(ωh : y₀ ∘∘ final_hom (Fiber f₁ y₀)
= f₂ ∘∘ h ∘∘ pullback.π C (f₁ ↗→ y₀ ↗→↗) (@fin_of 1 0))
: C^.hom (Fiber f₁ y₀) (Fiber f₂ y₀)
:= pullback.univ C (f₂ ↗→ y₀ ↗→↗) (Fiber.cone h ωh)
/- -----------------------------------------------------------------------
Products in OverCat.
----------------------------------------------------------------------- -/
/-! #brief Existence of products in an over-category.
-/
definition OverCat.HasFinProduct₀ (C : Cat.{ℓobj ℓhom}) (c : C^.obj)
: HasFinProduct (OverCat C c) []
:= @HasFinProduct.show (OverCat C c) []
(@final (OverCat C c) (OverCat.HasFinal C c))
HomsOut.nil
(λ X homs, @final_hom (OverCat C c) (OverCat.HasFinal C c) X)
(λ X homs, begin cases homs, trivial end)
(λ X homs h ωh, @final_hom.uniq (OverCat C c) (OverCat.HasFinal C c) X h)
/-! #brief Existence of products in an over-category.
-/
definition OverCat.HasFinProduct₁ (C : Cat.{ℓobj ℓhom}) (c : C^.obj)
(factors : list (OverCat C c)^.obj)
[factors_HasPullback : HasPullback C (HomsIn.of_list_OverObj factors)]
: HasFinProduct (OverCat C c) factors
:= let pb : OverObj C c
:= { obj := pullback C (HomsIn.of_list_OverObj factors)
, hom := pullback.πbase C (HomsIn.of_list_OverObj factors)
}
in HasProduct.show (OverCat C c) (list.get factors)
pb
(λ n, { hom := cast_hom sorry
∘∘ pullback.π C (HomsIn.of_list_OverObj factors)
{ val := n^.val, is_lt := cast sorry n^.is_lt }
, triangle := sorry
})
(λ X homs
, { hom := pullback.univ C (HomsIn.of_list_OverObj factors)
(PullbackCone.mk (HomsIn.of_list_OverObj factors) X^.obj
X^.hom
(HomsOut.enum
(λ n, cast_hom sorry
∘∘ (homs { val := n^.val, is_lt := cast sorry n^.is_lt})^.hom))
sorry)
, triangle := sorry
})
(λ X hom n, sorry)
(λ X hom h ωh, sorry)
/-! #brief Existence of products in an over-category.
-/
instance OverCat.HasFinProduct (C : Cat.{ℓobj ℓhom}) (c : C^.obj)
[C_HasAllPullbacks : HasAllPullbacks C]
: ∀ (factors : list (OverCat C c)^.obj)
, HasFinProduct (OverCat C c) factors
| [] := OverCat.HasFinProduct₀ C c
| (factor₀ :: factors)
:= @OverCat.HasFinProduct₁ C c (factor₀ :: factors)
(HasAllPullbacks.HasPullback C _)
instance OverCat.HasAllFinProducts (C : Cat.{ℓobj ℓhom}) (c : C^.obj)
[C_HasAllPullbacks : HasAllPullbacks C]
: HasAllFinProducts (OverCat C c)
:= { has_product := OverCat.HasFinProduct C c
}
/- -----------------------------------------------------------------------
Pullbacks along final homs.
----------------------------------------------------------------------- -/
/-! Categories with products have pullbacks along final homs.
-/
instance HasAllFinProducts.final_hom.HasPullbacksAlong
{C : Cat.{ℓobj ℓhom}}
[C_HasFinal : HasFinal C]
[C_HasAllFinProducts : HasAllFinProducts C]
(x : C^.obj)
: HasPullbacksAlong C (final_hom x)
:= { has_pullback
:= λ y map
, HasPullback.show C (final_hom x ↗→ map ↗→↗)
(finproduct C [x, y])
(final_hom (finproduct C [x, y]))
(finproduct.cone C [x, y])^.Proj
begin
apply eq.symm,
apply dlist.eq,
{ apply final_hom.uniq },
-- induction maps with _ m _ maps rec,
-- { trivial },
-- apply dlist.eq,
-- { apply final_hom.uniq },
-- apply rec
exact sorry
end
begin exact sorry end
begin exact sorry end
begin exact sorry end
}
end qp
|
23a1384721181bb9b81ada0d327e4f296ead10d2 | 137c667471a40116a7afd7261f030b30180468c2 | /src/algebra/big_operators/basic.lean | a39498a6b1a7b335b2b888c3041f264898eb18d4 | [
"Apache-2.0"
] | permissive | bragadeesh153/mathlib | 46bf814cfb1eecb34b5d1549b9117dc60f657792 | b577bb2cd1f96eb47031878256856020b76f73cd | refs/heads/master | 1,687,435,188,334 | 1,626,384,207,000 | 1,626,384,207,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 59,299 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl
-/
import data.finset.fold
import data.equiv.mul_add
import tactic.abel
/-!
# Big operators
In this file we define products and sums indexed by finite sets (specifically, `finset`).
## Notation
We introduce the following notation, localized in `big_operators`.
To enable the notation, use `open_locale big_operators`.
Let `s` be a `finset α`, and `f : α → β` a function.
* `∏ x in s, f x` is notation for `finset.prod s f` (assuming `β` is a `comm_monoid`)
* `∑ x in s, f x` is notation for `finset.sum s f` (assuming `β` is an `add_comm_monoid`)
* `∏ x, f x` is notation for `finset.prod finset.univ f`
(assuming `α` is a `fintype` and `β` is a `comm_monoid`)
* `∑ x, f x` is notation for `finset.sum finset.univ f`
(assuming `α` is a `fintype` and `β` is an `add_comm_monoid`)
## Implementation Notes
The first arguments in all definitions and lemmas is the codomain of the function of the big
operator. This is necessary for the heuristic in `@[to_additive]`.
See the documentation of `to_additive.attr` for more information.
-/
universes u v w
variables {β : Type u} {α : Type v} {γ : Type w}
namespace finset
/--
`∏ x in s, f x` is the product of `f x`
as `x` ranges over the elements of the finite set `s`.
-/
@[to_additive "`∑ x in s, f` is the sum of `f x` as `x` ranges over the elements
of the finite set `s`."]
protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod
@[simp, to_additive] lemma prod_mk [comm_monoid β] (s : multiset α) (hs : s.nodup) (f : α → β) :
(⟨s, hs⟩ : finset α).prod f = (s.map f).prod :=
rfl
end finset
/--
There is no established mathematical convention
for the operator precedence of big operators like `∏` and `∑`.
We will have to make a choice.
Online discussions, such as https://math.stackexchange.com/q/185538/30839
seem to suggest that `∏` and `∑` should have the same precedence,
and that this should be somewhere between `*` and `+`.
The latter have precedence levels `70` and `65` respectively,
and we therefore choose the level `67`.
In practice, this means that parentheses should be placed as follows:
```lean
∑ k in K, (a k + b k) = ∑ k in K, a k + ∑ k in K, b k →
∏ k in K, a k * b k = (∏ k in K, a k) * (∏ k in K, b k)
```
(Example taken from page 490 of Knuth's *Concrete Mathematics*.)
-/
library_note "operator precedence of big operators"
localized "notation `∑` binders `, ` r:(scoped:67 f, finset.sum finset.univ f) := r"
in big_operators
localized "notation `∏` binders `, ` r:(scoped:67 f, finset.prod finset.univ f) := r"
in big_operators
localized "notation `∑` binders ` in ` s `, ` r:(scoped:67 f, finset.sum s f) := r"
in big_operators
localized "notation `∏` binders ` in ` s `, ` r:(scoped:67 f, finset.prod s f) := r"
in big_operators
open_locale big_operators
namespace finset
variables {s s₁ s₂ : finset α} {a : α} {f g : α → β}
@[to_additive] lemma prod_eq_multiset_prod [comm_monoid β] (s : finset α) (f : α → β) :
∏ x in s, f x = (s.1.map f).prod := rfl
@[to_additive]
theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) :
∏ x in s, f x = s.fold (*) 1 f :=
rfl
@[simp] lemma sum_multiset_singleton (s : finset α) :
s.sum (λ x, x ::ₘ 0) = s.val :=
by simp [sum_eq_multiset_sum]
end finset
@[to_additive]
lemma monoid_hom.map_prod [comm_monoid β] [comm_monoid γ] (g : β →* γ) (f : α → β) (s : finset α) :
g (∏ x in s, f x) = ∏ x in s, g (f x) :=
by simp only [finset.prod_eq_multiset_prod, g.map_multiset_prod, multiset.map_map]
@[to_additive]
lemma mul_equiv.map_prod [comm_monoid β] [comm_monoid γ] (g : β ≃* γ) (f : α → β) (s : finset α) :
g (∏ x in s, f x) = ∏ x in s, g (f x) :=
g.to_monoid_hom.map_prod f s
lemma ring_hom.map_list_prod [semiring β] [semiring γ] (f : β →+* γ) (l : list β) :
f l.prod = (l.map f).prod :=
f.to_monoid_hom.map_list_prod l
lemma ring_hom.map_list_sum [non_assoc_semiring β] [non_assoc_semiring γ]
(f : β →+* γ) (l : list β) :
f l.sum = (l.map f).sum :=
f.to_add_monoid_hom.map_list_sum l
lemma ring_hom.map_multiset_prod [comm_semiring β] [comm_semiring γ] (f : β →+* γ)
(s : multiset β) :
f s.prod = (s.map f).prod :=
f.to_monoid_hom.map_multiset_prod s
lemma ring_hom.map_multiset_sum [non_assoc_semiring β] [non_assoc_semiring γ]
(f : β →+* γ) (s : multiset β) :
f s.sum = (s.map f).sum :=
f.to_add_monoid_hom.map_multiset_sum s
lemma ring_hom.map_prod [comm_semiring β] [comm_semiring γ] (g : β →+* γ) (f : α → β)
(s : finset α) :
g (∏ x in s, f x) = ∏ x in s, g (f x) :=
g.to_monoid_hom.map_prod f s
lemma ring_hom.map_sum [non_assoc_semiring β] [non_assoc_semiring γ]
(g : β →+* γ) (f : α → β) (s : finset α) :
g (∑ x in s, f x) = ∑ x in s, g (f x) :=
g.to_add_monoid_hom.map_sum f s
@[to_additive]
lemma monoid_hom.coe_prod [mul_one_class β] [comm_monoid γ] (f : α → β →* γ) (s : finset α) :
⇑(∏ x in s, f x) = ∏ x in s, f x :=
(monoid_hom.coe_fn β γ).map_prod _ _
-- See also `finset.prod_apply`, with the same conclusion
-- but with the weaker hypothesis `f : α → β → γ`.
@[simp, to_additive]
lemma monoid_hom.finset_prod_apply [mul_one_class β] [comm_monoid γ] (f : α → β →* γ)
(s : finset α) (b : β) : (∏ x in s, f x) b = ∏ x in s, f x b :=
(monoid_hom.eval b).map_prod _ _
variables {s s₁ s₂ : finset α} {a : α} {f g : α → β}
namespace finset
section comm_monoid
variables [comm_monoid β]
@[simp, to_additive]
lemma prod_empty {f : α → β} : (∏ x in (∅:finset α), f x) = 1 := rfl
@[simp, to_additive]
lemma prod_insert [decidable_eq α] : a ∉ s → (∏ x in (insert a s), f x) = f a * ∏ x in s, f x :=
fold_insert
/--
The product of `f` over `insert a s` is the same as
the product over `s`, as long as `a` is in `s` or `f a = 1`.
-/
@[simp, to_additive "The sum of `f` over `insert a s` is the same as
the sum over `s`, as long as `a` is in `s` or `f a = 0`."]
lemma prod_insert_of_eq_one_if_not_mem [decidable_eq α] (h : a ∉ s → f a = 1) :
∏ x in insert a s, f x = ∏ x in s, f x :=
begin
by_cases hm : a ∈ s,
{ simp_rw insert_eq_of_mem hm },
{ rw [prod_insert hm, h hm, one_mul] },
end
/--
The product of `f` over `insert a s` is the same as the product over `s`, as long as `f a = 1`.
-/
@[simp, to_additive "The sum of `f` over `insert a s` is the same as
the sum over `s`, as long as `f a = 0`."]
lemma prod_insert_one [decidable_eq α] (h : f a = 1) :
∏ x in insert a s, f x = ∏ x in s, f x :=
prod_insert_of_eq_one_if_not_mem (λ _, h)
@[simp, to_additive]
lemma prod_singleton : (∏ x in (singleton a), f x) = f a :=
eq.trans fold_singleton $ mul_one _
@[to_additive]
lemma prod_pair [decidable_eq α] {a b : α} (h : a ≠ b) :
(∏ x in ({a, b} : finset α), f x) = f a * f b :=
by rw [prod_insert (not_mem_singleton.2 h), prod_singleton]
@[simp, priority 1100, to_additive]
lemma prod_const_one : (∏ x in s, (1 : β)) = 1 :=
by simp only [finset.prod, multiset.map_const, multiset.prod_repeat, one_pow]
@[simp, to_additive]
lemma prod_image [decidable_eq α] {s : finset γ} {g : γ → α} :
(∀ x ∈ s, ∀ y ∈ s, g x = g y → x = y) → (∏ x in (s.image g), f x) = ∏ x in s, f (g x) :=
fold_image
@[simp, to_additive]
lemma prod_map (s : finset α) (e : α ↪ γ) (f : γ → β) :
(∏ x in (s.map e), f x) = ∏ x in s, f (e x) :=
by rw [finset.prod, finset.map_val, multiset.map_map]; refl
@[congr, to_additive]
lemma prod_congr (h : s₁ = s₂) : (∀ x ∈ s₂, f x = g x) → s₁.prod f = s₂.prod g :=
by rw [h]; exact fold_congr
attribute [congr] finset.sum_congr
@[to_additive]
lemma prod_union_inter [decidable_eq α] :
(∏ x in (s₁ ∪ s₂), f x) * (∏ x in (s₁ ∩ s₂), f x) = (∏ x in s₁, f x) * (∏ x in s₂, f x) :=
fold_union_inter
@[to_additive]
lemma prod_union [decidable_eq α] (h : disjoint s₁ s₂) :
(∏ x in (s₁ ∪ s₂), f x) = (∏ x in s₁, f x) * (∏ x in s₂, f x) :=
by rw [←prod_union_inter, (disjoint_iff_inter_eq_empty.mp h)]; exact (mul_one _).symm
end comm_monoid
end finset
section
open finset
variables [fintype α] [decidable_eq α] [comm_monoid β]
@[to_additive]
lemma is_compl.prod_mul_prod {s t : finset α} (h : is_compl s t) (f : α → β) :
(∏ i in s, f i) * (∏ i in t, f i) = ∏ i, f i :=
(finset.prod_union h.disjoint).symm.trans $ by rw [← finset.sup_eq_union, h.sup_eq_top]; refl
end
namespace finset
section comm_monoid
variables [comm_monoid β]
@[to_additive]
lemma prod_mul_prod_compl [fintype α] [decidable_eq α] (s : finset α) (f : α → β) :
(∏ i in s, f i) * (∏ i in sᶜ, f i) = ∏ i, f i :=
is_compl_compl.prod_mul_prod f
@[to_additive]
lemma prod_compl_mul_prod [fintype α] [decidable_eq α] (s : finset α) (f : α → β) :
(∏ i in sᶜ, f i) * (∏ i in s, f i) = ∏ i, f i :=
is_compl_compl.symm.prod_mul_prod f
@[to_additive]
lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) :
(∏ x in (s₂ \ s₁), f x) * (∏ x in s₁, f x) = (∏ x in s₂, f x) :=
by rw [←prod_union sdiff_disjoint, sdiff_union_of_subset h]
@[simp, to_additive]
lemma prod_sum_elim [decidable_eq (α ⊕ γ)]
(s : finset α) (t : finset γ) (f : α → β) (g : γ → β) :
∏ x in s.map function.embedding.inl ∪ t.map function.embedding.inr, sum.elim f g x =
(∏ x in s, f x) * (∏ x in t, g x) :=
begin
rw [prod_union, prod_map, prod_map],
{ simp only [sum.elim_inl, function.embedding.inl_apply, function.embedding.inr_apply,
sum.elim_inr] },
{ simp only [disjoint_left, finset.mem_map, finset.mem_map],
rintros _ ⟨i, hi, rfl⟩ ⟨j, hj, H⟩,
cases H }
end
@[to_additive]
lemma prod_bUnion [decidable_eq α] {s : finset γ} {t : γ → finset α} :
(∀ x ∈ s, ∀ y ∈ s, x ≠ y → disjoint (t x) (t y)) →
(∏ x in (s.bUnion t), f x) = ∏ x in s, ∏ i in t x, f i :=
by haveI := classical.dec_eq γ; exact
finset.induction_on s (λ _, by simp only [bUnion_empty, prod_empty])
(assume x s hxs ih hd,
have hd' : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → disjoint (t x) (t y),
from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy),
have ∀ y ∈ s, x ≠ y,
from assume _ hy h, by rw [←h] at hy; contradiction,
have ∀ y ∈ s, disjoint (t x) (t y),
from assume _ hy, hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hy) (this _ hy),
have disjoint (t x) (finset.bUnion s t),
from (disjoint_bUnion_right _ _ _).mpr this,
by simp only [bUnion_insert, prod_insert hxs, prod_union this, ih hd'])
@[to_additive]
lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} :
(∏ x in s.product t, f x) = ∏ x in s, ∏ y in t, f (x, y) :=
begin
haveI := classical.dec_eq α, haveI := classical.dec_eq γ,
rw [product_eq_bUnion, prod_bUnion],
{ congr, funext, exact prod_image (λ _ _ _ _ H, (prod.mk.inj H).2) },
simp only [disjoint_iff_ne, mem_image],
rintros _ _ _ _ h ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ _,
apply h, cc
end
/-- An uncurried version of `finset.prod_product`. -/
@[to_additive "An uncurried version of `finset.sum_product`"]
lemma prod_product' {s : finset γ} {t : finset α} {f : γ → α → β} :
(∏ x in s.product t, f x.1 x.2) = ∏ x in s, ∏ y in t, f x y :=
prod_product
/-- Product over a sigma type equals the product of fiberwise products. For rewriting
in the reverse direction, use `finset.prod_sigma'`. -/
@[to_additive "Sum over a sigma type equals the sum of fiberwise sums. For rewriting
in the reverse direction, use `finset.sum_sigma'`"]
lemma prod_sigma {σ : α → Type*}
(s : finset α) (t : Π a, finset (σ a)) (f : sigma σ → β) :
(∏ x in s.sigma t, f x) = ∏ a in s, ∏ s in (t a), f ⟨a, s⟩ :=
by classical;
calc (∏ x in s.sigma t, f x) =
∏ x in s.bUnion (λ a, (t a).map (function.embedding.sigma_mk a)), f x : by rw sigma_eq_bUnion
... = ∏ a in s, ∏ x in (t a).map (function.embedding.sigma_mk a), f x :
prod_bUnion $ assume a₁ ha a₂ ha₂ h x hx,
by { simp only [inf_eq_inter, mem_inter, mem_map, function.embedding.sigma_mk_apply] at hx,
rcases hx with ⟨⟨y, hy, rfl⟩, ⟨z, hz, hz'⟩⟩, cc }
... = ∏ a in s, ∏ s in t a, f ⟨a, s⟩ :
prod_congr rfl $ λ _ _, prod_map _ _ _
@[to_additive]
lemma prod_sigma' {σ : α → Type*}
(s : finset α) (t : Π a, finset (σ a)) (f : Π a, σ a → β) :
(∏ a in s, ∏ s in (t a), f a s) = ∏ x in s.sigma t, f x.1 x.2 :=
eq.symm $ prod_sigma s t (λ x, f x.1 x.2)
@[to_additive]
lemma prod_fiberwise_of_maps_to [decidable_eq γ] {s : finset α} {t : finset γ} {g : α → γ}
(h : ∀ x ∈ s, g x ∈ t) (f : α → β) :
(∏ y in t, ∏ x in s.filter (λ x, g x = y), f x) = ∏ x in s, f x :=
begin
letI := classical.dec_eq α,
rw [← bUnion_filter_eq_of_maps_to h] {occs := occurrences.pos [2]},
refine (prod_bUnion $ λ x' hx y' hy hne, _).symm,
rw [disjoint_filter],
rintros x hx rfl,
exact hne
end
@[to_additive]
lemma prod_image' [decidable_eq α] {s : finset γ} {g : γ → α} (h : γ → β)
(eq : ∀ c ∈ s, f (g c) = ∏ x in s.filter (λ c', g c' = g c), h x) :
(∏ x in s.image g, f x) = ∏ x in s, h x :=
calc (∏ x in s.image g, f x) = ∏ x in s.image g, ∏ x in s.filter (λ c', g c' = x), h x :
prod_congr rfl $ λ x hx, let ⟨c, hcs, hc⟩ := mem_image.1 hx in hc ▸ (eq c hcs)
... = ∏ x in s, h x : prod_fiberwise_of_maps_to (λ x, mem_image_of_mem g) _
@[to_additive]
lemma prod_mul_distrib : ∏ x in s, (f x * g x) = (∏ x in s, f x) * (∏ x in s, g x) :=
eq.trans (by rw one_mul; refl) fold_op_distrib
@[to_additive]
lemma prod_comm {s : finset γ} {t : finset α} {f : γ → α → β} :
(∏ x in s, ∏ y in t, f x y) = (∏ y in t, ∏ x in s, f x y) :=
begin
classical,
apply finset.induction_on s,
{ simp only [prod_empty, prod_const_one] },
{ intros _ _ H ih,
simp only [prod_insert H, prod_mul_distrib, ih] }
end
@[to_additive]
lemma prod_hom [comm_monoid γ] (s : finset α) {f : α → β} (g : β → γ) [is_monoid_hom g] :
(∏ x in s, g (f x)) = g (∏ x in s, f x) :=
((monoid_hom.of g).map_prod f s).symm
@[to_additive]
lemma prod_hom_rel [comm_monoid γ] {r : β → γ → Prop} {f : α → β} {g : α → γ} {s : finset α}
(h₁ : r 1 1) (h₂ : ∀ a b c, r b c → r (f a * b) (g a * c)) : r (∏ x in s, f x) (∏ x in s, g x) :=
by { delta finset.prod, apply multiset.prod_hom_rel; assumption }
@[to_additive]
lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀ x ∈ s₂, x ∉ s₁ → f x = 1) :
(∏ x in s₁, f x) = ∏ x in s₂, f x :=
by haveI := classical.dec_eq α; exact
have ∏ x in s₂ \ s₁, f x = ∏ x in s₂ \ s₁, 1,
from prod_congr rfl $ by simpa only [mem_sdiff, and_imp],
by rw [←prod_sdiff h]; simp only [this, prod_const_one, one_mul]
@[to_additive]
lemma prod_filter_of_ne {p : α → Prop} [decidable_pred p] (hp : ∀ x ∈ s, f x ≠ 1 → p x) :
(∏ x in (s.filter p), f x) = (∏ x in s, f x) :=
prod_subset (filter_subset _ _) $ λ x,
by { classical, rw [not_imp_comm, mem_filter], exact λ h₁ h₂, ⟨h₁, hp _ h₁ h₂⟩ }
-- If we use `[decidable_eq β]` here, some rewrites fail because they find a wrong `decidable`
-- instance first; `{∀ x, decidable (f x ≠ 1)}` doesn't work with `rw ← prod_filter_ne_one`
@[to_additive]
lemma prod_filter_ne_one [∀ x, decidable (f x ≠ 1)] :
(∏ x in (s.filter $ λ x, f x ≠ 1), f x) = (∏ x in s, f x) :=
prod_filter_of_ne $ λ _ _, id
@[to_additive]
lemma prod_filter (p : α → Prop) [decidable_pred p] (f : α → β) :
(∏ a in s.filter p, f a) = (∏ a in s, if p a then f a else 1) :=
calc (∏ a in s.filter p, f a) = ∏ a in s.filter p, if p a then f a else 1 :
prod_congr rfl (assume a h, by rw [if_pos (mem_filter.1 h).2])
... = ∏ a in s, if p a then f a else 1 :
begin
refine prod_subset (filter_subset _ s) (assume x hs h, _),
rw [mem_filter, not_and] at h,
exact if_neg (h hs)
end
@[to_additive]
lemma prod_eq_single_of_mem {s : finset α} {f : α → β} (a : α) (h : a ∈ s)
(h₀ : ∀ b ∈ s, b ≠ a → f b = 1) : (∏ x in s, f x) = f a :=
begin
haveI := classical.dec_eq α,
calc (∏ x in s, f x) = ∏ x in {a}, f x :
begin
refine (prod_subset _ _).symm,
{ intros _ H, rwa mem_singleton.1 H },
{ simpa only [mem_singleton] }
end
... = f a : prod_singleton
end
@[to_additive]
lemma prod_eq_single {s : finset α} {f : α → β} (a : α)
(h₀ : ∀ b ∈ s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : (∏ x in s, f x) = f a :=
by haveI := classical.dec_eq α;
from classical.by_cases
(assume : a ∈ s, prod_eq_single_of_mem a this h₀)
(assume : a ∉ s,
(prod_congr rfl $ λ b hb, h₀ b hb $ by rintro rfl; cc).trans $
prod_const_one.trans (h₁ this).symm)
@[to_additive]
lemma prod_eq_mul_of_mem {s : finset α} {f : α → β} (a b : α) (ha : a ∈ s) (hb : b ∈ s) (hn : a ≠ b)
(h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) : (∏ x in s, f x) = (f a) * (f b) :=
begin
haveI := classical.dec_eq α;
let s' := ({a, b} : finset α),
have hu : s' ⊆ s,
{ refine insert_subset.mpr _, apply and.intro ha, apply singleton_subset_iff.mpr hb },
have hf : ∀ c ∈ s, c ∉ s' → f c = 1,
{ intros c hc hcs,
apply h₀ c hc,
apply not_or_distrib.mp,
intro hab,
apply hcs,
apply mem_insert.mpr,
rw mem_singleton,
exact hab },
rw ←prod_subset hu hf,
exact finset.prod_pair hn
end
@[to_additive]
lemma prod_eq_mul {s : finset α} {f : α → β} (a b : α) (hn : a ≠ b)
(h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) (ha : a ∉ s → f a = 1) (hb : b ∉ s → f b = 1) :
(∏ x in s, f x) = (f a) * (f b) :=
begin
haveI := classical.dec_eq α;
by_cases h₁ : a ∈ s; by_cases h₂ : b ∈ s,
{ exact prod_eq_mul_of_mem a b h₁ h₂ hn h₀ },
{ rw [hb h₂, mul_one],
apply prod_eq_single_of_mem a h₁,
exact λ c hc hca, h₀ c hc ⟨hca, ne_of_mem_of_not_mem hc h₂⟩ },
{ rw [ha h₁, one_mul],
apply prod_eq_single_of_mem b h₂,
exact λ c hc hcb, h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, hcb⟩ },
{ rw [ha h₁, hb h₂, mul_one],
exact trans
(prod_congr rfl (λ c hc, h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, ne_of_mem_of_not_mem hc h₂⟩))
prod_const_one }
end
@[to_additive]
lemma prod_attach {f : α → β} : (∏ x in s.attach, f x) = (∏ x in s, f x) :=
by haveI := classical.dec_eq α; exact
calc (∏ x in s.attach, f x.val) = (∏ x in (s.attach).image subtype.val, f x) :
by rw [prod_image]; exact assume x _ y _, subtype.eq
... = _ : by rw [attach_image_val]
/-- A product over `s.subtype p` equals one over `s.filter p`. -/
@[simp, to_additive "A sum over `s.subtype p` equals one over `s.filter p`."]
lemma prod_subtype_eq_prod_filter (f : α → β) {p : α → Prop} [decidable_pred p] :
∏ x in s.subtype p, f x = ∏ x in s.filter p, f x :=
begin
conv_lhs {
erw ←prod_map (s.subtype p) (function.embedding.subtype _) f
},
exact prod_congr (subtype_map _) (λ x hx, rfl)
end
/-- If all elements of a `finset` satisfy the predicate `p`, a product
over `s.subtype p` equals that product over `s`. -/
@[to_additive "If all elements of a `finset` satisfy the predicate `p`, a sum
over `s.subtype p` equals that sum over `s`."]
lemma prod_subtype_of_mem (f : α → β) {p : α → Prop} [decidable_pred p]
(h : ∀ x ∈ s, p x) : ∏ x in s.subtype p, f x = ∏ x in s, f x :=
by simp_rw [prod_subtype_eq_prod_filter, filter_true_of_mem h]
/-- A product of a function over a `finset` in a subtype equals a
product in the main type of a function that agrees with the first
function on that `finset`. -/
@[to_additive "A sum of a function over a `finset` in a subtype equals a
sum in the main type of a function that agrees with the first
function on that `finset`."]
lemma prod_subtype_map_embedding {p : α → Prop} {s : finset {x // p x}} {f : {x // p x} → β}
{g : α → β} (h : ∀ x : {x // p x}, x ∈ s → g x = f x) :
∏ x in s.map (function.embedding.subtype _), g x = ∏ x in s, f x :=
begin
rw finset.prod_map,
exact finset.prod_congr rfl h
end
@[to_additive]
lemma prod_finset_coe (f : α → β) (s : finset α) :
∏ (i : (s : set α)), f i = ∏ i in s, f i :=
prod_attach
@[to_additive]
lemma prod_subtype {p : α → Prop} {F : fintype (subtype p)} (s : finset α)
(h : ∀ x, x ∈ s ↔ p x) (f : α → β) :
∏ a in s, f a = ∏ a : subtype p, f a :=
have (∈ s) = p, from set.ext h, by { substI p, rw [←prod_finset_coe], congr }
@[to_additive]
lemma prod_eq_one {f : α → β} {s : finset α} (h : ∀ x ∈ s, f x = 1) : (∏ x in s, f x) = 1 :=
calc (∏ x in s, f x) = ∏ x in s, 1 : finset.prod_congr rfl h
... = 1 : finset.prod_const_one
@[to_additive] lemma prod_apply_dite {s : finset α} {p : α → Prop} {hp : decidable_pred p}
(f : Π (x : α), p x → γ) (g : Π (x : α), ¬p x → γ) (h : γ → β) :
(∏ x in s, h (if hx : p x then f x hx else g x hx)) =
(∏ x in (s.filter p).attach, h (f x.1 (mem_filter.mp x.2).2)) *
(∏ x in (s.filter (λ x, ¬ p x)).attach, h (g x.1 (mem_filter.mp x.2).2)) :=
by letI := classical.dec_eq α; exact
calc ∏ x in s, h (if hx : p x then f x hx else g x hx)
= ∏ x in s.filter p ∪ s.filter (λ x, ¬ p x), h (if hx : p x then f x hx else g x hx) :
by rw [filter_union_filter_neg_eq]
... = (∏ x in s.filter p, h (if hx : p x then f x hx else g x hx)) *
(∏ x in s.filter (λ x, ¬ p x), h (if hx : p x then f x hx else g x hx)) :
prod_union (by simp [disjoint_right] {contextual := tt})
... = (∏ x in (s.filter p).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) *
(∏ x in (s.filter (λ x, ¬ p x)).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) :
congr_arg2 _ prod_attach.symm prod_attach.symm
... = (∏ x in (s.filter p).attach, h (f x.1 (mem_filter.mp x.2).2)) *
(∏ x in (s.filter (λ x, ¬ p x)).attach, h (g x.1 (mem_filter.mp x.2).2)) :
congr_arg2 _
(prod_congr rfl (λ x hx, congr_arg h (dif_pos (mem_filter.mp x.2).2)))
(prod_congr rfl (λ x hx, congr_arg h (dif_neg (mem_filter.mp x.2).2)))
@[to_additive] lemma prod_apply_ite {s : finset α}
{p : α → Prop} {hp : decidable_pred p} (f g : α → γ) (h : γ → β) :
(∏ x in s, h (if p x then f x else g x)) =
(∏ x in s.filter p, h (f x)) * (∏ x in s.filter (λ x, ¬ p x), h (g x)) :=
trans (prod_apply_dite _ _ _)
(congr_arg2 _ (@prod_attach _ _ _ _ (h ∘ f)) (@prod_attach _ _ _ _ (h ∘ g)))
@[to_additive] lemma prod_dite {s : finset α} {p : α → Prop} {hp : decidable_pred p}
(f : Π (x : α), p x → β) (g : Π (x : α), ¬p x → β) :
(∏ x in s, if hx : p x then f x hx else g x hx) =
(∏ x in (s.filter p).attach, f x.1 (mem_filter.mp x.2).2) *
(∏ x in (s.filter (λ x, ¬ p x)).attach, g x.1 (mem_filter.mp x.2).2) :=
by simp [prod_apply_dite _ _ (λ x, x)]
@[to_additive] lemma prod_ite {s : finset α}
{p : α → Prop} {hp : decidable_pred p} (f g : α → β) :
(∏ x in s, if p x then f x else g x) =
(∏ x in s.filter p, f x) * (∏ x in s.filter (λ x, ¬ p x), g x) :=
by simp [prod_apply_ite _ _ (λ x, x)]
@[to_additive] lemma prod_ite_of_false {p : α → Prop} {hp : decidable_pred p} (f g : α → β)
(h : ∀ x ∈ s, ¬p x) : (∏ x in s, if p x then f x else g x) = (∏ x in s, g x) :=
by { rw prod_ite, simp [filter_false_of_mem h, filter_true_of_mem h] }
@[to_additive] lemma prod_ite_of_true {p : α → Prop} {hp : decidable_pred p} (f g : α → β)
(h : ∀ x ∈ s, p x) : (∏ x in s, if p x then f x else g x) = (∏ x in s, f x) :=
by { simp_rw ←(ite_not (p _)), apply prod_ite_of_false, simpa }
@[to_additive] lemma prod_apply_ite_of_false {p : α → Prop} {hp : decidable_pred p} (f g : α → γ)
(k : γ → β) (h : ∀ x ∈ s, ¬p x) :
(∏ x in s, k (if p x then f x else g x)) = (∏ x in s, k (g x)) :=
by { simp_rw apply_ite k, exact prod_ite_of_false _ _ h }
@[to_additive] lemma prod_apply_ite_of_true {p : α → Prop} {hp : decidable_pred p} (f g : α → γ)
(k : γ → β) (h : ∀ x ∈ s, p x) :
(∏ x in s, k (if p x then f x else g x)) = (∏ x in s, k (f x)) :=
by { simp_rw apply_ite k, exact prod_ite_of_true _ _ h }
@[to_additive]
lemma prod_extend_by_one [decidable_eq α] (s : finset α) (f : α → β) :
∏ i in s, (if i ∈ s then f i else 1) = ∏ i in s, f i :=
prod_congr rfl $ λ i hi, if_pos hi
@[simp, to_additive]
lemma prod_dite_eq [decidable_eq α] (s : finset α) (a : α) (b : Π x : α, a = x → β) :
(∏ x in s, (if h : a = x then b x h else 1)) = ite (a ∈ s) (b a rfl) 1 :=
begin
split_ifs with h,
{ rw [finset.prod_eq_single a, dif_pos rfl],
{ intros, rw dif_neg, cc },
{ cc } },
{ rw finset.prod_eq_one,
intros, rw dif_neg, intro, cc }
end
@[simp, to_additive]
lemma prod_dite_eq' [decidable_eq α] (s : finset α) (a : α) (b : Π x : α, x = a → β) :
(∏ x in s, (if h : x = a then b x h else 1)) = ite (a ∈ s) (b a rfl) 1 :=
begin
split_ifs with h,
{ rw [finset.prod_eq_single a, dif_pos rfl],
{ intros, rw dif_neg, cc },
{ cc } },
{ rw finset.prod_eq_one,
intros, rw dif_neg, intro, cc }
end
@[simp, to_additive] lemma prod_ite_eq [decidable_eq α] (s : finset α) (a : α) (b : α → β) :
(∏ x in s, (ite (a = x) (b x) 1)) = ite (a ∈ s) (b a) 1 :=
prod_dite_eq s a (λ x _, b x)
/--
When a product is taken over a conditional whose condition is an equality test on the index
and whose alternative is 1, then the product's value is either the term at that index or `1`.
The difference with `prod_ite_eq` is that the arguments to `eq` are swapped.
-/
@[simp, to_additive] lemma prod_ite_eq' [decidable_eq α] (s : finset α) (a : α) (b : α → β) :
(∏ x in s, (ite (x = a) (b x) 1)) = ite (a ∈ s) (b a) 1 :=
prod_dite_eq' s a (λ x _, b x)
@[to_additive]
lemma prod_ite_index (p : Prop) [decidable p] (s t : finset α) (f : α → β) :
(∏ x in if p then s else t, f x) = if p then ∏ x in s, f x else ∏ x in t, f x :=
apply_ite (λ s, ∏ x in s, f x) _ _ _
@[simp, to_additive]
lemma prod_dite_irrel (p : Prop) [decidable p] (s : finset α) (f : p → α → β) (g : ¬p → α → β):
(∏ x in s, if h : p then f h x else g h x) = if h : p then ∏ x in s, f h x else ∏ x in s, g h x :=
by { split_ifs with h; refl }
@[simp] lemma sum_pi_single' {ι M : Type*} [decidable_eq ι] [add_comm_monoid M]
(i : ι) (x : M) (s : finset ι) :
∑ j in s, pi.single i x j = if i ∈ s then x else 0 :=
sum_dite_eq' _ _ _
@[simp] lemma sum_pi_single {ι : Type*} {M : ι → Type*}
[decidable_eq ι] [Π i, add_comm_monoid (M i)] (i : ι) (f : Π i, M i) (s : finset ι) :
∑ j in s, pi.single j (f j) i = if i ∈ s then f i else 0 :=
sum_dite_eq _ _ _
/--
Reorder a product.
The difference with `prod_bij'` is that the bijection is specified as a surjective injection,
rather than by an inverse function.
-/
@[to_additive "
Reorder a sum.
The difference with `sum_bij'` is that the bijection is specified as a surjective injection,
rather than by an inverse function.
"]
lemma prod_bij {s : finset α} {t : finset γ} {f : α → β} {g : γ → β}
(i : Π a ∈ s, γ) (hi : ∀ a ha, i a ha ∈ t) (h : ∀ a ha, f a = g (i a ha))
(i_inj : ∀ a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀ b ∈ t, ∃ a ha, b = i a ha) :
(∏ x in s, f x) = (∏ x in t, g x) :=
congr_arg multiset.prod
(multiset.map_eq_map_of_bij_of_nodup f g s.2 t.2 i hi h i_inj i_surj)
/--
Reorder a product.
The difference with `prod_bij` is that the bijection is specified with an inverse, rather than
as a surjective injection.
-/
@[to_additive "
Reorder a sum.
The difference with `sum_bij` is that the bijection is specified with an inverse, rather than
as a surjective injection.
"]
lemma prod_bij' {s : finset α} {t : finset γ} {f : α → β} {g : γ → β}
(i : Π a ∈ s, γ) (hi : ∀ a ha, i a ha ∈ t) (h : ∀ a ha, f a = g (i a ha))
(j : Π a ∈ t, α) (hj : ∀ a ha, j a ha ∈ s) (left_inv : ∀ a ha, j (i a ha) (hi a ha) = a)
(right_inv : ∀ a ha, i (j a ha) (hj a ha) = a) :
(∏ x in s, f x) = (∏ x in t, g x) :=
begin
refine prod_bij i hi h _ _,
{intros a1 a2 h1 h2 eq, rw [←left_inv a1 h1, ←left_inv a2 h2], cc,},
{intros b hb, use j b hb, use hj b hb, exact (right_inv b hb).symm,},
end
@[to_additive]
lemma prod_bij_ne_one {s : finset α} {t : finset γ} {f : α → β} {g : γ → β}
(i : Π a ∈ s, f a ≠ 1 → γ) (hi : ∀ a h₁ h₂, i a h₁ h₂ ∈ t)
(i_inj : ∀ a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂)
(i_surj : ∀ b ∈ t, g b ≠ 1 → ∃ a h₁ h₂, b = i a h₁ h₂)
(h : ∀ a h₁ h₂, f a = g (i a h₁ h₂)) :
(∏ x in s, f x) = (∏ x in t, g x) :=
by classical; exact
calc (∏ x in s, f x) = ∏ x in (s.filter $ λ x, f x ≠ 1), f x : prod_filter_ne_one.symm
... = ∏ x in (t.filter $ λ x, g x ≠ 1), g x :
prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2)
(assume a ha, (mem_filter.mp ha).elim $ λ h₁ h₂, mem_filter.mpr
⟨hi a h₁ h₂, λ hg, h₂ (hg ▸ h a h₁ h₂)⟩)
(assume a ha, (mem_filter.mp ha).elim $ h a)
(assume a₁ a₂ ha₁ ha₂,
(mem_filter.mp ha₁).elim $ λ ha₁₁ ha₁₂,
(mem_filter.mp ha₂).elim $ λ ha₂₁ ha₂₂, i_inj a₁ a₂ _ _ _ _)
(assume b hb, (mem_filter.mp hb).elim $ λ h₁ h₂,
let ⟨a, ha₁, ha₂, eq⟩ := i_surj b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩)
... = (∏ x in t, g x) : prod_filter_ne_one
@[to_additive]
lemma nonempty_of_prod_ne_one (h : (∏ x in s, f x) ≠ 1) : s.nonempty :=
s.eq_empty_or_nonempty.elim (λ H, false.elim $ h $ H.symm ▸ prod_empty) id
@[to_additive]
lemma exists_ne_one_of_prod_ne_one (h : (∏ x in s, f x) ≠ 1) : ∃ a ∈ s, f a ≠ 1 :=
begin
classical,
rw ← prod_filter_ne_one at h,
rcases nonempty_of_prod_ne_one h with ⟨x, hx⟩,
exact ⟨x, (mem_filter.1 hx).1, (mem_filter.1 hx).2⟩
end
@[to_additive]
lemma prod_subset_one_on_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) (hg : ∀ x ∈ (s₂ \ s₁), g x = 1)
(hfg : ∀ x ∈ s₁, f x = g x) : ∏ i in s₁, f i = ∏ i in s₂, g i :=
begin
rw [← prod_sdiff h, prod_eq_one hg, one_mul],
exact prod_congr rfl hfg
end
@[to_additive]
lemma prod_range_succ_comm (f : ℕ → β) (n : ℕ) :
∏ x in range (n + 1), f x = f n * ∏ x in range n, f x :=
by rw [range_succ, prod_insert not_mem_range_self]
@[to_additive]
lemma prod_range_succ (f : ℕ → β) (n : ℕ) :
∏ x in range (n + 1), f x = (∏ x in range n, f x) * f n :=
by simp only [mul_comm, prod_range_succ_comm]
@[to_additive]
lemma prod_range_succ' (f : ℕ → β) :
∀ n : ℕ, (∏ k in range (n + 1), f k) = (∏ k in range n, f (k+1)) * f 0
| 0 := prod_range_succ _ _
| (n + 1) := by rw [prod_range_succ _ n, mul_right_comm, ← prod_range_succ', prod_range_succ]
@[to_additive]
lemma eventually_constant_prod {u : ℕ → β} {N : ℕ} (hu : ∀ n ≥ N, u n = 1) {n : ℕ} (hn : N ≤ n) :
∏ k in range (n + 1), u k = ∏ k in range (N + 1), u k :=
begin
obtain ⟨m, rfl : n = N + m⟩ := le_iff_exists_add.mp hn,
clear hn,
induction m with m hm,
{ simp },
erw [prod_range_succ, hm],
simp [hu]
end
@[to_additive]
lemma prod_range_add (f : ℕ → β) (n m : ℕ) :
∏ x in range (n + m), f x =
(∏ x in range n, f x) * (∏ x in range m, f (n + x)) :=
begin
induction m with m hm,
{ simp },
{ rw [nat.add_succ, prod_range_succ, hm, prod_range_succ, mul_assoc], },
end
@[to_additive]
lemma prod_range_zero (f : ℕ → β) :
∏ k in range 0, f k = 1 :=
by rw [range_zero, prod_empty]
@[to_additive sum_range_one]
lemma prod_range_one (f : ℕ → β) :
∏ k in range 1, f k = f 0 :=
by { rw [range_one], apply @prod_singleton β ℕ 0 f }
open multiset
lemma prod_multiset_map_count [decidable_eq α] (s : multiset α)
{M : Type*} [comm_monoid M] (f : α → M) :
(s.map f).prod = ∏ m in s.to_finset, (f m) ^ (s.count m) :=
begin
apply s.induction_on, { simp only [prod_const_one, count_zero, prod_zero, pow_zero, map_zero] },
intros a s ih,
simp only [prod_cons, map_cons, to_finset_cons, ih],
by_cases has : a ∈ s.to_finset,
{ rw [insert_eq_of_mem has, ← insert_erase has, prod_insert (not_mem_erase _ _),
prod_insert (not_mem_erase _ _), ← mul_assoc, count_cons_self, pow_succ],
congr' 1, refine prod_congr rfl (λ x hx, _),
rw [count_cons_of_ne (ne_of_mem_erase hx)] },
rw [prod_insert has, count_cons_self, count_eq_zero_of_not_mem (mt mem_to_finset.2 has), pow_one],
congr' 1, refine prod_congr rfl (λ x hx, _),
rw count_cons_of_ne,
rintro rfl, exact has hx
end
lemma sum_multiset_map_count [decidable_eq α] (s : multiset α)
{M : Type*} [add_comm_monoid M] (f : α → M) :
(s.map f).sum = ∑ m in s.to_finset, s.count m • f m :=
@prod_multiset_map_count _ _ _ (multiplicative M) _ f
attribute [to_additive] prod_multiset_map_count
@[to_additive]
lemma prod_multiset_count [decidable_eq α] [comm_monoid α] (s : multiset α) :
s.prod = ∏ m in s.to_finset, m ^ (s.count m) :=
by { convert prod_multiset_map_count s id, rw map_id }
/--
To prove a property of a product, it suffices to prove that
the property is multiplicative and holds on factors.
-/
@[to_additive "To prove a property of a sum, it suffices to prove that
the property is additive and holds on summands."]
lemma prod_induction {M : Type*} [comm_monoid M] (f : α → M) (p : M → Prop)
(p_mul : ∀ a b, p a → p b → p (a * b)) (p_one : p 1) (p_s : ∀ x ∈ s, p $ f x) :
p $ ∏ x in s, f x :=
multiset.prod_induction _ _ p_mul p_one (multiset.forall_mem_map_iff.mpr p_s)
/--
To prove a property of a product, it suffices to prove that
the property is multiplicative and holds on factors.
-/
@[to_additive "To prove a property of a sum, it suffices to prove that
the property is additive and holds on summands."]
lemma prod_induction_nonempty {M : Type*} [comm_monoid M] (f : α → M) (p : M → Prop)
(p_mul : ∀ a b, p a → p b → p (a * b)) (hs_nonempty : s.nonempty) (p_s : ∀ x ∈ s, p $ f x) :
p $ ∏ x in s, f x :=
multiset.prod_induction_nonempty p p_mul (by simp [nonempty_iff_ne_empty.mp hs_nonempty])
(multiset.forall_mem_map_iff.mpr p_s)
/--
For any product along `{0, ..., n-1}` of a commutative-monoid-valued function, we can verify that
it's equal to a different function just by checking ratios of adjacent terms.
This is a multiplicative discrete analogue of the fundamental theorem of calculus. -/
lemma prod_range_induction {M : Type*} [comm_monoid M]
(f s : ℕ → M) (h0 : s 0 = 1) (h : ∀ n, s (n + 1) = s n * f n) (n : ℕ) :
∏ k in finset.range n, f k = s n :=
begin
induction n with k hk,
{ simp only [h0, finset.prod_range_zero] },
{ simp only [hk, finset.prod_range_succ, h, mul_comm] }
end
/--
For any sum along `{0, ..., n-1}` of a commutative-monoid-valued function,
we can verify that it's equal to a different function
just by checking differences of adjacent terms.
This is a discrete analogue
of the fundamental theorem of calculus.
-/
lemma sum_range_induction {M : Type*} [add_comm_monoid M]
(f s : ℕ → M) (h0 : s 0 = 0) (h : ∀ n, s (n + 1) = s n + f n) (n : ℕ) :
∑ k in finset.range n, f k = s n :=
@prod_range_induction (multiplicative M) _ f s h0 h n
/-- A telescoping sum along `{0, ..., n-1}` of an additive commutative group valued function
reduces to the difference of the last and first terms.-/
lemma sum_range_sub {G : Type*} [add_comm_group G] (f : ℕ → G) (n : ℕ) :
∑ i in range n, (f (i+1) - f i) = f n - f 0 :=
by { apply sum_range_induction; abel, simp }
lemma sum_range_sub' {G : Type*} [add_comm_group G] (f : ℕ → G) (n : ℕ) :
∑ i in range n, (f i - f (i+1)) = f 0 - f n :=
by { apply sum_range_induction; abel, simp }
/-- A telescoping product along `{0, ..., n-1}` of a commutative group valued function
reduces to the ratio of the last and first factors.-/
@[to_additive]
lemma prod_range_div {M : Type*} [comm_group M] (f : ℕ → M) (n : ℕ) :
∏ i in range n, (f (i+1) * (f i)⁻¹) = f n * (f 0)⁻¹ :=
by simpa only [← div_eq_mul_inv] using @sum_range_sub (additive M) _ f n
@[to_additive]
lemma prod_range_div' {M : Type*} [comm_group M] (f : ℕ → M) (n : ℕ) :
∏ i in range n, (f i * (f (i+1))⁻¹) = (f 0) * (f n)⁻¹ :=
by simpa only [← div_eq_mul_inv] using @sum_range_sub' (additive M) _ f n
/--
A telescoping sum along `{0, ..., n-1}` of an `ℕ`-valued function
reduces to the difference of the last and first terms
when the function we are summing is monotone.
-/
lemma sum_range_sub_of_monotone {f : ℕ → ℕ} (h : monotone f) (n : ℕ) :
∑ i in range n, (f (i+1) - f i) = f n - f 0 :=
begin
refine sum_range_induction _ _ (nat.sub_self _) (λ n, _) _,
have h₁ : f n ≤ f (n+1) := h (nat.le_succ _),
have h₂ : f 0 ≤ f n := h (nat.zero_le _),
rw [←nat.sub_add_comm h₂, nat.add_sub_cancel' h₁],
end
@[simp] lemma prod_const (b : β) : (∏ x in s, b) = b ^ s.card :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp) (λ a s has ih,
by rw [prod_insert has, card_insert_of_not_mem has, pow_succ, ih])
lemma pow_eq_prod_const (b : β) : ∀ n, b ^ n = ∏ k in range n, b
| 0 := by simp
| (n+1) := by simp
lemma prod_pow (s : finset α) (n : ℕ) (f : α → β) :
∏ x in s, f x ^ n = (∏ x in s, f x) ^ n :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp) (by simp [mul_pow] {contextual := tt})
@[to_additive]
lemma prod_flip {n : ℕ} (f : ℕ → β) :
∏ r in range (n + 1), f (n - r) = ∏ k in range (n + 1), f k :=
begin
induction n with n ih,
{ rw [prod_range_one, prod_range_one] },
{ rw [prod_range_succ', prod_range_succ _ (nat.succ n)],
simp [← ih] }
end
@[to_additive]
lemma prod_involution {s : finset α} {f : α → β} :
∀ (g : Π a ∈ s, α)
(h : ∀ a ha, f a * f (g a ha) = 1)
(g_ne : ∀ a ha, f a ≠ 1 → g a ha ≠ a)
(g_mem : ∀ a ha, g a ha ∈ s)
(g_inv : ∀ a ha, g (g a ha) (g_mem a ha) = a),
(∏ x in s, f x) = 1 :=
by haveI := classical.dec_eq α;
haveI := classical.dec_eq β; exact
finset.strong_induction_on s
(λ s ih g h g_ne g_mem g_inv,
s.eq_empty_or_nonempty.elim (λ hs, hs.symm ▸ rfl)
(λ ⟨x, hx⟩,
have hmem : ∀ y ∈ (s.erase x).erase (g x hx), y ∈ s,
from λ y hy, (mem_of_mem_erase (mem_of_mem_erase hy)),
have g_inj : ∀ {x hx y hy}, g x hx = g y hy → x = y,
from λ x hx y hy h, by rw [← g_inv x hx, ← g_inv y hy]; simp [h],
have ih': ∏ y in erase (erase s x) (g x hx), f y = (1 : β) :=
ih ((s.erase x).erase (g x hx))
⟨subset.trans (erase_subset _ _) (erase_subset _ _),
λ h, not_mem_erase (g x hx) (s.erase x) (h (g_mem x hx))⟩
(λ y hy, g y (hmem y hy))
(λ y hy, h y (hmem y hy))
(λ y hy, g_ne y (hmem y hy))
(λ y hy, mem_erase.2 ⟨λ (h : g y _ = g x hx), by simpa [g_inj h] using hy,
mem_erase.2 ⟨λ (h : g y _ = x),
have y = g x hx, from g_inv y (hmem y hy) ▸ by simp [h],
by simpa [this] using hy, g_mem y (hmem y hy)⟩⟩)
(λ y hy, g_inv y (hmem y hy)),
if hx1 : f x = 1
then ih' ▸ eq.symm (prod_subset hmem
(λ y hy hy₁,
have y = x ∨ y = g x hx, by simp [hy] at hy₁; tauto,
this.elim (λ hy, hy.symm ▸ hx1)
(λ hy, h x hx ▸ hy ▸ hx1.symm ▸ (one_mul _).symm)))
else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _),
← insert_erase (mem_erase.2 ⟨g_ne x hx hx1, g_mem x hx⟩),
prod_insert (not_mem_erase _ _), ih', mul_one, h x hx]))
/-- The product of the composition of functions `f` and `g`, is the product
over `b ∈ s.image g` of `f b` to the power of the cardinality of the fibre of `b` -/
lemma prod_comp [decidable_eq γ] {s : finset α} (f : γ → β) (g : α → γ) :
∏ a in s, f (g a) = ∏ b in s.image g, f b ^ (s.filter (λ a, g a = b)).card :=
calc ∏ a in s, f (g a)
= ∏ x in (s.image g).sigma (λ b : γ, s.filter (λ a, g a = b)), f (g x.2) :
prod_bij (λ a ha, ⟨g a, a⟩) (by simp; tauto) (λ _ _, rfl) (by simp) (by finish)
... = ∏ b in s.image g, ∏ a in s.filter (λ a, g a = b), f (g a) : prod_sigma _ _ _
... = ∏ b in s.image g, ∏ a in s.filter (λ a, g a = b), f b :
prod_congr rfl (λ b hb, prod_congr rfl (by simp {contextual := tt}))
... = ∏ b in s.image g, f b ^ (s.filter (λ a, g a = b)).card :
prod_congr rfl (λ _ _, prod_const _)
@[to_additive]
lemma prod_piecewise [decidable_eq α] (s t : finset α) (f g : α → β) :
(∏ x in s, (t.piecewise f g) x) = (∏ x in s ∩ t, f x) * (∏ x in s \ t, g x) :=
by { rw [piecewise, prod_ite, filter_mem_eq_inter, ← sdiff_eq_filter], }
@[to_additive]
lemma prod_inter_mul_prod_diff [decidable_eq α] (s t : finset α) (f : α → β) :
(∏ x in s ∩ t, f x) * (∏ x in s \ t, f x) = (∏ x in s, f x) :=
by { convert (s.prod_piecewise t f f).symm, simp [finset.piecewise] }
@[to_additive]
lemma prod_eq_mul_prod_diff_singleton [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s)
(f : α → β) : ∏ x in s, f x = f i * ∏ x in s \ {i}, f x :=
by { convert (s.prod_inter_mul_prod_diff {i} f).symm, simp [h] }
@[to_additive]
lemma prod_eq_prod_diff_singleton_mul [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s)
(f : α → β) : ∏ x in s, f x = (∏ x in s \ {i}, f x) * f i :=
by { rw [prod_eq_mul_prod_diff_singleton h, mul_comm] }
@[to_additive]
lemma _root_.fintype.prod_eq_mul_prod_compl [decidable_eq α] [fintype α] (a : α) (f : α → β) :
∏ i, f i = (f a) * ∏ i in {a}ᶜ, f i :=
prod_eq_mul_prod_diff_singleton (mem_univ a) f
@[to_additive]
lemma _root_.fintype.prod_eq_prod_compl_mul [decidable_eq α] [fintype α] (a : α) (f : α → β) :
∏ i, f i = (∏ i in {a}ᶜ, f i) * f a :=
prod_eq_prod_diff_singleton_mul (mem_univ a) f
/-- A product can be partitioned into a product of products, each equivalent under a setoid. -/
@[to_additive "A sum can be partitioned into a sum of sums, each equivalent under a setoid."]
lemma prod_partition (R : setoid α) [decidable_rel R.r] :
(∏ x in s, f x) = ∏ xbar in s.image quotient.mk, ∏ y in s.filter (λ y, ⟦y⟧ = xbar), f y :=
begin
refine (finset.prod_image' f (λ x hx, _)).symm,
refl,
end
/-- If we can partition a product into subsets that cancel out, then the whole product cancels. -/
@[to_additive "If we can partition a sum into subsets that cancel out, then the whole sum cancels."]
lemma prod_cancels_of_partition_cancels (R : setoid α) [decidable_rel R.r]
(h : ∀ x ∈ s, (∏ a in s.filter (λ y, y ≈ x), f a) = 1) : (∏ x in s, f x) = 1 :=
begin
rw [prod_partition R, ←finset.prod_eq_one],
intros xbar xbar_in_s,
obtain ⟨x, x_in_s, xbar_eq_x⟩ := mem_image.mp xbar_in_s,
rw [←xbar_eq_x, filter_congr (λ y _, @quotient.eq _ R y x)],
apply h x x_in_s,
end
@[to_additive]
lemma prod_update_of_not_mem [decidable_eq α] {s : finset α} {i : α}
(h : i ∉ s) (f : α → β) (b : β) : (∏ x in s, function.update f i b x) = (∏ x in s, f x) :=
begin
apply prod_congr rfl (λ j hj, _),
have : j ≠ i, by { assume eq, rw eq at hj, exact h hj },
simp [this]
end
lemma prod_update_of_mem [decidable_eq α] {s : finset α} {i : α} (h : i ∈ s) (f : α → β) (b : β) :
(∏ x in s, function.update f i b x) = b * (∏ x in s \ (singleton i), f x) :=
by { rw [update_eq_piecewise, prod_piecewise], simp [h] }
/-- If a product of a `finset` of size at most 1 has a given value, so
do the terms in that product. -/
@[to_additive eq_of_card_le_one_of_sum_eq "If a sum of a `finset` of size at most 1 has a given
value, so do the terms in that sum."]
lemma eq_of_card_le_one_of_prod_eq {s : finset α} (hc : s.card ≤ 1) {f : α → β} {b : β}
(h : ∏ x in s, f x = b) : ∀ x ∈ s, f x = b :=
begin
intros x hx,
by_cases hc0 : s.card = 0,
{ exact false.elim (card_ne_zero_of_mem hx hc0) },
{ have h1 : s.card = 1 := le_antisymm hc (nat.one_le_of_lt (nat.pos_of_ne_zero hc0)),
rw card_eq_one at h1,
cases h1 with x2 hx2,
rw [hx2, mem_singleton] at hx,
simp_rw hx2 at h,
rw hx,
rw prod_singleton at h,
exact h }
end
/-- If a function applied at a point is 1, a product is unchanged by
removing that point, if present, from a `finset`. -/
@[to_additive "If a function applied at a point is 0, a sum is unchanged by
removing that point, if present, from a `finset`."]
lemma prod_erase [decidable_eq α] (s : finset α) {f : α → β} {a : α} (h : f a = 1) :
∏ x in s.erase a, f x = ∏ x in s, f x :=
begin
rw ←sdiff_singleton_eq_erase,
refine prod_subset (sdiff_subset _ _) (λ x hx hnx, _),
rw sdiff_singleton_eq_erase at hnx,
rwa eq_of_mem_of_not_mem_erase hx hnx
end
/-- If a product is 1 and the function is 1 except possibly at one
point, it is 1 everywhere on the `finset`. -/
@[to_additive "If a sum is 0 and the function is 0 except possibly at one
point, it is 0 everywhere on the `finset`."]
lemma eq_one_of_prod_eq_one {s : finset α} {f : α → β} {a : α} (hp : ∏ x in s, f x = 1)
(h1 : ∀ x ∈ s, x ≠ a → f x = 1) : ∀ x ∈ s, f x = 1 :=
begin
intros x hx,
classical,
by_cases h : x = a,
{ rw h,
rw h at hx,
rw [←prod_subset (singleton_subset_iff.2 hx)
(λ t ht ha, h1 t ht (not_mem_singleton.1 ha)),
prod_singleton] at hp,
exact hp },
{ exact h1 x hx h }
end
lemma prod_pow_boole [decidable_eq α] (s : finset α) (f : α → β) (a : α) :
(∏ x in s, (f x)^(ite (a = x) 1 0)) = ite (a ∈ s) (f a) 1 :=
by simp
end comm_monoid
/-- If `f = g = h` everywhere but at `i`, where `f i = g i + h i`, then the product of `f` over `s`
is the sum of the products of `g` and `h`. -/
lemma prod_add_prod_eq [comm_semiring β] {s : finset α} {i : α} {f g h : α → β}
(hi : i ∈ s) (h1 : g i + h i = f i) (h2 : ∀ j ∈ s, j ≠ i → g j = f j)
(h3 : ∀ j ∈ s, j ≠ i → h j = f j) : ∏ i in s, g i + ∏ i in s, h i = ∏ i in s, f i :=
by { classical, simp_rw [prod_eq_mul_prod_diff_singleton hi, ← h1, right_distrib],
congr' 2; apply prod_congr rfl; simpa }
lemma sum_update_of_mem [add_comm_monoid β] [decidable_eq α] {s : finset α} {i : α}
(h : i ∈ s) (f : α → β) (b : β) :
(∑ x in s, function.update f i b x) = b + (∑ x in s \ (singleton i), f x) :=
by { rw [update_eq_piecewise, sum_piecewise], simp [h] }
attribute [to_additive] prod_update_of_mem
lemma sum_nsmul [add_comm_monoid β] (s : finset α) (n : ℕ) (f : α → β) :
(∑ x in s, n • (f x)) = n • ((∑ x in s, f x)) :=
@prod_pow (multiplicative β) _ _ _ _ _
attribute [to_additive sum_nsmul] prod_pow
@[simp] lemma sum_const [add_comm_monoid β] (b : β) :
(∑ x in s, b) = s.card • b :=
@prod_const (multiplicative β) _ _ _ _
attribute [to_additive] prod_const
lemma card_eq_sum_ones (s : finset α) : s.card = ∑ _ in s, 1 :=
by simp
lemma sum_const_nat {m : ℕ} {f : α → ℕ} (h₁ : ∀ x ∈ s, f x = m) :
(∑ x in s, f x) = card s * m :=
begin
rw [← nat.nsmul_eq_mul, ← sum_const],
apply sum_congr rfl h₁
end
@[simp]
lemma sum_boole {s : finset α} {p : α → Prop} [non_assoc_semiring β] {hp : decidable_pred p} :
(∑ x in s, if p x then (1 : β) else (0 : β)) = (s.filter p).card :=
by simp [sum_ite]
lemma sum_comp [add_comm_monoid β] [decidable_eq γ] {s : finset α} (f : γ → β) (g : α → γ) :
∑ a in s, f (g a) = ∑ b in s.image g, (s.filter (λ a, g a = b)).card • (f b) :=
@prod_comp (multiplicative β) _ _ _ _ _ _ _
attribute [to_additive "The sum of the composition of functions `f` and `g`, is the sum
over `b ∈ s.image g` of `f b` times of the cardinality of the fibre of `b`"] prod_comp
lemma eq_sum_range_sub [add_comm_group β] (f : ℕ → β) (n : ℕ) :
f n = f 0 + ∑ i in range n, (f (i+1) - f i) :=
by { rw finset.sum_range_sub, abel }
lemma eq_sum_range_sub' [add_comm_group β] (f : ℕ → β) (n : ℕ) :
f n = ∑ i in range (n + 1), if i = 0 then f 0 else f i - f (i - 1) :=
begin
conv_lhs { rw [finset.eq_sum_range_sub f] },
simp [finset.sum_range_succ', add_comm]
end
section opposite
open opposite
/-- Moving to the opposite additive commutative monoid commutes with summing. -/
@[simp] lemma op_sum [add_comm_monoid β] {s : finset α} (f : α → β) :
op (∑ x in s, f x) = ∑ x in s, op (f x) :=
(op_add_equiv : β ≃+ βᵒᵖ).map_sum _ _
@[simp] lemma unop_sum [add_comm_monoid β] {s : finset α} (f : α → βᵒᵖ) :
unop (∑ x in s, f x) = ∑ x in s, unop (f x) :=
(op_add_equiv : β ≃+ βᵒᵖ).symm.map_sum _ _
end opposite
section comm_group
variables [comm_group β]
@[simp, to_additive]
lemma prod_inv_distrib : (∏ x in s, (f x)⁻¹) = (∏ x in s, f x)⁻¹ :=
s.prod_hom has_inv.inv
end comm_group
@[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) :
card (s.sigma t) = ∑ a in s, card (t a) :=
multiset.card_sigma _ _
lemma card_bUnion [decidable_eq β] {s : finset α} {t : α → finset β}
(h : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → disjoint (t x) (t y)) :
(s.bUnion t).card = ∑ u in s, card (t u) :=
calc (s.bUnion t).card = ∑ i in s.bUnion t, 1 : by simp
... = ∑ a in s, ∑ i in t a, 1 : finset.sum_bUnion h
... = ∑ u in s, card (t u) : by simp
lemma card_bUnion_le [decidable_eq β] {s : finset α} {t : α → finset β} :
(s.bUnion t).card ≤ ∑ a in s, (t a).card :=
by haveI := classical.dec_eq α; exact
finset.induction_on s (by simp)
(λ a s has ih,
calc ((insert a s).bUnion t).card ≤ (t a).card + (s.bUnion t).card :
by rw bUnion_insert; exact finset.card_union_le _ _
... ≤ ∑ a in insert a s, card (t a) :
by rw sum_insert has; exact add_le_add_left ih _)
theorem card_eq_sum_card_fiberwise [decidable_eq β] {f : α → β} {s : finset α} {t : finset β}
(H : ∀ x ∈ s, f x ∈ t) :
s.card = ∑ a in t, (s.filter (λ x, f x = a)).card :=
by simp only [card_eq_sum_ones, sum_fiberwise_of_maps_to H]
theorem card_eq_sum_card_image [decidable_eq β] (f : α → β) (s : finset α) :
s.card = ∑ a in s.image f, (s.filter (λ x, f x = a)).card :=
card_eq_sum_card_fiberwise (λ _, mem_image_of_mem _)
lemma gsmul_sum [add_comm_group β] {f : α → β} {s : finset α} (z : ℤ) :
gsmul z (∑ a in s, f a) = ∑ a in s, gsmul z (f a) :=
(s.sum_hom (gsmul z)).symm
@[simp] lemma sum_sub_distrib [add_comm_group β] :
∑ x in s, (f x - g x) = (∑ x in s, f x) - (∑ x in s, g x) :=
by simpa only [sub_eq_add_neg] using sum_add_distrib.trans (congr_arg _ sum_neg_distrib)
section prod_eq_zero
variables [comm_monoid_with_zero β]
lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : (∏ x in s, f x) = 0 :=
by haveI := classical.dec_eq α;
calc (∏ x in s, f x) = ∏ x in insert a (erase s a), f x : by rw insert_erase ha
... = 0 : by rw [prod_insert (not_mem_erase _ _), h, zero_mul]
lemma prod_boole {s : finset α} {p : α → Prop} [decidable_pred p] :
∏ i in s, ite (p i) (1 : β) (0 : β) = ite (∀ i ∈ s, p i) 1 0 :=
begin
split_ifs,
{ apply prod_eq_one,
intros i hi,
rw if_pos (h i hi) },
{ push_neg at h,
rcases h with ⟨i, hi, hq⟩,
apply prod_eq_zero hi,
rw [if_neg hq] },
end
variables [nontrivial β] [no_zero_divisors β]
lemma prod_eq_zero_iff : (∏ x in s, f x) = 0 ↔ (∃ a ∈ s, f a = 0) :=
begin
classical,
apply finset.induction_on s,
exact ⟨not.elim one_ne_zero, λ ⟨_, H, _⟩, H.elim⟩,
assume a s ha ih,
rw [prod_insert ha, mul_eq_zero, bex_def, exists_mem_insert, ih, ← bex_def]
end
theorem prod_ne_zero_iff : (∏ x in s, f x) ≠ 0 ↔ (∀ a ∈ s, f a ≠ 0) :=
by { rw [ne, prod_eq_zero_iff], push_neg }
end prod_eq_zero
section comm_group_with_zero
variables [comm_group_with_zero β]
@[simp]
lemma prod_inv_distrib' : (∏ x in s, (f x)⁻¹) = (∏ x in s, f x)⁻¹ :=
begin
classical,
by_cases h : ∃ x ∈ s, f x = 0,
{ simpa [prod_eq_zero_iff.mpr h, prod_eq_zero_iff] using h },
{ push_neg at h,
have h' := prod_ne_zero_iff.mpr h,
have hf : ∀ x ∈ s, (f x)⁻¹ * f x = 1 := λ x hx, inv_mul_cancel (h x hx),
apply mul_right_cancel' h',
simp [h, h', ← finset.prod_mul_distrib, prod_congr rfl hf] }
end
end comm_group_with_zero
end finset
namespace fintype
open finset
/-- `fintype.prod_bijective` is a variant of `finset.prod_bij` that accepts `function.bijective`.
See `function.bijective.prod_comp` for a version without `h`. -/
@[to_additive "`fintype.sum_equiv` is a variant of `finset.sum_bij` that accepts
`function.bijective`.
See `function.bijective.sum_comp` for a version without `h`. "]
lemma prod_bijective {α β M : Type*} [fintype α] [fintype β] [comm_monoid M]
(e : α → β) (he : function.bijective e) (f : α → M) (g : β → M) (h : ∀ x, f x = g (e x)) :
∏ x : α, f x = ∏ x : β, g x :=
prod_bij
(λ x _, e x)
(λ x _, mem_univ (e x))
(λ x _, h x)
(λ x x' _ _ h, he.injective h)
(λ y _, (he.surjective y).imp $ λ a h, ⟨mem_univ _, h.symm⟩)
/-- `fintype.prod_equiv` is a specialization of `finset.prod_bij` that
automatically fills in most arguments.
See `equiv.prod_comp` for a version without `h`.
-/
@[to_additive "`fintype.sum_equiv` is a specialization of `finset.sum_bij` that
automatically fills in most arguments.
See `equiv.sum_comp` for a version without `h`.
"]
lemma prod_equiv {α β M : Type*} [fintype α] [fintype β] [comm_monoid M]
(e : α ≃ β) (f : α → M) (g : β → M) (h : ∀ x, f x = g (e x)) :
∏ x : α, f x = ∏ x : β, g x :=
prod_bijective e e.bijective f g h
@[to_additive]
lemma prod_finset_coe [comm_monoid β] :
∏ (i : (s : set α)), f i = ∏ i in s, f i :=
(finset.prod_subtype s (λ _, iff.rfl) f).symm
end fintype
namespace list
@[to_additive] lemma prod_to_finset {M : Type*} [decidable_eq α] [comm_monoid M]
(f : α → M) : ∀ {l : list α} (hl : l.nodup), l.to_finset.prod f = (l.map f).prod
| [] _ := by simp
| (a :: l) hl := let ⟨not_mem, hl⟩ := list.nodup_cons.mp hl in
by simp [finset.prod_insert (mt list.mem_to_finset.mp not_mem), prod_to_finset hl]
end list
namespace multiset
lemma abs_sum_le_sum_abs [linear_ordered_add_comm_group α] {s : multiset α} :
abs s.sum ≤ (s.map abs).sum :=
le_sum_of_subadditive _ abs_zero abs_add s
variables [decidable_eq α]
@[simp] lemma to_finset_sum_count_eq (s : multiset α) :
(∑ a in s.to_finset, s.count a) = s.card :=
multiset.induction_on s rfl
(assume a s ih,
calc (∑ x in to_finset (a ::ₘ s), count x (a ::ₘ s)) =
∑ x in to_finset (a ::ₘ s), ((if x = a then 1 else 0) + count x s) :
finset.sum_congr rfl $ λ _ _, by split_ifs;
[simp only [h, count_cons_self, nat.one_add], simp only [count_cons_of_ne h, zero_add]]
... = card (a ::ₘ s) :
begin
by_cases a ∈ s.to_finset,
{ have : ∑ x in s.to_finset, ite (x = a) 1 0 = ∑ x in {a}, ite (x = a) 1 0,
{ rw [finset.sum_ite_eq', if_pos h, finset.sum_singleton, if_pos rfl], },
rw [to_finset_cons, finset.insert_eq_of_mem h, finset.sum_add_distrib, ih, this,
finset.sum_singleton, if_pos rfl, add_comm, card_cons] },
{ have ha : a ∉ s, by rwa mem_to_finset at h,
have : ∑ x in to_finset s, ite (x = a) 1 0 = ∑ x in to_finset s, 0, from
finset.sum_congr rfl (λ x hx, if_neg $ by rintro rfl; cc),
rw [to_finset_cons, finset.sum_insert h, if_pos rfl, finset.sum_add_distrib, this,
finset.sum_const_zero, ih, count_eq_zero_of_not_mem ha, zero_add, add_comm, card_cons] }
end)
lemma count_sum' {s : finset β} {a : α} {f : β → multiset α} :
count a (∑ x in s, f x) = ∑ x in s, count a (f x) :=
by { dunfold finset.sum, rw count_sum }
@[simp] lemma to_finset_sum_count_nsmul_eq (s : multiset α) :
(∑ a in s.to_finset, s.count a • (a ::ₘ 0)) = s :=
begin
apply ext', intro b,
rw count_sum',
have h : count b s = count b (count b s • (b ::ₘ 0)),
{ rw [singleton_coe, count_nsmul, ← singleton_coe, count_singleton, mul_one] },
rw h, clear h,
apply finset.sum_eq_single b,
{ intros c h hcb, rw count_nsmul, convert mul_zero (count c s),
apply count_eq_zero.mpr, exact finset.not_mem_singleton.mpr (ne.symm hcb) },
{ intro hb, rw [count_eq_zero_of_not_mem (mt mem_to_finset.2 hb), count_nsmul, zero_mul]}
end
theorem exists_smul_of_dvd_count (s : multiset α) {k : ℕ} (h : ∀ (a : α), k ∣ multiset.count a s) :
∃ (u : multiset α), s = k • u :=
begin
use ∑ a in s.to_finset, (s.count a / k) • (a ::ₘ 0),
have h₂ : ∑ (x : α) in s.to_finset, k • (count x s / k) • (x ::ₘ 0) =
∑ (x : α) in s.to_finset, count x s • (x ::ₘ 0),
{ refine congr_arg s.to_finset.sum _,
apply funext, intro x,
rw [← mul_nsmul, nat.mul_div_cancel' (h x)] },
rw [← finset.sum_nsmul, h₂, to_finset_sum_count_nsmul_eq]
end
end multiset
@[simp, norm_cast] lemma nat.cast_sum [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) :
↑(∑ x in s, f x : ℕ) = (∑ x in s, (f x : β)) :=
(nat.cast_add_monoid_hom β).map_sum f s
@[simp, norm_cast] lemma int.cast_sum [add_comm_group β] [has_one β] (s : finset α) (f : α → ℤ) :
↑(∑ x in s, f x : ℤ) = (∑ x in s, (f x : β)) :=
(int.cast_add_hom β).map_sum f s
@[simp, norm_cast] lemma nat.cast_prod {R : Type*} [comm_semiring R] (f : α → ℕ) (s : finset α) :
(↑∏ i in s, f i : R) = ∏ i in s, f i :=
(nat.cast_ring_hom R).map_prod _ _
@[simp, norm_cast] lemma int.cast_prod {R : Type*} [comm_ring R] (f : α → ℤ) (s : finset α) :
(↑∏ i in s, f i : R) = ∏ i in s, f i :=
(int.cast_ring_hom R).map_prod _ _
@[simp, norm_cast] lemma units.coe_prod {M : Type*} [comm_monoid M] (f : α → units M)
(s : finset α) : (↑∏ i in s, f i : M) = ∏ i in s, f i :=
(units.coe_hom M).map_prod _ _
lemma nat_abs_sum_le {ι : Type*} (s : finset ι) (f : ι → ℤ) :
(∑ i in s, f i).nat_abs ≤ ∑ i in s, (f i).nat_abs :=
begin
classical,
apply finset.induction_on s,
{ simp only [finset.sum_empty, int.nat_abs_zero] },
{ intros i s his IH,
simp only [his, finset.sum_insert, not_false_iff],
exact (int.nat_abs_add_le _ _).trans (add_le_add le_rfl IH) }
end
|
4eed8fe74cd184133b03adcc75814d7d95b44ccd | e61a235b8468b03aee0120bf26ec615c045005d2 | /src/Init/Data/PersistentArray/Basic.lean | 435e05f079e7019b484acc881fa0ad35ddf81390 | [
"Apache-2.0"
] | permissive | SCKelemen/lean4 | 140dc63a80539f7c61c8e43e1c174d8500ec3230 | e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc | refs/heads/master | 1,660,973,595,917 | 1,590,278,033,000 | 1,590,278,033,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 12,318 | 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.Control.Conditional
import Init.Data.Array
universes u v w
inductive PersistentArrayNode (α : Type u)
| node (cs : Array PersistentArrayNode) : PersistentArrayNode
| leaf (vs : Array α) : PersistentArrayNode
namespace PersistentArrayNode
instance {α : Type u} : Inhabited (PersistentArrayNode α) := ⟨leaf #[]⟩
def isNode {α} : PersistentArrayNode α → Bool
| node _ => true
| leaf _ => false
end PersistentArrayNode
abbrev PersistentArray.initShift : USize := 5
abbrev PersistentArray.branching : USize := USize.ofNat (2 ^ PersistentArray.initShift.toNat)
structure PersistentArray (α : Type u) :=
/- Recall that we run out of memory if we have more than `usizeSz/8` elements.
So, we can stop adding elements at `root` after `size > usizeSz`, and
keep growing the `tail`. This modification allow us to use `USize` instead
of `Nat` when traversing `root`. -/
(root : PersistentArrayNode α := PersistentArrayNode.node (Array.mkEmpty PersistentArray.branching.toNat))
(tail : Array α := Array.mkEmpty PersistentArray.branching.toNat)
(size : Nat := 0)
(shift : USize := PersistentArray.initShift)
(tailOff : Nat := 0)
abbrev PArray (α : Type u) := PersistentArray α
namespace PersistentArray
/- TODO: use proofs for showing that array accesses are not out of bounds.
We can do it after we reimplement the tactic framework. -/
variables {α : Type u}
open PersistentArrayNode
def empty : PersistentArray α :=
{}
def isEmpty (a : PersistentArray α) : Bool :=
a.size == 0
instance : Inhabited (PersistentArray α) := ⟨{}⟩
def mkEmptyArray : Array α := Array.mkEmpty branching.toNat
abbrev mul2Shift (i : USize) (shift : USize) : USize := i.shiftLeft shift
abbrev div2Shift (i : USize) (shift : USize) : USize := i.shiftRight shift
abbrev mod2Shift (i : USize) (shift : USize) : USize := USize.land i ((USize.shiftLeft 1 shift) - 1)
partial def getAux [Inhabited α] : PersistentArrayNode α → USize → USize → α
| node cs, i, shift => getAux (cs.get! (div2Shift i shift).toNat) (mod2Shift i shift) (shift - initShift)
| leaf cs, i, _ => cs.get! i.toNat
def get! [Inhabited α] (t : PersistentArray α) (i : Nat) : α :=
if i >= t.tailOff then
t.tail.get! (i - t.tailOff)
else
getAux t.root (USize.ofNat i) t.shift
def getOp [Inhabited α] (self : PersistentArray α) (idx : Nat) : α :=
self.get! idx
partial def setAux : PersistentArrayNode α → USize → USize → α → PersistentArrayNode α
| node cs, i, shift, a =>
let j := div2Shift i shift;
let i := mod2Shift i shift;
let shift := shift - initShift;
node $ cs.modify j.toNat $ fun c => setAux c i shift a
| leaf cs, i, _, a => leaf (cs.set! i.toNat a)
def set (t : PersistentArray α) (i : Nat) (a : α) : PersistentArray α :=
if i >= t.tailOff then
{ t with tail := t.tail.set! (i - t.tailOff) a }
else
{ t with root := setAux t.root (USize.ofNat i) t.shift a }
@[specialize] partial def modifyAux [Inhabited α] (f : α → α) : PersistentArrayNode α → USize → USize → PersistentArrayNode α
| node cs, i, shift =>
let j := div2Shift i shift;
let i := mod2Shift i shift;
let shift := shift - initShift;
node $ cs.modify j.toNat $ fun c => modifyAux c i shift
| leaf cs, i, _ => leaf (cs.modify i.toNat f)
@[specialize] def modify [Inhabited α] (t : PersistentArray α) (i : Nat) (f : α → α) : PersistentArray α :=
if i >= t.tailOff then
{ t with tail := t.tail.modify (i - t.tailOff) f }
else
{ t with root := modifyAux f t.root (USize.ofNat i) t.shift }
partial def mkNewPath : USize → Array α → PersistentArrayNode α
| shift, a =>
if shift == 0 then
leaf a
else
node (mkEmptyArray.push (mkNewPath (shift - initShift) a))
partial def insertNewLeaf : PersistentArrayNode α → USize → USize → Array α → PersistentArrayNode α
| node cs, i, shift, a =>
if i < branching then
node (cs.push (leaf a))
else
let j := div2Shift i shift;
let i := mod2Shift i shift;
let shift := shift - initShift;
if j.toNat < cs.size then
node $ cs.modify j.toNat $ fun c => insertNewLeaf c i shift a
else
node $ cs.push $ mkNewPath shift a
| n, _, _, _ => n -- unreachable
def mkNewTail (t : PersistentArray α) : PersistentArray α :=
if t.size <= (mul2Shift 1 (t.shift + initShift)).toNat then
{ t with
tail := mkEmptyArray, root := insertNewLeaf t.root (USize.ofNat (t.size - 1)) t.shift t.tail,
tailOff := t.size }
else
{ t with
tail := #[],
root := let n := mkEmptyArray.push t.root;
node (n.push (mkNewPath t.shift t.tail)),
shift := t.shift + initShift,
tailOff := t.size }
def tooBig : Nat := usizeSz / 8
def push (t : PersistentArray α) (a : α) : PersistentArray α :=
let r := { t with tail := t.tail.push a, size := t.size + 1 };
if r.tail.size < branching.toNat || t.size >= tooBig then
r
else
mkNewTail r
private def emptyArray {α : Type u} : Array (PersistentArrayNode α) :=
Array.mkEmpty PersistentArray.branching.toNat
partial def popLeaf : PersistentArrayNode α → Option (Array α) × Array (PersistentArrayNode α)
| n@(node cs) =>
if h : cs.size ≠ 0 then
let idx : Fin cs.size := ⟨cs.size - 1, Nat.predLt h⟩;
let last := cs.get idx;
let cs := cs.set idx (arbitrary _);
match popLeaf last with
| (none, _) => (none, emptyArray)
| (some l, newLast) =>
if newLast.size == 0 then
let cs := cs.pop;
if cs.isEmpty then (some l, emptyArray) else (some l, cs)
else
(some l, cs.set idx (node newLast))
else
(none, emptyArray)
| leaf vs => (some vs, emptyArray)
def pop (t : PersistentArray α) : PersistentArray α :=
if t.tail.size > 0 then
{ t with tail := t.tail.pop, size := t.size - 1 }
else
match popLeaf t.root with
| (none, _) => t
| (some last, newRoots) =>
let last := last.pop;
let newSize := t.size - 1;
let newTailOff := newSize - last.size;
if newRoots.size == 1 && (newRoots.get! 0).isNode then
{ root := newRoots.get! 0,
shift := t.shift - initShift,
size := newSize,
tail := last,
tailOff := newTailOff }
else
{ t with
root := node newRoots,
size := newSize,
tail := last,
tailOff := newTailOff }
section
variables {m : Type v → Type w} [Monad m]
variable {β : Type v}
@[specialize] partial def foldlMAux (f : β → α → m β) : PersistentArrayNode α → β → m β
| node cs, b => cs.foldlM (fun b c => foldlMAux c b) b
| leaf vs, b => vs.foldlM f b
@[specialize] def foldlM (t : PersistentArray α) (f : β → α → m β) (b : β) : m β := do
b ← foldlMAux f t.root b; t.tail.foldlM f b
@[specialize] partial def findSomeMAux (f : α → m (Option β)) : PersistentArrayNode α → m (Option β)
| node cs => cs.findSomeM? (fun c => findSomeMAux c)
| leaf vs => vs.findSomeM? f
@[specialize] def findSomeM? (t : PersistentArray α) (f : α → m (Option β)) : m (Option β) := do
b ← findSomeMAux f t.root;
match b with
| none => t.tail.findSomeM? f
| some b => pure (some b)
@[specialize] partial def findSomeRevMAux (f : α → m (Option β)) : PersistentArrayNode α → m (Option β)
| node cs => cs.findSomeRevM? (fun c => findSomeRevMAux c)
| leaf vs => vs.findSomeRevM? f
@[specialize] def findSomeRevM? (t : PersistentArray α) (f : α → m (Option β)) : m (Option β) := do
b ← t.tail.findSomeRevM? f;
match b with
| none => findSomeRevMAux f t.root
| some b => pure (some b)
partial def foldlFromMAux (f : β → α → m β) : PersistentArrayNode α → USize → USize → β → m β
| node cs, i, shift, b => do
let j := (div2Shift i shift).toNat;
b ← foldlFromMAux (cs.get! j) (mod2Shift i shift) (shift - initShift) b;
cs.foldlFromM (fun b c => foldlMAux f c b) b (j+1)
| leaf vs, i, _, b => vs.foldlFromM f b i.toNat
def foldlFromM (t : PersistentArray α) (f : β → α → m β) (b : β) (ini : Nat) : m β :=
if ini >= t.tailOff then
t.tail.foldlFromM f b (ini - t.tailOff)
else do
b ← foldlFromMAux f t.root (USize.ofNat ini) t.shift b;
t.tail.foldlM f b
@[specialize] partial def forMAux (f : α → m PUnit) : PersistentArrayNode α → m PUnit
| node cs => cs.forM (fun c => forMAux c)
| leaf vs => vs.forM f
@[specialize] def forM (t : PersistentArray α) (f : α → m PUnit) : m PUnit :=
forMAux f t.root *> t.tail.forM f
end
@[inline] def foldl {β} (t : PersistentArray α) (f : β → α → β) (b : β) : β :=
Id.run (t.foldlM f b)
def toArray (t : PersistentArray α) : Array α :=
t.foldl Array.push #[]
def append (t₁ t₂ : PersistentArray α) : PersistentArray α :=
if t₁.isEmpty then t₂
else t₂.foldl PersistentArray.push t₁
instance : HasAppend (PersistentArray α) := ⟨append⟩
@[inline] def findSome? {β} (t : PersistentArray α) (f : α → (Option β)) : Option β :=
Id.run (t.findSomeM? f)
@[inline] def findSomeRev? {β} (t : PersistentArray α) (f : α → (Option β)) : Option β :=
Id.run (t.findSomeRevM? f)
@[inline] def foldlFrom {β} (t : PersistentArray α) (f : β → α → β) (b : β) (ini : Nat) : β :=
Id.run (t.foldlFromM f b ini)
def toList (t : PersistentArray α) : List α :=
(t.foldl (fun xs x => x :: xs) []).reverse
section
variables {m : Type → Type w} [Monad m]
@[specialize] partial def anyMAux (p : α → m Bool) : PersistentArrayNode α → m Bool
| node cs => cs.anyM (fun c => anyMAux c)
| leaf vs => vs.anyM p
@[specialize] def anyM (t : PersistentArray α) (p : α → m Bool) : m Bool :=
anyMAux p t.root <||> t.tail.anyM p
@[inline] def allM (a : PersistentArray α) (p : α → m Bool) : m Bool := do
b ← anyM a (fun v => do b ← p v; pure (not b)); pure (not b)
end
@[inline] def any (a : PersistentArray α) (p : α → Bool) : Bool :=
Id.run $ anyM a p
@[inline] def all (a : PersistentArray α) (p : α → Bool) : Bool :=
!any a (fun v => !p v)
section
variables {m : Type u → Type v} [Monad m]
variable {β : Type u}
@[specialize] partial def mapMAux (f : α → m β) : PersistentArrayNode α → m (PersistentArrayNode β)
| node cs => node <$> cs.mapM (fun c => mapMAux c)
| leaf vs => leaf <$> vs.mapM f
@[specialize] def mapM (f : α → m β) (t : PersistentArray α) : m (PersistentArray β) := do
root ← mapMAux f t.root;
tail ← t.tail.mapM f;
pure { t with tail := tail, root := root }
end
@[inline] def map {β} (f : α → β) (t : PersistentArray α) : PersistentArray β :=
Id.run (t.mapM f)
structure Stats :=
(numNodes : Nat) (depth : Nat) (tailSize : Nat)
partial def collectStats : PersistentArrayNode α → Stats → Nat → Stats
| node cs, s, d =>
cs.foldl (fun s c => collectStats c s (d+1))
{ s with numNodes := s.numNodes + 1,
depth := Nat.max d s.depth }
| leaf vs, s, d => { s with numNodes := s.numNodes + 1, depth := Nat.max d s.depth }
def stats (r : PersistentArray α) : Stats :=
collectStats r.root { numNodes := 0, depth := 0, tailSize := r.tail.size } 0
def Stats.toString (s : Stats) : String :=
"{nodes := " ++ toString s.numNodes ++ ", depth := " ++ toString s.depth ++ ", tail size := " ++ toString s.tailSize ++ "}"
instance : HasToString Stats := ⟨Stats.toString⟩
end PersistentArray
def List.toPersistentArrayAux {α : Type u} : List α → PersistentArray α → PersistentArray α
| [], t => t
| x::xs, t => List.toPersistentArrayAux xs (t.push x)
def List.toPersistentArray {α : Type u} (xs : List α) : PersistentArray α :=
xs.toPersistentArrayAux {}
def Array.toPersistentArray {α : Type u} (xs : Array α) : PersistentArray α :=
xs.foldl (fun p x => p.push x) PersistentArray.empty
@[inline] def Array.toPArray {α : Type u} (xs : Array α) : PersistentArray α :=
xs.toPersistentArray
def mkPersistentArray {α : Type u} (n : Nat) (v : α) : PArray α :=
n.fold (fun i p => p.push v) PersistentArray.empty
@[inline] def mkPArray {α : Type u} (n : Nat) (v : α) : PArray α :=
mkPersistentArray n v
|
41e0e01353b02a6da1f0de956abd683f1a5e6a73 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/zmod/basic.lean | 39bc87ae5a45baba59e4c2e7c73484382f864929 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,656 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Chris Hughes
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.int.modeq
import Mathlib.algebra.char_p.basic
import Mathlib.data.nat.totient
import Mathlib.ring_theory.ideal.operations
import Mathlib.PostPort
universes u_1
namespace Mathlib
/-!
# Integers mod `n`
Definition of the integers mod n, and the field structure on the integers mod p.
## Definitions
* `zmod n`, which is for integers modulo a nat `n : ℕ`
* `val a` is defined as a natural number:
- for `a : zmod 0` it is the absolute value of `a`
- for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class
* `val_min_abs` returns the integer closest to zero in the equivalence class.
* A coercion `cast` is defined from `zmod n` into any ring.
This is a ring hom if the ring has characteristic dividing `n`
-/
namespace fin
/-!
## Ring structure on `fin n`
We define a commutative ring structure on `fin n`, but we do not register it as instance.
Afterwords, when we define `zmod n` in terms of `fin n`, we use these definitions
to register the ring structure on `zmod n` as type class instance.
-/
/-- Negation on `fin n` -/
def has_neg (n : ℕ) : Neg (fin n) :=
{ neg := fun (a : fin n) => { val := int.nat_mod (-↑(subtype.val a)) ↑n, property := sorry } }
/-- Multiplicative commutative semigroup structure on `fin (n+1)`. -/
def comm_semigroup (n : ℕ) : comm_semigroup (fin (n + 1)) :=
comm_semigroup.mk Mul.mul sorry sorry
/-- Commutative ring structure on `fin (n+1)`. -/
def comm_ring (n : ℕ) : comm_ring (fin (n + 1)) :=
comm_ring.mk add_comm_monoid.add sorry add_comm_monoid.zero sorry sorry Neg.neg
(ring.sub._default add_comm_monoid.add sorry add_comm_monoid.zero sorry sorry Neg.neg) sorry sorry comm_semigroup.mul
sorry 1 fin.one_mul fin.mul_one (left_distrib_aux n) sorry sorry
end fin
/-- The integers modulo `n : ℕ`. -/
def zmod : ℕ → Type :=
sorry
namespace zmod
protected instance fintype (n : ℕ) [fact (0 < n)] : fintype (zmod n) :=
sorry
theorem card (n : ℕ) [fact (0 < n)] : fintype.card (zmod n) = n :=
nat.cases_on n (fun [_inst_1 : fact (0 < 0)] => False._oldrec (nat.not_lt_zero 0 _inst_1))
(fun (n : ℕ) => fintype.card_fin (n + 1)) _inst_1
protected instance decidable_eq (n : ℕ) : DecidableEq (zmod n) :=
sorry
protected instance has_repr (n : ℕ) : has_repr (zmod n) :=
sorry
protected instance comm_ring (n : ℕ) : comm_ring (zmod n) :=
sorry
protected instance inhabited (n : ℕ) : Inhabited (zmod n) :=
{ default := 0 }
/-- `val a` is a natural number defined as:
- for `a : zmod 0` it is the absolute value of `a`
- for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class
See `zmod.val_min_abs` for a variant that takes values in the integers.
-/
def val {n : ℕ} : zmod n → ℕ :=
sorry
theorem val_lt {n : ℕ} [fact (0 < n)] (a : zmod n) : val a < n :=
nat.cases_on n (fun [_inst_1 : fact (0 < 0)] (a : zmod 0) => False._oldrec (nat.not_lt_zero 0 _inst_1))
(fun (n : ℕ) (a : zmod (Nat.succ n)) => fin.is_lt a) _inst_1 a
@[simp] theorem val_zero {n : ℕ} : val 0 = 0 :=
nat.cases_on n (idRhs (val 0 = val 0) rfl) fun (n : ℕ) => idRhs (val 0 = val 0) rfl
theorem val_cast_nat {n : ℕ} (a : ℕ) : val ↑a = a % n := sorry
protected instance char_p (n : ℕ) : char_p (zmod n) n := sorry
@[simp] theorem cast_self (n : ℕ) : ↑n = 0 :=
char_p.cast_eq_zero (zmod n) n
@[simp] theorem cast_self' (n : ℕ) : ↑n + 1 = 0 :=
eq.mpr (id (Eq._oldrec (Eq.refl (↑n + 1 = 0)) (Eq.symm (nat.cast_add_one n))))
(eq.mpr (id (Eq._oldrec (Eq.refl (↑(n + 1) = 0)) (cast_self (n + 1)))) (Eq.refl 0))
/-- Cast an integer modulo `n` to another semiring.
This function is a morphism if the characteristic of `R` divides `n`.
See `zmod.cast_hom` for a bundled version. -/
def cast {R : Type u_1} [HasZero R] [HasOne R] [Add R] [Neg R] {n : ℕ} : zmod n → R :=
sorry
-- see Note [coercion into rings]
protected instance has_coe_t {R : Type u_1} [HasZero R] [HasOne R] [Add R] [Neg R] (n : ℕ) : has_coe_t (zmod n) R :=
has_coe_t.mk cast
@[simp] theorem cast_zero {n : ℕ} {R : Type u_1} [HasZero R] [HasOne R] [Add R] [Neg R] : ↑0 = 0 :=
nat.cases_on n (Eq.refl ↑0) fun (n : ℕ) => Eq.refl ↑0
theorem nat_cast_surjective {n : ℕ} [fact (0 < n)] : function.surjective coe := sorry
theorem int_cast_surjective {n : ℕ} : function.surjective coe := sorry
theorem cast_val {n : ℕ} [fact (0 < n)] (a : zmod n) : ↑(val a) = a := sorry
@[simp] theorem cast_id (n : ℕ) (i : zmod n) : ↑i = i :=
nat.cases_on n (fun (i : zmod 0) => idRhs (↑i = i) (int.cast_id i))
(fun (n : ℕ) (i : zmod (Nat.succ n)) => idRhs (↑(val i) = i) (cast_val i)) i
@[simp] theorem nat_cast_val {n : ℕ} {R : Type u_1} [ring R] [fact (0 < n)] (i : zmod n) : ↑(val i) = ↑i :=
nat.cases_on n (fun [_inst_2 : fact (0 < 0)] (i : zmod 0) => False._oldrec (nat.not_lt_zero 0 _inst_2))
(fun (n : ℕ) (i : zmod (Nat.succ n)) => Eq.refl ↑(val i)) _inst_2 i
/-! If the characteristic of `R` divides `n`, then `cast` is a homomorphism. -/
@[simp] theorem cast_one {n : ℕ} {R : Type u_1} [ring R] {m : ℕ} [char_p R m] (h : m ∣ n) : ↑1 = 1 := sorry
theorem cast_add {n : ℕ} {R : Type u_1} [ring R] {m : ℕ} [char_p R m] (h : m ∣ n) (a : zmod n) (b : zmod n) : ↑(a + b) = ↑a + ↑b := sorry
theorem cast_mul {n : ℕ} {R : Type u_1} [ring R] {m : ℕ} [char_p R m] (h : m ∣ n) (a : zmod n) (b : zmod n) : ↑(a * b) = ↑a * ↑b := sorry
/-- The canonical ring homomorphism from `zmod n` to a ring of characteristic `n`. -/
def cast_hom {n : ℕ} {m : ℕ} (h : m ∣ n) (R : Type u_1) [ring R] [char_p R m] : zmod n →+* R :=
ring_hom.mk coe (cast_one h) (cast_mul h) sorry (cast_add h)
@[simp] theorem cast_hom_apply {n : ℕ} {R : Type u_1} [ring R] {m : ℕ} [char_p R m] {h : m ∣ n} (i : zmod n) : coe_fn (cast_hom h R) i = ↑i :=
rfl
@[simp] theorem cast_sub {n : ℕ} {R : Type u_1} [ring R] {m : ℕ} [char_p R m] (h : m ∣ n) (a : zmod n) (b : zmod n) : ↑(a - b) = ↑a - ↑b :=
ring_hom.map_sub (cast_hom h R) a b
@[simp] theorem cast_neg {n : ℕ} {R : Type u_1} [ring R] {m : ℕ} [char_p R m] (h : m ∣ n) (a : zmod n) : ↑(-a) = -↑a :=
ring_hom.map_neg (cast_hom h R) a
@[simp] theorem cast_pow {n : ℕ} {R : Type u_1} [ring R] {m : ℕ} [char_p R m] (h : m ∣ n) (a : zmod n) (k : ℕ) : ↑(a ^ k) = ↑a ^ k :=
ring_hom.map_pow (cast_hom h R) a k
@[simp] theorem cast_nat_cast {n : ℕ} {R : Type u_1} [ring R] {m : ℕ} [char_p R m] (h : m ∣ n) (k : ℕ) : ↑↑k = ↑k :=
ring_hom.map_nat_cast (cast_hom h R) k
@[simp] theorem cast_int_cast {n : ℕ} {R : Type u_1} [ring R] {m : ℕ} [char_p R m] (h : m ∣ n) (k : ℤ) : ↑↑k = ↑k :=
ring_hom.map_int_cast (cast_hom h R) k
/-! Some specialised simp lemmas which apply when `R` has characteristic `n`. -/
@[simp] theorem cast_one' {n : ℕ} {R : Type u_1} [ring R] [char_p R n] : ↑1 = 1 :=
cast_one (dvd_refl n)
@[simp] theorem cast_add' {n : ℕ} {R : Type u_1} [ring R] [char_p R n] (a : zmod n) (b : zmod n) : ↑(a + b) = ↑a + ↑b :=
cast_add (dvd_refl n) a b
@[simp] theorem cast_mul' {n : ℕ} {R : Type u_1} [ring R] [char_p R n] (a : zmod n) (b : zmod n) : ↑(a * b) = ↑a * ↑b :=
cast_mul (dvd_refl n) a b
@[simp] theorem cast_sub' {n : ℕ} {R : Type u_1} [ring R] [char_p R n] (a : zmod n) (b : zmod n) : ↑(a - b) = ↑a - ↑b :=
cast_sub (dvd_refl n) a b
@[simp] theorem cast_pow' {n : ℕ} {R : Type u_1} [ring R] [char_p R n] (a : zmod n) (k : ℕ) : ↑(a ^ k) = ↑a ^ k :=
cast_pow (dvd_refl n) a k
@[simp] theorem cast_nat_cast' {n : ℕ} {R : Type u_1} [ring R] [char_p R n] (k : ℕ) : ↑↑k = ↑k :=
cast_nat_cast (dvd_refl n) k
@[simp] theorem cast_int_cast' {n : ℕ} {R : Type u_1} [ring R] [char_p R n] (k : ℤ) : ↑↑k = ↑k :=
cast_int_cast (dvd_refl n) k
protected instance algebra {n : ℕ} (R : Type u_1) [comm_ring R] [char_p R n] : algebra (zmod n) R :=
ring_hom.to_algebra (cast_hom (dvd_refl n) R)
theorem cast_hom_injective {n : ℕ} (R : Type u_1) [ring R] [char_p R n] : function.injective ⇑(cast_hom (dvd_refl n) R) := sorry
theorem cast_hom_bijective {n : ℕ} (R : Type u_1) [ring R] [char_p R n] [fintype R] (h : fintype.card R = n) : function.bijective ⇑(cast_hom (dvd_refl n) R) := sorry
/-- The unique ring isomorphism between `zmod n` and a ring `R`
of characteristic `n` and cardinality `n`. -/
def ring_equiv {n : ℕ} (R : Type u_1) [ring R] [char_p R n] [fintype R] (h : fintype.card R = n) : zmod n ≃+* R :=
ring_equiv.of_bijective (cast_hom (dvd_refl n) R) (cast_hom_bijective R h)
theorem int_coe_eq_int_coe_iff (a : ℤ) (b : ℤ) (c : ℕ) : ↑a = ↑b ↔ int.modeq (↑c) a b :=
char_p.int_coe_eq_int_coe_iff (zmod c) c a b
theorem nat_coe_eq_nat_coe_iff (a : ℕ) (b : ℕ) (c : ℕ) : ↑a = ↑b ↔ nat.modeq c a b := sorry
theorem int_coe_zmod_eq_zero_iff_dvd (a : ℤ) (b : ℕ) : ↑a = 0 ↔ ↑b ∣ a := sorry
theorem nat_coe_zmod_eq_zero_iff_dvd (a : ℕ) (b : ℕ) : ↑a = 0 ↔ b ∣ a := sorry
@[simp] theorem cast_mod_int (a : ℤ) (b : ℕ) : ↑(a % ↑b) = ↑a :=
eq.mpr (id (Eq._oldrec (Eq.refl (↑(a % ↑b) = ↑a)) (propext (int_coe_eq_int_coe_iff (a % ↑b) a b))))
(int.modeq.mod_modeq a ↑b)
@[simp] theorem coe_to_nat (p : ℕ) {z : ℤ} (h : 0 ≤ z) : ↑(int.to_nat z) = ↑z := sorry
theorem val_injective (n : ℕ) [fact (0 < n)] : function.injective val :=
nat.cases_on n (fun [_inst_1 : fact (0 < 0)] => id fun (a₁ : zmod 0) => False._oldrec (nat.not_lt_zero 0 _inst_1))
(fun (n : ℕ) => id fun (a b : zmod (Nat.succ n)) (h : val a = val b) => fin.ext h) _inst_1
theorem val_one_eq_one_mod (n : ℕ) : val 1 = 1 % n :=
eq.mpr (id (Eq._oldrec (Eq.refl (val 1 = 1 % n)) (Eq.symm nat.cast_one)))
(eq.mpr (id (Eq._oldrec (Eq.refl (val ↑1 = 1 % n)) (val_cast_nat 1))) (Eq.refl (1 % n)))
theorem val_one (n : ℕ) [fact (1 < n)] : val 1 = 1 :=
eq.mpr (id (Eq._oldrec (Eq.refl (val 1 = 1)) (val_one_eq_one_mod n))) (nat.mod_eq_of_lt _inst_1)
theorem val_add {n : ℕ} [fact (0 < n)] (a : zmod n) (b : zmod n) : val (a + b) = (val a + val b) % n :=
nat.cases_on n (fun [_inst_1 : fact (0 < 0)] (a b : zmod 0) => False._oldrec (nat.not_lt_zero 0 _inst_1))
(fun (n : ℕ) (a b : zmod (Nat.succ n)) => fin.val_add a b) _inst_1 a b
theorem val_mul {n : ℕ} (a : zmod n) (b : zmod n) : val (a * b) = val a * val b % n := sorry
protected instance nontrivial (n : ℕ) [fact (1 < n)] : nontrivial (zmod n) :=
nontrivial.mk
(Exists.intro 0
(Exists.intro 1
fun (h : 0 = 1) =>
zero_ne_one
(Eq.trans (Eq.trans (eq.mpr (id (Eq._oldrec (Eq.refl (0 = val 0)) val_zero)) (Eq.refl 0)) (congr_arg val h))
(val_one n))))
/-- The inversion on `zmod n`.
It is setup in such a way that `a * a⁻¹` is equal to `gcd a.val n`.
In particular, if `a` is coprime to `n`, and hence a unit, `a * a⁻¹ = 1`. -/
def inv (n : ℕ) : zmod n → zmod n :=
sorry
protected instance has_inv (n : ℕ) : has_inv (zmod n) :=
has_inv.mk (inv n)
theorem inv_zero (n : ℕ) : 0⁻¹ = 0 := sorry
theorem mul_inv_eq_gcd {n : ℕ} (a : zmod n) : a * (a⁻¹) = ↑(nat.gcd (val a) n) := sorry
@[simp] theorem cast_mod_nat (n : ℕ) (a : ℕ) : ↑(a % n) = ↑a := sorry
theorem eq_iff_modeq_nat (n : ℕ) {a : ℕ} {b : ℕ} : ↑a = ↑b ↔ nat.modeq n a b := sorry
theorem coe_mul_inv_eq_one {n : ℕ} (x : ℕ) (h : nat.coprime x n) : ↑x * (↑x⁻¹) = 1 := sorry
/-- `unit_of_coprime` makes an element of `units (zmod n)` given
a natural number `x` and a proof that `x` is coprime to `n` -/
def unit_of_coprime {n : ℕ} (x : ℕ) (h : nat.coprime x n) : units (zmod n) :=
units.mk (↑x) (↑x⁻¹) (coe_mul_inv_eq_one x h) sorry
@[simp] theorem cast_unit_of_coprime {n : ℕ} (x : ℕ) (h : nat.coprime x n) : ↑(unit_of_coprime x h) = ↑x :=
rfl
theorem val_coe_unit_coprime {n : ℕ} (u : units (zmod n)) : nat.coprime (val ↑u) n := sorry
@[simp] theorem inv_coe_unit {n : ℕ} (u : units (zmod n)) : ↑u⁻¹ = ↑(u⁻¹) := sorry
theorem mul_inv_of_unit {n : ℕ} (a : zmod n) (h : is_unit a) : a * (a⁻¹) = 1 := sorry
theorem inv_mul_of_unit {n : ℕ} (a : zmod n) (h : is_unit a) : a⁻¹ * a = 1 :=
eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ * a = 1)) (mul_comm (a⁻¹) a)))
(eq.mpr (id (Eq._oldrec (Eq.refl (a * (a⁻¹) = 1)) (mul_inv_of_unit a h))) (Eq.refl 1))
/-- Equivalence between the units of `zmod n` and
the subtype of terms `x : zmod n` for which `x.val` is comprime to `n` -/
def units_equiv_coprime {n : ℕ} [fact (0 < n)] : units (zmod n) ≃ Subtype fun (x : zmod n) => nat.coprime (val x) n :=
equiv.mk (fun (x : units (zmod n)) => { val := ↑x, property := val_coe_unit_coprime x })
(fun (x : Subtype fun (x : zmod n) => nat.coprime (val x) n) => unit_of_coprime (val (subtype.val x)) sorry) sorry
sorry
@[simp] theorem card_units_eq_totient (n : ℕ) [fact (0 < n)] : fintype.card (units (zmod n)) = nat.totient n := sorry
protected instance subsingleton_units : subsingleton (units (zmod (bit0 1))) :=
subsingleton.intro
fun (x y : units (zmod (bit0 1))) =>
units.cases_on x
fun (x xi : zmod (bit0 1)) (x_val_inv : x * xi = 1) (x_inv_val : xi * x = 1) =>
units.cases_on y
fun (y yi : zmod (bit0 1)) (y_val_inv : y * yi = 1) (y_inv_val : yi * y = 1) =>
of_as_true trivial x y xi yi x_val_inv x_inv_val y_val_inv y_inv_val
theorem le_div_two_iff_lt_neg (n : ℕ) [hn : fact (n % bit0 1 = 1)] {x : zmod n} (hx0 : x ≠ 0) : val x ≤ n / bit0 1 ↔ n / bit0 1 < val (-x) := sorry
theorem ne_neg_self (n : ℕ) [hn : fact (n % bit0 1 = 1)] {a : zmod n} (ha : a ≠ 0) : a ≠ -a := sorry
theorem neg_one_ne_one {n : ℕ} [fact (bit0 1 < n)] : -1 ≠ 1 :=
char_p.neg_one_ne_one (zmod n) n
@[simp] theorem neg_eq_self_mod_two (a : zmod (bit0 1)) : -a = a :=
of_as_true trivial
@[simp] theorem nat_abs_mod_two (a : ℤ) : ↑(int.nat_abs a) = ↑a := sorry
@[simp] theorem val_eq_zero {n : ℕ} (a : zmod n) : val a = 0 ↔ a = 0 := sorry
theorem val_cast_of_lt {n : ℕ} {a : ℕ} (h : a < n) : val ↑a = a :=
eq.mpr (id (Eq._oldrec (Eq.refl (val ↑a = a)) (val_cast_nat a)))
(eq.mpr (id (Eq._oldrec (Eq.refl (a % n = a)) (nat.mod_eq_of_lt h))) (Eq.refl a))
theorem neg_val' {n : ℕ} [fact (0 < n)] (a : zmod n) : val (-a) = (n - val a) % n := sorry
theorem neg_val {n : ℕ} [fact (0 < n)] (a : zmod n) : val (-a) = ite (a = 0) 0 (n - val a) := sorry
/-- `val_min_abs x` returns the integer in the same equivalence class as `x` that is closest to `0`,
The result will be in the interval `(-n/2, n/2]`. -/
def val_min_abs {n : ℕ} : zmod n → ℤ :=
sorry
@[simp] theorem val_min_abs_def_zero (x : zmod 0) : val_min_abs x = x :=
rfl
theorem val_min_abs_def_pos {n : ℕ} [fact (0 < n)] (x : zmod n) : val_min_abs x = ite (val x ≤ n / bit0 1) (↑(val x)) (↑(val x) - ↑n) :=
nat.cases_on n (fun [_inst_1 : fact (0 < 0)] (x : zmod 0) => False._oldrec (nat.not_lt_zero 0 _inst_1))
(fun (n : ℕ) (x : zmod (Nat.succ n)) => Eq.refl (val_min_abs x)) _inst_1 x
@[simp] theorem coe_val_min_abs {n : ℕ} (x : zmod n) : ↑(val_min_abs x) = x := sorry
theorem nat_abs_val_min_abs_le {n : ℕ} [fact (0 < n)] (x : zmod n) : int.nat_abs (val_min_abs x) ≤ n / bit0 1 := sorry
@[simp] theorem val_min_abs_zero (n : ℕ) : val_min_abs 0 = 0 := sorry
@[simp] theorem val_min_abs_eq_zero {n : ℕ} (x : zmod n) : val_min_abs x = 0 ↔ x = 0 := sorry
theorem cast_nat_abs_val_min_abs {n : ℕ} [fact (0 < n)] (a : zmod n) : ↑(int.nat_abs (val_min_abs a)) = ite (val a ≤ n / bit0 1) a (-a) := sorry
@[simp] theorem nat_abs_val_min_abs_neg {n : ℕ} (a : zmod n) : int.nat_abs (val_min_abs (-a)) = int.nat_abs (val_min_abs a) := sorry
theorem val_eq_ite_val_min_abs {n : ℕ} [fact (0 < n)] (a : zmod n) : ↑(val a) = val_min_abs a + ite (val a ≤ n / bit0 1) 0 ↑n := sorry
theorem prime_ne_zero (p : ℕ) (q : ℕ) [hp : fact (nat.prime p)] [hq : fact (nat.prime q)] (hpq : p ≠ q) : ↑q ≠ 0 := sorry
end zmod
namespace zmod
/-- Field structure on `zmod p` if `p` is prime. -/
protected instance field (p : ℕ) [fact (nat.prime p)] : field (zmod p) :=
field.mk comm_ring.add sorry comm_ring.zero sorry sorry comm_ring.neg comm_ring.sub sorry sorry comm_ring.mul sorry
comm_ring.one sorry sorry sorry sorry sorry has_inv.inv sorry (mul_inv_cancel_aux p) (inv_zero p)
end zmod
theorem ring_hom.ext_zmod {n : ℕ} {R : Type u_1} [semiring R] (f : zmod n →+* R) (g : zmod n →+* R) : f = g := sorry
namespace zmod
protected instance subsingleton_ring_hom {n : ℕ} {R : Type u_1} [semiring R] : subsingleton (zmod n →+* R) :=
subsingleton.intro ring_hom.ext_zmod
protected instance subsingleton_ring_equiv {n : ℕ} {R : Type u_1} [semiring R] : subsingleton (zmod n ≃+* R) :=
subsingleton.intro
fun (f g : zmod n ≃+* R) =>
eq.mpr (id (Eq._oldrec (Eq.refl (f = g)) (propext (ring_equiv.coe_ring_hom_inj_iff f g)))) (ring_hom.ext_zmod ↑f ↑g)
theorem ring_hom_surjective {n : ℕ} {R : Type u_1} [ring R] (f : R →+* zmod n) : function.surjective ⇑f := sorry
theorem ring_hom_eq_of_ker_eq {n : ℕ} {R : Type u_1} [comm_ring R] (f : R →+* zmod n) (g : R →+* zmod n) (h : ring_hom.ker f = ring_hom.ker g) : f = g := sorry
|
2caf95b7bebbaa540f35738e62a04c085f2a60dc | 4727251e0cd73359b15b664c3170e5d754078599 | /src/ring_theory/prime.lean | 9d37527d15d08c9b3b893764b93f1e4cb8e2d992 | [
"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,187 | lean | /-
Copyright (c) 2020 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import algebra.associated
import algebra.big_operators.basic
/-!
# Prime elements in rings
This file contains lemmas about prime elements of commutative rings.
-/
section cancel_comm_monoid_with_zero
variables {R : Type*} [cancel_comm_monoid_with_zero R]
open finset
open_locale big_operators
/-- If `x * y = a * ∏ i in s, p i` where `p i` is always prime, then
`x` and `y` can both be written as a divisor of `a` multiplied by
a product over a subset of `s` -/
lemma mul_eq_mul_prime_prod {α : Type*} [decidable_eq α] {x y a : R} {s : finset α}
{p : α → R} (hp : ∀ i ∈ s, prime (p i)) (hx : x * y = a * ∏ i in s, p i) :
∃ (t u : finset α) (b c : R),
t ∪ u = s
∧ disjoint t u
∧ a = b * c
∧ x = b * ∏ i in t, p i
∧ y = c * ∏ i in u, p i :=
begin
induction s using finset.induction with i s his ih generalizing x y a,
{ exact ⟨∅, ∅, x, y, by simp [hx]⟩ },
{ rw [prod_insert his, ← mul_assoc] at hx,
have hpi : prime (p i), { exact hp i (mem_insert_self _ _) },
rcases ih (λ i hi, hp i (mem_insert_of_mem hi)) hx with
⟨t, u, b, c, htus, htu, hbc, rfl, rfl⟩,
have hit : i ∉ t, from λ hit, his (htus ▸ mem_union_left _ hit),
have hiu : i ∉ u, from λ hiu, his (htus ▸ mem_union_right _ hiu),
obtain ⟨d, rfl⟩ | ⟨d, rfl⟩ : p i ∣ b ∨ p i ∣ c,
from hpi.dvd_or_dvd ⟨a, by rw [← hbc, mul_comm]⟩,
{ rw [mul_assoc, mul_comm a, mul_right_inj' hpi.ne_zero] at hbc,
exact ⟨insert i t, u, d, c, by rw [insert_union, htus],
disjoint_insert_left.2 ⟨hiu, htu⟩,
by simp [hbc, prod_insert hit, mul_assoc, mul_comm, mul_left_comm]⟩ },
{ rw [← mul_assoc, mul_right_comm b, mul_left_inj' hpi.ne_zero] at hbc,
exact ⟨t, insert i u, b, d, by rw [union_insert, htus],
disjoint_insert_right.2 ⟨hit, htu⟩,
by simp [← hbc, prod_insert hiu, mul_assoc, mul_comm, mul_left_comm]⟩ } }
end
/-- If ` x * y = a * p ^ n` where `p` is prime, then `x` and `y` can both be written
as the product of a power of `p` and a divisor of `a`. -/
lemma mul_eq_mul_prime_pow {x y a p : R} {n : ℕ} (hp : prime p) (hx : x * y = a * p ^ n) :
∃ (i j : ℕ) (b c : R),
i + j = n
∧ a = b * c
∧ x = b * p ^ i
∧ y = c * p ^ j :=
begin
rcases mul_eq_mul_prime_prod (λ _ _, hp)
(show x * y = a * (range n).prod (λ _, p), by simpa) with
⟨t, u, b, c, htus, htu, rfl, rfl, rfl⟩,
exact ⟨t.card, u.card, b, c, by rw [← card_disjoint_union htu, htus, card_range], by simp⟩,
end
end cancel_comm_monoid_with_zero
section comm_ring
variables {α : Type*} [comm_ring α]
lemma prime.neg {p : α} (hp : prime p) : prime (-p) :=
begin
obtain ⟨h1, h2, h3⟩ := hp,
exact ⟨neg_ne_zero.mpr h1, by rwa is_unit.neg_iff, by simpa [neg_dvd] using h3⟩
end
lemma prime.abs [linear_order α] {p : α} (hp : prime p) : prime (abs p) :=
begin
obtain h|h := abs_choice p; rw h,
{ exact hp },
{ exact hp.neg }
end
end comm_ring
|
3290e21a4889956cb24bdf7facbf4fcdc7488f86 | b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77 | /src/topology/algebra/monoid.lean | 3c0ab0a647dd1735d9a491da005b971815762d4e | [
"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 | 8,342 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import topology.continuous_on
import group_theory.submonoid.basic
import algebra.group.prod
import algebra.pointwise
/-!
# Theory of topological monoids
In this file we define mixin classes `has_continuous_mul` and `has_continuous_add`. While in many
applications the underlying type is a monoid (multiplicative or additive), we do not require this in
the definitions.
-/
open classical set filter topological_space
open_locale classical topological_space big_operators
variables {α β M N : Type*}
/-- Basic hypothesis to talk about a topological additive monoid or a topological additive
semigroup. A topological additive monoid over `α`, for example, is obtained by requiring both the
instances `add_monoid α` and `has_continuous_add α`. -/
class has_continuous_add (M : Type*) [topological_space M] [has_add M] : Prop :=
(continuous_add : continuous (λ p : M × M, p.1 + p.2))
/-- Basic hypothesis to talk about a topological monoid or a topological semigroup.
A topological monoid over `α`, for example, is obtained by requiring both the instances `monoid α`
and `has_continuous_mul α`. -/
@[to_additive]
class has_continuous_mul (M : Type*) [topological_space M] [has_mul M] : Prop :=
(continuous_mul : continuous (λ p : M × M, p.1 * p.2))
section has_continuous_mul
variables [topological_space M] [has_mul M] [has_continuous_mul M]
@[to_additive]
lemma continuous_mul : continuous (λp:M×M, p.1 * p.2) :=
has_continuous_mul.continuous_mul
@[to_additive, continuity]
lemma continuous.mul [topological_space α] {f : α → M} {g : α → M}
(hf : continuous f) (hg : continuous g) :
continuous (λx, f x * g x) :=
continuous_mul.comp (hf.prod_mk hg : _)
attribute [continuity] continuous.add
@[to_additive]
lemma continuous_mul_left (a : M) : continuous (λ b:M, a * b) :=
continuous_const.mul continuous_id
@[to_additive]
lemma continuous_mul_right (a : M) : continuous (λ b:M, b * a) :=
continuous_id.mul continuous_const
@[to_additive]
lemma continuous_on.mul [topological_space α] {f : α → M} {g : α → M} {s : set α}
(hf : continuous_on f s) (hg : continuous_on g s) :
continuous_on (λx, f x * g x) s :=
(continuous_mul.comp_continuous_on (hf.prod hg) : _)
@[to_additive]
lemma tendsto_mul {a b : M} : tendsto (λp:M×M, p.fst * p.snd) (𝓝 (a, b)) (𝓝 (a * b)) :=
continuous_iff_continuous_at.mp has_continuous_mul.continuous_mul (a, b)
@[to_additive]
lemma filter.tendsto.mul {f : α → M} {g : α → M} {x : filter α} {a b : M}
(hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) :
tendsto (λx, f x * g x) x (𝓝 (a * b)) :=
tendsto_mul.comp (hf.prod_mk_nhds hg)
@[to_additive]
lemma tendsto.const_mul (b : M) {c : M} {f : α → M} {l : filter α}
(h : tendsto (λ (k:α), f k) l (𝓝 c)) : tendsto (λ (k:α), b * f k) l (𝓝 (b * c)) :=
tendsto_const_nhds.mul h
@[to_additive]
lemma tendsto.mul_const (b : M) {c : M} {f : α → M} {l : filter α}
(h : tendsto (λ (k:α), f k) l (𝓝 c)) : tendsto (λ (k:α), f k * b) l (𝓝 (c * b)) :=
h.mul tendsto_const_nhds
@[to_additive]
lemma continuous_at.mul [topological_space α] {f : α → M} {g : α → M} {x : α}
(hf : continuous_at f x) (hg : continuous_at g x) :
continuous_at (λx, f x * g x) x :=
hf.mul hg
@[to_additive]
lemma continuous_within_at.mul [topological_space α] {f : α → M} {g : α → M} {s : set α} {x : α}
(hf : continuous_within_at f s x) (hg : continuous_within_at g s x) :
continuous_within_at (λx, f x * g x) s x :=
hf.mul hg
@[to_additive]
instance [topological_space N] [has_mul N] [has_continuous_mul N] : has_continuous_mul (M × N) :=
⟨((continuous_fst.comp continuous_fst).mul (continuous_fst.comp continuous_snd)).prod_mk
((continuous_snd.comp continuous_fst).mul (continuous_snd.comp continuous_snd))⟩
end has_continuous_mul
section has_continuous_mul
variables [topological_space M] [monoid M] [has_continuous_mul M]
@[to_additive exists_open_nhds_zero_half]
lemma exists_open_nhds_one_split {s : set M} (hs : s ∈ 𝓝 (1 : M)) :
∃ V : set M, is_open V ∧ (1 : M) ∈ V ∧ ∀ (v ∈ V) (w ∈ V), v * w ∈ s :=
have ((λa:M×M, a.1 * a.2) ⁻¹' s) ∈ 𝓝 ((1, 1) : M × M),
from tendsto_mul (by simpa only [one_mul] using hs),
by simpa only [prod_subset_iff] using exists_nhds_square this
@[to_additive exists_nhds_zero_half]
lemma exists_nhds_one_split {s : set M} (hs : s ∈ 𝓝 (1 : M)) :
∃ V ∈ 𝓝 (1 : M), ∀ (v ∈ V) (w ∈ V), v * w ∈ s :=
let ⟨V, Vo, V1, hV⟩ := exists_open_nhds_one_split hs
in ⟨V, mem_nhds_sets Vo V1, hV⟩
@[to_additive exists_nhds_zero_quarter]
lemma exists_nhds_one_split4 {u : set M} (hu : u ∈ 𝓝 (1 : M)) :
∃ V ∈ 𝓝 (1 : M),
∀ {v w s t}, v ∈ V → w ∈ V → s ∈ V → t ∈ V → v * w * s * t ∈ u :=
begin
rcases exists_nhds_one_split hu with ⟨W, W1, h⟩,
rcases exists_nhds_one_split W1 with ⟨V, V1, h'⟩,
use [V, V1],
intros v w s t v_in w_in s_in t_in,
simpa only [mul_assoc] using h _ (h' v v_in w w_in) _ (h' s s_in t t_in)
end
/-- Given a neighborhood `U` of `1` there is an open neighborhood `V` of `1`
such that `VV ⊆ U`. -/
@[to_additive "Given a open neighborhood `U` of `0` there is a open neighborhood `V` of `0`
such that `V + V ⊆ U`."]
lemma exists_open_nhds_one_mul_subset {U : set M} (hU : U ∈ 𝓝 (1 : M)) :
∃ V : set M, is_open V ∧ (1 : M) ∈ V ∧ V * V ⊆ U :=
begin
rcases exists_open_nhds_one_split hU with ⟨V, Vo, V1, hV⟩,
use [V, Vo, V1],
rintros _ ⟨x, y, hx, hy, rfl⟩,
exact hV _ hx _ hy
end
@[to_additive]
lemma tendsto_list_prod {f : β → α → M} {x : filter α} {a : β → M} :
∀l:list β, (∀c∈l, tendsto (f c) x (𝓝 (a c))) →
tendsto (λb, (l.map (λc, f c b)).prod) x (𝓝 ((l.map a).prod))
| [] _ := by simp [tendsto_const_nhds]
| (f :: l) h :=
begin
simp only [list.map_cons, list.prod_cons],
exact (h f (list.mem_cons_self _ _)).mul
(tendsto_list_prod l (assume c hc, h c (list.mem_cons_of_mem _ hc)))
end
@[to_additive]
lemma continuous_list_prod [topological_space α] {f : β → α → M} (l : list β)
(h : ∀c∈l, continuous (f c)) :
continuous (λa, (l.map (λc, f c a)).prod) :=
continuous_iff_continuous_at.2 $ assume x, tendsto_list_prod l $ assume c hc,
continuous_iff_continuous_at.1 (h c hc) x
-- @[to_additive continuous_smul]
@[continuity]
lemma continuous_pow : ∀ n : ℕ, continuous (λ a : M, a ^ n)
| 0 := by simpa using continuous_const
| (k+1) := show continuous (λ (a : M), a * a ^ k), from continuous_id.mul (continuous_pow _)
@[continuity]
lemma continuous.pow {f : α → M} [topological_space α] (h : continuous f) (n : ℕ) :
continuous (λ b, (f b) ^ n) :=
continuous.comp (continuous_pow n) h
end has_continuous_mul
section
variables [topological_space M] [comm_monoid M]
@[to_additive]
lemma submonoid.mem_nhds_one (S : submonoid M) (oS : is_open (S : set M)) :
(S : set M) ∈ 𝓝 (1 : M) :=
mem_nhds_sets oS S.one_mem
variable [has_continuous_mul M]
@[to_additive]
lemma tendsto_multiset_prod {f : β → α → M} {x : filter α} {a : β → M} (s : multiset β) :
(∀c∈s, tendsto (f c) x (𝓝 (a c))) →
tendsto (λb, (s.map (λc, f c b)).prod) x (𝓝 ((s.map a).prod)) :=
by { rcases s with ⟨l⟩, simp, exact tendsto_list_prod l }
@[to_additive]
lemma tendsto_finset_prod {f : β → α → M} {x : filter α} {a : β → M} (s : finset β) :
(∀c∈s, tendsto (f c) x (𝓝 (a c))) → tendsto (λb, ∏ c in s, f c b) x (𝓝 (∏ c in s, a c)) :=
tendsto_multiset_prod _
@[to_additive, continuity]
lemma continuous_multiset_prod [topological_space α] {f : β → α → M} (s : multiset β) :
(∀c∈s, continuous (f c)) → continuous (λa, (s.map (λc, f c a)).prod) :=
by { rcases s with ⟨l⟩, simp, exact continuous_list_prod l }
attribute [continuity] continuous_multiset_sum
@[to_additive, continuity]
lemma continuous_finset_prod [topological_space α] {f : β → α → M} (s : finset β) :
(∀c∈s, continuous (f c)) → continuous (λa, ∏ c in s, f c a) :=
continuous_multiset_prod _
attribute [continuity] continuous_finset_sum
end
|
0be67a99cc2ab0cbee953981d78217a5a3a990c5 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/linear_algebra/linear_pmap.lean | dda74f390baf4ab88f974778720c0808bf5f44b1 | [
"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 | 16,712 | lean | /-
Copyright (c) 2020 Yury Kudryashov All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import linear_algebra.basic
import linear_algebra.prod
/-!
# Partially defined linear maps
A `linear_pmap R E F` is a linear map from a submodule of `E` to `F`. We define
a `semilattice_inf_bot` instance on this this, and define three operations:
* `mk_span_singleton` defines a partial linear map defined on the span of a singleton.
* `sup` takes two partial linear maps `f`, `g` that agree on the intersection of their
domains, and returns the unique partial linear map on `f.domain ⊔ g.domain` that
extends both `f` and `g`.
* `Sup` takes a `directed_on (≤)` set of partial linear maps, and returns the unique
partial linear map on the `Sup` of their domains that extends all these maps.
Partially defined maps are currently used in `mathlib` to prove Hahn-Banach theorem
and its variations. Namely, `linear_pmap.Sup` implies that every chain of `linear_pmap`s
is bounded above.
Another possible use (not yet in `mathlib`) would be the theory of unbounded linear operators.
-/
open set
universes u v w
/-- A `linear_pmap R E F` is a linear map from a submodule of `E` to `F`. -/
structure linear_pmap (R : Type u) [ring R] (E : Type v) [add_comm_group E] [module R E]
(F : Type w) [add_comm_group F] [module R F] :=
(domain : submodule R E)
(to_fun : domain →ₗ[R] F)
variables {R : Type*} [ring R] {E : Type*} [add_comm_group E] [module R E]
{F : Type*} [add_comm_group F] [module R F]
{G : Type*} [add_comm_group G] [module R G]
namespace linear_pmap
open submodule
instance : has_coe_to_fun (linear_pmap R E F) :=
⟨λ f : linear_pmap R E F, f.domain → F, λ f, f.to_fun⟩
@[simp] lemma to_fun_eq_coe (f : linear_pmap R E F) (x : f.domain) :
f.to_fun x = f x := rfl
@[simp] lemma map_zero (f : linear_pmap R E F) : f 0 = 0 := f.to_fun.map_zero
lemma map_add (f : linear_pmap R E F) (x y : f.domain) : f (x + y) = f x + f y :=
f.to_fun.map_add x y
lemma map_neg (f : linear_pmap R E F) (x : f.domain) : f (-x) = -f x :=
f.to_fun.map_neg x
lemma map_sub (f : linear_pmap R E F) (x y : f.domain) : f (x - y) = f x - f y :=
f.to_fun.map_sub x y
lemma map_smul (f : linear_pmap R E F) (c : R) (x : f.domain) : f (c • x) = c • f x :=
f.to_fun.map_smul c x
@[simp] lemma mk_apply (p : submodule R E) (f : p →ₗ[R] F) (x : p) :
mk p f x = f x := rfl
/-- The unique `linear_pmap` on `R ∙ x` that sends `x` to `y`. This version works for modules
over rings, and requires a proof of `∀ c, c • x = 0 → c • y = 0`. -/
noncomputable def mk_span_singleton' (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) :
linear_pmap R E F :=
{ domain := R ∙ x,
to_fun :=
have H : ∀ c₁ c₂ : R, c₁ • x = c₂ • x → c₁ • y = c₂ • y,
{ intros c₁ c₂ h,
rw [← sub_eq_zero, ← sub_smul] at h ⊢,
exact H _ h },
{ to_fun := λ z, (classical.some (mem_span_singleton.1 z.prop) • y),
map_add' := λ y z, begin
rw [← add_smul],
apply H,
simp only [add_smul, sub_smul, classical.some_spec (mem_span_singleton.1 _)],
apply coe_add
end,
map_smul' := λ c z, begin
rw [smul_smul],
apply H,
simp only [mul_smul, classical.some_spec (mem_span_singleton.1 _)],
apply coe_smul
end } }
@[simp] lemma domain_mk_span_singleton (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) :
(mk_span_singleton' x y H).domain = R ∙ x := rfl
@[simp] lemma mk_span_singleton_apply (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0)
(c : R) (h) :
mk_span_singleton' x y H ⟨c • x, h⟩ = c • y :=
begin
dsimp [mk_span_singleton'],
rw [← sub_eq_zero, ← sub_smul],
apply H,
simp only [sub_smul, one_smul, sub_eq_zero],
apply classical.some_spec (mem_span_singleton.1 h),
end
/-- The unique `linear_pmap` on `span R {x}` that sends a non-zero vector `x` to `y`.
This version works for modules over division rings. -/
@[reducible] noncomputable def mk_span_singleton {K E F : Type*} [division_ring K]
[add_comm_group E] [module K E] [add_comm_group F] [module K F] (x : E) (y : F) (hx : x ≠ 0) :
linear_pmap K E F :=
mk_span_singleton' x y $ λ c hc, (smul_eq_zero.1 hc).elim
(λ hc, by rw [hc, zero_smul]) (λ hx', absurd hx' hx)
/-- Projection to the first coordinate as a `linear_pmap` -/
protected def fst (p : submodule R E) (p' : submodule R F) : linear_pmap R (E × F) E :=
{ domain := p.prod p',
to_fun := (linear_map.fst R E F).comp (p.prod p').subtype }
@[simp] lemma fst_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') :
linear_pmap.fst p p' x = (x : E × F).1 := rfl
/-- Projection to the second coordinate as a `linear_pmap` -/
protected def snd (p : submodule R E) (p' : submodule R F) : linear_pmap R (E × F) F :=
{ domain := p.prod p',
to_fun := (linear_map.snd R E F).comp (p.prod p').subtype }
@[simp] lemma snd_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') :
linear_pmap.snd p p' x = (x : E × F).2 := rfl
instance : has_neg (linear_pmap R E F) :=
⟨λ f, ⟨f.domain, -f.to_fun⟩⟩
@[simp] lemma neg_apply (f : linear_pmap R E F) (x) : (-f) x = -(f x) := rfl
instance : has_le (linear_pmap R E F) :=
⟨λ f g, f.domain ≤ g.domain ∧ ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (h : (x:E) = y), f x = g y⟩
lemma eq_of_le_of_domain_eq {f g : linear_pmap R E F} (hle : f ≤ g) (heq : f.domain = g.domain) :
f = g :=
begin
rcases f with ⟨f_dom, f⟩,
rcases g with ⟨g_dom, g⟩,
change f_dom = g_dom at heq,
subst g_dom,
have : f = g, from linear_map.ext (λ x, hle.2 rfl),
subst g
end
/-- Given two partial linear maps `f`, `g`, the set of points `x` such that
both `f` and `g` are defined at `x` and `f x = g x` form a submodule. -/
def eq_locus (f g : linear_pmap R E F) : submodule R E :=
{ carrier := {x | ∃ (hf : x ∈ f.domain) (hg : x ∈ g.domain), f ⟨x, hf⟩ = g ⟨x, hg⟩},
zero_mem' := ⟨zero_mem _, zero_mem _, f.map_zero.trans g.map_zero.symm⟩,
add_mem' := λ x y ⟨hfx, hgx, hx⟩ ⟨hfy, hgy, hy⟩, ⟨add_mem _ hfx hfy, add_mem _ hgx hgy,
by erw [f.map_add ⟨x, hfx⟩ ⟨y, hfy⟩, g.map_add ⟨x, hgx⟩ ⟨y, hgy⟩, hx, hy]⟩,
smul_mem' := λ c x ⟨hfx, hgx, hx⟩, ⟨smul_mem _ c hfx, smul_mem _ c hgx,
by erw [f.map_smul c ⟨x, hfx⟩, g.map_smul c ⟨x, hgx⟩, hx]⟩ }
instance : has_inf (linear_pmap R E F) :=
⟨λ f g, ⟨f.eq_locus g, f.to_fun.comp $ of_le $ λ x hx, hx.fst⟩⟩
instance : has_bot (linear_pmap R E F) := ⟨⟨⊥, 0⟩⟩
instance : inhabited (linear_pmap R E F) := ⟨⊥⟩
instance : semilattice_inf_bot (linear_pmap R E F) :=
{ le := (≤),
le_refl := λ f, ⟨le_refl f.domain, λ x y h, subtype.eq h ▸ rfl⟩,
le_trans := λ f g h ⟨fg_le, fg_eq⟩ ⟨gh_le, gh_eq⟩,
⟨le_trans fg_le gh_le, λ x z hxz,
have hxy : (x:E) = of_le fg_le x, from rfl,
(fg_eq hxy).trans (gh_eq $ hxy.symm.trans hxz)⟩,
le_antisymm := λ f g fg gf, eq_of_le_of_domain_eq fg (le_antisymm fg.1 gf.1),
bot := ⊥,
bot_le := λ f, ⟨bot_le, λ x y h,
have hx : x = 0, from subtype.eq ((mem_bot R).1 x.2),
have hy : y = 0, from subtype.eq (h.symm.trans (congr_arg _ hx)),
by rw [hx, hy, map_zero, map_zero]⟩,
inf := (⊓),
le_inf := λ f g h ⟨fg_le, fg_eq⟩ ⟨fh_le, fh_eq⟩,
⟨λ x hx, ⟨fg_le hx, fh_le hx,
by refine (fg_eq _).symm.trans (fh_eq _); [exact ⟨x, hx⟩, refl, refl]⟩,
λ x ⟨y, yg, hy⟩ h, by { apply fg_eq, exact h }⟩,
inf_le_left := λ f g, ⟨λ x hx, hx.fst,
λ x y h, congr_arg f $ subtype.eq $ by exact h⟩,
inf_le_right := λ f g, ⟨λ x hx, hx.snd.fst,
λ ⟨x, xf, xg, hx⟩ y h, hx.trans $ congr_arg g $ subtype.eq $ by exact h⟩ }
lemma le_of_eq_locus_ge {f g : linear_pmap R E F} (H : f.domain ≤ f.eq_locus g) :
f ≤ g :=
suffices f ≤ f ⊓ g, from le_trans this inf_le_right,
⟨H, λ x y hxy, ((inf_le_left : f ⊓ g ≤ f).2 hxy.symm).symm⟩
lemma domain_mono : strict_mono (@domain R _ E _ _ F _ _) :=
λ f g hlt, lt_of_le_of_ne hlt.1.1 $ λ heq, ne_of_lt hlt $
eq_of_le_of_domain_eq (le_of_lt hlt) heq
private lemma sup_aux (f g : linear_pmap R E F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
∃ fg : ↥(f.domain ⊔ g.domain) →ₗ[R] F,
∀ (x : f.domain) (y : g.domain) (z),
(x:E) + y = ↑z → fg z = f x + g y :=
begin
choose x hx y hy hxy using λ z : f.domain ⊔ g.domain, mem_sup.1 z.prop,
set fg := λ z, f ⟨x z, hx z⟩ + g ⟨y z, hy z⟩,
have fg_eq : ∀ (x' : f.domain) (y' : g.domain) (z' : f.domain ⊔ g.domain) (H : (x':E) + y' = z'),
fg z' = f x' + g y',
{ intros x' y' z' H,
dsimp [fg],
rw [add_comm, ← sub_eq_sub_iff_add_eq_add, eq_comm, ← map_sub, ← map_sub],
apply h,
simp only [← eq_sub_iff_add_eq] at hxy,
simp only [coe_sub, coe_mk, coe_mk, hxy, ← sub_add, ← sub_sub, sub_self, zero_sub, ← H],
apply neg_add_eq_sub },
refine ⟨{ to_fun := fg, .. }, fg_eq⟩,
{ rintros ⟨z₁, hz₁⟩ ⟨z₂, hz₂⟩,
rw [← add_assoc, add_right_comm (f _), ← map_add, add_assoc, ← map_add],
apply fg_eq,
simp only [coe_add, coe_mk, ← add_assoc],
rw [add_right_comm (x _), hxy, add_assoc, hxy, coe_mk, coe_mk] },
{ intros c z,
rw [smul_add, ← map_smul, ← map_smul],
apply fg_eq,
simp only [coe_smul, coe_mk, ← smul_add, hxy, ring_hom.id_apply] },
end
/-- Given two partial linear maps that agree on the intersection of their domains,
`f.sup g h` is the unique partial linear map on `f.domain ⊔ g.domain` that agrees
with `f` and `g`. -/
protected noncomputable def sup (f g : linear_pmap R E F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
linear_pmap R E F :=
⟨_, classical.some (sup_aux f g h)⟩
@[simp] lemma domain_sup (f g : linear_pmap R E F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
(f.sup g h).domain = f.domain ⊔ g.domain :=
rfl
lemma sup_apply {f g : linear_pmap R E F}
(H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y)
(x y z) (hz : (↑x:E) + ↑y = ↑z) :
f.sup g H z = f x + g y :=
classical.some_spec (sup_aux f g H) x y z hz
protected lemma left_le_sup (f g : linear_pmap R E F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
f ≤ f.sup g h :=
begin
refine ⟨le_sup_left, λ z₁ z₂ hz, _⟩,
rw [← add_zero (f _), ← g.map_zero],
refine (sup_apply h _ _ _ _).symm,
simpa
end
protected lemma right_le_sup (f g : linear_pmap R E F)
(h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) :
g ≤ f.sup g h :=
begin
refine ⟨le_sup_right, λ z₁ z₂ hz, _⟩,
rw [← zero_add (g _), ← f.map_zero],
refine (sup_apply h _ _ _ _).symm,
simpa
end
protected lemma sup_le {f g h : linear_pmap R E F}
(H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y)
(fh : f ≤ h) (gh : g ≤ h) :
f.sup g H ≤ h :=
have Hf : f ≤ (f.sup g H) ⊓ h, from le_inf (f.left_le_sup g H) fh,
have Hg : g ≤ (f.sup g H) ⊓ h, from le_inf (f.right_le_sup g H) gh,
le_of_eq_locus_ge $ sup_le Hf.1 Hg.1
/-- Hypothesis for `linear_pmap.sup` holds, if `f.domain` is disjoint with `g.domain`. -/
lemma sup_h_of_disjoint (f g : linear_pmap R E F) (h : disjoint f.domain g.domain)
(x : f.domain) (y : g.domain) (hxy : (x:E) = y) :
f x = g y :=
begin
rw [disjoint_def] at h,
have hy : y = 0, from subtype.eq (h y (hxy ▸ x.2) y.2),
have hx : x = 0, from subtype.eq (hxy.trans $ congr_arg _ hy),
simp [*]
end
section
variables {K : Type*} [division_ring K] [module K E] [module K F]
/-- Extend a `linear_pmap` to `f.domain ⊔ K ∙ x`. -/
noncomputable def sup_span_singleton (f : linear_pmap K E F) (x : E) (y : F) (hx : x ∉ f.domain) :
linear_pmap K E F :=
f.sup (mk_span_singleton x y (λ h₀, hx $ h₀.symm ▸ f.domain.zero_mem)) $
sup_h_of_disjoint _ _ $ by simpa [disjoint_span_singleton]
@[simp] lemma domain_sup_span_singleton (f : linear_pmap K E F) (x : E) (y : F)
(hx : x ∉ f.domain) :
(f.sup_span_singleton x y hx).domain = f.domain ⊔ K ∙ x := rfl
@[simp] lemma sup_span_singleton_apply_mk (f : linear_pmap K E F) (x : E) (y : F)
(hx : x ∉ f.domain) (x' : E) (hx' : x' ∈ f.domain) (c : K) :
f.sup_span_singleton x y hx ⟨x' + c • x,
mem_sup.2 ⟨x', hx', _, mem_span_singleton.2 ⟨c, rfl⟩, rfl⟩⟩ = f ⟨x', hx'⟩ + c • y :=
begin
erw [sup_apply _ ⟨x', hx'⟩ ⟨c • x, _⟩, mk_span_singleton_apply],
refl,
exact mem_span_singleton.2 ⟨c, rfl⟩
end
end
private lemma Sup_aux (c : set (linear_pmap R E F)) (hc : directed_on (≤) c) :
∃ f : ↥(Sup (domain '' c)) →ₗ[R] F, (⟨_, f⟩ : linear_pmap R E F) ∈ upper_bounds c :=
begin
cases c.eq_empty_or_nonempty with ceq cne, { subst c, simp },
have hdir : directed_on (≤) (domain '' c),
from directed_on_image.2 (hc.mono domain_mono.monotone),
have P : Π x : Sup (domain '' c), {p : c // (x : E) ∈ p.val.domain },
{ rintros x,
apply classical.indefinite_description,
have := (mem_Sup_of_directed (cne.image _) hdir).1 x.2,
rwa [bex_image_iff, set_coe.exists'] at this },
set f : Sup (domain '' c) → F := λ x, (P x).val.val ⟨x, (P x).property⟩,
have f_eq : ∀ (p : c) (x : Sup (domain '' c)) (y : p.1.1) (hxy : (x : E) = y), f x = p.1 y,
{ intros p x y hxy,
rcases hc (P x).1.1 (P x).1.2 p.1 p.2 with ⟨q, hqc, hxq, hpq⟩,
refine (hxq.2 _).trans (hpq.2 _).symm,
exacts [of_le hpq.1 y, hxy, rfl] },
refine ⟨{ to_fun := f, .. }, _⟩,
{ intros x y,
rcases hc (P x).1.1 (P x).1.2 (P y).1.1 (P y).1.2 with ⟨p, hpc, hpx, hpy⟩,
set x' := of_le hpx.1 ⟨x, (P x).2⟩,
set y' := of_le hpy.1 ⟨y, (P y).2⟩,
rw [f_eq ⟨p, hpc⟩ x x' rfl, f_eq ⟨p, hpc⟩ y y' rfl, f_eq ⟨p, hpc⟩ (x + y) (x' + y') rfl,
map_add] },
{ intros c x,
simp [f_eq (P x).1 (c • x) (c • ⟨x, (P x).2⟩) rfl, ← map_smul] },
{ intros p hpc,
refine ⟨le_Sup $ mem_image_of_mem domain hpc, λ x y hxy, eq.symm _⟩,
exact f_eq ⟨p, hpc⟩ _ _ hxy.symm }
end
/-- Glue a collection of partially defined linear maps to a linear map defined on `Sup`
of these submodules. -/
protected noncomputable def Sup (c : set (linear_pmap R E F)) (hc : directed_on (≤) c) :
linear_pmap R E F :=
⟨_, classical.some $ Sup_aux c hc⟩
protected lemma le_Sup {c : set (linear_pmap R E F)} (hc : directed_on (≤) c)
{f : linear_pmap R E F} (hf : f ∈ c) : f ≤ linear_pmap.Sup c hc :=
classical.some_spec (Sup_aux c hc) hf
protected lemma Sup_le {c : set (linear_pmap R E F)} (hc : directed_on (≤) c)
{g : linear_pmap R E F} (hg : ∀ f ∈ c, f ≤ g) : linear_pmap.Sup c hc ≤ g :=
le_of_eq_locus_ge $ Sup_le $ λ _ ⟨f, hf, eq⟩, eq ▸
have f ≤ (linear_pmap.Sup c hc) ⊓ g, from le_inf (linear_pmap.le_Sup _ hf) (hg f hf),
this.1
end linear_pmap
namespace linear_map
/-- Restrict a linear map to a submodule, reinterpreting the result as a `linear_pmap`. -/
def to_pmap (f : E →ₗ[R] F) (p : submodule R E) : linear_pmap R E F :=
⟨p, f.comp p.subtype⟩
@[simp] lemma to_pmap_apply (f : E →ₗ[R] F) (p : submodule R E) (x : p) :
f.to_pmap p x = f x := rfl
/-- Compose a linear map with a `linear_pmap` -/
def comp_pmap (g : F →ₗ[R] G) (f : linear_pmap R E F) : linear_pmap R E G :=
{ domain := f.domain,
to_fun := g.comp f.to_fun }
@[simp] lemma comp_pmap_apply (g : F →ₗ[R] G) (f : linear_pmap R E F) (x) :
g.comp_pmap f x = g (f x) := rfl
end linear_map
namespace linear_pmap
/-- Restrict codomain of a `linear_pmap` -/
def cod_restrict (f : linear_pmap R E F) (p : submodule R F) (H : ∀ x, f x ∈ p) :
linear_pmap R E p :=
{ domain := f.domain,
to_fun := f.to_fun.cod_restrict p H }
/-- Compose two `linear_pmap`s -/
def comp (g : linear_pmap R F G) (f : linear_pmap R E F)
(H : ∀ x : f.domain, f x ∈ g.domain) :
linear_pmap R E G :=
g.to_fun.comp_pmap $ f.cod_restrict _ H
/-- `f.coprod g` is the partially defined linear map defined on `f.domain × g.domain`,
and sending `p` to `f p.1 + g p.2`. -/
def coprod (f : linear_pmap R E G) (g : linear_pmap R F G) :
linear_pmap R (E × F) G :=
{ domain := f.domain.prod g.domain,
to_fun := (f.comp (linear_pmap.fst f.domain g.domain) (λ x, x.2.1)).to_fun +
(g.comp (linear_pmap.snd f.domain g.domain) (λ x, x.2.2)).to_fun }
@[simp] lemma coprod_apply (f : linear_pmap R E G) (g : linear_pmap R F G) (x) :
f.coprod g x = f ⟨(x : E × F).1, x.2.1⟩ + g ⟨(x : E × F).2, x.2.2⟩ :=
rfl
end linear_pmap
|
63f8887ab950d53d0baa8504dc95b0f50957969b | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/topology/algebra/order/intermediate_value.lean | ea6373adce4de35d64ac83f50d3d06cc132d80f7 | [
"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 | 31,406 | 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, Alistair Tucker
-/
import order.complete_lattice_intervals
import topology.algebra.order.basic
/-!
# Intermediate Value Theorem
In this file we prove the Intermediate Value Theorem: if `f : α → β` is a function defined on a
connected set `s` that takes both values `≤ a` and values `≥ a` on `s`, then it is equal to `a` at
some point of `s`. We also prove that intervals in a dense conditionally complete order are
preconnected and any preconnected set is an interval. Then we specialize IVT to functions continuous
on intervals.
## Main results
* `is_preconnected_I??` : all intervals `I??` are preconnected,
* `is_preconnected.intermediate_value`, `intermediate_value_univ` : Intermediate Value Theorem for
connected sets and connected spaces, respectively;
* `intermediate_value_Icc`, `intermediate_value_Icc'`: Intermediate Value Theorem for functions
on closed intervals.
### Miscellaneous facts
* `is_closed.Icc_subset_of_forall_mem_nhds_within` : “Continuous induction” principle;
if `s ∩ [a, b]` is closed, `a ∈ s`, and for each `x ∈ [a, b) ∩ s` some of its right neighborhoods
is included `s`, then `[a, b] ⊆ s`.
* `is_closed.Icc_subset_of_forall_exists_gt`, `is_closed.mem_of_ge_of_forall_exists_gt` : two
other versions of the “continuous induction” principle.
## Tags
intermediate value theorem, connected space, connected set
-/
open filter order_dual topological_space function set
open_locale topological_space filter
universes u v w
/-!
### Intermediate value theorem on a (pre)connected space
In this section we prove the following theorem (see `is_preconnected.intermediate_value₂`): if `f`
and `g` are two functions continuous on a preconnected set `s`, `f a ≤ g a` at some `a ∈ s` and
`g b ≤ f b` at some `b ∈ s`, then `f c = g c` at some `c ∈ s`. We prove several versions of this
statement, including the classical IVT that corresponds to a constant function `g`.
-/
section
variables {X : Type u} {α : Type v} [topological_space X]
[linear_order α] [topological_space α] [order_closed_topology α]
/-- Intermediate value theorem for two functions: if `f` and `g` are two continuous functions
on a preconnected space and `f a ≤ g a` and `g b ≤ f b`, then for some `x` we have `f x = g x`. -/
lemma intermediate_value_univ₂ [preconnected_space X] {a b : X} {f g : X → α} (hf : continuous f)
(hg : continuous g) (ha : f a ≤ g a) (hb : g b ≤ f b) :
∃ x, f x = g x :=
begin
obtain ⟨x, h, hfg, hgf⟩ : (univ ∩ {x | f x ≤ g x ∧ g x ≤ f x}).nonempty,
from is_preconnected_closed_iff.1 preconnected_space.is_preconnected_univ _ _
(is_closed_le hf hg) (is_closed_le hg hf) (λ x hx, le_total _ _) ⟨a, trivial, ha⟩
⟨b, trivial, hb⟩,
exact ⟨x, le_antisymm hfg hgf⟩
end
lemma intermediate_value_univ₂_eventually₁ [preconnected_space X] {a : X} {l : filter X} [ne_bot l]
{f g : X → α} (hf : continuous f) (hg : continuous g) (ha : f a ≤ g a) (he : g ≤ᶠ[l] f) :
∃ x, f x = g x :=
let ⟨c, hc⟩ := he.frequently.exists in intermediate_value_univ₂ hf hg ha hc
lemma intermediate_value_univ₂_eventually₂ [preconnected_space X] {l₁ l₂ : filter X}
[ne_bot l₁] [ne_bot l₂] {f g : X → α} (hf : continuous f) (hg : continuous g)
(he₁ : f ≤ᶠ[l₁] g ) (he₂ : g ≤ᶠ[l₂] f) :
∃ x, f x = g x :=
let ⟨c₁, hc₁⟩ := he₁.frequently.exists, ⟨c₂, hc₂⟩ := he₂.frequently.exists in
intermediate_value_univ₂ hf hg hc₁ hc₂
/-- Intermediate value theorem for two functions: if `f` and `g` are two functions continuous
on a preconnected set `s` and for some `a b ∈ s` we have `f a ≤ g a` and `g b ≤ f b`,
then for some `x ∈ s` we have `f x = g x`. -/
lemma is_preconnected.intermediate_value₂ {s : set X} (hs : is_preconnected s)
{a b : X} (ha : a ∈ s) (hb : b ∈ s) {f g : X → α}
(hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a ≤ g a) (hb' : g b ≤ f b) :
∃ x ∈ s, f x = g x :=
let ⟨x, hx⟩ := @intermediate_value_univ₂ s α _ _ _ _ (subtype.preconnected_space hs) ⟨a, ha⟩ ⟨b, hb⟩
_ _ (continuous_on_iff_continuous_restrict.1 hf) (continuous_on_iff_continuous_restrict.1 hg)
ha' hb'
in ⟨x, x.2, hx⟩
lemma is_preconnected.intermediate_value₂_eventually₁ {s : set X} (hs : is_preconnected s)
{a : X} {l : filter X} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f g : X → α}
(hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a ≤ g a) (he : g ≤ᶠ[l] f) :
∃ x ∈ s, f x = g x :=
begin
rw continuous_on_iff_continuous_restrict at hf hg,
obtain ⟨b, h⟩ := @intermediate_value_univ₂_eventually₁ _ _ _ _ _ _ (subtype.preconnected_space hs)
⟨a, ha⟩ _ (comap_coe_ne_bot_of_le_principal hl) _ _ hf hg ha' (he.comap _),
exact ⟨b, b.prop, h⟩,
end
lemma is_preconnected.intermediate_value₂_eventually₂ {s : set X} (hs : is_preconnected s)
{l₁ l₂ : filter X} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f g : X → α}
(hf : continuous_on f s) (hg : continuous_on g s) (he₁ : f ≤ᶠ[l₁] g) (he₂ : g ≤ᶠ[l₂] f) :
∃ x ∈ s, f x = g x :=
begin
rw continuous_on_iff_continuous_restrict at hf hg,
obtain ⟨b, h⟩ := @intermediate_value_univ₂_eventually₂ _ _ _ _ _ _ (subtype.preconnected_space hs)
_ _ (comap_coe_ne_bot_of_le_principal hl₁) (comap_coe_ne_bot_of_le_principal hl₂)
_ _ hf hg (he₁.comap _) (he₂.comap _),
exact ⟨b, b.prop, h⟩,
end
/-- **Intermediate Value Theorem** for continuous functions on connected sets. -/
lemma is_preconnected.intermediate_value {s : set X} (hs : is_preconnected s)
{a b : X} (ha : a ∈ s) (hb : b ∈ s) {f : X → α} (hf : continuous_on f s) :
Icc (f a) (f b) ⊆ f '' s :=
λ x hx, mem_image_iff_bex.2 $ hs.intermediate_value₂ ha hb hf continuous_on_const hx.1 hx.2
lemma is_preconnected.intermediate_value_Ico {s : set X} (hs : is_preconnected s)
{a : X} {l : filter X} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f : X → α}
(hf : continuous_on f s) {v : α} (ht : tendsto f l (𝓝 v)) :
Ico (f a) v ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₁ ha hl
hf continuous_on_const h.1 (eventually_ge_of_tendsto_gt h.2 ht)
lemma is_preconnected.intermediate_value_Ioc {s : set X} (hs : is_preconnected s)
{a : X} {l : filter X} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f : X → α}
(hf : continuous_on f s) {v : α} (ht : tendsto f l (𝓝 v)) :
Ioc v (f a) ⊆ f '' s :=
λ y h, bex_def.1 $ bex.imp_right (λ x _, eq.symm) $ hs.intermediate_value₂_eventually₁ ha hl
continuous_on_const hf h.2 (eventually_le_of_tendsto_lt h.1 ht)
lemma is_preconnected.intermediate_value_Ioo {s : set X} (hs : is_preconnected s)
{l₁ l₂ : filter X} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f : X → α}
(hf : continuous_on f s) {v₁ v₂ : α} (ht₁ : tendsto f l₁ (𝓝 v₁)) (ht₂ : tendsto f l₂ (𝓝 v₂)) :
Ioo v₁ v₂ ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₂ hl₁ hl₂
hf continuous_on_const (eventually_le_of_tendsto_lt h.1 ht₁) (eventually_ge_of_tendsto_gt h.2 ht₂)
lemma is_preconnected.intermediate_value_Ici {s : set X} (hs : is_preconnected s)
{a : X} {l : filter X} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f : X → α}
(hf : continuous_on f s) (ht : tendsto f l at_top) :
Ici (f a) ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₁ ha hl
hf continuous_on_const h (tendsto_at_top.1 ht y)
lemma is_preconnected.intermediate_value_Iic {s : set X} (hs : is_preconnected s)
{a : X} {l : filter X} (ha : a ∈ s) [ne_bot l] (hl : l ≤ 𝓟 s) {f : X → α}
(hf : continuous_on f s) (ht : tendsto f l at_bot) :
Iic (f a) ⊆ f '' s :=
λ y h, bex_def.1 $ bex.imp_right (λ x _, eq.symm) $ hs.intermediate_value₂_eventually₁ ha hl
continuous_on_const hf h (tendsto_at_bot.1 ht y)
lemma is_preconnected.intermediate_value_Ioi {s : set X} (hs : is_preconnected s)
{l₁ l₂ : filter X} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f : X → α}
(hf : continuous_on f s) {v : α} (ht₁ : tendsto f l₁ (𝓝 v)) (ht₂ : tendsto f l₂ at_top) :
Ioi v ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₂ hl₁ hl₂
hf continuous_on_const (eventually_le_of_tendsto_lt h ht₁) (tendsto_at_top.1 ht₂ y)
lemma is_preconnected.intermediate_value_Iio {s : set X} (hs : is_preconnected s)
{l₁ l₂ : filter X} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f : X → α}
(hf : continuous_on f s) {v : α} (ht₁ : tendsto f l₁ at_bot) (ht₂ : tendsto f l₂ (𝓝 v)) :
Iio v ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₂ hl₁ hl₂
hf continuous_on_const (tendsto_at_bot.1 ht₁ y) (eventually_ge_of_tendsto_gt h ht₂)
lemma is_preconnected.intermediate_value_Iii {s : set X} (hs : is_preconnected s)
{l₁ l₂ : filter X} [ne_bot l₁] [ne_bot l₂] (hl₁ : l₁ ≤ 𝓟 s) (hl₂ : l₂ ≤ 𝓟 s) {f : X → α}
(hf : continuous_on f s) (ht₁ : tendsto f l₁ at_bot) (ht₂ : tendsto f l₂ at_top) :
univ ⊆ f '' s :=
λ y h, bex_def.1 $ hs.intermediate_value₂_eventually₂ hl₁ hl₂
hf continuous_on_const (tendsto_at_bot.1 ht₁ y) (tendsto_at_top.1 ht₂ y)
/-- **Intermediate Value Theorem** for continuous functions on connected spaces. -/
lemma intermediate_value_univ [preconnected_space X] (a b : X) {f : X → α} (hf : continuous f) :
Icc (f a) (f b) ⊆ range f :=
λ x hx, intermediate_value_univ₂ hf continuous_const hx.1 hx.2
/-- **Intermediate Value Theorem** for continuous functions on connected spaces. -/
lemma mem_range_of_exists_le_of_exists_ge [preconnected_space X] {c : α} {f : X → α}
(hf : continuous f) (h₁ : ∃ a, f a ≤ c) (h₂ : ∃ b, c ≤ f b) :
c ∈ range f :=
let ⟨a, ha⟩ := h₁, ⟨b, hb⟩ := h₂ in intermediate_value_univ a b hf ⟨ha, hb⟩
/-!
### (Pre)connected sets in a linear order
In this section we prove the following results:
* `is_preconnected.ord_connected`: any preconnected set `s` in a linear order is `ord_connected`,
i.e. `a ∈ s` and `b ∈ s` imply `Icc a b ⊆ s`;
* `is_preconnected.mem_intervals`: any preconnected set `s` in a conditionally complete linear order
is one of the intervals `set.Icc`, `set.`Ico`, `set.Ioc`, `set.Ioo`, ``set.Ici`, `set.Iic`,
`set.Ioi`, `set.Iio`; note that this is false for non-complete orders: e.g., in `ℝ \ {0}`, the set
of positive numbers cannot be represented as `set.Ioi _`.
-/
/-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/
lemma is_preconnected.Icc_subset {s : set α} (hs : is_preconnected s)
{a b : α} (ha : a ∈ s) (hb : b ∈ s) :
Icc a b ⊆ s :=
by simpa only [image_id] using hs.intermediate_value ha hb continuous_on_id
lemma is_preconnected.ord_connected {s : set α} (h : is_preconnected s) :
ord_connected s :=
⟨λ x hx y hy, h.Icc_subset hx hy⟩
/-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/
lemma is_connected.Icc_subset {s : set α} (hs : is_connected s)
{a b : α} (ha : a ∈ s) (hb : b ∈ s) :
Icc a b ⊆ s :=
hs.2.Icc_subset ha hb
/-- If preconnected set in a linear order space is unbounded below and above, then it is the whole
space. -/
lemma is_preconnected.eq_univ_of_unbounded {s : set α} (hs : is_preconnected s) (hb : ¬bdd_below s)
(ha : ¬bdd_above s) :
s = univ :=
begin
refine eq_univ_of_forall (λ x, _),
obtain ⟨y, ys, hy⟩ : ∃ y ∈ s, y < x := not_bdd_below_iff.1 hb x,
obtain ⟨z, zs, hz⟩ : ∃ z ∈ s, x < z := not_bdd_above_iff.1 ha x,
exact hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩
end
end
variables {α : Type u} {β : Type v} {γ : Type w}
[conditionally_complete_linear_order α] [topological_space α] [order_topology α]
[conditionally_complete_linear_order β] [topological_space β] [order_topology β]
[nonempty γ]
/-- A bounded connected subset of a conditionally complete linear order includes the open interval
`(Inf s, Sup s)`. -/
lemma is_connected.Ioo_cInf_cSup_subset {s : set α} (hs : is_connected s) (hb : bdd_below s)
(ha : bdd_above s) :
Ioo (Inf s) (Sup s) ⊆ s :=
λ x hx, let ⟨y, ys, hy⟩ := (is_glb_lt_iff (is_glb_cInf hs.nonempty hb)).1 hx.1 in
let ⟨z, zs, hz⟩ := (lt_is_lub_iff (is_lub_cSup hs.nonempty ha)).1 hx.2 in
hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩
lemma eq_Icc_cInf_cSup_of_connected_bdd_closed {s : set α} (hc : is_connected s) (hb : bdd_below s)
(ha : bdd_above s) (hcl : is_closed s) :
s = Icc (Inf s) (Sup s) :=
subset.antisymm (subset_Icc_cInf_cSup hb ha) $
hc.Icc_subset (hcl.cInf_mem hc.nonempty hb) (hcl.cSup_mem hc.nonempty ha)
lemma is_preconnected.Ioi_cInf_subset {s : set α} (hs : is_preconnected s) (hb : bdd_below s)
(ha : ¬bdd_above s) :
Ioi (Inf s) ⊆ s :=
begin
have sne : s.nonempty := @nonempty_of_not_bdd_above α _ s ⟨Inf ∅⟩ ha,
intros x hx,
obtain ⟨y, ys, hy⟩ : ∃ y ∈ s, y < x := (is_glb_lt_iff (is_glb_cInf sne hb)).1 hx,
obtain ⟨z, zs, hz⟩ : ∃ z ∈ s, x < z := not_bdd_above_iff.1 ha x,
exact hs.Icc_subset ys zs ⟨le_of_lt hy, le_of_lt hz⟩
end
lemma is_preconnected.Iio_cSup_subset {s : set α} (hs : is_preconnected s) (hb : ¬bdd_below s)
(ha : bdd_above s) :
Iio (Sup s) ⊆ s :=
@is_preconnected.Ioi_cInf_subset αᵒᵈ _ _ _ s hs ha hb
/-- A preconnected set in a conditionally complete linear order is either one of the intervals
`[Inf s, Sup s]`, `[Inf s, Sup s)`, `(Inf s, Sup s]`, `(Inf s, Sup s)`, `[Inf s, +∞)`,
`(Inf s, +∞)`, `(-∞, Sup s]`, `(-∞, Sup s)`, `(-∞, +∞)`, or `∅`. The converse statement requires
`α` to be densely ordererd. -/
lemma is_preconnected.mem_intervals {s : set α} (hs : is_preconnected s) :
s ∈ ({Icc (Inf s) (Sup s), Ico (Inf s) (Sup s), Ioc (Inf s) (Sup s), Ioo (Inf s) (Sup s),
Ici (Inf s), Ioi (Inf s), Iic (Sup s), Iio (Sup s), univ, ∅} : set (set α)) :=
begin
rcases s.eq_empty_or_nonempty with rfl|hne,
{ apply_rules [or.inr, mem_singleton] },
have hs' : is_connected s := ⟨hne, hs⟩,
by_cases hb : bdd_below s; by_cases ha : bdd_above s,
{ rcases mem_Icc_Ico_Ioc_Ioo_of_subset_of_subset (hs'.Ioo_cInf_cSup_subset hb ha)
(subset_Icc_cInf_cSup hb ha) with hs|hs|hs|hs,
{ exact (or.inl hs) },
{ exact (or.inr $ or.inl hs) },
{ exact (or.inr $ or.inr $ or.inl hs) },
{ exact (or.inr $ or.inr $ or.inr $ or.inl hs) } },
{ refine (or.inr $ or.inr $ or.inr $ or.inr _),
cases mem_Ici_Ioi_of_subset_of_subset (hs.Ioi_cInf_subset hb ha) (λ x hx, cInf_le hb hx)
with hs hs,
{ exact or.inl hs },
{ exact or.inr (or.inl hs) } },
{ iterate 6 { apply or.inr },
cases mem_Iic_Iio_of_subset_of_subset (hs.Iio_cSup_subset hb ha) (λ x hx, le_cSup ha hx)
with hs hs,
{ exact or.inl hs },
{ exact or.inr (or.inl hs) } },
{ iterate 8 { apply or.inr },
exact or.inl (hs.eq_univ_of_unbounded hb ha) }
end
/-- A preconnected set is either one of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`,
`Iic`, `Iio`, or `univ`, or `∅`. The converse statement requires `α` to be densely ordered. Though
one can represent `∅` as `(Inf s, Inf s)`, we include it into the list of possible cases to improve
readability. -/
lemma set_of_is_preconnected_subset_of_ordered :
{s : set α | is_preconnected s} ⊆
-- bounded intervals
(range (uncurry Icc) ∪ range (uncurry Ico) ∪ range (uncurry Ioc) ∪ range (uncurry Ioo)) ∪
-- unbounded intervals and `univ`
(range Ici ∪ range Ioi ∪ range Iic ∪ range Iio ∪ {univ, ∅}) :=
begin
intros s hs,
rcases hs.mem_intervals with hs|hs|hs|hs|hs|hs|hs|hs|hs|hs,
{ exact (or.inl $ or.inl $ or.inl $ or.inl ⟨(Inf s, Sup s), hs.symm⟩) },
{ exact (or.inl $ or.inl $ or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) },
{ exact (or.inl $ or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) },
{ exact (or.inl $ or.inr ⟨(Inf s, Sup s), hs.symm⟩) },
{ exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inl ⟨Inf s, hs.symm⟩) },
{ exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inr ⟨Inf s, hs.symm⟩) },
{ exact (or.inr $ or.inl $ or.inl $ or.inr ⟨Sup s, hs.symm⟩) },
{ exact (or.inr $ or.inl $ or.inr ⟨Sup s, hs.symm⟩) },
{ exact (or.inr $ or.inr $ or.inl hs) },
{ exact (or.inr $ or.inr $ or.inr hs) }
end
/-!
### Intervals are connected
In this section we prove that a closed interval (hence, any `ord_connected` set) in a dense
conditionally complete linear order is preconnected.
-/
/-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]`
on a closed subset, contains `a`, and the set `s ∩ [a, b)` has no maximal point, then `b ∈ s`. -/
lemma is_closed.mem_of_ge_of_forall_exists_gt {a b : α} {s : set α} (hs : is_closed (s ∩ Icc a b))
(ha : a ∈ s) (hab : a ≤ b) (hgt : ∀ x ∈ s ∩ Ico a b, (s ∩ Ioc x b).nonempty) :
b ∈ s :=
begin
let S := s ∩ Icc a b,
replace ha : a ∈ S, from ⟨ha, left_mem_Icc.2 hab⟩,
have Sbd : bdd_above S, from ⟨b, λ z hz, hz.2.2⟩,
let c := Sup (s ∩ Icc a b),
have c_mem : c ∈ S, from hs.cSup_mem ⟨_, ha⟩ Sbd,
have c_le : c ≤ b, from cSup_le ⟨_, ha⟩ (λ x hx, hx.2.2),
cases eq_or_lt_of_le c_le with hc hc, from hc ▸ c_mem.1,
exfalso,
rcases hgt c ⟨c_mem.1, c_mem.2.1, hc⟩ with ⟨x, xs, cx, xb⟩,
exact not_lt_of_le (le_cSup Sbd ⟨xs, le_trans (le_cSup Sbd ha) (le_of_lt cx), xb⟩) cx
end
/-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]`
on a closed subset, contains `a`, and for any `a ≤ x < y ≤ b`, `x ∈ s`, the set `s ∩ (x, y]`
is not empty, then `[a, b] ⊆ s`. -/
lemma is_closed.Icc_subset_of_forall_exists_gt {a b : α} {s : set α} (hs : is_closed (s ∩ Icc a b))
(ha : a ∈ s) (hgt : ∀ x ∈ s ∩ Ico a b, ∀ y ∈ Ioi x, (s ∩ Ioc x y).nonempty) :
Icc a b ⊆ s :=
begin
assume y hy,
have : is_closed (s ∩ Icc a y),
{ suffices : s ∩ Icc a y = s ∩ Icc a b ∩ Icc a y,
{ rw this, exact is_closed.inter hs is_closed_Icc },
rw [inter_assoc],
congr,
exact (inter_eq_self_of_subset_right $ Icc_subset_Icc_right hy.2).symm },
exact is_closed.mem_of_ge_of_forall_exists_gt this ha hy.1
(λ x hx, hgt x ⟨hx.1, Ico_subset_Ico_right hy.2 hx.2⟩ y hx.2.2)
end
variables [densely_ordered α] {a b : α}
/-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]`
on a closed subset, contains `a`, and for any `x ∈ s ∩ [a, b)` the set `s` includes some open
neighborhood of `x` within `(x, +∞)`, then `[a, b] ⊆ s`. -/
lemma is_closed.Icc_subset_of_forall_mem_nhds_within {a b : α} {s : set α}
(hs : is_closed (s ∩ Icc a b)) (ha : a ∈ s)
(hgt : ∀ x ∈ s ∩ Ico a b, s ∈ 𝓝[>] x) :
Icc a b ⊆ s :=
begin
apply hs.Icc_subset_of_forall_exists_gt ha,
rintros x ⟨hxs, hxab⟩ y hyxb,
have : s ∩ Ioc x y ∈ 𝓝[>] x,
from inter_mem (hgt x ⟨hxs, hxab⟩) (Ioc_mem_nhds_within_Ioi ⟨le_rfl, hyxb⟩),
exact (nhds_within_Ioi_self_ne_bot' ⟨b, hxab.2⟩).nonempty_of_mem this
end
/-- A closed interval in a densely ordered conditionally complete linear order is preconnected. -/
lemma is_preconnected_Icc : is_preconnected (Icc a b) :=
is_preconnected_closed_iff.2
begin
rintros s t hs ht hab ⟨x, hx⟩ ⟨y, hy⟩,
wlog hxy : x ≤ y := le_total x y using [x y s t, y x t s],
have xyab : Icc x y ⊆ Icc a b := Icc_subset_Icc hx.1.1 hy.1.2,
by_contradiction hst,
suffices : Icc x y ⊆ s,
from hst ⟨y, xyab $ right_mem_Icc.2 hxy, this $ right_mem_Icc.2 hxy, hy.2⟩,
apply (is_closed.inter hs is_closed_Icc).Icc_subset_of_forall_mem_nhds_within hx.2,
rintros z ⟨zs, hz⟩,
have zt : z ∈ tᶜ, from λ zt, hst ⟨z, xyab $ Ico_subset_Icc_self hz, zs, zt⟩,
have : tᶜ ∩ Ioc z y ∈ 𝓝[>] z,
{ rw [← nhds_within_Ioc_eq_nhds_within_Ioi hz.2],
exact mem_nhds_within.2 ⟨tᶜ, ht.is_open_compl, zt, subset.refl _⟩},
apply mem_of_superset this,
have : Ioc z y ⊆ s ∪ t, from λ w hw, hab (xyab ⟨le_trans hz.1 (le_of_lt hw.1), hw.2⟩),
exact λ w ⟨wt, wzy⟩, (this wzy).elim id (λ h, (wt h).elim)
end
lemma is_preconnected_interval : is_preconnected (interval a b) := is_preconnected_Icc
lemma set.ord_connected.is_preconnected {s : set α} (h : s.ord_connected) :
is_preconnected s :=
is_preconnected_of_forall_pair $ λ x hx y hy, ⟨interval x y, h.interval_subset hx hy,
left_mem_interval, right_mem_interval, is_preconnected_interval⟩
lemma is_preconnected_iff_ord_connected {s : set α} :
is_preconnected s ↔ ord_connected s :=
⟨is_preconnected.ord_connected, set.ord_connected.is_preconnected⟩
lemma is_preconnected_Ici : is_preconnected (Ici a) := ord_connected_Ici.is_preconnected
lemma is_preconnected_Iic : is_preconnected (Iic a) := ord_connected_Iic.is_preconnected
lemma is_preconnected_Iio : is_preconnected (Iio a) := ord_connected_Iio.is_preconnected
lemma is_preconnected_Ioi : is_preconnected (Ioi a) := ord_connected_Ioi.is_preconnected
lemma is_preconnected_Ioo : is_preconnected (Ioo a b) := ord_connected_Ioo.is_preconnected
lemma is_preconnected_Ioc : is_preconnected (Ioc a b) := ord_connected_Ioc.is_preconnected
lemma is_preconnected_Ico : is_preconnected (Ico a b) := ord_connected_Ico.is_preconnected
lemma is_connected_Ici : is_connected (Ici a) := ⟨nonempty_Ici, is_preconnected_Ici⟩
lemma is_connected_Iic : is_connected (Iic a) := ⟨nonempty_Iic, is_preconnected_Iic⟩
lemma is_connected_Ioi [no_max_order α] : is_connected (Ioi a) :=
⟨nonempty_Ioi, is_preconnected_Ioi⟩
lemma is_connected_Iio [no_min_order α] : is_connected (Iio a) :=
⟨nonempty_Iio, is_preconnected_Iio⟩
lemma is_connected_Icc (h : a ≤ b) : is_connected (Icc a b) :=
⟨nonempty_Icc.2 h, is_preconnected_Icc⟩
lemma is_connected_Ioo (h : a < b) : is_connected (Ioo a b) :=
⟨nonempty_Ioo.2 h, is_preconnected_Ioo⟩
lemma is_connected_Ioc (h : a < b) : is_connected (Ioc a b) :=
⟨nonempty_Ioc.2 h, is_preconnected_Ioc⟩
lemma is_connected_Ico (h : a < b) : is_connected (Ico a b) :=
⟨nonempty_Ico.2 h, is_preconnected_Ico⟩
@[priority 100]
instance ordered_connected_space : preconnected_space α :=
⟨ord_connected_univ.is_preconnected⟩
/-- In a dense conditionally complete linear order, the set of preconnected sets is exactly
the set of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`, `Iic`, `Iio`, `(-∞, +∞)`,
or `∅`. Though one can represent `∅` as `(Inf s, Inf s)`, we include it into the list of
possible cases to improve readability. -/
lemma set_of_is_preconnected_eq_of_ordered :
{s : set α | is_preconnected s} =
-- bounded intervals
(range (uncurry Icc) ∪ range (uncurry Ico) ∪ range (uncurry Ioc) ∪ range (uncurry Ioo)) ∪
-- unbounded intervals and `univ`
(range Ici ∪ range Ioi ∪ range Iic ∪ range Iio ∪ {univ, ∅}) :=
begin
refine subset.antisymm set_of_is_preconnected_subset_of_ordered _,
simp only [subset_def, -mem_range, forall_range_iff, uncurry, or_imp_distrib, forall_and_distrib,
mem_union, mem_set_of_eq, insert_eq, mem_singleton_iff, forall_eq, forall_true_iff, and_true,
is_preconnected_Icc, is_preconnected_Ico, is_preconnected_Ioc,
is_preconnected_Ioo, is_preconnected_Ioi, is_preconnected_Iio, is_preconnected_Ici,
is_preconnected_Iic, is_preconnected_univ, is_preconnected_empty],
end
/-!
### Intermediate Value Theorem on an interval
In this section we prove several versions of the Intermediate Value Theorem for a function
continuous on an interval.
-/
variables {δ : Type*} [linear_order δ] [topological_space δ] [order_closed_topology δ]
/-- **Intermediate Value Theorem** for continuous functions on closed intervals, case
`f a ≤ t ≤ f b`.-/
lemma intermediate_value_Icc {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Icc (f a) (f b) ⊆ f '' (Icc a b) :=
is_preconnected_Icc.intermediate_value (left_mem_Icc.2 hab) (right_mem_Icc.2 hab) hf
/-- **Intermediate Value Theorem** for continuous functions on closed intervals, case
`f a ≥ t ≥ f b`.-/
lemma intermediate_value_Icc' {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Icc (f b) (f a) ⊆ f '' (Icc a b) :=
is_preconnected_Icc.intermediate_value (right_mem_Icc.2 hab) (left_mem_Icc.2 hab) hf
/-- **Intermediate Value Theorem** for continuous functions on closed intervals, unordered case. -/
lemma intermediate_value_interval {a b : α} {f : α → δ} (hf : continuous_on f (interval a b)) :
interval (f a) (f b) ⊆ f '' interval a b :=
by cases le_total (f a) (f b); simp [*, is_preconnected_interval.intermediate_value]
lemma intermediate_value_Ico {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ico (f a) (f b) ⊆ f '' (Ico a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.2 (not_lt_of_le (he ▸ h.1)))
(λ hlt, @is_preconnected.intermediate_value_Ico _ _ _ _ _ _ _ (is_preconnected_Ico)
_ _ ⟨refl a, hlt⟩ (right_nhds_within_Ico_ne_bot hlt) inf_le_right _ (hf.mono Ico_subset_Icc_self)
_ ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ico_subset_Icc_self))
lemma intermediate_value_Ico' {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ioc (f b) (f a) ⊆ f '' (Ico a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.1 (not_lt_of_le (he ▸ h.2)))
(λ hlt, @is_preconnected.intermediate_value_Ioc _ _ _ _ _ _ _ (is_preconnected_Ico)
_ _ ⟨refl a, hlt⟩ (right_nhds_within_Ico_ne_bot hlt) inf_le_right _ (hf.mono Ico_subset_Icc_self)
_ ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ico_subset_Icc_self))
lemma intermediate_value_Ioc {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ioc (f a) (f b) ⊆ f '' (Ioc a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.2 (not_le_of_lt (he ▸ h.1)))
(λ hlt, @is_preconnected.intermediate_value_Ioc _ _ _ _ _ _ _ (is_preconnected_Ioc)
_ _ ⟨hlt, refl b⟩ (left_nhds_within_Ioc_ne_bot hlt) inf_le_right _ (hf.mono Ioc_subset_Icc_self)
_ ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioc_subset_Icc_self))
lemma intermediate_value_Ioc' {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ico (f b) (f a) ⊆ f '' (Ioc a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.1 (not_le_of_lt (he ▸ h.2)))
(λ hlt, @is_preconnected.intermediate_value_Ico _ _ _ _ _ _ _ (is_preconnected_Ioc)
_ _ ⟨hlt, refl b⟩ (left_nhds_within_Ioc_ne_bot hlt) inf_le_right _ (hf.mono Ioc_subset_Icc_self)
_ ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioc_subset_Icc_self))
lemma intermediate_value_Ioo {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ioo (f a) (f b) ⊆ f '' (Ioo a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.2 (not_lt_of_lt (he ▸ h.1)))
(λ hlt, @is_preconnected.intermediate_value_Ioo _ _ _ _ _ _ _ (is_preconnected_Ioo)
_ _ (left_nhds_within_Ioo_ne_bot hlt) (right_nhds_within_Ioo_ne_bot hlt)
inf_le_right inf_le_right _ (hf.mono Ioo_subset_Icc_self)
_ _ ((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioo_subset_Icc_self)
((hf.continuous_within_at ⟨hab, refl b⟩).mono Ioo_subset_Icc_self))
lemma intermediate_value_Ioo' {a b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (Icc a b)) :
Ioo (f b) (f a) ⊆ f '' (Ioo a b) :=
or.elim (eq_or_lt_of_le hab) (λ he y h, absurd h.1 (not_lt_of_lt (he ▸ h.2)))
(λ hlt, @is_preconnected.intermediate_value_Ioo _ _ _ _ _ _ _ (is_preconnected_Ioo)
_ _ (right_nhds_within_Ioo_ne_bot hlt) (left_nhds_within_Ioo_ne_bot hlt)
inf_le_right inf_le_right _ (hf.mono Ioo_subset_Icc_self)
_ _ ((hf.continuous_within_at ⟨hab, refl b⟩).mono Ioo_subset_Icc_self)
((hf.continuous_within_at ⟨refl a, hab⟩).mono Ioo_subset_Icc_self))
/-- **Intermediate value theorem**: if `f` is continuous on an order-connected set `s` and `a`,
`b` are two points of this set, then `f` sends `s` to a superset of `Icc (f x) (f y)`. -/
lemma continuous_on.surj_on_Icc {s : set α} [hs : ord_connected s] {f : α → δ}
(hf : continuous_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) :
surj_on f s (Icc (f a) (f b)) :=
hs.is_preconnected.intermediate_value ha hb hf
/-- **Intermediate value theorem**: if `f` is continuous on an order-connected set `s` and `a`,
`b` are two points of this set, then `f` sends `s` to a superset of `[f x, f y]`. -/
lemma continuous_on.surj_on_interval {s : set α} [hs : ord_connected s] {f : α → δ}
(hf : continuous_on f s) {a b : α} (ha : a ∈ s) (hb : b ∈ s) :
surj_on f s (interval (f a) (f b)) :=
by cases le_total (f a) (f b) with hab hab; simp [hf.surj_on_Icc, *]
/-- A continuous function which tendsto `at_top` `at_top` and to `at_bot` `at_bot` is surjective. -/
lemma continuous.surjective {f : α → δ} (hf : continuous f) (h_top : tendsto f at_top at_top)
(h_bot : tendsto f at_bot at_bot) :
function.surjective f :=
λ p, mem_range_of_exists_le_of_exists_ge hf
(h_bot.eventually (eventually_le_at_bot p)).exists
(h_top.eventually (eventually_ge_at_top p)).exists
/-- A continuous function which tendsto `at_bot` `at_top` and to `at_top` `at_bot` is surjective. -/
lemma continuous.surjective' {f : α → δ} (hf : continuous f) (h_top : tendsto f at_bot at_top)
(h_bot : tendsto f at_top at_bot) :
function.surjective f :=
@continuous.surjective αᵒᵈ _ _ _ _ _ _ _ _ _ hf h_top h_bot
/-- If a function `f : α → β` is continuous on a nonempty interval `s`, its restriction to `s`
tends to `at_bot : filter β` along `at_bot : filter ↥s` and tends to `at_top : filter β` along
`at_top : filter ↥s`, then the restriction of `f` to `s` is surjective. We formulate the
conclusion as `surj_on f s univ`. -/
lemma continuous_on.surj_on_of_tendsto {f : α → δ} {s : set α} [ord_connected s]
(hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (λ x : s, f x) at_bot at_bot)
(htop : tendsto (λ x : s, f x) at_top at_top) :
surj_on f s univ :=
by haveI := classical.inhabited_of_nonempty hs.to_subtype;
exact (surj_on_iff_surjective.2 $
(continuous_on_iff_continuous_restrict.1 hf).surjective htop hbot)
/-- If a function `f : α → β` is continuous on a nonempty interval `s`, its restriction to `s`
tends to `at_top : filter β` along `at_bot : filter ↥s` and tends to `at_bot : filter β` along
`at_top : filter ↥s`, then the restriction of `f` to `s` is surjective. We formulate the
conclusion as `surj_on f s univ`. -/
lemma continuous_on.surj_on_of_tendsto' {f : α → δ} {s : set α} [ord_connected s]
(hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (λ x : s, f x) at_bot at_top)
(htop : tendsto (λ x : s, f x) at_top at_bot) :
surj_on f s univ :=
@continuous_on.surj_on_of_tendsto α _ _ _ _ δᵒᵈ _ _ _ _ _ _ hs hf hbot htop
|
3506c78c9a2ecf44918455be07b9f9160ed4adc0 | d31b9f832ff922a603f76cf32e0f3aa822640508 | /src/hott/types/pointed.lean | 6c58c65146d8ff59dff2000ae09790856d3cafa4 | [
"Apache-2.0"
] | permissive | javra/hott3 | 6e7a9e72a991a2fae32e5764982e521dca617b16 | cd51f2ab2aa48c1246a188f9b525b30f76c3d651 | refs/heads/master | 1,585,819,679,148 | 1,531,232,382,000 | 1,536,682,965,000 | 154,294,022 | 0 | 0 | Apache-2.0 | 1,540,284,376,000 | 1,540,284,375,000 | null | UTF-8 | Lean | false | false | 49,005 | lean | /-
Copyright (c) 2014-2016 Jakob von Raumer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jakob von Raumer, Floris van Doorn
Early library ported from Coq HoTT, but greatly extended since.
The basic definitions are in init.pointed
See also .pointed2
-/
/-.nat.basic-/ /-..prop_trunc-/
import ..arity ..prop_trunc .bool
universes u u₁ u₂ u₃ u₄
--open eq prod sigma equiv option is_equiv bool unit sigma.ops sum algebra function
namespace hott
hott_theory
open is_trunc nat hott.bool hott.is_equiv hott.equiv hott.sigma
namespace pointed
variables {A : Type _} {B : Type _}
@[hott, instance] def pointed_loop (a : A) : pointed (a = a) :=
pointed.mk idp
@[hott] def pointed_fun_closed (f : A → B) [H : pointed A] : pointed B :=
pointed.mk (f pt)
@[hott, reducible] def loop (A : Type*) : Type* :=
pointed.mk' (point A = point A)
@[hott, reducible] def loopn : ℕ → Type* → Type*
| 0 X := X
| (n+1) X := loop (loopn n X)
notation `Ω` := loop
notation `Ω[`:95 n:0 `]`:0 := loopn n
@[hott] def is_trunc_pointed_MK (n : ℕ₋₂) {A : Type _} (a : A)
[H : is_trunc n A] : is_trunc n (pointed.MK A a) :=
H
@[hott, instance, priority 1100] def is_trunc_loop (A : Type*)
(n : ℕ₋₂) [H : is_trunc (n.+1) A] : is_trunc n (Ω A) :=
is_trunc_eq _ _ _
@[hott] def loopn_zero_eq (A : Type*)
: Ω[0] A = A := rfl
@[hott] def loopn_succ_eq (k : ℕ) (A : Type*)
: Ω[succ k] A = Ω (Ω[k] A) := rfl
@[hott,reducible] def rfln {n : ℕ} {A : Type*} : Ω[n] A := pt
@[hott,reducible] def refln (n : ℕ) (A : Type*) : Ω[n] A := Point _
@[hott] def refln_eq_refl (A : Type*) (n : ℕ) : rfln = rfl :> Ω[succ n] A := rfl
@[hott] def loopn_space (A : Type _) [H : pointed A] (n : ℕ) : Type _ :=
Ω[n] (pointed.mk' A)
@[hott] def loop_mul {k : ℕ} {A : Type*} (mul : A → A → A) : Ω[k] A → Ω[k] A → Ω[k] A :=
begin cases k with k, exact mul, exact concat end
@[hott] def pType_eq {A B : Type*} (f : A ≃ B) (p : f pt = pt) : A = B :=
begin
cases A with A a, cases B with B b, dsimp at f p,
fapply apdt011 @pType.mk,
{ apply ua f },
{ rwr [←cast_def, cast_ua, p] },
end
@[hott] def pType_eq_elim {A B : Type*} (p : A = B :> Type*)
: Σ(p : carrier A = carrier B :> Type _), Point A =[p; λX, X] Point B :=
by induction p; exact ⟨idp, idpo⟩
@[hott] protected def pType.sigma_char : pType.{u} ≃ Σ(X : Type u), X :=
begin
fapply equiv.MK,
{ intro x, induction x with X x, exact ⟨X, x⟩},
{ intro x, induction x with X x, exact pointed.MK X x},
{ intro x, induction x with X x, reflexivity},
{ intro x, induction x with X x, reflexivity},
end
@[hott] def pType.eta_expand (A : Type*) : Type* :=
pointed.MK A pt
@[hott] def add_point (A : Type _) : Type* :=
pointed.Mk (none : option A)
postfix `₊`:(max+1) := add_point
-- the inclusion A → A₊ is called "some", the extra point "pt" or "none" ("@none A")
end pointed
namespace pointed
/- truncated pointed types -/
@[hott] def ptrunctype_eq {n : ℕ₋₂} {A B : n-Type*}
(p : A = B :> Type _) (q : Point (A.to_pType) =[p; λX, X] Point (B.to_pType)) : A = B :=
begin
induction A with A HA a, induction B with B HB b, dsimp at p q,
induction q,
refine @ap010 _ (is_trunc n A) _ _ _ (ptrunctype.mk A) _ a,
exact is_prop.elim _ _
end
@[hott] def ptrunctype_eq_of_pType_eq {n : ℕ₋₂} {A B : n-Type*} (p : A.to_pType = B.to_pType)
: A = B :=
begin
cases pType_eq_elim p with q r,
exact ptrunctype_eq q r
end
@[hott, instance] def is_trunc_ptrunctype {n : ℕ₋₂} (A : n-Type*) : is_trunc n A :=
trunctype.struct A
end pointed open pointed
namespace pointed
variables {A : pType.{u₁}} {B : pType.{u₂}} {C : pType.{u₃}} {D : pType.{u₄}}
{f g h : A →* B} {P : A → Type _} {p₀ : P pt} {k k' l m : ppi P p₀}
/- categorical properties of pointed maps -/
@[hott, refl] def pid (A : Type*) : A →* A :=
pmap.mk id idp
@[hott, trans] def pcompose {A B C : Type*} (g : B →* C) (f : A →* B) : A →* C :=
pmap.mk (λa, g (f a)) (ap g (respect_pt f) ⬝ respect_pt g)
infixr ` ∘* `:60 := pcompose
@[hott] def pmap_of_map {A B : Type _} (f : A → B) (a : A) :
pointed.MK A a →* pointed.MK B (f a) :=
pmap.mk f idp
@[hott, hsimp] def respect_pt_pcompose {A B C : Type*} (g : B →* C) (f : A →* B)
: respect_pt (g ∘* f) = ap g (respect_pt f) ⬝ respect_pt g :=
idp
@[hott] def passoc (h : C →* D) (g : B →* C) (f : A →* B) : (h ∘* g) ∘* f ~* h ∘* (g ∘* f) :=
phomotopy.mk (λa, idp)
begin abstract {
refine (idp_con _ ⬝ whisker_right _ (ap_con _ _ _ ⬝ whisker_right _ _) ⬝ (con.assoc _ _ _)),
exact ap_compose' h g (respect_pt f)
} end
@[hott] def pid_pcompose (f : A →* B) : pid B ∘* f ~* f :=
begin
fapply phomotopy.mk,
{ intro a, reflexivity},
{ reflexivity}
end
@[hott] def pcompose_pid (f : A →* B) : f ∘* pid A ~* f :=
begin
fapply phomotopy.mk,
{ intro a, reflexivity},
{ reflexivity}
end
/- equivalences and equalities -/
@[hott] protected def ppi.sigma_char {A : Type*} (B : A → Type _) (b₀ : B pt) :
ppi B b₀ ≃ Σ(k : Πa, B a), k pt = b₀ :=
begin
fapply equiv.MK; all_goals {intro x},
{ constructor, exact respect_pt x },
{ induction x with f p, constructor, exact p },
{ induction x, reflexivity },
{ induction x, reflexivity }
end
@[hott] def pmap.sigma_char {A B : Type*} : (A →* B) ≃ Σ(f : A → B), f pt = pt :=
ppi.sigma_char _ _
@[hott] def pmap.eta_expand {A B : Type*} (f : A →* B) : A →* B :=
pmap.mk f (respect_pt f)
@[hott] def pmap_equiv_right (A : Type*) (B : Type _)
: (Σ(b : B), A →* (pointed.Mk b)) ≃ (A → B) :=
begin
fapply equiv.MK,
{ intros u a, exact pmap.to_fun u.2 a},
{ intro f, refine ⟨f pt, _⟩, fapply pmap.mk,
intro a, exact f a,
reflexivity},
{ intro f, reflexivity},
{ intro u, cases u with b f, cases f with f p, dsimp at f p, induction p,
reflexivity}
end
/- some specific pointed maps -/
-- The constant pointed map between any two types
@[hott] def pconst (A B : Type*) : A →* B :=
ppi_const _
-- the pointed type of pointed maps -- TODO: remove
@[hott] def ppmap (A B : Type*) : Type* :=
@pppi A (λa, B)
@[hott] def pcast {A B : Type*} (p : A = B) : A →* B :=
pmap.mk (cast (ap pType.carrier p)) (by induction p; reflexivity)
@[hott] def pinverse (X : Type*) : Ω X →* Ω X :=
pmap.mk eq.inverse idp
/-
we generalize the @[hott] def of ap1 to arbitrary paths, so that we can prove properties about it
using path induction (see for example ap1_gen_con and ap1_gen_con_natural)
-/
@[hott, reducible] def ap1_gen {A B : Type _} (f : A → B) {a a' : A}
{b b' : B} (q : f a = b) (q' : f a' = b') (p : a = a') : b = b' :=
q⁻¹ ⬝ ap f p ⬝ q'
@[hott] def ap1_gen_idp {A B : Type _} (f : A → B) {a : A} {b : B} (q : f a = b) :
ap1_gen f q q idp = idp :=
con.left_inv q
@[hott] def ap1_gen_idp_left {A B : Type _} (f : A → B) {a a' : A} (p : a = a') :
ap1_gen f idp idp p = ap f p :=
idp_con (ap f p)
@[hott] def ap1_gen_idp_left_con {A B : Type _} (f : A → B) {a : A} (p : a = a) (q : ap f p = idp) :
ap1_gen_idp_left f p ⬝ q = ap (concat idp) q :=
idp_con_idp q
@[hott] def ap1 (f : A →* B) : Ω A →* Ω B :=
pmap.mk (λp, ap1_gen f (respect_pt f) (respect_pt f) p) (ap1_gen_idp f (respect_pt f))
@[hott] def apn (n : ℕ) (f : A →* B) : Ω[n] A →* Ω[n] B :=
begin
induction n with n IH,
{ exact f },
{ exact ap1 IH }
end
notation `Ω→`:(max+5) := ap1
notation `Ω→[`:95 n:0 `]`:0 := apn n
@[hott] def ptransport {A : Type _} (B : A → Type*) {a a' : A} (p : a = a')
: B a →* B a' :=
pmap.mk (transport _ p) (apdt (λa, Point (B a)) p)
@[hott] def pmap_of_eq_pt {A : Type _} {a a' : A} (p : a = a') :
pointed.MK A a →* pointed.MK A a' :=
pmap.mk id p
@[hott] def pbool_pmap {A : Type*} (a : A) : pbool →* A :=
pmap.mk (λb, bool.rec pt a b) idp
/- properties of pointed maps -/
@[hott] def apn_zero (f : A →* B) : Ω→[0] f = f := idp
@[hott] def apn_succ (n : ℕ) (f : A →* B) : Ω→[n + 1] f = Ω→ (Ω→[n] f) := idp
@[hott] def ap1_gen_con {A B : Type _} (f : A → B) {a₁ a₂ a₃ : A} {b₁ b₂ b₃ : B}
(q₁ : f a₁ = b₁) (q₂ : f a₂ = b₂) (q₃ : f a₃ = b₃) (p₁ : a₁ = a₂) (p₂ : a₂ = a₃) :
ap1_gen f q₁ q₃ (p₁ ⬝ p₂) = ap1_gen f q₁ q₂ p₁ ⬝ ap1_gen f q₂ q₃ p₂ :=
begin induction p₂, induction q₃, induction q₂, reflexivity end
@[hott] def ap1_gen_inv {A B : Type _} (f : A → B) {a₁ a₂ : A}
{b₁ b₂ : B} (q₁ : f a₁ = b₁) (q₂ : f a₂ = b₂) (p₁ : a₁ = a₂) :
ap1_gen f q₂ q₁ p₁⁻¹ = (ap1_gen f q₁ q₂ p₁)⁻¹ :=
begin induction p₁, induction q₁, induction q₂, reflexivity end
@[hott] def ap1_con {A B : Type*} (f : A →* B) (p q : Ω A) : ap1 f (p ⬝ q) = ap1 f p ⬝ ap1 f q :=
ap1_gen_con f (respect_pt f) (respect_pt f) (respect_pt f) p q
@[hott] def ap1_inv (f : A →* B) (p : Ω A) : ap1 f p⁻¹ = (ap1 f p)⁻¹ :=
ap1_gen_inv f (respect_pt f) (respect_pt f) p
-- the following two facts are used for the suspension axiom to define spectrum cohomology
@[hott] def ap1_gen_con_natural {A B : Type _} (f : A → B) {a₁ a₂ a₃ : A} {p₁ p₁' : a₁ = a₂}
{p₂ p₂' : a₂ = a₃}
{b₁ b₂ b₃ : B} (q₁ : f a₁ = b₁) (q₂ : f a₂ = b₂) (q₃ : f a₃ = b₃)
(r₁ : p₁ = p₁') (r₂ : p₂ = p₂') :
square (ap1_gen_con f q₁ q₂ q₃ p₁ p₂)
(ap1_gen_con f q₁ q₂ q₃ p₁' p₂')
(ap (ap1_gen f q₁ q₃) (r₁ ◾ r₂))
(ap (ap1_gen f q₁ q₂) r₁ ◾ ap (ap1_gen f q₂ q₃) r₂) :=
begin induction r₁, induction r₂, exact vrfl end
@[hott] def ap1_gen_con_idp {A B : Type _} (f : A → B) {a : A} {b : B} (q : f a = b) :
ap1_gen_con f q q q idp idp ⬝ con.left_inv q ◾ con.left_inv q = con.left_inv q :=
by induction q; reflexivity
@[hott] def apn_con (n : ℕ) (f : A →* B) (p q : Ω[succ n] A)
: (Ω→[succ n] f) (p ⬝ q) = (Ω→[succ n] f) p ⬝ (Ω→[succ n] f) q :=
ap1_con (Ω→[n] f) p q
@[hott] def apn_inv (n : ℕ) (f : A →* B) (p : Ω[succ n] A) :
Ω→[succ n] f p⁻¹ᵖ = (Ω→[succ n] f p)⁻¹ᵖ :=
ap1_inv (Ω→[n] f) p
@[hott] def is_equiv_ap1 (f : A →* B) [H : is_equiv f] : is_equiv (ap1 f) :=
begin
unfreezeI, induction B with B b, induction f with f pf, dsimp at f pf H, induction pf,
apply is_equiv.homotopy_closed (ap f),
introI p, exact (idp_con _)⁻¹, apply_instance
end
@[hott] def is_equiv_apn (n : ℕ) (f : A →* B) [H : is_equiv f]
: is_equiv (Ω→[n] f) :=
begin
induction n with n IH,
{ exact H },
{ exact @is_equiv_ap1 _ _ (Ω→[n] f) IH }
end
@[hott] def pinverse_con {X : Type*} (p q : Ω X)
: pinverse X (p ⬝ q) = pinverse X q ⬝ pinverse X p :=
con_inv p q
@[hott] def pinverse_inv {X : Type*} (p : Ω X)
: pinverse X p⁻¹ = (pinverse X p)⁻¹ :=
idp
@[hott] def ap1_pcompose_pinverse {X Y : Type*} (f : X →* Y) :
Ω→ f ∘* pinverse X ~* pinverse Y ∘* Ω→ f :=
phomotopy.mk (ap1_gen_inv f (respect_pt f) (respect_pt f))
begin
induction Y with Y y₀, induction f with f f₀, dsimp at f f₀, induction f₀,
refl
end
@[hott, instance] def is_equiv_pcast {A B : Type*} (p : A = B) : is_equiv (pcast p) :=
is_equiv_cast _
/- categorical properties of pointed homotopies -/
variable (k)
@[hott] protected def phomotopy.refl : k ~* k :=
phomotopy.mk homotopy.rfl (idp_con _)
variable {k}
@[hott, reducible, refl] protected def phomotopy.rfl : k ~* k :=
phomotopy.refl k
@[hott, symm] protected def phomotopy.symm (p : k ~* l) : l ~* k :=
phomotopy.mk p⁻¹ʰᵗʸ (inv_con_eq_of_eq_con (to_homotopy_pt p)⁻¹)
@[hott, trans] protected def phomotopy.trans (p : k ~* l) (q : l ~* m) :
k ~* m :=
phomotopy.mk (λa, p a ⬝ q a) (con.assoc _ _ _ ⬝ whisker_left (p pt) (to_homotopy_pt q) ⬝ to_homotopy_pt p)
infix ` ⬝* `:75 := phomotopy.trans
postfix `⁻¹*`:(max+1) := phomotopy.symm
/- equalities and equivalences relating pointed homotopies -/
@[hott, reducible, elab_as_eliminator] def phomotopy.rec' (B : k ~* l → Type _)
(H : Π(h : k ~ l) (p : h pt ⬝ respect_pt l = respect_pt k), B (phomotopy.mk h p))
(h : k ~* l) : B h :=
begin
induction h with h p,
refine transport (λp, B (ppi.mk h p)) _ (H h (con_eq_of_eq_con_inv p)),
apply (eq_con_inv_equiv_con_eq _ _ _).to_left_inv p
end
@[hott] def phomotopy.eta_expand (p : k ~* l) : k ~* l :=
phomotopy.mk p (to_homotopy_pt p)
@[hott, instance] def is_trunc_ppi (n : ℕ₋₂) {A : Type*} (B : A → Type _) (b₀ : B pt) [Πa, is_trunc n (B a)] :
is_trunc n (ppi B b₀) :=
is_trunc_equiv_closed_rev _ (ppi.sigma_char _ _) (by infer)
@[hott, instance] def is_trunc_pmap (n : ℕ₋₂) (A B : Type*) [is_trunc n B] :
is_trunc n (A →* B) :=
is_trunc_ppi _ _ _
@[hott, instance] def is_trunc_ppmap (n : ℕ₋₂) {A B : Type*} [is_trunc n B] :
is_trunc n (ppmap A B) :=
is_trunc_pmap _ _ _
@[hott] def phomotopy_of_eq (p : k = l) : k ~* l :=
phomotopy.mk (ap010 ppi.to_fun p) begin induction p, refine !idp_con end
@[hott] def phomotopy_of_eq_idp (k : ppi P p₀) : phomotopy_of_eq idp = phomotopy.refl k :=
idp
@[hott] def pconcat_eq (p : k ~* l) (q : l = m) : k ~* m :=
p ⬝* phomotopy_of_eq q
@[hott] def eq_pconcat (p : k = l) (q : l ~* m) : k ~* m :=
phomotopy_of_eq p ⬝* q
infix ` ⬝*p `:75 := pconcat_eq
infix ` ⬝p* `:75 := eq_pconcat
@[hott] def fst_phomotopy_eq {p q : k ~* l} (r : p = q) (a : A) : p a = q a :=
ap010 to_homotopy r a
@[hott] def pwhisker_left (h : B →* C) (p : f ~* g) : h ∘* f ~* h ∘* g :=
phomotopy.mk (λa, ap h (p a))
begin abstract {exact con.assoc' _ _ _ ⬝ whisker_right _ ((ap_con _ _ _)⁻¹ ⬝ ap02 _ (to_homotopy_pt p))} end
@[hott] def pwhisker_right (h : C →* A) (p : f ~* g) : f ∘* h ~* g ∘* h :=
phomotopy.mk (λc, p (h c))
(by abstract {exact con.assoc' _ _ _ ⬝ whisker_right _ (ap_con_eq_con_ap _ _)⁻¹ ⬝
con.assoc _ _ _ ⬝ whisker_left _ (to_homotopy_pt p)})
@[hott] def pconcat2 {A B C : Type*} {h i : B →* C} {f g : A →* B}
(q : h ~* i) (p : f ~* g) : h ∘* f ~* i ∘* g :=
pwhisker_left _ p ⬝* pwhisker_right _ q
variables (k l)
@[hott] def phomotopy.sigma_char
: (k ~* l) ≃ Σ(p : k ~ l), p pt ⬝ respect_pt l = respect_pt k :=
begin
fapply equiv.MK, all_goals {intros h},
{ exact ⟨h , to_homotopy_pt h⟩ },
{ cases h with h p, exact phomotopy.mk h p },
{ cases h with h p, exact ap (dpair h) ((eq_con_inv_equiv_con_eq _ _ _).to_right_inv p) },
{ refine phomotopy.rec' _ _ h, clear h, intros h p,
exact (ap (phomotopy.mk h) $ (eq_con_inv_equiv_con_eq _ _ _).to_right_inv p) }
end
@[hott] def ppi_eq_equiv_internal : (k = l) ≃ (k ~* l) :=
calc (k = l) ≃ ppi.sigma_char P p₀ k = ppi.sigma_char P p₀ l
: eq_equiv_fn_eq (ppi.sigma_char P p₀) k l
... ≃ Σ(p : k = l :> Πa, P a),
respect_pt k =[p; λ(h : Πa, P a), h pt = p₀] respect_pt l
: sigma_eq_equiv _ _
... ≃ Σ(p : k = l :> Πa, P a),
respect_pt k = ap (λ(h : Πa, P a), h pt) p ⬝ respect_pt l
: sigma_equiv_sigma_right
(λp, eq_pathover_equiv_Fl p (respect_pt k) (respect_pt l))
... ≃ Σ(p : k = l :> Πa, P a),
respect_pt k = apd10 p pt ⬝ respect_pt l
: sigma_equiv_sigma_right
(λp, equiv_eq_closed_right _ (whisker_right _ (ap_eq_apd10 p _)))
... ≃ Σ(p : k ~ l), respect_pt k = p pt ⬝ respect_pt l
: sigma_equiv_sigma_left' (λ(p : k ~ l), respect_pt k = p pt ⬝ respect_pt l) (eq_equiv_homotopy k l)
... ≃ Σ(p : k ~ l), p pt ⬝ respect_pt l = respect_pt k
: sigma_equiv_sigma_right (λp, eq_equiv_eq_symm _ _)
... ≃ (k ~* l) : (phomotopy.sigma_char k l)⁻¹ᵉ
@[hott] def ppi_eq_equiv_internal_idp :
ppi_eq_equiv_internal k k idp = phomotopy.refl k :=
begin
--apply ap (phomotopy.mk (homotopy.refl _)), /- do we need this? -/
induction k with k k₀,
induction k₀, reflexivity
end
@[hott] def ppi_eq_equiv : (k = l) ≃ (k ~* l) :=
begin
refine equiv_change_fun (ppi_eq_equiv_internal k l) _,
{ apply phomotopy_of_eq },
{ intro p, induction p, exact ppi_eq_equiv_internal_idp k }
end
variables {k l}
@[hott] def pmap_eq_equiv (f g : A →* B) : (f = g) ≃ (f ~* g) :=
ppi_eq_equiv f g
@[hott] def eq_of_phomotopy (p : k ~* l) : k = l :=
to_inv (ppi_eq_equiv k l) p
@[hott] def eq_of_phomotopy_refl (k : ppi P p₀) : eq_of_phomotopy (phomotopy.refl k) = idpath k :=
begin
apply to_inv_eq_of_eq, reflexivity
end
@[hott] def phomotopy_of_homotopy (h : k ~ l) [Πa, is_set (P a)] : k ~* l :=
begin
fapply phomotopy.mk,
{ exact h },
{ apply is_set.elim }
end
@[hott] def ppi_eq_of_homotopy [Πa, is_set (P a)] (p : k ~ l) : k = l :=
eq_of_phomotopy (phomotopy_of_homotopy p)
@[hott] def pmap_eq_of_homotopy [is_set B] (p : f ~ g) : f = g :=
ppi_eq_of_homotopy p
@[hott] def phomotopy_of_eq_of_phomotopy (p : k ~* l) : phomotopy_of_eq (eq_of_phomotopy p) = p :=
to_right_inv (ppi_eq_equiv k l) p
@[hott, induction, reducible] def phomotopy_rec_eq {Q : (k ~* k') → Type _} (p : k ~* k')
(H : Π(q : k = k'), Q (phomotopy_of_eq q)) : Q p :=
phomotopy_of_eq_of_phomotopy p ▸ H (eq_of_phomotopy p)
@[hott, induction, reducible] def phomotopy_rec_idp {Q : Π {k' : ppi P p₀}, (k ~* k') → Type _}
{k' : ppi P p₀} (H : k ~* k') (q : Q (phomotopy.refl k)) : Q H :=
begin
hinduction H using phomotopy_rec_eq with t,
induction t, exact phomotopy_of_eq_idp k ▸ q,
end
@[hott] def phomotopy_rec_idp' (Q : Π ⦃k' : ppi P p₀⦄, (k ~* k') → (k = k') → Type _)
(q : Q phomotopy.rfl idp) ⦃k' : ppi P p₀⦄ (H : k ~* k') : Q H (eq_of_phomotopy H) :=
begin
hinduction H using phomotopy_rec_idp,
exact transport (Q phomotopy.rfl) (eq_of_phomotopy_refl _)⁻¹ q
end
@[hott] theorem phomotopy_rec_eq_phomotopy_of_eq {Q : (k ~* l) → Type _} (p : k = l)
(H : Π(q : k = l), Q (phomotopy_of_eq q)) : phomotopy_rec_eq (phomotopy_of_eq p) H = H p :=
begin
refine transport2 _ (adj (ppi_eq_equiv _ _).to_fun _) _ ⬝ _,
refine tr_ap _ _ _ _ ⬝ _,
apply apdt
end
@[hott] def phomotopy_rec_idp_refl {Q : Π{l}, (k ~* l) → Type _} (H : Q (phomotopy.refl k)) :
phomotopy_rec_idp phomotopy.rfl H = H :=
begin
apply phomotopy_rec_eq_phomotopy_of_eq idp
end
@[hott] def phomotopy_rec_idp'_refl (Q : Π ⦃k' : ppi P p₀⦄, (k ~* k') → (k = k') → Type _)
(q : Q phomotopy.rfl idp) :
phomotopy_rec_idp' Q q phomotopy.rfl = transport (Q phomotopy.rfl) (eq_of_phomotopy_refl _)⁻¹ q :=
begin dsimp [phomotopy_rec_idp'], exact phomotopy_rec_idp_refl _ end
/- maps out of or into contractible types -/
@[hott] def phomotopy_of_is_contr_cod (k l : ppi P p₀) [Πa, is_contr (P a)] :
k ~* l :=
phomotopy.mk (λa, eq_of_is_contr _ _) (eq_of_is_contr _ _)
@[hott] def phomotopy_of_is_contr_cod_pmap (f g : A →* B) [is_contr B] : f ~* g :=
phomotopy_of_is_contr_cod f g
@[hott] def phomotopy_of_is_contr_dom (k l : ppi P p₀) [is_contr A] : k ~* l :=
begin
fapply phomotopy.mk,
{ hintro a, exact eq_of_pathover_idp (change_path (is_prop.elim _ _)
(apd k (is_prop.elim _ _) ⬝op respect_pt k ⬝ (respect_pt l)⁻¹ ⬝o apd l (is_prop.elim _ _))) },
dsimp, rwr [is_prop_elim_self],
dsimp, rwr [is_prop_elim_self, apd_idp, apd_idp],
dsimp, rwr [idpo_concato_eq, inv_con_cancel_right],
end
/- adjunction between (-)₊ : Type _ → Type* and pType.carrier : Type* → Type _ -/
@[hott] def pmap_equiv_left (A : Type _) (B : Type*) : A₊ →* B ≃ (A → B) :=
begin
fapply equiv.MK,
{ intros f a, cases f with f p, exact f (some a) },
{ intro f, fconstructor,
intro a, cases a, exact pt, exact f a,
reflexivity },
{ intro f, reflexivity },
{ intro f, cases f with f p, fapply eq_of_phomotopy, fapply phomotopy.mk,
{ intro a, cases a, exact p⁻¹, refl },
{ apply con.left_inv }},
end
-- pmap_pbool_pequiv is the pointed equivalence
@[hott] def pmap_pbool_equiv (B : Type*) : (pbool →* B) ≃ B :=
begin
fapply equiv.MK,
{ intro f, cases f with f p, exact f tt },
{ intro b, fconstructor,
intro u, cases u, exact pt, exact b,
reflexivity },
{ intro b, reflexivity },
{ intro f, cases f with f p, fapply eq_of_phomotopy, fapply phomotopy.mk,
{ intro a, cases a, exact p⁻¹, refl },
{ apply con.left_inv }},
end
/-
Pointed maps respecting pointed homotopies.
In general we need function extensionality for pap,
but for particular F we can do it without function extensionality.
This might be preferred, because such pointed homotopies compute. On the other hand,
when using function extensionality, it's easier to prove that if p is reflexivity, then the
resulting pointed homotopy is reflexivity
-/
@[hott] def pap (F : (A →* B) → (C →* D)) {f g : A →* B} (p : f ~* g) : F f ~* F g :=
begin
hinduction p using phomotopy_rec_idp, refl
end
@[hott] def pap_refl (F : (A →* B) → (C →* D)) (f : A →* B) :
pap F (phomotopy.refl f) = phomotopy.refl (F f) :=
begin dsimp [pap], exact phomotopy_rec_idp_refl _ end
@[hott] def ap1_phomotopy {f g : A →* B} (p : f ~* g) : Ω→ f ~* Ω→ g :=
pap Ω→ p
@[hott] def ap1_phomotopy_refl {X Y : Type*} (f : X →* Y) :
ap1_phomotopy (phomotopy.refl f) = phomotopy.refl (Ω→ f) :=
pap_refl _ _
--a proof not using function extensionality:
@[hott] def ap1_phomotopy_explicit {f g : A →* B} (p : f ~* g) : Ω→ f ~* Ω→ g :=
begin
induction p with p q, induction f with f pf, induction g with g pg, induction B with B b,
dsimp at *, induction pg, dsimp [respect_pt] at *, induction q,
fapply phomotopy.mk,
{ hintro l, refine _ ⬝ (idp_con _)⁻¹, refine con.assoc _ _ _ ⬝ _,
exact inv_con_eq_of_eq_con (ap_con_eq_con_ap p l) },
{ induction A with A a,
dsimp [respect_pt, point, ap_con_eq_con_ap, ap1, pmap.mk, pppi.mk, ap1_gen_idp],
hgeneralize : p a = q, revert q, clear p,
hgeneralize : g a = b', intro q,
induction q, reflexivity }
end
@[hott] def apn_phomotopy {f g : A →* B} (n : ℕ) (p : f ~* g) : apn n f ~* apn n g :=
begin
induction n with n IH,
{ exact p},
{ exact ap1_phomotopy IH}
end
-- the following two definitiongs are mostly the same, maybe we should remove one
@[hott] def ap_eq_of_phomotopy {A B : Type*} {f g : A →* B} (p : f ~* g) (a : A) :
ap (λf : A →* B, f a) (eq_of_phomotopy p) = p a :=
ap010 to_homotopy (phomotopy_of_eq_of_phomotopy p) a
@[hott] def to_fun_eq_of_phomotopy {A B : Type*} {f g : A →* B} (p : f ~* g) (a : A) :
ap010 pmap.to_fun (eq_of_phomotopy p) a = p a :=
begin
hinduction p using phomotopy_rec_idp,
exact ap (λx, ap010 pmap.to_fun x a) (eq_of_phomotopy_refl _)
end
@[hott] def ap1_eq_of_phomotopy {A B : Type*} {f g : A →* B} (p : f ~* g) :
ap Ω→ (eq_of_phomotopy p) = eq_of_phomotopy (ap1_phomotopy p) :=
begin
hinduction p using phomotopy_rec_idp,
refine ap02 _ (eq_of_phomotopy_refl _) ⬝ (eq_of_phomotopy_refl _)⁻¹ ⬝ ap eq_of_phomotopy _,
exact (ap1_phomotopy_refl _)⁻¹
end
/- pointed homotopies between the given pointed maps -/
@[hott] def ap1_pid {A : Type*} : ap1 (pid A) ~* pid (Ω A) :=
begin
fapply phomotopy.mk,
{ intro p, refine idp_con _ ⬝ ap_id _ },
{ refl }
end
@[hott] def ap1_pinverse {A : Type*} : ap1 (@pinverse A) ~* @pinverse (Ω A) :=
begin
fapply phomotopy.mk,
{ intro p, refine idp_con _ ⬝ _, exact (inv_eq_inv2 _)⁻¹ },
{ refl }
end
@[hott] def ap1_gen_compose {A B C : Type _} (g : B → C) (f : A → B) {a₁ a₂ : A} {b₁ b₂ : B}
{c₁ c₂ : C} (q₁ : f a₁ = b₁) (q₂ : f a₂ = b₂) (r₁ : g b₁ = c₁) (r₂ : g b₂ = c₂) (p : a₁ = a₂) :
ap1_gen (g ∘ f) (ap g q₁ ⬝ r₁) (ap g q₂ ⬝ r₂) p = ap1_gen g r₁ r₂ (ap1_gen f q₁ q₂ p) :=
begin induction p, induction q₁, induction q₂, induction r₁, induction r₂, reflexivity end
@[hott] def ap1_gen_compose_idp {A B C : Type _} (g : B → C) (f : A → B) {a : A}
{b : B} {c : C} (q : f a = b) (r : g b = c) :
ap1_gen_compose g f q q r r idp ⬝ (ap (ap1_gen g r r) (ap1_gen_idp f q) ⬝ ap1_gen_idp g r) =
ap1_gen_idp (g ∘ f) (ap g q ⬝ r) :=
begin induction q, induction r, reflexivity end
@[hott] def ap1_pcompose {A B C : Type*} (g : B →* C) (f : A →* B) :
ap1 (g ∘* f) ~* ap1 g ∘* ap1 f :=
phomotopy.mk (ap1_gen_compose g f (respect_pt f) (respect_pt f) (respect_pt g) (respect_pt g))
(ap1_gen_compose_idp g f (respect_pt f) (respect_pt g))
@[hott] def ap1_pconst (A B : Type*) : Ω→(pconst A B) ~* pconst (Ω A) (Ω B) :=
phomotopy.mk (λp, ap1_gen_idp_left (const A pt) p ⬝ ap_constant p pt) rfl
@[hott] def ap1_gen_con_left {A B : Type _} {a a' : A} {b₀ b₁ b₂ : B}
{f : A → b₀ = b₁} {f' : A → b₁ = b₂} {q₀ q₁ : b₀ = b₁} {q₀' q₁' : b₁ = b₂}
(r₀ : f a = q₀) (r₁ : f a' = q₁) (r₀' : f' a = q₀') (r₁' : f' a' = q₁') (p : a = a') :
ap1_gen (λa, f a ⬝ f' a) (r₀ ◾ r₀') (r₁ ◾ r₁') p =
whisker_right q₀' (ap1_gen f r₀ r₁ p) ⬝ whisker_left q₁ (ap1_gen f' r₀' r₁' p) :=
begin induction r₀, induction r₁, induction r₀', induction r₁', induction p, reflexivity end
@[hott] def ap1_gen_con_left_idp {A B : Type _} {a : A} {b₀ b₁ b₂ : B}
{f : A → b₀ = b₁} {f' : A → b₁ = b₂} {q₀ : b₀ = b₁} {q₁ : b₁ = b₂}
(r₀ : f a = q₀) (r₁ : f' a = q₁) :
ap1_gen_con_left r₀ r₀ r₁ r₁ idp =
con.left_inv _ ⬝ (ap (whisker_right q₁) (con.left_inv _) ◾ ap (whisker_left _) (con.left_inv _))⁻¹ :=
begin induction r₀, induction r₁, reflexivity end
@[hott] def ptransport_change_eq {A : Type _} (B : A → Type*) {a a' : A} {p q : a = a'}
(r : p = q) : ptransport B p ~* ptransport B q :=
phomotopy.mk (λb, ap (λp, transport (λa, B a) p b) r) begin induction r, apply idp_con end
@[hott] def pnatural_square {A B : Type _} (X : B → Type*) {f g : A → B}
(h : Πa, X (f a) →* X (g a)) {a a' : A} (p : a = a') :
h a' ∘* ptransport X (ap f p) ~* ptransport X (ap g p) ∘* h a :=
by induction p; exact pcompose_pid _ ⬝* (pid_pcompose _)⁻¹*
@[hott] def apn_pid {A : Type*} (n : ℕ) : apn n (pid A) ~* pid (Ω[n] A) :=
begin
induction n with n IH,
{ reflexivity},
{ exact ap1_phomotopy IH ⬝* ap1_pid}
end
@[hott] def apn_pconst (A B : Type*) (n : ℕ) :
apn n (pconst A B) ~* pconst (Ω[n] A) (Ω[n] B) :=
begin
induction n with n IH,
{ reflexivity },
{ exact ap1_phomotopy IH ⬝* ap1_pconst _ _ }
end
@[hott] def apn_pcompose (n : ℕ) (g : B →* C) (f : A →* B) :
apn n (g ∘* f) ~* apn n g ∘* apn n f :=
begin
induction n with n IH,
{ reflexivity},
{ refine ap1_phomotopy IH ⬝* _, apply ap1_pcompose}
end
@[hott] def pcast_idp {A : Type*} : pcast (idpath A) ~* pid A :=
by reflexivity
@[hott] def pinverse_pinverse (A : Type*) : pinverse A ∘* pinverse A ~* pid (Ω A) :=
begin
fapply phomotopy.mk,
{ apply hott.eq.inv_inv },
{ reflexivity}
end
@[hott] def pcast_ap_loop {A B : Type*} (p : A = B) :
pcast (ap Ω p) ~* ap1 (pcast p) :=
begin
fapply phomotopy.mk,
{ intro a, induction p, symmetry, exact idp_con _ ⬝ ap_id _ },
{ induction p, refl }
end
@[hott] def ap1_pmap_of_map {A B : Type _} (f : A → B) (a : A) :
ap1 (pmap_of_map f a) ~* pmap_of_map (ap f) (idpath a) :=
begin
fapply phomotopy.mk,
{ intro a, apply idp_con },
{ reflexivity }
end
@[hott] def pcast_commute {A : Type _} {B C : A → Type*} (f : Πa, B a →* C a)
{a₁ a₂ : A} (p : a₁ = a₂) : pcast (ap C p) ∘* f a₁ ~* f a₂ ∘* pcast (ap B p) :=
phomotopy.mk
begin induction p, reflexivity end
begin induction p, refine idp_con _ ⬝ idp_con _ ⬝ _, symmetry, apply ap_id end
/- pointed equivalences -/
structure pequiv (A B : Type*) :=
mk' :: (to_pmap : A →* B)
(to_pinv1 : B →* A)
(to_pinv2 : B →* A)
(pright_inv : to_pmap ∘* to_pinv1 ~* pid B)
(pleft_inv : to_pinv2 ∘* to_pmap ~* pid A)
infix ` ≃* `:25 := pequiv
@[hott, reducible] def pmap_of_pequiv {A B : Type*} (f : A ≃* B) :
@ppi A (λa, B) pt :=
f.to_pmap
@[hott, reducible] def pequiv.to_fun {A B : Type*} (f : A ≃* B) : A → B := f.to_pmap
@[hott] instance {A B : Type*} (f : A ≃* B) : has_coe (A ≃* B) (A →* B) :=
⟨pmap_of_pequiv⟩
@[hott] def to_pinv (f : A ≃* B) : B →* A :=
pequiv.to_pinv1 f
@[hott] def pleft_inv' (f : A ≃* B) : to_pinv f ∘* f.to_pmap ~* pid A :=
let g := to_pinv f in
let h := pequiv.to_pinv2 f in
calc g ∘* f.to_pmap ~* pid A ∘* (g ∘* f.to_pmap) : by exact (pid_pcompose _)⁻¹*
... ~* (h ∘* f.to_pmap) ∘* (g ∘* f.to_pmap) : by exact pwhisker_right _ (pequiv.pleft_inv f)⁻¹*
... ~* h ∘* (f.to_pmap ∘* g) ∘* f.to_pmap : by exact passoc _ _ _ ⬝* pwhisker_left _ (passoc _ _ _)⁻¹*
... ~* h ∘* pid B ∘* f.to_pmap : by exact pwhisker_left _ (pwhisker_right _ (pequiv.pright_inv _))
... ~* h ∘* f.to_pmap : by exact pwhisker_left _ (pid_pcompose _)
... ~* pid A : by exact pequiv.pleft_inv f
@[hott] def equiv_of_pequiv (f : A ≃* B) : A ≃ B :=
equiv.mk f.to_pmap $ adjointify f.to_pmap (to_pinv f) (pequiv.pright_inv f) (pleft_inv' f)
@[hott] def pequiv.to_equiv (f : A ≃* B) : A ≃ B := equiv_of_pequiv f
@[hott] instance pequiv_to_equiv {A B : Type*} (f : A ≃* B) : has_coe (A ≃* B) (A ≃ B) :=
⟨equiv_of_pequiv⟩
@[hott, instance] def pequiv.to_is_equiv (f : A ≃* B) : is_equiv (f.to_pmap) :=
to_is_equiv (equiv_of_pequiv f)
@[hott] protected def pequiv.MK (f : A →* B) (g : B →* A)
(gf : g ∘* f ~* pid A) (fg : f ∘* g ~* pid B) : A ≃* B :=
pequiv.mk' f g g fg gf
@[hott] def pinv (f : A →* B) (H : is_equiv f) : B →* A :=
pmap.mk f⁻¹ᶠ (ap f⁻¹ᶠ (respect_pt f)⁻¹ ⬝ (left_inv f pt))
@[hott] def pequiv_of_pmap (f : A →* B) (H : is_equiv f) : A ≃* B :=
pequiv.mk' f (pinv f H) (pinv f H)
begin abstract
{fapply phomotopy.mk, exact right_inv f,
unfreezeI, induction f with f f₀, induction B with B b₀, dsimp at *, induction f₀,
exactI adj f pt ⬝ ap02 f (idp_con _)⁻¹ᵖ }
end
begin abstract
{fapply phomotopy.mk, exact left_inv f,
unfreezeI, induction f with f f₀, induction B with B b₀, dsimp at *, induction f₀,
exact (idp_con _)⁻¹ ⬝ (idp_con _)⁻¹}
end
@[hott] def pequiv.mk (f : A → B) (H : is_equiv f) (p : f pt = pt) : A ≃* B :=
pequiv_of_pmap (pmap.mk f p) H
@[hott] def pequiv_of_equiv (f : A ≃ B) (H : f pt = pt) : A ≃* B :=
pequiv.mk f f.to_is_equiv H
@[hott, hsimp] def respect_pt_pequiv_of_equiv (f : A ≃ B) (H : f pt = pt) :
respect_pt (pequiv_of_equiv f H).to_pmap = H :=
by refl
@[hott] protected def pequiv.MK' (f : A →* B) (g : B → A)
(gf : Πa, g (f a) = a) (fg : Πb, f (g b) = b) : A ≃* B :=
pequiv.mk f (adjointify f g fg gf) (respect_pt f)
/- reflexivity and symmetry (transitivity is below) -/
@[hott] protected def pequiv.refl (A : Type*) : A ≃* A :=
pequiv.mk' (pid A) (pid A) (pid A) (pid_pcompose _) (pcompose_pid _)
@[hott, refl, reducible] protected def pequiv.rfl : A ≃* A :=
pequiv.refl A
@[hott, symm] protected def pequiv.symm (f : A ≃* B) : B ≃* A :=
pequiv.MK (to_pinv f) f.to_pmap (pequiv.pright_inv f) (pleft_inv' f)
postfix `⁻¹ᵉ*`:(max + 1) := pequiv.symm
@[hott] def pleft_inv (f : A ≃* B) : f⁻¹ᵉ*.to_pmap ∘* f.to_pmap ~* pid A :=
pleft_inv' f
@[hott] def pright_inv (f : A ≃* B) : f.to_pmap ∘* f⁻¹ᵉ*.to_pmap ~* pid B :=
pequiv.pright_inv f
@[hott] def to_pmap_pequiv_of_pmap {A B : Type*} (f : A →* B) (H : is_equiv f)
: pequiv.to_pmap (pequiv_of_pmap f H) = f :=
by reflexivity
@[hott] def to_pmap_pequiv_MK (f : A →* B) (g : B →* A)
(gf : g ∘* f ~* pid A) (fg : f ∘* g ~* pid B) : (pequiv.MK f g gf fg).to_pmap ~* f :=
by reflexivity
@[hott] def to_pinv_pequiv_MK (f : A →* B) (g : B →* A)
(gf : g ∘* f ~* pid A) (fg : f ∘* g ~* pid B) : to_pinv (pequiv.MK f g gf fg) ~* g :=
by reflexivity
/- more on pointed equivalences -/
@[hott] def pequiv_ap {A : Type _} (B : A → Type*) {a a' : A} (p : a = a')
: B a ≃* B a' :=
pequiv_of_pmap (ptransport B p) (is_equiv_tr (λa, B a) p)
@[hott] def pequiv_change_fun (f : A ≃* B) (f' : A →* B) (Heq : f.to_pmap ~ f') : A ≃* B :=
pequiv_of_pmap f' (is_equiv.homotopy_closed f.to_pmap Heq)
@[hott] def pequiv_change_inv (f : A ≃* B) (f' : B →* A) (Heq : to_pinv f ~ f')
: A ≃* B :=
pequiv.MK' f.to_pmap f' (to_left_inv (equiv_change_inv (equiv_of_pequiv f) Heq)) (to_right_inv (equiv_change_inv (equiv_of_pequiv f) Heq))
@[hott] def pequiv_rect' (f : A ≃* B) (P : A → B → Type _)
(g : Πb, P ((equiv_of_pequiv f)⁻¹ᵉ b) b) (a : A) : P a (f.to_pmap a) :=
transport (λx, P x (f.to_pmap a)) (left_inv f.to_pmap a) (g (f.to_pmap a))
@[hott] def pua {A B : Type*} (f : A ≃* B) : A = B :=
pType_eq (equiv_of_pequiv f) (respect_pt _)
@[hott] def pequiv_of_eq {A B : Type*} (p : A = B) : A ≃* B :=
pequiv_of_pmap (pcast p) (is_equiv_tr (λa, a) _)
@[hott] def eq_of_pequiv {A B : Type*} (p : A ≃* B) : A = B :=
pType_eq (equiv_of_pequiv p) (respect_pt _)
@[hott] def peap {A B : Type*} (F : Type* → Type*) (p : A ≃* B) : F A ≃* F B :=
pequiv_of_pmap (pcast (ap F (eq_of_pequiv p))) begin induction eq_of_pequiv p, apply is_equiv_id end
-- rename pequiv_of_eq_natural
@[hott] def pequiv_of_eq_commute {A : Type _} {B C : A → Type*} (f : Πa, B a →* C a)
{a₁ a₂ : A} (p : a₁ = a₂) : (pequiv_of_eq (ap C p)).to_pmap ∘* f a₁ ~* f a₂ ∘* (pequiv_of_eq (ap B p)).to_pmap :=
pcast_commute f p
-- @[hott] def pequiv.eta_expand {A B : Type*} (f : A ≃* B) : A ≃* B :=
-- pequiv.mk' f (to_pinv f) (pequiv.to_pinv2 f) (pright_inv f) _
/-
the @[hott] theorem pequiv_eq, which gives a condition for two pointed equivalences are equal
is in types.equiv to avoid circular imports
-/
/- computation rules of pointed homotopies, possibly combined with pointed equivalences -/
@[hott] def pcancel_left (f : B ≃* C) {g h : A →* B} (p : f.to_pmap ∘* g ~* f.to_pmap ∘* h) : g ~* h :=
begin
refine _⁻¹* ⬝* pwhisker_left f⁻¹ᵉ*.to_pmap p ⬝* _,
all_goals {refine (passoc _ _ _)⁻¹* ⬝* _,
refine pwhisker_right _ (pleft_inv f) ⬝* _,
apply pid_pcompose }
end
@[hott] def pcancel_right (f : A ≃* B) {g h : B →* C} (p : g ∘* f.to_pmap ~* h ∘* f.to_pmap) : g ~* h :=
begin
refine _⁻¹* ⬝* pwhisker_right f⁻¹ᵉ*.to_pmap p ⬝* _,
all_goals {refine passoc _ _ _ ⬝* _,
refine pwhisker_left _ (pright_inv f) ⬝* _,
apply pcompose_pid }
end
@[hott] def phomotopy_pinv_right_of_phomotopy {f : A ≃* B} {g : B →* C} {h : A →* C}
(p : g ∘* f.to_pmap ~* h) : g ~* h ∘* f⁻¹ᵉ*.to_pmap :=
begin
refine _ ⬝* pwhisker_right _ p, symmetry,
refine passoc _ _ _ ⬝* _,
refine pwhisker_left _ (pright_inv f) ⬝* _,
apply pcompose_pid
end
@[hott] def phomotopy_of_pinv_right_phomotopy {f : B ≃* A} {g : B →* C} {h : A →* C}
(p : g ∘* f⁻¹ᵉ*.to_pmap ~* h) : g ~* h ∘* f.to_pmap :=
begin
refine _ ⬝* pwhisker_right _ p, symmetry,
refine passoc _ _ _ ⬝* _,
refine pwhisker_left _ (pleft_inv f) ⬝* _,
apply pcompose_pid
end
@[hott] def pinv_right_phomotopy_of_phomotopy {f : A ≃* B} {g : B →* C} {h : A →* C}
(p : h ~* g ∘* f.to_pmap) : h ∘* f⁻¹ᵉ*.to_pmap ~* g :=
(phomotopy_pinv_right_of_phomotopy p⁻¹*)⁻¹*
@[hott] def phomotopy_of_phomotopy_pinv_right {f : B ≃* A} {g : B →* C} {h : A →* C}
(p : h ~* g ∘* f⁻¹ᵉ*.to_pmap) : h ∘* f.to_pmap ~* g :=
(phomotopy_of_pinv_right_phomotopy p⁻¹*)⁻¹*
@[hott] def phomotopy_pinv_left_of_phomotopy {f : B ≃* C} {g : A →* B} {h : A →* C}
(p : f.to_pmap ∘* g ~* h) : g ~* f⁻¹ᵉ*.to_pmap ∘* h :=
begin
refine _ ⬝* pwhisker_left _ p, symmetry,
refine (passoc _ _ _)⁻¹* ⬝* _,
refine pwhisker_right _ (pleft_inv f) ⬝* _,
apply pid_pcompose
end
@[hott] def phomotopy_of_pinv_left_phomotopy {f : C ≃* B} {g : A →* B} {h : A →* C}
(p : f⁻¹ᵉ*.to_pmap ∘* g ~* h) : g ~* f.to_pmap ∘* h :=
begin
refine _ ⬝* pwhisker_left _ p, symmetry,
refine (passoc _ _ _)⁻¹* ⬝* _,
refine pwhisker_right _ (pright_inv f) ⬝* _,
apply pid_pcompose
end
@[hott] def pinv_left_phomotopy_of_phomotopy {f : B ≃* C} {g : A →* B} {h : A →* C}
(p : h ~* f.to_pmap ∘* g) : f⁻¹ᵉ*.to_pmap ∘* h ~* g :=
(phomotopy_pinv_left_of_phomotopy p⁻¹*)⁻¹*
@[hott] def phomotopy_of_phomotopy_pinv_left {f : C ≃* B} {g : A →* B} {h : A →* C}
(p : h ~* f⁻¹ᵉ*.to_pmap ∘* g) : f.to_pmap ∘* h ~* g :=
(phomotopy_of_pinv_left_phomotopy p⁻¹*)⁻¹*
@[hott] def pcompose2 {A B C : Type*} {g g' : B →* C} {f f' : A →* B} (q : g ~* g') (p : f ~* f') :
g ∘* f ~* g' ∘* f' :=
pwhisker_right f q ⬝* pwhisker_left g' p
infixr ` ◾* `:80 := pcompose2
@[hott] def phomotopy_pinv_of_phomotopy_pid {A B : Type*} {f : A →* B} {g : B ≃* A}
(p : g.to_pmap ∘* f ~* pid A) : f ~* g⁻¹ᵉ*.to_pmap :=
phomotopy_pinv_left_of_phomotopy p ⬝* pcompose_pid _
@[hott] def phomotopy_pinv_of_phomotopy_pid' {A B : Type*} {f : A →* B} {g : B ≃* A}
(p : f ∘* g.to_pmap ~* pid B) : f ~* g⁻¹ᵉ*.to_pmap :=
phomotopy_pinv_right_of_phomotopy p ⬝* pid_pcompose _
@[hott] def pinv_phomotopy_of_pid_phomotopy {A B : Type*} {f : A →* B} {g : B ≃* A}
(p : pid A ~* g.to_pmap ∘* f) : g⁻¹ᵉ*.to_pmap ~* f :=
(phomotopy_pinv_of_phomotopy_pid p⁻¹*)⁻¹*
@[hott] def pinv_phomotopy_of_pid_phomotopy' {A B : Type*} {f : A →* B} {g : B ≃* A}
(p : pid B ~* f ∘* g.to_pmap) : g⁻¹ᵉ*.to_pmap ~* f :=
(phomotopy_pinv_of_phomotopy_pid' p⁻¹*)⁻¹*
@[hott] def pinv_pcompose_cancel_left {A B C : Type*} (g : B ≃* C) (f : A →* B) :
g⁻¹ᵉ*.to_pmap ∘* (g.to_pmap ∘* f) ~* f :=
(passoc _ _ _)⁻¹* ⬝* pwhisker_right f (pleft_inv _) ⬝* pid_pcompose _
@[hott] def pcompose_pinv_cancel_left {A B C : Type*} (g : C ≃* B) (f : A →* B) :
g.to_pmap ∘* (g⁻¹ᵉ*.to_pmap ∘* f) ~* f :=
(passoc _ _ _)⁻¹* ⬝* pwhisker_right f (pright_inv _) ⬝* pid_pcompose _
@[hott] def pinv_pcompose_cancel_right {A B C : Type*} (g : B →* C) (f : B ≃* A) :
(g ∘* f⁻¹ᵉ*.to_pmap) ∘* f.to_pmap ~* g :=
passoc _ _ _ ⬝* pwhisker_left g (pleft_inv _) ⬝* pcompose_pid _
@[hott] def pcompose_pinv_cancel_right {A B C : Type*} (g : B →* C) (f : A ≃* B) :
(g ∘* f.to_pmap) ∘* f⁻¹ᵉ*.to_pmap ~* g :=
passoc _ _ _ ⬝* pwhisker_left g (pright_inv _) ⬝* pcompose_pid _
@[hott] def pinv_pinv {A B : Type*} (f : A ≃* B) : (f⁻¹ᵉ*)⁻¹ᵉ*.to_pmap ~* f.to_pmap :=
(phomotopy_pinv_of_phomotopy_pid (pleft_inv f))⁻¹*
@[hott] def pinv2 {A B : Type*} {f f' : A ≃* B} (p : f.to_pmap ~* f'.to_pmap) : f⁻¹ᵉ*.to_pmap ~* f'⁻¹ᵉ*.to_pmap :=
phomotopy_pinv_of_phomotopy_pid (pinv_right_phomotopy_of_phomotopy (pid_pcompose _ ⬝* p)⁻¹*)
postfix [parsing_only] `⁻²*`:(max+10) := pinv2
@[hott, trans] protected def pequiv.trans (f : A ≃* B) (g : B ≃* C) : A ≃* C :=
pequiv.MK (g.to_pmap ∘* f.to_pmap) (f⁻¹ᵉ*.to_pmap ∘* g⁻¹ᵉ*.to_pmap)
begin abstract {exact passoc _ _ _ ⬝* pwhisker_left _ (pinv_pcompose_cancel_left g f.to_pmap) ⬝* pleft_inv f} end
begin abstract {exact passoc _ _ _ ⬝* pwhisker_left _ (pcompose_pinv_cancel_left f g⁻¹ᵉ*.to_pmap) ⬝* pright_inv g} end
@[hott] def pequiv_compose {A B C : Type*} (g : B ≃* C) (f : A ≃* B) : A ≃* C :=
pequiv.trans f g
infix ` ⬝e* `:75 := pequiv.trans
infixr ` ∘*ᵉ `:60 := pequiv_compose
@[hott] def to_pmap_pequiv_trans {A B C : Type*} (f : A ≃* B) (g : B ≃* C)
: (f ⬝e* g).to_pmap = g.to_pmap ∘* f.to_pmap :=
by reflexivity
@[hott] def to_fun_pequiv_trans {X Y Z : Type*} (f : X ≃* Y) (g :Y ≃* Z) : (f ⬝e* g).to_pmap ~ g.to_pmap ∘ f.to_pmap :=
λx, idp
@[hott] def peconcat_eq {A B C : Type*} (p : A ≃* B) (q : B = C) : A ≃* C :=
p ⬝e* pequiv_of_eq q
@[hott] def eq_peconcat {A B C : Type*} (p : A = B) (q : B ≃* C) : A ≃* C :=
pequiv_of_eq p ⬝e* q
infix ` ⬝e*p `:75 := peconcat_eq
infix ` ⬝pe* `:75 := eq_peconcat
@[hott] def trans_pinv {A B C : Type*} (f : A ≃* B) (g : B ≃* C) :
(f ⬝e* g)⁻¹ᵉ*.to_pmap ~* f⁻¹ᵉ*.to_pmap ∘* g⁻¹ᵉ*.to_pmap :=
by reflexivity
@[hott] def pinv_trans_pinv_left {A B C : Type*} (f : B ≃* A) (g : B ≃* C) :
(f⁻¹ᵉ* ⬝e* g)⁻¹ᵉ*.to_pmap ~* f.to_pmap ∘* g⁻¹ᵉ*.to_pmap :=
by reflexivity
@[hott] def pinv_trans_pinv_right {A B C : Type*} (f : A ≃* B) (g : C ≃* B) :
(f ⬝e* g⁻¹ᵉ*)⁻¹ᵉ*.to_pmap ~* f⁻¹ᵉ*.to_pmap ∘* g.to_pmap :=
by reflexivity
@[hott] def pinv_trans_pinv_pinv {A B C : Type*} (f : B ≃* A) (g : C ≃* B) :
(f⁻¹ᵉ* ⬝e* g⁻¹ᵉ*)⁻¹ᵉ*.to_pmap ~* f.to_pmap ∘* g.to_pmap :=
by reflexivity
/- pointed equivalences between particular pointed types -/
-- TODO: remove is_equiv_apn, which is proven again here
@[hott] def loopn_pequiv_loopn (n : ℕ) (f : A ≃* B) : Ω[n] A ≃* Ω[n] B :=
pequiv.MK (apn n f.to_pmap) (apn n f⁻¹ᵉ*.to_pmap)
begin abstract
{induction n with n IH,
{ apply pleft_inv},
{ rwr [show nat.succ n = n + 1, from idp, apn_succ],
refine (ap1_pcompose _ _)⁻¹* ⬝* _,
refine ap1_phomotopy IH ⬝* _,
apply ap1_pid}}
end
begin abstract
{induction n with n IH,
{ apply pright_inv},
{ rwr [show nat.succ n = n + 1, from idp, apn_succ],
refine (ap1_pcompose _ _)⁻¹* ⬝* _,
refine ap1_phomotopy IH ⬝* _,
apply ap1_pid}}
end
@[hott] def loop_pequiv_loop (f : A ≃* B) : Ω A ≃* Ω B :=
loopn_pequiv_loopn 1 f
@[hott] def loop_pequiv_eq_closed {A : Type _} {a a' : A} (p : a = a')
: pointed.MK (a = a) idp ≃* pointed.MK (a' = a') idp :=
pequiv_of_equiv (loop_equiv_eq_closed p) (con.left_inv p)
@[hott] def to_pmap_loopn_pequiv_loopn (n : ℕ) (f : A ≃* B)
: (loopn_pequiv_loopn n f).to_pmap ~* apn n f.to_pmap :=
by refl
@[hott] def to_pinv_loopn_pequiv_loopn (n : ℕ) (f : A ≃* B)
: (loopn_pequiv_loopn n f)⁻¹ᵉ*.to_pmap ~* apn n f⁻¹ᵉ*.to_pmap :=
by refl
@[hott] def loopn_pequiv_loopn_con (n : ℕ) (f : A ≃* B) (p q : Ω[n+1] A)
: (loopn_pequiv_loopn (n+1) f).to_pmap.to_fun (p ⬝ q) =
(loopn_pequiv_loopn (n+1) f).to_pmap.to_fun p ⬝ (loopn_pequiv_loopn (n+1) f).to_pmap.to_fun q :=
ap1_con (loopn_pequiv_loopn n f).to_pmap p q
@[hott] def loop_pequiv_loop_con {A B : Type*} (f : A ≃* B) (p q : Ω A)
: (loop_pequiv_loop f).to_pmap (p ⬝ q) = (loop_pequiv_loop f).to_pmap p ⬝ (loop_pequiv_loop f).to_pmap q :=
loopn_pequiv_loopn_con 0 f p q
@[hott] def loopn_pequiv_loopn_rfl (n : ℕ) (A : Type*) :
(loopn_pequiv_loopn n (pequiv.refl A)).to_pmap ~* (pequiv.refl (Ω[n] A)).to_pmap :=
begin
exact to_pmap_loopn_pequiv_loopn _ _ ⬝* apn_pid n,
end
@[hott] def loop_pequiv_loop_rfl (A : Type*) :
(loop_pequiv_loop (pequiv.refl A)).to_pmap ~* (pequiv.refl (Ω A)).to_pmap :=
loopn_pequiv_loopn_rfl 1 A
-- duplicate of to_pinv_loopn_pequiv_loopn
@[hott] def apn_pinv (n : ℕ) {A B : Type*} (f : A ≃* B) :
Ω→[n] f⁻¹ᵉ*.to_pmap ~* (loopn_pequiv_loopn n f)⁻¹ᵉ*.to_pmap :=
by reflexivity
@[hott] def pmap_functor {A A' B B' : Type*} (f : A' →* A) (g : B →* B') :
ppmap A B →* ppmap A' B' :=
pmap.mk (λh, g ∘* h ∘* f)
begin abstract {fapply eq_of_phomotopy, fapply phomotopy.mk,
{ hintro a, exact respect_pt g},
{ symmetry, refine _ ◾ idp ⬝ idp_con _,
exact ap02 g (ap_constant _ _) }}
end
@[hott] def pequiv_pinverse (A : Type*) : Ω A ≃* Ω A :=
pequiv_of_pmap (pinverse A) (is_equiv_eq_inverse _ _)
@[hott] def pequiv_of_eq_pt {A : Type _} {a a' : A} (p : a = a') :
pointed.MK A a ≃* pointed.MK A a' :=
pequiv_of_pmap (pmap_of_eq_pt p) (is_equiv_id _)
@[hott] def pointed_eta_pequiv (A : Type*) : A ≃* pointed.MK A pt :=
pequiv.mk id (is_equiv_id _) idp
/- every pointed map is homotopic to one of the form `pmap_of_map _ _`, up to some
pointed equivalences -/
@[hott] def phomotopy_pmap_of_map {A B : Type*} (f : A →* B) :
(pointed_eta_pequiv B ⬝e* (pequiv_of_eq_pt (respect_pt f))⁻¹ᵉ*).to_pmap ∘* f ∘*
(pointed_eta_pequiv A)⁻¹ᵉ*.to_pmap ~* pmap_of_map f pt :=
begin
fapply phomotopy.mk,
{ reflexivity},
{ symmetry, exact (ap_id _ ⬝ idp_con _) ◾ (idp_con _ ⬝ ap_id _) ⬝ con.right_inv _ }
end
/- properties of iterated loop space -/
variable (A)
@[hott] def loopn_succ_in (n : ℕ) : Ω[succ n] A ≃* Ω[n] (Ω A) :=
begin
induction n with n IH,
{ reflexivity},
{ exact loop_pequiv_loop IH}
end
@[hott] def loopn_add (n m : ℕ) : Ω[n] (Ω[m] A) ≃* Ω[m+n] (A) :=
begin
induction n with n IH,
{ reflexivity},
{ exact loop_pequiv_loop IH}
end
@[hott] def loopn_succ_out (n : ℕ) : Ω[succ n] A ≃* Ω(Ω[n] A) :=
by reflexivity
variable {A}
@[hott] def loopn_succ_in_con {n : ℕ} (p q : Ω[succ (succ n)] A) :
(loopn_succ_in A (succ n)).to_pmap (p ⬝ q) =
(loopn_succ_in A (succ n)).to_pmap p ⬝ (loopn_succ_in A (succ n)).to_pmap q :=
loop_pequiv_loop_con _ _ _
@[hott] def loopn_loop_irrel (p : point A = point A) : Ω(pointed.Mk p) = Ω[2] A :=
begin
intros, fapply pType_eq,
{ transitivity _,
apply eq_equiv_fn_eq_of_equiv (equiv_eq_closed_right _ p⁻¹),
apply eq_equiv_eq_closed, apply con.right_inv, apply con.right_inv},
{ apply con.left_inv}
end
@[hott] def loopn_space_loop_irrel (n : ℕ) (p : point A = point A)
: Ω[succ n](pointed.Mk p) = Ω[succ (succ n)] A :> pType :=
calc
Ω[succ n](pointed.Mk p) = Ω[n](Ω (pointed.Mk p)) : eq_of_pequiv $ loopn_succ_in _ _
... = Ω[n] (Ω[2] A) : ap Ω[n] $ loopn_loop_irrel p
... = Ω[n+1] (Ω A) : eq_of_pequiv $ (loopn_succ_in _ _)⁻¹ᵉ*
... = Ω[n+2] A : eq_of_pequiv $ (loopn_succ_in _ _)⁻¹ᵉ*
@[hott] def apn_succ_phomotopy_in (n : ℕ) (f : A →* B) :
(loopn_succ_in B n).to_pmap ∘* Ω→[n + 1] f ~* Ω→[n] (Ω→ f) ∘* (loopn_succ_in A n).to_pmap :=
begin
induction n with n IH,
{ reflexivity},
{ exact (ap1_pcompose _ _)⁻¹* ⬝* ap1_phomotopy IH ⬝* (ap1_pcompose _ _)}
end
@[hott] def loopn_succ_in_natural {A B : Type*} (n : ℕ) (f : A →* B) :
(loopn_succ_in B n).to_pmap ∘* Ω→[n+1] f ~* Ω→[n] (Ω→ f) ∘* (loopn_succ_in A n).to_pmap :=
apn_succ_phomotopy_in _ _
@[hott] def loopn_succ_in_inv_natural {A B : Type*} (n : ℕ) (f : A →* B) :
Ω→[n + 1] f ∘* (loopn_succ_in A n)⁻¹ᵉ*.to_pmap ~* (loopn_succ_in B n)⁻¹ᵉ*.to_pmap ∘* Ω→[n] (Ω→ f):=
begin
apply pinv_right_phomotopy_of_phomotopy,
refine _ ⬝* (passoc _ _ _)⁻¹*,
apply phomotopy_pinv_left_of_phomotopy,
apply apn_succ_phomotopy_in
end
end pointed
end hott
|
6d01ae5316aae1d26fc7a09a8634d8dcae33b9d9 | a4673261e60b025e2c8c825dfa4ab9108246c32e | /stage0/src/Lean/Meta/Tactic/Induction.lean | d9aa61f6575b60449d3c05825a1bbcb367315940 | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 11,602 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Meta.RecursorInfo
import Lean.Meta.SynthInstance
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.Revert
import Lean.Meta.Tactic.Intro
import Lean.Meta.Tactic.Clear
import Lean.Meta.Tactic.FVarSubst
namespace Lean.Meta
private partial def getTargetArity : Expr → Nat
| Expr.mdata _ b _ => getTargetArity b
| Expr.forallE _ _ b _ => getTargetArity b + 1
| e => if e.isHeadBetaTarget then getTargetArity e.headBeta else 0
private def addRecParams (mvarId : MVarId) (majorTypeArgs : Array Expr) : List (Option Nat) → Expr → MetaM Expr
| [], recursor => pure recursor
| some pos :: rest, recursor =>
if h : pos < majorTypeArgs.size then
addRecParams mvarId majorTypeArgs rest (mkApp recursor (majorTypeArgs.get ⟨pos, h⟩))
else
throwTacticEx `induction mvarId "ill-formed recursor"
| none :: rest, recursor => do
let recursorType ← inferType recursor
let recursorType ← whnfForall recursorType
match recursorType with
| Expr.forallE _ d _ _ => do
let param ← try synthInstance d catch _ => throwTacticEx `induction mvarId "failed to generate type class instance parameter"
addRecParams mvarId majorTypeArgs rest (mkApp recursor param)
| _ =>
throwTacticEx `induction mvarId "ill-formed recursor"
structure InductionSubgoal :=
(mvarId : MVarId)
(fields : Array Expr := #[])
(subst : FVarSubst := {})
instance : Inhabited InductionSubgoal := ⟨{ mvarId := arbitrary }⟩
private def getTypeBody (mvarId : MVarId) (type : Expr) (x : Expr) : MetaM Expr := do
let type ← whnfForall type
match type with
| Expr.forallE _ _ b _ => pure $ b.instantiate1 x
| _ => throwTacticEx `induction mvarId "ill-formed recursor"
private partial def finalize
(mvarId : MVarId) (givenNames : Array (List Name)) (recursorInfo : RecursorInfo)
(reverted : Array FVarId) (major : Expr) (indices : Array Expr) (baseSubst : FVarSubst) (recursor : Expr)
: MetaM (Array InductionSubgoal) := do
let target ← getMVarType mvarId
let initialArity := getTargetArity target
let recursorType ← inferType recursor
let numMinors := recursorInfo.produceMotive.length
let rec loop (pos : Nat) (minorIdx : Nat) (recursor recursorType : Expr) (consumedMajor : Bool) (subgoals : Array InductionSubgoal) := do
let recursorType ← whnfForall recursorType
if recursorType.isForall && pos < recursorInfo.numArgs then
if pos == recursorInfo.firstIndexPos then
let (recursor, recursorType) ← indices.foldlM (init := (recursor, recursorType)) fun (recursor, recursorType) index => do
let recursor := mkApp recursor index
let recursorType ← getTypeBody mvarId recursorType index
pure (recursor, recursorType)
let recursor := mkApp recursor major
let recursorType ← getTypeBody mvarId recursorType major
loop (pos+1+indices.size) minorIdx recursor recursorType true subgoals
else
-- consume motive
let tag ← getMVarTag mvarId
if minorIdx ≥ numMinors then throwTacticEx `induction mvarId "ill-formed recursor"
match recursorType with
| Expr.forallE n d b c =>
let d := d.headBeta
-- Remark is givenNames is not empty, then user provided explicit alternatives for each minor premise
if c.binderInfo.isInstImplicit && givenNames.isEmpty then
match (← synthInstance? d) with
| some inst =>
let recursor := mkApp recursor inst
let recursorType ← getTypeBody mvarId recursorType inst
loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor subgoals
| none => do
-- Add newSubgoal if type class resolution failed
let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n)
let recursor := mkApp recursor mvar
let recursorType ← getTypeBody mvarId recursorType mvar
loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvar.mvarId! })
else
let arity := getTargetArity d
if arity < initialArity then throwTacticEx `induction mvarId "ill-formed recursor"
let nparams := arity - initialArity -- number of fields due to minor premise
let nextra := reverted.size - indices.size - 1 -- extra dependencies that have been reverted
let minorGivenNames := if h : minorIdx < givenNames.size then givenNames.get ⟨minorIdx, h⟩ else []
let mvar ← mkFreshExprSyntheticOpaqueMVar d (tag ++ n)
let recursor := mkApp recursor mvar
let recursorType ← getTypeBody mvarId recursorType mvar
-- Try to clear major premise from new goal
let mvarId' ← tryClear mvar.mvarId! major.fvarId!
let (fields, mvarId') ← introN mvarId' nparams minorGivenNames
let (extra, mvarId') ← introNP mvarId' nextra
let subst := reverted.size.fold (init := baseSubst) fun i (subst : FVarSubst) =>
if i < indices.size + 1 then subst
else
let revertedFVarId := reverted[i]
let newFVarId := extra[i - indices.size - 1]
subst.insert revertedFVarId (mkFVar newFVarId)
let fields := fields.map mkFVar
loop (pos+1) (minorIdx+1) recursor recursorType consumedMajor (subgoals.push { mvarId := mvarId', fields := fields, subst := subst })
| _ => unreachable!
else
unless consumedMajor do throwTacticEx `induction mvarId "ill-formed recursor"
assignExprMVar mvarId recursor
pure subgoals
loop (recursorInfo.paramsPos.length + 1) 0 recursor recursorType false #[]
private def throwUnexpectedMajorType {α} (mvarId : MVarId) (majorType : Expr) : MetaM α :=
throwTacticEx `induction mvarId m!"unexpected major premise type{indentExpr majorType}"
def induction (mvarId : MVarId) (majorFVarId : FVarId) (recursorName : Name) (givenNames : Array (List Name) := #[]) (useUnusedNames := false) :
MetaM (Array InductionSubgoal) :=
withMVarContext mvarId do
checkNotAssigned mvarId `induction
let majorLocalDecl ← getLocalDecl majorFVarId
let recursorInfo ← mkRecursorInfo recursorName
let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type
majorType.withApp fun _ majorTypeArgs => do
recursorInfo.paramsPos.forM fun paramPos? => do
match paramPos? with
| none => pure ()
| some paramPos => if paramPos ≥ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}"
let mctx ← getMCtx
let indices ← recursorInfo.indicesPos.toArray.mapM fun idxPos => do
if idxPos ≥ majorTypeArgs.size then throwTacticEx `induction mvarId m!"major premise type is ill-formed{indentExpr majorType}"
let idx := majorTypeArgs.get! idxPos
unless idx.isFVar do throwTacticEx `induction mvarId m!"major premise type index {idx} is not a variable{indentExpr majorType}"
majorTypeArgs.size.forM fun i => do
let arg := majorTypeArgs[i]
if i != idxPos && arg == idx then
throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs more than once{indentExpr majorType}"
if i < idxPos && mctx.exprDependsOn arg idx.fvarId! then
throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it occurs in previous arguments{indentExpr majorType}"
-- If arg is also and index and a variable occurring after `idx`, we need to make sure it doesn't depend on `idx`.
-- Note that if `arg` is not a variable, we will fail anyway when we visit it.
if i > idxPos && recursorInfo.indicesPos.contains i && arg.isFVar then
let idxDecl ← getLocalDecl idx.fvarId!
if mctx.localDeclDependsOn idxDecl arg.fvarId! then
throwTacticEx `induction mvarId m!"'{idx}' is an index in major premise, but it depends on index occurring at position #{i+1}"
pure idx
let target ← getMVarType mvarId
if !recursorInfo.depElim && mctx.exprDependsOn target majorFVarId then
throwTacticEx `induction mvarId m!"recursor '{recursorName}' does not support dependent elimination, but conclusion depends on major premise"
-- Revert indices and major premise preserving variable order
let (reverted, mvarId) ← revert mvarId ((indices.map Expr.fvarId!).push majorFVarId) true
-- Re-introduce indices and major
let (indices', mvarId) ← introNP mvarId indices.size
let (majorFVarId', mvarId) ← intro1P mvarId
-- Create FVarSubst with indices
let baseSubst := do
let mut subst : FVarSubst := {}
let mut i := 0
for index in indices do
subst := subst.insert index.fvarId! (mkFVar indices'[i])
i := i + 1
pure subst
trace[Meta.Tactic.induction]! "after revert&intro\n{MessageData.ofGoal mvarId}"
-- Update indices and major
let indices := indices'.map mkFVar
let majorFVarId := majorFVarId'
let major := mkFVar majorFVarId
withMVarContext mvarId do
let target ← getMVarType mvarId
let targetLevel ← getLevel target
let targetLevel ← normalizeLevel targetLevel
let majorLocalDecl ← getLocalDecl majorFVarId
let some majorType ← whnfUntil majorLocalDecl.type recursorInfo.typeName | throwUnexpectedMajorType mvarId majorLocalDecl.type
majorType.withApp fun majorTypeFn majorTypeArgs => do
match majorTypeFn with
| Expr.const majorTypeFnName majorTypeFnLevels _ => do
let majorTypeFnLevels := majorTypeFnLevels.toArray
let (recursorLevels, foundTargetLevel) ← recursorInfo.univLevelPos.foldlM (init := (#[], false))
fun (recursorLevels, foundTargetLevel) (univPos : RecursorUnivLevelPos) => do
match univPos with
| RecursorUnivLevelPos.motive => pure (recursorLevels.push targetLevel, true)
| RecursorUnivLevelPos.majorType idx =>
if idx ≥ majorTypeFnLevels.size then throwTacticEx `induction mvarId "ill-formed recursor"
pure (recursorLevels.push (majorTypeFnLevels.get! idx), foundTargetLevel)
if !foundTargetLevel && !targetLevel.isZero then
throwTacticEx `induction mvarId m!"recursor '{recursorName}' can only eliminate into Prop"
let recursor := mkConst recursorName recursorLevels.toList
let recursor ← addRecParams mvarId majorTypeArgs recursorInfo.paramsPos recursor
-- Compute motive
let motive := target
let motive ← if recursorInfo.depElim then mkLambdaFVars #[major] motive else pure motive
let motive ← mkLambdaFVars indices motive
let recursor := mkApp recursor motive
finalize mvarId givenNames recursorInfo reverted major indices baseSubst recursor
| _ =>
throwTacticEx `induction mvarId "major premise is not of the form (C ...)"
builtin_initialize registerTraceClass `Meta.Tactic.induction
end Lean.Meta
|
c897dc16d28cc4f950336e75a2136b2adef21195 | 86f6f4f8d827a196a32bfc646234b73328aeb306 | /examples/basics/unnamed_1639.lean | cea47d144441211d9377eec65906193d558e3fa6 | [] | no_license | jamescheuk91/mathematics_in_lean | 09f1f87d2b0dce53464ff0cbe592c568ff59cf5e | 4452499264e2975bca2f42565c0925506ba5dda3 | refs/heads/master | 1,679,716,410,967 | 1,613,957,947,000 | 1,613,957,947,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 103 | lean | import data.real.basic
-- BEGIN
#check (abs_add : ∀ a b : ℝ, abs (a + b) ≤ abs a + abs b)
-- END |
d2e2488270fd0be49aa6d66c252af19a8abfa4ef | 4727251e0cd73359b15b664c3170e5d754078599 | /src/probability/stopping.lean | 65962981ecf2fa83ed65042a2c71941b39f96974 | [
"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 | 44,477 | 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.constructions.borel_space
import measure_theory.function.l1_space
import measure_theory.function.strongly_measurable
import topology.instances.discrete
/-!
# Filtration and stopping time
This file defines some standard definition from the theory of stochastic processes including
filtrations and stopping times. These definitions are used to model the amount of information
at a specific time and is the first step in formalizing stochastic processes.
## Main definitions
* `measure_theory.filtration`: a filtration on a measurable space
* `measure_theory.adapted`: a sequence of functions `u` is said to be adapted to a
filtration `f` if at each point in time `i`, `u i` is `f i`-strongly measurable
* `measure_theory.prog_measurable`: a sequence of functions `u` is said to be progressively
measurable with respect to a filtration `f` if at each point in time `i`, `u` restricted to
`set.Iic i × α` is strongly measurable with respect to the product `measurable_space` structure
where the σ-algebra used for `α` is `f i`.
* `measure_theory.filtration.natural`: the natural filtration with respect to a sequence of
measurable functions is the smallest filtration to which it is adapted to
* `measure_theory.is_stopping_time`: a stopping time with respect to some filtration `f` is a
function `τ` such that for all `i`, the preimage of `{j | j ≤ i}` along `τ` is
`f i`-measurable
* `measure_theory.is_stopping_time.measurable_space`: the σ-algebra associated with a stopping time
## Main results
* `adapted.prog_measurable_of_continuous`: a continuous adapted process is progressively measurable.
* `prog_measurable.stopped_process`: the stopped process of a progressively measurable process is
progressively measurable.
* `mem_ℒp_stopped_process`: if a process belongs to `ℒp` at every time in `ℕ`, then its stopped
process belongs to `ℒp` as well.
## Tags
filtration, stopping time, stochastic process
-/
open filter order topological_space
open_locale classical measure_theory nnreal ennreal topological_space big_operators
namespace measure_theory
/-! ### Filtrations -/
/-- A `filtration` on measurable space `α` with σ-algebra `m` is a monotone
sequence of sub-σ-algebras of `m`. -/
structure filtration {α : Type*} (ι : Type*) [preorder ι] (m : measurable_space α) :=
(seq : ι → measurable_space α)
(mono' : monotone seq)
(le' : ∀ i : ι, seq i ≤ m)
variables {α β ι : Type*} {m : measurable_space α}
instance [preorder ι] : has_coe_to_fun (filtration ι m) (λ _, ι → measurable_space α) :=
⟨λ f, f.seq⟩
namespace filtration
variables [preorder ι]
protected lemma mono {i j : ι} (f : filtration ι m) (hij : i ≤ j) : f i ≤ f j := f.mono' hij
protected lemma le (f : filtration ι m) (i : ι) : f i ≤ m := f.le' i
@[ext] protected lemma ext {f g : filtration ι m} (h : (f : ι → measurable_space α) = g) : f = g :=
by { cases f, cases g, simp only, exact h, }
variable (ι)
/-- The constant filtration which is equal to `m` for all `i : ι`. -/
def const (m' : measurable_space α) (hm' : m' ≤ m) : filtration ι m :=
⟨λ _, m', monotone_const, λ _, hm'⟩
variable {ι}
@[simp]
lemma const_apply {m' : measurable_space α} {hm' : m' ≤ m} (i : ι) : const ι m' hm' i = m' := rfl
instance : inhabited (filtration ι m) := ⟨const ι m le_rfl⟩
instance : has_le (filtration ι m) := ⟨λ f g, ∀ i, f i ≤ g i⟩
instance : has_bot (filtration ι m) := ⟨const ι ⊥ bot_le⟩
instance : has_top (filtration ι m) := ⟨const ι m le_rfl⟩
instance : has_sup (filtration ι m) := ⟨λ f g,
{ seq := λ i, f i ⊔ g i,
mono' := λ i j hij, sup_le ((f.mono hij).trans le_sup_left) ((g.mono hij).trans le_sup_right),
le' := λ i, sup_le (f.le i) (g.le i) }⟩
@[norm_cast] lemma coe_fn_sup {f g : filtration ι m} : ⇑(f ⊔ g) = f ⊔ g := rfl
instance : has_inf (filtration ι m) := ⟨λ f g,
{ seq := λ i, f i ⊓ g i,
mono' := λ i j hij, le_inf (inf_le_left.trans (f.mono hij)) (inf_le_right.trans (g.mono hij)),
le' := λ i, inf_le_left.trans (f.le i) }⟩
@[norm_cast] lemma coe_fn_inf {f g : filtration ι m} : ⇑(f ⊓ g) = f ⊓ g := rfl
instance : has_Sup (filtration ι m) := ⟨λ s,
{ seq := λ i, Sup ((λ f : filtration ι m, f i) '' s),
mono' := λ i j hij,
begin
refine Sup_le (λ m' hm', _),
rw [set.mem_image] at hm',
obtain ⟨f, hf_mem, hfm'⟩ := hm',
rw ← hfm',
refine (f.mono hij).trans _,
have hfj_mem : f j ∈ ((λ g : filtration ι m, g j) '' s), from ⟨f, hf_mem, rfl⟩,
exact le_Sup hfj_mem,
end,
le' := λ i,
begin
refine Sup_le (λ m' hm', _),
rw [set.mem_image] at hm',
obtain ⟨f, hf_mem, hfm'⟩ := hm',
rw ← hfm',
exact f.le i,
end, }⟩
lemma Sup_def (s : set (filtration ι m)) (i : ι) :
Sup s i = Sup ((λ f : filtration ι m, f i) '' s) :=
rfl
noncomputable
instance : has_Inf (filtration ι m) := ⟨λ s,
{ seq := λ i, if set.nonempty s then Inf ((λ f : filtration ι m, f i) '' s) else m,
mono' := λ i j hij,
begin
by_cases h_nonempty : set.nonempty s,
swap, { simp only [h_nonempty, set.nonempty_image_iff, if_false, le_refl], },
simp only [h_nonempty, if_true, le_Inf_iff, set.mem_image, forall_exists_index, and_imp,
forall_apply_eq_imp_iff₂],
refine λ f hf_mem, le_trans _ (f.mono hij),
have hfi_mem : f i ∈ ((λ g : filtration ι m, g i) '' s), from ⟨f, hf_mem, rfl⟩,
exact Inf_le hfi_mem,
end,
le' := λ i,
begin
by_cases h_nonempty : set.nonempty s,
swap, { simp only [h_nonempty, if_false, le_refl], },
simp only [h_nonempty, if_true],
obtain ⟨f, hf_mem⟩ := h_nonempty,
exact le_trans (Inf_le ⟨f, hf_mem, rfl⟩) (f.le i),
end, }⟩
lemma Inf_def (s : set (filtration ι m)) (i : ι) :
Inf s i = if set.nonempty s then Inf ((λ f : filtration ι m, f i) '' s) else m :=
rfl
noncomputable
instance : complete_lattice (filtration ι m) :=
{ le := (≤),
le_refl := λ f i, le_rfl,
le_trans := λ f g h h_fg h_gh i, (h_fg i).trans (h_gh i),
le_antisymm := λ f g h_fg h_gf, filtration.ext $ funext $ λ i, (h_fg i).antisymm (h_gf i),
sup := (⊔),
le_sup_left := λ f g i, le_sup_left,
le_sup_right := λ f g i, le_sup_right,
sup_le := λ f g h h_fh h_gh i, sup_le (h_fh i) (h_gh _),
inf := (⊓),
inf_le_left := λ f g i, inf_le_left,
inf_le_right := λ f g i, inf_le_right,
le_inf := λ f g h h_fg h_fh i, le_inf (h_fg i) (h_fh i),
Sup := Sup,
le_Sup := λ s f hf_mem i, le_Sup ⟨f, hf_mem, rfl⟩,
Sup_le := λ s f h_forall i, Sup_le $ λ m' hm',
begin
obtain ⟨g, hg_mem, hfm'⟩ := hm',
rw ← hfm',
exact h_forall g hg_mem i,
end,
Inf := Inf,
Inf_le := λ s f hf_mem i,
begin
have hs : s.nonempty := ⟨f, hf_mem⟩,
simp only [Inf_def, hs, if_true],
exact Inf_le ⟨f, hf_mem, rfl⟩,
end,
le_Inf := λ s f h_forall i,
begin
by_cases hs : s.nonempty,
swap, { simp only [Inf_def, hs, if_false], exact f.le i, },
simp only [Inf_def, hs, if_true, le_Inf_iff, set.mem_image, forall_exists_index, and_imp,
forall_apply_eq_imp_iff₂],
exact λ g hg_mem, h_forall g hg_mem i,
end,
top := ⊤,
bot := ⊥,
le_top := λ f i, f.le' i,
bot_le := λ f i, bot_le, }
end filtration
lemma measurable_set_of_filtration [preorder ι] {f : filtration ι m} {s : set α} {i : ι}
(hs : measurable_set[f i] s) : measurable_set[m] s :=
f.le i s hs
/-- A measure is σ-finite with respect to filtration if it is σ-finite with respect
to all the sub-σ-algebra of the filtration. -/
class sigma_finite_filtration [preorder ι] (μ : measure α) (f : filtration ι m) : Prop :=
(sigma_finite : ∀ i : ι, sigma_finite (μ.trim (f.le i)))
instance sigma_finite_of_sigma_finite_filtration [preorder ι] (μ : measure α) (f : filtration ι m)
[hf : sigma_finite_filtration μ f] (i : ι) :
sigma_finite (μ.trim (f.le i)) :=
by apply hf.sigma_finite -- can't exact here
section adapted_process
variables [topological_space β] [preorder ι]
{u v : ι → α → β} {f : filtration ι m}
/-- A sequence of functions `u` is adapted to a filtration `f` if for all `i`,
`u i` is `f i`-measurable. -/
def adapted (f : filtration ι m) (u : ι → α → β) : Prop :=
∀ i : ι, strongly_measurable[f i] (u i)
namespace adapted
lemma add [has_add β] [has_continuous_add β] (hu : adapted f u) (hv : adapted f v) :
adapted f (u + v) :=
λ i, (hu i).add (hv i)
lemma neg [add_group β] [topological_add_group β] (hu : adapted f u) : adapted f (-u) :=
λ i, (hu i).neg
lemma smul [has_scalar ℝ β] [has_continuous_smul ℝ β] (c : ℝ) (hu : adapted f u) :
adapted f (c • u) :=
λ i, (hu i).const_smul c
end adapted
variable (β)
lemma adapted_zero [has_zero β] (f : filtration ι m) : adapted f (0 : ι → α → β) :=
λ i, @strongly_measurable_zero α β (f i) _ _
variable {β}
/-- Progressively measurable process. A sequence of functions `u` is said to be progressively
measurable with respect to a filtration `f` if at each point in time `i`, `u` restricted to
`set.Iic i × α` is measurable with respect to the product `measurable_space` structure where the
σ-algebra used for `α` is `f i`.
The usual definition uses the interval `[0,i]`, which we replace by `set.Iic i`. We recover the
usual definition for index types `ℝ≥0` or `ℕ`. -/
def prog_measurable [measurable_space ι] (f : filtration ι m) (u : ι → α → β) : Prop :=
∀ i, strongly_measurable[subtype.measurable_space.prod (f i)] (λ p : set.Iic i × α, u p.1 p.2)
lemma prog_measurable_const [measurable_space ι] (f : filtration ι m) (b : β) :
prog_measurable f ((λ _ _, b) : ι → α → β) :=
λ i, @strongly_measurable_const _ _ (subtype.measurable_space.prod (f i)) _ _
namespace prog_measurable
variables [measurable_space ι]
protected lemma adapted (h : prog_measurable f u) : adapted f u :=
begin
intro i,
have : u i = (λ p : set.Iic i × α, u p.1 p.2) ∘ (λ x, (⟨i, set.mem_Iic.mpr le_rfl⟩, x)) := rfl,
rw this,
exact (h i).comp_measurable measurable_prod_mk_left,
end
protected lemma comp {t : ι → α → ι} [topological_space ι] [borel_space ι] [metrizable_space ι]
(h : prog_measurable f u) (ht : prog_measurable f t)
(ht_le : ∀ i x, t i x ≤ i) :
prog_measurable f (λ i x, u (t i x) x) :=
begin
intro i,
have : (λ p : ↥(set.Iic i) × α, u (t (p.fst : ι) p.snd) p.snd)
= (λ p : ↥(set.Iic i) × α, u (p.fst : ι) p.snd) ∘ (λ p : ↥(set.Iic i) × α,
(⟨t (p.fst : ι) p.snd, set.mem_Iic.mpr ((ht_le _ _).trans p.fst.prop)⟩, p.snd)) := rfl,
rw this,
exact (h i).comp_measurable ((ht i).measurable.subtype_mk.prod_mk measurable_snd),
end
section arithmetic
@[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β]
(hu : prog_measurable f u) (hv : prog_measurable f v) :
prog_measurable f (λ i x, u i x * v i x) :=
λ i, (hu i).mul (hv i)
@[to_additive] protected lemma finset_prod' {γ} [comm_monoid β] [has_continuous_mul β]
{U : γ → ι → α → β} {s : finset γ} (h : ∀ c ∈ s, prog_measurable f (U c)) :
prog_measurable f (∏ c in s, U c) :=
finset.prod_induction U (prog_measurable f) (λ _ _, prog_measurable.mul)
(prog_measurable_const _ 1) h
@[to_additive] protected lemma finset_prod {γ} [comm_monoid β] [has_continuous_mul β]
{U : γ → ι → α → β} {s : finset γ} (h : ∀ c ∈ s, prog_measurable f (U c)) :
prog_measurable f (λ i a, ∏ c in s, U c i a) :=
by { convert prog_measurable.finset_prod' h, ext i a, simp only [finset.prod_apply], }
@[to_additive] protected lemma inv [group β] [topological_group β] (hu : prog_measurable f u) :
prog_measurable f (λ i x, (u i x)⁻¹) :=
λ i, (hu i).inv
@[to_additive] protected lemma div [group β] [topological_group β]
(hu : prog_measurable f u) (hv : prog_measurable f v) :
prog_measurable f (λ i x, u i x / v i x) :=
λ i, (hu i).div (hv i)
end arithmetic
end prog_measurable
lemma prog_measurable_of_tendsto' {γ} [measurable_space ι] [metrizable_space β]
(fltr : filter γ) [fltr.ne_bot] [fltr.is_countably_generated] {U : γ → ι → α → β}
(h : ∀ l, prog_measurable f (U l)) (h_tendsto : tendsto U fltr (𝓝 u)) :
prog_measurable f u :=
begin
assume i,
apply @strongly_measurable_of_tendsto (set.Iic i × α) β γ (measurable_space.prod _ (f i))
_ _ fltr _ _ _ _ (λ l, h l i),
rw tendsto_pi_nhds at h_tendsto ⊢,
intro x,
specialize h_tendsto x.fst,
rw tendsto_nhds at h_tendsto ⊢,
exact λ s hs h_mem, h_tendsto {g | g x.snd ∈ s} (hs.preimage (continuous_apply x.snd)) h_mem,
end
lemma prog_measurable_of_tendsto [measurable_space ι] [metrizable_space β]
{U : ℕ → ι → α → β}
(h : ∀ l, prog_measurable f (U l)) (h_tendsto : tendsto U at_top (𝓝 u)) :
prog_measurable f u :=
prog_measurable_of_tendsto' at_top h h_tendsto
/-- A continuous and adapted process is progressively measurable. -/
theorem adapted.prog_measurable_of_continuous
[topological_space ι] [metrizable_space ι] [measurable_space ι]
[second_countable_topology ι] [opens_measurable_space ι] [metrizable_space β]
(h : adapted f u) (hu_cont : ∀ x, continuous (λ i, u i x)) :
prog_measurable f u :=
λ i, @strongly_measurable_uncurry_of_continuous_of_strongly_measurable _ _ (set.Iic i) _ _ _ _ _ _ _
(f i) _ (λ x, (hu_cont x).comp continuous_induced_dom) (λ j, (h j).mono (f.mono j.prop))
end adapted_process
namespace filtration
variables [topological_space β] [metrizable_space β] [mβ : measurable_space β] [borel_space β]
[preorder ι]
include mβ
/-- Given a sequence of functions, the natural filtration is the smallest sequence
of σ-algebras such that that sequence of functions is measurable with respect to
the filtration. -/
def natural (u : ι → α → β) (hum : ∀ i, strongly_measurable (u i)) : filtration ι m :=
{ seq := λ i, ⨆ j ≤ i, measurable_space.comap (u j) mβ,
mono' := λ i j hij, bsupr_mono $ λ k, ge_trans hij,
le' := λ i,
begin
refine supr₂_le _,
rintros j hj s ⟨t, ht, rfl⟩,
exact (hum j).measurable ht,
end }
lemma adapted_natural {u : ι → α → β} (hum : ∀ i, strongly_measurable[m] (u i)) :
adapted (natural u hum) u :=
begin
assume i,
refine strongly_measurable.mono _ (le_supr₂_of_le i (le_refl i) le_rfl),
rw strongly_measurable_iff_measurable_separable,
exact ⟨measurable_iff_comap_le.2 le_rfl, (hum i).is_separable_range⟩
end
end filtration
/-! ### Stopping times -/
/-- A stopping time with respect to some filtration `f` is a function
`τ` such that for all `i`, the preimage of `{j | j ≤ i}` along `τ` is measurable
with respect to `f i`.
Intuitively, the stopping time `τ` describes some stopping rule such that at time
`i`, we may determine it with the information we have at time `i`. -/
def is_stopping_time [preorder ι] (f : filtration ι m) (τ : α → ι) :=
∀ i : ι, measurable_set[f i] $ {x | τ x ≤ i}
lemma is_stopping_time_const [preorder ι] (f : filtration ι m) (i : ι) :
is_stopping_time f (λ x, i) :=
λ j, by simp only [measurable_set.const]
section measurable_set
section preorder
variables [preorder ι] {f : filtration ι m} {τ : α → ι}
protected lemma is_stopping_time.measurable_set_le (hτ : is_stopping_time f τ) (i : ι) :
measurable_set[f i] {x | τ x ≤ i} :=
hτ i
lemma is_stopping_time.measurable_set_lt_of_pred [pred_order ι]
(hτ : is_stopping_time f τ) (i : ι) :
measurable_set[f i] {x | τ x < i} :=
begin
by_cases hi_min : is_min i,
{ suffices : {x : α | τ x < i} = ∅, by { rw this, exact @measurable_set.empty _ (f i), },
ext1 x,
simp only [set.mem_set_of_eq, set.mem_empty_eq, iff_false],
rw is_min_iff_forall_not_lt at hi_min,
exact hi_min (τ x), },
have : {x : α | τ x < i} = τ ⁻¹' (set.Iio i) := rfl,
rw [this, ←Iic_pred_of_not_is_min hi_min],
exact f.mono (pred_le i) _ (hτ.measurable_set_le $ pred i),
end
end preorder
section linear_order
variables [linear_order ι] {f : filtration ι m} {τ : α → ι}
lemma is_stopping_time.measurable_set_gt (hτ : is_stopping_time f τ) (i : ι) :
measurable_set[f i] {x | i < τ x} :=
begin
have : {x | i < τ x} = {x | τ x ≤ i}ᶜ,
{ ext1 x, simp only [set.mem_set_of_eq, set.mem_compl_eq, not_le], },
rw this,
exact (hτ.measurable_set_le i).compl,
end
variables [topological_space ι] [order_topology ι] [first_countable_topology ι]
/-- Auxiliary lemma for `is_stopping_time.measurable_set_lt`. -/
lemma is_stopping_time.measurable_set_lt_of_is_lub
(hτ : is_stopping_time f τ) (i : ι) (h_lub : is_lub (set.Iio i) i) :
measurable_set[f i] {x | τ x < i} :=
begin
by_cases hi_min : is_min i,
{ suffices : {x : α | τ x < i} = ∅, by { rw this, exact @measurable_set.empty _ (f i), },
ext1 x,
simp only [set.mem_set_of_eq, set.mem_empty_eq, iff_false],
exact is_min_iff_forall_not_lt.mp hi_min (τ x), },
obtain ⟨seq, -, -, h_tendsto, h_bound⟩ : ∃ seq : ℕ → ι,
monotone seq ∧ (∀ j, seq j ≤ i) ∧ tendsto seq at_top (𝓝 i) ∧ (∀ j, seq j < i),
from h_lub.exists_seq_monotone_tendsto (not_is_min_iff.mp hi_min),
have h_Ioi_eq_Union : set.Iio i = ⋃ j, { k | k ≤ seq j},
{ ext1 k,
simp only [set.mem_Iio, set.mem_Union, set.mem_set_of_eq],
refine ⟨λ hk_lt_i, _, λ h_exists_k_le_seq, _⟩,
{ rw tendsto_at_top' at h_tendsto,
have h_nhds : set.Ici k ∈ 𝓝 i,
from mem_nhds_iff.mpr ⟨set.Ioi k, set.Ioi_subset_Ici le_rfl, is_open_Ioi, hk_lt_i⟩,
obtain ⟨a, ha⟩ : ∃ (a : ℕ), ∀ (b : ℕ), b ≥ a → k ≤ seq b := h_tendsto (set.Ici k) h_nhds,
exact ⟨a, ha a le_rfl⟩, },
{ obtain ⟨j, hk_seq_j⟩ := h_exists_k_le_seq,
exact hk_seq_j.trans_lt (h_bound j), }, },
have h_lt_eq_preimage : {x : α | τ x < i} = τ ⁻¹' (set.Iio i),
{ ext1 x, simp only [set.mem_set_of_eq, set.mem_preimage, set.mem_Iio], },
rw [h_lt_eq_preimage, h_Ioi_eq_Union],
simp only [set.preimage_Union, set.preimage_set_of_eq],
exact measurable_set.Union
(λ n, f.mono (h_bound n).le _ (hτ.measurable_set_le (seq n))),
end
lemma is_stopping_time.measurable_set_lt (hτ : is_stopping_time f τ) (i : ι) :
measurable_set[f i] {x | τ x < i} :=
begin
obtain ⟨i', hi'_lub⟩ : ∃ i', is_lub (set.Iio i) i', from exists_lub_Iio i,
cases lub_Iio_eq_self_or_Iio_eq_Iic i hi'_lub with hi'_eq_i h_Iio_eq_Iic,
{ rw ← hi'_eq_i at hi'_lub ⊢,
exact hτ.measurable_set_lt_of_is_lub i' hi'_lub, },
{ have h_lt_eq_preimage : {x : α | τ x < i} = τ ⁻¹' (set.Iio i) := rfl,
rw [h_lt_eq_preimage, h_Iio_eq_Iic],
exact f.mono (lub_Iio_le i hi'_lub) _ (hτ.measurable_set_le i'), },
end
lemma is_stopping_time.measurable_set_ge (hτ : is_stopping_time f τ) (i : ι) :
measurable_set[f i] {x | i ≤ τ x} :=
begin
have : {x | i ≤ τ x} = {x | τ x < i}ᶜ,
{ ext1 x, simp only [set.mem_set_of_eq, set.mem_compl_eq, not_lt], },
rw this,
exact (hτ.measurable_set_lt i).compl,
end
lemma is_stopping_time.measurable_set_eq (hτ : is_stopping_time f τ) (i : ι) :
measurable_set[f i] {x | τ x = i} :=
begin
have : {x | τ x = i} = {x | τ x ≤ i} ∩ {x | τ x ≥ i},
{ ext1 x, simp only [set.mem_set_of_eq, ge_iff_le, set.mem_inter_eq, le_antisymm_iff], },
rw this,
exact (hτ.measurable_set_le i).inter (hτ.measurable_set_ge i),
end
lemma is_stopping_time.measurable_set_eq_le (hτ : is_stopping_time f τ) {i j : ι} (hle : i ≤ j) :
measurable_set[f j] {x | τ x = i} :=
f.mono hle _ $ hτ.measurable_set_eq i
lemma is_stopping_time.measurable_set_lt_le (hτ : is_stopping_time f τ) {i j : ι} (hle : i ≤ j) :
measurable_set[f j] {x | τ x < i} :=
f.mono hle _ $ hτ.measurable_set_lt i
end linear_order
section encodable
lemma is_stopping_time_of_measurable_set_eq [preorder ι] [encodable ι]
{f : filtration ι m} {τ : α → ι} (hτ : ∀ i, measurable_set[f i] {x | τ x = i}) :
is_stopping_time f τ :=
begin
intro i,
rw show {x | τ x ≤ i} = ⋃ k ≤ i, {x | τ x = k}, by { ext, simp },
refine measurable_set.bUnion (set.countable_encodable _) (λ k hk, _),
exact f.mono hk _ (hτ k),
end
end encodable
end measurable_set
namespace is_stopping_time
protected lemma max [linear_order ι] {f : filtration ι m} {τ π : α → ι}
(hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) :
is_stopping_time f (λ x, max (τ x) (π x)) :=
begin
intro i,
simp_rw [max_le_iff, set.set_of_and],
exact (hτ i).inter (hπ i),
end
protected lemma max_const [linear_order ι] {f : filtration ι m} {τ : α → ι}
(hτ : is_stopping_time f τ) (i : ι) :
is_stopping_time f (λ x, max (τ x) i) :=
hτ.max (is_stopping_time_const f i)
protected lemma min [linear_order ι] {f : filtration ι m} {τ π : α → ι}
(hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) :
is_stopping_time f (λ x, min (τ x) (π x)) :=
begin
intro i,
simp_rw [min_le_iff, set.set_of_or],
exact (hτ i).union (hπ i),
end
protected lemma min_const [linear_order ι] {f : filtration ι m} {τ : α → ι}
(hτ : is_stopping_time f τ) (i : ι) :
is_stopping_time f (λ x, min (τ x) i) :=
hτ.min (is_stopping_time_const f i)
lemma add_const [add_group ι] [preorder ι] [covariant_class ι ι (function.swap (+)) (≤)]
[covariant_class ι ι (+) (≤)]
{f : filtration ι m} {τ : α → ι} (hτ : is_stopping_time f τ) {i : ι} (hi : 0 ≤ i) :
is_stopping_time f (λ x, τ x + i) :=
begin
intro j,
simp_rw [← le_sub_iff_add_le],
exact f.mono (sub_le_self j hi) _ (hτ (j - i)),
end
lemma add_const_nat
{f : filtration ℕ m} {τ : α → ℕ} (hτ : is_stopping_time f τ) {i : ℕ} :
is_stopping_time f (λ x, τ x + i) :=
begin
refine is_stopping_time_of_measurable_set_eq (λ j, _),
by_cases hij : i ≤ j,
{ simp_rw [eq_comm, ← nat.sub_eq_iff_eq_add hij, eq_comm],
exact f.mono (j.sub_le i) _ (hτ.measurable_set_eq (j - i)) },
{ rw not_le at hij,
convert measurable_set.empty,
ext x,
simp only [set.mem_empty_eq, iff_false],
rintro (hx : τ x + i = j),
linarith },
end
-- generalize to certain encodable type?
lemma add
{f : filtration ℕ m} {τ π : α → ℕ} (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) :
is_stopping_time f (τ + π) :=
begin
intro i,
rw (_ : {x | (τ + π) x ≤ i} = ⋃ k ≤ i, {x | π x = k} ∩ {x | τ x + k ≤ i}),
{ exact measurable_set.Union (λ k, measurable_set.Union_Prop
(λ hk, (hπ.measurable_set_eq_le hk).inter (hτ.add_const_nat i))) },
ext,
simp only [pi.add_apply, set.mem_set_of_eq, set.mem_Union, set.mem_inter_eq, exists_prop],
refine ⟨λ h, ⟨π x, by linarith, rfl, h⟩, _⟩,
rintro ⟨j, hj, rfl, h⟩,
assumption
end
section preorder
variables [preorder ι] {f : filtration ι m} {τ π : α → ι}
/-- The associated σ-algebra with a stopping time. -/
protected def measurable_space (hτ : is_stopping_time f τ) : measurable_space α :=
{ measurable_set' := λ s, ∀ i : ι, measurable_set[f i] (s ∩ {x | τ x ≤ i}),
measurable_set_empty :=
λ i, (set.empty_inter {x | τ x ≤ i}).symm ▸ @measurable_set.empty _ (f i),
measurable_set_compl := λ s hs i,
begin
rw (_ : sᶜ ∩ {x | τ x ≤ i} = (sᶜ ∪ {x | τ x ≤ i}ᶜ) ∩ {x | τ x ≤ i}),
{ refine measurable_set.inter _ _,
{ rw ← set.compl_inter,
exact (hs i).compl },
{ exact hτ i} },
{ rw set.union_inter_distrib_right,
simp only [set.compl_inter_self, set.union_empty] }
end,
measurable_set_Union := λ s hs i,
begin
rw forall_swap at hs,
rw set.Union_inter,
exact measurable_set.Union (hs i),
end }
protected lemma measurable_set (hτ : is_stopping_time f τ) (s : set α) :
measurable_set[hτ.measurable_space] s ↔
∀ i : ι, measurable_set[f i] (s ∩ {x | τ x ≤ i}) :=
iff.rfl
lemma measurable_space_mono
(hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (hle : τ ≤ π) :
hτ.measurable_space ≤ hπ.measurable_space :=
begin
intros s hs i,
rw (_ : s ∩ {x | π x ≤ i} = s ∩ {x | τ x ≤ i} ∩ {x | π x ≤ i}),
{ exact (hs i).inter (hπ i) },
{ ext,
simp only [set.mem_inter_eq, iff_self_and, and.congr_left_iff, set.mem_set_of_eq],
intros hle' _,
exact le_trans (hle _) hle' },
end
lemma measurable_space_le [encodable ι] (hτ : is_stopping_time f τ) :
hτ.measurable_space ≤ m :=
begin
intros s hs,
change ∀ i, measurable_set[f i] (s ∩ {x | τ x ≤ i}) at hs,
rw (_ : s = ⋃ i, s ∩ {x | τ x ≤ i}),
{ exact measurable_set.Union (λ i, f.le i _ (hs i)) },
{ ext x, split; rw set.mem_Union,
{ exact λ hx, ⟨τ x, hx, le_rfl⟩ },
{ rintro ⟨_, hx, _⟩,
exact hx } }
end
@[simp] lemma measurable_space_const (f : filtration ι m) (i : ι) :
(is_stopping_time_const f i).measurable_space = f i :=
begin
ext1 s,
change measurable_set[(is_stopping_time_const f i).measurable_space] s ↔ measurable_set[f i] s,
rw is_stopping_time.measurable_set,
split; intro h,
{ specialize h i,
simpa only [le_refl, set.set_of_true, set.inter_univ] using h, },
{ intro j,
by_cases hij : i ≤ j,
{ simp only [hij, set.set_of_true, set.inter_univ],
exact f.mono hij _ h, },
{ simp only [hij, set.set_of_false, set.inter_empty, measurable_set.empty], }, },
end
lemma measurable_set_inter_eq_iff (hτ : is_stopping_time f τ) (s : set α) (i : ι) :
measurable_set[hτ.measurable_space] (s ∩ {x | τ x = i})
↔ measurable_set[f i] (s ∩ {x | τ x = i}) :=
begin
have : ∀ j, ({x : α | τ x = i} ∩ {x : α | τ x ≤ j}) = {x : α | τ x = i} ∩ {x | i ≤ j},
{ intro j,
ext1 x,
simp only [set.mem_inter_eq, set.mem_set_of_eq, and.congr_right_iff],
intro hxi,
rw hxi, },
split; intro h,
{ specialize h i,
simpa only [set.inter_assoc, this, le_refl, set.set_of_true, set.inter_univ] using h, },
{ intro j,
rw [set.inter_assoc, this],
by_cases hij : i ≤ j,
{ simp only [hij, set.set_of_true, set.inter_univ],
exact f.mono hij _ h, },
{ simp [hij], }, },
end
lemma measurable_space_le_of_le_const (hτ : is_stopping_time f τ) {i : ι} (hτ_le : ∀ x, τ x ≤ i) :
hτ.measurable_space ≤ f i :=
(measurable_space_mono hτ _ hτ_le).trans (is_stopping_time.measurable_space_const _ _).le
lemma le_measurable_space_of_const_le (hτ : is_stopping_time f τ) {i : ι} (hτ_le : ∀ x, i ≤ τ x) :
f i ≤ hτ.measurable_space :=
(is_stopping_time.measurable_space_const _ _).symm.le.trans (measurable_space_mono _ hτ hτ_le)
end preorder
section linear_order
variables [linear_order ι] {f : filtration ι m} {τ π : α → ι}
protected lemma measurable_set_le' (hτ : is_stopping_time f τ) (i : ι) :
measurable_set[hτ.measurable_space] {x | τ x ≤ i} :=
begin
intro j,
have : {x : α | τ x ≤ i} ∩ {x : α | τ x ≤ j} = {x : α | τ x ≤ min i j},
{ ext1 x, simp only [set.mem_inter_eq, set.mem_set_of_eq, le_min_iff], },
rw this,
exact f.mono (min_le_right i j) _ (hτ _),
end
protected lemma measurable_set_gt' (hτ : is_stopping_time f τ) (i : ι) :
measurable_set[hτ.measurable_space] {x | i < τ x} :=
begin
have : {x : α | i < τ x} = {x : α | τ x ≤ i}ᶜ, by { ext1 x, simp, },
rw this,
exact (hτ.measurable_set_le' i).compl,
end
protected lemma measurable_set_eq' [topological_space ι] [order_topology ι]
[first_countable_topology ι]
(hτ : is_stopping_time f τ) (i : ι) :
measurable_set[hτ.measurable_space] {x | τ x = i} :=
begin
rw [← set.univ_inter {x | τ x = i}, measurable_set_inter_eq_iff, set.univ_inter],
exact hτ.measurable_set_eq i,
end
protected lemma measurable_set_ge' [topological_space ι] [order_topology ι]
[first_countable_topology ι]
(hτ : is_stopping_time f τ) (i : ι) :
measurable_set[hτ.measurable_space] {x | i ≤ τ x} :=
begin
have : {x | i ≤ τ x} = {x | τ x = i} ∪ {x | i < τ x},
{ ext1 x,
simp only [le_iff_lt_or_eq, set.mem_set_of_eq, set.mem_union_eq],
rw [@eq_comm _ i, or_comm], },
rw this,
exact (hτ.measurable_set_eq' i).union (hτ.measurable_set_gt' i),
end
protected lemma measurable_set_lt' [topological_space ι] [order_topology ι]
[first_countable_topology ι]
(hτ : is_stopping_time f τ) (i : ι) :
measurable_set[hτ.measurable_space] {x | τ x < i} :=
begin
have : {x | τ x < i} = {x | τ x ≤ i} \ {x | τ x = i},
{ ext1 x,
simp only [lt_iff_le_and_ne, set.mem_set_of_eq, set.mem_diff], },
rw this,
exact (hτ.measurable_set_le' i).diff (hτ.measurable_set_eq' i),
end
protected lemma measurable [topological_space ι] [measurable_space ι]
[borel_space ι] [order_topology ι] [second_countable_topology ι]
(hτ : is_stopping_time f τ) :
measurable[hτ.measurable_space] τ :=
@measurable_of_Iic ι α _ _ _ hτ.measurable_space _ _ _ _ (λ i, hτ.measurable_set_le' i)
protected lemma measurable_of_le [topological_space ι] [measurable_space ι]
[borel_space ι] [order_topology ι] [second_countable_topology ι]
(hτ : is_stopping_time f τ) {i : ι} (hτ_le : ∀ x, τ x ≤ i) :
measurable[f i] τ :=
hτ.measurable.mono (measurable_space_le_of_le_const _ hτ_le) le_rfl
lemma measurable_space_min (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) :
(hτ.min hπ).measurable_space = hτ.measurable_space ⊓ hπ.measurable_space :=
begin
refine le_antisymm _ _,
{ exact le_inf (is_stopping_time.measurable_space_mono _ hτ (λ _, min_le_left _ _))
(is_stopping_time.measurable_space_mono _ hπ (λ _, min_le_right _ _)), },
{ intro s,
change measurable_set[hτ.measurable_space] s ∧ measurable_set[hπ.measurable_space] s
→ measurable_set[(hτ.min hπ).measurable_space] s,
simp_rw is_stopping_time.measurable_set,
have : ∀ i, {x | min (τ x) (π x) ≤ i} = {x | τ x ≤ i} ∪ {x | π x ≤ i},
{ intro i, ext1 x, simp, },
simp_rw [this, set.inter_union_distrib_left],
exact λ h i, (h.left i).union (h.right i), },
end
lemma measurable_set_min_iff (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (s : set α) :
measurable_set[(hτ.min hπ).measurable_space] s
↔ measurable_set[hτ.measurable_space] s ∧ measurable_set[hπ.measurable_space] s :=
by { rw measurable_space_min, refl, }
lemma measurable_set_inter_le [topological_space ι] [second_countable_topology ι] [order_topology ι]
[measurable_space ι] [borel_space ι]
(hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (s : set α)
(hs : measurable_set[hτ.measurable_space] s) :
measurable_set[(hτ.min hπ).measurable_space] (s ∩ {x | τ x ≤ π x}) :=
begin
simp_rw is_stopping_time.measurable_set at ⊢ hs,
intro i,
have : (s ∩ {x | τ x ≤ π x} ∩ {x | min (τ x) (π x) ≤ i})
= (s ∩ {x | τ x ≤ i}) ∩ {x | min (τ x) (π x) ≤ i} ∩ {x | min (τ x) i ≤ min (min (τ x) (π x)) i},
{ ext1 x,
simp only [min_le_iff, set.mem_inter_eq, set.mem_set_of_eq, le_min_iff, le_refl, true_and,
and_true, true_or, or_true],
by_cases hτi : τ x ≤ i,
{ simp only [hτi, true_or, and_true, and.congr_right_iff],
intro hx,
split; intro h,
{ exact or.inl h, },
{ cases h,
{ exact h, },
{ exact hτi.trans h, }, }, },
simp only [hτi, false_or, and_false, false_and, iff_false, not_and, not_le, and_imp],
refine λ hx hτ_le_π, lt_of_lt_of_le _ hτ_le_π,
rw ← not_le,
exact hτi, },
rw this,
refine ((hs i).inter ((hτ.min hπ) i)).inter _,
apply measurable_set_le,
{ exact (hτ.min_const i).measurable_of_le (λ _, min_le_right _ _), },
{ exact ((hτ.min hπ).min_const i).measurable_of_le (λ _, min_le_right _ _), },
end
end linear_order
end is_stopping_time
section linear_order
/-! ## Stopped value and stopped process -/
/-- Given a map `u : ι → α → E`, its stopped value with respect to the stopping
time `τ` is the map `x ↦ u (τ x) x`. -/
def stopped_value (u : ι → α → β) (τ : α → ι) : α → β :=
λ x, u (τ x) x
lemma stopped_value_const (u : ι → α → β) (i : ι) : stopped_value u (λ x, i) = u i :=
rfl
variable [linear_order ι]
/-- Given a map `u : ι → α → E`, the stopped process with respect to `τ` is `u i x` if
`i ≤ τ x`, and `u (τ x) x` otherwise.
Intuitively, the stopped process stops evolving once the stopping time has occured. -/
def stopped_process (u : ι → α → β) (τ : α → ι) : ι → α → β :=
λ i x, u (min i (τ x)) x
lemma stopped_process_eq_of_le {u : ι → α → β} {τ : α → ι}
{i : ι} {x : α} (h : i ≤ τ x) : stopped_process u τ i x = u i x :=
by simp [stopped_process, min_eq_left h]
lemma stopped_process_eq_of_ge {u : ι → α → β} {τ : α → ι}
{i : ι} {x : α} (h : τ x ≤ i) : stopped_process u τ i x = u (τ x) x :=
by simp [stopped_process, min_eq_right h]
section prog_measurable
variables [measurable_space ι] [topological_space ι] [order_topology ι]
[second_countable_topology ι] [borel_space ι] [metrizable_space ι]
[topological_space β]
{u : ι → α → β} {τ : α → ι} {f : filtration ι m}
lemma prog_measurable_min_stopping_time (hτ : is_stopping_time f τ) :
prog_measurable f (λ i x, min i (τ x)) :=
begin
intro i,
let m_prod : measurable_space (set.Iic i × α) := measurable_space.prod _ (f i),
let m_set : ∀ t : set (set.Iic i × α), measurable_space t :=
λ _, @subtype.measurable_space (set.Iic i × α) _ m_prod,
let s := {p : set.Iic i × α | τ p.2 ≤ i},
have hs : measurable_set[m_prod] s, from @measurable_snd (set.Iic i) α _ (f i) _ (hτ i),
have h_meas_fst : ∀ t : set (set.Iic i × α),
measurable[m_set t] (λ x : t, ((x : set.Iic i × α).fst : ι)),
from λ t, (@measurable_subtype_coe (set.Iic i × α) m_prod _).fst.subtype_coe,
apply measurable.strongly_measurable,
refine measurable_of_restrict_of_restrict_compl hs _ _,
{ refine @measurable.min _ _ _ _ _ (m_set s) _ _ _ _ _ (h_meas_fst s) _,
refine @measurable_of_Iic ι s _ _ _ (m_set s) _ _ _ _ (λ j, _),
have h_set_eq : (λ x : s, τ (x : set.Iic i × α).snd) ⁻¹' set.Iic j
= (λ x : s, (x : set.Iic i × α).snd) ⁻¹' {x | τ x ≤ min i j},
{ ext1 x,
simp only [set.mem_preimage, set.mem_Iic, iff_and_self, le_min_iff, set.mem_set_of_eq],
exact λ _, x.prop, },
rw h_set_eq,
suffices h_meas : @measurable _ _ (m_set s) (f i) (λ x : s, (x : set.Iic i × α).snd),
from h_meas (f.mono (min_le_left _ _) _ (hτ.measurable_set_le (min i j))),
exact measurable_snd.comp (@measurable_subtype_coe _ m_prod _), },
{ suffices h_min_eq_left : (λ x : sᶜ, min ↑((x : set.Iic i × α).fst) (τ (x : set.Iic i × α).snd))
= λ x : sᶜ, ↑((x : set.Iic i × α).fst),
{ rw [set.restrict, h_min_eq_left],
exact h_meas_fst _, },
ext1 x,
rw min_eq_left,
have hx_fst_le : ↑(x : set.Iic i × α).fst ≤ i, from (x : set.Iic i × α).fst.prop,
refine hx_fst_le.trans (le_of_lt _),
convert x.prop,
simp only [not_le, set.mem_compl_eq, set.mem_set_of_eq], },
end
lemma prog_measurable.stopped_process (h : prog_measurable f u) (hτ : is_stopping_time f τ) :
prog_measurable f (stopped_process u τ) :=
h.comp (prog_measurable_min_stopping_time hτ) (λ i x, min_le_left _ _)
lemma prog_measurable.adapted_stopped_process
(h : prog_measurable f u) (hτ : is_stopping_time f τ) :
adapted f (stopped_process u τ) :=
(h.stopped_process hτ).adapted
lemma prog_measurable.strongly_measurable_stopped_process
(hu : prog_measurable f u) (hτ : is_stopping_time f τ) (i : ι) :
strongly_measurable (stopped_process u τ i) :=
(hu.adapted_stopped_process hτ i).mono (f.le _)
end prog_measurable
end linear_order
section nat
/-! ### Filtrations indexed by `ℕ` -/
open filtration
variables {f : filtration ℕ m} {u : ℕ → α → β} {τ π : α → ℕ}
lemma stopped_value_sub_eq_sum [add_comm_group β] (hle : τ ≤ π) :
stopped_value u π - stopped_value u τ =
λ x, (∑ i in finset.Ico (τ x) (π x), (u (i + 1) - u i)) x :=
begin
ext x,
rw [finset.sum_Ico_eq_sub _ (hle x), finset.sum_range_sub, finset.sum_range_sub],
simp [stopped_value],
end
lemma stopped_value_sub_eq_sum' [add_comm_group β] (hle : τ ≤ π) {N : ℕ} (hbdd : ∀ x, π x ≤ N) :
stopped_value u π - stopped_value u τ =
λ x, (∑ i in finset.range (N + 1),
set.indicator {x | τ x ≤ i ∧ i < π x} (u (i + 1) - u i)) x :=
begin
rw stopped_value_sub_eq_sum hle,
ext x,
simp only [finset.sum_apply, finset.sum_indicator_eq_sum_filter],
refine finset.sum_congr _ (λ _ _, rfl),
ext i,
simp only [finset.mem_filter, set.mem_set_of_eq, finset.mem_range, finset.mem_Ico],
exact ⟨λ h, ⟨lt_trans h.2 (nat.lt_succ_iff.2 $ hbdd _), h⟩, λ h, h.2⟩
end
section add_comm_monoid
variables [add_comm_monoid β]
/-- For filtrations indexed by `ℕ`, `adapted` and `prog_measurable` are equivalent. This lemma
provides `adapted f u → prog_measurable f u`. See `prog_measurable.adapted` for the reverse
direction, which is true more generally. -/
lemma adapted.prog_measurable_of_nat [topological_space β] [has_continuous_add β]
(h : adapted f u) : prog_measurable f u :=
begin
intro i,
have : (λ p : ↥(set.Iic i) × α, u ↑(p.fst) p.snd)
= λ p : ↥(set.Iic i) × α, ∑ j in finset.range (i + 1), if ↑p.fst = j then u j p.snd else 0,
{ ext1 p,
rw finset.sum_ite_eq,
have hp_mem : (p.fst : ℕ) ∈ finset.range (i + 1) := finset.mem_range_succ_iff.mpr p.fst.prop,
simp only [hp_mem, if_true], },
rw this,
refine finset.strongly_measurable_sum _ (λ j hj, strongly_measurable.ite _ _ _),
{ suffices h_meas : measurable[measurable_space.prod _ (f i)]
(λ a : ↥(set.Iic i) × α, (a.fst : ℕ)),
from h_meas (measurable_set_singleton j),
exact measurable_fst.subtype_coe, },
{ have h_le : j ≤ i, from finset.mem_range_succ_iff.mp hj,
exact (strongly_measurable.mono (h j) (f.mono h_le)).comp_measurable measurable_snd, },
{ exact strongly_measurable_const, },
end
/-- For filtrations indexed by `ℕ`, the stopped process obtained from an adapted process is
adapted. -/
lemma adapted.stopped_process_of_nat [topological_space β] [has_continuous_add β]
(hu : adapted f u) (hτ : is_stopping_time f τ) :
adapted f (stopped_process u τ) :=
(hu.prog_measurable_of_nat.stopped_process hτ).adapted
lemma adapted.strongly_measurable_stopped_process_of_nat [topological_space β]
[has_continuous_add β]
(hτ : is_stopping_time f τ) (hu : adapted f u) (n : ℕ) :
strongly_measurable (stopped_process u τ n) :=
hu.prog_measurable_of_nat.strongly_measurable_stopped_process hτ n
lemma stopped_value_eq {N : ℕ} (hbdd : ∀ x, τ x ≤ N) :
stopped_value u τ =
λ x, (∑ i in finset.range (N + 1), set.indicator {x | τ x = i} (u i)) x :=
begin
ext y,
rw [stopped_value, finset.sum_apply, finset.sum_eq_single (τ y)],
{ rw set.indicator_of_mem,
exact rfl },
{ exact λ i hi hneq, set.indicator_of_not_mem hneq.symm _ },
{ intro hy,
rw set.indicator_of_not_mem,
exact λ _, hy (finset.mem_range.2 $ lt_of_le_of_lt (hbdd _) (nat.lt_succ_self _)) }
end
lemma stopped_process_eq (n : ℕ) :
stopped_process u τ n =
set.indicator {a | n ≤ τ a} (u n) +
∑ i in finset.range n, set.indicator {a | τ a = i} (u i) :=
begin
ext x,
rw [pi.add_apply, finset.sum_apply],
cases le_or_lt n (τ x),
{ rw [stopped_process_eq_of_le h, set.indicator_of_mem, finset.sum_eq_zero, add_zero],
{ intros m hm,
rw finset.mem_range at hm,
exact set.indicator_of_not_mem ((lt_of_lt_of_le hm h).ne.symm) _ },
{ exact h } },
{ rw [stopped_process_eq_of_ge (le_of_lt h), finset.sum_eq_single_of_mem (τ x)],
{ rw [set.indicator_of_not_mem, zero_add, set.indicator_of_mem],
{ exact rfl }, -- refl does not work
{ exact not_le.2 h } },
{ rwa [finset.mem_range] },
{ intros b hb hneq,
rw set.indicator_of_not_mem,
exact hneq.symm } },
end
end add_comm_monoid
section normed_group
variables [normed_group β] {p : ℝ≥0∞} {μ : measure α}
lemma mem_ℒp_stopped_process (hτ : is_stopping_time f τ) (hu : ∀ n, mem_ℒp (u n) p μ) (n : ℕ) :
mem_ℒp (stopped_process u τ n) p μ :=
begin
rw stopped_process_eq,
refine mem_ℒp.add _ _,
{ exact mem_ℒp.indicator (f.le n {a : α | n ≤ τ a} (hτ.measurable_set_ge n)) (hu n) },
{ suffices : mem_ℒp (λ x, ∑ (i : ℕ) in finset.range n, {a : α | τ a = i}.indicator (u i) x) p μ,
{ convert this, ext1 x, simp only [finset.sum_apply] },
refine mem_ℒp_finset_sum _ (λ i hi, mem_ℒp.indicator _ (hu i)),
exact f.le i {a : α | τ a = i} (hτ.measurable_set_eq i) },
end
lemma integrable_stopped_process (hτ : is_stopping_time f τ)
(hu : ∀ n, integrable (u n) μ) (n : ℕ) :
integrable (stopped_process u τ n) μ :=
by { simp_rw ← mem_ℒp_one_iff_integrable at hu ⊢, exact mem_ℒp_stopped_process hτ hu n, }
lemma mem_ℒp_stopped_value (hτ : is_stopping_time f τ)
(hu : ∀ n, mem_ℒp (u n) p μ) {N : ℕ} (hbdd : ∀ x, τ x ≤ N) :
mem_ℒp (stopped_value u τ) p μ :=
begin
rw stopped_value_eq hbdd,
suffices : mem_ℒp (λ x, ∑ (i : ℕ) in finset.range (N + 1),
{a : α | τ a = i}.indicator (u i) x) p μ,
{ convert this, ext1 x, simp only [finset.sum_apply] },
refine mem_ℒp_finset_sum _ (λ i hi, mem_ℒp.indicator _ (hu i)),
exact f.le i {a : α | τ a = i} (hτ.measurable_set_eq i)
end
lemma integrable_stopped_value (hτ : is_stopping_time f τ)
(hu : ∀ n, integrable (u n) μ) {N : ℕ} (hbdd : ∀ x, τ x ≤ N) :
integrable (stopped_value u τ) μ :=
by { simp_rw ← mem_ℒp_one_iff_integrable at hu ⊢, exact mem_ℒp_stopped_value hτ hu hbdd, }
end normed_group
end nat
section piecewise_const
variables [preorder ι] {𝒢 : filtration ι m} {τ η : α → ι} {i j : ι} {s : set α}
[decidable_pred (∈ s)]
/-- Given stopping times `τ` and `η` which are bounded below, `set.piecewise s τ η` is also
a stopping time with respect to the same filtration. -/
lemma is_stopping_time.piecewise_of_le (hτ_st : is_stopping_time 𝒢 τ)
(hη_st : is_stopping_time 𝒢 η) (hτ : ∀ x, i ≤ τ x) (hη : ∀ x, i ≤ η x)
(hs : measurable_set[𝒢 i] s) :
is_stopping_time 𝒢 (s.piecewise τ η) :=
begin
intro n,
have : {x | s.piecewise τ η x ≤ n}
= (s ∩ {x | τ x ≤ n}) ∪ (sᶜ ∩ {x | η x ≤ n}),
{ ext1 x,
simp only [set.piecewise, set.mem_inter_eq, set.mem_set_of_eq, and.congr_right_iff],
by_cases hx : x ∈ s; simp [hx], },
rw this,
by_cases hin : i ≤ n,
{ have hs_n : measurable_set[𝒢 n] s, from 𝒢.mono hin _ hs,
exact (hs_n.inter (hτ_st n)).union (hs_n.compl.inter (hη_st n)), },
{ have hτn : ∀ x, ¬ τ x ≤ n := λ x hτn, hin ((hτ x).trans hτn),
have hηn : ∀ x, ¬ η x ≤ n := λ x hηn, hin ((hη x).trans hηn),
simp [hτn, hηn], },
end
lemma is_stopping_time_piecewise_const (hij : i ≤ j) (hs : measurable_set[𝒢 i] s) :
is_stopping_time 𝒢 (s.piecewise (λ _, i) (λ _, j)) :=
(is_stopping_time_const 𝒢 i).piecewise_of_le (is_stopping_time_const 𝒢 j)
(λ x, le_rfl) (λ _, hij) hs
lemma stopped_value_piecewise_const {ι' : Type*} {i j : ι'} {f : ι' → α → ℝ} :
stopped_value f (s.piecewise (λ _, i) (λ _, j)) = s.piecewise (f i) (f j) :=
by { ext x, rw stopped_value, by_cases hx : x ∈ s; simp [hx] }
lemma stopped_value_piecewise_const' {ι' : Type*} {i j : ι'} {f : ι' → α → ℝ} :
stopped_value f (s.piecewise (λ _, i) (λ _, j)) = s.indicator (f i) + sᶜ.indicator (f j) :=
by { ext x, rw stopped_value, by_cases hx : x ∈ s; simp [hx] }
end piecewise_const
end measure_theory
|
ef516b98f019dcd05a9ec1b6641073521e0fa9b2 | 947b78d97130d56365ae2ec264df196ce769371a | /src/Std/Data/RBMap.lean | 192660fec7df21e672f3c7f0609a47bf97c0e629 | [
"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 | 12,158 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
namespace Std
universes u v w w'
inductive Rbcolor
| red | black
inductive RBNode (α : Type u) (β : α → Type v)
| leaf : RBNode
| node (color : Rbcolor) (lchild : RBNode) (key : α) (val : β key) (rchild : RBNode) : RBNode
namespace RBNode
variables {α : Type u} {β : α → Type v} {σ : Type w}
open Std.Rbcolor Nat
def depth (f : Nat → Nat → Nat) : RBNode α β → Nat
| leaf => 0
| node _ l _ _ r => succ (f (depth l) (depth r))
protected def min : RBNode α β → Option (Sigma (fun k => β k))
| leaf => none
| node _ leaf k v _ => some ⟨k, v⟩
| node _ l k v _ => min l
protected def max : RBNode α β → Option (Sigma (fun k => β k))
| leaf => none
| node _ _ k v leaf => some ⟨k, v⟩
| node _ _ k v r => max r
@[specialize] def fold (f : σ → ∀ (k : α), β k → σ) : σ → RBNode α β → σ
| b, leaf => b
| b, node _ l k v r => fold (f (fold b l) k v) r
@[specialize] def foldM {m : Type w → Type w'} [Monad m] (f : σ → ∀ (k : α), β k → m σ) : σ → RBNode α β → m σ
| b, leaf => pure b
| b, node _ l k v r => do
b ← foldM b l;
b ← f b k v;
foldM b r
@[specialize] def revFold (f : σ → ∀ (k : α), β k → σ) : σ → RBNode α β → σ
| b, leaf => b
| b, node _ l k v r => revFold (f (revFold b r) k v) l
@[specialize] def all (p : ∀ k, β k → Bool) : RBNode α β → Bool
| leaf => true
| node _ l k v r => p k v && all l && all r
@[specialize] def any (p : ∀ k, β k → Bool) : RBNode α β → Bool
| leaf => false
| node _ l k v r => p k v || any l || any r
def singleton (k : α) (v : β k) : RBNode α β :=
node red leaf k v leaf
@[inline] def balance1 : ∀ a, β a → RBNode α β → RBNode α β → RBNode α β
| kv, vv, t, node _ (node red l kx vx r₁) ky vy r₂ => node red (node black l kx vx r₁) ky vy (node black r₂ kv vv t)
| kv, vv, t, node _ l₁ ky vy (node red l₂ kx vx r) => node red (node black l₁ ky vy l₂) kx vx (node black r kv vv t)
| kv, vv, t, node _ l ky vy r => node black (node red l ky vy r) kv vv t
| _, _, _, _ => leaf -- unreachable
@[inline] def balance2 : RBNode α β → ∀ a, β a → RBNode α β → RBNode α β
| t, kv, vv, node _ (node red l kx₁ vx₁ r₁) ky vy r₂ => node red (node black t kv vv l) kx₁ vx₁ (node black r₁ ky vy r₂)
| t, kv, vv, node _ l₁ ky vy (node red l₂ kx₂ vx₂ r₂) => node red (node black t kv vv l₁) ky vy (node black l₂ kx₂ vx₂ r₂)
| t, kv, vv, node _ l ky vy r => node black t kv vv (node red l ky vy r)
| _, _, _, _ => leaf -- unreachable
def isRed : RBNode α β → Bool
| node red _ _ _ _ => true
| _ => false
def isBlack : RBNode α β → Bool
| node black _ _ _ _ => true
| _ => false
section Insert
variables (lt : α → α → Bool)
@[specialize] def ins : RBNode α β → ∀ k, β k → RBNode α β
| leaf, kx, vx => node red leaf kx vx leaf
| node red a ky vy b, kx, vx =>
if lt kx ky then node red (ins a kx vx) ky vy b
else if lt ky kx then node red a ky vy (ins b kx vx)
else node red a kx vx b
| node black a ky vy b, kx, vx =>
if lt kx ky then
if isRed a then balance1 ky vy b (ins a kx vx)
else node black (ins a kx vx) ky vy b
else if lt ky kx then
if isRed b then balance2 a ky vy (ins b kx vx)
else node black a ky vy (ins b kx vx)
else
node black a kx vx b
def setBlack : RBNode α β → RBNode α β
| node _ l k v r => node black l k v r
| e => e
@[specialize] def insert (t : RBNode α β) (k : α) (v : β k) : RBNode α β :=
if isRed t then setBlack (ins lt t k v)
else ins lt t k v
end Insert
def balance₃ : RBNode α β → ∀ k, β k → RBNode α β → RBNode α β
| node red (node red a kx vx b) ky vy c, k, v, d => node red (node black a kx vx b) ky vy (node black c k v d)
| node red a kx vx (node red b ky vy c), k, v, d => node red (node black a kx vx b) ky vy (node black c k v d)
| a, k, v, node red b ky vy (node red c kz vz d) => node red (node black a k v b) ky vy (node black c kz vz d)
| a, k, v, node red (node red b ky vy c) kz vz d => node red (node black a k v b) ky vy (node black c kz vz d)
| l, k, v, r => node black l k v r
def setRed : RBNode α β → RBNode α β
| node _ a k v b => node red a k v b
| e => e
def balLeft : RBNode α β → ∀ k, β k → RBNode α β → RBNode α β
| node red a kx vx b, k, v, r => node red (node black a kx vx b) k v r
| l, k, v, node black a ky vy b => balance₃ l k v (node red a ky vy b)
| l, k, v, node red (node black a ky vy b) kz vz c => node red (node black l k v a) ky vy (balance₃ b kz vz (setRed c))
| l, k, v, r => node red l k v r -- unreachable
def balRight (l : RBNode α β) (k : α) (v : β k) (r : RBNode α β) : RBNode α β :=
match r with
| (node red b ky vy c) => node red l k v (node black b ky vy c)
| _ => match l with
| node black a kx vx b => balance₃ (node red a kx vx b) k v r
| node red a kx vx (node black b ky vy c) => node red (balance₃ (setRed a) kx vx b) ky vy (node black c k v r)
| _ => node red l k v r -- unreachable
-- TODO: use wellfounded recursion
partial def appendTrees : RBNode α β → RBNode α β → RBNode α β
| leaf, x => x
| x, leaf => x
| node red a kx vx b, node red c ky vy d =>
match appendTrees b c with
| node red b' kz vz c' => node red (node red a kx vx b') kz vz (node red c' ky vy d)
| bc => node red a kx vx (node red bc ky vy d)
| node black a kx vx b, node black c ky vy d =>
match appendTrees b c with
| node red b' kz vz c' => node red (node black a kx vx b') kz vz (node black c' ky vy d)
| bc => balLeft a kx vx (node black bc ky vy d)
| a, node red b kx vx c => node red (appendTrees a b) kx vx c
| node red a kx vx b, c => node red a kx vx (appendTrees b c)
section Erase
variables (lt : α → α → Bool)
@[specialize] def del (x : α) : RBNode α β → RBNode α β
| leaf => leaf
| node _ a y v b =>
if lt x y then
if a.isBlack then balLeft (del a) y v b
else node red (del a) y v b
else if lt y x then
if b.isBlack then balRight a y v (del b)
else node red a y v (del b)
else appendTrees a b
@[specialize] def erase (x : α) (t : RBNode α β) : RBNode α β :=
let t := del lt x t;
t.setBlack
end Erase
section Membership
variable (lt : α → α → Bool)
@[specialize] def findCore : RBNode α β → ∀ (k : α), Option (Sigma (fun k => β k))
| leaf, x => none
| node _ a ky vy b, x =>
if lt x ky then findCore a x
else if lt ky x then findCore b x
else some ⟨ky, vy⟩
@[specialize] def find {β : Type v} : RBNode α (fun _ => β) → α → Option β
| leaf, x => none
| node _ a ky vy b, x =>
if lt x ky then find a x
else if lt ky x then find b x
else some vy
@[specialize] def lowerBound : RBNode α β → α → Option (Sigma β) → Option (Sigma β)
| leaf, x, lb => lb
| node _ a ky vy b, x, lb =>
if lt x ky then lowerBound a x lb
else if lt ky x then lowerBound b x (some ⟨ky, vy⟩)
else some ⟨ky, vy⟩
end Membership
inductive WellFormed (lt : α → α → Bool) : RBNode α β → Prop
| leafWff : WellFormed leaf
| insertWff {n n' : RBNode α β} {k : α} {v : β k} : WellFormed n → n' = insert lt n k v → WellFormed n'
| eraseWff {n n' : RBNode α β} {k : α} : WellFormed n → n' = erase lt k n → WellFormed n'
end RBNode
open Std.RBNode
/- TODO(Leo): define dRBMap -/
def RBMap (α : Type u) (β : Type v) (lt : α → α → Bool) : Type (max u v) :=
{t : RBNode α (fun _ => β) // t.WellFormed lt }
@[inline] def mkRBMap (α : Type u) (β : Type v) (lt : α → α → Bool) : RBMap α β lt :=
⟨leaf, WellFormed.leafWff⟩
@[inline] def RBMap.empty {α : Type u} {β : Type v} {lt : α → α → Bool} : RBMap α β lt :=
mkRBMap _ _ _
instance (α : Type u) (β : Type v) (lt : α → α → Bool) : HasEmptyc (RBMap α β lt) :=
⟨RBMap.empty⟩
namespace RBMap
variables {α : Type u} {β : Type v} {σ : Type w} {lt : α → α → Bool}
def depth (f : Nat → Nat → Nat) (t : RBMap α β lt) : Nat :=
t.val.depth f
@[inline] def fold (f : σ → α → β → σ) : σ → RBMap α β lt → σ
| b, ⟨t, _⟩ => t.fold f b
@[inline] def revFold (f : σ → α → β → σ) : σ → RBMap α β lt → σ
| b, ⟨t, _⟩ => t.revFold f b
@[inline] def foldM {m : Type w → Type w'} [Monad m] (f : σ → α → β → m σ) : σ → RBMap α β lt → m σ
| b, ⟨t, _⟩ => t.foldM f b
@[inline] def forM {m : Type w → Type w'} [Monad m] (f : α → β → m PUnit) (t : RBMap α β lt) : m PUnit :=
t.foldM (fun _ k v => f k v) ⟨⟩
@[inline] def isEmpty : RBMap α β lt → Bool
| ⟨leaf, _⟩ => true
| _ => false
@[specialize] def toList : RBMap α β lt → List (α × β)
| ⟨t, _⟩ => t.revFold (fun ps k v => (k, v)::ps) []
@[inline] protected def min : RBMap α β lt → Option (α × β)
| ⟨t, _⟩ =>
match t.min with
| some ⟨k, v⟩ => some (k, v)
| none => none
@[inline] protected def max : RBMap α β lt → Option (α × β)
| ⟨t, _⟩ =>
match t.max with
| some ⟨k, v⟩ => some (k, v)
| none => none
instance [HasRepr α] [HasRepr β] : HasRepr (RBMap α β lt) :=
⟨fun t => "rbmapOf " ++ repr t.toList⟩
@[inline] def insert : RBMap α β lt → α → β → RBMap α β lt
| ⟨t, w⟩, k, v => ⟨t.insert lt k v, WellFormed.insertWff w rfl⟩
@[inline] def erase : RBMap α β lt → α → RBMap α β lt
| ⟨t, w⟩, k => ⟨t.erase lt k, WellFormed.eraseWff w rfl⟩
@[specialize] def ofList : List (α × β) → RBMap α β lt
| [] => mkRBMap _ _ _
| ⟨k,v⟩::xs => (ofList xs).insert k v
@[inline] def findCore? : RBMap α β lt → α → Option (Sigma (fun (k : α) => β))
| ⟨t, _⟩, x => t.findCore lt x
@[inline] def find? : RBMap α β lt → α → Option β
| ⟨t, _⟩, x => t.find lt x
@[inline] def findD (t : RBMap α β lt) (k : α) (v₀ : β) : β :=
(t.find? k).getD v₀
/-- (lowerBound k) retrieves the kv pair of the largest key smaller than or equal to `k`,
if it exists. -/
@[inline] def lowerBound : RBMap α β lt → α → Option (Sigma (fun (k : α) => β))
| ⟨t, _⟩, x => t.lowerBound lt x none
@[inline] def contains (t : RBMap α β lt) (a : α) : Bool :=
(t.find? a).isSome
@[inline] def fromList (l : List (α × β)) (lt : α → α → Bool) : RBMap α β lt :=
l.foldl (fun r p => r.insert p.1 p.2) (mkRBMap α β lt)
@[inline] def all : RBMap α β lt → (α → β → Bool) → Bool
| ⟨t, _⟩, p => t.all p
@[inline] def any : RBMap α β lt → (α → β → Bool) → Bool
| ⟨t, _⟩, p => t.any p
def size (m : RBMap α β lt) : Nat :=
m.fold (fun sz _ _ => sz+1) 0
def maxDepth (t : RBMap α β lt) : Nat :=
t.val.depth Nat.max
@[inline] def min! [Inhabited α] [Inhabited β] (t : RBMap α β lt) : α × β :=
match t.min with
| some p => p
| none => panic! "map is empty"
@[inline] def max! [Inhabited α] [Inhabited β] (t : RBMap α β lt) : α × β :=
match t.max with
| some p => p
| none => panic! "map is empty"
@[inline] def find! [Inhabited β] (t : RBMap α β lt) (k : α) : β :=
match t.find? k with
| some b => b
| none => panic! "key is not in the map"
end RBMap
def rbmapOf {α : Type u} {β : Type v} (l : List (α × β)) (lt : α → α → Bool) : RBMap α β lt :=
RBMap.fromList l lt
end Std
|
e842663a21c9fc6487c30ef74bba414ff2a31753 | 3ed5a65c1ab3ce5d1a094edce8fa3287980f197b | /src/herstein/ex2_5/Q_24.lean | 34b74d6d63652f790de11bc2b03b535b415176c1 | [] | no_license | group-study-group/herstein | 35d32e77158efa2cc303c84e1ee5e3bc80831137 | f5a1a72eb56fa19c19ece0cb3ab6cf7ffd161f66 | refs/heads/master | 1,586,202,191,519 | 1,548,969,759,000 | 1,548,969,759,000 | 157,746,953 | 0 | 0 | null | 1,542,412,901,000 | 1,542,302,366,000 | Lean | UTF-8 | Lean | false | false | 1,728 | lean | import algebra.group algebra.group_power
variables {G: Type*} (a b : G)
theorem Q_24 [group G] (H1 : ∀ a b : G, (a * b)^3 = a^3 * b^3) (H2 : ∀ x : G, x^3 = 1 → x = 1) :
∀ a b : G, a * b = b * a :=
λ a b : G,
have Lem1 : ∀ x y : G, (y * x)^2 = x^2 * y^2, from
λ x y : G,
have H10 : (a * b) * (a * b) * (a * b) = (a * a^2) * b^3, from sorry,
have H11 : a * (b * (a * b) * (a * b)) = a * (a^2 * b^3), from sorry,
have H12 : b * (a * b) * (a * b) = a^2 * (b^2 * b), from sorry,
have H13 : (b * (a * b) * a) * b = (a^2 * b^2) * b, from sorry,
have H14 : b * (a * b) * a = a^2 * b^2, from sorry,
sorry,
have Lem2 : b^2 * a^3 = a^3 * b^2, from
have H20 : a * ((b * a) * (b * a)) = a * (a^2 * b^2), from sorry,
have H21 : (a * b) * (a * b) * a = (a * a^2) * b^2, from sorry,
have H22 : (a * b)^2 * a = a^3 * b^2, from sorry,
have H23 : b^2 * a^2 * a = a^3 * b^2, from sorry,
sorry,
let h : G := a * b * a^(-1 : ℤ) * b^(-1 : ℤ) in
have Lem3 : (h^2)^3 = 1, from
calc (h^2)^3
= ((a * b * a^(-1 : ℤ) * b^(-1 : ℤ))^2)^3 : sorry
... = (b^(-2 : ℤ) * ((a * b * a^(-1 : ℤ))^2))^3 : sorry
... = (b^(-2 : ℤ) * (a^(-2 : ℤ)) * (a * b)^2)^3 : sorry
... = (b^(-2 : ℤ) * ((a^(-2 : ℤ)) * (a^2 * b^2)))^3 : sorry
... = ( b^(-2 : ℤ) * (a^(-2 : ℤ) * (b^2 * a^2)) )^3 : sorry
... = (b^(-2 : ℤ))^3 * (a^(-2 : ℤ))^3 * b^6 * a^6 : sorry
... = (b^(-3 : ℤ))^2 * (a^(-2 : ℤ))^3 * b^6 * a^6 : sorry
... = (a^(-2 : ℤ))^3 * (b^(-3 : ℤ))^2 * b^6 * a^6 : sorry
... = a^(-6 : ℤ) * b^(-6 : ℤ) * b^6 * a^6 : sorry
... = a^(-6 : ℤ) * b^(-6 : ℤ) * b^6 * a^6 : sorry
... = 1 : sorry,
have Lem4 : h^2 = 1, from sorry,
show a * b = b * a, from sorry
|
b631fd4e661e0f9e1074d032c59aff3514f593f0 | 367134ba5a65885e863bdc4507601606690974c1 | /src/algebra/category/Group/images.lean | 649e458a81b81ad3781bb3a7d91cf03bc8b7a117 | [
"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,297 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebra.category.Group.abelian
import category_theory.limits.shapes.images
import category_theory.limits.types
/-!
# The category of commutative additive groups has images.
Note that we don't need to register any of the constructions here as instances, because we get them
from the fact that `AddCommGroup` is an abelian category.
-/
open category_theory
open category_theory.limits
universe u
namespace AddCommGroup
-- Note that because `injective_of_mono` is currently only proved in `Type 0`,
-- we restrict to the lowest universe here for now.
variables {G H : AddCommGroup.{0}} (f : G ⟶ H)
local attribute [ext] subtype.ext_val
section -- implementation details of `has_image` for AddCommGroup; use the API, not these
/-- the image of a morphism in AddCommGroup is just the bundling of `add_monoid_hom.range f` -/
def image : AddCommGroup := AddCommGroup.of (add_monoid_hom.range f)
/-- the inclusion of `image f` into the target -/
def image.ι : image f ⟶ H := f.range.subtype
instance : mono (image.ι f) := concrete_category.mono_of_injective (image.ι f) subtype.val_injective
/-- the corestriction map to the image -/
def factor_thru_image : G ⟶ image f := f.to_range
lemma image.fac : factor_thru_image f ≫ image.ι f = f :=
by { ext, refl, }
local attribute [simp] image.fac
variables {f}
/-- the universal property for the image factorisation -/
noncomputable def image.lift (F' : mono_factorisation f) : image f ⟶ F'.I :=
{ to_fun :=
(λ x, F'.e (classical.indefinite_description _ x.2).1 : image f → F'.I),
map_zero' :=
begin
haveI := F'.m_mono,
apply injective_of_mono F'.m,
change (F'.e ≫ F'.m) _ = _,
rw [F'.fac, add_monoid_hom.map_zero],
exact (classical.indefinite_description (λ y, f y = 0) _).2,
end,
map_add' :=
begin
intros x y,
haveI := F'.m_mono,
apply injective_of_mono F'.m,
rw [add_monoid_hom.map_add],
change (F'.e ≫ F'.m) _ = (F'.e ≫ F'.m) _ + (F'.e ≫ F'.m) _,
rw [F'.fac],
rw (classical.indefinite_description (λ z, f z = _) _).2,
rw (classical.indefinite_description (λ z, f z = _) _).2,
rw (classical.indefinite_description (λ z, f z = _) _).2,
refl,
end,
}
lemma image.lift_fac (F' : mono_factorisation f) : image.lift F' ≫ F'.m = image.ι f :=
begin
ext x,
change (F'.e ≫ F'.m) _ = _,
rw [F'.fac, (classical.indefinite_description _ x.2).2],
refl,
end
end
/-- the factorisation of any morphism in AddCommGroup through a mono. -/
def mono_factorisation : mono_factorisation f :=
{ I := image f,
m := image.ι f,
e := factor_thru_image f }
/-- the factorisation of any morphism in AddCommGroup through a mono has the universal property of
the image. -/
noncomputable def is_image : is_image (mono_factorisation f) :=
{ lift := image.lift,
lift_fac' := image.lift_fac }
/--
The categorical image of a morphism in `AddCommGroup`
agrees with the usual group-theoretical range.
-/
noncomputable def image_iso_range {G H : AddCommGroup.{0}} (f : G ⟶ H) :
limits.image f ≅ AddCommGroup.of f.range :=
is_image.iso_ext (image.is_image f) (is_image f)
end AddCommGroup
|
35b3dc03375d10606327c3a3e0134975cc5cc29c | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /src/Lean/Data/SMap.lean | 07021ca686dc5a75b7a5ac0dc12e8a72a246c0ff | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 3,710 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Std.Data.HashMap
import Std.Data.PersistentHashMap
universes u v w w'
namespace Lean
open Std (HashMap PHashMap)
/- Staged map for implementing the Environment. The idea is to store
imported entries into a hashtable and local entries into a persistent hashtable.
Hypotheses:
- The number of entries (i.e., declarations) coming from imported files is much bigger than
the number of entries in the current file.
- HashMap is faster than PersistentHashMap.
- When we are reading imported files, we have exclusive access to the map, and efficient
destructive updates are performed.
Remarks:
- We never remove declarations from the Environment. In principle, we could support
deletion by using `(PHashMap α (Option β))` where the value `none` would indicate
that an entry was "removed" from the hashtable.
- We do not need additional bookkeeping for extracting the local entries.
-/
structure SMap (α : Type u) (β : Type v) [BEq α] [Hashable α] where
stage₁ : Bool := true
map₁ : HashMap α β := {}
map₂ : PHashMap α β := {}
namespace SMap
variable {α : Type u} {β : Type v} [BEq α] [Hashable α]
instance : Inhabited (SMap α β) := ⟨{}⟩
def empty : SMap α β := {}
@[specialize] def insert : SMap α β → α → β → SMap α β
| ⟨true, m₁, m₂⟩, k, v => ⟨true, m₁.insert k v, m₂⟩
| ⟨false, m₁, m₂⟩, k, v => ⟨false, m₁, m₂.insert k v⟩
@[specialize] def find? : SMap α β → α → Option β
| ⟨true, m₁, _⟩, k => m₁.find? k
| ⟨false, m₁, m₂⟩, k => (m₂.find? k).orElse (m₁.find? k)
@[inline] def findD (m : SMap α β) (a : α) (b₀ : β) : β :=
(m.find? a).getD b₀
@[inline] def find! [Inhabited β] (m : SMap α β) (a : α) : β :=
match m.find? a with
| some b => b
| none => panic! "key is not in the map"
@[specialize] def contains : SMap α β → α → Bool
| ⟨true, m₁, _⟩, k => m₁.contains k
| ⟨false, m₁, m₂⟩, k => m₁.contains k || m₂.contains k
/- Similar to `find?`, but searches for result in the hashmap first.
So, the result is correct only if we never "overwrite" `map₁` entries using `map₂`. -/
@[specialize] def find?' : SMap α β → α → Option β
| ⟨true, m₁, _⟩, k => m₁.find? k
| ⟨false, m₁, m₂⟩, k => (m₁.find? k).orElse (m₂.find? k)
def forM [Monad m] (s : SMap α β) (f : α → β → m PUnit) : m PUnit := do
s.map₁.forM f
s.map₂.forM f
/- Move from stage 1 into stage 2. -/
def switch (m : SMap α β) : SMap α β :=
if m.stage₁ then { m with stage₁ := false } else m
@[inline] def foldStage2 {σ : Type w} (f : σ → α → β → σ) (s : σ) (m : SMap α β) : σ :=
m.map₂.foldl f s
def fold {σ : Type w} (f : σ → α → β → σ) (init : σ) (m : SMap α β) : σ :=
m.map₂.foldl f $ m.map₁.fold f init
def size (m : SMap α β) : Nat :=
m.map₁.size + m.map₂.size
def stageSizes (m : SMap α β) : Nat × Nat :=
(m.map₁.size, m.map₂.size)
def numBuckets (m : SMap α β) : Nat :=
m.map₁.numBuckets
def toList (m : SMap α β) : List (α × β) :=
m.fold (init := []) fun es a b => (a, b)::es
end SMap
def List.toSMap [BEq α] [Hashable α] (es : List (α × β)) : SMap α β :=
es.foldl (init := {}) fun s (a, b) => s.insert a b
instance {_ : BEq α} {_ : Hashable α} [Repr α] [Repr β] : Repr (SMap α β) where
reprPrec v prec := Repr.addAppParen (reprArg v.toList ++ ".toSMap") prec
end Lean
|
3cbfdbf759caf1f3cfc6646800bd55806a0f4263 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/tactic/omega/find_scalars.lean | 8b96215763e2460fe388ab4651a13afcbe9a05eb | [] | 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 | 607 | lean | /-
Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Seul Baek
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.tactic.omega.term
import Mathlib.data.list.min_max
import Mathlib.PostPort
namespace Mathlib
/-
Tactic for performing Fourier–Motzkin elimination to find
a contradictory linear combination of input constraints.
-/
namespace omega
/-- Divide linear combinations into three groups by the coefficient of the
`m`th variable in their resultant terms: negative, zero, or positive. -/
|
a0ee72e9b0d84e9a708a7f59d0aba0efc84c6b38 | f618aea02cb4104ad34ecf3b9713065cc0d06103 | /src/topology/instances/real.lean | 1099fa5c600d16ed58802acf100da220645d2007 | [
"Apache-2.0"
] | permissive | joehendrix/mathlib | 84b6603f6be88a7e4d62f5b1b0cbb523bb82b9a5 | c15eab34ad754f9ecd738525cb8b5a870e834ddc | refs/heads/master | 1,589,606,591,630 | 1,555,946,393,000 | 1,555,946,393,000 | 182,813,854 | 0 | 0 | null | 1,555,946,309,000 | 1,555,946,308,000 | null | UTF-8 | Lean | false | false | 19,045 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
The real numbers ℝ.
They are constructed as the topological completion of ℚ. With the following steps:
(1) prove that ℚ forms a uniform space.
(2) subtraction and addition are uniform continuous functions in this space
(3) for multiplication and inverse this only holds on bounded subsets
(4) ℝ is defined as separated Cauchy filters over ℚ (the separation requires a quotient construction)
(5) extend the uniform continuous functions along the completion
(6) proof field properties using the principle of extension of identities
TODO
generalizations:
* topological groups & rings
* order topologies
* Archimedean fields
-/
import logic.function topology.metric_space.basic topology.algebra.uniform_group
topology.algebra.ring tactic.linarith
noncomputable theory
open classical set lattice filter topological_space metric
local attribute [instance] prop_decidable
local attribute [instance, priority 0] nat.cast_coe
local attribute [instance, priority 0] int.cast_coe
local attribute [instance, priority 0] rat.cast_coe
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
instance : metric_space ℚ :=
metric_space.induced coe rat.cast_injective real.metric_space
theorem rat.dist_eq (x y : ℚ) : dist x y = abs (x - y) := rfl
instance : metric_space ℤ :=
begin
letI M := metric_space.induced coe int.cast_injective real.metric_space,
refine @metric_space.replace_uniformity _ int.uniform_space M
(le_antisymm refl_le_uniformity $ λ r ru,
mem_uniformity_dist.2 ⟨1, zero_lt_one, λ a b h,
mem_principal_sets.1 ru $ dist_le_zero.1 (_ : (abs (a - b) : ℝ) ≤ 0)⟩),
simpa using (@int.cast_le ℝ _ _ 0).2 (int.lt_add_one_iff.1 $
(@int.cast_lt ℝ _ (abs (a - b)) 1).1 $ by simpa using h)
end
theorem uniform_continuous_of_rat : uniform_continuous (coe : ℚ → ℝ) :=
uniform_continuous_comap
theorem uniform_embedding_of_rat : uniform_embedding (coe : ℚ → ℝ) :=
uniform_embedding_comap rat.cast_injective
theorem dense_embedding_of_rat : dense_embedding (coe : ℚ → ℝ) :=
uniform_embedding_of_rat.dense_embedding $
λ x, mem_closure_iff_nhds.2 $ λ t ht,
let ⟨ε,ε0, hε⟩ := mem_nhds_iff.1 ht in
let ⟨q, h⟩ := exists_rat_near x ε0 in
ne_empty_iff_exists_mem.2 ⟨_, hε (mem_ball'.2 h), q, rfl⟩
theorem embedding_of_rat : embedding (coe : ℚ → ℝ) := dense_embedding_of_rat.embedding
theorem continuous_of_rat : continuous (coe : ℚ → ℝ) := uniform_continuous_of_rat.continuous
theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in
⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩
-- TODO(Mario): Find a way to use rat_add_continuous_lemma
theorem rat.uniform_continuous_add : uniform_continuous (λp : ℚ × ℚ, p.1 + p.2) :=
uniform_embedding_of_rat.uniform_continuous_iff.2 $ by simp [(∘)]; exact
((uniform_continuous_fst.comp uniform_continuous_of_rat).prod_mk
(uniform_continuous_snd.comp uniform_continuous_of_rat)).comp real.uniform_continuous_add
theorem real.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℝ _) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h,
by rw dist_comm at h; simpa [real.dist_eq] using h⟩
theorem rat.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℚ _) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h,
by rw dist_comm at h; simpa [rat.dist_eq] using h⟩
instance : uniform_add_group ℝ :=
uniform_add_group.mk' real.uniform_continuous_add real.uniform_continuous_neg
instance : uniform_add_group ℚ :=
uniform_add_group.mk' rat.uniform_continuous_add rat.uniform_continuous_neg
instance : topological_add_group ℝ := by apply_instance
instance : topological_add_group ℚ := by apply_instance
instance : orderable_topology ℚ :=
induced_orderable_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _)
lemma real.is_topological_basis_Ioo_rat :
@is_topological_basis ℝ _ (⋃(a b : ℚ) (h : a < b), {Ioo a b}) :=
is_topological_basis_of_open_of_nhds
(by simp [is_open_Ioo] {contextual:=tt})
(assume a v hav hv,
let ⟨l, u, hl, hu, h⟩ := (mem_nhds_unbounded (no_top _) (no_bot _)).mp (mem_nhds_sets hv hav),
⟨q, hlq, hqa⟩ := exists_rat_btwn hl,
⟨p, hap, hpu⟩ := exists_rat_btwn hu in
⟨Ioo q p,
by simp; exact ⟨q, p, rat.cast_lt.1 $ lt_trans hqa hap, rfl⟩,
⟨hqa, hap⟩, assume a' ⟨hqa', ha'p⟩, h _ (lt_trans hlq hqa') (lt_trans ha'p hpu)⟩)
instance : second_countable_topology ℝ :=
⟨⟨(⋃(a b : ℚ) (h : a < b), {Ioo a b}),
by simp [countable_Union, countable_Union_Prop],
real.is_topological_basis_Ioo_rat.2.2⟩⟩
/- TODO(Mario): Prove that these are uniform isomorphisms instead of uniform embeddings
lemma uniform_embedding_add_rat {r : ℚ} : uniform_embedding (λp:ℚ, p + r) :=
_
lemma uniform_embedding_mul_rat {q : ℚ} (hq : q ≠ 0) : uniform_embedding ((*) q) :=
_ -/
lemma real.uniform_continuous_inv (s : set ℝ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) :
uniform_continuous (λp:s, p.1⁻¹) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in
⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩
lemma real.uniform_continuous_abs : uniform_continuous (abs : ℝ → ℝ) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _)⟩
lemma real.continuous_abs : continuous (abs : ℝ → ℝ) :=
real.uniform_continuous_abs.continuous
lemma rat.uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
⟨ε, ε0, λ a b h, lt_of_le_of_lt
(by simpa [rat.dist_eq] using abs_abs_sub_abs_le_abs_sub _ _) h⟩
lemma rat.continuous_abs : continuous (abs : ℚ → ℚ) :=
rat.uniform_continuous_abs.continuous
lemma real.tendsto_inv {r : ℝ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (nhds r) (nhds r⁻¹) :=
by rw ← abs_pos_iff at r0; exact
tendsto_of_uniform_continuous_subtype
(real.uniform_continuous_inv {x | abs r / 2 < abs x} (half_pos r0) (λ x h, le_of_lt h))
(mem_nhds_sets (real.continuous_abs _ $ is_open_lt' (abs r / 2)) (half_lt_self r0))
lemma real.continuous_inv' : continuous (λa:{r:ℝ // r ≠ 0}, a.val⁻¹) :=
continuous_iff_continuous_at.mpr $ assume ⟨r, hr⟩,
(continuous_iff_continuous_at.mp continuous_subtype_val _).comp (real.tendsto_inv hr)
lemma real.continuous_inv [topological_space α] {f : α → ℝ} (h : ∀a, f a ≠ 0) (hf : continuous f) :
continuous (λa, (f a)⁻¹) :=
show continuous ((has_inv.inv ∘ @subtype.val ℝ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩),
from (continuous_subtype_mk _ hf).comp real.continuous_inv'
lemma real.uniform_continuous_mul_const {x : ℝ} : uniform_continuous ((*) x) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, begin
cases no_top (abs x) with y xy,
have y0 := lt_of_le_of_lt (abs_nonneg _) xy,
refine ⟨_, div_pos ε0 y0, λ a b h, _⟩,
rw [real.dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)],
exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0
end
lemma real.uniform_continuous_mul (s : set (ℝ × ℝ))
{r₁ r₂ : ℝ} (r₁0 : 0 < r₁) (r₂0 : 0 < r₂)
(H : ∀ x ∈ s, abs (x : ℝ × ℝ).1 < r₁ ∧ abs x.2 < r₂) :
uniform_continuous (λp:s, p.1.1 * p.1.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 r₁0 r₂0 in
⟨δ, δ0, λ a b h,
let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩
protected lemma real.continuous_mul : continuous (λp : ℝ × ℝ, p.1 * p.2) :=
continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩,
tendsto_of_uniform_continuous_subtype
(real.uniform_continuous_mul
({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1})
(lt_of_le_of_lt (abs_nonneg _) (lt_add_one _))
(lt_of_le_of_lt (abs_nonneg _) (lt_add_one _))
(λ x, id))
(mem_nhds_sets
(is_open_prod
(real.continuous_abs _ $ is_open_gt' (abs a₁ + 1))
(real.continuous_abs _ $ is_open_gt' (abs a₂ + 1)))
⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩)
instance : topological_ring ℝ :=
{ continuous_mul := real.continuous_mul, ..real.topological_add_group }
instance : topological_semiring ℝ := by apply_instance
lemma rat.continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) :=
embedding_of_rat.continuous_iff.2 $ by simp [(∘)]; exact
((continuous_fst.comp continuous_of_rat).prod_mk
(continuous_snd.comp continuous_of_rat)).comp real.continuous_mul
instance : topological_ring ℚ :=
{ continuous_mul := rat.continuous_mul, ..rat.topological_add_group }
theorem real.ball_eq_Ioo (x ε : ℝ) : ball x ε = Ioo (x - ε) (x + ε) :=
set.ext $ λ y, by rw [mem_ball, real.dist_eq,
abs_sub_lt_iff, sub_lt_iff_lt_add', and_comm, sub_lt]; refl
theorem real.Ioo_eq_ball (x y : ℝ) : Ioo x y = ball ((x + y) / 2) ((y - x) / 2) :=
by rw [real.ball_eq_Ioo, ← sub_div, add_comm, ← sub_add,
add_sub_cancel', add_self_div_two, ← add_div,
add_assoc, add_sub_cancel'_right, add_self_div_two]
lemma real.totally_bounded_Ioo (a b : ℝ) : totally_bounded (Ioo a b) :=
metric.totally_bounded_iff.2 $ λ ε ε0, begin
rcases exists_nat_gt ((b - a) / ε) with ⟨n, ba⟩,
rw [div_lt_iff' ε0, sub_lt_iff_lt_add'] at ba,
let s := (λ i:ℕ, a + ε * i) '' {i:ℕ | i < n},
refine ⟨s, finite_image _ ⟨set.fintype_lt_nat _⟩, λ x h, _⟩,
rcases h with ⟨ax, xb⟩,
let i : ℕ := ⌊(x - a) / ε⌋.to_nat,
have : (i : ℤ) = ⌊(x - a) / ε⌋ :=
int.to_nat_of_nonneg (floor_nonneg.2 $ le_of_lt (div_pos (sub_pos.2 ax) ε0)),
simp, refine ⟨_, ⟨i, _, rfl⟩, _⟩,
{ rw [← int.coe_nat_lt, this],
refine int.cast_lt.1 (lt_of_le_of_lt (floor_le _) _),
rw [int.cast_coe_nat, div_lt_iff' ε0, sub_lt_iff_lt_add'],
exact lt_trans xb ba },
{ rw [real.dist_eq, ← int.cast_coe_nat, this, abs_of_nonneg,
← sub_sub, sub_lt_iff_lt_add'],
{ have := lt_floor_add_one ((x - a) / ε),
rwa [div_lt_iff' ε0, mul_add, mul_one] at this },
{ have := floor_le ((x - a) / ε),
rwa [ge, sub_nonneg, ← le_sub_iff_add_le', ← le_div_iff' ε0] } }
end
lemma real.totally_bounded_ball (x ε : ℝ) : totally_bounded (ball x ε) :=
by rw real.ball_eq_Ioo; apply real.totally_bounded_Ioo
lemma real.totally_bounded_Ico (a b : ℝ) : totally_bounded (Ico a b) :=
let ⟨c, ac⟩ := no_bot a in totally_bounded_subset
(by exact λ x ⟨h₁, h₂⟩, ⟨lt_of_lt_of_le ac h₁, h₂⟩)
(real.totally_bounded_Ioo c b)
lemma real.totally_bounded_Icc (a b : ℝ) : totally_bounded (Icc a b) :=
let ⟨c, bc⟩ := no_top b in totally_bounded_subset
(by exact λ x ⟨h₁, h₂⟩, ⟨h₁, lt_of_le_of_lt h₂ bc⟩)
(real.totally_bounded_Ico a c)
lemma rat.totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) :=
begin
have := totally_bounded_preimage uniform_embedding_of_rat (real.totally_bounded_Icc a b),
rwa (set.ext (λ q, _) : Icc _ _ = _), simp
end
-- TODO(Mario): Generalize to first-countable uniform spaces?
instance : complete_space ℝ :=
⟨λ f cf, begin
let g : ℕ → {ε:ℝ//ε>0} := λ n, ⟨n.to_pnat'⁻¹, inv_pos (nat.cast_pos.2 n.to_pnat'.pos)⟩,
choose S hS hS_dist using show ∀n:ℕ, ∃t ∈ f.sets, ∀ x y ∈ t, dist x y < g n, from
assume n, let ⟨t, tf, h⟩ := (metric.cauchy_iff.1 cf).2 (g n).1 (g n).2 in ⟨t, tf, h⟩,
let F : ℕ → set ℝ := λn, ⋂i≤n, S i,
have hF : ∀n, F n ∈ f.sets := assume n, Inter_mem_sets (finite_le_nat n) (λ i _, hS i),
have hF_dist : ∀n, ∀ x y ∈ F n, dist x y < g n :=
assume n x y hx hy,
have F n ⊆ S n := bInter_subset_of_mem (le_refl n),
(hS_dist n) _ _ (this hx) (this hy),
choose G hG using assume n:ℕ, inhabited_of_mem_sets cf.1 (hF n),
have hg : ∀ ε > 0, ∃ n, ∀ j ≥ n, (g j : ℝ) < ε,
{ intros ε ε0,
cases exists_nat_gt ε⁻¹ with n hn,
refine ⟨n, λ j nj, _⟩,
have hj := lt_of_lt_of_le hn (nat.cast_le.2 nj),
have j0 := lt_trans (inv_pos ε0) hj,
have jε := (inv_lt j0 ε0).2 hj,
rwa ← pnat.to_pnat'_coe (nat.cast_pos.1 j0) at jε },
let c : cau_seq ℝ abs,
{ refine ⟨λ n, G n, λ ε ε0, _⟩,
cases hg _ ε0 with n hn,
refine ⟨n, λ j jn, _⟩,
have : F j ⊆ F n :=
bInter_subset_bInter_left (λ i h, @le_trans _ _ i n j h jn),
exact lt_trans (hF_dist n _ _ (this (hG j)) (hG n)) (hn _ $ le_refl _) },
refine ⟨cau_seq.lim c, λ s h, _⟩,
rcases metric.mem_nhds_iff.1 h with ⟨ε, ε0, hε⟩,
cases exists_forall_ge_and (hg _ $ half_pos ε0)
(cau_seq.equiv_lim c _ $ half_pos ε0) with n hn,
cases hn _ (le_refl _) with h₁ h₂,
refine sets_of_superset _ (hF n) (subset.trans _ $
subset.trans (ball_half_subset (G n) h₂) hε),
exact λ x h, lt_trans ((hF_dist n) x (G n) h (hG n)) h₁
end⟩
lemma tendsto_coe_nat_real_at_top_iff {f : α → ℕ} {l : filter α} :
tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top :=
tendsto_at_top_embedding (assume a₁ a₂, nat.cast_le) $
assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨n, le_of_lt hn⟩
lemma tendsto_coe_nat_real_at_top_at_top : tendsto (coe : ℕ → ℝ) at_top at_top :=
tendsto_coe_nat_real_at_top_iff.2 tendsto_id
lemma tendsto_coe_int_real_at_top_iff {f : α → ℤ} {l : filter α} :
tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top :=
tendsto_at_top_embedding (assume a₁ a₂, int.cast_le) $
assume r, let ⟨n, hn⟩ := exists_nat_gt r in
⟨(n:ℤ), le_of_lt $ by rwa [int.cast_coe_nat]⟩
lemma tendsto_coe_int_real_at_top_at_top : tendsto (coe : ℤ → ℝ) at_top at_top :=
tendsto_coe_int_real_at_top_iff.2 tendsto_id
section
lemma closure_of_rat_image_lt {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q < x}) = {r | ↑q ≤ r} :=
subset.antisymm
((closure_subset_iff_subset_of_is_closed (is_closed_ge' _)).2
(image_subset_iff.2 $ λ p h, le_of_lt $ (@rat.cast_lt ℝ _ _ _).2 h)) $
λ x hx, mem_closure_iff_nhds.2 $ λ t ht,
let ⟨ε, ε0, hε⟩ := metric.mem_nhds_iff.1 ht in
let ⟨p, h₁, h₂⟩ := exists_rat_btwn ((lt_add_iff_pos_right x).2 ε0) in
ne_empty_iff_exists_mem.2 ⟨_, hε (show abs _ < _,
by rwa [abs_of_nonneg (le_of_lt $ sub_pos.2 h₁), sub_lt_iff_lt_add']),
p, rat.cast_lt.1 (@lt_of_le_of_lt ℝ _ _ _ _ hx h₁), rfl⟩
/- TODO(Mario): Put these back only if needed later
lemma closure_of_rat_image_le_eq {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q ≤ x}) = {r | ↑q ≤ r} :=
_
lemma closure_of_rat_image_le_le_eq {a b : ℚ} (hab : a ≤ b) :
closure (of_rat '' {q:ℚ | a ≤ q ∧ q ≤ b}) = {r:ℝ | of_rat a ≤ r ∧ r ≤ of_rat b} :=
_-/
lemma compact_Icc {a b : ℝ} : compact (Icc a b) :=
compact_of_totally_bounded_is_closed
(real.totally_bounded_Icc a b)
(is_closed_inter (is_closed_ge' a) (is_closed_le' b))
instance : proper_space ℝ :=
{ compact_ball := λx r, by rw closed_ball_Icc; apply compact_Icc }
open real
lemma real.intermediate_value {f : ℝ → ℝ} {a b t : ℝ}
(hf : ∀ x, a ≤ x → x ≤ b → tendsto f (nhds x) (nhds (f x)))
(ha : f a ≤ t) (hb : t ≤ f b) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t :=
let x := real.Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} in
have hx₁ : ∃ y, ∀ g ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}, g ≤ y := ⟨b, λ _ h, h.2.2⟩,
have hx₂ : ∃ y, y ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} := ⟨a, ha, le_refl _, hab⟩,
have hax : a ≤ x, from le_Sup _ hx₁ ⟨ha, le_refl _, hab⟩,
have hxb : x ≤ b, from (Sup_le _ hx₂ hx₁).2 (λ _ h, h.2.2),
⟨x, hax, hxb,
eq_of_forall_dist_le $ λ ε ε0,
let ⟨δ, hδ0, hδ⟩ := metric.tendsto_nhds_nhds.1 (hf _ hax hxb) ε ε0 in
(le_total t (f x)).elim
(λ h, le_of_not_gt $ λ hfε, begin
rw [dist_eq, abs_of_nonneg (sub_nonneg.2 h)] at hfε,
refine mt (Sup_le {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} hx₂ hx₁).2
(not_le_of_gt (sub_lt_self x (half_pos hδ0)))
(λ g hg, le_of_not_gt
(λ hgδ, not_lt_of_ge hg.1
(lt_trans (lt_sub.1 hfε) (sub_lt_of_sub_lt
(lt_of_le_of_lt (le_abs_self _) _))))),
rw abs_sub,
exact hδ (abs_sub_lt_iff.2 ⟨lt_of_le_of_lt (sub_nonpos.2 (le_Sup _ hx₁ hg)) hδ0,
by simp only [x] at *; linarith⟩)
end)
(λ h, le_of_not_gt $ λ hfε, begin
rw [dist_eq, abs_of_nonpos (sub_nonpos.2 h)] at hfε,
exact mt (le_Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b})
(λ h : ∀ k, k ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} → k ≤ x,
not_le_of_gt ((lt_add_iff_pos_left x).2 (half_pos hδ0))
(h _ ⟨le_trans (le_sub_iff_add_le.2 (le_trans (le_abs_self _)
(le_of_lt (hδ $ by rw [dist_eq, add_sub_cancel, abs_of_nonneg (le_of_lt (half_pos hδ0))];
exact half_lt_self hδ0))))
(by linarith),
le_trans hax (le_of_lt ((lt_add_iff_pos_left _).2 (half_pos hδ0))),
le_of_not_gt (λ hδy, not_lt_of_ge hb (lt_of_le_of_lt
(show f b ≤ f b - f x - ε + t, by linarith)
(add_lt_of_neg_of_le
(sub_neg_of_lt (lt_of_le_of_lt (le_abs_self _)
(@hδ b (abs_sub_lt_iff.2 ⟨by simp only [x] at *; linarith,
by linarith⟩))))
(le_refl _))))⟩))
hx₁
end)⟩
lemma real.intermediate_value' {f : ℝ → ℝ} {a b t : ℝ}
(hf : ∀ x, a ≤ x → x ≤ b → tendsto f (nhds x) (nhds (f x)))
(ha : t ≤ f a) (hb : f b ≤ t) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t :=
let ⟨x, hx₁, hx₂, hx₃⟩ := @real.intermediate_value
(λ x, - f x) a b (-t) (λ x hax hxb, tendsto_neg (hf x hax hxb))
(neg_le_neg ha) (neg_le_neg hb) hab in
⟨x, hx₁, hx₂, neg_inj hx₃⟩
lemma real.bounded_iff_bdd_below_bdd_above {s : set ℝ} : bounded s ↔ bdd_below s ∧ bdd_above s :=
⟨begin
assume bdd,
rcases (bounded_iff_subset_ball 0).1 bdd with ⟨r, hr⟩, -- hr : s ⊆ closed_ball 0 r
rw closed_ball_Icc at hr, -- hr : s ⊆ Icc (0 - r) (0 + r)
exact ⟨⟨-r, λy hy, by simpa using (hr hy).1⟩, ⟨r, λy hy, by simpa using (hr hy).2⟩⟩
end,
begin
rintros ⟨⟨m, hm⟩, ⟨M, hM⟩⟩,
have I : s ⊆ Icc m M := λx hx, ⟨hm x hx, hM x hx⟩,
have : Icc m M = closed_ball ((m+M)/2) ((M-m)/2) :=
by rw closed_ball_Icc; congr; ring,
rw this at I,
exact bounded.subset I bounded_closed_ball
end⟩
end
|
753236e077238dc7da195f6a2e8fba5935709831 | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch3/ex0209.lean | 6480894a5913f9012f2745de5b92f00077131850 | [] | no_license | Ailrun/Theorem_Proving_in_Lean | ae6a23f3c54d62d401314d6a771e8ff8b4132db2 | 2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68 | refs/heads/master | 1,609,838,270,467 | 1,586,846,743,000 | 1,586,846,743,000 | 240,967,761 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 86 | lean | theorem t1 : ∀ (p q : Prop), p → q → p := λ (p q : Prop) (hp : p) (hq : q), hp
|
3b3fdca2eadc6efbfbe8439627f9951850212bc9 | 92b50235facfbc08dfe7f334827d47281471333b | /hott/hit/two_quotient.hlean | b44a5ee944e2199ff13e59952c4acd3877038c04 | [
"Apache-2.0"
] | permissive | htzh/lean | 24f6ed7510ab637379ec31af406d12584d31792c | d70c79f4e30aafecdfc4a60b5d3512199200ab6e | refs/heads/master | 1,607,677,731,270 | 1,437,089,952,000 | 1,437,089,952,000 | 37,078,816 | 0 | 0 | null | 1,433,780,956,000 | 1,433,780,955,000 | null | UTF-8 | Lean | false | false | 14,333 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import hit.circle types.eq2 algebra.e_closure
open quotient eq circle sum sigma equiv function relation
namespace simple_two_quotient
section
parameters {A : Type}
(R : A → A → Type)
local abbreviation T := e_closure R -- the (type-valued) equivalence closure of R
parameter (Q : Π⦃a⦄, T a a → Type)
variables ⦃a a' : A⦄ {s : R a a'} {r : T a a}
local abbreviation B := A ⊎ Σ(a : A) (r : T a a), Q r
inductive pre_simple_two_quotient_rel : B → B → Type :=
| pre_Rmk {} : Π⦃a a'⦄ (r : R a a'), pre_simple_two_quotient_rel (inl a) (inl a')
--BUG: if {} not provided, the alias for pre_Rmk is wrong
definition pre_simple_two_quotient := quotient pre_simple_two_quotient_rel
open pre_simple_two_quotient_rel
local abbreviation C := quotient pre_simple_two_quotient_rel
protected definition j [constructor] (a : A) : C := class_of pre_simple_two_quotient_rel (inl a)
protected definition pre_aux [constructor] (q : Q r) : C :=
class_of pre_simple_two_quotient_rel (inr ⟨a, r, q⟩)
protected definition e (s : R a a') : j a = j a' := eq_of_rel _ (pre_Rmk s)
protected definition et (t : T a a') : j a = j a' := e_closure.elim e t
protected definition f [unfold 7] (q : Q r) : S¹ → C :=
circle.elim (j a) (et r)
protected definition pre_rec [unfold 8] {P : C → Type}
(Pj : Πa, P (j a)) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), P (pre_aux q))
(Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a =[e s] Pj a') (x : C) : P x :=
begin
induction x with p,
{ induction p,
{ apply Pj},
{ induction a with a1 a2, induction a2, apply Pa}},
{ induction H, esimp, apply Pe},
end
protected definition pre_elim [unfold 8] {P : Type} (Pj : A → P)
(Pa : Π⦃a : A⦄ ⦃r : T a a⦄, Q r → P) (Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a = Pj a') (x : C)
: P :=
pre_rec Pj Pa (λa a' s, pathover_of_eq (Pe s)) x
protected theorem rec_e {P : C → Type}
(Pj : Πa, P (j a)) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), P (pre_aux q))
(Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a =[e s] Pj a') ⦃a a' : A⦄ (s : R a a')
: apdo (pre_rec Pj Pa Pe) (e s) = Pe s :=
!rec_eq_of_rel
protected theorem elim_e {P : Type} (Pj : A → P) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄, Q r → P)
(Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a = Pj a') ⦃a a' : A⦄ (s : R a a')
: ap (pre_elim Pj Pa Pe) (e s) = Pe s :=
begin
apply eq_of_fn_eq_fn_inv !(pathover_constant (e s)),
rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑pre_elim,rec_e],
end
protected definition elim_et {P : Type} (Pj : A → P) (Pa : Π⦃a : A⦄ ⦃r : T a a⦄, Q r → P)
(Pe : Π⦃a a' : A⦄ (s : R a a'), Pj a = Pj a') ⦃a a' : A⦄ (t : T a a')
: ap (pre_elim Pj Pa Pe) (et t) = e_closure.elim Pe t :=
ap_e_closure_elim_h e (elim_e Pj Pa Pe) t
inductive simple_two_quotient_rel : C → C → Type :=
| Rmk {} : Π{a : A} {r : T a a} (q : Q r) (x : circle), simple_two_quotient_rel (f q x) (pre_aux q)
open simple_two_quotient_rel
definition simple_two_quotient := quotient simple_two_quotient_rel
local abbreviation D := simple_two_quotient
local abbreviation i := class_of simple_two_quotient_rel
definition incl0 (a : A) : D := i (j a)
protected definition aux (q : Q r) : D := i (pre_aux q)
definition incl1 (s : R a a') : incl0 a = incl0 a' := ap i (e s)
definition inclt (t : T a a') : incl0 a = incl0 a' := e_closure.elim incl1 t
-- "wrong" version inclt, which is ap i (p ⬝ q) instead of ap i p ⬝ ap i q
-- it is used in the proof, because inclt is easier to work with
protected definition incltw (t : T a a') : incl0 a = incl0 a' := ap i (et t)
protected definition inclt_eq_incltw (t : T a a') : inclt t = incltw t :=
(ap_e_closure_elim i e t)⁻¹
definition incl2' (q : Q r) (x : S¹) : i (f q x) = aux q :=
eq_of_rel simple_two_quotient_rel (Rmk q x)
protected definition incl2w (q : Q r) : incltw r = idp :=
(ap02 i (elim_loop (j a) (et r))⁻¹) ⬝
(ap_compose i (f q) loop)⁻¹ ⬝
ap_weakly_constant (incl2' q) loop ⬝
!con.right_inv
definition incl2 (q : Q r) : inclt r = idp :=
inclt_eq_incltw r ⬝ incl2w q
local attribute simple_two_quotient f i incl0 aux incl1 incl2' inclt [reducible]
local attribute i aux incl0 [constructor]
protected definition elim {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
(x : D) : P :=
begin
induction x,
{ refine (pre_elim _ _ _ a),
{ exact P0},
{ intro a r q, exact P0 a},
{ exact P1}},
{ exact abstract begin induction H, induction x,
{ exact idpath (P0 a)},
{ unfold f, apply pathover_eq, apply hdeg_square,
exact abstract ap_compose (pre_elim P0 _ P1) (f q) loop ⬝
ap _ !elim_loop ⬝
!elim_et ⬝
P2 q ⬝
!ap_constant⁻¹ end
} end end},
end
local attribute elim [unfold 8]
protected definition elim_on {P : Type} (x : D) (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
: P :=
elim P0 P1 P2 x
definition elim_incl1 {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a a' : A⦄ (s : R a a') : ap (elim P0 P1 P2) (incl1 s) = P1 s :=
(ap_compose (elim P0 P1 P2) i (e s))⁻¹ ⬝ !elim_e
definition elim_inclt {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (inclt t) = e_closure.elim P1 t :=
ap_e_closure_elim_h incl1 (elim_incl1 P2) t
protected definition elim_incltw {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (incltw t) = e_closure.elim P1 t :=
(ap_compose (elim P0 P1 P2) i (et t))⁻¹ ⬝ !elim_et
protected theorem elim_inclt_eq_elim_incltw {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a a' : A⦄ (t : T a a')
: elim_inclt P2 t = ap (ap (elim P0 P1 P2)) (inclt_eq_incltw t) ⬝ elim_incltw P2 t :=
begin
unfold [elim_inclt,elim_incltw,inclt_eq_incltw,et],
refine !ap_e_closure_elim_h_eq ⬝ _,
rewrite [ap_inv,-con.assoc],
xrewrite [eq_of_square (ap_ap_e_closure_elim i (elim P0 P1 P2) e t)⁻¹ʰ],
rewrite [↓incl1,con.assoc], apply whisker_left,
rewrite [↑[elim_et,elim_incl1],+ap_e_closure_elim_h_eq,con_inv,↑[i,function.compose]],
rewrite [-con.assoc (_ ⬝ _),con.assoc _⁻¹,con.left_inv,▸*,-ap_inv,-ap_con],
apply ap (ap _),
krewrite [-eq_of_homotopy3_inv,-eq_of_homotopy3_con]
end
definition elim_incl2'_incl0 {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a : A⦄ ⦃r : T a a⦄ (q : Q r) : ap (elim P0 P1 P2) (incl2' q base) = idpath (P0 a) :=
!elim_eq_of_rel
-- set_option pp.implicit true
protected theorem elim_incl2w {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a : A⦄ ⦃r : T a a⦄ (q : Q r)
: square (ap02 (elim P0 P1 P2) (incl2w q)) (P2 q) (elim_incltw P2 r) idp :=
begin
esimp [incl2w,ap02],
rewrite [+ap_con (ap _),▸*],
xrewrite [-ap_compose (ap _) (ap i)],
rewrite [+ap_inv],
xrewrite [eq_top_of_square
((ap_compose_natural (elim P0 P1 P2) i (elim_loop (j a) (et r)))⁻¹ʰ⁻¹ᵛ ⬝h
(ap_ap_compose (elim P0 P1 P2) i (f q) loop)⁻¹ʰ⁻¹ᵛ ⬝h
ap_ap_weakly_constant (elim P0 P1 P2) (incl2' q) loop ⬝h
ap_con_right_inv_sq (elim P0 P1 P2) (incl2' q base)),
↑[elim_incltw]],
apply whisker_tl,
rewrite [ap_weakly_constant_eq],
xrewrite [naturality_apdo_eq (λx, !elim_eq_of_rel) loop],
rewrite [↑elim_2,rec_loop,square_of_pathover_concato_eq,square_of_pathover_eq_concato,
eq_of_square_vconcat_eq,eq_of_square_eq_vconcat],
apply eq_vconcat,
{ apply ap (λx, _ ⬝ eq_con_inv_of_con_eq ((_ ⬝ x ⬝ _)⁻¹ ⬝ _) ⬝ _),
transitivity _, apply ap eq_of_square,
apply to_right_inv !pathover_eq_equiv_square (hdeg_square (elim_1 P A R Q P0 P1 a r q P2)),
transitivity _, apply eq_of_square_hdeg_square,
unfold elim_1, reflexivity},
rewrite [+con_inv,whisker_left_inv,+inv_inv,-whisker_right_inv,
con.assoc (whisker_left _ _),con.assoc _ (whisker_right _ _),▸*,
whisker_right_con_whisker_left _ !ap_constant],
xrewrite [-con.assoc _ _ (whisker_right _ _)],
rewrite [con.assoc _ _ (whisker_left _ _),idp_con_whisker_left,▸*,
con.assoc _ !ap_constant⁻¹,con.left_inv],
xrewrite [eq_con_inv_of_con_eq_whisker_left,▸*],
rewrite [+con.assoc _ _ !con.right_inv,
right_inv_eq_idp (
(λ(x : ap (elim P0 P1 P2) (incl2' q base) = idpath
(elim P0 P1 P2 (class_of simple_two_quotient_rel (f q base)))), x)
(elim_incl2'_incl0 P2 q)),
↑[whisker_left]],
xrewrite [con2_con_con2],
rewrite [idp_con,↑elim_incl2'_incl0,con.left_inv,whisker_right_inv,↑whisker_right],
xrewrite [con.assoc _ _ (_ ◾ _)],
rewrite [con.left_inv,▸*,-+con.assoc,con.assoc _⁻¹,↑[elim,function.compose],con.left_inv,
▸*,↑j,con.left_inv,idp_con],
apply square_of_eq, reflexivity
end
theorem elim_incl2 {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a : A⦄ ⦃r : T a a⦄ (q : Q r), e_closure.elim P1 r = idp)
⦃a : A⦄ ⦃r : T a a⦄ (q : Q r)
: square (ap02 (elim P0 P1 P2) (incl2 q)) (P2 q) (elim_inclt P2 r) idp :=
begin
rewrite [↑incl2,↑ap02,ap_con,elim_inclt_eq_elim_incltw],
apply whisker_tl,
apply elim_incl2w
end
end
end simple_two_quotient
--attribute simple_two_quotient.j [constructor] --TODO
attribute /-simple_two_quotient.rec-/ simple_two_quotient.elim [unfold 8] [recursor 8]
--attribute simple_two_quotient.elim_type [unfold 9]
attribute /-simple_two_quotient.rec_on-/ simple_two_quotient.elim_on [unfold 5]
--attribute simple_two_quotient.elim_type_on [unfold 6]
namespace two_quotient
open e_closure simple_two_quotient
section
parameters {A : Type}
(R : A → A → Type)
local abbreviation T := e_closure R -- the (type-valued) equivalence closure of R
parameter (Q : Π⦃a a'⦄, T a a' → T a a' → Type)
variables ⦃a a' : A⦄ {s : R a a'} {t t' : T a a'}
inductive two_quotient_Q : Π⦃a : A⦄, e_closure R a a → Type :=
| Qmk : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄, Q t t' → two_quotient_Q (t ⬝r t'⁻¹ʳ)
open two_quotient_Q
local abbreviation Q2 := two_quotient_Q
definition two_quotient := simple_two_quotient R Q2
definition incl0 (a : A) : two_quotient := incl0 _ _ a
definition incl1 (s : R a a') : incl0 a = incl0 a' := incl1 _ _ s
definition inclt (t : T a a') : incl0 a = incl0 a' := e_closure.elim incl1 t
definition incl2 (q : Q t t') : inclt t = inclt t' :=
eq_of_con_inv_eq_idp (incl2 _ _ (Qmk R q))
protected definition elim {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
(x : two_quotient) : P :=
begin
induction x,
{ exact P0 a},
{ exact P1 s},
{ exact abstract [unfold 10] begin induction q with a a' t t' q,
rewrite [↑e_closure.elim],
apply con_inv_eq_idp, exact P2 q end end},
end
local attribute elim [unfold 8]
protected definition elim_on {P : Type} (x : two_quotient) (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
: P :=
elim P0 P1 P2 x
definition elim_incl1 {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
⦃a a' : A⦄ (s : R a a') : ap (elim P0 P1 P2) (incl1 s) = P1 s :=
!elim_incl1
definition elim_inclt {P : Type} {P0 : A → P}
{P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a'}
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
⦃a a' : A⦄ (t : T a a') : ap (elim P0 P1 P2) (inclt t) = e_closure.elim P1 t :=
!elim_inclt --ap_e_closure_elim_h incl1 (elim_incl1 P2) t
--print elim
theorem elim_incl2 {P : Type} (P0 : A → P)
(P1 : Π⦃a a' : A⦄ (s : R a a'), P0 a = P0 a')
(P2 : Π⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t'), e_closure.elim P1 t = e_closure.elim P1 t')
⦃a a' : A⦄ ⦃t t' : T a a'⦄ (q : Q t t')
: square (ap02 (elim P0 P1 P2) (incl2 q)) (P2 q) (elim_inclt P2 t) (elim_inclt P2 t') :=
begin
-- let H := elim_incl2 R Q2 P0 P1 (two_quotient_Q.rec (λ (a a' : A) (t t' : T a a') (q : Q t t'), con_inv_eq_idp (P2 q))) (Qmk R q),
-- esimp at H,
rewrite [↑[incl2,elim],ap_eq_of_con_inv_eq_idp],
xrewrite [eq_top_of_square (elim_incl2 R Q2 P0 P1 (elim_1 A R Q P P0 P1 P2) (Qmk R q)),▸*],
exact sorry
end
end
end two_quotient
--attribute two_quotient.j [constructor] --TODO
attribute /-two_quotient.rec-/ two_quotient.elim [unfold 8] [recursor 8]
--attribute two_quotient.elim_type [unfold 9]
attribute /-two_quotient.rec_on-/ two_quotient.elim_on [unfold 5]
--attribute two_quotient.elim_type_on [unfold 6]
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.