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
0cfffafdaf49446fbb6bdff4fa73838134f92dbc
2d787ac9a5ab28a7d164cd13cee8a3cdc1e4680a
/src/my_exercises/08_limits_negation.lean
fbd0a7832e9bdf2483cc89196b46e7e7970dfd6d
[ "Apache-2.0" ]
permissive
paulzfm/lean3-tutorials
8c113ec2c9494d401c8a877f094db93e5a631fd6
f41baf1c62c251976ec8dd9ccae9e85ec4c4d336
refs/heads/master
1,679,267,488,532
1,614,782,461,000
1,614,782,461,000
344,157,026
0
0
null
null
null
null
UTF-8
Lean
false
false
4,914
lean
import tuto_lib section /- The first part of this file makes sure you can negate quantified statements in your head without the help of `push_neg`. You need to complete the statement and then use the `check_me` tactic to check your answer. This tactic exists only for those exercises, it mostly calls `push_neg` and then cleans up a bit. def seq_limit (u : ℕ → ℝ) (l : ℝ) : Prop := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε -/ -- In this section, u denotes a sequence of real numbers -- f is a function from ℝ to ℝ -- x₀ and l are real numbers variables (u : ℕ → ℝ) (f : ℝ → ℝ) (x₀ l : ℝ) /- Negation of "u tends to l" -/ -- 0062 example : ¬ (∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε) ↔ ∃ ε > 0, ∀ N, ∃ n ≥ N, |u n - l| > ε := begin check_me, end /- Negation of "f is continuous at x₀" -/ -- 0063 example : ¬ (∀ ε > 0, ∃ δ > 0, ∀ x, |x - x₀| ≤ δ → |f x - f x₀| ≤ ε) ↔ ∃ ε > 0, ∀ δ > 0, ∃ x, |x - x₀| ≤ δ ∧ |f x - f x₀| > ε := begin check_me, end /- In the next exercise, we need to keep in mind that `∀ x x', ...` is the abbreviation of `∀ x, ∀ x', ... `. Also, `∃ x x', ...` is the abbreviation of `∃ x, ∃ x', ...`. -/ /- Negation of "f is uniformly continuous on ℝ" -/ -- 0064 example : ¬ (∀ ε > 0, ∃ δ > 0, ∀ x x', |x' - x| ≤ δ → |f x' - f x| ≤ ε) ↔ ∃ ε > 0, ∀ δ > 0, ∃ x x', |x' - x| ≤ δ ∧ |f x' - f x| > ε := begin check_me, end /- Negation of "f is sequentially continuous at x₀" -/ -- 0065 example : ¬ (∀ u : ℕ → ℝ, (∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - x₀| ≤ ε) → (∀ ε > 0, ∃ N, ∀ n ≥ N, |(f ∘ u) n - f x₀| ≤ ε)) ↔ (∃ u : ℕ → ℝ, (∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - x₀| ≤ ε) ∧ (∃ ε > 0, ∀ N, ∃ n ≥ N, |(f ∘ u) n - f x₀| > ε)) := begin check_me, end end /- We now turn to elementary applications of negations to limits of sequences. Remember that `linarith` can find easy numerical contradictions. Also recall the following lemmas: abs_le (x y : ℝ) : |x| ≤ y ↔ -y ≤ x ∧ x ≤ y ge_max_iff (p q r) : r ≥ max p q ↔ r ≥ p ∧ r ≥ q le_max_left p q : p ≤ max p q le_max_right p q : q ≤ max p q /-- The sequence `u` tends to `+∞`. -/ def tendsto_infinity (u : ℕ → ℝ) := ∀ A, ∃ N, ∀ n ≥ N, u n ≥ A -/ -- 0066 example {u : ℕ → ℝ} : tendsto_infinity u → ∀ l, ¬ seq_limit u l := begin unfold tendsto_infinity, contrapose!, rintros ⟨l, hl⟩, cases hl 1 (by linarith) with N hN, use 2 + l, intro N', use max N N', specialize hN (max N N') (by apply le_max_left), rw abs_le at hN, exact ⟨by apply le_max_right, by linarith⟩, end def nondecreasing_seq (u : ℕ → ℝ) := ∀ n m, n ≤ m → u n ≤ u m -- 0067 example (u : ℕ → ℝ) (l : ℝ) (h : seq_limit u l) (h' : nondecreasing_seq u) : ∀ n, u n ≤ l := begin by_contradiction H, push_neg at H, cases H with n hn, cases h ((u n - l)/2) (by linarith) with N hN, specialize hN (max n N) (by apply le_max_right), rw abs_le at hN, have : u n ≤ u (max n N), from h' _ _ (by apply le_max_left), linarith, end /- In the following exercises, `A : set ℝ` means that A is a set of real numbers. We can use the usual notation x ∈ A. The notation `∀ x ∈ A, ...` is the abbreviation of `∀ x, x ∈ A → ... ` The notation `∃ x ∈ A, ...` is the abbreviation of `∃ x, x ∈ A ∧ ... `. More precisely it is the abbreviation of `∃ x (H : x ∈ A), ...` which is Lean's strange way of saying `∃ x, x ∈ A ∧ ... `. You can convert between these forms using the lemma exists_prop {p q : Prop} : (∃ (h : p), q) ↔ p ∧ q We'll work with upper bounds and supremums. Again we'll introduce specialized definitions for the sake of exercises, but mathlib has more general versions. def upper_bound (A : set ℝ) (x : ℝ) := ∀ a ∈ A, a ≤ x def is_sup (A : set ℝ) (x : ℝ) := upper_bound A x ∧ ∀ y, upper_bound A y → x ≤ y Remark: one can easily show that a set of real numbers has at most one sup, but we won't need this. -/ -- 0068 example {A : set ℝ} {x : ℝ} (hx : is_sup A x) : ∀ y, y < x → ∃ a ∈ A, y < a := begin intros y hy, by_contradiction H, push_neg at H, have : x ≤ y, from hx.2 y H, linarith, end /- Let's do a variation on an example from file 07 that will be useful in the last exercise below. -/ -- 0069 lemma le_of_le_add_all' {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin contrapose!, intro h, use (y-x)/2, split; linarith, end -- 0070 example {x y : ℝ} {u : ℕ → ℝ} (hu : seq_limit u x) (ineg : ∀ n, u n ≤ y) : x ≤ y := begin apply le_of_le_add_all', intros ε hε, cases hu ε hε with N hN, specialize hN N (by linarith), rw abs_le at hN, linarith [ineg N], end
77f218be136f4234f6f0469c25f58f5e44d58208
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Meta/Tactic/Intro.lean
2ec8f277e48da62f743ed78073e6d9ccce547c80
[ "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
4,579
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Tactic.Util namespace Lean namespace Meta @[specialize] private partial def introNImpAux {σ} (mvarId : MVarId) (mkName : LocalContext → Name → σ → MetaM (Name × σ)) : Nat → LocalContext → Array Expr → Nat → σ → Expr → MetaM (Array Expr × MVarId) | 0, lctx, fvars, j, _, type => let type := type.instantiateRevRange j fvars.size fvars; adaptReader (fun (ctx : Context) => { ctx with lctx := lctx }) $ withNewLocalInstances fvars j $ do tag ← getMVarTag mvarId; let type := type.headBeta; newMVar ← mkFreshExprSyntheticOpaqueMVar type tag; lctx ← getLCtx; newVal ← mkLambdaFVars fvars newMVar; assignExprMVar mvarId newVal; pure $ (fvars, newMVar.mvarId!) | (i+1), lctx, fvars, j, s, Expr.letE n type val body _ => do let type := type.instantiateRevRange j fvars.size fvars; let type := type.headBeta; let val := val.instantiateRevRange j fvars.size fvars; fvarId ← mkFreshId; (n, s) ← mkName lctx n s; let lctx := lctx.mkLetDecl fvarId n type val; let fvar := mkFVar fvarId; let fvars := fvars.push fvar; introNImpAux i lctx fvars j s body | (i+1), lctx, fvars, j, s, Expr.forallE n type body c => do let type := type.instantiateRevRange j fvars.size fvars; let type := type.headBeta; fvarId ← mkFreshId; (n, s) ← mkName lctx n s; let lctx := lctx.mkLocalDecl fvarId n type c.binderInfo; let fvar := mkFVar fvarId; let fvars := fvars.push fvar; introNImpAux i lctx fvars j s body | (i+1), lctx, fvars, j, s, type => let type := type.instantiateRevRange j fvars.size fvars; adaptReader (fun (ctx : Context) => { ctx with lctx := lctx }) $ withNewLocalInstances fvars j $ do newType ← whnf type; if newType.isForall then introNImpAux (i+1) lctx fvars fvars.size s newType else throwTacticEx `introN mvarId "insufficient number of binders" @[specialize] private def introNImp {σ} (mvarId : MVarId) (n : Nat) (mkName : LocalContext → Name → σ → MetaM (Name × σ)) (s : σ) : MetaM (Array FVarId × MVarId) := withMVarContext mvarId $ do checkNotAssigned mvarId `introN; mvarType ← getMVarType mvarId; lctx ← getLCtx; (fvars, mvarId) ← introNImpAux mvarId mkName n lctx #[] 0 s mvarType; pure (fvars.map Expr.fvarId!, mvarId) def hygienicIntroDef := true def getHygienicIntro : MetaM Bool := do o ← getOptions; pure $ o.get `hygienicIntro hygienicIntroDef @[init] def registerHygienicIntro : IO Unit := registerOption `hygienicIntro { defValue := hygienicIntroDef, group := "tactic", descr := "make sure 'intro'-like tactics are hygienic" } private def mkAuxNameImp (preserveBinderNames : Bool) (hygienic : Bool) (lctx : LocalContext) (binderName : Name) : List Name → MetaM (Name × List Name) | [] => if preserveBinderNames then pure (binderName, []) else if hygienic then do binderName ← mkFreshUserName binderName; pure (binderName, []) else pure (lctx.getUnusedName binderName, []) | n :: rest => if n != "_" then pure (n, rest) else if preserveBinderNames then pure (binderName, rest) else if hygienic then do binderName ← mkFreshUserName binderName; pure (binderName, rest) else pure (lctx.getUnusedName binderName, rest) def introNCore (mvarId : MVarId) (n : Nat) (givenNames : List Name) (preserveBinderNames : Bool) : MetaM (Array FVarId × MVarId) := do hygienic ← getHygienicIntro; if n == 0 then pure (#[], mvarId) else introNImp mvarId n (mkAuxNameImp preserveBinderNames hygienic) givenNames abbrev introN (mvarId : MVarId) (n : Nat) (givenNames : List Name := []) : MetaM (Array FVarId × MVarId) := introNCore mvarId n givenNames false abbrev introNP (mvarId : MVarId) (n : Nat) : MetaM (Array FVarId × MVarId) := introNCore mvarId n [] true def intro (mvarId : MVarId) (name : Name) : MetaM (FVarId × MVarId) := do (fvarIds, mvarId) ← introN mvarId 1 [name]; pure (fvarIds.get! 0, mvarId) def intro1Core (mvarId : MVarId) (preserveBinderNames : Bool) : MetaM (FVarId × MVarId) := do (fvarIds, mvarId) ← introNCore mvarId 1 [] preserveBinderNames; pure (fvarIds.get! 0, mvarId) abbrev intro1 (mvarId : MVarId) : MetaM (FVarId × MVarId) := do intro1Core mvarId false abbrev intro1P (mvarId : MVarId) : MetaM (FVarId × MVarId) := do intro1Core mvarId true end Meta end Lean
5d285f8f1136293d3ca6270b02959d87ea922594
f09e92753b1d3d2eb3ce2cfb5288a7f5d1d4bd89
/src/Spa.lean
7ff3785bfefcfbde09639dca8dd13a824b4843c9
[ "Apache-2.0" ]
permissive
PatrickMassot/lean-perfectoid-spaces
7f63c581db26461b5a92d968e7563247e96a5597
5f70b2020b3c6d508431192b18457fa988afa50d
refs/heads/master
1,625,797,721,782
1,547,308,357,000
1,547,309,364,000
136,658,414
0
1
Apache-2.0
1,528,486,100,000
1,528,486,100,000
null
UTF-8
Lean
false
false
10,463
lean
import ring_theory.localization import ring_theory.subring import continuous_valuations import Huber_pair universes u₁ u₂ u₃ local attribute [instance] classical.prop_decidable open set function Spv -- Wedhorn def 7.23. definition Spa (A : Huber_pair) : set (Spv A) := {v | (v ∈ Cont A) ∧ ∀ r, r ∈ A⁺ → v r ≤ 1} lemma mk_mem_Spa {A : Huber_pair} {v : Valuation A} : mk v ∈ Spa A ↔ (mk v ∈ Cont A) ∧ ∀ r, r ∈ A⁺ → v r ≤ 1 := begin split; intro h; split; try { exact h.left }; intros r hr, { rw ← (v.val).map_one, apply (out_mk r 1).mp, convert h.right r hr, exact valuation.map_one _, }, { rw ← (v.val).map_one at h, convert (out_mk r 1).mpr (h.right r hr), exact (valuation.map_one _).symm } end namespace Spa variable {A : Huber_pair} instance : has_coe (Spa A) (Spv A) := ⟨subtype.val⟩ definition basic_open (r s : A) : set (Spa A) := {v | v r ≤ v s ∧ v s ≠ 0 } lemma mk_mem_basic_open {r s : A} {v : Valuation A} {hv : mk v ∈ Spa A} : (⟨mk v, hv⟩ : Spa A) ∈ basic_open r s ↔ v r ≤ v s ∧ v s ≠ 0 := begin split; intro h; split, { exact (out_mk r s).mp h.left }, { exact Valuation.ne_zero_of_equiv_ne_zero out_mk h.right }, { exact (out_mk r s).mpr h.left }, { exact Valuation.ne_zero_of_equiv_ne_zero (setoid.symm out_mk) h.right } end instance (A : Huber_pair) : topological_space (Spa A) := topological_space.generate_from {U : set (Spa A) | ∃ r s : A, U = basic_open r s} lemma basic_open.is_open (r s : A) : is_open (basic_open r s) := topological_space.generate_open.basic (basic_open r s) ⟨r, ⟨s, rfl⟩⟩ lemma basic_open_eq (s : A) : basic_open s s = {v | v s ≠ 0} := set.ext $ λ v, ⟨λ h, h.right, λ h, ⟨le_refl _, h⟩⟩ -- should only be applied with (HfinT : fintype T) and (Hopen: is_open (span T)) definition rational_open (s : A) (T : set A) : set (Spa A) := {v | (∀ t ∈ T, (v t ≤ v s)) ∧ (v s ≠ 0)} lemma mk_mem_rational_open {s : A} {T : set A} {v : Valuation A} {hv : mk v ∈ Spa A} : (⟨mk v, hv⟩ : Spa A) ∈ rational_open s T ↔ (∀ t ∈ T, (v t ≤ v s)) ∧ (v s ≠ 0) := begin split; intro h; split, { intros t ht, exact (out_mk t s).mp (h.left t ht) }, { exact Valuation.ne_zero_of_equiv_ne_zero out_mk h.right }, { intros t ht, exact (out_mk t s).mpr (h.left t ht) }, { exact Valuation.ne_zero_of_equiv_ne_zero (setoid.symm out_mk) h.right } end definition rational_open_Inter (s : A) (T : set A) : rational_open s T = (set.Inter (λ (t : T), basic_open t s)) ∩ {v | v s ≠ 0} := set.ext $ λ v, ⟨λ ⟨H1, H2⟩, ⟨set.mem_Inter.2 $ λ t, ⟨H1 _ t.property, H2⟩, H2⟩, λ ⟨H1, H2⟩, ⟨λ t ht, (set.mem_Inter.1 H1 ⟨t, ht⟩).1, H2⟩⟩ lemma rational_open_add_s (s : A) (T : set A) : rational_open s T = rational_open s (insert s T) := set.ext $ λ v, ⟨ λ ⟨H1, H2⟩, ⟨λ t Ht, or.rec_on Ht (λ H, by rw H; exact le_refl _) (H1 t), H2⟩, λ ⟨H1, H2⟩, ⟨λ t Ht, H1 t $ set.mem_insert_of_mem _ Ht,H2⟩⟩ lemma rational_open.is_open (s : A) (T : set A) [fintype T] : is_open (rational_open s T) := begin rw rational_open_Inter, apply is_open_inter, swap, rw ← basic_open_eq s, exact basic_open.is_open s s, simpa using @is_open_bInter _ _ _ _ (λ t : T, basic_open t.1 s) (finite_mem_finset finset.univ) (λ t ht, basic_open.is_open t s), end lemma rational_open_inter.aux1 {s₁ s₂ : A} {T₁ T₂ : set A} [fintype T₁] [fintype T₂] (h₁ : s₁ ∈ T₁) (h₂ : s₂ ∈ T₂) : rational_open s₁ T₁ ∩ rational_open s₂ T₂ ⊆ rational_open (s₁ * s₂) ((*) <$> T₁ <*> T₂) := begin rintros v ⟨⟨hv₁, hs₁⟩, ⟨hv₂, hs₂⟩⟩, have vmuls : v (s₁ * s₂) = v s₁ * v s₂ := valuation.map_mul _ _ _, split, { rintros t ⟨_, ⟨t₁, ht₁, rfl⟩, t₂, ht₂, ht⟩, subst ht, have vmult : v (t₁ * t₂) = v t₁ * v t₂ := valuation.map_mul _ _ _, rw [vmuls, vmult], refine le_trans (linear_ordered_comm_monoid.mul_le_mul_left (hv₂ _ ht₂) _) (linear_ordered_comm_monoid.mul_le_mul_right (hv₁ _ ht₁) _ ) }, { intro H, rw vmuls at H, cases H1 : v s₁ with γ₁, exact hs₁ H1, cases H2 : v s₂ with γ₂, exact hs₂ H2, rw [H1,H2] at H, change some (γ₁ * γ₂) = none at H, exact option.no_confusion H }, end lemma rational_open_inter.aux2 {s₁ s₂ : A} {T₁ T₂ : set A} [fintype T₁] [fintype T₂] (h₁ : s₁ ∈ T₁) (h₂ : s₂ ∈ T₂) : rational_open (s₁ * s₂) ((*) <$> T₁ <*> T₂) ⊆ rational_open s₁ T₁ ∩ rational_open s₂ T₂ := begin rintros v ⟨hv,hs⟩, have vmuls : v (s₁ * s₂) = v s₁ * v s₂ := valuation.map_mul _ _ _, have vs₁ne0 : v s₁ ≠ 0 := λ H, by simpa only [vmuls,H,zero_mul,ne.def,eq_self_iff_true,not_true] using hs, have vs₂ne0 : v s₂ ≠ 0 := λ H, by simpa only [vmuls,H,mul_zero,ne.def,eq_self_iff_true,not_true] using hs, split; split, { intros t ht, suffices H : v t * v s₂ ≤ v s₁ * v s₂, { cases H' : v s₂ with γ, exfalso; exact vs₂ne0 H', rw H' at H, have := linear_ordered_comm_monoid.mul_le_mul_right H (some (γ⁻¹)), conv at this { to_lhs, rw mul_assoc, congr, skip, change some (γ * γ⁻¹) }, conv at this { to_rhs, rw mul_assoc, congr, skip, change some (γ * γ⁻¹) }, simp only [mul_right_inv] at this, change v t * 1 ≤ v s₁ * 1 at this, rwa [mul_one,mul_one] at this }, { rw ←vmuls, rw show v t * v s₂ = v (t * s₂), from (valuation.map_mul _ _ _).symm, refine hv (t * s₂) ⟨_,⟨_,ht,rfl⟩,_,h₂,rfl⟩ } }, { exact vs₁ne0 }, { intros t ht, suffices H : v s₁ * v t ≤ v s₁ * v s₂, { cases H' : v s₁ with γ, exfalso; exact vs₁ne0 H', rw H' at H, have := linear_ordered_comm_monoid.mul_le_mul_left H (some (γ⁻¹)), conv at this { to_lhs, rw ← mul_assoc, congr, change some (γ⁻¹ * γ) }, conv at this { to_rhs, rw ← mul_assoc, congr, change some (γ⁻¹ * γ) }, simp only [mul_left_inv] at this, change 1 * v t ≤ 1 * v s₂ at this, rwa [one_mul,one_mul] at this }, { rw ←vmuls, rw show v s₁ * v t = v (s₁ * t), from (valuation.map_mul _ _ _).symm, refine hv _ ⟨_, ⟨s₁, h₁, rfl⟩, t, ht, rfl⟩ } }, { exact vs₂ne0 } end lemma rational_open_inter {s₁ s₂ : A} {T₁ T₂ : set A} [fintype T₁] [fintype T₂] (h₁ : s₁ ∈ T₁) (h₂ : s₂ ∈ T₂) : rational_open s₁ T₁ ∩ rational_open s₂ T₂ = rational_open (s₁ * s₂) ((*) <$> T₁ <*> T₂) := begin ext v, split; intro h, exact rational_open_inter.aux1 h₁ h₂ h, exact rational_open_inter.aux2 h₁ h₂ h end @[simp] lemma rational_open_singleton {r s : A} : rational_open s {r} = basic_open r s := ext $ λ v, { mp := λ h, ⟨h.left r (mem_singleton_iff.mpr rfl), h.right⟩, mpr := λ h, ⟨λ t ht, begin rw mem_singleton_iff at ht, subst ht, exact h.left end, h.right⟩ } @[simp] lemma basic_open_eq_univ : basic_open (1 : A) (1 : A) = univ := begin apply le_antisymm, { exact subset_univ _ }, { intros v h, split, exact le_refl _, have v1 : v 1 = 1 := valuation.map_one _, rw v1, intro h, exact option.no_confusion h }, end @[simp] lemma rational_open_eq_univ : rational_open (1 : A) {(1 : A)} = univ := by simp def rational_basis (A : Huber_pair) : set (set (Spa A)) := {U : set (Spa A) | ∃ {s : A} {T : set A} {h : fintype T}, U = rational_open s T } attribute [instance] set.fintype_seq -- should move to mathlib lemma rational_basis.is_basis : topological_space.is_topological_basis (rational_basis A) := begin split, { rintros U₁ ⟨s₁, T₁, hfin₁, H₁⟩ U₂ ⟨s₂, T₂, hfin₂, H₂⟩ v hv, haveI := hfin₁, haveI := hfin₂, existsi U₁ ∩ U₂, rw rational_open_add_s at H₁ H₂, split, { simp [H₁, H₂,rational_open_inter,-set.fmap_eq_image,-set.seq_eq_set_seq], exact ⟨_,_,by apply_instance,rfl⟩ }, { exact ⟨hv, subset.refl _⟩ } }, split, { apply le_antisymm, { exact subset_univ _ }, apply subset_sUnion_of_mem, refine ⟨(1 : A), {(1 : A)}, by apply_instance, by simp⟩ }, { apply le_antisymm, { unfold Spa.topological_space, rw generate_from_le_iff_subset_is_open, rintros U ⟨r, s, H⟩, rw [H,←rational_open_singleton], refine topological_space.generate_open.basic _ ⟨s, {r}, _, rfl⟩, exact set.fintype_singleton _ }, { rw generate_from_le_iff_subset_is_open, rintros U ⟨s, T, hT, H⟩, subst H, haveI := hT, exact rational_open.is_open s T, } } end namespace rational_open def presheaf.ring (s : A) := localization.away s instance (s : A) : comm_ring (presheaf.ring s) := by dunfold presheaf.ring ; apply_instance def localize (s : A) : A → presheaf.ring s := localization.of_comm_ring A _ -- Definition of A\left(\frac T s\right) as a topological ring def presheaf.top_ring (s : A) (T : set A) (HfinT : fintype T) : topological_space (presheaf.ring s) := let As := presheaf.ring s in sorry /-let D := ring.closure ((localize s) '' A.RHuber.A₀ ∪ (((λ x, x*s⁻¹) ∘ localize s) '' T)) in begin let nhd := λ n : ℕ, mul_ideal (pow_ideal ((localize s) '' A.Rplus) n) D, sorry end-/ end rational_open end Spa -- goal now to define the 𝓞_X on *rational subsets* and then to extend. -- to define it on rational subsets it's just a ring completion. -- remember that a rational open is not actually `rational_open s T` in full -- generality -- we also need that T is finite and that T generates an open ideal in A. -- The construction on p73/74 (note typo in first line of p74 -- ideal should be I.D) -- gives A<T/s> (need completion) and A<T/s>^+ (need integral closure). -- Once we have this, we see mid way through p75 that the definition of the presheaf -- on V is proj lim of O_X(U) as U runs through rationals opens in V. This gets -- the projective limit topology and then we have a presheaf (hopefully this is -- straightforward) of complete topological rings (need proj lim of complete is complete) -- We then need the valuations on the stalks (this is direct limit in cat of rings, forget -- the topology). This will be fiddly but not impossible. -- We then have an object in V^pre and I think then everything should follow.
a9a66ca6780365a2b2bf46dee9d47e970e98f7bf
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/run/choiceMacroRules.lean
80708bc6562d98e146cc00d20aac3fab2df8f667
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
526
lean
syntax:65 [myAdd1] term "+++" term:65 : term syntax:65 [myAdd2] term "+++" term:65 : term macro_rules [myAdd1] | `($a +++ $b) => `($a + $b) macro_rules [myAdd2] | `($a +++ $b) => `($a ++ $b) #check (1:Nat) +++ 3 theorem tst1 : ((1:Nat) +++ 3) = 1 + 3 := rfl #check fun (x : Nat) => if x +++ 3 = x then x else x + 1 #check [1, 2] +++ [3, 4] theorem tst2 : ([1, 2] +++ [3, 4]) = [1, 2] ++ [3, 4] := rfl syntax:65 [myAdd3] term "++" term:65 : term macro_rules [myAdd3] | `($a ++ $b) => `($a + $b) #check (1:Nat) ++ 2
0cc9ea93260fc347765b81dbfdcf4c04949bb70e
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/category_theory/opposites.lean
42c7c63b12c80bb9ccf0708ed029c2987f34f727
[ "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
8,499
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison -/ import category_theory.types import category_theory.natural_isomorphism import data.opposite universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory open opposite variables {C : Type u₁} section has_hom variables [𝒞 : has_hom.{v₁} C] include 𝒞 /-- The hom types of the opposite of a category (or graph). As with the objects, we'll make this irreducible below. Use `f.op` and `f.unop` to convert between morphisms of C and morphisms of Cᵒᵖ. -/ instance has_hom.opposite : has_hom Cᵒᵖ := { hom := λ X Y, unop Y ⟶ unop X } def has_hom.hom.op {X Y : C} (f : X ⟶ Y) : op Y ⟶ op X := f def has_hom.hom.unop {X Y : Cᵒᵖ} (f : X ⟶ Y) : unop Y ⟶ unop X := f attribute [irreducible] has_hom.opposite lemma has_hom.hom.op_inj {X Y : C} : function.injective (has_hom.hom.op : (X ⟶ Y) → (op Y ⟶ op X)) := λ _ _ H, congr_arg has_hom.hom.unop H lemma has_hom.hom.unop_inj {X Y : Cᵒᵖ} : function.injective (has_hom.hom.unop : (X ⟶ Y) → (unop Y ⟶ unop X)) := λ _ _ H, congr_arg has_hom.hom.op H @[simp] lemma has_hom.hom.unop_op {X Y : C} {f : X ⟶ Y} : f.op.unop = f := rfl @[simp] lemma has_hom.hom.op_unop {X Y : Cᵒᵖ} {f : X ⟶ Y} : f.unop.op = f := rfl end has_hom variables [𝒞 : category.{v₁} C] include 𝒞 instance category.opposite : category.{v₁} Cᵒᵖ := { comp := λ _ _ _ f g, (g.unop ≫ f.unop).op, id := λ X, (𝟙 (unop X)).op } @[simp] lemma op_comp {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).op = g.op ≫ f.op := rfl @[simp] lemma op_id {X : C} : (𝟙 X).op = 𝟙 (op X) := rfl @[simp] lemma unop_comp {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).unop = g.unop ≫ f.unop := rfl @[simp] lemma unop_id {X : Cᵒᵖ} : (𝟙 X).unop = 𝟙 (unop X) := rfl @[simp] lemma unop_id_op {X : C} : (𝟙 (op X)).unop = 𝟙 X := rfl @[simp] lemma op_id_unop {X : Cᵒᵖ} : (𝟙 (unop X)).op = 𝟙 X := rfl def op_op : (Cᵒᵖ)ᵒᵖ ⥤ C := { obj := λ X, unop (unop X), map := λ X Y f, f.unop.unop } -- TODO this is an equivalence def is_iso_of_op {X Y : C} (f : X ⟶ Y) [is_iso f.op] : is_iso f := { inv := (inv (f.op)).unop, hom_inv_id' := has_hom.hom.op_inj (by simp), inv_hom_id' := has_hom.hom.op_inj (by simp) } namespace functor section variables {D : Type u₂} [𝒟 : category.{v₂} D] include 𝒟 variables {C D} protected definition op (F : C ⥤ D) : Cᵒᵖ ⥤ Dᵒᵖ := { obj := λ X, op (F.obj (unop X)), map := λ X Y f, (F.map f.unop).op } @[simp] lemma op_obj (F : C ⥤ D) (X : Cᵒᵖ) : (F.op).obj X = op (F.obj (unop X)) := rfl @[simp] lemma op_map (F : C ⥤ D) {X Y : Cᵒᵖ} (f : X ⟶ Y) : (F.op).map f = (F.map f.unop).op := rfl protected definition unop (F : Cᵒᵖ ⥤ Dᵒᵖ) : C ⥤ D := { obj := λ X, unop (F.obj (op X)), map := λ X Y f, (F.map f.op).unop } @[simp] lemma unop_obj (F : Cᵒᵖ ⥤ Dᵒᵖ) (X : C) : (F.unop).obj X = unop (F.obj (op X)) := rfl @[simp] lemma unop_map (F : Cᵒᵖ ⥤ Dᵒᵖ) {X Y : C} (f : X ⟶ Y) : (F.unop).map f = (F.map f.op).unop := rfl variables (C D) definition op_hom : (C ⥤ D)ᵒᵖ ⥤ (Cᵒᵖ ⥤ Dᵒᵖ) := { obj := λ F, (unop F).op, map := λ F G α, { app := λ X, (α.unop.app (unop X)).op, naturality' := λ X Y f, has_hom.hom.unop_inj $ eq.symm (α.unop.naturality f.unop) } } @[simp] lemma op_hom.obj (F : (C ⥤ D)ᵒᵖ) : (op_hom C D).obj F = (unop F).op := rfl @[simp] lemma op_hom.map_app {F G : (C ⥤ D)ᵒᵖ} (α : F ⟶ G) (X : Cᵒᵖ) : ((op_hom C D).map α).app X = (α.unop.app (unop X)).op := rfl definition op_inv : (Cᵒᵖ ⥤ Dᵒᵖ) ⥤ (C ⥤ D)ᵒᵖ := { obj := λ F, op F.unop, map := λ F G α, has_hom.hom.op { app := λ X, (α.app (op X)).unop, naturality' := λ X Y f, has_hom.hom.op_inj $ eq.symm (α.naturality f.op) } } @[simp] lemma op_inv.obj (F : Cᵒᵖ ⥤ Dᵒᵖ) : (op_inv C D).obj F = op F.unop := rfl @[simp] lemma op_inv.map_app {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ⟶ G) (X : C) : (((op_inv C D).map α).unop).app X = (α.app (op X)).unop := rfl -- TODO show these form an equivalence variables {C D} protected definition left_op (F : C ⥤ Dᵒᵖ) : Cᵒᵖ ⥤ D := { obj := λ X, unop (F.obj (unop X)), map := λ X Y f, (F.map f.unop).unop } @[simp] lemma left_op_obj (F : C ⥤ Dᵒᵖ) (X : Cᵒᵖ) : (F.left_op).obj X = unop (F.obj (unop X)) := rfl @[simp] lemma left_op_map (F : C ⥤ Dᵒᵖ) {X Y : Cᵒᵖ} (f : X ⟶ Y) : (F.left_op).map f = (F.map f.unop).unop := rfl protected definition right_op (F : Cᵒᵖ ⥤ D) : C ⥤ Dᵒᵖ := { obj := λ X, op (F.obj (op X)), map := λ X Y f, (F.map f.op).op } @[simp] lemma right_op_obj (F : Cᵒᵖ ⥤ D) (X : C) : (F.right_op).obj X = op (F.obj (op X)) := rfl @[simp] lemma right_op_map (F : Cᵒᵖ ⥤ D) {X Y : C} (f : X ⟶ Y) : (F.right_op).map f = (F.map f.op).op := rfl -- TODO show these form an equivalence instance {F : C ⥤ D} [full F] : full F.op := { preimage := λ X Y f, (F.preimage f.unop).op } instance {F : C ⥤ D} [faithful F] : faithful F.op := { injectivity' := λ X Y f g h, has_hom.hom.unop_inj $ by simpa using injectivity F (has_hom.hom.op_inj h) } end end functor namespace nat_trans variables {D : Type u₂} [𝒟 : category.{v₂} D] include 𝒟 section variables {F G : C ⥤ D} @[simps] protected definition op (α : F ⟶ G) : G.op ⟶ F.op := { app := λ X, (α.app (unop X)).op, naturality' := begin tidy, erw α.naturality, refl, end } @[simp] lemma op_id (F : C ⥤ D) : nat_trans.op (𝟙 F) = 𝟙 (F.op) := rfl @[simps] protected definition unop (α : F.op ⟶ G.op) : G ⟶ F := { app := λ X, (α.app (op X)).unop, naturality' := begin tidy, erw α.naturality, refl, end } @[simp] lemma unop_id (F : C ⥤ D) : nat_trans.unop (𝟙 F.op) = 𝟙 F := rfl end section variables {F G : C ⥤ Dᵒᵖ} protected definition left_op (α : F ⟶ G) : G.left_op ⟶ F.left_op := { app := λ X, (α.app (unop X)).unop, naturality' := begin tidy, erw α.naturality, refl, end } @[simp] lemma left_op_app (α : F ⟶ G) (X) : (nat_trans.left_op α).app X = (α.app (unop X)).unop := rfl protected definition right_op (α : F.left_op ⟶ G.left_op) : G ⟶ F := { app := λ X, (α.app (op X)).op, naturality' := begin tidy, erw α.naturality, refl, end } @[simp] lemma right_op_app (α : F.left_op ⟶ G.left_op) (X) : (nat_trans.right_op α).app X = (α.app (op X)).op := rfl end end nat_trans namespace iso variables {X Y : C} protected definition op (α : X ≅ Y) : op Y ≅ op X := { hom := α.hom.op, inv := α.inv.op, hom_inv_id' := has_hom.hom.unop_inj α.inv_hom_id, inv_hom_id' := has_hom.hom.unop_inj α.hom_inv_id } @[simp] lemma op_hom {α : X ≅ Y} : α.op.hom = α.hom.op := rfl @[simp] lemma op_inv {α : X ≅ Y} : α.op.inv = α.inv.op := rfl end iso namespace nat_iso variables {D : Type u₂} [𝒟 : category.{v₂} D] include 𝒟 variables {F G : C ⥤ D} /-- The natural isomorphism between opposite functors `G.op ≅ F.op` induced by a natural isomorphism between the original functors `F ≅ G`. -/ protected definition op (α : F ≅ G) : G.op ≅ F.op := { hom := nat_trans.op α.hom, inv := nat_trans.op α.inv, hom_inv_id' := begin ext, dsimp, rw ←op_comp, rw inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←op_comp, rw hom_inv_id_app, refl, end } @[simp] lemma op_hom (α : F ≅ G) : (nat_iso.op α).hom = nat_trans.op α.hom := rfl @[simp] lemma op_inv (α : F ≅ G) : (nat_iso.op α).inv = nat_trans.op α.inv := rfl /-- The natural isomorphism between functors `G ≅ F` induced by a natural isomorphism between the opposite functors `F.op ≅ G.op`. -/ protected definition unop (α : F.op ≅ G.op) : G ≅ F := { hom := nat_trans.unop α.hom, inv := nat_trans.unop α.inv, hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw hom_inv_id_app, refl, end } @[simp] lemma unop_hom (α : F.op ≅ G.op) : (nat_iso.unop α).hom = nat_trans.unop α.hom := rfl @[simp] lemma unop_inv (α : F.op ≅ G.op) : (nat_iso.unop α).inv = nat_trans.unop α.inv := rfl end nat_iso end category_theory
beaf729be6e8fc63119d5eff9fcbf50ca07c43c1
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/pnat/basic.lean
6afff1bc11ed06f71871c71ab7cc5be802766631
[ "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
11,532
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Neil Strickland -/ import data.pnat.defs import data.nat.bits import data.nat.order.basic import data.set.basic import algebra.group_with_zero.divisibility import algebra.order.positive.ring /-! # The positive natural numbers This file develops the type `ℕ+` or `pnat`, the subtype of natural numbers that are positive. It is defined in `data.pnat.defs`, but most of the development is deferred to here so that `data.pnat.defs` can have very few imports. -/ attribute [derive [add_left_cancel_semigroup, add_right_cancel_semigroup, add_comm_semigroup, linear_ordered_cancel_comm_monoid, has_add, has_mul, distrib]] pnat namespace pnat @[simp] lemma one_add_nat_pred (n : ℕ+) : 1 + n.nat_pred = n := by rw [nat_pred, add_tsub_cancel_iff_le.mpr $ show 1 ≤ (n : ℕ), from n.2] @[simp] lemma nat_pred_add_one (n : ℕ+) : n.nat_pred + 1 = n := (add_comm _ _).trans n.one_add_nat_pred @[mono] lemma nat_pred_strict_mono : strict_mono nat_pred := λ m n h, nat.pred_lt_pred m.2.ne' h @[mono] lemma nat_pred_monotone : monotone nat_pred := nat_pred_strict_mono.monotone lemma nat_pred_injective : function.injective nat_pred := nat_pred_strict_mono.injective @[simp] lemma nat_pred_lt_nat_pred {m n : ℕ+} : m.nat_pred < n.nat_pred ↔ m < n := nat_pred_strict_mono.lt_iff_lt @[simp] lemma nat_pred_le_nat_pred {m n : ℕ+} : m.nat_pred ≤ n.nat_pred ↔ m ≤ n := nat_pred_strict_mono.le_iff_le @[simp] lemma nat_pred_inj {m n : ℕ+} : m.nat_pred = n.nat_pred ↔ m = n := nat_pred_injective.eq_iff end pnat namespace nat @[mono] theorem succ_pnat_strict_mono : strict_mono succ_pnat := λ m n, nat.succ_lt_succ @[mono] theorem succ_pnat_mono : monotone succ_pnat := succ_pnat_strict_mono.monotone @[simp] theorem succ_pnat_lt_succ_pnat {m n : ℕ} : m.succ_pnat < n.succ_pnat ↔ m < n := succ_pnat_strict_mono.lt_iff_lt @[simp] theorem succ_pnat_le_succ_pnat {m n : ℕ} : m.succ_pnat ≤ n.succ_pnat ↔ m ≤ n := succ_pnat_strict_mono.le_iff_le theorem succ_pnat_injective : function.injective succ_pnat := succ_pnat_strict_mono.injective @[simp] theorem succ_pnat_inj {n m : ℕ} : succ_pnat n = succ_pnat m ↔ n = m := succ_pnat_injective.eq_iff end nat namespace pnat open nat /-- We now define a long list of structures on ℕ+ induced by similar structures on ℕ. Most of these behave in a completely obvious way, but there are a few things to be said about subtraction, division and powers. -/ @[simp, norm_cast] lemma coe_inj {m n : ℕ+} : (m : ℕ) = n ↔ m = n := set_coe.ext_iff @[simp, norm_cast] theorem add_coe (m n : ℕ+) : ((m + n : ℕ+) : ℕ) = m + n := rfl /-- `pnat.coe` promoted to an `add_hom`, that is, a morphism which preserves addition. -/ def coe_add_hom : add_hom ℕ+ ℕ := { to_fun := coe, map_add' := add_coe } instance : covariant_class ℕ+ ℕ+ (+) (≤) := positive.covariant_class_add_le instance : covariant_class ℕ+ ℕ+ (+) (<) := positive.covariant_class_add_lt instance : contravariant_class ℕ+ ℕ+ (+) (≤) := positive.contravariant_class_add_le instance : contravariant_class ℕ+ ℕ+ (+) (<) := positive.contravariant_class_add_lt /-- An equivalence between `ℕ+` and `ℕ` given by `pnat.nat_pred` and `nat.succ_pnat`. -/ @[simps { fully_applied := ff }] def _root_.equiv.pnat_equiv_nat : ℕ+ ≃ ℕ := { to_fun := pnat.nat_pred, inv_fun := nat.succ_pnat, left_inv := succ_pnat_nat_pred, right_inv := nat.nat_pred_succ_pnat } /-- The order isomorphism between ℕ and ℕ+ given by `succ`. -/ @[simps apply { fully_applied := ff }] def _root_.order_iso.pnat_iso_nat : ℕ+ ≃o ℕ := { to_equiv := equiv.pnat_equiv_nat, map_rel_iff' := λ _ _, nat_pred_le_nat_pred } @[simp] lemma _root_.order_iso.pnat_iso_nat_symm_apply : ⇑order_iso.pnat_iso_nat.symm = nat.succ_pnat := rfl theorem lt_add_one_iff : ∀ {a b : ℕ+}, a < b + 1 ↔ a ≤ b := λ a b, nat.lt_add_one_iff theorem add_one_le_iff : ∀ {a b : ℕ+}, a + 1 ≤ b ↔ a < b := λ a b, nat.add_one_le_iff instance : order_bot ℕ+ := { bot := 1, bot_le := λ a, a.property } @[simp] lemma bot_eq_one : (⊥ : ℕ+) = 1 := rfl -- Some lemmas that rewrite `pnat.mk n h`, for `n` an explicit numeral, into explicit numerals. @[simp] lemma mk_bit0 (n) {h} : (⟨bit0 n, h⟩ : ℕ+) = (bit0 ⟨n, pos_of_bit0_pos h⟩ : ℕ+) := rfl @[simp] lemma mk_bit1 (n) {h} {k} : (⟨bit1 n, h⟩ : ℕ+) = (bit1 ⟨n, k⟩ : ℕ+) := rfl -- Some lemmas that rewrite inequalities between explicit numerals in `ℕ+` -- into the corresponding inequalities in `ℕ`. -- TODO: perhaps this should not be attempted by `simp`, -- and instead we should expect `norm_num` to take care of these directly? -- TODO: these lemmas are perhaps incomplete: -- * 1 is not represented as a bit0 or bit1 -- * strict inequalities? @[simp] lemma bit0_le_bit0 (n m : ℕ+) : (bit0 n) ≤ (bit0 m) ↔ (bit0 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.rfl @[simp] lemma bit0_le_bit1 (n m : ℕ+) : (bit0 n) ≤ (bit1 m) ↔ (bit0 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.rfl @[simp] lemma bit1_le_bit0 (n m : ℕ+) : (bit1 n) ≤ (bit0 m) ↔ (bit1 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.rfl @[simp] lemma bit1_le_bit1 (n m : ℕ+) : (bit1 n) ≤ (bit1 m) ↔ (bit1 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.rfl @[simp, norm_cast] theorem mul_coe (m n : ℕ+) : ((m * n : ℕ+) : ℕ) = m * n := rfl /-- `pnat.coe` promoted to a `monoid_hom`. -/ def coe_monoid_hom : ℕ+ →* ℕ := { to_fun := coe, map_one' := one_coe, map_mul' := mul_coe } @[simp] lemma coe_coe_monoid_hom : (coe_monoid_hom : ℕ+ → ℕ) = coe := rfl @[simp] lemma le_one_iff {n : ℕ+} : n ≤ 1 ↔ n = 1 := le_bot_iff lemma lt_add_left (n m : ℕ+) : n < m + n := lt_add_of_pos_left _ m.2 lemma lt_add_right (n m : ℕ+) : n < n + m := (lt_add_left n m).trans_eq (add_comm _ _) @[simp, norm_cast] lemma coe_bit0 (a : ℕ+) : ((bit0 a : ℕ+) : ℕ) = bit0 (a : ℕ) := rfl @[simp, norm_cast] lemma coe_bit1 (a : ℕ+) : ((bit1 a : ℕ+) : ℕ) = bit1 (a : ℕ) := rfl @[simp, norm_cast] theorem pow_coe (m : ℕ+) (n : ℕ) : ((m ^ n : ℕ+) : ℕ) = (m : ℕ) ^ n := rfl /-- Subtraction a - b is defined in the obvious way when a > b, and by a - b = 1 if a ≤ b. -/ instance : has_sub ℕ+ := ⟨λ a b, to_pnat' (a - b : ℕ)⟩ theorem sub_coe (a b : ℕ+) : ((a - b : ℕ+) : ℕ) = ite (b < a) (a - b : ℕ) 1 := begin change (to_pnat' _ : ℕ) = ite _ _ _, split_ifs with h, { exact to_pnat'_coe (tsub_pos_of_lt h) }, { rw tsub_eq_zero_iff_le.mpr (le_of_not_gt h : (a : ℕ) ≤ b), refl } end theorem add_sub_of_lt {a b : ℕ+} : a < b → a + (b - a) = b := λ h, eq $ by { rw [add_coe, sub_coe, if_pos h], exact add_tsub_cancel_of_le h.le } /-- If `n : ℕ+` is different from `1`, then it is the successor of some `k : ℕ+`. -/ lemma exists_eq_succ_of_ne_one : ∀ {n : ℕ+} (h1 : n ≠ 1), ∃ (k : ℕ+), n = k + 1 | ⟨1, _⟩ h1 := false.elim $ h1 rfl | ⟨n+2, _⟩ _ := ⟨⟨n+1, by simp⟩, rfl⟩ /-- Strong induction on `ℕ+`, with `n = 1` treated separately. -/ def case_strong_induction_on {p : ℕ+ → Sort*} (a : ℕ+) (hz : p 1) (hi : ∀ n, (∀ m, m ≤ n → p m) → p (n + 1)) : p a := begin apply strong_induction_on a, rintro ⟨k, kprop⟩ hk, cases k with k, { exact (lt_irrefl 0 kprop).elim }, cases k with k, { exact hz }, exact hi ⟨k.succ, nat.succ_pos _⟩ (λ m hm, hk _ (lt_succ_iff.2 hm)), end /-- An induction principle for `ℕ+`: it takes values in `Sort*`, so it applies also to Types, not only to `Prop`. -/ @[elab_as_eliminator] def rec_on (n : ℕ+) {p : ℕ+ → Sort*} (p1 : p 1) (hp : ∀ n, p n → p (n + 1)) : p n := begin rcases n with ⟨n, h⟩, induction n with n IH, { exact absurd h dec_trivial }, { cases n with n, { exact p1 }, { exact hp _ (IH n.succ_pos) } } end @[simp] theorem rec_on_one {p} (p1 hp) : @pnat.rec_on 1 p p1 hp = p1 := rfl @[simp] theorem rec_on_succ (n : ℕ+) {p : ℕ+ → Sort*} (p1 hp) : @pnat.rec_on (n + 1) p p1 hp = hp n (@pnat.rec_on n p p1 hp) := by { cases n with n h, cases n; [exact absurd h dec_trivial, refl] } lemma mod_div_aux_spec : ∀ (k : ℕ+) (r q : ℕ) (h : ¬ (r = 0 ∧ q = 0)), (((mod_div_aux k r q).1 : ℕ) + k * (mod_div_aux k r q).2 = (r + k * q)) | k 0 0 h := (h ⟨rfl, rfl⟩).elim | k 0 (q + 1) h := by { change (k : ℕ) + (k : ℕ) * (q + 1).pred = 0 + (k : ℕ) * (q + 1), rw [nat.pred_succ, nat.mul_succ, zero_add, add_comm]} | k (r + 1) q h := rfl theorem mod_add_div (m k : ℕ+) : ((mod m k) + k * (div m k) : ℕ) = m := begin let h₀ := nat.mod_add_div (m : ℕ) (k : ℕ), have : ¬ ((m : ℕ) % (k : ℕ) = 0 ∧ (m : ℕ) / (k : ℕ) = 0), by { rintro ⟨hr, hq⟩, rw [hr, hq, mul_zero, zero_add] at h₀, exact (m.ne_zero h₀.symm).elim }, have := mod_div_aux_spec k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) this, exact (this.trans h₀), end theorem div_add_mod (m k : ℕ+) : (k * (div m k) + mod m k : ℕ) = m := (add_comm _ _).trans (mod_add_div _ _) lemma mod_add_div' (m k : ℕ+) : ((mod m k) + (div m k) * k : ℕ) = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : ℕ+) : ((div m k) * k + mod m k : ℕ) = m := by { rw mul_comm, exact div_add_mod _ _ } theorem mod_le (m k : ℕ+) : mod m k ≤ m ∧ mod m k ≤ k := begin change ((mod m k) : ℕ) ≤ (m : ℕ) ∧ ((mod m k) : ℕ) ≤ (k : ℕ), rw [mod_coe], split_ifs, { have hm : (m : ℕ) > 0 := m.pos, rw [← nat.mod_add_div (m : ℕ) (k : ℕ), h, zero_add] at hm ⊢, by_cases h' : ((m : ℕ) / (k : ℕ)) = 0, { rw [h', mul_zero] at hm, exact (lt_irrefl _ hm).elim}, { let h' := nat.mul_le_mul_left (k : ℕ) (nat.succ_le_of_lt (nat.pos_of_ne_zero h')), rw [mul_one] at h', exact ⟨h', le_refl (k : ℕ)⟩ } }, { exact ⟨nat.mod_le (m : ℕ) (k : ℕ), (nat.mod_lt (m : ℕ) k.pos).le⟩ } end theorem dvd_iff {k m : ℕ+} : k ∣ m ↔ (k : ℕ) ∣ (m : ℕ) := begin split; intro h, rcases h with ⟨_, rfl⟩, apply dvd_mul_right, rcases h with ⟨a, h⟩, cases a, { contrapose h, apply ne_zero, }, use a.succ, apply nat.succ_pos, rw [← coe_inj, h, mul_coe, mk_coe], end theorem dvd_iff' {k m : ℕ+} : k ∣ m ↔ mod m k = k := begin rw dvd_iff, rw [nat.dvd_iff_mod_eq_zero], split, { intro h, apply eq, rw [mod_coe, if_pos h] }, { intro h, by_cases h' : (m : ℕ) % (k : ℕ) = 0, { exact h'}, { replace h : ((mod m k) : ℕ) = (k : ℕ) := congr_arg _ h, rw [mod_coe, if_neg h'] at h, exact ((nat.mod_lt (m : ℕ) k.pos).ne h).elim } } end lemma le_of_dvd {m n : ℕ+} : m ∣ n → m ≤ n := by { rw dvd_iff', intro h, rw ← h, apply (mod_le n m).left } theorem mul_div_exact {m k : ℕ+} (h : k ∣ m) : k * (div_exact m k) = m := begin apply eq, rw [mul_coe], change (k : ℕ) * (div m k).succ = m, rw [← div_add_mod m k, dvd_iff'.mp h, nat.mul_succ] end theorem dvd_antisymm {m n : ℕ+} : m ∣ n → n ∣ m → m = n := λ hmn hnm, (le_of_dvd hmn).antisymm (le_of_dvd hnm) theorem dvd_one_iff (n : ℕ+) : n ∣ 1 ↔ n = 1 := ⟨λ h, dvd_antisymm h (one_dvd n), λ h, h.symm ▸ (dvd_refl 1)⟩ lemma pos_of_div_pos {n : ℕ+} {a : ℕ} (h : a ∣ n) : 0 < a := begin apply pos_iff_ne_zero.2, intro hzero, rw hzero at h, exact pnat.ne_zero n (eq_zero_of_zero_dvd h) end end pnat
aad48a60d68326b4c11ca28f4f58b67254f38a83
4727251e0cd73359b15b664c3170e5d754078599
/src/algebraic_geometry/projective_spectrum/topology.lean
68e82cd8de7e5a98f9a9c834c82a4e39f3eea8e5
[ "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
18,947
lean
/- Copyright (c) 2020 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang, Johan Commelin -/ import topology.category.Top import ring_theory.graded_algebra.homogeneous_ideal /-! # Projective spectrum of a graded ring The projective spectrum of a graded commutative ring is the subtype of all homogenous ideals that are prime and do not contain the irrelevant ideal. It is naturally endowed with a topology: the Zariski topology. ## Notation - `R` is a commutative semiring; - `A` is a commutative ring and an `R`-algebra; - `𝒜 : ℕ → submodule R A` is the grading of `A`; ## Main definitions * `projective_spectrum 𝒜`: The projective spectrum of a graded ring `A`, or equivalently, the set of all homogeneous ideals of `A` that is both prime and relevant i.e. not containing irrelevant ideal. Henceforth, we call elements of projective spectrum *relevant homogeneous prime ideals*. * `projective_spectrum.zero_locus 𝒜 s`: The zero locus of a subset `s` of `A` is the subset of `projective_spectrum 𝒜` consisting of all relevant homogeneous prime ideals that contain `s`. * `projective_spectrum.vanishing_ideal t`: The vanishing ideal of a subset `t` of `projective_spectrum 𝒜` is the intersection of points in `t` (viewed as relevant homogeneous prime ideals). * `projective_spectrum.Top`: the topological space of `projective_spectrum 𝒜` endowed with the Zariski topology -/ noncomputable theory open_locale direct_sum big_operators pointwise open direct_sum set_like Top topological_space category_theory opposite variables {R A: Type*} variables [comm_semiring R] [comm_ring A] [algebra R A] variables (𝒜 : ℕ → submodule R A) [graded_algebra 𝒜] /-- The projective spectrum of a graded commutative ring is the subtype of all homogenous ideals that are prime and do not contain the irrelevant ideal. -/ @[nolint has_inhabited_instance] def projective_spectrum := {I : homogeneous_ideal 𝒜 // I.to_ideal.is_prime ∧ ¬(homogeneous_ideal.irrelevant 𝒜 ≤ I)} namespace projective_spectrum variable {𝒜} /-- A method to view a point in the projective spectrum of a graded ring as a homogeneous ideal of that ring. -/ abbreviation as_homogeneous_ideal (x : projective_spectrum 𝒜) : homogeneous_ideal 𝒜 := x.1 lemma as_homogeneous_ideal_def (x : projective_spectrum 𝒜) : x.as_homogeneous_ideal = x.1 := rfl instance is_prime (x : projective_spectrum 𝒜) : x.as_homogeneous_ideal.to_ideal.is_prime := x.2.1 @[ext] lemma ext {x y : projective_spectrum 𝒜} : x = y ↔ x.as_homogeneous_ideal = y.as_homogeneous_ideal := subtype.ext_iff_val variable (𝒜) /-- The zero locus of a set `s` of elements of a commutative ring `A` is the set of all relevant homogeneous prime ideals of the ring that contain the set `s`. An element `f` of `A` can be thought of as a dependent function on the projective spectrum of `𝒜`. At a point `x` (a homogeneous prime ideal) the function (i.e., element) `f` takes values in the quotient ring `A` modulo the prime ideal `x`. In this manner, `zero_locus s` is exactly the subset of `projective_spectrum 𝒜` where all "functions" in `s` vanish simultaneously. -/ def zero_locus (s : set A) : set (projective_spectrum 𝒜) := {x | s ⊆ x.as_homogeneous_ideal} @[simp] lemma mem_zero_locus (x : projective_spectrum 𝒜) (s : set A) : x ∈ zero_locus 𝒜 s ↔ s ⊆ x.as_homogeneous_ideal := iff.rfl @[simp] lemma zero_locus_span (s : set A) : zero_locus 𝒜 (ideal.span s) = zero_locus 𝒜 s := by { ext x, exact (submodule.gi _ _).gc s x.as_homogeneous_ideal.to_ideal } variable {𝒜} /-- The vanishing ideal of a set `t` of points of the prime spectrum of a commutative ring `R` is the intersection of all the prime ideals in the set `t`. An element `f` of `A` can be thought of as a dependent function on the projective spectrum of `𝒜`. At a point `x` (a homogeneous prime ideal) the function (i.e., element) `f` takes values in the quotient ring `A` modulo the prime ideal `x`. In this manner, `vanishing_ideal t` is exactly the ideal of `A` consisting of all "functions" that vanish on all of `t`. -/ def vanishing_ideal (t : set (projective_spectrum 𝒜)) : homogeneous_ideal 𝒜 := ⨅ (x : projective_spectrum 𝒜) (h : x ∈ t), x.as_homogeneous_ideal lemma coe_vanishing_ideal (t : set (projective_spectrum 𝒜)) : (vanishing_ideal t : set A) = {f | ∀ x : projective_spectrum 𝒜, x ∈ t → f ∈ x.as_homogeneous_ideal} := begin ext f, rw [vanishing_ideal, set_like.mem_coe, ← homogeneous_ideal.mem_iff, homogeneous_ideal.to_ideal_infi, submodule.mem_infi], apply forall_congr (λ x, _), rw [homogeneous_ideal.to_ideal_infi, submodule.mem_infi, homogeneous_ideal.mem_iff], end lemma mem_vanishing_ideal (t : set (projective_spectrum 𝒜)) (f : A) : f ∈ vanishing_ideal t ↔ ∀ x : projective_spectrum 𝒜, x ∈ t → f ∈ x.as_homogeneous_ideal := by rw [← set_like.mem_coe, coe_vanishing_ideal, set.mem_set_of_eq] @[simp] lemma vanishing_ideal_singleton (x : projective_spectrum 𝒜) : vanishing_ideal ({x} : set (projective_spectrum 𝒜)) = x.as_homogeneous_ideal := by simp [vanishing_ideal] lemma subset_zero_locus_iff_le_vanishing_ideal (t : set (projective_spectrum 𝒜)) (I : ideal A) : t ⊆ zero_locus 𝒜 I ↔ I ≤ (vanishing_ideal t).to_ideal := ⟨λ h f k, (mem_vanishing_ideal _ _).mpr (λ x j, (mem_zero_locus _ _ _).mpr (h j) k), λ h, λ x j, (mem_zero_locus _ _ _).mpr (le_trans h (λ f h, ((mem_vanishing_ideal _ _).mp h) x j))⟩ variable (𝒜) /-- `zero_locus` and `vanishing_ideal` form a galois connection. -/ lemma gc_ideal : @galois_connection (ideal A) (set (projective_spectrum 𝒜))ᵒᵈ _ _ (λ I, zero_locus 𝒜 I) (λ t, (vanishing_ideal t).to_ideal) := λ I t, subset_zero_locus_iff_le_vanishing_ideal t I /-- `zero_locus` and `vanishing_ideal` form a galois connection. -/ lemma gc_set : @galois_connection (set A) (set (projective_spectrum 𝒜))ᵒᵈ _ _ (λ s, zero_locus 𝒜 s) (λ t, vanishing_ideal t) := have ideal_gc : galois_connection (ideal.span) coe := (submodule.gi A _).gc, by simpa [zero_locus_span, function.comp] using galois_connection.compose ideal_gc (gc_ideal 𝒜) lemma gc_homogeneous_ideal : @galois_connection (homogeneous_ideal 𝒜) (set (projective_spectrum 𝒜))ᵒᵈ _ _ (λ I, zero_locus 𝒜 I) (λ t, (vanishing_ideal t)) := λ I t, by simpa [show I.to_ideal ≤ (vanishing_ideal t).to_ideal ↔ I ≤ (vanishing_ideal t), from iff.rfl] using subset_zero_locus_iff_le_vanishing_ideal t I.to_ideal lemma subset_zero_locus_iff_subset_vanishing_ideal (t : set (projective_spectrum 𝒜)) (s : set A) : t ⊆ zero_locus 𝒜 s ↔ s ⊆ vanishing_ideal t := (gc_set _) s t lemma subset_vanishing_ideal_zero_locus (s : set A) : s ⊆ vanishing_ideal (zero_locus 𝒜 s) := (gc_set _).le_u_l s lemma ideal_le_vanishing_ideal_zero_locus (I : ideal A) : I ≤ (vanishing_ideal (zero_locus 𝒜 I)).to_ideal := (gc_ideal _).le_u_l I lemma homogeneous_ideal_le_vanishing_ideal_zero_locus (I : homogeneous_ideal 𝒜) : I ≤ vanishing_ideal (zero_locus 𝒜 I) := (gc_homogeneous_ideal _).le_u_l I lemma subset_zero_locus_vanishing_ideal (t : set (projective_spectrum 𝒜)) : t ⊆ zero_locus 𝒜 (vanishing_ideal t) := (gc_ideal _).l_u_le t lemma zero_locus_anti_mono {s t : set A} (h : s ⊆ t) : zero_locus 𝒜 t ⊆ zero_locus 𝒜 s := (gc_set _).monotone_l h lemma zero_locus_anti_mono_ideal {s t : ideal A} (h : s ≤ t) : zero_locus 𝒜 (t : set A) ⊆ zero_locus 𝒜 (s : set A) := (gc_ideal _).monotone_l h lemma zero_locus_anti_mono_homogeneous_ideal {s t : homogeneous_ideal 𝒜} (h : s ≤ t) : zero_locus 𝒜 (t : set A) ⊆ zero_locus 𝒜 (s : set A) := (gc_homogeneous_ideal _).monotone_l h lemma vanishing_ideal_anti_mono {s t : set (projective_spectrum 𝒜)} (h : s ⊆ t) : vanishing_ideal t ≤ vanishing_ideal s := (gc_ideal _).monotone_u h lemma zero_locus_bot : zero_locus 𝒜 ((⊥ : ideal A) : set A) = set.univ := (gc_ideal 𝒜).l_bot @[simp] lemma zero_locus_singleton_zero : zero_locus 𝒜 ({0} : set A) = set.univ := zero_locus_bot _ @[simp] lemma zero_locus_empty : zero_locus 𝒜 (∅ : set A) = set.univ := (gc_set 𝒜).l_bot @[simp] lemma vanishing_ideal_univ : vanishing_ideal (∅ : set (projective_spectrum 𝒜)) = ⊤ := by simpa using (gc_ideal _).u_top lemma zero_locus_empty_of_one_mem {s : set A} (h : (1:A) ∈ s) : zero_locus 𝒜 s = ∅ := set.eq_empty_iff_forall_not_mem.mpr $ λ x hx, (infer_instance : x.as_homogeneous_ideal.to_ideal.is_prime).ne_top $ x.as_homogeneous_ideal.to_ideal.eq_top_iff_one.mpr $ hx h @[simp] lemma zero_locus_singleton_one : zero_locus 𝒜 ({1} : set A) = ∅ := zero_locus_empty_of_one_mem 𝒜 (set.mem_singleton (1 : A)) @[simp] lemma zero_locus_univ : zero_locus 𝒜 (set.univ : set A) = ∅ := zero_locus_empty_of_one_mem _ (set.mem_univ 1) lemma zero_locus_sup_ideal (I J : ideal A) : zero_locus 𝒜 ((I ⊔ J : ideal A) : set A) = zero_locus _ I ∩ zero_locus _ J := (gc_ideal 𝒜).l_sup lemma zero_locus_sup_homogeneous_ideal (I J : homogeneous_ideal 𝒜) : zero_locus 𝒜 ((I ⊔ J : homogeneous_ideal 𝒜) : set A) = zero_locus _ I ∩ zero_locus _ J := (gc_homogeneous_ideal 𝒜).l_sup lemma zero_locus_union (s s' : set A) : zero_locus 𝒜 (s ∪ s') = zero_locus _ s ∩ zero_locus _ s' := (gc_set 𝒜).l_sup lemma vanishing_ideal_union (t t' : set (projective_spectrum 𝒜)) : vanishing_ideal (t ∪ t') = vanishing_ideal t ⊓ vanishing_ideal t' := by ext1; convert (gc_ideal 𝒜).u_inf lemma zero_locus_supr_ideal {γ : Sort*} (I : γ → ideal A) : zero_locus _ ((⨆ i, I i : ideal A) : set A) = (⋂ i, zero_locus 𝒜 (I i)) := (gc_ideal 𝒜).l_supr lemma zero_locus_supr_homogeneous_ideal {γ : Sort*} (I : γ → homogeneous_ideal 𝒜) : zero_locus _ ((⨆ i, I i : homogeneous_ideal 𝒜) : set A) = (⋂ i, zero_locus 𝒜 (I i)) := (gc_homogeneous_ideal 𝒜).l_supr lemma zero_locus_Union {γ : Sort*} (s : γ → set A) : zero_locus 𝒜 (⋃ i, s i) = (⋂ i, zero_locus 𝒜 (s i)) := (gc_set 𝒜).l_supr lemma zero_locus_bUnion (s : set (set A)) : zero_locus 𝒜 (⋃ s' ∈ s, s' : set A) = ⋂ s' ∈ s, zero_locus 𝒜 s' := by simp only [zero_locus_Union] lemma vanishing_ideal_Union {γ : Sort*} (t : γ → set (projective_spectrum 𝒜)) : vanishing_ideal (⋃ i, t i) = (⨅ i, vanishing_ideal (t i)) := homogeneous_ideal.to_ideal_injective $ by convert (gc_ideal 𝒜).u_infi; exact homogeneous_ideal.to_ideal_infi _ lemma zero_locus_inf (I J : ideal A) : zero_locus 𝒜 ((I ⊓ J : ideal A) : set A) = zero_locus 𝒜 I ∪ zero_locus 𝒜 J := set.ext $ λ x, by simpa using x.2.1.inf_le lemma union_zero_locus (s s' : set A) : zero_locus 𝒜 s ∪ zero_locus 𝒜 s' = zero_locus 𝒜 ((ideal.span s) ⊓ (ideal.span s'): ideal A) := by { rw zero_locus_inf, simp } lemma zero_locus_mul_ideal (I J : ideal A) : zero_locus 𝒜 ((I * J : ideal A) : set A) = zero_locus 𝒜 I ∪ zero_locus 𝒜 J := set.ext $ λ x, by simpa using x.2.1.mul_le lemma zero_locus_mul_homogeneous_ideal (I J : homogeneous_ideal 𝒜) : zero_locus 𝒜 ((I * J : homogeneous_ideal 𝒜) : set A) = zero_locus 𝒜 I ∪ zero_locus 𝒜 J := set.ext $ λ x, by simpa using x.2.1.mul_le lemma zero_locus_singleton_mul (f g : A) : zero_locus 𝒜 ({f * g} : set A) = zero_locus 𝒜 {f} ∪ zero_locus 𝒜 {g} := set.ext $ λ x, by simpa using x.2.1.mul_mem_iff_mem_or_mem @[simp] lemma zero_locus_singleton_pow (f : A) (n : ℕ) (hn : 0 < n) : zero_locus 𝒜 ({f ^ n} : set A) = zero_locus 𝒜 {f} := set.ext $ λ x, by simpa using x.2.1.pow_mem_iff_mem n hn lemma sup_vanishing_ideal_le (t t' : set (projective_spectrum 𝒜)) : vanishing_ideal t ⊔ vanishing_ideal t' ≤ vanishing_ideal (t ∩ t') := begin intros r, rw [← homogeneous_ideal.mem_iff, homogeneous_ideal.to_ideal_sup, mem_vanishing_ideal, submodule.mem_sup], rintro ⟨f, hf, g, hg, rfl⟩ x ⟨hxt, hxt'⟩, erw mem_vanishing_ideal at hf hg, apply submodule.add_mem; solve_by_elim end lemma mem_compl_zero_locus_iff_not_mem {f : A} {I : projective_spectrum 𝒜} : I ∈ (zero_locus 𝒜 {f} : set (projective_spectrum 𝒜))ᶜ ↔ f ∉ I.as_homogeneous_ideal := by rw [set.mem_compl_eq, mem_zero_locus, set.singleton_subset_iff]; refl /-- The Zariski topology on the prime spectrum of a commutative ring is defined via the closed sets of the topology: they are exactly those sets that are the zero locus of a subset of the ring. -/ instance zariski_topology : topological_space (projective_spectrum 𝒜) := topological_space.of_closed (set.range (projective_spectrum.zero_locus 𝒜)) (⟨set.univ, by simp⟩) begin intros Zs h, rw set.sInter_eq_Inter, let f : Zs → set _ := λ i, classical.some (h i.2), have hf : ∀ i : Zs, ↑i = zero_locus 𝒜 (f i) := λ i, (classical.some_spec (h i.2)).symm, simp only [hf], exact ⟨_, zero_locus_Union 𝒜 _⟩ end (by { rintros _ ⟨s, rfl⟩ _ ⟨t, rfl⟩, exact ⟨_, (union_zero_locus 𝒜 s t).symm⟩ }) /-- The underlying topology of `Proj` is the projective spectrum of graded ring `A`. -/ def Top : Top := Top.of (projective_spectrum 𝒜) lemma is_open_iff (U : set (projective_spectrum 𝒜)) : is_open U ↔ ∃ s, Uᶜ = zero_locus 𝒜 s := by simp only [@eq_comm _ Uᶜ]; refl lemma is_closed_iff_zero_locus (Z : set (projective_spectrum 𝒜)) : is_closed Z ↔ ∃ s, Z = zero_locus 𝒜 s := by rw [← is_open_compl_iff, is_open_iff, compl_compl] lemma is_closed_zero_locus (s : set A) : is_closed (zero_locus 𝒜 s) := by { rw [is_closed_iff_zero_locus], exact ⟨s, rfl⟩ } lemma zero_locus_vanishing_ideal_eq_closure (t : set (projective_spectrum 𝒜)) : zero_locus 𝒜 (vanishing_ideal t : set A) = closure t := begin apply set.subset.antisymm, { rintro x hx t' ⟨ht', ht⟩, obtain ⟨fs, rfl⟩ : ∃ s, t' = zero_locus 𝒜 s, by rwa [is_closed_iff_zero_locus] at ht', rw [subset_zero_locus_iff_subset_vanishing_ideal] at ht, exact set.subset.trans ht hx }, { rw (is_closed_zero_locus _ _).closure_subset_iff, exact subset_zero_locus_vanishing_ideal 𝒜 t } end lemma vanishing_ideal_closure (t : set (projective_spectrum 𝒜)) : vanishing_ideal (closure t) = vanishing_ideal t := begin have := (gc_ideal 𝒜).u_l_u_eq_u t, dsimp only at this, ext1, erw zero_locus_vanishing_ideal_eq_closure 𝒜 t at this, exact this, end section basic_open /-- `basic_open r` is the open subset containing all prime ideals not containing `r`. -/ def basic_open (r : A) : topological_space.opens (projective_spectrum 𝒜) := { val := { x | r ∉ x.as_homogeneous_ideal }, property := ⟨{r}, set.ext $ λ x, set.singleton_subset_iff.trans $ not_not.symm⟩ } @[simp] lemma mem_basic_open (f : A) (x : projective_spectrum 𝒜) : x ∈ basic_open 𝒜 f ↔ f ∉ x.as_homogeneous_ideal := iff.rfl lemma mem_coe_basic_open (f : A) (x : projective_spectrum 𝒜) : x ∈ (↑(basic_open 𝒜 f): set (projective_spectrum 𝒜)) ↔ f ∉ x.as_homogeneous_ideal := iff.rfl lemma is_open_basic_open {a : A} : is_open ((basic_open 𝒜 a) : set (projective_spectrum 𝒜)) := (basic_open 𝒜 a).property @[simp] lemma basic_open_eq_zero_locus_compl (r : A) : (basic_open 𝒜 r : set (projective_spectrum 𝒜)) = (zero_locus 𝒜 {r})ᶜ := set.ext $ λ x, by simpa only [set.mem_compl_eq, mem_zero_locus, set.singleton_subset_iff] @[simp] lemma basic_open_one : basic_open 𝒜 (1 : A) = ⊤ := topological_space.opens.ext $ by simp @[simp] lemma basic_open_zero : basic_open 𝒜 (0 : A) = ⊥ := topological_space.opens.ext $ by simp lemma basic_open_mul (f g : A) : basic_open 𝒜 (f * g) = basic_open 𝒜 f ⊓ basic_open 𝒜 g := topological_space.opens.ext $ by {simp [zero_locus_singleton_mul]} lemma basic_open_mul_le_left (f g : A) : basic_open 𝒜 (f * g) ≤ basic_open 𝒜 f := by { rw basic_open_mul 𝒜 f g, exact inf_le_left } lemma basic_open_mul_le_right (f g : A) : basic_open 𝒜 (f * g) ≤ basic_open 𝒜 g := by { rw basic_open_mul 𝒜 f g, exact inf_le_right } @[simp] lemma basic_open_pow (f : A) (n : ℕ) (hn : 0 < n) : basic_open 𝒜 (f ^ n) = basic_open 𝒜 f := topological_space.opens.ext $ by simpa using zero_locus_singleton_pow 𝒜 f n hn lemma basic_open_eq_union_of_projection (f : A) : basic_open 𝒜 f = ⨆ (i : ℕ), basic_open 𝒜 (graded_algebra.proj 𝒜 i f) := topological_space.opens.ext $ set.ext $ λ z, begin erw [mem_coe_basic_open, topological_space.opens.mem_Sup], split; intros hz, { rcases show ∃ i, graded_algebra.proj 𝒜 i f ∉ z.as_homogeneous_ideal, begin contrapose! hz with H, haveI : Π (i : ℕ) (x : 𝒜 i), decidable (x ≠ 0) := λ _, classical.dec_pred _, rw ←graded_algebra.sum_support_decompose 𝒜 f, apply ideal.sum_mem _ (λ i hi, H i) end with ⟨i, hi⟩, exact ⟨basic_open 𝒜 (graded_algebra.proj 𝒜 i f), ⟨i, rfl⟩, by rwa mem_basic_open⟩ }, { obtain ⟨_, ⟨i, rfl⟩, hz⟩ := hz, exact λ rid, hz (z.1.2 i rid) }, end lemma is_topological_basis_basic_opens : topological_space.is_topological_basis (set.range (λ (r : A), (basic_open 𝒜 r : set (projective_spectrum 𝒜)))) := begin apply topological_space.is_topological_basis_of_open_of_nhds, { rintros _ ⟨r, rfl⟩, exact is_open_basic_open 𝒜 }, { rintros p U hp ⟨s, hs⟩, rw [← compl_compl U, set.mem_compl_eq, ← hs, mem_zero_locus, set.not_subset] at hp, obtain ⟨f, hfs, hfp⟩ := hp, refine ⟨basic_open 𝒜 f, ⟨f, rfl⟩, hfp, _⟩, rw [← set.compl_subset_compl, ← hs, basic_open_eq_zero_locus_compl, compl_compl], exact zero_locus_anti_mono 𝒜 (set.singleton_subset_iff.mpr hfs) } end end basic_open section order /-! ## The specialization order We endow `projective_spectrum 𝒜` with a partial order, where `x ≤ y` if and only if `y ∈ closure {x}`. -/ instance : partial_order (projective_spectrum 𝒜) := subtype.partial_order _ @[simp] lemma as_ideal_le_as_ideal (x y : projective_spectrum 𝒜) : x.as_homogeneous_ideal ≤ y.as_homogeneous_ideal ↔ x ≤ y := subtype.coe_le_coe @[simp] lemma as_ideal_lt_as_ideal (x y : projective_spectrum 𝒜) : x.as_homogeneous_ideal < y.as_homogeneous_ideal ↔ x < y := subtype.coe_lt_coe lemma le_iff_mem_closure (x y : projective_spectrum 𝒜) : x ≤ y ↔ y ∈ closure ({x} : set (projective_spectrum 𝒜)) := begin rw [← as_ideal_le_as_ideal, ← zero_locus_vanishing_ideal_eq_closure, mem_zero_locus, vanishing_ideal_singleton], simp only [coe_subset_coe, subtype.coe_le_coe, coe_coe], end end order end projective_spectrum
7fbbdd6f762f5df6196f8f08e22561389f55e3ff
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/unify_equations.lean
f6f777a399da02782313e2bc58db50ccee64708d
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
1,470
lean
/- Copyright (c) 2020 Jannis Limperg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jannis Limperg -/ import tactic.unify_equations -- An example which exercises the injectivity, substitution and heterogeneous -- downgrading rules. example (P : ∀ n, fin n → Prop) (n m : ℕ) (f : fin n) (g : fin m) (h₁ : n + 1 = m + 1) (h₂ : f == g) (h₃ : P n f) : P m g := begin unify_equations h₁ h₂, exact h₃ end -- An example which exercises the injectivity and no-confusion rules. example (n m : ℕ) (h : [n] = [n, m]) : false := begin unify_equations h end -- An example which exercises the deletion rule. example (n) (h : n + 1 = n + 1) : true := begin unify_equations h, guard_hyp n : ℕ, guard_target true, trivial end -- An example which exercises the substitution and cycle elimination rules. example (n m : ℕ) (h₁ : n = m) (h₂ : n = m + 3) : false := begin unify_equations h₁ h₂ end -- The tactic should also support generalised inductive types. Here's a nested -- type. inductive rose : Type | tip : ℕ → rose | node : list rose → rose open rose example (t u v : rose) (h : node [t] = node [u, v]) : false := begin unify_equations h end -- However, the cycle elimination rule currently does not work with generalised -- inductive types. example (t u : rose) (h : t = node [t]) : true := begin unify_equations h, guard_hyp h : t = node [t], trivial end
69c106a565850a7ffd796517ad2ed33f73491eae
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/wlog.lean
f009ffa2887a6dfaa359ca824fe6d798d1d741d5
[ "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
9,115
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 Without loss of generality tactic. -/ import data.list.perm open expr tactic lean lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic private meta def update_pp_name : expr → name → expr | (local_const n _ bi d) pp := local_const n pp bi d | e n := e private meta def elim_or : ℕ → expr → tactic (list expr) | 0 h := fail "zero cases" | 1 h := return [h] | (n + 1) h := do [(_, [hl], []), (_, [hr], [])] ← induction h, -- there should be no dependent terms [gl, gr] ← get_goals, set_goals [gr], hsr ← elim_or n hr, gsr ← get_goals, set_goals (gl :: gsr), return (hl :: hsr) private meta def dest_or : expr → tactic (list expr) | e := do `(%%a ∨ %%b) ← whnf e | return [e], lb ← dest_or b, return (a :: lb) private meta def match_perms (pat : pattern) : expr → tactic (list $ list expr) | t := (do m ← match_pattern pat t, guard (m.2.all expr.is_local_constant), return [m.2]) <|> (do `(%%l ∨ %%r) ← whnf t, m ← match_pattern pat l, rs ← match_perms r, return (m.2 :: rs)) meta def wlog (vars' : list expr) (h_cases fst_case : expr) (perms : list (list expr)) : tactic unit := do guard h_cases.is_local_constant, -- reorder s.t. context is Γ ⬝ vars ⬝ cases ⊢ ∀deps, … nr ← revert_lst (vars' ++ [h_cases]), vars ← intron' vars'.length, h_cases ← intro h_cases.local_pp_name, cases ← infer_type h_cases, h_fst_case ← mk_local_def h_cases.local_pp_name (fst_case.instantiate_locals $ (vars'.zip vars).map $ λ⟨o, n⟩, (o.local_uniq_name, n)), ((), pr) ← solve_aux cases (repeat $ exact h_fst_case <|> left >> skip), t ← target, fixed_vars ← vars.mmap update_type, let t' := (instantiate_local h_cases.local_uniq_name pr t).pis (fixed_vars ++ [h_fst_case]), (h, [g]) ← local_proof `this t' (do clear h_cases, vars.mmap clear, intron nr), h₀ :: hs ← elim_or perms.length h_cases, solve1 (do exact (h.mk_app $ vars ++ [h₀])), focus ((hs.zip perms.tail).map $ λ⟨h_case, perm⟩, do let p_v := (vars'.zip vars).map (λ⟨p, v⟩, (p.local_uniq_name, v)), let p := perm.map (λp, p.instantiate_locals p_v), note `this none (h.mk_app $ p ++ [h_case]), clear h, return ()), gs ← get_goals, set_goals (g :: gs) namespace interactive open interactive interactive.types expr private meta def parse_permutations : option (list (list name)) → tactic (list (list expr)) | none := return [] | (some []) := return [] | (some perms@(p₀ :: ps)) := do (guard p₀.nodup <|> fail "No permutation `xs_i` in `using [xs_1, …, xs_n]` should contain the same variable twice."), (guard (perms.all $ λp, p.perm p₀) <|> fail "The permutations `xs_i` in `using [xs_1, …, xs_n]` must be permutations of the same variables."), perms.mmap (λp, p.mmap get_local) /-- Without loss of generality: reduces to one goal under variables permutations. Given a goal of the form `g xs`, a predicate `p` over a set of variables, as well as variable permutations `xs_i`. Then `wlog` produces goals of the form The case goal, i.e. the permutation `xs_i` covers all possible cases: `⊢ p xs_0 ∨ ⋯ ∨ p xs_n` The main goal, i.e. the goal reduced to `xs_0`: `(h : p xs_0) ⊢ g xs_0` The invariant goals, i.e. `g` is invariant under `xs_i`: `(h : p xs_i) (this : g xs_0) ⊢ gs xs_i` Either the permutation is provided, or a proof of the disjunction is provided to compute the permutation. The disjunction need to be in assoc normal form, e.g. `p₀ ∨ (p₁ ∨ p₂)`. In many cases the invariant goals can be solved by AC rewriting using `cc` etc. Example: On a state `(n m : ℕ) ⊢ p n m` the tactic `wlog h : n ≤ m using [n m, m n]` produces the following states: `(n m : ℕ) ⊢ n ≤ m ∨ m ≤ n` `(n m : ℕ) (h : n ≤ m) ⊢ p n m` `(n m : ℕ) (h : m ≤ n) (this : p n m) ⊢ p m n` `wlog` supports different calling conventions. The name `h` is used to give a name to the introduced case hypothesis. If the name is avoided, the default will be `case`. (1) `wlog : p xs0 using [xs0, …, xsn]` Results in the case goal `p xs0 ∨ ⋯ ∨ ps xsn`, the main goal `(case : p xs0) ⊢ g xs0` and the invariance goals `(case : p xsi) (this : g xs0) ⊢ g xsi`. (2) `wlog : p xs0 := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the variable permutations. (3) `wlog := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the variable permutations. This is not as stable as (2), for example `p` cannot be a disjunction. (4) `wlog : R x y using x y` and `wlog : R x y` Produces the case `R x y ∨ R y x`. If `R` is ≤, then the disjunction discharged using linearity. If `using x y` is avoided then `x` and `y` are the last two variables appearing in the expression `R x y`. -/ meta def wlog (h : parse ident?) (pat : parse (tk ":" *> texpr)?) (cases : parse (tk ":=" *> texpr)?) (perms : parse (tk "using" *> (list_of (ident*) <|> (λx, [x]) <$> ident*))?) (discharger : tactic unit := (tactic.solve_by_elim <|> tactic.tautology {classical := tt} <|> using_smt (smt_tactic.intros >> smt_tactic.solve_goals))) : tactic unit := do perms ← parse_permutations perms, (pat, cases_pr, cases_goal, vars, perms) ← (match cases with | some r := do vars::_ ← return perms | fail "At least one set of variables expected, i.e. `using x y` or `using [x y, y x]`.", cases_pr ← to_expr r, cases_pr ← (if cases_pr.is_local_constant then return $ match h with some n := update_pp_name cases_pr n | none := cases_pr end else do note (h.get_or_else `case) none cases_pr), cases ← infer_type cases_pr, (pat, perms') ← match pat with | some pat := do pat ← to_expr pat, let vars' := vars.filter $ λv, v.occurs pat, case_pat ← mk_pattern [] vars' pat [] vars', perms' ← match_perms case_pat cases, return (pat, perms') | none := do (p :: ps) ← dest_or cases, let vars' := vars.filter $ λv, v.occurs p, case_pat ← mk_pattern [] vars' p [] vars', perms' ← (p :: ps).mmap (λp, do m ← match_pattern case_pat p, return m.2), return (p, perms') end, let vars_name := vars.map local_uniq_name, guard (perms'.all $ λp, p.all $ λv, v.is_local_constant ∧ v.local_uniq_name ∈ vars_name) <|> fail "Cases contains variables not declared in `using x y z`", perms ← (if perms.length = 1 then do return (perms'.map $ λp, p ++ vars.filter (λv, p.all (λv', v'.local_uniq_name ≠ v.local_uniq_name))) else do guard (perms.length = perms'.length) <|> fail "The provided permutation list has a different length then the provided cases.", return perms), return (pat, cases_pr, @none expr, vars, perms) | none := do let name_h := h.get_or_else `case, some pat ← return pat | fail "Either specify cases or a pattern with permutations", pat ← to_expr pat, (do [x, y] ← match perms with | [] := return pat.list_local_consts | [l] := return l | _ := failed end, let cases := mk_or_lst [pat, pat.instantiate_locals [(x.local_uniq_name, y), (y.local_uniq_name, x)]], (do `(%%x' ≤ %%y') ← return pat, (cases_pr, []) ← local_proof name_h cases (exact ``(le_total %%x' %%y')), return (pat, cases_pr, none, [x, y], [[x, y], [y, x]])) <|> (do (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, [x, y], [[x, y], [y, x]]))) <|> (do guard (perms.length ≥ 2) <|> fail ("To generate cases at least two permutations are required, i.e. `using [x y, y x]`" ++ " or exactly 0 or 2 variables"), (vars :: perms') ← return perms, let names := vars.map local_uniq_name, let cases := mk_or_lst (pat :: perms'.map (λp, pat.instantiate_locals (names.zip p))), (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, vars, perms)) end), let name_fn := (if perms.length = 2 then λi, `invariant else λi, mk_simple_name ("invariant_" ++ to_string (i + 1))), with_enable_tags $ tactic.focus1 $ do t ← get_main_tag, tactic.wlog vars cases_pr pat perms, tactic.focus (set_main_tag (mk_num_name `_case 0 :: `main :: t) :: (list.range (perms.length - 1)).map (λi, do set_main_tag (mk_num_name `_case 0 :: name_fn i :: t), try discharger)), match cases_goal with | some g := do set_tag g (mk_num_name `_case 0 :: `cases :: t), gs ← get_goals, set_goals (g :: gs) | none := skip end add_tactic_doc { name := "wlog", category := doc_category.tactic, decl_names := [``wlog], tags := ["logic"] } end interactive end tactic
e1c4aca3132647504aa52be4e8b50f0592227867
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/ring_theory/subring.lean
d95757a88bcb0fcae257d11147c3fd229c0f7df4
[ "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
30,702
lean
/- Copyright (c) 2020 Ashvni Narayanan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors : Ashvni Narayanan -/ import deprecated.subring import group_theory.subgroup import ring_theory.subsemiring /-! # Subrings Let `R` be a ring. This file defines the "bundled" subring type `subring R`, a type whose terms correspond to subrings of `R`. This is the preferred way to talk about subrings in mathlib. Unbundled subrings (`s : set R` and `is_subring s`) are not in this file, and they will ultimately be deprecated. We prove that subrings are a complete lattice, and that you can `map` (pushforward) and `comap` (pull back) them along ring homomorphisms. We define the `closure` construction from `set R` to `subring R`, sending a subset of `R` to the subring it generates, and prove that it is a Galois insertion. ## Main definitions Notation used here: `(R : Type u) [ring R] (S : Type u) [ring S] (f g : R →+* S)` `(A : subring R) (B : subring S) (s : set R)` * `subring R` : the type of subrings of a ring `R`. * `instance : complete_lattice (subring R)` : the complete lattice structure on the subrings. * `subring.closure` : subring closure of a set, i.e., the smallest subring that includes the set. * `subring.gi` : `closure : set M → subring M` and coercion `coe : subring M → set M` form a `galois_insertion`. * `comap f B : subring A` : the preimage of a subring `B` along the ring homomorphism `f` * `map f A : subring B` : the image of a subring `A` along the ring homomorphism `f`. * `prod A B : subring (R × S)` : the product of subrings * `f.range : subring B` : the range of the ring homomorphism `f`. * `eq_locus f g : subring R` : given ring homomorphisms `f g : R →+* S`, the subring of `R` where `f x = g x` ## Implementation notes A subring is implemented as a subsemiring which is also an additive subgroup. The initial PR was as a submonoid which is also an additive subgroup. Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although `∈` is defined as membership of a subring's underlying set. ## Tags subring, subrings -/ open_locale big_operators universes u v w variables {R : Type u} {S : Type v} {T : Type w} [ring R] [ring S] [ring T] set_option old_structure_cmd true /-- `subring R` is the type of subrings of `R`. A subring of `R` is a subset `s` that is a multiplicative submonoid and an additive subgroup. Note in particular that it shares the same 0 and 1 as R. -/ structure subring (R : Type u) [ring R] extends subsemiring R, add_subgroup R /-- Reinterpret a `subring` as a `subsemiring`. -/ add_decl_doc subring.to_subsemiring /-- Reinterpret a `subring` as an `add_subgroup`. -/ add_decl_doc subring.to_add_subgroup namespace subring /-- The underlying submonoid of a subring. -/ def to_submonoid (s : subring R) : submonoid R := { carrier := s.carrier, ..s.to_subsemiring.to_submonoid } instance : has_coe (subring R) (set R) := ⟨subring.carrier⟩ instance : has_coe_to_sort (subring R) := ⟨Type*, λ S, S.carrier⟩ instance : has_mem R (subring R) := ⟨λ m S, m ∈ (S:set R)⟩ /-- Construct a `subring R` from a set `s`, a submonoid `sm`, and an additive subgroup `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/ protected def mk' (s : set R) (sm : submonoid R) (sa : add_subgroup R) (hm : ↑sm = s) (ha : ↑sa = s) : subring R := { carrier := s, zero_mem' := ha ▸ sa.zero_mem, one_mem' := hm ▸ sm.one_mem, add_mem' := λ x y, by simpa only [← ha] using sa.add_mem, mul_mem' := λ x y, by simpa only [← hm] using sm.mul_mem, neg_mem' := λ x, by simpa only [← ha] using sa.neg_mem, } @[simp] lemma coe_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha : set R) = s := rfl @[simp] lemma mem_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) {x : R} : x ∈ subring.mk' s sm sa hm ha ↔ x ∈ s := iff.rfl @[simp] lemma mk'_to_submonoid {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha).to_submonoid = sm := submonoid.ext' hm.symm @[simp] lemma mk'_to_add_subgroup {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa =s) : (subring.mk' s sm sa hm ha).to_add_subgroup = sa := add_subgroup.ext' ha.symm end subring /-- Construct a `subring` from a set satisfying `is_subring`. -/ def set.to_subring (S : set R) [is_subring S] : subring R := { carrier := S, one_mem' := is_submonoid.one_mem, mul_mem' := λ a b, is_submonoid.mul_mem, zero_mem' := is_add_submonoid.zero_mem, add_mem' := λ a b, is_add_submonoid.add_mem, neg_mem' := λ a, is_add_subgroup.neg_mem } protected lemma subring.exists {s : subring R} {p : s → Prop} : (∃ x : s, p x) ↔ ∃ x ∈ s, p ⟨x, ‹x ∈ s›⟩ := set_coe.exists protected lemma subring.forall {s : subring R} {p : s → Prop} : (∀ x : s, p x) ↔ ∀ x ∈ s, p ⟨x, ‹x ∈ s›⟩ := set_coe.forall /-- A `subsemiring` containing -1 is a `subring`. -/ def subsemiring.to_subring (s : subsemiring R) (hneg : (-1 : R) ∈ s) : subring R := { neg_mem' := by { rintros x, rw <-neg_one_mul, apply subsemiring.mul_mem, exact hneg, } ..s.to_submonoid, ..s.to_add_submonoid } namespace subring variables (s : subring R) /-- Two subrings are equal if the underlying subsets are equal. -/ theorem ext' ⦃s t : subring R⦄ (h : (s : set R) = t) : s = t := by { cases s, cases t, congr' } /-- Two subrings are equal if and only if the underlying subsets are equal. -/ protected theorem ext'_iff {s t : subring R} : s = t ↔ (s : set R) = t := ⟨λ h, h ▸ rfl, λ h, ext' h⟩ /-- Two subrings are equal if they have the same elements. -/ @[ext] theorem ext {S T : subring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h /-- A subring contains the ring's 1. -/ theorem one_mem : (1 : R) ∈ s := s.one_mem' /-- A subring contains the ring's 0. -/ theorem zero_mem : (0 : R) ∈ s := s.zero_mem' /-- A subring is closed under multiplication. -/ theorem mul_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x * y ∈ s := s.mul_mem' /-- A subring is closed under addition. -/ theorem add_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x + y ∈ s := s.add_mem' /-- A subring is closed under negation. -/ theorem neg_mem : ∀ {x : R}, x ∈ s → -x ∈ s := s.neg_mem' /-- A subring is closed under subtraction -/ theorem sub_mem {x y : R} (hx : x ∈ s) (hy : y ∈ s) : x - y ∈ s := by { rw sub_eq_add_neg, exact s.add_mem hx (s.neg_mem hy) } /-- Product of a list of elements in a subring is in the subring. -/ lemma list_prod_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.prod ∈ s := s.to_submonoid.list_prod_mem /-- Sum of a list of elements in a subring is in the subring. -/ lemma list_sum_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.sum ∈ s := s.to_add_subgroup.list_sum_mem /-- Product of a multiset of elements in a subring of a `comm_ring` is in the subring. -/ lemma multiset_prod_mem {R} [comm_ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.prod ∈ s := s.to_submonoid.multiset_prod_mem m /-- Sum of a multiset of elements in an `subring` of a `ring` is in the `subring`. -/ lemma multiset_sum_mem {R} [ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.sum ∈ s := s.to_add_subgroup.multiset_sum_mem m /-- Product of elements of a subring of a `comm_ring` indexed by a `finset` is in the subring. -/ lemma prod_mem {R : Type*} [comm_ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∏ i in t, f i ∈ s := s.to_submonoid.prod_mem h /-- Sum of elements in a `subring` of a `ring` indexed by a `finset` is in the `subring`. -/ lemma sum_mem {R : Type*} [ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∑ i in t, f i ∈ s := s.to_add_subgroup.sum_mem h lemma pow_mem {x : R} (hx : x ∈ s) (n : ℕ) : x^n ∈ s := s.to_submonoid.pow_mem hx n lemma gsmul_mem {x : R} (hx : x ∈ s) (n : ℤ) : n •ℤ x ∈ s := s.to_add_subgroup.gsmul_mem hx n lemma coe_int_mem (n : ℤ) : (n : R) ∈ s := by simp only [← gsmul_one, gsmul_mem, one_mem] /-- A subring of a ring inherits a ring structure -/ instance to_ring : ring s := { right_distrib := λ x y z, subtype.eq $ right_distrib x y z, left_distrib := λ x y z, subtype.eq $ left_distrib x y z, .. s.to_submonoid.to_monoid, .. s.to_add_subgroup.to_add_comm_group } @[simp, norm_cast] lemma coe_add (x y : s) : (↑(x + y) : R) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_neg (x : s) : (↑(-x) : R) = -↑x := rfl @[simp, norm_cast] lemma coe_mul (x y : s) : (↑(x * y) : R) = ↑x * ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : s) : R) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : s) : R) = 1 := rfl @[simp] lemma coe_eq_zero_iff {x : s} : (x : R) = 0 ↔ x = 0 := ⟨λ h, subtype.ext (trans h s.coe_zero.symm), λ h, h.symm ▸ s.coe_zero⟩ /-- A subring of a `comm_ring` is a `comm_ring`. -/ instance to_comm_ring {R} [comm_ring R] (s : subring R) : comm_ring s := { mul_comm := λ _ _, subtype.eq $ mul_comm _ _, ..subring.to_ring s} /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype (s : subring R) : s →+* R := { to_fun := coe, .. s.to_submonoid.subtype, .. s.to_add_subgroup.subtype } @[simp] theorem coe_subtype : ⇑s.subtype = coe := rfl /-! # Partial order -/ instance : partial_order (subring R) := { le := λ s t, ∀ ⦃x⦄, x ∈ s → x ∈ t, .. partial_order.lift (coe : subring R → set R) ext' } lemma le_def {s t : subring R} : s ≤ t ↔ ∀ ⦃x : R⦄, x ∈ s → x ∈ t := iff.rfl @[simp, norm_cast] lemma coe_subset_coe {s t : subring R} : (s : set R) ⊆ t ↔ s ≤ t := iff.rfl @[simp, norm_cast] lemma coe_ssubset_coe {s t : subring R} : (s : set R) ⊂ t ↔ s < t := iff.rfl @[simp, norm_cast] lemma mem_coe {S : subring R} {m : R} : m ∈ (S : set R) ↔ m ∈ S := iff.rfl @[simp, norm_cast] lemma coe_coe (s : subring R) : ↥(s : set R) = s := rfl @[simp] lemma mem_to_submonoid {s : subring R} {x : R} : x ∈ s.to_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_submonoid (s : subring R) : (s.to_submonoid : set R) = s := rfl @[simp] lemma mem_to_add_subgroup {s : subring R} {x : R} : x ∈ s.to_add_subgroup ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_add_subgroup (s : subring R) : (s.to_add_subgroup : set R) = s := rfl /-! # top -/ /-- The subring `R` of the ring `R`. -/ instance : has_top (subring R) := ⟨{ .. (⊤ : submonoid R), .. (⊤ : add_subgroup R) }⟩ @[simp] lemma mem_top (x : R) : x ∈ (⊤ : subring R) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : subring R) : set R) = set.univ := rfl /-! # comap -/ /-- The preimage of a subring along a ring homomorphism is a subring. -/ def comap {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) : subring R := { carrier := f ⁻¹' s.carrier, .. s.to_submonoid.comap (f : R →* S), .. s.to_add_subgroup.comap (f : R →+ S) } @[simp] lemma coe_comap (s : subring S) (f : R →+* S) : (s.comap f : set R) = f ⁻¹' s := rfl @[simp] lemma mem_comap {s : subring S} {f : R →+* S} {x : R} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl lemma comap_comap (s : subring T) (g : S →+* T) (f : R →+* S) : (s.comap g).comap f = s.comap (g.comp f) := rfl /-! # map -/ /-- The image of a subring along a ring homomorphism is a subring. -/ def map {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : subring S := { carrier := f '' s.carrier, .. s.to_submonoid.map (f : R →* S), .. s.to_add_subgroup.map (f : R →+ S) } @[simp] lemma coe_map (f : R →+* S) (s : subring R) : (s.map f : set S) = f '' s := rfl @[simp] lemma mem_map {f : R →+* S} {s : subring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := set.mem_image_iff_bex lemma map_map (g : S →+* T) (f : R →+* S) : (s.map f).map g = s.map (g.comp f) := ext' $ set.image_image _ _ _ lemma map_le_iff_le_comap {f : R →+* S} {s : subring R} {t : subring S} : s.map f ≤ t ↔ s ≤ t.comap f := set.image_subset_iff lemma gc_map_comap (f : R →+* S) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap end subring namespace ring_hom variables (g : S →+* T) (f : R →+* S) /-! # range -/ /-- The range of a ring homomorphism, as a subring of the target. -/ def range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : subring S := (⊤ : subring R).map f @[simp] lemma coe_range : (f.range : set S) = set.range f := set.image_univ @[simp] lemma mem_range {f : R →+* S} {y : S} : y ∈ f.range ↔ ∃ x, f x = y := by simp [range] lemma mem_range_self (f : R →+* S) (x : R) : f x ∈ f.range := mem_range.mpr ⟨x, rfl⟩ lemma map_range : f.range.map g = (g.comp f).range := (⊤ : subring R).map_map g f -- TODO -- rename to `cod_restrict` when is_ring_hom is deprecated /-- Restrict the codomain of a ring homomorphism to a subring that includes the range. -/ def cod_restrict' {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) (h : ∀ x, f x ∈ s) : R →+* s := { to_fun := λ x, ⟨f x, h x⟩, map_add' := λ x y, subtype.eq $ f.map_add x y, map_zero' := subtype.eq f.map_zero, map_mul' := λ x y, subtype.eq $ f.map_mul x y, map_one' := subtype.eq f.map_one } lemma surjective_onto_range : function.surjective (f.cod_restrict' f.range f.mem_range_self) := λ ⟨y, hy⟩, let ⟨x, hx⟩ := mem_range.mp hy in ⟨x, subtype.ext hx⟩ end ring_hom namespace subring variables {cR : Type u} [comm_ring cR] /-- A subring of a commutative ring is a commutative ring. -/ def subset_comm_ring (S : subring cR) : comm_ring S := {mul_comm := λ _ _, subtype.eq $ mul_comm _ _, ..subring.to_ring S} /-- A subring of an integral domain is an integral domain. -/ instance subring.domain {D : Type*} [integral_domain D] (S : subring D) : integral_domain S := { exists_pair_ne := ⟨0, 1, mt subtype.ext_iff_val.1 zero_ne_one⟩, eq_zero_or_eq_zero_of_mul_eq_zero := λ ⟨x, hx⟩ ⟨y, hy⟩, by { simp only [subtype.ext_iff_val, subtype.coe_mk], exact eq_zero_or_eq_zero_of_mul_eq_zero }, .. S.subset_comm_ring, } /-! # bot -/ instance : has_bot (subring R) := ⟨(int.cast_ring_hom R).range⟩ instance : inhabited (subring R) := ⟨⊥⟩ lemma coe_bot : ((⊥ : subring R) : set R) = set.range (coe : ℤ → R) := ring_hom.coe_range (int.cast_ring_hom R) lemma mem_bot {x : R} : x ∈ (⊥ : subring R) ↔ ∃ (n : ℤ), ↑n = x := ring_hom.mem_range /-! # inf -/ /-- The inf of two subrings is their intersection. -/ instance : has_inf (subring R) := ⟨λ s t, { carrier := s ∩ t, .. s.to_submonoid ⊓ t.to_submonoid, .. s.to_add_subgroup ⊓ t.to_add_subgroup }⟩ @[simp] lemma coe_inf (p p' : subring R) : ((p ⊓ p' : subring R) : set R) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : subring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (subring R) := ⟨λ s, subring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, subring.to_submonoid t ) (⨅ t ∈ s, subring.to_add_subgroup t) (by simp) (by simp)⟩ @[simp, norm_cast] lemma coe_Inf (S : set (subring R)) : ((Inf S : subring R) : set R) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (subring R)} {x : R} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[simp] lemma Inf_to_submonoid (s : set (subring R)) : (Inf s).to_submonoid = ⨅ t ∈ s, subring.to_submonoid t := mk'_to_submonoid _ _ @[simp] lemma Inf_to_add_subgroup (s : set (subring R)) : (Inf s).to_add_subgroup = ⨅ t ∈ s, subring.to_add_subgroup t := mk'_to_add_subgroup _ _ /-- Subrings of a ring form a complete lattice. -/ instance : complete_lattice (subring R) := { bot := (⊥), bot_le := λ s x hx, let ⟨n, hn⟩ := mem_bot.1 hx in hn ▸ s.coe_int_mem n, top := (⊤), le_top := λ s x hx, trivial, inf := (⊓), inf_le_left := λ s t x, and.left, inf_le_right := λ s t x, and.right, le_inf := λ s t₁ t₂ h₁ h₂ x hx, ⟨h₁ hx, h₂ hx⟩, .. complete_lattice_of_Inf (subring R) (λ s, is_glb.of_image (λ s t, show (s : set R) ≤ t ↔ s ≤ t, from coe_subset_coe) is_glb_binfi)} /-! # subring closure of a subset -/ /-- The `subring` generated by a set. -/ def closure (s : set R) : subring R := Inf {S | s ⊆ S} lemma mem_closure {x : R} {s : set R} : x ∈ closure s ↔ ∀ S : subring R, s ⊆ S → x ∈ S := mem_Inf /-- The subring generated by a set includes the set. -/ @[simp] lemma subset_closure {s : set R} : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx /-- A subring `t` includes `closure s` if and only if it includes `s`. -/ @[simp] lemma closure_le {s : set R} {t : subring R} : closure s ≤ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, λ h, Inf_le h⟩ /-- Subring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ lemma closure_mono ⦃s t : set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ set.subset.trans h subset_closure lemma closure_eq_of_le {s : set R} {t : subring R} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (closure_le.2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements of `s`, and is preserved under addition, negation, and multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {s : set R} {p : R → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : p 1) (Hadd : ∀ x y, p x → p y → p (x + y)) (Hneg : ∀ (x : R), p x → p (-x)) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, H1, Hmul, H0, Hadd, Hneg⟩).2 Hs h lemma mem_closure_iff {s : set R} {x} : x ∈ closure s ↔ x ∈ add_subgroup.closure (submonoid.closure s : set R) := ⟨ λ h, closure_induction h (λ x hx, add_subgroup.subset_closure $ submonoid.subset_closure hx ) (add_subgroup.zero_mem _) (add_subgroup.subset_closure ( submonoid.one_mem (submonoid.closure s)) ) (λ x y hx hy, add_subgroup.add_mem _ hx hy ) (λ x hx, add_subgroup.neg_mem _ hx ) ( λ x y hx hy, add_subgroup.closure_induction hy (λ q hq, add_subgroup.closure_induction hx ( λ p hp, add_subgroup.subset_closure ((submonoid.closure s).mul_mem hp hq) ) ( begin rw zero_mul q, apply add_subgroup.zero_mem _, end ) ( λ p₁ p₂ ihp₁ ihp₂, begin rw add_mul p₁ p₂ q, apply add_subgroup.add_mem _ ihp₁ ihp₂, end ) ( λ x hx, begin have f : -x * q = -(x*q) := by simp, rw f, apply add_subgroup.neg_mem _ hx, end ) ) ( begin rw mul_zero x, apply add_subgroup.zero_mem _, end ) ( λ q₁ q₂ ihq₁ ihq₂, begin rw mul_add x q₁ q₂, apply add_subgroup.add_mem _ ihq₁ ihq₂ end ) ( λ z hz, begin have f : x * -z = -(x*z) := by simp, rw f, apply add_subgroup.neg_mem _ hz, end ) ), λ h, add_subgroup.closure_induction h ( λ x hx, submonoid.closure_induction hx ( λ x hx, subset_closure hx ) ( one_mem _ ) ( λ x y hx hy, mul_mem _ hx hy ) ) ( zero_mem _ ) (λ x y hx hy, add_mem _ hx hy) ( λ x hx, neg_mem _ hx ) ⟩ theorem exists_list_of_mem_closure {s : set R} {x : R} (h : x ∈ closure s) : (∃ L : list (list R), (∀ t ∈ L, ∀ y ∈ t, y ∈ s ∨ y = (-1:R)) ∧ (L.map list.prod).sum = x) := add_subgroup.closure_induction (mem_closure_iff.1 h) (λ x hx, let ⟨l, hl, h⟩ :=submonoid.exists_list_of_mem_closure hx in ⟨[l], by simp [h]; clear_aux_decl; tauto!⟩) ⟨[], by simp⟩ (λ x y ⟨l, hl1, hl2⟩ ⟨m, hm1, hm2⟩, ⟨l ++ m, λ t ht, (list.mem_append.1 ht).elim (hl1 t) (hm1 t), by simp [hl2, hm2]⟩) (λ x ⟨L, hL⟩, ⟨L.map (list.cons (-1)), list.forall_mem_map_iff.2 $ λ j hj, list.forall_mem_cons.2 ⟨or.inr rfl, hL.1 j hj⟩, hL.2 ▸ list.rec_on L (by simp) (by simp [list.map_cons, add_comm] {contextual := tt})⟩) variable (R) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure R _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {R} /-- Closure of a subring `S` equals `S`. -/ lemma closure_eq (s : subring R) : closure (s : set R) = s := (subring.gi R).l_u_eq s @[simp] lemma closure_empty : closure (∅ : set R) = ⊥ := (subring.gi R).gc.l_bot @[simp] lemma closure_univ : closure (set.univ : set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ lemma closure_union (s t : set R) : closure (s ∪ t) = closure s ⊔ closure t := (subring.gi R).gc.l_sup lemma closure_Union {ι} (s : ι → set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subring.gi R).gc.l_supr lemma closure_sUnion (s : set (set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (subring.gi R).gc.l_Sup lemma map_sup (s t : subring R) (f : R →+* S) : (s ⊔ t).map f = s.map f ⊔ t.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : R →+* S) (s : ι → subring R) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (s t : subring S) (f : R →+* S) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : R →+* S) (s : ι → subring S) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : R →+* S) : (⊥ : subring R).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : R →+* S) : (⊤ : subring S).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `subring`s `s`, `t` of rings `R`, `S` respectively, `s.prod t` is `s × t` as a subring of `R × S`. -/ def prod (s : subring R) (t : subring S) : subring (R × S) := { carrier := (s : set R).prod t, .. s.to_submonoid.prod t.to_submonoid, .. s.to_add_subgroup.prod t.to_add_subgroup} @[norm_cast] lemma coe_prod (s : subring R) (t : subring S) : (s.prod t : set (R × S)) = (s : set R).prod (t : set S) := rfl lemma mem_prod {s : subring R} {t : subring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[mono] lemma prod_mono ⦃s₁ s₂ : subring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : subring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht lemma prod_mono_right (s : subring R) : monotone (λ t : subring S, s.prod t) := prod_mono (le_refl s) lemma prod_mono_left (t : subring S) : monotone (λ s : subring R, s.prod t) := λ s₁ s₂ hs, prod_mono hs (le_refl t) lemma prod_top (s : subring R) : s.prod (⊤ : subring S) = s.comap (ring_hom.fst R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] lemma top_prod (s : subring S) : (⊤ : subring R).prod s = s.comap (ring_hom.snd R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp] lemma top_prod_top : (⊤ : subring R).prod (⊤ : subring S) = ⊤ := (top_prod _).trans $ comap_top _ /-- Product of subrings is isomorphic to their product as rings. -/ def prod_equiv (s : subring R) (t : subring S) : s.prod t ≃+* s × t := { map_mul' := λ x y, rfl, map_add' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } /-- The underlying set of a non-empty directed Sup of subrings is just a union of the subrings. Note that this fails without the directedness assumption (the union of two subrings is typically not a subring) -/ lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) {x : R} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (le_def.1 $ le_supr S i) hi⟩, let U : subring R := subring.mk' (⋃ i, (S i : set R)) (⨆ i, (S i).to_submonoid) (⨆ i, (S i).to_add_subgroup) (submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)) (add_subgroup.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)), suffices : (⨆ i, S i) ≤ U, by simpa using @this x, exact supr_le (λ i x hx, set.mem_Union.2 ⟨i, hx⟩), end lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) : ((⨆ i, S i : subring R) : set R) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] lemma mem_Sup_of_directed_on {S : set (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : R} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end lemma coe_Sup_of_directed_on {S : set (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set R) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] end subring namespace ring_hom variables [ring T] {s : subring R} open subring /-- Restriction of a ring homomorphism to a subring of the domain. -/ def restrict (f : R →+* S) (s : subring R) : s →+* S := f.comp s.subtype @[simp] lemma restrict_apply (f : R →+* S) (x : s) : f.restrict s x = f x := rfl /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. -/ def range_restrict (f : R →+* S) : R →+* f.range := f.cod_restrict' f.range $ λ x, ⟨x, subring.mem_top x, rfl⟩ @[simp] lemma coe_range_restrict (f : R →+* S) (x : R) : (f.range_restrict x : S) = f x := rfl lemma range_top_iff_surjective {f : R →+* S} : f.range = (⊤ : subring S) ↔ function.surjective f := subring.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ lemma range_top_of_surjective (f : R →+* S) (hf : function.surjective f) : f.range = (⊤ : subring S) := range_top_iff_surjective.2 hf /-- The subring of elements `x : R` such that `f x = g x`, i.e., the equalizer of f and g as a subring of R -/ def eq_locus (f g : R →+* S) : subring R := { carrier := {x | f x = g x}, .. (f : R →* S).eq_mlocus g, .. (f : R →+ S).eq_locus g } /-- If two ring homomorphisms are equal on a set, then they are equal on its subring closure. -/ lemma eq_on_set_closure {f g : R →+* S} {s : set R} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_locus g, from closure_le.2 h lemma eq_of_eq_on_set_top {f g : R →+* S} (h : set.eq_on f g (⊤ : subring R)) : f = g := ext $ λ x, h trivial lemma eq_of_eq_on_set_dense {s : set R} (hs : closure s = ⊤) {f g : R →+* S} (h : s.eq_on f g) : f = g := eq_of_eq_on_set_top $ hs ▸ eq_on_set_closure h lemma closure_preimage_le (f : R →+* S) (s : set S) : 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 ring homomorphism of the subring generated by a set equals the subring generated by the image of the set. -/ lemma map_closure (f : R →+* S) (s : set R) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (closure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end ring_hom namespace subring open ring_hom /-- The ring homomorphism associated to an inclusion of subrings. -/ def inclusion {S T : subring R} (h : S ≤ T) : S →* T := S.subtype.cod_restrict' _ (λ x, h x.2) @[simp] lemma range_subtype (s : subring R) : s.subtype.range = s := ext' $ (coe_srange _).trans subtype.range_coe @[simp] lemma range_fst : (fst R S).srange = ⊤ := (fst R S).srange_top_of_surjective $ prod.fst_surjective @[simp] lemma range_snd : (snd R S).srange = ⊤ := (snd R S).srange_top_of_surjective $ prod.snd_surjective @[simp] lemma prod_bot_sup_bot_prod (s : subring R) (t : subring S) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem _ ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, mem_coe.2 $ one_mem ⊥⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨mem_coe.2 $ one_mem ⊥, hp.2⟩) end subring namespace ring_equiv variables {s t : subring R} /-- Makes the identity isomorphism from a proof two subrings of a multiplicative monoid are equal. -/ def subring_congr (h : s = t) : s ≃+* t := { map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ subring.ext'_iff.1 h } end ring_equiv namespace subring variables {s : set R} local attribute [reducible] closure @[elab_as_eliminator] protected theorem in_closure.rec_on {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ z ∈ s, ∀ n, C n → C (z * n)) (ha : ∀ {x y}, C x → C y → C (x + y)) : C x := begin have h0 : C 0 := add_neg_self (1:R) ▸ ha h1 hneg1, rcases exists_list_of_mem_closure hx with ⟨L, HL, rfl⟩, clear hx, induction L with hd tl ih, { exact h0 }, rw list.forall_mem_cons at HL, suffices : C (list.prod hd), { rw [list.map_cons, list.sum_cons], exact ha this (ih HL.2) }, replace HL := HL.1, clear ih tl, suffices : ∃ L : list R, (∀ x ∈ L, x ∈ s) ∧ (list.prod hd = list.prod L ∨ list.prod hd = -list.prod L), { rcases this with ⟨L, HL', HP | HP⟩, { rw HP, clear HP HL hd, induction L with hd tl ih, { exact h1 }, rw list.forall_mem_cons at HL', rw list.prod_cons, exact hs _ HL'.1 _ (ih HL'.2) }, rw HP, clear HP HL hd, induction L with hd tl ih, { exact hneg1 }, rw [list.prod_cons, neg_mul_eq_mul_neg], rw list.forall_mem_cons at HL', exact hs _ HL'.1 _ (ih HL'.2) }, induction hd with hd tl ih, { exact ⟨[], list.forall_mem_nil _, or.inl rfl⟩ }, rw list.forall_mem_cons at HL, rcases ih HL.2 with ⟨L, HL', HP | HP⟩; cases HL.1 with hhd hhd, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inl $ by rw [list.prod_cons, list.prod_cons, HP]⟩ }, { exact ⟨L, HL', or.inr $ by rw [list.prod_cons, hhd, neg_one_mul, HP]⟩ }, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inr $ by rw [list.prod_cons, list.prod_cons, HP, neg_mul_eq_mul_neg]⟩ }, { exact ⟨L, HL', or.inl $ by rw [list.prod_cons, hhd, HP, neg_one_mul, neg_neg]⟩ } end lemma closure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx end subring
8fe0d756240d921dfcc51d54aec68070ccebef65
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/06_Inductive_Types.org.11.lean
243782345d6df74a9ae44e6082ddcc7ed5d6c0e1
[]
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
187
lean
/- page 80 -/ import standard import data.bool open bool namespace hide -- BEGIN definition band (b1 b2 : bool) : bool := bool.cases_on b1 ff (bool.cases_on b2 ff tt) -- END end hide
d646f444efa39b62181b1f8e5fe058e47cc0869c
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/algebra/category/Group/limits.lean
c9bdcd874a034449d62b5f4bca99f62f61ff7879
[ "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
9,747
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.Mon.limits import algebra.category.Group.preadditive import category_theory.over import category_theory.limits.concrete_category import category_theory.limits.shapes.concrete_category import group_theory.subgroup /-! # The category of (commutative) (additive) groups 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 universe u noncomputable theory variables {J : Type u} [small_category J] namespace Group @[to_additive] instance group_obj (F : J ⥤ Group) (j) : group ((F ⋙ forget Group).obj j) := by { change group (F.obj j), apply_instance } /-- The flat sections of a functor into `Group` form a subgroup of all sections. -/ @[to_additive "The flat sections of a functor into `AddGroup` form an additive subgroup of all sections."] def sections_subgroup (F : J ⥤ Group) : subgroup (Π j, F.obj j) := { carrier := (F ⋙ forget Group).sections, inv_mem' := λ a ah j j' f, begin simp only [forget_map_eq_coe, functor.comp_map, pi.inv_apply, monoid_hom.map_inv, inv_inj], dsimp [functor.sections] at ah, rw ah f, end, ..(Mon.sections_submonoid (F ⋙ forget₂ Group Mon)) } @[to_additive] instance limit_group (F : J ⥤ Group) : group (types.limit_cone (F ⋙ forget Group.{u})).X := begin change group (sections_subgroup F), apply_instance, end /-- We show that the forgetful functor `Group ⥤ Mon` creates limits. All we need to do is notice that the limit point has a `group` instance available, and then reuse the existing limit. -/ @[to_additive] instance (F : J ⥤ Group) : creates_limit F (forget₂ Group Mon.{u}) := creates_limit_of_reflects_iso (λ c' t, { lifted_cone := { X := Group.of (types.limit_cone (F ⋙ forget Group)).X, π := { app := Mon.limit_π_monoid_hom (F ⋙ forget₂ Group Mon.{u}), naturality' := (Mon.has_limits.limit_cone (F ⋙ forget₂ _ _)).π.naturality, } }, valid_lift := is_limit.unique_up_to_iso (Mon.has_limits.limit_cone_is_limit _) t, makes_limit := is_limit.of_faithful (forget₂ Group Mon.{u}) (Mon.has_limits.limit_cone_is_limit _) (λ s, _) (λ s, rfl) }) /-- A choice of limit cone for a functor into `Group`. (Generally, you'll just want to use `limit F`.) -/ @[to_additive "A choice of limit cone for a functor into `Group`. (Generally, you'll just want to use `limit F`.)"] def limit_cone (F : J ⥤ Group) : cone F := lift_limit (limit.is_limit (F ⋙ (forget₂ Group Mon.{u}))) /-- The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.) -/ @[to_additive "The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.)"] def limit_cone_is_limit (F : J ⥤ Group) : is_limit (limit_cone F) := lifted_limit_is_limit _ /-- The category of groups has all limits. -/ @[to_additive] instance has_limits : has_limits Group := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit_of_created F (forget₂ Group Mon) } } -- TODO use the above instead? /-- The forgetful functor from groups to monoids preserves all limits. (That is, the underlying monoid could have been computed instead as limits in the category of monoids.) -/ @[to_additive AddGroup.forget₂_AddMon_preserves_limits] instance forget₂_Mon_preserves_limits : preserves_limits (forget₂ Group Mon) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by apply_instance } } /-- The forgetful functor from groups to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ @[to_additive] instance forget_preserves_limits : preserves_limits (forget Group) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, limits.comp_preserves_limit (forget₂ Group Mon) (forget Mon) } } end Group namespace CommGroup @[to_additive] instance comm_group_obj (F : J ⥤ CommGroup) (j) : comm_group ((F ⋙ forget CommGroup).obj j) := by { change comm_group (F.obj j), apply_instance } @[to_additive] instance limit_comm_group (F : J ⥤ CommGroup) : comm_group (types.limit_cone (F ⋙ forget CommGroup.{u})).X := @subgroup.to_comm_group (Π j, F.obj j) _ (Group.sections_subgroup (F ⋙ forget₂ CommGroup Group.{u})) /-- We show that the forgetful functor `CommGroup ⥤ Group` creates limits. All we need to do is notice that the limit point has a `comm_group` instance available, and then reuse the existing limit. -/ @[to_additive] instance (F : J ⥤ CommGroup) : creates_limit F (forget₂ CommGroup Group.{u}) := creates_limit_of_reflects_iso (λ c' t, { lifted_cone := { X := CommGroup.of (types.limit_cone (F ⋙ forget CommGroup)).X, π := { app := Mon.limit_π_monoid_hom (F ⋙ forget₂ CommGroup Group.{u} ⋙ forget₂ Group Mon), naturality' := (Mon.has_limits.limit_cone _).π.naturality, } }, valid_lift := is_limit.unique_up_to_iso (Group.limit_cone_is_limit _) t, makes_limit := is_limit.of_faithful (forget₂ _ Group.{u} ⋙ forget₂ _ Mon.{u}) (Mon.has_limits.limit_cone_is_limit _) (λ s, _) (λ s, rfl) }) /-- A choice of limit cone for a functor into `CommGroup`. (Generally, you'll just want to use `limit F`.) -/ @[to_additive "A choice of limit cone for a functor into `CommGroup`. (Generally, you'll just want to use `limit F`.)"] def limit_cone (F : J ⥤ CommGroup) : cone F := lift_limit (limit.is_limit (F ⋙ (forget₂ CommGroup Group.{u}))) /-- The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.) -/ @[to_additive "The chosen cone is a limit cone. (Generally, you'll just wantto use `limit.cone F`.)"] def limit_cone_is_limit (F : J ⥤ CommGroup) : is_limit (limit_cone F) := lifted_limit_is_limit _ /-- The category of commutative groups has all limits. -/ @[to_additive] instance has_limits : has_limits CommGroup := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit_of_created F (forget₂ CommGroup Group) } } /-- The forgetful functor from commutative groups to groups preserves all limits. (That is, the underlying group could have been computed instead as limits in the category of groups.) -/ @[to_additive AddCommGroup.forget₂_AddGroup_preserves_limits] instance forget₂_Group_preserves_limits : preserves_limits (forget₂ CommGroup Group) := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ F, by apply_instance } } /-- An auxiliary declaration to speed up typechecking. -/ @[to_additive AddCommGroup.forget₂_AddCommMon_preserves_limits_aux "An auxiliary declaration to speed up typechecking."] def forget₂_CommMon_preserves_limits_aux (F : J ⥤ CommGroup) : is_limit ((forget₂ CommGroup CommMon).map_cone (limit_cone F)) := CommMon.limit_cone_is_limit (F ⋙ forget₂ CommGroup CommMon) /-- The forgetful functor from commutative groups to commutative monoids preserves all limits. (That is, the underlying commutative monoids could have been computed instead as limits in the category of commutative monoids.) -/ @[to_additive AddCommGroup.forget₂_AddCommMon_preserves_limits] instance forget₂_CommMon_preserves_limits : preserves_limits (forget₂ CommGroup CommMon) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (forget₂_CommMon_preserves_limits_aux F) } } /-- The forgetful functor from commutative groups to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ @[to_additive AddCommGroup.forget_preserves_limits] instance forget_preserves_limits : preserves_limits (forget CommGroup) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, limits.comp_preserves_limit (forget₂ CommGroup Group) (forget Group) } } end CommGroup namespace AddCommGroup /-- The categorical kernel of a morphism in `AddCommGroup` agrees with the usual group-theoretical kernel. -/ def kernel_iso_ker {G H : AddCommGroup} (f : G ⟶ H) : kernel f ≅ AddCommGroup.of f.ker := { hom := { to_fun := λ g, ⟨kernel.ι f g, begin -- TODO where is this `has_coe_t_aux.coe` coming from? can we prevent it appearing? change (kernel.ι f) g ∈ f.ker, simp [add_monoid_hom.mem_ker], end⟩, map_zero' := by { ext, simp, }, map_add' := λ g g', by { ext, simp, }, }, inv := kernel.lift f (add_subgroup.subtype f.ker) (by tidy), hom_inv_id' := by { apply equalizer.hom_ext _, ext, simp, }, inv_hom_id' := begin apply AddCommGroup.ext, simp only [add_monoid_hom.coe_mk, coe_id, coe_comp], rintro ⟨x, mem⟩, simp, end, }. @[simp] lemma kernel_iso_ker_hom_comp_subtype {G H : AddCommGroup} (f : G ⟶ H) : (kernel_iso_ker f).hom ≫ add_subgroup.subtype f.ker = kernel.ι f := begin ext, simp [kernel_iso_ker], end @[simp] lemma kernel_iso_ker_inv_comp_ι {G H : AddCommGroup} (f : G ⟶ H) : (kernel_iso_ker f).inv ≫ kernel.ι f = add_subgroup.subtype f.ker := begin ext, simp [kernel_iso_ker], end /-- The categorical kernel inclusion for `f : G ⟶ H`, as an object over `G`, agrees with the `subtype` map. -/ @[simps] def kernel_iso_ker_over {G H : AddCommGroup.{u}} (f : G ⟶ H) : over.mk (kernel.ι f) ≅ @over.mk _ _ G (AddCommGroup.of f.ker) (add_subgroup.subtype f.ker) := over.iso_mk (kernel_iso_ker f) (by simp) end AddCommGroup
ab2685e306270787998a4991287095a3357da507
2fbe653e4bc441efde5e5d250566e65538709888
/src/topology/uniform_space/basic.lean
98a2bfc629713fc460d0f1bb78909c5920892f3e
[ "Apache-2.0" ]
permissive
aceg00/mathlib
5e15e79a8af87ff7eb8c17e2629c442ef24e746b
8786ea6d6d46d6969ac9a869eb818bf100802882
refs/heads/master
1,649,202,698,930
1,580,924,783,000
1,580,924,783,000
149,197,272
0
0
Apache-2.0
1,537,224,208,000
1,537,224,207,000
null
UTF-8
Lean
false
false
42,046
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot Theory of uniform spaces. Uniform spaces are a generalization of metric spaces and topological groups. Many concepts directly generalize to uniform spaces, e.g. * completeness * extension of uniform continuous functions to complete spaces * uniform contiunuity & embedding * totally bounded * totally bounded ∧ complete → compact The central concept of uniform spaces is its uniformity: a filter relating two elements of the space. This filter is reflexive, symmetric and transitive. So a set (i.e. a relation) in this filter represents a 'distance': it is reflexive, symmetric and the uniformity contains a set for which the `triangular` rule holds. The formalization is mostly based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import order.filter.basic order.filter.lift topology.separation open set lattice filter classical open_locale classical topological_space set_option eqn_compiler.zeta true universes u section variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*} /-- The identity relation, or the graph of the identity function -/ def id_rel {α : Type*} := {p : α × α | p.1 = p.2} @[simp] theorem mem_id_rel {a b : α} : (a, b) ∈ @id_rel α ↔ a = b := iff.rfl @[simp] theorem id_rel_subset {s : set (α × α)} : id_rel ⊆ s ↔ ∀ a, (a, a) ∈ s := by simp [subset_def]; exact forall_congr (λ a, by simp) /-- The composition of relations -/ def comp_rel {α : Type u} (r₁ r₂ : set (α×α)) := {p : α × α | ∃z:α, (p.1, z) ∈ r₁ ∧ (z, p.2) ∈ r₂} @[simp] theorem mem_comp_rel {r₁ r₂ : set (α×α)} {x y : α} : (x, y) ∈ comp_rel r₁ r₂ ↔ ∃ z, (x, z) ∈ r₁ ∧ (z, y) ∈ r₂ := iff.rfl @[simp] theorem swap_id_rel : prod.swap '' id_rel = @id_rel α := set.ext $ assume ⟨a, b⟩, by simp [image_swap_eq_preimage_swap]; exact eq_comm theorem monotone_comp_rel [preorder β] {f g : β → set (α×α)} (hf : monotone f) (hg : monotone g) : monotone (λx, comp_rel (f x) (g x)) := assume a b h p ⟨z, h₁, h₂⟩, ⟨z, hf h h₁, hg h h₂⟩ lemma prod_mk_mem_comp_rel {a b c : α} {s t : set (α×α)} (h₁ : (a, c) ∈ s) (h₂ : (c, b) ∈ t) : (a, b) ∈ comp_rel s t := ⟨c, h₁, h₂⟩ @[simp] lemma id_comp_rel {r : set (α×α)} : comp_rel id_rel r = r := set.ext $ assume ⟨a, b⟩, by simp lemma comp_rel_assoc {r s t : set (α×α)} : comp_rel (comp_rel r s) t = comp_rel r (comp_rel s t) := by ext p; cases p; simp only [mem_comp_rel]; tauto /-- This core description of a uniform space is outside of the type class hierarchy. It is useful for constructions of uniform spaces, when the topology is derived from the uniform space. -/ structure uniform_space.core (α : Type u) := (uniformity : filter (α × α)) (refl : principal id_rel ≤ uniformity) (symm : tendsto prod.swap uniformity uniformity) (comp : uniformity.lift' (λs, comp_rel s s) ≤ uniformity) def uniform_space.core.mk' {α : Type u} (U : filter (α × α)) (refl : ∀ (r ∈ U) x, (x, x) ∈ r) (symm : ∀ r ∈ U, {p | prod.swap p ∈ r} ∈ U) (comp : ∀ r ∈ U, ∃ t ∈ U, comp_rel t t ⊆ r) : uniform_space.core α := ⟨U, λ r ru, id_rel_subset.2 (refl _ ru), symm, begin intros r ru, rw [mem_lift'_sets], exact comp _ ru, apply monotone_comp_rel; exact monotone_id, end⟩ /-- A uniform space generates a topological space -/ def uniform_space.core.to_topological_space {α : Type u} (u : uniform_space.core α) : topological_space α := { is_open := λs, ∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ u.uniformity, is_open_univ := by simp; intro; exact univ_mem_sets, is_open_inter := assume s t hs ht x ⟨xs, xt⟩, by filter_upwards [hs x xs, ht x xt]; simp {contextual := tt}, is_open_sUnion := assume s hs x ⟨t, ts, xt⟩, by filter_upwards [hs t ts x xt] assume p ph h, ⟨t, ts, ph h⟩ } lemma uniform_space.core_eq : ∀{u₁ u₂ : uniform_space.core α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | ⟨u₁, _, _, _⟩ ⟨u₂, _, _, _⟩ h := have u₁ = u₂, from h, by simp [*] section prio set_option default_priority 100 -- see Note [default priority] /-- A uniform space is a generalization of the "uniform" topological aspects of a metric space. It consists of a filter on `α × α` called the "uniformity", which satisfies properties analogous to the reflexivity, symmetry, and triangle properties of a metric. A metric space has a natural uniformity, and a uniform space has a natural topology. A topological group also has a natural uniformity, even when it is not metrizable. -/ class uniform_space (α : Type u) extends topological_space α, uniform_space.core α := (is_open_uniformity : ∀s, is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ uniformity)) end prio @[pattern] def uniform_space.mk' {α} (t : topological_space α) (c : uniform_space.core α) (is_open_uniformity : ∀s:set α, t.is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ c.uniformity)) : uniform_space α := ⟨c, is_open_uniformity⟩ def uniform_space.of_core {α : Type u} (u : uniform_space.core α) : uniform_space α := { to_core := u, to_topological_space := u.to_topological_space, is_open_uniformity := assume a, iff.rfl } def uniform_space.of_core_eq {α : Type u} (u : uniform_space.core α) (t : topological_space α) (h : t = u.to_topological_space) : uniform_space α := { to_core := u, to_topological_space := t, is_open_uniformity := assume a, h.symm ▸ iff.rfl } lemma uniform_space.to_core_to_topological_space (u : uniform_space α) : u.to_core.to_topological_space = u.to_topological_space := topological_space_eq $ funext $ assume s, by rw [uniform_space.core.to_topological_space, uniform_space.is_open_uniformity] @[ext] lemma uniform_space_eq : ∀{u₁ u₂ : uniform_space α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | (uniform_space.mk' t₁ u₁ o₁) (uniform_space.mk' t₂ u₂ o₂) h := have u₁ = u₂, from uniform_space.core_eq h, have t₁ = t₂, from topological_space_eq $ funext $ assume s, by rw [o₁, o₂]; simp [this], by simp [*] lemma uniform_space.of_core_eq_to_core (u : uniform_space α) (t : topological_space α) (h : t = u.to_core.to_topological_space) : uniform_space.of_core_eq u.to_core t h = u := uniform_space_eq rfl section uniform_space variables [uniform_space α] /-- The uniformity is a filter on α × α (inferred from an ambient uniform space structure on α). -/ def uniformity (α : Type u) [uniform_space α] : filter (α × α) := (@uniform_space.to_core α _).uniformity localized "notation `𝓤` := uniformity" in uniformity lemma is_open_uniformity {s : set α} : is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ 𝓤 α) := uniform_space.is_open_uniformity s lemma refl_le_uniformity : principal id_rel ≤ 𝓤 α := (@uniform_space.to_core α _).refl lemma refl_mem_uniformity {x : α} {s : set (α × α)} (h : s ∈ 𝓤 α) : (x, x) ∈ s := refl_le_uniformity h rfl lemma symm_le_uniformity : map (@prod.swap α α) (𝓤 _) ≤ (𝓤 _) := (@uniform_space.to_core α _).symm lemma comp_le_uniformity : (𝓤 α).lift' (λs:set (α×α), comp_rel s s) ≤ 𝓤 α := (@uniform_space.to_core α _).comp lemma tendsto_swap_uniformity : tendsto (@prod.swap α α) (𝓤 α) (𝓤 α) := symm_le_uniformity lemma tendsto_const_uniformity {a : α} {f : filter β} : tendsto (λ _, (a, a)) f (𝓤 α) := assume s hs, show {x | (a, a) ∈ s} ∈ f, from univ_mem_sets' $ assume b, refl_mem_uniformity hs lemma comp_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, comp_rel t t ⊆ s := have s ∈ (𝓤 α).lift' (λt:set (α×α), comp_rel t t), from comp_le_uniformity hs, (mem_lift'_sets $ monotone_comp_rel monotone_id monotone_id).mp this lemma symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, (∀a b, (a, b) ∈ t → (b, a) ∈ t) ∧ t ⊆ s := have preimage prod.swap s ∈ 𝓤 α, from symm_le_uniformity hs, ⟨s ∩ preimage prod.swap s, inter_mem_sets hs this, assume a b ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, inter_subset_left _ _⟩ lemma comp_symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, (∀{a b}, (a, b) ∈ t → (b, a) ∈ t) ∧ comp_rel t t ⊆ s := let ⟨t, ht₁, ht₂⟩ := comp_mem_uniformity_sets hs in let ⟨t', ht', ht'₁, ht'₂⟩ := symm_of_uniformity ht₁ in ⟨t', ht', ht'₁, subset.trans (monotone_comp_rel monotone_id monotone_id ht'₂) ht₂⟩ lemma uniformity_le_symm : 𝓤 α ≤ (@prod.swap α α) <$> 𝓤 α := by rw [map_swap_eq_comap_swap]; from map_le_iff_le_comap.1 tendsto_swap_uniformity lemma uniformity_eq_symm : 𝓤 α = (@prod.swap α α) <$> 𝓤 α := le_antisymm uniformity_le_symm symm_le_uniformity theorem uniformity_lift_le_swap {g : set (α×α) → filter β} {f : filter β} (hg : monotone g) (h : (𝓤 α).lift (λs, g (preimage prod.swap s)) ≤ f) : (𝓤 α).lift g ≤ f := calc (𝓤 α).lift g ≤ (filter.map (@prod.swap α α) $ 𝓤 α).lift g : lift_mono uniformity_le_symm (le_refl _) ... ≤ _ : by rw [map_lift_eq2 hg, image_swap_eq_preimage_swap]; exact h lemma uniformity_lift_le_comp {f : set (α×α) → filter β} (h : monotone f) : (𝓤 α).lift (λs, f (comp_rel s s)) ≤ (𝓤 α).lift f := calc (𝓤 α).lift (λs, f (comp_rel s s)) = ((𝓤 α).lift' (λs:set (α×α), comp_rel s s)).lift f : begin rw [lift_lift'_assoc], exact monotone_comp_rel monotone_id monotone_id, exact h end ... ≤ (𝓤 α).lift f : lift_mono comp_le_uniformity (le_refl _) lemma comp_le_uniformity3 : (𝓤 α).lift' (λs:set (α×α), comp_rel s (comp_rel s s)) ≤ (𝓤 α) := calc (𝓤 α).lift' (λd, comp_rel d (comp_rel d d)) = (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), comp_rel s (comp_rel t t))) : begin rw [lift_lift'_same_eq_lift'], exact (assume x, monotone_comp_rel monotone_const $ monotone_comp_rel monotone_id monotone_id), exact (assume x, monotone_comp_rel monotone_id monotone_const), end ... ≤ (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), comp_rel s t)) : lift_mono' $ assume s hs, @uniformity_lift_le_comp α _ _ (principal ∘ comp_rel s) $ monotone_principal.comp (monotone_comp_rel monotone_const monotone_id) ... = (𝓤 α).lift' (λs:set(α×α), comp_rel s s) : lift_lift'_same_eq_lift' (assume s, monotone_comp_rel monotone_const monotone_id) (assume s, monotone_comp_rel monotone_id monotone_const) ... ≤ (𝓤 α) : comp_le_uniformity lemma mem_nhds_uniformity_iff {x : α} {s : set α} : s ∈ 𝓝 x ↔ {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α := ⟨ begin simp only [mem_nhds_sets_iff, is_open_uniformity, and_imp, exists_imp_distrib], exact assume t ts ht xt, by filter_upwards [ht x xt] assume ⟨x', y⟩ h eq, ts $ h eq end, assume hs, mem_nhds_sets_iff.mpr ⟨{x | {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α}, assume x' hx', refl_mem_uniformity hx' rfl, is_open_uniformity.mpr $ assume x' hx', let ⟨t, ht, tr⟩ := comp_mem_uniformity_sets hx' in by filter_upwards [ht] assume ⟨a, b⟩ hp' (hax' : a = x'), by filter_upwards [ht] assume ⟨a, b'⟩ hp'' (hab : a = b), have hp : (x', b) ∈ t, from hax' ▸ hp', have (b, b') ∈ t, from hab ▸ hp'', have (x', b') ∈ comp_rel t t, from ⟨b, hp, this⟩, show b' ∈ s, from tr this rfl, hs⟩⟩ lemma nhds_eq_comap_uniformity {x : α} : 𝓝 x = (𝓤 α).comap (prod.mk x) := by ext s; rw [mem_nhds_uniformity_iff, mem_comap_sets]; from iff.intro (assume hs, ⟨_, hs, assume x hx, hx rfl⟩) (assume ⟨t, h, ht⟩, (𝓤 α).sets_of_superset h $ assume ⟨p₁, p₂⟩ hp (h : p₁ = x), ht $ by simp [h.symm, hp]) lemma nhds_eq_uniformity {x : α} : 𝓝 x = (𝓤 α).lift' (λs:set (α×α), {y | (x, y) ∈ s}) := begin ext s, rw [mem_lift'_sets], tactic.swap, apply monotone_preimage, simp [mem_nhds_uniformity_iff], exact ⟨assume h, ⟨_, h, assume y h, h rfl⟩, assume ⟨t, h₁, h₂⟩, (𝓤 α).sets_of_superset h₁ $ assume ⟨x', y⟩ hp (eq : x' = x), h₂ $ show (x, y) ∈ t, from eq ▸ hp⟩ end lemma mem_nhds_left (x : α) {s : set (α×α)} (h : s ∈ 𝓤 α) : {y : α | (x, y) ∈ s} ∈ 𝓝 x := have 𝓝 x ≤ principal {y : α | (x, y) ∈ s}, by rw [nhds_eq_uniformity]; exact infi_le_of_le s (infi_le _ h), by simp at this; assumption lemma mem_nhds_right (y : α) {s : set (α×α)} (h : s ∈ 𝓤 α) : {x : α | (x, y) ∈ s} ∈ 𝓝 y := mem_nhds_left _ (symm_le_uniformity h) lemma tendsto_right_nhds_uniformity {a : α} : tendsto (λa', (a', a)) (𝓝 a) (𝓤 α) := assume s, mem_nhds_right a lemma tendsto_left_nhds_uniformity {a : α} : tendsto (λa', (a, a')) (𝓝 a) (𝓤 α) := assume s, mem_nhds_left a lemma lift_nhds_left {x : α} {g : set α → filter β} (hg : monotone g) : (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) := eq.trans begin rw [nhds_eq_uniformity], exact (filter.lift_assoc $ monotone_principal.comp $ monotone_preimage.comp monotone_preimage ) end (congr_arg _ $ funext $ assume s, filter.lift_principal hg) lemma lift_nhds_right {x : α} {g : set α → filter β} (hg : monotone g) : (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (y, x) ∈ s}) := calc (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : lift_nhds_left hg ... = ((@prod.swap α α) <$> (𝓤 α)).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : by rw [←uniformity_eq_symm] ... = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ image prod.swap s}) : map_lift_eq2 $ hg.comp monotone_preimage ... = _ : by simp [image_swap_eq_preimage_swap] lemma nhds_nhds_eq_uniformity_uniformity_prod {a b : α} : filter.prod (𝓝 a) (𝓝 b) = (𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ t})) := begin rw [prod_def], show (𝓝 a).lift (λs:set α, (𝓝 b).lift (λt:set α, principal (set.prod s t))) = _, rw [lift_nhds_right], apply congr_arg, funext s, rw [lift_nhds_left], refl, exact monotone_principal.comp (monotone_prod monotone_const monotone_id), exact (monotone_lift' monotone_const $ monotone_lam $ assume x, monotone_prod monotone_id monotone_const) end lemma nhds_eq_uniformity_prod {a b : α} : 𝓝 (a, b) = (𝓤 α).lift' (λs:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ s}) := begin rw [nhds_prod_eq, nhds_nhds_eq_uniformity_uniformity_prod, lift_lift'_same_eq_lift'], { intro s, exact monotone_prod monotone_const monotone_preimage }, { intro t, exact monotone_prod monotone_preimage monotone_const } end lemma nhdset_of_mem_uniformity {d : set (α×α)} (s : set (α×α)) (hd : d ∈ 𝓤 α) : ∃(t : set (α×α)), is_open t ∧ s ⊆ t ∧ t ⊆ {p | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} := let cl_d := {p:α×α | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} in have ∀p ∈ s, ∃t ⊆ cl_d, is_open t ∧ p ∈ t, from assume ⟨x, y⟩ hp, mem_nhds_sets_iff.mp $ show cl_d ∈ 𝓝 (x, y), begin rw [nhds_eq_uniformity_prod, mem_lift'_sets], exact ⟨d, hd, assume ⟨a, b⟩ ⟨ha, hb⟩, ⟨x, y, ha, hp, hb⟩⟩, exact monotone_prod monotone_preimage monotone_preimage end, have ∃t:(Π(p:α×α) (h:p ∈ s), set (α×α)), ∀p, ∀h:p ∈ s, t p h ⊆ cl_d ∧ is_open (t p h) ∧ p ∈ t p h, by simp [classical.skolem] at this; simp; assumption, match this with | ⟨t, ht⟩ := ⟨(⋃ p:α×α, ⋃ h : p ∈ s, t p h : set (α×α)), is_open_Union $ assume (p:α×α), is_open_Union $ assume hp, (ht p hp).right.left, assume ⟨a, b⟩ hp, begin simp; exact ⟨a, b, hp, (ht (a,b) hp).right.right⟩ end, Union_subset $ assume p, Union_subset $ assume hp, (ht p hp).left⟩ end lemma closure_eq_inter_uniformity {t : set (α×α)} : closure t = (⋂ d ∈ 𝓤 α, comp_rel d (comp_rel t d)) := set.ext $ assume ⟨a, b⟩, calc (a, b) ∈ closure t ↔ (𝓝 (a, b) ⊓ principal t ≠ ⊥) : by simp [closure_eq_nhds] ... ↔ (((@prod.swap α α) <$> 𝓤 α).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ principal t ≠ ⊥) : by rw [←uniformity_eq_symm, nhds_eq_uniformity_prod] ... ↔ ((map (@prod.swap α α) (𝓤 α)).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ principal t ≠ ⊥) : by refl ... ↔ ((𝓤 α).lift' (λ (s : set (α × α)), set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s}) ⊓ principal t ≠ ⊥) : begin rw [map_lift'_eq2], simp [image_swap_eq_preimage_swap, function.comp], exact monotone_prod monotone_preimage monotone_preimage end ... ↔ (∀s ∈ 𝓤 α, (set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s} ∩ t).nonempty) : begin rw [lift'_inf_principal_eq, lift'_ne_bot_iff], exact monotone_inter (monotone_prod monotone_preimage monotone_preimage) monotone_const end ... ↔ (∀ s ∈ 𝓤 α, (a, b) ∈ comp_rel s (comp_rel t s)) : forall_congr $ assume s, forall_congr $ assume hs, ⟨assume ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩, ⟨x, hx, y, hxyt, hy⟩, assume ⟨x, hx, y, hxyt, hy⟩, ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩⟩ ... ↔ _ : by simp lemma uniformity_eq_uniformity_closure : 𝓤 α = (𝓤 α).lift' closure := le_antisymm (le_infi $ assume s, le_infi $ assume hs, by simp; filter_upwards [hs] subset_closure) (calc (𝓤 α).lift' closure ≤ (𝓤 α).lift' (λd, comp_rel d (comp_rel d d)) : lift'_mono' (by intros s hs; rw [closure_eq_inter_uniformity]; exact bInter_subset_of_mem hs) ... ≤ (𝓤 α) : comp_le_uniformity3) lemma uniformity_eq_uniformity_interior : 𝓤 α = (𝓤 α).lift' interior := le_antisymm (le_infi $ assume d, le_infi $ assume hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in let ⟨t, ht, hst, ht_comp⟩ := nhdset_of_mem_uniformity s hs in have s ⊆ interior d, from calc s ⊆ t : hst ... ⊆ interior d : (subset_interior_iff_subset_of_open ht).mpr $ assume x, assume : x ∈ t, let ⟨x, y, h₁, h₂, h₃⟩ := ht_comp this in hs_comp ⟨x, h₁, y, h₂, h₃⟩, have interior d ∈ 𝓤 α, by filter_upwards [hs] this, by simp [this]) (assume s hs, ((𝓤 α).lift' interior).sets_of_superset (mem_lift' hs) interior_subset) lemma interior_mem_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : interior s ∈ 𝓤 α := by rw [uniformity_eq_uniformity_interior]; exact mem_lift' hs lemma mem_uniformity_is_closed {s : set (α×α)} (h : s ∈ 𝓤 α) : ∃t ∈ 𝓤 α, is_closed t ∧ t ⊆ s := have s ∈ (𝓤 α).lift' closure, by rwa [uniformity_eq_uniformity_closure] at h, have ∃ t ∈ 𝓤 α, closure t ⊆ s, by rwa [mem_lift'_sets] at this; apply closure_mono, let ⟨t, ht, hst⟩ := this in ⟨closure t, (𝓤 α).sets_of_superset ht subset_closure, is_closed_closure, hst⟩ /- uniform continuity -/ def uniform_continuous [uniform_space β] (f : α → β) := tendsto (λx:α×α, (f x.1, f x.2)) (𝓤 α) (𝓤 β) theorem uniform_continuous_def [uniform_space β] {f : α → β} : uniform_continuous f ↔ ∀ r ∈ 𝓤 β, {x : α × α | (f x.1, f x.2) ∈ r} ∈ 𝓤 α := iff.rfl lemma uniform_continuous_of_const [uniform_space β] {c : α → β} (h : ∀a b, c a = c b) : uniform_continuous c := have (λ (x : α × α), (c (x.fst), c (x.snd))) ⁻¹' id_rel = univ, from eq_univ_iff_forall.2 $ assume ⟨a, b⟩, h a b, le_trans (map_le_iff_le_comap.2 $ by simp [comap_principal, this, univ_mem_sets]) refl_le_uniformity lemma uniform_continuous_id : uniform_continuous (@id α) := by simp [uniform_continuous]; exact tendsto_id lemma uniform_continuous_const [uniform_space β] {b : β} : uniform_continuous (λa:α, b) := @tendsto_const_uniformity _ _ _ b (𝓤 α) lemma uniform_continuous.comp [uniform_space β] [uniform_space γ] {g : β → γ} {f : α → β} (hg : uniform_continuous g) (hf : uniform_continuous f) : uniform_continuous (g ∘ f) := hg.comp hf end uniform_space end open_locale uniformity section constructions variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*} instance : partial_order (uniform_space α) := { le := λt s, t.uniformity ≤ s.uniformity, le_antisymm := assume t s h₁ h₂, uniform_space_eq $ le_antisymm h₁ h₂, le_refl := assume t, le_refl _, le_trans := assume a b c h₁ h₂, le_trans h₁ h₂ } instance : has_Inf (uniform_space α) := ⟨assume s, uniform_space.of_core { uniformity := (⨅u∈s, @uniformity α u), refl := le_infi $ assume u, le_infi $ assume hu, u.refl, symm := le_infi $ assume u, le_infi $ assume hu, le_trans (map_mono $ infi_le_of_le _ $ infi_le _ hu) u.symm, comp := le_infi $ assume u, le_infi $ assume hu, le_trans (lift'_mono (infi_le_of_le _ $ infi_le _ hu) $ le_refl _) u.comp }⟩ private lemma Inf_le {tt : set (uniform_space α)} {t : uniform_space α} (h : t ∈ tt) : Inf tt ≤ t := show (⨅u∈tt, @uniformity α u) ≤ t.uniformity, from infi_le_of_le t $ infi_le _ h private lemma le_Inf {tt : set (uniform_space α)} {t : uniform_space α} (h : ∀t'∈tt, t ≤ t') : t ≤ Inf tt := show t.uniformity ≤ (⨅u∈tt, @uniformity α u), from le_infi $ assume t', le_infi $ assume ht', h t' ht' instance : has_top (uniform_space α) := ⟨uniform_space.of_core { uniformity := ⊤, refl := le_top, symm := le_top, comp := le_top }⟩ instance : has_bot (uniform_space α) := ⟨{ to_topological_space := ⊥, uniformity := principal id_rel, refl := le_refl _, symm := by simp [tendsto]; apply subset.refl, comp := begin rw [lift'_principal], {simp}, exact monotone_comp_rel monotone_id monotone_id end, is_open_uniformity := assume s, by simp [is_open_fold, subset_def, id_rel] {contextual := tt } } ⟩ instance : complete_lattice (uniform_space α) := { sup := λa b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf (λ _ ⟨h, _⟩, h), le_sup_right := λ a b, le_Inf (λ _ ⟨_, h⟩, h), sup_le := λ a b c h₁ h₂, Inf_le ⟨h₁, h₂⟩, inf := λ a b, Inf {a, b}, le_inf := λ a b c h₁ h₂, le_Inf (λ u h, by { cases h, exact h.symm ▸ h₂, exact (mem_singleton_iff.1 h).symm ▸ h₁ }), inf_le_left := λ a b, Inf_le (by simp), inf_le_right := λ a b, Inf_le (by simp), top := ⊤, le_top := λ a, show a.uniformity ≤ ⊤, from le_top, bot := ⊥, bot_le := λ u, u.refl, Sup := λ tt, Inf {t | ∀ t' ∈ tt, t' ≤ t}, le_Sup := λ s u h, le_Inf (λ u' h', h' u h), Sup_le := λ s u h, Inf_le h, Inf := Inf, le_Inf := λ s a hs, le_Inf hs, Inf_le := λ s a ha, Inf_le ha, ..uniform_space.partial_order } lemma infi_uniformity {ι : Sort*} {u : ι → uniform_space α} : (infi u).uniformity = (⨅i, (u i).uniformity) := show (⨅a (h : ∃i:ι, u i = a), a.uniformity) = _, from le_antisymm (le_infi $ assume i, infi_le_of_le (u i) $ infi_le _ ⟨i, rfl⟩) (le_infi $ assume a, le_infi $ assume ⟨i, (ha : u i = a)⟩, ha ▸ infi_le _ _) lemma inf_uniformity {u v : uniform_space α} : (u ⊓ v).uniformity = u.uniformity ⊓ v.uniformity := have (u ⊓ v) = (⨅i (h : i = u ∨ i = v), i), by simp [infi_or, infi_inf_eq], calc (u ⊓ v).uniformity = ((⨅i (h : i = u ∨ i = v), i) : uniform_space α).uniformity : by rw [this] ... = _ : by simp [infi_uniformity, infi_or, infi_inf_eq] instance inhabited_uniform_space : inhabited (uniform_space α) := ⟨⊥⟩ instance inhabited_uniform_space_core : inhabited (uniform_space.core α) := ⟨@uniform_space.to_core _ (default _)⟩ /-- Given `f : α → β` and a uniformity `u` on `β`, the inverse image of `u` under `f` is the inverse image in the filter sense of the induced function `α × α → β × β`. -/ def uniform_space.comap (f : α → β) (u : uniform_space β) : uniform_space α := { uniformity := u.uniformity.comap (λp:α×α, (f p.1, f p.2)), to_topological_space := u.to_topological_space.induced f, refl := le_trans (by simp; exact assume ⟨a, b⟩ (h : a = b), h ▸ rfl) (comap_mono u.refl), symm := by simp [tendsto_comap_iff, prod.swap, (∘)]; exact tendsto_swap_uniformity.comp tendsto_comap, comp := le_trans begin rw [comap_lift'_eq, comap_lift'_eq2], exact (lift'_mono' $ assume s hs ⟨a₁, a₂⟩ ⟨x, h₁, h₂⟩, ⟨f x, h₁, h₂⟩), repeat { exact monotone_comp_rel monotone_id monotone_id } end (comap_mono u.comp), is_open_uniformity := λ s, begin change (@is_open α (u.to_topological_space.induced f) s ↔ _), simp [is_open_iff_nhds, nhds_induced, mem_nhds_uniformity_iff, filter.comap, and_comm], refine ball_congr (λ x hx, ⟨_, _⟩), { rintro ⟨t, hts, ht⟩, refine ⟨_, ht, _⟩, rintro ⟨x₁, x₂⟩ h rfl, exact hts (h rfl) }, { rintro ⟨t, ht, hts⟩, exact ⟨{y | (f x, y) ∈ t}, λ y hy, @hts (x, y) hy rfl, mem_nhds_uniformity_iff.1 $ mem_nhds_left _ ht⟩ } end } lemma uniform_space_comap_id {α : Type*} : uniform_space.comap (id : α → α) = id := by ext u ; dsimp [uniform_space.comap] ; rw [prod.id_prod, filter.comap_id] lemma uniform_space.comap_comap_comp {α β γ} [uγ : uniform_space γ] {f : α → β} {g : β → γ} : uniform_space.comap (g ∘ f) uγ = uniform_space.comap f (uniform_space.comap g uγ) := by ext ; dsimp [uniform_space.comap] ; rw filter.comap_comap_comp lemma uniform_continuous_iff {α β} [uα : uniform_space α] [uβ : uniform_space β] {f : α → β} : uniform_continuous f ↔ uα ≤ uβ.comap f := filter.map_le_iff_le_comap lemma uniform_continuous_comap {f : α → β} [u : uniform_space β] : @uniform_continuous α β (uniform_space.comap f u) u f := tendsto_comap theorem to_topological_space_comap {f : α → β} {u : uniform_space β} : @uniform_space.to_topological_space _ (uniform_space.comap f u) = topological_space.induced f (@uniform_space.to_topological_space β u) := rfl lemma uniform_continuous_comap' {f : γ → β} {g : α → γ} [v : uniform_space β] [u : uniform_space α] (h : uniform_continuous (f ∘ g)) : @uniform_continuous α γ u (uniform_space.comap f v) g := tendsto_comap_iff.2 h lemma to_topological_space_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) : @uniform_space.to_topological_space _ u₁ ≤ @uniform_space.to_topological_space _ u₂ := le_of_nhds_le_nhds $ assume a, by rw [@nhds_eq_uniformity α u₁ a, @nhds_eq_uniformity α u₂ a]; exact (lift'_mono h $ le_refl _) lemma uniform_continuous.continuous [uniform_space α] [uniform_space β] {f : α → β} (hf : uniform_continuous f) : continuous f := continuous_iff_le_induced.mpr $ to_topological_space_mono $ uniform_continuous_iff.1 hf lemma to_topological_space_bot : @uniform_space.to_topological_space α ⊥ = ⊥ := rfl lemma to_topological_space_top : @uniform_space.to_topological_space α ⊤ = ⊤ := top_unique $ assume s hs, s.eq_empty_or_nonempty.elim (assume : s = ∅, this.symm ▸ @is_open_empty _ ⊤) (assume ⟨x, hx⟩, have s = univ, from top_unique $ assume y hy, hs x hx (x, y) rfl, this.symm ▸ @is_open_univ _ ⊤) lemma to_topological_space_infi {ι : Sort*} {u : ι → uniform_space α} : (infi u).to_topological_space = ⨅i, (u i).to_topological_space := classical.by_cases (assume h : nonempty ι, eq_of_nhds_eq_nhds $ assume a, begin rw [nhds_infi, nhds_eq_uniformity], change (infi u).uniformity.lift' (preimage $ prod.mk a) = _, begin rw [infi_uniformity, lift'_infi], exact (congr_arg _ $ funext $ assume i, (@nhds_eq_uniformity α (u i) a).symm), exact h, exact assume a b, rfl end end) (assume : ¬ nonempty ι, le_antisymm (le_infi $ assume i, to_topological_space_mono $ infi_le _ _) (have infi u = ⊤, from top_unique $ le_infi $ assume i, (this ⟨i⟩).elim, have @uniform_space.to_topological_space _ (infi u) = ⊤, from this.symm ▸ to_topological_space_top, this.symm ▸ le_top)) lemma to_topological_space_Inf {s : set (uniform_space α)} : (Inf s).to_topological_space = (⨅i∈s, @uniform_space.to_topological_space α i) := begin rw [Inf_eq_infi, to_topological_space_infi], apply congr rfl, funext x, exact to_topological_space_infi end lemma to_topological_space_inf {u v : uniform_space α} : (u ⊓ v).to_topological_space = u.to_topological_space ⊓ v.to_topological_space := by rw [to_topological_space_Inf, infi_pair] instance : uniform_space empty := ⊥ instance : uniform_space unit := ⊥ instance : uniform_space bool := ⊥ instance : uniform_space ℕ := ⊥ instance : uniform_space ℤ := ⊥ instance {p : α → Prop} [t : uniform_space α] : uniform_space (subtype p) := uniform_space.comap subtype.val t lemma uniformity_subtype {p : α → Prop} [t : uniform_space α] : 𝓤 (subtype p) = comap (λq:subtype p × subtype p, (q.1.1, q.2.1)) (𝓤 α) := rfl lemma uniform_continuous_subtype_val {p : α → Prop} [uniform_space α] : uniform_continuous (subtype.val : {a : α // p a} → α) := uniform_continuous_comap lemma uniform_continuous_subtype_mk {p : α → Prop} [uniform_space α] [uniform_space β] {f : β → α} (hf : uniform_continuous f) (h : ∀x, p (f x)) : uniform_continuous (λx, ⟨f x, h x⟩ : β → subtype p) := uniform_continuous_comap' hf lemma tendsto_of_uniform_continuous_subtype [uniform_space α] [uniform_space β] {f : α → β} {s : set α} {a : α} (hf : uniform_continuous (λx:s, f x.val)) (ha : s ∈ 𝓝 a) : tendsto f (𝓝 a) (𝓝 (f a)) := by rw [(@map_nhds_subtype_val_eq α _ s a (mem_of_nhds ha) ha).symm]; exact tendsto_map' (continuous_iff_continuous_at.mp hf.continuous _) section prod /- a similar product space is possible on the function space (uniformity of pointwise convergence), but we want to have the uniformity of uniform convergence on function spaces -/ instance [u₁ : uniform_space α] [u₂ : uniform_space β] : uniform_space (α × β) := uniform_space.of_core_eq (u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_core prod.topological_space (calc prod.topological_space = (u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_topological_space : by rw [to_topological_space_inf, to_topological_space_comap, to_topological_space_comap]; refl ... = _ : by rw [uniform_space.to_core_to_topological_space]) theorem uniformity_prod [uniform_space α] [uniform_space β] : 𝓤 (α × β) = (𝓤 α).comap (λp:(α × β) × α × β, (p.1.1, p.2.1)) ⊓ (𝓤 β).comap (λp:(α × β) × α × β, (p.1.2, p.2.2)) := inf_uniformity lemma uniformity_prod_eq_prod [uniform_space α] [uniform_space β] : 𝓤 (α×β) = map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) (filter.prod (𝓤 α) (𝓤 β)) := have map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) = comap (λp:(α×β)×(α×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))), from funext $ assume f, map_eq_comap_of_inverse (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl) (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl), by rw [this, uniformity_prod, filter.prod, comap_inf, comap_comap_comp, comap_comap_comp] lemma mem_map_sets_iff' {α : Type*} {β : Type*} {f : filter α} {m : α → β} {t : set β} : t ∈ (map m f).sets ↔ (∃s∈f, m '' s ⊆ t) := mem_map_sets_iff lemma mem_uniformity_of_uniform_continuous_invariant [uniform_space α] {s:set (α×α)} {f : α → α → α} (hf : uniform_continuous (λp:α×α, f p.1 p.2)) (hs : s ∈ 𝓤 α) : ∃u∈𝓤 α, ∀a b c, (a, b) ∈ u → (f a c, f b c) ∈ s := begin rw [uniform_continuous, uniformity_prod_eq_prod, tendsto_map'_iff, (∘)] at hf, rcases mem_map_sets_iff'.1 (hf hs) with ⟨t, ht, hts⟩, clear hf, rcases mem_prod_iff.1 ht with ⟨u, hu, v, hv, huvt⟩, clear ht, refine ⟨u, hu, assume a b c hab, hts $ (mem_image _ _ _).2 ⟨⟨⟨a, b⟩, ⟨c, c⟩⟩, huvt ⟨_, _⟩, _⟩⟩, exact hab, exact refl_mem_uniformity hv, refl end lemma mem_uniform_prod [t₁ : uniform_space α] [t₂ : uniform_space β] {a : set (α × α)} {b : set (β × β)} (ha : a ∈ 𝓤 α) (hb : b ∈ 𝓤 β) : {p:(α×β)×(α×β) | (p.1.1, p.2.1) ∈ a ∧ (p.1.2, p.2.2) ∈ b } ∈ (@uniformity (α × β) _) := by rw [uniformity_prod]; exact inter_mem_inf_sets (preimage_mem_comap ha) (preimage_mem_comap hb) lemma tendsto_prod_uniformity_fst [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.1, p.2.1)) (𝓤 (α × β)) (𝓤 α) := le_trans (map_mono (@inf_le_left (uniform_space (α×β)) _ _ _)) map_comap_le lemma tendsto_prod_uniformity_snd [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.2, p.2.2)) (𝓤 (α × β)) (𝓤 β) := le_trans (map_mono (@inf_le_right (uniform_space (α×β)) _ _ _)) map_comap_le lemma uniform_continuous_fst [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.1) := tendsto_prod_uniformity_fst lemma uniform_continuous_snd [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.2) := tendsto_prod_uniformity_snd variables [uniform_space α] [uniform_space β] [uniform_space γ] lemma uniform_continuous.prod_mk {f₁ : α → β} {f₂ : α → γ} (h₁ : uniform_continuous f₁) (h₂ : uniform_continuous f₂) : uniform_continuous (λa, (f₁ a, f₂ a)) := by rw [uniform_continuous, uniformity_prod]; exact tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma uniform_continuous.prod_mk_left {f : α × β → γ} (h : uniform_continuous f) (b) : uniform_continuous (λ a, f (a,b)) := h.comp (uniform_continuous_id.prod_mk uniform_continuous_const) lemma uniform_continuous.prod_mk_right {f : α × β → γ} (h : uniform_continuous f) (a) : uniform_continuous (λ b, f (a,b)) := h.comp (uniform_continuous_const.prod_mk uniform_continuous_id) lemma to_topological_space_prod {α} {β} [u : uniform_space α] [v : uniform_space β] : @uniform_space.to_topological_space (α × β) prod.uniform_space = @prod.topological_space α β u.to_topological_space v.to_topological_space := rfl end prod section open uniform_space function variables [uniform_space α] [uniform_space β] [uniform_space γ] [uniform_space δ] local notation f `∘₂` g := function.bicompr f g def uniform_continuous₂ (f : α → β → γ) := uniform_continuous (uncurry' f) lemma uniform_continuous₂_def (f : α → β → γ) : uniform_continuous₂ f ↔ uniform_continuous (uncurry' f) := iff.rfl lemma uniform_continuous₂_curry (f : α × β → γ) : uniform_continuous₂ (function.curry f) ↔ uniform_continuous f := by rw [←uncurry'_curry f] {occs := occurrences.pos [2]} ; refl lemma uniform_continuous₂.comp {f : α → β → γ} {g : γ → δ} (hg : uniform_continuous g) (hf : uniform_continuous₂ f) : uniform_continuous₂ (g ∘₂ f) := hg.comp hf end lemma to_topological_space_subtype [u : uniform_space α] {p : α → Prop} : @uniform_space.to_topological_space (subtype p) subtype.uniform_space = @subtype.topological_space α p u.to_topological_space := rfl section sum variables [uniform_space α] [uniform_space β] open sum /-- Uniformity on a disjoint union. Entourages of the diagonal in the union are obtained by taking independently an entourage of the diagonal in the first part, and an entourage of the diagonal in the second part. -/ def uniform_space.core.sum : uniform_space.core (α ⊕ β) := uniform_space.core.mk' (map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β)) (λ r ⟨H₁, H₂⟩ x, by cases x; [apply refl_mem_uniformity H₁, apply refl_mem_uniformity H₂]) (λ r ⟨H₁, H₂⟩, ⟨symm_le_uniformity H₁, symm_le_uniformity H₂⟩) (λ r ⟨Hrα, Hrβ⟩, begin rcases comp_mem_uniformity_sets Hrα with ⟨tα, htα, Htα⟩, rcases comp_mem_uniformity_sets Hrβ with ⟨tβ, htβ, Htβ⟩, refine ⟨_, ⟨mem_map_sets_iff.2 ⟨tα, htα, subset_union_left _ _⟩, mem_map_sets_iff.2 ⟨tβ, htβ, subset_union_right _ _⟩⟩, _⟩, rintros ⟨_, _⟩ ⟨z, ⟨⟨a, b⟩, hab, ⟨⟩⟩ | ⟨⟨a, b⟩, hab, ⟨⟩⟩, ⟨⟨_, c⟩, hbc, ⟨⟩⟩ | ⟨⟨_, c⟩, hbc, ⟨⟩⟩⟩, { have A : (a, c) ∈ comp_rel tα tα := ⟨b, hab, hbc⟩, exact Htα A }, { have A : (a, c) ∈ comp_rel tβ tβ := ⟨b, hab, hbc⟩, exact Htβ A } end) /-- The union of an entourage of the diagonal in each set of a disjoint union is again an entourage of the diagonal. -/ lemma union_mem_uniformity_sum {a : set (α × α)} (ha : a ∈ 𝓤 α) {b : set (β × β)} (hb : b ∈ 𝓤 β) : ((λ p : (α × α), (inl p.1, inl p.2)) '' a ∪ (λ p : (β × β), (inr p.1, inr p.2)) '' b) ∈ (@uniform_space.core.sum α β _ _).uniformity := ⟨mem_map_sets_iff.2 ⟨_, ha, subset_union_left _ _⟩, mem_map_sets_iff.2 ⟨_, hb, subset_union_right _ _⟩⟩ /- To prove that the topology defined by the uniform structure on the disjoint union coincides with the disjoint union topology, we need two lemmas saying that open sets can be characterized by the uniform structure -/ lemma uniformity_sum_of_open_aux {s : set (α ⊕ β)} (hs : is_open s) {x : α ⊕ β} (xs : x ∈ s) : { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity := begin cases x, { refine mem_sets_of_superset (union_mem_uniformity_sum (mem_nhds_uniformity_iff.1 (mem_nhds_sets hs.1 xs)) univ_mem_sets) (union_subset _ _); rintro _ ⟨⟨_, b⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, { refine mem_sets_of_superset (union_mem_uniformity_sum univ_mem_sets (mem_nhds_uniformity_iff.1 (mem_nhds_sets hs.2 xs))) (union_subset _ _); rintro _ ⟨⟨a, _⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, end lemma open_of_uniformity_sum_aux {s : set (α ⊕ β)} (hs : ∀x ∈ s, { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity) : is_open s := begin split, { refine (@is_open_iff_mem_nhds α _ _).2 (λ a ha, mem_nhds_uniformity_iff.2 _), rcases mem_map_sets_iff.1 (hs _ ha).1 with ⟨t, ht, st⟩, refine mem_sets_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl }, { refine (@is_open_iff_mem_nhds β _ _).2 (λ b hb, mem_nhds_uniformity_iff.2 _), rcases mem_map_sets_iff.1 (hs _ hb).2 with ⟨t, ht, st⟩, refine mem_sets_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl } end /- We can now define the uniform structure on the disjoint union -/ instance sum.uniform_space : uniform_space (α ⊕ β) := { to_core := uniform_space.core.sum, is_open_uniformity := λ s, ⟨uniformity_sum_of_open_aux, open_of_uniformity_sum_aux⟩ } lemma sum.uniformity : 𝓤 (α ⊕ β) = map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β) := rfl end sum end constructions lemma lebesgue_number_lemma {α : Type u} [uniform_space α] {s : set α} {ι} {c : ι → set α} (hs : compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) : ∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ i, {y | (x, y) ∈ n} ⊆ c i := begin let u := λ n, {x | ∃ i (m ∈ 𝓤 α), {y | (x, y) ∈ comp_rel m n} ⊆ c i}, have hu₁ : ∀ n ∈ 𝓤 α, is_open (u n), { refine λ n hn, is_open_uniformity.2 _, rintro x ⟨i, m, hm, h⟩, rcases comp_mem_uniformity_sets hm with ⟨m', hm', mm'⟩, apply (𝓤 α).sets_of_superset hm', rintros ⟨x, y⟩ hp rfl, refine ⟨i, m', hm', λ z hz, h (monotone_comp_rel monotone_id monotone_const mm' _)⟩, dsimp at hz ⊢, rw comp_rel_assoc, exact ⟨y, hp, hz⟩ }, have hu₂ : s ⊆ ⋃ n ∈ 𝓤 α, u n, { intros x hx, rcases mem_Union.1 (hc₂ hx) with ⟨i, h⟩, rcases comp_mem_uniformity_sets (is_open_uniformity.1 (hc₁ i) x h) with ⟨m', hm', mm'⟩, exact mem_bUnion hm' ⟨i, _, hm', λ y hy, mm' hy rfl⟩ }, rcases hs.elim_finite_subcover_image hu₁ hu₂ with ⟨b, bu, b_fin, b_cover⟩, refine ⟨_, Inter_mem_sets b_fin bu, λ x hx, _⟩, rcases mem_bUnion_iff.1 (b_cover hx) with ⟨n, bn, i, m, hm, h⟩, refine ⟨i, λ y hy, h _⟩, exact prod_mk_mem_comp_rel (refl_mem_uniformity hm) (bInter_subset_of_mem bn hy) end lemma lebesgue_number_lemma_sUnion {α : Type u} [uniform_space α] {s : set α} {c : set (set α)} (hs : compact s) (hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ t ∈ c, ∀ y, (x, y) ∈ n → y ∈ t := by rw sUnion_eq_Union at hc₂; simpa using lebesgue_number_lemma hs (by simpa) hc₂
9b9b59fb4252f21eb7adbbfac7b96ab56b4e99ab
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/algebra/polynomial.lean
3c9f217178406614c311a39fc8f278f8d878616b
[ "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
5,221
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import analysis.normed_space.basic import data.polynomial.algebra_map import data.polynomial.inductions /-! # Polynomials and limits In this file we prove the following lemmas. * `polynomial.continuous_eval₂: `polynomial.eval₂` defines a continuous function. * `polynomial.continuous_aeval: `polynomial.aeval` defines a continuous function; we also prove convenience lemmas `polynomial.continuous_at_aeval`, `polynomial.continuous_within_at_aeval`, `polynomial.continuous_on_aeval`. * `polynomial.continuous`: `polynomial.eval` defines a continuous functions; we also prove convenience lemmas `polynomial.continuous_at`, `polynomial.continuous_within_at`, `polynomial.continuous_on`. * `polynomial.tendsto_norm_at_top`: `λ x, ∥polynomial.eval (z x) p∥` tends to infinity provided that `λ x, ∥z x∥` tends to infinity and `0 < degree p`; * `polynomial.tendsto_abv_eval₂_at_top`, `polynomial.tendsto_abv_at_top`, `polynomial.tendsto_abv_aeval_at_top`: a few versions of the previous statement for `is_absolute_value abv` instead of norm. ## Tags polynomial, continuity -/ open is_absolute_value filter namespace polynomial section topological_ring variables {R S : Type*} [semiring R] [topological_space R] [topological_ring R] (p : polynomial R) @[continuity] protected lemma continuous_eval₂ [semiring S] (p : polynomial S) (f : S →+* R) : continuous (λ x, p.eval₂ f x) := begin dsimp only [eval₂_eq_sum, finsupp.sum], exact continuous_finset_sum _ (λ c hc, continuous_const.mul (continuous_pow _)) end @[continuity] protected lemma continuous : continuous (λ x, p.eval x) := p.continuous_eval₂ _ protected lemma continuous_at {a : R} : continuous_at (λ x, p.eval x) a := p.continuous.continuous_at protected lemma continuous_within_at {s a} : continuous_within_at (λ x, p.eval x) s a := p.continuous.continuous_within_at protected lemma continuous_on {s} : continuous_on (λ x, p.eval x) s := p.continuous.continuous_on end topological_ring section topological_algebra variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] [topological_space A] [topological_ring A] (p : polynomial R) @[continuity] protected lemma continuous_aeval : continuous (λ x : A, aeval x p) := p.continuous_eval₂ _ protected lemma continuous_at_aeval {a : A} : continuous_at (λ x : A, aeval x p) a := p.continuous_aeval.continuous_at protected lemma continuous_within_at_aeval {s a} : continuous_within_at (λ x : A, aeval x p) s a := p.continuous_aeval.continuous_within_at protected lemma continuous_on_aeval {s} : continuous_on (λ x : A, aeval x p) s := p.continuous_aeval.continuous_on end topological_algebra lemma tendsto_abv_eval₂_at_top {R S k α : Type*} [semiring R] [ring S] [linear_ordered_field k] (f : R →+* S) (abv : S → k) [is_absolute_value abv] (p : polynomial R) (hd : 0 < degree p) (hf : f p.leading_coeff ≠ 0) {l : filter α} {z : α → S} (hz : tendsto (abv ∘ z) l at_top) : tendsto (λ x, abv (p.eval₂ f (z x))) l at_top := begin revert hf, refine degree_pos_induction_on p hd _ _ _; clear hd p, { rintros c - hc, rw [leading_coeff_mul_X, leading_coeff_C] at hc, simpa [abv_mul abv] using hz.const_mul_at_top ((abv_pos abv).2 hc) }, { intros p hpd ihp hf, rw [leading_coeff_mul_X] at hf, simpa [abv_mul abv] using (ihp hf).at_top_mul_at_top hz }, { intros p a hd ihp hf, rw [add_comm, leading_coeff_add_of_degree_lt (degree_C_le.trans_lt hd)] at hf, refine tendsto_at_top_of_add_const_right (abv (-f a)) _, refine tendsto_at_top_mono (λ _, abv_add abv _ _) _, simpa using ihp hf } end lemma tendsto_abv_at_top {R k α : Type*} [ring R] [linear_ordered_field k] (abv : R → k) [is_absolute_value abv] (p : polynomial R) (h : 0 < degree p) {l : filter α} {z : α → R} (hz : tendsto (abv ∘ z) l at_top) : tendsto (λ x, abv (p.eval (z x))) l at_top := tendsto_abv_eval₂_at_top _ _ _ h (mt leading_coeff_eq_zero.1 $ ne_zero_of_degree_gt h) hz lemma tendsto_abv_aeval_at_top {R A k α : Type*} [comm_semiring R] [ring A] [algebra R A] [linear_ordered_field k] (abv : A → k) [is_absolute_value abv] (p : polynomial R) (hd : 0 < degree p) (h₀ : algebra_map R A p.leading_coeff ≠ 0) {l : filter α} {z : α → A} (hz : tendsto (abv ∘ z) l at_top) : tendsto (λ x, abv (aeval (z x) p)) l at_top := tendsto_abv_eval₂_at_top _ abv p hd h₀ hz variables {α R : Type*} [normed_ring R] [is_absolute_value (norm : R → ℝ)] lemma tendsto_norm_at_top (p : polynomial R) (h : 0 < degree p) {l : filter α} {z : α → R} (hz : tendsto (λ x, ∥z x∥) l at_top) : tendsto (λ x, ∥p.eval (z x)∥) l at_top := p.tendsto_abv_at_top norm h hz lemma exists_forall_norm_le [proper_space R] (p : polynomial R) : ∃ x, ∀ y, ∥p.eval x∥ ≤ ∥p.eval y∥ := if hp0 : 0 < degree p then p.continuous.norm.exists_forall_le $ p.tendsto_norm_at_top hp0 tendsto_norm_cocompact_at_top else ⟨p.coeff 0, by rw [eq_C_of_degree_le_zero (le_of_not_gt hp0)]; simp⟩ end polynomial
59fba140a6692c9dc3bc8d008b28a306d07834b3
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/measure/ae_disjoint.lean
6b272e35de93b9cee3e7ea86385b6ab1aeb0a7a0
[ "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
4,877
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import measure_theory.measure.measure_space_def /-! # Almost everywhere disjoint sets We say that sets `s` and `t` are `μ`-a.e. disjoint (see `measure_theory.ae_disjoint`) if their intersection has measure zero. This assumption can be used instead of `disjoint` in most theorems in measure theory. -/ open set function namespace measure_theory variables {ι α : Type*} {m : measurable_space α} (μ : measure α) /-- Two sets are said to be `μ`-a.e. disjoint if their intersection has measure zero. -/ def ae_disjoint (s t : set α) := μ (s ∩ t) = 0 variables {μ} {s t u v : set α} /-- If `s : ι → set α` is a countable family of pairwise a.e. disjoint sets, then there exists a family of measurable null sets `t i` such that `s i \ t i` are pairwise disjoint. -/ lemma exists_null_pairwise_disjoint_diff [encodable ι] {s : ι → set α} (hd : pairwise (ae_disjoint μ on s)) : ∃ t : ι → set α, (∀ i, measurable_set (t i)) ∧ (∀ i, μ (t i) = 0) ∧ pairwise (disjoint on (λ i, s i \ t i)) := begin refine ⟨λ i, to_measurable μ (s i ∩ ⋃ j ∈ ({i}ᶜ : set ι), s j), λ i, measurable_set_to_measurable _ _, λ i, _, _⟩, { simp only [measure_to_measurable, inter_Union, measure_bUnion_null_iff (countable_encodable _)], exact λ j hj, hd _ _ (ne.symm hj) }, { simp only [pairwise, disjoint_left, on_fun, mem_diff, not_and, and_imp, not_not], intros i j hne x hi hU hj, replace hU : x ∉ s i ∩ ⋃ j ≠ i, s j := λ h, hU (subset_to_measurable _ _ h), simp only [mem_inter_eq, mem_Union, not_and, not_exists] at hU, exact (hU hi j hne.symm hj).elim } end namespace ae_disjoint protected lemma eq (h : ae_disjoint μ s t) : μ (s ∩ t) = 0 := h @[symm] protected lemma symm (h : ae_disjoint μ s t) : ae_disjoint μ t s := by rwa [ae_disjoint, inter_comm] protected lemma symmetric : symmetric (ae_disjoint μ) := λ s t h, h.symm protected lemma comm : ae_disjoint μ s t ↔ ae_disjoint μ t s := ⟨λ h, h.symm, λ h, h.symm⟩ lemma _root_.disjoint.ae_disjoint (h : disjoint s t) : ae_disjoint μ s t := by rw [ae_disjoint, disjoint_iff_inter_eq_empty.1 h, measure_empty] lemma mono_ae (h : ae_disjoint μ s t) (hu : u ≤ᵐ[μ] s) (hv : v ≤ᵐ[μ] t) : ae_disjoint μ u v := measure_mono_null_ae (hu.inter hv) h lemma mono (h : ae_disjoint μ s t) (hu : u ⊆ s) (hv : v ⊆ t) : ae_disjoint μ u v := h.mono_ae hu.eventually_le hv.eventually_le @[simp] lemma Union_left_iff [encodable ι] {s : ι → set α} : ae_disjoint μ (⋃ i, s i) t ↔ ∀ i, ae_disjoint μ (s i) t := by simp only [ae_disjoint, Union_inter, measure_Union_null_iff] @[simp] lemma Union_right_iff [encodable ι] {t : ι → set α} : ae_disjoint μ s (⋃ i, t i) ↔ ∀ i, ae_disjoint μ s (t i) := by simp only [ae_disjoint, inter_Union, measure_Union_null_iff] @[simp] lemma union_left_iff : ae_disjoint μ (s ∪ t) u ↔ ae_disjoint μ s u ∧ ae_disjoint μ t u := by simp [union_eq_Union, and.comm] @[simp] lemma union_right_iff : ae_disjoint μ s (t ∪ u) ↔ ae_disjoint μ s t ∧ ae_disjoint μ s u := by simp [union_eq_Union, and.comm] lemma union_left (hs : ae_disjoint μ s u) (ht : ae_disjoint μ t u) : ae_disjoint μ (s ∪ t) u := union_left_iff.mpr ⟨hs, ht⟩ lemma union_right (ht : ae_disjoint μ s t) (hu : ae_disjoint μ s u) : ae_disjoint μ s (t ∪ u) := union_right_iff.2 ⟨ht, hu⟩ lemma diff_ae_eq_left (h : ae_disjoint μ s t) : (s \ t : set α) =ᵐ[μ] s := @diff_self_inter _ s t ▸ diff_null_ae_eq_self h lemma diff_ae_eq_right (h : ae_disjoint μ s t) : (t \ s : set α) =ᵐ[μ] t := h.symm.diff_ae_eq_left lemma measure_diff_left (h : ae_disjoint μ s t) : μ (s \ t) = μ s := measure_congr h.diff_ae_eq_left lemma measure_diff_right (h : ae_disjoint μ s t) : μ (t \ s) = μ t := measure_congr h.diff_ae_eq_right /-- If `s` and `t` are `μ`-a.e. disjoint, then `s \ u` and `t` are disjoint for some measurable null set `u`. -/ lemma exists_disjoint_diff (h : ae_disjoint μ s t) : ∃ u, measurable_set u ∧ μ u = 0 ∧ disjoint (s \ u) t := ⟨to_measurable μ (s ∩ t), measurable_set_to_measurable _ _, (measure_to_measurable _).trans h, disjoint_diff.symm.mono_left (λ x hx, ⟨hx.1, λ hxt, hx.2 $ subset_to_measurable _ _ ⟨hx.1, hxt⟩⟩)⟩ lemma of_null_right (h : μ t = 0) : ae_disjoint μ s t := measure_mono_null (inter_subset_right _ _) h lemma of_null_left (h : μ s = 0) : ae_disjoint μ s t := (of_null_right h).symm end ae_disjoint lemma ae_disjoint_compl_left : ae_disjoint μ sᶜ s := (@disjoint_compl_left _ s _).ae_disjoint lemma ae_disjoint_compl_right : ae_disjoint μ s sᶜ := (@disjoint_compl_right _ s _).ae_disjoint end measure_theory
f73245db6cbe2e70e35393c68f20c4af06c10e34
bd12a817ba941113eb7fdb7ddf0979d9ed9386a0
/src/category_theory/products/default.lean
0524c99bcd590e8fcd159801037628eba90930de
[ "Apache-2.0" ]
permissive
flypitch/mathlib
563d9c3356c2885eb6cefaa704d8d86b89b74b15
70cd00bc20ad304f2ac0886b2291b44261787607
refs/heads/master
1,590,167,818,658
1,557,762,121,000
1,557,762,121,000
186,450,076
0
0
Apache-2.0
1,557,762,289,000
1,557,762,288,000
null
UTF-8
Lean
false
false
5,873
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import category_theory.functor_category import category_theory.isomorphism import tactic.interactive namespace category_theory universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ -- declare the `v`'s first; see `category_theory.category` for an explanation -- Am awkward note on universes: -- we need to make sure we're in `Type`, not `Sort` -- for both objects and morphisms when taking products. section variables (C : Type u₁) [𝒞 : category.{v₁+1} C] (D : Type u₂) [𝒟 : category.{v₂+1} D] include 𝒞 𝒟 /-- `prod C D` gives the cartesian product of two categories. -/ instance prod : category.{max (v₁+1) (v₂+1)} (C × D) := { hom := λ X Y, ((X.1) ⟶ (Y.1)) × ((X.2) ⟶ (Y.2)), id := λ X, ⟨ 𝟙 (X.1), 𝟙 (X.2) ⟩, comp := λ _ _ _ f g, (f.1 ≫ g.1, f.2 ≫ g.2) } -- rfl lemmas for category.prod @[simp] lemma prod_id (X : C) (Y : D) : 𝟙 (X, Y) = (𝟙 X, 𝟙 Y) := rfl @[simp] lemma prod_comp {P Q R : C} {S T U : D} (f : (P, S) ⟶ (Q, T)) (g : (Q, T) ⟶ (R, U)) : f ≫ g = (f.1 ≫ g.1, f.2 ≫ g.2) := rfl @[simp] lemma prod_id_fst (X : prod C D) : _root_.prod.fst (𝟙 X) = 𝟙 X.fst := rfl @[simp] lemma prod_id_snd (X : prod C D) : _root_.prod.snd (𝟙 X) = 𝟙 X.snd := rfl @[simp] lemma prod_comp_fst {X Y Z : prod C D} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g).1 = f.1 ≫ g.1 := rfl @[simp] lemma prod_comp_snd {X Y Z : prod C D} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g).2 = f.2 ≫ g.2 := rfl end section variables (C : Type u₁) [𝒞 : category.{v₁+1} C] (D : Type u₁) [𝒟 : category.{v₁+1} D] include 𝒞 𝒟 /-- `prod.category.uniform C D` is an additional instance specialised so both factors have the same universe levels. This helps typeclass resolution. -/ instance uniform_prod : category (C × D) := category_theory.prod C D end -- Next we define the natural functors into and out of product categories. For now this doesn't address the universal properties. namespace prod variables (C : Type u₁) [𝒞 : category.{v₁+1} C] (D : Type u₂) [𝒟 : category.{v₂+1} D] include 𝒞 𝒟 /-- `inl C Z` is the functor `X ↦ (X, Z)`. -/ def inl (Z : D) : C ⥤ C × D := { obj := λ X, (X, Z), map := λ X Y f, (f, 𝟙 Z) } /-- `inr D Z` is the functor `X ↦ (Z, X)`. -/ def inr (Z : C) : D ⥤ C × D := { obj := λ X, (Z, X), map := λ X Y f, (𝟙 Z, f) } /-- `fst` is the functor `(X, Y) ↦ X`. -/ def fst : C × D ⥤ C := { obj := λ X, X.1, map := λ X Y f, f.1 } /-- `snd` is the functor `(X, Y) ↦ Y`. -/ def snd : C × D ⥤ D := { obj := λ X, X.2, map := λ X Y f, f.2 } def swap : C × D ⥤ D × C := { obj := λ X, (X.2, X.1), map := λ _ _ f, (f.2, f.1) } def symmetry : swap C D ⋙ swap D C ≅ functor.id (C × D) := { hom := { app := λ X, 𝟙 X, naturality' := λ X Y f, begin erw [category.comp_id (C × D), category.id_comp (C × D)], dsimp [swap], simp, end }, inv := { app := λ X, 𝟙 X, naturality' := λ X Y f, begin erw [category.comp_id (C × D), category.id_comp (C × D)], dsimp [swap], simp, end } } end prod section variables (C : Sort u₁) [𝒞 : category.{v₁} C] (D : Sort u₂) [𝒟 : category.{v₂} D] include 𝒞 𝒟 @[simp] def evaluation : C ⥤ (C ⥤ D) ⥤ D := { obj := λ X, { obj := λ F, F.obj X, map := λ F G α, α.app X, }, map := λ X Y f, { app := λ F, F.map f, naturality' := λ F G α, eq.symm (α.naturality f) }, map_comp' := λ X Y Z f g, begin ext, dsimp, rw functor.map_comp, end } end section variables (C : Type u₁) [𝒞 : category.{v₁+1} C] (D : Type u₂) [𝒟 : category.{v₂+1} D] include 𝒞 𝒟 @[simp] def evaluation_uncurried : C × (C ⥤ D) ⥤ D := { obj := λ p, p.2.obj p.1, map := λ x y f, (x.2.map f.1) ≫ (f.2.app y.1), map_comp' := begin intros X Y Z f g, cases g, cases f, cases Z, cases Y, cases X, dsimp at *, simp at *, erw [←functor.category.comp_app, nat_trans.naturality, category.assoc, nat_trans.naturality] end } end variables {A : Type u₁} [𝒜 : category.{v₁+1} A] {B : Type u₂} [ℬ : category.{v₂+1} B] {C : Type u₃} [𝒞 : category.{v₃+1} C] {D : Type u₄} [𝒟 : category.{v₄+1} D] include 𝒜 ℬ 𝒞 𝒟 namespace functor /-- The cartesian product of two functors. -/ def prod (F : A ⥤ B) (G : C ⥤ D) : A × C ⥤ B × D := { obj := λ X, (F.obj X.1, G.obj X.2), map := λ _ _ f, (F.map f.1, G.map f.2) } /- Because of limitations in Lean 3's handling of notations, we do not setup a notation `F × G`. You can use `F.prod G` as a "poor man's infix", or just write `functor.prod F G`. -/ @[simp] lemma prod_obj (F : A ⥤ B) (G : C ⥤ D) (a : A) (c : C) : (F.prod G).obj (a, c) = (F.obj a, G.obj c) := rfl @[simp] lemma prod_map (F : A ⥤ B) (G : C ⥤ D) {a a' : A} {c c' : C} (f : (a, c) ⟶ (a', c')) : (F.prod G).map f = (F.map f.1, G.map f.2) := rfl end functor namespace nat_trans /-- The cartesian product of two natural transformations. -/ def prod {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟶ G) (β : H ⟶ I) : F.prod H ⟶ G.prod I := { app := λ X, (α.app X.1, β.app X.2), naturality' := begin /- `obviously'` says: -/ intros, cases f, cases Y, cases X, dsimp at *, simp, split, rw naturality, rw naturality end } /- Again, it is inadvisable in Lean 3 to setup a notation `α × β`; use instead `α.prod β` or `nat_trans.prod α β`. -/ @[simp] lemma prod_app {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟶ G) (β : H ⟶ I) (a : A) (c : C) : (nat_trans.prod α β).app (a, c) = (α.app a, β.app c) := rfl end nat_trans end category_theory
047ccf4809c2b1bff1e5963574715d4ecc19e8c7
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/natural_transformation.lean
f79814846df4d5a0d0bc0804de24a93e4ac27607
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,553
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, Floris van Doorn -/ import category_theory.functor /-! # Natural transformations Defines natural transformations between functors. A natural transformation `α : nat_trans F G` consists of morphisms `α.app X : F.obj X ⟶ G.obj X`, and the naturality squares `α.naturality f : F.map f ≫ α.app Y = α.app X ≫ G.map f`, where `f : X ⟶ Y`. Note that we make `nat_trans.naturality` a simp lemma, with the preferred simp normal form pushing components of natural transformations to the left. See also `category_theory.functor_category`, where we provide the category structure on functors and natural transformations. Introduces notations * `τ.app X` for the components of natural transformations, * `F ⟶ G` for the type of natural transformations between functors `F` and `G` (this and the next require `category_theory.functor_category`), * `σ ≫ τ` for vertical compositions, and * `σ ◫ τ` for horizontal compositions. -/ namespace category_theory -- declare the `v`'s first; see `category_theory.category` for an explanation universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] /-- `nat_trans F G` represents a natural transformation between functors `F` and `G`. The field `app` provides the components of the natural transformation. Naturality is expressed by `α.naturality_lemma`. -/ @[ext] structure nat_trans (F G : C ⥤ D) : Type (max u₁ v₂) := (app : Π X : C, F.obj X ⟶ G.obj X) (naturality' : ∀ ⦃X Y : C⦄ (f : X ⟶ Y), F.map f ≫ app Y = app X ≫ G.map f . obviously) restate_axiom nat_trans.naturality' -- Rather arbitrarily, we say that the 'simpler' form is -- components of natural transfomations moving earlier. attribute [simp, reassoc] nat_trans.naturality lemma congr_app {F G : C ⥤ D} {α β : nat_trans F G} (h : α = β) (X : C) : α.app X = β.app X := congr_fun (congr_arg nat_trans.app h) X namespace nat_trans /-- `nat_trans.id F` is the identity natural transformation on a functor `F`. -/ protected def id (F : C ⥤ D) : nat_trans F F := { app := λ X, 𝟙 (F.obj X) } @[simp] lemma id_app' (F : C ⥤ D) (X : C) : (nat_trans.id F).app X = 𝟙 (F.obj X) := rfl instance (F : C ⥤ D) : inhabited (nat_trans F F) := ⟨nat_trans.id F⟩ open category open category_theory.functor section variables {F G H I : C ⥤ D} /-- `vcomp α β` is the vertical compositions of natural transformations. -/ def vcomp (α : nat_trans F G) (β : nat_trans G H) : nat_trans F H := { app := λ X, (α.app X) ≫ (β.app X) } -- functor_category will rewrite (vcomp α β) to (α ≫ β), so this is not a -- suitable simp lemma. We will declare the variant vcomp_app' there. lemma vcomp_app (α : nat_trans F G) (β : nat_trans G H) (X : C) : (vcomp α β).app X = (α.app X) ≫ (β.app X) := rfl end /-- The diagram F(f) F(g) F(h) F X ----> F Y ----> F U ----> F U | | | | | α(X) | α(Y) | α(U) | α(V) v v v v G X ----> G Y ----> G U ----> G V G(f) G(g) G(h) commutes. -/ example {F G : C ⥤ D} (α : nat_trans F G) {X Y U V : C} (f : X ⟶ Y) (g : Y ⟶ U) (h : U ⟶ V) : α.app X ≫ G.map f ≫ G.map g ≫ G.map h = F.map f ≫ F.map g ≫ F.map h ≫ α.app V := by simp end nat_trans end category_theory
b5cef1d065db03a13231c40e0e85387ce5b04887
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/550.lean
bb286c9bad2ae9f3915dd7d9e5a5f8356d8c1f00
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
1,174
lean
-- open function structure bijection (A : Type) := (func finv : A → A) (linv : finv ∘ func = id) (rinv : func ∘ finv = id) attribute bijection.func [coercion] namespace bijection variable {A : Type} definition comp (f g : bijection A) : bijection A := bijection.mk (f ∘ g) (finv g ∘ finv f) (by rewrite [comp.assoc, -{finv f ∘ _}comp.assoc, linv f, comp.left_id, linv g]) (by rewrite [-comp.assoc, {_ ∘ finv g}comp.assoc, rinv g, comp.right_id, rinv f]) infixr ` ∘b `:100 := comp lemma compose.assoc (f g h : bijection A) : (f ∘b g) ∘b h = f ∘b (g ∘b h) := rfl definition id : bijection A := bijection.mk id id (comp.left_id id) (comp.left_id id) lemma id.left_id (f : bijection A) : id ∘b f = f := bijection.rec_on f (λx x x x, rfl) lemma id.right_id (f : bijection A) : f ∘b id = f := bijection.rec_on f (λx x x x, rfl) definition inv (f : bijection A) : bijection A := bijection.mk (finv f) (func f) (rinv f) (linv f) lemma inv.linv (f : bijection A) : inv f ∘b f = id := bijection.rec_on f (λfunc finv linv rinv, by rewrite [↑inv, ↑comp, linv]) end bijection
d4cf6478936975ae20040561b616445f5a4e46a6
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/minimize_errors.lean
d32b313c2b02d9790986a4dc526c3109f57f55a2
[ "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
161
lean
def f : nat → nat → nat := λ a, a check f def g : nat → nat → nat := f check g print g def h : nat → nat → nat | x y := g x y + f y x print h
ffc0578b19e6403fcfa42effdb35bc8949698b2e
618003631150032a5676f229d13a079ac875ff77
/src/data/subtype.lean
1b932fff7f78fb49594123b2aacc3759edd2ff08
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
4,670
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl -/ import tactic.lint -- Lean complains if this section is turned into a namespace open function section subtype variables {α : Sort*} {p : α → Prop} @[simp] theorem subtype.forall {q : {a // p a} → Prop} : (∀ x, q x) ↔ (∀ a b, q ⟨a, b⟩) := ⟨assume h a b, h ⟨a, b⟩, assume h ⟨a, b⟩, h a b⟩ /-- An alternative version of `subtype.forall`. This one is useful if Lean cannot figure out `q` when using `subtype.forall` from right to left. -/ theorem subtype.forall' {q : ∀x, p x → Prop} : (∀ x h, q x h) ↔ (∀ x : {a // p a}, q x.1 x.2) := (@subtype.forall _ _ (λ x, q x.1 x.2)).symm @[simp] theorem subtype.exists {q : {a // p a} → Prop} : (∃ x, q x) ↔ (∃ a b, q ⟨a, b⟩) := ⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩ end subtype namespace subtype variables {α : Sort*} {β : Sort*} {γ : Sort*} {p : α → Prop} lemma val_eq_coe : @val _ p = coe := rfl protected lemma eq' : ∀ {a1 a2 : {x // p x}}, a1.val = a2.val → a1 = a2 | ⟨x, h1⟩ ⟨.(x), h2⟩ rfl := rfl lemma ext {a1 a2 : {x // p x}} : a1 = a2 ↔ a1.val = a2.val := ⟨congr_arg _, subtype.eq'⟩ lemma coe_ext {a1 a2 : {x // p x}} : a1 = a2 ↔ (a1 : α) = a2 := ext theorem val_injective : injective (@val _ p) := λ a b, subtype.eq' /-- Restrict a (dependent) function to a subtype -/ def restrict {α} {β : α → Type*} (f : Πx, β x) (p : α → Prop) (x : subtype p) : β x.1 := f x.1 lemma restrict_apply {α} {β : α → Type*} (f : Πx, β x) (p : α → Prop) (x : subtype p) : restrict f p x = f x.1 := by refl lemma restrict_def {α β} (f : α → β) (p : α → Prop) : restrict f p = f ∘ subtype.val := by refl lemma restrict_injective {α β} {f : α → β} (p : α → Prop) (h : injective f) : injective (restrict f p) := h.comp subtype.val_injective /-- Defining a map into a subtype, this can be seen as an "coinduction principle" of `subtype`-/ def coind {α β} (f : α → β) {p : β → Prop} (h : ∀a, p (f a)) : α → subtype p := λ a, ⟨f a, h a⟩ theorem coind_injective {α β} {f : α → β} {p : β → Prop} (h : ∀a, p (f a)) (hf : injective f) : injective (coind f h) := λ x y hxy, hf $ by apply congr_arg subtype.val hxy /-- Restriction of a function to a function on subtypes. -/ def map {p : α → Prop} {q : β → Prop} (f : α → β) (h : ∀a, p a → q (f a)) : subtype p → subtype q := λ x, ⟨f x.1, h x.1 x.2⟩ theorem map_comp {p : α → Prop} {q : β → Prop} {r : γ → Prop} {x : subtype p} (f : α → β) (h : ∀a, p a → q (f a)) (g : β → γ) (l : ∀a, q a → r (g a)) : map g l (map f h x) = map (g ∘ f) (assume a ha, l (f a) $ h a ha) x := rfl theorem map_id {p : α → Prop} {h : ∀a, p a → p (id a)} : map (@id α) h = id := funext $ assume ⟨v, h⟩, rfl lemma map_injective {p : α → Prop} {q : β → Prop} {f : α → β} (h : ∀a, p a → q (f a)) (hf : injective f) : injective (map f h) := coind_injective _ $ hf.comp val_injective instance [has_equiv α] (p : α → Prop) : has_equiv (subtype p) := ⟨λ s t, s.val ≈ t.val⟩ theorem equiv_iff [has_equiv α] {p : α → Prop} {s t : subtype p} : s ≈ t ↔ s.val ≈ t.val := iff.rfl variables [setoid α] protected theorem refl (s : subtype p) : s ≈ s := setoid.refl s.val protected theorem symm {s t : subtype p} (h : s ≈ t) : t ≈ s := setoid.symm h protected theorem trans {s t u : subtype p} (h₁ : s ≈ t) (h₂ : t ≈ u) : s ≈ u := setoid.trans h₁ h₂ theorem equivalence (p : α → Prop) : equivalence (@has_equiv.equiv (subtype p) _) := mk_equivalence _ subtype.refl (@subtype.symm _ p _) (@subtype.trans _ p _) instance (p : α → Prop) : setoid (subtype p) := setoid.mk (≈) (equivalence p) end subtype namespace subtype variables {α : Type*} {β : Type*} {γ : Type*} {p : α → Prop} @[simp] theorem coe_eta {α : Type*} {p : α → Prop} (a : {a // p a}) (h : p a) : mk ↑a h = a := eta _ _ @[simp] theorem coe_mk {α : Type*} {p : α → Prop} (a h) : (@mk α p a h : α) = a := rfl @[simp, nolint simp_nf] -- built-in reduction doesn't always work theorem mk_eq_mk {α : Type*} {p : α → Prop} {a h a' h'} : @mk α p a h = @mk α p a' h' ↔ a = a' := ⟨λ H, by injection H, λ H, by congr; assumption⟩ @[simp] lemma val_prop {S : set α} (a : {a // a ∈ S}) : a.val ∈ S := a.property @[simp] lemma val_prop' {S : set α} (a : {a // a ∈ S}) : ↑a ∈ S := a.property end subtype
625f3bf10f0e718ca6b4f824c07754d036d26a76
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch3/ex0501.lean
368072f57e94100030dd15d89e82da40ece8bbf5
[]
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
47
lean
open classical variable p : Prop #check em p
c90629cb69d61d9f888b6c8a0cdd82044bc8c913
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Lean/Data/Trie.lean
0a7d3755b3c64d026264f7330f06d23ea6694a31
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
2,882
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich, Leonardo de Moura Trie for tokenizing the Lean language -/ prelude import Init.Data.RBMap import Init.Lean.Data.Format namespace Lean namespace Parser inductive Trie (α : Type) | Node : Option α → RBNode Char (fun _ => Trie) → Trie namespace Trie variables {α : Type} def empty : Trie α := ⟨none, RBNode.leaf⟩ instance : HasEmptyc (Trie α) := ⟨empty⟩ instance : Inhabited (Trie α) := ⟨Node none RBNode.leaf⟩ private partial def insertEmptyAux (s : String) (val : α) : String.Pos → Trie α | i => match s.atEnd i with | true => Trie.Node (some val) RBNode.leaf | false => let c := s.get i; let t := insertEmptyAux (s.next i); Trie.Node none (RBNode.singleton c t) private partial def insertAux (s : String) (val : α) : Trie α → String.Pos → Trie α | Trie.Node v m, i => match s.atEnd i with | true => Trie.Node (some val) m -- overrides old value | false => let c := s.get i; let i := s.next i; let t := match RBNode.find Char.lt m c with | none => insertEmptyAux s val i | some t => insertAux t i; Trie.Node v (RBNode.insert Char.lt m c t) def insert (t : Trie α) (s : String) (val : α) : Trie α := insertAux s val t 0 private partial def findAux (s : String) : Trie α → String.Pos → Option α | Trie.Node val m, i => match s.atEnd i with | true => val | false => let c := s.get i; let i := s.next i; match RBNode.find Char.lt m c with | none => none | some t => findAux t i def find (t : Trie α) (s : String) : Option α := findAux s t 0 private def updtAcc (v : Option α) (i : String.Pos) (acc : String.Pos × Option α) : String.Pos × Option α := match v, acc with | some v, (j, w) => (i, some v) -- we pattern match on `acc` to enable memory reuse | none, acc => acc private partial def matchPrefixAux (s : String) : Trie α → String.Pos → (String.Pos × Option α) → String.Pos × Option α | Trie.Node v m, i, acc => match s.atEnd i with | true => updtAcc v i acc | false => let acc := updtAcc v i acc; let c := s.get i; let i := s.next i; match RBNode.find Char.lt m c with | some t => matchPrefixAux t i acc | none => acc def matchPrefix (s : String) (t : Trie α) (i : String.Pos) : String.Pos × Option α := matchPrefixAux s t i (i, none) private partial def toStringAux {α : Type} : Trie α → List Format | Trie.Node val map => map.fold (fun Fs c t => format (repr c) :: (Format.group $ Format.nest 2 $ flip Format.joinSep Format.line $ toStringAux t) :: Fs) [] instance {α : Type} : HasToString (Trie α) := ⟨fun t => (flip Format.joinSep Format.line $ toStringAux t).pretty⟩ end Trie end Parser end Lean
a53e0803c660c6af2c6f3369bfb4d546d19aa662
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/list/indexes.lean
f8264c39876d8ff7c3a9a40974e8611c5c4d6206
[ "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
8,124
lean
/- Copyright (c) 2020 Jannis Limperg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jannis Limperg -/ import data.list.of_fn import data.list.range /-! # Lemmas about list.*_with_index functions. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Some specification lemmas for `list.map_with_index`, `list.mmap_with_index`, `list.foldl_with_index` and `list.foldr_with_index`. -/ universes u v open function namespace list variables {α : Type u} {β : Type v} section map_with_index @[simp] lemma map_with_index_nil {α β} (f : ℕ → α → β) : map_with_index f [] = [] := rfl lemma map_with_index_core_eq (l : list α) (f : ℕ → α → β) (n : ℕ) : l.map_with_index_core f n = l.map_with_index (λ i a, f (i + n) a) := begin induction l with hd tl hl generalizing f n, { simpa }, { rw [map_with_index], simp [map_with_index_core, hl, add_left_comm, add_assoc, add_comm] } end lemma map_with_index_eq_enum_map (l : list α) (f : ℕ → α → β) : l.map_with_index f = l.enum.map (function.uncurry f) := begin induction l with hd tl hl generalizing f, { simp [list.enum_eq_zip_range] }, { rw [map_with_index, map_with_index_core, map_with_index_core_eq, hl], simp [enum_eq_zip_range, range_succ_eq_map, zip_with_map_left, map_uncurry_zip_eq_zip_with] } end @[simp] lemma map_with_index_cons {α β} (l : list α) (f : ℕ → α → β) (a : α) : map_with_index f (a :: l) = f 0 a :: map_with_index (λ i, f (i + 1)) l := by simp [map_with_index_eq_enum_map, enum_eq_zip_range, map_uncurry_zip_eq_zip_with, range_succ_eq_map, zip_with_map_left] lemma map_with_index_append {α} (K L : list α) (f : ℕ → α → β) : (K ++ L).map_with_index f = K.map_with_index f ++ L.map_with_index (λ i a, f (i + K.length) a) := begin induction K with a J IH generalizing f, { simp }, { simp [IH (λ i, f (i+1)), add_assoc], } end @[simp] lemma length_map_with_index {α β} (l : list α) (f : ℕ → α → β) : (l.map_with_index f).length = l.length := begin induction l with hd tl IH generalizing f, { simp }, { simp [IH] } end @[simp] lemma nth_le_map_with_index {α β} (l : list α) (f : ℕ → α → β) (i : ℕ) (h : i < l.length) (h' : i < (l.map_with_index f).length := h.trans_le (l.length_map_with_index f).ge): (l.map_with_index f).nth_le i h' = f i (l.nth_le i h) := by simp [map_with_index_eq_enum_map, enum_eq_zip_range] lemma map_with_index_eq_of_fn {α β} (l : list α) (f : ℕ → α → β) : l.map_with_index f = of_fn (λ (i : fin l.length), f (i : ℕ) (l.nth_le i i.is_lt)) := begin induction l with hd tl IH generalizing f, { simp }, { simpa [IH] } end end map_with_index section foldr_with_index /-- Specification of `foldr_with_index_aux`. -/ def foldr_with_index_aux_spec (f : ℕ → α → β → β) (start : ℕ) (b : β) (as : list α) : β := foldr (uncurry f) b $ enum_from start as theorem foldr_with_index_aux_spec_cons (f : ℕ → α → β → β) (start b a as) : foldr_with_index_aux_spec f start b (a :: as) = f start a (foldr_with_index_aux_spec f (start + 1) b as) := rfl theorem foldr_with_index_aux_eq_foldr_with_index_aux_spec (f : ℕ → α → β → β) (start b as) : foldr_with_index_aux f start b as = foldr_with_index_aux_spec f start b as := begin induction as generalizing start, { refl }, { simp only [foldr_with_index_aux, foldr_with_index_aux_spec_cons, *] } end theorem foldr_with_index_eq_foldr_enum (f : ℕ → α → β → β) (b : β) (as : list α) : foldr_with_index f b as = foldr (uncurry f) b (enum as) := by simp only [foldr_with_index, foldr_with_index_aux_spec, foldr_with_index_aux_eq_foldr_with_index_aux_spec, enum] end foldr_with_index theorem indexes_values_eq_filter_enum (p : α → Prop) [decidable_pred p] (as : list α) : indexes_values p as = filter (p ∘ prod.snd) (enum as) := by simp [indexes_values, foldr_with_index_eq_foldr_enum, uncurry, filter_eq_foldr] theorem find_indexes_eq_map_indexes_values (p : α → Prop) [decidable_pred p] (as : list α) : find_indexes p as = map prod.fst (indexes_values p as) := by simp only [indexes_values_eq_filter_enum, map_filter_eq_foldr, find_indexes, foldr_with_index_eq_foldr_enum, uncurry] section foldl_with_index /-- Specification of `foldl_with_index_aux`. -/ def foldl_with_index_aux_spec (f : ℕ → α → β → α) (start : ℕ) (a : α) (bs : list β) : α := foldl (λ a (p : ℕ × β), f p.fst a p.snd) a $ enum_from start bs theorem foldl_with_index_aux_spec_cons (f : ℕ → α → β → α) (start a b bs) : foldl_with_index_aux_spec f start a (b :: bs) = foldl_with_index_aux_spec f (start + 1) (f start a b) bs := rfl theorem foldl_with_index_aux_eq_foldl_with_index_aux_spec (f : ℕ → α → β → α) (start a bs) : foldl_with_index_aux f start a bs = foldl_with_index_aux_spec f start a bs := begin induction bs generalizing start a, { refl }, { simp [foldl_with_index_aux, foldl_with_index_aux_spec_cons, *] } end theorem foldl_with_index_eq_foldl_enum (f : ℕ → α → β → α) (a : α) (bs : list β) : foldl_with_index f a bs = foldl (λ a (p : ℕ × β), f p.fst a p.snd) a (enum bs) := by simp only [foldl_with_index, foldl_with_index_aux_spec, foldl_with_index_aux_eq_foldl_with_index_aux_spec, enum] end foldl_with_index section mfold_with_index variables {m : Type u → Type v} [monad m] theorem mfoldr_with_index_eq_mfoldr_enum {α β} (f : ℕ → α → β → m β) (b : β) (as : list α) : mfoldr_with_index f b as = mfoldr (uncurry f) b (enum as) := by simp only [mfoldr_with_index, mfoldr_eq_foldr, foldr_with_index_eq_foldr_enum, uncurry] theorem mfoldl_with_index_eq_mfoldl_enum [is_lawful_monad m] {α β} (f : ℕ → β → α → m β) (b : β) (as : list α) : mfoldl_with_index f b as = mfoldl (λ b (p : ℕ × α), f p.fst b p.snd) b (enum as) := by rw [mfoldl_with_index, mfoldl_eq_foldl, foldl_with_index_eq_foldl_enum] end mfold_with_index section mmap_with_index variables {m : Type u → Type v} [applicative m] /-- Specification of `mmap_with_index_aux`. -/ def mmap_with_index_aux_spec {α β} (f : ℕ → α → m β) (start : ℕ) (as : list α) : m (list β) := list.traverse (uncurry f) $ enum_from start as -- Note: `traverse` the class method would require a less universe-polymorphic -- `m : Type u → Type u`. theorem mmap_with_index_aux_spec_cons {α β} (f : ℕ → α → m β) (start : ℕ) (a : α) (as : list α) : mmap_with_index_aux_spec f start (a :: as) = list.cons <$> f start a <*> mmap_with_index_aux_spec f (start + 1) as := rfl theorem mmap_with_index_aux_eq_mmap_with_index_aux_spec {α β} (f : ℕ → α → m β) (start : ℕ) (as : list α) : mmap_with_index_aux f start as = mmap_with_index_aux_spec f start as := begin induction as generalizing start, { refl }, { simp [mmap_with_index_aux, mmap_with_index_aux_spec_cons, *] } end theorem mmap_with_index_eq_mmap_enum {α β} (f : ℕ → α → m β) (as : list α) : mmap_with_index f as = list.traverse (uncurry f) (enum as) := by simp only [mmap_with_index, mmap_with_index_aux_spec, mmap_with_index_aux_eq_mmap_with_index_aux_spec, enum ] end mmap_with_index section mmap_with_index' variables {m : Type u → Type v} [applicative m] [is_lawful_applicative m] theorem mmap_with_index'_aux_eq_mmap_with_index_aux {α} (f : ℕ → α → m punit) (start : ℕ) (as : list α) : mmap_with_index'_aux f start as = mmap_with_index_aux f start as *> pure punit.star := by induction as generalizing start; simp [mmap_with_index'_aux, mmap_with_index_aux, *, seq_right_eq, const, -comp_const] with functor_norm theorem mmap_with_index'_eq_mmap_with_index {α} (f : ℕ → α → m punit) (as : list α) : mmap_with_index' f as = mmap_with_index f as *> pure punit.star := by apply mmap_with_index'_aux_eq_mmap_with_index_aux end mmap_with_index' end list
7851f8dc8ab72a0df234a60a692f3bd888690edb
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/category_theory/products.lean
90b3be060c67c50c245364760495a98d8cb1337c
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
4,892
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import category_theory.functor_category import category_theory.isomorphism import tactic.interactive namespace category_theory universes u₁ v₁ u₂ v₂ u₃ v₃ u₄ v₄ section variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] (D : Type u₂) [𝒟 : category.{u₂ v₂} D] include 𝒞 𝒟 /-- `prod.category C D` gives the cartesian product of two categories. -/ instance prod : category.{(max u₁ u₂) (max v₁ v₂)} (C × D) := { hom := λ X Y, ((X.1) ⟶ (Y.1)) × ((X.2) ⟶ (Y.2)), id := λ X, ⟨ 𝟙 (X.1), 𝟙 (X.2) ⟩, comp := λ _ _ _ f g, (f.1 ≫ g.1, f.2 ≫ g.2) } -- rfl lemmas for category.prod @[simp] lemma prod_id (X : C) (Y : D) : 𝟙 (X, Y) = (𝟙 X, 𝟙 Y) := rfl @[simp] lemma prod_comp {P Q R : C} {S T U : D} (f : (P, S) ⟶ (Q, T)) (g : (Q, T) ⟶ (R, U)) : f ≫ g = (f.1 ≫ g.1, f.2 ≫ g.2) := rfl end section variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] (D : Type u₁) [𝒟 : category.{u₁ v₁} D] include 𝒞 𝒟 /-- `prod.category.uniform C D` is an additional instance specialised so both factors have the same universe levels. This helps typeclass resolution. -/ instance uniform_prod : category (C × D) := category_theory.prod C D end -- Next we define the natural functors into and out of product categories. For now this doesn't address the universal properties. namespace prod variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] (D : Type u₂) [𝒟 : category.{u₂ v₂} D] include 𝒞 𝒟 /-- `inl C Z` is the functor `X ↦ (X, Z)`. -/ def inl (Z : D) : C ⥤ (C × D) := { obj := λ X, (X, Z), map := λ X Y f, (f, 𝟙 Z) } /-- `inr D Z` is the functor `X ↦ (Z, X)`. -/ def inr (Z : C) : D ⥤ (C × D) := { obj := λ X, (Z, X), map := λ X Y f, (𝟙 Z, f) } /-- `fst` is the functor `(X, Y) ↦ X`. -/ def fst : (C × D) ⥤ C := { obj := λ X, X.1, map := λ X Y f, f.1 } /-- `snd` is the functor `(X, Y) ↦ Y`. -/ def snd : (C × D) ⥤ D := { obj := λ X, X.2, map := λ X Y f, f.2 } def swap : (C × D) ⥤ (D × C) := { obj := λ X, (X.2, X.1), map := λ _ _ f, (f.2, f.1) } def symmetry : ((swap C D) ⋙ (swap D C)) ≅ (functor.id (C × D)) := { hom := { app := λ X, 𝟙 X, naturality' := begin intros, erw [category.comp_id (C × D), category.id_comp (C × D)], dsimp [swap], simp, end }, inv := { app := λ X, 𝟙 X, naturality' := begin intros, erw [category.comp_id (C × D), category.id_comp (C × D)], dsimp [swap], simp, end } } end prod section variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] (D : Type u₂) [𝒟 : category.{u₂ v₂} D] include 𝒞 𝒟 -- TODO, later this can be defined by uncurrying `functor.id (C ⥤ D)` def evaluation : ((C ⥤ D) × C) ⥤ D := { obj := λ p, p.1.obj p.2, map := λ x y f, (x.1.map f.2) ≫ (f.1.app y.2), map_comp' := begin intros X Y Z f g, cases g, cases f, cases Z, cases Y, cases X, dsimp at *, simp at *, erw [←nat_trans.vcomp_app, nat_trans.naturality, category.assoc, nat_trans.naturality] end } end variables {A : Type u₁} [𝒜 : category.{u₁ v₁} A] {B : Type u₂} [ℬ : category.{u₂ v₂} B] {C : Type u₃} [𝒞 : category.{u₃ v₃} C] {D : Type u₄} [𝒟 : category.{u₄ v₄} D] include 𝒜 ℬ 𝒞 𝒟 namespace functor /-- The cartesian product of two functors. -/ def prod (F : A ⥤ B) (G : C ⥤ D) : (A × C) ⥤ (B × D) := { obj := λ X, (F.obj X.1, G.obj X.2), map := λ _ _ f, (F.map f.1, G.map f.2) } /- Because of limitations in Lean 3's handling of notations, we do not setup a notation `F × G`. You can use `F.prod G` as a "poor man's infix", or just write `functor.prod F G`. -/ @[simp] lemma prod_obj (F : A ⥤ B) (G : C ⥤ D) (a : A) (c : C) : (F.prod G).obj (a, c) = (F.obj a, G.obj c) := rfl @[simp] lemma prod_map (F : A ⥤ B) (G : C ⥤ D) {a a' : A} {c c' : C} (f : (a, c) ⟶ (a', c')) : (F.prod G).map f = (F.map f.1, G.map f.2) := rfl end functor namespace nat_trans /-- The cartesian product of two natural transformations. -/ def prod {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟹ G) (β : H ⟹ I) : F.prod H ⟹ G.prod I := { app := λ X, (α.app X.1, β.app X.2), naturality' := begin /- `obviously'` says: -/ intros, cases f, cases Y, cases X, dsimp at *, simp, split, rw naturality, rw naturality end } /- Again, it is inadvisable in Lean 3 to setup a notation `α × β`; use instead `α.prod β` or `nat_trans.prod α β`. -/ @[simp] lemma prod_app {F G : A ⥤ B} {H I : C ⥤ D} (α : F ⟹ G) (β : H ⟹ I) (a : A) (c : C) : (nat_trans.prod α β).app (a, c) = (α.app a, β.app c) := rfl end nat_trans end category_theory
e575b851b9db8343f53e4eb039857285b9bd5110
4727251e0cd73359b15b664c3170e5d754078599
/src/data/nat/choose/bounds.lean
7ecb3999ac693dc647de8ebde1c28354d063bcc8
[ "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
1,264
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Eric Rodriguez -/ import data.nat.choose.basic import data.nat.cast import algebra.group_power.lemmas /-! # Inequalities for binomial coefficients This file proves exponential bounds on binomial coefficients. We might want to add here the bounds `n^r/r^r ≤ n.choose r ≤ e^r n^r/r^r` in the future. ## Main declarations * `nat.choose_le_pow`: `n.choose r ≤ n^r / r!` * `nat.pow_le_choose`: `(n + 1 - r)^r / r! ≤ n.choose r`. Beware of the fishy ℕ-subtraction. -/ open_locale nat variables {α : Type*} [linear_ordered_field α] namespace nat lemma choose_le_pow (r n : ℕ) : (n.choose r : α) ≤ n^r / r! := begin rw le_div_iff', { norm_cast, rw ←nat.desc_factorial_eq_factorial_mul_choose, exact n.desc_factorial_le_pow r }, exact_mod_cast r.factorial_pos, end -- horrific casting is due to ℕ-subtraction lemma pow_le_choose (r n : ℕ) : ((n + 1 - r : ℕ)^r : α) / r! ≤ n.choose r := begin rw div_le_iff', { norm_cast, rw [←nat.desc_factorial_eq_factorial_mul_choose], exact n.pow_sub_le_desc_factorial r }, exact_mod_cast r.factorial_pos, end end nat
6ffee68f8e407b4d9656879e8f851239f593f121
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/data/sigma.lean
ce54f3cd69341fc874987c7f59d1a2d12590358f
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
383
lean
universe variables u v variables {α : Type u} {β : α → Type v} variables {x y : sigma β} variables H : x.1 = y.1 variables H' : x.1 = y.1 → x.2 == y.2 include H H' lemma sigma.ext : x = y := begin cases x with x₀ x₁, cases y with y₀ y₁, have H'' := H' H, clear H', unfold sigma.fst at H, unfold sigma.snd at H'', subst y₀, rw eq_of_heq H'', end
0f3adfd0d9fca6984751083996c68ec36faa048c
744d2f2ae1c950e037b080a0e29c8c49ca9b35e5
/racket/lib/lemmas.lean
52b2a2d387cf55d9bd068c256a0fab593707a0b5
[]
no_license
uw-unsat/bpf-jit-verif
5a6b18ce04ab291eca921dfeb4e5de9950f9b9fb
7b15b8f30643509eb3b058cc6ca9060b5bad3bd3
refs/heads/master
1,601,284,957,342
1,583,173,155,000
1,583,173,155,000
226,746,308
10
0
null
null
null
null
UTF-8
Lean
false
false
17,026
lean
-- Theorems for axiomatization: -- bvmul_comm, bvmulhu_comm, bvmul_decomp -- -- The theorems prove that both bvmul and bvmulhu are commutative, -- and that 64-bit bvmul can be decomposed into 32-bit operations. -- They are used by spec.rkt to replace bitvector primitives with -- uninterpreted functions. -- -- One can check the proofs using the Lean theorem prover 3.x: -- https://leanprover.github.io namespace nat theorem eq_sub_of_add_eq {a b c : ℕ} (h : a + c = b) : a = b - c := calc a = a + c - c : by rw nat.add_sub_cancel ... = b - c : by rw h theorem sub_add_eq_add_sub {a b c : ℕ} (h : a ≥ b) : a - b + c = a + c - b := calc a - b + c = c + (a - b) : by rw add_comm ... = c + a - b : by rw ← nat.add_sub_assoc h ... = a + c - b : by rw add_comm theorem sub_sub_eq_sub_add {a b c : ℕ} (ab : a ≥ b) (bc : b ≥ c) : a - (b - c) = a - b + c := have a = a - b + c + (b - c), by calc a = a - b + b : by rw nat.sub_add_cancel ab ... = a - b + b + c - c : by rw nat.add_sub_cancel ... = a - b + c + b - c : by rw [add_assoc, add_comm b c, add_assoc] ... = a - b + c + (b - c) : by rw nat.add_sub_assoc bc, begin rw nat.sub_eq_iff_eq_add, apply this, apply nat.le_trans (sub_le b c) ab, end theorem le_mul_of_pos_left {n k : ℕ} (h : k > 0) : n ≤ k * n := calc n = 1 * n : by rw one_mul ... ≤ k * n : by { apply mul_le_mul_right, apply succ_le_of_lt h } theorem mul_add_mod_left (k x z : ℕ) : (k * x + z) % x = z % x := by simp end nat def bitvector (n : ℕ) := { x : ℕ // x < 2^n } namespace bitvector variable {n : ℕ} instance decidable_eq : decidable_eq (bitvector n) := by apply_instance @[simp] def to_nat (v : bitvector n) : ℕ := v.1 theorem eq : ∀ (v₁ v₂ : bitvector n), to_nat v₁ = to_nat v₂ → v₁ = v₂ | ⟨_, _⟩ ⟨_, _⟩ rfl := rfl @[reducible] def length (v : bitvector n) : ℕ := n theorem modulus_pos : 0 < 2^n := have 0 < 2, by { apply nat.le_succ }, nat.pos_pow_of_pos n this. theorem modulus_ge_one : 1 ≤ 2^n := by { apply modulus_pos }. theorem modulus_le {n m : ℕ} : n ≤ m → 2^n ≤ 2^m := have 0 < 2, { apply nat.le_succ }, nat.pow_le_pow_of_le_right this. theorem modulus_lt {n m : ℕ} : n < m → 2^n < 2^m := have 1 < 2, { apply nat.lt_succ_self }, nat.pow_lt_pow_of_lt_right this. theorem modulus_le_add_right (n m : ℕ) : 2^m ≤ 2^(n + m) := begin apply modulus_le, apply nat.le_add_left end theorem mod_modulus_range (x : ℕ) : x % 2^n < 2^n := nat.mod_lt x modulus_pos theorem modulus_le_of_lt {x : ℕ}: x < 2^n → x ≤ 2^n - 1 := begin rw ← nat.add_le_to_le_sub _ modulus_ge_one, apply nat.succ_le_of_lt end -- Lean doesn't have many theorems about pow, so use shifts as a detour. theorem modulus_add_mul (n m : ℕ) : 2^(n + m) = 2^n * 2^m := calc 2^(n + m) = 1 * 2^(n + m) : by rw nat.one_mul ... = nat.shiftl 1 (n + m) : by rw nat.shiftl_eq_mul_pow ... = nat.shiftl (nat.shiftl 1 n) m : by rw nat.shiftl_add ... = nat.shiftl (2^n) m : by rw nat.one_shiftl ... = 2^n * 2^m : by rw nat.shiftl_eq_mul_pow theorem modulus_sub_div {n m : ℕ} (H : m ≤ n) : 2^(n - m) = 2^n / 2^m := calc 2^(n - m) = 1 * 2^(n - m) : by rw nat.one_mul ... = nat.shiftl 1 (n - m) : by rw nat.shiftl_eq_mul_pow ... = nat.shiftr (nat.shiftl 1 n) m : by rw nat.shiftl_sub _ H ... = nat.shiftr (2^n) m : by rw nat.one_shiftl ... = 2^n / 2^m : by rw nat.shiftr_eq_div_pow theorem modulus_sub_mul {n m : ℕ} (H : m ≤ n) : 2^n = 2^(n - m) * 2^m := calc 2^n = 2^(n - m + m) : by rw nat.sub_add_cancel H ... = 2^(n - m) * 2^m : by rw modulus_add_mul theorem modulus_dvd {n m : ℕ} (H : m ≤ n) : 2^m ∣ 2^n := begin apply nat.dvd_of_mod_eq_zero, rw modulus_sub_mul H, apply nat.mul_mod_left end def of_nat (n x : ℕ) : bitvector n := ⟨ x % 2^n, by { apply mod_modulus_range } ⟩ @[reducible] protected def zero (n : ℕ) : bitvector n := ⟨ 0, modulus_pos ⟩ @[reducible] protected def one (n : ℕ) : bitvector n := ⟨ 1 % 2^n, mod_modulus_range 1 ⟩ @[reducible] protected def umax (n : ℕ) : bitvector n := ⟨ 2^n - 1, by { apply nat.sub_lt_of_pos_le, apply nat.lt_succ_self, apply modulus_ge_one } ⟩ def bvadd (x y : bitvector n) : bitvector n := of_nat n (to_nat x + to_nat y) theorem bvadd_comm (x y : bitvector n) : bvadd x y = bvadd y x := begin unfold bvadd, rw add_comm end theorem bvadd_zero_left (x : bitvector n) : bvadd (bitvector.zero n) x = x := begin apply bitvector.eq, cases x with _ hx, unfold bvadd, unfold of_nat, simp, apply nat.mod_eq_of_lt hx end def bvmul (x y : bitvector n) : bitvector n := of_nat n (to_nat x * to_nat y) theorem bvmul_comm (x y : bitvector n) : bvmul x y = bvmul y x := begin unfold bvmul, rw mul_comm, end theorem bvmul_zero_right (x : bitvector n) : bvmul x (bitvector.zero n) = bitvector.zero n := begin apply bitvector.eq, unfold bvmul, unfold of_nat, simp end instance : has_zero (bitvector n) := ⟨bitvector.zero n⟩ instance : has_one (bitvector n) := ⟨bitvector.one n⟩ instance : has_add (bitvector n) := ⟨bitvector.bvadd⟩ instance : has_mul (bitvector n) := ⟨bitvector.bvmul⟩ def bvneg (x : bitvector n) : bitvector n := of_nat n (2^n - to_nat x) theorem bvneg_bvmul_umax_right (x : bitvector n) : bvneg x = bvmul x (bitvector.umax n) := begin apply bitvector.eq, cases x with x hx, unfold bvneg, unfold bvmul, unfold of_nat, simp, rw nat.mul_sub_left_distrib, simp, cases nat.eq_zero_or_pos x with h h, { simp [h] }, { rw ← nat.mul_add_mod_left (x - 1), rw nat.mul_sub_right_distrib, rw one_mul, rw ← nat.add_sub_assoc (le_of_lt hx), rw nat.sub_add_cancel, apply nat.le_mul_of_pos_left h } end def zext {n m : ℕ} (H : n ≤ m) : bitvector n → bitvector m | ⟨ x, p ⟩ := ⟨ x, by { apply nat.lt_of_lt_of_le p, apply modulus_le H } ⟩ def concat {n m : ℕ} : bitvector n → bitvector m → bitvector (n + m) | ⟨ x₁, p₁ ⟩ ⟨ x₂, p₂ ⟩ := have x₁ * 2^m ≤ (2^n - 1) * 2^m, by { apply nat.mul_le_mul_right, apply modulus_le_of_lt p₁ }, have x₁ * 2^m + x₂ < 2^(n + m), by calc x₁ * 2^m + x₂ ≤ (2^n - 1) * 2^m + x₂ : by { apply nat.add_le_add_right this } ... = 2^(n + m) - 2^m + x₂ : by simp [nat.mul_sub_right_distrib, modulus_add_mul] ... < 2^(n + m) - 2^m + 2^m : by { apply nat.add_lt_add_left p₂ } ... = 2^(n + m) : by { apply nat.sub_add_cancel, apply modulus_le_add_right }, ⟨ x₁ * 2^m + x₂, this ⟩ -- An alternative definition for extract. def trunc (i m : ℕ) : bitvector n → bitvector m | ⟨ x, p ⟩ := ⟨ (x / 2^i) % 2^m, by { apply mod_modulus_range } ⟩ theorem concat_trunc {n m : ℕ} (x : bitvector (n + m)) : concat (trunc m n x) (trunc 0 m x) = x := begin apply bitvector.eq, cases x with x hx, unfold trunc, unfold concat, simp, rw @nat.mod_eq_of_lt _ (2^n), rw [mul_comm, nat.mod_add_div], rw nat.div_lt_iff_lt_mul, rw modulus_add_mul at hx, apply hx, apply modulus_pos end theorem trunc_concat {n m : ℕ} (x : bitvector n) (y : bitvector m) : trunc 0 m (concat x y) = y := begin apply bitvector.eq, cases x with x hx, cases y with y hy, unfold concat, unfold trunc, simp, rw nat.mod_eq_of_lt, apply hy end end bitvector theorem mod_sub_div (x y : ℕ) : x % y = x - y * (x / y) := have (x % y) + (y * (x / y)) = x, by { apply nat.mod_add_div }, by { apply nat.eq_sub_of_add_eq this } theorem mul_div_le_self : ∀ (m n : ℕ), n * (m / n) ≤ m := begin intros, rewrite mul_comm, apply nat.div_mul_le_self end theorem add_mod_self_left (x y z : ℕ) : (x % z + y) % z = (x + y) % z := have hxz: z * (x / z) ≤ x, by { apply mul_div_le_self }, have hxzy : z * (x / z) ≤ x + y, by { apply le_trans hxz, apply nat.le_add_right }, calc (x % z + y) % z = (x - z * (x / z) + y) % z : by rw mod_sub_div x z ... = (x + y - z * (x / z)) % z : by rw nat.sub_add_eq_add_sub hxz ... = (x + y) % z : by { apply nat.sub_mul_mod _ _ _ hxzy } theorem add_mod_self_right (x y z : ℕ) : (x + y % z) % z = (x + y) % z := begin rw add_comm, rw add_mod_self_left, rw add_comm end theorem add_mod (x y z : ℕ) : (x + y) % z = (x % z + y % z) % z := calc (x + y) % z = (x % z + y) % z : by rw ← add_mod_self_left ... = (x % z + y % z) % z : by rw ← add_mod_self_right theorem mul_mod_self_left (x y z : ℕ) : ((x % z) * y) % z = (x * y) % z := have z * (x / z) ≤ x, by { apply mul_div_le_self }, have z * (x / z) * y ≤ x * y, by { apply nat.mul_le_mul_right _ this }, have h : z * (x / z * y) ≤ x * y, by { rw ← nat.mul_assoc, apply this }, calc ((x % z) * y) % z = ((x - z * (x / z)) * y) % z : by rw mod_sub_div x z ... = (x * y - z * ((x / z) * y)) % z : by simp [nat.mul_sub_right_distrib, nat.mul_assoc] ... = (x * y) % z : by { apply nat.sub_mul_mod _ _ _ h } theorem mul_mod_self_right (x y z : ℕ) : (x * (y % z)) % z = (x * y) % z := calc (x * (y % z)) % z = ((y % z) * x) % z : by rw mul_comm ... = (y * x) % z : by rw mul_mod_self_left ... = (x * y) % z : by rw mul_comm theorem mul_mod (x y z : ℕ) : (x * y) % z = ((x % z) * (y % z)) % z := calc (x * y) % z = ((x % z) * y) % z : by rw mul_mod_self_left ... = ((x % z) * (y % z)) % z : by rw mul_mod_self_right theorem mod_mul (x y z : ℕ) : x % (y * z) = x % y + y * ((x / y) % z) := have x % (y * z) + y * z * (x / (y * z)) = x % y + y * ((x / y) % z) + y * z * (x / (y * z)), by calc x % (y * z) + y * z * (x / (y * z)) = x : by rw nat.mod_add_div ... = x % y + y * (x / y) : by rw nat.mod_add_div ... = x % y + y * ((x / y) % z + z * (x / y / z)) : by rw nat.mod_add_div (x / y) ... = x % y + y * ((x / y) % z) + y * z * (x / y / z) : by rw [left_distrib, add_assoc, mul_assoc] ... = x % y + y * ((x / y) % z) + y * z * (x / (y * z)) : by rw nat.div_div_eq_div_mul, by { apply nat.add_right_cancel this } theorem mod_mod (x y : ℕ) : (x % y) % y = x % y := begin cases nat.eq_zero_or_pos y with h h, { rw h, simp [nat.mod_zero] }, { apply nat.mod_eq_of_lt, apply nat.mod_lt, exact h } end theorem dvd_mod_mod (x y z : ℕ) (H : z ∣ y) : (x % y) % z = x % z := match exists_eq_mul_right_of_dvd H with ⟨k, hk⟩ := calc (x % y) % z = (x % (z * k)) % z : by rw hk ... = (x % z + z * ((x / z) % k)) % z : by rw mod_mul ... = (x % z) % z : by rw nat.add_mul_mod_self_left ... = x % z : by rw mod_mod end theorem mod_div (x : ℕ) {y : ℕ} (h : y > 0) : (x % y) / y = 0 := begin apply nat.div_eq_of_lt, apply nat.mod_lt _ h end open bitvector -- (bveq (bvmul (extract 31 0 x) (extract 31 0 y)) -- (extract 31 0 (bvmul x y))) theorem mul_extract {n m : ℕ} (x y : bitvector n) (H : m ≤ n) : bvmul (trunc 0 m x) (trunc 0 m y) = (trunc 0 m (bvmul x y)) := begin apply bitvector.eq, cases x with _ _, cases y with _ _, unfold bvmul, unfold of_nat, unfold trunc, simp, rw ← mul_mod, rw dvd_mod_mod, apply modulus_dvd H, end theorem mullu_extract {n : ℕ} (x₁ x₀ y₁ y₀ : bitvector n) : bvmul x₀ y₀ = trunc 0 n (bvmul (concat x₁ x₀) (concat y₁ y₀)) := begin intros, rw ← mul_extract (concat x₁ x₀) (concat y₁ y₀), simp [trunc_concat], apply nat.le_add_right end theorem bvadd_of_nat (n a b : ℕ): bvadd (of_nat n a) (of_nat n b) = of_nat n (a + b) := begin apply bitvector.eq, unfold bvadd, unfold of_nat, simp, rw ← add_mod end -- common operations def zext_double {n : ℕ} (x : bitvector n) : bitvector (n + n) := zext (nat.le_add_right n n) x. def bvmulhu {n : ℕ} (x y : bitvector n) : bitvector n := trunc n n (bvmul (zext_double x) (zext_double y)). theorem bvmulhu_comm {n : ℕ} (x y : bitvector n) : bvmulhu x y = bvmulhu y x := begin unfold bvmulhu, rw bvmul_comm end -- (bveq (bvadd (extract 63 32 (bvmul (zero-extend x0 (bitvector 64)) -- (zero-extend y0 (bitvector 64)))) -- (bvadd (bvmul x0 y1) -- (bvmul x1 y0))) -- (extract 63 32 (bvmul (concat x1 x0) (concat y1 y0)))) lemma mod_mul_div (x y : ℕ) : x % (y * y) / y = x / y % y := begin cases nat.eq_zero_or_pos y with h h, { rw h, rw nat.mod_zero, repeat { rw nat.div_zero } }, calc x % (y * y) / y = (x % y + y * (x / y % y)) / y : by rw mod_mul ... = (x % y / y + x / y % y) : by rw nat.add_mul_div_left _ _ h ... = x / y % y : by simp [mod_div _ h] end lemma trunc_bvmul_zext {n : ℕ} {x y : bitvector n} : trunc n n (bvmul (zext_double x) (zext_double y)) = of_nat n ((to_nat x * to_nat y) / 2^n) := begin apply bitvector.eq, cases x with _ _, cases y with _ _, unfold zext_double, unfold zext, unfold bvmul, unfold of_nat, unfold trunc, simp, rw modulus_add_mul, rw mod_mul_div, rw mod_mod end lemma bvadd_bvmul {n : ℕ} (x y z w : bitvector n) : bvadd (bvmul x y) (bvmul z w) = of_nat n (to_nat x * to_nat y + to_nat z * to_nat w) := begin apply bitvector.eq, cases x with _ _, cases y with _ _, cases z with _ _, cases w with _ _, unfold bvmul, unfold bvadd, unfold of_nat, simp, rw ← add_mod end lemma trunc_bvmul_high {n : ℕ} (x y : bitvector (n + n)) : trunc n n (bvmul x y) = of_nat n ((to_nat x * to_nat y) / 2^n) := begin apply bitvector.eq, cases x with _ _, cases y with _ _, unfold bvmul, unfold of_nat, unfold trunc, simp, rw modulus_add_mul, rw mod_mul_div, rw mod_mod end lemma mul_comm_3 (x y z : ℕ) : x * y * z = x * z * y := calc x * y * z = x * (y * z) : by rw mul_assoc ... = x * (z * y) : by rw mul_comm y z ... = x * z * y : by rw ← mul_assoc theorem mul_add_div_right (x z : ℕ) {y : ℕ} (H : y > 0) : (x * y + z) / y = x + z / y := begin rw add_comm, rw nat.add_mul_div_right _ _ H, rw add_comm end theorem mul_add_mod_self_right (x y z : ℕ) : (x * y + z) % y = z % y := begin rw add_comm, apply nat.add_mul_mod_self_right end lemma two_distrib (x y z w n : ℕ) (H : n > 0): (x + y * n) * (z + w * n) / n = y * w * n + (x * w + ((y * z) + (x * z / n))) := calc (x + y * n) * (z + w * n) / n = (y * n + x) * (w * n + z) / n : by rw [add_comm x, add_comm z] ... = (y * n * (w * n) + x * (w * n) + (y * n * z + x * z)) / n : by rw [left_distrib, right_distrib, right_distrib] ... = (y * w * n * n + x * (w * n) + (y * n * z + x * z)) / n : by rw [mul_comm_3 y n (w * n), ← mul_assoc] ... = (y * w * n * n + (x * w * n + (y * n * z + x * z))) / n : by rw [add_assoc, ← mul_assoc] ... = (y * w * n * n + (x * w * n + (y * z * n + x * z))) / n : by rw mul_comm_3 y n z ... = y * w * n + (x * w + ((y * z) + (x * z / n))) : by rw [mul_add_div_right _ _ H, mul_add_div_right _ _ H, mul_add_div_right _ _ H] theorem mulhu_extract {n : ℕ} (x₁ x₀ y₁ y₀ : bitvector n) : bvadd (bvmulhu x₀ y₀) (bvadd (bvmul x₀ y₁) (bvmul x₁ y₀)) = trunc n n (bvmul (concat x₁ x₀) (concat y₁ y₀)) := begin unfold bvmulhu, rw trunc_bvmul_zext, rw bvadd_bvmul, rw bvadd_of_nat, rw trunc_bvmul_high, cases x₀ with _ _, cases x₁ with _ _, cases y₀ with _ _, cases y₁ with _ _, unfold concat, unfold of_nat, apply bitvector.eq, simp, rw two_distrib, rw mul_add_mod_self_right, apply modulus_pos end -- (bveq (concat (bvadd (bvmulhu x0 y0) -- (bvadd (bvmul x0 y1) (bvmul x1 y0))) -- (bvmul x0 y0)) -- (bvmul (concat x1 x0) (concat y1 y0))) theorem bvmul_decomp' {n : ℕ} (x₁ x₀ y₁ y₀ : bitvector n) : concat (bvadd (bvmulhu x₀ y₀) (bvadd (bvmul x₀ y₁) (bvmul x₁ y₀))) (bvmul x₀ y₀) = bvmul (concat x₁ x₀) (concat y₁ y₀) := begin intros, rw mulhu_extract, rw mullu_extract x₁ x₀ y₁ y₀, apply concat_trunc end -- (bveq (concat (bvadd (bvmulhu (extract 31 0 x) (extract 31 0 y)) -- (bvadd (bvmul (extract 31 0 x) (extract 63 32 y)) -- (bvmul (extract 63 32 x) (extract 31 0 y)))) -- (bvmul (extract 31 0 x) (extract 31 0 y))) -- (bvmul (extract 31 0 x) (extract 31 0 y))) theorem bvmul_decomp {n : ℕ} (x y : bitvector (n + n)) : concat (bvadd (bvmulhu (trunc 0 n x) (trunc 0 n y)) (bvadd (bvmul (trunc 0 n x) (trunc n n y)) (bvmul (trunc n n x) (trunc 0 n y)))) (bvmul (trunc 0 n x) (trunc 0 n y)) = bvmul x y := by simp [bvmul_decomp', concat_trunc]
98152c68a31b1bfab20461a7185990bdfa63a408
b70031c8e2c5337b91d7e70f1e0c5f528f7b0e77
/src/data/real/hyperreal.lean
ada957e500c45da9afc93a78090fc367fd93260f
[ "Apache-2.0" ]
permissive
molodiuc/mathlib
cae2ba3ef1601c1f42ca0b625c79b061b63fef5b
98ebe5a6739fbe254f9ee9d401882d4388f91035
refs/heads/master
1,674,237,127,059
1,606,353,533,000
1,606,353,533,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
37,046
lean
/- Copyright (c) 2019 Abhimanyu Pallavi Sudhir. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Abhimanyu Pallavi Sudhir -/ import order.filter.filter_product import analysis.specific_limits /-! # Construction of the hyperreal numbers as an ultraproduct of real sequences. -/ open filter filter.germ open_locale topological_space classical /-- Hyperreal numbers on the ultrafilter extending the cofinite filter -/ def hyperreal : Type := germ (@hyperfilter ℕ) ℝ namespace hyperreal notation `ℝ*` := hyperreal private def U : is_ultrafilter (@hyperfilter ℕ) := is_ultrafilter_hyperfilter noncomputable instance : linear_ordered_field ℝ* := germ.linear_ordered_field U noncomputable instance : inhabited ℝ* := ⟨0⟩ noncomputable instance : has_coe_t ℝ ℝ* := ⟨λ x, (↑x : germ _ _)⟩ @[simp, norm_cast] lemma coe_eq_coe {x y : ℝ} : (x : ℝ*) = y ↔ x = y := germ.const_inj @[simp, norm_cast] lemma coe_eq_zero {x : ℝ} : (x : ℝ*) = 0 ↔ x = 0 := coe_eq_coe @[simp, norm_cast] lemma coe_eq_one {x : ℝ} : (x : ℝ*) = 1 ↔ x = 1 := coe_eq_coe @[simp, norm_cast] lemma coe_one : ↑(1 : ℝ) = (1 : ℝ*) := rfl @[simp, norm_cast] lemma coe_zero : ↑(0 : ℝ) = (0 : ℝ*) := rfl @[simp, norm_cast] lemma coe_inv (x : ℝ) : ↑(x⁻¹) = (x⁻¹ : ℝ*) := rfl @[simp, norm_cast] lemma coe_neg (x : ℝ) : ↑(-x) = (-x : ℝ*) := rfl @[simp, norm_cast] lemma coe_add (x y : ℝ) : ↑(x + y) = (x + y : ℝ*) := rfl @[simp, norm_cast] lemma coe_bit0 (x : ℝ) : ↑(bit0 x) = (bit0 x : ℝ*) := rfl @[simp, norm_cast] lemma coe_bit1 (x : ℝ) : ↑(bit1 x) = (bit1 x : ℝ*) := rfl @[simp, norm_cast] lemma coe_mul (x y : ℝ) : ↑(x * y) = (x * y : ℝ*) := rfl @[simp, norm_cast] lemma coe_div (x y : ℝ) : ↑(x / y) = (x / y : ℝ*) := rfl @[simp, norm_cast] lemma coe_sub (x y : ℝ) : ↑(x - y) = (x - y : ℝ*) := rfl @[simp, norm_cast] lemma coe_lt_coe {x y : ℝ} : (x : ℝ*) < y ↔ x < y := germ.const_lt U @[simp, norm_cast] lemma coe_pos {x : ℝ} : 0 < (x : ℝ*) ↔ 0 < x := coe_lt_coe @[simp, norm_cast] lemma coe_le_coe {x y : ℝ} : (x : ℝ*) ≤ y ↔ x ≤ y := germ.const_le_iff @[simp, norm_cast] lemma coe_abs (x : ℝ) : ((abs x : ℝ) : ℝ*) = abs x := germ.const_abs U _ @[simp, norm_cast] lemma coe_max (x y : ℝ) : ((max x y : ℝ) : ℝ*) = max x y := germ.const_max U _ _ @[simp, norm_cast] lemma coe_min (x y : ℝ) : ((min x y : ℝ) : ℝ*) = min x y := germ.const_min U _ _ /-- Construct a hyperreal number from a sequence of real numbers. -/ noncomputable def of_seq (f : ℕ → ℝ) : ℝ* := (↑f : germ (@hyperfilter ℕ) ℝ) /-- A sample infinitesimal hyperreal-/ noncomputable def epsilon : ℝ* := of_seq $ λ n, n⁻¹ /-- A sample infinite hyperreal-/ noncomputable def omega : ℝ* := of_seq coe localized "notation `ε` := hyperreal.epsilon" in hyperreal localized "notation `ω` := hyperreal.omega" in hyperreal lemma epsilon_eq_inv_omega : ε = ω⁻¹ := rfl lemma inv_epsilon_eq_omega : ε⁻¹ = ω := @inv_inv' _ _ ω lemma epsilon_pos : 0 < ε := suffices ∀ᶠ i in hyperfilter, (0 : ℝ) < (i : ℕ)⁻¹, by rwa lt_def U, have h0' : {n : ℕ | ¬ 0 < n} = {0} := by simp only [not_lt, (set.set_of_eq_eq_singleton).symm]; ext; exact nat.le_zero_iff, begin simp only [inv_pos, nat.cast_pos], exact mem_hyperfilter_of_finite_compl (by convert set.finite_singleton _), end lemma epsilon_ne_zero : ε ≠ 0 := ne_of_gt epsilon_pos lemma omega_pos : 0 < ω := by rw ←inv_epsilon_eq_omega; exact inv_pos.2 epsilon_pos lemma omega_ne_zero : ω ≠ 0 := ne_of_gt omega_pos theorem epsilon_mul_omega : ε * ω = 1 := @inv_mul_cancel _ _ ω omega_ne_zero lemma lt_of_tendsto_zero_of_pos {f : ℕ → ℝ} (hf : tendsto f at_top (𝓝 0)) : ∀ {r : ℝ}, 0 < r → of_seq f < (r : ℝ*) := begin simp only [metric.tendsto_at_top, dist_zero_right, norm, lt_def U] at hf ⊢, intros r hr, cases hf r hr with N hf', have hs : {i : ℕ | f i < r}ᶜ ⊆ {i : ℕ | i ≤ N} := λ i hi1, le_of_lt (by simp only [lt_iff_not_ge]; exact λ hi2, hi1 (lt_of_le_of_lt (le_abs_self _) (hf' i hi2)) : i < N), exact mem_hyperfilter_of_finite_compl ((set.finite_le_nat N).subset hs) end lemma neg_lt_of_tendsto_zero_of_pos {f : ℕ → ℝ} (hf : tendsto f at_top (𝓝 0)) : ∀ {r : ℝ}, 0 < r → (-r : ℝ*) < of_seq f := λ r hr, have hg : _ := hf.neg, neg_lt_of_neg_lt (by rw [neg_zero] at hg; exact lt_of_tendsto_zero_of_pos hg hr) lemma gt_of_tendsto_zero_of_neg {f : ℕ → ℝ} (hf : tendsto f at_top (𝓝 0)) : ∀ {r : ℝ}, r < 0 → (r : ℝ*) < of_seq f := λ r hr, by rw [←neg_neg r, coe_neg]; exact neg_lt_of_tendsto_zero_of_pos hf (neg_pos.mpr hr) lemma epsilon_lt_pos (x : ℝ) : 0 < x → ε < x := lt_of_tendsto_zero_of_pos tendsto_inverse_at_top_nhds_0_nat /-- Standard part predicate -/ def is_st (x : ℝ*) (r : ℝ) := ∀ δ : ℝ, 0 < δ → (r - δ : ℝ*) < x ∧ x < r + δ /-- Standard part function: like a "round" to ℝ instead of ℤ -/ noncomputable def st : ℝ* → ℝ := λ x, if h : ∃ r, is_st x r then classical.some h else 0 /-- A hyperreal number is infinitesimal if its standard part is 0 -/ def infinitesimal (x : ℝ*) := is_st x 0 /-- A hyperreal number is positive infinite if it is larger than all real numbers -/ def infinite_pos (x : ℝ*) := ∀ r : ℝ, ↑r < x /-- A hyperreal number is negative infinite if it is smaller than all real numbers -/ def infinite_neg (x : ℝ*) := ∀ r : ℝ, x < r /-- A hyperreal number is infinite if it is infinite positive or infinite negative -/ def infinite (x : ℝ*) := infinite_pos x ∨ infinite_neg x /-! ### Some facts about `st` -/ private lemma is_st_unique' (x : ℝ*) (r s : ℝ) (hr : is_st x r) (hs : is_st x s) (hrs : r < s) : false := have hrs' : _ := half_pos $ sub_pos_of_lt hrs, have hr' : _ := (hr _ hrs').2, have hs' : _ := (hs _ hrs').1, have h : s - ((s - r) / 2) = r + (s - r) / 2 := by linarith, begin norm_cast at *, rw h at hs', exact not_lt_of_lt hs' hr' end theorem is_st_unique {x : ℝ*} {r s : ℝ} (hr : is_st x r) (hs : is_st x s) : r = s := begin rcases lt_trichotomy r s with h | h | h, { exact false.elim (is_st_unique' x r s hr hs h) }, { exact h }, { exact false.elim (is_st_unique' x s r hs hr h) } end theorem not_infinite_of_exists_st {x : ℝ*} : (∃ r : ℝ, is_st x r) → ¬ infinite x := λ he hi, Exists.dcases_on he $ λ r hr, hi.elim (λ hip, not_lt_of_lt (hr 2 zero_lt_two).2 (hip $ r + 2)) (λ hin, not_lt_of_lt (hr 2 zero_lt_two).1 (hin $ r - 2)) theorem is_st_Sup {x : ℝ*} (hni : ¬ infinite x) : is_st x (Sup {y : ℝ | (y : ℝ*) < x}) := let S : set ℝ := {y : ℝ | (y : ℝ*) < x} in let R : _ := Sup S in have hnile : _ := not_forall.mp (not_or_distrib.mp hni).1, have hnige : _ := not_forall.mp (not_or_distrib.mp hni).2, Exists.dcases_on hnile $ Exists.dcases_on hnige $ λ r₁ hr₁ r₂ hr₂, have HR₁ : ∃ y : ℝ, y ∈ S := ⟨r₁ - 1, lt_of_lt_of_le (coe_lt_coe.2 $ sub_one_lt _) (not_lt.mp hr₁) ⟩, have HR₂ : ∃ z : ℝ, ∀ y ∈ S, y ≤ z := ⟨ r₂, λ y hy, le_of_lt (coe_lt_coe.1 (lt_of_lt_of_le hy (not_lt.mp hr₂))) ⟩, λ δ hδ, ⟨ lt_of_not_ge' $ λ c, have hc : ∀ y ∈ S, y ≤ R - δ := λ y hy, coe_le_coe.1 $ le_of_lt $ lt_of_lt_of_le hy c, not_lt_of_le ((real.Sup_le _ HR₁ HR₂).mpr hc) $ sub_lt_self R hδ, lt_of_not_ge' $ λ c, have hc : ↑(R + δ / 2) < x := lt_of_lt_of_le (add_lt_add_left (coe_lt_coe.2 (half_lt_self hδ)) R) c, not_lt_of_le (real.le_Sup _ HR₂ hc) $ (lt_add_iff_pos_right _).mpr $ half_pos hδ⟩ theorem exists_st_of_not_infinite {x : ℝ*} (hni : ¬ infinite x) : ∃ r : ℝ, is_st x r := ⟨Sup {y : ℝ | (y : ℝ*) < x}, is_st_Sup hni⟩ theorem st_eq_Sup {x : ℝ*} : st x = Sup {y : ℝ | (y : ℝ*) < x} := begin unfold st, split_ifs, { exact is_st_unique (classical.some_spec h) (is_st_Sup (not_infinite_of_exists_st h)) }, { cases not_imp_comm.mp exists_st_of_not_infinite h with H H, { rw (set.ext (λ i, ⟨λ hi, set.mem_univ i, λ hi, H i⟩) : {y : ℝ | (y : ℝ*) < x} = set.univ), exact (real.Sup_univ).symm }, { rw (set.ext (λ i, ⟨λ hi, false.elim (not_lt_of_lt (H i) hi), λ hi, false.elim (set.not_mem_empty i hi)⟩) : {y : ℝ | (y : ℝ*) < x} = ∅), exact (real.Sup_empty).symm } } end theorem exists_st_iff_not_infinite {x : ℝ*} : (∃ r : ℝ, is_st x r) ↔ ¬ infinite x := ⟨ not_infinite_of_exists_st, exists_st_of_not_infinite ⟩ theorem infinite_iff_not_exists_st {x : ℝ*} : infinite x ↔ ¬ ∃ r : ℝ, is_st x r := iff_not_comm.mp exists_st_iff_not_infinite theorem st_infinite {x : ℝ*} (hi : infinite x) : st x = 0 := begin unfold st, split_ifs, { exact false.elim ((infinite_iff_not_exists_st.mp hi) h) }, { refl } end lemma st_of_is_st {x : ℝ*} {r : ℝ} (hxr : is_st x r) : st x = r := begin unfold st, split_ifs, { exact is_st_unique (classical.some_spec h) hxr }, { exact false.elim (h ⟨r, hxr⟩) } end lemma is_st_st_of_is_st {x : ℝ*} {r : ℝ} (hxr : is_st x r) : is_st x (st x) := by rwa [st_of_is_st hxr] lemma is_st_st_of_exists_st {x : ℝ*} (hx : ∃ r : ℝ, is_st x r) : is_st x (st x) := Exists.dcases_on hx (λ r, is_st_st_of_is_st) lemma is_st_st {x : ℝ*} (hx : st x ≠ 0) : is_st x (st x) := begin unfold st, split_ifs, { exact classical.some_spec h }, { exact false.elim (hx (by unfold st; split_ifs; refl)) } end lemma is_st_st' {x : ℝ*} (hx : ¬ infinite x) : is_st x (st x) := is_st_st_of_exists_st $ exists_st_of_not_infinite hx lemma is_st_refl_real (r : ℝ) : is_st r r := λ δ hδ, ⟨ sub_lt_self _ (coe_lt_coe.2 hδ), (lt_add_of_pos_right _ (coe_lt_coe.2 hδ)) ⟩ lemma st_id_real (r : ℝ) : st r = r := st_of_is_st (is_st_refl_real r) lemma eq_of_is_st_real {r s : ℝ} : is_st r s → r = s := is_st_unique (is_st_refl_real r) lemma is_st_real_iff_eq {r s : ℝ} : is_st r s ↔ r = s := ⟨eq_of_is_st_real, λ hrs, by rw [hrs]; exact is_st_refl_real s⟩ lemma is_st_symm_real {r s : ℝ} : is_st r s ↔ is_st s r := by rw [is_st_real_iff_eq, is_st_real_iff_eq, eq_comm] lemma is_st_trans_real {r s t : ℝ} : is_st r s → is_st s t → is_st r t := by rw [is_st_real_iff_eq, is_st_real_iff_eq, is_st_real_iff_eq]; exact eq.trans lemma is_st_inj_real {r₁ r₂ s : ℝ} (h1 : is_st r₁ s) (h2 : is_st r₂ s) : r₁ = r₂ := eq.trans (eq_of_is_st_real h1) (eq_of_is_st_real h2).symm lemma is_st_iff_abs_sub_lt_delta {x : ℝ*} {r : ℝ} : is_st x r ↔ ∀ (δ : ℝ), 0 < δ → abs (x - r) < δ := by simp only [abs_sub_lt_iff, @sub_lt _ _ (r : ℝ*) x _, @sub_lt_iff_lt_add' _ _ x (r : ℝ*) _, and_comm]; refl lemma is_st_add {x y : ℝ*} {r s : ℝ} : is_st x r → is_st y s → is_st (x + y) (r + s) := λ hxr hys d hd, have hxr' : _ := hxr (d / 2) (half_pos hd), have hys' : _ := hys (d / 2) (half_pos hd), ⟨by convert add_lt_add hxr'.1 hys'.1 using 1; norm_cast; linarith, by convert add_lt_add hxr'.2 hys'.2 using 1; norm_cast; linarith⟩ lemma is_st_neg {x : ℝ*} {r : ℝ} (hxr : is_st x r) : is_st (-x) (-r) := λ d hd, by show -(r : ℝ*) - d < -x ∧ -x < -r + d; cases (hxr d hd); split; linarith lemma is_st_sub {x y : ℝ*} {r s : ℝ} : is_st x r → is_st y s → is_st (x - y) (r - s) := λ hxr hys, by rw [sub_eq_add_neg, sub_eq_add_neg]; exact is_st_add hxr (is_st_neg hys) /- (st x < st y) → (x < y) → (x ≤ y) → (st x ≤ st y) -/ lemma lt_of_is_st_lt {x y : ℝ*} {r s : ℝ} (hxr : is_st x r) (hys : is_st y s) : r < s → x < y := λ hrs, have hrs' : 0 < (s - r) / 2 := half_pos (sub_pos.mpr hrs), have hxr' : _ := (hxr _ hrs').2, have hys' : _ := (hys _ hrs').1, have H1 : r + ((s - r) / 2) = (r + s) / 2 := by linarith, have H2 : s - ((s - r) / 2) = (r + s) / 2 := by linarith, begin norm_cast at *, rw H1 at hxr', rw H2 at hys', exact lt_trans hxr' hys' end lemma is_st_le_of_le {x y : ℝ*} {r s : ℝ} (hrx : is_st x r) (hsy : is_st y s) : x ≤ y → r ≤ s := by rw [←not_lt, ←not_lt, not_imp_not]; exact lt_of_is_st_lt hsy hrx lemma st_le_of_le {x y : ℝ*} (hix : ¬ infinite x) (hiy : ¬ infinite y) : x ≤ y → st x ≤ st y := have hx' : _ := is_st_st' hix, have hy' : _ := is_st_st' hiy, is_st_le_of_le hx' hy' lemma lt_of_st_lt {x y : ℝ*} (hix : ¬ infinite x) (hiy : ¬ infinite y) : st x < st y → x < y := have hx' : _ := is_st_st' hix, have hy' : _ := is_st_st' hiy, lt_of_is_st_lt hx' hy' /-! ### Basic lemmas about infinite -/ lemma infinite_pos_def {x : ℝ*} : infinite_pos x ↔ ∀ r : ℝ, ↑r < x := by rw iff_eq_eq; refl lemma infinite_neg_def {x : ℝ*} : infinite_neg x ↔ ∀ r : ℝ, x < r := by rw iff_eq_eq; refl lemma ne_zero_of_infinite {x : ℝ*} : infinite x → x ≠ 0 := λ hI h0, or.cases_on hI (λ hip, lt_irrefl (0 : ℝ*) ((by rwa ←h0 : infinite_pos 0) 0)) (λ hin, lt_irrefl (0 : ℝ*) ((by rwa ←h0 : infinite_neg 0) 0)) lemma not_infinite_zero : ¬ infinite 0 := λ hI, ne_zero_of_infinite hI rfl lemma pos_of_infinite_pos {x : ℝ*} : infinite_pos x → 0 < x := λ hip, hip 0 lemma neg_of_infinite_neg {x : ℝ*} : infinite_neg x → x < 0 := λ hin, hin 0 lemma not_infinite_pos_of_infinite_neg {x : ℝ*} : infinite_neg x → ¬ infinite_pos x := λ hn hp, not_lt_of_lt (hn 1) (hp 1) lemma not_infinite_neg_of_infinite_pos {x : ℝ*} : infinite_pos x → ¬ infinite_neg x := imp_not_comm.mp not_infinite_pos_of_infinite_neg lemma infinite_neg_neg_of_infinite_pos {x : ℝ*} : infinite_pos x → infinite_neg (-x) := λ hp r, neg_lt.mp (hp (-r)) lemma infinite_pos_neg_of_infinite_neg {x : ℝ*} : infinite_neg x → infinite_pos (-x) := λ hp r, lt_neg.mp (hp (-r)) lemma infinite_pos_iff_infinite_neg_neg {x : ℝ*} : infinite_pos x ↔ infinite_neg (-x) := ⟨ infinite_neg_neg_of_infinite_pos, λ hin, neg_neg x ▸ infinite_pos_neg_of_infinite_neg hin ⟩ lemma infinite_neg_iff_infinite_pos_neg {x : ℝ*} : infinite_neg x ↔ infinite_pos (-x) := ⟨ infinite_pos_neg_of_infinite_neg, λ hin, neg_neg x ▸ infinite_neg_neg_of_infinite_pos hin ⟩ lemma infinite_iff_infinite_neg {x : ℝ*} : infinite x ↔ infinite (-x) := ⟨ λ hi, or.cases_on hi (λ hip, or.inr (infinite_neg_neg_of_infinite_pos hip)) (λ hin, or.inl (infinite_pos_neg_of_infinite_neg hin)), λ hi, or.cases_on hi (λ hipn, or.inr (infinite_neg_iff_infinite_pos_neg.mpr hipn)) (λ hinp, or.inl (infinite_pos_iff_infinite_neg_neg.mpr hinp))⟩ lemma not_infinite_of_infinitesimal {x : ℝ*} : infinitesimal x → ¬ infinite x := λ hi hI, have hi' : _ := (hi 2 zero_lt_two), or.dcases_on hI (λ hip, have hip' : _ := hip 2, not_lt_of_lt hip' (by convert hi'.2; exact (zero_add 2).symm)) (λ hin, have hin' : _ := hin (-2), not_lt_of_lt hin' (by convert hi'.1; exact (zero_sub 2).symm)) lemma not_infinitesimal_of_infinite {x : ℝ*} : infinite x → ¬ infinitesimal x := imp_not_comm.mp not_infinite_of_infinitesimal lemma not_infinitesimal_of_infinite_pos {x : ℝ*} : infinite_pos x → ¬ infinitesimal x := λ hp, not_infinitesimal_of_infinite (or.inl hp) lemma not_infinitesimal_of_infinite_neg {x : ℝ*} : infinite_neg x → ¬ infinitesimal x := λ hn, not_infinitesimal_of_infinite (or.inr hn) lemma infinite_pos_iff_infinite_and_pos {x : ℝ*} : infinite_pos x ↔ (infinite x ∧ 0 < x) := ⟨ λ hip, ⟨or.inl hip, hip 0⟩, λ ⟨hi, hp⟩, hi.cases_on (λ hip, hip) (λ hin, false.elim (not_lt_of_lt hp (hin 0))) ⟩ lemma infinite_neg_iff_infinite_and_neg {x : ℝ*} : infinite_neg x ↔ (infinite x ∧ x < 0) := ⟨ λ hip, ⟨or.inr hip, hip 0⟩, λ ⟨hi, hp⟩, hi.cases_on (λ hin, false.elim (not_lt_of_lt hp (hin 0))) (λ hip, hip) ⟩ lemma infinite_pos_iff_infinite_of_pos {x : ℝ*} (hp : 0 < x) : infinite_pos x ↔ infinite x := by rw [infinite_pos_iff_infinite_and_pos]; exact ⟨λ hI, hI.1, λ hI, ⟨hI, hp⟩⟩ lemma infinite_pos_iff_infinite_of_nonneg {x : ℝ*} (hp : 0 ≤ x) : infinite_pos x ↔ infinite x := or.cases_on (lt_or_eq_of_le hp) (infinite_pos_iff_infinite_of_pos) (λ h, by rw h.symm; exact ⟨λ hIP, false.elim (not_infinite_zero (or.inl hIP)), λ hI, false.elim (not_infinite_zero hI)⟩) lemma infinite_neg_iff_infinite_of_neg {x : ℝ*} (hn : x < 0) : infinite_neg x ↔ infinite x := by rw [infinite_neg_iff_infinite_and_neg]; exact ⟨λ hI, hI.1, λ hI, ⟨hI, hn⟩⟩ lemma infinite_pos_abs_iff_infinite_abs {x : ℝ*} : infinite_pos (abs x) ↔ infinite (abs x) := infinite_pos_iff_infinite_of_nonneg (abs_nonneg _) lemma infinite_iff_infinite_pos_abs {x : ℝ*} : infinite x ↔ infinite_pos (abs x) := ⟨ λ hi d, or.cases_on hi (λ hip, by rw [abs_of_pos (hip 0)]; exact hip d) (λ hin, by rw [abs_of_neg (hin 0)]; exact lt_neg.mp (hin (-d))), λ hipa, by { rcases (lt_trichotomy x 0) with h | h | h, { exact or.inr (infinite_neg_iff_infinite_pos_neg.mpr (by rwa abs_of_neg h at hipa)) }, { exact false.elim (ne_zero_of_infinite (or.inl (by rw [h]; rwa [h, abs_zero] at hipa)) h) }, { exact or.inl (by rwa abs_of_pos h at hipa) } } ⟩ lemma infinite_iff_infinite_abs {x : ℝ*} : infinite x ↔ infinite (abs x) := by rw [←infinite_pos_iff_infinite_of_nonneg (abs_nonneg _), infinite_iff_infinite_pos_abs] lemma infinite_iff_abs_lt_abs {x : ℝ*} : infinite x ↔ ∀ r : ℝ, (abs r : ℝ*) < abs x := ⟨ λ hI r, (coe_abs r) ▸ infinite_iff_infinite_pos_abs.mp hI (abs r), λ hR, or.cases_on (max_choice x (-x)) (λ h, or.inl $ λ r, lt_of_le_of_lt (le_abs_self _) (h ▸ (hR r))) (λ h, or.inr $ λ r, neg_lt_neg_iff.mp $ lt_of_le_of_lt (neg_le_abs_self _) (h ▸ (hR r)))⟩ lemma infinite_pos_add_not_infinite_neg {x y : ℝ*} : infinite_pos x → ¬ infinite_neg y → infinite_pos (x + y) := begin intros hip hnin r, cases not_forall.mp hnin with r₂ hr₂, convert add_lt_add_of_lt_of_le (hip (r + -r₂)) (not_lt.mp hr₂) using 1, simp end lemma not_infinite_neg_add_infinite_pos {x y : ℝ*} : ¬ infinite_neg x → infinite_pos y → infinite_pos (x + y) := λ hx hy, by rw [add_comm]; exact infinite_pos_add_not_infinite_neg hy hx lemma infinite_neg_add_not_infinite_pos {x y : ℝ*} : infinite_neg x → ¬ infinite_pos y → infinite_neg (x + y) := by rw [@infinite_neg_iff_infinite_pos_neg x, @infinite_pos_iff_infinite_neg_neg y, @infinite_neg_iff_infinite_pos_neg (x + y), neg_add]; exact infinite_pos_add_not_infinite_neg lemma not_infinite_pos_add_infinite_neg {x y : ℝ*} : ¬ infinite_pos x → infinite_neg y → infinite_neg (x + y) := λ hx hy, by rw [add_comm]; exact infinite_neg_add_not_infinite_pos hy hx lemma infinite_pos_add_infinite_pos {x y : ℝ*} : infinite_pos x → infinite_pos y → infinite_pos (x + y) := λ hx hy, infinite_pos_add_not_infinite_neg hx (not_infinite_neg_of_infinite_pos hy) lemma infinite_neg_add_infinite_neg {x y : ℝ*} : infinite_neg x → infinite_neg y → infinite_neg (x + y) := λ hx hy, infinite_neg_add_not_infinite_pos hx (not_infinite_pos_of_infinite_neg hy) lemma infinite_pos_add_not_infinite {x y : ℝ*} : infinite_pos x → ¬ infinite y → infinite_pos (x + y) := λ hx hy, infinite_pos_add_not_infinite_neg hx (not_or_distrib.mp hy).2 lemma infinite_neg_add_not_infinite {x y : ℝ*} : infinite_neg x → ¬ infinite y → infinite_neg (x + y) := λ hx hy, infinite_neg_add_not_infinite_pos hx (not_or_distrib.mp hy).1 theorem infinite_pos_of_tendsto_top {f : ℕ → ℝ} (hf : tendsto f at_top at_top) : infinite_pos (of_seq f) := λ r, have hf' : _ := tendsto_at_top_at_top.mp hf, Exists.cases_on (hf' (r + 1)) $ λ i hi, have hi' : ∀ (a : ℕ), f a < (r + 1) → a < i := λ a, by rw [←not_le, ←not_le]; exact not_imp_not.mpr (hi a), have hS : {a : ℕ | r < f a}ᶜ ⊆ {a : ℕ | a ≤ i} := by simp only [set.compl_set_of, not_lt]; exact λ a har, le_of_lt (hi' a (lt_of_le_of_lt har (lt_add_one _))), (germ.coe_lt U).2 $ mem_hyperfilter_of_finite_compl $ (set.finite_le_nat _).subset hS theorem infinite_neg_of_tendsto_bot {f : ℕ → ℝ} (hf : tendsto f at_top at_bot) : infinite_neg (of_seq f) := λ r, have hf' : _ := tendsto_at_top_at_bot.mp hf, Exists.cases_on (hf' (r - 1)) $ λ i hi, have hi' : ∀ (a : ℕ), r - 1 < f a → a < i := λ a, by rw [←not_le, ←not_le]; exact not_imp_not.mpr (hi a), have hS : {a : ℕ | f a < r}ᶜ ⊆ {a : ℕ | a ≤ i} := by simp only [set.compl_set_of, not_lt]; exact λ a har, le_of_lt (hi' a (lt_of_lt_of_le (sub_one_lt _) har)), (germ.coe_lt U).2 $ mem_hyperfilter_of_finite_compl $ (set.finite_le_nat _).subset hS lemma not_infinite_neg {x : ℝ*} : ¬ infinite x → ¬ infinite (-x) := not_imp_not.mpr infinite_iff_infinite_neg.mpr lemma not_infinite_add {x y : ℝ*} (hx : ¬ infinite x) (hy : ¬ infinite y) : ¬ infinite (x + y) := have hx' : _ := exists_st_of_not_infinite hx, have hy' : _ := exists_st_of_not_infinite hy, Exists.cases_on hx' $ Exists.cases_on hy' $ λ r hr s hs, not_infinite_of_exists_st $ ⟨s + r, is_st_add hs hr⟩ theorem not_infinite_iff_exist_lt_gt {x : ℝ*} : ¬ infinite x ↔ ∃ r s : ℝ, (r : ℝ*) < x ∧ x < s := ⟨ λ hni, Exists.dcases_on (not_forall.mp (not_or_distrib.mp hni).1) $ Exists.dcases_on (not_forall.mp (not_or_distrib.mp hni).2) $ λ r hr s hs, by rw [not_lt] at hr hs; exact ⟨r - 1, s + 1, ⟨ lt_of_lt_of_le (by rw sub_eq_add_neg; norm_num) hr, lt_of_le_of_lt hs (by norm_num)⟩ ⟩, λ hrs, Exists.dcases_on hrs $ λ r hr, Exists.dcases_on hr $ λ s hs, not_or_distrib.mpr ⟨not_forall.mpr ⟨s, lt_asymm (hs.2)⟩, not_forall.mpr ⟨r, lt_asymm (hs.1) ⟩⟩⟩ theorem not_infinite_real (r : ℝ) : ¬ infinite r := by rw not_infinite_iff_exist_lt_gt; exact ⟨ r - 1, r + 1, coe_lt_coe.2 $ sub_one_lt r, coe_lt_coe.2 $ lt_add_one r⟩ theorem not_real_of_infinite {x : ℝ*} : infinite x → ∀ r : ℝ, x ≠ r := λ hi r hr, not_infinite_real r $ @eq.subst _ infinite _ _ hr hi /-! ### Facts about `st` that require some infinite machinery -/ private lemma is_st_mul' {x y : ℝ*} {r s : ℝ} (hxr : is_st x r) (hys : is_st y s) (hs : s ≠ 0) : is_st (x * y) (r * s) := have hxr' : _ := is_st_iff_abs_sub_lt_delta.mp hxr, have hys' : _ := is_st_iff_abs_sub_lt_delta.mp hys, have h : _ := not_infinite_iff_exist_lt_gt.mp $ not_imp_not.mpr infinite_iff_infinite_abs.mpr $ not_infinite_of_exists_st ⟨r, hxr⟩, Exists.cases_on h $ λ u h', Exists.cases_on h' $ λ t ⟨hu, ht⟩, is_st_iff_abs_sub_lt_delta.mpr $ λ d hd, calc abs (x * y - r * s) = abs (x * (y - s) + (x - r) * s) : by rw [mul_sub, sub_mul, add_sub, sub_add_cancel] ... ≤ abs (x * (y - s)) + abs ((x - r) * s) : abs_add _ _ ... ≤ abs x * abs (y - s) + abs (x - r) * abs s : by simp only [abs_mul] ... ≤ abs x * ((d / t) / 2 : ℝ) + ((d / abs s) / 2 : ℝ) * abs s : add_le_add (mul_le_mul_of_nonneg_left (le_of_lt $ hys' _ $ half_pos $ div_pos hd $ coe_pos.1 $ lt_of_le_of_lt (abs_nonneg x) ht) $ abs_nonneg _) (mul_le_mul_of_nonneg_right (le_of_lt $ hxr' _ $ half_pos $ div_pos hd $ abs_pos.2 hs) $ abs_nonneg _) ... = (d / 2 * (abs x / t) + d / 2 : ℝ*) : by { push_cast, have : (abs s : ℝ*) ≠ 0, by simpa, field_simp [this, @two_ne_zero ℝ* _, add_mul, mul_add, mul_assoc, mul_comm, mul_left_comm] } ... < (d / 2 * 1 + d / 2 : ℝ*) : add_lt_add_right (mul_lt_mul_of_pos_left ((div_lt_one $ lt_of_le_of_lt (abs_nonneg x) ht).mpr ht) $ half_pos $ coe_pos.2 hd) _ ... = (d : ℝ*) : by rw [mul_one, add_halves] lemma is_st_mul {x y : ℝ*} {r s : ℝ} (hxr : is_st x r) (hys : is_st y s) : is_st (x * y) (r * s) := have h : _ := not_infinite_iff_exist_lt_gt.mp $ not_imp_not.mpr infinite_iff_infinite_abs.mpr $ not_infinite_of_exists_st ⟨r, hxr⟩, Exists.cases_on h $ λ u h', Exists.cases_on h' $ λ t ⟨hu, ht⟩, begin by_cases hs : s = 0, { apply is_st_iff_abs_sub_lt_delta.mpr, intros d hd, have hys' : _ := is_st_iff_abs_sub_lt_delta.mp hys (d / t) (div_pos hd (coe_pos.1 (lt_of_le_of_lt (abs_nonneg x) ht))), rw [hs, coe_zero, sub_zero] at hys', rw [hs, mul_zero, coe_zero, sub_zero, abs_mul, mul_comm, ←div_mul_cancel (d : ℝ*) (ne_of_gt (lt_of_le_of_lt (abs_nonneg x) ht)), ←coe_div], exact mul_lt_mul'' hys' ht (abs_nonneg _) (abs_nonneg _) }, exact is_st_mul' hxr hys hs, end --AN INFINITE LEMMA THAT REQUIRES SOME MORE ST MACHINERY lemma not_infinite_mul {x y : ℝ*} (hx : ¬ infinite x) (hy : ¬ infinite y) : ¬ infinite (x * y) := have hx' : _ := exists_st_of_not_infinite hx, have hy' : _ := exists_st_of_not_infinite hy, Exists.cases_on hx' $ Exists.cases_on hy' $ λ r hr s hs, not_infinite_of_exists_st $ ⟨s * r, is_st_mul hs hr⟩ --- lemma st_add {x y : ℝ*} (hx : ¬infinite x) (hy : ¬infinite y) : st (x + y) = st x + st y := have hx' : _ := is_st_st' hx, have hy' : _ := is_st_st' hy, have hxy : _ := is_st_st' (not_infinite_add hx hy), have hxy' : _ := is_st_add hx' hy', is_st_unique hxy hxy' lemma st_neg (x : ℝ*) : st (-x) = - st x := if h : infinite x then by rw [st_infinite h, st_infinite (infinite_iff_infinite_neg.mp h), neg_zero] else is_st_unique (is_st_st' (not_infinite_neg h)) (is_st_neg (is_st_st' h)) lemma st_mul {x y : ℝ*} (hx : ¬infinite x) (hy : ¬infinite y) : st (x * y) = (st x) * (st y) := have hx' : _ := is_st_st' hx, have hy' : _ := is_st_st' hy, have hxy : _ := is_st_st' (not_infinite_mul hx hy), have hxy' : _ := is_st_mul hx' hy', is_st_unique hxy hxy' /-! ### Basic lemmas about infinitesimal -/ theorem infinitesimal_def {x : ℝ*} : infinitesimal x ↔ (∀ r : ℝ, 0 < r → -(r : ℝ*) < x ∧ x < r) := ⟨ λ hi r hr, by { convert (hi r hr); simp }, λ hi d hd, by { convert (hi d hd); simp } ⟩ theorem lt_of_pos_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, 0 < r → x < r := λ hi r hr, ((infinitesimal_def.mp hi) r hr).2 theorem lt_neg_of_pos_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, 0 < r → -↑r < x := λ hi r hr, ((infinitesimal_def.mp hi) r hr).1 theorem gt_of_neg_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, r < 0 → ↑r < x := λ hi r hr, by convert ((infinitesimal_def.mp hi) (-r) (neg_pos.mpr hr)).1; exact (neg_neg ↑r).symm theorem abs_lt_real_iff_infinitesimal {x : ℝ*} : infinitesimal x ↔ ∀ r : ℝ, r ≠ 0 → abs x < abs r := ⟨ λ hi r hr, abs_lt.mpr (by rw ←coe_abs; exact infinitesimal_def.mp hi (abs r) (abs_pos.2 hr)), λ hR, infinitesimal_def.mpr $ λ r hr, abs_lt.mp $ (abs_of_pos $ coe_pos.2 hr) ▸ hR r $ ne_of_gt hr ⟩ lemma infinitesimal_zero : infinitesimal 0 := is_st_refl_real 0 lemma zero_of_infinitesimal_real {r : ℝ} : infinitesimal r → r = 0 := eq_of_is_st_real lemma zero_iff_infinitesimal_real {r : ℝ} : infinitesimal r ↔ r = 0 := ⟨zero_of_infinitesimal_real, λ hr, by rw hr; exact infinitesimal_zero⟩ lemma infinitesimal_add {x y : ℝ*} (hx : infinitesimal x) (hy : infinitesimal y) : infinitesimal (x + y) := by simpa only [add_zero] using is_st_add hx hy lemma infinitesimal_neg {x : ℝ*} (hx : infinitesimal x) : infinitesimal (-x) := by simpa only [neg_zero] using is_st_neg hx lemma infinitesimal_neg_iff {x : ℝ*} : infinitesimal x ↔ infinitesimal (-x) := ⟨infinitesimal_neg, λ h, (neg_neg x) ▸ @infinitesimal_neg (-x) h⟩ lemma infinitesimal_mul {x y : ℝ*} (hx : infinitesimal x) (hy : infinitesimal y) : infinitesimal (x * y) := by simpa only [mul_zero] using is_st_mul hx hy theorem infinitesimal_of_tendsto_zero {f : ℕ → ℝ} : tendsto f at_top (𝓝 0) → infinitesimal (of_seq f) := λ hf d hd, by rw [sub_eq_add_neg, ←coe_neg, ←coe_add, ←coe_add, zero_add, zero_add]; exact ⟨neg_lt_of_tendsto_zero_of_pos hf hd, lt_of_tendsto_zero_of_pos hf hd⟩ theorem infinitesimal_epsilon : infinitesimal ε := infinitesimal_of_tendsto_zero tendsto_inverse_at_top_nhds_0_nat lemma not_real_of_infinitesimal_ne_zero (x : ℝ*) : infinitesimal x → x ≠ 0 → ∀ r : ℝ, x ≠ r := λ hi hx r hr, hx $ hr.trans $ coe_eq_zero.2 $ is_st_unique (hr.symm ▸ is_st_refl_real r : is_st x r) hi theorem infinitesimal_sub_is_st {x : ℝ*} {r : ℝ} (hxr : is_st x r) : infinitesimal (x - r) := show is_st (x + -r) 0, by rw ←add_neg_self r; exact is_st_add hxr (is_st_refl_real (-r)) theorem infinitesimal_sub_st {x : ℝ*} (hx : ¬infinite x) : infinitesimal (x - st x) := infinitesimal_sub_is_st $ is_st_st' hx lemma infinite_pos_iff_infinitesimal_inv_pos {x : ℝ*} : infinite_pos x ↔ (infinitesimal x⁻¹ ∧ 0 < x⁻¹) := ⟨ λ hip, ⟨ infinitesimal_def.mpr $ λ r hr, ⟨ lt_trans (coe_lt_coe.2 (neg_neg_of_pos hr)) (inv_pos.2 (hip 0)), (inv_lt (coe_lt_coe.2 hr) (hip 0)).mp (by convert hip r⁻¹) ⟩, inv_pos.2 $ hip 0 ⟩, λ ⟨hi, hp⟩ r, @classical.by_cases (r = 0) (↑r < x) (λ h, eq.substr h (inv_pos.mp hp)) $ λ h, lt_of_le_of_lt (coe_le_coe.2 (le_abs_self r)) ((inv_lt_inv (inv_pos.mp hp) (coe_lt_coe.2 (abs_pos.2 h))).mp ((infinitesimal_def.mp hi) ((abs r)⁻¹) (inv_pos.2 (abs_pos.2 h))).2) ⟩ lemma infinite_neg_iff_infinitesimal_inv_neg {x : ℝ*} : infinite_neg x ↔ (infinitesimal x⁻¹ ∧ x⁻¹ < 0) := ⟨ λ hin, have hin' : _ := infinite_pos_iff_infinitesimal_inv_pos.mp (infinite_pos_neg_of_infinite_neg hin), by rwa [infinitesimal_neg_iff, ←neg_pos, neg_inv], λ hin, by rwa [←neg_pos, infinitesimal_neg_iff, neg_inv, ←infinite_pos_iff_infinitesimal_inv_pos, ←infinite_neg_iff_infinite_pos_neg] at hin ⟩ theorem infinitesimal_inv_of_infinite {x : ℝ*} : infinite x → infinitesimal x⁻¹ := λ hi, or.cases_on hi (λ hip, (infinite_pos_iff_infinitesimal_inv_pos.mp hip).1) (λ hin, (infinite_neg_iff_infinitesimal_inv_neg.mp hin).1) theorem infinite_of_infinitesimal_inv {x : ℝ*} (h0 : x ≠ 0) (hi : infinitesimal x⁻¹ ) : infinite x := begin cases (lt_or_gt_of_ne h0) with hn hp, { exact or.inr (infinite_neg_iff_infinitesimal_inv_neg.mpr ⟨hi, inv_lt_zero.mpr hn⟩) }, { exact or.inl (infinite_pos_iff_infinitesimal_inv_pos.mpr ⟨hi, inv_pos.mpr hp⟩) } end theorem infinite_iff_infinitesimal_inv {x : ℝ*} (h0 : x ≠ 0) : infinite x ↔ infinitesimal x⁻¹ := ⟨ infinitesimal_inv_of_infinite, infinite_of_infinitesimal_inv h0 ⟩ lemma infinitesimal_pos_iff_infinite_pos_inv {x : ℝ*} : infinite_pos x⁻¹ ↔ (infinitesimal x ∧ 0 < x) := by convert infinite_pos_iff_infinitesimal_inv_pos; simp only [inv_inv'] lemma infinitesimal_neg_iff_infinite_neg_inv {x : ℝ*} : infinite_neg x⁻¹ ↔ (infinitesimal x ∧ x < 0) := by convert infinite_neg_iff_infinitesimal_inv_neg; simp only [inv_inv'] theorem infinitesimal_iff_infinite_inv {x : ℝ*} (h : x ≠ 0) : infinitesimal x ↔ infinite x⁻¹ := by convert (infinite_iff_infinitesimal_inv (inv_ne_zero h)).symm; simp only [inv_inv'] /-! ### `st` stuff that requires infinitesimal machinery -/ theorem is_st_of_tendsto {f : ℕ → ℝ} {r : ℝ} (hf : tendsto f at_top (𝓝 r)) : is_st (of_seq f) r := have hg : tendsto (λ n, f n - r) at_top (𝓝 0) := (sub_self r) ▸ (hf.sub tendsto_const_nhds), by rw [←(zero_add r), ←(sub_add_cancel f (λ n, r))]; exact is_st_add (infinitesimal_of_tendsto_zero hg) (is_st_refl_real r) lemma is_st_inv {x : ℝ*} {r : ℝ} (hi : ¬ infinitesimal x) : is_st x r → is_st x⁻¹ r⁻¹ := λ hxr, have h : x ≠ 0 := (λ h, hi (h.symm ▸ infinitesimal_zero)), have H : _ := exists_st_of_not_infinite $ not_imp_not.mpr (infinitesimal_iff_infinite_inv h).mpr hi, Exists.cases_on H $ λ s hs, have H' : is_st 1 (r * s) := mul_inv_cancel h ▸ is_st_mul hxr hs, have H'' : s = r⁻¹ := one_div r ▸ eq_one_div_of_mul_eq_one (eq_of_is_st_real H').symm, H'' ▸ hs lemma st_inv (x : ℝ*) : st x⁻¹ = (st x)⁻¹ := begin by_cases h0 : x = 0, rw [h0, inv_zero, ←coe_zero, st_id_real, inv_zero], by_cases h1 : infinitesimal x, rw [st_infinite ((infinitesimal_iff_infinite_inv h0).mp h1), st_of_is_st h1, inv_zero], by_cases h2 : infinite x, rw [st_of_is_st (infinitesimal_inv_of_infinite h2), st_infinite h2, inv_zero], exact st_of_is_st (is_st_inv h1 (is_st_st' h2)), end /-! ### Infinite stuff that requires infinitesimal machinery -/ lemma infinite_pos_omega : infinite_pos ω := infinite_pos_iff_infinitesimal_inv_pos.mpr ⟨infinitesimal_epsilon, epsilon_pos⟩ lemma infinite_omega : infinite ω := (infinite_iff_infinitesimal_inv omega_ne_zero).mpr infinitesimal_epsilon lemma infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos {x y : ℝ*} : infinite_pos x → ¬ infinitesimal y → 0 < y → infinite_pos (x * y) := λ hx hy₁ hy₂ r, have hy₁' : _ := not_forall.mp (by rw infinitesimal_def at hy₁; exact hy₁), Exists.dcases_on hy₁' $ λ r₁ hy₁'', have hyr : _ := by rw [not_imp, ←abs_lt, not_lt, abs_of_pos hy₂] at hy₁''; exact hy₁'', by rw [←div_mul_cancel r (ne_of_gt hyr.1), coe_mul]; exact mul_lt_mul (hx (r / r₁)) hyr.2 (coe_lt_coe.2 hyr.1) (le_of_lt (hx 0)) lemma infinite_pos_mul_of_not_infinitesimal_pos_infinite_pos {x y : ℝ*} : ¬ infinitesimal x → 0 < x → infinite_pos y → infinite_pos (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos hy hx hp lemma infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg {x y : ℝ*} : infinite_neg x → ¬ infinitesimal y → y < 0 → infinite_pos (x * y) := by rw [infinite_neg_iff_infinite_pos_neg, ←neg_pos, ←neg_mul_neg, infinitesimal_neg_iff]; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos lemma infinite_pos_mul_of_not_infinitesimal_neg_infinite_neg {x y : ℝ*} : ¬ infinitesimal x → x < 0 → infinite_neg y → infinite_pos (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg hy hx hp lemma infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg {x y : ℝ*} : infinite_pos x → ¬ infinitesimal y → y < 0 → infinite_neg (x * y) := by rw [infinite_neg_iff_infinite_pos_neg, ←neg_pos, neg_mul_eq_mul_neg, infinitesimal_neg_iff]; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos lemma infinite_neg_mul_of_not_infinitesimal_neg_infinite_pos {x y : ℝ*} : ¬ infinitesimal x → x < 0 → infinite_pos y → infinite_neg (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg hy hx hp lemma infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos {x y : ℝ*} : infinite_neg x → ¬ infinitesimal y → 0 < y → infinite_neg (x * y) := by rw [infinite_neg_iff_infinite_pos_neg, infinite_neg_iff_infinite_pos_neg, neg_mul_eq_neg_mul]; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos lemma infinite_neg_mul_of_not_infinitesimal_pos_infinite_neg {x y : ℝ*} : ¬ infinitesimal x → 0 < x → infinite_neg y → infinite_neg (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos hy hx hp lemma infinite_pos_mul_infinite_pos {x y : ℝ*} : infinite_pos x → infinite_pos y → infinite_pos (x * y) := λ hx hy, infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos hx (not_infinitesimal_of_infinite_pos hy) (hy 0) lemma infinite_neg_mul_infinite_neg {x y : ℝ*} : infinite_neg x → infinite_neg y → infinite_pos (x * y) := λ hx hy, infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg hx (not_infinitesimal_of_infinite_neg hy) (hy 0) lemma infinite_pos_mul_infinite_neg {x y : ℝ*} : infinite_pos x → infinite_neg y → infinite_neg (x * y) := λ hx hy, infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg hx (not_infinitesimal_of_infinite_neg hy) (hy 0) lemma infinite_neg_mul_infinite_pos {x y : ℝ*} : infinite_neg x → infinite_pos y → infinite_neg (x * y) := λ hx hy, infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos hx (not_infinitesimal_of_infinite_pos hy) (hy 0) lemma infinite_mul_of_infinite_not_infinitesimal {x y : ℝ*} : infinite x → ¬ infinitesimal y → infinite (x * y) := λ hx hy, have h0 : y < 0 ∨ 0 < y := lt_or_gt_of_ne (λ H0, hy (eq.substr H0 (is_st_refl_real 0))), or.dcases_on hx (or.dcases_on h0 (λ H0 Hx, or.inr (infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg Hx hy H0)) (λ H0 Hx, or.inl (infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos Hx hy H0))) (or.dcases_on h0 (λ H0 Hx, or.inl (infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg Hx hy H0)) (λ H0 Hx, or.inr (infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos Hx hy H0))) lemma infinite_mul_of_not_infinitesimal_infinite {x y : ℝ*} : ¬ infinitesimal x → infinite y → infinite (x * y) := λ hx hy, by rw [mul_comm]; exact infinite_mul_of_infinite_not_infinitesimal hy hx lemma infinite_mul_infinite {x y : ℝ*} : infinite x → infinite y → infinite (x * y) := λ hx hy, infinite_mul_of_infinite_not_infinitesimal hx (not_infinitesimal_of_infinite hy) end hyperreal
6447e47e813ac570113c2995d9af968cc48516d1
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/analysis/calculus/inverse.lean
d34387ee425680c6f57f6a2c96eba1506ade897d
[ "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
32,285
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 := _root_.mem_nhds_iff.1 hs, have := is_open.mem_nhds ((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_mem_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_congr 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, is_open.mem_nhds 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
aae9e458ab022e7d3c5479da77eec2a5f6bdd6f2
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/noncompSection.lean
b7f121e2f4babc0b3a66c3a7b3941eb25299a3cb
[ "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
477
lean
noncomputable section theorem ex : ∃ x : Nat, x > 0 := ⟨1, by decide⟩ def a : Nat := Classical.choose ex def b : Nat := 0 abbrev c : Nat := Classical.choose ex abbrev d : Nat := 1 instance e : Inhabited Nat := ⟨a⟩ instance f : Inhabited Nat := ⟨b⟩ #eval b + d + f.default section Foo def g : Nat := Classical.choose ex def h (x : Nat) : Nat := match x with | 0 => a | x+1 => h x + 1 end Foo end def i : Nat := Classical.choose ex -- Error
17046baf9b9f215e5ae253d1178540b46e4022bd
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/field_theory/primitive_element.lean
243145268a41175c969f3a37d084b7844c4eb067
[ "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
9,062
lean
/- Copyright (c) 2020 Thomas Browning, Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning, Patrick Lutz -/ import field_theory.adjoin import field_theory.is_alg_closed.basic import field_theory.separable /-! # Primitive Element Theorem In this file we prove the primitive element theorem. ## Main results - `exists_primitive_element`: a finite separable extension `E / F` has a primitive element, i.e. there is an `α : E` such that `F⟮α⟯ = (⊤ : subalgebra F E)`. ## Implementation notes In declaration names, `primitive_element` abbreviates `adjoin_simple_eq_top`: it stands for the statement `F⟮α⟯ = (⊤ : subalgebra F E)`. We did not add an extra declaration `is_primitive_element F α := F⟮α⟯ = (⊤ : subalgebra F E)` because this requires more unfolding without much obvious benefit. ## Tags primitive element, separable field extension, separable extension, intermediate field, adjoin, exists_adjoin_simple_eq_top -/ noncomputable theory open_locale classical open finite_dimensional polynomial intermediate_field namespace field section primitive_element_finite variables (F : Type*) [field F] (E : Type*) [field E] [algebra F E] /-! ### Primitive element theorem for finite fields -/ /-- **Primitive element theorem** assuming E is finite. -/ lemma exists_primitive_element_of_fintype_top [fintype E] : ∃ α : E, F⟮α⟯ = ⊤ := begin obtain ⟨α, hα⟩ := is_cyclic.exists_generator (units E), use α, apply eq_top_iff.mpr, rintros x -, by_cases hx : x = 0, { rw hx, exact F⟮α.val⟯.zero_mem }, { obtain ⟨n, hn⟩ := set.mem_range.mp (hα (units.mk0 x hx)), rw (show x = α^n, by { norm_cast, rw [hn, units.coe_mk0] }), exact pow_mem F⟮↑α⟯ (mem_adjoin_simple_self F ↑α) n, }, end /-- Primitive element theorem for finite dimensional extension of a finite field. -/ theorem exists_primitive_element_of_fintype_bot [fintype F] [finite_dimensional F E] : ∃ α : E, F⟮α⟯ = ⊤ := begin haveI : fintype E := fintype_of_fintype F E, exact exists_primitive_element_of_fintype_top F E, end end primitive_element_finite /-! ### Primitive element theorem for infinite fields -/ section primitive_element_inf variables {F : Type*} [field F] [infinite F] {E : Type*} [field E] (ϕ : F →+* E) (α β : E) lemma primitive_element_inf_aux_exists_c (f g : polynomial F) : ∃ c : F, ∀ (α' ∈ (f.map ϕ).roots) (β' ∈ (g.map ϕ).roots), -(α' - α)/(β' - β) ≠ ϕ c := begin let sf := (f.map ϕ).roots, let sg := (g.map ϕ).roots, let s := (sf.bind (λ α', sg.map (λ β', -(α' - α) / (β' - β)))).to_finset, let s' := s.preimage ϕ (λ x hx y hy h, ϕ.injective h), obtain ⟨c, hc⟩ := infinite.exists_not_mem_finset s', simp_rw [finset.mem_preimage, multiset.mem_to_finset, multiset.mem_bind, multiset.mem_map] at hc, push_neg at hc, exact ⟨c, hc⟩, end variables (F) [algebra F E] -- This is the heart of the proof of the primitive element theorem. It shows that if `F` is -- infinite and `α` and `β` are separable over `F` then `F⟮α, β⟯` is generated by a single element. lemma primitive_element_inf_aux [is_separable F E] : ∃ γ : E, F⟮α, β⟯ = F⟮γ⟯ := begin have hα := is_separable.is_integral F α, have hβ := is_separable.is_integral F β, let f := minpoly F α, let g := minpoly F β, let ιFE := algebra_map F E, let ιEE' := algebra_map E (splitting_field (g.map ιFE)), obtain ⟨c, hc⟩ := primitive_element_inf_aux_exists_c (ιEE'.comp ιFE) (ιEE' α) (ιEE' β) f g, let γ := α + c • β, suffices β_in_Fγ : β ∈ F⟮γ⟯, { use γ, apply le_antisymm, { rw adjoin_le_iff, have α_in_Fγ : α ∈ F⟮γ⟯, { rw ← add_sub_cancel α (c • β), exact F⟮γ⟯.sub_mem (mem_adjoin_simple_self F γ) (F⟮γ⟯.to_subalgebra.smul_mem β_in_Fγ c) }, exact λ x hx, by cases hx; cases hx; cases hx; assumption }, { rw adjoin_le_iff, change {γ} ⊆ _, rw set.singleton_subset_iff, have α_in_Fαβ : α ∈ F⟮α, β⟯ := subset_adjoin F {α, β} (set.mem_insert α {β}), have β_in_Fαβ : β ∈ F⟮α, β⟯ := subset_adjoin F {α, β} (set.mem_insert_of_mem α rfl), exact F⟮α,β⟯.add_mem α_in_Fαβ (F⟮α, β⟯.smul_mem β_in_Fαβ) } }, let p := euclidean_domain.gcd ((f.map (algebra_map F F⟮γ⟯)).comp (C (adjoin_simple.gen F γ) - (C ↑c * X))) (g.map (algebra_map F F⟮γ⟯)), let h := euclidean_domain.gcd ((f.map ιFE).comp (C γ - (C (ιFE c) * X))) (g.map ιFE), have map_g_ne_zero : g.map ιFE ≠ 0 := map_ne_zero (minpoly.ne_zero hβ), have h_ne_zero : h ≠ 0 := mt euclidean_domain.gcd_eq_zero_iff.mp (not_and.mpr (λ _, map_g_ne_zero)), suffices p_linear : p.map (algebra_map F⟮γ⟯ E) = (C h.leading_coeff) * (X - C β), { have finale : β = algebra_map F⟮γ⟯ E (-p.coeff 0 / p.coeff 1), { rw [ring_hom.map_div, ring_hom.map_neg, ←coeff_map, ←coeff_map, p_linear], simp [mul_sub, coeff_C, mul_div_cancel_left β (mt leading_coeff_eq_zero.mp h_ne_zero)] }, rw finale, exact subtype.mem (-p.coeff 0 / p.coeff 1) }, have h_sep : h.separable := separable_gcd_right _ (is_separable.separable F β).map, have h_root : h.eval β = 0, { apply eval_gcd_eq_zero, { rw [eval_comp, eval_sub, eval_mul, eval_C, eval_C, eval_X, eval_map, ←aeval_def, ←algebra.smul_def, add_sub_cancel, minpoly.aeval] }, { rw [eval_map, ←aeval_def, minpoly.aeval] } }, have h_splits : splits ιEE' h := splits_of_splits_gcd_right ιEE' map_g_ne_zero (splitting_field.splits _), have h_roots : ∀ x ∈ (h.map ιEE').roots, x = ιEE' β, { intros x hx, rw mem_roots_map h_ne_zero at hx, specialize hc ((ιEE' γ) - (ιEE' (ιFE c)) * x) (begin have f_root := root_left_of_root_gcd hx, rw [eval₂_comp, eval₂_sub, eval₂_mul,eval₂_C, eval₂_C, eval₂_X, eval₂_map] at f_root, exact (mem_roots_map (minpoly.ne_zero hα)).mpr f_root, end), specialize hc x (begin rw [mem_roots_map (minpoly.ne_zero hβ), ←eval₂_map], exact root_right_of_root_gcd hx, end), by_contradiction a, apply hc, apply (div_eq_iff (sub_ne_zero.mpr a)).mpr, simp only [algebra.smul_def, ring_hom.map_add, ring_hom.map_mul, ring_hom.comp_apply], ring }, rw ← eq_X_sub_C_of_separable_of_root_eq h_ne_zero h_sep h_root h_splits h_roots, transitivity euclidean_domain.gcd (_ : polynomial E) (_ : polynomial E), { dsimp only [p], convert (gcd_map (algebra_map F⟮γ⟯ E)).symm }, { simpa [map_comp, map_map, ←is_scalar_tower.algebra_map_eq, h] }, end end primitive_element_inf variables (F E : Type*) [field F] [field E] variables [algebra F E] [finite_dimensional F E] [is_separable F E] /-- Primitive element theorem: a finite separable field extension `E` of `F` has a primitive element, i.e. there is an `α ∈ E` such that `F⟮α⟯ = (⊤ : subalgebra F E)`.-/ theorem exists_primitive_element : ∃ α : E, F⟮α⟯ = ⊤ := begin rcases is_empty_or_nonempty (fintype F) with F_inf|⟨⟨F_finite⟩⟩, { let P : intermediate_field F E → Prop := λ K, ∃ α : E, F⟮α⟯ = K, have base : P ⊥ := ⟨0, adjoin_zero⟩, have ih : ∀ (K : intermediate_field F E) (x : E), P K → P ↑K⟮x⟯, { intros K β hK, cases hK with α hK, rw [←hK, adjoin_simple_adjoin_simple], haveI : infinite F := is_empty_fintype.mp F_inf, cases primitive_element_inf_aux F α β with γ hγ, exact ⟨γ, hγ.symm⟩ }, exact induction_on_adjoin P base ih ⊤ }, { exactI exists_primitive_element_of_fintype_bot F E } end /-- Alternative phrasing of primitive element theorem: a finite separable field extension has a basis `1, α, α^2, ..., α^n`. See also `exists_primitive_element`. -/ noncomputable def power_basis_of_finite_of_separable : power_basis F E := let α := (exists_primitive_element F E).some, pb := (adjoin.power_basis (is_separable.is_integral F α)) in have e : F⟮α⟯ = ⊤ := (exists_primitive_element F E).some_spec, pb.map ((intermediate_field.equiv_of_eq e).trans intermediate_field.top_equiv) /-- If `E / F` is a finite separable extension, then there are finitely many embeddings from `E` into `K` that fix `F`, corresponding to the number of conjugate roots of the primitive element generating `F`. -/ instance {K : Type*} [field K] [algebra F K] : fintype (E →ₐ[F] K) := power_basis.alg_hom.fintype (power_basis_of_finite_of_separable F E) end field @[simp] lemma alg_hom.card (F E K : Type*) [field F] [field E] [field K] [is_alg_closed K] [algebra F E] [finite_dimensional F E] [is_separable F E] [algebra F K] : fintype.card (E →ₐ[F] K) = finrank F E := (alg_hom.card_of_power_basis (field.power_basis_of_finite_of_separable F E) (is_separable.separable _ _) (is_alg_closed.splits_codomain _)).trans (power_basis.finrank _).symm
8dde70b6ac099057ef4aeffa1b52305841307f81
e38e95b38a38a99ecfa1255822e78e4b26f65bb0
/src/certigrad/run_utils.lean
7102a10df2554122903d82579a224d419ac057df
[ "Apache-2.0" ]
permissive
ColaDrill/certigrad
fefb1be3670adccd3bed2f3faf57507f156fd501
fe288251f623ac7152e5ce555f1cd9d3a20203c2
refs/heads/master
1,593,297,324,250
1,499,903,753,000
1,499,903,753,000
97,075,797
1
0
null
1,499,916,210,000
1,499,916,210,000
null
UTF-8
Lean
false
false
6,393
lean
/- Copyright (c) 2017 Daniel Selsam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Daniel Selsam Utilities for optimizing over stochastic computation graphs. -/ import system.io system.time .tensor .tvec .optim .compute_grad .graph namespace certigrad open io namespace T meta constant read_mnist (dir : string) [io.interface] : io (T [60000, 784] × T [60000]) meta constant read_from_file (shape : S) (s : string) [io.interface] : io (T shape) meta constant write_to_file {shape : S} (x : T shape) (s : string) [io.interface] : io unit meta constant set_num_threads (n : ℕ) [io.interface] : io unit end T namespace tvec meta def write_all_core [io.interface] (pfix sfix : string) : Π (names : list ID) (shapes : list S), dvec T shapes → io unit | (name::names) (shape::shapes) (dvec.cons x xs) := do T.write_to_file x (pfix ++ to_string name ++ sfix), write_all_core names shapes xs | _ _ _ := return () meta def write_all [io.interface] (dir pfix sfix : string) (refs : list reference) (xs : dvec T refs^.p2) : io unit := do mkdir dir, write_all_core (dir ++ "/" ++ pfix) sfix refs^.p1 refs^.p2 xs end tvec def xavier_init : Π (shape : S), state RNG (T shape) | [] := return 0 | shape := let low :ℝ := - T.sqrt ((6 : ℝ) / (T.of_nat $ list.sumr shape)), high :ℝ := T.sqrt ((6 : ℝ) / (T.of_nat $ list.sumr shape)) in T.sample_uniform shape low high def sample_initial_weights : Π (refs : list reference), state RNG (dvec T refs.p2) | [] := return ⟦⟧ | (ref::refs) := sample_initial_weights refs >>= λ ws, xavier_init ref.2 >>= λ w, return (w ::: ws) namespace run open optim def get_batch_start (batch_size batch_num : ℕ) : ℕ := batch_size * batch_num def mk_initial_env ⦃n_in n_x : ℕ⦄ (x_all : T [n_in, n_x]) (batch_size batch_num : ℕ) (targets : list reference) (θ : dvec T targets^.p2) : env := env.insert (ID.str label.x, [n_in, batch_size]) (T.get_col_range batch_size x_all (get_batch_start batch_size batch_num)) (tvec.to_env targets θ) def compute_costs (g : graph) (inputs : env) : state RNG (ℝ) := let dist : sprog [[]] := graph.to_dist (λ env₀, ⟦sum_costs env₀ g^.costs⟧) inputs g^.nodes in do result ← dist^.to_rngprog, return (dvec.head result) def compute_cost_epoch_core (g : graph) ⦃n_in n_x : ℕ⦄ (x_all : T [n_in, n_x]) (batch_size : ℕ) (θ : dvec T g^.targets^.p2) : Π (batches_left : ℕ) (costs_so_far : ℝ), state RNG (ℝ) | 0 cs := return cs | (bl + 1) cs := do inputs ← return $ mk_initial_env x_all batch_size bl g^.targets θ, c ← compute_costs g inputs, compute_cost_epoch_core bl (cs + c) def compute_cost_epoch (g : graph) ⦃n_in n_x : ℕ⦄ (x_all : T [n_in, n_x]) (batch_size : ℕ) (θ : dvec T g^.targets^.p2) (num_batches : ℕ) : state RNG (ℝ) := do total_ecost ← compute_cost_epoch_core g x_all batch_size θ num_batches 0, return $ total_ecost / (T.of_nat $ batch_size * num_batches) -- Assumes that "x" is an argument to the graph def optimize_step (g : graph) (inputs : env) (astate : adam.state g^.targets^.p2) (θ : dvec T g^.targets^.p2) (init_dict : env) (batch_size : ℕ) : state RNG (dvec T g^.targets^.p2 × adam.state g^.targets^.p2) := do grads ← (graph.to_dist (λ env, bprop g^.costs init_dict g^.nodes env g^.targets) inputs g^.nodes)^.to_rngprog, return $ adam.step θ ((1 / T.of_nat batch_size) ⬝ grads) astate def optimize_epoch_core (g : graph) ⦃n_in n_x : ℕ⦄ (x_all : T [n_in, n_x]) (batch_size : ℕ) : Π (batches_left : ℕ) (astate : adam.state g^.targets^.p2) (θ : dvec T g^.targets^.p2) (init_dict : env), state RNG (dvec T g^.targets^.p2 × adam.state g^.targets^.p2) | 0 astate θ init_dict := return (θ, astate) | (bl + 1) astate θ init_dict := do inputs ← return $ mk_initial_env x_all batch_size bl g^.targets θ, (θ_new, astate_new) ← optimize_step g inputs astate θ init_dict batch_size, optimize_epoch_core bl astate_new θ_new init_dict def optimize_epoch (g : graph) ⦃n_in n_x : ℕ⦄ (x_all : T [n_in, n_x]) (batch_size : ℕ) (astate : adam.state g^.targets^.p2) (θ : dvec T g^.targets^.p2) (init_dict : env) : state RNG (dvec T g^.targets^.p2 × ℝ × adam.state g^.targets^.p2) := let num_batches : ℕ := n_x / batch_size in do (θ_new, astate_new) ← optimize_epoch_core g x_all batch_size num_batches astate θ init_dict, epoch_cost ← compute_cost_epoch g x_all batch_size θ_new num_batches, return (θ_new, epoch_cost, astate_new) meta def run_epoch [io.interface] (g : graph) ⦃n_in n_x : ℕ⦄ (x_all : T [n_in, n_x]) (batch_size : ℕ) (astate : adam.state g^.targets^.p2) (θ : dvec T g^.targets^.p2) (rng : RNG) (init_dict : env) : io (dvec T g^.targets^.p2 × ℝ × adam.state g^.targets^.p2 × RNG) := do ((θ_new, epoch_costs, astate_new), rng_new) ← return $ optimize_epoch g x_all batch_size astate θ init_dict rng, return (θ_new, epoch_costs, astate_new, rng_new) meta def run_iters_core [io.interface] (tval : time) (dir : string) (g : graph) ⦃n_in n_x : ℕ⦄ (x_all : T [n_in, n_x]) (batch_size : ℕ) (init_dict : env) : Π (num_iters : ℕ), dvec T g^.targets^.p2 → adam.state g^.targets^.p2 → RNG → io (dvec T g^.targets^.p2 × adam.state g^.targets^.p2 × RNG) | 0 θ astate rng := return (θ, astate, rng) | (t+1) θ astate rng := do ((θ', epoch_cost, astate', rng')) ← run_epoch g x_all batch_size astate θ rng init_dict, tval' ← time.get, put_str (to_string epoch_cost ++ ", " ++ time.sdiff tval' tval ++ "\n"), run_iters_core t θ' astate' rng' meta def run_iters [io.interface] (dir : string) (g : graph) ⦃n_x n_in : ℕ⦄ (x_all : T [n_in, n_x]) (batch_size : ℕ) (num_iters : ℕ) (θ : dvec T g^.targets^.p2) (astate : adam.state g^.targets^.p2) (rng : RNG) : io (dvec T g^.targets^.p2 × adam.state g^.targets^.p2 × RNG) := do init_dict ← return $ compute_init_dict g^.costs g^.nodes g^.targets, (epoch_cost, rng) ← return $ compute_cost_epoch g x_all batch_size θ (n_x / batch_size) rng, tval ← time.get, put_str (to_string epoch_cost ++ ", 0\n"), run_iters_core tval dir g x_all batch_size init_dict num_iters θ astate rng end run end certigrad
5fd07baa6f6f62ff8d7d805ad4d166753a87374f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/control/except.lean
004ab976824362bfe71185638c746ac1242b668c
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
5,625
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jared Roesch, Sebastian Ullrich The except monad transformer. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.control.alternative import Mathlib.Lean3Lib.init.control.lift universes u v l u_1 w u_2 namespace Mathlib inductive except (ε : Type u) (α : Type v) where | error : ε → except ε α | ok : α → except ε α namespace except protected def return {ε : Type u} {α : Type v} (a : α) : except ε α := ok a protected def map {ε : Type u} {α : Type v} {β : Type v} (f : α → β) : except ε α → except ε β := sorry protected def map_error {ε : Type u} {ε' : Type u} {α : Type v} (f : ε → ε') : except ε α → except ε' α := sorry protected def bind {ε : Type u} {α : Type v} {β : Type v} (ma : except ε α) (f : α → except ε β) : except ε β := sorry protected def to_bool {ε : Type u} {α : Type v} : except ε α → Bool := sorry protected def to_option {ε : Type u} {α : Type v} : except ε α → Option α := sorry protected instance monad {ε : Type u} : Monad (except ε) := sorry end except structure except_t (ε : Type u) (m : Type u → Type v) (α : Type u) where run : m (except ε α) namespace except_t protected def return {ε : Type u} {m : Type u → Type v} [Monad m] {α : Type u} (a : α) : except_t ε m α := mk (pure (except.ok a)) protected def bind_cont {ε : Type u} {m : Type u → Type v} [Monad m] {α : Type u} {β : Type u} (f : α → except_t ε m β) : except ε α → m (except ε β) := sorry protected def bind {ε : Type u} {m : Type u → Type v} [Monad m] {α : Type u} {β : Type u} (ma : except_t ε m α) (f : α → except_t ε m β) : except_t ε m β := mk (run ma >>= except_t.bind_cont f) protected def lift {ε : Type u} {m : Type u → Type v} [Monad m] {α : Type u} (t : m α) : except_t ε m α := mk (except.ok <$> t) protected instance has_monad_lift {ε : Type u} {m : Type u → Type v} [Monad m] : has_monad_lift m (except_t ε m) := has_monad_lift.mk except_t.lift protected def catch {ε : Type u} {m : Type u → Type v} [Monad m] {α : Type u} (ma : except_t ε m α) (handle : ε → except_t ε m α) : except_t ε m α := mk do run ma sorry protected def monad_map {ε : Type u} {m : Type u → Type v} [Monad m] {m' : Type u → Type u_1} [Monad m'] {α : Type u} (f : {α : Type u} → m α → m' α) : except_t ε m α → except_t ε m' α := fun (x : except_t ε m α) => mk (f (run x)) protected instance monad_functor {ε : Type u} {m : Type u → Type v} [Monad m] (m' : Type u → Type v) [Monad m'] : monad_functor m m' (except_t ε m) (except_t ε m') := monad_functor.mk except_t.monad_map protected instance monad {ε : Type u} {m : Type u → Type v} [Monad m] : Monad (except_t ε m) := sorry protected def adapt {ε : Type u} {m : Type u → Type v} [Monad m] {ε' : Type u} {α : Type u} (f : ε → ε') : except_t ε m α → except_t ε' m α := fun (x : except_t ε m α) => mk (except.map_error f <$> run x) end except_t /-- An implementation of [MonadError](https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Except.html#t:MonadError) -/ class monad_except (ε : outParam (Type u)) (m : Type v → Type w) where throw : {α : Type v} → ε → m α catch : {α : Type v} → m α → (ε → m α) → m α namespace monad_except protected def orelse {ε : Type u} {m : Type v → Type w} [monad_except ε m] {α : Type v} (t₁ : m α) (t₂ : m α) : m α := catch t₁ fun (_x : ε) => t₂ /-- Alternative orelse operator that allows to select which exception should be used. The default is to use the first exception since the standard `orelse` uses the second. -/ end monad_except protected instance except_t.monad_except (m : Type u_1 → Type u_2) (ε : outParam (Type u_1)) [Monad m] : monad_except ε (except_t ε m) := monad_except.mk (fun (α : Type u_1) => except_t.mk ∘ pure ∘ except.error) except_t.catch /-- Adapt a monad stack, changing its top-most error type. Note: This class can be seen as a simplification of the more "principled" definition ``` class monad_except_functor (ε ε' : out_param (Type u)) (n n' : Type u → Type u) := (map {α : Type u} : (∀ {m : Type u → Type u} [monad m], except_t ε m α → except_t ε' m α) → n α → n' α) ``` -/ class monad_except_adapter (ε : outParam (Type u)) (ε' : outParam (Type u)) (m : Type u → Type v) (m' : Type u → Type v) where adapt_except : {α : Type u} → (ε → ε') → m α → m' α protected instance monad_except_adapter_trans {ε : Type u} {ε' : Type u} {m : Type u → Type v} {m' : Type u → Type v} {n : Type u → Type v} {n' : Type u → Type v} [monad_functor m m' n n'] [monad_except_adapter ε ε' m m'] : monad_except_adapter ε ε' n n' := monad_except_adapter.mk fun (α : Type u) (f : ε → ε') => monad_map fun (α : Type u) => adapt_except f protected instance except_t.monad_except_adapter {ε : Type u} {ε' : Type u} {m : Type u → Type v} [Monad m] : monad_except_adapter ε ε' (except_t ε m) (except_t ε' m) := monad_except_adapter.mk fun (α : Type u) => except_t.adapt protected instance except_t.monad_run (ε : Type u_1) (m : Type u_1 → Type u_2) (out : outParam (Type u_1 → Type u_2)) [monad_run out m] : monad_run (fun (α : Type u_1) => out (except ε α)) (except_t ε m) := monad_run.mk fun (α : Type u_1) => run ∘ except_t.run
00a80363c0a22977889dc37206d7e4f2e64ade65
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/order/pilex.lean
aa44fa40efd679af831cc38618da2a8cfa13cf40
[ "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
4,104
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.group.pi import order.rel_classes import algebra.order_functions variables {ι : Type*} {β : ι → Type*} (r : ι → ι → Prop) (s : Π {i}, β i → β i → Prop) def pi.lex (x y : Π i, β i) : Prop := ∃ i, (∀ j, r j i → x j = y j) ∧ s (x i) (y i) def pilex (α : Type*) (β : α → Type*) : Type* := Π a, β a instance [has_lt ι] [∀ a, has_lt (β a)] : has_lt (pilex ι β) := { lt := pi.lex (<) (λ _, (<)) } instance [∀ a, inhabited (β a)] : inhabited (pilex ι β) := by unfold pilex; apply_instance set_option eqn_compiler.zeta true instance [linear_order ι] [∀ a, partial_order (β a)] : partial_order (pilex ι β) := let I := classical.DLO ι in have lt_not_symm : ∀ {x y : pilex ι β}, ¬ (x < y ∧ y < x), from λ x y ⟨⟨i, hi⟩, ⟨j, hj⟩⟩, begin rcases lt_trichotomy i j with hij | hij | hji, { exact lt_irrefl (x i) (by simpa [hj.1 _ hij] using hi.2) }, { exact not_le_of_gt hj.2 (hij ▸ le_of_lt hi.2) }, { exact lt_irrefl (x j) (by simpa [hi.1 _ hji] using hj.2) }, end, { le := λ x y, x < y ∨ x = y, le_refl := λ _, or.inr rfl, le_antisymm := λ x y hxy hyx, hxy.elim (λ hxy, hyx.elim (λ hyx, false.elim (lt_not_symm ⟨hxy, hyx⟩)) eq.symm) id, le_trans := λ x y z hxy hyz, hxy.elim (λ ⟨i, hi⟩, hyz.elim (λ ⟨j, hj⟩, or.inl ⟨by exactI min i j, by resetI; exact λ k hk, by rw [hi.1 _ (lt_min_iff.1 hk).1, hj.1 _ (lt_min_iff.1 hk).2], by resetI; exact (le_total i j).elim (λ hij, by rw [min_eq_left hij]; exact lt_of_lt_of_le hi.2 ((lt_or_eq_of_le hij).elim (λ h, le_of_eq (hj.1 _ h)) (λ h, h.symm ▸ le_of_lt hj.2))) (λ hji, by rw [min_eq_right hji]; exact lt_of_le_of_lt ((lt_or_eq_of_le hji).elim (λ h, le_of_eq (hi.1 _ h)) (λ h, h.symm ▸ le_of_lt hi.2)) hj.2)⟩) (λ hyz, hyz ▸ hxy)) (λ hxy, hxy.symm ▸ hyz), lt_iff_le_not_le := λ x y, show x < y ↔ (x < y ∨ x = y) ∧ ¬ (y < x ∨ y = x), from ⟨λ ⟨i, hi⟩, ⟨or.inl ⟨i, hi⟩, λ h, h.elim (λ ⟨j, hj⟩, begin rcases lt_trichotomy i j with hij | hij | hji, { exact lt_irrefl (x i) (by simpa [hj.1 _ hij] using hi.2) }, { exact not_le_of_gt hj.2 (hij ▸ le_of_lt hi.2) }, { exact lt_irrefl (x j) (by simpa [hi.1 _ hji] using hj.2) }, end) (λ hyx, lt_irrefl (x i) (by simpa [hyx] using hi.2))⟩, by tauto⟩, ..pilex.has_lt } /-- `pilex` is a linear order if the original order is well-founded. This cannot be an instance, since it depends on the well-foundedness of `<`. -/ protected def pilex.linear_order [linear_order ι] (wf : well_founded ((<) : ι → ι → Prop)) [∀ a, linear_order (β a)] : linear_order (pilex ι β) := { le_total := λ x y, by classical; exact or_iff_not_imp_left.2 (λ hxy, begin have := not_or_distrib.1 hxy, let i : ι := well_founded.min wf _ (not_forall.1 (this.2 ∘ funext)), have hjiyx : ∀ j < i, y j = x j, { assume j, rw [eq_comm, ← not_imp_not], exact λ h, well_founded.not_lt_min wf _ _ h }, refine or.inl ⟨i, hjiyx, _⟩, { refine lt_of_not_ge (λ hyx, _), exact this.1 ⟨i, (λ j hj, (hjiyx j hj).symm), lt_of_le_of_ne hyx (well_founded.min_mem _ {i | x i ≠ y i} _)⟩ } end), ..pilex.partial_order } instance [linear_order ι] [∀ a, ordered_add_comm_group (β a)] : ordered_add_comm_group (pilex ι β) := { add_le_add_left := λ x y hxy z, hxy.elim (λ ⟨i, hi⟩, or.inl ⟨i, λ j hji, show z j + x j = z j + y j, by rw [hi.1 j hji], add_lt_add_left hi.2 _⟩) (λ hxy, hxy ▸ le_refl _), ..pilex.partial_order, ..pi.add_comm_group }
9e119d497aa30dcf75b91e4b55df5d3a47b0a705
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/category/traversable/basic.lean
3c8e76e11e32d3bbec4d7d231fc7d73eff670231
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
5,610
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import tactic.cache import category.applicative /-! # Traversable type class Type classes for traversing collections. The concepts and laws are taken from http://hackage.haskell.org/package/base-4.11.1.0/docs/Data-Traversable.html Traversable collections are a generalization of functors. Whereas functors (such as `list`) allow us to apply a function to every element, it does not allow functions which external effects encoded in a monad. Consider for instance a functor `invite : email → io response` that takes an email address, sends an email and waits for a response. If we have a list `guests : list email`, using calling `invite` using `map` gives us the following: `map invite guests : list (io response)`. It is not what we need. We need something of type `io (list response)`. Instead of using `map`, we can use `traverse` to send all the invites: `traverse invite guests : io (list response)`. `traverse` applies `invite` to every element of `guests` and combines all the resulting effects. In the example, the effect is encoded in the monad `io` but any applicative functor is accepted by `traverse`. For more on how to use traversable, consider the Haskell tutorial: https://en.wikibooks.org/wiki/Haskell/Traversable ## Main definitions * `traversable` type class - exposes the `traverse` function * `sequence` - based on `traverse`, turns a collection of effects into an effect returning a collection * is_lawful_traversable - laws ## Tags traversable iterator functor applicative ## References * "Applicative Programming with Effects", by Conor McBride and Ross Paterson, Journal of Functional Programming 18:1 (2008) 1-13, online at http://www.soi.city.ac.uk/~ross/papers/Applicative.html. * "The Essence of the Iterator Pattern", by Jeremy Gibbons and Bruno Oliveira, in Mathematically-Structured Functional Programming, 2006, online at http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/#iterator. * "An Investigation of the Laws of Traversals", by Mauro Jaskelioff and Ondrej Rypacek, in Mathematically-Structured Functional Programming, 2012, online at http://arxiv.org/pdf/1202.2919. Synopsis -/ open function (hiding comp) universes u v w section applicative_transformation variables (F : Type u → Type v) [applicative F] [is_lawful_applicative F] variables (G : Type u → Type w) [applicative G] [is_lawful_applicative G] structure applicative_transformation : Type (max (u+1) v w) := (app : ∀ α : Type u, F α → G α) (preserves_pure' : ∀ {α : Type u} (x : α), app _ (pure x) = pure x) (preserves_seq' : ∀ {α β : Type u} (x : F (α → β)) (y : F α), app _ (x <*> y) = app _ x <*> app _ y) end applicative_transformation namespace applicative_transformation variables (F : Type u → Type v) [applicative F] [is_lawful_applicative F] variables (G : Type u → Type w) [applicative G] [is_lawful_applicative G] instance : has_coe_to_fun (applicative_transformation F G) := { F := λ _, Π {α}, F α → G α, coe := λ a, a.app } variables {F G} variables (η : applicative_transformation F G) @[functor_norm] lemma preserves_pure : ∀ {α} (x : α), η (pure x) = pure x := η.preserves_pure' @[functor_norm] lemma preserves_seq : ∀ {α β : Type u} (x : F (α → β)) (y : F α), η (x <*> y) = η x <*> η y := η.preserves_seq' @[functor_norm] lemma preserves_map {α β} (x : α → β) (y : F α) : η (x <$> y) = x <$> η y := by rw [← pure_seq_eq_map, η.preserves_seq]; simp with functor_norm end applicative_transformation open applicative_transformation class traversable (t : Type u → Type u) extends functor t := (traverse : Π {m : Type u → Type u} [applicative m] {α β}, (α → m β) → t α → m (t β)) open functor export traversable (traverse) section functions variables {t : Type u → Type u} variables {m : Type u → Type v} [applicative m] variables {α β : Type u} variables {f : Type u → Type u} [applicative f] def sequence [traversable t] : t (f α) → f (t α) := traverse id end functions class is_lawful_traversable (t : Type u → Type u) [traversable t] extends is_lawful_functor t : Type (u+1) := (id_traverse : ∀ {α} (x : t α), traverse id.mk x = x ) (comp_traverse : ∀ {F G} [applicative F] [applicative G] [is_lawful_applicative F] [is_lawful_applicative G] {α β γ} (f : β → F γ) (g : α → G β) (x : t α), traverse (comp.mk ∘ map f ∘ g) x = comp.mk (map (traverse f) (traverse g x))) (traverse_eq_map_id : ∀ {α β} (f : α → β) (x : t α), traverse (id.mk ∘ f) x = id.mk (f <$> x)) (naturality : ∀ {F G} [applicative F] [applicative G] [is_lawful_applicative F] [is_lawful_applicative G] (η : applicative_transformation F G) {α β} (f : α → F β) (x : t α), η (traverse f x) = traverse (@η _ ∘ f) x) instance : traversable id := ⟨λ _ _ _ _, id⟩ instance : is_lawful_traversable id := by refine {..}; intros; refl section variables {F : Type u → Type v} [applicative F] instance : traversable option := ⟨@option.traverse⟩ instance : traversable list := ⟨@list.traverse⟩ end namespace sum variables {σ : Type u} variables {F : Type u → Type u} variables [applicative F] protected def traverse {α β} (f : α → F β) : σ ⊕ α → F (σ ⊕ β) | (sum.inl x) := pure (sum.inl x) | (sum.inr x) := sum.inr <$> f x end sum instance {σ : Type u} : traversable.{u} (sum σ) := ⟨@sum.traverse _⟩
f2df4607d45bd99824fbbfd5c6c1b25d1b168b70
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/logic/unnamed_798.lean
5e16a094b9ce07a62c1a3d79860eb9a83e620d96
[]
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
611
lean
import data.real.basic def fn_ub (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a def fn_lb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x def fn_has_ub (f : ℝ → ℝ) := ∃ a, fn_ub f a def fn_has_lb (f : ℝ → ℝ) := ∃ a, fn_lb f a variables {f g : ℝ → ℝ} theorem fn_ub_add {f g : ℝ → ℝ} {a b : ℝ} (hfa : fn_ub f a) (hgb : fn_ub g b) : fn_ub (λ x, f x + g x) (a + b) := λ x, add_le_add (hfa x) (hgb x) -- BEGIN example : fn_has_ub f → fn_has_ub g → fn_has_ub (λ x, f x + g x) := λ ⟨a, ubfa⟩ ⟨b, ubfb⟩, ⟨a + b, fn_ub_add ubfa ubfb⟩ -- END
b4c69654f6685fa9a021d4918538fe2f655f606e
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/limits/shapes/finite_products.lean
27c9184bf1faa98da1727415756d4462d452e27c
[ "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
2,508
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_limits import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.terminal universes v u open category_theory namespace category_theory.limits variables (C : Type u) [category.{v} C] /-- A category has finite products if there is a chosen limit for every diagram with shape `discrete J`, where we have `[decidable_eq J]` and `[fintype J]`. -/ -- We can't simply make this an abbreviation, as we do with other `has_Xs` limits typeclasses, -- because of https://github.com/leanprover-community/lean/issues/429 def has_finite_products : Type (max (v+1) u) := Π (J : Type v) [decidable_eq J] [fintype J], has_limits_of_shape (discrete J) C attribute [class] has_finite_products instance has_limits_of_shape_discrete (J : Type v) [decidable_eq J] [fintype J] [has_finite_products C] : has_limits_of_shape (discrete J) C := ‹has_finite_products C› J /-- If `C` has finite limits then it has finite products. -/ def has_finite_products_of_has_finite_limits [has_finite_limits C] : has_finite_products C := λ J 𝒥₁ 𝒥₂, by exactI infer_instance /-- If a category has all products then in particular it has finite products. -/ def has_finite_products_of_has_products [has_products C] : has_finite_products C := by { dsimp [has_finite_products], apply_instance } /-- A category has finite coproducts if there is a chosen colimit for every diagram with shape `discrete J`, where we have `[decidable_eq J]` and `[fintype J]`. -/ def has_finite_coproducts : Type (max (v+1) u) := Π (J : Type v) [decidable_eq J] [fintype J], has_colimits_of_shape (discrete J) C attribute [class] has_finite_coproducts instance has_colimits_of_shape_discrete (J : Type v) [decidable_eq J] [fintype J] [has_finite_coproducts C] : has_colimits_of_shape (discrete J) C := ‹has_finite_coproducts C› J /-- If `C` has finite colimits then it has finite coproducts. -/ def has_finite_coproducts_of_has_finite_colimits [has_finite_colimits C] : has_finite_coproducts C := λ J 𝒥₁ 𝒥₂, by exactI infer_instance /-- If a category has all coproducts then in particular it has finite coproducts. -/ def has_finite_coproducts_of_has_coproducts [has_coproducts C] : has_finite_coproducts C := by { dsimp [has_finite_coproducts], apply_instance } end category_theory.limits
1e81fa9b0ba253ded750f5ac5c63872a30cbd02a
618003631150032a5676f229d13a079ac875ff77
/src/analysis/special_functions/exp_log.lean
f64d81f62e5a9aa53bfe45e05ab5a317aa7ff117
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
21,728
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne -/ import data.complex.exponential import analysis.complex.basic import analysis.calculus.mean_value /-! # Complex and real exponential, real logarithm ## Main statements This file establishes the basic analytical properties of the complex and real exponential functions (continuity, differentiability, computation of the derivative). It also contains the definition of the real logarithm function (as the inverse of the exponential on `(0, +∞)`, extended to `ℝ` by setting `log (-x) = log x`) and its basic properties (continuity, differentiability, formula for the derivative). The complex logarithm is *not* defined in this file as it relies on trigonometric functions. See instead `trigonometric.lean`. ## Tags exp, log -/ noncomputable theory open finset filter metric asymptotics open_locale classical topological_space namespace complex /-- The complex exponential is everywhere differentiable, with the derivative `exp x`. -/ lemma has_deriv_at_exp (x : ℂ) : has_deriv_at exp (exp x) x := begin rw has_deriv_at_iff_is_o_nhds_zero, have : (1 : ℕ) < 2 := by norm_num, refine (is_O.of_bound (∥exp x∥) _).trans_is_o (is_o_pow_id this), have : metric.ball (0 : ℂ) 1 ∈ nhds (0 : ℂ) := metric.ball_mem_nhds 0 zero_lt_one, apply filter.mem_sets_of_superset this (λz hz, _), simp only [metric.mem_ball, dist_zero_right] at hz, simp only [exp_zero, mul_one, one_mul, add_comm, normed_field.norm_pow, zero_add, set.mem_set_of_eq], calc ∥exp (x + z) - exp x - z * exp x∥ = ∥exp x * (exp z - 1 - z)∥ : by { congr, rw [exp_add], ring } ... = ∥exp x∥ * ∥exp z - 1 - z∥ : normed_field.norm_mul _ _ ... ≤ ∥exp x∥ * ∥z∥^2 : mul_le_mul_of_nonneg_left (abs_exp_sub_one_sub_id_le (le_of_lt hz)) (norm_nonneg _) end lemma differentiable_exp : differentiable ℂ exp := λx, (has_deriv_at_exp x).differentiable_at lemma differentiable_at_exp {x : ℂ} : differentiable_at ℂ exp x := differentiable_exp x @[simp] lemma deriv_exp : deriv exp = exp := funext $ λ x, (has_deriv_at_exp x).deriv @[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp | 0 := rfl | (n+1) := by rw [function.iterate_succ_apply, deriv_exp, iter_deriv_exp n] lemma continuous_exp : continuous exp := differentiable_exp.continuous end complex section variables {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ} lemma has_deriv_at.cexp (hf : has_deriv_at f f' x) : has_deriv_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') x := (complex.has_deriv_at_exp (f x)).comp x hf lemma has_deriv_within_at.cexp (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, complex.exp (f x)) (complex.exp (f x) * f') s x := (complex.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.cexp (hf : differentiable_within_at ℂ f s x) : differentiable_within_at ℂ (λ x, complex.exp (f x)) s x := hf.has_deriv_within_at.cexp.differentiable_within_at @[simp] lemma differentiable_at.cexp (hc : differentiable_at ℂ f x) : differentiable_at ℂ (λx, complex.exp (f x)) x := hc.has_deriv_at.cexp.differentiable_at lemma differentiable_on.cexp (hc : differentiable_on ℂ f s) : differentiable_on ℂ (λx, complex.exp (f x)) s := λx h, (hc x h).cexp @[simp] lemma differentiable.cexp (hc : differentiable ℂ f) : differentiable ℂ (λx, complex.exp (f x)) := λx, (hc x).cexp lemma deriv_within_cexp (hf : differentiable_within_at ℂ f s x) (hxs : unique_diff_within_at ℂ s x) : deriv_within (λx, complex.exp (f x)) s x = complex.exp (f x) * (deriv_within f s x) := hf.has_deriv_within_at.cexp.deriv_within hxs @[simp] lemma deriv_cexp (hc : differentiable_at ℂ f x) : deriv (λx, complex.exp (f x)) x = complex.exp (f x) * (deriv f x) := hc.has_deriv_at.cexp.deriv end namespace real variables {x y z : ℝ} lemma has_deriv_at_exp (x : ℝ) : has_deriv_at exp (exp x) x := has_deriv_at_real_of_complex (complex.has_deriv_at_exp x) lemma differentiable_exp : differentiable ℝ exp := λx, (has_deriv_at_exp x).differentiable_at lemma differentiable_at_exp : differentiable_at ℝ exp x := differentiable_exp x @[simp] lemma deriv_exp : deriv exp = exp := funext $ λ x, (has_deriv_at_exp x).deriv @[simp] lemma iter_deriv_exp : ∀ n : ℕ, (deriv^[n] exp) = exp | 0 := rfl | (n+1) := by rw [function.iterate_succ_apply, deriv_exp, iter_deriv_exp n] lemma continuous_exp : continuous exp := differentiable_exp.continuous end real section /-! Register lemmas for the derivatives of the composition of `real.exp`, `real.cos`, `real.sin`, `real.cosh` and `real.sinh` with a differentiable function, for standalone use and use with `simp`. -/ variables {f : ℝ → ℝ} {f' x : ℝ} {s : set ℝ} /-! `real.exp`-/ lemma has_deriv_at.exp (hf : has_deriv_at f f' x) : has_deriv_at (λ x, real.exp (f x)) (real.exp (f x) * f') x := (real.has_deriv_at_exp (f x)).comp x hf lemma has_deriv_within_at.exp (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, real.exp (f x)) (real.exp (f x) * f') s x := (real.has_deriv_at_exp (f x)).comp_has_deriv_within_at x hf lemma differentiable_within_at.exp (hf : differentiable_within_at ℝ f s x) : differentiable_within_at ℝ (λ x, real.exp (f x)) s x := hf.has_deriv_within_at.exp.differentiable_within_at @[simp] lemma differentiable_at.exp (hc : differentiable_at ℝ f x) : differentiable_at ℝ (λx, real.exp (f x)) x := hc.has_deriv_at.exp.differentiable_at lemma differentiable_on.exp (hc : differentiable_on ℝ f s) : differentiable_on ℝ (λx, real.exp (f x)) s := λx h, (hc x h).exp @[simp] lemma differentiable.exp (hc : differentiable ℝ f) : differentiable ℝ (λx, real.exp (f x)) := λx, (hc x).exp lemma deriv_within_exp (hf : differentiable_within_at ℝ f s x) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, real.exp (f x)) s x = real.exp (f x) * (deriv_within f s x) := hf.has_deriv_within_at.exp.deriv_within hxs @[simp] lemma deriv_exp (hc : differentiable_at ℝ f x) : deriv (λx, real.exp (f x)) x = real.exp (f x) * (deriv f x) := hc.has_deriv_at.exp.deriv end namespace real variables {x y z : ℝ} lemma exists_exp_eq_of_pos {x : ℝ} (hx : 0 < x) : ∃ y, exp y = x := have ∀ {z:ℝ}, 1 ≤ z → z ∈ set.range exp, from λ z hz, intermediate_value_univ 0 (z - 1) continuous_exp ⟨by simpa, by simpa using add_one_le_exp_of_nonneg (sub_nonneg.2 hz)⟩, match le_total x 1 with | (or.inl hx1) := let ⟨y, hy⟩ := this (one_le_inv hx hx1) in ⟨-y, by rw [exp_neg, hy, inv_inv']⟩ | (or.inr hx1) := this hx1 end /-- The real logarithm function, equal to the inverse of the exponential for `x > 0`, to `log |x|` for `x < 0`, and to `0` for `0`. We use this unconventional extension to `(-∞, 0]` as it gives the formula `log (x * y) = log x + log y` for all nonzero `x` and `y`, and the derivative of `log` is `1/x` away from `0`. -/ noncomputable def log (x : ℝ) : ℝ := if hx : x ≠ 0 then classical.some (exists_exp_eq_of_pos (abs_pos_iff.mpr hx)) else 0 lemma exp_log_eq_abs (hx : x ≠ 0) : exp (log x) = abs x := by { rw [log, dif_pos hx], exact classical.some_spec (exists_exp_eq_of_pos ((abs_pos_iff.mpr hx))) } lemma exp_log (hx : 0 < x) : exp (log x) = x := by { rw exp_log_eq_abs (ne_of_gt hx), exact abs_of_pos hx } @[simp] lemma log_exp (x : ℝ) : log (exp x) = x := exp_injective $ exp_log (exp_pos x) @[simp] lemma log_zero : log 0 = 0 := by simp [log] @[simp] lemma log_one : log 1 = 0 := exp_injective $ by rw [exp_log zero_lt_one, exp_zero] @[simp] lemma log_abs (x : ℝ) : log (abs x) = log x := begin by_cases h : x = 0, { simp [h] }, { apply exp_injective, rw [exp_log_eq_abs h, exp_log_eq_abs, abs_abs], simp [h] } end @[simp] lemma log_neg_eq_log (x : ℝ) : log (-x) = log x := by rw [← log_abs x, ← log_abs (-x), abs_neg] lemma log_mul (hx : x ≠ 0) (hy : y ≠ 0) : log (x * y) = log x + log y := exp_injective $ by rw [exp_log_eq_abs (mul_ne_zero hx hy), exp_add, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_mul] lemma log_le_log (h : 0 < x) (h₁ : 0 < y) : real.log x ≤ real.log y ↔ x ≤ y := ⟨λ h₂, by rwa [←real.exp_le_exp, real.exp_log h, real.exp_log h₁] at h₂, λ h₂, (real.exp_le_exp).1 $ by rwa [real.exp_log h₁, real.exp_log h]⟩ lemma log_lt_log (hx : 0 < x) : x < y → log x < log y := by { intro h, rwa [← exp_lt_exp, exp_log hx, exp_log (lt_trans hx h)] } lemma log_lt_log_iff (hx : 0 < x) (hy : 0 < y) : log x < log y ↔ x < y := by { rw [← exp_lt_exp, exp_log hx, exp_log hy] } lemma log_pos_iff (hx : 0 < x) : 0 < log x ↔ 1 < x := by { rw ← log_one, exact log_lt_log_iff (by norm_num) hx } lemma log_pos (hx : 1 < x) : 0 < log x := (log_pos_iff (lt_trans zero_lt_one hx)).2 hx lemma log_neg_iff (h : 0 < x) : log x < 0 ↔ x < 1 := by { rw ← log_one, exact log_lt_log_iff h (by norm_num) } lemma log_neg (h0 : 0 < x) (h1 : x < 1) : log x < 0 := (log_neg_iff h0).2 h1 lemma log_nonneg : 1 ≤ x → 0 ≤ log x := by { intro, rwa [← log_one, log_le_log], norm_num, linarith } lemma log_nonpos (hx : 0 ≤ x) (h'x : x ≤ 1) : log x ≤ 0 := begin by_cases x_zero : x = 0, { simp [x_zero] }, { rwa [← log_one, log_le_log (lt_of_le_of_ne hx (ne.symm x_zero))], norm_num } end section prove_log_is_continuous lemma tendsto_log_one_zero : tendsto log (𝓝 1) (𝓝 0) := begin rw tendsto_nhds_nhds, assume ε ε0, let δ := min (exp ε - 1) (1 - exp (-ε)), have : 0 < δ, refine lt_min (sub_pos_of_lt (by rwa one_lt_exp_iff)) (sub_pos_of_lt _), by { rw exp_lt_one_iff, linarith }, use [δ, this], assume x h, cases le_total 1 x with hx hx, { have h : x < exp ε, rw [dist_eq, abs_of_nonneg (sub_nonneg_of_le hx)] at h, linarith [(min_le_left _ _ : δ ≤ exp ε - 1)], calc abs (log x - 0) = abs (log x) : by simp ... = log x : abs_of_nonneg $ log_nonneg hx ... < ε : by { rwa [← exp_lt_exp, exp_log], linarith }}, { have h : exp (-ε) < x, rw [dist_eq, abs_of_nonpos (sub_nonpos_of_le hx)] at h, linarith [(min_le_right _ _ : δ ≤ 1 - exp (-ε))], have : 0 < x := lt_trans (exp_pos _) h, calc abs (log x - 0) = abs (log x) : by simp ... = -log x : abs_of_nonpos $ log_nonpos (le_of_lt this) hx ... < ε : by { rw [neg_lt, ← exp_lt_exp, exp_log], assumption' } } end lemma continuous_log' : continuous (λx : {x:ℝ // 0 < x}, log x.val) := continuous_iff_continuous_at.2 $ λ x, begin rw continuous_at, let f₁ := λ h:{h:ℝ // 0 < h}, log (x.1 * h.1), let f₂ := λ y:{y:ℝ // 0 < y}, subtype.mk (x.1 ⁻¹ * y.1) (mul_pos (inv_pos.2 x.2) y.2), have H1 : tendsto f₁ (𝓝 ⟨1, zero_lt_one⟩) (𝓝 (log (x.1*1))), have : f₁ = λ h:{h:ℝ // 0 < h}, log x.1 + log h.1, ext h, rw ← log_mul (ne_of_gt x.2) (ne_of_gt h.2), simp only [this, log_mul (ne_of_gt x.2) one_ne_zero, log_one], exact tendsto_const_nhds.add (tendsto.comp tendsto_log_one_zero continuous_at_subtype_val), have H2 : tendsto f₂ (𝓝 x) (𝓝 ⟨x.1⁻¹ * x.1, mul_pos (inv_pos.2 x.2) x.2⟩), rw tendsto_subtype_rng, exact tendsto_const_nhds.mul continuous_at_subtype_val, suffices h : tendsto (f₁ ∘ f₂) (𝓝 x) (𝓝 (log x.1)), begin convert h, ext y, have : x.val * (x.val⁻¹ * y.val) = y.val, rw [← mul_assoc, mul_inv_cancel (ne_of_gt x.2), one_mul], show log (y.val) = log (x.val * (x.val⁻¹ * y.val)), rw this end, exact tendsto.comp (by rwa mul_one at H1) (by { simp only [inv_mul_cancel (ne_of_gt x.2)] at H2, assumption }) end lemma continuous_at_log (hx : 0 < x) : continuous_at log x := continuous_within_at.continuous_at (continuous_on_iff_continuous_restrict.2 continuous_log' _ hx) (mem_nhds_sets (is_open_lt' _) hx) /-- Three forms of the continuity of `real.log` are provided. For the other two forms, see `real.continuous_log'` and `real.continuous_at_log` -/ lemma continuous_log {α : Type*} [topological_space α] {f : α → ℝ} (h : ∀a, 0 < f a) (hf : continuous f) : continuous (λa, log (f a)) := show continuous ((log ∘ @subtype.val ℝ (λr, 0 < r)) ∘ λa, ⟨f a, h a⟩), from continuous_log'.comp (continuous_subtype_mk _ hf) end prove_log_is_continuous lemma has_deriv_at_log_of_pos (hx : 0 < x) : has_deriv_at log x⁻¹ x := have has_deriv_at log (exp $ log x)⁻¹ x, from (has_deriv_at_exp $ log x).of_local_left_inverse (continuous_at_log hx) (ne_of_gt $ exp_pos _) $ eventually.mono (mem_nhds_sets is_open_Ioi hx) @exp_log, by rwa [exp_log hx] at this lemma has_deriv_at_log (hx : x ≠ 0) : has_deriv_at log x⁻¹ x := begin by_cases h : 0 < x, { exact has_deriv_at_log_of_pos h }, push_neg at h, convert ((has_deriv_at_log_of_pos (neg_pos.mpr (lt_of_le_of_ne h hx))) .comp x (has_deriv_at_id x).neg), { ext y, exact (log_neg_eq_log y).symm }, { field_simp [hx] } end end real section log_differentiable open real variables {f : ℝ → ℝ} {x f' : ℝ} {s : set ℝ} lemma has_deriv_within_at.log (hf : has_deriv_within_at f f' s x) (hx : f x ≠ 0) : has_deriv_within_at (λ y, log (f y)) (f' / (f x)) s x := begin convert (has_deriv_at_log hx).comp_has_deriv_within_at x hf, field_simp end lemma has_deriv_at.log (hf : has_deriv_at f f' x) (hx : f x ≠ 0) : has_deriv_at (λ y, log (f y)) (f' / f x) x := begin rw ← has_deriv_within_at_univ at *, exact hf.log hx end lemma differentiable_within_at.log (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) : differentiable_within_at ℝ (λx, log (f x)) s x := (hf.has_deriv_within_at.log hx).differentiable_within_at @[simp] lemma differentiable_at.log (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : differentiable_at ℝ (λx, log (f x)) x := (hf.has_deriv_at.log hx).differentiable_at lemma differentiable_on.log (hf : differentiable_on ℝ f s) (hx : ∀ x ∈ s, f x ≠ 0) : differentiable_on ℝ (λx, log (f x)) s := λx h, (hf x h).log (hx x h) @[simp] lemma differentiable.log (hf : differentiable ℝ f) (hx : ∀ x, f x ≠ 0) : differentiable ℝ (λx, log (f x)) := λx, (hf x).log (hx x) lemma deriv_within_log' (hf : differentiable_within_at ℝ f s x) (hx : f x ≠ 0) (hxs : unique_diff_within_at ℝ s x) : deriv_within (λx, log (f x)) s x = (deriv_within f s x) / (f x) := (hf.has_deriv_within_at.log hx).deriv_within hxs @[simp] lemma deriv_log' (hf : differentiable_at ℝ f x) (hx : f x ≠ 0) : deriv (λx, log (f x)) x = (deriv f x) / (f x) := (hf.has_deriv_at.log hx).deriv end log_differentiable namespace real /-- The real exponential function tends to `+∞` at `+∞`. -/ lemma tendsto_exp_at_top : tendsto exp at_top at_top := begin have A : tendsto (λx:ℝ, x + 1) at_top at_top := tendsto_at_top_add_const_right at_top 1 tendsto_id, have B : ∀ᶠ x in at_top, x + 1 ≤ exp x, { have : ∀ᶠ (x : ℝ) in at_top, 0 ≤ x := mem_at_top 0, filter_upwards [this], exact λx hx, add_one_le_exp_of_nonneg hx }, exact tendsto_at_top_mono' at_top B A end /-- The real exponential function tends to 0 at -infinity or, equivalently, `exp(-x)` tends to `0` at +infinity -/ lemma tendsto_exp_neg_at_top_nhds_0 : tendsto (λx, exp (-x)) at_top (𝓝 0) := (tendsto_inv_at_top_zero.comp (tendsto_exp_at_top)).congr (λx, (exp_neg x).symm) /-- The function `exp(x)/x^n` tends to +infinity at +infinity, for any natural number `n` -/ lemma tendsto_exp_div_pow_at_top (n : ℕ) : tendsto (λx, exp x / x^n) at_top at_top := begin have n_pos : (0 : ℝ) < n + 1 := nat.cast_add_one_pos n, have n_ne_zero : (n : ℝ) + 1 ≠ 0 := ne_of_gt n_pos, have A : ∀x:ℝ, 0 < x → exp (x / (n+1)) / (n+1)^n ≤ exp x / x^n, { assume x hx, let y := x / (n+1), have y_pos : 0 < y := div_pos hx n_pos, have : exp (x / (n+1)) ≤ (n+1)^n * (exp x / x^n), from calc exp y = exp y * 1 : by simp ... ≤ exp y * (exp y / y)^n : begin apply mul_le_mul_of_nonneg_left (one_le_pow_of_one_le _ n) (le_of_lt (exp_pos _)), apply one_le_div_of_le _ y_pos, apply le_trans _ (add_one_le_exp_of_nonneg (le_of_lt y_pos)), exact le_add_of_le_of_nonneg (le_refl _) (zero_le_one) end ... = exp y * exp (n * y) / y^n : by rw [div_pow, exp_nat_mul, mul_div_assoc] ... = exp ((n + 1) * y) / y^n : by rw [← exp_add, add_mul, one_mul, add_comm] ... = exp x / (x / (n+1))^n : by { dsimp [y], rw mul_div_cancel' _ n_ne_zero } ... = (n+1)^n * (exp x / x^n) : by rw [← mul_div_assoc, div_pow, div_div_eq_mul_div, mul_comm], rwa div_le_iff' (pow_pos n_pos n) }, have B : ∀ᶠ x in at_top, exp (x / (n+1)) / (n+1)^n ≤ exp x / x^n := mem_at_top_sets.2 ⟨1, λx hx, A _ (lt_of_lt_of_le zero_lt_one hx)⟩, have C : tendsto (λx, exp (x / (n+1)) / (n+1)^n) at_top at_top := tendsto_at_top_div (pow_pos n_pos n) (tendsto_exp_at_top.comp (tendsto_at_top_div (nat.cast_add_one_pos n) tendsto_id)), exact tendsto_at_top_mono' at_top B C end /-- The function `x^n * exp(-x)` tends to `0` at `+∞`, for any natural number `n`. -/ lemma tendsto_pow_mul_exp_neg_at_top_nhds_0 (n : ℕ) : tendsto (λx, x^n * exp (-x)) at_top (𝓝 0) := (tendsto_inv_at_top_zero.comp (tendsto_exp_div_pow_at_top n)).congr $ λx, by rw [function.comp_app, inv_eq_one_div, div_div_eq_mul_div, one_mul, div_eq_mul_inv, exp_neg] open_locale big_operators /-- A crude lemma estimating the difference between `log (1-x)` and its Taylor series at `0`, where the main point of the bound is that it tends to `0`. The goal is to deduce the series expansion of the logarithm, in `has_sum_pow_div_log_of_abs_lt_1`. -/ lemma abs_log_sub_add_sum_range_le {x : ℝ} (h : abs x < 1) (n : ℕ) : abs ((∑ i in range n, x^(i+1)/(i+1)) + log (1-x)) ≤ (abs x)^(n+1) / (1 - abs x) := begin /- For the proof, we show that the derivative of the function to be estimated is small, and then apply the mean value inequality. -/ let F : ℝ → ℝ := λ x, ∑ i in range n, x^(i+1)/(i+1) + log (1-x), -- First step: compute the derivative of `F` have A : ∀ y ∈ set.Ioo (-1 : ℝ) 1, deriv F y = - (y^n) / (1 - y), { assume y hy, have : (∑ i in range n, (↑i + 1) * y ^ i / (↑i + 1)) = (∑ i in range n, y ^ i), { congr, ext i, have : (i : ℝ) + 1 ≠ 0 := ne_of_gt (nat.cast_add_one_pos i), field_simp [this, mul_comm] }, field_simp [F, this, ← geom_series_def, geom_sum (ne_of_lt hy.2), sub_ne_zero_of_ne (ne_of_gt hy.2), sub_ne_zero_of_ne (ne_of_lt hy.2)], ring }, -- second step: show that the derivative of `F` is small have B : ∀ y ∈ set.Icc (-abs x) (abs x), abs (deriv F y) ≤ (abs x)^n / (1 - abs x), { assume y hy, have : y ∈ set.Ioo (-(1 : ℝ)) 1 := ⟨lt_of_lt_of_le (neg_lt_neg h) hy.1, lt_of_le_of_lt hy.2 h⟩, calc abs (deriv F y) = abs (-(y^n) / (1 - y)) : by rw [A y this] ... ≤ (abs x)^n / (1 - abs x) : begin have : abs y ≤ abs x := abs_le_of_le_of_neg_le hy.2 (by linarith [hy.1]), have : 0 < 1 - abs x, by linarith, have : 1 - abs x ≤ abs (1 - y) := le_trans (by linarith [hy.2]) (le_abs_self _), simp only [← pow_abs, abs_div, abs_neg], apply_rules [div_le_div, pow_nonneg, abs_nonneg, pow_le_pow_of_le_left] end }, -- third step: apply the mean value inequality have C : ∥F x - F 0∥ ≤ ((abs x)^n / (1 - abs x)) * ∥x - 0∥, { have : ∀ y ∈ set.Icc (- abs x) (abs x), differentiable_at ℝ F y, { assume y hy, have : 1 - y ≠ 0 := sub_ne_zero_of_ne (ne_of_gt (lt_of_le_of_lt hy.2 h)), simp [F, this] }, apply convex.norm_image_sub_le_of_norm_deriv_le this B (convex_Icc _ _) _ _, { simpa using abs_nonneg x }, { simp [le_abs_self x, neg_le.mp (neg_le_abs_self x)] } }, -- fourth step: conclude by massaging the inequality of the third step simpa [F, norm_eq_abs, div_mul_eq_mul_div, pow_succ'] using C end /-- Power series expansion of the logarithm around `1`. -/ theorem has_sum_pow_div_log_of_abs_lt_1 {x : ℝ} (h : abs x < 1) : has_sum (λ (n : ℕ), x ^ (n + 1) / (n + 1)) (-log (1 - x)) := begin rw has_sum_iff_tendsto_nat_of_summable, show tendsto (λ (n : ℕ), ∑ (i : ℕ) in range n, x ^ (i + 1) / (i + 1)) at_top (𝓝 (-log (1 - x))), { rw [tendsto_iff_norm_tendsto_zero], simp only [norm_eq_abs, sub_neg_eq_add], refine squeeze_zero (λ n, abs_nonneg _) (abs_log_sub_add_sum_range_le h) _, suffices : tendsto (λ (t : ℕ), abs x ^ (t + 1) / (1 - abs x)) at_top (𝓝 (abs x * 0 / (1 - abs x))), by simpa, simp only [pow_succ], refine (tendsto_const_nhds.mul _).div_const, exact tendsto_pow_at_top_nhds_0_of_lt_1 (abs_nonneg _) h }, show summable (λ (n : ℕ), x ^ (n + 1) / (n + 1)), { refine summable_of_norm_bounded _ (summable_geometric_of_lt_1 (abs_nonneg _) h) (λ i, _), calc ∥x ^ (i + 1) / (i + 1)∥ = abs x ^ (i+1) / (i+1) : begin have : (0 : ℝ) ≤ i + 1 := le_of_lt (nat.cast_add_one_pos i), rw [norm_eq_abs, abs_div, ← pow_abs, abs_of_nonneg this], end ... ≤ abs x ^ (i+1) / (0 + 1) : begin apply_rules [div_le_div_of_le_left, pow_nonneg, abs_nonneg, add_le_add_right (nat.cast_nonneg i)], norm_num, end ... ≤ abs x ^ i : by simpa [pow_succ'] using mul_le_of_le_one_right (pow_nonneg (abs_nonneg x) i) (le_of_lt h) } end end real
ec8b8cc41e9bd6aeefc074b706b88f2fb652e027
46125763b4dbf50619e8846a1371029346f4c3db
/src/analysis/specific_limits.lean
d1d2b47159a0794331d808e0c444bbd91d499e0d
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
19,795
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 algebra.geom_sum import topology.instances.ennreal noncomputable theory open_locale classical topological_space open classical function lattice filter finset metric 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 add_monoid.smul_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 add_monoid.smul_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 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 C0) hx, exact ⟨inv_pos 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, (range n).sum (λi, 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_pow_at_top_at_top_of_gt_1 {r : ℝ} (h : 1 < r) : tendsto (λn:ℕ, r ^ n) at_top at_top := (tendsto_at_top_at_top _).2 $ assume p, let ⟨n, hn⟩ := pow_unbounded_of_one_lt p h in ⟨n, λ m hnm, le_of_lt $ lt_of_lt_of_le hn (pow_le_pow (le_of_lt h) hnm)⟩ 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_gt_1 $ 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_lt_1_normed_field {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_at_top_of_gt_1_nat {k : ℕ} (h : 1 < k) : tendsto (λn:ℕ, k ^ n) at_top at_top := tendsto_coe_nat_real_at_top_iff.1 $ have hr : 1 < (k : ℝ), by rw [← nat.cast_one, nat.cast_lt]; exact h, by simpa using tendsto_pow_at_top_at_top_of_gt_1 hr 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 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) lemma has_sum_geometric {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, (range n).sum (λ i, 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 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : summable (λn:ℕ, r ^ n) := ⟨_, has_sum_geometric h₁ h₂⟩ lemma tsum_geometric {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : (∑n:ℕ, r ^ n) = (1 - r)⁻¹ := tsum_eq_has_sum (has_sum_geometric h₁ h₂) lemma has_sum_geometric_two : has_sum (λn:ℕ, ((1:ℝ)/2) ^ n) 2 := by convert has_sum_geometric _ _; 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 (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, refl, }, { norm_num, rw div_mul_cancel, 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 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) = (range n).sum (λ _, 1) : by rw [sum_const, add_monoid.smul_one, card_range] ... ≤ (range n).sum (pow r) : sum_le_sum (λ k _, this k) } end /-- 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 summable_comp_of_summable_of_injective f (summable_spec hf) (@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 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.mul_tsum, 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.mul_tsum, 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 rnonneg hr } end variables (r C) /-- If `edist (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 $ has_sum_mul_left _ this) 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 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.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.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
bf8e08c32fdea3cc98ae6549d4c8d6fa27f526a8
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/group_theory/group_action/units.lean
4601235dc869f0f9cead1beee62c3c6227a1aedb
[ "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
5,028
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import group_theory.group_action.defs /-! # Group actions on and by `units M` This file provides the action of a unit on a type `α`, `has_scalar (units M) α`, in the presence of `has_scalar M α`, with the obvious definition stated in `units.smul_def`. This definition preserves `mul_action` and `distrib_mul_action` structures too. Additionally, a `mul_action G M` for some group `G` satisfying some additional properties admits a `mul_action G (units M)` structure, again with the obvious definition stated in `units.coe_smul`. These instances use a primed name. The results are repeated for `add_units` and `has_vadd` where relevant. -/ variables {G H M N : Type*} {α : Type*} namespace units /-! ### Action of the units of `M` on a type `α` -/ @[to_additive] instance [monoid M] [has_scalar M α] : has_scalar (units M) α := { smul := λ m a, (m : M) • a } @[to_additive] lemma smul_def [monoid M] [has_scalar M α] (m : units M) (a : α) : m • a = (m : M) • a := rfl lemma _root_.is_unit.inv_smul [monoid α] {a : α} (h : is_unit a) : (h.unit)⁻¹ • a = 1 := h.coe_inv_mul @[to_additive] instance [monoid M] [has_scalar M α] [has_faithful_scalar M α] : has_faithful_scalar (units M) α := { eq_of_smul_eq_smul := λ u₁ u₂ h, units.ext $ eq_of_smul_eq_smul h, } @[to_additive] instance [monoid M] [mul_action M α] : mul_action (units M) α := { one_smul := (one_smul M : _), mul_smul := λ m n, mul_smul (m : M) n, } instance [monoid M] [add_monoid α] [distrib_mul_action M α] : distrib_mul_action (units M) α := { smul_add := λ m, smul_add (m : M), smul_zero := λ m, smul_zero m, } instance [monoid M] [monoid α] [mul_distrib_mul_action M α] : mul_distrib_mul_action (units M) α := { smul_mul := λ m, smul_mul' (m : M), smul_one := λ m, smul_one m, } instance smul_comm_class_left [monoid M] [has_scalar M α] [has_scalar N α] [smul_comm_class M N α] : smul_comm_class (units M) N α := { smul_comm := λ m n, (smul_comm (m : M) n : _)} instance smul_comm_class_right [monoid N] [has_scalar M α] [has_scalar N α] [smul_comm_class M N α] : smul_comm_class M (units N) α := { smul_comm := λ m n, (smul_comm m (n : N) : _)} instance [monoid M] [has_scalar M N] [has_scalar M α] [has_scalar N α] [is_scalar_tower M N α] : is_scalar_tower (units M) N α := { smul_assoc := λ m n, (smul_assoc (m : M) n : _)} /-! ### Action of a group `G` on units of `M` -/ /-- If an action `G` associates and commutes with multiplication on `M`, then it lifts to an action on `units M`. Notably, this provides `mul_action (units M) (units N)` under suitable conditions. -/ instance mul_action' [group G] [monoid M] [mul_action G M] [smul_comm_class G M M] [is_scalar_tower G M M] : mul_action G (units M) := { smul := λ g m, ⟨g • (m : M), g⁻¹ • ↑(m⁻¹), by rw [smul_mul_smul, units.mul_inv, mul_right_inv, one_smul], by rw [smul_mul_smul, units.inv_mul, mul_left_inv, one_smul]⟩, one_smul := λ m, units.ext $ one_smul _ _, mul_smul := λ g₁ g₂ m, units.ext $ mul_smul _ _ _ } @[simp] lemma coe_smul [group G] [monoid M] [mul_action G M] [smul_comm_class G M M] [is_scalar_tower G M M] (g : G) (m : units M) : ↑(g • m) = g • (m : M) := rfl /-- Note that this lemma exists more generally as the global `smul_inv` -/ @[simp] lemma smul_inv [group G] [monoid M] [mul_action G M] [smul_comm_class G M M] [is_scalar_tower G M M] (g : G) (m : units M) : (g • m)⁻¹ = g⁻¹ • m⁻¹ := ext rfl /-- Transfer `smul_comm_class G H M` to `smul_comm_class G H (units M)` -/ instance smul_comm_class' [group G] [group H] [monoid M] [mul_action G M] [smul_comm_class G M M] [mul_action H M] [smul_comm_class H M M] [is_scalar_tower G M M] [is_scalar_tower H M M] [smul_comm_class G H M] : smul_comm_class G H (units M) := { smul_comm := λ g h m, units.ext $ smul_comm g h (m : M) } /-- Transfer `is_scalar_tower G H M` to `is_scalar_tower G H (units M)` -/ instance is_scalar_tower' [has_scalar G H] [group G] [group H] [monoid M] [mul_action G M] [smul_comm_class G M M] [mul_action H M] [smul_comm_class H M M] [is_scalar_tower G M M] [is_scalar_tower H M M] [is_scalar_tower G H M] : is_scalar_tower G H (units M) := { smul_assoc := λ g h m, units.ext $ smul_assoc g h (m : M) } /-- Transfer `is_scalar_tower G M α` to `is_scalar_tower G (units M) α` -/ instance is_scalar_tower'_left [group G] [monoid M] [mul_action G M] [has_scalar M α] [has_scalar G α] [smul_comm_class G M M] [is_scalar_tower G M M] [is_scalar_tower G M α] : is_scalar_tower G (units M) α := { smul_assoc := λ g m, (smul_assoc g (m : M) : _)} -- Just to prove this transfers a particularly useful instance. example [monoid M] [monoid N] [mul_action M N] [smul_comm_class M N N] [is_scalar_tower M N N] : mul_action (units M) (units N) := units.mul_action' end units
24ee5631ac74f42c46c3721f0fbd2e50e642136f
626e312b5c1cb2d88fca108f5933076012633192
/src/number_theory/primes_congruent_one.lean
2bb4dc0da0d4b501fd5b09ae37c61ecb88e5c091
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,674
lean
/- Copyright (c) 2020 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import ring_theory.polynomial.cyclotomic import topology.algebra.polynomial import field_theory.finite.basic /-! # Primes congruent to one We prove that, for any positive `k : ℕ`, there are infinitely many primes `p` such that `p ≡ 1 [MOD k]`. -/ namespace nat open polynomial nat filter /-- For any positive `k : ℕ` there are infinitely many primes `p` such that `p ≡ 1 [MOD k]`. -/ lemma exists_prime_ge_modeq_one (k n : ℕ) (hpos : 0 < k) : ∃ (p : ℕ), nat.prime p ∧ n ≤ p ∧ p ≡ 1 [MOD k] := begin have hli : tendsto (abs ∘ (λ (a : ℕ), abs(a : ℚ))) at_top at_top, { simp only [(∘), abs_cast], exact nat.strict_mono_cast.monotone.tendsto_at_top_at_top exists_nat_ge }, have hcff : int.cast_ring_hom ℚ (cyclotomic k ℤ).leading_coeff ≠ 0, { simp only [cyclotomic.monic, ring_hom.eq_int_cast, monic.leading_coeff, int.cast_one, ne.def, not_false_iff, one_ne_zero] }, obtain ⟨a, ha⟩ := tendsto_at_top_at_top.1 (tendsto_abv_eval₂_at_top (int.cast_ring_hom ℚ) abs (cyclotomic k ℤ) (degree_cyclotomic_pos k ℤ hpos) hcff hli) 2, let b := a * (k * n.factorial), have hgt : 1 < (eval ↑(a * (k * n.factorial)) (cyclotomic k ℤ)).nat_abs, { suffices hgtabs : 1 < abs (eval ↑b (cyclotomic k ℤ)), { rw [int.abs_eq_nat_abs] at hgtabs, exact_mod_cast hgtabs }, suffices hgtrat : 1 < abs (eval ↑b (cyclotomic k ℚ)), { rw [← map_cyclotomic_int k ℚ, ← int.cast_coe_nat, ← int.coe_cast_ring_hom, eval_map, eval₂_hom, int.coe_cast_ring_hom] at hgtrat, assumption_mod_cast }, suffices hleab : a ≤ b, { replace ha := lt_of_lt_of_le one_lt_two (ha b hleab), rwa [← eval_map, map_cyclotomic_int k ℚ, abs_cast] at ha }, exact le_mul_of_pos_right (mul_pos hpos (factorial_pos n)) }, let p := min_fac (eval ↑b (cyclotomic k ℤ)).nat_abs, haveI hprime : fact p.prime := ⟨min_fac_prime (ne_of_lt hgt).symm⟩, have hroot : is_root (cyclotomic k (zmod p)) (cast_ring_hom (zmod p) b), { rw [is_root.def, ← map_cyclotomic_int k (zmod p), eval_map, coe_cast_ring_hom, ← int.cast_coe_nat, ← int.coe_cast_ring_hom, eval₂_hom, int.coe_cast_ring_hom, zmod.int_coe_zmod_eq_zero_iff_dvd _ _], apply int.dvd_nat_abs.1, exact_mod_cast min_fac_dvd (eval ↑b (cyclotomic k ℤ)).nat_abs }, refine ⟨p, hprime.1, _, _⟩, { by_contra habs, exact (prime.dvd_iff_not_coprime hprime.1).1 (dvd_factorial (min_fac_pos _) (le_of_not_ge habs)) (coprime_of_root_cyclotomic hpos hroot).symm.coprime_mul_left_right.coprime_mul_left_right }, { have hdiv := order_of_dvd_of_pow_eq_one (zmod.units_pow_card_sub_one_eq_one p (zmod.unit_of_coprime b (coprime_of_root_cyclotomic hpos hroot))), have : ¬p ∣ k := hprime.1.coprime_iff_not_dvd.1 (coprime_of_root_cyclotomic hpos hroot).symm.coprime_mul_left_right.coprime_mul_right_right, rw [order_of_root_cyclotomic hpos this hroot] at hdiv, exact ((modeq_iff_dvd' hprime.1.pos).2 hdiv).symm } end lemma frequently_at_top_modeq_one (k : ℕ) (hpos : 0 < k) : ∃ᶠ p in at_top, nat.prime p ∧ p ≡ 1 [MOD k] := begin refine frequently_at_top.2 (λ n, _), obtain ⟨p, hp⟩ := exists_prime_ge_modeq_one k n hpos, exact ⟨p, ⟨hp.2.1, hp.1, hp.2.2⟩⟩ end lemma infinite_set_of_prime_modeq_one (k : ℕ) (hpos : 0 < k) : set.infinite {p : ℕ | nat.prime p ∧ p ≡ 1 [MOD k]} := frequently_at_top_iff_infinite.1 (frequently_at_top_modeq_one k hpos) end nat
ccbd169b8efd82f2255b0dca5b3db07abb50df30
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/data/padics/padic_numbers.lean
b54b50cc5acc0541e806b5bc04e03de1a45e4b0b
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
32,575
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import data.real.cau_seq_completion topology.metric_space.cau_seq_filter import data.padics.padic_norm algebra.archimedean analysis.normed_space.basic import tactic.norm_cast /-! # p-adic numbers This file defines the p-adic numbers (rationals) ℚ_p as the completion of ℚ with respect to the p-adic norm. We show that the p-adic norm on ℚ extends to ℚ_p, that ℚ is embedded in ℚ_p, and that ℚ_p is Cauchy complete. ## Important definitions * `padic` : the type of p-adic numbers * `padic_norm_e` : the rational ralued p-adic norm on ℚ_p ## Notation We introduce the notation ℚ_[p] for the p-adic numbers. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking (prime p) as a type class argument. We use the same concrete Cauchy sequence construction that is used to construct ℝ. ℚ_p inherits a field structure from this construction. The extension of the norm on ℚ to ℚ_p is *not* analogous to extending the absolute value to ℝ, and hence the proof that ℚ_p is complete is different from the proof that ℝ is complete. A small special-purpose simplification tactic, `padic_index_simp`, is used to manipulate sequence indices in the proof that the norm extends. `padic_norm_e` is the rational-valued p-adic norm on ℚ_p. To instantiate ℚ_p as a normed field, we must cast this into a ℝ-valued norm. The ℝ-valued norm, using notation ∥ ∥ from normed spaces, is the canonical representation of this norm. Coercions from ℚ to ℚ_p are set up to work with the `norm_cast` tactic. ## References * [F. Q. Gouêva, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * https://en.wikipedia.org/wiki/P-adic_number ## Tags p-adic, p adic, padic, norm, valuation, cauchy, completion, p-adic completion -/ noncomputable theory local attribute [instance, priority 1] classical.prop_decidable open nat multiplicity padic_norm cau_seq cau_seq.completion metric /-- The type of Cauchy sequences of rationals with respect to the p-adic norm. -/ @[reducible] def padic_seq (p : ℕ) [p.prime] := cau_seq _ (padic_norm p) namespace padic_seq section variables {p : ℕ} [nat.prime p] /-- The p-adic norm of the entries of a nonzero Cauchy sequence of rationals is eventually constant. -/ lemma stationary {f : cau_seq ℚ (padic_norm p)} (hf : ¬ f ≈ 0) : ∃ N, ∀ m n, m ≥ N → n ≥ N → padic_norm p (f n) = padic_norm p (f m) := have ∃ ε > 0, ∃ N1, ∀ j ≥ N1, ε ≤ padic_norm p (f j), from cau_seq.abv_pos_of_not_lim_zero $ not_lim_zero_of_not_congr_zero hf, let ⟨ε, hε, N1, hN1⟩ := this, ⟨N2, hN2⟩ := cau_seq.cauchy₂ f hε in ⟨ max N1 N2, λ n m hn hm, have padic_norm p (f n - f m) < ε, from hN2 _ _ (max_le_iff.1 hn).2 (max_le_iff.1 hm).2, have padic_norm p (f n - f m) < padic_norm p (f n), from lt_of_lt_of_le this $ hN1 _ (max_le_iff.1 hn).1, have padic_norm p (f n - f m) < max (padic_norm p (f n)) (padic_norm p (f m)), from lt_max_iff.2 (or.inl this), begin by_contradiction hne, rw ←padic_norm.neg p (f m) at hne, have hnam := add_eq_max_of_ne p hne, rw [padic_norm.neg, max_comm] at hnam, rw ←hnam at this, apply _root_.lt_irrefl _ (by simp at this; exact this) end ⟩ /-- For all n ≥ stationary_point f hf, the p-adic norm of f n is the same. -/ def stationary_point {f : padic_seq p} (hf : ¬ f ≈ 0) : ℕ := classical.some $ stationary hf lemma stationary_point_spec {f : padic_seq p} (hf : ¬ f ≈ 0) : ∀ {m n}, m ≥ stationary_point hf → n ≥ stationary_point hf → padic_norm p (f n) = padic_norm p (f m) := classical.some_spec $ stationary hf /-- Since the norm of the entries of a Cauchy sequence is eventually stationary, we can lift the norm to sequences. -/ def norm (f : padic_seq p) : ℚ := if hf : f ≈ 0 then 0 else padic_norm p (f (stationary_point hf)) lemma norm_zero_iff (f : padic_seq p) : f.norm = 0 ↔ f ≈ 0 := begin constructor, { intro h, by_contradiction hf, unfold norm at h, split_ifs at h, apply hf, intros ε hε, existsi stationary_point hf, intros j hj, have heq := stationary_point_spec hf (le_refl _) hj, simpa [h, heq] }, { intro h, simp [norm, h] } end end section embedding open cau_seq variables {p : ℕ} [nat.prime p] lemma equiv_zero_of_val_eq_of_equiv_zero {f g : padic_seq p} (h : ∀ k, padic_norm p (f k) = padic_norm p (g k)) (hf : f ≈ 0) : g ≈ 0 := λ ε hε, let ⟨i, hi⟩ := hf _ hε in ⟨i, λ j hj, by simpa [h] using hi _ hj⟩ lemma norm_nonzero_of_not_equiv_zero {f : padic_seq p} (hf : ¬ f ≈ 0) : f.norm ≠ 0 := hf ∘ f.norm_zero_iff.1 lemma norm_eq_norm_app_of_nonzero {f : padic_seq p} (hf : ¬ f ≈ 0) : ∃ k, f.norm = padic_norm p k ∧ k ≠ 0 := have heq : f.norm = padic_norm p (f $ stationary_point hf), by simp [norm, hf], ⟨f $ stationary_point hf, heq, λ h, norm_nonzero_of_not_equiv_zero hf (by simpa [h] using heq)⟩ lemma not_lim_zero_const_of_nonzero {q : ℚ} (hq : q ≠ 0) : ¬ lim_zero (const (padic_norm p) q) := λ h', hq $ const_lim_zero.1 h' lemma not_equiv_zero_const_of_nonzero {q : ℚ} (hq : q ≠ 0) : ¬ (const (padic_norm p) q) ≈ 0 := λ h : lim_zero (const (padic_norm p) q - 0), not_lim_zero_const_of_nonzero hq $ by simpa using h lemma norm_nonneg (f : padic_seq p) : f.norm ≥ 0 := if hf : f ≈ 0 then by simp [hf, norm] else by simp [norm, hf, padic_norm.nonneg] /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_left_left {f : padic_seq p} (hf : ¬ f ≈ 0) (v2 v3 : ℕ) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max (stationary_point hf) (max v2 v3))) := let i := max (stationary_point hf) (max v2 v3) in begin apply stationary_point_spec hf, { apply le_max_left }, { apply le_refl } end /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_left {f : padic_seq p} (hf : ¬ f ≈ 0) (v1 v3 : ℕ) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max v1 (max (stationary_point hf) v3))) := let i := max v1 (max (stationary_point hf) v3) in begin apply stationary_point_spec hf, { apply le_trans, { apply le_max_left _ v3 }, { apply le_max_right } }, { apply le_refl } end /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_right {f : padic_seq p} (hf : ¬ f ≈ 0) (v1 v2 : ℕ) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max v1 (max v2 (stationary_point hf)))) := let i := max v1 (max v2 (stationary_point hf)) in begin apply stationary_point_spec hf, { apply le_trans, { apply le_max_right v2 }, { apply le_max_right } }, { apply le_refl } end end embedding end padic_seq section open padic_seq private meta def index_simp_core (hh hf hg : expr) (at_ : interactive.loc := interactive.loc.ns [none]) : tactic unit := do [v1, v2, v3] ← [hh, hf, hg].mmap (λ n, tactic.mk_app ``stationary_point [n] <|> return n), e1 ← tactic.mk_app ``lift_index_left_left [hh, v2, v3] <|> return `(true), e2 ← tactic.mk_app ``lift_index_left [hf, v1, v3] <|> return `(true), e3 ← tactic.mk_app ``lift_index_right [hg, v1, v2] <|> return `(true), sl ← [e1, e2, e3].mfoldl (λ s e, simp_lemmas.add s e) simp_lemmas.mk, when at_.include_goal (tactic.simp_target sl), hs ← at_.get_locals, hs.mmap' (tactic.simp_hyp sl []) /-- This is a special-purpose tactic that lifts padic_norm (f (stationary_point f)) to padic_norm (f (max _ _ _)). -/ meta def tactic.interactive.padic_index_simp (l : interactive.parse interactive.types.pexpr_list) (at_ : interactive.parse interactive.types.location) : tactic unit := do [h, f, g] ← l.mmap tactic.i_to_expr, index_simp_core h f g at_ end namespace padic_seq section embedding open cau_seq variables {p : ℕ} [hp : nat.prime p] include hp lemma norm_mul (f g : padic_seq p) : (f * g).norm = f.norm * g.norm := if hf : f ≈ 0 then have hg : f * g ≈ 0, from mul_equiv_zero' _ hf, by simp [hf, hg, norm] else if hg : g ≈ 0 then have hf : f * g ≈ 0, from mul_equiv_zero _ hg, by simp [hf, hg, norm] else have hfg : ¬ f * g ≈ 0, by apply mul_not_equiv_zero; assumption, begin unfold norm, split_ifs, padic_index_simp [hfg, hf, hg], apply padic_norm.mul end lemma eq_zero_iff_equiv_zero (f : padic_seq p) : mk f = 0 ↔ f ≈ 0 := mk_eq lemma ne_zero_iff_nequiv_zero (f : padic_seq p) : mk f ≠ 0 ↔ ¬ f ≈ 0 := not_iff_not.2 (eq_zero_iff_equiv_zero _) lemma norm_const (q : ℚ) : norm (const (padic_norm p) q) = padic_norm p q := if hq : q = 0 then have (const (padic_norm p) q) ≈ 0, by simp [hq]; apply setoid.refl (const (padic_norm p) 0), by subst hq; simp [norm, this] else have ¬ (const (padic_norm p) q) ≈ 0, from not_equiv_zero_const_of_nonzero hq, by simp [norm, this] lemma norm_image (a : padic_seq p) (ha : ¬ a ≈ 0) : (∃ (n : ℤ), a.norm = ↑p ^ (-n)) := let ⟨k, hk, hk'⟩ := norm_eq_norm_app_of_nonzero ha in by simpa [hk] using padic_norm.image p hk' lemma norm_one : norm (1 : padic_seq p) = 1 := have h1 : ¬ (1 : padic_seq p) ≈ 0, from one_not_equiv_zero _, by simp [h1, norm, hp.gt_one] private lemma norm_eq_of_equiv_aux {f g : padic_seq p} (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) (hfg : f ≈ g) (h : padic_norm p (f (stationary_point hf)) ≠ padic_norm p (g (stationary_point hg))) (hgt : padic_norm p (f (stationary_point hf)) > padic_norm p (g (stationary_point hg))) : false := begin have hpn : padic_norm p (f (stationary_point hf)) - padic_norm p (g (stationary_point hg)) > 0, from sub_pos_of_lt hgt, cases hfg _ hpn with N hN, let i := max N (max (stationary_point hf) (stationary_point hg)), have hi : i ≥ N, from le_max_left _ _, have hN' := hN _ hi, padic_index_simp [N, hf, hg] at hN' h hgt, have hpne : padic_norm p (f i) ≠ padic_norm p (-(g i)), by rwa [ ←padic_norm.neg p (g i)] at h, let hpnem := add_eq_max_of_ne p hpne, have hpeq : padic_norm p ((f - g) i) = max (padic_norm p (f i)) (padic_norm p (g i)), { rwa padic_norm.neg at hpnem }, rw [hpeq, max_eq_left_of_lt hgt] at hN', have : padic_norm p (f i) < padic_norm p (f i), { apply lt_of_lt_of_le hN', apply sub_le_self, apply padic_norm.nonneg }, exact lt_irrefl _ this end private lemma norm_eq_of_equiv {f g : padic_seq p} (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) (hfg : f ≈ g) : padic_norm p (f (stationary_point hf)) = padic_norm p (g (stationary_point hg)) := begin by_contradiction h, cases (decidable.em (padic_norm p (f (stationary_point hf)) > padic_norm p (g (stationary_point hg)))) with hgt hngt, { exact norm_eq_of_equiv_aux hf hg hfg h hgt }, { apply norm_eq_of_equiv_aux hg hf (setoid.symm hfg) (ne.symm h), apply lt_of_le_of_ne, apply le_of_not_gt hngt, apply h } end theorem norm_equiv {f g : padic_seq p} (hfg : f ≈ g) : f.norm = g.norm := if hf : f ≈ 0 then have hg : g ≈ 0, from setoid.trans (setoid.symm hfg) hf, by simp [norm, hf, hg] else have hg : ¬ g ≈ 0, from hf ∘ setoid.trans hfg, by unfold norm; split_ifs; exact norm_eq_of_equiv hf hg hfg private lemma norm_nonarchimedean_aux {f g : padic_seq p} (hfg : ¬ f + g ≈ 0) (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) : (f + g).norm ≤ max (f.norm) (g.norm) := begin unfold norm, split_ifs, padic_index_simp [hfg, hf, hg], apply padic_norm.nonarchimedean end theorem norm_nonarchimedean (f g : padic_seq p) : (f + g).norm ≤ max (f.norm) (g.norm) := if hfg : f + g ≈ 0 then have 0 ≤ max (f.norm) (g.norm), from le_max_left_of_le (norm_nonneg _), by simpa [hfg, norm] else if hf : f ≈ 0 then have hfg' : f + g ≈ g, { change lim_zero (f - 0) at hf, show lim_zero (f + g - g), by simpa using hf }, have hcfg : (f + g).norm = g.norm, from norm_equiv hfg', have hcl : f.norm = 0, from (norm_zero_iff f).2 hf, have max (f.norm) (g.norm) = g.norm, by rw hcl; exact max_eq_right (norm_nonneg _), by rw [this, hcfg] else if hg : g ≈ 0 then have hfg' : f + g ≈ f, { change lim_zero (g - 0) at hg, show lim_zero (f + g - f), by simpa [add_sub_cancel'] using hg }, have hcfg : (f + g).norm = f.norm, from norm_equiv hfg', have hcl : g.norm = 0, from (norm_zero_iff g).2 hg, have max (f.norm) (g.norm) = f.norm, by rw hcl; exact max_eq_left (norm_nonneg _), by rw [this, hcfg] else norm_nonarchimedean_aux hfg hf hg lemma norm_eq {f g : padic_seq p} (h : ∀ k, padic_norm p (f k) = padic_norm p (g k)) : f.norm = g.norm := if hf : f ≈ 0 then have hg : g ≈ 0, from equiv_zero_of_val_eq_of_equiv_zero h hf, by simp [hf, hg, norm] else have hg : ¬ g ≈ 0, from λ hg, hf $ equiv_zero_of_val_eq_of_equiv_zero (by simp [h]) hg, begin simp [hg, hf, norm], let i := max (stationary_point hf) (stationary_point hg), have hpf : padic_norm p (f (stationary_point hf)) = padic_norm p (f i), { apply stationary_point_spec, apply le_max_left, apply le_refl }, have hpg : padic_norm p (g (stationary_point hg)) = padic_norm p (g i), { apply stationary_point_spec, apply le_max_right, apply le_refl }, rw [hpf, hpg, h] end lemma norm_neg (a : padic_seq p) : (-a).norm = a.norm := norm_eq $ by simp lemma norm_eq_of_add_equiv_zero {f g : padic_seq p} (h : f + g ≈ 0) : f.norm = g.norm := have lim_zero (f + g - 0), from h, have f ≈ -g, from show lim_zero (f - (-g)), by simpa, have f.norm = (-g).norm, from norm_equiv this, by simpa [norm_neg] using this lemma add_eq_max_of_ne {f g : padic_seq p} (hfgne : f.norm ≠ g.norm) : (f + g).norm = max f.norm g.norm := have hfg : ¬f + g ≈ 0, from mt norm_eq_of_add_equiv_zero hfgne, if hf : f ≈ 0 then have lim_zero (f - 0), from hf, have f + g ≈ g, from show lim_zero ((f + g) - g), by simpa, have h1 : (f+g).norm = g.norm, from norm_equiv this, have h2 : f.norm = 0, from (norm_zero_iff _).2 hf, by rw [h1, h2]; rw max_eq_right (norm_nonneg _) else if hg : g ≈ 0 then have lim_zero (g - 0), from hg, have f + g ≈ f, from show lim_zero ((f + g) - f), by rw [add_sub_cancel']; simpa, have h1 : (f+g).norm = f.norm, from norm_equiv this, have h2 : g.norm = 0, from (norm_zero_iff _).2 hg, by rw [h1, h2]; rw max_eq_left (norm_nonneg _) else begin unfold norm at ⊢ hfgne, split_ifs at ⊢ hfgne, padic_index_simp [hfg, hf, hg] at ⊢ hfgne, apply padic_norm.add_eq_max_of_ne, simpa [hf, hg, norm] using hfgne end end embedding end padic_seq /-- The p-adic numbers `Q_[p]` are the Cauchy completion of `ℚ` with respect to the p-adic norm. -/ def padic (p : ℕ) [nat.prime p] := @cau_seq.completion.Cauchy _ _ _ _ (padic_norm p) _ notation `ℚ_[` p `]` := padic p namespace padic section completion variables {p : ℕ} [nat.prime p] /-- The discrete field structure on ℚ_p is inherited from the Cauchy completion construction. -/ instance discrete_field : discrete_field (ℚ_[p]) := cau_seq.completion.discrete_field -- short circuits instance : has_zero ℚ_[p] := by apply_instance instance : has_one ℚ_[p] := by apply_instance instance : has_add ℚ_[p] := by apply_instance instance : has_mul ℚ_[p] := by apply_instance instance : has_sub ℚ_[p] := by apply_instance instance : has_neg ℚ_[p] := by apply_instance instance : has_div ℚ_[p] := by apply_instance instance : add_comm_group ℚ_[p] := by apply_instance instance : comm_ring ℚ_[p] := by apply_instance /-- Builds the equivalence class of a Cauchy sequence of rationals. -/ def mk : padic_seq p → ℚ_[p] := quotient.mk end completion section completion variables (p : ℕ) [nat.prime p] lemma mk_eq {f g : padic_seq p} : mk f = mk g ↔ f ≈ g := quotient.eq /-- Embeds the rational numbers in the p-adic numbers. -/ def of_rat : ℚ → ℚ_[p] := cau_seq.completion.of_rat @[simp] lemma of_rat_add : ∀ (x y : ℚ), of_rat p (x + y) = of_rat p x + of_rat p y := cau_seq.completion.of_rat_add @[simp] lemma of_rat_neg : ∀ (x : ℚ), of_rat p (-x) = -of_rat p x := cau_seq.completion.of_rat_neg @[simp] lemma of_rat_mul : ∀ (x y : ℚ), of_rat p (x * y) = of_rat p x * of_rat p y := cau_seq.completion.of_rat_mul @[simp] lemma of_rat_sub : ∀ (x y : ℚ), of_rat p (x - y) = of_rat p x - of_rat p y := cau_seq.completion.of_rat_sub @[simp] lemma of_rat_div : ∀ (x y : ℚ), of_rat p (x / y) = of_rat p x / of_rat p y := cau_seq.completion.of_rat_div @[simp] lemma of_rat_one : of_rat p 1 = 1 := rfl @[simp] lemma of_rat_zero : of_rat p 0 = 0 := rfl @[simp] lemma cast_eq_of_rat_of_nat (n : ℕ) : (↑n : ℚ_[p]) = of_rat p n := begin induction n with n ih, { refl }, { simpa using ih } end -- without short circuits, this needs an increase of class.instance_max_depth @[simp] lemma cast_eq_of_rat_of_int (n : ℤ) : ↑n = of_rat p n := by induction n; simp lemma cast_eq_of_rat : ∀ (q : ℚ), (↑q : ℚ_[p]) = of_rat p q | ⟨n, d, h1, h2⟩ := show ↑n / ↑d = _, from have (⟨n, d, h1, h2⟩ : ℚ) = rat.mk n d, from rat.num_denom', by simp [this, rat.mk_eq_div, of_rat_div] @[move_cast] lemma coe_add : ∀ {x y : ℚ}, (↑(x + y) : ℚ_[p]) = ↑x + ↑y := by simp [cast_eq_of_rat] @[move_cast] lemma coe_neg : ∀ {x : ℚ}, (↑(-x) : ℚ_[p]) = -↑x := by simp [cast_eq_of_rat] @[move_cast] lemma coe_mul : ∀ {x y : ℚ}, (↑(x * y) : ℚ_[p]) = ↑x * ↑y := by simp [cast_eq_of_rat] @[move_cast] lemma coe_sub : ∀ {x y : ℚ}, (↑(x - y) : ℚ_[p]) = ↑x - ↑y := by simp [cast_eq_of_rat] @[move_cast] lemma coe_div : ∀ {x y : ℚ}, (↑(x / y) : ℚ_[p]) = ↑x / ↑y := by simp [cast_eq_of_rat] @[squash_cast] lemma coe_one : (↑1 : ℚ_[p]) = 1 := rfl @[squash_cast] lemma coe_zero : (↑1 : ℚ_[p]) = 1 := rfl lemma const_equiv {q r : ℚ} : const (padic_norm p) q ≈ const (padic_norm p) r ↔ q = r := ⟨ λ heq : lim_zero (const (padic_norm p) (q - r)), eq_of_sub_eq_zero $ const_lim_zero.1 heq, λ heq, by rw heq; apply setoid.refl _ ⟩ lemma of_rat_eq {q r : ℚ} : of_rat p q = of_rat p r ↔ q = r := ⟨(const_equiv p).1 ∘ quotient.eq.1, λ h, by rw h⟩ @[elim_cast] lemma coe_inj {q r : ℚ} : (↑q : ℚ_[p]) = ↑r ↔ q = r := by simp [cast_eq_of_rat, of_rat_eq] instance : char_zero ℚ_[p] := ⟨λ m n, by { rw ← rat.cast_coe_nat, norm_cast }⟩ end completion end padic /-- The rational-valued p-adic norm on ℚ_p is lifted from the norm on Cauchy sequences. The canonical form of this function is the normed space instance, with notation `∥ ∥`. -/ def padic_norm_e {p : ℕ} [hp : nat.prime p] : ℚ_[p] → ℚ := quotient.lift padic_seq.norm $ @padic_seq.norm_equiv _ _ namespace padic_norm_e section embedding open padic_seq variables {p : ℕ} [nat.prime p] lemma defn (f : padic_seq p) {ε : ℚ} (hε : ε > 0) : ∃ N, ∀ i ≥ N, padic_norm_e (⟦f⟧ - f i) < ε := begin simp only [padic.cast_eq_of_rat], change ∃ N, ∀ i ≥ N, (f - const _ (f i)).norm < ε, by_contradiction h, cases cauchy₂ f hε with N hN, have : ∀ N, ∃ i ≥ N, (f - const _ (f i)).norm ≥ ε, by simpa [not_forall] using h, rcases this N with ⟨i, hi, hge⟩, have hne : ¬ (f - const (padic_norm p) (f i)) ≈ 0, { intro h, unfold padic_seq.norm at hge; split_ifs at hge, exact not_lt_of_ge hge hε }, unfold padic_seq.norm at hge; split_ifs at hge, apply not_le_of_gt _ hge, cases decidable.em ((stationary_point hne) ≥ N) with hgen hngen, { apply hN; assumption }, { have := stationary_point_spec hne (le_refl _) (le_of_not_le hngen), rw ←this, apply hN, apply le_refl, assumption } end protected lemma nonneg (q : ℚ_[p]) : padic_norm_e q ≥ 0 := quotient.induction_on q $ norm_nonneg lemma zero_def : (0 : ℚ_[p]) = ⟦0⟧ := rfl lemma zero_iff (q : ℚ_[p]) : padic_norm_e q = 0 ↔ q = 0 := quotient.induction_on q $ by simpa only [zero_def, quotient.eq] using norm_zero_iff @[simp] protected lemma zero : padic_norm_e (0 : ℚ_[p]) = 0 := (zero_iff _).2 rfl /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`∥ ∥`). -/ @[simp] protected lemma one' : padic_norm_e (1 : ℚ_[p]) = 1 := norm_one @[simp] protected lemma neg (q : ℚ_[p]) : padic_norm_e (-q) = padic_norm_e q := quotient.induction_on q $ norm_neg /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`∥ ∥`). -/ theorem nonarchimedean' (q r : ℚ_[p]) : padic_norm_e (q + r) ≤ max (padic_norm_e q) (padic_norm_e r) := quotient.induction_on₂ q r $ norm_nonarchimedean /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`∥ ∥`). -/ theorem add_eq_max_of_ne' {q r : ℚ_[p]} : padic_norm_e q ≠ padic_norm_e r → padic_norm_e (q + r) = max (padic_norm_e q) (padic_norm_e r) := quotient.induction_on₂ q r $ λ _ _, padic_seq.add_eq_max_of_ne lemma triangle_ineq (x y z : ℚ_[p]) : padic_norm_e (x - z) ≤ padic_norm_e (x - y) + padic_norm_e (y - z) := calc padic_norm_e (x - z) = padic_norm_e ((x - y) + (y - z)) : by rw sub_add_sub_cancel ... ≤ max (padic_norm_e (x - y)) (padic_norm_e (y - z)) : padic_norm_e.nonarchimedean' _ _ ... ≤ padic_norm_e (x - y) + padic_norm_e (y - z) : max_le_add_of_nonneg (padic_norm_e.nonneg _) (padic_norm_e.nonneg _) protected lemma add (q r : ℚ_[p]) : padic_norm_e (q + r) ≤ (padic_norm_e q) + (padic_norm_e r) := calc padic_norm_e (q + r) ≤ max (padic_norm_e q) (padic_norm_e r) : nonarchimedean' _ _ ... ≤ (padic_norm_e q) + (padic_norm_e r) : max_le_add_of_nonneg (padic_norm_e.nonneg _) (padic_norm_e.nonneg _) protected lemma mul' (q r : ℚ_[p]) : padic_norm_e (q * r) = (padic_norm_e q) * (padic_norm_e r) := quotient.induction_on₂ q r $ norm_mul instance : is_absolute_value (@padic_norm_e p _) := { abv_nonneg := padic_norm_e.nonneg, abv_eq_zero := zero_iff, abv_add := padic_norm_e.add, abv_mul := padic_norm_e.mul' } @[simp] lemma eq_padic_norm' (q : ℚ) : padic_norm_e (padic.of_rat p q) = padic_norm p q := norm_const _ protected theorem image' {q : ℚ_[p]} : q ≠ 0 → ∃ n : ℤ, padic_norm_e q = p ^ (-n) := quotient.induction_on q $ λ f hf, have ¬ f ≈ 0, from (ne_zero_iff_nequiv_zero f).1 hf, norm_image f this lemma sub_rev (q r : ℚ_[p]) : padic_norm_e (q - r) = padic_norm_e (r - q) := by rw ←(padic_norm_e.neg); simp end embedding end padic_norm_e namespace padic section complete open padic_seq padic theorem rat_dense' {p : ℕ} [nat.prime p] (q : ℚ_[p]) {ε : ℚ} (hε : ε > 0) : ∃ r : ℚ, padic_norm_e (q - r) < ε := quotient.induction_on q $ λ q', have ∃ N, ∀ m n ≥ N, padic_norm p (q' m - q' n) < ε, from cauchy₂ _ hε, let ⟨N, hN⟩ := this in ⟨q' N, begin simp only [padic.cast_eq_of_rat], change padic_seq.norm (q' - const _ (q' N)) < ε, cases decidable.em ((q' - const (padic_norm p) (q' N)) ≈ 0) with heq hne', { simpa only [heq, padic_seq.norm, dif_pos] }, { simp only [padic_seq.norm, dif_neg hne'], change padic_norm p (q' _ - q' _) < ε, have := stationary_point_spec hne', cases decidable.em (N ≥ stationary_point hne') with hle hle, { have := eq.symm (this (le_refl _) hle), simp at this, simpa [this] }, { apply hN, apply le_of_lt, apply lt_of_not_ge, apply hle, apply le_refl }} end⟩ variables {p : ℕ} [nat.prime p] (f : cau_seq _ (@padic_norm_e p _)) open classical private lemma div_nat_pos (n : ℕ) : (1 / ((n + 1): ℚ)) > 0 := div_pos zero_lt_one (by exact_mod_cast succ_pos _) def lim_seq : ℕ → ℚ := λ n, classical.some (rat_dense' (f n) (div_nat_pos n)) lemma exi_rat_seq_conv {ε : ℚ} (hε : 0 < ε) : ∃ N, ∀ i ≥ N, padic_norm_e (f i - ((lim_seq f) i : ℚ_[p])) < ε := begin refine (exists_nat_gt (1/ε)).imp (λ N hN i hi, _), have h := classical.some_spec (rat_dense' (f i) (div_nat_pos i)), refine lt_of_lt_of_le h (div_le_of_le_mul (by exact_mod_cast succ_pos _) _), rw right_distrib, apply le_add_of_le_of_nonneg, { exact le_mul_of_div_le hε (le_trans (le_of_lt hN) (by exact_mod_cast hi)) }, { apply le_of_lt, simpa } end lemma exi_rat_seq_conv_cauchy : is_cau_seq (padic_norm p) (lim_seq f) := assume ε hε, have hε3 : ε / 3 > 0, from div_pos hε (by norm_num), let ⟨N, hN⟩ := exi_rat_seq_conv f hε3, ⟨N2, hN2⟩ := f.cauchy₂ hε3 in begin existsi max N N2, intros j hj, suffices : padic_norm_e ((↑(lim_seq f j) - f (max N N2)) + (f (max N N2) - lim_seq f (max N N2))) < ε, { ring at this ⊢, rw [← padic_norm_e.eq_padic_norm', ← padic.cast_eq_of_rat], exact_mod_cast this }, { apply lt_of_le_of_lt, { apply padic_norm_e.add }, { have : (3 : ℚ) ≠ 0, by norm_num, have : ε = ε / 3 + ε / 3 + ε / 3, { apply eq_of_mul_eq_mul_left this, simp [left_distrib, mul_div_cancel' _ this ], ring }, rw this, apply add_lt_add, { suffices : padic_norm_e ((↑(lim_seq f j) - f j) + (f j - f (max N N2))) < ε / 3 + ε / 3, by simpa, apply lt_of_le_of_lt, { apply padic_norm_e.add }, { apply add_lt_add, { rw [padic_norm_e.sub_rev], apply_mod_cast hN, exact le_of_max_le_left hj }, { apply hN2, exact le_of_max_le_right hj, apply le_max_right }}}, { apply_mod_cast hN, apply le_max_left }}} end private def lim' : padic_seq p := ⟨_, exi_rat_seq_conv_cauchy f⟩ private def lim : ℚ_[p] := ⟦lim' f⟧ theorem complete' : ∃ q : ℚ_[p], ∀ ε > 0, ∃ N, ∀ i ≥ N, padic_norm_e (q - f i) < ε := ⟨ lim f, λ ε hε, let ⟨N, hN⟩ := exi_rat_seq_conv f (show ε / 2 > 0, from div_pos hε (by norm_num)), ⟨N2, hN2⟩ := padic_norm_e.defn (lim' f) (show ε / 2 > 0, from div_pos hε (by norm_num)) in begin existsi max N N2, intros i hi, suffices : padic_norm_e ((lim f - lim' f i) + (lim' f i - f i)) < ε, { ring at this; exact this }, { apply lt_of_le_of_lt, { apply padic_norm_e.add }, { have : ε = ε / 2 + ε / 2, by rw ←(add_self_div_two ε); simp, rw this, apply add_lt_add, { apply hN2, exact le_of_max_le_right hi }, { rw_mod_cast [padic_norm_e.sub_rev], apply hN, exact le_of_max_le_left hi }}} end ⟩ end complete section normed_space variables (p : ℕ) [nat.prime p] instance : has_dist ℚ_[p] := ⟨λ x y, padic_norm_e (x - y)⟩ instance : metric_space ℚ_[p] := { dist_self := by simp [dist], dist_comm := λ x y, by unfold dist; rw ←padic_norm_e.neg (x - y); simp, dist_triangle := begin intros, unfold dist, exact_mod_cast padic_norm_e.triangle_ineq _ _ _, end, eq_of_dist_eq_zero := begin unfold dist, intros _ _ h, apply eq_of_sub_eq_zero, apply (padic_norm_e.zero_iff _).1, exact_mod_cast h end } instance : has_norm ℚ_[p] := ⟨λ x, padic_norm_e x⟩ instance : normed_field ℚ_[p] := { dist_eq := λ _ _, rfl, norm_mul := by simp [has_norm.norm, padic_norm_e.mul'] } instance : is_absolute_value (λ a : ℚ_[p], ∥a∥) := { abv_nonneg := norm_nonneg, abv_eq_zero := norm_eq_zero, abv_add := norm_triangle, abv_mul := by simp [has_norm.norm, padic_norm_e.mul'] } theorem rat_dense {p : ℕ} {hp : p.prime} (q : ℚ_[p]) {ε : ℝ} (hε : ε > 0) : ∃ r : ℚ, ∥q - r∥ < ε := let ⟨ε', hε'l, hε'r⟩ := exists_rat_btwn hε, ⟨r, hr⟩ := rat_dense' q (by simpa using hε'l) in ⟨r, lt.trans (by simpa [has_norm.norm] using hr) hε'r⟩ end normed_space end padic namespace padic_norm_e section normed_space variables {p : ℕ} [hp : p.prime] include hp @[simp] protected lemma mul (q r : ℚ_[p]) : ∥q * r∥ = ∥q∥ * ∥r∥ := by simp [has_norm.norm, padic_norm_e.mul'] protected lemma is_norm (q : ℚ_[p]) : ↑(padic_norm_e q) = ∥q∥ := rfl theorem nonarchimedean (q r : ℚ_[p]) : ∥q + r∥ ≤ max (∥q∥) (∥r∥) := begin unfold has_norm.norm, exact_mod_cast nonarchimedean' _ _ end theorem add_eq_max_of_ne {q r : ℚ_[p]} (h : ∥q∥ ≠ ∥r∥) : ∥q+r∥ = max (∥q∥) (∥r∥) := begin unfold has_norm.norm, apply_mod_cast add_eq_max_of_ne', intro h', apply h, unfold has_norm.norm, exact_mod_cast h' end @[simp] lemma eq_padic_norm (q : ℚ) : ∥(↑q : ℚ_[p])∥ = padic_norm p q := begin unfold has_norm.norm, rw [← padic_norm_e.eq_padic_norm', ← padic.cast_eq_of_rat] end instance : nondiscrete_normed_field ℚ_[p] := { non_trivial := ⟨padic.of_rat p (p⁻¹), begin have h0 : p ≠ 0 := ne_of_gt (hp.pos), have h1 : 1 < p := prime.gt_one hp, rw [← padic.cast_eq_of_rat, eq_padic_norm], simp only [padic_norm, inv_eq_zero], simp only [if_neg] {discharger := `[exact_mod_cast h0]}, norm_cast, simp only [padic_val_rat.inv] {discharger := `[exact_mod_cast h0]}, rw [neg_neg, padic_val_rat.padic_val_rat_self h1], erw _root_.pow_one, exact_mod_cast h1, end⟩ } protected theorem image {q : ℚ_[p]} : q ≠ 0 → ∃ n : ℤ, ∥q∥ = ↑((↑p : ℚ) ^ (-n)) := quotient.induction_on q $ λ f hf, have ¬ f ≈ 0, from (padic_seq.ne_zero_iff_nequiv_zero f).1 hf, let ⟨n, hn⟩ := padic_seq.norm_image f this in ⟨n, congr_arg rat.cast hn⟩ protected lemma is_rat (q : ℚ_[p]) : ∃ q' : ℚ, ∥q∥ = ↑q' := if h : q = 0 then ⟨0, by simp [h]⟩ else let ⟨n, hn⟩ := padic_norm_e.image h in ⟨_, hn⟩ def rat_norm (q : ℚ_[p]) : ℚ := classical.some (padic_norm_e.is_rat q) lemma eq_rat_norm (q : ℚ_[p]) : ∥q∥ = rat_norm q := classical.some_spec (padic_norm_e.is_rat q) theorem norm_rat_le_one : ∀ {q : ℚ} (hq : ¬ p ∣ q.denom), ∥(q : ℚ_[p])∥ ≤ 1 | ⟨n, d, hn, hd⟩ := λ hq : ¬ p ∣ d, if hnz : n = 0 then have (⟨n, d, hn, hd⟩ : ℚ) = 0, from rat.zero_iff_num_zero.mpr hnz, by norm_num [this] else begin have hnz' : {rat . num := n, denom := d, pos := hn, cop := hd} ≠ 0, from mt rat.zero_iff_num_zero.1 hnz, rw [padic_norm_e.eq_padic_norm], norm_cast, rw [padic_norm.eq_fpow_of_nonzero p hnz', padic_val_rat_def p hnz'], have h : (multiplicity p d).get _ = 0, by simp [multiplicity_eq_zero_of_not_dvd, hq], rw_mod_cast [h, sub_zero], apply fpow_le_one_of_nonpos, { exact_mod_cast le_of_lt hp.gt_one, }, { apply neg_nonpos_of_nonneg, norm_cast, simp, } end lemma eq_of_norm_add_lt_right {p : ℕ} {hp : p.prime} {z1 z2 : ℚ_[p]} (h : ∥z1 + z2∥ < ∥z2∥) : ∥z1∥ = ∥z2∥ := by_contradiction $ λ hne, not_lt_of_ge (by rw padic_norm_e.add_eq_max_of_ne hne; apply le_max_right) h lemma eq_of_norm_add_lt_left {p : ℕ} {hp : p.prime} {z1 z2 : ℚ_[p]} (h : ∥z1 + z2∥ < ∥z1∥) : ∥z1∥ = ∥z2∥ := by_contradiction $ λ hne, not_lt_of_ge (by rw padic_norm_e.add_eq_max_of_ne hne; apply le_max_left) h end normed_space end padic_norm_e namespace padic variables {p : ℕ} [nat.prime p] set_option eqn_compiler.zeta true instance complete : cau_seq.is_complete ℚ_[p] norm := begin split, intro f, have cau_seq_norm_e : is_cau_seq padic_norm_e f, { intros ε hε, let h := is_cau f ε (by exact_mod_cast hε), unfold norm at h, apply_mod_cast h }, cases padic.complete' ⟨f, cau_seq_norm_e⟩ with q hq, existsi q, intros ε hε, cases exists_rat_btwn hε with ε' hε', norm_cast at hε', cases hq ε' hε'.1 with N hN, existsi N, intros i hi, let h := hN i hi, unfold norm, rw_mod_cast [cau_seq.sub_apply, padic_norm_e.sub_rev], refine lt.trans _ hε'.2, exact_mod_cast hN i hi end lemma padic_norm_e_lim_le {f : cau_seq ℚ_[p] norm} {a : ℝ} (ha : a > 0) (hf : ∀ i, ∥f i∥ ≤ a) : ∥f.lim∥ ≤ a := let ⟨N, hN⟩ := setoid.symm (cau_seq.equiv_lim f) _ ha in calc ∥f.lim∥ = ∥f.lim - f N + f N∥ : by simp ... ≤ max (∥f.lim - f N∥) (∥f N∥) : padic_norm_e.nonarchimedean _ _ ... ≤ a : max_le (le_of_lt (hN _ (le_refl _))) (hf _) end padic
4d022a01423d0a6238da74d9a02111b0d243dd3e
8b9f17008684d796c8022dab552e42f0cb6fb347
/library/logic/identities.lean
799fcb96cbe12ba6bce050b52ea71d9ea11d0370
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,170
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: logic.identities Authors: Jeremy Avigad, Leonardo de Moura Useful logical identities. Since we are not using propositional extensionality, some of the calculations use the type class support provided by logic.instances. -/ import logic.connectives logic.instances logic.quantifiers logic.cast open relation decidable relation.iff_ops theorem or.right_comm (a b c : Prop) : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ b := calc (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) : or.assoc ... ↔ a ∨ (c ∨ b) : {or.comm} ... ↔ (a ∨ c) ∨ b : iff.symm or.assoc theorem or.left_comm (a b c : Prop) : a ∨ (b ∨ c) ↔ b ∨ (a ∨ c) := calc a ∨ (b ∨ c) ↔ (a ∨ b) ∨ c : iff.symm or.assoc ... ↔ (b ∨ a) ∨ c : {or.comm} ... ↔ b ∨ (a ∨ c) : or.assoc theorem and.right_comm (a b c : Prop) : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := calc (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) : and.assoc ... ↔ a ∧ (c ∧ b) : {and.comm} ... ↔ (a ∧ c) ∧ b : iff.symm and.assoc theorem and.left_comm (a b c : Prop) : a ∧ (b ∧ c) ↔ b ∧ (a ∧ c) := calc a ∧ (b ∧ c) ↔ (a ∧ b) ∧ c : iff.symm and.assoc ... ↔ (b ∧ a) ∧ c : {and.comm} ... ↔ b ∧ (a ∧ c) : and.assoc theorem not_not_iff {a : Prop} [D : decidable a] : (¬¬a) ↔ a := iff.intro (assume H : ¬¬a, by_cases (assume H' : a, H') (assume H' : ¬a, absurd H' H)) (assume H : a, assume H', H' H) theorem not_not_elim {a : Prop} [D : decidable a] (H : ¬¬a) : a := iff.mp not_not_iff H theorem not_true_iff_false : ¬true ↔ false := iff.intro (assume H, H trivial) false.elim theorem not_false_iff_true : ¬false ↔ true := iff.intro (assume H, trivial) (assume H H', H') theorem not_or_iff_not_and_not {a b : Prop} [Da : decidable a] [Db : decidable b] : ¬(a ∨ b) ↔ ¬a ∧ ¬b := iff.intro (assume H, or.elim (em a) (assume Ha, absurd (or.inl Ha) H) (assume Hna, or.elim (em b) (assume Hb, absurd (or.inr Hb) H) (assume Hnb, and.intro Hna Hnb))) (assume (H : ¬a ∧ ¬b) (N : a ∨ b), or.elim N (assume Ha, absurd Ha (and.elim_left H)) (assume Hb, absurd Hb (and.elim_right H))) theorem not_and_iff_not_or_not {a b : Prop} [Da : decidable a] [Db : decidable b] : ¬(a ∧ b) ↔ ¬a ∨ ¬b := iff.intro (assume H, or.elim (em a) (assume Ha, or.elim (em b) (assume Hb, absurd (and.intro Ha Hb) H) (assume Hnb, or.inr Hnb)) (assume Hna, or.inl Hna)) (assume (H : ¬a ∨ ¬b) (N : a ∧ b), or.elim H (assume Hna, absurd (and.elim_left N) Hna) (assume Hnb, absurd (and.elim_right N) Hnb)) theorem imp_iff_not_or {a b : Prop} [Da : decidable a] : (a → b) ↔ ¬a ∨ b := iff.intro (assume H : a → b, (or.elim (em a) (assume Ha : a, or.inr (H Ha)) (assume Hna : ¬a, or.inl Hna))) (assume (H : ¬a ∨ b) (Ha : a), or_resolve_right H (not_not_iff⁻¹ ▸ Ha)) theorem not_implies_iff_and_not {a b : Prop} [Da : decidable a] [Db : decidable b] : ¬(a → b) ↔ a ∧ ¬b := calc ¬(a → b) ↔ ¬(¬a ∨ b) : {imp_iff_not_or} ... ↔ ¬¬a ∧ ¬b : not_or_iff_not_and_not ... ↔ a ∧ ¬b : {not_not_iff} theorem peirce {a b : Prop} [D : decidable a] : ((a → b) → a) → a := assume H, by_contradiction (assume Hna : ¬a, have Hnna : ¬¬a, from not_not_of_not_implies (mt H Hna), absurd (not_not_elim Hnna) Hna) theorem forall_not_of_not_exists {A : Type} {P : A → Prop} [D : ∀x, decidable (P x)] (H : ¬∃x, P x) : ∀x, ¬P x := take x, or.elim (em (P x)) (assume Hp : P x, absurd (exists.intro x Hp) H) (assume Hn : ¬P x, Hn) theorem exists_not_of_not_forall {A : Type} {P : A → Prop} [D : ∀x, decidable (P x)] [D' : decidable (∃x, ¬P x)] (H : ¬∀x, P x) : ∃x, ¬P x := @by_contradiction _ D' (assume H1 : ¬∃x, ¬P x, have H2 : ∀x, ¬¬P x, from @forall_not_of_not_exists _ _ (take x, decidable_not) H1, have H3 : ∀x, P x, from take x, @not_not_elim _ (D x) (H2 x), absurd H3 H) theorem ne_self_iff_false {A : Type} (a : A) : (a ≠ a) ↔ false := iff.intro (assume H, false.of_ne H) (assume H, false.elim H) theorem eq_self_iff_true {A : Type} (a : A) : (a = a) ↔ true := iff_true_intro rfl theorem heq_self_iff_true {A : Type} (a : A) : (a == a) ↔ true := iff_true_intro (heq.refl a) theorem iff_not_self (a : Prop) : (a ↔ ¬a) ↔ false := iff.intro (assume H, have H' : ¬a, from assume Ha, (H ▸ Ha) Ha, H' (H⁻¹ ▸ H')) (assume H, false.elim H) theorem true_iff_false : (true ↔ false) ↔ false := not_true_iff_false ▸ (iff_not_self true) theorem false_iff_true : (false ↔ true) ↔ false := not_false_iff_true ▸ (iff_not_self false) theorem iff_true_iff (a : Prop) : (a ↔ true) ↔ a := iff.intro (assume H, of_iff_true H) (assume H, iff_true_intro H) theorem iff_false_iff_not (a : Prop) : (a ↔ false) ↔ ¬a := iff.intro (assume H, not_of_iff_false H) (assume H, iff_false_intro H)
416064f8f55a51703c404d21b7e4fccad5b4c3a0
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/data/polynomial/identities.lean
53ccf1007b7d62c62ee6fd36b1cfafd6246ad25c
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,324
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.derivative import tactic.ring_exp /-! # Theory of univariate polynomials The main def is `binom_expansion`. -/ noncomputable theory namespace polynomial universes u v w x y z variables {R : Type u} {S : Type v} {T : Type w} {ι : Type x} {k : Type y} {A : Type z} {a b : R} {m n : ℕ} section identities /- @TODO: pow_add_expansion and pow_sub_pow_factor are not specific to polynomials. These belong somewhere else. But not in group_power because they depend on tactic.ring_exp Maybe use data.nat.choose to prove it. -/ def pow_add_expansion {R : Type*} [comm_semiring R] (x y : R) : ∀ (n : ℕ), {k // (x + y)^n = x^n + n*x^(n-1)*y + k * y^2} | 0 := ⟨0, by simp⟩ | 1 := ⟨0, by simp⟩ | (n+2) := begin cases pow_add_expansion (n+1) with z hz, existsi x*z + (n+1)*x^n+z*y, calc (x + y) ^ (n + 2) = (x + y) * (x + y) ^ (n + 1) : by ring_exp ... = (x + y) * (x ^ (n + 1) + ↑(n + 1) * x ^ (n + 1 - 1) * y + z * y ^ 2) : by rw hz ... = x ^ (n + 2) + ↑(n + 2) * x ^ (n + 1) * y + (x*z + (n+1)*x^n+z*y) * y ^ 2 : by { push_cast, ring_exp! } end variables [comm_ring R] private def poly_binom_aux1 (x y : R) (e : ℕ) (a : R) : {k : R // a * (x + y)^e = a * (x^e + e*x^(e-1)*y + k*y^2)} := begin existsi (pow_add_expansion x y e).val, congr, apply (pow_add_expansion _ _ _).property end private lemma poly_binom_aux2 (f : polynomial R) (x y : R) : f.eval (x + y) = f.sum (λ e a, a * (x^e + e*x^(e-1)*y + (poly_binom_aux1 x y e a).val*y^2)) := begin unfold eval eval₂, congr' with n z, apply (poly_binom_aux1 x y _ _).property end private lemma poly_binom_aux3 (f : polynomial R) (x y : R) : f.eval (x + y) = f.sum (λ e a, a * x^e) + f.sum (λ e a, (a * e * x^(e-1)) * y) + f.sum (λ e a, (a *(poly_binom_aux1 x y e a).val)*y^2) := by { rw poly_binom_aux2, simp [left_distrib, sum_add, mul_assoc] } def binom_expansion (f : polynomial R) (x y : R) : {k : R // f.eval (x + y) = f.eval x + (f.derivative.eval x) * y + k * y^2} := begin existsi f.sum (λ e a, a *((poly_binom_aux1 x y e a).val)), rw poly_binom_aux3, congr, { rw [←eval_eq_sum], }, { rw derivative_eval, exact finset.sum_mul.symm }, { exact finset.sum_mul.symm } end def pow_sub_pow_factor (x y : R) : Π (i : ℕ), {z : R // 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), calc x ^ (k + 2) - y ^ (k + 2) = x * (x ^ (k + 1) - y ^ (k + 1)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by ring_exp ... = x * (z * (x - y)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by rw hz ... = (z * x + y ^ (k + 1)) * (x - y) : by ring_exp end def eval_sub_factor (f : polynomial R) (x y : R) : {z : R // f.eval x - f.eval y = z * (x - y)} := begin refine ⟨f.sum (λ i r, r * (pow_sub_pow_factor x y i).val), _⟩, delta eval eval₂, simp only [sum, ← finset.sum_sub_distrib, finset.sum_mul], dsimp, congr' with i r, rw [mul_assoc, ←(pow_sub_pow_factor x y _).prop, mul_sub], end end identities end polynomial
671fc68a510e3fb2d64ad52ead80882c518b51d7
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/argNameAtPlaceholderError.lean
6c849548461c7a1a88758883112621d9d6826ad3
[ "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
117
lean
import Lean open Lean open Lean.Elab open Lean.Elab.Term def f (stx : Syntax) : TermElabM Expr := elabTerm _ _ _
8eb190920dd8a71d89f9b6741603e75c1b7221eb
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Util/Profile.lean
a5e4964cc19965f493101c5e72d3cb9c2a727f7b
[ "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
573
lean
/- Copyright (c) 2019 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich -/ import Lean.Data.Position namespace Lean /-- Print and accumulate run time of `act` when Option `profiler` is set to `true`. -/ @[extern 5 "lean_lean_profileit"] constant profileit {α : Type} (category : @& String) (pos : @& Position) (act : IO α) : IO α := act def profileitPure {α : Type} (category : String) (pos : Position) (fn : Unit → α) : IO α := profileit category pos $ IO.lazyPure fn end Lean
6320c2022f02c965d708eb2cc7c9a65b7e18a93e
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/algebra/direct_limit.lean
7793159f124fed3a80052650d9b3fe8a36e5db79
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
25,256
lean
/- Copyright (c) 2019 Kenny Lau, Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes Direct limit of modules, abelian groups, rings, and fields. See Atiyah-Macdonald PP.32-33, Matsumura PP.269-270 Generalizes the notion of "union", or "gluing", of incomparable modules over the same ring, or incomparable abelian groups, or rings, or fields. It is constructed as a quotient of the free module (for the module case) or quotient of the free commutative ring (for the ring case) instead of a quotient of the disjoint union so as to make the operations (addition etc.) "computable". -/ import ring_theory.free_comm_ring universes u v w u₁ open submodule variables {R : Type u} [ring R] variables {ι : Type v} [nonempty ι] variables [decidable_eq ι] [directed_order ι] variables (G : ι → Type w) [Π i, decidable_eq (G i)] /-- A directed system is a functor from the category (directed poset) to another category. This is used for abelian groups and rings and fields because their maps are not bundled. See module.directed_system -/ class directed_system (f : Π i j, i ≤ j → G i → G j) : Prop := (map_self [] : ∀ i x h, f i i h x = x) (map_map [] : ∀ i j k hij hjk x, f j k hjk (f i j hij x) = f i k (le_trans hij hjk) x) namespace module variables [Π i, add_comm_group (G i)] [Π i, module R (G i)] /-- A directed system is a functor from the category (directed poset) to the category of R-modules. -/ class directed_system (f : Π i j, i ≤ j → G i →ₗ[R] G j) : Prop := (map_self [] : ∀ i x h, f i i h x = x) (map_map [] : ∀ i j k hij hjk x, f j k hjk (f i j hij x) = f i k (le_trans hij hjk) x) variables (f : Π i j, i ≤ j → G i →ₗ[R] G j) [directed_system G f] /-- The direct limit of a directed system is the modules glued together along the maps. -/ def direct_limit : Type (max v w) := (span R $ { a | ∃ (i j) (H : i ≤ j) x, direct_sum.lof R ι G i x - direct_sum.lof R ι G j (f i j H x) = a }).quotient namespace direct_limit instance : add_comm_group (direct_limit G f) := quotient.add_comm_group _ instance : module R (direct_limit G f) := quotient.module _ variables (R ι) /-- The canonical map from a component to the direct limit. -/ def of (i) : G i →ₗ[R] direct_limit G f := (mkq _).comp $ direct_sum.lof R ι G i variables {R ι G f} @[simp] lemma of_f {i j hij x} : (of R ι G f j (f i j hij x)) = of R ι G f i x := eq.symm $ (submodule.quotient.eq _).2 $ subset_span ⟨i, j, hij, x, rfl⟩ /-- Every element of the direct limit corresponds to some element in some component of the directed system. -/ theorem exists_of (z : direct_limit G f) : ∃ i x, of R ι G f i x = z := nonempty.elim (by apply_instance) $ assume ind : ι, quotient.induction_on' z $ λ z, direct_sum.induction_on z ⟨ind, 0, linear_map.map_zero _⟩ (λ i x, ⟨i, x, rfl⟩) (λ p q ⟨i, x, ihx⟩ ⟨j, y, ihy⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, f i k hik x + f j k hjk y, by rw [linear_map.map_add, of_f, of_f, ihx, ihy]; refl⟩) @[elab_as_eliminator] protected theorem induction_on {C : direct_limit G f → Prop} (z : direct_limit G f) (ih : ∀ i x, C (of R ι G f i x)) : C z := let ⟨i, x, h⟩ := exists_of z in h ▸ ih i x variables {P : Type u₁} [add_comm_group P] [module R P] (g : Π i, G i →ₗ[R] P) variables (Hg : ∀ i j hij x, g j (f i j hij x) = g i x) include Hg variables (R ι G f) /-- The universal property of the direct limit: maps from the components to another module that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. -/ def lift : direct_limit G f →ₗ[R] P := liftq _ (direct_sum.to_module R ι P g) (span_le.2 $ λ a ⟨i, j, hij, x, hx⟩, by rw [← hx, mem_coe, linear_map.sub_mem_ker_iff, direct_sum.to_module_lof, direct_sum.to_module_lof, Hg]) variables {R ι G f} omit Hg lemma lift_of {i} (x) : lift R ι G f g Hg (of R ι G f i x) = g i x := direct_sum.to_module_lof R _ _ theorem lift_unique (F : direct_limit G f →ₗ[R] P) (x) : F x = lift R ι G f (λ i, F.comp $ of R ι G f i) (λ i j hij x, by rw [linear_map.comp_apply, of_f]; refl) x := direct_limit.induction_on x $ λ i x, by rw lift_of; refl section totalize open_locale classical variables (G f) noncomputable def totalize : Π i j, G i →ₗ[R] G j := λ i j, if h : i ≤ j then f i j h else 0 variables {G f} lemma totalize_apply (i j x) : totalize G f i j x = if h : i ≤ j then f i j h x else 0 := if h : i ≤ j then by dsimp only [totalize]; rw [dif_pos h, dif_pos h] else by dsimp only [totalize]; rw [dif_neg h, dif_neg h, linear_map.zero_apply] end totalize lemma to_module_totalize_of_le {x : direct_sum ι G} {i j : ι} (hij : i ≤ j) (hx : ∀ k ∈ x.support, k ≤ i) : direct_sum.to_module R ι (G j) (λ k, totalize G f k j) x = f i j hij (direct_sum.to_module R ι (G i) (λ k, totalize G f k i) x) := begin rw [← @dfinsupp.sum_single ι G _ _ _ x], unfold dfinsupp.sum, simp only [linear_map.map_sum], refine finset.sum_congr rfl (λ k hk, _), rw direct_sum.single_eq_lof R k (x k), simp [totalize_apply, hx k hk, le_trans (hx k hk) hij, directed_system.map_map f] end lemma of.zero_exact_aux {x : direct_sum ι G} (H : submodule.quotient.mk x = (0 : direct_limit G f)) : ∃ j, (∀ k ∈ x.support, k ≤ j) ∧ direct_sum.to_module R ι (G j) (λ i, totalize G f i j) x = (0 : G j) := nonempty.elim (by apply_instance) $ assume ind : ι, span_induction ((quotient.mk_eq_zero _).1 H) (λ x ⟨i, j, hij, y, hxy⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, begin clear_, subst hxy, split, { intros i0 hi0, rw [dfinsupp.mem_support_iff, dfinsupp.sub_apply, ← direct_sum.single_eq_lof, ← direct_sum.single_eq_lof, dfinsupp.single_apply, dfinsupp.single_apply] at hi0, split_ifs at hi0 with hi hj hj, { rwa hi at hik }, { rwa hi at hik }, { rwa hj at hjk }, exfalso, apply hi0, rw sub_zero }, simp [linear_map.map_sub, totalize_apply, hik, hjk, directed_system.map_map f, direct_sum.apply_eq_component, direct_sum.component.of], end⟩) ⟨ind, λ _ h, (finset.not_mem_empty _ h).elim, linear_map.map_zero _⟩ (λ x y ⟨i, hi, hxi⟩ ⟨j, hj, hyj⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, λ l hl, (finset.mem_union.1 (dfinsupp.support_add hl)).elim (λ hl, le_trans (hi _ hl) hik) (λ hl, le_trans (hj _ hl) hjk), by simp [linear_map.map_add, hxi, hyj, to_module_totalize_of_le hik hi, to_module_totalize_of_le hjk hj]⟩) (λ a x ⟨i, hi, hxi⟩, ⟨i, λ k hk, hi k (dfinsupp.support_smul hk), by simp [linear_map.map_smul, hxi]⟩) /-- A component that corresponds to zero in the direct limit is already zero in some bigger module in the directed system. -/ theorem of.zero_exact {i x} (H : of R ι G f i x = 0) : ∃ j hij, f i j hij x = (0 : G j) := let ⟨j, hj, hxj⟩ := of.zero_exact_aux H in if hx0 : x = 0 then ⟨i, le_refl _, by simp [hx0]⟩ else have hij : i ≤ j, from hj _ $ by simp [direct_sum.apply_eq_component, hx0], ⟨j, hij, by simpa [totalize_apply, hij] using hxj⟩ end direct_limit end module namespace add_comm_group variables [Π i, add_comm_group (G i)] /-- The direct limit of a directed system is the abelian groups glued together along the maps. -/ def direct_limit (f : Π i j, i ≤ j → G i → G j) [Π i j hij, is_add_group_hom (f i j hij)] [directed_system G f] : Type* := @module.direct_limit ℤ _ ι _ _ _ G _ _ _ (λ i j hij, (add_monoid_hom.of $ f i j hij).to_int_linear_map) ⟨directed_system.map_self f, directed_system.map_map f⟩ namespace direct_limit variables (f : Π i j, i ≤ j → G i → G j) variables [Π i j hij, is_add_group_hom (f i j hij)] [directed_system G f] lemma directed_system : module.directed_system G (λ i j hij, (add_monoid_hom.of $ f i j hij).to_int_linear_map) := ⟨directed_system.map_self f, directed_system.map_map f⟩ local attribute [instance] directed_system instance : add_comm_group (direct_limit G f) := module.direct_limit.add_comm_group G (λ i j hij, (add_monoid_hom.of $f i j hij).to_int_linear_map) /-- The canonical map from a component to the direct limit. -/ def of (i) : G i → direct_limit G f := module.direct_limit.of ℤ ι G (λ i j hij, (add_monoid_hom.of $ f i j hij).to_int_linear_map) i variables {G f} instance of.is_add_group_hom (i) : is_add_group_hom (of G f i) := linear_map.is_add_group_hom _ @[simp] lemma of_f {i j} (hij) (x) : of G f j (f i j hij x) = of G f i x := module.direct_limit.of_f @[simp] lemma of_zero (i) : of G f i 0 = 0 := is_add_group_hom.map_zero _ @[simp] lemma of_add (i x y) : of G f i (x + y) = of G f i x + of G f i y := is_add_hom.map_add _ _ _ @[simp] lemma of_neg (i x) : of G f i (-x) = -of G f i x := is_add_group_hom.map_neg _ _ @[simp] lemma of_sub (i x y) : of G f i (x - y) = of G f i x - of G f i y := is_add_group_hom.map_sub _ _ _ @[elab_as_eliminator] protected theorem induction_on {C : direct_limit G f → Prop} (z : direct_limit G f) (ih : ∀ i x, C (of G f i x)) : C z := module.direct_limit.induction_on z ih /-- A component that corresponds to zero in the direct limit is already zero in some bigger module in the directed system. -/ theorem of.zero_exact (i x) (h : of G f i x = 0) : ∃ j hij, f i j hij x = 0 := module.direct_limit.of.zero_exact h variables (P : Type u₁) [add_comm_group P] variables (g : Π i, G i → P) [Π i, is_add_group_hom (g i)] variables (Hg : ∀ i j hij x, g j (f i j hij x) = g i x) variables (G f) /-- The universal property of the direct limit: maps from the components to another abelian group that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. -/ def lift : direct_limit G f → P := module.direct_limit.lift ℤ ι G (λ i j hij, (add_monoid_hom.of $ f i j hij).to_int_linear_map) (λ i, (add_monoid_hom.of $ g i).to_int_linear_map) Hg variables {G f} instance lift.is_add_group_hom : is_add_group_hom (lift G f P g Hg) := linear_map.is_add_group_hom _ @[simp] lemma lift_of (i x) : lift G f P g Hg (of G f i x) = g i x := module.direct_limit.lift_of _ _ _ @[simp] lemma lift_zero : lift G f P g Hg 0 = 0 := is_add_group_hom.map_zero _ @[simp] lemma lift_add (x y) : lift G f P g Hg (x + y) = lift G f P g Hg x + lift G f P g Hg y := is_add_hom.map_add _ _ _ @[simp] lemma lift_neg (x) : lift G f P g Hg (-x) = -lift G f P g Hg x := is_add_group_hom.map_neg _ _ @[simp] lemma lift_sub (x y) : lift G f P g Hg (x - y) = lift G f P g Hg x - lift G f P g Hg y := is_add_group_hom.map_sub _ _ _ lemma lift_unique (F : direct_limit G f → P) [is_add_group_hom F] (x) : F x = @lift _ _ _ _ G _ _ f _ _ P _ (λ i x, F $ of G f i x) (λ i, is_add_group_hom.comp _ _) (λ i j hij x, by dsimp; rw of_f) x := direct_limit.induction_on x $ λ i x, by rw lift_of end direct_limit end add_comm_group namespace ring variables [Π i, comm_ring (G i)] variables (f : Π i j, i ≤ j → G i → G j) variables [Π i j hij, is_ring_hom (f i j hij)] variables [directed_system G f] open free_comm_ring /-- The direct limit of a directed system is the rings glued together along the maps. -/ def direct_limit : Type (max v w) := (ideal.span { a | (∃ i j H x, of (⟨j, f i j H x⟩ : Σ i, G i) - of ⟨i, x⟩ = a) ∨ (∃ i, of (⟨i, 1⟩ : Σ i, G i) - 1 = a) ∨ (∃ i x y, of (⟨i, x + y⟩ : Σ i, G i) - (of ⟨i, x⟩ + of ⟨i, y⟩) = a) ∨ (∃ i x y, of (⟨i, x * y⟩ : Σ i, G i) - (of ⟨i, x⟩ * of ⟨i, y⟩) = a) }).quotient namespace direct_limit instance : comm_ring (direct_limit G f) := ideal.quotient.comm_ring _ instance : ring (direct_limit G f) := comm_ring.to_ring _ /-- The canonical map from a component to the direct limit. -/ def of (i) (x : G i) : direct_limit G f := ideal.quotient.mk _ $ of ⟨i, x⟩ variables {G f} instance of.is_ring_hom (i) : is_ring_hom (of G f i) := { map_one := ideal.quotient.eq.2 $ subset_span $ or.inr $ or.inl ⟨i, rfl⟩, map_mul := λ x y, ideal.quotient.eq.2 $ subset_span $ or.inr $ or.inr $ or.inr ⟨i, x, y, rfl⟩, map_add := λ x y, ideal.quotient.eq.2 $ subset_span $ or.inr $ or.inr $ or.inl ⟨i, x, y, rfl⟩ } @[simp] lemma of_f {i j} (hij) (x) : of G f j (f i j hij x) = of G f i x := ideal.quotient.eq.2 $ subset_span $ or.inl ⟨i, j, hij, x, rfl⟩ @[simp] lemma of_zero (i) : of G f i 0 = 0 := is_ring_hom.map_zero _ @[simp] lemma of_one (i) : of G f i 1 = 1 := is_ring_hom.map_one _ @[simp] lemma of_add (i x y) : of G f i (x + y) = of G f i x + of G f i y := is_ring_hom.map_add _ @[simp] lemma of_neg (i x) : of G f i (-x) = -of G f i x := is_ring_hom.map_neg _ @[simp] lemma of_sub (i x y) : of G f i (x - y) = of G f i x - of G f i y := is_ring_hom.map_sub _ @[simp] lemma of_mul (i x y) : of G f i (x * y) = of G f i x * of G f i y := is_ring_hom.map_mul _ @[simp] lemma of_pow (i x) (n : ℕ) : of G f i (x ^ n) = of G f i x ^ n := is_semiring_hom.map_pow _ _ _ /-- Every element of the direct limit corresponds to some element in some component of the directed system. -/ theorem exists_of (z : direct_limit G f) : ∃ i x, of G f i x = z := nonempty.elim (by apply_instance) $ assume ind : ι, quotient.induction_on' z $ λ x, free_abelian_group.induction_on x ⟨ind, 0, of_zero ind⟩ (λ s, multiset.induction_on s ⟨ind, 1, of_one ind⟩ (λ a s ih, let ⟨i, x⟩ := a, ⟨j, y, hs⟩ := ih, ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, f i k hik x * f j k hjk y, by rw [of_mul, of_f, of_f, hs]; refl⟩)) (λ s ⟨i, x, ih⟩, ⟨i, -x, by rw [of_neg, ih]; refl⟩) (λ p q ⟨i, x, ihx⟩ ⟨j, y, ihy⟩, let ⟨k, hik, hjk⟩ := directed_order.directed i j in ⟨k, f i k hik x + f j k hjk y, by rw [of_add, of_f, of_f, ihx, ihy]; refl⟩) @[elab_as_eliminator] theorem induction_on {C : direct_limit G f → Prop} (z : direct_limit G f) (ih : ∀ i x, C (of G f i x)) : C z := let ⟨i, x, hx⟩ := exists_of z in hx ▸ ih i x section of_zero_exact open_locale classical variables (G f) lemma of.zero_exact_aux2 {x : free_comm_ring Σ i, G i} {s t} (hxs : is_supported x s) {j k} (hj : ∀ z : Σ i, G i, z ∈ s → z.1 ≤ j) (hk : ∀ z : Σ i, G i, z ∈ t → z.1 ≤ k) (hjk : j ≤ k) (hst : s ⊆ t) : f j k hjk (lift (λ ix : s, f ix.1.1 j (hj ix ix.2) ix.1.2) (restriction s x)) = lift (λ ix : t, f ix.1.1 k (hk ix ix.2) ix.1.2) (restriction t x) := begin refine ring.in_closure.rec_on hxs _ _ _ _, { rw [restriction_one, lift_one, is_ring_hom.map_one (f j k hjk), restriction_one, lift_one] }, { rw [restriction_neg, restriction_one, lift_neg, lift_one, is_ring_hom.map_neg (f j k hjk), is_ring_hom.map_one (f j k hjk), restriction_neg, restriction_one, lift_neg, lift_one] }, { rintros _ ⟨p, hps, rfl⟩ n ih, rw [restriction_mul, lift_mul, is_ring_hom.map_mul (f j k hjk), ih, restriction_mul, lift_mul, restriction_of, dif_pos hps, lift_of, restriction_of, dif_pos (hst hps), lift_of], dsimp only, rw directed_system.map_map f, refl }, { rintros x y ihx ihy, rw [restriction_add, lift_add, is_ring_hom.map_add (f j k hjk), ihx, ihy, restriction_add, lift_add] } end variables {G f} lemma of.zero_exact_aux {x : free_comm_ring Σ i, G i} (H : ideal.quotient.mk _ x = (0 : direct_limit G f)) : ∃ j s, ∃ H : (∀ k : Σ i, G i, k ∈ s → k.1 ≤ j), is_supported x s ∧ lift (λ ix : s, f ix.1.1 j (H ix ix.2) ix.1.2) (restriction s x) = (0 : G j) := begin refine span_induction (ideal.quotient.eq_zero_iff_mem.1 H) _ _ _ _, { rintros x (⟨i, j, hij, x, rfl⟩ | ⟨i, rfl⟩ | ⟨i, x, y, rfl⟩ | ⟨i, x, y, rfl⟩), { refine ⟨j, {⟨i, x⟩, ⟨j, f i j hij x⟩}, _, is_supported_sub (is_supported_of.2 $ or.inr rfl) (is_supported_of.2 $ or.inl rfl), _⟩, { rintros k (rfl | ⟨rfl | _⟩), exact hij, refl }, { rw [restriction_sub, lift_sub, restriction_of, dif_pos, restriction_of, dif_pos, lift_of, lift_of], dsimp only, rw directed_system.map_map f, exact sub_self _, exacts [or.inr rfl, or.inl rfl] } }, { refine ⟨i, {⟨i, 1⟩}, _, is_supported_sub (is_supported_of.2 rfl) is_supported_one, _⟩, { rintros k (rfl|h), refl }, { rw [restriction_sub, lift_sub, restriction_of, dif_pos, restriction_one, lift_of, lift_one], dsimp only, rw [is_ring_hom.map_one (f i i _), sub_self], exacts [_inst_7 i i _, rfl] } }, { refine ⟨i, {⟨i, x+y⟩, ⟨i, x⟩, ⟨i, y⟩}, _, is_supported_sub (is_supported_of.2 $ or.inl rfl) (is_supported_add (is_supported_of.2 $ or.inr $ or.inl rfl) (is_supported_of.2 $ or.inr $ or.inr rfl)), _⟩, { rintros k (rfl | ⟨rfl | ⟨rfl | hk⟩⟩); refl }, { rw [restriction_sub, restriction_add, restriction_of, restriction_of, restriction_of, dif_pos, dif_pos, dif_pos, lift_sub, lift_add, lift_of, lift_of, lift_of], dsimp only, rw is_ring_hom.map_add (f i i _), exact sub_self _, exacts [or.inl rfl, by apply_instance, or.inr (or.inr rfl), or.inr (or.inl rfl)] } }, { refine ⟨i, {⟨i, x*y⟩, ⟨i, x⟩, ⟨i, y⟩}, _, is_supported_sub (is_supported_of.2 $ or.inl rfl) (is_supported_mul (is_supported_of.2 $ or.inr $ or.inl rfl) (is_supported_of.2 $ or.inr $ or.inr rfl)), _⟩, { rintros k (rfl | ⟨rfl | ⟨rfl | hk⟩⟩); refl }, { rw [restriction_sub, restriction_mul, restriction_of, restriction_of, restriction_of, dif_pos, dif_pos, dif_pos, lift_sub, lift_mul, lift_of, lift_of, lift_of], dsimp only, rw is_ring_hom.map_mul (f i i _), exacts [sub_self _, or.inl rfl, by apply_instance, or.inr (or.inr rfl), or.inr (or.inl rfl)] } } }, { refine nonempty.elim (by apply_instance) (assume ind : ι, _), refine ⟨ind, ∅, λ _, false.elim, is_supported_zero, _⟩, rw [restriction_zero, lift_zero] }, { rintros x y ⟨i, s, hi, hxs, ihs⟩ ⟨j, t, hj, hyt, iht⟩, rcases directed_order.directed i j with ⟨k, hik, hjk⟩, have : ∀ z : Σ i, G i, z ∈ s ∪ t → z.1 ≤ k, { rintros z (hz | hz), exact le_trans (hi z hz) hik, exact le_trans (hj z hz) hjk }, refine ⟨k, s ∪ t, this, is_supported_add (is_supported_upwards hxs $ set.subset_union_left s t) (is_supported_upwards hyt $ set.subset_union_right s t), _⟩, { rw [restriction_add, lift_add, ← of.zero_exact_aux2 G f hxs hi this hik (set.subset_union_left s t), ← of.zero_exact_aux2 G f hyt hj this hjk (set.subset_union_right s t), ihs, is_ring_hom.map_zero (f i k hik), iht, is_ring_hom.map_zero (f j k hjk), zero_add] } }, { rintros x y ⟨j, t, hj, hyt, iht⟩, rw smul_eq_mul, rcases exists_finset_support x with ⟨s, hxs⟩, rcases (s.image sigma.fst).exists_le with ⟨i, hi⟩, rcases directed_order.directed i j with ⟨k, hik, hjk⟩, have : ∀ z : Σ i, G i, z ∈ ↑s ∪ t → z.1 ≤ k, { rintros z (hz | hz), exact le_trans (hi z.1 $ finset.mem_image.2 ⟨z, hz, rfl⟩) hik, exact le_trans (hj z hz) hjk }, refine ⟨k, ↑s ∪ t, this, is_supported_mul (is_supported_upwards hxs $ set.subset_union_left ↑s t) (is_supported_upwards hyt $ set.subset_union_right ↑s t), _⟩, rw [restriction_mul, lift_mul, ← of.zero_exact_aux2 G f hyt hj this hjk (set.subset_union_right ↑s t), iht, is_ring_hom.map_zero (f j k hjk), mul_zero] } end /-- A component that corresponds to zero in the direct limit is already zero in some bigger module in the directed system. -/ lemma of.zero_exact {i x} (hix : of G f i x = 0) : ∃ j, ∃ hij : i ≤ j, f i j hij x = 0 := let ⟨j, s, H, hxs, hx⟩ := of.zero_exact_aux hix in have hixs : (⟨i, x⟩ : Σ i, G i) ∈ s, from is_supported_of.1 hxs, ⟨j, H ⟨i, x⟩ hixs, by rw [restriction_of, dif_pos hixs, lift_of] at hx; exact hx⟩ end of_zero_exact /-- If the maps in the directed system are injective, then the canonical maps from the components to the direct limits are injective. -/ theorem of_inj (hf : ∀ i j hij, function.injective (f i j hij)) (i) : function.injective (of G f i) := begin suffices : ∀ x, of G f i x = 0 → x = 0, { intros x y hxy, rw ← sub_eq_zero_iff_eq, apply this, rw [is_ring_hom.map_sub (of G f i), hxy, sub_self] }, intros x hx, rcases of.zero_exact hx with ⟨j, hij, hfx⟩, apply hf i j hij, rw [hfx, is_ring_hom.map_zero (f i j hij)] end variables (P : Type u₁) [comm_ring P] variables (g : Π i, G i → P) [Π i, is_ring_hom (g i)] variables (Hg : ∀ i j hij x, g j (f i j hij x) = g i x) include Hg open free_comm_ring variables (G f) /-- The universal property of the direct limit: maps from the components to another ring that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. We don't use this function as the canonical form because Lean 3 fails to automatically coerce it to a function; use `lift` instead. -/ def lift_hom : direct_limit G f →+* P := ideal.quotient.lift _ (free_comm_ring.lift_hom $ λ x, g x.1 x.2) begin suffices : ideal.span _ ≤ ideal.comap (free_comm_ring.lift_hom (λ (x : Σ (i : ι), G i), g (x.fst) (x.snd))) ⊥, { intros x hx, exact (mem_bot P).1 (this hx) }, rw ideal.span_le, intros x hx, rw [mem_coe, ideal.mem_comap, mem_bot], rcases hx with ⟨i, j, hij, x, rfl⟩ | ⟨i, rfl⟩ | ⟨i, x, y, rfl⟩ | ⟨i, x, y, rfl⟩; simp only [coe_lift_hom, lift_sub, lift_of, Hg, lift_one, lift_add, lift_mul, is_ring_hom.map_one (g i), is_ring_hom.map_add (g i), is_ring_hom.map_mul (g i), sub_self] end /-- The universal property of the direct limit: maps from the components to another ring that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. -/ def lift : direct_limit G f → P := lift_hom G f P g Hg instance lift_is_ring_hom : is_ring_hom (lift G f P g Hg) := (lift_hom G f P g Hg).is_ring_hom variables {G f} omit Hg @[simp] lemma lift_of (i x) : lift G f P g Hg (of G f i x) = g i x := free_comm_ring.lift_of _ _ @[simp] lemma lift_zero : lift G f P g Hg 0 = 0 := (lift_hom G f P g Hg).map_zero @[simp] lemma lift_one : lift G f P g Hg 1 = 1 := (lift_hom G f P g Hg).map_one @[simp] lemma lift_add (x y) : lift G f P g Hg (x + y) = lift G f P g Hg x + lift G f P g Hg y := (lift_hom G f P g Hg).map_add x y @[simp] lemma lift_neg (x) : lift G f P g Hg (-x) = -lift G f P g Hg x := (lift_hom G f P g Hg).map_neg x @[simp] lemma lift_sub (x y) : lift G f P g Hg (x - y) = lift G f P g Hg x - lift G f P g Hg y := (lift_hom G f P g Hg).map_sub x y @[simp] lemma lift_mul (x y) : lift G f P g Hg (x * y) = lift G f P g Hg x * lift G f P g Hg y := (lift_hom G f P g Hg).map_mul x y @[simp] lemma lift_pow (x) (n : ℕ) : lift G f P g Hg (x ^ n) = lift G f P g Hg x ^ n := (lift_hom G f P g Hg).map_pow x n local attribute [instance, priority 100] is_ring_hom.comp theorem lift_unique (F : direct_limit G f → P) [is_ring_hom F] (x) : F x = lift G f P (λ i x, F $ of G f i x) (λ i j hij x, by rw [of_f]) x := direct_limit.induction_on x $ λ i x, by rw lift_of end direct_limit end ring namespace field variables [Π i, field (G i)] variables (f : Π i j, i ≤ j → G i → G j) [Π i j hij, is_ring_hom (f i j hij)] variables [directed_system G f] namespace direct_limit instance nonzero_comm_ring : nonzero_comm_ring (ring.direct_limit G f) := { zero_ne_one := nonempty.elim (by apply_instance) $ assume i : ι, begin change (0 : ring.direct_limit G f) ≠ 1, rw ← ring.direct_limit.of_one, intros H, rcases ring.direct_limit.of.zero_exact H.symm with ⟨j, hij, hf⟩, rw is_ring_hom.map_one (f i j hij) at hf, exact one_ne_zero hf end, .. ring.direct_limit.comm_ring G f } theorem exists_inv {p : ring.direct_limit G f} : p ≠ 0 → ∃ y, p * y = 1 := ring.direct_limit.induction_on p $ λ i x H, ⟨ring.direct_limit.of G f i (x⁻¹), by erw [← ring.direct_limit.of_mul, mul_inv_cancel (assume h : x = 0, H $ by rw [h, ring.direct_limit.of_zero]), ring.direct_limit.of_one]⟩ section open_locale classical noncomputable def inv (p : ring.direct_limit G f) : ring.direct_limit G f := if H : p = 0 then 0 else classical.some (direct_limit.exists_inv G f H) protected theorem mul_inv_cancel {p : ring.direct_limit G f} (hp : p ≠ 0) : p * inv G f p = 1 := by rw [inv, dif_neg hp, classical.some_spec (direct_limit.exists_inv G f hp)] protected theorem inv_mul_cancel {p : ring.direct_limit G f} (hp : p ≠ 0) : inv G f p * p = 1 := by rw [_root_.mul_comm, direct_limit.mul_inv_cancel G f hp] protected noncomputable def field : field (ring.direct_limit G f) := { inv := inv G f, mul_inv_cancel := λ p, direct_limit.mul_inv_cancel G f, inv_zero := dif_pos rfl, .. direct_limit.nonzero_comm_ring G f } end end direct_limit end field
94f033e9c7eefb408c8be34c6c0f68003c975edd
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/divisibility.lean
e00d6e6785ddddf6c41c207c922e52407c6e5ff9
[ "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
8,926
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Amelia Livingston, Yury Kudryashov, Neil Strickland, Aaron Anderson -/ import algebra.group_with_zero.basic /-! # Divisibility This file defines the basics of the divisibility relation in the context of `(comm_)` `monoid`s `(_with_zero)`. ## Main definitions * `monoid.has_dvd` ## Implementation notes The divisibility relation is defined for all monoids, and as such, depends on the order of multiplication if the monoid is not commutative. There are two possible conventions for divisibility in the noncommutative context, and this relation follows the convention for ordinals, so `a | b` is defined as `∃ c, b = a * c`. ## Tags divisibility, divides -/ variables {α : Type*} section monoid variables [monoid α] {a b c : α} /-- There are two possible conventions for divisibility, which coincide in a `comm_monoid`. This matches the convention for ordinals. -/ @[priority 100] instance monoid_has_dvd : has_dvd α := has_dvd.mk (λ a b, ∃ c, b = a * c) -- TODO: this used to not have `c` explicit, but that seems to be important -- for use with tactics, similar to `exists.intro` theorem dvd.intro (c : α) (h : a * c = b) : a ∣ b := exists.intro c h^.symm alias dvd.intro ← dvd_of_mul_right_eq theorem exists_eq_mul_right_of_dvd (h : a ∣ b) : ∃ c, b = a * c := h theorem dvd.elim {P : Prop} {a b : α} (H₁ : a ∣ b) (H₂ : ∀ c, b = a * c → P) : P := exists.elim H₁ H₂ @[refl, simp] theorem dvd_refl (a : α) : a ∣ a := dvd.intro 1 (mul_one _) lemma dvd_rfl {a : α} : a ∣ a := dvd_refl a local attribute [simp] mul_assoc mul_comm mul_left_comm @[trans] theorem dvd_trans (h₁ : a ∣ b) (h₂ : b ∣ c) : a ∣ c := match h₁, h₂ with | ⟨d, (h₃ : b = a * d)⟩, ⟨e, (h₄ : c = b * e)⟩ := ⟨d * e, show c = a * (d * e), by simp [h₃, h₄]⟩ end alias dvd_trans ← has_dvd.dvd.trans theorem one_dvd (a : α) : 1 ∣ a := dvd.intro a (one_mul _) @[simp] theorem dvd_mul_right (a b : α) : a ∣ a * b := dvd.intro b rfl theorem dvd_mul_of_dvd_left (h : a ∣ b) (c : α) : a ∣ b * c := h.trans (dvd_mul_right b c) alias dvd_mul_of_dvd_left ← has_dvd.dvd.mul_right theorem dvd_of_mul_right_dvd (h : a * b ∣ c) : a ∣ c := (dvd_mul_right a b).trans h section map_dvd variables {M N : Type*} lemma mul_hom.map_dvd [monoid M] [monoid N] (f : mul_hom M N) {a b} : a ∣ b → f a ∣ f b | ⟨c, h⟩ := ⟨f c, h.symm ▸ f.map_mul a c⟩ lemma monoid_hom.map_dvd [monoid M] [monoid N] (f : M →* N) {a b} : a ∣ b → f a ∣ f b := f.to_mul_hom.map_dvd end map_dvd end monoid section comm_monoid variables [comm_monoid α] {a b c : α} theorem dvd.intro_left (c : α) (h : c * a = b) : a ∣ b := dvd.intro _ (begin rewrite mul_comm at h, apply h end) alias dvd.intro_left ← dvd_of_mul_left_eq theorem exists_eq_mul_left_of_dvd (h : a ∣ b) : ∃ c, b = c * a := dvd.elim h (assume c, assume H1 : b = a * c, exists.intro c (eq.trans H1 (mul_comm a c))) lemma dvd_iff_exists_eq_mul_left : a ∣ b ↔ ∃ c, b = c * a := ⟨exists_eq_mul_left_of_dvd, by { rintro ⟨c, rfl⟩, exact ⟨c, mul_comm _ _⟩, }⟩ theorem dvd.elim_left {P : Prop} (h₁ : a ∣ b) (h₂ : ∀ c, b = c * a → P) : P := exists.elim (exists_eq_mul_left_of_dvd h₁) (assume c, assume h₃ : b = c * a, h₂ c h₃) @[simp] theorem dvd_mul_left (a b : α) : a ∣ b * a := dvd.intro b (mul_comm a b) theorem dvd_mul_of_dvd_right (h : a ∣ b) (c : α) : a ∣ c * b := begin rw mul_comm, exact h.mul_right _ end alias dvd_mul_of_dvd_right ← has_dvd.dvd.mul_left local attribute [simp] mul_assoc mul_comm mul_left_comm theorem mul_dvd_mul : ∀ {a b c d : α}, a ∣ b → c ∣ d → a * c ∣ b * d | a ._ c ._ ⟨e, rfl⟩ ⟨f, rfl⟩ := ⟨e * f, by simp⟩ theorem mul_dvd_mul_left (a : α) {b c : α} (h : b ∣ c) : a * b ∣ a * c := mul_dvd_mul (dvd_refl a) h theorem mul_dvd_mul_right (h : a ∣ b) (c : α) : a * c ∣ b * c := mul_dvd_mul h (dvd_refl c) theorem dvd_of_mul_left_dvd (h : a * b ∣ c) : b ∣ c := dvd.elim h (λ d ceq, dvd.intro (a * d) (by simp [ceq])) end comm_monoid section monoid_with_zero variables [monoid_with_zero α] {a : α} theorem eq_zero_of_zero_dvd (h : 0 ∣ a) : a = 0 := dvd.elim h (assume c, assume H' : a = 0 * c, eq.trans H' (zero_mul c)) /-- Given an element `a` of a commutative monoid with zero, there exists another element whose product with zero equals `a` iff `a` equals zero. -/ @[simp] lemma zero_dvd_iff : 0 ∣ a ↔ a = 0 := ⟨eq_zero_of_zero_dvd, λ h, by rw h⟩ @[simp] theorem dvd_zero (a : α) : a ∣ 0 := dvd.intro 0 (by simp) end monoid_with_zero /-- Given two elements `b`, `c` of a `cancel_monoid_with_zero` and a nonzero element `a`, `a*b` divides `a*c` iff `b` divides `c`. -/ theorem mul_dvd_mul_iff_left [cancel_monoid_with_zero α] {a b c : α} (ha : a ≠ 0) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ λ d, by rw [mul_assoc, mul_right_inj' ha] /-- Given two elements `a`, `b` of a commutative `cancel_monoid_with_zero` and a nonzero element `c`, `a*c` divides `b*c` iff `a` divides `b`. -/ theorem mul_dvd_mul_iff_right [cancel_comm_monoid_with_zero α] {a b c : α} (hc : c ≠ 0) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ λ d, by rw [mul_right_comm, mul_left_inj' hc] /-! ### Units in various monoids -/ namespace units section monoid variables [monoid α] {a b : α} {u : αˣ} /-- Elements of the unit group of a monoid represented as elements of the monoid divide any element of the monoid. -/ lemma coe_dvd : ↑u ∣ a := ⟨↑u⁻¹ * a, by simp⟩ /-- In a monoid, an element `a` divides an element `b` iff `a` divides all associates of `b`. -/ lemma dvd_mul_right : a ∣ b * u ↔ a ∣ b := iff.intro (assume ⟨c, eq⟩, ⟨c * ↑u⁻¹, by rw [← mul_assoc, ← eq, units.mul_inv_cancel_right]⟩) (assume ⟨c, eq⟩, eq.symm ▸ (dvd_mul_right _ _).mul_right _) /-- In a monoid, an element `a` divides an element `b` iff all associates of `a` divide `b`. -/ lemma mul_right_dvd : a * u ∣ b ↔ a ∣ b := iff.intro (λ ⟨c, eq⟩, ⟨↑u * c, eq.trans (mul_assoc _ _ _)⟩) (λ h, dvd_trans (dvd.intro ↑u⁻¹ (by rw [mul_assoc, u.mul_inv, mul_one])) h) end monoid section comm_monoid variables [comm_monoid α] {a b : α} {u : αˣ} /-- In a commutative monoid, an element `a` divides an element `b` iff `a` divides all left associates of `b`. -/ lemma dvd_mul_left : a ∣ u * b ↔ a ∣ b := by { rw mul_comm, apply dvd_mul_right } /-- In a commutative monoid, an element `a` divides an element `b` iff all left associates of `a` divide `b`.-/ lemma mul_left_dvd : ↑u * a ∣ b ↔ a ∣ b := by { rw mul_comm, apply mul_right_dvd } end comm_monoid end units namespace is_unit section monoid variables [monoid α] {a b u : α} (hu : is_unit u) include hu /-- Units of a monoid divide any element of the monoid. -/ @[simp] lemma dvd : u ∣ a := by { rcases hu with ⟨u, rfl⟩, apply units.coe_dvd, } @[simp] lemma dvd_mul_right : a ∣ b * u ↔ a ∣ b := by { rcases hu with ⟨u, rfl⟩, apply units.dvd_mul_right, } /-- In a monoid, an element a divides an element b iff all associates of `a` divide `b`.-/ @[simp] lemma mul_right_dvd : a * u ∣ b ↔ a ∣ b := by { rcases hu with ⟨u, rfl⟩, apply units.mul_right_dvd, } end monoid section comm_monoid variables [comm_monoid α] (a b u : α) (hu : is_unit u) include hu /-- In a commutative monoid, an element `a` divides an element `b` iff `a` divides all left associates of `b`. -/ @[simp] lemma dvd_mul_left : a ∣ u * b ↔ a ∣ b := by { rcases hu with ⟨u, rfl⟩, apply units.dvd_mul_left, } /-- In a commutative monoid, an element `a` divides an element `b` iff all left associates of `a` divide `b`.-/ @[simp] lemma mul_left_dvd : u * a ∣ b ↔ a ∣ b := by { rcases hu with ⟨u, rfl⟩, apply units.mul_left_dvd, } end comm_monoid end is_unit section comm_monoid_with_zero variable [comm_monoid_with_zero α] /-- `dvd_not_unit a b` expresses that `a` divides `b` "strictly", i.e. that `b` divided by `a` is not a unit. -/ def dvd_not_unit (a b : α) : Prop := a ≠ 0 ∧ ∃ x, ¬is_unit x ∧ b = a * x lemma dvd_not_unit_of_dvd_of_not_dvd {a b : α} (hd : a ∣ b) (hnd : ¬ b ∣ a) : dvd_not_unit a b := begin split, { rintro rfl, exact hnd (dvd_zero _) }, { rcases hd with ⟨c, rfl⟩, refine ⟨c, _, rfl⟩, rintro ⟨u, rfl⟩, simpa using hnd } end end comm_monoid_with_zero section monoid_with_zero variable [monoid_with_zero α] theorem ne_zero_of_dvd_ne_zero {p q : α} (h₁ : q ≠ 0) (h₂ : p ∣ q) : p ≠ 0 := begin rcases h₂ with ⟨u, rfl⟩, exact left_ne_zero_of_mul h₁, end end monoid_with_zero
195f2b8281d89b1890882a69b91d586ce00e602c
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/691.lean
d3dbbb7825860b8d97ef1eae3d13799f2edee78a
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
203
lean
theorem foo : Type₁ := unit example : foo = unit := by unfold foo example : foo = unit := by unfold [foo] example : foo = unit := by rewrite [↑foo] example : foo = unit := by rewrite [↑[foo] ]
4da3a22dcbf2b790388011ee5ff9e72cea0e86b9
82e44445c70db0f03e30d7be725775f122d72f3e
/src/data/int/order.lean
7b0f9c1211b894e85a088da24e1e74799f1a6fbf
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
2,962
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 order.conditionally_complete_lattice import data.int.least_greatest /-! ## `ℤ` forms a conditionally complete linear order The integers form a conditionally complete linear order. -/ open int open_locale classical noncomputable theory instance : conditionally_complete_linear_order ℤ := { Sup := λ s, if h : s.nonempty ∧ bdd_above s then greatest_of_bdd (classical.some h.2) (classical.some_spec h.2) h.1 else 0, Inf := λ s, if h : s.nonempty ∧ bdd_below s then least_of_bdd (classical.some h.2) (classical.some_spec h.2) h.1 else 0, le_cSup := begin intros s n hs hns, have : s.nonempty ∧ bdd_above s := ⟨⟨n, hns⟩, hs⟩, rw [dif_pos this], exact (greatest_of_bdd _ _ _).2.2 n hns end, cSup_le := begin intros s n hs hns, have : s.nonempty ∧ bdd_above s := ⟨hs, ⟨n, hns⟩⟩, rw [dif_pos this], exact hns (greatest_of_bdd _ (classical.some_spec this.2) _).2.1 end, cInf_le := begin intros s n hs hns, have : s.nonempty ∧ bdd_below s := ⟨⟨n, hns⟩, hs⟩, rw [dif_pos this], exact (least_of_bdd _ _ _).2.2 n hns end, le_cInf := begin intros s n hs hns, have : s.nonempty ∧ bdd_below s := ⟨hs, ⟨n, hns⟩⟩, rw [dif_pos this], exact hns (least_of_bdd _ (classical.some_spec this.2) _).2.1 end, .. int.linear_order, ..lattice_of_linear_order } namespace int lemma cSup_eq_greatest_of_bdd {s : set ℤ} [decidable_pred (∈ s)] (b : ℤ) (Hb : ∀ z ∈ s, z ≤ b) (Hinh : ∃ z : ℤ, z ∈ s) : Sup s = greatest_of_bdd b Hb Hinh := begin convert dif_pos _ using 1, { convert coe_greatest_of_bdd_eq _ (classical.some_spec (⟨b, Hb⟩ : bdd_above s)) _ }, { exact ⟨Hinh, b, Hb⟩, } end @[simp] lemma cSup_empty : Sup (∅ : set ℤ) = 0 := dif_neg (by simp) lemma cSup_of_not_bdd_above {s : set ℤ} (h : ¬ bdd_above s) : Sup s = 0 := dif_neg (by simp [h]) lemma cInf_eq_least_of_bdd {s : set ℤ} [decidable_pred (∈ s)] (b : ℤ) (Hb : ∀ z ∈ s, b ≤ z) (Hinh : ∃ z : ℤ, z ∈ s) : Inf s = least_of_bdd b Hb Hinh := begin convert dif_pos _ using 1, { convert coe_least_of_bdd_eq _ (classical.some_spec (⟨b, Hb⟩ : bdd_below s)) _ }, { exact ⟨Hinh, b, Hb⟩, } end @[simp] lemma cInf_empty : Inf (∅ : set ℤ) = 0 := dif_neg (by simp) lemma cInf_of_not_bdd_below {s : set ℤ} (h : ¬ bdd_below s) : Inf s = 0 := dif_neg (by simp [h]) lemma cSup_mem {s : set ℤ} (h1 : s.nonempty) (h2 : bdd_above s) : Sup s ∈ s := begin convert (greatest_of_bdd _ (classical.some_spec h2) h1).2.1, exact dif_pos ⟨h1, h2⟩, end lemma cInf_mem {s : set ℤ} (h1 : s.nonempty) (h2 : bdd_below s) : Inf s ∈ s := begin convert (least_of_bdd _ (classical.some_spec h2) h1).2.1, exact dif_pos ⟨h1, h2⟩, end end int
1e59361298884b0637e8f7390a1ce4e9435bbc11
76df16d6c3760cb415f1294caee997cc4736e09b
/lean/src/tactic/all.lean
5f551722a6cc1a9efd9c02f9dbc2063a36517e7f
[ "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
88
lean
import .focus_case import .cases_all import .with_cases import .anon_case import .contra
858871455a78d0338c1a05437f7d47958b25bb3f
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/finset/slice.lean
7f780f844b2b4b9e5909f495eaf185abd8248a79
[ "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
5,277
lean
/- Copyright (c) 2021 Bhavik Mehta, Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Alena Gusakov, Yaël Dillies -/ import algebra.big_operators.basic import data.nat.interval import order.antichain /-! # `r`-sets and slice This file defines the `r`-th slice of a set family and provides a way to say that a set family is made of `r`-sets. An `r`-set is a finset of cardinality `r` (aka of *size* `r`). The `r`-th slice of a set family is the set family made of its `r`-sets. ## Main declarations * `set.sized`: `A.sized r` means that `A` only contains `r`-sets. * `finset.slice`: `A.slice r` is the set of `r`-sets in `A`. ## Notation `A # r` is notation for `A.slice r` in locale `finset_family`. -/ open finset nat open_locale big_operators variables {α : Type*} {ι : Sort*} {κ : ι → Sort*} namespace set variables {A B : set (finset α)} {r : ℕ} /-! ### Families of `r`-sets -/ /-- `sized r A` means that every finset in `A` has size `r`. -/ def sized (r : ℕ) (A : set (finset α)) : Prop := ∀ ⦃x⦄, x ∈ A → card x = r lemma sized.mono (h : A ⊆ B) (hB : B.sized r) : A.sized r := λ x hx, hB $ h hx lemma sized_union : (A ∪ B).sized r ↔ A.sized r ∧ B.sized r := ⟨λ hA, ⟨hA.mono $ subset_union_left _ _, hA.mono $ subset_union_right _ _⟩, λ hA x hx, hx.elim (λ h, hA.1 h) $ λ h, hA.2 h⟩ alias sized_union ↔ _ sized.union --TODO: A `forall_Union` lemma would be handy here. @[simp] lemma sized_Union {f : ι → set (finset α)} : (⋃ i, f i).sized r ↔ ∀ i, (f i).sized r := by { simp_rw [set.sized, set.mem_Union, forall_exists_index], exact forall_swap } @[simp] lemma sized_Union₂ {f : Π i, κ i → set (finset α)} : (⋃ i j, f i j).sized r ↔ ∀ i j, (f i j).sized r := by simp_rw sized_Union protected lemma sized.is_antichain (hA : A.sized r) : is_antichain (⊆) A := λ s hs t ht h hst, h $ finset.eq_of_subset_of_card_le hst ((hA ht).trans (hA hs).symm).le protected lemma sized.subsingleton (hA : A.sized 0) : A.subsingleton := subsingleton_of_forall_eq ∅ $ λ s hs, card_eq_zero.1 $ hA hs lemma sized.subsingleton' [fintype α] (hA : A.sized (fintype.card α)) : A.subsingleton := subsingleton_of_forall_eq finset.univ $ λ s hs, s.card_eq_iff_eq_univ.1 $ hA hs lemma sized.empty_mem_iff (hA : A.sized r) : ∅ ∈ A ↔ A = {∅} := hA.is_antichain.bot_mem_iff lemma sized.univ_mem_iff [fintype α] (hA : A.sized r) : finset.univ ∈ A ↔ A = {finset.univ} := hA.is_antichain.top_mem_iff lemma sized_powerset_len (s : finset α) (r : ℕ) : (powerset_len r s : set (finset α)).sized r := λ t ht, (mem_powerset_len.1 ht).2 end set namespace finset section sized variables [fintype α] {𝒜 : finset (finset α)} {s : finset α} {r : ℕ} lemma subset_powerset_len_univ_iff : 𝒜 ⊆ powerset_len r univ ↔ (𝒜 : set (finset α)).sized r := forall_congr $ λ A, by rw [mem_powerset_len_univ_iff, mem_coe] alias subset_powerset_len_univ_iff ↔ _ _root_.set.sized.subset_powerset_len_univ lemma _root_.set.sized.card_le (h𝒜 : (𝒜 : set (finset α)).sized r) : card 𝒜 ≤ (fintype.card α).choose r := begin rw [fintype.card, ←card_powerset_len], exact card_le_of_subset h𝒜.subset_powerset_len_univ, end end sized /-! ### Slices -/ section slice variables {𝒜 : finset (finset α)} {A A₁ A₂ : finset α} {r r₁ r₂ : ℕ} /-- The `r`-th slice of a set family is the subset of its elements which have cardinality `r`. -/ def slice (𝒜 : finset (finset α)) (r : ℕ) : finset (finset α) := 𝒜.filter (λ i, i.card = r) localized "infix (name := finset.slice) ` # `:90 := finset.slice" in finset_family /-- `A` is in the `r`-th slice of `𝒜` iff it's in `𝒜` and has cardinality `r`. -/ lemma mem_slice : A ∈ 𝒜 # r ↔ A ∈ 𝒜 ∧ A.card = r := mem_filter /-- The `r`-th slice of `𝒜` is a subset of `𝒜`. -/ lemma slice_subset : 𝒜 # r ⊆ 𝒜 := filter_subset _ _ /-- Everything in the `r`-th slice of `𝒜` has size `r`. -/ lemma sized_slice : (𝒜 # r : set (finset α)).sized r := λ _, and.right ∘ mem_slice.mp lemma eq_of_mem_slice (h₁ : A ∈ 𝒜 # r₁) (h₂ : A ∈ 𝒜 # r₂) : r₁ = r₂ := (sized_slice h₁).symm.trans $ sized_slice h₂ /-- Elements in distinct slices must be distinct. -/ lemma ne_of_mem_slice (h₁ : A₁ ∈ 𝒜 # r₁) (h₂ : A₂ ∈ 𝒜 # r₂) : r₁ ≠ r₂ → A₁ ≠ A₂ := mt $ λ h, (sized_slice h₁).symm.trans ((congr_arg card h).trans (sized_slice h₂)) lemma pairwise_disjoint_slice [decidable_eq α] : (set.univ : set ℕ).pairwise_disjoint (slice 𝒜) := λ m _ n _ hmn, disjoint_filter.2 $ λ s hs hm hn, hmn $ hm.symm.trans hn variables [fintype α] (𝒜) @[simp] lemma bUnion_slice [decidable_eq α] : (Iic $ fintype.card α).bUnion 𝒜.slice = 𝒜 := subset.antisymm (bUnion_subset.2 $ λ r _, slice_subset) $ λ s hs, mem_bUnion.2 ⟨s.card, mem_Iic.2 $ s.card_le_univ, mem_slice.2 $ ⟨hs, rfl⟩⟩ @[simp] lemma sum_card_slice : ∑ r in Iic (fintype.card α), (𝒜 # r).card = 𝒜.card := by { rw [←card_bUnion (finset.pairwise_disjoint_slice.subset (set.subset_univ _)), bUnion_slice], exact classical.dec_eq _ } end slice end finset
b82103d2437d2f68dbb78b63abb1835d618c8277
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/bounded.lean
b3b74598b5b9ba4401b6c3d9f61fdec362aa35fb
[ "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
13,552
lean
/- Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta Hernández Palacios -/ import order.min_max import order.rel_classes import data.set.intervals.basic /-! # Bounded and unbounded sets We prove miscellaneous lemmas about bounded and unbounded sets. Many of these are just variations on the same ideas, or similar results with a few minor differences. The file is divided into these different general ideas. -/ namespace set variables {α : Type*} {r : α → α → Prop} {s t : set α} /-! ### Subsets of bounded and unbounded sets -/ theorem bounded.mono (hst : s ⊆ t) (hs : bounded r t) : bounded r s := hs.imp $ λ a ha b hb, ha b (hst hb) theorem unbounded.mono (hst : s ⊆ t) (hs : unbounded r s) : unbounded r t := λ a, let ⟨b, hb, hb'⟩ := hs a in ⟨b, hst hb, hb'⟩ /-! ### Alternate characterizations of unboundedness on orders -/ lemma unbounded_le_of_forall_exists_lt [preorder α] (h : ∀ a, ∃ b ∈ s, a < b) : unbounded (≤) s := λ a, let ⟨b, hb, hb'⟩ := h a in ⟨b, hb, λ hba, hba.not_lt hb'⟩ lemma unbounded_le_iff [linear_order α] : unbounded (≤) s ↔ ∀ a, ∃ b ∈ s, a < b := by simp only [unbounded, not_le] lemma unbounded_lt_of_forall_exists_le [preorder α] (h : ∀ a, ∃ b ∈ s, a ≤ b) : unbounded (<) s := λ a, let ⟨b, hb, hb'⟩ := h a in ⟨b, hb, λ hba, hba.not_le hb'⟩ lemma unbounded_lt_iff [linear_order α] : unbounded (<) s ↔ ∀ a, ∃ b ∈ s, a ≤ b := by simp only [unbounded, not_lt] lemma unbounded_ge_of_forall_exists_gt [preorder α] (h : ∀ a, ∃ b ∈ s, b < a) : unbounded (≥) s := @unbounded_le_of_forall_exists_lt (order_dual α) _ _ h lemma unbounded_ge_iff [linear_order α] : unbounded (≥) s ↔ ∀ a, ∃ b ∈ s, b < a := ⟨λ h a, let ⟨b, hb, hba⟩ := h a in ⟨b, hb, lt_of_not_ge hba⟩, unbounded_ge_of_forall_exists_gt⟩ lemma unbounded_gt_of_forall_exists_ge [preorder α] (h : ∀ a, ∃ b ∈ s, b ≤ a) : unbounded (>) s := λ a, let ⟨b, hb, hb'⟩ := h a in ⟨b, hb, λ hba, not_le_of_gt hba hb'⟩ lemma unbounded_gt_iff [linear_order α] : unbounded (>) s ↔ ∀ a, ∃ b ∈ s, b ≤ a := ⟨λ h a, let ⟨b, hb, hba⟩ := h a in ⟨b, hb, le_of_not_gt hba⟩, unbounded_gt_of_forall_exists_ge⟩ /-! ### Relation between boundedness by strict and nonstrict orders. -/ /-! #### Less and less or equal -/ lemma bounded.rel_mono {r' : α → α → Prop} (h : bounded r s) (hrr' : r ≤ r') : bounded r' s := let ⟨a, ha⟩ := h in ⟨a, λ b hb, hrr' b a (ha b hb)⟩ lemma bounded_le_of_bounded_lt [preorder α] (h : bounded (<) s) : bounded (≤) s := h.rel_mono $ λ _ _, le_of_lt lemma unbounded.rel_mono {r' : α → α → Prop} (hr : r' ≤ r) (h : unbounded r s) : unbounded r' s := λ a, let ⟨b, hb, hba⟩ := h a in ⟨b, hb, λ hba', hba (hr b a hba')⟩ lemma unbounded_lt_of_unbounded_le [preorder α] (h : unbounded (≤) s) : unbounded (<) s := h.rel_mono $ λ _ _, le_of_lt lemma bounded_le_iff_bounded_lt [preorder α] [no_max_order α] : bounded (≤) s ↔ bounded (<) s := begin refine ⟨λ h, _, bounded_le_of_bounded_lt⟩, cases h with a ha, cases exists_gt a with b hb, exact ⟨b, λ c hc, lt_of_le_of_lt (ha c hc) hb⟩ end lemma unbounded_lt_iff_unbounded_le [preorder α] [no_max_order α] : unbounded (<) s ↔ unbounded (≤) s := by simp_rw [← not_bounded_iff, bounded_le_iff_bounded_lt] /-! #### Greater and greater or equal -/ lemma bounded_ge_of_bounded_gt [preorder α] (h : bounded (>) s) : bounded (≥) s := let ⟨a, ha⟩ := h in ⟨a, λ b hb, le_of_lt (ha b hb)⟩ lemma unbounded_gt_of_unbounded_ge [preorder α] (h : unbounded (≥) s) : unbounded (>) s := λ a, let ⟨b, hb, hba⟩ := h a in ⟨b, hb, λ hba', hba (le_of_lt hba')⟩ lemma bounded_ge_iff_bounded_gt [preorder α] [no_min_order α] : bounded (≥) s ↔ bounded (>) s := @bounded_le_iff_bounded_lt (order_dual α) _ _ _ lemma unbounded_gt_iff_unbounded_ge [preorder α] [no_min_order α] : unbounded (>) s ↔ unbounded (≥) s := @unbounded_lt_iff_unbounded_le (order_dual α) _ _ _ /-! ### Bounded and unbounded intervals -/ theorem bounded_self (a : α) : bounded r {b | r b a} := ⟨a, λ x, id⟩ /-! #### Half-open bounded intervals -/ theorem bounded_lt_Iio [preorder α] (a : α) : bounded (<) (set.Iio a) := bounded_self a theorem bounded_le_Iio [preorder α] (a : α) : bounded (≤) (set.Iio a) := bounded_le_of_bounded_lt (bounded_lt_Iio a) theorem bounded_le_Iic [preorder α] (a : α) : bounded (≤) (set.Iic a) := bounded_self a theorem bounded_lt_Iic [preorder α] [no_max_order α] (a : α) : bounded (<) (set.Iic a) := by simp only [← bounded_le_iff_bounded_lt, bounded_le_Iic] theorem bounded_gt_Ioi [preorder α] (a : α) : bounded (>) (set.Ioi a) := bounded_self a theorem bounded_ge_Ioi [preorder α] (a : α) : bounded (≥) (set.Ioi a) := bounded_ge_of_bounded_gt (bounded_gt_Ioi a) theorem bounded_ge_Ici [preorder α] (a : α) : bounded (≥) (set.Ici a) := bounded_self a theorem bounded_gt_Ici [preorder α] [no_min_order α] (a : α) : bounded (>) (set.Ici a) := by simp only [← bounded_ge_iff_bounded_gt, bounded_ge_Ici] /-! #### Other bounded intervals -/ theorem bounded_lt_Ioo [preorder α] (a b : α) : bounded (<) (set.Ioo a b) := (bounded_lt_Iio b).mono set.Ioo_subset_Iio_self theorem bounded_lt_Ico [preorder α] (a b : α) : bounded (<) (set.Ico a b) := (bounded_lt_Iio b).mono set.Ico_subset_Iio_self theorem bounded_lt_Ioc [preorder α] [no_max_order α] (a b : α) : bounded (<) (set.Ioc a b) := (bounded_lt_Iic b).mono set.Ioc_subset_Iic_self theorem bounded_lt_Icc [preorder α] [no_max_order α] (a b : α) : bounded (<) (set.Icc a b) := (bounded_lt_Iic b).mono set.Icc_subset_Iic_self theorem bounded_le_Ioo [preorder α] (a b : α) : bounded (≤) (set.Ioo a b) := (bounded_le_Iio b).mono set.Ioo_subset_Iio_self theorem bounded_le_Ico [preorder α] (a b : α) : bounded (≤) (set.Ico a b) := (bounded_le_Iio b).mono set.Ico_subset_Iio_self theorem bounded_le_Ioc [preorder α] (a b : α) : bounded (≤) (set.Ioc a b) := (bounded_le_Iic b).mono set.Ioc_subset_Iic_self theorem bounded_le_Icc [preorder α] (a b : α) : bounded (≤) (set.Icc a b) := (bounded_le_Iic b).mono set.Icc_subset_Iic_self theorem bounded_gt_Ioo [preorder α] (a b : α) : bounded (>) (set.Ioo a b) := (bounded_gt_Ioi a).mono set.Ioo_subset_Ioi_self theorem bounded_gt_Ioc [preorder α] (a b : α) : bounded (>) (set.Ioc a b) := (bounded_gt_Ioi a).mono set.Ioc_subset_Ioi_self theorem bounded_gt_Ico [preorder α] [no_min_order α] (a b : α) : bounded (>) (set.Ico a b) := (bounded_gt_Ici a).mono set.Ico_subset_Ici_self theorem bounded_gt_Icc [preorder α] [no_min_order α] (a b : α) : bounded (>) (set.Icc a b) := (bounded_gt_Ici a).mono set.Icc_subset_Ici_self theorem bounded_ge_Ioo [preorder α] (a b : α) : bounded (≥) (set.Ioo a b) := (bounded_ge_Ioi a).mono set.Ioo_subset_Ioi_self theorem bounded_ge_Ioc [preorder α] (a b : α) : bounded (≥) (set.Ioc a b) := (bounded_ge_Ioi a).mono set.Ioc_subset_Ioi_self theorem bounded_ge_Ico [preorder α] (a b : α) : bounded (≥) (set.Ico a b) := (bounded_ge_Ici a).mono set.Ico_subset_Ici_self theorem bounded_ge_Icc [preorder α] (a b : α) : bounded (≥) (set.Icc a b) := (bounded_ge_Ici a).mono set.Icc_subset_Ici_self /-! #### Unbounded intervals -/ theorem unbounded_le_Ioi [semilattice_sup α] [no_max_order α] (a : α) : unbounded (≤) (set.Ioi a) := λ b, let ⟨c, hc⟩ := exists_gt (a ⊔ b) in ⟨c, le_sup_left.trans_lt hc, (le_sup_right.trans_lt hc).not_le⟩ theorem unbounded_le_Ici [semilattice_sup α] [no_max_order α] (a : α) : unbounded (≤) (set.Ici a) := (unbounded_le_Ioi a).mono set.Ioi_subset_Ici_self theorem unbounded_lt_Ioi [semilattice_sup α] [no_max_order α] (a : α) : unbounded (<) (set.Ioi a) := unbounded_lt_of_unbounded_le (unbounded_le_Ioi a) theorem unbounded_lt_Ici [semilattice_sup α] (a : α) : unbounded (<) (set.Ici a) := λ b, ⟨a ⊔ b, le_sup_left, le_sup_right.not_lt⟩ /-! ### Bounded initial segments -/ theorem bounded_inter_not (H : ∀ a b, ∃ m, ∀ c, r c a ∨ r c b → r c m) (a : α) : bounded r (s ∩ {b | ¬ r b a}) ↔ bounded r s := begin refine ⟨_, bounded.mono (set.inter_subset_left s _)⟩, rintro ⟨b, hb⟩, cases H a b with m hm, exact ⟨m, λ c hc, hm c (or_iff_not_imp_left.2 (λ hca, (hb c ⟨hc, hca⟩)))⟩ end theorem unbounded_inter_not (H : ∀ a b, ∃ m, ∀ c, r c a ∨ r c b → r c m) (a : α) : unbounded r (s ∩ {b | ¬ r b a}) ↔ unbounded r s := by simp_rw [← not_bounded_iff, bounded_inter_not H] /-! #### Less or equal -/ theorem bounded_le_inter_not_le [semilattice_sup α] (a : α) : bounded (≤) (s ∩ {b | ¬ b ≤ a}) ↔ bounded (≤) s := bounded_inter_not (λ x y, ⟨x ⊔ y, λ z h, h.elim le_sup_of_le_left le_sup_of_le_right⟩) a theorem unbounded_le_inter_not_le [semilattice_sup α] (a : α) : unbounded (≤) (s ∩ {b | ¬ b ≤ a}) ↔ unbounded (≤) s := begin rw [←not_bounded_iff, ←not_bounded_iff, not_iff_not], exact bounded_le_inter_not_le a end theorem bounded_le_inter_lt [linear_order α] (a : α) : bounded (≤) (s ∩ {b | a < b}) ↔ bounded (≤) s := by simp_rw [← not_le, bounded_le_inter_not_le] theorem unbounded_le_inter_lt [linear_order α] (a : α) : unbounded (≤) (s ∩ {b | a < b}) ↔ unbounded (≤) s := by { convert unbounded_le_inter_not_le a, ext, exact lt_iff_not_ge' } theorem bounded_le_inter_le [linear_order α] (a : α) : bounded (≤) (s ∩ {b | a ≤ b}) ↔ bounded (≤) s := begin refine ⟨_, bounded.mono (set.inter_subset_left s _)⟩, rw ←@bounded_le_inter_lt _ s _ a, exact bounded.mono (λ x ⟨hx, hx'⟩, ⟨hx, le_of_lt hx'⟩) end theorem unbounded_le_inter_le [linear_order α] (a : α) : unbounded (≤) (s ∩ {b | a ≤ b}) ↔ unbounded (≤) s := begin rw [←not_bounded_iff, ←not_bounded_iff, not_iff_not], exact bounded_le_inter_le a end /-! #### Less than -/ theorem bounded_lt_inter_not_lt [semilattice_sup α] (a : α) : bounded (<) (s ∩ {b | ¬ b < a}) ↔ bounded (<) s := bounded_inter_not (λ x y, ⟨x ⊔ y, λ z h, h.elim lt_sup_of_lt_left lt_sup_of_lt_right⟩) a theorem unbounded_lt_inter_not_lt [semilattice_sup α] (a : α) : unbounded (<) (s ∩ {b | ¬ b < a}) ↔ unbounded (<) s := begin rw [←not_bounded_iff, ←not_bounded_iff, not_iff_not], exact bounded_lt_inter_not_lt a end theorem bounded_lt_inter_le [linear_order α] (a : α) : bounded (<) (s ∩ {b | a ≤ b}) ↔ bounded (<) s := by { convert bounded_lt_inter_not_lt a, ext, exact not_lt.symm } theorem unbounded_lt_inter_le [linear_order α] (a : α) : unbounded (<) (s ∩ {b | a ≤ b}) ↔ unbounded (<) s := by { convert unbounded_lt_inter_not_lt a, ext, exact not_lt.symm } theorem bounded_lt_inter_lt [linear_order α] [no_max_order α] (a : α) : bounded (<) (s ∩ {b | a < b}) ↔ bounded (<) s := begin rw [←bounded_le_iff_bounded_lt, ←bounded_le_iff_bounded_lt], exact bounded_le_inter_lt a end theorem unbounded_lt_inter_lt [linear_order α] [no_max_order α] (a : α) : unbounded (<) (s ∩ {b | a < b}) ↔ unbounded (<) s := begin rw [←not_bounded_iff, ←not_bounded_iff, not_iff_not], exact bounded_lt_inter_lt a end /-! #### Greater or equal -/ theorem bounded_ge_inter_not_ge [semilattice_inf α] (a : α) : bounded (≥) (s ∩ {b | ¬ a ≤ b}) ↔ bounded (≥) s := @bounded_le_inter_not_le (order_dual α) s _ a theorem unbounded_ge_inter_not_ge [semilattice_inf α] (a : α) : unbounded (≥) (s ∩ {b | ¬ a ≤ b}) ↔ unbounded (≥) s := @unbounded_le_inter_not_le (order_dual α) s _ a theorem bounded_ge_inter_gt [linear_order α] (a : α) : bounded (≥) (s ∩ {b | b < a}) ↔ bounded (≥) s := @bounded_le_inter_lt (order_dual α) s _ a theorem unbounded_ge_inter_gt [linear_order α] (a : α) : unbounded (≥) (s ∩ {b | b < a}) ↔ unbounded (≥) s := @unbounded_le_inter_lt (order_dual α) s _ a theorem bounded_ge_inter_ge [linear_order α] (a : α) : bounded (≥) (s ∩ {b | b ≤ a}) ↔ bounded (≥) s := @bounded_le_inter_le (order_dual α) s _ a theorem unbounded_ge_iff_unbounded_inter_ge [linear_order α] (a : α) : unbounded (≥) (s ∩ {b | b ≤ a}) ↔ unbounded (≥) s := @unbounded_le_inter_le (order_dual α) s _ a /-! #### Greater than -/ theorem bounded_gt_inter_not_gt [semilattice_inf α] (a : α) : bounded (>) (s ∩ {b | ¬ a < b}) ↔ bounded (>) s := @bounded_lt_inter_not_lt (order_dual α) s _ a theorem unbounded_gt_inter_not_gt [semilattice_inf α] (a : α) : unbounded (>) (s ∩ {b | ¬ a < b}) ↔ unbounded (>) s := @unbounded_lt_inter_not_lt (order_dual α) s _ a theorem bounded_gt_inter_ge [linear_order α] (a : α) : bounded (>) (s ∩ {b | b ≤ a}) ↔ bounded (>) s := @bounded_lt_inter_le (order_dual α) s _ a theorem unbounded_inter_ge [linear_order α] (a : α) : unbounded (>) (s ∩ {b | b ≤ a}) ↔ unbounded (>) s := @unbounded_lt_inter_le (order_dual α) s _ a theorem bounded_gt_inter_gt [linear_order α] [no_min_order α] (a : α) : bounded (>) (s ∩ {b | b < a}) ↔ bounded (>) s := @bounded_lt_inter_lt (order_dual α) s _ _ a theorem unbounded_gt_inter_gt [linear_order α] [no_min_order α] (a : α) : unbounded (>) (s ∩ {b | b < a}) ↔ unbounded (>) s := @unbounded_lt_inter_lt (order_dual α) s _ _ a end set
0212b905ed1c96d6a668d844888d0c6d586f7e39
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Std/Data/RBTree.lean
042668213e5cfde8674ce3a3028f50e09c60c456
[ "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
3,658
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Std.Data.RBMap namespace Std universe u v w def RBTree (α : Type u) (cmp : α → α → Ordering) : Type u := RBMap α Unit cmp instance : Inhabited (RBTree α p) where default := RBMap.empty @[inline] def mkRBTree (α : Type u) (cmp : α → α → Ordering) : RBTree α cmp := mkRBMap α Unit cmp instance (α : Type u) (cmp : α → α → Ordering) : EmptyCollection (RBTree α cmp) := ⟨mkRBTree α cmp⟩ namespace RBTree variable {α : Type u} {β : Type v} {cmp : α → α → Ordering} @[inline] def empty : RBTree α cmp := RBMap.empty @[inline] def depth (f : Nat → Nat → Nat) (t : RBTree α cmp) : Nat := RBMap.depth f t @[inline] def fold (f : β → α → β) (init : β) (t : RBTree α cmp) : β := RBMap.fold (fun r a _ => f r a) init t @[inline] def revFold (f : β → α → β) (init : β) (t : RBTree α cmp) : β := RBMap.revFold (fun r a _ => f r a) init t @[inline] def foldM {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (t : RBTree α cmp) : m β := RBMap.foldM (fun r a _ => f r a) init t @[inline] def forM {m : Type v → Type w} [Monad m] (f : α → m PUnit) (t : RBTree α cmp) : m PUnit := t.foldM (fun _ a => f a) ⟨⟩ @[inline] protected def forIn [Monad m] (t : RBTree α cmp) (init : σ) (f : α → σ → m (ForInStep σ)) : m σ := t.val.forIn init (fun a _ acc => f a acc) instance : ForIn m (RBTree α cmp) α where forIn := RBTree.forIn @[inline] def isEmpty (t : RBTree α cmp) : Bool := RBMap.isEmpty t @[specialize] def toList (t : RBTree α cmp) : List α := t.revFold (fun as a => a::as) [] @[specialize] def toArray (t : RBTree α cmp) : Array α := t.fold (fun as a => as.push a) #[] @[inline] protected def min (t : RBTree α cmp) : Option α := match RBMap.min t with | some ⟨a, _⟩ => some a | none => none @[inline] protected def max (t : RBTree α cmp) : Option α := match RBMap.max t with | some ⟨a, _⟩ => some a | none => none instance [Repr α] : Repr (RBTree α cmp) where reprPrec t prec := Repr.addAppParen ("Std.rbtreeOf " ++ repr t.toList) prec @[inline] def insert (t : RBTree α cmp) (a : α) : RBTree α cmp := RBMap.insert t a () @[inline] def erase (t : RBTree α cmp) (a : α) : RBTree α cmp := RBMap.erase t a @[specialize] def ofList : List α → RBTree α cmp | [] => mkRBTree .. | x::xs => (ofList xs).insert x @[inline] def find? (t : RBTree α cmp) (a : α) : Option α := match RBMap.findCore? t a with | some ⟨a, _⟩ => some a | none => none @[inline] def contains (t : RBTree α cmp) (a : α) : Bool := (t.find? a).isSome def fromList (l : List α) (cmp : α → α → Ordering) : RBTree α cmp := l.foldl insert (mkRBTree α cmp) @[inline] def all (t : RBTree α cmp) (p : α → Bool) : Bool := RBMap.all t (fun a _ => p a) @[inline] def any (t : RBTree α cmp) (p : α → Bool) : Bool := RBMap.any t (fun a _ => p a) def subset (t₁ t₂ : RBTree α cmp) : Bool := t₁.all fun a => (t₂.find? a).toBool def seteq (t₁ t₂ : RBTree α cmp) : Bool := subset t₁ t₂ && subset t₂ t₁ def union (t₁ t₂ : RBTree α cmp) : RBTree α cmp := if t₁.isEmpty then t₂ else t₂.fold .insert t₁ def diff (t₁ t₂ : RBTree α cmp) : RBTree α cmp := t₂.fold .erase t₁ end RBTree def rbtreeOf {α : Type u} (l : List α) (cmp : α → α → Ordering) : RBTree α cmp := RBTree.fromList l cmp end Std
0fa57b889ffd350c9229a0a36900da7e230f12ce
3dd1b66af77106badae6edb1c4dea91a146ead30
/library/standard/sum.lean
b61e2f4ac0e0d281a9f47e1fa52ff472188377fd
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
429
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura import logic namespace sum inductive sum (A : Type) (B : Type) : Type := | inl : A → sum A B | inr : B → sum A B infixr `+`:25 := sum theorem induction_on {A : Type} {B : Type} {C : Prop} (s : A + B) (H1 : A → C) (H2 : B → C) : C := sum_rec H1 H2 s end
756ae71bfae035f130bc055c7e6bf9dc85952c58
e07b1aca72e83a272dd59d24c6e0fa246034d774
/src/surreal/cardinal/basic.lean
f2c1520470c08371fe4dcfcecbc9947b9633cf8f
[]
no_license
pedrominicz/learn
637a343bd4f8669d76819ac660a2d2d3e0958710
b79b802a9846c86c21d4b6f3e17af36e7382f0ef
refs/heads/master
1,671,746,990,402
1,670,778,113,000
1,670,778,113,000
265,735,177
1
0
null
null
null
null
UTF-8
Lean
false
false
56,599
lean
import data.nat.enat data.set.countable import logic.small import order.conditionally_complete_lattice order.succ_pred.basic import set_theory.cardinal.schroeder_bernstein universes u v w variables {α β : Type u} instance cardinal.is_equivalent : setoid (Type u) := { r := λ α β, nonempty (α ≃ β), iseqv := ⟨ λ α, ⟨equiv.refl α⟩, λ α β ⟨e⟩, ⟨e.symm⟩, λ α β γ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ } def cardinal : Type (u+1) := quotient cardinal.is_equivalent namespace cardinal def mk (α : Type u) : cardinal := quotient.mk α prefix `#` := cardinal.mk instance can_lift_cardinal_Type : can_lift cardinal.{u} (Type u) := ⟨mk, λ c, true, λ c h, quotient.induction_on c (λ α, ⟨α, rfl⟩)⟩ @[elab_as_eliminator] lemma induction_on {p : cardinal → Prop} (c : cardinal) : (∀ α, p #α) → p c := λ h, quotient.induction_on c h @[elab_as_eliminator] lemma induction_on₂ {p : cardinal → cardinal → Prop} (c₁ : cardinal.{u}) (c₂ : cardinal.{v}) : (∀ α β, p #α #β) → p c₁ c₂ := λ h, quotient.induction_on₂ c₁ c₂ h @[elab_as_eliminator] lemma induction_on₃ {p : cardinal → cardinal → cardinal → Prop} (c₁ : cardinal.{u}) (c₂ : cardinal.{v}) (c₃ : cardinal.{w}) : (∀ α β γ, p #α #β #γ) → p c₁ c₂ c₃ := λ h, quotient.induction_on₃ c₁ c₂ c₃ h protected lemma eq : #α = #β ↔ nonempty (α ≃ β) := quotient.eq @[simp] theorem mk_def (α : Type u) : @eq cardinal ⟦α⟧ #α := rfl @[simp] theorem mk_out (c : cardinal) : #c.out = c := quotient.out_eq c noncomputable def out_mk_equiv : (#α).out ≃ α := nonempty.some (cardinal.eq.mp (mk_out #α)) lemma mk_congr (e : α ≃ β) : #α = #β := quotient.sound ⟨e⟩ alias mk_congr ← _root_.equiv.cardinal_eq def map (f : Type u → Type v) (hf : ∀ α β, α ≃ β → f α ≃ f β) : cardinal.{u} → cardinal.{v} := quotient.map f (λ α β ⟨e⟩, ⟨hf α β e⟩) @[simp] lemma map_mk (f : Type u → Type v) (hf : ∀ α β, α ≃ β → f α ≃ f β) (α : Type u) : map f hf #α = #(f α) := rfl def map₂ (f : Type u → Type v → Type w) (hf : ∀ α₁ β₁ α₂ β₂, α₁ ≃ β₁ → α₂ ≃ β₂ → f α₁ α₂ ≃ f β₁ β₂) : cardinal.{u} → cardinal.{v} → cardinal.{w} := quotient.map₂ f (λ α β ⟨e₁⟩ γ δ ⟨e₂⟩, ⟨hf α β γ δ e₁ e₂⟩) def lift (c : cardinal.{v}) : cardinal.{max v u} := map ulift (λ α β e, equiv.ulift.trans (e.trans equiv.ulift.symm)) c @[simp] theorem mk_ulift (α : Type u) : #(ulift α) = lift #α := rfl @[simp] theorem lift_umax : lift.{(max u v) u} = lift.{v u} := funext (λ c, induction_on c (λ α, mk_congr (equiv.ulift.trans equiv.ulift.symm))) @[simp] theorem lift_umax' : lift.{(max v u) u} = lift.{v u} := lift_umax @[simp] theorem lift_id' (c : cardinal.{max u v}) : lift.{u} c = c := induction_on c (λ α, mk_congr equiv.ulift) @[simp] theorem lift_id (c : cardinal.{u}) : lift.{u u} c = c := lift_id'.{u u} c @[simp] theorem lift_uzero (c : cardinal.{u}) : lift.{0} c = c := lift_id'.{0 u} c @[simp] theorem lift_lift (c : cardinal.{u}) : lift.{w} (lift.{v} c) = lift.{max v w} c := induction_on c (λ α, mk_congr (equiv.ulift.trans (equiv.ulift.trans equiv.ulift.symm))) instance : has_le cardinal.{u} := ⟨λ c₁ c₂, quotient.lift_on₂ c₁ c₂ (λ α β, nonempty (α ↪ β)) (λ α₁ β₁ α₂ β₂ ⟨e₁⟩ ⟨e₂⟩, propext ⟨λ ⟨e⟩, ⟨e.congr e₁ e₂⟩, λ ⟨e⟩, ⟨e.congr e₁.symm e₂.symm⟩⟩)⟩ instance : partial_order cardinal.{u} := { le := (≤), le_refl := by { rintros ⟨α⟩, exact ⟨function.embedding.refl α⟩ }, le_trans := by { rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨e₁⟩ ⟨e₂⟩, exact ⟨e₁.trans e₂⟩ }, le_antisymm := by { rintros ⟨α⟩ ⟨β⟩ ⟨e₁⟩ ⟨e₂⟩, exact quotient.sound (e₁.antisymm e₂) } } theorem le_def (α β : Type u) : #α ≤ #β ↔ nonempty (α ↪ β) := iff.rfl theorem mk_le_of_injective {f : α → β} (hf : function.injective f) : #α ≤ #β := ⟨⟨f, hf⟩⟩ theorem _root_.function.embedding.cardinal_le (f : α ↪ β) : #α ≤ #β := ⟨f⟩ theorem mk_le_of_surjective {f : α → β} (hf : function.surjective f) : #β ≤ #α := ⟨function.embedding.of_surjective f hf⟩ theorem le_mk_iff_exists_set {c : cardinal} {α : Type u} : c ≤ #α ↔ ∃ s : set α, #s = c := ⟨induction_on c (λ β ⟨⟨f, hf⟩⟩, ⟨set.range f, mk_congr (equiv.of_injective f hf).symm⟩), λ ⟨s, hs⟩, hs ▸ ⟨⟨subtype.val, λ a₁ a₂, subtype.eq⟩⟩⟩ theorem mk_subtype_le {α : Type u} (p : α → Prop) : #(subtype p) ≤ #α := ⟨function.embedding.subtype p⟩ theorem mk_set_le (s : set α) : #s ≤ #α := mk_subtype_le s theorem out_embedding {c₁ c₂ : cardinal} : c₁ ≤ c₂ ↔ nonempty (c₁.out ↪ c₂.out) := begin transitivity #c₁.out ≤ #c₂.out, { conv { to_lhs, rw [←mk_out c₁, ←mk_out c₂] } }, { refl } end theorem lift_mk_le {α : Type u} {β : Type v} : lift.{max v w} #α ≤ lift.{max u w} #β ↔ nonempty (α ↪ β) := ⟨λ ⟨f⟩, ⟨function.embedding.congr equiv.ulift equiv.ulift f⟩, λ ⟨f⟩, ⟨function.embedding.congr equiv.ulift.symm equiv.ulift.symm f⟩⟩ theorem lift_mk_le' {α : Type u} {β : Type v} : lift.{v} #α ≤ lift.{u} #β ↔ nonempty (α ↪ β) := lift_mk_le.{u v 0} theorem lift_mk_eq {α : Type u} {β : Type v} : lift.{max v w} #α = lift.{max u w} #β ↔ nonempty (α ≃ β) := quotient.eq.trans ⟨λ ⟨f⟩, ⟨equiv.ulift.symm.trans (f.trans equiv.ulift)⟩, λ ⟨f⟩, ⟨equiv.ulift.trans (f.trans equiv.ulift.symm)⟩⟩ theorem lift_mk_eq' {α : Type u} {β : Type v} : lift.{v} #α = lift.{u} #β ↔ nonempty (α ≃ β) := lift_mk_eq.{u v 0} @[simp] theorem lift_le {c₁ c₂ : cardinal} : lift c₁ ≤ lift c₂ ↔ c₁ ≤ c₂ := induction_on₂ c₁ c₂ (λ α β, lift_umax ▸ lift_mk_le) @[simps { fully_applied := ff }] def lift_order_embedding : cardinal.{v} ↪o cardinal.{max v u} := order_embedding.of_map_le_iff lift (λ c₁ c₂, lift_le) theorem lift_injective : function.injective lift.{u v} := lift_order_embedding.injective @[simp] theorem lift_inj {c₁ c₂ : cardinal} : lift c₁ = lift c₂ ↔ c₁ = c₂ := lift_injective.eq_iff @[simp] theorem lift_lt {c₁ c₂ : cardinal} : lift c₁ < lift c₂ ↔ c₁ < c₂ := lift_order_embedding.lt_iff_lt theorem lift_strict_mono : strict_mono lift := λ c₁ c₂, lift_lt.mpr theorem lift_monotone : monotone lift := lift_strict_mono.monotone instance : has_zero cardinal := ⟨#pempty⟩ instance : inhabited cardinal := ⟨0⟩ lemma mk_eq_zero (α : Type u) [is_empty α] : #α = 0 := mk_congr (equiv.equiv_pempty α) @[simp] theorem lift_zero : lift 0 = 0 := mk_congr (equiv.equiv_pempty (ulift pempty)) @[simp] theorem lift_eq_zero {c : cardinal} : lift c = 0 ↔ c = 0 := lift_injective.eq_iff' lift_zero lemma mk_eq_zero_iff {α : Type u} : #α = 0 ↔ is_empty α := ⟨λ h, let ⟨e⟩ := quotient.exact h in equiv.is_empty e, @mk_eq_zero α⟩ theorem mk_ne_zero_iff {α : Type u} : #α ≠ 0 ↔ nonempty α := (not_iff_not.mpr mk_eq_zero_iff).trans not_is_empty_iff @[simp] lemma mk_ne_zero (α : Type u) [nonempty α] : #α ≠ 0 := mk_ne_zero_iff.mpr infer_instance instance : has_one cardinal := ⟨#punit⟩ instance : nontrivial cardinal := ⟨⟨1, 0, mk_ne_zero punit⟩⟩ lemma mk_eq_one (α : Type u) [unique α] : #α = 1 := mk_congr (equiv.equiv_punit α) theorem le_one_iff_subsingleton {α : Type u} : #α ≤ 1 ↔ subsingleton α := ⟨λ ⟨f⟩, ⟨λ a₁ a₂, f.injective (subsingleton.elim (f a₁) (f a₂))⟩, λ ⟨h⟩, ⟨⟨λ a, punit.star, λ a₁ a₂ h', h a₁ a₂⟩⟩⟩ instance : has_add cardinal := ⟨map₂ sum (λ α₁ β₁ α₂ β₂, equiv.sum_congr)⟩ theorem add_def (α β : Type u) : #α + #β = #(α ⊕ β) := rfl instance : has_nat_cast cardinal := ⟨nat.unary_cast⟩ @[simp] lemma mk_sum (α : Type u) (β : Type v) : #(α ⊕ β) = lift #α + lift.{u} #β := mk_congr (equiv.ulift.symm.sum_congr equiv.ulift.symm) @[simp] theorem mk_option {α : Type u} : #(option α) = #α + 1 := mk_congr (equiv.option_equiv_sum_punit α) @[simp] lemma mk_psum (α : Type u) (β : Type v) : #(psum α β) = lift #α + lift.{u} #β := (mk_congr (equiv.psum_equiv_sum α β)).trans (mk_sum α β) @[simp] lemma mk_fintype (α : Type u) [hα : fintype α] : #α = fintype.card α := begin refine fintype.induction_empty_option' _ _ _ α, { intros α β hβ e h, let hα := @fintype.of_equiv α β hβ e.symm, rwa [mk_congr e, @fintype.card_congr α β hα hβ e] at h }, { refl }, { intros α hα h, simp only [h, mk_option, @fintype.card_option α hα], refl } end instance : has_mul cardinal := ⟨map₂ prod (λ α₁ β₁ α₂ β₂, equiv.prod_congr)⟩ theorem mul_def (α β : Type u) : #α * #β = #(α × β) := rfl @[simp] lemma mk_prod (α : Type u) (β : Type v) : #(α × β) = lift #α * lift.{u} #β := mk_congr (equiv.ulift.symm.prod_congr equiv.ulift.symm) private theorem mul_comm' (c₁ c₂ : cardinal.{u}) : c₁ * c₂ = c₂ * c₁ := induction_on₂ c₁ c₂ (λ α β, mk_congr (equiv.prod_comm α β)) instance : has_pow cardinal.{u} cardinal.{u} := ⟨map₂ (λ α β, β → α) (λ α₁ β₁ α₂ β₂, flip equiv.arrow_congr)⟩ local infixr ` ^ ` := @has_pow.pow cardinal cardinal cardinal.has_pow local infixr ` ^ℕ `:80 := @has_pow.pow cardinal ℕ monoid.has_pow theorem power_def (α β : Type u) : #α ^ #β = #(β → α) := rfl theorem mk_arrow (α : Type u) (β : Type v) : #(α → β) = lift.{u} #β ^ lift.{v} #α := mk_congr (equiv.ulift.symm.arrow_congr equiv.ulift.symm) @[simp] theorem lift_power (c₁ c₂ : cardinal) : lift (c₁ ^ c₂) = lift c₁ ^ lift c₂ := induction_on₂ c₁ c₂ (λ α β, mk_congr (equiv.ulift.trans (equiv.ulift.arrow_congr equiv.ulift).symm)) @[simp] theorem power_zero {c : cardinal} : c ^ 0 = 1 := induction_on c (λ α, mk_congr (equiv.pempty_arrow_equiv_punit α)) @[simp] theorem power_one {c : cardinal} : c ^ 1 = c := induction_on c (λ α, mk_congr (equiv.punit_arrow_equiv α)) theorem power_add {c₁ c₂ c₃ : cardinal} : c₁ ^ (c₂ + c₃) = c₁ ^ c₂ * c₁ ^ c₃ := induction_on₃ c₁ c₂ c₃ (λ α β γ, mk_congr (equiv.sum_arrow_equiv_prod_arrow β γ α)) instance : comm_semiring cardinal.{u} := { zero := 0, one := 1, add := (+), mul := (*), zero_add := λ c, induction_on c (λ α, mk_congr (equiv.empty_sum pempty α)), add_zero := λ c, induction_on c (λ α, mk_congr (equiv.sum_empty α pempty)), add_assoc := λ c₁ c₂ c₃, induction_on₃ c₁ c₂ c₃ (λ α β γ, mk_congr (equiv.sum_assoc α β γ)), add_comm := λ c₁ c₂, induction_on₂ c₁ c₂ (λ α β, mk_congr (equiv.sum_comm α β)), zero_mul := λ c, induction_on c (λ α, mk_congr (equiv.pempty_prod α)), mul_zero := λ c, induction_on c (λ α, mk_congr (equiv.prod_pempty α)), one_mul := λ c, induction_on c (λ α, mk_congr (equiv.punit_prod α)), mul_one := λ c, induction_on c (λ α, mk_congr (equiv.prod_punit α)), mul_assoc := λ c₁ c₂ c₃, induction_on₃ c₁ c₂ c₃ (λ α β γ, mk_congr (equiv.prod_assoc α β γ)), mul_comm := mul_comm', left_distrib := λ c₁ c₂ c₃, induction_on₃ c₁ c₂ c₃ (λ α β γ, mk_congr (equiv.prod_sum_distrib α β γ)), right_distrib := λ c₁ c₂ c₃, induction_on₃ c₁ c₂ c₃ (λ α β γ, mk_congr (equiv.sum_prod_distrib α β γ)), npow := λ n c, c ^ n, npow_zero' := @power_zero, npow_succ' := λ n c, show c ^ (n + 1) = c * c ^ n, by rw [power_add, power_one, mul_comm'] } theorem power_bit0 (c₁ c₂ : cardinal) : c₁ ^ (bit0 c₂) = c₁ ^ c₂ * c₁ ^ c₂ := power_add theorem power_bit1 (c₁ c₂ : cardinal) : c₁ ^ (bit1 c₂) = c₁ ^ c₂ * c₁ ^ c₂ * c₁ := by rw [bit1, ←power_bit0, power_add, power_one] @[simp] theorem one_power {c : cardinal} : 1 ^ c = 1 := induction_on c (λ α, mk_congr (equiv.arrow_punit_equiv_punit α)) @[simp] theorem mk_bool : #bool = 2 := by simp @[simp] theorem mk_Prop : #Prop = 2 := by simp @[simp] theorem zero_power {c : cardinal} : c ≠ 0 → 0 ^ c = 0 := induction_on c (λ α h, mk_eq_zero_iff.mpr (is_empty_pi.mpr ⟨classical.choice (mk_ne_zero_iff.mp h), pempty.is_empty⟩)) theorem power_ne_zero {c₁ : cardinal.{u}} (c₂ : cardinal.{u}) : c₁ ≠ 0 → c₁ ^ c₂ ≠ 0 := induction_on₂ c₁ c₂ (λ α β h, mk_ne_zero_iff.mpr ⟨λ b, classical.choice (mk_ne_zero_iff.mp h)⟩) theorem mul_power {c₁ c₂ c₃ : cardinal} : (c₁ * c₂) ^ c₃ = c₁ ^ c₃ * c₂ ^ c₃ := induction_on₃ c₁ c₂ c₃ (λ α β γ, mk_congr (equiv.arrow_prod_equiv_prod_arrow α β γ)) theorem power_mul {c₁ c₂ c₃ : cardinal} : c₁ ^ (c₂ * c₃) = (c₁ ^ c₂) ^ c₃ := begin rw [mul_comm c₂ c₃], exact induction_on₃ c₁ c₂ c₃ (λ α β γ, mk_congr (equiv.curry γ β α)) end @[simp] lemma pow_cast_right (c : cardinal) (n : ℕ) : c ^ n = c ^ℕ n := rfl @[simp] theorem lift_one : lift 1 = 1 := mk_congr (equiv.ulift.trans equiv.punit_equiv_punit) @[simp] theorem lift_add (c₁ c₂) : lift (c₁ + c₂) = lift c₁ + lift c₂ := induction_on₂ c₁ c₂ (λ α β, mk_congr (equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symm)) @[simp] theorem lift_mul (c₁ c₂) : lift (c₁ * c₂) = lift c₁ * lift c₂ := induction_on₂ c₁ c₂ (λ α β, mk_congr (equiv.ulift.trans (equiv.prod_congr equiv.ulift equiv.ulift).symm)) @[simp] theorem lift_bit0 (c : cardinal) : lift (bit0 c) = bit0 (lift c) := lift_add c c @[simp] theorem lift_bit1 (c : cardinal) : lift (bit1 c) = bit1 (lift c) := by simp [bit1] theorem lift_two : lift 2 = 2 := by simp @[simp] theorem mk_set {α : Type u} : #(set α) = 2 ^ #α := by simp [set, mk_arrow] @[simp] theorem mk_powerset {α : Type u} (s : set α) : #(set.powerset s) = 2 ^ #s := (mk_congr (equiv.set.powerset s)).trans mk_set theorem lift_two_power (c : cardinal) : lift (2 ^ c) = 2 ^ lift c := by simp protected theorem zero_le (c : cardinal) : 0 ≤ c := induction_on c (λ α, ⟨function.embedding.of_is_empty⟩) private theorem add_le_add' : ∀ {c₁ c₂ c₃ c₄ : cardinal}, c₁ ≤ c₂ → c₃ ≤ c₄ → c₁ + c₃ ≤ c₂ + c₄ := by { rintros ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩, exact ⟨function.embedding.sum_map e₁ e₂⟩ } instance add_covariant_class : covariant_class cardinal cardinal (+) (≤) := ⟨λ c₁ c₂ c₃, add_le_add' le_rfl⟩ instance add_swap_covariant_class : covariant_class cardinal cardinal (function.swap (+)) (≤) := ⟨λ c₁ c₂ c₃ h, add_le_add' h le_rfl⟩ theorem exists_add_of_le {c₁ c₂ : cardinal} : c₁ ≤ c₂ → ∃ c₃, c₂ = c₁ + c₃ := begin classical, refine induction_on₂ c₁ c₂ _, rintros α β ⟨⟨f, hf⟩⟩, have h : α ⊕ ↥(set.range f)ᶜ ≃ β, { transitivity ↥(set.range f) ⊕ ↥(set.range f)ᶜ, { exact equiv.sum_congr (equiv.of_injective f hf) (equiv.refl ↥(set.range f)ᶜ) }, { exact equiv.set.sum_compl (set.range f) } }, exact ⟨#↥(set.range f)ᶜ, mk_congr h.symm⟩ end theorem le_self_add {c₁ c₂ : cardinal} : c₁ ≤ c₁ + c₂ := begin transitivity c₁ + 0, { exact eq.ge (add_zero c₁) }, { exact add_le_add_left (cardinal.zero_le c₂) c₁ } end theorem eq_zero_or_eq_zero_of_mul_eq_zero (c₁ c₂ : cardinal) : c₁ * c₂ = 0 → c₁ = 0 ∨ c₂ = 0 := begin refine induction_on₂ c₁ c₂ (λ α β, _), simpa only [mul_def, mk_eq_zero_iff, is_empty_prod] using id end instance : canonically_ordered_comm_semiring cardinal.{u} := { bot := 0, bot_le := cardinal.zero_le, add_le_add_left := λ c₁ c₂, add_le_add_left, exists_add_of_le := @exists_add_of_le, le_self_add := @le_self_add, eq_zero_or_eq_zero_of_mul_eq_zero := eq_zero_or_eq_zero_of_mul_eq_zero, ..cardinal.comm_semiring, ..cardinal.partial_order } @[simp] theorem zero_lt_one : (0 : cardinal) < 1 := lt_of_le_of_ne (zero_le 1) zero_ne_one lemma zero_power_le (c : cardinal.{u}) : (0 : cardinal.{u}) ^ c ≤ 1 := begin by_cases h : c = 0, { rw [h, power_zero] }, { rw [zero_power h], exact zero_le 1 } end theorem power_le_power_left {c₁ c₂ c₃ : cardinal} : c₁ ≠ 0 → c₂ ≤ c₃ → c₁ ^ c₂ ≤ c₁ ^ c₃ := begin refine induction_on₃ c₁ c₂ c₃ _, rintros α β γ h ⟨e⟩, haveI hα := classical.inhabited_of_nonempty (mk_ne_zero_iff.mp h), exact ⟨function.embedding.arrow_congr_left e⟩ end theorem self_le_power (c₁ : cardinal) {c₂ : cardinal} (h₂ : 1 ≤ c₂) : c₁ ≤ c₁ ^ c₂ := begin by_cases h₁ : c₁ = 0, { exact h₁.symm ▸ zero_le (0 ^ c₂) }, { convert power_le_power_left h₁ h₂, rw power_one } end theorem cantor (c : cardinal) : c < 2 ^ c := begin refine induction_on c (λ α, _), rw ←mk_set, refine ⟨⟨⟨singleton, λ a₁ a₂, set.singleton_eq_singleton_iff.mp⟩⟩, _⟩, rintro ⟨⟨f, hf⟩⟩, exact function.cantor_injective f hf end instance : no_max_order cardinal := { exists_gt := λ c, ⟨2 ^ c, cantor c⟩ } noncomputable instance : canonically_linear_ordered_add_monoid cardinal := { le_total := λ c₁ c₂, induction_on₂ c₁ c₂ (λ α β, function.embedding.total α β), decidable_le := classical.dec_rel (≤), ..cardinal.canonically_ordered_comm_semiring, ..cardinal.partial_order } noncomputable instance : distrib_lattice cardinal := infer_instance theorem one_lt_iff_nontrivial {α : Type u} : 1 < #α ↔ nontrivial α := by rw [←not_le, le_one_iff_subsingleton, ←not_nontrivial_iff_subsingleton, not_not] theorem power_le_max_power_one {c₁ c₂ c₃ : cardinal} (h : c₂ ≤ c₃) : c₁ ^ c₂ ≤ max (c₁ ^ c₃) 1 := begin by_cases h₁ : c₁ = 0, { rw [h₁, max_eq_right (zero_power_le c₃)], exact zero_power_le c₂ }, { transitivity c₁ ^ c₃, { exact power_le_power_left h₁ h }, { exact le_max_left (c₁ ^ c₃) 1 } } end theorem power_le_power_right {c₁ c₂ c₃ : cardinal} : c₁ ≤ c₂ → c₁ ^ c₃ ≤ c₂ ^ c₃ := induction_on₃ c₁ c₂ c₃ (λ α β γ ⟨e⟩, ⟨function.embedding.arrow_congr_right e⟩) protected theorem lt_wf : @well_founded cardinal (<) := begin refine ⟨λ c, _⟩, by_contra hc, let α := {c : cardinal // ¬ acc (<) c}, haveI hα : nonempty α := ⟨⟨c, hc⟩⟩, obtain ⟨⟨c, hc⟩, ⟨h⟩⟩ := function.embedding.min_injective (λ a : α, a.val.out), refine hc ⟨c, _⟩, rintros c' ⟨h₁, h₂⟩, by_contra hc', refine h₂ _, replace h : #c.out ≤ #c'.out := ⟨h ⟨c', hc'⟩⟩, simpa using h end instance : has_well_founded cardinal := ⟨(<), cardinal.lt_wf⟩ instance wo : @is_well_order cardinal (<) := ⟨cardinal.lt_wf⟩ noncomputable instance : conditionally_complete_linear_order_bot cardinal := is_well_order.conditionally_complete_linear_order_bot cardinal @[simp] theorem Inf_empty : Inf (∅ : set cardinal) = 0 := begin classical, exact dif_neg set.not_nonempty_empty end noncomputable instance : succ_order cardinal := begin refine succ_order.of_succ_le_iff (λ c, Inf {c' | c < c'}) _, intros c₁ c₂, refine ⟨_, _⟩, { let s : set cardinal := {c | c₁ < c}, have h : set.nonempty s := exists_gt c₁, replace h : Inf s ∈ s := Inf_mem h, exact lt_of_lt_of_le h }, { exact cInf_le' } end theorem succ_def (c : cardinal) : order.succ c = Inf {c' | c < c'} := rfl theorem add_one_le_succ (c : cardinal) : c + 1 ≤ order.succ c := begin refine induction_on c (λ α, _), let s : set cardinal := {c | #α < c}, have h : set.nonempty s := exists_gt #α, rw [succ_def, le_cInf_iff'' h], intro c, refine induction_on c (λ β h', _), change #α < #β at h', cases le_of_lt h' with f, have hf : ¬ function.surjective f, { exact λ hf, not_le_of_lt h' (mk_le_of_surjective hf) }, simp only [function.surjective, not_forall] at hf, cases hf with b hb, rw ←mk_option, exact ⟨function.embedding.option_elim f b hb⟩ end lemma succ_pos (c : cardinal) : 0 < order.succ c := order.bot_lt_succ c lemma succ_ne_zero (c : cardinal) : order.succ c ≠ 0 := ne.symm (ne_of_lt (succ_pos c)) def sum {ι : Type u} (f : ι → cardinal.{v}) : cardinal.{max u v} := #(Σ i, (f i).out) theorem le_sum {ι : Type u} (f : ι → cardinal.{max u v}) (i : ι) : f i ≤ sum f := mk_out (f i) ▸ ⟨⟨λ x, ⟨i, x⟩, λ x₁ x₂ h, eq_of_heq (by injection h)⟩⟩ @[simp] theorem mk_sigma {ι : Type u} (f : ι → Type v) : #(Σ i, f i) = sum (λ i, #(f i)) := mk_congr (equiv.sigma_congr_right (λ i, out_mk_equiv.symm)) @[simp] theorem sum_const (ι : Type u) (c : cardinal.{v}) : sum (λ i : ι, c) = lift #ι * lift.{u} c := begin refine induction_on c (λ α, mk_congr _), transitivity ι × (#α).out, { exact equiv.sigma_equiv_prod ι (#α).out }, { exact equiv.ulift.symm.prod_congr (out_mk_equiv.trans equiv.ulift.symm) } end theorem sum_const' (ι : Type u) (c : cardinal.{u}) : sum (λ i : ι, c) = #ι * c := by simp @[simp] theorem sum_add_distrib {ι : Type u} (f g : ι → cardinal.{v}) : sum (f + g) = sum f + sum g := begin have h := mk_congr (equiv.sigma_sum_distrib (quotient.out ∘ f) (quotient.out ∘ g)), simpa [mk_sigma, mk_sum, mk_out, lift_id] using h end @[simp] theorem sum_add_distrib' {ι : Type u} (f g : ι → cardinal.{v}) : sum (λ i, f i + g i) = sum f + sum g := sum_add_distrib f g @[simp] theorem lift_sum {ι : Type u} (f : ι → cardinal.{v}) : lift.{w} (sum f) = sum (λ i, lift.{w} (f i)) := begin refine mk_congr _, transitivity Σ i, (f i).out, { exact equiv.ulift }, { refine equiv.sigma_congr_right (λ i, classical.choice _), rw [←lift_mk_eq, mk_out, mk_out, lift_lift] } end theorem sum_le_sum {ι : Type u} (f g : ι → cardinal.{v}) (h : ∀ i, f i ≤ g i) : sum f ≤ sum g := begin refine ⟨function.embedding.sigma_map _ (λ i, classical.choice _)⟩, { exact function.embedding.refl ι }, { specialize h i, rwa [←mk_out (f i), ←mk_out (g i)] at h } end lemma mk_le_mk_mul_of_mk_preimage_le {c : cardinal} (f : α → β) : (∀ b, #(f ⁻¹' {b}) ≤ c) → #α ≤ #β * c := begin simp only [←mk_congr (@equiv.sigma_fiber_equiv α β f), mk_sigma, ←sum_const'], exact sum_le_sum (λ b, #(f ⁻¹' {b})) (λ b, c) end theorem bdd_above_range {ι : Type u} (f : ι → cardinal.{max u v}) : bdd_above (set.range f) := ⟨sum f, by { rintros c ⟨i, rfl⟩, exact le_sum f i }⟩ instance (c : cardinal.{u}) : small.{u} (set.Iic c) := begin rw ←mk_out c, let f : set c.out → set.Iic #c.out := λ s, ⟨#s, mk_set_le s⟩, refine @small_of_surjective (set c.out) (set.Iic #c.out) infer_instance f _, rintro ⟨c', h⟩, change c' ≤ #c.out at h, simp only [subtype.mk_eq_mk], rwa le_mk_iff_exists_set at h end theorem bdd_above_iff_small {s : set cardinal.{u}} : bdd_above s ↔ small.{u} s := begin split, { rintro ⟨c, hc⟩, change ∀ c', c' ∈ s → c' ≤ c at hc, exact @small_subset cardinal (set.Iic c) s hc infer_instance }, { rintro ⟨ι, ⟨e⟩⟩, let f : ι → cardinal := λ i : ι, subtype.val (e.symm i), suffices h : set.range f = s, { exact h ▸ bdd_above_range.{u u} f }, ext c, split, { rintro ⟨i, rfl⟩, exact subtype.prop (e.symm i) }, { intro hc, use e ⟨c, hc⟩, dsimp only [f], rw [equiv.symm_apply_apply] } } end theorem bdd_above_image (f : cardinal.{u} → cardinal.{max u v}) {s : set cardinal.{u}} (hs : bdd_above s) : bdd_above (f '' s) := begin rw bdd_above_iff_small at ⊢ hs, exactI small_lift ↥(f '' s) end theorem bdd_above_range_comp {ι : Type u} {f : ι → cardinal.{v}} (hf : bdd_above (set.range f)) (g : cardinal.{v} → cardinal.{max v w}) : bdd_above (set.range (g ∘ f)) := by { rw set.range_comp, exact bdd_above_image g hf } theorem supr_le_sum {ι : Type u} (f : ι → cardinal.{max u v}) : supr f ≤ sum f := csupr_le' (le_sum f) theorem sum_le_supr_lift {ι : Type u} (f : ι → cardinal.{max u v}) : sum f ≤ lift #ι * supr f := begin rw [←lift_id (supr f), ←lift_umax, lift_umax.{(max u v) u}, ←sum_const], exact sum_le_sum f (λ i, supr f) (le_csupr (bdd_above_range f)) end theorem sum_le_supr {ι : Type u} (f : ι → cardinal.{u}) : sum f ≤ #ι * supr f := lift_id #ι ▸ sum_le_supr_lift f theorem sum_nat_eq_add_sum_succ (f : ℕ → cardinal.{u}) : sum f = f 0 + sum (λ i, f (i + 1)) := begin transitivity #((f 0).out ⊕ Σ i, (f (i + 1)).out), { exact mk_congr (equiv.sigma_nat_succ (λ i, (f i).out)) }, { simp only [mk_sum, mk_out, mk_sigma, lift_id] } end @[simp] protected theorem supr_of_empty {ι : Type u} (f : ι → cardinal.{v}) [is_empty ι] : supr f = 0 := csupr_of_empty f @[simp] lemma lift_mk_shrink (α : Type u) [small.{v} α] : lift.{max u w} #(shrink α) = lift.{max v w} #α := lift_mk_eq.mpr ⟨(equiv_shrink α).symm⟩ @[simp] lemma lift_mk_shrink' (α : Type u) [small.{v} α] : lift.{u} #(shrink α) = lift.{v} #α := lift_mk_shrink.{u v 0} α @[simp] lemma lift_mk_shrink'' (α : Type (max u v)) [small.{v} α] : lift.{u} #(shrink α) = #α := by rw [←lift_umax', lift_mk_shrink'.{(max u v) v} α, ←lift_umax, lift_id] def prod {ι : Type u} (f : ι → cardinal.{v}) : cardinal.{max u v} := #(Π i, (f i).out) @[simp] theorem mk_pi {ι : Type u} (f : ι → Type v) : #(Π i, f i) = prod (λ i, #(f i)) := mk_congr (equiv.Pi_congr_right (λ i, out_mk_equiv.symm)) @[simp] theorem prod_const (ι : Type u) (c : cardinal.{v}) : prod (λ i : ι, c) = lift.{u} c ^ lift.{v} #ι := begin refine induction_on c (λ α, mk_congr (equiv.Pi_congr _ _)), { exact equiv.ulift.symm }, { intro i, transitivity α, { exact out_mk_equiv }, { exact equiv.ulift.symm } } end theorem prod_const' (ι : Type u) (c : cardinal.{u}) : prod (λ i : ι, c) = c ^ #ι := by simp theorem prod_le_prod {ι : Type u} (f g : ι → cardinal.{v}) (h : ∀ i, f i ≤ g i) : prod f ≤ prod g := begin refine ⟨function.embedding.Pi_congr_right (λ i, classical.choice _)⟩, specialize h i, rwa [←mk_out (f i), ←mk_out (g i)] at h end @[simp] theorem prod_eq_zero {ι : Type u} (f : ι → cardinal.{v}) : prod f = 0 ↔ ∃ i, f i = 0 := begin lift f to ι → Type v using λ i, trivial, simp only [mk_eq_zero_iff, ←mk_pi, is_empty_pi] end theorem prod_ne_zero {ι : Type u} (f : ι → cardinal.{v}) : prod f ≠ 0 ↔ ∀ i, f i ≠ 0 := by simp [prod_eq_zero] @[simp] theorem lift_prod {ι : Type u} (f : ι → cardinal.{v}) : lift.{w} (prod f) = prod (λ i, lift.{w} (f i)) := begin lift f to ι → Type v using λ i, trivial, simp only [←mk_pi, ←mk_ulift], refine mk_congr _, transitivity Π i, f i, { exact equiv.ulift }, { exact equiv.Pi_congr_right (λ i, equiv.ulift.symm) } end @[simp] theorem lift_Inf (s : set cardinal) : lift (Inf s) = Inf (lift '' s) := begin cases set.eq_empty_or_nonempty s with hs hs, { rw [hs, Inf_empty, lift_zero, set.image_empty, Inf_empty] }, { exact lift_monotone.map_Inf hs } end @[simp] theorem lift_infi {ι : Type u} (f : ι → cardinal.{v}) : lift (infi f) = infi (λ i, lift (f i)) := begin unfold infi, convert lift_Inf (set.range f), rw set.range_comp end theorem lift_down {c₁ : cardinal.{u}} {c₂ : cardinal.{max u v}} : c₂ ≤ lift c₁ → ∃ c₃, lift c₃ = c₂ := begin refine induction_on₂ c₁ c₂ (λ α β, _), rw [←lift_id #β, ←lift_umax, ←lift_umax.{u}, lift_mk_le], rintro ⟨f⟩, use #(set.range f), rw [eq_comm, lift_mk_eq], refine ⟨function.embedding.equiv_of_surjective _ _⟩, { exact function.embedding.cod_restrict (set.range f) f set.mem_range_self }, { rintro ⟨a, ⟨b, h⟩⟩, simp only [function.embedding.cod_restrict_apply, subtype.mk_eq_mk], exact ⟨b, h⟩ } end theorem le_lift_iff {c₁ : cardinal.{u}} {c₂ : cardinal.{max u v}} : c₂ ≤ lift c₁ ↔ ∃ c₃, lift c₃ = c₂ ∧ c₃ ≤ c₁ := begin split, { intro h, rcases lift_down h with ⟨c₃, rfl⟩, rw lift_le at h, exact ⟨c₃, rfl, h⟩ }, { rintro ⟨c₃, rfl, h⟩, rwa lift_le } end theorem lt_lift_iff {c₁ : cardinal.{u}} {c₂ : cardinal.{max u v}} : c₂ < lift c₁ ↔ ∃ c₃, lift c₃ = c₂ ∧ c₃ < c₁ := begin split, { intro h, rcases lift_down (le_of_lt h) with ⟨c₃, rfl⟩, rw lift_lt at h, exact ⟨c₃, rfl, h⟩ }, { rintro ⟨c₃, rfl, h⟩, rwa lift_lt } end @[simp] theorem lift_succ (c : cardinal) : lift (order.succ c) = order.succ (lift c) := begin refine le_antisymm (le_of_not_gt _) _, { intro h, rcases lt_lift_iff.mp h with ⟨c', h₁, h₂⟩, rw [order.lt_succ_iff, ←lift_le, h₁, ←not_lt] at h₂, exact h₂ (order.lt_succ c.lift) }, { refine order.succ_le_of_lt _, rw lift_lt, exact order.lt_succ c } end @[simp] theorem lift_umax_eq {c₁ : cardinal.{u}} {c₂ : cardinal.{v}} : lift.{max v w} c₁ = lift.{max u w} c₂ ↔ lift.{v} c₁ = lift.{u} c₂ := by rw [←lift_lift, ←lift_lift, lift_inj] @[simp] theorem lift_min {c₁ c₂ : cardinal} : lift (min c₁ c₂) = min (lift c₁) (lift c₂) := lift_monotone.map_min @[simp] theorem lift_max {c₁ c₂ : cardinal} : lift (max c₁ c₂) = max (lift c₁) (lift c₂) := lift_monotone.map_max lemma lift_Sup {s : set cardinal} (hs : bdd_above s) : lift (Sup s) = Sup (lift '' s) := begin refine le_antisymm _ _, { rw le_cSup_iff' (bdd_above_image lift hs), intros c hc, by_contra h, obtain ⟨c, rfl⟩ := lift_down (le_of_not_le h), rw [lift_le, cSup_le_iff' hs] at h, refine h _, intros c' hc', change ∀ c', c' ∈ lift '' s → c' ≤ c.lift at hc, specialize hc (lift c') (set.mem_image_of_mem lift hc'), rwa lift_le at hc }, { refine cSup_le' _, rintros c ⟨c, hc, rfl⟩, rw lift_le, exact le_cSup hs hc } end lemma lift_supr {ι : Type u} {f : ι → cardinal.{v}} (hf : bdd_above (set.range f)) : lift (supr f) = supr (λ i, lift (f i)) := by rw [supr, supr, lift_Sup hf, set.range_comp lift] @[simp] lemma lift_supr_le_iff {ι : Type u} {f : ι → cardinal.{v}} {t : cardinal.{max v w}} : bdd_above (set.range f) → (lift (supr f) ≤ t ↔ ∀ i, lift (f i) ≤ t) := begin intro hf, rw lift_supr hf, exact csupr_le_iff' (bdd_above_range_comp hf lift) end lemma lift_supr_le {ι : Type u} {f : ι → cardinal.{v}} {t : cardinal.{max v w}} : bdd_above (set.range f) → (∀ i, lift (f i) ≤ t) → lift (supr f) ≤ t := by { intro hf, simp [hf] } lemma {u' v'} lift_supr_le_lift_supr {ι : Type u} {ι' : Type u'} {f : ι → cardinal.{v}} {f' : ι' → cardinal.{v'}} (hf : bdd_above (set.range f)) (hf' : bdd_above (set.range f')) {g : ι → ι'} (h : ∀ i, lift.{v'} (f i) ≤ lift.{v} (f' (g i))) : lift.{v'} (supr f) ≤ lift.{v} (supr f') := begin rw [lift_supr hf, lift_supr hf'], refine csupr_mono' (bdd_above_range_comp hf' lift) _, intro i, use g i, exact h i end lemma lift_supr_le_lift_supr' {ι : Type u} {ι' : Type v} {f : ι → cardinal.{u}} {f' : ι' → cardinal.{v}} (hf : bdd_above (set.range f)) (hf' : bdd_above (set.range f')) {g : ι → ι'} (h : ∀ i, lift.{v} (f i) ≤ lift.{u} (f' (g i))) : lift.{v} (supr f) ≤ lift.{u} (supr f') := lift_supr_le_lift_supr hf hf' h def aleph_0 : cardinal.{u} := lift #ℕ notation `ℵ₀` := cardinal.aleph_0 lemma mk_nat : #ℕ = ℵ₀ := eq.symm (lift_id #ℕ) theorem aleph_0_ne_zero : ℵ₀ ≠ 0 := mk_ne_zero (ulift ℕ) theorem aleph_0_pos : 0 < ℵ₀ := pos_iff_ne_zero.mpr aleph_0_ne_zero @[simp] theorem lift_aleph_0 : lift ℵ₀ = ℵ₀ := lift_lift #ℕ @[simp] theorem aleph_0_le_lift {c : cardinal} : ℵ₀ ≤ lift c ↔ ℵ₀ ≤ c := by rw [←lift_aleph_0, lift_le] @[simp] theorem lift_le_aleph_0 {c : cardinal} : lift c ≤ ℵ₀ ↔ c ≤ ℵ₀ := by rw [←lift_aleph_0, lift_le] @[simp] theorem mk_fin (n : ℕ) : #(fin n) = n := by simp @[simp] theorem lift_nat_cast (n : ℕ) : lift.{v} (n : cardinal.{u}) = n := begin induction n with n ih, { rw [nat.cast_zero, nat.cast_zero, lift_zero] }, { rw [nat.cast_succ, nat.cast_succ, lift_add, lift_one, ih] } end @[simp] lemma lift_eq_nat_iff {c : cardinal.{u}} {n : ℕ} : lift.{v} c = n ↔ c = n := by rw [←lift_nat_cast.{u} n, lift_inj] @[simp] lemma nat_eq_lift_iff {n : ℕ} {c : cardinal.{u}} : ↑n = lift.{v} c ↔ ↑n = c := by rw [←lift_nat_cast.{u} n, lift_inj] theorem lift_mk_fin (n : ℕ) : lift #(fin n) = n := by simp lemma mk_coe_finset {s : finset α} : #s = finset.card s := by simp lemma mk_finset_of_fintype [fintype α] : #(finset α) = 2 ^ℕ fintype.card α := by simp theorem card_le_of_finset (s : finset α) : ↑s.card ≤ #α := begin rw [show ↑s.card = #s, by rw [cardinal.mk_fintype, fintype.card_coe]], exact ⟨function.embedding.subtype (λ a, a ∈ s)⟩ end @[simp, norm_cast] theorem nat_cast_pow {n m : ℕ} : ↑(pow n m) = n ^ m := begin induction m with m ih, { rw [pow_zero, nat.cast_one, nat.cast_zero, power_zero] }, { rw [pow_succ', nat.cast_mul, ih, nat.cast_succ, power_add, power_one] } end @[simp, norm_cast] theorem nat_cast_le {n m : ℕ} : (n : cardinal) ≤ m ↔ n ≤ m := begin rw [←lift_mk_fin, ←lift_mk_fin, lift_le], split, { rintro ⟨⟨f, hf⟩⟩, have h : fintype.card (fin n) ≤ fintype.card (fin m), { exact fintype.card_le_of_injective f hf }, rwa [fintype.card_fin n, fintype.card_fin m] at h }, { intro h, obtain ⟨f, hf⟩ : fin n ↪o fin m := fin.cast_le h, exact ⟨f⟩ } end @[simp, norm_cast] theorem nat_cast_lt {n m : ℕ} : (n : cardinal) < m ↔ n < m := by { rw [←not_le, ←not_le, not_iff_not], exact nat_cast_le } instance : char_zero cardinal := ⟨strict_mono.injective (λ m n, nat_cast_lt.mpr)⟩ theorem nat_cast_inj {n m : ℕ} : (n : cardinal) = m ↔ n = m := nat.cast_inj lemma nat_cast_injective : function.injective (coe : ℕ → cardinal) := nat.cast_injective @[simp, norm_cast, priority 900] theorem nat_succ (n : ℕ) : (n.succ : cardinal) = order.succ n := begin refine le_antisymm _ _, { exact add_one_le_succ n }, { refine order.succ_le_of_lt _, rw nat_cast_lt, exact nat.lt_succ_self n } end @[simp] theorem succ_zero : order.succ (0 : cardinal) = 1 := by norm_cast @[simp] theorem succ_one : order.succ (1 : cardinal) = 2 := by norm_cast theorem card_le_of {n : ℕ} (h : ∀ s : finset α, s.card ≤ n) : #α ≤ n := begin refine order.le_of_lt_succ (lt_of_not_ge _), intro h', rw [←nat_succ, ←lift_mk_fin n.succ] at h', cases h' with f, specialize h (finset.map f finset.univ), refine not_lt_of_le h _, rw [finset.card_map, ←fintype.card, fintype.card_ulift, fintype.card_fin], exact nat.lt_succ_self n end theorem cantor' (c₁ : cardinal) {c₂ : cardinal} (hb : 1 < c₂) : c₁ < c₂ ^ c₁ := begin rw [←order.succ_le_iff, succ_one] at hb, calc c₁ < 2 ^ c₁ : cantor c₁ ... ≤ c₂ ^ c₁ : power_le_power_right hb end theorem one_le_iff_pos {c : cardinal} : 1 ≤ c ↔ 0 < c := succ_zero ▸ order.succ_le_iff theorem one_le_iff_ne_zero {c : cardinal} : 1 ≤ c ↔ c ≠ 0 := by rw [one_le_iff_pos, pos_iff_ne_zero] theorem nat_lt_aleph_0 (n : ℕ) : ↑n < ℵ₀ := begin rw [←order.succ_le_iff, ←nat_succ, ←lift_mk_fin, aleph_0, lift_mk_le.{0 0}], exact ⟨⟨coe, λ n m, fin.ext⟩⟩ end @[simp] theorem one_lt_aleph_0 : 1 < ℵ₀ := @nat.cast_one cardinal infer_instance ▸ nat_lt_aleph_0 1 theorem one_le_aleph_0 : 1 ≤ ℵ₀ := le_of_lt one_lt_aleph_0 theorem lt_aleph_0 {c : cardinal} : c < ℵ₀ ↔ ∃ n : ℕ, c = n := begin split, { intro h, change c < lift #ℕ at h, rw lt_lift_iff at h, rcases h with ⟨c, rfl, h, h'⟩, rw le_mk_iff_exists_set at h, rcases h with ⟨s, rfl⟩, suffices h : set.finite s, { lift s to finset ℕ using h, simp }, contrapose! h' with h, replace h : infinite s := set.infinite.to_subtype h, exactI ⟨infinite.nat_embedding s⟩ }, { rintro ⟨n, rfl⟩, exact nat_lt_aleph_0 n } end theorem aleph_0_le {c : cardinal} : ℵ₀ ≤ c ↔ ∀ n : ℕ, ↑n ≤ c := begin refine ⟨λ h n, _, λ h, le_of_not_lt (λ h', _)⟩, { transitivity ℵ₀, { exact le_of_lt (nat_lt_aleph_0 n) }, { exact h } }, { rw lt_aleph_0 at h', rcases h' with ⟨n, rfl⟩, specialize h n.succ, rw nat_cast_le at h, exact not_le_of_lt (nat.lt_succ_self n) h } end theorem lt_aleph_0_iff_fintype : #α < ℵ₀ ↔ nonempty (fintype α) := begin transitivity ∃ n, #α = ↑n, { exact lt_aleph_0 }, { split, { rintro ⟨n, h⟩, rw ←lift_mk_fin n at h, cases quotient.exact h with e, exact ⟨fintype.of_equiv (ulift (fin n)) e.symm⟩ }, { rintro ⟨h⟩, exactI ⟨fintype.card α, mk_fintype α⟩ } } end theorem lt_aleph_0_of_fintype (α : Type u) [fintype α] : #α < ℵ₀ := lt_aleph_0_iff_fintype.mpr ⟨infer_instance⟩ theorem lt_aleph_0_iff_finite {s : set α} : #s < ℵ₀ ↔ s.finite := begin transitivity nonempty (fintype s), { exact lt_aleph_0_iff_fintype }, { rw set.finite_def } end instance can_lift_cardinal_nat : can_lift cardinal ℕ := ⟨coe, λ c, c < ℵ₀, λ c h, let ⟨n, h⟩ := lt_aleph_0.mp h in ⟨n, h.symm⟩⟩ theorem add_lt_aleph_0 {c₁ c₂ : cardinal} (h₁ : c₁ < ℵ₀) (h₂ : c₂ < ℵ₀) : c₁ + c₂ < ℵ₀ := begin rw lt_aleph_0 at h₁ h₂, rcases h₁ with ⟨n, rfl⟩, rcases h₂ with ⟨m, rfl⟩, rw ←nat.cast_add, exact nat_lt_aleph_0 (n + m) end lemma add_lt_aleph_0_iff {c₁ c₂ : cardinal} : c₁ + c₂ < ℵ₀ ↔ c₁ < ℵ₀ ∧ c₂ < ℵ₀ := begin split, { refine λ h, ⟨lt_of_le_of_lt _ h, lt_of_le_of_lt _ h⟩, { exact self_le_add_right c₁ c₂ }, { exact self_le_add_left c₂ c₁ } }, { rintros ⟨h₁, h₂⟩, exact add_lt_aleph_0 h₁ h₂ } end lemma aleph_0_le_add_iff {c₁ c₂ : cardinal} : ℵ₀ ≤ c₁ + c₂ ↔ ℵ₀ ≤ c₁ ∨ ℵ₀ ≤ c₂ := by rw [←not_lt, ←not_lt, ←not_lt, add_lt_aleph_0_iff, not_and_distrib] lemma nsmul_lt_aleph_0_iff {n : ℕ} {c : cardinal} : n • c < ℵ₀ ↔ n = 0 ∨ c < ℵ₀ := begin cases n with n, { rw [zero_smul, eq_self_iff_true, true_or, iff_true], exact nat_lt_aleph_0 0 }, { simp only [nat.succ_ne_zero, false_or], induction n with n ih, { rw [nsmul_eq_mul, nat.cast_one, one_mul] }, { rw [succ_nsmul, add_lt_aleph_0_iff, ih, and_self] } } end lemma nsmul_lt_aleph_0_iff_of_ne_zero {n : ℕ} {c : cardinal} (h : n ≠ 0) : n • c < ℵ₀ ↔ c < ℵ₀ := begin transitivity n = 0 ∨ c < ℵ₀, { exact nsmul_lt_aleph_0_iff }, { exact or_iff_right h } end theorem mul_lt_aleph_0 {c₁ c₂ : cardinal} (h₁ : c₁ < ℵ₀) (h₂ : c₂ < ℵ₀) : c₁ * c₂ < ℵ₀ := begin rw lt_aleph_0 at h₁ h₂, rcases h₁ with ⟨n, rfl⟩, rcases h₂ with ⟨m, rfl⟩, rw ←nat.cast_mul, exact nat_lt_aleph_0 (n * m) end lemma mul_lt_aleph_0_iff {c₁ c₂ : cardinal} : c₁ * c₂ < ℵ₀ ↔ c₁ = 0 ∨ c₂ = 0 ∨ (c₁ < ℵ₀ ∧ c₂ < ℵ₀) := begin split, { intro h, by_cases h₁ : c₁ = 0, exact or.inl h₁, right, by_cases h₂ : c₂ = 0, exact or.inl h₂, right, rw [←ne, ←one_le_iff_ne_zero] at h₁ h₂, split, { rw ←mul_one c₁, exact lt_of_le_of_lt (mul_le_mul' le_rfl h₂) h }, { rw ←one_mul c₂, exact lt_of_le_of_lt (mul_le_mul' h₁ le_rfl) h } }, { rintro (rfl | rfl | ⟨h₁, h₂⟩), { rw [zero_mul], exact aleph_0_pos }, { rw [mul_zero], exact aleph_0_pos }, { exact mul_lt_aleph_0 h₁ h₂ } } end lemma aleph_0_le_mul_iff {c₁ c₂ : cardinal} : ℵ₀ ≤ c₁ * c₂ ↔ c₁ ≠ 0 ∧ c₂ ≠ 0 ∧ (ℵ₀ ≤ c₁ ∨ ℵ₀ ≤ c₂) := begin have h := not_congr (@mul_lt_aleph_0_iff c₁ c₂), rwa [not_lt, not_or_distrib, not_or_distrib, not_and_distrib, not_lt, not_lt] at h end lemma aleph_0_le_mul_iff' {c₁ c₂ : cardinal.{u}} : ℵ₀ ≤ c₁ * c₂ ↔ (c₁ ≠ 0 ∧ ℵ₀ ≤ c₂) ∨ (ℵ₀ ≤ c₁ ∧ c₂ ≠ 0) := begin transitivity c₁ ≠ 0 ∧ c₂ ≠ 0 ∧ (ℵ₀ ≤ c₁ ∨ ℵ₀ ≤ c₂), { exact aleph_0_le_mul_iff }, { have h₁ : ℵ₀ ≤ c₁ → c₁ ≠ 0 := ne_bot_of_le_ne_bot aleph_0_ne_zero, have h₂ : ℵ₀ ≤ c₂ → c₂ ≠ 0 := ne_bot_of_le_ne_bot aleph_0_ne_zero, tauto } end lemma mul_lt_aleph_0_iff_of_ne_zero {c₁ c₂ : cardinal} (h₁ : c₁ ≠ 0) (h₂ : c₂ ≠ 0) : c₁ * c₂ < ℵ₀ ↔ c₁ < ℵ₀ ∧ c₂ < ℵ₀ := by simp only [mul_lt_aleph_0_iff, h₁, h₂, false_or] theorem power_lt_aleph_0 {c₁ c₂ : cardinal} (h₁ : c₁ < ℵ₀) (h₂ : c₂ < ℵ₀) : c₁ ^ c₂ < ℵ₀ := begin rw lt_aleph_0 at h₁ h₂, rcases h₁ with ⟨n, rfl⟩, rcases h₂ with ⟨m, rfl⟩, rw ←nat_cast_pow, exact nat_lt_aleph_0 (pow n m) end lemma eq_one_iff_unique : #α = 1 ↔ subsingleton α ∧ nonempty α := begin transitivity #α ≤ 1 ∧ 1 ≤ #α, { exact le_antisymm_iff }, { refine and_congr le_one_iff_subsingleton _, transitivity #α ≠ 0, { exact one_le_iff_ne_zero }, { exact mk_ne_zero_iff } } end lemma eq_one_iff_unique' : #α = 1 ↔ nonempty (unique α) := begin transitivity subsingleton α ∧ nonempty α, { exact eq_one_iff_unique }, { exact iff.symm (unique_iff_subsingleton_and_nonempty α) } end theorem infinite_iff : infinite α ↔ ℵ₀ ≤ #α := by rw [←not_lt, lt_aleph_0_iff_fintype, not_nonempty_iff, is_empty_fintype] @[simp] lemma aleph_0_le_mk (α : Type u) [infinite α] : ℵ₀ ≤ #α := infinite_iff.mp infer_instance lemma encodable_iff : nonempty (encodable α) ↔ #α ≤ ℵ₀ := begin split, { rintro ⟨h⟩, refine ⟨_⟩, transitivity ℕ, { exact @encodable.encode' α h }, { exact equiv.ulift.symm.to_embedding } }, { rintro ⟨f⟩, replace f : α ↪ ℕ := function.embedding.trans f equiv.ulift.to_embedding, exact ⟨encodable.of_inj f (function.embedding.injective f)⟩ } end @[simp] lemma mk_le_aleph_0 (α : Type u) [encodable α] : #α ≤ ℵ₀ := encodable_iff.mp ⟨infer_instance⟩ lemma denumerable_iff : nonempty (denumerable α) ↔ #α = ℵ₀ := begin split, { rintro ⟨h⟩, refine mk_congr _, transitivity ℕ, { exact @denumerable.eqv α h }, { exact equiv.ulift.symm } }, { intro h, cases quotient.exact h with e, refine ⟨denumerable.mk' _⟩, transitivity ulift ℕ, { exact e }, { exact equiv.ulift } } end @[simp] lemma mk_denumerable (α : Type u) [denumerable α] : #α = ℵ₀ := denumerable_iff.mp ⟨infer_instance⟩ @[simp] lemma mk_set_le_aleph_0 (s : set α) : #s ≤ ℵ₀ ↔ set.countable s := begin rw [set.countable_iff_exists_injective], split, { rintro ⟨f⟩, obtain ⟨f, hf⟩ : s ↪ ℕ := function.embedding.trans f equiv.ulift.to_embedding, exact ⟨f, hf⟩ }, { rintro ⟨f, hf⟩, have f : s ↪ ulift ℕ := function.embedding.trans ⟨f, hf⟩ equiv.ulift.symm.to_embedding, exact ⟨f⟩ } end @[simp] lemma mk_subtype_le_aleph_0 (p : α → Prop) : #{a // p a} ≤ ℵ₀ ↔ set.countable {a | p a} := mk_set_le_aleph_0 (set_of p) @[simp] lemma aleph_0_add_aleph_0 : (ℵ₀ + ℵ₀ : cardinal.{u}) = ℵ₀ := mk_denumerable (ulift.{u} ℕ ⊕ ulift.{u} ℕ) lemma aleph_0_mul_aleph_0 : (ℵ₀ * ℵ₀ : cardinal.{u}) = ℵ₀ := mk_denumerable (ulift.{u} ℕ × ulift.{u} ℕ) @[simp] lemma add_le_aleph_0 {c₁ c₂ : cardinal} : c₁ + c₂ ≤ ℵ₀ ↔ c₁ ≤ ℵ₀ ∧ c₂ ≤ ℵ₀ := begin split, { intro h, split, { exact le_trans le_self_add h }, { exact le_trans le_add_self h } }, { rintro ⟨h₁, h₂⟩, exact aleph_0_add_aleph_0 ▸ add_le_add h₁ h₂ } end noncomputable def to_nat.to_fun : cardinal → ℕ := λ c, if h : c < ℵ₀ then classical.some (lt_aleph_0.mp h) else 0 theorem to_nat.map_zero' : to_nat.to_fun 0 = 0 := begin unfold to_nat.to_fun, have h : 0 < ℵ₀ := nat_lt_aleph_0 0, rw [dif_pos h, ←nat_cast_inj, ←classical.some_spec (lt_aleph_0.mp h), nat.cast_zero] end noncomputable def to_nat : zero_hom cardinal ℕ := { to_fun := to_nat.to_fun, map_zero' := to_nat.map_zero' } lemma to_nat_apply_of_lt_aleph_0 {c : cardinal} (h : c < ℵ₀) : to_nat c = classical.some (lt_aleph_0.mp h) := dif_pos h lemma to_nat_apply_of_aleph_0_le {c : cardinal} (h : ℵ₀ ≤ c) : to_nat c = 0 := dif_neg (not_lt_of_le h) lemma cast_to_nat_of_lt_aleph_0 {c : cardinal} (h : c < ℵ₀) : ↑(to_nat c) = c := by rw [to_nat_apply_of_lt_aleph_0 h, ←classical.some_spec (lt_aleph_0.mp h)] lemma cast_to_nat_of_aleph_0_le {c : cardinal} (h : ℵ₀ ≤ c) : (to_nat c : cardinal) = 0 := by rw [to_nat_apply_of_aleph_0_le h, nat.cast_zero] lemma to_nat_le_iff_le_of_lt_aleph_0 {c₁ c₂ : cardinal} (h₁ : c₁ < ℵ₀) (h₂ : c₂ < ℵ₀) : to_nat c₁ ≤ to_nat c₂ ↔ c₁ ≤ c₂ := by rw [←nat_cast_le, cast_to_nat_of_lt_aleph_0 h₁, cast_to_nat_of_lt_aleph_0 h₂] lemma to_nat_lt_iff_lt_of_lt_aleph_0 {c₁ c₂ : cardinal} (h₁ : c₁ < ℵ₀) (h₂ : c₂ < ℵ₀) : to_nat c₁ < to_nat c₂ ↔ c₁ < c₂ := by rw [←nat_cast_lt, cast_to_nat_of_lt_aleph_0 h₁, cast_to_nat_of_lt_aleph_0 h₂] lemma to_nat_le_of_le_of_lt_aleph_0 {c₁ c₂ : cardinal} (h₁ : c₂ < ℵ₀) (h₂ : c₁ ≤ c₂) : to_nat c₁ ≤ to_nat c₂ := by rwa [to_nat_le_iff_le_of_lt_aleph_0 (lt_of_le_of_lt h₂ h₁) h₁] lemma to_nat_lt_of_lt_of_lt_aleph_0 {c₁ c₂ : cardinal} (h₁ : c₂ < ℵ₀) (h₂ : c₁ < c₂) : to_nat c₁ < to_nat c₂ := by rwa [to_nat_lt_iff_lt_of_lt_aleph_0 (lt_trans h₂ h₁) h₁] @[simp] lemma to_nat_cast (n : ℕ) : to_nat n = n := begin have h : ↑n < ℵ₀ := nat_lt_aleph_0 n, rw [to_nat_apply_of_lt_aleph_0 h, ←nat_cast_inj, ←classical.some_spec (lt_aleph_0.mp h)] end lemma to_nat_right_inverse : function.right_inverse coe to_nat := to_nat_cast lemma to_nat_surjective : function.surjective to_nat := to_nat_right_inverse.surjective @[simp] lemma mk_to_nat_of_infinite [infinite α] : to_nat #α = 0 := dif_neg (not_lt_of_le (infinite_iff.mp infer_instance)) @[simp] theorem aleph_0_to_nat : to_nat ℵ₀ = 0 := to_nat_apply_of_aleph_0_le le_rfl lemma mk_to_nat_eq_card [fintype α] : to_nat #α = fintype.card α := by simp @[simp] lemma zero_to_nat : to_nat 0 = 0 := by rw [←to_nat_cast 0, nat.cast_zero] @[simp] lemma one_to_nat : to_nat 1 = 1 := by rw [←to_nat_cast 1, nat.cast_one] @[simp] lemma to_nat_eq_one {c : cardinal} : to_nat c = 1 ↔ c = 1 := begin split, { intro h, transitivity ↑(to_nat c), { replace h : ¬ ℵ₀ ≤ c := one_ne_zero ∘ h.symm.trans ∘ to_nat_apply_of_aleph_0_le, rw cast_to_nat_of_lt_aleph_0 (lt_of_not_ge h) }, { replace h : (to_nat c : cardinal) = ↑1 := congr_arg coe h, rwa nat.cast_one at h } }, { rintro rfl, exact one_to_nat } end lemma to_nat_eq_one_iff_unique : to_nat #α = 1 ↔ subsingleton α ∧ nonempty α := begin transitivity #α = 1, { exact to_nat_eq_one }, { exact eq_one_iff_unique } end @[simp] lemma to_nat_lift (c : cardinal) : to_nat (lift c) = to_nat c := begin refine nat_cast_injective _, cases lt_or_ge c ℵ₀ with h₁ h₁, { have h₂ : lift c < ℵ₀ := lift_aleph_0 ▸ lift_lt.mpr h₁, rw [cast_to_nat_of_lt_aleph_0 h₂, ←lift_nat_cast, cast_to_nat_of_lt_aleph_0 h₁] }, { have h₂ : ℵ₀ ≤ lift c := lift_aleph_0 ▸ lift_le.mpr h₁, rw [cast_to_nat_of_aleph_0_le h₂, cast_to_nat_of_aleph_0_le h₁] } end lemma to_nat_congr (e : α ≃ β) : to_nat #α = to_nat #β := by rw [←to_nat_lift, lift_mk_eq.mpr ⟨e⟩, to_nat_lift] @[simp] lemma to_nat_mul (c₁ c₂ : cardinal) : to_nat (c₁ * c₂) = to_nat c₁ * to_nat c₂ := begin rcases eq_or_ne c₁ 0 with rfl | h₁₁, { rw [zero_mul, zero_to_nat, zero_mul] }, rcases eq_or_ne c₂ 0 with rfl | h₂₁, { rw [mul_zero, zero_to_nat, mul_zero] }, cases lt_or_le c₁ ℵ₀ with h₁₂ h₁₂, { cases lt_or_le c₂ ℵ₀ with h₂₂ h₂₂, { lift c₁ to ℕ using h₁₂, lift c₂ to ℕ using h₂₂, rw [←nat.cast_mul, to_nat_cast, to_nat_cast, to_nat_cast] }, { have h : ℵ₀ ≤ c₁ * c₂ := aleph_0_le_mul_iff'.mpr (or.inl ⟨h₁₁, h₂₂⟩), rw [to_nat_apply_of_aleph_0_le h₂₂, mul_zero, to_nat_apply_of_aleph_0_le h] } }, { have h : ℵ₀ ≤ c₁ * c₂ := aleph_0_le_mul_iff'.mpr (or.inr ⟨h₁₂, h₂₁⟩), rw [to_nat_apply_of_aleph_0_le h₁₂, zero_mul, to_nat_apply_of_aleph_0_le h] }, end @[simp] lemma to_nat_add_of_lt_aleph_0 {c₁ : cardinal.{u}} {c₂ : cardinal.{v}} : c₁ < ℵ₀ → c₂ < ℵ₀ → to_nat (lift.{v} c₁ + lift.{u} c₂) = to_nat c₁ + to_nat c₂ := begin intros h₁ h₂, refine cardinal.nat_cast_injective _, replace h₁ : lift.{v} c₁ < ℵ₀ := lift_aleph_0 ▸ lift_lt.mpr h₁, replace h₂ : lift.{u} c₂ < ℵ₀ := lift_aleph_0 ▸ lift_lt.mpr h₂, rw [nat.cast_add, ←to_nat_lift c₁, ←to_nat_lift c₂, cast_to_nat_of_lt_aleph_0 h₁, cast_to_nat_of_lt_aleph_0 h₂], exact cast_to_nat_of_lt_aleph_0 (add_lt_aleph_0 h₁ h₂) end noncomputable def to_enat.to_fun : cardinal → enat := λ c, if c < ℵ₀ then to_nat c else ⊤ theorem to_enat.map_zero' : to_enat.to_fun 0 = 0 := begin unfold to_enat.to_fun, have h : 0 < ℵ₀ := nat_lt_aleph_0 0, rw [if_pos h, zero_to_nat, nat.cast_zero] end theorem to_enat.map_add' (c₁ c₂ : cardinal) : to_enat.to_fun (c₁ + c₂) = to_enat.to_fun c₁ + to_enat.to_fun c₂ := begin unfold to_enat.to_fun, by_cases h₁ : c₁ < ℵ₀, { by_cases h₂ : c₂ < ℵ₀, { obtain ⟨n, rfl⟩ := lt_aleph_0.mp h₁, obtain ⟨m, rfl⟩ := lt_aleph_0.mp h₂, rw [if_pos (add_lt_aleph_0 h₁ h₂), if_pos h₁, if_pos h₂], rw [←nat.cast_add, to_nat_cast, to_nat_cast, to_nat_cast, nat.cast_add] }, { have h : ¬ c₁ + c₂ < ℵ₀, { contrapose! h₂, exact lt_of_le_of_lt le_add_self h₂ }, rw [if_neg h₂, if_neg h, enat.add_top] } }, { have h : ¬ c₁ + c₂ < ℵ₀, { contrapose! h₁, exact lt_of_le_of_lt le_self_add h₁ }, rw [if_neg h₁, if_neg h, enat.top_add] } end noncomputable def to_enat : cardinal →+ enat := { to_fun := to_enat.to_fun, map_zero' := to_enat.map_zero', map_add' := to_enat.map_add' } lemma to_enat_apply_of_lt_aleph_0 {c : cardinal} (h : c < ℵ₀) : to_enat c = to_nat c := if_pos h lemma to_enat_apply_of_aleph_0_le {c : cardinal} (h : ℵ₀ ≤ c) : c.to_enat = ⊤ := if_neg (not_lt_of_le h) @[simp] lemma to_enat_cast (n : ℕ) : to_enat n = n := by rw [to_enat_apply_of_lt_aleph_0 (nat_lt_aleph_0 n), to_nat_cast] @[simp] lemma mk_to_enat_of_infinite [infinite α] : to_enat #α = ⊤ := to_enat_apply_of_aleph_0_le (infinite_iff.mp infer_instance) @[simp] theorem aleph_0_to_enat : to_enat ℵ₀ = ⊤ := to_enat_apply_of_aleph_0_le le_rfl lemma to_enat_surjective : function.surjective to_enat := begin refine λ n, enat.cases_on n _ (λ n, _), { exact ⟨ℵ₀, to_enat_apply_of_aleph_0_le le_rfl⟩ }, { exact ⟨n, to_enat_cast n⟩ } end lemma mk_to_enat_eq_coe_card [fintype α] : to_enat #α = fintype.card α := by simp lemma mk_int : #ℤ = ℵ₀ := mk_denumerable ℤ lemma mk_pnat : #ℕ+ = ℵ₀ := mk_denumerable ℕ+ theorem sum_lt_prod {ι : Type u} (f g : ι → cardinal.{v}) (h : ∀ i, f i < g i) : sum f < prod g := begin refine lt_of_not_ge _, rintro ⟨F⟩, haveI I : nonempty (Π i, (g i).out), { refine ⟨λ i, classical.choice (mk_ne_zero_iff.mp _)⟩, rw mk_out, exact ne_bot_of_gt (h i) }, let G : (Σ i, (f i).out) → Π i, (g i).out := function.inv_fun F, have hG : function.surjective G, { exact function.inv_fun_surjective (function.embedding.injective F) }, have hC : ∀ i, ∃ y, ∀ x, G ⟨i, x⟩ i ≠ y, { simp only [←not_exists, ←not_forall], change ¬ ∃ i, ∀ y, ∃ x, G ⟨i, x⟩ i = y, rintro ⟨i, hi⟩, refine not_le_of_lt (h i) _, rw [←mk_out (f i), ←mk_out (g i)], exact ⟨function.embedding.of_surjective (λ x, G ⟨i, x⟩ i) hi⟩ }, choose C hC using hC, obtain ⟨⟨i, x⟩, h⟩ := hG C, exact hC i x (congr_fun h i), end namespace choice def lt (α β : Type u) : Type u := pprod (α ↪ β) ((β ↪ α) → false) noncomputable def lt.of_cardinal_lt : #α < #β → lt α β := λ ⟨f, h⟩, ⟨classical.choice f, λ f, h ⟨f⟩⟩ def lt.to_cardinal_lt : lt α β → #α < #β := λ ⟨f, h⟩, ⟨⟨f⟩, λ f, nonempty.rec h f⟩ axiom sum_lt_prod {α : Type u} (β₁ : α → Type v) (β₂ : α → Type v) : (Π a, lt (β₁ a) (β₂ a)) → lt (Σ a, β₁ a) (Π a, β₂ a) noncomputable example {α : Type u} (β₁ : α → Type v) (β₂ : α → Type v) : (Π a, lt (β₁ a) (β₂ a)) → lt (Σ a, β₁ a) (Π a, β₂ a) := begin intro h, replace h := λ a, lt.to_cardinal_lt (h a), replace h := cardinal.sum_lt_prod (λ a, #(β₁ a)) (λ a, #(β₂ a)) h, replace h := lt.of_cardinal_lt h, dsimp only at h, rcases h with ⟨⟨f, hf⟩, h⟩, refine ⟨⟨_, _⟩, _⟩, { rintros ⟨a₁, b₁⟩ a₂, have e := classical.choice (quotient.mk_out (β₁ a₁)), exact classical.choice (quotient.mk_out (β₂ a₂)) (f ⟨a₁, e.symm b₁⟩ a₂) }, { clear h, rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, replace h : f ⟨a₁, (classical.choice (quotient.mk_out (β₁ a₁))).symm b₁⟩ = f ⟨a₂, (classical.choice (quotient.mk_out (β₁ a₂))).symm b₂⟩, { exact funext (λ a, (classical.choice (quotient.mk_out (β₂ a))).injective (congr_fun h a)) }, specialize hf h, clear h, simp only at hf, rcases hf with ⟨rfl, h⟩, rw (classical.choice (quotient.mk_out (β₁ a₁))).symm.injective (eq_of_heq h) }, { clear hf f, refine λ h', h _, clear h, cases h' with f hf, refine ⟨_, _⟩, { intro g, cases f (λ a, classical.choice (quotient.mk_out (β₂ a)) (g a)) with a b, exact ⟨a, (classical.choice (quotient.mk_out (β₁ a))).symm b⟩ }, { intros g₁ g₂ h, dsimp only at h, let F : (Σ a, β₁ a) → Σ a, (#(β₁ a)).out := @sigma.rec α β₁ (λ x, Σ a, (#(β₁ a)).out) (λ a b, ⟨a, (classical.choice (quotient.mk_out (β₁ a))).symm b⟩), suffices h' : function.injective F, { exact funext (λ a, (classical.choice (quotient.mk_out (β₂ a))).injective (congr_fun (hf (h' h)) a)) }, rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, simp only [F] at h, rcases h with ⟨rfl, h⟩, rw (classical.choice (quotient.mk_out (β₁ a₁))).symm.injective (eq_of_heq h) } } end axiom not_not {p : Prop} : ¬¬p → p theorem choice {α : Type u} {β : α → Type v} : (∀ a, nonempty (β a)) → nonempty (Π a, β a) := begin intro h, suffices : Π a, lt pempty (β a), { cases sum_lt_prod (λ a, pempty) β this with f h, exact not_not (λ h', h ⟨λ f, false.elim (h' ⟨f⟩), λ f, false.elim (h' ⟨f⟩)⟩) }, intro a, specialize h a, refine ⟨⟨pempty.elim, λ x, pempty.elim x⟩, _⟩, rintro ⟨f, hf⟩, cases h with b, exact pempty.elim (f b) end theorem choice' {α : Sort u} {β : α → Sort v} : (∀ a, nonempty (β a)) → nonempty (Π a, β a) := begin intro h, suffices : ∀ a, nonempty (plift (β (plift.down a))), { cases choice this with h, exact ⟨λ a, (h ⟨a⟩).down⟩ }, rintro ⟨a⟩, cases h a with h, exact ⟨⟨h⟩⟩ end end choice end cardinal
62a306f68215948cfddf4b5f37652799f9db6524
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/223.lean
c44647b8a98f5dec0bbb097a84ee01079c847671
[ "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
359
lean
universe u v inductive Imf {α : Type u} {β : Type v} (f : α → β) : β → Type (max u v) | mk : (a : α) → Imf f (f a) def h {α β} {f : α → β} : {b : β} → Imf f b → α | _, Imf.mk a => a #print h infix:50 " ≅ " => HEq theorem ex : ∀ {α β : Sort u} (h : α = β) (a : α), cast h a ≅ a | α, _, rfl, a => HEq.refl a #print ex
189b191d4246fca7b1b1970a139b650ac2ed5a6a
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/valuation/basic.lean
e966286baa593f29b4f77ccd8ddc90c8c1da7d5f
[ "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
27,850
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Johan Commelin, Patrick Massot -/ import algebra.order.with_zero import algebra.punit_instances import ring_theory.ideal.operations /-! # The basics of valuation theory. The basic theory of valuations (non-archimedean norms) on a commutative ring, following T. Wedhorn's unpublished notes “Adic Spaces” ([wedhorn_adic]). The definition of a valuation we use here is Definition 1.22 of [wedhorn_adic]. A valuation on a ring `R` is a monoid homomorphism `v` to a linearly ordered commutative monoid with zero, that in addition satisfies the following two axioms: * `v 0 = 0` * `∀ x y, v (x + y) ≤ max (v x) (v y)` `valuation R Γ₀`is the type of valuations `R → Γ₀`, with a coercion to the underlying function. If `v` is a valuation from `R` to `Γ₀` then the induced group homomorphism `units(R) → Γ₀` is called `unit_map v`. The equivalence "relation" `is_equiv v₁ v₂ : Prop` defined in 1.27 of [wedhorn_adic] is not strictly speaking a relation, because `v₁ : valuation R Γ₁` and `v₂ : valuation R Γ₂` might not have the same type. This corresponds in ZFC to the set-theoretic difficulty that the class of all valuations (as `Γ₀` varies) on a ring `R` is not a set. The "relation" is however reflexive, symmetric and transitive in the obvious sense. Note that we use 1.27(iii) of [wedhorn_adic] as the definition of equivalence. The support of a valuation `v : valuation R Γ₀` is `supp v`. If `J` is an ideal of `R` with `h : J ⊆ supp v` then the induced valuation on R / J = `ideal.quotient J` is `on_quot v h`. ## Main definitions * `valuation R Γ₀`, the type of valuations on `R` with values in `Γ₀` * `valuation.is_equiv`, the heterogeneous equivalence relation on valuations * `valuation.supp`, the support of a valuation * `add_valuation R Γ₀`, the type of additive valuations on `R` with values in a linearly ordered additive commutative group with a top element, `Γ₀`. ## Implementation Details `add_valuation R Γ₀` is implemented as `valuation R (multiplicative Γ₀)ᵒᵈ`. ## TODO If ever someone extends `valuation`, we should fully comply to the `fun_like` by migrating the boilerplate lemmas to `valuation_class`. -/ open_locale classical big_operators noncomputable theory open function ideal variables {F R : Type*} -- This will be a ring, assumed commutative in some sections section variables (F R) (Γ₀ : Type*) [linear_ordered_comm_monoid_with_zero Γ₀] [ring R] /-- The type of `Γ₀`-valued valuations on `R`. When you extend this structure, make sure to extend `valuation_class`. -/ @[nolint has_inhabited_instance] structure valuation extends R →*₀ Γ₀ := (map_add_le_max' : ∀ x y, to_fun (x + y) ≤ max (to_fun x) (to_fun y)) /-- `valuation_class F α β` states that `F` is a type of valuations. You should also extend this typeclass when you extend `valuation`. -/ class valuation_class extends monoid_with_zero_hom_class F R Γ₀ := (map_add_le_max (f : F) (x y : R) : f (x + y) ≤ max (f x) (f y)) export valuation_class (map_add_le_max) instance [valuation_class F R Γ₀] : has_coe_t F (valuation R Γ₀) := ⟨λ f, { to_fun := f, map_one' := map_one f, map_zero' := map_zero f, map_mul' := map_mul f, map_add_le_max' := map_add_le_max f }⟩ end namespace valuation variables {Γ₀ : Type*} variables {Γ'₀ : Type*} variables {Γ''₀ : Type*} [linear_ordered_comm_monoid_with_zero Γ''₀] section basic variables [ring R] section monoid variables [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀] instance : valuation_class (valuation R Γ₀) R Γ₀ := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_mul := λ f, f.map_mul', map_one := λ f, f.map_one', map_zero := λ f, f.map_zero', map_add_le_max := λ f, f.map_add_le_max' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (valuation R Γ₀) (λ _, R → Γ₀) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe (v : valuation R Γ₀) : v.to_fun = v := rfl @[ext] lemma ext {v₁ v₂ : valuation R Γ₀} (h : ∀ r, v₁ r = v₂ r) : v₁ = v₂ := fun_like.ext _ _ h variables (v : valuation R Γ₀) {x y z : R} @[simp, norm_cast] lemma coe_coe : ⇑(v : R →*₀ Γ₀) = v := rfl @[simp] lemma map_zero : v 0 = 0 := v.map_zero' @[simp] lemma map_one : v 1 = 1 := v.map_one' @[simp] lemma map_mul : ∀ x y, v (x * y) = v x * v y := v.map_mul' @[simp] lemma map_add : ∀ x y, v (x + y) ≤ max (v x) (v y) := v.map_add_le_max' lemma map_add_le {x y g} (hx : v x ≤ g) (hy : v y ≤ g) : v (x + y) ≤ g := le_trans (v.map_add x y) $ max_le hx hy lemma map_add_lt {x y g} (hx : v x < g) (hy : v y < g) : v (x + y) < g := lt_of_le_of_lt (v.map_add x y) $ max_lt hx hy lemma map_sum_le {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hf : ∀ i ∈ s, v (f i) ≤ g) : v (∑ i in s, f i) ≤ g := begin refine finset.induction_on s (λ _, trans_rel_right (≤) v.map_zero zero_le') (λ a s has ih hf, _) hf, rw finset.forall_mem_insert at hf, rw finset.sum_insert has, exact v.map_add_le hf.1 (ih hf.2) end lemma map_sum_lt {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g ≠ 0) (hf : ∀ i ∈ s, v (f i) < g) : v (∑ i in s, f i) < g := begin refine finset.induction_on s (λ _, trans_rel_right (<) v.map_zero (zero_lt_iff.2 hg)) (λ a s has ih hf, _) hf, rw finset.forall_mem_insert at hf, rw finset.sum_insert has, exact v.map_add_lt hf.1 (ih hf.2) end lemma map_sum_lt' {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : 0 < g) (hf : ∀ i ∈ s, v (f i) < g) : v (∑ i in s, f i) < g := v.map_sum_lt (ne_of_gt hg) hf @[simp] lemma map_pow : ∀ x (n:ℕ), v (x^n) = (v x)^n := v.to_monoid_with_zero_hom.to_monoid_hom.map_pow /-- Deprecated. Use `fun_like.ext_iff`. -/ lemma ext_iff {v₁ v₂ : valuation R Γ₀} : v₁ = v₂ ↔ ∀ r, v₁ r = v₂ r := fun_like.ext_iff -- The following definition is not an instance, because we have more than one `v` on a given `R`. -- In addition, type class inference would not be able to infer `v`. /-- A valuation gives a preorder on the underlying ring. -/ def to_preorder : preorder R := preorder.lift v /-- If `v` is a valuation on a division ring then `v(x) = 0` iff `x = 0`. -/ @[simp] lemma zero_iff [nontrivial Γ₀] {K : Type*} [division_ring K] (v : valuation K Γ₀) {x : K} : v x = 0 ↔ x = 0 := v.to_monoid_with_zero_hom.map_eq_zero lemma ne_zero_iff [nontrivial Γ₀] {K : Type*} [division_ring K] (v : valuation K Γ₀) {x : K} : v x ≠ 0 ↔ x ≠ 0 := v.to_monoid_with_zero_hom.map_ne_zero theorem unit_map_eq (u : Rˣ) : (units.map (v : R →* Γ₀) u : Γ₀) = v u := rfl /-- A ring homomorphism `S → R` induces a map `valuation R Γ₀ → valuation S Γ₀`. -/ def comap {S : Type*} [ring S] (f : S →+* R) (v : valuation R Γ₀) : valuation S Γ₀ := { to_fun := v ∘ f, map_add_le_max' := λ x y, by simp only [comp_app, map_add, f.map_add], .. v.to_monoid_with_zero_hom.comp f.to_monoid_with_zero_hom, } @[simp] lemma comap_apply {S : Type*} [ring S] (f : S →+* R) (v : valuation R Γ₀) (s : S) : v.comap f s = v (f s) := rfl @[simp] lemma comap_id : v.comap (ring_hom.id R) = v := ext $ λ r, rfl lemma comap_comp {S₁ : Type*} {S₂ : Type*} [ring S₁] [ring S₂] (f : S₁ →+* S₂) (g : S₂ →+* R) : v.comap (g.comp f) = (v.comap g).comap f := ext $ λ r, rfl /-- A `≤`-preserving group homomorphism `Γ₀ → Γ'₀` induces a map `valuation R Γ₀ → valuation R Γ'₀`. -/ def map (f : Γ₀ →*₀ Γ'₀) (hf : monotone f) (v : valuation R Γ₀) : valuation R Γ'₀ := { to_fun := f ∘ v, map_add_le_max' := λ r s, calc f (v (r + s)) ≤ f (max (v r) (v s)) : hf (v.map_add r s) ... = max (f (v r)) (f (v s)) : hf.map_max, .. monoid_with_zero_hom.comp f v.to_monoid_with_zero_hom } /-- Two valuations on `R` are defined to be equivalent if they induce the same preorder on `R`. -/ def is_equiv (v₁ : valuation R Γ₀) (v₂ : valuation R Γ'₀) : Prop := ∀ r s, v₁ r ≤ v₁ s ↔ v₂ r ≤ v₂ s end monoid section group variables [linear_ordered_comm_group_with_zero Γ₀] {R} {Γ₀} (v : valuation R Γ₀) {x y z : R} @[simp] lemma map_inv {K : Type*} [division_ring K] (v : valuation K Γ₀) {x : K} : v x⁻¹ = (v x)⁻¹ := v.to_monoid_with_zero_hom.map_inv x @[simp] lemma map_zpow {K : Type*} [division_ring K] (v : valuation K Γ₀) {x : K} {n : ℤ} : v (x^n) = (v x)^n := v.to_monoid_with_zero_hom.map_zpow x n lemma map_units_inv (x : Rˣ) : v (x⁻¹ : Rˣ) = (v x)⁻¹ := v.to_monoid_with_zero_hom.to_monoid_hom.map_units_inv x @[simp] lemma map_neg (x : R) : v (-x) = v x := v.to_monoid_with_zero_hom.to_monoid_hom.map_neg x lemma map_sub_swap (x y : R) : v (x - y) = v (y - x) := v.to_monoid_with_zero_hom.to_monoid_hom.map_sub_swap x y lemma map_sub (x y : R) : v (x - y) ≤ max (v x) (v y) := calc v (x - y) = v (x + -y) : by rw [sub_eq_add_neg] ... ≤ max (v x) (v $ -y) : v.map_add _ _ ... = max (v x) (v y) : by rw map_neg lemma map_sub_le {x y g} (hx : v x ≤ g) (hy : v y ≤ g) : v (x - y) ≤ g := begin rw sub_eq_add_neg, exact v.map_add_le hx (le_trans (le_of_eq (v.map_neg y)) hy) end lemma map_add_of_distinct_val (h : v x ≠ v y) : v (x + y) = max (v x) (v y) := begin suffices : ¬v (x + y) < max (v x) (v y), from or_iff_not_imp_right.1 (le_iff_eq_or_lt.1 (v.map_add x y)) this, intro h', wlog vyx : v y < v x using x y, { apply lt_or_gt_of_ne h.symm }, { rw max_eq_left_of_lt vyx at h', apply lt_irrefl (v x), calc v x = v ((x+y) - y) : by simp ... ≤ max (v $ x + y) (v y) : map_sub _ _ _ ... < v x : max_lt h' vyx }, { apply this h.symm, rwa [add_comm, max_comm] at h' } end lemma map_add_eq_of_lt_right (h : v x < v y) : v (x + y) = v y := begin convert v.map_add_of_distinct_val _, { symmetry, rw max_eq_right_iff, exact le_of_lt h }, { exact ne_of_lt h } end lemma map_add_eq_of_lt_left (h : v y < v x) : v (x + y) = v x := begin rw add_comm, exact map_add_eq_of_lt_right _ h, end lemma map_eq_of_sub_lt (h : v (y - x) < v x) : v y = v x := begin have := valuation.map_add_of_distinct_val v (ne_of_gt h).symm, rw max_eq_right (le_of_lt h) at this, simpa using this end /-- The subgroup of elements whose valuation is less than a certain unit.-/ def lt_add_subgroup (v : valuation R Γ₀) (γ : Γ₀ˣ) : add_subgroup R := { carrier := {x | v x < γ}, zero_mem' := by { have h := units.ne_zero γ, contrapose! h, simpa using h }, add_mem' := λ x y x_in y_in, lt_of_le_of_lt (v.map_add x y) (max_lt x_in y_in), neg_mem' := λ x x_in, by rwa [set.mem_set_of_eq, map_neg] } end group end basic -- end of section namespace is_equiv variables [ring R] variables [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀] variables {v : valuation R Γ₀} variables {v₁ : valuation R Γ₀} {v₂ : valuation R Γ'₀} {v₃ : valuation R Γ''₀} @[refl] lemma refl : v.is_equiv v := λ _ _, iff.refl _ @[symm] lemma symm (h : v₁.is_equiv v₂) : v₂.is_equiv v₁ := λ _ _, iff.symm (h _ _) @[trans] lemma trans (h₁₂ : v₁.is_equiv v₂) (h₂₃ : v₂.is_equiv v₃) : v₁.is_equiv v₃ := λ _ _, iff.trans (h₁₂ _ _) (h₂₃ _ _) lemma of_eq {v' : valuation R Γ₀} (h : v = v') : v.is_equiv v' := by { subst h } lemma map {v' : valuation R Γ₀} (f : Γ₀ →*₀ Γ'₀) (hf : monotone f) (inf : injective f) (h : v.is_equiv v') : (v.map f hf).is_equiv (v'.map f hf) := let H : strict_mono f := hf.strict_mono_of_injective inf in λ r s, calc f (v r) ≤ f (v s) ↔ v r ≤ v s : by rw H.le_iff_le ... ↔ v' r ≤ v' s : h r s ... ↔ f (v' r) ≤ f (v' s) : by rw H.le_iff_le /-- `comap` preserves equivalence. -/ lemma comap {S : Type*} [ring S] (f : S →+* R) (h : v₁.is_equiv v₂) : (v₁.comap f).is_equiv (v₂.comap f) := λ r s, h (f r) (f s) lemma val_eq (h : v₁.is_equiv v₂) {r s : R} : v₁ r = v₁ s ↔ v₂ r = v₂ s := by simpa only [le_antisymm_iff] using and_congr (h r s) (h s r) lemma ne_zero (h : v₁.is_equiv v₂) {r : R} : v₁ r ≠ 0 ↔ v₂ r ≠ 0 := begin have : v₁ r ≠ v₁ 0 ↔ v₂ r ≠ v₂ 0 := not_iff_not_of_iff h.val_eq, rwa [v₁.map_zero, v₂.map_zero] at this, end end is_equiv -- end of namespace section lemma is_equiv_of_map_strict_mono [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀] [ring R] {v : valuation R Γ₀} (f : Γ₀ →*₀ Γ'₀) (H : strict_mono f) : is_equiv (v.map f (H.monotone)) v := λ x y, ⟨H.le_iff_le.mp, λ h, H.monotone h⟩ lemma is_equiv_of_val_le_one [linear_ordered_comm_group_with_zero Γ₀] [linear_ordered_comm_group_with_zero Γ'₀] {K : Type*} [division_ring K] (v : valuation K Γ₀) (v' : valuation K Γ'₀) (h : ∀ {x:K}, v x ≤ 1 ↔ v' x ≤ 1) : v.is_equiv v' := begin intros x y, by_cases hy : y = 0, { simp [hy, zero_iff], }, rw show y = 1 * y, by rw one_mul, rw [← (inv_mul_cancel_right₀ hy x)], iterate 2 {rw [v.map_mul _ y, v'.map_mul _ y]}, rw [v.map_one, v'.map_one], split; intro H, { apply mul_le_mul_right', replace hy := v.ne_zero_iff.mpr hy, replace H := le_of_le_mul_right hy H, rwa h at H, }, { apply mul_le_mul_right', replace hy := v'.ne_zero_iff.mpr hy, replace H := le_of_le_mul_right hy H, rwa h, }, end lemma is_equiv_iff_val_le_one [linear_ordered_comm_group_with_zero Γ₀] [linear_ordered_comm_group_with_zero Γ'₀] {K : Type*} [division_ring K] (v : valuation K Γ₀) (v' : valuation K Γ'₀) : v.is_equiv v' ↔ ∀ {x : K}, v x ≤ 1 ↔ v' x ≤ 1 := ⟨λ h x, by simpa using h x 1, is_equiv_of_val_le_one _ _⟩ lemma is_equiv_iff_val_eq_one [linear_ordered_comm_group_with_zero Γ₀] [linear_ordered_comm_group_with_zero Γ'₀] {K : Type*} [division_ring K] (v : valuation K Γ₀) (v' : valuation K Γ'₀) : v.is_equiv v' ↔ ∀ {x : K}, v x = 1 ↔ v' x = 1 := begin split, { intros h x, simpa using @is_equiv.val_eq _ _ _ _ _ _ v v' h x 1 }, { intros h, apply is_equiv_of_val_le_one, intros x, split, { intros hx, cases lt_or_eq_of_le hx with hx' hx', { have : v (1 + x) = 1, { rw ← v.map_one, apply map_add_eq_of_lt_left, simpa }, rw h at this, rw (show x = (-1) + (1 + x), by simp), refine le_trans (v'.map_add _ _) _, simp [this] }, { rw h at hx', exact le_of_eq hx' } }, { intros hx, cases lt_or_eq_of_le hx with hx' hx', { have : v' (1 + x) = 1, { rw ← v'.map_one, apply map_add_eq_of_lt_left, simpa }, rw ← h at this, rw (show x = (-1) + (1 + x), by simp), refine le_trans (v.map_add _ _) _, simp [this] }, { rw ← h at hx', exact le_of_eq hx' } } } end end section supp variables [comm_ring R] variables [linear_ordered_comm_monoid_with_zero Γ₀] [linear_ordered_comm_monoid_with_zero Γ'₀] variables (v : valuation R Γ₀) /-- The support of a valuation `v : R → Γ₀` is the ideal of `R` where `v` vanishes. -/ def supp : ideal R := { carrier := {x | v x = 0}, zero_mem' := map_zero v, add_mem' := λ x y hx hy, le_zero_iff.mp $ calc v (x + y) ≤ max (v x) (v y) : v.map_add x y ... ≤ 0 : max_le (le_zero_iff.mpr hx) (le_zero_iff.mpr hy), smul_mem' := λ c x hx, calc v (c * x) = v c * v x : map_mul v c x ... = v c * 0 : congr_arg _ hx ... = 0 : mul_zero _ } @[simp] lemma mem_supp_iff (x : R) : x ∈ supp v ↔ v x = 0 := iff.rfl -- @[simp] lemma mem_supp_iff' (x : R) : x ∈ (supp v : set R) ↔ v x = 0 := iff.rfl /-- The support of a valuation is a prime ideal. -/ instance [nontrivial Γ₀] [no_zero_divisors Γ₀] : ideal.is_prime (supp v) := ⟨λ (h : v.supp = ⊤), one_ne_zero $ show (1 : Γ₀) = 0, from calc 1 = v 1 : v.map_one.symm ... = 0 : show (1:R) ∈ supp v, by { rw h, trivial }, λ x y hxy, begin show v x = 0 ∨ v y = 0, change v (x * y) = 0 at hxy, rw [v.map_mul x y] at hxy, exact eq_zero_or_eq_zero_of_mul_eq_zero hxy end⟩ lemma map_add_supp (a : R) {s : R} (h : s ∈ supp v) : v (a + s) = v a := begin have aux : ∀ a s, v s = 0 → v (a + s) ≤ v a, { intros a' s' h', refine le_trans (v.map_add a' s') (max_le le_rfl _), simp [h'], }, apply le_antisymm (aux a s h), calc v a = v (a + s + -s) : by simp ... ≤ v (a + s) : aux (a + s) (-s) (by rwa ←ideal.neg_mem_iff at h) end /-- If `hJ : J ⊆ supp v` then `on_quot_val hJ` is the induced function on R/J as a function. Note: it's just the function; the valuation is `on_quot hJ`. -/ def on_quot_val {J : ideal R} (hJ : J ≤ supp v) : R ⧸ J → Γ₀ := λ q, quotient.lift_on' q v $ λ a b h, calc v a = v (b + -(-a + b)) : by simp ... = v b : v.map_add_supp b ((ideal.neg_mem_iff _).2 $ hJ h) /-- The extension of valuation v on R to valuation on R/J if J ⊆ supp v -/ def on_quot {J : ideal R} (hJ : J ≤ supp v) : valuation (R ⧸ J) Γ₀ := { to_fun := v.on_quot_val hJ, map_zero' := v.map_zero, map_one' := v.map_one, map_mul' := λ xbar ybar, quotient.ind₂' v.map_mul xbar ybar, map_add_le_max' := λ xbar ybar, quotient.ind₂' v.map_add xbar ybar } @[simp] lemma on_quot_comap_eq {J : ideal R} (hJ : J ≤ supp v) : (v.on_quot hJ).comap (ideal.quotient.mk J) = v := ext $ λ r, rfl lemma comap_supp {S : Type*} [comm_ring S] (f : S →+* R) : supp (v.comap f) = ideal.comap f v.supp := ideal.ext $ λ x, begin rw [mem_supp_iff, ideal.mem_comap, mem_supp_iff], refl, end lemma self_le_supp_comap (J : ideal R) (v : valuation (R ⧸ J) Γ₀) : J ≤ (v.comap (ideal.quotient.mk J)).supp := by { rw [comap_supp, ← ideal.map_le_iff_le_comap], simp } @[simp] lemma comap_on_quot_eq (J : ideal R) (v : valuation (R ⧸ J) Γ₀) : (v.comap (ideal.quotient.mk J)).on_quot (v.self_le_supp_comap J) = v := ext $ by { rintro ⟨x⟩, refl } /-- The quotient valuation on R/J has support supp(v)/J if J ⊆ supp v. -/ lemma supp_quot {J : ideal R} (hJ : J ≤ supp v) : supp (v.on_quot hJ) = (supp v).map (ideal.quotient.mk J) := begin apply le_antisymm, { rintro ⟨x⟩ hx, apply ideal.subset_span, exact ⟨x, hx, rfl⟩ }, { rw ideal.map_le_iff_le_comap, intros x hx, exact hx } end lemma supp_quot_supp : supp (v.on_quot le_rfl) = 0 := by { rw supp_quot, exact ideal.map_quotient_self _ } end supp -- end of section end valuation section add_monoid variables (R) [ring R] (Γ₀ : Type*) [linear_ordered_add_comm_monoid_with_top Γ₀] /-- The type of `Γ₀`-valued additive valuations on `R`. -/ @[nolint has_inhabited_instance] def add_valuation := valuation R (multiplicative Γ₀ᵒᵈ) end add_monoid namespace add_valuation variables {Γ₀ : Type*} {Γ'₀ : Type*} section basic section monoid variables [linear_ordered_add_comm_monoid_with_top Γ₀] [linear_ordered_add_comm_monoid_with_top Γ'₀] variables (R) (Γ₀) [ring R] /-- A valuation is coerced to the underlying function `R → Γ₀`. -/ instance : has_coe_to_fun (add_valuation R Γ₀) (λ _, R → Γ₀) := { coe := λ v, v.to_monoid_with_zero_hom.to_fun } variables {R} {Γ₀} (v : add_valuation R Γ₀) {x y z : R} section variables (f : R → Γ₀) (h0 : f 0 = ⊤) (h1 : f 1 = 0) variables (hadd : ∀ x y, min (f x) (f y) ≤ f (x + y)) (hmul : ∀ x y, f (x * y) = f x + f y) /-- An alternate constructor of `add_valuation`, that doesn't reference `multiplicative Γ₀ᵒᵈ` -/ def of : add_valuation R Γ₀ := { to_fun := f, map_one' := h1, map_zero' := h0, map_add_le_max' := hadd, map_mul' := hmul } variables {h0} {h1} {hadd} {hmul} {r : R} @[simp] theorem of_apply : (of f h0 h1 hadd hmul) r = f r := rfl /-- The `valuation` associated to an `add_valuation` (useful if the latter is constructed using `add_valuation.of`). -/ def valuation : valuation R (multiplicative Γ₀ᵒᵈ) := v @[simp] lemma valuation_apply (r : R) : v.valuation r = multiplicative.of_add (order_dual.to_dual (v r)) := rfl end @[simp] lemma map_zero : v 0 = ⊤ := v.map_zero @[simp] lemma map_one : v 1 = 0 := v.map_one @[simp] lemma map_mul : ∀ x y, v (x * y) = v x + v y := v.map_mul @[simp] lemma map_add : ∀ x y, min (v x) (v y) ≤ v (x + y) := v.map_add lemma map_le_add {x y g} (hx : g ≤ v x) (hy : g ≤ v y) : g ≤ v (x + y) := v.map_add_le hx hy lemma map_lt_add {x y g} (hx : g < v x) (hy : g < v y) : g < v (x + y) := v.map_add_lt hx hy lemma map_le_sum {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hf : ∀ i ∈ s, g ≤ v (f i)) : g ≤ v (∑ i in s, f i) := v.map_sum_le hf lemma map_lt_sum {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g ≠ ⊤) (hf : ∀ i ∈ s, g < v (f i)) : g < v (∑ i in s, f i) := v.map_sum_lt hg hf lemma map_lt_sum' {ι : Type*} {s : finset ι} {f : ι → R} {g : Γ₀} (hg : g < ⊤) (hf : ∀ i ∈ s, g < v (f i)) : g < v (∑ i in s, f i) := v.map_sum_lt' hg hf @[simp] lemma map_pow : ∀ x (n:ℕ), v (x^n) = n • (v x) := v.map_pow @[ext] lemma ext {v₁ v₂ : add_valuation R Γ₀} (h : ∀ r, v₁ r = v₂ r) : v₁ = v₂ := valuation.ext h lemma ext_iff {v₁ v₂ : add_valuation R Γ₀} : v₁ = v₂ ↔ ∀ r, v₁ r = v₂ r := valuation.ext_iff -- The following definition is not an instance, because we have more than one `v` on a given `R`. -- In addition, type class inference would not be able to infer `v`. /-- A valuation gives a preorder on the underlying ring. -/ def to_preorder : preorder R := preorder.lift v /-- If `v` is an additive valuation on a division ring then `v(x) = ⊤` iff `x = 0`. -/ @[simp] lemma top_iff [nontrivial Γ₀] {K : Type*} [division_ring K] (v : add_valuation K Γ₀) {x : K} : v x = ⊤ ↔ x = 0 := v.zero_iff lemma ne_top_iff [nontrivial Γ₀] {K : Type*} [division_ring K] (v : add_valuation K Γ₀) {x : K} : v x ≠ ⊤ ↔ x ≠ 0 := v.ne_zero_iff /-- A ring homomorphism `S → R` induces a map `add_valuation R Γ₀ → add_valuation S Γ₀`. -/ def comap {S : Type*} [ring S] (f : S →+* R) (v : add_valuation R Γ₀) : add_valuation S Γ₀ := v.comap f @[simp] lemma comap_id : v.comap (ring_hom.id R) = v := v.comap_id lemma comap_comp {S₁ : Type*} {S₂ : Type*} [ring S₁] [ring S₂] (f : S₁ →+* S₂) (g : S₂ →+* R) : v.comap (g.comp f) = (v.comap g).comap f := v.comap_comp f g /-- A `≤`-preserving, `⊤`-preserving group homomorphism `Γ₀ → Γ'₀` induces a map `add_valuation R Γ₀ → add_valuation R Γ'₀`. -/ def map (f : Γ₀ →+ Γ'₀) (ht : f ⊤ = ⊤) (hf : monotone f) (v : add_valuation R Γ₀) : add_valuation R Γ'₀ := v.map { to_fun := f, map_mul' := f.map_add, map_one' := f.map_zero, map_zero' := ht } (λ x y h, hf h) /-- Two additive valuations on `R` are defined to be equivalent if they induce the same preorder on `R`. -/ def is_equiv (v₁ : add_valuation R Γ₀) (v₂ : add_valuation R Γ'₀) : Prop := v₁.is_equiv v₂ end monoid section group variables [linear_ordered_add_comm_group_with_top Γ₀] [ring R] (v : add_valuation R Γ₀) {x y z : R} @[simp] lemma map_inv {K : Type*} [division_ring K] (v : add_valuation K Γ₀) {x : K} : v x⁻¹ = - (v x) := v.map_inv lemma map_units_inv (x : Rˣ) : v (x⁻¹ : Rˣ) = - (v x) := v.map_units_inv x @[simp] lemma map_neg (x : R) : v (-x) = v x := v.map_neg x lemma map_sub_swap (x y : R) : v (x - y) = v (y - x) := v.map_sub_swap x y lemma map_sub (x y : R) : min (v x) (v y) ≤ v (x - y) := v.map_sub x y lemma map_le_sub {x y g} (hx : g ≤ v x) (hy : g ≤ v y) : g ≤ v (x - y) := v.map_sub_le hx hy lemma map_add_of_distinct_val (h : v x ≠ v y) : v (x + y) = min (v x) (v y) := v.map_add_of_distinct_val h lemma map_eq_of_lt_sub (h : v x < v (y - x)) : v y = v x := v.map_eq_of_sub_lt h end group end basic namespace is_equiv variables [linear_ordered_add_comm_monoid_with_top Γ₀] [linear_ordered_add_comm_monoid_with_top Γ'₀] variables [ring R] variables {Γ''₀ : Type*} [linear_ordered_add_comm_monoid_with_top Γ''₀] variables {v : add_valuation R Γ₀} variables {v₁ : add_valuation R Γ₀} {v₂ : add_valuation R Γ'₀} {v₃ : add_valuation R Γ''₀} @[refl] lemma refl : v.is_equiv v := valuation.is_equiv.refl @[symm] lemma symm (h : v₁.is_equiv v₂) : v₂.is_equiv v₁ := h.symm @[trans] lemma trans (h₁₂ : v₁.is_equiv v₂) (h₂₃ : v₂.is_equiv v₃) : v₁.is_equiv v₃ := h₁₂.trans h₂₃ lemma of_eq {v' : add_valuation R Γ₀} (h : v = v') : v.is_equiv v' := valuation.is_equiv.of_eq h lemma map {v' : add_valuation R Γ₀} (f : Γ₀ →+ Γ'₀) (ht : f ⊤ = ⊤) (hf : monotone f) (inf : injective f) (h : v.is_equiv v') : (v.map f ht hf).is_equiv (v'.map f ht hf) := h.map { to_fun := f, map_mul' := f.map_add, map_one' := f.map_zero, map_zero' := ht } (λ x y h, hf h) inf /-- `comap` preserves equivalence. -/ lemma comap {S : Type*} [ring S] (f : S →+* R) (h : v₁.is_equiv v₂) : (v₁.comap f).is_equiv (v₂.comap f) := h.comap f lemma val_eq (h : v₁.is_equiv v₂) {r s : R} : v₁ r = v₁ s ↔ v₂ r = v₂ s := h.val_eq lemma ne_top (h : v₁.is_equiv v₂) {r : R} : v₁ r ≠ ⊤ ↔ v₂ r ≠ ⊤ := h.ne_zero end is_equiv section supp variables [linear_ordered_add_comm_monoid_with_top Γ₀] [linear_ordered_add_comm_monoid_with_top Γ'₀] variables [comm_ring R] variables (v : add_valuation R Γ₀) /-- The support of an additive valuation `v : R → Γ₀` is the ideal of `R` where `v x = ⊤` -/ def supp : ideal R := v.supp @[simp] lemma mem_supp_iff (x : R) : x ∈ supp v ↔ v x = ⊤ := v.mem_supp_iff x lemma map_add_supp (a : R) {s : R} (h : s ∈ supp v) : v (a + s) = v a := v.map_add_supp a h /-- If `hJ : J ⊆ supp v` then `on_quot_val hJ` is the induced function on R/J as a function. Note: it's just the function; the valuation is `on_quot hJ`. -/ def on_quot_val {J : ideal R} (hJ : J ≤ supp v) : (R ⧸ J) → Γ₀ := v.on_quot_val hJ /-- The extension of valuation v on R to valuation on R/J if J ⊆ supp v -/ def on_quot {J : ideal R} (hJ : J ≤ supp v) : add_valuation (R ⧸ J) Γ₀ := v.on_quot hJ @[simp] lemma on_quot_comap_eq {J : ideal R} (hJ : J ≤ supp v) : (v.on_quot hJ).comap (ideal.quotient.mk J) = v := v.on_quot_comap_eq hJ lemma comap_supp {S : Type*} [comm_ring S] (f : S →+* R) : supp (v.comap f) = ideal.comap f v.supp := v.comap_supp f lemma self_le_supp_comap (J : ideal R) (v : add_valuation (R ⧸ J) Γ₀) : J ≤ (v.comap (ideal.quotient.mk J)).supp := v.self_le_supp_comap J @[simp] lemma comap_on_quot_eq (J : ideal R) (v : add_valuation (R ⧸ J) Γ₀) : (v.comap (ideal.quotient.mk J)).on_quot (v.self_le_supp_comap J) = v := v.comap_on_quot_eq J /-- The quotient valuation on R/J has support supp(v)/J if J ⊆ supp v. -/ lemma supp_quot {J : ideal R} (hJ : J ≤ supp v) : supp (v.on_quot hJ) = (supp v).map (ideal.quotient.mk J) := v.supp_quot hJ lemma supp_quot_supp : supp (v.on_quot le_rfl) = 0 := v.supp_quot_supp end supp -- end of section attribute [irreducible] add_valuation end add_valuation
c3d2f27cee475e544fbc4c1265f7c4a64dfbf530
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/lint/type_classes_auto.lean
f0358411b90b05e9080a7419133b9aaaa0e926c3
[]
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
2,496
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.lint.basic import Mathlib.PostPort namespace Mathlib /-! # Linters about type classes This file defines several linters checking the correct usage of type classes and the appropriate definition of instances: * `instance_priority` ensures that blanket instances have low priority * `has_inhabited_instances` checks that every type has an `inhabited` instance * `impossible_instance` checks that there are no instances which can never apply * `incorrect_type_class_argument` checks that only type classes are used in instance-implicit arguments * `dangerous_instance` checks for instances that generate subproblems with metavariables * `fails_quickly` checks that type class resolution finishes quickly * `has_coe_variable` checks that there is no instance of type `has_coe α t` * `inhabited_nonempty` checks whether `[inhabited α]` arguments could be generalized to `[nonempty α]` * `decidable_classical` checks propositions for `[decidable_... p]` hypotheses that are not used in the statement, and could thus be removed by using `classical` in the proof. * `linter.has_coe_to_fun` checks whether necessary `has_coe_to_fun` instances are declared -/ /-- Pretty prints a list of arguments of a declaration. Assumes `l` is a list of argument positions and binders (or any other element that can be pretty printed). `l` can be obtained e.g. by applying `list.indexes_values` to a list obtained by `get_pi_binders`. -/ /-- checks whether an instance that always applies has priority ≥ 1000. -/ /-- There are places where typeclass arguments are specified with implicit `{}` brackets instead of the usual `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type of one of the other arguments. When they can be inferred from these other arguments, it is faster to use this method than to use type class inference. For example, when writing lemmas about `(f : α →+* β)`, it is faster to specify the fact that `α` and `β` are `semiring`s as `{rα : semiring α} {rβ : semiring β}` rather than the usual `[semiring α] [semiring β]`. -/ /-- Certain instances always apply during type-class resolution. For example, the instance end Mathlib
aea8732d3d0c89e31abec8d117a34a0c6efa306e
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Syntax/Translate/Tactic/Mathlib/Interactive.lean
cc9feae1a255100d88629a08b9d1557e9fe15261
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
9,596
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathport.Syntax.Translate.Tactic.Basic import Mathport.Syntax.Translate.Tactic.Lean3 open Lean namespace Mathport.Translate.Tactic open AST3 Mathport.Translate.Parser -- # tactic.interactive @[tr_tactic fconstructor] def trFConstructor : TacM Syntax.Tactic := `(tactic| fconstructor) @[tr_tactic try_for] def trTryFor : TacM Syntax.Tactic := do `(tactic| try_for $(← trExpr (← parse pExpr)) $(← trBlock (← itactic)):tacticSeq) @[tr_tactic substs] def trSubsts : TacM Syntax.Tactic := do `(tactic| substs $[$((← parse ident*).map mkIdent)]*) @[tr_tactic unfold_coes] def trUnfoldCoes : TacM Syntax.Tactic := do `(tactic| unfold_coes $(← trLoc (← parse location))?) @[tr_tactic unfold_wf] def trUnfoldWf : TacM Syntax.Tactic := `(tactic| unfold_wf) @[tr_tactic unfold_aux] def trUnfoldAux : TacM Syntax.Tactic := `(tactic| unfold_aux) @[tr_tactic recover] def trRecover : TacM Syntax.Tactic := return mkBlockTransform fun args => `(tactic| recover $args*) @[tr_tactic «continue»] def trContinue : TacM Syntax.Tactic := do `(tactic| continue $(← trBlock (← itactic)):tacticSeq) @[tr_tactic id] def trId : TacM Syntax.Tactic := do trIdTactic (← itactic) @[tr_tactic work_on_goal] def trWorkOnGoal : TacM Syntax.Tactic := do `(tactic| on_goal $(Quote.quote (← parse smallNat)) => $(← trBlock (← itactic)):tacticSeq) @[tr_tactic swap] def trSwap : TacM Syntax.Tactic := do let n ← (← expr?).mapM fun | ⟨_, AST3.Expr.nat n⟩ => pure n | _ => warn! "unsupported: weird nat" match n.getD 2 with | 1 => `(tactic| skip) | 2 => `(tactic| swap) | n => `(tactic| pick_goal $(Quote.quote n)) @[tr_tactic rotate] def trRotate : TacM Syntax.Tactic := do let n ← (← expr?).mapM fun | ⟨_, AST3.Expr.nat n⟩ => pure n | _ => warn! "unsupported: weird nat" match n.getD 1 with | 0 => `(tactic| skip) | 1 => `(tactic| rotate_left) | n => `(tactic| rotate_left $(Quote.quote n)) @[tr_tactic clear_] def trClear_ : TacM Syntax.Tactic := `(tactic| clear_) @[tr_tactic replace] def trReplace : TacM Syntax.Tactic := do let h := (← parse (ident)?).map mkIdent let ty ← (← parse (tk ":" *> pExpr)?).mapM (trExpr ·) match h, ← (← parse (tk ":=" *> pExpr)?).mapM (trExpr ·) with | none, none => `(tactic| replace $[: $ty]?) | some h, none => `(tactic| replace $h:ident $[: $ty]?) | none, some pr => `(tactic| replace $[: $ty]? := $pr) | some h, some pr => `(tactic| replace $h $[: $ty]? := $pr) @[tr_tactic classical] def trClassical : TacM Syntax.Tactic := do let force := (← parse (tk "!")?).isSome if force then `(tactic| classical!) else return mkBlockTransform fun args => `(tactic| classical $args*) @[tr_tactic generalize_hyp] def trGeneralizeHyp : TacM Syntax.Tactic := do let h := (← parse (ident)?).map mkIdent parse (tk ":") let (e, x) ← parse generalizeArg `(tactic| generalize $[$h :]? $(← trExpr e) = $(mkIdent x) $(← trLoc (← parse location))?) @[tr_tactic clean] def trClean : TacM Syntax.Tactic := do `(tactic| clean $(← trExpr (← parse pExpr))) @[tr_tactic refine_struct] def trRefineStruct : TacM Syntax.Tactic := do `(tactic| refine_struct $(← trExpr (← parse pExpr))) @[tr_tactic guard_hyp'] def trGuardHyp' : TacM Syntax.Tactic := do `(tactic| guard_hyp $(mkIdent (← parse ident)) :ₐ $(← trExpr (← parse (tk ":" *> pExpr)))) @[tr_tactic match_hyp] def trMatchHyp : TacM Syntax.Tactic := do let h := mkIdent (← parse ident) let ty ← trExpr (← parse (tk ":" *> pExpr)) let m ← liftM $ (← expr?).mapM trExpr `(tactic| match_hyp $[(m := $m)]? $h : $ty) @[tr_tactic guard_expr_strict] def trGuardExprStrict : TacM Syntax.Tactic := do let t ← expr! let p ← parse (tk ":=" *> pExpr) `(tactic| guard_expr $(← trExpr t):term =ₛ $(← trExpr p):term) @[tr_tactic guard_target_strict] def trGuardTargetStrict : TacM Syntax.Tactic := do `(tactic| guard_target =ₛ $(← trExpr (← parse pExpr))) @[tr_tactic guard_hyp_strict] def trGuardHypStrict : TacM Syntax.Tactic := do `(tactic| guard_hyp $(mkIdent (← parse ident)) :ₛ $(← trExpr (← parse (tk ":" *> pExpr)))) @[tr_tactic guard_hyp_nums] def trGuardHypNums : TacM Syntax.Tactic := do match (← expr!).kind.unparen with | AST3.Expr.nat n => `(tactic| guard_hyp_nums $(Quote.quote n)) | _ => warn! "unsupported: weird nat" @[tr_tactic guard_target_mod_implicit] def trGuardTargetModImplicit : TacM Syntax.Tactic := do `(tactic| guard_target = $(← trExpr (← parse pExpr))) @[tr_tactic guard_hyp_mod_implicit] def trGuardHypModImplicit : TacM Syntax.Tactic := do `(tactic| guard_hyp $(mkIdent (← parse ident)) : $(← trExpr (← parse (tk ":" *> pExpr)))) @[tr_tactic guard_tags] def trGuardTags : TacM Syntax.Tactic := do `(tactic| guard_tags $[$((← parse ident*).map mkIdent)]*) @[tr_tactic guard_proof_term] def trGuardProofTerm : TacM Syntax.Tactic := do `(tactic| guard_proof_term $(← trIdTactic (← itactic)) => $(← trExpr (← parse pExpr))) @[tr_tactic success_if_fail_with_msg] def trSuccessIfFailWithMsg : TacM Syntax.Tactic := do let t ← trBlock (← itactic) match (← expr!).kind.unparen with | AST3.Expr.string s => `(tactic| fail_if_success? $(Syntax.mkStrLit s) $t:tacticSeq) | _ => warn! "unsupported: weird string" @[tr_tactic field] def trField : TacM Syntax.Tactic := do `(tactic| field $(mkIdent (← parse ident)) => $(← trBlock (← itactic)):tacticSeq) @[tr_tactic have_field] def trHaveField : TacM Syntax.Tactic := `(tactic| have_field) @[tr_tactic apply_field] def trApplyField : TacM Syntax.Tactic := `(tactic| apply_field) @[tr_tactic apply_rules] def trApplyRules : TacM Syntax.Tactic := do let hs ← liftM $ (← parse optPExprList).mapM trExpr let hs := hs ++ (← parse ((tk "with" *> ident*) <|> pure #[])).map (mkIdent ·) let n ← (← expr?).mapM fun | ⟨_, AST3.Expr.nat n⟩ => pure n | _ => warn! "unsupported: weird nat" let mut cfg ← liftM $ (← expr?).mapM trExpr if let some n := n then match cfg.getD (← `({})) with | `(Parser.Term.structInst| { $field,* }) => cfg ← `(Parser.Term.structInst| { $field,*, maxDepth := $(quote n) }) | _ => warn!"unsupported configuration syntax, cannot add bound {n}" `(tactic| apply_rules $[(config := $cfg)]? [$[$hs:term],*]) @[tr_tactic h_generalize] def trHGeneralize : TacM Syntax.Tactic := do let rev ← parse (tk "!")? let h := (← parse (ident_)?).map trBinderIdent let (e, x) ← parse (tk ":") *> parse hGeneralizeArg let e ← trExpr e; let x := mkIdent x let eqsH := (← parse (tk "with" *> ident_)?).map trBinderIdent match rev with | none => `(tactic| h_generalize $[$h :]? $e = $x $[with $eqsH]?) | some _ => `(tactic| h_generalize! $[$h :]? $e = $x $[with $eqsH]?) @[tr_tactic guard_expr_eq'] def trGuardExprEq' : TacM Syntax.Tactic := do `(tactic| guard_expr $(← trExpr (← expr!)) =~ $(← trExpr (← parse (tk ":=" *> pExpr)))) @[tr_tactic guard_target'] def trGuardTarget' : TacM Syntax.Tactic := do `(tactic| guard_target =~ $(← trExpr (← parse pExpr))) @[tr_tactic triv] def trTriv : TacM Syntax.Tactic := `(tactic| triv) @[tr_tactic use] def trUse : TacM Syntax.Tactic := do `(tactic| use $(← liftM $ (← parse pExprListOrTExpr).mapM trExpr):term,*) @[tr_tactic clear_aux_decl] def trClearAuxDecl : TacM Syntax.Tactic := `(tactic| clear_aux_decl) attribute [tr_tactic change'] trChange open Mathlib.Tactic in @[tr_tactic set] def trSet : TacM Syntax.Tactic := do let hSimp := (← parse (tk "!")?).isSome let a ← parse ident let ty ← parse (tk ":" *> pExpr)? let val ← parse (tk ":=") *> parse pExpr let revName ← parse (tk "with" *> return (← (tk "<-")?, ← ident))? let (rev, name) := match revName with | none => (none, none) | some (rev, name) => (some (optTk rev.isSome), some (mkIdent name)) let ty ← ty.mapM (trExpr ·) let val ← trExpr val let args ← `(setArgsRest| $(mkIdent a):ident $[: $ty]? := $val $[with $[←%$rev]? $name]?) if hSimp then `(tactic| set! $args:setArgsRest) else `(tactic| set $args:setArgsRest) @[tr_tactic clear_except] def trClearExcept : TacM Syntax.Tactic := do `(tactic| clear* - $((← parse ident*).map mkIdent)*) @[tr_tactic extract_goal] def trExtractGoal : TacM Syntax.Tactic := do _ ← parse (tk "!")? let n := (← parse (ident)?).map mkIdent match ← parse (tk "with" *> ident*)? with | none => `(tactic| extract_goal* $(n)?) | some vs => if !vs.isEmpty then warn! "unsupported: extract_goal with <names>" `(tactic| extract_goal $(n)?) @[tr_tactic inhabit] def trInhabit : TacM Syntax.Tactic := do let t ← trExpr (← parse pExpr) `(tactic| inhabit $[$((← parse (ident)?).map mkIdent) :]? $t) @[tr_tactic revert_deps] def trRevertDeps : TacM Syntax.Tactic := do `(tactic| revert_deps $[$((← parse ident*).map mkIdent)]*) @[tr_tactic revert_after] def trRevertAfter : TacM Syntax.Tactic := do `(tactic| revert_after $(mkIdent (← parse ident))) @[tr_tactic revert_target_deps] def trRevertTargetDeps : TacM Syntax.Tactic := `(tactic| revert_target_deps) @[tr_tactic clear_value] def trClearValue : TacM Syntax.Tactic := do `(tactic| clear_value $[$((← parse ident*).map mkIdent)]*) attribute [tr_tactic generalize'] trGeneralize attribute [tr_tactic subst'] trSubst
865b4841c144ab459e88e666e7e7f0676d080de0
cb1829c15cd3d28210f93507f96dfb1f56ec0128
/theorem_proving/08-induction_and_recursion.lean
5b5061c6db3fd2607b0427b8ec6aa57e4f002dfc
[]
no_license
williamdemeo/LEAN_wjd
69f9f76e35092b89e4479a320be2fa3c18aed6fe
13826c75c06ef435166a26a72e76fe984c15bad7
refs/heads/master
1,609,516,630,137
1,518,123,893,000
1,518,123,893,000
97,740,278
2
0
null
null
null
null
UTF-8
Lean
false
false
1,327
lean
-- 8. Induction and Recursion #print "===========================================" #print "Section 8.1. Pattern Matching" #print " " namespace Sec_8_1 end Sec_8_1 #print "===========================================" #print "Section 8.2. Wildcards and Overlapping Patterns" #print " " namespace Sec_8_2 end Sec_8_2 #print "===========================================" #print "Section 8.3. Structural Recursion and Induction" #print " " namespace Sec_8_3 end Sec_8_3 #print "===========================================" #print "Section 8.4. Well-Founded Recursion and Induction" #print " " namespace Sec_8_4 end Sec_8_4 #print "===========================================" #print "Section 8.5. Mutual Recursion" #print " " namespace Sec_8_5 end Sec_8_5 #print "===========================================" #print "Section 8.6. Dependent Pattern Matching" #print " " namespace Sec_8_6 end Sec_8_6 #print "===========================================" #print "Section 8.7. Inaccessible Terms" #print " " namespace Sec_8_7 end Sec_8_7 #print "===========================================" #print "Section 8.8. Match Expressions" #print " " namespace Sec_8_8 end Sec_8_8 #print "===========================================" #print "Section 8.9. Exercises" #print " " namespace Sec_8_9 end Sec_8_9
01070dc2447ff8505d9c6453f75503dbef5f5d7e
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/normed_space/matrix_exponential.lean
9426abbcf06086cc1617ee43c1f0e3aad1fcf86f
[ "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
9,035
lean
/- Copyright (c) 2022 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import analysis.normed_space.exponential import analysis.matrix import linear_algebra.matrix.zpow import topology.uniform_space.matrix /-! # Lemmas about the matrix exponential In this file, we provide results about `exp` on `matrix`s over a topological or normed algebra. Note that generic results over all topological spaces such as `exp_zero` can be used on matrices without issue, so are not repeated here. The topological results specific to matrices are: * `matrix.exp_transpose` * `matrix.exp_conj_transpose` * `matrix.exp_diagonal` * `matrix.exp_block_diagonal` * `matrix.exp_block_diagonal'` Lemmas like `exp_add_of_commute` require a canonical norm on the type; while there are multiple sensible choices for the norm of a `matrix` (`matrix.normed_group`, `matrix.frobenius_normed_group`, `matrix.linfty_op_normed_group`), none of them are canonical. In an application where a particular norm is chosen using `local attribute [instance]`, then the usual lemmas about `exp` are fine. When choosing a norm is undesirable, the results in this file can be used. In this file, we copy across the lemmas about `exp`, but hide the requirement for a norm inside the proof. * `matrix.exp_add_of_commute` * `matrix.exp_sum_of_commute` * `matrix.exp_nsmul` * `matrix.is_unit_exp` * `matrix.exp_units_conj` * `matrix.exp_units_conj'` Additionally, we prove some results about `matrix.has_inv` and `matrix.div_inv_monoid`, as the results for general rings are instead stated about `ring.inverse`: * `matrix.exp_neg` * `matrix.exp_zsmul` * `matrix.exp_conj` * `matrix.exp_conj'` ## Implementation notes This file runs into some sharp edges on typeclass search in lean 3, especially regarding pi types. To work around this, we copy a handful of instances for when lean can't find them by itself. Hopefully we will be able to remove these in Lean 4. ## TODO * Show that `matrix.det (exp 𝕂 A) = exp 𝕂 (matrix.trace A)` ## References * https://en.wikipedia.org/wiki/Matrix_exponential -/ open_locale matrix big_operators section hacks_for_pi_instance_search /-- A special case of `pi.topological_ring` for when `R` is not dependently typed. -/ instance function.topological_ring (I : Type*) (R : Type*) [non_unital_ring R] [topological_space R] [topological_ring R] : topological_ring (I → R) := pi.topological_ring /-- A special case of `function.algebra` for when A is a `ring` not a `semiring` -/ instance function.algebra_ring (I : Type*) {R : Type*} (A : Type*) [comm_semiring R] [ring A] [algebra R A] : algebra R (I → A) := pi.algebra _ _ /-- A special case of `pi.algebra` for when `f = λ i, matrix (m i) (m i) A`. -/ instance pi.matrix_algebra (I R A : Type*) (m : I → Type*) [comm_semiring R] [semiring A] [algebra R A] [Π i, fintype (m i)] [Π i, decidable_eq (m i)] : algebra R (Π i, matrix (m i) (m i) A) := @pi.algebra I R (λ i, matrix (m i) (m i) A) _ _ (λ i, matrix.algebra) /-- A special case of `pi.topological_ring` for when `f = λ i, matrix (m i) (m i) A`. -/ instance pi.matrix_topological_ring (I A : Type*) (m : I → Type*) [ring A] [topological_space A] [topological_ring A] [Π i, fintype (m i)] : topological_ring (Π i, matrix (m i) (m i) A) := @pi.topological_ring _ (λ i, matrix (m i) (m i) A) _ _ (λ i, matrix.topological_ring) end hacks_for_pi_instance_search variables (𝕂 : Type*) {m n p : Type*} {n' : m → Type*} {𝔸 : Type*} namespace matrix section topological section ring variables [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] [Π i, fintype (n' i)] [Π i, decidable_eq (n' i)] [field 𝕂] [ring 𝔸] [topological_space 𝔸] [topological_ring 𝔸] [algebra 𝕂 𝔸] [t2_space 𝔸] lemma exp_diagonal (v : m → 𝔸) : exp 𝕂 (diagonal v) = diagonal (exp 𝕂 v) := by simp_rw [exp_eq_tsum, diagonal_pow, ←diagonal_smul, ←diagonal_tsum] lemma exp_block_diagonal (v : m → matrix n n 𝔸) : exp 𝕂 (block_diagonal v) = block_diagonal (exp 𝕂 v) := by simp_rw [exp_eq_tsum, ←block_diagonal_pow, ←block_diagonal_smul, ←block_diagonal_tsum] lemma exp_block_diagonal' (v : Π i, matrix (n' i) (n' i) 𝔸) : exp 𝕂 (block_diagonal' v) = block_diagonal' (exp 𝕂 v) := by simp_rw [exp_eq_tsum, ←block_diagonal'_pow, ←block_diagonal'_smul, ←block_diagonal'_tsum] lemma exp_conj_transpose [star_ring 𝔸] [has_continuous_star 𝔸] (A : matrix m m 𝔸) : exp 𝕂 Aᴴ = (exp 𝕂 A)ᴴ := (star_exp A).symm end ring section comm_ring variables [fintype m] [decidable_eq m] [field 𝕂] [comm_ring 𝔸] [topological_space 𝔸] [topological_ring 𝔸] [algebra 𝕂 𝔸] [t2_space 𝔸] lemma exp_transpose (A : matrix m m 𝔸) : exp 𝕂 Aᵀ = (exp 𝕂 A)ᵀ := by simp_rw [exp_eq_tsum, transpose_tsum, transpose_smul, transpose_pow] end comm_ring end topological section normed variables [is_R_or_C 𝕂] [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] [Π i, fintype (n' i)] [Π i, decidable_eq (n' i)] [normed_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] lemma exp_add_of_commute (A B : matrix m m 𝔸) (h : commute A B) : exp 𝕂 (A + B) = exp 𝕂 A ⬝ exp 𝕂 B := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact exp_add_of_commute h, end lemma exp_sum_of_commute {ι} (s : finset ι) (f : ι → matrix m m 𝔸) (h : ∀ (i ∈ s) (j ∈ s), commute (f i) (f j)) : exp 𝕂 (∑ i in s, f i) = s.noncomm_prod (λ i, exp 𝕂 (f i)) (λ i hi j hj, (h i hi j hj).exp 𝕂) := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact exp_sum_of_commute s f h, end lemma exp_nsmul (n : ℕ) (A : matrix m m 𝔸) : exp 𝕂 (n • A) = exp 𝕂 A ^ n := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact exp_nsmul n A, end lemma is_unit_exp (A : matrix m m 𝔸) : is_unit (exp 𝕂 A) := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact is_unit_exp _ A, end lemma exp_units_conj (U : (matrix m m 𝔸)ˣ) (A : matrix m m 𝔸) : exp 𝕂 (↑U ⬝ A ⬝ ↑(U⁻¹) : matrix m m 𝔸) = ↑U ⬝ exp 𝕂 A ⬝ ↑(U⁻¹) := begin letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact exp_units_conj _ U A, end lemma exp_units_conj' (U : (matrix m m 𝔸)ˣ) (A : matrix m m 𝔸) : exp 𝕂 (↑(U⁻¹) ⬝ A ⬝ U : matrix m m 𝔸) = ↑(U⁻¹) ⬝ exp 𝕂 A ⬝ U := exp_units_conj 𝕂 U⁻¹ A end normed section normed_comm variables [is_R_or_C 𝕂] [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] [Π i, fintype (n' i)] [Π i, decidable_eq (n' i)] [normed_comm_ring 𝔸] [normed_algebra 𝕂 𝔸] [complete_space 𝔸] lemma exp_neg (A : matrix m m 𝔸) : exp 𝕂 (-A) = (exp 𝕂 A)⁻¹ := begin rw nonsing_inv_eq_ring_inverse, letI : semi_normed_ring (matrix m m 𝔸) := matrix.linfty_op_semi_normed_ring, letI : normed_ring (matrix m m 𝔸) := matrix.linfty_op_normed_ring, letI : normed_algebra 𝕂 (matrix m m 𝔸) := matrix.linfty_op_normed_algebra, exact (ring.inverse_exp _ A).symm, end lemma exp_zsmul (z : ℤ) (A : matrix m m 𝔸) : exp 𝕂 (z • A) = exp 𝕂 A ^ z := begin obtain ⟨n, rfl | rfl⟩ := z.eq_coe_or_neg, { rw [zpow_coe_nat, coe_nat_zsmul, exp_nsmul] }, { have : is_unit (exp 𝕂 A).det := (matrix.is_unit_iff_is_unit_det _).mp (is_unit_exp _ _), rw [matrix.zpow_neg this, zpow_coe_nat, neg_smul, exp_neg, coe_nat_zsmul, exp_nsmul] }, end lemma exp_conj (U : matrix m m 𝔸) (A : matrix m m 𝔸) (hy : is_unit U) : exp 𝕂 (U ⬝ A ⬝ U⁻¹) = U ⬝ exp 𝕂 A ⬝ U⁻¹ := let ⟨u, hu⟩ := hy in hu ▸ by simpa only [matrix.coe_units_inv] using exp_units_conj 𝕂 u A lemma exp_conj' (U : matrix m m 𝔸) (A : matrix m m 𝔸) (hy : is_unit U) : exp 𝕂 (U⁻¹ ⬝ A ⬝ U) = U⁻¹ ⬝ exp 𝕂 A ⬝ U := let ⟨u, hu⟩ := hy in hu ▸ by simpa only [matrix.coe_units_inv] using exp_units_conj' 𝕂 u A end normed_comm end matrix
174ae604c52f78a4a46231f5b801e252bc21ba76
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/alias.lean
abc686de5d6b4b5c24d91eefff245ecfe16ae089
[]
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
846
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.Lean3Lib.data.buffer.parser import Mathlib.tactic.core import Mathlib.PostPort namespace Mathlib /-! # The `alias` command This file defines an `alias` command, which can be used to create copies of a theorem or definition with different names. Syntax: ```lean /-- doc string -/ alias my_theorem ← alias1 alias2 ... ``` This produces defs or theorems of the form: ```lean /-- doc string -/ /-- doc string -/ namespace tactic.alias /-- The `alias` command can be used to create copies of a theorem or definition with different names. Syntax: ```lean /-- doc string -/ /-- doc string -/ /-- doc string -/
7ad0c88701078dc8f76a7221fa9333e35a0d33c8
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/init/meta/simp_tactic.lean
f82cab4d7687832d2aa1adc3bbfb11cf034ea3d7
[ "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
18,046
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.meta.attribute init.meta.constructor_tactic import init.meta.relation_tactics init.meta.occurrences init.meta.quote open tactic meta constant simp_lemmas : Type meta constant simp_lemmas.mk : simp_lemmas meta constant simp_lemmas.join : simp_lemmas → simp_lemmas → simp_lemmas meta constant simp_lemmas.erase : simp_lemmas → list name → simp_lemmas meta constant simp_lemmas.mk_default_core : transparency → tactic simp_lemmas meta constant simp_lemmas.add_core : transparency → simp_lemmas → expr → tactic simp_lemmas meta constant simp_lemmas.add_simp_core : transparency → simp_lemmas → name → tactic simp_lemmas meta constant simp_lemmas.add_congr_core : transparency → simp_lemmas → name → tactic simp_lemmas meta def simp_lemmas.mk_default : tactic simp_lemmas := simp_lemmas.mk_default_core reducible meta def simp_lemmas.add : simp_lemmas → expr → tactic simp_lemmas := simp_lemmas.add_core reducible meta def simp_lemmas.add_simp : simp_lemmas → name → tactic simp_lemmas := simp_lemmas.add_simp_core reducible meta def simp_lemmas.add_congr : simp_lemmas → name → tactic simp_lemmas := simp_lemmas.add_congr_core reducible meta def simp_lemmas.append : simp_lemmas → list expr → tactic simp_lemmas | sls [] := return sls | sls (l::ls) := do new_sls ← simp_lemmas.add sls l, simp_lemmas.append new_sls ls /- (simp_lemmas.rewrite_core m s prove R e) apply a simplification lemma from 's' - 'prove' is used to discharge proof obligations. - 'R' is the equivalence relation being used (e.g., 'eq', 'iff') - 'e' is the expression to be "simplified" Result (new_e, pr) is the new expression 'new_e' and a proof (pr : e R new_e) -/ meta constant simp_lemmas.rewrite_core : transparency → simp_lemmas → tactic unit → name → expr → tactic (expr × expr) meta def simp_lemmas.rewrite : simp_lemmas → tactic unit → name → expr → tactic (expr × expr) := simp_lemmas.rewrite_core reducible /- (simp_lemmas.drewrite s e) tries to rewrite 'e' using only refl lemmas in 's' -/ meta constant simp_lemmas.drewrite_core : transparency → simp_lemmas → expr → tactic expr meta def simp_lemmas.drewrite : simp_lemmas → expr → tactic expr := simp_lemmas.drewrite_core reducible /- (Definitional) Simplify the given expression using *only* reflexivity equality lemmas from the given set of lemmas. The resulting expression is definitionally equal to the input. -/ meta constant simp_lemmas.dsimplify_core (max_steps : nat) (visit_instances : bool) : simp_lemmas → expr → tactic expr meta constant is_valid_simp_lemma_cnst : transparency → name → tactic bool meta constant is_valid_simp_lemma : transparency → expr → tactic bool def default_max_steps := 10000000 meta def simp_lemmas.dsimplify : simp_lemmas → expr → tactic expr := simp_lemmas.dsimplify_core default_max_steps ff meta constant simp_lemmas.pp : simp_lemmas → tactic format namespace tactic /- (get_eqn_lemmas_for deps d) returns the automatically generated equational lemmas for definition d. If deps is tt, then lemmas for automatically generated auxiliary declarations used to define d are also included. -/ meta constant get_eqn_lemmas_for : bool → name → tactic (list name) meta constant dsimplify_core /- The user state type. -/ {α : Type} /- Initial user data -/ (a : α) (max_steps : nat) /- If visit_instances = ff, then instance implicit arguments are not visited, but tactic will canonize them. -/ (visit_instances : bool) /- (pre a e) is invoked before visiting the children of subterm 'e', if it succeeds the result (new_a, new_e, flag) where - 'new_a' is the new value for the user data - 'new_e' is a new expression that must be definitionally equal to 'e', - 'flag' if tt 'new_e' children should be visited, and 'post' invoked. -/ (pre : α → expr → tactic (α × expr × bool)) /- (post a e) is invoked after visiting the children of subterm 'e', The output is similar to (pre a e), but the 'flag' indicates whether the new expression should be revisited or not. -/ (post : α → expr → tactic (α × expr × bool)) : expr → tactic (α × expr) meta def dsimplify (pre : expr → tactic (expr × bool)) (post : expr → tactic (expr × bool)) : expr → tactic expr := λ e, do (a, new_e) ← dsimplify_core () default_max_steps ff (λ u e, do r ← pre e, return (u, r)) (λ u e, do r ← post e, return (u, r)) e, return new_e meta constant dunfold_expr_core : transparency → expr → tactic expr meta def dunfold_expr : expr → tactic expr := dunfold_expr_core reducible meta constant unfold_projection_core : transparency → expr → tactic expr meta def unfold_projection : expr → tactic expr := unfold_projection_core reducible meta def dunfold_occs_core (m : transparency) (max_steps : nat) (occs : occurrences) (cs : list name) (e : expr) : tactic expr := let unfold (c : nat) (e : expr) : tactic (nat × expr × bool) := do guard (cs^.any e^.is_app_of), new_e ← dunfold_expr_core m e, if occs^.contains c then return (c+1, new_e, tt) else return (c+1, e, tt) in do (c, new_e) ← dsimplify_core 1 max_steps tt unfold (λ c e, failed) e, return new_e meta def dunfold_core (m : transparency) (max_steps : nat) (cs : list name) (e : expr) : tactic expr := let unfold (u : unit) (e : expr) : tactic (unit × expr × bool) := do guard (cs^.any e^.is_app_of), new_e ← dunfold_expr_core m e, return (u, new_e, tt) in do (c, new_e) ← dsimplify_core () max_steps tt (λ c e, failed) unfold e, return new_e meta def dunfold : list name → tactic unit := λ cs, target >>= dunfold_core reducible default_max_steps cs >>= change meta def dunfold_occs_of (occs : list nat) (c : name) : tactic unit := target >>= dunfold_occs_core reducible default_max_steps (occurrences.pos occs) [c] >>= change meta def dunfold_core_at (occs : occurrences) (cs : list name) (h : expr) : tactic unit := do num_reverted ← revert h, (expr.pi n bi d b : expr) ← target, new_d ← dunfold_occs_core reducible default_max_steps occs cs d, change $ expr.pi n bi new_d b, intron num_reverted meta def dunfold_at (cs : list name) (h : expr) : tactic unit := do num_reverted ← revert h, (expr.pi n bi d b : expr) ← target, new_d ← dunfold_core reducible default_max_steps cs d, change $ expr.pi n bi new_d b, intron num_reverted structure delta_config := (max_steps := default_max_steps) (visit_instances := tt) private meta def is_delta_target (e : expr) (cs : list name) : bool := cs^.any (λ c, if e^.is_app_of c then tt /- Exact match -/ else let f := e^.get_app_fn in /- f is an auxiliary constant generated when compiling c -/ f^.is_constant && f^.const_name^.is_internal && (f^.const_name^.get_prefix = c)) /- Delta reduce the given constant names -/ meta def delta_core (cfg : delta_config) (cs : list name) (e : expr) : tactic expr := let unfold (u : unit) (e : expr) : tactic (unit × expr × bool) := do guard (is_delta_target e cs), (expr.const f_name f_lvls) ← return e^.get_app_fn, env ← get_env, decl ← env^.get f_name, new_f ← decl^.instantiate_value_univ_params f_lvls, new_e ← beta (expr.mk_app new_f e^.get_app_args), return (u, new_e, tt) in do (c, new_e) ← dsimplify_core () cfg^.max_steps cfg^.visit_instances (λ c e, failed) unfold e, return new_e meta def delta (cs : list name) : tactic unit := target >>= delta_core {} cs >>= change meta def delta_at (cs : list name) (h : expr) : tactic unit := do num_reverted ← revert h, (expr.pi n bi d b : expr) ← target, new_d ← delta_core {} cs d, change $ expr.pi n bi new_d b, intron num_reverted structure simp_config := (max_steps : nat := default_max_steps) (contextual : bool := ff) (lift_eq : bool := tt) (canonize_instances : bool := tt) (canonize_proofs : bool := ff) (use_axioms : bool := tt) (zeta : bool := tt) meta constant simplify_core (c : simp_config) (s : simp_lemmas) (r : name) : expr → tactic (expr × expr) meta constant ext_simplify_core /- The user state type. -/ {α : Type} /- Initial user data -/ (a : α) (c : simp_config) /- Congruence and simplification lemmas. Remark: the simplification lemmas at not applied automatically like in the simplify_core tactic. the caller must use them at pre/post. -/ (s : simp_lemmas) /- Tactic for dischaging hypothesis in conditional rewriting rules. The argument 'α' is the current user state. -/ (prove : α → tactic α) /- (pre a S r s p e) is invoked before visiting the children of subterm 'e', 'r' is the simplification relation being used, 's' is the updated set of lemmas if 'contextual' is tt, 'p' is the "parent" expression (if there is one). if it succeeds the result is (new_a, new_e, new_pr, flag) where - 'new_a' is the new value for the user data - 'new_e' is a new expression s.t. 'e r new_e' - 'new_pr' is a proof for 'e r new_e', If it is none, the proof is assumed to be by reflexivity - 'flag' if tt 'new_e' children should be visited, and 'post' invoked. -/ (pre : α → simp_lemmas → name → option expr → expr → tactic (α × expr × option expr × bool)) /- (post a r s p e) is invoked after visiting the children of subterm 'e', The output is similar to (pre a r s p e), but the 'flag' indicates whether the new expression should be revisited or not. -/ (post : α → simp_lemmas → name → option expr → expr → tactic (α × expr × option expr × bool)) /- simplification relation -/ (r : name) : expr → tactic (α × expr × expr) meta def simplify (S : simp_lemmas) (e : expr) (cfg : simp_config := {}) : tactic (expr × expr) := do e_type ← infer_type e >>= whnf, simplify_core cfg S `eq e meta def replace_target (new_target : expr) (pr : expr) : tactic unit := do assert `htarget new_target, swap, ht ← get_local `htarget, mk_eq_mpr pr ht >>= exact meta def simplify_goal (S : simp_lemmas) (cfg : simp_config := {}) : tactic unit := do t ← target, (new_t, pr) ← simplify S t cfg, replace_target new_t pr meta def simp (cfg : simp_config := {}) : tactic unit := do S ← simp_lemmas.mk_default, simplify_goal S cfg >> try triv >> try (reflexivity reducible) meta def simp_using (hs : list expr) (cfg : simp_config := {}) : tactic unit := do S ← simp_lemmas.mk_default, S ← S^.append hs, simplify_goal S cfg >> try triv meta def dsimp_core (s : simp_lemmas) : tactic unit := target >>= s^.dsimplify >>= change meta def dsimp : tactic unit := simp_lemmas.mk_default >>= dsimp_core meta def dsimp_at_core (s : simp_lemmas) (h : expr) : tactic unit := do num_reverted : ℕ ← revert h, expr.pi n bi d b ← target, h_simp ← s^.dsimplify d, change $ expr.pi n bi h_simp b, intron num_reverted meta def dsimp_at (h : expr) : tactic unit := do s ← simp_lemmas.mk_default, dsimp_at_core s h private meta def is_equation : expr → bool | (expr.pi n bi d b) := is_equation b | e := match (expr.is_eq e) with (some a) := tt | none := ff end private meta def collect_simps : list expr → tactic (list expr) | [] := return [] | (h :: hs) := do result ← collect_simps hs, htype ← infer_type h >>= whnf, if is_equation htype then return (h :: result) else do pr ← is_prop htype, return $ if pr then (h :: result) else result meta def collect_ctx_simps : tactic (list expr) := local_context >>= collect_simps /-- Simplify target using all hypotheses in the local context. -/ meta def simp_using_hs (cfg : simp_config := {}) : tactic unit := do es ← collect_ctx_simps, simp_using es cfg meta def simph (cfg : simp_config := {}) := simp_using_hs cfg meta def intro1_aux : bool → list name → tactic expr | ff _ := intro1 | tt (n::ns) := intro n | _ _ := failed meta def simp_intro_aux (cfg : simp_config) (updt : bool) : simp_lemmas → bool → list name → tactic simp_lemmas | S tt [] := try (simplify_goal S cfg) >> return S | S use_ns ns := do t ← target, if t^.is_napp_of `not 1 then intro1_aux use_ns ns >> simp_intro_aux S use_ns ns^.tail else if t^.is_arrow then do d ← return t^.binding_domain, (new_d, h_d_eq_new_d) ← simplify S d cfg, h_d ← intro1_aux use_ns ns, h_new_d ← mk_eq_mp h_d_eq_new_d h_d, assertv_core h_d^.local_pp_name new_d h_new_d, h_new ← intro1, new_S ← if updt && is_equation new_d then S^.add h_new else return S, clear h_d, simp_intro_aux new_S use_ns ns else if t^.is_pi || t^.is_let then intro1_aux use_ns ns >> simp_intro_aux S use_ns ns^.tail else do new_t ← whnf t reducible, if new_t^.is_pi then change new_t >> simp_intro_aux S use_ns ns else try (simplify_goal S cfg) >> mcond (expr.is_pi <$> target) (simp_intro_aux S use_ns ns) (if use_ns ∧ ¬ns^.empty then failed else return S) meta def simp_intros_using (s : simp_lemmas) (cfg : simp_config := {}) : tactic unit := step $ simp_intro_aux cfg ff s ff [] meta def simph_intros_using (s : simp_lemmas) (cfg : simp_config := {}) : tactic unit := step $ do s ← collect_ctx_simps >>= s^.append, simp_intro_aux cfg tt s ff [] meta def simp_intro_lst_using (ns : list name) (s : simp_lemmas) (cfg : simp_config := {}) : tactic unit := step $ simp_intro_aux cfg ff s tt ns meta def simph_intro_lst_using (ns : list name) (s : simp_lemmas) (cfg : simp_config := {}) : tactic unit := step $ do s ← collect_ctx_simps >>= s^.append, simp_intro_aux cfg tt s tt ns meta def simp_at (h : expr) (extra_lemmas : list expr := []) (cfg : simp_config := {}) : tactic unit := do when (expr.is_local_constant h = ff) (fail "tactic simp_at failed, the given expression is not a hypothesis"), htype ← infer_type h, S ← simp_lemmas.mk_default, S ← S^.append extra_lemmas, (new_htype, heq) ← simplify S htype cfg, assert (expr.local_pp_name h) new_htype, mk_eq_mp heq h >>= exact, try $ clear h meta def simp_at_using_hs (h : expr) (extra_lemmas : list expr := []) (cfg : simp_config := {}) : tactic unit := do hs ← collect_ctx_simps, simp_at h (list.filter (≠ h) hs ++ extra_lemmas) cfg meta def simph_at (h : expr) (extra_lemmas : list expr := []) (cfg : simp_config := {}) : tactic unit := simp_at_using_hs h extra_lemmas cfg meta def mk_eq_simp_ext (simp_ext : expr → tactic (expr × expr)) : tactic unit := do (lhs, rhs) ← target >>= match_eq, (new_rhs, heq) ← simp_ext lhs, unify rhs new_rhs, exact heq /- Simp attribute support -/ meta def to_simp_lemmas : simp_lemmas → list name → tactic simp_lemmas | S [] := return S | S (n::ns) := do S' ← S^.add_simp n, to_simp_lemmas S' ns meta def mk_simp_attr (attr_name : name) : command := do t ← to_expr ``(caching_user_attribute simp_lemmas), v ← to_expr ``({name := %%(quote attr_name), descr := "simplifier attribute", mk_cache := λ ns, do {tactic.to_simp_lemmas simp_lemmas.mk ns}, dependencies := [`reducibility] } : caching_user_attribute simp_lemmas), add_decl (declaration.defn attr_name [] t v reducibility_hints.abbrev ff), attribute.register attr_name meta def get_user_simp_lemmas (attr_name : name) : tactic simp_lemmas := if attr_name = `default then simp_lemmas.mk_default else do cnst ← return (expr.const attr_name []), attr ← eval_expr (caching_user_attribute simp_lemmas) cnst, caching_user_attribute.get_cache attr meta def join_user_simp_lemmas_core : simp_lemmas → list name → tactic simp_lemmas | S [] := return S | S (attr_name::R) := do S' ← get_user_simp_lemmas attr_name, join_user_simp_lemmas_core (S^.join S') R meta def join_user_simp_lemmas : list name → tactic simp_lemmas | [] := simp_lemmas.mk_default | attr_names := join_user_simp_lemmas_core simp_lemmas.mk attr_names /- Normalize numerical expression, returns a pair (n, pr) where n is the resultant numeral, and pr is a proof that the input argument is equal to n. -/ meta constant norm_num : expr → tactic (expr × expr) meta def simplify_top_down {α} (a : α) (pre : α → expr → tactic (α × expr × expr)) (e : expr) (cfg : simp_config := {}) : tactic (α × expr × expr) := ext_simplify_core a cfg simp_lemmas.mk (λ _, failed) (λ a _ _ _ e, do (new_a, new_e, pr) ← pre a e, guard (¬ new_e =ₐ e), return (new_a, new_e, some pr, tt)) (λ _ _ _ _ _, failed) `eq e meta def simp_top_down (pre : expr → tactic (expr × expr)) (cfg : simp_config := {}) : tactic unit := do t ← target, (_, new_target, pr) ← simplify_top_down () (λ _ e, do (new_e, pr) ← pre e, return ((), new_e, pr)) t cfg, replace_target new_target pr meta def simplify_bottom_up {α} (a : α) (post : α → expr → tactic (α × expr × expr)) (e : expr) (cfg : simp_config := {}) : tactic (α × expr × expr) := ext_simplify_core a cfg simp_lemmas.mk (λ _, failed) (λ _ _ _ _ _, failed) (λ a _ _ _ e, do (new_a, new_e, pr) ← post a e, guard (¬ new_e =ₐ e), return (new_a, new_e, some pr, tt)) `eq e meta def simp_bottom_up (post : expr → tactic (expr × expr)) (cfg : simp_config := {}) : tactic unit := do t ← target, (_, new_target, pr) ← simplify_bottom_up () (λ _ e, do (new_e, pr) ← post e, return ((), new_e, pr)) t cfg, replace_target new_target pr end tactic export tactic (mk_simp_attr)
bc2fdb32bc2c678e937e06f6aa25562e5fa99a3c
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/tests/lean/run/PPTopDownAnalyze.lean
d7a001c02d6bbf3d04edb2477cc1988d55c77f03
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
13,640
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam -/ import Lean open Lean Lean.Meta Lean.Elab Lean.Elab.Term Lean.Elab.Command open Lean.PrettyPrinter def checkDelab (e : Expr) (tgt? : Option Term) (name? : Option Name := none) : TermElabM Unit := do let pfix := "[checkDelab" ++ (match name? with | some n => ("." ++ toString n) | none => "") ++ "]" if e.hasMVar then throwError "{pfix} original term has mvars, {e}" let stx ← delab e match tgt? with | some tgt => if toString (← PrettyPrinter.ppTerm stx) != toString (← PrettyPrinter.ppTerm tgt?.get!) then throwError "{pfix} missed target\n{← PrettyPrinter.ppTerm stx}\n!=\n{← PrettyPrinter.ppTerm tgt}\n\nExpr: {e}\nType: {← inferType e}" | _ => pure () let e' ← try let e' ← elabTerm stx (some (← inferType e)) synthesizeSyntheticMVarsNoPostponing let e' ← instantiateMVars e' -- let ⟨e', _⟩ ← levelMVarToParam e' throwErrorIfErrors pure e' catch ex => throwError "{pfix} failed to re-elaborate,\n{stx}\n{← ex.toMessageData.toString}" withTheReader Core.Context (fun ctx => { ctx with options := ctx.options.setBool `pp.all true }) do if not (← isDefEq e e') then println! "{pfix} {← inferType e} {← inferType e'}" throwError "{pfix} roundtrip not structurally equal\n\nOriginal: {e}\n\nSyntax: {stx}\n\nNew: {e'}" let e' ← instantiateMVars e' if e'.hasMVar then throwError "{pfix} elaborated term still has mvars\n\nSyntax: {stx}\n\nExpression: {e'}" syntax (name := testDelabTD) "#testDelab " term " expecting " term : command @[commandElab testDelabTD] def elabTestDelabTD : CommandElab | `(#testDelab $stx:term expecting $tgt:term) => liftTermElabM do withDeclName `delabTD do let e ← elabTerm stx none let e ← levelMVarToParam e let e ← instantiateMVars e checkDelab e (some tgt) | _ => throwUnsupportedSyntax syntax (name := testDelabTDN) "#testDelabN " ident : command @[commandElab testDelabTDN] def elabTestDelabTDN : CommandElab | `(#testDelabN $name:ident) => liftTermElabM do withDeclName `delabTD do let name := name.getId let [name] ← resolveGlobalConst (mkIdent name) | throwError "cannot resolve name" let some cInfo := (← getEnv).find? name | throwError "no decl for name" let some value ← pure cInfo.value? | throwError "decl has no value" modify fun s => { s with levelNames := cInfo.levelParams } withTheReader Core.Context (fun ctx => { ctx with currNamespace := name.getPrefix, openDecls := [] }) do checkDelab value none (some name) | _ => throwUnsupportedSyntax ------------------------------------------------- ------------------------------------------------- ------------------------------------------------- universe u v set_option pp.analyze true set_option pp.analyze.checkInstances true set_option pp.analyze.explicitHoles true set_option pp.proofs true #testDelab @Nat.brecOn (fun x => Nat) 0 (fun x ih => x) expecting Nat.brecOn (motive := fun x => Nat) 0 fun x ih => x #testDelab @Nat.brecOn (fun x => Nat → Nat) 0 (fun x ih => fun y => y + x) expecting Nat.brecOn (motive := fun x => Nat → Nat) 0 fun x ih y => y + x #testDelab @Nat.brecOn (fun x => Nat → Nat) 0 (fun x ih => fun y => y + x) 0 expecting Nat.brecOn (motive := fun x => Nat → Nat) 0 (fun x ih y => y + x) 0 #testDelab let xs := #[]; xs.push (5 : Nat) expecting let xs : Array Nat := #[]; Array.push xs 5 #testDelab let x := Nat.zero; x + Nat.zero expecting let x := Nat.zero; x + Nat.zero def fHole (α : Type) (x : α) : α := x #testDelab fHole Nat Nat.zero expecting fHole _ Nat.zero def fPoly {α : Type u} (x : α) : α := x #testDelab fPoly Nat.zero expecting fPoly Nat.zero #testDelab fPoly (id Nat.zero) expecting fPoly (id Nat.zero) def fPoly2 {α : Type u} {β : Type v} (x : α) : α := x #testDelab @fPoly2 _ (Type 3) Nat.zero expecting fPoly2 (β := Type 3) Nat.zero def fPolyInst {α : Type u} [Add α] : α → α → α := Add.add #testDelab @fPolyInst Nat ⟨Nat.add⟩ expecting fPolyInst def fPolyNotInst {α : Type u} (inst : Add α) : α → α → α := Add.add #testDelab @fPolyNotInst Nat ⟨Nat.add⟩ expecting fPolyNotInst { add := Nat.add } #testDelab (fun (x : Nat) => x) Nat.zero expecting (fun (x : Nat) => x) Nat.zero #testDelab (fun (α : Type) (x : α) => x) Nat Nat.zero expecting (fun (α : Type) (x : α) => x) _ Nat.zero #testDelab (fun {α : Type} (x : α) => x) Nat.zero expecting (fun {α : Type} (x : α) => x) Nat.zero #testDelab ((@Add.mk Nat Nat.add).1 : Nat → Nat → Nat) expecting Add.add class Foo (α : Type v) where foo : α instance : Foo Bool := ⟨true⟩ #testDelab @Foo.foo Bool ⟨true⟩ expecting Foo.foo #testDelab @Foo.foo Bool ⟨false⟩ expecting Foo.foo (self := { foo := false }) axiom wild {α : Type u} {f : α → Type v} {x : α} [_inst_1 : Foo (f x)] : Nat abbrev nat2bool : Nat → Type := fun _ => Bool #testDelab @wild Nat nat2bool Nat.zero ⟨false⟩ expecting wild (f := nat2bool) (x := Nat.zero) (_inst_1 := { foo := false }) #testDelab @wild Nat (fun (n : Nat) => Bool) Nat.zero ⟨false⟩ expecting wild (f := fun n => Bool) (x := Nat.zero) (_inst_1 := { foo := false }) def takesFooUnnamed {Impl : Type} (Expl : Type) [Foo Nat] (x : Impl) (y : Expl) : Impl × Expl := (x, y) #testDelab @takesFooUnnamed _ Nat (Foo.mk 7) false 5 expecting @takesFooUnnamed _ _ { foo := 7 } false 5 #testDelab (fun {α : Type u} (x : α) => x : ∀ {α : Type u}, α → α) expecting fun {α} x => x #testDelab (fun {α : Type} (x : α) => x) Nat.zero expecting (fun {α : Type} (x : α) => x) Nat.zero #testDelab (fun {α : Type} [Add α] (x : α) => x + x) (0 : Nat) expecting (fun {α : Type} [Add α] (x : α) => x + x) 0 #testDelab (fun {α : Type} [Add α] (x : α) => x + x) Nat.zero expecting (fun {α : Type} [Add α] (x : α) => x + x) Nat.zero #testDelab id id id id Nat.zero expecting id id id id Nat.zero def zzz : Unit := () def Z1.zzz : Unit := () def Z1.Z2.zzz : Unit := () namespace Z1.Z2 #testDelab _root_.zzz expecting _root_.zzz #testDelab Z1.zzz expecting Z1.zzz #testDelab zzz expecting zzz end Z1.Z2 #testDelab fun {σ : Type u} {m : Type u → Type v} [Monad m] {α : Type u} (f : σ → α × σ) (s : σ) => pure (f := m) (f s) expecting fun {σ} {m} [Monad m] {α} f s => pure (f s) set_option pp.analyze.trustSubst false in #testDelab (fun (x y z : Nat) (hxy : x = y) (hyz : x = z) => hxy ▸ hyz : ∀ (x y z : Nat), x = y → x = z → y = z) expecting fun x y z hxy hyz => Eq.rec (motive := fun x_1 h => x_1 = z) hyz hxy set_option pp.analyze.trustSubst true in #testDelab (fun (x y z : Nat) (hxy : x = y) (hyz : x = z) => hxy ▸ hyz : ∀ (x y z : Nat), x = y → x = z → y = z) expecting fun x y z hxy hyz => hxy ▸ hyz set_option pp.analyze.trustId true in #testDelab Sigma.mk (β := fun α => α) Bool true expecting { fst := _, snd := true } set_option pp.analyze.trustId false in #testDelab Sigma.mk (β := fun α => α) Bool true expecting Sigma.mk (β := fun α => α) _ true #testDelab let xs := #[true]; xs expecting let xs := #[true]; xs def fooReadGetModify : ReaderT Unit (StateT Unit IO) Unit := do let _ ← read let _ ← get modify fun s => s #testDelab (do discard read pure () : ReaderT Bool IO Unit) expecting do discard read pure () #testDelab ((do let ctx ← read let s ← get modify fun s => s : ReaderT Bool (StateT Bool IO) Unit)) expecting do let _ ← read let _ ← get modify fun s => s set_option pp.analyze.typeAscriptions true in #testDelab (fun (x : Unit) => @id (ReaderT Bool IO Bool) (do read : ReaderT Bool IO Bool)) () expecting (fun (x : Unit) => (id read : ReaderT Bool IO Bool)) () set_option pp.analyze.typeAscriptions false in #testDelab (fun (x : Unit) => @id (ReaderT Bool IO Bool) (do read : ReaderT Bool IO Bool)) () expecting (fun (x : Unit) => id read) () instance : CoeFun Bool (fun b => Bool → Bool) := { coe := fun b x => b && x } #testDelab fun (xs : List Nat) => xs ≠ xs expecting fun xs => xs ≠ xs structure S1 where x : Unit structure S2 where x : Unit #testDelab { x := () : S1 } expecting { x := () } #testDelab (fun (u : Unit) => { x := () : S2 }) () expecting (fun (u : Unit) => { x := () : S2 }) () #testDelab Eq.refl True expecting Eq.refl _ #testDelab (fun (u : Unit) => Eq.refl True) () expecting (fun (u : Unit) => Eq.refl True) () inductive NeedsAnalysis {α : Type} : Prop | mk : NeedsAnalysis set_option pp.proofs false in #testDelab @NeedsAnalysis.mk Unit expecting (_ : NeedsAnalysis (α := Unit)) set_option pp.proofs false in set_option pp.proofs.withType false in #testDelab @NeedsAnalysis.mk Unit expecting _ #testDelab ∀ (α : Type u) (vals vals_1 : List α), { data := vals : Array α } = { data := vals_1 : Array α } expecting ∀ (α : Type u) (vals vals_1 : List α), { data := vals : Array α } = { data := vals_1 } #testDelab (do let ctxCore ← readThe Core.Context; pure ctxCore.currNamespace : MetaM Name) expecting do let ctxCore ← readThe Core.Context pure ctxCore.currNamespace structure SubtypeLike1 {α : Sort u} (p : α → Prop) where #testDelab SubtypeLike1 fun (x : Nat) => x < 10 expecting SubtypeLike1 fun (x : Nat) => x < 10 #eval "prevent comment from parsing as part of previous expression" --Note: currently we do not try "bottom-upping" inside lambdas --(so we will always annotate the binder type) #testDelab SubtypeLike1 fun (x : Nat) => Nat.succ x = x expecting SubtypeLike1 fun (x : Nat) => Nat.succ x = x structure SubtypeLike3 {α β γ : Sort u} (p : α → β → γ → Prop) where #testDelab SubtypeLike3 fun (x y z : Nat) => x + y < z expecting SubtypeLike3 fun (x y z : Nat) => x + y < z structure SubtypeLike3Double {α β γ : Sort u} (p₁ : α → β → Prop) (p₂ : β → γ → Prop) where #testDelab SubtypeLike3Double (fun (x y : Nat) => x = y) (fun (y z : Nat) => y = z) expecting SubtypeLike3Double (fun (x y : Nat) => x = y) fun y (z : Nat) => y = z def takesStricts ⦃α : Type⦄ {β : Type} ⦃γ : Type⦄ : Unit := () #testDelab takesStricts expecting takesStricts #testDelab @takesStricts expecting takesStricts #testDelab @takesStricts Unit Unit expecting takesStricts (α := Unit) (β := Unit) #testDelab @takesStricts Unit Unit Unit expecting takesStricts (α := Unit) (β := Unit) (γ := Unit) def takesStrictMotive ⦃motive : Nat → Type⦄ {n : Nat} (x : motive n) : motive n := x #testDelab takesStrictMotive expecting takesStrictMotive #testDelab @takesStrictMotive (fun x => Unit) 0 expecting takesStrictMotive (motive := fun x => Unit) (n := 0) #testDelab @takesStrictMotive (fun x => Unit) 0 () expecting takesStrictMotive (motive := fun x => Unit) (n := 0) () def arrayMkInjEqSnippet := fun {α : Type} (xs : List α) => Eq.ndrec (motive := fun _ => (Array.mk xs = Array.mk xs)) (Eq.refl (Array.mk xs)) (rfl : xs = xs) #testDelabN arrayMkInjEqSnippet def typeAs (α : Type u) (a : α) := () set_option pp.analyze.explicitHoles false in #testDelab ∀ {α : Sort u} {β : α → Sort v} {f₁ f₂ : (x : α) → β x}, (∀ (x : α), f₁ x = f₂ _) → f₁ = f₂ expecting ∀ {α : Sort u} {β : α → Sort v} {f₁ f₂ : (x : α) → β x}, (∀ (x : α), f₁ x = f₂ x) → f₁ = f₂ set_option pp.analyze.trustSubtypeMk true in #testDelab fun (n : Nat) (val : List Nat) (property : List.length val = n) => List.length { val := val, property := property : { x : List Nat // List.length x = n } }.val = n expecting fun n val property => List.length { val := val, property := property : { x : List Nat // List.length x = n } }.val = n #testDelabN Nat.brecOn #testDelabN Nat.below #testDelabN Nat.mod_lt #testDelabN Array.qsort #testDelabN List.partition #testDelabN List.partitionAux #testDelabN StateT.modifyGet #testDelabN Nat.gcd_one_left #testDelabN List.hasDecidableLt #testDelabN Lean.Xml.parse #testDelabN Add.noConfusionType #testDelabN List.filterMapM.loop #testDelabN instMonadReaderOf #testDelabN instInhabitedPUnit #testDelabN Lean.Syntax.getOptionalIdent? #testDelabN Lean.Meta.ppExpr #testDelabN MonadLift.noConfusion #testDelabN MonadLift.noConfusionType #testDelabN MonadExcept.noConfusion #testDelabN MonadFinally.noConfusion #testDelabN Lean.Elab.InfoTree.goalsAt?.match_1 #testDelabN Array.mk.injEq #testDelabN Lean.PrefixTree.empty #testDelabN Lean.PersistentHashMap.getCollisionNodeSize.match_1 #testDelabN Lean.HashMap.size.match_1 #testDelabN and_false -- TODO: this one prints out a structure instance with keyword field `end` set_option pp.structureInstances false in #testDelabN Lean.Server.FileWorker.handlePlainTermGoal -- TODO: this one desugars to a `doLet` in an assignment set_option pp.notation false in #testDelabN Lean.Server.FileWorker.handlePlainGoal -- TODO: this error occurs because we use a term's type to determine `blockImplicit` (@), -- whereas we should actually use the expected type based on the function being applied. -- #testDelabN HEq.subst -- TODO: this error occurs because it cannot solve the universe constraints -- (unclear if it is too few or too many annotations) -- #testDelabN ExceptT.seqRight_eq -- TODO: this error occurs because a function has explicit binders while its type has -- implicit binders. This may be an issue in the elaborator. -- #testDelabN Char.eqOfVeq
aafd34db415607e450056b55fa801e8562d1bed4
33340b3a23ca62ef3c8a7f6a2d4e14c07c6d3354
/lia/bval.lean
fa9fa77d0d3de52edbaf2e92e35fa8facdc31daa
[]
no_license
lclem/cooper
79554e72ced343c64fed24b2d892d24bf9447dfe
812afc6b158821f2e7dac9c91d3b6123c7a19faf
refs/heads/master
1,607,554,257,488
1,578,694,133,000
1,578,694,133,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
967
lean
import .qfree def bval : formula → bool | ⊤' := tt | ⊥' := ff | (A' a) := a.eval [] | (p ∧' q) := bval p && bval q | (p ∨' q) := bval p || bval q | (¬' p) := bnot (bval p) | (∃' p) := ff lemma bval_not {p} : bval (¬' p) = bnot (bval p) := rfl lemma bnot_eq_tt_iff_ne_tt {b : bool} : bnot b = tt ↔ ¬ (b = tt) := by simp def bval_eq_tt_iff : ∀ {p : formula}, qfree p → (bval p = tt ↔ p.eval []) | ⊤' h := begin constructor; intro h; trivial end | ⊥' h := begin constructor; intro h; cases h end | (A' a) h := begin simp [bval, formula.eval, atom.eval] end | (p ∧' q) h := begin cases h with hp hq, simp [bval, formula.eval, bval_eq_tt_iff hp, bval_eq_tt_iff hq] end | (p ∨' q) h := begin cases h with hp hq, simp [bval, formula.eval, bval_eq_tt_iff hp, bval_eq_tt_iff hq] end | (¬' p) h := by rw [ bval_not, eval_not, bnot_eq_tt_iff_ne_tt, @bval_eq_tt_iff p h ] | (∃' p) h := by cases h
b78096e7d6dac339952d13422d2f5e3418be8680
b074a51e20fdb737b2d4c635dd292fc54685e010
/src/category_theory/adjunction/fully_faithful.lean
baca12b07806d2520175abf0a0fb12d67e88cb93
[ "Apache-2.0" ]
permissive
minchaowu/mathlib
2daf6ffdb5a56eeca403e894af88bcaaf65aec5e
879da1cf04c2baa9eaa7bd2472100bc0335e5c73
refs/heads/master
1,609,628,676,768
1,564,310,105,000
1,564,310,105,000
99,461,307
0
0
null
null
null
null
UTF-8
Lean
false
false
2,273
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.yoneda 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] include 𝒞 𝒟 variables {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) -- Lemma 4.5.13 from Riehl -- Proof in https://stacks.math.columbia.edu/tag/0036 -- or at https://math.stackexchange.com/a/2727177 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) { inv := { app := λ Y f, L.preimage ((h.hom_equiv (unop Y) (L.obj X)).symm f) }, inv_hom_id' := begin ext1, ext1, dsimp, simp only [adjunction.hom_equiv_counit, preimage_comp, preimage_map, category.assoc], rw ←h.unit_naturality, simp, end, hom_inv_id' := begin ext1, ext1, dsimp, apply L.injectivity, simp, end }. 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 { inv := { app := λ Y f, R.preimage ((h.hom_equiv (R.obj X) Y) f) }, inv_hom_id' := begin ext1, ext1, dsimp, simp only [adjunction.hom_equiv_unit, preimage_comp, preimage_map], rw ←h.counit_naturality, simp, end, hom_inv_id' := begin ext1, ext1, dsimp, apply R.injectivity, simp, end } -- TODO also prove the converses? -- def L_full_of_unit_is_iso [is_iso (adjunction.unit h)] : full L := sorry -- def L_faithful_of_unit_is_iso [is_iso (adjunction.unit h)] : faithful L := sorry -- def R_full_of_counit_is_iso [is_iso (adjunction.counit h)] : full R := sorry -- def R_faithful_of_counit_is_iso [is_iso (adjunction.counit h)] : faithful R := sorry -- TODO also do the statements from Riehl 4.5.13 for full and faithful separately? end category_theory
31773bc5df35fcaf353772e4d45d63398a8f1002
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/measure_theory/l1_space.lean
7f76436ba61465f9b431aba8e7e45060d3bd69ce
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
29,485
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.ae_eq_fun /-! # Integrable functions and `L¹` space In the first part of this file, the predicate `integrable` is defined and basic properties of integrable functions are proved. In the second part, the space `L¹` of equivalence classes of integrable functions under the relation of being almost everywhere equal is defined as a subspace of the space `L⁰`. See the file `src/measure_theory/ae_eq_fun.lean` for information on `L⁰` space. ## Notation * `α →₁ β` is the type of `L¹` space, where `α` is a `measure_space` and `β` is a `normed_group` with a `second_countable_topology`. `f : α →ₘ β` is a "function" in `L¹`. In comments, `[f]` is also used to denote an `L¹` function. `₁` can be typed as `\1`. ## Main definitions * Let `f : α → β` be a function, where `α` is a `measure_space` and `β` a `normed_group`. Then `f` is called `integrable` if `(∫⁻ a, nnnorm (f a)) < ⊤` holds. * The space `L¹` is defined as a subspace of `L⁰` : An `ae_eq_fun` `[f] : α →ₘ β` is in the space `L¹` if `edist [f] 0 < ⊤`, which means `(∫⁻ a, edist (f a) 0) < ⊤` if we expand the definition of `edist` in `L⁰`. ## Main statements `L¹`, as a subspace, inherits most of the structures of `L⁰`. ## Implementation notes Maybe `integrable f` should be mean `(∫⁻ a, edist (f a) 0) < ⊤`, so that `integrable` and `ae_eq_fun.integrable` are more aligned. But in the end one can use the lemma `lintegral_nnnorm_eq_lintegral_edist : (∫⁻ a, nnnorm (f a)) = (∫⁻ a, edist (f a) 0)` to switch the two forms. ## Tags integrable, function space, l1 -/ noncomputable theory open_locale classical topological_space namespace measure_theory open set filter topological_space ennreal emetric open_locale big_operators universes u v w variables {α : Type u} [measurable_space α] {μ ν : measure α} variables {β : Type v} [normed_group β] {γ : Type w} [normed_group γ] /-- `integrable f μ` means that the integral `∫⁻ a, ∥f a∥ ∂μ` is finite; `integrable f` means `integrable f volume`. -/ def integrable (f : α → β) (μ : measure α . volume_tac) : Prop := ∫⁻ a, nnnorm (f a) ∂μ < ⊤ lemma integrable_iff_norm (f : α → β) : integrable f μ ↔ ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ < ⊤ := by simp only [integrable, of_real_norm_eq_coe_nnnorm] lemma integrable_iff_edist (f : α → β) : integrable f μ ↔ ∫⁻ a, edist (f a) 0 ∂μ < ⊤ := by simp only [integrable_iff_norm, edist_dist, dist_zero_right] lemma integrable_iff_of_real {f : α → ℝ} (h : 0 ≤ᵐ[μ] f) : integrable f μ ↔ ∫⁻ a, ennreal.of_real (f a) ∂μ < ⊤ := have lintegral_eq : ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ = ∫⁻ a, ennreal.of_real (f a) ∂μ := begin refine lintegral_congr_ae (h.mono $ λ a h, _), rwa [real.norm_eq_abs, abs_of_nonneg] end, by rw [integrable_iff_norm, lintegral_eq] lemma integrable.mono {f : α → β} {g : α → γ} (hg : integrable g μ) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ ∥g a∥) : integrable f μ := begin simp only [integrable_iff_norm] at *, calc ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ ≤ ∫⁻ (a : α), (ennreal.of_real ∥g a∥) ∂μ : lintegral_mono_ae (h.mono $ assume a h, of_real_le_of_real h) ... < ⊤ : hg end lemma integrable.congr {f g : α → β} (hf : integrable f μ) (h : f =ᵐ[μ] g) : integrable g μ := hf.mono $ h.rw (λ a b, ∥b∥ ≤ ∥f a∥) (eventually_le.refl _ $ λ x, ∥f x∥) lemma integrable_congr {f g : α → β} (h : f =ᵐ[μ] g) : integrable f μ ↔ integrable g μ := ⟨λ hf, hf.congr h, λ hg, hg.congr h.symm⟩ lemma integrable_const {c : β} : integrable (λ x : α, c) μ ↔ c = 0 ∨ μ univ < ⊤ := begin simp only [integrable, lintegral_const], by_cases hc : c = 0, { simp [hc] }, { simp only [hc, false_or], refine ⟨λ h, _, λ h, mul_lt_top coe_lt_top h⟩, replace h := mul_lt_top (@coe_lt_top $ (nnnorm c)⁻¹) h, rwa [← mul_assoc, ← coe_mul, _root_.inv_mul_cancel, coe_one, one_mul] at h, rwa [ne.def, nnnorm_eq_zero] } end lemma integrable.mono_meas {f : α → β} (h : integrable f ν) (hμ : μ ≤ ν) : integrable f μ := lt_of_le_of_lt (lintegral_mono' hμ (le_refl _)) h lemma integrable.add_meas {f : α → β} (hμ : integrable f μ) (hν : integrable f ν) : integrable f (μ + ν) := begin simp only [integrable, lintegral_add_meas] at *, exact add_lt_top.2 ⟨hμ, hν⟩ end lemma integrable.left_of_add_meas {f : α → β} (h : integrable f (μ + ν)) : integrable f μ := h.mono_meas $ measure.le_add_right $ le_refl _ lemma integrable.right_of_add_meas {f : α → β} (h : integrable f (μ + ν)) : integrable f ν := h.mono_meas $ measure.le_add_left $ le_refl _ lemma integrable.smul_meas {f : α → β} (h : integrable f μ) {c : ennreal} (hc : c < ⊤) : integrable f (c • μ) := begin simp only [integrable, lintegral_smul_meas] at *, exact mul_lt_top hc h end lemma integrable.mono_set {f : α → β} {s t : set α} (h : integrable f (μ.restrict t)) (hst : s ⊆ t) : integrable f (μ.restrict s) := h.mono_meas $ measure.restrict_mono hst (le_refl μ) lemma integrable.union {f : α → β} {s t : set α} (hs : integrable f (μ.restrict s)) (ht : integrable f (μ.restrict t)) : integrable f (μ.restrict (s ∪ t)) := (hs.add_meas ht).mono_meas $ measure.restrict_union_le _ _ lemma lintegral_nnnorm_eq_lintegral_edist (f : α → β) : ∫⁻ a, nnnorm (f a) ∂μ = ∫⁻ a, edist (f a) 0 ∂μ := by simp only [edist_eq_coe_nnnorm] lemma lintegral_norm_eq_lintegral_edist (f : α → β) : ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ = ∫⁻ a, edist (f a) 0 ∂μ := by simp only [of_real_norm_eq_coe_nnnorm, edist_eq_coe_nnnorm] lemma lintegral_edist_triangle [second_countable_topology β] [measurable_space β] [opens_measurable_space β] {f g h : α → β} (hf : measurable f) (hg : measurable g) (hh : measurable h) : ∫⁻ a, edist (f a) (g a) ∂μ ≤ ∫⁻ a, edist (f a) (h a) ∂μ + ∫⁻ a, edist (g a) (h a) ∂μ := begin rw ← lintegral_add (hf.edist hh) (hg.edist hh), refine lintegral_mono (λ a, _), apply edist_triangle_right end lemma lintegral_edist_lt_top [second_countable_topology β] [measurable_space β] [opens_measurable_space β] {f g : α → β} (hfm : measurable f) (hfi : integrable f μ) (hgm : measurable g) (hgi : integrable g μ) : ∫⁻ a, edist (f a) (g a) ∂μ < ⊤ := lt_of_le_of_lt (lintegral_edist_triangle hfm hgm (measurable_const : measurable (λa, (0 : β)))) (ennreal.add_lt_top.2 $ by { split; rw ← integrable_iff_edist; assumption }) lemma lintegral_nnnorm_zero : ∫⁻ a : α, nnnorm (0 : β) ∂μ = 0 := by simp variables (α β μ) @[simp] lemma integrable_zero : integrable (λa:α, (0:β)) μ := by simp [integrable] variables {α β μ} lemma lintegral_nnnorm_add [measurable_space β] [opens_measurable_space β] [measurable_space γ] [opens_measurable_space γ] {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : ∫⁻ a, nnnorm (f a) + nnnorm (g a) ∂μ = ∫⁻ a, nnnorm (f a) ∂μ + ∫⁻ a, nnnorm (g a) ∂μ := lintegral_add hf.ennnorm hg.ennnorm lemma integrable.add [measurable_space β] [opens_measurable_space β] {f g : α → β} (hfm : measurable f) (hfi : integrable f μ) (hgm : measurable g) (hgi : integrable g μ) : integrable (f + g) μ := calc ∫⁻ a, nnnorm (f a + g a) ∂μ ≤ ∫⁻ a, nnnorm (f a) + nnnorm (g a) ∂μ : lintegral_mono (assume a, by { simp only [← coe_add, coe_le_coe], exact nnnorm_add_le _ _ }) ... = _ : lintegral_nnnorm_add hfm hgm ... < ⊤ : add_lt_top.2 ⟨hfi, hgi⟩ lemma integrable_finset_sum {ι} [measurable_space β] [borel_space β] [second_countable_topology β] (s : finset ι) {f : ι → α → β} (hfm : ∀ i, measurable (f i)) (hfi : ∀ i, integrable (f i) μ) : integrable (λ a, ∑ i in s, f i a) μ := begin refine finset.induction_on s _ _, { simp only [finset.sum_empty, integrable_zero] }, { assume i s his ih, simp only [his, finset.sum_insert, not_false_iff], refine (hfi _).add (hfm _) (s.measurable_sum hfm) ih } end lemma lintegral_nnnorm_neg {f : α → β} : ∫⁻ a, nnnorm ((-f) a) ∂μ = ∫⁻ a, nnnorm (f a) ∂μ := by simp only [pi.neg_apply, nnnorm_neg] lemma integrable.neg {f : α → β} (hfi : integrable f μ) : integrable (-f) μ := calc _ = _ : lintegral_nnnorm_neg ... < ⊤ : hfi @[simp] lemma integrable_neg_iff {f : α → β} : integrable (-f) μ ↔ integrable f μ := ⟨λ h, neg_neg f ▸ h.neg, integrable.neg⟩ lemma integrable.sub [measurable_space β] [opens_measurable_space β] {f g : α → β} (hfm : measurable f) (hfi : integrable f μ) (hgm : measurable g) (hgi : integrable g μ) : integrable (f - g) μ := calc ∫⁻ a, nnnorm (f a - g a) ∂μ ≤ ∫⁻ a, nnnorm (f a) + nnnorm (-g a) ∂μ : lintegral_mono (assume a, by exact_mod_cast nnnorm_add_le _ _ ) ... = _ : by { simp only [nnnorm_neg], exact lintegral_nnnorm_add hfm hgm } ... < ⊤ : add_lt_top.2 ⟨hfi, hgi⟩ lemma integrable.norm {f : α → β} (hfi : integrable f μ) : integrable (λa, ∥f a∥) μ := have eq : (λa, (nnnorm ∥f a∥ : ennreal)) = λa, (nnnorm (f a) : ennreal), by { funext, rw nnnorm_norm }, by { rwa [integrable, eq] } lemma integrable_norm_iff (f : α → β) : integrable (λa, ∥f a∥) μ ↔ integrable f μ := have eq : (λa, (nnnorm ∥f a∥ : ennreal)) = λa, (nnnorm (f a) : ennreal), by { funext, rw nnnorm_norm }, by { rw [integrable, integrable, eq] } lemma integrable_of_integrable_bound {f : α → β} {bound : α → ℝ} (h : integrable bound μ) (h_bound : ∀ᵐ a ∂μ, ∥f a∥ ≤ bound a) : integrable f μ := have h₁ : ∀ᵐ a ∂μ, (nnnorm (f a) : ennreal) ≤ ennreal.of_real (bound a), begin refine h_bound.mono (assume a h, _), calc (nnnorm (f a) : ennreal) = ennreal.of_real (∥f a∥) : by rw of_real_norm_eq_coe_nnnorm ... ≤ ennreal.of_real (bound a) : ennreal.of_real_le_of_real h end, calc ∫⁻ a, nnnorm (f a) ∂μ ≤ ∫⁻ a, ennreal.of_real (bound a) ∂μ : lintegral_mono_ae h₁ ... ≤ ∫⁻ a, (ennreal.of_real ∥bound a∥) ∂μ : lintegral_mono $ assume a, ennreal.of_real_le_of_real $ le_max_left (bound a) (-bound a) ... < ⊤ : by { rwa [integrable_iff_norm] at h } section dominated_convergence variables {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} lemma all_ae_of_real_F_le_bound (h : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) : ∀ n, ∀ᵐ a ∂μ, ennreal.of_real ∥F n a∥ ≤ ennreal.of_real (bound a) := λn, (h n).mono $ λ a h, ennreal.of_real_le_of_real h lemma all_ae_tendsto_of_real_norm (h : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top $ 𝓝 $ f a) : ∀ᵐ a ∂μ, tendsto (λn, ennreal.of_real ∥F n a∥) at_top $ 𝓝 $ ennreal.of_real ∥f a∥ := h.mono $ λ a h, tendsto_of_real $ tendsto.comp (continuous.tendsto continuous_norm _) h lemma all_ae_of_real_f_le_bound (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : ∀ᵐ a ∂μ, ennreal.of_real ∥f a∥ ≤ ennreal.of_real (bound a) := begin have F_le_bound := all_ae_of_real_F_le_bound h_bound, rw ← ae_all_iff at F_le_bound, apply F_le_bound.mp ((all_ae_tendsto_of_real_norm h_lim).mono _), assume a tendsto_norm F_le_bound, exact le_of_tendsto' tendsto_norm (F_le_bound) end lemma integrable_of_dominated_convergence {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (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))) : integrable f μ := /- `∥F n a∥ ≤ bound a` and `∥F n a∥ --> ∥f a∥` implies `∥f a∥ ≤ bound a`, and so `∫ ∥f∥ ≤ ∫ bound < ⊤` since `bound` is integrable -/ begin rw integrable_iff_norm, calc ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ ≤ ∫⁻ a, ennreal.of_real (bound a) ∂μ : lintegral_mono_ae $ all_ae_of_real_f_le_bound h_bound h_lim ... < ⊤ : begin rw ← integrable_iff_of_real, { exact bound_integrable }, exact (h_bound 0).mono (λ a h, le_trans (norm_nonneg _) h) end end lemma tendsto_lintegral_norm_of_dominated_convergence [measurable_space β] [borel_space β] [second_countable_topology β] {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (F_measurable : ∀ n, measurable (F n)) (f_measurable : measurable f) (bound_integrable : integrable bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 0) := let b := λa, 2 * ennreal.of_real (bound a) in /- `∥F n a∥ ≤ bound a` and `F n a --> f a` implies `∥f a∥ ≤ bound a`, and thus by the triangle inequality, have `∥F n a - f a∥ ≤ 2 * (bound a). -/ have hb : ∀ n, ∀ᵐ a ∂μ, ennreal.of_real ∥F n a - f a∥ ≤ b a, begin assume n, filter_upwards [all_ae_of_real_F_le_bound h_bound n, all_ae_of_real_f_le_bound h_bound h_lim], assume a h₁ h₂, calc ennreal.of_real ∥F n a - f a∥ ≤ (ennreal.of_real ∥F n a∥) + (ennreal.of_real ∥f a∥) : begin rw [← ennreal.of_real_add], apply of_real_le_of_real, { apply norm_sub_le }, { exact norm_nonneg _ }, { exact norm_nonneg _ } end ... ≤ (ennreal.of_real (bound a)) + (ennreal.of_real (bound a)) : add_le_add h₁ h₂ ... = b a : by rw ← two_mul end, /- On the other hand, `F n a --> f a` implies that `∥F n a - f a∥ --> 0` -/ have h : ∀ᵐ a ∂μ, tendsto (λ n, ennreal.of_real ∥F n a - f a∥) at_top (𝓝 0), begin rw ← ennreal.of_real_zero, refine h_lim.mono (λ a h, (continuous_of_real.tendsto _).comp _), rwa ← tendsto_iff_norm_tendsto_zero end, /- Therefore, by the dominated convergence theorem for nonnegative integration, have ` ∫ ∥f a - F n a∥ --> 0 ` -/ begin suffices h : tendsto (λn, ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 (∫⁻ (a:α), 0 ∂μ)), { rwa lintegral_zero at h }, -- Using the dominated convergence theorem. refine tendsto_lintegral_of_dominated_convergence _ _ hb _ _, -- Show `λa, ∥f a - F n a∥` is measurable for all `n` { exact λn, measurable_of_real.comp ((F_measurable n).sub f_measurable).norm }, -- Show `2 * bound` is integrable { rw integrable_iff_of_real at bound_integrable, { calc ∫⁻ a, b a ∂μ = 2 * ∫⁻ a, ennreal.of_real (bound a) ∂μ : by { rw lintegral_const_mul', exact coe_ne_top } ... < ⊤ : mul_lt_top (coe_lt_top) bound_integrable }, filter_upwards [h_bound 0] λ a h, le_trans (norm_nonneg _) h }, -- Show `∥f a - F n a∥ --> 0` { exact h } end end dominated_convergence section pos_part /-! Lemmas used for defining the positive part of a `L¹` function -/ lemma integrable.max_zero {f : α → ℝ} (hf : integrable f μ) : integrable (λa, max (f a) 0) μ := begin simp only [integrable_iff_norm] at *, calc ∫⁻ a, (ennreal.of_real ∥max (f a) 0∥) ∂μ ≤ ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ : lintegral_mono begin assume a, apply of_real_le_of_real, simp only [real.norm_eq_abs], calc abs (max (f a) 0) = max (f a) 0 : by { rw abs_of_nonneg, apply le_max_right } ... ≤ abs (f a) : max_le (le_abs_self _) (abs_nonneg _) end ... < ⊤ : hf end lemma integrable.min_zero {f : α → ℝ} (hf : integrable f μ) : integrable (λa, min (f a) 0) μ := begin have : (λa, min (f a) 0) = (λa, - max (-f a) 0), { funext, rw [min_eq_neg_max_neg_neg, neg_zero] }, rw this, exact (integrable.max_zero hf.neg).neg, end end pos_part section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma integrable.smul (c : 𝕜) {f : α → β} : integrable f μ → integrable (c • f) μ := begin simp only [integrable], assume hfi, calc ∫⁻ (a : α), nnnorm (c • f a) ∂μ = ∫⁻ (a : α), (nnnorm c) * nnnorm (f a) ∂μ : by simp only [nnnorm_smul, ennreal.coe_mul] ... < ⊤ : begin rw lintegral_const_mul', exacts [mul_lt_top coe_lt_top hfi, coe_ne_top] end end lemma integrable_smul_iff {c : 𝕜} (hc : c ≠ 0) (f : α → β) : integrable (c • f) μ ↔ integrable f μ := begin split, { assume h, simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.smul c⁻¹ }, exact integrable.smul _ end end normed_space variables [second_countable_topology β] namespace ae_eq_fun variable [measurable_space β] section variable [opens_measurable_space β] /-- An almost everywhere equal function is `integrable` if it has a finite distance to the origin. Should mean the same thing as the predicate `integrable` over functions. -/ def integrable (f : α →ₘ[μ] β) : Prop := f ∈ ball (0 : α →ₘ[μ] β) ⊤ lemma integrable_mk {f : α → β} (hf : measurable f) : (integrable (mk f hf : α →ₘ[μ] β)) ↔ measure_theory.integrable f μ := by simp [integrable, zero_def, edist_mk_mk', measure_theory.integrable, nndist_eq_nnnorm] lemma integrable_coe_fn {f : α →ₘ[μ] β} : (measure_theory.integrable f μ) ↔ integrable f := by rw [← integrable_mk, mk_coe_fn] local attribute [simp] integrable_mk lemma integrable_zero : integrable (0 : α →ₘ[μ] β) := mem_ball_self coe_lt_top end section variable [borel_space β] lemma integrable.add {f g : α →ₘ[μ] β} : integrable f → integrable g → integrable (f + g) := begin refine induction_on₂ f g (λ f hf g hg, _), simp only [integrable_mk, mk_add_mk], exact λ hfi hgi, hfi.add hf hg hgi end lemma integrable.neg {f : α →ₘ[μ] β} : integrable f → integrable (-f) := induction_on f $ λ f hfm hfi, (integrable_mk _).2 ((integrable_mk hfm).1 hfi).neg lemma integrable.sub {f g : α →ₘ[μ] β} (hf : integrable f) (hg : integrable g) : integrable (f - g) := hf.add hg.neg protected lemma is_add_subgroup : is_add_subgroup (ball (0 : α →ₘ[μ] β) ⊤) := { zero_mem := integrable_zero, add_mem := λ _ _, integrable.add, neg_mem := λ _, integrable.neg } section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma integrable.smul {c : 𝕜} {f : α →ₘ[μ] β} : integrable f → integrable (c • f) := induction_on f $ λ f hfm hfi, (integrable_mk _).2 $ ((integrable_mk hfm).1 hfi).smul _ end normed_space end end ae_eq_fun variables (α β) /-- The space of equivalence classes of integrable (and measurable) functions, where two integrable functions are equivalent if they agree almost everywhere, i.e., they differ on a set of measure `0`. -/ def l1 [measurable_space β] [opens_measurable_space β] (μ : measure α) : Type (max u v) := {f : α →ₘ[μ] β // f.integrable} notation α ` →₁[`:25 μ `] ` β := l1 α β μ variables {α β} namespace l1 open ae_eq_fun local attribute [instance] ae_eq_fun.is_add_subgroup variables [measurable_space β] section variable [opens_measurable_space β] instance : has_coe (α →₁[μ] β) (α →ₘ[μ] β) := coe_subtype instance : has_coe_to_fun (α →₁[μ] β) := ⟨λ f, α → β, λ f, ⇑(f : α →ₘ[μ] β)⟩ @[simp, norm_cast] lemma coe_coe (f : α →₁[μ] β) : ⇑(f : α →ₘ[μ] β) = f := rfl protected lemma eq {f g : α →₁[μ] β} : (f : α →ₘ[μ] β) = (g : α →ₘ[μ] β) → f = g := subtype.eq @[norm_cast] protected lemma eq_iff {f g : α →₁[μ] β} : (f : α →ₘ[μ] β) = (g : α →ₘ[μ] β) ↔ f = g := iff.intro (l1.eq) (congr_arg coe) /- TODO : order structure of l1-/ /-- `L¹` space forms a `emetric_space`, with the emetric being inherited from almost everywhere functions, i.e., `edist f g = ∫⁻ a, edist (f a) (g a)`. -/ instance : emetric_space (α →₁[μ] β) := subtype.emetric_space /-- `L¹` space forms a `metric_space`, with the metric being inherited from almost everywhere functions, i.e., `edist f g = ennreal.to_real (∫⁻ a, edist (f a) (g a))`. -/ instance : metric_space (α →₁[μ] β) := metric_space_emetric_ball 0 ⊤ end variable [borel_space β] instance : add_comm_group (α →₁[μ] β) := subtype.add_comm_group instance : inhabited (α →₁[μ] β) := ⟨0⟩ @[simp, norm_cast] lemma coe_zero : ((0 : α →₁[μ] β) : α →ₘ[μ] β) = 0 := rfl @[simp, norm_cast] lemma coe_add (f g : α →₁[μ] β) : ((f + g : α →₁[μ] β) : α →ₘ[μ] β) = f + g := rfl @[simp, norm_cast] lemma coe_neg (f : α →₁[μ] β) : ((-f : α →₁[μ] β) : α →ₘ[μ] β) = -f := rfl @[simp, norm_cast] lemma coe_sub (f g : α →₁[μ] β) : ((f - g : α →₁[μ] β) : α →ₘ[μ] β) = f - g := rfl @[simp] lemma edist_eq (f g : α →₁[μ] β) : edist f g = edist (f : α →ₘ[μ] β) (g : α →ₘ[μ] β) := rfl lemma dist_eq (f g : α →₁[μ] β) : dist f g = ennreal.to_real (edist (f : α →ₘ[μ] β) (g : α →ₘ[μ] β)) := rfl /-- The norm on `L¹` space is defined to be `∥f∥ = ∫⁻ a, edist (f a) 0`. -/ instance : has_norm (α →₁[μ] β) := ⟨λ f, dist f 0⟩ lemma norm_eq (f : α →₁[μ] β) : ∥f∥ = ennreal.to_real (edist (f : α →ₘ[μ] β) 0) := rfl instance : normed_group (α →₁[μ] β) := normed_group.of_add_dist (λ x, rfl) $ by { intros, simp only [dist_eq, coe_add], rw edist_add_right } section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] instance : has_scalar 𝕜 (α →₁[μ] β) := ⟨λ x f, ⟨x • (f : α →ₘ[μ] β), ae_eq_fun.integrable.smul f.2⟩⟩ @[simp, norm_cast] lemma coe_smul (c : 𝕜) (f : α →₁[μ] β) : ((c • f : α →₁[μ] β) : α →ₘ[μ] β) = c • (f : α →ₘ[μ] β) := rfl instance : semimodule 𝕜 (α →₁[μ] β) := { one_smul := λf, l1.eq (by { simp only [coe_smul], exact one_smul _ _ }), mul_smul := λx y f, l1.eq (by { simp only [coe_smul], exact mul_smul _ _ _ }), smul_add := λx f g, l1.eq (by { simp only [coe_smul, coe_add], exact smul_add _ _ _ }), smul_zero := λx, l1.eq (by { simp only [coe_zero, coe_smul], exact smul_zero _ }), add_smul := λx y f, l1.eq (by { simp only [coe_smul], exact add_smul _ _ _ }), zero_smul := λf, l1.eq (by { simp only [coe_smul], exact zero_smul _ _ }) } instance : normed_space 𝕜 (α →₁[μ] β) := ⟨ begin rintros x ⟨f, hf⟩, show ennreal.to_real (edist (x • f) 0) ≤ ∥x∥ * ennreal.to_real (edist f 0), rw [edist_smul, to_real_of_real_mul], exact norm_nonneg _ end ⟩ end normed_space section of_fun /-- Construct the equivalence class `[f]` of a measurable and integrable function `f`. -/ def of_fun (f : α → β) (hfm : measurable f) (hfi : integrable f μ) : (α →₁[μ] β) := ⟨mk f hfm, by { rw integrable_mk, exact hfi }⟩ @[simp] lemma of_fun_eq_mk (f : α → β) (hfm hfi) : ((of_fun f hfm hfi : α →₁[μ] β) : α →ₘ[μ] β) = mk f hfm := rfl lemma of_fun_eq_of_fun (f g : α → β) (hfm hfi hgm hgi) : (of_fun f hfm hfi : α →₁[μ] β) = of_fun g hgm hgi ↔ f =ᵐ[μ] g := by { rw ← l1.eq_iff, simp only [of_fun_eq_mk, mk_eq_mk] } lemma of_fun_zero : (of_fun (λ _, 0) measurable_zero (integrable_zero α μ β) : α →₁[μ] β) = 0 := rfl lemma of_fun_add (f g : α → β) (hfm hfi hgm hgi) : (of_fun (f + g) (measurable.add hfm hgm) (integrable.add hfm hfi hgm hgi) : α →₁[μ] β) = of_fun f hfm hfi + of_fun g hgm hgi := rfl lemma of_fun_neg (f : α → β) (hfm hfi) : (of_fun (- f) (measurable.neg hfm) (integrable.neg hfi) : α →₁[μ] β) = - of_fun f hfm hfi := rfl lemma of_fun_sub (f g : α → β) (hfm hfi hgm hgi) : (of_fun (f - g) (measurable.sub hfm hgm) (integrable.sub hfm hfi hgm hgi) : α →₁[μ] β) = of_fun f hfm hfi - of_fun g hgm hgi := rfl lemma norm_of_fun (f : α → β) (hfm hfi) : ∥(of_fun f hfm hfi : α →₁[μ] β)∥ = ennreal.to_real (∫⁻ a, edist (f a) 0 ∂μ) := rfl lemma norm_of_fun_eq_lintegral_norm (f : α → β) (hfm hfi) : ∥(of_fun f hfm hfi : α →₁[μ] β)∥ = ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) := by { rw [norm_of_fun, lintegral_norm_eq_lintegral_edist] } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma of_fun_smul (f : α → β) (hfm : measurable f) (hfi : integrable f μ) (k : 𝕜) : of_fun (λa, k • f a) (hfm.const_smul _) (hfi.smul _) = k • of_fun f hfm hfi := rfl end of_fun section to_fun protected lemma measurable (f : α →₁[μ] β) : measurable f := f.1.measurable protected lemma integrable (f : α →₁[μ] β) : integrable ⇑f μ := integrable_coe_fn.2 f.2 lemma of_fun_to_fun (f : α →₁[μ] β) : of_fun f f.measurable f.integrable = f := subtype.ext (f : α →ₘ[μ] β).mk_coe_fn lemma mk_to_fun (f : α →₁[μ] β) : (mk f f.measurable : α →ₘ[μ] β) = f := by { rw ← of_fun_eq_mk, rw l1.eq_iff, exact of_fun_to_fun f } lemma to_fun_of_fun (f : α → β) (hfm hfi) : ⇑(of_fun f hfm hfi : α →₁[μ] β) =ᵐ[μ] f := coe_fn_mk f hfm variables (α β) lemma zero_to_fun : ⇑(0 : α →₁[μ] β) =ᵐ[μ] 0 := ae_eq_fun.coe_fn_zero variables {α β} lemma add_to_fun (f g : α →₁[μ] β) : ⇑(f + g) =ᵐ[μ] f + g := ae_eq_fun.coe_fn_add _ _ lemma neg_to_fun (f : α →₁[μ] β) : ⇑(-f) =ᵐ[μ] -⇑f := ae_eq_fun.coe_fn_neg _ lemma sub_to_fun (f g : α →₁[μ] β) : ⇑(f - g) =ᵐ[μ] ⇑f - ⇑g := ae_eq_fun.coe_fn_sub _ _ lemma dist_to_fun (f g : α →₁[μ] β) : dist f g = ennreal.to_real (∫⁻ x, edist (f x) (g x) ∂μ) := by { simp only [← coe_coe, dist_eq, edist_eq_coe] } lemma norm_eq_nnnorm_to_fun (f : α →₁[μ] β) : ∥f∥ = ennreal.to_real (∫⁻ a, nnnorm (f a) ∂μ) := by { rw [← coe_coe, lintegral_nnnorm_eq_lintegral_edist, ← edist_zero_eq_coe], refl } lemma norm_eq_norm_to_fun (f : α →₁[μ] β) : ∥f∥ = ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) := by { rw norm_eq_nnnorm_to_fun, congr, funext, rw of_real_norm_eq_coe_nnnorm } lemma lintegral_edist_to_fun_lt_top (f g : α →₁[μ] β) : (∫⁻ a, edist (f a) (g a) ∂μ) < ⊤ := begin apply lintegral_edist_lt_top, exact f.measurable, exact f.integrable, exact g.measurable, exact g.integrable end variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma smul_to_fun (c : 𝕜) (f : α →₁[μ] β) : ⇑(c • f) =ᵐ[μ] c • f := ae_eq_fun.coe_fn_smul _ _ end to_fun section pos_part /-- Positive part of a function in `L¹` space. -/ def pos_part (f : α →₁[μ] ℝ) : α →₁[μ] ℝ := ⟨ae_eq_fun.pos_part f, begin rw [← ae_eq_fun.integrable_coe_fn, integrable_congr (coe_fn_pos_part _)], exact integrable.max_zero f.integrable end ⟩ /-- Negative part of a function in `L¹` space. -/ def neg_part (f : α →₁[μ] ℝ) : α →₁[μ] ℝ := pos_part (-f) @[norm_cast] lemma coe_pos_part (f : α →₁[μ] ℝ) : (f.pos_part : α →ₘ[μ] ℝ) = (f : α →ₘ[μ] ℝ).pos_part := rfl lemma pos_part_to_fun (f : α →₁[μ] ℝ) : ⇑(pos_part f) =ᵐ[μ] λ a, max (f a) 0 := ae_eq_fun.coe_fn_pos_part _ lemma neg_part_to_fun_eq_max (f : α →₁[μ] ℝ) : ∀ᵐ a ∂μ, neg_part f a = max (- f a) 0 := begin rw neg_part, filter_upwards [pos_part_to_fun (-f), neg_to_fun f], simp only [mem_set_of_eq], assume a h₁ h₂, rw [h₁, h₂, pi.neg_apply] end lemma neg_part_to_fun_eq_min (f : α →₁[μ] ℝ) : ∀ᵐ a ∂μ, neg_part f a = - min (f a) 0 := (neg_part_to_fun_eq_max f).mono $ assume a h, by rw [h, min_eq_neg_max_neg_neg, _root_.neg_neg, neg_zero] lemma norm_le_norm_of_ae_le {f g : α →₁[μ] β} (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ ∥g a∥) : ∥f∥ ≤ ∥g∥ := begin simp only [l1.norm_eq_norm_to_fun], rw to_real_le_to_real, { apply lintegral_mono_ae, exact h.mono (λ a h, of_real_le_of_real h) }, { rw [← lt_top_iff_ne_top, ← integrable_iff_norm], exact f.integrable }, { rw [← lt_top_iff_ne_top, ← integrable_iff_norm], exact g.integrable } end lemma continuous_pos_part : continuous $ λf : α →₁[μ] ℝ, pos_part f := begin simp only [metric.continuous_iff], assume g ε hε, use ε, use hε, simp only [dist_eq_norm], assume f hfg, refine lt_of_le_of_lt (norm_le_norm_of_ae_le _) hfg, filter_upwards [l1.sub_to_fun f g, l1.sub_to_fun (pos_part f) (pos_part g), pos_part_to_fun f, pos_part_to_fun g], simp only [mem_set_of_eq], assume a h₁ h₂ h₃ h₄, simp only [real.norm_eq_abs, h₁, h₂, h₃, h₄, pi.sub_apply], exact abs_max_sub_max_le_abs _ _ _ end lemma continuous_neg_part : continuous $ λf : α →₁[μ] ℝ, neg_part f := have eq : (λf : α →₁[μ] ℝ, neg_part f) = (λf : α →₁[μ] ℝ, pos_part (-f)) := rfl, by { rw eq, exact continuous_pos_part.comp continuous_neg } end pos_part /- TODO: l1 is a complete space -/ end l1 end measure_theory
b8a854baeea94f204f8a2445a48f069f30c3c6bb
4727251e0cd73359b15b664c3170e5d754078599
/src/data/finset/pairwise.lean
e88d00420d3124eca27ed7debd044bf7821020b5
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
2,172
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.finset.lattice /-! # Relations holding pairwise on finite sets In this file we prove a few results about the interaction of `set.pairwise_disjoint` and `finset`. -/ open finset variables {α ι ι' : Type*} instance [decidable_eq α] {r : α → α → Prop} [decidable_rel r] {s : finset α} : decidable ((s : set α).pairwise r) := decidable_of_iff' (∀ a ∈ s, ∀ b ∈ s, a ≠ b → r a b) iff.rfl lemma finset.pairwise_disjoint_range_singleton [decidable_eq α] : (set.range (singleton : α → finset α)).pairwise_disjoint id := begin rintro _ ⟨a, rfl⟩ _ ⟨b, rfl⟩ h, exact disjoint_singleton.2 (ne_of_apply_ne _ h), end namespace set lemma pairwise_disjoint.elim_finset [decidable_eq α] {s : set ι} {f : ι → finset α} (hs : s.pairwise_disjoint f) {i j : ι} (hi : i ∈ s) (hj : j ∈ s) (a : α) (hai : a ∈ f i) (haj : a ∈ f j) : i = j := hs.elim hi hj (finset.not_disjoint_iff.2 ⟨a, hai, haj⟩) lemma pairwise_disjoint.image_finset_of_le [decidable_eq ι] [semilattice_inf α] [order_bot α] {s : finset ι} {f : ι → α} (hs : (s : set ι).pairwise_disjoint f) {g : ι → ι} (hf : ∀ a, f (g a) ≤ f a) : (s.image g : set ι).pairwise_disjoint f := begin rw coe_image, exact hs.image_of_le hf, end variables [lattice α] [order_bot α] /-- Bind operation for `set.pairwise_disjoint`. In a complete lattice, you can use `set.pairwise_disjoint.bUnion`. -/ lemma pairwise_disjoint.bUnion_finset {s : set ι'} {g : ι' → finset ι} {f : ι → α} (hs : s.pairwise_disjoint (λ i' : ι', (g i').sup f)) (hg : ∀ i ∈ s, (g i : set ι).pairwise_disjoint f) : (⋃ i ∈ s, ↑(g i)).pairwise_disjoint f := begin rintro a ha b hb hab, simp_rw set.mem_Union at ha hb, obtain ⟨c, hc, ha⟩ := ha, obtain ⟨d, hd, hb⟩ := hb, obtain hcd | hcd := eq_or_ne (g c) (g d), { exact hg d hd (by rwa hcd at ha) hb hab }, { exact (hs hc hd (ne_of_apply_ne _ hcd)).mono (finset.le_sup ha) (finset.le_sup hb) } end end set
e1f568917719007d687a18ad57b625cb3d6922bd
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/jacobson_ideal.lean
d93f2eed2a9ab499eb97ef7a0b8f0dadab55e8e5
[ "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
14,415
lean
/- Copyright (c) 2020 Devon Tuma. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Devon Tuma -/ import ring_theory.ideal.operations import ring_theory.polynomial.basic /-! # Jacobson radical The Jacobson radical of a ring `R` is defined to be the intersection of all maximal ideals of `R`. This is similar to how the nilradical is equal to the intersection of all prime ideals of `R`. We can extend the idea of the nilradical to ideals of `R`, by letting the radical of an ideal `I` be the intersection of prime ideals containing `I`. Under this extension, the original nilradical is the radical of the zero ideal `⊥`. Here we define the Jacobson radical of an ideal `I` in a similar way, as the intersection of maximal ideals containing `I`. ## Main definitions Let `R` be a commutative ring, and `I` be an ideal of `R` * `jacobson I` is the jacobson radical, i.e. the infimum of all maximal ideals containing I. * `is_local I` is the proposition that the jacobson radical of `I` is itself a maximal ideal ## Main statements * `mem_jacobson_iff` gives a characterization of members of the jacobson of I * `is_local_of_is_maximal_radical`: if the radical of I is maximal then so is the jacobson radical ## Tags Jacobson, Jacobson radical, Local Ideal -/ universes u v namespace ideal variables {R : Type u} [comm_ring R] {I : ideal R} variables {S : Type v} [comm_ring S] section jacobson /-- 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} lemma le_jacobson : I ≤ jacobson I := λ x hx, mem_Inf.mpr (λ J hJ, hJ.left hx) @[simp] lemma jacobson_idem : jacobson (jacobson I) = jacobson I := le_antisymm (Inf_le_Inf (λ J hJ, ⟨Inf_le hJ, hJ.2⟩)) le_jacobson lemma radical_le_jacobson : radical I ≤ jacobson I := le_Inf (λ J hJ, (radical_eq_Inf I).symm ▸ Inf_le ⟨hJ.left, is_maximal.is_prime hJ.right⟩) lemma eq_radical_of_eq_jacobson : jacobson I = I → radical I = I := λ h, le_antisymm (le_trans radical_le_jacobson (le_of_eq h)) le_radical @[simp] lemma jacobson_top : jacobson (⊤ : ideal R) = ⊤ := eq_top_iff.2 le_jacobson @[simp] theorem jacobson_eq_top_iff : 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.ne_top) H, λ H, eq_top_iff.2 $ le_Inf $ λ J ⟨hij, hj⟩, H ▸ hij⟩ lemma jacobson_eq_bot : jacobson I = ⊥ → I = ⊥ := λ h, eq_bot_iff.mpr (h ▸ le_jacobson) lemma jacobson_eq_self_of_is_maximal [H : is_maximal I] : I.jacobson = I := le_antisymm (Inf_le ⟨le_of_eq rfl, H⟩) le_jacobson @[priority 100] instance jacobson.is_maximal [H : is_maximal I] : is_maximal (jacobson I) := ⟨⟨λ htop, H.1.1 (jacobson_eq_top_iff.1 htop), λ J hJ, H.1.2 _ (lt_of_le_of_lt le_jacobson hJ)⟩⟩ theorem mem_jacobson_iff {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.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.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, mul_comm _ (- z)], rcases hy with ⟨i, hi, df⟩, rw [← (sub_eq_iff_eq_add.mpr df.symm), sub_sub, add_comm, ← sub_sub, sub_self, zero_sub], refine M.mul_mem_left (-z) ((neg_mem_iff _).mpr hi) }) (him hz)⟩ /-- An ideal equals its Jacobson radical iff it is the intersection of a set of maximal ideals. Allowing the set to include ⊤ is equivalent, and is included only to simplify some proofs. -/ theorem eq_jacobson_iff_Inf_maximal : I.jacobson = I ↔ ∃ M : set (ideal R), (∀ J ∈ M, is_maximal J ∨ J = ⊤) ∧ I = Inf M := begin use λ hI, ⟨{J : ideal R | I ≤ J ∧ J.is_maximal}, ⟨λ _ hJ, or.inl hJ.right, hI.symm⟩⟩, rintros ⟨M, hM, hInf⟩, refine le_antisymm (λ x hx, _) le_jacobson, rw [hInf, mem_Inf], intros I hI, cases hM I hI with is_max is_top, { exact (mem_Inf.1 hx) ⟨le_Inf_iff.1 (le_of_eq hInf) I hI, is_max⟩ }, { exact is_top.symm ▸ submodule.mem_top } end theorem eq_jacobson_iff_Inf_maximal' : I.jacobson = I ↔ ∃ M : set (ideal R), (∀ (J ∈ M) (K : ideal R), J < K → K = ⊤) ∧ I = Inf M := eq_jacobson_iff_Inf_maximal.trans ⟨λ h, let ⟨M, hM⟩ := h in ⟨M, ⟨λ J hJ K hK, or.rec_on (hM.1 J hJ) (λ h, h.1.2 K hK) (λ h, eq_top_iff.2 (le_of_lt (h ▸ hK))), hM.2⟩⟩, λ h, let ⟨M, hM⟩ := h in ⟨M, ⟨λ J hJ, or.rec_on (classical.em (J = ⊤)) (λ h, or.inr h) (λ h, or.inl ⟨⟨h, hM.1 J hJ⟩⟩), hM.2⟩⟩⟩ /-- An ideal `I` equals its Jacobson radical if and only if every element outside `I` also lies outside of a maximal ideal containing `I`. -/ lemma eq_jacobson_iff_not_mem : I.jacobson = I ↔ ∀ x ∉ I, ∃ M : ideal R, (I ≤ M ∧ M.is_maximal) ∧ x ∉ M := begin split, { intros h x hx, erw [← h, mem_Inf] at hx, push_neg at hx, exact hx }, { refine λ h, le_antisymm (λ x hx, _) le_jacobson, contrapose hx, erw mem_Inf, push_neg, exact h x hx } end theorem map_jacobson_of_surjective {f : R →+* S} (hf : function.surjective f) : ring_hom.ker f ≤ I → map f (I.jacobson) = (map f I).jacobson := begin intro h, unfold ideal.jacobson, have : ∀ J ∈ {J : ideal R | I ≤ J ∧ J.is_maximal}, f.ker ≤ J := λ J hJ, le_trans h hJ.left, refine trans (map_Inf hf this) (le_antisymm _ _), { refine Inf_le_Inf (λ J hJ, ⟨comap f J, ⟨⟨le_comap_of_map_le hJ.1, _⟩, map_comap_of_surjective f hf J⟩⟩), haveI : J.is_maximal := hJ.right, exact comap_is_maximal_of_surjective f hf }, { refine Inf_le_Inf_of_subset_insert_top (λ j hj, hj.rec_on (λ J hJ, _)), rw ← hJ.2, cases map_eq_top_or_is_maximal_of_surjective f hf hJ.left.right with htop hmax, { exact htop.symm ▸ set.mem_insert ⊤ _ }, { exact set.mem_insert_of_mem ⊤ ⟨map_mono hJ.1.1, hmax⟩ } }, end lemma map_jacobson_of_bijective {f : R →+* S} (hf : function.bijective f) : map f (I.jacobson) = (map f I).jacobson := map_jacobson_of_surjective hf.right (le_trans (le_of_eq (f.injective_iff_ker_eq_bot.1 hf.left)) bot_le) lemma comap_jacobson {f : R →+* S} {K : ideal S} : comap f (K.jacobson) = Inf (comap f '' {J : ideal S | K ≤ J ∧ J.is_maximal}) := trans (comap_Inf' f _) (Inf_eq_infi).symm theorem comap_jacobson_of_surjective {f : R →+* S} (hf : function.surjective f) {K : ideal S} : comap f (K.jacobson) = (comap f K).jacobson := begin unfold ideal.jacobson, refine le_antisymm _ _, { refine le_trans (comap_mono (le_of_eq (trans top_inf_eq.symm Inf_insert.symm))) _, rw [comap_Inf', Inf_eq_infi], refine infi_le_infi_of_subset (λ J hJ, _), have : comap f (map f J) = J := trans (comap_map_of_surjective f hf J) (le_antisymm (sup_le_iff.2 ⟨le_of_eq rfl, le_trans (comap_mono bot_le) hJ.left⟩) le_sup_left), cases map_eq_top_or_is_maximal_of_surjective _ hf hJ.right with htop hmax, { refine ⟨⊤, ⟨set.mem_insert ⊤ _, htop ▸ this⟩⟩ }, { refine ⟨map f J, ⟨set.mem_insert_of_mem _ ⟨le_map_of_comap_le_of_surjective f hf hJ.1, hmax⟩, this⟩⟩ } }, { rw comap_Inf, refine le_infi_iff.2 (λ J, (le_infi_iff.2 (λ hJ, _))), haveI : J.is_maximal := hJ.right, refine Inf_le ⟨comap_mono hJ.left, comap_is_maximal_of_surjective _ hf⟩ } end lemma mem_jacobson_bot {x : R} : x ∈ jacobson (⊥ : ideal R) ↔ ∀ y, is_unit (x * y + 1) := ⟨λ hx y, let ⟨z, hz⟩ := (mem_jacobson_iff.1 hx) y in is_unit_iff_exists_inv.2 ⟨z, by rwa [add_mul, one_mul, ← sub_eq_zero]⟩, λ h, mem_jacobson_iff.mpr (λ y, (let ⟨b, hb⟩ := is_unit_iff_exists_inv.1 (h y) in ⟨b, (submodule.mem_bot R).2 (hb ▸ (by ring))⟩))⟩ /-- An ideal `I` of `R` is equal to its Jacobson radical if and only if the Jacobson radical of the quotient ring `R/I` is the zero ideal -/ theorem jacobson_eq_iff_jacobson_quotient_eq_bot : I.jacobson = I ↔ jacobson (⊥ : ideal (I.quotient)) = ⊥ := begin have hf : function.surjective (quotient.mk I) := submodule.quotient.mk_surjective I, split, { intro h, replace h := congr_arg (map (quotient.mk I)) h, rw map_jacobson_of_surjective hf (le_of_eq mk_ker) at h, simpa using h }, { intro h, replace h := congr_arg (comap (quotient.mk I)) h, rw [comap_jacobson_of_surjective hf, ← (quotient.mk I).ker_eq_comap_bot] at h, simpa using h } end /-- The standard radical and Jacobson radical of an ideal `I` of `R` are equal if and only if the nilradical and Jacobson radical of the quotient ring `R/I` coincide -/ theorem radical_eq_jacobson_iff_radical_quotient_eq_jacobson_bot : I.radical = I.jacobson ↔ radical (⊥ : ideal (I.quotient)) = jacobson ⊥ := begin have hf : function.surjective (quotient.mk I) := submodule.quotient.mk_surjective I, split, { intro h, have := congr_arg (map (quotient.mk I)) h, rw [map_radical_of_surjective hf (le_of_eq mk_ker), map_jacobson_of_surjective hf (le_of_eq mk_ker)] at this, simpa using this }, { intro h, have := congr_arg (comap (quotient.mk I)) h, rw [comap_radical, comap_jacobson_of_surjective hf, ← (quotient.mk I).ker_eq_comap_bot] at this, simpa using this } end @[mono] lemma jacobson_mono {I J : ideal R} : I ≤ J → I.jacobson ≤ J.jacobson := begin intros h x hx, erw mem_Inf at ⊢ hx, exact λ K ⟨hK, hK_max⟩, hx ⟨trans h hK, hK_max⟩ end lemma jacobson_radical_eq_jacobson : I.radical.jacobson = I.jacobson := le_antisymm (le_trans (le_of_eq (congr_arg jacobson (radical_eq_Inf I))) (Inf_le_Inf (λ J hJ, ⟨Inf_le ⟨hJ.1, hJ.2.is_prime⟩, hJ.2⟩))) (jacobson_mono le_radical) end jacobson section polynomial open polynomial lemma jacobson_bot_polynomial_le_Inf_map_maximal : jacobson (⊥ : ideal (polynomial R)) ≤ Inf (map C '' {J : ideal R | J.is_maximal}) := begin refine le_Inf (λ J, exists_imp_distrib.2 (λ j hj, _)), haveI : j.is_maximal := hj.1, refine trans (jacobson_mono bot_le) (le_of_eq _ : J.jacobson ≤ J), suffices : (⊥ : ideal (polynomial j.quotient)).jacobson = ⊥, { rw [← hj.2, jacobson_eq_iff_jacobson_quotient_eq_bot], replace this := congr_arg (map (polynomial_quotient_equiv_quotient_polynomial j).to_ring_hom) this, rwa [map_jacobson_of_bijective _, map_bot] at this, exact (ring_equiv.bijective (polynomial_quotient_equiv_quotient_polynomial j)) }, refine eq_bot_iff.2 (λ f hf, _), simpa [(λ hX, by simpa using congr_arg (λ f, coeff f 1) hX : (X : polynomial j.quotient) ≠ 0)] using eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit ((mem_jacobson_bot.1 hf) X)), end lemma jacobson_bot_polynomial_of_jacobson_bot (h : jacobson (⊥ : ideal R) = ⊥) : jacobson (⊥ : ideal (polynomial R)) = ⊥ := begin refine eq_bot_iff.2 (le_trans jacobson_bot_polynomial_le_Inf_map_maximal _), refine (λ f hf, ((submodule.mem_bot _).2 (polynomial.ext (λ n, trans _ (coeff_zero n).symm)))), suffices : f.coeff n ∈ ideal.jacobson ⊥, by rwa [h, submodule.mem_bot] at this, exact mem_Inf.2 (λ j hj, (mem_map_C_iff.1 ((mem_Inf.1 hf) ⟨j, ⟨hj.2, rfl⟩⟩)) n), end end polynomial section is_local /-- An ideal `I` is local iff its Jacobson radical is maximal. -/ class is_local (I : ideal R) : Prop := (out : is_maximal (jacobson I)) theorem is_local_iff {I : ideal R} : is_local I ↔ is_maximal (jacobson I) := ⟨λ h, h.1, λ h, ⟨h⟩⟩ 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.1.eq_of_le hm.1.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 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.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 ideal
98211fa7f057cc782b23ea75b218615b50d5cb54
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/confuse_ind.lean
cad894350cda141b7abedb4560c7b6e30ae1fe1e
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
244
lean
import logic data.prod data.unit definition mk_arrow (A : Type) (B : Type) := A → A → B inductive confuse (A : Type) := | leaf1 : confuse A | leaf2 : num → confuse A | node : mk_arrow A (confuse A) → confuse A check confuse.cases_on
8061a10c829c0d9f9910749f6bb4c8f8d585a918
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/order/filter/bases.lean
f950acea0e7f454a1e9a422ad1b1a549585d4a6f
[ "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
8,845
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury Kudryashov -/ import order.filter.basic /-! # Filter bases In this file we define `filter.has_basis l p s`, where `l` is a filter on `α`, `p` is a predicate on some index set `ι`, and `s : ι → set α`. ## Main statements * `has_basis.mem_iff`, `has_basis.mem_of_superset`, `has_basis.mem_of_mem` : restate `t ∈ f` in terms of a basis; * `basis_sets` : all sets of a filter form a basis; * `has_basis.inf`, `has_basis.inf_principal`, `has_basis.prod`, `has_basis.prod_self`, `has_basis.map`, `has_basis.comap` : combinators to construct filters of `l ⊓ l'`, `l ⊓ principal t`, `l.prod l'`, `l.prod l`, `l.map f`, `l.comap f` respectively; * `has_basis.le_iff`, `has_basis.ge_iff`, has_basis.le_basis_iff` : restate `l ≤ l'` in terms of bases. * `has_basis.tendsto_right_iff`, `has_basis.tendsto_left_iff`, `has_basis.tendsto_iff` : restate `tendsto f l l'` in terms of bases. ## Implementation notes As with `Union`/`bUnion`/`sUnion`, there are three different approaches to filter bases: * `has_basis l s`, `s : set (set α)`; * `has_basis l s`, `s : ι → set α`; * `has_basis l p s`, `p : ι → Prop`, `s : ι → set α`. We use the latter one because, e.g., `𝓝 x` in an `emetric_space` or in a `metric_space` has a basis of this form. The other two can be emulated using `s = id` or `p = λ _, true`. With this approach sometimes one needs to `simp` the statement provided by the `has_basis` machinery, e.g., `simp only [exists_prop, true_and]` or `simp only [forall_const]` can help with the case `p = λ _, true`. -/ namespace filter variables {α : Type*} {β : Type*} {γ : Type*} {ι : Type*} {ι' : Type*} open set /-- We say that a filter `l` has a basis `s : ι → set α` bounded by `p : ι → Prop`, if `t ∈ l` if and only if `t` includes `s i` for some `i` such that `p i`. -/ protected def has_basis (l : filter α) (p : ι → Prop) (s : ι → set α) : Prop := ∀ t : set α, t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t section same_type variables {l l' : filter α} {p : ι → Prop} {s : ι → set α} {t : set α} {i : ι} {p' : ι' → Prop} {s' : ι' → set α} {i' : ι'} /-- Definition of `has_basis` unfolded to make it useful for `rw` and `simp`. -/ lemma has_basis.mem_iff (hl : l.has_basis p s) : t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t := hl t lemma has_basis.eventually_iff (hl : l.has_basis p s) {q : α → Prop} : (∀ᶠ x in l, q x) ↔ ∃ i (hi : p i), ∀ ⦃x⦄, x ∈ s i → q x := hl _ lemma has_basis.mem_of_superset (hl : l.has_basis p s) (hi : p i) (ht : s i ⊆ t) : t ∈ l := (hl t).2 ⟨i, hi, ht⟩ lemma has_basis.mem_of_mem (hl : l.has_basis p s) (hi : p i) : s i ∈ l := hl.mem_of_superset hi $ subset.refl _ lemma has_basis.forall_nonempty_iff_ne_bot (hl : l.has_basis p s) : (∀ {i}, p i → (s i).nonempty) ↔ l ≠ ⊥ := ⟨λ H, forall_sets_nonempty_iff_ne_bot.1 $ λ s hs, let ⟨i, hi, his⟩ := (hl s).1 hs in (H hi).mono his, λ H i hi, nonempty_of_mem_sets H (hl.mem_of_mem hi)⟩ lemma basis_sets (l : filter α) : l.has_basis (λ s : set α, s ∈ l) id := λ t, exists_sets_subset_iff.symm lemma at_top_basis [nonempty α] [semilattice_sup α] : (@at_top α _).has_basis (λ _, true) Ici := λ t, by simpa only [exists_prop, true_and] using @mem_at_top_sets α _ _ t lemma at_top_basis' [semilattice_sup α] (a : α) : (@at_top α _).has_basis (λ x, a ≤ x) Ici := λ t, (@at_top_basis α ⟨a⟩ _ t).trans ⟨λ ⟨x, _, hx⟩, ⟨x ⊔ a, le_sup_right, λ y hy, hx (le_trans le_sup_left hy)⟩, λ ⟨x, _, hx⟩, ⟨x, trivial, hx⟩⟩ theorem has_basis.ge_iff (hl' : l'.has_basis p' s') : l ≤ l' ↔ ∀ i', p' i' → s' i' ∈ l := ⟨λ h i' hi', h $ hl'.mem_of_mem hi', λ h s hs, let ⟨i', hi', hs⟩ := (hl' s).1 hs in mem_sets_of_superset (h _ hi') hs⟩ theorem has_basis.le_iff (hl : l.has_basis p s) : l ≤ l' ↔ ∀ t ∈ l', ∃ i (hi : p i), s i ⊆ t := by simp only [le_def, hl.mem_iff] theorem has_basis.le_basis_iff (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : l ≤ l' ↔ ∀ i', p' i' → ∃ i (hi : p i), s i ⊆ s' i' := by simp only [hl'.ge_iff, hl.mem_iff] lemma has_basis.inf (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊓ l').has_basis (λ i : ι × ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∩ s' i.2) := begin intro t, simp only [mem_inf_sets, exists_prop, hl.mem_iff, hl'.mem_iff], split, { rintros ⟨t, ⟨i, hi, ht⟩, t', ⟨i', hi', ht'⟩, H⟩, use [(i, i'), ⟨hi, hi'⟩, subset.trans (inter_subset_inter ht ht') H] }, { rintros ⟨⟨i, i'⟩, ⟨hi, hi'⟩, H⟩, use [s i, i, hi, subset.refl _, s' i', i', hi', subset.refl _, H] } end lemma has_basis.inf_principal (hl : l.has_basis p s) (s' : set α) : (l ⊓ principal s').has_basis p (λ i, s i ∩ s') := λ t, by simp only [mem_inf_principal, hl.mem_iff, subset_def, mem_set_of_eq, mem_inter_iff, and_imp] lemma has_basis.eq_binfi (h : l.has_basis p s) : l = ⨅ i (_ : p i), principal (s i) := eq_binfi_of_mem_sets_iff_exists_mem $ λ t, by simp only [h.mem_iff, mem_principal_sets] lemma has_basis.eq_infi (h : l.has_basis (λ _, true) s) : l = ⨅ i, principal (s i) := by simpa only [infi_true] using h.eq_binfi @[nolint ge_or_gt] -- see Note [nolint_ge] lemma has_basis_infi_principal {s : ι → set α} (h : directed (≥) s) (ne : nonempty ι) : (⨅ i, principal (s i)).has_basis (λ _, true) s := begin refine λ t, (mem_infi (h.mono_comp _ _) ne t).trans $ by simp only [exists_prop, true_and, mem_principal_sets], exact λ _ _, principal_mono.2 end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma has_basis_binfi_principal {s : β → set α} {S : set β} (h : directed_on (s ⁻¹'o (≥)) S) (ne : S.nonempty) : (⨅ i ∈ S, principal (s i)).has_basis (λ i, i ∈ S) s := begin refine λ t, (mem_binfi _ ne).trans $ by simp only [mem_principal_sets], rw [directed_on_iff_directed, ← directed_comp, (∘)] at h ⊢, apply h.mono_comp _ _, exact λ _ _, principal_mono.2 end lemma has_basis.map (f : α → β) (hl : l.has_basis p s) : (l.map f).has_basis p (λ i, f '' (s i)) := λ t, by simp only [mem_map, image_subset_iff, hl.mem_iff, preimage] lemma has_basis.comap (f : β → α) (hl : l.has_basis p s) : (l.comap f).has_basis p (λ i, f ⁻¹' (s i)) := begin intro t, simp only [mem_comap_sets, exists_prop, hl.mem_iff], split, { rintros ⟨t', ⟨i, hi, ht'⟩, H⟩, exact ⟨i, hi, subset.trans (preimage_mono ht') H⟩ }, { rintros ⟨i, hi, H⟩, exact ⟨s i, ⟨i, hi, subset.refl _⟩, H⟩ } end lemma has_basis.prod_self (hl : l.has_basis p s) : (l.prod l).has_basis p (λ i, (s i).prod (s i)) := begin intro t, apply mem_prod_iff.trans, split, { rintros ⟨t₁, ht₁, t₂, ht₂, H⟩, rcases hl.mem_iff.1 (inter_mem_sets ht₁ ht₂) with ⟨i, hi, ht⟩, exact ⟨i, hi, λ p ⟨hp₁, hp₂⟩, H ⟨(ht hp₁).1, (ht hp₂).2⟩⟩ }, { rintros ⟨i, hi, H⟩, exact ⟨s i, hl.mem_of_mem hi, s i, hl.mem_of_mem hi, H⟩ } end end same_type section two_types variables {la : filter α} {pa : ι → Prop} {sa : ι → set α} {lb : filter β} {pb : ι' → Prop} {sb : ι' → set β} {f : α → β} lemma has_basis.tendsto_left_iff (hla : la.has_basis pa sa) : tendsto f la lb ↔ ∀ t ∈ lb, ∃ i (hi : pa i), ∀ x ∈ sa i, f x ∈ t := by { simp only [tendsto, (hla.map f).le_iff, image_subset_iff], refl } lemma has_basis.tendsto_right_iff (hlb : lb.has_basis pb sb) : tendsto f la lb ↔ ∀ i (hi : pb i), ∀ᶠ x in la, f x ∈ sb i := by simp only [tendsto, hlb.ge_iff, mem_map, filter.eventually] lemma has_basis.tendsto_iff (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : tendsto f la lb ↔ ∀ ib (hib : pb ib), ∃ ia (hia : pa ia), ∀ x ∈ sa ia, f x ∈ sb ib := by simp only [hlb.tendsto_right_iff, hla.eventually_iff, subset_def, mem_set_of_eq] lemma tendsto.basis_left (H : tendsto f la lb) (hla : la.has_basis pa sa) : ∀ t ∈ lb, ∃ i (hi : pa i), ∀ x ∈ sa i, f x ∈ t := hla.tendsto_left_iff.1 H lemma tendsto.basis_right (H : tendsto f la lb) (hlb : lb.has_basis pb sb) : ∀ i (hi : pb i), ∀ᶠ x in la, f x ∈ sb i := hlb.tendsto_right_iff.1 H lemma tendsto.basis_both (H : tendsto f la lb) (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : ∀ ib (hib : pb ib), ∃ ia (hia : pa ia), ∀ x ∈ sa ia, f x ∈ sb ib := (hla.tendsto_iff hlb).1 H lemma has_basis.prod (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : (la.prod lb).has_basis (λ i : ι × ι', pa i.1 ∧ pb i.2) (λ i, (sa i.1).prod (sb i.2)) := (hla.comap prod.fst).inf (hlb.comap prod.snd) end two_types end filter
69b10f1b9313761ecce0bfb97b83871e9869ef77
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Elab/Util.lean
20574beda3bd8f9a112fb9de77d01163c9bbf98a
[ "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
10,543
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.Trace import Lean.Parser.Syntax import Lean.Parser.Extension import Lean.KeyedDeclsAttribute import Lean.Elab.Exception import Lean.Elab.InfoTree import Lean.DocString import Lean.DeclarationRange import Lean.Compiler.InitAttr import Lean.Log namespace Lean def Syntax.prettyPrint (stx : Syntax) : Format := match stx.unsetTrailing.reprint with -- TODO use syntax pretty printer | some str => format str.toFormat | none => format stx def MacroScopesView.format (view : MacroScopesView) (mainModule : Name) : Format := Std.format <| if view.scopes.isEmpty then view.name else if view.mainModule == mainModule then view.scopes.foldl Name.mkNum (view.name ++ view.imported) else view.scopes.foldl Name.mkNum (view.name ++ view.imported ++ view.mainModule) namespace Elab def expandOptNamedPrio (stx : Syntax) : MacroM Nat := if stx.isNone then return eval_prio default else match stx[0] with | `(Parser.Command.namedPrio| (priority := $prio)) => evalPrio prio | _ => Macro.throwUnsupported structure MacroStackElem where before : Syntax after : Syntax abbrev MacroStack := List MacroStackElem /-- If `ref` does not have position information, then try to use macroStack -/ def getBetterRef (ref : Syntax) (macroStack : MacroStack) : Syntax := match ref.getPos? with | some _ => ref | none => match macroStack.find? (·.before.getPos? != none) with | some elem => elem.before | none => ref register_builtin_option pp.macroStack : Bool := { defValue := false group := "pp" descr := "dispaly macro expansion stack" } def addMacroStack {m} [Monad m] [MonadOptions m] (msgData : MessageData) (macroStack : MacroStack) : m MessageData := do if !pp.macroStack.get (← getOptions) then pure msgData else match macroStack with | [] => pure msgData | stack@(top::_) => let msgData := msgData ++ Format.line ++ "with resulting expansion" ++ indentD top.after pure $ stack.foldl (fun (msgData : MessageData) (elem : MacroStackElem) => msgData ++ Format.line ++ "while expanding" ++ indentD elem.before) msgData def checkSyntaxNodeKind [Monad m] [MonadEnv m] [MonadError m] (k : Name) : m Name := do if Parser.isValidSyntaxNodeKind (← getEnv) k then pure k else throwError "failed" def checkSyntaxNodeKindAtNamespaces [Monad m] [MonadEnv m] [MonadError m] (k : Name) : Name → m Name | n@(.str p _) => checkSyntaxNodeKind (n ++ k) <|> checkSyntaxNodeKindAtNamespaces k p | .anonymous => checkSyntaxNodeKind k | _ => throwError "failed" def checkSyntaxNodeKindAtCurrentNamespaces (k : Name) : AttrM Name := do let ctx ← read checkSyntaxNodeKindAtNamespaces k ctx.currNamespace def syntaxNodeKindOfAttrParam (defaultParserNamespace : Name) (stx : Syntax) : AttrM SyntaxNodeKind := do let k ← Attribute.Builtin.getId stx checkSyntaxNodeKindAtCurrentNamespaces k <|> checkSyntaxNodeKind (defaultParserNamespace ++ k) <|> throwError "invalid syntax node kind '{k}'" private unsafe def evalSyntaxConstantUnsafe (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax := env.evalConstCheck Syntax opts `Lean.Syntax constName @[implementedBy evalSyntaxConstantUnsafe] opaque evalSyntaxConstant (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax := throw "" unsafe def mkElabAttribute (γ) (attrDeclName attrBuiltinName attrName : Name) (parserNamespace : Name) (typeName : Name) (kind : String) : IO (KeyedDeclsAttribute γ) := KeyedDeclsAttribute.init { builtinName := attrBuiltinName name := attrName descr := kind ++ " elaborator" valueTypeName := typeName evalKey := fun _ stx => do let kind ← syntaxNodeKindOfAttrParam parserNamespace stx /- Recall that a `SyntaxNodeKind` is often the name of the parser, but this is not always true, and we must check it. -/ if (← getEnv).contains kind && (← getInfoState).enabled then pushInfoLeaf <| Info.ofTermInfo { elaborator := .anonymous lctx := {} expr := mkConst kind stx := stx[1] expectedType? := none } return kind onAdded := fun builtin declName => do if builtin then if let some doc ← findDocString? (← getEnv) declName then declareBuiltin (declName ++ `docString) (mkAppN (mkConst ``addBuiltinDocString) #[toExpr declName, toExpr doc]) if let some declRanges ← findDeclarationRanges? declName then declareBuiltin (declName ++ `declRange) (mkAppN (mkConst ``addBuiltinDeclarationRanges) #[toExpr declName, toExpr declRanges]) } attrDeclName unsafe def mkMacroAttributeUnsafe : IO (KeyedDeclsAttribute Macro) := mkElabAttribute Macro `Lean.Elab.macroAttribute `builtinMacro `macro Name.anonymous `Lean.Macro "macro" @[implementedBy mkMacroAttributeUnsafe] opaque mkMacroAttribute : IO (KeyedDeclsAttribute Macro) builtin_initialize macroAttribute : KeyedDeclsAttribute Macro ← mkMacroAttribute /-- Try to expand macro at syntax tree root and return macro declaration name and new syntax if successful. Return none if all macros threw `Macro.Exception.unsupportedSyntax`. -/ def expandMacroImpl? (env : Environment) : Syntax → MacroM (Option (Name × Except Macro.Exception Syntax)) := fun stx => do for e in macroAttribute.getEntries env stx.getKind do try let stx' ← withFreshMacroScope (e.value stx) return (e.declName, Except.ok stx') catch | Macro.Exception.unsupportedSyntax => pure () | ex => return (e.declName, Except.error ex) return none class MonadMacroAdapter (m : Type → Type) where getCurrMacroScope : m MacroScope getNextMacroScope : m MacroScope setNextMacroScope : MacroScope → m Unit instance (m n) [MonadLift m n] [MonadMacroAdapter m] : MonadMacroAdapter n := { getCurrMacroScope := liftM (MonadMacroAdapter.getCurrMacroScope : m _) getNextMacroScope := liftM (MonadMacroAdapter.getNextMacroScope : m _) setNextMacroScope := fun s => liftM (MonadMacroAdapter.setNextMacroScope s : m _) } def liftMacroM {α} {m : Type → Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] (x : MacroM α) : m α := do let env ← getEnv let currNamespace ← getCurrNamespace let openDecls ← getOpenDecls let methods := Macro.mkMethods { -- TODO: record recursive expansions in info tree? expandMacro? := fun stx => do match (← expandMacroImpl? env stx) with | some (_, stx?) => liftExcept stx? | none => return none hasDecl := fun declName => return env.contains declName getCurrNamespace := return currNamespace resolveNamespace := fun n => return ResolveName.resolveNamespace env currNamespace openDecls n resolveGlobalName := fun n => return ResolveName.resolveGlobalName env currNamespace openDecls n } match x { methods := methods ref := ← getRef currMacroScope := ← MonadMacroAdapter.getCurrMacroScope mainModule := env.mainModule currRecDepth := ← MonadRecDepth.getRecDepth maxRecDepth := ← MonadRecDepth.getMaxRecDepth } { macroScope := (← MonadMacroAdapter.getNextMacroScope) } with | EStateM.Result.error Macro.Exception.unsupportedSyntax _ => throwUnsupportedSyntax | EStateM.Result.error (Macro.Exception.error ref msg) _ => if msg == maxRecDepthErrorMessage then -- Make sure we can detect exception using `Exception.isMaxRecDepth` throwMaxRecDepthAt ref else throwErrorAt ref msg | EStateM.Result.ok a s => MonadMacroAdapter.setNextMacroScope s.macroScope s.traceMsgs.reverse.forM fun (clsName, msg) => trace clsName fun _ => msg return a @[inline] def adaptMacro {m : Type → Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] (x : Macro) (stx : Syntax) : m Syntax := liftMacroM (x stx) partial def mkUnusedBaseName (baseName : Name) : MacroM Name := do let currNamespace ← Macro.getCurrNamespace if ← Macro.hasDecl (currNamespace ++ baseName) then let rec loop (idx : Nat) := do let name := baseName.appendIndexAfter idx if ← Macro.hasDecl (currNamespace ++ name) then loop (idx+1) else return name loop 1 else return baseName def logException [Monad m] [MonadLog m] [AddMessageContext m] [MonadOptions m] [MonadLiftT IO m] (ex : Exception) : m Unit := do match ex with | Exception.error ref msg => logErrorAt ref msg | Exception.internal id _ => unless isAbortExceptionId id do let name ← id.getName logError m!"internal exception: {name}" def withLogging [Monad m] [MonadLog m] [MonadExcept Exception m] [AddMessageContext m] [MonadOptions m] [MonadLiftT IO m] (x : m Unit) : m Unit := do try x catch ex => logException ex @[inline] def trace [Monad m] [MonadLog m] [AddMessageContext m] [MonadOptions m] (cls : Name) (msg : Unit → MessageData) : m Unit := do if checkTraceOption (← getOptions) cls then logTrace cls (msg ()) def logDbgTrace [Monad m] [MonadLog m] [AddMessageContext m] [MonadOptions m] (msg : MessageData) : m Unit := do trace `Elab.debug fun _ => msg def nestedExceptionToMessageData [Monad m] [MonadLog m] (ex : Exception) : m MessageData := do let pos ← getRefPos match ex.getRef.getPos? with | none => return ex.toMessageData | some exPos => if pos == exPos then return ex.toMessageData else let exPosition := (← getFileMap).toPosition exPos return m!"{exPosition.line}:{exPosition.column} {ex.toMessageData}" def throwErrorWithNestedErrors [MonadError m] [Monad m] [MonadLog m] (msg : MessageData) (exs : Array Exception) : m α := do throwError "{msg}, errors {toMessageList (← exs.mapM fun | ex => nestedExceptionToMessageData ex)}" builtin_initialize registerTraceClass `Elab registerTraceClass `Elab.step end Lean.Elab
ff14900098cd6a9ee9d9b40cdc2ff5a56ee0f661
947b78d97130d56365ae2ec264df196ce769371a
/tests/bench/unionfind.lean
cfc91e9225a6c54ae93a1b129bce73f565b3f46e
[ "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
4,373
lean
def StateT' (m : Type → Type) (σ : Type) (α : Type) := σ → m (α × σ) namespace StateT' variables {m : Type → Type} [Monad m] {σ : Type} {α β : Type} @[inline] protected def pure (a : α) : StateT' m σ α := fun s => pure (a, s) @[inline] protected def bind (x : StateT' m σ α) (f : α → StateT' m σ β) : StateT' m σ β := fun s => do (a, s') ← x s; f a s' @[inline] def read : StateT' m σ σ := fun s => pure (s, s) @[inline] def write (s' : σ) : StateT' m σ Unit := fun s => pure ((), s') @[inline] def updt (f : σ → σ) : StateT' m σ Unit := fun s => pure ((), f s) instance : Monad (StateT' m σ) := {pure := @StateT'.pure _ _ _, bind := @StateT'.bind _ _ _} end StateT' def ExceptT' (m : Type → Type) (ε : Type) (α : Type) := m (Except ε α) namespace ExceptT' variables {m : Type → Type} [Monad m] {ε : Type} {α β : Type} @[inline] protected def pure (a : α) : ExceptT' m ε α := (pure (Except.ok a) : m (Except ε α)) @[inline] protected def bind (x : ExceptT' m ε α) (f : α → ExceptT' m ε β) : ExceptT' m ε β := (do { v ← x; match v with | Except.error e => pure (Except.error e) | Except.ok a => f a } : m (Except ε β)) @[inline] def error (e : ε) : ExceptT' m ε α := (pure (Except.error e) : m (Except ε α)) @[inline] def lift (x : m α) : ExceptT' m ε α := (do {a ← x; pure (Except.ok a) } : m (Except ε α)) instance : Monad (ExceptT' m ε) := {pure := @ExceptT'.pure _ _ _, bind := @ExceptT'.bind _ _ _} end ExceptT' abbrev Node := Nat structure nodeData := (find : Node) (rank : Nat := 0) abbrev ufData := Array nodeData abbrev M (α : Type) := ExceptT' (StateT' Id ufData) String α @[inline] def read : M ufData := ExceptT'.lift StateT'.read @[inline] def write (s : ufData) : M Unit := ExceptT'.lift (StateT'.write s) @[inline] def updt (f : ufData → ufData) : M Unit := ExceptT'.lift (StateT'.updt f) @[inline] def error {α : Type} (e : String) : M α := ExceptT'.error e def run {α : Type} (x : M α) (s : ufData := ∅) : Except String α × ufData := x s def capacity : M Nat := do d ← read; pure d.size def findEntryAux : Nat → Node → M nodeData | 0, n => error "out of fuel" | i+1, n => do s ← read; if h : n < s.size then do { let e := s.get ⟨n, h⟩; if e.find = n then pure e else do e₁ ← findEntryAux i e.find; updt (fun s => s.set! n e₁); pure e₁ } else error "invalid Node" def findEntry (n : Node) : M nodeData := do c ← capacity; findEntryAux c n def find (n : Node) : M Node := do e ← findEntry n; pure e.find def mk : M Node := do n ← capacity; updt $ fun s => s.push {find := n, rank := 1}; pure n def union (n₁ n₂ : Node) : M Unit := do r₁ ← findEntry n₁; r₂ ← findEntry n₂; if r₁.find = r₂.find then pure () else updt $ fun s => if r₁.rank < r₂.rank then s.set! r₁.find { find := r₂.find } else if r₁.rank = r₂.rank then let s₁ := s.set! r₁.find { find := r₂.find }; s₁.set! r₂.find { r₂ with rank := r₂.rank + 1 } else s.set! r₂.find { find := r₁.find } def mkNodes : Nat → M Unit | 0 => pure () | n+1 => do _ ← mk; mkNodes n def checkEq (n₁ n₂ : Node) : M Unit := do r₁ ← find n₁; r₂ ← find n₂; unless (r₁ = r₂) $ error "nodes are not equal" def mergePackAux : Nat → Nat → Nat → M Unit | 0, _, _ => pure () | i+1, n, d => do c ← capacity; if (n+d) < c then union n (n+d) *> mergePackAux i (n+1) d else pure () def mergePack (d : Nat) : M Unit := do c ← capacity; mergePackAux c 0 d def numEqsAux : Nat → Node → Nat → M Nat | 0, _, r => pure r | i+1, n, r => do c ← capacity; if n < c then do { n₁ ← find n; numEqsAux i (n+1) (if n = n₁ then r else r+1) } else pure r def numEqs : M Nat := do c ← capacity; numEqsAux c 0 0 def test (n : Nat) : M Nat := if n < 2 then error "input must be greater than 1" else do mkNodes n; mergePack 50000; mergePack 10000; mergePack 5000; mergePack 1000; numEqs def main (xs : List String) : IO UInt32 := let n := xs.head!.toNat!; match run (test n) with | (Except.ok v, s) => IO.println ("ok " ++ toString v) *> pure 0 | (Except.error e, s) => IO.println ("Error : " ++ e) *> pure 1
5fedfe3ca1f53ebd3e16c152fd8024ddc786f233
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/topology/sequences.lean
41d1001cec61351a57c3d0eb6b1fe7b1b791ce5d
[ "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
18,869
lean
/- Copyright (c) 2018 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Patrick Massot -/ import topology.bases import topology.subset_properties import topology.metric_space.basic /-! # Sequences in topological spaces In this file we define sequences in topological spaces and show how they are related to filters and the topology. In particular, we * define the sequential closure of a set and prove that it's contained in the closure, * define a type class "sequential_space" in which closure and sequential closure agree, * define sequential continuity and show that it coincides with continuity in sequential spaces, * provide an instance that shows that every first-countable (and in particular metric) space is a sequential space. * define sequential compactness, prove that compactness implies sequential compactness in first countable spaces, and prove they are equivalent for uniform spaces having a countable uniformity basis (in particular metric spaces). -/ open set filter open_locale topological_space variables {α : Type*} {β : Type*} local notation f ` ⟶ ` limit := tendsto f at_top (𝓝 limit) /-! ### Sequential closures, sequential continuity, and sequential spaces. -/ section topological_space variables [topological_space α] [topological_space β] /-- A sequence converges in the sence of topological spaces iff the associated statement for filter holds. -/ lemma topological_space.seq_tendsto_iff {x : ℕ → α} {limit : α} : tendsto x at_top (𝓝 limit) ↔ ∀ U : set α, limit ∈ U → is_open U → ∃ N, ∀ n ≥ N, (x n) ∈ U := (at_top_basis.tendsto_iff (nhds_basis_opens limit)).trans $ by simp only [and_imp, exists_prop, true_and, set.mem_Ici, ge_iff_le, id] /-- The sequential closure of a subset M ⊆ α of a topological space α is the set of all p ∈ α which arise as limit of sequences in M. -/ def sequential_closure (M : set α) : set α := {p | ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ M) ∧ (x ⟶ p)} lemma subset_sequential_closure (M : set α) : M ⊆ sequential_closure M := assume p (_ : p ∈ M), show p ∈ sequential_closure M, from ⟨λ n, p, assume n, ‹p ∈ M›, tendsto_const_nhds⟩ /-- A set `s` is sequentially closed if for any converging sequence `x n` of elements of `s`, the limit belongs to `s` as well. -/ def is_seq_closed (s : set α) : Prop := s = sequential_closure s /-- A convenience lemma for showing that a set is sequentially closed. -/ lemma is_seq_closed_of_def {A : set α} (h : ∀(x : ℕ → α) (p : α), (∀ n : ℕ, x n ∈ A) → (x ⟶ p) → p ∈ A) : is_seq_closed A := show A = sequential_closure A, from subset.antisymm (subset_sequential_closure A) (show ∀ p, p ∈ sequential_closure A → p ∈ A, from (assume p ⟨x, _, _⟩, show p ∈ A, from h x p ‹∀ n : ℕ, ((x n) ∈ A)› ‹(x ⟶ p)›)) /-- The sequential closure of a set is contained in the closure of that set. The converse is not true. -/ lemma sequential_closure_subset_closure (M : set α) : sequential_closure M ⊆ closure M := assume p ⟨x, xM, xp⟩, mem_closure_of_tendsto xp (univ_mem_sets' xM) /-- A set is sequentially closed if it is closed. -/ lemma is_seq_closed_of_is_closed (M : set α) (_ : is_closed M) : is_seq_closed M := suffices sequential_closure M ⊆ M, from set.eq_of_subset_of_subset (subset_sequential_closure M) this, calc sequential_closure M ⊆ closure M : sequential_closure_subset_closure M ... = M : is_closed.closure_eq ‹is_closed M› /-- The limit of a convergent sequence in a sequentially closed set is in that set.-/ lemma mem_of_is_seq_closed {A : set α} (_ : is_seq_closed A) {x : ℕ → α} (_ : ∀ n, x n ∈ A) {limit : α} (_ : (x ⟶ limit)) : limit ∈ A := have limit ∈ sequential_closure A, from show ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ A) ∧ (x ⟶ limit), from ⟨x, ‹∀ n, x n ∈ A›, ‹(x ⟶ limit)›⟩, eq.subst (eq.symm ‹is_seq_closed A›) ‹limit ∈ sequential_closure A› /-- The limit of a convergent sequence in a closed set is in that set.-/ lemma mem_of_is_closed_sequential {A : set α} (_ : is_closed A) {x : ℕ → α} (_ : ∀ n, x n ∈ A) {limit : α} (_ : x ⟶ limit) : limit ∈ A := mem_of_is_seq_closed (is_seq_closed_of_is_closed A ‹is_closed A›) ‹∀ n, x n ∈ A› ‹(x ⟶ limit)› /-- A sequential space is a space in which 'sequences are enough to probe the topology'. This can be formalised by demanding that the sequential closure and the closure coincide. The following statements show that other topological properties can be deduced from sequences in sequential spaces. -/ class sequential_space (α : Type*) [topological_space α] : Prop := (sequential_closure_eq_closure : ∀ M : set α, sequential_closure M = closure M) /-- In a sequential space, a set is closed iff it's sequentially closed. -/ lemma is_seq_closed_iff_is_closed [sequential_space α] {M : set α} : is_seq_closed M ↔ is_closed M := iff.intro (assume _, closure_eq_iff_is_closed.mp (eq.symm (calc M = sequential_closure M : by assumption ... = closure M : sequential_space.sequential_closure_eq_closure M))) (is_seq_closed_of_is_closed M) /-- In a sequential space, a point belongs to the closure of a set iff it is a limit of a sequence taking values in this set. -/ lemma mem_closure_iff_seq_limit [sequential_space α] {s : set α} {a : α} : a ∈ closure s ↔ ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ s) ∧ (x ⟶ a) := by { rw ← sequential_space.sequential_closure_eq_closure, exact iff.rfl } /-- A function between topological spaces is sequentially continuous if it commutes with limit of convergent sequences. -/ def sequentially_continuous (f : α → β) : Prop := ∀ (x : ℕ → α), ∀ {limit : α}, (x ⟶ limit) → (f∘x ⟶ f limit) /- A continuous function is sequentially continuous. -/ lemma continuous.to_sequentially_continuous {f : α → β} (_ : continuous f) : sequentially_continuous f := assume x limit (_ : x ⟶ limit), have tendsto f (𝓝 limit) (𝓝 (f limit)), from continuous.tendsto ‹continuous f› limit, show (f ∘ x) ⟶ (f limit), from tendsto.comp this ‹(x ⟶ limit)› /-- In a sequential space, continuity and sequential continuity coincide. -/ lemma continuous_iff_sequentially_continuous {f : α → β} [sequential_space α] : continuous f ↔ sequentially_continuous f := iff.intro (assume _, ‹continuous f›.to_sequentially_continuous) (assume : sequentially_continuous f, show continuous f, from suffices h : ∀ {A : set β}, is_closed A → is_seq_closed (f ⁻¹' A), from continuous_iff_is_closed.mpr (assume A _, is_seq_closed_iff_is_closed.mp $ h ‹is_closed A›), assume A (_ : is_closed A), is_seq_closed_of_def $ assume (x : ℕ → α) p (_ : ∀ n, f (x n) ∈ A) (_ : x ⟶ p), have (f ∘ x) ⟶ (f p), from ‹sequentially_continuous f› x ‹(x ⟶ p)›, show f p ∈ A, from mem_of_is_closed_sequential ‹is_closed A› ‹∀ n, f (x n) ∈ A› ‹(f∘x ⟶ f p)›) end topological_space namespace topological_space namespace first_countable_topology variables [topological_space α] [first_countable_topology α] /-- Every first-countable space is sequential. -/ @[priority 100] -- see Note [lower instance priority] instance : sequential_space α := ⟨show ∀ M, sequential_closure M = closure M, from assume M, suffices closure M ⊆ sequential_closure M, from set.subset.antisymm (sequential_closure_subset_closure M) this, -- For every p ∈ closure M, we need to construct a sequence x in M that converges to p: assume (p : α) (hp : p ∈ closure M), -- Since we are in a first-countable space, the neighborhood filter around `p` has a decreasing -- basis `U` indexed by `ℕ`. let ⟨U, hU ⟩ := (nhds_generated_countable p).has_antimono_basis in -- Since `p ∈ closure M`, there is an element in each `M ∩ U i` have hp : ∀ (i : ℕ), ∃ (y : α), y ∈ M ∧ y ∈ U i, by simpa using (mem_closure_iff_nhds_basis hU.1).mp hp, begin -- The axiom of (countable) choice builds our sequence from the later fact choose u hu using hp, rw forall_and_distrib at hu, -- It clearly takes values in `M` use [u, hu.1], -- and converges to `p` because the basis is decreasing. apply hU.tendsto hu.2, end⟩ end first_countable_topology end topological_space section seq_compact open topological_space topological_space.first_countable_topology variables [topological_space α] /-- A set `s` is sequentially compact if every sequence taking values in `s` has a converging subsequence. -/ def is_seq_compact (s : set α) := ∀ ⦃u : ℕ → α⦄, (∀ n, u n ∈ s) → ∃ (x ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) /-- A space `α` is sequentially compact if every sequence in `α` has a converging subsequence. -/ class seq_compact_space (α : Type*) [topological_space α] : Prop := (seq_compact_univ : is_seq_compact (univ : set α)) lemma is_seq_compact.subseq_of_frequently_in {s : set α} (hs : is_seq_compact s) {u : ℕ → α} (hu : ∃ᶠ n in at_top, u n ∈ s) : ∃ (x ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := let ⟨ψ, hψ, huψ⟩ := extraction_of_frequently_at_top hu, ⟨x, x_in, φ, hφ, h⟩ := hs huψ in ⟨x, x_in, ψ ∘ φ, hψ.comp hφ, h⟩ lemma seq_compact_space.tendsto_subseq [seq_compact_space α] (u : ℕ → α) : ∃ x (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := let ⟨x, _, φ, mono, h⟩ := seq_compact_space.seq_compact_univ (by simp : ∀ n, u n ∈ univ) in ⟨x, φ, mono, h⟩ section first_countable_topology variables [first_countable_topology α] open topological_space.first_countable_topology lemma is_compact.is_seq_compact {s : set α} (hs : is_compact s) : is_seq_compact s := λ u u_in, let ⟨x, x_in, hx⟩ := @hs (map u at_top) _ (le_principal_iff.mpr (univ_mem_sets' u_in : _)) in ⟨x, x_in, tendsto_subseq hx⟩ lemma is_compact.tendsto_subseq' {s : set α} {u : ℕ → α} (hs : is_compact s) (hu : ∃ᶠ n in at_top, u n ∈ s) : ∃ (x ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := hs.is_seq_compact.subseq_of_frequently_in hu lemma is_compact.tendsto_subseq {s : set α} {u : ℕ → α} (hs : is_compact s) (hu : ∀ n, u n ∈ s) : ∃ (x ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := hs.is_seq_compact hu @[priority 100] -- see Note [lower instance priority] instance first_countable_topology.seq_compact_of_compact [compact_space α] : seq_compact_space α := ⟨compact_univ.is_seq_compact⟩ lemma compact_space.tendsto_subseq [compact_space α] (u : ℕ → α) : ∃ x (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := seq_compact_space.tendsto_subseq u end first_countable_topology end seq_compact section uniform_space_seq_compact open_locale uniformity open uniform_space prod variables [uniform_space β] {s : set β} lemma lebesgue_number_lemma_seq {ι : Type*} {c : ι → set β} (hs : is_seq_compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) (hU : is_countably_generated (𝓤 β)) : ∃ V ∈ 𝓤 β, symmetric_rel V ∧ ∀ x ∈ s, ∃ i, ball x V ⊆ c i := begin classical, obtain ⟨V, hV, Vsymm⟩ : ∃ V : ℕ → set (β × β), (𝓤 β).has_antimono_basis (λ _, true) V ∧ ∀ n, swap ⁻¹' V n = V n, from uniform_space.has_seq_basis hU, clear hU, suffices : ∃ n, ∀ x ∈ s, ∃ i, ball x (V n) ⊆ c i, { cases this with n hn, exact ⟨V n, hV.to_has_basis.mem_of_mem trivial, Vsymm n, hn⟩ }, by_contradiction H, obtain ⟨x, x_in, hx⟩ : ∃ x : ℕ → β, (∀ n, x n ∈ s) ∧ ∀ n i, ¬ ball (x n) (V n) ⊆ c i, { push_neg at H, choose x hx using H, exact ⟨x, forall_and_distrib.mp hx⟩ }, clear H, obtain ⟨x₀, x₀_in, φ, φ_mono, hlim⟩ : ∃ (x₀ ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ (x ∘ φ ⟶ x₀), from hs x_in, clear hs, obtain ⟨i₀, x₀_in⟩ : ∃ i₀, x₀ ∈ c i₀, { rcases hc₂ x₀_in with ⟨_, ⟨i₀, rfl⟩, x₀_in_c⟩, exact ⟨i₀, x₀_in_c⟩ }, clear hc₂, obtain ⟨n₀, hn₀⟩ : ∃ n₀, ball x₀ (V n₀) ⊆ c i₀, { rcases (nhds_basis_uniformity hV.to_has_basis).mem_iff.mp (is_open_iff_mem_nhds.mp (hc₁ i₀) _ x₀_in) with ⟨n₀, _, h⟩, use n₀, rwa ← ball_eq_of_symmetry (Vsymm n₀) at h }, clear hc₁, obtain ⟨W, W_in, hWW⟩ : ∃ W ∈ 𝓤 β, W ○ W ⊆ V n₀, from comp_mem_uniformity_sets (hV.to_has_basis.mem_of_mem trivial), obtain ⟨N, x_φ_N_in, hVNW⟩ : ∃ N, x (φ N) ∈ ball x₀ W ∧ V (φ N) ⊆ W, { obtain ⟨N₁, h₁⟩ : ∃ N₁, ∀ n ≥ N₁, x (φ n) ∈ ball x₀ W, from (tendsto_at_top' (λ (b : ℕ), (x ∘ φ) b) (𝓝 x₀)).mp hlim _ (mem_nhds_left x₀ W_in), obtain ⟨N₂, h₂⟩ : ∃ N₂, V (φ N₂) ⊆ W, { rcases hV.to_has_basis.mem_iff.mp W_in with ⟨N, _, hN⟩, use N, exact subset.trans (hV.decreasing trivial trivial $ φ_mono.id_le _) hN }, have : φ N₂ ≤ φ (max N₁ N₂), from φ_mono.le_iff_le.mpr (le_max_right _ _), exact ⟨max N₁ N₂, h₁ _ (le_max_left _ _), subset.trans (hV.decreasing trivial trivial this) h₂⟩ }, suffices : ball (x (φ N)) (V (φ N)) ⊆ c i₀, from hx (φ N) i₀ this, calc ball (x $ φ N) (V $ φ N) ⊆ ball (x $ φ N) W : preimage_mono hVNW ... ⊆ ball x₀ (V n₀) : ball_subset_of_comp_subset x_φ_N_in hWW ... ⊆ c i₀ : hn₀, end lemma is_seq_compact.totally_bounded (h : is_seq_compact s) : totally_bounded s := begin classical, apply totally_bounded_of_forall_symm, unfold is_seq_compact at h, contrapose! h, rcases h with ⟨V, V_in, V_symm, h⟩, simp_rw [not_subset] at h, have : ∀ (t : set β), finite t → ∃ a, a ∈ s ∧ a ∉ ⋃ y ∈ t, ball y V, { intros t ht, obtain ⟨a, a_in, H⟩ : ∃ a ∈ s, ∀ (x : β), x ∈ t → (x, a) ∉ V, by simpa [ht] using h t, use [a, a_in], intro H', obtain ⟨x, x_in, hx⟩ := mem_bUnion_iff.mp H', exact H x x_in hx }, cases seq_of_forall_finite_exists this with u hu, clear h this, simp [forall_and_distrib] at hu, cases hu with u_in hu, use [u, u_in], clear u_in, intros x x_in φ, intros hφ huφ, obtain ⟨N, hN⟩ : ∃ N, ∀ p q, p ≥ N → q ≥ N → (u (φ p), u (φ q)) ∈ V, from (cauchy_seq_of_tendsto_nhds _ huφ).mem_entourage V_in, specialize hN N (N+1) (le_refl N) (nat.le_succ N), specialize hu (φ $ N+1) (φ N) (hφ $ lt_add_one N), exact hu hN, end protected lemma is_seq_compact.is_compact (h : is_countably_generated $ 𝓤 β) (hs : is_seq_compact s) : is_compact s := begin classical, rw compact_iff_finite_subcover, intros ι U Uop s_sub, rcases lebesgue_number_lemma_seq hs Uop s_sub h with ⟨V, V_in, Vsymm, H⟩, rcases totally_bounded_iff_subset.mp hs.totally_bounded V V_in with ⟨t,t_sub, tfin, ht⟩, have : ∀ x : t, ∃ (i : ι), ball x.val V ⊆ U i, { rintros ⟨x, x_in⟩, exact H x (t_sub x_in) }, choose i hi using this, haveI : fintype t := tfin.fintype, use finset.image i finset.univ, transitivity ⋃ y ∈ t, ball y V, { intros x x_in, specialize ht x_in, rw mem_bUnion_iff at *, simp_rw ball_eq_of_symmetry Vsymm, exact ht }, { apply bUnion_subset_bUnion, intros x x_in, exact ⟨i ⟨x, x_in⟩, finset.mem_image_of_mem _ (finset.mem_univ _), hi ⟨x, x_in⟩⟩ }, end protected lemma uniform_space.compact_iff_seq_compact (h : is_countably_generated $ 𝓤 β) : is_compact s ↔ is_seq_compact s := begin haveI := uniform_space.first_countable_topology h, exact ⟨λ H, H.is_seq_compact, λ H, H.is_compact h⟩ end lemma uniform_space.compact_space_iff_seq_compact_space (H : is_countably_generated $ 𝓤 β) : compact_space β ↔ seq_compact_space β := have key : is_compact univ ↔ is_seq_compact univ := uniform_space.compact_iff_seq_compact H, ⟨λ ⟨h⟩, ⟨key.mp h⟩, λ ⟨h⟩, ⟨key.mpr h⟩⟩ end uniform_space_seq_compact section metric_seq_compact variables [metric_space β] {s : set β} open metric /-- A version of Bolzano-Weistrass: in a metric space, is_compact s ↔ is_seq_compact s -/ lemma metric.compact_iff_seq_compact : is_compact s ↔ is_seq_compact s := uniform_space.compact_iff_seq_compact emetric.uniformity_has_countable_basis /-- A version of Bolzano-Weistrass: in a proper metric space (eg. $ℝ^n$), every bounded sequence has a converging subsequence. This version assumes only that the sequence is frequently in some bounded set. -/ lemma tendsto_subseq_of_frequently_bounded [proper_space β] (hs : bounded s) {u : ℕ → β} (hu : ∃ᶠ n in at_top, u n ∈ s) : ∃ b ∈ closure s, ∃ φ : ℕ → ℕ, strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 b) := begin have hcs : is_compact (closure s) := compact_iff_closed_bounded.mpr ⟨is_closed_closure, bounded_closure_of_bounded hs⟩, replace hcs : is_seq_compact (closure s), by rwa metric.compact_iff_seq_compact at hcs, have hu' : ∃ᶠ n in at_top, u n ∈ closure s, { apply frequently.mono hu, intro n, apply subset_closure }, exact hcs.subseq_of_frequently_in hu', end /-- A version of Bolzano-Weistrass: in a proper metric space (eg. $ℝ^n$), every bounded sequence has a converging subsequence. -/ lemma tendsto_subseq_of_bounded [proper_space β] (hs : bounded s) {u : ℕ → β} (hu : ∀ n, u n ∈ s) : ∃ b ∈ closure s, ∃ φ : ℕ → ℕ, strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 b) := tendsto_subseq_of_frequently_bounded hs $ frequently_of_forall hu lemma metric.compact_space_iff_seq_compact_space : compact_space β ↔ seq_compact_space β := uniform_space.compact_space_iff_seq_compact_space emetric.uniformity_has_countable_basis lemma seq_compact.lebesgue_number_lemma_of_metric {ι : Type*} {c : ι → set β} (hs : is_seq_compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) : ∃ δ > 0, ∀ x ∈ s, ∃ i, ball x δ ⊆ c i := begin rcases lebesgue_number_lemma_seq hs hc₁ hc₂ emetric.uniformity_has_countable_basis with ⟨V, V_in, _, hV⟩, rcases uniformity_basis_dist.mem_iff.mp V_in with ⟨δ, δ_pos, h⟩, use [δ, δ_pos], intros x x_in, rcases hV x x_in with ⟨i, hi⟩, use i, have := ball_mono h x, rw ball_eq_ball' at this, exact subset.trans this hi, end end metric_seq_compact
b9a540f9f5c7be5fade9c87090ea73c81089d05d
c9ba4946202cfd1e2586e71960dfed00503dcdf4
/src/meta_k/default_symbols.lean
3c0fc1c39d9e45b2c0cfa4789e2c370b67e9f48f
[]
no_license
ammkrn/learning_semantics_of_k
f55f669b369e32ef8407c16521b21ac5c106dc4d
c1487b538e1decc0f1fd389cd36bc36d2da012ab
refs/heads/master
1,588,081,593,954
1,552,449,093,000
1,552,449,093,000
175,315,800
0
0
null
null
null
null
UTF-8
Lean
false
false
3,957
lean
import .meta_sort import .meta_symbol def k_list : #SymbolList := [ #symbol "#nilCharList" [%Char] [] %CharList, #symbol "#consCharList" [%Char] [%CharList, %Char] %CharList, #symbol "#epsilon" [%Char] [%CharList, %Char] %CharList, #symbol "#appendKCharList" [%Char] [%CharList, %CharList] %CharList, #symbol "#concat" [%Char] [%CharList, %CharList] %CharList, #symbol "#sort" [%Sort] [%String, %SortList] %Sort, #symbol "#nilSortList" [%Sort] [] %SortList, #symbol "#consSortList" [%Sort] [%Sort, %SortList] %SortList, #symbol "#appendSortList" [%Sort] [%SortList, %SortList] %SortList, #symbol "#inSortList" [%Sort] [%Sort, %Sort, %SortList] %Pattern, #symbol "#deleteSortList" [%Sort] [%Sort, %Sort, %SortList] %Sort, #symbol "#symbol" [] [%String, %SortList, %SortList, %Sort] %Symbol, #symbol "#getArgumentSorts" [] [%Symbol] %SortList, #symbol "#nilSymbolList" [%Symbol] [] %SymbolList, #symbol "#consSymbolList" [%Symbol] [%Symbol, %SymbolList] %SymbolList, #symbol "#appendSymbolList" [%Symbol] [%SymbolList, %SymbolList] %SymbolList, #symbol "#variable" [] [%String, %Sort] %Variable, #symbol "#nilVariableList" [%Variable] [] %VariableList, #symbol "#consVariableList" [%Variable] [%Variable, %VariableList] %VariableList, #symbol "#inSymbolList" [%Sort] [%Symbol, %SymbolList] %Pattern, #symbol "#inVariableList" [%Sort] [%Variable, %VariableList] %Pattern, #symbol "#deleteSymbolList" [] [%Symbol, %SymbolList] %SymbolList, #symbol "#'ceil" [] [%Sort, %Sort] %Symbol, #symbol "#appendVariableList" [%Variable] [%VariableList, %VariableList] %VariableList, #symbol "#deleteVariableList" [] [%Variable, %VariableList] %VariableList, #symbol "#variableAsPattern" [] [%Variable] %Pattern, #symbol "#variablePattern" [] [%Variable] %Pattern, #symbol "#application" [] [%Symbol, %PatternList] %Pattern, #symbol "#and" [] [%Sort, %Pattern, %Pattern] %Pattern, #symbol "#not" [] [%Sort, %Pattern] %Pattern, #symbol "#exists" [] [%Sort, %Variable, %Pattern] %Pattern, #symbol "#or" [] [%Sort, %Pattern, %Pattern] %Pattern, #symbol "#implies" [] [%Sort, %Pattern, %Pattern] %Pattern, #symbol "#iff" [] [%Sort, %Pattern, %Pattern] %Pattern, #symbol "#forall" [] [%Sort, %Variable, %Pattern] %Pattern, #symbol "#ceil" [] [%Sort, %Sort, %Pattern] %Pattern, #symbol "#floor" [] [%Sort, %Sort, %Pattern] %Pattern, #symbol "#equals" [] [%Sort, %Sort, %Pattern, %Pattern] %Pattern, #symbol "#in" [] [%Sort, %Sort, %Pattern, %Pattern] %Pattern, #symbol "#top" [] [%Sort] %Pattern, #symbol "#bottom" [] [%Sort] %Pattern, #symbol "#nilPatternList" [%Sort] [] %PatternList, #symbol "#consPatternList" [%Sort] [%Pattern, %PatternList] %PatternList, #symbol "#appendPatternList" [%Sort] [%PatternList, %PatternList] %PatternList, #symbol "#inPatternList" [%Sort] [%Sort, %Pattern, %PatternList] %Sort, #symbol "#deletePatternList" [%Sort] [%Sort, %Pattern, %PatternList] %PatternList, #symbol "getFV" [] [%Pattern] %VariableList, #symbol "getFVFromPatterns" [] [%PatternList] %VariableList, #symbol "occursFree" [%Sort] [%Variable, %Pattern] %Sort, #symbol "freshName" [] [%PatternList] %String, #symbol "substitute" [] [%Variable, %Pattern, %Pattern] %Pattern, #symbol "substitutePatterns" [] [%Variable, %Pattern, %PatternList] %PatternList, #symbol "sortDeclared" [%Sort] [%Sort, %SortList] %Pattern, #symbol "sortsDeclared" [%Sort] [%SortList, %SortList] %Pattern, #symbol "symbolDeclared" [%Sort] [%Symbol, %SymbolList] %Pattern, #symbol "axiomDeclared" [%Sort] [%Pattern, %PatternList] %Pattern, #symbol "wellFormed" [%Sort] [%Pattern] %Sort, #symbol "wellFormedPatterns" [%Sort] [%PatternList] %Pattern, #symbol "getSortsFromPatterns" [] [%PatternList] %SortList, #symbol "provable" [%Sort] [%Pattern] %Pattern ]
6393508ddec0d2ae379bbba7a5269fe52da68dc5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/geometry/euclidean/angle/unoriented/right_angle.lean
21e7df7841621def937ec3773a0006d9f7a099e5
[ "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
26,764
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import analysis.special_functions.trigonometric.arctan import geometry.euclidean.angle.unoriented.affine /-! # Right-angled triangles This file proves basic geometrical results about distances and angles in (possibly degenerate) right-angled triangles in real inner product spaces and Euclidean affine spaces. ## Implementation notes Results in this file are generally given in a form with only those non-degeneracy conditions needed for the particular result, rather than requiring affine independence of the points of a triangle unnecessarily. ## References * https://en.wikipedia.org/wiki/Pythagorean_theorem -/ noncomputable theory open_locale big_operators open_locale euclidean_geometry open_locale real open_locale real_inner_product_space namespace inner_product_geometry variables {V : Type*} [inner_product_space ℝ V] /-- Pythagorean theorem, if-and-only-if vector angle form. -/ lemma norm_add_sq_eq_norm_sq_add_norm_sq_iff_angle_eq_pi_div_two (x y : V) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ ↔ angle x y = π / 2 := begin rw norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero, exact inner_eq_zero_iff_angle_eq_pi_div_two x y end /-- Pythagorean theorem, vector angle form. -/ lemma norm_add_sq_eq_norm_sq_add_norm_sq' (x y : V) (h : angle x y = π / 2) : ‖x + y‖ * ‖x + y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ := (norm_add_sq_eq_norm_sq_add_norm_sq_iff_angle_eq_pi_div_two x y).2 h /-- Pythagorean theorem, subtracting vectors, if-and-only-if vector angle form. -/ lemma norm_sub_sq_eq_norm_sq_add_norm_sq_iff_angle_eq_pi_div_two (x y : V) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ ↔ angle x y = π / 2 := begin rw norm_sub_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero, exact inner_eq_zero_iff_angle_eq_pi_div_two x y end /-- Pythagorean theorem, subtracting vectors, vector angle form. -/ lemma norm_sub_sq_eq_norm_sq_add_norm_sq' (x y : V) (h : angle x y = π / 2) : ‖x - y‖ * ‖x - y‖ = ‖x‖ * ‖x‖ + ‖y‖ * ‖y‖ := (norm_sub_sq_eq_norm_sq_add_norm_sq_iff_angle_eq_pi_div_two x y).2 h /-- An angle in a right-angled triangle expressed using `arccos`. -/ lemma angle_add_eq_arccos_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : angle x (x + y) = real.arccos (‖x‖ / ‖x + y‖) := begin rw [angle, inner_add_right, h, add_zero, real_inner_self_eq_norm_mul_norm], by_cases hx : ‖x‖ = 0, { simp [hx] }, rw [div_mul_eq_div_div, mul_self_div_self] end /-- An angle in a right-angled triangle expressed using `arcsin`. -/ lemma angle_add_eq_arcsin_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0 ∨ y ≠ 0) : angle x (x + y) = real.arcsin (‖y‖ / ‖x + y‖) := begin have hxy : ‖x + y‖ ^ 2 ≠ 0, { rw [pow_two, norm_add_sq_eq_norm_sq_add_norm_sq_real h, ne_comm], refine ne_of_lt _, rcases h0 with h0 | h0, { exact left.add_pos_of_pos_of_nonneg (mul_self_pos.2 (norm_ne_zero_iff.2 h0)) (mul_self_nonneg _) }, { exact left.add_pos_of_nonneg_of_pos (mul_self_nonneg _) (mul_self_pos.2 (norm_ne_zero_iff.2 h0)) } }, rw [angle_add_eq_arccos_of_inner_eq_zero h, real.arccos_eq_arcsin (div_nonneg (norm_nonneg _) (norm_nonneg _)), div_pow, one_sub_div hxy], nth_rewrite 0 [pow_two], rw [norm_add_sq_eq_norm_sq_add_norm_sq_real h, pow_two, add_sub_cancel', ←pow_two, ←div_pow, real.sqrt_sq (div_nonneg (norm_nonneg _) (norm_nonneg _))] end /-- An angle in a right-angled triangle expressed using `arctan`. -/ lemma angle_add_eq_arctan_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0) : angle x (x + y) = real.arctan (‖y‖ / ‖x‖) := begin rw [angle_add_eq_arcsin_of_inner_eq_zero h (or.inl h0), real.arctan_eq_arcsin, ←div_mul_eq_div_div, norm_add_eq_sqrt_iff_real_inner_eq_zero.2 h], nth_rewrite 2 [←real.sqrt_sq (norm_nonneg x)], rw [←real.sqrt_mul (sq_nonneg _), div_pow, pow_two, pow_two, mul_add, mul_one, mul_div, mul_comm (‖x‖ * ‖x‖), ←mul_div, div_self (mul_self_pos.2 (norm_ne_zero_iff.2 h0)).ne', mul_one] end /-- An angle in a non-degenerate right-angled triangle is positive. -/ lemma angle_add_pos_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x = 0 ∨ y ≠ 0) : 0 < angle x (x + y) := begin rw [angle_add_eq_arccos_of_inner_eq_zero h, real.arccos_pos, norm_add_eq_sqrt_iff_real_inner_eq_zero.2 h], by_cases hx : x = 0, { simp [hx] }, rw [div_lt_one (real.sqrt_pos.2 (left.add_pos_of_pos_of_nonneg (mul_self_pos.2 (norm_ne_zero_iff.2 hx)) (mul_self_nonneg _))), real.lt_sqrt (norm_nonneg _), pow_two], simpa [hx] using h0 end /-- An angle in a right-angled triangle is at most `π / 2`. -/ lemma angle_add_le_pi_div_two_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : angle x (x + y) ≤ π / 2 := begin rw [angle_add_eq_arccos_of_inner_eq_zero h, real.arccos_le_pi_div_two], exact div_nonneg (norm_nonneg _) (norm_nonneg _) end /-- An angle in a non-degenerate right-angled triangle is less than `π / 2`. -/ lemma angle_add_lt_pi_div_two_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0) : angle x (x + y) < π / 2 := begin rw [angle_add_eq_arccos_of_inner_eq_zero h, real.arccos_lt_pi_div_two, norm_add_eq_sqrt_iff_real_inner_eq_zero.2 h], exact div_pos (norm_pos_iff.2 h0) (real.sqrt_pos.2 (left.add_pos_of_pos_of_nonneg (mul_self_pos.2 (norm_ne_zero_iff.2 h0)) (mul_self_nonneg _))) end /-- The cosine of an angle in a right-angled triangle as a ratio of sides. -/ lemma cos_angle_add_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : real.cos (angle x (x + y)) = ‖x‖ / ‖x + y‖ := begin rw [angle_add_eq_arccos_of_inner_eq_zero h, real.cos_arccos (le_trans (by norm_num) (div_nonneg (norm_nonneg _) (norm_nonneg _))) (div_le_one_of_le _ (norm_nonneg _))], rw [mul_self_le_mul_self_iff (norm_nonneg _) (norm_nonneg _), norm_add_sq_eq_norm_sq_add_norm_sq_real h], exact le_add_of_nonneg_right (mul_self_nonneg _) end /-- The sine of an angle in a right-angled triangle as a ratio of sides. -/ lemma sin_angle_add_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0 ∨ y ≠ 0) : real.sin (angle x (x + y)) = ‖y‖ / ‖x + y‖ := begin rw [angle_add_eq_arcsin_of_inner_eq_zero h h0, real.sin_arcsin (le_trans (by norm_num) (div_nonneg (norm_nonneg _) (norm_nonneg _))) (div_le_one_of_le _ (norm_nonneg _))], rw [mul_self_le_mul_self_iff (norm_nonneg _) (norm_nonneg _), norm_add_sq_eq_norm_sq_add_norm_sq_real h], exact le_add_of_nonneg_left (mul_self_nonneg _) end /-- The tangent of an angle in a right-angled triangle as a ratio of sides. -/ lemma tan_angle_add_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : real.tan (angle x (x + y)) = ‖y‖ / ‖x‖ := begin by_cases h0 : x = 0, { simp [h0] }, rw [angle_add_eq_arctan_of_inner_eq_zero h h0, real.tan_arctan] end /-- The cosine of an angle in a right-angled triangle multiplied by the hypotenuse equals the adjacent side. -/ lemma cos_angle_add_mul_norm_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : real.cos (angle x (x + y)) * ‖x + y‖ = ‖x‖ := begin rw cos_angle_add_of_inner_eq_zero h, by_cases hxy : ‖x + y‖ = 0, { have h' := norm_add_sq_eq_norm_sq_add_norm_sq_real h, rw [hxy, zero_mul, eq_comm, add_eq_zero_iff' (mul_self_nonneg ‖x‖) (mul_self_nonneg ‖y‖), mul_self_eq_zero] at h', simp [h'.1] }, { exact div_mul_cancel _ hxy } end /-- The sine of an angle in a right-angled triangle multiplied by the hypotenuse equals the opposite side. -/ lemma sin_angle_add_mul_norm_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : real.sin (angle x (x + y)) * ‖x + y‖ = ‖y‖ := begin by_cases h0 : x = 0 ∧ y = 0, { simp [h0] }, rw not_and_distrib at h0, rw [sin_angle_add_of_inner_eq_zero h h0, div_mul_cancel], rw [←mul_self_ne_zero, norm_add_sq_eq_norm_sq_add_norm_sq_real h], refine (ne_of_lt _).symm, rcases h0 with h0 | h0, { exact left.add_pos_of_pos_of_nonneg (mul_self_pos.2 (norm_ne_zero_iff.2 h0)) (mul_self_nonneg _) }, { exact left.add_pos_of_nonneg_of_pos (mul_self_nonneg _) (mul_self_pos.2 (norm_ne_zero_iff.2 h0)) } end /-- The tangent of an angle in a right-angled triangle multiplied by the adjacent side equals the opposite side. -/ lemma tan_angle_add_mul_norm_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0 ∨ y = 0) : real.tan (angle x (x + y)) * ‖x‖ = ‖y‖ := begin rw [tan_angle_add_of_inner_eq_zero h], rcases h0 with h0 | h0; simp [h0] end /-- A side of a right-angled triangle divided by the cosine of the adjacent angle equals the hypotenuse. -/ lemma norm_div_cos_angle_add_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0 ∨ y = 0) : ‖x‖ / real.cos (angle x (x + y)) = ‖x + y‖ := begin rw cos_angle_add_of_inner_eq_zero h, rcases h0 with h0 | h0, { rw [div_div_eq_mul_div, mul_comm, div_eq_mul_inv, mul_inv_cancel_right₀ (norm_ne_zero_iff.2 h0)] }, { simp [h0] } end /-- A side of a right-angled triangle divided by the sine of the opposite angle equals the hypotenuse. -/ lemma norm_div_sin_angle_add_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x = 0 ∨ y ≠ 0) : ‖y‖ / real.sin (angle x (x + y)) = ‖x + y‖ := begin rcases h0 with h0 | h0, { simp [h0] }, rw [sin_angle_add_of_inner_eq_zero h (or.inr h0), div_div_eq_mul_div, mul_comm, div_eq_mul_inv, mul_inv_cancel_right₀ (norm_ne_zero_iff.2 h0)] end /-- A side of a right-angled triangle divided by the tangent of the opposite angle equals the adjacent side. -/ lemma norm_div_tan_angle_add_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x = 0 ∨ y ≠ 0) : ‖y‖ / real.tan (angle x (x + y)) = ‖x‖ := begin rw tan_angle_add_of_inner_eq_zero h, rcases h0 with h0 | h0, { simp [h0] }, { rw [div_div_eq_mul_div, mul_comm, div_eq_mul_inv, mul_inv_cancel_right₀ (norm_ne_zero_iff.2 h0)] } end /-- An angle in a right-angled triangle expressed using `arccos`, version subtracting vectors. -/ lemma angle_sub_eq_arccos_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : angle x (x - y) = real.arccos (‖x‖ / ‖x - y‖) := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw [sub_eq_add_neg, angle_add_eq_arccos_of_inner_eq_zero h] end /-- An angle in a right-angled triangle expressed using `arcsin`, version subtracting vectors. -/ lemma angle_sub_eq_arcsin_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0 ∨ y ≠ 0) : angle x (x - y) = real.arcsin (‖y‖ / ‖x - y‖) := begin rw [←neg_eq_zero, ←inner_neg_right] at h, nth_rewrite 1 ←neg_ne_zero at h0, rw [sub_eq_add_neg, angle_add_eq_arcsin_of_inner_eq_zero h h0, norm_neg] end /-- An angle in a right-angled triangle expressed using `arctan`, version subtracting vectors. -/ lemma angle_sub_eq_arctan_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0) : angle x (x - y) = real.arctan (‖y‖ / ‖x‖) := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw [sub_eq_add_neg, angle_add_eq_arctan_of_inner_eq_zero h h0, norm_neg] end /-- An angle in a non-degenerate right-angled triangle is positive, version subtracting vectors. -/ lemma angle_sub_pos_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x = 0 ∨ y ≠ 0) : 0 < angle x (x - y) := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw ←neg_ne_zero at h0, rw sub_eq_add_neg, exact angle_add_pos_of_inner_eq_zero h h0 end /-- An angle in a right-angled triangle is at most `π / 2`, version subtracting vectors. -/ lemma angle_sub_le_pi_div_two_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : angle x (x - y) ≤ π / 2 := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw sub_eq_add_neg, exact angle_add_le_pi_div_two_of_inner_eq_zero h end /-- An angle in a non-degenerate right-angled triangle is less than `π / 2`, version subtracting vectors. -/ lemma angle_sub_lt_pi_div_two_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0) : angle x (x - y) < π / 2 := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw sub_eq_add_neg, exact angle_add_lt_pi_div_two_of_inner_eq_zero h h0 end /-- The cosine of an angle in a right-angled triangle as a ratio of sides, version subtracting vectors. -/ lemma cos_angle_sub_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : real.cos (angle x (x - y)) = ‖x‖ / ‖x - y‖ := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw [sub_eq_add_neg, cos_angle_add_of_inner_eq_zero h] end /-- The sine of an angle in a right-angled triangle as a ratio of sides, version subtracting vectors. -/ lemma sin_angle_sub_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0 ∨ y ≠ 0) : real.sin (angle x (x - y)) = ‖y‖ / ‖x - y‖ := begin rw [←neg_eq_zero, ←inner_neg_right] at h, nth_rewrite 1 ←neg_ne_zero at h0, rw [sub_eq_add_neg, sin_angle_add_of_inner_eq_zero h h0, norm_neg] end /-- The tangent of an angle in a right-angled triangle as a ratio of sides, version subtracting vectors. -/ lemma tan_angle_sub_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : real.tan (angle x (x - y)) = ‖y‖ / ‖x‖ := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw [sub_eq_add_neg, tan_angle_add_of_inner_eq_zero h, norm_neg] end /-- The cosine of an angle in a right-angled triangle multiplied by the hypotenuse equals the adjacent side, version subtracting vectors. -/ lemma cos_angle_sub_mul_norm_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : real.cos (angle x (x - y)) * ‖x - y‖ = ‖x‖ := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw [sub_eq_add_neg, cos_angle_add_mul_norm_of_inner_eq_zero h] end /-- The sine of an angle in a right-angled triangle multiplied by the hypotenuse equals the opposite side, version subtracting vectors. -/ lemma sin_angle_sub_mul_norm_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) : real.sin (angle x (x - y)) * ‖x - y‖ = ‖y‖ := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw [sub_eq_add_neg, sin_angle_add_mul_norm_of_inner_eq_zero h, norm_neg] end /-- The tangent of an angle in a right-angled triangle multiplied by the adjacent side equals the opposite side, version subtracting vectors. -/ lemma tan_angle_sub_mul_norm_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0 ∨ y = 0) : real.tan (angle x (x - y)) * ‖x‖ = ‖y‖ := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw ←neg_eq_zero at h0, rw [sub_eq_add_neg, tan_angle_add_mul_norm_of_inner_eq_zero h h0, norm_neg] end /-- A side of a right-angled triangle divided by the cosine of the adjacent angle equals the hypotenuse, version subtracting vectors. -/ lemma norm_div_cos_angle_sub_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x ≠ 0 ∨ y = 0) : ‖x‖ / real.cos (angle x (x - y)) = ‖x - y‖ := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw ←neg_eq_zero at h0, rw [sub_eq_add_neg, norm_div_cos_angle_add_of_inner_eq_zero h h0] end /-- A side of a right-angled triangle divided by the sine of the opposite angle equals the hypotenuse, version subtracting vectors. -/ lemma norm_div_sin_angle_sub_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x = 0 ∨ y ≠ 0) : ‖y‖ / real.sin (angle x (x - y)) = ‖x - y‖ := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw ←neg_ne_zero at h0, rw [sub_eq_add_neg, ←norm_neg, norm_div_sin_angle_add_of_inner_eq_zero h h0] end /-- A side of a right-angled triangle divided by the tangent of the opposite angle equals the adjacent side, version subtracting vectors. -/ lemma norm_div_tan_angle_sub_of_inner_eq_zero {x y : V} (h : ⟪x, y⟫ = 0) (h0 : x = 0 ∨ y ≠ 0) : ‖y‖ / real.tan (angle x (x - y)) = ‖x‖ := begin rw [←neg_eq_zero, ←inner_neg_right] at h, rw ←neg_ne_zero at h0, rw [sub_eq_add_neg, ←norm_neg, norm_div_tan_angle_add_of_inner_eq_zero h h0] end end inner_product_geometry namespace euclidean_geometry open inner_product_geometry variables {V : Type*} {P : Type*} [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P] include V /-- **Pythagorean theorem**, if-and-only-if angle-at-point form. -/ lemma dist_sq_eq_dist_sq_add_dist_sq_iff_angle_eq_pi_div_two (p1 p2 p3 : P) : dist p1 p3 * dist p1 p3 = dist p1 p2 * dist p1 p2 + dist p3 p2 * dist p3 p2 ↔ ∠ p1 p2 p3 = π / 2 := by erw [dist_comm p3 p2, dist_eq_norm_vsub V p1 p3, dist_eq_norm_vsub V p1 p2, dist_eq_norm_vsub V p2 p3, ←norm_sub_sq_eq_norm_sq_add_norm_sq_iff_angle_eq_pi_div_two, vsub_sub_vsub_cancel_right p1, ←neg_vsub_eq_vsub_rev p2 p3, norm_neg] /-- An angle in a right-angled triangle expressed using `arccos`. -/ lemma angle_eq_arccos_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) : ∠ p₂ p₃ p₁ = real.arccos (dist p₃ p₂ / dist p₁ p₃) := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [angle, dist_eq_norm_vsub' V p₃ p₂, dist_eq_norm_vsub V p₁ p₃, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, angle_add_eq_arccos_of_inner_eq_zero h] end /-- An angle in a right-angled triangle expressed using `arcsin`. -/ lemma angle_eq_arcsin_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) (h0 : p₁ ≠ p₂ ∨ p₃ ≠ p₂) : ∠ p₂ p₃ p₁ = real.arcsin (dist p₁ p₂ / dist p₁ p₃) := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [←@vsub_ne_zero V, @ne_comm _ p₃, ←@vsub_ne_zero V _ _ _ p₂, or_comm] at h0, rw [angle, dist_eq_norm_vsub V p₁ p₂, dist_eq_norm_vsub V p₁ p₃, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, angle_add_eq_arcsin_of_inner_eq_zero h h0] end /-- An angle in a right-angled triangle expressed using `arctan`. -/ lemma angle_eq_arctan_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) (h0 : p₃ ≠ p₂) : ∠ p₂ p₃ p₁ = real.arctan (dist p₁ p₂ / dist p₃ p₂) := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [ne_comm, ←@vsub_ne_zero V] at h0, rw [angle, dist_eq_norm_vsub V p₁ p₂, dist_eq_norm_vsub' V p₃ p₂, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, angle_add_eq_arctan_of_inner_eq_zero h h0] end /-- An angle in a non-degenerate right-angled triangle is positive. -/ lemma angle_pos_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) (h0 : p₁ ≠ p₂ ∨ p₃ = p₂) : 0 < ∠ p₂ p₃ p₁ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [←@vsub_ne_zero V, eq_comm, ←@vsub_eq_zero_iff_eq V, or_comm] at h0, rw [angle, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm], exact angle_add_pos_of_inner_eq_zero h h0 end /-- An angle in a right-angled triangle is at most `π / 2`. -/ lemma angle_le_pi_div_two_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) : ∠ p₂ p₃ p₁ ≤ π / 2 := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [angle, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm], exact angle_add_le_pi_div_two_of_inner_eq_zero h end /-- An angle in a non-degenerate right-angled triangle is less than `π / 2`. -/ lemma angle_lt_pi_div_two_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) (h0 : p₃ ≠ p₂) : ∠ p₂ p₃ p₁ < π / 2 := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [ne_comm, ←@vsub_ne_zero V] at h0, rw [angle, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm], exact angle_add_lt_pi_div_two_of_inner_eq_zero h h0 end /-- The cosine of an angle in a right-angled triangle as a ratio of sides. -/ lemma cos_angle_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) : real.cos (∠ p₂ p₃ p₁) = dist p₃ p₂ / dist p₁ p₃ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [angle, dist_eq_norm_vsub' V p₃ p₂, dist_eq_norm_vsub V p₁ p₃, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, cos_angle_add_of_inner_eq_zero h] end /-- The sine of an angle in a right-angled triangle as a ratio of sides. -/ lemma sin_angle_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) (h0 : p₁ ≠ p₂ ∨ p₃ ≠ p₂) : real.sin (∠ p₂ p₃ p₁) = dist p₁ p₂ / dist p₁ p₃ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [←@vsub_ne_zero V, @ne_comm _ p₃, ←@vsub_ne_zero V _ _ _ p₂, or_comm] at h0, rw [angle, dist_eq_norm_vsub V p₁ p₂, dist_eq_norm_vsub V p₁ p₃, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, sin_angle_add_of_inner_eq_zero h h0] end /-- The tangent of an angle in a right-angled triangle as a ratio of sides. -/ lemma tan_angle_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) : real.tan (∠ p₂ p₃ p₁) = dist p₁ p₂ / dist p₃ p₂ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [angle, dist_eq_norm_vsub V p₁ p₂, dist_eq_norm_vsub' V p₃ p₂, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, tan_angle_add_of_inner_eq_zero h] end /-- The cosine of an angle in a right-angled triangle multiplied by the hypotenuse equals the adjacent side. -/ lemma cos_angle_mul_norm_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) : real.cos (∠ p₂ p₃ p₁) * dist p₁ p₃ = dist p₃ p₂ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [angle, dist_eq_norm_vsub' V p₃ p₂, dist_eq_norm_vsub V p₁ p₃, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, cos_angle_add_mul_norm_of_inner_eq_zero h] end /-- The sine of an angle in a right-angled triangle multiplied by the hypotenuse equals the opposite side. -/ lemma sin_angle_mul_norm_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) : real.sin (∠ p₂ p₃ p₁) * dist p₁ p₃ = dist p₁ p₂ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [angle, dist_eq_norm_vsub V p₁ p₂, dist_eq_norm_vsub V p₁ p₃, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, sin_angle_add_mul_norm_of_inner_eq_zero h] end /-- The tangent of an angle in a right-angled triangle multiplied by the adjacent side equals the opposite side. -/ lemma tan_angle_mul_norm_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) (h0 : p₁ = p₂ ∨ p₃ ≠ p₂) : real.tan (∠ p₂ p₃ p₁) * dist p₃ p₂ = dist p₁ p₂ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [ne_comm, ←@vsub_ne_zero V, ←@vsub_eq_zero_iff_eq V, or_comm] at h0, rw [angle, dist_eq_norm_vsub V p₁ p₂, dist_eq_norm_vsub' V p₃ p₂, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, tan_angle_add_mul_norm_of_inner_eq_zero h h0] end /-- A side of a right-angled triangle divided by the cosine of the adjacent angle equals the hypotenuse. -/ lemma norm_div_cos_angle_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) (h0 : p₁ = p₂ ∨ p₃ ≠ p₂) : dist p₃ p₂ / real.cos (∠ p₂ p₃ p₁) = dist p₁ p₃ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [ne_comm, ←@vsub_ne_zero V, ←@vsub_eq_zero_iff_eq V, or_comm] at h0, rw [angle, dist_eq_norm_vsub' V p₃ p₂, dist_eq_norm_vsub V p₁ p₃, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, norm_div_cos_angle_add_of_inner_eq_zero h h0] end /-- A side of a right-angled triangle divided by the sine of the opposite angle equals the hypotenuse. -/ lemma norm_div_sin_angle_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) (h0 : p₁ ≠ p₂ ∨ p₃ = p₂) : dist p₁ p₂ / real.sin (∠ p₂ p₃ p₁) = dist p₁ p₃ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [eq_comm, ←@vsub_ne_zero V, ←@vsub_eq_zero_iff_eq V, or_comm] at h0, rw [angle, dist_eq_norm_vsub V p₁ p₂, dist_eq_norm_vsub V p₁ p₃, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, norm_div_sin_angle_add_of_inner_eq_zero h h0] end /-- A side of a right-angled triangle divided by the tangent of the opposite angle equals the adjacent side. -/ lemma norm_div_tan_angle_of_angle_eq_pi_div_two {p₁ p₂ p₃ : P} (h : ∠ p₁ p₂ p₃ = π / 2) (h0 : p₁ ≠ p₂ ∨ p₃ = p₂) : dist p₁ p₂ / real.tan (∠ p₂ p₃ p₁) = dist p₃ p₂ := begin rw [angle, ←inner_eq_zero_iff_angle_eq_pi_div_two, real_inner_comm, ←neg_eq_zero, ←inner_neg_left, neg_vsub_eq_vsub_rev] at h, rw [eq_comm, ←@vsub_ne_zero V, ←@vsub_eq_zero_iff_eq V, or_comm] at h0, rw [angle, dist_eq_norm_vsub V p₁ p₂, dist_eq_norm_vsub' V p₃ p₂, ←vsub_add_vsub_cancel p₁ p₂ p₃, add_comm, norm_div_tan_angle_add_of_inner_eq_zero h h0] end end euclidean_geometry
58853e6adaa58e1d20186c9d9958cf7ffacea385
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/calculus/tangent_cone.lean
cfdc15a72bf7ef0f6a2b51bb70342a43a57bed66
[ "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
20,174
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.convex.topology import analysis.normed_space.basic import analysis.specific_limits.basic /-! # Tangent cone In this file, we define two predicates `unique_diff_within_at 𝕜 s x` and `unique_diff_on 𝕜 s` ensuring that, if a function has two derivatives, then they have to coincide. As a direct definition of this fact (quantifying on all target types and all functions) would depend on universes, we use a more intrinsic definition: if all the possible tangent directions to the set `s` at the point `x` span a dense subset of the whole subset, it is easy to check that the derivative has to be unique. Therefore, we introduce the set of all tangent directions, named `tangent_cone_at`, and express `unique_diff_within_at` and `unique_diff_on` in terms of it. One should however think of this definition as an implementation detail: the only reason to introduce the predicates `unique_diff_within_at` and `unique_diff_on` is to ensure the uniqueness of the derivative. This is why their names reflect their uses, and not how they are defined. ## Implementation details Note that this file is imported by `fderiv.lean`. Hence, derivatives are not defined yet. The property of uniqueness of the derivative is therefore proved in `fderiv.lean`, but based on the properties of the tangent cone we prove here. -/ variables (𝕜 : Type*) [nontrivially_normed_field 𝕜] open filter set open_locale topological_space section tangent_cone variables {E : Type*} [add_comm_monoid E] [module 𝕜 E] [topological_space E] /-- The set of all tangent directions to the set `s` at the point `x`. -/ def tangent_cone_at (s : set E) (x : E) : set E := {y : E | ∃(c : ℕ → 𝕜) (d : ℕ → E), (∀ᶠ n in at_top, x + d n ∈ s) ∧ (tendsto (λn, ∥c n∥) at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))} /-- A property ensuring that the tangent cone to `s` at `x` spans a dense subset of the whole space. The main role of this property is to ensure that the differential within `s` at `x` is unique, hence this name. The uniqueness it asserts is proved in `unique_diff_within_at.eq` in `fderiv.lean`. To avoid pathologies in dimension 0, we also require that `x` belongs to the closure of `s` (which is automatic when `E` is not `0`-dimensional). -/ @[mk_iff] structure unique_diff_within_at (s : set E) (x : E) : Prop := (dense_tangent_cone : dense ((submodule.span 𝕜 (tangent_cone_at 𝕜 s x)) : set E)) (mem_closure : x ∈ closure s) /-- A property ensuring that the tangent cone to `s` at any of its points spans a dense subset of the whole space. The main role of this property is to ensure that the differential along `s` is unique, hence this name. The uniqueness it asserts is proved in `unique_diff_on.eq` in `fderiv.lean`. -/ def unique_diff_on (s : set E) : Prop := ∀x ∈ s, unique_diff_within_at 𝕜 s x end tangent_cone variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] variables {G : Type*} [normed_add_comm_group G] [normed_space ℝ G] variables {𝕜} {x y : E} {s t : set E} section tangent_cone /- This section is devoted to the properties of the tangent cone. -/ open normed_field lemma tangent_cone_univ : tangent_cone_at 𝕜 univ x = univ := begin refine univ_subset_iff.1 (λy hy, _), rcases exists_one_lt_norm 𝕜 with ⟨w, hw⟩, refine ⟨λn, w^n, λn, (w^n)⁻¹ • y, univ_mem' (λn, mem_univ _), _, _⟩, { simp only [norm_pow], exact tendsto_pow_at_top_at_top_of_one_lt hw }, { convert tendsto_const_nhds, ext n, have : w ^ n * (w ^ n)⁻¹ = 1, { apply mul_inv_cancel, apply pow_ne_zero, simpa [norm_eq_zero] using (ne_of_lt (lt_trans zero_lt_one hw)).symm }, rw [smul_smul, this, one_smul] } end lemma tangent_cone_mono (h : s ⊆ t) : tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x := begin rintros y ⟨c, d, ds, ctop, clim⟩, exact ⟨c, d, mem_of_superset ds (λn hn, h hn), ctop, clim⟩ end /-- Auxiliary lemma ensuring that, under the assumptions defining the tangent cone, the sequence `d` tends to 0 at infinity. -/ lemma tangent_cone_at.lim_zero {α : Type*} (l : filter α) {c : α → 𝕜} {d : α → E} (hc : tendsto (λn, ∥c n∥) l at_top) (hd : tendsto (λn, c n • d n) l (𝓝 y)) : tendsto d l (𝓝 0) := begin have A : tendsto (λn, ∥c n∥⁻¹) l (𝓝 0) := tendsto_inv_at_top_zero.comp hc, have B : tendsto (λn, ∥c n • d n∥) l (𝓝 ∥y∥) := (continuous_norm.tendsto _).comp hd, have C : tendsto (λn, ∥c n∥⁻¹ * ∥c n • d n∥) l (𝓝 (0 * ∥y∥)) := A.mul B, rw zero_mul at C, have : ∀ᶠ n in l, ∥c n∥⁻¹ * ∥c n • d n∥ = ∥d n∥, { apply (eventually_ne_of_tendsto_norm_at_top hc 0).mono (λn hn, _), rw [norm_smul, ← mul_assoc, inv_mul_cancel, one_mul], rwa [ne.def, norm_eq_zero] }, have D : tendsto (λ n, ∥d n∥) l (𝓝 0) := tendsto.congr' this C, rw tendsto_zero_iff_norm_tendsto_zero, exact D end lemma tangent_cone_mono_nhds (h : 𝓝[s] x ≤ 𝓝[t] x) : tangent_cone_at 𝕜 s x ⊆ tangent_cone_at 𝕜 t x := begin rintros y ⟨c, d, ds, ctop, clim⟩, refine ⟨c, d, _, ctop, clim⟩, suffices : tendsto (λ n, x + d n) at_top (𝓝[t] x), from tendsto_principal.1 (tendsto_inf.1 this).2, refine (tendsto_inf.2 ⟨_, tendsto_principal.2 ds⟩).mono_right h, simpa only [add_zero] using tendsto_const_nhds.add (tangent_cone_at.lim_zero at_top ctop clim) end /-- Tangent cone of `s` at `x` depends only on `𝓝[s] x`. -/ lemma tangent_cone_congr (h : 𝓝[s] x = 𝓝[t] x) : tangent_cone_at 𝕜 s x = tangent_cone_at 𝕜 t x := subset.antisymm (tangent_cone_mono_nhds $ le_of_eq h) (tangent_cone_mono_nhds $ le_of_eq h.symm) /-- Intersecting with a neighborhood of the point does not change the tangent cone. -/ lemma tangent_cone_inter_nhds (ht : t ∈ 𝓝 x) : tangent_cone_at 𝕜 (s ∩ t) x = tangent_cone_at 𝕜 s x := tangent_cone_congr (nhds_within_restrict' _ ht).symm /-- The tangent cone of a product contains the tangent cone of its left factor. -/ lemma subset_tangent_cone_prod_left {t : set F} {y : F} (ht : y ∈ closure t) : linear_map.inl 𝕜 E F '' (tangent_cone_at 𝕜 s x) ⊆ tangent_cone_at 𝕜 (s ×ˢ t) (x, y) := begin rintros _ ⟨v, ⟨c, d, hd, hc, hy⟩, rfl⟩, have : ∀n, ∃d', y + d' ∈ t ∧ ∥c n • d'∥ < ((1:ℝ)/2)^n, { assume n, rcases mem_closure_iff_nhds.1 ht _ (eventually_nhds_norm_smul_sub_lt (c n) y (pow_pos one_half_pos n)) with ⟨z, hz, hzt⟩, exact ⟨z - y, by simpa using hzt, by simpa using hz⟩ }, choose d' hd' using this, refine ⟨c, λn, (d n, d' n), _, hc, _⟩, show ∀ᶠ n in at_top, (x, y) + (d n, d' n) ∈ s ×ˢ t, { filter_upwards [hd] with n hn, simp [hn, (hd' n).1] }, { apply tendsto.prod_mk_nhds hy _, refine squeeze_zero_norm (λn, (hd' n).2.le) _, exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one } end /-- The tangent cone of a product contains the tangent cone of its right factor. -/ lemma subset_tangent_cone_prod_right {t : set F} {y : F} (hs : x ∈ closure s) : linear_map.inr 𝕜 E F '' (tangent_cone_at 𝕜 t y) ⊆ tangent_cone_at 𝕜 (s ×ˢ t) (x, y) := begin rintros _ ⟨w, ⟨c, d, hd, hc, hy⟩, rfl⟩, have : ∀n, ∃d', x + d' ∈ s ∧ ∥c n • d'∥ < ((1:ℝ)/2)^n, { assume n, rcases mem_closure_iff_nhds.1 hs _ (eventually_nhds_norm_smul_sub_lt (c n) x (pow_pos one_half_pos n)) with ⟨z, hz, hzs⟩, exact ⟨z - x, by simpa using hzs, by simpa using hz⟩ }, choose d' hd' using this, refine ⟨c, λn, (d' n, d n), _, hc, _⟩, show ∀ᶠ n in at_top, (x, y) + (d' n, d n) ∈ s ×ˢ t, { filter_upwards [hd] with n hn, simp [hn, (hd' n).1] }, { apply tendsto.prod_mk_nhds _ hy, refine squeeze_zero_norm (λn, (hd' n).2.le) _, exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one } end /-- The tangent cone of a product contains the tangent cone of each factor. -/ lemma maps_to_tangent_cone_pi {ι : Type*} [decidable_eq ι] {E : ι → Type*} [Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)] {s : Π i, set (E i)} {x : Π i, E i} {i : ι} (hi : ∀ j ≠ i, x j ∈ closure (s j)) : maps_to (linear_map.single i : E i →ₗ[𝕜] Π j, E j) (tangent_cone_at 𝕜 (s i) (x i)) (tangent_cone_at 𝕜 (set.pi univ s) x) := begin rintros w ⟨c, d, hd, hc, hy⟩, have : ∀ n (j ≠ i), ∃ d', x j + d' ∈ s j ∧ ∥c n • d'∥ < (1 / 2 : ℝ) ^ n, { assume n j hj, rcases mem_closure_iff_nhds.1 (hi j hj) _ (eventually_nhds_norm_smul_sub_lt (c n) (x j) (pow_pos one_half_pos n)) with ⟨z, hz, hzs⟩, exact ⟨z - x j, by simpa using hzs, by simpa using hz⟩ }, choose! d' hd's hcd', refine ⟨c, λ n, function.update (d' n) i (d n), hd.mono (λ n hn j hj', _), hc, tendsto_pi_nhds.2 $ λ j, _⟩, { rcases em (j = i) with rfl|hj; simp * }, { rcases em (j = i) with rfl|hj, { simp [hy] }, { suffices : tendsto (λ n, c n • d' n j) at_top (𝓝 0), by simpa [hj], refine squeeze_zero_norm (λ n, (hcd' n j hj).le) _, exact tendsto_pow_at_top_nhds_0_of_lt_1 one_half_pos.le one_half_lt_one } } end /-- If a subset of a real vector space contains an open segment, then the direction of this segment belongs to the tangent cone at its endpoints. -/ lemma mem_tangent_cone_of_open_segment_subset {s : set G} {x y : G} (h : open_segment ℝ x y ⊆ s) : y - x ∈ tangent_cone_at ℝ s x := begin let c := λn:ℕ, (2:ℝ)^(n+1), let d := λn:ℕ, (c n)⁻¹ • (y-x), refine ⟨c, d, filter.univ_mem' (λn, h _), _, _⟩, show x + d n ∈ open_segment ℝ x y, { rw open_segment_eq_image, refine ⟨(c n)⁻¹, ⟨_, _⟩, _⟩, { rw inv_pos, apply pow_pos, norm_num }, { apply inv_lt_one, apply one_lt_pow _ (nat.succ_ne_zero _), norm_num }, { simp only [d, sub_smul, smul_sub, one_smul], abel } }, show filter.tendsto (λ (n : ℕ), ∥c n∥) filter.at_top filter.at_top, { have : (λ (n : ℕ), ∥c n∥) = c, by { ext n, exact abs_of_nonneg (pow_nonneg (by norm_num) _) }, rw this, exact (tendsto_pow_at_top_at_top_of_one_lt (by norm_num)).comp (tendsto_add_at_top_nat 1) }, show filter.tendsto (λ (n : ℕ), c n • d n) filter.at_top (𝓝 (y - x)), { have : (λ (n : ℕ), c n • d n) = (λn, y - x), { ext n, simp only [d, smul_smul], rw [mul_inv_cancel, one_smul], exact pow_ne_zero _ (by norm_num) }, rw this, apply tendsto_const_nhds } end /-- If a subset of a real vector space contains a segment, then the direction of this segment belongs to the tangent cone at its endpoints. -/ lemma mem_tangent_cone_of_segment_subset {s : set G} {x y : G} (h : segment ℝ x y ⊆ s) : y - x ∈ tangent_cone_at ℝ s x := mem_tangent_cone_of_open_segment_subset ((open_segment_subset_segment ℝ x y).trans h) end tangent_cone section unique_diff /-! ### Properties of `unique_diff_within_at` and `unique_diff_on` This section is devoted to properties of the predicates `unique_diff_within_at` and `unique_diff_on`. -/ lemma unique_diff_on.unique_diff_within_at {s : set E} {x} (hs : unique_diff_on 𝕜 s) (h : x ∈ s) : unique_diff_within_at 𝕜 s x := hs x h lemma unique_diff_within_at_univ : unique_diff_within_at 𝕜 univ x := by { rw [unique_diff_within_at_iff, tangent_cone_univ], simp } lemma unique_diff_on_univ : unique_diff_on 𝕜 (univ : set E) := λx hx, unique_diff_within_at_univ lemma unique_diff_on_empty : unique_diff_on 𝕜 (∅ : set E) := λ x hx, hx.elim lemma unique_diff_within_at.mono_nhds (h : unique_diff_within_at 𝕜 s x) (st : 𝓝[s] x ≤ 𝓝[t] x) : unique_diff_within_at 𝕜 t x := begin simp only [unique_diff_within_at_iff] at *, rw [mem_closure_iff_nhds_within_ne_bot] at h ⊢, exact ⟨h.1.mono $ submodule.span_mono $ tangent_cone_mono_nhds st, h.2.mono st⟩ end lemma unique_diff_within_at.mono (h : unique_diff_within_at 𝕜 s x) (st : s ⊆ t) : unique_diff_within_at 𝕜 t x := h.mono_nhds $ nhds_within_mono _ st lemma unique_diff_within_at_congr (st : 𝓝[s] x = 𝓝[t] x) : unique_diff_within_at 𝕜 s x ↔ unique_diff_within_at 𝕜 t x := ⟨λ h, h.mono_nhds $ le_of_eq st, λ h, h.mono_nhds $ le_of_eq st.symm⟩ lemma unique_diff_within_at_inter (ht : t ∈ 𝓝 x) : unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x := unique_diff_within_at_congr $ (nhds_within_restrict' _ ht).symm lemma unique_diff_within_at.inter (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝 x) : unique_diff_within_at 𝕜 (s ∩ t) x := (unique_diff_within_at_inter ht).2 hs lemma unique_diff_within_at_inter' (ht : t ∈ 𝓝[s] x) : unique_diff_within_at 𝕜 (s ∩ t) x ↔ unique_diff_within_at 𝕜 s x := unique_diff_within_at_congr $ (nhds_within_restrict'' _ ht).symm lemma unique_diff_within_at.inter' (hs : unique_diff_within_at 𝕜 s x) (ht : t ∈ 𝓝[s] x) : unique_diff_within_at 𝕜 (s ∩ t) x := (unique_diff_within_at_inter' ht).2 hs lemma unique_diff_within_at_of_mem_nhds (h : s ∈ 𝓝 x) : unique_diff_within_at 𝕜 s x := by simpa only [univ_inter] using unique_diff_within_at_univ.inter h lemma is_open.unique_diff_within_at (hs : is_open s) (xs : x ∈ s) : unique_diff_within_at 𝕜 s x := unique_diff_within_at_of_mem_nhds (is_open.mem_nhds hs xs) lemma unique_diff_on.inter (hs : unique_diff_on 𝕜 s) (ht : is_open t) : unique_diff_on 𝕜 (s ∩ t) := λx hx, (hs x hx.1).inter (is_open.mem_nhds ht hx.2) lemma is_open.unique_diff_on (hs : is_open s) : unique_diff_on 𝕜 s := λx hx, is_open.unique_diff_within_at hs hx /-- The product of two sets of unique differentiability at points `x` and `y` has unique differentiability at `(x, y)`. -/ lemma unique_diff_within_at.prod {t : set F} {y : F} (hs : unique_diff_within_at 𝕜 s x) (ht : unique_diff_within_at 𝕜 t y) : unique_diff_within_at 𝕜 (s ×ˢ t) (x, y) := begin rw [unique_diff_within_at_iff] at ⊢ hs ht, rw [closure_prod_eq], refine ⟨_, hs.2, ht.2⟩, have : _ ≤ submodule.span 𝕜 (tangent_cone_at 𝕜 (s ×ˢ t) (x, y)) := submodule.span_mono (union_subset (subset_tangent_cone_prod_left ht.2) (subset_tangent_cone_prod_right hs.2)), rw [linear_map.span_inl_union_inr, set_like.le_def] at this, exact (hs.1.prod ht.1).mono this end lemma unique_diff_within_at.univ_pi (ι : Type*) [finite ι] (E : ι → Type*) [Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)] (s : Π i, set (E i)) (x : Π i, E i) (h : ∀ i, unique_diff_within_at 𝕜 (s i) (x i)) : unique_diff_within_at 𝕜 (set.pi univ s) x := begin classical, simp only [unique_diff_within_at_iff, closure_pi_set] at h ⊢, refine ⟨(dense_pi univ (λ i _, (h i).1)).mono _, λ i _, (h i).2⟩, norm_cast, simp only [← submodule.supr_map_single, supr_le_iff, linear_map.map_span, submodule.span_le, ← maps_to'], exact λ i, (maps_to_tangent_cone_pi $ λ j hj, (h j).2).mono subset.rfl submodule.subset_span end lemma unique_diff_within_at.pi (ι : Type*) [finite ι] (E : ι → Type*) [Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)] (s : Π i, set (E i)) (x : Π i, E i) (I : set ι) (h : ∀ i ∈ I, unique_diff_within_at 𝕜 (s i) (x i)) : unique_diff_within_at 𝕜 (set.pi I s) x := begin classical, rw [← set.univ_pi_piecewise], refine unique_diff_within_at.univ_pi _ _ _ _ (λ i, _), by_cases hi : i ∈ I; simp [*, unique_diff_within_at_univ], end /-- The product of two sets of unique differentiability is a set of unique differentiability. -/ lemma unique_diff_on.prod {t : set F} (hs : unique_diff_on 𝕜 s) (ht : unique_diff_on 𝕜 t) : unique_diff_on 𝕜 (s ×ˢ t) := λ ⟨x, y⟩ h, unique_diff_within_at.prod (hs x h.1) (ht y h.2) /-- The finite product of a family of sets of unique differentiability is a set of unique differentiability. -/ lemma unique_diff_on.pi (ι : Type*) [finite ι] (E : ι → Type*) [Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)] (s : Π i, set (E i)) (I : set ι) (h : ∀ i ∈ I, unique_diff_on 𝕜 (s i)) : unique_diff_on 𝕜 (set.pi I s) := λ x hx, unique_diff_within_at.pi _ _ _ _ _ $ λ i hi, h i hi (x i) (hx i hi) /-- The finite product of a family of sets of unique differentiability is a set of unique differentiability. -/ lemma unique_diff_on.univ_pi (ι : Type*) [finite ι] (E : ι → Type*) [Π i, normed_add_comm_group (E i)] [Π i, normed_space 𝕜 (E i)] (s : Π i, set (E i)) (h : ∀ i, unique_diff_on 𝕜 (s i)) : unique_diff_on 𝕜 (set.pi univ s) := unique_diff_on.pi _ _ _ _ $ λ i _, h i /-- In a real vector space, a convex set with nonempty interior is a set of unique differentiability at every point of its closure. -/ theorem unique_diff_within_at_convex {s : set G} (conv : convex ℝ s) (hs : (interior s).nonempty) {x : G} (hx : x ∈ closure s) : unique_diff_within_at ℝ s x := begin rcases hs with ⟨y, hy⟩, suffices : y - x ∈ interior (tangent_cone_at ℝ s x), { refine ⟨dense.of_closure _, hx⟩, simp [(submodule.span ℝ (tangent_cone_at ℝ s x)).eq_top_of_nonempty_interior' ⟨y - x, interior_mono submodule.subset_span this⟩] }, rw [mem_interior_iff_mem_nhds], replace hy : interior s ∈ 𝓝 y := is_open.mem_nhds is_open_interior hy, apply mem_of_superset ((is_open_map_sub_right x).image_mem_nhds hy), rintros _ ⟨z, zs, rfl⟩, refine mem_tangent_cone_of_open_segment_subset (subset.trans _ interior_subset), exact conv.open_segment_closure_interior_subset_interior hx zs, end /-- In a real vector space, a convex set with nonempty interior is a set of unique differentiability. -/ theorem unique_diff_on_convex {s : set G} (conv : convex ℝ s) (hs : (interior s).nonempty) : unique_diff_on ℝ s := λ x xs, unique_diff_within_at_convex conv hs (subset_closure xs) lemma unique_diff_on_Ici (a : ℝ) : unique_diff_on ℝ (Ici a) := unique_diff_on_convex (convex_Ici a) $ by simp only [interior_Ici, nonempty_Ioi] lemma unique_diff_on_Iic (a : ℝ) : unique_diff_on ℝ (Iic a) := unique_diff_on_convex (convex_Iic a) $ by simp only [interior_Iic, nonempty_Iio] lemma unique_diff_on_Ioi (a : ℝ) : unique_diff_on ℝ (Ioi a) := is_open_Ioi.unique_diff_on lemma unique_diff_on_Iio (a : ℝ) : unique_diff_on ℝ (Iio a) := is_open_Iio.unique_diff_on lemma unique_diff_on_Icc {a b : ℝ} (hab : a < b) : unique_diff_on ℝ (Icc a b) := unique_diff_on_convex (convex_Icc a b) $ by simp only [interior_Icc, nonempty_Ioo, hab] lemma unique_diff_on_Ico (a b : ℝ) : unique_diff_on ℝ (Ico a b) := if hab : a < b then unique_diff_on_convex (convex_Ico a b) $ by simp only [interior_Ico, nonempty_Ioo, hab] else by simp only [Ico_eq_empty hab, unique_diff_on_empty] lemma unique_diff_on_Ioc (a b : ℝ) : unique_diff_on ℝ (Ioc a b) := if hab : a < b then unique_diff_on_convex (convex_Ioc a b) $ by simp only [interior_Ioc, nonempty_Ioo, hab] else by simp only [Ioc_eq_empty hab, unique_diff_on_empty] lemma unique_diff_on_Ioo (a b : ℝ) : unique_diff_on ℝ (Ioo a b) := is_open_Ioo.unique_diff_on /-- The real interval `[0, 1]` is a set of unique differentiability. -/ lemma unique_diff_on_Icc_zero_one : unique_diff_on ℝ (Icc (0:ℝ) 1) := unique_diff_on_Icc zero_lt_one lemma unique_diff_within_at_Ioo {a b t : ℝ} (ht : t ∈ set.Ioo a b) : unique_diff_within_at ℝ (set.Ioo a b) t := is_open.unique_diff_within_at is_open_Ioo ht lemma unique_diff_within_at_Ioi (a : ℝ) : unique_diff_within_at ℝ (Ioi a) a := unique_diff_within_at_convex (convex_Ioi a) (by simp) (by simp) lemma unique_diff_within_at_Iio (a : ℝ) : unique_diff_within_at ℝ (Iio a) a := unique_diff_within_at_convex (convex_Iio a) (by simp) (by simp) end unique_diff
f31ea06d6ff8c3eb4abfaf595915ecdc328e637a
63abd62053d479eae5abf4951554e1064a4c45b4
/src/order/bounds.lean
7ae8ac7438aef11233b1931a91a9d27fc23a814f
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
29,159
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import data.set.intervals.basic import algebra.ordered_group /-! # Upper / lower bounds In this file we define: * `upper_bounds`, `lower_bounds` : the set of upper bounds (resp., lower bounds) of a set; * `bdd_above s`, `bdd_below s` : the set `s` is bounded above (resp., below), i.e., the set of upper (resp., lower) bounds of `s` is nonempty; * `is_least s a`, `is_greatest s a` : `a` is a least (resp., greatest) element of `s`; for a partial order, it is unique if exists; * `is_lub s a`, `is_glb s a` : `a` is a least upper bound (resp., a greatest lower bound) of `s`; for a partial order, it is unique if exists. We also prove various lemmas about monotonicity, behaviour under `∪`, `∩`, `insert`, and provide formulas for `∅`, `univ`, and intervals. -/ open set universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section variables [preorder α] [preorder β] {s t : set α} {a b : α} /-! ### Definitions -/ /-- The set of upper bounds of a set. -/ def upper_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → a ≤ x } /-- The set of lower bounds of a set. -/ def lower_bounds (s : set α) : set α := { x | ∀ ⦃a⦄, a ∈ s → x ≤ a } /-- A set is bounded above if there exists an upper bound. -/ def bdd_above (s : set α) := (upper_bounds s).nonempty /-- A set is bounded below if there exists a lower bound. -/ def bdd_below (s : set α) := (lower_bounds s).nonempty /-- `a` is a least element of a set `s`; for a partial order, it is unique if exists. -/ def is_least (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ lower_bounds s /-- `a` is a greatest element of a set `s`; for a partial order, it is unique if exists -/ def is_greatest (s : set α) (a : α) : Prop := a ∈ s ∧ a ∈ upper_bounds s /-- `a` is a least upper bound of a set `s`; for a partial order, it is unique if exists. -/ def is_lub (s : set α) : α → Prop := is_least (upper_bounds s) /-- `a` is a greatest lower bound of a set `s`; for a partial order, it is unique if exists. -/ def is_glb (s : set α) : α → Prop := is_greatest (lower_bounds s) lemma mem_upper_bounds : a ∈ upper_bounds s ↔ ∀ x ∈ s, x ≤ a := iff.rfl lemma mem_lower_bounds : a ∈ lower_bounds s ↔ ∀ x ∈ s, a ≤ x := iff.rfl /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` such that `x` is not greater than or equal to `y`. This version only assumes `preorder` structure and uses `¬(y ≤ x)`. A version for linear orders is called `not_bdd_above_iff`. -/ lemma not_bdd_above_iff' : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, ¬(y ≤ x) := by simp [bdd_above, upper_bounds, set.nonempty] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` such that `x` is not less than or equal to `y`. This version only assumes `preorder` structure and uses `¬(x ≤ y)`. A version for linear orders is called `not_bdd_below_iff`. -/ lemma not_bdd_below_iff' : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, ¬(x ≤ y) := @not_bdd_above_iff' (order_dual α) _ _ /-- A set `s` is not bounded above if and only if for each `x` there exists `y ∈ s` that is greater than `x`. A version for preorders is called `not_bdd_above_iff'`. -/ lemma not_bdd_above_iff {α : Type*} [linear_order α] {s : set α} : ¬bdd_above s ↔ ∀ x, ∃ y ∈ s, x < y := by simp only [not_bdd_above_iff', not_le] /-- A set `s` is not bounded below if and only if for each `x` there exists `y ∈ s` that is less than `x`. A version for preorders is called `not_bdd_below_iff'`. -/ lemma not_bdd_below_iff {α : Type*} [linear_order α] {s : set α} : ¬bdd_below s ↔ ∀ x, ∃ y ∈ s, y < x := @not_bdd_above_iff (order_dual α) _ _ /-! ### Monotonicity -/ lemma upper_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) : upper_bounds t ⊆ upper_bounds s := λ b hb x h, hb $ hst h lemma lower_bounds_mono_set ⦃s t : set α⦄ (hst : s ⊆ t) : lower_bounds t ⊆ lower_bounds s := λ b hb x h, hb $ hst h lemma upper_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds s → b ∈ upper_bounds s := λ ha x h, le_trans (ha h) hab lemma lower_bounds_mono_mem ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds s → a ∈ lower_bounds s := λ hb x h, le_trans hab (hb h) lemma upper_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : a ∈ upper_bounds t → b ∈ upper_bounds s := λ ha, upper_bounds_mono_set hst $ upper_bounds_mono_mem hab ha lemma lower_bounds_mono ⦃s t : set α⦄ (hst : s ⊆ t) ⦃a b⦄ (hab : a ≤ b) : b ∈ lower_bounds t → a ∈ lower_bounds s := λ hb, lower_bounds_mono_set hst $ lower_bounds_mono_mem hab hb /-- If `s ⊆ t` and `t` is bounded above, then so is `s`. -/ lemma bdd_above.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_above t → bdd_above s := nonempty.mono $ upper_bounds_mono_set h /-- If `s ⊆ t` and `t` is bounded below, then so is `s`. -/ lemma bdd_below.mono ⦃s t : set α⦄ (h : s ⊆ t) : bdd_below t → bdd_below s := nonempty.mono $ lower_bounds_mono_set h /-- If `a` is a least upper bound for sets `s` and `p`, then it is a least upper bound for any set `t`, `s ⊆ t ⊆ p`. -/ lemma is_lub.of_subset_of_superset {s t p : set α} (hs : is_lub s a) (hp : is_lub p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_lub t a := ⟨upper_bounds_mono_set htp hp.1, lower_bounds_mono_set (upper_bounds_mono_set hst) hs.2⟩ /-- If `a` is a greatest lower bound for sets `s` and `p`, then it is a greater lower bound for any set `t`, `s ⊆ t ⊆ p`. -/ lemma is_glb.of_subset_of_superset {s t p : set α} (hs : is_glb s a) (hp : is_glb p a) (hst : s ⊆ t) (htp : t ⊆ p) : is_glb t a := @is_lub.of_subset_of_superset (order_dual α) _ a s t p hs hp hst htp lemma is_least.mono (ha : is_least s a) (hb : is_least t b) (hst : s ⊆ t) : b ≤ a := hb.2 (hst ha.1) lemma is_greatest.mono (ha : is_greatest s a) (hb : is_greatest t b) (hst : s ⊆ t) : a ≤ b := hb.2 (hst ha.1) lemma is_lub.mono (ha : is_lub s a) (hb : is_lub t b) (hst : s ⊆ t) : a ≤ b := hb.mono ha $ upper_bounds_mono_set hst lemma is_glb.mono (ha : is_glb s a) (hb : is_glb t b) (hst : s ⊆ t) : b ≤ a := hb.mono ha $ lower_bounds_mono_set hst /-! ### Conversions -/ lemma is_least.is_glb (h : is_least s a) : is_glb s a := ⟨h.2, λ b hb, hb h.1⟩ lemma is_greatest.is_lub (h : is_greatest s a) : is_lub s a := ⟨h.2, λ b hb, hb h.1⟩ lemma is_lub.upper_bounds_eq (h : is_lub s a) : upper_bounds s = Ici a := set.ext $ λ b, ⟨λ hb, h.2 hb, λ hb, upper_bounds_mono_mem hb h.1⟩ lemma is_glb.lower_bounds_eq (h : is_glb s a) : lower_bounds s = Iic a := @is_lub.upper_bounds_eq (order_dual α) _ _ _ h lemma is_least.lower_bounds_eq (h : is_least s a) : lower_bounds s = Iic a := h.is_glb.lower_bounds_eq lemma is_greatest.upper_bounds_eq (h : is_greatest s a) : upper_bounds s = Ici a := h.is_lub.upper_bounds_eq lemma is_lub_le_iff (h : is_lub s a) : a ≤ b ↔ b ∈ upper_bounds s := by { rw h.upper_bounds_eq, refl } lemma le_is_glb_iff (h : is_glb s a) : b ≤ a ↔ b ∈ lower_bounds s := by { rw h.lower_bounds_eq, refl } /-- If `s` has a least upper bound, then it is bounded above. -/ lemma is_lub.bdd_above (h : is_lub s a) : bdd_above s := ⟨a, h.1⟩ /-- If `s` has a greatest lower bound, then it is bounded below. -/ lemma is_glb.bdd_below (h : is_glb s a) : bdd_below s := ⟨a, h.1⟩ /-- If `s` has a greatest element, then it is bounded above. -/ lemma is_greatest.bdd_above (h : is_greatest s a) : bdd_above s := ⟨a, h.2⟩ /-- If `s` has a least element, then it is bounded below. -/ lemma is_least.bdd_below (h : is_least s a) : bdd_below s := ⟨a, h.2⟩ lemma is_least.nonempty (h : is_least s a) : s.nonempty := ⟨a, h.1⟩ lemma is_greatest.nonempty (h : is_greatest s a) : s.nonempty := ⟨a, h.1⟩ /-! ### Union and intersection -/ @[simp] lemma upper_bounds_union : upper_bounds (s ∪ t) = upper_bounds s ∩ upper_bounds t := subset.antisymm (λ b hb, ⟨λ x hx, hb (or.inl hx), λ x hx, hb (or.inr hx)⟩) (λ b hb x hx, hx.elim (λ hs, hb.1 hs) (λ ht, hb.2 ht)) @[simp] lemma lower_bounds_union : lower_bounds (s ∪ t) = lower_bounds s ∩ lower_bounds t := @upper_bounds_union (order_dual α) _ s t lemma union_upper_bounds_subset_upper_bounds_inter : upper_bounds s ∪ upper_bounds t ⊆ upper_bounds (s ∩ t) := union_subset (upper_bounds_mono_set $ inter_subset_left _ _) (upper_bounds_mono_set $ inter_subset_right _ _) lemma union_lower_bounds_subset_lower_bounds_inter : lower_bounds s ∪ lower_bounds t ⊆ lower_bounds (s ∩ t) := @union_upper_bounds_subset_upper_bounds_inter (order_dual α) _ s t lemma is_least_union_iff {a : α} {s t : set α} : is_least (s ∪ t) a ↔ (is_least s a ∧ a ∈ lower_bounds t ∨ a ∈ lower_bounds s ∧ is_least t a) := by simp [is_least, lower_bounds_union, or_and_distrib_right, and_comm (a ∈ t), and_assoc] lemma is_greatest_union_iff : is_greatest (s ∪ t) a ↔ (is_greatest s a ∧ a ∈ upper_bounds t ∨ a ∈ upper_bounds s ∧ is_greatest t a) := @is_least_union_iff (order_dual α) _ a s t /-- If `s` is bounded, then so is `s ∩ t` -/ lemma bdd_above.inter_of_left (h : bdd_above s) : bdd_above (s ∩ t) := h.mono $ inter_subset_left s t /-- If `t` is bounded, then so is `s ∩ t` -/ lemma bdd_above.inter_of_right (h : bdd_above t) : bdd_above (s ∩ t) := h.mono $ inter_subset_right s t /-- If `s` is bounded, then so is `s ∩ t` -/ lemma bdd_below.inter_of_left (h : bdd_below s) : bdd_below (s ∩ t) := h.mono $ inter_subset_left s t /-- If `t` is bounded, then so is `s ∩ t` -/ lemma bdd_below.inter_of_right (h : bdd_below t) : bdd_below (s ∩ t) := h.mono $ inter_subset_right s t /-- If `s` and `t` are bounded above sets in a `semilattice_sup`, then so is `s ∪ t`. -/ lemma bdd_above.union [semilattice_sup γ] {s t : set γ} : bdd_above s → bdd_above t → bdd_above (s ∪ t) := begin rintros ⟨bs, hs⟩ ⟨bt, ht⟩, use bs ⊔ bt, rw upper_bounds_union, exact ⟨upper_bounds_mono_mem le_sup_left hs, upper_bounds_mono_mem le_sup_right ht⟩ end /-- The union of two sets is bounded above if and only if each of the sets is. -/ lemma bdd_above_union [semilattice_sup γ] {s t : set γ} : bdd_above (s ∪ t) ↔ bdd_above s ∧ bdd_above t := ⟨λ h, ⟨h.mono $ subset_union_left s t, h.mono $ subset_union_right s t⟩, λ h, h.1.union h.2⟩ lemma bdd_below.union [semilattice_inf γ] {s t : set γ} : bdd_below s → bdd_below t → bdd_below (s ∪ t) := @bdd_above.union (order_dual γ) _ s t /--The union of two sets is bounded above if and only if each of the sets is.-/ lemma bdd_below_union [semilattice_inf γ] {s t : set γ} : bdd_below (s ∪ t) ↔ bdd_below s ∧ bdd_below t := @bdd_above_union (order_dual γ) _ s t /-- If `a` is the least upper bound of `s` and `b` is the least upper bound of `t`, then `a ⊔ b` is the least upper bound of `s ∪ t`. -/ lemma is_lub.union [semilattice_sup γ] {a b : γ} {s t : set γ} (hs : is_lub s a) (ht : is_lub t b) : is_lub (s ∪ t) (a ⊔ b) := ⟨assume c h, h.cases_on (λ h, le_sup_left_of_le $ hs.left h) (λ h, le_sup_right_of_le $ ht.left h), assume c hc, sup_le (hs.right $ assume d hd, hc $ or.inl hd) (ht.right $ assume d hd, hc $ or.inr hd)⟩ /-- If `a` is the greatest lower bound of `s` and `b` is the greatest lower bound of `t`, then `a ⊓ b` is the greatest lower bound of `s ∪ t`. -/ lemma is_glb.union [semilattice_inf γ] {a₁ a₂ : γ} {s t : set γ} (hs : is_glb s a₁) (ht : is_glb t a₂) : is_glb (s ∪ t) (a₁ ⊓ a₂) := @is_lub.union (order_dual γ) _ _ _ _ _ hs ht /-- If `a` is the least element of `s` and `b` is the least element of `t`, then `min a b` is the least element of `s ∪ t`. -/ lemma is_least.union [linear_order γ] {a b : γ} {s t : set γ} (ha : is_least s a) (hb : is_least t b) : is_least (s ∪ t) (min a b) := ⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1], (ha.is_glb.union hb.is_glb).1⟩ /-- If `a` is the greatest element of `s` and `b` is the greatest element of `t`, then `max a b` is the greatest element of `s ∪ t`. -/ lemma is_greatest.union [linear_order γ] {a b : γ} {s t : set γ} (ha : is_greatest s a) (hb : is_greatest t b) : is_greatest (s ∪ t) (max a b) := ⟨by cases (le_total a b) with h h; simp [h, ha.1, hb.1], (ha.is_lub.union hb.is_lub).1⟩ /-! ### Specific sets #### Unbounded intervals -/ lemma is_least_Ici : is_least (Ici a) a := ⟨left_mem_Ici, λ x, id⟩ lemma is_greatest_Iic : is_greatest (Iic a) a := ⟨right_mem_Iic, λ x, id⟩ lemma is_lub_Iic : is_lub (Iic a) a := is_greatest_Iic.is_lub lemma is_glb_Ici : is_glb (Ici a) a := is_least_Ici.is_glb lemma upper_bounds_Iic : upper_bounds (Iic a) = Ici a := is_lub_Iic.upper_bounds_eq lemma lower_bounds_Ici : lower_bounds (Ici a) = Iic a := is_glb_Ici.lower_bounds_eq lemma bdd_above_Iic : bdd_above (Iic a) := is_lub_Iic.bdd_above lemma bdd_below_Ici : bdd_below (Ici a) := is_glb_Ici.bdd_below lemma bdd_above_Iio : bdd_above (Iio a) := ⟨a, λ x hx, le_of_lt hx⟩ lemma bdd_below_Ioi : bdd_below (Ioi a) := ⟨a, λ x hx, le_of_lt hx⟩ section variables [linear_order γ] [densely_ordered γ] lemma is_lub_Iio {a : γ} : is_lub (Iio a) a := ⟨λ x hx, le_of_lt hx, λ y hy, le_of_forall_ge_of_dense hy⟩ lemma is_glb_Ioi {a : γ} : is_glb (Ioi a) a := @is_lub_Iio (order_dual γ) _ _ a lemma upper_bounds_Iio {a : γ} : upper_bounds (Iio a) = Ici a := is_lub_Iio.upper_bounds_eq lemma lower_bounds_Ioi {a : γ} : lower_bounds (Ioi a) = Iic a := is_glb_Ioi.lower_bounds_eq end /-! #### Singleton -/ lemma is_greatest_singleton : is_greatest {a} a := ⟨mem_singleton a, λ x hx, le_of_eq $ eq_of_mem_singleton hx⟩ lemma is_least_singleton : is_least {a} a := @is_greatest_singleton (order_dual α) _ a lemma is_lub_singleton : is_lub {a} a := is_greatest_singleton.is_lub lemma is_glb_singleton : is_glb {a} a := is_least_singleton.is_glb lemma bdd_above_singleton : bdd_above ({a} : set α) := is_lub_singleton.bdd_above lemma bdd_below_singleton : bdd_below ({a} : set α) := is_glb_singleton.bdd_below @[simp] lemma upper_bounds_singleton : upper_bounds {a} = Ici a := is_lub_singleton.upper_bounds_eq @[simp] lemma lower_bounds_singleton : lower_bounds {a} = Iic a := is_glb_singleton.lower_bounds_eq /-! #### Bounded intervals -/ lemma bdd_above_Icc : bdd_above (Icc a b) := ⟨b, λ _, and.right⟩ lemma bdd_above_Ico : bdd_above (Ico a b) := bdd_above_Icc.mono Ico_subset_Icc_self lemma bdd_above_Ioc : bdd_above (Ioc a b) := bdd_above_Icc.mono Ioc_subset_Icc_self lemma bdd_above_Ioo : bdd_above (Ioo a b) := bdd_above_Icc.mono Ioo_subset_Icc_self lemma is_greatest_Icc (h : a ≤ b) : is_greatest (Icc a b) b := ⟨right_mem_Icc.2 h, λ x, and.right⟩ lemma is_lub_Icc (h : a ≤ b) : is_lub (Icc a b) b := (is_greatest_Icc h).is_lub lemma upper_bounds_Icc (h : a ≤ b) : upper_bounds (Icc a b) = Ici b := (is_lub_Icc h).upper_bounds_eq lemma is_least_Icc (h : a ≤ b) : is_least (Icc a b) a := ⟨left_mem_Icc.2 h, λ x, and.left⟩ lemma is_glb_Icc (h : a ≤ b) : is_glb (Icc a b) a := (is_least_Icc h).is_glb lemma lower_bounds_Icc (h : a ≤ b) : lower_bounds (Icc a b) = Iic a := (is_glb_Icc h).lower_bounds_eq lemma is_greatest_Ioc (h : a < b) : is_greatest (Ioc a b) b := ⟨right_mem_Ioc.2 h, λ x, and.right⟩ lemma is_lub_Ioc (h : a < b) : is_lub (Ioc a b) b := (is_greatest_Ioc h).is_lub lemma upper_bounds_Ioc (h : a < b) : upper_bounds (Ioc a b) = Ici b := (is_lub_Ioc h).upper_bounds_eq lemma is_least_Ico (h : a < b) : is_least (Ico a b) a := ⟨left_mem_Ico.2 h, λ x, and.left⟩ lemma is_glb_Ico (h : a < b) : is_glb (Ico a b) a := (is_least_Ico h).is_glb lemma lower_bounds_Ico (h : a < b) : lower_bounds (Ico a b) = Iic a := (is_glb_Ico h).lower_bounds_eq section variables [linear_order γ] [densely_ordered γ] lemma is_glb_Ioo {a b : γ} (hab : a < b) : is_glb (Ioo a b) a := begin refine ⟨λx hx, le_of_lt hx.1, λy hy, le_of_not_lt $ λ h, _⟩, have : a < min b y, by { rw lt_min_iff, exact ⟨hab, h⟩ }, rcases exists_between this with ⟨z, az, zy⟩, rw lt_min_iff at zy, exact lt_irrefl _ (lt_of_le_of_lt (hy ⟨az, zy.1⟩) zy.2) end lemma lower_bounds_Ioo {a b : γ} (hab : a < b) : lower_bounds (Ioo a b) = Iic a := (is_glb_Ioo hab).lower_bounds_eq lemma is_glb_Ioc {a b : γ} (hab : a < b) : is_glb (Ioc a b) a := (is_glb_Ioo hab).of_subset_of_superset (is_glb_Icc $ le_of_lt hab) Ioo_subset_Ioc_self Ioc_subset_Icc_self lemma lower_bound_Ioc {a b : γ} (hab : a < b) : lower_bounds (Ioc a b) = Iic a := (is_glb_Ioc hab).lower_bounds_eq lemma is_lub_Ioo {a b : γ} (hab : a < b) : is_lub (Ioo a b) b := by simpa only [dual_Ioo] using @is_glb_Ioo (order_dual γ) _ _ b a hab lemma upper_bounds_Ioo {a b : γ} (hab : a < b) : upper_bounds (Ioo a b) = Ici b := (is_lub_Ioo hab).upper_bounds_eq lemma is_lub_Ico {a b : γ} (hab : a < b) : is_lub (Ico a b) b := by simpa only [dual_Ioc] using @is_glb_Ioc (order_dual γ) _ _ b a hab lemma upper_bounds_Ico {a b : γ} (hab : a < b) : upper_bounds (Ico a b) = Ici b := (is_lub_Ico hab).upper_bounds_eq end lemma bdd_below_iff_subset_Ici : bdd_below s ↔ ∃ a, s ⊆ Ici a := iff.rfl lemma bdd_above_iff_subset_Iic : bdd_above s ↔ ∃ a, s ⊆ Iic a := iff.rfl lemma bdd_below_bdd_above_iff_subset_Icc : bdd_below s ∧ bdd_above s ↔ ∃ a b, s ⊆ Icc a b := by simp only [Ici_inter_Iic.symm, subset_inter_iff, bdd_below_iff_subset_Ici, bdd_above_iff_subset_Iic, exists_and_distrib_left, exists_and_distrib_right] /-! ### Univ -/ lemma order_top.upper_bounds_univ [order_top γ] : upper_bounds (univ : set γ) = {⊤} := set.ext $ λ b, iff.trans ⟨λ hb, top_unique $ hb trivial, λ hb x hx, hb.symm ▸ le_top⟩ mem_singleton_iff.symm lemma is_greatest_univ [order_top γ] : is_greatest (univ : set γ) ⊤ := by simp only [is_greatest, order_top.upper_bounds_univ, mem_univ, mem_singleton, true_and] lemma is_lub_univ [order_top γ] : is_lub (univ : set γ) ⊤ := is_greatest_univ.is_lub lemma order_bot.lower_bounds_univ [order_bot γ] : lower_bounds (univ : set γ) = {⊥} := @order_top.upper_bounds_univ (order_dual γ) _ lemma is_least_univ [order_bot γ] : is_least (univ : set γ) ⊥ := @is_greatest_univ (order_dual γ) _ lemma is_glb_univ [order_bot γ] : is_glb (univ : set γ) ⊥ := is_least_univ.is_glb lemma no_top_order.upper_bounds_univ [no_top_order α] : upper_bounds (univ : set α) = ∅ := eq_empty_of_subset_empty $ λ b hb, let ⟨x, hx⟩ := no_top b in not_le_of_lt hx (hb trivial) lemma no_bot_order.lower_bounds_univ [no_bot_order α] : lower_bounds (univ : set α) = ∅ := @no_top_order.upper_bounds_univ (order_dual α) _ _ /-! ### Empty set -/ @[simp] lemma upper_bounds_empty : upper_bounds (∅ : set α) = univ := by simp only [upper_bounds, eq_univ_iff_forall, mem_set_of_eq, ball_empty_iff, forall_true_iff] @[simp] lemma lower_bounds_empty : lower_bounds (∅ : set α) = univ := @upper_bounds_empty (order_dual α) _ @[simp] lemma bdd_above_empty [nonempty α] : bdd_above (∅ : set α) := by simp only [bdd_above, upper_bounds_empty, univ_nonempty] @[simp] lemma bdd_below_empty [nonempty α] : bdd_below (∅ : set α) := by simp only [bdd_below, lower_bounds_empty, univ_nonempty] lemma is_glb_empty [order_top γ] : is_glb ∅ (⊤:γ) := by simp only [is_glb, lower_bounds_empty, is_greatest_univ] lemma is_lub_empty [order_bot γ] : is_lub ∅ (⊥:γ) := @is_glb_empty (order_dual γ) _ lemma is_lub.nonempty [no_bot_order α] (hs : is_lub s a) : s.nonempty := let ⟨a', ha'⟩ := no_bot a in ne_empty_iff_nonempty.1 $ assume h, have a ≤ a', from hs.right $ by simp only [h, upper_bounds_empty], not_le_of_lt ha' this lemma is_glb.nonempty [no_top_order α] (hs : is_glb s a) : s.nonempty := @is_lub.nonempty (order_dual α) _ _ _ _ hs lemma nonempty_of_not_bdd_above [ha : nonempty α] (h : ¬bdd_above s) : s.nonempty := nonempty.elim ha $ λ x, (not_bdd_above_iff'.1 h x).imp $ λ a ha, ha.fst lemma nonempty_of_not_bdd_below [ha : nonempty α] (h : ¬bdd_below s) : s.nonempty := @nonempty_of_not_bdd_above (order_dual α) _ _ _ h /-! ### insert -/ /-- Adding a point to a set preserves its boundedness above. -/ @[simp] lemma bdd_above_insert [semilattice_sup γ] (a : γ) {s : set γ} : bdd_above (insert a s) ↔ bdd_above s := by simp only [insert_eq, bdd_above_union, bdd_above_singleton, true_and] lemma bdd_above.insert [semilattice_sup γ] (a : γ) {s : set γ} (hs : bdd_above s) : bdd_above (insert a s) := (bdd_above_insert a).2 hs /--Adding a point to a set preserves its boundedness below.-/ @[simp] lemma bdd_below_insert [semilattice_inf γ] (a : γ) {s : set γ} : bdd_below (insert a s) ↔ bdd_below s := by simp only [insert_eq, bdd_below_union, bdd_below_singleton, true_and] lemma bdd_below.insert [semilattice_inf γ] (a : γ) {s : set γ} (hs : bdd_below s) : bdd_below (insert a s) := (bdd_below_insert a).2 hs lemma is_lub.insert [semilattice_sup γ] (a) {b} {s : set γ} (hs : is_lub s b) : is_lub (insert a s) (a ⊔ b) := by { rw insert_eq, exact is_lub_singleton.union hs } lemma is_glb.insert [semilattice_inf γ] (a) {b} {s : set γ} (hs : is_glb s b) : is_glb (insert a s) (a ⊓ b) := by { rw insert_eq, exact is_glb_singleton.union hs } lemma is_greatest.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_greatest s b) : is_greatest (insert a s) (max a b) := by { rw insert_eq, exact is_greatest_singleton.union hs } lemma is_least.insert [linear_order γ] (a) {b} {s : set γ} (hs : is_least s b) : is_least (insert a s) (min a b) := by { rw insert_eq, exact is_least_singleton.union hs } @[simp] lemma upper_bounds_insert (a : α) (s : set α) : upper_bounds (insert a s) = Ici a ∩ upper_bounds s := by rw [insert_eq, upper_bounds_union, upper_bounds_singleton] @[simp] lemma lower_bounds_insert (a : α) (s : set α) : lower_bounds (insert a s) = Iic a ∩ lower_bounds s := by rw [insert_eq, lower_bounds_union, lower_bounds_singleton] /-- When there is a global maximum, every set is bounded above. -/ @[simp] protected lemma order_top.bdd_above [order_top γ] (s : set γ) : bdd_above s := ⟨⊤, assume a ha, order_top.le_top a⟩ /-- When there is a global minimum, every set is bounded below. -/ @[simp] protected lemma order_bot.bdd_below [order_bot γ] (s : set γ) : bdd_below s := ⟨⊥, assume a ha, order_bot.bot_le a⟩ /-! ### Pair -/ lemma is_lub_pair [semilattice_sup γ] {a b : γ} : is_lub {a, b} (a ⊔ b) := is_lub_singleton.insert _ lemma is_glb_pair [semilattice_inf γ] {a b : γ} : is_glb {a, b} (a ⊓ b) := is_glb_singleton.insert _ lemma is_least_pair [linear_order γ] {a b : γ} : is_least {a, b} (min a b) := is_least_singleton.insert _ lemma is_greatest_pair [linear_order γ] {a b : γ} : is_greatest {a, b} (max a b) := is_greatest_singleton.insert _ end /-! ### (In)equalities with the least upper bound and the greatest lower bound -/ section preorder variables [preorder α] {s : set α} {a b : α} lemma lower_bounds_le_upper_bounds (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) : s.nonempty → a ≤ b | ⟨c, hc⟩ := le_trans (ha hc) (hb hc) lemma is_glb_le_is_lub (ha : is_glb s a) (hb : is_lub s b) (hs : s.nonempty) : a ≤ b := lower_bounds_le_upper_bounds ha.1 hb.1 hs lemma is_lub_lt_iff (ha : is_lub s a) : a < b ↔ ∃ c ∈ upper_bounds s, c < b := ⟨λ hb, ⟨a, ha.1, hb⟩, λ ⟨c, hcs, hcb⟩, lt_of_le_of_lt (ha.2 hcs) hcb⟩ lemma lt_is_glb_iff (ha : is_glb s a) : b < a ↔ ∃ c ∈ lower_bounds s, b < c := @is_lub_lt_iff (order_dual α) _ s _ _ ha end preorder section partial_order variables [partial_order α] {s : set α} {a b : α} lemma is_least.unique (Ha : is_least s a) (Hb : is_least s b) : a = b := le_antisymm (Ha.right Hb.left) (Hb.right Ha.left) lemma is_least.is_least_iff_eq (Ha : is_least s a) : is_least s b ↔ a = b := iff.intro Ha.unique (assume h, h ▸ Ha) lemma is_greatest.unique (Ha : is_greatest s a) (Hb : is_greatest s b) : a = b := le_antisymm (Hb.right Ha.left) (Ha.right Hb.left) lemma is_greatest.is_greatest_iff_eq (Ha : is_greatest s a) : is_greatest s b ↔ a = b := iff.intro Ha.unique (assume h, h ▸ Ha) lemma is_lub.unique (Ha : is_lub s a) (Hb : is_lub s b) : a = b := Ha.unique Hb lemma is_glb.unique (Ha : is_glb s a) (Hb : is_glb s b) : a = b := Ha.unique Hb end partial_order section linear_order variables [linear_order α] {s : set α} {a b : α} lemma lt_is_lub_iff (h : is_lub s a) : b < a ↔ ∃ c ∈ s, b < c := by haveI := classical.dec; simpa [upper_bounds, not_ball] using not_congr (@is_lub_le_iff _ _ _ _ b h) lemma is_glb_lt_iff (h : is_glb s a) : a < b ↔ ∃ c ∈ s, c < b := @lt_is_lub_iff (order_dual α) _ _ _ _ h end linear_order /-! ### Least upper bound and the greatest lower bound in linear ordered additive commutative groups -/ section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] {s : set α} {a ε : α} (h₃ : 0 < ε) include h₃ lemma is_glb.exists_between_self_add (h₁ : is_glb s a) : ∃ b, b ∈ s ∧ a ≤ b ∧ b < a + ε := begin have h' : a + ε ∉ lower_bounds s, { set A := a + ε, have : a < A := by { simp [A, h₃] }, intros hA, exact lt_irrefl a (lt_of_lt_of_le this (h₁.2 hA)) }, obtain ⟨b, hb, hb'⟩ : ∃ b ∈ s, b < a + ε, by simpa [lower_bounds] using h', exact ⟨b, hb, h₁.1 hb, hb'⟩ end lemma is_glb.exists_between_self_add' (h₁ : is_glb s a) (h₂ : a ∉ s) : ∃ b, b ∈ s ∧ a < b ∧ b < a + ε := begin rcases h₁.exists_between_self_add h₃ with ⟨b, b_in, hb₁, hb₂⟩, have h₅ : a ≠ b, { intros contra, apply h₂, rwa ← contra at b_in }, exact ⟨b, b_in, lt_of_le_of_ne (h₁.1 b_in) h₅, hb₂⟩ end lemma is_lub.exists_between_sub_self (h₁ : is_lub s a) : ∃ b, b ∈ s ∧ a - ε < b ∧ b ≤ a := begin have h' : a - ε ∉ upper_bounds s, { set A := a - ε, have : A < a := sub_lt_self a h₃, intros hA, exact lt_irrefl a (lt_of_le_of_lt (h₁.2 hA) this) }, obtain ⟨b, hb, hb'⟩ : ∃ (x : α), x ∈ s ∧ a - ε < x, by simpa [upper_bounds] using h', exact ⟨b, hb, hb', h₁.1 hb⟩ end lemma is_lub.exists_between_sub_self' (h₁ : is_lub s a) (h₂ : a ∉ s) : ∃ b, b ∈ s ∧ a - ε < b ∧ b < a := begin rcases h₁.exists_between_sub_self h₃ with ⟨b, b_in, hb₁, hb₂⟩, have h₅ : a ≠ b, { intros contra, apply h₂, rwa ← contra at b_in }, exact ⟨b, b_in, hb₁, lt_of_le_of_ne (h₁.1 b_in) h₅.symm⟩ end end linear_ordered_add_comm_group /-! ### Images of upper/lower bounds under monotone functions -/ namespace monotone variables [preorder α] [preorder β] {f : α → β} (Hf : monotone f) {a : α} {s : set α} lemma mem_upper_bounds_image (Ha : a ∈ upper_bounds s) : f a ∈ upper_bounds (f '' s) := ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›)) lemma mem_lower_bounds_image (Ha : a ∈ lower_bounds s) : f a ∈ lower_bounds (f '' s) := ball_image_of_ball (assume x H, Hf (Ha ‹x ∈ s›)) /-- The image under a monotone function of a set which is bounded above is bounded above. -/ lemma map_bdd_above (hf : monotone f) : bdd_above s → bdd_above (f '' s) | ⟨C, hC⟩ := ⟨f C, hf.mem_upper_bounds_image hC⟩ /-- The image under a monotone function of a set which is bounded below is bounded below. -/ lemma map_bdd_below (hf : monotone f) : bdd_below s → bdd_below (f '' s) | ⟨C, hC⟩ := ⟨f C, hf.mem_lower_bounds_image hC⟩ /-- A monotone map sends a least element of a set to a least element of its image. -/ lemma map_is_least (Ha : is_least s a) : is_least (f '' s) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_lower_bounds_image Ha.2⟩ /-- A monotone map sends a greatest element of a set to a greatest element of its image. -/ lemma map_is_greatest (Ha : is_greatest s a) : is_greatest (f '' s) (f a) := ⟨mem_image_of_mem _ Ha.1, Hf.mem_upper_bounds_image Ha.2⟩ lemma is_lub_image_le (Ha : is_lub s a) {b : β} (Hb : is_lub (f '' s) b) : b ≤ f a := Hb.2 (Hf.mem_upper_bounds_image Ha.1) lemma le_is_glb_image (Ha : is_glb s a) {b : β} (Hb : is_glb (f '' s) b) : f a ≤ b := Hb.2 (Hf.mem_lower_bounds_image Ha.1) end monotone lemma is_glb.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_glb (f '' s) (f x)) : is_glb s x := ⟨λ y hy, hf.1 $ hx.1 $ mem_image_of_mem _ hy, λ y hy, hf.1 $ hx.2 $ monotone.mem_lower_bounds_image (λ x y, hf.2) hy⟩ lemma is_lub.of_image [preorder α] [preorder β] {f : α → β} (hf : ∀ {x y}, f x ≤ f y ↔ x ≤ y) {s : set α} {x : α} (hx : is_lub (f '' s) (f x)) : is_lub s x := @is_glb.of_image (order_dual α) (order_dual β) _ _ f (λ x y, hf) _ _ hx
ef4adcd531a6b6ca2f2e1f0cd223eb35f2695194
ff691356469bf5f2bf3e4f1bdd8ad19630e71465
/cayley-dickson.hlean
addf184b4b91a397d0b984ed2e32ae443390b74e
[ "Apache-2.0" ]
permissive
EgbertRijke/K-theory
45952adb052bd51646474c07fdd9ee5c9f9d5f7f
e1be97216092617f6c97f0e6534ad42241b01899
refs/heads/master
1,610,298,649,146
1,447,810,070,000
1,447,810,070,000
46,100,234
0
0
null
null
null
null
UTF-8
Lean
false
false
5,904
hlean
/- Copyright (c) 2015 Ulrik Buchholtz and Egbert Rijke. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ulrik Buchholtz, Egbert Rijke Cayley-Dickson construction on the level of spheres. -/ import algebra.group homotopy.join cubical.square open eq -- TODO: move all of these to hott std lib namespace pi section variables {A : Type} {B : A → Type} {f g : Πa, B a} definition ap_eval_eq_adp10 (a : A) (H : f = g) : ap (λh : Πx, B x, h a) H = apd10 H a := eq.rec_on H idp end end pi namespace algebra structure has_star [class] (A : Type) := (star : A → A) reserve postfix `*` : (max+1) postfix `*` := has_star.star structure h_space [class] (A : Type) extends has_mul A, has_one A := (one_mul : ∀a, mul one a = a) (mul_one : ∀a, mul a one = a) end algebra namespace bool definition biff (a b : bool) := bool.rec_on a (bnot b) b end bool namespace join section parameters {A B : Type} open pushout protected definition rec {P : join A B → Type} (Pinl : Π(x : A), P (inl x)) (Pinr : Π(y : B), P (inr y)) (Pglue : Π(x : A)(y : B), Pinl x =[jglue x y] Pinr y) (z : join A B) : P z := pushout.rec Pinl Pinr (prod.rec Pglue) z theorem rec_glue {P : join A B → Type} (Pinl : Π(x : A), P (inl x)) (Pinr : Π(y : B), P (inr y)) (Pglue : Π(x : A)(y : B), Pinl x =[jglue x y] Pinr y) (x : A) (y : B) : apdo (rec Pinl Pinr Pglue) (jglue x y) = Pglue x y := !quotient.rec_eq_of_rel protected definition elim {P : Type} (Pinl : A → P) (Pinr : B → P) (Pglue : Π(x : A)(y : B), Pinl x = Pinr y) (z : join A B) : P := rec Pinl Pinr (λx y, pathover_of_eq (Pglue x y)) z theorem elim_glue {P : Type} (Pinl : A → P) (Pinr : B → P) (Pglue : Π(x : A)(y : B), Pinl x = Pinr y) (x : A) (y : B) : ap (elim Pinl Pinr Pglue) (jglue x y) = Pglue x y := begin apply equiv.eq_of_fn_eq_fn_inv !(pathover_constant (jglue x y)), rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑join.elim], apply rec_glue end definition join_square {a a' : A} {b b' : B} (p : a = a') (q : b = b') : square (ap inl p) (ap inr q) (jglue a b) (jglue a' b') := eq.rec_on p (eq.rec_on q hrfl) end end join -- Here starts the main content: namespace homotopy open algebra structure cayley_dickson [class] (A : Type) extends h_space A, has_neg A, has_star A := (one_star : star one = one) (neg_neg : ∀a, neg (neg a) = a) (star_star : ∀a, star (star a) = a) (star_mul : ∀a b, star (mul a b) = mul (star b) (star a)) /- possible further laws: neg_mul : ∀a b, mul (neg a) b = neg (mul a b) neg_star : ∀a, star (neg a) = neg (star a) norm : ∀a, mul (star a) a = one -- this expresses that A is nicely normed … -/ section variable {A : Type} theorem one_mul [s : h_space A] (a : A) : 1 * a = a := !h_space.one_mul theorem mul_one [s : h_space A] (a : A) : a * 1 = a := !h_space.mul_one theorem one_star [s : cayley_dickson A] : @eq A (1*) 1 := !cayley_dickson.one_star theorem star_one_mul (A : Type) [H : cayley_dickson A] (a : A) : 1* * a = a := calc 1* * a = 1 * a : by rewrite one_star ... = a : one_mul end section open bool definition cayley_dickson_bool [instance] : cayley_dickson bool := ⦃ cayley_dickson, one := tt, mul := biff, neg := bnot, star := function.id, one_mul := bool.rec idp idp, mul_one := bool.rec idp idp, neg_neg := bool.rec idp idp, star_star := λa, idp, one_star := idp, star_mul := bool.rec (bool.rec idp idp) (bool.rec idp idp) ⦄ end section parameter A : Type parameter [H : cayley_dickson A] include A include H open cayley_dickson open join open prod open pushout definition carrier : Type := join A A definition one [instance] : has_one carrier := ⦃ has_one, one := (inl 1) ⦄ definition neg [instance] : has_neg carrier := ⦃ has_neg, neg := join.elim (λa, inl (-a)) (λb, inr (-b)) (λa b, jglue (-a) (-b)) ⦄ definition star [instance] : has_star carrier := ⦃ has_star, star := join.elim (λa, inl (a*)) (λb, inr (-b)) (λa b, jglue (a*) (-b)) ⦄ /- in the algebraic form, the Cayley-Dickson multiplication has: (a,b)(c,d) = (a * c - d * b*, a* * d + c * b) here we do the spherical form where one of the coordinates is zero. -/ open eq.ops definition mul [instance] : has_mul carrier := ⦃ has_mul, mul := join.elim (λa, join.elim (λc, inl (a * c)) (λd, inr (a* * d)) (λc d, jglue (a * c) (a* * d))) (λb, join.elim (λc, inr (c * b)) (λd, inl (- d * b*)) (λc d, by apply (jglue (- d * b*) (c * b))⁻¹)) (λa b, begin apply eq_of_homotopy, fapply join.rec, { intro c, apply jglue (a * c) (c * b) }, { intro d, apply (jglue (- d * b*) (a* * d))⁻¹ }, { intros c d, apply eq_pathover, krewrite [join.elim_glue,join.elim_glue], exact sorry } end ) ⦄ definition cd_one_mul : ∀a : carrier, 1 * a = a := begin fapply join.rec, { intro a, apply ap inl, exact one_mul a }, { intro b, apply ap inr, exact star_one_mul A b }, { intros a b, apply eq_pathover, rewrite ap_id, krewrite join.elim_glue, apply join_square } end definition cd_mul_one : ∀a : carrier, a * 1 = a := begin fapply join.rec, { intro a, apply ap inl, exact mul_one a }, { intro b, apply ap inr, exact one_mul b }, { intros a b, apply eq_pathover, rewrite ap_id, krewrite [ap_compose (λf, f 1) (λx y, x * y)], krewrite join.elim_glue, krewrite [pi.ap_eval_eq_adp10 1], rewrite pi.apd10_eq_of_homotopy, apply join_square } end definition cd_h_space [instance] : h_space carrier := ⦃ h_space, one_mul := cd_one_mul, mul_one := cd_mul_one ⦄ end end homotopy
3a148715d229d1918f5bc8bc83d6bb3d11eface0
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/group_theory/specific_groups/dihedral.lean
8f396d7436b4cf7fbb1e23c696cbe912adb0726f
[ "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
5,879
lean
/- Copyright (c) 2020 Shing Tak Lam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Shing Tak Lam -/ import data.fintype.card import data.zmod.basic import group_theory.exponent import data.int.parity /-! # Dihedral Groups We define the dihedral groups `dihedral_group n`, with elements `r i` and `sr i` for `i : zmod n`. For `n ≠ 0`, `dihedral_group n` represents the symmetry group of the regular `n`-gon. `r i` represents the rotations of the `n`-gon by `2πi/n`, and `sr i` represents the reflections of the `n`-gon. `dihedral_group 0` corresponds to the infinite dihedral group. -/ /-- For `n ≠ 0`, `dihedral_group n` represents the symmetry group of the regular `n`-gon. `r i` represents the rotations of the `n`-gon by `2πi/n`, and `sr i` represents the reflections of the `n`-gon. `dihedral_group 0` corresponds to the infinite dihedral group. -/ @[derive decidable_eq] inductive dihedral_group (n : ℕ) : Type | r : zmod n → dihedral_group | sr : zmod n → dihedral_group namespace dihedral_group variables {n : ℕ} /-- Multiplication of the dihedral group. -/ private def mul : dihedral_group n → dihedral_group n → dihedral_group n | (r i) (r j) := r (i + j) | (r i) (sr j) := sr (j - i) | (sr i) (r j) := sr (i + j) | (sr i) (sr j) := r (j - i) /-- The identity `1` is the rotation by `0`. -/ private def one : dihedral_group n := r 0 instance : inhabited (dihedral_group n) := ⟨one⟩ /-- The inverse of a an element of the dihedral group. -/ private def inv : dihedral_group n → dihedral_group n | (r i) := r (-i) | (sr i) := sr i /-- The group structure on `dihedral_group n`. -/ instance : group (dihedral_group n) := { mul := mul, mul_assoc := begin rintros (a | a) (b | b) (c | c); simp only [mul]; ring, end, one := one, one_mul := begin rintros (a | a), exact congr_arg r (zero_add a), exact congr_arg sr (sub_zero a), end, mul_one := begin rintros (a | a), exact congr_arg r (add_zero a), exact congr_arg sr (add_zero a), end, inv := inv, mul_left_inv := begin rintros (a | a), exact congr_arg r (neg_add_self a), exact congr_arg r (sub_self a), end } @[simp] lemma r_mul_r (i j : zmod n) : r i * r j = r (i + j) := rfl @[simp] lemma r_mul_sr (i j : zmod n) : r i * sr j = sr (j - i) := rfl @[simp] lemma sr_mul_r (i j : zmod n) : sr i * r j = sr (i + j) := rfl @[simp] lemma sr_mul_sr (i j : zmod n) : sr i * sr j = r (j - i) := rfl lemma one_def : (1 : dihedral_group n) = r 0 := rfl private def fintype_helper : (zmod n ⊕ zmod n) ≃ dihedral_group n := { inv_fun := λ i, match i with | (r j) := sum.inl j | (sr j) := sum.inr j end, to_fun := λ i, match i with | (sum.inl j) := r j | (sum.inr j) := sr j end, left_inv := by rintro (x | x); refl, right_inv := by rintro (x | x); refl } /-- If `0 < n`, then `dihedral_group n` is a finite group. -/ instance [fact (0 < n)] : fintype (dihedral_group n) := fintype.of_equiv _ fintype_helper instance : nontrivial (dihedral_group n) := ⟨⟨r 0, sr 0, dec_trivial⟩⟩ /-- If `0 < n`, then `dihedral_group n` has `2n` elements. -/ lemma card [fact (0 < n)] : fintype.card (dihedral_group n) = 2 * n := by rw [← fintype.card_eq.mpr ⟨fintype_helper⟩, fintype.card_sum, zmod.card, two_mul] @[simp] lemma r_one_pow (k : ℕ) : (r 1 : dihedral_group n) ^ k = r k := begin induction k with k IH, { refl }, { rw [pow_succ, IH, r_mul_r], congr' 1, norm_cast, rw nat.one_add } end @[simp] lemma r_one_pow_n : (r (1 : zmod n))^n = 1 := begin cases n, { rw pow_zero }, { rw [r_one_pow, one_def], congr' 1, exact zmod.nat_cast_self _, } end @[simp] lemma sr_mul_self (i : zmod n) : sr i * sr i = 1 := by rw [sr_mul_sr, sub_self, one_def] /-- If `0 < n`, then `sr i` has order 2. -/ @[simp] lemma order_of_sr (i : zmod n) : order_of (sr i) = 2 := begin rw order_of_eq_prime _ _, { exact ⟨nat.prime_two⟩ }, rw [sq, sr_mul_self], dec_trivial, end /-- If `0 < n`, then `r 1` has order `n`. -/ @[simp] lemma order_of_r_one : order_of (r 1 : dihedral_group n) = n := begin rcases n.eq_zero_or_pos with rfl | hn, { rw order_of_eq_zero_iff', intros n hn, rw [r_one_pow, one_def], apply mt r.inj, simpa using hn.ne' }, { haveI := fact.mk hn, apply (nat.le_of_dvd hn $ order_of_dvd_of_pow_eq_one $ @r_one_pow_n n).lt_or_eq.resolve_left, intro h, have h1 : (r 1 : dihedral_group n)^(order_of (r 1)) = 1, { exact pow_order_of_eq_one _ }, rw r_one_pow at h1, injection h1 with h2, rw [← zmod.val_eq_zero, zmod.val_nat_cast, nat.mod_eq_of_lt h] at h2, exact absurd h2.symm (order_of_pos _).ne }, end /-- If `0 < n`, then `i : zmod n` has order `n / gcd n i`. -/ lemma order_of_r [fact (0 < n)] (i : zmod n) : order_of (r i) = n / nat.gcd n i.val := begin conv_lhs { rw ←zmod.nat_cast_zmod_val i }, rw [←r_one_pow, order_of_pow, order_of_r_one] end lemma exponent : monoid.exponent (dihedral_group n) = lcm n 2 := begin rcases n.eq_zero_or_pos with rfl | hn, { exact monoid.exponent_eq_zero_of_order_zero order_of_r_one }, haveI := fact.mk hn, apply nat.dvd_antisymm, { apply monoid.exponent_dvd_of_forall_pow_eq_one, rintro (m | m), { rw [←order_of_dvd_iff_pow_eq_one, order_of_r], refine nat.dvd_trans ⟨gcd n m.val, _⟩ (dvd_lcm_left n 2), { exact (nat.div_mul_cancel (nat.gcd_dvd_left n m.val)).symm } }, { rw [←order_of_dvd_iff_pow_eq_one, order_of_sr], exact dvd_lcm_right n 2 } }, { apply lcm_dvd, { convert monoid.order_dvd_exponent (r 1), exact order_of_r_one.symm }, { convert monoid.order_dvd_exponent (sr 0), exact (order_of_sr 0).symm } } end end dihedral_group
5663061b35f623093a6e8f46892db6ed36eabcb2
f20db13587f4dd28a4b1fbd31953afd491691fa0
/library/init/data/basic.lean
fb480735f52f2eb3cd390acf58651c38cfafd9ea
[ "Apache-2.0" ]
permissive
AHartNtkn/lean
9a971edfc6857c63edcbf96bea6841b9a84cf916
0d83a74b26541421fc1aa33044c35b03759710ed
refs/heads/master
1,620,592,591,236
1,516,749,881,000
1,516,749,881,000
118,697,288
1
0
null
1,516,759,470,000
1,516,759,470,000
null
UTF-8
Lean
false
false
597
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.data.setoid init.data.quot init.data.bool.basic init.data.unit import init.data.nat.basic init.data.prod init.data.sum.basic import init.data.sigma.basic init.data.subtype.basic import init.data.fin.basic init.data.list.basic init.data.char.basic import init.data.string.basic init.data.option.basic init.data.set import init.data.unsigned.basic init.data.ordering.basic init.data.repr import init.data.to_string
5eaf731ace0ce749807eaaf753526968665d66fb
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/inferForallTypeLCNF.lean
f2cf58879d0d9790ec6b72a48e67c928c14a734e
[ "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
import Lean variable {U V} def f : (U → V) → (U → U) := sorry #eval Lean.Compiler.compile #[``f]
a3e12d0594b5113b7e77536f15630b1a825dd324
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/src/creates.lean
cb5bf57f2cbe467226324cc9a5990723e117414a
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
5,219
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.limits import category_theory.limits.preserves open category_theory category_theory.limits namespace category_theory universes v u₁ u₂ u₃ variables {C : Type u₁} [𝒞 : category.{v} C] include 𝒞 section isomorphisms variables {J : Type v} [small_category J] {K : J ⥤ C} /-- Given a cone morphism whose object part is an isomorphism, produce an isomorphism of cones. -/ def cone_iso_of_hom_iso {K : J ⥤ C} {c d : cone K} (f : c ⟶ d) [i : is_iso f.hom] : is_iso f := { inv := { hom := i.inv, w' := λ j, (as_iso f.hom).inv_comp_eq.2 (f.w j).symm } } variables {D : Type u₂} [𝒟 : category.{v} D] include 𝒟 /-- Define what it means for a functor `F : C ⥤ D` to reflect isomorphisms: for any morphism `f : A ⟶ B`, if `F.map f` is an isomorphism then `f` is as well. Note that we do not assume or require that `F` is faithful. -/ class reflects_isomorphisms (F : C ⥤ D) := (reflects : Π {A B : C} (f : A ⟶ B) [is_iso (F.map f)], is_iso f) -- TODO: should cones.functoriality take K as an explicit argument? I think so... /-- If `F` reflects isomorphisms, then `cones.functoriality F` reflects isomorphisms as well. -/ instance reflects_cone_isomorphism (F : C ⥤ D) [reflects_isomorphisms F] (K : J ⥤ C) : reflects_isomorphisms (@cones.functoriality _ _ _ _ K _ _ F) := begin constructor, introsI, haveI : is_iso (F.map f.hom) := (cones.forget (K ⋙ F)).map_is_iso ((cones.functoriality F).map f), haveI := reflects_isomorphisms.reflects F f.hom, apply cone_iso_of_hom_iso end -- Having this as an instance seems to break resolution, so let's not. /-- If `F` reflects isos and `F.map f` is an iso, then `f` is an iso. -/ def is_iso_of_reflects_iso {A B : C} (f : A ⟶ B) (F : C ⥤ D) [h : reflects_isomorphisms F] [is_iso (F.map f)] : is_iso f := reflects_isomorphisms.reflects F f end isomorphisms variables {D : Type u₂} [𝒟 : category.{v} D] include 𝒟 variables {J : Type v} [small_category J] {K : J ⥤ C} /-- Note this definition is really only useful when `c` is a limit already. For a cone `c` for `K ⋙ F`, give a cone for `K` which is a lift of `c`, i.e. the image of it under `F` is (iso) to `c`. -/ structure lift_cone (K : J ⥤ C) (F : C ⥤ D) (c : cone (K ⋙ F)) := (above_cone : cone K) (above_hits_original : F.map_cone above_cone ≅ c) /-- Definition 3.3.1 of [Riehl]. We say that `F` creates limits of `K` if, given any limit cone `c` for `K ⋙ F` (i.e. below) we can lift it to a cone above, and further that `F` reflects limits for `K`. Note this is equivalent to Riehl's definition - the missing part here appears to be that the lifted cone is not a limit, but `reflects` guarantees that it is. If `F` reflects isomorphisms, it suffices to show only that the lifted cone is a limit - see `creates_limit_of_reflects_iso` -/ class creates_limit (K : J ⥤ C) (F : C ⥤ D) : Type (max u₁ u₂ v) := (lifts : Π (c : cone (K ⋙ F)), is_limit c → lift_cone K F c) (reflects : reflects_limit K F) class creates_limits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) : Type (max u₁ u₂ v) := (creates_limit : Π {K : J ⥤ C}, creates_limit K F) class creates_limits (F : C ⥤ D) : Type (max u₁ u₂ (v+1)) := (creates_limits_of_shape : Π {J : Type v} {𝒥 : small_category J}, by exactI creates_limits_of_shape J F) -- TODO: reflects iso is equivalent to reflecting limits of shape 1 /-- A helper to show a functor creates limits. In particular, if we can show that for any limit cone `c` for `K ⋙ F`, there is a lift of it which is a limit and `F` reflects isomorphisms, then `F` creates limits. Usually, `F` creating limits says that _any_ lift of `c` is a limit, but here we only need to show that our particular lift of `c` is a limit. -/ structure lifts_to_limit (K : J ⥤ C) (F : C ⥤ D) (c : cone (K ⋙ F)) (t : is_limit c) := (lifted : lift_cone K F c) (makes_limit : is_limit lifted.above_cone) /-- If `F` reflects isomorphisms and we can lift any limit cone to a limit cone, then `F` creates limits. -/ def creates_limit_of_reflects_iso {K : J ⥤ C} {F : C ⥤ D} [reflects_isomorphisms F] (h : Π c t, lifts_to_limit K F c t) : creates_limit K F := { lifts := λ c t, (h c t).lifted, reflects := { reflects := λ (d : cone K) (hd : is_limit (F.map_cone d)), begin let d' : cone K := (h (F.map_cone d) hd).lifted.above_cone, let hd'₁ : F.map_cone d' ≅ F.map_cone d := (h (F.map_cone d) hd).lifted.above_hits_original, let hd'₂ : is_limit d' := (h (F.map_cone d) hd).makes_limit, let f : d ⟶ d' := hd'₂.lift_cone_morphism d, have: F.map_cone_morphism f = hd'₁.inv := (hd.of_iso_limit hd'₁.symm).uniq_cone_morphism, have: @is_iso _ cone.category _ _ (functor.map_cone_morphism F f), rw this, apply_instance, haveI: is_iso ((cones.functoriality F).map f) := this, haveI := is_iso_of_reflects_iso f (cones.functoriality F), exact is_limit.of_iso_limit hd'₂ (as_iso f).symm, end } } end category_theory
1843de7a757398aba2d98dc0763061f7bc254ace
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/library/algebra/ring_power.lean
c50d7b895dce1a24df0a9a6f2a6f7db1f39a514f
[ "Apache-2.0" ]
permissive
tectronics/lean
ab977ba6be0fcd46047ddbb3c8e16e7c26710701
f38af35e0616f89c6e9d7e3eb1d48e47ee666efe
refs/heads/master
1,532,358,526,384
1,456,276,623,000
1,456,276,623,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,292
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Properties of the power operation in various structures, including ordered rings and fields. -/ import .group_power .ordered_field open nat variable {A : Type} section semiring variable [s : semiring A] include s definition semiring_has_pow_nat [reducible] [instance] : has_pow_nat A := monoid_has_pow_nat theorem zero_pow {m : ℕ} (mpos : m > 0) : 0^m = (0 : A) := have h₁ : ∀ m : nat, (0 : A)^(succ m) = (0 : A), begin intro m, induction m, krewrite pow_one, apply zero_mul end, obtain m' (h₂ : m = succ m'), from exists_eq_succ_of_pos mpos, show 0^m = 0, by rewrite h₂; apply h₁ end semiring section integral_domain variable [s : integral_domain A] include s definition integral_domain_has_pow_nat [reducible] [instance] : has_pow_nat A := monoid_has_pow_nat theorem eq_zero_of_pow_eq_zero {a : A} {m : ℕ} (H : a^m = 0) : a = 0 := or.elim (eq_zero_or_pos m) (suppose m = 0, by rewrite [`m = 0` at H, pow_zero at H]; apply absurd H (ne.symm zero_ne_one)) (suppose m > 0, have h₁ : ∀ m, a^succ m = 0 → a = 0, begin intro m, induction m with m ih, {krewrite pow_one; intros; assumption}, rewrite pow_succ, intro H, cases eq_zero_or_eq_zero_of_mul_eq_zero H with h₃ h₄, assumption, exact ih h₄ end, obtain m' (h₂ : m = succ m'), from exists_eq_succ_of_pos `m > 0`, show a = 0, by rewrite h₂ at H; apply h₁ m' H) theorem pow_ne_zero_of_ne_zero {a : A} {m : ℕ} (H : a ≠ 0) : a^m ≠ 0 := assume H', H (eq_zero_of_pow_eq_zero H') end integral_domain section division_ring variable [s : division_ring A] include s theorem division_ring.pow_ne_zero_of_ne_zero {a : A} {m : ℕ} (H : a ≠ 0) : a^m ≠ 0 := or.elim (eq_zero_or_pos m) (suppose m = 0, by rewrite [`m = 0`, pow_zero]; exact (ne.symm zero_ne_one)) (suppose m > 0, have h₁ : ∀ m, a^succ m ≠ 0, begin intro m, induction m with m ih, { krewrite pow_one; assumption }, rewrite pow_succ, apply division_ring.mul_ne_zero H ih end, obtain m' (h₂ : m = succ m'), from exists_eq_succ_of_pos `m > 0`, show a^m ≠ 0, by rewrite h₂; apply h₁ m') end division_ring section linear_ordered_semiring variable [s : linear_ordered_semiring A] include s theorem pow_pos_of_pos {x : A} (i : ℕ) (H : x > 0) : x^i > 0 := begin induction i with [j, ih], {show (1 : A) > 0, from zero_lt_one}, {show x^(succ j) > 0, from mul_pos H ih} end theorem pow_nonneg_of_nonneg {x : A} (i : ℕ) (H : x ≥ 0) : x^i ≥ 0 := begin induction i with j ih, {show (1 : A) ≥ 0, from le_of_lt zero_lt_one}, {show x^(succ j) ≥ 0, from mul_nonneg H ih} end theorem pow_le_pow_of_le {x y : A} (i : ℕ) (H₁ : 0 ≤ x) (H₂ : x ≤ y) : x^i ≤ y^i := begin induction i with i ih, {rewrite *pow_zero, apply le.refl}, rewrite *pow_succ, have H : 0 ≤ x^i, from pow_nonneg_of_nonneg i H₁, apply mul_le_mul H₂ ih H (le.trans H₁ H₂) end theorem pow_ge_one {x : A} (i : ℕ) (xge1 : x ≥ 1) : x^i ≥ 1 := assert H : x^i ≥ 1^i, from pow_le_pow_of_le i (le_of_lt zero_lt_one) xge1, by rewrite one_pow at H; exact H theorem pow_gt_one {x : A} {i : ℕ} (xgt1 : x > 1) (ipos : i > 0) : x^i > 1 := assert xpos : x > 0, from lt.trans zero_lt_one xgt1, begin induction i with [i, ih], {exfalso, exact !lt.irrefl ipos}, have xige1 : x^i ≥ 1, from pow_ge_one _ (le_of_lt xgt1), rewrite [pow_succ, -mul_one 1], apply mul_lt_mul xgt1 xige1 zero_lt_one, apply le_of_lt xpos end theorem squared_lt_squared {x y : A} (H1 : 0 ≤ x) (H2 : x < y) : x^2 < y^2 := by rewrite [*pow_two]; apply mul_self_lt_mul_self H1 H2 theorem squared_le_squared {x y : A} (H1 : 0 ≤ x) (H2 : x ≤ y) : x^2 ≤ y^2 := or.elim (lt_or_eq_of_le H2) (assume xlty, le_of_lt (squared_lt_squared H1 xlty)) (assume xeqy, by rewrite xeqy; apply le.refl) theorem lt_of_squared_lt_squared {x y : A} (H1 : y ≥ 0) (H2 : x^2 < y^2) : x < y := lt_of_not_ge (assume H : x ≥ y, not_le_of_gt H2 (squared_le_squared H1 H)) theorem le_of_squared_le_squared {x y : A} (H1 : y ≥ 0) (H2 : x^2 ≤ y^2) : x ≤ y := le_of_not_gt (assume H : x > y, not_lt_of_ge H2 (squared_lt_squared H1 H)) theorem eq_of_squared_eq_squared_of_nonneg {x y : A} (H1 : x ≥ 0) (H2 : y ≥ 0) (H3 : x^2 = y^2) : x = y := lt.by_cases (suppose x < y, absurd (eq.subst H3 (squared_lt_squared H1 this)) !lt.irrefl) (suppose x = y, this) (suppose x > y, absurd (eq.subst H3 (squared_lt_squared H2 this)) !lt.irrefl) end linear_ordered_semiring section decidable_linear_ordered_comm_ring variable [s : decidable_linear_ordered_comm_ring A] include s definition decidable_linear_ordered_comm_ring_has_pow_nat [reducible] [instance] : has_pow_nat A := monoid_has_pow_nat theorem abs_pow (a : A) (n : ℕ) : abs (a^n) = abs a^n := begin induction n with n ih, krewrite [*pow_zero, (abs_of_nonneg zero_le_one : abs (1 : A) = 1)], rewrite [*pow_succ, abs_mul, ih] end theorem squared_nonneg (x : A) : x^2 ≥ 0 := by rewrite [pow_two]; apply mul_self_nonneg theorem eq_zero_of_squared_eq_zero {x : A} (H : x^2 = 0) : x = 0 := by rewrite [pow_two at H]; exact eq_zero_of_mul_self_eq_zero H theorem abs_eq_abs_of_squared_eq_squared {x y : A} (H : x^2 = y^2) : abs x = abs y := have (abs x)^2 = (abs y)^2, by rewrite [-+abs_pow, H], eq_of_squared_eq_squared_of_nonneg (abs_nonneg x) (abs_nonneg y) this end decidable_linear_ordered_comm_ring section field variable [s : field A] include s theorem field.div_pow (a : A) {b : A} {n : ℕ} (bnz : b ≠ 0) : (a / b)^n = a^n / b^n := begin induction n with n ih, krewrite [*pow_zero, div_one], have bnnz : b^n ≠ 0, from division_ring.pow_ne_zero_of_ne_zero bnz, rewrite [*pow_succ, ih, !field.div_mul_div bnz bnnz] end end field section discrete_field variable [s : discrete_field A] include s theorem div_pow (a : A) {b : A} {n : ℕ} : (a / b)^n = a^n / b^n := begin induction n with n ih, krewrite [*pow_zero, div_one], rewrite [*pow_succ, ih, div_mul_div] end end discrete_field
00c024d4678dfe9a91c5f64257e1cc1222f85c0f
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/analysis/limits.lean
04e2f38e2835842c3ac85b33591e196c1ab836bd
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
9,770
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 limit properties. -/ import algebra.big_operators algebra.group_power tactic.norm_num analysis.real analysis.topology.infinite_sum noncomputable theory open classical finset function filter local attribute [instance] prop_decidable section real lemma has_sum_of_absolute_convergence {f : ℕ → ℝ} (hf : ∃r, tendsto (λn, (range n).sum (λi, abs (f i))) at_top (nhds r)) : has_sum f := let f' := λs:finset ℕ, s.sum (λi, abs (f i)) in suffices cauchy (map (λs:finset ℕ, s.sum f) at_top), from complete_space.complete this, cauchy_iff.mpr $ and.intro (map_ne_bot at_top_ne_bot) $ assume s hs, let ⟨ε, hε, hsε⟩ := mem_uniformity_dist.mp hs, ⟨r, hr⟩ := hf in have hε' : {p : ℝ × ℝ | dist p.1 p.2 < ε / 2} ∈ (@uniformity ℝ _).sets, from mem_uniformity_dist.mpr ⟨ε / 2, div_pos_of_pos_of_pos hε two_pos, assume a b h, h⟩, have cauchy (at_top.map $ λn, f' (range n)), from cauchy_downwards cauchy_nhds (map_ne_bot at_top_ne_bot) hr, have ∃n, ∀{n'}, n ≤ n' → dist (f' (range n)) (f' (range n')) < ε / 2, by simp [cauchy_iff, mem_at_top_sets] at this; from let ⟨t, ⟨u, hu⟩, ht⟩ := this _ hε' in ⟨u, assume n' hn, ht $ set.prod_mk_mem_set_prod_eq.mpr ⟨hu _ (le_refl _), hu _ hn⟩⟩, let ⟨n, hn⟩ := this in have ∀{s}, range n ⊆ s → abs ((s \ range n).sum f) < ε / 2, from assume s hs, let ⟨n', hn'⟩ := @exists_nat_subset_range s in have range n ⊆ range n', from finset.subset.trans hs hn', have f'_nn : 0 ≤ f' (range n' \ range n), from zero_le_sum $ assume _ _, abs_nonneg _, calc abs ((s \ range n).sum f) ≤ f' (s \ range n) : abs_sum_le_sum_abs ... ≤ f' (range n' \ range n) : sum_le_sum_of_subset_of_nonneg (finset.sdiff_subset_sdiff hn' (finset.subset.refl _)) (assume _ _ _, abs_nonneg _) ... = abs (f' (range n' \ range n)) : (abs_of_nonneg f'_nn).symm ... = abs (f' (range n') - f' (range n)) : by simp [f', (sum_sdiff ‹range n ⊆ range n'›).symm] ... = abs (f' (range n) - f' (range n')) : abs_sub _ _ ... < ε / 2 : hn $ range_subset.mp this, have ∀{s t}, range n ⊆ s → range n ⊆ t → dist (s.sum f) (t.sum f) < ε, from assume s t hs ht, calc abs (s.sum f - t.sum f) = abs ((s \ range n).sum f + - (t \ range n).sum f) : by rw [←sum_sdiff hs, ←sum_sdiff ht]; simp ... ≤ abs ((s \ range n).sum f) + abs ((t \ range n).sum f) : le_trans (abs_add_le_abs_add_abs _ _) $ by rw [abs_neg]; exact le_refl _ ... < ε / 2 + ε / 2 : add_lt_add (this hs) (this ht) ... = ε : by rw [←add_div, add_self_div_two], ⟨(λs:finset ℕ, s.sum f) '' {s | range n ⊆ s}, image_mem_map $ mem_at_top (range n), assume ⟨a, b⟩ ⟨⟨t, ht, ha⟩, ⟨s, hs, hb⟩⟩, by simp at ha hb; exact ha ▸ hb ▸ hsε (this ht hs)⟩ lemma is_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} {r : ℝ} (hf : ∀n, 0 ≤ f n) : is_sum f r ↔ tendsto (λn, (range n).sum f) at_top (nhds r) := ⟨tendsto_sum_nat_of_is_sum, assume hr, have tendsto (λn, (range n).sum (λn, abs (f n))) at_top (nhds r), by simp [(λi, abs_of_nonneg (hf i)), hr], let ⟨p, h⟩ := has_sum_of_absolute_convergence ⟨r, this⟩ in have hp : tendsto (λn, (range n).sum f) at_top (nhds p), from tendsto_sum_nat_of_is_sum h, have p = r, from tendsto_nhds_unique at_top_ne_bot hp hr, this ▸ h⟩ end real lemma mul_add_one_le_pow {r : ℝ} (hr : 0 ≤ r) : ∀{n:ℕ}, (n:ℝ) * r + 1 ≤ (r + 1) ^ n | 0 := by simp; exact le_refl 1 | (n + 1) := let h : (n:ℝ) ≥ 0 := nat.cast_nonneg n in calc ↑(n + 1) * r + 1 ≤ ((n + 1) * r + 1) + r * r * n : le_add_of_le_of_nonneg (le_refl _) (mul_nonneg (mul_nonneg hr hr) h) ... = (r + 1) * (n * r + 1) : by simp [mul_add, add_mul, mul_comm, mul_assoc] ... ≤ (r + 1) * (r + 1) ^ n : mul_le_mul (le_refl _) mul_add_one_le_pow (add_nonneg (mul_nonneg h hr) zero_le_one) (add_nonneg hr zero_le_one) lemma tendsto_pow_at_top_at_top_of_gt_1 {r : ℝ} (h : r > 1) : tendsto (λn:ℕ, r ^ n) at_top at_top := tendsto_infi.2 $ assume p, tendsto_principal.2 $ let ⟨n, hn⟩ := exists_nat_gt (p / (r - 1)) in have hn_nn : (0:ℝ) ≤ n, from nat.cast_nonneg n, have r - 1 > 0, from sub_lt_iff_lt_add.mp $ by simp; assumption, have p ≤ r ^ n, from calc p = (p / (r - 1)) * (r - 1) : (div_mul_cancel _ $ ne_of_gt this).symm ... ≤ n * (r - 1) : mul_le_mul (le_of_lt hn) (le_refl _) (le_of_lt this) hn_nn ... ≤ n * (r - 1) + 1 : le_add_of_le_of_nonneg (le_refl _) zero_le_one ... ≤ ((r - 1) + 1) ^ n : mul_add_one_le_pow $ le_of_lt this ... ≤ r ^ n : by simp; exact le_refl _, show {n | p ≤ r ^ n} ∈ at_top.sets, from mem_at_top_sets.mpr ⟨n, assume m hnm, le_trans this (pow_le_pow (le_of_lt h) hnm)⟩ lemma tendsto_inverse_at_top_nhds_0 : tendsto (λr:ℝ, r⁻¹) at_top (nhds 0) := tendsto_orderable_unbounded (no_top 0) (no_bot 0) $ assume l u hl hu, mem_at_top_sets.mpr ⟨u⁻¹ + 1, assume b hb, have u⁻¹ < b, from lt_of_lt_of_le (lt_add_of_pos_right _ zero_lt_one) hb, ⟨lt_trans hl $ inv_pos $ lt_trans (inv_pos hu) this, lt_of_one_div_lt_one_div hu $ begin rw [inv_eq_one_div], simp [-one_div_eq_inv, div_div_eq_mul_div, div_one], simp [this] end⟩⟩ lemma map_succ_at_top_eq : map nat.succ at_top = at_top := le_antisymm (assume s hs, let ⟨b, hb⟩ := mem_at_top_sets.mp hs in mem_at_top_sets.mpr ⟨b, assume c hc, hb (c + 1) $ le_trans hc $ nat.le_succ _⟩) (assume s hs, let ⟨b, hb⟩ := mem_at_top_sets.mp hs in mem_at_top_sets.mpr ⟨b + 1, assume c, match c with | 0 := assume h, have 0 > 0, from lt_of_lt_of_le (lt_add_of_le_of_pos (nat.zero_le _) zero_lt_one) h, (lt_irrefl 0 this).elim | (c+1) := assume h, hb _ (nat.le_of_succ_le_succ h) end⟩) lemma tendsto_comp_succ_at_top_iff {α : Type*} {f : ℕ → α} {x : filter α} : tendsto (λn, f (nat.succ n)) at_top x ↔ tendsto f at_top x := calc tendsto (f ∘ nat.succ) at_top x ↔ tendsto f (map nat.succ at_top) x : by simp [tendsto, filter.map_map] ... ↔ _ : by rw [map_succ_at_top_eq] lemma tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (nhds 0) := by_cases (assume : r = 0, tendsto_comp_succ_at_top_iff.mp $ by simp [pow_succ, this, tendsto_const_nhds]) (assume : r ≠ 0, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (nhds 0), from (tendsto_pow_at_top_at_top_of_gt_1 $ one_lt_inv (lt_of_le_of_ne h₁ this.symm) h₂).comp tendsto_inverse_at_top_nhds_0, tendsto_cong this $ univ_mem_sets' $ by simp *) lemma sum_geometric' {r : ℝ} (h : r ≠ 0) : ∀{n}, (finset.range n).sum (λi, (r + 1) ^ i) = ((r + 1) ^ n - 1) / r | 0 := by simp [zero_div] | (n+1) := by simp [@sum_geometric' n, h, pow_succ, add_div_eq_mul_add_div, add_mul, mul_comm, mul_assoc] lemma sum_geometric {r : ℝ} {n : ℕ} (h : r ≠ 1) : (range n).sum (λi, r ^ i) = (r ^ n - 1) / (r - 1) := calc (range n).sum (λi, r ^ i) = (range n).sum (λi, ((r - 1) + 1) ^ i) : by simp ... = (((r - 1) + 1) ^ n - 1) / (r - 1) : sum_geometric' $ by simp [sub_eq_iff_eq_add, -sub_eq_add_neg, h] ... = (r ^ n - 1) / (r - 1) : by simp lemma is_sum_geometric {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : is_sum (λn:ℕ, r ^ n) (1 / (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 (nhds ((0 - 1) * (r - 1)⁻¹)), from tendsto_mul (tendsto_sub (tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂) tendsto_const_nhds) tendsto_const_nhds, (is_sum_iff_tendsto_nat_of_nonneg $ pow_nonneg h₁).mpr $ by simp [neg_inv, sum_geometric, div_eq_mul_inv, *] at * lemma is_sum_geometric_two (a : ℝ) : is_sum (λn:ℕ, (a / 2) / 2 ^ n) a := begin convert is_sum_mul_left (a / 2) (is_sum_geometric (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, rw ← pow_inv; [refl, exact two_ne_zero] }, { norm_num, rw div_mul_cancel _ two_ne_zero } end def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε) (ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, is_sum ε' c ∧ c ≤ ε} := begin let f := λ n, (ε / 2) / 2 ^ n, have hf : is_sum f ε := is_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 _, _⟩, let g : ℕ → ℝ := λ n, option.cases_on (encodable.decode2 ι n) 0 (f ∘ encodable.encode), have : ∀ n, g n = 0 ∨ g n = f n, { intro n, dsimp [g], cases e : encodable.decode2 ι n with a, { exact or.inl rfl }, { simp [encodable.mem_decode2.1 e] } }, cases has_sum_of_has_sum_of_sub ⟨_, hf⟩ this with c hg, have cε : c ≤ ε, { refine is_sum_le (λ n, _) hg hf, cases this n; rw h, exact le_of_lt (f0 _) }, have hs : ∀ n, g n ≠ 0 → (encodable.decode2 ι n).is_some, { intros n h, dsimp [g] at h, cases encodable.decode2 ι n, exact (h rfl).elim, exact rfl }, refine ⟨c, _, cε⟩, refine is_sum_of_is_sum_ne_zero (λ n h, option.get (hs n h)) (λ n _, ne_of_gt (f0 _)) (λ i _, encodable.encode i) (λ n h, ne_of_gt _) (λ n h, _) (λ i _, _) (λ i _, _) hg, { dsimp [g], rw encodable.encodek2, exact f0 _ }, { exact encodable.mem_decode2.1 (option.get_mem _) }, { exact option.get_of_mem _ (encodable.encodek2 _) }, { dsimp [g], rw encodable.encodek2 } end
2e1835bdbe15c4b77c5b327d0f7688d9e89fd9a0
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/sheaves/forget.lean
3bafd8544aaaa9d5aeb0fb10c653532758e4c53f
[ "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
8,022
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.preserves.shapes.products import topology.sheaves.sheaf /-! # Checking the sheaf condition on the underlying presheaf of types. If `G : C ⥤ D` is a functor which reflects isomorphisms and preserves limits (we assume all limits exist in both `C` and `D`), then checking the sheaf condition for a presheaf `F : presheaf C X` is equivalent to checking the sheaf condition for `F ⋙ G`. The important special case is when `C` is a concrete category with a forgetful functor that preserves limits and reflects isomorphisms. Then to check the sheaf condition it suffices to check it on the underlying sheaf of types. ## References * https://stacks.math.columbia.edu/tag/0073 -/ noncomputable theory open category_theory open category_theory.limits open topological_space open opposite namespace Top namespace presheaf namespace sheaf_condition open sheaf_condition_equalizer_products universes v u₁ u₂ variables {C : Type u₁} [category.{v} C] [has_limits C] variables {D : Type u₂} [category.{v} D] [has_limits D] variables (G : C ⥤ D) [preserves_limits G] variables {X : Top.{v}} (F : presheaf C X) variables {ι : Type v} (U : ι → opens X) local attribute [reducible] diagram left_res right_res /-- When `G` preserves limits, the sheaf condition diagram for `F` composed with `G` is naturally isomorphic to the sheaf condition diagram for `F ⋙ G`. -/ def diagram_comp_preserves_limits : diagram F U ⋙ G ≅ diagram (F ⋙ G) U := begin fapply nat_iso.of_components, rintro ⟨j⟩, exact (preserves_product.iso _ _), exact (preserves_product.iso _ _), rintros ⟨⟩ ⟨⟩ ⟨⟩, { ext, simp, dsimp, simp, }, -- non-terminal `simp`, but `squeeze_simp` fails { ext, simp only [limit.lift_π, functor.comp_map, map_lift_pi_comparison, fan.mk_π_app, preserves_product.iso_hom, parallel_pair_map_left, functor.map_comp, category.assoc], dsimp, simp, }, { ext, simp only [limit.lift_π, functor.comp_map, parallel_pair_map_right, fan.mk_π_app, preserves_product.iso_hom, map_lift_pi_comparison, functor.map_comp, category.assoc], dsimp, simp, }, { ext, simp, dsimp, simp, }, end local attribute [reducible] res /-- When `G` preserves limits, the image under `G` of the sheaf condition fork for `F` is the sheaf condition fork for `F ⋙ G`, postcomposed with the inverse of the natural isomorphism `diagram_comp_preserves_limits`. -/ def map_cone_fork : G.map_cone (fork F U) ≅ (cones.postcompose (diagram_comp_preserves_limits G F U).inv).obj (fork (F ⋙ G) U) := cones.ext (iso.refl _) (λ j, begin dsimp, simp [diagram_comp_preserves_limits], cases j; dsimp, { rw iso.eq_comp_inv, ext, simp, dsimp, simp, }, { rw iso.eq_comp_inv, ext, simp, -- non-terminal `simp`, but `squeeze_simp` fails dsimp, simp only [limit.lift_π, fan.mk_π_app, ←G.map_comp, limit.lift_π_assoc, fan.mk_π_app] } end) end sheaf_condition universes v u₁ u₂ open sheaf_condition sheaf_condition_equalizer_products variables {C : Type u₁} [category.{v} C] {D : Type u₂} [category.{v} D] variables (G : C ⥤ D) variables [reflects_isomorphisms G] variables [has_limits C] [has_limits D] [preserves_limits G] variables {X : Top.{v}} (F : presheaf C X) /-- If `G : C ⥤ D` is a functor which reflects isomorphisms and preserves limits (we assume all limits exist in both `C` and `D`), then checking the sheaf condition for a presheaf `F : presheaf C X` is equivalent to checking the sheaf condition for `F ⋙ G`. The important special case is when `C` is a concrete category with a forgetful functor that preserves limits and reflects isomorphisms. Then to check the sheaf condition it suffices to check it on the underlying sheaf of types. Another useful example is the forgetful functor `TopCommRing ⥤ Top`. See https://stacks.math.columbia.edu/tag/0073. In fact we prove a stronger version with arbitrary complete target category. -/ lemma is_sheaf_iff_is_sheaf_comp : presheaf.is_sheaf F ↔ presheaf.is_sheaf (F ⋙ G) := begin split, { intros S ι U, -- We have that the sheaf condition fork for `F` is a limit fork, obtain ⟨t₁⟩ := S U, -- and since `G` preserves limits, the image under `G` of this fork is a limit fork too. have t₂ := @preserves_limit.preserves _ _ _ _ _ _ _ G _ _ t₁, -- As we established above, that image is just the sheaf condition fork -- for `F ⋙ G` postcomposed with some natural isomorphism, have t₃ := is_limit.of_iso_limit t₂ (map_cone_fork G F U), -- and as postcomposing by a natural isomorphism preserves limit cones, have t₄ := is_limit.postcompose_inv_equiv _ _ t₃, -- we have our desired conclusion. exact ⟨t₄⟩, }, { intros S ι U, refine ⟨_⟩, -- Let `f` be the universal morphism from `F.obj U` to the equalizer -- of the sheaf condition fork, whatever it is. -- Our goal is to show that this is an isomorphism. let f := equalizer.lift _ (w F U), -- If we can do that, suffices : is_iso (G.map f), { resetI, -- we have that `f` itself is an isomorphism, since `G` reflects isomorphisms haveI : is_iso f := is_iso_of_reflects_iso f G, -- TODO package this up as a result elsewhere: apply is_limit.of_iso_limit (limit.is_limit _), apply iso.symm, fapply cones.ext, exact (as_iso f), rintro ⟨_|_⟩; { dsimp [f], simp, }, }, { -- Returning to the task of shwoing that `G.map f` is an isomorphism, -- we note that `G.map f` is almost but not quite (see below) a morphism -- from the sheaf condition cone for `F ⋙ G` to the -- image under `G` of the equalizer cone for the sheaf condition diagram. let c := fork (F ⋙ G) U, obtain ⟨hc⟩ := S U, let d := G.map_cone (equalizer.fork (left_res F U) (right_res F U)), have hd : is_limit d := preserves_limit.preserves (limit.is_limit _), -- Since both of these are limit cones -- (`c` by our hypothesis `S`, and `d` because `G` preserves limits), -- we hope to be able to conclude that `f` is an isomorphism. -- We say "not quite" above because `c` and `d` don't quite have the same shape: -- we need to postcompose by the natural isomorphism `diagram_comp_preserves_limits` -- introduced above. let d' := (cones.postcompose (diagram_comp_preserves_limits G F U).hom).obj d, have hd' : is_limit d' := (is_limit.postcompose_hom_equiv (diagram_comp_preserves_limits G F U : _) d).symm hd, -- Now everything works: we verify that `f` really is a morphism between these cones: let f' : c ⟶ d' := fork.mk_hom (G.map f) begin dsimp only [c, d, d', f, diagram_comp_preserves_limits, res], dunfold fork.ι, ext1 j, dsimp, simp only [category.assoc, ←functor.map_comp_assoc, equalizer.lift_ι, map_lift_pi_comparison_assoc], dsimp [res], simp, end, -- conclude that it is an isomorphism, -- just because it's a morphism between two limit cones. haveI : is_iso f' := is_limit.hom_is_iso hc hd' f', -- A cone morphism is an isomorphism exactly if the morphism between the cone points is, -- so we're done! exact is_iso.of_iso ((cones.forget _).map_iso (as_iso f')) }, }, end /-! As an example, we now have everything we need to check the sheaf condition for a presheaf of commutative rings, merely by checking the sheaf condition for the underlying sheaf of types. ``` import algebra.category.CommRing.limits example (X : Top) (F : presheaf CommRing X) (h : presheaf.is_sheaf (F ⋙ (forget CommRing))) : F.is_sheaf := (is_sheaf_iff_is_sheaf_comp (forget CommRing) F).mpr h ``` -/ end presheaf end Top
b7212dcec1c0e1dcd2e4e65487aead378576bcbe
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/tests/lean/run/record2.lean
7636e65f6bcbef4796eb6b0ac17ed3956a2749e9
[ "Apache-2.0" ]
permissive
respu/lean
6582d19a2f2838a28ecd2b3c6f81c32d07b5341d
8c76419c60b63d0d9f7bc04ebb0b99812d0ec654
refs/heads/master
1,610,882,451,231
1,427,747,084,000
1,427,747,429,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
283
lean
import logic data.unit set_option pp.universes true context parameter (A : Type) context parameter (B : Type) structure point := mk :: (x : A) (y : B) check point check point.mk check point.x end check point check point.mk check point.x end
47de476e08a52f6b6dc929daf33ea9e7eff5f480
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/src/Lean/Elab/Tactic/Simp.lean
280f5eb017b4c8615e79b2c4d5c4ab33df3314bc
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
10,158
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.Tactic.Simp import Lean.Meta.Tactic.Replace import Lean.Elab.BuiltinNotation import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.ElabTerm import Lean.Elab.Tactic.Location import Lean.Elab.Tactic.Config namespace Lean.Elab.Tactic open Meta declare_config_elab elabSimpConfigCore Meta.Simp.Config declare_config_elab elabSimpConfigCtxCore Meta.Simp.ConfigCtx /-- Implement a `simp` discharge function using the given tactic syntax code. Recall that `simp` dischargers are in `SimpM` which does not have access to `Term.State`. We need access to `Term.State` to store messages and update the info tree. Thus, we create an `IO.ref` to track these changes at `Term.State` when we execute `tacticCode`. We must set this reference with the current `Term.State` before we execute `simp` using the generated `Simp.Discharge`. -/ def tacticToDischarge (tacticCode : Syntax) : TacticM (IO.Ref Term.State × Simp.Discharge) := do let tacticCode ← `(tactic| try ($tacticCode:tacticSeq)) let ref ← IO.mkRef (← getThe Term.State) let ctx ← readThe Term.Context let disch : Simp.Discharge := fun e => do let mvar ← mkFreshExprSyntheticOpaqueMVar e `simp.discharger let s ← ref.get let runTac? : TermElabM (Option Expr) := try /- We must only save messages and info tree changes. Recall that `simp` uses temporary metavariables (`withNewMCtxDepth`). So, we must not save references to them at `Term.State`. -/ withoutModifyingStateWithInfoAndMessages do Term.withSynthesize (mayPostpone := false) <| Term.runTactic mvar.mvarId! tacticCode let result ← instantiateMVars mvar if result.hasExprMVar then return none else return some result catch _ => return none let (result?, s) ← liftM (m := MetaM) <| Term.TermElabM.run runTac? ctx s ref.set s return result? return (ref, disch) inductive Simp.DischargeWrapper where | default | custom (ref : IO.Ref Term.State) (discharge : Simp.Discharge) def Simp.DischargeWrapper.with (w : Simp.DischargeWrapper) (x : Option Simp.Discharge → MetaM α) : TacticM α := do match w with | default => x none | custom ref d => ref.set (← getThe Term.State) try x d finally set (← ref.get) private def mkDischargeWrapper (optDischargeSyntax : Syntax) : TacticM Simp.DischargeWrapper := do if optDischargeSyntax.isNone then return Simp.DischargeWrapper.default else let (ref, d) ← tacticToDischarge optDischargeSyntax[0][3] return Simp.DischargeWrapper.custom ref d /- `optConfig` is of the form `("(" "config" ":=" term ")")?` If `ctx == false`, the argument is assumed to have type `Meta.Simp.Config`, and `Meta.Simp.ConfigCtx` otherwise. -/ def elabSimpConfig (optConfig : Syntax) (ctx : Bool) : TermElabM Meta.Simp.Config := do if ctx then return (← elabSimpConfigCtxCore optConfig).toConfig else elabSimpConfigCore optConfig private def addDeclToUnfoldOrLemma (lemmas : Meta.SimpLemmas) (e : Expr) (post : Bool) (inv : Bool) : MetaM Meta.SimpLemmas := do if e.isConst then let declName := e.constName! let info ← getConstInfo declName if (← isProp info.type) then lemmas.addConst declName (post := post) (inv := inv) else if inv then throwError "invalid '←' modifier, '{declName}' is a declaration name to be unfolded" lemmas.addDeclToUnfold declName else lemmas.add #[] e (post := post) (inv := inv) private def addSimpLemma (lemmas : Meta.SimpLemmas) (stx : Syntax) (post : Bool) (inv : Bool) : TermElabM Meta.SimpLemmas := do let (levelParams, proof) ← Term.withoutModifyingElabMetaStateWithInfo <| withRef stx <| Term.withoutErrToSorry do let e ← Term.elabTerm stx none Term.synthesizeSyntheticMVars (mayPostpone := false) (ignoreStuckTC := true) let e ← instantiateMVars e let e := e.eta if e.hasMVar then let r ← abstractMVars e return (r.paramNames, r.expr) else return (#[], e) lemmas.add levelParams proof (post := post) (inv := inv) structure ElabSimpArgsResult where ctx : Simp.Context starArg : Bool := false /-- Elaborate extra simp lemmas provided to `simp`. `stx` is of the `simpLemma,*` If `eraseLocal == true`, then we consider local declarations when resolving names for erased lemmas (`- id`), this option only makes sense for `simp_all`. -/ private def elabSimpArgs (stx : Syntax) (ctx : Simp.Context) (eraseLocal : Bool) : TacticM ElabSimpArgsResult := do if stx.isNone then return { ctx } else /- syntax simpPre := "↓" syntax simpPost := "↑" syntax simpLemma := (simpPre <|> simpPost)? term syntax simpErase := "-" ident -/ withMainContext do let mut lemmas := ctx.simpLemmas let mut starArg := false for arg in stx[1].getSepArgs do if arg.getKind == ``Lean.Parser.Tactic.simpErase then if eraseLocal && (← Term.isLocalIdent? arg[1]).isSome then -- We use `eraseCore` because the simp lemma for the hypothesis was not added yet lemmas ← lemmas.eraseCore arg[1].getId else let declName ← resolveGlobalConstNoOverloadWithInfo arg[1] lemmas ← lemmas.erase declName else if arg.getKind == ``Lean.Parser.Tactic.simpLemma then let post := if arg[0].isNone then true else arg[0][0].getKind == ``Parser.Tactic.simpPost let inv := !arg[1].isNone let term := arg[2] match (← resolveSimpIdLemma? term) with | some e => lemmas ← addDeclToUnfoldOrLemma lemmas e post inv | _ => lemmas ← addSimpLemma lemmas term post inv else if arg.getKind == ``Lean.Parser.Tactic.simpStar then starArg := true else throwUnsupportedSyntax return { ctx := { ctx with simpLemmas := lemmas }, starArg } where resolveSimpIdLemma? (simpArgTerm : Syntax) : TacticM (Option Expr) := do if simpArgTerm.isIdent then try Term.resolveId? simpArgTerm (withInfo := true) catch _ => return none else Term.elabCDotFunctionAlias? simpArgTerm abbrev FVarIdToLemmaId := FVarIdMap Name -- TODO: move? private def getPropHyps : MetaM (Array FVarId) := do let mut result := #[] for localDecl in (← getLCtx) do unless localDecl.isAuxDecl do if (← isProp localDecl.type) then result := result.push localDecl.fvarId return result structure MkSimpContextResult where ctx : Simp.Context dischargeWrapper : Simp.DischargeWrapper fvarIdToLemmaId : FVarIdToLemmaId /-- If `ctx == false`, the config argument is assumed to have type `Meta.Simp.Config`, and `Meta.Simp.ConfigCtx` otherwise. If `ctx == false`, the `discharge` option must be none -/ def mkSimpContext (stx : Syntax) (eraseLocal : Bool) (ctx := false) (ignoreStarArg : Bool := false) : TacticM MkSimpContextResult := do if ctx && !stx[2].isNone then throwError "'simp_all' tactic does not support 'discharger' option" let dischargeWrapper ← mkDischargeWrapper stx[2] let simpOnly := !stx[3].isNone let simpLemmas ← if simpOnly then ({} : SimpLemmas).addConst ``eq_self else getSimpLemmas let congrLemmas ← getCongrLemmas let r ← elabSimpArgs stx[4] (eraseLocal := eraseLocal) { config := (← elabSimpConfig stx[1] (ctx := ctx)) simpLemmas, congrLemmas } if !r.starArg || ignoreStarArg then return { r with fvarIdToLemmaId := {}, dischargeWrapper } else let ctx := r.ctx let erased := ctx.simpLemmas.erased let hs ← getPropHyps let mut ctx := ctx let mut fvarIdToLemmaId := {} for h in hs do let localDecl ← getLocalDecl h unless erased.contains localDecl.userName do let fvarId := localDecl.fvarId let proof := localDecl.toExpr let id ← mkFreshUserName `h fvarIdToLemmaId := fvarIdToLemmaId.insert fvarId id let simpLemmas ← ctx.simpLemmas.add #[] proof (name? := id) ctx := { ctx with simpLemmas } return { ctx, fvarIdToLemmaId, dischargeWrapper } /- "simp " (config)? (discharger)? ("only ")? ("[" simpLemma,* "]")? (location)? -/ @[builtinTactic Lean.Parser.Tactic.simp] def evalSimp : Tactic := fun stx => do let { ctx, fvarIdToLemmaId, dischargeWrapper } ← withMainContext <| mkSimpContext stx (eraseLocal := false) -- trace[Meta.debug] "Lemmas {← toMessageData ctx.simpLemmas.post}" let loc := expandOptLocation stx[5] match loc with | Location.targets hUserNames simplifyTarget => withMainContext do let fvarIds ← hUserNames.mapM fun hUserName => return (← getLocalDeclFromUserName hUserName).fvarId go ctx dischargeWrapper fvarIds simplifyTarget fvarIdToLemmaId | Location.wildcard => withMainContext do go ctx dischargeWrapper (← getNondepPropHyps (← getMainGoal)) (simplifyTarget := true) fvarIdToLemmaId where go (ctx : Simp.Context) (dischargeWrapper : Simp.DischargeWrapper) (fvarIdsToSimp : Array FVarId) (simplifyTarget : Bool) (fvarIdToLemmaId : FVarIdToLemmaId) : TacticM Unit := do let mvarId ← getMainGoal let result? ← dischargeWrapper.with fun discharge? => return (← simpGoal mvarId ctx (simplifyTarget := simplifyTarget) (discharge? := discharge?) (fvarIdsToSimp := fvarIdsToSimp) (fvarIdToLemmaId := fvarIdToLemmaId)).map (·.2) match result? with | none => replaceMainGoal [] | some mvarId => replaceMainGoal [mvarId] @[builtinTactic Lean.Parser.Tactic.simpAll] def evalSimpAll : Tactic := fun stx => do let { ctx, .. } ← mkSimpContext stx (eraseLocal := true) (ctx := true) (ignoreStarArg := true) match (← simpAll (← getMainGoal) ctx) with | none => replaceMainGoal [] | some mvarId => replaceMainGoal [mvarId] end Lean.Elab.Tactic
d85c21ee11dfd2cd6a9642ad11e43d0c0770bcea
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/module/linear_map.lean
96457fd912eb800c7a3f8f1021a94052ca06a1e4
[ "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
37,991
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro, Anne Baanen, Frédéric Dupuis, Heather Macbeth -/ import algebra.hom.group_action import algebra.module.pi import algebra.star.basic import data.set.pointwise.smul import algebra.ring.comp_typeclasses /-! # (Semi)linear maps In this file we define * `linear_map σ M M₂`, `M →ₛₗ[σ] M₂` : a semilinear map between two `module`s. Here, `σ` is a `ring_hom` from `R` to `R₂` and an `f : M →ₛₗ[σ] M₂` satisfies `f (c • x) = (σ c) • (f x)`. We recover plain linear maps by choosing `σ` to be `ring_hom.id R`. This is denoted by `M →ₗ[R] M₂`. We also add the notation `M →ₗ⋆[R] M₂` for star-linear maps. * `is_linear_map R f` : predicate saying that `f : M → M₂` is a linear map. (Note that this was not generalized to semilinear maps.) We then provide `linear_map` with the following instances: * `linear_map.add_comm_monoid` and `linear_map.add_comm_group`: the elementwise addition structures corresponding to addition in the codomain * `linear_map.distrib_mul_action` and `linear_map.module`: the elementwise scalar action structures corresponding to applying the action in the codomain. * `module.End.semiring` and `module.End.ring`: the (semi)ring of endomorphisms formed by taking the additive structure above with composition as multiplication. ## Implementation notes To ensure that composition works smoothly for semilinear maps, we use the typeclasses `ring_hom_comp_triple`, `ring_hom_inv_pair` and `ring_hom_surjective` from `algebra/ring/comp_typeclasses`. ## Notation * Throughout the file, we denote regular linear maps by `fₗ`, `gₗ`, etc, and semilinear maps by `f`, `g`, etc. ## TODO * Parts of this file have not yet been generalized to semilinear maps (i.e. `compatible_smul`) ## Tags linear map -/ open function open_locale big_operators universes u u' v w x y z variables {R : Type*} {R₁ : Type*} {R₂ : Type*} {R₃ : Type*} variables {k : Type*} {S : Type*} {S₃ : Type*} {T : Type*} variables {M : Type*} {M₁ : Type*} {M₂ : Type*} {M₃ : Type*} variables {N₁ : Type*} {N₂ : Type*} {N₃ : Type*} {ι : Type*} /-- A map `f` between modules over a semiring is linear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = c • f x`. The predicate `is_linear_map R f` asserts this property. A bundled version is available with `linear_map`, and should be favored over `is_linear_map` most of the time. -/ structure is_linear_map (R : Type u) {M : Type v} {M₂ : Type w} [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] (f : M → M₂) : Prop := (map_add : ∀ x y, f (x + y) = f x + f y) (map_smul : ∀ (c : R) x, f (c • x) = c • f x) section set_option old_structure_cmd true /-- A map `f` between an `R`-module and an `S`-module over a ring homomorphism `σ : R →+* S` is semilinear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = (σ c) • f x`. Elements of `linear_map σ M M₂` (available under the notation `M →ₛₗ[σ] M₂`) are bundled versions of such maps. For plain linear maps (i.e. for which `σ = ring_hom.id R`), the notation `M →ₗ[R] M₂` is available. An unbundled version of plain linear maps is available with the predicate `is_linear_map`, but it should be avoided most of the time. -/ structure linear_map {R : Type*} {S : Type*} [semiring R] [semiring S] (σ : R →+* S) (M : Type*) (M₂ : Type*) [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module S M₂] extends add_hom M M₂ := (map_smul' : ∀ (r : R) (x : M), to_fun (r • x) = (σ r) • to_fun x) /-- The `add_hom` underlying a `linear_map`. -/ add_decl_doc linear_map.to_add_hom notation M ` →ₛₗ[`:25 σ:25 `] `:0 M₂:0 := linear_map σ M M₂ notation M ` →ₗ[`:25 R:25 `] `:0 M₂:0 := linear_map (ring_hom.id R) M M₂ notation M ` →ₗ⋆[`:25 R:25 `] `:0 M₂:0 := linear_map (star_ring_end R) M M₂ /-- `semilinear_map_class F σ M M₂` asserts `F` is a type of bundled `σ`-semilinear maps `M → M₂`. See also `linear_map_class F R M M₂` for the case where `σ` is the identity map on `R`. A map `f` between an `R`-module and an `S`-module over a ring homomorphism `σ : R →+* S` is semilinear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = (σ c) • f x`. -/ class semilinear_map_class (F : Type*) {R S : out_param Type*} [semiring R] [semiring S] (σ : out_param $ R →+* S) (M M₂ : out_param Type*) [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module S M₂] extends add_hom_class F M M₂ := (map_smulₛₗ : ∀ (f : F) (r : R) (x : M), f (r • x) = (σ r) • f x) end -- `σ` becomes a metavariable but that's fine because it's an `out_param` attribute [nolint dangerous_instance] semilinear_map_class.to_add_hom_class export semilinear_map_class (map_smulₛₗ) attribute [simp] map_smulₛₗ /-- `linear_map_class F R M M₂` asserts `F` is a type of bundled `R`-linear maps `M → M₂`. This is an abbreviation for `semilinear_map_class F (ring_hom.id R) M M₂`. -/ abbreviation linear_map_class (F : Type*) (R M M₂ : out_param Type*) [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] := semilinear_map_class F (ring_hom.id R) M M₂ namespace semilinear_map_class variables (F : Type*) variables [semiring R] [semiring S] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [add_comm_monoid N₁] [add_comm_monoid N₂] [add_comm_monoid N₃] variables [module R M] [module R M₂] [module S M₃] variables {σ : R →+* S} @[priority 100, nolint dangerous_instance] -- `σ` is an `out_param` so it's not dangerous instance [semilinear_map_class F σ M M₃] : add_monoid_hom_class F M M₃ := { coe := λ f, (f : M → M₃), map_zero := λ f, show f 0 = 0, by { rw [← zero_smul R (0 : M), map_smulₛₗ], simp }, .. semilinear_map_class.to_add_hom_class F σ M M₃ } @[priority 100, nolint dangerous_instance] -- `R` is an `out_param` so it's not dangerous instance [linear_map_class F R M M₂] : distrib_mul_action_hom_class F R M M₂ := { coe := λ f, (f : M → M₂), map_smul := λ f c x, by rw [map_smulₛₗ, ring_hom.id_apply], .. semilinear_map_class.add_monoid_hom_class F } variables {F} (f : F) [i : semilinear_map_class F σ M M₃] include i lemma map_smul_inv {σ' : S →+* R} [ring_hom_inv_pair σ σ'] (c : S) (x : M) : c • f x = f (σ' c • x) := by simp end semilinear_map_class namespace linear_map section add_comm_monoid variables [semiring R] [semiring S] section variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [add_comm_monoid N₁] [add_comm_monoid N₂] [add_comm_monoid N₃] variables [module R M] [module R M₂] [module S M₃] variables {σ : R →+* S} instance : semilinear_map_class (M →ₛₗ[σ] M₃) σ M M₃ := { coe := linear_map.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_add := linear_map.map_add', map_smulₛₗ := linear_map.map_smul' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (M →ₛₗ[σ] M₃) (λ _, M → M₃) := ⟨λ f, f⟩ /-- The `distrib_mul_action_hom` underlying a `linear_map`. -/ def to_distrib_mul_action_hom (f : M →ₗ[R] M₂) : distrib_mul_action_hom R M M₂ := { map_zero' := show f 0 = 0, from map_zero f, ..f } @[simp] lemma to_fun_eq_coe {f : M →ₛₗ[σ] M₃} : f.to_fun = (f : M → M₃) := rfl @[ext] theorem ext {f g : M →ₛₗ[σ] M₃} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h /-- Copy of a `linear_map` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : M →ₛₗ[σ] M₃) (f' : M → M₃) (h : f' = ⇑f) : M →ₛₗ[σ] M₃ := { to_fun := f', map_add' := h.symm ▸ f.map_add', map_smul' := h.symm ▸ f.map_smul' } @[simp] lemma coe_copy (f : M →ₛₗ[σ] M₃) (f' : M → M₃) (h : f' = ⇑f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : M →ₛₗ[σ] M₃) (f' : M → M₃) (h : f' = ⇑f) : f.copy f' h = f := fun_like.ext' h /-- See Note [custom simps projection]. -/ protected def simps.apply {R S : Type*} [semiring R] [semiring S] (σ : R →+* S) (M M₃ : Type*) [add_comm_monoid M] [add_comm_monoid M₃] [module R M] [module S M₃] (f : M →ₛₗ[σ] M₃) : M → M₃ := f initialize_simps_projections linear_map (to_fun → apply) @[simp] lemma coe_mk {σ : R →+* S} (f : M → M₃) (h₁ h₂) : ((linear_map.mk f h₁ h₂ : M →ₛₗ[σ] M₃) : M → M₃) = f := rfl /-- Identity map as a `linear_map` -/ def id : M →ₗ[R] M := { to_fun := id, ..distrib_mul_action_hom.id R } lemma id_apply (x : M) : @id R M _ _ _ x = x := rfl @[simp, norm_cast] lemma id_coe : ((linear_map.id : M →ₗ[R] M) : M → M) = _root_.id := rfl end section variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [add_comm_monoid N₁] [add_comm_monoid N₂] [add_comm_monoid N₃] variables [module R M] [module R M₂] [module S M₃] variables (σ : R →+* S) variables (fₗ gₗ : M →ₗ[R] M₂) (f g : M →ₛₗ[σ] M₃) theorem is_linear : is_linear_map R fₗ := ⟨fₗ.map_add', fₗ.map_smul'⟩ variables {fₗ gₗ f g σ} theorem coe_injective : @injective (M →ₛₗ[σ] M₃) (M → M₃) coe_fn := fun_like.coe_injective protected lemma congr_arg {x x' : M} : x = x' → f x = f x' := fun_like.congr_arg f /-- If two linear maps are equal, they are equal at each point. -/ protected lemma congr_fun (h : f = g) (x : M) : f x = g x := fun_like.congr_fun h x theorem ext_iff : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff @[simp] lemma mk_coe (f : M →ₛₗ[σ] M₃) (h₁ h₂) : (linear_map.mk f h₁ h₂ : M →ₛₗ[σ] M₃) = f := ext $ λ _, rfl variables (fₗ gₗ f g) protected lemma map_add (x y : M) : f (x + y) = f x + f y := map_add f x y protected lemma map_zero : f 0 = 0 := map_zero f -- TODO: `simp` isn't picking up `map_smulₛₗ` for `linear_map`s without specifying `map_smulₛₗ f` @[simp] protected lemma map_smulₛₗ (c : R) (x : M) : f (c • x) = (σ c) • f x := map_smulₛₗ f c x protected lemma map_smul (c : R) (x : M) : fₗ (c • x) = c • fₗ x := map_smul fₗ c x protected lemma map_smul_inv {σ' : S →+* R} [ring_hom_inv_pair σ σ'] (c : S) (x : M) : c • f x = f (σ' c • x) := by simp -- TODO: generalize to `zero_hom_class` @[simp] lemma map_eq_zero_iff (h : function.injective f) {x : M} : f x = 0 ↔ x = 0 := ⟨λ w, by { apply h, simp [w], }, λ w, by { subst w, simp, }⟩ section pointwise open_locale pointwise variables (M M₃ σ) {F : Type*} (h : F) @[simp] lemma _root_.image_smul_setₛₗ [semilinear_map_class F σ M M₃] (c : R) (s : set M) : h '' (c • s) = (σ c) • h '' s := begin apply set.subset.antisymm, { rintros x ⟨y, ⟨z, zs, rfl⟩, rfl⟩, exact ⟨h z, set.mem_image_of_mem _ zs, (map_smulₛₗ _ _ _).symm ⟩ }, { rintros x ⟨y, ⟨z, hz, rfl⟩, rfl⟩, exact (set.mem_image _ _ _).2 ⟨c • z, set.smul_mem_smul_set hz, map_smulₛₗ _ _ _⟩ } end lemma _root_.preimage_smul_setₛₗ [semilinear_map_class F σ M M₃] {c : R} (hc : is_unit c) (s : set M₃) : h ⁻¹' (σ c • s) = c • h ⁻¹' s := begin apply set.subset.antisymm, { rintros x ⟨y, ys, hy⟩, refine ⟨(hc.unit.inv : R) • x, _, _⟩, { simp only [←hy, smul_smul, set.mem_preimage, units.inv_eq_coe_inv, map_smulₛₗ h, ← map_mul, is_unit.coe_inv_mul, one_smul, map_one, ys] }, { simp only [smul_smul, is_unit.mul_coe_inv, one_smul, units.inv_eq_coe_inv] } }, { rintros x ⟨y, hy, rfl⟩, refine ⟨h y, hy, by simp only [ring_hom.id_apply, map_smulₛₗ h]⟩ } end variables (R M₂) lemma _root_.image_smul_set [linear_map_class F R M M₂] (c : R) (s : set M) : h '' (c • s) = c • h '' s := image_smul_setₛₗ _ _ _ h c s lemma _root_.preimage_smul_set [linear_map_class F R M M₂] {c : R} (hc : is_unit c) (s : set M₂) : h ⁻¹' (c • s) = c • h ⁻¹' s := preimage_smul_setₛₗ _ _ _ h hc s end pointwise variables (M M₂) /-- A typeclass for `has_smul` structures which can be moved through a `linear_map`. This typeclass is generated automatically from a `is_scalar_tower` instance, but exists so that we can also add an instance for `add_comm_group.int_module`, allowing `z •` to be moved even if `R` does not support negation. -/ class compatible_smul (R S : Type*) [semiring S] [has_smul R M] [module S M] [has_smul R M₂] [module S M₂] := (map_smul : ∀ (fₗ : M →ₗ[S] M₂) (c : R) (x : M), fₗ (c • x) = c • fₗ x) variables {M M₂} @[priority 100] instance is_scalar_tower.compatible_smul {R S : Type*} [semiring S] [has_smul R S] [has_smul R M] [module S M] [is_scalar_tower R S M] [has_smul R M₂] [module S M₂] [is_scalar_tower R S M₂] : compatible_smul M M₂ R S := ⟨λ fₗ c x, by rw [← smul_one_smul S c x, ← smul_one_smul S c (fₗ x), map_smul]⟩ @[simp, priority 900] lemma map_smul_of_tower {R S : Type*} [semiring S] [has_smul R M] [module S M] [has_smul R M₂] [module S M₂] [compatible_smul M M₂ R S] (fₗ : M →ₗ[S] M₂) (c : R) (x : M) : fₗ (c • x) = c • fₗ x := compatible_smul.map_smul fₗ c x /-- convert a linear map to an additive map -/ def to_add_monoid_hom : M →+ M₃ := { to_fun := f, map_zero' := f.map_zero, map_add' := f.map_add } @[simp] lemma to_add_monoid_hom_coe : ⇑f.to_add_monoid_hom = f := rfl section restrict_scalars variables (R) [module S M] [module S M₂] [compatible_smul M M₂ R S] /-- If `M` and `M₂` are both `R`-modules and `S`-modules and `R`-module structures are defined by an action of `R` on `S` (formally, we have two scalar towers), then any `S`-linear map from `M` to `M₂` is `R`-linear. See also `linear_map.map_smul_of_tower`. -/ def restrict_scalars (fₗ : M →ₗ[S] M₂) : M →ₗ[R] M₂ := { to_fun := fₗ, map_add' := fₗ.map_add, map_smul' := fₗ.map_smul_of_tower } @[simp] lemma coe_restrict_scalars (fₗ : M →ₗ[S] M₂) : ⇑(restrict_scalars R fₗ) = fₗ := rfl lemma restrict_scalars_apply (fₗ : M →ₗ[S] M₂) (x) : restrict_scalars R fₗ x = fₗ x := rfl lemma restrict_scalars_injective : function.injective (restrict_scalars R : (M →ₗ[S] M₂) → (M →ₗ[R] M₂)) := λ fₗ gₗ h, ext (linear_map.congr_fun h : _) @[simp] lemma restrict_scalars_inj (fₗ gₗ : M →ₗ[S] M₂) : fₗ.restrict_scalars R = gₗ.restrict_scalars R ↔ fₗ = gₗ := (restrict_scalars_injective R).eq_iff end restrict_scalars variable {R} @[simp] lemma map_sum {ι} {t : finset ι} {g : ι → M} : f (∑ i in t, g i) = (∑ i in t, f (g i)) := f.to_add_monoid_hom.map_sum _ _ theorem to_add_monoid_hom_injective : function.injective (to_add_monoid_hom : (M →ₛₗ[σ] M₃) → (M →+ M₃)) := λ f g h, ext $ add_monoid_hom.congr_fun h /-- If two `σ`-linear maps from `R` are equal on `1`, then they are equal. -/ @[ext] theorem ext_ring {f g : R →ₛₗ[σ] M₃} (h : f 1 = g 1) : f = g := ext $ λ x, by rw [← mul_one x, ← smul_eq_mul, f.map_smulₛₗ, g.map_smulₛₗ, h] theorem ext_ring_iff {σ : R →+* R} {f g : R →ₛₗ[σ] M} : f = g ↔ f 1 = g 1 := ⟨λ h, h ▸ rfl, ext_ring⟩ @[ext] theorem ext_ring_op {σ : Rᵐᵒᵖ →+* S} {f g : R →ₛₗ[σ] M₃} (h : f 1 = g 1) : f = g := ext $ λ x, by rw [← one_mul x, ← op_smul_eq_mul, f.map_smulₛₗ, g.map_smulₛₗ, h] end /-- Interpret a `ring_hom` `f` as an `f`-semilinear map. -/ @[simps] def _root_.ring_hom.to_semilinear_map (f : R →+* S) : R →ₛₗ[f] S := { to_fun := f, map_smul' := f.map_mul, .. f} section variables [semiring R₁] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₃] variables {module_M₁ : module R₁ M₁} {module_M₂ : module R₂ M₂} {module_M₃ : module R₃ M₃} variables {σ₁₂ : R₁ →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R₁ →+* R₃} variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables (f : M₂ →ₛₗ[σ₂₃] M₃) (g : M₁ →ₛₗ[σ₁₂] M₂) include module_M₁ module_M₂ module_M₃ /-- Composition of two linear maps is a linear map -/ def comp : M₁ →ₛₗ[σ₁₃] M₃ := { to_fun := f ∘ g, map_add' := by simp only [map_add, forall_const, eq_self_iff_true, comp_app], map_smul' := λ r x, by rw [comp_app, map_smulₛₗ, map_smulₛₗ, ring_hom_comp_triple.comp_apply] } omit module_M₁ module_M₂ module_M₃ infixr ` ∘ₗ `:80 := @linear_map.comp _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) ring_hom_comp_triple.ids include σ₁₃ lemma comp_apply (x : M₁) : f.comp g x = f (g x) := rfl omit σ₁₃ include σ₁₃ @[simp, norm_cast] lemma coe_comp : (f.comp g : M₁ → M₃) = f ∘ g := rfl omit σ₁₃ @[simp] theorem comp_id : f.comp id = f := linear_map.ext $ λ x, rfl @[simp] theorem id_comp : id.comp f = f := linear_map.ext $ λ x, rfl variables {f g} {f' : M₂ →ₛₗ[σ₂₃] M₃} {g' : M₁ →ₛₗ[σ₁₂] M₂} include σ₁₃ theorem cancel_right (hg : function.surjective g) : f.comp g = f'.comp g ↔ f = f' := ⟨λ h, ext $ hg.forall.2 (ext_iff.1 h), λ h, h ▸ rfl⟩ theorem cancel_left (hf : function.injective f) : f.comp g = f.comp g' ↔ g = g' := ⟨λ h, ext $ λ x, hf $ by rw [← comp_apply, h, comp_apply], λ h, h ▸ rfl⟩ omit σ₁₃ end variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₃] /-- If a function `g` is a left and right inverse of a linear map `f`, then `g` is linear itself. -/ def inverse [module R M] [module S M₂] {σ : R →+* S} {σ' : S →+* R} [ring_hom_inv_pair σ σ'] (f : M →ₛₗ[σ] M₂) (g : M₂ → M) (h₁ : left_inverse g f) (h₂ : right_inverse g f) : M₂ →ₛₗ[σ'] M := by dsimp [left_inverse, function.right_inverse] at h₁ h₂; exact { to_fun := g, map_add' := λ x y, by { rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [h₂] }, map_smul' := λ a b, by { rw [← h₁ (g (a • b)), ← h₁ ((σ' a) • g b)], simp [h₂] } } end add_comm_monoid section add_comm_group variables [semiring R] [semiring S] [add_comm_group M] [add_comm_group M₂] variables {module_M : module R M} {module_M₂ : module S M₂} {σ : R →+* S} variables (f : M →ₛₗ[σ] M₂) protected lemma map_neg (x : M) : f (- x) = - f x := map_neg f x protected lemma map_sub (x y : M) : f (x - y) = f x - f y := map_sub f x y instance compatible_smul.int_module {S : Type*} [semiring S] [module S M] [module S M₂] : compatible_smul M M₂ ℤ S := ⟨λ fₗ c x, begin induction c using int.induction_on, case hz : { simp }, case hp : n ih { simp [add_smul, ih] }, case hn : n ih { simp [sub_smul, ih] } end⟩ instance compatible_smul.units {R S : Type*} [monoid R] [mul_action R M] [mul_action R M₂] [semiring S] [module S M] [module S M₂] [compatible_smul M M₂ R S] : compatible_smul M M₂ Rˣ S := ⟨λ fₗ c x, (compatible_smul.map_smul fₗ (c : R) x : _)⟩ end add_comm_group end linear_map namespace module /-- `g : R →+* S` is `R`-linear when the module structure on `S` is `module.comp_hom S g` . -/ @[simps] def comp_hom.to_linear_map {R S : Type*} [semiring R] [semiring S] (g : R →+* S) : (by haveI := comp_hom S g; exact (R →ₗ[R] S)) := by exact { to_fun := (g : R → S), map_add' := g.map_add, map_smul' := g.map_mul } end module namespace distrib_mul_action_hom variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] /-- A `distrib_mul_action_hom` between two modules is a linear map. -/ def to_linear_map (fₗ : M →+[R] M₂) : M →ₗ[R] M₂ := { ..fₗ } instance : has_coe (M →+[R] M₂) (M →ₗ[R] M₂) := ⟨to_linear_map⟩ @[simp] lemma to_linear_map_eq_coe (f : M →+[R] M₂) : f.to_linear_map = ↑f := rfl @[simp, norm_cast] lemma coe_to_linear_map (f : M →+[R] M₂) : ((f : M →ₗ[R] M₂) : M → M₂) = f := rfl lemma to_linear_map_injective {f g : M →+[R] M₂} (h : (f : M →ₗ[R] M₂) = (g : M →ₗ[R] M₂)) : f = g := by { ext m, exact linear_map.congr_fun h m, } end distrib_mul_action_hom namespace is_linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] variables [module R M] [module R M₂] include R /-- Convert an `is_linear_map` predicate to a `linear_map` -/ def mk' (f : M → M₂) (H : is_linear_map R f) : M →ₗ[R] M₂ := { to_fun := f, map_add' := H.1, map_smul' := H.2 } @[simp] theorem mk'_apply {f : M → M₂} (H : is_linear_map R f) (x : M) : mk' f H x = f x := rfl lemma is_linear_map_smul {R M : Type*} [comm_semiring R] [add_comm_monoid M] [module R M] (c : R) : is_linear_map R (λ (z : M), c • z) := begin refine is_linear_map.mk (smul_add c) _, intros _ _, simp only [smul_smul, mul_comm] end lemma is_linear_map_smul' {R M : Type*} [semiring R] [add_comm_monoid M] [module R M] (a : M) : is_linear_map R (λ (c : R), c • a) := is_linear_map.mk (λ x y, add_smul x y a) (λ x y, mul_smul x y a) variables {f : M → M₂} (lin : is_linear_map R f) include M M₂ lin lemma map_zero : f (0 : M) = (0 : M₂) := (lin.mk' f).map_zero end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] variables [module R M] [module R M₂] include R lemma is_linear_map_neg : is_linear_map R (λ (z : M), -z) := is_linear_map.mk neg_add (λ x y, (smul_neg x y).symm) variables {f : M → M₂} (lin : is_linear_map R f) include M M₂ lin lemma map_neg (x : M) : f (- x) = - f x := (lin.mk' f).map_neg x lemma map_sub (x y) : f (x - y) = f x - f y := (lin.mk' f).map_sub x y end add_comm_group end is_linear_map /-- Linear endomorphisms of a module, with associated ring structure `module.End.semiring` and algebra structure `module.End.algebra`. -/ abbreviation module.End (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [module R M] := M →ₗ[R] M /-- Reinterpret an additive homomorphism as a `ℕ`-linear map. -/ def add_monoid_hom.to_nat_linear_map [add_comm_monoid M] [add_comm_monoid M₂] (f : M →+ M₂) : M →ₗ[ℕ] M₂ := { to_fun := f, map_add' := f.map_add, map_smul' := map_nsmul f } lemma add_monoid_hom.to_nat_linear_map_injective [add_comm_monoid M] [add_comm_monoid M₂] : function.injective (@add_monoid_hom.to_nat_linear_map M M₂ _ _) := by { intros f g h, ext, exact linear_map.congr_fun h x } /-- Reinterpret an additive homomorphism as a `ℤ`-linear map. -/ def add_monoid_hom.to_int_linear_map [add_comm_group M] [add_comm_group M₂] (f : M →+ M₂) : M →ₗ[ℤ] M₂ := { to_fun := f, map_add' := f.map_add, map_smul' := map_zsmul f } lemma add_monoid_hom.to_int_linear_map_injective [add_comm_group M] [add_comm_group M₂] : function.injective (@add_monoid_hom.to_int_linear_map M M₂ _ _) := by { intros f g h, ext, exact linear_map.congr_fun h x } @[simp] lemma add_monoid_hom.coe_to_int_linear_map [add_comm_group M] [add_comm_group M₂] (f : M →+ M₂) : ⇑f.to_int_linear_map = f := rfl /-- Reinterpret an additive homomorphism as a `ℚ`-linear map. -/ def add_monoid_hom.to_rat_linear_map [add_comm_group M] [module ℚ M] [add_comm_group M₂] [module ℚ M₂] (f : M →+ M₂) : M →ₗ[ℚ] M₂ := { map_smul' := map_rat_smul f, ..f } lemma add_monoid_hom.to_rat_linear_map_injective [add_comm_group M] [module ℚ M] [add_comm_group M₂] [module ℚ M₂] : function.injective (@add_monoid_hom.to_rat_linear_map M M₂ _ _ _ _) := by { intros f g h, ext, exact linear_map.congr_fun h x } @[simp] lemma add_monoid_hom.coe_to_rat_linear_map [add_comm_group M] [module ℚ M] [add_comm_group M₂] [module ℚ M₂] (f : M →+ M₂) : ⇑f.to_rat_linear_map = f := rfl namespace linear_map section has_smul variables [semiring R] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R₂ M₂] [module R₃ M₃] variables {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables [monoid S] [distrib_mul_action S M₂] [smul_comm_class R₂ S M₂] variables [monoid S₃] [distrib_mul_action S₃ M₃] [smul_comm_class R₃ S₃ M₃] variables [monoid T] [distrib_mul_action T M₂] [smul_comm_class R₂ T M₂] instance : has_smul S (M →ₛₗ[σ₁₂] M₂) := ⟨λ a f, { to_fun := a • f, map_add' := λ x y, by simp only [pi.smul_apply, f.map_add, smul_add], map_smul' := λ c x, by simp [pi.smul_apply, smul_comm (σ₁₂ c)] }⟩ @[simp] lemma smul_apply (a : S) (f : M →ₛₗ[σ₁₂] M₂) (x : M) : (a • f) x = a • f x := rfl lemma coe_smul (a : S) (f : M →ₛₗ[σ₁₂] M₂) : ⇑(a • f) = a • f := rfl instance [smul_comm_class S T M₂] : smul_comm_class S T (M →ₛₗ[σ₁₂] M₂) := ⟨λ a b f, ext $ λ x, smul_comm _ _ _⟩ -- example application of this instance: if S -> T -> R are homomorphisms of commutative rings and -- M and M₂ are R-modules then the S-module and T-module structures on Hom_R(M,M₂) are compatible. instance [has_smul S T] [is_scalar_tower S T M₂] : is_scalar_tower S T (M →ₛₗ[σ₁₂] M₂) := { smul_assoc := λ _ _ _, ext $ λ _, smul_assoc _ _ _ } instance [distrib_mul_action Sᵐᵒᵖ M₂] [smul_comm_class R₂ Sᵐᵒᵖ M₂] [is_central_scalar S M₂] : is_central_scalar S (M →ₛₗ[σ₁₂] M₂) := { op_smul_eq_smul := λ a b, ext $ λ x, op_smul_eq_smul _ _ } end has_smul /-! ### Arithmetic on the codomain -/ section arithmetic variables [semiring R₁] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [add_comm_group N₁] [add_comm_group N₂] [add_comm_group N₃] variables [module R₁ M] [module R₂ M₂] [module R₃ M₃] variables [module R₁ N₁] [module R₂ N₂] [module R₃ N₃] variables {σ₁₂ : R₁ →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R₁ →+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] /-- The constant 0 map is linear. -/ instance : has_zero (M →ₛₗ[σ₁₂] M₂) := ⟨{ to_fun := 0, map_add' := by simp, map_smul' := by simp }⟩ @[simp] lemma zero_apply (x : M) : (0 : M →ₛₗ[σ₁₂] M₂) x = 0 := rfl @[simp] theorem comp_zero (g : M₂ →ₛₗ[σ₂₃] M₃) : (g.comp (0 : M →ₛₗ[σ₁₂] M₂) : M →ₛₗ[σ₁₃] M₃) = 0 := ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, g.map_zero] @[simp] theorem zero_comp (f : M →ₛₗ[σ₁₂] M₂) : ((0 : M₂ →ₛₗ[σ₂₃] M₃).comp f : M →ₛₗ[σ₁₃] M₃) = 0 := rfl instance : inhabited (M →ₛₗ[σ₁₂] M₂) := ⟨0⟩ @[simp] lemma default_def : (default : (M →ₛₗ[σ₁₂] M₂)) = 0 := rfl /-- The sum of two linear maps is linear. -/ instance : has_add (M →ₛₗ[σ₁₂] M₂) := ⟨λ f g, { to_fun := f + g, map_add' := by simp [add_comm, add_left_comm], map_smul' := by simp [smul_add] }⟩ @[simp] lemma add_apply (f g : M →ₛₗ[σ₁₂] M₂) (x : M) : (f + g) x = f x + g x := rfl lemma add_comp (f : M →ₛₗ[σ₁₂] M₂) (g h : M₂ →ₛₗ[σ₂₃] M₃) : ((h + g).comp f : M →ₛₗ[σ₁₃] M₃) = h.comp f + g.comp f := rfl lemma comp_add (f g : M →ₛₗ[σ₁₂] M₂) (h : M₂ →ₛₗ[σ₂₃] M₃) : (h.comp (f + g) : M →ₛₗ[σ₁₃] M₃) = h.comp f + h.comp g := ext $ λ _, h.map_add _ _ /-- The type of linear maps is an additive monoid. -/ instance : add_comm_monoid (M →ₛₗ[σ₁₂] M₂) := fun_like.coe_injective.add_comm_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) /-- The negation of a linear map is linear. -/ instance : has_neg (M →ₛₗ[σ₁₂] N₂) := ⟨λ f, { to_fun := -f, map_add' := by simp [add_comm], map_smul' := by simp }⟩ @[simp] lemma neg_apply (f : M →ₛₗ[σ₁₂] N₂) (x : M) : (- f) x = - f x := rfl include σ₁₃ @[simp] lemma neg_comp (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₃] N₃) : (- g).comp f = - g.comp f := rfl @[simp] lemma comp_neg (f : M →ₛₗ[σ₁₂] N₂) (g : N₂ →ₛₗ[σ₂₃] N₃) : g.comp (- f) = - g.comp f := ext $ λ _, g.map_neg _ omit σ₁₃ /-- The negation of a linear map is linear. -/ instance : has_sub (M →ₛₗ[σ₁₂] N₂) := ⟨λ f g, { to_fun := f - g, map_add' := λ x y, by simp only [pi.sub_apply, map_add, add_sub_add_comm], map_smul' := λ r x, by simp [pi.sub_apply, map_smul, smul_sub] }⟩ @[simp] lemma sub_apply (f g : M →ₛₗ[σ₁₂] N₂) (x : M) : (f - g) x = f x - g x := rfl include σ₁₃ lemma sub_comp (f : M →ₛₗ[σ₁₂] M₂) (g h : M₂ →ₛₗ[σ₂₃] N₃) : (g - h).comp f = g.comp f - h.comp f := rfl lemma comp_sub (f g : M →ₛₗ[σ₁₂] N₂) (h : N₂ →ₛₗ[σ₂₃] N₃) : h.comp (g - f) = h.comp g - h.comp f := ext $ λ _, h.map_sub _ _ omit σ₁₃ /-- The type of linear maps is an additive group. -/ instance : add_comm_group (M →ₛₗ[σ₁₂] N₂) := fun_like.coe_injective.add_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) end arithmetic section actions variables [semiring R] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R₂ M₂] [module R₃ M₃] variables {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] section has_smul variables [monoid S] [distrib_mul_action S M₂] [smul_comm_class R₂ S M₂] variables [monoid S₃] [distrib_mul_action S₃ M₃] [smul_comm_class R₃ S₃ M₃] variables [monoid T] [distrib_mul_action T M₂] [smul_comm_class R₂ T M₂] instance : distrib_mul_action S (M →ₛₗ[σ₁₂] M₂) := { one_smul := λ f, ext $ λ _, one_smul _ _, mul_smul := λ c c' f, ext $ λ _, mul_smul _ _ _, smul_add := λ c f g, ext $ λ x, smul_add _ _ _, smul_zero := λ c, ext $ λ x, smul_zero _ } include σ₁₃ theorem smul_comp (a : S₃) (g : M₂ →ₛₗ[σ₂₃] M₃) (f : M →ₛₗ[σ₁₂] M₂) : (a • g).comp f = a • (g.comp f) := rfl omit σ₁₃ -- TODO: generalize this to semilinear maps theorem comp_smul [module R M₂] [module R M₃] [smul_comm_class R S M₂] [distrib_mul_action S M₃] [smul_comm_class R S M₃] [compatible_smul M₃ M₂ S R] (g : M₃ →ₗ[R] M₂) (a : S) (f : M →ₗ[R] M₃) : g.comp (a • f) = a • (g.comp f) := ext $ λ x, g.map_smul_of_tower _ _ end has_smul section module variables [semiring S] [module S M₂] [smul_comm_class R₂ S M₂] instance : module S (M →ₛₗ[σ₁₂] M₂) := { add_smul := λ a b f, ext $ λ x, add_smul _ _ _, zero_smul := λ f, ext $ λ x, zero_smul _ _ } instance [no_zero_smul_divisors S M₂] : no_zero_smul_divisors S (M →ₛₗ[σ₁₂] M₂) := coe_injective.no_zero_smul_divisors _ rfl coe_smul end module end actions /-! ### Monoid structure of endomorphisms Lemmas about `pow` such as `linear_map.pow_apply` appear in later files. -/ section endomorphisms variables [semiring R] [add_comm_monoid M] [add_comm_group N₁] [module R M] [module R N₁] instance : has_one (module.End R M) := ⟨linear_map.id⟩ instance : has_mul (module.End R M) := ⟨linear_map.comp⟩ lemma one_eq_id : (1 : module.End R M) = id := rfl lemma mul_eq_comp (f g : module.End R M) : f * g = f.comp g := rfl @[simp] lemma one_apply (x : M) : (1 : module.End R M) x = x := rfl @[simp] lemma mul_apply (f g : module.End R M) (x : M) : (f * g) x = f (g x) := rfl lemma coe_one : ⇑(1 : module.End R M) = _root_.id := rfl lemma coe_mul (f g : module.End R M) : ⇑(f * g) = f ∘ g := rfl instance _root_.module.End.monoid : monoid (module.End R M) := { mul := (*), one := (1 : M →ₗ[R] M), mul_assoc := λ f g h, linear_map.ext $ λ x, rfl, mul_one := comp_id, one_mul := id_comp } instance _root_.module.End.semiring : semiring (module.End R M) := { mul := (*), one := (1 : M →ₗ[R] M), zero := 0, add := (+), mul_zero := comp_zero, zero_mul := zero_comp, left_distrib := λ f g h, comp_add _ _ _, right_distrib := λ f g h, add_comp _ _ _, nat_cast := λ n, n • 1, nat_cast_zero := add_monoid.nsmul_zero' _, nat_cast_succ := λ n, (add_monoid.nsmul_succ' n 1).trans (add_comm _ _), .. add_monoid_with_one.unary, .. _root_.module.End.monoid, .. linear_map.add_comm_monoid } /-- See also `module.End.nat_cast_def`. -/ @[simp] lemma _root_.module.End.nat_cast_apply (n : ℕ) (m : M) : (↑n : module.End R M) m = n • m := rfl instance _root_.module.End.ring : ring (module.End R N₁) := { int_cast := λ z, z • 1, int_cast_of_nat := of_nat_zsmul _, int_cast_neg_succ_of_nat := zsmul_neg_succ_of_nat _, ..module.End.semiring, ..linear_map.add_comm_group } /-- See also `module.End.int_cast_def`. -/ @[simp] lemma _root_.module.End.int_cast_apply (z : ℤ) (m : N₁) : (↑z : module.End R N₁) m = z • m := rfl section variables [monoid S] [distrib_mul_action S M] [smul_comm_class R S M] instance _root_.module.End.is_scalar_tower : is_scalar_tower S (module.End R M) (module.End R M) := ⟨smul_comp⟩ instance _root_.module.End.smul_comm_class [has_smul S R] [is_scalar_tower S R M] : smul_comm_class S (module.End R M) (module.End R M) := ⟨λ s _ _, (comp_smul _ s _).symm⟩ instance _root_.module.End.smul_comm_class' [has_smul S R] [is_scalar_tower S R M] : smul_comm_class (module.End R M) S (module.End R M) := smul_comm_class.symm _ _ _ end /-! ### Action by a module endomorphism. -/ /-- The tautological action by `module.End R M` (aka `M →ₗ[R] M`) on `M`. This generalizes `function.End.apply_mul_action`. -/ instance apply_module : module (module.End R M) M := { smul := ($), smul_zero := linear_map.map_zero, smul_add := linear_map.map_add, add_smul := linear_map.add_apply, zero_smul := (linear_map.zero_apply : ∀ m, (0 : M →ₗ[R] M) m = 0), one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma smul_def (f : module.End R M) (a : M) : f • a = f a := rfl /-- `linear_map.apply_module` is faithful. -/ instance apply_has_faithful_smul : has_faithful_smul (module.End R M) M := ⟨λ _ _, linear_map.ext⟩ instance apply_smul_comm_class : smul_comm_class R (module.End R M) M := { smul_comm := λ r e m, (e.map_smul r m).symm } instance apply_smul_comm_class' : smul_comm_class (module.End R M) R M := { smul_comm := linear_map.map_smul } instance apply_is_scalar_tower {R M : Type*} [comm_semiring R] [add_comm_monoid M] [module R M] : is_scalar_tower R (module.End R M) M := ⟨λ t f m, rfl⟩ end endomorphisms end linear_map /-! ### Actions as module endomorphisms -/ namespace distrib_mul_action variables (R M) [semiring R] [add_comm_monoid M] [module R M] variables [monoid S] [distrib_mul_action S M] [smul_comm_class S R M] /-- Each element of the monoid defines a linear map. This is a stronger version of `distrib_mul_action.to_add_monoid_hom`. -/ @[simps] def to_linear_map (s : S) : M →ₗ[R] M := { to_fun := has_smul.smul s, map_add' := smul_add s, map_smul' := λ a b, smul_comm _ _ _ } /-- Each element of the monoid defines a module endomorphism. This is a stronger version of `distrib_mul_action.to_add_monoid_End`. -/ @[simps] def to_module_End : S →* module.End R M := { to_fun := to_linear_map R M, map_one' := linear_map.ext $ one_smul _, map_mul' := λ a b, linear_map.ext $ mul_smul _ _ } end distrib_mul_action namespace module variables (R M) [semiring R] [add_comm_monoid M] [module R M] variables [semiring S] [module S M] [smul_comm_class S R M] /-- Each element of the semiring defines a module endomorphism. This is a stronger version of `distrib_mul_action.to_module_End`. -/ @[simps] def to_module_End : S →+* module.End R M := { to_fun := distrib_mul_action.to_linear_map R M, map_zero' := linear_map.ext $ zero_smul _, map_add' := λ f g, linear_map.ext $ add_smul _ _, ..distrib_mul_action.to_module_End R M } /-- The canonical (semi)ring isomorphism from `Rᵐᵒᵖ` to `module.End R R` induced by the right multiplication. -/ @[simps] def module_End_self : Rᵐᵒᵖ ≃+* module.End R R := { to_fun := distrib_mul_action.to_linear_map R R, inv_fun := λ f, mul_opposite.op (f 1), left_inv := mul_one, right_inv := λ f, linear_map.ext_ring $ one_mul _, ..module.to_module_End R R } /-- The canonical (semi)ring isomorphism from `R` to `module.End Rᵐᵒᵖ R` induced by the left multiplication. -/ @[simps] def module_End_self_op : R ≃+* module.End Rᵐᵒᵖ R := { to_fun := distrib_mul_action.to_linear_map _ _, inv_fun := λ f, f 1, left_inv := mul_one, right_inv := λ f, linear_map.ext_ring_op $ mul_one _, ..module.to_module_End _ _ } lemma End.nat_cast_def (n : ℕ) [add_comm_monoid N₁] [module R N₁] : (↑n : module.End R N₁) = module.to_module_End R N₁ n := rfl lemma End.int_cast_def (z : ℤ) [add_comm_group N₁] [module R N₁] : (↑z : module.End R N₁) = module.to_module_End R N₁ z := rfl end module
779e91f29b4ec1d797d9d5c49ead0961ee544ecf
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/power_series/basic.lean
683aaf683c40523e9f8461a4286caf2653145395
[ "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
76,119
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Kenny Lau -/ import algebra.big_operators.nat_antidiagonal import data.finsupp.interval import data.mv_polynomial.basic import data.polynomial.algebra_map import data.polynomial.coeff import linear_algebra.std_basis import ring_theory.ideal.local_ring import ring_theory.multiplicity import tactic.linarith /-! # Formal power series This file defines (multivariate) formal power series and develops the basic properties of these objects. A formal power series is to a polynomial like an infinite sum is to a finite sum. We provide the natural inclusion from polynomials to formal power series. ## Generalities The file starts with setting up the (semi)ring structure on multivariate power series. `trunc n φ` truncates a formal power series to the polynomial that has the same coefficients as `φ`, for all `m < n`, and `0` otherwise. If the constant coefficient of a formal power series is invertible, then this formal power series is invertible. Formal power series over a local ring form a local ring. ## Formal power series in one variable We prove that if the ring of coefficients is an integral domain, then formal power series in one variable form an integral domain. The `order` of a formal power series `φ` is the multiplicity of the variable `X` in `φ`. If the coefficients form an integral domain, then `order` is a valuation (`order_mul`, `le_order_add`). ## Implementation notes In this file we define multivariate formal power series with variables indexed by `σ` and coefficients in `R` as `mv_power_series σ R := (σ →₀ ℕ) → R`. Unfortunately there is not yet enough API to show that they are the completion of the ring of multivariate polynomials. However, we provide most of the infrastructure that is needed to do this. Once I-adic completion (topological or algebraic) is available it should not be hard to fill in the details. Formal power series in one variable are defined as `power_series R := mv_power_series unit R`. This allows us to port a lot of proofs and properties from the multivariate case to the single variable case. However, it means that formal power series are indexed by `unit →₀ ℕ`, which is of course canonically isomorphic to `ℕ`. We then build some glue to treat formal power series as if they are indexed by `ℕ`. Occasionally this leads to proofs that are uglier than expected. -/ noncomputable theory open_locale classical big_operators polynomial /-- Multivariate formal power series, where `σ` is the index set of the variables and `R` is the coefficient ring.-/ def mv_power_series (σ : Type*) (R : Type*) := (σ →₀ ℕ) → R namespace mv_power_series open finsupp variables {σ R : Type*} instance [inhabited R] : inhabited (mv_power_series σ R) := ⟨λ _, default⟩ instance [has_zero R] : has_zero (mv_power_series σ R) := pi.has_zero instance [add_monoid R] : add_monoid (mv_power_series σ R) := pi.add_monoid instance [add_group R] : add_group (mv_power_series σ R) := pi.add_group instance [add_comm_monoid R] : add_comm_monoid (mv_power_series σ R) := pi.add_comm_monoid instance [add_comm_group R] : add_comm_group (mv_power_series σ R) := pi.add_comm_group instance [nontrivial R] : nontrivial (mv_power_series σ R) := function.nontrivial instance {A} [semiring R] [add_comm_monoid A] [module R A] : module R (mv_power_series σ A) := pi.module _ _ _ instance {A S} [semiring R] [semiring S] [add_comm_monoid A] [module R A] [module S A] [has_smul R S] [is_scalar_tower R S A] : is_scalar_tower R S (mv_power_series σ A) := pi.is_scalar_tower section semiring variables (R) [semiring R] /-- The `n`th monomial with coefficient `a` as multivariate formal power series.-/ def monomial (n : σ →₀ ℕ) : R →ₗ[R] mv_power_series σ R := linear_map.std_basis R _ n /-- The `n`th coefficient of a multivariate formal power series.-/ def coeff (n : σ →₀ ℕ) : (mv_power_series σ R) →ₗ[R] R := linear_map.proj n variables {R} /-- Two multivariate formal power series are equal if all their coefficients are equal.-/ @[ext] lemma ext {φ ψ} (h : ∀ (n : σ →₀ ℕ), coeff R n φ = coeff R n ψ) : φ = ψ := funext h /-- Two multivariate formal power series are equal if and only if all their coefficients are equal.-/ lemma ext_iff {φ ψ : mv_power_series σ R} : φ = ψ ↔ (∀ (n : σ →₀ ℕ), coeff R n φ = coeff R n ψ) := function.funext_iff lemma monomial_def [decidable_eq σ] (n : σ →₀ ℕ) : monomial R n = linear_map.std_basis R _ n := by convert rfl -- unify the `decidable` arguments lemma coeff_monomial [decidable_eq σ] (m n : σ →₀ ℕ) (a : R) : coeff R m (monomial R n a) = if m = n then a else 0 := by rw [coeff, monomial_def, linear_map.proj_apply, linear_map.std_basis_apply, function.update_apply, pi.zero_apply] @[simp] lemma coeff_monomial_same (n : σ →₀ ℕ) (a : R) : coeff R n (monomial R n a) = a := linear_map.std_basis_same R _ n a lemma coeff_monomial_ne {m n : σ →₀ ℕ} (h : m ≠ n) (a : R) : coeff R m (monomial R n a) = 0 := linear_map.std_basis_ne R _ _ _ h a lemma eq_of_coeff_monomial_ne_zero {m n : σ →₀ ℕ} {a : R} (h : coeff R m (monomial R n a) ≠ 0) : m = n := by_contra $ λ h', h $ coeff_monomial_ne h' a @[simp] lemma coeff_comp_monomial (n : σ →₀ ℕ) : (coeff R n).comp (monomial R n) = linear_map.id := linear_map.ext $ coeff_monomial_same n @[simp] lemma coeff_zero (n : σ →₀ ℕ) : coeff R n (0 : mv_power_series σ R) = 0 := rfl variables (m n : σ →₀ ℕ) (φ ψ : mv_power_series σ R) instance : has_one (mv_power_series σ R) := ⟨monomial R (0 : σ →₀ ℕ) 1⟩ lemma coeff_one [decidable_eq σ] : coeff R n (1 : mv_power_series σ R) = if n = 0 then 1 else 0 := coeff_monomial _ _ _ lemma coeff_zero_one : coeff R (0 : σ →₀ ℕ) 1 = 1 := coeff_monomial_same 0 1 lemma monomial_zero_one : monomial R (0 : σ →₀ ℕ) 1 = 1 := rfl instance : add_monoid_with_one (mv_power_series σ R) := { nat_cast := λ n, monomial R 0 n, nat_cast_zero := by simp [nat.cast], nat_cast_succ := by simp [nat.cast, monomial_zero_one], one := 1, .. mv_power_series.add_monoid } instance : has_mul (mv_power_series σ R) := ⟨λ φ ψ n, ∑ p in finsupp.antidiagonal n, coeff R p.1 φ * coeff R p.2 ψ⟩ lemma coeff_mul : coeff R n (φ * ψ) = ∑ p in finsupp.antidiagonal n, coeff R p.1 φ * coeff R p.2 ψ := rfl protected lemma zero_mul : (0 : mv_power_series σ R) * φ = 0 := ext $ λ n, by simp [coeff_mul] protected lemma mul_zero : φ * 0 = 0 := ext $ λ n, by simp [coeff_mul] lemma coeff_monomial_mul (a : R) : coeff R m (monomial R n a * φ) = if n ≤ m then a * coeff R (m - n) φ else 0 := begin have : ∀ p ∈ antidiagonal m, coeff R (p : (σ →₀ ℕ) × (σ →₀ ℕ)).1 (monomial R n a) * coeff R p.2 φ ≠ 0 → p.1 = n := λ p _ hp, eq_of_coeff_monomial_ne_zero (left_ne_zero_of_mul hp), rw [coeff_mul, ← finset.sum_filter_of_ne this, antidiagonal_filter_fst_eq, finset.sum_ite_index], simp only [finset.sum_singleton, coeff_monomial_same, finset.sum_empty] end lemma coeff_mul_monomial (a : R) : coeff R m (φ * monomial R n a) = if n ≤ m then coeff R (m - n) φ * a else 0 := begin have : ∀ p ∈ antidiagonal m, coeff R (p : (σ →₀ ℕ) × (σ →₀ ℕ)).1 φ * coeff R p.2 (monomial R n a) ≠ 0 → p.2 = n := λ p _ hp, eq_of_coeff_monomial_ne_zero (right_ne_zero_of_mul hp), rw [coeff_mul, ← finset.sum_filter_of_ne this, antidiagonal_filter_snd_eq, finset.sum_ite_index], simp only [finset.sum_singleton, coeff_monomial_same, finset.sum_empty] end lemma coeff_add_monomial_mul (a : R) : coeff R (m + n) (monomial R m a * φ) = a * coeff R n φ := begin rw [coeff_monomial_mul, if_pos, add_tsub_cancel_left], exact le_add_right le_rfl end lemma coeff_add_mul_monomial (a : R) : coeff R (m + n) (φ * monomial R n a) = coeff R m φ * a := begin rw [coeff_mul_monomial, if_pos, add_tsub_cancel_right], exact le_add_left le_rfl end protected lemma one_mul : (1 : mv_power_series σ R) * φ = φ := ext $ λ n, by simpa using coeff_add_monomial_mul 0 n φ 1 protected lemma mul_one : φ * 1 = φ := ext $ λ n, by simpa using coeff_add_mul_monomial n 0 φ 1 protected lemma mul_add (φ₁ φ₂ φ₃ : mv_power_series σ R) : φ₁ * (φ₂ + φ₃) = φ₁ * φ₂ + φ₁ * φ₃ := ext $ λ n, by simp only [coeff_mul, mul_add, finset.sum_add_distrib, linear_map.map_add] protected lemma add_mul (φ₁ φ₂ φ₃ : mv_power_series σ R) : (φ₁ + φ₂) * φ₃ = φ₁ * φ₃ + φ₂ * φ₃ := ext $ λ n, by simp only [coeff_mul, add_mul, finset.sum_add_distrib, linear_map.map_add] protected lemma mul_assoc (φ₁ φ₂ φ₃ : mv_power_series σ R) : (φ₁ * φ₂) * φ₃ = φ₁ * (φ₂ * φ₃) := begin ext1 n, simp only [coeff_mul, finset.sum_mul, finset.mul_sum, finset.sum_sigma'], refine finset.sum_bij (λ p _, ⟨(p.2.1, p.2.2 + p.1.2), (p.2.2, p.1.2)⟩) _ _ _ _; simp only [mem_antidiagonal, finset.mem_sigma, heq_iff_eq, prod.mk.inj_iff, and_imp, exists_prop], { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩, dsimp only, rintro rfl rfl, simp [add_assoc] }, { rintros ⟨⟨a, b⟩, ⟨c, d⟩⟩, dsimp only, rintro rfl rfl, apply mul_assoc }, { rintros ⟨⟨a, b⟩, ⟨c, d⟩⟩ ⟨⟨i, j⟩, ⟨k, l⟩⟩, dsimp only, rintro rfl rfl - rfl rfl - rfl rfl, refl }, { rintro ⟨⟨i, j⟩, ⟨k, l⟩⟩, dsimp only, rintro rfl rfl, refine ⟨⟨(i + k, l), (i, k)⟩, _, _⟩; simp [add_assoc] } end instance : semiring (mv_power_series σ R) := { mul_one := mv_power_series.mul_one, one_mul := mv_power_series.one_mul, mul_assoc := mv_power_series.mul_assoc, mul_zero := mv_power_series.mul_zero, zero_mul := mv_power_series.zero_mul, left_distrib := mv_power_series.mul_add, right_distrib := mv_power_series.add_mul, .. mv_power_series.add_monoid_with_one, .. mv_power_series.has_mul, .. mv_power_series.add_comm_monoid } end semiring instance [comm_semiring R] : comm_semiring (mv_power_series σ R) := { mul_comm := λ φ ψ, ext $ λ n, by simpa only [coeff_mul, mul_comm] using sum_antidiagonal_swap n (λ a b, coeff R a φ * coeff R b ψ), .. mv_power_series.semiring } instance [ring R] : ring (mv_power_series σ R) := { .. mv_power_series.semiring, .. mv_power_series.add_comm_group } instance [comm_ring R] : comm_ring (mv_power_series σ R) := { .. mv_power_series.comm_semiring, .. mv_power_series.add_comm_group } section semiring variables [semiring R] lemma monomial_mul_monomial (m n : σ →₀ ℕ) (a b : R) : monomial R m a * monomial R n b = monomial R (m + n) (a * b) := begin ext k, simp only [coeff_mul_monomial, coeff_monomial], split_ifs with h₁ h₂ h₃ h₃ h₂; try { refl }, { rw [← h₂, tsub_add_cancel_of_le h₁] at h₃, exact (h₃ rfl).elim }, { rw [h₃, add_tsub_cancel_right] at h₂, exact (h₂ rfl).elim }, { exact zero_mul b }, { rw h₂ at h₁, exact (h₁ $ le_add_left le_rfl).elim } end variables (σ) (R) /-- The constant multivariate formal power series.-/ def C : R →+* mv_power_series σ R := { map_one' := rfl, map_mul' := λ a b, (monomial_mul_monomial 0 0 a b).symm, map_zero' := (monomial R (0 : _)).map_zero, .. monomial R (0 : σ →₀ ℕ) } variables {σ} {R} @[simp] lemma monomial_zero_eq_C : ⇑(monomial R (0 : σ →₀ ℕ)) = C σ R := rfl lemma monomial_zero_eq_C_apply (a : R) : monomial R (0 : σ →₀ ℕ) a = C σ R a := rfl lemma coeff_C [decidable_eq σ] (n : σ →₀ ℕ) (a : R) : coeff R n (C σ R a) = if n = 0 then a else 0 := coeff_monomial _ _ _ lemma coeff_zero_C (a : R) : coeff R (0 : σ →₀ℕ) (C σ R a) = a := coeff_monomial_same 0 a /-- The variables of the multivariate formal power series ring.-/ def X (s : σ) : mv_power_series σ R := monomial R (single s 1) 1 lemma coeff_X [decidable_eq σ] (n : σ →₀ ℕ) (s : σ) : coeff R n (X s : mv_power_series σ R) = if n = (single s 1) then 1 else 0 := coeff_monomial _ _ _ lemma coeff_index_single_X [decidable_eq σ] (s t : σ) : coeff R (single t 1) (X s : mv_power_series σ R) = if t = s then 1 else 0 := by simp only [coeff_X, single_left_inj one_ne_zero] @[simp] lemma coeff_index_single_self_X (s : σ) : coeff R (single s 1) (X s : mv_power_series σ R) = 1 := coeff_monomial_same _ _ lemma coeff_zero_X (s : σ) : coeff R (0 : σ →₀ ℕ) (X s : mv_power_series σ R) = 0 := by { rw [coeff_X, if_neg], intro h, exact one_ne_zero (single_eq_zero.mp h.symm) } lemma X_def (s : σ) : X s = monomial R (single s 1) 1 := rfl lemma X_pow_eq (s : σ) (n : ℕ) : (X s : mv_power_series σ R)^n = monomial R (single s n) 1 := begin induction n with n ih, { rw [pow_zero, finsupp.single_zero, monomial_zero_one] }, { rw [pow_succ', ih, nat.succ_eq_add_one, finsupp.single_add, X, monomial_mul_monomial, one_mul] } end lemma coeff_X_pow [decidable_eq σ] (m : σ →₀ ℕ) (s : σ) (n : ℕ) : coeff R m ((X s : mv_power_series σ R)^n) = if m = single s n then 1 else 0 := by rw [X_pow_eq s n, coeff_monomial] @[simp] lemma coeff_mul_C (n : σ →₀ ℕ) (φ : mv_power_series σ R) (a : R) : coeff R n (φ * C σ R a) = coeff R n φ * a := by simpa using coeff_add_mul_monomial n 0 φ a @[simp] lemma coeff_C_mul (n : σ →₀ ℕ) (φ : mv_power_series σ R) (a : R) : coeff R n (C σ R a * φ) = a * coeff R n φ := by simpa using coeff_add_monomial_mul 0 n φ a lemma coeff_zero_mul_X (φ : mv_power_series σ R) (s : σ) : coeff R (0 : σ →₀ ℕ) (φ * X s) = 0 := begin have : ¬single s 1 ≤ 0, from λ h, by simpa using h s, simp only [X, coeff_mul_monomial, if_neg this] end lemma coeff_zero_X_mul (φ : mv_power_series σ R) (s : σ) : coeff R (0 : σ →₀ ℕ) (X s * φ) = 0 := begin have : ¬single s 1 ≤ 0, from λ h, by simpa using h s, simp only [X, coeff_monomial_mul, if_neg this] end variables (σ) (R) /-- The constant coefficient of a formal power series.-/ def constant_coeff : (mv_power_series σ R) →+* R := { to_fun := coeff R (0 : σ →₀ ℕ), map_one' := coeff_zero_one, map_mul' := λ φ ψ, by simp [coeff_mul, support_single_ne_zero], map_zero' := linear_map.map_zero _, .. coeff R (0 : σ →₀ ℕ) } variables {σ} {R} @[simp] lemma coeff_zero_eq_constant_coeff : ⇑(coeff R (0 : σ →₀ ℕ)) = constant_coeff σ R := rfl lemma coeff_zero_eq_constant_coeff_apply (φ : mv_power_series σ R) : coeff R (0 : σ →₀ ℕ) φ = constant_coeff σ R φ := rfl @[simp] lemma constant_coeff_C (a : R) : constant_coeff σ R (C σ R a) = a := rfl @[simp] lemma constant_coeff_comp_C : (constant_coeff σ R).comp (C σ R) = ring_hom.id R := rfl @[simp] lemma constant_coeff_zero : constant_coeff σ R 0 = 0 := rfl @[simp] lemma constant_coeff_one : constant_coeff σ R 1 = 1 := rfl @[simp] lemma constant_coeff_X (s : σ) : constant_coeff σ R (X s) = 0 := coeff_zero_X s /-- If a multivariate formal power series is invertible, then so is its constant coefficient.-/ lemma is_unit_constant_coeff (φ : mv_power_series σ R) (h : is_unit φ) : is_unit (constant_coeff σ R φ) := h.map _ @[simp] lemma coeff_smul (f : mv_power_series σ R) (n) (a : R) : coeff _ n (a • f) = a * coeff _ n f := rfl lemma smul_eq_C_mul (f : mv_power_series σ R) (a : R) : a • f = C σ R a * f := by { ext, simp } lemma X_inj [nontrivial R] {s t : σ} : (X s : mv_power_series σ R) = X t ↔ s = t := ⟨begin intro h, replace h := congr_arg (coeff R (single s 1)) h, rw [coeff_X, if_pos rfl, coeff_X] at h, split_ifs at h with H, { rw finsupp.single_eq_single_iff at H, cases H, { exact H.1 }, { exfalso, exact one_ne_zero H.1 } }, { exfalso, exact one_ne_zero h } end, congr_arg X⟩ end semiring section map variables {S T : Type*} [semiring R] [semiring S] [semiring T] variables (f : R →+* S) (g : S →+* T) variable (σ) /-- The map between multivariate formal power series induced by a map on the coefficients.-/ def map : mv_power_series σ R →+* mv_power_series σ S := { to_fun := λ φ n, f $ coeff R n φ, map_zero' := ext $ λ n, f.map_zero, map_one' := ext $ λ n, show f ((coeff R n) 1) = (coeff S n) 1, by { rw [coeff_one, coeff_one], split_ifs; simp [f.map_one, f.map_zero] }, map_add' := λ φ ψ, ext $ λ n, show f ((coeff R n) (φ + ψ)) = f ((coeff R n) φ) + f ((coeff R n) ψ), by simp, map_mul' := λ φ ψ, ext $ λ n, show f _ = _, begin rw [coeff_mul, f.map_sum, coeff_mul, finset.sum_congr rfl], rintros ⟨i,j⟩ hij, rw [f.map_mul], refl, end } variable {σ} @[simp] lemma map_id : map σ (ring_hom.id R) = ring_hom.id _ := rfl lemma map_comp : map σ (g.comp f) = (map σ g).comp (map σ f) := rfl @[simp] lemma coeff_map (n : σ →₀ ℕ) (φ : mv_power_series σ R) : coeff S n (map σ f φ) = f (coeff R n φ) := rfl @[simp] lemma constant_coeff_map (φ : mv_power_series σ R) : constant_coeff σ S (map σ f φ) = f (constant_coeff σ R φ) := rfl @[simp] lemma map_monomial (n : σ →₀ ℕ) (a : R) : map σ f (monomial R n a) = monomial S n (f a) := by { ext m, simp [coeff_monomial, apply_ite f] } @[simp] lemma map_C (a : R) : map σ f (C σ R a) = C σ S (f a) := map_monomial _ _ _ @[simp] lemma map_X (s : σ) : map σ f (X s) = X s := by simp [mv_power_series.X] end map section algebra variables {A : Type*} [comm_semiring R] [semiring A] [algebra R A] instance : algebra R (mv_power_series σ A) := { commutes' := λ a φ, by { ext n, simp [algebra.commutes] }, smul_def' := λ a σ, by { ext n, simp [(coeff A n).map_smul_of_tower a, algebra.smul_def] }, to_ring_hom := (mv_power_series.map σ (algebra_map R A)).comp (C σ R), .. mv_power_series.module } theorem C_eq_algebra_map : C σ R = (algebra_map R (mv_power_series σ R)) := rfl theorem algebra_map_apply {r : R} : algebra_map R (mv_power_series σ A) r = C σ A (algebra_map R A r) := begin change (mv_power_series.map σ (algebra_map R A)).comp (C σ R) r = _, simp, end instance [nonempty σ] [nontrivial R] : nontrivial (subalgebra R (mv_power_series σ R)) := ⟨⟨⊥, ⊤, begin rw [ne.def, set_like.ext_iff, not_forall], inhabit σ, refine ⟨X default, _⟩, simp only [algebra.mem_bot, not_exists, set.mem_range, iff_true, algebra.mem_top], intros x, rw [ext_iff, not_forall], refine ⟨finsupp.single default 1, _⟩, simp [algebra_map_apply, coeff_C], end⟩⟩ end algebra section trunc variables [comm_semiring R] (n : σ →₀ ℕ) /-- Auxiliary definition for the truncation function. -/ def trunc_fun (φ : mv_power_series σ R) : mv_polynomial σ R := ∑ m in finset.Iio n, mv_polynomial.monomial m (coeff R m φ) lemma coeff_trunc_fun (m : σ →₀ ℕ) (φ : mv_power_series σ R) : (trunc_fun n φ).coeff m = if m < n then coeff R m φ else 0 := by simp [trunc_fun, mv_polynomial.coeff_sum] variable (R) /-- The `n`th truncation of a multivariate formal power series to a multivariate polynomial -/ def trunc : mv_power_series σ R →+ mv_polynomial σ R := { to_fun := trunc_fun n, map_zero' := by { ext, simp [coeff_trunc_fun] }, map_add' := by { intros, ext, simp [coeff_trunc_fun, ite_add], split_ifs; refl } } variable {R} lemma coeff_trunc (m : σ →₀ ℕ) (φ : mv_power_series σ R) : (trunc R n φ).coeff m = if m < n then coeff R m φ else 0 := by simp [trunc, coeff_trunc_fun] @[simp] lemma trunc_one (hnn : n ≠ 0) : trunc R n 1 = 1 := mv_polynomial.ext _ _ $ λ m, begin rw [coeff_trunc, coeff_one], split_ifs with H H' H', { subst m, simp }, { symmetry, rw mv_polynomial.coeff_one, exact if_neg (ne.symm H'), }, { symmetry, rw mv_polynomial.coeff_one, refine if_neg _, rintro rfl, apply H, exact ne.bot_lt hnn, } end @[simp] lemma trunc_C (hnn : n ≠ 0) (a : R) : trunc R n (C σ R a) = mv_polynomial.C a := mv_polynomial.ext _ _ $ λ m, begin rw [coeff_trunc, coeff_C, mv_polynomial.coeff_C], split_ifs with H; refl <|> try {simp * at *}, exfalso, apply H, subst m, exact ne.bot_lt hnn, end end trunc section comm_semiring variable [comm_semiring R] lemma X_pow_dvd_iff {s : σ} {n : ℕ} {φ : mv_power_series σ R} : (X s : mv_power_series σ R)^n ∣ φ ↔ ∀ m : σ →₀ ℕ, m s < n → coeff R m φ = 0 := begin split, { rintros ⟨φ, rfl⟩ m h, rw [coeff_mul, finset.sum_eq_zero], rintros ⟨i,j⟩ hij, rw [coeff_X_pow, if_neg, zero_mul], contrapose! h, subst i, rw finsupp.mem_antidiagonal at hij, rw [← hij, finsupp.add_apply, finsupp.single_eq_same], exact nat.le_add_right n _ }, { intro h, refine ⟨λ m, coeff R (m + (single s n)) φ, _⟩, ext m, by_cases H : m - single s n + single s n = m, { rw [coeff_mul, finset.sum_eq_single (single s n, m - single s n)], { rw [coeff_X_pow, if_pos rfl, one_mul], simpa using congr_arg (λ (m : σ →₀ ℕ), coeff R m φ) H.symm }, { rintros ⟨i,j⟩ hij hne, rw finsupp.mem_antidiagonal at hij, rw coeff_X_pow, split_ifs with hi, { exfalso, apply hne, rw [← hij, ← hi, prod.mk.inj_iff], refine ⟨rfl, _⟩, ext t, simp only [add_tsub_cancel_left, finsupp.add_apply, finsupp.tsub_apply] }, { exact zero_mul _ } }, { intro hni, exfalso, apply hni, rwa [finsupp.mem_antidiagonal, add_comm] } }, { rw [h, coeff_mul, finset.sum_eq_zero], { rintros ⟨i,j⟩ hij, rw finsupp.mem_antidiagonal at hij, rw coeff_X_pow, split_ifs with hi, { exfalso, apply H, rw [← hij, hi], ext, rw [coe_add, coe_add, pi.add_apply, pi.add_apply, add_tsub_cancel_left, add_comm], }, { exact zero_mul _ } }, { classical, contrapose! H, ext t, by_cases hst : s = t, { subst t, simpa using tsub_add_cancel_of_le H }, { simp [finsupp.single_apply, hst] } } } } end lemma X_dvd_iff {s : σ} {φ : mv_power_series σ R} : (X s : mv_power_series σ R) ∣ φ ↔ ∀ m : σ →₀ ℕ, m s = 0 → coeff R m φ = 0 := begin rw [← pow_one (X s : mv_power_series σ R), X_pow_dvd_iff], split; intros h m hm, { exact h m (hm.symm ▸ zero_lt_one) }, { exact h m (nat.eq_zero_of_le_zero $ nat.le_of_succ_le_succ hm) } end end comm_semiring section ring variables [ring R] /- The inverse of a multivariate formal power series is defined by well-founded recursion on the coeffients of the inverse. -/ /-- Auxiliary definition that unifies the totalised inverse formal power series `(_)⁻¹` and the inverse formal power series that depends on an inverse of the constant coefficient `inv_of_unit`.-/ protected noncomputable def inv.aux (a : R) (φ : mv_power_series σ R) : mv_power_series σ R | n := if n = 0 then a else - a * ∑ x in n.antidiagonal, if h : x.2 < n then coeff R x.1 φ * inv.aux x.2 else 0 using_well_founded { rel_tac := λ _ _, `[exact ⟨_, finsupp.lt_wf σ⟩], dec_tac := tactic.assumption } lemma coeff_inv_aux [decidable_eq σ] (n : σ →₀ ℕ) (a : R) (φ : mv_power_series σ R) : coeff R n (inv.aux a φ) = if n = 0 then a else - a * ∑ x in n.antidiagonal, if x.2 < n then coeff R x.1 φ * coeff R x.2 (inv.aux a φ) else 0 := show inv.aux a φ n = _, begin rw inv.aux, convert rfl -- unify `decidable` instances end /-- A multivariate formal power series is invertible if the constant coefficient is invertible.-/ def inv_of_unit (φ : mv_power_series σ R) (u : Rˣ) : mv_power_series σ R := inv.aux (↑u⁻¹) φ lemma coeff_inv_of_unit [decidable_eq σ] (n : σ →₀ ℕ) (φ : mv_power_series σ R) (u : Rˣ) : coeff R n (inv_of_unit φ u) = if n = 0 then ↑u⁻¹ else - ↑u⁻¹ * ∑ x in n.antidiagonal, if x.2 < n then coeff R x.1 φ * coeff R x.2 (inv_of_unit φ u) else 0 := coeff_inv_aux n (↑u⁻¹) φ @[simp] lemma constant_coeff_inv_of_unit (φ : mv_power_series σ R) (u : Rˣ) : constant_coeff σ R (inv_of_unit φ u) = ↑u⁻¹ := by rw [← coeff_zero_eq_constant_coeff_apply, coeff_inv_of_unit, if_pos rfl] lemma mul_inv_of_unit (φ : mv_power_series σ R) (u : Rˣ) (h : constant_coeff σ R φ = u) : φ * inv_of_unit φ u = 1 := ext $ λ n, if H : n = 0 then by { rw H, simp [coeff_mul, support_single_ne_zero, h], } else begin have : ((0 : σ →₀ ℕ), n) ∈ n.antidiagonal, { rw [finsupp.mem_antidiagonal, zero_add] }, rw [coeff_one, if_neg H, coeff_mul, ← finset.insert_erase this, finset.sum_insert (finset.not_mem_erase _ _), coeff_zero_eq_constant_coeff_apply, h, coeff_inv_of_unit, if_neg H, neg_mul, mul_neg, units.mul_inv_cancel_left, ← finset.insert_erase this, finset.sum_insert (finset.not_mem_erase _ _), finset.insert_erase this, if_neg (not_lt_of_ge $ le_rfl), zero_add, add_comm, ← sub_eq_add_neg, sub_eq_zero, finset.sum_congr rfl], rintros ⟨i,j⟩ hij, rw [finset.mem_erase, finsupp.mem_antidiagonal] at hij, cases hij with h₁ h₂, subst n, rw if_pos, suffices : (0 : _) + j < i + j, {simpa}, apply add_lt_add_right, split, { intro s, exact nat.zero_le _ }, { intro H, apply h₁, suffices : i = 0, {simp [this]}, ext1 s, exact nat.eq_zero_of_le_zero (H s) } end end ring section comm_ring variable [comm_ring R] /-- Multivariate formal power series over a local ring form a local ring. -/ instance [local_ring R] : local_ring (mv_power_series σ R) := local_ring.of_is_unit_or_is_unit_one_sub_self $ by { intro φ, rcases local_ring.is_unit_or_is_unit_one_sub_self (constant_coeff σ R φ) with ⟨u,h⟩|⟨u,h⟩; [left, right]; { refine is_unit_of_mul_eq_one _ _ (mul_inv_of_unit _ u _), simpa using h.symm } } -- TODO(jmc): once adic topology lands, show that this is complete end comm_ring section local_ring variables {S : Type*} [comm_ring R] [comm_ring S] (f : R →+* S) [is_local_ring_hom f] -- Thanks to the linter for informing us that this instance does -- not actually need R and S to be local rings! /-- The map `A[[X]] → B[[X]]` induced by a local ring hom `A → B` is local -/ instance map.is_local_ring_hom : is_local_ring_hom (map σ f) := ⟨begin rintros φ ⟨ψ, h⟩, replace h := congr_arg (constant_coeff σ S) h, rw constant_coeff_map at h, have : is_unit (constant_coeff σ S ↑ψ) := @is_unit_constant_coeff σ S _ (↑ψ) ψ.is_unit, rw h at this, rcases is_unit_of_map_unit f _ this with ⟨c, hc⟩, exact is_unit_of_mul_eq_one φ (inv_of_unit φ c) (mul_inv_of_unit φ c hc.symm) end⟩ end local_ring section field variables {k : Type*} [field k] /-- The inverse `1/f` of a multivariable power series `f` over a field -/ protected def inv (φ : mv_power_series σ k) : mv_power_series σ k := inv.aux (constant_coeff σ k φ)⁻¹ φ instance : has_inv (mv_power_series σ k) := ⟨mv_power_series.inv⟩ lemma coeff_inv [decidable_eq σ] (n : σ →₀ ℕ) (φ : mv_power_series σ k) : coeff k n (φ⁻¹) = if n = 0 then (constant_coeff σ k φ)⁻¹ else - (constant_coeff σ k φ)⁻¹ * ∑ x in n.antidiagonal, if x.2 < n then coeff k x.1 φ * coeff k x.2 (φ⁻¹) else 0 := coeff_inv_aux n _ φ @[simp] lemma constant_coeff_inv (φ : mv_power_series σ k) : constant_coeff σ k (φ⁻¹) = (constant_coeff σ k φ)⁻¹ := by rw [← coeff_zero_eq_constant_coeff_apply, coeff_inv, if_pos rfl] lemma inv_eq_zero {φ : mv_power_series σ k} : φ⁻¹ = 0 ↔ constant_coeff σ k φ = 0 := ⟨λ h, by simpa using congr_arg (constant_coeff σ k) h, λ h, ext $ λ n, by { rw coeff_inv, split_ifs; simp only [h, mv_power_series.coeff_zero, zero_mul, inv_zero, neg_zero] }⟩ @[simp] lemma zero_inv : (0 : mv_power_series σ k)⁻¹ = 0 := by rw [inv_eq_zero, constant_coeff_zero] @[simp, priority 1100] lemma inv_of_unit_eq (φ : mv_power_series σ k) (h : constant_coeff σ k φ ≠ 0) : inv_of_unit φ (units.mk0 _ h) = φ⁻¹ := rfl @[simp] lemma inv_of_unit_eq' (φ : mv_power_series σ k) (u : units k) (h : constant_coeff σ k φ = u) : inv_of_unit φ u = φ⁻¹ := begin rw ← inv_of_unit_eq φ (h.symm ▸ u.ne_zero), congr' 1, rw [units.ext_iff], exact h.symm, end @[simp] protected lemma mul_inv_cancel (φ : mv_power_series σ k) (h : constant_coeff σ k φ ≠ 0) : φ * φ⁻¹ = 1 := by rw [← inv_of_unit_eq φ h, mul_inv_of_unit φ (units.mk0 _ h) rfl] @[simp] protected lemma inv_mul_cancel (φ : mv_power_series σ k) (h : constant_coeff σ k φ ≠ 0) : φ⁻¹ * φ = 1 := by rw [mul_comm, φ.mul_inv_cancel h] protected lemma eq_mul_inv_iff_mul_eq {φ₁ φ₂ φ₃ : mv_power_series σ k} (h : constant_coeff σ k φ₃ ≠ 0) : φ₁ = φ₂ * φ₃⁻¹ ↔ φ₁ * φ₃ = φ₂ := ⟨λ k, by simp [k, mul_assoc, mv_power_series.inv_mul_cancel _ h], λ k, by simp [← k, mul_assoc, mv_power_series.mul_inv_cancel _ h]⟩ protected lemma eq_inv_iff_mul_eq_one {φ ψ : mv_power_series σ k} (h : constant_coeff σ k ψ ≠ 0) : φ = ψ⁻¹ ↔ φ * ψ = 1 := by rw [← mv_power_series.eq_mul_inv_iff_mul_eq h, one_mul] protected lemma inv_eq_iff_mul_eq_one {φ ψ : mv_power_series σ k} (h : constant_coeff σ k ψ ≠ 0) : ψ⁻¹ = φ ↔ φ * ψ = 1 := by rw [eq_comm, mv_power_series.eq_inv_iff_mul_eq_one h] @[simp] protected lemma mul_inv_rev (φ ψ : mv_power_series σ k) : (φ * ψ)⁻¹ = ψ⁻¹ * φ⁻¹ := begin by_cases h : constant_coeff σ k (φ * ψ) = 0, { rw inv_eq_zero.mpr h, simp only [map_mul, mul_eq_zero] at h, -- we don't have `no_zero_divisors (mw_power_series σ k)` yet, cases h; simp [inv_eq_zero.mpr h] }, { rw [mv_power_series.inv_eq_iff_mul_eq_one h], simp only [not_or_distrib, map_mul, mul_eq_zero] at h, rw [←mul_assoc, mul_assoc _⁻¹, mv_power_series.inv_mul_cancel _ h.left, mul_one, mv_power_series.inv_mul_cancel _ h.right] } end @[simp] lemma inv_one : (1 : mv_power_series σ k)⁻¹ = 1 := by { rw [mv_power_series.inv_eq_iff_mul_eq_one, mul_one], simp } @[simp] lemma C_inv (r : k) : (C σ k r)⁻¹ = C σ k r⁻¹ := begin rcases eq_or_ne r 0 with rfl|hr, { simp }, rw [mv_power_series.inv_eq_iff_mul_eq_one, ←map_mul, inv_mul_cancel hr, map_one], simpa using hr end @[simp] lemma X_inv (s : σ) : (X s : mv_power_series σ k)⁻¹ = 0 := by rw [inv_eq_zero, constant_coeff_X] @[simp] lemma smul_inv (r : k) (φ : mv_power_series σ k) : (r • φ)⁻¹ = r⁻¹ • φ⁻¹ := by simp [smul_eq_C_mul, mul_comm] end field end mv_power_series namespace mv_polynomial open finsupp variables {σ : Type*} {R : Type*} [comm_semiring R] (φ ψ : mv_polynomial σ R) /-- The natural inclusion from multivariate polynomials into multivariate formal power series.-/ instance coe_to_mv_power_series : has_coe (mv_polynomial σ R) (mv_power_series σ R) := ⟨λ φ n, coeff n φ⟩ lemma coe_def : (φ : mv_power_series σ R) = λ n, coeff n φ := rfl @[simp, norm_cast] lemma coeff_coe (n : σ →₀ ℕ) : mv_power_series.coeff R n ↑φ = coeff n φ := rfl @[simp, norm_cast] lemma coe_monomial (n : σ →₀ ℕ) (a : R) : (monomial n a : mv_power_series σ R) = mv_power_series.monomial R n a := mv_power_series.ext $ λ m, begin rw [coeff_coe, coeff_monomial, mv_power_series.coeff_monomial], split_ifs with h₁ h₂; refl <|> subst m; contradiction end @[simp, norm_cast] lemma coe_zero : ((0 : mv_polynomial σ R) : mv_power_series σ R) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : mv_polynomial σ R) : mv_power_series σ R) = 1 := coe_monomial _ _ @[simp, norm_cast] lemma coe_add : ((φ + ψ : mv_polynomial σ R) : mv_power_series σ R) = φ + ψ := rfl @[simp, norm_cast] lemma coe_mul : ((φ * ψ : mv_polynomial σ R) : mv_power_series σ R) = φ * ψ := mv_power_series.ext $ λ n, by simp only [coeff_coe, mv_power_series.coeff_mul, coeff_mul] @[simp, norm_cast] lemma coe_C (a : R) : ((C a : mv_polynomial σ R) : mv_power_series σ R) = mv_power_series.C σ R a := coe_monomial _ _ @[simp, norm_cast] lemma coe_bit0 : ((bit0 φ : mv_polynomial σ R) : mv_power_series σ R) = bit0 (φ : mv_power_series σ R) := coe_add _ _ @[simp, norm_cast] lemma coe_bit1 : ((bit1 φ : mv_polynomial σ R) : mv_power_series σ R) = bit1 (φ : mv_power_series σ R) := by rw [bit1, bit1, coe_add, coe_one, coe_bit0] @[simp, norm_cast] lemma coe_X (s : σ) : ((X s : mv_polynomial σ R) : mv_power_series σ R) = mv_power_series.X s := coe_monomial _ _ variables (σ R) lemma coe_injective : function.injective (coe : mv_polynomial σ R → mv_power_series σ R) := λ x y h, by { ext, simp_rw [←coeff_coe, h] } variables {σ R φ ψ} @[simp, norm_cast] lemma coe_inj : (φ : mv_power_series σ R) = ψ ↔ φ = ψ := (coe_injective σ R).eq_iff @[simp] lemma coe_eq_zero_iff : (φ : mv_power_series σ R) = 0 ↔ φ = 0 := by rw [←coe_zero, coe_inj] @[simp] lemma coe_eq_one_iff : (φ : mv_power_series σ R) = 1 ↔ φ = 1 := by rw [←coe_one, coe_inj] /-- The coercion from multivariable polynomials to multivariable power series as a ring homomorphism. -/ def coe_to_mv_power_series.ring_hom : mv_polynomial σ R →+* mv_power_series σ R := { to_fun := (coe : mv_polynomial σ R → mv_power_series σ R), map_zero' := coe_zero, map_one' := coe_one, map_add' := coe_add, map_mul' := coe_mul } @[simp, norm_cast] lemma coe_pow (n : ℕ) : ((φ ^ n : mv_polynomial σ R) : mv_power_series σ R) = (φ : mv_power_series σ R) ^ n := coe_to_mv_power_series.ring_hom.map_pow _ _ variables (φ ψ) @[simp] lemma coe_to_mv_power_series.ring_hom_apply : coe_to_mv_power_series.ring_hom φ = φ := rfl section algebra variables (A : Type*) [comm_semiring A] [algebra R A] lemma algebra_map_apply (r : R) : algebra_map R (mv_polynomial σ A) r = C (algebra_map R A r) := rfl /-- The coercion from multivariable polynomials to multivariable power series as an algebra homomorphism. -/ def coe_to_mv_power_series.alg_hom : mv_polynomial σ R →ₐ[R] mv_power_series σ A := { commutes' := λ r, by simp [algebra_map_apply, mv_power_series.algebra_map_apply], ..(mv_power_series.map σ (algebra_map R A)).comp coe_to_mv_power_series.ring_hom} @[simp] lemma coe_to_mv_power_series.alg_hom_apply : (coe_to_mv_power_series.alg_hom A φ) = mv_power_series.map σ (algebra_map R A) ↑φ := rfl end algebra end mv_polynomial namespace mv_power_series variables {σ R A : Type*} [comm_semiring R] [comm_semiring A] [algebra R A] (f : mv_power_series σ R) instance algebra_mv_polynomial : algebra (mv_polynomial σ R) (mv_power_series σ A) := ring_hom.to_algebra (mv_polynomial.coe_to_mv_power_series.alg_hom A).to_ring_hom instance algebra_mv_power_series : algebra (mv_power_series σ R) (mv_power_series σ A) := (map σ (algebra_map R A)).to_algebra variables (A) lemma algebra_map_apply' (p : mv_polynomial σ R): algebra_map (mv_polynomial σ R) (mv_power_series σ A) p = map σ (algebra_map R A) p := rfl lemma algebra_map_apply'' : algebra_map (mv_power_series σ R) (mv_power_series σ A) f = map σ (algebra_map R A) f := rfl end mv_power_series /-- Formal power series over the coefficient ring `R`.-/ def power_series (R : Type*) := mv_power_series unit R namespace power_series open finsupp (single) variable {R : Type*} section local attribute [reducible] power_series instance [inhabited R] : inhabited (power_series R) := by apply_instance instance [add_monoid R] : add_monoid (power_series R) := by apply_instance instance [add_group R] : add_group (power_series R) := by apply_instance instance [add_comm_monoid R] : add_comm_monoid (power_series R) := by apply_instance instance [add_comm_group R] : add_comm_group (power_series R) := by apply_instance instance [semiring R] : semiring (power_series R) := by apply_instance instance [comm_semiring R] : comm_semiring (power_series R) := by apply_instance instance [ring R] : ring (power_series R) := by apply_instance instance [comm_ring R] : comm_ring (power_series R) := by apply_instance instance [nontrivial R] : nontrivial (power_series R) := by apply_instance instance {A} [semiring R] [add_comm_monoid A] [module R A] : module R (power_series A) := by apply_instance instance {A S} [semiring R] [semiring S] [add_comm_monoid A] [module R A] [module S A] [has_smul R S] [is_scalar_tower R S A] : is_scalar_tower R S (power_series A) := pi.is_scalar_tower instance {A} [semiring A] [comm_semiring R] [algebra R A] : algebra R (power_series A) := by apply_instance end section semiring variables (R) [semiring R] /-- The `n`th coefficient of a formal power series.-/ def coeff (n : ℕ) : power_series R →ₗ[R] R := mv_power_series.coeff R (single () n) /-- The `n`th monomial with coefficient `a` as formal power series.-/ def monomial (n : ℕ) : R →ₗ[R] power_series R := mv_power_series.monomial R (single () n) variables {R} lemma coeff_def {s : unit →₀ ℕ} {n : ℕ} (h : s () = n) : coeff R n = mv_power_series.coeff R s := by erw [coeff, ← h, ← finsupp.unique_single s] /-- Two formal power series are equal if all their coefficients are equal.-/ @[ext] lemma ext {φ ψ : power_series R} (h : ∀ n, coeff R n φ = coeff R n ψ) : φ = ψ := mv_power_series.ext $ λ n, by { rw ← coeff_def, { apply h }, refl } /-- Two formal power series are equal if all their coefficients are equal.-/ lemma ext_iff {φ ψ : power_series R} : φ = ψ ↔ (∀ n, coeff R n φ = coeff R n ψ) := ⟨λ h n, congr_arg (coeff R n) h, ext⟩ /-- Constructor for formal power series.-/ def mk {R} (f : ℕ → R) : power_series R := λ s, f (s ()) @[simp] lemma coeff_mk (n : ℕ) (f : ℕ → R) : coeff R n (mk f) = f n := congr_arg f finsupp.single_eq_same lemma coeff_monomial (m n : ℕ) (a : R) : coeff R m (monomial R n a) = if m = n then a else 0 := calc coeff R m (monomial R n a) = _ : mv_power_series.coeff_monomial _ _ _ ... = if m = n then a else 0 : by simp only [finsupp.unique_single_eq_iff] lemma monomial_eq_mk (n : ℕ) (a : R) : monomial R n a = mk (λ m, if m = n then a else 0) := ext $ λ m, by { rw [coeff_monomial, coeff_mk] } @[simp] lemma coeff_monomial_same (n : ℕ) (a : R) : coeff R n (monomial R n a) = a := mv_power_series.coeff_monomial_same _ _ @[simp] lemma coeff_comp_monomial (n : ℕ) : (coeff R n).comp (monomial R n) = linear_map.id := linear_map.ext $ coeff_monomial_same n variable (R) /--The constant coefficient of a formal power series. -/ def constant_coeff : power_series R →+* R := mv_power_series.constant_coeff unit R /-- The constant formal power series.-/ def C : R →+* power_series R := mv_power_series.C unit R variable {R} /-- The variable of the formal power series ring.-/ def X : power_series R := mv_power_series.X () @[simp] lemma coeff_zero_eq_constant_coeff : ⇑(coeff R 0) = constant_coeff R := by { rw [coeff, finsupp.single_zero], refl } lemma coeff_zero_eq_constant_coeff_apply (φ : power_series R) : coeff R 0 φ = constant_coeff R φ := by rw [coeff_zero_eq_constant_coeff]; refl @[simp] lemma monomial_zero_eq_C : ⇑(monomial R 0) = C R := by rw [monomial, finsupp.single_zero, mv_power_series.monomial_zero_eq_C, C] lemma monomial_zero_eq_C_apply (a : R) : monomial R 0 a = C R a := by simp lemma coeff_C (n : ℕ) (a : R) : coeff R n (C R a : power_series R) = if n = 0 then a else 0 := by rw [← monomial_zero_eq_C_apply, coeff_monomial] @[simp] lemma coeff_zero_C (a : R) : coeff R 0 (C R a) = a := by rw [← monomial_zero_eq_C_apply, coeff_monomial_same 0 a] lemma X_eq : (X : power_series R) = monomial R 1 1 := rfl lemma coeff_X (n : ℕ) : coeff R n (X : power_series R) = if n = 1 then 1 else 0 := by rw [X_eq, coeff_monomial] @[simp] lemma coeff_zero_X : coeff R 0 (X : power_series R) = 0 := by rw [coeff, finsupp.single_zero, X, mv_power_series.coeff_zero_X] @[simp] lemma coeff_one_X : coeff R 1 (X : power_series R) = 1 := by rw [coeff_X, if_pos rfl] @[simp] lemma X_ne_zero [nontrivial R] : (X : power_series R) ≠ 0 := λ H, by simpa only [coeff_one_X, one_ne_zero, map_zero] using congr_arg (coeff R 1) H lemma X_pow_eq (n : ℕ) : (X : power_series R)^n = monomial R n 1 := mv_power_series.X_pow_eq _ n lemma coeff_X_pow (m n : ℕ) : coeff R m ((X : power_series R)^n) = if m = n then 1 else 0 := by rw [X_pow_eq, coeff_monomial] @[simp] lemma coeff_X_pow_self (n : ℕ) : coeff R n ((X : power_series R)^n) = 1 := by rw [coeff_X_pow, if_pos rfl] @[simp] lemma coeff_one (n : ℕ) : coeff R n (1 : power_series R) = if n = 0 then 1 else 0 := coeff_C n 1 lemma coeff_zero_one : coeff R 0 (1 : power_series R) = 1 := coeff_zero_C 1 lemma coeff_mul (n : ℕ) (φ ψ : power_series R) : coeff R n (φ * ψ) = ∑ p in finset.nat.antidiagonal n, coeff R p.1 φ * coeff R p.2 ψ := begin symmetry, apply finset.sum_bij (λ (p : ℕ × ℕ) h, (single () p.1, single () p.2)), { rintros ⟨i,j⟩ hij, rw finset.nat.mem_antidiagonal at hij, rw [finsupp.mem_antidiagonal, ← finsupp.single_add, hij], }, { rintros ⟨i,j⟩ hij, refl }, { rintros ⟨i,j⟩ ⟨k,l⟩ hij hkl, simpa only [prod.mk.inj_iff, finsupp.unique_single_eq_iff] using id }, { rintros ⟨f,g⟩ hfg, refine ⟨(f (), g ()), _, _⟩, { rw finsupp.mem_antidiagonal at hfg, rw [finset.nat.mem_antidiagonal, ← finsupp.add_apply, hfg, finsupp.single_eq_same] }, { rw prod.mk.inj_iff, dsimp, exact ⟨finsupp.unique_single f, finsupp.unique_single g⟩ } } end @[simp] lemma coeff_mul_C (n : ℕ) (φ : power_series R) (a : R) : coeff R n (φ * C R a) = coeff R n φ * a := mv_power_series.coeff_mul_C _ φ a @[simp] lemma coeff_C_mul (n : ℕ) (φ : power_series R) (a : R) : coeff R n (C R a * φ) = a * coeff R n φ := mv_power_series.coeff_C_mul _ φ a @[simp] lemma coeff_smul {S : Type*} [semiring S] [module R S] (n : ℕ) (φ : power_series S) (a : R) : coeff S n (a • φ) = a • coeff S n φ := rfl lemma smul_eq_C_mul (f : power_series R) (a : R) : a • f = C R a * f := by { ext, simp } @[simp] lemma coeff_succ_mul_X (n : ℕ) (φ : power_series R) : coeff R (n+1) (φ * X) = coeff R n φ := begin simp only [coeff, finsupp.single_add], convert φ.coeff_add_mul_monomial (single () n) (single () 1) _, rw mul_one end @[simp] lemma coeff_succ_X_mul (n : ℕ) (φ : power_series R) : coeff R (n + 1) (X * φ) = coeff R n φ := begin simp only [coeff, finsupp.single_add, add_comm n 1], convert φ.coeff_add_monomial_mul (single () 1) (single () n) _, rw one_mul, end @[simp] lemma constant_coeff_C (a : R) : constant_coeff R (C R a) = a := rfl @[simp] lemma constant_coeff_comp_C : (constant_coeff R).comp (C R) = ring_hom.id R := rfl @[simp] lemma constant_coeff_zero : constant_coeff R 0 = 0 := rfl @[simp] lemma constant_coeff_one : constant_coeff R 1 = 1 := rfl @[simp] lemma constant_coeff_X : constant_coeff R X = 0 := mv_power_series.coeff_zero_X _ lemma coeff_zero_mul_X (φ : power_series R) : coeff R 0 (φ * X) = 0 := by simp lemma coeff_zero_X_mul (φ : power_series R) : coeff R 0 (X * φ) = 0 := by simp -- The following section duplicates the api of `data.polynomial.coeff` and should attempt to keep -- up to date with that section lemma coeff_C_mul_X_pow (x : R) (k n : ℕ) : coeff R n (C R x * X ^ k : power_series R) = if n = k then x else 0 := by simp [X_pow_eq, coeff_monomial] @[simp] theorem coeff_mul_X_pow (p : power_series R) (n d : ℕ) : coeff R (d + n) (p * X ^ n) = coeff R d p := begin rw [coeff_mul, finset.sum_eq_single (d, n), coeff_X_pow, if_pos rfl, mul_one], { rintros ⟨i,j⟩ h1 h2, rw [coeff_X_pow, if_neg, mul_zero], rintro rfl, apply h2, rw [finset.nat.mem_antidiagonal, add_right_cancel_iff] at h1, subst h1 }, { exact λ h1, (h1 (finset.nat.mem_antidiagonal.2 rfl)).elim } end @[simp] theorem coeff_X_pow_mul (p : power_series R) (n d : ℕ) : coeff R (d + n) (X ^ n * p) = coeff R d p := begin rw [coeff_mul, finset.sum_eq_single (n,d), coeff_X_pow, if_pos rfl, one_mul], { rintros ⟨i,j⟩ h1 h2, rw [coeff_X_pow, if_neg, zero_mul], rintro rfl, apply h2, rw [finset.nat.mem_antidiagonal, add_comm, add_right_cancel_iff] at h1, subst h1 }, { rw add_comm, exact λ h1, (h1 (finset.nat.mem_antidiagonal.2 rfl)).elim } end lemma coeff_mul_X_pow' (p : power_series R) (n d : ℕ) : coeff R d (p * X ^ n) = ite (n ≤ d) (coeff R (d - n) p) 0 := begin split_ifs, { rw [← tsub_add_cancel_of_le h, coeff_mul_X_pow, add_tsub_cancel_right] }, { refine (coeff_mul _ _ _).trans (finset.sum_eq_zero (λ x hx, _)), rw [coeff_X_pow, if_neg, mul_zero], exact ((le_of_add_le_right (finset.nat.mem_antidiagonal.mp hx).le).trans_lt $ not_le.mp h).ne } end lemma coeff_X_pow_mul' (p : power_series R) (n d : ℕ) : coeff R d (X ^ n * p) = ite (n ≤ d) (coeff R (d - n) p) 0 := begin split_ifs, { rw [← tsub_add_cancel_of_le h, coeff_X_pow_mul], simp, }, { refine (coeff_mul _ _ _).trans (finset.sum_eq_zero (λ x hx, _)), rw [coeff_X_pow, if_neg, zero_mul], have := finset.nat.mem_antidiagonal.mp hx, rw add_comm at this, exact ((le_of_add_le_right this.le).trans_lt $ not_le.mp h).ne } end end /-- If a formal power series is invertible, then so is its constant coefficient.-/ lemma is_unit_constant_coeff (φ : power_series R) (h : is_unit φ) : is_unit (constant_coeff R φ) := mv_power_series.is_unit_constant_coeff φ h /-- Split off the constant coefficient. -/ lemma eq_shift_mul_X_add_const (φ : power_series R) : φ = mk (λ p, coeff R (p + 1) φ) * X + C R (constant_coeff R φ) := begin ext (_ | n), { simp only [ring_hom.map_add, constant_coeff_C, constant_coeff_X, coeff_zero_eq_constant_coeff, zero_add, mul_zero, ring_hom.map_mul], }, { simp only [coeff_succ_mul_X, coeff_mk, linear_map.map_add, coeff_C, n.succ_ne_zero, sub_zero, if_false, add_zero], } end /-- Split off the constant coefficient. -/ lemma eq_X_mul_shift_add_const (φ : power_series R) : φ = X * mk (λ p, coeff R (p + 1) φ) + C R (constant_coeff R φ) := begin ext (_ | n), { simp only [ring_hom.map_add, constant_coeff_C, constant_coeff_X, coeff_zero_eq_constant_coeff, zero_add, zero_mul, ring_hom.map_mul], }, { simp only [coeff_succ_X_mul, coeff_mk, linear_map.map_add, coeff_C, n.succ_ne_zero, sub_zero, if_false, add_zero], } end section map variables {S : Type*} {T : Type*} [semiring S] [semiring T] variables (f : R →+* S) (g : S →+* T) /-- The map between formal power series induced by a map on the coefficients.-/ def map : power_series R →+* power_series S := mv_power_series.map _ f @[simp] lemma map_id : (map (ring_hom.id R) : power_series R → power_series R) = id := rfl lemma map_comp : map (g.comp f) = (map g).comp (map f) := rfl @[simp] lemma coeff_map (n : ℕ) (φ : power_series R) : coeff S n (map f φ) = f (coeff R n φ) := rfl @[simp] lemma map_C (r : R) : map f (C _ r) = C _ (f r) := by { ext, simp [coeff_C, apply_ite f] } @[simp] lemma map_X : map f X = X := by { ext, simp [coeff_X, apply_ite f] } end map end semiring section comm_semiring variables [comm_semiring R] lemma X_pow_dvd_iff {n : ℕ} {φ : power_series R} : (X : power_series R)^n ∣ φ ↔ ∀ m, m < n → coeff R m φ = 0 := begin convert @mv_power_series.X_pow_dvd_iff unit R _ () n φ, apply propext, classical, split; intros h m hm, { rw finsupp.unique_single m, convert h _ hm }, { apply h, simpa only [finsupp.single_eq_same] using hm } end lemma X_dvd_iff {φ : power_series R} : (X : power_series R) ∣ φ ↔ constant_coeff R φ = 0 := begin rw [← pow_one (X : power_series R), X_pow_dvd_iff, ← coeff_zero_eq_constant_coeff_apply], split; intro h, { exact h 0 zero_lt_one }, { intros m hm, rwa nat.eq_zero_of_le_zero (nat.le_of_succ_le_succ hm) } end open finset nat /-- The ring homomorphism taking a power series `f(X)` to `f(aX)`. -/ noncomputable def rescale (a : R) : power_series R →+* power_series R := { to_fun := λ f, power_series.mk $ λ n, a^n * (power_series.coeff R n f), map_zero' := by { ext, simp only [linear_map.map_zero, power_series.coeff_mk, mul_zero], }, map_one' := by { ext1, simp only [mul_boole, power_series.coeff_mk, power_series.coeff_one], split_ifs, { rw [h, pow_zero], }, refl, }, map_add' := by { intros, ext, exact mul_add _ _ _, }, map_mul' := λ f g, by { ext, rw [power_series.coeff_mul, power_series.coeff_mk, power_series.coeff_mul, finset.mul_sum], apply sum_congr rfl, simp only [coeff_mk, prod.forall, nat.mem_antidiagonal], intros b c H, rw [←H, pow_add, mul_mul_mul_comm] }, } @[simp] lemma coeff_rescale (f : power_series R) (a : R) (n : ℕ) : coeff R n (rescale a f) = a^n * coeff R n f := coeff_mk n _ @[simp] lemma rescale_zero : rescale 0 = (C R).comp (constant_coeff R) := begin ext, simp only [function.comp_app, ring_hom.coe_comp, rescale, ring_hom.coe_mk, power_series.coeff_mk _ _, coeff_C], split_ifs, { simp only [h, one_mul, coeff_zero_eq_constant_coeff, pow_zero], }, { rw [zero_pow' n h, zero_mul], }, end lemma rescale_zero_apply : rescale 0 X = C R (constant_coeff R X) := by simp @[simp] lemma rescale_one : rescale 1 = ring_hom.id (power_series R) := by { ext, simp only [ring_hom.id_apply, rescale, one_pow, coeff_mk, one_mul, ring_hom.coe_mk], } lemma rescale_mk (f : ℕ → R) (a : R) : rescale a (mk f) = mk (λ n : ℕ, a^n * (f n)) := by { ext, rw [coeff_rescale, coeff_mk, coeff_mk], } lemma rescale_rescale (f : power_series R) (a b : R) : rescale b (rescale a f) = rescale (a * b) f := begin ext, repeat { rw coeff_rescale, }, rw [mul_pow, mul_comm _ (b^n), mul_assoc], end lemma rescale_mul (a b : R) : rescale (a * b) = (rescale b).comp (rescale a) := by { ext, simp [← rescale_rescale], } section trunc /-- The `n`th truncation of a formal power series to a polynomial -/ def trunc (n : ℕ) (φ : power_series R) : R[X] := ∑ m in Ico 0 n, polynomial.monomial m (coeff R m φ) lemma coeff_trunc (m) (n) (φ : power_series R) : (trunc n φ).coeff m = if m < n then coeff R m φ else 0 := by simp [trunc, polynomial.coeff_sum, polynomial.coeff_monomial, nat.lt_succ_iff] @[simp] lemma trunc_zero (n) : trunc n (0 : power_series R) = 0 := polynomial.ext $ λ m, begin rw [coeff_trunc, linear_map.map_zero, polynomial.coeff_zero], split_ifs; refl end @[simp] lemma trunc_one (n) : trunc (n + 1) (1 : power_series R) = 1 := polynomial.ext $ λ m, begin rw [coeff_trunc, coeff_one], split_ifs with H H' H'; rw [polynomial.coeff_one], { subst m, rw [if_pos rfl] }, { symmetry, exact if_neg (ne.elim (ne.symm H')) }, { symmetry, refine if_neg _, rintro rfl, apply H, exact nat.zero_lt_succ _ } end @[simp] lemma trunc_C (n) (a : R) : trunc (n + 1) (C R a) = polynomial.C a := polynomial.ext $ λ m, begin rw [coeff_trunc, coeff_C, polynomial.coeff_C], split_ifs with H; refl <|> try {simp * at *} end @[simp] lemma trunc_add (n) (φ ψ : power_series R) : trunc n (φ + ψ) = trunc n φ + trunc n ψ := polynomial.ext $ λ m, begin simp only [coeff_trunc, add_monoid_hom.map_add, polynomial.coeff_add], split_ifs with H, {refl}, {rw [zero_add]} end end trunc end comm_semiring section ring variables [ring R] /-- Auxiliary function used for computing inverse of a power series -/ protected def inv.aux : R → power_series R → power_series R := mv_power_series.inv.aux lemma coeff_inv_aux (n : ℕ) (a : R) (φ : power_series R) : coeff R n (inv.aux a φ) = if n = 0 then a else - a * ∑ x in finset.nat.antidiagonal n, if x.2 < n then coeff R x.1 φ * coeff R x.2 (inv.aux a φ) else 0 := begin rw [coeff, inv.aux, mv_power_series.coeff_inv_aux], simp only [finsupp.single_eq_zero], split_ifs, {refl}, congr' 1, symmetry, apply finset.sum_bij (λ (p : ℕ × ℕ) h, (single () p.1, single () p.2)), { rintros ⟨i,j⟩ hij, rw finset.nat.mem_antidiagonal at hij, rw [finsupp.mem_antidiagonal, ← finsupp.single_add, hij], }, { rintros ⟨i,j⟩ hij, by_cases H : j < n, { rw [if_pos H, if_pos], {refl}, split, { rintro ⟨⟩, simpa [finsupp.single_eq_same] using le_of_lt H }, { intro hh, rw lt_iff_not_ge at H, apply H, simpa [finsupp.single_eq_same] using hh () } }, { rw [if_neg H, if_neg], rintro ⟨h₁, h₂⟩, apply h₂, rintro ⟨⟩, simpa [finsupp.single_eq_same] using not_lt.1 H } }, { rintros ⟨i,j⟩ ⟨k,l⟩ hij hkl, simpa only [prod.mk.inj_iff, finsupp.unique_single_eq_iff] using id }, { rintros ⟨f,g⟩ hfg, refine ⟨(f (), g ()), _, _⟩, { rw finsupp.mem_antidiagonal at hfg, rw [finset.nat.mem_antidiagonal, ← finsupp.add_apply, hfg, finsupp.single_eq_same] }, { rw prod.mk.inj_iff, dsimp, exact ⟨finsupp.unique_single f, finsupp.unique_single g⟩ } } end /-- A formal power series is invertible if the constant coefficient is invertible.-/ def inv_of_unit (φ : power_series R) (u : Rˣ) : power_series R := mv_power_series.inv_of_unit φ u lemma coeff_inv_of_unit (n : ℕ) (φ : power_series R) (u : Rˣ) : coeff R n (inv_of_unit φ u) = if n = 0 then ↑u⁻¹ else - ↑u⁻¹ * ∑ x in finset.nat.antidiagonal n, if x.2 < n then coeff R x.1 φ * coeff R x.2 (inv_of_unit φ u) else 0 := coeff_inv_aux n ↑u⁻¹ φ @[simp] lemma constant_coeff_inv_of_unit (φ : power_series R) (u : Rˣ) : constant_coeff R (inv_of_unit φ u) = ↑u⁻¹ := by rw [← coeff_zero_eq_constant_coeff_apply, coeff_inv_of_unit, if_pos rfl] lemma mul_inv_of_unit (φ : power_series R) (u : Rˣ) (h : constant_coeff R φ = u) : φ * inv_of_unit φ u = 1 := mv_power_series.mul_inv_of_unit φ u $ h /-- Two ways of removing the constant coefficient of a power series are the same. -/ lemma sub_const_eq_shift_mul_X (φ : power_series R) : φ - C R (constant_coeff R φ) = power_series.mk (λ p, coeff R (p + 1) φ) * X := sub_eq_iff_eq_add.mpr (eq_shift_mul_X_add_const φ) lemma sub_const_eq_X_mul_shift (φ : power_series R) : φ - C R (constant_coeff R φ) = X * power_series.mk (λ p, coeff R (p + 1) φ) := sub_eq_iff_eq_add.mpr (eq_X_mul_shift_add_const φ) end ring section comm_ring variables {A : Type*} [comm_ring A] @[simp] lemma rescale_X (a : A) : rescale a X = C A a * X := begin ext, simp only [coeff_rescale, coeff_C_mul, coeff_X], split_ifs with h; simp [h], end lemma rescale_neg_one_X : rescale (-1 : A) X = -X := by rw [rescale_X, map_neg, map_one, neg_one_mul] /-- The ring homomorphism taking a power series `f(X)` to `f(-X)`. -/ noncomputable def eval_neg_hom : power_series A →+* power_series A := rescale (-1 : A) @[simp] lemma eval_neg_hom_X : eval_neg_hom (X : power_series A) = -X := rescale_neg_one_X end comm_ring section domain variables [ring R] [is_domain R] lemma eq_zero_or_eq_zero_of_mul_eq_zero (φ ψ : power_series R) (h : φ * ψ = 0) : φ = 0 ∨ ψ = 0 := begin rw or_iff_not_imp_left, intro H, have ex : ∃ m, coeff R m φ ≠ 0, { contrapose! H, exact ext H }, let m := nat.find ex, have hm₁ : coeff R m φ ≠ 0 := nat.find_spec ex, have hm₂ : ∀ k < m, ¬coeff R k φ ≠ 0 := λ k, nat.find_min ex, ext n, rw (coeff R n).map_zero, apply nat.strong_induction_on n, clear n, intros n ih, replace h := congr_arg (coeff R (m + n)) h, rw [linear_map.map_zero, coeff_mul, finset.sum_eq_single (m,n)] at h, { replace h := eq_zero_or_eq_zero_of_mul_eq_zero h, rw or_iff_not_imp_left at h, exact h hm₁ }, { rintro ⟨i,j⟩ hij hne, by_cases hj : j < n, { rw [ih j hj, mul_zero] }, by_cases hi : i < m, { specialize hm₂ _ hi, push_neg at hm₂, rw [hm₂, zero_mul] }, rw finset.nat.mem_antidiagonal at hij, push_neg at hi hj, suffices : m < i, { have : m + n < i + j := add_lt_add_of_lt_of_le this hj, exfalso, exact ne_of_lt this hij.symm }, contrapose! hne, obtain rfl := le_antisymm hi hne, simpa [ne.def, prod.mk.inj_iff] using (add_right_inj m).mp hij }, { contrapose!, intro h, rw finset.nat.mem_antidiagonal } end instance : is_domain (power_series R) := { eq_zero_or_eq_zero_of_mul_eq_zero := eq_zero_or_eq_zero_of_mul_eq_zero, .. power_series.nontrivial, } end domain section is_domain variables [comm_ring R] [is_domain R] /-- The ideal spanned by the variable in the power series ring over an integral domain is a prime ideal.-/ lemma span_X_is_prime : (ideal.span ({X} : set (power_series R))).is_prime := begin suffices : ideal.span ({X} : set (power_series R)) = (constant_coeff R).ker, { rw this, exact ring_hom.ker_is_prime _ }, apply ideal.ext, intro φ, rw [ring_hom.mem_ker, ideal.mem_span_singleton, X_dvd_iff] end /-- The variable of the power series ring over an integral domain is prime.-/ lemma X_prime : prime (X : power_series R) := begin rw ← ideal.span_singleton_prime, { exact span_X_is_prime }, { intro h, simpa using congr_arg (coeff R 1) h } end lemma rescale_injective {a : R} (ha : a ≠ 0) : function.injective (rescale a) := begin intros p q h, rw power_series.ext_iff at *, intros n, specialize h n, rw [coeff_rescale, coeff_rescale, mul_eq_mul_left_iff] at h, apply h.resolve_right, intro h', exact ha (pow_eq_zero h'), end end is_domain section local_ring variables {S : Type*} [comm_ring R] [comm_ring S] (f : R →+* S) [is_local_ring_hom f] instance map.is_local_ring_hom : is_local_ring_hom (map f) := mv_power_series.map.is_local_ring_hom f variables [local_ring R] [local_ring S] instance : local_ring (power_series R) := mv_power_series.local_ring end local_ring section algebra variables {A : Type*} [comm_semiring R] [semiring A] [algebra R A] theorem C_eq_algebra_map {r : R} : C R r = (algebra_map R (power_series R)) r := rfl theorem algebra_map_apply {r : R} : algebra_map R (power_series A) r = C A (algebra_map R A r) := mv_power_series.algebra_map_apply instance [nontrivial R] : nontrivial (subalgebra R (power_series R)) := mv_power_series.subalgebra.nontrivial end algebra section field variables {k : Type*} [field k] /-- The inverse 1/f of a power series f defined over a field -/ protected def inv : power_series k → power_series k := mv_power_series.inv instance : has_inv (power_series k) := ⟨power_series.inv⟩ lemma inv_eq_inv_aux (φ : power_series k) : φ⁻¹ = inv.aux (constant_coeff k φ)⁻¹ φ := rfl lemma coeff_inv (n) (φ : power_series k) : coeff k n (φ⁻¹) = if n = 0 then (constant_coeff k φ)⁻¹ else - (constant_coeff k φ)⁻¹ * ∑ x in finset.nat.antidiagonal n, if x.2 < n then coeff k x.1 φ * coeff k x.2 (φ⁻¹) else 0 := by rw [inv_eq_inv_aux, coeff_inv_aux n (constant_coeff k φ)⁻¹ φ] @[simp] lemma constant_coeff_inv (φ : power_series k) : constant_coeff k (φ⁻¹) = (constant_coeff k φ)⁻¹ := mv_power_series.constant_coeff_inv φ lemma inv_eq_zero {φ : power_series k} : φ⁻¹ = 0 ↔ constant_coeff k φ = 0 := mv_power_series.inv_eq_zero @[simp] lemma zero_inv : (0 : power_series k)⁻¹ = 0 := mv_power_series.zero_inv @[simp, priority 1100] lemma inv_of_unit_eq (φ : power_series k) (h : constant_coeff k φ ≠ 0) : inv_of_unit φ (units.mk0 _ h) = φ⁻¹ := mv_power_series.inv_of_unit_eq _ _ @[simp] lemma inv_of_unit_eq' (φ : power_series k) (u : units k) (h : constant_coeff k φ = u) : inv_of_unit φ u = φ⁻¹ := mv_power_series.inv_of_unit_eq' φ _ h @[simp] protected lemma mul_inv_cancel (φ : power_series k) (h : constant_coeff k φ ≠ 0) : φ * φ⁻¹ = 1 := mv_power_series.mul_inv_cancel φ h @[simp] protected lemma inv_mul_cancel (φ : power_series k) (h : constant_coeff k φ ≠ 0) : φ⁻¹ * φ = 1 := mv_power_series.inv_mul_cancel φ h lemma eq_mul_inv_iff_mul_eq {φ₁ φ₂ φ₃ : power_series k} (h : constant_coeff k φ₃ ≠ 0) : φ₁ = φ₂ * φ₃⁻¹ ↔ φ₁ * φ₃ = φ₂ := mv_power_series.eq_mul_inv_iff_mul_eq h lemma eq_inv_iff_mul_eq_one {φ ψ : power_series k} (h : constant_coeff k ψ ≠ 0) : φ = ψ⁻¹ ↔ φ * ψ = 1 := mv_power_series.eq_inv_iff_mul_eq_one h lemma inv_eq_iff_mul_eq_one {φ ψ : power_series k} (h : constant_coeff k ψ ≠ 0) : ψ⁻¹ = φ ↔ φ * ψ = 1 := mv_power_series.inv_eq_iff_mul_eq_one h @[simp] protected lemma mul_inv_rev (φ ψ : power_series k) : (φ * ψ)⁻¹ = ψ⁻¹ * φ⁻¹ := mv_power_series.mul_inv_rev _ _ @[simp] lemma inv_one : (1 : power_series k)⁻¹ = 1 := mv_power_series.inv_one @[simp] lemma C_inv (r : k) : (C k r)⁻¹ = C k r⁻¹ := mv_power_series.C_inv _ @[simp] lemma X_inv : (X : power_series k)⁻¹ = 0 := mv_power_series.X_inv _ @[simp] lemma smul_inv (r : k) (φ : power_series k) : (r • φ)⁻¹ = r⁻¹ • φ⁻¹ := mv_power_series.smul_inv _ _ end field end power_series namespace power_series variable {R : Type*} local attribute [instance, priority 1] classical.prop_decidable noncomputable theory section order_basic open multiplicity variables [semiring R] {φ : power_series R} lemma exists_coeff_ne_zero_iff_ne_zero : (∃ (n : ℕ), coeff R n φ ≠ 0) ↔ φ ≠ 0 := begin refine not_iff_not.mp _, push_neg, simp [power_series.ext_iff] end /-- The order of a formal power series `φ` is the greatest `n : part_enat` such that `X^n` divides `φ`. The order is `⊤` if and only if `φ = 0`. -/ def order (φ : power_series R) : part_enat := if h : φ = 0 then ⊤ else nat.find (exists_coeff_ne_zero_iff_ne_zero.mpr h) /-- The order of the `0` power series is infinite.-/ @[simp] lemma order_zero : order (0 : power_series R) = ⊤ := dif_pos rfl lemma order_finite_iff_ne_zero : (order φ).dom ↔ φ ≠ 0 := begin simp only [order], split, { split_ifs with h h; intro H, { contrapose! H, simpa [←part.eq_none_iff'] }, { exact h } }, { intro h, simp [h] } end /-- If the order of a formal power series is finite, then the coefficient indexed by the order is nonzero.-/ lemma coeff_order (h : (order φ).dom) : coeff R (φ.order.get h) φ ≠ 0 := begin simp only [order, order_finite_iff_ne_zero.mp h, not_false_iff, dif_neg, part_enat.get_coe'], generalize_proofs h, exact nat.find_spec h end /-- If the `n`th coefficient of a formal power series is nonzero, then the order of the power series is less than or equal to `n`.-/ lemma order_le (n : ℕ) (h : coeff R n φ ≠ 0) : order φ ≤ n := begin have := exists.intro n h, rw [order, dif_neg], { simp only [part_enat.coe_le_coe, nat.find_le_iff], exact ⟨n, le_rfl, h⟩ }, { exact exists_coeff_ne_zero_iff_ne_zero.mp ⟨n, h⟩ } end /-- The `n`th coefficient of a formal power series is `0` if `n` is strictly smaller than the order of the power series.-/ lemma coeff_of_lt_order (n : ℕ) (h: ↑n < order φ) : coeff R n φ = 0 := by { contrapose! h, exact order_le _ h } /-- The `0` power series is the unique power series with infinite order.-/ @[simp] lemma order_eq_top {φ : power_series R} : φ.order = ⊤ ↔ φ = 0 := begin split, { intro h, ext n, rw [(coeff R n).map_zero, coeff_of_lt_order], simp [h] }, { rintros rfl, exact order_zero } end /-- The order of a formal power series is at least `n` if the `i`th coefficient is `0` for all `i < n`.-/ lemma nat_le_order (φ : power_series R) (n : ℕ) (h : ∀ i < n, coeff R i φ = 0) : ↑n ≤ order φ := begin by_contra H, rw not_le at H, have : (order φ).dom := part_enat.dom_of_le_coe H.le, rw [← part_enat.coe_get this, part_enat.coe_lt_coe] at H, exact coeff_order this (h _ H) end /-- The order of a formal power series is at least `n` if the `i`th coefficient is `0` for all `i < n`.-/ lemma le_order (φ : power_series R) (n : part_enat) (h : ∀ i : ℕ, ↑i < n → coeff R i φ = 0) : n ≤ order φ := begin induction n using part_enat.cases_on, { show _ ≤ _, rw [top_le_iff, order_eq_top], ext i, exact h _ (part_enat.coe_lt_top i) }, { apply nat_le_order, simpa only [part_enat.coe_lt_coe] using h } end /-- The order of a formal power series is exactly `n` if the `n`th coefficient is nonzero, and the `i`th coefficient is `0` for all `i < n`.-/ lemma order_eq_nat {φ : power_series R} {n : ℕ} : order φ = n ↔ (coeff R n φ ≠ 0) ∧ (∀ i, i < n → coeff R i φ = 0) := begin rcases eq_or_ne φ 0 with rfl|hφ, { simpa using (part_enat.coe_ne_top _).symm }, simp [order, dif_neg hφ, nat.find_eq_iff] end /-- The order of a formal power series is exactly `n` if the `n`th coefficient is nonzero, and the `i`th coefficient is `0` for all `i < n`.-/ lemma order_eq {φ : power_series R} {n : part_enat} : order φ = n ↔ (∀ i:ℕ, ↑i = n → coeff R i φ ≠ 0) ∧ (∀ i:ℕ, ↑i < n → coeff R i φ = 0) := begin induction n using part_enat.cases_on, { rw order_eq_top, split, { rintro rfl, split; intros, { exfalso, exact part_enat.coe_ne_top ‹_› ‹_› }, { exact (coeff _ _).map_zero } }, { rintro ⟨h₁, h₂⟩, ext i, exact h₂ i (part_enat.coe_lt_top i) } }, { simpa [part_enat.coe_inj] using order_eq_nat } end /-- The order of the sum of two formal power series is at least the minimum of their orders.-/ lemma le_order_add (φ ψ : power_series R) : min (order φ) (order ψ) ≤ order (φ + ψ) := begin refine le_order _ _ _, simp [coeff_of_lt_order] {contextual := tt} end private lemma order_add_of_order_eq.aux (φ ψ : power_series R) (h : order φ ≠ order ψ) (H : order φ < order ψ) : order (φ + ψ) ≤ order φ ⊓ order ψ := begin suffices : order (φ + ψ) = order φ, { rw [le_inf_iff, this], exact ⟨le_rfl, le_of_lt H⟩ }, { rw order_eq, split, { intros i hi, rw ←hi at H, rw [(coeff _ _).map_add, coeff_of_lt_order i H, add_zero], exact (order_eq_nat.1 hi.symm).1 }, { intros i hi, rw [(coeff _ _).map_add, coeff_of_lt_order i hi, coeff_of_lt_order i (lt_trans hi H), zero_add] } } end /-- The order of the sum of two formal power series is the minimum of their orders if their orders differ.-/ lemma order_add_of_order_eq (φ ψ : power_series R) (h : order φ ≠ order ψ) : order (φ + ψ) = order φ ⊓ order ψ := begin refine le_antisymm _ (le_order_add _ _), by_cases H₁ : order φ < order ψ, { apply order_add_of_order_eq.aux _ _ h H₁ }, by_cases H₂ : order ψ < order φ, { simpa only [add_comm, inf_comm] using order_add_of_order_eq.aux _ _ h.symm H₂ }, exfalso, exact h (le_antisymm (not_lt.1 H₂) (not_lt.1 H₁)) end /-- The order of the product of two formal power series is at least the sum of their orders.-/ lemma order_mul_ge (φ ψ : power_series R) : order φ + order ψ ≤ order (φ * ψ) := begin apply le_order, intros n hn, rw [coeff_mul, finset.sum_eq_zero], rintros ⟨i,j⟩ hij, by_cases hi : ↑i < order φ, { rw [coeff_of_lt_order i hi, zero_mul] }, by_cases hj : ↑j < order ψ, { rw [coeff_of_lt_order j hj, mul_zero] }, rw not_lt at hi hj, rw finset.nat.mem_antidiagonal at hij, exfalso, apply ne_of_lt (lt_of_lt_of_le hn $ add_le_add hi hj), rw [← nat.cast_add, hij] end /-- The order of the monomial `a*X^n` is infinite if `a = 0` and `n` otherwise.-/ lemma order_monomial (n : ℕ) (a : R) [decidable (a = 0)] : order (monomial R n a) = if a = 0 then ⊤ else n := begin split_ifs with h, { rw [h, order_eq_top, linear_map.map_zero] }, { rw [order_eq], split; intros i hi, { rw [part_enat.coe_inj] at hi, rwa [hi, coeff_monomial_same] }, { rw [part_enat.coe_lt_coe] at hi, rw [coeff_monomial, if_neg], exact ne_of_lt hi } } end /-- The order of the monomial `a*X^n` is `n` if `a ≠ 0`.-/ lemma order_monomial_of_ne_zero (n : ℕ) (a : R) (h : a ≠ 0) : order (monomial R n a) = n := by rw [order_monomial, if_neg h] /-- If `n` is strictly smaller than the order of `ψ`, then the `n`th coefficient of its product with any other power series is `0`. -/ lemma coeff_mul_of_lt_order {φ ψ : power_series R} {n : ℕ} (h : ↑n < ψ.order) : coeff R n (φ * ψ) = 0 := begin suffices : coeff R n (φ * ψ) = ∑ p in finset.nat.antidiagonal n, 0, rw [this, finset.sum_const_zero], rw [coeff_mul], apply finset.sum_congr rfl (λ x hx, _), refine mul_eq_zero_of_right (coeff R x.fst φ) (coeff_of_lt_order x.snd (lt_of_le_of_lt _ h)), rw finset.nat.mem_antidiagonal at hx, norm_cast, linarith, end lemma coeff_mul_one_sub_of_lt_order {R : Type*} [comm_ring R] {φ ψ : power_series R} (n : ℕ) (h : ↑n < ψ.order) : coeff R n (φ * (1 - ψ)) = coeff R n φ := by simp [coeff_mul_of_lt_order h, mul_sub] lemma coeff_mul_prod_one_sub_of_lt_order {R ι : Type*} [comm_ring R] (k : ℕ) (s : finset ι) (φ : power_series R) (f : ι → power_series R) : (∀ i ∈ s, ↑k < (f i).order) → coeff R k (φ * ∏ i in s, (1 - f i)) = coeff R k φ := begin apply finset.induction_on s, { simp }, { intros a s ha ih t, simp only [finset.mem_insert, forall_eq_or_imp] at t, rw [finset.prod_insert ha, ← mul_assoc, mul_right_comm, coeff_mul_one_sub_of_lt_order _ t.1], exact ih t.2 }, end -- TODO: link with `X_pow_dvd_iff` lemma X_pow_order_dvd (h : (order φ).dom) : X ^ ((order φ).get h) ∣ φ := begin refine ⟨power_series.mk (λ n, coeff R (n + (order φ).get h) φ), _⟩, ext n, simp only [coeff_mul, coeff_X_pow, coeff_mk, boole_mul, finset.sum_ite, finset.nat.filter_fst_eq_antidiagonal, finset.sum_const_zero, add_zero], split_ifs with hn hn, { simp [tsub_add_cancel_of_le hn] }, { simp only [finset.sum_empty], refine coeff_of_lt_order _ _, simpa [part_enat.coe_lt_iff] using λ _, hn } end lemma order_eq_multiplicity_X {R : Type*} [comm_semiring R] (φ : power_series R) : order φ = multiplicity X φ := begin rcases eq_or_ne φ 0 with rfl|hφ, { simp }, induction ho : order φ using part_enat.cases_on with n, { simpa [hφ] using ho }, have hn : φ.order.get (order_finite_iff_ne_zero.mpr hφ) = n, { simp [ho] }, rw ←hn, refine le_antisymm (le_multiplicity_of_pow_dvd $ X_pow_order_dvd (order_finite_iff_ne_zero.mpr hφ)) (part_enat.find_le _ _ _), rintro ⟨ψ, H⟩, have := congr_arg (coeff R n) H, rw [mul_comm, coeff_mul_of_lt_order, ←hn] at this, { exact coeff_order _ this }, { rw [X_pow_eq, order_monomial], split_ifs, { exact part_enat.coe_lt_top _ }, { rw [←hn, part_enat.coe_lt_coe], exact nat.lt_succ_self _ } } end end order_basic section order_zero_ne_one variables [semiring R] [nontrivial R] /-- The order of the formal power series `1` is `0`.-/ @[simp] lemma order_one : order (1 : power_series R) = 0 := by simpa using order_monomial_of_ne_zero 0 (1:R) one_ne_zero /-- The order of the formal power series `X` is `1`.-/ @[simp] lemma order_X : order (X : power_series R) = 1 := by simpa only [nat.cast_one] using order_monomial_of_ne_zero 1 (1:R) one_ne_zero /-- The order of the formal power series `X^n` is `n`.-/ @[simp] lemma order_X_pow (n : ℕ) : order ((X : power_series R)^n) = n := by { rw [X_pow_eq, order_monomial_of_ne_zero], exact one_ne_zero } end order_zero_ne_one section order_is_domain -- TODO: generalize to `[semiring R] [no_zero_divisors R]` variables [comm_ring R] [is_domain R] /-- The order of the product of two formal power series over an integral domain is the sum of their orders.-/ lemma order_mul (φ ψ : power_series R) : order (φ * ψ) = order φ + order ψ := begin simp_rw [order_eq_multiplicity_X], exact multiplicity.mul X_prime end end order_is_domain end power_series namespace polynomial open finsupp variables {σ : Type*} {R : Type*} [comm_semiring R] (φ ψ : R[X]) /-- The natural inclusion from polynomials into formal power series.-/ instance coe_to_power_series : has_coe R[X] (power_series R) := ⟨λ φ, power_series.mk $ λ n, coeff φ n⟩ lemma coe_def : (φ : power_series R) = power_series.mk (coeff φ) := rfl @[simp, norm_cast] lemma coeff_coe (n) : power_series.coeff R n φ = coeff φ n := congr_arg (coeff φ) (finsupp.single_eq_same) @[simp, norm_cast] lemma coe_monomial (n : ℕ) (a : R) : (monomial n a : power_series R) = power_series.monomial R n a := by { ext, simp [coeff_coe, power_series.coeff_monomial, polynomial.coeff_monomial, eq_comm] } @[simp, norm_cast] lemma coe_zero : ((0 : R[X]) : power_series R) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : R[X]) : power_series R) = 1 := begin have := coe_monomial 0 (1:R), rwa power_series.monomial_zero_eq_C_apply at this, end @[simp, norm_cast] lemma coe_add : ((φ + ψ : R[X]) : power_series R) = φ + ψ := by { ext, simp } @[simp, norm_cast] lemma coe_mul : ((φ * ψ : R[X]) : power_series R) = φ * ψ := power_series.ext $ λ n, by simp only [coeff_coe, power_series.coeff_mul, coeff_mul] @[simp, norm_cast] lemma coe_C (a : R) : ((C a : R[X]) : power_series R) = power_series.C R a := begin have := coe_monomial 0 a, rwa power_series.monomial_zero_eq_C_apply at this, end @[simp, norm_cast] lemma coe_bit0 : ((bit0 φ : R[X]) : power_series R) = bit0 (φ : power_series R) := coe_add φ φ @[simp, norm_cast] lemma coe_bit1 : ((bit1 φ : R[X]) : power_series R) = bit1 (φ : power_series R) := by rw [bit1, bit1, coe_add, coe_one, coe_bit0] @[simp, norm_cast] lemma coe_X : ((X : R[X]) : power_series R) = power_series.X := coe_monomial _ _ @[simp] lemma constant_coeff_coe : power_series.constant_coeff R φ = φ.coeff 0 := rfl variables (R) lemma coe_injective : function.injective (coe : R[X] → power_series R) := λ x y h, by { ext, simp_rw [←coeff_coe, h] } variables {R φ ψ} @[simp, norm_cast] lemma coe_inj : (φ : power_series R) = ψ ↔ φ = ψ := (coe_injective R).eq_iff @[simp] lemma coe_eq_zero_iff : (φ : power_series R) = 0 ↔ φ = 0 := by rw [←coe_zero, coe_inj] @[simp] lemma coe_eq_one_iff : (φ : power_series R) = 1 ↔ φ = 1 := by rw [←coe_one, coe_inj] variables (φ ψ) /-- The coercion from polynomials to power series as a ring homomorphism. -/ def coe_to_power_series.ring_hom : R[X] →+* power_series R := { to_fun := (coe : R[X] → power_series R), map_zero' := coe_zero, map_one' := coe_one, map_add' := coe_add, map_mul' := coe_mul } @[simp] lemma coe_to_power_series.ring_hom_apply : coe_to_power_series.ring_hom φ = φ := rfl @[simp, norm_cast] lemma coe_pow (n : ℕ): ((φ ^ n : R[X]) : power_series R) = (φ : power_series R) ^ n := coe_to_power_series.ring_hom.map_pow _ _ variables (A : Type*) [semiring A] [algebra R A] /-- The coercion from polynomials to power series as an algebra homomorphism. -/ def coe_to_power_series.alg_hom : R[X] →ₐ[R] power_series A := { commutes' := λ r, by simp [algebra_map_apply, power_series.algebra_map_apply], ..(power_series.map (algebra_map R A)).comp coe_to_power_series.ring_hom } @[simp] lemma coe_to_power_series.alg_hom_apply : (coe_to_power_series.alg_hom A φ) = power_series.map (algebra_map R A) ↑φ := rfl end polynomial namespace power_series variables {R A : Type*} [comm_semiring R] [comm_semiring A] [algebra R A] (f : power_series R) instance algebra_polynomial : algebra R[X] (power_series A) := ring_hom.to_algebra (polynomial.coe_to_power_series.alg_hom A).to_ring_hom instance algebra_power_series : algebra (power_series R) (power_series A) := (map (algebra_map R A)).to_algebra @[priority 100] -- see Note [lower instance priority] instance algebra_polynomial' {A : Type*} [comm_semiring A] [algebra R (polynomial A)] : algebra R (power_series A) := ring_hom.to_algebra $ polynomial.coe_to_power_series.ring_hom.comp (algebra_map R (polynomial A)) variables (A) lemma algebra_map_apply' (p : R[X]) : algebra_map R[X] (power_series A) p = map (algebra_map R A) p := rfl lemma algebra_map_apply'' : algebra_map (power_series R) (power_series A) f = map (algebra_map R A) f := rfl end power_series
667a5eef04a45e74786ba801b40a9393816bfe82
ba4794a0deca1d2aaa68914cd285d77880907b5c
/src/game/world3/level5.lean
c14ea10eceb3b03c94915f66d01ee1216f027e5d
[ "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
1,101
lean
import game.world3.level4 -- hide import mynat.mul -- hide namespace mynat -- hide /- # Multiplication World ## Level 5: `mul_assoc` We now have enough to prove that multiplication is associative. ## Random tactic hints 1) Did you know you can do `repeat {rw mul_succ}`? 2) Did you know you can do `rwa [hd, mul_add]`? (I learnt that trick from Ken Lee). `rwa` is like `rw` except that at the end it will check to see if the goal it's working on can be proved either by `refl` or `exact X` where `X` is one of the assumptions. -/ /- Lemma Multiplication is associative. In other words, for all natural numbers $a$, $b$ and $c$, we have $$ (ab)c = a(bc). $$ -/ lemma mul_assoc (a b c : mynat) : (a * b) * c = a * (b * c) := begin [less_leaky] induction c with d hd, { repeat {rw mul_zero}, }, { rw mul_succ, rw mul_succ, rw hd, rw mul_add, refl, } end /- A mathematician could now remark that you have proved that the natural numbers form a monoid under multiplication. -/ def collectible_4 : monoid mynat := by structure_helper -- hide end mynat -- hide
c021ac60e506c95d4bdbd721065e4bf45685b2b9
2c096fdfecf64e46ea7bc6ce5521f142b5926864
/src/Lean/Server/Snapshots.lean
474692c36d5786077420bee7442c87a1a2ce9031
[ "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
Kha/lean4
1005785d2c8797ae266a303968848e5f6ce2fe87
b99e11346948023cd6c29d248cd8f3e3fb3474cf
refs/heads/master
1,693,355,498,027
1,669,080,461,000
1,669,113,138,000
184,748,176
0
0
Apache-2.0
1,665,995,520,000
1,556,884,930,000
Lean
UTF-8
Lean
false
false
7,549
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Init.System.IO import Lean.Elab.Import import Lean.Elab.Command import Lean.Widget.InteractiveDiagnostic /-! One can think of this module as being a partial reimplementation of Lean.Elab.Frontend which also stores a snapshot of the world after each command. Importantly, we allow (re)starting compilation from any snapshot/position in the file for interactive editing purposes. -/ namespace Lean.Server.Snapshots open Elab /- For `Inhabited Snapshot` -/ builtin_initialize dummyTacticCache : IO.Ref Tactic.Cache ← IO.mkRef {} /-- What Lean knows about the world after the header and each command. -/ structure Snapshot where /-- Where the command which produced this snapshot begins. Note that neighbouring snapshots are *not* necessarily attached beginning-to-end, since inputs outside the grammar advance the parser but do not produce snapshots. -/ beginPos : String.Pos stx : Syntax mpState : Parser.ModuleParserState cmdState : Command.State /-- We cache interactive diagnostics in order not to invoke the pretty-printer again on messages from previous snapshots when publishing diagnostics for every new snapshot (this is quadratic), as well as not to invoke it once again when handling `$/lean/interactiveDiagnostics`. -/ interactiveDiags : PersistentArray Widget.InteractiveDiagnostic tacticCache : IO.Ref Tactic.Cache instance : Inhabited Snapshot where default := { beginPos := default stx := default mpState := default cmdState := default interactiveDiags := default tacticCache := dummyTacticCache } namespace Snapshot def endPos (s : Snapshot) : String.Pos := s.mpState.pos def env (s : Snapshot) : Environment := s.cmdState.env def msgLog (s : Snapshot) : MessageLog := s.cmdState.messages def diagnostics (s : Snapshot) : PersistentArray Lsp.Diagnostic := s.interactiveDiags.map fun d => d.toDiagnostic def infoTree (s : Snapshot) : InfoTree := -- the parser returns exactly one command per snapshot, and the elaborator creates exactly one node per command assert! s.cmdState.infoState.trees.size == 1 s.cmdState.infoState.trees[0]! def isAtEnd (s : Snapshot) : Bool := Parser.isEOI s.stx || Parser.isTerminalCommand s.stx open Command in /-- Use the command state in the given snapshot to run a `CommandElabM`.-/ def runCommandElabM (snap : Snapshot) (meta : DocumentMeta) (c : CommandElabM α) : EIO Exception α := do let ctx : Command.Context := { cmdPos := snap.beginPos, fileName := meta.uri, fileMap := meta.text, tacticCache? := snap.tacticCache, } c.run ctx |>.run' snap.cmdState /-- Run a `CoreM` computation using the data in the given snapshot.-/ def runCoreM (snap : Snapshot) (meta : DocumentMeta) (c : CoreM α) : EIO Exception α := snap.runCommandElabM meta <| Command.liftCoreM c /-- Run a `TermElabM` computation using the data in the given snapshot.-/ def runTermElabM (snap : Snapshot) (meta : DocumentMeta) (c : TermElabM α) : EIO Exception α := snap.runCommandElabM meta <| Command.liftTermElabM c end Snapshot /-- Parses the next command occurring after the given snapshot without elaborating it. -/ def parseNextCmd (inputCtx : Parser.InputContext) (snap : Snapshot) : IO Syntax := do let cmdState := snap.cmdState let scope := cmdState.scopes.head! let pmctx := { env := cmdState.env, options := scope.opts, currNamespace := scope.currNamespace, openDecls := scope.openDecls } let (cmdStx, _, _) := Parser.parseCommand inputCtx pmctx snap.mpState snap.msgLog return cmdStx register_builtin_option server.stderrAsMessages : Bool := { defValue := true group := "server" descr := "(server) capture output to the Lean stderr channel (such as from `dbg_trace`) during elaboration of a command as a diagnostic message" } /-- Compiles the next command occurring after the given snapshot. If there is no next command (file ended), `Snapshot.isAtEnd` will hold of the return value. -/ -- NOTE: This code is really very similar to Elab.Frontend. But generalizing it -- over "store snapshots"/"don't store snapshots" would likely result in confusing -- isServer? conditionals and not be worth it due to how short it is. def compileNextCmd (inputCtx : Parser.InputContext) (snap : Snapshot) (hasWidgets : Bool) : IO Snapshot := do let cmdState := snap.cmdState let scope := cmdState.scopes.head! let pmctx := { env := cmdState.env, options := scope.opts, currNamespace := scope.currNamespace, openDecls := scope.openDecls } let (cmdStx, cmdParserState, msgLog) := Parser.parseCommand inputCtx pmctx snap.mpState snap.msgLog let cmdPos := cmdStx.getPos?.get! if Parser.isEOI cmdStx then let endSnap : Snapshot := { beginPos := cmdPos stx := cmdStx mpState := cmdParserState cmdState := snap.cmdState interactiveDiags := ← withNewInteractiveDiags msgLog tacticCache := snap.tacticCache } return endSnap else let cmdStateRef ← IO.mkRef { snap.cmdState with messages := msgLog } /- The same snapshot may be executed by different tasks. So, to make sure `elabCommandTopLevel` has exclusive access to the cache, we create a fresh reference here. Before this change, the following `snap.tacticCache.modify` would reset the tactic post cache while another snapshot was still using it. -/ let tacticCacheNew ← IO.mkRef (← snap.tacticCache.get) let cmdCtx : Elab.Command.Context := { cmdPos := snap.endPos fileName := inputCtx.fileName fileMap := inputCtx.fileMap tacticCache? := some tacticCacheNew } let (output, _) ← IO.FS.withIsolatedStreams (isolateStderr := server.stderrAsMessages.get scope.opts) <| liftM (m := BaseIO) do Elab.Command.catchExceptions (getResetInfoTrees *> Elab.Command.elabCommandTopLevel cmdStx) cmdCtx cmdStateRef let postNew := (← tacticCacheNew.get).post snap.tacticCache.modify fun _ => { pre := postNew, post := {} } let mut postCmdState ← cmdStateRef.get if !output.isEmpty then postCmdState := { postCmdState with messages := postCmdState.messages.add { fileName := inputCtx.fileName severity := MessageSeverity.information pos := inputCtx.fileMap.toPosition snap.endPos data := output } } let postCmdSnap : Snapshot := { beginPos := cmdPos stx := cmdStx mpState := cmdParserState cmdState := postCmdState interactiveDiags := ← withNewInteractiveDiags postCmdState.messages tacticCache := (← IO.mkRef {}) } return postCmdSnap where /-- Compute the current interactive diagnostics log by finding a "diff" relative to the parent snapshot. We need to do this because unlike the `MessageLog` itself, interactive diags are not part of the command state. -/ withNewInteractiveDiags (msgLog : MessageLog) : IO (PersistentArray Widget.InteractiveDiagnostic) := do let newMsgCount := msgLog.msgs.size - snap.msgLog.msgs.size let mut ret := snap.interactiveDiags for i in List.iota newMsgCount do let newMsg := msgLog.msgs.get! (msgLog.msgs.size - i) ret := ret.push (← Widget.msgToInteractiveDiagnostic inputCtx.fileMap newMsg hasWidgets) return ret end Lean.Server.Snapshots
c043298c19aa9734fbc31ade97d117ac755b4b77
5ec8f5218a7c8e87dd0d70dc6b715b36d61a8d61
/globalenvs.lean
a4caea0b7bd91132755bdd59b14d65ad26638b13
[]
no_license
mbrodersen/kremlin
f9f2f9dd77b9744fe0ffd5f70d9fa0f1f8bd8cec
d4665929ce9012e93a0b05fc7063b96256bab86f
refs/heads/master
1,624,057,268,130
1,496,957,084,000
1,496,957,084,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
32,670
lean
/- Global environments are a component of the dynamic semantics of all languages involved in the compiler. A global environment maps symbol names (names of functions and of global variables) to the corresponding memory addresses. It also maps memory addresses of functions to the corresponding function descriptions. Global environments, along with the initial memory state at the beginning of program execution, are built from the program of interest, as follows: - A distinct memory address is assigned to each function of the program. These function addresses use negative numbers to distinguish them from addresses of memory blocks. The associations of function name to function address and function address to function description are recorded in the global environment. - For each global variable, a memory block is allocated and associated to the name of the variable. These operations reflect (at a high level of abstraction) what takes place during program linking and program loading in a real operating system. -/ import .memory .linking namespace globalenvs open memdata memory values ast integers maps linking errors memdata.memval word floats memdata.quantity memory.perm_kind ast.memory_chunk /- Auxiliary function for initialization of global variables. -/ def store_zeros : mem → block → ℕ → ℕ → option mem | m b p 0 := some m | m b p (n+1) := do m' ← store Mint8unsigned m b p Vzero, store_zeros m' b (p + 1) n /- * Symbol environments -/ /- Symbol environments are a restricted view of global environments, focusing on symbol names and their associated blocks. They do not contain mappings from blocks to function or variable definitions. -/ structure Senv : Type := (find_symbol : ident → option block) (public_symbol : ident → bool) (invert_symbol : block → option ident) (block_is_volatile : block → bool) (nextblock : block) /- Properties -/ (find_symbol_injective : ∀ {id1 id2 b}, find_symbol id1 = some b → find_symbol id2 = some b → id1 = id2) (invert_find_symbol : ∀ {id b}, invert_symbol b = some id → find_symbol id = some b) (find_invert_symbol : ∀ {id b}, find_symbol id = some b → invert_symbol b = some id) (public_symbol_exists : ∀ {id}, public_symbol id → ∃ b, find_symbol id = some b) (find_symbol_below : ∀ {id b}, find_symbol id = some b → b < nextblock) (block_is_volatile_below : ∀ {b}, block_is_volatile b → b < nextblock) namespace Senv def symbol_address (ge : Senv) (id : ident) (ofs : ptrofs) : val := match find_symbol ge id with | some b := Vptr b ofs | none := Vundef end theorem shift_symbol_address {ge id ofs delta} : symbol_address ge id (ofs + delta) = val.offset_ptr (symbol_address ge id ofs) delta := sorry' theorem shift_symbol_address_32 {ge id ofs n} : ¬ archi.ptr64 → symbol_address ge id (ofs + ptrofs.of_int n) = symbol_address ge id ofs + n := sorry' theorem shift_symbol_address_64 {ge id ofs n} : archi.ptr64 → symbol_address ge id (ofs + ptrofs.of_int64 n) = (symbol_address ge id ofs).addl n := sorry' def equiv (se1 se2 : Senv) : Prop := (∀ id, find_symbol se2 id = find_symbol se1 id) ∧ (∀ id, public_symbol se2 id = public_symbol se1 id) ∧ (∀ b, block_is_volatile se2 b = block_is_volatile se1 b) end Senv /- * Global environments -/ /- The type of global environments. -/ structure Genv (F V : Type) : Type := (public : list ident) /- which symbol names are public -/ (symb : PTree block) /- mapping symbol -> block -/ (defs : PTree (globdef F V)) /- mapping block -> definition -/ (next : block) /- next symbol pointer -/ (symb_range : ∀ {id b}, PTree.get id symb = some b → b < next) (defs_range : ∀ {b g}, PTree.get b defs = some g → b < next) (vars_inj : ∀ {id1 id2 b}, PTree.get id1 symb = some b → PTree.get id2 symb = some b → id1 = id2) namespace Genv section variable {F : Type} /- The type of function descriptions -/ variable {V : Type} /- The type of information attached to variables -/ /- ** Lookup functions -/ /- [find_symbol ge id] returns the block associated with the given name, if any -/ def find_symbol (ge : Genv F V) (id : ident) : option block := PTree.get id ge.symb /- [symbol_address ge id ofs] returns a pointer into the block associated with [id], at byte offset [ofs]. [Vundef] is returned if no block is associated to [id]. -/ def symbol_address (ge : Genv F V) (id : ident) (ofs : ptrofs) : val := match find_symbol ge id with | some b := Vptr b ofs | none := Vundef end /- [public_symbol ge id] says whether the name [id] is public and defined. -/ def public_symbol (ge : Genv F V) (id : ident) : bool := (find_symbol ge id).is_some && (id ∈ ge.public) /- [find_def ge b] returns the global definition associated with the given address. -/ def find_def (ge : Genv F V) (b : block) : option (globdef F V) := PTree.get b ge.defs /- [find_funct_ptr ge b] returns the function description associated with the given address. -/ def find_funct_ptr (ge : Genv F V) (b : block) : option F := match find_def ge b with some (Gfun f) := some f | _ := none end /- [find_funct] is similar to [find_funct_ptr], but the function address is given as a value, which must be a pointer with offset 0. -/ def find_funct (ge : Genv F V) : val → option F | (Vptr b ofs) := if ofs = 0 then find_funct_ptr ge b else none | _ := none /- [invert_symbol ge b] returns the name associated with the given block, if any -/ def invert_symbol (ge : Genv F V) (b : block) : option ident := PTree.fold (λ res id b', if b = b' then some id else res) ge.symb none /- [find_var_info ge b] returns the information attached to the variable at address [b]. -/ def find_var_info (ge : Genv F V) (b : block) : option (globvar V) := match find_def ge b with some (Gvar v) := some v | _ := none end /- [block_is_volatile ge b] returns [true] if [b] points to a global variable of volatile type, [false] otherwise. -/ def block_is_volatile (ge : Genv F V) (b : block) : bool := match find_var_info ge b with | none := ff | some gv := gv.volatile end /- ** Constructing the global environment -/ def add_global (ge : Genv F V) : ident × globdef F V → Genv F V | (id, g) := { public := ge.public, symb := PTree.set id ge.next ge.symb, defs := PTree.set ge.next g ge.defs, next := ge.next.succ, symb_range := λid' b h, show (_:ℕ)<_, begin rw pos_num.succ_to_nat, apply nat.lt_succ_of_le, rw PTree.gsspec at h, by_cases id' = id with ii; simp [ii] at h, { injection h, rw h }, { exact le_of_lt (ge.symb_range h) } end, defs_range := λb g' h, show (_:ℕ)<_, begin rw pos_num.succ_to_nat, apply nat.lt_succ_of_le, rw PTree.gsspec at h, by_cases b = ge.next with bb, { rw bb }, { simp [bb] at h, exact le_of_lt (ge.defs_range h) } end, vars_inj := λid1 id2 b h1 h2, begin rw PTree.gsspec at h1 h2, by_cases id1 = id with i1; simp [i1] at h1; by_cases id2 = id with i2; simp [i2] at h2; try {simp [i1, i2]}, { rw -h1 at h2, exact absurd (ge.symb_range h2) (lt_irrefl _) }, { rw -h2 at h1, exact absurd (ge.symb_range h1) (lt_irrefl _) }, { exact ge.vars_inj h1 h2 }, end } def add_globals (ge : Genv F V) (gl : list (ident × globdef F V)) : Genv F V := gl.foldl add_global ge lemma add_globals_app (ge : Genv F V) (gl2 gl1) : add_globals ge (gl1 ++ gl2) = add_globals (add_globals ge gl1) gl2 := sorry' def empty (pub : list ident) : Genv F V := { public := pub, symb := ∅, defs := ∅, next := 1, symb_range := λid' b h, by rw PTree.gempty at h; contradiction, defs_range := λb g' h, by rw PTree.gempty at h; contradiction, vars_inj := λid1 id2 b h, by rw PTree.gempty at h; contradiction } def globalenv (p : program F V) : Genv F V := add_globals (empty p.public) p.defs /- Proof principles -/ section globalenv_principles variable P : Genv F V → Prop include P lemma add_globals_preserves {gl ge} : (∀ ge id g, P ge → (id, g) ∈ gl → P (add_global ge (id, g))) → P ge → P (add_globals ge gl) := sorry' lemma add_globals_ensures {id g gl ge} : (∀ ge id g, P ge → (id, g) ∈ gl → P (add_global ge (id, g))) → (∀ ge, P (add_global ge (id, g))) → (id, g) ∈ gl → P (add_globals ge gl) := sorry' lemma add_globals_unique_preserves {id gl ge} : (∀ ge id1 g, P ge → (id1, g) ∈ gl → id1 ≠ id → P (add_global ge (id1, g))) → id ∉ list.map prod.fst gl → P ge → P (add_globals ge gl) := sorry' lemma add_globals_unique_ensures {gl1 id g gl2 ge} : (∀ ge id1 g1, P ge → (id1, g1) ∈ gl2 → id1 ≠ id → P (add_global ge (id1, g1))) → (∀ ge, P (add_global ge (id, g))) → id ∉ list.map prod.fst gl2 → P (add_globals ge (gl1 ++ (id, g) :: gl2)) := sorry' theorem in_norepet_unique {id g} {gl : list (ident × globdef F V)} : (id, g) ∈ gl → (gl.map prod.fst).nodup → ∃ gl1 gl2, gl = gl1 ++ (id, g) :: gl2 ∧ id ∈ gl2.map prod.fst := sorry' lemma add_globals_norepet_ensures {id g gl ge} : (∀ ge id1 g1, P ge → (id1, g1) ∈ gl → id1 ≠ id → P (add_global ge (id1, g1))) → (∀ ge, P (add_global ge (id, g))) → (id, g) ∈ gl → (list.map prod.fst gl).nodup → P (add_globals ge gl) := sorry' end globalenv_principles /- ** Properties of the operations over global environments -/ theorem public_symbol_exists {ge : Genv F V} {id} : public_symbol ge id → ∃ b, find_symbol ge id = some b := sorry' theorem shift_symbol_address {ge : Genv F V} {id ofs delta} : symbol_address ge id (ofs + delta) = (symbol_address ge id ofs).offset_ptr delta := sorry' theorem shift_symbol_address_32 {ge : Genv F V} {id ofs n} : ¬ archi.ptr64 → symbol_address ge id (ofs + ptrofs.of_int n) = symbol_address ge id ofs + n := sorry' theorem shift_symbol_address_64 {ge : Genv F V} {id ofs n} : archi.ptr64 → symbol_address ge id (ofs + ptrofs.of_int64 n) = (symbol_address ge id ofs).addl n := sorry' theorem find_funct_inv {ge : Genv F V} {v f} : find_funct ge v = some f → ∃ b, v = Vptr b 0 := sorry' theorem find_funct_find_funct_ptr {ge : Genv F V} {b} : find_funct ge (Vptr b 0) = find_funct_ptr ge b := sorry' theorem find_funct_ptr_iff {ge : Genv F V} {b f} : find_funct_ptr ge b = some f ↔ find_def ge b = some (Gfun f) := sorry' theorem find_var_info_iff {ge : Genv F V} {b v} : find_var_info ge b = some v ↔ find_def ge b = some (Gvar v) := sorry' theorem find_def_symbol {p : program F V} {id g} : (prog_defmap p^!id) = some g ↔ ∃ b, find_symbol (globalenv p) id = some b ∧ find_def (globalenv p) b = some g := sorry' theorem find_symbol_exists {p : program F V} {id g} : (id, g) ∈ p.defs → ∃ b, find_symbol (globalenv p) id = some b := sorry' theorem find_symbol_inversion {p : program F V} {x b} : find_symbol (globalenv p) x = some b → x ∈ p.defs_names := sorry' theorem find_def_inversion {p : program F V} {b g} : find_def (globalenv p) b = some g → ∃ id, (id, g) ∈ p.defs := sorry' theorem find_funct_ptr_inversion {p : program F V} {b f} : find_funct_ptr (globalenv p) b = some f → ∃ id, (id, Gfun f) ∈ p.defs := sorry' theorem find_funct_inversion {p : program F V} {v f} : find_funct (globalenv p) v = some f → ∃ id, (id, Gfun f) ∈ p.defs := sorry' theorem find_funct_ptr_prop (P : F → Prop) {p : program F V} {b f} : (∀ id f, (id, Gfun f) ∈ p.defs → P f) → find_funct_ptr (globalenv p) b = some f → P f := sorry' theorem find_funct_prop (P : F → Prop) {p : program F V} {v f} : (∀ id f, (id, Gfun f) ∈ p.defs → P f) → find_funct (globalenv p) v = some f → P f := sorry' theorem global_addresses_distinct {ge : Genv F V} {id1 id2 b1 b2} : id1 ≠ id2 → find_symbol ge id1 = some b1 → find_symbol ge id2 = some b2 → b1 ≠ b2 := sorry' theorem invert_find_symbol {ge : Genv F V} {id b} : invert_symbol ge b = some id → find_symbol ge id = some b := sorry' theorem find_invert_symbol {ge : Genv F V} {id b} : find_symbol ge id = some b → invert_symbol ge b = some id := sorry' def advance_next (gl : list (ident × globdef F V)) (x : pos_num) := gl.foldl (λ n g, pos_num.succ n) x theorem genv_next_add_globals (gl) (ge : Genv F V) : (add_globals ge gl).next = advance_next gl ge.next := sorry' theorem genv_public_add_globals (gl) (ge : Genv F V) : (add_globals ge gl).public = ge.public := sorry' theorem globalenv_public (p : program F V) : (globalenv p).public = p.public := sorry' theorem block_is_volatile_below {ge : Genv F V} {b} : block_is_volatile ge b → b < ge.next := sorry' /- ** Coercing a global environment into a symbol environment -/ def to_senv (ge : Genv F V) : Senv := { find_symbol := ge.find_symbol, public_symbol := ge.public_symbol, invert_symbol := ge.invert_symbol, block_is_volatile := ge.block_is_volatile, nextblock := ge.next, find_symbol_injective := ge.vars_inj, invert_find_symbol := ge.invert_find_symbol, find_invert_symbol := ge.find_invert_symbol, public_symbol_exists := ge.public_symbol_exists, find_symbol_below := ge.symb_range, block_is_volatile_below := ge.block_is_volatile_below } instance : has_coe (Genv F V) Senv := ⟨to_senv⟩ /- * Construction of the initial memory state -/ section init_mem variable ge : Genv F V def store_init_data (m : mem) (b : block) (p : ℕ) : init_data → option mem | (init_data.int8 n) := store Mint8unsigned m b p (Vint n) | (init_data.int16 n) := store Mint16unsigned m b p (Vint n) | (init_data.int32 n) := store Mint32 m b p (Vint n) | (init_data.int64 n) := store Mint64 m b p (Vlong n) | (init_data.float32 n) := store Mfloat32 m b p (Vsingle n) | (init_data.float64 n) := store Mfloat64 m b p (Vfloat n) | (init_data.addrof symb ofs) := do b' ← find_symbol ge symb, store Mptr m b p (Vptr b' ofs) | (init_data.space n) := some m def store_init_data_list : mem → block → ℕ → list init_data → option mem | m b p [] := some m | m b p (id :: idl') := do m' ← store_init_data ge m b p id, store_init_data_list m' b (p + id.size) idl' def perm_globvar (gv : globvar V) : permission := if gv.volatile then Nonempty else if gv.readonly then Readable else Writable def alloc_global (m : mem) : ident × globdef F V → option mem | (id, Gfun f) := drop_perm (m.alloc 0 1) m.nextblock 0 1 Nonempty | (id, Gvar v) := do let init := v.init, let sz := init_data.list_size init, let b := m.nextblock, let m1 := m.alloc 0 sz, m2 ← store_zeros m1 b 0 sz, m3 ← store_init_data_list ge m2 b 0 init, drop_perm m3 b 0 sz (perm_globvar v) def alloc_globals : mem → list (ident × globdef F V) → option mem := mfoldl (alloc_global ge) lemma alloc_globals_app {gl1 gl2 m m1} : alloc_globals ge m gl1 = some m1 → alloc_globals ge m1 gl2 = alloc_globals ge m (gl1 ++ gl2) := sorry' /- Next-block properties -/ theorem store_zeros_nextblock {m b p n m'} : store_zeros m b p n = some m' → m'.nextblock = m.nextblock := sorry' theorem store_init_data_list_nextblock {idl b m p m'} : store_init_data_list ge m b p idl = some m' → m'.nextblock = m.nextblock := sorry' theorem alloc_global_nextblock {g m m'} : alloc_global ge m g = some m' → m'.nextblock = m.nextblock.succ := sorry' theorem alloc_globals_nextblock {gl m m'} : alloc_globals ge m gl = some m' → m'.nextblock = advance_next gl m.nextblock := sorry' /- Permissions -/ theorem store_zeros_perm {k prm b' q m b p n m'} : store_zeros m b p n = some m' → (perm m b' q k prm ↔ perm m' b' q k prm) := sorry' theorem store_init_data_perm {k prm b' q i b m p m'} : store_init_data ge m b p i = some m' → (perm m b' q k prm ↔ perm m' b' q k prm) := sorry' theorem store_init_data_list_perm {k prm b' q idl b m p m'} : store_init_data_list ge m b p idl = some m' → (perm m b' q k prm ↔ perm m' b' q k prm) := sorry' theorem alloc_global_perm {k prm b' q idg m m'} : alloc_global ge m idg = some m' → valid_block m b' → (perm m b' q k prm ↔ perm m' b' q k prm) := sorry' theorem alloc_globals_perm {k prm b' q gl m m'} : alloc_globals ge m gl = some m' → valid_block m b' → (perm m b' q k prm ↔ perm m' b' q k prm) := sorry' /- Data preservation properties -/ theorem store_zeros_unchanged {P : block → ℕ → Prop} {m b p n m'} : store_zeros m b p n = some m' → (∀ i, p ≤ i → i < p + n → ¬ P b i) → unchanged_on P m m' := sorry' theorem store_init_data_unchanged {P : block → ℕ → Prop} {b i m p m'} : store_init_data ge m b p i = some m' → (∀ ofs, p ≤ ofs → ofs < p + i.size → ¬ P b ofs) → unchanged_on P m m' := sorry' theorem store_init_data_list_unchanged {P : block → ℕ → Prop} {b il m p m'} : store_init_data_list ge m b p il = some m' → (∀ ofs, p ≤ ofs → ¬ P b ofs) → unchanged_on P m m' := sorry' /- Properties related to [load_bytes] -/ def readbytes_as_zero (m : mem) (b : block) (ofs len : ℕ) : Prop := ∀ p n, ofs ≤ p → p + n ≤ ofs + len → load_bytes m b p n = some (list.repeat (Byte 0) n) lemma store_zeros_load_bytes {m b p n m'} : store_zeros m b p n = some m' → readbytes_as_zero m' b p n := sorry' def bytes_of_init_data (i : init_data) : list memval := match i with | (init_data.int8 n) := inj_bytes (encode_int 1 (unsigned n)) | (init_data.int16 n) := inj_bytes (encode_int 2 (unsigned n)) | (init_data.int32 n) := inj_bytes (encode_int 4 (unsigned n)) | (init_data.int64 n) := inj_bytes (encode_int 8 (unsigned n)) | (init_data.float32 n) := inj_bytes (encode_int 4 (unsigned (float32.to_bits n))) | (init_data.float64 n) := inj_bytes (encode_int 8 (unsigned (float.to_bits n))) | (init_data.space n) := list.repeat (Byte 0) n | (init_data.addrof id ofs) := match find_symbol ge id with | some b := inj_value (if archi.ptr64 then Q64 else Q32) (Vptr b ofs) | none := list.repeat Undef (if archi.ptr64 then 8 else 4) end end theorem init_data_size_addrof {id ofs} : (init_data.addrof id ofs).size = (Mptr).size := sorry' lemma store_init_data_load_bytes {m b p i m'} : store_init_data ge m b p i = some m' → readbytes_as_zero m b p i.size → load_bytes m' b p i.size = some (bytes_of_init_data ge i) := sorry' def bytes_of_init_data_list (il : list init_data) : list memval := il >>= bytes_of_init_data ge lemma store_init_data_list_load_bytes {b il m p m'} : store_init_data_list ge m b p il = some m' → readbytes_as_zero m b p (init_data.list_size il) → load_bytes m' b p (init_data.list_size il) = some (bytes_of_init_data_list ge il) := sorry' /- Properties related to [load] -/ def chunk_zero_val : memory_chunk → val | Mint8unsigned := Vint 0 | Mint8signed := Vint 0 | Mint16unsigned := Vint 0 | Mint16signed := Vint 0 | Mint32 := Vint 0 | Mint64 := Vlong 0 | Mfloat32 := Vsingle 0 | Mfloat64 := Vfloat 0 | Many32 := Vundef | Many64 := Vundef def read_as_zero (m : mem) (b : block) (ofs len : ℕ) : Prop := ∀ (chunk : memory_chunk) p, ofs ≤ p → p + chunk.size ≤ ofs + len → chunk.align ∣ p → load chunk m b p = some (chunk_zero_val chunk) theorem read_as_zero_unchanged {P : block → ℕ → Prop} {m b ofs len m'} : read_as_zero m b ofs len → unchanged_on P m m' → (∀ i, ofs ≤ i → i < ofs + len → P b i) → read_as_zero m' b ofs len := sorry' lemma store_zeros_read_as_zero {m b p n m'} : store_zeros m b p n = some m' → read_as_zero m' b p n := sorry' def load_store_init_data (m : mem) (b : block) : ℕ → list init_data → Prop | p [] := true | p (init_data.int8 n :: il') := load Mint8unsigned m b p = some (Vint (zero_ext 8 n)) ∧ load_store_init_data (p + 1) il' | p (init_data.int16 n :: il') := load Mint16unsigned m b p = some (Vint (zero_ext 16 n)) ∧ load_store_init_data (p + 2) il' | p (init_data.int32 n :: il') := load Mint32 m b p = some (Vint n) ∧ load_store_init_data (p + 4) il' | p (init_data.int64 n :: il') := load Mint64 m b p = some (Vlong n) ∧ load_store_init_data (p + 8) il' | p (init_data.float32 n :: il') := load Mfloat32 m b p = some (Vsingle n) ∧ load_store_init_data (p + 4) il' | p (init_data.float64 n :: il') := load Mfloat64 m b p = some (Vfloat n) ∧ load_store_init_data (p + 8) il' | p (init_data.addrof symb ofs :: il') := (∃ b', find_symbol ge symb = some b' ∧ load Mptr m b p = some (Vptr b' ofs)) ∧ load_store_init_data (p + (Mptr).size) il' | p (init_data.space n :: il') := read_as_zero m b p n ∧ load_store_init_data (p + n) il' lemma store_init_data_list_charact {b il m p m'} : store_init_data_list ge m b p il = some m' → read_as_zero m b p (init_data.list_size il) → load_store_init_data ge m' b p il := sorry' theorem alloc_global_unchanged {P : block → ℕ → Prop} {m id g m'} : alloc_global ge m (id, g) = some m' → unchanged_on P m m' := sorry' theorem alloc_globals_unchanged {P : block → ℕ → Prop} {gl m m'} : alloc_globals ge m gl = some m' → unchanged_on P m m' := sorry' theorem load_store_init_data_invariant {m m' b} : (∀ chunk ofs, load chunk m' b ofs = load chunk m b ofs) → ∀ il p, load_store_init_data ge m b p il → load_store_init_data ge m' b p il := sorry' def globdef_initialized (m : mem) (b : block) : globdef F V → Prop | (Gfun f) := perm m b 0 Cur Nonempty ∧ (∀ ofs k p, perm m b ofs k p → ofs = 0 ∧ p = Nonempty) | (Gvar v) := range_perm m b 0 (init_data.list_size v.init) Cur (perm_globvar v) ∧ (∀ ofs k p, perm m b ofs k p → ofs < init_data.list_size v.init ∧ perm_order (perm_globvar v) p) ∧ (¬ v.volatile → load_store_init_data ge m b 0 v.init) ∧ (¬ v.volatile → load_bytes m b 0 (init_data.list_size v.init) = some (bytes_of_init_data_list ge v.init)) def globals_initialized (g : Genv F V) (m : mem) := ∀ b gd, find_def g b = some gd → globdef_initialized ge m b gd lemma alloc_global_initialized {g : Genv F V} {m : mem} {id gd m'} : g.next = m.nextblock → alloc_global ge m (id, gd) = some m' → globals_initialized ge g m → globals_initialized ge (add_global g (id, gd)) m' ∧ (add_global g (id, gd)).next = m'.nextblock := sorry' lemma alloc_globals_initialized {gl} {g : Genv F V} {m m'} : alloc_globals g m gl = some m' → g.next = m.nextblock → globals_initialized ge g m → globals_initialized ge (add_globals g gl) m' := sorry' end init_mem def init_mem (p : program F V) := alloc_globals (globalenv p) ∅ p.defs lemma init_mem_genv_next {p : program F V} {m} : init_mem p = some m → (globalenv p).next = m.nextblock := sorry' theorem find_symbol_not_fresh {p : program F V} {id b m} : init_mem p = some m → find_symbol (globalenv p) id = some b → valid_block m b := sorry' theorem find_def_not_fresh {p : program F V} {b g m} : init_mem p = some m → find_def (globalenv p) b = some g → valid_block m b := sorry' theorem find_funct_ptr_not_fresh {p : program F V} {b f m} : init_mem p = some m → find_funct_ptr (globalenv p) b = some f → valid_block m b := sorry' theorem find_var_info_not_fresh {p : program F V} {b gv m} : init_mem p = some m → find_var_info (globalenv p) b = some gv → valid_block m b := sorry' lemma init_mem_characterization_gen {p : program F V} {m} : init_mem p = some m → globals_initialized (globalenv p) (globalenv p) m := sorry' theorem init_mem_characterization {p : program F V} {b gv m} : find_var_info (globalenv p) b = some gv → init_mem p = some m → range_perm m b 0 (init_data.list_size gv.init) Cur (perm_globvar gv) ∧ (∀ ofs k p, perm m b ofs k p → ofs < init_data.list_size gv.init ∧ perm_order (perm_globvar gv) p) ∧ (¬ gv.volatile → load_store_init_data (globalenv p) m b 0 gv.init) ∧ (¬ gv.volatile → load_bytes m b 0 (init_data.list_size gv.init) = some (bytes_of_init_data_list (globalenv p) gv.init)) := sorry' theorem init_mem_characterization_2 {p : program F V} {b fd m} : find_funct_ptr (globalenv p) b = some fd → init_mem p = some m → perm m b 0 Cur Nonempty ∧ (∀ ofs k p, perm m b ofs k p → ofs = 0 ∧ p = Nonempty) := sorry' /- ** Compatibility with memory injections -/ section init_mem_inj variable {ge : Genv F V} variable {thr : block} variable (symb_inject : ∀ id b, find_symbol ge id = some b → b < thr) include symb_inject lemma store_zeros_neutral {m b p n m'} : inject_neutral thr m → b < thr → store_zeros m b p n = some m' → inject_neutral thr m' := sorry' lemma store_init_data_neutral {m b p id m'} : inject_neutral thr m → b < thr → store_init_data ge m b p id = some m' → inject_neutral thr m' := sorry' lemma store_init_data_list_neutral {b idl m p m'} : inject_neutral thr m → b < thr → store_init_data_list ge m b p idl = some m' → inject_neutral thr m' := sorry' lemma alloc_global_neutral {idg m m'} : alloc_global ge m idg = some m' → inject_neutral thr m → m.nextblock < thr → inject_neutral thr m' := sorry' theorem advance_next_le : ∀ gl x, x ≤ @advance_next F V gl x := sorry' lemma alloc_globals_neutral {gl m m'} : alloc_globals ge m gl = some m' → inject_neutral thr m → m'.nextblock ≤ thr → inject_neutral thr m' := sorry' end init_mem_inj theorem initmem_inject {p : program F V} {m} : init_mem p = some m → inject (flat_inj m.nextblock) m m := sorry' /- ** Sufficient and necessary conditions for the initial memory to exist. -/ /- Alignment properties -/ section init_mem_inversion variable (ge : Genv F V) lemma store_init_data_aligned {m b p i m'} : store_init_data ge m b p i = some m' → i.align ∣ p := sorry' lemma store_init_data_list_aligned {b il m p m'} : store_init_data_list ge m b p il = some m' → init_data.list_aligned p il := sorry' lemma store_init_data_list_free_idents {b i o il m p m'} : store_init_data_list ge m b p il = some m' → init_data.addrof i o ∈ il → ∃ b', find_symbol ge i = some b' := sorry' end init_mem_inversion theorem init_mem_inversion {p : program F V} {m id v} : init_mem p = some m → (id, Gvar v) ∈ p.defs → init_data.list_aligned 0 v.init ∧ ∀ i o, init_data.addrof i o ∈ v.init → ∃ b, find_symbol (globalenv p) i = some b := sorry' section init_mem_exists variable (ge : Genv F V) lemma store_zeros_exists {m b p n} : range_perm m b p (p + n) Cur Writable → ∃ m', store_zeros m b p n = some m' := sorry' lemma store_init_data_exists {m b p} {i : init_data} : range_perm m b p (p + i.size) Cur Writable → i.align ∣ p → (∀ id ofs, i = init_data.addrof id ofs → ∃ b, find_symbol ge id = some b) → ∃ m', store_init_data ge m b p i = some m' := sorry' lemma store_init_data_list_exists {b il m p} : range_perm m b p (p + init_data.list_size il) Cur Writable → init_data.list_aligned p il → (∀ id ofs, init_data.addrof id ofs ∈ il → ∃ b, find_symbol ge id = some b) → ∃ m', store_init_data_list ge m b p il = some m' := sorry' def alloc_global_exists_ty : globdef F V → Prop | (Gfun f) := true | (Gvar v) := init_data.list_aligned 0 v.init ∧ ∀ i o, init_data.addrof i o ∈ v.init → ∃ b, find_symbol ge i = some b lemma alloc_global_exists {m id g} : alloc_global_exists_ty ge g → ∃ m', alloc_global ge m (id, g) = some m' := sorry' end init_mem_exists theorem init_mem_exists {p : program F V} : (∀ id v, (id, Gvar v) ∈ p.defs → init_data.list_aligned 0 v.init ∧ ∀ i o, init_data.addrof i o ∈ v.init → ∃ b, find_symbol (globalenv p) i = some b) → ∃ m, init_mem p = some m := sorry' end /- * Commutation with program transformations -/ section match_genvs parameters {A B V W : Type} (R : globdef A V → globdef B W → Prop) structure match_genvs (ge1 : Genv A V) (ge2 : Genv B W) : Prop := (next : ge2.next = ge1.next) (symb : ∀ id, PTree.get id ge2.symb = PTree.get id ge1.symb) (defs : ∀ b, option.rel R (PTree.get b ge1.defs) (PTree.get b ge2.defs)) lemma add_global_match {ge1 ge2 id g1 g2} : match_genvs ge1 ge2 → R g1 g2 → match_genvs (ge1.add_global (id, g1)) (ge2.add_global (id, g2)) := sorry' lemma add_globals_match {gl1 gl2} : list.forall2 (λ ⟨id1, g1⟩ ⟨id2, g2⟩, id1 = id2 ∧ R g1 g2 : (ident × globdef A V) → (ident × globdef B W) → Prop) gl1 gl2 → ∀ {ge1 ge2}, match_genvs ge1 ge2 → match_genvs (ge1.add_globals gl1) (ge2.add_globals gl2) := sorry' end match_genvs section match_programs parameters {C F1 V1 F2 V2 : Type} [linker C] [linker F1] [linker V1] parameter {match_fundef : C → F1 → F2 → Prop} parameter {match_varinfo : V1 → V2 → Prop} parameter {ctx : C} parameter {p : program F1 V1} parameter {tp : program F2 V2} parameter (progmatch : match_program_gen match_fundef match_varinfo ctx p tp) lemma globalenvs_match : match_genvs (match_globdef match_fundef match_varinfo ctx) (globalenv p) (globalenv tp) := sorry' theorem find_def_match_2 {b} : option.rel (match_globdef match_fundef match_varinfo ctx) (find_def (globalenv p) b) (find_def (globalenv tp) b) := sorry' theorem find_funct_ptr_match {b f} : find_funct_ptr (globalenv p) b = some f → ∃ cunit tf, find_funct_ptr (globalenv tp) b = some tf ∧ match_fundef cunit f tf ∧ linkorder cunit ctx := sorry' theorem find_funct_match {v f} : find_funct (globalenv p) v = some f → ∃ cunit tf, find_funct (globalenv tp) v = some tf ∧ match_fundef cunit f tf ∧ linkorder cunit ctx := sorry' theorem find_var_info_match {b v} : find_var_info (globalenv p) b = some v → ∃ tv, find_var_info (globalenv tp) b = some tv ∧ match_globvar match_varinfo v tv := sorry' theorem find_symbol_match {s : ident} : find_symbol (globalenv tp) s = find_symbol (globalenv p) s := sorry' theorem senv_match : Senv.equiv (globalenv p) (globalenv tp) := sorry' lemma store_init_data_list_match {idl m b ofs m'} : store_init_data_list (globalenv p) m b ofs idl = some m' → store_init_data_list (globalenv tp) m b ofs idl = some m' := sorry' lemma alloc_globals_match {gl1 gl2} : list.forall2 (match_ident_globdef match_fundef match_varinfo ctx) gl1 gl2 → ∀ m m', alloc_globals (globalenv p) m gl1 = some m' → alloc_globals (globalenv tp) m gl2 = some m' := sorry' theorem init_mem_match {m} : init_mem p = some m → init_mem tp = some m := sorry' end match_programs /- Special case for partial transformations that do not depend on the compilation unit -/ section transform_partial parameters {A B V : Type} [linker A] [linker V] parameters {transf : A → res B} {p : program A V} {tp : program B V} parameter (progmatch : match_program (λ cu f tf, transf f = OK tf) eq p tp) theorem find_funct_ptr_transf_partial {b f} : find_funct_ptr (globalenv p) b = some f → ∃ tf, find_funct_ptr (globalenv tp) b = some tf ∧ transf f = OK tf := sorry' theorem find_funct_transf_partial {v f} : find_funct (globalenv p) v = some f → ∃ tf, find_funct (globalenv tp) v = some tf ∧ transf f = OK tf := sorry' theorem find_symbol_transf_partial {s : ident} : find_symbol (globalenv tp) s = find_symbol (globalenv p) s := sorry' theorem senv_transf_partial : Senv.equiv (globalenv p) (globalenv tp) := sorry' theorem init_mem_transf_partial {m} : init_mem p = some m → init_mem tp = some m := sorry' end transform_partial /- Special case for total transformations that do not depend on the compilation unit -/ section transform_total parameters {A B V : Type} [linker A] [linker V] parameters {transf : A → B} {p : program A V} {tp : program B V} parameter (progmatch : match_program (λ cu f tf, tf = transf f) eq p tp) theorem find_funct_ptr_transf {b f} : find_funct_ptr (globalenv p) b = some f → find_funct_ptr (globalenv tp) b = some (transf f) := sorry' theorem find_funct_transf {v f} : find_funct (globalenv p) v = some f → find_funct (globalenv tp) v = some (transf f) := sorry' theorem find_symbol_transf {s : ident} : find_symbol (globalenv tp) s = find_symbol (globalenv p) s := sorry' theorem senv_transf : Senv.equiv (globalenv p) (globalenv tp) := sorry' theorem init_mem_transf {m} : init_mem p = some m → init_mem tp = some m := sorry' end transform_total end Genv end globalenvs
2bd2b7da6c51f09719292f4c846bbc88e11625d9
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Util/Recognizers.lean
346d9c52049f62ea5ee3aa440b4d568984bcd42e
[ "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
3,969
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.Environment namespace Lean namespace Expr @[inline] def const? (e : Expr) : Option (Name × List Level) := match e with | Expr.const n us => some (n, us) | _ => none @[inline] def app1? (e : Expr) (fName : Name) : Option Expr := if e.isAppOfArity fName 1 then some e.appArg! else none @[inline] def app2? (e : Expr) (fName : Name) : Option (Expr × Expr) := if e.isAppOfArity fName 2 then some (e.appFn!.appArg!, e.appArg!) else none @[inline] def app3? (e : Expr) (fName : Name) : Option (Expr × Expr × Expr) := if e.isAppOfArity fName 3 then some (e.appFn!.appFn!.appArg!, e.appFn!.appArg!, e.appArg!) else none @[inline] def app4? (e : Expr) (fName : Name) : Option (Expr × Expr × Expr × Expr) := if e.isAppOfArity fName 4 then some (e.appFn!.appFn!.appFn!.appArg!, e.appFn!.appFn!.appArg!, e.appFn!.appArg!, e.appArg!) else none @[inline] def eq? (p : Expr) : Option (Expr × Expr × Expr) := p.app3? ``Eq @[inline] def ne? (p : Expr) : Option (Expr × Expr × Expr) := p.app3? ``Ne @[inline] def iff? (p : Expr) : Option (Expr × Expr) := p.app2? ``Iff @[inline] def not? (p : Expr) : Option Expr := p.app1? ``Not @[inline] def notNot? (p : Expr) : Option Expr := match p.not? with | some p => p.not? | none => none @[inline] def and? (p : Expr) : Option (Expr × Expr) := p.app2? ``And @[inline] def heq? (p : Expr) : Option (Expr × Expr × Expr × Expr) := p.app4? ``HEq def natAdd? (e : Expr) : Option (Expr × Expr) := e.app2? ``Nat.add @[inline] def arrow? : Expr → Option (Expr × Expr) | Expr.forallE _ α β _ => if β.hasLooseBVars then none else some (α, β) | _ => none def isEq (e : Expr) := e.isAppOfArity ``Eq 3 def isHEq (e : Expr) := e.isAppOfArity ``HEq 4 def isIte (e : Expr) := e.isAppOfArity ``ite 5 def isDIte (e : Expr) := e.isAppOfArity ``dite 5 partial def listLit? (e : Expr) : Option (Expr × List Expr) := let rec loop (e : Expr) (acc : List Expr) := if e.isAppOfArity' ``List.nil 1 then some (e.appArg!', acc.reverse) else if e.isAppOfArity' ``List.cons 3 then loop e.appArg!' (e.appFn!'.appArg!' :: acc) else none loop e [] def arrayLit? (e : Expr) : Option (Expr × List Expr) := if e.isAppOfArity' ``List.toArray 2 then listLit? e.appArg!' else none /-- Recognize `α × β` -/ def prod? (e : Expr) : Option (Expr × Expr) := e.app2? ``Prod private def getConstructorVal? (env : Environment) (ctorName : Name) : Option ConstructorVal := match env.find? ctorName with | some (ConstantInfo.ctorInfo v) => v | _ => none def isConstructorApp? (env : Environment) (e : Expr) : Option ConstructorVal := match e with | Expr.lit (Literal.natVal n) => if n == 0 then getConstructorVal? env `Nat.zero else getConstructorVal? env `Nat.succ | _ => match e.getAppFn with | Expr.const n _ => match getConstructorVal? env n with | some v => if v.numParams + v.numFields == e.getAppNumArgs then some v else none | none => none | _ => none def isConstructorApp (env : Environment) (e : Expr) : Bool := e.isConstructorApp? env |>.isSome def constructorApp? (env : Environment) (e : Expr) : Option (ConstructorVal × Array Expr) := do match e with | Expr.lit (Literal.natVal n) => if n == 0 then do let v ← getConstructorVal? env `Nat.zero pure (v, #[]) else do let v ← getConstructorVal? env `Nat.succ pure (v, #[mkNatLit (n-1)]) | _ => match e.getAppFn with | Expr.const n _ => do let v ← getConstructorVal? env n if v.numParams + v.numFields == e.getAppNumArgs then pure (v, e.getAppArgs) else none | _ => none end Lean.Expr
30d3209f001ac81b4431bf037ef4a5fedcf3d5e4
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/matrix_algebra.lean
ca2d5e863b7a91c42853e5eca9085cc6a8fcd39c
[]
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,999
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.tensor_product import Mathlib.PostPort universes u v w namespace Mathlib /-! We provide the `R`-algebra structure on `matrix n n A` when `A` is an `R`-algebra, and show `matrix n n A ≃ₐ[R] (A ⊗[R] matrix n n R)`. -/ protected instance matrix.algebra {R : Type u} [comm_semiring R] {A : Type v} [semiring A] [algebra R A] {n : Type w} [fintype n] [DecidableEq n] : algebra R (matrix n n A) := algebra.mk (ring_hom.mk (ring_hom.to_fun (ring_hom.comp (matrix.scalar n) (algebra_map R A))) sorry sorry sorry sorry) sorry sorry theorem algebra_map_matrix_apply {R : Type u} [comm_semiring R] {A : Type v} [semiring A] [algebra R A] {n : Type w} [fintype n] [DecidableEq n] {r : R} {i : n} {j : n} : coe_fn (algebra_map R (matrix n n A)) r i j = ite (i = j) (coe_fn (algebra_map R A) r) 0 := sorry namespace matrix_equiv_tensor /-- (Implementation detail). The bare function underlying `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`, on pure tensors. -/ def to_fun (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] (a : A) (m : matrix n n R) : matrix n n A := fun (i j : n) => a * coe_fn (algebra_map R A) (m i j) /-- (Implementation detail). The function underlying `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`, as an `R`-linear map in the second tensor factor. -/ def to_fun_right_linear (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] (a : A) : linear_map R (matrix n n R) (matrix n n A) := linear_map.mk (to_fun R A n a) sorry sorry /-- (Implementation detail). The function underlying `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`, as an `R`-bilinear map. -/ def to_fun_bilinear (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] : linear_map R A (linear_map R (matrix n n R) (matrix n n A)) := linear_map.mk (to_fun_right_linear R A n) sorry sorry /-- (Implementation detail). The function underlying `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`, as an `R`-linear map. -/ def to_fun_linear (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] : linear_map R (tensor_product R A (matrix n n R)) (matrix n n A) := tensor_product.lift (to_fun_bilinear R A n) /-- The function `(A ⊗[R] matrix n n R) →ₐ[R] matrix n n A`, as an algebra homomorphism. -/ def to_fun_alg_hom (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] : alg_hom R (tensor_product R A (matrix n n R)) (matrix n n A) := algebra.tensor_product.alg_hom_of_linear_map_tensor_product (to_fun_linear R A n) sorry sorry @[simp] theorem to_fun_alg_hom_apply (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (a : A) (m : matrix n n R) : coe_fn (to_fun_alg_hom R A n) (tensor_product.tmul R a m) = fun (i j : n) => a * coe_fn (algebra_map R A) (m i j) := sorry /-- (Implementation detail.) The bare function `matrix n n A → A ⊗[R] matrix n n R`. (We don't need to show that it's an algebra map, thankfully --- just that it's an inverse.) -/ def inv_fun (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (M : matrix n n A) : tensor_product R A (matrix n n R) := finset.sum finset.univ fun (p : n × n) => tensor_product.tmul R (M (prod.fst p) (prod.snd p)) (matrix.std_basis_matrix (prod.fst p) (prod.snd p) 1) @[simp] theorem inv_fun_zero (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] : inv_fun R A n 0 = 0 := sorry @[simp] theorem inv_fun_add (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (M : matrix n n A) (N : matrix n n A) : inv_fun R A n (M + N) = inv_fun R A n M + inv_fun R A n N := sorry @[simp] theorem inv_fun_smul (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (a : A) (M : matrix n n A) : (inv_fun R A n fun (i j : n) => a * M i j) = tensor_product.tmul R a 1 * inv_fun R A n M := sorry @[simp] theorem inv_fun_algebra_map (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (M : matrix n n R) : (inv_fun R A n fun (i j : n) => coe_fn (algebra_map R A) (M i j)) = tensor_product.tmul R 1 M := sorry theorem right_inv (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (M : matrix n n A) : coe_fn (to_fun_alg_hom R A n) (inv_fun R A n M) = M := sorry theorem left_inv (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (M : tensor_product R A (matrix n n R)) : inv_fun R A n (coe_fn (to_fun_alg_hom R A n) M) = M := sorry /-- (Implementation detail) The equivalence, ignoring the algebra structure, `(A ⊗[R] matrix n n R) ≃ matrix n n A`. -/ def equiv (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] : tensor_product R A (matrix n n R) ≃ matrix n n A := equiv.mk (⇑(to_fun_alg_hom R A n)) (inv_fun R A n) sorry sorry end matrix_equiv_tensor /-- The `R`-algebra isomorphism `matrix n n A ≃ₐ[R] (A ⊗[R] matrix n n R)`. -/ def matrix_equiv_tensor (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] : alg_equiv R (matrix n n A) (tensor_product R A (matrix n n R)) := alg_equiv.symm (alg_equiv.mk (alg_hom.to_fun sorry) (equiv.inv_fun sorry) sorry sorry sorry sorry sorry) @[simp] theorem matrix_equiv_tensor_apply (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (M : matrix n n A) : coe_fn (matrix_equiv_tensor R A n) M = finset.sum finset.univ fun (p : n × n) => tensor_product.tmul R (M (prod.fst p) (prod.snd p)) (matrix.std_basis_matrix (prod.fst p) (prod.snd p) 1) := rfl @[simp] theorem matrix_equiv_tensor_apply_std_basis (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (i : n) (j : n) (x : A) : coe_fn (matrix_equiv_tensor R A n) (matrix.std_basis_matrix i j x) = tensor_product.tmul R x (matrix.std_basis_matrix i j 1) := sorry @[simp] theorem matrix_equiv_tensor_apply_symm (R : Type u) [comm_semiring R] (A : Type v) [semiring A] [algebra R A] (n : Type w) [fintype n] [DecidableEq n] (a : A) (M : matrix n n R) : coe_fn (alg_equiv.symm (matrix_equiv_tensor R A n)) (tensor_product.tmul R a M) = fun (i j : n) => a * coe_fn (algebra_map R A) (M i j) := sorry
928d70125e46c1e66ae963acac2b07423ffc5084
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/Match/CaseArraySizes.lean
3d9832d7f44c346f0844d617c5a21fdefec18214
[ "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
3,728
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.Tactic.Assert import Lean.Meta.Match.CaseValues namespace Lean.Meta structure CaseArraySizesSubgoal where mvarId : MVarId elems : Array FVarId := #[] diseqs : Array FVarId := #[] subst : FVarSubst := {} deriving Inhabited def getArrayArgType (a : Expr) : MetaM Expr := do let aType ← inferType a let aType ← whnfD aType unless aType.isAppOfArity `Array 1 do throwError "array expected{indentExpr a}" pure aType.appArg! private def mkArrayGetLit (a : Expr) (i : Nat) (n : Nat) (h : Expr) : MetaM Expr := do let lt ← mkLt (mkRawNatLit i) (mkRawNatLit n) let ltPrf ← mkDecideProof lt mkAppM `Array.getLit #[a, mkRawNatLit i, h, ltPrf] private partial def introArrayLit (mvarId : MVarId) (a : Expr) (n : Nat) (xNamePrefix : Name) (aSizeEqN : Expr) : MetaM MVarId := do let α ← getArrayArgType a let rec loop (i : Nat) (xs : Array Expr) (args : Array Expr) := do if i < n then withLocalDeclD (xNamePrefix.appendIndexAfter (i+1)) α fun xi => do let xs := xs.push xi let ai ← mkArrayGetLit a i n aSizeEqN let args := args.push ai loop (i+1) xs args else let xsLit ← mkArrayLit α xs.toList let aEqXsLit ← mkEq a xsLit let aEqLitPrf ← mkAppM ``Array.toArrayLit_eq #[a, mkRawNatLit n, aSizeEqN] withLocalDeclD `hEqALit aEqXsLit fun heq => do let target ← mvarId.getType let newTarget ← mkForallFVars (xs.push heq) target pure (newTarget, args.push aEqLitPrf) let (newTarget, args) ← loop 0 #[] #[] let tag ← mvarId.getTag let newMVar ← mkFreshExprSyntheticOpaqueMVar newTarget tag mvarId.assign (mkAppN newMVar args) pure newMVar.mvarId! /-- Split goal `... |- C a` into sizes.size + 1 subgoals 1) `..., x_1 ... x_{sizes[0]} |- C #[x_1, ... x_{sizes[0]}]` ... n) `..., x_1 ... x_{sizes[n-1]} |- C #[x_1, ..., x_{sizes[n-1]}]` n+1) `..., (h_1 : a.size != sizes[0]), ..., (h_n : a.size != sizes[n-1]) |- C a` where `n = sizes.size` -/ def caseArraySizes (mvarId : MVarId) (fvarId : FVarId) (sizes : Array Nat) (xNamePrefix := `x) (hNamePrefix := `h) : MetaM (Array CaseArraySizesSubgoal) := mvarId.withContext do let a := mkFVar fvarId let aSize ← mkAppM `Array.size #[a] let mvarId ← mvarId.assertExt `aSize (mkConst `Nat) aSize let (aSizeFVarId, mvarId) ← mvarId.intro1 let (hEq, mvarId) ← mvarId.intro1 let subgoals ← caseValues mvarId aSizeFVarId (sizes.map mkRawNatLit) hNamePrefix subgoals.mapIdxM fun i subgoal => do let subst := subgoal.subst let mvarId := subgoal.mvarId let hEqSz := (subst.get hEq).fvarId! if h : i.val < sizes.size then let n := sizes.get ⟨i, h⟩ let mvarId ← mvarId.clear subgoal.newHs[0]! let mvarId ← mvarId.clear (subst.get aSizeFVarId).fvarId! mvarId.withContext do let hEqSzSymm ← mkEqSymm (mkFVar hEqSz) let mvarId ← introArrayLit mvarId a n xNamePrefix hEqSzSymm let (xs, mvarId) ← mvarId.introN n let (hEqLit, mvarId) ← mvarId.intro1 let mvarId ← mvarId.clear hEqSz let (subst, mvarId) ← substCore mvarId hEqLit false subst pure { mvarId := mvarId, elems := xs, subst := subst } else let (subst, mvarId) ← substCore mvarId hEq false subst let diseqs := subgoal.newHs.map fun fvarId => (subst.get fvarId).fvarId! pure { mvarId := mvarId, diseqs := diseqs, subst := subst } end Lean.Meta
11063c6b601ffc55a94b7ce63988824d041fa847
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/integral/set_integral.lean
a6ed1044d17f86ba3f9c66eebf5a943ec8cf637e
[ "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
51,395
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov -/ import measure_theory.integral.integrable_on import measure_theory.integral.bochner import order.filter.indicator_function import topology.metric_space.thickened_indicator /-! # Set integral In this file we prove some properties of `∫ x in s, f x ∂μ`. Recall that this notation is defined as `∫ x, f x ∂(μ.restrict s)`. In `integral_indicator` we prove that for a measurable function `f` and a measurable set `s` this definition coincides with another natural definition: `∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ`, where `indicator s f x` is equal to `f x` for `x ∈ s` and is zero otherwise. Since `∫ x in s, f x ∂μ` is a notation, one can rewrite or apply any theorem about `∫ x, f x ∂μ` directly. In this file we prove some theorems about dependence of `∫ x in s, f x ∂μ` on `s`, e.g. `integral_union`, `integral_empty`, `integral_univ`. We use the property `integrable_on f s μ := integrable f (μ.restrict s)`, defined in `measure_theory.integrable_on`. We also defined in that same file a predicate `integrable_at_filter (f : α → E) (l : filter α) (μ : measure α)` saying that `f` is integrable at some set `s ∈ l`. Finally, we prove a version of the [Fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus) for set integral, see `filter.tendsto.integral_sub_linear_is_o_ae` and its corollaries. Namely, consider a measurably generated filter `l`, a measure `μ` finite at this filter, and a function `f` that has a finite limit `c` at `l ⊓ μ.ae`. Then `∫ x in s, f x ∂μ = μ s • c + o(μ s)` as `s` tends to `l.small_sets`, i.e. for any `ε>0` there exists `t ∈ l` such that `∥∫ x in s, f x ∂μ - μ s • c∥ ≤ ε * μ s` whenever `s ⊆ t`. We also formulate a version of this theorem for a locally finite measure `μ` and a function `f` continuous at a point `a`. ## Notation We provide the following notations for expressing the integral of a function on a set : * `∫ a in s, f a ∂μ` is `measure_theory.integral (μ.restrict s) f` * `∫ a in s, f a` is `∫ a in s, f a ∂volume` Note that the set notations are defined in the file `measure_theory/integral/bochner`, but we reference them here because all theorems about set integrals are in this file. -/ noncomputable theory open set filter topological_space measure_theory function open_locale classical topological_space interval big_operators filter ennreal nnreal measure_theory variables {α β E F : Type*} [measurable_space α] namespace measure_theory section normed_add_comm_group variables [normed_add_comm_group E] {f g : α → E} {s t : set α} {μ ν : measure α} {l l' : filter α} variables [complete_space E] [normed_space ℝ E] lemma set_integral_congr_ae (hs : measurable_set s) (h : ∀ᵐ x ∂μ, x ∈ s → f x = g x) : ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ := integral_congr_ae ((ae_restrict_iff' hs).2 h) lemma set_integral_congr (hs : measurable_set s) (h : eq_on f g s) : ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ := set_integral_congr_ae hs $ eventually_of_forall h lemma set_integral_congr_set_ae (hst : s =ᵐ[μ] t) : ∫ x in s, f x ∂μ = ∫ x in t, f x ∂μ := by rw measure.restrict_congr_set hst lemma integral_union_ae (hst : ae_disjoint μ s t) (ht : null_measurable_set t μ) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ∫ x in s ∪ t, f x ∂μ = ∫ x in s, f x ∂μ + ∫ x in t, f x ∂μ := by simp only [integrable_on, measure.restrict_union₀ hst ht, integral_add_measure hfs hft] lemma integral_union (hst : disjoint s t) (ht : measurable_set t) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ∫ x in s ∪ t, f x ∂μ = ∫ x in s, f x ∂μ + ∫ x in t, f x ∂μ := integral_union_ae hst.ae_disjoint ht.null_measurable_set hfs hft lemma integral_diff (ht : measurable_set t) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) (hts : t ⊆ s) : ∫ x in s \ t, f x ∂μ = ∫ x in s, f x ∂μ - ∫ x in t, f x ∂μ := begin rw [eq_sub_iff_add_eq, ← integral_union, diff_union_of_subset hts], exacts [disjoint_diff.symm, ht, hfs.mono_set (diff_subset _ _), hft] end lemma integral_finset_bUnion {ι : Type*} (t : finset ι) {s : ι → set α} (hs : ∀ i ∈ t, measurable_set (s i)) (h's : set.pairwise ↑t (disjoint on s)) (hf : ∀ i ∈ t, integrable_on f (s i) μ) : ∫ x in (⋃ i ∈ t, s i), f x ∂ μ = ∑ i in t, ∫ x in s i, f x ∂ μ := begin induction t using finset.induction_on with a t hat IH hs h's, { simp }, { simp only [finset.coe_insert, finset.forall_mem_insert, set.pairwise_insert, finset.set_bUnion_insert] at hs hf h's ⊢, rw [integral_union _ _ hf.1 (integrable_on_finset_Union.2 hf.2)], { rw [finset.sum_insert hat, IH hs.2 h's.1 hf.2] }, { simp only [disjoint_Union_right], exact (λ i hi, (h's.2 i hi (ne_of_mem_of_not_mem hi hat).symm).1) }, { exact finset.measurable_set_bUnion _ hs.2 } } end lemma integral_fintype_Union {ι : Type*} [fintype ι] {s : ι → set α} (hs : ∀ i, measurable_set (s i)) (h's : pairwise (disjoint on s)) (hf : ∀ i, integrable_on f (s i) μ) : ∫ x in (⋃ i, s i), f x ∂ μ = ∑ i, ∫ x in s i, f x ∂ μ := begin convert integral_finset_bUnion finset.univ (λ i hi, hs i) _ (λ i _, hf i), { simp }, { simp [pairwise_univ, h's] } end lemma integral_empty : ∫ x in ∅, f x ∂μ = 0 := by rw [measure.restrict_empty, integral_zero_measure] lemma integral_univ : ∫ x in univ, f x ∂μ = ∫ x, f x ∂μ := by rw [measure.restrict_univ] lemma integral_add_compl (hs : measurable_set s) (hfi : integrable f μ) : ∫ x in s, f x ∂μ + ∫ x in sᶜ, f x ∂μ = ∫ x, f x ∂μ := by rw [← integral_union (@disjoint_compl_right (set α) _ _) hs.compl hfi.integrable_on hfi.integrable_on, union_compl_self, integral_univ] /-- For a function `f` and a measurable set `s`, the integral of `indicator s f` over the whole space is equal to `∫ x in s, f x ∂μ` defined as `∫ x, f x ∂(μ.restrict s)`. -/ lemma integral_indicator (hs : measurable_set s) : ∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ := begin by_cases hfi : integrable_on f s μ, swap, { rwa [integral_undef, integral_undef], rwa integrable_indicator_iff hs }, calc ∫ x, indicator s f x ∂μ = ∫ x in s, indicator s f x ∂μ + ∫ x in sᶜ, indicator s f x ∂μ : (integral_add_compl hs (hfi.indicator hs)).symm ... = ∫ x in s, f x ∂μ + ∫ x in sᶜ, 0 ∂μ : congr_arg2 (+) (integral_congr_ae (indicator_ae_eq_restrict hs)) (integral_congr_ae (indicator_ae_eq_restrict_compl hs)) ... = ∫ x in s, f x ∂μ : by simp end lemma set_integral_indicator (ht : measurable_set t) : ∫ x in s, t.indicator f x ∂μ = ∫ x in s ∩ t, f x ∂μ := by rw [integral_indicator ht, measure.restrict_restrict ht, set.inter_comm] lemma of_real_set_integral_one_of_measure_ne_top {α : Type*} {m : measurable_space α} {μ : measure α} {s : set α} (hs : μ s ≠ ∞) : ennreal.of_real (∫ x in s, (1 : ℝ) ∂μ) = μ s := calc ennreal.of_real (∫ x in s, (1 : ℝ) ∂μ) = ennreal.of_real (∫ x in s, ∥(1 : ℝ)∥ ∂μ) : by simp only [norm_one] ... = ∫⁻ x in s, 1 ∂μ : begin rw of_real_integral_norm_eq_lintegral_nnnorm (integrable_on_const.2 (or.inr hs.lt_top)), simp only [nnnorm_one, ennreal.coe_one], end ... = μ s : set_lintegral_one _ lemma of_real_set_integral_one {α : Type*} {m : measurable_space α} (μ : measure α) [is_finite_measure μ] (s : set α) : ennreal.of_real (∫ x in s, (1 : ℝ) ∂μ) = μ s := of_real_set_integral_one_of_measure_ne_top (measure_ne_top μ s) lemma integral_piecewise [decidable_pred (∈ s)] (hs : measurable_set s) {f g : α → E} (hf : integrable_on f s μ) (hg : integrable_on g sᶜ μ) : ∫ x, s.piecewise f g x ∂μ = ∫ x in s, f x ∂μ + ∫ x in sᶜ, g x ∂μ := by rw [← set.indicator_add_compl_eq_piecewise, integral_add' (hf.indicator hs) (hg.indicator hs.compl), integral_indicator hs, integral_indicator hs.compl] lemma tendsto_set_integral_of_monotone {ι : Type*} [countable ι] [semilattice_sup ι] {s : ι → set α} {f : α → E} (hsm : ∀ i, measurable_set (s i)) (h_mono : monotone s) (hfi : integrable_on f (⋃ n, s n) μ) : tendsto (λ i, ∫ a in s i, f a ∂μ) at_top (𝓝 (∫ a in (⋃ n, s n), f a ∂μ)) := begin have hfi' : ∫⁻ x in ⋃ n, s n, ∥f x∥₊ ∂μ < ∞ := hfi.2, set S := ⋃ i, s i, have hSm : measurable_set S := measurable_set.Union hsm, have hsub : ∀ {i}, s i ⊆ S, from subset_Union s, rw [← with_density_apply _ hSm] at hfi', set ν := μ.with_density (λ x, ∥f x∥₊) with hν, refine metric.nhds_basis_closed_ball.tendsto_right_iff.2 (λ ε ε0, _), lift ε to ℝ≥0 using ε0.le, have : ∀ᶠ i in at_top, ν (s i) ∈ Icc (ν S - ε) (ν S + ε), from tendsto_measure_Union h_mono (ennreal.Icc_mem_nhds hfi'.ne (ennreal.coe_pos.2 ε0).ne'), refine this.mono (λ i hi, _), rw [mem_closed_ball_iff_norm', ← integral_diff (hsm i) hfi (hfi.mono_set hsub) hsub, ← coe_nnnorm, nnreal.coe_le_coe, ← ennreal.coe_le_coe], refine (ennnorm_integral_le_lintegral_ennnorm _).trans _, rw [← with_density_apply _ (hSm.diff (hsm _)), ← hν, measure_diff hsub (hsm _)], exacts [tsub_le_iff_tsub_le.mp hi.1, (hi.2.trans_lt $ ennreal.add_lt_top.2 ⟨hfi', ennreal.coe_lt_top⟩).ne] end lemma has_sum_integral_Union_ae {ι : Type*} [countable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, null_measurable_set (s i) μ) (hd : pairwise (ae_disjoint μ on s)) (hfi : integrable_on f (⋃ i, s i) μ) : has_sum (λ n, ∫ a in s n, f a ∂ μ) (∫ a in ⋃ n, s n, f a ∂μ) := begin simp only [integrable_on, measure.restrict_Union_ae hd hm] at hfi ⊢, exact has_sum_integral_measure hfi end lemma has_sum_integral_Union {ι : Type*} [countable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (hfi : integrable_on f (⋃ i, s i) μ) : has_sum (λ n, ∫ a in s n, f a ∂ μ) (∫ a in ⋃ n, s n, f a ∂μ) := has_sum_integral_Union_ae (λ i, (hm i).null_measurable_set) (hd.mono (λ i j h, h.ae_disjoint)) hfi lemma integral_Union {ι : Type*} [countable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (hfi : integrable_on f (⋃ i, s i) μ) : (∫ a in (⋃ n, s n), f a ∂μ) = ∑' n, ∫ a in s n, f a ∂ μ := (has_sum.tsum_eq (has_sum_integral_Union hm hd hfi)).symm lemma integral_Union_ae {ι : Type*} [countable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, null_measurable_set (s i) μ) (hd : pairwise (ae_disjoint μ on s)) (hfi : integrable_on f (⋃ i, s i) μ) : (∫ a in (⋃ n, s n), f a ∂μ) = ∑' n, ∫ a in s n, f a ∂ μ := (has_sum.tsum_eq (has_sum_integral_Union_ae hm hd hfi)).symm lemma set_integral_eq_zero_of_forall_eq_zero {f : α → E} (hf : strongly_measurable f) (ht_eq : ∀ x ∈ t, f x = 0) : ∫ x in t, f x ∂μ = 0 := begin refine integral_eq_zero_of_ae _, rw [eventually_eq, ae_restrict_iff (hf.measurable_set_eq_fun strongly_measurable_zero)], refine eventually_of_forall (λ x hx, _), rw pi.zero_apply, exact ht_eq x hx, end lemma set_integral_union_eq_left {f : α → E} (hf : strongly_measurable f) (hfi : integrable f μ) (hs : measurable_set s) (ht_eq : ∀ x ∈ t, f x = 0) : ∫ x in (s ∪ t), f x ∂μ = ∫ x in s, f x ∂μ := begin rw [← set.union_diff_self, union_comm, integral_union, set_integral_eq_zero_of_forall_eq_zero _ (λ x hx, ht_eq x (diff_subset _ _ hx)), zero_add], exacts [hf, disjoint_diff.symm, hs, hfi.integrable_on, hfi.integrable_on] end lemma set_integral_neg_eq_set_integral_nonpos [linear_order E] [order_closed_topology E] {f : α → E} (hf : strongly_measurable f) (hfi : integrable f μ) : ∫ x in {x | f x < 0}, f x ∂μ = ∫ x in {x | f x ≤ 0}, f x ∂μ := begin have h_union : {x | f x ≤ 0} = {x | f x < 0} ∪ {x | f x = 0}, by { ext, simp_rw [set.mem_union_eq, set.mem_set_of_eq], exact le_iff_lt_or_eq, }, rw h_union, exact (set_integral_union_eq_left hf hfi (hf.measurable_set_lt strongly_measurable_const) (λ x hx, hx)).symm, end lemma integral_norm_eq_pos_sub_neg {f : α → ℝ} (hf : strongly_measurable f) (hfi : integrable f μ) : ∫ x, ∥f x∥ ∂μ = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | f x ≤ 0}, f x ∂μ := have h_meas : measurable_set {x | 0 ≤ f x}, from strongly_measurable_const.measurable_set_le hf, calc ∫ x, ∥f x∥ ∂μ = ∫ x in {x | 0 ≤ f x}, ∥f x∥ ∂μ + ∫ x in {x | 0 ≤ f x}ᶜ, ∥f x∥ ∂μ : by rw ← integral_add_compl h_meas hfi.norm ... = ∫ x in {x | 0 ≤ f x}, f x ∂μ + ∫ x in {x | 0 ≤ f x}ᶜ, ∥f x∥ ∂μ : begin congr' 1, refine set_integral_congr h_meas (λ x hx, _), dsimp only, rw [real.norm_eq_abs, abs_eq_self.mpr _], exact hx, end ... = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | 0 ≤ f x}ᶜ, f x ∂μ : begin congr' 1, rw ← integral_neg, refine set_integral_congr h_meas.compl (λ x hx, _), dsimp only, rw [real.norm_eq_abs, abs_eq_neg_self.mpr _], rw [set.mem_compl_iff, set.nmem_set_of_eq] at hx, linarith, end ... = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | f x ≤ 0}, f x ∂μ : by { rw ← set_integral_neg_eq_set_integral_nonpos hf hfi, congr, ext1 x, simp, } lemma set_integral_const (c : E) : ∫ x in s, c ∂μ = (μ s).to_real • c := by rw [integral_const, measure.restrict_apply_univ] @[simp] lemma integral_indicator_const (e : E) ⦃s : set α⦄ (s_meas : measurable_set s) : ∫ (a : α), s.indicator (λ (x : α), e) a ∂μ = (μ s).to_real • e := by rw [integral_indicator s_meas, ← set_integral_const] @[simp] lemma integral_indicator_one ⦃s : set α⦄ (hs : measurable_set s) : ∫ a, s.indicator 1 a ∂μ = (μ s).to_real := (integral_indicator_const 1 hs).trans ((smul_eq_mul _).trans (mul_one _)) lemma set_integral_indicator_const_Lp {p : ℝ≥0∞} (hs : measurable_set s) (ht : measurable_set t) (hμt : μ t ≠ ∞) (x : E) : ∫ a in s, indicator_const_Lp p ht hμt x a ∂μ = (μ (t ∩ s)).to_real • x := calc ∫ a in s, indicator_const_Lp p ht hμt x a ∂μ = (∫ a in s, t.indicator (λ _, x) a ∂μ) : by rw set_integral_congr_ae hs (indicator_const_Lp_coe_fn.mono (λ x hx hxs, hx)) ... = (μ (t ∩ s)).to_real • x : by rw [integral_indicator_const _ ht, measure.restrict_apply ht] lemma integral_indicator_const_Lp {p : ℝ≥0∞} (ht : measurable_set t) (hμt : μ t ≠ ∞) (x : E) : ∫ a, indicator_const_Lp p ht hμt x a ∂μ = (μ t).to_real • x := calc ∫ a, indicator_const_Lp p ht hμt x a ∂μ = ∫ a in univ, indicator_const_Lp p ht hμt x a ∂μ : by rw integral_univ ... = (μ (t ∩ univ)).to_real • x : set_integral_indicator_const_Lp measurable_set.univ ht hμt x ... = (μ t).to_real • x : by rw inter_univ lemma set_integral_map {β} [measurable_space β] {g : α → β} {f : β → E} {s : set β} (hs : measurable_set s) (hf : ae_strongly_measurable f (measure.map g μ)) (hg : ae_measurable g μ) : ∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ := begin rw [measure.restrict_map_of_ae_measurable hg hs, integral_map (hg.mono_measure measure.restrict_le_self) (hf.mono_measure _)], exact measure.map_mono_of_ae_measurable measure.restrict_le_self hg end lemma _root_.measurable_embedding.set_integral_map {β} {_ : measurable_space β} {f : α → β} (hf : measurable_embedding f) (g : β → E) (s : set β) : ∫ y in s, g y ∂(measure.map f μ) = ∫ x in f ⁻¹' s, g (f x) ∂μ := by rw [hf.restrict_map, hf.integral_map] lemma _root_.closed_embedding.set_integral_map [topological_space α] [borel_space α] {β} [measurable_space β] [topological_space β] [borel_space β] {g : α → β} {f : β → E} (s : set β) (hg : closed_embedding g) : ∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ := hg.measurable_embedding.set_integral_map _ _ lemma measure_preserving.set_integral_preimage_emb {β} {_ : measurable_space β} {f : α → β} {ν} (h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) (g : β → E) (s : set β) : ∫ x in f ⁻¹' s, g (f x) ∂μ = ∫ y in s, g y ∂ν := (h₁.restrict_preimage_emb h₂ s).integral_comp h₂ _ lemma measure_preserving.set_integral_image_emb {β} {_ : measurable_space β} {f : α → β} {ν} (h₁ : measure_preserving f μ ν) (h₂ : measurable_embedding f) (g : β → E) (s : set α) : ∫ y in f '' s, g y ∂ν = ∫ x in s, g (f x) ∂μ := eq.symm $ (h₁.restrict_image_emb h₂ s).integral_comp h₂ _ lemma set_integral_map_equiv {β} [measurable_space β] (e : α ≃ᵐ β) (f : β → E) (s : set β) : ∫ y in s, f y ∂(measure.map e μ) = ∫ x in e ⁻¹' s, f (e x) ∂μ := e.measurable_embedding.set_integral_map f s lemma norm_set_integral_le_of_norm_le_const_ae {C : ℝ} (hs : μ s < ∞) (hC : ∀ᵐ x ∂μ.restrict s, ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := begin rw ← measure.restrict_apply_univ at *, haveI : is_finite_measure (μ.restrict s) := ⟨‹_›⟩, exact norm_integral_le_of_norm_le_const hC end lemma norm_set_integral_le_of_norm_le_const_ae' {C : ℝ} (hs : μ s < ∞) (hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) (hfm : ae_strongly_measurable f (μ.restrict s)) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := begin apply norm_set_integral_le_of_norm_le_const_ae hs, have A : ∀ᵐ (x : α) ∂μ, x ∈ s → ∥ae_strongly_measurable.mk f hfm x∥ ≤ C, { filter_upwards [hC, hfm.ae_mem_imp_eq_mk] with _ h1 h2 h3, rw [← h2 h3], exact h1 h3 }, have B : measurable_set {x | ∥(hfm.mk f) x∥ ≤ C} := hfm.strongly_measurable_mk.norm.measurable measurable_set_Iic, filter_upwards [hfm.ae_eq_mk, (ae_restrict_iff B).2 A] with _ h1 _, rwa h1, end lemma norm_set_integral_le_of_norm_le_const_ae'' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s) (hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae hs $ by rwa [ae_restrict_eq hsm, eventually_inf_principal] lemma norm_set_integral_le_of_norm_le_const {C : ℝ} (hs : μ s < ∞) (hC : ∀ x ∈ s, ∥f x∥ ≤ C) (hfm : ae_strongly_measurable f (μ.restrict s)) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae' hs (eventually_of_forall hC) hfm lemma norm_set_integral_le_of_norm_le_const' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s) (hC : ∀ x ∈ s, ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae'' hs hsm $ eventually_of_forall hC lemma set_integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f) (hfi : integrable_on f s μ) : ∫ x in s, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict s] 0 := integral_eq_zero_iff_of_nonneg_ae hf hfi lemma set_integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f) (hfi : integrable_on f s μ) : 0 < ∫ x in s, f x ∂μ ↔ 0 < μ (support f ∩ s) := begin rw [integral_pos_iff_support_of_nonneg_ae hf hfi, measure.restrict_apply₀], rw support_eq_preimage, exact hfi.ae_strongly_measurable.ae_measurable.null_measurable (measurable_set_singleton 0).compl end lemma set_integral_gt_gt {R : ℝ} {f : α → ℝ} (hR : 0 ≤ R) (hfm : measurable f) (hfint : integrable_on f {x | ↑R < f x} μ) (hμ : μ {x | ↑R < f x} ≠ 0): (μ {x | ↑R < f x}).to_real * R < ∫ x in {x | ↑R < f x}, f x ∂μ := begin have : integrable_on (λ x, R) {x | ↑R < f x} μ, { refine ⟨ae_strongly_measurable_const, lt_of_le_of_lt _ hfint.2⟩, refine set_lintegral_mono (measurable.nnnorm _).coe_nnreal_ennreal hfm.nnnorm.coe_nnreal_ennreal (λ x hx, _), { exact measurable_const }, { simp only [ennreal.coe_le_coe, real.nnnorm_of_nonneg hR, real.nnnorm_of_nonneg (hR.trans $ le_of_lt hx), subtype.mk_le_mk], exact le_of_lt hx } }, rw [← sub_pos, ← smul_eq_mul, ← set_integral_const, ← integral_sub hfint this, set_integral_pos_iff_support_of_nonneg_ae], { rw ← zero_lt_iff at hμ, rwa set.inter_eq_self_of_subset_right, exact λ x hx, ne.symm (ne_of_lt $ sub_pos.2 hx) }, { change ∀ᵐ x ∂(μ.restrict _), _, rw ae_restrict_iff, { exact eventually_of_forall (λ x hx, sub_nonneg.2 $ le_of_lt hx) }, { exact measurable_set_le measurable_zero (hfm.sub measurable_const) } }, { exact integrable.sub hfint this }, end lemma set_integral_trim {α} {m m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) {f : α → E} (hf_meas : strongly_measurable[m] f) {s : set α} (hs : measurable_set[m] s) : ∫ x in s, f x ∂μ = ∫ x in s, f x ∂(μ.trim hm) := by rwa [integral_trim hm hf_meas, restrict_trim hm μ] lemma integral_Icc_eq_integral_Ioc' [partial_order α] {f : α → E} {a b : α} (ha : μ {a} = 0) : ∫ t in Icc a b, f t ∂μ = ∫ t in Ioc a b, f t ∂μ := set_integral_congr_set_ae (Ioc_ae_eq_Icc' ha).symm lemma integral_Ioc_eq_integral_Ioo' [partial_order α] {f : α → E} {a b : α} (hb : μ {b} = 0) : ∫ t in Ioc a b, f t ∂μ = ∫ t in Ioo a b, f t ∂μ := set_integral_congr_set_ae (Ioo_ae_eq_Ioc' hb).symm lemma integral_Icc_eq_integral_Ioc [partial_order α] {f : α → E} {a b : α} [has_no_atoms μ] : ∫ t in Icc a b, f t ∂μ = ∫ t in Ioc a b, f t ∂μ := integral_Icc_eq_integral_Ioc' $ measure_singleton a lemma integral_Ioc_eq_integral_Ioo [partial_order α] {f : α → E} {a b : α} [has_no_atoms μ] : ∫ t in Ioc a b, f t ∂μ = ∫ t in Ioo a b, f t ∂μ := integral_Ioc_eq_integral_Ioo' $ measure_singleton b end normed_add_comm_group section mono variables {μ : measure α} {f g : α → ℝ} {s t : set α} (hf : integrable_on f s μ) (hg : integrable_on g s μ) lemma set_integral_mono_ae_restrict (h : f ≤ᵐ[μ.restrict s] g) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := integral_mono_ae hf hg h lemma set_integral_mono_ae (h : f ≤ᵐ[μ] g) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := set_integral_mono_ae_restrict hf hg (ae_restrict_of_ae h) lemma set_integral_mono_on (hs : measurable_set s) (h : ∀ x ∈ s, f x ≤ g x) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := set_integral_mono_ae_restrict hf hg (by simp [hs, eventually_le, eventually_inf_principal, ae_of_all _ h]) include hf hg -- why do I need this include, but we don't need it in other lemmas? lemma set_integral_mono_on_ae (hs : measurable_set s) (h : ∀ᵐ x ∂μ, x ∈ s → f x ≤ g x) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := by { refine set_integral_mono_ae_restrict hf hg _, rwa [eventually_le, ae_restrict_iff' hs], } omit hf hg lemma set_integral_mono (h : f ≤ g) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := integral_mono hf hg h lemma set_integral_mono_set (hfi : integrable_on f t μ) (hf : 0 ≤ᵐ[μ.restrict t] f) (hst : s ≤ᵐ[μ] t) : ∫ x in s, f x ∂μ ≤ ∫ x in t, f x ∂μ := integral_mono_measure (measure.restrict_mono_ae hst) hf hfi lemma set_integral_ge_of_const_le {c : ℝ} (hs : measurable_set s) (hμs : μ s ≠ ∞) (hf : ∀ x ∈ s, c ≤ f x) (hfint : integrable_on (λ (x : α), f x) s μ) : c * (μ s).to_real ≤ ∫ x in s, f x ∂μ := begin rw [mul_comm, ← smul_eq_mul, ← set_integral_const c], exact set_integral_mono_on (integrable_on_const.2 (or.inr hμs.lt_top)) hfint hs hf, end end mono section nonneg variables {μ : measure α} {f : α → ℝ} {s : set α} lemma set_integral_nonneg_of_ae_restrict (hf : 0 ≤ᵐ[μ.restrict s] f) : 0 ≤ ∫ a in s, f a ∂μ := integral_nonneg_of_ae hf lemma set_integral_nonneg_of_ae (hf : 0 ≤ᵐ[μ] f) : 0 ≤ ∫ a in s, f a ∂μ := set_integral_nonneg_of_ae_restrict (ae_restrict_of_ae hf) lemma set_integral_nonneg (hs : measurable_set s) (hf : ∀ a, a ∈ s → 0 ≤ f a) : 0 ≤ ∫ a in s, f a ∂μ := set_integral_nonneg_of_ae_restrict ((ae_restrict_iff' hs).mpr (ae_of_all μ hf)) lemma set_integral_nonneg_ae (hs : measurable_set s) (hf : ∀ᵐ a ∂μ, a ∈ s → 0 ≤ f a) : 0 ≤ ∫ a in s, f a ∂μ := set_integral_nonneg_of_ae_restrict $ by rwa [eventually_le, ae_restrict_iff' hs] lemma set_integral_le_nonneg {s : set α} (hs : measurable_set s) (hf : strongly_measurable f) (hfi : integrable f μ) : ∫ x in s, f x ∂μ ≤ ∫ x in {y | 0 ≤ f y}, f x ∂μ := begin rw [← integral_indicator hs, ← integral_indicator (strongly_measurable_const.measurable_set_le hf)], exact integral_mono (hfi.indicator hs) (hfi.indicator (strongly_measurable_const.measurable_set_le hf)) (indicator_le_indicator_nonneg s f), end lemma set_integral_nonpos_of_ae_restrict (hf : f ≤ᵐ[μ.restrict s] 0) : ∫ a in s, f a ∂μ ≤ 0 := integral_nonpos_of_ae hf lemma set_integral_nonpos_of_ae (hf : f ≤ᵐ[μ] 0) : ∫ a in s, f a ∂μ ≤ 0 := set_integral_nonpos_of_ae_restrict (ae_restrict_of_ae hf) lemma set_integral_nonpos (hs : measurable_set s) (hf : ∀ a, a ∈ s → f a ≤ 0) : ∫ a in s, f a ∂μ ≤ 0 := set_integral_nonpos_of_ae_restrict ((ae_restrict_iff' hs).mpr (ae_of_all μ hf)) lemma set_integral_nonpos_ae (hs : measurable_set s) (hf : ∀ᵐ a ∂μ, a ∈ s → f a ≤ 0) : ∫ a in s, f a ∂μ ≤ 0 := set_integral_nonpos_of_ae_restrict $ by rwa [eventually_le, ae_restrict_iff' hs] lemma set_integral_nonpos_le {s : set α} (hs : measurable_set s) (hf : strongly_measurable f) (hfi : integrable f μ) : ∫ x in {y | f y ≤ 0}, f x ∂μ ≤ ∫ x in s, f x ∂μ := begin rw [← integral_indicator hs, ← integral_indicator (hf.measurable_set_le strongly_measurable_const)], exact integral_mono (hfi.indicator (hf.measurable_set_le strongly_measurable_const)) (hfi.indicator hs) (indicator_nonpos_le_indicator s f), end end nonneg section tendsto_mono variables {μ : measure α} [normed_add_comm_group E] [complete_space E] [normed_space ℝ E] {s : ℕ → set α} {f : α → E} lemma _root_.antitone.tendsto_set_integral (hsm : ∀ i, measurable_set (s i)) (h_anti : antitone s) (hfi : integrable_on f (s 0) μ) : tendsto (λi, ∫ a in s i, f a ∂μ) at_top (𝓝 (∫ a in (⋂ n, s n), f a ∂μ)) := begin let bound : α → ℝ := indicator (s 0) (λ a, ∥f a∥), have h_int_eq : (λ i, ∫ a in s i, f a ∂μ) = (λ i, ∫ a, (s i).indicator f a ∂μ), from funext (λ i, (integral_indicator (hsm i)).symm), rw h_int_eq, rw ← integral_indicator (measurable_set.Inter hsm), refine tendsto_integral_of_dominated_convergence bound _ _ _ _, { intro n, rw ae_strongly_measurable_indicator_iff (hsm n), exact (integrable_on.mono_set hfi (h_anti (zero_le n))).1 }, { rw integrable_indicator_iff (hsm 0), exact hfi.norm, }, { simp_rw norm_indicator_eq_indicator_norm, refine λ n, eventually_of_forall (λ x, _), exact indicator_le_indicator_of_subset (h_anti (zero_le n)) (λ a, norm_nonneg _) _ }, { filter_upwards with a using le_trans (h_anti.tendsto_indicator _ _ _) (pure_le_nhds _), }, end end tendsto_mono /-! ### Continuity of the set integral We prove that for any set `s`, the function `λ f : α →₁[μ] E, ∫ x in s, f x ∂μ` is continuous. -/ section continuous_set_integral variables [normed_add_comm_group E] {𝕜 : Type*} [normed_field 𝕜] [normed_add_comm_group F] [normed_space 𝕜 F] {p : ℝ≥0∞} {μ : measure α} /-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by `(Lp.mem_ℒp f).restrict s).to_Lp f`. This map is additive. -/ lemma Lp_to_Lp_restrict_add (f g : Lp E p μ) (s : set α) : ((Lp.mem_ℒp (f + g)).restrict s).to_Lp ⇑(f + g) = ((Lp.mem_ℒp f).restrict s).to_Lp f + ((Lp.mem_ℒp g).restrict s).to_Lp g := begin ext1, refine (ae_restrict_of_ae (Lp.coe_fn_add f g)).mp _, refine (Lp.coe_fn_add (mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s)) (mem_ℒp.to_Lp g ((Lp.mem_ℒp g).restrict s))).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp g).restrict s)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp (f+g)).restrict s)).mono (λ x hx1 hx2 hx3 hx4 hx5, _), rw [hx4, hx1, pi.add_apply, hx2, hx3, hx5, pi.add_apply], end /-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by `(Lp.mem_ℒp f).restrict s).to_Lp f`. This map commutes with scalar multiplication. -/ lemma Lp_to_Lp_restrict_smul (c : 𝕜) (f : Lp F p μ) (s : set α) : ((Lp.mem_ℒp (c • f)).restrict s).to_Lp ⇑(c • f) = c • (((Lp.mem_ℒp f).restrict s).to_Lp f) := begin ext1, refine (ae_restrict_of_ae (Lp.coe_fn_smul c f)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp (c • f)).restrict s)).mp _, refine (Lp.coe_fn_smul c (mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s))).mono (λ x hx1 hx2 hx3 hx4, _), rw [hx2, hx1, pi.smul_apply, hx3, hx4, pi.smul_apply], end /-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by `(Lp.mem_ℒp f).restrict s).to_Lp f`. This map is non-expansive. -/ lemma norm_Lp_to_Lp_restrict_le (s : set α) (f : Lp E p μ) : ∥((Lp.mem_ℒp f).restrict s).to_Lp f∥ ≤ ∥f∥ := begin rw [Lp.norm_def, Lp.norm_def, ennreal.to_real_le_to_real (Lp.snorm_ne_top _) (Lp.snorm_ne_top _)], refine (le_of_eq _).trans (snorm_mono_measure _ measure.restrict_le_self), { exact s, }, exact snorm_congr_ae (mem_ℒp.coe_fn_to_Lp _), end variables (α F 𝕜) /-- Continuous linear map sending a function of `Lp F p μ` to the same function in `Lp F p (μ.restrict s)`. -/ def Lp_to_Lp_restrict_clm (μ : measure α) (p : ℝ≥0∞) [hp : fact (1 ≤ p)] (s : set α) : Lp F p μ →L[𝕜] Lp F p (μ.restrict s) := @linear_map.mk_continuous 𝕜 𝕜 (Lp F p μ) (Lp F p (μ.restrict s)) _ _ _ _ _ _ (ring_hom.id 𝕜) ⟨λ f, mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s), λ f g, Lp_to_Lp_restrict_add f g s, λ c f, Lp_to_Lp_restrict_smul c f s⟩ 1 (by { intro f, rw one_mul, exact norm_Lp_to_Lp_restrict_le s f, }) variables {α F 𝕜} variables (𝕜) lemma Lp_to_Lp_restrict_clm_coe_fn [hp : fact (1 ≤ p)] (s : set α) (f : Lp F p μ) : Lp_to_Lp_restrict_clm α F 𝕜 μ p s f =ᵐ[μ.restrict s] f := mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s) variables {𝕜} @[continuity] lemma continuous_set_integral [normed_space ℝ E] [complete_space E] (s : set α) : continuous (λ f : α →₁[μ] E, ∫ x in s, f x ∂μ) := begin haveI : fact ((1 : ℝ≥0∞) ≤ 1) := ⟨le_rfl⟩, have h_comp : (λ f : α →₁[μ] E, ∫ x in s, f x ∂μ) = (integral (μ.restrict s)) ∘ (λ f, Lp_to_Lp_restrict_clm α E ℝ μ 1 s f), { ext1 f, rw [function.comp_apply, integral_congr_ae (Lp_to_Lp_restrict_clm_coe_fn ℝ s f)], }, rw h_comp, exact continuous_integral.comp (Lp_to_Lp_restrict_clm α E ℝ μ 1 s).continuous, end end continuous_set_integral end measure_theory open measure_theory asymptotics metric variables {ι : Type*} [normed_add_comm_group E] /-- Fundamental theorem of calculus for set integrals: if `μ` is a measure that is finite at a filter `l` and `f` is a measurable function that has a finite limit `b` at `l ⊓ μ.ae`, then `∫ x in s i, f x ∂μ = μ (s i) • b + o(μ (s i))` at a filter `li` provided that `s i` tends to `l.small_sets` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma filter.tendsto.integral_sub_linear_is_o_ae [normed_space ℝ E] [complete_space E] {μ : measure α} {l : filter α} [l.is_measurably_generated] {f : α → E} {b : E} (h : tendsto f (l ⊓ μ.ae) (𝓝 b)) (hfm : strongly_measurable_at_filter f l μ) (hμ : μ.finite_at_filter l) {s : ι → set α} {li : filter ι} (hs : tendsto s li l.small_sets) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : (λ i, ∫ x in s i, f x ∂μ - m i • b) =o[li] m := begin suffices : (λ s, ∫ x in s, f x ∂μ - (μ s).to_real • b) =o[l.small_sets] (λ s, (μ s).to_real), from (this.comp_tendsto hs).congr' (hsμ.mono $ λ a ha, ha ▸ rfl) hsμ, refine is_o_iff.2 (λ ε ε₀, _), have : ∀ᶠ s in l.small_sets, ∀ᶠ x in μ.ae, x ∈ s → f x ∈ closed_ball b ε := eventually_small_sets_eventually.2 (h.eventually $ closed_ball_mem_nhds _ ε₀), filter_upwards [hμ.eventually, (hμ.integrable_at_filter_of_tendsto_ae hfm h).eventually, hfm.eventually, this], simp only [mem_closed_ball, dist_eq_norm], intros s hμs h_integrable hfm h_norm, rw [← set_integral_const, ← integral_sub h_integrable (integrable_on_const.2 $ or.inr hμs), real.norm_eq_abs, abs_of_nonneg ennreal.to_real_nonneg], exact norm_set_integral_le_of_norm_le_const_ae' hμs h_norm (hfm.sub ae_strongly_measurable_const) end /-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally finite measure and `f` is an almost everywhere measurable function that is continuous at a point `a` within a measurable set `t`, then `∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at a filter `li` provided that `s i` tends to `(𝓝[t] a).small_sets` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma continuous_within_at.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [complete_space E] {μ : measure α} [is_locally_finite_measure μ] {a : α} {t : set α} {f : α → E} (ha : continuous_within_at f t a) (ht : measurable_set t) (hfm : strongly_measurable_at_filter f (𝓝[t] a) μ) {s : ι → set α} {li : filter ι} (hs : tendsto s li (𝓝[t] a).small_sets) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : (λ i, ∫ x in s i, f x ∂μ - m i • f a) =o[li] m := by haveI : (𝓝[t] a).is_measurably_generated := ht.nhds_within_is_measurably_generated _; exact (ha.mono_left inf_le_left).integral_sub_linear_is_o_ae hfm (μ.finite_at_nhds_within a t) hs m hsμ /-- Fundamental theorem of calculus for set integrals, `nhds` version: if `μ` is a locally finite measure and `f` is an almost everywhere measurable function that is continuous at a point `a`, then `∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at `li` provided that `s` tends to `(𝓝 a).small_sets` along `li. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma continuous_at.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [complete_space E] {μ : measure α} [is_locally_finite_measure μ] {a : α} {f : α → E} (ha : continuous_at f a) (hfm : strongly_measurable_at_filter f (𝓝 a) μ) {s : ι → set α} {li : filter ι} (hs : tendsto s li (𝓝 a).small_sets) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : (λ i, ∫ x in s i, f x ∂μ - m i • f a) =o[li] m := (ha.mono_left inf_le_left).integral_sub_linear_is_o_ae hfm (μ.finite_at_nhds a) hs m hsμ /-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally finite measure, `f` is continuous on a measurable set `t`, and `a ∈ t`, then `∫ x in (s i), f x ∂μ = μ (s i) • f a + o(μ (s i))` at `li` provided that `s i` tends to `(𝓝[t] a).small_sets` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma continuous_on.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [complete_space E] [second_countable_topology_either α E] {μ : measure α} [is_locally_finite_measure μ] {a : α} {t : set α} {f : α → E} (hft : continuous_on f t) (ha : a ∈ t) (ht : measurable_set t) {s : ι → set α} {li : filter ι} (hs : tendsto s li (𝓝[t] a).small_sets) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : (λ i, ∫ x in s i, f x ∂μ - m i • f a) =o[li] m := (hft a ha).integral_sub_linear_is_o_ae ht ⟨t, self_mem_nhds_within, hft.ae_strongly_measurable ht⟩ hs m hsμ section /-! ### Continuous linear maps composed with integration The goal of this section is to prove that integration commutes with continuous linear maps. This holds for simple functions. The general result follows from the continuity of all involved operations on the space `L¹`. Note that composition by a continuous linear map on `L¹` is not just the composition, as we are dealing with classes of functions, but it has already been defined as `continuous_linear_map.comp_Lp`. We take advantage of this construction here. -/ open_locale complex_conjugate variables {μ : measure α} {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] [normed_add_comm_group F] [normed_space 𝕜 F] {p : ennreal} namespace continuous_linear_map variables [complete_space F] [normed_space ℝ F] lemma integral_comp_Lp (L : E →L[𝕜] F) (φ : Lp E p μ) : ∫ a, (L.comp_Lp φ) a ∂μ = ∫ a, L (φ a) ∂μ := integral_congr_ae $ coe_fn_comp_Lp _ _ lemma set_integral_comp_Lp (L : E →L[𝕜] F) (φ : Lp E p μ) {s : set α} (hs : measurable_set s) : ∫ a in s, (L.comp_Lp φ) a ∂μ = ∫ a in s, L (φ a) ∂μ := set_integral_congr_ae hs ((L.coe_fn_comp_Lp φ).mono (λ x hx hx2, hx)) lemma continuous_integral_comp_L1 (L : E →L[𝕜] F) : continuous (λ (φ : α →₁[μ] E), ∫ (a : α), L (φ a) ∂μ) := by { rw ← funext L.integral_comp_Lp, exact continuous_integral.comp (L.comp_LpL 1 μ).continuous, } variables [complete_space E] [normed_space ℝ E] lemma integral_comp_comm (L : E →L[𝕜] F) {φ : α → E} (φ_int : integrable φ μ) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := begin apply integrable.induction (λ φ, ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ)), { intros e s s_meas s_finite, rw [integral_indicator_const e s_meas, ← @smul_one_smul E ℝ 𝕜 _ _ _ _ _ (μ s).to_real e, continuous_linear_map.map_smul, @smul_one_smul F ℝ 𝕜 _ _ _ _ _ (μ s).to_real (L e), ← integral_indicator_const (L e) s_meas], congr' 1 with a, rw set.indicator_comp_of_zero L.map_zero }, { intros f g H f_int g_int hf hg, simp [L.map_add, integral_add f_int g_int, integral_add (L.integrable_comp f_int) (L.integrable_comp g_int), hf, hg] }, { exact is_closed_eq L.continuous_integral_comp_L1 (L.continuous.comp continuous_integral) }, { intros f g hfg f_int hf, convert hf using 1 ; clear hf, { exact integral_congr_ae (hfg.fun_comp L).symm }, { rw integral_congr_ae hfg.symm } }, all_goals { assumption } end lemma integral_apply {H : Type*} [normed_add_comm_group H] [normed_space 𝕜 H] {φ : α → H →L[𝕜] E} (φ_int : integrable φ μ) (v : H) : (∫ a, φ a ∂μ) v = ∫ a, φ a v ∂μ := ((continuous_linear_map.apply 𝕜 E v).integral_comp_comm φ_int).symm lemma integral_comp_comm' (L : E →L[𝕜] F) {K} (hL : antilipschitz_with K L) (φ : α → E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := begin by_cases h : integrable φ μ, { exact integral_comp_comm L h }, have : ¬ (integrable (L ∘ φ) μ), by rwa lipschitz_with.integrable_comp_iff_of_antilipschitz L.lipschitz hL (L.map_zero), simp [integral_undef, h, this] end lemma integral_comp_L1_comm (L : E →L[𝕜] F) (φ : α →₁[μ] E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := L.integral_comp_comm (L1.integrable_coe_fn φ) end continuous_linear_map namespace linear_isometry variables [complete_space F] [normed_space ℝ F] [complete_space E] [normed_space ℝ E] lemma integral_comp_comm (L : E →ₗᵢ[𝕜] F) (φ : α → E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := L.to_continuous_linear_map.integral_comp_comm' L.antilipschitz _ end linear_isometry variables [complete_space E] [normed_space ℝ E] [complete_space F] [normed_space ℝ F] @[norm_cast] lemma integral_of_real {f : α → ℝ} : ∫ a, (f a : 𝕜) ∂μ = ↑∫ a, f a ∂μ := (@is_R_or_C.of_real_li 𝕜 _).integral_comp_comm f lemma integral_re {f : α → 𝕜} (hf : integrable f μ) : ∫ a, is_R_or_C.re (f a) ∂μ = is_R_or_C.re ∫ a, f a ∂μ := (@is_R_or_C.re_clm 𝕜 _).integral_comp_comm hf lemma integral_im {f : α → 𝕜} (hf : integrable f μ) : ∫ a, is_R_or_C.im (f a) ∂μ = is_R_or_C.im ∫ a, f a ∂μ := (@is_R_or_C.im_clm 𝕜 _).integral_comp_comm hf lemma integral_conj {f : α → 𝕜} : ∫ a, conj (f a) ∂μ = conj ∫ a, f a ∂μ := (@is_R_or_C.conj_lie 𝕜 _).to_linear_isometry.integral_comp_comm f lemma integral_coe_re_add_coe_im {f : α → 𝕜} (hf : integrable f μ) : ∫ x, (is_R_or_C.re (f x) : 𝕜) ∂μ + ∫ x, is_R_or_C.im (f x) ∂μ * is_R_or_C.I = ∫ x, f x ∂μ := begin rw [mul_comm, ← smul_eq_mul, ← integral_smul, ← integral_add], { congr, ext1 x, rw [smul_eq_mul, mul_comm, is_R_or_C.re_add_im] }, { exact hf.re.of_real }, { exact hf.im.of_real.smul is_R_or_C.I } end lemma integral_re_add_im {f : α → 𝕜} (hf : integrable f μ) : ((∫ x, is_R_or_C.re (f x) ∂μ : ℝ) : 𝕜) + (∫ x, is_R_or_C.im (f x) ∂μ : ℝ) * is_R_or_C.I = ∫ x, f x ∂μ := by { rw [← integral_of_real, ← integral_of_real, integral_coe_re_add_coe_im hf] } lemma set_integral_re_add_im {f : α → 𝕜} {i : set α} (hf : integrable_on f i μ) : ((∫ x in i, is_R_or_C.re (f x) ∂μ : ℝ) : 𝕜) + (∫ x in i, is_R_or_C.im (f x) ∂μ : ℝ) * is_R_or_C.I = ∫ x in i, f x ∂μ := integral_re_add_im hf lemma fst_integral {f : α → E × F} (hf : integrable f μ) : (∫ x, f x ∂μ).1 = ∫ x, (f x).1 ∂μ := ((continuous_linear_map.fst ℝ E F).integral_comp_comm hf).symm lemma snd_integral {f : α → E × F} (hf : integrable f μ) : (∫ x, f x ∂μ).2 = ∫ x, (f x).2 ∂μ := ((continuous_linear_map.snd ℝ E F).integral_comp_comm hf).symm lemma integral_pair {f : α → E} {g : α → F} (hf : integrable f μ) (hg : integrable g μ) : ∫ x, (f x, g x) ∂μ = (∫ x, f x ∂μ, ∫ x, g x ∂μ) := have _ := hf.prod_mk hg, prod.ext (fst_integral this) (snd_integral this) lemma integral_smul_const {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] (f : α → 𝕜) (c : E) : ∫ x, f x • c ∂μ = (∫ x, f x ∂μ) • c := begin by_cases hf : integrable f μ, { exact ((1 : 𝕜 →L[𝕜] 𝕜).smul_right c).integral_comp_comm hf }, { by_cases hc : c = 0, { simp only [hc, integral_zero, smul_zero] }, rw [integral_undef hf, integral_undef, zero_smul], simp_rw [integrable_smul_const hc, hf, not_false_iff] } end section inner variables {E' : Type*} [inner_product_space 𝕜 E'] [complete_space E'] [normed_space ℝ E'] local notation `⟪`x`, `y`⟫` := @inner 𝕜 E' _ x y lemma integral_inner {f : α → E'} (hf : integrable f μ) (c : E') : ∫ x, ⟪c, f x⟫ ∂μ = ⟪c, ∫ x, f x ∂μ⟫ := ((@innerSL 𝕜 E' _ _ c).restrict_scalars ℝ).integral_comp_comm hf lemma integral_eq_zero_of_forall_integral_inner_eq_zero (f : α → E') (hf : integrable f μ) (hf_int : ∀ (c : E'), ∫ x, ⟪c, f x⟫ ∂μ = 0) : ∫ x, f x ∂μ = 0 := by { specialize hf_int (∫ x, f x ∂μ), rwa [integral_inner hf, inner_self_eq_zero] at hf_int } end inner lemma integral_with_density_eq_integral_smul {f : α → ℝ≥0} (f_meas : measurable f) (g : α → E) : ∫ a, g a ∂(μ.with_density (λ x, f x)) = ∫ a, f a • g a ∂μ := begin by_cases hg : integrable g (μ.with_density (λ x, f x)), swap, { rw [integral_undef hg, integral_undef], rwa [← integrable_with_density_iff_integrable_smul f_meas]; apply_instance }, refine integrable.induction _ _ _ _ _ hg, { assume c s s_meas hs, rw integral_indicator s_meas, simp_rw [← indicator_smul_apply, integral_indicator s_meas], simp only [s_meas, integral_const, measure.restrict_apply', univ_inter, with_density_apply], rw [lintegral_coe_eq_integral, ennreal.to_real_of_real, ← integral_smul_const], { refl }, { exact integral_nonneg (λ x, nnreal.coe_nonneg _) }, { refine ⟨(f_meas.coe_nnreal_real).ae_measurable.ae_strongly_measurable, _⟩, rw with_density_apply _ s_meas at hs, rw has_finite_integral, convert hs, ext1 x, simp only [nnreal.nnnorm_eq] } }, { assume u u' h_disj u_int u'_int h h', change ∫ (a : α), (u a + u' a) ∂μ.with_density (λ (x : α), ↑(f x)) = ∫ (a : α), f a • (u a + u' a) ∂μ, simp_rw [smul_add], rw [integral_add u_int u'_int, h, h', integral_add], { exact (integrable_with_density_iff_integrable_smul f_meas).1 u_int }, { exact (integrable_with_density_iff_integrable_smul f_meas).1 u'_int } }, { have C1 : continuous (λ (u : Lp E 1 (μ.with_density (λ x, f x))), ∫ x, u x ∂(μ.with_density (λ x, f x))) := continuous_integral, have C2 : continuous (λ (u : Lp E 1 (μ.with_density (λ x, f x))), ∫ x, f x • u x ∂μ), { have : continuous ((λ (u : Lp E 1 μ), ∫ x, u x ∂μ) ∘ (with_density_smul_li μ f_meas)) := continuous_integral.comp (with_density_smul_li μ f_meas).continuous, convert this, ext1 u, simp only [function.comp_app, with_density_smul_li_apply], exact integral_congr_ae (mem_ℒ1_smul_of_L1_with_density f_meas u).coe_fn_to_Lp.symm }, exact is_closed_eq C1 C2 }, { assume u v huv u_int hu, rw [← integral_congr_ae huv, hu], apply integral_congr_ae, filter_upwards [(ae_with_density_iff f_meas.coe_nnreal_ennreal).1 huv] with x hx, rcases eq_or_ne (f x) 0 with h'x|h'x, { simp only [h'x, zero_smul]}, { rw [hx _], simpa only [ne.def, ennreal.coe_eq_zero] using h'x } } end lemma integral_with_density_eq_integral_smul₀ {f : α → ℝ≥0} (hf : ae_measurable f μ) (g : α → E) : ∫ a, g a ∂(μ.with_density (λ x, f x)) = ∫ a, f a • g a ∂μ := begin let f' := hf.mk _, calc ∫ a, g a ∂(μ.with_density (λ x, f x)) = ∫ a, g a ∂(μ.with_density (λ x, f' x)) : begin congr' 1, apply with_density_congr_ae, filter_upwards [hf.ae_eq_mk] with x hx, rw hx, end ... = ∫ a, f' a • g a ∂μ : integral_with_density_eq_integral_smul hf.measurable_mk _ ... = ∫ a, f a • g a ∂μ : begin apply integral_congr_ae, filter_upwards [hf.ae_eq_mk] with x hx, rw hx, end end lemma set_integral_with_density_eq_set_integral_smul {f : α → ℝ≥0} (f_meas : measurable f) (g : α → E) {s : set α} (hs : measurable_set s) : ∫ a in s, g a ∂(μ.with_density (λ x, f x)) = ∫ a in s, f a • g a ∂μ := by rw [restrict_with_density hs, integral_with_density_eq_integral_smul f_meas] lemma set_integral_with_density_eq_set_integral_smul₀ {f : α → ℝ≥0} {s : set α} (hf : ae_measurable f (μ.restrict s)) (g : α → E) (hs : measurable_set s) : ∫ a in s, g a ∂(μ.with_density (λ x, f x)) = ∫ a in s, f a • g a ∂μ := by rw [restrict_with_density hs, integral_with_density_eq_integral_smul₀ hf] end section thickened_indicator variables [pseudo_emetric_space α] lemma measure_le_lintegral_thickened_indicator_aux (μ : measure α) {E : set α} (E_mble : measurable_set E) (δ : ℝ) : μ E ≤ ∫⁻ a, (thickened_indicator_aux δ E a : ℝ≥0∞) ∂μ := begin convert_to lintegral μ (E.indicator (λ _, (1 : ℝ≥0∞))) ≤ lintegral μ (thickened_indicator_aux δ E), { rw [lintegral_indicator _ E_mble], simp only [lintegral_one, measure.restrict_apply, measurable_set.univ, univ_inter], }, { apply lintegral_mono, apply indicator_le_thickened_indicator_aux, }, end lemma measure_le_lintegral_thickened_indicator (μ : measure α) {E : set α} (E_mble : measurable_set E) {δ : ℝ} (δ_pos : 0 < δ) : μ E ≤ ∫⁻ a, (thickened_indicator δ_pos E a : ℝ≥0∞) ∂μ := begin convert measure_le_lintegral_thickened_indicator_aux μ E_mble δ, dsimp, simp only [thickened_indicator_aux_lt_top.ne, ennreal.coe_to_nnreal, ne.def, not_false_iff], end end thickened_indicator section bilinear_map namespace measure_theory variables {f : β → ℝ} {m m0 : measurable_space β} {μ : measure β} lemma integrable.simple_func_mul (g : simple_func β ℝ) (hf : integrable f μ) : integrable (g * f) μ := begin refine simple_func.induction (λ c s hs, _) (λ g₁ g₂ h_disj h_int₁ h_int₂, (h_int₁.add h_int₂).congr (by rw [simple_func.coe_add, add_mul])) g, simp only [simple_func.const_zero, simple_func.coe_piecewise, simple_func.coe_const, simple_func.coe_zero, set.piecewise_eq_indicator], have : set.indicator s (function.const β c) * f = s.indicator (c • f), { ext1 x, by_cases hx : x ∈ s, { simp only [hx, pi.mul_apply, set.indicator_of_mem, pi.smul_apply, algebra.id.smul_eq_mul] }, { simp only [hx, pi.mul_apply, set.indicator_of_not_mem, not_false_iff, zero_mul], }, }, rw [this, integrable_indicator_iff hs], exact (hf.smul c).integrable_on, end lemma integrable.simple_func_mul' (hm : m ≤ m0) (g : @simple_func β m ℝ) (hf : integrable f μ) : integrable (g * f) μ := by { rw ← simple_func.coe_to_larger_space_eq hm g, exact hf.simple_func_mul (g.to_larger_space hm) } end measure_theory end bilinear_map
1d72fbe113a548853d4aa6f790e3f7b98d8befab
bb31430994044506fa42fd667e2d556327e18dfe
/src/data/polynomial/algebra_map.lean
cd83ab6081dc163a579fc1349c7ec0ebe87dfb17
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
13,567
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import algebra.algebra.pi import ring_theory.adjoin.basic import data.polynomial.eval /-! # Theory of univariate polynomials We show that `A[X]` is an R-algebra when `A` is an R-algebra. We promote `eval₂` to an algebra hom in `aeval`. -/ noncomputable theory open finset open_locale big_operators polynomial namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {A' B' : Type*} {a b : R} {n : ℕ} variables [comm_semiring A'] [semiring B'] section comm_semiring variables [comm_semiring R] {p q r : R[X]} variables [semiring A] [algebra R A] /-- Note that this instance also provides `algebra R R[X]`. -/ instance algebra_of_algebra : algebra R A[X] := { smul_def' := λ r p, to_finsupp_injective $ begin dsimp only [ring_hom.to_fun_eq_coe, ring_hom.comp_apply], rw [to_finsupp_smul, to_finsupp_mul, to_finsupp_C], exact algebra.smul_def' _ _, end, commutes' := λ r p, to_finsupp_injective $ begin dsimp only [ring_hom.to_fun_eq_coe, ring_hom.comp_apply], simp_rw [to_finsupp_mul, to_finsupp_C], convert algebra.commutes' r p.to_finsupp, end, to_ring_hom := C.comp (algebra_map R A) } lemma algebra_map_apply (r : R) : algebra_map R A[X] r = C (algebra_map R A r) := rfl @[simp] lemma to_finsupp_algebra_map (r : R) : (algebra_map R A[X] r).to_finsupp = algebra_map R _ r := show to_finsupp (C (algebra_map _ _ r)) = _, by { rw to_finsupp_C, refl } lemma of_finsupp_algebra_map (r : R) : (⟨algebra_map R _ r⟩ : A[X]) = algebra_map R A[X] r := to_finsupp_injective (to_finsupp_algebra_map _).symm /-- When we have `[comm_semiring R]`, the function `C` is the same as `algebra_map R R[X]`. (But note that `C` is defined when `R` is not necessarily commutative, in which case `algebra_map` is not available.) -/ lemma C_eq_algebra_map (r : R) : C r = algebra_map R R[X] r := rfl variables {R} /-- Extensionality lemma for algebra maps out of `A'[X]` over a smaller base ring than `A'` -/ @[ext] lemma alg_hom_ext' [algebra R A'] [algebra R B'] {f g : A'[X] →ₐ[R] B'} (h₁ : f.comp (is_scalar_tower.to_alg_hom R A' A'[X]) = g.comp (is_scalar_tower.to_alg_hom R A' A'[X])) (h₂ : f X = g X) : f = g := alg_hom.coe_ring_hom_injective (polynomial.ring_hom_ext' (congr_arg alg_hom.to_ring_hom h₁) h₂) variable (R) /-- Algebra isomorphism between `R[X]` and `add_monoid_algebra R ℕ`. This is just an implementation detail, but it can be useful to transfer results from `finsupp` to polynomials. -/ @[simps] def to_finsupp_iso_alg : R[X] ≃ₐ[R] add_monoid_algebra R ℕ := { commutes' := λ r, begin dsimp, exact to_finsupp_algebra_map _, end, ..to_finsupp_iso R } variable {R} instance [nontrivial A] : nontrivial (subalgebra R A[X]) := ⟨⟨⊥, ⊤, begin rw [ne.def, set_like.ext_iff, not_forall], refine ⟨X, _⟩, simp only [algebra.mem_bot, not_exists, set.mem_range, iff_true, algebra.mem_top, algebra_map_apply, not_forall], intro x, rw [ext_iff, not_forall], refine ⟨1, _⟩, simp [coeff_C], end⟩⟩ @[simp] lemma alg_hom_eval₂_algebra_map {R A B : Type*} [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] (p : R[X]) (f : A →ₐ[R] B) (a : A) : f (eval₂ (algebra_map R A) a p) = eval₂ (algebra_map R B) (f a) p := begin dsimp [eval₂, sum], simp only [f.map_sum, f.map_mul, f.map_pow, eq_int_cast, map_int_cast, alg_hom.commutes], end @[simp] lemma eval₂_algebra_map_X {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] (p : R[X]) (f : R[X] →ₐ[R] A) : eval₂ (algebra_map R A) (f X) p = f p := begin conv_rhs { rw [←polynomial.sum_C_mul_X_pow_eq p], }, dsimp [eval₂, sum], simp only [f.map_sum, f.map_mul, f.map_pow, eq_int_cast, map_int_cast], simp [polynomial.C_eq_algebra_map], end -- these used to be about `algebra_map ℤ R`, but now the simp-normal form is `int.cast_ring_hom R`. @[simp] lemma ring_hom_eval₂_cast_int_ring_hom {R S : Type*} [ring R] [ring S] (p : ℤ[X]) (f : R →+* S) (r : R) : f (eval₂ (int.cast_ring_hom R) r p) = eval₂ (int.cast_ring_hom S) (f r) p := alg_hom_eval₂_algebra_map p f.to_int_alg_hom r @[simp] lemma eval₂_int_cast_ring_hom_X {R : Type*} [ring R] (p : ℤ[X]) (f : ℤ[X] →+* R) : eval₂ (int.cast_ring_hom R) (f X) p = f p := eval₂_algebra_map_X p f.to_int_alg_hom end comm_semiring section aeval variables [comm_semiring R] {p q : R[X]} variables [semiring A] [algebra R A] variables {B : Type*} [semiring B] [algebra R B] variables (x : A) /-- Given a valuation `x` of the variable in an `R`-algebra `A`, `aeval R A x` is the unique `R`-algebra homomorphism from `R[X]` to `A` sending `X` to `x`. This is a stronger variant of the linear map `polynomial.leval`. -/ def aeval : R[X] →ₐ[R] A := { commutes' := λ r, eval₂_C _ _, ..eval₂_ring_hom' (algebra_map R A) x (λ a, algebra.commutes _ _) } variables {R A} @[simp] lemma adjoin_X : algebra.adjoin R ({X} : set R[X]) = ⊤ := begin refine top_unique (λ p hp, _), set S := algebra.adjoin R ({X} : set R[X]), rw [← sum_monomial_eq p], simp only [← smul_X_eq_monomial, sum], exact S.sum_mem (λ n hn, S.smul_mem (S.pow_mem (algebra.subset_adjoin rfl) _) _) end @[ext] lemma alg_hom_ext {f g : R[X] →ₐ[R] A} (h : f X = g X) : f = g := alg_hom.ext_of_adjoin_eq_top adjoin_X $ λ p hp, (set.mem_singleton_iff.1 hp).symm ▸ h theorem aeval_def (p : R[X]) : aeval x p = eval₂ (algebra_map R A) x p := rfl @[simp] lemma aeval_zero : aeval x (0 : R[X]) = 0 := alg_hom.map_zero (aeval x) @[simp] lemma aeval_X : aeval x (X : R[X]) = x := eval₂_X _ x @[simp] lemma aeval_C (r : R) : aeval x (C r) = algebra_map R A r := eval₂_C _ x @[simp] lemma aeval_monomial {n : ℕ} {r : R} : aeval x (monomial n r) = (algebra_map _ _ r) * x^n := eval₂_monomial _ _ @[simp] lemma aeval_X_pow {n : ℕ} : aeval x ((X : R[X])^n) = x^n := eval₂_X_pow _ _ @[simp] lemma aeval_add : aeval x (p + q) = aeval x p + aeval x q := alg_hom.map_add _ _ _ @[simp] lemma aeval_one : aeval x (1 : R[X]) = 1 := alg_hom.map_one _ @[simp] lemma aeval_bit0 : aeval x (bit0 p) = bit0 (aeval x p) := alg_hom.map_bit0 _ _ @[simp] lemma aeval_bit1 : aeval x (bit1 p) = bit1 (aeval x p) := alg_hom.map_bit1 _ _ @[simp] lemma aeval_nat_cast (n : ℕ) : aeval x (n : R[X]) = n := map_nat_cast _ _ lemma aeval_mul : aeval x (p * q) = aeval x p * aeval x q := alg_hom.map_mul _ _ _ lemma aeval_comp {A : Type*} [comm_semiring A] [algebra R A] (x : A) : aeval x (p.comp q) = (aeval (aeval x q) p) := eval₂_comp (algebra_map R A) theorem aeval_alg_hom (f : A →ₐ[R] B) (x : A) : aeval (f x) = f.comp (aeval x) := alg_hom_ext $ by simp only [aeval_X, alg_hom.comp_apply] @[simp] theorem aeval_X_left : aeval (X : R[X]) = alg_hom.id R R[X] := alg_hom_ext $ aeval_X X theorem aeval_X_left_apply (p : R[X]) : aeval X p = p := alg_hom.congr_fun (@aeval_X_left R _) p theorem eval_unique (φ : R[X] →ₐ[R] A) (p) : φ p = eval₂ (algebra_map R A) (φ X) p := by rw [← aeval_def, aeval_alg_hom, aeval_X_left, alg_hom.comp_id] theorem aeval_alg_hom_apply {F : Type*} [alg_hom_class F R A B] (f : F) (x : A) (p : R[X]) : aeval (f x) p = f (aeval x p) := begin refine polynomial.induction_on p (by simp) (λ p q hp hq, _) (by simp), rw [map_add, hp, hq, ← map_add, ← map_add] end theorem aeval_alg_equiv (f : A ≃ₐ[R] B) (x : A) : aeval (f x) = (f : A →ₐ[R] B).comp (aeval x) := aeval_alg_hom (f : A →ₐ[R] B) x lemma aeval_algebra_map_apply_eq_algebra_map_eval (x : R) (p : R[X]) : aeval (algebra_map R A x) p = algebra_map R A (p.eval x) := aeval_alg_hom_apply (algebra.of_id R A) x p @[simp] lemma coe_aeval_eq_eval (r : R) : (aeval r : R[X] → R) = eval r := rfl @[simp] lemma aeval_fn_apply {X : Type*} (g : R[X]) (f : X → R) (x : X) : ((aeval f) g) x = aeval (f x) g := (aeval_alg_hom_apply (pi.eval_alg_hom R (λ _, R) x) f g).symm @[norm_cast] lemma aeval_subalgebra_coe (g : R[X]) {A : Type*} [semiring A] [algebra R A] (s : subalgebra R A) (f : s) : (aeval f g : A) = aeval (f : A) g := (aeval_alg_hom_apply s.val f g).symm lemma coeff_zero_eq_aeval_zero (p : R[X]) : p.coeff 0 = aeval 0 p := by simp [coeff_zero_eq_eval_zero] lemma coeff_zero_eq_aeval_zero' (p : R[X]) : algebra_map R A (p.coeff 0) = aeval (0 : A) p := by simp [aeval_def] variable (R) theorem _root_.algebra.adjoin_singleton_eq_range_aeval (x : A) : algebra.adjoin R {x} = (polynomial.aeval x).range := by rw [← algebra.map_top, ← adjoin_X, alg_hom.map_adjoin, set.image_singleton, aeval_X] variable {R} section comm_semiring variables [comm_semiring S] {f : R →+* S} lemma aeval_eq_sum_range [algebra R S] {p : R[X]} (x : S) : aeval x p = ∑ i in finset.range (p.nat_degree + 1), p.coeff i • x ^ i := by { simp_rw algebra.smul_def, exact eval₂_eq_sum_range (algebra_map R S) x } lemma aeval_eq_sum_range' [algebra R S] {p : R[X]} {n : ℕ} (hn : p.nat_degree < n) (x : S) : aeval x p = ∑ i in finset.range n, p.coeff i • x ^ i := by { simp_rw algebra.smul_def, exact eval₂_eq_sum_range' (algebra_map R S) hn x } lemma is_root_of_eval₂_map_eq_zero (hf : function.injective f) {r : R} : eval₂ f (f r) p = 0 → p.is_root r := begin intro h, apply hf, rw [←eval₂_hom, h, f.map_zero], end lemma is_root_of_aeval_algebra_map_eq_zero [algebra R S] {p : R[X]} (inj : function.injective (algebra_map R S)) {r : R} (hr : aeval (algebra_map R S r) p = 0) : p.is_root r := is_root_of_eval₂_map_eq_zero inj hr section aeval_tower variables [algebra S R] [algebra S A'] [algebra S B'] /-- Version of `aeval` for defining algebra homs out of `R[X]` over a smaller base ring than `R`. -/ def aeval_tower (f : R →ₐ[S] A') (x : A') : R[X] →ₐ[S] A' := { commutes' := λ r, by simp [algebra_map_apply], ..eval₂_ring_hom ↑f x } variables (g : R →ₐ[S] A') (y : A') @[simp] lemma aeval_tower_X : aeval_tower g y X = y := eval₂_X _ _ @[simp] lemma aeval_tower_C (x : R) : aeval_tower g y (C x) = g x := eval₂_C _ _ @[simp] lemma aeval_tower_comp_C : ((aeval_tower g y : R[X] →+* A').comp C) = g := ring_hom.ext $ aeval_tower_C _ _ @[simp] lemma aeval_tower_algebra_map (x : R) : aeval_tower g y (algebra_map R R[X] x) = g x := eval₂_C _ _ @[simp] lemma aeval_tower_comp_algebra_map : (aeval_tower g y : R[X] →+* A').comp (algebra_map R R[X]) = g := aeval_tower_comp_C _ _ lemma aeval_tower_to_alg_hom (x : R) : aeval_tower g y (is_scalar_tower.to_alg_hom S R R[X] x) = g x := aeval_tower_algebra_map _ _ _ @[simp] lemma aeval_tower_comp_to_alg_hom : (aeval_tower g y).comp (is_scalar_tower.to_alg_hom S R R[X]) = g := alg_hom.coe_ring_hom_injective $ aeval_tower_comp_algebra_map _ _ @[simp] lemma aeval_tower_id : aeval_tower (alg_hom.id S S) = aeval := by { ext, simp only [eval_X, aeval_tower_X, coe_aeval_eq_eval], } @[simp] lemma aeval_tower_of_id : aeval_tower (algebra.of_id S A') = aeval := by { ext, simp only [aeval_X, aeval_tower_X], } end aeval_tower end comm_semiring section comm_ring variables [comm_ring S] {f : R →+* S} lemma dvd_term_of_dvd_eval_of_dvd_terms {z p : S} {f : S[X]} (i : ℕ) (dvd_eval : p ∣ f.eval z) (dvd_terms : ∀ (j ≠ i), p ∣ f.coeff j * z ^ j) : p ∣ f.coeff i * z ^ i := begin by_cases hi : i ∈ f.support, { rw [eval, eval₂, sum] at dvd_eval, rw [←finset.insert_erase hi, finset.sum_insert (finset.not_mem_erase _ _)] at dvd_eval, refine (dvd_add_left _).mp dvd_eval, apply finset.dvd_sum, intros j hj, exact dvd_terms j (finset.ne_of_mem_erase hj) }, { convert dvd_zero p, rw not_mem_support_iff at hi, simp [hi] } end lemma dvd_term_of_is_root_of_dvd_terms {r p : S} {f : S[X]} (i : ℕ) (hr : f.is_root r) (h : ∀ (j ≠ i), p ∣ f.coeff j * r ^ j) : p ∣ f.coeff i * r ^ i := dvd_term_of_dvd_eval_of_dvd_terms i (eq.symm hr ▸ dvd_zero p) h end comm_ring end aeval section ring variables [ring R] /-- The evaluation map is not generally multiplicative when the coefficient ring is noncommutative, but nevertheless any polynomial of the form `p * (X - monomial 0 r)` is sent to zero when evaluated at `r`. This is the key step in our proof of the Cayley-Hamilton theorem. -/ lemma eval_mul_X_sub_C {p : R[X]} (r : R) : (p * (X - C r)).eval r = 0 := begin simp only [eval, eval₂, ring_hom.id_apply], have bound := calc (p * (X - C r)).nat_degree ≤ p.nat_degree + (X - C r).nat_degree : nat_degree_mul_le ... ≤ p.nat_degree + 1 : add_le_add_left (nat_degree_X_sub_C_le _) _ ... < p.nat_degree + 2 : lt_add_one _, rw sum_over_range' _ _ (p.nat_degree + 2) bound, swap, { simp, }, rw sum_range_succ', conv_lhs { congr, apply_congr, skip, rw [coeff_mul_X_sub_C, sub_mul, mul_assoc, ←pow_succ], }, simp [sum_range_sub', coeff_monomial], end theorem not_is_unit_X_sub_C [nontrivial R] (r : R) : ¬ is_unit (X - C r) := λ ⟨⟨_, g, hfg, hgf⟩, rfl⟩, zero_ne_one' R $ by erw [← eval_mul_X_sub_C, hgf, eval_one] end ring lemma aeval_endomorphism {M : Type*} [comm_ring R] [add_comm_group M] [module R M] (f : M →ₗ[R] M) (v : M) (p : R[X]) : aeval f p v = p.sum (λ n b, b • (f ^ n) v) := begin rw [aeval_def, eval₂], exact (linear_map.applyₗ v).map_sum, end end polynomial
0e8cbbd2c21c02531afd72a235438455d3798005
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/category_theory/category/Cat.lean
a537b08bbf14c8066f183024f15059a09798e505
[ "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,371
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.concrete_category /-! # Category of categories This file contains definition of category `Cat` of all categories. In this category objects are categories and morphisms are functors between these categories. ## Implementation notes Though `Cat` is not a concrete category, we use `bundled` to define its carrier type. -/ universes v u namespace category_theory /-- Category of categories. -/ def Cat := bundled category.{v u} namespace Cat instance str (C : Cat.{v u}) : category.{v u} C.α := C.str /-- Construct a bundled `Cat` from the underlying type and the typeclass. -/ def of (C : Type u) [category.{v} C] : Cat.{v u} := bundled.of C /-- Category structure on `Cat` -/ instance category : large_category.{max v u} Cat.{v u} := { hom := λ C D, C.α ⥤ D.α, id := λ C, 𝟭 C.α, comp := λ C D E F G, F ⋙ G, id_comp' := λ C D F, by cases F; refl, comp_id' := λ C D F, by cases F; refl, assoc' := by intros; refl } /-- Functor that gets the set of objects of a category. It is not called `forget`, because it is not a faithful functor. -/ def objects : Cat.{v u} ⥤ Type u := { obj := bundled.α, map := λ C D F, F.obj } end Cat end category_theory