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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dab234a69bc4ee5eb00bcb4cc68042e5ef30f344 | f4bff2062c030df03d65e8b69c88f79b63a359d8 | /src/game/functions/seqContinDef.lean | 4161057ade75d19fce435e78b9502156281e0f7a | [
"Apache-2.0"
] | permissive | adastra7470/real-number-game | 776606961f52db0eb824555ed2f8e16f92216ea3 | f9dcb7d9255a79b57e62038228a23346c2dc301b | refs/heads/master | 1,669,221,575,893 | 1,594,669,800,000 | 1,594,669,800,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,530 | lean | import data.real.basic
namespace xena -- hide
open function
open real
open_locale classical
/-
Classic eps-delta definition of continuity is equivalent to
the definition using sequences.
-/
notation `|` x `|` := abs x
def is_limit (a : ℕ → ℝ) (l : ℝ) :=
∀ ε : ℝ, 0 < ε → ∃ N : ℕ, ∀ n : ℕ, N ≤ n → |a n - l| < ε
def continuous_at_x (f : ℝ → ℝ) (x : ℝ) :=
∀ ε : ℝ, 0 < ε → ∃ δ : ℝ, 0 < δ ∧ ∀ y : ℝ, |y - x| < δ → |f y - f x| < ε
def seq_continuous_at_x (f : ℝ → ℝ) (x : ℝ) :=
∀ (a : ℕ → ℝ), is_limit a x → is_limit ( λ n : ℕ, f (a n) ) (f x)
/- Lemma
The two definitions of continuity are equivalent.
-/
lemma cont_iff_seq_cont (f : ℝ → ℝ) :
∀ x : ℝ, continuous_at_x f x ↔ seq_continuous_at_x f x :=
begin
intro x,
split,
{ -- classical continuity def -> sequence def
intros H a hax e he,
have h1 := H e he,
cases h1 with d hdd,
cases hdd with hd hy,
have h2 := hax d hd,
cases h2 with N hN,
use N,
intros n hn,
have hnd := hN n hn,
have G := hy (a n) hnd, exact G,
},
{ -- sequence def -> classical def is a little trickier
-- contrapositive
contrapose!,
intro H, unfold continuous_at_x at H,
push_neg at H,
cases H with e hee,
cases hee with he hdd,
unfold seq_continuous_at_x, push_neg,
-- using these hypotheses, choose a sequence
have k : ∀ n : ℕ, ∃ y : ℝ, |y - x| < (1:ℝ)/(n+1) ∧ e ≤ | f y - f x|,
intro n,
have g1 := hdd ( (1:ℝ)/(n+1) ),
cases g1 with g11 g12,
exfalso,
{ -- this seems complicated, but other ways got into coercion problems
have f1 : ∀ m : ℕ, 0 < m+1,
intro m, exact nat.succ_pos m,
have f2 : ∀ m : ℕ, 0 < ( (m+1): ℝ),
intro m,
have f21 := f1 m,
norm_cast, linarith,
have f3 : ∀ m : ℕ, 0 < 1 / ( (m+1): ℝ),
intro m,
have f31 := one_div_pos_of_pos (f2 m),
exact f31,
have f4 := f3 n,
linarith,
},
exact g12,
choose a ha using k,
use a,
-- prove this sequence does converge to x
split,
{
intros ε hε,
set N := nat_ceil ( (1:ℝ)/ ε ) with hN,
use N,
intros n hn,
have H := ha n,
cases H with h1 h2,
have hN1 := le_nat_ceil ( (1:ℝ)/ε), rw ← hN at hN1,
have hN2 : ((1:ℝ) / ↑N) ≤ ε,
exact one_div_le_of_one_div_le_of_pos hε hN1,
have hN3 : ((1:ℝ)/(↑n+1)) < ((1:ℝ) / ↑N),
have hN31 : (n + 1) > N, linarith,
have hN32 : 0 < (n+1), linarith,
have hN33 : 0 < N,
have hN34 : 0 < ( (1:ℝ)/ε ), exact one_div_pos_of_pos hε,
have hN35 := lt_of_lt_of_le hN34 hN1,
norm_cast at hN35, exact hN35,
apply one_div_lt_one_div_of_lt,
norm_cast, linarith, norm_cast, linarith,
have hN4 : ((1:ℝ)/(↑n+1)) < ε, linarith,
linarith,
},
{ -- but f(a n) does not converge to f(x)
unfold is_limit, push_neg,
use e, split, exact he,
intro N, use N, split, linarith,
have G := ha N, cases G with G1 G2, exact G2,
},
done
},
done
end
end xena -- hide
|
5ebd4cd7c04bcd3f4a52fd4c208833b1e50a21cd | 4a092885406df4e441e9bb9065d9405dacb94cd8 | /src/Huber_pair.lean | 663d24069e5d53b8bb86303712ba9f7d682ddc23 | [
"Apache-2.0"
] | permissive | semorrison/lean-perfectoid-spaces | 78c1572cedbfae9c3e460d8aaf91de38616904d8 | bb4311dff45791170bcb1b6a983e2591bee88a19 | refs/heads/master | 1,588,841,765,494 | 1,554,805,620,000 | 1,554,805,620,000 | 180,353,546 | 0 | 1 | null | 1,554,809,880,000 | 1,554,809,880,000 | null | UTF-8 | Lean | false | false | 1,303 | lean | import power_bounded Huber_ring.basic data.polynomial
universes u v
--notation
local postfix `ᵒ` : 66 := power_bounded_subring
open power_bounded
section integral
variables {R : Type u} [comm_ring R] [decidable_eq R]
instance subtype.val.is_ring_hom (S : set R) [is_subring S] : is_ring_hom (@subtype.val _ S) :=
by apply_instance
def is_integral (S : set R) [is_subring S] (r : R) : Prop :=
∃ f : polynomial ↥S, (f.monic) ∧ f.eval₂ (@subtype.val _ S) r = 0
def is_integrally_closed (S : set R) [is_subring S] : Prop :=
∀ r : R, (is_integral S r) → r ∈ S
end integral
-- Wedhorn Def 7.14
structure is_ring_of_integral_elements {R : Type u} [Huber_ring R] [decidable_eq R] (Rplus : set R) : Prop :=
[is_subring : is_subring Rplus]
(is_open : is_open Rplus)
(is_int_closed : is_integrally_closed Rplus)
(is_power_bounded : Rplus ⊆ Rᵒ)
-- a Huber Ring is an f-adic ring.
-- a Huber Pair is what Huber called an Affinoid Ring.
structure Huber_pair :=
(R : Type u)
[RHuber : Huber_ring R]
[dec : decidable_eq R]
(Rplus : set R)
[intel : is_ring_of_integral_elements Rplus]
instance : has_coe_to_sort Huber_pair :=
{ S := Type u, coe := Huber_pair.R }
instance Huber_pair.Huber_ring (A : Huber_pair) : Huber_ring A := A.RHuber
postfix `⁺` : 66 := λ A : Huber_pair, A.Rplus
|
0ce3d8d5acbcf65f4a9617c413c85b13b852f44c | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch2/ex0210.lean | a22092e624ef2b24b389d40de9e969a6d676cb21 | [] | 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 | 13 | lean | #check prod
|
2b6b3c3464f89f05bef63a73a4eacdd34a2eaeac | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebra/category/Module/limits.lean | 25efbe7d6b5c8b22256e03a0a3853a47b2ebb1b0 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,699 | 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.Module.basic
import algebra.category.Group.limits
import algebra.direct_limit
/-!
# The category of R-modules has all limits
Further, these limits are preserved by the forgetful functor --- that is,
the underlying types are just the limits in the category of types.
-/
open category_theory
open category_theory.limits
universes u v
noncomputable theory
namespace Module
variables {R : Type u} [ring R]
variables {J : Type v} [small_category J]
instance add_comm_group_obj (F : J ⥤ Module.{v} R) (j) :
add_comm_group ((F ⋙ forget (Module R)).obj j) :=
by { change add_comm_group (F.obj j), apply_instance }
instance module_obj (F : J ⥤ Module.{v} R) (j) :
module R ((F ⋙ forget (Module R)).obj j) :=
by { change module R (F.obj j), apply_instance }
/--
The flat sections of a functor into `Module R` form a submodule of all sections.
-/
def sections_submodule (F : J ⥤ Module R) :
submodule R (Π j, F.obj j) :=
{ carrier := (F ⋙ forget (Module R)).sections,
smul_mem' := λ r s sh j j' f,
begin
simp only [forget_map_eq_coe, functor.comp_map, pi.smul_apply, linear_map.map_smul],
dsimp [functor.sections] at sh,
rw sh f,
end,
..(AddGroup.sections_add_subgroup
(F ⋙ forget₂ (Module R) AddCommGroup.{v} ⋙ forget₂ AddCommGroup AddGroup.{v})) }
-- Adding the following instance speeds up `limit_module` noticeably,
-- by preventing a bad unfold of `limit_add_comm_group`.
instance limit_add_comm_monoid (F : J ⥤ Module R) :
add_comm_monoid (types.limit_cone (F ⋙ forget (Module.{v} R))).X :=
show add_comm_monoid (sections_submodule F), by apply_instance
instance limit_add_comm_group (F : J ⥤ Module R) :
add_comm_group (types.limit_cone (F ⋙ forget (Module.{v} R))).X :=
show add_comm_group (sections_submodule F), by apply_instance
instance limit_module (F : J ⥤ Module R) :
module R (types.limit_cone (F ⋙ forget (Module.{v} R))).X :=
show module R (sections_submodule F), by apply_instance
/-- `limit.π (F ⋙ forget Ring) j` as a `ring_hom`. -/
def limit_π_linear_map (F : J ⥤ Module R) (j) :
(types.limit_cone (F ⋙ forget (Module.{v} R))).X →ₗ[R] (F ⋙ forget (Module R)).obj j :=
{ to_fun := (types.limit_cone (F ⋙ forget (Module R))).π.app j,
map_smul' := λ x y, rfl,
map_add' := λ x y, rfl }
namespace has_limits
-- The next two definitions are used in the construction of `has_limits (Module R)`.
-- After that, the limits should be constructed using the generic limits API,
-- e.g. `limit F`, `limit.cone F`, and `limit.is_limit F`.
/--
Construction of a limit cone in `Module R`.
(Internal use only; use the limits API.)
-/
def limit_cone (F : J ⥤ Module R) : cone F :=
{ X := Module.of R (types.limit_cone (F ⋙ forget _)).X,
π :=
{ app := limit_π_linear_map F,
naturality' := λ j j' f,
linear_map.coe_injective ((types.limit_cone (F ⋙ forget _)).π.naturality f) } }
/--
Witness that the limit cone in `Module R` is a limit cone.
(Internal use only; use the limits API.)
-/
def limit_cone_is_limit (F : J ⥤ Module R) : is_limit (limit_cone F) :=
begin
refine is_limit.of_faithful
(forget (Module R)) (types.limit_cone_is_limit _)
(λ s, ⟨_, _, _⟩) (λ s, rfl); tidy
end
end has_limits
open has_limits
/-- The category of R-modules has all limits. -/
@[irreducible]
instance has_limits : has_limits (Module.{v} R) :=
{ has_limits_of_shape := λ J 𝒥, by exactI
{ has_limit := λ F, has_limit.mk
{ cone := limit_cone F,
is_limit := limit_cone_is_limit F } } }
/--
An auxiliary declaration to speed up typechecking.
-/
def forget₂_AddCommGroup_preserves_limits_aux (F : J ⥤ Module R) :
is_limit ((forget₂ (Module R) AddCommGroup).map_cone (limit_cone F)) :=
AddCommGroup.limit_cone_is_limit (F ⋙ forget₂ (Module R) AddCommGroup)
/--
The forgetful functor from R-modules to abelian groups preserves all limits.
-/
instance forget₂_AddCommGroup_preserves_limits :
preserves_limits (forget₂ (Module R) AddCommGroup.{v}) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (forget₂_AddCommGroup_preserves_limits_aux F) } }
/--
The forgetful functor from R-modules to types preserves all limits.
-/
instance forget_preserves_limits : preserves_limits (forget (Module R)) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ F, preserves_limit_of_preserves_limit_cone
(limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget _)) } }
section direct_limit
open module
variables {ι : Type v}
variables [dec_ι : decidable_eq ι] [directed_order ι]
variables (G : ι → Type v)
variables [Π i, add_comm_group (G i)] [Π i, module R (G i)]
variables (f : Π i j, i ≤ j → G i →ₗ[R] G j) [module.directed_system G f]
/-- The diagram (in the sense of `category_theory`)
of an unbundled `direct_limit` of modules. -/
@[simps]
def direct_limit_diagram : ι ⥤ Module R :=
{ obj := λ i, Module.of R (G i),
map := λ i j hij, f i j hij.le,
map_id' := λ i, by { apply linear_map.ext, intro x, apply module.directed_system.map_self },
map_comp' := λ i j k hij hjk,
begin
apply linear_map.ext,
intro x,
symmetry,
apply module.directed_system.map_map
end }
variables [decidable_eq ι]
/-- The `cocone` on `direct_limit_diagram` corresponding to
the unbundled `direct_limit` of modules.
In `direct_limit_is_colimit` we show that it is a colimit cocone. -/
@[simps]
def direct_limit_cocone : cocone (direct_limit_diagram G f) :=
{ X := Module.of R $ direct_limit G f,
ι := { app := module.direct_limit.of R ι G f,
naturality' := λ i j hij, by { apply linear_map.ext, intro x, exact direct_limit.of_f } } }
/-- The unbundled `direct_limit` of modules is a colimit
in the sense of `category_theory`. -/
@[simps]
def direct_limit_is_colimit [nonempty ι] : is_colimit (direct_limit_cocone G f) :=
{ desc := λ s, direct_limit.lift R ι G f s.ι.app $ λ i j h x, by { rw [←s.w (hom_of_le h)], refl },
fac' := λ s i,
begin
apply linear_map.ext,
intro x,
dsimp,
exact direct_limit.lift_of s.ι.app _ x,
end,
uniq' := λ s m h,
begin
have : s.ι.app = λ i, linear_map.comp m (direct_limit.of R ι (λ i, G i) (λ i j H, f i j H) i),
{ funext i, rw ← h, refl },
apply linear_map.ext,
intro x,
simp only [this],
apply module.direct_limit.lift_unique
end }
end direct_limit
end Module
|
b9b034b9df96f66d7b23db5abbfbc7dbb73977e1 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /doc/examples/NFM2022/nfm20.lean | 391b31cbb5bd3b9cdaab5cd17c712a5d99c78420 | [
"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 | 647 | lean | /- Rewriting -/
example (f : Nat → Nat) (k : Nat) (h₁ : f 0 = 0) (h₂ : k = 0) : f k = 0 := by
rw [h₂] -- replace k with 0
rw [h₁] -- replace f 0 with 0
example (f : Nat → Nat) (k : Nat) (h₁ : f 0 = 0) (h₂ : k = 0) : f k = 0 := by
rw [h₂, h₁]
example (f : Nat → Nat) (a b : Nat) (h₁ : a = b) (h₂ : f a = 0) : f b = 0 := by
rw [← h₁, h₂]
example (f : Nat → Nat) (a : Nat) (h : 0 + a = 0) : f a = f 0 := by
rw [Nat.zero_add] at h
rw [h]
def Tuple (α : Type) (n : Nat) :=
{ as : List α // as.length = n }
example (n : Nat) (h : n = 0) (t : Tuple α n) : Tuple α 0 := by
rw [h] at t
exact t
|
3bc96708d3761e45c16d2200fd8d99366f51d5d1 | 76df16d6c3760cb415f1294caee997cc4736e09b | /rosette-benchmarks-4/jitterbug/jitterbug/lean/src/jit.lean | 0d77e19cb4a6f25e95454cc31e9d25968d689d40 | [
"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 | 27,444 | lean |
import tactic.tauto
/-!
This file contains the metatheory of JIT correctness.
The main theorem is interpreter_equivalence, proved based on the following two sets of axioms.
Two axioms are assumed to be correct (e.g., ensured by the Linux kernel):
* ctx_correctness: the JIT computes a correct JIT context for the source program
* layout_consistency: the output of the JIT contains the output of individual parts.
Three axioms about the correctness of individal parts of the JIT are expected
to be proved separately in SMT:
* prologue_correct: running the emitted prologue from the initial target state reaches a target
state that relates to the initial source state.
* per_insn_correct: running the emitted target code preserves the relation between source and
target states and produces the same trace.
* epilogue_correct: running the emitted epilogue from a target state that relates to the final
source state reaches the final target state.
## References
* Xavier Leroy. A formally verified compiler back-end. Journal of Automated
Reasoning, 43(4):363--446, December 2009.
-/
namespace machine
section machine
parameters {CONTEXT EVENT ORACLE INPUT OUTPUT PC STATE INSN : Type}
-- A trace is a list of externally visible events.
definition TRACE : Type := list EVENT
instance : has_append TRACE := ⟨list.append⟩
-- Get program counter of machine.
parameter pc_of : STATE → PC
-- Step a given instruction, producing new state and trace.
parameter step_insn : ORACLE → INSN → STATE → option (STATE × TRACE)
-- Code is a partial map from PC to instruction. We intentionally avoid
-- using a list and favor this more abstract representation.
definition CODE : Type := PC → option INSN
-- Whether the state is an inital state.
parameter initial : STATE → CONTEXT → INPUT → Prop
-- Whether the state is a final state.
parameter final : STATE → OUTPUT → Prop
inductive step (nd : ORACLE) (code : CODE) (σ : STATE) : STATE → TRACE → Prop
-- Can take a step if there exists an instruction to execute and the state is not final.
| step_one :
∀ (insn : INSN) (σ' : STATE) (tr : TRACE),
code (pc_of σ) = some insn →
step_insn nd insn σ = some (σ', tr) →
(¬ ∃ (r : OUTPUT), final σ r) →
step σ' tr
-- Final states step to themselves.
| step_final :
∀ (o : OUTPUT),
final σ o →
step σ []
-- Step behaves like a function.
lemma step_deterministic :
∀ (nd : ORACLE) (code : CODE) (s s1' s2' : STATE) (tr1 tr2 : TRACE),
step nd code s s1' tr1 →
step nd code s s2' tr2 →
(s1' = s2' ∧ tr1 = tr2) :=
begin
intros _ _ _ _ _ _ _ S1 S2,
cases S1 with S1_insn B C S1_code S1_dostep S1_notfinal S1_r S2_final;
cases S2 with S2_insn J K S2_code S2_dostep S2_notfinal S2_r S2_final,
{ rw S1_code at *,
cases S2_code,
rw S1_dostep at *,
cases S2_dostep, by tauto,
},
{ exfalso, apply S1_notfinal,
existsi S2_r, by assumption,
},
{ exfalso, apply S2_notfinal,
existsi S1_r, by assumption,
},
{ by tauto, },
end
-- A standard way to define reachable states.
inductive star (nd : ORACLE) (code : CODE) : STATE → STATE → TRACE → Prop
| refl :
∀ (a : STATE),
star a a []
| step :
∀ (a b c : STATE) (tr₁ tr₂ : TRACE),
step nd code a b tr₁ →
star b c tr₂ →
star a c (tr₁ ++ tr₂)
-- A safe state can always fetch an instruction from any reachable state,
-- or it's the final state
definition safe (nd : ORACLE) (code : CODE) (σ : STATE) : Prop :=
∀ (σ' : STATE) (tr : TRACE),
star nd code σ σ' tr →
(∃ (insn : INSN), code (pc_of σ') = some insn) ∨ (∃ (o : OUTPUT), final σ' o)
-- If you can take one step, you can show star.
lemma star_one :
∀ (nd : ORACLE) (code : CODE) (a b : STATE) (tr : TRACE),
step nd code a b tr →
star nd code a b tr :=
begin
intros,
rw ← list.append_nil tr,
constructor,
{ assumption },
{ constructor }
end
-- Star is transitive for fixed code and appending traces.
lemma star_trans :
∀ (nd : ORACLE) (code : CODE) (a b : STATE) (tr₁ : TRACE),
star nd code a b tr₁ →
∀ (c : STATE) (tr₂ : TRACE),
star nd code b c tr₂ →
star nd code a c (tr₁ ++ tr₂) :=
begin
intros nd code _ _ _ Hab,
induction Hab with _ _ _ _ _ _ _ _ Hab_ih; intros; simp,
{ assumption },
{ constructor,
{ assumption },
{ apply Hab_ih,
assumption } }
end
-- c1 is a subset of c2 if any instruction c1 has is also in c2.
definition subset (c1 c2 : CODE) : Prop :=
∀ (idx : PC) (insn : INSN),
c1 idx = some insn →
c2 idx = some insn
local infix ` <+ ` := subset
lemma step_subset :
∀ (nd : ORACLE) (code₁ code₂ : CODE) (s1 s2 : STATE) (tr : TRACE),
step nd code₁ s1 s2 tr →
code₁ <+ code₂ →
step nd code₂ s1 s2 tr :=
begin
intros _ _ _ _ _ _ H1 H2,
cases H1 with H1_insn _ _ H1_a H1_a_1 H1_a_2 _ H1_a,
{ apply step.step_one,
apply H2,
from H1_a,
by assumption,
by assumption },
{ apply step.step_final,
from H1_a }
end
-- If you can star given some code, you can always
-- add more code and preserve star.
lemma star_subset :
∀ (nd : ORACLE) (code₁ code₂ : CODE) (s1 s2 : STATE) (tr : TRACE),
star nd code₁ s1 s2 tr →
code₁ <+ code₂ →
star nd code₂ s1 s2 tr :=
begin
intros _ _ _ _ _ _ H1 _,
induction H1,
{ apply star.refl, },
econstructor,
{ apply step_subset, all_goals {assumption}, },
assumption,
end
lemma final_step :
∀ (nd : ORACLE) (s : STATE) (c : CODE) (o : OUTPUT),
final s o →
∀ (s' : STATE) (tr : TRACE),
step nd c s s' tr →
tr = [] ∧ s' = s :=
begin
intros _ _ _ _ h1 _ _ h2,
cases h2 with _ _ _ h2_a h2_a_1 h2_a_2 _ h2_a,
{ exfalso, apply h2_a_2,
existsi o, by assumption },
{ cc }
end
lemma final_star :
∀ (nd : ORACLE) (s : STATE) (c : CODE) (o : OUTPUT),
final s o →
∀ (s' : STATE) (tr : TRACE),
star nd c s s' tr →
(tr = [] ∧ s' = s) :=
begin
intros _ _ _ _ h1 _ _ h2,
induction h2 with s2 s1 s2 s3 tr1 tr2 h3 h4 IH, cc,
have : tr1 = [] ∧ s2 = s1,
by { apply final_step; assumption <|> skip, from h1 },
cases this, subst this_left, subst this_right, simp, cc,
end
-- A final state is always safe.
lemma final_safe :
∀ (nd : ORACLE) (code : CODE) (s : STATE) (o : OUTPUT),
final s o →
safe nd code s :=
begin
dsimp [safe, machine.safe], intros _ _ _ _ a _ _ a_1, right,
induction a_1 with _ _ _ _ _ _ a_1_a a_1_a_1,
{ existsi o, by assumption },
{ apply a_1_ih,
cases a_1_a with _ _ _ a_1_a_a a_1_a_a_1 a_1_a_a_2 _ a_1_a_a,
{ exfalso, apply a_1_a_a_2,
existsi o,
assumption },
{ assumption } }
end
-- A safe state can always take a step if it's not final.
lemma safe_self :
∀ (nd : ORACLE) (code : CODE) (s : STATE),
safe nd code s →
(¬ ∃ (o : OUTPUT), final s o) →
∃ (insn : INSN),
code (pc_of s) = some insn :=
begin
intros _ _ _ a a_1,
dsimp [safe] at *,
specialize a s [] (by constructor),
cases a with insn a,
by assumption,
cases a with res res_final,
exfalso, apply a_1, existsi res, by assumption,
end
-- A safe state is still safe after one step.
lemma safe_step :
∀ (nd : ORACLE) (code : CODE) (s₁ s₂ : STATE) (tr : TRACE),
safe nd code s₁ →
step nd code s₁ s₂ tr →
safe nd code s₂ :=
begin
intros _ _ _ _ _ H1 _,
simp [safe] at *,
intros _ _ _,
apply H1 _ (tr ++ tr_1),
constructor; assumption,
end
-- Safety is preserved across an arbitrary number of steps.
lemma safe_star :
∀ (nd : ORACLE) (code : CODE) (s₁ s₂ : STATE) (tr : TRACE),
safe nd code s₁ →
star nd code s₁ s₂ tr →
safe nd code s₂ :=
begin
intros _ _ _ _ _ a a_1,
induction a_1, by assumption,
apply a_1_ih,
apply safe_step; assumption,
end
-- If a state is safe, and it was obtained by taking a step,
-- then the state it stepped from is also safe.
lemma safe_step_backwards :
∀ (nd : ORACLE) (code : CODE) (s₁ s₂ : STATE) (tr : TRACE),
safe nd code s₂ →
step nd code s₁ s₂ tr →
safe nd code s₁ :=
begin
intros _ _ _ _ _ a a_1,
simp [safe] at *, intros _ _ a_2,
cases a_2 with _ _ _ _ _ _ a_2_a a_2_a_1,
{ cases a_1,
left, existsi a_1_insn, by assumption,
right, existsi a_1_o, by assumption },
{ apply a,
suffices h : a_2_b = s₂,
rw h at *,
{ rw h at *,
by assumption },
cases (step_deterministic _ _ _ _ code _ _ _ _ _ a_2_a a_1),
by assumption,
},
end
-- A state that eventually terminates is always safe.
lemma terminates_safe :
∀ (nd : ORACLE) (code : CODE) (s₁ s₂ : STATE) (tr : TRACE) (o : OUTPUT),
star nd code s₁ s₂ tr →
final s₂ o →
safe nd code s₁ :=
begin
intros _ _ _ _ _ _ a a_1,
induction a,
apply final_safe; assumption,
apply safe_step_backwards; try{assumption},
apply a_ih, assumption,
end
-- Holds if the code always terminates
definition always_terminates (code : CODE) : Prop :=
∀ (ctx : CONTEXT) (nd : ORACLE) (s : STATE) (i : INPUT),
initial s ctx i →
∃ (s' : STATE) (tr : TRACE) (o : OUTPUT),
star nd code s s' tr ∧
final s' o
-- A program which always terminates is same from the initial state.
lemma always_terminates_safe :
∀ (ctx : CONTEXT) (s : STATE) (code : CODE) (i : INPUT),
always_terminates code →
∀ (nd : ORACLE),
initial s ctx i →
safe nd code s :=
begin
intros _ _ _ _ a _ a_1,
dsimp [always_terminates] at *,
specialize a ctx nd s i a_1,
cases a with _ a,
cases a with _ a,
cases a with _ a,
cases a with _ a,
apply machine.terminates_safe; assumption,
end
end machine
end machine
-- Re-declare infix notation outside of machine scope.
local infix ` <+ ` := machine.subset
constants CONTEXT EVENT ORACLE INPUT OUTPUT : Type
definition TRACE : Type := @machine.TRACE EVENT
noncomputable instance : has_append TRACE := ⟨list.append⟩
-- This models the behavior of the source language.
namespace source
constants INSN PC STATE : Type
constant pc_of : STATE → PC
constant step_insn : ORACLE → INSN → STATE → option (STATE × TRACE)
constant final : STATE → OUTPUT → Prop
constant initial : STATE → CONTEXT → INPUT → Prop
axiom initial_inhabited : ∀ (i : INPUT) (ctx : CONTEXT), ∃ (s : STATE), initial s ctx i
definition step := machine.step pc_of step_insn final
definition star := machine.star pc_of step_insn final
definition safe := machine.safe pc_of step_insn final
@[simp] definition always_terminates := machine.always_terminates pc_of step_insn initial final
definition CODE : Type := @machine.CODE PC INSN
-- This captures whether a JIT context is well-formed for a particular source BPF program.
constant wf : CONTEXT → CODE → Prop
end source
-- This models the behavior of the target language.
namespace target
constants INSN PC STATE : Type
constant pc_of : STATE → PC
constant step_insn : ORACLE → INSN → STATE → option (STATE × TRACE)
constant final : STATE → OUTPUT → Prop
definition CODE : Type := @machine.CODE PC INSN
constant initial : STATE → CONTEXT → INPUT → Prop
definition step := machine.step pc_of step_insn final
@[reducible] definition star := machine.star pc_of step_insn final
-- Whether the architectural invariants hold for some state
-- w.r.t an initial state
constant arch_safe : STATE → STATE → Prop
-- Inductive form of arch safety, depends on ctx.
constant arch_safe_inv : CONTEXT → STATE → STATE → Prop
-- The output of a final state is uniquely determined by the state.
axiom output_deterministic :
∀ (s : STATE) (o₁ o₂ : OUTPUT),
final s o₁ →
final s o₂ →
o₁ = o₂
end target
-- This models the JIT implementation.
namespace jit
-- Emit target code for a single source instruction.
constant emit_insn : CONTEXT → source.INSN → source.PC → option target.CODE
constant emit_prologue : CONTEXT → option target.CODE
constant emit_epilogue : CONTEXT → option target.CODE
-- Emit target code for an entire source program, including BPF checker, prologue, and epilogue.
constant compile : CONTEXT → source.CODE → option target.CODE
-- If the JIT suceeds for an entire source program,
-- it must have succeeded for each (valid) source instruction,
-- and the produced code must contain the prologue and epilogue.
--
-- This is assumed to hold.
axiom layout_consistency :
∀ (ctx : CONTEXT) (code_S : source.CODE) (code_T : target.CODE),
source.wf ctx code_S →
jit.compile ctx code_S = some code_T →
(∀ (i : source.PC) (insn : source.INSN),
code_S i = some insn →
∃ (frag_T : target.CODE),
jit.emit_insn ctx insn i = some frag_T ∧ frag_T <+ code_T) ∧
(∃ (frag_T : target.CODE), jit.emit_prologue ctx = some frag_T ∧ frag_T <+ code_T) ∧
(∃ (frag_T : target.CODE), jit.emit_epilogue ctx = some frag_T ∧ frag_T <+ code_T)
end jit
-- JIT correctness
-- This relates source and target states, parameterized by a JIT context.
constant related : CONTEXT → source.STATE → target.STATE → Prop
notation s1 `~[`:50 ctx `]` s2:50 := related ctx s1 s2
-- Running the prologue from an initial state produces a target state related to the initial source
-- state (with no trace).
--
-- This is proved in SMT.
axiom prologue_correct :
∀ (nd : ORACLE) (ctx : CONTEXT) (σ_T : target.STATE) (σ_S : source.STATE) (i : INPUT)
(code_S : source.CODE) (code_T : target.CODE),
source.wf ctx code_S →
target.initial σ_T ctx i →
source.initial σ_S ctx i →
jit.emit_prologue ctx = some code_T →
∃ (σ_T' : target.STATE),
target.star nd code_T σ_T σ_T' [] ∧
σ_S ~[ctx] σ_T' ∧
target.arch_safe_inv ctx σ_T σ_T'
-- If the source state has reached a OUTPUT, then executing the epilogue in a related target state
-- reaches a state with the same OUTPUT (and no trace).
--
-- This is proved in SMT.
axiom epilogue_correct :
∀ (nd : ORACLE) (ctx : CONTEXT) (code_S : source.CODE) (code_T : target.CODE) (σ_S : source.STATE)
(init_T σ_T : target.STATE) (o : OUTPUT),
source.wf ctx code_S →
σ_S ~[ctx] σ_T →
target.arch_safe_inv ctx init_T σ_T →
source.final σ_S o →
jit.emit_epilogue ctx = some code_T →
∃ (σ_T' : target.STATE),
target.star nd code_T σ_T σ_T' [] ∧
target.final σ_T' o ∧
target.arch_safe init_T σ_T'
-- If the JIT produces some code for one source instruction, then starting from related source and
-- target states, stepping the source instruction is related to some state reachable from the
-- target state, for the jited code.
--
-- This is proved in SMT.
axiom per_insn_correct :
∀ (nd : ORACLE) (ctx : CONTEXT) (idx : source.PC) (code_S : source.CODE) (insn : source.INSN)
(frag_T : target.CODE) (σ_S σ_S' : source.STATE) (init_T σ_T : target.STATE) (tr : TRACE)
(code_T : target.CODE),
source.wf ctx code_S →
code_S idx = some insn →
jit.emit_insn ctx insn idx = some frag_T →
σ_S ~[ctx] σ_T →
target.arch_safe_inv ctx init_T σ_T →
source.pc_of σ_S = idx →
source.step nd code_S σ_S σ_S' tr →
∃ (σ_T' : target.STATE),
target.star nd frag_T σ_T σ_T' tr ∧ σ_S' ~[ctx] σ_T' ∧ target.arch_safe_inv ctx init_T σ_T'
lemma star_src_correct :
∀ (ctx : CONTEXT) (nd : ORACLE) (code_S : source.CODE) (σ_S σ_S' : source.STATE) (tr : TRACE),
source.wf ctx code_S →
source.safe nd code_S σ_S →
source.star nd code_S σ_S σ_S' tr →
∀ (init_T σ_T : target.STATE) (code_T : target.CODE),
σ_S ~[ctx] σ_T →
target.arch_safe_inv ctx init_T σ_T →
jit.compile ctx code_S = some code_T →
∃ (σ_T' : target.STATE),
target.star nd code_T σ_T σ_T' tr ∧
σ_S' ~[ctx] σ_T' ∧
target.arch_safe_inv ctx init_T σ_T' :=
begin
intros _ _ _ _ _ _ wf_S safe_S star_S,
induction star_S with s1 s1 s2 s3 tr1 tr2 step_S star_S' IH,
{ intros _ _ _ related archinv emitted,
existsi σ_T, split, constructor, split; assumption, },
intros _ _ _ related archinv emitted,
-- Handle the case where s1 is a final state. If it is, it steps to itself and
-- the target state can take zero steps.
cases step_S with insn _ _ code_at do_step not_final r is_final,
tactic.swap,
{ apply IH; assumption, },
have hemit : ∃ f_T, jit.emit_insn ctx insn (source.pc_of s1) = some f_T ∧ f_T <+ code_T,
{
cases (jit.layout_consistency ctx code_S code_T wf_S emitted),
apply left; assumption,
},
cases hemit with f_T hemit,
cases hemit with hemit_left hemit_right,
have hstep_T : ∃ t2, target.star nd f_T σ_T t2 tr1 ∧ s2 ~[ctx] t2 ∧ target.arch_safe_inv ctx init_T t2,
{
apply per_insn_correct; try{assumption <|> reflexivity},
constructor; assumption,
},
cases hstep_T with t2 hstep_T,
cases hstep_T with hstep_T_left hstep_T_right,
have hstar_T : ∃ t3, target.star nd code_T t2 t3 tr2 ∧ s3 ~[ctx] t3 ∧ target.arch_safe_inv ctx init_T t3,
{
cases hstep_T_right,
apply IH; try{assumption},
apply machine.safe_step,
from safe_S,
constructor; assumption,
},
cases hstar_T with t3 hstar_T,
cases hstar_T with hstar_T_left hstar_T_right,
existsi t3,
split, tactic.swap, assumption,
apply machine.star_trans; try{assumption},
apply machine.star_subset; assumption,
end
-- The behavior of the source program is implemented by the jited target code.
theorem forward_simulation :
∀ (ctx : CONTEXT) (nd : ORACLE) (code_S : source.CODE) (σ_S σ_S' : source.STATE) (tr : TRACE) (i : INPUT)
(o : OUTPUT),
source.wf ctx code_S →
source.initial σ_S ctx i →
source.always_terminates code_S →
source.star nd code_S σ_S σ_S' tr →
source.final σ_S' o →
∀ (σ_T : target.STATE) (code_T : target.CODE),
target.initial σ_T ctx i →
jit.compile ctx code_S = some code_T →
∃ (σ_T' : target.STATE),
target.star nd code_T σ_T σ_T' tr ∧
target.final σ_T' o ∧
target.arch_safe σ_T σ_T' :=
begin
intros _ _ _ _ _ _ _ _ wf_S Hinitial_S terminates_S H2 H3 _ _ H4 H5,
-- Get the code to run in the target and show code_T is a superset of each component
cases (jit.layout_consistency ctx code_S code_T wf_S H5) with ec_insn ec,
cases ec with ec_prologue ec_epilogue,
cases ec_prologue with prologue prologue_subseteq,
cases prologue_subseteq with prologue_eq prologue_subseteq,
cases ec_epilogue with epilogue epilogue_subseteq,
cases epilogue_subseteq with epilogue_eq epilogue_subseteq,
-- Construct the prologue star
have hprologue : ∃ t2, target.star nd prologue σ_T t2 [] ∧
σ_S ~[ctx] t2 ∧ target.arch_safe_inv ctx σ_T t2,
{ apply prologue_correct; try{assumption},
},
cases hprologue with t2 hprologue,
cases hprologue,
-- construct the regular instr star using the lemma defined above
have hstar : ∃ t3, target.star nd code_T t2 t3 tr ∧ σ_S' ~[ctx] t3 ∧ target.arch_safe_inv ctx σ_T t3,
{
cases hprologue_right,
apply star_src_correct; try{assumption},
apply machine.always_terminates_safe; try{assumption},
},
cases hstar with t3 hstar,
cases hstar with hstar_left hstar_right,
-- construct the epilogue star
have hepilogue : ∃ t4, target.star nd epilogue t3 t4 [] ∧ target.final t4 o ∧ target.arch_safe σ_T t4,
{
cases hstar_right,
apply epilogue_correct; by assumption,
},
cases hepilogue with t4 hepilogue,
cases hepilogue,
-- Now we can glue all the steps together
existsi t4,
split, tactic.swap, split,
cases hepilogue_right with hepilogue_right regs_same,
by assumption,
cases hepilogue_right, by assumption,
-- Run the prologue
change tr with (list.nil ++ tr),
apply machine.star_trans,
{
apply machine.star_subset,
apply hprologue_left,
assumption,
},
-- Run the middle
rewrite ← list.append_nil tr,
apply machine.star_trans,
{
assumption,
},
-- Run the epilogue
apply machine.star_subset,
from hepilogue_left,
assumption,
end
lemma target_deterministic :
∀ (nd : ORACLE) (code : target.CODE) (σ σ'₁ : target.STATE) (tr₁ : TRACE) (o₁ : OUTPUT),
target.star nd code σ σ'₁ tr₁ →
target.final σ'₁ o₁ →
∀ (σ'₂ : target.STATE) (tr₂ : TRACE) (o₂ : OUTPUT),
target.star nd code σ σ'₂ tr₂ →
target.final σ'₂ o₂ →
(tr₁ = tr₂ ∧ σ'₁ = σ'₂ ∧ o₁ = o₂) :=
begin
intros _ _ _ _ _ _ h1 h2,
induction h1 with s' s' s'' s''' tr1 tr2 h1 h3 IH,
{ intros _ _ _ h4 h5,
have : tr₂ = [] ∧ σ'₂ = s',
{ apply machine.final_star, tactic.swap,
from h4,
from h2,
},
cases this,
rw this_right at *,
rw this_left at *,
have : o₁ = o₂, apply target.output_deterministic; try{assumption},
cc,
},
{ intros _ _ _ h4 h5,
cases h4 with _ _ _ h4_b h4_tr₁ h4_tr₂ h4_a h4_a_1,
{ have : tr1 = [] ∧ s'' = s',
by { apply machine.final_step, from h5, all_goals{assumption}},
cases this, subst this_left, subst this_right,
simp,
apply IH; tauto,
},
{ cases (machine.step_deterministic _ _ _ _ code _ _ _ _ _ h4_a h1),
rw left at *,
rw right at *,
suffices : tr2 = h4_tr₂ ∧ s''' = σ'₂ ∧ o₁ = o₂, by tauto,
apply IH; tauto,
} }
end
lemma source_target_deterministic :
∀ (ctx : CONTEXT) (nd : ORACLE) (code_S : source.CODE) (σ_S σ_S' : source.STATE) (tr_S : TRACE) (i : INPUT)
(o_S : OUTPUT),
source.wf ctx code_S →
source.initial σ_S ctx i →
source.always_terminates code_S →
source.star nd code_S σ_S σ_S' tr_S →
source.final σ_S' o_S →
∀ (code_T : target.CODE) (σ_T σ_T' : target.STATE) (tr_T : TRACE) (o_T : OUTPUT),
target.initial σ_T ctx i →
jit.compile ctx code_S = some code_T →
target.star nd code_T σ_T σ_T' tr_T →
target.final σ_T' o_T →
(tr_S = tr_T ∧ o_S = o_T ∧ target.arch_safe σ_T σ_T') :=
begin
intros _ _ _ _ _ _ _ _ wf_S HinitS Sterminates h1 h2 _ _ _ _ _ HinitT h3 h4 h5,
let x := forward_simulation,
specialize x ctx nd code_S σ_S σ_S' tr_S i o_S (by assumption)
(by assumption) (by assumption) (by assumption) (by assumption) σ_T code_T (by assumption)
(by assumption),
cases x with T H,
cases H with H1 H2,
cases H2,
suffices : tr_S = tr_T ∧ T = σ_T',
{ cases this with left right,
rw left at *,
rw right at *,
split, by reflexivity,
split; try{assumption},
apply target.output_deterministic; by assumption,
},
have : tr_S = tr_T ∧ T = σ_T' ∧ o_S = o_T, apply target_deterministic; assumption,
cc,
end
-- The behavior of the jited target code is allowed by the source program.
lemma backward_simulation :
∀ (ctx : CONTEXT) (code_S : source.CODE) (code_T : target.CODE) (nd : ORACLE) (σ_T σ_T' : target.STATE)
(σ_S : source.STATE) (tr : TRACE) (i : INPUT) (o : OUTPUT),
source.wf ctx code_S →
source.always_terminates code_S →
target.initial σ_T ctx i →
jit.compile ctx code_S = some code_T →
target.star nd code_T σ_T σ_T' tr →
target.final σ_T' o →
source.initial σ_S ctx i →
∃ (σ_S' : source.STATE),
source.star nd code_S σ_S σ_S' tr ∧
source.final σ_S' o ∧
target.arch_safe σ_T σ_T' :=
begin
intros _ _ _ _ _ _ _ _ _ _ wf_S terminates_S a a_1 a_2 a_3 a_4,
let y := terminates_S,
dsimp [source.always_terminates] at *,
specialize terminates_S ctx nd σ_S i a_4,
cases terminates_S with s' x,
cases x with tr' x,
cases x with res' H,
existsi s',
cases H,
suffices : tr' = tr ∧ res' = o ∧ target.arch_safe σ_T σ_T',
{ cases this with left right, rw left at *,
cases right with left right, rw left at *, rw right at *,
repeat{split <|> assumption},
},
apply source_target_deterministic; assumption,
end
theorem arch_safety :
∀ (ctx : CONTEXT) (nd : ORACLE) (code_S : source.CODE) (code_T : target.CODE) (σ_T σ_T' : target.STATE)
(i : INPUT) (o : OUTPUT) (tr : TRACE),
source.wf ctx code_S →
source.always_terminates code_S →
jit.compile ctx code_S = some code_T →
target.initial σ_T ctx i →
target.star nd code_T σ_T σ_T' tr →
target.final σ_T' o →
target.arch_safe σ_T σ_T' :=
begin
intros _ _ _ _ _ _ _ _ _ wf_S sterm comp tinit tstar tfinal,
let y := sterm,
dsimp [source.always_terminates, machine.always_terminates] at *,
specialize sterm ctx nd,
cases (source.initial_inhabited i ctx) with σ_S sinit,
specialize sterm σ_S i sinit,
cases sterm with σ_S' sterm,
cases sterm with tr2 sterm,
cases sterm with res2 sterm,
cases sterm with sstar sfinal,
cases (forward_simulation ctx nd code_S σ_S σ_S' tr2 i res2 wf_S sinit y sstar sfinal σ_T code_T tinit comp)
with σ_T2' forward,
cases forward,
cases forward_right,
have : tr = tr2 ∧ σ_T' = σ_T2' ∧ o = res2, apply target_deterministic; assumption,
cc,
end
theorem interpreter_equivalence :
∀ (ctx : CONTEXT) (nd : ORACLE) (code_S : source.CODE) (code_T : target.CODE) (σ_S : source.STATE)
(σ_T : target.STATE) (i : INPUT) (o : OUTPUT) (tr : TRACE),
source.wf ctx code_S →
source.always_terminates code_S →
jit.compile ctx code_S = some code_T →
source.initial σ_S ctx i →
target.initial σ_T ctx i →
((∃ (σ_S' : source.STATE),
source.star nd code_S σ_S σ_S' tr ∧
source.final σ_S' o)
↔
(∃ (σ_T' : target.STATE),
target.star nd code_T σ_T σ_T' tr ∧
target.final σ_T' o)) :=
begin
intros _ _ _ _ _ _ _ _ _ wf_S sterm comp inits initt,
split; intros a,
{ cases a with σ_S' a,
cases a with sstar sfinal,
cases (forward_simulation ctx nd code_S σ_S σ_S' tr i o _ _ _ sstar sfinal σ_T code_T _ _)
with σ_T' a,
cases a with tstar a,
cases a with tfinal tinv,
existsi σ_T', cc,
all_goals{assumption},
},
{ cases a with σ_T' a,
cases a with tstar tfinal,
cases (backward_simulation ctx code_S code_T nd σ_T σ_T' _ _ _ _ _ _ _ _ _ _ _) with σ_S' a; repeat{any_goals{assumption}},
cases a with sstar a,
cases a with sfinal inv,
existsi σ_S',
cc,
},
end
|
0dcf8032bd125bad438eabc107e8513377516342 | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /tests/lean/server/diags.lean | 04f1868e7abcc50bb387f519b435a814ed28f809 | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 1,195 | lean | import Lean.Data.Lsp
open IO Lean Lsp
#eval (do
Ipc.runWith (←IO.appPath) #["--server"] do
let hIn ← Ipc.stdin
hIn.write (←FS.readBinFile "init_vscode_1_47_2.log")
hIn.flush
discard $ Ipc.readResponseAs 0 InitializeResult
Ipc.writeNotification ⟨"initialized", InitializedParams.mk⟩
hIn.write (←FS.readBinFile "open_content.log")
hIn.flush
let diags ← Ipc.collectDiagnostics 1 "file:///test.lean" 1
if diags.isEmpty then
throw $ userError "Test failed, no diagnostics received."
else
let diag := diags.getLast!
FS.writeFile "content_diag.json.produced" (toString <| toJson (diag : JsonRpc.Message))
if let some (refDiag : JsonRpc.Notification PublishDiagnosticsParams) :=
OptionM.run $ (Json.parse $ ←FS.readFile "content_diag.json").toOption >>= fromJson?
then
assert! (diag == refDiag)
else
throw $ userError "Failed parsing test file."
Ipc.writeRequest ⟨2, "shutdown", Json.null⟩
let shutResp ← Ipc.readResponseAs 2 Json
assert! shutResp.result.isNull
Ipc.writeNotification ⟨"exit", Json.null⟩
discard $ Ipc.waitForExit
: IO Unit)
|
4aca0cb19737c2b1fc07a3100986a882d9365e50 | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/analysis/calculus/cont_diff.lean | ba9b28dbd8a350d01e8a875a00dfeeeba2d1879c | [
"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 | 159,941 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import analysis.calculus.mean_value
import analysis.normed_space.multilinear
import analysis.calculus.formal_multilinear_series
import tactic.congrm
/-!
# Higher differentiability
A function is `C^1` on a domain if it is differentiable there, and its derivative is continuous.
By induction, it is `C^n` if it is `C^{n-1}` and its (n-1)-th derivative is `C^1` there or,
equivalently, if it is `C^1` and its derivative is `C^{n-1}`.
Finally, it is `C^∞` if it is `C^n` for all n.
We formalize these notions by defining iteratively the `n+1`-th derivative of a function as the
derivative of the `n`-th derivative. It is called `iterated_fderiv 𝕜 n f x` where `𝕜` is the
field, `n` is the number of iterations, `f` is the function and `x` is the point, and it is given
as an `n`-multilinear map. We also define a version `iterated_fderiv_within` relative to a domain,
as well as predicates `cont_diff_within_at`, `cont_diff_at`, `cont_diff_on` and
`cont_diff` saying that the function is `C^n` within a set at a point, at a point, on a set
and on the whole space respectively.
To avoid the issue of choice when choosing a derivative in sets where the derivative is not
necessarily unique, `cont_diff_on` is not defined directly in terms of the
regularity of the specific choice `iterated_fderiv_within 𝕜 n f s` inside `s`, but in terms of the
existence of a nice sequence of derivatives, expressed with a predicate
`has_ftaylor_series_up_to_on`.
We prove basic properties of these notions.
## Main definitions and results
Let `f : E → F` be a map between normed vector spaces over a nontrivially normed field `𝕜`.
* `has_ftaylor_series_up_to n f p`: expresses that the formal multilinear series `p` is a sequence
of iterated derivatives of `f`, up to the `n`-th term (where `n` is a natural number or `∞`).
* `has_ftaylor_series_up_to_on n f p s`: same thing, but inside a set `s`. The notion of derivative
is now taken inside `s`. In particular, derivatives don't have to be unique.
* `cont_diff 𝕜 n f`: expresses that `f` is `C^n`, i.e., it admits a Taylor series up to
rank `n`.
* `cont_diff_on 𝕜 n f s`: expresses that `f` is `C^n` in `s`.
* `cont_diff_at 𝕜 n f x`: expresses that `f` is `C^n` around `x`.
* `cont_diff_within_at 𝕜 n f s x`: expresses that `f` is `C^n` around `x` within the set `s`.
* `iterated_fderiv_within 𝕜 n f s x` is an `n`-th derivative of `f` over the field `𝕜` on the
set `s` at the point `x`. It is a continuous multilinear map from `E^n` to `F`, defined as a
derivative within `s` of `iterated_fderiv_within 𝕜 (n-1) f s` if one exists, and `0` otherwise.
* `iterated_fderiv 𝕜 n f x` is the `n`-th derivative of `f` over the field `𝕜` at the point `x`.
It is a continuous multilinear map from `E^n` to `F`, defined as a derivative of
`iterated_fderiv 𝕜 (n-1) f` if one exists, and `0` otherwise.
In sets of unique differentiability, `cont_diff_on 𝕜 n f s` can be expressed in terms of the
properties of `iterated_fderiv_within 𝕜 m f s` for `m ≤ n`. In the whole space,
`cont_diff 𝕜 n f` can be expressed in terms of the properties of `iterated_fderiv 𝕜 m f`
for `m ≤ n`.
We also prove that the usual operations (addition, multiplication, difference, composition, and
so on) preserve `C^n` functions.
## Implementation notes
The definitions in this file are designed to work on any field `𝕜`. They are sometimes slightly more
complicated than the naive definitions one would guess from the intuition over the real or complex
numbers, but they are designed to circumvent the lack of gluing properties and partitions of unity
in general. In the usual situations, they coincide with the usual definitions.
### Definition of `C^n` functions in domains
One could define `C^n` functions in a domain `s` by fixing an arbitrary choice of derivatives (this
is what we do with `iterated_fderiv_within`) and requiring that all these derivatives up to `n` are
continuous. If the derivative is not unique, this could lead to strange behavior like two `C^n`
functions `f` and `g` on `s` whose sum is not `C^n`. A better definition is thus to say that a
function is `C^n` inside `s` if it admits a sequence of derivatives up to `n` inside `s`.
This definition still has the problem that a function which is locally `C^n` would not need to
be `C^n`, as different choices of sequences of derivatives around different points might possibly
not be glued together to give a globally defined sequence of derivatives. (Note that this issue
can not happen over reals, thanks to partition of unity, but the behavior over a general field is
not so clear, and we want a definition for general fields). Also, there are locality
problems for the order parameter: one could image a function which, for each `n`, has a nice
sequence of derivatives up to order `n`, but they do not coincide for varying `n` and can therefore
not be glued to give rise to an infinite sequence of derivatives. This would give a function
which is `C^n` for all `n`, but not `C^∞`. We solve this issue by putting locality conditions
in space and order in our definition of `cont_diff_within_at` and `cont_diff_on`.
The resulting definition is slightly more complicated to work with (in fact not so much), but it
gives rise to completely satisfactory theorems.
For instance, with this definition, a real function which is `C^m` (but not better) on `(-1/m, 1/m)`
for each natural `m` is by definition `C^∞` at `0`.
There is another issue with the definition of `cont_diff_within_at 𝕜 n f s x`. We can
require the existence and good behavior of derivatives up to order `n` on a neighborhood of `x`
within `s`. However, this does not imply continuity or differentiability within `s` of the function
at `x` when `x` does not belong to `s`. Therefore, we require such existence and good behavior on
a neighborhood of `x` within `s ∪ {x}` (which appears as `insert x s` in this file).
### Side of the composition, and universe issues
With a naïve direct definition, the `n`-th derivative of a function belongs to the space
`E →L[𝕜] (E →L[𝕜] (E ... F)...)))` where there are n iterations of `E →L[𝕜]`. This space
may also be seen as the space of continuous multilinear functions on `n` copies of `E` with
values in `F`, by uncurrying. This is the point of view that is usually adopted in textbooks,
and that we also use. This means that the definition and the first proofs are slightly involved,
as one has to keep track of the uncurrying operation. The uncurrying can be done from the
left or from the right, amounting to defining the `n+1`-th derivative either as the derivative of
the `n`-th derivative, or as the `n`-th derivative of the derivative.
For proofs, it would be more convenient to use the latter approach (from the right),
as it means to prove things at the `n+1`-th step we only need to understand well enough the
derivative in `E →L[𝕜] F` (contrary to the approach from the left, where one would need to know
enough on the `n`-th derivative to deduce things on the `n+1`-th derivative).
However, the definition from the right leads to a universe polymorphism problem: if we define
`iterated_fderiv 𝕜 (n + 1) f x = iterated_fderiv 𝕜 n (fderiv 𝕜 f) x` by induction, we need to
generalize over all spaces (as `f` and `fderiv 𝕜 f` don't take values in the same space). It is
only possible to generalize over all spaces in some fixed universe in an inductive definition.
For `f : E → F`, then `fderiv 𝕜 f` is a map `E → (E →L[𝕜] F)`. Therefore, the definition will only
work if `F` and `E →L[𝕜] F` are in the same universe.
This issue does not appear with the definition from the left, where one does not need to generalize
over all spaces. Therefore, we use the definition from the left. This means some proofs later on
become a little bit more complicated: to prove that a function is `C^n`, the most efficient approach
is to exhibit a formula for its `n`-th derivative and prove it is continuous (contrary to the
inductive approach where one would prove smoothness statements without giving a formula for the
derivative). In the end, this approach is still satisfactory as it is good to have formulas for the
iterated derivatives in various constructions.
One point where we depart from this explicit approach is in the proof of smoothness of a
composition: there is a formula for the `n`-th derivative of a composition (Faà di Bruno's formula),
but it is very complicated and barely usable, while the inductive proof is very simple. Thus, we
give the inductive proof. As explained above, it works by generalizing over the target space, hence
it only works well if all spaces belong to the same universe. To get the general version, we lift
things to a common universe using a trick.
### Variables management
The textbook definitions and proofs use various identifications and abuse of notations, for instance
when saying that the natural space in which the derivative lives, i.e.,
`E →L[𝕜] (E →L[𝕜] ( ... →L[𝕜] F))`, is the same as a space of multilinear maps. When doing things
formally, we need to provide explicit maps for these identifications, and chase some diagrams to see
everything is compatible with the identifications. In particular, one needs to check that taking the
derivative and then doing the identification, or first doing the identification and then taking the
derivative, gives the same result. The key point for this is that taking the derivative commutes
with continuous linear equivalences. Therefore, we need to implement all our identifications with
continuous linear equivs.
## Notations
We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with
values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives.
In this file, we denote `⊤ : with_top ℕ` with `∞`.
## Tags
derivative, differentiability, higher derivative, `C^n`, multilinear, Taylor series, formal series
-/
noncomputable theory
open_locale classical big_operators nnreal
local notation `∞` := (⊤ : with_top ℕ)
universes u v w
local attribute [instance, priority 1001]
normed_add_comm_group.to_add_comm_group normed_space.to_module' add_comm_group.to_add_comm_monoid
open set fin filter
open_locale topological_space
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
{F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F]
{G : Type*} [normed_add_comm_group G] [normed_space 𝕜 G]
{X : Type*} [normed_add_comm_group X] [normed_space 𝕜 X]
{s s₁ t u : set E} {f f₁ : E → F} {g : F → G} {x : E} {c : F}
{b : E × F → G} {m n : with_top ℕ}
/-! ### Functions with a Taylor series on a domain -/
variable {p : E → formal_multilinear_series 𝕜 E F}
/-- `has_ftaylor_series_up_to_on n f p s` registers the fact that `p 0 = f` and `p (m+1)` is a
derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to
`has_fderiv_within_at` but for higher order derivatives. -/
structure has_ftaylor_series_up_to_on (n : with_top ℕ)
(f : E → F) (p : E → formal_multilinear_series 𝕜 E F) (s : set E) : Prop :=
(zero_eq : ∀ x ∈ s, (p x 0).uncurry0 = f x)
(fderiv_within : ∀ (m : ℕ) (hm : (m : with_top ℕ) < n), ∀ x ∈ s,
has_fderiv_within_at (λ y, p y m) (p x m.succ).curry_left s x)
(cont : ∀ (m : ℕ) (hm : (m : with_top ℕ) ≤ n), continuous_on (λ x, p x m) s)
lemma has_ftaylor_series_up_to_on.zero_eq'
(h : has_ftaylor_series_up_to_on n f p s) {x : E} (hx : x ∈ s) :
p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x) :=
by { rw ← h.zero_eq x hx, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ }
/-- If two functions coincide on a set `s`, then a Taylor series for the first one is as well a
Taylor series for the second one. -/
lemma has_ftaylor_series_up_to_on.congr
(h : has_ftaylor_series_up_to_on n f p s) (h₁ : ∀ x ∈ s, f₁ x = f x) :
has_ftaylor_series_up_to_on n f₁ p s :=
begin
refine ⟨λ x hx, _, h.fderiv_within, h.cont⟩,
rw h₁ x hx,
exact h.zero_eq x hx
end
lemma has_ftaylor_series_up_to_on.mono
(h : has_ftaylor_series_up_to_on n f p s) {t : set E} (hst : t ⊆ s) :
has_ftaylor_series_up_to_on n f p t :=
⟨λ x hx, h.zero_eq x (hst hx),
λ m hm x hx, (h.fderiv_within m hm x (hst hx)).mono hst,
λ m hm, (h.cont m hm).mono hst⟩
lemma has_ftaylor_series_up_to_on.of_le
(h : has_ftaylor_series_up_to_on n f p s) (hmn : m ≤ n) :
has_ftaylor_series_up_to_on m f p s :=
⟨h.zero_eq,
λ k hk x hx, h.fderiv_within k (lt_of_lt_of_le hk hmn) x hx,
λ k hk, h.cont k (le_trans hk hmn)⟩
lemma has_ftaylor_series_up_to_on.continuous_on
(h : has_ftaylor_series_up_to_on n f p s) : continuous_on f s :=
begin
have := (h.cont 0 bot_le).congr (λ x hx, (h.zero_eq' hx).symm),
rwa linear_isometry_equiv.comp_continuous_on_iff at this
end
lemma has_ftaylor_series_up_to_on_zero_iff :
has_ftaylor_series_up_to_on 0 f p s ↔ continuous_on f s ∧ (∀ x ∈ s, (p x 0).uncurry0 = f x) :=
begin
refine ⟨λ H, ⟨H.continuous_on, H.zero_eq⟩,
λ H, ⟨H.2, λ m hm, false.elim (not_le.2 hm bot_le), _⟩⟩,
assume m hm,
obtain rfl : m = 0, by exact_mod_cast (hm.antisymm (zero_le _)),
have : ∀ x ∈ s, p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x),
by { assume x hx, rw ← H.2 x hx, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ },
rw [continuous_on_congr this, linear_isometry_equiv.comp_continuous_on_iff],
exact H.1
end
lemma has_ftaylor_series_up_to_on_top_iff :
(has_ftaylor_series_up_to_on ∞ f p s) ↔ (∀ (n : ℕ), has_ftaylor_series_up_to_on n f p s) :=
begin
split,
{ assume H n, exact H.of_le le_top },
{ assume H,
split,
{ exact (H 0).zero_eq },
{ assume m hm,
apply (H m.succ).fderiv_within m (with_top.coe_lt_coe.2 (lt_add_one m)) },
{ assume m hm,
apply (H m).cont m le_rfl } }
end
/-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this
series is a derivative of `f`. -/
lemma has_ftaylor_series_up_to_on.has_fderiv_within_at
(h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : x ∈ s) :
has_fderiv_within_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) s x :=
begin
have A : ∀ y ∈ s, f y = (continuous_multilinear_curry_fin0 𝕜 E F) (p y 0),
{ assume y hy, rw ← h.zero_eq y hy, refl },
suffices H : has_fderiv_within_at
(λ y, continuous_multilinear_curry_fin0 𝕜 E F (p y 0))
(continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) s x,
by exact H.congr A (A x hx),
rw linear_isometry_equiv.comp_has_fderiv_within_at_iff',
have : ((0 : ℕ) : with_top ℕ) < n :=
lt_of_lt_of_le (with_top.coe_lt_coe.2 nat.zero_lt_one) hn,
convert h.fderiv_within _ this x hx,
ext y v,
change (p x 1) (snoc 0 y) = (p x 1) (cons y v),
unfold_coes,
congr' with i,
rw unique.eq_default i,
refl
end
lemma has_ftaylor_series_up_to_on.differentiable_on
(h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s :=
λ x hx, (h.has_fderiv_within_at hn hx).differentiable_within_at
/-- If a function has a Taylor series at order at least `1` on a neighborhood of `x`, then the term
of order `1` of this series is a derivative of `f` at `x`. -/
lemma has_ftaylor_series_up_to_on.has_fderiv_at
(h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : s ∈ 𝓝 x) :
has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) x :=
(h.has_fderiv_within_at hn (mem_of_mem_nhds hx)).has_fderiv_at hx
/-- If a function has a Taylor series at order at least `1` on a neighborhood of `x`, then
in a neighborhood of `x`, the term of order `1` of this series is a derivative of `f`. -/
lemma has_ftaylor_series_up_to_on.eventually_has_fderiv_at
(h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : s ∈ 𝓝 x) :
∀ᶠ y in 𝓝 x, has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p y 1)) y :=
(eventually_eventually_nhds.2 hx).mono $ λ y hy, h.has_fderiv_at hn hy
/-- If a function has a Taylor series at order at least `1` on a neighborhood of `x`, then
it is differentiable at `x`. -/
lemma has_ftaylor_series_up_to_on.differentiable_at
(h : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hx : s ∈ 𝓝 x) :
differentiable_at 𝕜 f x :=
(h.has_fderiv_at hn hx).differentiable_at
/-- `p` is a Taylor series of `f` up to `n+1` if and only if `p` is a Taylor series up to `n`, and
`p (n + 1)` is a derivative of `p n`. -/
theorem has_ftaylor_series_up_to_on_succ_iff_left {n : ℕ} :
has_ftaylor_series_up_to_on (n + 1) f p s ↔
has_ftaylor_series_up_to_on n f p s
∧ (∀ x ∈ s, has_fderiv_within_at (λ y, p y n) (p x n.succ).curry_left s x)
∧ continuous_on (λ x, p x (n + 1)) s :=
begin
split,
{ assume h,
exact ⟨h.of_le (with_top.coe_le_coe.2 (nat.le_succ n)),
h.fderiv_within _ (with_top.coe_lt_coe.2 (lt_add_one n)),
h.cont (n + 1) le_rfl⟩ },
{ assume h,
split,
{ exact h.1.zero_eq },
{ assume m hm,
by_cases h' : m < n,
{ exact h.1.fderiv_within m (with_top.coe_lt_coe.2 h') },
{ have : m = n := nat.eq_of_lt_succ_of_not_lt (with_top.coe_lt_coe.1 hm) h',
rw this,
exact h.2.1 } },
{ assume m hm,
by_cases h' : m ≤ n,
{ apply h.1.cont m (with_top.coe_le_coe.2 h') },
{ have : m = (n + 1) := le_antisymm (with_top.coe_le_coe.1 hm) (not_le.1 h'),
rw this,
exact h.2.2 } } }
end
/-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n`
for `p 1`, which is a derivative of `f`. -/
theorem has_ftaylor_series_up_to_on_succ_iff_right {n : ℕ} :
has_ftaylor_series_up_to_on ((n + 1) : ℕ) f p s ↔
(∀ x ∈ s, (p x 0).uncurry0 = f x)
∧ (∀ x ∈ s, has_fderiv_within_at (λ y, p y 0) (p x 1).curry_left s x)
∧ has_ftaylor_series_up_to_on n
(λ x, continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) (λ x, (p x).shift) s :=
begin
split,
{ assume H,
refine ⟨H.zero_eq, H.fderiv_within 0 (with_top.coe_lt_coe.2 (nat.succ_pos n)), _⟩,
split,
{ assume x hx, refl },
{ assume m (hm : (m : with_top ℕ) < n) x (hx : x ∈ s),
have A : (m.succ : with_top ℕ) < n.succ,
by { rw with_top.coe_lt_coe at ⊢ hm, exact nat.lt_succ_iff.mpr hm },
change has_fderiv_within_at
((continuous_multilinear_curry_right_equiv' 𝕜 m E F).symm
∘ (λ (y : E), p y m.succ))
(p x m.succ.succ).curry_right.curry_left s x,
rw linear_isometry_equiv.comp_has_fderiv_within_at_iff',
convert H.fderiv_within _ A x hx,
ext y v,
change (p x m.succ.succ) (snoc (cons y (init v)) (v (last _)))
= (p x (nat.succ (nat.succ m))) (cons y v),
rw [← cons_snoc_eq_snoc_cons, snoc_init_self] },
{ assume m (hm : (m : with_top ℕ) ≤ n),
have A : (m.succ : with_top ℕ) ≤ n.succ,
by { rw with_top.coe_le_coe at ⊢ hm, exact nat.pred_le_iff.mp hm },
change continuous_on ((continuous_multilinear_curry_right_equiv' 𝕜 m E F).symm
∘ (λ (y : E), p y m.succ)) s,
rw linear_isometry_equiv.comp_continuous_on_iff,
exact H.cont _ A } },
{ rintros ⟨Hzero_eq, Hfderiv_zero, Htaylor⟩,
split,
{ exact Hzero_eq },
{ assume m (hm : (m : with_top ℕ) < n.succ) x (hx : x ∈ s),
cases m,
{ exact Hfderiv_zero x hx },
{ have A : (m : with_top ℕ) < n,
by { rw with_top.coe_lt_coe at hm ⊢, exact nat.lt_of_succ_lt_succ hm },
have : has_fderiv_within_at ((continuous_multilinear_curry_right_equiv' 𝕜 m E F).symm
∘ (λ (y : E), p y m.succ)) ((p x).shift m.succ).curry_left s x :=
Htaylor.fderiv_within _ A x hx,
rw linear_isometry_equiv.comp_has_fderiv_within_at_iff' at this,
convert this,
ext y v,
change (p x (nat.succ (nat.succ m))) (cons y v)
= (p x m.succ.succ) (snoc (cons y (init v)) (v (last _))),
rw [← cons_snoc_eq_snoc_cons, snoc_init_self] } },
{ assume m (hm : (m : with_top ℕ) ≤ n.succ),
cases m,
{ have : differentiable_on 𝕜 (λ x, p x 0) s :=
λ x hx, (Hfderiv_zero x hx).differentiable_within_at,
exact this.continuous_on },
{ have A : (m : with_top ℕ) ≤ n,
by { rw with_top.coe_le_coe at hm ⊢, exact nat.lt_succ_iff.mp hm },
have : continuous_on ((continuous_multilinear_curry_right_equiv' 𝕜 m E F).symm
∘ (λ (y : E), p y m.succ)) s :=
Htaylor.cont _ A,
rwa linear_isometry_equiv.comp_continuous_on_iff at this } } }
end
/-! ### Smooth functions within a set around a point -/
variable (𝕜)
/-- A function is continuously differentiable up to order `n` within a set `s` at a point `x` if
it admits continuous derivatives up to order `n` in a neighborhood of `x` in `s ∪ {x}`.
For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may
depend on the finite order we consider).
For instance, a real function which is `C^m` on `(-1/m, 1/m)` for each natural `m`, but not
better, is `C^∞` at `0` within `univ`.
-/
def cont_diff_within_at (n : with_top ℕ) (f : E → F) (s : set E) (x : E) :=
∀ (m : ℕ), (m : with_top ℕ) ≤ n →
∃ u ∈ 𝓝[insert x s] x, ∃ p : E → formal_multilinear_series 𝕜 E F,
has_ftaylor_series_up_to_on m f p u
variable {𝕜}
lemma cont_diff_within_at_nat {n : ℕ} :
cont_diff_within_at 𝕜 n f s x ↔
∃ u ∈ 𝓝[insert x s] x, ∃ p : E → formal_multilinear_series 𝕜 E F,
has_ftaylor_series_up_to_on n f p u :=
⟨λ H, H n le_rfl, λ ⟨u, hu, p, hp⟩ m hm, ⟨u, hu, p, hp.of_le hm⟩⟩
lemma cont_diff_within_at.of_le
(h : cont_diff_within_at 𝕜 n f s x) (hmn : m ≤ n) :
cont_diff_within_at 𝕜 m f s x :=
λ k hk, h k (le_trans hk hmn)
lemma cont_diff_within_at_iff_forall_nat_le :
cont_diff_within_at 𝕜 n f s x ↔ ∀ m : ℕ, ↑m ≤ n → cont_diff_within_at 𝕜 m f s x :=
⟨λ H m hm, H.of_le hm, λ H m hm, H m hm _ le_rfl⟩
lemma cont_diff_within_at_top :
cont_diff_within_at 𝕜 ∞ f s x ↔ ∀ (n : ℕ), cont_diff_within_at 𝕜 n f s x :=
cont_diff_within_at_iff_forall_nat_le.trans $ by simp only [forall_prop_of_true, le_top]
lemma cont_diff_within_at.continuous_within_at
(h : cont_diff_within_at 𝕜 n f s x) : continuous_within_at f s x :=
begin
rcases h 0 bot_le with ⟨u, hu, p, H⟩,
rw [mem_nhds_within_insert] at hu,
exact (H.continuous_on.continuous_within_at hu.1).mono_of_mem hu.2
end
lemma cont_diff_within_at.congr_of_eventually_eq
(h : cont_diff_within_at 𝕜 n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) :
cont_diff_within_at 𝕜 n f₁ s x :=
λ m hm, let ⟨u, hu, p, H⟩ := h m hm in
⟨{x ∈ u | f₁ x = f x}, filter.inter_mem hu (mem_nhds_within_insert.2 ⟨hx, h₁⟩), p,
(H.mono (sep_subset _ _)).congr (λ _, and.right)⟩
lemma cont_diff_within_at.congr_of_eventually_eq_insert
(h : cont_diff_within_at 𝕜 n f s x) (h₁ : f₁ =ᶠ[𝓝[insert x s] x] f) :
cont_diff_within_at 𝕜 n f₁ s x :=
h.congr_of_eventually_eq (nhds_within_mono x (subset_insert x s) h₁)
(mem_of_mem_nhds_within (mem_insert x s) h₁ : _)
lemma cont_diff_within_at.congr_of_eventually_eq'
(h : cont_diff_within_at 𝕜 n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : x ∈ s) :
cont_diff_within_at 𝕜 n f₁ s x :=
h.congr_of_eventually_eq h₁ $ h₁.self_of_nhds_within hx
lemma filter.eventually_eq.cont_diff_within_at_iff
(h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) :
cont_diff_within_at 𝕜 n f₁ s x ↔ cont_diff_within_at 𝕜 n f s x :=
⟨λ H, cont_diff_within_at.congr_of_eventually_eq H h₁.symm hx.symm,
λ H, H.congr_of_eventually_eq h₁ hx⟩
lemma cont_diff_within_at.congr
(h : cont_diff_within_at 𝕜 n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) :
cont_diff_within_at 𝕜 n f₁ s x :=
h.congr_of_eventually_eq (filter.eventually_eq_of_mem self_mem_nhds_within h₁) hx
lemma cont_diff_within_at.congr'
(h : cont_diff_within_at 𝕜 n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : x ∈ s) :
cont_diff_within_at 𝕜 n f₁ s x :=
h.congr h₁ (h₁ _ hx)
lemma cont_diff_within_at.mono_of_mem
(h : cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : s ∈ 𝓝[t] x) :
cont_diff_within_at 𝕜 n f t x :=
begin
assume m hm,
rcases h m hm with ⟨u, hu, p, H⟩,
exact ⟨u, nhds_within_le_of_mem (insert_mem_nhds_within_insert hst) hu, p, H⟩
end
lemma cont_diff_within_at.mono
(h : cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : t ⊆ s) :
cont_diff_within_at 𝕜 n f t x :=
h.mono_of_mem $ filter.mem_of_superset self_mem_nhds_within hst
lemma cont_diff_within_at.congr_nhds
(h : cont_diff_within_at 𝕜 n f s x) {t : set E} (hst : 𝓝[s] x = 𝓝[t] x) :
cont_diff_within_at 𝕜 n f t x :=
h.mono_of_mem $ hst ▸ self_mem_nhds_within
lemma cont_diff_within_at_congr_nhds {t : set E} (hst : 𝓝[s] x = 𝓝[t] x) :
cont_diff_within_at 𝕜 n f s x ↔ cont_diff_within_at 𝕜 n f t x :=
⟨λ h, h.congr_nhds hst, λ h, h.congr_nhds hst.symm⟩
lemma cont_diff_within_at_inter' (h : t ∈ 𝓝[s] x) :
cont_diff_within_at 𝕜 n f (s ∩ t) x ↔ cont_diff_within_at 𝕜 n f s x :=
cont_diff_within_at_congr_nhds $ eq.symm $ nhds_within_restrict'' _ h
lemma cont_diff_within_at_inter (h : t ∈ 𝓝 x) :
cont_diff_within_at 𝕜 n f (s ∩ t) x ↔ cont_diff_within_at 𝕜 n f s x :=
cont_diff_within_at_inter' (mem_nhds_within_of_mem_nhds h)
/-- If a function is `C^n` within a set at a point, with `n ≥ 1`, then it is differentiable
within this set at this point. -/
lemma cont_diff_within_at.differentiable_within_at'
(h : cont_diff_within_at 𝕜 n f s x) (hn : 1 ≤ n) :
differentiable_within_at 𝕜 f (insert x s) x :=
begin
rcases h 1 hn with ⟨u, hu, p, H⟩,
rcases mem_nhds_within.1 hu with ⟨t, t_open, xt, tu⟩,
rw inter_comm at tu,
have := ((H.mono tu).differentiable_on le_rfl) x ⟨mem_insert x s, xt⟩,
exact (differentiable_within_at_inter (is_open.mem_nhds t_open xt)).1 this,
end
lemma cont_diff_within_at.differentiable_within_at
(h : cont_diff_within_at 𝕜 n f s x) (hn : 1 ≤ n) :
differentiable_within_at 𝕜 f s x :=
(h.differentiable_within_at' hn).mono (subset_insert x s)
/-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/
theorem cont_diff_within_at_succ_iff_has_fderiv_within_at {n : ℕ} :
cont_diff_within_at 𝕜 ((n + 1) : ℕ) f s x
↔ ∃ u ∈ 𝓝[insert x s] x, ∃ f' : E → (E →L[𝕜] F),
(∀ x ∈ u, has_fderiv_within_at f (f' x) u x) ∧ (cont_diff_within_at 𝕜 n f' u x) :=
begin
split,
{ assume h,
rcases h n.succ le_rfl with ⟨u, hu, p, Hp⟩,
refine ⟨u, hu, λ y, (continuous_multilinear_curry_fin1 𝕜 E F) (p y 1),
λ y hy, Hp.has_fderiv_within_at (with_top.coe_le_coe.2 (nat.le_add_left 1 n)) hy, _⟩,
assume m hm,
refine ⟨u, _, λ (y : E), (p y).shift, _⟩,
{ convert self_mem_nhds_within,
have : x ∈ insert x s, by simp,
exact (insert_eq_of_mem (mem_of_mem_nhds_within this hu)) },
{ rw has_ftaylor_series_up_to_on_succ_iff_right at Hp,
exact Hp.2.2.of_le hm } },
{ rintros ⟨u, hu, f', f'_eq_deriv, Hf'⟩,
rw cont_diff_within_at_nat,
rcases Hf' n le_rfl with ⟨v, hv, p', Hp'⟩,
refine ⟨v ∩ u, _, λ x, (p' x).unshift (f x), _⟩,
{ apply filter.inter_mem _ hu,
apply nhds_within_le_of_mem hu,
exact nhds_within_mono _ (subset_insert x u) hv },
{ rw has_ftaylor_series_up_to_on_succ_iff_right,
refine ⟨λ y hy, rfl, λ y hy, _, _⟩,
{ change has_fderiv_within_at (λ z, (continuous_multilinear_curry_fin0 𝕜 E F).symm (f z))
((formal_multilinear_series.unshift (p' y) (f y) 1).curry_left) (v ∩ u) y,
rw linear_isometry_equiv.comp_has_fderiv_within_at_iff',
convert (f'_eq_deriv y hy.2).mono (inter_subset_right v u),
rw ← Hp'.zero_eq y hy.1,
ext z,
change ((p' y 0) (init (@cons 0 (λ i, E) z 0))) (@cons 0 (λ i, E) z 0 (last 0))
= ((p' y 0) 0) z,
unfold_coes,
congr },
{ convert (Hp'.mono (inter_subset_left v u)).congr (λ x hx, Hp'.zero_eq x hx.1),
{ ext x y,
change p' x 0 (init (@snoc 0 (λ i : fin 1, E) 0 y)) y = p' x 0 0 y,
rw init_snoc },
{ ext x k v y,
change p' x k (init (@snoc k (λ i : fin k.succ, E) v y))
(@snoc k (λ i : fin k.succ, E) v y (last k)) = p' x k v y,
rw [snoc_last, init_snoc] } } } }
end
/-- One direction of `cont_diff_within_at_succ_iff_has_fderiv_within_at`, but where all derivatives
are taken within the same set. -/
lemma cont_diff_within_at.has_fderiv_within_at_nhds {n : ℕ}
(hf : cont_diff_within_at 𝕜 (n + 1 : ℕ) f s x) :
∃ u ∈ 𝓝[insert x s] x, u ⊆ insert x s ∧ ∃ f' : E → E →L[𝕜] F,
(∀ x ∈ u, has_fderiv_within_at f (f' x) s x) ∧ cont_diff_within_at 𝕜 n f' s x :=
begin
obtain ⟨u, hu, f', huf', hf'⟩ := cont_diff_within_at_succ_iff_has_fderiv_within_at.mp hf,
obtain ⟨w, hw, hxw, hwu⟩ := mem_nhds_within.mp hu,
rw [inter_comm] at hwu,
refine ⟨insert x s ∩ w, inter_mem_nhds_within _ (hw.mem_nhds hxw), inter_subset_left _ _,
f', λ y hy, _, _⟩,
{ refine ((huf' y $ hwu hy).mono hwu).mono_of_mem _,
refine mem_of_superset _ (inter_subset_inter_left _ (subset_insert _ _)),
refine inter_mem_nhds_within _ (hw.mem_nhds hy.2) },
{ exact hf'.mono_of_mem (nhds_within_mono _ (subset_insert _ _) hu) }
end
/-- A version of `cont_diff_within_at_succ_iff_has_fderiv_within_at` where all derivatives
are taken within the same set. This lemma assumes `x ∈ s`. -/
lemma cont_diff_within_at_succ_iff_has_fderiv_within_at_of_mem {n : ℕ} (hx : x ∈ s) :
cont_diff_within_at 𝕜 (n + 1 : ℕ) f s x
↔ ∃ u ∈ 𝓝[s] x, u ⊆ s ∧ ∃ f' : E → E →L[𝕜] F,
(∀ x ∈ u, has_fderiv_within_at f (f' x) s x) ∧ cont_diff_within_at 𝕜 n f' s x :=
begin
split,
{ intro hf, simpa only [insert_eq_of_mem hx] using hf.has_fderiv_within_at_nhds },
rw [cont_diff_within_at_succ_iff_has_fderiv_within_at, insert_eq_of_mem hx],
rintro ⟨u, hu, hus, f', huf', hf'⟩,
exact ⟨u, hu, f', λ y hy, (huf' y hy).mono hus, hf'.mono hus⟩
end
/-! ### Smooth functions within a set -/
variable (𝕜)
/-- A function is continuously differentiable up to `n` on `s` if, for any point `x` in `s`, it
admits continuous derivatives up to order `n` on a neighborhood of `x` in `s`.
For `n = ∞`, we only require that this holds up to any finite order (where the neighborhood may
depend on the finite order we consider).
-/
definition cont_diff_on (n : with_top ℕ) (f : E → F) (s : set E) :=
∀ x ∈ s, cont_diff_within_at 𝕜 n f s x
variable {𝕜}
lemma cont_diff_on.cont_diff_within_at (h : cont_diff_on 𝕜 n f s) (hx : x ∈ s) :
cont_diff_within_at 𝕜 n f s x :=
h x hx
lemma cont_diff_within_at.cont_diff_on {m : ℕ}
(hm : (m : with_top ℕ) ≤ n) (h : cont_diff_within_at 𝕜 n f s x) :
∃ u ∈ 𝓝[insert x s] x, u ⊆ insert x s ∧ cont_diff_on 𝕜 m f u :=
begin
rcases h m hm with ⟨u, u_nhd, p, hp⟩,
refine ⟨u ∩ insert x s, filter.inter_mem u_nhd self_mem_nhds_within,
inter_subset_right _ _, _⟩,
assume y hy m' hm',
refine ⟨u ∩ insert x s, _, p, (hp.mono (inter_subset_left _ _)).of_le hm'⟩,
convert self_mem_nhds_within,
exact insert_eq_of_mem hy
end
protected lemma cont_diff_within_at.eventually {n : ℕ}
(h : cont_diff_within_at 𝕜 n f s x) :
∀ᶠ y in 𝓝[insert x s] x, cont_diff_within_at 𝕜 n f s y :=
begin
rcases h.cont_diff_on le_rfl with ⟨u, hu, hu_sub, hd⟩,
have : ∀ᶠ (y : E) in 𝓝[insert x s] x, u ∈ 𝓝[insert x s] y ∧ y ∈ u,
from (eventually_nhds_within_nhds_within.2 hu).and hu,
refine this.mono (λ y hy, (hd y hy.2).mono_of_mem _),
exact nhds_within_mono y (subset_insert _ _) hy.1
end
lemma cont_diff_on.of_le (h : cont_diff_on 𝕜 n f s) (hmn : m ≤ n) :
cont_diff_on 𝕜 m f s :=
λ x hx, (h x hx).of_le hmn
lemma cont_diff_on_iff_forall_nat_le :
cont_diff_on 𝕜 n f s ↔ ∀ m : ℕ, ↑m ≤ n → cont_diff_on 𝕜 m f s :=
⟨λ H m hm, H.of_le hm, λ H x hx m hm, H m hm x hx m le_rfl⟩
lemma cont_diff_on_top :
cont_diff_on 𝕜 ∞ f s ↔ ∀ (n : ℕ), cont_diff_on 𝕜 n f s :=
cont_diff_on_iff_forall_nat_le.trans $ by simp only [le_top, forall_prop_of_true]
lemma cont_diff_on_all_iff_nat :
(∀ n, cont_diff_on 𝕜 n f s) ↔ (∀ n : ℕ, cont_diff_on 𝕜 n f s) :=
begin
refine ⟨λ H n, H n, _⟩,
rintro H (_|n),
exacts [cont_diff_on_top.2 H, H n]
end
lemma cont_diff_on.continuous_on
(h : cont_diff_on 𝕜 n f s) : continuous_on f s :=
λ x hx, (h x hx).continuous_within_at
lemma cont_diff_on.congr
(h : cont_diff_on 𝕜 n f s) (h₁ : ∀ x ∈ s, f₁ x = f x) :
cont_diff_on 𝕜 n f₁ s :=
λ x hx, (h x hx).congr h₁ (h₁ x hx)
lemma cont_diff_on_congr (h₁ : ∀ x ∈ s, f₁ x = f x) :
cont_diff_on 𝕜 n f₁ s ↔ cont_diff_on 𝕜 n f s :=
⟨λ H, H.congr (λ x hx, (h₁ x hx).symm), λ H, H.congr h₁⟩
lemma cont_diff_on.mono
(h : cont_diff_on 𝕜 n f s) {t : set E} (hst : t ⊆ s) :
cont_diff_on 𝕜 n f t :=
λ x hx, (h x (hst hx)).mono hst
lemma cont_diff_on.congr_mono
(hf : cont_diff_on 𝕜 n f s) (h₁ : ∀ x ∈ s₁, f₁ x = f x) (hs : s₁ ⊆ s) :
cont_diff_on 𝕜 n f₁ s₁ :=
(hf.mono hs).congr h₁
/-- If a function is `C^n` on a set with `n ≥ 1`, then it is differentiable there. -/
lemma cont_diff_on.differentiable_on
(h : cont_diff_on 𝕜 n f s) (hn : 1 ≤ n) : differentiable_on 𝕜 f s :=
λ x hx, (h x hx).differentiable_within_at hn
/-- If a function is `C^n` around each point in a set, then it is `C^n` on the set. -/
lemma cont_diff_on_of_locally_cont_diff_on
(h : ∀ x ∈ s, ∃u, is_open u ∧ x ∈ u ∧ cont_diff_on 𝕜 n f (s ∩ u)) :
cont_diff_on 𝕜 n f s :=
begin
assume x xs,
rcases h x xs with ⟨u, u_open, xu, hu⟩,
apply (cont_diff_within_at_inter _).1 (hu x ⟨xs, xu⟩),
exact is_open.mem_nhds u_open xu
end
/-- A function is `C^(n + 1)` on a domain iff locally, it has a derivative which is `C^n`. -/
theorem cont_diff_on_succ_iff_has_fderiv_within_at {n : ℕ} :
cont_diff_on 𝕜 ((n + 1) : ℕ) f s
↔ ∀ x ∈ s, ∃ u ∈ 𝓝[insert x s] x, ∃ f' : E → (E →L[𝕜] F),
(∀ x ∈ u, has_fderiv_within_at f (f' x) u x) ∧ (cont_diff_on 𝕜 n f' u) :=
begin
split,
{ assume h x hx,
rcases (h x hx) n.succ le_rfl with ⟨u, hu, p, Hp⟩,
refine ⟨u, hu, λ y, (continuous_multilinear_curry_fin1 𝕜 E F) (p y 1),
λ y hy, Hp.has_fderiv_within_at (with_top.coe_le_coe.2 (nat.le_add_left 1 n)) hy, _⟩,
rw has_ftaylor_series_up_to_on_succ_iff_right at Hp,
assume z hz m hm,
refine ⟨u, _, λ (x : E), (p x).shift, Hp.2.2.of_le hm⟩,
convert self_mem_nhds_within,
exact insert_eq_of_mem hz, },
{ assume h x hx,
rw cont_diff_within_at_succ_iff_has_fderiv_within_at,
rcases h x hx with ⟨u, u_nhbd, f', hu, hf'⟩,
have : x ∈ u := mem_of_mem_nhds_within (mem_insert _ _) u_nhbd,
exact ⟨u, u_nhbd, f', hu, hf' x this⟩ }
end
/-! ### Iterated derivative within a set -/
variable (𝕜)
/--
The `n`-th derivative of a function along a set, defined inductively by saying that the `n+1`-th
derivative of `f` is the derivative of the `n`-th derivative of `f` along this set, together with
an uncurrying step to see it as a multilinear map in `n+1` variables..
-/
noncomputable def iterated_fderiv_within (n : ℕ) (f : E → F) (s : set E) :
E → (E [×n]→L[𝕜] F) :=
nat.rec_on n
(λ x, continuous_multilinear_map.curry0 𝕜 E (f x))
(λ n rec x, continuous_linear_map.uncurry_left (fderiv_within 𝕜 rec s x))
/-- Formal Taylor series associated to a function within a set. -/
def ftaylor_series_within (f : E → F) (s : set E) (x : E) : formal_multilinear_series 𝕜 E F :=
λ n, iterated_fderiv_within 𝕜 n f s x
variable {𝕜}
@[simp] lemma iterated_fderiv_within_zero_apply (m : (fin 0) → E) :
(iterated_fderiv_within 𝕜 0 f s x : ((fin 0) → E) → F) m = f x := rfl
lemma iterated_fderiv_within_zero_eq_comp :
iterated_fderiv_within 𝕜 0 f s = (continuous_multilinear_curry_fin0 𝕜 E F).symm ∘ f := rfl
lemma iterated_fderiv_within_succ_apply_left {n : ℕ} (m : fin (n + 1) → E):
(iterated_fderiv_within 𝕜 (n + 1) f s x : (fin (n + 1) → E) → F) m
= (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s x : E → (E [×n]→L[𝕜] F))
(m 0) (tail m) := rfl
/-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv,
and the derivative of the `n`-th derivative. -/
lemma iterated_fderiv_within_succ_eq_comp_left {n : ℕ} :
iterated_fderiv_within 𝕜 (n + 1) f s =
(continuous_multilinear_curry_left_equiv 𝕜 (λ(i : fin (n + 1)), E) F)
∘ (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n f s) s) := rfl
theorem iterated_fderiv_within_succ_apply_right {n : ℕ}
(hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : fin (n + 1) → E) :
(iterated_fderiv_within 𝕜 (n + 1) f s x : (fin (n + 1) → E) → F) m
= iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s x (init m) (m (last n)) :=
begin
induction n with n IH generalizing x,
{ rw [iterated_fderiv_within_succ_eq_comp_left, iterated_fderiv_within_zero_eq_comp,
iterated_fderiv_within_zero_apply,
function.comp_apply, linear_isometry_equiv.comp_fderiv_within _ (hs x hx)],
refl },
{ let I := continuous_multilinear_curry_right_equiv' 𝕜 n E F,
have A : ∀ y ∈ s, iterated_fderiv_within 𝕜 n.succ f s y
= (I ∘ (iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s)) y,
by { assume y hy, ext m, rw @IH m y hy, refl },
calc
(iterated_fderiv_within 𝕜 (n+2) f s x : (fin (n+2) → E) → F) m =
(fderiv_within 𝕜 (iterated_fderiv_within 𝕜 n.succ f s) s x
: E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) : rfl
... = (fderiv_within 𝕜 (I ∘ (iterated_fderiv_within 𝕜 n (fderiv_within 𝕜 f s) s)) s x
: E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) :
by rw fderiv_within_congr (hs x hx) A (A x hx)
... = (I ∘ fderiv_within 𝕜 ((iterated_fderiv_within 𝕜 n (fderiv_within 𝕜 f s) s)) s x
: E → (E [×(n + 1)]→L[𝕜] F)) (m 0) (tail m) :
by { rw linear_isometry_equiv.comp_fderiv_within _ (hs x hx), refl }
... = (fderiv_within 𝕜 ((iterated_fderiv_within 𝕜 n (λ y, fderiv_within 𝕜 f s y) s)) s x
: E → (E [×n]→L[𝕜] (E →L[𝕜] F))) (m 0) (init (tail m)) ((tail m) (last n)) : rfl
... = iterated_fderiv_within 𝕜 (nat.succ n) (λ y, fderiv_within 𝕜 f s y) s x
(init m) (m (last (n + 1))) :
by { rw [iterated_fderiv_within_succ_apply_left, tail_init_eq_init_tail], refl } }
end
/-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv,
and the `n`-th derivative of the derivative. -/
lemma iterated_fderiv_within_succ_eq_comp_right {n : ℕ} (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) :
iterated_fderiv_within 𝕜 (n + 1) f s x =
((continuous_multilinear_curry_right_equiv' 𝕜 n E F)
∘ (iterated_fderiv_within 𝕜 n (λy, fderiv_within 𝕜 f s y) s)) x :=
by { ext m, rw iterated_fderiv_within_succ_apply_right hs hx, refl }
@[simp] lemma iterated_fderiv_within_one_apply
(hs : unique_diff_on 𝕜 s) (hx : x ∈ s) (m : (fin 1) → E) :
(iterated_fderiv_within 𝕜 1 f s x : ((fin 1) → E) → F) m
= (fderiv_within 𝕜 f s x : E → F) (m 0) :=
by { rw [iterated_fderiv_within_succ_apply_right hs hx, iterated_fderiv_within_zero_apply], refl }
/-- If two functions coincide on a set `s` of unique differentiability, then their iterated
differentials within this set coincide. -/
lemma iterated_fderiv_within_congr {n : ℕ}
(hs : unique_diff_on 𝕜 s) (hL : ∀y∈s, f₁ y = f y) (hx : x ∈ s) :
iterated_fderiv_within 𝕜 n f₁ s x = iterated_fderiv_within 𝕜 n f s x :=
begin
induction n with n IH generalizing x,
{ ext m, simp [hL x hx] },
{ have : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f₁ s y) s x
= fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) s x :=
fderiv_within_congr (hs x hx) (λ y hy, IH hy) (IH hx),
ext m,
rw [iterated_fderiv_within_succ_apply_left, iterated_fderiv_within_succ_apply_left, this] }
end
/-- The iterated differential within a set `s` at a point `x` is not modified if one intersects
`s` with an open set containing `x`. -/
lemma iterated_fderiv_within_inter_open {n : ℕ} (hu : is_open u)
(hs : unique_diff_on 𝕜 (s ∩ u)) (hx : x ∈ s ∩ u) :
iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x :=
begin
induction n with n IH generalizing x,
{ ext m, simp },
{ have A : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f (s ∩ u) y) (s ∩ u) x
= fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) (s ∩ u) x :=
fderiv_within_congr (hs x hx) (λ y hy, IH hy) (IH hx),
have B : fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) (s ∩ u) x
= fderiv_within 𝕜 (λ y, iterated_fderiv_within 𝕜 n f s y) s x :=
fderiv_within_inter (is_open.mem_nhds hu hx.2)
((unique_diff_within_at_inter (is_open.mem_nhds hu hx.2)).1 (hs x hx)),
ext m,
rw [iterated_fderiv_within_succ_apply_left, iterated_fderiv_within_succ_apply_left, A, B] }
end
/-- The iterated differential within a set `s` at a point `x` is not modified if one intersects
`s` with a neighborhood of `x` within `s`. -/
lemma iterated_fderiv_within_inter' {n : ℕ}
(hu : u ∈ 𝓝[s] x) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) :
iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x :=
begin
obtain ⟨v, v_open, xv, vu⟩ : ∃ v, is_open v ∧ x ∈ v ∧ v ∩ s ⊆ u := mem_nhds_within.1 hu,
have A : (s ∩ u) ∩ v = s ∩ v,
{ apply subset.antisymm (inter_subset_inter (inter_subset_left _ _) (subset.refl _)),
exact λ y ⟨ys, yv⟩, ⟨⟨ys, vu ⟨yv, ys⟩⟩, yv⟩ },
have : iterated_fderiv_within 𝕜 n f (s ∩ v) x = iterated_fderiv_within 𝕜 n f s x :=
iterated_fderiv_within_inter_open v_open (hs.inter v_open) ⟨xs, xv⟩,
rw ← this,
have : iterated_fderiv_within 𝕜 n f ((s ∩ u) ∩ v) x = iterated_fderiv_within 𝕜 n f (s ∩ u) x,
{ refine iterated_fderiv_within_inter_open v_open _ ⟨⟨xs, vu ⟨xv, xs⟩⟩, xv⟩,
rw A,
exact hs.inter v_open },
rw A at this,
rw ← this
end
/-- The iterated differential within a set `s` at a point `x` is not modified if one intersects
`s` with a neighborhood of `x`. -/
lemma iterated_fderiv_within_inter {n : ℕ}
(hu : u ∈ 𝓝 x) (hs : unique_diff_on 𝕜 s) (xs : x ∈ s) :
iterated_fderiv_within 𝕜 n f (s ∩ u) x = iterated_fderiv_within 𝕜 n f s x :=
iterated_fderiv_within_inter' (mem_nhds_within_of_mem_nhds hu) hs xs
@[simp] lemma cont_diff_on_zero :
cont_diff_on 𝕜 0 f s ↔ continuous_on f s :=
begin
refine ⟨λ H, H.continuous_on, λ H, _⟩,
assume x hx m hm,
have : (m : with_top ℕ) = 0 := le_antisymm hm bot_le,
rw this,
refine ⟨insert x s, self_mem_nhds_within, ftaylor_series_within 𝕜 f s, _⟩,
rw has_ftaylor_series_up_to_on_zero_iff,
exact ⟨by rwa insert_eq_of_mem hx, λ x hx, by simp [ftaylor_series_within]⟩
end
lemma cont_diff_within_at_zero (hx : x ∈ s) :
cont_diff_within_at 𝕜 0 f s x ↔ ∃ u ∈ 𝓝[s] x, continuous_on f (s ∩ u) :=
begin
split,
{ intros h,
obtain ⟨u, H, p, hp⟩ := h 0 (by norm_num),
refine ⟨u, _, _⟩,
{ simpa [hx] using H },
{ simp only [with_top.coe_zero, has_ftaylor_series_up_to_on_zero_iff] at hp,
exact hp.1.mono (inter_subset_right s u) } },
{ rintros ⟨u, H, hu⟩,
rw ← cont_diff_within_at_inter' H,
have h' : x ∈ s ∩ u := ⟨hx, mem_of_mem_nhds_within hx H⟩,
exact (cont_diff_on_zero.mpr hu).cont_diff_within_at h' }
end
/-- On a set with unique differentiability, any choice of iterated differential has to coincide
with the one we have chosen in `iterated_fderiv_within 𝕜 m f s`. -/
theorem has_ftaylor_series_up_to_on.eq_ftaylor_series_of_unique_diff_on
(h : has_ftaylor_series_up_to_on n f p s)
{m : ℕ} (hmn : (m : with_top ℕ) ≤ n) (hs : unique_diff_on 𝕜 s) (hx : x ∈ s) :
p x m = iterated_fderiv_within 𝕜 m f s x :=
begin
induction m with m IH generalizing x,
{ rw [h.zero_eq' hx, iterated_fderiv_within_zero_eq_comp] },
{ have A : (m : with_top ℕ) < n := lt_of_lt_of_le (with_top.coe_lt_coe.2 (lt_add_one m)) hmn,
have : has_fderiv_within_at (λ (y : E), iterated_fderiv_within 𝕜 m f s y)
(continuous_multilinear_map.curry_left (p x (nat.succ m))) s x :=
(h.fderiv_within m A x hx).congr (λ y hy, (IH (le_of_lt A) hy).symm) (IH (le_of_lt A) hx).symm,
rw [iterated_fderiv_within_succ_eq_comp_left, function.comp_apply,
this.fderiv_within (hs x hx)],
exact (continuous_multilinear_map.uncurry_curry_left _).symm }
end
/-- When a function is `C^n` in a set `s` of unique differentiability, it admits
`ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/
theorem cont_diff_on.ftaylor_series_within
(h : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) :
has_ftaylor_series_up_to_on n f (ftaylor_series_within 𝕜 f s) s :=
begin
split,
{ assume x hx,
simp only [ftaylor_series_within, continuous_multilinear_map.uncurry0_apply,
iterated_fderiv_within_zero_apply] },
{ assume m hm x hx,
rcases (h x hx) m.succ (with_top.add_one_le_of_lt hm) with ⟨u, hu, p, Hp⟩,
rw insert_eq_of_mem hx at hu,
rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩,
rw inter_comm at ho,
have : p x m.succ = ftaylor_series_within 𝕜 f s x m.succ,
{ change p x m.succ = iterated_fderiv_within 𝕜 m.succ f s x,
rw ← iterated_fderiv_within_inter (is_open.mem_nhds o_open xo) hs hx,
exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on le_rfl
(hs.inter o_open) ⟨hx, xo⟩ },
rw [← this, ← has_fderiv_within_at_inter (is_open.mem_nhds o_open xo)],
have A : ∀ y ∈ s ∩ o, p y m = ftaylor_series_within 𝕜 f s y m,
{ rintros y ⟨hy, yo⟩,
change p y m = iterated_fderiv_within 𝕜 m f s y,
rw ← iterated_fderiv_within_inter (is_open.mem_nhds o_open yo) hs hy,
exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on (with_top.coe_le_coe.2 (nat.le_succ m))
(hs.inter o_open) ⟨hy, yo⟩ },
exact ((Hp.mono ho).fderiv_within m (with_top.coe_lt_coe.2 (lt_add_one m)) x ⟨hx, xo⟩).congr
(λ y hy, (A y hy).symm) (A x ⟨hx, xo⟩).symm },
{ assume m hm,
apply continuous_on_of_locally_continuous_on,
assume x hx,
rcases h x hx m hm with ⟨u, hu, p, Hp⟩,
rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩,
rw insert_eq_of_mem hx at ho,
rw inter_comm at ho,
refine ⟨o, o_open, xo, _⟩,
have A : ∀ y ∈ s ∩ o, p y m = ftaylor_series_within 𝕜 f s y m,
{ rintros y ⟨hy, yo⟩,
change p y m = iterated_fderiv_within 𝕜 m f s y,
rw ← iterated_fderiv_within_inter (is_open.mem_nhds o_open yo) hs hy,
exact (Hp.mono ho).eq_ftaylor_series_of_unique_diff_on le_rfl
(hs.inter o_open) ⟨hy, yo⟩ },
exact ((Hp.mono ho).cont m le_rfl).congr (λ y hy, (A y hy).symm) }
end
lemma cont_diff_on_of_continuous_on_differentiable_on
(Hcont : ∀ (m : ℕ), (m : with_top ℕ) ≤ n →
continuous_on (λ x, iterated_fderiv_within 𝕜 m f s x) s)
(Hdiff : ∀ (m : ℕ), (m : with_top ℕ) < n →
differentiable_on 𝕜 (λ x, iterated_fderiv_within 𝕜 m f s x) s) :
cont_diff_on 𝕜 n f s :=
begin
assume x hx m hm,
rw insert_eq_of_mem hx,
refine ⟨s, self_mem_nhds_within, ftaylor_series_within 𝕜 f s, _⟩,
split,
{ assume y hy,
simp only [ftaylor_series_within, continuous_multilinear_map.uncurry0_apply,
iterated_fderiv_within_zero_apply] },
{ assume k hk y hy,
convert (Hdiff k (lt_of_lt_of_le hk hm) y hy).has_fderiv_within_at,
simp only [ftaylor_series_within, iterated_fderiv_within_succ_eq_comp_left,
continuous_linear_equiv.coe_apply, function.comp_app, coe_fn_coe_base],
exact continuous_linear_map.curry_uncurry_left _ },
{ assume k hk,
exact Hcont k (le_trans hk hm) }
end
lemma cont_diff_on_of_differentiable_on
(h : ∀(m : ℕ), (m : with_top ℕ) ≤ n → differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s) :
cont_diff_on 𝕜 n f s :=
cont_diff_on_of_continuous_on_differentiable_on
(λ m hm, (h m hm).continuous_on) (λ m hm, (h m (le_of_lt hm)))
lemma cont_diff_on.continuous_on_iterated_fderiv_within {m : ℕ}
(h : cont_diff_on 𝕜 n f s) (hmn : (m : with_top ℕ) ≤ n) (hs : unique_diff_on 𝕜 s) :
continuous_on (iterated_fderiv_within 𝕜 m f s) s :=
(h.ftaylor_series_within hs).cont m hmn
lemma cont_diff_on.differentiable_on_iterated_fderiv_within {m : ℕ}
(h : cont_diff_on 𝕜 n f s) (hmn : (m : with_top ℕ) < n) (hs : unique_diff_on 𝕜 s) :
differentiable_on 𝕜 (iterated_fderiv_within 𝕜 m f s) s :=
λ x hx, ((h.ftaylor_series_within hs).fderiv_within m hmn x hx).differentiable_within_at
lemma cont_diff_on_iff_continuous_on_differentiable_on
(hs : unique_diff_on 𝕜 s) :
cont_diff_on 𝕜 n f s ↔
(∀ (m : ℕ), (m : with_top ℕ) ≤ n →
continuous_on (λ x, iterated_fderiv_within 𝕜 m f s x) s)
∧ (∀ (m : ℕ), (m : with_top ℕ) < n →
differentiable_on 𝕜 (λ x, iterated_fderiv_within 𝕜 m f s x) s) :=
begin
split,
{ assume h,
split,
{ assume m hm, exact h.continuous_on_iterated_fderiv_within hm hs },
{ assume m hm, exact h.differentiable_on_iterated_fderiv_within hm hs } },
{ assume h,
exact cont_diff_on_of_continuous_on_differentiable_on h.1 h.2 }
end
lemma cont_diff_on_succ_of_fderiv_within {n : ℕ} (hf : differentiable_on 𝕜 f s)
(h : cont_diff_on 𝕜 n (λ y, fderiv_within 𝕜 f s y) s) :
cont_diff_on 𝕜 ((n + 1) : ℕ) f s :=
begin
intros x hx,
rw [cont_diff_within_at_succ_iff_has_fderiv_within_at, insert_eq_of_mem hx],
exact ⟨s, self_mem_nhds_within, fderiv_within 𝕜 f s,
λ y hy, (hf y hy).has_fderiv_within_at, h x hx⟩
end
/-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is
differentiable there, and its derivative (expressed with `fderiv_within`) is `C^n`. -/
theorem cont_diff_on_succ_iff_fderiv_within {n : ℕ} (hs : unique_diff_on 𝕜 s) :
cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔
differentiable_on 𝕜 f s ∧ cont_diff_on 𝕜 n (λ y, fderiv_within 𝕜 f s y) s :=
begin
refine ⟨λ H, _, λ h, cont_diff_on_succ_of_fderiv_within h.1 h.2⟩,
refine ⟨H.differentiable_on (with_top.coe_le_coe.2 (nat.le_add_left 1 n)), λ x hx, _⟩,
rcases cont_diff_within_at_succ_iff_has_fderiv_within_at.1 (H x hx)
with ⟨u, hu, f', hff', hf'⟩,
rcases mem_nhds_within.1 hu with ⟨o, o_open, xo, ho⟩,
rw [inter_comm, insert_eq_of_mem hx] at ho,
have := hf'.mono ho,
rw cont_diff_within_at_inter' (mem_nhds_within_of_mem_nhds (is_open.mem_nhds o_open xo))
at this,
apply this.congr_of_eventually_eq' _ hx,
have : o ∩ s ∈ 𝓝[s] x := mem_nhds_within.2 ⟨o, o_open, xo, subset.refl _⟩,
rw inter_comm at this,
apply filter.eventually_eq_of_mem this (λ y hy, _),
have A : fderiv_within 𝕜 f (s ∩ o) y = f' y :=
((hff' y (ho hy)).mono ho).fderiv_within (hs.inter o_open y hy),
rwa fderiv_within_inter (is_open.mem_nhds o_open hy.2) (hs y hy.1) at A
end
/-- A function is `C^(n + 1)` on an open domain if and only if it is
differentiable there, and its derivative (expressed with `fderiv`) is `C^n`. -/
theorem cont_diff_on_succ_iff_fderiv_of_open {n : ℕ} (hs : is_open s) :
cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔
differentiable_on 𝕜 f s ∧ cont_diff_on 𝕜 n (λ y, fderiv 𝕜 f y) s :=
begin
rw cont_diff_on_succ_iff_fderiv_within hs.unique_diff_on,
congrm _ ∧ _,
apply cont_diff_on_congr,
assume x hx,
exact fderiv_within_of_open hs hx
end
/-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable
there, and its derivative (expressed with `fderiv_within`) is `C^∞`. -/
theorem cont_diff_on_top_iff_fderiv_within (hs : unique_diff_on 𝕜 s) :
cont_diff_on 𝕜 ∞ f s ↔
differentiable_on 𝕜 f s ∧ cont_diff_on 𝕜 ∞ (λ y, fderiv_within 𝕜 f s y) s :=
begin
split,
{ assume h,
refine ⟨h.differentiable_on le_top, _⟩,
apply cont_diff_on_top.2 (λ n, ((cont_diff_on_succ_iff_fderiv_within hs).1 _).2),
exact h.of_le le_top },
{ assume h,
refine cont_diff_on_top.2 (λ n, _),
have A : (n : with_top ℕ) ≤ ∞ := le_top,
apply ((cont_diff_on_succ_iff_fderiv_within hs).2 ⟨h.1, h.2.of_le A⟩).of_le,
exact with_top.coe_le_coe.2 (nat.le_succ n) }
end
/-- A function is `C^∞` on an open domain if and only if it is differentiable there, and its
derivative (expressed with `fderiv`) is `C^∞`. -/
theorem cont_diff_on_top_iff_fderiv_of_open (hs : is_open s) :
cont_diff_on 𝕜 ∞ f s ↔
differentiable_on 𝕜 f s ∧ cont_diff_on 𝕜 ∞ (λ y, fderiv 𝕜 f y) s :=
begin
rw cont_diff_on_top_iff_fderiv_within hs.unique_diff_on,
congrm _ ∧ _,
apply cont_diff_on_congr,
assume x hx,
exact fderiv_within_of_open hs hx
end
lemma cont_diff_on.fderiv_within
(hf : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) :
cont_diff_on 𝕜 m (λ y, fderiv_within 𝕜 f s y) s :=
begin
cases m,
{ change ∞ + 1 ≤ n at hmn,
have : n = ∞, by simpa using hmn,
rw this at hf,
exact ((cont_diff_on_top_iff_fderiv_within hs).1 hf).2 },
{ change (m.succ : with_top ℕ) ≤ n at hmn,
exact ((cont_diff_on_succ_iff_fderiv_within hs).1 (hf.of_le hmn)).2 }
end
lemma cont_diff_on.fderiv_of_open
(hf : cont_diff_on 𝕜 n f s) (hs : is_open s) (hmn : m + 1 ≤ n) :
cont_diff_on 𝕜 m (λ y, fderiv 𝕜 f y) s :=
(hf.fderiv_within hs.unique_diff_on hmn).congr (λ x hx, (fderiv_within_of_open hs hx).symm)
lemma cont_diff_on.continuous_on_fderiv_within
(h : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) :
continuous_on (λ x, fderiv_within 𝕜 f s x) s :=
((cont_diff_on_succ_iff_fderiv_within hs).1 (h.of_le hn)).2.continuous_on
lemma cont_diff_on.continuous_on_fderiv_of_open
(h : cont_diff_on 𝕜 n f s) (hs : is_open s) (hn : 1 ≤ n) :
continuous_on (λ x, fderiv 𝕜 f x) s :=
((cont_diff_on_succ_iff_fderiv_of_open hs).1 (h.of_le hn)).2.continuous_on
lemma cont_diff_within_at.fderiv_within'
(hf : cont_diff_within_at 𝕜 n f s x) (hs : ∀ᶠ y in 𝓝[insert x s] x, unique_diff_within_at 𝕜 s y)
(hmn : m + 1 ≤ n) :
cont_diff_within_at 𝕜 m (fderiv_within 𝕜 f s) s x :=
begin
have : ∀ k : ℕ, (k + 1 : with_top ℕ) ≤ n → cont_diff_within_at 𝕜 k (fderiv_within 𝕜 f s) s x,
{ intros k hkn,
obtain ⟨v, hv, -, f', hvf', hf'⟩ := (hf.of_le hkn).has_fderiv_within_at_nhds,
apply hf'.congr_of_eventually_eq_insert,
filter_upwards [hv, hs],
exact λ y hy h2y, (hvf' y hy).fderiv_within h2y },
induction m using with_top.rec_top_coe,
{ obtain rfl := eq_top_iff.mpr hmn,
rw [cont_diff_within_at_top],
exact λ m, this m le_top },
exact this m hmn
end
lemma cont_diff_within_at.fderiv_within
(hf : cont_diff_within_at 𝕜 n f s x) (hs : unique_diff_on 𝕜 s)
(hmn : (m + 1 : with_top ℕ) ≤ n) (hxs : x ∈ s) :
cont_diff_within_at 𝕜 m (fderiv_within 𝕜 f s) s x :=
hf.fderiv_within' (by { rw [insert_eq_of_mem hxs], exact eventually_of_mem self_mem_nhds_within hs})
hmn
/-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is
continuous. -/
lemma cont_diff_on.continuous_on_fderiv_within_apply
(h : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hn : 1 ≤ n) :
continuous_on (λp : E × E, (fderiv_within 𝕜 f s p.1 : E → F) p.2) (s ×ˢ univ) :=
begin
have A : continuous (λq : (E →L[𝕜] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous,
have B : continuous_on (λp : E × E, (fderiv_within 𝕜 f s p.1, p.2)) (s ×ˢ univ),
{ apply continuous_on.prod _ continuous_snd.continuous_on,
exact continuous_on.comp (h.continuous_on_fderiv_within hs hn) continuous_fst.continuous_on
(prod_subset_preimage_fst _ _) },
exact A.comp_continuous_on B
end
/-! ### Functions with a Taylor series on the whole space -/
/-- `has_ftaylor_series_up_to n f p` registers the fact that `p 0 = f` and `p (m+1)` is a
derivative of `p m` for `m < n`, and is continuous for `m ≤ n`. This is a predicate analogous to
`has_fderiv_at` but for higher order derivatives. -/
structure has_ftaylor_series_up_to (n : with_top ℕ)
(f : E → F) (p : E → formal_multilinear_series 𝕜 E F) : Prop :=
(zero_eq : ∀ x, (p x 0).uncurry0 = f x)
(fderiv : ∀ (m : ℕ) (hm : (m : with_top ℕ) < n), ∀ x,
has_fderiv_at (λ y, p y m) (p x m.succ).curry_left x)
(cont : ∀ (m : ℕ) (hm : (m : with_top ℕ) ≤ n), continuous (λ x, p x m))
lemma has_ftaylor_series_up_to.zero_eq'
(h : has_ftaylor_series_up_to n f p) (x : E) :
p x 0 = (continuous_multilinear_curry_fin0 𝕜 E F).symm (f x) :=
by { rw ← h.zero_eq x, symmetry, exact continuous_multilinear_map.uncurry0_curry0 _ }
lemma has_ftaylor_series_up_to_on_univ_iff :
has_ftaylor_series_up_to_on n f p univ ↔ has_ftaylor_series_up_to n f p :=
begin
split,
{ assume H,
split,
{ exact λ x, H.zero_eq x (mem_univ x) },
{ assume m hm x,
rw ← has_fderiv_within_at_univ,
exact H.fderiv_within m hm x (mem_univ x) },
{ assume m hm,
rw continuous_iff_continuous_on_univ,
exact H.cont m hm } },
{ assume H,
split,
{ exact λ x hx, H.zero_eq x },
{ assume m hm x hx,
rw has_fderiv_within_at_univ,
exact H.fderiv m hm x },
{ assume m hm,
rw ← continuous_iff_continuous_on_univ,
exact H.cont m hm } }
end
lemma has_ftaylor_series_up_to.has_ftaylor_series_up_to_on
(h : has_ftaylor_series_up_to n f p) (s : set E) :
has_ftaylor_series_up_to_on n f p s :=
(has_ftaylor_series_up_to_on_univ_iff.2 h).mono (subset_univ _)
lemma has_ftaylor_series_up_to.of_le
(h : has_ftaylor_series_up_to n f p) (hmn : m ≤ n) :
has_ftaylor_series_up_to m f p :=
by { rw ← has_ftaylor_series_up_to_on_univ_iff at h ⊢, exact h.of_le hmn }
lemma has_ftaylor_series_up_to.continuous
(h : has_ftaylor_series_up_to n f p) : continuous f :=
begin
rw ← has_ftaylor_series_up_to_on_univ_iff at h,
rw continuous_iff_continuous_on_univ,
exact h.continuous_on
end
lemma has_ftaylor_series_up_to_zero_iff :
has_ftaylor_series_up_to 0 f p ↔ continuous f ∧ (∀ x, (p x 0).uncurry0 = f x) :=
by simp [has_ftaylor_series_up_to_on_univ_iff.symm, continuous_iff_continuous_on_univ,
has_ftaylor_series_up_to_on_zero_iff]
/-- If a function has a Taylor series at order at least `1`, then the term of order `1` of this
series is a derivative of `f`. -/
lemma has_ftaylor_series_up_to.has_fderiv_at
(h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) (x : E) :
has_fderiv_at f (continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) x :=
begin
rw [← has_fderiv_within_at_univ],
exact (has_ftaylor_series_up_to_on_univ_iff.2 h).has_fderiv_within_at hn (mem_univ _)
end
lemma has_ftaylor_series_up_to.differentiable
(h : has_ftaylor_series_up_to n f p) (hn : 1 ≤ n) : differentiable 𝕜 f :=
λ x, (h.has_fderiv_at hn x).differentiable_at
/-- `p` is a Taylor series of `f` up to `n+1` if and only if `p.shift` is a Taylor series up to `n`
for `p 1`, which is a derivative of `f`. -/
theorem has_ftaylor_series_up_to_succ_iff_right {n : ℕ} :
has_ftaylor_series_up_to ((n + 1) : ℕ) f p ↔
(∀ x, (p x 0).uncurry0 = f x)
∧ (∀ x, has_fderiv_at (λ y, p y 0) (p x 1).curry_left x)
∧ has_ftaylor_series_up_to n
(λ x, continuous_multilinear_curry_fin1 𝕜 E F (p x 1)) (λ x, (p x).shift) :=
by simp only [has_ftaylor_series_up_to_on_succ_iff_right, ← has_ftaylor_series_up_to_on_univ_iff,
mem_univ, forall_true_left, has_fderiv_within_at_univ]
/-! ### Smooth functions at a point -/
variable (𝕜)
/-- A function is continuously differentiable up to `n` at a point `x` if, for any integer `k ≤ n`,
there is a neighborhood of `x` where `f` admits derivatives up to order `n`, which are continuous.
-/
def cont_diff_at (n : with_top ℕ) (f : E → F) (x : E) :=
cont_diff_within_at 𝕜 n f univ x
variable {𝕜}
theorem cont_diff_within_at_univ :
cont_diff_within_at 𝕜 n f univ x ↔ cont_diff_at 𝕜 n f x :=
iff.rfl
lemma cont_diff_at_top :
cont_diff_at 𝕜 ∞ f x ↔ ∀ (n : ℕ), cont_diff_at 𝕜 n f x :=
by simp [← cont_diff_within_at_univ, cont_diff_within_at_top]
lemma cont_diff_at.cont_diff_within_at
(h : cont_diff_at 𝕜 n f x) : cont_diff_within_at 𝕜 n f s x :=
h.mono (subset_univ _)
lemma cont_diff_within_at.cont_diff_at
(h : cont_diff_within_at 𝕜 n f s x) (hx : s ∈ 𝓝 x) :
cont_diff_at 𝕜 n f x :=
by rwa [cont_diff_at, ← cont_diff_within_at_inter hx, univ_inter]
lemma cont_diff_at.congr_of_eventually_eq
(h : cont_diff_at 𝕜 n f x) (hg : f₁ =ᶠ[𝓝 x] f) :
cont_diff_at 𝕜 n f₁ x :=
h.congr_of_eventually_eq' (by rwa nhds_within_univ) (mem_univ x)
lemma cont_diff_at.of_le
(h : cont_diff_at 𝕜 n f x) (hmn : m ≤ n) :
cont_diff_at 𝕜 m f x :=
h.of_le hmn
lemma cont_diff_at.continuous_at
(h : cont_diff_at 𝕜 n f x) : continuous_at f x :=
by simpa [continuous_within_at_univ] using h.continuous_within_at
/-- If a function is `C^n` with `n ≥ 1` at a point, then it is differentiable there. -/
lemma cont_diff_at.differentiable_at
(h : cont_diff_at 𝕜 n f x) (hn : 1 ≤ n) : differentiable_at 𝕜 f x :=
by simpa [hn, differentiable_within_at_univ] using h.differentiable_within_at
/-- A function is `C^(n + 1)` at a point iff locally, it has a derivative which is `C^n`. -/
theorem cont_diff_at_succ_iff_has_fderiv_at {n : ℕ} :
cont_diff_at 𝕜 ((n + 1) : ℕ) f x
↔ (∃ f' : E → E →L[𝕜] F, (∃ u ∈ 𝓝 x, ∀ x ∈ u, has_fderiv_at f (f' x) x)
∧ cont_diff_at 𝕜 n f' x) :=
begin
rw [← cont_diff_within_at_univ, cont_diff_within_at_succ_iff_has_fderiv_within_at],
simp only [nhds_within_univ, exists_prop, mem_univ, insert_eq_of_mem],
split,
{ rintros ⟨u, H, f', h_fderiv, h_cont_diff⟩,
rcases mem_nhds_iff.mp H with ⟨t, htu, ht, hxt⟩,
refine ⟨f', ⟨t, _⟩, h_cont_diff.cont_diff_at H⟩,
refine ⟨mem_nhds_iff.mpr ⟨t, subset.rfl, ht, hxt⟩, _⟩,
intros y hyt,
refine (h_fderiv y (htu hyt)).has_fderiv_at _,
exact mem_nhds_iff.mpr ⟨t, htu, ht, hyt⟩ },
{ rintros ⟨f', ⟨u, H, h_fderiv⟩, h_cont_diff⟩,
refine ⟨u, H, f', _, h_cont_diff.cont_diff_within_at⟩,
intros x hxu,
exact (h_fderiv x hxu).has_fderiv_within_at }
end
protected theorem cont_diff_at.eventually {n : ℕ} (h : cont_diff_at 𝕜 n f x) :
∀ᶠ y in 𝓝 x, cont_diff_at 𝕜 n f y :=
by simpa [nhds_within_univ] using h.eventually
/-! ### Smooth functions -/
variable (𝕜)
/-- A function is continuously differentiable up to `n` if it admits derivatives up to
order `n`, which are continuous. Contrary to the case of definitions in domains (where derivatives
might not be unique) we do not need to localize the definition in space or time.
-/
definition cont_diff (n : with_top ℕ) (f : E → F) :=
∃ p : E → formal_multilinear_series 𝕜 E F, has_ftaylor_series_up_to n f p
variable {𝕜}
theorem cont_diff_on_univ : cont_diff_on 𝕜 n f univ ↔ cont_diff 𝕜 n f :=
begin
split,
{ assume H,
use ftaylor_series_within 𝕜 f univ,
rw ← has_ftaylor_series_up_to_on_univ_iff,
exact H.ftaylor_series_within unique_diff_on_univ },
{ rintros ⟨p, hp⟩ x hx m hm,
exact ⟨univ, filter.univ_sets _, p, (hp.has_ftaylor_series_up_to_on univ).of_le hm⟩ }
end
lemma cont_diff_iff_cont_diff_at : cont_diff 𝕜 n f ↔ ∀ x, cont_diff_at 𝕜 n f x :=
by simp [← cont_diff_on_univ, cont_diff_on, cont_diff_at]
lemma cont_diff.cont_diff_at (h : cont_diff 𝕜 n f) : cont_diff_at 𝕜 n f x :=
cont_diff_iff_cont_diff_at.1 h x
lemma cont_diff.cont_diff_within_at (h : cont_diff 𝕜 n f) : cont_diff_within_at 𝕜 n f s x :=
h.cont_diff_at.cont_diff_within_at
lemma cont_diff_top : cont_diff 𝕜 ∞ f ↔ ∀ (n : ℕ), cont_diff 𝕜 n f :=
by simp [cont_diff_on_univ.symm, cont_diff_on_top]
lemma cont_diff_all_iff_nat : (∀ n, cont_diff 𝕜 n f) ↔ (∀ n : ℕ, cont_diff 𝕜 n f) :=
by simp only [← cont_diff_on_univ, cont_diff_on_all_iff_nat]
lemma cont_diff.cont_diff_on (h : cont_diff 𝕜 n f) : cont_diff_on 𝕜 n f s :=
(cont_diff_on_univ.2 h).mono (subset_univ _)
@[simp] lemma cont_diff_zero : cont_diff 𝕜 0 f ↔ continuous f :=
begin
rw [← cont_diff_on_univ, continuous_iff_continuous_on_univ],
exact cont_diff_on_zero
end
lemma cont_diff_at_zero : cont_diff_at 𝕜 0 f x ↔ ∃ u ∈ 𝓝 x, continuous_on f u :=
by { rw ← cont_diff_within_at_univ, simp [cont_diff_within_at_zero, nhds_within_univ] }
theorem cont_diff_at_one_iff : cont_diff_at 𝕜 1 f x ↔
∃ f' : E → (E →L[𝕜] F), ∃ u ∈ 𝓝 x, continuous_on f' u ∧ ∀ x ∈ u, has_fderiv_at f (f' x) x :=
by simp_rw [show (1 : with_top ℕ) = (0 + 1 : ℕ), from (zero_add 1).symm,
cont_diff_at_succ_iff_has_fderiv_at, show ((0 : ℕ) : with_top ℕ) = 0, from rfl,
cont_diff_at_zero, exists_mem_and_iff antitone_bforall antitone_continuous_on, and_comm]
lemma cont_diff.of_le (h : cont_diff 𝕜 n f) (hmn : m ≤ n) : cont_diff 𝕜 m f :=
cont_diff_on_univ.1 $ (cont_diff_on_univ.2 h).of_le hmn
lemma cont_diff.of_succ {n : ℕ} (h : cont_diff 𝕜 (n + 1) f) : cont_diff 𝕜 n f :=
h.of_le $ with_top.coe_le_coe.mpr le_self_add
lemma cont_diff.one_of_succ {n : ℕ} (h : cont_diff 𝕜 (n + 1) f) : cont_diff 𝕜 1 f :=
h.of_le $ with_top.coe_le_coe.mpr le_add_self
lemma cont_diff.continuous (h : cont_diff 𝕜 n f) : continuous f :=
cont_diff_zero.1 (h.of_le bot_le)
/-- If a function is `C^n` with `n ≥ 1`, then it is differentiable. -/
lemma cont_diff.differentiable (h : cont_diff 𝕜 n f) (hn : 1 ≤ n) : differentiable 𝕜 f :=
differentiable_on_univ.1 $ (cont_diff_on_univ.2 h).differentiable_on hn
/-! ### Iterated derivative -/
variable (𝕜)
/-- The `n`-th derivative of a function, as a multilinear map, defined inductively. -/
noncomputable def iterated_fderiv (n : ℕ) (f : E → F) :
E → (E [×n]→L[𝕜] F) :=
nat.rec_on n
(λ x, continuous_multilinear_map.curry0 𝕜 E (f x))
(λ n rec x, continuous_linear_map.uncurry_left (fderiv 𝕜 rec x))
/-- Formal Taylor series associated to a function within a set. -/
def ftaylor_series (f : E → F) (x : E) : formal_multilinear_series 𝕜 E F :=
λ n, iterated_fderiv 𝕜 n f x
variable {𝕜}
@[simp] lemma iterated_fderiv_zero_apply (m : (fin 0) → E) :
(iterated_fderiv 𝕜 0 f x : ((fin 0) → E) → F) m = f x := rfl
lemma iterated_fderiv_zero_eq_comp :
iterated_fderiv 𝕜 0 f = (continuous_multilinear_curry_fin0 𝕜 E F).symm ∘ f := rfl
lemma iterated_fderiv_succ_apply_left {n : ℕ} (m : fin (n + 1) → E):
(iterated_fderiv 𝕜 (n + 1) f x : (fin (n + 1) → E) → F) m
= (fderiv 𝕜 (iterated_fderiv 𝕜 n f) x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) := rfl
/-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv,
and the derivative of the `n`-th derivative. -/
lemma iterated_fderiv_succ_eq_comp_left {n : ℕ} :
iterated_fderiv 𝕜 (n + 1) f =
(continuous_multilinear_curry_left_equiv 𝕜 (λ(i : fin (n + 1)), E) F)
∘ (fderiv 𝕜 (iterated_fderiv 𝕜 n f)) := rfl
lemma iterated_fderiv_within_univ {n : ℕ} :
iterated_fderiv_within 𝕜 n f univ = iterated_fderiv 𝕜 n f :=
begin
induction n with n IH,
{ ext x, simp },
{ ext x m,
rw [iterated_fderiv_succ_apply_left, iterated_fderiv_within_succ_apply_left, IH,
fderiv_within_univ] }
end
/-- In an open set, the iterated derivative within this set coincides with the global iterated
derivative. -/
lemma iterated_fderiv_within_of_is_open (n : ℕ) (hs : is_open s) :
eq_on (iterated_fderiv_within 𝕜 n f s) (iterated_fderiv 𝕜 n f) s :=
begin
induction n with n IH,
{ assume x hx,
ext1 m,
simp only [iterated_fderiv_within_zero_apply, iterated_fderiv_zero_apply] },
{ assume x hx,
rw [iterated_fderiv_succ_eq_comp_left, iterated_fderiv_within_succ_eq_comp_left],
dsimp,
congr' 1,
rw fderiv_within_of_open hs hx,
apply filter.eventually_eq.fderiv_eq,
filter_upwards [hs.mem_nhds hx],
exact IH }
end
lemma ftaylor_series_within_univ :
ftaylor_series_within 𝕜 f univ = ftaylor_series 𝕜 f :=
begin
ext1 x, ext1 n,
change iterated_fderiv_within 𝕜 n f univ x = iterated_fderiv 𝕜 n f x,
rw iterated_fderiv_within_univ
end
theorem iterated_fderiv_succ_apply_right {n : ℕ} (m : fin (n + 1) → E) :
(iterated_fderiv 𝕜 (n + 1) f x : (fin (n + 1) → E) → F) m
= iterated_fderiv 𝕜 n (λy, fderiv 𝕜 f y) x (init m) (m (last n)) :=
begin
rw [← iterated_fderiv_within_univ, ← iterated_fderiv_within_univ, ← fderiv_within_univ],
exact iterated_fderiv_within_succ_apply_right unique_diff_on_univ (mem_univ _) _
end
/-- Writing explicitly the `n+1`-th derivative as the composition of a currying linear equiv,
and the `n`-th derivative of the derivative. -/
lemma iterated_fderiv_succ_eq_comp_right {n : ℕ} :
iterated_fderiv 𝕜 (n + 1) f x =
((continuous_multilinear_curry_right_equiv' 𝕜 n E F)
∘ (iterated_fderiv 𝕜 n (λy, fderiv 𝕜 f y))) x :=
by { ext m, rw iterated_fderiv_succ_apply_right, refl }
@[simp] lemma iterated_fderiv_one_apply (m : (fin 1) → E) :
(iterated_fderiv 𝕜 1 f x : ((fin 1) → E) → F) m
= (fderiv 𝕜 f x : E → F) (m 0) :=
by { rw [iterated_fderiv_succ_apply_right, iterated_fderiv_zero_apply], refl }
/-- When a function is `C^n` in a set `s` of unique differentiability, it admits
`ftaylor_series_within 𝕜 f s` as a Taylor series up to order `n` in `s`. -/
theorem cont_diff_on_iff_ftaylor_series :
cont_diff 𝕜 n f ↔ has_ftaylor_series_up_to n f (ftaylor_series 𝕜 f) :=
begin
split,
{ rw [← cont_diff_on_univ, ← has_ftaylor_series_up_to_on_univ_iff,
← ftaylor_series_within_univ],
exact λ h, cont_diff_on.ftaylor_series_within h unique_diff_on_univ },
{ assume h, exact ⟨ftaylor_series 𝕜 f, h⟩ }
end
lemma cont_diff_iff_continuous_differentiable :
cont_diff 𝕜 n f ↔
(∀ (m : ℕ), (m : with_top ℕ) ≤ n → continuous (λ x, iterated_fderiv 𝕜 m f x))
∧ (∀ (m : ℕ), (m : with_top ℕ) < n → differentiable 𝕜 (λ x, iterated_fderiv 𝕜 m f x)) :=
by simp [cont_diff_on_univ.symm, continuous_iff_continuous_on_univ,
differentiable_on_univ.symm, iterated_fderiv_within_univ,
cont_diff_on_iff_continuous_on_differentiable_on unique_diff_on_univ]
/-- If `f` is `C^n` then its `m`-times iterated derivative is continuous for `m ≤ n`. -/
lemma cont_diff.continuous_iterated_fderiv {m : ℕ} (hm : (m : with_top ℕ) ≤ n)
(hf : cont_diff 𝕜 n f) : continuous (λ x, iterated_fderiv 𝕜 m f x) :=
(cont_diff_iff_continuous_differentiable.mp hf).1 m hm
/-- If `f` is `C^n` then its `m`-times iterated derivative is differentiable for `m < n`. -/
lemma cont_diff.differentiable_iterated_fderiv {m : ℕ} (hm : (m : with_top ℕ) < n)
(hf : cont_diff 𝕜 n f) : differentiable 𝕜 (λ x, iterated_fderiv 𝕜 m f x) :=
(cont_diff_iff_continuous_differentiable.mp hf).2 m hm
lemma cont_diff_of_differentiable_iterated_fderiv
(h : ∀(m : ℕ), (m : with_top ℕ) ≤ n → differentiable 𝕜 (iterated_fderiv 𝕜 m f)) :
cont_diff 𝕜 n f :=
cont_diff_iff_continuous_differentiable.2
⟨λ m hm, (h m hm).continuous, λ m hm, (h m (le_of_lt hm))⟩
/-- A function is `C^(n + 1)` if and only if it is differentiable,
and its derivative (formulated in terms of `fderiv`) is `C^n`. -/
theorem cont_diff_succ_iff_fderiv {n : ℕ} :
cont_diff 𝕜 ((n + 1) : ℕ) f ↔
differentiable 𝕜 f ∧ cont_diff 𝕜 n (λ y, fderiv 𝕜 f y) :=
by simp only [← cont_diff_on_univ, ← differentiable_on_univ, ← fderiv_within_univ,
cont_diff_on_succ_iff_fderiv_within unique_diff_on_univ]
theorem cont_diff_one_iff_fderiv :
cont_diff 𝕜 1 f ↔ differentiable 𝕜 f ∧ continuous (fderiv 𝕜 f) :=
cont_diff_succ_iff_fderiv.trans $ iff.rfl.and cont_diff_zero
/-- A function is `C^∞` if and only if it is differentiable,
and its derivative (formulated in terms of `fderiv`) is `C^∞`. -/
theorem cont_diff_top_iff_fderiv :
cont_diff 𝕜 ∞ f ↔
differentiable 𝕜 f ∧ cont_diff 𝕜 ∞ (λ y, fderiv 𝕜 f y) :=
begin
simp [cont_diff_on_univ.symm, differentiable_on_univ.symm, fderiv_within_univ.symm,
- fderiv_within_univ],
rw cont_diff_on_top_iff_fderiv_within unique_diff_on_univ,
end
lemma cont_diff.continuous_fderiv
(h : cont_diff 𝕜 n f) (hn : 1 ≤ n) :
continuous (λ x, fderiv 𝕜 f x) :=
((cont_diff_succ_iff_fderiv).1 (h.of_le hn)).2.continuous
/-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is
continuous. -/
lemma cont_diff.continuous_fderiv_apply
(h : cont_diff 𝕜 n f) (hn : 1 ≤ n) :
continuous (λp : E × E, (fderiv 𝕜 f p.1 : E → F) p.2) :=
begin
have A : continuous (λq : (E →L[𝕜] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous,
have B : continuous (λp : E × E, (fderiv 𝕜 f p.1, p.2)),
{ apply continuous.prod_mk _ continuous_snd,
exact continuous.comp (h.continuous_fderiv hn) continuous_fst },
exact A.comp B
end
/-! ### Constants -/
@[simp] lemma iterated_fderiv_zero_fun {n : ℕ} :
iterated_fderiv 𝕜 n (λ x : E, (0 : F)) = 0 :=
begin
induction n with n IH,
{ ext m, simp },
{ ext x m,
rw [iterated_fderiv_succ_apply_left, IH],
change (fderiv 𝕜 (λ (x : E), (0 : (E [×n]→L[𝕜] F))) x : E → (E [×n]→L[𝕜] F)) (m 0) (tail m) = _,
rw fderiv_const,
refl }
end
lemma cont_diff_zero_fun :
cont_diff 𝕜 n (λ x : E, (0 : F)) :=
begin
apply cont_diff_of_differentiable_iterated_fderiv (λm hm, _),
rw iterated_fderiv_zero_fun,
apply differentiable_const (0 : (E [×m]→L[𝕜] F))
end
/--
Constants are `C^∞`.
-/
lemma cont_diff_const {c : F} : cont_diff 𝕜 n (λx : E, c) :=
begin
suffices h : cont_diff 𝕜 ∞ (λx : E, c), by exact h.of_le le_top,
rw cont_diff_top_iff_fderiv,
refine ⟨differentiable_const c, _⟩,
rw fderiv_const,
exact cont_diff_zero_fun
end
lemma cont_diff_on_const {c : F} {s : set E} :
cont_diff_on 𝕜 n (λx : E, c) s :=
cont_diff_const.cont_diff_on
lemma cont_diff_at_const {c : F} :
cont_diff_at 𝕜 n (λx : E, c) x :=
cont_diff_const.cont_diff_at
lemma cont_diff_within_at_const {c : F} :
cont_diff_within_at 𝕜 n (λx : E, c) s x :=
cont_diff_at_const.cont_diff_within_at
@[nontriviality] lemma cont_diff_of_subsingleton [subsingleton F] :
cont_diff 𝕜 n f :=
by { rw [subsingleton.elim f (λ _, 0)], exact cont_diff_const }
@[nontriviality] lemma cont_diff_at_of_subsingleton [subsingleton F] :
cont_diff_at 𝕜 n f x :=
by { rw [subsingleton.elim f (λ _, 0)], exact cont_diff_at_const }
@[nontriviality] lemma cont_diff_within_at_of_subsingleton [subsingleton F] :
cont_diff_within_at 𝕜 n f s x :=
by { rw [subsingleton.elim f (λ _, 0)], exact cont_diff_within_at_const }
@[nontriviality] lemma cont_diff_on_of_subsingleton [subsingleton F] :
cont_diff_on 𝕜 n f s :=
by { rw [subsingleton.elim f (λ _, 0)], exact cont_diff_on_const }
/-! ### Smoothness of linear functions -/
/--
Unbundled bounded linear functions are `C^∞`.
-/
lemma is_bounded_linear_map.cont_diff (hf : is_bounded_linear_map 𝕜 f) :
cont_diff 𝕜 n f :=
begin
suffices h : cont_diff 𝕜 ∞ f, by exact h.of_le le_top,
rw cont_diff_top_iff_fderiv,
refine ⟨hf.differentiable, _⟩,
simp_rw [hf.fderiv],
exact cont_diff_const
end
lemma continuous_linear_map.cont_diff (f : E →L[𝕜] F) : cont_diff 𝕜 n f :=
f.is_bounded_linear_map.cont_diff
lemma continuous_linear_equiv.cont_diff (f : E ≃L[𝕜] F) : cont_diff 𝕜 n f :=
(f : E →L[𝕜] F).cont_diff
lemma linear_isometry.cont_diff (f : E →ₗᵢ[𝕜] F) : cont_diff 𝕜 n f :=
f.to_continuous_linear_map.cont_diff
lemma linear_isometry_equiv.cont_diff (f : E ≃ₗᵢ[𝕜] F) : cont_diff 𝕜 n f :=
(f : E →L[𝕜] F).cont_diff
/--
The identity is `C^∞`.
-/
lemma cont_diff_id : cont_diff 𝕜 n (id : E → E) :=
is_bounded_linear_map.id.cont_diff
lemma cont_diff_within_at_id {s x} : cont_diff_within_at 𝕜 n (id : E → E) s x :=
cont_diff_id.cont_diff_within_at
lemma cont_diff_at_id {x} : cont_diff_at 𝕜 n (id : E → E) x :=
cont_diff_id.cont_diff_at
lemma cont_diff_on_id {s} : cont_diff_on 𝕜 n (id : E → E) s :=
cont_diff_id.cont_diff_on
/--
Bilinear functions are `C^∞`.
-/
lemma is_bounded_bilinear_map.cont_diff (hb : is_bounded_bilinear_map 𝕜 b) :
cont_diff 𝕜 n b :=
begin
suffices h : cont_diff 𝕜 ∞ b, by exact h.of_le le_top,
rw cont_diff_top_iff_fderiv,
refine ⟨hb.differentiable, _⟩,
simp [hb.fderiv],
exact hb.is_bounded_linear_map_deriv.cont_diff
end
/-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `g ∘ f` admits a Taylor
series whose `k`-th term is given by `g ∘ (p k)`. -/
lemma has_ftaylor_series_up_to_on.continuous_linear_map_comp (g : F →L[𝕜] G)
(hf : has_ftaylor_series_up_to_on n f p s) :
has_ftaylor_series_up_to_on n (g ∘ f) (λ x k, g.comp_continuous_multilinear_map (p x k)) s :=
begin
set L : Π m : ℕ, (E [×m]→L[𝕜] F) →L[𝕜] (E [×m]→L[𝕜] G) :=
λ m, continuous_linear_map.comp_continuous_multilinear_mapL 𝕜 (λ _, E) F G g,
split,
{ exact λ x hx, congr_arg g (hf.zero_eq x hx) },
{ intros m hm x hx,
convert (L m).has_fderiv_at.comp_has_fderiv_within_at x (hf.fderiv_within m hm x hx) },
{ intros m hm,
convert (L m).continuous.comp_continuous_on (hf.cont m hm) }
end
/-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain
at a point. -/
lemma cont_diff_within_at.continuous_linear_map_comp (g : F →L[𝕜] G)
(hf : cont_diff_within_at 𝕜 n f s x) :
cont_diff_within_at 𝕜 n (g ∘ f) s x :=
begin
assume m hm,
rcases hf m hm with ⟨u, hu, p, hp⟩,
exact ⟨u, hu, _, hp.continuous_linear_map_comp g⟩,
end
/-- Composition by continuous linear maps on the left preserves `C^n` functions in a domain
at a point. -/
lemma cont_diff_at.continuous_linear_map_comp (g : F →L[𝕜] G) (hf : cont_diff_at 𝕜 n f x) :
cont_diff_at 𝕜 n (g ∘ f) x :=
cont_diff_within_at.continuous_linear_map_comp g hf
/-- Composition by continuous linear maps on the left preserves `C^n` functions on domains. -/
lemma cont_diff_on.continuous_linear_map_comp (g : F →L[𝕜] G) (hf : cont_diff_on 𝕜 n f s) :
cont_diff_on 𝕜 n (g ∘ f) s :=
λ x hx, (hf x hx).continuous_linear_map_comp g
/-- Composition by continuous linear maps on the left preserves `C^n` functions. -/
lemma cont_diff.continuous_linear_map_comp {f : E → F} (g : F →L[𝕜] G)
(hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λx, g (f x)) :=
cont_diff_on_univ.1 $ cont_diff_on.continuous_linear_map_comp
_ (cont_diff_on_univ.2 hf)
/-- Composition by continuous linear equivs on the left respects higher differentiability at a
point in a domain. -/
lemma continuous_linear_equiv.comp_cont_diff_within_at_iff
(e : F ≃L[𝕜] G) :
cont_diff_within_at 𝕜 n (e ∘ f) s x ↔ cont_diff_within_at 𝕜 n f s x :=
⟨λ H, by simpa only [(∘), e.symm.coe_coe, e.symm_apply_apply]
using H.continuous_linear_map_comp (e.symm : G →L[𝕜] F),
λ H, H.continuous_linear_map_comp (e : F →L[𝕜] G)⟩
/-- Composition by continuous linear equivs on the left respects higher differentiability at a
point. -/
lemma continuous_linear_equiv.comp_cont_diff_at_iff (e : F ≃L[𝕜] G) :
cont_diff_at 𝕜 n (e ∘ f) x ↔ cont_diff_at 𝕜 n f x :=
by simp only [← cont_diff_within_at_univ, e.comp_cont_diff_within_at_iff]
/-- Composition by continuous linear equivs on the left respects higher differentiability on
domains. -/
lemma continuous_linear_equiv.comp_cont_diff_on_iff
(e : F ≃L[𝕜] G) :
cont_diff_on 𝕜 n (e ∘ f) s ↔ cont_diff_on 𝕜 n f s :=
by simp [cont_diff_on, e.comp_cont_diff_within_at_iff]
/-- Composition by continuous linear equivs on the left respects higher differentiability. -/
lemma continuous_linear_equiv.comp_cont_diff_iff
(e : F ≃L[𝕜] G) :
cont_diff 𝕜 n (e ∘ f) ↔ cont_diff 𝕜 n f :=
by simp only [← cont_diff_on_univ, e.comp_cont_diff_on_iff]
/-- If `f` admits a Taylor series `p` in a set `s`, and `g` is linear, then `f ∘ g` admits a Taylor
series in `g ⁻¹' s`, whose `k`-th term is given by `p k (g v₁, ..., g vₖ)` . -/
lemma has_ftaylor_series_up_to_on.comp_continuous_linear_map
(hf : has_ftaylor_series_up_to_on n f p s) (g : G →L[𝕜] E) :
has_ftaylor_series_up_to_on n (f ∘ g)
(λ x k, (p (g x) k).comp_continuous_linear_map (λ _, g)) (g ⁻¹' s) :=
begin
let A : Π m : ℕ, (E [×m]→L[𝕜] F) → (G [×m]→L[𝕜] F) :=
λ m h, h.comp_continuous_linear_map (λ _, g),
have hA : ∀ m, is_bounded_linear_map 𝕜 (A m) :=
λ m, is_bounded_linear_map_continuous_multilinear_map_comp_linear g,
split,
{ assume x hx,
simp only [(hf.zero_eq (g x) hx).symm, function.comp_app],
change p (g x) 0 (λ (i : fin 0), g 0) = p (g x) 0 0,
rw continuous_linear_map.map_zero,
refl },
{ assume m hm x hx,
convert ((hA m).has_fderiv_at).comp_has_fderiv_within_at x
((hf.fderiv_within m hm (g x) hx).comp x (g.has_fderiv_within_at) (subset.refl _)),
ext y v,
change p (g x) (nat.succ m) (g ∘ (cons y v)) = p (g x) m.succ (cons (g y) (g ∘ v)),
rw comp_cons },
{ assume m hm,
exact (hA m).continuous.comp_continuous_on
((hf.cont m hm).comp g.continuous.continuous_on (subset.refl _)) }
end
/-- Composition by continuous linear maps on the right preserves `C^n` functions at a point on
a domain. -/
lemma cont_diff_within_at.comp_continuous_linear_map {x : G}
(g : G →L[𝕜] E) (hf : cont_diff_within_at 𝕜 n f s (g x)) :
cont_diff_within_at 𝕜 n (f ∘ g) (g ⁻¹' s) x :=
begin
assume m hm,
rcases hf m hm with ⟨u, hu, p, hp⟩,
refine ⟨g ⁻¹' u, _, _, hp.comp_continuous_linear_map g⟩,
apply continuous_within_at.preimage_mem_nhds_within',
{ exact g.continuous.continuous_within_at },
{ apply nhds_within_mono (g x) _ hu,
rw image_insert_eq,
exact insert_subset_insert (image_preimage_subset g s) }
end
/-- Composition by continuous linear maps on the right preserves `C^n` functions on domains. -/
lemma cont_diff_on.comp_continuous_linear_map
(hf : cont_diff_on 𝕜 n f s) (g : G →L[𝕜] E) :
cont_diff_on 𝕜 n (f ∘ g) (g ⁻¹' s) :=
λ x hx, (hf (g x) hx).comp_continuous_linear_map g
/-- Composition by continuous linear maps on the right preserves `C^n` functions. -/
lemma cont_diff.comp_continuous_linear_map {f : E → F} {g : G →L[𝕜] E}
(hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (f ∘ g) :=
cont_diff_on_univ.1 $
cont_diff_on.comp_continuous_linear_map (cont_diff_on_univ.2 hf) _
/-- Composition by continuous linear equivs on the right respects higher differentiability at a
point in a domain. -/
lemma continuous_linear_equiv.cont_diff_within_at_comp_iff (e : G ≃L[𝕜] E) :
cont_diff_within_at 𝕜 n (f ∘ e) (e ⁻¹' s) (e.symm x) ↔
cont_diff_within_at 𝕜 n f s x :=
begin
split,
{ assume H,
simpa [← preimage_comp, (∘)] using H.comp_continuous_linear_map (e.symm : E →L[𝕜] G) },
{ assume H,
rw [← e.apply_symm_apply x, ← e.coe_coe] at H,
exact H.comp_continuous_linear_map _ },
end
/-- Composition by continuous linear equivs on the right respects higher differentiability at a
point. -/
lemma continuous_linear_equiv.cont_diff_at_comp_iff (e : G ≃L[𝕜] E) :
cont_diff_at 𝕜 n (f ∘ e) (e.symm x) ↔ cont_diff_at 𝕜 n f x :=
begin
rw [← cont_diff_within_at_univ, ← cont_diff_within_at_univ, ← preimage_univ],
exact e.cont_diff_within_at_comp_iff
end
/-- Composition by continuous linear equivs on the right respects higher differentiability on
domains. -/
lemma continuous_linear_equiv.cont_diff_on_comp_iff (e : G ≃L[𝕜] E) :
cont_diff_on 𝕜 n (f ∘ e) (e ⁻¹' s) ↔ cont_diff_on 𝕜 n f s :=
begin
refine ⟨λ H, _, λ H, H.comp_continuous_linear_map (e : G →L[𝕜] E)⟩,
have A : f = (f ∘ e) ∘ e.symm,
by { ext y, simp only [function.comp_app], rw e.apply_symm_apply y },
have B : e.symm ⁻¹' (e ⁻¹' s) = s,
by { rw [← preimage_comp, e.self_comp_symm], refl },
rw [A, ← B],
exact H.comp_continuous_linear_map (e.symm : E →L[𝕜] G)
end
/-- Composition by continuous linear equivs on the right respects higher differentiability. -/
lemma continuous_linear_equiv.cont_diff_comp_iff (e : G ≃L[𝕜] E) :
cont_diff 𝕜 n (f ∘ e) ↔ cont_diff 𝕜 n f :=
begin
rw [← cont_diff_on_univ, ← cont_diff_on_univ, ← preimage_univ],
exact e.cont_diff_on_comp_iff
end
/-- If two functions `f` and `g` admit Taylor series `p` and `q` in a set `s`, then the cartesian
product of `f` and `g` admits the cartesian product of `p` and `q` as a Taylor series. -/
lemma has_ftaylor_series_up_to_on.prod (hf : has_ftaylor_series_up_to_on n f p s)
{g : E → G} {q : E → formal_multilinear_series 𝕜 E G} (hg : has_ftaylor_series_up_to_on n g q s) :
has_ftaylor_series_up_to_on n (λ y, (f y, g y)) (λ y k, (p y k).prod (q y k)) s :=
begin
set L := λ m, continuous_multilinear_map.prodL 𝕜 (λ i : fin m, E) F G,
split,
{ assume x hx, rw [← hf.zero_eq x hx, ← hg.zero_eq x hx], refl },
{ assume m hm x hx,
convert (L m).has_fderiv_at.comp_has_fderiv_within_at x
((hf.fderiv_within m hm x hx).prod (hg.fderiv_within m hm x hx)) },
{ assume m hm,
exact (L m).continuous.comp_continuous_on ((hf.cont m hm).prod (hg.cont m hm)) }
end
/-- The cartesian product of `C^n` functions at a point in a domain is `C^n`. -/
lemma cont_diff_within_at.prod {s : set E} {f : E → F} {g : E → G}
(hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) :
cont_diff_within_at 𝕜 n (λx:E, (f x, g x)) s x :=
begin
assume m hm,
rcases hf m hm with ⟨u, hu, p, hp⟩,
rcases hg m hm with ⟨v, hv, q, hq⟩,
exact ⟨u ∩ v, filter.inter_mem hu hv, _,
(hp.mono (inter_subset_left u v)).prod (hq.mono (inter_subset_right u v))⟩
end
/-- The cartesian product of `C^n` functions on domains is `C^n`. -/
lemma cont_diff_on.prod {s : set E} {f : E → F} {g : E → G}
(hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) :
cont_diff_on 𝕜 n (λ x : E, (f x, g x)) s :=
λ x hx, (hf x hx).prod (hg x hx)
/-- The cartesian product of `C^n` functions at a point is `C^n`. -/
lemma cont_diff_at.prod {f : E → F} {g : E → G}
(hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) :
cont_diff_at 𝕜 n (λ x : E, (f x, g x)) x :=
cont_diff_within_at_univ.1 $ cont_diff_within_at.prod
(cont_diff_within_at_univ.2 hf)
(cont_diff_within_at_univ.2 hg)
/-- The cartesian product of `C^n` functions is `C^n`.-/
lemma cont_diff.prod {f : E → F} {g : E → G} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) :
cont_diff 𝕜 n (λ x : E, (f x, g x)) :=
cont_diff_on_univ.1 $ cont_diff_on.prod (cont_diff_on_univ.2 hf)
(cont_diff_on_univ.2 hg)
/-!
### Composition of `C^n` functions
We show that the composition of `C^n` functions is `C^n`. One way to prove it would be to write
the `n`-th derivative of the composition (this is Faà di Bruno's formula) and check its continuity,
but this is very painful. Instead, we go for a simple inductive proof. Assume it is done for `n`.
Then, to check it for `n+1`, one needs to check that the derivative of `g ∘ f` is `C^n`, i.e.,
that `Dg(f x) ⬝ Df(x)` is `C^n`. The term `Dg (f x)` is the composition of two `C^n` functions, so
it is `C^n` by the inductive assumption. The term `Df(x)` is also `C^n`. Then, the matrix
multiplication is the application of a bilinear map (which is `C^∞`, and therefore `C^n`) to
`x ↦ (Dg(f x), Df x)`. As the composition of two `C^n` maps, it is again `C^n`, and we are done.
There is a subtlety in this argument: we apply the inductive assumption to functions on other Banach
spaces. In maths, one would say: prove by induction over `n` that, for all `C^n` maps between all
pairs of Banach spaces, their composition is `C^n`. In Lean, this is fine as long as the spaces
stay in the same universe. This is not the case in the above argument: if `E` lives in universe `u`
and `F` lives in universe `v`, then linear maps from `E` to `F` (to which the derivative of `f`
belongs) is in universe `max u v`. If one could quantify over finitely many universes, the above
proof would work fine, but this is not the case. One could still write the proof considering spaces
in any universe in `u, v, w, max u v, max v w, max u v w`, but it would be extremely tedious and
lead to a lot of duplication. Instead, we formulate the above proof when all spaces live in the same
universe (where everything is fine), and then we deduce the general result by lifting all our spaces
to a common universe. We use the trick that any space `H` is isomorphic through a continuous linear
equiv to `continuous_multilinear_map (λ (i : fin 0), E × F × G) H` to change the universe level,
and then argue that composing with such a linear equiv does not change the fact of being `C^n`,
which we have already proved previously.
-/
/-- Auxiliary lemma proving that the composition of `C^n` functions on domains is `C^n` when all
spaces live in the same universe. Use instead `cont_diff_on.comp` which removes the universe
assumption (but is deduced from this one). -/
private lemma cont_diff_on.comp_same_univ
{Eu : Type u} [normed_add_comm_group Eu] [normed_space 𝕜 Eu]
{Fu : Type u} [normed_add_comm_group Fu] [normed_space 𝕜 Fu]
{Gu : Type u} [normed_add_comm_group Gu] [normed_space 𝕜 Gu]
{s : set Eu} {t : set Fu} {g : Fu → Gu} {f : Eu → Fu}
(hg : cont_diff_on 𝕜 n g t) (hf : cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) :
cont_diff_on 𝕜 n (g ∘ f) s :=
begin
unfreezingI { induction n using with_top.nat_induction with n IH Itop generalizing Eu Fu Gu },
{ rw cont_diff_on_zero at hf hg ⊢,
exact continuous_on.comp hg hf st },
{ rw cont_diff_on_succ_iff_has_fderiv_within_at at hg ⊢,
assume x hx,
rcases (cont_diff_on_succ_iff_has_fderiv_within_at.1 hf) x hx
with ⟨u, hu, f', hf', f'_diff⟩,
rcases hg (f x) (st hx) with ⟨v, hv, g', hg', g'_diff⟩,
rw insert_eq_of_mem hx at hu ⊢,
have xu : x ∈ u := mem_of_mem_nhds_within hx hu,
let w := s ∩ (u ∩ f⁻¹' v),
have wv : w ⊆ f ⁻¹' v := λ y hy, hy.2.2,
have wu : w ⊆ u := λ y hy, hy.2.1,
have ws : w ⊆ s := λ y hy, hy.1,
refine ⟨w, _, λ y, (g' (f y)).comp (f' y), _, _⟩,
show w ∈ 𝓝[s] x,
{ apply filter.inter_mem self_mem_nhds_within,
apply filter.inter_mem hu,
apply continuous_within_at.preimage_mem_nhds_within',
{ rw ← continuous_within_at_inter' hu,
exact (hf' x xu).differentiable_within_at.continuous_within_at.mono
(inter_subset_right _ _) },
{ apply nhds_within_mono _ _ hv,
exact subset.trans (image_subset_iff.mpr st) (subset_insert (f x) t) } },
show ∀ y ∈ w,
has_fderiv_within_at (g ∘ f) ((g' (f y)).comp (f' y)) w y,
{ rintros y ⟨ys, yu, yv⟩,
exact (hg' (f y) yv).comp y ((hf' y yu).mono wu) wv },
show cont_diff_on 𝕜 n (λ y, (g' (f y)).comp (f' y)) w,
{ have A : cont_diff_on 𝕜 n (λ y, g' (f y)) w :=
IH g'_diff ((hf.of_le (with_top.coe_le_coe.2 (nat.le_succ n))).mono ws) wv,
have B : cont_diff_on 𝕜 n f' w := f'_diff.mono wu,
have C : cont_diff_on 𝕜 n (λ y, (g' (f y), f' y)) w := A.prod B,
have D : cont_diff_on 𝕜 n (λ p : (Fu →L[𝕜] Gu) × (Eu →L[𝕜] Fu), p.1.comp p.2) univ :=
is_bounded_bilinear_map_comp.cont_diff.cont_diff_on,
exact IH D C (subset_univ _) } },
{ rw cont_diff_on_top at hf hg ⊢,
exact λ n, Itop n (hg n) (hf n) st }
end
/-- The composition of `C^n` functions on domains is `C^n`. -/
lemma cont_diff_on.comp
{s : set E} {t : set F} {g : F → G} {f : E → F}
(hg : cont_diff_on 𝕜 n g t) (hf : cont_diff_on 𝕜 n f s) (st : s ⊆ f ⁻¹' t) :
cont_diff_on 𝕜 n (g ∘ f) s :=
begin
/- we lift all the spaces to a common universe, as we have already proved the result in this
situation. For the lift, we use the trick that `H` is isomorphic through a
continuous linear equiv to `continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) H`, and
continuous linear equivs respect smoothness classes. -/
let Eu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) E,
letI : normed_add_comm_group Eu := by apply_instance,
letI : normed_space 𝕜 Eu := by apply_instance,
let Fu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) F,
letI : normed_add_comm_group Fu := by apply_instance,
letI : normed_space 𝕜 Fu := by apply_instance,
let Gu := continuous_multilinear_map 𝕜 (λ (i : fin 0), (E × F × G)) G,
letI : normed_add_comm_group Gu := by apply_instance,
letI : normed_space 𝕜 Gu := by apply_instance,
-- declare the isomorphisms
let isoE : Eu ≃L[𝕜] E := continuous_multilinear_curry_fin0 𝕜 (E × F × G) E,
let isoF : Fu ≃L[𝕜] F := continuous_multilinear_curry_fin0 𝕜 (E × F × G) F,
let isoG : Gu ≃L[𝕜] G := continuous_multilinear_curry_fin0 𝕜 (E × F × G) G,
-- lift the functions to the new spaces, check smoothness there, and then go back.
let fu : Eu → Fu := (isoF.symm ∘ f) ∘ isoE,
have fu_diff : cont_diff_on 𝕜 n fu (isoE ⁻¹' s),
by rwa [isoE.cont_diff_on_comp_iff, isoF.symm.comp_cont_diff_on_iff],
let gu : Fu → Gu := (isoG.symm ∘ g) ∘ isoF,
have gu_diff : cont_diff_on 𝕜 n gu (isoF ⁻¹' t),
by rwa [isoF.cont_diff_on_comp_iff, isoG.symm.comp_cont_diff_on_iff],
have main : cont_diff_on 𝕜 n (gu ∘ fu) (isoE ⁻¹' s),
{ apply cont_diff_on.comp_same_univ gu_diff fu_diff,
assume y hy,
simp only [fu, continuous_linear_equiv.coe_apply, function.comp_app, mem_preimage],
rw isoF.apply_symm_apply (f (isoE y)),
exact st hy },
have : gu ∘ fu = (isoG.symm ∘ (g ∘ f)) ∘ isoE,
{ ext y,
simp only [function.comp_apply, gu, fu],
rw isoF.apply_symm_apply (f (isoE y)) },
rwa [this, isoE.cont_diff_on_comp_iff, isoG.symm.comp_cont_diff_on_iff] at main
end
/-- The composition of `C^n` functions on domains is `C^n`. -/
lemma cont_diff_on.comp'
{s : set E} {t : set F} {g : F → G} {f : E → F}
(hg : cont_diff_on 𝕜 n g t) (hf : cont_diff_on 𝕜 n f s) :
cont_diff_on 𝕜 n (g ∘ f) (s ∩ f⁻¹' t) :=
hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _)
/-- The composition of a `C^n` function on a domain with a `C^n` function is `C^n`. -/
lemma cont_diff.comp_cont_diff_on {s : set E} {g : F → G} {f : E → F}
(hg : cont_diff 𝕜 n g) (hf : cont_diff_on 𝕜 n f s) :
cont_diff_on 𝕜 n (g ∘ f) s :=
(cont_diff_on_univ.2 hg).comp hf subset_preimage_univ
/-- The composition of `C^n` functions is `C^n`. -/
lemma cont_diff.comp {g : F → G} {f : E → F}
(hg : cont_diff 𝕜 n g) (hf : cont_diff 𝕜 n f) :
cont_diff 𝕜 n (g ∘ f) :=
cont_diff_on_univ.1 $ cont_diff_on.comp (cont_diff_on_univ.2 hg)
(cont_diff_on_univ.2 hf) (subset_univ _)
/-- The composition of `C^n` functions at points in domains is `C^n`. -/
lemma cont_diff_within_at.comp
{s : set E} {t : set F} {g : F → G} {f : E → F} (x : E)
(hg : cont_diff_within_at 𝕜 n g t (f x))
(hf : cont_diff_within_at 𝕜 n f s x) (st : s ⊆ f ⁻¹' t) :
cont_diff_within_at 𝕜 n (g ∘ f) s x :=
begin
assume m hm,
rcases hg.cont_diff_on hm with ⟨u, u_nhd, ut, hu⟩,
rcases hf.cont_diff_on hm with ⟨v, v_nhd, vs, hv⟩,
have xmem : x ∈ f ⁻¹' u ∩ v :=
⟨(mem_of_mem_nhds_within (mem_insert (f x) _) u_nhd : _),
mem_of_mem_nhds_within (mem_insert x s) v_nhd⟩,
have : f ⁻¹' u ∈ 𝓝[insert x s] x,
{ apply hf.continuous_within_at.insert_self.preimage_mem_nhds_within',
apply nhds_within_mono _ _ u_nhd,
rw image_insert_eq,
exact insert_subset_insert (image_subset_iff.mpr st) },
have Z := ((hu.comp (hv.mono (inter_subset_right (f ⁻¹' u) v)) (inter_subset_left _ _))
.cont_diff_within_at) xmem m le_rfl,
have : 𝓝[f ⁻¹' u ∩ v] x = 𝓝[insert x s] x,
{ have A : f ⁻¹' u ∩ v = (insert x s) ∩ (f ⁻¹' u ∩ v),
{ apply subset.antisymm _ (inter_subset_right _ _),
rintros y ⟨hy1, hy2⟩,
simp [hy1, hy2, vs hy2] },
rw [A, ← nhds_within_restrict''],
exact filter.inter_mem this v_nhd },
rwa [insert_eq_of_mem xmem, this] at Z,
end
/-- The composition of `C^n` functions at points in domains is `C^n`. -/
lemma cont_diff_within_at.comp' {s : set E} {t : set F} {g : F → G}
{f : E → F} (x : E)
(hg : cont_diff_within_at 𝕜 n g t (f x)) (hf : cont_diff_within_at 𝕜 n f s x) :
cont_diff_within_at 𝕜 n (g ∘ f) (s ∩ f⁻¹' t) x :=
hg.comp x (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _)
lemma cont_diff_at.comp_cont_diff_within_at {n} (x : E)
(hg : cont_diff_at 𝕜 n g (f x)) (hf : cont_diff_within_at 𝕜 n f s x) :
cont_diff_within_at 𝕜 n (g ∘ f) s x :=
hg.comp x hf (maps_to_univ _ _)
/-- The composition of `C^n` functions at points is `C^n`. -/
lemma cont_diff_at.comp (x : E)
(hg : cont_diff_at 𝕜 n g (f x))
(hf : cont_diff_at 𝕜 n f x) :
cont_diff_at 𝕜 n (g ∘ f) x :=
hg.comp x hf subset_preimage_univ
lemma cont_diff.comp_cont_diff_within_at
{g : F → G} {f : E → F} (h : cont_diff 𝕜 n g)
(hf : cont_diff_within_at 𝕜 n f t x) :
cont_diff_within_at 𝕜 n (g ∘ f) t x :=
begin
have : cont_diff_within_at 𝕜 n g univ (f x) :=
h.cont_diff_at.cont_diff_within_at,
exact this.comp x hf (subset_univ _),
end
lemma cont_diff.comp_cont_diff_at {g : F → G} {f : E → F} (x : E)
(hg : cont_diff 𝕜 n g) (hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (g ∘ f) x :=
hg.comp_cont_diff_within_at hf
/-!
### Smoothness of projections
-/
/-- The first projection in a product is `C^∞`. -/
lemma cont_diff_fst : cont_diff 𝕜 n (prod.fst : E × F → E) :=
is_bounded_linear_map.cont_diff is_bounded_linear_map.fst
/-- Postcomposing `f` with `prod.fst` is `C^n` -/
lemma cont_diff.fst {f : E → F × G} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ x, (f x).1) :=
cont_diff_fst.comp hf
/-- Precomposing `f` with `prod.fst` is `C^n` -/
lemma cont_diff.fst' {f : E → G} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ x : E × F, f x.1) :=
hf.comp cont_diff_fst
/-- The first projection on a domain in a product is `C^∞`. -/
lemma cont_diff_on_fst {s : set (E × F)} : cont_diff_on 𝕜 n (prod.fst : E × F → E) s :=
cont_diff.cont_diff_on cont_diff_fst
lemma cont_diff_on.fst {f : E → F × G} {s : set E} (hf : cont_diff_on 𝕜 n f s) :
cont_diff_on 𝕜 n (λ x, (f x).1) s :=
cont_diff_fst.comp_cont_diff_on hf
/-- The first projection at a point in a product is `C^∞`. -/
lemma cont_diff_at_fst {p : E × F} : cont_diff_at 𝕜 n (prod.fst : E × F → E) p :=
cont_diff_fst.cont_diff_at
/-- Postcomposing `f` with `prod.fst` is `C^n` at `(x, y)` -/
lemma cont_diff_at.fst {f : E → F × G} {x : E} (hf : cont_diff_at 𝕜 n f x) :
cont_diff_at 𝕜 n (λ x, (f x).1) x :=
cont_diff_at_fst.comp x hf
/-- Precomposing `f` with `prod.fst` is `C^n` at `(x, y)` -/
lemma cont_diff_at.fst' {f : E → G} {x : E} {y : F} (hf : cont_diff_at 𝕜 n f x) :
cont_diff_at 𝕜 n (λ x : E × F, f x.1) (x, y) :=
cont_diff_at.comp (x, y) hf cont_diff_at_fst
/-- Precomposing `f` with `prod.fst` is `C^n` at `x : E × F` -/
lemma cont_diff_at.fst'' {f : E → G} {x : E × F} (hf : cont_diff_at 𝕜 n f x.1) :
cont_diff_at 𝕜 n (λ x : E × F, f x.1) x :=
hf.comp x cont_diff_at_fst
/-- The first projection within a domain at a point in a product is `C^∞`. -/
lemma cont_diff_within_at_fst {s : set (E × F)} {p : E × F} :
cont_diff_within_at 𝕜 n (prod.fst : E × F → E) s p :=
cont_diff_fst.cont_diff_within_at
/-- The second projection in a product is `C^∞`. -/
lemma cont_diff_snd : cont_diff 𝕜 n (prod.snd : E × F → F) :=
is_bounded_linear_map.cont_diff is_bounded_linear_map.snd
/-- Postcomposing `f` with `prod.snd` is `C^n` -/
lemma cont_diff.snd {f : E → F × G} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ x, (f x).2) :=
cont_diff_snd.comp hf
/-- Precomposing `f` with `prod.snd` is `C^n` -/
lemma cont_diff.snd' {f : F → G} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ x : E × F, f x.2) :=
hf.comp cont_diff_snd
/-- The second projection on a domain in a product is `C^∞`. -/
lemma cont_diff_on_snd {s : set (E × F)} : cont_diff_on 𝕜 n (prod.snd : E × F → F) s :=
cont_diff.cont_diff_on cont_diff_snd
lemma cont_diff_on.snd {f : E → F × G} {s : set E} (hf : cont_diff_on 𝕜 n f s) :
cont_diff_on 𝕜 n (λ x, (f x).2) s :=
cont_diff_snd.comp_cont_diff_on hf
/-- The second projection at a point in a product is `C^∞`. -/
lemma cont_diff_at_snd {p : E × F} : cont_diff_at 𝕜 n (prod.snd : E × F → F) p :=
cont_diff_snd.cont_diff_at
/-- Postcomposing `f` with `prod.snd` is `C^n` at `x` -/
lemma cont_diff_at.snd {f : E → F × G} {x : E} (hf : cont_diff_at 𝕜 n f x) :
cont_diff_at 𝕜 n (λ x, (f x).2) x :=
cont_diff_at_snd.comp x hf
/-- Precomposing `f` with `prod.snd` is `C^n` at `(x, y)` -/
lemma cont_diff_at.snd' {f : F → G} {x : E} {y : F} (hf : cont_diff_at 𝕜 n f y) :
cont_diff_at 𝕜 n (λ x : E × F, f x.2) (x, y) :=
cont_diff_at.comp (x, y) hf cont_diff_at_snd
/-- Precomposing `f` with `prod.snd` is `C^n` at `x : E × F` -/
lemma cont_diff_at.snd'' {f : F → G} {x : E × F} (hf : cont_diff_at 𝕜 n f x.2) :
cont_diff_at 𝕜 n (λ x : E × F, f x.2) x :=
hf.comp x cont_diff_at_snd
/-- The second projection within a domain at a point in a product is `C^∞`. -/
lemma cont_diff_within_at_snd {s : set (E × F)} {p : E × F} :
cont_diff_within_at 𝕜 n (prod.snd : E × F → F) s p :=
cont_diff_snd.cont_diff_within_at
section n_ary
variables {E₁ E₂ E₃ E₄ : Type*}
variables [normed_add_comm_group E₁] [normed_add_comm_group E₂] [normed_add_comm_group E₃]
[normed_add_comm_group E₄] [normed_space 𝕜 E₁] [normed_space 𝕜 E₂] [normed_space 𝕜 E₃]
[normed_space 𝕜 E₄]
lemma cont_diff.comp₂ {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂}
(hg : cont_diff 𝕜 n g) (hf₁ : cont_diff 𝕜 n f₁) (hf₂ : cont_diff 𝕜 n f₂) :
cont_diff 𝕜 n (λ x, g (f₁ x, f₂ x)) :=
hg.comp $ hf₁.prod hf₂
lemma cont_diff.comp₃ {g : E₁ × E₂ × E₃ → G} {f₁ : F → E₁} {f₂ : F → E₂} {f₃ : F → E₃}
(hg : cont_diff 𝕜 n g) (hf₁ : cont_diff 𝕜 n f₁) (hf₂ : cont_diff 𝕜 n f₂)
(hf₃ : cont_diff 𝕜 n f₃) : cont_diff 𝕜 n (λ x, g (f₁ x, f₂ x, f₃ x)) :=
hg.comp₂ hf₁ $ hf₂.prod hf₃
lemma cont_diff.comp_cont_diff_on₂ {g : E₁ × E₂ → G} {f₁ : F → E₁} {f₂ : F → E₂} {s : set F}
(hg : cont_diff 𝕜 n g) (hf₁ : cont_diff_on 𝕜 n f₁ s) (hf₂ : cont_diff_on 𝕜 n f₂ s) :
cont_diff_on 𝕜 n (λ x, g (f₁ x, f₂ x)) s :=
hg.comp_cont_diff_on $ hf₁.prod hf₂
lemma cont_diff.comp_cont_diff_on₃ {g : E₁ × E₂ × E₃ → G} {f₁ : F → E₁} {f₂ : F → E₂} {f₃ : F → E₃}
{s : set F} (hg : cont_diff 𝕜 n g) (hf₁ : cont_diff_on 𝕜 n f₁ s) (hf₂ : cont_diff_on 𝕜 n f₂ s)
(hf₃ : cont_diff_on 𝕜 n f₃ s) : cont_diff_on 𝕜 n (λ x, g (f₁ x, f₂ x, f₃ x)) s :=
hg.comp_cont_diff_on₂ hf₁ $ hf₂.prod hf₃
end n_ary
/--
The natural equivalence `(E × F) × G ≃ E × (F × G)` is smooth.
Warning: if you think you need this lemma, it is likely that you can simplify your proof by
reformulating the lemma that you're applying next using the tips in
Note [continuity lemma statement]
-/
lemma cont_diff_prod_assoc : cont_diff 𝕜 ⊤ $ equiv.prod_assoc E F G :=
(linear_isometry_equiv.prod_assoc 𝕜 E F G).cont_diff
/--
The natural equivalence `E × (F × G) ≃ (E × F) × G` is smooth.
Warning: see remarks attached to `cont_diff_prod_assoc`
-/
lemma cont_diff_prod_assoc_symm : cont_diff 𝕜 ⊤ $ (equiv.prod_assoc E F G).symm :=
(linear_isometry_equiv.prod_assoc 𝕜 E F G).symm.cont_diff
/-! ### Bundled derivatives -/
/-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/
lemma cont_diff_on_fderiv_within_apply {m n : with_top ℕ} {s : set E}
{f : E → F} (hf : cont_diff_on 𝕜 n f s) (hs : unique_diff_on 𝕜 s) (hmn : m + 1 ≤ n) :
cont_diff_on 𝕜 m (λp : E × E, (fderiv_within 𝕜 f s p.1 : E →L[𝕜] F) p.2) (s ×ˢ univ) :=
begin
have A : cont_diff 𝕜 m (λp : (E →L[𝕜] F) × E, p.1 p.2),
{ apply is_bounded_bilinear_map.cont_diff,
exact is_bounded_bilinear_map_apply },
have B : cont_diff_on 𝕜 m
(λ (p : E × E), ((fderiv_within 𝕜 f s p.fst), p.snd)) (s ×ˢ univ),
{ apply cont_diff_on.prod _ _,
{ have I : cont_diff_on 𝕜 m (λ (x : E), fderiv_within 𝕜 f s x) s :=
hf.fderiv_within hs hmn,
have J : cont_diff_on 𝕜 m (λ (x : E × E), x.1) (s ×ˢ univ) :=
cont_diff_fst.cont_diff_on,
exact cont_diff_on.comp I J (prod_subset_preimage_fst _ _) },
{ apply cont_diff.cont_diff_on _ ,
apply is_bounded_linear_map.snd.cont_diff } },
exact A.comp_cont_diff_on B
end
/-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/
lemma cont_diff.cont_diff_fderiv_apply {f : E → F}
(hf : cont_diff 𝕜 n f) (hmn : m + 1 ≤ n) :
cont_diff 𝕜 m (λp : E × E, (fderiv 𝕜 f p.1 : E →L[𝕜] F) p.2) :=
begin
rw ← cont_diff_on_univ at ⊢ hf,
rw [← fderiv_within_univ, ← univ_prod_univ],
exact cont_diff_on_fderiv_within_apply hf unique_diff_on_univ hmn
end
/-!
### Smoothness of functions `f : E → Π i, F' i`
-/
section pi
variables {ι ι' : Type*} [fintype ι] [fintype ι'] {F' : ι → Type*}
[Π i, normed_add_comm_group (F' i)] [Π i, normed_space 𝕜 (F' i)] {φ : Π i, E → F' i}
{p' : Π i, E → formal_multilinear_series 𝕜 E (F' i)}
{Φ : E → Π i, F' i} {P' : E → formal_multilinear_series 𝕜 E (Π i, F' i)}
lemma has_ftaylor_series_up_to_on_pi :
has_ftaylor_series_up_to_on n (λ x i, φ i x)
(λ x m, continuous_multilinear_map.pi (λ i, p' i x m)) s ↔
∀ i, has_ftaylor_series_up_to_on n (φ i) (p' i) s :=
begin
set pr := @continuous_linear_map.proj 𝕜 _ ι F' _ _ _,
letI : Π (m : ℕ) (i : ι), normed_space 𝕜 (E [×m]→L[𝕜] (F' i)) := λ m i, infer_instance,
set L : Π m : ℕ, (Π i, E [×m]→L[𝕜] (F' i)) ≃ₗᵢ[𝕜] (E [×m]→L[𝕜] (Π i, F' i)) :=
λ m, continuous_multilinear_map.piₗᵢ _ _,
refine ⟨λ h i, _, λ h, ⟨λ x hx, _, _, _⟩⟩,
{ convert h.continuous_linear_map_comp (pr i),
ext, refl },
{ ext1 i,
exact (h i).zero_eq x hx },
{ intros m hm x hx,
have := has_fderiv_within_at_pi.2 (λ i, (h i).fderiv_within m hm x hx),
convert (L m).has_fderiv_at.comp_has_fderiv_within_at x this },
{ intros m hm,
have := continuous_on_pi.2 (λ i, (h i).cont m hm),
convert (L m).continuous.comp_continuous_on this }
end
@[simp] lemma has_ftaylor_series_up_to_on_pi' :
has_ftaylor_series_up_to_on n Φ P' s ↔
∀ i, has_ftaylor_series_up_to_on n (λ x, Φ x i)
(λ x m, (@continuous_linear_map.proj 𝕜 _ ι F' _ _ _ i).comp_continuous_multilinear_map
(P' x m)) s :=
by { convert has_ftaylor_series_up_to_on_pi, ext, refl }
lemma cont_diff_within_at_pi :
cont_diff_within_at 𝕜 n Φ s x ↔
∀ i, cont_diff_within_at 𝕜 n (λ x, Φ x i) s x :=
begin
set pr := @continuous_linear_map.proj 𝕜 _ ι F' _ _ _,
refine ⟨λ h i, h.continuous_linear_map_comp (pr i), λ h m hm, _⟩,
choose u hux p hp using λ i, h i m hm,
exact ⟨⋂ i, u i, filter.Inter_mem.2 hux, _,
has_ftaylor_series_up_to_on_pi.2 (λ i, (hp i).mono $ Inter_subset _ _)⟩,
end
lemma cont_diff_on_pi :
cont_diff_on 𝕜 n Φ s ↔ ∀ i, cont_diff_on 𝕜 n (λ x, Φ x i) s :=
⟨λ h i x hx, cont_diff_within_at_pi.1 (h x hx) _,
λ h x hx, cont_diff_within_at_pi.2 (λ i, h i x hx)⟩
lemma cont_diff_at_pi :
cont_diff_at 𝕜 n Φ x ↔ ∀ i, cont_diff_at 𝕜 n (λ x, Φ x i) x :=
cont_diff_within_at_pi
lemma cont_diff_pi :
cont_diff 𝕜 n Φ ↔ ∀ i, cont_diff 𝕜 n (λ x, Φ x i) :=
by simp only [← cont_diff_on_univ, cont_diff_on_pi]
variables (𝕜 E)
lemma cont_diff_apply (i : ι) : cont_diff 𝕜 n (λ (f : ι → E), f i) :=
cont_diff_pi.mp cont_diff_id i
lemma cont_diff_apply_apply (i : ι) (j : ι') : cont_diff 𝕜 n (λ (f : ι → ι' → E), f i j) :=
cont_diff_pi.mp (cont_diff_apply 𝕜 (ι' → E) i) j
variables {𝕜 E}
end pi
/-! ### Sum of two functions -/
section add
/- The sum is smooth. -/
lemma cont_diff_add : cont_diff 𝕜 n (λp : F × F, p.1 + p.2) :=
(is_bounded_linear_map.fst.add is_bounded_linear_map.snd).cont_diff
/-- The sum of two `C^n` functions within a set at a point is `C^n` within this set
at this point. -/
lemma cont_diff_within_at.add {s : set E} {f g : E → F}
(hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) :
cont_diff_within_at 𝕜 n (λx, f x + g x) s x :=
cont_diff_add.cont_diff_within_at.comp x (hf.prod hg) subset_preimage_univ
/-- The sum of two `C^n` functions at a point is `C^n` at this point. -/
lemma cont_diff_at.add {f g : E → F} (hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) :
cont_diff_at 𝕜 n (λx, f x + g x) x :=
by rw [← cont_diff_within_at_univ] at *; exact hf.add hg
/-- The sum of two `C^n`functions is `C^n`. -/
lemma cont_diff.add {f g : E → F} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) :
cont_diff 𝕜 n (λx, f x + g x) :=
cont_diff_add.comp (hf.prod hg)
/-- The sum of two `C^n` functions on a domain is `C^n`. -/
lemma cont_diff_on.add {s : set E} {f g : E → F}
(hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) :
cont_diff_on 𝕜 n (λx, f x + g x) s :=
λ x hx, (hf x hx).add (hg x hx)
variables {i : ℕ}
lemma iterated_fderiv_within_add_apply {f g : E → F}
(hf : cont_diff_on 𝕜 i f s) (hg : cont_diff_on 𝕜 i g s) (hu : unique_diff_on 𝕜 s)
(hx : x ∈ s) :
iterated_fderiv_within 𝕜 i (f + g) s x =
iterated_fderiv_within 𝕜 i f s x + iterated_fderiv_within 𝕜 i g s x :=
begin
induction i with i hi generalizing x,
{ ext h, simp },
{ ext h,
have hi' : (i : with_top ℕ) < i+1 :=
with_top.coe_lt_coe.mpr (nat.lt_succ_self _),
have hdf : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 i f s) s :=
hf.differentiable_on_iterated_fderiv_within hi' hu,
have hdg : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 i g s) s :=
hg.differentiable_on_iterated_fderiv_within hi' hu,
have hcdf : cont_diff_on 𝕜 i f s := hf.of_le hi'.le,
have hcdg : cont_diff_on 𝕜 i g s := hg.of_le hi'.le,
calc iterated_fderiv_within 𝕜 (i+1) (f + g) s x h
= fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i (f + g) s) s x (h 0) (fin.tail h) : rfl
... = fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i f s + iterated_fderiv_within 𝕜 i g s) s x
(h 0) (fin.tail h) :
begin
congr' 2,
exact fderiv_within_congr (hu x hx) (λ _, hi hcdf hcdg) (hi hcdf hcdg hx),
end
... = (fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i f s) s +
fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i g s) s)
x (h 0) (fin.tail h) :
by rw [pi.add_def, fderiv_within_add (hu x hx) (hdf x hx) (hdg x hx)]; refl
... = (iterated_fderiv_within 𝕜 (i+1) f s + iterated_fderiv_within 𝕜 (i+1) g s) x h : rfl }
end
lemma iterated_fderiv_add_apply {i : ℕ} {f g : E → F} (hf : cont_diff 𝕜 i f)
(hg : cont_diff 𝕜 i g) :
iterated_fderiv 𝕜 i (f + g) x = iterated_fderiv 𝕜 i f x + iterated_fderiv 𝕜 i g x :=
begin
simp_rw [←cont_diff_on_univ, ←iterated_fderiv_within_univ] at hf hg ⊢,
exact iterated_fderiv_within_add_apply hf hg unique_diff_on_univ (set.mem_univ _),
end
end add
/-! ### Negative -/
section neg
/- The negative is smooth. -/
lemma cont_diff_neg : cont_diff 𝕜 n (λp : F, -p) :=
is_bounded_linear_map.id.neg.cont_diff
/-- The negative of a `C^n` function within a domain at a point is `C^n` within this domain at
this point. -/
lemma cont_diff_within_at.neg {s : set E} {f : E → F}
(hf : cont_diff_within_at 𝕜 n f s x) : cont_diff_within_at 𝕜 n (λx, -f x) s x :=
cont_diff_neg.cont_diff_within_at.comp x hf subset_preimage_univ
/-- The negative of a `C^n` function at a point is `C^n` at this point. -/
lemma cont_diff_at.neg {f : E → F}
(hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (λx, -f x) x :=
by rw ← cont_diff_within_at_univ at *; exact hf.neg
/-- The negative of a `C^n`function is `C^n`. -/
lemma cont_diff.neg {f : E → F} (hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λx, -f x) :=
cont_diff_neg.comp hf
/-- The negative of a `C^n` function on a domain is `C^n`. -/
lemma cont_diff_on.neg {s : set E} {f : E → F}
(hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (λx, -f x) s :=
λ x hx, (hf x hx).neg
variables {i : ℕ}
lemma iterated_fderiv_within_neg_apply {f : E → F} (hu : unique_diff_on 𝕜 s) (hx : x ∈ s) :
iterated_fderiv_within 𝕜 i (-f) s x = -iterated_fderiv_within 𝕜 i f s x :=
begin
induction i with i hi generalizing x,
{ ext h, simp },
{ ext h,
have hi' : (i : with_top ℕ) < i+1 :=
with_top.coe_lt_coe.mpr (nat.lt_succ_self _),
calc iterated_fderiv_within 𝕜 (i+1) (-f) s x h
= fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i (-f) s) s x (h 0) (fin.tail h) : rfl
... = fderiv_within 𝕜 (-iterated_fderiv_within 𝕜 i f s) s x
(h 0) (fin.tail h) :
begin
congr' 2,
exact fderiv_within_congr (hu x hx) (λ _, hi) (hi hx),
end
... = -(fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i f s) s) x (h 0) (fin.tail h) :
by rw [pi.neg_def, fderiv_within_neg (hu x hx)]; refl
... = - (iterated_fderiv_within 𝕜 (i+1) f s) x h : rfl }
end
lemma iterated_fderiv_neg_apply {i : ℕ} {f : E → F} :
iterated_fderiv 𝕜 i (-f) x = -iterated_fderiv 𝕜 i f x :=
begin
simp_rw [←iterated_fderiv_within_univ],
exact iterated_fderiv_within_neg_apply unique_diff_on_univ (set.mem_univ _),
end
end neg
/-! ### Subtraction -/
/-- The difference of two `C^n` functions within a set at a point is `C^n` within this set
at this point. -/
lemma cont_diff_within_at.sub {s : set E} {f g : E → F}
(hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) :
cont_diff_within_at 𝕜 n (λx, f x - g x) s x :=
by simpa only [sub_eq_add_neg] using hf.add hg.neg
/-- The difference of two `C^n` functions at a point is `C^n` at this point. -/
lemma cont_diff_at.sub {f g : E → F}
(hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) :
cont_diff_at 𝕜 n (λx, f x - g x) x :=
by simpa only [sub_eq_add_neg] using hf.add hg.neg
/-- The difference of two `C^n` functions on a domain is `C^n`. -/
lemma cont_diff_on.sub {s : set E} {f g : E → F}
(hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) :
cont_diff_on 𝕜 n (λx, f x - g x) s :=
by simpa only [sub_eq_add_neg] using hf.add hg.neg
/-- The difference of two `C^n` functions is `C^n`. -/
lemma cont_diff.sub {f g : E → F}
(hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) : cont_diff 𝕜 n (λx, f x - g x) :=
by simpa only [sub_eq_add_neg] using hf.add hg.neg
/-! ### Sum of finitely many functions -/
lemma cont_diff_within_at.sum
{ι : Type*} {f : ι → E → F} {s : finset ι} {t : set E} {x : E}
(h : ∀ i ∈ s, cont_diff_within_at 𝕜 n (λ x, f i x) t x) :
cont_diff_within_at 𝕜 n (λ x, (∑ i in s, f i x)) t x :=
begin
classical,
induction s using finset.induction_on with i s is IH,
{ simp [cont_diff_within_at_const] },
{ simp only [is, finset.sum_insert, not_false_iff],
exact (h _ (finset.mem_insert_self i s)).add (IH (λ j hj, h _ (finset.mem_insert_of_mem hj))) }
end
lemma cont_diff_at.sum
{ι : Type*} {f : ι → E → F} {s : finset ι} {x : E}
(h : ∀ i ∈ s, cont_diff_at 𝕜 n (λ x, f i x) x) :
cont_diff_at 𝕜 n (λ x, (∑ i in s, f i x)) x :=
by rw [← cont_diff_within_at_univ] at *; exact cont_diff_within_at.sum h
lemma cont_diff_on.sum
{ι : Type*} {f : ι → E → F} {s : finset ι} {t : set E}
(h : ∀ i ∈ s, cont_diff_on 𝕜 n (λ x, f i x) t) :
cont_diff_on 𝕜 n (λ x, (∑ i in s, f i x)) t :=
λ x hx, cont_diff_within_at.sum (λ i hi, h i hi x hx)
lemma cont_diff.sum
{ι : Type*} {f : ι → E → F} {s : finset ι}
(h : ∀ i ∈ s, cont_diff 𝕜 n (λ x, f i x)) :
cont_diff 𝕜 n (λ x, (∑ i in s, f i x)) :=
by simp [← cont_diff_on_univ] at *; exact cont_diff_on.sum h
/-! ### Product of two functions -/
section mul_prod
variables {𝔸 𝔸' ι 𝕜' : Type*} [normed_ring 𝔸] [normed_algebra 𝕜 𝔸]
[normed_comm_ring 𝔸'] [normed_algebra 𝕜 𝔸'] [normed_field 𝕜'] [normed_algebra 𝕜 𝕜']
/- The product is smooth. -/
lemma cont_diff_mul : cont_diff 𝕜 n (λ p : 𝔸 × 𝔸, p.1 * p.2) :=
(continuous_linear_map.lmul 𝕜 𝔸).is_bounded_bilinear_map.cont_diff
/-- The product of two `C^n` functions within a set at a point is `C^n` within this set
at this point. -/
lemma cont_diff_within_at.mul {s : set E} {f g : E → 𝔸}
(hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) :
cont_diff_within_at 𝕜 n (λ x, f x * g x) s x :=
cont_diff_mul.comp_cont_diff_within_at (hf.prod hg)
/-- The product of two `C^n` functions at a point is `C^n` at this point. -/
lemma cont_diff_at.mul {f g : E → 𝔸} (hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) :
cont_diff_at 𝕜 n (λ x, f x * g x) x :=
hf.mul hg
/-- The product of two `C^n` functions on a domain is `C^n`. -/
lemma cont_diff_on.mul {f g : E → 𝔸} (hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) :
cont_diff_on 𝕜 n (λ x, f x * g x) s :=
λ x hx, (hf x hx).mul (hg x hx)
/-- The product of two `C^n`functions is `C^n`. -/
lemma cont_diff.mul {f g : E → 𝔸} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) :
cont_diff 𝕜 n (λ x, f x * g x) :=
cont_diff_mul.comp (hf.prod hg)
lemma cont_diff_within_at_prod' {t : finset ι} {f : ι → E → 𝔸'}
(h : ∀ i ∈ t, cont_diff_within_at 𝕜 n (f i) s x) :
cont_diff_within_at 𝕜 n (∏ i in t, f i) s x :=
finset.prod_induction f (λ f, cont_diff_within_at 𝕜 n f s x) (λ _ _, cont_diff_within_at.mul)
(@cont_diff_within_at_const _ _ _ _ _ _ _ _ _ _ _ 1) h
lemma cont_diff_within_at_prod {t : finset ι} {f : ι → E → 𝔸'}
(h : ∀ i ∈ t, cont_diff_within_at 𝕜 n (f i) s x) :
cont_diff_within_at 𝕜 n (λ y, ∏ i in t, f i y) s x :=
by simpa only [← finset.prod_apply] using cont_diff_within_at_prod' h
lemma cont_diff_at_prod' {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_at 𝕜 n (f i) x) :
cont_diff_at 𝕜 n (∏ i in t, f i) x :=
cont_diff_within_at_prod' h
lemma cont_diff_at_prod {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_at 𝕜 n (f i) x) :
cont_diff_at 𝕜 n (λ y, ∏ i in t, f i y) x :=
cont_diff_within_at_prod h
lemma cont_diff_on_prod' {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_on 𝕜 n (f i) s) :
cont_diff_on 𝕜 n (∏ i in t, f i) s :=
λ x hx, cont_diff_within_at_prod' (λ i hi, h i hi x hx)
lemma cont_diff_on_prod {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff_on 𝕜 n (f i) s) :
cont_diff_on 𝕜 n (λ y, ∏ i in t, f i y) s :=
λ x hx, cont_diff_within_at_prod (λ i hi, h i hi x hx)
lemma cont_diff_prod' {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff 𝕜 n (f i)) :
cont_diff 𝕜 n (∏ i in t, f i) :=
cont_diff_iff_cont_diff_at.mpr $ λ x, cont_diff_at_prod' $ λ i hi, (h i hi).cont_diff_at
lemma cont_diff_prod {t : finset ι} {f : ι → E → 𝔸'} (h : ∀ i ∈ t, cont_diff 𝕜 n (f i)) :
cont_diff 𝕜 n (λ y, ∏ i in t, f i y) :=
cont_diff_iff_cont_diff_at.mpr $ λ x, cont_diff_at_prod $ λ i hi, (h i hi).cont_diff_at
lemma cont_diff.pow {f : E → 𝔸} (hf : cont_diff 𝕜 n f) :
∀ m : ℕ, cont_diff 𝕜 n (λ x, (f x) ^ m)
| 0 := by simpa using cont_diff_const
| (m + 1) := by simpa [pow_succ] using hf.mul (cont_diff.pow m)
lemma cont_diff_within_at.pow {f : E → 𝔸} (hf : cont_diff_within_at 𝕜 n f s x) (m : ℕ) :
cont_diff_within_at 𝕜 n (λ y, f y ^ m) s x :=
(cont_diff_id.pow m).comp_cont_diff_within_at hf
lemma cont_diff_at.pow {f : E → 𝔸} (hf : cont_diff_at 𝕜 n f x) (m : ℕ) :
cont_diff_at 𝕜 n (λ y, f y ^ m) x :=
hf.pow m
lemma cont_diff_on.pow {f : E → 𝔸} (hf : cont_diff_on 𝕜 n f s) (m : ℕ) :
cont_diff_on 𝕜 n (λ y, f y ^ m) s :=
λ y hy, (hf y hy).pow m
lemma cont_diff_within_at.div_const {f : E → 𝕜'} {n} {c : 𝕜'}
(hf : cont_diff_within_at 𝕜 n f s x) :
cont_diff_within_at 𝕜 n (λ x, f x / c) s x :=
by simpa only [div_eq_mul_inv] using hf.mul cont_diff_within_at_const
lemma cont_diff_at.div_const {f : E → 𝕜'} {n} {c : 𝕜'} (hf : cont_diff_at 𝕜 n f x) :
cont_diff_at 𝕜 n (λ x, f x / c) x :=
hf.div_const
lemma cont_diff_on.div_const {f : E → 𝕜'} {n} {c : 𝕜'} (hf : cont_diff_on 𝕜 n f s) :
cont_diff_on 𝕜 n (λ x, f x / c) s :=
λ x hx, (hf x hx).div_const
lemma cont_diff.div_const {f : E → 𝕜'} {n} {c : 𝕜'} (hf : cont_diff 𝕜 n f) :
cont_diff 𝕜 n (λ x, f x / c) :=
by simpa only [div_eq_mul_inv] using hf.mul cont_diff_const
end mul_prod
/-! ### Scalar multiplication -/
section smul
/- The scalar multiplication is smooth. -/
lemma cont_diff_smul : cont_diff 𝕜 n (λ p : 𝕜 × F, p.1 • p.2) :=
is_bounded_bilinear_map_smul.cont_diff
/-- The scalar multiplication of two `C^n` functions within a set at a point is `C^n` within this
set at this point. -/
lemma cont_diff_within_at.smul {s : set E} {f : E → 𝕜} {g : E → F}
(hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x) :
cont_diff_within_at 𝕜 n (λ x, f x • g x) s x :=
cont_diff_smul.cont_diff_within_at.comp x (hf.prod hg) subset_preimage_univ
/-- The scalar multiplication of two `C^n` functions at a point is `C^n` at this point. -/
lemma cont_diff_at.smul {f : E → 𝕜} {g : E → F}
(hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x) :
cont_diff_at 𝕜 n (λ x, f x • g x) x :=
by rw [← cont_diff_within_at_univ] at *; exact hf.smul hg
/-- The scalar multiplication of two `C^n` functions is `C^n`. -/
lemma cont_diff.smul {f : E → 𝕜} {g : E → F} (hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) :
cont_diff 𝕜 n (λ x, f x • g x) :=
cont_diff_smul.comp (hf.prod hg)
/-- The scalar multiplication of two `C^n` functions on a domain is `C^n`. -/
lemma cont_diff_on.smul {s : set E} {f : E → 𝕜} {g : E → F}
(hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) :
cont_diff_on 𝕜 n (λ x, f x • g x) s :=
λ x hx, (hf x hx).smul (hg x hx)
end smul
/-! ### Constant scalar multiplication -/
section const_smul
variables {R : Type*} [semiring R] [module R F] [smul_comm_class 𝕜 R F]
variables [has_continuous_const_smul R F]
/- The scalar multiplication with a constant is smooth. -/
lemma cont_diff_const_smul (c : R) : cont_diff 𝕜 n (λ p : F, c • p) :=
(c • continuous_linear_map.id 𝕜 F).cont_diff
/-- The scalar multiplication of a constant and a `C^n` function within a set at a point is `C^n`
within this set at this point. -/
lemma cont_diff_within_at.const_smul {s : set E} {f : E → F} {x : E} (c : R)
(hf : cont_diff_within_at 𝕜 n f s x) : cont_diff_within_at 𝕜 n (λ y, c • f y) s x :=
(cont_diff_const_smul c).cont_diff_at.comp_cont_diff_within_at x hf
/-- The scalar multiplication of a constant and a `C^n` function at a point is `C^n` at this
point. -/
lemma cont_diff_at.const_smul {f : E → F} {x : E} (c : R)
(hf : cont_diff_at 𝕜 n f x) : cont_diff_at 𝕜 n (λ y, c • f y) x :=
by rw [←cont_diff_within_at_univ] at *; exact hf.const_smul c
/-- The scalar multiplication of a constant and a `C^n` function is `C^n`. -/
lemma cont_diff.const_smul {f : E → F} (c : R)
(hf : cont_diff 𝕜 n f) : cont_diff 𝕜 n (λ y, c • f y) :=
(cont_diff_const_smul c).comp hf
/-- The scalar multiplication of a constant and a `C^n` on a domain is `C^n`. -/
lemma cont_diff_on.const_smul {s : set E} {f : E → F} (c : R)
(hf : cont_diff_on 𝕜 n f s) : cont_diff_on 𝕜 n (λ y, c • f y) s :=
λ x hx, (hf x hx).const_smul c
variables {i : ℕ} {a : R}
lemma iterated_fderiv_within_const_smul_apply (hf : cont_diff_on 𝕜 i f s)
(hu : unique_diff_on 𝕜 s) (hx : x ∈ s) :
iterated_fderiv_within 𝕜 i (a • f) s x = a • (iterated_fderiv_within 𝕜 i f s x) :=
begin
induction i with i hi generalizing x,
{ ext, simp },
{ ext h,
have hi' : (i : with_top ℕ) < i+1 :=
with_top.coe_lt_coe.mpr (nat.lt_succ_self _),
have hdf : differentiable_on 𝕜 (iterated_fderiv_within 𝕜 i f s) s :=
hf.differentiable_on_iterated_fderiv_within hi' hu,
have hcdf : cont_diff_on 𝕜 i f s := hf.of_le hi'.le,
calc iterated_fderiv_within 𝕜 (i+1) (a • f) s x h
= fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i (a • f) s) s x (h 0) (fin.tail h) : rfl
... = fderiv_within 𝕜 (a • iterated_fderiv_within 𝕜 i f s) s x (h 0) (fin.tail h) :
begin
congr' 2,
exact fderiv_within_congr (hu x hx) (λ _, hi hcdf) (hi hcdf hx),
end
... = (a • fderiv_within 𝕜 (iterated_fderiv_within 𝕜 i f s)) s x (h 0) (fin.tail h) :
by rw [pi.smul_def, fderiv_within_const_smul (hu x hx) (hdf x hx)]; refl
... = a • iterated_fderiv_within 𝕜 (i+1) f s x h : rfl }
end
lemma iterated_fderiv_const_smul_apply {x : E} (hf : cont_diff 𝕜 i f) :
iterated_fderiv 𝕜 i (a • f) x = a • iterated_fderiv 𝕜 i f x :=
begin
simp_rw [←cont_diff_on_univ, ←iterated_fderiv_within_univ] at *,
refine iterated_fderiv_within_const_smul_apply hf unique_diff_on_univ (set.mem_univ _),
end
end const_smul
/-! ### Cartesian product of two functions -/
section prod_map
variables {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
variables {F' : Type*} [normed_add_comm_group F'] [normed_space 𝕜 F']
/-- The product map of two `C^n` functions within a set at a point is `C^n`
within the product set at the product point. -/
lemma cont_diff_within_at.prod_map'
{s : set E} {t : set E'} {f : E → F} {g : E' → F'} {p : E × E'}
(hf : cont_diff_within_at 𝕜 n f s p.1) (hg : cont_diff_within_at 𝕜 n g t p.2) :
cont_diff_within_at 𝕜 n (prod.map f g) (s ×ˢ t) p :=
(hf.comp p cont_diff_within_at_fst (prod_subset_preimage_fst _ _)).prod
(hg.comp p cont_diff_within_at_snd (prod_subset_preimage_snd _ _))
lemma cont_diff_within_at.prod_map
{s : set E} {t : set E'} {f : E → F} {g : E' → F'} {x : E} {y : E'}
(hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g t y) :
cont_diff_within_at 𝕜 n (prod.map f g) (s ×ˢ t) (x, y) :=
cont_diff_within_at.prod_map' hf hg
/-- The product map of two `C^n` functions on a set is `C^n` on the product set. -/
lemma cont_diff_on.prod_map {E' : Type*} [normed_add_comm_group E'] [normed_space 𝕜 E']
{F' : Type*} [normed_add_comm_group F'] [normed_space 𝕜 F']
{s : set E} {t : set E'} {f : E → F} {g : E' → F'}
(hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g t) :
cont_diff_on 𝕜 n (prod.map f g) (s ×ˢ t) :=
(hf.comp cont_diff_on_fst (prod_subset_preimage_fst _ _)).prod
(hg.comp (cont_diff_on_snd) (prod_subset_preimage_snd _ _))
/-- The product map of two `C^n` functions within a set at a point is `C^n`
within the product set at the product point. -/
lemma cont_diff_at.prod_map {f : E → F} {g : E' → F'} {x : E} {y : E'}
(hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g y) :
cont_diff_at 𝕜 n (prod.map f g) (x, y) :=
begin
rw cont_diff_at at *,
convert hf.prod_map hg,
simp only [univ_prod_univ]
end
/-- The product map of two `C^n` functions within a set at a point is `C^n`
within the product set at the product point. -/
lemma cont_diff_at.prod_map' {f : E → F} {g : E' → F'} {p : E × E'}
(hf : cont_diff_at 𝕜 n f p.1) (hg : cont_diff_at 𝕜 n g p.2) :
cont_diff_at 𝕜 n (prod.map f g) p :=
begin
rcases p,
exact cont_diff_at.prod_map hf hg
end
/-- The product map of two `C^n` functions is `C^n`. -/
lemma cont_diff.prod_map {f : E → F} {g : E' → F'}
(hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g) :
cont_diff 𝕜 n (prod.map f g) :=
begin
rw cont_diff_iff_cont_diff_at at *,
exact λ ⟨x, y⟩, (hf x).prod_map (hg y)
end
lemma cont_diff_prod_mk_left (f₀ : F) : cont_diff 𝕜 n (λ e : E, (e, f₀)) :=
cont_diff_id.prod cont_diff_const
lemma cont_diff_prod_mk_right (e₀ : E) : cont_diff 𝕜 n (λ f : F, (e₀, f)) :=
cont_diff_const.prod cont_diff_id
end prod_map
lemma cont_diff.clm_comp {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F}
(hg : cont_diff 𝕜 n g) (hf : cont_diff 𝕜 n f) :
cont_diff 𝕜 n (λ x, (g x).comp (f x)) :=
is_bounded_bilinear_map_comp.cont_diff.comp₂ hg hf
lemma cont_diff_on.clm_comp {g : X → F →L[𝕜] G} {f : X → E →L[𝕜] F}
{s : set X} (hg : cont_diff_on 𝕜 n g s) (hf : cont_diff_on 𝕜 n f s) :
cont_diff_on 𝕜 n (λ x, (g x).comp (f x)) s :=
is_bounded_bilinear_map_comp.cont_diff.comp_cont_diff_on₂ hg hf
/-! ### Inversion in a complete normed algebra -/
section algebra_inverse
variables (𝕜) {R : Type*} [normed_ring R] [normed_algebra 𝕜 R]
open normed_ring continuous_linear_map ring
/-- In a complete normed algebra, the operation of inversion is `C^n`, for all `n`, at each
invertible element. The proof is by induction, bootstrapping using an identity expressing the
derivative of inversion as a bilinear map of inversion itself. -/
lemma cont_diff_at_ring_inverse [complete_space R] (x : Rˣ) :
cont_diff_at 𝕜 n ring.inverse (x : R) :=
begin
induction n using with_top.nat_induction with n IH Itop,
{ intros m hm,
refine ⟨{y : R | is_unit y}, _, _⟩,
{ simp [nhds_within_univ],
exact x.nhds },
{ use (ftaylor_series_within 𝕜 inverse univ),
rw [le_antisymm hm bot_le, has_ftaylor_series_up_to_on_zero_iff],
split,
{ rintros _ ⟨x', rfl⟩,
exact (inverse_continuous_at x').continuous_within_at },
{ simp [ftaylor_series_within] } } },
{ apply cont_diff_at_succ_iff_has_fderiv_at.mpr,
refine ⟨λ (x : R), - lmul_left_right 𝕜 R (inverse x) (inverse x), _, _⟩,
{ refine ⟨{y : R | is_unit y}, x.nhds, _⟩,
rintros _ ⟨y, rfl⟩,
rw [inverse_unit],
exact has_fderiv_at_ring_inverse y },
{ convert (lmul_left_right_is_bounded_bilinear 𝕜 R).cont_diff.neg.comp_cont_diff_at
(x : R) (IH.prod IH) } },
{ exact cont_diff_at_top.mpr Itop }
end
variables (𝕜) {𝕜' : Type*} [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [complete_space 𝕜']
lemma cont_diff_at_inv {x : 𝕜'} (hx : x ≠ 0) {n} :
cont_diff_at 𝕜 n has_inv.inv x :=
by simpa only [ring.inverse_eq_inv'] using cont_diff_at_ring_inverse 𝕜 (units.mk0 x hx)
lemma cont_diff_on_inv {n} : cont_diff_on 𝕜 n (has_inv.inv : 𝕜' → 𝕜') {0}ᶜ :=
λ x hx, (cont_diff_at_inv 𝕜 hx).cont_diff_within_at
variable {𝕜}
-- TODO: the next few lemmas don't need `𝕜` or `𝕜'` to be complete
-- A good way to show this is to generalize `cont_diff_at_ring_inverse` to the setting
-- of a function `f` such that `∀ᶠ x in 𝓝 a, x * f x = 1`.
lemma cont_diff_within_at.inv {f : E → 𝕜'} {n} (hf : cont_diff_within_at 𝕜 n f s x)
(hx : f x ≠ 0) :
cont_diff_within_at 𝕜 n (λ x, (f x)⁻¹) s x :=
(cont_diff_at_inv 𝕜 hx).comp_cont_diff_within_at x hf
lemma cont_diff_on.inv {f : E → 𝕜'} {n} (hf : cont_diff_on 𝕜 n f s)
(h : ∀ x ∈ s, f x ≠ 0) :
cont_diff_on 𝕜 n (λ x, (f x)⁻¹) s :=
λ x hx, (hf.cont_diff_within_at hx).inv (h x hx)
lemma cont_diff_at.inv {f : E → 𝕜'} {n} (hf : cont_diff_at 𝕜 n f x) (hx : f x ≠ 0) :
cont_diff_at 𝕜 n (λ x, (f x)⁻¹) x :=
hf.inv hx
lemma cont_diff.inv {f : E → 𝕜'} {n} (hf : cont_diff 𝕜 n f) (h : ∀ x, f x ≠ 0) :
cont_diff 𝕜 n (λ x, (f x)⁻¹) :=
by { rw cont_diff_iff_cont_diff_at, exact λ x, hf.cont_diff_at.inv (h x) }
-- TODO: generalize to `f g : E → 𝕜'`
lemma cont_diff_within_at.div [complete_space 𝕜] {f g : E → 𝕜} {n}
(hf : cont_diff_within_at 𝕜 n f s x) (hg : cont_diff_within_at 𝕜 n g s x)
(hx : g x ≠ 0) :
cont_diff_within_at 𝕜 n (λ x, f x / g x) s x :=
by simpa only [div_eq_mul_inv] using hf.mul (hg.inv hx)
lemma cont_diff_on.div [complete_space 𝕜] {f g : E → 𝕜} {n}
(hf : cont_diff_on 𝕜 n f s) (hg : cont_diff_on 𝕜 n g s) (h₀ : ∀ x ∈ s, g x ≠ 0) :
cont_diff_on 𝕜 n (f / g) s :=
λ x hx, (hf x hx).div (hg x hx) (h₀ x hx)
lemma cont_diff_at.div [complete_space 𝕜] {f g : E → 𝕜} {n}
(hf : cont_diff_at 𝕜 n f x) (hg : cont_diff_at 𝕜 n g x)
(hx : g x ≠ 0) :
cont_diff_at 𝕜 n (λ x, f x / g x) x :=
hf.div hg hx
lemma cont_diff.div [complete_space 𝕜] {f g : E → 𝕜} {n}
(hf : cont_diff 𝕜 n f) (hg : cont_diff 𝕜 n g)
(h0 : ∀ x, g x ≠ 0) :
cont_diff 𝕜 n (λ x, f x / g x) :=
begin
simp only [cont_diff_iff_cont_diff_at] at *,
exact λ x, (hf x).div (hg x) (h0 x)
end
end algebra_inverse
/-! ### Inversion of continuous linear maps between Banach spaces -/
section map_inverse
open continuous_linear_map
/-- At a continuous linear equivalence `e : E ≃L[𝕜] F` between Banach spaces, the operation of
inversion is `C^n`, for all `n`. -/
lemma cont_diff_at_map_inverse [complete_space E] (e : E ≃L[𝕜] F) :
cont_diff_at 𝕜 n inverse (e : E →L[𝕜] F) :=
begin
nontriviality E,
-- first, we use the lemma `to_ring_inverse` to rewrite in terms of `ring.inverse` in the ring
-- `E →L[𝕜] E`
let O₁ : (E →L[𝕜] E) → (F →L[𝕜] E) := λ f, f.comp (e.symm : (F →L[𝕜] E)),
let O₂ : (E →L[𝕜] F) → (E →L[𝕜] E) := λ f, (e.symm : (F →L[𝕜] E)).comp f,
have : continuous_linear_map.inverse = O₁ ∘ ring.inverse ∘ O₂ :=
funext (to_ring_inverse e),
rw this,
-- `O₁` and `O₂` are `cont_diff`,
-- so we reduce to proving that `ring.inverse` is `cont_diff`
have h₁ : cont_diff 𝕜 n O₁ := cont_diff_id.clm_comp cont_diff_const,
have h₂ : cont_diff 𝕜 n O₂ := cont_diff_const.clm_comp cont_diff_id,
refine h₁.cont_diff_at.comp _ (cont_diff_at.comp _ _ h₂.cont_diff_at),
convert cont_diff_at_ring_inverse 𝕜 (1 : (E →L[𝕜] E)ˣ),
simp [O₂, one_def]
end
end map_inverse
section function_inverse
open continuous_linear_map
/-- If `f` is a local homeomorphism and the point `a` is in its target,
and if `f` is `n` times continuously differentiable at `f.symm a`,
and if the derivative at `f.symm a` is a continuous linear equivalence,
then `f.symm` is `n` times continuously differentiable at the point `a`.
This is one of the easy parts of the inverse function theorem: it assumes that we already have
an inverse function. -/
theorem local_homeomorph.cont_diff_at_symm [complete_space E]
(f : local_homeomorph E F) {f₀' : E ≃L[𝕜] F} {a : F} (ha : a ∈ f.target)
(hf₀' : has_fderiv_at f (f₀' : E →L[𝕜] F) (f.symm a)) (hf : cont_diff_at 𝕜 n f (f.symm a)) :
cont_diff_at 𝕜 n f.symm a :=
begin
-- We prove this by induction on `n`
induction n using with_top.nat_induction with n IH Itop,
{ rw cont_diff_at_zero,
exact ⟨f.target, is_open.mem_nhds f.open_target ha, f.continuous_inv_fun⟩ },
{ obtain ⟨f', ⟨u, hu, hff'⟩, hf'⟩ := cont_diff_at_succ_iff_has_fderiv_at.mp hf,
apply cont_diff_at_succ_iff_has_fderiv_at.mpr,
-- For showing `n.succ` times continuous differentiability (the main inductive step), it
-- suffices to produce the derivative and show that it is `n` times continuously differentiable
have eq_f₀' : f' (f.symm a) = f₀',
{ exact (hff' (f.symm a) (mem_of_mem_nhds hu)).unique hf₀' },
-- This follows by a bootstrapping formula expressing the derivative as a function of `f` itself
refine ⟨inverse ∘ f' ∘ f.symm, _, _⟩,
{ -- We first check that the derivative of `f` is that formula
have h_nhds : {y : E | ∃ (e : E ≃L[𝕜] F), ↑e = f' y} ∈ 𝓝 ((f.symm) a),
{ have hf₀' := f₀'.nhds,
rw ← eq_f₀' at hf₀',
exact hf'.continuous_at.preimage_mem_nhds hf₀' },
obtain ⟨t, htu, ht, htf⟩ := mem_nhds_iff.mp (filter.inter_mem hu h_nhds),
use f.target ∩ (f.symm) ⁻¹' t,
refine ⟨is_open.mem_nhds _ _, _⟩,
{ exact f.preimage_open_of_open_symm ht },
{ exact mem_inter ha (mem_preimage.mpr htf) },
intros x hx,
obtain ⟨hxu, e, he⟩ := htu hx.2,
have h_deriv : has_fderiv_at f ↑e ((f.symm) x),
{ rw he,
exact hff' (f.symm x) hxu },
convert f.has_fderiv_at_symm hx.1 h_deriv,
simp [← he] },
{ -- Then we check that the formula, being a composition of `cont_diff` pieces, is
-- itself `cont_diff`
have h_deriv₁ : cont_diff_at 𝕜 n inverse (f' (f.symm a)),
{ rw eq_f₀',
exact cont_diff_at_map_inverse _ },
have h_deriv₂ : cont_diff_at 𝕜 n f.symm a,
{ refine IH (hf.of_le _),
norm_cast,
exact nat.le_succ n },
exact (h_deriv₁.comp _ hf').comp _ h_deriv₂ } },
{ refine cont_diff_at_top.mpr _,
intros n,
exact Itop n (cont_diff_at_top.mp hf n) }
end
/-- If `f` is an `n` times continuously differentiable homeomorphism,
and if the derivative of `f` at each point is a continuous linear equivalence,
then `f.symm` is `n` times continuously differentiable.
This is one of the easy parts of the inverse function theorem: it assumes that we already have
an inverse function. -/
theorem homeomorph.cont_diff_symm [complete_space E] (f : E ≃ₜ F) {f₀' : E → E ≃L[𝕜] F}
(hf₀' : ∀ a, has_fderiv_at f (f₀' a : E →L[𝕜] F) a) (hf : cont_diff 𝕜 n (f : E → F)) :
cont_diff 𝕜 n (f.symm : F → E) :=
cont_diff_iff_cont_diff_at.2 $ λ x,
f.to_local_homeomorph.cont_diff_at_symm (mem_univ x) (hf₀' _) hf.cont_diff_at
/-- Let `f` be a local homeomorphism of a nontrivially normed field, let `a` be a point in its
target. if `f` is `n` times continuously differentiable at `f.symm a`, and if the derivative at
`f.symm a` is nonzero, then `f.symm` is `n` times continuously differentiable at the point `a`.
This is one of the easy parts of the inverse function theorem: it assumes that we already have
an inverse function. -/
theorem local_homeomorph.cont_diff_at_symm_deriv [complete_space 𝕜]
(f : local_homeomorph 𝕜 𝕜) {f₀' a : 𝕜} (h₀ : f₀' ≠ 0) (ha : a ∈ f.target)
(hf₀' : has_deriv_at f f₀' (f.symm a)) (hf : cont_diff_at 𝕜 n f (f.symm a)) :
cont_diff_at 𝕜 n f.symm a :=
f.cont_diff_at_symm ha (hf₀'.has_fderiv_at_equiv h₀) hf
/-- Let `f` be an `n` times continuously differentiable homeomorphism of a nontrivially normed
field. Suppose that the derivative of `f` is never equal to zero. Then `f.symm` is `n` times
continuously differentiable.
This is one of the easy parts of the inverse function theorem: it assumes that we already have
an inverse function. -/
theorem homeomorph.cont_diff_symm_deriv [complete_space 𝕜] (f : 𝕜 ≃ₜ 𝕜) {f' : 𝕜 → 𝕜}
(h₀ : ∀ x, f' x ≠ 0) (hf' : ∀ x, has_deriv_at f (f' x) x) (hf : cont_diff 𝕜 n (f : 𝕜 → 𝕜)) :
cont_diff 𝕜 n (f.symm : 𝕜 → 𝕜) :=
cont_diff_iff_cont_diff_at.2 $ λ x,
f.to_local_homeomorph.cont_diff_at_symm_deriv (h₀ _) (mem_univ x) (hf' _) hf.cont_diff_at
end function_inverse
/-! ### Finite dimensional results -/
section finite_dimensional
open function finite_dimensional
variables [complete_space 𝕜]
/-- A family of continuous linear maps is `C^n` on `s` if all its applications are. -/
lemma cont_diff_on_clm_apply {n : with_top ℕ} {f : E → F →L[𝕜] G}
{s : set E} [finite_dimensional 𝕜 F] :
cont_diff_on 𝕜 n f s ↔ ∀ y, cont_diff_on 𝕜 n (λ x, f x y) s :=
begin
refine ⟨λ h y, (continuous_linear_map.apply 𝕜 G y).cont_diff.comp_cont_diff_on h, λ h, _⟩,
let d := finrank 𝕜 F,
have hd : d = finrank 𝕜 (fin d → 𝕜) := (finrank_fin_fun 𝕜).symm,
let e₁ := continuous_linear_equiv.of_finrank_eq hd,
let e₂ := (e₁.arrow_congr (1 : G ≃L[𝕜] G)).trans (continuous_linear_equiv.pi_ring (fin d)),
rw [← comp.left_id f, ← e₂.symm_comp_self],
exact e₂.symm.cont_diff.comp_cont_diff_on (cont_diff_on_pi.mpr (λ i, h _))
end
lemma cont_diff_clm_apply {n : with_top ℕ} {f : E → F →L[𝕜] G} [finite_dimensional 𝕜 F] :
cont_diff 𝕜 n f ↔ ∀ y, cont_diff 𝕜 n (λ x, f x y) :=
by simp_rw [← cont_diff_on_univ, cont_diff_on_clm_apply]
/-- This is a useful lemma to prove that a certain operation preserves functions being `C^n`.
When you do induction on `n`, this gives a useful characterization of a function being `C^(n+1)`,
assuming you have already computed the derivative. The advantage of this version over
`cont_diff_succ_iff_fderiv` is that both occurences of `cont_diff` are for functions with the same
domain and codomain (`E` and `F`). This is not the case for `cont_diff_succ_iff_fderiv`, which
often requires an inconvenient need to generalize `F`, which results in universe issues
(see the discussion in the section of `cont_diff.comp`).
This lemma avoids these universe issues, but only applies for finite dimensional `E`. -/
lemma cont_diff_succ_iff_fderiv_apply [finite_dimensional 𝕜 E] {n : ℕ} {f : E → F} :
cont_diff 𝕜 ((n + 1) : ℕ) f ↔
differentiable 𝕜 f ∧ ∀ y, cont_diff 𝕜 n (λ x, fderiv 𝕜 f x y) :=
by rw [cont_diff_succ_iff_fderiv, cont_diff_clm_apply]
lemma cont_diff_on_succ_of_fderiv_apply [finite_dimensional 𝕜 E] {n : ℕ} {f : E → F}
{s : set E} (hf : differentiable_on 𝕜 f s)
(h : ∀ y, cont_diff_on 𝕜 n (λ x, fderiv_within 𝕜 f s x y) s) :
cont_diff_on 𝕜 ((n + 1) : ℕ) f s :=
cont_diff_on_succ_of_fderiv_within hf $ cont_diff_on_clm_apply.mpr h
lemma cont_diff_on_succ_iff_fderiv_apply [finite_dimensional 𝕜 E] {n : ℕ} {f : E → F}
{s : set E} (hs : unique_diff_on 𝕜 s) : cont_diff_on 𝕜 ((n + 1) : ℕ) f s ↔
differentiable_on 𝕜 f s ∧ ∀ y, cont_diff_on 𝕜 n (λ x, fderiv_within 𝕜 f s x y) s :=
by rw [cont_diff_on_succ_iff_fderiv_within hs, cont_diff_on_clm_apply]
end finite_dimensional
section real
/-!
### Results over `ℝ` or `ℂ`
The results in this section rely on the Mean Value Theorem, and therefore hold only over `ℝ` (and
its extension fields such as `ℂ`).
-/
variables
{𝕂 : Type*} [is_R_or_C 𝕂]
{E' : Type*} [normed_add_comm_group E'] [normed_space 𝕂 E']
{F' : Type*} [normed_add_comm_group F'] [normed_space 𝕂 F']
/-- If a function has a Taylor series at order at least 1, then at points in the interior of the
domain of definition, the term of order 1 of this series is a strict derivative of `f`. -/
lemma has_ftaylor_series_up_to_on.has_strict_fderiv_at
{s : set E'} {f : E' → F'} {x : E'} {p : E' → formal_multilinear_series 𝕂 E' F'}
(hf : has_ftaylor_series_up_to_on n f p s) (hn : 1 ≤ n) (hs : s ∈ 𝓝 x) :
has_strict_fderiv_at f ((continuous_multilinear_curry_fin1 𝕂 E' F') (p x 1)) x :=
has_strict_fderiv_at_of_has_fderiv_at_of_continuous_at (hf.eventually_has_fderiv_at hn hs) $
(continuous_multilinear_curry_fin1 𝕂 E' F').continuous_at.comp $
(hf.cont 1 hn).continuous_at hs
/-- If a function is `C^n` with `1 ≤ n` around a point, and its derivative at that point is given to
us as `f'`, then `f'` is also a strict derivative. -/
lemma cont_diff_at.has_strict_fderiv_at'
{f : E' → F'} {f' : E' →L[𝕂] F'} {x : E'}
(hf : cont_diff_at 𝕂 n f x) (hf' : has_fderiv_at f f' x) (hn : 1 ≤ n) :
has_strict_fderiv_at f f' x :=
begin
rcases hf 1 hn with ⟨u, H, p, hp⟩,
simp only [nhds_within_univ, mem_univ, insert_eq_of_mem] at H,
have := hp.has_strict_fderiv_at le_rfl H,
rwa hf'.unique this.has_fderiv_at
end
/-- If a function is `C^n` with `1 ≤ n` around a point, and its derivative at that point is given to
us as `f'`, then `f'` is also a strict derivative. -/
lemma cont_diff_at.has_strict_deriv_at' {f : 𝕂 → F'} {f' : F'} {x : 𝕂}
(hf : cont_diff_at 𝕂 n f x) (hf' : has_deriv_at f f' x) (hn : 1 ≤ n) :
has_strict_deriv_at f f' x :=
hf.has_strict_fderiv_at' hf' hn
/-- If a function is `C^n` with `1 ≤ n` around a point, then the derivative of `f` at this point
is also a strict derivative. -/
lemma cont_diff_at.has_strict_fderiv_at {f : E' → F'} {x : E'}
(hf : cont_diff_at 𝕂 n f x) (hn : 1 ≤ n) :
has_strict_fderiv_at f (fderiv 𝕂 f x) x :=
hf.has_strict_fderiv_at' (hf.differentiable_at hn).has_fderiv_at hn
/-- If a function is `C^n` with `1 ≤ n` around a point, then the derivative of `f` at this point
is also a strict derivative. -/
lemma cont_diff_at.has_strict_deriv_at {f : 𝕂 → F'} {x : 𝕂}
(hf : cont_diff_at 𝕂 n f x) (hn : 1 ≤ n) :
has_strict_deriv_at f (deriv f x) x :=
(hf.has_strict_fderiv_at hn).has_strict_deriv_at
/-- If a function is `C^n` with `1 ≤ n`, then the derivative of `f` is also a strict derivative. -/
lemma cont_diff.has_strict_fderiv_at
{f : E' → F'} {x : E'} (hf : cont_diff 𝕂 n f) (hn : 1 ≤ n) :
has_strict_fderiv_at f (fderiv 𝕂 f x) x :=
hf.cont_diff_at.has_strict_fderiv_at hn
/-- If a function is `C^n` with `1 ≤ n`, then the derivative of `f` is also a strict derivative. -/
lemma cont_diff.has_strict_deriv_at
{f : 𝕂 → F'} {x : 𝕂} (hf : cont_diff 𝕂 n f) (hn : 1 ≤ n) :
has_strict_deriv_at f (deriv f x) x :=
hf.cont_diff_at.has_strict_deriv_at hn
/-- If `f` has a formal Taylor series `p` up to order `1` on `{x} ∪ s`, where `s` is a convex set,
and `∥p x 1∥₊ < K`, then `f` is `K`-Lipschitz in a neighborhood of `x` within `s`. -/
lemma has_ftaylor_series_up_to_on.exists_lipschitz_on_with_of_nnnorm_lt {E F : Type*}
[normed_add_comm_group E] [normed_space ℝ E] [normed_add_comm_group F] [normed_space ℝ F]
{f : E → F} {p : E → formal_multilinear_series ℝ E F} {s : set E} {x : E}
(hf : has_ftaylor_series_up_to_on 1 f p (insert x s)) (hs : convex ℝ s) (K : ℝ≥0)
(hK : ∥p x 1∥₊ < K) :
∃ t ∈ 𝓝[s] x, lipschitz_on_with K f t :=
begin
set f' := λ y, continuous_multilinear_curry_fin1 ℝ E F (p y 1),
have hder : ∀ y ∈ s, has_fderiv_within_at f (f' y) s y,
from λ y hy, (hf.has_fderiv_within_at le_rfl (subset_insert x s hy)).mono (subset_insert x s),
have hcont : continuous_within_at f' s x,
from (continuous_multilinear_curry_fin1 ℝ E F).continuous_at.comp_continuous_within_at
((hf.cont _ le_rfl _ (mem_insert _ _)).mono (subset_insert x s)),
replace hK : ∥f' x∥₊ < K, by simpa only [linear_isometry_equiv.nnnorm_map],
exact hs.exists_nhds_within_lipschitz_on_with_of_has_fderiv_within_at_of_nnnorm_lt
(eventually_nhds_within_iff.2 $ eventually_of_forall hder) hcont K hK
end
/-- If `f` has a formal Taylor series `p` up to order `1` on `{x} ∪ s`, where `s` is a convex set,
then `f` is Lipschitz in a neighborhood of `x` within `s`. -/
lemma has_ftaylor_series_up_to_on.exists_lipschitz_on_with {E F : Type*} [normed_add_comm_group E]
[normed_space ℝ E] [normed_add_comm_group F] [normed_space ℝ F] {f : E → F}
{p : E → formal_multilinear_series ℝ E F} {s : set E} {x : E}
(hf : has_ftaylor_series_up_to_on 1 f p (insert x s)) (hs : convex ℝ s) :
∃ K (t ∈ 𝓝[s] x), lipschitz_on_with K f t :=
(exists_gt _).imp $ hf.exists_lipschitz_on_with_of_nnnorm_lt hs
/-- If `f` is `C^1` within a conves set `s` at `x`, then it is Lipschitz on a neighborhood of `x`
within `s`. -/
lemma cont_diff_within_at.exists_lipschitz_on_with {E F : Type*} [normed_add_comm_group E]
[normed_space ℝ E] [normed_add_comm_group F] [normed_space ℝ F] {f : E → F} {s : set E}
{x : E} (hf : cont_diff_within_at ℝ 1 f s x) (hs : convex ℝ s) :
∃ (K : ℝ≥0) (t ∈ 𝓝[s] x), lipschitz_on_with K f t :=
begin
rcases hf 1 le_rfl with ⟨t, hst, p, hp⟩,
rcases metric.mem_nhds_within_iff.mp hst with ⟨ε, ε0, hε⟩,
replace hp : has_ftaylor_series_up_to_on 1 f p (metric.ball x ε ∩ insert x s) := hp.mono hε,
clear hst hε t,
rw [← insert_eq_of_mem (metric.mem_ball_self ε0), ← insert_inter_distrib] at hp,
rcases hp.exists_lipschitz_on_with ((convex_ball _ _).inter hs) with ⟨K, t, hst, hft⟩,
rw [inter_comm, ← nhds_within_restrict' _ (metric.ball_mem_nhds _ ε0)] at hst,
exact ⟨K, t, hst, hft⟩
end
/-- If `f` is `C^1` at `x` and `K > ∥fderiv 𝕂 f x∥`, then `f` is `K`-Lipschitz in a neighborhood of
`x`. -/
lemma cont_diff_at.exists_lipschitz_on_with_of_nnnorm_lt {f : E' → F'} {x : E'}
(hf : cont_diff_at 𝕂 1 f x) (K : ℝ≥0) (hK : ∥fderiv 𝕂 f x∥₊ < K) :
∃ t ∈ 𝓝 x, lipschitz_on_with K f t :=
(hf.has_strict_fderiv_at le_rfl).exists_lipschitz_on_with_of_nnnorm_lt K hK
/-- If `f` is `C^1` at `x`, then `f` is Lipschitz in a neighborhood of `x`. -/
lemma cont_diff_at.exists_lipschitz_on_with {f : E' → F'} {x : E'}
(hf : cont_diff_at 𝕂 1 f x) :
∃ K (t ∈ 𝓝 x), lipschitz_on_with K f t :=
(hf.has_strict_fderiv_at le_rfl).exists_lipschitz_on_with
end real
section deriv
/-!
### One dimension
All results up to now have been expressed in terms of the general Fréchet derivative `fderiv`. For
maps defined on the field, the one-dimensional derivative `deriv` is often easier to use. In this
paragraph, we reformulate some higher smoothness results in terms of `deriv`.
-/
variables {f₂ : 𝕜 → F} {s₂ : set 𝕜}
open continuous_linear_map (smul_right)
/-- A function is `C^(n + 1)` on a domain with unique derivatives if and only if it is
differentiable there, and its derivative (formulated with `deriv_within`) is `C^n`. -/
theorem cont_diff_on_succ_iff_deriv_within {n : ℕ} (hs : unique_diff_on 𝕜 s₂) :
cont_diff_on 𝕜 ((n + 1) : ℕ) f₂ s₂ ↔
differentiable_on 𝕜 f₂ s₂ ∧ cont_diff_on 𝕜 n (deriv_within f₂ s₂) s₂ :=
begin
rw cont_diff_on_succ_iff_fderiv_within hs,
congr' 2,
apply le_antisymm,
{ assume h,
have : deriv_within f₂ s₂ = (λ u : 𝕜 →L[𝕜] F, u 1) ∘ (fderiv_within 𝕜 f₂ s₂),
by { ext x, refl },
simp only [this],
apply cont_diff.comp_cont_diff_on _ h,
exact (is_bounded_bilinear_map_apply.is_bounded_linear_map_left _).cont_diff },
{ assume h,
have : fderiv_within 𝕜 f₂ s₂ = smul_right (1 : 𝕜 →L[𝕜] 𝕜) ∘ deriv_within f₂ s₂,
by { ext x, simp [deriv_within] },
simp only [this],
apply cont_diff.comp_cont_diff_on _ h,
have : is_bounded_bilinear_map 𝕜 (λ _ : (𝕜 →L[𝕜] 𝕜) × F, _) :=
is_bounded_bilinear_map_smul_right,
exact (this.is_bounded_linear_map_right _).cont_diff }
end
/-- A function is `C^(n + 1)` on an open domain if and only if it is
differentiable there, and its derivative (formulated with `deriv`) is `C^n`. -/
theorem cont_diff_on_succ_iff_deriv_of_open {n : ℕ} (hs : is_open s₂) :
cont_diff_on 𝕜 ((n + 1) : ℕ) f₂ s₂ ↔
differentiable_on 𝕜 f₂ s₂ ∧ cont_diff_on 𝕜 n (deriv f₂) s₂ :=
begin
rw cont_diff_on_succ_iff_deriv_within hs.unique_diff_on,
congrm _ ∧ _,
apply cont_diff_on_congr,
assume x hx,
exact deriv_within_of_open hs hx
end
/-- A function is `C^∞` on a domain with unique derivatives if and only if it is differentiable
there, and its derivative (formulated with `deriv_within`) is `C^∞`. -/
theorem cont_diff_on_top_iff_deriv_within (hs : unique_diff_on 𝕜 s₂) :
cont_diff_on 𝕜 ∞ f₂ s₂ ↔
differentiable_on 𝕜 f₂ s₂ ∧ cont_diff_on 𝕜 ∞ (deriv_within f₂ s₂) s₂ :=
begin
split,
{ assume h,
refine ⟨h.differentiable_on le_top, _⟩,
apply cont_diff_on_top.2 (λ n, ((cont_diff_on_succ_iff_deriv_within hs).1 _).2),
exact h.of_le le_top },
{ assume h,
refine cont_diff_on_top.2 (λ n, _),
have A : (n : with_top ℕ) ≤ ∞ := le_top,
apply ((cont_diff_on_succ_iff_deriv_within hs).2 ⟨h.1, h.2.of_le A⟩).of_le,
exact with_top.coe_le_coe.2 (nat.le_succ n) }
end
/-- A function is `C^∞` on an open domain if and only if it is differentiable
there, and its derivative (formulated with `deriv`) is `C^∞`. -/
theorem cont_diff_on_top_iff_deriv_of_open (hs : is_open s₂) :
cont_diff_on 𝕜 ∞ f₂ s₂ ↔
differentiable_on 𝕜 f₂ s₂ ∧ cont_diff_on 𝕜 ∞ (deriv f₂) s₂ :=
begin
rw cont_diff_on_top_iff_deriv_within hs.unique_diff_on,
congrm _ ∧ _,
apply cont_diff_on_congr,
assume x hx,
exact deriv_within_of_open hs hx
end
lemma cont_diff_on.deriv_within
(hf : cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hmn : m + 1 ≤ n) :
cont_diff_on 𝕜 m (deriv_within f₂ s₂) s₂ :=
begin
cases m,
{ change ∞ + 1 ≤ n at hmn,
have : n = ∞, by simpa using hmn,
rw this at hf,
exact ((cont_diff_on_top_iff_deriv_within hs).1 hf).2 },
{ change (m.succ : with_top ℕ) ≤ n at hmn,
exact ((cont_diff_on_succ_iff_deriv_within hs).1 (hf.of_le hmn)).2 }
end
lemma cont_diff_on.deriv_of_open
(hf : cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hmn : m + 1 ≤ n) :
cont_diff_on 𝕜 m (deriv f₂) s₂ :=
(hf.deriv_within hs.unique_diff_on hmn).congr (λ x hx, (deriv_within_of_open hs hx).symm)
lemma cont_diff_on.continuous_on_deriv_within
(h : cont_diff_on 𝕜 n f₂ s₂) (hs : unique_diff_on 𝕜 s₂) (hn : 1 ≤ n) :
continuous_on (deriv_within f₂ s₂) s₂ :=
((cont_diff_on_succ_iff_deriv_within hs).1 (h.of_le hn)).2.continuous_on
lemma cont_diff_on.continuous_on_deriv_of_open
(h : cont_diff_on 𝕜 n f₂ s₂) (hs : is_open s₂) (hn : 1 ≤ n) :
continuous_on (deriv f₂) s₂ :=
((cont_diff_on_succ_iff_deriv_of_open hs).1 (h.of_le hn)).2.continuous_on
/-- A function is `C^(n + 1)` if and only if it is differentiable,
and its derivative (formulated in terms of `deriv`) is `C^n`. -/
theorem cont_diff_succ_iff_deriv {n : ℕ} :
cont_diff 𝕜 ((n + 1) : ℕ) f₂ ↔
differentiable 𝕜 f₂ ∧ cont_diff 𝕜 n (deriv f₂) :=
by simp only [← cont_diff_on_univ, cont_diff_on_succ_iff_deriv_of_open, is_open_univ,
differentiable_on_univ]
theorem cont_diff_one_iff_deriv :
cont_diff 𝕜 1 f₂ ↔ differentiable 𝕜 f₂ ∧ continuous (deriv f₂) :=
cont_diff_succ_iff_deriv.trans $ iff.rfl.and cont_diff_zero
/-- A function is `C^∞` if and only if it is differentiable,
and its derivative (formulated in terms of `deriv`) is `C^∞`. -/
theorem cont_diff_top_iff_deriv :
cont_diff 𝕜 ∞ f₂ ↔
differentiable 𝕜 f₂ ∧ cont_diff 𝕜 ∞ (deriv f₂) :=
begin
simp [cont_diff_on_univ.symm, differentiable_on_univ.symm, deriv_within_univ.symm,
- deriv_within_univ],
rw cont_diff_on_top_iff_deriv_within unique_diff_on_univ,
end
lemma cont_diff.continuous_deriv (h : cont_diff 𝕜 n f₂) (hn : 1 ≤ n) :
continuous (deriv f₂) :=
(cont_diff_succ_iff_deriv.mp (h.of_le hn)).2.continuous
end deriv
section restrict_scalars
/-!
### Restricting from `ℂ` to `ℝ`, or generally from `𝕜'` to `𝕜`
If a function is `n` times continuously differentiable over `ℂ`, then it is `n` times continuously
differentiable over `ℝ`. In this paragraph, we give variants of this statement, in the general
situation where `ℂ` and `ℝ` are replaced respectively by `𝕜'` and `𝕜` where `𝕜'` is a normed algebra
over `𝕜`.
-/
variables (𝕜) {𝕜' : Type*} [nontrivially_normed_field 𝕜'] [normed_algebra 𝕜 𝕜']
variables [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E]
variables [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F]
variables {p' : E → formal_multilinear_series 𝕜' E F}
lemma has_ftaylor_series_up_to_on.restrict_scalars
(h : has_ftaylor_series_up_to_on n f p' s) :
has_ftaylor_series_up_to_on n f (λ x, (p' x).restrict_scalars 𝕜) s :=
{ zero_eq := λ x hx, h.zero_eq x hx,
fderiv_within :=
begin
intros m hm x hx,
convert ((continuous_multilinear_map.restrict_scalars_linear 𝕜).has_fderiv_at)
.comp_has_fderiv_within_at _ ((h.fderiv_within m hm x hx).restrict_scalars 𝕜),
end,
cont := λ m hm, continuous_multilinear_map.continuous_restrict_scalars.comp_continuous_on
(h.cont m hm) }
lemma cont_diff_within_at.restrict_scalars (h : cont_diff_within_at 𝕜' n f s x) :
cont_diff_within_at 𝕜 n f s x :=
begin
intros m hm,
rcases h m hm with ⟨u, u_mem, p', hp'⟩,
exact ⟨u, u_mem, _, hp'.restrict_scalars _⟩
end
lemma cont_diff_on.restrict_scalars (h : cont_diff_on 𝕜' n f s) :
cont_diff_on 𝕜 n f s :=
λ x hx, (h x hx).restrict_scalars _
lemma cont_diff_at.restrict_scalars (h : cont_diff_at 𝕜' n f x) :
cont_diff_at 𝕜 n f x :=
cont_diff_within_at_univ.1 $ h.cont_diff_within_at.restrict_scalars _
lemma cont_diff.restrict_scalars (h : cont_diff 𝕜' n f) :
cont_diff 𝕜 n f :=
cont_diff_iff_cont_diff_at.2 $ λ x, h.cont_diff_at.restrict_scalars _
end restrict_scalars
|
04b9d83b29266a70ea476392113c96de4b4732ef | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/category_theory/Fintype.lean | 7492fd6bce3782e79b5eac976fcf0334d1ba616d | [
"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,306 | lean | /-
Copyright (c) 2020 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta, Adam Topaz
-/
import category_theory.concrete_category.bundled
import category_theory.full_subcategory
import category_theory.skeletal
import data.fin.basic
import data.fintype.basic
/-!
# The category of finite types.
We define the category of finite types, denoted `Fintype` as
(bundled) types with a `fintype` instance.
We also define `Fintype.skeleton`, the standard skeleton of `Fintype` whose objects are `fin n`
for `n : ℕ`. We prove that the obvious inclusion functor `Fintype.skeleton ⥤ Fintype` is an
equivalence of categories in `Fintype.skeleton.equivalence`.
We prove that `Fintype.skeleton` is a skeleton of `Fintype` in `Fintype.is_skeleton`.
-/
open_locale classical
open category_theory
/-- The category of finite types. -/
def Fintype := bundled fintype
namespace Fintype
instance : has_coe_to_sort Fintype Type* := bundled.has_coe_to_sort
/-- Construct a bundled `Fintype` from the underlying type and typeclass. -/
def of (X : Type*) [fintype X] : Fintype := bundled.of X
instance : inhabited Fintype := ⟨⟨pempty⟩⟩
instance {X : Fintype} : fintype X := X.2
instance : category Fintype := induced_category.category bundled.α
/-- The fully faithful embedding of `Fintype` into the category of types. -/
@[derive [full, faithful], simps]
def incl : Fintype ⥤ Type* := induced_functor _
instance : concrete_category Fintype := ⟨incl⟩
@[simp] lemma id_apply (X : Fintype) (x : X) : (𝟙 X : X → X) x = x := rfl
@[simp] lemma comp_apply {X Y Z : Fintype} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) :
(f ≫ g) x = g (f x) := rfl
universe u
/--
The "standard" skeleton for `Fintype`. This is the full subcategory of `Fintype` spanned by objects
of the form `ulift (fin n)` for `n : ℕ`. We parameterize the objects of `Fintype.skeleton`
directly as `ulift ℕ`, as the type `ulift (fin m) ≃ ulift (fin n)` is
nonempty if and only if `n = m`. Specifying universes, `skeleton : Type u` is a small
skeletal category equivalent to `Fintype.{u}`.
-/
def skeleton : Type u := ulift ℕ
namespace skeleton
/-- Given any natural number `n`, this creates the associated object of `Fintype.skeleton`. -/
def mk : ℕ → skeleton := ulift.up
instance : inhabited skeleton := ⟨mk 0⟩
/-- Given any object of `Fintype.skeleton`, this returns the associated natural number. -/
def len : skeleton → ℕ := ulift.down
@[ext]
lemma ext (X Y : skeleton) : X.len = Y.len → X = Y := ulift.ext _ _
instance : small_category skeleton.{u} :=
{ hom := λ X Y, ulift.{u} (fin X.len) → ulift.{u} (fin Y.len),
id := λ _, id,
comp := λ _ _ _ f g, g ∘ f }
lemma is_skeletal : skeletal skeleton.{u} := λ X Y ⟨h⟩, ext _ _ $ fin.equiv_iff_eq.mp $
nonempty.intro $
{ to_fun := λ x, (h.hom ⟨x⟩).down,
inv_fun := λ x, (h.inv ⟨x⟩).down,
left_inv := begin
intro a,
change ulift.down _ = _,
rw ulift.up_down,
change ((h.hom ≫ h.inv) _).down = _,
simpa,
end,
right_inv := begin
intro a,
change ulift.down _ = _,
rw ulift.up_down,
change ((h.inv ≫ h.hom) _).down = _,
simpa,
end }
/-- The canonical fully faithful embedding of `Fintype.skeleton` into `Fintype`. -/
def incl : skeleton.{u} ⥤ Fintype.{u} :=
{ obj := λ X, Fintype.of (ulift (fin X.len)),
map := λ _ _ f, f }
instance : full incl := { preimage := λ _ _ f, f }
instance : faithful incl := {}
instance : ess_surj incl :=
ess_surj.mk $ λ X, let F := fintype.equiv_fin X in ⟨mk (fintype.card X), nonempty.intro
{ hom := F.symm ∘ ulift.down,
inv := ulift.up ∘ F }⟩
noncomputable instance : is_equivalence incl :=
equivalence.of_fully_faithfully_ess_surj _
/-- The equivalence between `Fintype.skeleton` and `Fintype`. -/
noncomputable def equivalence : skeleton ≌ Fintype := incl.as_equivalence
@[simp] lemma incl_mk_nat_card (n : ℕ) : fintype.card (incl.obj (mk n)) = n :=
begin
convert finset.card_fin n,
apply fintype.of_equiv_card,
end
end skeleton
/-- `Fintype.skeleton` is a skeleton of `Fintype`. -/
noncomputable def is_skeleton : is_skeleton_of Fintype skeleton skeleton.incl :=
{ skel := skeleton.is_skeletal,
eqv := by apply_instance }
end Fintype
|
0af03299365f0b58e194df6001538603095f5725 | d53312f14e4be505a8abcc1969b73308b038f020 | /06-formalizacija-dokazov/vaje-imp.lean | 8c8c50352f9d8063de9d1afe7c678ef232f09540 | [] | no_license | lunar-starlight/teorija-programskih-jezikov | 18ccdfaa9200c10c2b1ec14624419532dd671d25 | bfdf69114a6b13b1ae5735e8749b543f722e5965 | refs/heads/master | 1,667,393,466,506 | 1,577,651,158,000 | 1,577,651,158,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,517 | lean | -- ==================== Syntax ====================
def loc := string
inductive aexp : Type
| Lookup : loc -> aexp
| Int : int -> aexp
| Plus : aexp -> aexp -> aexp
| Minus : aexp -> aexp -> aexp
| Times : aexp -> aexp -> aexp
inductive bexp : Type
| Bool : bool -> bexp
| Equal : aexp -> aexp -> bexp
| Less : aexp -> aexp -> bexp
inductive cmd : Type
| Assign : loc -> aexp -> cmd
| IfThenElse : bexp -> cmd -> cmd -> cmd
| Seq : cmd -> cmd -> cmd
| Skip : cmd
| WhileDo : bexp -> cmd -> cmd
-- ================== Example 'fact.imp' in LEAN notation. ==================
def fact : cmd :=
cmd.Seq
(cmd.Seq
(cmd.Assign "n" (aexp.Int 10))
(cmd.Assign "fact" (aexp.Int 1)) )
(cmd.WhileDo
(bexp.Less (aexp.Int 0) (aexp.Lookup "n"))
(cmd.Seq
(cmd.Assign "fact"
(aexp.Times (aexp.Lookup "fact") (aexp.Lookup "n")) )
(cmd.Assign "n"
(aexp.Minus (aexp.Lookup "n") (aexp.Int 1)) ) ) )
-- ==================== Environment ====================
inductive env : Type
| Nil : env
| Cons : loc -> int -> env -> env
inductive lookup : loc -> env -> int -> Prop
| Find {loc i E} :
lookup loc (env.Cons loc i E) i
| Search {loc loc' i' E' i} :
loc≠loc' -> lookup loc E' i ->
lookup loc (env.Cons loc' i' E') i
-- ==================== Operational Semantics ====================
inductive aeval : env -> aexp -> int -> Prop
| Lookup {E loc i} :
lookup loc E i ->
aeval E (aexp.Lookup loc) i
| Int {E i} :
aeval E (aexp.Int i) i
| Plus {E a1 a2 i1 i2} :
aeval E a1 i1 -> aeval E a2 i2 ->
aeval E (aexp.Plus a1 a2) (i1 + i2)
| Minus {E a1 a2 i1 i2} :
aeval E a1 i1 -> aeval E a2 i2 ->
aeval E (aexp.Minus a1 a2) (i1 - i2)
| Times {E a1 a2 i1 i2} :
aeval E a1 i1 -> aeval E a2 i2 ->
aeval E (aexp.Times a1 a2) (i1 * i2)
-- Lean works best with '<' and '≤' so we use them instead of '>' and '≥'.
inductive beval : env -> bexp -> bool -> Prop
| Bool {E b} :
beval E (bexp.Bool b) b
| Equal_t {E a1 a2 i1 i2}:
aeval E a1 i1 -> aeval E a2 i2 -> i1 = i2 ->
beval E (bexp.Equal a1 a2) true
| Equal_f {E a1 a2 i1 i2}:
aeval E a1 i1 -> aeval E a2 i2 -> i1 ≠ i2 ->
beval E (bexp.Equal a1 a2) false
| Less_t {E a1 a2 i1 i2}:
aeval E a1 i1 -> aeval E a2 i2 -> i1 < i2 ->
beval E (bexp.Equal a1 a2) true
| Less_f {E a1 a2 i1 i2}:
aeval E a1 i1 -> aeval E a2 i2 -> i2 ≤ i1 ->
beval E (bexp.Equal a1 a2) false
inductive ceval : env -> cmd -> env -> Prop
| Assign {E loc a i} :
aeval E a i ->
ceval E (cmd.Assign loc a) (env.Cons loc i E)
| IfThenElse_t {E b c1 c2 E'} :
beval E b true -> ceval E c1 E' ->
ceval E (cmd.IfThenElse b c1 c2) E'
| IfThenElse_f {E b c1 c2 E'} :
beval E b false -> ceval E c2 E' ->
ceval E (cmd.IfThenElse b c1 c2) E'
| Seq {E c1 c2 E' E''} :
ceval E c1 E' -> ceval E' c2 E'' ->
ceval E (cmd.Seq c1 c2) E''
| Skip {E} :
ceval E cmd.Skip E
| WhileDo_t {E b c E' E''}:
beval E b true -> ceval E c E' ->
ceval E' (cmd.WhileDo b c) E'' ->
ceval E (cmd.WhileDo b c) E''
| WhileDo_f {E b c} :
beval E b false ->
ceval E (cmd.WhileDo b c) E
-- ==================== Safety ====================
-- Contains all the names of already assigned locations.
inductive locs : Type
| Nil : locs
| Cons : loc -> locs -> locs
inductive loc_safe : loc -> locs -> Prop
| Find {loc L} :
loc_safe loc (locs.Cons loc L)
| Search {loc loc' L} :
loc'≠loc -> loc_safe loc L ->
loc_safe loc (locs.Cons loc' L)
inductive asafe : locs -> aexp -> Prop
| Lookup {L l} :
loc_safe l L ->
asafe L (aexp.Lookup l)
| Int {L i} :
asafe L (aexp.Int i)
| Plus {L a1 a2} :
asafe L a1 -> asafe L a2 ->
asafe L (aexp.Plus a1 a2)
| Minus {L a1 a2} :
asafe L a1 -> asafe L a2 ->
asafe L (aexp.Minus a1 a2)
| Times {L a1 a2} :
asafe L a1 -> asafe L a2 ->
asafe L (aexp.Times a1 a2)
inductive bsafe : locs -> bexp -> Prop
| Bool {L b}:
bsafe L (bexp.Bool b)
| Equal {L a1 a2} :
asafe L a1 -> asafe L a2 ->
bsafe L (bexp.Equal a1 a2)
| Less {L a1 a2} :
asafe L a1 -> asafe L a2 ->
bsafe L (bexp.Less a1 a2)
inductive csafe : locs -> cmd -> locs -> Prop
| Assign {L loc a} :
asafe L a ->
csafe L (cmd.Assign loc a) (locs.Cons loc L)
-- | IfThenElse {L b c1 c2 L1 L2} :
-- asafe L b -> csafe L c1 L1 -> csafe L c2 L2 ->
-- csafe L (cmd.IfThenElse b c1 c2) (L1 ∩ L2)
-- Note: This part requires a definition of locs intersection.
| Seq {L c1 L' c2 L''} :
csafe L c1 L' -> csafe L' c2 L'' ->
csafe L (cmd.Seq c1 c2) L''
| Skip {L} :
csafe L (cmd.Skip) L
| WhileDo {L b c L'} :
bsafe L b -> csafe L c L' ->
csafe L (cmd.WhileDo b c) L'
-- ==================== Auxiliary safety for lookup ====================
-- Ensures that the given environment maps all the required locations.
inductive env_maps : env -> locs -> Prop
| Nil {E} :
env_maps E locs.Nil
| Cons {loc E L} :
env_maps E L -> (∃i, lookup loc E i) ->
env_maps E (locs.Cons loc L)
-- Increasing the environment does not break its safety.
theorem env_maps_weaken {E L loc i}:
env_maps E L -> env_maps (env.Cons loc i E) L
:=
begin
intro es,
induction es with E' loc' E' L' maps finds ih,
case env_maps.Nil
{ apply env_maps.Nil, },
case env_maps.Cons {
apply env_maps.Cons, assumption,
-- we compare the the strings to know which lookup result is correct
cases string.has_decidable_eq loc' loc with neq eq,
{ cases finds with i', existsi i',
-- lookup must search deeper
apply lookup.Search,
repeat {assumption},
},
existsi i,
-- loc' and loc are equal, 'subst eq' will rewrite them to the same name.
subst eq,
apply lookup.Find,
}
end
-- If the location is safe in the same specification as the environment
-- then we are guaranteed to look up a value
theorem safe_lookup {L E loc}:
loc_safe loc L -> env_maps E L -> ∃ (i:int), lookup loc E i
:=
begin
-- if we have an impossible hypothesis 'h' (such as a safety check in
-- an empty locs list) we can complete the proof with 'cases h'
sorry
end
-- ==================== Safety theorems ====================
theorem asafety {L E a}:
asafe L a -> env_maps E L -> ∃ (i:int), aeval E a i
:=
begin
intros s es,
induction s,
case asafe.Lookup
{ cases safe_lookup s_a es with i,
-- cases safe_lookup ... applies the theorem and eliminates the ∃
existsi i, apply aeval.Lookup, assumption, },
case asafe.Int
{ existsi s_i, apply aeval.Int, },
case asafe.Plus
{ cases s_ih_a es with i1,
cases s_ih_a_1 es with i2,
existsi i1 + i2, apply aeval.Plus, repeat {assumption}, },
case asafe.Minus
{ cases s_ih_a es with i1,
cases s_ih_a_1 es with i2,
existsi i1 - i2, apply aeval.Minus, repeat {assumption}, },
case asafe.Times
{ cases s_ih_a es with i1,
cases s_ih_a_1 es with i2,
existsi i1 * i2, apply aeval.Times, repeat {assumption}, },
end
theorem bsafety {L E b}:
bsafe L b -> env_maps E L -> ∃ (v:bool), beval E b v
:=
begin
intros s es,
induction s,
case bsafe.Bool
{ existsi s_b, apply beval.Bool, },
case bsafe.Equal
{ cases asafety s_a es with i1,
cases asafety s_a_1 es with i2,
cases int.decidable_eq i1 i2 with neq eq,
-- we cannot just do a case analysis on logical formulas because
-- we are not using classical logic. Luckily integer equality is
-- decidable, so we can specify to do a case analysis on that
-- i1 ≠ i2
{ existsi ff, apply beval.Equal_f,
repeat {assumption}, },
-- i1 = i2
{ existsi tt, apply beval.Equal_t,
repeat {assumption}, },
},
case bsafe.Less
{ sorry,
cases int.decidable_lt i1 i2 with neq eq,
-- i1 ≰ i2
{ sorry },
-- i1 < i2
{ sorry }, },
end
theorem csafety {L L' E c }:
csafe L c L' -> env_maps E L -> ∃ (E':env), ceval E c E' ∧ env_maps E' L'
:=
begin
-- constructor splits ∧ into two subgoals
intros s, revert E, -- we revert to obtain a stronger induction
induction s; intros E es,
case csafe.Assign
{ cases asafety s_a_1 es with i,
existsi (env.Cons s_loc i E),
constructor, -- constructor splits ∧ into two subgoals
{ sorry },
sorry },
case csafe.Seq
{ sorry },
case csafe.Skip
{ sorry },
case csafe.WhileDo
{ -- this part can't really be done in big step semantics
sorry },
end |
be7b765cc6034b7c0f83e87ed4096720654da4a3 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/data/fintype/intervals_auto.lean | cf4d85117fe1be4361f1b79082b8cbd7b27b8779 | [] | 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,524 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.data.set.intervals.default
import Mathlib.data.set.finite
import Mathlib.data.pnat.intervals
import Mathlib.PostPort
namespace Mathlib
/-!
# fintype instances for intervals
We provide `fintype` instances for `Ico l u`, for `l u : ℕ`, and for `l u : ℤ`.
-/
namespace set
protected instance Ico_ℕ_fintype (l : ℕ) (u : ℕ) : fintype ↥(Ico l u) :=
fintype.of_finset (finset.Ico l u) sorry
@[simp] theorem Ico_ℕ_card (l : ℕ) (u : ℕ) : fintype.card ↥(Ico l u) = u - l :=
Eq.trans (fintype.card_of_finset (finset.Ico l u) (Ico_ℕ_fintype._proof_1 l u))
(finset.Ico.card l u)
protected instance Ico_pnat_fintype (l : ℕ+) (u : ℕ+) : fintype ↥(Ico l u) :=
fintype.of_finset (pnat.Ico l u) sorry
@[simp] theorem Ico_pnat_card (l : ℕ+) (u : ℕ+) : fintype.card ↥(Ico l u) = ↑u - ↑l :=
Eq.trans (fintype.card_of_finset (pnat.Ico l u) (Ico_pnat_fintype._proof_1 l u))
(pnat.Ico.card l u)
protected instance Ico_ℤ_fintype (l : ℤ) (u : ℤ) : fintype ↥(Ico l u) :=
fintype.of_finset (finset.Ico_ℤ l u) sorry
@[simp] theorem Ico_ℤ_card (l : ℤ) (u : ℤ) : fintype.card ↥(Ico l u) = int.to_nat (u - l) :=
Eq.trans (fintype.card_of_finset (finset.Ico_ℤ l u) (Ico_ℤ_fintype._proof_1 l u))
(finset.Ico_ℤ.card l u)
end Mathlib |
a776134c00bf30253c70ea239bd065857c67257f | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/measure_theory/integral/bochner.lean | 3cc528844a562d21ac750693abdf7dd520abc858 | [
"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 | 79,497 | lean | /-
Copyright (c) 2019 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou, Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne
-/
import measure_theory.integral.set_to_l1
import analysis.normed_space.bounded_linear_maps
import topology.sequences
/-!
# Bochner integral
The Bochner integral extends the definition of the Lebesgue integral to functions that map from a
measure space into a Banach space (complete normed vector space). It is constructed here by
extending the integral on simple functions.
## Main definitions
The Bochner integral is defined through the extension process described in the file `set_to_L1`,
which follows these steps:
1. Define the integral of the indicator of a set. This is `weighted_smul μ s x = (μ s).to_real * x`.
`weighted_smul μ` is shown to be linear in the value `x` and `dominated_fin_meas_additive`
(defined in the file `set_to_L1`) with respect to the set `s`.
2. Define the integral on simple functions of the type `simple_func α E` (notation : `α →ₛ E`)
where `E` is a real normed space. (See `simple_func.integral` for details.)
3. Transfer this definition to define the integral on `L1.simple_func α E` (notation :
`α →₁ₛ[μ] E`), see `L1.simple_func.integral`. Show that this integral is a continuous linear
map from `α →₁ₛ[μ] E` to `E`.
4. Define the Bochner integral on L1 functions by extending the integral on integrable simple
functions `α →₁ₛ[μ] E` using `continuous_linear_map.extend` and the fact that the embedding of
`α →₁ₛ[μ] E` into `α →₁[μ] E` is dense.
5. Define the Bochner integral on functions as the Bochner integral of its equivalence class in L1
space, if it is in L1, and 0 otherwise.
The result of that construction is `∫ a, f a ∂μ`, which is definitionally equal to
`set_to_fun (dominated_fin_meas_additive_weighted_smul μ) f`. Some basic properties of the integral
(like linearity) are particular cases of the properties of `set_to_fun` (which are described in the
file `set_to_L1`).
## Main statements
1. Basic properties of the Bochner integral on functions of type `α → E`, where `α` is a measure
space and `E` is a real normed space.
* `integral_zero` : `∫ 0 ∂μ = 0`
* `integral_add` : `∫ x, f x + g x ∂μ = ∫ x, f ∂μ + ∫ x, g x ∂μ`
* `integral_neg` : `∫ x, - f x ∂μ = - ∫ x, f x ∂μ`
* `integral_sub` : `∫ x, f x - g x ∂μ = ∫ x, f x ∂μ - ∫ x, g x ∂μ`
* `integral_smul` : `∫ x, r • f x ∂μ = r • ∫ x, f x ∂μ`
* `integral_congr_ae` : `f =ᵐ[μ] g → ∫ x, f x ∂μ = ∫ x, g x ∂μ`
* `norm_integral_le_integral_norm` : `∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ`
2. Basic properties of the Bochner integral on functions of type `α → ℝ`, where `α` is a measure
space.
* `integral_nonneg_of_ae` : `0 ≤ᵐ[μ] f → 0 ≤ ∫ x, f x ∂μ`
* `integral_nonpos_of_ae` : `f ≤ᵐ[μ] 0 → ∫ x, f x ∂μ ≤ 0`
* `integral_mono_ae` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ`
* `integral_nonneg` : `0 ≤ f → 0 ≤ ∫ x, f x ∂μ`
* `integral_nonpos` : `f ≤ 0 → ∫ x, f x ∂μ ≤ 0`
* `integral_mono` : `f ≤ᵐ[μ] g → ∫ x, f x ∂μ ≤ ∫ x, g x ∂μ`
3. Propositions connecting the Bochner integral with the integral on `ℝ≥0∞`-valued functions,
which is called `lintegral` and has the notation `∫⁻`.
* `integral_eq_lintegral_max_sub_lintegral_min` : `∫ x, f x ∂μ = ∫⁻ x, f⁺ x ∂μ - ∫⁻ x, f⁻ x ∂μ`,
where `f⁺` is the positive part of `f` and `f⁻` is the negative part of `f`.
* `integral_eq_lintegral_of_nonneg_ae` : `0 ≤ᵐ[μ] f → ∫ x, f x ∂μ = ∫⁻ x, f x ∂μ`
4. `tendsto_integral_of_dominated_convergence` : the Lebesgue dominated convergence theorem
5. (In the file `set_integral`) integration commutes with continuous linear maps.
* `continuous_linear_map.integral_comp_comm`
* `linear_isometry.integral_comp_comm`
## Notes
Some tips on how to prove a proposition if the API for the Bochner integral is not enough so that
you need to unfold the definition of the Bochner integral and go back to simple functions.
One method is to use the theorem `integrable.induction` in the file `simple_func_dense_lp` (or one
of the related results, like `Lp.induction` for functions in `Lp`), which allows you to prove
something for an arbitrary integrable function.
Another method is using the following steps.
See `integral_eq_lintegral_max_sub_lintegral_min` for a complicated example, which proves that
`∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, with the first integral sign being the Bochner integral of a real-valued
function `f : α → ℝ`, and second and third integral sign being the integral on `ℝ≥0∞`-valued
functions (called `lintegral`). The proof of `integral_eq_lintegral_max_sub_lintegral_min` is
scattered in sections with the name `pos_part`.
Here are the usual steps of proving that a property `p`, say `∫ f = ∫⁻ f⁺ - ∫⁻ f⁻`, holds for all
functions :
1. First go to the `L¹` space.
For example, if you see `ennreal.to_real (∫⁻ a, ennreal.of_real $ ∥f a∥)`, that is the norm of
`f` in `L¹` space. Rewrite using `L1.norm_of_fun_eq_lintegral_norm`.
2. Show that the set `{f ∈ L¹ | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}` is closed in `L¹` using `is_closed_eq`.
3. Show that the property holds for all simple functions `s` in `L¹` space.
Typically, you need to convert various notions to their `simple_func` counterpart, using lemmas
like `L1.integral_coe_eq_integral`.
4. Since simple functions are dense in `L¹`,
```
univ = closure {s simple}
= closure {s simple | ∫ s = ∫⁻ s⁺ - ∫⁻ s⁻} : the property holds for all simple functions
⊆ closure {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻}
= {f | ∫ f = ∫⁻ f⁺ - ∫⁻ f⁻} : closure of a closed set is itself
```
Use `is_closed_property` or `dense_range.induction_on` for this argument.
## Notations
* `α →ₛ E` : simple functions (defined in `measure_theory/integration`)
* `α →₁[μ] E` : functions in L1 space, i.e., equivalence classes of integrable functions (defined in
`measure_theory/lp_space`)
* `α →₁ₛ[μ] E` : simple functions in L1 space, i.e., equivalence classes of integrable simple
functions (defined in `measure_theory/simple_func_dense`)
* `∫ a, f a ∂μ` : integral of `f` with respect to a measure `μ`
* `∫ a, f a` : integral of `f` with respect to `volume`, the default measure on the ambient type
We also define notations for integral on a set, which are described in the file
`measure_theory/set_integral`.
Note : `ₛ` is typed using `\_s`. Sometimes it shows as a box if the font is missing.
## Tags
Bochner integral, simple function, function space, Lebesgue dominated convergence theorem
-/
noncomputable theory
open_locale topological_space big_operators nnreal ennreal measure_theory
open set filter topological_space ennreal emetric
namespace measure_theory
variables {α E F 𝕜 : Type*}
section weighted_smul
open continuous_linear_map
variables [normed_add_comm_group F] [normed_space ℝ F] {m : measurable_space α} {μ : measure α}
/-- Given a set `s`, return the continuous linear map `λ x, (μ s).to_real • x`. The extension of
that set function through `set_to_L1` gives the Bochner integral of L1 functions. -/
def weighted_smul {m : measurable_space α} (μ : measure α) (s : set α) : F →L[ℝ] F :=
(μ s).to_real • (continuous_linear_map.id ℝ F)
lemma weighted_smul_apply {m : measurable_space α} (μ : measure α) (s : set α) (x : F) :
weighted_smul μ s x = (μ s).to_real • x :=
by simp [weighted_smul]
@[simp] lemma weighted_smul_zero_measure {m : measurable_space α} :
weighted_smul (0 : measure α) = (0 : set α → F →L[ℝ] F) :=
by { ext1, simp [weighted_smul], }
@[simp] lemma weighted_smul_empty {m : measurable_space α} (μ : measure α) :
weighted_smul μ ∅ = (0 : F →L[ℝ] F) :=
by { ext1 x, rw [weighted_smul_apply], simp, }
lemma weighted_smul_add_measure {m : measurable_space α} (μ ν : measure α) {s : set α}
(hμs : μ s ≠ ∞) (hνs : ν s ≠ ∞) :
(weighted_smul (μ + ν) s : F →L[ℝ] F) = weighted_smul μ s + weighted_smul ν s :=
begin
ext1 x,
push_cast,
simp_rw [pi.add_apply, weighted_smul_apply],
push_cast,
rw [pi.add_apply, ennreal.to_real_add hμs hνs, add_smul],
end
lemma weighted_smul_smul_measure {m : measurable_space α} (μ : measure α) (c : ℝ≥0∞) {s : set α} :
(weighted_smul (c • μ) s : F →L[ℝ] F) = c.to_real • weighted_smul μ s :=
begin
ext1 x,
push_cast,
simp_rw [pi.smul_apply, weighted_smul_apply],
push_cast,
simp_rw [pi.smul_apply, smul_eq_mul, to_real_mul, smul_smul],
end
lemma weighted_smul_congr (s t : set α) (hst : μ s = μ t) :
(weighted_smul μ s : F →L[ℝ] F) = weighted_smul μ t :=
by { ext1 x, simp_rw weighted_smul_apply, congr' 2, }
lemma weighted_smul_null {s : set α} (h_zero : μ s = 0) : (weighted_smul μ s : F →L[ℝ] F) = 0 :=
by { ext1 x, rw [weighted_smul_apply, h_zero], simp, }
lemma weighted_smul_union' (s t : set α) (ht : measurable_set t)
(hs_finite : μ s ≠ ∞) (ht_finite : μ t ≠ ∞) (h_inter : s ∩ t = ∅) :
(weighted_smul μ (s ∪ t) : F →L[ℝ] F) = weighted_smul μ s + weighted_smul μ t :=
begin
ext1 x,
simp_rw [add_apply, weighted_smul_apply,
measure_union (set.disjoint_iff_inter_eq_empty.mpr h_inter) ht,
ennreal.to_real_add hs_finite ht_finite, add_smul],
end
@[nolint unused_arguments]
lemma weighted_smul_union (s t : set α) (hs : measurable_set s) (ht : measurable_set t)
(hs_finite : μ s ≠ ∞) (ht_finite : μ t ≠ ∞) (h_inter : s ∩ t = ∅) :
(weighted_smul μ (s ∪ t) : F →L[ℝ] F) = weighted_smul μ s + weighted_smul μ t :=
weighted_smul_union' s t ht hs_finite ht_finite h_inter
lemma weighted_smul_smul [normed_field 𝕜] [normed_space 𝕜 F] [smul_comm_class ℝ 𝕜 F]
(c : 𝕜) (s : set α) (x : F) :
weighted_smul μ s (c • x) = c • weighted_smul μ s x :=
by { simp_rw [weighted_smul_apply, smul_comm], }
lemma norm_weighted_smul_le (s : set α) : ∥(weighted_smul μ s : F →L[ℝ] F)∥ ≤ (μ s).to_real :=
calc ∥(weighted_smul μ s : F →L[ℝ] F)∥ = ∥(μ s).to_real∥ * ∥continuous_linear_map.id ℝ F∥ :
norm_smul _ _
... ≤ ∥(μ s).to_real∥ : (mul_le_mul_of_nonneg_left norm_id_le (norm_nonneg _)).trans (mul_one _).le
... = abs (μ s).to_real : real.norm_eq_abs _
... = (μ s).to_real : abs_eq_self.mpr ennreal.to_real_nonneg
lemma dominated_fin_meas_additive_weighted_smul {m : measurable_space α} (μ : measure α) :
dominated_fin_meas_additive μ (weighted_smul μ : set α → F →L[ℝ] F) 1 :=
⟨weighted_smul_union, λ s _ _, (norm_weighted_smul_le s).trans (one_mul _).symm.le⟩
lemma weighted_smul_nonneg (s : set α) (x : ℝ) (hx : 0 ≤ x) : 0 ≤ weighted_smul μ s x :=
begin
simp only [weighted_smul, algebra.id.smul_eq_mul, coe_smul', id.def, coe_id', pi.smul_apply],
exact mul_nonneg to_real_nonneg hx,
end
end weighted_smul
local infixr ` →ₛ `:25 := simple_func
namespace simple_func
section pos_part
variables [linear_order E] [has_zero E] [measurable_space α]
/-- Positive part of a simple function. -/
def pos_part (f : α →ₛ E) : α →ₛ E := f.map (λ b, max b 0)
/-- Negative part of a simple function. -/
def neg_part [has_neg E] (f : α →ₛ E) : α →ₛ E := pos_part (-f)
lemma pos_part_map_norm (f : α →ₛ ℝ) : (pos_part f).map norm = pos_part f :=
by { ext, rw [map_apply, real.norm_eq_abs, abs_of_nonneg], exact le_max_right _ _ }
lemma neg_part_map_norm (f : α →ₛ ℝ) : (neg_part f).map norm = neg_part f :=
by { rw neg_part, exact pos_part_map_norm _ }
lemma pos_part_sub_neg_part (f : α →ₛ ℝ) : f.pos_part - f.neg_part = f :=
begin
simp only [pos_part, neg_part],
ext a,
rw coe_sub,
exact max_zero_sub_eq_self (f a)
end
end pos_part
section integral
/-!
### The Bochner integral of simple functions
Define the Bochner integral of simple functions of the type `α →ₛ β` where `β` is a normed group,
and prove basic property of this integral.
-/
open finset
variables [normed_add_comm_group E] [normed_add_comm_group F] [normed_space ℝ F] {p : ℝ≥0∞}
{G F' : Type*} [normed_add_comm_group G] [normed_add_comm_group F'] [normed_space ℝ F']
{m : measurable_space α} {μ : measure α}
/-- Bochner integral of simple functions whose codomain is a real `normed_space`.
This is equal to `∑ x in f.range, (μ (f ⁻¹' {x})).to_real • x` (see `integral_eq`). -/
def integral {m : measurable_space α} (μ : measure α) (f : α →ₛ F) : F :=
f.set_to_simple_func (weighted_smul μ)
lemma integral_def {m : measurable_space α} (μ : measure α) (f : α →ₛ F) :
f.integral μ = f.set_to_simple_func (weighted_smul μ) := rfl
lemma integral_eq {m : measurable_space α} (μ : measure α) (f : α →ₛ F) :
f.integral μ = ∑ x in f.range, (μ (f ⁻¹' {x})).to_real • x :=
by simp [integral, set_to_simple_func, weighted_smul_apply]
lemma integral_eq_sum_filter [decidable_pred (λ x : F, x ≠ 0)] {m : measurable_space α} (f : α →ₛ F)
(μ : measure α) :
f.integral μ = ∑ x in f.range.filter (λ x, x ≠ 0), (μ (f ⁻¹' {x})).to_real • x :=
by { rw [integral_def, set_to_simple_func_eq_sum_filter], simp_rw weighted_smul_apply, congr }
/-- The Bochner integral is equal to a sum over any set that includes `f.range` (except `0`). -/
lemma integral_eq_sum_of_subset [decidable_pred (λ x : F, x ≠ 0)] {f : α →ₛ F} {s : finset F}
(hs : f.range.filter (λ x, x ≠ 0) ⊆ s) : f.integral μ = ∑ x in s, (μ (f ⁻¹' {x})).to_real • x :=
begin
rw [simple_func.integral_eq_sum_filter, finset.sum_subset hs],
rintro x - hx, rw [finset.mem_filter, not_and_distrib, ne.def, not_not] at hx,
rcases hx with hx|rfl; [skip, simp],
rw [simple_func.mem_range] at hx, rw [preimage_eq_empty]; simp [set.disjoint_singleton_left, hx]
end
@[simp] lemma integral_const {m : measurable_space α} (μ : measure α) (y : F) :
(const α y).integral μ = (μ univ).to_real • y :=
by classical; calc (const α y).integral μ = ∑ z in {y}, (μ ((const α y) ⁻¹' {z})).to_real • z :
integral_eq_sum_of_subset $ (filter_subset _ _).trans (range_const_subset _ _)
... = (μ univ).to_real • y : by simp
@[simp] lemma integral_piecewise_zero {m : measurable_space α} (f : α →ₛ F) (μ : measure α)
{s : set α} (hs : measurable_set s) :
(piecewise s hs f 0).integral μ = f.integral (μ.restrict s) :=
begin
classical,
refine (integral_eq_sum_of_subset _).trans
((sum_congr rfl $ λ y hy, _).trans (integral_eq_sum_filter _ _).symm),
{ intros y hy,
simp only [mem_filter, mem_range, coe_piecewise, coe_zero, piecewise_eq_indicator,
mem_range_indicator] at *,
rcases hy with ⟨⟨rfl, -⟩|⟨x, hxs, rfl⟩, h₀⟩,
exacts [(h₀ rfl).elim, ⟨set.mem_range_self _, h₀⟩] },
{ dsimp,
rw [set.piecewise_eq_indicator, indicator_preimage_of_not_mem,
measure.restrict_apply (f.measurable_set_preimage _)],
exact λ h₀, (mem_filter.1 hy).2 (eq.symm h₀) }
end
/-- Calculate the integral of `g ∘ f : α →ₛ F`, where `f` is an integrable function from `α` to `E`
and `g` is a function from `E` to `F`. We require `g 0 = 0` so that `g ∘ f` is integrable. -/
lemma map_integral (f : α →ₛ E) (g : E → F) (hf : integrable f μ) (hg : g 0 = 0) :
(f.map g).integral μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • (g x) :=
map_set_to_simple_func _ weighted_smul_union hf hg
/-- `simple_func.integral` and `simple_func.lintegral` agree when the integrand has type
`α →ₛ ℝ≥0∞`. But since `ℝ≥0∞` is not a `normed_space`, we need some form of coercion.
See `integral_eq_lintegral` for a simpler version. -/
lemma integral_eq_lintegral' {f : α →ₛ E} {g : E → ℝ≥0∞} (hf : integrable f μ) (hg0 : g 0 = 0)
(ht : ∀ b, g b ≠ ∞) :
(f.map (ennreal.to_real ∘ g)).integral μ = ennreal.to_real (∫⁻ a, g (f a) ∂μ) :=
begin
have hf' : f.fin_meas_supp μ := integrable_iff_fin_meas_supp.1 hf,
simp only [← map_apply g f, lintegral_eq_lintegral],
rw [map_integral f _ hf, map_lintegral, ennreal.to_real_sum],
{ refine finset.sum_congr rfl (λb hb, _),
rw [smul_eq_mul, to_real_mul, mul_comm] },
{ assume a ha,
by_cases a0 : a = 0,
{ rw [a0, hg0, zero_mul], exact with_top.zero_ne_top },
{ apply mul_ne_top (ht a) (hf'.meas_preimage_singleton_ne_zero a0).ne } },
{ simp [hg0] }
end
variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [smul_comm_class ℝ 𝕜 E]
lemma integral_congr {f g : α →ₛ E} (hf : integrable f μ) (h : f =ᵐ[μ] g) :
f.integral μ = g.integral μ :=
set_to_simple_func_congr (weighted_smul μ) (λ s hs, weighted_smul_null) weighted_smul_union hf h
/-- `simple_func.bintegral` and `simple_func.integral` agree when the integrand has type
`α →ₛ ℝ≥0∞`. But since `ℝ≥0∞` is not a `normed_space`, we need some form of coercion. -/
lemma integral_eq_lintegral {f : α →ₛ ℝ} (hf : integrable f μ) (h_pos : 0 ≤ᵐ[μ] f) :
f.integral μ = ennreal.to_real (∫⁻ a, ennreal.of_real (f a) ∂μ) :=
begin
have : f =ᵐ[μ] f.map (ennreal.to_real ∘ ennreal.of_real) :=
h_pos.mono (λ a h, (ennreal.to_real_of_real h).symm),
rw [← integral_eq_lintegral' hf],
exacts [integral_congr hf this, ennreal.of_real_zero, λ b, ennreal.of_real_ne_top]
end
lemma integral_add {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) :
integral μ (f + g) = integral μ f + integral μ g :=
set_to_simple_func_add _ weighted_smul_union hf hg
lemma integral_neg {f : α →ₛ E} (hf : integrable f μ) : integral μ (-f) = - integral μ f :=
set_to_simple_func_neg _ weighted_smul_union hf
lemma integral_sub {f g : α →ₛ E} (hf : integrable f μ) (hg : integrable g μ) :
integral μ (f - g) = integral μ f - integral μ g :=
set_to_simple_func_sub _ weighted_smul_union hf hg
lemma integral_smul (c : 𝕜) {f : α →ₛ E} (hf : integrable f μ) :
integral μ (c • f) = c • integral μ f :=
set_to_simple_func_smul _ weighted_smul_union weighted_smul_smul c hf
lemma norm_set_to_simple_func_le_integral_norm (T : set α → E →L[ℝ] F) {C : ℝ}
(hT_norm : ∀ s, measurable_set s → μ s < ∞ → ∥T s∥ ≤ C * (μ s).to_real) {f : α →ₛ E}
(hf : integrable f μ) :
∥f.set_to_simple_func T∥ ≤ C * (f.map norm).integral μ :=
calc ∥f.set_to_simple_func T∥
≤ C * ∑ x in f.range, ennreal.to_real (μ (f ⁻¹' {x})) * ∥x∥ :
norm_set_to_simple_func_le_sum_mul_norm_of_integrable T hT_norm f hf
... = C * (f.map norm).integral μ : by { rw map_integral f norm hf norm_zero, simp_rw smul_eq_mul, }
lemma norm_integral_le_integral_norm (f : α →ₛ E) (hf : integrable f μ) :
∥f.integral μ∥ ≤ (f.map norm).integral μ :=
begin
refine (norm_set_to_simple_func_le_integral_norm _ (λ s _ _, _) hf).trans (one_mul _).le,
exact (norm_weighted_smul_le s).trans (one_mul _).symm.le,
end
lemma integral_add_measure {ν} (f : α →ₛ E) (hf : integrable f (μ + ν)) :
f.integral (μ + ν) = f.integral μ + f.integral ν :=
begin
simp_rw [integral_def],
refine set_to_simple_func_add_left' (weighted_smul μ) (weighted_smul ν) (weighted_smul (μ + ν))
(λ s hs hμνs, _) hf,
rw [lt_top_iff_ne_top, measure.coe_add, pi.add_apply, ennreal.add_ne_top] at hμνs,
rw weighted_smul_add_measure _ _ hμνs.1 hμνs.2,
end
end integral
end simple_func
namespace L1
open ae_eq_fun Lp.simple_func Lp
variables [normed_add_comm_group E] [normed_add_comm_group F] {m : measurable_space α}
{μ : measure α}
variables {α E μ}
namespace simple_func
lemma norm_eq_integral (f : α →₁ₛ[μ] E) : ∥f∥ = ((to_simple_func f).map norm).integral μ :=
begin
rw [norm_eq_sum_mul f, (to_simple_func f).map_integral norm (simple_func.integrable f) norm_zero],
simp_rw smul_eq_mul,
end
section pos_part
/-- Positive part of a simple function in L1 space. -/
def pos_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := ⟨Lp.pos_part (f : α →₁[μ] ℝ),
begin
rcases f with ⟨f, s, hsf⟩,
use s.pos_part,
simp only [subtype.coe_mk, Lp.coe_pos_part, ← hsf, ae_eq_fun.pos_part_mk, simple_func.pos_part,
simple_func.coe_map, mk_eq_mk],
end ⟩
/-- Negative part of a simple function in L1 space. -/
def neg_part (f : α →₁ₛ[μ] ℝ) : α →₁ₛ[μ] ℝ := pos_part (-f)
@[norm_cast]
lemma coe_pos_part (f : α →₁ₛ[μ] ℝ) : (pos_part f : α →₁[μ] ℝ) = Lp.pos_part (f : α →₁[μ] ℝ) := rfl
@[norm_cast]
lemma coe_neg_part (f : α →₁ₛ[μ] ℝ) : (neg_part f : α →₁[μ] ℝ) = Lp.neg_part (f : α →₁[μ] ℝ) := rfl
end pos_part
section simple_func_integral
/-!
### The Bochner integral of `L1`
Define the Bochner integral on `α →₁ₛ[μ] E` by extension from the simple functions `α →₁ₛ[μ] E`,
and prove basic properties of this integral. -/
variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space ℝ E] [smul_comm_class ℝ 𝕜 E]
{F' : Type*} [normed_add_comm_group F'] [normed_space ℝ F']
local attribute [instance] simple_func.normed_space
/-- The Bochner integral over simple functions in L1 space. -/
def integral (f : α →₁ₛ[μ] E) : E := ((to_simple_func f)).integral μ
lemma integral_eq_integral (f : α →₁ₛ[μ] E) : integral f = ((to_simple_func f)).integral μ := rfl
lemma integral_eq_lintegral {f : α →₁ₛ[μ] ℝ} (h_pos : 0 ≤ᵐ[μ] (to_simple_func f)) :
integral f = ennreal.to_real (∫⁻ a, ennreal.of_real ((to_simple_func f) a) ∂μ) :=
by rw [integral, simple_func.integral_eq_lintegral (simple_func.integrable f) h_pos]
lemma integral_eq_set_to_L1s (f : α →₁ₛ[μ] E) : integral f = set_to_L1s (weighted_smul μ) f := rfl
lemma integral_congr {f g : α →₁ₛ[μ] E} (h : to_simple_func f =ᵐ[μ] to_simple_func g) :
integral f = integral g :=
simple_func.integral_congr (simple_func.integrable f) h
lemma integral_add (f g : α →₁ₛ[μ] E) : integral (f + g) = integral f + integral g :=
set_to_L1s_add _ (λ _ _, weighted_smul_null) weighted_smul_union _ _
lemma integral_smul (c : 𝕜) (f : α →₁ₛ[μ] E) :
integral (c • f) = c • integral f :=
set_to_L1s_smul _ (λ _ _, weighted_smul_null) weighted_smul_union weighted_smul_smul c f
lemma norm_integral_le_norm (f : α →₁ₛ[μ] E) : ∥integral f∥ ≤ ∥f∥ :=
begin
rw [integral, norm_eq_integral],
exact (to_simple_func f).norm_integral_le_integral_norm (simple_func.integrable f)
end
variables {E' : Type*} [normed_add_comm_group E'] [normed_space ℝ E'] [normed_space 𝕜 E']
variables (α E μ 𝕜)
/-- The Bochner integral over simple functions in L1 space as a continuous linear map. -/
def integral_clm' : (α →₁ₛ[μ] E) →L[𝕜] E :=
linear_map.mk_continuous ⟨integral, integral_add, integral_smul⟩
1 (λf, le_trans (norm_integral_le_norm _) $ by rw one_mul)
/-- The Bochner integral over simple functions in L1 space as a continuous linear map over ℝ. -/
def integral_clm : (α →₁ₛ[μ] E) →L[ℝ] E := integral_clm' α E ℝ μ
variables {α E μ 𝕜}
local notation (name := simple_func.integral_clm) `Integral` := integral_clm α E μ
open continuous_linear_map
lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 :=
linear_map.mk_continuous_norm_le _ (zero_le_one) _
section pos_part
lemma pos_part_to_simple_func (f : α →₁ₛ[μ] ℝ) :
to_simple_func (pos_part f) =ᵐ[μ] (to_simple_func f).pos_part :=
begin
have eq : ∀ a, (to_simple_func f).pos_part a = max ((to_simple_func f) a) 0 := λa, rfl,
have ae_eq : ∀ᵐ a ∂μ, to_simple_func (pos_part f) a = max ((to_simple_func f) a) 0,
{ filter_upwards [to_simple_func_eq_to_fun (pos_part f), Lp.coe_fn_pos_part (f : α →₁[μ] ℝ),
to_simple_func_eq_to_fun f] with _ _ h₂ _,
convert h₂, },
refine ae_eq.mono (assume a h, _),
rw [h, eq],
end
lemma neg_part_to_simple_func (f : α →₁ₛ[μ] ℝ) :
to_simple_func (neg_part f) =ᵐ[μ] (to_simple_func f).neg_part :=
begin
rw [simple_func.neg_part, measure_theory.simple_func.neg_part],
filter_upwards [pos_part_to_simple_func (-f), neg_to_simple_func f],
assume a h₁ h₂,
rw h₁,
show max _ _ = max _ _,
rw h₂,
refl
end
lemma integral_eq_norm_pos_part_sub (f : α →₁ₛ[μ] ℝ) :
integral f = ∥pos_part f∥ - ∥neg_part f∥ :=
begin
-- Convert things in `L¹` to their `simple_func` counterpart
have ae_eq₁ : (to_simple_func f).pos_part =ᵐ[μ] (to_simple_func (pos_part f)).map norm,
{ filter_upwards [pos_part_to_simple_func f] with _ h,
rw [simple_func.map_apply, h],
conv_lhs { rw [← simple_func.pos_part_map_norm, simple_func.map_apply] } },
-- Convert things in `L¹` to their `simple_func` counterpart
have ae_eq₂ : (to_simple_func f).neg_part =ᵐ[μ] (to_simple_func (neg_part f)).map norm,
{ filter_upwards [neg_part_to_simple_func f] with _ h,
rw [simple_func.map_apply, h],
conv_lhs { rw [← simple_func.neg_part_map_norm, simple_func.map_apply], }, },
-- Convert things in `L¹` to their `simple_func` counterpart
have ae_eq : ∀ᵐ a ∂μ, (to_simple_func f).pos_part a - (to_simple_func f).neg_part a =
(to_simple_func (pos_part f)).map norm a - (to_simple_func (neg_part f)).map norm a,
{ filter_upwards [ae_eq₁, ae_eq₂] with _ h₁ h₂,
rw [h₁, h₂], },
rw [integral, norm_eq_integral, norm_eq_integral, ← simple_func.integral_sub],
{ show (to_simple_func f).integral μ =
((to_simple_func (pos_part f)).map norm - (to_simple_func (neg_part f)).map norm).integral μ,
apply measure_theory.simple_func.integral_congr (simple_func.integrable f),
filter_upwards [ae_eq₁, ae_eq₂] with _ h₁ h₂,
show _ = _ - _,
rw [← h₁, ← h₂],
have := (to_simple_func f).pos_part_sub_neg_part,
conv_lhs {rw ← this},
refl, },
{ exact (simple_func.integrable f).pos_part.congr ae_eq₁ },
{ exact (simple_func.integrable f).neg_part.congr ae_eq₂ }
end
end pos_part
end simple_func_integral
end simple_func
open simple_func
local notation (name := simple_func.integral_clm) `Integral` := @integral_clm α E _ _ _ _ _ μ _
variables [normed_space ℝ E] [nontrivially_normed_field 𝕜] [normed_space 𝕜 E]
[smul_comm_class ℝ 𝕜 E] [normed_space ℝ F] [complete_space E]
section integration_in_L1
local attribute [instance] simple_func.normed_space
open continuous_linear_map
variables (𝕜)
/-- The Bochner integral in L1 space as a continuous linear map. -/
def integral_clm' : (α →₁[μ] E) →L[𝕜] E :=
(integral_clm' α E 𝕜 μ).extend
(coe_to_Lp α E 𝕜) (simple_func.dense_range one_ne_top) simple_func.uniform_inducing
variables {𝕜}
/-- The Bochner integral in L1 space as a continuous linear map over ℝ. -/
def integral_clm : (α →₁[μ] E) →L[ℝ] E := integral_clm' ℝ
/-- The Bochner integral in L1 space -/
def integral (f : α →₁[μ] E) : E := integral_clm f
lemma integral_eq (f : α →₁[μ] E) : integral f = integral_clm f := rfl
lemma integral_eq_set_to_L1 (f : α →₁[μ] E) :
integral f = set_to_L1 (dominated_fin_meas_additive_weighted_smul μ) f :=
rfl
@[norm_cast] lemma simple_func.integral_L1_eq_integral (f : α →₁ₛ[μ] E) :
integral (f : α →₁[μ] E) = (simple_func.integral f) :=
set_to_L1_eq_set_to_L1s_clm (dominated_fin_meas_additive_weighted_smul μ) f
variables (α E)
@[simp] lemma integral_zero : integral (0 : α →₁[μ] E) = 0 :=
map_zero integral_clm
variables {α E}
lemma integral_add (f g : α →₁[μ] E) : integral (f + g) = integral f + integral g :=
map_add integral_clm f g
lemma integral_neg (f : α →₁[μ] E) : integral (-f) = - integral f :=
map_neg integral_clm f
lemma integral_sub (f g : α →₁[μ] E) : integral (f - g) = integral f - integral g :=
map_sub integral_clm f g
lemma integral_smul (c : 𝕜) (f : α →₁[μ] E) : integral (c • f) = c • integral f :=
show (integral_clm' 𝕜) (c • f) = c • (integral_clm' 𝕜) f, from map_smul (integral_clm' 𝕜) c f
local notation (name := integral_clm) `Integral` := @integral_clm α E _ _ μ _ _
local notation (name := simple_func.integral_clm') `sIntegral` :=
@simple_func.integral_clm α E _ _ μ _
lemma norm_Integral_le_one : ∥Integral∥ ≤ 1 :=
norm_set_to_L1_le (dominated_fin_meas_additive_weighted_smul μ) zero_le_one
lemma norm_integral_le (f : α →₁[μ] E) : ∥integral f∥ ≤ ∥f∥ :=
calc ∥integral f∥ = ∥Integral f∥ : rfl
... ≤ ∥Integral∥ * ∥f∥ : le_op_norm _ _
... ≤ 1 * ∥f∥ : mul_le_mul_of_nonneg_right norm_Integral_le_one $ norm_nonneg _
... = ∥f∥ : one_mul _
@[continuity]
lemma continuous_integral : continuous (λ (f : α →₁[μ] E), integral f) :=
L1.integral_clm.continuous
section pos_part
lemma integral_eq_norm_pos_part_sub (f : α →₁[μ] ℝ) :
integral f = ∥Lp.pos_part f∥ - ∥Lp.neg_part f∥ :=
begin
-- Use `is_closed_property` and `is_closed_eq`
refine @is_closed_property _ _ _ (coe : (α →₁ₛ[μ] ℝ) → (α →₁[μ] ℝ))
(λ f : α →₁[μ] ℝ, integral f = ∥Lp.pos_part f∥ - ∥Lp.neg_part f∥)
(simple_func.dense_range one_ne_top) (is_closed_eq _ _) _ f,
{ exact cont _ },
{ refine continuous.sub (continuous_norm.comp Lp.continuous_pos_part)
(continuous_norm.comp Lp.continuous_neg_part) },
-- Show that the property holds for all simple functions in the `L¹` space.
{ assume s,
norm_cast,
exact simple_func.integral_eq_norm_pos_part_sub _ }
end
end pos_part
end integration_in_L1
end L1
/-!
## The Bochner integral on functions
Define the Bochner integral on functions generally to be the `L1` Bochner integral, for integrable
functions, and 0 otherwise; prove its basic properties.
-/
variables [normed_add_comm_group E] [normed_space ℝ E] [complete_space E]
[nontrivially_normed_field 𝕜] [normed_space 𝕜 E] [smul_comm_class ℝ 𝕜 E]
[normed_add_comm_group F] [normed_space ℝ F] [complete_space F]
section
open_locale classical
/-- The Bochner integral -/
def integral {m : measurable_space α} (μ : measure α) (f : α → E) : E :=
if hf : integrable f μ then L1.integral (hf.to_L1 f) else 0
end
/-! In the notation for integrals, an expression like `∫ x, g ∥x∥ ∂μ` will not be parsed correctly,
and needs parentheses. We do not set the binding power of `r` to `0`, because then
`∫ x, f x = 0` will be parsed incorrectly. -/
notation `∫` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral μ r
notation `∫` binders `, ` r:(scoped:60 f, integral volume f) := r
notation `∫` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := integral (measure.restrict μ s) r
notation `∫` binders ` in ` s `, ` r:(scoped:60 f, integral (measure.restrict volume s) f) := r
section properties
open continuous_linear_map measure_theory.simple_func
variables {f g : α → E} {m : measurable_space α} {μ : measure α}
lemma integral_eq (f : α → E) (hf : integrable f μ) :
∫ a, f a ∂μ = L1.integral (hf.to_L1 f) :=
@dif_pos _ (id _) hf _ _ _
lemma integral_eq_set_to_fun (f : α → E) :
∫ a, f a ∂μ = set_to_fun μ (weighted_smul μ) (dominated_fin_meas_additive_weighted_smul μ) f :=
rfl
lemma L1.integral_eq_integral (f : α →₁[μ] E) : L1.integral f = ∫ a, f a ∂μ :=
(L1.set_to_fun_eq_set_to_L1 (dominated_fin_meas_additive_weighted_smul μ) f).symm
lemma integral_undef (h : ¬ integrable f μ) : ∫ a, f a ∂μ = 0 :=
@dif_neg _ (id _) h _ _ _
lemma integral_non_ae_strongly_measurable (h : ¬ ae_strongly_measurable f μ) : ∫ a, f a ∂μ = 0 :=
integral_undef $ not_and_of_not_left _ h
variables (α E)
lemma integral_zero : ∫ a : α, (0:E) ∂μ = 0 :=
set_to_fun_zero (dominated_fin_meas_additive_weighted_smul μ)
@[simp] lemma integral_zero' : integral μ (0 : α → E) = 0 :=
integral_zero α E
variables {α E}
lemma integral_add (hf : integrable f μ) (hg : integrable g μ) :
∫ a, f a + g a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ :=
set_to_fun_add (dominated_fin_meas_additive_weighted_smul μ) hf hg
lemma integral_add' (hf : integrable f μ) (hg : integrable g μ) :
∫ a, (f + g) a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ :=
integral_add hf hg
lemma integral_finset_sum {ι} (s : finset ι) {f : ι → α → E} (hf : ∀ i ∈ s, integrable (f i) μ) :
∫ a, ∑ i in s, f i a ∂μ = ∑ i in s, ∫ a, f i a ∂μ :=
set_to_fun_finset_sum (dominated_fin_meas_additive_weighted_smul _) s hf
lemma integral_neg (f : α → E) : ∫ a, -f a ∂μ = - ∫ a, f a ∂μ :=
set_to_fun_neg (dominated_fin_meas_additive_weighted_smul μ) f
lemma integral_neg' (f : α → E) : ∫ a, (-f) a ∂μ = - ∫ a, f a ∂μ :=
integral_neg f
lemma integral_sub (hf : integrable f μ) (hg : integrable g μ) :
∫ a, f a - g a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ :=
set_to_fun_sub (dominated_fin_meas_additive_weighted_smul μ) hf hg
lemma integral_sub' (hf : integrable f μ) (hg : integrable g μ) :
∫ a, (f - g) a ∂μ = ∫ a, f a ∂μ - ∫ a, g a ∂μ :=
integral_sub hf hg
lemma integral_smul (c : 𝕜) (f : α → E) :
∫ a, c • (f a) ∂μ = c • ∫ a, f a ∂μ :=
set_to_fun_smul (dominated_fin_meas_additive_weighted_smul μ) weighted_smul_smul c f
lemma integral_mul_left (r : ℝ) (f : α → ℝ) : ∫ a, r * (f a) ∂μ = r * ∫ a, f a ∂μ :=
integral_smul r f
lemma integral_mul_right (r : ℝ) (f : α → ℝ) : ∫ a, (f a) * r ∂μ = ∫ a, f a ∂μ * r :=
by { simp only [mul_comm], exact integral_mul_left r f }
lemma integral_div (r : ℝ) (f : α → ℝ) : ∫ a, (f a) / r ∂μ = ∫ a, f a ∂μ / r :=
integral_mul_right r⁻¹ f
lemma integral_congr_ae (h : f =ᵐ[μ] g) : ∫ a, f a ∂μ = ∫ a, g a ∂μ :=
set_to_fun_congr_ae (dominated_fin_meas_additive_weighted_smul μ) h
@[simp] lemma L1.integral_of_fun_eq_integral {f : α → E} (hf : integrable f μ) :
∫ a, (hf.to_L1 f) a ∂μ = ∫ a, f a ∂μ :=
set_to_fun_to_L1 (dominated_fin_meas_additive_weighted_smul μ) hf
@[continuity]
lemma continuous_integral : continuous (λ (f : α →₁[μ] E), ∫ a, f a ∂μ) :=
continuous_set_to_fun (dominated_fin_meas_additive_weighted_smul μ)
lemma norm_integral_le_lintegral_norm (f : α → E) :
∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) :=
begin
by_cases hf : integrable f μ,
{ rw [integral_eq f hf, ← integrable.norm_to_L1_eq_lintegral_norm f hf],
exact L1.norm_integral_le _ },
{ rw [integral_undef hf, norm_zero], exact to_real_nonneg }
end
lemma ennnorm_integral_le_lintegral_ennnorm (f : α → E) :
(∥∫ a, f a ∂μ∥₊ : ℝ≥0∞) ≤ ∫⁻ a, ∥f a∥₊ ∂μ :=
by { simp_rw [← of_real_norm_eq_coe_nnnorm], apply ennreal.of_real_le_of_le_to_real,
exact norm_integral_le_lintegral_norm f }
lemma integral_eq_zero_of_ae {f : α → E} (hf : f =ᵐ[μ] 0) : ∫ a, f a ∂μ = 0 :=
by simp [integral_congr_ae hf, integral_zero]
/-- If `f` has finite integral, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends
to zero as `μ s` tends to zero. -/
lemma has_finite_integral.tendsto_set_integral_nhds_zero {ι} {f : α → E}
(hf : has_finite_integral f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) :
tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) :=
begin
rw [tendsto_zero_iff_norm_tendsto_zero],
simp_rw [← coe_nnnorm, ← nnreal.coe_zero, nnreal.tendsto_coe, ← ennreal.tendsto_coe,
ennreal.coe_zero],
exact tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds
(tendsto_set_lintegral_zero (ne_of_lt hf) hs) (λ i, zero_le _)
(λ i, ennnorm_integral_le_lintegral_ennnorm _)
end
/-- If `f` is integrable, then `∫ x in s, f x ∂μ` is absolutely continuous in `s`: it tends
to zero as `μ s` tends to zero. -/
lemma integrable.tendsto_set_integral_nhds_zero {ι} {f : α → E}
(hf : integrable f μ) {l : filter ι} {s : ι → set α} (hs : tendsto (μ ∘ s) l (𝓝 0)) :
tendsto (λ i, ∫ x in s i, f x ∂μ) l (𝓝 0) :=
hf.2.tendsto_set_integral_nhds_zero hs
/-- If `F i → f` in `L1`, then `∫ x, F i x ∂μ → ∫ x, f x ∂μ`. -/
lemma tendsto_integral_of_L1 {ι} (f : α → E) (hfi : integrable f μ)
{F : ι → α → E} {l : filter ι} (hFi : ∀ᶠ i in l, integrable (F i) μ)
(hF : tendsto (λ i, ∫⁻ x, ∥F i x - f x∥₊ ∂μ) l (𝓝 0)) :
tendsto (λ i, ∫ x, F i x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) :=
tendsto_set_to_fun_of_L1 (dominated_fin_meas_additive_weighted_smul μ) f hfi hFi hF
/-- Lebesgue dominated convergence theorem provides sufficient conditions under which almost
everywhere convergence of a sequence of functions implies the convergence of their integrals.
We could weaken the condition `bound_integrable` to require `has_finite_integral bound μ` instead
(i.e. not requiring that `bound` is measurable), but in all applications proving integrability
is easier. -/
theorem tendsto_integral_of_dominated_convergence {F : ℕ → α → E} {f : α → E} (bound : α → ℝ)
(F_measurable : ∀ n, ae_strongly_measurable (F n) μ)
(bound_integrable : integrable bound μ)
(h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a)
(h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) :
tendsto (λn, ∫ a, F n a ∂μ) at_top (𝓝 $ ∫ a, f a ∂μ) :=
tendsto_set_to_fun_of_dominated_convergence (dominated_fin_meas_additive_weighted_smul μ) bound
F_measurable bound_integrable h_bound h_lim
/-- Lebesgue dominated convergence theorem for filters with a countable basis -/
lemma tendsto_integral_filter_of_dominated_convergence {ι} {l : filter ι}
[l.is_countably_generated]
{F : ι → α → E} {f : α → E} (bound : α → ℝ)
(hF_meas : ∀ᶠ n in l, ae_strongly_measurable (F n) μ)
(h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a)
(bound_integrable : integrable bound μ)
(h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) :
tendsto (λn, ∫ a, F n a ∂μ) l (𝓝 $ ∫ a, f a ∂μ) :=
tendsto_set_to_fun_filter_of_dominated_convergence (dominated_fin_meas_additive_weighted_smul μ)
bound hF_meas h_bound bound_integrable h_lim
/-- Lebesgue dominated convergence theorem for series. -/
lemma has_sum_integral_of_dominated_convergence {ι} [countable ι]
{F : ι → α → E} {f : α → E} (bound : ι → α → ℝ)
(hF_meas : ∀ n, ae_strongly_measurable (F n) μ)
(h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound n a)
(bound_summable : ∀ᵐ a ∂μ, summable (λ n, bound n a))
(bound_integrable : integrable (λ a, ∑' n, bound n a) μ)
(h_lim : ∀ᵐ a ∂μ, has_sum (λ n, F n a) (f a)) :
has_sum (λn, ∫ a, F n a ∂μ) (∫ a, f a ∂μ) :=
begin
have hb_nonneg : ∀ᵐ a ∂μ, ∀ n, 0 ≤ bound n a :=
eventually_countable_forall.2 (λ n, (h_bound n).mono $ λ a, (norm_nonneg _).trans),
have hb_le_tsum : ∀ n, bound n ≤ᵐ[μ] (λ a, ∑' n, bound n a),
{ intro n,
filter_upwards [hb_nonneg, bound_summable] with _ ha0 ha_sum
using le_tsum ha_sum _ (λ i _, ha0 i) },
have hF_integrable : ∀ n, integrable (F n) μ,
{ refine λ n, bound_integrable.mono' (hF_meas n) _,
exact eventually_le.trans (h_bound n) (hb_le_tsum n) },
simp only [has_sum, ← integral_finset_sum _ (λ n _, hF_integrable n)],
refine tendsto_integral_filter_of_dominated_convergence (λ a, ∑' n, bound n a) _ _
bound_integrable h_lim,
{ exact eventually_of_forall (λ s, s.ae_strongly_measurable_sum $ λ n hn, hF_meas n) },
{ refine eventually_of_forall (λ s, _),
filter_upwards [eventually_countable_forall.2 h_bound, hb_nonneg, bound_summable]
with a hFa ha0 has,
calc ∥∑ n in s, F n a∥ ≤ ∑ n in s, bound n a : norm_sum_le_of_le _ (λ n hn, hFa n)
... ≤ ∑' n, bound n a : sum_le_tsum _ (λ n hn, ha0 n) has },
end
variables {X : Type*} [topological_space X] [first_countable_topology X]
lemma continuous_at_of_dominated {F : X → α → E} {x₀ : X} {bound : α → ℝ}
(hF_meas : ∀ᶠ x in 𝓝 x₀, ae_strongly_measurable (F x) μ)
(h_bound : ∀ᶠ x in 𝓝 x₀, ∀ᵐ a ∂μ, ∥F x a∥ ≤ bound a)
(bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous_at (λ x, F x a) x₀) :
continuous_at (λ x, ∫ a, F x a ∂μ) x₀ :=
continuous_at_set_to_fun_of_dominated (dominated_fin_meas_additive_weighted_smul μ) hF_meas h_bound
bound_integrable h_cont
lemma continuous_of_dominated {F : X → α → E} {bound : α → ℝ}
(hF_meas : ∀ x, ae_strongly_measurable (F x) μ) (h_bound : ∀ x, ∀ᵐ a ∂μ, ∥F x a∥ ≤ bound a)
(bound_integrable : integrable bound μ) (h_cont : ∀ᵐ a ∂μ, continuous (λ x, F x a)) :
continuous (λ x, ∫ a, F x a ∂μ) :=
continuous_set_to_fun_of_dominated (dominated_fin_meas_additive_weighted_smul μ) hF_meas h_bound
bound_integrable h_cont
/-- The Bochner integral of a real-valued function `f : α → ℝ` is the difference between the
integral of the positive part of `f` and the integral of the negative part of `f`. -/
lemma integral_eq_lintegral_pos_part_sub_lintegral_neg_part {f : α → ℝ} (hf : integrable f μ) :
∫ a, f a ∂μ =
ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) -
ennreal.to_real (∫⁻ a, (ennreal.of_real $ - f a) ∂μ) :=
let f₁ := hf.to_L1 f in
-- Go to the `L¹` space
have eq₁ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) = ∥Lp.pos_part f₁∥ :=
begin
rw L1.norm_def,
congr' 1,
apply lintegral_congr_ae,
filter_upwards [Lp.coe_fn_pos_part f₁, hf.coe_fn_to_L1] with _ h₁ h₂,
rw [h₁, h₂, ennreal.of_real],
congr' 1,
apply nnreal.eq,
rw real.nnnorm_of_nonneg (le_max_right _ _),
simp only [real.coe_to_nnreal', subtype.coe_mk],
end,
-- Go to the `L¹` space
have eq₂ : ennreal.to_real (∫⁻ a, (ennreal.of_real $ - f a) ∂μ) = ∥Lp.neg_part f₁∥ :=
begin
rw L1.norm_def,
congr' 1,
apply lintegral_congr_ae,
filter_upwards [Lp.coe_fn_neg_part f₁, hf.coe_fn_to_L1] with _ h₁ h₂,
rw [h₁, h₂, ennreal.of_real],
congr' 1,
apply nnreal.eq,
simp only [real.coe_to_nnreal', coe_nnnorm, nnnorm_neg],
rw [real.norm_of_nonpos (min_le_right _ _), ← max_neg_neg, neg_zero],
end,
begin
rw [eq₁, eq₂, integral, dif_pos],
exact L1.integral_eq_norm_pos_part_sub _
end
lemma integral_eq_lintegral_of_nonneg_ae
{f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfm : ae_strongly_measurable f μ) :
∫ a, f a ∂μ = ennreal.to_real (∫⁻ a, (ennreal.of_real $ f a) ∂μ) :=
begin
by_cases hfi : integrable f μ,
{ rw integral_eq_lintegral_pos_part_sub_lintegral_neg_part hfi,
have h_min : ∫⁻ a, ennreal.of_real (-f a) ∂μ = 0,
{ rw lintegral_eq_zero_iff',
{ refine hf.mono _,
simp only [pi.zero_apply],
assume a h,
simp only [h, neg_nonpos, of_real_eq_zero], },
{ exact measurable_of_real.comp_ae_measurable hfm.ae_measurable.neg } },
rw [h_min, zero_to_real, _root_.sub_zero] },
{ rw integral_undef hfi,
simp_rw [integrable, hfm, has_finite_integral_iff_norm, lt_top_iff_ne_top, ne.def, true_and,
not_not] at hfi,
have : ∫⁻ (a : α), ennreal.of_real (f a) ∂μ = ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ,
{ refine lintegral_congr_ae (hf.mono $ assume a h, _),
rw [real.norm_eq_abs, abs_of_nonneg h] },
rw [this, hfi], refl }
end
lemma integral_norm_eq_lintegral_nnnorm {G} [normed_add_comm_group G]
{f : α → G} (hf : ae_strongly_measurable f μ) :
∫ x, ∥f x∥ ∂μ = ennreal.to_real ∫⁻ x, ∥f x∥₊ ∂μ :=
begin
rw integral_eq_lintegral_of_nonneg_ae _ hf.norm,
{ simp_rw [of_real_norm_eq_coe_nnnorm], },
{ refine ae_of_all _ _, simp_rw [pi.zero_apply, norm_nonneg, imp_true_iff] },
end
lemma of_real_integral_norm_eq_lintegral_nnnorm {G} [normed_add_comm_group G] {f : α → G}
(hf : integrable f μ) :
ennreal.of_real ∫ x, ∥f x∥ ∂μ = ∫⁻ x, ∥f x∥₊ ∂μ :=
by rw [integral_norm_eq_lintegral_nnnorm hf.ae_strongly_measurable,
ennreal.of_real_to_real (lt_top_iff_ne_top.mp hf.2)]
lemma integral_eq_integral_pos_part_sub_integral_neg_part {f : α → ℝ} (hf : integrable f μ) :
∫ a, f a ∂μ = (∫ a, real.to_nnreal (f a) ∂μ) - (∫ a, real.to_nnreal (-f a) ∂μ) :=
begin
rw [← integral_sub hf.real_to_nnreal],
{ simp },
{ exact hf.neg.real_to_nnreal }
end
lemma integral_nonneg_of_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a, f a ∂μ :=
set_to_fun_nonneg (dominated_fin_meas_additive_weighted_smul μ)
(λ s _ _, weighted_smul_nonneg s) hf
lemma lintegral_coe_eq_integral (f : α → ℝ≥0) (hfi : integrable (λ x, (f x : ℝ)) μ) :
∫⁻ a, f a ∂μ = ennreal.of_real ∫ a, f a ∂μ :=
begin
simp_rw [integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (λ x, (f x).coe_nonneg))
hfi.ae_strongly_measurable, ← ennreal.coe_nnreal_eq], rw [ennreal.of_real_to_real],
rw [← lt_top_iff_ne_top], convert hfi.has_finite_integral, ext1 x, rw [nnreal.nnnorm_eq]
end
lemma of_real_integral_eq_lintegral_of_real {f : α → ℝ} (hfi : integrable f μ) (f_nn : 0 ≤ᵐ[μ] f) :
ennreal.of_real (∫ x, f x ∂μ) = ∫⁻ x, ennreal.of_real (f x) ∂μ :=
begin
simp_rw [integral_congr_ae (show f =ᵐ[μ] λ x, ∥f x∥,
by { filter_upwards [f_nn] with x hx,
rw [real.norm_eq_abs, abs_eq_self.mpr hx], }),
of_real_integral_norm_eq_lintegral_nnnorm hfi, ←of_real_norm_eq_coe_nnnorm],
apply lintegral_congr_ae,
filter_upwards [f_nn] with x hx,
exact congr_arg ennreal.of_real (by rw [real.norm_eq_abs, abs_eq_self.mpr hx]),
end
lemma integral_to_real {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) (hf : ∀ᵐ x ∂μ, f x < ∞) :
∫ a, (f a).to_real ∂μ = (∫⁻ a, f a ∂μ).to_real :=
begin
rw [integral_eq_lintegral_of_nonneg_ae _ hfm.ennreal_to_real.ae_strongly_measurable],
{ rw lintegral_congr_ae, refine hf.mp (eventually_of_forall _),
intros x hx, rw [lt_top_iff_ne_top] at hx, simp [hx] },
{ exact (eventually_of_forall $ λ x, ennreal.to_real_nonneg) }
end
lemma lintegral_coe_le_coe_iff_integral_le {f : α → ℝ≥0} (hfi : integrable (λ x, (f x : ℝ)) μ)
{b : ℝ≥0} :
∫⁻ a, f a ∂μ ≤ b ↔ ∫ a, (f a : ℝ) ∂μ ≤ b :=
by rw [lintegral_coe_eq_integral f hfi, ennreal.of_real, ennreal.coe_le_coe,
real.to_nnreal_le_iff_le_coe]
lemma integral_coe_le_of_lintegral_coe_le {f : α → ℝ≥0} {b : ℝ≥0} (h : ∫⁻ a, f a ∂μ ≤ b) :
∫ a, (f a : ℝ) ∂μ ≤ b :=
begin
by_cases hf : integrable (λ a, (f a : ℝ)) μ,
{ exact (lintegral_coe_le_coe_iff_integral_le hf).1 h },
{ rw integral_undef hf, exact b.2 }
end
lemma integral_nonneg {f : α → ℝ} (hf : 0 ≤ f) : 0 ≤ ∫ a, f a ∂μ :=
integral_nonneg_of_ae $ eventually_of_forall hf
lemma integral_nonpos_of_ae {f : α → ℝ} (hf : f ≤ᵐ[μ] 0) : ∫ a, f a ∂μ ≤ 0 :=
begin
have hf : 0 ≤ᵐ[μ] (-f) := hf.mono (assume a h, by rwa [pi.neg_apply, pi.zero_apply, neg_nonneg]),
have : 0 ≤ ∫ a, -f a ∂μ := integral_nonneg_of_ae hf,
rwa [integral_neg, neg_nonneg] at this,
end
lemma integral_nonpos {f : α → ℝ} (hf : f ≤ 0) : ∫ a, f a ∂μ ≤ 0 :=
integral_nonpos_of_ae $ eventually_of_forall hf
lemma integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) :
∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 :=
by simp_rw [integral_eq_lintegral_of_nonneg_ae hf hfi.1, ennreal.to_real_eq_zero_iff,
lintegral_eq_zero_iff' (ennreal.measurable_of_real.comp_ae_measurable hfi.1.ae_measurable),
← ennreal.not_lt_top, ← has_finite_integral_iff_of_real hf, hfi.2, not_true, or_false,
← hf.le_iff_eq, filter.eventually_eq, filter.eventually_le, (∘), pi.zero_apply,
ennreal.of_real_eq_zero]
lemma integral_eq_zero_iff_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) :
∫ x, f x ∂μ = 0 ↔ f =ᵐ[μ] 0 :=
integral_eq_zero_iff_of_nonneg_ae (eventually_of_forall hf) hfi
lemma integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hfi : integrable f μ) :
(0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) :=
by simp_rw [(integral_nonneg_of_ae hf).lt_iff_ne, pos_iff_ne_zero, ne.def, @eq_comm ℝ 0,
integral_eq_zero_iff_of_nonneg_ae hf hfi, filter.eventually_eq, ae_iff, pi.zero_apply,
function.support]
lemma integral_pos_iff_support_of_nonneg {f : α → ℝ} (hf : 0 ≤ f) (hfi : integrable f μ) :
(0 < ∫ x, f x ∂μ) ↔ 0 < μ (function.support f) :=
integral_pos_iff_support_of_nonneg_ae (eventually_of_forall hf) hfi
section normed_add_comm_group
variables {H : Type*} [normed_add_comm_group H]
lemma L1.norm_eq_integral_norm (f : α →₁[μ] H) : ∥f∥ = ∫ a, ∥f a∥ ∂μ :=
begin
simp only [snorm, snorm', ennreal.one_to_real, ennreal.rpow_one, Lp.norm_def,
if_false, ennreal.one_ne_top, one_ne_zero, _root_.div_one],
rw integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (by simp [norm_nonneg]))
(Lp.ae_strongly_measurable f).norm,
simp [of_real_norm_eq_coe_nnnorm]
end
lemma L1.norm_of_fun_eq_integral_norm {f : α → H} (hf : integrable f μ) :
∥hf.to_L1 f∥ = ∫ a, ∥f a∥ ∂μ :=
begin
rw L1.norm_eq_integral_norm,
refine integral_congr_ae _,
apply hf.coe_fn_to_L1.mono,
intros a ha,
simp [ha]
end
lemma mem_ℒp.snorm_eq_integral_rpow_norm {f : α → H} {p : ℝ≥0∞} (hp1 : p ≠ 0) (hp2 : p ≠ ∞)
(hf : mem_ℒp f p μ) :
snorm f p μ = ennreal.of_real ((∫ a, ∥f a∥ ^ p.to_real ∂μ) ^ (p.to_real ⁻¹)) :=
begin
have A : ∫⁻ (a : α), ennreal.of_real (∥f a∥ ^ p.to_real) ∂μ = ∫⁻ (a : α), ∥f a∥₊ ^ p.to_real ∂μ,
{ apply lintegral_congr (λ x, _),
rw [← of_real_rpow_of_nonneg (norm_nonneg _) to_real_nonneg, of_real_norm_eq_coe_nnnorm] },
simp only [snorm_eq_lintegral_rpow_nnnorm hp1 hp2, one_div],
rw integral_eq_lintegral_of_nonneg_ae, rotate,
{ exact eventually_of_forall (λ x, real.rpow_nonneg_of_nonneg (norm_nonneg _) _) },
{ exact (hf.ae_strongly_measurable.norm.ae_measurable.pow_const _).ae_strongly_measurable },
rw [A, ← of_real_rpow_of_nonneg to_real_nonneg (inv_nonneg.2 to_real_nonneg), of_real_to_real],
exact (lintegral_rpow_nnnorm_lt_top_of_snorm_lt_top hp1 hp2 hf.2).ne
end
end normed_add_comm_group
lemma integral_mono_ae {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ᵐ[μ] g) :
∫ a, f a ∂μ ≤ ∫ a, g a ∂μ :=
set_to_fun_mono (dominated_fin_meas_additive_weighted_smul μ) (λ s _ _, weighted_smul_nonneg s)
hf hg h
@[mono] lemma integral_mono {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (h : f ≤ g) :
∫ a, f a ∂μ ≤ ∫ a, g a ∂μ :=
integral_mono_ae hf hg $ eventually_of_forall h
lemma integral_mono_of_nonneg {f g : α → ℝ} (hf : 0 ≤ᵐ[μ] f) (hgi : integrable g μ)
(h : f ≤ᵐ[μ] g) : ∫ a, f a ∂μ ≤ ∫ a, g a ∂μ :=
begin
by_cases hfm : ae_strongly_measurable f μ,
{ refine integral_mono_ae ⟨hfm, _⟩ hgi h,
refine (hgi.has_finite_integral.mono $ h.mp $ hf.mono $ λ x hf hfg, _),
simpa [abs_of_nonneg hf, abs_of_nonneg (le_trans hf hfg)] },
{ rw [integral_non_ae_strongly_measurable hfm],
exact integral_nonneg_of_ae (hf.trans h) }
end
lemma integral_mono_measure {f : α → ℝ} {ν} (hle : μ ≤ ν) (hf : 0 ≤ᵐ[ν] f) (hfi : integrable f ν) :
∫ a, f a ∂μ ≤ ∫ a, f a ∂ν :=
begin
have hfi' : integrable f μ := hfi.mono_measure hle,
have hf' : 0 ≤ᵐ[μ] f := hle.absolutely_continuous hf,
rw [integral_eq_lintegral_of_nonneg_ae hf' hfi'.1, integral_eq_lintegral_of_nonneg_ae hf hfi.1,
ennreal.to_real_le_to_real],
exacts [lintegral_mono' hle le_rfl, ((has_finite_integral_iff_of_real hf').1 hfi'.2).ne,
((has_finite_integral_iff_of_real hf).1 hfi.2).ne]
end
lemma norm_integral_le_integral_norm (f : α → E) : ∥(∫ a, f a ∂μ)∥ ≤ ∫ a, ∥f a∥ ∂μ :=
have le_ae : ∀ᵐ a ∂μ, 0 ≤ ∥f a∥ := eventually_of_forall (λa, norm_nonneg _),
classical.by_cases
( λh : ae_strongly_measurable f μ,
calc ∥∫ a, f a ∂μ∥ ≤ ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) :
norm_integral_le_lintegral_norm _
... = ∫ a, ∥f a∥ ∂μ : (integral_eq_lintegral_of_nonneg_ae le_ae $ h.norm).symm )
( λh : ¬ae_strongly_measurable f μ,
begin
rw [integral_non_ae_strongly_measurable h, norm_zero],
exact integral_nonneg_of_ae le_ae
end )
lemma norm_integral_le_of_norm_le {f : α → E} {g : α → ℝ} (hg : integrable g μ)
(h : ∀ᵐ x ∂μ, ∥f x∥ ≤ g x) : ∥∫ x, f x ∂μ∥ ≤ ∫ x, g x ∂μ :=
calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, ∥f x∥ ∂μ : norm_integral_le_integral_norm f
... ≤ ∫ x, g x ∂μ :
integral_mono_of_nonneg (eventually_of_forall $ λ x, norm_nonneg _) hg h
lemma simple_func.integral_eq_integral (f : α →ₛ E) (hfi : integrable f μ) :
f.integral μ = ∫ x, f x ∂μ :=
begin
rw [integral_eq f hfi, ← L1.simple_func.to_Lp_one_eq_to_L1,
L1.simple_func.integral_L1_eq_integral, L1.simple_func.integral_eq_integral],
exact simple_func.integral_congr hfi (Lp.simple_func.to_simple_func_to_Lp _ _).symm
end
lemma simple_func.integral_eq_sum (f : α →ₛ E) (hfi : integrable f μ) :
∫ x, f x ∂μ = ∑ x in f.range, (ennreal.to_real (μ (f ⁻¹' {x}))) • x :=
by { rw [← f.integral_eq_integral hfi, simple_func.integral, ← simple_func.integral_eq], refl, }
@[simp] lemma integral_const (c : E) : ∫ x : α, c ∂μ = (μ univ).to_real • c :=
begin
cases (@le_top _ _ _ (μ univ)).lt_or_eq with hμ hμ,
{ haveI : is_finite_measure μ := ⟨hμ⟩,
exact set_to_fun_const (dominated_fin_meas_additive_weighted_smul _) _, },
{ by_cases hc : c = 0,
{ simp [hc, integral_zero] },
{ have : ¬integrable (λ x : α, c) μ,
{ simp only [integrable_const_iff, not_or_distrib],
exact ⟨hc, hμ.not_lt⟩ },
simp [integral_undef, *] } }
end
lemma norm_integral_le_of_norm_le_const [is_finite_measure μ] {f : α → E} {C : ℝ}
(h : ∀ᵐ x ∂μ, ∥f x∥ ≤ C) :
∥∫ x, f x ∂μ∥ ≤ C * (μ univ).to_real :=
calc ∥∫ x, f x ∂μ∥ ≤ ∫ x, C ∂μ : norm_integral_le_of_norm_le (integrable_const C) h
... = C * (μ univ).to_real : by rw [integral_const, smul_eq_mul, mul_comm]
lemma tendsto_integral_approx_on_of_measurable
[measurable_space E] [borel_space E]
{f : α → E} {s : set E} [separable_space s] (hfi : integrable f μ)
(hfm : measurable f) (hs : ∀ᵐ x ∂μ, f x ∈ closure s) {y₀ : E} (h₀ : y₀ ∈ s)
(h₀i : integrable (λ x, y₀) μ) :
tendsto (λ n, (simple_func.approx_on f hfm s y₀ h₀ n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ) :=
begin
have hfi' := simple_func.integrable_approx_on hfm hfi h₀ h₀i,
simp only [simple_func.integral_eq_integral _ (hfi' _)],
exact tendsto_set_to_fun_approx_on_of_measurable (dominated_fin_meas_additive_weighted_smul μ)
hfi hfm hs h₀ h₀i,
end
lemma tendsto_integral_approx_on_of_measurable_of_range_subset
[measurable_space E] [borel_space E] {f : α → E}
(fmeas : measurable f) (hf : integrable f μ) (s : set E) [separable_space s]
(hs : range f ∪ {0} ⊆ s) :
tendsto (λ n, (simple_func.approx_on f fmeas s 0 (hs $ by simp) n).integral μ) at_top
(𝓝 $ ∫ x, f x ∂μ) :=
begin
apply tendsto_integral_approx_on_of_measurable hf fmeas _ _ (integrable_zero _ _ _),
exact eventually_of_forall (λ x, subset_closure (hs (set.mem_union_left _ (mem_range_self _)))),
end
variable {ν : measure α}
lemma integral_add_measure {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) :
∫ x, f x ∂(μ + ν) = ∫ x, f x ∂μ + ∫ x, f x ∂ν :=
begin
have hfi := hμ.add_measure hν,
simp_rw [integral_eq_set_to_fun],
have hμ_dfma : dominated_fin_meas_additive (μ + ν) (weighted_smul μ : set α → E →L[ℝ] E) 1,
from dominated_fin_meas_additive.add_measure_right μ ν
(dominated_fin_meas_additive_weighted_smul μ) zero_le_one,
have hν_dfma : dominated_fin_meas_additive (μ + ν) (weighted_smul ν : set α → E →L[ℝ] E) 1,
from dominated_fin_meas_additive.add_measure_left μ ν
(dominated_fin_meas_additive_weighted_smul ν) zero_le_one,
rw [← set_to_fun_congr_measure_of_add_right hμ_dfma (dominated_fin_meas_additive_weighted_smul μ)
f hfi,
← set_to_fun_congr_measure_of_add_left hν_dfma (dominated_fin_meas_additive_weighted_smul ν)
f hfi],
refine set_to_fun_add_left' _ _ _ (λ s hs hμνs, _) f,
rw [measure.coe_add, pi.add_apply, add_lt_top] at hμνs,
rw [weighted_smul, weighted_smul, weighted_smul, ← add_smul, measure.coe_add, pi.add_apply,
to_real_add hμνs.1.ne hμνs.2.ne],
end
@[simp] lemma integral_zero_measure {m : measurable_space α} (f : α → E) :
∫ x, f x ∂(0 : measure α) = 0 :=
set_to_fun_measure_zero (dominated_fin_meas_additive_weighted_smul _) rfl
theorem integral_finset_sum_measure {ι} {m : measurable_space α} {f : α → E}
{μ : ι → measure α} {s : finset ι} (hf : ∀ i ∈ s, integrable f (μ i)) :
∫ a, f a ∂(∑ i in s, μ i) = ∑ i in s, ∫ a, f a ∂μ i :=
begin
classical,
refine finset.induction_on' s _ _, -- `induction s using finset.induction_on'` fails
{ simp },
{ intros i t hi ht hit iht,
simp only [finset.sum_insert hit, ← iht],
exact integral_add_measure (hf _ hi) (integrable_finset_sum_measure.2 $ λ j hj, hf j (ht hj)) }
end
lemma nndist_integral_add_measure_le_lintegral (h₁ : integrable f μ) (h₂ : integrable f ν) :
(nndist (∫ x, f x ∂μ) (∫ x, f x ∂(μ + ν)) : ℝ≥0∞) ≤ ∫⁻ x, ∥f x∥₊ ∂ν :=
begin
rw [integral_add_measure h₁ h₂, nndist_comm, nndist_eq_nnnorm, add_sub_cancel'],
exact ennnorm_integral_le_lintegral_ennnorm _
end
theorem has_sum_integral_measure {ι} {m : measurable_space α} {f : α → E} {μ : ι → measure α}
(hf : integrable f (measure.sum μ)) :
has_sum (λ i, ∫ a, f a ∂μ i) (∫ a, f a ∂measure.sum μ) :=
begin
have hfi : ∀ i, integrable f (μ i) := λ i, hf.mono_measure (measure.le_sum _ _),
simp only [has_sum, ← integral_finset_sum_measure (λ i _, hfi i)],
refine metric.nhds_basis_ball.tendsto_right_iff.mpr (λ ε ε0, _),
lift ε to ℝ≥0 using ε0.le,
have hf_lt : ∫⁻ x, ∥f x∥₊ ∂(measure.sum μ) < ∞ := hf.2,
have hmem : ∀ᶠ y in 𝓝 ∫⁻ x, ∥f x∥₊ ∂(measure.sum μ), ∫⁻ x, ∥f x∥₊ ∂(measure.sum μ) < y + ε,
{ refine tendsto_id.add tendsto_const_nhds (lt_mem_nhds $ ennreal.lt_add_right _ _),
exacts [hf_lt.ne, ennreal.coe_ne_zero.2 (nnreal.coe_ne_zero.1 ε0.ne')] },
refine ((has_sum_lintegral_measure (λ x, ∥f x∥₊) μ).eventually hmem).mono (λ s hs, _),
obtain ⟨ν, hν⟩ : ∃ ν, (∑ i in s, μ i) + ν = measure.sum μ,
{ refine ⟨measure.sum (λ i : ↥(sᶜ : set ι), μ i), _⟩,
simpa only [← measure.sum_coe_finset] using measure.sum_add_sum_compl (s : set ι) μ },
rw [metric.mem_ball, ← coe_nndist, nnreal.coe_lt_coe, ← ennreal.coe_lt_coe, ← hν],
rw [← hν, integrable_add_measure] at hf,
refine (nndist_integral_add_measure_le_lintegral hf.1 hf.2).trans_lt _,
rw [← hν, lintegral_add_measure, lintegral_finset_sum_measure] at hs,
exact lt_of_add_lt_add_left hs
end
theorem integral_sum_measure {ι} {m : measurable_space α} {f : α → E} {μ : ι → measure α}
(hf : integrable f (measure.sum μ)) :
∫ a, f a ∂measure.sum μ = ∑' i, ∫ a, f a ∂μ i :=
(has_sum_integral_measure hf).tsum_eq.symm
@[simp] lemma integral_smul_measure (f : α → E) (c : ℝ≥0∞) :
∫ x, f x ∂(c • μ) = c.to_real • ∫ x, f x ∂μ :=
begin
-- First we consider the “degenerate” case `c = ∞`
rcases eq_or_ne c ∞ with rfl|hc,
{ rw [ennreal.top_to_real, zero_smul, integral_eq_set_to_fun, set_to_fun_top_smul_measure], },
-- Main case: `c ≠ ∞`
simp_rw [integral_eq_set_to_fun, ← set_to_fun_smul_left],
have hdfma :
dominated_fin_meas_additive μ (weighted_smul (c • μ) : set α → E →L[ℝ] E) c.to_real,
from mul_one c.to_real
▸ (dominated_fin_meas_additive_weighted_smul (c • μ)).of_smul_measure c hc,
have hdfma_smul := (dominated_fin_meas_additive_weighted_smul (c • μ)),
rw ← set_to_fun_congr_smul_measure c hc hdfma hdfma_smul f,
exact set_to_fun_congr_left' _ _ (λ s hs hμs, weighted_smul_smul_measure μ c) f,
end
lemma integral_map_of_strongly_measurable {β} [measurable_space β] {φ : α → β} (hφ : measurable φ)
{f : β → E} (hfm : strongly_measurable f) :
∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ :=
begin
by_cases hfi : integrable f (measure.map φ μ), swap,
{ rw [integral_undef hfi, integral_undef],
rwa [← integrable_map_measure hfm.ae_strongly_measurable hφ.ae_measurable] },
borelize E,
haveI : separable_space (range f ∪ {0} : set E) := hfm.separable_space_range_union_singleton,
refine tendsto_nhds_unique
(tendsto_integral_approx_on_of_measurable_of_range_subset hfm.measurable hfi _ subset.rfl) _,
convert tendsto_integral_approx_on_of_measurable_of_range_subset (hfm.measurable.comp hφ)
((integrable_map_measure hfm.ae_strongly_measurable hφ.ae_measurable).1 hfi) (range f ∪ {0})
(by simp [insert_subset_insert, set.range_comp_subset_range]) using 1,
ext1 i,
simp only [simple_func.approx_on_comp, simple_func.integral_eq, measure.map_apply, hφ,
simple_func.measurable_set_preimage, ← preimage_comp, simple_func.coe_comp],
refine (finset.sum_subset (simple_func.range_comp_subset_range _ hφ) (λ y _ hy, _)).symm,
rw [simple_func.mem_range, ← set.preimage_singleton_eq_empty, simple_func.coe_comp] at hy,
rw [hy],
simp,
end
lemma integral_map {β} [measurable_space β] {φ : α → β} (hφ : ae_measurable φ μ)
{f : β → E} (hfm : ae_strongly_measurable f (measure.map φ μ)) :
∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ :=
let g := hfm.mk f in calc
∫ y, f y ∂(measure.map φ μ) = ∫ y, g y ∂(measure.map φ μ) : integral_congr_ae hfm.ae_eq_mk
... = ∫ y, g y ∂(measure.map (hφ.mk φ) μ) :
by { congr' 1, exact measure.map_congr hφ.ae_eq_mk }
... = ∫ x, g (hφ.mk φ x) ∂μ :
integral_map_of_strongly_measurable hφ.measurable_mk hfm.strongly_measurable_mk
... = ∫ x, g (φ x) ∂μ : integral_congr_ae (hφ.ae_eq_mk.symm.fun_comp _)
... = ∫ x, f (φ x) ∂μ : integral_congr_ae $ ae_eq_comp hφ (hfm.ae_eq_mk).symm
lemma _root_.measurable_embedding.integral_map {β} {_ : measurable_space β} {f : α → β}
(hf : measurable_embedding f) (g : β → E) :
∫ y, g y ∂(measure.map f μ) = ∫ x, g (f x) ∂μ :=
begin
by_cases hgm : ae_strongly_measurable g (measure.map f μ),
{ exact integral_map hf.measurable.ae_measurable hgm },
{ rw [integral_non_ae_strongly_measurable hgm, integral_non_ae_strongly_measurable],
rwa ← hf.ae_strongly_measurable_map_iff }
end
lemma _root_.closed_embedding.integral_map {β} [topological_space α] [borel_space α]
[topological_space β] [measurable_space β] [borel_space β]
{φ : α → β} (hφ : closed_embedding φ) (f : β → E) :
∫ y, f y ∂(measure.map φ μ) = ∫ x, f (φ x) ∂μ :=
hφ.measurable_embedding.integral_map _
lemma integral_map_equiv {β} [measurable_space β] (e : α ≃ᵐ β) (f : β → E) :
∫ y, f y ∂(measure.map e μ) = ∫ x, f (e x) ∂μ :=
e.measurable_embedding.integral_map f
lemma measure_preserving.integral_comp {β} {_ : measurable_space β} {f : α → β} {ν}
(h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) (g : β → E) :
∫ x, g (f x) ∂μ = ∫ y, g y ∂ν :=
h₁.map_eq ▸ (h₂.integral_map g).symm
lemma set_integral_eq_subtype {α} [measure_space α] {s : set α} (hs : measurable_set s)
(f : α → E) :
∫ x in s, f x = ∫ x : s, f x :=
by { rw ← map_comap_subtype_coe hs, exact (measurable_embedding.subtype_coe hs).integral_map _ }
@[simp] lemma integral_dirac' [measurable_space α] (f : α → E) (a : α)
(hfm : strongly_measurable f) :
∫ x, f x ∂(measure.dirac a) = f a :=
begin
borelize E,
calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) :
integral_congr_ae $ ae_eq_dirac' hfm.measurable
... = f a : by simp [measure.dirac_apply_of_mem]
end
@[simp] lemma integral_dirac [measurable_space α] [measurable_singleton_class α]
(f : α → E) (a : α) : ∫ x, f x ∂(measure.dirac a) = f a :=
calc ∫ x, f x ∂(measure.dirac a) = ∫ x, f a ∂(measure.dirac a) :
integral_congr_ae $ ae_eq_dirac f
... = f a : by simp [measure.dirac_apply_of_mem]
lemma mul_meas_ge_le_integral_of_nonneg [is_finite_measure μ] {f : α → ℝ} (hf_nonneg : 0 ≤ f)
(hf_int : integrable f μ) (ε : ℝ) :
ε * (μ {x | ε ≤ f x}).to_real ≤ ∫ x, f x ∂μ :=
begin
cases lt_or_le ε 0 with hε hε,
{ exact (mul_nonpos_of_nonpos_of_nonneg hε.le ennreal.to_real_nonneg).trans
(integral_nonneg hf_nonneg), },
rw [integral_eq_lintegral_of_nonneg_ae (eventually_of_forall (λ x, hf_nonneg x))
hf_int.ae_strongly_measurable, ← ennreal.to_real_of_real hε, ← ennreal.to_real_mul],
have : {x : α | (ennreal.of_real ε).to_real ≤ f x}
= {x : α | ennreal.of_real ε ≤ (λ x, ennreal.of_real (f x)) x},
{ ext1 x,
rw [set.mem_set_of_eq, set.mem_set_of_eq, ← ennreal.to_real_of_real (hf_nonneg x)],
exact ennreal.to_real_le_to_real ennreal.of_real_ne_top ennreal.of_real_ne_top, },
rw this,
have h_meas : ae_measurable (λ x, ennreal.of_real (f x)) μ,
from measurable_id'.ennreal_of_real.comp_ae_measurable hf_int.ae_measurable,
have h_mul_meas_le := @mul_meas_ge_le_lintegral₀ _ _ μ _ h_meas (ennreal.of_real ε),
rw ennreal.to_real_le_to_real _ _,
{ exact h_mul_meas_le, },
{ simp only [ne.def, with_top.mul_eq_top_iff, ennreal.of_real_eq_zero, not_le,
ennreal.of_real_ne_top, false_and, or_false, not_and],
exact λ _, measure_ne_top _ _, },
{ have h_lt_top : ∫⁻ a, ∥f a∥₊ ∂μ < ∞ := hf_int.has_finite_integral,
simp_rw [← of_real_norm_eq_coe_nnnorm, real.norm_eq_abs] at h_lt_top,
convert h_lt_top.ne,
ext1 x,
rw abs_of_nonneg (hf_nonneg x), },
end
/-- Hölder's inequality for the integral of a product of norms. The integral of the product of two
norms of functions is bounded by the product of their `ℒp` and `ℒq` seminorms when `p` and `q` are
conjugate exponents. -/
theorem integral_mul_norm_le_Lp_mul_Lq {E} [normed_add_comm_group E] {f g : α → E}
{p q : ℝ} (hpq : p.is_conjugate_exponent q)
(hf : mem_ℒp f (ennreal.of_real p) μ) (hg : mem_ℒp g (ennreal.of_real q) μ) :
∫ a, ∥f a∥ * ∥g a∥ ∂μ ≤ (∫ a, ∥f a∥ ^ p ∂μ) ^ (1/p) * (∫ a, ∥g a∥ ^ q ∂μ) ^ (1/q) :=
begin
-- translate the Bochner integrals into Lebesgue integrals.
rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae,
integral_eq_lintegral_of_nonneg_ae],
rotate 1,
{ exact eventually_of_forall (λ x, real.rpow_nonneg_of_nonneg (norm_nonneg _) _), },
{ exact (hg.1.norm.ae_measurable.pow ae_measurable_const).ae_strongly_measurable, },
{ exact eventually_of_forall (λ x, real.rpow_nonneg_of_nonneg (norm_nonneg _) _),},
{ exact (hf.1.norm.ae_measurable.pow ae_measurable_const).ae_strongly_measurable, },
{ exact eventually_of_forall (λ x, mul_nonneg (norm_nonneg _) (norm_nonneg _)), },
{ exact hf.1.norm.mul hg.1.norm, },
rw [ennreal.to_real_rpow, ennreal.to_real_rpow, ← ennreal.to_real_mul],
-- replace norms by nnnorm
have h_left : ∫⁻ a, ennreal.of_real (∥f a∥ * ∥g a∥) ∂μ
= ∫⁻ a, ((λ x, (∥f x∥₊ : ℝ≥0∞)) * (λ x, ∥g x∥₊)) a ∂μ,
{ simp_rw [pi.mul_apply, ← of_real_norm_eq_coe_nnnorm, ennreal.of_real_mul (norm_nonneg _)], },
have h_right_f : ∫⁻ a, ennreal.of_real (∥f a∥ ^ p) ∂μ = ∫⁻ a, ∥f a∥₊ ^ p ∂μ,
{ refine lintegral_congr (λ x, _),
rw [← of_real_norm_eq_coe_nnnorm, ennreal.of_real_rpow_of_nonneg (norm_nonneg _) hpq.nonneg], },
have h_right_g : ∫⁻ a, ennreal.of_real (∥g a∥ ^ q) ∂μ = ∫⁻ a, ∥g a∥₊ ^ q ∂μ,
{ refine lintegral_congr (λ x, _),
rw [← of_real_norm_eq_coe_nnnorm,
ennreal.of_real_rpow_of_nonneg (norm_nonneg _) hpq.symm.nonneg], },
rw [h_left, h_right_f, h_right_g],
-- we can now apply `ennreal.lintegral_mul_le_Lp_mul_Lq` (up to the `to_real` application)
refine ennreal.to_real_mono _ _,
{ refine ennreal.mul_ne_top _ _,
{ convert hf.snorm_ne_top,
rw snorm_eq_lintegral_rpow_nnnorm,
{ rw ennreal.to_real_of_real hpq.nonneg, },
{ rw [ne.def, ennreal.of_real_eq_zero, not_le],
exact hpq.pos, },
{ exact ennreal.coe_ne_top, }, },
{ convert hg.snorm_ne_top,
rw snorm_eq_lintegral_rpow_nnnorm,
{ rw ennreal.to_real_of_real hpq.symm.nonneg, },
{ rw [ne.def, ennreal.of_real_eq_zero, not_le],
exact hpq.symm.pos, },
{ exact ennreal.coe_ne_top, }, }, },
{ exact ennreal.lintegral_mul_le_Lp_mul_Lq μ hpq hf.1.nnnorm.ae_measurable.coe_nnreal_ennreal
hg.1.nnnorm.ae_measurable.coe_nnreal_ennreal, },
end
/-- Hölder's inequality for functions `α → ℝ`. The integral of the product of two nonnegative
functions is bounded by the product of their `ℒp` and `ℒq` seminorms when `p` and `q` are conjugate
exponents. -/
theorem integral_mul_le_Lp_mul_Lq_of_nonneg {p q : ℝ}
(hpq : p.is_conjugate_exponent q) {f g : α → ℝ} (hf_nonneg : 0 ≤ᵐ[μ] f) (hg_nonneg : 0 ≤ᵐ[μ] g)
(hf : mem_ℒp f (ennreal.of_real p) μ) (hg : mem_ℒp g (ennreal.of_real q) μ) :
∫ a, f a * g a ∂μ ≤ (∫ a, (f a) ^ p ∂μ) ^ (1/p) * (∫ a, (g a) ^ q ∂μ) ^ (1/q) :=
begin
have h_left : ∫ a, f a * g a ∂μ = ∫ a, ∥f a∥ * ∥g a∥ ∂μ,
{ refine integral_congr_ae _,
filter_upwards [hf_nonneg, hg_nonneg] with x hxf hxg,
rw [real.norm_of_nonneg hxf, real.norm_of_nonneg hxg], },
have h_right_f : ∫ a, (f a) ^ p ∂μ = ∫ a, ∥f a∥ ^ p ∂μ,
{ refine integral_congr_ae _,
filter_upwards [hf_nonneg] with x hxf,
rw real.norm_of_nonneg hxf, },
have h_right_g : ∫ a, (g a) ^ q ∂μ = ∫ a, ∥g a∥ ^ q ∂μ,
{ refine integral_congr_ae _,
filter_upwards [hg_nonneg] with x hxg,
rw real.norm_of_nonneg hxg, },
rw [h_left, h_right_f, h_right_g],
exact integral_mul_norm_le_Lp_mul_Lq hpq hf hg,
end
end properties
mk_simp_attribute integral_simps "Simp set for integral rules."
attribute [integral_simps] integral_neg integral_smul L1.integral_add L1.integral_sub
L1.integral_smul L1.integral_neg
attribute [irreducible] integral L1.integral
section integral_trim
variables {H β γ : Type*} [normed_add_comm_group H]
{m m0 : measurable_space β} {μ : measure β}
/-- Simple function seen as simple function of a larger `measurable_space`. -/
def simple_func.to_larger_space (hm : m ≤ m0) (f : @simple_func β m γ) : simple_func β γ :=
⟨@simple_func.to_fun β m γ f, λ x, hm _ (@simple_func.measurable_set_fiber β γ m f x),
@simple_func.finite_range β γ m f⟩
lemma simple_func.coe_to_larger_space_eq (hm : m ≤ m0) (f : @simple_func β m γ) :
⇑(f.to_larger_space hm) = f :=
rfl
lemma integral_simple_func_larger_space (hm : m ≤ m0) (f : @simple_func β m F)
(hf_int : integrable f μ) :
∫ x, f x ∂μ = ∑ x in (@simple_func.range β F m f), (ennreal.to_real (μ (f ⁻¹' {x}))) • x :=
begin
simp_rw ← f.coe_to_larger_space_eq hm,
have hf_int : integrable (f.to_larger_space hm) μ, by rwa simple_func.coe_to_larger_space_eq,
rw simple_func.integral_eq_sum _ hf_int,
congr,
end
lemma integral_trim_simple_func (hm : m ≤ m0) (f : @simple_func β m F) (hf_int : integrable f μ) :
∫ x, f x ∂μ = ∫ x, f x ∂(μ.trim hm) :=
begin
have hf : strongly_measurable[m] f, from @simple_func.strongly_measurable β F m _ f,
have hf_int_m := hf_int.trim hm hf,
rw [integral_simple_func_larger_space (le_refl m) f hf_int_m,
integral_simple_func_larger_space hm f hf_int],
congr' with x,
congr,
exact (trim_measurable_set_eq hm (@simple_func.measurable_set_fiber β F m f x)).symm,
end
lemma integral_trim (hm : m ≤ m0) {f : β → F} (hf : strongly_measurable[m] f) :
∫ x, f x ∂μ = ∫ x, f x ∂(μ.trim hm) :=
begin
borelize F,
by_cases hf_int : integrable f μ,
swap,
{ have hf_int_m : ¬ integrable f (μ.trim hm),
from λ hf_int_m, hf_int (integrable_of_integrable_trim hm hf_int_m),
rw [integral_undef hf_int, integral_undef hf_int_m], },
haveI : separable_space (range f ∪ {0} : set F) := hf.separable_space_range_union_singleton,
let f_seq := @simple_func.approx_on F β _ _ _ m _ hf.measurable (range f ∪ {0}) 0 (by simp) _,
have hf_seq_meas : ∀ n, strongly_measurable[m] (f_seq n),
from λ n, @simple_func.strongly_measurable β F m _ (f_seq n),
have hf_seq_int : ∀ n, integrable (f_seq n) μ,
from simple_func.integrable_approx_on_range (hf.mono hm).measurable hf_int,
have hf_seq_int_m : ∀ n, integrable (f_seq n) (μ.trim hm),
from λ n, (hf_seq_int n).trim hm (hf_seq_meas n) ,
have hf_seq_eq : ∀ n, ∫ x, f_seq n x ∂μ = ∫ x, f_seq n x ∂(μ.trim hm),
from λ n, integral_trim_simple_func hm (f_seq n) (hf_seq_int n),
have h_lim_1 : at_top.tendsto (λ n, ∫ x, f_seq n x ∂μ) (𝓝 (∫ x, f x ∂μ)),
{ refine tendsto_integral_of_L1 f hf_int (eventually_of_forall hf_seq_int) _,
exact simple_func.tendsto_approx_on_range_L1_nnnorm (hf.mono hm).measurable hf_int, },
have h_lim_2 : at_top.tendsto (λ n, ∫ x, f_seq n x ∂μ) (𝓝 (∫ x, f x ∂(μ.trim hm))),
{ simp_rw hf_seq_eq,
refine @tendsto_integral_of_L1 β F _ _ _ m (μ.trim hm) _ f
(hf_int.trim hm hf) _ _ (eventually_of_forall hf_seq_int_m) _,
exact @simple_func.tendsto_approx_on_range_L1_nnnorm β F m _ _ _ f _ _
hf.measurable (hf_int.trim hm hf), },
exact tendsto_nhds_unique h_lim_1 h_lim_2,
end
lemma integral_trim_ae (hm : m ≤ m0) {f : β → F} (hf : ae_strongly_measurable f (μ.trim hm)) :
∫ x, f x ∂μ = ∫ x, f x ∂(μ.trim hm) :=
begin
rw [integral_congr_ae (ae_eq_of_ae_eq_trim hf.ae_eq_mk), integral_congr_ae hf.ae_eq_mk],
exact integral_trim hm hf.strongly_measurable_mk,
end
lemma ae_eq_trim_of_strongly_measurable
[topological_space γ] [metrizable_space γ] (hm : m ≤ m0) {f g : β → γ}
(hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) (hfg : f =ᵐ[μ] g) :
f =ᵐ[μ.trim hm] g :=
begin
rwa [eventually_eq, ae_iff, trim_measurable_set_eq hm _],
exact (hf.measurable_set_eq_fun hg).compl
end
lemma ae_eq_trim_iff [topological_space γ] [metrizable_space γ]
(hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) :
f =ᵐ[μ.trim hm] g ↔ f =ᵐ[μ] g :=
⟨ae_eq_of_ae_eq_trim, ae_eq_trim_of_strongly_measurable hm hf hg⟩
lemma ae_le_trim_of_strongly_measurable
[linear_order γ] [topological_space γ] [order_closed_topology γ] [pseudo_metrizable_space γ]
(hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g)
(hfg : f ≤ᵐ[μ] g) :
f ≤ᵐ[μ.trim hm] g :=
begin
rwa [eventually_le, ae_iff, trim_measurable_set_eq hm _],
exact (hf.measurable_set_le hg).compl,
end
lemma ae_le_trim_iff
[linear_order γ] [topological_space γ] [order_closed_topology γ] [pseudo_metrizable_space γ]
(hm : m ≤ m0) {f g : β → γ} (hf : strongly_measurable[m] f) (hg : strongly_measurable[m] g) :
f ≤ᵐ[μ.trim hm] g ↔ f ≤ᵐ[μ] g :=
⟨ae_le_of_ae_le_trim, ae_le_trim_of_strongly_measurable hm hf hg⟩
end integral_trim
section snorm_bound
variables {m0 : measurable_space α} {μ : measure α}
lemma snorm_one_le_of_le {r : ℝ≥0} {f : α → ℝ}
(hfint : integrable f μ) (hfint' : 0 ≤ ∫ x, f x ∂μ) (hf : ∀ᵐ ω ∂μ, f ω ≤ r) :
snorm f 1 μ ≤ 2 * μ set.univ * r :=
begin
by_cases hr : r = 0,
{ suffices : f =ᵐ[μ] 0,
{ rw [snorm_congr_ae this, snorm_zero, hr, ennreal.coe_zero, mul_zero],
exact le_rfl },
rw [hr, nonneg.coe_zero] at hf,
have hnegf : ∫ x, -f x ∂μ = 0,
{ rw [integral_neg, neg_eq_zero],
exact le_antisymm (integral_nonpos_of_ae hf) hfint' },
have := (integral_eq_zero_iff_of_nonneg_ae _ hfint.neg).1 hnegf,
{ filter_upwards [this] with ω hω,
rwa [pi.neg_apply, pi.zero_apply, neg_eq_zero] at hω },
{ filter_upwards [hf] with ω hω,
rwa [pi.zero_apply, pi.neg_apply, right.nonneg_neg_iff] } },
by_cases hμ : is_finite_measure μ,
swap,
{ have : μ set.univ = ∞,
{ by_contra hμ',
exact hμ (is_finite_measure.mk $ lt_top_iff_ne_top.2 hμ') },
rw [this, ennreal.mul_top, if_neg, ennreal.top_mul, if_neg],
{ exact le_top },
{ simp [hr] },
{ norm_num } },
haveI := hμ,
rw [integral_eq_integral_pos_part_sub_integral_neg_part hfint, sub_nonneg] at hfint',
have hposbdd : ∫ ω, max (f ω) 0 ∂μ ≤ (μ set.univ).to_real • r,
{ rw ← integral_const,
refine integral_mono_ae hfint.real_to_nnreal (integrable_const r) _,
filter_upwards [hf] with ω hω using real.to_nnreal_le_iff_le_coe.2 hω },
rw [mem_ℒp.snorm_eq_integral_rpow_norm one_ne_zero ennreal.one_ne_top
(mem_ℒp_one_iff_integrable.2 hfint),
ennreal.of_real_le_iff_le_to_real (ennreal.mul_ne_top
(ennreal.mul_ne_top ennreal.two_ne_top $ @measure_ne_top _ _ _ hμ _) ennreal.coe_ne_top)],
simp_rw [ennreal.one_to_real, _root_.inv_one, real.rpow_one, real.norm_eq_abs,
← max_zero_add_max_neg_zero_eq_abs_self, ← real.coe_to_nnreal'],
rw integral_add hfint.real_to_nnreal,
{ simp only [real.coe_to_nnreal', ennreal.to_real_mul, ennreal.to_real_bit0,
ennreal.one_to_real, ennreal.coe_to_real] at hfint' ⊢,
refine (add_le_add_left hfint' _).trans _,
rwa [← two_mul, mul_assoc, mul_le_mul_left (two_pos : (0 : ℝ) < 2)] },
{ exact hfint.neg.sup (integrable_zero _ _ μ) }
end
lemma snorm_one_le_of_le' {r : ℝ} {f : α → ℝ}
(hfint : integrable f μ) (hfint' : 0 ≤ ∫ x, f x ∂μ) (hf : ∀ᵐ ω ∂μ, f ω ≤ r) :
snorm f 1 μ ≤ 2 * μ set.univ * ennreal.of_real r :=
begin
refine snorm_one_le_of_le hfint hfint' _,
simp only [real.coe_to_nnreal', le_max_iff],
filter_upwards [hf] with ω hω using or.inl hω,
end
end snorm_bound
end measure_theory
|
7e1e1046aacffc8d2aa4c7fe18d26b906105a555 | 6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf | /src/game/world8/level11.lean | 316f060b65936636b1fcea342f38aa2547c3b0d1 | [
"Apache-2.0"
] | permissive | arolihas/natural_number_game | 4f0c93feefec93b8824b2b96adff8b702b8b43ce | 8e4f7b4b42888a3b77429f90cce16292bd288138 | refs/heads/master | 1,621,872,426,808 | 1,586,270,467,000 | 1,586,270,467,000 | 253,648,466 | 0 | 0 | null | 1,586,219,694,000 | 1,586,219,694,000 | null | UTF-8 | Lean | false | false | 512 | lean | import mynat.definition -- hide
import mynat.add -- hide
import game.world8.level10 -- hide
namespace mynat -- hide
/-
# Advanced Addition World
## Level 11: `add_right_eq_zero`
We just proved `add_left_eq_zero (a b : mynat) : a + b = 0 → b = 0`.
Hopefully `add_right_eq_zero` shouldn't be too hard now.
-/
/- Lemma
If $a$ and $b$ are natural numbers such that
$$ a + b = 0, $$
then $a = 0$.
-/
lemma add_right_eq_zero {a b : mynat} : a + b = 0 → a = 0 :=
begin [nat_num_game]
end
end mynat -- hide
|
2666cf39230f6f21ee7a7ae8cbe4ed39ecee24b3 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/proofDataConfusionBug.lean | f809c8a9818e44a4c446290d317b073b9af8e7d4 | [
"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 | 327 | lean | def combine : PSum Unit (p → q) → PSum Unit p → PSum Unit q
| PSum.inr f, PSum.inr proof => PSum.inr $ f proof
| _, _ => PSum.inl ()
def tst : String :=
let f : PSum Unit (True → True) := .inr id
let v : PSum Unit True := .inr .intro
match combine f v with
| .inr _ => "inr"
| .inl _ => "inl"
#eval tst
|
70c7c8616c299418c0cc2172d4a021141a4d1a5e | a047a4718edfa935d17231e9e6ecec8c7b701e05 | /src/ring_theory/ideal_operations.lean | 267f9abc6fd5ff1c56ce979e8788e419ecf77e76 | [
"Apache-2.0"
] | permissive | utensil-contrib/mathlib | bae0c9fafe5e2bdb516efc89d6f8c1502ecc9767 | b91909e77e219098a2f8cc031f89d595fe274bd2 | refs/heads/master | 1,668,048,976,965 | 1,592,442,701,000 | 1,592,442,701,000 | 273,197,855 | 0 | 0 | null | 1,592,472,812,000 | 1,592,472,811,000 | null | UTF-8 | Lean | false | false | 34,407 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
More operations on modules and ideals.
-/
import data.nat.choose
import data.equiv.ring
import ring_theory.algebra_operations
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)⟩
def annihilator (N : submodule R M) : ideal R :=
(linear_map.lsmul R N).ker
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))
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)
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, quotient.mk_one, quotient.mk_prod],
apply finset.prod_eq_one, intros, rw [← quotient.mk_one, quotient.eq], apply hgi },
intros j hjs hji, rw [← quotient.eq_zero_iff_mem, quotient.mk_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, quotient.mk_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, quotient.mk_one] at hφ1,
rw [quotient.mk_mul, hφ1, mul_one]
end
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_hom (f i)) },
{ intros r hr,
rw submodule.mem_infi at hr,
ext i,
exact quotient.eq_zero_iff_mem.2 (hr i) }
end
theorem bijective_quotient_inf_to_pi_quotient [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 _ (bijective_quotient_inf_to_pi_quotient 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}
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
/-- 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, classical.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 (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}
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 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
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⟩)
@[simp] lemma map_quotient_self :
map (quotient.mk_hom 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 _ _⟩)
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))
/-- Correspondence theorem -/
def order_iso_of_surjective :
((≤) : ideal S → ideal S → Prop) ≃o
((≤) : { p : ideal R // comap f ⊥ ≤ p } → { p : ideal R // comap f ⊥ ≤ p } → Prop) :=
{ 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,
ord' := λ I1 I2, ⟨comap_mono, λ H, map_comap_of_surjective f hf I1 ▸
map_comap_of_surjective f hf I2 ▸ map_mono H⟩ }
def le_order_embedding_of_surjective :
((≤) : ideal S → ideal S → Prop) ≼o ((≤) : ideal R → ideal R → Prop) :=
(order_iso_of_surjective f hf).to_order_embedding.trans (subtype.order_embedding _ _)
def lt_order_embedding_of_surjective :
((<) : ideal S → ideal S → Prop) ≼o ((<) : ideal R → ideal R → Prop) :=
(le_order_embedding_of_surjective f hf).lt_embedding_of_le_embedding
end surjective
end map_and_comap
section jacobson
variables {R : Type u} [comm_ring R]
/-- The Jacobson radical of `I` is the infimum of all maximal ideals containing `I`. -/
def jacobson (I : ideal R) : ideal R :=
Inf {J : ideal R | I ≤ J ∧ is_maximal J}
theorem jacobson_eq_top_iff {I : ideal R} : jacobson I = ⊤ ↔ I = ⊤ :=
⟨λ H, classical.by_contradiction $ λ hi, let ⟨M, hm, him⟩ := exists_le_maximal I hi in
lt_top_iff_ne_top.1 (lt_of_le_of_lt (show jacobson I ≤ M, from Inf_le ⟨him, hm⟩) $ lt_top_iff_ne_top.2 hm.1) H,
λ H, eq_top_iff.2 $ le_Inf $ λ J ⟨hij, hj⟩, H ▸ hij⟩
theorem mem_jacobson_iff {I : ideal R} {x : R} :
x ∈ jacobson I ↔ ∀ y, ∃ z, x * y * z + z - 1 ∈ I :=
⟨λ hx y, classical.by_cases
(assume hxy : I ⊔ span {x * y + 1} = ⊤,
let ⟨p, hpi, q, hq, hpq⟩ := submodule.mem_sup.1 ((eq_top_iff_one _).1 hxy) in
let ⟨r, hr⟩ := mem_span_singleton.1 hq in
⟨r, by rw [← one_mul r, ← mul_assoc, ← add_mul, mul_one, ← hr, ← hpq, ← neg_sub, add_sub_cancel]; exact I.neg_mem hpi⟩)
(assume hxy : I ⊔ span {x * y + 1} ≠ ⊤,
let ⟨M, hm1, hm2⟩ := exists_le_maximal _ hxy in
suffices x ∉ M, from (this $ mem_Inf.1 hx ⟨le_trans le_sup_left hm2, hm1⟩).elim,
λ hxm, hm1.1 $ (eq_top_iff_one _).2 $ add_sub_cancel' (x * y) 1 ▸ M.sub_mem
(le_trans le_sup_right hm2 $ mem_span_singleton.2 $ dvd_refl _)
(M.mul_mem_right hxm)),
λ hx, mem_Inf.2 $ λ M ⟨him, hm⟩, classical.by_contradiction $ λ hxm,
let ⟨y, hy⟩ := hm.exists_inv hxm, ⟨z, hz⟩ := hx (-y) in
hm.1 $ (eq_top_iff_one _).2 $ sub_sub_cancel (x * -y * z + z) 1 ▸ M.sub_mem
(by rw [← one_mul z, ← mul_assoc, ← add_mul, mul_one, mul_neg_eq_neg_mul_symm, neg_add_eq_sub, ← neg_sub,
neg_mul_eq_neg_mul_symm, neg_mul_eq_mul_neg, mul_comm x y]; exact M.mul_mem_right hy)
(him hz)⟩
end jacobson
section is_local
variables {R : Type u} [comm_ring R]
/-- An ideal `I` is local iff its Jacobson radical is maximal. -/
@[class] def is_local (I : ideal R) : Prop :=
is_maximal (jacobson I)
theorem is_local_of_is_maximal_radical {I : ideal R} (hi : is_maximal (radical I)) : is_local I :=
have radical I = jacobson I,
from le_antisymm (le_Inf $ λ M ⟨him, hm⟩, hm.is_prime.radical_le_iff.2 him)
(Inf_le ⟨le_radical, hi⟩),
show is_maximal (jacobson I), from this ▸ hi
theorem is_local.le_jacobson {I J : ideal R} (hi : is_local I) (hij : I ≤ J) (hj : J ≠ ⊤) : J ≤ jacobson I :=
let ⟨M, hm, hjm⟩ := exists_le_maximal J hj in
le_trans hjm $ le_of_eq $ eq.symm $ hi.eq_of_le hm.1 $ Inf_le ⟨le_trans hij hjm, hm⟩
theorem is_local.mem_jacobson_or_exists_inv {I : ideal R} (hi : is_local I) (x : R) :
x ∈ jacobson I ∨ ∃ y, y * x - 1 ∈ I :=
classical.by_cases
(assume h : I ⊔ span {x} = ⊤,
let ⟨p, hpi, q, hq, hpq⟩ := submodule.mem_sup.1 ((eq_top_iff_one _).1 h) in
let ⟨r, hr⟩ := mem_span_singleton.1 hq in
or.inr ⟨r, by rw [← hpq, mul_comm, ← hr, ← neg_sub, add_sub_cancel]; exact I.neg_mem hpi⟩)
(assume h : I ⊔ span {x} ≠ ⊤,
or.inl $ le_trans le_sup_right (hi.le_jacobson le_sup_left h) $ mem_span_singleton.2 $ dvd_refl x)
end is_local
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⟩
theorem is_primary_of_is_maximal_radical {I : ideal R} (hi : is_maximal (radical I)) : is_primary I :=
have radical I = jacobson I,
from le_antisymm (le_Inf $ λ M ⟨him, hm⟩, hm.is_prime.radical_le_iff.2 him)
(Inf_le ⟨le_radical, hi⟩),
⟨ne_top_of_lt $ lt_of_le_of_lt le_radical (lt_top_iff_ne_top.2 hi.1),
λ x y hxy, ((is_local_of_is_maximal_radical hi).mem_jacobson_or_exists_inv y).symm.imp
(λ ⟨z, hz⟩, by rw [← mul_one x, ← sub_sub_cancel (z * y) 1, mul_sub, mul_left_comm]; exact
I.sub_mem (I.mul_mem_left hxy) (I.mul_mem_left hz))
(this ▸ id)⟩
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 inj_iff_ker_eq_bot : function.injective f ↔ ker f = ⊥ :=
by rw [submodule.ext'_iff, ker_eq]; exact is_add_group_hom.inj_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 [nonzero S] (f : R →+* S) : (1:R) ∉ ker f :=
by { rw [mem_ker, f.map_one], exact one_ne_zero }
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]
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 := smul_assoc,
one_smul := by simp,
zero_smul := bot_smul,
smul_zero := smul_bot }
end submodule
|
8034beb4ad8c0b4fcc43a8d9324046aa1272d4a7 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/section3.lean | 36620ea7159048cef4e042ff9f12de81bf1f2eb2 | [
"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 | 124 | lean | section
parameter (A : Type)
definition foo := A
noncomputable definition bar {X : Type} {A : X} : foo :=
sorry
end
|
6f69adcdd6fda4f42b676ba327b173cb2e30a318 | 86f6f4f8d827a196a32bfc646234b73328aeb306 | /examples/basics/unnamed_631.lean | 6c10fb6866ef553dd9cd3d667f06f7d505219e6d | [] | 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 | 205 | lean | import algebra.ring
namespace my_ring
variables {R : Type*} [ring R]
-- BEGIN
theorem neg_add_cancel_left (a b : R) : -a + (a + b) = b :=
by rw [←add_assoc, add_left_neg, zero_add]
-- END
end my_ring |
a79ef7ddf3016a34a5246a072e1c9b07e18fba9e | de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41 | /old/expressions/expression.lean | f35d6e905e20e41de3424e999c2f7303f9120a94 | [] | no_license | kevinsullivan/lang | d3e526ba363dc1ddf5ff1c2f36607d7f891806a7 | e9d869bff94fb13ad9262222a6f3c4aafba82d5e | refs/heads/master | 1,687,840,064,795 | 1,628,047,969,000 | 1,628,047,969,000 | 282,210,749 | 0 | 1 | null | 1,608,153,830,000 | 1,595,592,637,000 | Lean | UTF-8 | Lean | false | false | 79 | lean | structure var : Type :=
mk :: (num : ℕ)
structure expression : Type :=
mk :: |
bcf9bcfa03f55dca46ce87dd88151c015bef5300 | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/algebra/free_algebra.lean | d78afeff195c6fbe38a9e0147a14b5b8f07f8213 | [
"Apache-2.0"
] | permissive | abentkamp/mathlib | d9a75d291ec09f4637b0f30cc3880ffb07549ee5 | 5360e476391508e092b5a1e5210bd0ed22dc0755 | refs/heads/master | 1,682,382,954,948 | 1,622,106,077,000 | 1,622,106,077,000 | 149,285,665 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 16,020 | lean | /-
Copyright (c) 2020 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Adam Topaz
-/
import algebra.algebra.subalgebra
import algebra.monoid_algebra
import linear_algebra
import data.equiv.transfer_instance
/-!
# Free Algebras
Given a commutative semiring `R`, and a type `X`, we construct the free `R`-algebra on `X`.
## Notation
1. `free_algebra R X` is the free algebra itself. It is endowed with an `R`-algebra structure.
2. `free_algebra.ι R` is the function `X → free_algebra R X`.
3. Given a function `f : X → A` to an R-algebra `A`, `lift R f` is the lift of `f` to an
`R`-algebra morphism `free_algebra R X → A`.
## Theorems
1. `ι_comp_lift` states that the composition `(lift R f) ∘ (ι R)` is identical to `f`.
2. `lift_unique` states that whenever an R-algebra morphism `g : free_algebra R X → A` is
given whose composition with `ι R` is `f`, then one has `g = lift R f`.
3. `hom_ext` is a variant of `lift_unique` in the form of an extensionality theorem.
4. `lift_comp_ι` is a combination of `ι_comp_lift` and `lift_unique`. It states that the lift
of the composition of an algebra morphism with `ι` is the algebra morphism itself.
5. `equiv_monoid_algebra_free_monoid : free_algebra R X ≃ₐ[R] monoid_algebra R (free_monoid X)`
6. An inductive principle `induction`.
## Implementation details
We construct the free algebra on `X` as a quotient of an inductive type `free_algebra.pre` by an
inductively defined relation `free_algebra.rel`. Explicitly, the construction involves three steps:
1. We construct an inductive type `free_algebra.pre R X`, the terms of which should be thought
of as representatives for the elements of `free_algebra R X`.
It is the free type with maps from `R` and `X`, and with two binary operations `add` and `mul`.
2. We construct an inductive relation `free_algebra.rel R X` on `free_algebra.pre R X`.
This is the smallest relation for which the quotient is an `R`-algebra where addition resp.
multiplication are induced by `add` resp. `mul` from 1., and for which the map from `R` is the
structure map for the algebra.
3. The free algebra `free_algebra R X` is the quotient of `free_algebra.pre R X` by
the relation `free_algebra.rel R X`.
-/
variables (R : Type*) [comm_semiring R]
variables (X : Type*)
namespace free_algebra
/--
This inductive type is used to express representatives of the free algebra.
-/
inductive pre
| of : X → pre
| of_scalar : R → pre
| add : pre → pre → pre
| mul : pre → pre → pre
namespace pre
instance : inhabited (pre R X) := ⟨of_scalar 0⟩
-- Note: These instances are only used to simplify the notation.
/-- Coercion from `X` to `pre R X`. Note: Used for notation only. -/
def has_coe_generator : has_coe X (pre R X) := ⟨of⟩
/-- Coercion from `R` to `pre R X`. Note: Used for notation only. -/
def has_coe_semiring : has_coe R (pre R X) := ⟨of_scalar⟩
/-- Multiplication in `pre R X` defined as `pre.mul`. Note: Used for notation only. -/
def has_mul : has_mul (pre R X) := ⟨mul⟩
/-- Addition in `pre R X` defined as `pre.add`. Note: Used for notation only. -/
def has_add : has_add (pre R X) := ⟨add⟩
/-- Zero in `pre R X` defined as the image of `0` from `R`. Note: Used for notation only. -/
def has_zero : has_zero (pre R X) := ⟨of_scalar 0⟩
/-- One in `pre R X` defined as the image of `1` from `R`. Note: Used for notation only. -/
def has_one : has_one (pre R X) := ⟨of_scalar 1⟩
/--
Scalar multiplication defined as multiplication by the image of elements from `R`.
Note: Used for notation only.
-/
def has_scalar : has_scalar R (pre R X) := ⟨λ r m, mul (of_scalar r) m⟩
end pre
local attribute [instance]
pre.has_coe_generator pre.has_coe_semiring pre.has_mul pre.has_add pre.has_zero
pre.has_one pre.has_scalar
/--
Given a function from `X` to an `R`-algebra `A`, `lift_fun` provides a lift of `f` to a function
from `pre R X` to `A`. This is mainly used in the construction of `free_algebra.lift`.
-/
def lift_fun {A : Type*} [semiring A] [algebra R A] (f : X → A) : pre R X → A :=
λ t, pre.rec_on t f (algebra_map _ _) (λ _ _, (+)) (λ _ _, (*))
/--
An inductively defined relation on `pre R X` used to force the initial algebra structure on
the associated quotient.
-/
inductive rel : (pre R X) → (pre R X) → Prop
-- force `of_scalar` to be a central semiring morphism
| add_scalar {r s : R} : rel ↑(r + s) (↑r + ↑s)
| mul_scalar {r s : R} : rel ↑(r * s) (↑r * ↑s)
| central_scalar {r : R} {a : pre R X} : rel (r * a) (a * r)
-- commutative additive semigroup
| add_assoc {a b c : pre R X} : rel (a + b + c) (a + (b + c))
| add_comm {a b : pre R X} : rel (a + b) (b + a)
| zero_add {a : pre R X} : rel (0 + a) a
-- multiplicative monoid
| mul_assoc {a b c : pre R X} : rel (a * b * c) (a * (b * c))
| one_mul {a : pre R X} : rel (1 * a) a
| mul_one {a : pre R X} : rel (a * 1) a
-- distributivity
| left_distrib {a b c : pre R X} : rel (a * (b + c)) (a * b + a * c)
| right_distrib {a b c : pre R X} : rel ((a + b) * c) (a * c + b * c)
-- other relations needed for semiring
| zero_mul {a : pre R X} : rel (0 * a) 0
| mul_zero {a : pre R X} : rel (a * 0) 0
-- compatibility
| add_compat_left {a b c : pre R X} : rel a b → rel (a + c) (b + c)
| add_compat_right {a b c : pre R X} : rel a b → rel (c + a) (c + b)
| mul_compat_left {a b c : pre R X} : rel a b → rel (a * c) (b * c)
| mul_compat_right {a b c : pre R X} : rel a b → rel (c * a) (c * b)
end free_algebra
/--
The free algebra for the type `X` over the commutative semiring `R`.
-/
def free_algebra := quot (free_algebra.rel R X)
namespace free_algebra
local attribute [instance]
pre.has_coe_generator pre.has_coe_semiring pre.has_mul pre.has_add pre.has_zero
pre.has_one pre.has_scalar
instance : semiring (free_algebra R X) :=
{ add := quot.map₂ (+) (λ _ _ _, rel.add_compat_right) (λ _ _ _, rel.add_compat_left),
add_assoc := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact quot.sound rel.add_assoc },
zero := quot.mk _ 0,
zero_add := by { rintro ⟨⟩, exact quot.sound rel.zero_add },
add_zero := begin
rintros ⟨⟩,
change quot.mk _ _ = _,
rw [quot.sound rel.add_comm, quot.sound rel.zero_add],
end,
add_comm := by { rintros ⟨⟩ ⟨⟩, exact quot.sound rel.add_comm },
mul := quot.map₂ (*) (λ _ _ _, rel.mul_compat_right) (λ _ _ _, rel.mul_compat_left),
mul_assoc := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact quot.sound rel.mul_assoc },
one := quot.mk _ 1,
one_mul := by { rintros ⟨⟩, exact quot.sound rel.one_mul },
mul_one := by { rintros ⟨⟩, exact quot.sound rel.mul_one },
left_distrib := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact quot.sound rel.left_distrib },
right_distrib := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact quot.sound rel.right_distrib },
zero_mul := by { rintros ⟨⟩, exact quot.sound rel.zero_mul },
mul_zero := by { rintros ⟨⟩, exact quot.sound rel.mul_zero } }
instance : inhabited (free_algebra R X) := ⟨0⟩
instance : has_scalar R (free_algebra R X) :=
{ smul := λ r a, quot.lift_on a (λ x, quot.mk _ $ ↑r * x) $
λ a b h, quot.sound (rel.mul_compat_right h) }
instance : algebra R (free_algebra R X) :=
{ to_fun := λ r, quot.mk _ r,
map_one' := rfl,
map_mul' := λ _ _, quot.sound rel.mul_scalar,
map_zero' := rfl,
map_add' := λ _ _, quot.sound rel.add_scalar,
commutes' := λ _, by { rintros ⟨⟩, exact quot.sound rel.central_scalar },
smul_def' := λ _ _, rfl }
instance {S : Type*} [comm_ring S] : ring (free_algebra S X) := algebra.semiring_to_ring S
variables {X}
/--
The canonical function `X → free_algebra R X`.
-/
def ι : X → free_algebra R X := λ m, quot.mk _ m
@[simp] lemma quot_mk_eq_ι (m : X) : quot.mk (free_algebra.rel R X) m = ι R m := rfl
variables {A : Type*} [semiring A] [algebra R A]
/-- Internal definition used to define `lift` -/
private def lift_aux (f : X → A) : (free_algebra R X →ₐ[R] A) :=
{ to_fun := λ a, quot.lift_on a (lift_fun _ _ f) $ λ a b h,
begin
induction h,
{ exact (algebra_map R A).map_add h_r h_s, },
{ exact (algebra_map R A).map_mul h_r h_s },
{ apply algebra.commutes },
{ change _ + _ + _ = _ + (_ + _),
rw add_assoc },
{ change _ + _ = _ + _,
rw add_comm, },
{ change (algebra_map _ _ _) + lift_fun R X f _ = lift_fun R X f _,
simp, },
{ change _ * _ * _ = _ * (_ * _),
rw mul_assoc },
{ change (algebra_map _ _ _) * lift_fun R X f _ = lift_fun R X f _,
simp, },
{ change lift_fun R X f _ * (algebra_map _ _ _) = lift_fun R X f _,
simp, },
{ change _ * (_ + _) = _ * _ + _ * _,
rw left_distrib, },
{ change (_ + _) * _ = _ * _ + _ * _,
rw right_distrib, },
{ change (algebra_map _ _ _) * _ = algebra_map _ _ _,
simp },
{ change _ * (algebra_map _ _ _) = algebra_map _ _ _,
simp },
repeat { change lift_fun R X f _ + lift_fun R X f _ = _,
rw h_ih,
refl, },
repeat { change lift_fun R X f _ * lift_fun R X f _ = _,
rw h_ih,
refl, },
end,
map_one' := by { change algebra_map _ _ _ = _, simp },
map_mul' := by { rintros ⟨⟩ ⟨⟩, refl },
map_zero' := by { change algebra_map _ _ _ = _, simp },
map_add' := by { rintros ⟨⟩ ⟨⟩, refl },
commutes' := by tauto }
/--
Given a function `f : X → A` where `A` is an `R`-algebra, `lift R f` is the unique lift
of `f` to a morphism of `R`-algebras `free_algebra R X → A`.
-/
def lift : (X → A) ≃ (free_algebra R X →ₐ[R] A) :=
{ to_fun := lift_aux R,
inv_fun := λ F, F ∘ (ι R),
left_inv := λ f, by {ext, refl},
right_inv := λ F, by {
ext x,
rcases x,
induction x,
case pre.of : {
change ((F : free_algebra R X → A) ∘ (ι R)) _ = _,
refl },
case pre.of_scalar : {
change algebra_map _ _ x = F (algebra_map _ _ x),
rw alg_hom.commutes F x, },
case pre.add : a b ha hb {
change lift_aux R (F ∘ ι R) (quot.mk _ _ + quot.mk _ _) = F (quot.mk _ _ + quot.mk _ _),
rw [alg_hom.map_add, alg_hom.map_add, ha, hb], },
case pre.mul : a b ha hb {
change lift_aux R (F ∘ ι R) (quot.mk _ _ * quot.mk _ _) = F (quot.mk _ _ * quot.mk _ _),
rw [alg_hom.map_mul, alg_hom.map_mul, ha, hb], }, }, }
@[simp] lemma lift_aux_eq (f : X → A) : lift_aux R f = lift R f := rfl
@[simp]
lemma lift_symm_apply (F : free_algebra R X →ₐ[R] A) : (lift R).symm F = F ∘ (ι R) := rfl
variables {R X}
@[simp]
theorem ι_comp_lift (f : X → A) :
(lift R f : free_algebra R X → A) ∘ (ι R) = f := by {ext, refl}
@[simp]
theorem lift_ι_apply (f : X → A) (x) :
lift R f (ι R x) = f x := rfl
@[simp]
theorem lift_unique (f : X → A) (g : free_algebra R X →ₐ[R] A) :
(g : free_algebra R X → A) ∘ (ι R) = f ↔ g = lift R f :=
(lift R).symm_apply_eq
/-!
At this stage we set the basic definitions as `@[irreducible]`, so from this point onwards one
should only use the universal properties of the free algebra, and consider the actual implementation
as a quotient of an inductive type as completely hidden.
Of course, one still has the option to locally make these definitions `semireducible` if so desired,
and Lean is still willing in some circumstances to do unification based on the underlying
definition.
-/
attribute [irreducible] ι lift
-- Marking `free_algebra` irreducible makes `ring` instances inaccessible on quotients.
-- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/algebra.2Esemiring_to_ring.20breaks.20semimodule.20typeclass.20lookup/near/212580241
-- For now, we avoid this by not marking it irreducible.
@[simp]
theorem lift_comp_ι (g : free_algebra R X →ₐ[R] A) :
lift R ((g : free_algebra R X → A) ∘ (ι R)) = g :=
by { rw ←lift_symm_apply, exact (lift R).apply_symm_apply g }
/-- See note [partially-applied ext lemmas]. -/
@[ext]
theorem hom_ext {f g : free_algebra R X →ₐ[R] A}
(w : ((f : free_algebra R X → A) ∘ (ι R)) = ((g : free_algebra R X → A) ∘ (ι R))) : f = g :=
begin
rw [←lift_symm_apply, ←lift_symm_apply] at w,
exact (lift R).symm.injective w,
end
/--
The free algebra on `X` is "just" the monoid algebra on the free monoid on `X`.
This would be useful when constructing linear maps out of a free algebra,
for example.
-/
noncomputable
def equiv_monoid_algebra_free_monoid : free_algebra R X ≃ₐ[R] monoid_algebra R (free_monoid X) :=
alg_equiv.of_alg_hom
(lift R (λ x, (monoid_algebra.of R (free_monoid X)) (free_monoid.of x)))
((monoid_algebra.lift R (free_monoid X) (free_algebra R X)) (free_monoid.lift (ι R)))
begin
apply monoid_algebra.alg_hom_ext, intro x,
apply free_monoid.rec_on x,
{ simp, refl, },
{ intros x y ih, simp at ih, simp [ih], }
end
(by { ext, simp, })
instance [nontrivial R] : nontrivial (free_algebra R X) :=
equiv_monoid_algebra_free_monoid.to_equiv.nontrivial
section
open_locale classical
/-- The left-inverse of `algebra_map`. -/
def algebra_map_inv : free_algebra R X →ₐ[R] R :=
lift R (0 : X → R)
lemma algebra_map_left_inverse :
function.left_inverse algebra_map_inv (algebra_map R $ free_algebra R X) :=
λ x, by simp [algebra_map_inv]
-- this proof is copied from the approach in `free_abelian_group.of_injective`
lemma ι_injective [nontrivial R] : function.injective (ι R : X → free_algebra R X) :=
λ x y hoxy, classical.by_contradiction $ assume hxy : x ≠ y,
let f : free_algebra R X →ₐ[R] R := lift R (λ z, if x = z then (1 : R) else 0) in
have hfx1 : f (ι R x) = 1, from (lift_ι_apply _ _).trans $ if_pos rfl,
have hfy1 : f (ι R y) = 1, from hoxy ▸ hfx1,
have hfy0 : f (ι R y) = 0, from (lift_ι_apply _ _).trans $ if_neg hxy,
one_ne_zero $ hfy1.symm.trans hfy0
end
end free_algebra
/- There is something weird in the above namespace that breaks the typeclass resolution of
`has_coe_to_sort` below. Closing it and reopening it fixes it... -/
namespace free_algebra
/-- An induction principle for the free algebra.
If `C` holds for the `algebra_map` of `r : R` into `free_algebra R X`, the `ι` of `x : X`, and is
preserved under addition and muliplication, then it holds for all of `free_algebra R X`.
-/
@[elab_as_eliminator]
lemma induction {C : free_algebra R X → Prop}
(h_grade0 : ∀ r, C (algebra_map R (free_algebra R X) r))
(h_grade1 : ∀ x, C (ι R x))
(h_mul : ∀ a b, C a → C b → C (a * b))
(h_add : ∀ a b, C a → C b → C (a + b))
(a : free_algebra R X) :
C a :=
begin
-- the arguments are enough to construct a subalgebra, and a mapping into it from X
let s : subalgebra R (free_algebra R X) := {
carrier := C,
mul_mem' := h_mul,
add_mem' := h_add,
algebra_map_mem' := h_grade0, },
let of : X → s := subtype.coind (ι R) h_grade1,
-- the mapping through the subalgebra is the identity
have of_id : alg_hom.id R (free_algebra R X) = s.val.comp (lift R of),
{ ext,
simp [of, subtype.coind], },
-- finding a proof is finding an element of the subalgebra
convert subtype.prop (lift R of a),
simp [alg_hom.ext_iff] at of_id,
exact of_id a,
end
/-- The star ring formed by reversing the elements of products -/
instance : star_ring (free_algebra R X) :=
{ star := opposite.unop ∘ lift R (opposite.op ∘ ι R),
star_involutive := λ x, by {
unfold has_star.star,
simp only [function.comp_apply],
refine free_algebra.induction R X _ _ _ _ x; intros; simp [*] },
star_mul := λ a b, by simp,
star_add := λ a b, by simp }
@[simp]
lemma star_ι (x : X) : star (ι R x) = (ι R x) :=
by simp [star, has_star.star]
@[simp]
lemma star_algebra_map (r : R) : star (algebra_map R (free_algebra R X) r) = (algebra_map R _ r) :=
by simp [star, has_star.star]
/-- `star` as an `alg_equiv` -/
def star_hom : free_algebra R X ≃ₐ[R] (free_algebra R X)ᵒᵖ :=
{ commutes' := λ r, by simp [star_algebra_map],
..star_ring_equiv }
end free_algebra
|
f9eeaee9c94c33130f65971232d216f5e95faf4f | 7490bf5d40d31857a58062614642bb5a41c36154 | /dm_box_test.lean | 5ba9f9fad481c9e6f6b64d6b9bdafb41e5520c03 | [] | no_license | reesegrayallen/Lean-Discrete-Mathematics | 9f1d6fe1c814cc9264ce868a67adcf5a82566e22 | 00c875284613ea12e0a729f519738aab8599456b | refs/heads/main | 1,674,181,372,629 | 1,606,801,004,000 | 1,606,801,004,000 | 317,387,970 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 255 | lean | /-
HOMEWORK 3
Reese Allen (rga2uz)
CS2102 - Sullivan
-/
import .dm_box
def b1 := dm_box.mk 123
def b2 := dm_box.mk "stuck in a box"
def b3 := dm_box.mk (4,5)
def b4 := dm_box.mk tt
#reduce unbox b1
#eval unbox b2
#reduce unbox b3
#reduce unbox b4
|
10b060f644f6206133a236dd10f55e39773a000b | 6b45072eb2b3db3ecaace2a7a0241ce81f815787 | /data/nat/basic.lean | 805594b662d6242f51eada24d1c57198f87186a1 | [] | no_license | avigad/library_dev | 27b47257382667b5eb7e6476c4f5b0d685dd3ddc | 9d8ac7c7798ca550874e90fed585caad030bbfac | refs/heads/master | 1,610,452,468,791 | 1,500,712,839,000 | 1,500,713,478,000 | 69,311,142 | 1 | 0 | null | 1,474,942,903,000 | 1,474,942,902,000 | null | UTF-8 | Lean | false | false | 3,233 | lean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad
Basic operations on the natural numbers.
-/
import super.prover
open tactic
namespace nat
-- TODO(gabriel): can we drop addl?
/- a variant of add, defined by recursion on the first argument -/
definition addl : ℕ → ℕ → ℕ
| (succ x) y := succ (addl x y)
| 0 y := y
local infix ` ⊕ `:65 := addl
@[simp] lemma addl_zero_left (n : ℕ) : 0 ⊕ n = n := rfl
@[simp] lemma addl_succ_left (m n : ℕ) : succ m ⊕ n = succ (m ⊕ n) := rfl
@[simp] lemma zero_has_zero : nat.zero = 0 := rfl
local attribute [simp] nat.add_zero nat.add_succ nat.zero_add nat.succ_add
@[simp] theorem addl_zero_right (n : ℕ) : n ⊕ 0 = n :=
begin induction n, simp, simp [ih_1] end
@[simp] theorem addl_succ_right (n m : ℕ) : n ⊕ succ m = succ (n ⊕ m) :=
begin induction n, simp, simp [ih_1] end
@[simp] theorem add_eq_addl (x y : ℕ) : x ⊕ y = x + y :=
begin induction x, simp, simp [ih_1] end
/- successor and predecessor -/
theorem add_one_ne_zero (n : ℕ) : n + 1 ≠ 0 := succ_ne_zero _
theorem eq_zero_or_eq_succ_pred (n : ℕ) : n = 0 ∨ n = succ (pred n) :=
begin induction n, repeat { super } end
theorem exists_eq_succ_of_ne_zero {n : ℕ} (H : n ≠ 0) : ∃k : ℕ, n = succ k :=
by super with nat.eq_zero_or_eq_succ_pred
theorem succ_inj {n m : ℕ} (H : succ n = succ m) : n = m := by super
theorem discriminate {B : Type _} {n : ℕ} (H1: n = 0 → B) (H2 : ∀m, n = succ m → B) : B :=
begin cases n, super, super end
lemma one_succ_zero : 1 = succ 0 := rfl
local attribute [simp] one_succ_zero
theorem two_step_induction_on {P : ℕ → Prop} (a : ℕ) (H1 : P 0) (H2 : P 1)
(H3 : ∀ (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : P a :=
begin have stronger : P a ∧ P (succ a), induction a, repeat { super with nat.one_succ_zero } end
theorem sub_induction {P : ℕ → ℕ → Prop} (n m : ℕ) (H1 : ∀m, P 0 m)
(H2 : ∀n, P (succ n) 0) (H3 : ∀n m, P n m → P (succ n) (succ m)) : P n m :=
begin
have general : ∀m, P n m,
induction n, super with nat.one_succ_zero, intro, induction m_1,
repeat { super with nat.one_succ_zero }
end
/- addition -/
/-
Remark: we use 'local attributes' because in the end of the file
we show nat is a comm_semiring, and we will automatically inherit
the associated [simp] lemmas from algebra
-/
theorem succ_add_eq_succ_add (n m : ℕ) : succ n + m = n + succ m := by simp
local attribute [simp] nat.add_comm nat.add_assoc nat.add_left_comm
theorem eq_zero_and_eq_zero_of_add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 ∧ m = 0 :=
⟨nat.eq_zero_of_add_eq_zero_right H, nat.eq_zero_of_add_eq_zero_left H⟩
theorem add_one (n : ℕ) : n + 1 = succ n := rfl
-- TODO(gabriel): make add_one and one_add global simp lemmas?
local attribute [simp] add_one
theorem one_add (n : ℕ) : 1 + n = succ n := by simp
local attribute [simp] one_add
end nat
section
open nat
definition iterate {A : Type} (op : A → A) : ℕ → A → A
| 0 := λ a, a
| (succ k) := λ a, op (iterate k a)
notation f`^[`n`]` := iterate f n
end
|
b821418080f50d685da18fc2142b74ad58601458 | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/struct_inst_typed.lean | 646beb5126bcca15f48a4f07281cffcf0bfdbe2f | [
"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 | 212 | lean | new_frontend
#check { fst := 10, snd := 20 : Nat × Nat }
structure S :=
(x : Nat) (y : Bool) (z : String)
#check { x := 10, y := true, z := "hello" : S }
#check { fst := "hello", snd := "world" : Prod _ _ }
|
69e6bc24d18f7186d7c4bcdecd6c543947083386 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/102_lean3.lean | cd089225b0886f887a9547a876e0ccb89002c26a | [
"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 | 510 | lean | inductive time (D : Type) : Type
| start : time D
| after : (D → time D) → time D
def foo (C D : Type) : time D → (γ : Type) × (γ → C)
| time.start => ⟨C, id⟩
| time.after ts => ⟨(∀ (i : D), (foo C D $ ts i).1) × D, λ p => (foo C D $ ts p.2).2 $ p.1 p.2⟩
theorem fooEq1 (C D) : foo C D time.start = ⟨C, id⟩ :=
rfl
theorem fooEq2 (C D) (ts : D → time D): foo C D (time.after ts) = ⟨(∀ (i : D), (foo C D $ ts i).1) × D, λ p => (foo C D $ ts p.2).2 $ p.1 p.2⟩ :=
rfl
|
e8487cc5d8bc01fa003eb05d9a75bccc1658254a | 57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d | /tests/lean/inductionErrors.lean | b75b155422303ada134425430faded2abc4a160a | [
"Apache-2.0"
] | permissive | collares/lean4 | 861a9269c4592bce49b71059e232ff0bfe4594cc | 52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee | refs/heads/master | 1,691,419,031,324 | 1,618,678,138,000 | 1,618,678,138,000 | 358,989,750 | 0 | 0 | Apache-2.0 | 1,618,696,333,000 | 1,618,696,333,000 | null | UTF-8 | Lean | false | false | 2,475 | lean | universes u
axiom elimEx (motive : Nat → Nat → Sort u) (x y : Nat)
(diag : (a : Nat) → motive a a)
(upper : (delta a : Nat) → motive a (a + delta.succ))
(lower : (delta a : Nat) → motive (a + delta.succ) a)
: motive y x
theorem ex1 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl -- Error
| upper d => apply Or.inr -- Error
| diag => apply Or.inl; apply Nat.leRefl
theorem ex2 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx2 with -- Error
| lower d => apply Or.inl
| upper d => apply Or.inr
| diag => apply Or.inl; apply Nat.leRefl
theorem ex3 (p q : Nat) : p ≤ q ∨ p > q := by
cases p /- Error -/ using elimEx with
| lower d => apply Or.inl
| upper d => apply Or.inr
| diag => apply Or.inl; apply Nat.leRefl
theorem ex4 (p q : Nat) : p ≤ q ∨ p > q := by
cases p using Nat.add with -- Error
| lower d => apply Or.inl
| upper d => apply Or.inr
| diag => apply Or.inl; apply Nat.leRefl
theorem ex5 (x : Nat) : 0 + x = x := by
match x with
| 0 => done -- Error
| y+1 => done -- Error
theorem ex5b (x : Nat) : 0 + x = x := by
cases x with
| zero => done -- Error
| succ y => done -- Error
inductive Vec : Nat → Type
| nil : Vec 0
| cons : Bool → {n : Nat} → Vec n → Vec (n+1)
theorem ex6 (x : Vec 0) : x = Vec.nil := by
cases x using Vec.casesOn with
| nil => rfl
| cons => done -- Error
theorem ex7 (x : Vec 0) : x = Vec.nil := by
cases x with -- Error: TODO: improve error location
| nil => rfl
| cons => done
theorem ex8 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl; admit
| upper2 /- Error -/ d => apply Or.inr
| diag => apply Or.inl; apply Nat.leRefl
theorem ex9 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl; admit
| _ => apply Or.inr; admit
| diag => apply Or.inl; apply Nat.leRefl
theorem ex10 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl; admit
| upper d => apply Or.inr; admit
| diag => apply Or.inl; apply Nat.leRefl
| _ /- error unused -/ => admit
theorem ex11 (p q : Nat) : p ≤ q ∨ p > q := by
cases p, q using elimEx with
| lower d => apply Or.inl; admit
| upper d => apply Or.inr; admit
| lower d /- error unused -/ => apply Or.inl; admit
| diag => apply Or.inl; apply Nat.leRefl
|
ebce3f36dfbb90bbb29bf7a1dab3d8e5307b0c4b | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/essential_image.lean | 6715336149b3cee8fb405ca12075bf1c863abbb5 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,152 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.natural_isomorphism
import Mathlib.category_theory.full_subcategory
import Mathlib.PostPort
universes v₁ v₂ u₁ u₂ l
namespace Mathlib
/-!
# Essential image of a functor
The essential image `ess_image` of a functor consists of the objects in the target category which
are isomorphic to an object in the image of the object function.
This, for instance, allows us to talk about objects belonging to a subcategory expressed as a
functor rather than a subtype, preserving the principle of equivalence. For example this lets us
define exponential ideals.
The essential image can also be seen as a subcategory of the target category, and witnesses that
a functor decomposes into a essentially surjective functor and a fully faithful functor.
(TODO: show that this decomposition forms an orthogonal factorisation system).
-/
namespace category_theory
namespace functor
/--
The essential image of a functor `F` consists of those objects in the target category which are
isomorphic to an object in the image of the function `F.obj`. In other words, this is the closure
under isomorphism of the function `F.obj`.
This is the "non-evil" way of describing the image of a functor.
-/
def ess_image {C : Type u₁} {D : Type u₂} [category C] [category D] (F : C ⥤ D) : set D :=
fun (Y : D) => ∃ (X : C), Nonempty (obj F X ≅ Y)
/-- Get the witnessing object that `Y` is in the subcategory given by `F`. -/
def ess_image.witness {C : Type u₁} {D : Type u₂} [category C] [category D] {F : C ⥤ D} {Y : D} (h : Y ∈ ess_image F) : C :=
Exists.some h
/-- Extract the isomorphism between `F.obj h.witness` and `Y` itself. -/
def ess_image.get_iso {C : Type u₁} {D : Type u₂} [category C] [category D] {F : C ⥤ D} {Y : D} (h : Y ∈ ess_image F) : obj F (ess_image.witness h) ≅ Y :=
Classical.choice sorry
/-- Being in the essential image is a "hygenic" property: it is preserved under isomorphism. -/
theorem ess_image.of_iso {C : Type u₁} {D : Type u₂} [category C] [category D] {F : C ⥤ D} {Y : D} {Y' : D} (h : Y ≅ Y') (hY : Y ∈ ess_image F) : Y' ∈ ess_image F :=
Exists.imp (fun (B : C) => nonempty.map fun (_x : obj F B ≅ Y) => _x ≪≫ h) hY
/--
If `Y` is in the essential image of `F` then it is in the essential image of `F'` as long as
`F ≅ F'`.
-/
theorem ess_image.of_nat_iso {C : Type u₁} {D : Type u₂} [category C] [category D] {F : C ⥤ D} {F' : C ⥤ D} (h : F ≅ F') {Y : D} (hY : Y ∈ ess_image F) : Y ∈ ess_image F' :=
Exists.imp (fun (X : C) => nonempty.map fun (t : obj F X ≅ Y) => iso.app (iso.symm h) X ≪≫ t) hY
/-- Isomorphic functors have equal essential images. -/
theorem ess_image_eq_of_nat_iso {C : Type u₁} {D : Type u₂} [category C] [category D] {F : C ⥤ D} {F' : C ⥤ D} (h : F ≅ F') : ess_image F = ess_image F' :=
set.ext fun (A : D) => { mp := ess_image.of_nat_iso h, mpr := ess_image.of_nat_iso (iso.symm h) }
/-- An object in the image is in the essential image. -/
theorem obj_mem_ess_image {C : Type u₁} {D : Type u₂} [category C] [category D] (F : D ⥤ C) (Y : D) : obj F Y ∈ ess_image F :=
Exists.intro Y (Nonempty.intro (iso.refl (obj F Y)))
protected instance ess_image.category_theory.category {C : Type u₁} {D : Type u₂} [category C] [category D] {F : C ⥤ D} : category ↥(ess_image F) :=
category_theory.full_subcategory fun (x : D) => x ∈ ess_image F
/-- The essential image as a subcategory has a fully faithful inclusion into the target category. -/
@[simp] theorem ess_image_inclusion_obj {C : Type u₁} {D : Type u₂} [category C] [category D] (F : C ⥤ D) (c : Subtype fun (X : D) => (fun (x : D) => x ∈ ess_image F) X) : obj (ess_image_inclusion F) c = ↑c :=
Eq.refl ↑c
/--
Given a functor `F : C ⥤ D`, we have an (essentially surjective) functor from `C` to the essential
image of `F`.
-/
def to_ess_image {C : Type u₁} {D : Type u₂} [category C] [category D] (F : C ⥤ D) : C ⥤ ↥(ess_image F) :=
mk (fun (X : C) => { val := obj F X, property := obj_mem_ess_image F X })
fun (X Y : C) (f : X ⟶ Y) => preimage (ess_image_inclusion F) (map F f)
/--
The functor `F` factorises through its essential image, where the first functor is essentially
surjective and the second is fully faithful.
-/
@[simp] theorem to_ess_image_comp_essential_image_inclusion_hom_app {C : Type u₁} {D : Type u₂} [category C] [category D] {F : C ⥤ D} (X : C) : nat_trans.app (iso.hom to_ess_image_comp_essential_image_inclusion) X = 𝟙 :=
Eq.refl 𝟙
end functor
/--
A functor `F : C ⥤ D` is essentially surjective if every object of `D` is in the essential image
of `F`. In other words, for every `Y : D`, there is some `X : C` with `F.obj X ≅ Y`.
See https://stacks.math.columbia.edu/tag/001C.
-/
class ess_surj {C : Type u₁} {D : Type u₂} [category C] [category D] (F : C ⥤ D)
where
mem_ess_image : ∀ (Y : D), Y ∈ functor.ess_image F
protected instance functor.to_ess_image.ess_surj {C : Type u₁} {D : Type u₂} [category C] [category D] {F : C ⥤ D} : ess_surj (functor.to_ess_image F) :=
ess_surj.mk fun (_x : ↥(functor.ess_image F)) => sorry
/-- Given an essentially surjective functor, we can find a preimage for every object `Y` in the
codomain. Applying the functor to this preimage will yield an object isomorphic to `Y`, see
`obj_obj_preimage_iso`. -/
/-- Applying an essentially surjective functor to a preimage of `Y` yields an object that is
def functor.obj_preimage {C : Type u₁} {D : Type u₂} [category C] [category D] (F : C ⥤ D) [ess_surj F] (Y : D) : C :=
functor.ess_image.witness (ess_surj.mem_ess_image F Y)
isomorphic to `Y`. -/
def functor.obj_obj_preimage_iso {C : Type u₁} {D : Type u₂} [category C] [category D] (F : C ⥤ D) [ess_surj F] (Y : D) : functor.obj F (functor.obj_preimage F Y) ≅ Y :=
functor.ess_image.get_iso (ess_surj.mem_ess_image F Y)
|
49fc05fb7e9326245cb43b424b7671705b734b63 | e151e9053bfd6d71740066474fc500a087837323 | /src/hott/init/funext.lean | 4a066d523dec6cd0bf4ee86434518586dbc104ff | [
"Apache-2.0"
] | permissive | daniel-carranza/hott3 | 15bac2d90589dbb952ef15e74b2837722491963d | 913811e8a1371d3a5751d7d32ff9dec8aa6815d9 | refs/heads/master | 1,610,091,349,670 | 1,596,222,336,000 | 1,596,222,336,000 | 241,957,822 | 0 | 0 | Apache-2.0 | 1,582,222,839,000 | 1,582,222,838,000 | null | UTF-8 | Lean | false | false | 12,847 | lean | /-
Copyright (c) 2014 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
Ported from Coq HoTT
-/
import .trunc .equiv .ua
universes u v w l k
hott_theory
namespace hott
open hott.eq hott.is_trunc sigma function hott.is_equiv hott.equiv prod unit
/-
We now prove that funext follows from a couple of weaker-looking forms
of function extensionality.
This proof is originally due to Voevodsky; it has since been simplified
by Peter Lumsdaine and Michael Shulman.
-/
@[hott] def funext :=
Π ⦃A : Type l⦄ {P : A → Type k} (f g : Π x, P x), is_equiv (@apd10 A P f g)
-- Naive funext is the simple assertion that pointwise equal functions are equal.
@[hott] def naive_funext :=
Π ⦃A : Type _⦄ {P : A → Type _} (f g : Πx, P x), (f ~ g) → f = g
-- Weak funext says that a product of contractible types is contractible.
@[hott] def weak_funext :=
Π ⦃A : Type _⦄ (P : A → Type _) [H: Πx, is_contr (P x)], is_contr (Πx, P x)
@[hott] def weak_funext_of_naive_funext : naive_funext → weak_funext :=
begin
intros nf A P Pc,
unfreezeI,
let c := λ x, center (P x),
apply is_contr.mk c, intro f,
have eq' : (λx, center (P x)) ~ f := λ x, center_eq (f x),
have eq : (λx, center (P x)) = f := @nf A P (λx, center (P x)) f eq',
assumption
end
/-
The less obvious direction is that weak_funext implies funext
(and hence all three are logically equivalent). The point is that under weak
funext, the space of "pointwise homotopies" has the same universal property as
the space of paths.
-/
section
parameters [wf : weak_funext.{l k}] {A : Type.{l}} {B : A → Type.{k}} (f : Π x, B x)
@[hott] def is_contr_sigma_homotopy : is_contr (Σ (g : Π x, B x), f ~ g) :=
is_contr.mk (sigma.mk f (homotopy.refl f))
(λ ⟨(g : Π x, B x), (h : f ~ g)⟩,
let r := λ (k : Π x, Σ y, f x = y),
@sigma.mk _ (λg, f ~ g)
(λx, fst (k x)) (λx, snd (k x)) in
let s (g : Π x, B x) (h : f ~ g) (x) :=
@sigma.mk _ (λy, f x = y) (g x) (h x) in
have t1 : Πx, is_contr (Σ y, f x = y),
from (λx, is_contr_sigma_eq _),
have t2 : is_contr (Πx, Σ y, f x = y),
from wf _,
have t3 : (λ x, @sigma.mk _ (λ y, f x = y) (f x) idp) = s g h,
from @eq_of_is_contr (Π x, Σ y, f x = y) t2 _ _,
have t4 : r (λ x, sigma.mk (f x) idp) = r (s g h),
from ap r t3,
t4
)
local attribute [instance] is_contr_sigma_homotopy
parameters (Q : Π g (h : f ~ g), Type _) (d : Q f (homotopy.refl f))
@[hott] def homotopy_ind (g : Πx, B x) (h : f ~ g) : Q g h :=
@transport _ (λ gh, Q (sigma.fst gh) (snd gh)) (sigma.mk f (homotopy.refl f)) (sigma.mk g h)
(@eq_of_is_contr _ is_contr_sigma_homotopy _ _) d
-- local attribute weak_funext [reducible]
-- local attribute homotopy_ind [reducible]
@[hott] def homotopy_ind_comp : homotopy_ind f (homotopy.refl f) = d :=
begin
dsimp [homotopy_ind],
have := @prop_eq_of_is_contr _ _ _ _ (eq_of_is_contr _ _) idp,
rwr this, apply @is_contr_sigma_homotopy wf,
end
end
/- Now the proof is fairly easy; we can just use the same induction principle on both sides. -/
section
-- local attribute weak_funext [reducible]
@[hott] def funext_of_weak_funext (wf : weak_funext.{l k}) : funext.{l k} :=
begin
intros A B f g,
let eq_to_f := λ g' x, f = g',
let sim2path := @homotopy_ind wf _ _ f eq_to_f idp,
have t1 : sim2path f (homotopy.refl f) = idp,
by apply @homotopy_ind_comp wf,
have t2 : apd10 (sim2path f (homotopy.refl f)) = (homotopy.refl f),
from ap apd10 t1,
have left_inv : apd10 ∘ (sim2path g) ~ id,
from (@homotopy_ind wf _ _ f (λ g' x, apd10 (sim2path g' x) = x) t2) g,
have right_inv : @comp (f = g) (f ~ g) (f = g) (sim2path g) (@apd10 A B f g) ~ id,
{intro h, induction h, apply homotopy_ind_comp},
exact is_equiv.adjointify apd10 (sim2path g) left_inv right_inv
end
@[hott] def funext_from_naive_funext : naive_funext → funext :=
funext_of_weak_funext ∘ weak_funext_of_naive_funext
end
section
@[hott] private theorem ua_isequiv_postcompose {A B : Type.{l}} {C : Type u}
{w : A → B} [H0 : is_equiv w] : is_equiv (@comp C A B w) :=
let w' := equiv.mk w H0 in
let eqinv : A = B := ((@is_equiv.inv _ _ _ (univalence A B)) w') in
let eq' := equiv_of_eq eqinv in
have eqretr : eq' = w', from (@right_inv _ _ (@equiv_of_eq A B) (univalence A B) w'),
have invs_eq : eq'⁻¹ᶠ = w'⁻¹ᶠ, from inv_eq eq' w' eqretr,
is_equiv.adjointify (@comp C A B w)
(@comp C B A (is_equiv.inv w))
(λ (x : C → B),
have eqfin1 : Π(p : A = B), equiv_of_eq p ∘ ((equiv_of_eq p)⁻¹ᶠ ∘ x) = x,
by intro p; unfreezeI; induction p; reflexivity,
have eqfin : eq' ∘ (eq'⁻¹ᶠ ∘ x) = x,
from eqfin1 eqinv,
have eqfin' : w' ∘ (eq'⁻¹ᶠ ∘ x) = x,
by { refine _ ⬝ eqfin, rwr eqretr },
show w' ∘ (w'⁻¹ᶠ ∘ x) = x,
by { refine _ ⬝ eqfin', rwr invs_eq }
)
(λ (x : C → A),
have eqfin1 : Π(p : A = B), (equiv_of_eq p)⁻¹ᶠ ∘ (equiv_of_eq p ∘ x) = x,
by intro p; unfreezeI; induction p; reflexivity,
have eqfin : eq'⁻¹ᶠ ∘ (eq' ∘ x) = x,
from eqfin1 eqinv,
have eqfin' : eq'⁻¹ᶠ ∘ (w' ∘ x) = x,
by { refine _ ⬝ eqfin, rwr eqretr },
show w'⁻¹ᶠ ∘ (w' ∘ x) = x,
by { refine _ ⬝ eqfin', rwr invs_eq }
)
-- We are ready to prove functional extensionality,
-- starting with the naive non-dependent version.
@[hott] private def diagonal (B : Type _) : Type _
:= Σ xy : B × B, fst xy = snd xy
@[hott] private def isequiv_src_compose {A : Type u} {B : Type v}
: @is_equiv (A → diagonal B)
(A → B)
(comp (prod.fst ∘ sigma.fst)) :=
@ua_isequiv_postcompose (diagonal B) _ _ (prod.fst ∘ sigma.fst)
(is_equiv.adjointify (prod.fst ∘ sigma.fst)
(λ x : B, (sigma.mk (x , x) idp : diagonal B)) (λx, idp)
(λ ⟨⟨b, c⟩, p⟩, by { dsimp at p, induction p, refl }))
@[hott] private def isequiv_tgt_compose {A : Type u} {B : Type v}
: is_equiv (comp (prod.snd ∘ sigma.fst) : (A → diagonal B) → (A → B)) :=
@ua_isequiv_postcompose _ _ _ _ begin
fapply adjointify,
{ intro b, exact ⟨(b, b), idp⟩},
{ intro b, reflexivity},
{ intro a, induction a with q p, induction q, dsimp at *, induction p, reflexivity}
end
@[hott] theorem nondep_funext_from_ua {A : Type _} {B : Type _}
: Π {f g : A → B}, f ~ g → f = g :=
begin
intros f g p,
let d := λ (x : A), @sigma.mk (B × B) (λ (xy : B × B), xy.1 = xy.2) (f x , f x) (eq.refl (f x, f x).1),
let e := λ (x : A), @sigma.mk (B × B) (λ (xy : B × B), xy.1 = xy.2) (f x , g x) (p x),
let precomp1 : (A → diagonal B) → (A → B) := comp (prod.fst ∘ sigma.fst),
haveI equiv1 : is_equiv precomp1,
from @isequiv_src_compose A B,
haveI equiv2 : Π (x y : A → diagonal B), is_equiv (ap precomp1),
from is_equiv.is_equiv_ap precomp1,
haveI H' : Π (x y : A → diagonal B), prod.fst ∘ sigma.fst ∘ x = prod.fst ∘ sigma.fst ∘ y → x = y,
from (λ x y, is_equiv.inv (ap precomp1)),
have eq2 : prod.fst ∘ sigma.fst ∘ d = prod.fst ∘ sigma.fst ∘ e,
from idp,
have eq0 : d = e,
from H' d e eq2,
have eq1 : (prod.snd ∘ sigma.fst) ∘ d = (prod.snd ∘ sigma.fst) ∘ e,
from ap _ eq0,
exact eq1
end
end
-- Now we use this to prove weak funext, which as we know
-- implies (with dependent eta) also the strong dependent funext.
@[hott] theorem weak_funext_of_ua : weak_funext.{u v} :=
(λ (A : Type _) (P : A → Type _) allcontr,
let U := (λ (x : A), ulift unit) in
have pequiv : Π (x : A), P x ≃ unit,
from (λ x, @equiv_unit_of_is_contr (P x) (allcontr x)),
have psim : Π (x : A), P x = U x,
from (λ x, eq_of_equiv_lift (pequiv x)),
have p : P = U,
from @nondep_funext_from_ua A _ P U psim,
have tU' : is_contr (A → ulift unit),
from is_contr.mk (λ x, ulift.up ())
(λ f, nondep_funext_from_ua (λa, by induction (f a) with u;induction u;reflexivity)),
have tU : is_contr (Π x, U x),
from tU',
have tlast : is_contr (Πx, P x),
from transport (λ PU : A → Type v, is_contr $ Π x, PU x) p⁻¹ᵖ tU,
tlast)
-- we have proven function extensionality from the univalence axiom
@[hott] def funext_of_ua : funext :=
funext_of_weak_funext (@weak_funext_of_ua)
/-
We still take funext as an axiom, so that when you write "print axioms foo", you can see whether
it uses only function extensionality, and not also univalence.
-/
axiom function_extensionality : funext
variables {A : Type _} {P : A → Type _} {f g : Π x, P x}
namespace funext
@[instance, hott]
theorem is_equiv_apdt (f g : Π x, P x) : is_equiv (@apd10 A P f g) :=
function_extensionality f g
end funext
open funext
namespace eq
variables (f g)
@[hott] def eq_equiv_homotopy : (f = g) ≃ (f ~ g) :=
equiv.mk apd10 (by apply_instance)
variables {f g}
@[hott] def eq_of_homotopy : f ~ g → f = g :=
(@apd10 A P f g)⁻¹ᶠ
@[hott] def apd10_eq_of_homotopy (p : f ~ g) : apd10 (eq_of_homotopy p) = p :=
right_inv apd10 p
@[hott] def eq_of_homotopy_apd10 (p : f = g) : eq_of_homotopy (apd10 p) = p :=
left_inv apd10 p
@[hott] def eq_of_homotopy_idp (f : Π x, P x) : eq_of_homotopy (λx : A, idpath (f x)) = idpath f :=
is_equiv.left_inv apd10 idp
@[hott] def eq_of_homotopy_refl (f : Π x, P x) : eq_of_homotopy (homotopy.refl f) = idpath f :=
eq_of_homotopy_idp _
@[hott] def naive_funext_of_ua : naive_funext :=
λ A P f g h, eq_of_homotopy h
@[hott] protected def homotopy.rec_on {Q : (f ~ g) → Type _} (p : f ~ g)
(H : Π(q : f = g), Q (apd10 q)) : Q p :=
right_inv apd10 p ▸ H (eq_of_homotopy p)
@[hott] protected def homotopy.rec_on_idp {Q : Π{g}, (f ~ g) → Type _} {g : Π x, P x}
(p : f ~ g) (H : Q (homotopy.refl f)) : Q p :=
homotopy.rec_on p (λq, by induction q; exact H)
@[hott] protected def homotopy.rec_on' {f f' : Πa, P a} {Q : (f ~ f') → (f = f') → Type _}
(p : f ~ f') (H : Π(q : f = f'), Q (apd10 q) q) : Q p (eq_of_homotopy p) :=
begin
refine homotopy.rec_on p _,
intro q,
dunfold eq_of_homotopy, rwr left_inv apd10 q,
apply H
end
@[hott] protected def homotopy.rec_on_idp' {f : Πa, P a} {Q : Π{g}, (f ~ g) → (f = g) → Type _}
{g : Πa, P a} (p : f ~ g) (H : Q (homotopy.refl f) idp) : Q p (eq_of_homotopy p) :=
begin
refine homotopy.rec_on' p _, intro q, induction q, exact H
end
@[hott] protected def homotopy.rec_on_idp_left {A : Type _} {P : A → Type _} {g : Πa, P a}
{Q : Πf, (f ~ g) → Type _} {f : Π x, P x}
(p : f ~ g) (H : Q g (homotopy.refl g)) : Q f p :=
begin
refine homotopy.rec_on p (λ q, _), induction q, exact H
end
@[hott] def homotopy.rec_idp {A : Type _} {P : A → Type _} {f : Πa, P a}
(Q : Π{g}, (f ~ g) → Type _) (H : Q (homotopy.refl f)) {g : Π x, P x} (p : f ~ g) : Q p :=
homotopy.rec_on_idp p H
@[hott] def homotopy_rec_on_apd10 {A : Type _} {P : A → Type _} {f g : Πa, P a}
(Q : f ~ g → Type _) (H : Π(q : f = g), Q (apd10 q)) (p : f = g) :
homotopy.rec_on (apd10 p) H = H p :=
begin
dunfold homotopy.rec_on,
transitivity, rwr adj,
transitivity, symmetry; apply tr_compose,
apply apdt
end
@[hott] def homotopy_rec_idp_refl {A : Type _} {P : A → Type _} {f : Πa, P a}
(Q : Π{g}, f ~ g → Type _) (H : Q homotopy.rfl) :
homotopy.rec_idp @Q H homotopy.rfl = H :=
homotopy_rec_on_apd10 Q _ rfl
@[hott] def eq_of_homotopy_inv {f g : Π x, P x} (H : f ~ g)
: eq_of_homotopy (λx, (H x)⁻¹ᵖ) = (eq_of_homotopy H)⁻¹ :=
begin
apply homotopy.rec_on_idp H,
change eq_of_homotopy (λ x, idp) = _,
rwr [eq_of_homotopy_refl],
end
@[hott] def eq_of_homotopy_con {f g h : Π x, P x} (H1 : f ~ g) (H2 : g ~ h)
: eq_of_homotopy (λx, H1 x ⬝ H2 x) = eq_of_homotopy H1 ⬝ eq_of_homotopy H2 :=
begin
apply homotopy.rec_on_idp H2, apply homotopy.rec_on_idp H1,
rwr eq_of_homotopy_refl, apply eq_of_homotopy_idp,
end
@[hott] def compose_eq_of_homotopy {A B C : Type _} (g : B → C) {f f' : A → B} (H : f ~ f') :
ap (λf : A → B, g ∘ f) (eq_of_homotopy H) = eq_of_homotopy (hwhisker_left g H) :=
begin
apply homotopy.rec_on_idp H, rwr eq_of_homotopy_refl,
symmetry, apply eq_of_homotopy_idp
end
end eq
end hott |
67b56df6812fec495da77d0b5d24fe7f5ce13387 | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /src/Lean/Meta/Tactic/Replace.lean | 41a58315bcb736324fefd6ad3d855ced05cebd98 | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,146 | 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.Tactic.Util
import Lean.Meta.Tactic.Revert
import Lean.Meta.Tactic.Intro
import Lean.Meta.Tactic.Clear
import Lean.Meta.Tactic.Assert
namespace Lean.Meta
/--
Convert the given goal `Ctx |- target` into `Ctx |- targetNew` using an equality proof `eqProof : target = targetNew`.
It assumes `eqProof` has type `target = targetNew` -/
def replaceTargetEq (mvarId : MVarId) (targetNew : Expr) (eqProof : Expr) : MetaM MVarId :=
withMVarContext mvarId do
checkNotAssigned mvarId `replaceTarget
let tag ← getMVarTag mvarId
let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag
let target ← getMVarType mvarId
let u ← getLevel target
let eq ← mkEq target targetNew
let newProof ← mkExpectedTypeHint eqProof eq
let val := mkAppN (Lean.mkConst `Eq.mpr [u]) #[target, targetNew, eqProof, mvarNew]
assignExprMVar mvarId val
pure mvarNew.mvarId!
/--
Convert the given goal `Ctx | target` into `Ctx |- targetNew`. It assumes the goals are definitionally equal.
We use the proof term
```
@id target mvarNew
```
to create a checkpoint. -/
def replaceTargetDefEq (mvarId : MVarId) (targetNew : Expr) : MetaM MVarId :=
withMVarContext mvarId do
checkNotAssigned mvarId `change
let target ← getMVarType mvarId
if target == targetNew then pure mvarId
else
let tag ← getMVarTag mvarId
let mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag
let newVal ← mkExpectedTypeHint mvarNew target
assignExprMVar mvarId mvarNew
pure mvarNew.mvarId!
/--
Replace type of the local declaration with id `fvarId` with one with the same user-facing name, but with type `typeNew`.
This method assumes `eqProof` is a proof that type of `fvarId` is equal to `typeNew`.
This tactic actually adds a new declaration and (try to) clear the old one.
If the old one cannot be cleared, then at least its user-facing name becomes inaccessible.
Remark: the new declaration is added immediately after `fvarId`.
`typeNew` must be well-formed at `fvarId`, but `eqProof` may contain variables declared after `fvarId`. -/
def replaceLocalDecl (mvarId : MVarId) (fvarId : FVarId) (typeNew : Expr) (eqProof : Expr) : MetaM AssertAfterResult :=
withMVarContext mvarId do
let localDecl ← getLocalDecl fvarId
let typeNewPr ← mkEqMP eqProof (mkFVar fvarId)
let result ← assertAfter mvarId localDecl.fvarId localDecl.userName typeNew typeNewPr
(do let mvarIdNew ← clear result.mvarId fvarId
pure { result with mvarId := mvarIdNew })
<|> pure result
def change (mvarId : MVarId) (targetNew : Expr) : MetaM MVarId := withMVarContext mvarId do
let target ← getMVarType mvarId
unless (← isDefEq target targetNew) do
throwTacticEx `change mvarId msg!"given type{indentExpr targetNew}\nis not definitionally equal to{indentExpr target}"
replaceTargetDefEq mvarId targetNew
def changeLocalDecl (mvarId : MVarId) (fvarId : FVarId) (typeNew : Expr) : MetaM MVarId := do
checkNotAssigned mvarId `changeLocalDecl
let (xs, mvarId) ← revert mvarId #[fvarId] true
withMVarContext mvarId do
let numReverted := xs.size
let target ← getMVarType mvarId
let checkDefEq (typeOld : Expr) : MetaM Unit := do
unless (← isDefEq typeNew typeOld) do
throwTacticEx `changeHypothesis mvarId msg!"given type{indentExpr typeNew}\nis not definitionally equal to{indentExpr typeOld}"
let finalize (targetNew : Expr) : MetaM MVarId := do
let mvarId ← replaceTargetDefEq mvarId targetNew
let (_, mvarId) ← introNP mvarId (numReverted-1)
pure mvarId
match target with
| Expr.forallE n d b c => do checkDefEq d; finalize (mkForall n c.binderInfo typeNew b)
| Expr.letE n t v b _ => do checkDefEq t; finalize (mkLet n typeNew v b)
| _ => throwTacticEx `changeHypothesis mvarId "unexpected auxiliary target"
end Lean.Meta
|
b7ffe31de05cf215c4a122b92a2a9e5a07056dba | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/mllist.lean | 8fa1b232225cf73c9b97939ddcda1164d16e0095 | [
"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 | 8,179 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Keeley Hoek, Simon Hudon, Scott Morrison
-/
import data.option.defs
/-! # Monadic lazy lists.
An alternative construction of lazy lists (see also `data.lazy_list`),
with "lazyness" controlled by an arbitrary monad.
The inductive construction is not allowed outside of meta (indeed, we can build infinite objects).
This isn't so bad, as the typical use is with the tactic monad, in any case.
As we're in meta anyway, we don't bother with proofs about these constructions.
-/
universes u v
namespace tactic -- We hide this away in the tactic namespace, just because it's all meta.
/-- A monadic lazy list, controlled by an arbitrary monad. -/
meta inductive mllist (m : Type u → Type u) (α : Type u) : Type u
| nil : mllist
| cons : m (option α × mllist) → mllist
namespace mllist
variables {α β : Type u} {m : Type u → Type u}
/-- Construct an `mllist` recursively. -/
meta def fix [alternative m] (f : α → m α) : α → mllist m α
| x := cons $ (λ a, (some x, fix a)) <$> f x <|> pure (some x, nil)
variables [monad m]
/-- Repeatedly apply a function `f : α → m (α × list β)` to an initial `a : α`,
accumulating the elements of the resulting `list β` as a single monadic lazy list.
(This variant allows starting with a specified `list β` of elements, as well. )-/
meta def fixl_with [alternative m] (f : α → m (α × list β)) : α → list β → mllist m β
| s (b :: rest) := cons $ pure (some b, fixl_with s rest)
| s [] := cons $ do
{ (s', l) ← f s,
match l with
| (b :: rest) := pure (some b, fixl_with s' rest)
| [] := pure (none, fixl_with s' [])
end }
<|> pure (none, nil)
/-- Repeatedly apply a function `f : α → m (α × list β)` to an initial `a : α`,
accumulating the elements of the resulting `list β` as a single monadic lazy list. -/
meta def fixl [alternative m] (f : α → m (α × list β)) (s : α) : mllist m β := fixl_with f s []
/-- Deconstruct an `mllist`, returning inside the monad an optional pair `α × mllist m α`
representing the head and tail of the list. -/
meta def uncons {α : Type u} : mllist m α → m (option (α × mllist m α))
| nil := pure none
| (cons l) := do (x, xs) ← l,
some x ← return x | uncons xs,
return (x, xs)
/-- Compute, inside the monad, whether an `mllist` is empty. -/
meta def empty {α : Type u} (xs : mllist m α) : m (ulift bool) :=
(ulift.up ∘ option.is_some) <$> uncons xs
/-- Convert a `list` to an `mllist`. -/
meta def of_list {α : Type u} : list α → mllist m α
| [] := nil
| (h :: t) := cons (pure (h, of_list t))
/-- Convert a `list` of values inside the monad into an `mllist`. -/
meta def m_of_list {α : Type u} : list (m α) → mllist m α
| [] := nil
| (h :: t) := cons ((λ x, (x, m_of_list t)) <$> some <$> h)
/-- Extract a list inside the monad from an `mllist`. -/
meta def force {α} : mllist m α → m (list α)
| nil := pure []
| (cons l) :=
do (x, xs) ← l,
some x ← pure x | force xs,
(::) x <$> (force xs)
/-- Take the first `n` elements, as a list inside the monad. -/
meta def take {α} : mllist m α → ℕ → m (list α)
| nil _ := pure []
| _ 0 := pure []
| (cons l) (n+1) :=
do (x, xs) ← l,
some x ← pure x | take xs (n+1),
(::) x <$> (take xs n)
/-- Apply a function to every element of an `mllist`. -/
meta def map {α β : Type u} (f : α → β) : mllist m α → mllist m β
| nil := nil
| (cons l) := cons $ do (x, xs) ← l, pure (f <$> x, map xs)
/-- Apply a function which returns values in the monad to every element of an `mllist`. -/
meta def mmap {α β : Type u} (f : α → m β) : mllist m α → mllist m β
| nil := nil
| (cons l) :=
cons $ do (x, xs) ← l,
b ← x.traverse f,
return (b, mmap xs)
/-- Filter a `mllist`. -/
meta def filter {α : Type u} (p : α → Prop) [decidable_pred p] : mllist m α → mllist m α
| nil := nil
| (cons l) :=
cons $ do (a, r) ← l,
some a ← return a | return (none, filter r),
return (if p a then some a else none, filter r)
/-- Filter a `mllist` using a function which returns values in the (alternative) monad.
Whenever the function "succeeds", we accept the element, and reject otherwise. -/
meta def mfilter [alternative m] {α β : Type u} (p : α → m β) : mllist m α → mllist m α
| nil := nil
| (cons l) :=
cons $ do (a, r) ← l,
some a ← return a | return (none, mfilter r),
(p a >> return (a, mfilter r)) <|> return (none , mfilter r)
/-- Filter and transform a `mllist` using an `option` valued function. -/
meta def filter_map {α β : Type u} (f : α → option β) : mllist m α → mllist m β
| nil := nil
| (cons l) :=
cons $ do (a, r) ← l,
some a ← return a | return (none, filter_map r),
match f a with
| (some b) := return (some b, filter_map r)
| none := return (none, filter_map r)
end
/-- Filter and transform a `mllist` using a function that returns values inside the monad.
We discard elements where the function fails. -/
meta def mfilter_map [alternative m] {α β : Type u} (f : α → m β) : mllist m α → mllist m β
| nil := nil
| (cons l) :=
cons $ do (a, r) ← l,
some a ← return a | return (none, mfilter_map r),
(f a >>= (λ b, return (some b, mfilter_map r))) <|> return (none, mfilter_map r)
/-- Concatenate two monadic lazty lists. -/
meta def append {α : Type u} : mllist m α → mllist m α → mllist m α
| nil ys := ys
| (cons xs) ys :=
cons $ do (x, xs) ← xs,
return (x, append xs ys)
/-- Join a monadic lazy list of monadic lazy lists into a single monadic lazy list. -/
meta def join {α : Type u} : mllist m (mllist m α) → mllist m α
| nil := nil
| (cons l) :=
cons $ do (xs,r) ← l,
some xs ← return xs | return (none, join r),
match xs with
| nil := return (none, join r)
| cons m := do (a,n) ← m, return (a, join (cons $ return (n, r)))
end
/-- Lift a monadic lazy list inside the monad to a monadic lazy list. -/
meta def squash {α} (t : m (mllist m α)) : mllist m α :=
(mllist.m_of_list [t]).join
/-- Enumerate the elements of a monadic lazy list, starting at a specified offset. -/
meta def enum_from {α : Type u} : ℕ → mllist m α → mllist m (ℕ × α)
| _ nil := nil
| n (cons l) :=
cons $ do (a, r) ← l,
some a ← return a | return (none, enum_from n r),
return ((n, a), (enum_from (n + 1) r))
/-- Enumerate the elements of a monadic lazy list. -/
meta def enum {α : Type u} : mllist m α → mllist m (ℕ × α) := enum_from 0
/-- The infinite monadic lazy list of natural numbers.-/
meta def range {m : Type → Type} [alternative m] : mllist m ℕ := mllist.fix (λ n, pure (n + 1)) 0
/-- Add one element to the end of a monadic lazy list. -/
meta def concat {α : Type u} : mllist m α → α → mllist m α
| L a := (mllist.of_list [L, mllist.of_list [a]]).join
/-- Apply a function returning a monadic lazy list to each element of a monadic lazy list,
joining the results. -/
meta def bind_ {α β : Type u} : mllist m α → (α → mllist m β) → mllist m β
| nil f := nil
| (cons ll) f :=
cons $ do (x, xs) ← ll,
some x ← return x | return (none, bind_ xs f),
return (none, append (f x) (bind_ xs f))
/-- Convert any value in the monad to the singleton monadic lazy list. -/
meta def monad_lift {α} (x : m α) : mllist m α := cons $ (flip prod.mk nil ∘ some) <$> x
/-- Return the head of a monadic lazy list, as a value in the monad. -/
meta def head [alternative m] {α} (L : mllist m α) : m α :=
do some (r, _) ← L.uncons | failure,
return r
/-- Apply a function returning values inside the monad to a monadic lazy list,
returning only the first successful result. -/
meta def mfirst [alternative m] {α β} (L : mllist m α) (f : α → m β) : m β :=
(L.mfilter_map f).head
end mllist
end tactic
|
9ca85ba4a61059e28e651827afc4db3cbe2f1b0a | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /_target/deps/mathlib/src/measure_theory/content.lean | e4a7962137362fe27aa458f932668057e01ef54d | [
"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 | 12,393 | lean | /-
Copyright (c) 2020 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import measure_theory.measure_space
import measure_theory.borel_space
import topology.opens
import topology.compacts
/-!
# Contents
In this file we work with *contents*. A content `λ` is a function from a certain class of subsets
(such as the the compact subsets) to `ennreal` (or `ℝ≥0`) that is
* additive: If `K₁` and `K₂` are disjoint sets in the domain of `λ`,
then `λ(K₁ ∪ K₂) = λ(K₁) + λ(K₂)`;
* subadditive: If `K₁` and `K₂` are in the domain of `λ`, then `λ(K₁ ∪ K₂) ≤ λ(K₁) + λ(K₂)`;
* monotone: If `K₁ ⊆ K₂` are in the domain of `λ`, then `λ(K₁) ≤ λ(K₂)`.
We show that:
* Given a content `λ` on compact sets, we get a countably subadditive map that vanishes at `∅`.
In Halmos (1950) this is called the *inner content* `λ*` of `λ`.
* Given an inner content, we define an outer measure.
We don't explicitly define the type of contents.
In this file we only work on contents on compact sets, and inner contents on open sets, and both
contents and inner contents map into the extended nonnegative reals. However, in other applications
other choices can be made, and it is not a priori clear what the best interface should be.
## Main definitions
* `measure_theory.inner_content`: define an inner content from an content
* `measure_theory.outer_measure.of_content`: construct an outer measure from a content
## References
* Paul Halmos (1950), Measure Theory, §53
* https://en.wikipedia.org/wiki/Content_(measure_theory)
-/
universe variables u v w
noncomputable theory
open set topological_space
open_locale nnreal
namespace measure_theory
variables {G : Type w} [topological_space G]
/-- Constructing the inner content of a content. From a content defined on the compact sets, we
obtain a function defined on all open sets, by taking the supremum of the content of all compact
subsets. -/
def inner_content (μ : compacts G → ennreal) (U : opens G) : ennreal :=
⨆ (K : compacts G) (h : K.1 ⊆ U), μ K
lemma le_inner_content {μ : compacts G → ennreal} (K : compacts G) (U : opens G)
(h2 : K.1 ⊆ U) : μ K ≤ inner_content μ U :=
le_supr_of_le K $ le_supr _ h2
lemma inner_content_le {μ : compacts G → ennreal}
(h : ∀ (K₁ K₂ : compacts G), K₁.1 ⊆ K₂.1 → μ K₁ ≤ μ K₂)
(U : opens G) (K : compacts G)
(h2 : (U : set G) ⊆ K.1) : inner_content μ U ≤ μ K :=
bsupr_le $ λ K' hK', h _ _ (subset.trans hK' h2)
lemma inner_content_of_is_compact {μ : compacts G → ennreal}
(h : ∀ (K₁ K₂ : compacts G), K₁.1 ⊆ K₂.1 → μ K₁ ≤ μ K₂) {K : set G} (h1K : is_compact K)
(h2K : is_open K) : inner_content μ ⟨K, h2K⟩ = μ ⟨K, h1K⟩ :=
le_antisymm (bsupr_le $ λ K' hK', h _ ⟨K, h1K⟩ hK')
(le_inner_content _ _ subset.rfl)
lemma inner_content_empty {μ : compacts G → ennreal} (h : μ ⊥ = 0) :
inner_content μ ∅ = 0 :=
begin
refine le_antisymm _ (zero_le _), rw ←h,
refine bsupr_le (λ K hK, _),
have : K = ⊥, { ext1, rw [subset_empty_iff.mp hK, compacts.bot_val] }, rw this, refl'
end
/-- This is "unbundled", because that it required for the API of `induced_outer_measure`. -/
lemma inner_content_mono {μ : compacts G → ennreal} ⦃U V : set G⦄ (hU : is_open U) (hV : is_open V)
(h2 : U ⊆ V) : inner_content μ ⟨U, hU⟩ ≤ inner_content μ ⟨V, hV⟩ :=
supr_le_supr $ λ K, supr_le_supr_const $ λ hK, subset.trans hK h2
lemma inner_content_exists_compact {μ : compacts G → ennreal} {U : opens G}
(hU : inner_content μ U < ⊤) {ε : ℝ≥0} (hε : 0 < ε) :
∃ K : compacts G, K.1 ⊆ U ∧ inner_content μ U ≤ μ K + ε :=
begin
have h'ε := ennreal.zero_lt_coe_iff.2 hε,
cases le_or_lt (inner_content μ U) ε,
{ exact ⟨⊥, empty_subset _, le_trans h (le_add_of_nonneg_left (zero_le _))⟩ },
have := ennreal.sub_lt_sub_self (ne_of_lt hU) (ne_of_gt $ lt_trans h'ε h) h'ε,
conv at this {to_rhs, rw inner_content }, simp only [lt_supr_iff] at this,
rcases this with ⟨U, h1U, h2U⟩, refine ⟨U, h1U, _⟩,
rw [← ennreal.sub_le_iff_le_add], exact le_of_lt h2U
end
/-- The inner content of a supremum of opens is at most the sum of the individual inner
contents. -/
lemma inner_content_Sup_nat [t2_space G] {μ : compacts G → ennreal}
(h1 : μ ⊥ = 0)
(h2 : ∀ (K₁ K₂ : compacts G), μ (K₁ ⊔ K₂) ≤ μ K₁ + μ K₂) (U : ℕ → opens G) :
inner_content μ (⨆ (i : ℕ), U i) ≤ ∑' (i : ℕ), inner_content μ (U i) :=
begin
have h3 : ∀ (t : finset ℕ) (K : ℕ → compacts G), μ (t.sup K) ≤ t.sum (λ i, μ (K i)),
{ intros t K, refine finset.induction_on t _ _,
{ simp only [h1, nonpos_iff_eq_zero, finset.sum_empty, finset.sup_empty] },
{ intros n s hn ih, rw [finset.sup_insert, finset.sum_insert hn],
exact le_trans (h2 _ _) (add_le_add_left ih _) }},
refine bsupr_le (λ K hK, _),
rcases is_compact.elim_finite_subcover K.2 _ (λ i, (U i).prop) _ with ⟨t, ht⟩, swap,
{ convert hK, rw [opens.supr_def, subtype.coe_mk] },
rcases K.2.finite_compact_cover t (coe ∘ U) (λ i _, (U _).prop) (by simp only [ht])
with ⟨K', h1K', h2K', h3K'⟩,
let L : ℕ → compacts G := λ n, ⟨K' n, h1K' n⟩,
convert le_trans (h3 t L) _,
{ ext1, rw [h3K', compacts.finset_sup_val, finset.sup_eq_supr] },
refine le_trans (finset.sum_le_sum _) (ennreal.sum_le_tsum t),
intros i hi, refine le_trans _ (le_supr _ (L i)),
refine le_trans _ (le_supr _ (h2K' i)), refl'
end
/-- The inner content of a union of sets is at most the sum of the individual inner contents.
This is the "unbundled" version of `inner_content_Sup_nat`.
It required for the API of `induced_outer_measure`. -/
lemma inner_content_Union_nat [t2_space G] {μ : compacts G → ennreal}
(h1 : μ ⊥ = 0)
(h2 : ∀ (K₁ K₂ : compacts G), μ (K₁ ⊔ K₂) ≤ μ K₁ + μ K₂)
⦃U : ℕ → set G⦄ (hU : ∀ (i : ℕ), is_open (U i)) :
inner_content μ ⟨⋃ (i : ℕ), U i, is_open_Union hU⟩ ≤ ∑' (i : ℕ), inner_content μ ⟨U i, hU i⟩ :=
by { have := inner_content_Sup_nat h1 h2 (λ i, ⟨U i, hU i⟩), rwa [opens.supr_def] at this }
lemma inner_content_comap {μ : compacts G → ennreal} (f : G ≃ₜ G)
(h : ∀ ⦃K : compacts G⦄, μ (K.map f f.continuous) = μ K) (U : opens G) :
inner_content μ (U.comap f.continuous) = inner_content μ U :=
begin
refine supr_congr _ ((compacts.equiv f).surjective) _,
intro K, refine supr_congr_Prop image_subset_iff _,
intro hK, simp only [equiv.coe_fn_mk, subtype.mk_eq_mk, ennreal.coe_eq_coe, compacts.equiv],
apply h,
end
lemma is_left_invariant_inner_content [group G] [topological_group G] {μ : compacts G → ennreal}
(h : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (g : G)
(U : opens G) : inner_content μ (U.comap $ continuous_mul_left g) = inner_content μ U :=
by convert inner_content_comap (homeomorph.mul_left g) (λ K, h g) U
lemma inner_content_pos [t2_space G] [group G] [topological_group G] {μ : compacts G → ennreal}
(h1 : μ ⊥ = 0)
(h2 : ∀ (K₁ K₂ : compacts G), μ (K₁ ⊔ K₂) ≤ μ K₁ + μ K₂)
(h3 : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K)
(K : compacts G) (hK : 0 < μ K) (U : opens G) (hU : (U : set G).nonempty) :
0 < inner_content μ U :=
begin
have : (interior (U : set G)).nonempty, rwa [U.prop.interior_eq],
rcases compact_covered_by_mul_left_translates K.2 this with ⟨s, hs⟩,
suffices : μ K ≤ s.card * inner_content μ U,
{ exact (ennreal.mul_pos.mp $ lt_of_lt_of_le hK this).2 },
have : K.1 ⊆ ↑⨆ (g ∈ s), U.comap $ continuous_mul_left g,
{ simpa only [opens.supr_def, opens.coe_comap, subtype.coe_mk] },
refine (le_inner_content _ _ this).trans _,
refine (rel_supr_sum (inner_content μ) (inner_content_empty h1) (≤)
(inner_content_Sup_nat h1 h2) _ _).trans _,
simp only [is_left_invariant_inner_content h3, finset.sum_const, nsmul_eq_mul, le_refl]
end
lemma inner_content_mono' {μ : compacts G → ennreal} ⦃U V : set G⦄
(hU : is_open U) (hV : is_open V) (h2 : U ⊆ V) :
inner_content μ ⟨U, hU⟩ ≤ inner_content μ ⟨V, hV⟩ :=
supr_le_supr $ λ K, supr_le_supr_const $ λ hK, subset.trans hK h2
namespace outer_measure
/-- Extending a content on compact sets to an outer measure on all sets. -/
def of_content [t2_space G] (μ : compacts G → ennreal) (h1 : μ ⊥ = 0) : outer_measure G :=
induced_outer_measure (λ U hU, inner_content μ ⟨U, hU⟩) is_open_empty (inner_content_empty h1)
variables [t2_space G] {μ : compacts G → ennreal} {h1 : μ ⊥ = 0}
(h2 : ∀ (K₁ K₂ : compacts G), μ (K₁ ⊔ K₂) ≤ μ K₁ + μ K₂)
include h2
lemma of_content_opens (U : opens G) : of_content μ h1 U = inner_content μ U :=
induced_outer_measure_eq' (λ _, is_open_Union) (inner_content_Union_nat h1 h2)
inner_content_mono U.2
lemma of_content_le (h : ∀ (K₁ K₂ : compacts G), K₁.1 ⊆ K₂.1 → μ K₁ ≤ μ K₂)
(U : opens G) (K : compacts G) (hUK : (U : set G) ⊆ K.1) : of_content μ h1 U ≤ μ K :=
(of_content_opens h2 U).le.trans $ inner_content_le h U K hUK
lemma le_of_content_compacts (K : compacts G) : μ K ≤ of_content μ h1 K.1 :=
begin
rw [of_content, induced_outer_measure_eq_infi],
{ exact le_infi (λ U, le_infi $ λ hU, le_infi $ le_inner_content K ⟨U, hU⟩) },
{ exact inner_content_Union_nat h1 h2 },
{ exact inner_content_mono }
end
lemma of_content_eq_infi (A : set G) :
of_content μ h1 A = ⨅ (U : set G) (hU : is_open U) (h : A ⊆ U), inner_content μ ⟨U, hU⟩ :=
induced_outer_measure_eq_infi _ (inner_content_Union_nat h1 h2) inner_content_mono A
lemma of_content_interior_compacts (h3 : ∀ (K₁ K₂ : compacts G), K₁.1 ⊆ K₂.1 → μ K₁ ≤ μ K₂)
(K : compacts G) : of_content μ h1 (interior K.1) ≤ μ K :=
le_trans (le_of_eq $ of_content_opens h2 (opens.interior K.1))
(inner_content_le h3 _ _ interior_subset)
lemma of_content_exists_compact {U : opens G} (hU : of_content μ h1 U < ⊤) {ε : ℝ≥0}
(hε : 0 < ε) : ∃ K : compacts G, K.1 ⊆ U ∧ of_content μ h1 U ≤ of_content μ h1 K.1 + ε :=
begin
rw [of_content_opens h2] at hU ⊢,
rcases inner_content_exists_compact hU hε with ⟨K, h1K, h2K⟩,
exact ⟨K, h1K, le_trans h2K $ add_le_add_right (le_of_content_compacts h2 K) _⟩,
end
lemma of_content_exists_open {A : set G} (hA : of_content μ h1 A < ⊤) {ε : ℝ≥0} (hε : 0 < ε) :
∃ U : opens G, A ⊆ U ∧ of_content μ h1 U ≤ of_content μ h1 A + ε :=
begin
rcases induced_outer_measure_exists_set _ _ inner_content_mono hA hε with ⟨U, hU, h2U, h3U⟩,
exact ⟨⟨U, hU⟩, h2U, h3U⟩, swap, exact inner_content_Union_nat h1 h2
end
lemma of_content_preimage (f : G ≃ₜ G) (h : ∀ ⦃K : compacts G⦄, μ (K.map f f.continuous) = μ K)
(A : set G) : of_content μ h1 (f ⁻¹' A) = of_content μ h1 A :=
begin
refine induced_outer_measure_preimage _ (inner_content_Union_nat h1 h2) inner_content_mono _
(λ s, f.is_open_preimage) _,
intros s hs, convert inner_content_comap f h ⟨s, hs⟩
end
lemma is_left_invariant_of_content [group G] [topological_group G]
(h : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (g : G)
(A : set G) : of_content μ h1 ((λ h, g * h) ⁻¹' A) = of_content μ h1 A :=
by convert of_content_preimage h2 (homeomorph.mul_left g) (λ K, h g) A
lemma of_content_caratheodory (A : set G) :
(of_content μ h1).caratheodory.is_measurable' A ↔ ∀ (U : opens G),
of_content μ h1 (U ∩ A) + of_content μ h1 (U \ A) ≤ of_content μ h1 U :=
begin
dsimp [opens], rw subtype.forall,
apply induced_outer_measure_caratheodory,
apply inner_content_Union_nat h1 h2,
apply inner_content_mono'
end
lemma of_content_pos_of_is_open [group G] [topological_group G]
(h3 : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K)
(K : compacts G) (hK : 0 < μ K) {U : set G} (h1U : is_open U) (h2U : U.nonempty) :
0 < of_content μ h1 U :=
by { convert inner_content_pos h1 h2 h3 K hK ⟨U, h1U⟩ h2U, exact of_content_opens h2 ⟨U, h1U⟩ }
end outer_measure
end measure_theory
|
3e4d25c0026d396f3af3387885835e42cc2ae7c6 | 3f48345ac9bbaa421714efc9872a0409379bb4ae | /src/coalgebra/colimits/coalgebra_pushout.lean | 6f465cdee3364445cf12f0a1ad1464e8cf4e2682 | [] | no_license | QaisHamarneh/Coalgebra-in-Lean | b4318ee6d83780e5c734eb78fed98b1fe8016f7e | bd0452df98bc64b608e5dfd7babc42c301bb6a46 | refs/heads/master | 1,663,371,200,241 | 1,661,004,695,000 | 1,661,004,695,000 | 209,798,828 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,561 | lean | import category_theory.category
import category_theory.functor
import help_functions
import set_category.colimits.Coequalizer
import coalgebra.Coalgebra
import coalgebra.colimits.coalgebra_sum
import coalgebra.colimits.coalgebra_coequalizer
import set_category.colimits.Pushout
import set_category.colimits.Sum
import set_category.category_set
import tactic.tidy
universes u
namespace coalgebra_pushout
open category_theory
set
sum
Sum
classical
function
help_functions
Coequalizer
coalgebra
coalgebra_sum
coalgebra_coequalizer
Pushout
category_set
local notation f ` ⊚ `:80 g:80 := category_struct.comp g f
variables {F : Type u ⥤ Type u}
{𝔸 Β₁ Β₂: Coalgebra F}
(ϕ : 𝔸 ⟶ Β₁)
(ψ : 𝔸 ⟶ Β₂)
theorem pushout_is_coalgebra :
let S := Β₁ ⊞ Β₂ in
let Β_Θ := @theta 𝔸 S (inl ∘ ϕ) (inr ∘ ψ) in
let π_Θ := @coequalizer 𝔸 S (inl ∘ ϕ) (inr ∘ ψ) in
∃! α : Β_Θ → F.obj Β_Θ,
let P : Coalgebra F := ⟨Β_Θ, α⟩ in
@is_coalgebra_homomorphism F Β₁ P (π_Θ ∘ inl) ∧
@is_coalgebra_homomorphism F Β₂ P (π_Θ ∘ inr) :=
begin
assume S Β_Θ π_Θ,
have po1 : π_Θ ∘ inl ∘ ϕ = π_Θ ∘ inr ∘ ψ
:= (coequalizer_sum_is_pushout ϕ ψ).1,
let p₁ : 𝔸 → S := (inl ∘ ϕ),
let p₂ : 𝔸 → S := (inr ∘ ψ),
have hom_p₁ : is_coalgebra_homomorphism p₁ :=
@comp_is_hom F 𝔸 Β₁ S ϕ ⟨inl, inl_is_homomorphism Β₁ Β₂⟩,
have hom_p₂ : is_coalgebra_homomorphism p₂ :=
@comp_is_hom F 𝔸 Β₂ S ψ ⟨inr, inr_is_homomorphism Β₁ Β₂⟩,
have co : _ := coequalizer_is_homomorphism
⟨p₁, hom_p₁⟩ ⟨p₂, hom_p₂⟩,
let α := some co,
have hom_π : @is_coalgebra_homomorphism F S ⟨Β_Θ , α⟩ π_Θ
:= (some_spec co).1,
let P : Coalgebra F := ⟨Β_Θ, α⟩,
have hom_π_inl : α ∘ (π_Θ ∘ inl) = (F.map (π_Θ ∘ inl)) ∘ Β₁.α
:= @comp_is_hom F Β₁ S P
⟨inl, inl_is_homomorphism Β₁ Β₂⟩
⟨π_Θ, hom_π⟩,
have hom_π_inr : α ∘ (π_Θ ∘ inr) = (F.map (π_Θ ∘ inr)) ∘ Β₂.α
:= @comp_is_hom F Β₂ S P
⟨inr, inr_is_homomorphism Β₁ Β₂⟩
⟨π_Θ, hom_π⟩,
use α,
split,
exact ⟨hom_π_inl , hom_π_inr⟩ ,
intros α₁ hom,
have hom1 : α₁ ∘ (π_Θ ∘ inl) = (F.map (π_Θ ∘ inl)) ∘ Β₁.α := hom.1,
have hom2 : α₁ ∘ (π_Θ ∘ inr) = (F.map (π_Θ ∘ inr)) ∘ Β₂.α := hom.2,
have α_α₁_l : α₁ ∘ π_Θ ∘ inl = α ∘ π_Θ ∘ inl :=
by simp [hom_π_inl, hom1],
have α_α₁_r : α₁ ∘ π_Θ ∘ inr = α ∘ π_Θ ∘ inr :=
by simp [hom_π_inr, hom2],
have α_α₁_π : α₁ ∘ π_Θ = α ∘ π_Θ :=
jointly_epi (α ∘ π_Θ) (α₁ ∘ π_Θ) α_α₁_l α_α₁_r,
let mor_π : (Β₁ ⊕ Β₂) ⟶ Β_Θ := π_Θ,
haveI ep : epi mor_π :=
(epi_iff_surjective mor_π).2
(quot_is_surjective (inl ∘ ϕ) (inr ∘ ψ)),
exact right_cancel mor_π α_α₁_π,
end
end coalgebra_pushout |
fb2ff190906e57741cc15f3b89bc1dc9770d3a34 | c055f4b7c29cf1aac2223bd8c1ac8d181a7c6447 | /src/categories/universal/instances.lean | 7a290e9b8470e75209a7621f98f595b9c7f601a5 | [
"Apache-2.0"
] | permissive | rwbarton/lean-category-theory-pr | 77207b6674eeec1e258ec85dea58f3bff8d27065 | 591847d70c6a11c4d5561cd0eaf69b1fe85a70ab | refs/heads/master | 1,584,595,111,303 | 1,528,029,041,000 | 1,528,029,041,000 | 135,919,126 | 0 | 0 | null | 1,528,041,805,000 | 1,528,041,805,000 | null | UTF-8 | Lean | false | false | 2,715 | 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 categories.universal
open categories
open categories.functor
open categories.isomorphism
open categories.initial
open categories.types
namespace categories.universal
universes u v w
section
variables (C : Type u) [𝒞 : category.{u v} C]
include 𝒞
class has_InitialObject :=
(initial_object : InitialObject C)
class has_BinaryProducts :=
(binary_product : Π X Y : C, BinaryProduct.{u v} X Y)
class has_FiniteProducts :=
(product : Π {I : Type w} [fintype I] (f : I → C), Product.{u v w} f)
class has_TerminalObject :=
(terminal_object : TerminalObject C)
class has_BinaryCoproducts :=
(binary_coproduct : Π X Y : C, BinaryCoproduct.{u v} X Y)
class has_FiniteCoproducts :=
(coproduct : Π {I : Type w} [fintype I] (f : I → C), Coproduct.{u v w} f)
class has_Equalizers :=
(equalizer : Π {X Y : C} (f g : X ⟶ Y), Equalizer f g)
class has_Coequalizers :=
(coequalizer : Π {X Y : C} (f g : X ⟶ Y), Coequalizer f g)
definition initial_object [has_InitialObject.{u v} C] : C := has_InitialObject.initial_object.{u v} C
definition terminal_object [has_TerminalObject.{u v} C] : C := has_TerminalObject.terminal_object.{u v} C
end
section
variables {C : Type u} [𝒞 : category.{u v} C]
include 𝒞
definition binary_product [has_BinaryProducts.{u v} C] (X Y : C) := has_BinaryProducts.binary_product.{u v} X Y
definition finite_product [has_FiniteProducts.{u v w} C] {I : Type w} [fin : fintype I] (f : I → C) := @has_FiniteProducts.product.{u v w} C _ _ I fin f
definition binary_coproduct [has_BinaryCoproducts.{u v} C] (X Y : C) := has_BinaryCoproducts.binary_coproduct.{u v} X Y
definition finite_coproduct [has_FiniteCoproducts.{u v w} C] {I : Type w} [fin : fintype I] (f : I → C) := @has_FiniteCoproducts.coproduct.{u v w} C _ _ I fin f
definition equalizer [has_Equalizers.{u v} C] {X Y : C} (f g : X ⟶ Y) := has_Equalizers.equalizer.{u v} f g
definition coequalizer [has_Coequalizers.{u v} C] {X Y : C} (f g : X ⟶ Y) := has_Coequalizers.coequalizer.{u v} f g
end
section
class has_Products (C : Type (u+1)) [large_category C] :=
(product : Π {I : Type u} (f : I → C), Product.{u+1 u u} f)
class has_Coproducts (C : Type (u+1)) [large_category C] :=
(coproduct : Π {I : Type u} (f : I → C), Coproduct.{u+1 u u} f)
variables {C : Type (u+1)} [large_category C]
definition product [has_Products C] {I : Type u} (F : I → C) := has_Products.product F
definition coproduct [has_Coproducts C] {I : Type u} (F : I → C) := has_Coproducts.coproduct F
end
end categories.universal
|
291b47036b04d8f0a712ce2b906e0670dd2b27df | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/algebra/ring_quot.lean | 9b2578706fcd7832afd41fc22bf0541f10b45a7a | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 11,341 | 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 ring_theory.ideal.basic
import ring_theory.algebra
/-!
# Quotients of non-commutative rings
Unfortunately, ideals have only been developed in the commutative case as `ideal`,
and it's not immediately clear how one should formalise ideals in the non-commutative case.
In this file, we directly define the quotient of a semiring by any relation,
by building a bigger relation that represents the ideal generated by that relation.
We prove the universal properties of the quotient,
and recommend avoiding relying on the actual definition!
Since everything runs in parallel for quotients of `R`-algebras, we do that case at the same time.
-/
universes u₁ u₂ u₃ u₄
variables {R : Type u₁} [semiring R]
variables {S : Type u₂} [comm_semiring S]
variables {A : Type u₃} [semiring A] [algebra S A]
namespace ring_quot
/--
Given an arbitrary relation `r` on a ring, we strengthen it to a relation `rel r`,
such that the equivalence relation generated by `rel r` has `x ~ y` if and only if
`x - y` is in the ideal generated by elements `a - b` such that `r a b`.
-/
inductive rel (r : R → R → Prop) : R → R → Prop
| of ⦃x y : R⦄ (h : r x y) : rel x y
| add_left ⦃a b c⦄ : rel a b → rel (a + c) (b + c)
| mul_left ⦃a b c⦄ : rel a b → rel (a * c) (b * c)
| mul_right ⦃a b c⦄ : rel b c → rel (a * b) (a * c)
theorem rel.add_right {r : R → R → Prop} ⦃a b c : R⦄ (h : rel r b c) : rel r (a + b) (a + c) :=
by { rw [add_comm a b, add_comm a c], exact rel.add_left h }
theorem rel.neg {R : Type u₁} [ring R] {r : R → R → Prop} ⦃a b : R⦄ (h : rel r a b) : rel r (-a) (-b) :=
by simp only [neg_eq_neg_one_mul a, neg_eq_neg_one_mul b, rel.mul_right h]
theorem rel.smul {r : A → A → Prop} (k : S) ⦃a b : A⦄ (h : rel r a b) : rel r (k • a) (k • b) :=
by simp only [algebra.smul_def, rel.mul_right h]
end ring_quot
/-- The quotient of a ring by an arbitrary relation. -/
def ring_quot (r : R → R → Prop) := quot (ring_quot.rel r)
namespace ring_quot
instance (r : R → R → Prop) : semiring (ring_quot r) :=
{ add := quot.map₂ (+) rel.add_right rel.add_left,
add_assoc := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact congr_arg (quot.mk _) (add_assoc _ _ _), },
zero := quot.mk _ 0,
zero_add := by { rintros ⟨⟩, exact congr_arg (quot.mk _) (zero_add _), },
add_zero := by { rintros ⟨⟩, exact congr_arg (quot.mk _) (add_zero _), },
zero_mul := by { rintros ⟨⟩, exact congr_arg (quot.mk _) (zero_mul _), },
mul_zero := by { rintros ⟨⟩, exact congr_arg (quot.mk _) (mul_zero _), },
add_comm := by { rintros ⟨⟩ ⟨⟩, exact congr_arg (quot.mk _) (add_comm _ _), },
mul := quot.map₂ (*) rel.mul_right rel.mul_left,
mul_assoc := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact congr_arg (quot.mk _) (mul_assoc _ _ _), },
one := quot.mk _ 1,
one_mul := by { rintros ⟨⟩, exact congr_arg (quot.mk _) (one_mul _), },
mul_one := by { rintros ⟨⟩, exact congr_arg (quot.mk _) (mul_one _), },
left_distrib := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact congr_arg (quot.mk _) (left_distrib _ _ _), },
right_distrib := by { rintros ⟨⟩ ⟨⟩ ⟨⟩, exact congr_arg (quot.mk _) (right_distrib _ _ _), }, }
instance {R : Type u₁} [ring R] (r : R → R → Prop) : ring (ring_quot r) :=
{ neg := quot.map (λ a, -a)
rel.neg,
add_left_neg := by { rintros ⟨⟩, exact congr_arg (quot.mk _) (add_left_neg _), },
.. (ring_quot.semiring r) }
instance {R : Type u₁} [comm_semiring R] (r : R → R → Prop) : comm_semiring (ring_quot r) :=
{ mul_comm := by { rintros ⟨⟩ ⟨⟩, exact congr_arg (quot.mk _) (mul_comm _ _), }
.. (ring_quot.semiring r) }
instance {R : Type u₁} [comm_ring R] (r : R → R → Prop) : comm_ring (ring_quot r) :=
{ .. (ring_quot.comm_semiring r),
.. (ring_quot.ring r) }
instance (s : A → A → Prop) : algebra S (ring_quot s) :=
{ smul := λ r, quot.map ((•) r) (rel.smul r),
to_fun := λ r, quot.mk _ (algebra_map S A r),
map_one' := congr_arg (quot.mk _) (ring_hom.map_one _),
map_mul' := λ r s, congr_arg (quot.mk _) (ring_hom.map_mul _ _ _),
map_zero' := congr_arg (quot.mk _) (ring_hom.map_zero _),
map_add' := λ r s, congr_arg (quot.mk _) (ring_hom.map_add _ _ _),
commutes' := λ r, by { rintro ⟨a⟩, exact congr_arg (quot.mk _) (algebra.commutes _ _) },
smul_def' := λ r, by { rintro ⟨a⟩, exact congr_arg (quot.mk _) (algebra.smul_def _ _) }, }
instance (r : R → R → Prop) : inhabited (ring_quot r) := ⟨0⟩
/--
The quotient map from a ring to its quotient, as a homomorphism of rings.
-/
def mk_ring_hom (r : R → R → Prop) : R →+* ring_quot r :=
{ to_fun := quot.mk _,
map_one' := rfl,
map_mul' := λ a b, rfl,
map_zero' := rfl,
map_add' := λ a b, rfl, }
lemma mk_ring_hom_rel {r : R → R → Prop} {x y : R} (w : r x y) :
mk_ring_hom r x = mk_ring_hom r y :=
quot.sound (rel.of w)
lemma mk_ring_hom_surjective (r : R → R → Prop) : function.surjective (mk_ring_hom r) :=
by { dsimp [mk_ring_hom], rintro ⟨⟩, simp, }
@[ext]
lemma ring_quot_ext {T : Type u₄} [semiring T] {r : R → R → Prop} (f g : ring_quot r →+* T)
(w : f.comp (mk_ring_hom r) = g.comp (mk_ring_hom r)) : f = g :=
begin
ext,
rcases mk_ring_hom_surjective r x with ⟨x, rfl⟩,
exact (congr_arg (λ h : R →+* T, h x) w), -- TODO should we have `ring_hom.congr` for this?
end
variables {T : Type u₄} [semiring T]
/--
Any ring homomorphism `f : R →+* T` which respects a relation `r : R → R → Prop`
factors through a morphism `ring_quot r →+* T`.
-/
def lift (f : R →+* T) {r : R → R → Prop} (w : ∀ ⦃x y⦄, r x y → f x = f y) :
ring_quot r →+* T :=
{ to_fun := quot.lift f
begin
rintros _ _ r,
induction r,
case of : _ _ r { exact w r, },
case add_left : _ _ _ _ r' { simp [r'], },
case mul_left : _ _ _ _ r' { simp [r'], },
case mul_right : _ _ _ _ r' { simp [r'], },
end,
map_zero' := f.map_zero,
map_add' := by { rintros ⟨x⟩ ⟨y⟩, exact f.map_add x y, },
map_one' := f.map_one,
map_mul' := by { rintros ⟨x⟩ ⟨y⟩, exact f.map_mul x y, }, }
@[simp]
lemma lift_mk_ring_hom_apply (f : R →+* T) {r : R → R → Prop} (w : ∀ ⦃x y⦄, r x y → f x = f y) (x) :
(lift f w) (mk_ring_hom r x) = f x :=
rfl
lemma lift_unique (f : R →+* T) {r : R → R → Prop} (w : ∀ ⦃x y⦄, r x y → f x = f y)
(g : ring_quot r →+* T) (h : g.comp (mk_ring_hom r) = f) : g = lift f w :=
by { ext, simp [h], }
lemma eq_lift_comp_mk_ring_hom {r : R → R → Prop} (f : ring_quot r →+* T) :
f = lift (f.comp (mk_ring_hom r)) (λ x y h, by { dsimp, rw mk_ring_hom_rel h, }) :=
by { ext, simp, }
section comm_ring
/-!
We now verify that in the case of a commutative ring, the `ring_quot` construction
agrees with the quotient by the appropriate ideal.
-/
variables {B : Type u₁} [comm_ring B]
/-- The universal ring homomorphism from `ring_quot r` to `(ideal.of_rel r).quotient`. -/
def ring_quot_to_ideal_quotient (r : B → B → Prop) :
ring_quot r →+* (ideal.of_rel r).quotient :=
lift
(ideal.quotient.mk (ideal.of_rel r))
(λ x y h, quot.sound (submodule.mem_Inf.mpr (λ p w, w ⟨x, y, h, rfl⟩)))
@[simp] lemma ring_quot_to_ideal_quotient_apply (r : B → B → Prop) (x : B) :
ring_quot_to_ideal_quotient r (mk_ring_hom r x) = ideal.quotient.mk _ x := rfl
/-- The universal ring homomorphism from `(ideal.of_rel r).quotient` to `ring_quot r`. -/
def ideal_quotient_to_ring_quot (r : B → B → Prop) :
(ideal.of_rel r).quotient →+* ring_quot r :=
ideal.quotient.lift (ideal.of_rel r) (mk_ring_hom r)
begin
intros x h,
apply submodule.span_induction h,
{ rintros - ⟨a,b,h,rfl⟩,
rw [ring_hom.map_sub, mk_ring_hom_rel h, sub_self], },
{ simp, },
{ intros a b ha hb, simp [ha, hb], },
{ intros a x hx, simp [hx], },
end
@[simp] lemma ideal_quotient_to_ring_quot_apply (r : B → B → Prop) (x : B) :
ideal_quotient_to_ring_quot r (ideal.quotient.mk _ x) = mk_ring_hom r x := rfl
/--
The ring equivalence between `ring_quot r` and `(ideal.of_rel r).quotient`
-/
def ring_quot_equiv_ideal_quotient (r : B → B → Prop) :
ring_quot r ≃+* (ideal.of_rel r).quotient :=
ring_equiv.of_hom_inv (ring_quot_to_ideal_quotient r) (ideal_quotient_to_ring_quot r)
(by { ext, simp, }) (by { ext ⟨x⟩, simp, })
end comm_ring
section algebra
variables (S)
/--
The quotient map from an `S`-algebra to its quotient, as a homomorphism of `S`-algebras.
-/
def mk_alg_hom (s : A → A → Prop) : A →ₐ[S] ring_quot s :=
{ commutes' := λ r, rfl,
..mk_ring_hom s }
@[simp]
lemma mk_alg_hom_coe (s : A → A → Prop) : (mk_alg_hom S s : A →+* ring_quot s) = mk_ring_hom s :=
rfl
lemma mk_alg_hom_rel {s : A → A → Prop} {x y : A} (w : s x y) :
mk_alg_hom S s x = mk_alg_hom S s y :=
quot.sound (rel.of w)
lemma mk_alg_hom_surjective (s : A → A → Prop) : function.surjective (mk_alg_hom S s) :=
by { dsimp [mk_alg_hom], rintro ⟨a⟩, use a, refl, }
variables {B : Type u₄} [semiring B] [algebra S B]
@[ext]
lemma ring_quot_ext' {s : A → A → Prop}
(f g : ring_quot s →ₐ[S] B) (w : f.comp (mk_alg_hom S s) = g.comp (mk_alg_hom S s)) : f = g :=
begin
ext,
rcases mk_alg_hom_surjective S s x with ⟨x, rfl⟩,
exact (congr_arg (λ h : A →ₐ[S] B, h x) w), -- TODO should we have `alg_hom.congr` for this?
end
/--
Any `S`-algebra homomorphism `f : A →ₐ[S] B` which respects a relation `s : A → A → Prop`
factors through a morphism `ring_quot s →ₐ[S] B`.
-/
def lift_alg_hom (f : A →ₐ[S] B) {s : A → A → Prop} (w : ∀ ⦃x y⦄, s x y → f x = f y) :
ring_quot s →ₐ[S] B :=
{ to_fun := quot.lift f
begin
rintros _ _ r,
induction r,
case of : _ _ r { exact w r, },
case add_left : _ _ _ _ r' { simp [r'], },
case mul_left : _ _ _ _ r' { simp [r'], },
case mul_right : _ _ _ _ r' { simp [r'], },
end,
map_zero' := f.map_zero,
map_add' := by { rintros ⟨x⟩ ⟨y⟩, exact f.map_add x y, },
map_one' := f.map_one,
map_mul' := by { rintros ⟨x⟩ ⟨y⟩, exact f.map_mul x y, },
commutes' :=
begin
rintros x,
conv_rhs { rw [algebra.algebra_map_eq_smul_one, ←f.map_one, ←f.map_smul], },
rw algebra.algebra_map_eq_smul_one,
exact quot.lift_beta f @w (x • 1),
end, }
@[simp]
lemma lift_alg_hom_mk_alg_hom_apply (f : A →ₐ[S] B) {s : A → A → Prop} (w : ∀ ⦃x y⦄, s x y → f x = f y) (x) :
(lift_alg_hom S f w) ((mk_alg_hom S s) x) = f x :=
rfl
lemma lift_alg_hom_unique (f : A →ₐ[S] B) {s : A → A → Prop} (w : ∀ ⦃x y⦄, s x y → f x = f y)
(g : ring_quot s →ₐ[S] B) (h : g.comp (mk_alg_hom S s) = f) : g = lift_alg_hom S f w :=
by { ext, simp [h], }
lemma eq_lift_alg_hom_comp_mk_alg_hom {s : A → A → Prop} (f : ring_quot s →ₐ[S] B) :
f = lift_alg_hom S (f.comp (mk_alg_hom S s)) (λ x y h, by { dsimp, erw mk_alg_hom_rel S h, }) :=
by { ext, simp, }
end algebra
attribute [irreducible] ring_quot mk_ring_hom mk_alg_hom lift lift_alg_hom
end ring_quot
|
82e0ddf41a416ca63a572c73c20129036b054cfb | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/linear_algebra/finite_dimensional.lean | 47909e57ddba22a744f9ed267e021dac23b56ca8 | [
"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 | 63,375 | 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 algebra.algebra.subalgebra
import field_theory.finiteness
import linear_algebra.dimension
import ring_theory.principal_ideal_domain
/-!
# Finite dimensional vector spaces
Definition and basic properties of finite dimensional vector spaces, of their dimensions, and
of linear maps on such spaces.
## Main definitions
Assume `V` is a vector space over a field `K`. There are (at least) three equivalent definitions of
finite-dimensionality of `V`:
- it admits a finite basis.
- it is finitely generated.
- it is noetherian, i.e., every subspace is finitely generated.
We introduce a typeclass `finite_dimensional K V` capturing this property. For ease of transfer of
proof, it is defined using the third point of view, i.e., as `is_noetherian`. However, we prove
that all these points of view are equivalent, with the following lemmas
(in the namespace `finite_dimensional`):
- `basis.of_vector_space_index.fintype` states that a finite-dimensional
vector space has a finite basis
- `finite_dimensional.fintype_basis_index` states that a basis of a
finite-dimensional vector space contains finitely many basis vectors
- `finite_dimensional.finset_basis` states that a finite-dimensional
vector space has a basis indexed by a `finset`
- `finite_dimensional.fin_basis` and `finite_dimensional.fin_basis_of_finrank_eq`
are bases for finite dimensional vector spaces, where the index type
is `fin`
- `of_fintype_basis` states that the existence of a basis indexed by a
finite type implies finite-dimensionality
- `of_finset_basis` states that the existence of a basis indexed by a
`finset` implies finite-dimensionality
- `of_finite_basis` states that the existence of a basis indexed by a
finite set implies finite-dimensionality
- `iff_fg` states that the space is finite-dimensional if and only if
it is finitely generated
Also defined is `finrank`, the dimension of a finite dimensional space, returning a `nat`,
as opposed to `module.rank`, which returns a `cardinal`. When the space has infinite dimension, its
`finrank` is by convention set to `0`.
Preservation of finite-dimensionality and formulas for the dimension are given for
- submodules
- quotients (for the dimension of a quotient, see `finrank_quotient_add_finrank`)
- linear equivs, in `linear_equiv.finite_dimensional` and `linear_equiv.finrank_eq`
- image under a linear map (the rank-nullity formula is in `finrank_range_add_finrank_ker`)
Basic properties of linear maps of a finite-dimensional vector space are given. Notably, the
equivalence of injectivity and surjectivity is proved in `linear_map.injective_iff_surjective`,
and the equivalence between left-inverse and right-inverse in `mul_eq_one_comm` and
`comp_eq_id_comm`.
## Implementation notes
Most results are deduced from the corresponding results for the general dimension (as a cardinal),
in `dimension.lean`. Not all results have been ported yet.
One of the characterizations of finite-dimensionality is in terms of finite generation. This
property is currently defined only for submodules, so we express it through the fact that the
maximal submodule (which, as a set, coincides with the whole space) is finitely generated. This is
not very convenient to use, although there are some helper functions. However, this becomes very
convenient when speaking of submodules which are finite-dimensional, as this notion coincides with
the fact that the submodule is finitely generated (as a submodule of the whole space). This
equivalence is proved in `submodule.fg_iff_finite_dimensional`.
-/
universes u v v' w
open_locale classical
open cardinal submodule module function
variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [module K V]
{V₂ : Type v'} [add_comm_group V₂] [module K V₂]
/-- `finite_dimensional` vector spaces are defined to be noetherian modules.
Use `finite_dimensional.iff_fg` or `finite_dimensional.of_fintype_basis` to prove finite dimension
from a conventional definition. -/
@[reducible] def finite_dimensional (K V : Type*) [field K]
[add_comm_group V] [module K V] := is_noetherian K V
namespace finite_dimensional
open is_noetherian
variables (K V)
/-- A finite dimensional vector space over a finite field is finite -/
noncomputable def fintype_of_fintype [fintype K] [is_noetherian K V] : fintype V :=
module.fintype_of_fintype (finset_basis K V)
variables {K V}
/-- If a vector space has a finite basis, then it is finite-dimensional. -/
lemma of_fintype_basis {ι : Type w} [fintype ι] (h : basis ι K V) :
finite_dimensional K V :=
iff_fg.2 $ ⟨⟨finset.univ.image h, by { convert h.span_eq, simp } ⟩⟩
/-- If a vector space has a basis indexed by elements of a finite set, then it is
finite-dimensional. -/
lemma of_finite_basis {ι : Type w} {s : set ι} (h : basis s K V) (hs : set.finite s) :
finite_dimensional K V :=
by haveI := hs.fintype; exact of_fintype_basis h
-- TODO: why do we have to specify `.{w}` explicitly here?
/-- If a vector space has a finite basis, then it is finite-dimensional, finset style. -/
lemma of_finset_basis {ι : Type w} {s : finset ι} (h : basis.{w} s K V) :
finite_dimensional K V :=
of_finite_basis h s.finite_to_set
/-- A subspace of a finite-dimensional space is also finite-dimensional. -/
instance finite_dimensional_submodule [finite_dimensional K V] (S : submodule K V) :
finite_dimensional K S :=
is_noetherian.iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_submodule_le _) (dim_lt_omega K V))
/-- A quotient of a finite-dimensional space is also finite-dimensional. -/
instance finite_dimensional_quotient [finite_dimensional K V] (S : submodule K V) :
finite_dimensional K (quotient S) :=
is_noetherian.iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_quotient_le _) (dim_lt_omega K V))
/-- The rank of a module as a natural number.
Defined by convention to be `0` if the space has infinite rank.
For a vector space `V` over a field `K`, this is the same as the finite dimension
of `V` over `K`.
-/
noncomputable def finrank (K V : Type*) [field K]
[add_comm_group V] [module K V] : ℕ :=
(module.rank K V).to_nat
/-- In a finite-dimensional space, its dimension (seen as a cardinal) coincides with its
`finrank`. -/
lemma finrank_eq_dim (K : Type u) (V : Type v) [field K]
[add_comm_group V] [module K V] [finite_dimensional K V] :
(finrank K V : cardinal.{v}) = module.rank K V :=
by rw [finrank, cast_to_nat_of_lt_omega (dim_lt_omega K V)]
lemma finrank_eq_of_dim_eq {n : ℕ} (h : module.rank K V = ↑ n) : finrank K V = n :=
begin
apply_fun to_nat at h,
rw to_nat_cast at h,
exact_mod_cast h,
end
lemma finrank_of_infinite_dimensional {K V : Type*} [field K] [add_comm_group V] [module K V]
(h : ¬finite_dimensional K V) : finrank K V = 0 :=
dif_neg $ mt is_noetherian.iff_dim_lt_omega.2 h
lemma finite_dimensional_of_finrank {K V : Type*} [field K] [add_comm_group V] [module K V]
(h : 0 < finrank K V) : finite_dimensional K V :=
by { contrapose h, simp [finrank_of_infinite_dimensional h] }
/-- We can infer `finite_dimensional K V` in the presence of `[fact (finrank K V = n + 1)]`. Declare
this as a local instance where needed. -/
lemma finite_dimensional_of_finrank_eq_succ {K V : Type*} [field K] [add_comm_group V]
[module K V] (n : ℕ) [fact (finrank K V = n + 1)] :
finite_dimensional K V :=
finite_dimensional_of_finrank $ by convert nat.succ_pos n; apply fact.out
/-- If a vector space has a finite basis, then its dimension (seen as a cardinal) is equal to the
cardinality of the basis. -/
lemma dim_eq_card_basis {ι : Type w} [fintype ι] (h : basis ι K V) :
module.rank K V = fintype.card ι :=
by rw [←h.mk_range_eq_dim, cardinal.fintype_card,
set.card_range_of_injective h.injective]
/-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the
basis. -/
lemma finrank_eq_card_basis {ι : Type w} [fintype ι] (h : basis ι K V) :
finrank K V = fintype.card ι :=
begin
haveI : finite_dimensional K V := of_fintype_basis h,
have := dim_eq_card_basis h,
rw ← finrank_eq_dim at this,
exact_mod_cast this
end
/-- If a vector space is finite-dimensional, then the cardinality of any basis is equal to its
`finrank`. -/
lemma finrank_eq_card_basis' [finite_dimensional K V] {ι : Type w} (h : basis ι K V) :
(finrank K V : cardinal.{w}) = cardinal.mk ι :=
begin
haveI : fintype ι := fintype_basis_index h,
rw [cardinal.fintype_card, finrank_eq_card_basis h]
end
/-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the
basis. This lemma uses a `finset` instead of indexed types. -/
lemma finrank_eq_card_finset_basis {ι : Type w} {b : finset ι}
(h : basis.{w} b K V) :
finrank K V = finset.card b :=
by rw [finrank_eq_card_basis h, fintype.card_coe]
variables (K V)
/-- A finite dimensional vector space has a basis indexed by `fin (finrank K V)`. -/
noncomputable def fin_basis [finite_dimensional K V] : basis (fin (finrank K V)) K V :=
have h : fintype.card (finset_basis_index K V) = finrank K V,
from (finrank_eq_card_basis (finset_basis K V)).symm,
(finset_basis K V).reindex (fintype.equiv_fin_of_card_eq h)
/-- An `n`-dimensional vector space has a basis indexed by `fin n`. -/
noncomputable def fin_basis_of_finrank_eq [finite_dimensional K V] {n : ℕ} (hn : finrank K V = n) :
basis (fin n) K V :=
(fin_basis K V).reindex (fin.cast hn).to_equiv
variables {K V}
/-- A module with dimension 1 has a basis with one element. -/
noncomputable def basis_unique (ι : Type*) [unique ι] (h : finrank K V = 1) :
basis ι K V :=
begin
haveI := finite_dimensional_of_finrank (_root_.zero_lt_one.trans_le h.symm.le),
exact (fin_basis_of_finrank_eq K V h).reindex equiv_of_unique_of_unique
end
@[simp]
lemma basis_unique.repr_eq_zero_iff {ι : Type*} [unique ι] {h : finrank K V = 1}
{v : V} {i : ι} : (basis_unique ι h).repr v i = 0 ↔ v = 0 :=
⟨λ hv, (basis_unique ι h).repr.map_eq_zero_iff.mp (finsupp.ext $ λ j, subsingleton.elim i j ▸ hv),
λ hv, by rw [hv, linear_equiv.map_zero, finsupp.zero_apply]⟩
/-- In a vector space with dimension 1, each set {v} is a basis for `v ≠ 0`. -/
noncomputable def basis_singleton (ι : Type*) [unique ι]
(h : finrank K V = 1) (v : V) (hv : v ≠ 0) :
basis ι K V :=
let b := basis_unique ι h in
b.map (linear_equiv.smul_of_unit (units.mk0
(b.repr v (default ι))
(mt basis_unique.repr_eq_zero_iff.mp hv)))
@[simp] lemma basis_singleton_apply (ι : Type*) [unique ι]
(h : finrank K V = 1) (v : V) (hv : v ≠ 0) (i : ι) :
basis_singleton ι h v hv i = v :=
calc basis_singleton ι h v hv i
= (((basis_unique ι h).repr) v) (default ι) • (basis_unique ι h) (default ι) :
by simp [subsingleton.elim i (default ι), basis_singleton, linear_equiv.smul_of_unit]
... = v : by rw [← finsupp.total_unique K (basis.repr _ v), basis.total_repr]
@[simp] lemma range_basis_singleton (ι : Type*) [unique ι]
(h : finrank K V = 1) (v : V) (hv : v ≠ 0) :
set.range (basis_singleton ι h v hv) = {v} :=
by rw [set.range_unique, basis_singleton_apply]
lemma cardinal_mk_le_finrank_of_linear_independent
[finite_dimensional K V] {ι : Type w} {b : ι → V} (h : linear_independent K b) :
cardinal.mk ι ≤ finrank K V :=
begin
rw ← lift_le.{_ (max v w)},
simpa [← finrank_eq_dim K V] using
cardinal_lift_le_dim_of_linear_independent.{_ _ _ (max v w)} h
end
lemma fintype_card_le_finrank_of_linear_independent
[finite_dimensional K V] {ι : Type*} [fintype ι] {b : ι → V} (h : linear_independent K b) :
fintype.card ι ≤ finrank K V :=
by simpa [fintype_card] using cardinal_mk_le_finrank_of_linear_independent h
lemma finset_card_le_finrank_of_linear_independent [finite_dimensional K V] {b : finset V}
(h : linear_independent K (λ x, x : b → V)) :
b.card ≤ finrank K V :=
begin
rw ←fintype.card_coe,
exact fintype_card_le_finrank_of_linear_independent h,
end
lemma lt_omega_of_linear_independent {ι : Type w} [finite_dimensional K V]
{v : ι → V} (h : linear_independent K v) :
cardinal.mk ι < cardinal.omega :=
begin
apply cardinal.lift_lt.1,
apply lt_of_le_of_lt,
apply linear_independent_le_dim h,
rw [←finrank_eq_dim, cardinal.lift_omega, cardinal.lift_nat_cast],
apply cardinal.nat_lt_omega,
end
lemma not_linear_independent_of_infinite {ι : Type w} [inf : infinite ι] [finite_dimensional K V]
(v : ι → V) : ¬ linear_independent K v :=
begin
intro h_lin_indep,
have : ¬ omega ≤ mk ι := not_le.mpr (lt_omega_of_linear_independent h_lin_indep),
have : omega ≤ mk ι := infinite_iff.mp inf,
contradiction
end
/-- A finite dimensional space has positive `finrank` iff it has a nonzero element. -/
lemma finrank_pos_iff_exists_ne_zero [finite_dimensional K V] : 0 < finrank K V ↔ ∃ x : V, x ≠ 0 :=
iff.trans (by { rw ← finrank_eq_dim, norm_cast }) (@dim_pos_iff_exists_ne_zero K V _ _ _)
/-- A finite dimensional space has positive `finrank` iff it is nontrivial. -/
lemma finrank_pos_iff [finite_dimensional K V] : 0 < finrank K V ↔ nontrivial V :=
iff.trans (by { rw ← finrank_eq_dim, norm_cast }) (@dim_pos_iff_nontrivial K V _ _ _)
/-- A nontrivial finite dimensional space has positive `finrank`. -/
lemma finrank_pos [finite_dimensional K V] [h : nontrivial V] : 0 < finrank K V :=
finrank_pos_iff.mpr h
/-- A finite dimensional space has zero `finrank` iff it is a subsingleton.
This is the `finrank` version of `dim_zero_iff`. -/
lemma finrank_zero_iff [finite_dimensional K V] :
finrank K V = 0 ↔ subsingleton V :=
iff.trans (by { rw ← finrank_eq_dim, norm_cast }) (@dim_zero_iff K V _ _ _)
/-- A finite dimensional space that is a subsingleton has zero `finrank`. -/
lemma finrank_zero_of_subsingleton [h : subsingleton V] :
finrank K V = 0 :=
finrank_zero_iff.2 h
section
open_locale big_operators
open finset
/--
If a finset has cardinality larger than the dimension of the space,
then there is a nontrivial linear relation amongst its elements.
-/
lemma exists_nontrivial_relation_of_dim_lt_card
[finite_dimensional K V] {t : finset V} (h : finrank K V < t.card) :
∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∃ x ∈ t, f x ≠ 0 :=
begin
have := mt finset_card_le_finrank_of_linear_independent (by { simpa using h }),
rw linear_dependent_iff at this,
obtain ⟨s, g, sum, z, zm, nonzero⟩ := this,
-- Now we have to extend `g` to all of `t`, then to all of `V`.
let f : V → K :=
λ x, if h : x ∈ t then if (⟨x, h⟩ : t) ∈ s then g ⟨x, h⟩ else 0 else 0,
-- and finally clean up the mess caused by the extension.
refine ⟨f, _, _⟩,
{ dsimp [f],
rw ← sum,
fapply sum_bij_ne_zero (λ v hvt _, (⟨v, hvt⟩ : {v // v ∈ t})),
{ intros v hvt H, dsimp,
rw [dif_pos hvt] at H,
contrapose! H,
rw [if_neg H, zero_smul], },
{ intros _ _ _ _ _ _, exact subtype.mk.inj, },
{ intros b hbs hb,
use b,
simpa only [hbs, exists_prop, dif_pos, finset.mk_coe, and_true, if_true, finset.coe_mem,
eq_self_iff_true, exists_prop_of_true, ne.def] using hb, },
{ intros a h₁, dsimp, rw [dif_pos h₁],
intro h₂, rw [if_pos], contrapose! h₂,
rw [if_neg h₂, zero_smul], }, },
{ refine ⟨z, z.2, _⟩, dsimp only [f], erw [dif_pos z.2, if_pos]; rwa [subtype.coe_eta] },
end
/--
If a finset has cardinality larger than `finrank + 1`,
then there is a nontrivial linear relation amongst its elements,
such that the coefficients of the relation sum to zero.
-/
lemma exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card
[finite_dimensional K V] {t : finset V} (h : finrank K V + 1 < t.card) :
∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, f x ≠ 0 :=
begin
-- Pick an element x₀ ∈ t,
have card_pos : 0 < t.card := lt_trans (nat.succ_pos _) h,
obtain ⟨x₀, m⟩ := (finset.card_pos.1 card_pos).bex,
-- and apply the previous lemma to the {xᵢ - x₀}
let shift : V ↪ V := ⟨λ x, x - x₀, sub_left_injective⟩,
let t' := (t.erase x₀).map shift,
have h' : finrank K V < t'.card,
{ simp only [t', card_map, finset.card_erase_of_mem m],
exact nat.lt_pred_iff.mpr h, },
-- to obtain a function `g`.
obtain ⟨g, gsum, x₁, x₁_mem, nz⟩ := exists_nontrivial_relation_of_dim_lt_card h',
-- Then obtain `f` by translating back by `x₀`,
-- and setting the value of `f` at `x₀` to ensure `∑ e in t, f e = 0`.
let f : V → K := λ z, if z = x₀ then - ∑ z in (t.erase x₀), g (z - x₀) else g (z - x₀),
refine ⟨f, _ ,_ ,_⟩,
-- After this, it's a matter of verifiying the properties,
-- based on the corresponding properties for `g`.
{ show ∑ (e : V) in t, f e • e = 0,
-- We prove this by splitting off the `x₀` term of the sum,
-- which is itself a sum over `t.erase x₀`,
-- combining the two sums, and
-- observing that after reindexing we have exactly
-- ∑ (x : V) in t', g x • x = 0.
simp only [f],
conv_lhs { apply_congr, skip, rw [ite_smul], },
rw [finset.sum_ite],
conv { congr, congr, apply_congr, simp [filter_eq', m], },
conv { congr, congr, skip, apply_congr, simp [filter_ne'], },
rw [sum_singleton, neg_smul, add_comm, ←sub_eq_add_neg, sum_smul, ←sum_sub_distrib],
simp only [←smul_sub],
-- At the end we have to reindex the sum, so we use `change` to
-- express the summand using `shift`.
change (∑ (x : V) in t.erase x₀, (λ e, g e • e) (shift x)) = 0,
rw ←sum_map _ shift,
exact gsum, },
{ show ∑ (e : V) in t, f e = 0,
-- Again we split off the `x₀` term,
-- observing that it exactly cancels the other terms.
rw [← insert_erase m, sum_insert (not_mem_erase x₀ t)],
dsimp [f],
rw [if_pos rfl],
conv_lhs { congr, skip, apply_congr, skip, rw if_neg (show x ≠ x₀, from (mem_erase.mp H).1), },
exact neg_add_self _, },
{ show ∃ (x : V) (H : x ∈ t), f x ≠ 0,
-- We can use x₁ + x₀.
refine ⟨x₁ + x₀, _, _⟩,
{ rw finset.mem_map at x₁_mem,
rcases x₁_mem with ⟨x₁, x₁_mem, rfl⟩,
rw mem_erase at x₁_mem,
simp only [x₁_mem, sub_add_cancel, function.embedding.coe_fn_mk], },
{ dsimp only [f],
rwa [if_neg, add_sub_cancel],
rw [add_left_eq_self], rintro rfl,
simpa only [sub_eq_zero, exists_prop, finset.mem_map, embedding.coe_fn_mk, eq_self_iff_true,
mem_erase, not_true, exists_eq_right, ne.def, false_and] using x₁_mem, } },
end
section
variables {L : Type*} [linear_ordered_field L]
variables {W : Type v} [add_comm_group W] [module L W]
/--
A slight strengthening of `exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card`
available when working over an ordered field:
we can ensure a positive coefficient, not just a nonzero coefficient.
-/
lemma exists_relation_sum_zero_pos_coefficient_of_dim_succ_lt_card
[finite_dimensional L W] {t : finset W} (h : finrank L W + 1 < t.card) :
∃ f : W → L, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, 0 < f x :=
begin
obtain ⟨f, sum, total, nonzero⟩ := exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card h,
exact ⟨f, sum, total, exists_pos_of_sum_zero_of_exists_nonzero f total nonzero⟩,
end
end
end
lemma basis.subset_extend {s : set V} (hs : linear_independent K (coe : s → V)) :
s ⊆ hs.extend (set.subset_univ _) :=
hs.subset_extend _
/-- If a submodule has maximal dimension in a finite dimensional space, then it is equal to the
whole space. -/
lemma eq_top_of_finrank_eq [finite_dimensional K V] {S : submodule K V}
(h : finrank K S = finrank K V) : S = ⊤ :=
begin
set bS := basis.of_vector_space K S with bS_eq,
have : linear_independent K (coe : (coe '' basis.of_vector_space_index K S : set V) → V),
from @linear_independent.image_subtype _ _ _ _ _ _ _ _ _
(submodule.subtype S) (by simpa using bS.linear_independent) (by simp),
set b := basis.extend this with b_eq,
letI : fintype (this.extend _) :=
classical.choice (finite_of_linear_independent (by simpa using b.linear_independent)),
letI : fintype (subtype.val '' basis.of_vector_space_index K S) :=
classical.choice (finite_of_linear_independent this),
letI : fintype (basis.of_vector_space_index K S) :=
classical.choice (finite_of_linear_independent (by simpa using bS.linear_independent)),
have : subtype.val '' (basis.of_vector_space_index K S) = this.extend (set.subset_univ _),
from set.eq_of_subset_of_card_le (this.subset_extend _)
(by rw [set.card_image_of_injective _ subtype.val_injective, ← finrank_eq_card_basis bS,
← finrank_eq_card_basis b, h]; apply_instance),
rw [← b.span_eq, b_eq, basis.coe_extend, subtype.range_coe, ← this, ← subtype_eq_val, span_image],
have := bS.span_eq,
rw [bS_eq, basis.coe_of_vector_space, subtype.range_coe] at this,
rw [this, map_top (submodule.subtype S), range_subtype],
end
variable (K)
/-- A field is one-dimensional as a vector space over itself. -/
@[simp] lemma finrank_of_field : finrank K K = 1 :=
begin
have := dim_of_field K,
rw [← finrank_eq_dim] at this,
exact_mod_cast this
end
instance finite_dimensional_self : finite_dimensional K K :=
by apply_instance
/-- The vector space of functions on a fintype ι has finrank equal to the cardinality of ι. -/
@[simp] lemma finrank_fintype_fun_eq_card {ι : Type v} [fintype ι] :
finrank K (ι → K) = fintype.card ι :=
begin
have : module.rank K (ι → K) = fintype.card ι := dim_fun',
rwa [← finrank_eq_dim, nat_cast_inj] at this,
end
/-- The vector space of functions on `fin n` has finrank equal to `n`. -/
@[simp] lemma finrank_fin_fun {n : ℕ} : finrank K (fin n → K) = n :=
by simp
/-- The submodule generated by a finite set is finite-dimensional. -/
theorem span_of_finite {A : set V} (hA : set.finite A) :
finite_dimensional K (submodule.span K A) :=
is_noetherian_span_of_finite K hA
/-- The submodule generated by a single element is finite-dimensional. -/
instance (x : V) : finite_dimensional K (K ∙ x) := by {apply span_of_finite, simp}
end finite_dimensional
section zero_dim
open finite_dimensional
lemma finite_dimensional_of_dim_eq_zero (h : module.rank K V = 0) : finite_dimensional K V :=
begin
dsimp [finite_dimensional],
rw [is_noetherian.iff_dim_lt_omega, h],
exact cardinal.omega_pos
end
lemma finite_dimensional_of_dim_eq_one (h : module.rank K V = 1) : finite_dimensional K V :=
begin
dsimp [finite_dimensional],
rw [is_noetherian.iff_dim_lt_omega, h],
exact one_lt_omega
end
lemma finrank_eq_zero_of_dim_eq_zero [finite_dimensional K V] (h : module.rank K V = 0) :
finrank K V = 0 :=
begin
convert finrank_eq_dim K V,
rw h, norm_cast
end
lemma finrank_eq_zero_of_basis_imp_not_finite
(h : ∀ s : set V, basis.{v} (s : set V) K V → ¬ s.finite) : finrank K V = 0 :=
dif_neg (λ dim_lt,
h _ (basis.of_vector_space K V) ((basis.of_vector_space K V).finite_index_of_dim_lt_omega dim_lt))
lemma finrank_eq_zero_of_basis_imp_false
(h : ∀ s : finset V, basis.{v} (s : set V) K V → false) : finrank K V = 0 :=
finrank_eq_zero_of_basis_imp_not_finite (λ s b hs, h hs.to_finset (by { convert b, simp }))
lemma finrank_eq_zero_of_not_exists_basis
(h : ¬ (∃ s : finset V, nonempty (basis (s : set V) K V))) : finrank K V = 0 :=
finrank_eq_zero_of_basis_imp_false (λ s b, h ⟨s, ⟨b⟩⟩)
lemma finrank_eq_zero_of_not_exists_basis_finite
(h : ¬ ∃ (s : set V) (b : basis.{v} (s : set V) K V), s.finite) : finrank K V = 0 :=
finrank_eq_zero_of_basis_imp_not_finite (λ s b hs, h ⟨s, b, hs⟩)
lemma finrank_eq_zero_of_not_exists_basis_finset
(h : ¬ ∃ (s : finset V), nonempty (basis s K V)) : finrank K V = 0 :=
finrank_eq_zero_of_basis_imp_false (λ s b, h ⟨s, ⟨b⟩⟩)
variables (K V)
lemma finite_dimensional_bot : finite_dimensional K (⊥ : submodule K V) :=
finite_dimensional_of_dim_eq_zero $ by simp
@[simp] lemma finrank_bot : finrank K (⊥ : submodule K V) = 0 :=
begin
haveI := finite_dimensional_bot K V,
convert finrank_eq_dim K (⊥ : submodule K V),
rw dim_bot, norm_cast
end
variables {K V}
lemma bot_eq_top_of_dim_eq_zero (h : module.rank K V = 0) : (⊥ : submodule K V) = ⊤ :=
begin
haveI := finite_dimensional_of_dim_eq_zero h,
apply eq_top_of_finrank_eq,
rw [finrank_bot, finrank_eq_zero_of_dim_eq_zero h]
end
@[simp] theorem dim_eq_zero {S : submodule K V} : module.rank K S = 0 ↔ S = ⊥ :=
⟨λ h, (submodule.eq_bot_iff _).2 $ λ x hx, congr_arg subtype.val $
((submodule.eq_bot_iff _).1 $ eq.symm $ bot_eq_top_of_dim_eq_zero h) ⟨x, hx⟩ submodule.mem_top,
λ h, by rw [h, dim_bot]⟩
@[simp] theorem finrank_eq_zero {S : submodule K V} [finite_dimensional K S] :
finrank K S = 0 ↔ S = ⊥ :=
by rw [← dim_eq_zero, ← finrank_eq_dim, ← @nat.cast_zero cardinal, cardinal.nat_cast_inj]
end zero_dim
namespace submodule
open is_noetherian finite_dimensional
/-- A submodule is finitely generated if and only if it is finite-dimensional -/
theorem fg_iff_finite_dimensional (s : submodule K V) :
s.fg ↔ finite_dimensional K s :=
⟨λh, is_noetherian_of_fg_of_noetherian s h,
λh, by { rw ← map_subtype_top s, exact fg_map (iff_fg.1 h).out }⟩
/-- A submodule contained in a finite-dimensional submodule is
finite-dimensional. -/
lemma finite_dimensional_of_le {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (h : S₁ ≤ S₂) :
finite_dimensional K S₁ :=
is_noetherian.iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_le_of_submodule _ _ h) (dim_lt_omega K S₂))
/-- The inf of two submodules, the first finite-dimensional, is
finite-dimensional. -/
instance finite_dimensional_inf_left (S₁ S₂ : submodule K V) [finite_dimensional K S₁] :
finite_dimensional K (S₁ ⊓ S₂ : submodule K V) :=
finite_dimensional_of_le inf_le_left
/-- The inf of two submodules, the second finite-dimensional, is
finite-dimensional. -/
instance finite_dimensional_inf_right (S₁ S₂ : submodule K V) [finite_dimensional K S₂] :
finite_dimensional K (S₁ ⊓ S₂ : submodule K V) :=
finite_dimensional_of_le inf_le_right
/-- The sup of two finite-dimensional submodules is
finite-dimensional. -/
instance finite_dimensional_sup (S₁ S₂ : submodule K V) [h₁ : finite_dimensional K S₁]
[h₂ : finite_dimensional K S₂] : finite_dimensional K (S₁ ⊔ S₂ : submodule K V) :=
begin
rw ←submodule.fg_iff_finite_dimensional at *,
exact submodule.fg_sup h₁ h₂
end
/-- In a finite-dimensional vector space, the dimensions of a submodule and of the corresponding
quotient add up to the dimension of the space. -/
theorem finrank_quotient_add_finrank [finite_dimensional K V] (s : submodule K V) :
finrank K s.quotient + finrank K s = finrank K V :=
begin
have := dim_quotient_add_dim s,
rw [← finrank_eq_dim, ← finrank_eq_dim, ← finrank_eq_dim] at this,
exact_mod_cast this
end
/-- The dimension of a submodule is bounded by the dimension of the ambient space. -/
lemma finrank_le [finite_dimensional K V] (s : submodule K V) : finrank K s ≤ finrank K V :=
by { rw ← s.finrank_quotient_add_finrank, exact nat.le_add_left _ _ }
/-- The dimension of a strict submodule is strictly bounded by the dimension of the ambient
space. -/
lemma finrank_lt [finite_dimensional K V] {s : submodule K V} (h : s < ⊤) :
finrank K s < finrank K V :=
begin
rw [← s.finrank_quotient_add_finrank, add_comm],
exact nat.lt_add_of_zero_lt_left _ _ (finrank_pos_iff.mpr (quotient.nontrivial_of_lt_top _ h))
end
/-- The dimension of a quotient is bounded by the dimension of the ambient space. -/
lemma finrank_quotient_le [finite_dimensional K V] (s : submodule K V) :
finrank K s.quotient ≤ finrank K V :=
by { rw ← s.finrank_quotient_add_finrank, exact nat.le_add_right _ _ }
/-- The sum of the dimensions of s + t and s ∩ t is the sum of the dimensions of s and t -/
theorem dim_sup_add_dim_inf_eq (s t : submodule K V)
[finite_dimensional K s] [finite_dimensional K t] :
finrank K ↥(s ⊔ t) + finrank K ↥(s ⊓ t) = finrank K ↥s + finrank K ↥t :=
begin
have key : module.rank K ↥(s ⊔ t) + module.rank K ↥(s ⊓ t) =
module.rank K s + module.rank K t := dim_sup_add_dim_inf_eq s t,
repeat { rw ←finrank_eq_dim at key },
norm_cast at key,
exact key
end
lemma eq_top_of_disjoint [finite_dimensional K V] (s t : submodule K V)
(hdim : finrank K s + finrank K t = finrank K V)
(hdisjoint : disjoint s t) : s ⊔ t = ⊤ :=
begin
have h_finrank_inf : finrank K ↥(s ⊓ t) = 0,
{ rw [disjoint, le_bot_iff] at hdisjoint,
rw [hdisjoint, finrank_bot] },
apply eq_top_of_finrank_eq,
rw ←hdim,
convert s.dim_sup_add_dim_inf_eq t,
rw h_finrank_inf,
refl,
end
end submodule
namespace linear_equiv
open finite_dimensional
/-- Finite dimensionality is preserved under linear equivalence. -/
protected theorem finite_dimensional (f : V ≃ₗ[K] V₂) [finite_dimensional K V] :
finite_dimensional K V₂ :=
is_noetherian_of_linear_equiv f
/-- The dimension of a finite dimensional space is preserved under linear equivalence. -/
theorem finrank_eq (f : V ≃ₗ[K] V₂) [finite_dimensional K V] :
finrank K V = finrank K V₂ :=
begin
haveI : finite_dimensional K V₂ := f.finite_dimensional,
simpa [← finrank_eq_dim] using f.lift_dim_eq
end
end linear_equiv
instance finite_dimensional_finsupp {ι : Type*} [fintype ι] [finite_dimensional K V] :
finite_dimensional K (ι →₀ V) :=
(finsupp.linear_equiv_fun_on_fintype K : (ι →₀ V) ≃ₗ[K] (ι → V)).symm.finite_dimensional
namespace finite_dimensional
/--
Two finite-dimensional vector spaces are isomorphic if they have the same (finite) dimension.
-/
theorem nonempty_linear_equiv_of_finrank_eq [finite_dimensional K V] [finite_dimensional K V₂]
(cond : finrank K V = finrank K V₂) : nonempty (V ≃ₗ[K] V₂) :=
nonempty_linear_equiv_of_lift_dim_eq $ by simp only [← finrank_eq_dim, cond, lift_nat_cast]
/--
Two finite-dimensional vector spaces are isomorphic if and only if they have the same (finite)
dimension.
-/
theorem nonempty_linear_equiv_iff_finrank_eq [finite_dimensional K V] [finite_dimensional K V₂] :
nonempty (V ≃ₗ[K] V₂) ↔ finrank K V = finrank K V₂ :=
⟨λ ⟨h⟩, h.finrank_eq, λ h, nonempty_linear_equiv_of_finrank_eq h⟩
section
variables (V V₂)
/--
Two finite-dimensional vector spaces are isomorphic if they have the same (finite) dimension.
-/
noncomputable def linear_equiv.of_finrank_eq [finite_dimensional K V] [finite_dimensional K V₂]
(cond : finrank K V = finrank K V₂) : V ≃ₗ[K] V₂ :=
classical.choice $ nonempty_linear_equiv_of_finrank_eq cond
end
lemma eq_of_le_of_finrank_le {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (hle : S₁ ≤ S₂)
(hd : finrank K S₂ ≤ finrank K S₁) : S₁ = S₂ :=
begin
rw ←linear_equiv.finrank_eq (submodule.comap_subtype_equiv_of_le hle) at hd,
exact le_antisymm hle (submodule.comap_subtype_eq_top.1 (eq_top_of_finrank_eq
(le_antisymm (comap (submodule.subtype S₂) S₁).finrank_le hd))),
end
/-- If a submodule is less than or equal to a finite-dimensional
submodule with the same dimension, they are equal. -/
lemma eq_of_le_of_finrank_eq {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (hle : S₁ ≤ S₂)
(hd : finrank K S₁ = finrank K S₂) : S₁ = S₂ :=
eq_of_le_of_finrank_le hle hd.ge
variables [finite_dimensional K V] [finite_dimensional K V₂]
/-- Given isomorphic subspaces `p q` of vector spaces `V` and `V₁` respectively,
`p.quotient` is isomorphic to `q.quotient`. -/
noncomputable def linear_equiv.quot_equiv_of_equiv
{p : subspace K V} {q : subspace K V₂}
(f₁ : p ≃ₗ[K] q) (f₂ : V ≃ₗ[K] V₂) : p.quotient ≃ₗ[K] q.quotient :=
linear_equiv.of_finrank_eq _ _
begin
rw [← @add_right_cancel_iff _ _ (finrank K p), submodule.finrank_quotient_add_finrank,
linear_equiv.finrank_eq f₁, submodule.finrank_quotient_add_finrank,
linear_equiv.finrank_eq f₂],
end
/-- Given the subspaces `p q`, if `p.quotient ≃ₗ[K] q`, then `q.quotient ≃ₗ[K] p` -/
noncomputable def linear_equiv.quot_equiv_of_quot_equiv
{p q : subspace K V} (f : p.quotient ≃ₗ[K] q) : q.quotient ≃ₗ[K] p :=
linear_equiv.of_finrank_eq _ _
begin
rw [← @add_right_cancel_iff _ _ (finrank K q), submodule.finrank_quotient_add_finrank,
← linear_equiv.finrank_eq f, add_comm, submodule.finrank_quotient_add_finrank]
end
@[simp]
lemma finrank_map_subtype_eq (p : subspace K V) (q : subspace K p) :
finite_dimensional.finrank K (q.map p.subtype) = finite_dimensional.finrank K q :=
(submodule.equiv_subtype_map p q).symm.finrank_eq
end finite_dimensional
namespace linear_map
open finite_dimensional
/-- On a finite-dimensional space, an injective linear map is surjective. -/
lemma surjective_of_injective [finite_dimensional K V] {f : V →ₗ[K] V}
(hinj : injective f) : surjective f :=
begin
have h := dim_eq_of_injective _ hinj,
rw [← finrank_eq_dim, ← finrank_eq_dim, nat_cast_inj] at h,
exact range_eq_top.1 (eq_top_of_finrank_eq h.symm)
end
/-- On a finite-dimensional space, a linear map is injective if and only if it is surjective. -/
lemma injective_iff_surjective [finite_dimensional K V] {f : V →ₗ[K] V} :
injective f ↔ surjective f :=
⟨surjective_of_injective,
λ hsurj, let ⟨g, hg⟩ := f.exists_right_inverse_of_surjective (range_eq_top.2 hsurj) in
have function.right_inverse g f, from linear_map.ext_iff.1 hg,
(left_inverse_of_surjective_of_right_inverse
(surjective_of_injective this.injective) this).injective⟩
lemma ker_eq_bot_iff_range_eq_top [finite_dimensional K V] {f : V →ₗ[K] V} :
f.ker = ⊥ ↔ f.range = ⊤ :=
by rw [range_eq_top, ker_eq_bot, injective_iff_surjective]
/-- In a finite-dimensional space, if linear maps are inverse to each other on one side then they
are also inverse to each other on the other side. -/
lemma mul_eq_one_of_mul_eq_one [finite_dimensional K V] {f g : V →ₗ[K] V} (hfg : f * g = 1) :
g * f = 1 :=
have ginj : injective g, from has_left_inverse.injective
⟨f, (λ x, show (f * g) x = (1 : V →ₗ[K] V) x, by rw hfg; refl)⟩,
let ⟨i, hi⟩ := g.exists_right_inverse_of_surjective
(range_eq_top.2 (injective_iff_surjective.1 ginj)) in
have f * (g * i) = f * 1, from congr_arg _ hi,
by rw [← mul_assoc, hfg, one_mul, mul_one] at this; rwa ← this
/-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if
they are inverse to each other on the other side. -/
lemma mul_eq_one_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f * g = 1 ↔ g * f = 1 :=
⟨mul_eq_one_of_mul_eq_one, mul_eq_one_of_mul_eq_one⟩
/-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if
they are inverse to each other on the other side. -/
lemma comp_eq_id_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f.comp g = id ↔ g.comp f = id :=
mul_eq_one_comm
/-- The image under an onto linear map of a finite-dimensional space is also finite-dimensional. -/
lemma finite_dimensional_of_surjective [h : finite_dimensional K V]
(f : V →ₗ[K] V₂) (hf : f.range = ⊤) : finite_dimensional K V₂ :=
is_noetherian_of_surjective V f hf
/-- The range of a linear map defined on a finite-dimensional space is also finite-dimensional. -/
instance finite_dimensional_range [h : finite_dimensional K V] (f : V →ₗ[K] V₂) :
finite_dimensional K f.range :=
f.quot_ker_equiv_range.finite_dimensional
/-- rank-nullity theorem : the dimensions of the kernel and the range of a linear map add up to
the dimension of the source space. -/
theorem finrank_range_add_finrank_ker [finite_dimensional K V] (f : V →ₗ[K] V₂) :
finrank K f.range + finrank K f.ker = finrank K V :=
by { rw [← f.quot_ker_equiv_range.finrank_eq], exact submodule.finrank_quotient_add_finrank _ }
end linear_map
namespace linear_equiv
open finite_dimensional
variables [finite_dimensional K V]
/-- The linear equivalence corresponging to an injective endomorphism. -/
noncomputable def of_injective_endo (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) : V ≃ₗ[K] V :=
(linear_equiv.of_injective f h_inj).trans
(linear_equiv.of_top _ (linear_map.ker_eq_bot_iff_range_eq_top.1 h_inj))
@[simp] lemma coe_of_injective_endo (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) :
⇑(of_injective_endo f h_inj) = f := rfl
@[simp] lemma of_injective_endo_right_inv (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) :
f * (of_injective_endo f h_inj).symm = 1 :=
linear_map.ext $ (of_injective_endo f h_inj).apply_symm_apply
@[simp] lemma of_injective_endo_left_inv (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) :
((of_injective_endo f h_inj).symm : V →ₗ[K] V) * f = 1 :=
linear_map.ext $ (of_injective_endo f h_inj).symm_apply_apply
end linear_equiv
namespace linear_map
lemma is_unit_iff [finite_dimensional K V] (f : V →ₗ[K] V): is_unit f ↔ f.ker = ⊥ :=
begin
split,
{ rintro ⟨u, rfl⟩,
exact linear_map.ker_eq_bot_of_inverse u.inv_mul },
{ intro h_inj,
exact ⟨⟨f, (linear_equiv.of_injective_endo f h_inj).symm.to_linear_map,
linear_equiv.of_injective_endo_right_inv f h_inj,
linear_equiv.of_injective_endo_left_inv f h_inj⟩, rfl⟩ }
end
end linear_map
open module finite_dimensional
section top
@[simp]
theorem finrank_top : finrank K (⊤ : submodule K V) = finrank K V :=
by { unfold finrank, simp [dim_top] }
end top
lemma finrank_zero_iff_forall_zero [finite_dimensional K V] :
finrank K V = 0 ↔ ∀ x : V, x = 0 :=
finrank_zero_iff.trans (subsingleton_iff_forall_eq 0)
/-- If `ι` is an empty type and `V` is zero-dimensional, there is a unique `ι`-indexed basis. -/
noncomputable def basis_of_finrank_zero [finite_dimensional K V]
{ι : Type*} [is_empty ι] (hV : finrank K V = 0) :
basis ι K V :=
begin
haveI : subsingleton V := finrank_zero_iff.1 hV,
exact basis.empty _
end
namespace linear_map
theorem injective_iff_surjective_of_finrank_eq_finrank [finite_dimensional K V]
[finite_dimensional K V₂] (H : finrank K V = finrank K V₂) {f : V →ₗ[K] V₂} :
function.injective f ↔ function.surjective f :=
begin
have := finrank_range_add_finrank_ker f,
rw [← ker_eq_bot, ← range_eq_top], refine ⟨λ h, _, λ h, _⟩,
{ rw [h, finrank_bot, add_zero, H] at this, exact eq_top_of_finrank_eq this },
{ rw [h, finrank_top, H] at this, exact finrank_eq_zero.1 (add_right_injective _ this) }
end
lemma ker_eq_bot_iff_range_eq_top_of_finrank_eq_finrank [finite_dimensional K V]
[finite_dimensional K V₂] (H : finrank K V = finrank K V₂) {f : V →ₗ[K] V₂} :
f.ker = ⊥ ↔ f.range = ⊤ :=
by rw [range_eq_top, ker_eq_bot, injective_iff_surjective_of_finrank_eq_finrank H]
theorem finrank_le_finrank_of_injective [finite_dimensional K V] [finite_dimensional K V₂]
{f : V →ₗ[K] V₂} (hf : function.injective f) : finrank K V ≤ finrank K V₂ :=
calc finrank K V
= finrank K f.range + finrank K f.ker : (finrank_range_add_finrank_ker f).symm
... = finrank K f.range : by rw [ker_eq_bot.2 hf, finrank_bot, add_zero]
... ≤ finrank K V₂ : submodule.finrank_le _
/-- Given a linear map `f` between two vector spaces with the same dimension, if
`ker f = ⊥` then `linear_equiv_of_ker_eq_bot` is the induced isomorphism
between the two vector spaces. -/
noncomputable def linear_equiv_of_ker_eq_bot
[finite_dimensional K V] [finite_dimensional K V₂]
(f : V →ₗ[K] V₂) (hf : f.ker = ⊥) (hdim : finrank K V = finrank K V₂) : V ≃ₗ[K] V₂ :=
linear_equiv.of_bijective f hf (linear_map.range_eq_top.2 $
(linear_map.injective_iff_surjective_of_finrank_eq_finrank hdim).1 (linear_map.ker_eq_bot.1 hf))
@[simp] lemma linear_equiv_of_ker_eq_bot_apply
[finite_dimensional K V] [finite_dimensional K V₂]
{f : V →ₗ[K] V₂} (hf : f.ker = ⊥) (hdim : finrank K V = finrank K V₂) (x : V) :
f.linear_equiv_of_ker_eq_bot hf hdim x = f x := rfl
end linear_map
namespace alg_hom
lemma bijective {F : Type*} [field F] {E : Type*} [field E] [algebra F E]
[finite_dimensional F E] (ϕ : E →ₐ[F] E) : function.bijective ϕ :=
have inj : function.injective ϕ.to_linear_map := ϕ.to_ring_hom.injective,
⟨inj, (linear_map.injective_iff_surjective_of_finrank_eq_finrank rfl).mp inj⟩
end alg_hom
/-- Bijection between algebra equivalences and algebra homomorphisms -/
noncomputable def alg_equiv_equiv_alg_hom (F : Type u) [field F] (E : Type v) [field E]
[algebra F E] [finite_dimensional F E] : (E ≃ₐ[F] E) ≃ (E →ₐ[F] E) :=
{ to_fun := λ ϕ, ϕ.to_alg_hom,
inv_fun := λ ϕ, alg_equiv.of_bijective ϕ ϕ.bijective,
left_inv := λ _, by {ext, refl},
right_inv := λ _, by {ext, refl} }
section
/-- An integral domain that is module-finite as an algebra over a field is a field. -/
noncomputable def field_of_finite_dimensional (F K : Type*) [field F] [integral_domain K]
[algebra F K] [finite_dimensional F K] : field K :=
{ inv := λ x, if H : x = 0 then 0 else classical.some $
(show function.surjective (algebra.lmul_left F x), from
linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' H).1) 1,
mul_inv_cancel := λ x hx, show x * dite _ _ _ = _, by { rw dif_neg hx,
exact classical.some_spec ((show function.surjective (algebra.lmul_left F x), from
linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' hx).1) 1) },
inv_zero := dif_pos rfl,
.. ‹integral_domain K› }
end
namespace submodule
lemma finrank_mono [finite_dimensional K V] :
monotone (λ (s : submodule K V), finrank K s) :=
λ s t hst,
calc finrank K s = finrank K (comap t.subtype s)
: linear_equiv.finrank_eq (comap_subtype_equiv_of_le hst).symm
... ≤ finrank K t : submodule.finrank_le _
lemma lt_of_le_of_finrank_lt_finrank {s t : submodule K V}
(le : s ≤ t) (lt : finrank K s < finrank K t) : s < t :=
lt_of_le_of_ne le (λ h, ne_of_lt lt (by rw h))
lemma lt_top_of_finrank_lt_finrank {s : submodule K V}
(lt : finrank K s < finrank K V) : s < ⊤ :=
begin
rw ← @finrank_top K V at lt,
exact lt_of_le_of_finrank_lt_finrank le_top lt
end
lemma finrank_lt_finrank_of_lt [finite_dimensional K V] {s t : submodule K V} (hst : s < t) :
finrank K s < finrank K t :=
begin
rw linear_equiv.finrank_eq (comap_subtype_equiv_of_le (le_of_lt hst)).symm,
refine finrank_lt (lt_of_le_of_ne le_top _),
intro h_eq_top,
rw comap_subtype_eq_top at h_eq_top,
apply not_le_of_lt hst h_eq_top,
end
lemma finrank_add_eq_of_is_compl
[finite_dimensional K V] {U W : submodule K V} (h : is_compl U W) :
finrank K U + finrank K W = finrank K V :=
begin
rw [← submodule.dim_sup_add_dim_inf_eq, top_le_iff.1 h.2, le_bot_iff.1 h.1,
finrank_bot, add_zero],
exact finrank_top
end
end submodule
section span
open submodule
lemma finrank_span_le_card (s : set V) [fin : fintype s] :
finrank K (span K s) ≤ s.to_finset.card :=
begin
haveI := span_of_finite K ⟨fin⟩,
have : module.rank K (span K s) ≤ (mk s : cardinal) := dim_span_le s,
rw [←finrank_eq_dim, cardinal.fintype_card, ←set.to_finset_card] at this,
exact_mod_cast this
end
lemma finrank_span_finset_le_card (s : finset V) :
finrank K (span K (s : set V)) ≤ s.card :=
calc finrank K (span K (s : set V)) ≤ (s : set V).to_finset.card : finrank_span_le_card s
... = s.card : by simp
lemma finrank_span_eq_card {ι : Type*} [fintype ι] {b : ι → V}
(hb : linear_independent K b) :
finrank K (span K (set.range b)) = fintype.card ι :=
begin
haveI : finite_dimensional K (span K (set.range b)) := span_of_finite K (set.finite_range b),
have : module.rank K (span K (set.range b)) = (mk (set.range b) : cardinal) := dim_span hb,
rwa [←finrank_eq_dim, ←lift_inj, mk_range_eq_of_injective hb.injective,
cardinal.fintype_card, lift_nat_cast, lift_nat_cast, nat_cast_inj] at this,
end
lemma finrank_span_set_eq_card (s : set V) [fin : fintype s]
(hs : linear_independent K (coe : s → V)) :
finrank K (span K s) = s.to_finset.card :=
begin
haveI := span_of_finite K ⟨fin⟩,
have : module.rank K (span K s) = (mk s : cardinal) := dim_span_set hs,
rw [←finrank_eq_dim, cardinal.fintype_card, ←set.to_finset_card] at this,
exact_mod_cast this
end
lemma finrank_span_finset_eq_card (s : finset V)
(hs : linear_independent K (coe : s → V)) :
finrank K (span K (s : set V)) = s.card :=
begin
convert finrank_span_set_eq_card ↑s hs,
ext,
simp
end
lemma span_lt_of_subset_of_card_lt_finrank {s : set V} [fintype s] {t : submodule K V}
(subset : s ⊆ t) (card_lt : s.to_finset.card < finrank K t) : span K s < t :=
lt_of_le_of_finrank_lt_finrank
(span_le.mpr subset)
(lt_of_le_of_lt (finrank_span_le_card _) card_lt)
lemma span_lt_top_of_card_lt_finrank {s : set V} [fintype s]
(card_lt : s.to_finset.card < finrank K V) : span K s < ⊤ :=
lt_top_of_finrank_lt_finrank (lt_of_le_of_lt (finrank_span_le_card _) card_lt)
lemma finrank_span_singleton {v : V} (hv : v ≠ 0) : finrank K (K ∙ v) = 1 :=
begin
apply le_antisymm,
{ exact finrank_span_le_card ({v} : set V) },
{ rw [nat.succ_le_iff, finrank_pos_iff],
use [⟨v, mem_span_singleton_self v⟩, 0],
simp [hv] }
end
end span
section basis
lemma linear_independent_of_span_eq_top_of_card_eq_finrank {ι : Type*} [fintype ι] {b : ι → V}
(span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = finrank K V) :
linear_independent K b :=
linear_independent_iff'.mpr $ λ s g dependent i i_mem_s,
begin
by_contra gx_ne_zero,
-- We'll derive a contradiction by showing `b '' (univ \ {i})` of cardinality `n - 1`
-- spans a vector space of dimension `n`.
refine ne_of_lt (span_lt_top_of_card_lt_finrank
(show (b '' (set.univ \ {i})).to_finset.card < finrank K V, from _)) _,
{ calc (b '' (set.univ \ {i})).to_finset.card = ((set.univ \ {i}).to_finset.image b).card
: by rw [set.to_finset_card, fintype.card_of_finset]
... ≤ (set.univ \ {i}).to_finset.card : finset.card_image_le
... = (finset.univ.erase i).card : congr_arg finset.card (finset.ext (by simp [and_comm]))
... < finset.univ.card : finset.card_erase_lt_of_mem (finset.mem_univ i)
... = finrank K V : card_eq },
-- We already have that `b '' univ` spans the whole space,
-- so we only need to show that the span of `b '' (univ \ {i})` contains each `b j`.
refine trans (le_antisymm (span_mono (set.image_subset_range _ _)) (span_le.mpr _)) span_eq,
rintros _ ⟨j, rfl, rfl⟩,
-- The case that `j ≠ i` is easy because `b j ∈ b '' (univ \ {i})`.
by_cases j_eq : j = i,
swap,
{ refine subset_span ⟨j, (set.mem_diff _).mpr ⟨set.mem_univ _, _⟩, rfl⟩,
exact mt set.mem_singleton_iff.mp j_eq },
-- To show `b i ∈ span (b '' (univ \ {i}))`, we use that it's a weighted sum
-- of the other `b j`s.
rw [j_eq, set_like.mem_coe, show b i = -((g i)⁻¹ • (s.erase i).sum (λ j, g j • b j)), from _],
{ refine submodule.neg_mem _ (smul_mem _ _ (sum_mem _ (λ k hk, _))),
obtain ⟨k_ne_i, k_mem⟩ := finset.mem_erase.mp hk,
refine smul_mem _ _ (subset_span ⟨k, _, rfl⟩),
simpa using k_mem },
-- To show `b i` is a weighted sum of the other `b j`s, we'll rewrite this sum
-- to have the form of the assumption `dependent`.
apply eq_neg_of_add_eq_zero,
calc b i + (g i)⁻¹ • (s.erase i).sum (λ j, g j • b j)
= (g i)⁻¹ • (g i • b i + (s.erase i).sum (λ j, g j • b j))
: by rw [smul_add, ←mul_smul, inv_mul_cancel gx_ne_zero, one_smul]
... = (g i)⁻¹ • 0 : congr_arg _ _
... = 0 : smul_zero _,
-- And then it's just a bit of manipulation with finite sums.
rwa [← finset.insert_erase i_mem_s, finset.sum_insert (finset.not_mem_erase _ _)] at dependent
end
/-- A finite family of vectors is linearly independent if and only if
its cardinality equals the dimension of its span. -/
lemma linear_independent_iff_card_eq_finrank_span {ι : Type*} [fintype ι] {b : ι → V} :
linear_independent K b ↔ fintype.card ι = finrank K (span K (set.range b)) :=
begin
split,
{ intro h,
exact (finrank_span_eq_card h).symm },
{ intro hc,
let f := (submodule.subtype (span K (set.range b))),
let b' : ι → span K (set.range b) :=
λ i, ⟨b i, mem_span.2 (λ p hp, hp (set.mem_range_self _))⟩,
have hs : span K (set.range b') = ⊤,
{ rw eq_top_iff',
intro x,
have h : span K (f '' (set.range b')) = map f (span K (set.range b')) := span_image f,
have hf : f '' (set.range b') = set.range b, { ext x, simp [set.mem_image, set.mem_range] },
rw hf at h,
have hx : (x : V) ∈ span K (set.range b) := x.property,
conv at hx { congr, skip, rw h },
simpa [mem_map] using hx },
have hi : f.ker = ⊥ := ker_subtype _,
convert (linear_independent_of_span_eq_top_of_card_eq_finrank hs hc).map' _ hi }
end
/-- A family of `finrank K V` vectors forms a basis if they span the whole space. -/
noncomputable def basis_of_span_eq_top_of_card_eq_finrank {ι : Type*} [fintype ι] (b : ι → V)
(span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = finrank K V) :
basis ι K V :=
basis.mk (linear_independent_of_span_eq_top_of_card_eq_finrank span_eq card_eq) span_eq
@[simp] lemma coe_basis_of_span_eq_top_of_card_eq_finrank {ι : Type*} [fintype ι] (b : ι → V)
(span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = finrank K V) :
⇑(basis_of_span_eq_top_of_card_eq_finrank b span_eq card_eq) = b :=
basis.coe_mk _ _
/-- A finset of `finrank K V` vectors forms a basis if they span the whole space. -/
@[simps]
noncomputable def finset_basis_of_span_eq_top_of_card_eq_finrank {s : finset V}
(span_eq : span K (s : set V) = ⊤) (card_eq : s.card = finrank K V) :
basis (s : set V) K V :=
basis_of_span_eq_top_of_card_eq_finrank (coe : (s : set V) → V)
((@subtype.range_coe_subtype _ (λ x, x ∈ s)).symm ▸ span_eq)
(trans (fintype.card_coe _) card_eq)
/-- A set of `finrank K V` vectors forms a basis if they span the whole space. -/
@[simps]
noncomputable def set_basis_of_span_eq_top_of_card_eq_finrank {s : set V} [fintype s]
(span_eq : span K s = ⊤) (card_eq : s.to_finset.card = finrank K V) :
basis s K V :=
basis_of_span_eq_top_of_card_eq_finrank (coe : s → V)
((@subtype.range_coe_subtype _ s).symm ▸ span_eq)
(trans s.to_finset_card.symm card_eq)
lemma span_eq_top_of_linear_independent_of_card_eq_finrank
{ι : Type*} [hι : nonempty ι] [fintype ι] {b : ι → V}
(lin_ind : linear_independent K b) (card_eq : fintype.card ι = finrank K V) :
span K (set.range b) = ⊤ :=
begin
by_cases fin : (finite_dimensional K V),
{ haveI := fin,
by_contra ne_top,
have lt_top : span K (set.range b) < ⊤ := lt_of_le_of_ne le_top ne_top,
exact ne_of_lt (submodule.finrank_lt lt_top) (trans (finrank_span_eq_card lin_ind) card_eq) },
{ exfalso,
apply ne_of_lt (fintype.card_pos_iff.mpr hι),
symmetry,
calc fintype.card ι = finrank K V : card_eq
... = 0 : dif_neg (mt is_noetherian.iff_dim_lt_omega.mpr fin) }
end
/-- A linear independent family of `finrank K V` vectors forms a basis. -/
@[simps]
noncomputable def basis_of_linear_independent_of_card_eq_finrank
{ι : Type*} [nonempty ι] [fintype ι] {b : ι → V}
(lin_ind : linear_independent K b) (card_eq : fintype.card ι = finrank K V) :
basis ι K V :=
basis.mk lin_ind $
span_eq_top_of_linear_independent_of_card_eq_finrank lin_ind card_eq
@[simp] lemma coe_basis_of_linear_independent_of_card_eq_finrank
{ι : Type*} [nonempty ι] [fintype ι] {b : ι → V}
(lin_ind : linear_independent K b) (card_eq : fintype.card ι = finrank K V) :
⇑(basis_of_linear_independent_of_card_eq_finrank lin_ind card_eq) = b :=
basis.coe_mk _ _
/-- A linear independent finset of `finrank K V` vectors forms a basis. -/
@[simps]
noncomputable def finset_basis_of_linear_independent_of_card_eq_finrank
{s : finset V} (hs : s.nonempty)
(lin_ind : linear_independent K (coe : s → V)) (card_eq : s.card = finrank K V) :
basis s K V :=
@basis_of_linear_independent_of_card_eq_finrank _ _ _ _ _ _
⟨(⟨hs.some, hs.some_spec⟩ : s)⟩ _ _
lin_ind
(trans (fintype.card_coe _) card_eq)
@[simp] lemma coe_finset_basis_of_linear_independent_of_card_eq_finrank
{s : finset V} (hs : s.nonempty)
(lin_ind : linear_independent K (coe : s → V)) (card_eq : s.card = finrank K V) :
⇑(finset_basis_of_linear_independent_of_card_eq_finrank hs lin_ind card_eq) = coe :=
basis.coe_mk _ _
/-- A linear independent set of `finrank K V` vectors forms a basis. -/
@[simps]
noncomputable def set_basis_of_linear_independent_of_card_eq_finrank
{s : set V} [nonempty s] [fintype s]
(lin_ind : linear_independent K (coe : s → V)) (card_eq : s.to_finset.card = finrank K V) :
basis s K V :=
basis_of_linear_independent_of_card_eq_finrank lin_ind (trans s.to_finset_card.symm card_eq)
@[simp] lemma coe_set_basis_of_linear_independent_of_card_eq_finrank
{s : set V} [nonempty s] [fintype s]
(lin_ind : linear_independent K (coe : s → V)) (card_eq : s.to_finset.card = finrank K V) :
⇑(set_basis_of_linear_independent_of_card_eq_finrank lin_ind card_eq) = coe :=
basis.coe_mk _ _
end basis
/-!
We now give characterisations of `finrank K V = 1` and `finrank K V ≤ 1`.
-/
section finrank_eq_one
/-- If there is a nonzero vector and every other vector is a multiple of it,
then the module has dimension one. -/
lemma finrank_eq_one (v : V) (n : v ≠ 0) (h : ∀ w : V, ∃ c : K, c • v = w) :
finrank K V = 1 :=
begin
obtain ⟨b⟩ := (basis.basis_singleton_iff punit).mpr ⟨v, n, h⟩,
rw [finrank_eq_card_basis b, fintype.card_punit]
end
/--
If every vector is a multiple of some `v : V`, then `V` has dimension at most one.
-/
lemma finrank_le_one (v : V) (h : ∀ w : V, ∃ c : K, c • v = w) :
finrank K V ≤ 1 :=
begin
by_cases n : v = 0,
{ subst n,
convert zero_le_one,
haveI := subsingleton_of_forall_eq (0 : V) (λ w, by { obtain ⟨c, rfl⟩ := h w, simp, }),
exact finrank_zero_of_subsingleton, },
{ exact (finrank_eq_one v n h).le, }
end
/--
A vector space with a nonzero vector `v` has dimension 1 iff `v` spans.
-/
lemma finrank_eq_one_iff_of_nonzero (v : V) (nz : v ≠ 0) :
finrank K V = 1 ↔ span K ({v} : set V) = ⊤ :=
⟨λ h, by simpa using (basis_singleton punit h v nz).span_eq,
λ s, finrank_eq_card_basis (basis.mk (linear_independent_singleton nz) (by { convert s, simp }))⟩
/--
A module with a nonzero vector `v` has dimension 1 iff every vector is a multiple of `v`.
-/
lemma finrank_eq_one_iff_of_nonzero' (v : V) (nz : v ≠ 0) :
finrank K V = 1 ↔ ∀ w : V, ∃ c : K, c • v = w :=
begin
rw finrank_eq_one_iff_of_nonzero v nz,
apply span_singleton_eq_top_iff,
end
/--
A module has dimension 1 iff there is some `v : V` so `{v}` is a basis.
-/
lemma finrank_eq_one_iff (ι : Type*) [unique ι] :
finrank K V = 1 ↔ nonempty (basis ι K V) :=
begin
fsplit,
{ intro h,
haveI := finite_dimensional_of_finrank (_root_.zero_lt_one.trans_le h.symm.le),
exact ⟨basis_unique ι h⟩ },
{ rintro ⟨b⟩,
simpa using finrank_eq_card_basis b }
end
/--
A module has dimension 1 iff there is some nonzero `v : V` so every vector is a multiple of `v`.
-/
lemma finrank_eq_one_iff' :
finrank K V = 1 ↔ ∃ (v : V) (n : v ≠ 0), ∀ w : V, ∃ c : K, c • v = w :=
begin
convert finrank_eq_one_iff punit,
simp only [exists_prop, eq_iff_iff, ne.def],
convert (basis.basis_singleton_iff punit).symm,
funext v,
simp,
apply_instance, apply_instance, -- Not sure why this aren't found automatically.
end
/--
A finite dimensional module has dimension at most 1 iff
there is some `v : V` so every vector is a multiple of `v`.
-/
lemma finrank_le_one_iff [finite_dimensional K V] :
finrank K V ≤ 1 ↔ ∃ (v : V), ∀ w : V, ∃ c : K, c • v = w :=
begin
fsplit,
{ intro h,
by_cases h' : finrank K V = 0,
{ use 0, intro w, use 0, haveI := finrank_zero_iff.mp h', apply subsingleton.elim, },
{ replace h' := zero_lt_iff.mpr h', have : finrank K V = 1, { linarith },
obtain ⟨v, -, p⟩ := finrank_eq_one_iff'.mp this,
use ⟨v, p⟩, }, },
{ rintro ⟨v, p⟩,
exact finrank_le_one v p, }
end
end finrank_eq_one
section subalgebra_dim
open module
variables {F E : Type*} [field F] [field E] [algebra F E]
lemma subalgebra.dim_eq_one_of_eq_bot {S : subalgebra F E} (h : S = ⊥) : module.rank F S = 1 :=
begin
rw [← S.to_submodule_equiv.dim_eq, h,
(linear_equiv.of_eq (⊥ : subalgebra F E).to_submodule _ algebra.to_submodule_bot).dim_eq,
dim_span_set],
exacts [mk_singleton _, linear_independent_singleton one_ne_zero]
end
@[simp]
lemma subalgebra.dim_bot : module.rank F (⊥ : subalgebra F E) = 1 :=
subalgebra.dim_eq_one_of_eq_bot rfl
lemma subalgebra_top_dim_eq_submodule_top_dim :
module.rank F (⊤ : subalgebra F E) = module.rank F (⊤ : submodule F E) :=
by { rw ← algebra.top_to_submodule, refl }
lemma subalgebra_top_finrank_eq_submodule_top_finrank :
finrank F (⊤ : subalgebra F E) = finrank F (⊤ : submodule F E) :=
by { rw ← algebra.top_to_submodule, refl }
lemma subalgebra.dim_top : module.rank F (⊤ : subalgebra F E) = module.rank F E :=
by { rw subalgebra_top_dim_eq_submodule_top_dim, exact dim_top }
lemma subalgebra.finite_dimensional_bot : finite_dimensional F (⊥ : subalgebra F E) :=
finite_dimensional_of_dim_eq_one subalgebra.dim_bot
@[simp]
lemma subalgebra.finrank_bot : finrank F (⊥ : subalgebra F E) = 1 :=
begin
haveI : finite_dimensional F (⊥ : subalgebra F E) := subalgebra.finite_dimensional_bot,
have : module.rank F (⊥ : subalgebra F E) = 1 := subalgebra.dim_bot,
rw ← finrank_eq_dim at this,
norm_cast at *,
simp *,
end
lemma subalgebra.finrank_eq_one_of_eq_bot {S : subalgebra F E} (h : S = ⊥) : finrank F S = 1 :=
by { rw h, exact subalgebra.finrank_bot }
lemma subalgebra.eq_bot_of_finrank_one {S : subalgebra F E} (h : finrank F S = 1) : S = ⊥ :=
begin
rw eq_bot_iff,
let b : set S := {1},
have : fintype b := unique.fintype,
have b_lin_ind : linear_independent F (coe : b → S) := linear_independent_singleton one_ne_zero,
have b_card : fintype.card b = 1 := fintype.card_of_subsingleton _,
let hb := set_basis_of_linear_independent_of_card_eq_finrank
b_lin_ind (by simp only [*, set.to_finset_card]),
have b_spans := hb.span_eq,
intros x hx,
rw [algebra.mem_bot],
have x_in_span_b : (⟨x, hx⟩ : S) ∈ submodule.span F b,
{ rw [coe_set_basis_of_linear_independent_of_card_eq_finrank, subtype.range_coe] at b_spans,
rw b_spans,
exact submodule.mem_top, },
obtain ⟨a, ha⟩ := submodule.mem_span_singleton.mp x_in_span_b,
replace ha : a • 1 = x := by injections with ha,
exact ⟨a, by rw [← ha, algebra.smul_def, mul_one]⟩,
end
lemma subalgebra.eq_bot_of_dim_one {S : subalgebra F E} (h : module.rank F S = 1) : S = ⊥ :=
begin
haveI : finite_dimensional F S := finite_dimensional_of_dim_eq_one h,
rw ← finrank_eq_dim at h,
norm_cast at h,
exact subalgebra.eq_bot_of_finrank_one h,
end
@[simp]
lemma subalgebra.bot_eq_top_of_dim_eq_one (h : module.rank F E = 1) : (⊥ : subalgebra F E) = ⊤ :=
begin
rw [← dim_top, ← subalgebra_top_dim_eq_submodule_top_dim] at h,
exact eq.symm (subalgebra.eq_bot_of_dim_one h),
end
@[simp]
lemma subalgebra.bot_eq_top_of_finrank_eq_one (h : finrank F E = 1) : (⊥ : subalgebra F E) = ⊤ :=
begin
rw [← finrank_top, ← subalgebra_top_finrank_eq_submodule_top_finrank] at h,
exact eq.symm (subalgebra.eq_bot_of_finrank_one h),
end
@[simp]
theorem subalgebra.dim_eq_one_iff {S : subalgebra F E} : module.rank F S = 1 ↔ S = ⊥ :=
⟨subalgebra.eq_bot_of_dim_one, subalgebra.dim_eq_one_of_eq_bot⟩
@[simp]
theorem subalgebra.finrank_eq_one_iff {S : subalgebra F E} : finrank F S = 1 ↔ S = ⊥ :=
⟨subalgebra.eq_bot_of_finrank_one, subalgebra.finrank_eq_one_of_eq_bot⟩
end subalgebra_dim
namespace module
namespace End
lemma exists_ker_pow_eq_ker_pow_succ [finite_dimensional K V] (f : End K V) :
∃ (k : ℕ), k ≤ finrank K V ∧ (f ^ k).ker = (f ^ k.succ).ker :=
begin
classical,
by_contradiction h_contra,
simp_rw [not_exists, not_and] at h_contra,
have h_le_ker_pow : ∀ (n : ℕ), n ≤ (finrank K V).succ → n ≤ finrank K (f ^ n).ker,
{ intros n hn,
induction n with n ih,
{ exact zero_le (finrank _ _) },
{ have h_ker_lt_ker : (f ^ n).ker < (f ^ n.succ).ker,
{ refine lt_of_le_of_ne _ (h_contra n (nat.le_of_succ_le_succ hn)),
rw pow_succ,
apply linear_map.ker_le_ker_comp },
have h_finrank_lt_finrank : finrank K (f ^ n).ker < finrank K (f ^ n.succ).ker,
{ apply submodule.finrank_lt_finrank_of_lt h_ker_lt_ker },
calc
n.succ ≤ (finrank K ↥(linear_map.ker (f ^ n))).succ :
nat.succ_le_succ (ih (nat.le_of_succ_le hn))
... ≤ finrank K ↥(linear_map.ker (f ^ n.succ)) :
nat.succ_le_of_lt h_finrank_lt_finrank } },
have h_le_finrank_V : ∀ n, finrank K (f ^ n).ker ≤ finrank K V :=
λ n, submodule.finrank_le _,
have h_any_n_lt: ∀ n, n ≤ (finrank K V).succ → n ≤ finrank K V :=
λ n hn, (h_le_ker_pow n hn).trans (h_le_finrank_V n),
show false,
from nat.not_succ_le_self _ (h_any_n_lt (finrank K V).succ (finrank K V).succ.le_refl),
end
lemma ker_pow_constant {f : End K V} {k : ℕ} (h : (f ^ k).ker = (f ^ k.succ).ker) :
∀ m, (f ^ k).ker = (f ^ (k + m)).ker
| 0 := by simp
| (m + 1) :=
begin
apply le_antisymm,
{ rw [add_comm, pow_add],
apply linear_map.ker_le_ker_comp },
{ rw [ker_pow_constant m, add_comm m 1, ←add_assoc, pow_add, pow_add f k m],
change linear_map.ker ((f ^ (k + 1)).comp (f ^ m)) ≤ linear_map.ker ((f ^ k).comp (f ^ m)),
rw [linear_map.ker_comp, linear_map.ker_comp, h, nat.add_one],
exact le_refl _, }
end
lemma ker_pow_eq_ker_pow_finrank_of_le [finite_dimensional K V]
{f : End K V} {m : ℕ} (hm : finrank K V ≤ m) :
(f ^ m).ker = (f ^ finrank K V).ker :=
begin
obtain ⟨k, h_k_le, hk⟩ :
∃ k, k ≤ finrank K V ∧ linear_map.ker (f ^ k) = linear_map.ker (f ^ k.succ) :=
exists_ker_pow_eq_ker_pow_succ f,
calc (f ^ m).ker = (f ^ (k + (m - k))).ker :
by rw nat.add_sub_of_le (h_k_le.trans hm)
... = (f ^ k).ker : by rw ker_pow_constant hk _
... = (f ^ (k + (finrank K V - k))).ker : ker_pow_constant hk (finrank K V - k)
... = (f ^ finrank K V).ker : by rw nat.add_sub_of_le h_k_le
end
lemma ker_pow_le_ker_pow_finrank [finite_dimensional K V] (f : End K V) (m : ℕ) :
(f ^ m).ker ≤ (f ^ finrank K V).ker :=
begin
by_cases h_cases: m < finrank K V,
{ rw [←nat.add_sub_of_le (nat.le_of_lt h_cases), add_comm, pow_add],
apply linear_map.ker_le_ker_comp },
{ rw [ker_pow_eq_ker_pow_finrank_of_le (le_of_not_lt h_cases)],
exact le_refl _ }
end
end End
end module
|
84bb0112cafd92f8ce640fc0bc3c9b7786f720c6 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/number_theory/primorial.lean | bcb4485006754fa9296a5ddb09c7fbf051f9a2e9 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 5,834 | lean | /-
Copyright (c) 2020 Patrick Stevens. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Stevens
-/
import algebra.big_operators.associated
import data.nat.choose.sum
import data.nat.parity
import tactic.ring_exp
/-!
# Primorial
This file defines the primorial function (the product of primes less than or equal to some bound),
and proves that `primorial n ≤ 4 ^ n`.
## Notations
We use the local notation `n#` for the primorial of `n`: that is, the product of the primes less
than or equal to `n`.
-/
open finset
open nat
open_locale big_operators nat
/-- The primorial `n#` of `n` is the product of the primes less than or equal to `n`.
-/
def primorial (n : ℕ) : ℕ := ∏ p in (filter nat.prime (range (n + 1))), p
local notation x`#` := primorial x
lemma primorial_succ {n : ℕ} (n_big : 1 < n) (r : n % 2 = 1) : (n + 1)# = n# :=
begin
refine prod_congr _ (λ _ _, rfl),
rw [range_succ, filter_insert, if_neg (λ h, _)],
have two_dvd : 2 ∣ n + 1 := dvd_iff_mod_eq_zero.mpr (by rw [← mod_add_mod, r, mod_self]),
linarith [(h.dvd_iff_eq (nat.bit0_ne_one 1)).mp two_dvd],
end
lemma dvd_choose_of_middling_prime (p : ℕ) (is_prime : nat.prime p) (m : ℕ)
(p_big : m + 1 < p) (p_small : p ≤ 2 * m + 1) : p ∣ choose (2 * m + 1) (m + 1) :=
begin
have m_size : m + 1 ≤ 2 * m + 1 := le_of_lt (lt_of_lt_of_le p_big p_small),
have s : ¬(p ∣ (m + 1)!),
{ intros p_div_fact,
exact lt_le_antisymm p_big (is_prime.dvd_factorial.mp p_div_fact), },
have t : ¬(p ∣ (2 * m + 1 - (m + 1))!),
{ intros p_div_fact,
refine lt_le_antisymm (lt_of_succ_lt p_big) _,
convert is_prime.dvd_factorial.mp p_div_fact,
rw [two_mul, add_assoc, nat.add_sub_cancel] },
have expanded :
choose (2 * m + 1) (m + 1) * (m + 1)! * (2 * m + 1 - (m + 1))! = (2 * m + 1)! :=
@choose_mul_factorial_mul_factorial (2 * m + 1) (m + 1) m_size,
have p_div_big_fact : p ∣ (2 * m + 1)! := (prime.dvd_factorial is_prime).mpr p_small,
rw [←expanded, mul_assoc] at p_div_big_fact,
obtain p_div_choose | p_div_facts : p ∣ choose (2 * m + 1) (m + 1) ∨ p ∣ _! * _! :=
(prime.dvd_mul is_prime).1 p_div_big_fact,
{ exact p_div_choose, },
cases (prime.dvd_mul is_prime).1 p_div_facts,
exacts [(s h).elim, (t h).elim],
end
lemma primorial_le_4_pow : ∀ (n : ℕ), n# ≤ 4 ^ n
| 0 := le_rfl
| 1 := le_of_inf_eq rfl
| (n + 2) :=
match nat.mod_two_eq_zero_or_one (n + 1) with
| or.inl n_odd :=
match nat.even_iff.2 n_odd with
| ⟨m, twice_m⟩ :=
have recurse : m + 1 < n + 2 := by linarith,
begin
calc (n + 2)#
= ∏ i in filter nat.prime (range (2 * m + 2)), i : by simpa [two_mul, ←twice_m]
... = ∏ i in filter nat.prime (finset.Ico (m + 2) (2 * m + 2) ∪ range (m + 2)), i :
begin
rw [range_eq_Ico, finset.union_comm, finset.Ico_union_Ico_eq_Ico],
{ exact bot_le },
{ simpa only [add_le_add_iff_right, two_mul] using nat.le_add_left m m },
end
... = ∏ i in (filter nat.prime (finset.Ico (m + 2) (2 * m + 2))
∪ (filter nat.prime (range (m + 2)))), i :
by rw filter_union
... = (∏ i in filter nat.prime (finset.Ico (m + 2) (2 * m + 2)), i)
* (∏ i in filter nat.prime (range (m + 2)), i) :
begin
apply finset.prod_union,
have disj : disjoint (finset.Ico (m + 2) (2 * m + 2)) (range (m + 2)),
{ simp only [finset.disjoint_left, and_imp, finset.mem_Ico, not_lt,
finset.mem_range],
intros _ pr _, exact pr, },
exact finset.disjoint_filter_filter disj,
end
... ≤ (∏ i in filter nat.prime (finset.Ico (m + 2) (2 * m + 2)), i) * 4 ^ (m + 1) :
nat.mul_le_mul_left _ (primorial_le_4_pow (m + 1))
... ≤ (choose (2 * m + 1) (m + 1)) * 4 ^ (m + 1) :
begin
have s : ∏ i in filter nat.prime (finset.Ico (m + 2) (2 * m + 2)),
i ∣ choose (2 * m + 1) (m + 1),
{ refine prod_primes_dvd (choose (2 * m + 1) (m + 1)) _ _,
{ intros a, rw [finset.mem_filter, nat.prime_iff], apply and.right, },
{ intros a, rw finset.mem_filter,
intros pr,
rcases pr with ⟨ size, is_prime ⟩,
simp only [finset.mem_Ico] at size,
rcases size with ⟨ a_big , a_small ⟩,
exact dvd_choose_of_middling_prime a is_prime m a_big
(nat.lt_succ_iff.mp a_small), }, },
have r : ∏ i in filter nat.prime (finset.Ico (m + 2) (2 * m + 2)),
i ≤ choose (2 * m + 1) (m + 1),
{ refine @nat.le_of_dvd _ _ _ s,
exact @choose_pos (2 * m + 1) (m + 1) (by linarith), },
exact nat.mul_le_mul_right _ r,
end
... = (choose (2 * m + 1) m) * 4 ^ (m + 1) : by rw choose_symm_half m
... ≤ 4 ^ m * 4 ^ (m + 1) : nat.mul_le_mul_right _ (choose_middle_le_pow m)
... = 4 ^ (2 * m + 1) : by ring_exp
... = 4 ^ (n + 2) : by rw [two_mul, ←twice_m],
end
end
| or.inr n_even :=
begin
obtain one_lt_n | n_le_one : 1 < n + 1 ∨ n + 1 ≤ 1 := lt_or_le 1 (n + 1),
{ rw primorial_succ one_lt_n n_even,
calc (n + 1)#
≤ 4 ^ n.succ : primorial_le_4_pow (n + 1)
... ≤ 4 ^ (n + 2) : pow_le_pow (by norm_num) (nat.le_succ _), },
{ have n_zero : n = 0 := eq_bot_iff.2 (succ_le_succ_iff.1 n_le_one),
norm_num [n_zero, primorial, range_succ, prod_filter, nat.not_prime_zero, nat.prime_two] },
end
end
|
54687af4fac8dbc5ebc262fb42cb53900391e7f9 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/unfoldMany.lean | d821ecd4d461e77b516bc9db340e5608b5b37a08 | [
"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 | 106 | lean | def f (x : Nat) := x + 1
def g (x : Nat) := f x + f x
example : g x > 0 := by
unfold g f
simp_arith
|
d06b4130ae93ced44ccd74a5597c8aff866b77e6 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/data/monoid_algebra.lean | d50eba7253674623c4ec9f7ac7ec8d18fb1d2cab | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 27,174 | 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 G. Kudryashov, Scott Morrison
-/
import ring_theory.algebra
/-!
# Monoid algebras
When the domain of a `finsupp` has a multiplicative or additive structure, we can define
a convolution product. To mathematicians this structure is known as the "monoid algebra",
i.e. the finite formal linear combinations over a given semiring of elements of the monoid.
The "group ring" ℤ[G] or the "group algebra" k[G] are typical uses.
In this file we define `monoid_algebra k G := G →₀ k`, and `add_monoid_algebra k G`
in the same way, and then define the convolution product on these.
When the domain is additive, this is used to define polynomials:
```
polynomial α := add_monoid_algebra ℕ α
mv_polynominal σ α := add_monoid_algebra (σ →₀ ℕ) α
```
When the domain is multiplicative, e.g. a group, this will be used to define the group ring.
## Implementation note
Unfortunately because additive and multiplicative structures both appear in both cases,
it doesn't appear to be possible to make much use of `to_additive`, and we just settle for
saying everything twice.
Similarly, I attempted to just define `add_monoid_algebra k G := monoid_algebra k (multiplicative G)`,
but the definitional equality `multiplicative G = G` leaks through everywhere, and
seems impossible to use.
-/
noncomputable theory
open_locale classical big_operators
open finset finsupp
universes u₁ u₂ u₃
variables (k : Type u₁) (G : Type u₂)
section
variables [semiring k]
/--
The monoid algebra over a semiring `k` generated by the monoid `G`.
It is the type of finite formal `k`-linear combinations of terms of `G`,
endowed with the convolution product.
-/
@[derive [inhabited, add_comm_monoid]]
def monoid_algebra : Type (max u₁ u₂) := G →₀ k
end
namespace monoid_algebra
variables {k G}
local attribute [reducible] monoid_algebra
section
variables [semiring k] [monoid G]
/-- The product of `f g : monoid_algebra k G` is the finitely supported function
whose value at `a` is the sum of `f x * g y` over all pairs `x, y`
such that `x * y = a`. (Think of the group ring of a group.) -/
instance : has_mul (monoid_algebra k G) :=
⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)⟩
lemma mul_def {f g : monoid_algebra k G} :
f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)) :=
rfl
lemma mul_apply (f g : monoid_algebra k G) (x : G) :
(f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ * a₂ = x then b₁ * b₂ else 0) :=
begin
rw [mul_def],
simp only [finsupp.sum_apply, single_apply],
end
lemma mul_apply_antidiagonal (f g : monoid_algebra k G) (x : G) (s : finset (G × G))
(hs : ∀ {p : G × G}, p ∈ s ↔ p.1 * p.2 = x) :
(f * g) x = ∑ p in s, (f p.1 * g p.2) :=
let F : G × G → k := λ p, if p.1 * p.2 = x then f p.1 * g p.2 else 0 in
calc (f * g) x = (∑ a₁ in f.support, ∑ a₂ in g.support, F (a₁, a₂)) :
mul_apply f g x
... = ∑ p in f.support.product g.support, F p : finset.sum_product.symm
... = ∑ p in (f.support.product g.support).filter (λ p : G × G, p.1 * p.2 = x), f p.1 * g p.2 :
(finset.sum_filter _ _).symm
... = ∑ p in s.filter (λ p : G × G, p.1 ∈ f.support ∧ p.2 ∈ g.support), f p.1 * g p.2 :
sum_congr (by { ext, simp [hs, and_comm] }) (λ _ _, rfl)
... = ∑ p in s, f p.1 * g p.2 : sum_subset (filter_subset _) $ λ p hps hp,
begin
simp only [mem_filter, mem_support_iff, not_and, not_not] at hp ⊢,
by_cases h1 : f p.1 = 0,
{ rw [h1, zero_mul] },
{ rw [hp hps h1, mul_zero] }
end
end
section
variables [semiring k] [monoid G]
lemma support_mul (a b : monoid_algebra k G) :
(a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ * a₂}) :=
subset.trans support_sum $ bind_mono $ assume a₁ _,
subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset
/-- The unit of the multiplication is `single 1 1`, i.e. the function
that is `1` at `1` and zero elsewhere. -/
instance : has_one (monoid_algebra k G) :=
⟨single 1 1⟩
lemma one_def : (1 : monoid_algebra k G) = single 1 1 :=
rfl
-- TODO: the simplifier unfolds 0 in the instance proof!
protected lemma zero_mul (f : monoid_algebra k G) : 0 * f = 0 :=
by simp only [mul_def, sum_zero_index]
protected lemma mul_zero (f : monoid_algebra k G) : f * 0 = 0 :=
by simp only [mul_def, sum_zero_index, sum_zero]
private lemma left_distrib (a b c : monoid_algebra k G) : a * (b + c) = a * b + a * c :=
by simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add,
eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add]
private lemma right_distrib (a b c : monoid_algebra k G) : (a + b) * c = a * c + b * c :=
by simp only [mul_def, sum_add_index, add_mul, mul_zero, zero_mul, single_zero,
single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add]
instance : semiring (monoid_algebra k G) :=
{ one := 1,
mul := (*),
one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul,
single_zero, sum_zero, zero_add, one_mul, sum_single],
mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero,
single_zero, sum_zero, add_zero, mul_one, sum_single],
zero_mul := monoid_algebra.zero_mul,
mul_zero := monoid_algebra.mul_zero,
mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index,
sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff,
add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add],
left_distrib := left_distrib,
right_distrib := right_distrib,
.. finsupp.add_comm_monoid }
@[simp] lemma single_mul_single {a₁ a₂ : G} {b₁ b₂ : k} :
(single a₁ b₁ : monoid_algebra k G) * single a₂ b₂ = single (a₁ * a₂) (b₁ * b₂) :=
(sum_single_index (by simp only [zero_mul, single_zero, sum_zero])).trans
(sum_single_index (by rw [mul_zero, single_zero]))
@[simp] lemma single_pow {a : G} {b : k} :
∀ n : ℕ, (single a b : monoid_algebra k G)^n = single (a^n) (b ^ n)
| 0 := rfl
| (n+1) := by simp only [pow_succ, single_pow n, single_mul_single]
section
variables (k G)
/-- Embedding of a monoid into its monoid algebra. -/
def of : G →* monoid_algebra k G :=
{ to_fun := λ a, single a 1,
map_one' := rfl,
map_mul' := λ a b, by rw [single_mul_single, one_mul] }
end
@[simp] lemma of_apply (a : G) : of k G a = single a 1 := rfl
lemma mul_single_apply_aux (f : monoid_algebra k G) {r : k}
{x y z : G} (H : ∀ a, a * x = z ↔ a = y) :
(f * single x r) z = f y * r :=
have A : ∀ a₁ b₁, (single x r).sum (λ a₂ b₂, ite (a₁ * a₂ = z) (b₁ * b₂) 0) =
ite (a₁ * x = z) (b₁ * r) 0,
from λ a₁ b₁, sum_single_index $ by simp,
calc (f * single x r) z = sum f (λ a b, if (a = y) then (b * r) else 0) :
-- different `decidable` instances make it not trivial
by { simp only [mul_apply, A, H], congr, funext, split_ifs; refl }
... = if y ∈ f.support then f y * r else 0 : f.support.sum_ite_eq' _ _
... = f y * r : by split_ifs with h; simp at h; simp [h]
lemma mul_single_one_apply (f : monoid_algebra k G) (r : k) (x : G) :
(f * single 1 r) x = f x * r :=
f.mul_single_apply_aux $ λ a, by rw [mul_one]
lemma single_mul_apply_aux (f : monoid_algebra k G) {r : k} {x y z : G}
(H : ∀ a, x * a = y ↔ a = z) :
(single x r * f) y = r * f z :=
have f.sum (λ a b, ite (x * a = y) (0 * b) 0) = 0, by simp,
calc (single x r * f) y = sum f (λ a b, ite (x * a = y) (r * b) 0) :
(mul_apply _ _ _).trans $ sum_single_index this
... = f.sum (λ a b, ite (a = z) (r * b) 0) :
by { simp only [H], congr' with g s, split_ifs; refl }
... = if z ∈ f.support then (r * f z) else 0 : f.support.sum_ite_eq' _ _
... = _ : by split_ifs with h; simp at h; simp [h]
lemma single_one_mul_apply (f : monoid_algebra k G) (r : k) (x : G) :
(single 1 r * f) x = r * f x :=
f.single_mul_apply_aux $ λ a, by rw [one_mul]
end
instance [comm_semiring k] [comm_monoid G] : comm_semiring (monoid_algebra k G) :=
{ mul_comm := assume f g,
begin
simp only [mul_def, finsupp.sum, mul_comm],
rw [finset.sum_comm],
simp only [mul_comm]
end,
.. monoid_algebra.semiring }
instance [ring k] : has_neg (monoid_algebra k G) :=
by apply_instance
instance [ring k] [monoid G] : ring (monoid_algebra k G) :=
{ neg := has_neg.neg,
add_left_neg := add_left_neg,
.. monoid_algebra.semiring }
instance [comm_ring k] [comm_monoid G] : comm_ring (monoid_algebra k G) :=
{ mul_comm := mul_comm, .. monoid_algebra.ring}
instance [semiring k] : has_scalar k (monoid_algebra k G) :=
finsupp.has_scalar
instance [semiring k] : semimodule k (monoid_algebra k G) :=
finsupp.semimodule G k
lemma single_one_comm [comm_semiring k] [monoid G] (r : k) (f : monoid_algebra k G) :
single 1 r * f = f * single 1 r :=
by { ext, rw [single_one_mul_apply, mul_single_one_apply, mul_comm] }
/--
As a preliminary to defining the `k`-algebra structure on `monoid_algebra k G`,
we define the underlying ring homomorphism.
In fact, we do this in more generality, providing the ring homomorphism
`k →+* monoid_algebra A G` given any ring homomorphism `k →+* A`.
-/
def algebra_map' {A : Type*} [semiring k] [semiring A] (f : k →+* A) [monoid G] :
k →+* monoid_algebra A G :=
{ to_fun := λ x, single 1 (f x),
map_one' := by { simp, refl },
map_mul' := λ x y, by rw [single_mul_single, one_mul, f.map_mul],
map_zero' := by rw [f.map_zero, single_zero],
map_add' := λ x y, by rw [f.map_add, single_add], }
/--
The instance `algebra k (monoid_algebra A G)` whenever we have `algebra k A`.
In particular this provides the instance `algebra k (monoid_algebra k G)`.
-/
instance {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] :
algebra k (monoid_algebra A G) :=
{ smul_def' := λ r a, by { ext x, dsimp [algebra_map'], rw single_one_mul_apply, rw algebra.smul_def'', },
commutes' := λ r f, show single 1 (algebra_map k A r) * f = f * single 1 (algebra_map k A r),
by { ext, rw [single_one_mul_apply, mul_single_one_apply, algebra.commutes], },
..algebra_map' (algebra_map k A) }
@[simp] lemma coe_algebra_map [comm_semiring k] [monoid G] :
(algebra_map k (monoid_algebra k G) : k → monoid_algebra k G) = single 1 :=
rfl
lemma single_eq_algebra_map_mul_of [comm_semiring k] [monoid G] (a : G) (b : k) :
single a b = (algebra_map k (monoid_algebra k G) : k → monoid_algebra k G) b * of k G a :=
by simp
instance [group G] [semiring k] :
distrib_mul_action G (monoid_algebra k G) :=
finsupp.comap_distrib_mul_action_self
section lift
variables (k G) [comm_semiring k] [monoid G] (R : Type u₃) [semiring R] [algebra k R]
/-- Any monoid homomorphism `G →* R` can be lifted to an algebra homomorphism
`monoid_algebra k G →ₐ[k] R`. -/
def lift : (G →* R) ≃ (monoid_algebra k G →ₐ[k] R) :=
{ inv_fun := λ f, (f : monoid_algebra k G →* R).comp (of k G),
to_fun := λ F, { to_fun := λ f, f.sum (λ a b, b • F a),
map_one' := by { rw [one_def, sum_single_index, one_smul, F.map_one], apply zero_smul },
map_mul' :=
begin
intros f g,
rw [mul_def, finsupp.sum_mul, finsupp.sum_sum_index];
try { intros, simp only [zero_smul, add_smul], done },
refine finset.sum_congr rfl (λ a ha, _), simp only,
rw [finsupp.mul_sum, finsupp.sum_sum_index];
try { intros, simp only [zero_smul, add_smul], done },
refine finset.sum_congr rfl (λ a' ha', _), simp only,
rw [sum_single_index, F.map_mul, algebra.mul_smul_comm, algebra.smul_mul_assoc,
smul_smul, mul_comm],
apply zero_smul
end,
map_zero' := sum_zero_index,
map_add' := λ f g, by rw [sum_add_index]; intros; simp only [zero_smul, add_smul],
commutes' := λ r, by rw [coe_algebra_map, sum_single_index, F.map_one, algebra.smul_def,
mul_one]; apply zero_smul },
left_inv := λ f, begin ext x, simp [sum_single_index] end,
right_inv := λ F,
begin
ext f,
conv_rhs { rw ← f.sum_single },
simp [← F.map_smul, finsupp.sum, ← F.map_sum]
end }
variables {k G R}
lemma lift_apply (F : G →* R) (f : monoid_algebra k G) :
lift k G R F f = f.sum (λ a b, b • F a) := rfl
@[simp] lemma lift_symm_apply (F : monoid_algebra k G →ₐ[k] R) (x : G) :
(lift k G R).symm F x = F (single x 1) := rfl
lemma lift_of (F : G →* R) (x) :
lift k G R F (of k G x) = F x :=
by rw [of_apply, ← lift_symm_apply, equiv.symm_apply_apply]
@[simp] lemma lift_single (F : G →* R) (a b) :
lift k G R F (single a b) = b • F a :=
by rw [single_eq_algebra_map_mul_of, ← algebra.smul_def, alg_hom.map_smul, lift_of]
lemma lift_unique' (F : monoid_algebra k G →ₐ[k] R) :
F = lift k G R ((F : monoid_algebra k G →* R).comp (of k G)) :=
((lift k G R).apply_symm_apply F).symm
/-- Decomposition of a `k`-algebra homomorphism from `monoid_algebra k G` by
its values on `F (single a 1)`. -/
lemma lift_unique (F : monoid_algebra k G →ₐ[k] R) (f : monoid_algebra k G) :
F f = f.sum (λ a b, b • F (single a 1)) :=
by conv_lhs { rw lift_unique' F, simp [lift_apply] }
/-- A `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its
values on the functions `single a 1`. -/
lemma alg_hom_ext ⦃φ₁ φ₂ : monoid_algebra k G →ₐ[k] R⦄
(h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ :=
(lift k G R).symm.injective $ monoid_hom.ext h
end lift
section
variables (k)
/-- When `V` is a `k[G]`-module, multiplication by a group element `g` is a `k`-linear map. -/
def group_smul.linear_map [group G] [comm_ring k]
(V : Type u₃) [add_comm_group V] [module (monoid_algebra k G) V] (g : G) :
(semimodule.restrict_scalars k (monoid_algebra k G) V) →ₗ[k]
(semimodule.restrict_scalars k (monoid_algebra k G) V) :=
{ to_fun := λ v, (single g (1 : k) • v : V),
map_add' := λ x y, smul_add (single g (1 : k)) x y,
map_smul' := λ c x,
by simp only [semimodule.restrict_scalars_smul_def, coe_algebra_map, ←mul_smul, single_one_comm], }.
@[simp]
lemma group_smul.linear_map_apply [group G] [comm_ring k]
(V : Type u₃) [add_comm_group V] [module (monoid_algebra k G) V] (g : G) (v : V) :
(group_smul.linear_map k V g) v = (single g (1 : k) • v : V) :=
rfl
section
variables {k}
variables [group G] [comm_ring k]
{V : Type u₃} {gV : add_comm_group V} {mV : module (monoid_algebra k G) V}
{W : Type u₃} {gW : add_comm_group W} {mW : module (monoid_algebra k G) W}
(f : (semimodule.restrict_scalars k (monoid_algebra k G) V) →ₗ[k]
(semimodule.restrict_scalars k (monoid_algebra k G) W))
(h : ∀ (g : G) (v : V), f (single g (1 : k) • v : V) = (single g (1 : k) • (f v) : W))
include h
/-- Build a `k[G]`-linear map from a `k`-linear map and evidence that it is `G`-equivariant. -/
def equivariant_of_linear_of_comm : V →ₗ[monoid_algebra k G] W :=
{ to_fun := f,
map_add' := λ v v', by simp,
map_smul' := λ c v,
begin
apply finsupp.induction c,
{ simp, },
{ intros g r c' nm nz w,
rw [add_smul, linear_map.map_add, w, add_smul, add_left_inj,
single_eq_algebra_map_mul_of, ←smul_smul, ←smul_smul],
erw [f.map_smul, h g v],
refl, }
end, }
@[simp]
lemma equivariant_of_linear_of_comm_apply (v : V) : (equivariant_of_linear_of_comm f h) v = f v :=
rfl
end
end
universe ui
variable {ι : Type ui}
lemma prod_single [comm_semiring k] [comm_monoid G]
{s : finset ι} {a : ι → G} {b : ι → k} :
(∏ i in s, single (a i) (b i)) = single (∏ i in s, a i) (∏ i in s, b i) :=
finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih,
single_mul_single, prod_insert has, prod_insert has]
section -- We now prove some additional statements that hold for group algebras.
variables [semiring k] [group G]
@[simp]
lemma mul_single_apply (f : monoid_algebra k G) (r : k) (x y : G) :
(f * single x r) y = f (y * x⁻¹) * r :=
f.mul_single_apply_aux $ λ a, eq_mul_inv_iff_mul_eq.symm
@[simp]
lemma single_mul_apply (r : k) (x : G) (f : monoid_algebra k G) (y : G) :
(single x r * f) y = r * f (x⁻¹ * y) :=
f.single_mul_apply_aux $ λ z, eq_inv_mul_iff_mul_eq.symm
lemma mul_apply_left (f g : monoid_algebra k G) (x : G) :
(f * g) x = (f.sum $ λ a b, b * (g (a⁻¹ * x))) :=
calc (f * g) x = sum f (λ a b, (single a b * g) x) :
by rw [← finsupp.sum_apply, ← finsupp.sum_mul, f.sum_single]
... = _ : by simp only [single_mul_apply, finsupp.sum]
-- If we'd assumed `comm_semiring`, we could deduce this from `mul_apply_left`.
lemma mul_apply_right (f g : monoid_algebra k G) (x : G) :
(f * g) x = (g.sum $ λa b, (f (x * a⁻¹)) * b) :=
calc (f * g) x = sum g (λ a b, (f * single a b) x) :
by rw [← finsupp.sum_apply, ← finsupp.mul_sum, g.sum_single]
... = _ : by simp only [mul_single_apply, finsupp.sum]
end
end monoid_algebra
section
variables [semiring k]
/--
The monoid algebra over a semiring `k` generated by the additive monoid `G`.
It is the type of finite formal `k`-linear combinations of terms of `G`,
endowed with the convolution product.
-/
@[derive [inhabited, add_comm_monoid]]
def add_monoid_algebra := G →₀ k
end
namespace add_monoid_algebra
variables {k G}
local attribute [reducible] add_monoid_algebra
section
variables [semiring k] [add_monoid G]
/-- The product of `f g : add_monoid_algebra k G` is the finitely supported function
whose value at `a` is the sum of `f x * g y` over all pairs `x, y`
such that `x + y = a`. (Think of the product of multivariate
polynomials where `α` is the additive monoid of monomial exponents.) -/
instance : has_mul (add_monoid_algebra k G) :=
⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩
lemma mul_def {f g : add_monoid_algebra k G} :
f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) :=
rfl
lemma mul_apply (f g : add_monoid_algebra k G) (x : G) :
(f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ + a₂ = x then b₁ * b₂ else 0) :=
begin
rw [mul_def],
simp only [finsupp.sum_apply, single_apply],
end
lemma support_mul (a b : add_monoid_algebra k G) :
(a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ + a₂}) :=
subset.trans support_sum $ bind_mono $ assume a₁ _,
subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset
/-- The unit of the multiplication is `single 1 1`, i.e. the function
that is `1` at `0` and zero elsewhere. -/
instance : has_one (add_monoid_algebra k G) :=
⟨single 0 1⟩
lemma one_def : (1 : add_monoid_algebra k G) = single 0 1 :=
rfl
-- TODO: the simplifier unfolds 0 in the instance proof!
protected lemma zero_mul (f : add_monoid_algebra k G) : 0 * f = 0 :=
by simp only [mul_def, sum_zero_index]
protected lemma mul_zero (f : add_monoid_algebra k G) : f * 0 = 0 :=
by simp only [mul_def, sum_zero_index, sum_zero]
private lemma left_distrib (a b c : add_monoid_algebra k G) : a * (b + c) = a * b + a * c :=
by simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add,
eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add]
private lemma right_distrib (a b c : add_monoid_algebra k G) : (a + b) * c = a * c + b * c :=
by simp only [mul_def, sum_add_index, add_mul, mul_zero, zero_mul, single_zero,
single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add]
instance : semiring (add_monoid_algebra k G) :=
{ one := 1,
mul := (*),
one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul,
single_zero, sum_zero, zero_add, one_mul, sum_single],
mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero,
single_zero, sum_zero, add_zero, mul_one, sum_single],
zero_mul := add_monoid_algebra.zero_mul,
mul_zero := add_monoid_algebra.mul_zero,
mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index,
sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff,
add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add],
left_distrib := left_distrib,
right_distrib := right_distrib,
.. finsupp.add_comm_monoid }
lemma single_mul_single {a₁ a₂ : G} {b₁ b₂ : k} :
(single a₁ b₁ : add_monoid_algebra k G) * single a₂ b₂ = single (a₁ + a₂) (b₁ * b₂) :=
(sum_single_index (by simp only [zero_mul, single_zero, sum_zero])).trans
(sum_single_index (by rw [mul_zero, single_zero]))
section
variables (k G)
/-- Embedding of a monoid into its monoid algebra. -/
def of : multiplicative G →* add_monoid_algebra k G :=
{ to_fun := λ a, single a 1,
map_one' := rfl,
map_mul' := λ a b, by { rw [single_mul_single, one_mul], refl } }
end
@[simp] lemma of_apply (a : G) : of k G a = single a 1 := rfl
lemma mul_single_apply_aux (f : add_monoid_algebra k G) (r : k)
(x y z : G) (H : ∀ a, a + x = z ↔ a = y) :
(f * single x r) z = f y * r :=
have A : ∀ a₁ b₁, (single x r).sum (λ a₂ b₂, ite (a₁ + a₂ = z) (b₁ * b₂) 0) =
ite (a₁ + x = z) (b₁ * r) 0,
from λ a₁ b₁, sum_single_index $ by simp,
calc (f * single x r) z = sum f (λ a b, if (a = y) then (b * r) else 0) :
-- different `decidable` instances make it not trivial
by { simp only [mul_apply, A, H], congr, funext, split_ifs; refl }
... = if y ∈ f.support then f y * r else 0 : f.support.sum_ite_eq' _ _
... = f y * r : by split_ifs with h; simp at h; simp [h]
lemma mul_single_zero_apply (f : add_monoid_algebra k G) (r : k) (x : G) :
(f * single 0 r) x = f x * r :=
f.mul_single_apply_aux r _ _ _ $ λ a, by rw [add_zero]
lemma single_mul_apply_aux (f : add_monoid_algebra k G) (r : k) (x y z : G)
(H : ∀ a, x + a = y ↔ a = z) :
(single x r * f) y = r * f z :=
have f.sum (λ a b, ite (x + a = y) (0 * b) 0) = 0, by simp,
calc (single x r * f) y = sum f (λ a b, ite (x + a = y) (r * b) 0) :
(mul_apply _ _ _).trans $ sum_single_index this
... = f.sum (λ a b, ite (a = z) (r * b) 0) :
by { simp only [H], congr' with g s, split_ifs; refl }
... = if z ∈ f.support then (r * f z) else 0 : f.support.sum_ite_eq' _ _
... = _ : by split_ifs with h; simp at h; simp [h]
lemma single_zero_mul_apply (f : add_monoid_algebra k G) (r : k) (x : G) :
(single 0 r * f) x = r * f x :=
f.single_mul_apply_aux r _ _ _ $ λ a, by rw [zero_add]
end
instance [comm_semiring k] [add_comm_monoid G] : comm_semiring (add_monoid_algebra k G) :=
{ mul_comm := assume f g,
begin
simp only [mul_def, finsupp.sum, mul_comm],
rw [finset.sum_comm],
simp only [add_comm]
end,
.. add_monoid_algebra.semiring }
instance [ring k] : has_neg (add_monoid_algebra k G) :=
by apply_instance
instance [ring k] [add_monoid G] : ring (add_monoid_algebra k G) :=
{ neg := has_neg.neg,
add_left_neg := add_left_neg,
.. add_monoid_algebra.semiring }
instance [comm_ring k] [add_comm_monoid G] : comm_ring (add_monoid_algebra k G) :=
{ mul_comm := mul_comm, .. add_monoid_algebra.ring}
instance [semiring k] : has_scalar k (add_monoid_algebra k G) :=
finsupp.has_scalar
instance [semiring k] : semimodule k (add_monoid_algebra k G) :=
finsupp.semimodule G k
/--
As a preliminary to defining the `k`-algebra structure on `add_monoid_algebra k G`,
we define the underlying ring homomorphism.
In fact, we do this in more generality, providing the ring homomorphism
`k →+* add_monoid_algebra A G` given any ring homomorphism `k →+* A`.
-/
def algebra_map' {A : Type*} [semiring k] [semiring A] (f : k →+* A) [add_monoid G] :
k →+* add_monoid_algebra A G :=
{ to_fun := λ x, single 0 (f x),
map_one' := by { simp, refl },
map_mul' := λ x y, by rw [single_mul_single, zero_add, f.map_mul],
map_zero' := by rw [f.map_zero, single_zero],
map_add' := λ x y, by rw [f.map_add, single_add], }
/--
The instance `algebra k (add_monoid_algebra A G)` whenever we have `algebra k A`.
In particular this provides the instance `algebra k (add_monoid_algebra k G)`.
-/
instance {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [add_monoid G] :
algebra k (add_monoid_algebra A G) :=
{ smul_def' := λ r a, by { ext x, dsimp [algebra_map'], rw single_zero_mul_apply, rw algebra.smul_def'', },
commutes' := λ r f, show single 0 (algebra_map k A r) * f = f * single 0 (algebra_map k A r),
by { ext, rw [single_zero_mul_apply, mul_single_zero_apply, algebra.commutes], },
..algebra_map' (algebra_map k A) }
@[simp] lemma coe_algebra_map [comm_semiring k] [add_monoid G] :
(algebra_map k (add_monoid_algebra k G) : k → add_monoid_algebra k G) = single 0 :=
rfl
/-- Any monoid homomorphism `multiplicative G →* R` can be lifted to an algebra homomorphism
`add_monoid_algebra k G →ₐ[k] R`. -/
def lift [comm_semiring k] [add_monoid G] {R : Type u₃} [semiring R] [algebra k R] :
(multiplicative G →* R) ≃ (add_monoid_algebra k G →ₐ[k] R) :=
{ inv_fun := λ f, ((f : add_monoid_algebra k G →+* R) : add_monoid_algebra k G →* R).comp (of k G),
to_fun := λ F, { to_fun := λ f, f.sum (λ a b, b • F a),
map_one' := by { rw [one_def, sum_single_index, one_smul], erw [F.map_one], apply zero_smul },
map_mul' :=
begin
intros f g,
rw [mul_def, finsupp.sum_mul, finsupp.sum_sum_index];
try { intros, simp only [zero_smul, add_smul], done },
refine finset.sum_congr rfl (λ a ha, _), simp only,
rw [finsupp.mul_sum, finsupp.sum_sum_index];
try { intros, simp only [zero_smul, add_smul], done },
refine finset.sum_congr rfl (λ a' ha', _), simp only,
rw [sum_single_index],
erw [F.map_mul],
rw [algebra.mul_smul_comm, algebra.smul_mul_assoc, smul_smul, mul_comm],
apply zero_smul
end,
map_zero' := sum_zero_index,
map_add' := λ f g, by rw [sum_add_index]; intros; simp only [zero_smul, add_smul],
commutes' := λ r,
begin
rw [coe_algebra_map, sum_single_index],
erw [F.map_one],
rw [algebra.smul_def, mul_one],
apply zero_smul
end, },
left_inv := λ f, begin ext x, simp [sum_single_index] end,
right_inv := λ F,
begin
ext f,
conv_rhs { rw ← f.sum_single },
simp [← F.map_smul, finsupp.sum, ← F.map_sum]
end }
-- It is hard to state the equivalent of `distrib_mul_action G (monoid_algebra k G)`
-- because we've never discussed actions of additive groups.
universe ui
variable {ι : Type ui}
lemma prod_single [comm_semiring k] [add_comm_monoid G]
{s : finset ι} {a : ι → G} {b : ι → k} :
(∏ i in s, single (a i) (b i)) = single (∑ i in s, a i) (∏ i in s, b i) :=
finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih,
single_mul_single, sum_insert has, prod_insert has]
end add_monoid_algebra
|
d95100ae2aba635c61e0c3858109af84581c4b37 | 958488bc7f3c2044206e0358e56d7690b6ae696c | /lean/typeclass.lean | 31a9cbcceaf9e6667754c46d0f60a451c62ec93c | [] | no_license | possientis/Prog | a08eec1c1b121c2fd6c70a8ae89e2fbef952adb4 | d4b3debc37610a88e0dac3ac5914903604fd1d1f | refs/heads/master | 1,692,263,717,723 | 1,691,757,179,000 | 1,691,757,179,000 | 40,361,602 | 3 | 0 | null | 1,679,896,438,000 | 1,438,953,859,000 | Coq | UTF-8 | Lean | false | false | 2,296 | lean | namespace hidden
class inhabited (α : Type _) :=
(default : α)
-- syntactic sugar for
@[class] structure inhabited2 (α : Type _) :=
(default : α)
instance Prop_inhabited : inhabited Prop :=
inhabited.mk true
instance Prop_inhabited2 : inhabited Prop :=
{ default := true}
instance Prop_inhabited3 : inhabited Prop :=
⟨true⟩
instance bool_inhabited : inhabited bool :=
{ default := tt}
instance nat_inhabited : inhabited ℕ :=
{ default := 0}
instance unit_inhabited : inhabited unit :=
{ default := ()}
def default (α : Type _) [s : inhabited α] : α :=
@inhabited.default α s
def defaul2 (α : Type _) [s : inhabited α] : α :=
inhabited.default α
--#check default Prop
--#reduce default Prop
instance prod_inhabited {α : Type _} {β : Type _} [inhabited α] [inhabited β] : inhabited (α × β) :=
{ default := (default α, default β)}
--#reduce default (Prop × bool)
instance fun_inhabited (α : Type _) {β : Type _} [inhabited β] : inhabited (α → β) :=
{default := λ _, default β}
--#reduce default (ℕ → ℕ)
universe u
-- just a structure
class has_add (α : Type u) :=
mk :: (add : α → α → α)
def add {α : Type u} [has_add α] : α → α → α := has_add.add
notation x `+` y := add x y
instance add_nat : has_add ℕ :=
{add := nat.add}
instance add_prod {α β : Type u} [has_add α] [has_add β] : has_add (α × β) :=
{ add := λ ⟨x₁,x₂⟩ ⟨y₁,y₂⟩, (x₁ + y₁, x₂ + y₂)}
instance add_fun {α β : Type} [has_add β] : has_add (α → β) :=
{add := λ f g x, f x + g x}
--#reduce (λ (x:ℕ), 1) + (λ x, 2)
end hidden
--#print classes
--#print instances inhabited
def foo : has_add ℕ := by apply_instance
def bar : inhabited (ℕ → ℕ) := by apply_instance
def baz : has_add ℕ := infer_instance
def bla : inhabited (ℕ → ℕ) := infer_instance
--#print foo
--#reduce foo
--#print bar
--#reduce bar
--#reduce (by apply_instance : inhabited ℕ)
--#reduce (infer_instance : inhabited ℕ)
-- example {α : Type} : inhabited (set α) := by apply_instance
def inhabited.set (α : Type) : inhabited (set α) := ⟨∅⟩
-- #print inhabited.set
-- #reduce inhabited.set ℕ
set_option trace.class_instances true
set_option class.instance_max_depth 5
|
5b36c5fb91cf61fdd703b52ca7b146d6ce5bb42a | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/IO1.lean | b464ea4288828ca411769eedb47a84d5bf2585bb | [
"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 | 988 | lean | import system.io
open list
open io
-- set_option pp.all true
definition main : io unit :=
do l₁ ← get_line,
l₂ ← get_line,
put_str (l₂ ++ l₁)
-- vm_eval main
-- set_option trace.compiler.code_gen true
#eval put_str "hello\n"
#print "************************"
definition aux (n : nat) : io unit :=
do put_str "========\nvalue: ",
print n,
put_str "\n========\n"
#eval aux 20
#print "************************"
definition repeat : nat → (nat → io unit) → io unit
| 0 a := return ()
| (n+1) a := do a n, repeat n a
#eval repeat 10 aux
#print "************************"
definition execute : list (io unit) → io unit
| [] := return ()
| (x::xs) := do x, execute xs
#eval repeat 10 (λ i, execute [aux i, put_str "hello\n"])
#print "************************"
#eval
do n ← return 10,
put_str "value: ",
print n,
put_str "\n",
print (n+2),
put_str "\n----------\n"
#print "************************"
|
7152626c117e886cbf09998402b740fa671590c4 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /library/data/fintype/function.lean | 5484a627604fa00e4205e2be1be1bb6f7ef585a9 | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 18,724 | lean | /-
Copyright (c) 2015 Haitao Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author : Haitao Zhang
-/
import data
open nat function eq.ops
namespace list
-- this is in preparation for counting the number of finite functions
section list_of_lists
open prod
variable {A : Type}
definition cons_pair (pr : A × list A) := (pr1 pr) :: (pr2 pr)
definition cons_all_of (elts : list A) (ls : list (list A)) : list (list A) :=
map cons_pair (product elts ls)
lemma pair_of_cons {a} {l} {pr : A × list A} : cons_pair pr = a::l → pr = (a, l) :=
prod.destruct pr (λ p1 p2, assume Peq, list.no_confusion Peq (by intros; substvars))
lemma cons_pair_inj : injective (@cons_pair A) :=
take p1 p2, assume Pl,
prod.eq (list.no_confusion Pl (λ P1 P2, P1)) (list.no_confusion Pl (λ P1 P2, P2))
lemma nodup_of_cons_all {elts : list A} {ls : list (list A)}
: nodup elts → nodup ls → nodup (cons_all_of elts ls) :=
assume Pelts Pls,
nodup_map cons_pair_inj (nodup_product Pelts Pls)
lemma length_cons_all {elts : list A} {ls : list (list A)} :
length (cons_all_of elts ls) = length elts * length ls := calc
length (cons_all_of elts ls) = length (product elts ls) : length_map
... = length elts * length ls : length_product
variable [finA : fintype A]
include finA
definition all_lists_of_len : ∀ (n : nat), list (list A)
| 0 := [[]]
| (succ n) := cons_all_of (elements_of A) (all_lists_of_len n)
definition all_nodups_of_len [deceqA : decidable_eq A] (n : nat) : list (list A) :=
filter nodup (all_lists_of_len n)
lemma nodup_all_lists : ∀ {n : nat}, nodup (@all_lists_of_len A _ n)
| 0 := nodup_singleton []
| (succ n) := nodup_of_cons_all (fintype.unique A) nodup_all_lists
lemma nodup_all_nodups [deceqA : decidable_eq A] {n : nat} :
nodup (@all_nodups_of_len A _ _ n) :=
nodup_filter nodup nodup_all_lists
lemma mem_all_lists : ∀ {n : nat} {l : list A}, length l = n → l ∈ all_lists_of_len n
| 0 [] := assume P, mem_cons [] []
| 0 (a::l) := assume Peq, by contradiction
| (succ n) [] := assume Peq, by contradiction
| (succ n) (a::l) := assume Peq, begin
apply mem_map, apply mem_product,
exact fintype.complete a,
exact mem_all_lists (succ.inj Peq)
end
lemma mem_all_nodups [deceqA : decidable_eq A] (n : nat) (l : list A) :
length l = n → nodup l → l ∈ all_nodups_of_len n :=
assume Pl Pn, mem_filter_of_mem (mem_all_lists Pl) Pn
lemma nodup_mem_all_nodups [deceqA : decidable_eq A] {n : nat} ⦃l : list A⦄ :
l ∈ all_nodups_of_len n → nodup l :=
assume Pl, of_mem_filter Pl
lemma length_mem_all_lists : ∀ {n : nat} ⦃l : list A⦄,
l ∈ all_lists_of_len n → length l = n
| 0 [] := assume P, rfl
| 0 (a::l) := assume Pin, assert Peq : (a::l) = [], from mem_singleton Pin,
by contradiction
| (succ n) [] := assume Pin, obtain pr Pprin Ppr, from exists_of_mem_map Pin,
by contradiction
| (succ n) (a::l) := assume Pin, obtain pr Pprin Ppr, from exists_of_mem_map Pin,
assert Pl : l ∈ all_lists_of_len n,
from mem_of_mem_product_right ((pair_of_cons Ppr) ▸ Pprin),
by rewrite [length_cons, length_mem_all_lists Pl]
lemma length_mem_all_nodups [deceqA : decidable_eq A] {n : nat} ⦃l : list A⦄ :
l ∈ all_nodups_of_len n → length l = n :=
assume Pl, length_mem_all_lists (mem_of_mem_filter Pl)
open fintype
lemma length_all_lists : ∀ {n : nat}, length (@all_lists_of_len A _ n) = (card A) ^ n
| 0 := calc length [[]] = 1 : length_cons
| (succ n) := calc length _ = card A * length (all_lists_of_len n) : length_cons_all
... = card A * (card A ^ n) : length_all_lists
... = (card A ^ n) * card A : nat.mul.comm
... = (card A) ^ (succ n) : pow_succ'
end list_of_lists
section kth
variable {A : Type}
definition kth : ∀ k (l : list A), k < length l → A
| k [] := begin rewrite length_nil, intro Pltz, exact absurd Pltz !not_lt_zero end
| 0 (a::l) := λ P, a
| (k+1) (a::l):= by rewrite length_cons; intro Plt; exact kth k l (lt_of_succ_lt_succ Plt)
lemma kth_zero_of_cons {a} (l : list A) (P : 0 < length (a::l)) : kth 0 (a::l) P = a :=
rfl
lemma kth_succ_of_cons {a} k (l : list A) (P : k+1 < length (a::l)) :
kth (succ k) (a::l) P = kth k l (lt_of_succ_lt_succ P) :=
rfl
lemma kth_mem : ∀ {k : nat} {l : list A} P, kth k l P ∈ l
| k [] := assume P, absurd P !not_lt_zero
| 0 (a::l) := assume P, by rewrite kth_zero_of_cons; apply mem_cons
| (succ k) (a::l) := assume P, by
rewrite [kth_succ_of_cons]; apply mem_cons_of_mem a; apply kth_mem
-- Leo provided the following proof.
lemma eq_of_kth_eq [deceqA : decidable_eq A]
: ∀ {l1 l2 : list A} (Pleq : length l1 = length l2),
(∀ (k : nat) (Plt1 : k < length l1) (Plt2 : k < length l2), kth k l1 Plt1 = kth k l2 Plt2) → l1 = l2
| [] [] h₁ h₂ := rfl
| (a₁::l₁) [] h₁ h₂ := by contradiction
| [] (a₂::l₂) h₁ h₂ := by contradiction
| (a₁::l₁) (a₂::l₂) h₁ h₂ :=
have ih₁ : length l₁ = length l₂, by injection h₁; eassumption,
have ih₂ : ∀ (k : nat) (plt₁ : k < length l₁) (plt₂ : k < length l₂), kth k l₁ plt₁ = kth k l₂ plt₂,
begin
intro k plt₁ plt₂,
have splt₁ : succ k < length l₁ + 1, from succ_le_succ plt₁,
have splt₂ : succ k < length l₂ + 1, from succ_le_succ plt₂,
have keq : kth (succ k) (a₁::l₁) splt₁ = kth (succ k) (a₂::l₂) splt₂, from h₂ (succ k) splt₁ splt₂,
rewrite *kth_succ_of_cons at keq,
exact keq
end,
assert ih : l₁ = l₂, from eq_of_kth_eq ih₁ ih₂,
assert k₁ : a₁ = a₂,
begin
have lt₁ : 0 < length (a₁::l₁), from !zero_lt_succ,
have lt₂ : 0 < length (a₂::l₂), from !zero_lt_succ,
have e₁ : kth 0 (a₁::l₁) lt₁ = kth 0 (a₂::l₂) lt₂, from h₂ 0 lt₁ lt₂,
rewrite *kth_zero_of_cons at e₁,
assumption
end,
by subst l₁; subst a₁
lemma kth_of_map {B : Type} {f : A → B} :
∀ {k : nat} {l : list A} Plt Pmlt, kth k (map f l) Pmlt = f (kth k l Plt)
| k [] := assume P, absurd P !not_lt_zero
| 0 (a::l) := assume Plt, by
rewrite [map_cons]; intro Pmlt; rewrite [kth_zero_of_cons]
| (succ k) (a::l) := assume P, begin
rewrite [map_cons], intro Pmlt, rewrite [*kth_succ_of_cons],
apply kth_of_map
end
lemma kth_find [deceqA : decidable_eq A] :
∀ {l : list A} {a} P, kth (find a l) l P = a
| [] := take a, assume P, absurd P !not_lt_zero
| (x::l) := take a, begin
assert Pd : decidable (a = x), {apply deceqA},
cases Pd with Pe Pne,
rewrite [find_cons_of_eq l Pe], intro P, rewrite [kth_zero_of_cons, Pe],
rewrite [find_cons_of_ne l Pne], intro P, rewrite [kth_succ_of_cons],
apply kth_find
end
lemma find_kth [deceqA : decidable_eq A] :
∀ {k : nat} {l : list A} P, find (kth k l P) l < length l
| k [] := assume P, absurd P !not_lt_zero
| 0 (a::l) := assume P, begin
rewrite [kth_zero_of_cons, find_cons_of_eq l rfl, length_cons],
exact !zero_lt_succ
end
| (succ k) (a::l) := assume P, begin
rewrite [kth_succ_of_cons],
assert Pd : decidable ((kth k l (lt_of_succ_lt_succ P)) = a),
{apply deceqA},
cases Pd with Pe Pne,
rewrite [find_cons_of_eq l Pe], apply zero_lt_succ,
rewrite [find_cons_of_ne l Pne], apply succ_lt_succ, apply find_kth
end
lemma find_kth_of_nodup [deceqA : decidable_eq A] :
∀ {k : nat} {l : list A} P, nodup l → find (kth k l P) l = k
| k [] := assume P, absurd P !not_lt_zero
| 0 (a::l) := assume Plt Pnodup,
by rewrite [kth_zero_of_cons, find_cons_of_eq l rfl]
| (succ k) (a::l) := assume Plt Pnodup, begin
rewrite [kth_succ_of_cons],
assert Pd : decidable ((kth k l (lt_of_succ_lt_succ Plt)) = a),
{apply deceqA},
cases Pd with Pe Pne,
assert Pin : a ∈ l, {rewrite -Pe, apply kth_mem},
exact absurd Pin (not_mem_of_nodup_cons Pnodup),
rewrite [find_cons_of_ne l Pne], apply congr (eq.refl succ),
apply find_kth_of_nodup (lt_of_succ_lt_succ Plt) (nodup_of_nodup_cons Pnodup)
end
end kth
end list
namespace fintype
open list
section found
variables {A B : Type}
variable [finA : fintype A]
include finA
lemma find_in_range [deceqB : decidable_eq B] {f : A → B} (b : B) :
∀ (l : list A) P, f (kth (find b (map f l)) l P) = b
| [] := assume P, begin exact absurd P !not_lt_zero end
| (a::l) := decidable.rec_on (deceqB b (f a))
(assume Peq, begin
rewrite [map_cons f a l, find_cons_of_eq _ Peq],
intro P, rewrite [kth_zero_of_cons], exact (Peq⁻¹)
end)
(assume Pne, begin
rewrite [map_cons f a l, find_cons_of_ne _ Pne],
intro P,
rewrite [kth_succ_of_cons (find b (map f l)) l P],
exact find_in_range l (lt_of_succ_lt_succ P)
end)
end found
section list_to_fun
variables {A B : Type}
variable [finA : fintype A]
include finA
definition fun_to_list (f : A → B) : list B := map f (elems A)
lemma length_map_of_fintype (f : A → B) : length (map f (elems A)) = card A :=
by apply length_map
variable [deceqA : decidable_eq A]
include deceqA
lemma fintype_find (a : A) : find a (elems A) < card A :=
find_lt_length (complete a)
definition list_to_fun (l : list B) (leq : length l = card A) : A → B :=
take x,
kth _ _ (leq⁻¹ ▸ fintype_find x)
definition all_funs [finB : fintype B] : list (A → B) :=
dmap (λ l, length l = card A) list_to_fun (all_lists_of_len (card A))
lemma list_to_fun_apply (l : list B) (leq : length l = card A) (a : A) :
∀ P, list_to_fun l leq a = kth (find a (elems A)) l P :=
assume P, rfl
variable [deceqB : decidable_eq B]
include deceqB
lemma fun_eq_list_to_fun_map (f : A → B) : ∀ P, f = list_to_fun (map f (elems A)) P :=
assume Pleq, funext (take a,
assert Plt : _, from Pleq⁻¹ ▸ find_lt_length (complete a), begin
rewrite [list_to_fun_apply _ Pleq a (Pleq⁻¹ ▸ find_lt_length (complete a))],
assert Pmlt : find a (elems A) < length (map f (elems A)),
{rewrite length_map, exact Plt},
rewrite [@kth_of_map A B f (find a (elems A)) (elems A) Plt _, kth_find]
end)
lemma list_eq_map_list_to_fun (l : list B) (leq : length l = card A)
: l = map (list_to_fun l leq) (elems A) :=
begin
apply eq_of_kth_eq, rewrite length_map, apply leq,
intro k Plt Plt2,
assert Plt1 : k < length (elems A), {apply leq ▸ Plt},
assert Plt3 : find (kth k (elems A) Plt1) (elems A) < length l,
{rewrite leq, apply find_kth},
rewrite [kth_of_map Plt1 Plt2, list_to_fun_apply l leq _ Plt3],
congruence,
rewrite [find_kth_of_nodup Plt1 (unique A)]
end
lemma fun_to_list_to_fun (f : A → B) : ∀ P, list_to_fun (fun_to_list f) P = f :=
assume P, (fun_eq_list_to_fun_map f P)⁻¹
lemma list_to_fun_to_list (l : list B) (leq : length l = card A) :
fun_to_list (list_to_fun l leq) = l
:= (list_eq_map_list_to_fun l leq)⁻¹
lemma dinj_list_to_fun : dinj (λ (l : list B), length l = card A) list_to_fun :=
take l1 l2 Pl1 Pl2 Peq,
by rewrite [list_eq_map_list_to_fun l1 Pl1, list_eq_map_list_to_fun l2 Pl2, Peq]
variable [finB : fintype B]
include finB
lemma nodup_all_funs : nodup (@all_funs A B _ _ _) :=
dmap_nodup_of_dinj dinj_list_to_fun nodup_all_lists
lemma all_funs_complete (f : A → B) : f ∈ all_funs :=
assert Plin : map f (elems A) ∈ all_lists_of_len (card A),
from mem_all_lists (by rewrite length_map),
assert Plfin : list_to_fun (map f (elems A)) (length_map_of_fintype f) ∈ all_funs,
from mem_dmap _ Plin,
begin rewrite [fun_eq_list_to_fun_map f (length_map_of_fintype f)], apply Plfin end
lemma all_funs_to_all_lists :
map fun_to_list (@all_funs A B _ _ _) = all_lists_of_len (card A) :=
map_dmap_of_inv_of_pos list_to_fun_to_list length_mem_all_lists
lemma length_all_funs : length (@all_funs A B _ _ _) = (card B) ^ (card A) := calc
length _ = length (map fun_to_list all_funs) : length_map
... = length (all_lists_of_len (card A)) : all_funs_to_all_lists
... = (card B) ^ (card A) : length_all_lists
definition fun_is_fintype [instance] : fintype (A → B) :=
fintype.mk all_funs nodup_all_funs all_funs_complete
lemma card_funs : card (A → B) = (card B) ^ (card A) := length_all_funs
end list_to_fun
section surj_inv
variables {A B : Type}
variable [finA : fintype A]
include finA
-- surj from fintype domain implies fintype range
lemma mem_map_of_surj {f : A → B} (surj : surjective f) : ∀ b, b ∈ map f (elems A) :=
take b, obtain a Peq, from surj b,
Peq ▸ mem_map f (complete a)
variable [deceqB : decidable_eq B]
include deceqB
lemma found_of_surj {f : A → B} (surj : surjective f) :
∀ b, let elts := elems A, k := find b (map f elts) in k < length elts :=
λ b, let elts := elems A, img := map f elts, k := find b img in
have Pin : b ∈ img, from mem_map_of_surj surj b,
assert Pfound : k < length img, from find_lt_length (mem_map_of_surj surj b),
length_map f elts ▸ Pfound
definition right_inv {f : A → B} (surj : surjective f) : B → A :=
λ b, let elts := elems A, k := find b (map f elts) in
kth k elts (found_of_surj surj b)
lemma right_inv_of_surj {f : A → B} (surj : surjective f) : f ∘ (right_inv surj) = id :=
funext (λ b, find_in_range b (elems A) (found_of_surj surj b))
end surj_inv
-- inj functions for equal card types are also surj and therefore bij
-- the right inv (since it is surj) is also the left inv
section inj
open finset
variables {A B : Type}
variable [finA : fintype A]
include finA
variable [deceqA : decidable_eq A]
include deceqA
lemma inj_of_card_image_eq [deceqB : decidable_eq B] {f : A → B} :
finset.card (image f univ) = card A → injective f :=
assume Peq, by
rewrite [set.injective_iff_inj_on_univ, -to_set_univ];
apply inj_on_of_card_image_eq Peq
variable [deceqB : decidable_eq B]
include deceqB
lemma nodup_of_inj {f : A → B} : injective f → nodup (map f (elems A)) :=
assume Pinj, nodup_map Pinj (unique A)
lemma inj_of_nodup {f : A → B} :
nodup (map f (elems A)) → injective f :=
assume Pnodup, inj_of_card_image_eq (calc
finset.card (image f univ) = finset.card (to_finset (map f (elems A))) : rfl
... = finset.card (to_finset_of_nodup (map f (elems A)) Pnodup) : {(to_finset_eq_of_nodup Pnodup)⁻¹}
... = length (map f (elems A)) : rfl
... = length (elems A) : length_map
... = card A : rfl)
variable [finB : fintype B]
include finB
lemma surj_of_inj_eq_card : card A = card B → ∀ {f : A → B}, injective f → surjective f :=
assume Peqcard, take f, assume Pinj,
decidable.rec_on decidable_forall_finite
(assume P : surjective f, P)
(assume Pnsurj : ¬surjective f, obtain b Pne, from exists_not_of_not_forall Pnsurj,
assert Pall : ∀ a, f a ≠ b, from forall_not_of_not_exists Pne,
assert Pbnin : b ∉ image f univ, from λ Pin,
obtain a Pa, from exists_of_mem_image Pin, absurd (and.right Pa) (Pall a),
assert Puniv : finset.card (image f univ) = card A,
from card_eq_card_image_of_inj Pinj,
assert Punivb : finset.card (image f univ) = card B, from eq.trans Puniv Peqcard,
assert P : image f univ = univ, from univ_of_card_eq_univ Punivb,
absurd (P⁻¹▸ mem_univ b) Pbnin)
end inj
section perm
definition all_injs (A : Type) [finA : fintype A] [deceqA : decidable_eq A] : list (A → A) :=
dmap (λ l, length l = card A) list_to_fun (all_nodups_of_len (card A))
variable {A : Type}
variable [finA : fintype A]
include finA
variable [deceqA : decidable_eq A]
include deceqA
lemma nodup_all_injs : nodup (all_injs A) :=
dmap_nodup_of_dinj dinj_list_to_fun nodup_all_nodups
lemma all_injs_complete {f : A → A} : injective f → f ∈ (all_injs A) :=
assume Pinj,
assert Plin : map f (elems A) ∈ all_nodups_of_len (card A),
from begin apply mem_all_nodups, apply length_map, apply nodup_of_inj Pinj end,
assert Plfin : list_to_fun (map f (elems A)) (length_map_of_fintype f) ∈ !all_injs,
from mem_dmap _ Plin,
begin rewrite [fun_eq_list_to_fun_map f (length_map_of_fintype f)], apply Plfin end
open finset
lemma univ_of_leq_univ_of_nodup {l : list A} (n : nodup l) (leq : length l = card A) :
to_finset_of_nodup l n = univ :=
univ_of_card_eq_univ (calc
finset.card (to_finset_of_nodup l n) = length l : rfl
... = card A : leq)
lemma inj_of_mem_all_injs {f : A → A} : f ∈ (all_injs A) → injective f :=
assume Pfin, obtain l Pex, from exists_of_mem_dmap Pfin,
obtain leq Pin Peq, from Pex,
assert Pmap : map f (elems A) = l, from Peq⁻¹ ▸ list_to_fun_to_list l leq,
begin apply inj_of_nodup, rewrite Pmap, apply nodup_mem_all_nodups Pin end
lemma perm_of_inj {f : A → A} : injective f → perm (map f (elems A)) (elems A) :=
assume Pinj,
assert P1 : univ = to_finset_of_nodup (elems A) (unique A), from rfl,
assert P2 : to_finset_of_nodup (map f (elems A)) (nodup_of_inj Pinj) = univ,
from univ_of_leq_univ_of_nodup _ !length_map,
quot.exact (P1 ▸ P2)
end perm
end fintype
|
e78934478595ed507dec2408af33b0d630a8b6b4 | 9028d228ac200bbefe3a711342514dd4e4458bff | /src/measure_theory/borel_space.lean | 656b2ea4ffe1a5b17199425793a873c3f3e8771a | [
"Apache-2.0"
] | permissive | mcncm/mathlib | 8d25099344d9d2bee62822cb9ed43aa3e09fa05e | fde3d78cadeec5ef827b16ae55664ef115e66f57 | refs/heads/master | 1,672,743,316,277 | 1,602,618,514,000 | 1,602,618,514,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 45,695 | 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.measure_space
import analysis.normed_space.finite_dimension
/-!
# 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`, `ennreal`.
* A measure is `regular` if it is finite on compact sets, inner regular and outer regular.
## Main statements
* `is_open.is_measurable`, `is_closed.is_measurable`: 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`;
* `measurable.ennreal*` : special cases for arithmetic operations on `ennreal`s.
-/
noncomputable theory
open classical set filter
open_locale classical big_operators topological_space nnreal
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 is_measurable.bUnion s.countable_encodable,
intros x hx,
apply is_measurable.of_compl,
apply generate_measurable.basic,
exact is_closed_singleton
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 @is_measurable.univ α (generate_from s) },
case generate_open.inter : s₁ s₂ _ _ hs₁ hs₂
{ exact @is_measurable.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 @is_measurable.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 is_pi_system_is_open [topological_space α] : is_pi_system (is_open : set α → Prop) :=
λ s t hs ht hst, is_open_inter hs ht
section order_topology
variable (α)
variables [topological_space α] [second_countable_topology α] [linear_order α] [order_topology α]
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 : α, is_measurable (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 is_measurable.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) (hf s hs)
/-- 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 γ]
[measurable_space δ]
lemma is_open.is_measurable (h : is_open s) : is_measurable s :=
opens_measurable_space.borel_le _ $ generate_measurable.basic _ h
lemma is_measurable_interior : is_measurable (interior s) := is_open_interior.is_measurable
lemma is_closed.is_measurable (h : is_closed s) : is_measurable s :=
h.is_measurable.of_compl
lemma is_compact.is_measurable [t2_space α] (h : is_compact s) : is_measurable s :=
h.is_closed.is_measurable
lemma is_measurable_closure : is_measurable (closure s) :=
is_closed_closure.is_measurable
lemma measurable_of_is_open {f : δ → γ} (hf : ∀ s, is_open s → is_measurable (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 → is_measurable (f ⁻¹' s)) :
measurable f :=
begin
apply measurable_of_is_open, intros s hs,
rw [← is_measurable.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 → is_measurable (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.is_measurable.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 : is_measurable s`.
-/
lemma is_measurable.nhds_within_is_measurably_generated {s : set α} (hs : is_measurable 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.is_measurable⟩
instance prod.opens_measurable_space [second_countable_topology α] [second_countable_topology β] :
opens_measurable_space (α × β) :=
begin
refine ⟨_⟩,
rcases is_open_generated_countable_inter α with ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩,
rcases is_open_generated_countable_inter β with ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩,
have : prod.topological_space = generate_from {g | ∃u∈a, ∃v∈b, g = set.prod u v},
{ rw [ha₅, hb₅], exact prod_generate_from_generate_from_eq ha₄ hb₄ },
rw [borel_eq_generate_from_of_subbasis this],
apply generate_from_le,
rintros _ ⟨u, hu, v, hv, rfl⟩,
have hu : is_open u, by { rw [ha₅], exact generate_open.basic _ hu },
have hv : is_open v, by { rw [hb₅], exact generate_open.basic _ hv },
exact hu.is_measurable.prod hv.is_measurable
end
section preorder
variables [preorder α] [order_closed_topology α] {a b : α}
lemma is_measurable_Ici : is_measurable (Ici a) := is_closed_Ici.is_measurable
lemma is_measurable_Iic : is_measurable (Iic a) := is_closed_Iic.is_measurable
lemma is_measurable_Icc : is_measurable (Icc a b) := is_closed_Icc.is_measurable
instance nhds_within_Ici_is_measurably_generated :
(𝓝[Ici b] a).is_measurably_generated :=
is_measurable_Ici.nhds_within_is_measurably_generated _
instance nhds_within_Iic_is_measurably_generated :
(𝓝[Iic b] a).is_measurably_generated :=
is_measurable_Iic.nhds_within_is_measurably_generated _
instance at_top_is_measurably_generated : (filter.at_top : filter α).is_measurably_generated :=
@filter.infi_is_measurably_generated _ _ _ _ $
λ a, (is_measurable_Ici : is_measurable (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, (is_measurable_Iic : is_measurable (Iic a)).principal_is_measurably_generated
end preorder
section partial_order
variables [partial_order α] [order_closed_topology α] [second_countable_topology α]
{a b : α}
lemma is_measurable_le' : is_measurable {p : α × α | p.1 ≤ p.2} :=
order_closed_topology.is_closed_le'.is_measurable
lemma is_measurable_le {f g : δ → α} (hf : measurable f) (hg : measurable g) :
is_measurable {a | f a ≤ g a} :=
hf.prod_mk hg is_measurable_le'
end partial_order
section linear_order
variables [linear_order α] [order_closed_topology α] {a b : α}
lemma is_measurable_Iio : is_measurable (Iio a) := is_open_Iio.is_measurable
lemma is_measurable_Ioi : is_measurable (Ioi a) := is_open_Ioi.is_measurable
lemma is_measurable_Ioo : is_measurable (Ioo a b) := is_open_Ioo.is_measurable
lemma is_measurable_Ioc : is_measurable (Ioc a b) := is_measurable_Ioi.inter is_measurable_Iic
lemma is_measurable_Ico : is_measurable (Ico a b) := is_measurable_Ici.inter is_measurable_Iio
instance nhds_within_Ioi_is_measurably_generated :
(𝓝[Ioi b] a).is_measurably_generated :=
is_measurable_Ioi.nhds_within_is_measurably_generated _
instance nhds_within_Iio_is_measurably_generated :
(𝓝[Iio b] a).is_measurably_generated :=
is_measurable_Iio.nhds_within_is_measurably_generated _
variables [second_countable_topology α]
lemma is_measurable_lt' : is_measurable {p : α × α | p.1 < p.2} :=
(is_open_lt continuous_fst continuous_snd).is_measurable
lemma is_measurable_lt {f g : δ → α} (hf : measurable f) (hg : measurable g) :
is_measurable {a | f a < g a} :=
hf.prod_mk hg is_measurable_lt'
end linear_order
section decidable_linear_order
variables [decidable_linear_order α] [order_closed_topology α]
lemma is_measurable_interval {a b : α} : is_measurable (interval a b) :=
is_measurable_Icc
variables [second_countable_topology α]
lemma measurable.max {f g : δ → α} (hf : measurable f) (hg : measurable g) :
measurable (λ a, max (f a) (g a)) :=
hf.piecewise (is_measurable_le hg hf) hg
lemma measurable.min {f g : δ → α} (hf : measurable f) (hg : measurable g) :
measurable (λ a, min (f a) (g a)) :=
hf.piecewise (is_measurable_le hf hg) hg
end decidable_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 homeomorphism between two Borel spaces is a measurable equivalence.-/
def homeomorph.to_measurable_equiv {α : Type*} {β : Type*} [topological_space α]
[measurable_space α] [borel_space α] [topological_space β] [measurable_space β]
[borel_space β] (h : α ≃ₜ β) :
measurable_equiv α β :=
{ measurable_to_fun := h.continuous_to_fun.measurable,
measurable_inv_fun := h.continuous_inv_fun.measurable,
.. h }
lemma measurable_of_continuous_on_compl_singleton [t1_space α] {f : α → γ} (a : α)
(hf : continuous_on f {x | x ≠ 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 measurable.smul [semiring α] [second_countable_topology α]
[add_comm_monoid γ] [second_countable_topology γ]
[semimodule α γ] [topological_semimodule α γ]
{f : δ → α} {g : δ → γ} (hf : measurable f) (hg : measurable g) :
measurable (λ c, f c • g c) :=
continuous_smul.measurable2 hf hg
lemma measurable.const_smul {R M : Type*} [topological_space R] [semiring R]
[add_comm_monoid M] [semimodule R M] [topological_space M] [topological_semimodule R M]
[measurable_space M] [borel_space M]
{f : δ → M} (hf : measurable f) (c : R) :
measurable (λ x, c • f x) :=
(continuous_const.smul continuous_id).measurable.comp hf
lemma measurable_const_smul_iff {α : Type*} [topological_space α]
[division_ring α] [add_comm_monoid γ]
[semimodule α γ] [topological_semimodule α γ]
{f : δ → γ} {c : α} (hc : c ≠ 0) :
measurable (λ x, c • f x) ↔ measurable f :=
⟨λ h, by simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.const_smul c⁻¹,
λ h, h.const_smul c⟩
lemma measurable.const_mul {R : Type*} [topological_space R] [measurable_space R]
[borel_space R] [semiring R] [topological_semiring R]
{f : δ → R} (hf : measurable f) (c : R) :
measurable (λ x, c * f x) :=
hf.const_smul c
lemma measurable.mul_const {R : Type*} [topological_space R] [measurable_space R]
[borel_space R] [semiring R] [topological_semiring R]
{f : δ → R} (hf : measurable f) (c : R) :
measurable (λ x, f x * c) :=
(continuous_id.mul continuous_const).measurable.comp hf
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 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 prod.borel_space [second_countable_topology α] [second_countable_topology β] :
borel_space (α × β) :=
⟨le_antisymm prod_le_borel_prod opens_measurable_space.borel_le⟩
@[to_additive]
lemma measurable_mul [has_mul α] [has_continuous_mul α] [second_countable_topology α] :
measurable (λ p : α × α, p.1 * p.2) :=
continuous_mul.measurable
@[to_additive]
lemma measurable.mul [has_mul α] [has_continuous_mul α] [second_countable_topology α]
{f : δ → α} {g : δ → α} : measurable f → measurable g → measurable (λ a, f a * g a) :=
continuous_mul.measurable2
/-- A variant of `measurable.mul` that uses `*` on functions -/
@[to_additive]
lemma measurable.mul' [has_mul α] [has_continuous_mul α] [second_countable_topology α]
{f : δ → α} {g : δ → α} : measurable f → measurable g → measurable (f * g) :=
measurable.mul
@[to_additive]
lemma measurable_mul_left [has_mul α] [has_continuous_mul α] (x : α) :
measurable (λ y : α, x * y) :=
continuous.measurable $ continuous_const.mul continuous_id
@[to_additive]
lemma measurable_mul_right [has_mul α] [has_continuous_mul α] (x : α) :
measurable (λ y : α, y * x) :=
continuous.measurable $ continuous_id.mul continuous_const
@[to_additive]
lemma finset.measurable_prod {ι : Type*} [comm_monoid α] [has_continuous_mul α]
[second_countable_topology α] {f : ι → δ → α} (s : finset ι) (hf : ∀i, measurable (f i)) :
measurable (λ a, ∏ i in s, f i a) :=
finset.induction_on s
(by simp only [finset.prod_empty, measurable_const])
(assume i s his ih, by simpa [his] using (hf i).mul ih)
@[to_additive]
lemma measurable_inv [group α] [topological_group α] : measurable (has_inv.inv : α → α) :=
continuous_inv.measurable
@[to_additive]
lemma measurable.inv [group α] [topological_group α] {f : δ → α} (hf : measurable f) :
measurable (λ a, (f a)⁻¹) :=
measurable_inv.comp hf
lemma measurable_inv' {α : Type*} [normed_field α] [measurable_space α] [borel_space α] :
measurable (has_inv.inv : α → α) :=
measurable_of_continuous_on_compl_singleton 0 normed_field.continuous_on_inv
lemma measurable.inv' {α : Type*} [normed_field α] [measurable_space α] [borel_space α]
{f : δ → α} (hf : measurable f) :
measurable (λ a, (f a)⁻¹) :=
measurable_inv'.comp hf
@[to_additive]
lemma measurable.of_inv [group α] [topological_group α] {f : δ → α}
(hf : measurable (λ a, (f a)⁻¹)) : measurable f :=
by simpa only [inv_inv] using hf.inv
@[simp, to_additive]
lemma measurable_inv_iff [group α] [topological_group α] {f : δ → α} :
measurable (λ a, (f a)⁻¹) ↔ measurable f :=
⟨measurable.of_inv, measurable.inv⟩
lemma measurable.sub [add_group α] [topological_add_group α] [second_countable_topology α]
{f g : δ → α} (hf : measurable f) (hg : measurable g) :
measurable (λ x, f x - g x) :=
hf.add hg.neg
lemma measurable_comp_iff_of_closed_embedding {f : δ → β} (g : β → γ) (hg : closed_embedding g) :
measurable (g ∘ f) ↔ measurable f :=
begin
refine ⟨λ hf, _, λ hf, hg.continuous.measurable.comp hf⟩,
apply measurable_of_is_closed, intros s hs,
convert hf (hg.is_closed_map s hs).is_measurable,
rw [@preimage_comp _ _ _ f g, preimage_image_eq _ hg.to_embedding.inj]
end
section linear_order
variables [linear_order α] [order_topology α] [second_countable_topology α]
lemma measurable_of_Iio {f : δ → α} (hf : ∀ x, is_measurable (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 measurable_of_Ioi {f : δ → α} (hf : ∀ x, is_measurable (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 measurable_of_Iic {f : δ → α} (hf : ∀ x, is_measurable (f ⁻¹' Iic x)) : measurable f :=
begin
apply measurable_of_Ioi,
simp_rw [← compl_Iic, preimage_compl, is_measurable.compl_iff],
assumption
end
lemma measurable_of_Ici {f : δ → α} (hf : ∀ x, is_measurable (f ⁻¹' Ici x)) : measurable f :=
begin
apply measurable_of_Iio,
simp_rw [← compl_Ici, preimage_compl, is_measurable.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 only [set.preimage, mem_Ioi, lt_is_lub_iff (hg _), exists_range_iff, set_of_exists],
exact is_measurable.Union (λ i, hf i (is_open_lt' _).is_measurable)
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 only [set.preimage, mem_Iio, is_glb_lt_iff (hg _), exists_range_iff, set_of_exists],
exact is_measurable.Union (λ i, hf i (is_open_gt' _).is_measurable)
end
end linear_order
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)
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 α]
lemma measurable_supr {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ b, ⨆ i, f i b) :=
measurable.is_lub hf $ λ b, is_lub_supr
lemma measurable_infi {ι} [encodable ι] {f : ι → δ → α} (hf : ∀ i, measurable (f i)) :
measurable (λ b, ⨅ i, f i b) :=
measurable.is_glb hf $ λ 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 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) }
/-- `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.
-/
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.
-/
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 α] [second_countable_topology α] [order_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 is_measurable.bInter hs (λ i hi, is_measurable_le (hf i) measurable_const) }
end
end conditionally_complete_linear_order
/-- Convert a `homeomorph` to a `measurable_equiv`. -/
def homemorph.to_measurable_equiv (h : α ≃ₜ β) :
measurable_equiv α β :=
{ 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 := borel ℝ≥0
instance nnreal.borel_space : borel_space ℝ≥0 := ⟨rfl⟩
instance ennreal.measurable_space : measurable_space ennreal := borel ennreal
instance ennreal.borel_space : borel_space ennreal := ⟨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
lemma is_measurable_ball : is_measurable (metric.ball x ε) :=
metric.is_open_ball.is_measurable
lemma is_measurable_closed_ball : is_measurable (metric.closed_ball x ε) :=
metric.is_closed_ball.is_measurable
lemma measurable_inf_dist {s : set α} : measurable (λ x, inf_dist x s) :=
(continuous_inf_dist_pt s).measurable
lemma measurable.inf_dist {f : β → α} (hf : measurable f) {s : set α} :
measurable (λ x, inf_dist (f x) s) :=
measurable_inf_dist.comp hf
lemma measurable_inf_nndist {s : set α} : measurable (λ x, inf_nndist x s) :=
(continuous_inf_nndist_pt s).measurable
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 α]
lemma measurable_dist : measurable (λ p : α × α, dist p.1 p.2) :=
continuous_dist.measurable
lemma measurable.dist {f g : β → α} (hf : measurable f) (hg : measurable g) :
measurable (λ b, dist (f b) (g b)) :=
continuous_dist.measurable2 hf hg
lemma measurable_nndist : measurable (λ p : α × α, nndist p.1 p.2) :=
continuous_nndist.measurable
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 : α} {ε : ennreal}
open emetric
lemma is_measurable_eball : is_measurable (emetric.ball x ε) :=
emetric.is_open_ball.is_measurable
lemma measurable_edist_right : measurable (edist x) :=
(continuous_const.edist continuous_id).measurable
lemma measurable_edist_left : measurable (λ y, edist y x) :=
(continuous_id.edist continuous_const).measurable
lemma measurable_inf_edist {s : set α} : measurable (λ x, inf_edist x s) :=
continuous_inf_edist.measurable
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 α]
lemma measurable_edist : measurable (λ p : α × α, edist p.1 p.2) :=
continuous_edist.measurable
lemma measurable.edist {f g : β → α} (hf : measurable f) (hg : measurable g) :
measurable (λ b, edist (f b) (g b)) :=
continuous_edist.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}) :=
borel_eq_generate_from_of_subbasis is_topological_basis_Ioo_rat.2.2
lemma measure_ext_Ioo_rat {μ ν : measure ℝ} [locally_finite_measure μ]
(h : ∀ a b : ℚ, μ (Ioo a b) = ν (Ioo a b)) : μ = ν :=
begin
refine measure.ext_of_generate_from_of_cover_subset borel_eq_generate_from_Ioo_rat _
(subset.refl _) _ _ _ _,
{ simp only [is_pi_system, mem_Union, mem_singleton_iff],
rintros _ _ ⟨a₁, b₁, h₁, rfl⟩ ⟨a₂, b₂, h₂, rfl⟩ ne,
simp only [Ioo_inter_Ioo, sup_eq_max, inf_eq_min, ← rat.cast_max, ← rat.cast_min,
nonempty_Ioo] at ne ⊢,
refine ⟨_, _, _, rfl⟩,
assumption_mod_cast },
{ exact countable_Union (λ a, (countable_encodable _).bUnion $ λ _ _, countable_singleton _) },
{ exact is_topological_basis_Ioo_rat.2.1 },
{ simp only [mem_Union, mem_singleton_iff],
rintros _ ⟨a, b, h, rfl⟩,
refine (measure_mono subset_closure).trans_lt _,
rw [closure_Ioo],
exacts [compact_Icc.finite_measure, rat.cast_lt.2 h] },
{ simp only [mem_Union, mem_singleton_iff],
rintros _ ⟨a, b, hab, rfl⟩,
exact h a b }
end
lemma borel_eq_generate_from_Iio_rat :
borel ℝ = generate_from (⋃ a : ℚ, {Iio a}) :=
begin
let g, swap,
apply le_antisymm (_ : _ ≤ g) (measurable_space.generate_from_le (λ t, _)),
{ rw borel_eq_generate_from_Ioo_rat,
refine generate_from_le (λ t, _),
simp only [mem_Union], rintro ⟨a, b, h, H⟩,
rw [mem_singleton_iff.1 H],
rw (set.ext (λ x, _) : Ioo (a : ℝ) b = (⋃c>a, (Iio c)ᶜ) ∩ Iio b),
{ have hg : ∀ q : ℚ, g.is_measurable' (Iio q) :=
λ q, generate_measurable.basic (Iio q) (by { simp, exact ⟨_, rfl⟩ }),
refine @is_measurable.inter _ g _ _ _ (hg _),
refine @is_measurable.bUnion _ _ g _ _ (countable_encodable _) (λ c h, _),
exact @is_measurable.compl _ _ g (hg _) },
{ simp [Ioo, Iio],
refine and_congr _ iff.rfl,
exact ⟨λ h,
let ⟨c, ac, cx⟩ := exists_rat_btwn h in
⟨c, rat.cast_lt.1 ac, le_of_lt cx⟩,
λ ⟨c, ac, cx⟩, lt_of_lt_of_le (rat.cast_lt.2 ac) cx⟩ } },
{ simp, rintro r rfl, exact is_open_Iio.is_measurable }
end
end real
variable [measurable_space α]
lemma measurable.sub_nnreal {f g : α → ℝ≥0} :
measurable f → measurable g → measurable (λ a, f a - g a) :=
continuous_sub.measurable2
lemma measurable.nnreal_of_real {f : α → ℝ} (hf : measurable f) :
measurable (λ x, nnreal.of_real (f x)) :=
nnreal.continuous_of_real.measurable.comp hf
lemma nnreal.measurable_coe : measurable (coe : ℝ≥0 → ℝ) :=
nnreal.continuous_coe.measurable
lemma measurable.nnreal_coe {f : α → ℝ≥0} (hf : measurable f) :
measurable (λ x, (f x : ℝ)) :=
nnreal.measurable_coe.comp hf
lemma measurable.ennreal_coe {f : α → ℝ≥0} (hf : measurable f) :
measurable (λ x, (f x : ennreal)) :=
ennreal.continuous_coe.measurable.comp hf
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 `ennreal` numbers is `measurable_equiv` to `ℝ≥0`. -/
def measurable_equiv.ennreal_equiv_nnreal : measurable_equiv {r : ennreal | r ≠ ⊤} ℝ≥0 :=
ennreal.ne_top_homeomorph_nnreal.to_measurable_equiv
namespace ennreal
lemma measurable_coe : measurable (coe : ℝ≥0 → ennreal) :=
measurable_id.ennreal_coe
lemma measurable_of_measurable_nnreal {f : ennreal → α}
(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)
/-- `ennreal` is `measurable_equiv` to `ℝ≥0 ⊕ unit`. -/
def ennreal_equiv_sum :
measurable_equiv ennreal (ℝ≥0 ⊕ unit) :=
{ measurable_to_fun := measurable_of_measurable_nnreal measurable_inl,
measurable_inv_fun := measurable_sum measurable_coe (@measurable_const ennreal unit _ _ ⊤),
.. equiv.option_equiv_sum_punit ℝ≥0 }
open function (uncurry)
lemma measurable_of_measurable_nnreal_prod [measurable_space β] [measurable_space γ]
{f : ennreal × β → γ} (H₁ : measurable (λ p : ℝ≥0 × β, f (p.1, p.2)))
(H₂ : measurable (λ x, f (⊤, x))) :
measurable f :=
let e : measurable_equiv (ennreal × β) (ℝ≥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 : ennreal × ennreal → β} (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₂)
lemma measurable_of_real : measurable ennreal.of_real :=
ennreal.continuous_of_real.measurable
lemma measurable_to_real : measurable ennreal.to_real :=
ennreal.measurable_of_measurable_nnreal nnreal.measurable_coe
lemma measurable_to_nnreal : measurable ennreal.to_nnreal :=
ennreal.measurable_of_measurable_nnreal measurable_id
lemma measurable_mul : measurable (λ p : ennreal × ennreal, p.1 * p.2) :=
begin
apply measurable_of_measurable_nnreal_nnreal,
{ simp only [← ennreal.coe_mul, measurable_mul.ennreal_coe] },
{ simp only [ennreal.top_mul, ennreal.coe_eq_zero],
exact measurable_const.piecewise (is_measurable_singleton _) measurable_const },
{ simp only [ennreal.mul_top, ennreal.coe_eq_zero],
exact measurable_const.piecewise (is_measurable_singleton _) measurable_const }
end
lemma measurable_sub : measurable (λ p : ennreal × ennreal, p.1 - p.2) :=
by apply measurable_of_measurable_nnreal_nnreal;
simp [← ennreal.coe_sub, continuous_sub.measurable.ennreal_coe]
end ennreal
lemma measurable.to_nnreal {f : α → ennreal} (hf : measurable f) :
measurable (λ x, (f x).to_nnreal) :=
ennreal.measurable_to_nnreal.comp hf
lemma measurable_ennreal_coe_iff {f : α → ℝ≥0} :
measurable (λ x, (f x : ennreal)) ↔ measurable f :=
⟨λ h, h.to_nnreal, λ h, h.ennreal_coe⟩
lemma measurable.to_real {f : α → ennreal} (hf : measurable f) :
measurable (λ x, ennreal.to_real (f x)) :=
ennreal.measurable_to_real.comp hf
lemma measurable.ennreal_mul {f g : α → ennreal} (hf : measurable f) (hg : measurable g) :
measurable (λ a, f a * g a) :=
ennreal.measurable_mul.comp (hf.prod_mk hg)
lemma measurable.ennreal_add {f g : α → ennreal}
(hf : measurable f) (hg : measurable g) : measurable (λ a, f a + g a) :=
hf.add hg
lemma measurable.ennreal_sub {f g : α → ennreal} (hf : measurable f) (hg : measurable g) :
measurable (λ a, f a - g a) :=
ennreal.measurable_sub.comp (hf.prod_mk hg)
/-- note: `ennreal` can probably be generalized in a future version of this lemma. -/
lemma measurable.ennreal_tsum {ι} [encodable ι] {f : ι → α → ennreal} (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 h }
section normed_group
variables [normed_group α] [opens_measurable_space α] [measurable_space β]
lemma measurable_norm : measurable (norm : α → ℝ) :=
continuous_norm.measurable
lemma measurable.norm {f : β → α} (hf : measurable f) : measurable (λ a, norm (f a)) :=
measurable_norm.comp hf
lemma measurable_nnnorm : measurable (nnnorm : α → ℝ≥0) :=
continuous_nnnorm.measurable
lemma measurable.nnnorm {f : β → α} (hf : measurable f) : measurable (λ a, nnnorm (f a)) :=
measurable_nnnorm.comp hf
lemma measurable_ennnorm : measurable (λ x : α, (nnnorm x : ennreal)) :=
measurable_nnnorm.ennreal_coe
lemma measurable.ennnorm {f : β → α} (hf : measurable f) :
measurable (λ a, (nnnorm (f a) : ennreal)) :=
hf.nnnorm.ennreal_coe
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_ennreal_coe_iff],
have : ∀ x, liminf u (λ n, (f n x : ennreal)) = (g x : ennreal) :=
λ x, ((ennreal.continuous_coe.tendsto (g x)).comp (lim x)).liminf_eq,
simp_rw [← this],
show measurable (λ x, liminf u (λ n, (f n x : ennreal))),
exact measurable_liminf' (λ i, (hf i).ennreal_coe) 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 (is_measurable_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 _)
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]
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
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)
end normed_space
namespace measure_theory
namespace measure
variables [topological_space α]
/-- A measure `μ` is regular if
- it is finite on all compact sets;
- it is outer regular: `μ(A) = inf { μ(U) | A ⊆ U open }` for `A` measurable;
- it is inner regular: `μ(U) = sup { μ(K) | K ⊆ U compact }` for `U` open. -/
structure regular (μ : measure α) : Prop :=
(lt_top_of_is_compact : ∀ {{K : set α}}, is_compact K → μ K < ⊤)
(outer_regular : ∀ {{A : set α}}, is_measurable A →
(⨅ (U : set α) (h : is_open U) (h2 : A ⊆ U), μ U) ≤ μ A)
(inner_regular : ∀ {{U : set α}}, is_open U →
μ U ≤ ⨆ (K : set α) (h : is_compact K) (h2 : K ⊆ U), μ K)
namespace regular
lemma outer_regular_eq {μ : measure α} (hμ : μ.regular) {{A : set α}}
(hA : is_measurable A) : (⨅ (U : set α) (h : is_open U) (h2 : A ⊆ U), μ U) = μ A :=
le_antisymm (hμ.outer_regular hA) $ le_infi $ λ s, le_infi $ λ hs, le_infi $ λ h2s, μ.mono h2s
lemma inner_regular_eq {μ : measure α} (hμ : μ.regular) {{U : set α}}
(hU : is_open U) : (⨆ (K : set α) (h : is_compact K) (h2 : K ⊆ U), μ K) = μ U :=
le_antisymm (supr_le $ λ s, supr_le $ λ hs, supr_le $ λ h2s, μ.mono h2s) (hμ.inner_regular hU)
protected lemma map [opens_measurable_space α] [measurable_space β] [topological_space β]
[t2_space β] [borel_space β] {μ : measure α} (hμ : μ.regular) (f : α ≃ₜ β) :
(measure.map f μ).regular :=
begin
have hf := f.continuous.measurable,
have h2f := f.to_equiv.injective.preimage_surjective,
have h3f := f.to_equiv.surjective,
split,
{ intros K hK, rw [map_apply hf hK.is_measurable],
apply hμ.lt_top_of_is_compact, rwa f.compact_preimage },
{ intros A hA, rw [map_apply hf hA, ← hμ.outer_regular_eq (hf hA)],
refine le_of_eq _, apply infi_congr (preimage f) h2f,
intro U, apply infi_congr_Prop f.is_open_preimage, intro hU,
apply infi_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U,
rw [map_apply hf hU.is_measurable], },
{ intros U hU, rw [map_apply hf hU.is_measurable, ← hμ.inner_regular_eq (f.continuous U hU)],
refine ge_of_eq _, apply supr_congr (preimage f) h2f,
intro K, apply supr_congr_Prop f.compact_preimage, intro hK,
apply supr_congr_Prop h3f.preimage_subset_preimage_iff, intro h2U,
rw [map_apply hf hK.is_measurable] }
end
protected lemma smul {μ : measure α} (hμ : μ.regular) {x : ennreal} (hx : x < ⊤) :
(x • μ).regular :=
begin
split,
{ intros K hK, exact ennreal.mul_lt_top hx (hμ.lt_top_of_is_compact hK) },
{ intros A hA, rw [coe_smul],
refine le_trans _ (ennreal.mul_left_mono $ hμ.outer_regular hA),
simp only [infi_and'], simp only [infi_subtype'],
haveI : nonempty {s : set α // is_open s ∧ A ⊆ s} := ⟨⟨set.univ, is_open_univ, subset_univ _⟩⟩,
rw [ennreal.mul_infi], refl', exact ne_of_lt hx },
{ intros U hU, rw [coe_smul], refine le_trans (ennreal.mul_left_mono $ hμ.inner_regular hU) _,
simp only [supr_and'], simp only [supr_subtype'],
rw [ennreal.mul_supr], refl' }
end
end regular
end measure
end measure_theory
|
5c47d34a3732a8612c5332b4e0c2a07a5c8e8004 | 6dbfa3fb323bb2e2c3917c61d46053dd0de739cd | /src/defs_lemmas.lean | 03f4e2550debab607a8bb6f7506e455986134e72 | [] | no_license | amichaelsen/Lean-Chinese-Remainder-Theorem | b054fc32e0f5383eb83ec7155932c50d92de1038 | 9efb4b97da30e5aedb6af3c95d5132ac300f2902 | refs/heads/master | 1,680,787,277,127 | 1,619,155,524,000 | 1,619,155,524,000 | 275,883,966 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,195 | lean | import data.nat.basic
import data.nat.modeq
import data.nat.gcd
import data.zmod.basic
import tactic
import algebra.euclidean_domain
import data.int.basic
import data.equiv.ring
import lemmas2
noncomputable theory
open nat nat.modeq zmod euclidean_domain lemmas2
namespace defs_lemmas
/- DEFINITIONS -/
/--
A structure for a congruence x ≡ a [MOD n]
-/
def cong := (Σ (n : ℕ), zmod n)
/--
A list of congruence relations
-/
def congruences := list cong
/-Examples of above definitions
def x : cong := ⟨5, ↑2⟩
def y : congruences := [⟨5, ↑2⟩ , ⟨3, ↑2⟩]
-/
/- LIST PROPERTIES -/
/--
All moduli for the congruences in the list are pairwise coprime
-/
def pairwise_coprime (l : congruences) : Prop :=
list.pairwise (λ (x y : cong), nat.coprime x.1 y.1) l
/--
All moduli for the congruences in the list are nonzero
-/
def nonzero_cong ( l : congruences) : Prop :=
list.all l (λ (c : cong), 0 < c.1)
/--
Defines when x is a solution to the list of congruences in l
-/
def solution (x : ℕ) (l : congruences) : Prop :=
list.all l (λ (c : cong), modeq c.1 x c.2.val)
/--
Takes the product of the defining moduli of all congruences in the list
-/
def cong_prod : congruences → ℕ
| list.nil := 1
| (h :: t) := h.1 * cong_prod t
--
/- LEMMAS ABOUT LIST PROPERTIES -/
/--
If a list satisfies nonzero_cong, so does the tail and the head has nonzero moduli
-/
lemma subset_nonzero (c : cong) (l : list cong) (H : nonzero_cong (c :: l)) :
0 < c.1 ∧ nonzero_cong l :=
begin
unfold nonzero_cong at H,
rw list.all_iff_forall_prop at H,
split,
{ exact H c (by exact list.mem_cons_self c l), },
{ unfold nonzero_cong,
rw list.all_iff_forall_prop,
rintros a ha,
exact H a (by exact list.mem_cons_of_mem _ ha), }
end
/--
If a list satisfies pairwise_coprime the head is coprime to all
moduli in the tail and the tail satisfies pairwise_coprime
-/
lemma subset_coprime (c : cong) (l : list cong) (H : pairwise_coprime (c :: l)) :
(∀ (a : cong), a ∈ l → coprime c.1 a.1) ∧ pairwise_coprime l :=
begin
unfold pairwise_coprime at H,
rw list.pairwise_cons at H,
exact H,
end
/--
The modulus for an element of list of congruences will divide the
cong_prod of the list, the product of all moduli in the list
-/
lemma mem_div_prod (l : list cong) (a : cong) (H : a ∈ l) : a.1 ∣ cong_prod l :=
begin
induction l with head tail ihtail,
--nil case
exfalso,
exact H,
--induction case
dsimp[cong_prod],
cases H,
rw H,
simp only [nat.dvd_mul_right],
specialize ihtail H,
cases ihtail with c hc,
rw hc,
rw mul_comm,
use c * head.fst,
ring,
end
/- LEMMAS ABOUT CONG_PROD OUTPUTS -/
/--
Given a list of congruences with nonzero (i.e. positive)
moduli, the product of those moduli will be positive
-/
lemma pos_prod (l : congruences) (H : nonzero_cong l) : 0 < cong_prod l :=
begin
induction l with head tail ihtail,
{ dsimp [cong_prod],
linarith, },
{ have nonzero_parts := subset_nonzero head tail H,
specialize ihtail nonzero_parts.right,
dsimp [cong_prod],
exact mul_pos nonzero_parts.left ihtail,}
end
/--
The modulus of the first congruence is coprime to the product of the moduli of
the tail of the list assuming that the entire list satisfies pairwise_coprime
-/
lemma coprime_prod (c : cong) (l : list cong) (H : pairwise_coprime (c :: l)) :
coprime c.1 (cong_prod l) :=
begin
induction l with head tail ihtail,
{ dsimp[cong_prod],
by exact c.fst.coprime_one_right, },
{ dsimp[cong_prod],
apply nat.coprime.mul_right,
exact (subset_coprime c (head :: tail) H).left head (by exact list.mem_cons_self head tail),
apply ihtail,
unfold pairwise_coprime at *,
rw list.pairwise_cons at *,
split,
intros a ha,
refine H.left a (by exact list.mem_cons_of_mem head ha),
exact list.pairwise_of_pairwise_cons H.right, },
end
end defs_lemmas
|
5b6d117db0fed9143cf9bd20845993f428acc79a | 130c49f47783503e462c16b2eff31933442be6ff | /stage0/src/Lean.lean | edfa89684e44f896da28069b4d4d2e1c22f75634 | [
"Apache-2.0"
] | permissive | Hazel-Brown/lean4 | 8aa5860e282435ffc30dcdfccd34006c59d1d39c | 79e6732fc6bbf5af831b76f310f9c488d44e7a16 | refs/heads/master | 1,689,218,208,951 | 1,629,736,869,000 | 1,629,736,896,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 775 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Data
import Lean.Compiler
import Lean.Environment
import Lean.Modifiers
import Lean.ProjFns
import Lean.Runtime
import Lean.ResolveName
import Lean.Attributes
import Lean.Parser
import Lean.ReducibilityAttrs
import Lean.Elab
import Lean.Class
import Lean.LocalContext
import Lean.MetavarContext
import Lean.AuxRecursor
import Lean.Meta
import Lean.Util
import Lean.Eval
import Lean.Structure
import Lean.PrettyPrinter
import Lean.CoreM
import Lean.InternalExceptionId
import Lean.Server
import Lean.ScopedEnvExtension
import Lean.DocString
import Lean.DeclarationRange
import Lean.LazyInitExtension
|
9f7118bf9e38830078f3faabcf466a02cdf67ebc | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/Lean3Lib/init/data/list/lemmas_auto.lean | 5f4c6a158878407838bc0256003adbadbcfd28e8 | [] | 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 | 10,058 | lean | /-
Copyright (c) 2014 Parikshit Khanna. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.data.list.basic
import Mathlib.Lean3Lib.init.function
import Mathlib.Lean3Lib.init.meta.default
import Mathlib.Lean3Lib.init.data.nat.lemmas
import Mathlib.Lean3Lib.init.meta.interactive
import Mathlib.Lean3Lib.init.meta.smt.rsimp
universes u v w w₂ w₁
namespace Mathlib
namespace list
/- append -/
@[simp] theorem nil_append {α : Type u} (s : List α) : [] ++ s = s := rfl
@[simp] theorem cons_append {α : Type u} (x : α) (s : List α) (t : List α) :
x :: s ++ t = x :: (s ++ t) :=
rfl
@[simp] theorem append_nil {α : Type u} (t : List α) : t ++ [] = t := sorry
@[simp] theorem append_assoc {α : Type u} (s : List α) (t : List α) (u : List α) :
s ++ t ++ u = s ++ (t ++ u) :=
sorry
/- length -/
theorem length_cons {α : Type u} (a : α) (l : List α) : length (a :: l) = length l + 1 := rfl
@[simp] theorem length_append {α : Type u} (s : List α) (t : List α) :
length (s ++ t) = length s + length t :=
sorry
@[simp] theorem length_repeat {α : Type u} (a : α) (n : ℕ) : length (repeat a n) = n := sorry
@[simp] theorem length_tail {α : Type u} (l : List α) : length (tail l) = length l - 1 :=
list.cases_on l (Eq.refl (length (tail [])))
fun (l_hd : α) (l_tl : List α) => Eq.refl (length (tail (l_hd :: l_tl)))
-- TODO(Leo): cleanup proof after arith dec proc
@[simp] theorem length_drop {α : Type u} (i : ℕ) (l : List α) : length (drop i l) = length l - i :=
sorry
/- map -/
theorem map_cons {α : Type u} {β : Type v} (f : α → β) (a : α) (l : List α) :
map f (a :: l) = f a :: map f l :=
rfl
@[simp] theorem map_append {α : Type u} {β : Type v} (f : α → β) (l₁ : List α) (l₂ : List α) :
map f (l₁ ++ l₂) = map f l₁ ++ map f l₂ :=
sorry
theorem map_singleton {α : Type u} {β : Type v} (f : α → β) (a : α) : map f [a] = [f a] := rfl
@[simp] theorem map_id {α : Type u} (l : List α) : map id l = l := sorry
@[simp] theorem map_map {α : Type u} {β : Type v} {γ : Type w} (g : β → γ) (f : α → β)
(l : List α) : map g (map f l) = map (g ∘ f) l :=
sorry
@[simp] theorem length_map {α : Type u} {β : Type v} (f : α → β) (l : List α) :
length (map f l) = length l :=
sorry
/- bind -/
@[simp] theorem nil_bind {α : Type u} {β : Type v} (f : α → List β) : list.bind [] f = [] := sorry
@[simp] theorem cons_bind {α : Type u} {β : Type v} (x : α) (xs : List α) (f : α → List β) :
list.bind (x :: xs) f = f x ++ list.bind xs f :=
sorry
@[simp] theorem append_bind {α : Type u} {β : Type v} (xs : List α) (ys : List α) (f : α → List β) :
list.bind (xs ++ ys) f = list.bind xs f ++ list.bind ys f :=
sorry
/- mem -/
@[simp] theorem mem_nil_iff {α : Type u} (a : α) : a ∈ [] ↔ False := iff.rfl
@[simp] theorem not_mem_nil {α : Type u} (a : α) : ¬a ∈ [] := iff.mp (mem_nil_iff a)
@[simp] theorem mem_cons_self {α : Type u} (a : α) (l : List α) : a ∈ a :: l := Or.inl rfl
@[simp] theorem mem_cons_iff {α : Type u} (a : α) (y : α) (l : List α) :
a ∈ y :: l ↔ a = y ∨ a ∈ l :=
iff.rfl
theorem mem_cons_eq {α : Type u} (a : α) (y : α) (l : List α) : a ∈ y :: l = (a = y ∨ a ∈ l) := rfl
theorem mem_cons_of_mem {α : Type u} (y : α) {a : α} {l : List α} : a ∈ l → a ∈ y :: l :=
fun (H : a ∈ l) => Or.inr H
theorem eq_or_mem_of_mem_cons {α : Type u} {a : α} {y : α} {l : List α} :
a ∈ y :: l → a = y ∨ a ∈ l :=
fun (h : a ∈ y :: l) => h
@[simp] theorem mem_append {α : Type u} {a : α} {s : List α} {t : List α} :
a ∈ s ++ t ↔ a ∈ s ∨ a ∈ t :=
sorry
theorem mem_append_eq {α : Type u} (a : α) (s : List α) (t : List α) :
a ∈ s ++ t = (a ∈ s ∨ a ∈ t) :=
propext mem_append
theorem mem_append_left {α : Type u} {a : α} {l₁ : List α} (l₂ : List α) (h : a ∈ l₁) :
a ∈ l₁ ++ l₂ :=
iff.mpr mem_append (Or.inl h)
theorem mem_append_right {α : Type u} {a : α} (l₁ : List α) {l₂ : List α} (h : a ∈ l₂) :
a ∈ l₁ ++ l₂ :=
iff.mpr mem_append (Or.inr h)
@[simp] theorem not_bex_nil {α : Type u} (p : α → Prop) : ¬∃ (x : α), ∃ (H : x ∈ []), p x := sorry
@[simp] theorem ball_nil {α : Type u} (p : α → Prop) (x : α) (H : x ∈ []) : p x := false.elim
@[simp] theorem bex_cons {α : Type u} (p : α → Prop) (a : α) (l : List α) :
(∃ (x : α), ∃ (H : x ∈ a :: l), p x) ↔ p a ∨ ∃ (x : α), ∃ (H : x ∈ l), p x :=
sorry
@[simp] theorem ball_cons {α : Type u} (p : α → Prop) (a : α) (l : List α) :
(∀ (x : α), x ∈ a :: l → p x) ↔ p a ∧ ∀ (x : α), x ∈ l → p x :=
sorry
/- list subset -/
protected def subset {α : Type u} (l₁ : List α) (l₂ : List α) := ∀ {a : α}, a ∈ l₁ → a ∈ l₂
protected instance has_subset {α : Type u} : has_subset (List α) := has_subset.mk list.subset
@[simp] theorem nil_subset {α : Type u} (l : List α) : [] ⊆ l :=
fun (b : α) (i : b ∈ []) => false.elim (iff.mp (mem_nil_iff b) i)
@[simp] theorem subset.refl {α : Type u} (l : List α) : l ⊆ l := fun (b : α) (i : b ∈ l) => i
theorem subset.trans {α : Type u} {l₁ : List α} {l₂ : List α} {l₃ : List α} (h₁ : l₁ ⊆ l₂)
(h₂ : l₂ ⊆ l₃) : l₁ ⊆ l₃ :=
fun (b : α) (i : b ∈ l₁) => h₂ (h₁ i)
@[simp] theorem subset_cons {α : Type u} (a : α) (l : List α) : l ⊆ a :: l :=
fun (b : α) (i : b ∈ l) => Or.inr i
theorem subset_of_cons_subset {α : Type u} {a : α} {l₁ : List α} {l₂ : List α} :
a :: l₁ ⊆ l₂ → l₁ ⊆ l₂ :=
fun (s : a :: l₁ ⊆ l₂) (b : α) (i : b ∈ l₁) => s (mem_cons_of_mem a i)
theorem cons_subset_cons {α : Type u} {l₁ : List α} {l₂ : List α} (a : α) (s : l₁ ⊆ l₂) :
a :: l₁ ⊆ a :: l₂ :=
fun (b : α) (hin : b ∈ a :: l₁) =>
or.elim (eq_or_mem_of_mem_cons hin) (fun (e : b = a) => Or.inl e)
fun (i : b ∈ l₁) => Or.inr (s i)
@[simp] theorem subset_append_left {α : Type u} (l₁ : List α) (l₂ : List α) : l₁ ⊆ l₁ ++ l₂ :=
fun (b : α) => mem_append_left l₂
@[simp] theorem subset_append_right {α : Type u} (l₁ : List α) (l₂ : List α) : l₂ ⊆ l₁ ++ l₂ :=
fun (b : α) => mem_append_right l₁
theorem subset_cons_of_subset {α : Type u} (a : α) {l₁ : List α} {l₂ : List α} :
l₁ ⊆ l₂ → l₁ ⊆ a :: l₂ :=
fun (s : l₁ ⊆ l₂) (a_1 : α) (i : a_1 ∈ l₁) => Or.inr (s i)
theorem eq_nil_of_length_eq_zero {α : Type u} {l : List α} : length l = 0 → l = [] := sorry
theorem ne_nil_of_length_eq_succ {α : Type u} {l : List α} {n : ℕ} :
length l = Nat.succ n → l ≠ [] :=
sorry
@[simp] theorem length_map₂ {α : Type u} {β : Type v} {γ : Type w} (f : α → β → γ) (l₁ : List α)
(l₂ : List β) : length (map₂ f l₁ l₂) = min (length l₁) (length l₂) :=
sorry
@[simp] theorem length_take {α : Type u} (i : ℕ) (l : List α) :
length (take i l) = min i (length l) :=
sorry
theorem length_take_le {α : Type u} (n : ℕ) (l : List α) : length (take n l) ≤ n := sorry
theorem length_remove_nth {α : Type u} (l : List α) (i : ℕ) :
i < length l → length (remove_nth l i) = length l - 1 :=
sorry
@[simp] theorem partition_eq_filter_filter {α : Type u} (p : α → Prop) [decidable_pred p]
(l : List α) : partition p l = (filter p l, filter (Not ∘ p) l) :=
sorry
/- sublists -/
inductive sublist {α : Type u} : List α → List α → Prop where
| slnil : sublist [] []
| cons : ∀ (l₁ l₂ : List α) (a : α), sublist l₁ l₂ → sublist l₁ (a :: l₂)
| cons2 : ∀ (l₁ l₂ : List α) (a : α), sublist l₁ l₂ → sublist (a :: l₁) (a :: l₂)
infixl:50 " <+ " => Mathlib.list.sublist
theorem length_le_of_sublist {α : Type u} {l₁ : List α} {l₂ : List α} :
l₁ <+ l₂ → length l₁ ≤ length l₂ :=
sorry
/- filter -/
@[simp] theorem filter_nil {α : Type u} (p : α → Prop) [h : decidable_pred p] : filter p [] = [] :=
rfl
@[simp] theorem filter_cons_of_pos {α : Type u} {p : α → Prop} [h : decidable_pred p] {a : α}
(l : List α) : p a → filter p (a :: l) = a :: filter p l :=
fun (pa : p a) => if_pos pa
@[simp] theorem filter_cons_of_neg {α : Type u} {p : α → Prop} [h : decidable_pred p] {a : α}
(l : List α) : ¬p a → filter p (a :: l) = filter p l :=
fun (pa : ¬p a) => if_neg pa
@[simp] theorem filter_append {α : Type u} {p : α → Prop} [h : decidable_pred p] (l₁ : List α)
(l₂ : List α) : filter p (l₁ ++ l₂) = filter p l₁ ++ filter p l₂ :=
sorry
@[simp] theorem filter_sublist {α : Type u} {p : α → Prop} [h : decidable_pred p] (l : List α) :
filter p l <+ l :=
sorry
/- map_accumr -/
-- This runs a function over a list returning the intermediate results and a
-- a final result.
def map_accumr {α : Type u} {β : Type v} {σ : Type w₂} (f : α → σ → σ × β) :
List α → σ → σ × List β :=
sorry
@[simp] theorem length_map_accumr {α : Type u} {β : Type v} {σ : Type w₂} (f : α → σ → σ × β)
(x : List α) (s : σ) : length (prod.snd (map_accumr f x s)) = length x :=
sorry
-- This runs a function over two lists returning the intermediate results and a
-- a final result.
def map_accumr₂ {α : Type u} {β : Type v} {φ : Type w₁} {σ : Type w₂} (f : α → β → σ → σ × φ) :
List α → List β → σ → σ × List φ :=
sorry
@[simp] theorem length_map_accumr₂ {α : Type u} {β : Type v} {φ : Type w₁} {σ : Type w₂}
(f : α → β → σ → σ × φ) (x : List α) (y : List β) (c : σ) :
length (prod.snd (map_accumr₂ f x y c)) = min (length x) (length y) :=
sorry
end Mathlib |
de7f2be35ccd979677c586c3a25676e240551584 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /test/ring_exp.lean | 58e10c7ce9e0b90181433c03af2ba4f79283add3 | [
"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 | 5,891 | lean | import tactic.ring_exp
universes u
section addition
/-!
### `addition` section
Test associativity and commutativity of `(+)`.
-/
example (a b : ℚ) : a = a := by ring_exp
example (a b : ℚ) : a + b = a + b := by ring_exp
example (a b : ℚ) : b + a = a + b := by ring_exp
example (a b : ℤ) : a + b + b = b + (a + b) := by ring_exp
example (a b c : ℕ) : a + b + b + (c + c) = c + (b + c) + (a + b) := by ring_exp
end addition
section numerals
/-!
### `numerals` section
Test that numerals behave like rational numbers.
-/
example (a : ℕ) : a + 5 + 5 = 0 + a + 10 := by ring_exp
example (a : ℤ) : a + 5 + 5 = 0 + a + 10 := by ring_exp
example (a : ℚ) : (1/2) * a + (1/2) * a = a := by ring_exp
end numerals
section multiplication
/-!
### `multiplication section`
Test that multiplication is associative and commutative.
Also test distributivity of `(+)` and `(*)`.
-/
example (a : ℕ) : 0 = a * 0 := by ring_exp_eq
example (a : ℕ) : a = a * 1 := by ring_exp
example (a : ℕ) : a + a = a * 2 := by ring_exp
example (a b : ℤ) : a * b = b * a := by ring_exp
example (a b : ℕ) : a * 4 * b + a = a * (4 * b + 1) := by ring_exp
end multiplication
section exponentiation
/-!
### `exponentiation` section
Test that exponentiation has the correct distributivity properties.
-/
example : 0 ^ 1 = 0 := by ring_exp
example : 0 ^ 2 = 0 := by ring_exp
example (a : ℕ) : a ^ 0 = 1 := by ring_exp
example (a : ℕ) : a ^ 1 = a := by ring_exp
example (a : ℕ) : a ^ 2 = a * a := by ring_exp
example (a b : ℕ) : a ^ b = a ^ b := by ring_exp
example (a b : ℕ) : a ^ (b + 1) = a * a ^ b := by ring_exp
example (n : ℕ) (a m : ℕ) : a * a^n * m = a^(n+1) * m := by ring_exp
example (n : ℕ) (a m : ℕ) : m * a^n * a = a^(n+1) * m := by ring_exp
example (n : ℕ) (a m : ℤ) : a * a^n * m = a^(n+1) * m := by ring_exp
example (n : ℕ) (m : ℤ) : 2 * 2^n * m = 2^(n+1) * m := by ring_exp
example (n : ℕ) (m : ℤ) : 2^(n+1) * m = 2 * 2^n * m := by ring_exp
example (n m : ℕ) (a : ℤ) : (a ^ n)^m = a^(n * m) := by ring_exp
example (n m : ℕ) (a : ℤ) : a^(n^0) = a^1 := by ring_exp
example (n : ℕ) : 0^(n + 1) = 0 := by ring_exp
example {α} [comm_ring α] (x : α) (k : ℕ) : x ^ (k + 2) = x * x * x^k := by ring_exp
example {α} [comm_ring α] (k : ℕ) (x y z : α) :
x * (z * (x - y)) + (x * (y * y ^ k) - y * (y * y ^ k)) = (z * x + y * y ^ k) * (x - y)
:= by ring_exp
-- We can represent a large exponent `n` more efficiently than just `n` multiplications:
example (a b : ℚ) : (a * b) ^ 1000000 = (b * a) ^ 1000000 := by ring_exp
end exponentiation
section power_of_sum
/-!
### `power_of_sum` section
Test that raising a sum to a power behaves like repeated multiplication,
if needed.
-/
example (a b : ℤ) : (a + b)^2 = a^2 + b^2 + a * b + b * a := by ring_exp
example (a b : ℤ) (n : ℕ) : (a + b)^(n + 2) = (a^2 + b^2 + a * b + b * a) * (a + b)^n := by ring_exp
end power_of_sum
section negation
/-!
### `negation` section
Test that negation and subtraction satisfy the expected properties,
also in semirings such as `ℕ`.
-/
example {α} [comm_ring α] (a : α) : a - a = 0 := by ring_exp_eq
example (a : ℤ) : a - a = 0 := by ring_exp
example (a : ℤ) : a + - a = 0 := by ring_exp
example (a : ℤ) : - a = (-1) * a := by ring_exp
example (a b : ℕ) : a - b + a + a = a - b + 2 * a := by ring_exp -- Here, (a - b) is treated as an atom.
example (n : ℕ) : n + 1 - 1 = n := by ring_exp! -- But we can force a bit of evaluation anyway.
end negation
constant f {α} : α → α
section complicated
/-!
### `complicated` section
Test that complicated, real-life expressions also get normalized.
-/
example {α : Type} [linear_ordered_field α] (x : α) :
2 * x + 1 * 1 - (2 * f (x + 1 / 2) + 2 * 1) + (1 * 1 - (2 * x - 2 * f (x + 1 / 2))) = 0
:= by ring_exp_eq
example {α : Type u} [linear_ordered_field α] (x : α) :
f (x + 1 / 2) ^ 1 * -2 + (f (x + 1 / 2) ^ 1 * 2 + 0) = 0
:= by ring_exp_eq
example (x y : ℕ) : x + id y = y + id x := by ring_exp!
-- Here, we check that `n - s` is not treated as `n + additive_inverse s`,
-- if `s` doesn't have an additive inverse.
example (B s n : ℕ) : B * (f s * ((n - s) * f (n - s - 1))) = B * (n - s) * (f s * f (n - s - 1)) :=
by ring_exp
-- This is a somewhat subtle case: `-c/b` is parsed as `(-c)/b`,
-- so we can't simply treat both sides of the division as atoms.
-- Instead, we follow the `ring` tactic in interpreting `-c / b` as `-c * b⁻¹`,
-- with only `b⁻¹` an atom.
example {α} [linear_ordered_field α] (a b c : α) : a*(-c/b)*(-c/b) = a*((c/b)*(c/b)) := by ring_exp
-- test that `field_simp` works fine with powers and `ring_exp`.
example (x y : ℚ) (n : ℕ) (hx : x ≠ 0) (hy : y ≠ 0) :
1/ (2/(x / y))^(2 * n) + y / y^(n+1) - (x/y)^n * (x/(2 * y))^n / 2 ^n = 1/y^n :=
begin
field_simp [hx, hy],
ring_exp
end
end complicated
section benchmark
/-!
### `benchmark` section
The `ring_exp` tactic shouldn't be too slow.
-/
-- This last example was copied from `data/polynomial.lean`, because it timed out.
-- After some optimization, it doesn't.
variables {α : Type} [comm_ring α]
def pow_sub_pow_factor (x y : α) : Π {i : ℕ},{z // x^i - y^i = z*(x - y)}
| 0 := ⟨0, by simp⟩
| 1 := ⟨1, by simp⟩
| (k+2) :=
begin
cases @pow_sub_pow_factor (k+1) with z hz,
existsi z*x + y^(k+1),
rw [_root_.pow_succ x, _root_.pow_succ y, ←sub_add_sub_cancel (x*x^(k+1)) (x*y^(k+1)),
←mul_sub x, hz],
ring_exp_eq
end
-- Another benchmark: bound the growth of the complexity somewhat.
example {α} [comm_semiring α] (x : α) : (x + 1) ^ 4 = (1 + x) ^ 4 := by try_for 5000 {ring_exp}
example {α} [comm_semiring α] (x : α) : (x + 1) ^ 6 = (1 + x) ^ 6 := by try_for 10000 {ring_exp}
example {α} [comm_semiring α] (x : α) : (x + 1) ^ 8 = (1 + x) ^ 8 := by try_for 15000 {ring_exp}
end benchmark
|
dab4291644e159a9508889bde2432cb2d26230a1 | 367134ba5a65885e863bdc4507601606690974c1 | /src/measure_theory/prod_group.lean | 8e95353cdb1ee9aab7f41b1fcf703eb60693572a | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 10,824 | lean | /-
Copyright (c) 2021 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import measure_theory.prod
import measure_theory.group
/-!
# Measure theory in the product of groups
In this file we show properties about measure theory in products of topological groups
and properties of iterated integrals in topological groups.
These lemmas show the uniqueness of left invariant measures on locally compact groups, up to
scaling. In this file we follow the proof and refer to the book *Measure Theory* by Paul Halmos.
The idea of the proof is to use the translation invariance of measures to prove `μ(F) = c * μ(E)`
for two sets `E` and `F`, where `c` is a constant that does not depend on `μ`. Let `e` and `f` be
the characteristic functions of `E` and `F`.
Assume that `μ` and `ν` are left-invariant measures. Then the map `(x, y) ↦ (y * x, x⁻¹)`
preserves the measure `μ.prod ν`, which means that
```
∫ x, ∫ y, h x y ∂ν ∂μ = ∫ x, ∫ y, h (y * x) x⁻¹ ∂ν ∂μ
```
If we apply this to `h x y := e x * f y⁻¹ / ν ((λ h, h * y⁻¹) ⁻¹' E)`, we can rewrite the RHS to
`μ(F)`, and the LHS to `c * μ(E)`, where `c = c(ν)` does not depend on `μ`.
Applying this to `μ` and to `ν` gives `μ (F) / μ (E) = ν (F) / ν (E)`, which is the uniqueness up to
scalar multiplication.
The proof in [Halmos] seems to contain an omission in §60 Th. A, see
`measure_theory.measure_lintegral_div_measure` and
https://math.stackexchange.com/questions/3974485/does-right-translation-preserve-finiteness-for-a-left-invariant-measure
-/
noncomputable theory
open topological_space set (hiding prod_eq) function
open_locale classical ennreal
namespace measure_theory
open measure
variables {G : Type*} [topological_space G] [measurable_space G] [second_countable_topology G]
[borel_space G] [group G] [topological_group G]
variables {μ ν : measure G} [sigma_finite ν] [sigma_finite μ]
/-- This condition is part of the definition of a measurable group in [Halmos, §59].
There, the map in this lemma is called `S`. -/
lemma map_prod_mul_eq (hν : is_mul_left_invariant ν) :
map (λ z : G × G, (z.1, z.1 * z.2)) (μ.prod ν) = μ.prod ν :=
begin
refine (prod_eq _).symm, intros s t hs ht,
simp_rw [map_apply (measurable_fst.prod_mk (measurable_fst.mul measurable_snd)) (hs.prod ht),
prod_apply ((measurable_fst.prod_mk (measurable_fst.mul measurable_snd)) (hs.prod ht)),
preimage_preimage],
conv_lhs { congr, skip, funext, rw [mk_preimage_prod_right_fn_eq_if ((*) x), measure_if] },
simp_rw [hν _ ht, lintegral_indicator _ hs, set_lintegral_const, mul_comm]
end
/-- The function we are mapping along is `SR` in [Halmos, §59],
where `S` is the map in `map_prod_mul_eq` and `R` is `prod.swap`. -/
lemma map_prod_mul_eq_swap (hμ : is_mul_left_invariant μ) :
map (λ z : G × G, (z.2, z.2 * z.1)) (μ.prod ν) = ν.prod μ :=
begin
rw [← prod_swap],
simp_rw [map_map (measurable_snd.prod_mk (measurable_snd.mul measurable_fst)) measurable_swap],
exact map_prod_mul_eq hμ
end
/-- The function we are mapping along is `S⁻¹` in [Halmos, §59],
where `S` is the map in `map_prod_mul_eq`. -/
lemma map_prod_inv_mul_eq (hν : is_mul_left_invariant ν) :
map (λ z : G × G, (z.1, z.1⁻¹ * z.2)) (μ.prod ν) = μ.prod ν :=
(homeomorph.shear_mul_right G).to_measurable_equiv.map_apply_eq_iff_map_symm_apply_eq.mp $
map_prod_mul_eq hν
/-- The function we are mapping along is `S⁻¹R` in [Halmos, §59],
where `S` is the map in `map_prod_mul_eq` and `R` is `prod.swap`. -/
lemma map_prod_inv_mul_eq_swap (hμ : is_mul_left_invariant μ) :
map (λ z : G × G, (z.2, z.2⁻¹ * z.1)) (μ.prod ν) = ν.prod μ :=
begin
rw [← prod_swap],
simp_rw
[map_map (measurable_snd.prod_mk $ measurable_snd.inv.mul measurable_fst) measurable_swap],
exact map_prod_inv_mul_eq hμ
end
/-- The function we are mapping along is `S⁻¹RSR` in [Halmos, §59],
where `S` is the map in `map_prod_mul_eq` and `R` is `prod.swap`. -/
lemma map_prod_mul_inv_eq (hμ : is_mul_left_invariant μ) (hν : is_mul_left_invariant ν) :
map (λ z : G × G, (z.2 * z.1, z.1⁻¹)) (μ.prod ν) = μ.prod ν :=
begin
let S := (homeomorph.shear_mul_right G).to_measurable_equiv,
suffices : map ((λ z : G × G, (z.2, z.2⁻¹ * z.1)) ∘ (λ z : G × G, (z.2, z.2 * z.1))) (μ.prod ν) =
μ.prod ν,
{ convert this, ext1 ⟨x, y⟩, simp },
simp_rw [← map_map (measurable_snd.prod_mk (measurable_snd.inv.mul measurable_fst))
(measurable_snd.prod_mk (measurable_snd.mul measurable_fst)), map_prod_mul_eq_swap hμ,
map_prod_inv_mul_eq_swap hν]
end
lemma measure_null_of_measure_inv_null (hμ : is_mul_left_invariant μ)
{E : set G} (hE : measurable_set E) (h2E : μ ((λ x, x⁻¹) ⁻¹' E) = 0) : μ E = 0 :=
begin
have hf : measurable (λ z : G × G, (z.2 * z.1, z.1⁻¹)) :=
(measurable_snd.mul measurable_fst).prod_mk measurable_fst.inv,
suffices : map (λ z : G × G, (z.2 * z.1, z.1⁻¹)) (μ.prod μ) (E.prod E) = 0,
{ simpa only [map_prod_mul_inv_eq hμ hμ, prod_prod hE hE, mul_eq_zero, or_self] using this },
simp_rw [map_apply hf (hE.prod hE), prod_apply_symm (hf (hE.prod hE)), preimage_preimage,
mk_preimage_prod],
convert lintegral_zero, ext1 x, refine measure_mono_null (inter_subset_right _ _) h2E
end
lemma measure_inv_null (hμ : is_mul_left_invariant μ) {E : set G} (hE : measurable_set E) :
μ ((λ x, x⁻¹) ⁻¹' E) = 0 ↔ μ E = 0 :=
begin
refine ⟨measure_null_of_measure_inv_null hμ hE, _⟩,
intro h2E,
apply measure_null_of_measure_inv_null hμ (measurable_inv hE),
convert h2E using 2,
exact set.inv_inv
end
lemma measurable_measure_mul_right {E : set G} (hE : measurable_set E) :
measurable (λ x, μ ((λ y, y * x) ⁻¹' E)) :=
begin
suffices :
measurable (λ y, μ ((λ x, (x, y)) ⁻¹' ((λ z : G × G, (1, z.1 * z.2)) ⁻¹' set.prod univ E))),
{ convert this, ext1 x, congr' 1 with y : 1, simp },
apply measurable_measure_prod_mk_right,
exact measurable_const.prod_mk (measurable_fst.mul measurable_snd) (measurable_set.univ.prod hE)
end
lemma lintegral_lintegral_mul_inv (hμ : is_mul_left_invariant μ) (hν : is_mul_left_invariant ν)
(f : G → G → ℝ≥0∞) (hf : ae_measurable (uncurry f) (μ.prod ν)) :
∫⁻ x, ∫⁻ y, f (y * x) x⁻¹ ∂ν ∂μ = ∫⁻ x, ∫⁻ y, f x y ∂ν ∂μ :=
begin
have h : measurable (λ z : G × G, (z.2 * z.1, z.1⁻¹)) :=
(measurable_snd.mul measurable_fst).prod_mk measurable_fst.inv,
have h2f : ae_measurable (uncurry $ λ x y, f (y * x) x⁻¹) (μ.prod ν),
{ apply hf.comp_measurable' h (map_prod_mul_inv_eq hμ hν).absolutely_continuous },
simp_rw [lintegral_lintegral h2f, lintegral_lintegral hf],
conv_rhs { rw [← map_prod_mul_inv_eq hμ hν] },
symmetry,
exact lintegral_map' (hf.mono' (map_prod_mul_inv_eq hμ hν).absolutely_continuous) h,
end
lemma measure_mul_right_null (hμ : is_mul_left_invariant μ) {E : set G} (hE : measurable_set E)
(y : G) : μ ((λ x, x * y) ⁻¹' E) = 0 ↔ μ E = 0 :=
begin
rw [← measure_inv_null hμ hE, ← hμ y⁻¹ (measurable_inv hE),
← measure_inv_null hμ (measurable_mul_right y hE)],
convert iff.rfl using 3, ext x, simp,
end
lemma measure_mul_right_ne_zero (hμ : is_mul_left_invariant μ) {E : set G} (hE : measurable_set E)
(h2E : μ E ≠ 0) (y : G) : μ ((λ x, x * y) ⁻¹' E) ≠ 0 :=
(not_iff_not_of_iff (measure_mul_right_null hμ hE y)).mpr h2E
/-- A technical lemma relating two different measures. This is basically [Halmos, §60 Th. A].
Note that if `f` is the characteristic function of a measurable set `F` this states that
`μ F = c * μ E` for a constant `c` that does not depend on `μ`.
There seems to be a gap in the last step of the proof in [Halmos].
In the last line, the equality `g(x⁻¹)ν(Ex⁻¹) = f(x)` holds if we can prove that
`0 < ν(Ex⁻¹) < ∞`. The first inequality follows from §59, Th. D, but I couldn't find the second
inequality. For this reason, we use a compact `E` instead of a measurable `E` as in [Halmos], and
additionally assume that `ν` is a regular measure (we only need that it is finite on compact
sets). -/
lemma measure_lintegral_div_measure [t2_space G] (hμ : is_mul_left_invariant μ)
(hν : is_mul_left_invariant ν) (h2ν : regular ν) {E : set G} (hE : is_compact E) (h2E : ν E ≠ 0)
(f : G → ℝ≥0∞) (hf : measurable f) :
μ E * ∫⁻ y, f y⁻¹ / ν ((λ h, h * y⁻¹) ⁻¹' E) ∂ν = ∫⁻ x, f x ∂μ :=
begin
have Em := hE.measurable_set,
symmetry,
set g := λ y, f y⁻¹ / ν ((λ h, h * y⁻¹) ⁻¹' E),
have hg : measurable g := (hf.comp measurable_inv).ennreal_div
((measurable_measure_mul_right Em).comp measurable_inv),
rw [← set_lintegral_one, ← lintegral_indicator _ Em,
← lintegral_lintegral_mul (measurable_const.indicator Em).ae_measurable hg.ae_measurable,
← lintegral_lintegral_mul_inv hμ hν],
swap, { exact (((measurable_const.indicator Em).comp measurable_fst).ennreal_mul
(hg.comp measurable_snd)).ae_measurable },
have mE : ∀ x : G, measurable (λ y, ((λ z, z * x) ⁻¹' E).indicator (λ z, (1 : ℝ≥0∞)) y) :=
λ x, measurable_const.indicator (measurable_mul_right _ Em),
have : ∀ x y, E.indicator (λ (z : G), (1 : ℝ≥0∞)) (y * x) =
((λ z, z * x) ⁻¹' E).indicator (λ (b : G), 1) y,
{ intros x y, symmetry, convert indicator_comp_right (λ y, y * x), ext1 z, simp },
have h3E : ∀ y, ν ((λ x, x * y) ⁻¹' E) ≠ ∞ :=
λ y, ennreal.lt_top_iff_ne_top.mp (h2ν.lt_top_of_is_compact $
(homeomorph.mul_right _).compact_preimage.mpr hE),
simp_rw [this, lintegral_mul_const _ (mE _), lintegral_indicator _ (measurable_mul_right _ Em),
set_lintegral_one, g, inv_inv,
ennreal.mul_div_cancel' (measure_mul_right_ne_zero hν Em h2E _) (h3E _)]
end
/-- This is roughly the uniqueness (up to a scalar) of left invariant Borel measures on a second
countable locally compact group. The uniqueness of Haar measure is proven from this in
`measure_theory.measure.haar_measure_unique` -/
lemma measure_mul_measure_eq [t2_space G] (hμ : is_mul_left_invariant μ)
(hν : is_mul_left_invariant ν) (h2ν : regular ν) {E F : set G}
(hE : is_compact E) (hF : measurable_set F) (h2E : ν E ≠ 0) : μ E * ν F = ν E * μ F :=
begin
have h1 := measure_lintegral_div_measure hν hν h2ν hE h2E (F.indicator (λ x, 1))
(measurable_const.indicator hF),
have h2 := measure_lintegral_div_measure hμ hν h2ν hE h2E (F.indicator (λ x, 1))
(measurable_const.indicator hF),
rw [lintegral_indicator _ hF, set_lintegral_one] at h1 h2,
rw [← h1, mul_left_comm, h2],
end
end measure_theory
|
040bbbf846543c31abaff75641e8f6137004b59a | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/category_theory/isomorphism_classes.lean | 7b3cfb31ec706e6e76dde28536f3c538c63db724 | [
"Apache-2.0"
] | permissive | gebner/mathlib | eab0150cc4f79ec45d2016a8c21750244a2e7ff0 | cc6a6edc397c55118df62831e23bfbd6e6c6b4ab | refs/heads/master | 1,625,574,853,976 | 1,586,712,827,000 | 1,586,712,827,000 | 99,101,412 | 1 | 0 | Apache-2.0 | 1,586,716,389,000 | 1,501,667,958,000 | Lean | UTF-8 | Lean | false | false | 1,652 | lean | /-
Copyright (c) 2019 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import category_theory.category.Cat category_theory.groupoid data.quot
/-!
# Objects of a category up to an isomorphism
`is_isomorphic X Y := nonempty (X ≅ Y)` is an equivalence relation on the objects of a category.
The quotient with respect to this relation defines a functor from our category to `Type`.
-/
universes v u
namespace category_theory
section category
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
/-- An object `X` is isomorphic to an object `Y`, if `X ≅ Y` is not empty. -/
def is_isomorphic : C → C → Prop := λ X Y, nonempty (X ≅ Y)
variable (C)
/-- `is_isomorphic` defines a setoid. -/
def is_isomorphic_setoid : setoid C :=
{ r := is_isomorphic,
iseqv := ⟨λ X, ⟨iso.refl X⟩, λ X Y ⟨α⟩, ⟨α.symm⟩, λ X Y Z ⟨α⟩ ⟨β⟩, ⟨α.trans β⟩⟩ }
end category
/--
The functor that sends each category to the quotient space of its objects up to an isomorphism.
-/
def isomorphism_classes : Cat.{v u} ⥤ Type u :=
{ obj := λ C, quotient (is_isomorphic_setoid C.α),
map := λ C D F, quot.map F.obj $ λ X Y ⟨f⟩, ⟨F.map_iso f⟩ }
lemma groupoid.is_isomorphic_iff_nonempty_hom {C : Type u} [groupoid.{v} C] {X Y : C} :
is_isomorphic X Y ↔ nonempty (X ⟶ Y) :=
(groupoid.iso_equiv_hom X Y).nonempty_iff_nonempty
-- PROJECT: define `skeletal`, and show every category is equivalent to a skeletal category,
-- using the axiom of choice to pick a representative of every isomorphism class.
end category_theory
|
2fdbf648f606351b7be48ac27f2c7ccbdcdf7a80 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/category_theory/natural_isomorphism.lean | 903b483e8e2b3ad787557b9c102b84ab4b030340 | [
"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 | 5,504 | lean | -- Copyright (c) 2017 Scott Morrison. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Authors: Tim Baumann, Stephen Morgan, Scott Morrison
import category_theory.functor_category
import category_theory.isomorphism
import tactic.simpa
open category_theory
universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace category_theory.nat_iso
variables {C : Sort u₁} [𝒞 : category.{v₁} C] {D : Sort u₂} [𝒟 : category.{v₂} D]
include 𝒞 𝒟
def app {F G : C ⥤ D} (α : F ≅ G) (X : C) : F.obj X ≅ G.obj X :=
{ hom := α.hom.app X,
inv := α.inv.app X,
hom_inv_id' := begin rw [← functor.category.comp_app, iso.hom_inv_id], refl, end,
inv_hom_id' := begin rw [← functor.category.comp_app, iso.inv_hom_id], refl, end }
@[simp] lemma comp_app {F G H : C ⥤ D} (α : F ≅ G) (β : G ≅ H) (X : C) :
app (α ≪≫ β) X = app α X ≪≫ app β X := rfl
@[simp] lemma app_hom {F G : C ⥤ D} (α : F ≅ G) (X : C) : (app α X).hom = α.hom.app X := rfl
@[simp] lemma app_inv {F G : C ⥤ D} (α : F ≅ G) (X : C) : (app α X).inv = α.inv.app X := rfl
@[simp] lemma hom_inv_id_app {F G : C ⥤ D} (α : F ≅ G) (X : C) : α.hom.app X ≫ α.inv.app X = 𝟙 (F.obj X) :=
congr_fun (congr_arg nat_trans.app α.hom_inv_id) X
@[simp] lemma inv_hom_id_app {F G : C ⥤ D} (α : F ≅ G) (X : C) : α.inv.app X ≫ α.hom.app X = 𝟙 (G.obj X) :=
congr_fun (congr_arg nat_trans.app α.inv_hom_id) X
variables {F G : C ⥤ D}
instance hom_app_is_iso (α : F ≅ G) (X : C) : is_iso (α.hom.app X) :=
{ inv := α.inv.app X,
hom_inv_id' := begin rw [←functor.category.comp_app, iso.hom_inv_id, ←functor.category.id_app] end,
inv_hom_id' := begin rw [←functor.category.comp_app, iso.inv_hom_id, ←functor.category.id_app] end }
instance inv_app_is_iso (α : F ≅ G) (X : C) : is_iso (α.inv.app X) :=
{ inv := α.hom.app X,
hom_inv_id' := begin rw [←functor.category.comp_app, iso.inv_hom_id, ←functor.category.id_app] end,
inv_hom_id' := begin rw [←functor.category.comp_app, iso.hom_inv_id, ←functor.category.id_app] end }
@[simp] lemma hom_app_inv_app_id (α : F ≅ G) (X : C) : α.hom.app X ≫ α.inv.app X = 𝟙 _ :=
begin
rw ←functor.category.comp_app,
simp,
end
@[simp] lemma inv_app_hom_app_id (α : F ≅ G) (X : C) : α.inv.app X ≫ α.hom.app X = 𝟙 _ :=
begin
rw ←functor.category.comp_app,
simp,
end
variables {X Y : C}
@[simp] lemma naturality_1 (α : F ≅ G) (f : X ⟶ Y) :
(α.inv.app X) ≫ (F.map f) ≫ (α.hom.app Y) = G.map f :=
begin erw [nat_trans.naturality, ←category.assoc, is_iso.hom_inv_id, category.id_comp] end
@[simp] lemma naturality_2 (α : F ≅ G) (f : X ⟶ Y) :
(α.hom.app X) ≫ (G.map f) ≫ (α.inv.app Y) = F.map f :=
begin erw [nat_trans.naturality, ←category.assoc, is_iso.hom_inv_id, category.id_comp] end
instance is_iso_of_is_iso_app (α : F ⟶ G) [∀ X : C, is_iso (α.app X)] : is_iso α :=
{ inv :=
{ app := λ X, inv (α.app X),
naturality' := λ X Y f,
by simpa using congr_arg (λ f, inv (α.app X) ≫ (f ≫ inv (α.app Y))) (α.naturality f).symm } }
def of_components (app : ∀ X : C, (F.obj X) ≅ (G.obj X))
(naturality : ∀ {X Y : C} (f : X ⟶ Y), (F.map f) ≫ ((app Y).hom) = ((app X).hom) ≫ (G.map f)) :
F ≅ G :=
as_iso { app := λ X, (app X).hom }
@[simp] def of_components.app (app' : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality) (X) :
app (of_components app' naturality) X = app' X :=
by tidy
@[simp] def of_components.hom_app (app : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality) (X) :
(of_components app naturality).hom.app X = (app X).hom := rfl
@[simp] def of_components.inv_app (app : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality) (X) :
(of_components app naturality).inv.app X = (app X).inv := rfl
end category_theory.nat_iso
open category_theory
namespace category_theory.functor
section
variables {C : Sort u₁} [𝒞 : category.{v₁} C]
{D : Sort u₂} [𝒟 : category.{v₂} D]
include 𝒞 𝒟
@[simp] protected def id_comp (F : C ⥤ D) : functor.id C ⋙ F ≅ F :=
{ hom := { app := λ X, 𝟙 (F.obj X) },
inv := { app := λ X, 𝟙 (F.obj X) } }
@[simp] protected def comp_id (F : C ⥤ D) : F ⋙ functor.id D ≅ F :=
{ hom := { app := λ X, 𝟙 (F.obj X) },
inv := { app := λ X, 𝟙 (F.obj X) } }
universes v₄ u₄
variables {A : Sort u₃} [𝒜 : category.{v₃} A]
{B : Sort u₄} [ℬ : category.{v₄} B]
include 𝒜 ℬ
variables (F : A ⥤ B) (G : B ⥤ C) (H : C ⥤ D)
@[simp] protected def assoc : (F ⋙ G) ⋙ H ≅ F ⋙ (G ⋙ H ):=
{ hom := { app := λ X, 𝟙 (H.obj (G.obj (F.obj X))) },
inv := { app := λ X, 𝟙 (H.obj (G.obj (F.obj X))) } }
-- When it's time to define monoidal categories and 2-categories,
-- we'll need to add lemmas relating these natural isomorphisms,
-- in particular the pentagon for the associator.
end
section
variables {C : Type u₁} [𝒞 : category.{v₁} C]
include 𝒞
def ulift_down_up : ulift_down.{v₁} C ⋙ ulift_up C ≅ functor.id (ulift.{u₂} C) :=
{ hom := { app := λ X, @category_struct.id (ulift.{u₂} C) _ X },
inv := { app := λ X, @category_struct.id (ulift.{u₂} C) _ X } }
def ulift_up_down : ulift_up.{v₁} C ⋙ ulift_down C ≅ functor.id C :=
{ hom := { app := λ X, 𝟙 X },
inv := { app := λ X, 𝟙 X } }
end
end category_theory.functor
|
60e1f40658a5ddde32fb59858d7797a663449449 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/tactic/solve_by_elim.lean | 64b25d398cc8ab542e3765f5d16e7d05dc9a3356 | [
"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 | 15,187 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Scott Morrison
-/
import tactic.core
/-!
# solve_by_elim
A depth-first search backwards reasoner.
`solve_by_elim` takes a list of lemmas, and repeating tries to `apply` these against
the goals, recursively acting on any generated subgoals.
It accepts a variety of configuration options described below, enabling
* backtracking across multiple goals,
* pruning the search tree, and
* invoking other tactics before or after trying to apply lemmas.
At present it has no "premise selection", and simply tries the supplied lemmas in order
at each step of the search.
-/
namespace tactic
namespace solve_by_elim
/--
`mk_assumption_set` builds a collection of lemmas for use in
the backtracking search in `solve_by_elim`.
* By default, it includes all local hypotheses, along with `rfl`, `trivial`, `congr_fun` and
`congr_arg`.
* The flag `no_dflt` removes these.
* The argument `hs` is a list of `simp_arg_type`s,
and can be used to add, or remove, lemmas or expressions from the set.
* The argument `attr : list name` adds all lemmas tagged with one of a specified list of attributes.
`mk_assumption_set` returns not a `list expr`, but a `list (tactic expr) × tactic (list expr)`.
There are two separate problems that need to be solved.
### Relevant local hypotheses
`solve_by_elim*` works with multiple goals,
and we need to use separate sets of local hypotheses for each goal.
The second component of the returned value provides these local hypotheses.
(Essentially using `local_context`, along with some filtering to remove hypotheses
that have been explicitly removed via `only` or `[-h]`.)
### Stuck metavariables
Lemmas with implicit arguments would be filled in with metavariables if we created the
`expr` objects immediately, so instead we return thunks that generate the expressions
on demand. This is the first component, with type `list (tactic expr)`.
As an example, we have `def rfl : ∀ {α : Sort u} {a : α}, a = a`, which on elaboration will become
`@rfl ?m_1 ?m_2`.
Because `solve_by_elim` works by repeated application of lemmas against subgoals,
the first time such a lemma is successfully applied,
those metavariables will be unified, and thereafter have fixed values.
This would make it impossible to apply the lemma
a second time with different values of the metavariables.
See https://github.com/leanprover-community/mathlib/issues/2269
As an optimisation, after we build the list of `tactic expr`s, we actually run them, and replace any
that do not in fact produce metavariables with a simple `return` tactic.
-/
meta def mk_assumption_set (no_dflt : bool) (hs : list simp_arg_type) (attr : list name) :
tactic (list (tactic expr) × tactic (list expr)) :=
-- We lock the tactic state so that any spurious goals generated during
-- elaboration of pre-expressions are discarded
lock_tactic_state $
do
-- `hs` are expressions specified explicitly,
-- `hex` are exceptions (specified via `solve_by_elim [-h]`) referring to local hypotheses,
-- `gex` are the other exceptions
(hs, gex, hex, all_hyps) ← decode_simp_arg_list hs,
-- Recall, per the discussion above, we produce `tactic expr` thunks rather than actual `expr`s.
-- Note that while we evaluate these thunks on two occasions below while preparing the list,
-- this is a one-time cost during `mk_assumption_set`, rather than a cost proportional to the
-- length of the search `solve_by_elim` executes.
let hs := hs.map (λ h, i_to_expr_for_apply h),
l ← attr.mmap $ λ a, attribute.get_instances a,
let l := l.join,
let m := l.map (λ h, mk_const h),
-- In order to remove the expressions we need to evaluate the thunks.
hs ← (hs ++ m).mfilter $ λ h, (do h ← h, return $ expr.const_name h ∉ gex),
let hs := if no_dflt then hs else
([`rfl, `trivial, `congr_fun, `congr_arg].map (λ n, (mk_const n))) ++ hs,
let locals : tactic (list expr) := if ¬ no_dflt ∨ all_hyps then do
ctx ← local_context,
-- Remove local exceptions specified in `hex`:
return $ ctx.filter (λ h : expr, h.local_uniq_name ∉ hex)
else return [],
-- Finally, run all of the tactics: any that return an expression without metavariables can safely
-- be replaced by a `return` tactic.
hs ← hs.mmap (λ h : tactic expr, do
e ← h,
if e.has_meta_var then return h else return (return e)),
return (hs, locals)
/--
Configuration options for `solve_by_elim`.
* `accept : list expr → tactic unit` determines whether the current branch should be explored.
At each step, before the lemmas are applied,
`accept` is passed the proof terms for the original goals,
as reported by `get_goals` when `solve_by_elim` started.
These proof terms may be metavariables (if no progress has been made on that goal)
or may contain metavariables at some leaf nodes
(if the goal has been partially solved by previous `apply` steps).
If the `accept` tactic fails `solve_by_elim` aborts searching this branch and backtracks.
By default `accept := λ _, skip` always succeeds.
(There is an example usage in `tests/solve_by_elim.lean`.)
* `pre_apply : tactic unit` specifies an additional tactic to run before each round of `apply`.
* `discharger : tactic unit` specifies an additional tactic to apply on subgoals
for which no lemma applies.
If that tactic succeeds, `solve_by_elim` will continue applying lemmas on resulting goals.
-/
meta structure basic_opt extends apply_any_opt :=
(accept : list expr → tactic unit := λ _, skip)
(pre_apply : tactic unit := skip)
(discharger : tactic unit := failed)
(max_depth : ℕ := 3)
declare_trace solve_by_elim -- trace attempted lemmas
/--
A helper function for trace messages, prepending '....' depending on the current search depth.
-/
meta def solve_by_elim_trace (n : ℕ) (f : format) : tactic unit :=
trace_if_enabled `solve_by_elim
(format!"[solve_by_elim {(list.repeat '.' (n+1)).as_string} " ++ f ++ "]")
/-- A helper function to generate trace messages on successful applications. -/
meta def on_success (g : format) (n : ℕ) (e : expr) : tactic unit :=
do
pp ← pp e,
solve_by_elim_trace n (format!"✅ `{pp}` solves `⊢ {g}`")
/-- A helper function to generate trace messages on unsuccessful applications. -/
meta def on_failure (g : format) (n : ℕ) : tactic unit :=
solve_by_elim_trace n (format!"❌ failed to solve `⊢ {g}`")
/--
A helper function to generate the tactic that print trace messages.
This function exists to ensure the target is pretty printed only as necessary.
-/
meta def trace_hooks (n : ℕ) : tactic ((expr → tactic unit) × tactic unit) :=
if is_trace_enabled_for `solve_by_elim then
do
g ← target >>= pp,
return (on_success g n, on_failure g n)
else
return (λ _, skip, skip)
/--
The internal implementation of `solve_by_elim`, with a limiting counter.
-/
meta def solve_by_elim_aux (opt : basic_opt) (original_goals : list expr)
(lemmas : list (tactic expr)) (ctx : tactic (list expr)) :
ℕ → tactic unit
| n := do
-- First, check that progress so far is `accept`able.
lock_tactic_state (original_goals.mmap instantiate_mvars >>= opt.accept),
-- Then check if we've finished.
(done >> solve_by_elim_trace (opt.max_depth - n) "success!") <|> (do
-- Otherwise, if there's more time left,
(guard (n > 0) <|>
solve_by_elim_trace opt.max_depth "🛑 aborting, hit depth limit" >> failed),
-- run the `pre_apply` tactic, then
opt.pre_apply,
-- try either applying a lemma and recursing,
(on_success, on_failure) ← trace_hooks (opt.max_depth - n),
ctx_lemmas ← ctx,
(apply_any_thunk (lemmas ++ (ctx_lemmas.map return)) opt.to_apply_any_opt
(solve_by_elim_aux (n-1))
on_success on_failure) <|>
-- or if that doesn't work, run the discharger and recurse.
(opt.discharger >> solve_by_elim_aux (n-1)))
/--
Arguments for `solve_by_elim`:
* By default `solve_by_elim` operates only on the first goal,
but with `backtrack_all_goals := true`, it operates on all goals at once,
backtracking across goals as needed,
and only succeeds if it discharges all goals.
* `lemmas` specifies the list of lemmas to use in the backtracking search.
If `none`, `solve_by_elim` uses the local hypotheses,
along with `rfl`, `trivial`, `congr_arg`, and `congr_fun`.
* `lemma_thunks` provides the lemmas as a list of `tactic expr`,
which are used to regenerate the `expr` objects to avoid binding metavariables.
It should not usually be specified by the user.
(If both `lemmas` and `lemma_thunks` are specified, only `lemma_thunks` is used.)
* `ctx_thunk` is for internal use only: it returns the local hypotheses which will be used.
* `max_depth` bounds the depth of the search.
-/
meta structure opt extends basic_opt :=
(backtrack_all_goals : bool := ff)
(lemmas : option (list expr) := none)
(lemma_thunks : option (list (tactic expr)) := lemmas.map (λ l, l.map return))
(ctx_thunk : tactic (list expr) := local_context)
/--
If no lemmas have been specified, generate the default set
(local hypotheses, along with `rfl`, `trivial`, `congr_arg`, and `congr_fun`).
-/
meta def opt.get_lemma_thunks (opt : opt) : tactic (list (tactic expr) × tactic (list expr)) :=
match opt.lemma_thunks with
| none := mk_assumption_set ff [] []
| some lemma_thunks := return (lemma_thunks, opt.ctx_thunk)
end
end solve_by_elim
open solve_by_elim
/--
`solve_by_elim` repeatedly tries `apply`ing a lemma
from the list of assumptions (passed via the `opt` argument),
recursively operating on any generated subgoals, backtracking as necessary.
`solve_by_elim` succeeds only if it discharges the goal.
(By default, `solve_by_elim` focuses on the first goal, and only attempts to solve that.
With the option `backtrack_all_goals := tt`,
it attempts to solve all goals, and only succeeds if it does so.
With `backtrack_all_goals := tt`, `solve_by_elim` will backtrack a solution it has found for
one goal if it then can't discharge other goals.)
If passed an empty list of assumptions, `solve_by_elim` builds a default set
as per the interactive tactic, using the `local_context` along with
`rfl`, `trivial`, `congr_arg`, and `congr_fun`.
To pass a particular list of assumptions, use the `lemmas` field
in the configuration argument. This expects an
`option (list expr)`. In certain situations it may be necessary to instead use the
`lemma_thunks` field, which expects a `option (list (tactic expr))`.
This allows for regenerating metavariables
for each application, which might otherwise get stuck.
See also the simpler tactic `apply_rules`, which does not perform backtracking.
-/
meta def solve_by_elim (opt : opt := { }) : tactic unit :=
do
tactic.fail_if_no_goals,
(lemmas, ctx_lemmas) ← opt.get_lemma_thunks,
(if opt.backtrack_all_goals then id else focus1) $ (do
gs ← get_goals,
solve_by_elim_aux opt.to_basic_opt gs lemmas ctx_lemmas opt.max_depth <|>
fail ("`solve_by_elim` failed.\n" ++
"Try `solve_by_elim { max_depth := N }` for `N > " ++ (to_string opt.max_depth) ++ "`\n" ++
"or use `set_option trace.solve_by_elim true` to view the search."))
setup_tactic_parser
namespace interactive
/--
`apply_assumption` looks for an assumption of the form `... → ∀ _, ... → head`
where `head` matches the current goal.
If this fails, `apply_assumption` will call `symmetry` and try again.
If this also fails, `apply_assumption` will call `exfalso` and try again,
so that if there is an assumption of the form `P → ¬ Q`, the new tactic state
will have two goals, `P` and `Q`.
Optional arguments:
- `lemmas`: a list of expressions to apply, instead of the local constants
- `tac`: a tactic to run on each subgoal after applying an assumption; if
this tactic fails, the corresponding assumption will be rejected and
the next one will be attempted.
-/
meta def apply_assumption
(lemmas : option (list expr) := none)
(opt : apply_any_opt := {})
(tac : tactic unit := skip) : tactic unit :=
do
lemmas ← match lemmas with
| none := local_context
| some lemmas := return lemmas
end,
tactic.apply_any lemmas opt tac
add_tactic_doc
{ name := "apply_assumption",
category := doc_category.tactic,
decl_names := [`tactic.interactive.apply_assumption],
tags := ["context management", "lemma application"] }
/--
`solve_by_elim` calls `apply` on the main goal to find an assumption whose head matches
and then repeatedly calls `apply` on the generated subgoals until no subgoals remain,
performing at most `max_depth` recursive steps.
`solve_by_elim` discharges the current goal or fails.
`solve_by_elim` performs back-tracking if subgoals can not be solved.
By default, the assumptions passed to `apply` are the local context, `rfl`, `trivial`,
`congr_fun` and `congr_arg`.
The assumptions can be modified with similar syntax as for `simp`:
* `solve_by_elim [h₁, h₂, ..., hᵣ]` also applies the named lemmas.
* `solve_by_elim with attr₁ ... attrᵣ` also applies all lemmas tagged with the specified attributes.
* `solve_by_elim only [h₁, h₂, ..., hᵣ]` does not include the local context,
`rfl`, `trivial`, `congr_fun`, or `congr_arg` unless they are explicitly included.
* `solve_by_elim [-id_1, ... -id_n]` uses the default assumptions, removing the specified ones.
`solve_by_elim*` tries to solve all goals together, using backtracking if a solution for one goal
makes other goals impossible.
optional arguments passed via a configuration argument as `solve_by_elim { ... }`
- max_depth: number of attempts at discharging generated sub-goals
- discharger: a subsidiary tactic to try at each step when no lemmas apply
(e.g. `cc` may be helpful).
- pre_apply: a subsidiary tactic to run at each step before applying lemmas (e.g. `intros`).
- accept: a subsidiary tactic `list expr → tactic unit` that at each step,
before any lemmas are applied, is passed the original proof terms
as reported by `get_goals` when `solve_by_elim` started
(but which may by now have been partially solved by previous `apply` steps).
If the `accept` tactic fails,
`solve_by_elim` will abort searching the current branch and backtrack.
This may be used to filter results, either at every step of the search,
or filtering complete results
(by testing for the absence of metavariables, and then the filtering condition).
-/
meta def solve_by_elim (all_goals : parse $ (tk "*")?) (no_dflt : parse only_flag)
(hs : parse simp_arg_list) (attr_names : parse with_ident_list) (opt : solve_by_elim.opt := { }) :
tactic unit :=
do (lemma_thunks, ctx_thunk) ← mk_assumption_set no_dflt hs attr_names,
tactic.solve_by_elim
{ backtrack_all_goals := all_goals.is_some ∨ opt.backtrack_all_goals,
lemma_thunks := some lemma_thunks,
ctx_thunk := ctx_thunk,
..opt }
add_tactic_doc
{ name := "solve_by_elim",
category := doc_category.tactic,
decl_names := [`tactic.interactive.solve_by_elim],
tags := ["search"] }
end interactive
end tactic
|
4d8dad8ac6a773ec6947148443fffd01aaec33a2 | 969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb | /src/group_theory/submonoid/operations.lean | d8db5c0757c84ee7cfa0a6f802195b3f9b7f314c | [
"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 | 24,351 | lean | /-
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard,
Amelia Livingston, Yury Kudryashov
-/
import group_theory.submonoid.basic
import data.equiv.mul_add
import algebra.group.prod
import algebra.group.inj_surj
/-!
# Operations on `submonoid`s
In this file we define various operations on `submonoid`s and `monoid_hom`s.
## Main definitions
### Conversion between multiplicative and additive definitions
* `submonoid.to_add_submonoid`, `submonoid.of_add_submonoid`, `add_submonoid.to_submonoid`,
`add_submonoid.of_submonoid`: convert between multiplicative and additive submonoids of `M`,
`multiplicative M`, and `additive M`.
* `submonoid.add_submonoid_equiv`: equivalence between `submonoid M`
and `add_submonoid (additive M)`.
### (Commutative) monoid structure on a submonoid
* `submonoid.to_monoid`, `submonoid.to_comm_monoid`: a submonoid inherits a (commutative) monoid
structure.
### Operations on submonoids
* `submonoid.comap`: preimage of a submonoid under a monoid homomorphism as a submonoid of the
domain;
* `submonoid.map`: image of a submonoid under a monoid homomorphism as a submonoid of the codomain;
* `submonoid.prod`: product of two submonoids `s : submonoid M` and `t : submonoid N` as a submonoid
of `M × N`;
### Monoid homomorphisms between submonoid
* `submonoid.subtype`: embedding of a submonoid into the ambient monoid.
* `submonoid.inclusion`: given two submonoids `S`, `T` such that `S ≤ T`, `S.inclusion T` is the
inclusion of `S` into `T` as a monoid homomorphism;
* `mul_equiv.submonoid_congr`: converts a proof of `S = T` into a monoid isomorphism between `S`
and `T`.
* `submonoid.prod_equiv`: monoid isomorphism between `s.prod t` and `s × t`;
### Operations on `monoid_hom`s
* `monoid_hom.mrange`: range of a monoid homomorphism as a submonoid of the codomain;
* `monoid_hom.mrestrict`: restrict a monoid homomorphism to a submonoid;
* `monoid_hom.cod_mrestrict`: restrict the codomain of a monoid homomorphism to a submonoid;
* `monoid_hom.mrange_restrict`: restrict a monoid homomorphism to its range;
## Tags
submonoid, range, product, map, comap
-/
variables {M N P : Type*} [monoid M] [monoid N] [monoid P] (S : submonoid M)
/-!
### Conversion to/from `additive`/`multiplicative`
-/
/-- Map from submonoids of monoid `M` to `add_submonoid`s of `additive M`. -/
def submonoid.to_add_submonoid {M : Type*} [monoid M] (S : submonoid M) :
add_submonoid (additive M) :=
{ carrier := S.carrier,
zero_mem' := S.one_mem',
add_mem' := S.mul_mem' }
/-- Map from `add_submonoid`s of `additive M` to submonoids of `M`. -/
def submonoid.of_add_submonoid {M : Type*} [monoid M] (S : add_submonoid (additive M)) :
submonoid M :=
{ carrier := S.carrier,
one_mem' := S.zero_mem',
mul_mem' := S.add_mem' }
/-- Map from `add_submonoid`s of `add_monoid M` to submonoids of `multiplicative M`. -/
def add_submonoid.to_submonoid {M : Type*} [add_monoid M] (S : add_submonoid M) :
submonoid (multiplicative M) :=
{ carrier := S.carrier,
one_mem' := S.zero_mem',
mul_mem' := S.add_mem' }
/-- Map from submonoids of `multiplicative M` to `add_submonoid`s of `add_monoid M`. -/
def add_submonoid.of_submonoid {M : Type*} [add_monoid M] (S : submonoid (multiplicative M)) :
add_submonoid M :=
{ carrier := S.carrier,
zero_mem' := S.one_mem',
add_mem' := S.mul_mem' }
/-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/
def submonoid.add_submonoid_equiv (M : Type*) [monoid M] :
submonoid M ≃ add_submonoid (additive M) :=
{ to_fun := submonoid.to_add_submonoid,
inv_fun := submonoid.of_add_submonoid,
left_inv := λ x, by cases x; refl,
right_inv := λ x, by cases x; refl }
namespace submonoid
open set
/-!
### `comap` and `map`
-/
/-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/
@[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an
`add_submonoid`."]
def comap (f : M →* N) (S : submonoid N) : submonoid M :=
{ carrier := (f ⁻¹' S),
one_mem' := show f 1 ∈ S, by rw f.map_one; exact S.one_mem,
mul_mem' := λ a b ha hb,
show f (a * b) ∈ S, by rw f.map_mul; exact S.mul_mem ha hb }
@[simp, to_additive]
lemma coe_comap (S : submonoid N) (f : M →* N) : (S.comap f : set M) = f ⁻¹' S := rfl
@[simp, to_additive]
lemma mem_comap {S : submonoid N} {f : M →* N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl
@[to_additive]
lemma comap_comap (S : submonoid P) (g : N →* P) (f : M →* N) :
(S.comap g).comap f = S.comap (g.comp f) :=
rfl
@[simp, to_additive]
lemma comap_id (S : submonoid P) : S.comap (monoid_hom.id _) = S :=
ext (by simp)
/-- The image of a submonoid along a monoid homomorphism is a submonoid. -/
@[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is
an `add_submonoid`."]
def map (f : M →* N) (S : submonoid M) : submonoid N :=
{ carrier := (f '' S),
one_mem' := ⟨1, S.one_mem, f.map_one⟩,
mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy,
by rw f.map_mul; refl⟩ end }
@[simp, to_additive]
lemma coe_map (f : M →* N) (S : submonoid M) :
(S.map f : set N) = f '' S := rfl
@[simp, to_additive]
lemma mem_map {f : M →* N} {S : submonoid M} {y : N} :
y ∈ S.map f ↔ ∃ x ∈ S, f x = y :=
mem_image_iff_bex
@[to_additive]
lemma mem_map_of_mem (f : M →* N) (x : S) : f x ∈ S.map f :=
mem_image_of_mem f x.2
@[to_additive]
lemma map_map (g : N →* P) (f : M →* N) : (S.map f).map g = S.map (g.comp f) :=
ext' $ image_image _ _ _
@[to_additive]
lemma map_le_iff_le_comap {f : M →* N} {S : submonoid M} {T : submonoid N} :
S.map f ≤ T ↔ S ≤ T.comap f :=
image_subset_iff
@[to_additive]
lemma gc_map_comap (f : M →* N) : galois_connection (map f) (comap f) :=
λ S T, map_le_iff_le_comap
@[to_additive]
lemma map_le_of_le_comap {T : submonoid N} {f : M →* N} : S ≤ T.comap f → S.map f ≤ T :=
(gc_map_comap f).l_le
@[to_additive]
lemma le_comap_of_map_le {T : submonoid N} {f : M →* N} : S.map f ≤ T → S ≤ T.comap f :=
(gc_map_comap f).le_u
@[to_additive]
lemma le_comap_map {f : M →* N} : S ≤ (S.map f).comap f :=
(gc_map_comap f).le_u_l _
@[to_additive]
lemma map_comap_le {S : submonoid N} {f : M →* N} : (S.comap f).map f ≤ S :=
(gc_map_comap f).l_u_le _
@[to_additive]
lemma monotone_map {f : M →* N} : monotone (map f) :=
(gc_map_comap f).monotone_l
@[to_additive]
lemma monotone_comap {f : M →* N} : monotone (comap f) :=
(gc_map_comap f).monotone_u
@[simp, to_additive]
lemma map_comap_map {f : M →* N} : ((S.map f).comap f).map f = S.map f :=
congr_fun ((gc_map_comap f).l_u_l_eq_l) _
@[simp, to_additive]
lemma comap_map_comap {S : submonoid N} {f : M →* N} : ((S.comap f).map f).comap f = S.comap f :=
congr_fun ((gc_map_comap f).u_l_u_eq_u) _
@[to_additive]
lemma map_sup (S T : submonoid M) (f : M →* N) : (S ⊔ T).map f = S.map f ⊔ T.map f :=
(gc_map_comap f).l_sup
@[to_additive]
lemma map_supr {ι : Sort*} (f : M →* N) (s : ι → submonoid M) :
(supr s).map f = ⨆ i, (s i).map f :=
(gc_map_comap f).l_supr
@[to_additive]
lemma comap_inf (S T : submonoid N) (f : M →* N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f :=
(gc_map_comap f).u_inf
@[to_additive]
lemma comap_infi {ι : Sort*} (f : M →* N) (s : ι → submonoid N) :
(infi s).comap f = ⨅ i, (s i).comap f :=
(gc_map_comap f).u_infi
@[simp, to_additive] lemma map_bot (f : M →* N) : (⊥ : submonoid M).map f = ⊥ :=
(gc_map_comap f).l_bot
@[simp, to_additive] lemma comap_top (f : M →* N) : (⊤ : submonoid N).comap f = ⊤ :=
(gc_map_comap f).u_top
@[simp, to_additive] lemma map_id (S : submonoid M) : S.map (monoid_hom.id M) = S :=
ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩)
section galois_coinsertion
variables {ι : Type*} {f : M →* N} (hf : function.injective f)
include hf
/-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/
def gci_map_comap : galois_coinsertion (map f) (comap f) :=
(gc_map_comap f).to_galois_coinsertion
(λ S x, by simp [mem_comap, mem_map, hf.eq_iff])
lemma comap_map_eq_of_injective (S : submonoid M) : (S.map f).comap f = S :=
(gci_map_comap hf).u_l_eq _
lemma comap_surjective_of_injective : function.surjective (comap f) :=
(gci_map_comap hf).u_surjective
lemma map_injective_of_injective : function.injective (map f) :=
(gci_map_comap hf).l_injective
lemma comap_inf_map_of_injective (S T : submonoid M) : (S.map f ⊓ T.map f).comap f = S ⊓ T :=
(gci_map_comap hf).u_inf_l _ _
lemma comap_infi_map_of_injective (S : ι → submonoid M) : (⨅ i, (S i).map f).comap f = infi S :=
(gci_map_comap hf).u_infi_l _
lemma comap_sup_map_of_injective (S T : submonoid M) : (S.map f ⊔ T.map f).comap f = S ⊔ T :=
(gci_map_comap hf).u_sup_l _ _
lemma comap_supr_map_of_injective (S : ι → submonoid M) : (⨆ i, (S i).map f).comap f = supr S :=
(gci_map_comap hf).u_supr_l _
lemma map_le_map_iff_of_injective {S T : submonoid M} : S.map f ≤ T.map f ↔ S ≤ T :=
(gci_map_comap hf).l_le_l_iff
lemma map_strict_mono_of_injective : strict_mono (map f) :=
(gci_map_comap hf).strict_mono_l
end galois_coinsertion
section galois_insertion
variables {ι : Type*} {f : M →* N} (hf : function.surjective f)
include hf
/-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/
def gi_map_comap : galois_insertion (map f) (comap f) :=
(gc_map_comap f).to_galois_insertion
(λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩)
lemma map_comap_eq_of_surjective (S : submonoid N) : (S.comap f).map f = S :=
(gi_map_comap hf).l_u_eq _
lemma map_surjective_of_surjective : function.surjective (map f) :=
(gi_map_comap hf).l_surjective
lemma comap_injective_of_surjective : function.injective (comap f) :=
(gi_map_comap hf).u_injective
lemma map_inf_comap_of_surjective (S T : submonoid N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T :=
(gi_map_comap hf).l_inf_u _ _
lemma map_infi_comap_of_surjective (S : ι → submonoid N) : (⨅ i, (S i).comap f).map f = infi S :=
(gi_map_comap hf).l_infi_u _
lemma map_sup_comap_of_surjective (S T : submonoid N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T :=
(gi_map_comap hf).l_sup_u _ _
lemma map_supr_comap_of_surjective (S : ι → submonoid N) : (⨆ i, (S i).comap f).map f = supr S :=
(gi_map_comap hf).l_supr_u _
lemma comap_le_comap_iff_of_surjective {S T : submonoid N} : S.comap f ≤ T.comap f ↔ S ≤ T :=
(gi_map_comap hf).u_le_u_iff
lemma comap_strict_mono_of_surjective : strict_mono (comap f) :=
(gi_map_comap hf).strict_mono_u
end galois_insertion
/-- A submonoid of a monoid inherits a multiplication. -/
@[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."]
instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩
/-- A submonoid of a monoid inherits a 1. -/
@[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."]
instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩
@[simp, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl
@[simp, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl
attribute [norm_cast] coe_mul coe_one
attribute [norm_cast] add_submonoid.coe_add add_submonoid.coe_zero
/-- A submonoid of a monoid inherits a monoid structure. -/
@[to_additive "An `add_submonoid` of an `add_monoid` inherits an `add_monoid`
structure."]
instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S :=
S.coe_injective.monoid coe rfl (λ _ _, rfl)
/-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/
@[to_additive "An `add_submonoid` of an `add_comm_monoid` is
an `add_comm_monoid`."]
instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S :=
S.coe_injective.comm_monoid coe rfl (λ _ _, rfl)
/-- A submonoid of an `ordered_comm_monoid` is an `ordered_comm_monoid`. -/
@[to_additive "An `add_submonoid` of an `ordered_add_comm_monoid` is
an `ordered_add_comm_monoid`."]
instance to_ordered_comm_monoid {M} [ordered_comm_monoid M] (S : submonoid M) :
ordered_comm_monoid S :=
S.coe_injective.ordered_comm_monoid coe rfl (λ _ _, rfl)
/-- A submonoid of a `linear_ordered_comm_monoid` is a `linear_ordered_comm_monoid`. -/
@[to_additive "An `add_submonoid` of a `linear_ordered_add_comm_monoid` is
a `linear_ordered_add_comm_monoid`."]
instance to_linear_ordered_comm_monoid {M} [linear_ordered_comm_monoid M] (S : submonoid M) :
linear_ordered_comm_monoid S :=
S.coe_injective.linear_ordered_comm_monoid coe rfl (λ _ _, rfl)
/-- A submonoid of an `ordered_cancel_comm_monoid` is an `ordered_cancel_comm_monoid`. -/
@[to_additive "An `add_submonoid` of an `ordered_cancel_add_comm_monoid` is
an `ordered_cancel_add_comm_monoid`."]
instance to_ordered_cancel_comm_monoid {M} [ordered_cancel_comm_monoid M] (S : submonoid M) :
ordered_cancel_comm_monoid S :=
S.coe_injective.ordered_cancel_comm_monoid coe rfl (λ _ _, rfl)
/-- A submonoid of a `linear_ordered_cancel_comm_monoid` is a `linear_ordered_cancel_comm_monoid`.
-/
@[to_additive "An `add_submonoid` of a `linear_ordered_cancel_add_comm_monoid` is
a `linear_ordered_cancel_add_comm_monoid`."]
instance to_linear_ordered_cancel_comm_monoid {M} [linear_ordered_cancel_comm_monoid M]
(S : submonoid M) : linear_ordered_cancel_comm_monoid S :=
S.coe_injective.linear_ordered_cancel_comm_monoid coe rfl (λ _ _, rfl)
/-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/
@[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."]
def subtype : S →* M := ⟨coe, rfl, λ _ _, rfl⟩
@[simp, to_additive] theorem coe_subtype : ⇑S.subtype = coe := rfl
/-- An induction principle on elements of the type `submonoid.closure s`.
If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p`
holds for all elements of the closure of `s`.
The difference with `submonoid.closure_induction` is that this acts on the subtype.
-/
@[to_additive "An induction principle on elements of the type `add_submonoid.closure s`.
If `p` holds for `0` and all elements of `s`, and is preserved under addition, then `p`
holds for all elements of the closure of `s`.
The difference with `add_submonoid.closure_induction` is that this acts on the subtype."]
lemma closure_induction' (s : set M) {p : closure s → Prop}
(Hs : ∀ x (h : x ∈ s), p ⟨x, subset_closure h⟩)
(H1 : p 1)
(Hmul : ∀ x y, p x → p y → p (x * y))
(x : closure s) :
p x :=
subtype.rec_on x $ λ x hx, begin
refine exists.elim _ (λ (hx : x ∈ closure s) (hc : p ⟨x, hx⟩), hc),
exact closure_induction hx
(λ x hx, ⟨subset_closure hx, Hs x hx⟩)
⟨one_mem _, H1⟩
(λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy,
⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩),
end
attribute [elab_as_eliminator] submonoid.closure_induction' add_submonoid.closure_induction'
/-- Given `submonoid`s `s`, `t` of monoids `M`, `N` respectively, `s × t` as a submonoid
of `M × N`. -/
@[to_additive prod "Given `add_submonoid`s `s`, `t` of `add_monoid`s `A`, `B` respectively, `s × t`
as an `add_submonoid` of `A × B`."]
def prod (s : submonoid M) (t : submonoid N) : submonoid (M × N) :=
{ carrier := (s : set M).prod t,
one_mem' := ⟨s.one_mem, t.one_mem⟩,
mul_mem' := λ p q hp hq, ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ }
@[to_additive coe_prod]
lemma coe_prod (s : submonoid M) (t : submonoid N) :
(s.prod t : set (M × N)) = (s : set M).prod (t : set N) :=
rfl
@[to_additive mem_prod]
lemma mem_prod {s : submonoid M} {t : submonoid N} {p : M × N} :
p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl
@[to_additive prod_mono]
lemma prod_mono {s₁ s₂ : submonoid M} {t₁ t₂ : submonoid N} (hs : s₁ ≤ s₂) (ht : t₁ ≤ t₂) :
s₁.prod t₁ ≤ s₂.prod t₂ :=
set.prod_mono hs ht
@[to_additive prod_top]
lemma prod_top (s : submonoid M) :
s.prod (⊤ : submonoid N) = s.comap (monoid_hom.fst M N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst]
@[to_additive top_prod]
lemma top_prod (s : submonoid N) :
(⊤ : submonoid M).prod s = s.comap (monoid_hom.snd M N) :=
ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd]
@[simp, to_additive top_prod_top]
lemma top_prod_top : (⊤ : submonoid M).prod (⊤ : submonoid N) = ⊤ :=
(top_prod _).trans $ comap_top _
@[to_additive] lemma bot_prod_bot : (⊥ : submonoid M).prod (⊥ : submonoid N) = ⊥ :=
ext' $ by simp [coe_prod, prod.one_eq_mk]
/-- The product of submonoids is isomorphic to their product as monoids. -/
@[to_additive prod_equiv "The product of additive submonoids is isomorphic to their product
as additive monoids"]
def prod_equiv (s : submonoid M) (t : submonoid N) : s.prod t ≃* s × t :=
{ map_mul' := λ x y, rfl, .. equiv.set.prod ↑s ↑t }
open monoid_hom
@[to_additive]
lemma map_inl (s : submonoid M) : s.map (inl M N) = s.prod ⊥ :=
ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨hx, set.mem_singleton 1⟩,
λ ⟨hps, hp1⟩, ⟨p.1, hps, prod.ext rfl $ (set.eq_of_mem_singleton hp1).symm⟩⟩
@[to_additive]
lemma map_inr (s : submonoid N) : s.map (inr M N) = prod ⊥ s :=
ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨set.mem_singleton 1, hx⟩,
λ ⟨hp1, hps⟩, ⟨p.2, hps, prod.ext (set.eq_of_mem_singleton hp1).symm rfl⟩⟩
@[simp, to_additive prod_bot_sup_bot_prod]
lemma prod_bot_sup_bot_prod (s : submonoid M) (t : submonoid N) :
(s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t :=
le_antisymm (sup_le (prod_mono (le_refl s) bot_le) (prod_mono bot_le (le_refl t))) $
assume p hp, prod.fst_mul_snd p ▸ mul_mem _
((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set.mem_singleton 1⟩)
((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set.mem_singleton 1, hp.2⟩)
end submonoid
namespace monoid_hom
open submonoid
/-- The range of a monoid homomorphism is a submonoid. -/
@[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."]
def mrange (f : M →* N) : submonoid N := (⊤ : submonoid M).map f
@[simp, to_additive] lemma coe_mrange (f : M →* N) :
(f.mrange : set N) = set.range f :=
set.image_univ
@[simp, to_additive] lemma mem_mrange {f : M →* N} {y : N} :
y ∈ f.mrange ↔ ∃ x, f x = y :=
by simp [mrange]
@[to_additive]
lemma map_mrange (g : N →* P) (f : M →* N) : f.mrange.map g = (g.comp f).mrange :=
(⊤ : submonoid M).map_map g f
@[to_additive]
lemma mrange_top_iff_surjective {N} [monoid N] {f : M →* N} :
f.mrange = (⊤ : submonoid N) ↔ function.surjective f :=
submonoid.ext'_iff.trans $ iff.trans (by rw [coe_mrange, coe_top]) set.range_iff_surjective
/-- The range of a surjective monoid hom is the whole of the codomain. -/
@[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."]
lemma mrange_top_of_surjective {N} [monoid N] (f : M →* N) (hf : function.surjective f) :
f.mrange = (⊤ : submonoid N) :=
mrange_top_iff_surjective.2 hf
@[to_additive]
lemma mrange_eq_map (f : M →* N) : f.mrange = map f ⊤ := rfl
@[to_additive]
lemma mclosure_preimage_le (f : M →* N) (s : set N) :
closure (f ⁻¹' s) ≤ (closure s).comap f :=
closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx
/-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated
by the image of the set. -/
@[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals
the `add_submonoid` generated by the image of the set."]
lemma map_mclosure (f : M →* N) (s : set M) :
(closure s).map f = closure (f '' s) :=
le_antisymm
(map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _)
(mclosure_preimage_le _ _))
(closure_le.2 $ set.image_subset _ subset_closure)
/-- Restriction of a monoid hom to a submonoid of the domain. -/
@[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."]
def mrestrict {N : Type*} [monoid N] (f : M →* N) (S : submonoid M) : S →* N := f.comp S.subtype
@[simp, to_additive]
lemma mrestrict_apply {N : Type*} [monoid N] (f : M →* N) (x : S) : f.mrestrict S x = f x := rfl
/-- Restriction of a monoid hom to a submonoid of the codomain. -/
@[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain."]
def cod_mrestrict (f : M →* N) (S : submonoid N) (h : ∀ x, f x ∈ S) : M →* S :=
{ to_fun := λ n, ⟨f n, h n⟩,
map_one' := subtype.eq f.map_one,
map_mul' := λ x y, subtype.eq (f.map_mul x y) }
/-- Restriction of a monoid hom to its range interpreted as a submonoid. -/
@[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."]
def mrange_restrict {N} [monoid N] (f : M →* N) : M →* f.mrange :=
f.cod_mrestrict f.mrange $ λ x, ⟨x, submonoid.mem_top x, rfl⟩
@[simp, to_additive]
lemma coe_mrange_restrict {N} [monoid N] (f : M →* N) (x : M) :
(f.mrange_restrict x : N) = f x :=
rfl
end monoid_hom
namespace submonoid
open monoid_hom
@[to_additive]
lemma mrange_inl : (inl M N).mrange = prod ⊤ ⊥ := map_inl ⊤
@[to_additive]
lemma mrange_inr : (inr M N).mrange = prod ⊥ ⊤ := map_inr ⊤
@[to_additive]
lemma mrange_inl' : (inl M N).mrange = comap (snd M N) ⊥ := mrange_inl.trans (top_prod _)
@[to_additive]
lemma mrange_inr' : (inr M N).mrange = comap (fst M N) ⊥ := mrange_inr.trans (prod_top _)
@[simp, to_additive]
lemma mrange_fst : (fst M N).mrange = ⊤ :=
(fst M N).mrange_top_of_surjective $ @prod.fst_surjective _ _ ⟨1⟩
@[simp, to_additive]
lemma mrange_snd : (snd M N).mrange = ⊤ :=
(snd M N).mrange_top_of_surjective $ @prod.snd_surjective _ _ ⟨1⟩
@[simp, to_additive]
lemma mrange_inl_sup_mrange_inr : (inl M N).mrange ⊔ (inr M N).mrange = ⊤ :=
by simp only [mrange_inl, mrange_inr, prod_bot_sup_bot_prod, top_prod_top]
/-- The monoid hom associated to an inclusion of submonoids. -/
@[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."]
def inclusion {S T : submonoid M} (h : S ≤ T) : S →* T :=
S.subtype.cod_mrestrict _ (λ x, h x.2)
@[simp, to_additive]
lemma range_subtype (s : submonoid M) : s.subtype.mrange = s :=
ext' $ (coe_mrange _).trans $ subtype.range_coe
@[to_additive] lemma eq_top_iff' : S = ⊤ ↔ ∀ x : M, x ∈ S :=
eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩
@[to_additive] lemma eq_bot_iff_forall : S = ⊥ ↔ ∀ x ∈ S, x = (1 : M) :=
begin
split,
{ intros h x x_in,
rwa [h, mem_bot] at x_in },
{ intros h,
ext x,
rw mem_bot,
exact ⟨h x, by { rintros rfl, exact S.one_mem }⟩ },
end
@[to_additive] lemma nontrivial_iff_exists_ne_one (S : submonoid M) :
nontrivial S ↔ ∃ x ∈ S, x ≠ (1:M) :=
begin
split,
{ introI h,
rcases exists_ne (1 : S) with ⟨⟨h, h_in⟩, h_ne⟩,
use [h, h_in],
intro hyp,
apply h_ne,
simpa [hyp] },
{ rintros ⟨x, x_in, hx⟩,
apply nontrivial_of_ne (⟨x, x_in⟩ : S) 1,
intro hyp,
apply hx,
simpa [has_one.one] using hyp },
end
/-- A submonoid is either the trivial submonoid or nontrivial. -/
@[to_additive] lemma bot_or_nontrivial (S : submonoid M) : S = ⊥ ∨ nontrivial S :=
begin
classical,
by_cases h : ∀ x ∈ S, x = (1 : M),
{ left,
exact S.eq_bot_iff_forall.mpr h },
{ right,
push_neg at h,
simpa [nontrivial_iff_exists_ne_one] using h },
end
/-- A submonoid is either the trivial submonoid or contains a nonzero element. -/
@[to_additive] lemma bot_or_exists_ne_one (S : submonoid M) : S = ⊥ ∨ ∃ x ∈ S, x ≠ (1:M) :=
begin
convert S.bot_or_nontrivial,
rw nontrivial_iff_exists_ne_one
end
end submonoid
namespace mul_equiv
variables {S} {T : submonoid M}
/-- Makes the identity isomorphism from a proof that two submonoids of a multiplicative
monoid are equal. -/
@[to_additive "Makes the identity additive isomorphism from a proof two
submonoids of an additive monoid are equal."]
def submonoid_congr (h : S = T) : S ≃* T :=
{ map_mul' := λ _ _, rfl, ..equiv.set_congr $ submonoid.ext'_iff.1 h }
end mul_equiv
|
671a477c6422e6e7d0a3d4f355ff209f951b4cfb | ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5 | /tests/compiler/phashmap2.lean | b9270fff9741b11a3dc6f72d16cb43fe1ea938ff | [
"Apache-2.0"
] | permissive | dupuisf/lean4 | d082d13b01243e1de29ae680eefb476961221eef | 6a39c65bd28eb0e28c3870188f348c8914502718 | refs/heads/master | 1,676,948,755,391 | 1,610,665,114,000 | 1,610,665,114,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,584 | lean | #lang lean4
import Std.Data.PersistentHashMap
import Lean.Data.Format
open Lean Std Std.PersistentHashMap
abbrev Map := PersistentHashMap Nat Nat
partial def formatMap : Node Nat Nat → Format
| Node.collision keys vals _ => Format.sbracket $
keys.size.fold
(fun i fmt =>
let k := keys.get! i;
let v := vals.get! i;
let p := if i > 0 then fmt ++ format "," ++ Format.line else fmt;
p ++ "c@" ++ Format.paren (format k ++ " => " ++ format v))
Format.nil
| Node.entries entries => Format.sbracket $
entries.size.fold
(fun i fmt =>
let entry := entries.get! i;
let p := if i > 0 then fmt ++ format "," ++ Format.line else fmt;
p ++
match entry with
| Entry.null => "<null>"
| Entry.ref node => formatMap node
| Entry.entry k v => Format.paren (format k ++ " => " ++ format v))
Format.nil
def main : IO Unit :=
do
let a : Array Nat := [1, 2, 3].toArray;
IO.println (a.indexOf? 2);
let m : Map := PersistentHashMap.empty;
let m := m.insert 1 1;
let m := m.insert 33 2;
let m := m.insert 65 3;
-- IO.println (formatMap m.root);
IO.println m.stats;
let m := m.erase 33;
IO.println (m.find? 1);
IO.println (m.find? 33);
IO.println (m.find? 65);
IO.println m.stats;
let m := m.erase 1;
IO.println (m.find? 1);
IO.println (m.find? 33);
IO.println (m.find? 65);
IO.println m.stats;
let m := m.erase 1;
IO.println (m.find? 1);
IO.println (m.find? 33);
IO.println (m.find? 65);
let m := m.erase 65;
IO.println (m.find? 1);
IO.println (m.find? 33);
IO.println (m.find? 65);
IO.println m.stats
|
6848a9cbef35869e705f71e70e4cbbf3091d75d0 | abd85493667895c57a7507870867b28124b3998f | /src/analysis/specific_limits.lean | fc7e13bb9fcb51310869035293137875001b753e | [
"Apache-2.0"
] | permissive | pechersky/mathlib | d56eef16bddb0bfc8bc552b05b7270aff5944393 | f1df14c2214ee114c9738e733efd5de174deb95d | refs/heads/master | 1,666,714,392,571 | 1,591,747,567,000 | 1,591,747,567,000 | 270,557,274 | 0 | 0 | Apache-2.0 | 1,591,597,975,000 | 1,591,597,974,000 | null | UTF-8 | Lean | false | false | 23,921 | 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
A collection of specific limit computations.
-/
import analysis.normed_space.basic
import algebra.geom_sum
import topology.instances.ennreal
noncomputable theory
open_locale classical topological_space
open classical function filter finset metric
open_locale big_operators
variables {α : Type*} {β : Type*} {ι : Type*}
lemma tendsto_norm_at_top_at_top : tendsto (norm : ℝ → ℝ) at_top at_top :=
tendsto_abs_at_top_at_top
/-- If a function tends to infinity along a filter, then this function multiplied by a positive
constant (on the left) also tends to infinity. The archimedean assumption is convenient to get a
statement that works on `ℕ`, `ℤ` and `ℝ`, although not necessary (a version in ordered fields is
given in `tendsto_at_top_mul_left'`). -/
lemma tendsto_at_top_mul_left [decidable_linear_ordered_semiring α] [archimedean α]
{l : filter β} {r : α} (hr : 0 < r) {f : β → α} (hf : tendsto f l at_top) :
tendsto (λx, r * f x) l at_top :=
begin
apply (tendsto_at_top _ _).2 (λb, _),
obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1 : α) ≤ n • r := archimedean.arch 1 hr,
have hn' : 1 ≤ r * n, by rwa nsmul_eq_mul' at hn,
filter_upwards [(tendsto_at_top _ _).1 hf (n * max b 0)],
assume x hx,
calc b ≤ 1 * max b 0 : by { rw [one_mul], exact le_max_left _ _ }
... ≤ (r * n) * max b 0 : mul_le_mul_of_nonneg_right hn' (le_max_right _ _)
... = r * (n * max b 0) : by rw [mul_assoc]
... ≤ r * f x : mul_le_mul_of_nonneg_left hx (le_of_lt hr)
end
/-- If a function tends to infinity along a filter, then this function multiplied by a positive
constant (on the right) also tends to infinity. The archimedean assumption is convenient to get a
statement that works on `ℕ`, `ℤ` and `ℝ`, although not necessary (a version in ordered fields is
given in `tendsto_at_top_mul_right'`). -/
lemma tendsto_at_top_mul_right [decidable_linear_ordered_semiring α] [archimedean α]
{l : filter β} {r : α} (hr : 0 < r) {f : β → α} (hf : tendsto f l at_top) :
tendsto (λx, f x * r) l at_top :=
begin
apply (tendsto_at_top _ _).2 (λb, _),
obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1 : α) ≤ n • r := archimedean.arch 1 hr,
have hn' : 1 ≤ (n : α) * r, by rwa nsmul_eq_mul at hn,
filter_upwards [(tendsto_at_top _ _).1 hf (max b 0 * n)],
assume x hx,
calc b ≤ max b 0 * 1 : by { rw [mul_one], exact le_max_left _ _ }
... ≤ max b 0 * (n * r) : mul_le_mul_of_nonneg_left hn' (le_max_right _ _)
... = (max b 0 * n) * r : by rw [mul_assoc]
... ≤ f x * r : mul_le_mul_of_nonneg_right hx (le_of_lt hr)
end
/-- If a function tends to infinity along a filter, then this function multiplied by a positive
constant (on the left) also tends to infinity. For a version working in `ℕ` or `ℤ`, use
`tendsto_at_top_mul_left` instead. -/
lemma tendsto_at_top_mul_left' [linear_ordered_field α]
{l : filter β} {r : α} (hr : 0 < r) {f : β → α} (hf : tendsto f l at_top) :
tendsto (λx, r * f x) l at_top :=
begin
apply (tendsto_at_top _ _).2 (λb, _),
filter_upwards [(tendsto_at_top _ _).1 hf (b/r)],
assume x hx,
simpa [div_le_iff' hr] using hx
end
/-- If a function tends to infinity along a filter, then this function multiplied by a positive
constant (on the right) also tends to infinity. For a version working in `ℕ` or `ℤ`, use
`tendsto_at_top_mul_right` instead. -/
lemma tendsto_at_top_mul_right' [linear_ordered_field α]
{l : filter β} {r : α} (hr : 0 < r) {f : β → α} (hf : tendsto f l at_top) :
tendsto (λx, f x * r) l at_top :=
by simpa [mul_comm] using tendsto_at_top_mul_left' hr hf
/-- If a function tends to infinity along a filter, then this function divided by a positive
constant also tends to infinity. -/
lemma tendsto_at_top_div [linear_ordered_field α]
{l : filter β} {r : α} (hr : 0 < r) {f : β → α} (hf : tendsto f l at_top) :
tendsto (λx, f x / r) l at_top :=
tendsto_at_top_mul_right' (inv_pos.2 hr) hf
/-- The function `x ↦ x⁻¹` tends to `+∞` on the right of `0`. -/
lemma tendsto_inv_zero_at_top [discrete_linear_ordered_field α] [topological_space α]
[order_topology α] : tendsto (λx:α, x⁻¹) (nhds_within (0 : α) (set.Ioi 0)) at_top :=
begin
apply (tendsto_at_top _ _).2 (λb, _),
refine mem_nhds_within_Ioi_iff_exists_Ioo_subset.2 ⟨(max b 1)⁻¹, by simp [zero_lt_one], λx hx, _⟩,
calc b ≤ max b 1 : le_max_left _ _
... ≤ x⁻¹ : begin
apply (le_inv _ hx.1).2 (le_of_lt hx.2),
exact lt_of_lt_of_le zero_lt_one (le_max_right _ _)
end
end
/-- The function `r ↦ r⁻¹` tends to `0` on the right as `r → +∞`. -/
lemma tendsto_inv_at_top_zero' [discrete_linear_ordered_field α] [topological_space α]
[order_topology α] : tendsto (λr:α, r⁻¹) at_top (nhds_within (0 : α) (set.Ioi 0)) :=
begin
assume s hs,
rw mem_nhds_within_Ioi_iff_exists_Ioc_subset at hs,
rcases hs with ⟨C, C0, hC⟩,
change 0 < C at C0,
refine filter.mem_map.2 (mem_sets_of_superset (mem_at_top C⁻¹) (λ x hx, hC _)),
have : 0 < x, from lt_of_lt_of_le (inv_pos.2 C0) hx,
exact ⟨inv_pos.2 this, (inv_le C0 this).1 hx⟩
end
lemma tendsto_inv_at_top_zero [discrete_linear_ordered_field α] [topological_space α]
[order_topology α] : tendsto (λr:α, r⁻¹) at_top (𝓝 0) :=
tendsto_le_right inf_le_left tendsto_inv_at_top_zero'
lemma summable_of_absolute_convergence_real {f : ℕ → ℝ} :
(∃r, tendsto (λn, (∑ i in range n, abs (f i))) at_top (𝓝 r)) → summable f
| ⟨r, hr⟩ :=
begin
refine summable_of_summable_norm ⟨r, (has_sum_iff_tendsto_nat_of_nonneg _ _).2 _⟩,
exact assume i, norm_nonneg _,
simpa only using hr
end
lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (𝓝 0) :=
tendsto_inv_at_top_zero.comp (tendsto_coe_nat_real_at_top_iff.2 tendsto_id)
lemma tendsto_const_div_at_top_nhds_0_nat (C : ℝ) : tendsto (λ n : ℕ, C / n) at_top (𝓝 0) :=
by simpa only [mul_zero] using tendsto_const_nhds.mul tendsto_inverse_at_top_nhds_0_nat
lemma nnreal.tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : nnreal)⁻¹) at_top (𝓝 0) :=
by { rw ← nnreal.tendsto_coe, convert tendsto_inverse_at_top_nhds_0_nat, simp }
lemma nnreal.tendsto_const_div_at_top_nhds_0_nat (C : nnreal) :
tendsto (λ n : ℕ, C / n) at_top (𝓝 0) :=
by simpa using tendsto_const_nhds.mul nnreal.tendsto_inverse_at_top_nhds_0_nat
lemma tendsto_one_div_add_at_top_nhds_0_nat :
tendsto (λ n : ℕ, 1 / ((n : ℝ) + 1)) at_top (𝓝 0) :=
suffices tendsto (λ n : ℕ, 1 / (↑(n + 1) : ℝ)) at_top (𝓝 0), by simpa,
(tendsto_add_at_top_iff_nat 1).2 (tendsto_const_div_at_top_nhds_0_nat 1)
/-! ### Powers -/
lemma tendsto_add_one_pow_at_top_at_top_of_pos [linear_ordered_semiring α] [archimedean α] {r : α}
(h : 0 < r) :
tendsto (λ n:ℕ, (r + 1)^n) at_top at_top :=
(tendsto_at_top_at_top_of_monotone (λ n m, pow_le_pow (le_add_of_nonneg_left' (le_of_lt h)))).2 $
λ x, (add_one_pow_unbounded_of_pos x h).imp $ λ _, le_of_lt
lemma tendsto_pow_at_top_at_top_of_one_lt [linear_ordered_ring α] [archimedean α]
{r : α} (h : 1 < r) :
tendsto (λn:ℕ, r ^ n) at_top at_top :=
sub_add_cancel r 1 ▸ tendsto_add_one_pow_at_top_at_top_of_pos (sub_pos.2 h)
lemma nat.tendsto_pow_at_top_at_top_of_one_lt {m : ℕ} (h : 1 < m) :
tendsto (λn:ℕ, m ^ n) at_top at_top :=
begin
simp only [← nat.pow_eq_pow],
exact nat.sub_add_cancel (le_of_lt h) ▸
tendsto_add_one_pow_at_top_at_top_of_pos (nat.sub_pos_of_lt h)
end
lemma lim_norm_zero' {𝕜 : Type*} [normed_group 𝕜] :
tendsto (norm : 𝕜 → ℝ) (nhds_within 0 {x | x ≠ 0}) (nhds_within 0 (set.Ioi 0)) :=
lim_norm_zero.inf $ tendsto_principal_principal.2 $ λ x hx, norm_pos_iff.2 hx
lemma normed_field.tendsto_norm_inverse_nhds_within_0_at_top {𝕜 : Type*} [normed_field 𝕜] :
tendsto (λ x:𝕜, ∥x⁻¹∥) (nhds_within 0 {x | x ≠ 0}) at_top :=
(tendsto_inv_zero_at_top.comp lim_norm_zero').congr $ λ x, (normed_field.norm_inv x).symm
lemma tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) :
tendsto (λn:ℕ, r^n) at_top (𝓝 0) :=
by_cases
(assume : r = 0, (tendsto_add_at_top_iff_nat 1).mp $ by simp [pow_succ, this, tendsto_const_nhds])
(assume : r ≠ 0,
have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (𝓝 0),
from tendsto_inv_at_top_zero.comp
(tendsto_pow_at_top_at_top_of_one_lt $ one_lt_inv (lt_of_le_of_ne h₁ this.symm) h₂),
tendsto.congr' (univ_mem_sets' $ by simp *) this)
lemma nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : nnreal} (hr : r < 1) :
tendsto (λ n:ℕ, r^n) at_top (𝓝 0) :=
nnreal.tendsto_coe.1 $ by simp only [nnreal.coe_pow, nnreal.coe_zero,
tendsto_pow_at_top_nhds_0_of_lt_1 r.coe_nonneg hr]
lemma ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 {r : ennreal} (hr : r < 1) :
tendsto (λ n:ℕ, r^n) at_top (𝓝 0) :=
begin
rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩,
rw [← ennreal.coe_zero],
norm_cast at *,
apply nnreal.tendsto_pow_at_top_nhds_0_of_lt_1 hr
end
lemma tendsto_pow_at_top_nhds_0_of_norm_lt_1 {K : Type*} [normed_field K] {ξ : K}
(_ : ∥ξ∥ < 1) : tendsto (λ n : ℕ, ξ^n) at_top (𝓝 0) :=
begin
rw [tendsto_iff_norm_tendsto_zero],
convert tendsto_pow_at_top_nhds_0_of_lt_1 (norm_nonneg ξ) ‹∥ξ∥ < 1›,
ext n,
simp
end
lemma tendsto_pow_at_top_nhds_0_of_abs_lt_1 {r : ℝ} (h : abs r < 1) :
tendsto (λn:ℕ, r^n) at_top (𝓝 0) :=
tendsto_pow_at_top_nhds_0_of_norm_lt_1 h
/-! ### Geometric series-/
section geometric
lemma has_sum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) :
has_sum (λn:ℕ, r ^ n) (1 - r)⁻¹ :=
have r ≠ 1, from ne_of_lt h₂,
have r + -1 ≠ 0,
by rw [←sub_eq_add_neg, ne, sub_eq_iff_eq_add]; simp; assumption,
have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (𝓝 ((0 - 1) * (r - 1)⁻¹)),
from ((tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂).sub tendsto_const_nhds).mul tendsto_const_nhds,
have (λ n, (∑ i in range n, r ^ i)) = (λ n, geom_series r n) := rfl,
(has_sum_iff_tendsto_nat_of_nonneg (pow_nonneg h₁) _).mpr $
by simp [neg_inv, geom_sum, div_eq_mul_inv, *] at *
lemma summable_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : summable (λn:ℕ, r ^ n) :=
⟨_, has_sum_geometric_of_lt_1 h₁ h₂⟩
lemma tsum_geometric_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : (∑'n:ℕ, r ^ n) = (1 - r)⁻¹ :=
tsum_eq_has_sum (has_sum_geometric_of_lt_1 h₁ h₂)
lemma has_sum_geometric_two : has_sum (λn:ℕ, ((1:ℝ)/2) ^ n) 2 :=
by convert has_sum_geometric_of_lt_1 _ _; norm_num
lemma summable_geometric_two : summable (λn:ℕ, ((1:ℝ)/2) ^ n) :=
⟨_, has_sum_geometric_two⟩
lemma tsum_geometric_two : (∑'n:ℕ, ((1:ℝ)/2) ^ n) = 2 :=
tsum_eq_has_sum has_sum_geometric_two
lemma has_sum_geometric_two' (a : ℝ) : has_sum (λn:ℕ, (a / 2) / 2 ^ n) a :=
begin
convert has_sum.mul_left (a / 2) (has_sum_geometric_of_lt_1
(le_of_lt one_half_pos) one_half_lt_one),
{ funext n, simp, refl, },
{ norm_num }
end
lemma summable_geometric_two' (a : ℝ) : summable (λ n:ℕ, (a / 2) / 2 ^ n) :=
⟨a, has_sum_geometric_two' a⟩
lemma tsum_geometric_two' (a : ℝ) : (∑' n:ℕ, (a / 2) / 2^n) = a :=
tsum_eq_has_sum $ has_sum_geometric_two' a
lemma nnreal.has_sum_geometric {r : nnreal} (hr : r < 1) :
has_sum (λ n : ℕ, r ^ n) (1 - r)⁻¹ :=
begin
apply nnreal.has_sum_coe.1,
push_cast,
rw [nnreal.coe_sub (le_of_lt hr)],
exact has_sum_geometric_of_lt_1 r.coe_nonneg hr
end
lemma nnreal.summable_geometric {r : nnreal} (hr : r < 1) : summable (λn:ℕ, r ^ n) :=
⟨_, nnreal.has_sum_geometric hr⟩
lemma tsum_geometric_nnreal {r : nnreal} (hr : r < 1) : (∑'n:ℕ, r ^ n) = (1 - r)⁻¹ :=
tsum_eq_has_sum (nnreal.has_sum_geometric hr)
/-- The series `pow r` converges to `(1-r)⁻¹`. For `r < 1` the RHS is a finite number,
and for `1 ≤ r` the RHS equals `∞`. -/
lemma ennreal.tsum_geometric (r : ennreal) : (∑'n:ℕ, r ^ n) = (1 - r)⁻¹ :=
begin
cases lt_or_le r 1 with hr hr,
{ rcases ennreal.lt_iff_exists_coe.1 hr with ⟨r, rfl, hr'⟩,
norm_cast at *,
convert ennreal.tsum_coe_eq (nnreal.has_sum_geometric hr),
rw [ennreal.coe_inv $ ne_of_gt $ nnreal.sub_pos.2 hr] },
{ rw [ennreal.sub_eq_zero_of_le hr, ennreal.inv_zero, ennreal.tsum_eq_supr_nat, supr_eq_top],
refine λ a ha, (ennreal.exists_nat_gt (lt_top_iff_ne_top.1 ha)).imp
(λ n hn, lt_of_lt_of_le hn _),
have : ∀ k:ℕ, 1 ≤ r^k, by simpa using canonically_ordered_semiring.pow_le_pow_of_le_left hr,
calc (n:ennreal) = (∑ i in range n, 1) : by rw [sum_const, nsmul_one, card_range]
... ≤ ∑ i in range n, r ^ i : sum_le_sum (λ k _, this k) }
end
variables {K : Type*} [normed_field K] {ξ : K}
lemma has_sum_geometric_of_norm_lt_1 (h : ∥ξ∥ < 1) : has_sum (λn:ℕ, ξ ^ n) (1 - ξ)⁻¹ :=
begin
have xi_ne_one : ξ ≠ 1, by { contrapose! h, simp [h] },
have A : tendsto (λn, (ξ ^ n - 1) * (ξ - 1)⁻¹) at_top (𝓝 ((0 - 1) * (ξ - 1)⁻¹)),
from ((tendsto_pow_at_top_nhds_0_of_norm_lt_1 h).sub tendsto_const_nhds).mul tendsto_const_nhds,
have B : (λ n, (∑ i in range n, ξ ^ i)) = (λ n, geom_series ξ n) := rfl,
rw [has_sum_iff_tendsto_nat_of_summable_norm, B],
{ simpa [geom_sum, xi_ne_one, neg_inv] using A },
{ simp [normed_field.norm_pow, summable_geometric_of_lt_1 (norm_nonneg _) h] }
end
lemma summable_geometric_of_norm_lt_1 (h : ∥ξ∥ < 1) : summable (λn:ℕ, ξ ^ n) :=
⟨_, has_sum_geometric_of_norm_lt_1 h⟩
lemma tsum_geometric_of_norm_lt_1 (h : ∥ξ∥ < 1) : (∑'n:ℕ, ξ ^ n) = (1 - ξ)⁻¹ :=
tsum_eq_has_sum (has_sum_geometric_of_norm_lt_1 h)
lemma has_sum_geometric_of_abs_lt_1 {r : ℝ} (h : abs r < 1) : has_sum (λn:ℕ, r ^ n) (1 - r)⁻¹ :=
has_sum_geometric_of_norm_lt_1 h
lemma summable_geometric_of_abs_lt_1 {r : ℝ} (h : abs r < 1) : summable (λn:ℕ, r ^ n) :=
summable_geometric_of_norm_lt_1 h
lemma tsum_geometric_of_abs_lt_1 {r : ℝ} (h : abs r < 1) : (∑'n:ℕ, r ^ n) = (1 - r)⁻¹ :=
tsum_geometric_of_norm_lt_1 h
end geometric
/-!
### Sequences with geometrically decaying distance in metric spaces
In this paragraph, we discuss sequences in metric spaces or emetric spaces for which the distance
between two consecutive terms decays geometrically. We show that such sequences are Cauchy
sequences, and bound their distances to the limit. We also discuss series with geometrically
decaying terms.
-/
section edist_le_geometric
variables [emetric_space α] (r C : ennreal) (hr : r < 1) (hC : C ≠ ⊤) {f : ℕ → α}
(hu : ∀n, edist (f n) (f (n+1)) ≤ C * r^n)
include hr hC hu
/-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, `C ≠ ∞`, `r < 1`,
then `f` is a Cauchy sequence.-/
lemma cauchy_seq_of_edist_le_geometric : cauchy_seq f :=
begin
refine cauchy_seq_of_edist_le_of_tsum_ne_top _ hu _,
rw [ennreal.tsum_mul_left, ennreal.tsum_geometric],
refine ennreal.mul_ne_top hC (ennreal.inv_ne_top.2 _),
exact ne_of_gt (ennreal.zero_lt_sub_iff_lt.2 hr)
end
omit hr hC
/-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from
`f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/
lemma edist_le_of_edist_le_geometric_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) :
edist (f n) a ≤ (C * r^n) / (1 - r) :=
begin
convert edist_le_tsum_of_edist_le_of_tendsto _ hu ha _,
simp only [pow_add, ennreal.tsum_mul_left, ennreal.tsum_geometric, ennreal.div_def, mul_assoc]
end
/-- If `edist (f n) (f (n+1))` is bounded by `C * r^n`, then the distance from
`f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/
lemma edist_le_of_edist_le_geometric_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) :
edist (f 0) a ≤ C / (1 - r) :=
by simpa only [pow_zero, mul_one] using edist_le_of_edist_le_geometric_of_tendsto r C hu ha 0
end edist_le_geometric
section edist_le_geometric_two
variables [emetric_space α] (C : ennreal) (hC : C ≠ ⊤) {f : ℕ → α}
(hu : ∀n, edist (f n) (f (n+1)) ≤ C / 2^n) {a : α} (ha : tendsto f at_top (𝓝 a))
include hC hu
/-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then `f` is a Cauchy sequence.-/
lemma cauchy_seq_of_edist_le_geometric_two : cauchy_seq f :=
begin
simp only [ennreal.div_def, ennreal.inv_pow] at hu,
refine cauchy_seq_of_edist_le_geometric 2⁻¹ C _ hC hu,
simp [ennreal.one_lt_two]
end
omit hC
include ha
/-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from
`f n` to the limit of `f` is bounded above by `2 * C * 2^-n`. -/
lemma edist_le_of_edist_le_geometric_two_of_tendsto (n : ℕ) :
edist (f n) a ≤ 2 * C / 2^n :=
begin
simp only [ennreal.div_def, ennreal.inv_pow] at hu,
rw [ennreal.div_def, mul_assoc, mul_comm, ennreal.inv_pow],
convert edist_le_of_edist_le_geometric_of_tendsto 2⁻¹ C hu ha n,
rw [ennreal.one_sub_inv_two, ennreal.inv_inv]
end
/-- If `edist (f n) (f (n+1))` is bounded by `C * 2^-n`, then the distance from
`f 0` to the limit of `f` is bounded above by `2 * C`. -/
lemma edist_le_of_edist_le_geometric_two_of_tendsto₀: edist (f 0) a ≤ 2 * C :=
by simpa only [pow_zero, ennreal.div_def, ennreal.inv_one, mul_one]
using edist_le_of_edist_le_geometric_two_of_tendsto C hu ha 0
end edist_le_geometric_two
section le_geometric
variables [metric_space α] {r C : ℝ} (hr : r < 1) {f : ℕ → α}
(hu : ∀n, dist (f n) (f (n+1)) ≤ C * r^n)
include hr hu
lemma aux_has_sum_of_le_geometric : has_sum (λ n : ℕ, C * r^n) (C / (1 - r)) :=
begin
have h0 : 0 ≤ C,
by simpa using le_trans dist_nonneg (hu 0),
rcases eq_or_lt_of_le h0 with rfl | Cpos,
{ simp [has_sum_zero] },
{ have rnonneg: r ≥ 0, from nonneg_of_mul_nonneg_left
(by simpa only [pow_one] using le_trans dist_nonneg (hu 1)) Cpos,
refine has_sum.mul_left C _,
by simpa using has_sum_geometric_of_lt_1 rnonneg hr }
end
variables (r C)
/-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then `f` is a Cauchy sequence.
Note that this lemma does not assume `0 ≤ C` or `0 ≤ r`. -/
lemma cauchy_seq_of_le_geometric : cauchy_seq f :=
cauchy_seq_of_dist_le_of_summable _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩
/-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from
`f n` to the limit of `f` is bounded above by `C * r^n / (1 - r)`. -/
lemma dist_le_of_le_geometric_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) :
dist (f 0) a ≤ C / (1 - r) :=
(tsum_eq_has_sum $ aux_has_sum_of_le_geometric hr hu) ▸
dist_le_tsum_of_dist_le_of_tendsto₀ _ hu ⟨_, aux_has_sum_of_le_geometric hr hu⟩ ha
/-- If `dist (f n) (f (n+1))` is bounded by `C * r^n`, `r < 1`, then the distance from
`f 0` to the limit of `f` is bounded above by `C / (1 - r)`. -/
lemma dist_le_of_le_geometric_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) :
dist (f n) a ≤ (C * r^n) / (1 - r) :=
begin
have := aux_has_sum_of_le_geometric hr hu,
convert dist_le_tsum_of_dist_le_of_tendsto _ hu ⟨_, this⟩ ha n,
simp only [pow_add, mul_left_comm C, mul_div_right_comm],
rw [mul_comm],
exact (eq.symm $ tsum_eq_has_sum $ this.mul_left _)
end
omit hr hu
variable (hu₂ : ∀ n, dist (f n) (f (n+1)) ≤ (C / 2) / 2^n)
/-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then `f` is a Cauchy sequence. -/
lemma cauchy_seq_of_le_geometric_two : cauchy_seq f :=
cauchy_seq_of_dist_le_of_summable _ hu₂ $ ⟨_, has_sum_geometric_two' C⟩
/-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from
`f 0` to the limit of `f` is bounded above by `C`. -/
lemma dist_le_of_le_geometric_two_of_tendsto₀ {a : α} (ha : tendsto f at_top (𝓝 a)) :
dist (f 0) a ≤ C :=
(tsum_geometric_two' C) ▸ dist_le_tsum_of_dist_le_of_tendsto₀ _ hu₂ (summable_geometric_two' C) ha
include hu₂
/-- If `dist (f n) (f (n+1))` is bounded by `(C / 2) / 2^n`, then the distance from
`f n` to the limit of `f` is bounded above by `C / 2^n`. -/
lemma dist_le_of_le_geometric_two_of_tendsto {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) :
dist (f n) a ≤ C / 2^n :=
begin
convert dist_le_tsum_of_dist_le_of_tendsto _ hu₂ (summable_geometric_two' C) ha n,
simp only [add_comm n, pow_add, (div_div_eq_div_mul _ _ _).symm],
symmetry,
exact tsum_eq_has_sum (has_sum.mul_right _ $ has_sum_geometric_two' C)
end
end le_geometric
section summable_le_geometric
variables [normed_group α] {r C : ℝ} {f : ℕ → α}
lemma dist_partial_sum_le_of_le_geometric (hf : ∀n, ∥f n∥ ≤ C * r^n) (n : ℕ) :
dist (∑ i in range n, f i) (∑ i in range (n+1), f i) ≤ C * r ^ n :=
begin
rw [sum_range_succ, dist_eq_norm, ← norm_neg],
convert hf n,
rw [neg_sub, add_sub_cancel]
end
/-- If `∥f n∥ ≤ C * r ^ n` for all `n : ℕ` and some `r < 1`, then the partial sums of `f` form a
Cauchy sequence. This lemma does not assume `0 ≤ r` or `0 ≤ C`. -/
lemma cauchy_seq_finset_of_geometric_bound (hr : r < 1) (hf : ∀n, ∥f n∥ ≤ C * r^n) :
cauchy_seq (λ s : finset (ℕ), s.sum f) :=
cauchy_seq_finset_of_norm_bounded _
(aux_has_sum_of_le_geometric hr (dist_partial_sum_le_of_le_geometric hf)).summable hf
/-- If `∥f n∥ ≤ C * r ^ n` for all `n : ℕ` and some `r < 1`, then the partial sums of `f` are within
distance `C * r ^ n / (1 - r)` of the sum of the series. This lemma does not assume `0 ≤ r` or
`0 ≤ C`. -/
lemma norm_sub_le_of_geometric_bound_of_has_sum (hr : r < 1) (hf : ∀n, ∥f n∥ ≤ C * r^n)
{a : α} (ha : has_sum f a) (n : ℕ) :
∥(finset.range n).sum f - a∥ ≤ (C * r ^ n) / (1 - r) :=
begin
rw ← dist_eq_norm,
apply dist_le_of_le_geometric_of_tendsto r C hr (dist_partial_sum_le_of_le_geometric hf),
exact ha.tendsto_sum_nat
end
end summable_le_geometric
/-! ### Positive sequences with small sums on encodable types -/
/-- For any positive `ε`, define on an encodable type a positive sequence with sum less than `ε` -/
def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε)
(ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, has_sum ε' c ∧ c ≤ ε} :=
begin
let f := λ n, (ε / 2) / 2 ^ n,
have hf : has_sum f ε := has_sum_geometric_two' _,
have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos two_pos _),
refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩,
rcases hf.summable.summable_comp_of_injective (@encodable.encode_injective ι _)
with ⟨c, hg⟩,
refine ⟨c, hg, has_sum_le_inj _ (@encodable.encode_injective ι _) _ _ hg hf⟩,
{ assume i _, exact le_of_lt (f0 _) },
{ assume n, exact le_refl _ }
end
namespace nnreal
theorem exists_pos_sum_of_encodable {ε : nnreal} (hε : 0 < ε) (ι) [encodable ι] :
∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ ∃c, has_sum ε' c ∧ c < ε :=
let ⟨a, a0, aε⟩ := dense hε in
let ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι in
⟨ λi, ⟨ε' i, le_of_lt $ hε' i⟩, assume i, nnreal.coe_lt_coe.2 $ hε' i,
⟨c, has_sum_le (assume i, le_of_lt $ hε' i) has_sum_zero hc ⟩, nnreal.has_sum_coe.1 hc,
lt_of_le_of_lt (nnreal.coe_le_coe.1 hcε) aε ⟩
end nnreal
namespace ennreal
theorem exists_pos_sum_of_encodable {ε : ennreal} (hε : 0 < ε) (ι) [encodable ι] :
∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ (∑' i, (ε' i : ennreal)) < ε :=
begin
rcases dense hε with ⟨r, h0r, hrε⟩,
rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩,
rcases nnreal.exists_pos_sum_of_encodable (coe_lt_coe.1 h0r) ι with ⟨ε', hp, c, hc, hcr⟩,
exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩
end
end ennreal
|
4c044eb93c65b9a0f1f35c27b858d028145e3dbd | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/playground/sizeof1.lean | f58c4b7ee3e6ad55eef33e4a9c8d21202ddda6f4 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 1,561 | lean | mutual
inductive TreePos (α : Type u) (β : Type v) where
| leaf (a : α)
| node (children : List (List (TreeNeg α β)))
inductive TreeNeg (α : Type u) (β : Type v) where
| leaf (a : β)
| node (children : List (List (TreePos α β)))
end
theorem aux_1 [SizeOf α] [SizeOf β] (cs : List (TreePos α β)) : TreePos._sizeOf_6 cs = sizeOf cs :=
@List.rec (TreePos α β) (fun cs => TreePos._sizeOf_6 cs = sizeOf cs)
rfl
(fun h t ih => by
show 1 + TreePos._sizeOf_1 h + TreePos._sizeOf_6 t = sizeOf (h::t)
rw ih
rfl)
cs
theorem aux_2 [SizeOf α] [SizeOf β] (cs : List (List (TreePos α β))) : TreePos._sizeOf_4 cs = sizeOf cs :=
@List.rec (List (TreePos α β)) (fun cs => TreePos._sizeOf_4 cs = sizeOf cs)
rfl
(fun h t ih => by
show 1 + TreePos._sizeOf_6 h + TreePos._sizeOf_4 t = sizeOf (h :: t)
rw aux_1
rw ih
rfl)
cs
theorem aux_3 [SizeOf α] [SizeOf β] (cs : List (TreeNeg α β)) : TreePos._sizeOf_5 cs = sizeOf cs :=
@List.rec (TreeNeg α β) (fun cs => TreePos._sizeOf_5 cs = sizeOf cs)
rfl
(fun h t ih => by
show 1 + TreePos._sizeOf_2 h + TreePos._sizeOf_5 t = sizeOf (h::t)
rw ih
rfl)
cs
theorem aux_4 [SizeOf α] [SizeOf β] (cs : List (List (TreeNeg α β))) : TreePos._sizeOf_3 cs = sizeOf cs :=
@List.rec (List (TreeNeg α β)) (fun cs => TreePos._sizeOf_3 cs = sizeOf cs)
rfl
(fun h t ih => by
show 1 + TreePos._sizeOf_5 h + TreePos._sizeOf_3 t = sizeOf (h :: t)
rw ih
rw aux_3
rfl)
cs
|
8439c7be3e617871d46a537b5501f070e1294e70 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/analysis/convex/specific_functions/deriv.lean | 60aa49399d058bebc3950bbf39bd9227ff8aa962 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 7,149 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Sébastien Gouëzel
-/
import analysis.calculus.deriv.zpow
import analysis.special_functions.pow.deriv
import analysis.special_functions.sqrt
/-!
# Collection of convex functions
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we prove that certain specific functions are strictly convex, including the following:
* `even.strict_convex_on_pow` : For an even `n : ℕ` with `2 ≤ n`, `λ x, x ^ n` is strictly convex.
* `strict_convex_on_pow` : For `n : ℕ`, with `2 ≤ n`, `λ x, x ^ n` is strictly convex on $[0, +∞)$.
* `strict_convex_on_zpow` : For `m : ℤ` with `m ≠ 0, 1`, `λ x, x ^ m` is strictly convex on
$[0, +∞)$.
* `strict_concave_on_sin_Icc` : `sin` is strictly concave on $[0, π]$
* `strict_concave_on_cos_Icc` : `cos` is strictly concave on $[-π/2, π/2]$
## TODO
These convexity lemmas are proved by checking the sign of the second derivative. If desired, most
of these could also be switched to elementary proofs, like in
`analysis.convex.specific_functions.basic`.
-/
open real set
open_locale big_operators nnreal
/-- `x^n`, `n : ℕ` is strictly convex on `[0, +∞)` for all `n` greater than `2`. -/
lemma strict_convex_on_pow {n : ℕ} (hn : 2 ≤ n) : strict_convex_on ℝ (Ici 0) (λ x : ℝ, x^n) :=
begin
apply strict_mono_on.strict_convex_on_of_deriv (convex_Ici _) (continuous_on_pow _),
rw [deriv_pow', interior_Ici],
exact λ x (hx : 0 < x) y hy hxy, mul_lt_mul_of_pos_left (pow_lt_pow_of_lt_left hxy hx.le $
nat.sub_pos_of_lt hn) (nat.cast_pos.2 $ zero_lt_two.trans_le hn),
end
/-- `x^n`, `n : ℕ` is strictly convex on the whole real line whenever `n ≠ 0` is even. -/
lemma even.strict_convex_on_pow {n : ℕ} (hn : even n) (h : n ≠ 0) :
strict_convex_on ℝ set.univ (λ x : ℝ, x^n) :=
begin
apply strict_mono.strict_convex_on_univ_of_deriv (continuous_pow n),
rw deriv_pow',
replace h := nat.pos_of_ne_zero h,
exact strict_mono.const_mul (odd.strict_mono_pow $ nat.even.sub_odd h hn $ nat.odd_iff.2 rfl)
(nat.cast_pos.2 h),
end
lemma finset.prod_nonneg_of_card_nonpos_even
{α β : Type*} [linear_ordered_comm_ring β]
{f : α → β} [decidable_pred (λ x, f x ≤ 0)]
{s : finset α} (h0 : even (s.filter (λ x, f x ≤ 0)).card) :
0 ≤ ∏ x in s, f x :=
calc 0 ≤ (∏ x in s, ((if f x ≤ 0 then (-1:β) else 1) * f x)) :
finset.prod_nonneg (λ x _, by
{ split_ifs with hx hx, by simp [hx], simp at hx ⊢, exact le_of_lt hx })
... = _ : by rw [finset.prod_mul_distrib, finset.prod_ite, finset.prod_const_one,
mul_one, finset.prod_const, neg_one_pow_eq_pow_mod_two, nat.even_iff.1 h0, pow_zero, one_mul]
lemma int_prod_range_nonneg (m : ℤ) (n : ℕ) (hn : even n) :
0 ≤ ∏ k in finset.range n, (m - k) :=
begin
rcases hn with ⟨n, rfl⟩,
induction n with n ihn, { simp },
rw ← two_mul at ihn,
rw [← two_mul, nat.succ_eq_add_one, mul_add, mul_one, bit0, ← add_assoc, finset.prod_range_succ,
finset.prod_range_succ, mul_assoc],
refine mul_nonneg ihn _, generalize : (1 + 1) * n = k,
cases le_or_lt m k with hmk hmk,
{ have : m ≤ k + 1, from hmk.trans (lt_add_one ↑k).le,
convert mul_nonneg_of_nonpos_of_nonpos (sub_nonpos_of_le hmk) _,
convert sub_nonpos_of_le this },
{ exact mul_nonneg (sub_nonneg_of_le hmk.le) (sub_nonneg_of_le hmk) }
end
lemma int_prod_range_pos {m : ℤ} {n : ℕ} (hn : even n) (hm : m ∉ Ico (0 : ℤ) n) :
0 < ∏ k in finset.range n, (m - k) :=
begin
refine (int_prod_range_nonneg m n hn).lt_of_ne (λ h, hm _),
rw [eq_comm, finset.prod_eq_zero_iff] at h,
obtain ⟨a, ha, h⟩ := h,
rw sub_eq_zero.1 h,
exact ⟨int.coe_zero_le _, int.coe_nat_lt.2 $ finset.mem_range.1 ha⟩,
end
/-- `x^m`, `m : ℤ` is convex on `(0, +∞)` for all `m` except `0` and `1`. -/
lemma strict_convex_on_zpow {m : ℤ} (hm₀ : m ≠ 0) (hm₁ : m ≠ 1) :
strict_convex_on ℝ (Ioi 0) (λ x : ℝ, x^m) :=
begin
apply strict_convex_on_of_deriv2_pos' (convex_Ioi 0),
{ exact (continuous_on_zpow₀ m).mono (λ x hx, ne_of_gt hx) },
intros x hx,
rw iter_deriv_zpow,
refine mul_pos _ (zpow_pos_of_pos hx _),
exact_mod_cast int_prod_range_pos (even_bit0 1) (λ hm, _),
norm_cast at hm,
rw ← finset.coe_Ico at hm,
fin_cases hm; cc,
end
section sqrt_mul_log
lemma has_deriv_at_sqrt_mul_log {x : ℝ} (hx : x ≠ 0) :
has_deriv_at (λ x, sqrt x * log x) ((2 + log x) / (2 * sqrt x)) x :=
begin
convert (has_deriv_at_sqrt hx).mul (has_deriv_at_log hx),
rw [add_div, div_mul_right (sqrt x) two_ne_zero, ←div_eq_mul_inv, sqrt_div_self',
add_comm, div_eq_mul_one_div, mul_comm],
end
lemma deriv_sqrt_mul_log (x : ℝ) : deriv (λ x, sqrt x * log x) x = (2 + log x) / (2 * sqrt x) :=
begin
cases lt_or_le 0 x with hx hx,
{ exact (has_deriv_at_sqrt_mul_log hx.ne').deriv },
{ rw [sqrt_eq_zero_of_nonpos hx, mul_zero, div_zero],
refine has_deriv_within_at.deriv_eq_zero _ (unique_diff_on_Iic 0 x hx),
refine (has_deriv_within_at_const x _ 0).congr_of_mem (λ x hx, _) hx,
rw [sqrt_eq_zero_of_nonpos hx, zero_mul] },
end
lemma deriv_sqrt_mul_log' : deriv (λ x, sqrt x * log x) = λ x, (2 + log x) / (2 * sqrt x) :=
funext deriv_sqrt_mul_log
lemma deriv2_sqrt_mul_log (x : ℝ) :
deriv^[2] (λ x, sqrt x * log x) x = -log x / (4 * sqrt x ^ 3) :=
begin
simp only [nat.iterate, deriv_sqrt_mul_log'],
cases le_or_lt x 0 with hx hx,
{ rw [sqrt_eq_zero_of_nonpos hx, zero_pow zero_lt_three, mul_zero, div_zero],
refine has_deriv_within_at.deriv_eq_zero _ (unique_diff_on_Iic 0 x hx),
refine (has_deriv_within_at_const _ _ 0).congr_of_mem (λ x hx, _) hx,
rw [sqrt_eq_zero_of_nonpos hx, mul_zero, div_zero] },
{ have h₀ : sqrt x ≠ 0, from sqrt_ne_zero'.2 hx,
convert (((has_deriv_at_log hx.ne').const_add 2).div
((has_deriv_at_sqrt hx.ne').const_mul 2) $ mul_ne_zero two_ne_zero h₀).deriv using 1,
nth_rewrite 2 [← mul_self_sqrt hx.le],
field_simp, ring },
end
lemma strict_concave_on_sqrt_mul_log_Ioi : strict_concave_on ℝ (set.Ioi 1) (λ x, sqrt x * log x) :=
begin
apply strict_concave_on_of_deriv2_neg' (convex_Ioi 1) _ (λ x hx, _),
{ exact continuous_sqrt.continuous_on.mul
(continuous_on_log.mono (λ x hx, ne_of_gt (zero_lt_one.trans hx))) },
{ rw [deriv2_sqrt_mul_log x],
exact div_neg_of_neg_of_pos (neg_neg_of_pos (log_pos hx))
(mul_pos four_pos (pow_pos (sqrt_pos.mpr (zero_lt_one.trans hx)) 3)) },
end
end sqrt_mul_log
open_locale real
lemma strict_concave_on_sin_Icc : strict_concave_on ℝ (Icc 0 π) sin :=
begin
apply strict_concave_on_of_deriv2_neg (convex_Icc _ _) continuous_on_sin (λ x hx, _),
rw interior_Icc at hx,
simp [sin_pos_of_mem_Ioo hx],
end
lemma strict_concave_on_cos_Icc : strict_concave_on ℝ (Icc (-(π/2)) (π/2)) cos :=
begin
apply strict_concave_on_of_deriv2_neg (convex_Icc _ _) continuous_on_cos (λ x hx, _),
rw interior_Icc at hx,
simp [cos_pos_of_mem_Ioo hx],
end
|
0cba78c5488bc0e571eb7fdbfba1a3f0800a3431 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/algebra/category/Group/default.lean | 01ab27a35f4280e3ea74dbef6370bb158ece5ce0 | [] | 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 | 289 | lean | import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.category.Group.limits
import Mathlib.algebra.category.Group.colimits
import Mathlib.algebra.category.Group.preadditive
import Mathlib.algebra.category.Group.zero
import Mathlib.PostPort
namespace Mathlib
|
54417564b1276c73b77a8bc79c82d32862bc7311 | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/topology/metric_space/basic.lean | dd4d5c046fa745643ea477de05d512fc520b7c37 | [
"Apache-2.0"
] | permissive | vaibhavkarve/mathlib | a574aaf68c0a431a47fa82ce0637f0f769826bfe | 17f8340912468f49bdc30acdb9a9fa02eeb0473a | refs/heads/master | 1,621,263,802,637 | 1,585,399,588,000 | 1,585,399,588,000 | 250,833,447 | 0 | 0 | Apache-2.0 | 1,585,410,341,000 | 1,585,410,341,000 | null | UTF-8 | Lean | false | false | 67,872 | lean | /-
Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Metric spaces.
Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel
Many definitions and theorems expected on metric spaces are already introduced on uniform spaces and
topological spaces. For example:
open and closed sets, compactness, completeness, continuity and uniform continuity
-/
import data.real.nnreal topology.metric_space.emetric_space topology.algebra.ordered
open set filter classical topological_space
noncomputable theory
open_locale uniformity
open_locale topological_space
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
/-- Construct a uniform structure from a distance function and metric space axioms -/
def uniform_space_of_dist
(dist : α → α → ℝ)
(dist_self : ∀ x : α, dist x x = 0)
(dist_comm : ∀ x y : α, dist x y = dist y x)
(dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : uniform_space α :=
uniform_space.of_core {
uniformity := (⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε}),
refl := le_infi $ assume ε, le_infi $
by simp [set.subset_def, id_rel, dist_self, (>)] {contextual := tt},
comp := le_infi $ assume ε, le_infi $ assume h, lift'_le
(mem_infi_sets (ε / 2) $ mem_infi_sets (div_pos_of_pos_of_pos h two_pos) (subset.refl _)) $
have ∀ (a b c : α), dist a c < ε / 2 → dist c b < ε / 2 → dist a b < ε,
from assume a b c hac hcb,
calc dist a b ≤ dist a c + dist c b : dist_triangle _ _ _
... < ε / 2 + ε / 2 : add_lt_add hac hcb
... = ε : by rw [div_add_div_same, add_self_div_two],
by simpa [comp_rel],
symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h,
tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [dist_comm] }
/-- The distance function (given an ambient metric space on `α`), which returns
a nonnegative real number `dist x y` given `x y : α`. -/
class has_dist (α : Type*) := (dist : α → α → ℝ)
export has_dist (dist)
section prio
set_option default_priority 100 -- see Note [default priority]
/-- Metric space
Each metric space induces a canonical `uniform_space` and hence a canonical `topological_space`.
This is enforced in the type class definition, by extending the `uniform_space` structure. When
instantiating a `metric_space` structure, the uniformity fields are not necessary, they will be
filled in by default. In the same way, each metric space induces an emetric space structure.
It is included in the structure, but filled in by default.
When one instantiates a metric space structure, for instance a product structure,
this makes it possible to use a uniform structure and an edistance that are exactly
the ones for the uniform spaces product and the emetric spaces products, thereby
ensuring that everything in defeq in diamonds.-/
class metric_space (α : Type u) extends has_dist α : Type u :=
(dist_self : ∀ x : α, dist x x = 0)
(eq_of_dist_eq_zero : ∀ {x y : α}, dist x y = 0 → x = y)
(dist_comm : ∀ x y : α, dist x y = dist y x)
(dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z)
(edist : α → α → ennreal := λx y, ennreal.of_real (dist x y))
(edist_dist : ∀ x y : α, edist x y = ennreal.of_real (dist x y) . control_laws_tac)
(to_uniform_space : uniform_space α := uniform_space_of_dist dist dist_self dist_comm dist_triangle)
(uniformity_dist : 𝓤 α = ⨅ ε>0, principal {p:α×α | dist p.1 p.2 < ε} . control_laws_tac)
end prio
variables [metric_space α]
@[priority 100] -- see Note [lower instance priority]
instance metric_space.to_uniform_space' : uniform_space α :=
metric_space.to_uniform_space α
@[priority 200] -- see Note [lower instance priority]
instance metric_space.to_has_edist : has_edist α := ⟨metric_space.edist⟩
@[simp] theorem dist_self (x : α) : dist x x = 0 := metric_space.dist_self x
theorem eq_of_dist_eq_zero {x y : α} : dist x y = 0 → x = y :=
metric_space.eq_of_dist_eq_zero
theorem dist_comm (x y : α) : dist x y = dist y x := metric_space.dist_comm x y
theorem edist_dist (x y : α) : edist x y = ennreal.of_real (dist x y) :=
metric_space.edist_dist _ x y
@[simp] theorem dist_eq_zero {x y : α} : dist x y = 0 ↔ x = y :=
iff.intro eq_of_dist_eq_zero (assume : x = y, this ▸ dist_self _)
@[simp] theorem zero_eq_dist {x y : α} : 0 = dist x y ↔ x = y :=
by rw [eq_comm, dist_eq_zero]
theorem dist_triangle (x y z : α) : dist x z ≤ dist x y + dist y z :=
metric_space.dist_triangle x y z
theorem dist_triangle_left (x y z : α) : dist x y ≤ dist z x + dist z y :=
by rw dist_comm z; apply dist_triangle
theorem dist_triangle_right (x y z : α) : dist x y ≤ dist x z + dist y z :=
by rw dist_comm y; apply dist_triangle
lemma dist_triangle4 (x y z w : α) :
dist x w ≤ dist x y + dist y z + dist z w :=
calc
dist x w ≤ dist x z + dist z w : dist_triangle x z w
... ≤ (dist x y + dist y z) + dist z w : add_le_add_right (metric_space.dist_triangle x y z) _
lemma dist_triangle4_left (x₁ y₁ x₂ y₂ : α) :
dist x₂ y₂ ≤ dist x₁ y₁ + (dist x₁ x₂ + dist y₁ y₂) :=
by rw [add_left_comm, dist_comm x₁, ← add_assoc]; apply dist_triangle4
lemma dist_triangle4_right (x₁ y₁ x₂ y₂ : α) :
dist x₁ y₁ ≤ dist x₁ x₂ + dist y₁ y₂ + dist x₂ y₂ :=
by rw [add_right_comm, dist_comm y₁]; apply dist_triangle4
/-- The triangle (polygon) inequality for sequences of points; `finset.Ico` version. -/
lemma dist_le_Ico_sum_dist (f : ℕ → α) {m n} (h : m ≤ n) :
dist (f m) (f n) ≤ (finset.Ico m n).sum (λ i, dist (f i) (f (i + 1))) :=
begin
revert n,
apply nat.le_induction,
{ simp only [finset.sum_empty, finset.Ico.self_eq_empty, dist_self] },
{ assume n hn hrec,
calc dist (f m) (f (n+1)) ≤ dist (f m) (f n) + dist _ _ : dist_triangle _ _ _
... ≤ (finset.Ico m n).sum _ + _ : add_le_add hrec (le_refl _)
... = (finset.Ico m (n+1)).sum _ :
by rw [finset.Ico.succ_top hn, finset.sum_insert, add_comm]; simp }
end
/-- The triangle (polygon) inequality for sequences of points; `finset.range` version. -/
lemma dist_le_range_sum_dist (f : ℕ → α) (n : ℕ) :
dist (f 0) (f n) ≤ (finset.range n).sum (λ i, dist (f i) (f (i + 1))) :=
finset.Ico.zero_bot n ▸ dist_le_Ico_sum_dist f (nat.zero_le n)
/-- A version of `dist_le_Ico_sum_dist` with each intermediate distance replaced
with an upper estimate. -/
lemma dist_le_Ico_sum_of_dist_le {f : ℕ → α} {m n} (hmn : m ≤ n)
{d : ℕ → ℝ} (hd : ∀ {k}, m ≤ k → k < n → dist (f k) (f (k + 1)) ≤ d k) :
dist (f m) (f n) ≤ (finset.Ico m n).sum d :=
le_trans (dist_le_Ico_sum_dist f hmn) $
finset.sum_le_sum $ λ k hk, hd (finset.Ico.mem.1 hk).1 (finset.Ico.mem.1 hk).2
/-- A version of `dist_le_range_sum_dist` with each intermediate distance replaced
with an upper estimate. -/
lemma dist_le_range_sum_of_dist_le {f : ℕ → α} (n : ℕ)
{d : ℕ → ℝ} (hd : ∀ {k}, k < n → dist (f k) (f (k + 1)) ≤ d k) :
dist (f 0) (f n) ≤ (finset.range n).sum d :=
finset.Ico.zero_bot n ▸ dist_le_Ico_sum_of_dist_le (zero_le n) (λ _ _, hd)
theorem swap_dist : function.swap (@dist α _) = dist :=
by funext x y; exact dist_comm _ _
theorem abs_dist_sub_le (x y z : α) : abs (dist x z - dist y z) ≤ dist x y :=
abs_sub_le_iff.2
⟨sub_le_iff_le_add.2 (dist_triangle _ _ _),
sub_le_iff_le_add.2 (dist_triangle_left _ _ _)⟩
theorem dist_nonneg {x y : α} : 0 ≤ dist x y :=
have 2 * dist x y ≥ 0,
from calc 2 * dist x y = dist x y + dist y x : by rw [dist_comm x y, two_mul]
... ≥ 0 : by rw ← dist_self x; apply dist_triangle,
nonneg_of_mul_nonneg_left this two_pos
@[simp] theorem dist_le_zero {x y : α} : dist x y ≤ 0 ↔ x = y :=
by simpa [le_antisymm_iff, dist_nonneg] using @dist_eq_zero _ _ x y
@[simp] theorem dist_pos {x y : α} : 0 < dist x y ↔ x ≠ y :=
by simpa [-dist_le_zero] using not_congr (@dist_le_zero _ _ x y)
@[simp] theorem abs_dist {a b : α} : abs (dist a b) = dist a b :=
abs_of_nonneg dist_nonneg
theorem eq_of_forall_dist_le {x y : α} (h : ∀ε, ε > 0 → dist x y ≤ ε) : x = y :=
eq_of_dist_eq_zero (eq_of_le_of_forall_le_of_dense dist_nonneg h)
/-- Distance as a nonnegative real number. -/
def nndist (a b : α) : nnreal := ⟨dist a b, dist_nonneg⟩
/--Express `nndist` in terms of `edist`-/
lemma nndist_edist (x y : α) : nndist x y = (edist x y).to_nnreal :=
by simp [nndist, edist_dist, nnreal.of_real, max_eq_left dist_nonneg, ennreal.of_real]
/--Express `edist` in terms of `nndist`-/
lemma edist_nndist (x y : α) : edist x y = ↑(nndist x y) :=
by { rw [edist_dist, nndist, ennreal.of_real_eq_coe_nnreal] }
/--In a metric space, the extended distance is always finite-/
lemma edist_ne_top (x y : α) : edist x y ≠ ⊤ :=
by rw [edist_dist x y]; apply ennreal.coe_ne_top
/--In a metric space, the extended distance is always finite-/
lemma edist_lt_top {α : Type*} [metric_space α] (x y : α) : edist x y < ⊤ :=
ennreal.lt_top_iff_ne_top.2 (edist_ne_top x y)
/--`nndist x x` vanishes-/
@[simp] lemma nndist_self (a : α) : nndist a a = 0 := (nnreal.coe_eq_zero _).1 (dist_self a)
/--Express `dist` in terms of `nndist`-/
lemma dist_nndist (x y : α) : dist x y = ↑(nndist x y) := rfl
/--Express `nndist` in terms of `dist`-/
lemma nndist_dist (x y : α) : nndist x y = nnreal.of_real (dist x y) :=
by rw [dist_nndist, nnreal.of_real_coe]
/--Deduce the equality of points with the vanishing of the nonnegative distance-/
theorem eq_of_nndist_eq_zero {x y : α} : nndist x y = 0 → x = y :=
by simp only [nnreal.eq_iff.symm, (dist_nndist _ _).symm, imp_self, nnreal.coe_zero, dist_eq_zero]
theorem nndist_comm (x y : α) : nndist x y = nndist y x :=
by simpa [nnreal.eq_iff.symm] using dist_comm x y
/--Characterize the equality of points with the vanishing of the nonnegative distance-/
@[simp] theorem nndist_eq_zero {x y : α} : nndist x y = 0 ↔ x = y :=
by simp only [nnreal.eq_iff.symm, (dist_nndist _ _).symm, imp_self, nnreal.coe_zero, dist_eq_zero]
@[simp] theorem zero_eq_nndist {x y : α} : 0 = nndist x y ↔ x = y :=
by simp only [nnreal.eq_iff.symm, (dist_nndist _ _).symm, imp_self, nnreal.coe_zero, zero_eq_dist]
/--Triangle inequality for the nonnegative distance-/
theorem nndist_triangle (x y z : α) : nndist x z ≤ nndist x y + nndist y z :=
by simpa [nnreal.coe_le_coe] using dist_triangle x y z
theorem nndist_triangle_left (x y z : α) : nndist x y ≤ nndist z x + nndist z y :=
by simpa [nnreal.coe_le_coe] using dist_triangle_left x y z
theorem nndist_triangle_right (x y z : α) : nndist x y ≤ nndist x z + nndist y z :=
by simpa [nnreal.coe_le_coe] using dist_triangle_right x y z
/--Express `dist` in terms of `edist`-/
lemma dist_edist (x y : α) : dist x y = (edist x y).to_real :=
by rw [edist_dist, ennreal.to_real_of_real (dist_nonneg)]
namespace metric
/- instantiate metric space as a topology -/
variables {x y z : α} {ε ε₁ ε₂ : ℝ} {s : set α}
/-- `ball x ε` is the set of all points `y` with `dist y x < ε` -/
def ball (x : α) (ε : ℝ) : set α := {y | dist y x < ε}
@[simp] theorem mem_ball : y ∈ ball x ε ↔ dist y x < ε := iff.rfl
theorem mem_ball' : y ∈ ball x ε ↔ dist x y < ε := by rw dist_comm; refl
/-- `closed_ball x ε` is the set of all points `y` with `dist y x ≤ ε` -/
def closed_ball (x : α) (ε : ℝ) := {y | dist y x ≤ ε}
@[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ dist y x ≤ ε := iff.rfl
theorem ball_subset_closed_ball : ball x ε ⊆ closed_ball x ε :=
assume y (hy : _ < _), le_of_lt hy
theorem pos_of_mem_ball (hy : y ∈ ball x ε) : ε > 0 :=
lt_of_le_of_lt dist_nonneg hy
theorem mem_ball_self (h : ε > 0) : x ∈ ball x ε :=
show dist x x < ε, by rw dist_self; assumption
theorem mem_closed_ball_self (h : ε ≥ 0) : x ∈ closed_ball x ε :=
show dist x x ≤ ε, by rw dist_self; assumption
theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε :=
by simp [dist_comm]
theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ :=
λ y (yx : _ < ε₁), lt_of_lt_of_le yx h
theorem closed_ball_subset_closed_ball {α : Type u} [metric_space α] {ε₁ ε₂ : ℝ} {x : α} (h : ε₁ ≤ ε₂) :
closed_ball x ε₁ ⊆ closed_ball x ε₂ :=
λ y (yx : _ ≤ ε₁), le_trans yx h
theorem ball_disjoint (h : ε₁ + ε₂ ≤ dist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ :=
eq_empty_iff_forall_not_mem.2 $ λ z ⟨h₁, h₂⟩,
not_lt_of_le (dist_triangle_left x y z)
(lt_of_lt_of_le (add_lt_add h₁ h₂) h)
theorem ball_disjoint_same (h : ε ≤ dist x y / 2) : ball x ε ∩ ball y ε = ∅ :=
ball_disjoint $ by rwa [← two_mul, ← le_div_iff' two_pos]
theorem ball_subset (h : dist x y ≤ ε₂ - ε₁) : ball x ε₁ ⊆ ball y ε₂ :=
λ z zx, by rw ← add_sub_cancel'_right ε₁ ε₂; exact
lt_of_le_of_lt (dist_triangle z x y) (add_lt_add_of_lt_of_le zx h)
theorem ball_half_subset (y) (h : y ∈ ball x (ε / 2)) : ball y (ε / 2) ⊆ ball x ε :=
ball_subset $ by rw sub_self_div_two; exact le_of_lt h
theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε :=
⟨_, sub_pos.2 h, ball_subset $ by rw sub_sub_self⟩
theorem ball_eq_empty_iff_nonpos : ε ≤ 0 ↔ ball x ε = ∅ :=
(eq_empty_iff_forall_not_mem.trans
⟨λ h, le_of_not_gt $ λ ε0, h _ $ mem_ball_self ε0,
λ ε0 y h, not_lt_of_le ε0 $ pos_of_mem_ball h⟩).symm
@[simp] lemma ball_zero : ball x 0 = ∅ :=
by rw [← metric.ball_eq_empty_iff_nonpos]
theorem uniformity_basis_dist :
(𝓤 α).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {p:α×α | dist p.1 p.2 < ε}) :=
(metric_space.uniformity_dist α).symm ▸ has_basis_binfi_principal
(λ r (hr : 0 < r) p (hp : 0 < p), ⟨min r p, lt_min hr hp,
λ x (hx : dist _ _ < _), lt_of_lt_of_le hx (min_le_left r p),
λ x (hx : dist _ _ < _), lt_of_lt_of_le hx (min_le_right r p)⟩) $
nonempty_Ioi
/-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`.
For specific bases see `uniformity_basis_dist`, `uniformity_basis_dist_inv_nat_succ`,
and `uniformity_basis_dist_inv_nat_pos`. -/
protected theorem mk_uniformity_basis {β : Type*} {p : β → Prop} {f : β → ℝ}
(hf₀ : ∀ i, p i → 0 < f i) (hf : ∀ ⦃ε⦄, 0 < ε → ∃ i (hi : p i), f i ≤ ε) :
(𝓤 α).has_basis p (λ i, {p:α×α | dist p.1 p.2 < f i}) :=
begin
refine λ s, uniformity_basis_dist.mem_iff.trans _,
split,
{ rintros ⟨ε, ε₀, hε⟩,
obtain ⟨i, hi, H⟩ : ∃ i (hi : p i), f i ≤ ε, from hf ε₀,
exact ⟨i, hi, λ x (hx : _ < _), hε $ lt_of_lt_of_le hx H⟩ },
{ exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, H⟩ }
end
theorem uniformity_basis_dist_inv_nat_succ :
(𝓤 α).has_basis (λ _, true) (λ n:ℕ, {p:α×α | dist p.1 p.2 < 1 / (↑n+1) }) :=
metric.mk_uniformity_basis (λ n _, div_pos zero_lt_one $ nat.cast_add_one_pos n)
(λ ε ε0, (exists_nat_one_div_lt ε0).imp $ λ n hn, ⟨trivial, le_of_lt hn⟩)
theorem uniformity_basis_dist_inv_nat_pos :
(𝓤 α).has_basis (λ n:ℕ, 0<n) (λ n:ℕ, {p:α×α | dist p.1 p.2 < 1 / ↑n }) :=
metric.mk_uniformity_basis (λ n hn, div_pos zero_lt_one $ nat.cast_pos.2 hn)
(λ ε ε0, let ⟨n, hn⟩ := exists_nat_one_div_lt ε0 in ⟨n+1, nat.succ_pos n, le_of_lt hn⟩)
/-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then closed neighborhoods of the diagonal of sizes `{f i | p i}`
form a basis of `𝓤 α`.
Currently we have only one specific basis `uniformity_basis_dist_le` based on this constructor.
More can be easily added if needed in the future. -/
protected theorem mk_uniformity_basis_le {β : Type*} {p : β → Prop} {f : β → ℝ}
(hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) :
(𝓤 α).has_basis p (λ x, {p:α×α | dist p.1 p.2 ≤ f x}) :=
begin
refine λ s, uniformity_basis_dist.mem_iff.trans _,
split,
{ rintros ⟨ε, ε₀, hε⟩,
rcases dense ε₀ with ⟨ε', hε'⟩,
rcases hf ε' hε'.1 with ⟨i, hi, H⟩,
exact ⟨i, hi, λ x (hx : _ ≤ _), hε $ lt_of_le_of_lt (le_trans hx H) hε'.2⟩ },
{ exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, λ x (hx : _ < _), H (le_of_lt hx)⟩ }
end
/-- Contant size closed neighborhoods of the diagonal form a basis
of the uniformity filter. -/
theorem uniformity_basis_dist_le :
(𝓤 α).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {p:α×α | dist p.1 p.2 ≤ ε}) :=
metric.mk_uniformity_basis_le (λ _, id) (λ ε ε₀, ⟨ε, ε₀, le_refl ε⟩)
theorem mem_uniformity_dist {s : set (α×α)} :
s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, dist a b < ε → (a, b) ∈ s) :=
uniformity_basis_dist.mem_uniformity_iff
/-- A constant size neighborhood of the diagonal is an entourage. -/
theorem dist_mem_uniformity {ε:ℝ} (ε0 : 0 < ε) :
{p:α×α | dist p.1 p.2 < ε} ∈ 𝓤 α :=
mem_uniformity_dist.2 ⟨ε, ε0, λ a b, id⟩
theorem uniform_continuous_iff [metric_space β] {f : α → β} :
uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0,
∀{a b:α}, dist a b < δ → dist (f a) (f b) < ε :=
uniformity_basis_dist.uniform_continuous_iff uniformity_basis_dist
theorem uniform_embedding_iff [metric_space β] {f : α → β} :
uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧
∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ :=
uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl
⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (dist_mem_uniformity δ0),
⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 tu in
⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩,
λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in
⟨_, dist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩
/-- A map between metric spaces is a uniform embedding if and only if the distance between `f x`
and `f y` is controlled in terms of the distance between `x` and `y` and conversely. -/
theorem uniform_embedding_iff' [metric_space β] {f : α → β} :
uniform_embedding f ↔
(∀ ε > 0, ∃ δ > 0, ∀ {a b : α}, dist a b < δ → dist (f a) (f b) < ε) ∧
(∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ) :=
begin
split,
{ assume h,
exact ⟨uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1,
(uniform_embedding_iff.1 h).2.2⟩ },
{ rintros ⟨h₁, h₂⟩,
refine uniform_embedding_iff.2 ⟨_, uniform_continuous_iff.2 h₁, h₂⟩,
assume x y hxy,
have : dist x y ≤ 0,
{ refine le_of_forall_lt' (λδ δpos, _),
rcases h₂ δ δpos with ⟨ε, εpos, hε⟩,
have : dist (f x) (f y) < ε, by simpa [hxy],
exact hε this },
simpa using this }
end
theorem totally_bounded_iff {s : set α} :
totally_bounded s ↔ ∀ ε > 0, ∃t : set α, finite t ∧ s ⊆ ⋃y∈t, ball y ε :=
⟨λ H ε ε0, H _ (dist_mem_uniformity ε0),
λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 ru,
⟨t, ft, h⟩ := H ε ε0 in
⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩
/-- A metric space space is totally bounded if one can reconstruct up to any ε>0 any element of the
space from finitely many data. -/
lemma totally_bounded_of_finite_discretization {α : Type u} [metric_space α] {s : set α}
(H : ∀ε > (0 : ℝ), ∃ (β : Type u) [fintype β] (F : s → β),
∀x y, F x = F y → dist (x:α) y < ε) :
totally_bounded s :=
begin
cases s.eq_empty_or_nonempty with hs hs,
{ rw hs, exact totally_bounded_empty },
rcases hs with ⟨x0, hx0⟩,
haveI : inhabited s := ⟨⟨x0, hx0⟩⟩,
refine totally_bounded_iff.2 (λ ε ε0, _),
rcases H ε ε0 with ⟨β, fβ, F, hF⟩,
let Finv := function.inv_fun F,
refine ⟨range (subtype.val ∘ Finv), finite_range _, λ x xs, _⟩,
let x' := Finv (F ⟨x, xs⟩),
have : F x' = F ⟨x, xs⟩ := function.inv_fun_eq ⟨⟨x, xs⟩, rfl⟩,
simp only [set.mem_Union, set.mem_range],
exact ⟨_, ⟨F ⟨x, xs⟩, rfl⟩, hF _ _ this.symm⟩
end
protected lemma cauchy_iff {f : filter α} :
cauchy f ↔ f ≠ ⊥ ∧ ∀ ε > 0, ∃ t ∈ f, ∀ x y ∈ t, dist x y < ε :=
uniformity_basis_dist.cauchy_iff
theorem nhds_basis_ball : (𝓝 x).has_basis (λ ε:ℝ, 0 < ε) (ball x) :=
nhds_basis_uniformity uniformity_basis_dist
theorem mem_nhds_iff : s ∈ 𝓝 x ↔ ∃ε>0, ball x ε ⊆ s :=
nhds_basis_ball.mem_iff
theorem nhds_basis_closed_ball : (𝓝 x).has_basis (λ ε:ℝ, 0 < ε) (closed_ball x) :=
nhds_basis_uniformity uniformity_basis_dist_le
theorem nhds_basis_ball_inv_nat_succ :
(𝓝 x).has_basis (λ _, true) (λ n:ℕ, ball x (1 / (↑n+1))) :=
nhds_basis_uniformity uniformity_basis_dist_inv_nat_succ
theorem nhds_basis_ball_inv_nat_pos :
(𝓝 x).has_basis (λ n, 0<n) (λ n:ℕ, ball x (1 / ↑n)) :=
nhds_basis_uniformity uniformity_basis_dist_inv_nat_pos
theorem is_open_iff : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s :=
by simp only [is_open_iff_nhds, mem_nhds_iff, le_principal_iff]
theorem is_open_ball : is_open (ball x ε) :=
is_open_iff.2 $ λ y, exists_ball_subset_ball
theorem ball_mem_nhds (x : α) {ε : ℝ} (ε0 : 0 < ε) : ball x ε ∈ 𝓝 x :=
mem_nhds_sets is_open_ball (mem_ball_self ε0)
theorem nhds_within_basis_ball {s : set α} :
(nhds_within x s).has_basis (λ ε:ℝ, 0 < ε) (λ ε, ball x ε ∩ s) :=
nhds_within_has_basis nhds_basis_ball s
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem mem_nhds_within_iff {t : set α} : s ∈ nhds_within x t ↔ ∃ε>0, ball x ε ∩ t ⊆ s :=
nhds_within_basis_ball.mem_iff
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem tendsto_nhds_within_nhds_within [metric_space β] {t : set β} {f : α → β} {a b} :
tendsto f (nhds_within a s) (nhds_within b t) ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, x ∈ s → dist x a < δ → f x ∈ t ∧ dist (f x) b < ε :=
(nhds_within_basis_ball.tendsto_iff nhds_within_basis_ball).trans $
by simp only [inter_comm, mem_inter_iff, and_imp, mem_ball]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem tendsto_nhds_within_nhds [metric_space β] {f : α → β} {a b} :
tendsto f (nhds_within a s) (𝓝 b) ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, x ∈ s → dist x a < δ → dist (f x) b < ε :=
by { rw [← nhds_within_univ, tendsto_nhds_within_nhds_within],
simp only [mem_univ, true_and] }
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem tendsto_nhds_nhds [metric_space β] {f : α → β} {a b} :
tendsto f (𝓝 a) (𝓝 b) ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, dist x a < δ → dist (f x) b < ε :=
nhds_basis_ball.tendsto_iff nhds_basis_ball
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_at_iff [metric_space β] {f : α → β} {a : α} :
continuous_at f a ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, dist x a < δ → dist (f x) (f a) < ε :=
by rw [continuous_at, tendsto_nhds_nhds]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_within_at_iff [metric_space β] {f : α → β} {a : α} {s : set α} :
continuous_within_at f s a ↔
∀ ε > 0, ∃ δ > 0, ∀{x:α}, x ∈ s → dist x a < δ → dist (f x) (f a) < ε :=
by rw [continuous_within_at, tendsto_nhds_within_nhds]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_on_iff [metric_space β] {f : α → β} {s : set α} :
continuous_on f s ↔
∀ (b ∈ s) (ε > 0), ∃ δ > 0, ∀a ∈ s, dist a b < δ → dist (f a) (f b) < ε :=
by simp [continuous_on, continuous_within_at_iff]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_iff [metric_space β] {f : α → β} :
continuous f ↔
∀b (ε > 0), ∃ δ > 0, ∀a, dist a b < δ → dist (f a) (f b) < ε :=
continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_nhds
theorem tendsto_nhds {f : filter β} {u : β → α} {a : α} :
tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, dist (u x) a < ε :=
nhds_basis_ball.tendsto_right_iff
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_at_iff' [topological_space β] {f : β → α} {b : β} :
continuous_at f b ↔
∀ ε > 0, ∀ᶠ x in 𝓝 b, dist (f x) (f b) < ε :=
by rw [continuous_at, tendsto_nhds]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_within_at_iff' [topological_space β] {f : β → α} {b : β} {s : set β} :
continuous_within_at f s b ↔
∀ ε > 0, ∀ᶠ x in nhds_within b s, dist (f x) (f b) < ε :=
by rw [continuous_within_at, tendsto_nhds]
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_on_iff' [topological_space β] {f : β → α} {s : set β} :
continuous_on f s ↔
∀ (b ∈ s) (ε > 0), ∀ᶠ x in nhds_within b s, dist (f x) (f b) < ε :=
by simp [continuous_on, continuous_within_at_iff']
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem continuous_iff' [topological_space β] {f : β → α} :
continuous f ↔ ∀a (ε > 0), ∀ᶠ x in 𝓝 a, dist (f x) (f a) < ε :=
continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds
theorem tendsto_at_top [nonempty β] [semilattice_sup β] {u : β → α} {a : α} :
tendsto u at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, dist (u n) a < ε :=
(at_top_basis.tendsto_iff nhds_basis_ball).trans $
by { simp only [exists_prop, true_and], refl }
end metric
open metric
@[priority 100] -- see Note [lower instance priority]
instance metric_space.to_separated : separated α :=
separated_def.2 $ λ x y h, eq_of_forall_dist_le $
λ ε ε0, le_of_lt (h _ (dist_mem_uniformity ε0))
/-Instantiate a metric space as an emetric space. Before we can state the instance,
we need to show that the uniform structure coming from the edistance and the
distance coincide. -/
/-- Expressing the uniformity in terms of `edist` -/
protected lemma metric.uniformity_basis_edist :
(𝓤 α).has_basis (λ ε:ennreal, 0 < ε) (λ ε, {p | edist p.1 p.2 < ε}) :=
begin
intro t,
refine mem_uniformity_dist.trans ⟨_, _⟩; rintro ⟨ε, ε0, Hε⟩,
{ use [ennreal.of_real ε, ennreal.of_real_pos.2 ε0],
rintros ⟨a, b⟩,
simp only [edist_dist, ennreal.of_real_lt_of_real_iff ε0],
exact Hε },
{ rcases ennreal.lt_iff_exists_real_btwn.1 ε0 with ⟨ε', _, ε0', hε⟩,
rw [ennreal.of_real_pos] at ε0',
refine ⟨ε', ε0', λ a b h, Hε (lt_trans _ hε)⟩,
rwa [edist_dist, ennreal.of_real_lt_of_real_iff ε0'] }
end
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem metric.uniformity_edist : 𝓤 α = (⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε}) :=
metric.uniformity_basis_edist.eq_binfi
/-- A metric space induces an emetric space -/
@[priority 100] -- see Note [lower instance priority]
instance metric_space.to_emetric_space : emetric_space α :=
{ edist := edist,
edist_self := by simp [edist_dist],
eq_of_edist_eq_zero := assume x y h, by simpa [edist_dist] using h,
edist_comm := by simp only [edist_dist, dist_comm]; simp,
edist_triangle := assume x y z, begin
simp only [edist_dist, (ennreal.of_real_add _ _).symm, dist_nonneg],
rw ennreal.of_real_le_of_real_iff _,
{ exact dist_triangle _ _ _ },
{ simpa using add_le_add (dist_nonneg : 0 ≤ dist x y) dist_nonneg }
end,
uniformity_edist := metric.uniformity_edist,
..‹metric_space α› }
/-- Balls defined using the distance or the edistance coincide -/
lemma metric.emetric_ball {x : α} {ε : ℝ} : emetric.ball x (ennreal.of_real ε) = ball x ε :=
begin
ext y,
simp only [emetric.mem_ball, mem_ball, edist_dist],
exact ennreal.of_real_lt_of_real_iff_of_nonneg dist_nonneg
end
/-- Closed balls defined using the distance or the edistance coincide -/
lemma metric.emetric_closed_ball {x : α} {ε : ℝ} (h : 0 ≤ ε) :
emetric.closed_ball x (ennreal.of_real ε) = closed_ball x ε :=
by ext y; simp [edist_dist]; rw ennreal.of_real_le_of_real_iff h
def metric_space.replace_uniformity {α} [U : uniform_space α] (m : metric_space α)
(H : @uniformity _ U = @uniformity _ (metric_space.to_uniform_space α)) :
metric_space α :=
{ dist := @dist _ m.to_has_dist,
dist_self := dist_self,
eq_of_dist_eq_zero := @eq_of_dist_eq_zero _ _,
dist_comm := dist_comm,
dist_triangle := dist_triangle,
edist := edist,
edist_dist := edist_dist,
to_uniform_space := U,
uniformity_dist := H.trans (metric_space.uniformity_dist α) }
/-- One gets a metric space from an emetric space if the edistance
is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the
uniformity are defeq in the metric space and the emetric space. In this definition, the distance
is given separately, to be able to prescribe some expression which is not defeq to the push-forward
of the edistance to reals. -/
def emetric_space.to_metric_space_of_dist {α : Type u} [e : emetric_space α]
(dist : α → α → ℝ)
(edist_ne_top : ∀x y: α, edist x y ≠ ⊤)
(h : ∀x y, dist x y = ennreal.to_real (edist x y)) :
metric_space α :=
let m : metric_space α :=
{ dist := dist,
eq_of_dist_eq_zero := λx y hxy, by simpa [h, ennreal.to_real_eq_zero_iff, edist_ne_top x y] using hxy,
dist_self := λx, by simp [h],
dist_comm := λx y, by simp [h, emetric_space.edist_comm],
dist_triangle := λx y z, begin
simp only [h],
rw [← ennreal.to_real_add (edist_ne_top _ _) (edist_ne_top _ _),
ennreal.to_real_le_to_real (edist_ne_top _ _)],
{ exact edist_triangle _ _ _ },
{ simp [ennreal.add_eq_top, edist_ne_top] }
end,
edist := λx y, edist x y,
edist_dist := λx y, by simp [h, ennreal.of_real_to_real, edist_ne_top] } in
m.replace_uniformity $ by { rw [uniformity_edist, metric.uniformity_edist], refl }
/-- One gets a metric space from an emetric space if the edistance
is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the
uniformity are defeq in the metric space and the emetric space. -/
def emetric_space.to_metric_space {α : Type u} [e : emetric_space α] (h : ∀x y: α, edist x y ≠ ⊤) :
metric_space α :=
emetric_space.to_metric_space_of_dist (λx y, ennreal.to_real (edist x y)) h (λx y, rfl)
/-- A very useful criterion to show that a space is complete is to show that all sequences
which satisfy a bound of the form `dist (u n) (u m) < B N` for all `n m ≥ N` are
converging. This is often applied for `B N = 2^{-N}`, i.e., with a very fast convergence to
`0`, which makes it possible to use arguments of converging series, while this is impossible
to do in general for arbitrary Cauchy sequences. -/
theorem metric.complete_of_convergent_controlled_sequences (B : ℕ → real) (hB : ∀n, 0 < B n)
(H : ∀u : ℕ → α, (∀N n m : ℕ, N ≤ n → N ≤ m → dist (u n) (u m) < B N) → ∃x, tendsto u at_top (𝓝 x)) :
complete_space α :=
begin
-- this follows from the same criterion in emetric spaces. We just need to translate
-- the convergence assumption from `dist` to `edist`
apply emetric.complete_of_convergent_controlled_sequences (λn, ennreal.of_real (B n)),
{ simp [hB] },
{ assume u Hu,
apply H,
assume N n m hn hm,
rw [← ennreal.of_real_lt_of_real_iff (hB N), ← edist_dist],
exact Hu N n m hn hm }
end
theorem metric.complete_of_cauchy_seq_tendsto :
(∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) → complete_space α :=
emetric.complete_of_cauchy_seq_tendsto
section real
/-- Instantiate the reals as a metric space. -/
instance real.metric_space : metric_space ℝ :=
{ dist := λx y, abs (x - y),
dist_self := by simp [abs_zero],
eq_of_dist_eq_zero := by simp [sub_eq_zero],
dist_comm := assume x y, abs_sub _ _,
dist_triangle := assume x y z, abs_sub_le _ _ _ }
theorem real.dist_eq (x y : ℝ) : dist x y = abs (x - y) := rfl
theorem real.dist_0_eq_abs (x : ℝ) : dist x 0 = abs x :=
by simp [real.dist_eq]
instance : order_topology ℝ :=
order_topology_of_nhds_abs $ λ x, begin
simp only [show ∀ r, {b : ℝ | abs (x - b) < r} = ball x r,
by simp [-sub_eq_add_neg, abs_sub, ball, real.dist_eq]],
apply le_antisymm,
{ simp [le_infi_iff],
exact λ ε ε0, mem_nhds_sets (is_open_ball) (mem_ball_self ε0) },
{ intros s h,
rcases mem_nhds_iff.1 h with ⟨ε, ε0, ss⟩,
exact mem_infi_sets _ (mem_infi_sets ε0 (mem_principal_sets.2 ss)) },
end
lemma closed_ball_Icc {x r : ℝ} : closed_ball x r = Icc (x-r) (x+r) :=
by ext y; rw [mem_closed_ball, dist_comm, real.dist_eq,
abs_sub_le_iff, mem_Icc, ← sub_le_iff_le_add', sub_le]
/-- Special case of the sandwich theorem; see `tendsto_of_tendsto_of_tendsto_of_le_of_le`
and `tendsto_of_tendsto_of_tendsto_of_le_of_le'` for the general case. -/
lemma squeeze_zero {α} {f g : α → ℝ} {t₀ : filter α} (hf : ∀t, 0 ≤ f t) (hft : ∀t, f t ≤ g t)
(g0 : tendsto g t₀ (𝓝 0)) : tendsto f t₀ (𝓝 0) :=
tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds g0 hf hft
theorem metric.uniformity_eq_comap_nhds_zero :
𝓤 α = comap (λp:α×α, dist p.1 p.2) (𝓝 (0 : ℝ)) :=
by { ext s,
simp [mem_uniformity_dist, (nhds_basis_ball.comap _).mem_iff, subset_def, real.dist_0_eq_abs] }
lemma cauchy_seq_iff_tendsto_dist_at_top_0 [nonempty β] [semilattice_sup β] {u : β → α} :
cauchy_seq u ↔ tendsto (λ (n : β × β), dist (u n.1) (u n.2)) at_top (𝓝 0) :=
by rw [cauchy_seq_iff_tendsto, metric.uniformity_eq_comap_nhds_zero, tendsto_comap_iff,
prod.map_def]
end real
section cauchy_seq
variables [nonempty β] [semilattice_sup β]
/-- In a metric space, Cauchy sequences are characterized by the fact that, eventually,
the distance between its elements is arbitrarily small -/
theorem metric.cauchy_seq_iff {u : β → α} :
cauchy_seq u ↔ ∀ε>0, ∃N, ∀m n≥N, dist (u m) (u n) < ε :=
uniformity_basis_dist.cauchy_seq_iff
/-- A variation around the metric characterization of Cauchy sequences -/
theorem metric.cauchy_seq_iff' {u : β → α} :
cauchy_seq u ↔ ∀ε>0, ∃N, ∀n≥N, dist (u n) (u N) < ε :=
uniformity_basis_dist.cauchy_seq_iff'
/-- If the distance between `s n` and `s m`, `n, m ≥ N` is bounded above by `b N`
and `b` converges to zero, then `s` is a Cauchy sequence. -/
lemma cauchy_seq_of_le_tendsto_0 {s : β → α} (b : β → ℝ)
(h : ∀ n m N : β, N ≤ n → N ≤ m → dist (s n) (s m) ≤ b N) (h₀ : tendsto b at_top (nhds 0)) :
cauchy_seq s :=
metric.cauchy_seq_iff.2 $ λ ε ε0,
(metric.tendsto_at_top.1 h₀ ε ε0).imp $ λ N hN m n hm hn,
calc dist (s m) (s n) ≤ b N : h m n N hm hn
... ≤ abs (b N) : le_abs_self _
... = dist (b N) 0 : by rw real.dist_0_eq_abs; refl
... < ε : (hN _ (le_refl N))
/-- A Cauchy sequence on the natural numbers is bounded. -/
theorem cauchy_seq_bdd {u : ℕ → α} (hu : cauchy_seq u) :
∃ R > 0, ∀ m n, dist (u m) (u n) < R :=
begin
rcases metric.cauchy_seq_iff'.1 hu 1 zero_lt_one with ⟨N, hN⟩,
suffices : ∃ R > 0, ∀ n, dist (u n) (u N) < R,
{ rcases this with ⟨R, R0, H⟩,
exact ⟨_, add_pos R0 R0, λ m n,
lt_of_le_of_lt (dist_triangle_right _ _ _) (add_lt_add (H m) (H n))⟩ },
let R := finset.sup (finset.range N) (λ n, nndist (u n) (u N)),
refine ⟨↑R + 1, add_pos_of_nonneg_of_pos R.2 zero_lt_one, λ n, _⟩,
cases le_or_lt N n,
{ exact lt_of_lt_of_le (hN _ h) (le_add_of_nonneg_left R.2) },
{ have : _ ≤ R := finset.le_sup (finset.mem_range.2 h),
exact lt_of_le_of_lt this (lt_add_of_pos_right _ zero_lt_one) }
end
/-- Yet another metric characterization of Cauchy sequences on integers. This one is often the
most efficient. -/
lemma cauchy_seq_iff_le_tendsto_0 {s : ℕ → α} : cauchy_seq s ↔ ∃ b : ℕ → ℝ,
(∀ n, 0 ≤ b n) ∧
(∀ n m N : ℕ, N ≤ n → N ≤ m → dist (s n) (s m) ≤ b N) ∧
tendsto b at_top (𝓝 0) :=
⟨λ hs, begin
/- `s` is a 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`.
First, we prove that all these distances are bounded, as otherwise the Sup
would not make sense. -/
let S := λ N, (λ(p : ℕ × ℕ), dist (s p.1) (s p.2)) '' {p | p.1 ≥ N ∧ p.2 ≥ N},
have hS : ∀ N, ∃ x, ∀ y ∈ S N, y ≤ x,
{ rcases cauchy_seq_bdd hs with ⟨R, R0, hR⟩,
refine λ N, ⟨R, _⟩, rintro _ ⟨⟨m, n⟩, _, rfl⟩,
exact le_of_lt (hR m n) },
have bdd : bdd_above (range (λ(p : ℕ × ℕ), dist (s p.1) (s p.2))),
{ rcases cauchy_seq_bdd hs with ⟨R, R0, hR⟩,
use R, rintro _ ⟨⟨m, n⟩, rfl⟩, exact le_of_lt (hR m n) },
-- Prove that it bounds the distances of points in the Cauchy sequence
have ub : ∀ m n N, N ≤ m → N ≤ n → dist (s m) (s n) ≤ Sup (S N) :=
λ m n N hm hn, real.le_Sup _ (hS N) ⟨⟨_, _⟩, ⟨hm, hn⟩, rfl⟩,
have S0m : ∀ n, (0:ℝ) ∈ S n := λ n, ⟨⟨n, n⟩, ⟨le_refl _, le_refl _⟩, dist_self _⟩,
have S0 := λ n, real.le_Sup _ (hS n) (S0m n),
-- Prove that it tends to `0`, by using the Cauchy property of `s`
refine ⟨λ N, Sup (S N), S0, ub, metric.tendsto_at_top.2 (λ ε ε0, _)⟩,
refine (metric.cauchy_seq_iff.1 hs (ε/2) (half_pos ε0)).imp (λ N hN n hn, _),
rw [real.dist_0_eq_abs, abs_of_nonneg (S0 n)],
refine lt_of_le_of_lt (real.Sup_le_ub _ ⟨_, S0m _⟩ _) (half_lt_self ε0),
rintro _ ⟨⟨m', n'⟩, ⟨hm', hn'⟩, rfl⟩,
exact le_of_lt (hN _ _ (le_trans hn hm') (le_trans hn hn'))
end,
λ ⟨b, _, b_bound, b_lim⟩, cauchy_seq_of_le_tendsto_0 b b_bound b_lim⟩
end cauchy_seq
def metric_space.induced {α β} (f : α → β) (hf : function.injective f)
(m : metric_space β) : metric_space α :=
{ dist := λ x y, dist (f x) (f y),
dist_self := λ x, dist_self _,
eq_of_dist_eq_zero := λ x y h, hf (dist_eq_zero.1 h),
dist_comm := λ x y, dist_comm _ _,
dist_triangle := λ x y z, dist_triangle _ _ _,
edist := λ x y, edist (f x) (f y),
edist_dist := λ x y, edist_dist _ _,
to_uniform_space := uniform_space.comap f m.to_uniform_space,
uniformity_dist := begin
apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, dist (f x) (f y)),
refine λ s, mem_comap_sets.trans _,
split; intro H,
{ rcases H with ⟨r, ru, rs⟩,
rcases mem_uniformity_dist.1 ru with ⟨ε, ε0, hε⟩,
refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h },
{ rcases H with ⟨ε, ε0, hε⟩,
exact ⟨_, dist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ }
end }
instance subtype.metric_space {α : Type*} {p : α → Prop} [t : metric_space α] :
metric_space (subtype p) :=
metric_space.induced coe (λ x y, subtype.coe_ext.2) t
theorem subtype.dist_eq {p : α → Prop} (x y : subtype p) : dist x y = dist (x : α) y := rfl
section nnreal
instance : metric_space nnreal := by unfold nnreal; apply_instance
lemma nnreal.dist_eq (a b : nnreal) : dist a b = abs ((a:ℝ) - b) := rfl
lemma nnreal.nndist_eq (a b : nnreal) :
nndist a b = max (a - b) (b - a) :=
begin
wlog h : a ≤ b,
{ apply nnreal.coe_eq.1,
rw [nnreal.sub_eq_zero h, max_eq_right (zero_le $ b - a), ← dist_nndist, nnreal.dist_eq,
nnreal.coe_sub h, abs, neg_sub],
apply max_eq_right,
linarith [nnreal.coe_le_coe.2 h] },
rwa [nndist_comm, max_comm]
end
end nnreal
section prod
instance prod.metric_space_max [metric_space β] : metric_space (α × β) :=
{ dist := λ x y, max (dist x.1 y.1) (dist x.2 y.2),
dist_self := λ x, by simp,
eq_of_dist_eq_zero := λ x y h, begin
cases max_le_iff.1 (le_of_eq h) with h₁ h₂,
exact prod.ext_iff.2 ⟨dist_le_zero.1 h₁, dist_le_zero.1 h₂⟩
end,
dist_comm := λ x y, by simp [dist_comm],
dist_triangle := λ x y z, max_le
(le_trans (dist_triangle _ _ _) (add_le_add (le_max_left _ _) (le_max_left _ _)))
(le_trans (dist_triangle _ _ _) (add_le_add (le_max_right _ _) (le_max_right _ _))),
edist := λ x y, max (edist x.1 y.1) (edist x.2 y.2),
edist_dist := assume x y, begin
have : monotone ennreal.of_real := assume x y h, ennreal.of_real_le_of_real h,
rw [edist_dist, edist_dist, this.map_max.symm]
end,
uniformity_dist := begin
refine uniformity_prod.trans _,
simp only [uniformity_basis_dist.eq_binfi, comap_infi],
rw ← infi_inf_eq, congr, funext,
rw ← infi_inf_eq, congr, funext,
simp [inf_principal, ext_iff, max_lt_iff]
end,
to_uniform_space := prod.uniform_space }
lemma prod.dist_eq [metric_space β] {x y : α × β} :
dist x y = max (dist x.1 y.1) (dist x.2 y.2) := rfl
end prod
theorem uniform_continuous_dist' : uniform_continuous (λp:α×α, dist p.1 p.2) :=
metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε/2, half_pos ε0,
begin
suffices,
{ intros p q h, cases p with p₁ p₂, cases q with q₁ q₂,
cases max_lt_iff.1 h with h₁ h₂, clear h,
dsimp at h₁ h₂ ⊢,
rw real.dist_eq,
refine abs_sub_lt_iff.2 ⟨_, _⟩,
{ revert p₁ p₂ q₁ q₂ h₁ h₂, exact this },
{ apply this; rwa dist_comm } },
intros p₁ p₂ q₁ q₂ h₁ h₂,
have := add_lt_add
(abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₁ q₁ p₂) h₁)).1
(abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₂ q₂ q₁) h₂)).1,
rwa [add_halves, dist_comm p₂, sub_add_sub_cancel, dist_comm q₂] at this
end⟩)
theorem uniform_continuous_dist [uniform_space β] {f g : β → α}
(hf : uniform_continuous f) (hg : uniform_continuous g) :
uniform_continuous (λb, dist (f b) (g b)) :=
uniform_continuous_dist'.comp (hf.prod_mk hg)
theorem continuous_dist' : continuous (λp:α×α, dist p.1 p.2) :=
uniform_continuous_dist'.continuous
theorem continuous_dist [topological_space β] {f g : β → α}
(hf : continuous f) (hg : continuous g) : continuous (λb, dist (f b) (g b)) :=
continuous_dist'.comp (hf.prod_mk hg)
theorem tendsto_dist {f g : β → α} {x : filter β} {a b : α}
(hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) :
tendsto (λx, dist (f x) (g x)) x (𝓝 (dist a b)) :=
have tendsto (λp:α×α, dist p.1 p.2) (𝓝 (a, b)) (𝓝 (dist a b)),
from continuous_iff_continuous_at.mp continuous_dist' (a, b),
tendsto.comp (by rw [nhds_prod_eq] at this; exact this) (hf.prod_mk hg)
lemma nhds_comap_dist (a : α) : (𝓝 (0 : ℝ)).comap (λa', dist a' a) = 𝓝 a :=
by simp only [@nhds_eq_comap_uniformity α, metric.uniformity_eq_comap_nhds_zero,
comap_comap_comp, (∘), dist_comm]
lemma tendsto_iff_dist_tendsto_zero {f : β → α} {x : filter β} {a : α} :
(tendsto f x (𝓝 a)) ↔ (tendsto (λb, dist (f b) a) x (𝓝 0)) :=
by rw [← nhds_comap_dist a, tendsto_comap_iff]
lemma uniform_continuous_nndist' : uniform_continuous (λp:α×α, nndist p.1 p.2) :=
uniform_continuous_subtype_mk uniform_continuous_dist' _
lemma continuous_nndist' : continuous (λp:α×α, nndist p.1 p.2) :=
uniform_continuous_nndist'.continuous
lemma continuous_nndist [topological_space β] {f g : β → α}
(hf : continuous f) (hg : continuous g) : continuous (λb, nndist (f b) (g b)) :=
continuous_nndist'.comp (hf.prod_mk hg)
lemma tendsto_nndist' (a b :α) :
tendsto (λp:α×α, nndist p.1 p.2) (filter.prod (𝓝 a) (𝓝 b)) (𝓝 (nndist a b)) :=
by rw [← nhds_prod_eq]; exact continuous_iff_continuous_at.1 continuous_nndist' _
namespace metric
variables {x y z : α} {ε ε₁ ε₂ : ℝ} {s : set α}
theorem is_closed_ball : is_closed (closed_ball x ε) :=
is_closed_le (continuous_dist continuous_id continuous_const) continuous_const
/-- ε-characterization of the closure in metric spaces-/
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem mem_closure_iff {α : Type u} [metric_space α] {s : set α} {a : α} :
a ∈ closure s ↔ ∀ε>0, ∃b ∈ s, dist a b < ε :=
(mem_closure_iff_nhds_basis nhds_basis_ball).trans $
by simp only [mem_ball, dist_comm]
lemma mem_closure_range_iff {α : Type u} [metric_space α] {e : β → α} {a : α} :
a ∈ closure (range e) ↔ ∀ε>0, ∃ k : β, dist a (e k) < ε :=
by simp only [mem_closure_iff, exists_range_iff]
lemma mem_closure_range_iff_nat {α : Type u} [metric_space α] {e : β → α} {a : α} :
a ∈ closure (range e) ↔ ∀n : ℕ, ∃ k : β, dist a (e k) < 1 / ((n : ℝ) + 1) :=
(mem_closure_iff_nhds_basis nhds_basis_ball_inv_nat_succ).trans $
by simp only [mem_ball, dist_comm, exists_range_iff, forall_const]
theorem mem_of_closed' {α : Type u} [metric_space α] {s : set α} (hs : is_closed s)
{a : α} : a ∈ s ↔ ∀ε>0, ∃b ∈ s, dist a b < ε :=
by simpa only [closure_eq_of_is_closed hs] using @mem_closure_iff _ _ s a
end metric
section pi
open finset
variables {π : β → Type*} [fintype β] [∀b, metric_space (π b)]
/-- A finite product of metric spaces is a metric space, with the sup distance. -/
instance metric_space_pi : metric_space (Πb, π b) :=
begin
/- we construct the instance from the emetric space instance to avoid checking again that the
uniformity is the same as the product uniformity, but we register nevertheless a nice formula
for the distance -/
refine emetric_space.to_metric_space_of_dist
(λf g, ((sup univ (λb, nndist (f b) (g b)) : nnreal) : ℝ)) _ _,
show ∀ (x y : Π (b : β), π b), edist x y ≠ ⊤,
{ assume x y,
rw ← lt_top_iff_ne_top,
have : (⊥ : ennreal) < ⊤ := ennreal.coe_lt_top,
simp [edist, this],
assume b,
rw lt_top_iff_ne_top,
exact edist_ne_top (x b) (y b) },
show ∀ (x y : Π (b : β), π b), ↑(sup univ (λ (b : β), nndist (x b) (y b))) =
ennreal.to_real (sup univ (λ (b : β), edist (x b) (y b))),
{ assume x y,
have : sup univ (λ (b : β), edist (x b) (y b)) = ↑(sup univ (λ (b : β), nndist (x b) (y b))),
{ simp [edist_nndist],
refine eq.symm (comp_sup_eq_sup_comp _ _ _),
exact (assume x y h, ennreal.coe_le_coe.2 h), refl },
rw this,
refl }
end
lemma dist_pi_def (f g : Πb, π b) :
dist f g = (sup univ (λb, nndist (f b) (g b)) : nnreal) := rfl
lemma dist_pi_lt_iff {f g : Πb, π b} {r : ℝ} (hr : 0 < r) :
dist f g < r ↔ ∀b, dist (f b) (g b) < r :=
begin
lift r to nnreal using le_of_lt hr,
rw_mod_cast [dist_pi_def, finset.sup_lt_iff],
{ simp [nndist], refl },
{ exact hr }
end
lemma dist_pi_le_iff {f g : Πb, π b} {r : ℝ} (hr : 0 ≤ r) :
dist f g ≤ r ↔ ∀b, dist (f b) (g b) ≤ r :=
begin
lift r to nnreal using hr,
rw_mod_cast [dist_pi_def, finset.sup_le_iff],
simp [nndist],
refl
end
/-- An open ball in a product space is a product of open balls. The assumption `0 < r`
is necessary for the case of the empty product. -/
lemma ball_pi (x : Πb, π b) {r : ℝ} (hr : 0 < r) :
ball x r = { y | ∀b, y b ∈ ball (x b) r } :=
by { ext p, simp [dist_pi_lt_iff hr] }
/-- A closed ball in a product space is a product of closed balls. The assumption `0 ≤ r`
is necessary for the case of the empty product. -/
lemma closed_ball_pi (x : Πb, π b) {r : ℝ} (hr : 0 ≤ r) :
closed_ball x r = { y | ∀b, y b ∈ closed_ball (x b) r } :=
by { ext p, simp [dist_pi_le_iff hr] }
end pi
section compact
/-- Any compact set in a metric space can be covered by finitely many balls of a given positive
radius -/
lemma finite_cover_balls_of_compact {α : Type u} [metric_space α] {s : set α}
(hs : compact s) {e : ℝ} (he : 0 < e) :
∃t ⊆ s, finite t ∧ s ⊆ ⋃x∈t, ball x e :=
begin
apply hs.elim_finite_subcover_image,
{ simp [is_open_ball] },
{ intros x xs,
simp,
exact ⟨x, ⟨xs, by simpa⟩⟩ }
end
alias finite_cover_balls_of_compact ← compact.finite_cover_balls
end compact
section proper_space
open metric
/-- A metric space is proper if all closed balls are compact. -/
class proper_space (α : Type u) [metric_space α] : Prop :=
(compact_ball : ∀x:α, ∀r, compact (closed_ball x r))
/-- If all closed balls of large enough radius are compact, then the space is proper. Especially
useful when the lower bound for the radius is 0. -/
lemma proper_space_of_compact_closed_ball_of_le
(R : ℝ) (h : ∀x:α, ∀r, R ≤ r → compact (closed_ball x r)) :
proper_space α :=
⟨begin
assume x r,
by_cases hr : R ≤ r,
{ exact h x r hr },
{ have : closed_ball x r = closed_ball x R ∩ closed_ball x r,
{ symmetry,
apply inter_eq_self_of_subset_right,
exact closed_ball_subset_closed_ball (le_of_lt (not_le.1 hr)) },
rw this,
exact (h x R (le_refl _)).inter_right is_closed_ball }
end⟩
/- A compact metric space is proper -/
@[priority 100] -- see Note [lower instance priority]
instance proper_of_compact [compact_space α] : proper_space α :=
⟨assume x r, compact_of_is_closed_subset compact_univ is_closed_ball (subset_univ _)⟩
/-- A proper space is locally compact -/
@[priority 100] -- see Note [lower instance priority]
instance locally_compact_of_proper [proper_space α] :
locally_compact_space α :=
begin
apply locally_compact_of_compact_nhds,
intros x,
existsi closed_ball x 1,
split,
{ apply mem_nhds_iff.2,
existsi (1 : ℝ),
simp,
exact ⟨zero_lt_one, ball_subset_closed_ball⟩ },
{ apply proper_space.compact_ball }
end
/-- A proper space is complete -/
@[priority 100] -- see Note [lower instance priority]
instance complete_of_proper [proper_space α] : complete_space α :=
⟨begin
intros f hf,
/- We want to show that the Cauchy filter `f` is converging. It suffices to find a closed
ball (therefore compact by properness) where it is nontrivial. -/
have A : ∃ t ∈ f, ∀ x y ∈ t, dist x y < 1 := (metric.cauchy_iff.1 hf).2 1 zero_lt_one,
rcases A with ⟨t, ⟨t_fset, ht⟩⟩,
rcases nonempty_of_mem_sets hf.1 t_fset with ⟨x, xt⟩,
have : t ⊆ closed_ball x 1 := by intros y yt; simp [dist_comm]; apply le_of_lt (ht x y xt yt),
have : closed_ball x 1 ∈ f := f.sets_of_superset t_fset this,
rcases (compact_iff_totally_bounded_complete.1 (proper_space.compact_ball x 1)).2 f hf (le_principal_iff.2 this)
with ⟨y, _, hy⟩,
exact ⟨y, hy⟩
end⟩
/-- A proper metric space is separable, and therefore second countable. Indeed, any ball is
compact, and therefore admits a countable dense subset. Taking a countable union over the balls
centered at a fixed point and with integer radius, one obtains a countable set which is
dense in the whole space. -/
@[priority 100] -- see Note [lower instance priority]
instance second_countable_of_proper [proper_space α] :
second_countable_topology α :=
begin
/- We show that the space admits a countable dense subset. The case where the space is empty
is special, and trivial. -/
have A : (univ : set α) = ∅ → ∃(s : set α), countable s ∧ closure s = (univ : set α) :=
assume H, ⟨∅, ⟨by simp, by simp; exact H.symm⟩⟩,
have B : (univ : set α).nonempty → ∃(s : set α), countable s ∧ closure s = (univ : set α) :=
begin
/- When the space is not empty, we take a point `x` in the space, and then a countable set
`T r` which is dense in the closed ball `closed_ball x r` for each `r`. Then the set
`t = ⋃ T n` (where the union is over all integers `n`) is countable, as a countable union
of countable sets, and dense in the space by construction. -/
rintros ⟨x, x_univ⟩,
choose T a using show ∀ (r:ℝ), ∃ t ⊆ closed_ball x r, (countable (t : set α) ∧ closed_ball x r = closure t),
from assume r, emetric.countable_closure_of_compact (proper_space.compact_ball _ _),
let t := (⋃n:ℕ, T (n : ℝ)),
have T₁ : countable t := by finish [countable_Union],
have T₂ : closure t ⊆ univ := by simp,
have T₃ : univ ⊆ closure t :=
begin
intros y y_univ,
rcases exists_nat_gt (dist y x) with ⟨n, n_large⟩,
have h : y ∈ closed_ball x (n : ℝ) := by simp; apply le_of_lt n_large,
have h' : closed_ball x (n : ℝ) = closure (T (n : ℝ)) := by finish,
have : y ∈ closure (T (n : ℝ)) := by rwa h' at h,
show y ∈ closure t, from mem_of_mem_of_subset this (by apply closure_mono; apply subset_Union (λ(n:ℕ), T (n:ℝ))),
end,
exact ⟨t, ⟨T₁, subset.antisymm T₂ T₃⟩⟩
end,
haveI : separable_space α := ⟨(eq_empty_or_nonempty univ).elim A B⟩,
apply emetric.second_countable_of_separable,
end
/-- A finite product of proper spaces is proper. -/
instance pi_proper_space {π : β → Type*} [fintype β] [∀b, metric_space (π b)]
[h : ∀b, proper_space (π b)] : proper_space (Πb, π b) :=
begin
refine proper_space_of_compact_closed_ball_of_le 0 (λx r hr, _),
rw closed_ball_pi _ hr,
apply compact_pi_infinite (λb, _),
apply (h b).compact_ball
end
end proper_space
namespace metric
section second_countable
open topological_space
/-- A metric space is second countable if, for every ε > 0, there is a countable set which is ε-dense. -/
lemma second_countable_of_almost_dense_set
(H : ∀ε > (0 : ℝ), ∃ s : set α, countable s ∧ (∀x, ∃y ∈ s, dist x y ≤ ε)) :
second_countable_topology α :=
begin
choose T T_dense using H,
have I1 : ∀n:ℕ, (n:ℝ) + 1 > 0 :=
λn, lt_of_lt_of_le zero_lt_one (le_add_of_nonneg_left (nat.cast_nonneg _)),
have I : ∀n:ℕ, (n+1 : ℝ)⁻¹ > 0 := λn, inv_pos.2 (I1 n),
let t := ⋃n:ℕ, T (n+1)⁻¹ (I n),
have count_t : countable t := by finish [countable_Union],
have clos_t : closure t = univ,
{ refine subset.antisymm (subset_univ _) (λx xuniv, mem_closure_iff.2 (λε εpos, _)),
rcases exists_nat_gt ε⁻¹ with ⟨n, hn⟩,
have : ε⁻¹ < n + 1 := lt_of_lt_of_le hn (le_add_of_nonneg_right zero_le_one),
have nε : ((n:ℝ)+1)⁻¹ < ε := (inv_lt (I1 n) εpos).2 this,
rcases (T_dense (n+1)⁻¹ (I n)).2 x with ⟨y, yT, Dxy⟩,
have : y ∈ t := mem_of_mem_of_subset yT (by apply subset_Union (λ (n:ℕ), T (n+1)⁻¹ (I n))),
exact ⟨y, this, lt_of_le_of_lt Dxy nε⟩ },
haveI : separable_space α := ⟨⟨t, ⟨count_t, clos_t⟩⟩⟩,
exact emetric.second_countable_of_separable α
end
/-- A metric space space is second countable if one can reconstruct up to any ε>0 any element of the
space from countably many data. -/
lemma second_countable_of_countable_discretization {α : Type u} [metric_space α]
(H : ∀ε > (0 : ℝ), ∃ (β : Type u) [encodable β] (F : α → β), ∀x y, F x = F y → dist x y ≤ ε) :
second_countable_topology α :=
begin
cases (univ : set α).eq_empty_or_nonempty with hs hs,
{ haveI : compact_space α := ⟨by rw hs; exact compact_empty⟩, by apply_instance },
rcases hs with ⟨x0, hx0⟩,
letI : inhabited α := ⟨x0⟩,
refine second_countable_of_almost_dense_set (λε ε0, _),
rcases H ε ε0 with ⟨β, fβ, F, hF⟩,
let Finv := function.inv_fun F,
refine ⟨range Finv, ⟨countable_range _, λx, _⟩⟩,
let x' := Finv (F x),
have : F x' = F x := function.inv_fun_eq ⟨x, rfl⟩,
exact ⟨x', mem_range_self _, hF _ _ this.symm⟩
end
end second_countable
end metric
lemma lebesgue_number_lemma_of_metric
{s : set α} {ι} {c : ι → set α} (hs : compact s)
(hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) :
∃ δ > 0, ∀ x ∈ s, ∃ i, ball x δ ⊆ c i :=
let ⟨n, en, hn⟩ := lebesgue_number_lemma hs hc₁ hc₂,
⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 en in
⟨δ, δ0, assume x hx, let ⟨i, hi⟩ := hn x hx in
⟨i, assume y hy, hi (hδ (mem_ball'.mp hy))⟩⟩
lemma lebesgue_number_lemma_of_metric_sUnion
{s : set α} {c : set (set α)} (hs : compact s)
(hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) :
∃ δ > 0, ∀ x ∈ s, ∃ t ∈ c, ball x δ ⊆ t :=
by rw sUnion_eq_Union at hc₂;
simpa using lebesgue_number_lemma_of_metric hs (by simpa) hc₂
namespace metric
/-- Boundedness of a subset of a metric space. We formulate the definition to work
even in the empty space. -/
def bounded (s : set α) : Prop :=
∃C, ∀x y ∈ s, dist x y ≤ C
section bounded
variables {x : α} {s t : set α} {r : ℝ}
@[simp] lemma bounded_empty : bounded (∅ : set α) :=
⟨0, by simp⟩
lemma bounded_iff_mem_bounded : bounded s ↔ ∀ x ∈ s, bounded s :=
⟨λ h _ _, h, λ H,
s.eq_empty_or_nonempty.elim
(λ hs, hs.symm ▸ bounded_empty)
(λ ⟨x, hx⟩, H x hx)⟩
/-- Subsets of a bounded set are also bounded -/
lemma bounded.subset (incl : s ⊆ t) : bounded t → bounded s :=
Exists.imp $ λ C hC x y hx hy, hC x y (incl hx) (incl hy)
/-- Closed balls are bounded -/
lemma bounded_closed_ball : bounded (closed_ball x r) :=
⟨r + r, λ y z hy hz, begin
simp only [mem_closed_ball] at *,
calc dist y z ≤ dist y x + dist z x : dist_triangle_right _ _ _
... ≤ r + r : add_le_add hy hz
end⟩
/-- Open balls are bounded -/
lemma bounded_ball : bounded (ball x r) :=
bounded_closed_ball.subset ball_subset_closed_ball
/-- Given a point, a bounded subset is included in some ball around this point -/
lemma bounded_iff_subset_ball (c : α) : bounded s ↔ ∃r, s ⊆ closed_ball c r :=
begin
split; rintro ⟨C, hC⟩,
{ cases s.eq_empty_or_nonempty with h h,
{ subst s, exact ⟨0, by simp⟩ },
{ rcases h with ⟨x, hx⟩,
exact ⟨C + dist x c, λ y hy, calc
dist y c ≤ dist y x + dist x c : dist_triangle _ _ _
... ≤ C + dist x c : add_le_add_right (hC y x hy hx) _⟩ } },
{ exact bounded_closed_ball.subset hC }
end
/-- The union of two bounded sets is bounded iff each of the sets is bounded -/
@[simp] lemma bounded_union :
bounded (s ∪ t) ↔ bounded s ∧ bounded t :=
⟨λh, ⟨h.subset (by simp), h.subset (by simp)⟩,
begin
rintro ⟨hs, ht⟩,
refine bounded_iff_mem_bounded.2 (λ x _, _),
rw bounded_iff_subset_ball x at hs ht ⊢,
rcases hs with ⟨Cs, hCs⟩, rcases ht with ⟨Ct, hCt⟩,
exact ⟨max Cs Ct, union_subset
(subset.trans hCs $ closed_ball_subset_closed_ball $ le_max_left _ _)
(subset.trans hCt $ closed_ball_subset_closed_ball $ le_max_right _ _)⟩,
end⟩
/-- A finite union of bounded sets is bounded -/
lemma bounded_bUnion {I : set β} {s : β → set α} (H : finite I) :
bounded (⋃i∈I, s i) ↔ ∀i ∈ I, bounded (s i) :=
finite.induction_on H (by simp) $ λ x I _ _ IH,
by simp [or_imp_distrib, forall_and_distrib, IH]
/-- A compact set is bounded -/
lemma bounded_of_compact {s : set α} (h : compact s) : bounded s :=
-- We cover the compact set by finitely many balls of radius 1,
-- and then argue that a finite union of bounded sets is bounded
let ⟨t, ht, fint, subs⟩ := finite_cover_balls_of_compact h zero_lt_one in
bounded.subset subs $ (bounded_bUnion fint).2 $ λ i hi, bounded_ball
alias bounded_of_compact ← compact.bounded
/-- A finite set is bounded -/
lemma bounded_of_finite {s : set α} (h : finite s) : bounded s :=
h.compact.bounded
/-- A singleton is bounded -/
lemma bounded_singleton {x : α} : bounded ({x} : set α) :=
bounded_of_finite $ finite_singleton _
/-- Characterization of the boundedness of the range of a function -/
lemma bounded_range_iff {f : β → α} : bounded (range f) ↔ ∃C, ∀x y, dist (f x) (f y) ≤ C :=
exists_congr $ λ C, ⟨
λ H x y, H _ _ ⟨x, rfl⟩ ⟨y, rfl⟩,
by rintro H _ _ ⟨x, rfl⟩ ⟨y, rfl⟩; exact H x y⟩
/-- In a compact space, all sets are bounded -/
lemma bounded_of_compact_space [compact_space α] : bounded s :=
compact_univ.bounded.subset (subset_univ _)
/-- The Heine–Borel theorem:
In a proper space, a set is compact if and only if it is closed and bounded -/
lemma compact_iff_closed_bounded [proper_space α] :
compact s ↔ is_closed s ∧ bounded s :=
⟨λ h, ⟨closed_of_compact _ h, h.bounded⟩, begin
rintro ⟨hc, hb⟩,
cases s.eq_empty_or_nonempty with h h, {simp [h, compact_empty]},
rcases h with ⟨x, hx⟩,
rcases (bounded_iff_subset_ball x).1 hb with ⟨r, hr⟩,
exact compact_of_is_closed_subset (proper_space.compact_ball x r) hc hr
end⟩
/-- The image of a proper space under an expanding onto map is proper. -/
lemma proper_image_of_proper [proper_space α] [metric_space β] (f : α → β)
(f_cont : continuous f) (hf : range f = univ) (C : ℝ)
(hC : ∀x y, dist x y ≤ C * dist (f x) (f y)) : proper_space β :=
begin
apply proper_space_of_compact_closed_ball_of_le 0 (λx₀ r hr, _),
let K := f ⁻¹' (closed_ball x₀ r),
have A : is_closed K :=
continuous_iff_is_closed.1 f_cont (closed_ball x₀ r) (is_closed_ball),
have B : bounded K := ⟨max C 0 * (r + r), λx y hx hy, calc
dist x y ≤ C * dist (f x) (f y) : hC x y
... ≤ max C 0 * dist (f x) (f y) : mul_le_mul_of_nonneg_right (le_max_left _ _) (dist_nonneg)
... ≤ max C 0 * (dist (f x) x₀ + dist (f y) x₀) :
mul_le_mul_of_nonneg_left (dist_triangle_right (f x) (f y) x₀) (le_max_right _ _)
... ≤ max C 0 * (r + r) : begin
simp only [mem_closed_ball, mem_preimage] at hx hy,
exact mul_le_mul_of_nonneg_left (add_le_add hx hy) (le_max_right _ _)
end⟩,
have : compact K := compact_iff_closed_bounded.2 ⟨A, B⟩,
have C : compact (f '' K) := this.image f_cont,
have : f '' K = closed_ball x₀ r,
by { rw image_preimage_eq_of_subset, rw hf, exact subset_univ _ },
rwa this at C
end
end bounded
section diam
variables {s : set α} {x y z : α}
/-- The diameter of a set in a metric space. To get controllable behavior even when the diameter
should be infinite, we express it in terms of the emetric.diameter -/
def diam (s : set α) : ℝ := ennreal.to_real (emetric.diam s)
/-- The diameter of a set is always nonnegative -/
lemma diam_nonneg : 0 ≤ diam s := ennreal.to_real_nonneg
lemma diam_subsingleton (hs : s.subsingleton) : diam s = 0 :=
by simp only [diam, emetric.diam_subsingleton hs, ennreal.zero_to_real]
/-- The empty set has zero diameter -/
@[simp] lemma diam_empty : diam (∅ : set α) = 0 :=
diam_subsingleton subsingleton_empty
/-- A singleton has zero diameter -/
@[simp] lemma diam_singleton : diam ({x} : set α) = 0 :=
diam_subsingleton subsingleton_singleton
-- Does not work as a simp-lemma, since {x, y} reduces to (insert y {x})
lemma diam_pair : diam ({x, y} : set α) = dist x y :=
by simp only [diam, emetric.diam_pair, dist_edist]
-- Does not work as a simp-lemma, since {x, y} reduces to (insert z (insert y {x}))
lemma diam_triple :
metric.diam ({x, y, z} : set α) = max (dist x y) (max (dist y z) (dist x z)) :=
begin
simp only [metric.diam, emetric.diam_triple, dist_edist],
rw [ennreal.to_real_max, ennreal.to_real_max];
apply_rules [ne_of_lt, edist_lt_top, max_lt]
end
/-- If the distance between any two points in a set is bounded by some constant `C`,
then `ennreal.of_real C` bounds the emetric diameter of this set. -/
lemma ediam_le_of_forall_dist_le {C : ℝ} (h : ∀ (x ∈ s) (y ∈ s), dist x y ≤ C) :
emetric.diam s ≤ ennreal.of_real C :=
emetric.diam_le_of_forall_edist_le $
λ x hx y hy, (edist_dist x y).symm ▸ ennreal.of_real_le_of_real (h x hx y hy)
/-- If the distance between any two points in a set is bounded by some non-negative constant,
this constant bounds the diameter. -/
lemma diam_le_of_forall_dist_le {C : ℝ} (h₀ : 0 ≤ C) (h : ∀ (x ∈ s) (y ∈ s), dist x y ≤ C) :
diam s ≤ C :=
ennreal.to_real_le_of_le_of_real h₀ (ediam_le_of_forall_dist_le h)
/-- If the distance between any two points in a nonempty set is bounded by some constant,
this constant bounds the diameter. -/
lemma diam_le_of_forall_dist_le_of_nonempty (hs : s.nonempty) {C : ℝ}
(h : ∀ (x ∈ s) (y ∈ s), dist x y ≤ C) : diam s ≤ C :=
have h₀ : 0 ≤ C, from let ⟨x, hx⟩ := hs in le_trans dist_nonneg (h x hx x hx),
diam_le_of_forall_dist_le h₀ h
/-- The distance between two points in a set is controlled by the diameter of the set. -/
lemma dist_le_diam_of_mem' (h : emetric.diam s ≠ ⊤) (hx : x ∈ s) (hy : y ∈ s) :
dist x y ≤ diam s :=
begin
rw [diam, dist_edist],
rw ennreal.to_real_le_to_real (edist_ne_top _ _) h,
exact emetric.edist_le_diam_of_mem hx hy
end
/-- Characterize the boundedness of a set in terms of the finiteness of its emetric.diameter. -/
lemma bounded_iff_ediam_ne_top : bounded s ↔ emetric.diam s ≠ ⊤ :=
iff.intro
(λ ⟨C, hC⟩, ne_top_of_le_ne_top ennreal.of_real_ne_top
(ediam_le_of_forall_dist_le $ λ x hx y hy, hC x y hx hy))
(λ h, ⟨diam s, λ x y hx hy, dist_le_diam_of_mem' h hx hy⟩)
lemma bounded.ediam_ne_top (h : bounded s) : emetric.diam s ≠ ⊤ :=
bounded_iff_ediam_ne_top.1 h
/-- The distance between two points in a set is controlled by the diameter of the set. -/
lemma dist_le_diam_of_mem (h : bounded s) (hx : x ∈ s) (hy : y ∈ s) : dist x y ≤ diam s :=
dist_le_diam_of_mem' h.ediam_ne_top hx hy
/-- An unbounded set has zero diameter. If you would prefer to get the value ∞, use `emetric.diam`.
This lemma makes it possible to avoid side conditions in some situations -/
lemma diam_eq_zero_of_unbounded (h : ¬(bounded s)) : diam s = 0 :=
begin
simp only [bounded_iff_ediam_ne_top, not_not, ne.def] at h,
simp [diam, h]
end
/-- If `s ⊆ t`, then the diameter of `s` is bounded by that of `t`, provided `t` is bounded. -/
lemma diam_mono {s t : set α} (h : s ⊆ t) (ht : bounded t) : diam s ≤ diam t :=
begin
unfold diam,
rw ennreal.to_real_le_to_real (bounded.subset h ht).ediam_ne_top ht.ediam_ne_top,
exact emetric.diam_mono h
end
/-- The diameter of a union is controlled by the sum of the diameters, and the distance between
any two points in each of the sets. This lemma is true without any side condition, since it is
obviously true if `s ∪ t` is unbounded. -/
lemma diam_union {t : set α} (xs : x ∈ s) (yt : y ∈ t) : diam (s ∪ t) ≤ diam s + dist x y + diam t :=
begin
classical, by_cases H : bounded (s ∪ t),
{ have hs : bounded s, from H.subset (subset_union_left _ _),
have ht : bounded t, from H.subset (subset_union_right _ _),
rw [bounded_iff_ediam_ne_top] at H hs ht,
rw [dist_edist, diam, diam, diam, ← ennreal.to_real_add, ← ennreal.to_real_add,
ennreal.to_real_le_to_real];
repeat { apply ennreal.add_ne_top.2; split }; try { assumption };
try { apply edist_ne_top },
exact emetric.diam_union xs yt },
{ rw [diam_eq_zero_of_unbounded H],
apply_rules [add_nonneg, diam_nonneg, dist_nonneg] }
end
/-- If two sets intersect, the diameter of the union is bounded by the sum of the diameters. -/
lemma diam_union' {t : set α} (h : (s ∩ t).nonempty) : diam (s ∪ t) ≤ diam s + diam t :=
begin
rcases h with ⟨x, ⟨xs, xt⟩⟩,
simpa using diam_union xs xt
end
/-- The diameter of a closed ball of radius `r` is at most `2 r`. -/
lemma diam_closed_ball {r : ℝ} (h : r ≥ 0) : diam (closed_ball x r) ≤ 2 * r :=
diam_le_of_forall_dist_le (mul_nonneg (le_of_lt two_pos) h) $ λa ha b hb, calc
dist a b ≤ dist a x + dist b x : dist_triangle_right _ _ _
... ≤ r + r : add_le_add ha hb
... = 2 * r : by simp [mul_two, mul_comm]
/-- The diameter of a ball of radius `r` is at most `2 r`. -/
lemma diam_ball {r : ℝ} (h : r ≥ 0) : diam (ball x r) ≤ 2 * r :=
le_trans (diam_mono ball_subset_closed_ball bounded_closed_ball) (diam_closed_ball h)
end diam
end metric
|
a60744c304bd5e809e626239a299ce154cc07c27 | 592ee40978ac7604005a4e0d35bbc4b467389241 | /Library/generated/mathscheme-lean/BoundedJoinSemilattice.lean | cae768813d0f9babcd92ac7ddcd10bd3ee581e21 | [] | no_license | ysharoda/Deriving-Definitions | 3e149e6641fae440badd35ac110a0bd705a49ad2 | dfecb27572022de3d4aa702cae8db19957523a59 | refs/heads/master | 1,679,127,857,700 | 1,615,939,007,000 | 1,615,939,007,000 | 229,785,731 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,446 | lean | import init.data.nat.basic
import init.data.fin.basic
import data.vector
import .Prelude
open Staged
open nat
open fin
open vector
section BoundedJoinSemilattice
structure BoundedJoinSemilattice (A : Type) : Type :=
(plus : (A → (A → A)))
(commutative_plus : (∀ {x y : A} , (plus x y) = (plus y x)))
(associative_plus : (∀ {x y z : A} , (plus (plus x y) z) = (plus x (plus y z))))
(zero : A)
(lunit_zero : (∀ {x : A} , (plus zero x) = x))
(runit_zero : (∀ {x : A} , (plus x zero) = x))
(idempotent_plus : (∀ {x : A} , (plus x x) = x))
open BoundedJoinSemilattice
structure Sig (AS : Type) : Type :=
(plusS : (AS → (AS → AS)))
(zeroS : AS)
structure Product (A : Type) : Type :=
(plusP : ((Prod A A) → ((Prod A A) → (Prod A A))))
(zeroP : (Prod A A))
(commutative_plusP : (∀ {xP yP : (Prod A A)} , (plusP xP yP) = (plusP yP xP)))
(associative_plusP : (∀ {xP yP zP : (Prod A A)} , (plusP (plusP xP yP) zP) = (plusP xP (plusP yP zP))))
(lunit_0P : (∀ {xP : (Prod A A)} , (plusP zeroP xP) = xP))
(runit_0P : (∀ {xP : (Prod A A)} , (plusP xP zeroP) = xP))
(idempotent_plusP : (∀ {xP : (Prod A A)} , (plusP xP xP) = xP))
structure Hom {A1 : Type} {A2 : Type} (Bo1 : (BoundedJoinSemilattice A1)) (Bo2 : (BoundedJoinSemilattice A2)) : Type :=
(hom : (A1 → A2))
(pres_plus : (∀ {x1 x2 : A1} , (hom ((plus Bo1) x1 x2)) = ((plus Bo2) (hom x1) (hom x2))))
(pres_zero : (hom (zero Bo1)) = (zero Bo2))
structure RelInterp {A1 : Type} {A2 : Type} (Bo1 : (BoundedJoinSemilattice A1)) (Bo2 : (BoundedJoinSemilattice A2)) : Type 1 :=
(interp : (A1 → (A2 → Type)))
(interp_plus : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((plus Bo1) x1 x2) ((plus Bo2) y1 y2))))))
(interp_zero : (interp (zero Bo1) (zero Bo2)))
inductive BoundedJoinSemilatticeTerm : Type
| plusL : (BoundedJoinSemilatticeTerm → (BoundedJoinSemilatticeTerm → BoundedJoinSemilatticeTerm))
| zeroL : BoundedJoinSemilatticeTerm
open BoundedJoinSemilatticeTerm
inductive ClBoundedJoinSemilatticeTerm (A : Type) : Type
| sing : (A → ClBoundedJoinSemilatticeTerm)
| plusCl : (ClBoundedJoinSemilatticeTerm → (ClBoundedJoinSemilatticeTerm → ClBoundedJoinSemilatticeTerm))
| zeroCl : ClBoundedJoinSemilatticeTerm
open ClBoundedJoinSemilatticeTerm
inductive OpBoundedJoinSemilatticeTerm (n : ℕ) : Type
| v : ((fin n) → OpBoundedJoinSemilatticeTerm)
| plusOL : (OpBoundedJoinSemilatticeTerm → (OpBoundedJoinSemilatticeTerm → OpBoundedJoinSemilatticeTerm))
| zeroOL : OpBoundedJoinSemilatticeTerm
open OpBoundedJoinSemilatticeTerm
inductive OpBoundedJoinSemilatticeTerm2 (n : ℕ) (A : Type) : Type
| v2 : ((fin n) → OpBoundedJoinSemilatticeTerm2)
| sing2 : (A → OpBoundedJoinSemilatticeTerm2)
| plusOL2 : (OpBoundedJoinSemilatticeTerm2 → (OpBoundedJoinSemilatticeTerm2 → OpBoundedJoinSemilatticeTerm2))
| zeroOL2 : OpBoundedJoinSemilatticeTerm2
open OpBoundedJoinSemilatticeTerm2
def simplifyCl {A : Type} : ((ClBoundedJoinSemilatticeTerm A) → (ClBoundedJoinSemilatticeTerm A))
| (plusCl zeroCl x) := x
| (plusCl x zeroCl) := x
| (plusCl x1 x2) := (plusCl (simplifyCl x1) (simplifyCl x2))
| zeroCl := zeroCl
| (sing x1) := (sing x1)
def simplifyOpB {n : ℕ} : ((OpBoundedJoinSemilatticeTerm n) → (OpBoundedJoinSemilatticeTerm n))
| (plusOL zeroOL x) := x
| (plusOL x zeroOL) := x
| (plusOL x1 x2) := (plusOL (simplifyOpB x1) (simplifyOpB x2))
| zeroOL := zeroOL
| (v x1) := (v x1)
def simplifyOp {n : ℕ} {A : Type} : ((OpBoundedJoinSemilatticeTerm2 n A) → (OpBoundedJoinSemilatticeTerm2 n A))
| (plusOL2 zeroOL2 x) := x
| (plusOL2 x zeroOL2) := x
| (plusOL2 x1 x2) := (plusOL2 (simplifyOp x1) (simplifyOp x2))
| zeroOL2 := zeroOL2
| (v2 x1) := (v2 x1)
| (sing2 x1) := (sing2 x1)
def evalB {A : Type} : ((BoundedJoinSemilattice A) → (BoundedJoinSemilatticeTerm → A))
| Bo (plusL x1 x2) := ((plus Bo) (evalB Bo x1) (evalB Bo x2))
| Bo zeroL := (zero Bo)
def evalCl {A : Type} : ((BoundedJoinSemilattice A) → ((ClBoundedJoinSemilatticeTerm A) → A))
| Bo (sing x1) := x1
| Bo (plusCl x1 x2) := ((plus Bo) (evalCl Bo x1) (evalCl Bo x2))
| Bo zeroCl := (zero Bo)
def evalOpB {A : Type} {n : ℕ} : ((BoundedJoinSemilattice A) → ((vector A n) → ((OpBoundedJoinSemilatticeTerm n) → A)))
| Bo vars (v x1) := (nth vars x1)
| Bo vars (plusOL x1 x2) := ((plus Bo) (evalOpB Bo vars x1) (evalOpB Bo vars x2))
| Bo vars zeroOL := (zero Bo)
def evalOp {A : Type} {n : ℕ} : ((BoundedJoinSemilattice A) → ((vector A n) → ((OpBoundedJoinSemilatticeTerm2 n A) → A)))
| Bo vars (v2 x1) := (nth vars x1)
| Bo vars (sing2 x1) := x1
| Bo vars (plusOL2 x1 x2) := ((plus Bo) (evalOp Bo vars x1) (evalOp Bo vars x2))
| Bo vars zeroOL2 := (zero Bo)
def inductionB {P : (BoundedJoinSemilatticeTerm → Type)} : ((∀ (x1 x2 : BoundedJoinSemilatticeTerm) , ((P x1) → ((P x2) → (P (plusL x1 x2))))) → ((P zeroL) → (∀ (x : BoundedJoinSemilatticeTerm) , (P x))))
| pplusl p0l (plusL x1 x2) := (pplusl _ _ (inductionB pplusl p0l x1) (inductionB pplusl p0l x2))
| pplusl p0l zeroL := p0l
def inductionCl {A : Type} {P : ((ClBoundedJoinSemilatticeTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClBoundedJoinSemilatticeTerm A)) , ((P x1) → ((P x2) → (P (plusCl x1 x2))))) → ((P zeroCl) → (∀ (x : (ClBoundedJoinSemilatticeTerm A)) , (P x)))))
| psing ppluscl p0cl (sing x1) := (psing x1)
| psing ppluscl p0cl (plusCl x1 x2) := (ppluscl _ _ (inductionCl psing ppluscl p0cl x1) (inductionCl psing ppluscl p0cl x2))
| psing ppluscl p0cl zeroCl := p0cl
def inductionOpB {n : ℕ} {P : ((OpBoundedJoinSemilatticeTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpBoundedJoinSemilatticeTerm n)) , ((P x1) → ((P x2) → (P (plusOL x1 x2))))) → ((P zeroOL) → (∀ (x : (OpBoundedJoinSemilatticeTerm n)) , (P x)))))
| pv pplusol p0ol (v x1) := (pv x1)
| pv pplusol p0ol (plusOL x1 x2) := (pplusol _ _ (inductionOpB pv pplusol p0ol x1) (inductionOpB pv pplusol p0ol x2))
| pv pplusol p0ol zeroOL := p0ol
def inductionOp {n : ℕ} {A : Type} {P : ((OpBoundedJoinSemilatticeTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpBoundedJoinSemilatticeTerm2 n A)) , ((P x1) → ((P x2) → (P (plusOL2 x1 x2))))) → ((P zeroOL2) → (∀ (x : (OpBoundedJoinSemilatticeTerm2 n A)) , (P x))))))
| pv2 psing2 pplusol2 p0ol2 (v2 x1) := (pv2 x1)
| pv2 psing2 pplusol2 p0ol2 (sing2 x1) := (psing2 x1)
| pv2 psing2 pplusol2 p0ol2 (plusOL2 x1 x2) := (pplusol2 _ _ (inductionOp pv2 psing2 pplusol2 p0ol2 x1) (inductionOp pv2 psing2 pplusol2 p0ol2 x2))
| pv2 psing2 pplusol2 p0ol2 zeroOL2 := p0ol2
def stageB : (BoundedJoinSemilatticeTerm → (Staged BoundedJoinSemilatticeTerm))
| (plusL x1 x2) := (stage2 plusL (codeLift2 plusL) (stageB x1) (stageB x2))
| zeroL := (Now zeroL)
def stageCl {A : Type} : ((ClBoundedJoinSemilatticeTerm A) → (Staged (ClBoundedJoinSemilatticeTerm A)))
| (sing x1) := (Now (sing x1))
| (plusCl x1 x2) := (stage2 plusCl (codeLift2 plusCl) (stageCl x1) (stageCl x2))
| zeroCl := (Now zeroCl)
def stageOpB {n : ℕ} : ((OpBoundedJoinSemilatticeTerm n) → (Staged (OpBoundedJoinSemilatticeTerm n)))
| (v x1) := (const (code (v x1)))
| (plusOL x1 x2) := (stage2 plusOL (codeLift2 plusOL) (stageOpB x1) (stageOpB x2))
| zeroOL := (Now zeroOL)
def stageOp {n : ℕ} {A : Type} : ((OpBoundedJoinSemilatticeTerm2 n A) → (Staged (OpBoundedJoinSemilatticeTerm2 n A)))
| (sing2 x1) := (Now (sing2 x1))
| (v2 x1) := (const (code (v2 x1)))
| (plusOL2 x1 x2) := (stage2 plusOL2 (codeLift2 plusOL2) (stageOp x1) (stageOp x2))
| zeroOL2 := (Now zeroOL2)
structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type :=
(plusT : ((Repr A) → ((Repr A) → (Repr A))))
(zeroT : (Repr A))
end BoundedJoinSemilattice |
87c06d0887d1a2d77e9b9b867609c2b163a77f37 | ba4794a0deca1d2aaa68914cd285d77880907b5c | /src/game/world10/level9.lean | 31dfd2fcf10dd273e14e88ea84eaa07612d66537 | [
"Apache-2.0"
] | permissive | ChrisHughes24/natural_number_game | c7c00aa1f6a95004286fd456ed13cf6e113159ce | 9d09925424da9f6275e6cfe427c8bcf12bb0944f | refs/heads/master | 1,600,715,773,528 | 1,573,910,462,000 | 1,573,910,462,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 629 | lean | import game.world10.level8 -- hide
namespace mynat -- hide
/-
# Inequality world.
## Level 9: `le_total`
-/
/- Lemma
For all naturals $a$ and $b$, either $a\le b$ or $b\le a$.
-/
theorem le_total (a b : mynat) : a ≤ b ∨ b ≤ a :=
begin [less_leaky]
revert a,
induction b with d hd,
intro a,
right,
exact zero_le a,
intro a,
cases a with a,
left,
exact zero_le _,
cases hd a,
left,
exact succ_le_succ a d h,
right,
exact succ_le_succ d a h,
end
-- Another collectible: the naturals are a linear order.
instance : linear_order mynat := by structure_helper
end mynat -- hide
|
43c23334a05f8f0c3bc2613e40bdcf83b44797a2 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/group_theory/group_action/conj_act.lean | 54cf1c4e853126a4d024638fdf26557118bbde6e | [
"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,686 | lean | /-
Copyright (c) 2021 . All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import group_theory.group_action.basic
import group_theory.subgroup.zpowers
import algebra.group_ring_action.basic
/-!
# Conjugation action of a group on itself
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines the conjugation action of a group on itself. See also `mul_aut.conj` for
the definition of conjugation as a homomorphism into the automorphism group.
## Main definitions
A type alias `conj_act G` is introduced for a group `G`. The group `conj_act G` acts on `G`
by conjugation. The group `conj_act G` also acts on any normal subgroup of `G` by conjugation.
As a generalization, this also allows:
* `conj_act Mˣ` to act on `M`, when `M` is a `monoid`
* `conj_act G₀` to act on `G₀`, when `G₀` is a `group_with_zero`
## Implementation Notes
The scalar action in defined in this file can also be written using `mul_aut.conj g • h`. This
has the advantage of not using the type alias `conj_act`, but the downside of this approach
is that some theorems about the group actions will not apply when since this
`mul_aut.conj g • h` describes an action of `mul_aut G` on `G`, and not an action of `G`.
-/
variables (α M G G₀ R K : Type*)
/-- A type alias for a group `G`. `conj_act G` acts on `G` by conjugation -/
def conj_act : Type* := G
namespace conj_act
open mul_action subgroup
variables {M G G₀ R K}
instance : Π [group G], group (conj_act G) := id
instance : Π [div_inv_monoid G], div_inv_monoid (conj_act G) := id
instance : Π [group_with_zero G], group_with_zero (conj_act G) := id
instance : Π [fintype G], fintype (conj_act G) := id
@[simp] lemma card [fintype G] : fintype.card (conj_act G) = fintype.card G := rfl
section div_inv_monoid
variable [div_inv_monoid G]
instance : inhabited (conj_act G) := ⟨1⟩
/-- Reinterpret `g : conj_act G` as an element of `G`. -/
def of_conj_act : conj_act G ≃* G := ⟨id, id, λ _, rfl, λ _, rfl, λ _ _, rfl⟩
/-- Reinterpret `g : G` as an element of `conj_act G`. -/
def to_conj_act : G ≃* conj_act G := of_conj_act.symm
/-- A recursor for `conj_act`, for use as `induction x using conj_act.rec` when `x : conj_act G`. -/
protected def rec {C : conj_act G → Sort*} (h : Π g, C (to_conj_act g)) : Π g, C g := h
@[simp] lemma «forall» (p : conj_act G → Prop) :
(∀ (x : conj_act G), p x) ↔ ∀ x : G, p (to_conj_act x) := iff.rfl
@[simp] lemma of_mul_symm_eq : (@of_conj_act G _).symm = to_conj_act := rfl
@[simp] lemma to_mul_symm_eq : (@to_conj_act G _).symm = of_conj_act := rfl
@[simp] lemma to_conj_act_of_conj_act (x : conj_act G) : to_conj_act (of_conj_act x) = x := rfl
@[simp] lemma of_conj_act_to_conj_act (x : G) : of_conj_act (to_conj_act x) = x := rfl
@[simp] lemma of_conj_act_one : of_conj_act (1 : conj_act G) = 1 := rfl
@[simp] lemma to_conj_act_one : to_conj_act (1 : G) = 1 := rfl
@[simp] lemma of_conj_act_inv (x : conj_act G) : of_conj_act (x⁻¹) = (of_conj_act x)⁻¹ := rfl
@[simp] lemma to_conj_act_inv (x : G) : to_conj_act (x⁻¹) = (to_conj_act x)⁻¹ := rfl
@[simp] lemma of_conj_act_mul (x y : conj_act G) :
of_conj_act (x * y) = of_conj_act x * of_conj_act y := rfl
@[simp] lemma to_conj_act_mul (x y : G) : to_conj_act (x * y) =
to_conj_act x * to_conj_act y := rfl
instance : has_smul (conj_act G) G :=
{ smul := λ g h, of_conj_act g * h * (of_conj_act g)⁻¹ }
lemma smul_def (g : conj_act G) (h : G) : g • h = of_conj_act g * h * (of_conj_act g)⁻¹ := rfl
end div_inv_monoid
section units
section monoid
variables [monoid M]
instance has_units_scalar : has_smul (conj_act Mˣ) M :=
{ smul := λ g h, of_conj_act g * h * ↑(of_conj_act g)⁻¹ }
lemma units_smul_def (g : conj_act Mˣ) (h : M) : g • h = of_conj_act g * h * ↑(of_conj_act g)⁻¹ :=
rfl
instance units_mul_distrib_mul_action : mul_distrib_mul_action (conj_act Mˣ) M :=
{ smul := (•),
one_smul := by simp [units_smul_def],
mul_smul := by simp [units_smul_def, mul_assoc, mul_inv_rev],
smul_mul := by simp [units_smul_def, mul_assoc],
smul_one := by simp [units_smul_def], }
instance units_smul_comm_class [has_smul α M] [smul_comm_class α M M] [is_scalar_tower α M M] :
smul_comm_class α (conj_act Mˣ) M :=
{ smul_comm := λ a um m, by rw [units_smul_def, units_smul_def, mul_smul_comm, smul_mul_assoc] }
instance units_smul_comm_class' [has_smul α M] [smul_comm_class M α M] [is_scalar_tower α M M] :
smul_comm_class (conj_act Mˣ) α M :=
by { haveI : smul_comm_class α M M := smul_comm_class.symm _ _ _, exact smul_comm_class.symm _ _ _ }
end monoid
section semiring
variables [semiring R]
instance units_mul_semiring_action : mul_semiring_action (conj_act Rˣ) R :=
{ smul := (•),
smul_zero := by simp [units_smul_def],
smul_add := by simp [units_smul_def, mul_add, add_mul],
..conj_act.units_mul_distrib_mul_action}
end semiring
end units
section group_with_zero
variable [group_with_zero G₀]
@[simp] lemma of_conj_act_zero : of_conj_act (0 : conj_act G₀) = 0 := rfl
@[simp] lemma to_conj_act_zero : to_conj_act (0 : G₀) = 0 := rfl
instance mul_action₀ : mul_action (conj_act G₀) G₀ :=
{ smul := (•),
one_smul := by simp [smul_def],
mul_smul := by simp [smul_def, mul_assoc, mul_inv_rev] }
instance smul_comm_class₀ [has_smul α G₀] [smul_comm_class α G₀ G₀] [is_scalar_tower α G₀ G₀] :
smul_comm_class α (conj_act G₀) G₀ :=
{ smul_comm := λ a ug g, by rw [smul_def, smul_def, mul_smul_comm, smul_mul_assoc] }
instance smul_comm_class₀' [has_smul α G₀] [smul_comm_class G₀ α G₀] [is_scalar_tower α G₀ G₀] :
smul_comm_class (conj_act G₀) α G₀ :=
by { haveI := smul_comm_class.symm G₀ α G₀, exact smul_comm_class.symm _ _ _ }
end group_with_zero
section division_ring
variables [division_ring K]
instance distrib_mul_action₀ : distrib_mul_action (conj_act K) K :=
{ smul := (•),
smul_zero := by simp [smul_def],
smul_add := by simp [smul_def, mul_add, add_mul],
..conj_act.mul_action₀ }
end division_ring
variables [group G]
instance : mul_distrib_mul_action (conj_act G) G :=
{ smul := (•),
smul_mul := by simp [smul_def, mul_assoc],
smul_one := by simp [smul_def],
one_smul := by simp [smul_def],
mul_smul := by simp [smul_def, mul_assoc] }
instance smul_comm_class [has_smul α G] [smul_comm_class α G G] [is_scalar_tower α G G] :
smul_comm_class α (conj_act G) G :=
{ smul_comm := λ a ug g, by rw [smul_def, smul_def, mul_smul_comm, smul_mul_assoc] }
instance smul_comm_class' [has_smul α G] [smul_comm_class G α G] [is_scalar_tower α G G] :
smul_comm_class (conj_act G) α G :=
by { haveI := smul_comm_class.symm G α G, exact smul_comm_class.symm _ _ _ }
lemma smul_eq_mul_aut_conj (g : conj_act G) (h : G) : g • h = mul_aut.conj (of_conj_act g) h := rfl
/-- The set of fixed points of the conjugation action of `G` on itself is the center of `G`. -/
lemma fixed_points_eq_center : fixed_points (conj_act G) G = center G :=
begin
ext x,
simp [mem_center_iff, smul_def, mul_inv_eq_iff_eq_mul]
end
@[simp] lemma mem_orbit_conj_act {g h : G} : g ∈ orbit (conj_act G) h ↔ is_conj g h :=
by { rw [is_conj_comm, is_conj_iff, mem_orbit_iff], refl }
lemma orbit_rel_conj_act : (orbit_rel (conj_act G) G).rel = is_conj :=
funext₂ $ λ g h, by rw [orbit_rel_apply, mem_orbit_conj_act]
lemma stabilizer_eq_centralizer (g : G) :
stabilizer (conj_act G) g = centralizer (zpowers (to_conj_act g) : set (conj_act G)) :=
le_antisymm (le_centralizer_iff.mp (zpowers_le.mpr (λ x, mul_inv_eq_iff_eq_mul.mp)))
(λ x h, mul_inv_eq_of_eq_mul (h g (mem_zpowers g)).symm)
/-- As normal subgroups are closed under conjugation, they inherit the conjugation action
of the underlying group. -/
instance subgroup.conj_action {H : subgroup G} [hH : H.normal] :
has_smul (conj_act G) H :=
⟨λ g h, ⟨g • h, hH.conj_mem h.1 h.2 (of_conj_act g)⟩⟩
lemma subgroup.coe_conj_smul {H : subgroup G} [hH : H.normal] (g : conj_act G) (h : H) :
↑(g • h) = g • (h : G) := rfl
instance subgroup.conj_mul_distrib_mul_action {H : subgroup G} [hH : H.normal] :
mul_distrib_mul_action (conj_act G) H :=
(subtype.coe_injective).mul_distrib_mul_action H.subtype subgroup.coe_conj_smul
/-- Group conjugation on a normal subgroup. Analogous to `mul_aut.conj`. -/
def _root_.mul_aut.conj_normal {H : subgroup G} [hH : H.normal] : G →* mul_aut H :=
(mul_distrib_mul_action.to_mul_aut (conj_act G) H).comp to_conj_act.to_monoid_hom
@[simp] lemma _root_.mul_aut.conj_normal_apply {H : subgroup G} [H.normal] (g : G) (h : H) :
↑(mul_aut.conj_normal g h) = g * h * g⁻¹ := rfl
@[simp] lemma _root_.mul_aut.conj_normal_symm_apply {H : subgroup G} [H.normal] (g : G) (h : H) :
↑((mul_aut.conj_normal g).symm h) = g⁻¹ * h * g :=
by { change _ * (_)⁻¹⁻¹ = _, rw inv_inv, refl }
@[simp] lemma _root_.mul_aut.conj_normal_inv_apply {H : subgroup G} [H.normal] (g : G) (h : H) :
↑((mul_aut.conj_normal g)⁻¹ h) = g⁻¹ * h * g :=
mul_aut.conj_normal_symm_apply g h
lemma _root_.mul_aut.conj_normal_coe {H : subgroup G} [H.normal] {h : H} :
mul_aut.conj_normal ↑h = mul_aut.conj h :=
mul_equiv.ext (λ x, rfl)
instance normal_of_characteristic_of_normal {H : subgroup G} [hH : H.normal]
{K : subgroup H} [h : K.characteristic] : (K.map H.subtype).normal :=
⟨λ a ha b, by
{ obtain ⟨a, ha, rfl⟩ := ha,
exact K.apply_coe_mem_map H.subtype
⟨_, ((set_like.ext_iff.mp (h.fixed (mul_aut.conj_normal b)) a).mpr ha)⟩ }⟩
end conj_act
|
046e59cda32ee31ac865f66d14221fce180e3026 | ee8cdbabf07f77e7be63a449b8483ce308d37218 | /lean/src/test/mathb-algebra-44.lean | 14dd8d2119ec2de746d92b0dfd8d341251131505 | [
"Apache-2.0",
"MIT"
] | permissive | zeta1999/miniF2F | 6d66c75d1c18152e224d07d5eed57624f731d4b7 | c1ba9629559c5273c92ec226894baa0c1ce27861 | refs/heads/main | 1,681,897,460,642 | 1,620,646,361,000 | 1,620,646,361,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 273 | lean | /-
Copyright (c) 2021 OpenAI. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kunhao Zheng
-/
import data.real.basic
example (s t : ℝ) (h₀ : s = 9 - 2 * t) (h₁ : t = 3 * s + 1) : s = 1 ∧ t = 4 :=
begin
sorry
end
|
ce743f263b65532598ea2a3bd918831cc3f02a49 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/ring/pi.lean | b56dbf7fafd417a03194ca4736d4e53396364397 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 7,758 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Patrick Massot
-/
import tactic.pi_instances
import algebra.group.pi
import algebra.hom.ring
/-!
# Pi instances for ring
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines instances for ring, semiring and related structures on Pi Types
-/
namespace pi
universes u v w
variable {I : Type u} -- The indexing type
variable {f : I → Type v} -- The family of types already equipped with instances
variables (x y : Π i, f i) (i : I)
instance distrib [Π i, distrib $ f i] : distrib (Π i : I, f i) :=
by refine_struct { add := (+), mul := (*), .. }; tactic.pi_instance_derive_field
instance non_unital_non_assoc_semiring [∀ i, non_unital_non_assoc_semiring $ f i] :
non_unital_non_assoc_semiring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), .. };
tactic.pi_instance_derive_field
instance non_unital_semiring [∀ i, non_unital_semiring $ f i] :
non_unital_semiring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), .. };
tactic.pi_instance_derive_field
instance non_assoc_semiring [∀ i, non_assoc_semiring $ f i] :
non_assoc_semiring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), .. };
tactic.pi_instance_derive_field
instance semiring [∀ i, semiring $ f i] : semiring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*),
nsmul := add_monoid.nsmul, npow := monoid.npow };
tactic.pi_instance_derive_field
instance non_unital_comm_semiring [∀ i, non_unital_comm_semiring $ f i] :
non_unital_comm_semiring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), nsmul := add_monoid.nsmul };
tactic.pi_instance_derive_field
instance comm_semiring [∀ i, comm_semiring $ f i] : comm_semiring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*),
nsmul := add_monoid.nsmul, npow := monoid.npow };
tactic.pi_instance_derive_field
instance non_unital_non_assoc_ring [∀ i, non_unital_non_assoc_ring $ f i] :
non_unital_non_assoc_ring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*),
neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul };
tactic.pi_instance_derive_field
instance non_unital_ring [∀ i, non_unital_ring $ f i] :
non_unital_ring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*),
neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul };
tactic.pi_instance_derive_field
instance non_assoc_ring [∀ i, non_assoc_ring $ f i] :
non_assoc_ring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*),
neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul };
tactic.pi_instance_derive_field
instance ring [∀ i, ring $ f i] : ring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*),
neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul,
npow := monoid.npow };
tactic.pi_instance_derive_field
instance non_unital_comm_ring [∀ i, non_unital_comm_ring $ f i] :
non_unital_comm_ring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), neg := has_neg.neg,
nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul };
tactic.pi_instance_derive_field
instance comm_ring [∀ i, comm_ring $ f i] : comm_ring (Π i : I, f i) :=
by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*),
neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul,
npow := monoid.npow };
tactic.pi_instance_derive_field
/-- A family of non-unital ring homomorphisms `f a : γ →ₙ+* β a` defines a non-unital ring
homomorphism `pi.non_unital_ring_hom f : γ →+* Π a, β a` given by
`pi.non_unital_ring_hom f x b = f b x`. -/
@[simps]
protected def non_unital_ring_hom {γ : Type w} [Π i, non_unital_non_assoc_semiring (f i)]
[non_unital_non_assoc_semiring γ] (g : Π i, γ →ₙ+* f i) : γ →ₙ+* Π i, f i :=
{ to_fun := λ x b, g b x,
.. pi.mul_hom (λ i, (g i).to_mul_hom),
.. pi.add_monoid_hom (λ i, (g i).to_add_monoid_hom) }
lemma non_unital_ring_hom_injective {γ : Type w} [nonempty I]
[Π i, non_unital_non_assoc_semiring (f i)] [non_unital_non_assoc_semiring γ] (g : Π i, γ →ₙ+* f i)
(hg : ∀ i, function.injective (g i)) : function.injective (pi.non_unital_ring_hom g) :=
mul_hom_injective (λ i, (g i).to_mul_hom) hg
/-- A family of ring homomorphisms `f a : γ →+* β a` defines a ring homomorphism
`pi.ring_hom f : γ →+* Π a, β a` given by `pi.ring_hom f x b = f b x`. -/
@[simps]
protected def ring_hom {γ : Type w} [Π i, non_assoc_semiring (f i)] [non_assoc_semiring γ]
(g : Π i, γ →+* f i) : γ →+* Π i, f i :=
{ to_fun := λ x b, g b x,
.. pi.monoid_hom (λ i, (g i).to_monoid_hom),
.. pi.add_monoid_hom (λ i, (g i).to_add_monoid_hom) }
lemma ring_hom_injective {γ : Type w} [nonempty I] [Π i, non_assoc_semiring (f i)]
[non_assoc_semiring γ] (g : Π i, γ →+* f i) (hg : ∀ i, function.injective (g i)) :
function.injective (pi.ring_hom g) :=
monoid_hom_injective (λ i, (g i).to_monoid_hom) hg
end pi
section non_unital_ring_hom
universes u v
variable {I : Type u}
/-- Evaluation of functions into an indexed collection of non-unital rings at a point is a
non-unital ring homomorphism. This is `function.eval` as a `non_unital_ring_hom`. -/
@[simps]
def pi.eval_non_unital_ring_hom (f : I → Type v)
[Π i, non_unital_non_assoc_semiring (f i)] (i : I) : (Π i, f i) →ₙ+* f i :=
{ ..(pi.eval_mul_hom f i),
..(pi.eval_add_monoid_hom f i) }
/-- `function.const` as a `non_unital_ring_hom`. -/
@[simps]
def pi.const_non_unital_ring_hom (α β : Type*) [non_unital_non_assoc_semiring β] : β →ₙ+* (α → β) :=
{ to_fun := function.const _,
.. pi.non_unital_ring_hom (λ _, non_unital_ring_hom.id β) }
/-- Non-unital ring homomorphism between the function spaces `I → α` and `I → β`, induced by a
non-unital ring homomorphism `f` between `α` and `β`. -/
@[simps] protected def non_unital_ring_hom.comp_left {α β : Type*} [non_unital_non_assoc_semiring α]
[non_unital_non_assoc_semiring β] (f : α →ₙ+* β) (I : Type*) :
(I → α) →ₙ+* (I → β) :=
{ to_fun := λ h, f ∘ h,
.. f.to_mul_hom.comp_left I,
.. f.to_add_monoid_hom.comp_left I }
end non_unital_ring_hom
section ring_hom
universes u v
variable {I : Type u}
/-- Evaluation of functions into an indexed collection of rings at a point is a ring
homomorphism. This is `function.eval` as a `ring_hom`. -/
@[simps]
def pi.eval_ring_hom (f : I → Type v) [Π i, non_assoc_semiring (f i)] (i : I) :
(Π i, f i) →+* f i :=
{ ..(pi.eval_monoid_hom f i),
..(pi.eval_add_monoid_hom f i) }
/-- `function.const` as a `ring_hom`. -/
@[simps]
def pi.const_ring_hom (α β : Type*) [non_assoc_semiring β] : β →+* (α → β) :=
{ to_fun := function.const _,
.. pi.ring_hom (λ _, ring_hom.id β) }
/-- Ring homomorphism between the function spaces `I → α` and `I → β`, induced by a ring
homomorphism `f` between `α` and `β`. -/
@[simps] protected def ring_hom.comp_left {α β : Type*} [non_assoc_semiring α]
[non_assoc_semiring β] (f : α →+* β) (I : Type*) :
(I → α) →+* (I → β) :=
{ to_fun := λ h, f ∘ h,
.. f.to_monoid_hom.comp_left I,
.. f.to_add_monoid_hom.comp_left I }
end ring_hom
|
d744d4f9d1f12920849efdae33b4cde37ec342eb | 1b8f093752ba748c5ca0083afef2959aaa7dace5 | /src/category_theory/tactics/obviously.lean | e443f7ecd20d75bccc0b8c2f9a12861533fccb05 | [] | no_license | khoek/lean-category-theory | 7ec4cda9cc64a5a4ffeb84712ac7d020dbbba386 | 63dcb598e9270a3e8b56d1769eb4f825a177cd95 | refs/heads/master | 1,585,251,725,759 | 1,539,344,445,000 | 1,539,344,445,000 | 145,281,070 | 0 | 0 | null | 1,534,662,376,000 | 1,534,662,376,000 | null | UTF-8 | Lean | false | false | 306 | lean | import category_theory.natural_isomorphism
import category_theory.products
import category_theory.types
import category_theory.embedding
import tidy.tidy
open category_theory
@[suggest] def use_category_theory := `category_theory
attribute [back'] full.preimage
attribute [forward] faithful.injectivity
|
f83610710ab8cd52da483db2a160edffe4cad99f | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/data/equiv/set.lean | 032a262236eb012f67b06c194a22dfd9e92a2828 | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 23,018 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Mario Carneiro
-/
import data.equiv.basic
import data.set.function
/-!
# Equivalences and sets
In this file we provide lemmas linking equivalences to sets.
Some notable definitions are:
* `equiv.of_injective`: an injective function is (noncomputably) equivalent to its range.
* `equiv.set_congr`: two equal sets are equivalent as types.
* `equiv.set.union`: a disjoint union of sets is equivalent to their `sum`.
This file is separate from `equiv/basic` such that we do not require the full lattice structure
on sets before defining what an equivalence is.
-/
open function
universes u v w z
variables {α : Sort u} {β : Sort v} {γ : Sort w}
namespace equiv
@[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : set.range e = set.univ :=
set.eq_univ_of_forall e.surjective
protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s :=
set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv
lemma _root_.set.mem_image_equiv {α β} {S : set α} {f : α ≃ β} {x : β} :
x ∈ f '' S ↔ f.symm x ∈ S :=
set.ext_iff.mp (f.image_eq_preimage S) x
/-- Alias for `equiv.image_eq_preimage` -/
lemma _root_.set.image_equiv_eq_preimage_symm {α β} (S : set α) (f : α ≃ β) :
f '' S = f.symm ⁻¹' S :=
f.image_eq_preimage S
/-- Alias for `equiv.image_eq_preimage` -/
lemma _root_.set.preimage_equiv_eq_image_symm {α β} (S : set α) (f : β ≃ α) :
f ⁻¹' S = f.symm '' S :=
(f.symm.image_eq_preimage S).symm
@[simp] protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) :
e.symm '' t ⊆ s ↔ t ⊆ e '' s :=
by rw [set.image_subset_iff, e.image_eq_preimage]
@[simp] protected lemma subset_image' {α β} (e : α ≃ β) (s : set α) (t : set β) :
s ⊆ e.symm '' t ↔ e '' s ⊆ t :=
calc s ⊆ e.symm '' t ↔ e.symm.symm '' s ⊆ t : by rw e.symm.subset_image
... ↔ e '' s ⊆ t : by rw e.symm_symm
@[simp] lemma symm_image_image {α β} (e : α ≃ β) (s : set α) : e.symm '' (e '' s) = s :=
e.left_inverse_symm.image_image s
lemma eq_image_iff_symm_image_eq {α β} (e : α ≃ β) (s : set α) (t : set β) :
t = e '' s ↔ e.symm '' t = s :=
(e.symm.injective.image_injective.eq_iff' (e.symm_image_image s)).symm
@[simp] lemma image_symm_image {α β} (e : α ≃ β) (s : set β) : e '' (e.symm '' s) = s :=
e.symm.symm_image_image s
@[simp] lemma image_preimage {α β} (e : α ≃ β) (s : set β) : e '' (e ⁻¹' s) = s :=
e.surjective.image_preimage s
@[simp] lemma preimage_image {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e '' s) = s :=
e.injective.preimage_image s
protected lemma image_compl {α β} (f : equiv α β) (s : set α) :
f '' sᶜ = (f '' s)ᶜ :=
set.image_compl_eq f.bijective
@[simp] lemma symm_preimage_preimage {α β} (e : α ≃ β) (s : set β) :
e.symm ⁻¹' (e ⁻¹' s) = s :=
e.right_inverse_symm.preimage_preimage s
@[simp] lemma preimage_symm_preimage {α β} (e : α ≃ β) (s : set α) :
e ⁻¹' (e.symm ⁻¹' s) = s :=
e.left_inverse_symm.preimage_preimage s
@[simp] lemma preimage_subset {α β} (e : α ≃ β) (s t : set β) : e ⁻¹' s ⊆ e ⁻¹' t ↔ s ⊆ t :=
e.surjective.preimage_subset_preimage_iff
@[simp] lemma image_subset {α β} (e : α ≃ β) (s t : set α) : e '' s ⊆ e '' t ↔ s ⊆ t :=
set.image_subset_image_iff e.injective
@[simp] lemma image_eq_iff_eq {α β} (e : α ≃ β) (s t : set α) : e '' s = e '' t ↔ s = t :=
set.image_eq_image e.injective
lemma preimage_eq_iff_eq_image {α β} (e : α ≃ β) (s t) : e ⁻¹' s = t ↔ s = e '' t :=
set.preimage_eq_iff_eq_image e.bijective
lemma eq_preimage_iff_image_eq {α β} (e : α ≃ β) (s t) : s = e ⁻¹' t ↔ e '' s = t :=
set.eq_preimage_iff_image_eq e.bijective
lemma prod_assoc_preimage {α β γ} {s : set α} {t : set β} {u : set γ} :
equiv.prod_assoc α β γ ⁻¹' s.prod (t.prod u) = (s.prod t).prod u :=
by { ext, simp [and_assoc] }
/-- A set `s` in `α × β` is equivalent to the sigma-type `Σ x, {y | (x, y) ∈ s}`. -/
def set_prod_equiv_sigma {α β : Type*} (s : set (α × β)) :
s ≃ Σ x : α, {y | (x, y) ∈ s} :=
{ to_fun := λ x, ⟨x.1.1, x.1.2, by simp⟩,
inv_fun := λ x, ⟨(x.1, x.2.1), x.2.2⟩,
left_inv := λ ⟨⟨x, y⟩, h⟩, rfl,
right_inv := λ ⟨x, y, h⟩, rfl }
/-- The subtypes corresponding to equal sets are equivalent. -/
@[simps apply]
def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t :=
subtype_equiv_prop h
/--
A set is equivalent to its image under an equivalence.
-/
-- We could construct this using `equiv.set.image e s e.injective`,
-- but this definition provides an explicit inverse.
@[simps]
def image {α β : Type*} (e : α ≃ β) (s : set α) : s ≃ e '' s :=
{ to_fun := λ x, ⟨e x.1, by simp⟩,
inv_fun := λ y, ⟨e.symm y.1, by { rcases y with ⟨-, ⟨a, ⟨m, rfl⟩⟩⟩, simpa using m, }⟩,
left_inv := λ x, by simp,
right_inv := λ y, by simp, }.
open set
namespace set
/-- `univ α` is equivalent to `α`. -/
@[simps apply symm_apply]
protected def univ (α) : @univ α ≃ α :=
⟨coe, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩
/-- An empty set is equivalent to the `empty` type. -/
protected def empty (α) : (∅ : set α) ≃ empty :=
equiv_empty _
/-- An empty set is equivalent to a `pempty` type. -/
protected def pempty (α) : (∅ : set α) ≃ pempty :=
equiv_pempty _
/-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to
`s ⊕ t`. -/
protected def union' {α} {s t : set α}
(p : α → Prop) [decidable_pred p]
(hs : ∀ x ∈ s, p x)
(ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t :=
{ to_fun := λ x, if hp : p x
then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩
else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩,
inv_fun := λ o, match o with
| (sum.inl x) := ⟨x, or.inl x.2⟩
| (sum.inr x) := ⟨x, or.inr x.2⟩
end,
left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr,
right_inv := λ o, begin
rcases o with ⟨x, h⟩ | ⟨x, h⟩;
dsimp [union'._match_1];
[simp [hs _ h], simp [ht _ h]]
end }
/-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/
protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) :
(s ∪ t : set α) ≃ s ⊕ t :=
set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩)
lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅)
{a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ :=
dif_pos ha
lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅)
{a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ :=
dif_neg $ λ h, H ⟨h, ha⟩
@[simp] lemma union_symm_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅)
(a : s) : (equiv.set.union H).symm (sum.inl a) = ⟨a, subset_union_left _ _ a.2⟩ :=
rfl
@[simp] lemma union_symm_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅)
(a : t) : (equiv.set.union H).symm (sum.inr a) = ⟨a, subset_union_right _ _ a.2⟩ :=
rfl
/-- A singleton set is equivalent to a `punit` type. -/
protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} :=
⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩,
λ ⟨x, h⟩, by { simp at h, subst x },
λ ⟨⟩, rfl⟩
/-- Equal sets are equivalent. -/
@[simps apply symm_apply]
protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t :=
{ to_fun := λ x, ⟨x, h ▸ x.2⟩,
inv_fun := λ x, ⟨x, h.symm ▸ x.2⟩,
left_inv := λ _, subtype.eq rfl,
right_inv := λ _, subtype.eq rfl }
/-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/
protected def insert {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) :
(insert a s : set α) ≃ s ⊕ punit.{u+1} :=
calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp)
... ≃ s ⊕ ({a} : set α) : equiv.set.union (by finish [set.subset_def])
... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _)
@[simp] lemma insert_symm_apply_inl {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s)
(b : s) : (equiv.set.insert H).symm (sum.inl b) = ⟨b, or.inr b.2⟩ :=
rfl
@[simp] lemma insert_symm_apply_inr {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s)
(b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = ⟨a, or.inl rfl⟩ :=
rfl
@[simp] lemma insert_apply_left {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) :
equiv.set.insert H ⟨a, or.inl rfl⟩ = sum.inr punit.star :=
(equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl
@[simp] lemma insert_apply_right {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s)
(b : s) : equiv.set.insert H ⟨b, or.inr b.2⟩ = sum.inl b :=
(equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl
/-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/
protected def sum_compl {α} (s : set α) [decidable_pred (∈ s)] : s ⊕ (sᶜ : set α) ≃ α :=
calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm
... ≃ @univ α : equiv.set.of_eq (by simp)
... ≃ α : equiv.set.univ _
@[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : s) :
equiv.set.sum_compl s (sum.inl x) = x := rfl
@[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : sᶜ) :
equiv.set.sum_compl s (sum.inr x) = x := rfl
lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α}
(hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ :=
have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx,
by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this }
lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α}
(hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ :=
have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx,
by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this }
@[simp] lemma sum_compl_symm_apply {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : s} :
(equiv.set.sum_compl s).symm x = sum.inl x :=
by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx
@[simp] lemma sum_compl_symm_apply_compl {α : Type*} {s : set α}
[decidable_pred (∈ s)] {x : sᶜ} : (equiv.set.sum_compl s).symm x = sum.inr x :=
by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx
/-- `sum_diff_subset s t` is the natural equivalence between
`s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/
protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] :
s ⊕ (t \ s : set α) ≃ t :=
calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) :
(equiv.set.union (by simp [inter_diff_self])).symm
... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] })
@[simp] lemma sum_diff_subset_apply_inl
{α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : s) :
equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl
@[simp] lemma sum_diff_subset_apply_inr
{α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : t \ s) :
equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl
lemma sum_diff_subset_symm_apply_of_mem
{α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∈ s) :
(equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ :=
begin
apply (equiv.set.sum_diff_subset h).injective,
simp only [apply_symm_apply, sum_diff_subset_apply_inl],
exact subtype.eq rfl,
end
lemma sum_diff_subset_symm_apply_of_not_mem
{α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∉ s) :
(equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ :=
begin
apply (equiv.set.sum_diff_subset h).injective,
simp only [apply_symm_apply, sum_diff_subset_apply_inr],
exact subtype.eq rfl,
end
/-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent
to `s ⊕ t`. -/
protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred (∈ s)] :
(s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t :=
calc (s ∪ t : set α) ⊕ (s ∩ t : set α)
≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self]
... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) :
sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _)
... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _
... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin
refine (set.union' (∉ s) _ _).symm,
exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1]
end
... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] }
/-- Given an equivalence `e₀` between sets `s : set α` and `t : set β`, the set of equivalences
`e : α ≃ β` such that `e ↑x = ↑(e₀ x)` for each `x : s` is equivalent to the set of equivalences
between `sᶜ` and `tᶜ`. -/
protected def compl {α : Type u} {β : Type v} {s : set α} {t : set β} [decidable_pred (∈ s)]
[decidable_pred (∈ t)] (e₀ : s ≃ t) :
{e : α ≃ β // ∀ x : s, e x = e₀ x} ≃ ((sᶜ : set α) ≃ (tᶜ : set β)) :=
{ to_fun := λ e, subtype_equiv e
(λ a, not_congr $ iff.symm $ maps_to.mem_iff
(maps_to_iff_exists_map_subtype.2 ⟨e₀, e.2⟩)
(surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2
⟨t, e₀, subset.refl t, e₀.surjective, e.2⟩) e.1.injective)),
inv_fun := λ e₁,
subtype.mk
(calc α ≃ s ⊕ (sᶜ : set α) : (set.sum_compl s).symm
... ≃ t ⊕ (tᶜ : set β) : e₀.sum_congr e₁
... ≃ β : set.sum_compl t)
(λ x, by simp only [sum.map_inl, trans_apply, sum_congr_apply,
set.sum_compl_apply_inl, set.sum_compl_symm_apply]),
left_inv := λ e,
begin
ext x,
by_cases hx : x ∈ s,
{ simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩,
sum.map_inl, sum_congr_apply, trans_apply,
subtype.coe_mk, set.sum_compl_apply_inl] },
{ simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr,
subtype_equiv_apply, set.sum_compl_apply_inr, trans_apply,
sum_congr_apply, subtype.coe_mk] },
end,
right_inv := λ e, equiv.ext $ λ x, by simp only [sum.map_inr, subtype_equiv_apply,
set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans,
subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] }
/-- The set product of two sets is equivalent to the type product of their coercions to types. -/
protected def prod {α β} (s : set α) (t : set β) :
s.prod t ≃ s × t :=
@subtype_prod_equiv_prod α β s t
/-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/
protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) :
s ≃ (f '' s) :=
⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩,
λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩,
λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h
(classical.some_spec (mem_image_of_mem f h)).2),
λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩
/-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/
@[simps apply]
protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) :=
equiv.set.image_of_inj_on f s (H.inj_on s)
@[simp] protected lemma image_symm_apply {α β} (f : α → β) (s : set α) (H : injective f)
(x : α) (h : x ∈ s) :
(set.image f s H).symm ⟨f x, ⟨x, ⟨h, rfl⟩⟩⟩ = ⟨x, h⟩ :=
begin
apply (set.image f s H).injective,
simp [(set.image f s H).apply_symm_apply],
end
lemma image_symm_preimage {α β} {f : α → β} (hf : injective f) (u s : set α) :
(λ x, (set.image f s hf).symm x : f '' s → α) ⁻¹' u = coe ⁻¹' (f '' u) :=
begin
ext ⟨b, a, has, rfl⟩,
have : ∀(h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λ h, (classical.some_spec h).2,
simp [equiv.set.image, equiv.set.image_of_inj_on, hf.eq_iff, this],
end
/-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/
@[simps]
protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β :=
⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩
/-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/
protected def sep {α : Type u} (s : set α) (t : α → Prop) :
({ x ∈ s | t x } : set α) ≃ { x : s | t x } :=
(equiv.subtype_subtype_equiv_subtype_inter s t).symm
/-- The set `𝒫 S := {x | x ⊆ S}` is equivalent to the type `set S`. -/
protected def powerset {α} (S : set α) : 𝒫 S ≃ set S :=
{ to_fun := λ x : 𝒫 S, coe ⁻¹' (x : set α),
inv_fun := λ x : set S, ⟨coe '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩,
left_inv := λ x, by ext y; exact ⟨λ ⟨⟨_, _⟩, h, rfl⟩, h, λ h, ⟨⟨_, x.2 h⟩, h, rfl⟩⟩,
right_inv := λ x, by ext; simp }
/--
If `s` is a set in `range f`,
then its image under `range_splitting f` is in bijection (via `f`) with `s`.
-/
@[simps]
noncomputable def range_splitting_image_equiv {α β : Type*} (f : α → β) (s : set (range f)) :
range_splitting f '' s ≃ s :=
{ to_fun := λ x, ⟨⟨f x, by simp⟩,
(by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simpa [apply_range_splitting f] using m, })⟩,
inv_fun := λ x, ⟨range_splitting f x, ⟨x, ⟨x.2, rfl⟩⟩⟩,
left_inv := λ x, by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simp [apply_range_splitting f] },
right_inv := λ x, by simp [apply_range_splitting f], }
end set
/-- If `f : α → β` has a left-inverse when `α` is nonempty, then `α` is computably equivalent to the
range of `f`.
While awkward, the `nonempty α` hypothesis on `f_inv` and `hf` allows this to be used when `α` is
empty too. This hypothesis is absent on analogous definitions on stronger `equiv`s like
`linear_equiv.of_left_inverse` and `ring_equiv.of_left_inverse` as their typeclass assumptions
are already sufficient to ensure non-emptiness. -/
@[simps]
def of_left_inverse {α β : Sort*}
(f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) :
α ≃ set.range f :=
{ to_fun := λ a, ⟨f a, a, rfl⟩,
inv_fun := λ b, f_inv (nonempty_of_exists b.2) b,
left_inv := λ a, hf ⟨a⟩ a,
right_inv := λ ⟨b, a, ha⟩, subtype.eq $ show f (f_inv ⟨a⟩ b) = b,
from eq.trans (congr_arg f $ by exact ha ▸ (hf _ a)) ha }
/-- If `f : α → β` has a left-inverse, then `α` is computably equivalent to the range of `f`.
Note that if `α` is empty, no such `f_inv` exists and so this definition can't be used, unlike
the stronger but less convenient `of_left_inverse`. -/
abbreviation of_left_inverse' {α β : Sort*}
(f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) :
α ≃ set.range f :=
of_left_inverse f (λ _, f_inv) (λ _, hf)
/-- If `f : α → β` is an injective function, then domain `α` is equivalent to the range of `f`. -/
@[simps apply]
noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ set.range f :=
equiv.of_left_inverse f
(λ h, by exactI function.inv_fun f) (λ h, by exactI function.left_inverse_inv_fun hf)
theorem apply_of_injective_symm {α β} (f : α → β) (hf : injective f) (b : set.range f) :
f ((of_injective f hf).symm b) = b :=
subtype.ext_iff.1 $ (of_injective f hf).apply_symm_apply b
@[simp] theorem of_injective_symm_apply {α β} (f : α → β) (hf : injective f) (a : α) :
(of_injective f hf).symm ⟨f a, ⟨a, rfl⟩⟩ = a :=
begin
apply (of_injective f hf).injective,
simp [apply_of_injective_symm f hf],
end
@[simp] lemma self_comp_of_injective_symm {α β} (f : α → β) (hf : injective f) :
f ∘ ((of_injective f hf).symm) = coe :=
funext (λ x, apply_of_injective_symm f hf x)
lemma of_left_inverse_eq_of_injective {α β : Type*}
(f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) :
of_left_inverse f f_inv hf = of_injective f
((em (nonempty α)).elim (λ h, (hf h).injective) (λ h _ _ _, by {
haveI : subsingleton α := subsingleton_of_not_nonempty h, simp })) :=
by { ext, simp }
lemma of_left_inverse'_eq_of_injective {α β : Type*}
(f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) :
of_left_inverse' f f_inv hf = of_injective f hf.injective :=
by { ext, simp }
protected lemma set_forall_iff {α β} (e : α ≃ β) {p : set α → Prop} :
(∀ a, p a) ↔ (∀ a, p (e ⁻¹' a)) :=
by simpa [equiv.image_eq_preimage] using (equiv.set.congr e).forall_congr_left'
protected lemma preimage_sUnion {α β} (f : α ≃ β) {s : set (set β)} :
f ⁻¹' (⋃₀ s) = ⋃₀ (_root_.set.image f ⁻¹' s) :=
by { ext x, simp [(equiv.set.congr f).symm.exists_congr_left] }
end equiv
/-- If a function is a bijection between two sets `s` and `t`, then it induces an
equivalence between the types `↥s` and ``↥t`. -/
noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set α} {t : set β} (f : α → β)
(h : set.bij_on f s t) : s ≃ t :=
equiv.of_bijective _ h.bijective
/-- The composition of an updated function with an equiv on a subset can be expressed as an
updated function. -/
lemma dite_comp_equiv_update {α : Type*} {β : Sort*} {γ : Sort*} {s : set α} (e : β ≃ s)
(v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α]
[∀ j, decidable (j ∈ s)] :
(λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) =
function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x :=
begin
ext i,
by_cases h : i ∈ s,
{ rw [dif_pos h,
function.update_apply_equiv_apply, equiv.symm_symm, function.comp,
function.update_apply, function.update_apply,
dif_pos h],
have h_coe : (⟨i, h⟩ : s) = e j ↔ i = e j := subtype.ext_iff.trans (by rw subtype.coe_mk),
simp_rw h_coe,
congr, },
{ have : i ≠ e j,
by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this },
simp [h, this] }
end
|
003c8d2100bbafb39b1910164ee1ec516ed65fe0 | 02005f45e00c7ecf2c8ca5db60251bd1e9c860b5 | /src/data/mv_polynomial/equiv.lean | 268a80340660b142d865f69836f0d75e2820a4e8 | [
"Apache-2.0"
] | permissive | anthony2698/mathlib | 03cd69fe5c280b0916f6df2d07c614c8e1efe890 | 407615e05814e98b24b2ff322b14e8e3eb5e5d67 | refs/heads/master | 1,678,792,774,873 | 1,614,371,563,000 | 1,614,371,563,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,660 | 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, Johan Commelin, Mario Carneiro
-/
import data.mv_polynomial.rename
import data.equiv.fin
/-!
# Equivalences between polynomial rings
This file establishes a number of equivalences between polynomial rings,
based on equivalences between the underlying types.
## Notation
As in other polynomial files, we typically use the notation:
+ `σ : Type*` (indexing the variables)
+ `R : Type*` `[comm_semiring R]` (the coefficients)
+ `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set.
This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s`
+ `a : R`
+ `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians
+ `p : mv_polynomial σ R`
## Tags
equivalence, isomorphism, morphism, ring hom, hom
-/
noncomputable theory
open_locale classical big_operators
open set function finsupp add_monoid_algebra
universes u v w x
variables {R : Type u} {S₁ : Type v} {S₂ : Type w} {S₃ : Type x}
namespace mv_polynomial
variables {σ : Type*} {a a' a₁ a₂ : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ}
section equiv
variables (R) [comm_semiring R]
/-- The ring isomorphism between multivariable polynomials in no variables and the ground ring. -/
@[simps]
def pempty_ring_equiv : mv_polynomial pempty R ≃+* R :=
{ to_fun := mv_polynomial.eval₂ (ring_hom.id _) $ pempty.elim,
inv_fun := C,
left_inv := is_id (C.comp (eval₂_hom (ring_hom.id _) pempty.elim))
(assume a : R, by { dsimp, rw [eval₂_C], refl }) (assume a, a.elim),
right_inv := λ r, eval₂_C _ _ _,
map_mul' := λ _ _, eval₂_mul _ _,
map_add' := λ _ _, eval₂_add _ _ }
/-- The algebra isomorphism between multivariable polynomials in no variables
and the ground ring. -/
@[simps]
def pempty_alg_equiv : mv_polynomial pempty R ≃ₐ[R] R :=
{ to_fun := mv_polynomial.eval₂ (ring_hom.id _) $ pempty.elim,
inv_fun := C,
left_inv := is_id (C.comp (eval₂_hom (ring_hom.id _) pempty.elim))
(assume a : R, by { dsimp, rw [eval₂_C], refl }) (assume a, a.elim),
right_inv := λ r, eval₂_C _ _ _,
map_mul' := λ _ _, eval₂_mul _ _,
map_add' := λ _ _, eval₂_add _ _,
commutes' := λ _, by rw [mv_polynomial.algebra_map_eq]; simp }
/--
The ring isomorphism between multivariable polynomials in a single variable and
polynomials over the ground ring.
-/
@[simps]
def punit_ring_equiv : mv_polynomial punit R ≃+* polynomial R :=
{ to_fun := eval₂ polynomial.C (λu:punit, polynomial.X),
inv_fun := polynomial.eval₂ mv_polynomial.C (X punit.star),
left_inv :=
begin
let f : polynomial R →+* mv_polynomial punit R :=
ring_hom.of (polynomial.eval₂ mv_polynomial.C (X punit.star)),
let g : mv_polynomial punit R →+* polynomial R :=
ring_hom.of (eval₂ polynomial.C (λu:punit, polynomial.X)),
show ∀ p, f.comp g p = p,
apply is_id,
{ assume a, dsimp, rw [eval₂_C, polynomial.eval₂_C] },
{ rintros ⟨⟩, dsimp, rw [eval₂_X, polynomial.eval₂_X] }
end,
right_inv := assume p, polynomial.induction_on p
(assume a, by rw [polynomial.eval₂_C, mv_polynomial.eval₂_C])
(assume p q hp hq, by rw [polynomial.eval₂_add, mv_polynomial.eval₂_add, hp, hq])
(assume p n hp,
by rw [polynomial.eval₂_mul, polynomial.eval₂_pow, polynomial.eval₂_X, polynomial.eval₂_C,
eval₂_mul, eval₂_C, eval₂_pow, eval₂_X]),
map_mul' := λ _ _, eval₂_mul _ _,
map_add' := λ _ _, eval₂_add _ _ }
/-- The ring isomorphism between multivariable polynomials induced by an equivalence
of the variables. -/
@[simps]
def ring_equiv_of_equiv (e : S₁ ≃ S₂) : mv_polynomial S₁ R ≃+* mv_polynomial S₂ R :=
{ to_fun := rename e,
inv_fun := rename e.symm,
left_inv := λ p, by simp only [rename_rename, (∘), e.symm_apply_apply]; exact rename_id p,
right_inv := λ p, by simp only [rename_rename, (∘), e.apply_symm_apply]; exact rename_id p,
map_mul' := (rename e).map_mul,
map_add' := (rename e).map_add }
/-- The ring isomorphism between multivariable polynomials induced by a ring isomorphism
of the ground ring. -/
@[simps]
def ring_equiv_congr [comm_semiring S₂] (e : R ≃+* S₂) :
mv_polynomial S₁ R ≃+* mv_polynomial S₁ S₂ :=
{ to_fun := map (e : R →+* S₂),
inv_fun := map (e.symm : S₂ →+* R),
left_inv := assume p,
have (e.symm : S₂ →+* R).comp (e : R →+* S₂) = ring_hom.id _,
{ ext a, exact e.symm_apply_apply a },
by simp only [map_map, this, map_id],
right_inv := assume p,
have (e : R →+* S₂).comp (e.symm : S₂ →+* R) = ring_hom.id _,
{ ext a, exact e.apply_symm_apply a },
by simp only [map_map, this, map_id],
map_mul' := ring_hom.map_mul _,
map_add' := ring_hom.map_add _ }
/-- If `e : A ≃ₐ[R] B` is an isomorphism of `R`-algebas and `e_var : S₁ ≃ S₂` is an isomorphism of
types, the induced isomorphism `mv_polynomial S₁ A ≃ₐ[R] mv_polynomial S₂ B`. -/
def alg_equiv_congr {A B : Type*} [comm_semiring A] [comm_semiring B] [algebra R A] [algebra R B]
(e : A ≃ₐ[R] B) (e_var : S₁ ≃ S₂) :
algebra.comap R A (mv_polynomial S₁ A) ≃ₐ[R] algebra.comap R B (mv_polynomial S₂ B) :=
{ commutes' := begin
intro r,
dsimp,
have h₁ : algebra_map R (algebra.comap R A (mv_polynomial S₁ A)) r =
C (algebra_map R A r) := rfl,
have h₂ : algebra_map R (algebra.comap R B (mv_polynomial S₂ B)) r =
C (algebra_map R B r) := rfl,
have h : (↑(e.to_ring_equiv) : A →+* B) ((algebra_map R A) r) = e ((algebra_map R A) r) := rfl,
rw [h₁, h₂, map, rename_C, eval₂_hom_C, ring_hom.comp_apply, h, alg_equiv.commutes],
end,
.. (ring_equiv_of_equiv A e_var).trans (ring_equiv_congr A e.to_ring_equiv)
}
/-- The algebra isomorphism between multivariable polynomials induced by an equivalence
of the variables. -/
@[simps]
def alg_equiv_congr_left (e : S₁ ≃ S₂) : mv_polynomial S₁ R ≃ₐ[R] mv_polynomial S₂ R :=
alg_equiv_congr R alg_equiv.refl e
/-- If `e : A ≃ₐ[R] B` is an isomorphism of `R`-algebas, the induced isomorphism
`mv_polynomial S₁ A ≃ₐ[R] mv_polynomial S₁ B`. -/
def alg_equiv_congr_right {A B : Type*} [comm_semiring A] [comm_semiring B] [algebra R A]
[algebra R B] (e : A ≃ₐ[R] B) :
algebra.comap R A (mv_polynomial S₁ A) ≃ₐ[R] algebra.comap R B (mv_polynomial S₁ B) :=
alg_equiv_congr R e (equiv.cast rfl)
section
variables (S₁ S₂ S₃)
/--
The function from multivariable polynomials in a sum of two types,
to multivariable polynomials in one of the types,
with coefficents in multivariable polynomials in the other type.
See `sum_ring_equiv` for the ring isomorphism.
-/
def sum_to_iter : mv_polynomial (S₁ ⊕ S₂) R →+* mv_polynomial S₁ (mv_polynomial S₂ R) :=
eval₂_hom (C.comp C) (λbc, sum.rec_on bc X (C ∘ X))
instance is_semiring_hom_sum_to_iter : is_semiring_hom (sum_to_iter R S₁ S₂) :=
eval₂.is_semiring_hom _ _
@[simp]
lemma sum_to_iter_C (a : R) : sum_to_iter R S₁ S₂ (C a) = C (C a) :=
eval₂_C _ _ a
@[simp]
lemma sum_to_iter_Xl (b : S₁) : sum_to_iter R S₁ S₂ (X (sum.inl b)) = X b :=
eval₂_X _ _ (sum.inl b)
@[simp]
lemma sum_to_iter_Xr (c : S₂) : sum_to_iter R S₁ S₂ (X (sum.inr c)) = C (X c) :=
eval₂_X _ _ (sum.inr c)
/--
The function from multivariable polynomials in one type,
with coefficents in multivariable polynomials in another type,
to multivariable polynomials in the sum of the two types.
See `sum_ring_equiv` for the ring isomorphism.
-/
def iter_to_sum : mv_polynomial S₁ (mv_polynomial S₂ R) →+* mv_polynomial (S₁ ⊕ S₂) R :=
eval₂_hom (ring_hom.of (eval₂ C (X ∘ sum.inr))) (X ∘ sum.inl)
lemma iter_to_sum_C_C (a : R) : iter_to_sum R S₁ S₂ (C (C a)) = C a :=
eq.trans (eval₂_C _ _ (C a)) (eval₂_C _ _ _)
lemma iter_to_sum_X (b : S₁) : iter_to_sum R S₁ S₂ (X b) = X (sum.inl b) :=
eval₂_X _ _ _
lemma iter_to_sum_C_X (c : S₂) : iter_to_sum R S₁ S₂ (C (X c)) = X (sum.inr c) :=
eq.trans (eval₂_C _ _ (X c)) (eval₂_X _ _ _)
/-- A helper function for `sum_ring_equiv`. -/
@[simps]
def mv_polynomial_equiv_mv_polynomial [comm_semiring S₃]
(f : mv_polynomial S₁ R →+* mv_polynomial S₂ S₃)
(g : mv_polynomial S₂ S₃ →+* mv_polynomial S₁ R)
(hfgC : ∀a, f (g (C a)) = C a)
(hfgX : ∀n, f (g (X n)) = X n)
(hgfC : ∀a, g (f (C a)) = C a)
(hgfX : ∀n, g (f (X n)) = X n) :
mv_polynomial S₁ R ≃+* mv_polynomial S₂ S₃ :=
{ to_fun := f, inv_fun := g,
left_inv := is_id (ring_hom.comp _ _) hgfC hgfX,
right_inv := is_id (ring_hom.comp _ _) hfgC hfgX,
map_mul' := f.map_mul,
map_add' := f.map_add }
/--
The ring isomorphism between multivariable polynomials in a sum of two types,
and multivariable polynomials in one of the types,
with coefficents in multivariable polynomials in the other type.
-/
def sum_ring_equiv : mv_polynomial (S₁ ⊕ S₂) R ≃+* mv_polynomial S₁ (mv_polynomial S₂ R) :=
begin
apply @mv_polynomial_equiv_mv_polynomial R (S₁ ⊕ S₂) _ _ _ _
(sum_to_iter R S₁ S₂) (iter_to_sum R S₁ S₂),
{ assume p,
convert hom_eq_hom ((sum_to_iter R S₁ S₂).comp ((iter_to_sum R S₁ S₂).comp C)) C _ _ p,
{ assume a, dsimp, rw [iter_to_sum_C_C R S₁ S₂, sum_to_iter_C R S₁ S₂] },
{ assume c, dsimp, rw [iter_to_sum_C_X R S₁ S₂, sum_to_iter_Xr R S₁ S₂] } },
{ assume b, rw [iter_to_sum_X R S₁ S₂, sum_to_iter_Xl R S₁ S₂] },
{ assume a, rw [sum_to_iter_C R S₁ S₂, iter_to_sum_C_C R S₁ S₂] },
{ assume n, cases n with b c,
{ rw [sum_to_iter_Xl, iter_to_sum_X] },
{ rw [sum_to_iter_Xr, iter_to_sum_C_X] } },
end
/--
The algebra isomorphism between multivariable polynomials in a sum of two types,
and multivariable polynomials in one of the types,
with coefficents in multivariable polynomials in the other type.
-/
def sum_alg_equiv : mv_polynomial (S₁ ⊕ S₂) R ≃ₐ[R]
algebra.comap R (mv_polynomial S₂ R) (mv_polynomial S₁ (mv_polynomial S₂ R)) :=
{ commutes' := begin
intro r,
change algebra_map R (algebra.comap R (mv_polynomial S₂ R)
(mv_polynomial S₁ (mv_polynomial S₂ R))) r with C (C r),
change algebra_map R (mv_polynomial (S₁ ⊕ S₂) R) r with C r,
simp only [sum_ring_equiv, sum_to_iter_C, mv_polynomial_equiv_mv_polynomial_apply,
ring_equiv.to_fun_eq_coe]
end,
..sum_ring_equiv R S₁ S₂
}
/--
The ring isomorphism between multivariable polynomials in `option S₁` and
polynomials with coefficients in `mv_polynomial S₁ R`.
-/
def option_equiv_left : mv_polynomial (option S₁) R ≃+* polynomial (mv_polynomial S₁ R) :=
(ring_equiv_of_equiv R $ (equiv.option_equiv_sum_punit.{0} S₁).trans (equiv.sum_comm _ _)).trans $
(sum_ring_equiv R _ _).trans $
punit_ring_equiv _
/--
The ring isomorphism between multivariable polynomials in `option S₁` and
multivariable polynomials with coefficients in polynomials.
-/
def option_equiv_right : mv_polynomial (option S₁) R ≃+* mv_polynomial S₁ (polynomial R) :=
(ring_equiv_of_equiv R $ equiv.option_equiv_sum_punit.{0} S₁).trans $
(sum_ring_equiv R S₁ unit).trans $
ring_equiv_congr (mv_polynomial unit R) (punit_ring_equiv R)
/--
The ring isomorphism between multivariable polynomials in `fin (n + 1)` and
polynomials over multivariable polynomials in `fin n`.
-/
def fin_succ_equiv (n : ℕ) :
mv_polynomial (fin (n + 1)) R ≃+* polynomial (mv_polynomial (fin n) R) :=
(ring_equiv_of_equiv R (fin_succ_equiv n)).trans
(option_equiv_left R (fin n))
lemma fin_succ_equiv_eq (n : ℕ) :
(fin_succ_equiv R n : mv_polynomial (fin (n + 1)) R →+* polynomial (mv_polynomial (fin n) R)) =
eval₂_hom (polynomial.C.comp (C : R →+* mv_polynomial (fin n) R))
(λ i : fin (n+1), fin.cases polynomial.X (λ k, polynomial.C (X k)) i) :=
begin
apply ring_hom_ext,
{ intro r,
dsimp [ring_equiv.coe_to_ring_hom, fin_succ_equiv, option_equiv_left, sum_ring_equiv],
simp only [sum_to_iter_C, eval₂_C, rename_C, ring_hom.coe_comp] },
{ intro i,
dsimp [fin_succ_equiv, option_equiv_left, sum_ring_equiv],
refine fin.cases _ (λ _, _) i,
{ simp only [fin.cases_zero, sum.swap, rename_X, equiv.option_equiv_sum_punit_none,
equiv.sum_comm_apply, comp_app, sum_to_iter_Xl, eval₂_X, fin_succ_equiv_zero] },
{ simp only [rename_X, equiv.sum_comm_apply, comp_app, eval₂_X, eval₂_C, fin_succ_equiv_succ,
equiv.option_equiv_sum_punit_some, sum.swap, fin.cases_succ, sum_to_iter_Xr] } }
end
@[simp] lemma fin_succ_equiv_apply (n : ℕ) (p : mv_polynomial (fin (n + 1)) R) :
fin_succ_equiv R n p =
eval₂_hom (polynomial.C.comp (C : R →+* mv_polynomial (fin n) R))
(λ i : fin (n+1), fin.cases polynomial.X (λ k, polynomial.C (X k)) i) p :=
by { rw ← fin_succ_equiv_eq, refl }
lemma fin_succ_equiv_comp_C_eq_C {R : Type u} [comm_semiring R] (n : ℕ) :
((mv_polynomial.fin_succ_equiv R n).symm.to_ring_hom).comp
((polynomial.C).comp (mv_polynomial.C))
= (mv_polynomial.C : R →+* mv_polynomial (fin n.succ) R) :=
begin
refine ring_hom.ext (λ x, _),
rw ring_hom.comp_apply,
refine (mv_polynomial.fin_succ_equiv R n).injective
(trans ((mv_polynomial.fin_succ_equiv R n).apply_symm_apply _) _),
simp only [mv_polynomial.fin_succ_equiv_apply, mv_polynomial.eval₂_hom_C],
end
end
end equiv
end mv_polynomial
|
e514a783460e7502abb2ea5d331077836624bda0 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/order/monoid/nat_cast.lean | 9960b3c1b13e27a7a3cc09aa14214f1be5e1350b | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 3,202 | 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, Yuyang Zhao
-/
import algebra.order.monoid.lemmas
import algebra.order.zero_le_one
import data.nat.cast.defs
/-!
# Order of numerials in an `add_monoid_with_one`.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
variable {α : Type*}
open function
lemma lt_add_one [has_one α] [add_zero_class α] [partial_order α] [zero_le_one_class α]
[ne_zero (1 : α)] [covariant_class α α (+) (<)] (a : α) : a < a + 1 :=
lt_add_of_pos_right _ zero_lt_one
lemma lt_one_add [has_one α] [add_zero_class α] [partial_order α] [zero_le_one_class α]
[ne_zero (1 : α)] [covariant_class α α (swap (+)) (<)] (a : α) : a < 1 + a :=
lt_add_of_pos_left _ zero_lt_one
variable [add_monoid_with_one α]
lemma zero_le_two [preorder α] [zero_le_one_class α] [covariant_class α α (+) (≤)] :
(0 : α) ≤ 2 :=
add_nonneg zero_le_one zero_le_one
lemma zero_le_three [preorder α] [zero_le_one_class α] [covariant_class α α (+) (≤)] :
(0 : α) ≤ 3 :=
add_nonneg zero_le_two zero_le_one
lemma zero_le_four [preorder α] [zero_le_one_class α] [covariant_class α α (+) (≤)] :
(0 : α) ≤ 4 :=
add_nonneg zero_le_two zero_le_two
lemma one_le_two [has_le α] [zero_le_one_class α] [covariant_class α α (+) (≤)] :
(1 : α) ≤ 2 :=
calc 1 = 1 + 0 : (add_zero 1).symm
... ≤ 1 + 1 : add_le_add_left zero_le_one _
lemma one_le_two' [has_le α] [zero_le_one_class α] [covariant_class α α (swap (+)) (≤)] :
(1 : α) ≤ 2 :=
calc 1 = 0 + 1 : (zero_add 1).symm
... ≤ 1 + 1 : add_le_add_right zero_le_one _
section
variables [partial_order α] [zero_le_one_class α] [ne_zero (1 : α)]
section
variables [covariant_class α α (+) (≤)]
/-- See `zero_lt_two'` for a version with the type explicit. -/
@[simp] lemma zero_lt_two : (0 : α) < 2 := zero_lt_one.trans_le one_le_two
/-- See `zero_lt_three'` for a version with the type explicit. -/
@[simp] lemma zero_lt_three : (0 : α) < 3 := lt_add_of_lt_of_nonneg zero_lt_two zero_le_one
/-- See `zero_lt_four'` for a version with the type explicit. -/
@[simp] lemma zero_lt_four : (0 : α) < 4 := lt_add_of_lt_of_nonneg zero_lt_two zero_le_two
variables (α)
/-- See `zero_lt_two` for a version with the type implicit. -/
lemma zero_lt_two' : (0 : α) < 2 := zero_lt_two
/-- See `zero_lt_three` for a version with the type implicit. -/
lemma zero_lt_three' : (0 : α) < 3 := zero_lt_three
/-- See `zero_lt_four` for a version with the type implicit. -/
lemma zero_lt_four' : (0 : α) < 4 := zero_lt_four
instance zero_le_one_class.ne_zero.two : ne_zero (2 : α) := ⟨zero_lt_two.ne'⟩
instance zero_le_one_class.ne_zero.three : ne_zero (3 : α) := ⟨zero_lt_three.ne'⟩
instance zero_le_one_class.ne_zero.four : ne_zero (4 : α) := ⟨zero_lt_four.ne'⟩
end
lemma one_lt_two [covariant_class α α (+) (<)] : (1 : α) < 2 := lt_add_one _
end
alias zero_lt_two ← two_pos
alias zero_lt_three ← three_pos
alias zero_lt_four ← four_pos
|
655ad1cca8a4936aaf379b22a5127ea4cec5d3a1 | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /tests/lean/def_inaccessible_issue.lean | b29bccf0a5c389f9b6fb2f9b92f1eb8bb6a6eab0 | [
"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 | 383 | lean | open nat
set_option pp.binder_types true
inductive bv : nat → Type
| nil : bv 0
| cons : ∀ (n) (hd : bool) (tl : bv n), bv (n+1)
open bv
variable (f : bool → bool → bool)
definition map2 : ∀ {n}, bv n → bv n → bv n
| 0 nil nil := nil
| (n+1) (cons .n b1 v1) (cons .n b2 v2) := cons n (f b1 b2) (map2 v1 v2)
check map2.equations._eqn_2
|
75067d6dcaa75a24545b2b1c310843a392a3e5f4 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/topology/dense_embedding.lean | 1a4348cb76eeade57debf2711ec218c6a6972558 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 15,318 | lean | /-
Copyright (c) 2019 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot
-/
import topology.separation
import topology.bases
/-!
# Dense embeddings
This file defines three properties of functions:
* `dense_range f` means `f` has dense image;
* `dense_inducing i` means `i` is also `inducing`;
* `dense_embedding e` means `e` is also an `embedding`.
The main theorem `continuous_extend` gives a criterion for a function
`f : X → Z` to a T₃ space Z to extend along a dense embedding
`i : X → Y` to a continuous function `g : Y → Z`. Actually `i` only
has to be `dense_inducing` (not necessarily injective).
-/
noncomputable theory
open set filter
open_locale classical topological_space filter
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
/-- `i : α → β` is "dense inducing" if it has dense range and the topology on `α`
is the one induced by `i` from the topology on `β`. -/
@[protect_proj] structure dense_inducing [topological_space α] [topological_space β] (i : α → β)
extends inducing i : Prop :=
(dense : dense_range i)
namespace dense_inducing
variables [topological_space α] [topological_space β]
variables {i : α → β} (di : dense_inducing i)
lemma nhds_eq_comap (di : dense_inducing i) :
∀ a : α, 𝓝 a = comap i (𝓝 $ i a) :=
di.to_inducing.nhds_eq_comap
protected lemma continuous (di : dense_inducing i) : continuous i :=
di.to_inducing.continuous
lemma closure_range : closure (range i) = univ :=
di.dense.closure_range
protected lemma preconnected_space [preconnected_space α] (di : dense_inducing i) :
preconnected_space β :=
di.dense.preconnected_space di.continuous
lemma closure_image_mem_nhds {s : set α} {a : α} (di : dense_inducing i) (hs : s ∈ 𝓝 a) :
closure (i '' s) ∈ 𝓝 (i a) :=
begin
rw [di.nhds_eq_comap a, ((nhds_basis_opens _).comap _).mem_iff] at hs,
rcases hs with ⟨U, ⟨haU, hUo⟩, sub : i ⁻¹' U ⊆ s⟩,
refine mem_of_superset (hUo.mem_nhds haU) _,
calc U ⊆ closure (i '' (i ⁻¹' U)) : di.dense.subset_closure_image_preimage_of_is_open hUo
... ⊆ closure (i '' s) : closure_mono (image_subset i sub)
end
lemma dense_image (di : dense_inducing i) {s : set α} : dense (i '' s) ↔ dense s :=
begin
refine ⟨λ H x, _, di.dense.dense_image di.continuous⟩,
rw [di.to_inducing.closure_eq_preimage_closure_image, H.closure_eq, preimage_univ],
trivial
end
/-- If `i : α → β` is a dense embedding with dense complement of the range, then any compact set in
`α` has empty interior. -/
lemma interior_compact_eq_empty [t2_space β] (di : dense_inducing i) (hd : dense (range i)ᶜ)
{s : set α} (hs : is_compact s) : interior s = ∅ :=
begin
refine eq_empty_iff_forall_not_mem.2 (λ x hx, _),
rw [mem_interior_iff_mem_nhds] at hx,
have := di.closure_image_mem_nhds hx,
rw (hs.image di.continuous).is_closed.closure_eq at this,
rcases hd.inter_nhds_nonempty this with ⟨y, hyi, hys⟩,
exact hyi (image_subset_range _ _ hys)
end
/-- The product of two dense inducings is a dense inducing -/
protected lemma prod [topological_space γ] [topological_space δ]
{e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_inducing e₁) (de₂ : dense_inducing e₂) :
dense_inducing (λ(p : α × γ), (e₁ p.1, e₂ p.2)) :=
{ induced := (de₁.to_inducing.prod_mk de₂.to_inducing).induced,
dense := de₁.dense.prod_map de₂.dense }
open topological_space
/-- If the domain of a `dense_inducing` map is a separable space, then so is the codomain. -/
protected lemma separable_space [separable_space α] : separable_space β :=
di.dense.separable_space di.continuous
variables [topological_space δ] {f : γ → α} {g : γ → δ} {h : δ → β}
/--
```
γ -f→ α
g↓ ↓e
δ -h→ β
```
-/
lemma tendsto_comap_nhds_nhds {d : δ} {a : α} (di : dense_inducing i)
(H : tendsto h (𝓝 d) (𝓝 (i a))) (comm : h ∘ g = i ∘ f) : tendsto f (comap g (𝓝 d)) (𝓝 a) :=
begin
have lim1 : map g (comap g (𝓝 d)) ≤ 𝓝 d := map_comap_le,
replace lim1 : map h (map g (comap g (𝓝 d))) ≤ map h (𝓝 d) := map_mono lim1,
rw [filter.map_map, comm, ← filter.map_map, map_le_iff_le_comap] at lim1,
have lim2 : comap i (map h (𝓝 d)) ≤ comap i (𝓝 (i a)) := comap_mono H,
rw ← di.nhds_eq_comap at lim2,
exact le_trans lim1 lim2,
end
protected lemma nhds_within_ne_bot (di : dense_inducing i) (b : β) :
ne_bot (𝓝[range i] b) :=
di.dense.nhds_within_ne_bot b
lemma comap_nhds_ne_bot (di : dense_inducing i) (b : β) : ne_bot (comap i (𝓝 b)) :=
comap_ne_bot $ λ s hs,
let ⟨_, ⟨ha, a, rfl⟩⟩ := mem_closure_iff_nhds.1 (di.dense b) s hs in ⟨a, ha⟩
variables [topological_space γ]
/-- If `i : α → β` is a dense inducing, then any function `f : α → γ` "extends"
to a function `g = extend di f : β → γ`. If `γ` is Hausdorff and `f` has a
continuous extension, then `g` is the unique such extension. In general,
`g` might not be continuous or even extend `f`. -/
def extend (di : dense_inducing i) (f : α → γ) (b : β) : γ :=
@@lim _ ⟨f (di.dense.some b)⟩ (comap i (𝓝 b)) f
lemma extend_eq_of_tendsto [t2_space γ] {b : β} {c : γ} {f : α → γ}
(hf : tendsto f (comap i (𝓝 b)) (𝓝 c)) :
di.extend f b = c :=
by haveI := di.comap_nhds_ne_bot; exact hf.lim_eq
lemma extend_eq_at [t2_space γ] {f : α → γ} {a : α} (hf : continuous_at f a) :
di.extend f (i a) = f a :=
extend_eq_of_tendsto _ $ di.nhds_eq_comap a ▸ hf
lemma extend_eq_at' [t2_space γ] {f : α → γ} {a : α} (c : γ) (hf : tendsto f (𝓝 a) (𝓝 c)) :
di.extend f (i a) = f a :=
di.extend_eq_at (continuous_at_of_tendsto_nhds hf)
lemma extend_eq [t2_space γ] {f : α → γ} (hf : continuous f) (a : α) :
di.extend f (i a) = f a :=
di.extend_eq_at hf.continuous_at
/-- Variation of `extend_eq` where we ask that `f` has a limit along `comap i (𝓝 b)` for each
`b : β`. This is a strictly stronger assumption than continuity of `f`, but in a lot of cases
you'd have to prove it anyway to use `continuous_extend`, so this avoids doing the work twice. -/
lemma extend_eq' [t2_space γ] {f : α → γ}
(di : dense_inducing i) (hf : ∀ b, ∃ c, tendsto f (comap i (𝓝 b)) (𝓝 c)) (a : α) :
di.extend f (i a) = f a :=
begin
rcases hf (i a) with ⟨b, hb⟩,
refine di.extend_eq_at' b _,
rwa ← di.to_inducing.nhds_eq_comap at hb,
end
lemma extend_unique_at [t2_space γ] {b : β} {f : α → γ} {g : β → γ} (di : dense_inducing i)
(hf : ∀ᶠ x in comap i (𝓝 b), g (i x) = f x) (hg : continuous_at g b) :
di.extend f b = g b :=
begin
refine di.extend_eq_of_tendsto (λ s hs, mem_map.2 _),
suffices : ∀ᶠ (x : α) in comap i (𝓝 b), g (i x) ∈ s,
from hf.mp (this.mono $ λ x hgx hfx, hfx ▸ hgx),
clear hf f,
refine eventually_comap.2 ((hg.eventually hs).mono _),
rintros _ hxs x rfl,
exact hxs
end
lemma extend_unique [t2_space γ] {f : α → γ} {g : β → γ} (di : dense_inducing i)
(hf : ∀ x, g (i x) = f x) (hg : continuous g) :
di.extend f = g :=
funext $ λ b, extend_unique_at di (eventually_of_forall hf) hg.continuous_at
lemma continuous_at_extend [t3_space γ] {b : β} {f : α → γ} (di : dense_inducing i)
(hf : ∀ᶠ x in 𝓝 b, ∃c, tendsto f (comap i $ 𝓝 x) (𝓝 c)) :
continuous_at (di.extend f) b :=
begin
set φ := di.extend f,
haveI := di.comap_nhds_ne_bot,
suffices : ∀ V' ∈ 𝓝 (φ b), is_closed V' → φ ⁻¹' V' ∈ 𝓝 b,
by simpa [continuous_at, (closed_nhds_basis _).tendsto_right_iff],
intros V' V'_in V'_closed,
set V₁ := {x | tendsto f (comap i $ 𝓝 x) (𝓝 $ φ x)},
have V₁_in : V₁ ∈ 𝓝 b,
{ filter_upwards [hf],
rintros x ⟨c, hc⟩,
dsimp [V₁, φ],
rwa di.extend_eq_of_tendsto hc },
obtain ⟨V₂, V₂_in, V₂_op, hV₂⟩ : ∃ V₂ ∈ 𝓝 b, is_open V₂ ∧ ∀ x ∈ i ⁻¹' V₂, f x ∈ V',
{ simpa [and_assoc] using ((nhds_basis_opens' b).comap i).tendsto_left_iff.mp
(mem_of_mem_nhds V₁_in : b ∈ V₁) V' V'_in },
suffices : ∀ x ∈ V₁ ∩ V₂, φ x ∈ V',
{ filter_upwards [inter_mem V₁_in V₂_in] using this, },
rintros x ⟨x_in₁, x_in₂⟩,
have hV₂x : V₂ ∈ 𝓝 x := is_open.mem_nhds V₂_op x_in₂,
apply V'_closed.mem_of_tendsto x_in₁,
use V₂,
tauto,
end
lemma continuous_extend [t3_space γ] {f : α → γ} (di : dense_inducing i)
(hf : ∀b, ∃c, tendsto f (comap i (𝓝 b)) (𝓝 c)) : continuous (di.extend f) :=
continuous_iff_continuous_at.mpr $ assume b, di.continuous_at_extend $ univ_mem' hf
lemma mk'
(i : α → β)
(c : continuous i)
(dense : ∀x, x ∈ closure (range i))
(H : ∀ (a:α) s ∈ 𝓝 a,
∃t ∈ 𝓝 (i a), ∀ b, i b ∈ t → b ∈ s) :
dense_inducing i :=
{ induced := (induced_iff_nhds_eq i).2 $
λ a, le_antisymm (tendsto_iff_comap.1 $ c.tendsto _) (by simpa [filter.le_def] using H a),
dense := dense }
end dense_inducing
/-- A dense embedding is an embedding with dense image. -/
structure dense_embedding [topological_space α] [topological_space β] (e : α → β)
extends dense_inducing e : Prop :=
(inj : function.injective e)
theorem dense_embedding.mk'
[topological_space α] [topological_space β] (e : α → β)
(c : continuous e)
(dense : dense_range e)
(inj : function.injective e)
(H : ∀ (a:α) s ∈ 𝓝 a,
∃t ∈ 𝓝 (e a), ∀ b, e b ∈ t → b ∈ s) :
dense_embedding e :=
{ inj := inj,
..dense_inducing.mk' e c dense H}
namespace dense_embedding
open topological_space
variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ]
variables {e : α → β} (de : dense_embedding e)
lemma inj_iff {x y} : e x = e y ↔ x = y := de.inj.eq_iff
lemma to_embedding : embedding e :=
{ induced := de.induced,
inj := de.inj }
/-- If the domain of a `dense_embedding` is a separable space, then so is its codomain. -/
protected lemma separable_space [separable_space α] : separable_space β :=
de.to_dense_inducing.separable_space
/-- The product of two dense embeddings is a dense embedding. -/
protected lemma prod {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁)
(de₂ : dense_embedding e₂) :
dense_embedding (λ(p : α × γ), (e₁ p.1, e₂ p.2)) :=
{ inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩,
by simp; exact assume h₁ h₂, ⟨de₁.inj h₁, de₂.inj h₂⟩,
..dense_inducing.prod de₁.to_dense_inducing de₂.to_dense_inducing }
/-- The dense embedding of a subtype inside its closure. -/
@[simps] def subtype_emb {α : Type*} (p : α → Prop) (e : α → β) (x : {x // p x}) :
{x // x ∈ closure (e '' {x | p x})} :=
⟨e x, subset_closure $ mem_image_of_mem e x.prop⟩
protected lemma subtype (p : α → Prop) : dense_embedding (subtype_emb p e) :=
{ dense := dense_iff_closure_eq.2 $
begin
ext ⟨x, hx⟩,
rw image_eq_range at hx,
simpa [closure_subtype, ← range_comp, (∘)],
end,
inj := (de.inj.comp subtype.coe_injective).cod_restrict _,
induced := (induced_iff_nhds_eq _).2 (assume ⟨x, hx⟩,
by simp [subtype_emb, nhds_subtype_eq_comap, de.to_inducing.nhds_eq_comap, comap_comap, (∘)]) }
lemma dense_image {s : set α} : dense (e '' s) ↔ dense s :=
de.to_dense_inducing.dense_image
end dense_embedding
lemma dense.dense_embedding_coe [topological_space α] {s : set α} (hs : dense s) :
dense_embedding (coe : s → α) :=
{ dense := hs.dense_range_coe,
.. embedding_subtype_coe }
lemma is_closed_property [topological_space β] {e : α → β} {p : β → Prop}
(he : dense_range e) (hp : is_closed {x | p x}) (h : ∀a, p (e a)) :
∀b, p b :=
have univ ⊆ {b | p b},
from calc univ = closure (range e) : he.closure_range.symm
... ⊆ closure {b | p b} : closure_mono $ range_subset_iff.mpr h
... = _ : hp.closure_eq,
assume b, this trivial
lemma is_closed_property2 [topological_space β] {e : α → β} {p : β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) :
∀b₁ b₂, p b₁ b₂ :=
have ∀q:β×β, p q.1 q.2,
from is_closed_property (he.prod_map he) hp $ λ _, h _ _,
assume b₁ b₂, this ⟨b₁, b₂⟩
lemma is_closed_property3 [topological_space β] {e : α → β} {p : β → β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2})
(h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) :
∀b₁ b₂ b₃, p b₁ b₂ b₃ :=
have ∀q:β×β×β, p q.1 q.2.1 q.2.2,
from is_closed_property (he.prod_map $ he.prod_map he) hp $ λ _, h _ _ _,
assume b₁ b₂ b₃, this ⟨b₁, b₂, b₃⟩
@[elab_as_eliminator]
lemma dense_range.induction_on [topological_space β] {e : α → β} (he : dense_range e) {p : β → Prop}
(b₀ : β) (hp : is_closed {b | p b}) (ih : ∀a:α, p $ e a) : p b₀ :=
is_closed_property he hp ih b₀
@[elab_as_eliminator]
lemma dense_range.induction_on₂ [topological_space β] {e : α → β} {p : β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂))
(b₁ b₂ : β) : p b₁ b₂ := is_closed_property2 he hp h _ _
@[elab_as_eliminator]
lemma dense_range.induction_on₃ [topological_space β] {e : α → β} {p : β → β → β → Prop}
(he : dense_range e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2})
(h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃))
(b₁ b₂ b₃ : β) : p b₁ b₂ b₃ := is_closed_property3 he hp h _ _ _
section
variables [topological_space β] [topological_space γ] [t2_space γ]
variables {f : α → β}
/-- Two continuous functions to a t2-space that agree on the dense range of a function are equal. -/
lemma dense_range.equalizer (hfd : dense_range f)
{g h : β → γ} (hg : continuous g) (hh : continuous h) (H : g ∘ f = h ∘ f) :
g = h :=
funext $ λ y, hfd.induction_on y (is_closed_eq hg hh) $ congr_fun H
end
-- Bourbaki GT III §3 no.4 Proposition 7 (generalised to any dense-inducing map to a T₃ space)
lemma filter.has_basis.has_basis_of_dense_inducing
[topological_space α] [topological_space β] [t3_space β]
{ι : Type*} {s : ι → set α} {p : ι → Prop} {x : α} (h : (𝓝 x).has_basis p s)
{f : α → β} (hf : dense_inducing f) :
(𝓝 (f x)).has_basis p $ λ i, closure $ f '' (s i) :=
begin
rw filter.has_basis_iff at h ⊢,
intros T,
refine ⟨λ hT, _, λ hT, _⟩,
{ obtain ⟨T', hT₁, hT₂, hT₃⟩ := exists_mem_nhds_is_closed_subset hT,
have hT₄ : f⁻¹' T' ∈ 𝓝 x,
{ rw hf.to_inducing.nhds_eq_comap x,
exact ⟨T', hT₁, subset.rfl⟩, },
obtain ⟨i, hi, hi'⟩ := (h _).mp hT₄,
exact ⟨i, hi, (closure_mono (image_subset f hi')).trans (subset.trans (closure_minimal
(image_subset_iff.mpr subset.rfl) hT₂) hT₃)⟩, },
{ obtain ⟨i, hi, hi'⟩ := hT,
suffices : closure (f '' s i) ∈ 𝓝 (f x), { filter_upwards [this] using hi', },
replace h := (h (s i)).mpr ⟨i, hi, subset.rfl⟩,
exact hf.closure_image_mem_nhds h, },
end
|
ef357f8d69d01812a63efaf96e53b6aa451b7111 | 367134ba5a65885e863bdc4507601606690974c1 | /src/analysis/calculus/inverse.lean | 06fe15d32607e39084d978877bde7d6d2d01d6bf | [
"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 | 32,267 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov, Heather Macbeth, Sébastien Gouëzel
-/
import analysis.calculus.times_cont_diff
import analysis.normed_space.banach
import topology.local_homeomorph
import topology.metric_space.contracting
/-!
# Inverse function theorem
In this file we prove the inverse function theorem. It says that if a map `f : E → F`
has an invertible strict derivative `f'` at `a`, then it is locally invertible,
and the inverse function has derivative `f' ⁻¹`.
We define `has_strict_deriv_at.to_local_homeomorph` that repacks a function `f`
with a `hf : has_strict_fderiv_at f f' a`, `f' : E ≃L[𝕜] F`, into a `local_homeomorph`.
The `to_fun` of this `local_homeomorph` is `defeq` to `f`, so one can apply theorems
about `local_homeomorph` to `hf.to_local_homeomorph f`, and get statements about `f`.
Then we define `has_strict_fderiv_at.local_inverse` to be the `inv_fun` of this `local_homeomorph`,
and prove two versions of the inverse function theorem:
* `has_strict_fderiv_at.to_local_inverse`: if `f` has an invertible derivative `f'` at `a` in the
strict sense (`hf`), then `hf.local_inverse f f' a` has derivative `f'.symm` at `f a` in the
strict sense;
* `has_strict_fderiv_at.to_local_left_inverse`: if `f` has an invertible derivative `f'` at `a` in
the strict sense and `g` is locally left inverse to `f` near `a`, then `g` has derivative
`f'.symm` at `f a` in the strict sense.
In the one-dimensional case we reformulate these theorems in terms of `has_strict_deriv_at` and
`f'⁻¹`.
We also reformulate the theorems in terms of `times_cont_diff`, to give that `C^k` (respectively,
smooth) inputs give `C^k` (smooth) inverses. These versions require that continuous
differentiability implies strict differentiability; this is false over a general field, true over
`ℝ` or `ℂ` and implemented here assuming `is_R_or_C 𝕂`.
Some related theorems, providing the derivative and higher regularity assuming that we already know
the inverse function, are formulated in `fderiv.lean`, `deriv.lean`, and `times_cont_diff.lean`.
## Notations
In the section about `approximates_linear_on` we introduce some `local notation` to make formulas
shorter:
* by `N` we denote `∥f'⁻¹∥`;
* by `g` we denote the auxiliary contracting map `x ↦ x + f'.symm (y - f x)` used to prove that
`{x | f x = y}` is nonempty.
## Tags
derivative, strictly differentiable, continuously differentiable, smooth, inverse function
-/
open function set filter metric
open_locale topological_space classical nnreal
noncomputable theory
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
variables {E : Type*} [normed_group E] [normed_space 𝕜 E]
variables {F : Type*} [normed_group F] [normed_space 𝕜 F]
variables {G : Type*} [normed_group G] [normed_space 𝕜 G]
variables {G' : Type*} [normed_group G'] [normed_space 𝕜 G']
variables {ε : ℝ}
open asymptotics filter metric set
open continuous_linear_map (id)
/-!
### Non-linear maps close to affine maps
In this section we study a map `f` such that `∥f x - f y - f' (x - y)∥ ≤ c * ∥x - y∥` on an open set
`s`, where `f' : E →L[𝕜] F` is a continuous linear map and `c` is suitably small. Maps of this type
behave like `f a + f' (x - a)` near each `a ∈ s`.
When `f'` is onto, we show that `f` is locally onto.
When `f'` is a continuous linear equiv, we show that `f` is a homeomorphism
between `s` and `f '' s`. More precisely, we define `approximates_linear_on.to_local_homeomorph` to
be a `local_homeomorph` with `to_fun = f`, `source = s`, and `target = f '' s`.
Maps of this type naturally appear in the proof of the inverse function theorem (see next section),
and `approximates_linear_on.to_local_homeomorph` will imply that the locally inverse function
exists.
We define this auxiliary notion to split the proof of the inverse function theorem into small
lemmas. This approach makes it possible
- to prove a lower estimate on the size of the domain of the inverse function;
- to reuse parts of the proofs in the case if a function is not strictly differentiable. E.g., for a
function `f : E × F → G` with estimates on `f x y₁ - f x y₂` but not on `f x₁ y - f x₂ y`.
-/
/-- We say that `f` approximates a continuous linear map `f'` on `s` with constant `c`,
if `∥f x - f y - f' (x - y)∥ ≤ c * ∥x - y∥` whenever `x, y ∈ s`.
This predicate is defined to facilitate the splitting of the inverse function theorem into small
lemmas. Some of these lemmas can be useful, e.g., to prove that the inverse function is defined
on a specific set. -/
def approximates_linear_on (f : E → F) (f' : E →L[𝕜] F) (s : set E) (c : ℝ≥0) : Prop :=
∀ (x ∈ s) (y ∈ s), ∥f x - f y - f' (x - y)∥ ≤ c * ∥x - y∥
namespace approximates_linear_on
variables [cs : complete_space E] {f : E → F}
/-! First we prove some properties of a function that `approximates_linear_on` a (not necessarily
invertible) continuous linear map. -/
section
variables {f' : E →L[𝕜] F} {s t : set E} {c c' : ℝ≥0}
theorem mono_num (hc : c ≤ c') (hf : approximates_linear_on f f' s c) :
approximates_linear_on f f' s c' :=
λ x hx y hy, le_trans (hf x hx y hy) (mul_le_mul_of_nonneg_right hc $ norm_nonneg _)
theorem mono_set (hst : s ⊆ t) (hf : approximates_linear_on f f' t c) :
approximates_linear_on f f' s c :=
λ x hx y hy, hf x (hst hx) y (hst hy)
lemma lipschitz_sub (hf : approximates_linear_on f f' s c) :
lipschitz_with c (λ x : s, f x - f' x) :=
begin
refine lipschitz_with.of_dist_le_mul (λ x y, _),
rw [dist_eq_norm, subtype.dist_eq, dist_eq_norm],
convert hf x x.2 y y.2 using 2,
rw [f'.map_sub], abel
end
protected lemma lipschitz (hf : approximates_linear_on f f' s c) :
lipschitz_with (nnnorm f' + c) (s.restrict f) :=
by simpa only [restrict_apply, add_sub_cancel'_right]
using (f'.lipschitz.restrict s).add hf.lipschitz_sub
protected lemma continuous (hf : approximates_linear_on f f' s c) :
continuous (s.restrict f) :=
hf.lipschitz.continuous
protected lemma continuous_on (hf : approximates_linear_on f f' s c) :
continuous_on f s :=
continuous_on_iff_continuous_restrict.2 hf.continuous
end
section locally_onto
/-!
We prove that a function which is linearly approximated by a continuous linear map with a nonlinear
right inverse is locally onto. This will apply to the case where the approximating map is a linear
equivalence, for the local inverse theorem, but also whenever the approximating map is onto,
by Banach's open mapping theorem. -/
include cs
variables {s : set E} {c : ℝ≥0} {f' : E →L[𝕜] F}
/-- If a function is linearly approximated by a continuous linear map with a (possibly nonlinear)
right inverse, then it is locally onto: a ball of an explicit radius is included in the image
of the map. -/
theorem surj_on_closed_ball_of_nonlinear_right_inverse
(hf : approximates_linear_on f f' s c) (f'symm : f'.nonlinear_right_inverse)
{ε : ℝ} {b : E} (ε0 : 0 ≤ ε) (hε : closed_ball b ε ⊆ s) :
surj_on f (closed_ball b ε) (closed_ball (f b) (((f'symm.nnnorm : ℝ)⁻¹ - c) * ε)) :=
begin
assume y hy,
cases le_or_lt (f'symm.nnnorm : ℝ) ⁻¹ c with hc hc,
{ refine ⟨b, by simp [ε0], _⟩,
have : dist y (f b) ≤ 0 :=
(mem_closed_ball.1 hy).trans (mul_nonpos_of_nonpos_of_nonneg (by linarith) ε0),
simp only [dist_le_zero] at this,
rw this },
have If' : (0 : ℝ) < f'symm.nnnorm,
by { rw [← inv_pos], exact (nnreal.coe_nonneg _).trans_lt hc },
have Icf' : (c : ℝ) * f'symm.nnnorm < 1, by rwa [inv_eq_one_div, lt_div_iff If'] at hc,
have Jf' : (f'symm.nnnorm : ℝ) ≠ 0 := ne_of_gt If',
have Jcf' : (1 : ℝ) - c * f'symm.nnnorm ≠ 0, by { apply ne_of_gt, linarith },
/- We have to show that `y` can be written as `f x` for some `x ∈ closed_ball b ε`.
The idea of the proof is to apply the Banach contraction principle to the map
`g : x ↦ x + f'symm (y - f x)`, as a fixed point of this map satisfies `f x = y`.
When `f'symm` is a genuine linear inverse, `g` is a contracting map. In our case, since `f'symm`
is nonlinear, this map is not contracting (it is not even continuous), but still the proof of
the contraction theorem holds: `uₙ = gⁿ b` is a Cauchy sequence, converging exponentially fast
to the desired point `x`. Instead of appealing to general results, we check this by hand.
The main point is that `f (u n)` becomes exponentially close to `y`, and therefore
`dist (u (n+1)) (u n)` becomes exponentally small, making it possible to get an inductive
bound on `dist (u n) b`, from which one checks that `u n` stays in the ball on which one has a
control. Therefore, the bound can be checked at the next step, and so on inductively.
-/
set g := λ x, x + f'symm (y - f x) with hg,
set u := λ (n : ℕ), g ^[n] b with hu,
have usucc : ∀ n, u (n + 1) = g (u n), by simp [hu, ← iterate_succ_apply' g _ b],
-- First bound: if `f z` is close to `y`, then `g z` is close to `z` (i.e., almost a fixed point).
have A : ∀ z, dist (g z) z ≤ f'symm.nnnorm * dist (f z) y,
{ assume z,
rw [dist_eq_norm, hg, add_sub_cancel', dist_eq_norm'],
exact f'symm.bound _ },
-- Second bound: if `z` and `g z` are in the set with good control, then `f (g z)` becomes closer
-- to `y` than `f z` was (this uses the linear approximation property, and is the reason for the
-- choice of the formula for `g`).
have B : ∀ z ∈ closed_ball b ε, g z ∈ closed_ball b ε →
dist (f (g z)) y ≤ c * f'symm.nnnorm * dist (f z) y,
{ assume z hz hgz,
set v := f'symm (y - f z) with hv,
calc dist (f (g z)) y = ∥f (z + v) - y∥ : by rw [dist_eq_norm]
... = ∥f (z + v) - f z - f' v + f' v - (y - f z)∥ : by { congr' 1, abel }
... = ∥f (z + v) - f z - f' ((z + v) - z)∥ :
by simp only [continuous_linear_map.nonlinear_right_inverse.right_inv,
add_sub_cancel', sub_add_cancel]
... ≤ c * ∥(z + v) - z∥ : hf _ (hε hgz) _ (hε hz)
... ≤ c * (f'symm.nnnorm * dist (f z) y) : begin
apply mul_le_mul_of_nonneg_left _ (nnreal.coe_nonneg c),
simpa [hv, dist_eq_norm'] using f'symm.bound (y - f z),
end
... = c * f'symm.nnnorm * dist (f z) y : by ring },
-- Third bound: a complicated bound on `dist w b` (that will show up in the induction) is enough
-- to check that `w` is in the ball on which one has controls. Will be used to check that `u n`
-- belongs to this ball for all `n`.
have C : ∀ (n : ℕ) (w : E),
dist w b ≤ f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n) / (1 - c * f'symm.nnnorm) * dist (f b) y
→ w ∈ closed_ball b ε,
{ assume n w hw,
apply hw.trans,
rw [div_mul_eq_mul_div, div_le_iff], swap, { linarith },
calc (f'symm.nnnorm : ℝ) * (1 - (c * f'symm.nnnorm) ^ n) * dist (f b) y
= f'symm.nnnorm * dist (f b) y * (1 - (c * f'symm.nnnorm) ^ n) : by ring
... ≤ f'symm.nnnorm * dist (f b) y * 1 :
begin
apply mul_le_mul_of_nonneg_left _ (mul_nonneg (nnreal.coe_nonneg _) dist_nonneg),
rw [sub_le_self_iff],
exact pow_nonneg (mul_nonneg (nnreal.coe_nonneg _) (nnreal.coe_nonneg _)) _,
end
... ≤ f'symm.nnnorm * (((f'symm.nnnorm : ℝ)⁻¹ - c) * ε) :
by { rw [mul_one],
exact mul_le_mul_of_nonneg_left (mem_closed_ball'.1 hy) (nnreal.coe_nonneg _) }
... = ε * (1 - c * f'symm.nnnorm) : by { field_simp, ring } },
/- Main inductive control: `f (u n)` becomes exponentially close to `y`, and therefore
`dist (u (n+1)) (u n)` becomes exponentally small, making it possible to get an inductive
bound on `dist (u n) b`, from which one checks that `u n` remains in the ball on which we
have estimates. -/
have D : ∀ (n : ℕ), dist (f (u n)) y ≤ (c * f'symm.nnnorm)^n * dist (f b) y
∧ dist (u n) b ≤ f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n) / (1 - c * f'symm.nnnorm)
* dist (f b) y,
{ assume n,
induction n with n IH, { simp [hu, le_refl] },
rw usucc,
have Ign : dist (g (u n)) b ≤
f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n.succ) / (1 - c * f'symm.nnnorm) * dist (f b) y :=
calc
dist (g (u n)) b ≤ dist (g (u n)) (u n) + dist (u n) b : dist_triangle _ _ _
... ≤ f'symm.nnnorm * dist (f (u n)) y + dist (u n) b : add_le_add (A _) (le_refl _)
... ≤ f'symm.nnnorm * ((c * f'symm.nnnorm)^n * dist (f b) y) +
f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n) / (1 - c * f'symm.nnnorm) * dist (f b) y :
add_le_add (mul_le_mul_of_nonneg_left IH.1 (nnreal.coe_nonneg _)) IH.2
... = f'symm.nnnorm * (1 - (c * f'symm.nnnorm)^n.succ) / (1 - c * f'symm.nnnorm)
* dist (f b) y : by { field_simp [Jcf'], ring_exp },
refine ⟨_, Ign⟩,
calc dist (f (g (u n))) y ≤ c * f'symm.nnnorm * dist (f (u n)) y :
B _ (C n _ IH.2) (C n.succ _ Ign)
... ≤ (c * f'symm.nnnorm) * ((c * f'symm.nnnorm)^n * dist (f b) y) :
mul_le_mul_of_nonneg_left IH.1 (mul_nonneg (nnreal.coe_nonneg _) (nnreal.coe_nonneg _))
... = (c * f'symm.nnnorm) ^ n.succ * dist (f b) y : by ring_exp },
-- Deduce from the inductive bound that `uₙ` is a Cauchy sequence, therefore converging.
have : cauchy_seq u,
{ have : ∀ (n : ℕ), dist (u n) (u (n+1)) ≤ f'symm.nnnorm * dist (f b) y * (c * f'symm.nnnorm)^n,
{ assume n,
calc dist (u n) (u (n+1)) = dist (g (u n)) (u n) : by rw [usucc, dist_comm]
... ≤ f'symm.nnnorm * dist (f (u n)) y : A _
... ≤ f'symm.nnnorm * ((c * f'symm.nnnorm)^n * dist (f b) y) :
mul_le_mul_of_nonneg_left (D n).1 (nnreal.coe_nonneg _)
... = f'symm.nnnorm * dist (f b) y * (c * f'symm.nnnorm)^n : by ring },
exact cauchy_seq_of_le_geometric _ _ Icf' this },
obtain ⟨x, hx⟩ : ∃ x, tendsto u at_top (𝓝 x) := cauchy_seq_tendsto_of_complete this,
-- As all the `uₙ` belong to the ball `closed_ball b ε`, so does their limit `x`.
have xmem : x ∈ closed_ball b ε :=
is_closed_ball.mem_of_tendsto hx (eventually_of_forall (λ n, C n _ (D n).2)),
refine ⟨x, xmem, _⟩,
-- It remains to check that `f x = y`. This follows from continuity of `f` on `closed_ball b ε`
-- and from the fact that `f uₙ` is converging to `y` by construction.
have hx' : tendsto u at_top (𝓝[closed_ball b ε] x),
{ simp only [nhds_within, tendsto_inf, hx, true_and, ge_iff_le, tendsto_principal],
exact eventually_of_forall (λ n, C n _ (D n).2) },
have T1 : tendsto (λ n, f (u n)) at_top (𝓝 (f x)) :=
(hf.continuous_on.mono hε x xmem).tendsto.comp hx',
have T2 : tendsto (λ n, f (u n)) at_top (𝓝 y),
{ rw tendsto_iff_dist_tendsto_zero,
refine squeeze_zero (λ n, dist_nonneg) (λ n, (D n).1) _,
simpa using (tendsto_pow_at_top_nhds_0_of_lt_1
(mul_nonneg (nnreal.coe_nonneg _) (nnreal.coe_nonneg _)) Icf').mul tendsto_const_nhds },
exact tendsto_nhds_unique T1 T2,
end
lemma open_image (hf : approximates_linear_on f f' s c) (f'symm : f'.nonlinear_right_inverse)
(hs : is_open s) (hc : subsingleton F ∨ c < f'symm.nnnorm⁻¹) : is_open (f '' s) :=
begin
cases hc with hE hc, { resetI, apply is_open_discrete },
simp only [is_open_iff_mem_nhds, nhds_basis_closed_ball.mem_iff, ball_image_iff] at hs ⊢,
intros x hx,
rcases hs x hx with ⟨ε, ε0, hε⟩,
refine ⟨(f'symm.nnnorm⁻¹ - c) * ε, mul_pos (sub_pos.2 hc) ε0, _⟩,
exact (hf.surj_on_closed_ball_of_nonlinear_right_inverse f'symm (le_of_lt ε0) hε).mono
hε (subset.refl _)
end
lemma image_mem_nhds (hf : approximates_linear_on f f' s c) (f'symm : f'.nonlinear_right_inverse)
{x : E} (hs : s ∈ 𝓝 x) (hc : subsingleton F ∨ c < f'symm.nnnorm⁻¹) :
f '' s ∈ 𝓝 (f x) :=
begin
obtain ⟨t, hts, ht, xt⟩ : ∃ t ⊆ s, is_open t ∧ x ∈ t := mem_nhds_sets_iff.1 hs,
have := mem_nhds_sets ((hf.mono_set hts).open_image f'symm ht hc) (mem_image_of_mem _ xt),
exact mem_sets_of_superset this (image_subset _ hts),
end
lemma map_nhds_eq (hf : approximates_linear_on f f' s c) (f'symm : f'.nonlinear_right_inverse)
{x : E} (hs : s ∈ 𝓝 x) (hc : subsingleton F ∨ c < f'symm.nnnorm⁻¹) :
map f (𝓝 x) = 𝓝 (f x) :=
begin
refine le_antisymm ((hf.continuous_on x (mem_of_nhds hs)).continuous_at hs) (le_map (λ t ht, _)),
have : f '' (s ∩ t) ∈ 𝓝 (f x) := (hf.mono_set (inter_subset_left s t)).image_mem_nhds
f'symm (inter_mem_sets hs ht) hc,
exact mem_sets_of_superset this (image_subset _ (inter_subset_right _ _)),
end
end locally_onto
/-!
From now on we assume that `f` approximates an invertible continuous linear map `f : E ≃L[𝕜] F`.
We also assume that either `E = {0}`, or `c < ∥f'⁻¹∥⁻¹`. We use `N` as an abbreviation for `∥f'⁻¹∥`.
-/
variables {f' : E ≃L[𝕜] F} {s : set E} {c : ℝ≥0}
local notation `N` := nnnorm (f'.symm : F →L[𝕜] E)
protected lemma antilipschitz (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) :
antilipschitz_with (N⁻¹ - c)⁻¹ (s.restrict f) :=
begin
cases hc with hE hc,
{ haveI : subsingleton s := ⟨λ x y, subtype.eq $ @subsingleton.elim _ hE _ _⟩,
exact antilipschitz_with.of_subsingleton },
convert (f'.antilipschitz.restrict s).add_lipschitz_with hf.lipschitz_sub hc,
simp [restrict]
end
protected lemma injective (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) :
injective (s.restrict f) :=
(hf.antilipschitz hc).injective
protected lemma inj_on (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) :
inj_on f s :=
inj_on_iff_injective.2 $ hf.injective hc
/-- A map approximating a linear equivalence on a set defines a local equivalence on this set.
Should not be used outside of this file, because it is superseded by `to_local_homeomorph` below.
This is a first step towards the inverse function. -/
def to_local_equiv (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) : local_equiv E F :=
(hf.inj_on hc).to_local_equiv _ _
/-- The inverse function is continuous on `f '' s`. Use properties of `local_homeomorph` instead. -/
lemma inverse_continuous_on (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) :
continuous_on (hf.to_local_equiv hc).symm (f '' s) :=
begin
apply continuous_on_iff_continuous_restrict.2,
refine ((hf.antilipschitz hc).to_right_inv_on' _ (hf.to_local_equiv hc).right_inv').continuous,
exact (λ x hx, (hf.to_local_equiv hc).map_target hx)
end
include cs
section
variables (f s)
/-- Given a function `f` that approximates a linear equivalence on an open set `s`,
returns a local homeomorph with `to_fun = f` and `source = s`. -/
def to_local_homeomorph (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) : local_homeomorph E F :=
{ to_local_equiv := hf.to_local_equiv hc,
open_source := hs,
open_target := hf.open_image f'.to_nonlinear_right_inverse hs
(by rwa f'.to_linear_equiv.to_equiv.subsingleton_iff at hc),
continuous_to_fun := hf.continuous_on,
continuous_inv_fun := hf.inverse_continuous_on hc }
end
@[simp] lemma to_local_homeomorph_coe (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) :
(hf.to_local_homeomorph f s hc hs : E → F) = f := rfl
@[simp] lemma to_local_homeomorph_source (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) :
(hf.to_local_homeomorph f s hc hs).source = s := rfl
@[simp] lemma to_local_homeomorph_target (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) :
(hf.to_local_homeomorph f s hc hs).target = f '' s := rfl
lemma closed_ball_subset_target (hf : approximates_linear_on f (f' : E →L[𝕜] F) s c)
(hc : subsingleton E ∨ c < N⁻¹) (hs : is_open s) {b : E} (ε0 : 0 ≤ ε) (hε : closed_ball b ε ⊆ s) :
closed_ball (f b) ((N⁻¹ - c) * ε) ⊆ (hf.to_local_homeomorph f s hc hs).target :=
(hf.surj_on_closed_ball_of_nonlinear_right_inverse f'.to_nonlinear_right_inverse
ε0 hε).mono hε (subset.refl _)
end approximates_linear_on
/-!
### Inverse function theorem
Now we prove the inverse function theorem. Let `f : E → F` be a map defined on a complete vector
space `E`. Assume that `f` has an invertible derivative `f' : E ≃L[𝕜] F` at `a : E` in the strict
sense. Then `f` approximates `f'` in the sense of `approximates_linear_on` on an open neighborhood
of `a`, and we can apply `approximates_linear_on.to_local_homeomorph` to construct the inverse
function. -/
namespace has_strict_fderiv_at
/-- If `f` has derivative `f'` at `a` in the strict sense and `c > 0`, then `f` approximates `f'`
with constant `c` on some neighborhood of `a`. -/
lemma approximates_deriv_on_nhds {f : E → F} {f' : E →L[𝕜] F} {a : E}
(hf : has_strict_fderiv_at f f' a) {c : ℝ≥0} (hc : subsingleton E ∨ 0 < c) :
∃ s ∈ 𝓝 a, approximates_linear_on f f' s c :=
begin
cases hc with hE hc,
{ refine ⟨univ, mem_nhds_sets is_open_univ trivial, λ x hx y hy, _⟩,
simp [@subsingleton.elim E hE x y] },
have := hf.def hc,
rw [nhds_prod_eq, filter.eventually, mem_prod_same_iff] at this,
rcases this with ⟨s, has, hs⟩,
exact ⟨s, has, λ x hx y hy, hs (mk_mem_prod hx hy)⟩
end
lemma map_nhds_eq_of_surj [complete_space E] [complete_space F]
{f : E → F} {f' : E →L[𝕜] F} {a : E}
(hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) (h : f'.range = ⊤) :
map f (𝓝 a) = 𝓝 (f a) :=
begin
let f'symm := f'.nonlinear_right_inverse_of_surjective h,
set c : ℝ≥0 := f'symm.nnnorm⁻¹ / 2 with hc,
have f'symm_pos : 0 < f'symm.nnnorm := f'.nonlinear_right_inverse_of_surjective_nnnorm_pos h,
have cpos : 0 < c, by simp [hc, nnreal.half_pos, nnreal.inv_pos, f'symm_pos],
obtain ⟨s, s_nhds, hs⟩ : ∃ s ∈ 𝓝 a, approximates_linear_on f f' s c :=
hf.approximates_deriv_on_nhds (or.inr cpos),
apply hs.map_nhds_eq f'symm s_nhds (or.inr (nnreal.half_lt_self _)),
simp [ne_of_gt f'symm_pos],
end
variables [cs : complete_space E] {f : E → F} {f' : E ≃L[𝕜] F} {a : E}
lemma approximates_deriv_on_open_nhds (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
∃ (s : set E) (hs : a ∈ s ∧ is_open s),
approximates_linear_on f (f' : E →L[𝕜] F) s ((nnnorm (f'.symm : F →L[𝕜] E))⁻¹ / 2) :=
begin
refine ((nhds_basis_opens a).exists_iff _).1 _,
exact (λ s t, approximates_linear_on.mono_set),
exact (hf.approximates_deriv_on_nhds $ f'.subsingleton_or_nnnorm_symm_pos.imp id $
λ hf', nnreal.half_pos $ nnreal.inv_pos.2 $ hf')
end
include cs
variable (f)
/-- Given a function with an invertible strict derivative at `a`, returns a `local_homeomorph`
with `to_fun = f` and `a ∈ source`. This is a part of the inverse function theorem.
The other part `has_strict_fderiv_at.to_local_inverse` states that the inverse function
of this `local_homeomorph` has derivative `f'.symm`. -/
def to_local_homeomorph (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) : local_homeomorph E F :=
approximates_linear_on.to_local_homeomorph f
(classical.some hf.approximates_deriv_on_open_nhds)
(classical.some_spec hf.approximates_deriv_on_open_nhds).snd
(f'.subsingleton_or_nnnorm_symm_pos.imp id $ λ hf', nnreal.half_lt_self $ ne_of_gt $
nnreal.inv_pos.2 $ hf')
(classical.some_spec hf.approximates_deriv_on_open_nhds).fst.2
variable {f}
@[simp] lemma to_local_homeomorph_coe (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
(hf.to_local_homeomorph f : E → F) = f := rfl
lemma mem_to_local_homeomorph_source (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
a ∈ (hf.to_local_homeomorph f).source :=
(classical.some_spec hf.approximates_deriv_on_open_nhds).fst.1
lemma image_mem_to_local_homeomorph_target (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
f a ∈ (hf.to_local_homeomorph f).target :=
(hf.to_local_homeomorph f).map_source hf.mem_to_local_homeomorph_source
lemma map_nhds_eq_of_equiv (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
map f (𝓝 a) = 𝓝 (f a) :=
(hf.to_local_homeomorph f).map_nhds_eq hf.mem_to_local_homeomorph_source
variables (f f' a)
/-- Given a function `f` with an invertible derivative, returns a function that is locally inverse
to `f`. -/
def local_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) : F → E :=
(hf.to_local_homeomorph f).symm
variables {f f' a}
lemma local_inverse_def (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
hf.local_inverse f _ _ = (hf.to_local_homeomorph f).symm :=
rfl
lemma eventually_left_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
∀ᶠ x in 𝓝 a, hf.local_inverse f f' a (f x) = x :=
(hf.to_local_homeomorph f).eventually_left_inverse hf.mem_to_local_homeomorph_source
@[simp] lemma local_inverse_apply_image (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
hf.local_inverse f f' a (f a) = a :=
hf.eventually_left_inverse.self_of_nhds
lemma eventually_right_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
∀ᶠ y in 𝓝 (f a), f (hf.local_inverse f f' a y) = y :=
(hf.to_local_homeomorph f).eventually_right_inverse' hf.mem_to_local_homeomorph_source
lemma local_inverse_continuous_at (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
continuous_at (hf.local_inverse f f' a) (f a) :=
(hf.to_local_homeomorph f).continuous_at_symm hf.image_mem_to_local_homeomorph_target
lemma local_inverse_tendsto (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
tendsto (hf.local_inverse f f' a) (𝓝 $ f a) (𝓝 a) :=
(hf.to_local_homeomorph f).tendsto_symm hf.mem_to_local_homeomorph_source
lemma local_inverse_unique (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) {g : F → E}
(hg : ∀ᶠ x in 𝓝 a, g (f x) = x) :
∀ᶠ y in 𝓝 (f a), g y = local_inverse f f' a hf y :=
eventually_eq_of_left_inv_of_right_inv hg hf.eventually_right_inverse $
(hf.to_local_homeomorph f).tendsto_symm hf.mem_to_local_homeomorph_source
/-- If `f` has an invertible derivative `f'` at `a` in the sense of strict differentiability `(hf)`,
then the inverse function `hf.local_inverse f` has derivative `f'.symm` at `f a`. -/
theorem to_local_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) :
has_strict_fderiv_at (hf.local_inverse f f' a) (f'.symm : F →L[𝕜] E) (f a) :=
(hf.to_local_homeomorph f).has_strict_fderiv_at_symm hf.image_mem_to_local_homeomorph_target $
by simpa [← local_inverse_def] using hf
/-- If `f : E → F` has an invertible derivative `f'` at `a` in the sense of strict differentiability
and `g (f x) = x` in a neighborhood of `a`, then `g` has derivative `f'.symm` at `f a`.
For a version assuming `f (g y) = y` and continuity of `g` at `f a` but not `[complete_space E]`
see `of_local_left_inverse`. -/
theorem to_local_left_inverse (hf : has_strict_fderiv_at f (f' : E →L[𝕜] F) a) {g : F → E}
(hg : ∀ᶠ x in 𝓝 a, g (f x) = x) :
has_strict_fderiv_at g (f'.symm : F →L[𝕜] E) (f a) :=
hf.to_local_inverse.congr_of_eventually_eq $ (hf.local_inverse_unique hg).mono $ λ _, eq.symm
end has_strict_fderiv_at
/-- If a function has an invertible strict derivative at all points, then it is an open map. -/
lemma open_map_of_strict_fderiv_equiv [complete_space E] {f : E → F} {f' : E → E ≃L[𝕜] F}
(hf : ∀ x, has_strict_fderiv_at f (f' x : E →L[𝕜] F) x) :
is_open_map f :=
is_open_map_iff_nhds_le.2 $ λ x, (hf x).map_nhds_eq_of_equiv.ge
/-!
### Inverse function theorem, 1D case
In this case we prove a version of the inverse function theorem for maps `f : 𝕜 → 𝕜`.
We use `continuous_linear_equiv.units_equiv_aut` to translate `has_strict_deriv_at f f' a` and
`f' ≠ 0` into `has_strict_fderiv_at f (_ : 𝕜 ≃L[𝕜] 𝕜) a`.
-/
namespace has_strict_deriv_at
variables [cs : complete_space 𝕜] {f : 𝕜 → 𝕜} {f' a : 𝕜} (hf : has_strict_deriv_at f f' a)
(hf' : f' ≠ 0)
include cs
variables (f f' a)
/-- A function that is inverse to `f` near `a`. -/
@[reducible] def local_inverse : 𝕜 → 𝕜 :=
(hf.has_strict_fderiv_at_equiv hf').local_inverse _ _ _
variables {f f' a}
lemma map_nhds_eq : map f (𝓝 a) = 𝓝 (f a) :=
(hf.has_strict_fderiv_at_equiv hf').map_nhds_eq_of_equiv
theorem to_local_inverse : has_strict_deriv_at (hf.local_inverse f f' a hf') f'⁻¹ (f a) :=
(hf.has_strict_fderiv_at_equiv hf').to_local_inverse
theorem to_local_left_inverse {g : 𝕜 → 𝕜} (hg : ∀ᶠ x in 𝓝 a, g (f x) = x) :
has_strict_deriv_at g f'⁻¹ (f a) :=
(hf.has_strict_fderiv_at_equiv hf').to_local_left_inverse hg
end has_strict_deriv_at
/-- If a function has a non-zero strict derivative at all points, then it is an open map. -/
lemma open_map_of_strict_deriv [complete_space 𝕜] {f f' : 𝕜 → 𝕜}
(hf : ∀ x, has_strict_deriv_at f (f' x) x) (h0 : ∀ x, f' x ≠ 0) :
is_open_map f :=
is_open_map_iff_nhds_le.2 $ λ x, ((hf x).map_nhds_eq (h0 x)).ge
/-!
### Inverse function theorem, smooth case
-/
namespace times_cont_diff_at
variables {𝕂 : Type*} [is_R_or_C 𝕂]
variables {E' : Type*} [normed_group E'] [normed_space 𝕂 E']
variables {F' : Type*} [normed_group F'] [normed_space 𝕂 F']
variables [complete_space E'] (f : E' → F') {f' : E' ≃L[𝕂] F'} {a : E'}
/-- Given a `times_cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible
derivative at `a`, returns a `local_homeomorph` with `to_fun = f` and `a ∈ source`. -/
def to_local_homeomorph
{n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
local_homeomorph E' F' :=
(hf.has_strict_fderiv_at' hf' hn).to_local_homeomorph f
variable {f}
@[simp] lemma to_local_homeomorph_coe
{n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
(hf.to_local_homeomorph f hf' hn : E' → F') = f := rfl
lemma mem_to_local_homeomorph_source
{n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
a ∈ (hf.to_local_homeomorph f hf' hn).source :=
(hf.has_strict_fderiv_at' hf' hn).mem_to_local_homeomorph_source
lemma image_mem_to_local_homeomorph_target
{n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
f a ∈ (hf.to_local_homeomorph f hf' hn).target :=
(hf.has_strict_fderiv_at' hf' hn).image_mem_to_local_homeomorph_target
/-- Given a `times_cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible derivative
at `a`, returns a function that is locally inverse to `f`. -/
def local_inverse
{n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
F' → E' :=
(hf.has_strict_fderiv_at' hf' hn).local_inverse f f' a
lemma local_inverse_apply_image
{n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
hf.local_inverse hf' hn (f a) = a :=
(hf.has_strict_fderiv_at' hf' hn).local_inverse_apply_image
/-- Given a `times_cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible derivative
at `a`, the inverse function (produced by `times_cont_diff.to_local_homeomorph`) is
also `times_cont_diff`. -/
lemma to_local_inverse
{n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (f' : E' →L[𝕂] F') a)
(hn : 1 ≤ n) :
times_cont_diff_at 𝕂 n (hf.local_inverse hf' hn) (f a) :=
begin
have := hf.local_inverse_apply_image hf' hn,
apply (hf.to_local_homeomorph f hf' hn).times_cont_diff_at_symm
(image_mem_to_local_homeomorph_target hf hf' hn),
{ convert hf' },
{ convert hf }
end
end times_cont_diff_at
|
a839084f0b756903e8d1866b366a52f2ce6ddcd5 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/adjunction/fully_faithful.lean | ca29238caf89bb508c7557ae89a5c5202555959c | [
"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 | 7,444 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.adjunction.basic
import category_theory.conj
import category_theory.yoneda
/-!
# Adjoints of fully faithful functors
A left adjoint is fully faithful, if and only if the unit is an isomorphism
(and similarly for right adjoints and the counit).
`adjunction.restrict_fully_faithful` shows that an adjunction can be restricted along fully faithful
inclusions.
## Future work
The statements from Riehl 4.5.13 for adjoints which are either full, or faithful.
-/
open category_theory
namespace category_theory
universes v₁ v₂ u₁ u₂
open category
open opposite
variables {C : Type u₁} [category.{v₁} C]
variables {D : Type u₂} [category.{v₂} D]
variables {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R)
/--
If the left adjoint is fully faithful, then the unit is an isomorphism.
See
* Lemma 4.5.13 from [Riehl][riehl2017]
* https://math.stackexchange.com/a/2727177
* https://stacks.math.columbia.edu/tag/07RB (we only prove the forward direction!)
-/
instance unit_is_iso_of_L_fully_faithful [full L] [faithful L] : is_iso (adjunction.unit h) :=
@nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ (adjunction.unit h) $ λ X,
@yoneda.is_iso _ _ _ _ ((adjunction.unit h).app X)
⟨⟨{ app := λ Y f, L.preimage ((h.hom_equiv (unop Y) (L.obj X)).symm f) },
⟨begin
ext x f, dsimp,
apply L.map_injective,
simp,
end, begin
ext x f, dsimp,
simp only [adjunction.hom_equiv_counit, preimage_comp, preimage_map, category.assoc],
rw ←h.unit_naturality,
simp,
end⟩⟩⟩
/--
If the right adjoint is fully faithful, then the counit is an isomorphism.
See <https://stacks.math.columbia.edu/tag/07RB> (we only prove the forward direction!)
-/
instance counit_is_iso_of_R_fully_faithful [full R] [faithful R] : is_iso (adjunction.counit h) :=
@nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ (adjunction.counit h) $ λ X,
@is_iso_of_op _ _ _ _ _ $
@coyoneda.is_iso _ _ _ _ ((adjunction.counit h).app X).op
⟨⟨{ app := λ Y f, R.preimage ((h.hom_equiv (R.obj X) Y) f) },
⟨begin
ext x f, dsimp,
apply R.map_injective,
simp,
end, begin
ext x f, dsimp,
simp only [adjunction.hom_equiv_unit, preimage_comp, preimage_map],
rw ←h.counit_naturality,
simp,
end⟩⟩⟩
/-- If the unit of an adjunction is an isomorphism, then its inverse on the image of L is given
by L whiskered with the counit. -/
@[simp]
lemma inv_map_unit {X : C} [is_iso (h.unit.app X)] :
inv (L.map (h.unit.app X)) = h.counit.app (L.obj X) :=
is_iso.inv_eq_of_hom_inv_id h.left_triangle_components
/-- If the unit is an isomorphism, bundle one has an isomorphism `L ⋙ R ⋙ L ≅ L`. -/
@[simps]
noncomputable def whisker_left_L_counit_iso_of_is_iso_unit [is_iso h.unit] :
L ⋙ R ⋙ L ≅ L :=
(L.associator R L).symm ≪≫ iso_whisker_right (as_iso h.unit).symm L ≪≫ functor.left_unitor _
/-- If the counit of an adjunction is an isomorphism, then its inverse on the image of R is given
by R whiskered with the unit. -/
@[simp]
lemma inv_counit_map {X : D} [is_iso (h.counit.app X)] :
inv (R.map (h.counit.app X)) = h.unit.app (R.obj X) :=
is_iso.inv_eq_of_inv_hom_id h.right_triangle_components
/-- If the counit of an is an isomorphism, one has an isomorphism `(R ⋙ L ⋙ R) ≅ R`. -/
@[simps]
noncomputable def whisker_left_R_unit_iso_of_is_iso_counit [is_iso h.counit] :
(R ⋙ L ⋙ R) ≅ R :=
(R.associator L R).symm ≪≫ iso_whisker_right (as_iso h.counit) R ≪≫ functor.left_unitor _
/-- If the unit is an isomorphism, then the left adjoint is full-/
noncomputable
def L_full_of_unit_is_iso [is_iso h.unit] : full L :=
{ preimage := λ X Y f, (h.hom_equiv X (L.obj Y) f) ≫ inv (h.unit.app Y) }
/-- If the unit is an isomorphism, then the left adjoint is faithful-/
lemma L_faithful_of_unit_is_iso [is_iso h.unit] : faithful L :=
{ map_injective' := λ X Y f g H,
begin
rw ←(h.hom_equiv X (L.obj Y)).apply_eq_iff_eq at H,
simpa using H =≫ inv (h.unit.app Y),
end }
/-- If the counit is an isomorphism, then the right adjoint is full-/
noncomputable
def R_full_of_counit_is_iso [is_iso h.counit] : full R :=
{ preimage := λ X Y f, inv (h.counit.app X) ≫ (h.hom_equiv (R.obj X) Y).symm f }
/-- If the counit is an isomorphism, then the right adjoint is faithful-/
lemma R_faithful_of_counit_is_iso [is_iso h.counit] : faithful R :=
{ map_injective' := λ X Y f g H,
begin
rw ←(h.hom_equiv (R.obj X) Y).symm.apply_eq_iff_eq at H,
simpa using inv (h.counit.app X) ≫= H,
end }
instance whisker_left_counit_iso_of_L_fully_faithful
[full L] [faithful L] : is_iso (whisker_left L h.counit) :=
begin
have := h.left_triangle,
rw ←is_iso.eq_inv_comp at this,
rw this,
apply_instance
end
instance whisker_right_counit_iso_of_L_fully_faithful
[full L] [faithful L] : is_iso (whisker_right h.counit R) :=
begin
have := h.right_triangle,
rw ←is_iso.eq_inv_comp at this,
rw this,
apply_instance
end
instance whisker_left_unit_iso_of_R_fully_faithful
[full R] [faithful R] : is_iso (whisker_left R h.unit) :=
begin
have := h.right_triangle,
rw ←is_iso.eq_comp_inv at this,
rw this,
apply_instance
end
instance whisker_right_unit_iso_of_R_fully_faithful
[full R] [faithful R] : is_iso (whisker_right h.unit L) :=
begin
have := h.left_triangle,
rw ←is_iso.eq_comp_inv at this,
rw this,
apply_instance
end
-- TODO also do the statements from Riehl 4.5.13 for full and faithful separately?
universes v₃ v₄ u₃ u₄
variables {C' : Type u₃} [category.{v₃} C']
variables {D' : Type u₄} [category.{v₄} D']
-- TODO: This needs some lemmas describing the produced adjunction, probably in terms of `adj`,
-- `iC` and `iD`.
/--
If `C` is a full subcategory of `C'` and `D` is a full subcategory of `D'`, then we can restrict
an adjunction `L' ⊣ R'` where `L' : C' ⥤ D'` and `R' : D' ⥤ C'` to `C` and `D`.
The construction here is slightly more general, in that `C` is required only to have a full and
faithful "inclusion" functor `iC : C ⥤ C'` (and similarly `iD : D ⥤ D'`) which commute (up to
natural isomorphism) with the proposed restrictions.
-/
def adjunction.restrict_fully_faithful (iC : C ⥤ C') (iD : D ⥤ D') {L' : C' ⥤ D'} {R' : D' ⥤ C'}
(adj : L' ⊣ R') {L : C ⥤ D} {R : D ⥤ C} (comm1 : iC ⋙ L' ≅ L ⋙ iD) (comm2 : iD ⋙ R' ≅ R ⋙ iC)
[full iC] [faithful iC] [full iD] [faithful iD] :
L ⊣ R :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X Y,
calc (L.obj X ⟶ Y) ≃ (iD.obj (L.obj X) ⟶ iD.obj Y) : equiv_of_fully_faithful iD
... ≃ (L'.obj (iC.obj X) ⟶ iD.obj Y) : iso.hom_congr (comm1.symm.app X) (iso.refl _)
... ≃ (iC.obj X ⟶ R'.obj (iD.obj Y)) : adj.hom_equiv _ _
... ≃ (iC.obj X ⟶ iC.obj (R.obj Y)) : iso.hom_congr (iso.refl _) (comm2.app Y)
... ≃ (X ⟶ R.obj Y) : (equiv_of_fully_faithful iC).symm,
hom_equiv_naturality_left_symm' := λ X' X Y f g,
begin
apply iD.map_injective,
simpa using (comm1.inv.naturality_assoc f _).symm,
end,
hom_equiv_naturality_right' := λ X Y' Y f g,
begin
apply iC.map_injective,
suffices : R'.map (iD.map g) ≫ comm2.hom.app Y = comm2.hom.app Y' ≫ iC.map (R.map g),
simp [this],
apply comm2.hom.naturality g,
end }
end category_theory
|
1b6624b8d5273dd9dae8c0c7d0568bc7a6e6e92d | b522075d31b564daeb3347a10eb9bb777ee93943 | /manifold/differentiable.lean | 086274d574a6e9446ca1d56a009aeec1ab271317 | [] | no_license | truonghoangle/manifolds | e6c2534dd46579f56ba99a48e2eb7ce51640e7c0 | 9c0d731a480e88758180b31ce7c3b371771d426b | refs/heads/master | 1,638,501,090,139 | 1,557,991,948,000 | 1,557,991,948,000 | 185,779,631 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,072 | lean | /-
Copyright (c) 2019 Joe Cool. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Hoang Le Truong.
-/
import manifold.manifold map
import analysis.normed_space.deriv analysis.normed_space.bounded_linear_maps
open pfun
local attribute [instance] classical.prop_decidable
noncomputable theory
universes u v w
variables {α : Type } {β : Type v} {γ : Type w} {n : ℕ}
variable [nondiscrete_normed_field α]
namespace diff
variables {E : cartesian α }
variables {F : cartesian α }
instance : normed_space α (E →L[α] F) := bounded_linear_map.to_normed_space
def is_dif_at_within (f : E →. α ) (s: set E) (x : E) : Prop :=
@differentiable_within_at α _ E _ α _ (@ext_by_zero α E _ f) s x
def is_dif_at (f : E →. α ) (x : E) : Prop := ∃g, @has_fderiv_at α _ _ _ _ _ (@ext_by_zero α E _ f) g x
def is_dif (f : (E →. α) ) : Prop := ∀ x:f.dom, is_dif_at_within f f.dom x
def is_dif_map (E : cartesian α) (F : cartesian α) : ( E →. F )→ Prop := λ f, ∀ i: fin (F.dim), is_dif (pfun.lift (cartesian.proj F i).to_fun ∘. f )
lemma exists_diff (f:E →. α ) (h:@is_dif α _ E f ) :
∀ (x:E)( H:x∈ f.dom), ∃ g, @has_fderiv_within_at α _ _ _ _ _ (ext_by_zero f) g (f.dom) x:=
by {
simp [is_dif,is_dif_at_within] at h,
repeat {intro},
have h2:= @h x H,
exact h2
}
def diff (f:E →. α ) [h:@is_dif α _ E f ]: E →. cartesian.dual E:=
λ x, { dom:= x ∈ f.dom,
get:=λ y,
let g:=classical.some (@exists_diff α _ E f h x y) in @is_bounded_linear_map.to_linear_map α _ E _ _ _ g (bounded_linear_map.is_bounded_linear_map g)}
def is_diff (f:E →. α ) [h:@is_dif α _ E f ]:Prop := @is_dif_map α _ E (@cartesian.dual α _ E) (@diff α _ E f h)
structure C1_diff_map (f : E →. F ) :=
(has_diff: ∀ i: fin (F.dim), (is_dif (pfun.lift (cartesian.proj F i).to_fun ∘. f)) )
(cont: ∀ i: fin (F.dim), is_continuous (@diff α _ E (pfun.lift (cartesian.proj F i).to_fun ∘. f ) (has_diff i)))
structure C2_diff_map (f : E →. F ) :=
(has_diff: ∀ i: fin (F.dim), (is_dif (pfun.lift (cartesian.proj F i).to_fun ∘. f)) )
(has_C1_diff: ∀ i: fin (F.dim), @is_diff α _ E (pfun.lift (cartesian.proj F i).to_fun ∘. f ) (has_diff i) )
(cont: ∀ i: fin (F.dim), is_continuous (@diff α _ E (pfun.lift (cartesian.proj F i).to_fun ∘. f ) (has_diff i)))
def has_diff (f : (E →. α )) (i:fin E.dim) : roption(E →. α ) :=
{dom:= @is_dif α _ E f,
get:=λ y, (pfun.lift (cartesian.proj (cartesian.dual E) i).to_fun ∘. (@diff α _ E f y ))}
def C:list(fin E.dim) → (E→. α )→ Prop
| []:= λ f, is_continuous f
| (a::l):=λ f, C l f → ∃g , g= @has_diff α _ E f a ∧ (∀ x, is_continuous (g.get x))
def 𝒞_n (n:ℕ ) (E : cartesian α) (F : cartesian α): (E→. F )→ Prop :=
λ f, ∀ (l: list(fin E.dim)) (i:fin F.dim), list.length l=n → C l (pfun.lift (cartesian.proj F i).to_fun ∘. f)
def 𝒞_infinity (E : cartesian α) (F : cartesian α): (E→. F )→ Prop :=
λ f, ∀ n:ℕ, 𝒞_n n E F f
lemma const (c:α):is_dif (@const_fun α E _ c):=
by {
rw[diff.is_dif],
{repeat {intro},
rw[diff.is_dif_at_within,ext_const],
have h2:= @has_fderiv_within_at_const α _ E _ α _ c x (@const_fun α E _ c).dom,
refine ⟨ 0 , h2 ⟩},
}
#check diff.is_dif
end diff
class diff_manifold (E : cartesian α ) (X:Top) extends manifold_prop E X :=
{equiv:(λ f, property f) = diff.is_dif }
class C_infinity_manifold (E : cartesian α ) (X:Top) extends manifold_prop E X :=
{equiv:(λ f, property f) = diff.𝒞_infinity E (cartesian.field α) }
class C_manifold (n:ℕ ) (E : cartesian α ) (X:Top) extends manifold_prop E X :=
{equiv:(λ f, property f) = diff.𝒞_n n E (cartesian.field α) }
class real_manifold (E : cartesian ℝ )(X:Top) extends diff_manifold (E : cartesian ℝ ) (X:Top)
class complex_manifold (E : cartesian ℂ )(X:Top) extends diff_manifold (E : cartesian ℂ ) (X:Top)
|
7a256e2a245c66a33294407b897260114286e4b7 | bbecf0f1968d1fba4124103e4f6b55251d08e9c4 | /src/field_theory/finite/galois_field.lean | 3a65c541f9bea02ae8d42dedd8ca209046c9cf4c | [
"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 | 5,438 | lean | /-
Copyright (c) 2021 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson, Alex J. Best, Johan Commelin, Eric Rodriguez, Ruben Van de Velde
-/
import algebra.char_p.algebra
import field_theory.finite.basic
import field_theory.separable
import linear_algebra.finite_dimensional
/-!
# Galois fields
If `p` is a prime number, and `n` a natural number,
then `galois_field p n` is defined as the splitting field of `X^(p^n) - X` over `zmod p`.
It is a finite field with `p ^ n` elements.
## Main definition
* `galois_field p n` is a field with `p ^ n` elements
-/
noncomputable theory
open polynomial
lemma galois_poly_separable {K : Type*} [field K] (p q : ℕ) [char_p K p] (h : p ∣ q) :
separable (X ^ q - X : polynomial K) :=
begin
use [1, (X ^ q - X - 1)],
rw [← char_p.cast_eq_zero_iff (polynomial K) p] at h,
rw [derivative_sub, derivative_pow, derivative_X, h],
ring,
end
/-- A finite field with `p ^ n` elements.
Every field with the same cardinality is (non-canonically)
isomorphic to this field. -/
@[derive field]
def galois_field (p : ℕ) [fact p.prime] (n : ℕ) :=
splitting_field (X^(p^n) - X : polynomial (zmod p))
instance : inhabited (@galois_field 2 (fact.mk nat.prime_two) 1) :=
⟨37⟩
namespace galois_field
variables (p : ℕ) [fact p.prime] (n : ℕ)
instance : algebra (zmod p) (galois_field p n) :=
splitting_field.algebra _
instance : is_splitting_field (zmod p) (galois_field p n) (X^(p^n) - X) :=
polynomial.is_splitting_field.splitting_field _
instance : char_p (galois_field p n) p :=
(algebra.char_p_iff (zmod p) (galois_field p n) p).mp (by apply_instance)
instance : fintype (galois_field p n) := by {dsimp only [galois_field],
exact finite_dimensional.fintype_of_fintype (zmod p) (galois_field p n) }
lemma finrank {n} (h : n ≠ 0) : finite_dimensional.finrank (zmod p) (galois_field p n) = n :=
begin
set g_poly := (X^(p^n) - X : polynomial (zmod p)),
have hp : 1 < p := (fact.out (nat.prime p)).one_lt,
have aux : g_poly ≠ 0 := finite_field.X_pow_card_pow_sub_X_ne_zero _ h hp,
have key : fintype.card ((g_poly).root_set (galois_field p n)) = (g_poly).nat_degree :=
card_root_set_eq_nat_degree (galois_poly_separable p _ (dvd_pow (dvd_refl p) h))
(splitting_field.splits g_poly),
have nat_degree_eq : (g_poly).nat_degree = p ^ n :=
finite_field.X_pow_card_pow_sub_X_nat_degree_eq _ h hp,
rw nat_degree_eq at key,
suffices : (g_poly).root_set (galois_field p n) = set.univ,
{ simp_rw [this, ←fintype.of_equiv_card (equiv.set.univ _)] at key,
rw [@card_eq_pow_finrank (zmod p), zmod.card] at key,
exact nat.pow_right_injective ((nat.prime.one_lt' p).out) key },
rw set.eq_univ_iff_forall,
suffices : ∀ x (hx : x ∈ (⊤ : subalgebra (zmod p) (galois_field p n))),
x ∈ (X ^ p ^ n - X : polynomial (zmod p)).root_set (galois_field p n),
{ simpa, },
rw ← splitting_field.adjoin_root_set,
simp_rw algebra.mem_adjoin_iff,
intros x hx,
-- We discharge the `p = 0` separately, to avoid typeclass issues on `zmod p`.
unfreezingI { cases p, cases hp, },
apply subring.closure_induction hx; clear_dependent x; simp_rw mem_root_set aux,
{ rintros x (⟨r, rfl⟩ | hx),
{ simp only [aeval_X_pow, aeval_X, alg_hom.map_sub],
rw [← ring_hom.map_pow, zmod.pow_card_pow, sub_self], },
{ dsimp only [galois_field] at hx,
rwa mem_root_set aux at hx, }, },
{ dsimp only [g_poly],
rw [← coeff_zero_eq_aeval_zero'],
simp only [coeff_X_pow, coeff_X_zero, sub_zero, ring_hom.map_eq_zero, ite_eq_right_iff,
one_ne_zero, coeff_sub],
intro hn,
exact nat.not_lt_zero 1 (pow_eq_zero hn.symm ▸ hp), },
{ simp, },
{ simp only [aeval_X_pow, aeval_X, alg_hom.map_sub, add_pow_char_pow, sub_eq_zero],
intros x y hx hy,
rw [hx, hy], },
{ intros x hx,
simp only [sub_eq_zero, aeval_X_pow, aeval_X, alg_hom.map_sub, sub_neg_eq_add] at *,
rw [neg_pow, hx, char_p.neg_one_pow_char_pow],
simp, },
{ simp only [aeval_X_pow, aeval_X, alg_hom.map_sub, mul_pow, sub_eq_zero],
intros x y hx hy,
rw [hx, hy], },
end
lemma card (h : n ≠ 0) : fintype.card (galois_field p n) = p ^ n :=
begin
let b := is_noetherian.finset_basis (zmod p) (galois_field p n),
rw [module.card_fintype b, ← finite_dimensional.finrank_eq_card_basis b, zmod.card, finrank p h],
end
theorem splits_zmod_X_pow_sub_X : splits (ring_hom.id (zmod p)) (X ^ p - X) :=
begin
have hp : 1 < p := (fact.out (nat.prime p)).one_lt,
have h1 : roots (X ^ p - X : polynomial (zmod p)) = finset.univ.val,
{ convert finite_field.roots_X_pow_card_sub_X _,
exact (zmod.card p).symm },
have h2 := finite_field.X_pow_card_sub_X_nat_degree_eq (zmod p) hp,
-- We discharge the `p = 0` separately, to avoid typeclass issues on `zmod p`.
unfreezingI { cases p, cases hp, },
rw [splits_iff_card_roots, h1, ←finset.card_def, finset.card_univ, h2, zmod.card],
end
/-- A Galois field with exponent 1 is equivalent to `zmod` -/
def equiv_zmod_p : galois_field p 1 ≃ₐ[zmod p] (zmod p) :=
have h : (X ^ p ^ 1 : polynomial (zmod p)) = X ^ (fintype.card (zmod p)),
by rw [pow_one, zmod.card p],
have inst : is_splitting_field (zmod p) (zmod p) (X ^ p ^ 1 - X),
by { rw h, apply_instance },
by exactI (is_splitting_field.alg_equiv (zmod p) (X ^ (p ^ 1) - X : polynomial (zmod p))).symm
end galois_field
|
5f2d53887f70a6ca3e504f76610e124377aef8da | f57749ca63d6416f807b770f67559503fdb21001 | /hott/types/pi.hlean | f5b96d7ee9658d358dfca280edb001e8b430ad08 | [
"Apache-2.0"
] | permissive | aliassaf/lean | bd54e85bed07b1ff6f01396551867b2677cbc6ac | f9b069b6a50756588b309b3d716c447004203152 | refs/heads/master | 1,610,982,152,948 | 1,438,916,029,000 | 1,438,916,029,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,924 | hlean | /-
Copyright (c) 2014-15 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Floris van Doorn
Partially ported from Coq HoTT
Theorems about pi-types (dependent function spaces)
-/
import types.sigma arity
open eq equiv is_equiv funext sigma
namespace pi
variables {A A' : Type} {B : A → Type} {B' : A' → Type} {C : Πa, B a → Type}
{D : Πa b, C a b → Type}
{a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {f g : Πa, B a}
/- Paths -/
/-
Paths [p : f ≈ g] in a function type [Πx:X, P x] are equivalent to functions taking values
in path types, [H : Πx:X, f x ≈ g x], or concisely, [H : f ~ g].
This equivalence, however, is just the combination of [apd10] and function extensionality
[funext], and as such, [eq_of_homotopy]
Now we show how these things compute.
-/
definition apd10_eq_of_homotopy (h : f ~ g) : apd10 (eq_of_homotopy h) ~ h :=
apd10 (right_inv apd10 h)
definition eq_of_homotopy_eta (p : f = g) : eq_of_homotopy (apd10 p) = p :=
left_inv apd10 p
definition eq_of_homotopy_idp (f : Πa, B a) : eq_of_homotopy (λx : A, refl (f x)) = refl f :=
!eq_of_homotopy_eta
/-
The identification of the path space of a dependent function space,
up to equivalence, is of course just funext.
-/
definition eq_equiv_homotopy (f g : Πx, B x) : (f = g) ≃ (f ~ g) :=
equiv.mk apd10 _
definition is_equiv_eq_of_homotopy (f g : Πx, B x) : is_equiv (@eq_of_homotopy _ _ f g) :=
_
definition homotopy_equiv_eq (f g : Πx, B x) : (f ~ g) ≃ (f = g) :=
equiv.mk eq_of_homotopy _
/- Transport -/
definition pi_transport (p : a = a') (f : Π(b : B a), C a b)
: (transport (λa, Π(b : B a), C a b) p f)
~ (λb, transport (C a') !tr_inv_tr (transportD _ p _ (f (p⁻¹ ▸ b)))) :=
eq.rec_on p (λx, idp)
/- A special case of [transport_pi] where the type [B] does not depend on [A],
and so it is just a fixed type [B]. -/
definition pi_transport_constant {C : A → A' → Type} (p : a = a') (f : Π(b : A'), C a b) (b : A')
: (transport _ p f) b = p ▸ (f b) :=
eq.rec_on p idp
/- Pathovers -/
definition pi_pathover {f : Πb, C a b} {g : Πb', C a' b'} {p : a = a'}
(r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[apo011 C p q] g b') : f =[p] g :=
begin
cases p, apply pathover_idp_of_eq,
apply eq_of_homotopy, intro b,
apply eq_of_pathover_idp, apply r
end
-- a version where C is uncurried, but where the conclusion of r is still a proper pathover
-- instead of a heterogenous equality
definition pi_pathover' {C : (Σa, B a) → Type} {f : Πb, C ⟨a, b⟩} {g : Πb', C ⟨a', b'⟩}
{p : a = a'} (r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[dpair_eq_dpair p q] g b')
: f =[p] g :=
begin
cases p, apply pathover_idp_of_eq,
apply eq_of_homotopy, intro b,
apply (@eq_of_pathover_idp _ C), exact (r b b (pathover.idpatho b)),
end
definition pi_pathover_left {f : Πb, C a b} {g : Πb', C a' b'} {p : a = a'}
(r : Π(b : B a), f b =[apo011 C p !pathover_tr] g (p ▸ b)) : f =[p] g :=
begin
cases p, apply pathover_idp_of_eq,
apply eq_of_homotopy, intro b,
apply eq_of_pathover_idp, apply r
end
definition pi_pathover_right {f : Πb, C a b} {g : Πb', C a' b'} {p : a = a'}
(r : Π(b' : B a'), f (p⁻¹ ▸ b') =[apo011 C p !tr_pathover] g b') : f =[p] g :=
begin
cases p, apply pathover_idp_of_eq,
apply eq_of_homotopy, intro b,
apply eq_of_pathover_idp, apply r
end
definition pi_pathover_constant {C : A → A' → Type} {f : Π(b : A'), C a b}
{g : Π(b : A'), C a' b} {p : a = a'}
(r : Π(b : A'), f b =[p] g b) : f =[p] g :=
begin
cases p, apply pathover_idp_of_eq,
apply eq_of_homotopy, intro b,
exact eq_of_pathover_idp (r b),
end
/- Maps on paths -/
/- The action of maps given by lambda. -/
definition ap_lambdaD {C : A' → Type} (p : a = a') (f : Πa b, C b) :
ap (λa b, f a b) p = eq_of_homotopy (λb, ap (λa, f a b) p) :=
begin
apply (eq.rec_on p),
apply inverse,
apply eq_of_homotopy_idp
end
/- Dependent paths -/
/- with more implicit arguments the conclusion of the following theorem is
(Π(b : B a), transportD B C p b (f b) = g (transport B p b)) ≃
(transport (λa, Π(b : B a), C a b) p f = g) -/
definition heq_piD (p : a = a') (f : Π(b : B a), C a b)
(g : Π(b' : B a'), C a' b') : (Π(b : B a), p ▸D (f b) = g (p ▸ b)) ≃ (p ▸ f = g) :=
eq.rec_on p (λg, !homotopy_equiv_eq) g
definition heq_pi {C : A → Type} (p : a = a') (f : Π(b : B a), C a)
(g : Π(b' : B a'), C a') : (Π(b : B a), p ▸ (f b) = g (p ▸ b)) ≃ (p ▸ f = g) :=
eq.rec_on p (λg, !homotopy_equiv_eq) g
section
open sigma sigma.ops
/- more implicit arguments:
(Π(b : B a), transport C (sigma_eq p idp) (f b) = g (p ▸ b)) ≃
(Π(b : B a), transportD B (λ(a : A) (b : B a), C ⟨a, b⟩) p b (f b) = g (transport B p b)) -/
definition heq_pi_sigma {C : (Σa, B a) → Type} (p : a = a')
(f : Π(b : B a), C ⟨a, b⟩) (g : Π(b' : B a'), C ⟨a', b'⟩) :
(Π(b : B a), (sigma_eq p !pathover_tr) ▸ (f b) = g (p ▸ b)) ≃
(Π(b : B a), p ▸D (f b) = g (p ▸ b)) :=
eq.rec_on p (λg, !equiv.refl) g
end
/- Functorial action -/
variables (f0 : A' → A) (f1 : Π(a':A'), B (f0 a') → B' a')
/- The functoriality of [forall] is slightly subtle: it is contravariant in the domain type and covariant in the codomain, but the codomain is dependent on the domain. -/
definition pi_functor [unfold-full] : (Π(a:A), B a) → (Π(a':A'), B' a') := λg a', f1 a' (g (f0 a'))
definition ap_pi_functor {g g' : Π(a:A), B a} (h : g ~ g')
: ap (pi_functor f0 f1) (eq_of_homotopy h)
= eq_of_homotopy (λa':A', (ap (f1 a') (h (f0 a')))) :=
begin
apply (equiv_rect (@apd10 A B g g')), intro p, clear h,
cases p,
apply concat,
exact (ap (ap (pi_functor f0 f1)) (eq_of_homotopy_idp g)),
apply symm, apply eq_of_homotopy_idp
end
/- Equivalences -/
definition is_equiv_pi_functor [instance] [H0 : is_equiv f0]
[H1 : Πa', @is_equiv (B (f0 a')) (B' a') (f1 a')] : is_equiv (pi_functor f0 f1) :=
begin
apply (adjointify (pi_functor f0 f1) (pi_functor f0⁻¹
(λ(a : A) (b' : B' (f0⁻¹ a)), transport B (right_inv f0 a) ((f1 (f0⁻¹ a))⁻¹ b')))),
begin
intro h, apply eq_of_homotopy, intro a', esimp,
rewrite [adj f0 a',-transport_compose,fn_tr_eq_tr_fn _ f1,right_inv (f1 _)],
apply apd
end,
begin
intro h, apply eq_of_homotopy, intro a, esimp,
rewrite [left_inv (f1 _)],
apply apd
end
end
definition pi_equiv_pi_of_is_equiv [H : is_equiv f0]
[H1 : Πa', @is_equiv (B (f0 a')) (B' a') (f1 a')] : (Πa, B a) ≃ (Πa', B' a') :=
equiv.mk (pi_functor f0 f1) _
definition pi_equiv_pi (f0 : A' ≃ A) (f1 : Πa', (B (to_fun f0 a') ≃ B' a'))
: (Πa, B a) ≃ (Πa', B' a') :=
pi_equiv_pi_of_is_equiv (to_fun f0) (λa', to_fun (f1 a'))
definition pi_equiv_pi_id {P Q : A → Type} (g : Πa, P a ≃ Q a) : (Πa, P a) ≃ (Πa, Q a) :=
pi_equiv_pi equiv.refl g
/- Truncatedness: any dependent product of n-types is an n-type -/
open is_trunc
definition is_trunc_pi (B : A → Type) (n : trunc_index)
[H : ∀a, is_trunc n (B a)] : is_trunc n (Πa, B a) :=
begin
revert B H,
eapply (trunc_index.rec_on n),
{intro B H,
fapply is_contr.mk,
intro a, apply center,
intro f, apply eq_of_homotopy,
intro x, apply (center_eq (f x))},
{intro n IH B H,
fapply is_trunc_succ_intro, intro f g,
fapply is_trunc_equiv_closed,
apply equiv.symm, apply eq_equiv_homotopy,
apply IH,
intro a,
show is_trunc n (f a = g a), from
is_trunc_eq n (f a) (g a)}
end
local attribute is_trunc_pi [instance]
definition is_trunc_eq_pi [instance] [priority 500] (n : trunc_index) (f g : Πa, B a)
[H : ∀a, is_trunc n (f a = g a)] : is_trunc n (f = g) :=
begin
apply is_trunc_equiv_closed_rev,
apply eq_equiv_homotopy
end
definition is_hprop_pi_eq [instance] [priority 490] (a : A) : is_hprop (Π(a' : A), a = a') :=
is_hprop_of_imp_is_contr
( assume (f : Πa', a = a'),
assert H : is_contr A, from is_contr.mk a f,
_)
/- Symmetry of Π -/
definition is_equiv_flip [instance] {P : A → A' → Type} : is_equiv (@function.flip A A' P) :=
begin
fapply is_equiv.mk,
exact (@function.flip _ _ (function.flip P)),
repeat (intro f; apply idp)
end
definition pi_comm_equiv {P : A → A' → Type} : (Πa b, P a b) ≃ (Πb a, P a b) :=
equiv.mk (@function.flip _ _ P) _
end pi
attribute pi.is_trunc_pi [instance] [priority 1510]
|
5330149e53134fb3a17f73873a325214355d0876 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/data/holor.lean | 2a15a70f4cfcea8f1d1f9ff80acfc2dc5978c106 | [
"Apache-2.0"
] | permissive | gebner/mathlib | eab0150cc4f79ec45d2016a8c21750244a2e7ff0 | cc6a6edc397c55118df62831e23bfbd6e6c6b4ab | refs/heads/master | 1,625,574,853,976 | 1,586,712,827,000 | 1,586,712,827,000 | 99,101,412 | 1 | 0 | Apache-2.0 | 1,586,716,389,000 | 1,501,667,958,000 | Lean | UTF-8 | Lean | false | false | 14,332 | lean | /-
Copyright (c) 2018 Alexander Bentkamp. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Alexander Bentkamp
-/
import algebra.pi_instances
/-!
# Basic properties of holors
Holors are indexed collections of tensor coefficients. Confusingly,
they are often called tensors in physics and in the neural network
community.
A holor is simply a multidimensional array of values. The size of a
holor is specified by a `list ℕ`, whose length is called the dimension
of the holor.
The tensor product of `x₁ : holor α ds₁` and `x₂ : holor α ds₂` is the
holor given by `(x₁ ⊗ x₂) (i₁ ++ i₂) = x₁ i₁ * x₂ i₂`. A holor is "of
rank at most 1" if it is a tensor product of one-dimensional holors.
The CP rank of a holor `x` is the smallest N such that `x` is the sum
of N holors of rank at most 1.
Based on the tensor library found in <https://www.isa-afp.org/entries/Deep_Learning.html>
## References
* <https://en.wikipedia.org/wiki/Tensor_rank_decomposition>
-/
universes u
open list
/-- `holor_index ds` is the type of valid index tuples to identify an entry of a holor of dimensions `ds` -/
def holor_index (ds : list ℕ) : Type := { is : list ℕ // forall₂ (<) is ds}
namespace holor_index
variables {ds₁ ds₂ ds₃ : list ℕ}
def take : Π {ds₁ : list ℕ}, holor_index (ds₁ ++ ds₂) → holor_index ds₁
| ds is := ⟨ list.take (length ds) is.1, forall₂_take_append is.1 ds ds₂ is.2 ⟩
def drop : Π {ds₁ : list ℕ}, holor_index (ds₁ ++ ds₂) → holor_index ds₂
| ds is := ⟨ list.drop (length ds) is.1, forall₂_drop_append is.1 ds ds₂ is.2 ⟩
lemma cast_type (is : list ℕ) (eq : ds₁ = ds₂) (h : forall₂ (<) is ds₁) :
(cast (congr_arg holor_index eq) ⟨is, h⟩).val = is :=
by subst eq; refl
def assoc_right :
holor_index (ds₁ ++ ds₂ ++ ds₃) → holor_index (ds₁ ++ (ds₂ ++ ds₃)) :=
cast (congr_arg holor_index (append_assoc ds₁ ds₂ ds₃))
def assoc_left :
holor_index (ds₁ ++ (ds₂ ++ ds₃)) → holor_index (ds₁ ++ ds₂ ++ ds₃) :=
cast (congr_arg holor_index (append_assoc ds₁ ds₂ ds₃).symm)
lemma take_take :
∀ t : holor_index (ds₁ ++ ds₂ ++ ds₃),
t.assoc_right.take = t.take.take
| ⟨ is , h ⟩ := subtype.eq (by simp [assoc_right,take, cast_type, list.take_take, nat.le_add_right, min_eq_left])
lemma drop_take :
∀ t : holor_index (ds₁ ++ ds₂ ++ ds₃),
t.assoc_right.drop.take = t.take.drop
| ⟨ is , h ⟩ := subtype.eq (by simp [assoc_right, take, drop, cast_type, list.drop_take])
lemma drop_drop :
∀ t : holor_index (ds₁ ++ ds₂ ++ ds₃),
t.assoc_right.drop.drop = t.drop
| ⟨ is , h ⟩ := subtype.eq (by simp [add_comm, assoc_right, drop, cast_type, list.drop_drop])
end holor_index
/-- Holor (indexed collections of tensor coefficients) -/
def holor (α : Type u) (ds:list ℕ) := holor_index ds → α
namespace holor
variables {α : Type} {d : ℕ} {ds : list ℕ} {ds₁ : list ℕ} {ds₂ : list ℕ} {ds₃ : list ℕ}
instance [inhabited α] : inhabited (holor α ds) := ⟨λ t, default α⟩
instance [has_zero α] : has_zero (holor α ds) := ⟨λ t, 0⟩
instance [has_add α] : has_add (holor α ds) := ⟨λ x y t, x t + y t⟩
instance [has_neg α] : has_neg (holor α ds) := ⟨λ a t, - a t⟩
instance [add_semigroup α] : add_semigroup (holor α ds) := by pi_instance
instance [add_comm_semigroup α] : add_comm_semigroup (holor α ds) := by pi_instance
instance [add_monoid α] : add_monoid (holor α ds) := by pi_instance
instance [add_comm_monoid α] : add_comm_monoid (holor α ds) := by pi_instance
instance [add_group α] : add_group (holor α ds) := by pi_instance
instance [add_comm_group α] : add_comm_group (holor α ds) := by pi_instance
/- scalar product -/
instance [has_mul α] : has_scalar α (holor α ds) :=
⟨λ a x, λ t, a * x t⟩
instance [ring α] : module α (holor α ds) := pi.module α
instance [field α] : vector_space α (holor α ds) := ⟨⟩
/-- The tensor product of two holors. -/
def mul [s : has_mul α] (x : holor α ds₁) (y : holor α ds₂) : holor α (ds₁ ++ ds₂) :=
λ t, x (t.take) * y (t.drop)
local infix ` ⊗ ` : 70 := mul
lemma cast_type (eq : ds₁ = ds₂) (a : holor α ds₁) :
cast (congr_arg (holor α) eq) a = (λ t, a (cast (congr_arg holor_index eq.symm) t)) :=
by subst eq; refl
def assoc_right :
holor α (ds₁ ++ ds₂ ++ ds₃) → holor α (ds₁ ++ (ds₂ ++ ds₃)) :=
cast (congr_arg (holor α) (append_assoc ds₁ ds₂ ds₃))
def assoc_left :
holor α (ds₁ ++ (ds₂ ++ ds₃)) → holor α (ds₁ ++ ds₂ ++ ds₃) :=
cast (congr_arg (holor α) (append_assoc ds₁ ds₂ ds₃).symm)
lemma mul_assoc0 [semigroup α] (x : holor α ds₁) (y : holor α ds₂) (z : holor α ds₃) :
x ⊗ y ⊗ z = (x ⊗ (y ⊗ z)).assoc_left :=
funext (assume t : holor_index (ds₁ ++ ds₂ ++ ds₃),
begin
rw assoc_left,
unfold mul,
rw mul_assoc,
rw [←holor_index.take_take, ←holor_index.drop_take, ←holor_index.drop_drop],
rw cast_type,
refl,
rw append_assoc
end)
lemma mul_assoc [semigroup α] (x : holor α ds₁) (y : holor α ds₂) (z : holor α ds₃) :
mul (mul x y) z == (mul x (mul y z)) :=
by simp [cast_heq, mul_assoc0, assoc_left].
lemma mul_left_distrib [distrib α] (x : holor α ds₁) (y : holor α ds₂) (z : holor α ds₂) :
x ⊗ (y + z) = x ⊗ y + x ⊗ z :=
funext (λt, left_distrib (x (holor_index.take t)) (y (holor_index.drop t)) (z (holor_index.drop t)))
lemma mul_right_distrib [distrib α] (x : holor α ds₁) (y : holor α ds₁) (z : holor α ds₂) :
(x + y) ⊗ z = x ⊗ z + y ⊗ z :=
funext (λt, right_distrib (x (holor_index.take t)) (y (holor_index.take t)) (z (holor_index.drop t)))
@[simp] lemma zero_mul {α : Type} [ring α] (x : holor α ds₂) :
(0 : holor α ds₁) ⊗ x = 0 :=
funext (λ t, zero_mul (x (holor_index.drop t)))
@[simp] lemma mul_zero {α : Type} [ring α] (x : holor α ds₁) :
x ⊗ (0 :holor α ds₂) = 0 :=
funext (λ t, mul_zero (x (holor_index.take t)))
lemma mul_scalar_mul [monoid α] (x : holor α []) (y : holor α ds) :
x ⊗ y = x ⟨[], forall₂.nil⟩ • y :=
by simp [mul, has_scalar.smul, holor_index.take, holor_index.drop]
/- holor slices -/
/-- A slice is a subholor consisting of all entries with initial index i. -/
def slice (x : holor α (d :: ds)) (i : ℕ) (h : i < d) : holor α ds :=
(λ is : holor_index ds, x ⟨ i :: is.1, forall₂.cons h is.2⟩)
/-- The 1-dimensional "unit" holor with 1 in the `j`th position. -/
def unit_vec [monoid α] [add_monoid α] (d : ℕ) (j : ℕ) : holor α [d] :=
λ ti, if ti.1 = [j] then 1 else 0
lemma holor_index_cons_decomp (p: holor_index (d :: ds) → Prop) :
Π (t : holor_index (d :: ds)),
(∀ i is, Π h : t.1 = i :: is, p ⟨ i :: is, begin rw [←h], exact t.2 end ⟩ ) → p t
| ⟨[], hforall₂⟩ hp := absurd (forall₂_nil_left_iff.1 hforall₂) (cons_ne_nil d ds)
| ⟨(i :: is), hforall₂⟩ hp := hp i is rfl
/-- Two holors are equal if all their slices are equal. -/
lemma slice_eq (x : holor α (d :: ds)) (y : holor α (d :: ds))
(h : slice x = slice y) : x = y :=
funext $ λ t : holor_index (d :: ds), holor_index_cons_decomp (λ t, x t = y t) t $ λ i is hiis,
have hiisdds: forall₂ (<) (i :: is) (d :: ds), begin rw [←hiis], exact t.2 end,
have hid: i<d, from (forall₂_cons.1 hiisdds).1,
have hisds: forall₂ (<) is ds, from (forall₂_cons.1 hiisdds).2,
calc
x ⟨i :: is, _⟩ = slice x i hid ⟨is, hisds⟩ : congr_arg (λ t, x t) (subtype.eq rfl)
... = slice y i hid ⟨is, hisds⟩ : by rw h
... = y ⟨i :: is, _⟩ : congr_arg (λ t, y t) (subtype.eq rfl)
lemma slice_unit_vec_mul [ring α] {i : ℕ} {j : ℕ}
(hid : i < d) (x : holor α ds) :
slice (unit_vec d j ⊗ x) i hid = if i=j then x else 0 :=
funext $ λ t : holor_index ds, if h : i = j
then by simp [slice, mul, holor_index.take, unit_vec, holor_index.drop, h]
else by simp [slice, mul, holor_index.take, unit_vec, holor_index.drop, h]; refl
lemma slice_add [has_add α] (i : ℕ) (hid : i < d) (x : holor α (d :: ds)) (y : holor α (d :: ds)) :
slice x i hid + slice y i hid = slice (x + y) i hid := funext (λ t, by simp [slice,(+)])
lemma slice_zero [has_zero α] (i : ℕ) (hid : i < d) :
slice (0 : holor α (d :: ds)) i hid = 0 := funext (λ t, by simp [slice]; refl)
lemma slice_sum [add_comm_monoid α] {β : Type}
(i : ℕ) (hid : i < d) (s : finset β) (f : β → holor α (d :: ds)) :
finset.sum s (λ x, slice (f x) i hid) = slice (finset.sum s f) i hid :=
begin
letI := classical.dec_eq β,
refine finset.induction_on s _ _,
{ simp [slice_zero] },
{ intros _ _ h_not_in ih,
rw [finset.sum_insert h_not_in, ih, slice_add, finset.sum_insert h_not_in] }
end
/-- The original holor can be recovered from its slices by multiplying with unit vectors and summing up. -/
@[simp] lemma sum_unit_vec_mul_slice [ring α] (x : holor α (d :: ds)) :
(finset.range d).attach.sum
(λ i, unit_vec d i.1 ⊗ slice x i.1 (nat.succ_le_of_lt (finset.mem_range.1 i.2))) = x :=
begin
apply slice_eq _ _ _,
ext i hid,
rw [←slice_sum],
simp only [slice_unit_vec_mul hid],
rw finset.sum_eq_single (subtype.mk i _),
{ simp, refl },
{ assume (b : {x // x ∈ finset.range d}) (hb : b ∈ (finset.range d).attach) (hbi : b ≠ ⟨i, _⟩),
have hbi' : i ≠ b.val,
{ apply not.imp hbi,
{ assume h0 : i = b.val,
apply subtype.eq,
simp only [h0] },
{ exact finset.mem_range.2 hid } },
simp [hbi']},
{ assume hid' : subtype.mk i _ ∉ finset.attach (finset.range d),
exfalso,
exact absurd (finset.mem_attach _ _) hid'
}
end
/- CP rank -/
/-- `cprank_max1 x` means `x` has CP rank at most 1, that is,
it is the tensor product of 1-dimensional holors. -/
inductive cprank_max1 [has_mul α]: Π {ds}, holor α ds → Prop
| nil (x : holor α []) :
cprank_max1 x
| cons {d} {ds} (x : holor α [d]) (y : holor α ds) :
cprank_max1 y → cprank_max1 (x ⊗ y)
/-- `cprank_max N x` means `x` has CP rank at most `N`, that is,
it can be written as the sum of N holors of rank at most 1. -/
inductive cprank_max [has_mul α] [add_monoid α] : ℕ → Π {ds}, holor α ds → Prop
| zero {ds} :
cprank_max 0 (0 : holor α ds)
| succ n {ds} (x : holor α ds) (y : holor α ds) :
cprank_max1 x → cprank_max n y → cprank_max (n+1) (x + y)
lemma cprank_max_nil [monoid α] [add_monoid α] (x : holor α nil) : cprank_max 1 x :=
have h : _, from cprank_max.succ 0 x 0 (cprank_max1.nil x) (cprank_max.zero),
by rwa [add_zero x, zero_add] at h
lemma cprank_max_1 [monoid α] [add_monoid α] {x : holor α ds}
(h : cprank_max1 x) : cprank_max 1 x :=
have h' : _, from cprank_max.succ 0 x 0 h cprank_max.zero,
by rwa [zero_add, add_zero] at h'
lemma cprank_max_add [monoid α] [add_monoid α]:
∀ {m : ℕ} {n : ℕ} {x : holor α ds} {y : holor α ds},
cprank_max m x → cprank_max n y → cprank_max (m + n) (x + y)
| 0 n x y (cprank_max.zero) hy := by simp [hy]
| (m+1) n _ y (cprank_max.succ k x₁ x₂ hx₁ hx₂) hy :=
begin
simp only [add_comm, add_assoc],
apply cprank_max.succ,
{ assumption },
{ exact cprank_max_add hx₂ hy }
end
lemma cprank_max_mul [ring α] :
∀ (n : ℕ) (x : holor α [d]) (y : holor α ds), cprank_max n y → cprank_max n (x ⊗ y)
| 0 x _ (cprank_max.zero) := by simp [mul_zero x, cprank_max.zero]
| (n+1) x _ (cprank_max.succ k y₁ y₂ hy₁ hy₂) :=
begin
rw mul_left_distrib,
rw nat.add_comm,
apply cprank_max_add,
{ exact cprank_max_1 (cprank_max1.cons _ _ hy₁) },
{ exact cprank_max_mul k x y₂ hy₂ }
end
lemma cprank_max_sum [ring α] {β} {n : ℕ} (s : finset β) (f : β → holor α ds) :
(∀ x ∈ s, cprank_max n (f x)) → cprank_max (s.card * n) (finset.sum s f) :=
by letI := classical.dec_eq β;
exact finset.induction_on s
(by simp [cprank_max.zero])
(begin
assume x s (h_x_notin_s : x ∉ s) ih h_cprank,
simp only [finset.sum_insert h_x_notin_s,finset.card_insert_of_not_mem h_x_notin_s],
rw nat.right_distrib,
simp only [nat.one_mul, nat.add_comm],
have ih' : cprank_max (finset.card s * n) (finset.sum s f),
{
apply ih,
assume (x : β) (h_x_in_s: x ∈ s),
simp only [h_cprank, finset.mem_insert_of_mem, h_x_in_s]
},
exact (cprank_max_add (h_cprank x (finset.mem_insert_self x s)) ih')
end)
lemma cprank_max_upper_bound [ring α] : Π {ds}, ∀ x : holor α ds, cprank_max ds.prod x
| [] x := cprank_max_nil x
| (d :: ds) x :=
have h_summands : Π (i : {x // x ∈ finset.range d}),
cprank_max ds.prod (unit_vec d i.1 ⊗ slice x i.1 (mem_range.1 i.2)),
from λ i, cprank_max_mul _ _ _ (cprank_max_upper_bound (slice x i.1 (mem_range.1 i.2))),
have h_dds_prod : (list.cons d ds).prod = finset.card (finset.range d) * prod ds,
by simp [finset.card_range],
have cprank_max (finset.card (finset.attach (finset.range d)) * prod ds)
(finset.sum (finset.attach (finset.range d))
(λ (i : {x // x ∈ finset.range d}), unit_vec d (i.val)⊗slice x (i.val) (mem_range.1 i.2))),
from cprank_max_sum (finset.range d).attach _ (λ i _, h_summands i),
have h_cprank_max_sum : cprank_max (finset.card (finset.range d) * prod ds)
(finset.sum (finset.attach (finset.range d))
(λ (i : {x // x ∈ finset.range d}), unit_vec d (i.val)⊗slice x (i.val) (mem_range.1 i.2))),
by rwa [finset.card_attach] at this,
begin
rw [←sum_unit_vec_mul_slice x],
rw [h_dds_prod],
exact h_cprank_max_sum,
end
/-- The CP rank of a holor `x`: the smallest N such that
`x` can be written as the sum of N holors of rank at most 1. -/
noncomputable def cprank [ring α] (x : holor α ds) : nat :=
@nat.find (λ n, cprank_max n x) (classical.dec_pred _) ⟨ds.prod, cprank_max_upper_bound x⟩
lemma cprank_upper_bound [ring α] :
Π {ds}, ∀ x : holor α ds, cprank x ≤ ds.prod :=
λ ds (x : holor α ds),
by letI := classical.dec_pred (λ (n : ℕ), cprank_max n x);
exact nat.find_min'
⟨ds.prod, show (λ n, cprank_max n x) ds.prod, from cprank_max_upper_bound x⟩
(cprank_max_upper_bound x)
end holor
|
9fc3cfdbb121f9a7e50b9aae371369be271976d2 | e0f9ba56b7fedc16ef8697f6caeef5898b435143 | /src/topology/metric_space/emetric_space.lean | ebfbbdbaca2056c5be063d574227bebe8b10f2a8 | [
"Apache-2.0"
] | permissive | anrddh/mathlib | 6a374da53c7e3a35cb0298b0cd67824efef362b4 | a4266a01d2dcb10de19369307c986d038c7bb6a6 | refs/heads/master | 1,656,710,827,909 | 1,589,560,456,000 | 1,589,560,456,000 | 264,271,800 | 0 | 0 | Apache-2.0 | 1,589,568,062,000 | 1,589,568,061,000 | null | UTF-8 | Lean | false | false | 40,259 | lean | /-
Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel
-/
import data.real.ennreal
import topology.uniform_space.uniform_embedding
import topology.uniform_space.pi
import topology.uniform_space.uniform_convergence
/-!
# Extended metric spaces
This file is devoted to the definition and study of `emetric_spaces`, i.e., metric
spaces in which the distance is allowed to take the value ∞. This extended distance is
called `edist`, and takes values in `ennreal`.
Many definitions and theorems expected on emetric spaces are already introduced on uniform spaces and
topological spaces. For example:
open and closed sets, compactness, completeness, continuity and uniform continuity
The class `emetric_space` therefore extends `uniform_space` (and `topological_space`).
-/
open set filter classical
noncomputable theory
open_locale uniformity topological_space
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
/-- Characterizing uniformities associated to a (generalized) distance function `D`
in terms of the elements of the uniformity. -/
theorem uniformity_dist_of_mem_uniformity [linear_order β] {U : filter (α × α)} (z : β) (D : α → α → β)
(H : ∀ s, s ∈ U ↔ ∃ε>z, ∀{a b:α}, D a b < ε → (a, b) ∈ s) :
U = ⨅ ε>z, principal {p:α×α | D p.1 p.2 < ε} :=
le_antisymm
(le_infi $ λ ε, le_infi $ λ ε0, le_principal_iff.2 $ (H _).2 ⟨ε, ε0, λ a b, id⟩)
(λ r ur, let ⟨ε, ε0, h⟩ := (H _).1 ur in
mem_infi_sets ε $ mem_infi_sets ε0 $ mem_principal_sets.2 $ λ ⟨a, b⟩, h)
class has_edist (α : Type*) := (edist : α → α → ennreal)
export has_edist (edist)
/- Design note: one could define an `emetric_space` just by giving `edist`, and then
derive an instance of `uniform_space` by taking the natural uniform structure
associated to the distance. This creates diamonds problem for products, as the
uniform structure on the product of two emetric spaces could be obtained first
by obtaining two uniform spaces and then taking their products, or by taking
the product of the emetric spaces and then the associated uniform structure.
The two uniform structure we have just described are equal, but not defeq, which
creates a lot of problem.
The idea is to add, in the very definition of an `emetric_space`, a uniform structure
with a uniformity which equal to the one given by the distance, but maybe not defeq.
And the instance from `emetric_space` to `uniform_space` uses this uniformity.
In this way, when we create the product of emetric spaces, we put in the product
the uniformity corresponding to the product of the uniformities. There is one more
proof obligation, that this product uniformity is equal to the uniformity corresponding
to the product metric. But the diamond problem disappears.
The same trick is used in the definition of a metric space, where one stores as well
a uniform structure and an edistance. -/
/-- Creating a uniform space from an extended distance. -/
def uniform_space_of_edist
(edist : α → α → ennreal)
(edist_self : ∀ x : α, edist x x = 0)
(edist_comm : ∀ x y : α, edist x y = edist y x)
(edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z) : uniform_space α :=
uniform_space.of_core {
uniformity := (⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε}),
refl := le_infi $ assume ε, le_infi $
by simp [set.subset_def, id_rel, edist_self, (>)] {contextual := tt},
comp :=
le_infi $ assume ε, le_infi $ assume h,
have (2 : ennreal) = (2 : ℕ) := by simp,
have A : 0 < ε / 2 := ennreal.div_pos_iff.2
⟨ne_of_gt h, by { convert ennreal.nat_ne_top 2 }⟩,
lift'_le
(mem_infi_sets (ε / 2) $ mem_infi_sets A (subset.refl _)) $
have ∀ (a b c : α), edist a c < ε / 2 → edist c b < ε / 2 → edist a b < ε,
from assume a b c hac hcb,
calc edist a b ≤ edist a c + edist c b : edist_triangle _ _ _
... < ε / 2 + ε / 2 : ennreal.add_lt_add hac hcb
... = ε : by rw [ennreal.add_halves],
by simpa [comp_rel],
symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h,
tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [edist_comm] }
section prio
set_option default_priority 100 -- see Note [default priority]
/-- Extended metric spaces, with an extended distance `edist` possibly taking the
value ∞
Each emetric space induces a canonical `uniform_space` and hence a canonical `topological_space`.
This is enforced in the type class definition, by extending the `uniform_space` structure. When
instantiating an `emetric_space` structure, the uniformity fields are not necessary, they will be
filled in by default. There is a default value for the uniformity, that can be substituted
in cases of interest, for instance when instantiating an `emetric_space` structure
on a product.
Continuity of `edist` is finally proving in `topology.instances.ennreal`
-/
class emetric_space (α : Type u) extends has_edist α : Type u :=
(edist_self : ∀ x : α, edist x x = 0)
(eq_of_edist_eq_zero : ∀ {x y : α}, edist x y = 0 → x = y)
(edist_comm : ∀ x y : α, edist x y = edist y x)
(edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z)
(to_uniform_space : uniform_space α := uniform_space_of_edist edist edist_self edist_comm edist_triangle)
(uniformity_edist : 𝓤 α = ⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε} . control_laws_tac)
end prio
/- emetric spaces are less common than metric spaces. Therefore, we work in a dedicated
namespace, while notions associated to metric spaces are mostly in the root namespace. -/
variables [emetric_space α]
@[priority 100] -- see Note [lower instance priority]
instance emetric_space.to_uniform_space' : uniform_space α :=
emetric_space.to_uniform_space
export emetric_space (edist_self eq_of_edist_eq_zero edist_comm edist_triangle)
attribute [simp] edist_self
/-- Characterize the equality of points by the vanishing of their extended distance -/
@[simp] theorem edist_eq_zero {x y : α} : edist x y = 0 ↔ x = y :=
iff.intro eq_of_edist_eq_zero (assume : x = y, this ▸ edist_self _)
@[simp] theorem zero_eq_edist {x y : α} : 0 = edist x y ↔ x = y :=
iff.intro (assume h, eq_of_edist_eq_zero (h.symm))
(assume : x = y, this ▸ (edist_self _).symm)
theorem edist_le_zero {x y : α} : (edist x y ≤ 0) ↔ x = y :=
le_zero_iff_eq.trans edist_eq_zero
/-- Triangle inequality for the extended distance -/
theorem edist_triangle_left (x y z : α) : edist x y ≤ edist z x + edist z y :=
by rw edist_comm z; apply edist_triangle
theorem edist_triangle_right (x y z : α) : edist x y ≤ edist x z + edist y z :=
by rw edist_comm y; apply edist_triangle
lemma edist_triangle4 (x y z t : α) :
edist x t ≤ edist x y + edist y z + edist z t :=
calc
edist x t ≤ edist x z + edist z t : edist_triangle x z t
... ≤ (edist x y + edist y z) + edist z t : add_le_add_right' (edist_triangle x y z)
/-- The triangle (polygon) inequality for sequences of points; `finset.Ico` version. -/
lemma edist_le_Ico_sum_edist (f : ℕ → α) {m n} (h : m ≤ n) :
edist (f m) (f n) ≤ (finset.Ico m n).sum (λ i, edist (f i) (f (i + 1))) :=
begin
revert n,
refine nat.le_induction _ _,
{ simp only [finset.sum_empty, finset.Ico.self_eq_empty, edist_self],
-- TODO: Why doesn't Lean close this goal automatically? `apply le_refl` fails too.
exact le_refl (0:ennreal) },
{ assume n hn hrec,
calc edist (f m) (f (n+1)) ≤ edist (f m) (f n) + edist (f n) (f (n+1)) : edist_triangle _ _ _
... ≤ (finset.Ico m n).sum _ + _ : add_le_add' hrec (le_refl _)
... = (finset.Ico m (n+1)).sum _ :
by rw [finset.Ico.succ_top hn, finset.sum_insert, add_comm]; simp }
end
/-- The triangle (polygon) inequality for sequences of points; `finset.range` version. -/
lemma edist_le_range_sum_edist (f : ℕ → α) (n : ℕ) :
edist (f 0) (f n) ≤ (finset.range n).sum (λ i, edist (f i) (f (i + 1))) :=
finset.Ico.zero_bot n ▸ edist_le_Ico_sum_edist f (nat.zero_le n)
/-- A version of `edist_le_Ico_sum_edist` with each intermediate distance replaced
with an upper estimate. -/
lemma edist_le_Ico_sum_of_edist_le {f : ℕ → α} {m n} (hmn : m ≤ n)
{d : ℕ → ennreal} (hd : ∀ {k}, m ≤ k → k < n → edist (f k) (f (k + 1)) ≤ d k) :
edist (f m) (f n) ≤ (finset.Ico m n).sum d :=
le_trans (edist_le_Ico_sum_edist f hmn) $
finset.sum_le_sum $ λ k hk, hd (finset.Ico.mem.1 hk).1 (finset.Ico.mem.1 hk).2
/-- A version of `edist_le_range_sum_edist` with each intermediate distance replaced
with an upper estimate. -/
lemma edist_le_range_sum_of_edist_le {f : ℕ → α} (n : ℕ)
{d : ℕ → ennreal} (hd : ∀ {k}, k < n → edist (f k) (f (k + 1)) ≤ d k) :
edist (f 0) (f n) ≤ (finset.range n).sum d :=
finset.Ico.zero_bot n ▸ edist_le_Ico_sum_of_edist_le (zero_le n) (λ _ _, hd)
/-- Two points coincide if their distance is `< ε` for all positive ε -/
theorem eq_of_forall_edist_le {x y : α} (h : ∀ε, ε > 0 → edist x y ≤ ε) : x = y :=
eq_of_edist_eq_zero (eq_of_le_of_forall_le_of_dense bot_le h)
/-- Reformulation of the uniform structure in terms of the extended distance -/
theorem uniformity_edist :
𝓤 α = ⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε} :=
emetric_space.uniformity_edist
theorem uniformity_basis_edist :
(𝓤 α).has_basis (λ ε : ennreal, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 < ε}) :=
(@uniformity_edist α _).symm ▸ has_basis_binfi_principal
(λ r hr p hp, ⟨min r p, lt_min hr hp,
λ x hx, lt_of_lt_of_le hx (min_le_left _ _),
λ x hx, lt_of_lt_of_le hx (min_le_right _ _)⟩)
⟨1, ennreal.zero_lt_one⟩
/-- Characterization of the elements of the uniformity in terms of the extended distance -/
theorem mem_uniformity_edist {s : set (α×α)} :
s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, edist a b < ε → (a, b) ∈ s) :=
uniformity_basis_edist.mem_uniformity_iff
/-- Given `f : β → ennreal`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`.
For specific bases see `uniformity_basis_edist`, `uniformity_basis_edist'`,
`uniformity_basis_edist_nnreal`, and `uniformity_basis_edist_inv_nat`. -/
protected theorem emetric.mk_uniformity_basis {β : Type*} {p : β → Prop} {f : β → ennreal}
(hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) :
(𝓤 α).has_basis p (λ x, {p:α×α | edist p.1 p.2 < f x}) :=
begin
refine ⟨λ s, uniformity_basis_edist.mem_iff.trans _⟩,
split,
{ rintros ⟨ε, ε₀, hε⟩,
rcases hf ε ε₀ with ⟨i, hi, H⟩,
exact ⟨i, hi, λ x hx, hε $ lt_of_lt_of_le hx H⟩ },
{ exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, H⟩ }
end
/-- Given `f : β → ennreal`, if `f` sends `{i | p i}` to a set of positive numbers
accumulating to zero, then closed `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`.
For specific bases see `uniformity_basis_edist_le` and `uniformity_basis_edist_le'`. -/
protected theorem emetric.mk_uniformity_basis_le {β : Type*} {p : β → Prop} {f : β → ennreal}
(hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) :
(𝓤 α).has_basis p (λ x, {p:α×α | edist p.1 p.2 ≤ f x}) :=
begin
refine ⟨λ s, uniformity_basis_edist.mem_iff.trans _⟩,
split,
{ rintros ⟨ε, ε₀, hε⟩,
rcases dense ε₀ with ⟨ε', hε'⟩,
rcases hf ε' hε'.1 with ⟨i, hi, H⟩,
exact ⟨i, hi, λ x hx, hε $ lt_of_le_of_lt (le_trans hx H) hε'.2⟩ },
{ exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, λ x hx, H (le_of_lt hx)⟩ }
end
theorem uniformity_basis_edist_le :
(𝓤 α).has_basis (λ ε : ennreal, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 ≤ ε}) :=
emetric.mk_uniformity_basis_le (λ _, id) (λ ε ε₀, ⟨ε, ε₀, le_refl ε⟩)
theorem uniformity_basis_edist' (ε' : ennreal) (hε' : 0 < ε') :
(𝓤 α).has_basis (λ ε : ennreal, ε ∈ Ioo 0 ε') (λ ε, {p:α×α | edist p.1 p.2 < ε}) :=
emetric.mk_uniformity_basis (λ _, and.left)
(λ ε ε₀, let ⟨δ, hδ⟩ := dense hε' in
⟨min ε δ, ⟨lt_min ε₀ hδ.1, lt_of_le_of_lt (min_le_right _ _) hδ.2⟩, min_le_left _ _⟩)
theorem uniformity_basis_edist_le' (ε' : ennreal) (hε' : 0 < ε') :
(𝓤 α).has_basis (λ ε : ennreal, ε ∈ Ioo 0 ε') (λ ε, {p:α×α | edist p.1 p.2 ≤ ε}) :=
emetric.mk_uniformity_basis_le (λ _, and.left)
(λ ε ε₀, let ⟨δ, hδ⟩ := dense hε' in
⟨min ε δ, ⟨lt_min ε₀ hδ.1, lt_of_le_of_lt (min_le_right _ _) hδ.2⟩, min_le_left _ _⟩)
theorem uniformity_basis_edist_nnreal :
(𝓤 α).has_basis (λ ε : nnreal, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 < ε}) :=
emetric.mk_uniformity_basis (λ _, ennreal.coe_pos.2)
(λ ε ε₀, let ⟨δ, hδ⟩ := ennreal.lt_iff_exists_nnreal_btwn.1 ε₀ in
⟨δ, ennreal.coe_pos.1 hδ.1, le_of_lt hδ.2⟩)
theorem uniformity_basis_edist_inv_nat :
(𝓤 α).has_basis (λ _, true) (λ n:ℕ, {p:α×α | edist p.1 p.2 < (↑n)⁻¹}) :=
emetric.mk_uniformity_basis
(λ n _, ennreal.inv_pos.2 $ ennreal.nat_ne_top n)
(λ ε ε₀, let ⟨n, hn⟩ := ennreal.exists_inv_nat_lt (ne_of_gt ε₀) in ⟨n, trivial, le_of_lt hn⟩)
/-- Fixed size neighborhoods of the diagonal belong to the uniform structure -/
theorem edist_mem_uniformity {ε:ennreal} (ε0 : 0 < ε) :
{p:α×α | edist p.1 p.2 < ε} ∈ 𝓤 α :=
mem_uniformity_edist.2 ⟨ε, ε0, λ a b, id⟩
namespace emetric
theorem uniformity_has_countable_basis : is_countably_generated (𝓤 α) :=
is_countably_generated_of_seq ⟨_, uniformity_basis_edist_inv_nat.eq_infi⟩
/-- ε-δ characterization of uniform continuity on emetric spaces -/
theorem uniform_continuous_iff [emetric_space β] {f : α → β} :
uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0,
∀{a b:α}, edist a b < δ → edist (f a) (f b) < ε :=
uniformity_basis_edist.uniform_continuous_iff uniformity_basis_edist
/-- ε-δ characterization of uniform embeddings on emetric spaces -/
theorem uniform_embedding_iff [emetric_space β] {f : α → β} :
uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧
∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, edist (f a) (f b) < ε → edist a b < δ :=
uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl
⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (edist_mem_uniformity δ0),
⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 tu in
⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩,
λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_edist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in
⟨_, edist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩
/-- A map between emetric spaces is a uniform embedding if and only if the edistance between `f x`
and `f y` is controlled in terms of the distance between `x` and `y` and conversely. -/
theorem uniform_embedding_iff' [emetric_space β] {f : α → β} :
uniform_embedding f ↔
(∀ ε > 0, ∃ δ > 0, ∀ {a b : α}, edist a b < δ → edist (f a) (f b) < ε) ∧
(∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, edist (f a) (f b) < ε → edist a b < δ) :=
begin
split,
{ assume h,
exact ⟨uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1,
(uniform_embedding_iff.1 h).2.2⟩ },
{ rintros ⟨h₁, h₂⟩,
refine uniform_embedding_iff.2 ⟨_, uniform_continuous_iff.2 h₁, h₂⟩,
assume x y hxy,
have : edist x y ≤ 0,
{ refine le_of_forall_lt' (λδ δpos, _),
rcases h₂ δ δpos with ⟨ε, εpos, hε⟩,
have : edist (f x) (f y) < ε, by simpa [hxy],
exact hε this },
simpa using this }
end
/-- ε-δ characterization of Cauchy sequences on emetric spaces -/
protected lemma cauchy_iff {f : filter α} :
cauchy f ↔ f ≠ ⊥ ∧ ∀ ε > 0, ∃ t ∈ f, ∀ x y ∈ t, edist x y < ε :=
uniformity_basis_edist.cauchy_iff
/-- A very useful criterion to show that a space is complete is to show that all sequences
which satisfy a bound of the form `edist (u n) (u m) < B N` for all `n m ≥ N` are
converging. This is often applied for `B N = 2^{-N}`, i.e., with a very fast convergence to
`0`, which makes it possible to use arguments of converging series, while this is impossible
to do in general for arbitrary Cauchy sequences. -/
theorem complete_of_convergent_controlled_sequences (B : ℕ → ennreal) (hB : ∀n, 0 < B n)
(H : ∀u : ℕ → α, (∀N n m : ℕ, N ≤ n → N ≤ m → edist (u n) (u m) < B N) → ∃x, tendsto u at_top (𝓝 x)) :
complete_space α :=
uniform_space.complete_of_convergent_controlled_sequences
uniformity_has_countable_basis
(λ n, {p:α×α | edist p.1 p.2 < B n}) (λ n, edist_mem_uniformity $ hB n) H
/-- A sequentially complete emetric space is complete. -/
theorem complete_of_cauchy_seq_tendsto :
(∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) → complete_space α :=
uniform_space.complete_of_cauchy_seq_tendsto uniformity_has_countable_basis
/-- Expressing locally uniform convergence on a set using `edist`. -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma tendsto_locally_uniformly_on_iff {ι : Type*} [topological_space β]
{F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} :
tendsto_locally_uniformly_on F f p s ↔
∀ ε > 0, ∀ x ∈ s, ∃ t ∈ nhds_within x s, ∀ᶠ n in p, ∀ y ∈ t, edist (f y) (F n y) < ε :=
begin
refine ⟨λ H ε hε, H _ (edist_mem_uniformity hε), λ H u hu x hx, _⟩,
rcases mem_uniformity_edist.1 hu with ⟨ε, εpos, hε⟩,
rcases H ε εpos x hx with ⟨t, ht, Ht⟩,
exact ⟨t, ht, Ht.mono (λ n hs x hx, hε (hs x hx))⟩
end
/-- Expressing uniform convergence on a set using `edist`. -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma tendsto_uniformly_on_iff {ι : Type*}
{F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} :
tendsto_uniformly_on F f p s ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x ∈ s, edist (f x) (F n x) < ε :=
begin
refine ⟨λ H ε hε, H _ (edist_mem_uniformity hε), λ H u hu, _⟩,
rcases mem_uniformity_edist.1 hu with ⟨ε, εpos, hε⟩,
exact (H ε εpos).mono (λ n hs x hx, hε (hs x hx))
end
/-- Expressing locally uniform convergence using `edist`. -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma tendsto_locally_uniformly_iff {ι : Type*} [topological_space β]
{F : ι → β → α} {f : β → α} {p : filter ι} :
tendsto_locally_uniformly F f p ↔
∀ ε > 0, ∀ (x : β), ∃ t ∈ 𝓝 x, ∀ᶠ n in p, ∀ y ∈ t, edist (f y) (F n y) < ε :=
by simp [← nhds_within_univ, ← tendsto_locally_uniformly_on_univ, tendsto_locally_uniformly_on_iff]
/-- Expressing uniform convergence using `edist`. -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
lemma tendsto_uniformly_iff {ι : Type*}
{F : ι → β → α} {f : β → α} {p : filter ι} :
tendsto_uniformly F f p ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x, edist (f x) (F n x) < ε :=
by { rw [← tendsto_uniformly_on_univ, tendsto_uniformly_on_iff], simp }
end emetric
open emetric
/-- An emetric space is separated -/
@[priority 100] -- see Note [lower instance priority]
instance to_separated : separated α :=
separated_def.2 $ λ x y h, eq_of_forall_edist_le $
λ ε ε0, le_of_lt (h _ (edist_mem_uniformity ε0))
/-- Auxiliary function to replace the uniformity on an emetric space with
a uniformity which is equal to the original one, but maybe not defeq.
This is useful if one wants to construct an emetric space with a
specified uniformity. -/
def emetric_space.replace_uniformity {α} [U : uniform_space α] (m : emetric_space α)
(H : @uniformity _ U = @uniformity _ emetric_space.to_uniform_space) :
emetric_space α :=
{ edist := @edist _ m.to_has_edist,
edist_self := edist_self,
eq_of_edist_eq_zero := @eq_of_edist_eq_zero _ _,
edist_comm := edist_comm,
edist_triangle := edist_triangle,
to_uniform_space := U,
uniformity_edist := H.trans (@emetric_space.uniformity_edist α _) }
/-- The extended metric induced by an injective function taking values in an emetric space. -/
def emetric_space.induced {α β} (f : α → β) (hf : function.injective f)
(m : emetric_space β) : emetric_space α :=
{ edist := λ x y, edist (f x) (f y),
edist_self := λ x, edist_self _,
eq_of_edist_eq_zero := λ x y h, hf (edist_eq_zero.1 h),
edist_comm := λ x y, edist_comm _ _,
edist_triangle := λ x y z, edist_triangle _ _ _,
to_uniform_space := uniform_space.comap f m.to_uniform_space,
uniformity_edist := begin
apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, edist (f x) (f y)),
refine λ s, mem_comap_sets.trans _,
split; intro H,
{ rcases H with ⟨r, ru, rs⟩,
rcases mem_uniformity_edist.1 ru with ⟨ε, ε0, hε⟩,
refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h },
{ rcases H with ⟨ε, ε0, hε⟩,
exact ⟨_, edist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ }
end }
/-- Emetric space instance on subsets of emetric spaces -/
instance {α : Type*} {p : α → Prop} [t : emetric_space α] : emetric_space (subtype p) :=
t.induced coe (λ x y, subtype.coe_ext.2)
/-- The extended distance on a subset of an emetric space is the restriction of
the original distance, by definition -/
theorem subtype.edist_eq {p : α → Prop} (x y : subtype p) : edist x y = edist (x : α) y := rfl
/-- The product of two emetric spaces, with the max distance, is an extended
metric spaces. We make sure that the uniform structure thus constructed is the one
corresponding to the product of uniform spaces, to avoid diamond problems. -/
instance prod.emetric_space_max [emetric_space β] : emetric_space (α × β) :=
{ edist := λ x y, max (edist x.1 y.1) (edist x.2 y.2),
edist_self := λ x, by simp,
eq_of_edist_eq_zero := λ x y h, begin
cases max_le_iff.1 (le_of_eq h) with h₁ h₂,
have A : x.fst = y.fst := edist_le_zero.1 h₁,
have B : x.snd = y.snd := edist_le_zero.1 h₂,
exact prod.ext_iff.2 ⟨A, B⟩
end,
edist_comm := λ x y, by simp [edist_comm],
edist_triangle := λ x y z, max_le
(le_trans (edist_triangle _ _ _) (add_le_add' (le_max_left _ _) (le_max_left _ _)))
(le_trans (edist_triangle _ _ _) (add_le_add' (le_max_right _ _) (le_max_right _ _))),
uniformity_edist := begin
refine uniformity_prod.trans _,
simp [emetric_space.uniformity_edist, comap_infi],
rw ← infi_inf_eq, congr, funext,
rw ← infi_inf_eq, congr, funext,
simp [inf_principal, ext_iff, max_lt_iff]
end,
to_uniform_space := prod.uniform_space }
lemma prod.edist_eq [emetric_space β] (x y : α × β) :
edist x y = max (edist x.1 y.1) (edist x.2 y.2) :=
rfl
section pi
open finset
variables {π : β → Type*} [fintype β]
/-- The product of a finite number of emetric spaces, with the max distance, is still
an emetric space.
This construction would also work for infinite products, but it would not give rise
to the product topology. Hence, we only formalize it in the good situation of finitely many
spaces. -/
instance emetric_space_pi [∀b, emetric_space (π b)] : emetric_space (Πb, π b) :=
{ edist := λ f g, finset.sup univ (λb, edist (f b) (g b)),
edist_self := assume f, bot_unique $ finset.sup_le $ by simp,
edist_comm := assume f g, by unfold edist; congr; funext a; exact edist_comm _ _,
edist_triangle := assume f g h,
begin
simp only [finset.sup_le_iff],
assume b hb,
exact le_trans (edist_triangle _ (g b) _) (add_le_add' (le_sup hb) (le_sup hb))
end,
eq_of_edist_eq_zero := assume f g eq0,
begin
have eq1 : sup univ (λ (b : β), edist (f b) (g b)) ≤ 0 := le_of_eq eq0,
simp only [finset.sup_le_iff] at eq1,
exact (funext $ assume b, edist_le_zero.1 $ eq1 b $ mem_univ b),
end,
to_uniform_space := Pi.uniform_space _,
uniformity_edist := begin
simp only [Pi.uniformity, emetric_space.uniformity_edist, comap_infi, gt_iff_lt, preimage_set_of_eq,
comap_principal],
rw infi_comm, congr, funext ε,
rw infi_comm, congr, funext εpos,
change 0 < ε at εpos,
simp [ext_iff, εpos]
end }
end pi
namespace emetric
variables {x y z : α} {ε ε₁ ε₂ : ennreal} {s : set α}
/-- `emetric.ball x ε` is the set of all points `y` with `edist y x < ε` -/
def ball (x : α) (ε : ennreal) : set α := {y | edist y x < ε}
@[simp] theorem mem_ball : y ∈ ball x ε ↔ edist y x < ε := iff.rfl
theorem mem_ball' : y ∈ ball x ε ↔ edist x y < ε := by rw edist_comm; refl
/-- `emetric.closed_ball x ε` is the set of all points `y` with `edist y x ≤ ε` -/
def closed_ball (x : α) (ε : ennreal) := {y | edist y x ≤ ε}
@[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ edist y x ≤ ε := iff.rfl
theorem ball_subset_closed_ball : ball x ε ⊆ closed_ball x ε :=
assume y, by simp; intros h; apply le_of_lt h
theorem pos_of_mem_ball (hy : y ∈ ball x ε) : 0 < ε :=
lt_of_le_of_lt (zero_le _) hy
theorem mem_ball_self (h : 0 < ε) : x ∈ ball x ε :=
show edist x x < ε, by rw edist_self; assumption
theorem mem_closed_ball_self : x ∈ closed_ball x ε :=
show edist x x ≤ ε, by rw edist_self; exact bot_le
theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε :=
by simp [edist_comm]
theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ :=
λ y (yx : _ < ε₁), lt_of_lt_of_le yx h
theorem closed_ball_subset_closed_ball (h : ε₁ ≤ ε₂) :
closed_ball x ε₁ ⊆ closed_ball x ε₂ :=
λ y (yx : _ ≤ ε₁), le_trans yx h
theorem ball_disjoint (h : ε₁ + ε₂ ≤ edist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ :=
eq_empty_iff_forall_not_mem.2 $ λ z ⟨h₁, h₂⟩,
not_lt_of_le (edist_triangle_left x y z)
(lt_of_lt_of_le (ennreal.add_lt_add h₁ h₂) h)
theorem ball_subset (h : edist x y + ε₁ ≤ ε₂) (h' : edist x y < ⊤) : ball x ε₁ ⊆ ball y ε₂ :=
λ z zx, calc
edist z y ≤ edist z x + edist x y : edist_triangle _ _ _
... = edist x y + edist z x : add_comm _ _
... < edist x y + ε₁ : (ennreal.add_lt_add_iff_left h').2 zx
... ≤ ε₂ : h
theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε :=
begin
have : 0 < ε - edist y x := by simpa using h,
refine ⟨ε - edist y x, this, ball_subset _ _⟩,
{ rw ennreal.add_sub_cancel_of_le (le_of_lt h), apply le_refl _},
{ have : edist y x ≠ ⊤ := ne_top_of_lt h, apply lt_top_iff_ne_top.2 this }
end
theorem ball_eq_empty_iff : ball x ε = ∅ ↔ ε = 0 :=
eq_empty_iff_forall_not_mem.trans
⟨λh, le_bot_iff.1 (le_of_not_gt (λ ε0, h _ (mem_ball_self ε0))),
λε0 y h, not_lt_of_le (le_of_eq ε0) (pos_of_mem_ball h)⟩
/-- Relation “two points are at a finite edistance” is an equivalence relation. -/
def edist_lt_top_setoid : setoid α :=
{ r := λ x y, edist x y < ⊤,
iseqv := ⟨λ x, by { rw edist_self, exact ennreal.coe_lt_top },
λ x y h, by rwa edist_comm,
λ x y z hxy hyz, lt_of_le_of_lt (edist_triangle x y z) (ennreal.add_lt_top.2 ⟨hxy, hyz⟩)⟩ }
@[simp] lemma ball_zero : ball x 0 = ∅ :=
by rw [emetric.ball_eq_empty_iff]
theorem nhds_basis_eball : (𝓝 x).has_basis (λ ε:ennreal, 0 < ε) (ball x) :=
nhds_basis_uniformity uniformity_basis_edist
theorem nhds_eq : 𝓝 x = (⨅ε>0, principal (ball x ε)) :=
nhds_basis_eball.eq_binfi
theorem mem_nhds_iff : s ∈ 𝓝 x ↔ ∃ε>0, ball x ε ⊆ s := nhds_basis_eball.mem_iff
theorem is_open_iff : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s :=
by simp [is_open_iff_nhds, mem_nhds_iff]
theorem is_open_ball : is_open (ball x ε) :=
is_open_iff.2 $ λ y, exists_ball_subset_ball
theorem is_closed_ball_top : is_closed (ball x ⊤) :=
is_open_iff.2 $ λ y hy, ⟨⊤, ennreal.coe_lt_top, subset_compl_iff_disjoint.2 $
ball_disjoint $ by { rw ennreal.top_add, exact le_of_not_lt hy }⟩
theorem ball_mem_nhds (x : α) {ε : ennreal} (ε0 : 0 < ε) : ball x ε ∈ 𝓝 x :=
mem_nhds_sets is_open_ball (mem_ball_self ε0)
/-- ε-characterization of the closure in emetric spaces -/
@[nolint ge_or_gt] -- see Note [nolint_ge]
theorem mem_closure_iff :
x ∈ closure s ↔ ∀ε>0, ∃y ∈ s, edist x y < ε :=
(mem_closure_iff_nhds_basis nhds_basis_eball).trans $
by simp only [mem_ball, edist_comm x]
theorem tendsto_nhds {f : filter β} {u : β → α} {a : α} :
tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, edist (u x) a < ε :=
nhds_basis_eball.tendsto_right_iff
theorem tendsto_at_top [nonempty β] [semilattice_sup β] (u : β → α) {a : α} :
tendsto u at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, edist (u n) a < ε :=
(at_top_basis.tendsto_iff nhds_basis_eball).trans $
by simp only [exists_prop, true_and, mem_Ici, mem_ball]
/-- In an emetric space, Cauchy sequences are characterized by the fact that, eventually,
the edistance between its elements is arbitrarily small -/
theorem cauchy_seq_iff [nonempty β] [semilattice_sup β] {u : β → α} :
cauchy_seq u ↔ ∀ε>0, ∃N, ∀m n≥N, edist (u m) (u n) < ε :=
uniformity_basis_edist.cauchy_seq_iff
/-- A variation around the emetric characterization of Cauchy sequences -/
theorem cauchy_seq_iff' [nonempty β] [semilattice_sup β] {u : β → α} :
cauchy_seq u ↔ ∀ε>(0 : ennreal), ∃N, ∀n≥N, edist (u n) (u N) < ε :=
uniformity_basis_edist.cauchy_seq_iff'
/-- A variation of the emetric characterization of Cauchy sequences that deals with
`nnreal` upper bounds. -/
theorem cauchy_seq_iff_nnreal [nonempty β] [semilattice_sup β] {u : β → α} :
cauchy_seq u ↔ ∀ ε : nnreal, 0 < ε → ∃ N, ∀ n, N ≤ n → edist (u n) (u N) < ε :=
uniformity_basis_edist_nnreal.cauchy_seq_iff'
theorem totally_bounded_iff {s : set α} :
totally_bounded s ↔ ∀ ε > 0, ∃t : set α, finite t ∧ s ⊆ ⋃y∈t, ball y ε :=
⟨λ H ε ε0, H _ (edist_mem_uniformity ε0),
λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru,
⟨t, ft, h⟩ := H ε ε0 in
⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩
theorem totally_bounded_iff' {s : set α} :
totally_bounded s ↔ ∀ ε > 0, ∃t⊆s, finite t ∧ s ⊆ ⋃y∈t, ball y ε :=
⟨λ H ε ε0, (totally_bounded_iff_subset.1 H) _ (edist_mem_uniformity ε0),
λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru,
⟨t, _, ft, h⟩ := H ε ε0 in
⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩
section compact
/-- A compact set in an emetric space is separable, i.e., it is the closure of a countable set -/
lemma countable_closure_of_compact {α : Type u} [emetric_space α] {s : set α} (hs : compact s) :
∃ t ⊆ s, (countable t ∧ s = closure t) :=
begin
have A : ∀ (e:ennreal), e > 0 → ∃ t ⊆ s, (finite t ∧ s ⊆ (⋃x∈t, ball x e)) :=
totally_bounded_iff'.1 (compact_iff_totally_bounded_complete.1 hs).1,
-- assume e, finite_cover_balls_of_compact hs,
have B : ∀ (e:ennreal), ∃ t ⊆ s, finite t ∧ (e > 0 → s ⊆ (⋃x∈t, ball x e)),
{ intro e,
cases le_or_gt e 0 with h,
{ exact ⟨∅, by finish⟩ },
{ rcases A e h with ⟨s, ⟨finite_s, closure_s⟩⟩, existsi s, finish }},
/-The desired countable set is obtained by taking for each `n` the centers of a finite cover
by balls of radius `1/n`, and then the union over `n`. -/
choose T T_in_s finite_T using B,
let t := ⋃n:ℕ, T n⁻¹,
have T₁ : t ⊆ s := begin apply Union_subset, assume n, apply T_in_s end,
have T₂ : countable t := by finish [countable_Union, finite.countable],
have T₃ : s ⊆ closure t,
{ intros x x_in_s,
apply mem_closure_iff.2,
intros ε εpos,
rcases ennreal.exists_inv_nat_lt (bot_lt_iff_ne_bot.1 εpos) with ⟨n, hn⟩,
have inv_n_pos : (0 : ennreal) < (n : ℕ)⁻¹ := by simp [ennreal.bot_lt_iff_ne_bot],
have C : x ∈ (⋃y∈ T (n : ℕ)⁻¹, ball y (n : ℕ)⁻¹) :=
mem_of_mem_of_subset x_in_s ((finite_T (n : ℕ)⁻¹).2 inv_n_pos),
rcases mem_Union.1 C with ⟨y, _, ⟨y_in_T, rfl⟩, Dxy⟩,
simp at Dxy, -- Dxy : edist x y < 1 / ↑n
have : y ∈ t := mem_of_mem_of_subset y_in_T (by apply subset_Union (λ (n:ℕ), T (n : ℕ)⁻¹)),
have : edist x y < ε := lt_trans Dxy hn,
exact ⟨y, ‹y ∈ t›, ‹edist x y < ε›⟩ },
have T₄ : closure t ⊆ s := calc
closure t ⊆ closure s : closure_mono T₁
... = s : closure_eq_of_is_closed (closed_of_compact _ hs),
exact ⟨t, ⟨T₁, T₂, subset.antisymm T₃ T₄⟩⟩
end
end compact
section first_countable
@[priority 100] -- see Note [lower instance priority]
instance (α : Type u) [emetric_space α] :
topological_space.first_countable_topology α :=
uniform_space.first_countable_topology uniformity_has_countable_basis
end first_countable
section second_countable
open topological_space
/-- A separable emetric space is second countable: one obtains a countable basis by taking
the balls centered at points in a dense subset, and with rational radii. We do not register
this as an instance, as there is already an instance going in the other direction
from second countable spaces to separable spaces, and we want to avoid loops. -/
lemma second_countable_of_separable (α : Type u) [emetric_space α] [separable_space α] :
second_countable_topology α :=
let ⟨S, ⟨S_countable, S_dense⟩⟩ := separable_space.exists_countable_closure_eq_univ in
⟨⟨⋃x ∈ S, ⋃ (n : nat), {ball x (n⁻¹)},
⟨show countable ⋃x ∈ S, ⋃ (n : nat), {ball x (n⁻¹)},
{ apply S_countable.bUnion,
intros a aS,
apply countable_Union,
simp },
show uniform_space.to_topological_space = generate_from (⋃x ∈ S, ⋃ (n : nat), {ball x (n⁻¹)}),
{ have A : ∀ (u : set α), (u ∈ ⋃x ∈ S, ⋃ (n : nat), ({ball x ((n : ennreal)⁻¹)} : set (set α))) → is_open u,
{ simp only [and_imp, exists_prop, set.mem_Union, set.mem_singleton_iff, exists_imp_distrib],
intros u x hx i u_ball,
rw [u_ball],
exact is_open_ball },
have B : is_topological_basis (⋃x ∈ S, ⋃ (n : nat), ({ball x (n⁻¹)} : set (set α))),
{ refine is_topological_basis_of_open_of_nhds A (λa u au open_u, _),
rcases is_open_iff.1 open_u a au with ⟨ε, εpos, εball⟩,
have : ε / 2 > 0 := ennreal.half_pos εpos,
/- The ball `ball a ε` is included in `u`. We need to find one of our balls `ball x (n⁻¹)`
containing `a` and contained in `ball a ε`. For this, we take `n` larger than `2/ε`, and
then `x` in `S` at distance at most `n⁻¹` of `a` -/
rcases ennreal.exists_inv_nat_lt (bot_lt_iff_ne_bot.1 (ennreal.half_pos εpos)) with ⟨n, εn⟩,
have : (0 : ennreal) < n⁻¹ := by simp [ennreal.bot_lt_iff_ne_bot],
have : (a : α) ∈ closure (S : set α) := by rw [S_dense]; simp,
rcases mem_closure_iff.1 this _ ‹(0 : ennreal) < n⁻¹› with ⟨x, xS, xdist⟩,
existsi ball x (↑n)⁻¹,
have I : ball x (n⁻¹) ⊆ ball a ε := λy ydist, calc
edist y a = edist a y : edist_comm _ _
... ≤ edist a x + edist y x : edist_triangle_right _ _ _
... < n⁻¹ + n⁻¹ : ennreal.add_lt_add xdist ydist
... < ε/2 + ε/2 : ennreal.add_lt_add εn εn
... = ε : ennreal.add_halves _,
simp only [emetric.mem_ball, exists_prop, set.mem_Union, set.mem_singleton_iff],
exact ⟨⟨x, ⟨xS, ⟨n, rfl⟩⟩⟩, ⟨by simpa, subset.trans I εball⟩⟩ },
exact B.2.2 }⟩⟩⟩
end second_countable
section diam
/-- The diameter of a set in an emetric space, named `emetric.diam` -/
def diam (s : set α) := ⨆ (x ∈ s) (y ∈ s), edist x y
lemma diam_le_iff_forall_edist_le {d : ennreal} :
diam s ≤ d ↔ ∀ (x ∈ s) (y ∈ s), edist x y ≤ d :=
by simp only [diam, supr_le_iff]
/-- If two points belong to some set, their edistance is bounded by the diameter of the set -/
lemma edist_le_diam_of_mem (hx : x ∈ s) (hy : y ∈ s) : edist x y ≤ diam s :=
diam_le_iff_forall_edist_le.1 (le_refl _) x hx y hy
/-- If the distance between any two points in a set is bounded by some constant, this constant
bounds the diameter. -/
lemma diam_le_of_forall_edist_le {d : ennreal} (h : ∀ (x ∈ s) (y ∈ s), edist x y ≤ d) :
diam s ≤ d :=
diam_le_iff_forall_edist_le.2 h
/-- The diameter of a subsingleton vanishes. -/
lemma diam_subsingleton (hs : s.subsingleton) : diam s = 0 :=
le_zero_iff_eq.1 $ diam_le_of_forall_edist_le $
λ x hx y hy, (hs hx hy).symm ▸ edist_self y ▸ le_refl _
/-- The diameter of the empty set vanishes -/
@[simp] lemma diam_empty : diam (∅ : set α) = 0 :=
diam_subsingleton subsingleton_empty
/-- The diameter of a singleton vanishes -/
@[simp] lemma diam_singleton : diam ({x} : set α) = 0 :=
diam_subsingleton subsingleton_singleton
lemma diam_eq_zero_iff : diam s = 0 ↔ s.subsingleton :=
⟨λ h x hx y hy, edist_le_zero.1 $ h ▸ edist_le_diam_of_mem hx hy, diam_subsingleton⟩
lemma diam_pos_iff : 0 < diam s ↔ ∃ (x ∈ s) (y ∈ s), x ≠ y :=
begin
have := not_congr (@diam_eq_zero_iff _ _ s),
dunfold set.subsingleton at this,
push_neg at this,
simpa only [zero_lt_iff_ne_zero, exists_prop] using this
end
lemma diam_insert : diam (insert x s) = max (diam s) (⨆ y ∈ s, edist y x) :=
eq_of_forall_ge_iff $ λ d, by simp only [diam_le_iff_forall_edist_le, ball_insert_iff, max_le_iff,
edist_self, zero_le, true_and, supr_le_iff, forall_and_distrib, edist_comm x, and_self,
(and_assoc _ _).symm, max_comm (diam s)]
lemma diam_pair : diam ({x, y} : set α) = edist x y :=
by simp only [supr_singleton, diam_insert, diam_singleton, ennreal.max_zero_left]
lemma diam_triple :
diam ({x, y, z} : set α) = max (edist x y) (max (edist y z) (edist x z)) :=
by simp only [diam_insert, supr_insert, supr_singleton, diam_singleton,
ennreal.max_zero_left, ennreal.sup_eq_max]
/-- The diameter is monotonous with respect to inclusion -/
lemma diam_mono {s t : set α} (h : s ⊆ t) : diam s ≤ diam t :=
diam_le_of_forall_edist_le $ λ x hx y hy, edist_le_diam_of_mem (h hx) (h hy)
/-- The diameter of a union is controlled by the diameter of the sets, and the edistance
between two points in the sets. -/
lemma diam_union {t : set α} (xs : x ∈ s) (yt : y ∈ t) : diam (s ∪ t) ≤ diam s + edist x y + diam t :=
begin
have A : ∀a ∈ s, ∀b ∈ t, edist a b ≤ diam s + edist x y + diam t := λa ha b hb, calc
edist a b ≤ edist a x + edist x y + edist y b : edist_triangle4 _ _ _ _
... ≤ diam s + edist x y + diam t :
add_le_add' (add_le_add' (edist_le_diam_of_mem ha xs) (le_refl _)) (edist_le_diam_of_mem yt hb),
refine diam_le_of_forall_edist_le (λa ha b hb, _),
cases (mem_union _ _ _).1 ha with h'a h'a; cases (mem_union _ _ _).1 hb with h'b h'b,
{ calc edist a b ≤ diam s : edist_le_diam_of_mem h'a h'b
... ≤ diam s + (edist x y + diam t) : le_add_right (le_refl _)
... = diam s + edist x y + diam t : by simp only [add_comm, eq_self_iff_true, add_left_comm] },
{ exact A a h'a b h'b },
{ have Z := A b h'b a h'a, rwa [edist_comm] at Z },
{ calc edist a b ≤ diam t : edist_le_diam_of_mem h'a h'b
... ≤ (diam s + edist x y) + diam t : le_add_left (le_refl _) }
end
lemma diam_union' {t : set α} (h : (s ∩ t).nonempty) : diam (s ∪ t) ≤ diam s + diam t :=
let ⟨x, ⟨xs, xt⟩⟩ := h in by simpa using diam_union xs xt
lemma diam_closed_ball {r : ennreal} : diam (closed_ball x r) ≤ 2 * r :=
diam_le_of_forall_edist_le $ λa ha b hb, calc
edist a b ≤ edist a x + edist b x : edist_triangle_right _ _ _
... ≤ r + r : add_le_add' ha hb
... = 2 * r : by simp [mul_two, mul_comm]
lemma diam_ball {r : ennreal} : diam (ball x r) ≤ 2 * r :=
le_trans (diam_mono ball_subset_closed_ball) diam_closed_ball
end diam
end emetric --namespace
|
acc20f9f948a40a0a0d73b5ee64ce555a26b856f | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/analysis/special_functions/trigonometric/complex.lean | 187a81d959bf98f599a46f490ce05e621abf3b34 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,026 | 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, Benjamin Davidson
-/
import algebra.quadratic_discriminant
import analysis.complex.polynomial
import field_theory.is_alg_closed.basic
import analysis.special_functions.trigonometric.deriv
/-!
# Complex trigonometric functions
Basic facts and derivatives for the complex trigonometric functions.
-/
noncomputable theory
namespace complex
open set filter
open_locale real
theorem cos_eq_zero_iff {θ : ℂ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 :=
begin
have h : (exp (θ * I) + exp (-θ * I)) / 2 = 0 ↔ exp (2 * θ * I) = -1,
{ rw [@div_eq_iff _ _ (exp (θ * I) + exp (-θ * I)) 2 0 two_ne_zero', zero_mul,
add_eq_zero_iff_eq_neg, neg_eq_neg_one_mul, ← div_eq_iff (exp_ne_zero _), ← exp_sub],
field_simp only, congr' 3, ring },
rw [cos, h, ← exp_pi_mul_I, exp_eq_exp_iff_exists_int, mul_right_comm],
refine exists_congr (λ x, _),
refine (iff_of_eq $ congr_arg _ _).trans (mul_right_inj' $ mul_ne_zero two_ne_zero' I_ne_zero),
ring,
end
theorem cos_ne_zero_iff {θ : ℂ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 :=
by rw [← not_exists, not_iff_not, cos_eq_zero_iff]
theorem sin_eq_zero_iff {θ : ℂ} : sin θ = 0 ↔ ∃ k : ℤ, θ = k * π :=
begin
rw [← complex.cos_sub_pi_div_two, cos_eq_zero_iff],
split,
{ rintros ⟨k, hk⟩,
use k + 1,
field_simp [eq_add_of_sub_eq hk],
ring },
{ rintros ⟨k, rfl⟩,
use k - 1,
field_simp,
ring }
end
theorem sin_ne_zero_iff {θ : ℂ} : sin θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π :=
by rw [← not_exists, not_iff_not, sin_eq_zero_iff]
lemma tan_eq_zero_iff {θ : ℂ} : tan θ = 0 ↔ ∃ k : ℤ, θ = k * π / 2 :=
begin
have h := (sin_two_mul θ).symm,
rw mul_assoc at h,
rw [tan, div_eq_zero_iff, ← mul_eq_zero, ← zero_mul ((1/2):ℂ), mul_one_div,
cancel_factors.cancel_factors_eq_div h two_ne_zero', mul_comm],
simpa only [zero_div, zero_mul, ne.def, not_false_iff] with field_simps using sin_eq_zero_iff,
end
lemma tan_ne_zero_iff {θ : ℂ} : tan θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π / 2 :=
by rw [← not_exists, not_iff_not, tan_eq_zero_iff]
lemma tan_int_mul_pi_div_two (n : ℤ) : tan (n * π/2) = 0 :=
tan_eq_zero_iff.mpr (by use n)
lemma cos_eq_cos_iff {x y : ℂ} :
cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x :=
calc cos x = cos y ↔ cos x - cos y = 0 : sub_eq_zero.symm
... ↔ -2 * sin((x + y)/2) * sin((x - y)/2) = 0 : by rw cos_sub_cos
... ↔ sin((x + y)/2) = 0 ∨ sin((x - y)/2) = 0 : by simp [(by norm_num : (2:ℂ) ≠ 0)]
... ↔ sin((x - y)/2) = 0 ∨ sin((x + y)/2) = 0 : or.comm
... ↔ (∃ k : ℤ, y = 2 * k * π + x) ∨ (∃ k :ℤ, y = 2 * k * π - x) :
begin
apply or_congr;
field_simp [sin_eq_zero_iff, (by norm_num : -(2:ℂ) ≠ 0), eq_sub_iff_add_eq',
sub_eq_iff_eq_add, mul_comm (2:ℂ), mul_right_comm _ (2:ℂ)],
split; { rintros ⟨k, rfl⟩, use -k, simp, },
end
... ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x : exists_or_distrib.symm
lemma sin_eq_sin_iff {x y : ℂ} :
sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x :=
begin
simp only [← complex.cos_sub_pi_div_two, cos_eq_cos_iff, sub_eq_iff_eq_add],
refine exists_congr (λ k, or_congr _ _); refine eq.congr rfl _; field_simp; ring
end
lemma tan_add {x y : ℂ}
(h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)
∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y = (2 * l + 1) * π / 2)) :
tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) :=
begin
rcases h with ⟨h1, h2⟩ | ⟨⟨k, rfl⟩, ⟨l, rfl⟩⟩,
{ rw [tan, sin_add, cos_add,
← div_div_div_cancel_right (sin x * cos y + cos x * sin y)
(mul_ne_zero (cos_ne_zero_iff.mpr h1) (cos_ne_zero_iff.mpr h2)),
add_div, sub_div],
simp only [←div_mul_div, ←tan, mul_one, one_mul,
div_self (cos_ne_zero_iff.mpr h1), div_self (cos_ne_zero_iff.mpr h2)] },
{ obtain ⟨t, hx, hy, hxy⟩ := ⟨tan_int_mul_pi_div_two, t (2*k+1), t (2*l+1), t (2*k+1+(2*l+1))⟩,
simp only [int.cast_add, int.cast_bit0, int.cast_mul, int.cast_one, hx, hy] at hx hy hxy,
rw [hx, hy, add_zero, zero_div,
mul_div_assoc, mul_div_assoc, ← add_mul (2*(k:ℂ)+1) (2*l+1) (π/2), ← mul_div_assoc, hxy] },
end
lemma tan_add' {x y : ℂ}
(h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)) :
tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) :=
tan_add (or.inl h)
lemma tan_two_mul {z : ℂ} : tan (2 * z) = 2 * tan z / (1 - tan z ^ 2) :=
begin
by_cases h : ∀ k : ℤ, z ≠ (2 * k + 1) * π / 2,
{ rw [two_mul, two_mul, sq, tan_add (or.inl ⟨h, h⟩)] },
{ rw not_forall_not at h,
rw [two_mul, two_mul, sq, tan_add (or.inr ⟨h, h⟩)] },
end
lemma tan_add_mul_I {x y : ℂ}
(h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y * I ≠ (2 * l + 1) * π / 2)
∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y * I = (2 * l + 1) * π / 2)) :
tan (x + y*I) = (tan x + tanh y * I) / (1 - tan x * tanh y * I) :=
by rw [tan_add h, tan_mul_I, mul_assoc]
lemma tan_eq {z : ℂ}
(h : ((∀ k : ℤ, (z.re:ℂ) ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, (z.im:ℂ) * I ≠ (2 * l + 1) * π / 2)
∨ ((∃ k : ℤ, (z.re:ℂ) = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, (z.im:ℂ) * I = (2 * l + 1) * π / 2)) :
tan z = (tan z.re + tanh z.im * I) / (1 - tan z.re * tanh z.im * I) :=
by convert tan_add_mul_I h; exact (re_add_im z).symm
lemma has_strict_deriv_at_tan {x : ℂ} (h : cos x ≠ 0) :
has_strict_deriv_at tan (1 / (cos x)^2) x :=
begin
convert (has_strict_deriv_at_sin x).div (has_strict_deriv_at_cos x) h,
rw ← sin_sq_add_cos_sq x,
ring,
end
lemma has_deriv_at_tan {x : ℂ} (h : cos x ≠ 0) :
has_deriv_at tan (1 / (cos x)^2) x :=
(has_strict_deriv_at_tan h).has_deriv_at
open_locale topological_space
lemma tendsto_abs_tan_of_cos_eq_zero {x : ℂ} (hx : cos x = 0) :
tendsto (λ x, abs (tan x)) (𝓝[{x}ᶜ] x) at_top :=
begin
simp only [tan_eq_sin_div_cos, ← norm_eq_abs, normed_field.norm_div],
have A : sin x ≠ 0 := λ h, by simpa [*, sq] using sin_sq_add_cos_sq x,
have B : tendsto cos (𝓝[{x}ᶜ] (x)) (𝓝[{0}ᶜ] 0),
{ refine tendsto_inf.2 ⟨tendsto.mono_left _ inf_le_left, tendsto_principal.2 _⟩,
exacts [continuous_cos.tendsto' x 0 hx,
hx ▸ (has_deriv_at_cos _).eventually_ne (neg_ne_zero.2 A)] },
exact continuous_sin.continuous_within_at.norm.mul_at_top (norm_pos_iff.2 A)
(tendsto_norm_nhds_within_zero.comp B).inv_tendsto_zero,
end
lemma tendsto_abs_tan_at_top (k : ℤ) :
tendsto (λ x, abs (tan x)) (𝓝[{(2 * k + 1) * π / 2}ᶜ] ((2 * k + 1) * π / 2)) at_top :=
tendsto_abs_tan_of_cos_eq_zero $ cos_eq_zero_iff.2 ⟨k, rfl⟩
@[simp] lemma continuous_at_tan {x : ℂ} : continuous_at tan x ↔ cos x ≠ 0 :=
begin
refine ⟨λ hc h₀, _, λ h, (has_deriv_at_tan h).continuous_at⟩,
exact not_tendsto_nhds_of_tendsto_at_top (tendsto_abs_tan_of_cos_eq_zero h₀) _
(hc.norm.tendsto.mono_left inf_le_left)
end
@[simp] lemma differentiable_at_tan {x : ℂ} : differentiable_at ℂ tan x ↔ cos x ≠ 0 :=
⟨λ h, continuous_at_tan.1 h.continuous_at, λ h, (has_deriv_at_tan h).differentiable_at⟩
@[simp] lemma deriv_tan (x : ℂ) : deriv tan x = 1 / (cos x)^2 :=
if h : cos x = 0 then
have ¬differentiable_at ℂ tan x := mt differentiable_at_tan.1 (not_not.2 h),
by simp [deriv_zero_of_not_differentiable_at this, h, sq]
else (has_deriv_at_tan h).deriv
lemma continuous_on_tan : continuous_on tan {x | cos x ≠ 0} :=
continuous_on_sin.div continuous_on_cos $ λ x, id
@[continuity]
lemma continuous_tan : continuous (λ x : {x | cos x ≠ 0}, tan x) :=
continuous_on_iff_continuous_restrict.1 continuous_on_tan
@[simp] lemma times_cont_diff_at_tan {x : ℂ} {n : with_top ℕ} :
times_cont_diff_at ℂ n tan x ↔ cos x ≠ 0 :=
⟨λ h, continuous_at_tan.1 h.continuous_at,
times_cont_diff_sin.times_cont_diff_at.div times_cont_diff_cos.times_cont_diff_at⟩
lemma cos_eq_iff_quadratic {z w : ℂ} :
cos z = w ↔ (exp (z * I)) ^ 2 - 2 * w * exp (z * I) + 1 = 0 :=
begin
rw ← sub_eq_zero,
field_simp [cos, exp_neg, exp_ne_zero],
refine eq.congr _ rfl,
ring
end
lemma cos_surjective : function.surjective cos :=
begin
intro x,
obtain ⟨w, w₀, hw⟩ : ∃ w ≠ 0, 1 * w * w + (-2 * x) * w + 1 = 0,
{ rcases exists_quadratic_eq_zero (@one_ne_zero ℂ _ _) (is_alg_closed.exists_eq_mul_self _)
with ⟨w, hw⟩,
refine ⟨w, _, hw⟩,
rintro rfl,
simpa only [zero_add, one_ne_zero, mul_zero] using hw },
refine ⟨log w / I, cos_eq_iff_quadratic.2 _⟩,
rw [div_mul_cancel _ I_ne_zero, exp_log w₀],
convert hw,
ring
end
@[simp] lemma range_cos : range cos = set.univ :=
cos_surjective.range_eq
lemma sin_surjective : function.surjective sin :=
begin
intro x,
rcases cos_surjective x with ⟨z, rfl⟩,
exact ⟨z + π / 2, sin_add_pi_div_two z⟩
end
@[simp] lemma range_sin : range sin = set.univ :=
sin_surjective.range_eq
end complex
namespace real
open_locale real
theorem cos_eq_zero_iff {θ : ℝ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 :=
by exact_mod_cast @complex.cos_eq_zero_iff θ
theorem cos_ne_zero_iff {θ : ℝ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 :=
by rw [← not_exists, not_iff_not, cos_eq_zero_iff]
lemma cos_eq_cos_iff {x y : ℝ} :
cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x :=
by exact_mod_cast @complex.cos_eq_cos_iff x y
lemma sin_eq_sin_iff {x y : ℝ} :
sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x :=
by exact_mod_cast @complex.sin_eq_sin_iff x y
end real
|
b9c1fb4968ed50aa1f76220d8a1ec7dab846f4bb | dd4e652c749fea9ac77e404005cb3470e5f75469 | /src/alex_playground/equiv.lean | 81dc3ba6f4368933d0a58f36dfdfa17fa388b139 | [] | 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 | 1,273 | lean | namespace optimization
structure var :=
(name : string)
(type : Type)
structure named_prod (name : string) (α : Type) (β : Type) :=
(fst : α)
(rest : β)
def domain : list var → Type
| [] := unit
| (v :: vs) := named_prod v.name v.type (domain vs)
structure constraint (vars : list var) :=
(name : string)
(predicate : domain vars → Prop)
structure problem :=
(vars : list var)
(obj_type : Type)
(obj_fun : domain vars → obj_type)
(constrs : list (constraint vars))
def problem.domain (p : problem) := domain p.vars
structure problem.solution (p : problem) [has_le p.obj_type] :=
(point : p.domain)
(feasability : ∀ c ∈ p.constrs, constraint.predicate c point)
(optimality : ∀ x, p.obj_fun x ≤ p.obj_fun x)
structure equiv (p q : problem) [has_le p.obj_type] [has_le q.obj_type] :=
(to_fun : p.solution → q.solution)
(inv_fun : q.solution → p.solution)
infix `≃`:10 := equiv
def my_equiv (p : problem) (c : string) (v : string)
(f : constraint p.vars)
(hf : f.predicate = (λ p, p.get v = 0))
(h : f ∈ p.constrs) :
sorry := sorry
--p ≃ p.remove_constr (p.subst_var v 0) `$v = 0`
/-
def my_equiv (p : problem) (v : var) (h : `$v = 0` ∈ constr p₁)) :
p ≃ p.remove_constr (p.subst_var v 0) `$v = 0`
-/
end optimization
|
2a69a90a6a4cd614080ff1f4d5e9d596c0751cb2 | 0707842a58b971bc2c537fdcab2f5cef1c12d77a | /lean4/03_complex_hello_world.lean | f2df388558f1b339ccfab3276c34a694d2a11581 | [] | no_license | adolfont/LearningProgramming | 7a41e5dfde8df72fc0b4a23592999ecdb22a26e0 | 866e6654d347287bd0c63aa31d18705174f00324 | refs/heads/master | 1,652,902,832,503 | 1,651,315,660,000 | 1,651,315,660,000 | 1,328,601 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 605 | lean | -- Source: https://leanprover.github.io/lean4/doc/whatIsLean.html
-- Defines a function that takes a name and produces a greeting.
def getGreeting (name : String) := s!"Hello, {name}! Isn't Lean great?"
-- The `main` function is the entry point of your program.
-- Its type is `IO Unit` because it can perform `IO` operations (side effects).
def main : IO Unit :=
-- Define a list of names
let names := ["Sebastian", "Leo", "Daniel"]
-- Map each name to a greeting
let greetings := names.map getGreeting
-- Print the list of greetings
for greeting in greetings do
IO.println greeting
|
5f627e435cb7256c27ef1ba91de00af1713593c1 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/linear_algebra/char_poly.lean | 85e8beae63df29568c55c44fa0130491b0c04c53 | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 3,798 | 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 tactic.apply_fun
import ring_theory.matrix_algebra
import ring_theory.polynomial_algebra
import linear_algebra.nonsingular_inverse
import tactic.squeeze
/-!
# Characteristic polynomials and the Cayley-Hamilton theorem
We define characteristic polynomials of matrices and
prove the Cayley–Hamilton theorem over arbitrary commutative rings.
## Main definitions
* `char_poly` is the characteristic polynomial of a matrix.
## Implementation details
We follow a nice proof from http://drorbn.net/AcademicPensieve/2015-12/CayleyHamilton.pdf
-/
noncomputable theory
universes u v w
open polynomial matrix
open_locale big_operators
variables {R : Type u} [comm_ring R]
variables {n : Type w} [decidable_eq n] [fintype n]
open finset
/--
The "characteristic matrix" of `M : matrix n n R` is the matrix of polynomials $t I - M$.
The determinant of this matrix is the characteristic polynomial.
-/
def char_matrix (M : matrix n n R) : matrix n n (polynomial R) :=
matrix.scalar n (X : polynomial R) - (C : R →+* polynomial R).map_matrix M
@[simp] lemma char_matrix_apply_eq (M : matrix n n R) (i : n) :
char_matrix M i i = (X : polynomial R) - C (M i i) :=
by simp only [char_matrix, sub_left_inj, pi.sub_apply, scalar_apply_eq,
ring_hom.map_matrix_apply, map_apply]
@[simp] lemma char_matrix_apply_ne (M : matrix n n R) (i j : n) (h : i ≠ j) :
char_matrix M i j = - C (M i j) :=
by simp only [char_matrix, pi.sub_apply, scalar_apply_ne _ _ _ h, zero_sub,
ring_hom.map_matrix_apply, map_apply]
lemma mat_poly_equiv_char_matrix (M : matrix n n R) :
mat_poly_equiv (char_matrix M) = X - C M :=
begin
ext k i j,
simp only [mat_poly_equiv_coeff_apply, coeff_sub, pi.sub_apply],
by_cases h : i = j,
{ subst h, rw [char_matrix_apply_eq, coeff_sub],
simp only [coeff_X, coeff_C],
split_ifs; simp, },
{ rw [char_matrix_apply_ne _ _ _ h, coeff_X, coeff_neg, coeff_C, coeff_C],
split_ifs; simp [h], }
end
/--
The characteristic polynomial of a matrix `M` is given by $\det (t I - M)$.
-/
def char_poly (M : matrix n n R) : polynomial R :=
(char_matrix M).det
/--
The Cayley-Hamilton theorem, that the characteristic polynomial of a matrix,
applied to the matrix itself, is zero.
This holds over any commutative ring.
-/
-- This proof follows http://drorbn.net/AcademicPensieve/2015-12/CayleyHamilton.pdf
theorem char_poly_map_eval_self (M : matrix n n R) :
((char_poly M).map (algebra_map R (matrix n n R))).eval M = 0 :=
begin
-- We begin with the fact $χ_M(t) I = adjugate (t I - M) * (t I - M)$,
-- as an identity in `matrix n n (polynomial R)`.
have h : (char_poly M) • (1 : matrix n n (polynomial R)) =
adjugate (char_matrix M) * (char_matrix M) :=
(adjugate_mul _).symm,
-- Using the algebra isomorphism `matrix n n (polynomial R) ≃ₐ[R] polynomial (matrix n n R)`,
-- we have the same identity in `polynomial (matrix n n R)`.
apply_fun mat_poly_equiv at h,
simp only [mat_poly_equiv.map_mul,
mat_poly_equiv_char_matrix] at h,
-- Because the coefficient ring `matrix n n R` is non-commutative,
-- evaluation at `M` is not multiplicative.
-- However, any polynomial which is a product of the form $N * (t I - M)$
-- is sent to zero, because the evaluation function puts the polynomial variable
-- to the right of any coefficients, so everything telescopes.
apply_fun (λ p, p.eval M) at h,
rw eval_mul_X_sub_C at h,
-- Now $χ_M (t) I$, when thought of as a polynomial of matrices
-- and evaluated at some `N` is exactly $χ_M (N)$.
rw mat_poly_equiv_smul_one at h,
-- Thus we have $χ_M(M) = 0$, which is the desired result.
exact h,
end
|
d4bdbf6ecb505127c49ca7eb1352d6de2f820c3f | 367134ba5a65885e863bdc4507601606690974c1 | /src/linear_algebra/affine_space/ordered.lean | f4c79f7f8c8edbea2a13d566f24c3e7538043aac | [
"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 | 15,435 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury G. Kudryashov
-/
import linear_algebra.affine_space.midpoint
import algebra.module.ordered
import tactic.field_simp
/-!
# Ordered modules as affine spaces
In this file we define the slope of a function `f : k → PE` taking values in an affine space over
`k` and prove some theorems about `slope` and `line_map` in the case when `PE` is an ordered
semimodule over `k`. The `slope` function naturally appears in the Mean Value Theorem, and in the
proof of the fact that a function with nonnegative second derivative on an interval is convex on
this interval. In the third part of this file we prove inequalities that will be used in
`analysis.convex.basic` to link convexity of a function on an interval to monotonicity of the slope,
see section docstring below for details.
## Implementation notes
We do not introduce the notion of ordered affine spaces (yet?). Instead, we prove various theorems
for an ordered semimodule interpreted as an affine space.
## Tags
affine space, ordered semimodule, slope
-/
open affine_map
variables {k E PE : Type*}
/-!
### Definition of `slope` and basic properties
In this section we define `slope f a b` and prove some properties that do not require order on the
codomain. -/
section no_order
variables [field k] [add_comm_group E] [semimodule k E] [add_torsor E PE]
include E
/-- `slope f a b = (b - a)⁻¹ • (f b -ᵥ f a)` is the slope of a function `f` on the interval
`[a, b]`. Note that `slope f a a = 0`, not the derivative of `f` at `a`. -/
def slope (f : k → PE) (a b : k) : E := (b - a)⁻¹ • (f b -ᵥ f a)
omit E
lemma slope_def_field (f : k → k) (a b : k) : slope f a b = (f b - f a) / (b - a) :=
div_eq_inv_mul.symm
@[simp] lemma slope_same (f : k → PE) (a : k) : (slope f a a : E) = 0 :=
by rw [slope, sub_self, inv_zero, zero_smul]
include E
lemma eq_of_slope_eq_zero {f : k → PE} {a b : k} (h : slope f a b = (0:E)) : f a = f b :=
begin
rw [slope, smul_eq_zero, inv_eq_zero, sub_eq_zero, vsub_eq_zero_iff_eq] at h,
exact h.elim (λ h, h ▸ rfl) eq.symm
end
lemma slope_comm (f : k → PE) (a b : k) : slope f a b = slope f b a :=
by rw [slope, slope, ← neg_vsub_eq_vsub_rev, smul_neg, ← neg_smul, neg_inv, neg_sub]
/-- `slope f a c` is a linear combination of `slope f a b` and `slope f b c`. This version
explicitly provides coefficients. If `a ≠ c`, then the sum of the coefficients is `1`, so it is
actually an affine combination, see `line_map_slope_slope_sub_div_sub`. -/
lemma sub_div_sub_smul_slope_add_sub_div_sub_smul_slope (f : k → PE) (a b c : k) :
((b - a) / (c - a)) • slope f a b + ((c - b) / (c - a)) • slope f b c = slope f a c :=
begin
by_cases hab : a = b,
{ subst hab,
rw [sub_self, zero_div, zero_smul, zero_add],
by_cases hac : a = c,
{ simp [hac] },
{ rw [div_self (sub_ne_zero.2 $ ne.symm hac), one_smul] } },
by_cases hbc : b = c, { subst hbc, simp [sub_ne_zero.2 (ne.symm hab)] },
rw [add_comm],
simp_rw [slope, div_eq_inv_mul, mul_smul, ← smul_add,
smul_inv_smul' (sub_ne_zero.2 $ ne.symm hab), smul_inv_smul' (sub_ne_zero.2 $ ne.symm hbc),
vsub_add_vsub_cancel],
end
/-- `slope f a c` is an affine combination of `slope f a b` and `slope f b c`. This version uses
`line_map` to express this property. -/
lemma line_map_slope_slope_sub_div_sub (f : k → PE) (a b c : k) (h : a ≠ c) :
line_map (slope f a b) (slope f b c) ((c - b) / (c - a)) = slope f a c :=
by field_simp [sub_ne_zero.2 h.symm, ← sub_div_sub_smul_slope_add_sub_div_sub_smul_slope f a b c,
line_map_apply_module]
/-- `slope f a b` is an affine combination of `slope f a (line_map a b r)` and
`slope f (line_map a b r) b`. We use `line_map` to express this property. -/
lemma line_map_slope_line_map_slope_line_map (f : k → PE) (a b r : k) :
line_map (slope f (line_map a b r) b) (slope f a (line_map a b r)) r = slope f a b :=
begin
obtain (rfl|hab) : a = b ∨ a ≠ b := classical.em _, { simp },
rw [slope_comm _ a, slope_comm _ a, slope_comm _ _ b],
convert line_map_slope_slope_sub_div_sub f b (line_map a b r) a hab.symm using 2,
rw [line_map_apply_ring, eq_div_iff (sub_ne_zero.2 hab), sub_mul, one_mul, mul_sub, ← sub_sub,
sub_sub_cancel]
end
end no_order
/-!
### Monotonicity of `line_map`
In this section we prove that `line_map a b r` is monotone (strictly or not) in its arguments if
other arguments belong to specific domains.
-/
section ordered_ring
variables [ordered_ring k] [ordered_add_comm_group E] [semimodule k E] [ordered_semimodule k E]
variables {a a' b b' : E} {r r' : k}
lemma line_map_mono_left (ha : a ≤ a') (hr : r ≤ 1) :
line_map a b r ≤ line_map a' b r :=
begin
simp only [line_map_apply_module],
exact add_le_add_right (smul_le_smul_of_nonneg ha (sub_nonneg.2 hr)) _
end
lemma line_map_strict_mono_left (ha : a < a') (hr : r < 1) :
line_map a b r < line_map a' b r :=
begin
simp only [line_map_apply_module],
exact add_lt_add_right (smul_lt_smul_of_pos ha (sub_pos.2 hr)) _
end
lemma line_map_mono_right (hb : b ≤ b') (hr : 0 ≤ r) :
line_map a b r ≤ line_map a b' r :=
begin
simp only [line_map_apply_module],
exact add_le_add_left (smul_le_smul_of_nonneg hb hr) _
end
lemma line_map_strict_mono_right (hb : b < b') (hr : 0 < r) :
line_map a b r < line_map a b' r :=
begin
simp only [line_map_apply_module],
exact add_lt_add_left (smul_lt_smul_of_pos hb hr) _
end
lemma line_map_mono_endpoints (ha : a ≤ a') (hb : b ≤ b') (h₀ : 0 ≤ r) (h₁ : r ≤ 1) :
line_map a b r ≤ line_map a' b' r :=
(line_map_mono_left ha h₁).trans (line_map_mono_right hb h₀)
lemma line_map_strict_mono_endpoints (ha : a < a') (hb : b < b') (h₀ : 0 ≤ r) (h₁ : r ≤ 1) :
line_map a b r < line_map a' b' r :=
begin
rcases h₀.eq_or_lt with (rfl|h₀), { simpa },
exact (line_map_mono_left ha.le h₁).trans_lt (line_map_strict_mono_right hb h₀)
end
lemma line_map_lt_line_map_iff_of_lt (h : r < r') :
line_map a b r < line_map a b r' ↔ a < b :=
begin
simp only [line_map_apply_module],
rw [← lt_sub_iff_add_lt, add_sub_assoc, ← sub_lt_iff_lt_add', ← sub_smul, ← sub_smul,
sub_sub_sub_cancel_left, smul_lt_smul_iff_of_pos (sub_pos.2 h)],
apply_instance,
end
lemma left_lt_line_map_iff_lt (h : 0 < r) : a < line_map a b r ↔ a < b :=
iff.trans (by rw line_map_apply_zero) (line_map_lt_line_map_iff_of_lt h)
lemma line_map_lt_left_iff_lt (h : 0 < r) : line_map a b r < a ↔ b < a :=
@left_lt_line_map_iff_lt k (order_dual E) _ _ _ _ _ _ _ h
lemma line_map_lt_right_iff_lt (h : r < 1) : line_map a b r < b ↔ a < b :=
iff.trans (by rw line_map_apply_one) (line_map_lt_line_map_iff_of_lt h)
lemma right_lt_line_map_iff_lt (h : r < 1) : b < line_map a b r ↔ b < a :=
@line_map_lt_right_iff_lt k (order_dual E) _ _ _ _ _ _ _ h
end ordered_ring
section linear_ordered_field
variables [linear_ordered_field k] [ordered_add_comm_group E]
variables [semimodule k E] [ordered_semimodule k E]
section
variables {a b : E} {r r' : k}
lemma line_map_le_line_map_iff_of_lt (h : r < r') :
line_map a b r ≤ line_map a b r' ↔ a ≤ b :=
begin
simp only [line_map_apply_module],
rw [← le_sub_iff_add_le, add_sub_assoc, ← sub_le_iff_le_add', ← sub_smul, ← sub_smul,
sub_sub_sub_cancel_left, smul_le_smul_iff_of_pos (sub_pos.2 h)],
apply_instance,
end
lemma left_le_line_map_iff_le (h : 0 < r) : a ≤ line_map a b r ↔ a ≤ b :=
iff.trans (by rw line_map_apply_zero) (line_map_le_line_map_iff_of_lt h)
@[simp] lemma left_le_midpoint : a ≤ midpoint k a b ↔ a ≤ b :=
left_le_line_map_iff_le $ inv_pos.2 zero_lt_two
lemma line_map_le_left_iff_le (h : 0 < r) : line_map a b r ≤ a ↔ b ≤ a :=
@left_le_line_map_iff_le k (order_dual E) _ _ _ _ _ _ _ h
@[simp] lemma midpoint_le_left : midpoint k a b ≤ a ↔ b ≤ a :=
line_map_le_left_iff_le $ inv_pos.2 zero_lt_two
lemma line_map_le_right_iff_le (h : r < 1) : line_map a b r ≤ b ↔ a ≤ b :=
iff.trans (by rw line_map_apply_one) (line_map_le_line_map_iff_of_lt h)
@[simp] lemma midpoint_le_right : midpoint k a b ≤ b ↔ a ≤ b :=
line_map_le_right_iff_le $ inv_lt_one one_lt_two
lemma right_le_line_map_iff_le (h : r < 1) : b ≤ line_map a b r ↔ b ≤ a :=
@line_map_le_right_iff_le k (order_dual E) _ _ _ _ _ _ _ h
@[simp] lemma right_le_midpoint : b ≤ midpoint k a b ↔ b ≤ a :=
right_le_line_map_iff_le $ inv_lt_one one_lt_two
end
/-!
### Convexity and slope
Given an interval `[a, b]` and a point `c ∈ (a, b)`, `c = line_map a b r`, there are a few ways to
say that the point `(c, f c)` is above/below the segment `[(a, f a), (b, f b)]`:
* compare `f c` to `line_map (f a) (f b) r`;
* compare `slope f a c` to `slope `f a b`;
* compare `slope f c b` to `slope f a b`;
* compare `slope f a c` to `slope f c b`.
In this section we prove equivalence of these four approaches. In order to make the statements more
readable, we introduce local notation `c = line_map a b r`. Then we prove lemmas like
```
lemma map_le_line_map_iff_slope_le_slope_left (h : 0 < r * (b - a)) :
f c ≤ line_map (f a) (f b) r ↔ slope f a c ≤ slope f a b :=
```
For each inequality between `f c` and `line_map (f a) (f b) r` we provide 3 lemmas:
* `*_left` relates it to an inequality on `slope f a c` and `slope f a b`;
* `*_right` relates it to an inequality on `slope f a b` and `slope f c b`;
* no-suffix version relates it to an inequality on `slope f a c` and `slope f c b`.
Later these inequalities will be used in to restate `convex_on` in terms of monotonicity of the
slope.
-/
variables {f : k → E} {a b r : k}
local notation `c` := line_map a b r
/-- Given `c = line_map a b r`, `a < c`, the point `(c, f c)` is non-strictly below the
segment `[(a, f a), (b, f b)]` if and only if `slope f a c ≤ slope f a b`. -/
lemma map_le_line_map_iff_slope_le_slope_left (h : 0 < r * (b - a)) :
f c ≤ line_map (f a) (f b) r ↔ slope f a c ≤ slope f a b :=
by simp_rw [line_map_apply, slope, vsub_eq_sub, vadd_eq_add, smul_eq_mul, add_sub_cancel, smul_sub,
sub_le_iff_le_add, mul_inv_rev', mul_smul, ← smul_sub, ← smul_add, smul_smul, ← mul_inv_rev',
smul_le_iff_of_pos (inv_pos.2 h), inv_inv', smul_smul,
mul_inv_cancel_right' (right_ne_zero_of_mul h.ne'), smul_add,
smul_inv_smul' (left_ne_zero_of_mul h.ne')]
/-- Given `c = line_map a b r`, `a < c`, the point `(c, f c)` is non-strictly above the
segment `[(a, f a), (b, f b)]` if and only if `slope f a b ≤ slope f a c`. -/
lemma line_map_le_map_iff_slope_le_slope_left (h : 0 < r * (b - a)) :
line_map (f a) (f b) r ≤ f c ↔ slope f a b ≤ slope f a c :=
@map_le_line_map_iff_slope_le_slope_left k (order_dual E) _ _ _ _ f a b r h
/-- Given `c = line_map a b r`, `a < c`, the point `(c, f c)` is strictly below the
segment `[(a, f a), (b, f b)]` if and only if `slope f a c < slope f a b`. -/
lemma map_lt_line_map_iff_slope_lt_slope_left (h : 0 < r * (b - a)) :
f c < line_map (f a) (f b) r ↔ slope f a c < slope f a b :=
lt_iff_lt_of_le_iff_le' (line_map_le_map_iff_slope_le_slope_left h)
(map_le_line_map_iff_slope_le_slope_left h)
/-- Given `c = line_map a b r`, `a < c`, the point `(c, f c)` is strictly above the
segment `[(a, f a), (b, f b)]` if and only if `slope f a b < slope f a c`. -/
lemma line_map_lt_map_iff_slope_lt_slope_left (h : 0 < r * (b - a)) :
line_map (f a) (f b) r < f c ↔ slope f a b < slope f a c :=
@map_lt_line_map_iff_slope_lt_slope_left k (order_dual E) _ _ _ _ f a b r h
/-- Given `c = line_map a b r`, `c < b`, the point `(c, f c)` is non-strictly below the
segment `[(a, f a), (b, f b)]` if and only if `slope f a b ≤ slope f c b`. -/
lemma map_le_line_map_iff_slope_le_slope_right (h : 0 < (1 - r) * (b - a)) :
f c ≤ line_map (f a) (f b) r ↔ slope f a b ≤ slope f c b :=
begin
rw [← line_map_apply_one_sub, ← line_map_apply_one_sub _ _ r],
revert h, generalize : 1 - r = r', clear r, intro h,
simp_rw [line_map_apply, slope, vsub_eq_sub, vadd_eq_add, smul_eq_mul],
rw [sub_add_eq_sub_sub_swap, sub_self, zero_sub, le_smul_iff_of_pos, inv_inv', smul_smul,
neg_mul_eq_mul_neg, neg_sub, mul_inv_cancel_right', le_sub, ← neg_sub (f b), smul_neg,
neg_add_eq_sub],
{ exact right_ne_zero_of_mul h.ne' },
{ simpa [mul_sub] using h }
end
/-- Given `c = line_map a b r`, `c < b`, the point `(c, f c)` is non-strictly above the
segment `[(a, f a), (b, f b)]` if and only if `slope f c b ≤ slope f a b`. -/
lemma line_map_le_map_iff_slope_le_slope_right (h : 0 < (1 - r) * (b - a)) :
line_map (f a) (f b) r ≤ f c ↔ slope f c b ≤ slope f a b :=
@map_le_line_map_iff_slope_le_slope_right k (order_dual E) _ _ _ _ f a b r h
/-- Given `c = line_map a b r`, `c < b`, the point `(c, f c)` is strictly below the
segment `[(a, f a), (b, f b)]` if and only if `slope f a b < slope f c b`. -/
lemma map_lt_line_map_iff_slope_lt_slope_right (h : 0 < (1 - r) * (b - a)) :
f c < line_map (f a) (f b) r ↔ slope f a b < slope f c b :=
lt_iff_lt_of_le_iff_le' (line_map_le_map_iff_slope_le_slope_right h)
(map_le_line_map_iff_slope_le_slope_right h)
/-- Given `c = line_map a b r`, `c < b`, the point `(c, f c)` is strictly above the
segment `[(a, f a), (b, f b)]` if and only if `slope f c b < slope f a b`. -/
lemma line_map_lt_map_iff_slope_lt_slope_right (h : 0 < (1 - r) * (b - a)) :
line_map (f a) (f b) r < f c ↔ slope f c b < slope f a b :=
@map_lt_line_map_iff_slope_lt_slope_right k (order_dual E) _ _ _ _ f a b r h
/-- Given `c = line_map a b r`, `a < c < b`, the point `(c, f c)` is non-strictly below the
segment `[(a, f a), (b, f b)]` if and only if `slope f a c ≤ slope f c b`. -/
lemma map_le_line_map_iff_slope_le_slope (hab : a < b) (h₀ : 0 < r) (h₁ : r < 1) :
f c ≤ line_map (f a) (f b) r ↔ slope f a c ≤ slope f c b :=
begin
rw [map_le_line_map_iff_slope_le_slope_left (mul_pos h₀ (sub_pos.2 hab)),
← line_map_slope_line_map_slope_line_map f a b r, right_le_line_map_iff_le h₁],
apply_instance,
apply_instance,
end
/-- Given `c = line_map a b r`, `a < c < b`, the point `(c, f c)` is non-strictly above the
segment `[(a, f a), (b, f b)]` if and only if `slope f c b ≤ slope f a c`. -/
lemma line_map_le_map_iff_slope_le_slope (hab : a < b) (h₀ : 0 < r) (h₁ : r < 1) :
line_map (f a) (f b) r ≤ f c ↔ slope f c b ≤ slope f a c :=
@map_le_line_map_iff_slope_le_slope k (order_dual E) _ _ _ _ _ _ _ _ hab h₀ h₁
/-- Given `c = line_map a b r`, `a < c < b`, the point `(c, f c)` is strictly below the
segment `[(a, f a), (b, f b)]` if and only if `slope f a c < slope f c b`. -/
lemma map_lt_line_map_iff_slope_lt_slope (hab : a < b) (h₀ : 0 < r) (h₁ : r < 1) :
f c < line_map (f a) (f b) r ↔ slope f a c < slope f c b :=
lt_iff_lt_of_le_iff_le' (line_map_le_map_iff_slope_le_slope hab h₀ h₁)
(map_le_line_map_iff_slope_le_slope hab h₀ h₁)
/-- Given `c = line_map a b r`, `a < c < b`, the point `(c, f c)` is strictly above the
segment `[(a, f a), (b, f b)]` if and only if `slope f c b < slope f a c`. -/
lemma line_map_lt_map_iff_slope_lt_slope (hab : a < b) (h₀ : 0 < r) (h₁ : r < 1) :
line_map (f a) (f b) r < f c ↔ slope f c b < slope f a c :=
@map_lt_line_map_iff_slope_lt_slope k (order_dual E) _ _ _ _ _ _ _ _ hab h₀ h₁
end linear_ordered_field
|
f7ae23eb6a313bf31b31dc7dc16d90852d734729 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/data/int/gcd.lean | 16384bf0f6852b72bcaf34c5fe054994d692fb26 | [
"Apache-2.0"
] | permissive | ayush1801/mathlib | 78949b9f789f488148142221606bf15c02b960d2 | ce164e28f262acbb3de6281b3b03660a9f744e3c | refs/heads/master | 1,692,886,907,941 | 1,635,270,866,000 | 1,635,270,866,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 24,390 | lean | /-
Copyright (c) 2018 Guy Leroy. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sangwoo Jo (aka Jason), Guy Leroy, Johannes Hölzl, Mario Carneiro
-/
import data.nat.prime
/-!
# Extended GCD and divisibility over ℤ
## Main definitions
* Given `x y : ℕ`, `xgcd x y` computes the pair of integers `(a, b)` such that
`gcd x y = x * a + y * b`. `gcd_a x y` and `gcd_b x y` are defined to be `a` and `b`,
respectively.
## Main statements
* `gcd_eq_gcd_ab`: Bézout's lemma, given `x y : ℕ`, `gcd x y = x * gcd_a x y + y * gcd_b x y`.
## Tags
Bézout's lemma, Bezout's lemma
-/
/-! ### Extended Euclidean algorithm -/
namespace nat
/-- Helper function for the extended GCD algorithm (`nat.xgcd`). -/
def xgcd_aux : ℕ → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ
| 0 s t r' s' t' := (r', s', t')
| r@(succ _) s t r' s' t' :=
have r' % r < r, from mod_lt _ $ succ_pos _,
let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t
@[simp] theorem xgcd_zero_left {s t r' s' t'} : xgcd_aux 0 s t r' s' t' = (r', s', t') :=
by simp [xgcd_aux]
theorem xgcd_aux_rec {r s t r' s' t'} (h : 0 < r) :
xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t :=
by cases r; [exact absurd h (lt_irrefl _), {simp only [xgcd_aux], refl}]
/-- Use the extended GCD algorithm to generate the `a` and `b` values
satisfying `gcd x y = x * a + y * b`. -/
def xgcd (x y : ℕ) : ℤ × ℤ := (xgcd_aux x 1 0 y 0 1).2
/-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_a (x y : ℕ) : ℤ := (xgcd x y).1
/-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_b (x y : ℕ) : ℤ := (xgcd x y).2
@[simp] theorem gcd_a_zero_left {s : ℕ} : gcd_a 0 s = 0 :=
by { unfold gcd_a, rw [xgcd, xgcd_zero_left] }
@[simp] theorem gcd_b_zero_left {s : ℕ} : gcd_b 0 s = 1 :=
by { unfold gcd_b, rw [xgcd, xgcd_zero_left] }
@[simp] theorem gcd_a_zero_right {s : ℕ} (h : s ≠ 0) : gcd_a s 0 = 1 :=
begin
unfold gcd_a xgcd,
induction s,
{ exact absurd rfl h, },
{ simp [xgcd_aux], }
end
@[simp] theorem gcd_b_zero_right {s : ℕ} (h : s ≠ 0) : gcd_b s 0 = 0 :=
begin
unfold gcd_b xgcd,
induction s,
{ exact absurd rfl h, },
{ simp [xgcd_aux], }
end
@[simp] theorem xgcd_aux_fst (x y) : ∀ s t s' t',
(xgcd_aux x s t y s' t').1 = gcd x y :=
gcd.induction x y (by simp) (λ x y h IH s t s' t', by simp [xgcd_aux_rec, h, IH]; rw ← gcd_rec)
theorem xgcd_aux_val (x y) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) :=
by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1]; cases xgcd_aux x 1 0 y 0 1; refl
theorem xgcd_val (x y) : xgcd x y = (gcd_a x y, gcd_b x y) :=
by unfold gcd_a gcd_b; cases xgcd x y; refl
section
parameters (x y : ℕ)
private def P : ℕ × ℤ × ℤ → Prop
| (r, s, t) := (r : ℤ) = x * s + y * t
theorem xgcd_aux_P {r r'} : ∀ {s t s' t'}, P (r, s, t) → P (r', s', t') →
P (xgcd_aux r s t r' s' t') :=
gcd.induction r r' (by simp) $ λ a b h IH s t s' t' p p', begin
rw [xgcd_aux_rec h], refine IH _ p, dsimp [P] at *,
rw [int.mod_def], generalize : (b / a : ℤ) = k,
rw [p, p'],
simp [mul_add, mul_comm, mul_left_comm, add_comm, add_left_comm, sub_eq_neg_add, mul_assoc]
end
/-- **Bézout's lemma**: given `x y : ℕ`, `gcd x y = x * a + y * b`, where `a = gcd_a x y` and
`b = gcd_b x y` are computed by the extended Euclidean algorithm.
-/
theorem gcd_eq_gcd_ab : (gcd x y : ℤ) = x * gcd_a x y + y * gcd_b x y :=
by have := @xgcd_aux_P x y x y 1 0 0 1 (by simp [P]) (by simp [P]);
rwa [xgcd_aux_val, xgcd_val] at this
end
lemma exists_mul_mod_eq_gcd {k n : ℕ} (hk : gcd n k < k) :
∃ m, n * m % k = gcd n k :=
begin
have hk' := int.coe_nat_ne_zero.mpr (ne_of_gt (lt_of_le_of_lt (zero_le (gcd n k)) hk)),
have key := congr_arg (λ m, int.nat_mod m k) (gcd_eq_gcd_ab n k),
simp_rw int.nat_mod at key,
rw [int.add_mul_mod_self_left, ←int.coe_nat_mod, int.to_nat_coe_nat, mod_eq_of_lt hk] at key,
refine ⟨(n.gcd_a k % k).to_nat, eq.trans (int.coe_nat_inj _) key.symm⟩,
rw [int.coe_nat_mod, int.coe_nat_mul, int.to_nat_of_nonneg (int.mod_nonneg _ hk'),
int.to_nat_of_nonneg (int.mod_nonneg _ hk'), int.mul_mod, int.mod_mod, ←int.mul_mod],
end
lemma exists_mul_mod_eq_one_of_coprime {k n : ℕ} (hkn : coprime n k) (hk : 1 < k) :
∃ m, n * m % k = 1 :=
Exists.cases_on (exists_mul_mod_eq_gcd (lt_of_le_of_lt (le_of_eq hkn) hk))
(λ m hm, ⟨m, hm.trans hkn⟩)
end nat
/-! ### Divisibility over ℤ -/
namespace int
protected lemma coe_nat_gcd (m n : ℕ) : int.gcd ↑m ↑n = nat.gcd m n := rfl
/-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_a : ℤ → ℤ → ℤ
| (of_nat m) n := m.gcd_a n.nat_abs
| -[1+ m] n := -m.succ.gcd_a n.nat_abs
/-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/
def gcd_b : ℤ → ℤ → ℤ
| m (of_nat n) := m.nat_abs.gcd_b n
| m -[1+ n] := -m.nat_abs.gcd_b n.succ
/-- **Bézout's lemma** -/
theorem gcd_eq_gcd_ab : ∀ x y : ℤ, (gcd x y : ℤ) = x * gcd_a x y + y * gcd_b x y
| (m : ℕ) (n : ℕ) := nat.gcd_eq_gcd_ab _ _
| (m : ℕ) -[1+ n] := show (_ : ℤ) = _ + -(n+1) * -_, by rw neg_mul_neg; apply nat.gcd_eq_gcd_ab
| -[1+ m] (n : ℕ) := show (_ : ℤ) = -(m+1) * -_ + _ , by rw neg_mul_neg; apply nat.gcd_eq_gcd_ab
| -[1+ m] -[1+ n] := show (_ : ℤ) = -(m+1) * -_ + -(n+1) * -_,
by { rw [neg_mul_neg, neg_mul_neg], apply nat.gcd_eq_gcd_ab }
theorem nat_abs_div (a b : ℤ) (H : b ∣ a) : nat_abs (a / b) = (nat_abs a) / (nat_abs b) :=
begin
cases (nat.eq_zero_or_pos (nat_abs b)),
{rw eq_zero_of_nat_abs_eq_zero h, simp [int.div_zero]},
calc
nat_abs (a / b) = nat_abs (a / b) * 1 : by rw mul_one
... = nat_abs (a / b) * (nat_abs b / nat_abs b) : by rw nat.div_self h
... = nat_abs (a / b) * nat_abs b / nat_abs b : by rw (nat.mul_div_assoc _ dvd_rfl)
... = nat_abs (a / b * b) / nat_abs b : by rw (nat_abs_mul (a / b) b)
... = nat_abs a / nat_abs b : by rw int.div_mul_cancel H,
end
theorem nat_abs_dvd_abs_iff {i j : ℤ} : i.nat_abs ∣ j.nat_abs ↔ i ∣ j :=
⟨assume (H : i.nat_abs ∣ j.nat_abs), dvd_nat_abs.mp (nat_abs_dvd.mp (coe_nat_dvd.mpr H)),
assume H : (i ∣ j), coe_nat_dvd.mp (dvd_nat_abs.mpr (nat_abs_dvd.mpr H))⟩
lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : ℕ} (p_prime : nat.prime p) {m n : ℤ} {k l : ℕ}
(hpm : ↑(p ^ k) ∣ m)
(hpn : ↑(p ^ l) ∣ n) (hpmn : ↑(p ^ (k+l+1)) ∣ m*n) : ↑(p ^ (k+1)) ∣ m ∨ ↑(p ^ (l+1)) ∣ n :=
have hpm' : p ^ k ∣ m.nat_abs, from int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpm,
have hpn' : p ^ l ∣ n.nat_abs, from int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpn,
have hpmn' : (p ^ (k+l+1)) ∣ m.nat_abs*n.nat_abs,
by rw ←int.nat_abs_mul; apply (int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpmn),
let hsd := nat.succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul p_prime hpm' hpn' hpmn' in
hsd.elim
(λ hsd1, or.inl begin apply int.dvd_nat_abs.1, apply int.coe_nat_dvd.2 hsd1 end)
(λ hsd2, or.inr begin apply int.dvd_nat_abs.1, apply int.coe_nat_dvd.2 hsd2 end)
theorem dvd_of_mul_dvd_mul_left {i j k : ℤ} (k_non_zero : k ≠ 0) (H : k * i ∣ k * j) : i ∣ j :=
dvd.elim H (λl H1, by rw mul_assoc at H1; exact ⟨_, mul_left_cancel₀ k_non_zero H1⟩)
theorem dvd_of_mul_dvd_mul_right {i j k : ℤ} (k_non_zero : k ≠ 0) (H : i * k ∣ j * k) : i ∣ j :=
by rw [mul_comm i k, mul_comm j k] at H; exact dvd_of_mul_dvd_mul_left k_non_zero H
lemma prime.dvd_nat_abs_of_coe_dvd_sq {p : ℕ} (hp : p.prime) (k : ℤ) (h : ↑p ∣ k ^ 2) :
p ∣ k.nat_abs :=
begin
apply @nat.prime.dvd_of_dvd_pow _ _ 2 hp,
rwa [sq, ← nat_abs_mul, ← coe_nat_dvd_left, ← sq]
end
/-- ℤ specific version of least common multiple. -/
def lcm (i j : ℤ) : ℕ := nat.lcm (nat_abs i) (nat_abs j)
theorem lcm_def (i j : ℤ) : lcm i j = nat.lcm (nat_abs i) (nat_abs j) := rfl
protected lemma coe_nat_lcm (m n : ℕ) : int.lcm ↑m ↑n = nat.lcm m n := rfl
theorem gcd_dvd_left (i j : ℤ) : (gcd i j : ℤ) ∣ i :=
dvd_nat_abs.mp $ coe_nat_dvd.mpr $ nat.gcd_dvd_left _ _
theorem gcd_dvd_right (i j : ℤ) : (gcd i j : ℤ) ∣ j :=
dvd_nat_abs.mp $ coe_nat_dvd.mpr $ nat.gcd_dvd_right _ _
theorem dvd_gcd {i j k : ℤ} (h1 : k ∣ i) (h2 : k ∣ j) : k ∣ gcd i j :=
nat_abs_dvd.1 $ coe_nat_dvd.2 $ nat.dvd_gcd (nat_abs_dvd_abs_iff.2 h1) (nat_abs_dvd_abs_iff.2 h2)
theorem gcd_mul_lcm (i j : ℤ) : gcd i j * lcm i j = nat_abs (i * j) :=
by rw [int.gcd, int.lcm, nat.gcd_mul_lcm, nat_abs_mul]
theorem gcd_comm (i j : ℤ) : gcd i j = gcd j i := nat.gcd_comm _ _
theorem gcd_assoc (i j k : ℤ) : gcd (gcd i j) k = gcd i (gcd j k) := nat.gcd_assoc _ _ _
@[simp] theorem gcd_self (i : ℤ) : gcd i i = nat_abs i := by simp [gcd]
@[simp] theorem gcd_zero_left (i : ℤ) : gcd 0 i = nat_abs i := by simp [gcd]
@[simp] theorem gcd_zero_right (i : ℤ) : gcd i 0 = nat_abs i := by simp [gcd]
@[simp] theorem gcd_one_left (i : ℤ) : gcd 1 i = 1 := nat.gcd_one_left _
@[simp] theorem gcd_one_right (i : ℤ) : gcd i 1 = 1 := nat.gcd_one_right _
theorem gcd_mul_left (i j k : ℤ) : gcd (i * j) (i * k) = nat_abs i * gcd j k :=
by { rw [int.gcd, int.gcd, nat_abs_mul, nat_abs_mul], apply nat.gcd_mul_left }
theorem gcd_mul_right (i j k : ℤ) : gcd (i * j) (k * j) = gcd i k * nat_abs j :=
by { rw [int.gcd, int.gcd, nat_abs_mul, nat_abs_mul], apply nat.gcd_mul_right }
theorem gcd_pos_of_non_zero_left {i : ℤ} (j : ℤ) (i_non_zero : i ≠ 0) : 0 < gcd i j :=
nat.gcd_pos_of_pos_left (nat_abs j) (nat_abs_pos_of_ne_zero i_non_zero)
theorem gcd_pos_of_non_zero_right (i : ℤ) {j : ℤ} (j_non_zero : j ≠ 0) : 0 < gcd i j :=
nat.gcd_pos_of_pos_right (nat_abs i) (nat_abs_pos_of_ne_zero j_non_zero)
theorem gcd_eq_zero_iff {i j : ℤ} : gcd i j = 0 ↔ i = 0 ∧ j = 0 :=
begin
rw int.gcd,
split,
{ intro h,
exact ⟨nat_abs_eq_zero.mp (nat.eq_zero_of_gcd_eq_zero_left h),
nat_abs_eq_zero.mp (nat.eq_zero_of_gcd_eq_zero_right h)⟩ },
{ intro h, rw [nat_abs_eq_zero.mpr h.left, nat_abs_eq_zero.mpr h.right],
apply nat.gcd_zero_left }
end
theorem gcd_div {i j k : ℤ} (H1 : k ∣ i) (H2 : k ∣ j) :
gcd (i / k) (j / k) = gcd i j / nat_abs k :=
by rw [gcd, nat_abs_div i k H1, nat_abs_div j k H2];
exact nat.gcd_div (nat_abs_dvd_abs_iff.mpr H1) (nat_abs_dvd_abs_iff.mpr H2)
theorem gcd_div_gcd_div_gcd {i j : ℤ} (H : 0 < gcd i j) :
gcd (i / gcd i j) (j / gcd i j) = 1 :=
begin
rw [gcd_div (gcd_dvd_left i j) (gcd_dvd_right i j)],
rw [nat_abs_of_nat, nat.div_self H]
end
theorem gcd_dvd_gcd_of_dvd_left {i k : ℤ} (j : ℤ) (H : i ∣ k) : gcd i j ∣ gcd k j :=
int.coe_nat_dvd.1 $ dvd_gcd ((gcd_dvd_left i j).trans H) (gcd_dvd_right i j)
theorem gcd_dvd_gcd_of_dvd_right {i k : ℤ} (j : ℤ) (H : i ∣ k) : gcd j i ∣ gcd j k :=
int.coe_nat_dvd.1 $ dvd_gcd (gcd_dvd_left j i) ((gcd_dvd_right j i).trans H)
theorem gcd_dvd_gcd_mul_left (i j k : ℤ) : gcd i j ∣ gcd (k * i) j :=
gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _)
theorem gcd_dvd_gcd_mul_right (i j k : ℤ) : gcd i j ∣ gcd (i * k) j :=
gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _)
theorem gcd_dvd_gcd_mul_left_right (i j k : ℤ) : gcd i j ∣ gcd i (k * j) :=
gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _)
theorem gcd_dvd_gcd_mul_right_right (i j k : ℤ) : gcd i j ∣ gcd i (j * k) :=
gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _)
theorem gcd_eq_left {i j : ℤ} (H : i ∣ j) : gcd i j = nat_abs i :=
nat.dvd_antisymm (by unfold gcd; exact nat.gcd_dvd_left _ _)
(by unfold gcd; exact nat.dvd_gcd dvd_rfl (nat_abs_dvd_abs_iff.mpr H))
theorem gcd_eq_right {i j : ℤ} (H : j ∣ i) : gcd i j = nat_abs j :=
by rw [gcd_comm, gcd_eq_left H]
theorem ne_zero_of_gcd {x y : ℤ}
(hc : gcd x y ≠ 0) : x ≠ 0 ∨ y ≠ 0 :=
begin
contrapose! hc,
rw [hc.left, hc.right, gcd_zero_right, nat_abs_zero]
end
theorem exists_gcd_one {m n : ℤ} (H : 0 < gcd m n) :
∃ (m' n' : ℤ), gcd m' n' = 1 ∧ m = m' * gcd m n ∧ n = n' * gcd m n :=
⟨_, _, gcd_div_gcd_div_gcd H,
(int.div_mul_cancel (gcd_dvd_left m n)).symm,
(int.div_mul_cancel (gcd_dvd_right m n)).symm⟩
theorem exists_gcd_one' {m n : ℤ} (H : 0 < gcd m n) :
∃ (g : ℕ) (m' n' : ℤ), 0 < g ∧ gcd m' n' = 1 ∧ m = m' * g ∧ n = n' * g :=
let ⟨m', n', h⟩ := exists_gcd_one H in ⟨_, m', n', H, h⟩
theorem pow_dvd_pow_iff {m n : ℤ} {k : ℕ} (k0 : 0 < k) : m ^ k ∣ n ^ k ↔ m ∣ n :=
begin
refine ⟨λ h, _, λ h, pow_dvd_pow_of_dvd h _⟩,
apply int.nat_abs_dvd_abs_iff.mp,
apply (nat.pow_dvd_pow_iff k0).mp,
rw [← int.nat_abs_pow, ← int.nat_abs_pow],
exact int.nat_abs_dvd_abs_iff.mpr h
end
/-! ### lcm -/
theorem lcm_comm (i j : ℤ) : lcm i j = lcm j i :=
by { rw [int.lcm, int.lcm], exact nat.lcm_comm _ _ }
theorem lcm_assoc (i j k : ℤ) : lcm (lcm i j) k = lcm i (lcm j k) :=
by { rw [int.lcm, int.lcm, int.lcm, int.lcm, nat_abs_of_nat, nat_abs_of_nat], apply nat.lcm_assoc }
@[simp] theorem lcm_zero_left (i : ℤ) : lcm 0 i = 0 :=
by { rw [int.lcm], apply nat.lcm_zero_left }
@[simp] theorem lcm_zero_right (i : ℤ) : lcm i 0 = 0 :=
by { rw [int.lcm], apply nat.lcm_zero_right }
@[simp] theorem lcm_one_left (i : ℤ) : lcm 1 i = nat_abs i :=
by { rw int.lcm, apply nat.lcm_one_left }
@[simp] theorem lcm_one_right (i : ℤ) : lcm i 1 = nat_abs i :=
by { rw int.lcm, apply nat.lcm_one_right }
@[simp] theorem lcm_self (i : ℤ) : lcm i i = nat_abs i :=
by { rw int.lcm, apply nat.lcm_self }
theorem dvd_lcm_left (i j : ℤ) : i ∣ lcm i j :=
by { rw int.lcm, apply coe_nat_dvd_right.mpr, apply nat.dvd_lcm_left }
theorem dvd_lcm_right (i j : ℤ) : j ∣ lcm i j :=
by { rw int.lcm, apply coe_nat_dvd_right.mpr, apply nat.dvd_lcm_right }
theorem lcm_dvd {i j k : ℤ} : i ∣ k → j ∣ k → (lcm i j : ℤ) ∣ k :=
begin
rw int.lcm,
intros hi hj,
exact coe_nat_dvd_left.mpr
(nat.lcm_dvd (nat_abs_dvd_abs_iff.mpr hi) (nat_abs_dvd_abs_iff.mpr hj))
end
end int
lemma pow_gcd_eq_one {M : Type*} [monoid M] (x : M) {m n : ℕ} (hm : x ^ m = 1) (hn : x ^ n = 1) :
x ^ m.gcd n = 1 :=
begin
cases m, { simp only [hn, nat.gcd_zero_left] },
obtain ⟨x, rfl⟩ : is_unit x,
{ apply is_unit_of_pow_eq_one _ _ hm m.succ_pos },
simp only [← units.coe_pow] at *,
rw [← units.coe_one, ← gpow_coe_nat, ← units.ext_iff] at *,
simp only [nat.gcd_eq_gcd_ab, gpow_add, gpow_mul, hm, hn, one_gpow, one_mul]
end
lemma gcd_nsmul_eq_zero {M : Type*} [add_monoid M] (x : M) {m n : ℕ} (hm : m • x = 0)
(hn : n • x = 0) : (m.gcd n) • x = 0 :=
begin
apply multiplicative.of_add.injective,
rw [of_add_nsmul, of_add_zero, pow_gcd_eq_one];
rwa [←of_add_nsmul, ←of_add_zero, equiv.apply_eq_iff_eq]
end
/-! ### GCD prover -/
open norm_num
namespace tactic
namespace norm_num
lemma int_gcd_helper' {d : ℕ} {x y a b : ℤ} (h₁ : (d:ℤ) ∣ x) (h₂ : (d:ℤ) ∣ y)
(h₃ : x * a + y * b = d) : int.gcd x y = d :=
begin
refine nat.dvd_antisymm _ (int.coe_nat_dvd.1 (int.dvd_gcd h₁ h₂)),
rw [← int.coe_nat_dvd, ← h₃],
apply dvd_add,
{ exact (int.gcd_dvd_left _ _).mul_right _ },
{ exact (int.gcd_dvd_right _ _).mul_right _ }
end
lemma nat_gcd_helper_dvd_left (x y a : ℕ) (h : x * a = y) : nat.gcd x y = x :=
nat.gcd_eq_left ⟨a, h.symm⟩
lemma nat_gcd_helper_dvd_right (x y a : ℕ) (h : y * a = x) : nat.gcd x y = y :=
nat.gcd_eq_right ⟨a, h.symm⟩
lemma nat_gcd_helper_2 (d x y a b u v tx ty : ℕ) (hu : d * u = x) (hv : d * v = y)
(hx : x * a = tx) (hy : y * b = ty) (h : ty + d = tx) : nat.gcd x y = d :=
begin
rw ← int.coe_nat_gcd, apply @int_gcd_helper' _ _ _ a (-b)
(int.coe_nat_dvd.2 ⟨_, hu.symm⟩) (int.coe_nat_dvd.2 ⟨_, hv.symm⟩),
rw [mul_neg_eq_neg_mul_symm, ← sub_eq_add_neg, sub_eq_iff_eq_add'],
norm_cast, rw [hx, hy, h]
end
lemma nat_gcd_helper_1 (d x y a b u v tx ty : ℕ) (hu : d * u = x) (hv : d * v = y)
(hx : x * a = tx) (hy : y * b = ty) (h : tx + d = ty) : nat.gcd x y = d :=
(nat.gcd_comm _ _).trans $ nat_gcd_helper_2 _ _ _ _ _ _ _ _ _ hv hu hy hx h
lemma nat_lcm_helper (x y d m n : ℕ) (hd : nat.gcd x y = d) (d0 : 0 < d)
(xy : x * y = n) (dm : d * m = n) : nat.lcm x y = m :=
(nat.mul_right_inj d0).1 $ by rw [dm, ← xy, ← hd, nat.gcd_mul_lcm]
lemma nat_coprime_helper_zero_left (x : ℕ) (h : 1 < x) : ¬ nat.coprime 0 x :=
mt (nat.coprime_zero_left _).1 $ ne_of_gt h
lemma nat_coprime_helper_zero_right (x : ℕ) (h : 1 < x) : ¬ nat.coprime x 0 :=
mt (nat.coprime_zero_right _).1 $ ne_of_gt h
lemma nat_coprime_helper_1 (x y a b tx ty : ℕ)
(hx : x * a = tx) (hy : y * b = ty) (h : tx + 1 = ty) : nat.coprime x y :=
nat_gcd_helper_1 _ _ _ _ _ _ _ _ _ (one_mul _) (one_mul _) hx hy h
lemma nat_coprime_helper_2 (x y a b tx ty : ℕ)
(hx : x * a = tx) (hy : y * b = ty) (h : ty + 1 = tx) : nat.coprime x y :=
nat_gcd_helper_2 _ _ _ _ _ _ _ _ _ (one_mul _) (one_mul _) hx hy h
lemma nat_not_coprime_helper (d x y u v : ℕ) (hu : d * u = x) (hv : d * v = y)
(h : 1 < d) : ¬ nat.coprime x y :=
nat.not_coprime_of_dvd_of_dvd h ⟨_, hu.symm⟩ ⟨_, hv.symm⟩
lemma int_gcd_helper (x y : ℤ) (nx ny d : ℕ) (hx : (nx:ℤ) = x) (hy : (ny:ℤ) = y)
(h : nat.gcd nx ny = d) : int.gcd x y = d :=
by rwa [← hx, ← hy, int.coe_nat_gcd]
lemma int_gcd_helper_neg_left (x y : ℤ) (d : ℕ) (h : int.gcd x y = d) : int.gcd (-x) y = d :=
by rw int.gcd at h ⊢; rwa int.nat_abs_neg
lemma int_gcd_helper_neg_right (x y : ℤ) (d : ℕ) (h : int.gcd x y = d) : int.gcd x (-y) = d :=
by rw int.gcd at h ⊢; rwa int.nat_abs_neg
lemma int_lcm_helper (x y : ℤ) (nx ny d : ℕ) (hx : (nx:ℤ) = x) (hy : (ny:ℤ) = y)
(h : nat.lcm nx ny = d) : int.lcm x y = d :=
by rwa [← hx, ← hy, int.coe_nat_lcm]
lemma int_lcm_helper_neg_left (x y : ℤ) (d : ℕ) (h : int.lcm x y = d) : int.lcm (-x) y = d :=
by rw int.lcm at h ⊢; rwa int.nat_abs_neg
lemma int_lcm_helper_neg_right (x y : ℤ) (d : ℕ) (h : int.lcm x y = d) : int.lcm x (-y) = d :=
by rw int.lcm at h ⊢; rwa int.nat_abs_neg
/-- Evaluates the `nat.gcd` function. -/
meta def prove_gcd_nat (c : instance_cache) (ex ey : expr) :
tactic (instance_cache × expr × expr) := do
x ← ex.to_nat,
y ← ey.to_nat,
match x, y with
| 0, _ := pure (c, ey, `(nat.gcd_zero_left).mk_app [ey])
| _, 0 := pure (c, ex, `(nat.gcd_zero_right).mk_app [ex])
| 1, _ := pure (c, `(1:ℕ), `(nat.gcd_one_left).mk_app [ey])
| _, 1 := pure (c, `(1:ℕ), `(nat.gcd_one_right).mk_app [ex])
| _, _ := do
let (d, a, b) := nat.xgcd_aux x 1 0 y 0 1,
if d = x then do
(c, ea) ← c.of_nat (y / x),
(c, _, p) ← prove_mul_nat c ex ea,
pure (c, ex, `(nat_gcd_helper_dvd_left).mk_app [ex, ey, ea, p])
else if d = y then do
(c, ea) ← c.of_nat (x / y),
(c, _, p) ← prove_mul_nat c ey ea,
pure (c, ey, `(nat_gcd_helper_dvd_right).mk_app [ex, ey, ea, p])
else do
(c, ed) ← c.of_nat d,
(c, ea) ← c.of_nat a.nat_abs,
(c, eb) ← c.of_nat b.nat_abs,
(c, eu) ← c.of_nat (x / d),
(c, ev) ← c.of_nat (y / d),
(c, _, pu) ← prove_mul_nat c ed eu,
(c, _, pv) ← prove_mul_nat c ed ev,
(c, etx, px) ← prove_mul_nat c ex ea,
(c, ety, py) ← prove_mul_nat c ey eb,
(c, p) ← if a ≥ 0 then prove_add_nat c ety ed etx else prove_add_nat c etx ed ety,
let pf : expr := if a ≥ 0 then `(nat_gcd_helper_2) else `(nat_gcd_helper_1),
pure (c, ed, pf.mk_app [ed, ex, ey, ea, eb, eu, ev, etx, ety, pu, pv, px, py, p])
end
/-- Evaluates the `nat.lcm` function. -/
meta def prove_lcm_nat (c : instance_cache) (ex ey : expr) :
tactic (instance_cache × expr × expr) := do
x ← ex.to_nat,
y ← ey.to_nat,
match x, y with
| 0, _ := pure (c, `(0:ℕ), `(nat.lcm_zero_left).mk_app [ey])
| _, 0 := pure (c, `(0:ℕ), `(nat.lcm_zero_right).mk_app [ex])
| 1, _ := pure (c, ey, `(nat.lcm_one_left).mk_app [ey])
| _, 1 := pure (c, ex, `(nat.lcm_one_right).mk_app [ex])
| _, _ := do
(c, ed, pd) ← prove_gcd_nat c ex ey,
(c, p0) ← prove_pos c ed,
(c, en, xy) ← prove_mul_nat c ex ey,
d ← ed.to_nat,
(c, em) ← c.of_nat ((x * y) / d),
(c, _, dm) ← prove_mul_nat c ed em,
pure (c, em, `(nat_lcm_helper).mk_app [ex, ey, ed, em, en, pd, p0, xy, dm])
end
/-- Evaluates the `int.gcd` function. -/
meta def prove_gcd_int (zc nc : instance_cache) : expr → expr →
tactic (instance_cache × instance_cache × expr × expr)
| x y := match match_neg x with
| some x := do
(zc, nc, d, p) ← prove_gcd_int x y,
pure (zc, nc, d, `(int_gcd_helper_neg_left).mk_app [x, y, d, p])
| none := match match_neg y with
| some y := do
(zc, nc, d, p) ← prove_gcd_int x y,
pure (zc, nc, d, `(int_gcd_helper_neg_right).mk_app [x, y, d, p])
| none := do
(zc, nc, nx, px) ← prove_nat_uncast zc nc x,
(zc, nc, ny, py) ← prove_nat_uncast zc nc y,
(nc, d, p) ← prove_gcd_nat nc nx ny,
pure (zc, nc, d, `(int_gcd_helper).mk_app [x, y, nx, ny, d, px, py, p])
end
end
/-- Evaluates the `int.lcm` function. -/
meta def prove_lcm_int (zc nc : instance_cache) : expr → expr →
tactic (instance_cache × instance_cache × expr × expr)
| x y := match match_neg x with
| some x := do
(zc, nc, d, p) ← prove_lcm_int x y,
pure (zc, nc, d, `(int_lcm_helper_neg_left).mk_app [x, y, d, p])
| none := match match_neg y with
| some y := do
(zc, nc, d, p) ← prove_lcm_int x y,
pure (zc, nc, d, `(int_lcm_helper_neg_right).mk_app [x, y, d, p])
| none := do
(zc, nc, nx, px) ← prove_nat_uncast zc nc x,
(zc, nc, ny, py) ← prove_nat_uncast zc nc y,
(nc, d, p) ← prove_lcm_nat nc nx ny,
pure (zc, nc, d, `(int_lcm_helper).mk_app [x, y, nx, ny, d, px, py, p])
end
end
/-- Evaluates the `nat.coprime` function. -/
meta def prove_coprime_nat (c : instance_cache) (ex ey : expr) :
tactic (instance_cache × (expr ⊕ expr)) := do
x ← ex.to_nat,
y ← ey.to_nat,
match x, y with
| 1, _ := pure (c, sum.inl $ `(nat.coprime_one_left).mk_app [ey])
| _, 1 := pure (c, sum.inl $ `(nat.coprime_one_right).mk_app [ex])
| 0, 0 := pure (c, sum.inr `(nat.not_coprime_zero_zero))
| 0, _ := do
c ← mk_instance_cache `(ℕ),
(c, p) ← prove_lt_nat c `(1) ey,
pure (c, sum.inr $ `(nat_coprime_helper_zero_left).mk_app [ey, p])
| _, 0 := do
c ← mk_instance_cache `(ℕ),
(c, p) ← prove_lt_nat c `(1) ex,
pure (c, sum.inr $ `(nat_coprime_helper_zero_right).mk_app [ex, p])
| _, _ := do
c ← mk_instance_cache `(ℕ),
let (d, a, b) := nat.xgcd_aux x 1 0 y 0 1,
if d = 1 then do
(c, ea) ← c.of_nat a.nat_abs,
(c, eb) ← c.of_nat b.nat_abs,
(c, etx, px) ← prove_mul_nat c ex ea,
(c, ety, py) ← prove_mul_nat c ey eb,
(c, p) ← if a ≥ 0 then prove_add_nat c ety `(1) etx else prove_add_nat c etx `(1) ety,
let pf : expr := if a ≥ 0 then `(nat_coprime_helper_2) else `(nat_coprime_helper_1),
pure (c, sum.inl $ pf.mk_app [ex, ey, ea, eb, etx, ety, px, py, p])
else do
(c, ed) ← c.of_nat d,
(c, eu) ← c.of_nat (x / d),
(c, ev) ← c.of_nat (y / d),
(c, _, pu) ← prove_mul_nat c ed eu,
(c, _, pv) ← prove_mul_nat c ed ev,
(c, p) ← prove_lt_nat c `(1) ed,
pure (c, sum.inr $ `(nat_not_coprime_helper).mk_app [ed, ex, ey, eu, ev, pu, pv, p])
end
/-- Evaluates the `gcd`, `lcm`, and `coprime` functions. -/
@[norm_num] meta def eval_gcd : expr → tactic (expr × expr)
| `(nat.gcd %%ex %%ey) := do
c ← mk_instance_cache `(ℕ),
prod.snd <$> prove_gcd_nat c ex ey
| `(nat.lcm %%ex %%ey) := do
c ← mk_instance_cache `(ℕ),
prod.snd <$> prove_lcm_nat c ex ey
| `(nat.coprime %%ex %%ey) := do
c ← mk_instance_cache `(ℕ),
prove_coprime_nat c ex ey >>= sum.elim true_intro false_intro ∘ prod.snd
| `(int.gcd %%ex %%ey) := do
zc ← mk_instance_cache `(ℤ),
nc ← mk_instance_cache `(ℕ),
(prod.snd ∘ prod.snd) <$> prove_gcd_int zc nc ex ey
| `(int.lcm %%ex %%ey) := do
zc ← mk_instance_cache `(ℤ),
nc ← mk_instance_cache `(ℕ),
(prod.snd ∘ prod.snd) <$> prove_lcm_int zc nc ex ey
| _ := failed
end norm_num
end tactic
|
7e816143f36d516f489bc374b5c11dbbd838b686 | f083c4ed5d443659f3ed9b43b1ca5bb037ddeb58 | /data/seq/computation.lean | 2c9bfc93fc9671f711d7e27c842a8fecc43f7196 | [
"Apache-2.0"
] | permissive | semorrison/mathlib | 1be6f11086e0d24180fec4b9696d3ec58b439d10 | 20b4143976dad48e664c4847b75a85237dca0a89 | refs/heads/master | 1,583,799,212,170 | 1,535,634,130,000 | 1,535,730,505,000 | 129,076,205 | 0 | 0 | Apache-2.0 | 1,551,697,998,000 | 1,523,442,265,000 | Lean | UTF-8 | Lean | false | false | 37,471 | lean | /-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
Coinductive formalization of unbounded computations.
-/
import data.stream data.nat.basic
universes u v w
/-
coinductive computation (α : Type u) : Type u
| return : α → computation α
| think : computation α → computation α
-/
/-- `computation α` is the type of unbounded computations returning `α`.
An element of `computation α` is an infinite sequence of `option α` such
that if `f n = some a` for some `n` then it is constantly `some a` after that. -/
def computation (α : Type u) : Type u :=
{ f : stream (option α) // ∀ {n a}, f n = some a → f (n+1) = some a }
namespace computation
variables {α : Type u} {β : Type v} {γ : Type w}
-- constructors
/-- `return a` is the computation that immediately terminates with result `a`. -/
def return (a : α) : computation α := ⟨stream.const (some a), λn a', id⟩
instance : has_coe α (computation α) := ⟨return⟩
/-- `think c` is the computation that delays for one "tick" and then performs
computation `c`. -/
def think (c : computation α) : computation α :=
⟨none :: c.1, λn a h, by {cases n with n, contradiction, exact c.2 h}⟩
/-- `thinkN c n` is the computation that delays for `n` ticks and then performs
computation `c`. -/
def thinkN (c : computation α) : ℕ → computation α
| 0 := c
| (n+1) := think (thinkN n)
-- check for immediate result
/-- `head c` is the first step of computation, either `some a` if `c = return a`
or `none` if `c = think c'`. -/
def head (c : computation α) : option α := c.1.head
-- one step of computation
/-- `tail c` is the remainder of computation, either `c` if `c = return a`
or `c'` if `c = think c'`. -/
def tail (c : computation α) : computation α :=
⟨c.1.tail, λ n a, let t := c.2 in t⟩
/-- `empty α` is the computation that never returns, an infinite sequence of
`think`s. -/
def empty (α) : computation α := ⟨stream.const none, λn a', id⟩
/-- `run_for c n` evaluates `c` for `n` steps and returns the result, or `none`
if it did not terminate after `n` steps. -/
def run_for : computation α → ℕ → option α := subtype.val
/-- `destruct c` is the destructor for `computation α` as a coinductive type.
It returns `inl a` if `c = return a` and `inr c'` if `c = think c'`. -/
def destruct (c : computation α) : α ⊕ computation α :=
match c.1 0 with
| none := sum.inr (tail c)
| some a := sum.inl a
end
/-- `run c` is an unsound meta function that runs `c` to completion, possibly
resulting in an infinite loop in the VM. -/
meta def run : computation α → α | c :=
match destruct c with
| sum.inl a := a
| sum.inr ca := run ca
end
theorem destruct_eq_ret {s : computation α} {a : α} :
destruct s = sum.inl a → s = return a :=
begin
dsimp [destruct],
induction f0 : s.1 0; intro h,
{ contradiction },
{ apply subtype.eq, funext n,
induction n with n IH,
{ injection h with h', rwa h' at f0 },
{ exact s.2 IH } }
end
theorem destruct_eq_think {s : computation α} {s'} :
destruct s = sum.inr s' → s = think s' :=
begin
dsimp [destruct],
induction f0 : s.1 0 with a'; intro h,
{ injection h with h', rw ←h',
cases s with f al,
apply subtype.eq, dsimp [think, tail],
rw ←f0, exact (stream.eta f).symm },
{ contradiction }
end
@[simp] theorem destruct_ret (a : α) : destruct (return a) = sum.inl a := rfl
@[simp] theorem destruct_think : ∀ s : computation α, destruct (think s) = sum.inr s
| ⟨f, al⟩ := rfl
@[simp] theorem destruct_empty : destruct (empty α) = sum.inr (empty α) := rfl
@[simp] theorem head_ret (a : α) : head (return a) = some a := rfl
@[simp] theorem head_think (s : computation α) : head (think s) = none := rfl
@[simp] theorem head_empty : head (empty α) = none := rfl
@[simp] theorem tail_ret (a : α) : tail (return a) = return a := rfl
@[simp] theorem tail_think (s : computation α) : tail (think s) = s :=
by cases s with f al; apply subtype.eq; dsimp [tail, think]; rw [stream.tail_cons]
@[simp] theorem tail_empty : tail (empty α) = empty α := rfl
theorem think_empty : empty α = think (empty α) :=
destruct_eq_think destruct_empty
def cases_on {C : computation α → Sort v} (s : computation α)
(h1 : ∀ a, C (return a)) (h2 : ∀ s, C (think s)) : C s := begin
induction H : destruct s with v v,
{ rw destruct_eq_ret H, apply h1 },
{ cases v with a s', rw destruct_eq_think H, apply h2 }
end
def corec.F (f : β → α ⊕ β) : α ⊕ β → option α × (α ⊕ β)
| (sum.inl a) := (some a, sum.inl a)
| (sum.inr b) := (match f b with
| sum.inl a := some a
| sum.inr b' := none
end, f b)
/-- `corec f b` is the corecursor for `computation α` as a coinductive type.
If `f b = inl a` then `corec f b = return a`, and if `f b = inl b'` then
`corec f b = think (corec f b')`. -/
def corec (f : β → α ⊕ β) (b : β) : computation α :=
begin
refine ⟨stream.corec' (corec.F f) (sum.inr b), λn a' h, _⟩,
rw stream.corec'_eq,
change stream.corec' (corec.F f) (corec.F f (sum.inr b)).2 n = some a',
revert h, generalize : sum.inr b = o, revert o,
induction n with n IH; intro o,
{ change (corec.F f o).1 = some a' → (corec.F f (corec.F f o).2).1 = some a',
cases o with a b; intro h, { exact h },
dsimp [corec.F] at h, dsimp [corec.F],
cases f b with a b', { exact h },
{ 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
/-- left map of `⊕` -/
def lmap (f : α → β) : α ⊕ γ → β ⊕ γ
| (sum.inl a) := sum.inl (f a)
| (sum.inr b) := sum.inr b
/-- right map of `⊕` -/
def rmap (f : β → γ) : α ⊕ β → α ⊕ γ
| (sum.inl a) := sum.inl a
| (sum.inr b) := sum.inr (f b)
attribute [simp] lmap rmap
@[simp] def corec_eq (f : β → α ⊕ β) (b : β) :
destruct (corec f b) = rmap (corec f) (f b) :=
begin
dsimp [corec, destruct],
change stream.corec' (corec.F f) (sum.inr b) 0 with corec.F._match_1 (f b),
induction h : f b with a b', { refl },
dsimp [corec.F, destruct],
apply congr_arg, apply subtype.eq,
dsimp [corec, tail],
rw [stream.corec'_eq, stream.tail_cons],
dsimp [corec.F], rw h
end
section bisim
variable (R : computation α → computation α → Prop)
local infix ~ := R
def bisim_o : α ⊕ computation α → α ⊕ computation α → Prop
| (sum.inl a) (sum.inl a') := a = a'
| (sum.inr s) (sum.inr s') := R s s'
| _ _ := false
attribute [simp] bisim_o
def is_bisimulation := ∀ ⦃s₁ s₂⦄, s₁ ~ s₂ → bisim_o R (destruct s₁) (destruct s₂)
-- If two computations 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' : computation α, 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, dsimp at this, rw this, assumption },
{ rw [destruct_ret, destruct_think] at this,
exact false.elim this },
{ rw [destruct_ret, destruct_think] at this,
exact false.elim this },
{ simp at this, simp [*] }
end
end,
exact ⟨s₁, s₂, rfl, rfl, r⟩
end
end bisim
-- It's more of a stretch to use ∈ for this relation, but it
-- asserts that the computation limits to the given value.
protected def mem (a : α) (s : computation α) := some a ∈ s.1
instance : has_mem α (computation α) := ⟨computation.mem⟩
theorem le_stable (s : computation α) {a m n} (h : m ≤ n) :
s.1 m = some a → s.1 n = some a :=
by {cases s with f al, induction h with n h IH, exacts [id, λ h2, al (IH h2)]}
theorem mem_unique :
relator.left_unique ((∈) : α → computation α → Prop) :=
λa s b ⟨m, ha⟩ ⟨n, hb⟩, by injection
(le_stable s (le_max_left m n) ha.symm).symm.trans
(le_stable s (le_max_right m n) hb.symm)
/-- `terminates s` asserts that the computation `s` eventually terminates with some value. -/
@[class] def terminates (s : computation α) : Prop := ∃ a, a ∈ s
theorem terminates_of_mem {s : computation α} {a : α} : a ∈ s → terminates s :=
exists.intro a
theorem terminates_def (s : computation α) : terminates s ↔ ∃ n, (s.1 n).is_some :=
⟨λ⟨a, n, h⟩, ⟨n, by {dsimp [stream.nth] at h, rw ←h, exact rfl}⟩,
λ⟨n, h⟩, ⟨option.get h, n, (option.eq_some_of_is_some h).symm⟩⟩
theorem ret_mem (a : α) : a ∈ return a :=
exists.intro 0 rfl
theorem eq_of_ret_mem {a a' : α} (h : a' ∈ return a) : a' = a :=
mem_unique h (ret_mem _)
instance ret_terminates (a : α) : terminates (return a) :=
terminates_of_mem (ret_mem _)
theorem think_mem {s : computation α} {a} : a ∈ s → a ∈ think s
| ⟨n, h⟩ := ⟨n+1, h⟩
instance think_terminates (s : computation α) :
∀ [terminates s], terminates (think s)
| ⟨a, n, h⟩ := ⟨a, n+1, h⟩
theorem of_think_mem {s : computation α} {a} : a ∈ think s → a ∈ s
| ⟨n, h⟩ := by {cases n with n', contradiction, exact ⟨n', h⟩}
theorem of_think_terminates {s : computation α} :
terminates (think s) → terminates s
| ⟨a, h⟩ := ⟨a, of_think_mem h⟩
theorem not_mem_empty (a : α) : a ∉ empty α :=
λ ⟨n, h⟩, by clear _fun_match; contradiction
theorem not_terminates_empty : ¬ terminates (empty α) :=
λ ⟨a, h⟩, not_mem_empty a h
theorem eq_empty_of_not_terminates {s} (H : ¬ terminates s) : s = empty α :=
begin
apply subtype.eq, funext n,
induction h : s.val n, {refl},
refine absurd _ H, exact ⟨_, _, h.symm⟩
end
theorem thinkN_mem {s : computation α} {a} : ∀ n, a ∈ thinkN s n ↔ a ∈ s
| 0 := iff.rfl
| (n+1) := iff.trans ⟨of_think_mem, think_mem⟩ (thinkN_mem n)
instance thinkN_terminates (s : computation α) :
∀ [terminates s] n, terminates (thinkN s n)
| ⟨a, h⟩ n := ⟨a, (thinkN_mem n).2 h⟩
theorem of_thinkN_terminates (s : computation α) (n) :
terminates (thinkN s n) → terminates s
| ⟨a, h⟩ := ⟨a, (thinkN_mem _).1 h⟩
/-- `promises s a`, or `s ~> a`, asserts that although the computation `s`
may not terminate, if it does, then the result is `a`. -/
def promises (s : computation α) (a : α) : Prop := ∀ ⦃a'⦄, a' ∈ s → a = a'
infix ` ~> `:50 := promises
theorem mem_promises {s : computation α} {a : α} : a ∈ s → s ~> a :=
λ h a', mem_unique h
theorem empty_promises (a : α) : empty α ~> a :=
λ a' h, absurd h (not_mem_empty _)
section get
variables (s : computation α) [h : terminates s]
include s h
/-- `length s` gets the number of steps of a terminating computation -/
def length : ℕ := nat.find ((terminates_def _).1 h)
/-- `get s` returns the result of a terminating computation -/
def get : α := option.get (nat.find_spec $ (terminates_def _).1 h)
theorem get_mem : get s ∈ s :=
exists.intro (length s) (option.eq_some_of_is_some _).symm
theorem get_eq_of_mem {a} : a ∈ s → get s = a :=
mem_unique (get_mem _)
theorem mem_of_get_eq {a} : get s = a → a ∈ s :=
by intro h; rw ←h; apply get_mem
@[simp] theorem get_think : get (think s) = get s :=
get_eq_of_mem _ $ let ⟨n, h⟩ := get_mem s in ⟨n+1, h⟩
@[simp] theorem get_thinkN (n) : get (thinkN s n) = get s :=
get_eq_of_mem _ $ (thinkN_mem _).2 (get_mem _)
theorem get_promises : s ~> get s := λ a, get_eq_of_mem _
theorem mem_of_promises {a} (p : s ~> a) : a ∈ s :=
by unfreezeI; cases h with a' h; rw p h; exact h
theorem get_eq_of_promises {a} : s ~> a → get s = a :=
get_eq_of_mem _ ∘ mem_of_promises _
end get
/-- `results s a n` completely characterizes a terminating computation:
it asserts that `s` terminates after exactly `n` steps, with result `a`. -/
def results (s : computation α) (a : α) (n : ℕ) :=
∃ (h : a ∈ s), @length _ s (terminates_of_mem h) = n
theorem results_of_terminates (s : computation α) [T : terminates s] :
results s (get s) (length s) :=
⟨get_mem _, rfl⟩
theorem results_of_terminates' (s : computation α) [T : terminates s] {a} (h : a ∈ s) :
results s a (length s) :=
by rw ←get_eq_of_mem _ h; apply results_of_terminates
theorem results.mem {s : computation α} {a n} : results s a n → a ∈ s
| ⟨m, _⟩ := m
theorem results.terminates {s : computation α} {a n} (h : results s a n) : terminates s :=
terminates_of_mem h.mem
theorem results.length {s : computation α} {a n} [T : terminates s] :
results s a n → length s = n
| ⟨_, h⟩ := h
theorem results.val_unique {s : computation α} {a b m n}
(h1 : results s a m) (h2 : results s b n) : a = b :=
mem_unique h1.mem h2.mem
theorem results.len_unique {s : computation α} {a b m n}
(h1 : results s a m) (h2 : results s b n) : m = n :=
by haveI := h1.terminates; haveI := h2.terminates; rw [←h1.length, h2.length]
theorem exists_results_of_mem {s : computation α} {a} (h : a ∈ s) : ∃ n, results s a n :=
by haveI := terminates_of_mem h; exact ⟨_, results_of_terminates' s h⟩
@[simp] theorem get_ret (a : α) : get (return a) = a :=
get_eq_of_mem _ ⟨0, rfl⟩
@[simp] theorem length_ret (a : α) : length (return a) = 0 :=
let h := computation.ret_terminates a in
nat.eq_zero_of_le_zero $ nat.find_min' ((terminates_def (return a)).1 h) rfl
theorem results_ret (a : α) : results (return a) a 0 :=
⟨_, length_ret _⟩
@[simp] theorem length_think (s : computation α) [h : terminates s] :
length (think s) = length s + 1 :=
begin
apply le_antisymm,
{ exact nat.find_min' _ (nat.find_spec ((terminates_def _).1 h)) },
{ have : (option.is_some ((think s).val (length (think s))) : Prop) :=
nat.find_spec ((terminates_def _).1 s.think_terminates),
cases length (think s) with n,
{ contradiction },
{ apply nat.succ_le_succ, apply nat.find_min', apply this } }
end
theorem results_think {s : computation α} {a n}
(h : results s a n) : results (think s) a (n + 1) :=
by haveI := h.terminates; exact ⟨think_mem h.mem, by rw [length_think, h.length]⟩
theorem of_results_think {s : computation α} {a n}
(h : results (think s) a n) : ∃ m, results s a m ∧ n = m + 1 :=
begin
haveI := of_think_terminates h.terminates,
have := results_of_terminates' _ (of_think_mem h.mem),
exact ⟨_, this, results.len_unique h (results_think this)⟩,
end
@[simp] theorem results_think_iff {s : computation α} {a n} :
results (think s) a (n + 1) ↔ results s a n :=
⟨λ h, let ⟨n', r, e⟩ := of_results_think h in by injection e with h'; rwa h',
results_think⟩
theorem results_thinkN {s : computation α} {a m} :
∀ n, results s a m → results (thinkN s n) a (m + n)
| 0 h := h
| (n+1) h := results_think (results_thinkN n h)
theorem results_thinkN_ret (a : α) (n) : results (thinkN (return a) n) a n :=
by have := results_thinkN n (results_ret a); rwa zero_add at this
@[simp] theorem length_thinkN (s : computation α) [h : terminates s] (n) :
length (thinkN s n) = length s + n :=
(results_thinkN n (results_of_terminates _)).length
theorem eq_thinkN {s : computation α} {a n} (h : results s a n) :
s = thinkN (return a) n :=
begin
revert s,
induction n with n IH; intro s;
apply cases_on s (λ a', _) (λ s, _); intro h,
{ rw ←eq_of_ret_mem h.mem, refl },
{ cases of_results_think h with n h, cases h, contradiction },
{ have := h.len_unique (results_ret _), contradiction },
{ rw IH (results_think_iff.1 h), refl }
end
theorem eq_thinkN' (s : computation α) [h : terminates s] :
s = thinkN (return (get s)) (length s) :=
eq_thinkN (results_of_terminates _)
def mem_rec_on {C : computation α → Sort v} {a s} (M : a ∈ s)
(h1 : C (return a)) (h2 : ∀ s, C s → C (think s)) : C s :=
begin
haveI T := terminates_of_mem M,
rw [eq_thinkN' s, get_eq_of_mem s M],
generalize : length s = n,
induction n with n IH, exacts [h1, h2 _ IH]
end
def terminates_rec_on {C : computation α → Sort v} (s) [terminates s]
(h1 : ∀ a, C (return a)) (h2 : ∀ s, C s → C (think s)) : C s :=
mem_rec_on (get_mem s) (h1 _) h2
/-- Map a function on the result of a computation. -/
def map (f : α → β) : computation α → computation β
| ⟨s, al⟩ := ⟨s.map (λo, option.cases_on o none (some ∘ f)),
λn b, begin
dsimp [stream.map, stream.nth],
induction e : s n with a; intro h,
{ contradiction }, { rw [al e, ←h] }
end⟩
def bind.G : β ⊕ computation β → β ⊕ computation α ⊕ computation β
| (sum.inl b) := sum.inl b
| (sum.inr cb') := sum.inr $ sum.inr cb'
def bind.F (f : α → computation β) :
computation α ⊕ computation β → β ⊕ computation α ⊕ computation β
| (sum.inl ca) :=
match destruct ca with
| sum.inl a := bind.G $ destruct (f a)
| sum.inr ca' := sum.inr $ sum.inl ca'
end
| (sum.inr cb) := bind.G $ destruct cb
/-- Compose two computations into a monadic `bind` operation. -/
def bind (c : computation α) (f : α → computation β) : computation β :=
corec (bind.F f) (sum.inl c)
instance : has_bind computation := ⟨@bind⟩
theorem has_bind_eq_bind {β} (c : computation α) (f : α → computation β) :
c >>= f = bind c f := rfl
/-- Flatten a computation of computations into a single computation. -/
def join (c : computation (computation α)) : computation α := c >>= id
@[simp] theorem map_ret (f : α → β) (a) : map f (return a) = return (f a) := rfl
@[simp] theorem map_think (f : α → β) : ∀ s, map f (think s) = think (map f s)
| ⟨s, al⟩ := by apply subtype.eq; dsimp [think, map]; rw stream.map_cons
@[simp] theorem destruct_map (f : α → β) (s) : destruct (map f s) = lmap f (rmap (map f) (destruct s)) :=
by apply s.cases_on; intro; simp
@[simp] theorem map_id : ∀ (s : computation α), map id s = s
| ⟨f, al⟩ := begin
apply subtype.eq; simp [map, function.comp],
have e : (@option.rec α (λ_, option α) none some) = id,
{ funext x, cases x; refl },
simp [e, stream.map_id]
end
theorem map_comp (f : α → β) (g : β → γ) :
∀ (s : computation α), 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 ret_bind (a) (f : α → computation β) :
bind (return a) f = f a :=
begin
apply eq_of_bisim (λc₁ c₂,
c₁ = bind (return a) f ∧ c₂ = f a ∨
c₁ = corec (bind.F f) (sum.inr c₂)),
{ intros c₁ c₂ h,
exact match c₁, c₂, h with
| ._, ._, or.inl ⟨rfl, rfl⟩ := begin
simp [bind, bind.F],
cases destruct (f a) with b cb; simp [bind.G]
end
| ._, c, or.inr rfl := begin
simp [bind.F],
cases destruct c with b cb; simp [bind.G]
end end },
{ simp }
end
@[simp] theorem think_bind (c) (f : α → computation β) :
bind (think c) f = think (bind c f) :=
destruct_eq_think $ by simp [bind, bind.F]
@[simp] theorem bind_ret (f : α → β) (s) : bind s (return ∘ f) = map f s :=
begin
apply eq_of_bisim (λc₁ c₂, c₁ = c₂ ∨
∃ s, c₁ = bind s (return ∘ f) ∧ c₂ = map f s),
{ intros c₁ c₂ h,
exact match c₁, c₂, h with
| _, _, or.inl (eq.refl c) := begin cases destruct c with b cb; simp end
| _, _, or.inr ⟨s, rfl, rfl⟩ := begin
apply cases_on s; intros s; simp,
exact or.inr ⟨s, rfl, rfl⟩
end end },
{ exact or.inr ⟨s, rfl, rfl⟩ }
end
@[simp] theorem bind_ret' (s : computation α) : bind s return = s :=
by rw bind_ret; change (λ x : α, x) with @id α; rw map_id
@[simp] theorem bind_assoc (s : computation α) (f : α → computation β) (g : β → computation γ) :
bind (bind s f) g = bind s (λ (x : α), bind (f x) g) :=
begin
apply eq_of_bisim (λc₁ c₂, c₁ = c₂ ∨
∃ s, c₁ = bind (bind s f) g ∧ c₂ = bind s (λ (x : α), bind (f x) g)),
{ intros c₁ c₂ h,
exact match c₁, c₂, h with
| _, _, or.inl (eq.refl c) := by cases destruct c with b cb; simp
| ._, ._, or.inr ⟨s, rfl, rfl⟩ := begin
apply cases_on s; intros s; simp,
{ generalize : f s = fs,
apply cases_on fs; intros t; simp,
{ cases destruct (g t) with b cb; simp } },
{ exact or.inr ⟨s, rfl, rfl⟩ }
end end },
{ exact or.inr ⟨s, rfl, rfl⟩ }
end
theorem results_bind {s : computation α} {f : α → computation β} {a b m n}
(h1 : results s a m) (h2 : results (f a) b n) : results (bind s f) b (n + m) :=
begin
have := h1.mem, revert m,
apply mem_rec_on this _ (λ s IH, _); intros m h1,
{ rw [ret_bind], rw h1.len_unique (results_ret _), exact h2 },
{ rw [think_bind], cases of_results_think h1 with m' h, cases h with h1 e,
rw e, exact results_think (IH h1) }
end
theorem mem_bind {s : computation α} {f : α → computation β} {a b}
(h1 : a ∈ s) (h2 : b ∈ f a) : b ∈ bind s f :=
let ⟨m, h1⟩ := exists_results_of_mem h1,
⟨n, h2⟩ := exists_results_of_mem h2 in (results_bind h1 h2).mem
instance terminates_bind (s : computation α) (f : α → computation β)
[terminates s] [terminates (f (get s))] :
terminates (bind s f) :=
terminates_of_mem (mem_bind (get_mem s) (get_mem (f (get s))))
@[simp] theorem get_bind (s : computation α) (f : α → computation β)
[terminates s] [terminates (f (get s))] :
get (bind s f) = get (f (get s)) :=
get_eq_of_mem _ (mem_bind (get_mem s) (get_mem (f (get s))))
@[simp] theorem length_bind (s : computation α) (f : α → computation β)
[T1 : terminates s] [T2 : terminates (f (get s))] :
length (bind s f) = length (f (get s)) + length s :=
(results_of_terminates _).len_unique $
results_bind (results_of_terminates _) (results_of_terminates _)
theorem of_results_bind {s : computation α} {f : α → computation β} {b k} :
results (bind s f) b k →
∃ a m n, results s a m ∧ results (f a) b n ∧ k = n + m :=
begin
induction k with n IH generalizing s;
apply cases_on s (λ a, _) (λ s', _); intro e,
{ simp [thinkN] at e, refine ⟨a, _, _, results_ret _, e, rfl⟩ },
{ have := congr_arg head (eq_thinkN e), contradiction },
{ simp at e, refine ⟨a, _, n+1, results_ret _, e, rfl⟩ },
{ simp at e, exact let ⟨a, m, n', h1, h2, e'⟩ := IH e in
by rw e'; exact ⟨a, m.succ, n', results_think h1, h2, rfl⟩ }
end
theorem exists_of_mem_bind {s : computation α} {f : α → computation β} {b}
(h : b ∈ bind s f) : ∃ a ∈ s, b ∈ f a :=
let ⟨k, h⟩ := exists_results_of_mem h,
⟨a, m, n, h1, h2, e⟩ := of_results_bind h in ⟨a, h1.mem, h2.mem⟩
theorem bind_promises {s : computation α} {f : α → computation β} {a b}
(h1 : s ~> a) (h2 : f a ~> b) : bind s f ~> b :=
λ b' bB, begin
rcases exists_of_mem_bind bB with ⟨a', a's, ba'⟩,
rw ←h1 a's at ba', exact h2 ba'
end
instance : monad computation :=
{ map := @map,
pure := @return,
bind := @bind }
instance : is_lawful_monad computation :=
{ id_map := @map_id,
bind_pure_comp_eq_map := @bind_ret,
pure_bind := @ret_bind,
bind_assoc := @bind_assoc }
theorem has_map_eq_map {β} (f : α → β) (c : computation α) : f <$> c = map f c := rfl
@[simp] theorem return_def (a) : (_root_.return a : computation α) = return a := rfl
@[simp] theorem map_ret' {α β} : ∀ (f : α → β) (a), f <$> return a = return (f a) := map_ret
@[simp] theorem map_think' {α β} : ∀ (f : α → β) s, f <$> think s = think (f <$> s) := map_think
theorem mem_map (f : α → β) {a} {s : computation α} (m : a ∈ s) : f a ∈ map f s :=
by rw ←bind_ret; apply mem_bind m; apply ret_mem
theorem exists_of_mem_map {f : α → β} {b : β} {s : computation α} (h : b ∈ map f s) :
∃ a, a ∈ s ∧ f a = b :=
by rw ←bind_ret at h; exact
let ⟨a, as, fb⟩ := exists_of_mem_bind h in ⟨a, as, mem_unique (ret_mem _) fb⟩
instance terminates_map (f : α → β) (s : computation α) [terminates s] : terminates (map f s) :=
by rw ←bind_ret; apply_instance
theorem terminates_map_iff (f : α → β) (s : computation α) :
terminates (map f s) ↔ terminates s :=
⟨λ⟨a, h⟩, let ⟨b, h1, _⟩ := exists_of_mem_map h in ⟨_, h1⟩, @computation.terminates_map _ _ _ _⟩
-- Parallel computation
/-- `c₁ <|> c₂` calculates `c₁` and `c₂` simultaneously, returning
the first one that gives a result. -/
def orelse (c₁ c₂ : computation α) : computation α :=
@computation.corec α (computation α × computation α)
(λ⟨c₁, c₂⟩, match destruct c₁ with
| sum.inl a := sum.inl a
| sum.inr c₁' := match destruct c₂ with
| sum.inl a := sum.inl a
| sum.inr c₂' := sum.inr (c₁', c₂')
end
end) (c₁, c₂)
instance : alternative computation :=
{ orelse := @orelse, failure := @empty, ..computation.monad }
@[simp] theorem ret_orelse (a : α) (c₂ : computation α) :
(return a <|> c₂) = return a :=
destruct_eq_ret $ by unfold has_orelse.orelse; simp [orelse]
@[simp] theorem orelse_ret (c₁ : computation α) (a : α) :
(think c₁ <|> return a) = return a :=
destruct_eq_ret $ by unfold has_orelse.orelse; simp [orelse]
@[simp] theorem orelse_think (c₁ c₂ : computation α) :
(think c₁ <|> think c₂) = think (c₁ <|> c₂) :=
destruct_eq_think $ by unfold has_orelse.orelse; simp [orelse]
@[simp] theorem empty_orelse (c) : (empty α <|> c) = c :=
begin
apply eq_of_bisim (λc₁ c₂, (empty α <|> c₂) = c₁) _ rfl,
intros s' s h, rw ←h,
apply cases_on s; intros s; rw think_empty; simp,
rw ←think_empty,
end
@[simp] theorem orelse_empty (c : computation α) : (c <|> empty α) = c :=
begin
apply eq_of_bisim (λc₁ c₂, (c₂ <|> empty α) = c₁) _ rfl,
intros s' s h, rw ←h,
apply cases_on s; intros s; rw think_empty; simp,
rw←think_empty,
end
/-- `c₁ ~ c₂` asserts that `c₁` and `c₂` either both terminate with the same result,
or both loop forever. -/
def equiv (c₁ c₂ : computation α) : Prop := ∀ a, a ∈ c₁ ↔ a ∈ c₂
infix ~ := equiv
@[refl] theorem equiv.refl (s : computation α) : s ~ s := λ_, iff.rfl
@[symm] theorem equiv.symm {s t : computation α} : s ~ t → t ~ s :=
λh a, (h a).symm
@[trans] theorem equiv.trans {s t u : computation α} : s ~ t → t ~ u → s ~ u :=
λh1 h2 a, (h1 a).trans (h2 a)
theorem equiv.equivalence : equivalence (@equiv α) :=
⟨@equiv.refl _, @equiv.symm _, @equiv.trans _⟩
theorem equiv_of_mem {s t : computation α} {a} (h1 : a ∈ s) (h2 : a ∈ t) : s ~ t :=
λa', ⟨λma, by rw mem_unique ma h1; exact h2,
λma, by rw mem_unique ma h2; exact h1⟩
theorem terminates_congr {c₁ c₂ : computation α}
(h : c₁ ~ c₂) : terminates c₁ ↔ terminates c₂ :=
exists_congr h
theorem promises_congr {c₁ c₂ : computation α}
(h : c₁ ~ c₂) (a) : c₁ ~> a ↔ c₂ ~> a :=
forall_congr (λa', imp_congr (h a') iff.rfl)
theorem get_equiv {c₁ c₂ : computation α} (h : c₁ ~ c₂)
[terminates c₁] [terminates c₂] : get c₁ = get c₂ :=
get_eq_of_mem _ $ (h _).2 $ get_mem _
theorem think_equiv (s : computation α) : think s ~ s :=
λ a, ⟨of_think_mem, think_mem⟩
theorem thinkN_equiv (s : computation α) (n) : thinkN s n ~ s :=
λ a, thinkN_mem n
theorem bind_congr {s1 s2 : computation α} {f1 f2 : α → computation β}
(h1 : s1 ~ s2) (h2 : ∀ a, f1 a ~ f2 a) : bind s1 f1 ~ bind s2 f2 :=
λ b, ⟨λh, let ⟨a, ha, hb⟩ := exists_of_mem_bind h in
mem_bind ((h1 a).1 ha) ((h2 a b).1 hb),
λh, let ⟨a, ha, hb⟩ := exists_of_mem_bind h in
mem_bind ((h1 a).2 ha) ((h2 a b).2 hb)⟩
theorem equiv_ret_of_mem {s : computation α} {a} (h : a ∈ s) : s ~ return a :=
equiv_of_mem h (ret_mem _)
/-- `lift_rel R ca cb` is a generalization of `equiv` to relations other than
equality. It asserts that if `ca` terminates with `a`, then `cb` terminates with
some `b` such that `R a b`, and if `cb` terminates with b` then `ca` terminates
with some `a` such that `R a b`. -/
def lift_rel (R : α → β → Prop) (ca : computation α) (cb : computation β) : Prop :=
(∀ {a}, a ∈ ca → ∃ {b}, b ∈ cb ∧ R a b) ∧
∀ {b}, b ∈ cb → ∃ {a}, a ∈ ca ∧ R a b
theorem lift_rel.swap (R : α → β → Prop) (ca : computation α) (cb : computation β) :
lift_rel (function.swap R) cb ca ↔ lift_rel R ca cb :=
and_comm _ _
theorem lift_eq_iff_equiv (c₁ c₂ : computation α) : lift_rel (=) c₁ c₂ ↔ c₁ ~ c₂ :=
⟨λ⟨h1, h2⟩ a,
⟨λ a1, let ⟨b, b2, ab⟩ := h1 a1 in by rwa ab,
λ a2, let ⟨b, b1, ab⟩ := h2 a2 in by rwa ←ab⟩,
λe, ⟨λ a a1, ⟨a, (e _).1 a1, rfl⟩, λ a a2, ⟨a, (e _).2 a2, rfl⟩⟩⟩
theorem lift_rel.refl (R : α → α → Prop) (H : reflexive R) : reflexive (lift_rel R) :=
λ s, ⟨λ a as, ⟨a, as, H a⟩, λ b bs, ⟨b, bs, H b⟩⟩
theorem lift_rel.symm (R : α → α → Prop) (H : symmetric R) : symmetric (lift_rel R) :=
λ s1 s2 ⟨l, r⟩,
⟨λ a a2, let ⟨b, b1, ab⟩ := r a2 in ⟨b, b1, H ab⟩,
λ a a1, let ⟨b, b2, ab⟩ := l a1 in ⟨b, b2, H ab⟩⟩
theorem lift_rel.trans (R : α → α → Prop) (H : transitive R) : transitive (lift_rel R) :=
λ s1 s2 s3 ⟨l1, r1⟩ ⟨l2, r2⟩,
⟨λ a a1, let ⟨b, b2, ab⟩ := l1 a1, ⟨c, c3, bc⟩ := l2 b2 in ⟨c, c3, H ab bc⟩,
λ c c3, let ⟨b, b2, bc⟩ := r2 c3, ⟨a, a1, ab⟩ := r1 b2 in ⟨a, a1, H ab bc⟩⟩
theorem lift_rel.equiv (R : α → α → Prop) : equivalence R → equivalence (lift_rel R)
| ⟨refl, symm, trans⟩ :=
⟨lift_rel.refl R refl, lift_rel.symm R symm, lift_rel.trans R trans⟩
theorem lift_rel.imp {R S : α → β → Prop} (H : ∀ {a b}, R a b → S a b) (s t) :
lift_rel R s t → lift_rel S s t | ⟨l, r⟩ :=
⟨λ a as, let ⟨b, bt, ab⟩ := l as in ⟨b, bt, H ab⟩,
λ b bt, let ⟨a, as, ab⟩ := r bt in ⟨a, as, H ab⟩⟩
theorem terminates_of_lift_rel {R : α → β → Prop} {s t} :
lift_rel R s t → (terminates s ↔ terminates t) | ⟨l, r⟩ :=
⟨λ ⟨a, as⟩, let ⟨b, bt, ab⟩ := l as in ⟨b, bt⟩,
λ ⟨b, bt⟩, let ⟨a, as, ab⟩ := r bt in ⟨a, as⟩⟩
theorem rel_of_lift_rel {R : α → β → Prop} {ca cb} :
lift_rel R ca cb → ∀ {a b}, a ∈ ca → b ∈ cb → R a b
| ⟨l, r⟩ a b ma mb :=
let ⟨b', mb', ab'⟩ := l ma in by rw mem_unique mb mb'; exact ab'
theorem lift_rel_of_mem {R : α → β → Prop} {a b ca cb}
(ma : a ∈ ca) (mb : b ∈ cb) (ab : R a b) : lift_rel R ca cb :=
⟨λ a' ma', by rw mem_unique ma' ma; exact ⟨b, mb, ab⟩,
λ b' mb', by rw mem_unique mb' mb; exact ⟨a, ma, ab⟩⟩
theorem exists_of_lift_rel_left {R : α → β → Prop} {ca cb}
(H : lift_rel R ca cb) {a} (h : a ∈ ca) : ∃ {b}, b ∈ cb ∧ R a b :=
H.left h
theorem exists_of_lift_rel_right {R : α → β → Prop} {ca cb}
(H : lift_rel R ca cb) {b} (h : b ∈ cb) : ∃ {a}, a ∈ ca ∧ R a b :=
H.right h
theorem lift_rel_def {R : α → β → Prop} {ca cb} : lift_rel R ca cb ↔
(terminates ca ↔ terminates cb) ∧ ∀ {a b}, a ∈ ca → b ∈ cb → R a b :=
⟨λh, ⟨terminates_of_lift_rel h, λ a b ma mb,
let ⟨b', mb', ab⟩ := h.left ma in by rwa mem_unique mb mb'⟩,
λ⟨l, r⟩,
⟨λ a ma, let ⟨b, mb⟩ := l.1 ⟨_, ma⟩ in ⟨b, mb, r ma mb⟩,
λ b mb, let ⟨a, ma⟩ := l.2 ⟨_, mb⟩ in ⟨a, ma, r ma mb⟩⟩⟩
theorem lift_rel_bind {δ} (R : α → β → Prop) (S : γ → δ → Prop)
{s1 : computation α} {s2 : computation β}
{f1 : α → computation γ} {f2 : β → computation δ}
(h1 : lift_rel R s1 s2) (h2 : ∀ {a b}, R a b → lift_rel S (f1 a) (f2 b))
: lift_rel S (bind s1 f1) (bind s2 f2) :=
let ⟨l1, r1⟩ := h1 in
⟨λ c cB,
let ⟨a, a1, c₁⟩ := exists_of_mem_bind cB,
⟨b, b2, ab⟩ := l1 a1,
⟨l2, r2⟩ := h2 ab,
⟨d, d2, cd⟩ := l2 c₁ in
⟨_, mem_bind b2 d2, cd⟩,
λ d dB,
let ⟨b, b1, d1⟩ := exists_of_mem_bind dB,
⟨a, a2, ab⟩ := r1 b1,
⟨l2, r2⟩ := h2 ab,
⟨c, c₂, cd⟩ := r2 d1 in
⟨_, mem_bind a2 c₂, cd⟩⟩
@[simp] theorem lift_rel_return_left (R : α → β → Prop) (a : α) (cb : computation β) :
lift_rel R (return a) cb ↔ ∃ {b}, b ∈ cb ∧ R a b :=
⟨λ⟨l, r⟩, l (ret_mem _),
λ⟨b, mb, ab⟩,
⟨λ a' ma', by rw eq_of_ret_mem ma'; exact ⟨b, mb, ab⟩,
λ b' mb', ⟨_, ret_mem _, by rw mem_unique mb' mb; exact ab⟩⟩⟩
@[simp] theorem lift_rel_return_right (R : α → β → Prop) (ca : computation α) (b : β) :
lift_rel R ca (return b) ↔ ∃ {a}, a ∈ ca ∧ R a b :=
by rw [lift_rel.swap, lift_rel_return_left]
@[simp] theorem lift_rel_return (R : α → β → Prop) (a : α) (b : β) :
lift_rel R (return a) (return b) ↔ R a b :=
by rw [lift_rel_return_left]; exact
⟨λ⟨b', mb', ab'⟩, by rwa eq_of_ret_mem mb' at ab',
λab, ⟨_, ret_mem _, ab⟩⟩
@[simp] theorem lift_rel_think_left (R : α → β → Prop) (ca : computation α) (cb : computation β) :
lift_rel R (think ca) cb ↔ lift_rel R ca cb :=
and_congr (forall_congr $ λb, imp_congr ⟨of_think_mem, think_mem⟩ iff.rfl)
(forall_congr $ λb, imp_congr iff.rfl $
exists_congr $ λ b, and_congr ⟨of_think_mem, think_mem⟩ iff.rfl)
@[simp] theorem lift_rel_think_right (R : α → β → Prop) (ca : computation α) (cb : computation β) :
lift_rel R ca (think cb) ↔ lift_rel R ca cb :=
by rw [←lift_rel.swap R, ←lift_rel.swap R]; apply lift_rel_think_left
theorem lift_rel_mem_cases {R : α → β → Prop} {ca cb}
(Ha : ∀ a ∈ ca, lift_rel R ca cb)
(Hb : ∀ b ∈ cb, lift_rel R ca cb) : lift_rel R ca cb :=
⟨λ a ma, (Ha _ ma).left ma, λ b mb, (Hb _ mb).right mb⟩
theorem lift_rel_congr {R : α → β → Prop} {ca ca' : computation α} {cb cb' : computation β}
(ha : ca ~ ca') (hb : cb ~ cb') : lift_rel R ca cb ↔ lift_rel R ca' cb' :=
and_congr
(forall_congr $ λ a, imp_congr (ha _) $ exists_congr $ λ b, and_congr (hb _) iff.rfl)
(forall_congr $ λ b, imp_congr (hb _) $ exists_congr $ λ a, and_congr (ha _) iff.rfl)
theorem lift_rel_map {δ} (R : α → β → Prop) (S : γ → δ → Prop)
{s1 : computation α} {s2 : computation β}
{f1 : α → γ} {f2 : β → δ}
(h1 : lift_rel R s1 s2) (h2 : ∀ {a b}, R a b → S (f1 a) (f2 b))
: lift_rel S (map f1 s1) (map f2 s2) :=
by rw [←bind_ret, ←bind_ret]; apply lift_rel_bind _ _ h1; simp; exact @h2
theorem map_congr (R : α → α → Prop) (S : β → β → Prop)
{s1 s2 : computation α} {f : α → β}
(h1 : s1 ~ s2) : map f s1 ~ map f s2 :=
by rw [←lift_eq_iff_equiv];
exact lift_rel_map eq _ ((lift_eq_iff_equiv _ _).2 h1) (λ a b, congr_arg _)
def lift_rel_aux (R : α → β → Prop)
(C : computation α → computation β → Prop) :
α ⊕ computation α → β ⊕ computation β → Prop
| (sum.inl a) (sum.inl b) := R a b
| (sum.inl a) (sum.inr cb) := ∃ {b}, b ∈ cb ∧ R a b
| (sum.inr ca) (sum.inl b) := ∃ {a}, a ∈ ca ∧ R a b
| (sum.inr ca) (sum.inr cb) := C ca cb
attribute [simp] lift_rel_aux
@[simp] def lift_rel_aux.ret_left (R : α → β → Prop)
(C : computation α → computation β → Prop) (a cb) :
lift_rel_aux R C (sum.inl a) (destruct cb) ↔ ∃ {b}, b ∈ cb ∧ R a b :=
begin
apply cb.cases_on (λ b, _) (λ cb, _),
{ exact ⟨λ h, ⟨_, ret_mem _, h⟩, λ ⟨b', mb, h⟩,
by rw [mem_unique (ret_mem _) mb]; exact h⟩ },
{ rw [destruct_think],
exact ⟨λ ⟨b, h, r⟩, ⟨b, think_mem h, r⟩,
λ ⟨b, h, r⟩, ⟨b, of_think_mem h, r⟩⟩ }
end
theorem lift_rel_aux.swap (R : α → β → Prop) (C) (a b) :
lift_rel_aux (function.swap R) (function.swap C) b a = lift_rel_aux R C a b :=
by cases a with a ca; cases b with b cb; simp only [lift_rel_aux]
@[simp] def lift_rel_aux.ret_right (R : α → β → Prop)
(C : computation α → computation β → Prop) (b ca) :
lift_rel_aux R C (destruct ca) (sum.inl b) ↔ ∃ {a}, a ∈ ca ∧ R a b :=
by rw [←lift_rel_aux.swap, lift_rel_aux.ret_left]
theorem lift_rel_rec.lem {R : α → β → Prop} (C : computation α → computation β → Prop)
(H : ∀ {ca cb}, C ca cb → lift_rel_aux R C (destruct ca) (destruct cb))
(ca cb) (Hc : C ca cb) (a) (ha : a ∈ ca) : lift_rel R ca cb :=
begin
revert cb, refine mem_rec_on ha _ (λ ca' IH, _);
intros cb Hc; have h := H Hc,
{ simp at h, simp [h] },
{ have h := H Hc, simp, revert h, apply cb.cases_on (λ b, _) (λ cb', _);
intro h; simp at h; simp [h], exact IH _ h }
end
theorem lift_rel_rec {R : α → β → Prop} (C : computation α → computation β → Prop)
(H : ∀ {ca cb}, C ca cb → lift_rel_aux R C (destruct ca) (destruct cb))
(ca cb) (Hc : C ca cb) : lift_rel R ca cb :=
lift_rel_mem_cases (lift_rel_rec.lem C @H ca cb Hc) (λ b hb,
(lift_rel.swap _ _ _).2 $
lift_rel_rec.lem (function.swap C)
(λ cb ca h, cast (lift_rel_aux.swap _ _ _ _).symm $ H h)
cb ca Hc b hb)
end computation
|
9772c0b467c91c03c6e037a3b024588c7a34cfc6 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/tactic/derive_fintype.lean | bffeae606ed1d3e065443eb8eba62eed9b250f3a | [
"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 | 15,294 | lean | /-
Copyright (c) 2020 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.fintype.basic
/-!
# Derive handler for `fintype` instances
This file introduces a derive handler to automatically generate `fintype`
instances for structures and inductives.
## Implementation notes
To construct a fintype instance, we need 3 things:
1. A list `l` of elements
2. A proof that `l` has no duplicates
3. A proof that every element in the type is in `l`
Now fintype is defined as a finset which enumerates all elements, so steps (1) and (2) are
bundled together. It is possible to use finset operations that remove duplicates to avoid the need
to prove (2), but this adds unnecessary functions to the constructed term, which makes it more
expensive to compute the list, and it also adds a dependence on decidable equality for the type,
which we want to avoid.
Because we will rely on fintype instances for constructor arguments, we can't actually build a list
directly, so (1) and (2) are necessarily somewhat intertwined. The inductive types we will be
proving instances for look something like this:
```
@[derive fintype]
inductive foo
| zero : foo
| one : bool → foo
| two : ∀ x : fin 3, bar x → foo
```
The list of elements that we generate is
```
{foo.zero}
∪ (finset.univ : bool).map (λ b, finset.one b)
∪ (finset.univ : Σ' x : fin 3, bar x).map (λ ⟨x, y⟩, finset.two x y)
```
except that instead of `∪`, that is `finset.union`, we use `finset.disj_union` which doesn't
require any deduplication, but does require a proof that the two parts of the union are disjoint.
We use `finset.cons` to append singletons like `foo.zero`.
The proofs of disjointness would be somewhat expensive since there are quadratically many of them,
so instead we use a "discriminant" function. Essentially, we define
```
def foo.enum : foo → ℕ
| foo.zero := 0
| (foo.one _) := 1
| (foo.two _ _) := 2
```
and now the existence of this function implies that foo.zero is not foo.two and so on because they
map to different natural numbers. We can prove that sets of natural numbers are mutually disjoint
more easily because they have a linear order: `0 < 1 < 2` so `0 ≠ 2`.
To package this argument up, we define `finset_above foo foo.enum n` to be a finset `s` together
with a proof that all elements `a ∈ s` have `n ≤ enum a`. Now we only have to prove that
`enum foo.zero = 0`, `enum (foo.one _) = 1`, etc. (linearly many proofs, all `rfl`) in order to
prove that all variants are mutually distinct.
We mirror the `finset.cons` and `finset.disj_union` functions into `finset_above.cons` and
`finset_above.union`, and this forms the main part of the finset construction.
This only handles distinguishing variants of a finset. Now we must enumerate the elements of a
variant, for example `{foo.one ff, foo.one tt}`, while at the same time proving that all these
elements have discriminant `1` in this case. To do that, we use the `finset_in` type, which
is a finset satisfying a property `P`, here `λ a, foo.enum a = 1`.
We could use `finset.bind` many times to construct the finset but it turns out to be somewhat
complicated to get good side goals for a naturally nodup version of `finset.bind` in the same way
as we did with `finset.cons` and `finset.union`. Instead, we tuple up all arguments into one type,
leveraging the `fintype` instance on `psigma`, and then define a map from this type to the
inductive type that untuples them and applies the constructor. The injectivity property of the
constructor ensures that this function is injective, so we can use `finset.map` to apply it. This
is the content of the constructor `finset_in.mk`.
That completes the proofs of (1) and (2). To prove (3), we perform one case analysis over the
inductive type, proving theorems like
```
foo.one a ∈ {foo.zero}
∪ (finset.univ : bool).map (λ b, finset.one b)
∪ (finset.univ : Σ' x : fin 3, bar x).map (λ ⟨x, y⟩, finset.two x y)
```
by seeking to the relevant disjunct and then supplying the constructor arguments. This part of the
proof is quadratic, but quite simple. (We could do it in `O(n log n)` if we used a balanced tree
for the unions.)
The tactics perform the following parts of this proof scheme:
* `mk_sigma` constructs the type `Γ` in `finset_in.mk`
* `mk_sigma_elim` constructs the function `f` in `finset_in.mk`
* `mk_sigma_elim_inj` proves that `f` is injective
* `mk_sigma_elim_eq` proves that `∀ a, enum (f a) = k`
* `mk_finset` constructs the finset `S = {foo.zero} ∪ ...` by recursion on the variants
* `mk_finset_total` constructs the proof `|- foo.zero ∈ S; |- foo.one a ∈ S; |- foo.two a b ∈ S`
by recursion on the subgoals coming out of the initial `cases`
* `mk_fintype_instance` puts it all together to produce a proof of `fintype foo`.
The construction of `foo.enum` is also done in this function.
-/
namespace derive_fintype
/-- A step in the construction of `finset.univ` for a finite inductive type.
We will set `enum` to the discriminant of the inductive type, so a `finset_above`
represents a finset that enumerates all elements in a tail of the constructor list. -/
def finset_above (α) (enum : α → ℕ) (n : ℕ) :=
{s : finset α // ∀ x ∈ s, n ≤ enum x}
/-- Construct a fintype instance from a completed `finset_above`. -/
def mk_fintype {α} (enum : α → ℕ) (s : finset_above α enum 0) (H : ∀ x, x ∈ s.1) :
fintype α := ⟨s.1, H⟩
/-- This is the case for a simple variant (no arguments) in an inductive type. -/
def finset_above.cons {α} {enum : α → ℕ} (n)
(a : α) (h : enum a = n) (s : finset_above α enum (n+1)) : finset_above α enum n :=
begin
refine ⟨finset.cons a s.1 _, _⟩,
{ intro h',
have := s.2 _ h', rw h at this,
exact nat.not_succ_le_self n this },
{ intros x h', rcases finset.mem_cons.1 h' with rfl | h',
{ exact ge_of_eq h },
{ exact nat.le_of_succ_le (s.2 _ h') } }
end
theorem finset_above.mem_cons_self {α} {enum : α → ℕ} {n a h s} :
a ∈ (@finset_above.cons α enum n a h s).1 := multiset.mem_cons_self _ _
theorem finset_above.mem_cons_of_mem {α} {enum : α → ℕ} {n a h s b} :
b ∈ (s : finset_above _ _ _).1 → b ∈ (@finset_above.cons α enum n a h s).1 :=
multiset.mem_cons_of_mem
/-- The base case is when we run out of variants; we just put an empty finset at the end. -/
def finset_above.nil {α} {enum : α → ℕ} (n) : finset_above α enum n := ⟨∅, by rintro _ ⟨⟩⟩
instance (α enum n) : inhabited (finset_above α enum n) := ⟨finset_above.nil _⟩
/-- This is a finset covering a nontrivial variant (with one or more constructor arguments).
The property `P` here is `λ a, enum a = n` where `n` is the discriminant for the current
variant. -/
@[nolint has_inhabited_instance]
def finset_in {α} (P : α → Prop) := {s : finset α // ∀ x ∈ s, P x}
/-- To construct the finset, we use an injective map from the type `Γ`, which will be the
sigma over all constructor arguments. We use sigma instances and existing fintype instances
to prove that `Γ` is a fintype, and construct the function `f` that maps `⟨a, b, c, ...⟩`
to `C_n a b c ...` where `C_n` is the nth constructor, and `mem` asserts
`enum (C_n a b c ...) = n`. -/
def finset_in.mk {α} {P : α → Prop} (Γ) [fintype Γ]
(f : Γ → α) (inj : function.injective f) (mem : ∀ x, P (f x)) : finset_in P :=
⟨finset.univ.map ⟨f, inj⟩,
λ x h, by rcases finset.mem_map.1 h with ⟨x, _, rfl⟩; exact mem x⟩
theorem finset_in.mem_mk {α} {P : α → Prop} {Γ} {s : fintype Γ} {f : Γ → α} {inj mem a}
(b) (H : f b = a) : a ∈ (@finset_in.mk α P Γ s f inj mem).1 :=
finset.mem_map.2 ⟨_, finset.mem_univ _, H⟩
/-- For nontrivial variants, we split the constructor list into a `finset_in` component for the
current constructor and a `finset_above` for the rest. -/
def finset_above.union {α} {enum : α → ℕ} (n)
(s : finset_in (λ a, enum a = n)) (t : finset_above α enum (n+1)) : finset_above α enum n :=
begin
refine ⟨finset.disj_union s.1 t.1 _, _⟩,
{ intros a hs ht,
have := t.2 _ ht, rw s.2 _ hs at this,
exact nat.not_succ_le_self n this },
{ intros x h', rcases finset.mem_disj_union.1 h' with h' | h',
{ exact ge_of_eq (s.2 _ h') },
{ exact nat.le_of_succ_le (t.2 _ h') } }
end
theorem finset_above.mem_union_left {α} {enum : α → ℕ} {n s t a}
(H : a ∈ (s : finset_in _).1) : a ∈ (@finset_above.union α enum n s t).1 :=
multiset.mem_add.2 (or.inl H)
theorem finset_above.mem_union_right {α} {enum : α → ℕ} {n s t a}
(H : a ∈ (t : finset_above _ _ _).1) : a ∈ (@finset_above.union α enum n s t).1 :=
multiset.mem_add.2 (or.inr H)
end derive_fintype
namespace tactic
open derive_fintype tactic expr
namespace derive_fintype
/-- Construct the term `Σ' (a:A) (b:B a) (c:C a b), unit` from
`Π (a:A) (b:B a), C a b → T` (the type of a constructor). -/
meta def mk_sigma : expr → tactic expr
| (expr.pi n bi d b) := do
p ← mk_local' n bi d,
e ← mk_sigma (expr.instantiate_var b p),
tactic.mk_app ``psigma [d, bind_lambda e p]
| _ := pure `(unit)
/-- Prove the goal `(Σ' (a:A) (b:B a) (c:C a b), unit) → T`
(this is the function `f` in `finset_in.mk`) using recursive `psigma.elim`,
finishing with the constructor. The two arguments are the type of the constructor,
and the constructor term itself; as we recurse we add arguments
to the constructor application and destructure the pi type of the constructor. We return the number
of `psigma.elim` applications constructed, which is the number of constructor arguments. -/
meta def mk_sigma_elim : expr → expr → tactic ℕ
| (expr.pi n bi d b) c := do
refine ``(@psigma.elim %%d _ _ _),
i ← intro_fresh n,
(+ 1) <$> mk_sigma_elim (expr.instantiate_var b i) (c i)
| _ c := do intro1, exact c $> 0
/-- Prove the goal `a, b |- f a = f b → g a = g b` where `f` is the function we constructed in
`mk_sigma_elim`, and `g` is some other term that gets built up and eventually closed by
reflexivity. Here `a` and `b` have sigma types so the proof approach is to case on `a` and `b`
until the goal reduces to `C_n a1 ... am = C_n b1 ... bm → ⟨a1, ..., am⟩ = ⟨b1, ..., bm⟩`, at which
point cases on the equality reduces the problem to reflexivity.
The arguments are the number `m` returned from `mk_sigma_elim`, and the hypotheses `a,b` that we
need to case on. -/
meta def mk_sigma_elim_inj : ℕ → expr → expr → tactic unit
| (m+1) x y := do
[(_, [x1, x2])] ← cases x,
[(_, [y1, y2])] ← cases y,
mk_sigma_elim_inj m x2 y2
| 0 x y := do
cases x, cases y,
is ← intro1 >>= injection,
is.mmap' cases,
reflexivity
/-- Prove the goal `a |- enum (f a) = n`, where `f` is the function constructed in `mk_sigma_elim`,
and `enum` is a function that reduces to `n` on the constructor `C_n`. Here we just have to case on
`a` `m` times, and then `reflexivity` finishes the proof. -/
meta def mk_sigma_elim_eq : ℕ → expr → tactic unit
| (n+1) x := do
[(_, [x1, x2])] ← cases x,
mk_sigma_elim_eq n x2
| 0 x := reflexivity
/-- Prove the goal `|- finset_above T enum k`, where `T` is the inductive type and `enum` is the
discriminant function. The arguments are `args`, the parameters to the inductive type (and all
constructors), `k`, the index of the current variant, and `cs`, the list of constructor names.
This uses `finset_above.cons` for basic variants and `finset_above.union` for variants with
arguments, using the auxiliary functions `mk_sigma`, `mk_sigma_elim`, `mk_sigma_elim_inj`,
`mk_sigma_elim_eq` to close subgoals. -/
meta def mk_finset (ls : list level) (args : list expr) : ℕ → list name → tactic unit
| k (c::cs) := do
let e := (expr.const c ls).mk_app args,
t ← infer_type e,
if is_pi t then do
to_expr ``(finset_above.union %%(reflect k)) tt ff >>=
(λ c, apply c {new_goals := new_goals.all}),
Γ ← mk_sigma t,
to_expr ``(finset_in.mk %%Γ) tt ff >>= (λ c, apply c {new_goals := new_goals.all}),
n ← mk_sigma_elim t e,
intro1 >>= (λ x, intro1 >>= mk_sigma_elim_inj n x),
intro1 >>= mk_sigma_elim_eq n,
mk_finset (k+1) cs
else do
c ← to_expr ``(finset_above.cons %%(reflect k) %%e) tt ff,
apply c {new_goals := new_goals.all}, reflexivity,
mk_finset (k+1) cs
| k [] := applyc ``finset_above.nil
/-- Prove the goal `|- Σ' (a:A) (b: B a) (c:C a b), unit` given a list of terms `a, b, c`. -/
meta def mk_sigma_mem : list expr → tactic unit
| (x::xs) := fconstructor >> exact x >> mk_sigma_mem xs
| [] := fconstructor $> ()
/-- This function is called to prove `a : T |- a ∈ S.1` where `S` is the `finset_above` constructed
by `mk_finset`, after the initial cases on `a : T`, producing a list of subgoals. For each case,
we have to navigate past all the variants that don't apply (which is what the `tac` input tactic
does), and then call either `finset_above.mem_cons_self` for trivial variants or
`finset_above.mem_union_left` and `finset_in.mem_mk` for nontrivial variants. Either way the proof
is quite simple. -/
meta def mk_finset_total : tactic unit → list (name × list expr) → tactic unit
| tac [] := done
| tac ((_, xs) :: gs) := do
tac,
b ← succeeds (applyc ``finset_above.mem_cons_self),
if b then
mk_finset_total (tac >> applyc ``finset_above.mem_cons_of_mem) gs
else do
applyc ``finset_above.mem_union_left,
applyc ``finset_in.mem_mk {new_goals := new_goals.all},
mk_sigma_mem xs,
reflexivity,
mk_finset_total (tac >> applyc ``finset_above.mem_union_right) gs
end derive_fintype
open tactic.derive_fintype
/-- Proves `|- fintype T` where `T` is a non-recursive inductive type with no indices,
where all arguments to all constructors are fintypes. -/
meta def mk_fintype_instance : tactic unit :=
do
intros,
`(fintype %%e) ← target >>= whnf,
(const I ls, args) ← pure (get_app_fn_args e),
env ← get_env,
let cs := env.constructors_of I,
guard (env.inductive_num_indices I = 0) <|>
fail "@[derive fintype]: inductive indices are not supported",
guard (¬ env.is_recursive I) <|>
fail ("@[derive fintype]: recursive inductive types are " ++
"not supported (they are also usually infinite)"),
applyc ``mk_fintype {new_goals := new_goals.all},
intro1 >>= cases >>= (λ gs,
gs.enum.mmap' $ λ ⟨i, _⟩, exact (reflect i)),
mk_finset ls args 0 cs,
intro1 >>= cases >>= mk_finset_total skip
/--
Tries to derive a `fintype` instance for inductives and structures.
For example:
```
@[derive fintype]
inductive foo (n m : ℕ)
| zero : foo
| one : bool → foo
| two : fin n → fin m → foo
```
Here, `@[derive fintype]` adds the instance `foo.fintype`. The underlying finset
definitionally unfolds to a list that enumerates the elements of the inductive in
lexicographic order.
If the structure/inductive has a type parameter `α`, then the generated instance will have an
argument `fintype α`, even if it is not used. (This is due to the implementation using
`instance_derive_handler`.)
-/
@[derive_handler] meta def fintype_instance : derive_handler :=
instance_derive_handler ``fintype mk_fintype_instance
end tactic
|
0813b2bc481226d15402988a9a45697115b5dc87 | 43390109ab88557e6090f3245c47479c123ee500 | /tmp.lean | 6cd915f779d2e9538155b9aba709f0708427e133 | [
"Apache-2.0"
] | permissive | Ja1941/xena-UROP-2018 | 41f0956519f94d56b8bf6834a8d39473f4923200 | b111fb87f343cf79eca3b886f99ee15c1dd9884b | refs/heads/master | 1,662,355,955,139 | 1,590,577,325,000 | 1,590,577,325,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,931 | lean | import algebra.module linear_algebra.basic analysis.real data.vector data.list.basic
universes u w
def R_n_basis (n : nat) : set (vector ℝ n) :=
{v | ∀ i : fin n, (v.nth i = 1 ∧ ∀ j : fin n, j.val ≠ i.val → v.nth j = 0) }
namespace vector
variables {n : ℕ}
include n
-- def has_scalar : has_scalar ℝ (vector ℝ n) :=
-- { smul := map ∘ real.has_mul.mul }
-- def has_scalar.smul := @has_scalar.smul ℝ _ (@has_scalar n)
-- infix ` • ` := has_scalar.smul
-- def has_add : has_add (vector ℝ n) :=
-- { add := map₂ real.has_add.add }
-- def has_add.add := @has_add.add _ (@has_add n)
-- infix ` + ` := has_add.add
-- def has_zero : has_zero (vector ℝ n) :=
-- { zero := repeat 0 n }
-- def has_zero.zero := @has_zero.zero _ (@vector.has_zero n)
-- notation 0 := vector.has_zero.zero
set_option pp.all false
-- set_option pp.all true
-- def has_add.add : vector ℝ n → vector ℝ n → vector ℝ n :=
-- map₂ real.has_add.add
-- instance add_semigroup : add_semigroup (vector ℝ n) :=
-- {
-- add := has_add.add,
-- add_assoc := by
-- { intros a b c, simp,
-- cases a with a la,
-- cases b with b lb,
-- cases c with c lc,
-- unfold has_add.add map₂, simp,
-- induction n with n ih generalizing a b c,
-- { have := list.length_eq_zero.mp la,
-- have := list.length_eq_zero.mp lc,
-- simp *,
-- rw [list.nil_map₂, list.map₂_nil, list.nil_map₂] },
-- {
-- cases a with ha ta, contradiction,
-- cases b with hb tb, contradiction,
-- cases c with hc tc, contradiction,
-- unfold list.map₂,
-- apply congr, simp,
-- simp [nat.add_one] at la lb lc,
-- exact ih _ _ _ la lb lc } }
-- }
-- instance add_comm_semigroup : add_comm_semigroup (vector ℝ n) :=
-- by {
-- have := add_semigroup,
-- }
instance : add_comm_group (vector ℝ n) :=
{
add := has_add.add,
add_assoc := by
{ intros a b c, simp,
cases a with a la,
cases b with b lb,
cases c with c lc,
unfold has_add.add map₂, simp,
induction n with n ih generalizing a b c,
{ have := list.length_eq_zero.mp la,
have := list.length_eq_zero.mp lc,
simp *,
rw [list.nil_map₂, list.map₂_nil, list.nil_map₂] },
{
cases a with ha ta, contradiction,
cases b with hb tb, contradiction,
cases c with hc tc, contradiction,
unfold list.map₂,
apply congr, simp,
simp [nat.add_one] at la lb lc,
exact ih _ _ _ la lb lc } },
add_comm := by
{ intros a b,
cases a with a la,
cases b with b lb,
},
neg := (map ∘ real.has_mul.mul) (-1),
add_left_neg := by simp,
zero := @repeat ℝ 0 n,
zero_add := by
{ intro a, simp,
apply vector.eq, unfold has_add.add to_list,
cases a with a la,
unfold repeat map₂, simp,
induction n with n ih generalizing a;
unfold list.repeat,
{ cases a,
apply list.map₂_nil,
contradiction },
{ cases a with ha ta,
contradiction,
unfold list.map₂,
rw [zero_add],
have := list.length_cons ha ta,
replace := eq.trans this.symm la,
simp [nat.add_one] at this,
apply congr rfl,
exact ih ta this } },
add_zero := by simp
}
#check list.map₂
noncomputable instance : vector_space ℝ (vector ℝ n) :=
{
smul := vector.map ∘ real.has_mul.mul,
smul_add := λ r x y, by {},
}
end vector
-- #reduce (vector ℝ 3)
#print module
#check list.map₂_nil
#check iff |
4e5abda998df98a126c97d53fc55946ee3c85632 | e9078bde91465351e1b354b353c9f9d8b8a9c8c2 | /propositional_truncation.hlean | 0a6f0a82a9cfc37a158cabab66a9c2f6e1fc2103 | [
"Apache-2.0"
] | permissive | EgbertRijke/leansnippets | 09fb7a9813477471532fbdd50c99be8d8fe3e6c4 | 1d9a7059784c92c0281fcc7ce66ac7b3619c8661 | refs/heads/master | 1,610,743,957,626 | 1,442,532,603,000 | 1,442,532,603,000 | 41,563,379 | 0 | 0 | null | 1,440,787,514,000 | 1,440,787,514,000 | null | UTF-8 | Lean | false | false | 14,099 | hlean | import types.eq types.pi hit.colimit types.nat.hott hit.trunc cubical.square
open eq is_trunc unit quotient seq_colim pi nat equiv sum
/-
In this file we define the propositional truncation (see very bottom), which, given (X : Type)
has constructors
* tr : X → trunc X
* is_hprop_trunc : is_hprop (trunc X)
and with a recursor which recurses to any family of mere propositions.
The construction uses a "one step truncation" of X, with two constructors:
* tr : X → one_step_tr X
* tr_eq : Π(a b : X), tr a = tr b
This is like a truncation, but taking out the recursive part.
Martin Escardo calls this construction the generalized circle, since the one step truncation of the
unit type is the circle.
Then we can repeat this n times:
A 0 = X,
A (n + 1) = one_step_tr (A n)
We have a map
f {n : ℕ} : A n → A (n + 1) := tr
Then trunc is defined as the sequential colimit of (A, f).
Both the one step truncation and the sequential colimit can be defined as a quotient, which is a
primitive HIT in Lean. Here, with a quotient, we mean the following HIT:
Given {X : Type} (R : X → X → Type) we have the constructors
* class_of : X → quotient R
* eq_of_rel : Π{a a' : X}, R a a' → a = a'
See the comment below for a sketch of the proof that (trunc A) is actually a mere proposition.
-/
/- HELPER LEMMAS -/
definition inv_con_con_eq_of_eq_con_con_inv {A : Type} {a₁ a₂ b₁ b₂ : A} {p : a₁ = b₁}
{q : a₁ = a₂} {r : a₂ = b₂} {s : b₁ = b₂} (H : q = p ⬝ s ⬝ r⁻¹) : p⁻¹ ⬝ q ⬝ r = s :=
begin
apply con_eq_of_eq_con_inv,
apply inv_con_eq_of_eq_con,
rewrite -con.assoc,
apply H
end
/-
Call a function f weakly constant if Πa a', f a = f a'
This theorem states that if f is weakly constant, then ap f is weakly constant.
-/
definition weakly_constant_ap {A B : Type} {f : A → B} {a a' : A} (p q : a = a')
(H : Π(a a' : A), f a = f a') : ap f p = ap f q :=
have L : Π{b c : A} {r : b = c}, (H a b)⁻¹ ⬝ H a c = ap f r, from
(λb c r, eq.rec_on r !con.left_inv),
L⁻¹ ⬝ L
/- definition of "one step truncation" -/
namespace one_step_tr
section
parameters {A : Type}
variables (a a' : A)
protected definition R (a a' : A) : Type₀ := unit
parameter (A)
definition one_step_tr : Type := quotient R
parameter {A}
definition tr : one_step_tr :=
class_of R a
definition tr_eq : tr a = tr a' :=
eq_of_rel _ star
protected definition rec {P : one_step_tr → Type} (Pt : Π(a : A), P (tr a))
(Pe : Π(a a' : A), Pt a =[tr_eq a a'] Pt a') (x : one_step_tr) : P x :=
begin
fapply (quotient.rec_on x),
{ intro a, apply Pt},
{ intro a a' H, cases H, apply Pe}
end
protected definition elim {P : Type} (Pt : A → P)
(Pe : Π(a a' : A), Pt a = Pt a') (x : one_step_tr) : P :=
rec Pt (λa a', pathover_of_eq (Pe a a')) x
theorem rec_tr_eq {P : one_step_tr → Type} (Pt : Π(a : A), P (tr a))
(Pe : Π(a a' : A), Pt a =[tr_eq a a'] Pt a') (a a' : A)
: apdo (rec Pt Pe) (tr_eq a a') = Pe a a' :=
!rec_eq_of_rel
theorem elim_tr_eq {P : Type} (Pt : A → P)
(Pe : Π(a a' : A), Pt a = Pt a') (a a' : A)
: ap (elim Pt Pe) (tr_eq a a') = Pe a a' :=
begin
apply eq_of_fn_eq_fn_inv !(pathover_constant (tr_eq a a')),
rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑elim,rec_tr_eq],
end
end
definition n_step_tr (A : Type) (n : ℕ) : Type := nat.rec_on n A (λn' A', one_step_tr A')
end one_step_tr
attribute one_step_tr.rec one_step_tr.elim [recursor 5]
open one_step_tr
section
parameter {X : Type}
/- basic constructors -/
private definition A [reducible] (n : ℕ) : Type := nat.rec_on n X (λn' X', one_step_tr X')
private definition f [reducible] ⦃n : ℕ⦄ (a : A n) : A (succ n) := tr a
private definition f_eq [reducible] {n : ℕ} (a a' : A n) : f a = f a' := tr_eq a a'
private definition truncX [reducible] : Type := @seq_colim A f
private definition i [reducible] {n : ℕ} (a : A n) : truncX := inclusion f a
private definition g [reducible] {n : ℕ} (a : A n) : i (f a) = i a := glue f a
/- defining the normal recursor is easy -/
private definition rec {P : truncX → Type} [Pt : Πx, is_hprop (P x)]
(H : Π(a : X), P (@i 0 a)) (x : truncX) : P x :=
begin
induction x,
{ induction n with n IH,
{ exact H a},
{ induction a,
{ exact !g⁻¹ ▸ IH a},
{ apply is_hprop.elimo}}},
{ apply is_hprop.elimo}
end
/-
The main effort is to prove that truncX is a mere proposition.
We prove
Π(a b : truncX), a = b
first by induction on a and then by induction on b
On the point level we need to construct
(1) a : A n, b : A m ⊢ p a b : i a = i b
On the path level (for the induction on b) we need to show that
(2) a : A n, b : A m ⊢ p a (f b) ⬝ g b = p a b
The path level for a is automatic, since (Πb, a = b) is a mere proposition
Thanks to Egbert Rijke for pointing this out
For (1) we distinguish the cases n ≤ m and n ≥ m,
and we prove that the two constructions coincide for n = m
For (2) we distinguish the cases n ≤ m and n > m
During the proof we heavily use induction on inequalities.
(n ≤ m), or (le n m), is defined as an inductive family:
inductive le (n : ℕ) : ℕ → Type₀ :=
| refl : le n n
| step : Π {m}, le n m → le n (succ m)
-/
/- point operations -/
definition fr [reducible] [unfold 4] {n m : ℕ} (a : A n) (H : n ≤ m) : A m :=
begin
induction H with m H b,
{ exact a},
{ exact f b},
end
/- path operations -/
definition i_fr [unfold 4] {n m : ℕ} (a : A n) (H : n ≤ m) : i (fr a H) = i a :=
begin
induction H with m H IH,
{ reflexivity},
{ exact g (fr a H) ⬝ IH},
end
definition eq_same {n : ℕ} (a a' : A n) : i a = i a' :=
calc
i a = i (f a) : g
... = i (f a') : ap i (f_eq a a')
... = i a' : g
-- step (1), case n ≥ m
definition eq_ge {n m : ℕ} (a : A n) (b : A m) (H : n ≥ m) : i a = i b :=
calc
i a = i (fr b H) : eq_same
... = i b : i_fr
-- step (1), case n ≤ m
definition eq_le {n m : ℕ} (a : A n) (b : A m) (H : n ≤ m) : i a = i b :=
calc
i a = i (fr a H) : i_fr
... = i b : eq_same
-- step (1), combined
definition eq_constructors {n m : ℕ} (a : A n) (b : A m) : i a = i b :=
lt_ge_by_cases (λH, eq_le a b (le_of_lt H)) !eq_ge
-- some other path operations needed for 2-dimensional path operations
definition fr_step {n m : ℕ} (a : A n) (H : n ≤ m) : fr a (le.step H) = f (fr a H) := idp
definition fr_irrel {n m : ℕ} (a : A n) (H H' : n ≤ m) : fr a H = fr a H' :=
ap (fr a) !is_hprop.elim
-- Maybe later: the proofs can probably be simplified a bit if H is expressed in terms of H2
definition fr_f {n m : ℕ} (a : A n) (H : n ≤ m) (H2 : succ n ≤ m) : fr a H = fr (f a) H2 :=
begin
induction H with m H IH,
{ exfalso, exact not_succ_le_self H2},
{ refine _ ⬝ ap (fr (f a)) (to_right_inv !le_equiv_succ_le_succ H2),
--TODO: add some unfold attributes in files
esimp [le_equiv_succ_le_succ,equiv_of_is_hprop, is_equiv_of_is_hprop],
revert H IH,
eapply le.rec_on (le_of_succ_le_succ H2),
{ intros, esimp [succ_le_succ], apply concat,
apply fr_irrel _ _ (le.step !le.refl),
reflexivity},
{ intros, rewrite [↑fr,↓fr a H,↓succ_le_succ a_1], exact ap (@f _) !IH}},
end
/- 2-dimensional path operations -/
theorem i_fr_step {n m : ℕ} (a : A n) (H : n ≤ m) : i_fr a (le.step H) = g (fr a H) ⬝ i_fr a H :=
idp
theorem eq_constructors_le {n m : ℕ} (a : A n) (b : A m) (H : n ≤ m)
: eq_constructors a b = eq_le a b H :=
lt_ge_by_cases_le H (λp, by cases p; exact !idp_con)
theorem eq_constructors_ge {n m : ℕ} (a : A n) (b : A m) (H : n ≥ m)
: eq_constructors a b = eq_ge a b H :=
by apply lt_ge_by_cases_ge
theorem ap_i_ap_f {n : ℕ} {a a' : A n} (p : a = a') : ap i (ap !f p) = !g ⬝ ap i p ⬝ !g⁻¹ :=
eq.rec_on p !con.right_inv⁻¹
theorem ap_i_eq_ap_i_same {n : ℕ} {a a' : A n} (p q : a = a') : ap i p = ap i q :=
!weakly_constant_ap eq_same
theorem ap_f_eq_f' {n : ℕ} (a a' : A n)
: ap i (f_eq (f a) (f a')) = g (f a) ⬝ ap i (f_eq a a') ⬝ (g (f a'))⁻¹ :=
!ap_i_eq_ap_i_same ⬝ !ap_i_ap_f
theorem ap_f_eq_f {n : ℕ} (a a' : A n)
: (g (f a))⁻¹ ⬝ ap i (f_eq (f a) (f a')) ⬝ g (f a') = ap i (f_eq a a') :=
inv_con_con_eq_of_eq_con_con_inv !ap_f_eq_f'
theorem eq_same_f {n : ℕ} (a a' : A n)
: (g a)⁻¹ ⬝ eq_same (f a) (f a') ⬝ g a' = eq_same a a' :=
begin
esimp [eq_same],
apply (ap (λx, _ ⬝ x ⬝ _)),
apply (ap_f_eq_f a a'),
end
theorem i_fr_g {n m : ℕ} (b : A n) (H1 : n ≤ m) (H2 : succ n ≤ m)
: ap i (fr_f b H1 H2) ⬝ i_fr (f b) H2 ⬝ g b = i_fr b H1 :=
begin
induction H1 with m H IH, exfalso, exact not_succ_le_self H2,
cases H2 with x H3, -- x is unused
{ rewrite [is_hprop.elim H !le.refl,↑fr_f,
↑le_equiv_succ_le_succ,▸*],
-- BUG(?): some le.rec's are not reduced if previous line is replaced by "↑le_equiv_succ_le_succ,↑i_fr,↑fr,▸*], state,"
refine (_ ⬝ !idp_con), apply ap (λx, x ⬝ _), apply (ap (ap i)),
rewrite [is_hprop_elim_self,↑fr_irrel,is_hprop_elim_self]},
{ rewrite [↑i_fr,-IH H3,-con.assoc,-con.assoc,-con.assoc],
apply ap (λx, x ⬝ _ ⬝ _), apply con_eq_of_eq_con_inv, rewrite [-ap_i_ap_f],
apply ap_i_eq_ap_i_same}
end
definition eq_same_con {n : ℕ} (a : A n) {a' a'' : A n} (p : a' = a'')
: eq_same a a' = eq_same a a'' ⬝ (ap i p)⁻¹ :=
by induction p; reflexivity
-- step (2), n > m
theorem eq_gt_f {n m : ℕ} (a : A n) (b : A m) (H1 : n ≥ succ m) (H2 : n ≥ m)
: eq_ge a (f b) H1 ⬝ g b = eq_ge a b H2 :=
begin
esimp [eq_ge],
let lem := eq_inv_con_of_con_eq (!con.assoc⁻¹ ⬝ i_fr_g b H2 H1),
rewrite [con.assoc,lem,-con.assoc], apply ap (λx, x ⬝ _),
exact !eq_same_con⁻¹
end
-- step (2), n ≤ m
theorem eq_le_f {n m : ℕ} (a : A n) (b : A m) (H1 : n ≤ succ m) (H2 : n ≤ m)
: eq_le a (f b) H1 ⬝ g b = eq_le a b H2 :=
begin
rewrite [↑eq_le,is_hprop.elim H1 (le.step H2),i_fr_step,con_inv,con.assoc,con.assoc],
clear H1,
apply ap (λx, _ ⬝ x),
rewrite [↑fr,↓fr a H2],
rewrite -con.assoc, exact !eq_same_f
end
-- step (2), combined
theorem eq_constructors_comp_right {n m : ℕ} (a : A n) (b : A m) :
eq_constructors a (f b) ⬝ g b = eq_constructors a b :=
begin
apply @lt_ge_by_cases m n,
{ intro H, let H2 := le.trans !le_succ H,
rewrite [eq_constructors_ge a (f b) H,eq_constructors_ge a b H2,eq_gt_f a b H H2]},
{ intro H2, let H := le.trans H2 !le_succ,
rewrite [eq_constructors_le a (f b) H,eq_constructors_le a b H2,eq_le_f a b H H2]}
end
-- induction on b
definition eq_constructor_left [reducible] (b : truncX) {n : ℕ} (a : A n) : i a = b :=
begin
induction b with m b,
{ apply eq_constructors},
{ apply (equiv.to_inv !pathover_eq_equiv_r), apply eq_constructors_comp_right},
end
-- induction on a
theorem eq_general (a : truncX) : Πb, a = b :=
begin
induction a,
{ intro b, apply eq_constructor_left},
{ apply is_hprop.elimo}
end
-- final result
theorem is_hprop_truncX : is_hprop truncX := is_hprop.mk eq_general
end
namespace my_trunc
definition trunc.{u} (A : Type.{u}) : Type.{u} := @truncX A
definition tr {A : Type} : A → trunc A := @i A 0
definition is_hprop_trunc (A : Type) : is_hprop (trunc A) := is_hprop_truncX
definition trunc.rec {A : Type} {P : trunc A → Type}
[Pt : Π(x : trunc A), is_hprop (P x)]
(H : Π(a : A), P (tr a)) : Π(x : trunc A), P x := @rec A P Pt H
example {A : Type} {P : trunc A → Type} [Pt : Πaa, is_hprop (P aa)]
(H : Πa, P (tr a)) (a : A) : (trunc.rec H) (tr a) = H a := by reflexivity
-- some other recursors we get from this construction:
definition trunc.elim2 {A P : Type} (h : Π{n}, n_step_tr A n → P)
(coh : Π(n : ℕ) (a : n_step_tr A n), h (f a) = h a) (x : trunc A) : P :=
begin
induction x,
{ exact h a},
{ apply coh}
end
definition trunc.rec2 {A : Type} {P : truncX → Type} (h : Π{n} (a : n_step_tr A n), P (i a))
(coh : Π(n : ℕ) (a : n_step_tr A n), h (f a) =[g a] h a)
(x : trunc A) : P x :=
begin
induction x,
{ exact h a},
{ apply coh}
end
open sigma
definition elim2_equiv {A P : Type} : (trunc A → P) ≃
Σ(h : Π{n}, n_step_tr A n → P), Π(n : ℕ) (a : n_step_tr A n), h (f a) = h a :=
begin
fapply equiv.MK,
{ intro h, fconstructor,
{ intro n a, refine h (i a)},
{ intro n a, exact ap h (g a)}},
{ intro x a, induction x with h p, induction a,
exact h a,
apply p},
{ intro x, induction x with h p, fapply sigma_eq,
{ reflexivity},
{ esimp, apply pathover_idp_of_eq, apply eq_of_homotopy2, intro n a, rewrite elim_glue}},
{ intro h, apply eq_of_homotopy, intro a, esimp, induction a,
esimp,
apply eq_pathover, apply hdeg_square, esimp, rewrite elim_glue}
end
-- the constructed truncation is equivalent to the "standard" propositional truncation
-- (called _root_.trunc below, because it lives in the root namespace)
open trunc
attribute trunc.rec [recursor]
attribute is_hprop_trunc [instance]
definition trunc_equiv (A : Type) : trunc A ≃ _root_.trunc -1 A :=
begin
fapply equiv.MK,
{ intro x, refine trunc.rec _ x, intro a, exact trunc.tr a},
{ intro x, refine _root_.trunc.rec _ x, intro a, exact tr a},
{ intro x, induction x with a, reflexivity},
{ intro x, induction x with a, reflexivity}
end
end my_trunc
|
47bc9ac321b62e769bf276dd42883aad7c90c4d5 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/hex_char.lean | 295f5077595f01535ab7ec4c20f5991afe770d91 | [
"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 | 45 | lean | vm_eval '\x41'
vm_eval '\x42'
vm_eval '\x43'
|
3e0c747dd5c070fec01fc0ff89914fe5bbefa6ce | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /tests/lean/run/irreducibleIssue.lean | 42cd5d789971a1721dce459422085050a631ef97 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 507 | lean | class Trait (X : Type u) where
R : Type v
attribute [reducible] Trait.R
class SemiInner (X : Type u) (R : Type v) where
semiInner : X → X → R
@[reducible] instance (X) (R : Type u) [SemiInner X R] : Trait X := ⟨R⟩
def norm {X} [Trait X] [inst : SemiInner X (Trait.R X)] (x : X) : Trait.R X := SemiInner.semiInner x x
section Real
def ℝ := Float
instance : SemiInner ℝ ℝ := ⟨λ x y : Float => x * y⟩
attribute [irreducible] ℝ
variable (x : ℝ)
#check (norm x : ℝ)
|
676c96eb9a4b2b949bc01716b2d30f5be41ed86d | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /src/Lean.lean | d5e58563dc9fa45f525684a85052927e61097ce3 | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 745 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Data
import Lean.Compiler
import Lean.Environment
import Lean.Modifiers
import Lean.ProjFns
import Lean.Runtime
import Lean.ResolveName
import Lean.Attributes
import Lean.Parser
import Lean.ReducibilityAttrs
import Lean.Elab
import Lean.Class
import Lean.LocalContext
import Lean.MetavarContext
import Lean.AuxRecursor
import Lean.Meta
import Lean.Util
import Lean.Eval
import Lean.Structure
import Lean.PrettyPrinter
import Lean.CoreM
import Lean.InternalExceptionId
import Lean.Server
import Lean.ScopedEnvExtension
import Lean.DocString
import Lean.DeclarationRange
|
d06d9c1b4cd4f7379773c28c0571c89fcb823d29 | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /library/init/num.lean | 84eee17874e9b4fa6bebae871e0985940d99d5b2 | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 2,041 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.bool
open bool
namespace pos_num
protected definition mul (a b : pos_num) : pos_num :=
pos_num.rec_on a
b
(λn r, bit0 r + b)
(λn r, bit0 r)
definition lt (a b : pos_num) : bool :=
pos_num.rec_on a
(λ b, pos_num.cases_on b
ff
(λm, tt)
(λm, tt))
(λn f b, pos_num.cases_on b
ff
(λm, f m)
(λm, f m))
(λn f b, pos_num.cases_on b
ff
(λm, f (succ m))
(λm, f m))
b
definition le (a b : pos_num) : bool :=
pos_num.lt a (succ b)
end pos_num
definition pos_num_has_mul [instance] [reducible] : has_mul pos_num :=
has_mul.mk pos_num.mul
namespace num
open pos_num
definition pred (a : num) : num :=
num.rec_on a zero (λp, cond (is_one p) zero (pos (pred p)))
definition size (a : num) : num :=
num.rec_on a (pos one) (λp, pos (size p))
protected definition mul (a b : num) : num :=
num.rec_on a zero (λpa, num.rec_on b zero (λpb, pos (pos_num.mul pa pb)))
end num
definition num_has_mul [instance] [reducible] : has_mul num :=
has_mul.mk num.mul
namespace num
protected definition le (a b : num) : bool :=
num.rec_on a tt (λpa, num.rec_on b ff (λpb, pos_num.le pa pb))
private definition psub (a b : pos_num) : num :=
pos_num.rec_on a
(λb, zero)
(λn f b,
cond (pos_num.le (bit1 n) b)
zero
(pos_num.cases_on b
(pos (bit0 n))
(λm, 2 * f m)
(λm, 2 * f m + 1)))
(λn f b,
cond (pos_num.le (bit0 n) b)
zero
(pos_num.cases_on b
(pos (pos_num.pred (bit0 n)))
(λm, pred (2 * f m))
(λm, 2 * f m)))
b
protected definition sub (a b : num) : num :=
num.rec_on a zero (λpa, num.rec_on b a (λpb, psub pa pb))
end num
definition num_has_sub [instance] [reducible] : has_sub num :=
has_sub.mk num.sub
|
1d18457e2afe00b4f92deb4bba625ec5ac539a0e | 0c1546a496eccfb56620165cad015f88d56190c5 | /library/init/category/applicative.lean | 474da301999caca6869409d11550f3291b2946e1 | [
"Apache-2.0"
] | permissive | Solertis/lean | 491e0939957486f664498fbfb02546e042699958 | 84188c5aa1673fdf37a082b2de8562dddf53df3f | refs/heads/master | 1,610,174,257,606 | 1,486,263,620,000 | 1,486,263,620,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 660 | 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.category.functor
universe variables u v
class applicative (f : Type u → Type v) extends functor f : Type (max u+1 v):=
(pure : Π {a : Type u}, a → f a)
(seq : Π {a b : Type u}, f (a → b) → f a → f b)
@[inline] def pure {f : Type u → Type v} [applicative f] {a : Type u} : a → f a :=
applicative.pure f
@[inline] def seq_app {a b : Type u} {f : Type u → Type v} [applicative f] : f (a → b) → f a → f b :=
applicative.seq
infixr ` <*> `:2 := seq_app
|
36bb129bfb500cbb9f710b2443a7c65a45034f2d | 7a76361040c55ae1eba5856c1a637593117a6556 | /src/lectures/love02_backward_proofs_demo.lean | b2a74ebf7985994171b27514192a1e0ed135f914 | [] | no_license | rgreenblatt/fpv2021 | c2cbe7b664b648cef7d240a654d6bdf97a559272 | c65d72e48c8fa827d2040ed6ea86c2be62db36fa | refs/heads/main | 1,692,245,693,819 | 1,633,364,621,000 | 1,633,364,621,000 | 407,231,487 | 0 | 0 | null | 1,631,808,608,000 | 1,631,808,608,000 | null | UTF-8 | Lean | false | false | 9,159 | lean | import .love01_definitions_and_statements_demo
/-! # LoVe Demo 2: Backward Proofs
A __tactic__ operates on a proof goal and either proves it or creates new
subgoals. Tactics are a __backward__ proof mechanism: They start from the goal
and work towards the available hypotheses and lemmas. -/
set_option pp.beta true
set_option pp.generalized_field_notation false
namespace LoVe
namespace backward_proofs
/-! ## Tactic Mode
Syntax of tactical proofs:
begin
_tactic₁_,
…,
_tacticN_
end -/
lemma fst_of_two_props :
∀a b : Prop, a → b → a :=
begin
intros a b,
intros ha hb,
apply ha
end
/-! ## Basic Tactics
`intro`(`s`) moves `∀`-quantified variables, or the assumptions of
implications `→`, from the goal's conclusion (after `⊢`) into the goal's
hypotheses (before `⊢`).
`apply` matches the goal's conclusion with the conclusion of the specified lemma
and adds the lemma's hypotheses as new goals. -/
lemma fst_of_two_props₂ (a b : Prop) (ha : a) (hb : b) :
a :=
begin
apply ha
end
/-! Terminal tactic syntax:
by _tactic_
abbreviates
begin
_tactic_
end -/
lemma fst_of_two_props₃ (a b : Prop) (ha : a) (hb : b) :
a :=
by apply ha
lemma prop_comp (a b c : Prop) (hab : a → b) (hbc : b → c) :
a → c :=
begin
intro ha,
apply hbc,
apply hab,
apply ha
end
/-! `exact` matches the goal's conclusion with the specified lemma, closing the
goal. We can often use `apply` in such situations, but `exact` communicates our
intentions better. -/
lemma fst_of_two_props₄ (a b : Prop) (ha : a) (hb : b) :
a :=
by exact ha
/-! `assumption` finds a hypothesis from the local context that matches the
goal's conclusion and applies it to prove the goal. -/
lemma fst_of_two_props₅ (a b : Prop) (ha : a) (hb : b) :
a :=
by assumption
/-! ## Reasoning about Logical Connectives and Quantifiers
Introduction rules:
The relevant symbol appears in the *conclusion* of the statement.
(On the right side of the ->)
We apply these rules when the symbol appears in our *goal*.
-/
#check true.intro
#check not.intro
#check and.intro
#check or.intro_left
#check or.intro_right
#check iff.intro
#check exists.intro
/-! Elimination rules:
The relevant symbol appears in a *hypothesis* of the statement.
(On the left side of the ->)
We apply these rules when the symbol appears in our *context*.
-/
#check false.elim
#check and.elim_left
#check and.elim_right
#check or.elim
#check iff.elim_left
#check iff.elim_right
#check exists.elim
/-! Definition of `¬` and related lemmas: -/
#print not
#check not_def
#check classical.em
#check classical.by_contradiction
lemma and_swap (a b : Prop) :
a ∧ b → b ∧ a :=
begin
intro hab,
apply and.intro,
apply and.elim_right,
exact hab,
apply and.elim_left,
exact hab
end
/-! The `{ … }` combinator focuses on the first subgoal. The tactic inside must
fully prove it. -/
lemma and_swap₂ :
∀a b : Prop, a ∧ b → b ∧ a :=
begin
intros a b hab,
apply and.intro,
{ exact and.elim_right hab },
{ exact and.elim_left hab }
end
/-! Notice above how we pass the hypothesis `hab` directly to the lemmas
`and.elim_right` and `and.elim_left`, instead of waiting for the lemmas's
assumptions to appear as new subgoals. This is a small forward step in an
otherwise backward proof. -/
lemma or_swap (a b : Prop) :
a ∨ b → b ∨ a :=
begin
intros hab,
apply or.elim hab,
{ intro ha,
exact or.intro_right _ ha },
{ intro hb,
exact or.intro_left _ hb }
end
lemma modus_ponens (a b : Prop) :
(a → b) → a → b :=
begin
intros hab ha,
apply hab,
exact ha
end
lemma not_not_intro (a : Prop) :
a → ¬¬ a :=
begin
intro ha,
apply not.intro,
intro hna,
apply hna,
exact ha
end
lemma not_not_intro₂ (a : Prop) :
a → ¬¬ a :=
begin
intros ha hna,
apply hna,
exact ha
end
def double (n : ℕ) : ℕ :=
n + n
lemma nat_exists_double_iden :
∃n : ℕ, double n = n :=
begin
apply exists.intro 0,
refl
end
/-! ## Reasoning about Equality
*Syntactic* equality:
x = x
[2, 1, 3] = [2, 1, 3]
*Definitional* equality (*intensional*, *up to computation*):
2 + 2 = 4
quicksort [2, 1, 3] = mergesort [2, 1, 3]
all of the `by refl` examples below
*Propositional* equality (*provable*):
x + y = y + x
quicksort = mergesort
-/
/-! `refl` proves `l = r`, where the two sides are equal up to
computation. Computation means unfolding of definitions, β-reduction
(application of λ to an argument), `let`, and more. -/
lemma α_example {α β : Type} (f : α → β) :
(λx, f x) = (λy, f y) :=
begin
refl
end
lemma α_example₂ {α β : Type} (f : α → β) :
(λx, f x) = (λy, f y) :=
by refl
lemma β_example {α β : Type} (f : α → β) (a : α) :
(λx, f x) a = f a :=
by refl
lemma δ_example :
double 5 = 5 + 5 :=
by refl
lemma ζ_example :
(let n : ℕ := 2 in n + n) = 2 + 2 :=
by refl
lemma η_example {α β : Type} (f : α → β) :
(λx, f x) = f :=
by refl
inductive my_prod (α β : Type) : Type
| mk : α → β → my_prod
def my_prod.first {α β : Type} : my_prod α β → α
| (my_prod.mk a b) := a
lemma ι_example {α β : Type} (a : α) (b : β) :
my_prod.first (my_prod.mk a b) = a :=
by refl
/-!
Which ones of these are *reduction rules*?
-/
#check eq.refl
#check eq.symm
#check eq.trans
#check eq.subst
/-! The above rules can be used directly: -/
lemma cong_fst_arg {α : Type} (a a' b : α)
(f : α → α → α) (ha : a = a') :
f a b = f a' b :=
begin
apply eq.subst ha,
apply eq.refl
end
lemma cong_two_args {α : Type} (a a' b b' : α)
(f : α → α → α) (ha : a = a') (hb : b = b') :
f a b = f a' b' :=
begin
apply eq.subst ha,
apply eq.subst hb,
apply eq.refl
end
/-! `rw` applies a single equation as a left-to-right rewrite rule, once. To
apply an equation right-to-left, prefix its name with `←`. -/
lemma cong_two_args₂ {α : Type} (a a' b b' : α)
(f : α → α → α) (ha : a = a') (hb : b = b') :
f a b = f a' b' :=
begin
rw ha,
rw hb
end
#check add_comm
#check add_assoc
lemma nat_comm_example (a b c : ℕ) :
a + b + c = c + b + a :=
begin
rw add_comm,
rw add_comm a,
rw add_assoc
end
lemma nat_comm_example₂ (a b c : ℕ) :
a + b + c = c + b + a :=
begin
rw [add_comm, add_comm a, add_assoc]
end
lemma double_example (n : ℕ) :
double n = n + n + 0 :=
begin
rw double,
refl
end
lemma a_proof_of_negation₃ (a : Prop) :
a → ¬¬ a :=
begin
rw not_def,
rw not_def,
intro ha,
intro hna,
apply hna,
exact ha
end
/-! `simp` applies a standard set of rewrite rules (the __simp set__)
exhaustively. The set can be extended using the `@[simp]` attribute. Lemmas can
be temporarily added to the simp set with the syntax
`simp [_lemma₁_, …, _lemmaN_]`. -/
lemma cong_two_args_etc {α : Type} (a a' b b' : α)
(g : α → α → ℕ → α) (ha : a = a') (hb : b = b') :
g a b (1 + 1) = g a' b' 2 :=
by simp [ha, hb]
/-! `cc` applies __congruence closure__ to derive new equalities. -/
lemma cong_two_args₃ {α : Type} (a a' b b' : α)
(f : α → α → α) (ha : a = a') (hb : b = b') :
f a b = f a' b' :=
by cc
/-! `cc` can also reason up to associativity and commutativity of `+`, `*`,
and other binary operators. -/
lemma cong_assoc_comm (a a' b c : ℝ) (f : ℝ → ℝ)
(ha : a = a') :
f (a + b + c) = f (c + b + a') :=
by cc
/-! ## Proofs by Mathematical Induction
`induction'` performs induction on the specified variable. It gives rise to one
subgoal per constructor. -/
lemma add_zero (n : ℕ) :
add 0 n = n :=
begin
induction' n,
{ refl },
{ simp [add, ih] }
end
/-! We use `induction'`, a variant of Lean's built-in `induction` tactic. The
two tactics are similar, but `induction'` is more user-friendly. -/
lemma add_succ (i j : ℕ) :
add (nat.succ i) j = nat.succ (add i j) :=
begin
induction' j,
{ refl },
{ simp [add, ih] }
end
lemma add_comm (i j : ℕ) :
add i j = add j i :=
begin
induction' j,
{ simp [add, add_zero] },
{ simp [add, add_succ, ih] }
end
lemma add_assoc (i j k : ℕ) :
add (add i j) k = add i (add j k) :=
begin
induction' k,
{ refl },
{ simp [add, ih] }
end
/-! `cc` is extensible. We can register `add` as a commutative and associative
operator using the type class instance mechanism (explained in lecture 4). This
is useful for the `cc` invocation below. -/
@[instance] def add.is_commutative : is_commutative ℕ add :=
{ comm := add_comm }
@[instance] def add.is_associative : is_associative ℕ add :=
{ assoc := add_assoc }
lemma mul_add (i j k : ℕ) :
mul i (add j k) = add (mul i j) (mul i k) :=
begin
induction' k,
{ refl },
{ simp [add, mul, ih],
cc }
end
/-! ## Cleanup Tactics
`rename` changes the name of a variable or hypothesis.
`clear` removes unused variables or hypotheses. -/
lemma cleanup_example (a b c : Prop) (ha : a) (hb : b)
(hab : a → b) (hbc : b → c) :
c :=
begin
clear ha hab a,
apply hbc,
clear hbc c,
rename hb h,
exact h
end
end backward_proofs
end LoVe
|
35e8359abe7136be3be1b3fa6293784ee96ef29f | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/category_theory/limits/shapes/biproducts.lean | 0c6fc623b5a4083bb7335e63d73fc3bfd7478260 | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 37,819 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.limits.shapes.finite_products
import category_theory.limits.shapes.binary_products
import category_theory.preadditive
/-!
# Biproducts and binary biproducts
We introduce the notion of (finite) biproducts and binary biproducts.
These are slightly unusual relative to the other shapes in the library,
as they are simultaneously limits and colimits.
(Zero objects are similar; they are "biterminal".)
We treat first the case of a general category with zero morphisms,
and subsequently the case of a preadditive category.
In a category with zero morphisms, we model the (binary) biproduct of `P Q : C`
using a `binary_bicone`, which has a cone point `X`,
and morphisms `fst : X ⟶ P`, `snd : X ⟶ Q`, `inl : P ⟶ X` and `inr : X ⟶ Q`,
such that `inl ≫ fst = 𝟙 P`, `inl ≫ snd = 0`, `inr ≫ fst = 0`, and `inr ≫ snd = 𝟙 Q`.
Such a `binary_bicone` is a biproduct if the cone is a limit cone, and the cocone is a colimit cocone.
In a preadditive category,
* any `binary_biproduct` satisfies `total : fst ≫ inl + snd ≫ inr = 𝟙 X`
* any `binary_product` is a `binary_biproduct`
* any `binary_coproduct` is a `binary_biproduct`
For biproducts indexed by a `fintype J`, a `bicone` again consists of a cone point `X`
and morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`,
such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise.
In a preadditive category,
* any `biproduct` satisfies `total : ∑ j : J, biproduct.π f j ≫ biproduct.ι f j = 𝟙 (⨁ f)`
* any `product` is a `biproduct`
* any `coproduct` is a `biproduct`
## Notation
As `⊕` is already taken for the sum of types, we introduce the notation `X ⊞ Y` for
a binary biproduct. We introduce `⨁ f` for the indexed biproduct.
-/
noncomputable theory
universes v u
open category_theory
open category_theory.functor
namespace category_theory.limits
variables {J : Type v} [decidable_eq J]
variables {C : Type u} [category.{v} C] [has_zero_morphisms C]
/--
A `c : bicone F` is:
* an object `c.X` and
* morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`,
* such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise.
-/
@[nolint has_inhabited_instance]
structure bicone (F : J → C) :=
(X : C)
(π : Π j, X ⟶ F j)
(ι : Π j, F j ⟶ X)
(ι_π : ∀ j j', ι j ≫ π j' = if h : j = j' then eq_to_hom (congr_arg F h) else 0)
@[simp] lemma bicone_ι_π_self {F : J → C} (B : bicone F) (j : J) : B.ι j ≫ B.π j = 𝟙 (F j) :=
by simpa using B.ι_π j j
@[simp] lemma bicone_ι_π_ne {F : J → C} (B : bicone F) {j j' : J} (h : j ≠ j') :
B.ι j ≫ B.π j' = 0 :=
by simpa [h] using B.ι_π j j'
variables {F : J → C}
namespace bicone
/-- Extract the cone from a bicone. -/
@[simps]
def to_cone (B : bicone F) : cone (discrete.functor F) :=
{ X := B.X,
π := { app := λ j, B.π j }, }
/-- Extract the cocone from a bicone. -/
@[simps]
def to_cocone (B : bicone F) : cocone (discrete.functor F) :=
{ X := B.X,
ι := { app := λ j, B.ι j }, }
end bicone
/--
A bicone over `F : J → C`, which is both a limit cone and a colimit cocone.
-/
@[nolint has_inhabited_instance]
structure limit_bicone (F : J → C) :=
(bicone : bicone F)
(is_limit : is_limit bicone.to_cone)
(is_colimit : is_colimit bicone.to_cocone)
/--
`has_biproduct F` expresses the mere existence of a bicone which is
simultaneously a limit and a colimit of the diagram `F`.
-/
class has_biproduct (F : J → C) : Prop :=
mk' :: (exists_biproduct : nonempty (limit_bicone F))
lemma has_biproduct.mk {F : J → C} (d : limit_bicone F) : has_biproduct F :=
⟨nonempty.intro d⟩
/-- Use the axiom of choice to extract explicit `biproduct_data F` from `has_biproduct F`. -/
def get_biproduct_data (F : J → C) [has_biproduct F] : limit_bicone F :=
classical.choice has_biproduct.exists_biproduct
/-- A bicone for `F` which is both a limit cone and a colimit cocone. -/
def biproduct.bicone (F : J → C) [has_biproduct F] : bicone F :=
(get_biproduct_data F).bicone
/-- `biproduct.bicone F` is a limit cone. -/
def biproduct.is_limit (F : J → C) [has_biproduct F] : is_limit (biproduct.bicone F).to_cone :=
(get_biproduct_data F).is_limit
/-- `biproduct.bicone F` is a colimit cocone. -/
def biproduct.is_colimit (F : J → C) [has_biproduct F] : is_colimit (biproduct.bicone F).to_cocone :=
(get_biproduct_data F).is_colimit
@[priority 100]
instance has_product_of_has_biproduct [has_biproduct F] : has_limit (discrete.functor F) :=
has_limit.mk { cone := (biproduct.bicone F).to_cone,
is_limit := biproduct.is_limit F, }
@[priority 100]
instance has_coproduct_of_has_biproduct [has_biproduct F] : has_colimit (discrete.functor F) :=
has_colimit.mk { cocone := (biproduct.bicone F).to_cocone,
is_colimit := biproduct.is_colimit F, }
variables (J C)
/--
`C` has biproducts of shape `J` if we have
a limit and a colimit, with the same cone points,
of every function `F : J → C`.
-/
class has_biproducts_of_shape : Prop :=
(has_biproduct : Π F : J → C, has_biproduct F)
attribute [instance, priority 100] has_biproducts_of_shape.has_biproduct
/-- `has_finite_biproducts C` represents a choice of biproduct for every family of objects in `C`
indexed by a finite type with decidable equality. -/
class has_finite_biproducts : Prop :=
(has_biproducts_of_shape : Π (J : Type v) [decidable_eq J] [fintype J],
has_biproducts_of_shape J C)
attribute [instance, priority 100] has_finite_biproducts.has_biproducts_of_shape
@[priority 100]
instance has_finite_products_of_has_finite_biproducts [has_finite_biproducts C] :
has_finite_products C :=
λ J _ _, ⟨λ F, by exactI has_limit_of_iso discrete.nat_iso_functor.symm⟩
@[priority 100]
instance has_finite_coproducts_of_has_finite_biproducts [has_finite_biproducts C] :
has_finite_coproducts C :=
λ J _ _, ⟨λ F, by exactI has_colimit_of_iso discrete.nat_iso_functor⟩
variables {J C}
/--
The isomorphism between the specified limit and the specified colimit for
a functor with a bilimit.
-/
def biproduct_iso (F : J → C) [has_biproduct F] :
limits.pi_obj F ≅ limits.sigma_obj F :=
(is_limit.cone_point_unique_up_to_iso (limit.is_limit _) (biproduct.is_limit F)).trans $
is_colimit.cocone_point_unique_up_to_iso (biproduct.is_colimit F) (colimit.is_colimit _)
end category_theory.limits
namespace category_theory.limits
variables {J : Type v} [decidable_eq J]
variables {C : Type u} [category.{v} C] [has_zero_morphisms C]
/-- `biproduct f` computes the biproduct of a family of elements `f`. (It is defined as an
abbreviation for `limit (discrete.functor f)`, so for most facts about `biproduct f`, you will
just use general facts about limits and colimits.) -/
abbreviation biproduct (f : J → C) [has_biproduct f] : C :=
(biproduct.bicone f).X
notation `⨁ ` f:20 := biproduct f
/-- The projection onto a summand of a biproduct. -/
abbreviation biproduct.π (f : J → C) [has_biproduct f] (b : J) : ⨁ f ⟶ f b :=
(biproduct.bicone f).π b
@[simp]
lemma biproduct.bicone_π (f : J → C) [has_biproduct f] (b : J) :
(biproduct.bicone f).π b = biproduct.π f b := rfl
/-- The inclusion into a summand of a biproduct. -/
abbreviation biproduct.ι (f : J → C) [has_biproduct f] (b : J) : f b ⟶ ⨁ f :=
(biproduct.bicone f).ι b
@[simp]
lemma biproduct.bicone_ι (f : J → C) [has_biproduct f] (b : J) :
(biproduct.bicone f).ι b = biproduct.ι f b := rfl
@[reassoc]
lemma biproduct.ι_π (f : J → C) [has_biproduct f] (j j' : J) :
biproduct.ι f j ≫ biproduct.π f j' = if h : j = j' then eq_to_hom (congr_arg f h) else 0 :=
(biproduct.bicone f).ι_π j j'
@[simp,reassoc]
lemma biproduct.ι_π_self (f : J → C) [has_biproduct f] (j : J) :
biproduct.ι f j ≫ biproduct.π f j = 𝟙 _ :=
by simp [biproduct.ι_π]
@[simp,reassoc]
lemma biproduct.ι_π_ne (f : J → C) [has_biproduct f] {j j' : J} (h : j ≠ j') :
biproduct.ι f j ≫ biproduct.π f j' = 0 :=
by simp [biproduct.ι_π, h]
/-- Given a collection of maps into the summands, we obtain a map into the biproduct. -/
abbreviation biproduct.lift
{f : J → C} [has_biproduct f] {P : C} (p : Π b, P ⟶ f b) : P ⟶ ⨁ f :=
(biproduct.is_limit f).lift (fan.mk p)
/-- Given a collection of maps out of the summands, we obtain a map out of the biproduct. -/
abbreviation biproduct.desc
{f : J → C} [has_biproduct f] {P : C} (p : Π b, f b ⟶ P) : ⨁ f ⟶ P :=
(biproduct.is_colimit f).desc (cofan.mk p)
@[simp, reassoc]
lemma biproduct.lift_π {f : J → C} [has_biproduct f] {P : C} (p : Π b, P ⟶ f b) (j : J) :
biproduct.lift p ≫ biproduct.π f j = p j :=
(biproduct.is_limit f).fac _ _
@[simp, reassoc]
lemma biproduct.ι_desc {f : J → C} [has_biproduct f] {P : C} (p : Π b, f b ⟶ P) (j : J) :
biproduct.ι f j ≫ biproduct.desc p = p j :=
(biproduct.is_colimit f).fac _ _
/-- Given a collection of maps between corresponding summands of a pair of biproducts
indexed by the same type, we obtain a map between the biproducts. -/
abbreviation biproduct.map [fintype J] {f g : J → C} [has_finite_biproducts C]
(p : Π b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g :=
is_limit.map (biproduct.bicone f).to_cone (biproduct.is_limit g) (discrete.nat_trans p)
/-- An alternative to `biproduct.map` constructed via colimits.
This construction only exists in order to show it is equal to `biproduct.map`. -/
abbreviation biproduct.map' [fintype J] {f g : J → C} [has_finite_biproducts C]
(p : Π b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g :=
is_colimit.map (biproduct.is_colimit f) (biproduct.bicone g).to_cocone (discrete.nat_trans p)
@[ext] lemma biproduct.hom_ext {f : J → C} [has_biproduct f]
{Z : C} (g h : Z ⟶ ⨁ f)
(w : ∀ j, g ≫ biproduct.π f j = h ≫ biproduct.π f j) : g = h :=
(biproduct.is_limit f).hom_ext w
@[ext] lemma biproduct.hom_ext' {f : J → C} [has_biproduct f]
{Z : C} (g h : ⨁ f ⟶ Z)
(w : ∀ j, biproduct.ι f j ≫ g = biproduct.ι f j ≫ h) : g = h :=
(biproduct.is_colimit f).hom_ext w
lemma biproduct.map_eq_map' [fintype J] {f g : J → C} [has_finite_biproducts C]
(p : Π b, f b ⟶ g b) : biproduct.map p = biproduct.map' p :=
begin
ext j j',
simp only [discrete.nat_trans_app, limits.ι_is_colimit_map, limits.is_limit_map_π, category.assoc,
←bicone.to_cone_π_app, ←biproduct.bicone_π, ←bicone.to_cocone_ι_app, ←biproduct.bicone_ι],
simp only [biproduct.bicone_ι, biproduct.bicone_π, bicone.to_cocone_ι_app, bicone.to_cone_π_app],
rw [biproduct.ι_π_assoc, biproduct.ι_π],
split_ifs,
{ subst h, rw [eq_to_hom_refl, category.id_comp], erw category.comp_id, },
{ simp, },
end
instance biproduct.ι_mono (f : J → C) [has_biproduct f]
(b : J) : split_mono (biproduct.ι f b) :=
{ retraction := biproduct.desc $
λ b', if h : b' = b then eq_to_hom (congr_arg f h) else biproduct.ι f b' ≫ biproduct.π f b }
instance biproduct.π_epi (f : J → C) [has_biproduct f]
(b : J) : split_epi (biproduct.π f b) :=
{ section_ := biproduct.lift $
λ b', if h : b = b' then eq_to_hom (congr_arg f h) else biproduct.ι f b ≫ biproduct.π f b' }
@[simp, reassoc]
lemma biproduct.map_π [fintype J] {f g : J → C} [has_finite_biproducts C]
(p : Π j, f j ⟶ g j) (j : J) :
biproduct.map p ≫ biproduct.π g j = biproduct.π f j ≫ p j :=
limits.is_limit_map_π _ _ _ _
@[simp, reassoc]
lemma biproduct.ι_map [fintype J] {f g : J → C} [has_finite_biproducts C]
(p : Π j, f j ⟶ g j) (j : J) :
biproduct.ι f j ≫ biproduct.map p = p j ≫ biproduct.ι g j :=
begin
rw biproduct.map_eq_map',
convert limits.ι_is_colimit_map _ _ _ _; refl
end
variables {C}
/--
A binary bicone for a pair of objects `P Q : C` consists of the cone point `X`,
maps from `X` to both `P` and `Q`, and maps from both `P` and `Q` to `X`,
so that `inl ≫ fst = 𝟙 P`, `inl ≫ snd = 0`, `inr ≫ fst = 0`, and `inr ≫ snd = 𝟙 Q`
-/
@[nolint has_inhabited_instance]
structure binary_bicone (P Q : C) :=
(X : C)
(fst : X ⟶ P)
(snd : X ⟶ Q)
(inl : P ⟶ X)
(inr : Q ⟶ X)
(inl_fst' : inl ≫ fst = 𝟙 P . obviously)
(inl_snd' : inl ≫ snd = 0 . obviously)
(inr_fst' : inr ≫ fst = 0 . obviously)
(inr_snd' : inr ≫ snd = 𝟙 Q . obviously)
restate_axiom binary_bicone.inl_fst'
restate_axiom binary_bicone.inl_snd'
restate_axiom binary_bicone.inr_fst'
restate_axiom binary_bicone.inr_snd'
attribute [simp, reassoc] binary_bicone.inl_fst binary_bicone.inl_snd
binary_bicone.inr_fst binary_bicone.inr_snd
namespace binary_bicone
variables {P Q : C}
/-- Extract the cone from a binary bicone. -/
def to_cone (c : binary_bicone P Q) : cone (pair P Q) :=
binary_fan.mk c.fst c.snd
@[simp]
lemma to_cone_X (c : binary_bicone P Q) :
c.to_cone.X = c.X := rfl
@[simp]
lemma to_cone_π_app_left (c : binary_bicone P Q) :
c.to_cone.π.app (walking_pair.left) = c.fst := rfl
@[simp]
lemma to_cone_π_app_right (c : binary_bicone P Q) :
c.to_cone.π.app (walking_pair.right) = c.snd := rfl
/-- Extract the cocone from a binary bicone. -/
def to_cocone (c : binary_bicone P Q) : cocone (pair P Q) :=
binary_cofan.mk c.inl c.inr
@[simp]
lemma to_cocone_X (c : binary_bicone P Q) :
c.to_cocone.X = c.X := rfl
@[simp]
lemma to_cocone_ι_app_left (c : binary_bicone P Q) :
c.to_cocone.ι.app (walking_pair.left) = c.inl := rfl
@[simp]
lemma to_cocone_ι_app_right (c : binary_bicone P Q) :
c.to_cocone.ι.app (walking_pair.right) = c.inr := rfl
end binary_bicone
namespace bicone
/-- Convert a `bicone` over a function on `walking_pair` to a binary_bicone. -/
@[simps]
def to_binary_bicone {X Y : C} (b : bicone (pair X Y).obj) : binary_bicone X Y :=
{ X := b.X,
fst := b.π walking_pair.left,
snd := b.π walking_pair.right,
inl := b.ι walking_pair.left,
inr := b.ι walking_pair.right,
inl_fst' := by { simp [bicone.ι_π], refl, },
inr_fst' := by simp [bicone.ι_π],
inl_snd' := by simp [bicone.ι_π],
inr_snd' := by { simp [bicone.ι_π], refl, }, }
/--
If the cone obtained from a bicone over `pair X Y` is a limit cone,
so is the cone obtained by converting that bicone to a binary_bicone, then to a cone.
-/
def to_binary_bicone_is_limit {X Y : C} {b : bicone (pair X Y).obj}
(c : is_limit (b.to_cone)) :
is_limit (b.to_binary_bicone.to_cone) :=
{ lift := λ s, c.lift s,
fac' := λ s j, by { cases j; erw c.fac, },
uniq' := λ s m w,
begin
apply c.uniq s,
rintro (⟨⟩|⟨⟩),
exact w walking_pair.left,
exact w walking_pair.right,
end, }
/--
If the cocone obtained from a bicone over `pair X Y` is a colimit cocone,
so is the cocone obtained by converting that bicone to a binary_bicone, then to a cocone.
-/
def to_binary_bicone_is_colimit {X Y : C} {b : bicone (pair X Y).obj}
(c : is_colimit (b.to_cocone)) :
is_colimit (b.to_binary_bicone.to_cocone) :=
{ desc := λ s, c.desc s,
fac' := λ s j, by { cases j; erw c.fac, },
uniq' := λ s m w,
begin
apply c.uniq s,
rintro (⟨⟩|⟨⟩),
exact w walking_pair.left,
exact w walking_pair.right,
end, }
end bicone
/--
A bicone over `P Q : C`, which is both a limit cone and a colimit cocone.
-/
@[nolint has_inhabited_instance]
structure binary_biproduct_data (P Q : C) :=
(bicone : binary_bicone P Q)
(is_limit : is_limit bicone.to_cone)
(is_colimit : is_colimit bicone.to_cocone)
/--
`has_binary_biproduct P Q` expresses the mere existence of a bicone which is
simultaneously a limit and a colimit of the diagram `pair P Q`.
-/
class has_binary_biproduct (P Q : C) : Prop :=
mk' :: (exists_binary_biproduct : nonempty (binary_biproduct_data P Q))
lemma has_binary_biproduct.mk {P Q : C} (d : binary_biproduct_data P Q) : has_binary_biproduct P Q :=
⟨nonempty.intro d⟩
/--
Use the axiom of choice to extract explicit `binary_biproduct_data F` from `has_binary_biproduct F`.
-/
def get_binary_biproduct_data (P Q : C) [has_binary_biproduct P Q] : binary_biproduct_data P Q :=
classical.choice has_binary_biproduct.exists_binary_biproduct
/-- A bicone for `P Q ` which is both a limit cone and a colimit cocone. -/
def binary_biproduct.bicone (P Q : C) [has_binary_biproduct P Q] : binary_bicone P Q :=
(get_binary_biproduct_data P Q).bicone
/-- `binary_biproduct.bicone P Q` is a limit cone. -/
def binary_biproduct.is_limit (P Q : C) [has_binary_biproduct P Q] :
is_limit (binary_biproduct.bicone P Q).to_cone :=
(get_binary_biproduct_data P Q).is_limit
/-- `binary_biproduct.bicone P Q` is a colimit cocone. -/
def binary_biproduct.is_colimit (P Q : C) [has_binary_biproduct P Q] :
is_colimit (binary_biproduct.bicone P Q).to_cocone :=
(get_binary_biproduct_data P Q).is_colimit
section
variable (C)
/--
`has_binary_biproducts C` represents the existence of a bicone which is
simultaneously a limit and a colimit of the diagram `pair P Q`, for every `P Q : C`.
-/
class has_binary_biproducts : Prop :=
(has_binary_biproduct : Π (P Q : C), has_binary_biproduct P Q)
attribute [instance, priority 100] has_binary_biproducts.has_binary_biproduct
/--
A category with finite biproducts has binary biproducts.
This is not an instance as typically in concrete categories there will be
an alternative construction with nicer definitional properties.
-/
lemma has_binary_biproducts_of_finite_biproducts [has_finite_biproducts C] :
has_binary_biproducts C :=
{ has_binary_biproduct := λ P Q, has_binary_biproduct.mk
{ bicone := (biproduct.bicone (pair P Q).obj).to_binary_bicone,
is_limit := bicone.to_binary_bicone_is_limit (biproduct.is_limit _),
is_colimit := bicone.to_binary_bicone_is_colimit (biproduct.is_colimit _) } }
end
variables {P Q : C}
instance has_binary_biproduct.has_limit_pair [has_binary_biproduct P Q] :
has_limit (pair P Q) :=
has_limit.mk ⟨_, binary_biproduct.is_limit P Q⟩
instance has_binary_biproduct.has_colimit_pair [has_binary_biproduct P Q] :
has_colimit (pair P Q) :=
has_colimit.mk ⟨_, binary_biproduct.is_colimit P Q⟩
@[priority 100]
instance has_binary_products_of_has_binary_biproducts [has_binary_biproducts C] :
has_binary_products C :=
{ has_limit := λ F, has_limit_of_iso (diagram_iso_pair F).symm }
@[priority 100]
instance has_binary_coproducts_of_has_binary_biproducts [has_binary_biproducts C] :
has_binary_coproducts C :=
{ has_colimit := λ F, has_colimit_of_iso (diagram_iso_pair F) }
/--
The isomorphism between the specified binary product and the specified binary coproduct for
a pair for a binary biproduct.
-/
def biprod_iso (X Y : C) [has_binary_biproduct X Y] :
limits.prod X Y ≅ limits.coprod X Y :=
(is_limit.cone_point_unique_up_to_iso (limit.is_limit _) (binary_biproduct.is_limit X Y)).trans $
is_colimit.cocone_point_unique_up_to_iso (binary_biproduct.is_colimit X Y) (colimit.is_colimit _)
/-- An arbitrary choice of biproduct of a pair of objects. -/
abbreviation biprod (X Y : C) [has_binary_biproduct X Y] := (binary_biproduct.bicone X Y).X
notation X ` ⊞ `:20 Y:20 := biprod X Y
/-- The projection onto the first summand of a binary biproduct. -/
abbreviation biprod.fst {X Y : C} [has_binary_biproduct X Y] : X ⊞ Y ⟶ X :=
(binary_biproduct.bicone X Y).fst
/-- The projection onto the second summand of a binary biproduct. -/
abbreviation biprod.snd {X Y : C} [has_binary_biproduct X Y] : X ⊞ Y ⟶ Y :=
(binary_biproduct.bicone X Y).snd
/-- The inclusion into the first summand of a binary biproduct. -/
abbreviation biprod.inl {X Y : C} [has_binary_biproduct X Y] : X ⟶ X ⊞ Y :=
(binary_biproduct.bicone X Y).inl
/-- The inclusion into the second summand of a binary biproduct. -/
abbreviation biprod.inr {X Y : C} [has_binary_biproduct X Y] : Y ⟶ X ⊞ Y :=
(binary_biproduct.bicone X Y).inr
section
variables {X Y : C} [has_binary_biproduct X Y]
@[simp] lemma binary_biproduct.bicone_fst : (binary_biproduct.bicone X Y).fst = biprod.fst := rfl
@[simp] lemma binary_biproduct.bicone_snd : (binary_biproduct.bicone X Y).snd = biprod.snd := rfl
@[simp] lemma binary_biproduct.bicone_inl : (binary_biproduct.bicone X Y).inl = biprod.inl := rfl
@[simp] lemma binary_biproduct.bicone_inr : (binary_biproduct.bicone X Y).inr = biprod.inr := rfl
end
@[simp,reassoc]
lemma biprod.inl_fst {X Y : C} [has_binary_biproduct X Y] :
(biprod.inl : X ⟶ X ⊞ Y) ≫ (biprod.fst : X ⊞ Y ⟶ X) = 𝟙 X :=
(binary_biproduct.bicone X Y).inl_fst
@[simp,reassoc]
lemma biprod.inl_snd {X Y : C} [has_binary_biproduct X Y] :
(biprod.inl : X ⟶ X ⊞ Y) ≫ (biprod.snd : X ⊞ Y ⟶ Y) = 0 :=
(binary_biproduct.bicone X Y).inl_snd
@[simp,reassoc]
lemma biprod.inr_fst {X Y : C} [has_binary_biproduct X Y] :
(biprod.inr : Y ⟶ X ⊞ Y) ≫ (biprod.fst : X ⊞ Y ⟶ X) = 0 :=
(binary_biproduct.bicone X Y).inr_fst
@[simp,reassoc]
lemma biprod.inr_snd {X Y : C} [has_binary_biproduct X Y] :
(biprod.inr : Y ⟶ X ⊞ Y) ≫ (biprod.snd : X ⊞ Y ⟶ Y) = 𝟙 Y :=
(binary_biproduct.bicone X Y).inr_snd
/-- Given a pair of maps into the summands of a binary biproduct,
we obtain a map into the binary biproduct. -/
abbreviation biprod.lift {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X) (g : W ⟶ Y) :
W ⟶ X ⊞ Y :=
(binary_biproduct.is_limit X Y).lift (binary_fan.mk f g)
/-- Given a pair of maps out of the summands of a binary biproduct,
we obtain a map out of the binary biproduct. -/
abbreviation biprod.desc {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) :
X ⊞ Y ⟶ W :=
(binary_biproduct.is_colimit X Y).desc (binary_cofan.mk f g)
@[simp, reassoc]
lemma biprod.lift_fst {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X) (g : W ⟶ Y) :
biprod.lift f g ≫ biprod.fst = f :=
(binary_biproduct.is_limit X Y).fac _ walking_pair.left
@[simp, reassoc]
lemma biprod.lift_snd {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X) (g : W ⟶ Y) :
biprod.lift f g ≫ biprod.snd = g :=
(binary_biproduct.is_limit X Y).fac _ walking_pair.right
@[simp, reassoc]
lemma biprod.inl_desc {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) :
biprod.inl ≫ biprod.desc f g = f :=
(binary_biproduct.is_colimit X Y).fac _ walking_pair.left
@[simp, reassoc]
lemma biprod.inr_desc {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) :
biprod.inr ≫ biprod.desc f g = g :=
(binary_biproduct.is_colimit X Y).fac _ walking_pair.right
instance biprod.mono_lift_of_mono_left {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X)
(g : W ⟶ Y) [mono f] : mono (biprod.lift f g) :=
mono_of_mono_fac $ biprod.lift_fst _ _
instance biprod.mono_lift_of_mono_right {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X)
(g : W ⟶ Y) [mono g] : mono (biprod.lift f g) :=
mono_of_mono_fac $ biprod.lift_snd _ _
instance biprod.epi_desc_of_epi_left {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W)
[epi f] : epi (biprod.desc f g) :=
epi_of_epi_fac $ biprod.inl_desc _ _
instance biprod.epi_desc_of_epi_right {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W)
[epi g] : epi (biprod.desc f g) :=
epi_of_epi_fac $ biprod.inr_desc _ _
/-- Given a pair of maps between the summands of a pair of binary biproducts,
we obtain a map between the binary biproducts. -/
abbreviation biprod.map {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : W ⊞ X ⟶ Y ⊞ Z :=
is_limit.map (binary_biproduct.bicone W X).to_cone (binary_biproduct.is_limit Y Z)
(@map_pair _ _ (pair W X) (pair Y Z) f g)
/-- An alternative to `biprod.map` constructed via colimits.
This construction only exists in order to show it is equal to `biprod.map`. -/
abbreviation biprod.map' {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : W ⊞ X ⟶ Y ⊞ Z :=
is_colimit.map (binary_biproduct.is_colimit W X) (binary_biproduct.bicone Y Z).to_cocone
(@map_pair _ _ (pair W X) (pair Y Z) f g)
@[ext] lemma biprod.hom_ext {X Y Z : C} [has_binary_biproduct X Y] (f g : Z ⟶ X ⊞ Y)
(h₀ : f ≫ biprod.fst = g ≫ biprod.fst) (h₁ : f ≫ biprod.snd = g ≫ biprod.snd) : f = g :=
binary_fan.is_limit.hom_ext (binary_biproduct.is_limit X Y) h₀ h₁
@[ext] lemma biprod.hom_ext' {X Y Z : C} [has_binary_biproduct X Y] (f g : X ⊞ Y ⟶ Z)
(h₀ : biprod.inl ≫ f = biprod.inl ≫ g) (h₁ : biprod.inr ≫ f = biprod.inr ≫ g) : f = g :=
binary_cofan.is_colimit.hom_ext (binary_biproduct.is_colimit X Y) h₀ h₁
lemma biprod.map_eq_map' {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) : biprod.map f g = biprod.map' f g :=
begin
ext,
{ simp only [map_pair_left, ι_is_colimit_map, is_limit_map_π, biprod.inl_fst_assoc, category.assoc,
←binary_bicone.to_cone_π_app_left, ←binary_biproduct.bicone_fst,
←binary_bicone.to_cocone_ι_app_left, ←binary_biproduct.bicone_inl],
simp },
{ simp only [map_pair_left, ι_is_colimit_map, is_limit_map_π, zero_comp,
biprod.inl_snd_assoc, category.assoc,
←binary_bicone.to_cone_π_app_right, ←binary_biproduct.bicone_snd,
←binary_bicone.to_cocone_ι_app_left, ←binary_biproduct.bicone_inl],
simp },
{ simp only [map_pair_right, biprod.inr_fst_assoc, ι_is_colimit_map, is_limit_map_π,
zero_comp, category.assoc,
←binary_bicone.to_cone_π_app_left, ←binary_biproduct.bicone_fst,
←binary_bicone.to_cocone_ι_app_right, ←binary_biproduct.bicone_inr],
simp },
{ simp only [map_pair_right, ι_is_colimit_map, is_limit_map_π, biprod.inr_snd_assoc, category.assoc,
←binary_bicone.to_cone_π_app_right, ←binary_biproduct.bicone_snd,
←binary_bicone.to_cocone_ι_app_right, ←binary_biproduct.bicone_inr],
simp }
end
instance biprod.inl_mono {X Y : C} [has_binary_biproduct X Y] :
split_mono (biprod.inl : X ⟶ X ⊞ Y) :=
{ retraction := biprod.desc (𝟙 X) (biprod.inr ≫ biprod.fst) }
instance biprod.inr_mono {X Y : C} [has_binary_biproduct X Y] :
split_mono (biprod.inr : Y ⟶ X ⊞ Y) :=
{ retraction := biprod.desc (biprod.inl ≫ biprod.snd) (𝟙 Y)}
instance biprod.fst_epi {X Y : C} [has_binary_biproduct X Y] :
split_epi (biprod.fst : X ⊞ Y ⟶ X) :=
{ section_ := biprod.lift (𝟙 X) (biprod.inl ≫ biprod.snd) }
instance biprod.snd_epi {X Y : C} [has_binary_biproduct X Y] :
split_epi (biprod.snd : X ⊞ Y ⟶ Y) :=
{ section_ := biprod.lift (biprod.inr ≫ biprod.fst) (𝟙 Y) }
@[simp,reassoc]
lemma biprod.map_fst {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) :
biprod.map f g ≫ biprod.fst = biprod.fst ≫ f :=
is_limit_map_π _ _ _ walking_pair.left
@[simp,reassoc]
lemma biprod.map_snd {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) :
biprod.map f g ≫ biprod.snd = biprod.snd ≫ g :=
is_limit_map_π _ _ _ walking_pair.right
-- Because `biprod.map` is defined in terms of `lim` rather than `colim`,
-- we need to provide additional `simp` lemmas.
@[simp,reassoc]
lemma biprod.inl_map {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) :
biprod.inl ≫ biprod.map f g = f ≫ biprod.inl :=
begin
rw biprod.map_eq_map',
exact ι_is_colimit_map (binary_biproduct.is_colimit W X) _ _ walking_pair.left
end
@[simp,reassoc]
lemma biprod.inr_map {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ⟶ Y) (g : X ⟶ Z) :
biprod.inr ≫ biprod.map f g = g ≫ biprod.inr :=
begin
rw biprod.map_eq_map',
exact ι_is_colimit_map (binary_biproduct.is_colimit W X) _ _ walking_pair.right
end
/-- Given a pair of isomorphisms between the summands of a pair of binary biproducts,
we obtain an isomorphism between the binary biproducts. -/
@[simps]
def biprod.map_iso {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z]
(f : W ≅ Y) (g : X ≅ Z) : W ⊞ X ≅ Y ⊞ Z :=
{ hom := biprod.map f.hom g.hom,
inv := biprod.map f.inv g.inv }
section
variables [has_binary_biproducts C]
/-- The braiding isomorphism which swaps a binary biproduct. -/
@[simps] def biprod.braiding (P Q : C) : P ⊞ Q ≅ Q ⊞ P :=
{ hom := biprod.lift biprod.snd biprod.fst,
inv := biprod.lift biprod.snd biprod.fst }
/--
An alternative formula for the braiding isomorphism which swaps a binary biproduct,
using the fact that the biproduct is a coproduct.
-/
@[simps]
def biprod.braiding' (P Q : C) : P ⊞ Q ≅ Q ⊞ P :=
{ hom := biprod.desc biprod.inr biprod.inl,
inv := biprod.desc biprod.inr biprod.inl }
lemma biprod.braiding'_eq_braiding {P Q : C} :
biprod.braiding' P Q = biprod.braiding P Q :=
by tidy
/-- The braiding isomorphism can be passed through a map by swapping the order. -/
@[reassoc] lemma biprod.braid_natural {W X Y Z : C} (f : X ⟶ Y) (g : Z ⟶ W) :
biprod.map f g ≫ (biprod.braiding _ _).hom = (biprod.braiding _ _).hom ≫ biprod.map g f :=
by tidy
@[reassoc] lemma biprod.braiding_map_braiding {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) :
(biprod.braiding X W).hom ≫ biprod.map f g ≫ (biprod.braiding Y Z).hom = biprod.map g f :=
by tidy
@[simp, reassoc] lemma biprod.symmetry' (P Q : C) :
biprod.lift biprod.snd biprod.fst ≫ biprod.lift biprod.snd biprod.fst = 𝟙 (P ⊞ Q) :=
by tidy
/-- The braiding isomorphism is symmetric. -/
@[reassoc] lemma biprod.symmetry (P Q : C) :
(biprod.braiding P Q).hom ≫ (biprod.braiding Q P).hom = 𝟙 _ :=
by simp
end
-- TODO:
-- If someone is interested, they could provide the constructions:
-- has_binary_biproducts ↔ has_finite_biproducts
end category_theory.limits
namespace category_theory.limits
section preadditive
variables {C : Type u} [category.{v} C] [preadditive C]
variables {J : Type v} [decidable_eq J] [fintype J]
open category_theory.preadditive
open_locale big_operators
/--
In a preadditive category, we can construct a biproduct for `f : J → C` from
any bicone `b` for `f` satisfying `total : ∑ j : J, b.π j ≫ b.ι j = 𝟙 b.X`.
(That is, such a bicone is a limit cone and a colimit cocone.)
-/
lemma has_biproduct_of_total {f : J → C} (b : bicone f) (total : ∑ j : J, b.π j ≫ b.ι j = 𝟙 b.X) :
has_biproduct f :=
has_biproduct.mk
{ bicone := b,
is_limit :=
{ lift := λ s, ∑ j, s.π.app j ≫ b.ι j,
uniq' := λ s m h,
begin
erw [←category.comp_id m, ←total, comp_sum],
apply finset.sum_congr rfl,
intros j m,
erw [reassoc_of (h j)],
end,
fac' := λ s j,
begin
simp only [sum_comp, category.assoc, bicone.to_cone_π_app, b.ι_π, comp_dite],
-- See note [dsimp, simp].
dsimp, simp,
end },
is_colimit :=
{ desc := λ s, ∑ j, b.π j ≫ s.ι.app j,
uniq' := λ s m h,
begin
erw [←category.id_comp m, ←total, sum_comp],
apply finset.sum_congr rfl,
intros j m,
erw [category.assoc, h],
end,
fac' := λ s j,
begin
simp only [comp_sum, ←category.assoc, bicone.to_cocone_ι_app, b.ι_π, dite_comp],
dsimp, simp,
end } }
/-- In a preadditive category, if the product over `f : J → C` exists,
then the biproduct over `f` exists. -/
lemma has_biproduct.of_has_product (f : J → C) [has_product f] :
has_biproduct f :=
has_biproduct_of_total
{ X := pi_obj f,
π := limits.pi.π f,
ι := λ j, pi.lift (λ j', if h : j = j' then eq_to_hom (congr_arg f h) else 0),
ι_π := λ j j', by simp, }
(by { ext, simp [sum_comp, comp_dite] })
/-- In a preadditive category, if the coproduct over `f : J → C` exists,
then the biproduct over `f` exists. -/
lemma has_biproduct.of_has_coproduct (f : J → C) [has_coproduct f] :
has_biproduct f :=
has_biproduct_of_total
{ X := sigma_obj f,
π := λ j, sigma.desc (λ j', if h : j' = j then eq_to_hom (congr_arg f h) else 0),
ι := limits.sigma.ι f,
ι_π := λ j j', by simp, }
begin
ext,
simp only [comp_sum, limits.cofan.mk_π_app, limits.colimit.ι_desc_assoc, eq_self_iff_true,
limits.colimit.ι_desc, category.comp_id],
dsimp,
simp only [dite_comp, finset.sum_dite_eq, finset.mem_univ, if_true, category.id_comp,
eq_to_hom_refl, zero_comp],
end
/-- A preadditive category with finite products has finite biproducts. -/
lemma has_finite_biproducts.of_has_finite_products [has_finite_products C] :
has_finite_biproducts C :=
⟨λ J _ _, { has_biproduct := λ F, by exactI has_biproduct.of_has_product _ }⟩
/-- A preadditive category with finite coproducts has finite biproducts. -/
lemma has_finite_biproducts.of_has_finite_coproducts [has_finite_coproducts C] :
has_finite_biproducts C :=
⟨λ J _ _, { has_biproduct := λ F, by exactI has_biproduct.of_has_coproduct _ }⟩
section
variables {f : J → C} [has_biproduct f]
/--
In any preadditive category, any biproduct satsifies
`∑ j : J, biproduct.π f j ≫ biproduct.ι f j = 𝟙 (⨁ f)`
-/
@[simp] lemma biproduct.total : ∑ j : J, biproduct.π f j ≫ biproduct.ι f j = 𝟙 (⨁ f) :=
begin
ext j j',
simp [comp_sum, sum_comp, biproduct.ι_π, comp_dite, dite_comp],
end
lemma biproduct.lift_eq {T : C} {g : Π j, T ⟶ f j} :
biproduct.lift g = ∑ j, g j ≫ biproduct.ι f j :=
begin
ext j,
simp [sum_comp, biproduct.ι_π, comp_dite],
end
lemma biproduct.desc_eq {T : C} {g : Π j, f j ⟶ T} :
biproduct.desc g = ∑ j, biproduct.π f j ≫ g j :=
begin
ext j,
simp [comp_sum, biproduct.ι_π_assoc, dite_comp],
end
@[simp, reassoc] lemma biproduct.lift_desc {T U : C} {g : Π j, T ⟶ f j} {h : Π j, f j ⟶ U} :
biproduct.lift g ≫ biproduct.desc h = ∑ j : J, g j ≫ h j :=
by simp [biproduct.lift_eq, biproduct.desc_eq, comp_sum, sum_comp, biproduct.ι_π_assoc,
comp_dite, dite_comp]
lemma biproduct.map_eq [has_finite_biproducts C] {f g : J → C} {h : Π j, f j ⟶ g j} :
biproduct.map h = ∑ j : J, biproduct.π f j ≫ h j ≫ biproduct.ι g j :=
begin
ext,
simp [biproduct.ι_π, biproduct.ι_π_assoc, comp_sum, sum_comp, comp_dite, dite_comp],
end
end
/--
In a preadditive category, we can construct a binary biproduct for `X Y : C` from
any binary bicone `b` satisfying `total : b.fst ≫ b.inl + b.snd ≫ b.inr = 𝟙 b.X`.
(That is, such a bicone is a limit cone and a colimit cocone.)
-/
lemma has_binary_biproduct_of_total {X Y : C} (b : binary_bicone X Y)
(total : b.fst ≫ b.inl + b.snd ≫ b.inr = 𝟙 b.X) :
has_binary_biproduct X Y :=
has_binary_biproduct.mk
{ bicone := b,
is_limit :=
{ lift := λ s, binary_fan.fst s ≫ b.inl +
binary_fan.snd s ≫ b.inr,
uniq' := λ s m h, by erw [←category.comp_id m, ←total,
comp_add, reassoc_of (h walking_pair.left), reassoc_of (h walking_pair.right)],
fac' := λ s j, by cases j; simp, },
is_colimit :=
{ desc := λ s, b.fst ≫ binary_cofan.inl s +
b.snd ≫ binary_cofan.inr s,
uniq' := λ s m h, by erw [←category.id_comp m, ←total,
add_comp, category.assoc, category.assoc, h walking_pair.left, h walking_pair.right],
fac' := λ s j, by cases j; simp, } }
/-- In a preadditive category, if the product of `X` and `Y` exists, then the
binary biproduct of `X` and `Y` exists. -/
lemma has_binary_biproduct.of_has_binary_product (X Y : C) [has_binary_product X Y] :
has_binary_biproduct X Y :=
has_binary_biproduct_of_total
{ X := X ⨯ Y,
fst := category_theory.limits.prod.fst,
snd := category_theory.limits.prod.snd,
inl := prod.lift (𝟙 X) 0,
inr := prod.lift 0 (𝟙 Y) }
begin
ext; simp [add_comp],
end
/-- In a preadditive category, if all binary products exist, then all binary biproducts exist. -/
lemma has_binary_biproducts.of_has_binary_products [has_binary_products C] :
has_binary_biproducts C :=
{ has_binary_biproduct := λ X Y, has_binary_biproduct.of_has_binary_product X Y, }
/-- In a preadditive category, if the coproduct of `X` and `Y` exists, then the
binary biproduct of `X` and `Y` exists. -/
lemma has_binary_biproduct.of_has_binary_coproduct (X Y : C) [has_binary_coproduct X Y] :
has_binary_biproduct X Y :=
has_binary_biproduct_of_total
{ X := X ⨿ Y,
fst := coprod.desc (𝟙 X) 0,
snd := coprod.desc 0 (𝟙 Y),
inl := category_theory.limits.coprod.inl,
inr := category_theory.limits.coprod.inr }
begin
ext; simp [add_comp],
end
/-- In a preadditive category, if all binary coproducts exist, then all binary biproducts exist. -/
lemma has_binary_biproducts.of_has_binary_coproducts [has_binary_coproducts C] :
has_binary_biproducts C :=
{ has_binary_biproduct := λ X Y, has_binary_biproduct.of_has_binary_coproduct X Y, }
section
variables {X Y : C} [has_binary_biproduct X Y]
/--
In any preadditive category, any binary biproduct satsifies
`biprod.fst ≫ biprod.inl + biprod.snd ≫ biprod.inr = 𝟙 (X ⊞ Y)`.
-/
@[simp] lemma biprod.total : biprod.fst ≫ biprod.inl + biprod.snd ≫ biprod.inr = 𝟙 (X ⊞ Y) :=
begin
ext; simp [add_comp],
end
lemma biprod.lift_eq {T : C} {f : T ⟶ X} {g : T ⟶ Y} :
biprod.lift f g = f ≫ biprod.inl + g ≫ biprod.inr :=
begin
ext; simp [add_comp],
end
lemma biprod.desc_eq {T : C} {f : X ⟶ T} {g : Y ⟶ T} :
biprod.desc f g = biprod.fst ≫ f + biprod.snd ≫ g :=
begin
ext; simp [add_comp],
end
@[simp, reassoc] lemma biprod.lift_desc {T U : C} {f : T ⟶ X} {g : T ⟶ Y} {h : X ⟶ U} {i : Y ⟶ U} :
biprod.lift f g ≫ biprod.desc h i = f ≫ h + g ≫ i :=
by simp [biprod.lift_eq, biprod.desc_eq]
lemma biprod.map_eq [has_binary_biproducts C] {W X Y Z : C} {f : W ⟶ Y} {g : X ⟶ Z} :
biprod.map f g = biprod.fst ≫ f ≫ biprod.inl + biprod.snd ≫ g ≫ biprod.inr :=
by apply biprod.hom_ext; apply biprod.hom_ext'; simp
end
end preadditive
end category_theory.limits
|
5595fbd1f10998d7fc409b22fb63b334a4ab6674 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/gcd_monoid/basic.lean | bce40f5f38246f1ef99d2d2ecb3cf634274c2d62 | [
"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 | 50,879 | lean | /-
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Jens Wagemaker
-/
import algebra.associated
import algebra.group_power.lemmas
import algebra.ring.regular
/-!
# Monoids with normalization functions, `gcd`, and `lcm`
This file defines extra structures on `cancel_comm_monoid_with_zero`s, including `is_domain`s.
## Main Definitions
* `normalization_monoid`
* `gcd_monoid`
* `normalized_gcd_monoid`
* `gcd_monoid_of_gcd`, `gcd_monoid_of_exists_gcd`, `normalized_gcd_monoid_of_gcd`,
`normalized_gcd_monoid_of_exists_gcd`
* `gcd_monoid_of_lcm`, `gcd_monoid_of_exists_lcm`, `normalized_gcd_monoid_of_lcm`,
`normalized_gcd_monoid_of_exists_lcm`
For the `normalized_gcd_monoid` instances on `ℕ` and `ℤ`, see `ring_theory.int.basic`.
## Implementation Notes
* `normalization_monoid` is defined by assigning to each element a `norm_unit` such that multiplying
by that unit normalizes the monoid, and `normalize` is an idempotent monoid homomorphism. This
definition as currently implemented does casework on `0`.
* `gcd_monoid` contains the definitions of `gcd` and `lcm` with the usual properties. They are
both determined up to a unit.
* `normalized_gcd_monoid` extends `normalization_monoid`, so the `gcd` and `lcm` are always
normalized. This makes `gcd`s of polynomials easier to work with, but excludes Euclidean domains,
and monoids without zero.
* `gcd_monoid_of_gcd` and `normalized_gcd_monoid_of_gcd` noncomputably construct a `gcd_monoid`
(resp. `normalized_gcd_monoid`) structure just from the `gcd` and its properties.
* `gcd_monoid_of_exists_gcd` and `normalized_gcd_monoid_of_exists_gcd` noncomputably construct a
`gcd_monoid` (resp. `normalized_gcd_monoid`) structure just from a proof that any two elements
have a (not necessarily normalized) `gcd`.
* `gcd_monoid_of_lcm` and `normalized_gcd_monoid_of_lcm` noncomputably construct a `gcd_monoid`
(resp. `normalized_gcd_monoid`) structure just from the `lcm` and its properties.
* `gcd_monoid_of_exists_lcm` and `normalized_gcd_monoid_of_exists_lcm` noncomputably construct a
`gcd_monoid` (resp. `normalized_gcd_monoid`) structure just from a proof that any two elements
have a (not necessarily normalized) `lcm`.
## TODO
* Port GCD facts about nats, definition of coprime
* Generalize normalization monoids to commutative (cancellative) monoids with or without zero
## Tags
divisibility, gcd, lcm, normalize
-/
variables {α : Type*}
/-- Normalization monoid: multiplying with `norm_unit` gives a normal form for associated
elements. -/
@[protect_proj] class normalization_monoid (α : Type*)
[cancel_comm_monoid_with_zero α] :=
(norm_unit : α → αˣ)
(norm_unit_zero : norm_unit 0 = 1)
(norm_unit_mul : ∀{a b}, a ≠ 0 → b ≠ 0 → norm_unit (a * b) = norm_unit a * norm_unit b)
(norm_unit_coe_units : ∀(u : αˣ), norm_unit u = u⁻¹)
export normalization_monoid (norm_unit norm_unit_zero norm_unit_mul norm_unit_coe_units)
attribute [simp] norm_unit_coe_units norm_unit_zero norm_unit_mul
section normalization_monoid
variables [cancel_comm_monoid_with_zero α] [normalization_monoid α]
@[simp] theorem norm_unit_one : norm_unit (1:α) = 1 :=
norm_unit_coe_units 1
/-- Chooses an element of each associate class, by multiplying by `norm_unit` -/
def normalize : α →*₀ α :=
{ to_fun := λ x, x * norm_unit x,
map_zero' := by simp,
map_one' := by rw [norm_unit_one, units.coe_one, mul_one],
map_mul' := λ x y,
classical.by_cases (λ hx : x = 0, by rw [hx, zero_mul, zero_mul, zero_mul]) $ λ hx,
classical.by_cases (λ hy : y = 0, by rw [hy, mul_zero, zero_mul, mul_zero]) $ λ hy,
by simp only [norm_unit_mul hx hy, units.coe_mul]; simp only [mul_assoc, mul_left_comm y], }
theorem associated_normalize (x : α) : associated x (normalize x) :=
⟨_, rfl⟩
theorem normalize_associated (x : α) : associated (normalize x) x :=
(associated_normalize _).symm
lemma associated_normalize_iff {x y : α} :
associated x (normalize y) ↔ associated x y :=
⟨λ h, h.trans (normalize_associated y), λ h, h.trans (associated_normalize y)⟩
lemma normalize_associated_iff {x y : α} :
associated (normalize x) y ↔ associated x y :=
⟨λ h, (associated_normalize _).trans h, λ h, (normalize_associated _).trans h⟩
lemma associates.mk_normalize (x : α) : associates.mk (normalize x) = associates.mk x :=
associates.mk_eq_mk_iff_associated.2 (normalize_associated _)
@[simp] lemma normalize_apply (x : α) : normalize x = x * norm_unit x := rfl
@[simp] lemma normalize_zero : normalize (0 : α) = 0 := normalize.map_zero
@[simp] lemma normalize_one : normalize (1 : α) = 1 := normalize.map_one
lemma normalize_coe_units (u : αˣ) : normalize (u : α) = 1 := by simp
lemma normalize_eq_zero {x : α} : normalize x = 0 ↔ x = 0 :=
⟨λ hx, (associated_zero_iff_eq_zero x).1 $ hx ▸ associated_normalize _,
by rintro rfl; exact normalize_zero⟩
lemma normalize_eq_one {x : α} : normalize x = 1 ↔ is_unit x :=
⟨λ hx, is_unit_iff_exists_inv.2 ⟨_, hx⟩, λ ⟨u, hu⟩, hu ▸ normalize_coe_units u⟩
@[simp] theorem norm_unit_mul_norm_unit (a : α) : norm_unit (a * norm_unit a) = 1 :=
begin
nontriviality α using [subsingleton.elim a 0],
obtain rfl|h := eq_or_ne a 0,
{ rw [norm_unit_zero, zero_mul, norm_unit_zero] },
{ rw [norm_unit_mul h (units.ne_zero _), norm_unit_coe_units, mul_inv_eq_one] }
end
theorem normalize_idem (x : α) : normalize (normalize x) = normalize x := by simp
theorem normalize_eq_normalize {a b : α}
(hab : a ∣ b) (hba : b ∣ a) : normalize a = normalize b :=
begin
nontriviality α,
rcases associated_of_dvd_dvd hab hba with ⟨u, rfl⟩,
refine classical.by_cases (by rintro rfl; simp only [zero_mul]) (assume ha : a ≠ 0, _),
suffices : a * ↑(norm_unit a) = a * ↑u * ↑(norm_unit a) * ↑u⁻¹,
by simpa only [normalize_apply, mul_assoc, norm_unit_mul ha u.ne_zero, norm_unit_coe_units],
calc a * ↑(norm_unit a) = a * ↑(norm_unit a) * ↑u * ↑u⁻¹:
(units.mul_inv_cancel_right _ _).symm
... = a * ↑u * ↑(norm_unit a) * ↑u⁻¹ : by rw mul_right_comm a
end
lemma normalize_eq_normalize_iff {x y : α} : normalize x = normalize y ↔ x ∣ y ∧ y ∣ x :=
⟨λ h, ⟨units.dvd_mul_right.1 ⟨_, h.symm⟩, units.dvd_mul_right.1 ⟨_, h⟩⟩,
λ ⟨hxy, hyx⟩, normalize_eq_normalize hxy hyx⟩
theorem dvd_antisymm_of_normalize_eq {a b : α}
(ha : normalize a = a) (hb : normalize b = b) (hab : a ∣ b) (hba : b ∣ a) :
a = b :=
ha ▸ hb ▸ normalize_eq_normalize hab hba
--can be proven by simp
lemma dvd_normalize_iff {a b : α} : a ∣ normalize b ↔ a ∣ b :=
units.dvd_mul_right
--can be proven by simp
lemma normalize_dvd_iff {a b : α} : normalize a ∣ b ↔ a ∣ b :=
units.mul_right_dvd
end normalization_monoid
namespace associates
variables [cancel_comm_monoid_with_zero α] [normalization_monoid α]
local attribute [instance] associated.setoid
/-- Maps an element of `associates` back to the normalized element of its associate class -/
protected def out : associates α → α :=
quotient.lift (normalize : α → α) $ λ a b ⟨u, hu⟩, hu ▸
normalize_eq_normalize ⟨_, rfl⟩ (units.mul_right_dvd.2 $ dvd_refl a)
@[simp] lemma out_mk (a : α) : (associates.mk a).out = normalize a := rfl
@[simp] lemma out_one : (1 : associates α).out = 1 :=
normalize_one
lemma out_mul (a b : associates α) : (a * b).out = a.out * b.out :=
quotient.induction_on₂ a b $ assume a b,
by simp only [associates.quotient_mk_eq_mk, out_mk, mk_mul_mk, normalize.map_mul]
lemma dvd_out_iff (a : α) (b : associates α) : a ∣ b.out ↔ associates.mk a ≤ b :=
quotient.induction_on b $
by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff]
lemma out_dvd_iff (a : α) (b : associates α) : b.out ∣ a ↔ b ≤ associates.mk a :=
quotient.induction_on b $
by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff]
@[simp] lemma out_top : (⊤ : associates α).out = 0 :=
normalize_zero
@[simp] lemma normalize_out (a : associates α) : normalize a.out = a.out :=
quotient.induction_on a normalize_idem
@[simp] lemma mk_out (a : associates α) : associates.mk (a.out) = a :=
quotient.induction_on a mk_normalize
lemma out_injective : function.injective (associates.out : _ → α) :=
function.left_inverse.injective mk_out
end associates
/-- GCD monoid: a `cancel_comm_monoid_with_zero` with `gcd` (greatest common divisor) and
`lcm` (least common multiple) operations, determined up to a unit. The type class focuses on `gcd`
and we derive the corresponding `lcm` facts from `gcd`.
-/
@[protect_proj] class gcd_monoid (α : Type*) [cancel_comm_monoid_with_zero α] :=
(gcd : α → α → α)
(lcm : α → α → α)
(gcd_dvd_left : ∀a b, gcd a b ∣ a)
(gcd_dvd_right : ∀a b, gcd a b ∣ b)
(dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b)
(gcd_mul_lcm : ∀a b, associated (gcd a b * lcm a b) (a * b))
(lcm_zero_left : ∀a, lcm 0 a = 0)
(lcm_zero_right : ∀a, lcm a 0 = 0)
/-- Normalized GCD monoid: a `cancel_comm_monoid_with_zero` with normalization and `gcd`
(greatest common divisor) and `lcm` (least common multiple) operations. In this setting `gcd` and
`lcm` form a bounded lattice on the associated elements where `gcd` is the infimum, `lcm` is the
supremum, `1` is bottom, and `0` is top. The type class focuses on `gcd` and we derive the
corresponding `lcm` facts from `gcd`.
-/
class normalized_gcd_monoid (α : Type*) [cancel_comm_monoid_with_zero α]
extends normalization_monoid α, gcd_monoid α :=
(normalize_gcd : ∀a b, normalize (gcd a b) = gcd a b)
(normalize_lcm : ∀a b, normalize (lcm a b) = lcm a b)
export gcd_monoid (gcd lcm gcd_dvd_left gcd_dvd_right dvd_gcd lcm_zero_left lcm_zero_right)
attribute [simp] lcm_zero_left lcm_zero_right
section gcd_monoid
variables [cancel_comm_monoid_with_zero α]
@[simp] theorem normalize_gcd [normalized_gcd_monoid α] : ∀a b:α, normalize (gcd a b) = gcd a b :=
normalized_gcd_monoid.normalize_gcd
theorem gcd_mul_lcm [gcd_monoid α] : ∀a b:α, associated (gcd a b * lcm a b) (a * b) :=
gcd_monoid.gcd_mul_lcm
section gcd
theorem dvd_gcd_iff [gcd_monoid α] (a b c : α) : a ∣ gcd b c ↔ (a ∣ b ∧ a ∣ c) :=
iff.intro
(assume h, ⟨h.trans (gcd_dvd_left _ _), h.trans (gcd_dvd_right _ _)⟩)
(assume ⟨hab, hac⟩, dvd_gcd hab hac)
theorem gcd_comm [normalized_gcd_monoid α] (a b : α) : gcd a b = gcd b a :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _)
(dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
(dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
theorem gcd_comm' [gcd_monoid α] (a b : α) : associated (gcd a b) (gcd b a) :=
associated_of_dvd_dvd
(dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
(dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _))
theorem gcd_assoc [normalized_gcd_monoid α] (m n k : α) : gcd (gcd m n) k = gcd m (gcd n k) :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _)
(dvd_gcd
((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n))
(dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n))
(gcd_dvd_right (gcd m n) k)))
(dvd_gcd
(dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k)))
((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k)))
theorem gcd_assoc' [gcd_monoid α] (m n k : α) : associated (gcd (gcd m n) k) (gcd m (gcd n k)) :=
associated_of_dvd_dvd
(dvd_gcd
((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n))
(dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n))
(gcd_dvd_right (gcd m n) k)))
(dvd_gcd
(dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k)))
((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k)))
instance [normalized_gcd_monoid α] : is_commutative α gcd := ⟨gcd_comm⟩
instance [normalized_gcd_monoid α] : is_associative α gcd := ⟨gcd_assoc⟩
theorem gcd_eq_normalize [normalized_gcd_monoid α] {a b c : α}
(habc : gcd a b ∣ c) (hcab : c ∣ gcd a b) :
gcd a b = normalize c :=
normalize_gcd a b ▸ normalize_eq_normalize habc hcab
@[simp] theorem gcd_zero_left [normalized_gcd_monoid α] (a : α) : gcd 0 a = normalize a :=
gcd_eq_normalize (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a))
theorem gcd_zero_left' [gcd_monoid α] (a : α) : associated (gcd 0 a) a :=
associated_of_dvd_dvd (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a))
@[simp] theorem gcd_zero_right [normalized_gcd_monoid α] (a : α) : gcd a 0 = normalize a :=
gcd_eq_normalize (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _))
theorem gcd_zero_right' [gcd_monoid α] (a : α) : associated (gcd a 0) a :=
associated_of_dvd_dvd (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _))
@[simp] theorem gcd_eq_zero_iff [gcd_monoid α] (a b : α) : gcd a b = 0 ↔ a = 0 ∧ b = 0 :=
iff.intro
(assume h, let ⟨ca, ha⟩ := gcd_dvd_left a b, ⟨cb, hb⟩ := gcd_dvd_right a b in
by rw [h, zero_mul] at ha hb; exact ⟨ha, hb⟩)
(assume ⟨ha, hb⟩, by
{ rw [ha, hb, ←zero_dvd_iff],
apply dvd_gcd; refl })
@[simp] theorem gcd_one_left [normalized_gcd_monoid α] (a : α) : gcd 1 a = 1 :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_left _ _) (one_dvd _)
@[simp] theorem gcd_one_left' [gcd_monoid α] (a : α) : associated (gcd 1 a) 1 :=
associated_of_dvd_dvd (gcd_dvd_left _ _) (one_dvd _)
@[simp] theorem gcd_one_right [normalized_gcd_monoid α] (a : α) : gcd a 1 = 1 :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_right _ _) (one_dvd _)
@[simp] theorem gcd_one_right' [gcd_monoid α] (a : α) : associated (gcd a 1) 1 :=
associated_of_dvd_dvd (gcd_dvd_right _ _) (one_dvd _)
theorem gcd_dvd_gcd [gcd_monoid α] {a b c d: α} (hab : a ∣ b) (hcd : c ∣ d) : gcd a c ∣ gcd b d :=
dvd_gcd ((gcd_dvd_left _ _).trans hab) ((gcd_dvd_right _ _).trans hcd)
@[simp] theorem gcd_same [normalized_gcd_monoid α] (a : α) : gcd a a = normalize a :=
gcd_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_refl a))
@[simp] theorem gcd_mul_left [normalized_gcd_monoid α] (a b c : α) :
gcd (a * b) (a * c) = normalize a * gcd b c :=
classical.by_cases (by rintro rfl; simp only [zero_mul, gcd_zero_left, normalize_zero]) $
assume ha : a ≠ 0,
suffices gcd (a * b) (a * c) = normalize (a * gcd b c),
by simpa only [normalize.map_mul, normalize_gcd],
let ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c) in
gcd_eq_normalize
(eq.symm ▸ mul_dvd_mul_left a $ show d ∣ gcd b c, from
dvd_gcd
((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_left _ _)
((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_right _ _))
(dvd_gcd
(mul_dvd_mul_left a $ gcd_dvd_left _ _)
(mul_dvd_mul_left a $ gcd_dvd_right _ _))
theorem gcd_mul_left' [gcd_monoid α] (a b c : α) : associated (gcd (a * b) (a * c)) (a * gcd b c) :=
begin
obtain rfl|ha := eq_or_ne a 0,
{ simp only [zero_mul, gcd_zero_left'] },
obtain ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c),
apply associated_of_dvd_dvd,
{ rw eq,
apply mul_dvd_mul_left,
exact dvd_gcd
((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_left _ _)
((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_right _ _) },
{ exact (dvd_gcd
(mul_dvd_mul_left a $ gcd_dvd_left _ _)
(mul_dvd_mul_left a $ gcd_dvd_right _ _)) },
end
@[simp] theorem gcd_mul_right [normalized_gcd_monoid α] (a b c : α) :
gcd (b * a) (c * a) = gcd b c * normalize a :=
by simp only [mul_comm, gcd_mul_left]
@[simp] theorem gcd_mul_right' [gcd_monoid α] (a b c : α) :
associated (gcd (b * a) (c * a)) (gcd b c * a) :=
by simp only [mul_comm, gcd_mul_left']
theorem gcd_eq_left_iff [normalized_gcd_monoid α] (a b : α) (h : normalize a = a) :
gcd a b = a ↔ a ∣ b :=
iff.intro (assume eq, eq ▸ gcd_dvd_right _ _) $
assume hab, dvd_antisymm_of_normalize_eq (normalize_gcd _ _) h (gcd_dvd_left _ _)
(dvd_gcd (dvd_refl a) hab)
theorem gcd_eq_right_iff [normalized_gcd_monoid α] (a b : α) (h : normalize b = b) :
gcd a b = b ↔ b ∣ a :=
by simpa only [gcd_comm a b] using gcd_eq_left_iff b a h
theorem gcd_dvd_gcd_mul_left [gcd_monoid α] (m n k : α) : gcd m n ∣ gcd (k * m) n :=
gcd_dvd_gcd (dvd_mul_left _ _) dvd_rfl
theorem gcd_dvd_gcd_mul_right [gcd_monoid α] (m n k : α) : gcd m n ∣ gcd (m * k) n :=
gcd_dvd_gcd (dvd_mul_right _ _) dvd_rfl
theorem gcd_dvd_gcd_mul_left_right [gcd_monoid α] (m n k : α) : gcd m n ∣ gcd m (k * n) :=
gcd_dvd_gcd dvd_rfl (dvd_mul_left _ _)
theorem gcd_dvd_gcd_mul_right_right [gcd_monoid α] (m n k : α) : gcd m n ∣ gcd m (n * k) :=
gcd_dvd_gcd dvd_rfl (dvd_mul_right _ _)
theorem associated.gcd_eq_left [normalized_gcd_monoid α] {m n : α} (h : associated m n) (k : α) :
gcd m k = gcd n k :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _)
(gcd_dvd_gcd h.dvd dvd_rfl)
(gcd_dvd_gcd h.symm.dvd dvd_rfl)
theorem associated.gcd_eq_right [normalized_gcd_monoid α] {m n : α} (h : associated m n) (k : α) :
gcd k m = gcd k n :=
dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _)
(gcd_dvd_gcd dvd_rfl h.dvd)
(gcd_dvd_gcd dvd_rfl h.symm.dvd)
lemma dvd_gcd_mul_of_dvd_mul [gcd_monoid α] {m n k : α} (H : k ∣ m * n) : k ∣ (gcd k m) * n :=
(dvd_gcd (dvd_mul_right _ n) H).trans (gcd_mul_right' n k m).dvd
lemma dvd_mul_gcd_of_dvd_mul [gcd_monoid α] {m n k : α} (H : k ∣ m * n) : k ∣ m * gcd k n :=
by { rw mul_comm at H ⊢, exact dvd_gcd_mul_of_dvd_mul H }
/-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`.
In other words, the nonzero elements of a `gcd_monoid` form a decomposition monoid
(more widely known as a pre-Schreier domain in the context of rings).
Note: In general, this representation is highly non-unique.
See `nat.prod_dvd_and_dvd_of_dvd_prod` for a constructive version on `ℕ`. -/
lemma exists_dvd_and_dvd_of_dvd_mul [gcd_monoid α] {m n k : α} (H : k ∣ m * n) :
∃ d₁ d₂, d₁ ∣ m ∧ d₂ ∣ n ∧ k = d₁ * d₂ :=
begin
by_cases h0 : gcd k m = 0,
{ rw gcd_eq_zero_iff at h0,
rcases h0 with ⟨rfl, rfl⟩,
refine ⟨0, n, dvd_refl 0, dvd_refl n, _⟩,
simp },
{ obtain ⟨a, ha⟩ := gcd_dvd_left k m,
refine ⟨gcd k m, a, gcd_dvd_right _ _, _, ha⟩,
suffices h : gcd k m * a ∣ gcd k m * n,
{ cases h with b hb,
use b,
rw mul_assoc at hb,
apply mul_left_cancel₀ h0 hb },
rw ← ha,
exact dvd_gcd_mul_of_dvd_mul H }
end
lemma dvd_mul [gcd_monoid α] {k m n : α} :
k ∣ (m * n) ↔ ∃ d₁ d₂, d₁ ∣ m ∧ d₂ ∣ n ∧ k = d₁ * d₂ :=
begin
refine ⟨exists_dvd_and_dvd_of_dvd_mul, _⟩,
rintro ⟨d₁, d₂, hy, hz, rfl⟩,
exact mul_dvd_mul hy hz,
end
theorem gcd_mul_dvd_mul_gcd [gcd_monoid α] (k m n : α) : gcd k (m * n) ∣ gcd k m * gcd k n :=
begin
obtain ⟨m', n', hm', hn', h⟩ := exists_dvd_and_dvd_of_dvd_mul (gcd_dvd_right k (m * n)),
replace h : gcd k (m * n) = m' * n' := h,
rw h,
have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _,
apply mul_dvd_mul,
{ have hm'k : m' ∣ k := (dvd_mul_right m' n').trans hm'n',
exact dvd_gcd hm'k hm' },
{ have hn'k : n' ∣ k := (dvd_mul_left n' m').trans hm'n',
exact dvd_gcd hn'k hn' }
end
theorem gcd_pow_right_dvd_pow_gcd [gcd_monoid α] {a b : α} {k : ℕ} :
gcd a (b ^ k) ∣ (gcd a b) ^ k :=
begin
by_cases hg : gcd a b = 0,
{ rw gcd_eq_zero_iff at hg,
rcases hg with ⟨rfl, rfl⟩,
exact (gcd_zero_left' (0 ^ k : α)).dvd.trans
(pow_dvd_pow_of_dvd (gcd_zero_left' (0 : α)).symm.dvd _) },
{ induction k with k hk,
{ simp only [pow_zero],
exact (gcd_one_right' a).dvd, },
rw [pow_succ, pow_succ],
transitivity gcd a b * gcd a (b ^ k),
apply gcd_mul_dvd_mul_gcd a b (b ^ k),
exact (mul_dvd_mul_iff_left hg).mpr hk }
end
theorem gcd_pow_left_dvd_pow_gcd [gcd_monoid α] {a b : α} {k : ℕ} :
gcd (a ^ k) b ∣ (gcd a b) ^ k :=
calc gcd (a ^ k) b
∣ gcd b (a ^ k) : (gcd_comm' _ _).dvd
... ∣ (gcd b a) ^ k : gcd_pow_right_dvd_pow_gcd
... ∣ (gcd a b) ^ k : pow_dvd_pow_of_dvd (gcd_comm' _ _).dvd _
theorem pow_dvd_of_mul_eq_pow [gcd_monoid α] {a b c d₁ d₂ : α} (ha : a ≠ 0)
(hab : is_unit (gcd a b)) {k : ℕ} (h : a * b = c ^ k) (hc : c = d₁ * d₂)
(hd₁ : d₁ ∣ a) : d₁ ^ k ≠ 0 ∧ d₁ ^ k ∣ a :=
begin
have h1 : is_unit (gcd (d₁ ^ k) b),
{ apply is_unit_of_dvd_one,
transitivity (gcd d₁ b) ^ k,
{ exact gcd_pow_left_dvd_pow_gcd },
{ apply is_unit.dvd, apply is_unit.pow, apply is_unit_of_dvd_one,
apply dvd_trans _ hab.dvd,
apply gcd_dvd_gcd hd₁ (dvd_refl b) } },
have h2 : d₁ ^ k ∣ a * b, { use d₂ ^ k, rw [h, hc], exact mul_pow d₁ d₂ k },
rw mul_comm at h2,
have h3 : d₁ ^ k ∣ a,
{ apply (dvd_gcd_mul_of_dvd_mul h2).trans,
rw is_unit.mul_left_dvd _ _ _ h1 },
have h4 : d₁ ^ k ≠ 0,
{ intro hdk, rw hdk at h3, apply absurd (zero_dvd_iff.mp h3) ha },
exact ⟨h4, h3⟩,
end
theorem exists_associated_pow_of_mul_eq_pow [gcd_monoid α] {a b c : α}
(hab : is_unit (gcd a b)) {k : ℕ}
(h : a * b = c ^ k) : ∃ (d : α), associated (d ^ k) a :=
begin
casesI subsingleton_or_nontrivial α,
{ use 0, rw [subsingleton.elim a (0 ^ k)] },
by_cases ha : a = 0,
{ use 0, rw ha,
obtain (rfl | hk) := k.eq_zero_or_pos,
{ exfalso, revert h, rw [ha, zero_mul, pow_zero], apply zero_ne_one },
{ rw zero_pow hk } },
by_cases hb : b = 0,
{ use 1, rw [one_pow],
apply (associated_one_iff_is_unit.mpr hab).symm.trans,
rw hb,
exact gcd_zero_right' a },
obtain (rfl | hk) := k.eq_zero_or_pos,
{ use 1, rw pow_zero at h ⊢, use units.mk_of_mul_eq_one _ _ h,
rw [units.coe_mk_of_mul_eq_one, one_mul] },
have hc : c ∣ a * b, { rw h, exact dvd_pow_self _ hk.ne' },
obtain ⟨d₁, d₂, hd₁, hd₂, hc⟩ := exists_dvd_and_dvd_of_dvd_mul hc,
use d₁,
obtain ⟨h0₁, ⟨a', ha'⟩⟩ := pow_dvd_of_mul_eq_pow ha hab h hc hd₁,
rw [mul_comm] at h hc,
rw (gcd_comm' a b).is_unit_iff at hab,
obtain ⟨h0₂, ⟨b', hb'⟩⟩ := pow_dvd_of_mul_eq_pow hb hab h hc hd₂,
rw [ha', hb', hc, mul_pow] at h,
have h' : a' * b' = 1,
{ apply (mul_right_inj' h0₁).mp, rw mul_one,
apply (mul_right_inj' h0₂).mp, rw ← h,
rw [mul_assoc, mul_comm a', ← mul_assoc _ b', ← mul_assoc b', mul_comm b'] },
use units.mk_of_mul_eq_one _ _ h',
rw [units.coe_mk_of_mul_eq_one, ha']
end
theorem exists_eq_pow_of_mul_eq_pow [gcd_monoid α] [unique αˣ] {a b c : α}
(hab : is_unit (gcd a b)) {k : ℕ}
(h : a * b = c ^ k) : ∃ (d : α), a = d ^ k :=
let ⟨d, hd⟩ := exists_associated_pow_of_mul_eq_pow hab h in ⟨d, (associated_iff_eq.mp hd).symm⟩
lemma gcd_greatest {α : Type*} [cancel_comm_monoid_with_zero α] [normalized_gcd_monoid α]
{a b d : α} (hda : d ∣ a) (hdb : d ∣ b)
(hd : ∀ e : α, e ∣ a → e ∣ b → e ∣ d) : gcd_monoid.gcd a b = normalize d :=
begin
have h := hd _ (gcd_monoid.gcd_dvd_left a b) (gcd_monoid.gcd_dvd_right a b),
exact gcd_eq_normalize h (gcd_monoid.dvd_gcd hda hdb),
end
lemma gcd_greatest_associated {α : Type*} [cancel_comm_monoid_with_zero α] [gcd_monoid α]
{a b d : α} (hda : d ∣ a) (hdb : d ∣ b)
(hd : ∀ e : α, e ∣ a → e ∣ b → e ∣ d) : associated d (gcd_monoid.gcd a b) :=
begin
have h := hd _ (gcd_monoid.gcd_dvd_left a b) (gcd_monoid.gcd_dvd_right a b),
exact associated_of_dvd_dvd (gcd_monoid.dvd_gcd hda hdb) h,
end
lemma is_unit_gcd_of_eq_mul_gcd {α : Type*} [cancel_comm_monoid_with_zero α] [gcd_monoid α]
{x y x' y' : α} (ex : x = gcd x y * x') (ey : y = gcd x y * y') (h : gcd x y ≠ 0) :
is_unit (gcd x' y') :=
begin
rw ← associated_one_iff_is_unit,
refine associated.of_mul_left _ (associated.refl $ gcd x y) h,
convert (gcd_mul_left' _ _ _).symm using 1,
rw [← ex, ← ey, mul_one],
end
lemma extract_gcd {α : Type*} [cancel_comm_monoid_with_zero α] [gcd_monoid α] (x y : α) :
∃ x' y', x = gcd x y * x' ∧ y = gcd x y * y' ∧ is_unit (gcd x' y') :=
begin
by_cases h : gcd x y = 0,
{ obtain ⟨rfl, rfl⟩ := (gcd_eq_zero_iff x y).1 h,
simp_rw ← associated_one_iff_is_unit,
exact ⟨1, 1, by rw [h, zero_mul], by rw [h, zero_mul], gcd_one_left' 1⟩ },
obtain ⟨x', ex⟩ := gcd_dvd_left x y,
obtain ⟨y', ey⟩ := gcd_dvd_right x y,
exact ⟨x', y', ex, ey, is_unit_gcd_of_eq_mul_gcd ex ey h⟩,
end
end gcd
section lcm
lemma lcm_dvd_iff [gcd_monoid α] {a b c : α} : lcm a b ∣ c ↔ a ∣ c ∧ b ∣ c :=
begin
by_cases this : a = 0 ∨ b = 0,
{ rcases this with rfl | rfl;
simp only [iff_def, lcm_zero_left, lcm_zero_right, zero_dvd_iff, dvd_zero,
eq_self_iff_true, and_true, imp_true_iff] {contextual:=tt} },
{ obtain ⟨h1, h2⟩ := not_or_distrib.1 this,
have h : gcd a b ≠ 0, from λ H, h1 ((gcd_eq_zero_iff _ _).1 H).1,
rw [← mul_dvd_mul_iff_left h, (gcd_mul_lcm a b).dvd_iff_dvd_left,
←(gcd_mul_right' c a b).dvd_iff_dvd_right, dvd_gcd_iff, mul_comm b c,
mul_dvd_mul_iff_left h1, mul_dvd_mul_iff_right h2, and_comm] }
end
lemma dvd_lcm_left [gcd_monoid α] (a b : α) : a ∣ lcm a b :=
(lcm_dvd_iff.1 (dvd_refl (lcm a b))).1
lemma dvd_lcm_right [gcd_monoid α] (a b : α) : b ∣ lcm a b :=
(lcm_dvd_iff.1 (dvd_refl (lcm a b))).2
lemma lcm_dvd [gcd_monoid α] {a b c : α} (hab : a ∣ b) (hcb : c ∣ b) : lcm a c ∣ b :=
lcm_dvd_iff.2 ⟨hab, hcb⟩
@[simp] theorem lcm_eq_zero_iff [gcd_monoid α] (a b : α) : lcm a b = 0 ↔ a = 0 ∨ b = 0 :=
iff.intro
(assume h : lcm a b = 0,
have associated (a * b) 0 := (gcd_mul_lcm a b).symm.trans $
by rw [h, mul_zero],
by simpa only [associated_zero_iff_eq_zero, mul_eq_zero])
(by rintro (rfl | rfl); [apply lcm_zero_left, apply lcm_zero_right])
@[simp] lemma normalize_lcm [normalized_gcd_monoid α] (a b : α) : normalize (lcm a b) = lcm a b :=
normalized_gcd_monoid.normalize_lcm a b
theorem lcm_comm [normalized_gcd_monoid α] (a b : α) : lcm a b = lcm b a :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _)
(lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
(lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
theorem lcm_comm' [gcd_monoid α] (a b : α) : associated (lcm a b) (lcm b a) :=
associated_of_dvd_dvd
(lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
(lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _))
theorem lcm_assoc [normalized_gcd_monoid α] (m n k : α) : lcm (lcm m n) k = lcm m (lcm n k) :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _)
(lcm_dvd
(lcm_dvd (dvd_lcm_left _ _) ((dvd_lcm_left _ _).trans (dvd_lcm_right _ _)))
((dvd_lcm_right _ _).trans (dvd_lcm_right _ _)))
(lcm_dvd
((dvd_lcm_left _ _).trans (dvd_lcm_left _ _))
(lcm_dvd ((dvd_lcm_right _ _).trans (dvd_lcm_left _ _)) (dvd_lcm_right _ _)))
theorem lcm_assoc' [gcd_monoid α] (m n k : α) : associated (lcm (lcm m n) k) (lcm m (lcm n k)) :=
associated_of_dvd_dvd
(lcm_dvd
(lcm_dvd (dvd_lcm_left _ _) ((dvd_lcm_left _ _).trans (dvd_lcm_right _ _)))
((dvd_lcm_right _ _).trans (dvd_lcm_right _ _)))
(lcm_dvd
((dvd_lcm_left _ _).trans (dvd_lcm_left _ _))
(lcm_dvd ((dvd_lcm_right _ _).trans (dvd_lcm_left _ _)) (dvd_lcm_right _ _)))
instance [normalized_gcd_monoid α] : is_commutative α lcm := ⟨lcm_comm⟩
instance [normalized_gcd_monoid α] : is_associative α lcm := ⟨lcm_assoc⟩
lemma lcm_eq_normalize [normalized_gcd_monoid α] {a b c : α}
(habc : lcm a b ∣ c) (hcab : c ∣ lcm a b) :
lcm a b = normalize c :=
normalize_lcm a b ▸ normalize_eq_normalize habc hcab
theorem lcm_dvd_lcm [gcd_monoid α] {a b c d : α} (hab : a ∣ b) (hcd : c ∣ d) :
lcm a c ∣ lcm b d :=
lcm_dvd (hab.trans (dvd_lcm_left _ _)) (hcd.trans (dvd_lcm_right _ _))
@[simp] theorem lcm_units_coe_left [normalized_gcd_monoid α] (u : αˣ) (a : α) :
lcm ↑u a = normalize a :=
lcm_eq_normalize (lcm_dvd units.coe_dvd dvd_rfl) (dvd_lcm_right _ _)
@[simp] theorem lcm_units_coe_right [normalized_gcd_monoid α] (a : α) (u : αˣ) :
lcm a ↑u = normalize a :=
(lcm_comm a u).trans $ lcm_units_coe_left _ _
@[simp] theorem lcm_one_left [normalized_gcd_monoid α] (a : α) : lcm 1 a = normalize a :=
lcm_units_coe_left 1 a
@[simp] theorem lcm_one_right [normalized_gcd_monoid α] (a : α) : lcm a 1 = normalize a :=
lcm_units_coe_right a 1
@[simp] theorem lcm_same [normalized_gcd_monoid α] (a : α) : lcm a a = normalize a :=
lcm_eq_normalize (lcm_dvd dvd_rfl dvd_rfl) (dvd_lcm_left _ _)
@[simp] theorem lcm_eq_one_iff [normalized_gcd_monoid α] (a b : α) : lcm a b = 1 ↔ a ∣ 1 ∧ b ∣ 1 :=
iff.intro
(assume eq, eq ▸ ⟨dvd_lcm_left _ _, dvd_lcm_right _ _⟩)
(assume ⟨⟨c, hc⟩, ⟨d, hd⟩⟩,
show lcm (units.mk_of_mul_eq_one a c hc.symm : α) (units.mk_of_mul_eq_one b d hd.symm) = 1,
by rw [lcm_units_coe_left, normalize_coe_units])
@[simp] theorem lcm_mul_left [normalized_gcd_monoid α] (a b c : α) :
lcm (a * b) (a * c) = normalize a * lcm b c :=
classical.by_cases (by rintro rfl; simp only [zero_mul, lcm_zero_left, normalize_zero]) $
assume ha : a ≠ 0,
suffices lcm (a * b) (a * c) = normalize (a * lcm b c),
by simpa only [normalize.map_mul, normalize_lcm],
have a ∣ lcm (a * b) (a * c), from (dvd_mul_right _ _).trans (dvd_lcm_left _ _),
let ⟨d, eq⟩ := this in
lcm_eq_normalize
(lcm_dvd (mul_dvd_mul_left a (dvd_lcm_left _ _)) (mul_dvd_mul_left a (dvd_lcm_right _ _)))
(eq.symm ▸ (mul_dvd_mul_left a $ lcm_dvd
((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_left _ _)
((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_right _ _)))
@[simp] theorem lcm_mul_right [normalized_gcd_monoid α] (a b c : α) :
lcm (b * a) (c * a) = lcm b c * normalize a :=
by simp only [mul_comm, lcm_mul_left]
theorem lcm_eq_left_iff [normalized_gcd_monoid α] (a b : α) (h : normalize a = a) :
lcm a b = a ↔ b ∣ a :=
iff.intro (assume eq, eq ▸ dvd_lcm_right _ _) $
assume hab, dvd_antisymm_of_normalize_eq (normalize_lcm _ _) h (lcm_dvd (dvd_refl a) hab)
(dvd_lcm_left _ _)
theorem lcm_eq_right_iff [normalized_gcd_monoid α] (a b : α) (h : normalize b = b) :
lcm a b = b ↔ a ∣ b :=
by simpa only [lcm_comm b a] using lcm_eq_left_iff b a h
theorem lcm_dvd_lcm_mul_left [gcd_monoid α] (m n k : α) : lcm m n ∣ lcm (k * m) n :=
lcm_dvd_lcm (dvd_mul_left _ _) dvd_rfl
theorem lcm_dvd_lcm_mul_right [gcd_monoid α] (m n k : α) : lcm m n ∣ lcm (m * k) n :=
lcm_dvd_lcm (dvd_mul_right _ _) dvd_rfl
theorem lcm_dvd_lcm_mul_left_right [gcd_monoid α] (m n k : α) : lcm m n ∣ lcm m (k * n) :=
lcm_dvd_lcm dvd_rfl (dvd_mul_left _ _)
theorem lcm_dvd_lcm_mul_right_right [gcd_monoid α] (m n k : α) : lcm m n ∣ lcm m (n * k) :=
lcm_dvd_lcm dvd_rfl (dvd_mul_right _ _)
theorem lcm_eq_of_associated_left [normalized_gcd_monoid α] {m n : α}
(h : associated m n) (k : α) : lcm m k = lcm n k :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _)
(lcm_dvd_lcm h.dvd dvd_rfl)
(lcm_dvd_lcm h.symm.dvd dvd_rfl)
theorem lcm_eq_of_associated_right [normalized_gcd_monoid α] {m n : α}
(h : associated m n) (k : α) : lcm k m = lcm k n :=
dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _)
(lcm_dvd_lcm dvd_rfl h.dvd)
(lcm_dvd_lcm dvd_rfl h.symm.dvd)
end lcm
namespace gcd_monoid
theorem prime_of_irreducible [gcd_monoid α] {x : α} (hi: irreducible x) : prime x :=
⟨hi.ne_zero, ⟨hi.1, λ a b h,
begin
cases gcd_dvd_left x a with y hy,
cases hi.is_unit_or_is_unit hy with hu hu,
{ right, transitivity (gcd (x * b) (a * b)), apply dvd_gcd (dvd_mul_right x b) h,
rw (gcd_mul_right' b x a).dvd_iff_dvd_left,
exact (associated_unit_mul_left _ _ hu).dvd },
{ left,
rw hy,
exact dvd_trans (associated_mul_unit_left _ _ hu).dvd (gcd_dvd_right x a) }
end ⟩⟩
theorem irreducible_iff_prime [gcd_monoid α] {p : α} : irreducible p ↔ prime p :=
⟨prime_of_irreducible, prime.irreducible⟩
end gcd_monoid
end gcd_monoid
section unique_unit
variables [cancel_comm_monoid_with_zero α] [unique αˣ]
@[priority 100] -- see Note [lower instance priority]
instance normalization_monoid_of_unique_units : normalization_monoid α :=
{ norm_unit := λ x, 1,
norm_unit_zero := rfl,
norm_unit_mul := λ x y hx hy, (mul_one 1).symm,
norm_unit_coe_units := λ u, subsingleton.elim _ _ }
instance unique_normalization_monoid_of_unique_units : unique (normalization_monoid α) :=
{ default := normalization_monoid_of_unique_units,
uniq := λ ⟨u, _, _, _⟩, by simpa only [(subsingleton.elim _ _ : u = λ _, 1)] }
instance subsingleton_gcd_monoid_of_unique_units : subsingleton (gcd_monoid α) :=
⟨λ g₁ g₂, begin
have hgcd : g₁.gcd = g₂.gcd,
{ ext a b,
refine associated_iff_eq.mp (associated_of_dvd_dvd _ _);
apply dvd_gcd (gcd_dvd_left _ _) (gcd_dvd_right _ _) },
have hlcm : g₁.lcm = g₂.lcm,
{ ext a b,
refine associated_iff_eq.mp (associated_of_dvd_dvd _ _);
apply lcm_dvd_iff.2 ⟨dvd_lcm_left _ _, dvd_lcm_right _ _⟩ },
cases g₁, cases g₂,
dsimp only at hgcd hlcm,
simp only [hgcd, hlcm],
end⟩
instance subsingleton_normalized_gcd_monoid_of_unique_units :
subsingleton (normalized_gcd_monoid α) :=
⟨begin
intros a b,
cases a with a_norm a_gcd,
cases b with b_norm b_gcd,
have := subsingleton.elim a_gcd b_gcd,
subst this,
have := subsingleton.elim a_norm b_norm,
subst this
end⟩
@[simp] lemma norm_unit_eq_one (x : α) : norm_unit x = 1 := rfl
@[simp] lemma normalize_eq (x : α) : normalize x = x := mul_one x
/-- If a monoid's only unit is `1`, then it is isomorphic to its associates. -/
@[simps]
def associates_equiv_of_unique_units : associates α ≃* α :=
{ to_fun := associates.out,
inv_fun := associates.mk,
left_inv := associates.mk_out,
right_inv := λ t, (associates.out_mk _).trans $ normalize_eq _,
map_mul' := associates.out_mul }
end unique_unit
section is_domain
variables [comm_ring α] [is_domain α] [normalized_gcd_monoid α]
lemma gcd_eq_of_dvd_sub_right {a b c : α} (h : a ∣ b - c) : gcd a b = gcd a c :=
begin
apply dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _);
rw dvd_gcd_iff; refine ⟨gcd_dvd_left _ _, _⟩,
{ rcases h with ⟨d, hd⟩,
rcases gcd_dvd_right a b with ⟨e, he⟩,
rcases gcd_dvd_left a b with ⟨f, hf⟩,
use e - f * d,
rw [mul_sub, ← he, ← mul_assoc, ← hf, ← hd, sub_sub_cancel] },
{ rcases h with ⟨d, hd⟩,
rcases gcd_dvd_right a c with ⟨e, he⟩,
rcases gcd_dvd_left a c with ⟨f, hf⟩,
use e + f * d,
rw [mul_add, ← he, ← mul_assoc, ← hf, ← hd, ← add_sub_assoc, add_comm c b, add_sub_cancel] }
end
lemma gcd_eq_of_dvd_sub_left {a b c : α} (h : a ∣ b - c) : gcd b a = gcd c a :=
by rw [gcd_comm _ a, gcd_comm _ a, gcd_eq_of_dvd_sub_right h]
end is_domain
section constructors
noncomputable theory
open associates
variables [cancel_comm_monoid_with_zero α]
private lemma map_mk_unit_aux [decidable_eq α] {f : associates α →* α}
(hinv : function.right_inverse f associates.mk) (a : α) :
a * ↑(classical.some (associated_map_mk hinv a)) = f (associates.mk a) :=
classical.some_spec (associated_map_mk hinv a)
/-- Define `normalization_monoid` on a structure from a `monoid_hom` inverse to `associates.mk`. -/
def normalization_monoid_of_monoid_hom_right_inverse [decidable_eq α] (f : associates α →* α)
(hinv : function.right_inverse f associates.mk) :
normalization_monoid α :=
{ norm_unit := λ a, if a = 0 then 1 else
classical.some (associates.mk_eq_mk_iff_associated.1 (hinv (associates.mk a)).symm),
norm_unit_zero := if_pos rfl,
norm_unit_mul := λ a b ha hb, by
{ rw [if_neg (mul_ne_zero ha hb), if_neg ha, if_neg hb, units.ext_iff, units.coe_mul],
suffices : (a * b) * ↑(classical.some (associated_map_mk hinv (a * b))) =
(a * ↑(classical.some (associated_map_mk hinv a))) *
(b * ↑(classical.some (associated_map_mk hinv b))),
{ apply mul_left_cancel₀ (mul_ne_zero ha hb) _,
simpa only [mul_assoc, mul_comm, mul_left_comm] using this },
rw [map_mk_unit_aux hinv a, map_mk_unit_aux hinv (a * b), map_mk_unit_aux hinv b,
← monoid_hom.map_mul, associates.mk_mul_mk] },
norm_unit_coe_units := λ u, by
{ nontriviality α,
rw [if_neg (units.ne_zero u), units.ext_iff],
apply mul_left_cancel₀ (units.ne_zero u),
rw [units.mul_inv, map_mk_unit_aux hinv u,
associates.mk_eq_mk_iff_associated.2 (associated_one_iff_is_unit.2 ⟨u, rfl⟩),
associates.mk_one, monoid_hom.map_one] } }
/-- Define `gcd_monoid` on a structure just from the `gcd` and its properties. -/
noncomputable def gcd_monoid_of_gcd [decidable_eq α] (gcd : α → α → α)
(gcd_dvd_left : ∀a b, gcd a b ∣ a)
(gcd_dvd_right : ∀a b, gcd a b ∣ b)
(dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b) :
gcd_monoid α :=
{ gcd := gcd,
gcd_dvd_left := gcd_dvd_left,
gcd_dvd_right := gcd_dvd_right,
dvd_gcd := λ a b c, dvd_gcd,
lcm := λ a b, if a = 0 then 0 else classical.some ((gcd_dvd_left a b).trans (dvd.intro b rfl)),
gcd_mul_lcm := λ a b, by
{ split_ifs with a0,
{ rw [mul_zero, a0, zero_mul] },
{ rw ←classical.some_spec ((gcd_dvd_left a b).trans (dvd.intro b rfl)) } },
lcm_zero_left := λ a, if_pos rfl,
lcm_zero_right := λ a, by
{ split_ifs with a0, { refl },
have h := (classical.some_spec ((gcd_dvd_left a 0).trans (dvd.intro 0 rfl))).symm,
have a0' : gcd a 0 ≠ 0,
{ contrapose! a0,
rw [←associated_zero_iff_eq_zero, ←a0],
exact associated_of_dvd_dvd (dvd_gcd (dvd_refl a) (dvd_zero a)) (gcd_dvd_left _ _) },
apply or.resolve_left (mul_eq_zero.1 _) a0',
rw [h, mul_zero] } }
/-- Define `normalized_gcd_monoid` on a structure just from the `gcd` and its properties. -/
noncomputable def normalized_gcd_monoid_of_gcd [normalization_monoid α] [decidable_eq α]
(gcd : α → α → α)
(gcd_dvd_left : ∀a b, gcd a b ∣ a)
(gcd_dvd_right : ∀a b, gcd a b ∣ b)
(dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b)
(normalize_gcd : ∀a b, normalize (gcd a b) = gcd a b) :
normalized_gcd_monoid α :=
{ gcd := gcd,
gcd_dvd_left := gcd_dvd_left,
gcd_dvd_right := gcd_dvd_right,
dvd_gcd := λ a b c, dvd_gcd,
normalize_gcd := normalize_gcd,
lcm := λ a b, if a = 0 then 0 else classical.some (dvd_normalize_iff.2
((gcd_dvd_left a b).trans (dvd.intro b rfl))),
normalize_lcm := λ a b, by
{ dsimp [normalize],
split_ifs with a0,
{ exact @normalize_zero α _ _ },
{ have := (classical.some_spec (dvd_normalize_iff.2
((gcd_dvd_left a b).trans (dvd.intro b rfl)))).symm,
set l := classical.some (dvd_normalize_iff.2
((gcd_dvd_left a b).trans (dvd.intro b rfl))),
obtain rfl|hb := eq_or_ne b 0,
{ simp only [normalize_zero, mul_zero, mul_eq_zero] at this,
obtain ha|hl := this,
{ apply (a0 _).elim,
rw [←zero_dvd_iff, ←ha],
exact gcd_dvd_left _ _ },
{ convert @normalize_zero α _ _ } },
have h1 : gcd a b ≠ 0,
{ have hab : a * b ≠ 0 := mul_ne_zero a0 hb,
contrapose! hab,
rw [←normalize_eq_zero, ←this, hab, zero_mul] },
have h2 : normalize (gcd a b * l) = gcd a b * l,
{ rw [this, normalize_idem] },
rw ←normalize_gcd at this,
rwa [normalize.map_mul, normalize_gcd, mul_right_inj' h1] at h2 } },
gcd_mul_lcm := λ a b, by
{ split_ifs with a0,
{ rw [mul_zero, a0, zero_mul] },
{ rw ←classical.some_spec (dvd_normalize_iff.2 ((gcd_dvd_left a b).trans (dvd.intro b rfl))),
exact normalize_associated (a * b) } },
lcm_zero_left := λ a, if_pos rfl,
lcm_zero_right := λ a, by
{ split_ifs with a0, { refl },
rw ← normalize_eq_zero at a0,
have h := (classical.some_spec (dvd_normalize_iff.2
((gcd_dvd_left a 0).trans (dvd.intro 0 rfl)))).symm,
have gcd0 : gcd a 0 = normalize a,
{ rw ← normalize_gcd,
exact normalize_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_zero a)) },
rw ← gcd0 at a0,
apply or.resolve_left (mul_eq_zero.1 _) a0,
rw [h, mul_zero, normalize_zero] },
.. (infer_instance : normalization_monoid α) }
/-- Define `gcd_monoid` on a structure just from the `lcm` and its properties. -/
noncomputable def gcd_monoid_of_lcm [decidable_eq α] (lcm : α → α → α)
(dvd_lcm_left : ∀a b, a ∣ lcm a b)
(dvd_lcm_right : ∀a b, b ∣ lcm a b)
(lcm_dvd : ∀{a b c}, c ∣ a → b ∣ a → lcm c b ∣ a):
gcd_monoid α :=
let exists_gcd := λ a b, lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl) in
{ lcm := lcm,
gcd := λ a b, if a = 0 then b else (if b = 0 then a else
classical.some (exists_gcd a b)),
gcd_mul_lcm := λ a b, by
{ split_ifs,
{ rw [h, eq_zero_of_zero_dvd (dvd_lcm_left _ _), mul_zero, zero_mul] },
{ rw [h_1, eq_zero_of_zero_dvd (dvd_lcm_right _ _), mul_zero] },
rw [mul_comm, ←classical.some_spec (exists_gcd a b)] },
lcm_zero_left := λ a, eq_zero_of_zero_dvd (dvd_lcm_left _ _),
lcm_zero_right := λ a, eq_zero_of_zero_dvd (dvd_lcm_right _ _),
gcd_dvd_left := λ a b, by
{ split_ifs with h h_1,
{ rw h, apply dvd_zero },
{ exact dvd_rfl },
have h0 : lcm a b ≠ 0,
{ intro con,
have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl),
rw [con, zero_dvd_iff, mul_eq_zero] at h,
cases h; tauto },
rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b),
mul_comm, mul_dvd_mul_iff_right h],
apply dvd_lcm_right },
gcd_dvd_right := λ a b, by
{ split_ifs with h h_1,
{ exact dvd_rfl },
{ rw h_1, apply dvd_zero },
have h0 : lcm a b ≠ 0,
{ intro con,
have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl),
rw [con, zero_dvd_iff, mul_eq_zero] at h,
cases h; tauto },
rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b),
mul_dvd_mul_iff_right h_1],
apply dvd_lcm_left },
dvd_gcd := λ a b c ac ab, by
{ split_ifs,
{ exact ab },
{ exact ac },
have h0 : lcm c b ≠ 0,
{ intro con,
have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left c rfl),
rw [con, zero_dvd_iff, mul_eq_zero] at h,
cases h; tauto },
rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd c b)],
rcases ab with ⟨d, rfl⟩,
rw mul_eq_zero at h_1,
push_neg at h_1,
rw [mul_comm a, ← mul_assoc, mul_dvd_mul_iff_right h_1.1],
apply lcm_dvd (dvd.intro d rfl),
rw [mul_comm, mul_dvd_mul_iff_right h_1.2],
apply ac } }
/-- Define `normalized_gcd_monoid` on a structure just from the `lcm` and its properties. -/
noncomputable def normalized_gcd_monoid_of_lcm [normalization_monoid α] [decidable_eq α]
(lcm : α → α → α)
(dvd_lcm_left : ∀a b, a ∣ lcm a b)
(dvd_lcm_right : ∀a b, b ∣ lcm a b)
(lcm_dvd : ∀{a b c}, c ∣ a → b ∣ a → lcm c b ∣ a)
(normalize_lcm : ∀a b, normalize (lcm a b) = lcm a b) :
normalized_gcd_monoid α :=
let exists_gcd := λ a b, dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl)) in
{ lcm := lcm,
gcd := λ a b, if a = 0 then normalize b else (if b = 0 then normalize a else
classical.some (exists_gcd a b)),
gcd_mul_lcm := λ a b, by
{ split_ifs with h h_1,
{ rw [h, eq_zero_of_zero_dvd (dvd_lcm_left _ _), mul_zero, zero_mul] },
{ rw [h_1, eq_zero_of_zero_dvd (dvd_lcm_right _ _), mul_zero, mul_zero] },
rw [mul_comm, ←classical.some_spec (exists_gcd a b)],
exact normalize_associated (a * b) },
normalize_lcm := normalize_lcm,
normalize_gcd := λ a b, by
{ dsimp [normalize],
split_ifs with h h_1,
{ apply normalize_idem },
{ apply normalize_idem },
have h0 : lcm a b ≠ 0,
{ intro con,
have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl),
rw [con, zero_dvd_iff, mul_eq_zero] at h,
cases h; tauto },
apply mul_left_cancel₀ h0,
refine trans _ (classical.some_spec (exists_gcd a b)),
conv_lhs { congr, rw [← normalize_lcm a b] },
erw [← normalize.map_mul, ← classical.some_spec (exists_gcd a b), normalize_idem] },
lcm_zero_left := λ a, eq_zero_of_zero_dvd (dvd_lcm_left _ _),
lcm_zero_right := λ a, eq_zero_of_zero_dvd (dvd_lcm_right _ _),
gcd_dvd_left := λ a b, by
{ split_ifs,
{ rw h, apply dvd_zero },
{ exact (normalize_associated _).dvd },
have h0 : lcm a b ≠ 0,
{ intro con,
have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl),
rw [con, zero_dvd_iff, mul_eq_zero] at h,
cases h; tauto },
rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b),
normalize_dvd_iff, mul_comm, mul_dvd_mul_iff_right h],
apply dvd_lcm_right },
gcd_dvd_right := λ a b, by
{ split_ifs,
{ exact (normalize_associated _).dvd },
{ rw h_1, apply dvd_zero },
have h0 : lcm a b ≠ 0,
{ intro con,
have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl),
rw [con, zero_dvd_iff, mul_eq_zero] at h,
cases h; tauto },
rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b),
normalize_dvd_iff, mul_dvd_mul_iff_right h_1],
apply dvd_lcm_left },
dvd_gcd := λ a b c ac ab, by
{ split_ifs,
{ apply dvd_normalize_iff.2 ab },
{ apply dvd_normalize_iff.2 ac },
have h0 : lcm c b ≠ 0,
{ intro con,
have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left c rfl),
rw [con, zero_dvd_iff, mul_eq_zero] at h,
cases h; tauto },
rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (dvd_normalize_iff.2
(lcm_dvd (dvd.intro b rfl) (dvd.intro_left c rfl))), dvd_normalize_iff],
rcases ab with ⟨d, rfl⟩,
rw mul_eq_zero at h_1,
push_neg at h_1,
rw [mul_comm a, ← mul_assoc, mul_dvd_mul_iff_right h_1.1],
apply lcm_dvd (dvd.intro d rfl),
rw [mul_comm, mul_dvd_mul_iff_right h_1.2],
apply ac },
.. (infer_instance : normalization_monoid α) }
/-- Define a `gcd_monoid` structure on a monoid just from the existence of a `gcd`. -/
noncomputable def gcd_monoid_of_exists_gcd [decidable_eq α]
(h : ∀ a b : α, ∃ c : α, ∀ d : α, d ∣ a ∧ d ∣ b ↔ d ∣ c) :
gcd_monoid α :=
gcd_monoid_of_gcd
(λ a b, (classical.some (h a b)))
(λ a b,
(((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).1)
(λ a b,
(((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).2)
(λ a b c ac ab, ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩))
/-- Define a `normalized_gcd_monoid` structure on a monoid just from the existence of a `gcd`. -/
noncomputable def normalized_gcd_monoid_of_exists_gcd [normalization_monoid α] [decidable_eq α]
(h : ∀ a b : α, ∃ c : α, ∀ d : α, d ∣ a ∧ d ∣ b ↔ d ∣ c) :
normalized_gcd_monoid α :=
normalized_gcd_monoid_of_gcd
(λ a b, normalize (classical.some (h a b)))
(λ a b, normalize_dvd_iff.2
(((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).1)
(λ a b, normalize_dvd_iff.2
(((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).2)
(λ a b c ac ab, dvd_normalize_iff.2 ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩))
(λ a b, normalize_idem _)
/-- Define a `gcd_monoid` structure on a monoid just from the existence of an `lcm`. -/
noncomputable def gcd_monoid_of_exists_lcm [decidable_eq α]
(h : ∀ a b : α, ∃ c : α, ∀ d : α, a ∣ d ∧ b ∣ d ↔ c ∣ d) :
gcd_monoid α :=
gcd_monoid_of_lcm
(λ a b, (classical.some (h a b)))
(λ a b,
(((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).1)
(λ a b,
(((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).2)
(λ a b c ac ab, ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩))
/-- Define a `normalized_gcd_monoid` structure on a monoid just from the existence of an `lcm`. -/
noncomputable def normalized_gcd_monoid_of_exists_lcm [normalization_monoid α] [decidable_eq α]
(h : ∀ a b : α, ∃ c : α, ∀ d : α, a ∣ d ∧ b ∣ d ↔ c ∣ d) :
normalized_gcd_monoid α :=
normalized_gcd_monoid_of_lcm
(λ a b, normalize (classical.some (h a b)))
(λ a b, dvd_normalize_iff.2
(((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).1)
(λ a b, dvd_normalize_iff.2
(((classical.some_spec (h a b) (classical.some (h a b))).2 dvd_rfl)).2)
(λ a b c ac ab, normalize_dvd_iff.2 ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩))
(λ a b, normalize_idem _)
end constructors
namespace comm_group_with_zero
variables (G₀ : Type*) [comm_group_with_zero G₀] [decidable_eq G₀]
@[priority 100] -- see Note [lower instance priority]
instance : normalized_gcd_monoid G₀ :=
{ norm_unit := λ x, if h : x = 0 then 1 else (units.mk0 x h)⁻¹,
norm_unit_zero := dif_pos rfl,
norm_unit_mul := λ x y x0 y0, units.eq_iff.1 (by simp [x0, y0, mul_comm]),
norm_unit_coe_units := λ u, by { rw [dif_neg (units.ne_zero _), units.mk0_coe], apply_instance },
gcd := λ a b, if a = 0 ∧ b = 0 then 0 else 1,
lcm := λ a b, if a = 0 ∨ b = 0 then 0 else 1,
gcd_dvd_left := λ a b, by { split_ifs with h, { rw h.1 }, { exact one_dvd _ } },
gcd_dvd_right := λ a b, by { split_ifs with h, { rw h.2 }, { exact one_dvd _ } },
dvd_gcd := λ a b c hac hab, begin
split_ifs with h, { apply dvd_zero },
cases not_and_distrib.mp h with h h;
refine is_unit_iff_dvd_one.mp (is_unit_of_dvd_unit _ (is_unit.mk0 _ h));
assumption
end,
gcd_mul_lcm := λ a b, begin
by_cases ha : a = 0, { simp [ha] },
by_cases hb : b = 0, { simp [hb] },
rw [if_neg (not_and_of_not_left _ ha), one_mul, if_neg (not_or ha hb)],
exact (associated_one_iff_is_unit.mpr ((is_unit.mk0 _ ha).mul (is_unit.mk0 _ hb))).symm
end,
lcm_zero_left := λ b, if_pos (or.inl rfl),
lcm_zero_right := λ a, if_pos (or.inr rfl),
-- `split_ifs` wants to split `normalize`, so handle the cases manually
normalize_gcd := λ a b, if h : a = 0 ∧ b = 0 then by simp [if_pos h] else by simp [if_neg h],
normalize_lcm := λ a b, if h : a = 0 ∨ b = 0 then by simp [if_pos h] else by simp [if_neg h] }
@[simp]
lemma coe_norm_unit {a : G₀} (h0 : a ≠ 0) : (↑(norm_unit a) : G₀) = a⁻¹ :=
by simp [norm_unit, h0]
lemma normalize_eq_one {a : G₀} (h0 : a ≠ 0) : normalize a = 1 :=
by simp [normalize_apply, h0]
end comm_group_with_zero
|
464955450b42a937835bbe343b69525292a5fa35 | b82c5bb4c3b618c23ba67764bc3e93f4999a1a39 | /src/formal_ml/exp_bound.lean | 7c53b950101119231e33fdd3ff8549392e37b0ce | [
"Apache-2.0"
] | permissive | nouretienne/formal-ml | 83c4261016955bf9bcb55bd32b4f2621b44163e0 | 40b6da3b6e875f47412d50c7cd97936cb5091a2b | refs/heads/master | 1,671,216,448,724 | 1,600,472,285,000 | 1,600,472,285,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,515 | lean | /-
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
import analysis.calculus.mean_value
import data.complex.exponential
import formal_ml.convex_optimization
import formal_ml.analytic_function
/-
These theorems are much harder than they look, as they depend upon
has_deriv_at_exp, which in turn depends upon calculating the derivative of a exponential
function, which (for better or worse) I solved by calculating the derivative of an arbitrary
function that is analytic everywhere (e.g. sine, cosine, et cetera).
In hindsight, this might have been necessary. The key was to consider an analytic function
of a sum, e.g.:
F (x + y) = ∑ n:ℕ, (x+y)^n * (f n)
where ∑ n:ℕ means to sum over the natural numbers. This is equivalent to:
F (x + y) = ∑ m:ℕ n:ℕ, x^m * y^n * (nat.choose (m + n) m) (f n)
F (x + y) = ∑ m:ℕ, x^m * ∑ n:ℕ, y^n * (nat.choose (m + n) m) (f n)
Then, we can consider
(F (h + x) = (F x) + h * ∑ m:ℕ, h^m * ∑ n:ℕ, x^n * (nat.choose (m + n + 1) (m + 1)) (f n)
I made the proof much harder than it should be. First of all, I focused on conditional sums.
While conditional sums are sometimes necessary (e.g. the Gregory series), functions that
are analytic everywhere have a Maclaurin series that is absolutely summable everywhere.
Secondly, I went with the conventional derivative. Once I
-/
lemma has_derivative_exp:has_derivative real.exp real.exp :=
begin
unfold has_derivative,
intro x,
apply has_deriv_at_exp,
end
lemma exp_bound3 (x:real):0 ≤ real.exp x - (x + 1) :=
begin
let f:=λ x,real.exp x - (x + 1),
let f':=λ x,real.exp x - 1,
let f'':=λ x,real.exp x,
begin
have f_def:f=λ x,real.exp x - (x + 1),
{
refl,
},
have f_def':f'=λ x,real.exp x - 1,
{
refl,
},
have f_def'':f''=λ x,real.exp x,
{
refl,
},
have A1:has_derivative f f',
{
rw f_def,
rw f_def',
apply has_derivative_sub real.exp real.exp (λ x, (x + 1)) (1),
{
apply has_derivative_exp,
},
{
apply has_derivative_add_const,
apply has_derivative_id,
},
},
have A2:has_derivative f' f'',
{
apply has_derivative_add_const,
apply has_derivative_exp,
},
have A3:∀ y, f 0 ≤ f y,
{
intro y,
apply is_minimum,
{
apply A1,
},
{
apply A2,
},
{
intro z,
rw f_def'',
apply le_of_lt,
apply real.exp_pos,
},
have A3D:f' 0 = real.exp 0 - 1,
{
refl,
},
rw A3D,
simp,
},
have A4:f 0 = 0,
{
rw f_def,
simp,
},
rw A4 at A3,
have A5:f x = real.exp x - (x + 1),
{
refl,
},
rw ← A5,
apply A3,
end
end
lemma exp_bound2 (x:real):1 - x ≤ real.exp(-x) :=
begin
have A1:0 ≤ real.exp(-x) - ((-x) + 1),
{
apply exp_bound3,
},
have A2:((-x) + 1) ≤ real.exp(-x) := le_of_sub_nonneg A1,
rw add_comm at A2,
rw sub_eq_add_neg,
exact A2,
end
--TODO: verify this is exactly what we need first.
lemma nnreal_exp_bound (x:nnreal):(1-x) ≤ nnreal.exp(-x) :=
begin
apply nnreal.coe_le_coe.mp,
rw nnreal_exp_eq,
have A1:((x:real) ≤ 1) ∨ (1 ≤ (x:real)),
{
apply decidable_linear_order.le_total,
},
cases A1,
{
rw nnreal.coe_sub,
apply exp_bound2 (↑x),
apply A1,
},
{
rw nnreal.sub_eq_zero,
{
apply lt_imp_le,
apply real.exp_pos,
},
{
apply A1,
}
}
end
--This exact theorem is used for PAC bounds.
lemma nnreal_exp_bound2 (x:nnreal) (k:ℕ):(1 - x)^k ≤ nnreal.exp(-x * k) :=
begin
have A1:(1-x) ≤ nnreal.exp(-x),
{
apply nnreal_exp_bound,
},
have A2:(1 - x)^k ≤ (nnreal.exp(-x))^k,
{
apply nnreal_pow_mono,
exact A1,
},
have A3:nnreal.exp(-x)^k=nnreal.exp(-x * k),
{
symmetry,
rw mul_comm,
apply nnreal_exp_pow,
},
rw ← A3,
apply A2,
end
|
cca0d211ec04e7cc9baddf0fd93ef19e810182f9 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/evalBuiltinInit.lean | 8db8697c1c920bdfcb74b50598366192ccbc0602 | [
"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 | 187 | lean | import Lean
-- option should be ignored when evaluating a `[builtin_init]` decl
set_option interpreter.prefer_native false
#eval toString Lean.PrettyPrinter.formatterAttribute.defn.name
|
40ee307e956f974b4e8502109fc47034523404c0 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /02_Dependent_Type_Theory.org.12.lean | 48bb04d647a343787dbd6c2015196b54bce475a2 | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 451 | lean | /- page 17 -/
import standard
constants A B C : Type
constant f : A → B
constant g : B → C
constant h : A → A
constants (a : A) (b : B)
check (λ x : A, x) a -- A
check (λ x : A, b) a -- B
check (λ x : A, b) (h a) -- B
check (λ x : A, g (f x)) (h (h a)) -- C
check (λ v u x, v (u x)) g f a -- C
check (λ (Q R S : Type) (v : R → S) (u : Q → R) (x : Q),
v (u x)) A B C g f a -- C
|
6da427c5b5e3d55fb7412c2fd1893b7c97561550 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/order/preorder_hom.lean | 8f66cf305a003be712790ab28f8982ea99daf33b | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 8,367 | lean | /-
Copyright (c) 2020 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
# Preorder homomorphisms
Bundled monotone functions, `x ≤ y → f x ≤ f y`.
-/
import logic.function.iterate
import order.basic
import order.bounded_lattice
import order.complete_lattice
import tactic.monotonicity
/-! # Category of preorders -/
/-- Bundled monotone (aka, increasing) function -/
structure preorder_hom (α β : Type*) [preorder α] [preorder β] :=
(to_fun : α → β)
(monotone' : monotone to_fun)
infixr ` →ₘ `:25 := preorder_hom
namespace preorder_hom
variables {α : Type*} {β : Type*} {γ : Type*} [preorder α] [preorder β] [preorder γ]
instance : has_coe_to_fun (preorder_hom α β) :=
{ F := λ f, α → β,
coe := preorder_hom.to_fun }
initialize_simps_projections preorder_hom (to_fun → coe)
@[mono]
lemma monotone (f : α →ₘ β) : monotone f :=
preorder_hom.monotone' f
@[simp] lemma to_fun_eq_coe {f : α →ₘ β} : f.to_fun = f := rfl
@[simp] lemma coe_fun_mk {f : α → β} (hf : _root_.monotone f) : (mk f hf : α → β) = f := rfl
@[ext] -- See library note [partially-applied ext lemmas]
lemma ext (f g : preorder_hom α β) (h : (f : α → β) = g) : f = g :=
by { cases f, cases g, congr, exact h }
/-- The identity function as bundled monotone function. -/
@[simps {fully_applied := ff}]
def id : preorder_hom α α :=
⟨id, monotone_id⟩
instance : inhabited (preorder_hom α α) := ⟨id⟩
/-- The composition of two bundled monotone functions. -/
@[simps {fully_applied := ff}]
def comp (g : preorder_hom β γ) (f : preorder_hom α β) : preorder_hom α γ :=
⟨g ∘ f, g.monotone.comp f.monotone⟩
@[simp] lemma comp_id (f : preorder_hom α β) : f.comp id = f :=
by { ext, refl }
@[simp] lemma id_comp (f : preorder_hom α β) : id.comp f = f :=
by { ext, refl }
/-- `subtype.val` as a bundled monotone function. -/
@[simps {fully_applied := ff}]
def subtype.val (p : α → Prop) : subtype p →ₘ α :=
⟨subtype.val, λ x y h, h⟩
-- TODO[gh-6025]: make this a global instance once safe to do so
/-- There is a unique monotone map from a subsingleton to itself. -/
local attribute [instance]
def unique [subsingleton α] : unique (α →ₘ α) :=
{ default := preorder_hom.id, uniq := λ a, ext _ _ (subsingleton.elim _ _) }
lemma preorder_hom_eq_id [subsingleton α] (g : α →ₘ α) : g = preorder_hom.id :=
subsingleton.elim _ _
/-- The preorder structure of `α →ₘ β` is pointwise inequality: `f ≤ g ↔ ∀ a, f a ≤ g a`. -/
instance : preorder (α →ₘ β) :=
preorder.lift preorder_hom.to_fun
instance {β : Type*} [partial_order β] : partial_order (α →ₘ β) :=
partial_order.lift preorder_hom.to_fun $ by rintro ⟨⟩ ⟨⟩ h; congr; exact h
@[simps]
instance {β : Type*} [semilattice_sup β] : has_sup (α →ₘ β) :=
{ sup := λ f g, ⟨λ a, f a ⊔ g a, λ x y h, sup_le_sup (f.monotone h) (g.monotone h)⟩ }
instance {β : Type*} [semilattice_sup β] : semilattice_sup (α →ₘ β) :=
{ sup := has_sup.sup,
le_sup_left := λ a b x, le_sup_left,
le_sup_right := λ a b x, le_sup_right,
sup_le := λ a b c h₀ h₁ x, sup_le (h₀ x) (h₁ x),
.. (_ : partial_order (α →ₘ β)) }
@[simps]
instance {β : Type*} [semilattice_inf β] : has_inf (α →ₘ β) :=
{ inf := λ f g, ⟨λ a, f a ⊓ g a, λ x y h, inf_le_inf (f.monotone h) (g.monotone h)⟩ }
instance {β : Type*} [semilattice_inf β] : semilattice_inf (α →ₘ β) :=
{ inf := (⊓),
inf_le_left := λ a b x, inf_le_left,
inf_le_right := λ a b x, inf_le_right,
le_inf := λ a b c h₀ h₁ x, le_inf (h₀ x) (h₁ x),
.. (_ : partial_order (α →ₘ β)) }
instance {β : Type*} [lattice β] : lattice (α →ₘ β) :=
{ .. (_ : semilattice_sup (α →ₘ β)),
.. (_ : semilattice_inf (α →ₘ β)) }
@[simps]
instance {β : Type*} [order_bot β] : has_bot (α →ₘ β) :=
{ bot := ⟨λ a, ⊥, λ a b h, le_refl _⟩ }
instance {β : Type*} [order_bot β] : order_bot (α →ₘ β) :=
{ bot := ⊥,
bot_le := λ a x, bot_le,
.. (_ : partial_order (α →ₘ β)) }
@[simps]
instance {β : Type*} [order_top β] : has_top (α →ₘ β) :=
{ top := ⟨λ a, ⊤, λ a b h, le_refl _⟩ }
instance {β : Type*} [order_top β] : order_top (α →ₘ β) :=
{ top := ⊤,
le_top := λ a x, le_top,
.. (_ : partial_order (α →ₘ β)) }
@[simps]
instance {β : Type*} [complete_lattice β] : has_Inf (α →ₘ β) :=
{ Inf := λ s, ⟨ λ x, Inf ((λ f : _ →ₘ _, f x) '' s), λ x y h,
Inf_le_Inf_of_forall_exists_le begin
simp only [and_imp, exists_prop, set.mem_image, exists_exists_and_eq_and, exists_imp_distrib],
intros,
subst_vars,
refine ⟨_,by assumption, monotone _ h⟩
end ⟩ }
@[simps]
instance {β : Type*} [complete_lattice β] : has_Sup (α →ₘ β) :=
{ Sup := λ s, ⟨ λ x, Sup ((λ f : _ →ₘ _, f x) '' s), λ x y h,
Sup_le_Sup_of_forall_exists_le begin
simp only [and_imp, exists_prop, set.mem_image, exists_exists_and_eq_and, exists_imp_distrib],
intros,
subst_vars,
refine ⟨_,by assumption, monotone _ h⟩
end ⟩ }
@[simps Sup Inf]
instance {β : Type*} [complete_lattice β] : complete_lattice (α →ₘ β) :=
{ Sup := has_Sup.Sup,
le_Sup := λ s f hf x, @le_Sup β _ ((λ f : _ →ₘ _, f x) '' s) (f x) ⟨f, hf, rfl⟩,
Sup_le := λ s f hf x, @Sup_le β _ _ _ $ λ b (h : b ∈ (λ (f : α →ₘ β), f x) '' s),
by rcases h with ⟨g, h, ⟨ ⟩⟩; apply hf _ h,
Inf := has_Inf.Inf,
le_Inf := λ s f hf x, @le_Inf β _ _ _ $ λ b (h : b ∈ (λ (f : α →ₘ β), f x) '' s),
by rcases h with ⟨g, h, ⟨ ⟩⟩; apply hf _ h,
Inf_le := λ s f hf x, @Inf_le β _ ((λ f : _ →ₘ _, f x) '' s) (f x) ⟨f, hf, rfl⟩,
.. (_ : lattice (α →ₘ β)),
.. (_ : order_top (α →ₘ β)),
.. (_ : order_bot (α →ₘ β)) }
lemma iterate_sup_le_sup_iff {α : Type*} [semilattice_sup α] (f : α →ₘ α) :
(∀ n₁ n₂ a₁ a₂, f^[n₁ + n₂] (a₁ ⊔ a₂) ≤ (f^[n₁] a₁) ⊔ (f^[n₂] a₂)) ↔
(∀ a₁ a₂, f (a₁ ⊔ a₂) ≤ (f a₁) ⊔ a₂) :=
begin
split; intros h,
{ exact h 1 0, },
{ intros n₁ n₂ a₁ a₂, have h' : ∀ n a₁ a₂, f^[n] (a₁ ⊔ a₂) ≤ (f^[n] a₁) ⊔ a₂,
{ intros n, induction n with n ih; intros a₁ a₂,
{ refl, },
{ calc f^[n + 1] (a₁ ⊔ a₂) = (f^[n] (f (a₁ ⊔ a₂))) : function.iterate_succ_apply f n _
... ≤ (f^[n] ((f a₁) ⊔ a₂)) : f.monotone.iterate n (h a₁ a₂)
... ≤ (f^[n] (f a₁)) ⊔ a₂ : ih _ _
... = (f^[n + 1] a₁) ⊔ a₂ : by rw ← function.iterate_succ_apply, }, },
calc f^[n₁ + n₂] (a₁ ⊔ a₂) = (f^[n₁] (f^[n₂] (a₁ ⊔ a₂))) : function.iterate_add_apply f n₁ n₂ _
... = (f^[n₁] (f^[n₂] (a₂ ⊔ a₁))) : by rw sup_comm
... ≤ (f^[n₁] ((f^[n₂] a₂) ⊔ a₁)) : f.monotone.iterate n₁ (h' n₂ _ _)
... = (f^[n₁] (a₁ ⊔ (f^[n₂] a₂))) : by rw sup_comm
... ≤ (f^[n₁] a₁) ⊔ (f^[n₂] a₂) : h' n₁ a₁ _, },
end
end preorder_hom
namespace order_embedding
/-- Convert an `order_embedding` to a `preorder_hom`. -/
@[simps {fully_applied := ff}]
def to_preorder_hom {X Y : Type*} [preorder X] [preorder Y] (f : X ↪o Y) : X →ₘ Y :=
{ to_fun := f,
monotone' := f.monotone }
end order_embedding
section rel_hom
variables {α β : Type*} [partial_order α] [preorder β]
namespace rel_hom
variables (f : ((<) : α → α → Prop) →r ((<) : β → β → Prop))
/-- A bundled expression of the fact that a map between partial orders that is strictly monotonic
is weakly monotonic. -/
@[simps {fully_applied := ff}]
def to_preorder_hom : α →ₘ β :=
{ to_fun := f,
monotone' := strict_mono.monotone (λ x y, f.map_rel), }
end rel_hom
lemma rel_embedding.to_preorder_hom_injective (f : ((<) : α → α → Prop) ↪r ((<) : β → β → Prop)) :
function.injective (f : ((<) : α → α → Prop) →r ((<) : β → β → Prop)).to_preorder_hom :=
λ _ _ h, f.injective h
end rel_hom
|
e79eddab684a0b30e29d2938ca9313abb6b8de12 | 592ee40978ac7604005a4e0d35bbc4b467389241 | /Library/generated/mathscheme-lean/PseudoInverseSig.lean | 86dbdde304cda7d751f2b81519ff8a0de012e6c5 | [] | no_license | ysharoda/Deriving-Definitions | 3e149e6641fae440badd35ac110a0bd705a49ad2 | dfecb27572022de3d4aa702cae8db19957523a59 | refs/heads/master | 1,679,127,857,700 | 1,615,939,007,000 | 1,615,939,007,000 | 229,785,731 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,962 | lean | import init.data.nat.basic
import init.data.fin.basic
import data.vector
import .Prelude
open Staged
open nat
open fin
open vector
section PseudoInverseSig
structure PseudoInverseSig (A : Type) : Type :=
(inv : (A → A))
(op : (A → (A → A)))
open PseudoInverseSig
structure Sig (AS : Type) : Type :=
(invS : (AS → AS))
(opS : (AS → (AS → AS)))
structure Product (A : Type) : Type :=
(invP : ((Prod A A) → (Prod A A)))
(opP : ((Prod A A) → ((Prod A A) → (Prod A A))))
structure Hom {A1 : Type} {A2 : Type} (Ps1 : (PseudoInverseSig A1)) (Ps2 : (PseudoInverseSig A2)) : Type :=
(hom : (A1 → A2))
(pres_inv : (∀ {x1 : A1} , (hom ((inv Ps1) x1)) = ((inv Ps2) (hom x1))))
(pres_op : (∀ {x1 x2 : A1} , (hom ((op Ps1) x1 x2)) = ((op Ps2) (hom x1) (hom x2))))
structure RelInterp {A1 : Type} {A2 : Type} (Ps1 : (PseudoInverseSig A1)) (Ps2 : (PseudoInverseSig A2)) : Type 1 :=
(interp : (A1 → (A2 → Type)))
(interp_inv : (∀ {x1 : A1} {y1 : A2} , ((interp x1 y1) → (interp ((inv Ps1) x1) ((inv Ps2) y1)))))
(interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Ps1) x1 x2) ((op Ps2) y1 y2))))))
inductive PseudoInverseSigTerm : Type
| invL : (PseudoInverseSigTerm → PseudoInverseSigTerm)
| opL : (PseudoInverseSigTerm → (PseudoInverseSigTerm → PseudoInverseSigTerm))
open PseudoInverseSigTerm
inductive ClPseudoInverseSigTerm (A : Type) : Type
| sing : (A → ClPseudoInverseSigTerm)
| invCl : (ClPseudoInverseSigTerm → ClPseudoInverseSigTerm)
| opCl : (ClPseudoInverseSigTerm → (ClPseudoInverseSigTerm → ClPseudoInverseSigTerm))
open ClPseudoInverseSigTerm
inductive OpPseudoInverseSigTerm (n : ℕ) : Type
| v : ((fin n) → OpPseudoInverseSigTerm)
| invOL : (OpPseudoInverseSigTerm → OpPseudoInverseSigTerm)
| opOL : (OpPseudoInverseSigTerm → (OpPseudoInverseSigTerm → OpPseudoInverseSigTerm))
open OpPseudoInverseSigTerm
inductive OpPseudoInverseSigTerm2 (n : ℕ) (A : Type) : Type
| v2 : ((fin n) → OpPseudoInverseSigTerm2)
| sing2 : (A → OpPseudoInverseSigTerm2)
| invOL2 : (OpPseudoInverseSigTerm2 → OpPseudoInverseSigTerm2)
| opOL2 : (OpPseudoInverseSigTerm2 → (OpPseudoInverseSigTerm2 → OpPseudoInverseSigTerm2))
open OpPseudoInverseSigTerm2
def simplifyCl {A : Type} : ((ClPseudoInverseSigTerm A) → (ClPseudoInverseSigTerm A))
| (invCl x1) := (invCl (simplifyCl x1))
| (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2))
| (sing x1) := (sing x1)
def simplifyOpB {n : ℕ} : ((OpPseudoInverseSigTerm n) → (OpPseudoInverseSigTerm n))
| (invOL x1) := (invOL (simplifyOpB x1))
| (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2))
| (v x1) := (v x1)
def simplifyOp {n : ℕ} {A : Type} : ((OpPseudoInverseSigTerm2 n A) → (OpPseudoInverseSigTerm2 n A))
| (invOL2 x1) := (invOL2 (simplifyOp x1))
| (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2))
| (v2 x1) := (v2 x1)
| (sing2 x1) := (sing2 x1)
def evalB {A : Type} : ((PseudoInverseSig A) → (PseudoInverseSigTerm → A))
| Ps (invL x1) := ((inv Ps) (evalB Ps x1))
| Ps (opL x1 x2) := ((op Ps) (evalB Ps x1) (evalB Ps x2))
def evalCl {A : Type} : ((PseudoInverseSig A) → ((ClPseudoInverseSigTerm A) → A))
| Ps (sing x1) := x1
| Ps (invCl x1) := ((inv Ps) (evalCl Ps x1))
| Ps (opCl x1 x2) := ((op Ps) (evalCl Ps x1) (evalCl Ps x2))
def evalOpB {A : Type} {n : ℕ} : ((PseudoInverseSig A) → ((vector A n) → ((OpPseudoInverseSigTerm n) → A)))
| Ps vars (v x1) := (nth vars x1)
| Ps vars (invOL x1) := ((inv Ps) (evalOpB Ps vars x1))
| Ps vars (opOL x1 x2) := ((op Ps) (evalOpB Ps vars x1) (evalOpB Ps vars x2))
def evalOp {A : Type} {n : ℕ} : ((PseudoInverseSig A) → ((vector A n) → ((OpPseudoInverseSigTerm2 n A) → A)))
| Ps vars (v2 x1) := (nth vars x1)
| Ps vars (sing2 x1) := x1
| Ps vars (invOL2 x1) := ((inv Ps) (evalOp Ps vars x1))
| Ps vars (opOL2 x1 x2) := ((op Ps) (evalOp Ps vars x1) (evalOp Ps vars x2))
def inductionB {P : (PseudoInverseSigTerm → Type)} : ((∀ (x1 : PseudoInverseSigTerm) , ((P x1) → (P (invL x1)))) → ((∀ (x1 x2 : PseudoInverseSigTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → (∀ (x : PseudoInverseSigTerm) , (P x))))
| pinvl popl (invL x1) := (pinvl _ (inductionB pinvl popl x1))
| pinvl popl (opL x1 x2) := (popl _ _ (inductionB pinvl popl x1) (inductionB pinvl popl x2))
def inductionCl {A : Type} {P : ((ClPseudoInverseSigTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 : (ClPseudoInverseSigTerm A)) , ((P x1) → (P (invCl x1)))) → ((∀ (x1 x2 : (ClPseudoInverseSigTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → (∀ (x : (ClPseudoInverseSigTerm A)) , (P x)))))
| psing pinvcl popcl (sing x1) := (psing x1)
| psing pinvcl popcl (invCl x1) := (pinvcl _ (inductionCl psing pinvcl popcl x1))
| psing pinvcl popcl (opCl x1 x2) := (popcl _ _ (inductionCl psing pinvcl popcl x1) (inductionCl psing pinvcl popcl x2))
def inductionOpB {n : ℕ} {P : ((OpPseudoInverseSigTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 : (OpPseudoInverseSigTerm n)) , ((P x1) → (P (invOL x1)))) → ((∀ (x1 x2 : (OpPseudoInverseSigTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → (∀ (x : (OpPseudoInverseSigTerm n)) , (P x)))))
| pv pinvol popol (v x1) := (pv x1)
| pv pinvol popol (invOL x1) := (pinvol _ (inductionOpB pv pinvol popol x1))
| pv pinvol popol (opOL x1 x2) := (popol _ _ (inductionOpB pv pinvol popol x1) (inductionOpB pv pinvol popol x2))
def inductionOp {n : ℕ} {A : Type} {P : ((OpPseudoInverseSigTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 : (OpPseudoInverseSigTerm2 n A)) , ((P x1) → (P (invOL2 x1)))) → ((∀ (x1 x2 : (OpPseudoInverseSigTerm2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → (∀ (x : (OpPseudoInverseSigTerm2 n A)) , (P x))))))
| pv2 psing2 pinvol2 popol2 (v2 x1) := (pv2 x1)
| pv2 psing2 pinvol2 popol2 (sing2 x1) := (psing2 x1)
| pv2 psing2 pinvol2 popol2 (invOL2 x1) := (pinvol2 _ (inductionOp pv2 psing2 pinvol2 popol2 x1))
| pv2 psing2 pinvol2 popol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 pinvol2 popol2 x1) (inductionOp pv2 psing2 pinvol2 popol2 x2))
def stageB : (PseudoInverseSigTerm → (Staged PseudoInverseSigTerm))
| (invL x1) := (stage1 invL (codeLift1 invL) (stageB x1))
| (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2))
def stageCl {A : Type} : ((ClPseudoInverseSigTerm A) → (Staged (ClPseudoInverseSigTerm A)))
| (sing x1) := (Now (sing x1))
| (invCl x1) := (stage1 invCl (codeLift1 invCl) (stageCl x1))
| (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2))
def stageOpB {n : ℕ} : ((OpPseudoInverseSigTerm n) → (Staged (OpPseudoInverseSigTerm n)))
| (v x1) := (const (code (v x1)))
| (invOL x1) := (stage1 invOL (codeLift1 invOL) (stageOpB x1))
| (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2))
def stageOp {n : ℕ} {A : Type} : ((OpPseudoInverseSigTerm2 n A) → (Staged (OpPseudoInverseSigTerm2 n A)))
| (sing2 x1) := (Now (sing2 x1))
| (v2 x1) := (const (code (v2 x1)))
| (invOL2 x1) := (stage1 invOL2 (codeLift1 invOL2) (stageOp x1))
| (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2))
structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type :=
(invT : ((Repr A) → (Repr A)))
(opT : ((Repr A) → ((Repr A) → (Repr A))))
end PseudoInverseSig |
45573cfedd94dffb5b6d8bf80b67f355cf48a972 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/ring_theory/fractional_ideal.lean | 7eea94685ba78cc896767a2be2522b732cde783a | [
"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 | 53,647 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen, Filippo A. E. Nuccio
-/
import algebra.big_operators.finprod
import ring_theory.integral_closure
import ring_theory.localization.integer
import ring_theory.localization.submodule
import ring_theory.noetherian
import ring_theory.principal_ideal_domain
import tactic.field_simp
/-!
# Fractional ideals
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines fractional ideals of an integral domain and proves basic facts about them.
## Main definitions
Let `S` be a submonoid of an integral domain `R`, `P` the localization of `R` at `S`, and `f` the
natural ring hom from `R` to `P`.
* `is_fractional` defines which `R`-submodules of `P` are fractional ideals
* `fractional_ideal S P` is the type of fractional ideals in `P`
* `has_coe_t (ideal R) (fractional_ideal S P)` instance
* `comm_semiring (fractional_ideal S P)` instance:
the typical ideal operations generalized to fractional ideals
* `lattice (fractional_ideal S P)` instance
* `map` is the pushforward of a fractional ideal along an algebra morphism
Let `K` be the localization of `R` at `R⁰ = R \ {0}` (i.e. the field of fractions).
* `fractional_ideal R⁰ K` is the type of fractional ideals in the field of fractions
* `has_div (fractional_ideal R⁰ K)` instance:
the ideal quotient `I / J` (typically written $I : J$, but a `:` operator cannot be defined)
## Main statements
* `mul_left_mono` and `mul_right_mono` state that ideal multiplication is monotone
* `prod_one_self_div_eq` states that `1 / I` is the inverse of `I` if one exists
* `is_noetherian` states that every fractional ideal of a noetherian integral domain is noetherian
## Implementation notes
Fractional ideals are considered equal when they contain the same elements,
independent of the denominator `a : R` such that `a I ⊆ R`.
Thus, we define `fractional_ideal` to be the subtype of the predicate `is_fractional`,
instead of having `fractional_ideal` be a structure of which `a` is a field.
Most definitions in this file specialize operations from submodules to fractional ideals,
proving that the result of this operation is fractional if the input is fractional.
Exceptions to this rule are defining `(+) := (⊔)` and `⊥ := 0`,
in order to re-use their respective proof terms.
We can still use `simp` to show `↑I + ↑J = ↑(I + J)` and `↑⊥ = ↑0`.
Many results in fact do not need that `P` is a localization, only that `P` is an
`R`-algebra. We omit the `is_localization` parameter whenever this is practical.
Similarly, we don't assume that the localization is a field until we need it to
define ideal quotients. When this assumption is needed, we replace `S` with `R⁰`,
making the localization a field.
## References
* https://en.wikipedia.org/wiki/Fractional_ideal
## Tags
fractional ideal, fractional ideals, invertible ideal
-/
open is_localization
open_locale pointwise
open_locale non_zero_divisors
section defs
variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P]
variables [algebra R P]
variables (S)
/-- A submodule `I` is a fractional ideal if `a I ⊆ R` for some `a ≠ 0`. -/
def is_fractional (I : submodule R P) :=
∃ a ∈ S, ∀ b ∈ I, is_integer R (a • b)
variables (S P)
/-- The fractional ideals of a domain `R` are ideals of `R` divided by some `a ∈ R`.
More precisely, let `P` be a localization of `R` at some submonoid `S`,
then a fractional ideal `I ⊆ P` is an `R`-submodule of `P`,
such that there is a nonzero `a : R` with `a I ⊆ R`.
-/
def fractional_ideal :=
{I : submodule R P // is_fractional S I}
end defs
namespace fractional_ideal
open set
open submodule
variables {R : Type*} [comm_ring R] {S : submonoid R} {P : Type*} [comm_ring P]
variables [algebra R P] [loc : is_localization S P]
/-- Map a fractional ideal `I` to a submodule by forgetting that `∃ a, a I ⊆ R`.
This coercion is typically called `coe_to_submodule` in lemma names
(or `coe` when the coercion is clear from the context),
not to be confused with `is_localization.coe_submodule : ideal R → submodule R P`
(which we use to define `coe : ideal R → fractional_ideal S P`).
-/
instance : has_coe (fractional_ideal S P) (submodule R P) := ⟨λ I, I.val⟩
protected lemma is_fractional (I : fractional_ideal S P) :
is_fractional S (I : submodule R P) :=
I.prop
section set_like
instance : set_like (fractional_ideal S P) P :=
{ coe := λ I, ↑(I : submodule R P),
coe_injective' := set_like.coe_injective.comp subtype.coe_injective }
@[simp] lemma mem_coe {I : fractional_ideal S P} {x : P} :
x ∈ (I : submodule R P) ↔ x ∈ I :=
iff.rfl
@[ext] lemma ext {I J : fractional_ideal S P} : (∀ x, x ∈ I ↔ x ∈ J) → I = J := set_like.ext
/-- Copy of a `fractional_ideal` with a new underlying set equal to the old one.
Useful to fix definitional equalities. -/
protected def copy (p : fractional_ideal S P) (s : set P) (hs : s = ↑p) : fractional_ideal S P :=
⟨submodule.copy p s hs, by { convert p.is_fractional, ext, simp only [hs], refl }⟩
@[simp] lemma coe_copy (p : fractional_ideal S P) (s : set P) (hs : s = ↑p) :
↑(p.copy s hs) = s :=
rfl
lemma coe_eq (p : fractional_ideal S P) (s : set P) (hs : s = ↑p) : p.copy s hs = p :=
set_like.coe_injective hs
end set_like
@[simp] lemma val_eq_coe (I : fractional_ideal S P) : I.val = I := rfl
@[simp, norm_cast] lemma coe_mk (I : submodule R P) (hI : is_fractional S I) :
(subtype.mk I hI : submodule R P) = I := rfl
/-! Transfer instances from `submodule R P` to `fractional_ideal S P`. -/
instance (I : fractional_ideal S P) : add_comm_group I := submodule.add_comm_group ↑I
instance (I : fractional_ideal S P) : module R I := submodule.module ↑I
lemma coe_to_submodule_injective :
function.injective (coe : fractional_ideal S P → submodule R P) :=
subtype.coe_injective
lemma coe_to_submodule_inj {I J : fractional_ideal S P} : (I : submodule R P) = J ↔ I = J :=
coe_to_submodule_injective.eq_iff
lemma is_fractional_of_le_one (I : submodule R P) (h : I ≤ 1) : is_fractional S I :=
begin
use [1, S.one_mem],
intros b hb,
rw one_smul,
obtain ⟨b', b'_mem, rfl⟩ := h hb,
exact set.mem_range_self b',
end
lemma is_fractional_of_le {I : submodule R P} {J : fractional_ideal S P} (hIJ : I ≤ J) :
is_fractional S I :=
begin
obtain ⟨a, a_mem, ha⟩ := J.is_fractional,
use [a, a_mem],
intros b b_mem,
exact ha b (hIJ b_mem)
end
/-- Map an ideal `I` to a fractional ideal by forgetting `I` is integral.
This is a bundled version of `is_localization.coe_submodule : ideal R → submodule R P`,
which is not to be confused with the `coe : fractional_ideal S P → submodule R P`,
also called `coe_to_submodule` in theorem names.
This map is available as a ring hom, called `fractional_ideal.coe_ideal_hom`.
-/
-- Is a `coe_t` rather than `coe` to speed up failing inference, see library note [use has_coe_t]
instance : has_coe_t (ideal R) (fractional_ideal S P) :=
⟨λ I, ⟨coe_submodule P I,
is_fractional_of_le_one _ $ by simpa using coe_submodule_mono P (le_top : I ≤ ⊤)⟩⟩
@[simp, norm_cast] lemma coe_coe_ideal (I : ideal R) :
((I : fractional_ideal S P) : submodule R P) = coe_submodule P I := rfl
variables (S)
@[simp] lemma mem_coe_ideal {x : P} {I : ideal R} :
x ∈ (I : fractional_ideal S P) ↔ ∃ x', x' ∈ I ∧ algebra_map R P x' = x :=
mem_coe_submodule _ _
lemma mem_coe_ideal_of_mem {x : R} {I : ideal R} (hx : x ∈ I) :
algebra_map R P x ∈ (I : fractional_ideal S P) :=
(mem_coe_ideal S).mpr ⟨x, hx, rfl⟩
lemma coe_ideal_le_coe_ideal' [is_localization S P] (h : S ≤ non_zero_divisors R)
{I J : ideal R} : (I : fractional_ideal S P) ≤ J ↔ I ≤ J :=
coe_submodule_le_coe_submodule h
@[simp] lemma coe_ideal_le_coe_ideal (K : Type*) [comm_ring K] [algebra R K] [is_fraction_ring R K]
{I J : ideal R} : (I : fractional_ideal R⁰ K) ≤ J ↔ I ≤ J :=
is_fraction_ring.coe_submodule_le_coe_submodule
instance : has_zero (fractional_ideal S P) := ⟨(0 : ideal R)⟩
@[simp] lemma mem_zero_iff {x : P} : x ∈ (0 : fractional_ideal S P) ↔ x = 0 :=
⟨(λ ⟨x', x'_mem_zero, x'_eq_x⟩,
have x'_eq_zero : x' = 0 := x'_mem_zero,
by simp [x'_eq_x.symm, x'_eq_zero]),
(λ hx, ⟨0, rfl, by simp [hx]⟩)⟩
variables {S}
@[simp, norm_cast] lemma coe_zero : ↑(0 : fractional_ideal S P) = (⊥ : submodule R P) :=
submodule.ext $ λ _, mem_zero_iff S
@[simp, norm_cast] lemma coe_ideal_bot : ((⊥ : ideal R) : fractional_ideal S P) = 0 := rfl
variables (P)
include loc
@[simp] lemma exists_mem_to_map_eq {x : R} {I : ideal R} (h : S ≤ non_zero_divisors R) :
(∃ x', x' ∈ I ∧ algebra_map R P x' = algebra_map R P x) ↔ x ∈ I :=
⟨λ ⟨x', hx', eq⟩, is_localization.injective _ h eq ▸ hx', λ h, ⟨x, h, rfl⟩⟩
variables {P}
lemma coe_ideal_injective' (h : S ≤ non_zero_divisors R) :
function.injective (coe : ideal R → fractional_ideal S P) :=
λ _ _ h', ((coe_ideal_le_coe_ideal' S h).mp h'.le).antisymm ((coe_ideal_le_coe_ideal' S h).mp h'.ge)
lemma coe_ideal_inj' (h : S ≤ non_zero_divisors R) {I J : ideal R} :
(I : fractional_ideal S P) = J ↔ I = J :=
(coe_ideal_injective' h).eq_iff
@[simp] lemma coe_ideal_eq_zero' {I : ideal R} (h : S ≤ non_zero_divisors R) :
(I : fractional_ideal S P) = 0 ↔ I = (⊥ : ideal R) :=
coe_ideal_inj' h
lemma coe_ideal_ne_zero' {I : ideal R} (h : S ≤ non_zero_divisors R) :
(I : fractional_ideal S P) ≠ 0 ↔ I ≠ (⊥ : ideal R) :=
not_iff_not.mpr $ coe_ideal_eq_zero' h
omit loc
lemma coe_to_submodule_eq_bot {I : fractional_ideal S P} :
(I : submodule R P) = ⊥ ↔ I = 0 :=
⟨λ h, coe_to_submodule_injective (by simp [h]),
λ h, by simp [h]⟩
lemma coe_to_submodule_ne_bot {I : fractional_ideal S P} :
↑I ≠ (⊥ : submodule R P) ↔ I ≠ 0 :=
not_iff_not.mpr coe_to_submodule_eq_bot
instance : inhabited (fractional_ideal S P) := ⟨0⟩
instance : has_one (fractional_ideal S P) :=
⟨(⊤ : ideal R)⟩
variables (S)
@[simp, norm_cast] lemma coe_ideal_top : ((⊤ : ideal R) : fractional_ideal S P) = 1 := rfl
lemma mem_one_iff {x : P} : x ∈ (1 : fractional_ideal S P) ↔ ∃ x' : R, algebra_map R P x' = x :=
iff.intro (λ ⟨x', _, h⟩, ⟨x', h⟩) (λ ⟨x', h⟩, ⟨x', ⟨⟩, h⟩)
lemma coe_mem_one (x : R) : algebra_map R P x ∈ (1 : fractional_ideal S P) :=
(mem_one_iff S).mpr ⟨x, rfl⟩
lemma one_mem_one : (1 : P) ∈ (1 : fractional_ideal S P) :=
(mem_one_iff S).mpr ⟨1, ring_hom.map_one _⟩
variables {S}
/-- `(1 : fractional_ideal S P)` is defined as the R-submodule `f(R) ≤ P`.
However, this is not definitionally equal to `1 : submodule R P`,
which is proved in the actual `simp` lemma `coe_one`. -/
lemma coe_one_eq_coe_submodule_top :
↑(1 : fractional_ideal S P) = coe_submodule P (⊤ : ideal R) :=
rfl
@[simp, norm_cast] lemma coe_one :
(↑(1 : fractional_ideal S P) : submodule R P) = 1 :=
by rw [coe_one_eq_coe_submodule_top, coe_submodule_top]
section lattice
/-!
### `lattice` section
Defines the order on fractional ideals as inclusion of their underlying sets,
and ports the lattice structure on submodules to fractional ideals.
-/
@[simp] lemma coe_le_coe {I J : fractional_ideal S P} :
(I : submodule R P) ≤ (J : submodule R P) ↔ I ≤ J :=
iff.rfl
lemma zero_le (I : fractional_ideal S P) : 0 ≤ I :=
begin
intros x hx,
convert submodule.zero_mem _,
simpa using hx
end
instance order_bot : order_bot (fractional_ideal S P) :=
{ bot := 0,
bot_le := zero_le }
@[simp] lemma bot_eq_zero : (⊥ : fractional_ideal S P) = 0 :=
rfl
@[simp] lemma le_zero_iff {I : fractional_ideal S P} : I ≤ 0 ↔ I = 0 :=
le_bot_iff
lemma eq_zero_iff {I : fractional_ideal S P} : I = 0 ↔ (∀ x ∈ I, x = (0 : P)) :=
⟨ (λ h x hx, by simpa [h, mem_zero_iff] using hx),
(λ h, le_bot_iff.mp (λ x hx, (mem_zero_iff S).mpr (h x hx))) ⟩
lemma _root_.is_fractional.sup {I J : submodule R P} :
is_fractional S I → is_fractional S J → is_fractional S (I ⊔ J)
| ⟨aI, haI, hI⟩ ⟨aJ, haJ, hJ⟩ := ⟨aI * aJ, S.mul_mem haI haJ, λ b hb, begin
rcases mem_sup.mp hb with ⟨bI, hbI, bJ, hbJ, rfl⟩,
rw smul_add,
apply is_integer_add,
{ rw [mul_smul, smul_comm],
exact is_integer_smul (hI bI hbI), },
{ rw mul_smul,
exact is_integer_smul (hJ bJ hbJ) }
end⟩
lemma _root_.is_fractional.inf_right {I : submodule R P} :
is_fractional S I → ∀ J, is_fractional S (I ⊓ J)
| ⟨aI, haI, hI⟩ J := ⟨aI, haI, λ b hb, begin
rcases mem_inf.mp hb with ⟨hbI, hbJ⟩,
exact hI b hbI
end⟩
instance : has_inf (fractional_ideal S P) := ⟨λ I J, ⟨I ⊓ J, I.is_fractional.inf_right J⟩⟩
@[simp, norm_cast]
lemma coe_inf (I J : fractional_ideal S P) : ↑(I ⊓ J) = (I ⊓ J : submodule R P) := rfl
instance : has_sup (fractional_ideal S P) := ⟨λ I J, ⟨I ⊔ J, I.is_fractional.sup J.is_fractional⟩⟩
@[norm_cast]
lemma coe_sup (I J : fractional_ideal S P) : ↑(I ⊔ J) = (I ⊔ J : submodule R P) := rfl
instance lattice : lattice (fractional_ideal S P) :=
function.injective.lattice _ subtype.coe_injective coe_sup coe_inf
instance : semilattice_sup (fractional_ideal S P) :=
{ ..fractional_ideal.lattice }
end lattice
section semiring
instance : has_add (fractional_ideal S P) := ⟨(⊔)⟩
@[simp]
lemma sup_eq_add (I J : fractional_ideal S P) : I ⊔ J = I + J := rfl
@[simp, norm_cast]
lemma coe_add (I J : fractional_ideal S P) : (↑(I + J) : submodule R P) = I + J := rfl
@[simp, norm_cast]
lemma coe_ideal_sup (I J : ideal R) : ↑(I ⊔ J) = (I + J : fractional_ideal S P) :=
coe_to_submodule_injective $ coe_submodule_sup _ _ _
lemma _root_.is_fractional.nsmul {I : submodule R P} :
Π n : ℕ, is_fractional S I → is_fractional S (n • I : submodule R P)
| 0 _ := begin
rw [zero_smul],
convert ((0 : ideal R) : fractional_ideal S P).is_fractional,
simp,
end
| (n + 1) h := begin
rw succ_nsmul,
exact h.sup (_root_.is_fractional.nsmul n h)
end
instance : has_smul ℕ (fractional_ideal S P) :=
{ smul := λ n I, ⟨n • I, I.is_fractional.nsmul n⟩}
@[norm_cast]
lemma coe_nsmul (n : ℕ) (I : fractional_ideal S P) : (↑(n • I) : submodule R P) = n • I := rfl
lemma _root_.is_fractional.mul {I J : submodule R P} :
is_fractional S I → is_fractional S J → is_fractional S (I * J : submodule R P)
| ⟨aI, haI, hI⟩ ⟨aJ, haJ, hJ⟩ := ⟨aI * aJ, S.mul_mem haI haJ, λ b hb, begin
apply submodule.mul_induction_on hb,
{ intros m hm n hn,
obtain ⟨n', hn'⟩ := hJ n hn,
rw [mul_smul, mul_comm m, ← smul_mul_assoc, ← hn', ← algebra.smul_def],
apply hI,
exact submodule.smul_mem _ _ hm },
{ intros x y hx hy,
rw smul_add,
apply is_integer_add hx hy },
end⟩
lemma _root_.is_fractional.pow {I : submodule R P} (h : is_fractional S I) :
∀ n : ℕ, is_fractional S (I ^ n : submodule R P)
| 0 := is_fractional_of_le_one _ (pow_zero _).le
| (n + 1) := (pow_succ I n).symm ▸ h.mul (_root_.is_fractional.pow n)
/-- `fractional_ideal.mul` is the product of two fractional ideals,
used to define the `has_mul` instance.
This is only an auxiliary definition: the preferred way of writing `I.mul J` is `I * J`.
Elaborated terms involving `fractional_ideal` tend to grow quite large,
so by making definitions irreducible, we hope to avoid deep unfolds.
-/
@[irreducible]
def mul (I J : fractional_ideal S P) : fractional_ideal S P :=
⟨I * J, I.is_fractional.mul J.is_fractional⟩
-- local attribute [semireducible] mul
instance : has_mul (fractional_ideal S P) := ⟨λ I J, mul I J⟩
@[simp] lemma mul_eq_mul (I J : fractional_ideal S P) : mul I J = I * J := rfl
lemma mul_def (I J : fractional_ideal S P) : I * J = ⟨I * J, I.is_fractional.mul J.is_fractional⟩ :=
by simp only [← mul_eq_mul, mul]
@[simp, norm_cast]
lemma coe_mul (I J : fractional_ideal S P) : (↑(I * J) : submodule R P) = I * J :=
by { simp only [mul_def], refl }
@[simp, norm_cast]
lemma coe_ideal_mul (I J : ideal R) : (↑(I * J) : fractional_ideal S P) = I * J :=
begin
simp only [mul_def],
exact coe_to_submodule_injective (coe_submodule_mul _ _ _)
end
lemma mul_left_mono (I : fractional_ideal S P) : monotone ((*) I) :=
begin
intros J J' h,
simp only [mul_def],
exact mul_le.mpr (λ x hx y hy, mul_mem_mul hx (h hy))
end
lemma mul_right_mono (I : fractional_ideal S P) : monotone (λ J, J * I) :=
begin
intros J J' h,
simp only [mul_def],
exact mul_le.mpr (λ x hx y hy, mul_mem_mul (h hx) hy)
end
lemma mul_mem_mul {I J : fractional_ideal S P} {i j : P} (hi : i ∈ I) (hj : j ∈ J) :
i * j ∈ I * J :=
by { simp only [mul_def], exact submodule.mul_mem_mul hi hj }
lemma mul_le {I J K : fractional_ideal S P} :
I * J ≤ K ↔ (∀ (i ∈ I) (j ∈ J), i * j ∈ K) :=
by { simp only [mul_def], exact submodule.mul_le }
instance : has_pow (fractional_ideal S P) ℕ := ⟨λ I n, ⟨I^n, I.is_fractional.pow n⟩⟩
@[simp, norm_cast]
lemma coe_pow (I : fractional_ideal S P) (n : ℕ) : ↑(I ^ n) = (I ^ n : submodule R P) := rfl
@[elab_as_eliminator] protected theorem mul_induction_on
{I J : fractional_ideal S P}
{C : P → Prop} {r : P} (hr : r ∈ I * J)
(hm : ∀ (i ∈ I) (j ∈ J), C (i * j))
(ha : ∀ x y, C x → C y → C (x + y)) : C r :=
begin
simp only [mul_def] at hr,
exact submodule.mul_induction_on hr hm ha
end
instance : has_nat_cast (fractional_ideal S P) := ⟨nat.unary_cast⟩
lemma coe_nat_cast (n : ℕ) : ((n : fractional_ideal S P) : submodule R P) = n :=
show ↑n.unary_cast = ↑n, by induction n; simp [*, nat.unary_cast]
instance : comm_semiring (fractional_ideal S P) :=
function.injective.comm_semiring coe subtype.coe_injective
coe_zero coe_one coe_add coe_mul (λ _ _, coe_nsmul _ _) coe_pow coe_nat_cast
variables (S P)
/-- `fractional_ideal.submodule.has_coe` as a bundled `ring_hom`. -/
@[simps] def coe_submodule_hom : fractional_ideal S P →+* submodule R P :=
⟨coe, coe_one, coe_mul, coe_zero, coe_add⟩
variables {S P}
section order
lemma add_le_add_left {I J : fractional_ideal S P} (hIJ : I ≤ J) (J' : fractional_ideal S P) :
J' + I ≤ J' + J :=
sup_le_sup_left hIJ J'
lemma mul_le_mul_left {I J : fractional_ideal S P} (hIJ : I ≤ J) (J' : fractional_ideal S P) :
J' * I ≤ J' * J :=
mul_le.mpr (λ k hk j hj, mul_mem_mul hk (hIJ hj))
lemma le_self_mul_self {I : fractional_ideal S P} (hI: 1 ≤ I) : I ≤ I * I :=
begin
convert mul_left_mono I hI,
exact (mul_one I).symm
end
lemma mul_self_le_self {I : fractional_ideal S P} (hI: I ≤ 1) : I * I ≤ I :=
begin
convert mul_left_mono I hI,
exact (mul_one I).symm
end
lemma coe_ideal_le_one {I : ideal R} : (I : fractional_ideal S P) ≤ 1 :=
λ x hx, let ⟨y, _, hy⟩ := (mem_coe_ideal S).mp hx in (mem_one_iff S).mpr ⟨y, hy⟩
lemma le_one_iff_exists_coe_ideal {J : fractional_ideal S P} :
J ≤ (1 : fractional_ideal S P) ↔ ∃ (I : ideal R), ↑I = J :=
begin
split,
{ intro hJ,
refine ⟨⟨{x : R | algebra_map R P x ∈ J}, _, _, _⟩, _⟩,
{ intros a b ha hb,
rw [mem_set_of_eq, ring_hom.map_add],
exact J.val.add_mem ha hb },
{ rw [mem_set_of_eq, ring_hom.map_zero],
exact J.val.zero_mem },
{ intros c x hx,
rw [smul_eq_mul, mem_set_of_eq, ring_hom.map_mul, ← algebra.smul_def],
exact J.val.smul_mem c hx },
{ ext x,
split,
{ rintros ⟨y, hy, eq_y⟩,
rwa ← eq_y },
{ intro hx,
obtain ⟨y, eq_x⟩ := (mem_one_iff S).mp (hJ hx),
rw ← eq_x at *,
exact ⟨y, hx, rfl⟩ } } },
{ rintro ⟨I, hI⟩,
rw ← hI,
apply coe_ideal_le_one },
end
@[simp] lemma one_le {I : fractional_ideal S P} :
1 ≤ I ↔ (1 : P) ∈ I :=
by rw [← coe_le_coe, coe_one, submodule.one_le, mem_coe]
variables (S P)
/-- `coe_ideal_hom (S : submonoid R) P` is `coe : ideal R → fractional_ideal S P` as a ring hom -/
@[simps]
def coe_ideal_hom : ideal R →+* fractional_ideal S P :=
{ to_fun := coe,
map_add' := coe_ideal_sup,
map_mul' := coe_ideal_mul,
map_one' := by rw [ideal.one_eq_top, coe_ideal_top],
map_zero' := coe_ideal_bot }
lemma coe_ideal_pow (I : ideal R) (n : ℕ) : (↑(I^n) : fractional_ideal S P) = I^n :=
(coe_ideal_hom S P).map_pow _ n
open_locale big_operators
lemma coe_ideal_finprod [is_localization S P] {α : Sort*} {f : α → ideal R}
(hS : S ≤ non_zero_divisors R) :
((∏ᶠ a : α, f a : ideal R) : fractional_ideal S P) = ∏ᶠ a : α, (f a : fractional_ideal S P) :=
monoid_hom.map_finprod_of_injective (coe_ideal_hom S P).to_monoid_hom (coe_ideal_injective' hS) f
end order
variables {P' : Type*} [comm_ring P'] [algebra R P'] [loc' : is_localization S P']
variables {P'' : Type*} [comm_ring P''] [algebra R P''] [loc'' : is_localization S P'']
lemma _root_.is_fractional.map (g : P →ₐ[R] P') {I : submodule R P} :
is_fractional S I → is_fractional S (submodule.map g.to_linear_map I)
| ⟨a, a_nonzero, hI⟩ := ⟨a, a_nonzero, λ b hb, begin
obtain ⟨b', b'_mem, hb'⟩ := submodule.mem_map.mp hb,
obtain ⟨x, hx⟩ := hI b' b'_mem,
use x,
erw [←g.commutes, hx, g.map_smul, hb']
end⟩
/-- `I.map g` is the pushforward of the fractional ideal `I` along the algebra morphism `g` -/
def map (g : P →ₐ[R] P') :
fractional_ideal S P → fractional_ideal S P' :=
λ I, ⟨submodule.map g.to_linear_map I, I.is_fractional.map g⟩
@[simp, norm_cast] lemma coe_map (g : P →ₐ[R] P') (I : fractional_ideal S P) :
↑(map g I) = submodule.map g.to_linear_map I := rfl
@[simp] lemma mem_map {I : fractional_ideal S P} {g : P →ₐ[R] P'}
{y : P'} : y ∈ I.map g ↔ ∃ x, x ∈ I ∧ g x = y :=
submodule.mem_map
variables (I J : fractional_ideal S P) (g : P →ₐ[R] P')
@[simp] lemma map_id : I.map (alg_hom.id _ _) = I :=
coe_to_submodule_injective (submodule.map_id I)
@[simp] lemma map_comp (g' : P' →ₐ[R] P'') :
I.map (g'.comp g) = (I.map g).map g' :=
coe_to_submodule_injective (submodule.map_comp g.to_linear_map g'.to_linear_map I)
@[simp, norm_cast] lemma map_coe_ideal (I : ideal R) :
(I : fractional_ideal S P).map g = I :=
begin
ext x,
simp only [mem_coe_ideal],
split,
{ rintro ⟨_, ⟨y, hy, rfl⟩, rfl⟩,
exact ⟨y, hy, (g.commutes y).symm⟩ },
{ rintro ⟨y, hy, rfl⟩,
exact ⟨_, ⟨y, hy, rfl⟩, g.commutes y⟩ },
end
@[simp] lemma map_one :
(1 : fractional_ideal S P).map g = 1 :=
map_coe_ideal g ⊤
@[simp] lemma map_zero :
(0 : fractional_ideal S P).map g = 0 :=
map_coe_ideal g 0
@[simp] lemma map_add : (I + J).map g = I.map g + J.map g :=
coe_to_submodule_injective (submodule.map_sup _ _ _)
@[simp] lemma map_mul : (I * J).map g = I.map g * J.map g :=
begin
simp only [mul_def],
exact coe_to_submodule_injective (submodule.map_mul _ _ _)
end
@[simp] lemma map_map_symm (g : P ≃ₐ[R] P') :
(I.map (g : P →ₐ[R] P')).map (g.symm : P' →ₐ[R] P) = I :=
by rw [←map_comp, g.symm_comp, map_id]
@[simp] lemma map_symm_map (I : fractional_ideal S P') (g : P ≃ₐ[R] P') :
(I.map (g.symm : P' →ₐ[R] P)).map (g : P →ₐ[R] P') = I :=
by rw [←map_comp, g.comp_symm, map_id]
lemma map_mem_map {f : P →ₐ[R] P'} (h : function.injective f) {x : P} {I : fractional_ideal S P} :
f x ∈ map f I ↔ x ∈ I :=
mem_map.trans ⟨λ ⟨x', hx', x'_eq⟩, h x'_eq ▸ hx', λ h, ⟨x, h, rfl⟩⟩
lemma map_injective (f : P →ₐ[R] P') (h : function.injective f) :
function.injective (map f : fractional_ideal S P → fractional_ideal S P') :=
λ I J hIJ, ext (λ x, (map_mem_map h).symm.trans (hIJ.symm ▸ map_mem_map h))
/-- If `g` is an equivalence, `map g` is an isomorphism -/
def map_equiv (g : P ≃ₐ[R] P') :
fractional_ideal S P ≃+* fractional_ideal S P' :=
{ to_fun := map g,
inv_fun := map g.symm,
map_add' := λ I J, map_add I J _,
map_mul' := λ I J, map_mul I J _,
left_inv := λ I, by { rw [←map_comp, alg_equiv.symm_comp, map_id] },
right_inv := λ I, by { rw [←map_comp, alg_equiv.comp_symm, map_id] } }
@[simp] lemma coe_fun_map_equiv (g : P ≃ₐ[R] P') :
(map_equiv g : fractional_ideal S P → fractional_ideal S P') = map g :=
rfl
@[simp] lemma map_equiv_apply (g : P ≃ₐ[R] P') (I : fractional_ideal S P) :
map_equiv g I = map ↑g I := rfl
@[simp] lemma map_equiv_symm (g : P ≃ₐ[R] P') :
((map_equiv g).symm : fractional_ideal S P' ≃+* _) = map_equiv g.symm := rfl
@[simp] lemma map_equiv_refl :
map_equiv alg_equiv.refl = ring_equiv.refl (fractional_ideal S P) :=
ring_equiv.ext (λ x, by simp)
lemma is_fractional_span_iff {s : set P} :
is_fractional S (span R s) ↔ ∃ a ∈ S, ∀ (b : P), b ∈ s → is_integer R (a • b) :=
⟨λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, h b (subset_span hb)⟩,
λ ⟨a, a_mem, h⟩, ⟨a, a_mem, λ b hb, span_induction hb
h
(by { rw smul_zero, exact is_integer_zero })
(λ x y hx hy, by { rw smul_add, exact is_integer_add hx hy })
(λ s x hx, by { rw smul_comm, exact is_integer_smul hx })⟩⟩
include loc
lemma is_fractional_of_fg {I : submodule R P} (hI : I.fg) :
is_fractional S I :=
begin
rcases hI with ⟨I, rfl⟩,
rcases exist_integer_multiples_of_finset S I with ⟨⟨s, hs1⟩, hs⟩,
rw is_fractional_span_iff,
exact ⟨s, hs1, hs⟩,
end
omit loc
lemma mem_span_mul_finite_of_mem_mul {I J : fractional_ideal S P} {x : P} (hx : x ∈ I * J) :
∃ (T T' : finset P), (T : set P) ⊆ I ∧ (T' : set P) ⊆ J ∧ x ∈ span R (T * T' : set P) :=
submodule.mem_span_mul_finite_of_mem_mul (by simpa using mem_coe.mpr hx)
variables (S)
lemma coe_ideal_fg (inj : function.injective (algebra_map R P)) (I : ideal R) :
fg ((I : fractional_ideal S P) : submodule R P) ↔ I.fg :=
coe_submodule_fg _ inj _
variables {S}
lemma fg_unit (I : (fractional_ideal S P)ˣ) :
fg (I : submodule R P) :=
submodule.fg_unit $ units.map (coe_submodule_hom S P).to_monoid_hom I
lemma fg_of_is_unit (I : fractional_ideal S P) (h : is_unit I) :
fg (I : submodule R P) :=
fg_unit h.unit
lemma _root_.ideal.fg_of_is_unit (inj : function.injective (algebra_map R P))
(I : ideal R) (h : is_unit (I : fractional_ideal S P)) :
I.fg :=
by { rw ← coe_ideal_fg S inj I, exact fg_of_is_unit I h }
variables (S P P')
include loc loc'
/-- `canonical_equiv f f'` is the canonical equivalence between the fractional
ideals in `P` and in `P'` -/
@[irreducible]
noncomputable def canonical_equiv :
fractional_ideal S P ≃+* fractional_ideal S P' :=
map_equiv
{ commutes' := λ r, ring_equiv_of_ring_equiv_eq _ _,
..ring_equiv_of_ring_equiv P P' (ring_equiv.refl R)
(show S.map _ = S, by rw [ring_equiv.to_monoid_hom_refl, submonoid.map_id]) }
@[simp] lemma mem_canonical_equiv_apply {I : fractional_ideal S P} {x : P'} :
x ∈ canonical_equiv S P P' I ↔
∃ y ∈ I, is_localization.map P' (ring_hom.id R)
(λ y (hy : y ∈ S), show ring_hom.id R y ∈ S, from hy) (y : P) = x :=
begin
rw [canonical_equiv, map_equiv_apply, mem_map],
exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩
end
@[simp] lemma canonical_equiv_symm :
(canonical_equiv S P P').symm = canonical_equiv S P' P :=
ring_equiv.ext $ λ I, set_like.ext_iff.mpr $ λ x,
by { rw [mem_canonical_equiv_apply, canonical_equiv, map_equiv_symm, map_equiv,
ring_equiv.coe_mk, mem_map],
exact ⟨λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩, λ ⟨y, mem, eq⟩, ⟨y, mem, eq⟩⟩ }
lemma canonical_equiv_flip (I) :
canonical_equiv S P P' (canonical_equiv S P' P I) = I :=
by rw [←canonical_equiv_symm, ring_equiv.symm_apply_apply]
@[simp]
lemma canonical_equiv_canonical_equiv (P'' : Type*) [comm_ring P''] [algebra R P'']
[is_localization S P''] (I : fractional_ideal S P) :
canonical_equiv S P' P'' (canonical_equiv S P P' I) = canonical_equiv S P P'' I :=
begin
ext,
simp only [is_localization.map_map, ring_hom_inv_pair.comp_eq₂, mem_canonical_equiv_apply,
exists_prop, exists_exists_and_eq_and],
refl
end
lemma canonical_equiv_trans_canonical_equiv (P'' : Type*) [comm_ring P'']
[algebra R P''] [is_localization S P''] :
(canonical_equiv S P P').trans (canonical_equiv S P' P'') = canonical_equiv S P P'' :=
ring_equiv.ext (canonical_equiv_canonical_equiv S P P' P'')
@[simp]
lemma canonical_equiv_coe_ideal (I : ideal R) :
canonical_equiv S P P' I = I :=
by { ext, simp [is_localization.map_eq] }
omit loc'
@[simp]
lemma canonical_equiv_self : canonical_equiv S P P = ring_equiv.refl _ :=
begin
rw ← canonical_equiv_trans_canonical_equiv S P P,
convert (canonical_equiv S P P).symm_trans_self,
exact (canonical_equiv_symm S P P).symm
end
end semiring
section is_fraction_ring
/-!
### `is_fraction_ring` section
This section concerns fractional ideals in the field of fractions,
i.e. the type `fractional_ideal R⁰ K` where `is_fraction_ring R K`.
-/
variables {K K' : Type*} [field K] [field K']
variables [algebra R K] [is_fraction_ring R K] [algebra R K'] [is_fraction_ring R K']
variables {I J : fractional_ideal R⁰ K} (h : K →ₐ[R] K')
/-- Nonzero fractional ideals contain a nonzero integer. -/
lemma exists_ne_zero_mem_is_integer [nontrivial R] (hI : I ≠ 0) :
∃ x ≠ (0 : R), algebra_map R K x ∈ I :=
begin
obtain ⟨y, y_mem, y_not_mem⟩ := set_like.exists_of_lt
(by simpa only using bot_lt_iff_ne_bot.mpr hI),
have y_ne_zero : y ≠ 0 := by simpa using y_not_mem,
obtain ⟨z, ⟨x, hx⟩⟩ := exists_integer_multiple R⁰ y,
refine ⟨x, _, _⟩,
{ rw [ne.def, ← @is_fraction_ring.to_map_eq_zero_iff R _ K, hx, algebra.smul_def],
exact mul_ne_zero (is_fraction_ring.to_map_ne_zero_of_mem_non_zero_divisors z.2) y_ne_zero },
{ rw hx,
exact smul_mem _ _ y_mem }
end
lemma map_ne_zero [nontrivial R] (hI : I ≠ 0) : I.map h ≠ 0 :=
begin
obtain ⟨x, x_ne_zero, hx⟩ := exists_ne_zero_mem_is_integer hI,
contrapose! x_ne_zero with map_eq_zero,
refine is_fraction_ring.to_map_eq_zero_iff.mp (eq_zero_iff.mp map_eq_zero _ (mem_map.mpr _)),
exact ⟨algebra_map R K x, hx, h.commutes x⟩,
end
@[simp] lemma map_eq_zero_iff [nontrivial R] : I.map h = 0 ↔ I = 0 :=
⟨imp_of_not_imp_not _ _ (map_ne_zero _), λ hI, hI.symm ▸ map_zero h⟩
lemma coe_ideal_injective : function.injective (coe : ideal R → fractional_ideal R⁰ K) :=
coe_ideal_injective' le_rfl
lemma coe_ideal_inj {I J : ideal R} :
(I : fractional_ideal R⁰ K) = (J : fractional_ideal R⁰ K) ↔ I = J :=
coe_ideal_inj' le_rfl
@[simp] lemma coe_ideal_eq_zero {I : ideal R} : (I : fractional_ideal R⁰ K) = 0 ↔ I = ⊥ :=
coe_ideal_eq_zero' le_rfl
lemma coe_ideal_ne_zero {I : ideal R} : (I : fractional_ideal R⁰ K) ≠ 0 ↔ I ≠ ⊥ :=
coe_ideal_ne_zero' le_rfl
@[simp] lemma coe_ideal_eq_one {I : ideal R} : (I : fractional_ideal R⁰ K) = 1 ↔ I = 1 :=
by simpa only [ideal.one_eq_top] using coe_ideal_inj
lemma coe_ideal_ne_one {I : ideal R} : (I : fractional_ideal R⁰ K) ≠ 1 ↔ I ≠ 1 :=
not_iff_not.mpr coe_ideal_eq_one
end is_fraction_ring
section quotient
/-!
### `quotient` section
This section defines the ideal quotient of fractional ideals.
In this section we need that each non-zero `y : R` has an inverse in
the localization, i.e. that the localization is a field. We satisfy this
assumption by taking `S = non_zero_divisors R`, `R`'s localization at which
is a field because `R` is a domain.
-/
open_locale classical
variables {R₁ : Type*} [comm_ring R₁] {K : Type*} [field K]
variables [algebra R₁ K] [frac : is_fraction_ring R₁ K]
instance : nontrivial (fractional_ideal R₁⁰ K) :=
⟨⟨0, 1, λ h,
have this : (1 : K) ∈ (0 : fractional_ideal R₁⁰ K) :=
by { rw ← (algebra_map R₁ K).map_one, simpa only [h] using coe_mem_one R₁⁰ 1 },
one_ne_zero ((mem_zero_iff _).mp this)⟩⟩
lemma ne_zero_of_mul_eq_one (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) : I ≠ 0 :=
λ hI, zero_ne_one' (fractional_ideal R₁⁰ K) (by { convert h, simp [hI], })
variables [is_domain R₁]
include frac
lemma _root_.is_fractional.div_of_nonzero {I J : submodule R₁ K} :
is_fractional R₁⁰ I → is_fractional R₁⁰ J → J ≠ 0 → is_fractional R₁⁰ (I / J)
| ⟨aI, haI, hI⟩ ⟨aJ, haJ, hJ⟩ h := begin
obtain ⟨y, mem_J, not_mem_zero⟩ := set_like.exists_of_lt
(by simpa only using bot_lt_iff_ne_bot.mpr h),
obtain ⟨y', hy'⟩ := hJ y mem_J,
use (aI * y'),
split,
{ apply (non_zero_divisors R₁).mul_mem haI (mem_non_zero_divisors_iff_ne_zero.mpr _),
intro y'_eq_zero,
have : algebra_map R₁ K aJ * y = 0,
{ rw [← algebra.smul_def, ←hy', y'_eq_zero, ring_hom.map_zero] },
have y_zero := (mul_eq_zero.mp this).resolve_left
(mt ((injective_iff_map_eq_zero (algebra_map R₁ K)).1 (is_fraction_ring.injective _ _) _)
(mem_non_zero_divisors_iff_ne_zero.mp haJ)),
apply not_mem_zero,
simpa only using (mem_zero_iff R₁⁰).mpr y_zero, },
intros b hb,
convert hI _ (hb _ (submodule.smul_mem _ aJ mem_J)) using 1,
rw [← hy', mul_comm b, ← algebra.smul_def, mul_smul]
end
lemma fractional_div_of_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) :
is_fractional R₁⁰ (I / J : submodule R₁ K) :=
I.is_fractional.div_of_nonzero J.is_fractional $ λ H, h $
coe_to_submodule_injective $ H.trans coe_zero.symm
noncomputable instance : has_div (fractional_ideal R₁⁰ K) :=
⟨ λ I J, if h : J = 0 then 0 else ⟨I / J, fractional_div_of_nonzero h⟩ ⟩
variables {I J : fractional_ideal R₁⁰ K} [ J ≠ 0 ]
@[simp] lemma div_zero {I : fractional_ideal R₁⁰ K} :
I / 0 = 0 :=
dif_pos rfl
lemma div_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) :
(I / J) = ⟨I / J, fractional_div_of_nonzero h⟩ :=
dif_neg h
@[simp] lemma coe_div {I J : fractional_ideal R₁⁰ K} (hJ : J ≠ 0) :
(↑(I / J) : submodule R₁ K) = ↑I / (↑J : submodule R₁ K) :=
congr_arg _ (dif_neg hJ)
lemma mem_div_iff_of_nonzero {I J : fractional_ideal R₁⁰ K} (h : J ≠ 0) {x} :
x ∈ I / J ↔ ∀ y ∈ J, x * y ∈ I :=
by { rw div_nonzero h, exact submodule.mem_div_iff_forall_mul_mem }
lemma mul_one_div_le_one {I : fractional_ideal R₁⁰ K} : I * (1 / I) ≤ 1 :=
begin
by_cases hI : I = 0,
{ rw [hI, div_zero, mul_zero],
exact zero_le 1 },
{ rw [← coe_le_coe, coe_mul, coe_div hI, coe_one],
apply submodule.mul_one_div_le_one },
end
lemma le_self_mul_one_div {I : fractional_ideal R₁⁰ K} (hI : I ≤ (1 : fractional_ideal R₁⁰ K)) :
I ≤ I * (1 / I) :=
begin
by_cases hI_nz : I = 0,
{ rw [hI_nz, div_zero, mul_zero], exact zero_le 0 },
{ rw [← coe_le_coe, coe_mul, coe_div hI_nz, coe_one],
rw [← coe_le_coe, coe_one] at hI,
exact submodule.le_self_mul_one_div hI },
end
lemma le_div_iff_of_nonzero {I J J' : fractional_ideal R₁⁰ K} (hJ' : J' ≠ 0) :
I ≤ J / J' ↔ ∀ (x ∈ I) (y ∈ J'), x * y ∈ J :=
⟨ λ h x hx, (mem_div_iff_of_nonzero hJ').mp (h hx),
λ h x hx, (mem_div_iff_of_nonzero hJ').mpr (h x hx) ⟩
lemma le_div_iff_mul_le {I J J' : fractional_ideal R₁⁰ K} (hJ' : J' ≠ 0) :
I ≤ J / J' ↔ I * J' ≤ J :=
begin
rw div_nonzero hJ',
convert submodule.le_div_iff_mul_le using 1,
rw [← coe_mul, coe_le_coe]
end
@[simp] lemma div_one {I : fractional_ideal R₁⁰ K} : I / 1 = I :=
begin
rw [div_nonzero (one_ne_zero' (fractional_ideal R₁⁰ K))],
ext,
split; intro h,
{ simpa using mem_div_iff_forall_mul_mem.mp h 1
((algebra_map R₁ K).map_one ▸ coe_mem_one R₁⁰ 1) },
{ apply mem_div_iff_forall_mul_mem.mpr,
rintros y ⟨y', _, rfl⟩,
rw mul_comm,
convert submodule.smul_mem _ y' h,
exact (algebra.smul_def _ _).symm }
end
theorem eq_one_div_of_mul_eq_one_right (I J : fractional_ideal R₁⁰ K) (h : I * J = 1) :
J = 1 / I :=
begin
have hI : I ≠ 0 := ne_zero_of_mul_eq_one I J h,
suffices h' : I * (1 / I) = 1,
{ exact (congr_arg units.inv $
@units.ext _ _ (units.mk_of_mul_eq_one _ _ h) (units.mk_of_mul_eq_one _ _ h') rfl) },
apply le_antisymm,
{ apply mul_le.mpr _,
intros x hx y hy,
rw mul_comm,
exact (mem_div_iff_of_nonzero hI).mp hy x hx },
rw ← h,
apply mul_left_mono I,
apply (le_div_iff_of_nonzero hI).mpr _,
intros y hy x hx,
rw mul_comm,
exact mul_mem_mul hx hy,
end
theorem mul_div_self_cancel_iff {I : fractional_ideal R₁⁰ K} :
I * (1 / I) = 1 ↔ ∃ J, I * J = 1 :=
⟨λ h, ⟨(1 / I), h⟩, λ ⟨J, hJ⟩, by rwa [← eq_one_div_of_mul_eq_one_right I J hJ]⟩
variables {K' : Type*} [field K'] [algebra R₁ K'] [is_fraction_ring R₁ K']
@[simp] lemma map_div (I J : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') :
(I / J).map (h : K →ₐ[R₁] K') = I.map h / J.map h :=
begin
by_cases H : J = 0,
{ rw [H, div_zero, map_zero, div_zero] },
{ apply coe_to_submodule_injective,
simp [div_nonzero H, div_nonzero (map_ne_zero _ H), submodule.map_div] }
end
@[simp] lemma map_one_div (I : fractional_ideal R₁⁰ K) (h : K ≃ₐ[R₁] K') :
(1 / I).map (h : K →ₐ[R₁] K') = 1 / I.map h :=
by rw [map_div, map_one]
end quotient
section field
variables {R₁ K L : Type*} [comm_ring R₁] [field K] [field L]
variables [algebra R₁ K] [is_fraction_ring R₁ K] [algebra K L] [is_fraction_ring K L]
lemma eq_zero_or_one (I : fractional_ideal K⁰ L) : I = 0 ∨ I = 1 :=
begin
rw or_iff_not_imp_left,
intro hI,
simp_rw [@set_like.ext_iff _ _ _ I 1, mem_one_iff],
intro x,
split,
{ intro x_mem,
obtain ⟨n, d, rfl⟩ := is_localization.mk'_surjective K⁰ x,
refine ⟨n / d, _⟩,
rw [map_div₀, is_fraction_ring.mk'_eq_div] },
{ rintro ⟨x, rfl⟩,
obtain ⟨y, y_ne, y_mem⟩ := exists_ne_zero_mem_is_integer hI,
rw [← div_mul_cancel x y_ne, ring_hom.map_mul, ← algebra.smul_def],
exact submodule.smul_mem I _ y_mem }
end
lemma eq_zero_or_one_of_is_field (hF : is_field R₁) (I : fractional_ideal R₁⁰ K) : I = 0 ∨ I = 1 :=
by letI : field R₁ := hF.to_field; exact eq_zero_or_one I
end field
section principal_ideal_ring
variables {R₁ : Type*} [comm_ring R₁] {K : Type*} [field K]
variables [algebra R₁ K] [is_fraction_ring R₁ K]
open_locale classical
variables (R₁)
/-- `fractional_ideal.span_finset R₁ s f` is the fractional ideal of `R₁` generated by `f '' s`. -/
@[simps] def span_finset {ι : Type*} (s : finset ι) (f : ι → K) : fractional_ideal R₁⁰ K :=
⟨submodule.span R₁ (f '' s), begin
obtain ⟨a', ha'⟩ := is_localization.exist_integer_multiples R₁⁰ s f,
refine ⟨a', a'.2, λ x hx, submodule.span_induction hx _ _ _ _⟩,
{ rintro _ ⟨i, hi, rfl⟩, exact ha' i hi },
{ rw smul_zero, exact is_localization.is_integer_zero },
{ intros x y hx hy, rw smul_add, exact is_localization.is_integer_add hx hy },
{ intros c x hx, rw smul_comm, exact is_localization.is_integer_smul hx }
end⟩
variables {R₁}
@[simp] lemma span_finset_eq_zero {ι : Type*} {s : finset ι} {f : ι → K} :
span_finset R₁ s f = 0 ↔ ∀ j ∈ s, f j = 0 :=
by simp only [← coe_to_submodule_inj, span_finset_coe, coe_zero, submodule.span_eq_bot,
set.mem_image, finset.mem_coe, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂]
lemma span_finset_ne_zero {ι : Type*} {s : finset ι} {f : ι → K} :
span_finset R₁ s f ≠ 0 ↔ ∃ j ∈ s, f j ≠ 0 :=
by simp
open submodule.is_principal
include loc
lemma is_fractional_span_singleton (x : P) : is_fractional S (span R {x} : submodule R P) :=
let ⟨a, ha⟩ := exists_integer_multiple S x in
is_fractional_span_iff.mpr ⟨a, a.2, λ x' hx', (set.mem_singleton_iff.mp hx').symm ▸ ha⟩
variables (S)
/-- `span_singleton x` is the fractional ideal generated by `x` if `0 ∉ S` -/
@[irreducible]
def span_singleton (x : P) : fractional_ideal S P :=
⟨span R {x}, is_fractional_span_singleton x⟩
-- local attribute [semireducible] span_singleton
@[simp] lemma coe_span_singleton (x : P) :
(span_singleton S x : submodule R P) = span R {x} :=
by { rw span_singleton, refl }
@[simp] lemma mem_span_singleton {x y : P} :
x ∈ span_singleton S y ↔ ∃ (z : R), z • y = x :=
by { rw span_singleton, exact submodule.mem_span_singleton }
lemma mem_span_singleton_self (x : P) :
x ∈ span_singleton S x :=
(mem_span_singleton S).mpr ⟨1, one_smul _ _⟩
variables {S}
@[simp] lemma span_singleton_le_iff_mem {x : P} {I : fractional_ideal S P} :
span_singleton S x ≤ I ↔ x ∈ I :=
by rw [← coe_le_coe, coe_span_singleton, submodule.span_singleton_le_iff_mem x ↑I, mem_coe]
lemma span_singleton_eq_span_singleton [no_zero_smul_divisors R P] {x y : P} :
span_singleton S x = span_singleton S y ↔ ∃ z : Rˣ, z • x = y :=
by { rw [← submodule.span_singleton_eq_span_singleton, span_singleton, span_singleton],
exact subtype.mk_eq_mk }
lemma eq_span_singleton_of_principal (I : fractional_ideal S P)
[is_principal (I : submodule R P)] :
I = span_singleton S (generator (I : submodule R P)) :=
by { rw span_singleton, exact coe_to_submodule_injective (span_singleton_generator ↑I).symm }
lemma is_principal_iff (I : fractional_ideal S P) :
is_principal (I : submodule R P) ↔ ∃ x, I = span_singleton S x :=
⟨λ h, ⟨@generator _ _ _ _ _ ↑I h, @eq_span_singleton_of_principal _ _ _ _ _ _ _ I h⟩,
λ ⟨x, hx⟩, { principal := ⟨x, trans (congr_arg _ hx) (coe_span_singleton _ x)⟩ } ⟩
@[simp] lemma span_singleton_zero : span_singleton S (0 : P) = 0 :=
by { ext, simp [submodule.mem_span_singleton, eq_comm] }
lemma span_singleton_eq_zero_iff {y : P} : span_singleton S y = 0 ↔ y = 0 :=
⟨λ h, span_eq_bot.mp (by simpa using congr_arg subtype.val h : span R {y} = ⊥) y (mem_singleton y),
λ h, by simp [h] ⟩
lemma span_singleton_ne_zero_iff {y : P} : span_singleton S y ≠ 0 ↔ y ≠ 0 :=
not_congr span_singleton_eq_zero_iff
@[simp] lemma span_singleton_one : span_singleton S (1 : P) = 1 :=
begin
ext,
refine (mem_span_singleton S).trans ((exists_congr _).trans (mem_one_iff S).symm),
intro x',
rw [algebra.smul_def, mul_one]
end
@[simp]
lemma span_singleton_mul_span_singleton (x y : P) :
span_singleton S x * span_singleton S y = span_singleton S (x * y) :=
begin
apply coe_to_submodule_injective,
simp only [coe_mul, coe_span_singleton, span_mul_span, singleton_mul_singleton],
end
@[simp]
lemma span_singleton_pow (x : P) (n : ℕ) : span_singleton S x ^ n = span_singleton S (x ^ n) :=
begin
induction n with n hn,
{ rw [pow_zero, pow_zero, span_singleton_one] },
{ rw [pow_succ, hn, span_singleton_mul_span_singleton, pow_succ] }
end
@[simp]
lemma coe_ideal_span_singleton (x : R) :
(↑(ideal.span {x} : ideal R) : fractional_ideal S P) = span_singleton S (algebra_map R P x) :=
begin
ext y,
refine (mem_coe_ideal S).trans (iff.trans _ (mem_span_singleton S).symm),
split,
{ rintros ⟨y', hy', rfl⟩,
obtain ⟨x', rfl⟩ := submodule.mem_span_singleton.mp hy',
use x',
rw [smul_eq_mul, ring_hom.map_mul, algebra.smul_def] },
{ rintros ⟨y', rfl⟩,
refine ⟨y' * x, submodule.mem_span_singleton.mpr ⟨y', rfl⟩, _⟩,
rw [ring_hom.map_mul, algebra.smul_def] }
end
@[simp]
lemma canonical_equiv_span_singleton {P'} [comm_ring P'] [algebra R P'] [is_localization S P']
(x : P) :
canonical_equiv S P P' (span_singleton S x) =
span_singleton S (is_localization.map P' (ring_hom.id R)
(λ y (hy : y ∈ S), show ring_hom.id R y ∈ S, from hy) x) :=
begin
apply set_like.ext_iff.mpr,
intro y,
split; intro h,
{ rw mem_span_singleton,
obtain ⟨x', hx', rfl⟩ := (mem_canonical_equiv_apply _ _ _).mp h,
obtain ⟨z, rfl⟩ := (mem_span_singleton _).mp hx',
use z,
rw is_localization.map_smul,
refl },
{ rw mem_canonical_equiv_apply,
obtain ⟨z, rfl⟩ := (mem_span_singleton _).mp h,
use z • x,
use (mem_span_singleton _).mpr ⟨z, rfl⟩,
simp [is_localization.map_smul] }
end
lemma mem_singleton_mul {x y : P} {I : fractional_ideal S P} :
y ∈ span_singleton S x * I ↔ ∃ y' ∈ I, y = x * y' :=
begin
split,
{ intro h,
apply fractional_ideal.mul_induction_on h,
{ intros x' hx' y' hy',
obtain ⟨a, ha⟩ := (mem_span_singleton S).mp hx',
use [a • y', submodule.smul_mem I a hy'],
rw [←ha, algebra.mul_smul_comm, algebra.smul_mul_assoc] },
{ rintros _ _ ⟨y, hy, rfl⟩ ⟨y', hy', rfl⟩,
exact ⟨y + y', submodule.add_mem I hy hy', (mul_add _ _ _).symm⟩ } },
{ rintros ⟨y', hy', rfl⟩,
exact mul_mem_mul ((mem_span_singleton S).mpr ⟨1, one_smul _ _⟩) hy' }
end
omit loc
variables (K)
lemma mk'_mul_coe_ideal_eq_coe_ideal {I J : ideal R₁} {x y : R₁} (hy : y ∈ R₁⁰) :
span_singleton R₁⁰ (is_localization.mk' K x ⟨y, hy⟩) * I = (J : fractional_ideal R₁⁰ K) ↔
ideal.span {x} * I = ideal.span {y} * J :=
begin
have : span_singleton R₁⁰ (is_localization.mk' _ (1 : R₁) ⟨y, hy⟩) *
span_singleton R₁⁰ (algebra_map R₁ K y) = 1,
{ rw [span_singleton_mul_span_singleton, mul_comm, ← is_localization.mk'_eq_mul_mk'_one,
is_localization.mk'_self, span_singleton_one] },
let y' : (fractional_ideal R₁⁰ K)ˣ := units.mk_of_mul_eq_one _ _ this,
have coe_y' : ↑y' = span_singleton R₁⁰ (is_localization.mk' K (1 : R₁) ⟨y, hy⟩) := rfl,
refine iff.trans _ (y'.mul_right_inj.trans coe_ideal_inj),
rw [coe_y', coe_ideal_mul, coe_ideal_span_singleton, coe_ideal_mul, coe_ideal_span_singleton,
←mul_assoc, span_singleton_mul_span_singleton, ←mul_assoc, span_singleton_mul_span_singleton,
mul_comm (mk' _ _ _), ← is_localization.mk'_eq_mul_mk'_one,
mul_comm (mk' _ _ _), ← is_localization.mk'_eq_mul_mk'_one,
is_localization.mk'_self, span_singleton_one, one_mul],
end
variables {K}
lemma span_singleton_mul_coe_ideal_eq_coe_ideal {I J : ideal R₁} {z : K} :
span_singleton R₁⁰ z * (I : fractional_ideal R₁⁰ K) = J ↔
ideal.span {((is_localization.sec R₁⁰ z).1 : R₁)} * I =
ideal.span {(is_localization.sec R₁⁰ z).2} * J :=
-- `erw` to deal with the distinction between `y` and `⟨y.1, y.2⟩`
by erw [← mk'_mul_coe_ideal_eq_coe_ideal K (is_localization.sec R₁⁰ z).2.prop,
is_localization.mk'_sec K z]
variables [is_domain R₁]
lemma one_div_span_singleton (x : K) :
1 / span_singleton R₁⁰ x = span_singleton R₁⁰ (x⁻¹) :=
if h : x = 0 then by simp [h] else (eq_one_div_of_mul_eq_one_right _ _ (by simp [h])).symm
@[simp] lemma div_span_singleton (J : fractional_ideal R₁⁰ K) (d : K) :
J / span_singleton R₁⁰ d = span_singleton R₁⁰ (d⁻¹) * J :=
begin
rw ← one_div_span_singleton,
by_cases hd : d = 0,
{ simp only [hd, span_singleton_zero, div_zero, zero_mul] },
have h_spand : span_singleton R₁⁰ d ≠ 0 := mt span_singleton_eq_zero_iff.mp hd,
apply le_antisymm,
{ intros x hx,
rw [← mem_coe, coe_div h_spand, submodule.mem_div_iff_forall_mul_mem] at hx,
specialize hx d (mem_span_singleton_self R₁⁰ d),
have h_xd : x = d⁻¹ * (x * d), { field_simp },
rw [← mem_coe, coe_mul, one_div_span_singleton, h_xd],
exact submodule.mul_mem_mul (mem_span_singleton_self R₁⁰ _) hx },
{ rw [le_div_iff_mul_le h_spand, mul_assoc, mul_left_comm, one_div_span_singleton,
span_singleton_mul_span_singleton, inv_mul_cancel hd, span_singleton_one, mul_one],
exact le_refl J },
end
lemma exists_eq_span_singleton_mul (I : fractional_ideal R₁⁰ K) :
∃ (a : R₁) (aI : ideal R₁), a ≠ 0 ∧ I = span_singleton R₁⁰ (algebra_map R₁ K a)⁻¹ * aI :=
begin
obtain ⟨a_inv, nonzero, ha⟩ := I.is_fractional,
have nonzero := mem_non_zero_divisors_iff_ne_zero.mp nonzero,
have map_a_nonzero : algebra_map R₁ K a_inv ≠ 0 :=
mt is_fraction_ring.to_map_eq_zero_iff.mp nonzero,
refine ⟨a_inv,
submodule.comap (algebra.linear_map R₁ K)
↑(span_singleton R₁⁰ (algebra_map R₁ K a_inv) * I),
nonzero,
ext (λ x, iff.trans ⟨_, _⟩ mem_singleton_mul.symm)⟩,
{ intro hx,
obtain ⟨x', hx'⟩ := ha x hx,
rw algebra.smul_def at hx',
refine ⟨algebra_map R₁ K x', (mem_coe_ideal _).mpr ⟨x', mem_singleton_mul.mpr _, rfl⟩, _⟩,
{ exact ⟨x, hx, hx'⟩ },
{ rw [hx', ← mul_assoc, inv_mul_cancel map_a_nonzero, one_mul] } },
{ rintros ⟨y, hy, rfl⟩,
obtain ⟨x', hx', rfl⟩ := (mem_coe_ideal _).mp hy,
obtain ⟨y', hy', hx'⟩ := mem_singleton_mul.mp hx',
rw algebra.linear_map_apply at hx',
rwa [hx', ←mul_assoc, inv_mul_cancel map_a_nonzero, one_mul] }
end
instance is_principal {R} [comm_ring R] [is_domain R] [is_principal_ideal_ring R]
[algebra R K] [is_fraction_ring R K]
(I : fractional_ideal R⁰ K) : (I : submodule R K).is_principal :=
begin
obtain ⟨a, aI, -, ha⟩ := exists_eq_span_singleton_mul I,
use (algebra_map R K a)⁻¹ * algebra_map R K (generator aI),
suffices : I = span_singleton R⁰ ((algebra_map R K a)⁻¹ * algebra_map R K (generator aI)),
{ rw span_singleton at this, exact congr_arg subtype.val this },
conv_lhs { rw [ha, ←span_singleton_generator aI] },
rw [ideal.submodule_span_eq, coe_ideal_span_singleton (generator aI),
span_singleton_mul_span_singleton]
end
include loc
lemma le_span_singleton_mul_iff {x : P} {I J : fractional_ideal S P} :
I ≤ span_singleton S x * J ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI :=
show (∀ {zI} (hzI : zI ∈ I), zI ∈ span_singleton _ x * J) ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI,
by simp only [mem_singleton_mul, eq_comm]
lemma span_singleton_mul_le_iff {x : P} {I J : fractional_ideal S P} :
span_singleton _ x * I ≤ J ↔ ∀ z ∈ I, x * z ∈ J :=
begin
simp only [mul_le, mem_singleton_mul, mem_span_singleton],
split,
{ intros h zI hzI,
exact h x ⟨1, one_smul _ _⟩ zI hzI },
{ rintros h _ ⟨z, rfl⟩ zI hzI,
rw [algebra.smul_mul_assoc],
exact submodule.smul_mem J.1 _ (h zI hzI) },
end
lemma eq_span_singleton_mul {x : P} {I J : fractional_ideal S P} :
I = span_singleton _ x * J ↔ (∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI) ∧ ∀ z ∈ J, x * z ∈ I :=
by simp only [le_antisymm_iff, le_span_singleton_mul_iff, span_singleton_mul_le_iff]
end principal_ideal_ring
variables {R₁ : Type*} [comm_ring R₁]
variables {K : Type*} [field K] [algebra R₁ K] [frac : is_fraction_ring R₁ K]
local attribute [instance] classical.prop_decidable
lemma is_noetherian_zero : is_noetherian R₁ (0 : fractional_ideal R₁⁰ K) :=
is_noetherian_submodule.mpr (λ I (hI : I ≤ (0 : fractional_ideal R₁⁰ K)),
by { rw coe_zero at hI, rw le_bot_iff.mp hI, exact fg_bot })
lemma is_noetherian_iff {I : fractional_ideal R₁⁰ K} :
is_noetherian R₁ I ↔ ∀ J ≤ I, (J : submodule R₁ K).fg :=
is_noetherian_submodule.trans ⟨λ h J hJ, h _ hJ, λ h J hJ, h ⟨J, is_fractional_of_le hJ⟩ hJ⟩
lemma is_noetherian_coe_ideal [_root_.is_noetherian_ring R₁] (I : ideal R₁) :
is_noetherian R₁ (I : fractional_ideal R₁⁰ K) :=
begin
rw is_noetherian_iff,
intros J hJ,
obtain ⟨J, rfl⟩ := le_one_iff_exists_coe_ideal.mp (le_trans hJ coe_ideal_le_one),
exact (is_noetherian.noetherian J).map _,
end
include frac
variables [is_domain R₁]
lemma is_noetherian_span_singleton_inv_to_map_mul (x : R₁) {I : fractional_ideal R₁⁰ K}
(hI : is_noetherian R₁ I) :
is_noetherian R₁ (span_singleton R₁⁰ (algebra_map R₁ K x)⁻¹ * I : fractional_ideal R₁⁰ K) :=
begin
by_cases hx : x = 0,
{ rw [hx, ring_hom.map_zero, _root_.inv_zero, span_singleton_zero, zero_mul],
exact is_noetherian_zero },
have h_gx : algebra_map R₁ K x ≠ 0,
from mt ((injective_iff_map_eq_zero (algebra_map R₁ K)).mp
(is_fraction_ring.injective _ _) x) hx,
have h_spanx : span_singleton R₁⁰ (algebra_map R₁ K x) ≠ 0,
from span_singleton_ne_zero_iff.mpr h_gx,
rw is_noetherian_iff at ⊢ hI,
intros J hJ,
rw [← div_span_singleton, le_div_iff_mul_le h_spanx] at hJ,
obtain ⟨s, hs⟩ := hI _ hJ,
use s * {(algebra_map R₁ K x)⁻¹},
rw [finset.coe_mul, finset.coe_singleton, ← span_mul_span, hs, ← coe_span_singleton R₁⁰,
← coe_mul, mul_assoc, span_singleton_mul_span_singleton, mul_inv_cancel h_gx,
span_singleton_one, mul_one],
end
/-- Every fractional ideal of a noetherian integral domain is noetherian. -/
theorem is_noetherian [_root_.is_noetherian_ring R₁] (I : fractional_ideal R₁⁰ K) :
is_noetherian R₁ I :=
begin
obtain ⟨d, J, h_nzd, rfl⟩ := exists_eq_span_singleton_mul I,
apply is_noetherian_span_singleton_inv_to_map_mul,
apply is_noetherian_coe_ideal
end
section adjoin
include loc
omit frac
variables {R P} (S) (x : P) (hx : is_integral R x)
/-- `A[x]` is a fractional ideal for every integral `x`. -/
lemma is_fractional_adjoin_integral :
is_fractional S (algebra.adjoin R ({x} : set P)).to_submodule :=
is_fractional_of_fg (fg_adjoin_singleton_of_integral x hx)
/-- `fractional_ideal.adjoin_integral (S : submonoid R) x hx` is `R[x]` as a fractional ideal,
where `hx` is a proof that `x : P` is integral over `R`. -/
@[simps]
def adjoin_integral : fractional_ideal S P :=
⟨_, is_fractional_adjoin_integral S x hx⟩
lemma mem_adjoin_integral_self :
x ∈ adjoin_integral S x hx :=
algebra.subset_adjoin (set.mem_singleton x)
end adjoin
end fractional_ideal
|
f641b3a91f640ac08485a1af8361d2ffb2fee63f | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/combinatorics/quiver.lean | a6df86507b3cf19ae9c27b896e9ebc6e7e8ce12a | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,860 | lean | /-
Copyright (c) 2021 David Wärn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: David Wärn
-/
import data.equiv.basic
import order.well_founded
import data.nat.basic
import data.opposite
/-!
# Quivers
This module defines quivers. A quiver on a type `V` of vertices assigns to every
pair `a b : V` of vertices a type `a ⟶ b` of arrows from `a` to `b`. This
is a very permissive notion of directed graph.
## Implementation notes
Currently `quiver` is defined with `arrow : V → V → Sort v`.
This is different from the category theory setup,
where we insist that morphisms live in some `Type`.
There's some balance here: it's nice to allow `Prop` to ensure there are no multiple arrows,
but it is also results in error-prone universe signatures when constraints require a `Type`.
-/
open opposite
-- We use the same universe order as in category theory.
-- See note [category_theory universes]
universes v v₁ v₂ u u₁ u₂
/--
A quiver `G` on a type `V` of vertices assigns to every pair `a b : V` of vertices
a type `a ⟶ b` of arrows from `a` to `b`.
For graphs with no repeated edges, one can use `quiver.{0} V`, which ensures
`a ⟶ b : Prop`. For multigraphs, one can use `quiver.{v+1} V`, which ensures
`a ⟶ b : Type v`.
Because `category` will later extend this class, we call the field `hom`.
Except when constructing instances, you should rarely see this, and use the `⟶` notation instead.
-/
class quiver (V : Type u) :=
(hom : V → V → Sort v)
infixr ` ⟶ `:10 := quiver.hom -- type as \h
/--
A morphism of quivers. As we will later have categorical functors extend this structure,
we call it a `prefunctor`.
-/
structure prefunctor (V : Type u₁) [quiver.{v₁} V] (W : Type u₂) [quiver.{v₂} W] :=
(obj [] : V → W)
(map : Π {X Y : V}, (X ⟶ Y) → (obj X ⟶ obj Y))
namespace prefunctor
/--
The identity morphism between quivers.
-/
@[simps]
def id (V : Type*) [quiver V] : prefunctor V V :=
{ obj := id,
map := λ X Y f, f, }
instance (V : Type*) [quiver V] : inhabited (prefunctor V V) := ⟨id V⟩
/--
Composition of morphisms between quivers.
-/
@[simps]
def comp {U : Type*} [quiver U] {V : Type*} [quiver V] {W : Type*} [quiver W]
(F : prefunctor U V) (G : prefunctor V W) : prefunctor U W :=
{ obj := λ X, G.obj (F.obj X),
map := λ X Y f, G.map (F.map f), }
end prefunctor
/-- A wide subquiver `H` of `G` picks out a set `H a b` of arrows from `a` to `b`
for every pair of vertices `a b`.
NB: this does not work for `Prop`-valued quivers. It requires `G : quiver.{v+1} V`. -/
def wide_subquiver (V) [quiver.{v+1} V] :=
Π a b : V, set (a ⟶ b)
/-- A type synonym for `V`, when thought of as a quiver having only the arrows from
some `wide_subquiver`. -/
@[nolint unused_arguments has_inhabited_instance]
def wide_subquiver.to_Type (V) [quiver V] (H : wide_subquiver V) : Type u := V
instance wide_subquiver_has_coe_to_sort {V} [quiver V] :
has_coe_to_sort (wide_subquiver V) (Type u) :=
{ coe := λ H, wide_subquiver.to_Type V H }
/-- A wide subquiver viewed as a quiver on its own. -/
instance wide_subquiver.quiver {V} [quiver V] (H : wide_subquiver V) : quiver H :=
⟨λ a b, H a b⟩
namespace quiver
/-- A type synonym for a quiver with no arrows. -/
@[nolint has_inhabited_instance]
def empty (V) : Type u := V
instance empty_quiver (V : Type u) : quiver.{u} (empty V) := ⟨λ a b, pempty⟩
@[simp] lemma empty_arrow {V : Type u} (a b : empty V) : (a ⟶ b) = pempty := rfl
instance {V} [quiver V] : has_bot (wide_subquiver V) := ⟨λ a b, ∅⟩
instance {V} [quiver V] : has_top (wide_subquiver V) := ⟨λ a b, set.univ⟩
instance {V} [quiver V] : inhabited (wide_subquiver V) := ⟨⊤⟩
/-- `Vᵒᵖ` reverses the direction of all arrows of `V`. -/
instance opposite {V} [quiver V] : quiver Vᵒᵖ :=
⟨λ a b, (unop b) ⟶ (unop a)⟩
/--
The opposite of an arrow in `V`.
-/
def hom.op {V} [quiver V] {X Y : V} (f : X ⟶ Y) : op Y ⟶ op X := f
/--
Given an arrow in `Vᵒᵖ`, we can take the "unopposite" back in `V`.
-/
def hom.unop {V} [quiver V] {X Y : Vᵒᵖ} (f : X ⟶ Y) : unop Y ⟶ unop X := f
attribute [irreducible] quiver.opposite
/-- A type synonym for the symmetrized quiver (with an arrow both ways for each original arrow).
NB: this does not work for `Prop`-valued quivers. It requires `[quiver.{v+1} V]`. -/
@[nolint has_inhabited_instance]
def symmetrify (V) : Type u := V
instance symmetrify_quiver (V : Type u) [quiver V] : quiver (symmetrify V) :=
⟨λ a b : V, (a ⟶ b) ⊕ (b ⟶ a)⟩
/-- `total V` is the type of _all_ arrows of `V`. -/
-- TODO Unify with `category_theory.arrow`? (The fields have been named to match.)
@[ext, nolint has_inhabited_instance]
structure total (V : Type u) [quiver.{v} V] : Sort (max (u+1) v) :=
(left : V)
(right : V)
(hom : left ⟶ right)
/-- A wide subquiver `H` of `G.symmetrify` determines a wide subquiver of `G`, containing an
an arrow `e` if either `e` or its reversal is in `H`. -/
-- Without the explicit universe level in `quiver.{v+1}` Lean comes up with
-- `quiver.{max u_2 u_3 + 1}`. This causes problems elsewhere, so we write `quiver.{v+1}`.
def wide_subquiver_symmetrify {V} [quiver.{v+1} V] :
wide_subquiver (symmetrify V) → wide_subquiver V :=
λ H a b, { e | sum.inl e ∈ H a b ∨ sum.inr e ∈ H b a }
/-- A wide subquiver of `G` can equivalently be viewed as a total set of arrows. -/
def wide_subquiver_equiv_set_total {V} [quiver V] :
wide_subquiver V ≃ set (total V) :=
{ to_fun := λ H, { e | e.hom ∈ H e.left e.right },
inv_fun := λ S a b, { e | total.mk a b e ∈ S },
left_inv := λ H, rfl,
right_inv := by { intro S, ext, cases x, refl } }
/-- `G.path a b` is the type of paths from `a` to `b` through the arrows of `G`. -/
inductive path {V : Type u} [quiver.{v} V] (a : V) : V → Sort (max (u+1) v)
| nil : path a
| cons : Π {b c : V}, path b → (b ⟶ c) → path c
/-- An arrow viewed as a path of length one. -/
def hom.to_path {V} [quiver V] {a b : V} (e : a ⟶ b) : path a b :=
path.nil.cons e
namespace path
variables {V : Type u} [quiver V]
/-- The length of a path is the number of arrows it uses. -/
def length {a : V} : Π {b : V}, path a b → ℕ
| _ path.nil := 0
| _ (path.cons p _) := p.length + 1
@[simp] lemma length_nil {a : V} :
(path.nil : path a a).length = 0 := rfl
@[simp] lemma length_cons (a b c : V) (p : path a b)
(e : b ⟶ c) : (p.cons e).length = p.length + 1 := rfl
/-- Composition of paths. -/
def comp {a b : V} : Π {c}, path a b → path b c → path a c
| _ p (path.nil) := p
| _ p (path.cons q e) := (p.comp q).cons e
@[simp] lemma comp_cons {a b c d : V} (p : path a b) (q : path b c) (e : c ⟶ d) :
p.comp (q.cons e) = (p.comp q).cons e := rfl
@[simp] lemma comp_nil {a b : V} (p : path a b) : p.comp path.nil = p := rfl
@[simp] lemma nil_comp {a : V} : ∀ {b} (p : path a b), path.nil.comp p = p
| a path.nil := rfl
| b (path.cons p e) := by rw [comp_cons, nil_comp]
@[simp] lemma comp_assoc {a b c : V} : ∀ {d}
(p : path a b) (q : path b c) (r : path c d),
(p.comp q).comp r = p.comp (q.comp r)
| c p q path.nil := rfl
| d p q (path.cons r e) := by rw [comp_cons, comp_cons, comp_cons, comp_assoc]
end path
end quiver
namespace prefunctor
open quiver
variables {V : Type u₁} [quiver.{v₁} V] {W : Type u₂} [quiver.{v₂} W] (F : prefunctor V W)
/-- The image of a path under a prefunctor. -/
def map_path {a : V} :
Π {b : V}, path a b → path (F.obj a) (F.obj b)
| _ path.nil := path.nil
| _ (path.cons p e) := path.cons (map_path p) (F.map e)
@[simp] lemma map_path_nil (a : V) : F.map_path (path.nil : path a a) = path.nil := rfl
@[simp] lemma map_path_cons {a b c : V} (p : path a b) (e : b ⟶ c) :
F.map_path (path.cons p e) = path.cons (F.map_path p) (F.map e) := rfl
@[simp] lemma map_path_comp {a b : V} (p : path a b) :
∀ {c : V} (q : path b c), F.map_path (p.comp q) = (F.map_path p).comp (F.map_path q)
| _ path.nil := rfl
| _ (path.cons p e) := begin dsimp, rw [map_path_comp], end
end prefunctor
namespace quiver
/-- A quiver is an arborescence when there is a unique path from the default vertex
to every other vertex. -/
class arborescence (V : Type u) [quiver.{v} V] : Type (max u v) :=
(root : V)
(unique_path : Π (b : V), unique (path root b))
/-- The root of an arborescence. -/
def root (V : Type u) [quiver V] [arborescence V] : V :=
arborescence.root
instance {V : Type u} [quiver V] [arborescence V] (b : V) : unique (path (root V) b) :=
arborescence.unique_path b
/-- An `L`-labelling of a quiver assigns to every arrow an element of `L`. -/
def labelling (V : Type u) [quiver V] (L : Sort*) := Π ⦃a b : V⦄, (a ⟶ b) → L
instance {V : Type u} [quiver V] (L) [inhabited L] : inhabited (labelling V L) :=
⟨λ a b e, default L⟩
/-- To show that `[quiver V]` is an arborescence with root `r : V`, it suffices to
- provide a height function `V → ℕ` such that every arrow goes from a
lower vertex to a higher vertex,
- show that every vertex has at most one arrow to it, and
- show that every vertex other than `r` has an arrow to it. -/
noncomputable def arborescence_mk {V : Type u} [quiver V] (r : V)
(height : V → ℕ)
(height_lt : ∀ ⦃a b⦄, (a ⟶ b) → height a < height b)
(unique_arrow : ∀ ⦃a b c : V⦄ (e : a ⟶ c) (f : b ⟶ c), a = b ∧ e == f)
(root_or_arrow : ∀ b, b = r ∨ ∃ a, nonempty (a ⟶ b)) : arborescence V :=
{ root := r,
unique_path := λ b, ⟨classical.inhabited_of_nonempty
begin
rcases (show ∃ n, height b < n, from ⟨_, lt_add_one _⟩) with ⟨n, hn⟩,
induction n with n ih generalizing b,
{ exact false.elim (nat.not_lt_zero _ hn) },
rcases root_or_arrow b with ⟨⟨⟩⟩ | ⟨a, ⟨e⟩⟩,
{ exact ⟨path.nil⟩ },
{ rcases ih a (lt_of_lt_of_le (height_lt e) (nat.lt_succ_iff.mp hn)) with ⟨p⟩,
exact ⟨p.cons e⟩ }
end,
begin
have height_le : ∀ {a b}, path a b → height a ≤ height b,
{ intros a b p, induction p with b c p e ih, refl,
exact le_of_lt (lt_of_le_of_lt ih (height_lt e)) },
suffices : ∀ p q : path r b, p = q,
{ intro p, apply this },
intros p q, induction p with a c p e ih; cases q with b _ q f,
{ refl },
{ exact false.elim (lt_irrefl _ (lt_of_le_of_lt (height_le q) (height_lt f))) },
{ exact false.elim (lt_irrefl _ (lt_of_le_of_lt (height_le p) (height_lt e))) },
{ rcases unique_arrow e f with ⟨⟨⟩, ⟨⟩⟩, rw ih },
end ⟩ }
/-- `rooted_connected r` means that there is a path from `r` to any other vertex. -/
class rooted_connected {V : Type u} [quiver V] (r : V) : Prop :=
(nonempty_path : ∀ b : V, nonempty (path r b))
attribute [instance] rooted_connected.nonempty_path
section geodesic_subtree
variables {V : Type u} [quiver.{v+1} V] (r : V) [rooted_connected r]
/-- A path from `r` of minimal length. -/
noncomputable def shortest_path (b : V) : path r b :=
well_founded.min (measure_wf path.length) set.univ set.univ_nonempty
/-- The length of a path is at least the length of the shortest path -/
lemma shortest_path_spec {a : V} (p : path r a) :
(shortest_path r a).length ≤ p.length :=
not_lt.mp (well_founded.not_lt_min (measure_wf _) set.univ _ trivial)
/-- A subquiver which by construction is an arborescence. -/
def geodesic_subtree : wide_subquiver V :=
λ a b, { e | ∃ p : path r a, shortest_path r b = p.cons e }
noncomputable instance geodesic_arborescence : arborescence (geodesic_subtree r) :=
arborescence_mk r (λ a, (shortest_path r a).length)
(by { rintros a b ⟨e, p, h⟩,
rw [h, path.length_cons, nat.lt_succ_iff], apply shortest_path_spec })
(by { rintros a b c ⟨e, p, h⟩ ⟨f, q, j⟩, cases h.symm.trans j, split; refl })
(by { intro b, have : ∃ p, shortest_path r b = p := ⟨_, rfl⟩,
rcases this with ⟨p, hp⟩, cases p with a _ p e,
{ exact or.inl rfl }, { exact or.inr ⟨a, ⟨⟨e, p, hp⟩⟩⟩ } })
end geodesic_subtree
variables (V : Type u) [quiver.{v+1} V]
/-- A quiver `has_reverse` if we can reverse an arrow `p` from `a` to `b` to get an arrow
`p.reverse` from `b` to `a`.-/
class has_reverse :=
(reverse' : Π {a b : V}, (a ⟶ b) → (b ⟶ a))
instance : has_reverse (symmetrify V) := ⟨λ a b e, e.swap⟩
variables {V} [has_reverse V]
/-- Reverse the direction of an arrow. -/
def reverse {a b : V} : (a ⟶ b) → (b ⟶ a) := has_reverse.reverse'
/-- Reverse the direction of a path. -/
def path.reverse {a : V} : Π {b}, path a b → path b a
| a path.nil := path.nil
| b (path.cons p e) := (reverse e).to_path.comp p.reverse
variables (V)
/-- Two vertices are related in the zigzag setoid if there is a
zigzag of arrows from one to the other. -/
def zigzag_setoid : setoid V :=
⟨λ a b, nonempty (path (a : symmetrify V) (b : symmetrify V)),
λ a, ⟨path.nil⟩,
λ a b ⟨p⟩, ⟨p.reverse⟩,
λ a b c ⟨p⟩ ⟨q⟩, ⟨p.comp q⟩⟩
/-- The type of weakly connected components of a directed graph. Two vertices are
in the same weakly connected component if there is a zigzag of arrows from one
to the other. -/
def weakly_connected_component : Type* := quotient (zigzag_setoid V)
namespace weakly_connected_component
variable {V}
/-- The weakly connected component corresponding to a vertex. -/
protected def mk : V → weakly_connected_component V := quotient.mk'
instance : has_coe_t V (weakly_connected_component V) := ⟨weakly_connected_component.mk⟩
instance [inhabited V] : inhabited (weakly_connected_component V) := ⟨↑(default V)⟩
protected lemma eq (a b : V) :
(a : weakly_connected_component V) = b ↔ nonempty (path (a : symmetrify V) (b : symmetrify V)) :=
quotient.eq'
end weakly_connected_component
end quiver
|
0519d28ffc637427caa3b641d4a11c39982e563c | fbf512ee44de430e3d3c5869751ad95325c938d7 | /abst.lean | 822a6f1df2150af150c587b038ddae6d713e27a4 | [] | no_license | skbaek/clausify | 4858c9005fb86a5e410bfcaa77524f82d955f655 | d09b071bdcce7577c3fffacd0893b776285b1590 | refs/heads/master | 1,588,360,590,818 | 1,553,553,880,000 | 1,553,553,880,000 | 177,644,542 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,382 | lean | import .rev .logic .tactic
open tactic expr
meta def arity_of : expr → nat
| `(%%x → %%y) := arity_of y + 1
| _ := 0
meta def get_symb_aux (dx x : expr) :
tactic (expr × expr) :=
do tx ← infer_type x,
if (is_pred_type dx tx || is_func_type dx tx)
then return (x,tx)
else failed
meta def get_symb (dx : expr) :
expr → tactic (expr × expr)
| `(true) := failed
| `(false) := failed
| `(¬ %%px) := get_symb px
| `(%%px ∨ %%qx) := get_symb px <|> get_symb qx
| `(%%px ∧ %%qx) := get_symb px <|> get_symb qx
| (pi _ _ _ px) := get_symb px
| `(Exists %%(expr.lam _ _ _ px)) := get_symb px
| `(Exists %%prx) := get_symb prx
| x@(app x1 x2) :=
get_symb x1
<|> get_symb x2
<|> get_symb_aux dx x
| x := get_symb_aux dx x
meta def subst_symb (x : expr) : nat → expr → expr
| k y@(app y1 y2) :=
if x = y
then var k
else app (subst_symb k y1) (subst_symb k y2)
| k (lam n b tx y) := lam n b tx (subst_symb (k+1) y)
| k (pi n b tx y) := pi n b tx (subst_symb (k+1) y)
| k y := if x = y then var k else y
meta def abst_symb (dx : expr) : tactic unit :=
do gx ← target,
(x,tx) ← get_symb dx gx,
n ← get_unused_name,
to_expr ``(imp_of_imp %%(pi n binder_info.default tx (subst_symb x 0 gx))) >>= apply,
intro_fresh >>= apply,
skip
meta def abst (dx : expr) : tactic unit :=
repeat (abst_symb dx) |
52f76a19277d7d58ea8dc11d2466755d3963a83b | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/set/pointwise/finite.lean | ba1fc40875cfda9e06dc3215ba762523b45d3ad1 | [
"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,948 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Floris van Doorn
-/
import data.set.finite
import data.set.pointwise.smul
/-!
# Finiteness lemmas for pointwise operations on sets
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
open_locale pointwise
variables {F α β γ : Type*}
namespace set
section has_one
variables [has_one α]
@[simp, to_additive] lemma finite_one : (1 : set α).finite := finite_singleton _
end has_one
section has_involutive_inv
variables [has_involutive_inv α] {s : set α}
@[to_additive] lemma finite.inv (hs : s.finite) : s⁻¹.finite :=
hs.preimage $ inv_injective.inj_on _
end has_involutive_inv
section has_mul
variables [has_mul α] {s t : set α}
@[to_additive] lemma finite.mul : s.finite → t.finite → (s * t).finite := finite.image2 _
/-- Multiplication preserves finiteness. -/
@[to_additive "Addition preserves finiteness."]
def fintype_mul [decidable_eq α] (s t : set α) [fintype s] [fintype t] : fintype (s * t : set α) :=
set.fintype_image2 _ _ _
end has_mul
section monoid
variables [monoid α] {s t : set α}
@[to_additive]
instance decidable_mem_mul [fintype α] [decidable_eq α] [decidable_pred (∈ s)]
[decidable_pred (∈ t)] :
decidable_pred (∈ s * t) :=
λ _, decidable_of_iff _ mem_mul.symm
@[to_additive]
instance decidable_mem_pow [fintype α] [decidable_eq α] [decidable_pred (∈ s)] (n : ℕ) :
decidable_pred (∈ (s ^ n)) :=
begin
induction n with n ih,
{ simp_rw [pow_zero, mem_one], apply_instance },
{ letI := ih, rw pow_succ, apply_instance }
end
end monoid
section has_smul
variables [has_smul α β] {s : set α} {t : set β}
@[to_additive] lemma finite.smul : s.finite → t.finite → (s • t).finite := finite.image2 _
end has_smul
section has_smul_set
variables [has_smul α β] {s : set β} {a : α}
@[to_additive] lemma finite.smul_set : s.finite → (a • s).finite := finite.image _
@[to_additive] lemma infinite.of_smul_set : (a • s).infinite → s.infinite := infinite.of_image _
end has_smul_set
section vsub
variables [has_vsub α β] {s t : set β}
include α
lemma finite.vsub (hs : s.finite) (ht : t.finite) : set.finite (s -ᵥ t) := hs.image2 _ ht
end vsub
section cancel
variables [has_mul α] [is_left_cancel_mul α] [is_right_cancel_mul α] {s t : set α}
@[to_additive] lemma infinite_mul :
(s * t).infinite ↔ s.infinite ∧ t.nonempty ∨ t.infinite ∧ s.nonempty :=
infinite_image2 (λ _ _, (mul_left_injective _).inj_on _) (λ _ _, (mul_right_injective _).inj_on _)
end cancel
section group
variables [group α] [mul_action α β] {a : α} {s : set β}
@[simp, to_additive] lemma finite_smul_set : (a • s).finite ↔ s.finite :=
finite_image_iff $ (mul_action.injective _).inj_on _
@[simp, to_additive] lemma infinite_smul_set : (a • s).infinite ↔ s.infinite :=
infinite_image_iff $ (mul_action.injective _).inj_on _
alias finite_smul_set ↔ finite.of_smul_set _
alias infinite_smul_set ↔ _ infinite.smul_set
attribute [to_additive] finite.of_smul_set infinite.smul_set
end group
end set
open set
namespace group
variables {G : Type*} [group G] [fintype G] (S : set G)
@[to_additive]
lemma card_pow_eq_card_pow_card_univ [∀ (k : ℕ), decidable_pred (∈ (S ^ k))] :
∀ k, fintype.card G ≤ k → fintype.card ↥(S ^ k) = fintype.card ↥(S ^ (fintype.card G)) :=
begin
have hG : 0 < fintype.card G := fintype.card_pos_iff.mpr ⟨1⟩,
by_cases hS : S = ∅,
{ refine λ k hk, fintype.card_congr _,
rw [hS, empty_pow (ne_of_gt (lt_of_lt_of_le hG hk)), empty_pow (ne_of_gt hG)] },
obtain ⟨a, ha⟩ := set.nonempty_iff_ne_empty.2 hS,
classical!,
have key : ∀ a (s t : set G), (∀ b : G, b ∈ s → a * b ∈ t) → fintype.card s ≤ fintype.card t,
{ refine λ a s t h, fintype.card_le_of_injective (λ ⟨b, hb⟩, ⟨a * b, h b hb⟩) _,
rintro ⟨b, hb⟩ ⟨c, hc⟩ hbc,
exact subtype.ext (mul_left_cancel (subtype.ext_iff.mp hbc)) },
have mono : monotone (λ n, fintype.card ↥(S ^ n) : ℕ → ℕ) :=
monotone_nat_of_le_succ (λ n, key a _ _ (λ b hb, set.mul_mem_mul ha hb)),
convert card_pow_eq_card_pow_card_univ_aux mono (λ n, set_fintype_card_le_univ (S ^ n))
(λ n h, le_antisymm (mono (n + 1).le_succ) (key a⁻¹ _ _ _)),
{ simp only [finset.filter_congr_decidable, fintype.card_of_finset] },
replace h : {a} * S ^ n = S ^ (n + 1),
{ refine set.eq_of_subset_of_card_le _ (le_trans (ge_of_eq h) _),
{ exact mul_subset_mul (set.singleton_subset_iff.mpr ha) set.subset.rfl },
{ convert key a (S ^ n) ({a} * S ^ n) (λ b hb, set.mul_mem_mul (set.mem_singleton a) hb) } },
rw [pow_succ', ←h, mul_assoc, ←pow_succ', h],
rintro _ ⟨b, c, hb, hc, rfl⟩,
rwa [set.mem_singleton_iff.mp hb, inv_mul_cancel_left],
end
end group
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.